关于上午模拟赛T2
  • 板块学术版
  • 楼主JoeBiden2020
  • 当前回复11
  • 已保存回复11
  • 发布时间2021/9/20 12:15
  • 上次更新2023/11/4 06:05:49
查看原帖
关于上午模拟赛T2
432183
JoeBiden2020楼主2021/9/20 12:15

我使用了分块算法,发现时间足够但是只得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;
		}
	}
}
2021/9/20 12:15
加载中...