#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ar array
const int INF = 1e17;
const int MOD = 1e9;
struct SegTree {
vector<deque<int>> values;
vector<int> operations;
int size, k;
void init(int n, int _k) {
size = 1; k = _k;
while (size < n) size <<= 1;
operations.resize(2*size);
values.resize(2*size);
}
void propogate(int x, int lx, int rx) {
if (rx - lx != 1) {
operations[2*x+1] += operations[x];
operations[2*x+2] += operations[x];
}
for ( ; operations[x] && values[x].size() > 1; operations[x]--) values[x].pop_front();
operations[x] = 0;
}
void update(int x, int lx, int rx) {
if (rx - lx == 1) return;
int m = (lx + rx) >> 1;
while (values[x].size() > 0) values[x].pop_front();
propogate(2*x+1, lx, m);
propogate(2*x+2, m, rx);
int i, j;
for (i = 0, j = 0; i < values[2*x+1].size() && j < values[2*x+2].size(); ++i, ++j) {
values[x].push_back(values[2*x+1][i] + values[2*x+2][j]);
}
for (; i < values[2*x+1].size(); ++i) values[x].push_back(values[2*x+1][i]);
for (; j < values[2*x+2].size(); ++j) values[x].push_back(values[2*x+2][j]);
}
void set(int i, int v, int x, int lx, int rx) {
propogate(x, lx, rx);
if (rx - lx == 1) {
while (values[x].size() > 0) values[x].pop_front();
while (v) {
values[x].push_back(v); v /= k;
}
values[x].push_back(v);
return;
}
int m = (lx + rx) >> 1;
if (i < m) set(i, v, 2*x+1, lx, m);
else set(i, v, 2*x+2, m, rx);
update(x, lx, rx);
}
void set(int i, int v) {
set(i, v, 0, 0, size);
}
void modify(int l, int r, int x, int lx, int rx) {
propogate(x, lx, rx);
if (lx >= r || l >= rx) return;
if (l <= lx && rx <= r) return void(operations[x]++);
int m = (lx + rx) >> 1;
modify(l, r, 2*x+1, lx, m);
modify(l, r, 2*x+2, m, rx);
update(x, lx, rx);
}
void modify(int l, int r) {
modify(l, r, 0, 0, size);
}
int calc(int l, int r, int x, int lx, int rx) {
propogate(x, lx, rx);
if (lx >= r || l >= rx) return 0;
if (l <= lx && rx <= r) {
return values[x].front();
}
int m = (lx + rx) >> 1;
int s1 = calc(l, r, 2*x+1, lx, m);
int s2 = calc(l, r, 2*x+2, m, rx);
return s1 + s2;
}
int calc(int l, int r) {
return calc(l, r, 0, 0, size);
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k; cin>>n>>m>>k;
SegTree st; st.init(n, k);
for (int i = 0; i < n; i++) {
int x; cin>>x;
st.set(i, x);
}
while (m--) {
int t, a, b; cin>>t>>a>>b;
if (t == 1) {
st.set(a-1, b);
} else if (t == 2) {
st.modify(a-1, b);
} else if (t == 3) {
cout<<st.calc(a-1, b)<<"\n";
}
}
}
Compilation message
sterilizing.cpp: In member function 'void SegTree::update(long long int, long long int, long long int)':
sterilizing.cpp:36:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (i = 0, j = 0; i < values[2*x+1].size() && j < values[2*x+2].size(); ++i, ++j) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
sterilizing.cpp:36:58: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (i = 0, j = 0; i < values[2*x+1].size() && j < values[2*x+2].size(); ++i, ++j) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
sterilizing.cpp:39:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (; i < values[2*x+1].size(); ++i) values[x].push_back(values[2*x+1][i]);
| ~~^~~~~~~~~~~~~~~~~~~~~~
sterilizing.cpp:40:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (; j < values[2*x+2].size(); ++j) values[x].push_back(values[2*x+2][j]);
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
620 KB |
Output is correct |
2 |
Runtime error |
994 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
895 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
12140 KB |
Output is correct |
2 |
Correct |
149 ms |
90676 KB |
Output is correct |
3 |
Correct |
177 ms |
90720 KB |
Output is correct |
4 |
Correct |
268 ms |
46572 KB |
Output is correct |
5 |
Correct |
584 ms |
181804 KB |
Output is correct |
6 |
Correct |
562 ms |
182124 KB |
Output is correct |
7 |
Runtime error |
548 ms |
524292 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
504 ms |
92156 KB |
Output is correct |
2 |
Correct |
534 ms |
93164 KB |
Output is correct |
3 |
Correct |
700 ms |
94072 KB |
Output is correct |
4 |
Correct |
598 ms |
52972 KB |
Output is correct |
5 |
Correct |
897 ms |
182252 KB |
Output is correct |
6 |
Correct |
1006 ms |
184172 KB |
Output is correct |
7 |
Correct |
858 ms |
184684 KB |
Output is correct |
8 |
Correct |
1224 ms |
198636 KB |
Output is correct |
9 |
Correct |
907 ms |
200632 KB |
Output is correct |
10 |
Correct |
1039 ms |
198588 KB |
Output is correct |
11 |
Correct |
759 ms |
186092 KB |
Output is correct |
12 |
Correct |
1350 ms |
197360 KB |
Output is correct |