-
Notifications
You must be signed in to change notification settings - Fork 16
/
First-Element-atleast-X_SegmentTree.cpp
105 lines (98 loc) · 1.91 KB
/
First-Element-atleast-X_SegmentTree.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
103
104
105
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+2,MOD=1e9+7;
int tree[4*N],a[N];
void buildTree(int node,int st,int en)
{
if(st==en)
{
tree[node]=a[st];
return;
}
int mid = (st+en)/2;
buildTree(2*node,st,mid);
buildTree(2*node+1,mid+1,en);
tree[node]=max(tree[2*node],tree[2*node+1]);
}
int query(int node,int st,int en,int l,int r)
{
if(st>r || en<l)
{
return -MOD;
}
if(l<=st && en<=r)
{
return tree[node];
}
int mid = (st+en)/2;
int q1 = query(2*node,st,mid,l,r);
int q2 = query(2*node+1,mid+1,en,l,r);
return max(q1,q2);
}
void update(int node,int st,int en,int idx,int val)
{
if(st==en)
{
a[st]=val;
tree[node]=val;
return;
}
int mid = (st+en)/2;
if(idx<=mid)
{
update(2*node,st,mid,idx,val);
}
else{
update(2*node+1,mid+1,en,idx,val);
}
tree[node]=max(tree[2*node],tree[2*node+1]);
}
signed main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
buildTree(1,0,n-1);
while(m--)
{
int type;
cin>>type;
if(type==1)
{
int idx,val;
cin>>idx>>val;
update(1,0,n-1,idx,val);
}
else if(type==2)
{
int x;
cin>>x;
int lo=0,hi=n-1;
int ans=n;
while(lo<=hi)
{
int mid=(lo+hi)/2;
if(query(1,0,n-1,lo,mid)<x)
{
lo = mid+1;
}
else{
hi = mid-1;
ans=min(ans,mid);
}
}
if(ans==n)
{
cout<<"-1"<<endl;
}
else{
cout<<ans<<endl;
}
}
}
return 0;
}