我使用了分块算法,发现时间足够但是只得45分,求问题。
思路是用二元组存储分块,对于每一次询问分刚好一块,多块,不足一块判断。
#include<cstring>
#include<iostream>
#include<stack>
#define pass
using namespace std;
struct node{
long long l,r;
};
stack<node> a;
bool flag;
int main(){
node temp;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("stack2.in","r",stdin);
// freopen("stack2.out","w",stdout);
long long int n,l,r,k,opt,ans=0;
cin>>n;
while(n--){
ans=0;
cin>>opt;
if(opt==1){
cin>>l>>r;
a.push(node{l,r});
}
else{
long long mp=a.top().r-a.top().l+1;
cin>>k;
if(flag){
a.push(temp);
flag=0;
}
if(k==mp){
temp=a.top();
ans=((temp.r-temp.l+1)*(temp.l+temp.r))/2;
a.pop();
}
else if(k<mp){
temp=a.top();
temp.r-=k;
k=0;
if(temp.l==temp.r)ans-=l;
ans=((a.top().r-a.top().l+1)*(a.top().l+a.top().r))/2-((temp.r-temp.l+1)*(temp.l+temp.r))/2;
a.pop();
a.push(temp);
}
else if(k>mp){
while(k>0){
if(k<a.top().r-a.top().l+1){
temp=a.top();
temp.r-=k;
k=0;
if(temp.l==temp.r)ans-=l;
ans+=((a.top().r-a.top().l+1)*(a.top().l+a.top().r))/2-((temp.r-temp.l+1)*(temp.l+temp.r))/2;
a.pop();
flag=1;
a.push(temp);
}
else{
ans+=((a.top().r-a.top().l+1)*(a.top().l+a.top().r))/2;
k-=a.top().r-a.top().l+1;
a.pop();
}
}
}
cout<<ans<<endl;
}
}
}