嘤嘤嘤,这个CDQ样例过了,但是全WA了,求救!
查看原帖
嘤嘤嘤,这个CDQ样例过了,但是全WA了,求救!
93701
Morgen_Kornblume楼主2022/4/8 14:10

自认为代码比较容易看得懂……

捉虫大佬可以得到我这个全洛谷最菜金钩的关注。

#include<bits/stdc++.h>
using namespace std;

const int maxq=200010,maxv=2000010;

int ans[maxq];int qt=0;
int w;

struct Binary_Indexed_Tree{
	int dat[maxv];
	
	void init(){
		memset(dat,0,sizeof(dat));
	}
	
	int lowbit(int x){
		return x&(-x);
	}
	
	void modify(int pos,int der){
		while(pos<=w){
			dat[pos]+=der;
			pos+=lowbit(pos);
		}
	}
	
	int query(int x){
		int res=0;
		while(x){
			res+=dat[x];
			x-=lowbit(x);
		}
		return res;
	}
	
}bit;

struct opera{
	int ptr;
	int x,y,a;
}op[maxq<<1];
int tot=0;

void cdq(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	cdq(l,mid);cdq(mid+1,r);
	sort(op+l,op+mid+1,[](opera tp1,opera tp2){return tp1.x<tp2.x;});
	sort(op+mid+1,op+r+1,[](opera tp1,opera tp2){return tp1.x<tp2.x;});
	int j=l;
	for(int i=mid+1;i<=r;i++){
		if(!op[i].ptr)continue;
		while(op[j].x<=op[i].x&&j<=mid){
			if(op[j].ptr!=0){
				j++;
				continue;
			}
			bit.modify(op[j].y,op[j].a);
			j++;
		}
		ans[op[i].ptr]+=op[i].a*bit.query(op[i].y);
	}
	for(int i=l;i<j;i++){
		if(op[i].ptr!=0)continue;
		bit.modify(op[i].y,-op[i].a);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
	int opt,a,b,c,d;
	while(1){
		cin>>opt;
		if(opt==0){
			cin>>w;
		}
		else if(opt==1){
			tot++;
			op[tot].ptr=0;
			cin>>op[tot].x>>op[tot].y>>op[tot].a;	
		}
		else if(opt==2){
			tot++;qt++;
			op[tot].ptr=qt;
			cin>>op[tot].x>>op[tot].y;
			op[tot].x--;op[tot].y--;op[tot].a=-1;
			tot++;
			op[tot].ptr=qt;
			cin>>op[tot].x>>op[tot].y;
			op[tot].a=1;
		}
		else if(opt==3){
			break;
		}
		else{
			cout<<"年轻人不讲武德,偷袭!!!"<<endl;
			return 0;
		}
	}
	bit.init();
	cdq(1,tot);
	
	for(int i=1;i<=qt;i++){
		cout<<ans[i]<<endl;
	}
	
	return 0;
}
2022/4/8 14:10
加载中...