-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1275.cpp
More file actions
108 lines (98 loc) · 1.9 KB
/
1275.cpp
File metadata and controls
108 lines (98 loc) · 1.9 KB
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
104
105
106
107
108
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
vector<long long> arr;
class SegmentTree
{
public:
typedef long long dtype;
vector<dtype> tree;
int s;
SegmentTree(int n)
{
for (s = 1; s < n; s *= 2)
{
}
tree.resize(s * 2);
for (int i = 0; i < s * 2; i++)
{
tree[i] = 0;
}
}
void insert(vector<dtype> &d)
{
for (int i = s; i < s + d.size(); i++)
{
tree[i] = d[i - s];
}
for (int i = s - 1; i >= 1; i--)
{
tree[i] = tree[i * 2] + tree[i * 2 + 1];
}
}
dtype getSum(int Left, int Right)
{
int l = Left + s - 1, r = Right + s - 1;
dtype rval = 0;
while (l <= r)
{
if (l % 2 == 0)
{
l /= 2;
}
else
{
rval += tree[l];
l = (l / 2) + 1;
}
if (r % 2 == 1)
{
r /= 2;
}
else
{
rval += tree[r];
r = (r / 2) - 1;
}
}
return rval;
}
void update(int index, dtype val)
{
int idx = index + s - 1;
tree[idx] = val;
while (idx)
{
idx /= 2;
tree[idx] = tree[idx * 2] + tree[idx * 2 + 1];
}
}
};
int main()
{
int N, Q;
int x, y, a;
long long b;
cin >> N >> Q;
arr.resize(N);
SegmentTree t(N);
for (int i = 0; i < N; i++)
{
scanf("%lld", &arr[i]);
}
t.insert(arr);
while (Q--)
{
scanf("%d %d %d %lld", &x, &y, &a, &b);
if (x > y)
{
int tmp = x;
x = y;
y = tmp;
}
printf("%lld\n", t.getSum(x, y));
t.update(a, b);
}
return 0;
}