50分代码求调
查看原帖
50分代码求调
445650
I_never_left楼主2022/8/15 20:55
#include<cmath>
#include<stack>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int n;
struct tuan{
   ll L,R,sum,len;
   tuan(ll L,ll R,ll sum,ll len): L(L),R(R),sum(sum),len(len) {}
};
stack<tuan> s;
int main(){
   cin>>n;
   int op;
   for(int i=1;i<=n;++i){
   	cin>>op;
   	if(op==1){
   		ll l,r;
   		cin>>l>>r;
   		ll su=(r-l+1)*(r+l)/2;
   		tuan tmp={l,r,su,r-l+1};
   		s.push(tmp); 
   	}
   	else{
   		ll k;
   		cin>>k;
   		ll ans=0ll;
   		while(k>=s.top().len){
   			tuan t=s.top();
   			s.pop();
   			ans+=t.sum;
   			k-=t.len;
   		}
   		if(k>0){
   			tuan t=s.top();
   			s.pop();
   			for(ll j=t.R;j>=t.R-k+1;--j)
   				ans+=j;
   			t.R-=k;
   			t.len-=k;
   			t.sum=(t.R-t.L+1)*(t.R+t.L)/2;
   			s.push(t); 
   		}
   		cout<<ans<<endl;
   	}
   }
   return 0;
}
2022/8/15 20:55
加载中...