主席树只过hack,TLE求助
查看原帖
主席树只过hack,TLE求助
672877
elswzl楼主2023/8/9 15:05
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
inline int rd()
{
    int xr=0,F=1; char cr;
    while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
    while(cr>='0'&&cr<='9') 
        xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
    return xr*F;
}
int cnt,root[N],n,m;
int l[N],r[N],num[N],s[N];
struct node{
	int l,r,lson,rson;
	long long sum;
}t[N*30];
int pushup(int &p){
	return t[p].sum=t[t[p].lson].sum+t[t[p].rson].sum;
}
int build(int &p,const int &l,const int &r){
	if(p==0)p=++cnt; 
	t[p].l=l;t[p].r=r;
	if(l==r){
		return 0;
	}
	int mid=l+r>>1;
	build(t[p].lson,l,mid);build(t[p].rson,mid+1,r);
	return pushup(p);
}
int add(const int &x,const int &d,const int &locp,int &p){
	if(t[p].l==t[p].r){
		return t[p].sum=d;
	}
	int mid=t[locp].l+t[locp].r>>1;
	if(mid>=x){
		t[p].rson=t[locp].rson;
		t[p].lson=++cnt;
		t[t[p].lson].l=t[t[locp].lson].l;
		t[t[p].lson].r=t[t[locp].lson].r;
		//cout<<" "<<p<<" "<<locp<<" "<<t[p].lson<<" "<<t[p].rson<<" "<<t[locp].lson<<" "<<t[locp].rson<<endl;
		add(x,d,t[locp].lson,t[p].lson);
	}
	else{
		t[p].lson=t[locp].lson;
		t[p].rson=++cnt;
		t[t[p].rson].l=t[t[locp].rson].l;
		t[t[p].rson].r=t[t[locp].rson].r;
		//cout<<" "<<p<<" "<<locp<<" "<<t[p].lson<<" "<<t[p].rson<<" "<<t[locp].lson<<" "<<t[locp].rson<<endl;
		add(x,d,t[locp].rson,t[p].rson);
	}
	return pushup(p);
}
int query(int L,int R,int p){
//	cout<<p<<" "<<t[p].l<<" "<<t[p].r<<" "<<L<<" "<<R<<endl;Sleep(100);
	if(t[p].l>=L&&t[p].r<=R)return t[p].sum;
	int mid=t[p].l+t[p].r>>1;
	int ret=0;
	if(mid>=L)ret+=query(L,R,t[p].lson);
	if(mid<R)ret+=query(L,R,t[p].rson);
	return ret;
}
int main(){
	n=rd();m=rd();
//	for(int k=1;k<=n;k++)for(int i=1;i<=20;i++)for(int j=1;j<=20;j++);return 0;
	build(root[0],1,m);
	for(int i=1;i<=n;i++){
		l[i]=rd();r[i]=rd();s[i]=rd();
	}
	for(int i=1;i<=m;i++){
		int x;x=rd();
		root[i]=++cnt;
		t[root[i]]=t[root[i-1]];
		add(x,1,root[i-1],root[i]);
	}
	for(int i=1;i<=n;i++){
		int p=0;
		for(int j=1<<18;j;j>>=1){
		/*	if(l[i]==4&&r[i]==5){
				cout<<root[p+j]<<endl;
			}*/
			if(p+j<=m&&(query(l[i],r[i],root[p+j])<=s[i])){
				p+=j;
				
			}
		}
		if(query(l[i],r[i],root[p])>=s[i])num[p]++;
	} 
	for(int i=1;i<=m;i++)printf("%d\n",num[i]);
    return 0;
}
/*
3 5 
1 3 2
4 5 5
3 5 5
5
4
5
3
1
*/
2023/8/9 15:05
加载中...