-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentree.sublime-snippet
51 lines (49 loc) · 1.1 KB
/
segmentree.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
<snippet>
<content><![CDATA[
int arr[200011];
int tree[800011];
void build(int node,int left,int right)
{
if(left==right)
{
tree[node]=arr[left];
return;
}
int mid=(left+right)/2;
build(2*node+1,left,mid);
build(2*node+2,mid+1,right);
tree[node]=min(tree[2*node+1],tree[2*node+2]);
return;
}
int query(int node,int left,int right,int ls,int rs)
{
if(ls>right || rs<left)
return INFINITY;
if(left>=ls && right<=rs )
return tree[node];
else
return min(query(2*node+1,left,(left+right)/2,ls,rs),query(2*node+2,((left+right)/2)+1,right,ls,rs));
}
void sol()
{
int n;cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
build(0,0,n-1);
int q;cin>>q;
while(q--)
{
int x,y;cin>>x>>y;
cout<<query(0,0,n-1,x,y)<<endl;
}
}
int main()
{
sol();
}
]]></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>