Submission #632564

# Submission time Handle Problem Language Result Execution time Memory
632564 2022-08-20T10:44:54 Z pragmatist Addk (eJOI21_addk) C++17
100 / 100
810 ms 4268 KB
#include<bits/stdc++.h>
 
#define ll long long
#define nl "\n"
#define sz(v) (int)v.size()
#define pb push_back
#define x first
#define y second

using namespace std;
using pii = pair<int, int>; 

const int N = (int)1e5 + 7;
 
int n, k, q, b[N], a[N];

ll p[N], z[N];
set<int> c; 

void build() {
	for(int i = 1; i <= n; ++i) {
	    a[i] = b[i];
		p[i] = p[i - 1] + a[i];
		z[i] = z[i - 1] + p[i];
	}
}

ll get(int l) {
	ll ans = z[l];
	for(auto i : c) {
		if(i > l) continue;
		int ch = l - i + 1;
		ans -= 1ll * ch * a[i];
		ans += 1ll * ch * b[i];
	}
	return ans;
}

signed main() {
	ios_base::sync_with_stdio(NULL);
	cin.tie(0); 	
	cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
		b[i] = a[i];
	}
	build();
	cin >> q;
	while(q--) {
		char tp;
		cin >> tp;
		if(tp == '1') {
			vector<int> v(k);
			for(auto &x : v) cin >> x;
			int o = b[v[0]];
			for(int i = 0; i < k - 1; ++i) {
				b[v[i]] = b[v[i + 1]];
				c.insert(v[i]);
			}
			c.insert(v.back());
			b[v.back()] = o;
	        if(1ll * sz(c) * sz(c) >= n) {
				build();
				c.clear();
			}
		} else {
			int l, r, m;
			cin >> l >> r >> m;
			ll ans = get(r) - get(l + m - 2);
			ans -= get(r - m) - (l - 2 < 0 ? 0 : get(l - 2));
			cout << ans << nl;
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 4 ms 496 KB Output is correct
6 Correct 5 ms 496 KB Output is correct
7 Correct 9 ms 468 KB Output is correct
8 Correct 10 ms 596 KB Output is correct
9 Correct 13 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 1076 KB Output is correct
2 Correct 59 ms 1416 KB Output is correct
3 Correct 93 ms 1780 KB Output is correct
4 Correct 199 ms 3156 KB Output is correct
5 Correct 364 ms 4268 KB Output is correct
6 Correct 343 ms 3984 KB Output is correct
7 Correct 355 ms 4032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 246 ms 2040 KB Output is correct
2 Correct 367 ms 3268 KB Output is correct
3 Correct 810 ms 3400 KB Output is correct
4 Correct 426 ms 4204 KB Output is correct