#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int maxStates = 30;
int n, q, k;
int a[maxn];
long long tree[4 * maxn][maxStates + 5];
int lazy[4 * maxn];
void build(int node, int l, int r)
{
lazy[node] = 0;
if (l == r)
{
tree[node][0] = a[l];
for (int i = 1; i <= maxStates; ++i)
{
tree[node][i] = tree[node][i - 1] / k;
}
return;
}
int mid = (l + r) / 2;
build(2 * node, l, mid);
build(2 * node + 1, mid + 1, r);
for (int i = 0; i <= maxStates; ++i)
{
tree[node][i] = tree[2 * node][i] + tree[2 * node + 1][i];
}
}
void pushLazy(int node, int l, int r)
{
if (lazy[node] == 0)
{
return;
}
for (int i = 0; i <= maxStates; ++i)
{
if (i + lazy[node] <= maxStates)
{
tree[node][i] = tree[node][i + lazy[node]];
}
else
{
tree[node][i] = 0;
}
}
if (l != r)
{
lazy[2 * node] += lazy[node];
lazy[2 * node + 1] += lazy[node];
}
lazy[node] = 0;
}
void updatePoint(int node, int l, int r, int pos, int val)
{
pushLazy(node, l, r);
if (l == r)
{
lazy[node] = 0;
a[pos] = val;
tree[node][0] = a[pos];
for (int i = 1; i <= maxStates; ++i)
{
tree[node][i] = tree[node][i - 1] / k;
}
return;
}
int mid = (l + r) / 2;
if (pos <= mid)
{
updatePoint(2 * node, l, mid, pos, val);
}
else
{
updatePoint(2 * node + 1, mid + 1, r, pos, val);
}
pushLazy(2 * node, l, mid);
pushLazy(2 * node + 1, mid + 1, r);
for (int i = 0; i <= maxStates; ++i)
{
tree[node][i] = tree[2 * node][i] + tree[2 * node + 1][i];
}
}
void updateRange(int node, int l, int r, int ql, int qr)
{
pushLazy(node, l, r);
if (l > qr || r < ql)
{
return;
}
if (ql <= l && r <= qr)
{
++lazy[node];
pushLazy(node, l, r);
return;
}
int mid = (l + r) / 2;
updateRange(2 * node, l, mid, ql, qr);
updateRange(2 * node + 1, mid + 1, r, ql, qr);
for (int i = 0; i <= maxStates; ++i)
{
tree[node][i] = tree[2 * node][i] + tree[2 * node + 1][i];
}
}
long long query(int node, int l, int r, int ql, int qr)
{
pushLazy(node, l, r);
if (l > qr || r < ql)
{
return 0;
}
if (ql <= l && r <= qr)
{
return tree[node][0];
}
int mid = (l + r) / 2;
return query(2 * node, l, mid, ql, qr) + query(2 * node + 1, mid + 1, r, ql, qr);
}
struct FenwickTree
{
long long fen[maxn];
void update(int pos, int val)
{
for (int i = pos; i <= n; i += (i & (-i)))
{
fen[i] += val;
}
}
long long query(int pos)
{
long long ans = 0;
for (int i = pos; i >= 1; i -= (i & (-i)))
{
ans += fen[i];
}
}
};
FenwickTree bit;
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> q >> k;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
if (k == 1)
{
for (int i = 1; i <= n; ++i)
{
bit.update(i, a[i]);
}
for (int i = 1; i <= q; ++i)
{
int type;
cin >> type;
if (type == 1)
{
int pos, val;
cin >> pos >> val;
bit.update(pos, val - a[pos]);
a[pos] = val;
}
else if (type == 2)
{
int l, r;
cin >> l >> r;
}
else if (type == 3)
{
int l, r;
cin >> l >> r;
cout << bit.query(r) - bit.query(l - 1) << "\n";
}
}
return 0;
}
build(1, 1, n);
for (int i = 1; i <= q; ++i)
{
int type;
cin >> type;
if (type == 1)
{
int pos, val;
cin >> pos >> val;
updatePoint(1, 1, n, pos, val);
}
else if (type == 2)
{
int l, r;
cin >> l >> r;
updateRange(1, 1, n, l, r);
}
else if (type == 3)
{
int l, r;
cin >> l >> r;
cout << query(1, 1, n, l, r) << "\n";
}
}
return 0;
}
Compilation message
sterilizing.cpp: In member function 'long long int FenwickTree::query(int)':
sterilizing.cpp:175:5: warning: no return statement in function returning non-void [-Wreturn-type]
175 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4440 KB |
Output is correct |
2 |
Runtime error |
3 ms |
600 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
9 ms |
5468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
8700 KB |
Output is correct |
2 |
Correct |
45 ms |
39616 KB |
Output is correct |
3 |
Correct |
80 ms |
39624 KB |
Output is correct |
4 |
Correct |
177 ms |
21092 KB |
Output is correct |
5 |
Correct |
274 ms |
76628 KB |
Output is correct |
6 |
Correct |
277 ms |
76628 KB |
Output is correct |
7 |
Runtime error |
10 ms |
5688 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
39820 KB |
Output is correct |
2 |
Correct |
189 ms |
39640 KB |
Output is correct |
3 |
Correct |
133 ms |
39712 KB |
Output is correct |
4 |
Correct |
194 ms |
21412 KB |
Output is correct |
5 |
Correct |
298 ms |
76880 KB |
Output is correct |
6 |
Correct |
290 ms |
76952 KB |
Output is correct |
7 |
Correct |
295 ms |
76984 KB |
Output is correct |
8 |
Correct |
297 ms |
76880 KB |
Output is correct |
9 |
Correct |
211 ms |
77100 KB |
Output is correct |
10 |
Correct |
206 ms |
76860 KB |
Output is correct |
11 |
Correct |
207 ms |
76880 KB |
Output is correct |
12 |
Correct |
207 ms |
77004 KB |
Output is correct |