题解:AT_abc413_c [ABC413C] Large Queue

· · 题解

思路:

范围这么大,肯定不能一个个加,所以我们考虑用乘法。用一个 pair,存这个数,以及这个数有几个。加的时候看一下能不能用完一组,如果能就直接把这组数加到答案里去,不能就把这个数的个数减去能拿走的个数。

实现用 queue 或 deque 都行,这里用的是 deque。

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define gc getchar
#define pb push_back
template<typename T>inline void read(T&x){x=0;int f=1;char ch=gc();while(!isdigit(ch)){if(ch=='-') f=-1;ch=gc();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=gc();}x*=f;}
template<typename T,typename ...T1>inline void read(T&x,T1&...x1){read(x);read(x1...);}
const int ri=2e5+5;
deque<pair<LL,LL>> e; 
LL n,x,y,minn,opt;
int main(){
    read(n);
    for(int i=1;i<=n;i++){
        read(opt);
        if(opt==1){
            read(x,y);
            e.pb({y,x});
        }else{
            LL sum=0;
            read(x);
            while(x){
                minn=min(e.front().second,x);
                x-=minn;
                e.front().second-=minn;
                sum+=e.front().first*minn;
                if(e.front().second==0) e.pop_front();
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}