-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1275.cpp
More file actions
67 lines (55 loc) · 1.41 KB
/
1275.cpp
File metadata and controls
67 lines (55 loc) · 1.41 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
#include <iostream>
#define N 100005
#define swap(x, y, z) z=x,x=y,y=z
using namespace std;
typedef long long ll;
ll n, q, arr[N];
ll t[N * 3];
ll x, y, a, b;
ll make_tree(ll start, ll end, ll index)
{
if (start == end)
return t[index] = arr[start];
ll mid = (start + end) / 2;
return t[index] = make_tree(start, mid, index * 2) + make_tree(mid + 1, end, index * 2 + 1);
}
ll get_sum(ll start, ll end, ll index)
{
if (end < x || start > y)
return 0;
if (x <= start && end <= y)
return t[index];
ll mid = (start + end) / 2;
return get_sum(start, mid, index * 2) + get_sum(mid + 1, end, index * 2 + 1);
}
ll modify_tree(ll start, ll end, ll index)
{
ll mid = (start + end) / 2;
if (start == end)
return t[index] = b;
if (a <= mid)
return t[index] = modify_tree(start, mid, index * 2) + t[index * 2 + 1];
else
return t[index] = t[index * 2] + modify_tree(mid + 1, end, index * 2 + 1);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int i, temp;
cin >> n >> q;
for (i = 1; i <= n; i++)
cin >> arr[i];
make_tree(1, n, 1);
for (i = 1; i <= q; i++)
{
cin >> x >> y >> a >> b;
if(x>y) swap(x, y, temp);
printf("%lld\n", get_sum(1,n,1));
modify_tree(1, n, 1);
}
return 0;
}