이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (stderr) 메시지
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |