#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
struct segtree {
vector<long long> tree;
int sz = 0;
void init(int n) {
sz = 1 << (32 - __builtin_clz(n - 1));
tree.assign(sz * 2 - 1, 0);
}
void set(int pos, long long val, int x, int lx, int rx) {
if(rx - lx == 1) {
tree[x] = val;
return;
}
int mid = (lx + rx) / 2;
if(pos >= mid) {
set(pos, val, x * 2 + 2, mid, rx);
}
else {
set(pos, val, x * 2 + 1, lx, mid);
}
tree[x] = tree[x * 2 + 1] + tree[x * 2 + 2];
}
void set(int pos, long long val) {
set(pos, val, 0, 0, sz);
}
long long get(int l, int r, int x, int lx, int rx) {
if(rx <= l || lx >= r) {
return 0;
}
if(lx >= l && rx <= r) {
return tree[x];
}
int mid = (lx + rx) / 2;
return get(l, r, x * 2 + 1, lx, mid) + get(l, r, x * 2 + 2, mid, rx);
}
long long get(int l, int r) {
return get(l, r + 1, 0, 0, sz);
}
long long get_element(int x) {
return tree[sz - 1 + x];
}
void build(vector<int> &a, int x, int lx, int rx) {
if(rx - lx == 1) {
if(lx < a.size()) {
tree[x] = a[lx];
}
}
else {
int mid = (lx + rx) / 2;
build(a, 2 * x + 1, lx, mid);
build(a, 2 * x + 2, mid, rx);
tree[x] = tree[x * 2 + 1] + tree[x * 2 + 2];
}
}
void build(vector<int> &a) {
init(a.size());
build(a, 0, 0, sz);
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> b(n);
for(int i = 0; i < n; i++) {
cin >> b[i];
}
segtree sg;
sg.build(b);
int q;
cin >> q;
while(q--) {
int x;
cin >> x;
if(x == 1) {
vector<int> a(k);
vector<long long> tmp(k);
for(int i = 0; i < k; i++) {
cin >> a[i];
a[i]--;
//tmp[i] = sg.get(a[i], a[i]);
tmp[i] = sg.get_element(a[i]);
}
for(int i = 0; i < k; i++) {
sg.set(a[((i - 1) % k + k) % k], tmp[i]);
}
}
else if(x == 2) {
int l, r, m;
cin >> l >> r >> m;
l--;
r--;
long long s = 0;
long long sprev = sg.get(l, r);
for(int i = 0; i < min(r - l + 1 - m + 1, m) && r - l + 1 - 2 * i > 0; i++) {
s += sprev;
sprev = sprev - sg.get_element(l + i) - sg.get_element(r - i);
}
cout << s << '\n';
}
}
return 0;
}
Compilation message
Main.cpp: In member function 'void segtree::build(std::vector<int>&, int, int, int)':
Main.cpp:60:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(lx < a.size()) {
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
408 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
532 KB |
Output is correct |
6 |
Correct |
8 ms |
648 KB |
Output is correct |
7 |
Correct |
9 ms |
596 KB |
Output is correct |
8 |
Correct |
16 ms |
700 KB |
Output is correct |
9 |
Correct |
27 ms |
940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
1648 KB |
Output is correct |
2 |
Correct |
151 ms |
2092 KB |
Output is correct |
3 |
Correct |
259 ms |
2988 KB |
Output is correct |
4 |
Correct |
784 ms |
5436 KB |
Output is correct |
5 |
Correct |
1284 ms |
6672 KB |
Output is correct |
6 |
Correct |
1189 ms |
6504 KB |
Output is correct |
7 |
Correct |
963 ms |
6520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
3928 KB |
Output is correct |
2 |
Correct |
831 ms |
6204 KB |
Output is correct |
3 |
Correct |
201 ms |
8060 KB |
Output is correct |
4 |
Correct |
1228 ms |
6908 KB |
Output is correct |