线段树 95pts MLE求调(玄关)
  • 板块P4939 Agent2
  • 楼主wuhupai
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/10/28 09:06
  • 上次更新2023/11/2 11:08:18
查看原帖
线段树 95pts MLE求调(玄关)
544310
wuhupai楼主2023/10/28 09:06

服了,心态炸了,不想调了。

#include<bits/stdc++.h>
using namespace std;
int n,m,op,x,y;
int d[40000005];
void push_up(int p){
	d[p]=d[p<<1]+d[p<<1|1];
}
void update(int l,int r,int p,int ad,int s){
	if(l==s&&r==s){
		d[p]+=ad;
		return;
	}
	int mid=(l+r)>>1;
	if(s<=mid) update(l,mid,p<<1,ad,s);
	if(s>mid) update(mid+1,r,p<<1|1,ad,s);
	push_up(p);
}
int getsum(int l,int r,int p,int s,int t){
	if(l>=s&&r<=t){
		return d[p];
	}
	int mid=(l+r)>>1,ans=0;
	if(s<=mid) ans+=getsum(l,mid,p<<1,s,t);
	if(t>mid) ans+=getsum(mid+1,r,p<<1|1,s,t);
	return ans;
}
int main(){
	cin>>n>>m;
	while(m--){
		cin>>op;
		if(op==0){
			cin>>x>>y;
			update(1,n,1,1,x);
			if(y<n) update(1,n,1,-1,y+1);
		}else{
			cin>>x;
			cout<<getsum(1,n,1,1,x)<<endl;
		}
	}
	
	return 0;
} 
/*
5 3
1 5 4 2 3
1 1 1 1.5
*/
2023/10/28 09:06
加载中...