#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, Q, K;
int C[100010];
struct node {
ll sum;
deque<int> dq;
};
node mrg(node &a, node &b) {
node ret;
ret.sum = a.sum + b.sum;
for(int i = 0; i < min(a.dq.size(), b.dq.size()); i++) {
ret.dq.push_back(a.dq[i] + b.dq[i]);
}
for(int i = min(a.dq.size(), b.dq.size()); i < a.dq.size(); i++) {
ret.dq.push_back(a.dq[i]);
}
for(int i = min(a.dq.size(), b.dq.size()); i < b.dq.size(); i++) {
ret.dq.push_back(b.dq[i]);
}
return ret;
}
struct BIT {
vector<node> tree;
vector<int> lazy;
void init() {
tree = vector<node>(4 * N);
lazy = vector<int>(4 * N, 0);
build(0, N - 1, 1);
}
void build(int l, int r, int n) {
if(l == r) {
tree[n].sum = C[l];
if(K == 1) tree[n].dq.push_back(C[l]);
else while(C[l]) tree[n].dq.push_back(C[l] % K), C[l] /= K;
return;
}
int m = (l + r)>>1;
build(l, m, 2*n);
build(m + 1, r, 2*n + 1);
tree[n] = mrg(tree[2*n], tree[2*n + 1]);
}
void prop(int l, int r, int n) {
if(l != r) {
lazy[2*n] += lazy[n];
lazy[2*n + 1] += lazy[n];
while(lazy[n]) {
if(!tree[2*n].dq.size() && !tree[2*n + 1].dq.size()) break;
if(tree[2*n].dq.size()) {
tree[2*n].sum -= tree[2*n].dq.front(); tree[2*n].dq.pop_front();
tree[2*n].sum /= K;
}
if(tree[2*n + 1].dq.size()) {
tree[2*n + 1].sum -= tree[2*n + 1].dq.front(); tree[2*n + 1].dq.pop_front();
tree[2*n + 1].sum /= K;
}
lazy[n]--;
}
lazy[n] = 0;
}
}
void upd1(int idx, int val, int l, int r, int n) {
if(idx < l || r < idx) return;
if(l == r) {
tree[n].sum = val;
while(!tree[n].dq.empty()) tree[n].dq.pop_back();
if(K == 1) tree[n].dq.push_back(val);
else while(val) tree[n].dq.push_back(val % K), val /= K;
return;
}
prop(l, r, n);
int m = (l + r)>>1;
upd1(idx, val, l, m, 2*n);
upd1(idx, val, m + 1, r, 2*n + 1);
tree[n] = mrg(tree[2*n], tree[2*n + 1]);
}
void upd2(int a, int b, int l, int r, int n) {
if(b < l || r < a) return;
if(a <= l && r <= b) {
if(!tree[n].dq.empty()) {
tree[n].sum -= tree[n].dq.front(); tree[n].dq.pop_front();
tree[n].sum /= K;
}
lazy[n]++;
return;
}
prop(l, r, n);
int m = (l + r)>>1;
upd2(a, b, l, m, 2*n);
upd2(a, b, m + 1, r, 2*n + 1);
tree[n] = mrg(tree[2*n], tree[2*n + 1]);
}
ll quer(int a, int b, int l, int r, int n) {
if(b < l || r < a) return 0;
if(a <= l && r <= b) return tree[n].sum;
prop(l, r, n);
int m = (l + r)>>1;
ll L = quer(a, b, l, m, 2*n);
ll R = quer(a, b, m + 1, r, 2*n + 1);
return L + R;
}
} bit;
int main() {
scanf("%d %d %d", &N, &Q, &K);
for(int i = 0; i < N; i++) {
scanf("%d", &C[i]);
}
bit.init();
for(int i = 0; i < Q; i++) {
int t, a, b; scanf("%d %d %d", &t, &a, &b);
if(t == 1) {
a--;
bit.upd1(a, b, 0, N - 1, 1);
}
if(t == 2) {
a--; b--;
bit.upd2(a, b, 0, N - 1, 1);
}
if(t == 3) {
a--; b--;
printf("%lld\n", bit.quer(a, b, 0, N - 1, 1));
}
}
}
Compilation message
sterilizing.cpp: In function 'node mrg(node&, node&)':
sterilizing.cpp:18:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < min(a.dq.size(), b.dq.size()); i++) {
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sterilizing.cpp:21:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = min(a.dq.size(), b.dq.size()); i < a.dq.size(); i++) {
~~^~~~~~~~~~~~~
sterilizing.cpp:24:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = min(a.dq.size(), b.dq.size()); i < b.dq.size(); i++) {
~~^~~~~~~~~~~~~
sterilizing.cpp: In function 'int main()':
sterilizing.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &N, &Q, &K);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sterilizing.cpp:115:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &C[i]);
~~~~~^~~~~~~~~~~~~
sterilizing.cpp:121:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int t, a, b; scanf("%d %d %d", &t, &a, &b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
764 KB |
Output is correct |
2 |
Incorrect |
6 ms |
2588 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
797 ms |
136608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
136608 KB |
Output is correct |
2 |
Correct |
263 ms |
136608 KB |
Output is correct |
3 |
Correct |
297 ms |
136608 KB |
Output is correct |
4 |
Correct |
557 ms |
136608 KB |
Output is correct |
5 |
Correct |
1070 ms |
281056 KB |
Output is correct |
6 |
Correct |
1047 ms |
282468 KB |
Output is correct |
7 |
Incorrect |
1076 ms |
283856 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
791 ms |
283856 KB |
Output is correct |
2 |
Correct |
968 ms |
283856 KB |
Output is correct |
3 |
Correct |
813 ms |
283856 KB |
Output is correct |
4 |
Correct |
939 ms |
283856 KB |
Output is correct |
5 |
Correct |
1407 ms |
284272 KB |
Output is correct |
6 |
Correct |
1602 ms |
284272 KB |
Output is correct |
7 |
Correct |
1520 ms |
284272 KB |
Output is correct |
8 |
Correct |
1937 ms |
284272 KB |
Output is correct |
9 |
Correct |
1296 ms |
284272 KB |
Output is correct |
10 |
Correct |
1309 ms |
284272 KB |
Output is correct |
11 |
Correct |
1155 ms |
284272 KB |
Output is correct |
12 |
Correct |
1566 ms |
284272 KB |
Output is correct |