QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#570054#7992. 【模板】线段树liaoyanxuTL 0ms5724kbC++142.3kb2024-09-17 13:32:032024-09-17 13:32:04

Judging History

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

  • [2024-09-17 13:32:04]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:5724kb
  • [2024-09-17 13:32:03]
  • 提交

answer

// Problem: P10045 [CCPC 2023 北京市赛] 线段树
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P10045
// Memory Limit: 1 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N=200005,M=1145,K=21,len=314;
int mod=1048576;
int n,q,a[N],mul[N][K],tag[N];
inline int bl(int x){
	return (x+len-1)/len;
}
inline int L(int id){
	return len*(id-1)+1;
}
inline int R(int id){
	return min(n,len*id);
}
inline void rebuild(int id){
	for(register int i=1;i<=20;i++){
		mul[id][i]=0;
	}
	mul[id][1]=1;
	for(register int i=L(id);i<=R(id);i++){
		for(register int j=20;j>=1;j--){
			mul[id][j]=mul[id][j]*a[i]+mul[id][j-1];
		}
	}
	return ;
}
inline void build(){
	for(int i=1;i<=n;i++){
		if(bl(i)!=bl(i-1)){
			rebuild(bl(i));
		}
	}
	return ;
}
inline void pushdown(int id){
	if(!tag[id]){
		return ;
	}
	for(register int i=L(id);i<=R(id);i++){
		a[i]+=tag[id];
	}
	tag[id]=0;
	rebuild(id);
	return ;
}
inline void add(int l,int r,int k){
	if(bl(l)==bl(r)){
		pushdown(bl(l));
		for(register int i=l;i<=r;i++){
			a[i]+=k;
		}
		rebuild(bl(l));
		return ;
	}
	pushdown(bl(l));
	for(register int i=l;i<=R(bl(r));i++){
		a[i]+=k;
	}
	rebuild(bl(l));
	pushdown(bl(r));
	for(register int i=L(bl(r));i<=r;i++){
		a[i]+=k;
	}
	rebuild(bl(r));
	for(register int i=bl(l)+1;i<=bl(r)-1;i++){
		tag[i]+=k;
	}
	return ;
}
inline int query(int l,int r){
	if(bl(l)==bl(r)){
		pushdown(bl(l));
		int ans=1;
		for(register int i=l;i<=r;i++){
			ans*=a[i];
		}
		return ans;
	}
	int ans=1;
	pushdown(bl(l));
	for(register int i=l;i<=R(bl(l));i++){
		ans*=a[i];
	}
	rebuild(bl(l));
	pushdown(bl(r));
	for(register int i=L(bl(r));i<=r;i++){
		ans*=a[i];
	}
	rebuild(bl(r));
	for(register int i=bl(l)+1;i<=bl(r)-1;i++){
		int tmp=1;
		int ml=0,cnt=1;
		while(tmp%mod){
			ml=ml+tmp*mul[i][cnt];
			tmp*=tag[i];
			++cnt;
		}
		ans*=ml;
	}
	return ans;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
    build();
	while(q--){
		int op,l,r,x;
		cin>>op>>l>>r;
		if(op==1){
			cin>>x;
			add(l,r,x);
		}
		else{
			int f=query(l,r)%mod+mod;
			cout<<(f>mod?f-mod:f)<<'\n';
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5724kb

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: -100
Time Limit Exceeded

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
802827
736735
651681
47189
349413
45671
271225
614017
871289
585231
611521
1046775
724409
638919
283135
616551
59091
918243
644797
490187
682573
7333
1018947
35665
419775
877693
580805
859147
850985
1038157
261145
441463
582853
875009
876643
1010827
475353
62397
747795
270911
723
969487
22639...

result: