1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| // 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
int n;
vector<long long> bit;
Fenwick(int n) : n(n), bit(n + 2, 0) {}
void add(int idx, long long delta) {
for (; idx <= n + 1; idx += idx & -idx) bit[idx] += delta;
}
long long sum(int idx) const {
long long res = 0;
for (; idx > 0; idx -= idx & -idx) res += bit[idx];
return res;
}
void rangeAdd(int l, int r, long long delta) {
add(l, delta);
add(r + 1, -delta);
}
long long pointQuery(int idx) const { return sum(idx); }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
if (!(cin >> N)) return 0;
vector<long long> A(N + 1);
for (int i = 1; i <= N; ++i) cin >> A[i];
Fenwick coverCnt(N); // number of updates covering index i
Fenwick sumStartL(N); // sum of L over updates covering index i
int Q; cin >> Q;
while (Q--) {
int t; cin >> t;
if (t == 1) {
int L, R; cin >> L >> R;
coverCnt.rangeAdd(L, R, 1);
sumStartL.rangeAdd(L, R, L);
} else {
int X; cin >> X;
long long cnt = coverCnt.pointQuery(X);
long long sL = sumStartL.pointQuery(X);
long long result = A[X] + (static_cast<long long>(X) + 1) * cnt - sL;
cout << result << '\n';
}
}
return 0;
}
|