-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ1275.cpp
103 lines (77 loc) · 2.31 KB
/
BOJ1275.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
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct segtree {
int n;
vector<ll> tree;
segtree(const vector<ll>& a) {
n = a.size();
tree.resize(4 * n);
init(a, 0, n - 1, 1);
}
ll init(const vector<ll>& a, int left, int right, int node) {
if (left == right) {
tree[node] = a[left];
return tree[node];
}
int mid = (left + right) / 2;
ll linit = init(a, left, mid, node * 2);
ll rinit = init(a, mid + 1, right, node * 2 + 1);
tree[node] = linit + rinit ;
return tree[node];
}
ll query(int left, int right, int node, int nodeLeft, int nodeRight) {
if (left > nodeRight || right < nodeLeft) {
return 0;
}
if (left <= nodeLeft && right >= nodeRight) {
return tree[node];
}
int nodeMid = (nodeLeft + nodeRight) / 2;
ll lquery = query(left, right, node * 2, nodeLeft, nodeMid);
ll rquery = query(left, right, node * 2 + 1, nodeMid + 1, nodeRight);
return (lquery + rquery);
}
ll update(int value, int index, int node, int nodeLeft, int nodeRight) {
if (index < nodeLeft || index > nodeRight) {
return tree[node];
}
if (nodeLeft == nodeRight) {
tree[node] = value;
return tree[node];
}
int nodeMid = (nodeLeft + nodeRight) / 2;
tree[node] = update(value, index, node * 2, nodeLeft, nodeMid)
+ update(value, index, node * 2 + 1, nodeMid + 1, nodeRight);
return tree[node];
}
ll querycall(int left, int right) {
return query(left, right, 1, 0, n - 1);
}
ll updatecall(int value, int index) {
return update(value, index, 1, 0, n - 1);
}
};
int N, M, K;
vector<ll> input;
int main()
{
scanf("%d %d", &N, &M);
for (int i = 0; i < N; ++i) {
ll t;
scanf("%lld", &t);
input.push_back(t);
}
segtree S(input);
for (int i = 0; i < M; ++i) {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int s, e;
s = (a > b) ? b : a;
e = (a > b) ? a : b;
printf("%lld\n", S.querycall(s - 1, e - 1));
S.updatecall(d, c - 1);
}
return 0;
}