Submission #475465

# Submission time Handle Problem Language Result Execution time Memory
475465 2021-09-22T14:47:57 Z mosiashvililuka Addk (eJOI21_addk) C++14
100 / 100
137 ms 4164 KB
#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,tes,t,k,f[100009],fen[100009],fen2[100009],pas,tp,g[100009],l,r,m;
void upd(long long q, long long w){
	while(q<=100002){
		fen[q]+=w;
		q=q+(q&(-q));
	}
}
void upd2(long long q, long long w){
	while(q<=100002){
		fen2[q]+=w;
		q=q+(q&(-q));
	}
}
long long read(long long q){
	long long sm=0;
	while(q>0){
		sm+=fen[q];
		q=q-(q&(-q));
	}
	return sm;
}
long long read2(long long q){
	long long sm=0;
	while(q>0){
		sm+=fen2[q];
		q=q-(q&(-q));
	}
	return sm;
}
long long calc(long long q, long long w){
	if(q>w) return 0;
	long long sm=0;
	sm=read2(w)-read2(q-1);sm-=(read(w)-read(q-1))*(q-1);
	return sm;
}
long long calc2(long long q, long long w){
	if(q>w) return 0;
	long long sm=0;
	sm=(read(w)-read(q-1))*(w-q+2)-calc(q,w);
	return sm;
}
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>a>>k;
	for(i=1; i<=a; i++){
		cin>>f[i];
		upd(i,f[i]);
		upd2(i,i*f[i]);
	}
	cin>>tes;
	for(t=1; t<=tes; t++){
		cin>>tp;
		if(tp==2){
			cin>>l>>r>>m;pas=0;
			if(r-l+1>=2*m-2){
				
			pas+=calc(l,l+m-2);i=l+m-1;
			pas+=calc2(r-m+2,r);j=r-m+1;
			if(j>=i){
				pas+=(read(j)-read(i-1))*m;
			}
			
			}else{
			
			pas+=calc(l,r-m);i=r-m+1;
			pas+=calc2(l+m,r);j=l+m-1;
			if(j>=i){
				//pas+=(read(j)-read(i-1))*(r-m+1);
				pas+=(read(j)-read(i-1))*(r-m+1-l+1);
			}
			
			}
			cout<<pas<<"\n";
		}else{
			for(i=1; i<=k; i++){
				cin>>g[i];
			}
			for(i=1; i<=k; i++){
				if(i==k) j=1; else j=i+1;
				upd(g[i],f[g[j]]-f[g[i]]);
				upd2(g[i],(f[g[j]]-f[g[i]])*g[i]);
			}
			for(i=1; i<k; i++) swap(f[g[i]],f[g[i+1]]);
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 4 ms 588 KB Output is correct
6 Correct 4 ms 460 KB Output is correct
7 Correct 5 ms 588 KB Output is correct
8 Correct 5 ms 588 KB Output is correct
9 Correct 8 ms 652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 1032 KB Output is correct
2 Correct 27 ms 1356 KB Output is correct
3 Correct 32 ms 1828 KB Output is correct
4 Correct 56 ms 3008 KB Output is correct
5 Correct 82 ms 4080 KB Output is correct
6 Correct 77 ms 4092 KB Output is correct
7 Correct 79 ms 3996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 2036 KB Output is correct
2 Correct 77 ms 3268 KB Output is correct
3 Correct 137 ms 3408 KB Output is correct
4 Correct 88 ms 4164 KB Output is correct