Submission #541339

# Submission time Handle Problem Language Result Execution time Memory
541339 2022-03-23T07:04:21 Z Fidan Addk (eJOI21_addk) C++17
0 / 100
98 ms 2632 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll N = 1000'010;

void upd(ll id, ll val, vector<ll> &v){
	while(id<=v.size()){
		v[id]+=val;
		id+=id&(-id);
	}
}

ll que(ll id, vector<ll> &v){
	ll s=0;
	while(id>0){
		s+=v[id];
		id-=id&(-id);
	}
	return s;
}

ll sum(ll x, ll y, vector<ll> &v){
	return que(y, v)-que(x-1, v);
}	

ll prefix(ll x, ll y, vector<ll> &pre, vector<ll> &v){
	return que(y, pre)-que(x-1, pre)-(x-1)*sum(x, y, v);
}

ll suffix(ll x, ll y, vector<ll> &pre, vector<ll> &v){
	return (y-x+2)*sum(x, y, v)-prefix(x, y, pre, v);
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	ll n, k, i;
	cin>>n>>k;
	
	vector<ll> a(n+1, 0);
	for(i=1; i<=n; i++){
		cin>>a[i];
	}
	
	vector<ll> pre(n+1, 0);
	
	for(i=1; i<=n; i++){
		pre[i]=i*a[i];
	}
	
	ll q;
	cin>>q;
	vector<ll> val(k+1);
	while(q--){
		ll t;
		cin>>t;
		
		if(t==1){
			vector<ll> x(k+1);
			for(i=0; i<k; i++){
				cin>>x[i];
				val[i]=a[x[i]];
			}
			x[k]=x[0];
			val[k]=val[0];
			for(i=0; i<k; i++){
				ll y=x[i];
				upd(x[i], x[i]*(val[i+1]-val[i]), pre);
				upd(x[i], val[i+1]-val[i], a);
			}
		}
		
		else {
			ll l, r, m, k1 , s=0, b;
			cin>>l>>r>>m;
			k1=r-l+1;
			
			if(k1>=2*m){
				cout<<prefix(l, l+m-1, pre, a)+m*sum(l+m, r-m, a)+suffix(r-m+1, r, pre, a);
			}
			else {
				b=k1-m+1;
				s=prefix(l, l+b-1, pre, a)+b*sum(l+b, r-b, a)+suffix(r-b+1, r, pre, a);
			}
			cout<<s<<endl;
		}
		
	}
	return 0;
}

/*
 * 
 * 
8 3
7 2 5 1 9 3 4 6
3
1 2 5 8
cout<<7 9 5 1 6 3 4 2

1 3 4 5
cout<<7 9 1 6 5 3 4 2
1 2 3 4
cout<<7 1 6 9 5 3 4 2

 
*/

Compilation message

Main.cpp: In function 'void upd(ll, ll, std::vector<long long int>&)':
Main.cpp:8:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |  while(id<=v.size()){
      |        ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:69:8: warning: unused variable 'y' [-Wunused-variable]
   69 |     ll y=x[i];
      |        ^
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 1288 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 98 ms 2632 KB Output isn't correct
2 Halted 0 ms 0 KB -