-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleSumSegTree.cpp
103 lines (91 loc) · 2.15 KB
/
SimpleSumSegTree.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define prec(k) fixed << setprecision(k)
#define up(i, k, n) for (int i = k ; i < n ; i ++)
#define down(i, k, n) for (int i = k ; i >= n ; i --)
#define ii pair<int, int>
#define pll pair <ll, ll>
#define X first
#define Y second
#define str basic_string <int>
#define QuanPham
const int maxN = (int) 1e4 + 7;
const int maxM = (int) 1e5 + 7;
const int maxT = (int) 1e6 + 7;
using namespace std;
struct NodeData
{
int lef, rig;
ll val;
NodeData()
{
val = 0;
}
NodeData(int lef, int rig, ll val)
{
this->lef = lef;
this->rig = rig;
this->val = val;
}
} tree[maxT];
static int m, n, a[maxT], leaf[maxT];
void BuildTree(int x, int l, int r)
{
if (l > r) return;
tree[x] = NodeData(l, r, -1);
if (l == r)
{
tree[x] = NodeData(l, r, a[l]);
leaf[l] = x;
return;
}
int mid = l + r >> 1;
BuildTree(x * 2, l, mid);
BuildTree(x * 2 + 1, mid + 1, r);
tree[x].val = tree[x * 2 + 1].val + tree[x * 2].val;
}
ll Query(int x, int l, int r)
{
if (l > tree[x].rig || r < tree[x].lef) return 0;
if (l <= tree[x].lef && tree[x].rig <= r) return tree[x].val;
return Query(x * 2, l, r) + Query(x * 2 + 1, l, r);
}
void Update(int id, int k)
{
int x = leaf[id];
a[id] = k;
tree[x].val = k;
for (x /= 2 ; x > 0 ; x /= 2)
tree[x].val = tree[x * 2].val + tree[x * 2 + 1].val;
}
int main()
{
#ifdef QuanPham
auto start = chrono::steady_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
memset(a, 0, sizeof(a));
BuildTree(1, 1, n);
while (m --)
{
char q; int x, y;
cin >> q >> x >> y;
switch (q)
{
case 'S': Update(x, y) ; break;
case 'Q': cout << Query(1, x, y) << '\n' ; break;
}
}
#ifdef QuanPham
auto end = chrono::steady_clock::now();
cerr << "In milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - start).count();
cerr << '\n' << "In seconds : "
<< chrono::duration_cast<chrono::seconds>(end - start).count() << '\n';
#endif
return 0;
}