QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#353490#7992. 【模板】线段树wjh213TL 3973ms113176kbC++143.9kb2024-03-14 10:04:092024-03-14 10:04:09

Judging History

你现在查看的是最新测评结果

  • [2024-03-14 10:04:09]
  • 评测
  • 测评结果:TL
  • 用时:3973ms
  • 内存:113176kb
  • [2024-03-14 10:04:09]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned
#define int unsigned
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
    int x=0,f=1;
    char ch=nc();
    while(ch<48||ch>57)
    {
        if(ch=='-')
            f=-1;
        ch=nc();
    }
    while(ch>=48&&ch<=57)
        x=x*10+ch-48,ch=nc();
   	return x*f;
}
void write(int x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
    return;
}
int const MAX=2e5+10;
ull C[MAX][22];
int a[MAX];
int n;
int qwer[4*MAX][21];
struct tree{
	ull f[4*MAX][22],tag[4*MAX];
	void pushup(int ind){
		for(int i=0;i<=19;i++){
			f[ind][i]=0;
			for(int j=0;j<=i;j++){
				f[ind][i]+=f[ind*2][i-j]*f[ind*2+1][j];
			}
		}
		return;
	}
	void maketag(int ind,int l,int r,int t1){
		if(!t1)return;
		tag[ind]+=t1;
		for(int i=19;i>=1;i--){
			//cerr<<i<<"\n";
			int res=1;
			for(int j=1;j<=i;j++){
				res*=t1;
				//cerr<<ind<<" "<<i<<" "<<i-j<<" "<<r-l+1-(i-j)<<" "<<j<<"\n";
				if((signed)r-(signed)l+1-(signed)(i-j)<0)continue;
				f[ind][i]+=res*f[ind][i-j]*C[r-l+1-(i-j)][j];
			}
		}
		//if(l==r&&r==1)cerr<<f[ind][1]<<"\n";
		return;
	}
	void pushdown(int ind,int l,int r){
		if(!tag[ind])return;
		int mid=(l+r)/2;
		maketag(ind*2,l,mid,tag[ind]);
		maketag(ind*2+1,mid+1,r,tag[ind]);
		tag[ind]=0;
		return;
	}
	void init(int ind=1,int l=1,int r=n){
		if(l==r){
			f[ind][0]=1;
			f[ind][1]=a[l]-1;
			return;
		}
		int mid=(l+r)/2;
		init(ind*2,l,mid);
		init(ind*2+1,mid+1,r);
		pushup(ind);
		return;
	}
	bool inrange(int l,int r,int ll,int rr){
		return l<=ll&&rr<=r;
	}
	bool outrange(int l,int r,int ll,int rr){
		return l>rr||ll>r;
	}
	void upd(int l,int r,int t1,int ind=1,int ll=1,int rr=n){
		//cerr<<l<<" "<<r<<" "<<t1<<" "<<ind<<" "<<ll<<" "<<rr<<"\n";
		if(inrange(l,r,ll,rr)){
			maketag(ind,ll,rr,t1);
			return;
		}
		if(outrange(l,r,ll,rr))return;
		pushdown(ind,ll,rr);
		int mid=(ll+rr)/2;
		upd(l,r,t1,ind*2,ll,mid);
		upd(l,r,t1,ind*2+1,mid+1,rr);
		pushup(ind);
		return;
	}
	void find(int l,int r,int ind=1,int ll=1,int rr=n){
		if(inrange(l,r,ll,rr)){
			//vector<int> tp(21);
			auto& tp=qwer[ind];
			for(int i=0;i<=19;i++){
				tp[i]=f[ind][i];
			}
			return;
		}else if(outrange(l,r,ll,rr)){
			auto& tp=qwer[ind];
			memset(tp,0,sizeof(tp));
			tp[0]=1;
			return ;
		}
		pushdown(ind,ll,rr);
		int mid=(ll+rr)/2;
		find(l,r,ind*2,ll,mid);
		find(l,r,ind*2+1,mid+1,rr);
		//vector<int> tp3(21,0);
		auto& tp1=qwer[ind*2];
		auto& tp2=qwer[ind*2+1];
		auto& tp3=qwer[ind];
		memset(tp3,0,sizeof(tp3));
		for(int i=0;i<=19;i++){
			for(int j=0;j<=i;j++){
				tp3[i]+=tp1[j]*tp2[i-j];
			}
		}
		return ;
	}
}T;
signed main(){
	//freopen("data.in","r",stdin);
	//freopen("g.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	C[0][0]=1;
	for(int i=1;i<MAX;i++){
		C[i][0]=1;
		for(int j=1;j<=min(i,(ull)19);j++){
			C[i][j]=C[i-1][j-1]+C[i-1][j];
		}
	}
	//cin>>n;
	n=read();
	int q;
	//cin>>q;
	q=read();
	for(int i=1;i<=n;i++){
		//cin>>a[i];
		a[i]=read();
	}
	T.init();
	while(q--){
		int op;
		//cin>>op;
		op=read();
		if(op==1){
			int l,r,x;
			//cin>>l>>r>>x;
			l=read(),r=read(),x=read();
			T.upd(l,r,x);
			//cerr<<'a';
		}else{
			int l,r;
			//cin>>l>>r;
			l=read(),r=read();
			T.find(l,r);
			auto& tp=qwer[1];
			int ans=0;
			for(int i=0;i<=19;i++){
				ans+=tp[i];
				//cerr<<tp[i]<<" ";
			}
			//cout<<ans%(1<<20)<<"\n";
			write(ans&((1<<20)-1));
			putchar('\n');
		}
	}
	//for(int i=1;i<=3;i++){
	//	cerr<<T.f[1][i]<<" ";
	//}
	//T.maketag(1,1,3,2);
	//cerr<<"\n";
	//for(int i=1;i<=3;i++){
	//	cerr<<T.f[1][i]<<" ";
	//}
	//for(int i=0;i<=3;i++){
	//	cerr<<C[3][i]<<" ";
	//}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 22560kb

input:

10 10
969575 741825 24903 1047319 450475 256145 1045323 479255 810659 768323
1 5 6 3034
2 1 10
2 1 9
2 1 4
1 3 6 126904
2 5 5
2 9 9
1 7 7 853094
1 4 9 1025178
2 5 8

output:

1045541
1012343
558151
580413
810659
527353

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 3973ms
memory: 113176kb

input:

200000 200000
496015 180543 330721 874799 740427 144379 598057 795949 323465 87657 683935 748203 748665 288301 846003 33033 746029 132621 876629 361899 701297 373189 256151 723161 377571 54947 91151 855991 433965 73347 155081 314317 790527 705555 1035217 298963 604641 203865 230029 802437 720769 843...

output:

746709
564663
426791
840425
762201
413693
881143
534387
189149
257625
60619
958793
250635
869079
383765
151047
272239
146175
46215
914259
617511
698623
381177
932779
792705
785375
1044293
202971
508317
237901
634919
646839
38501
304017
889609
214899
617927
720071
628729
202369
420511
528565
555717
7...

result:

ok 100378 lines

Test #3:

score: -100
Time Limit Exceeded

input:

200000 200000
313625 170101 477893 536285 651807 542493 870481 1038171 205037 914869 1020857 263797 349049 146425 49155 634785 620419 520999 216377 365705 284761 874801 450169 521981 238295 791805 292987 339767 765065 1017179 333101 73531 855729 410125 933189 192789 52457 526865 918271 334533 876331...

output:

225575
235385
743949
20373
509445
393347
140689
735475
977073
494895
593783
118129
492359
290607
103169
466197
609397
831915
388819
1031053
461107
492189
790925
208041
397517
1008911
672577
873151
784219
796179
760731
460383
118997
497147
238277
523161
689295
284013
911061
929085
706393
43425
510263...

result: