-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegtree.sublime-snippet
126 lines (122 loc) · 1.99 KB
/
segtree.sublime-snippet
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<snippet>
<content><![CDATA[
class Node
{
public:
int val;
Node()
{
val = 0;
}
Node(int a):val(a){}
static Node merge(Node left, Node right)
{
Node result;
result.val = left.val + right.val;
return result;
}
};
template<class T>
class ST{
public:
int n;
vector<int>arr;
vector<T>tree;
ST(){}
ST(int sz)
{
n = sz;
arr.resize(n,0);
tree.resize(4*n,0);
build(0,n-1);
}
ST(vector<int>ar)
{
n = ar.size();
arr = ar;
tree.resize(4*n,0);
build(0,n-1);
}
void build(int l,int r,int c = 1)
{
if(l==r)
{
tree[c] = arr[l];
return;
}
int mid = (l+r)/2,c1 = c*2,c2=c*2+1;
build(l,mid,c1);
build(mid+1,r,c2);
tree[c] = T::merge(tree[c1],tree[c2]);
}
Node query(int x,int y, int l, int r, int c)
{
if(l==x && y==r)
{
return tree[c];
}
int mid = (l+r)/2;
if(mid>=y)
return query(x,y,l,mid,c*2);
else if(mid<x)
return query(x,y,mid+1,r,c*2+1);
return T::merge(query(x,mid,l,mid,2*c),query(mid+1,y,mid+1,r,2*c+1));
}
Node query(int l,int r)
{
return query(l,r,0,n-1,1);
}
void update(int idx,int val,int l, int r ,int c)
{
if(l==r)
{
tree[c].val = val;
return;
}
int mid = (l+r)/2;
if(mid >= idx)
update(idx, val, l, mid, 2 * c);
else if(mid<idx)
update(idx, val, mid+1, r, 2*c+1);
tree[c] = T::merge(tree[2*c],tree[2*c+1]);
}
void update(int idx, int val)
{
update(idx,val,0,n-1,1);
}
};
void solve()
{
int n,q;
cin>>n;
cin>>q;
vector<int>v(n);
for(int i = 0;i<n;i++)cin>>v[i];
ST<Node>st(v);
while(q--)
{
int ty;
cin>>ty;
if(ty==1)
{
int idx,val;
cin>>idx>>val;
idx--;
st.update(idx,val);
}
else
{
int l,r;
cin>>l>>r;
l--;
r--;
cout<<st.query(l,r).val<<endl;
}
}
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>_segtree</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>