Advertisement
NgJaBach

Segment Tree

Dec 6th, 2021
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long int ll;
  5. //#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
  6. #define pb push_back
  7. #define mp make_pair
  8. #define fi first
  9. #define se second
  10. #define gcd __gcd
  11. #define getl(s) getline(cin, s);
  12. #define setpre(x) fixed << setprecision(x)
  13. #define mset(a) memset(a, 0, sizeof(a))
  14. #define endl '\n'
  15. const int N=200050,M=1000000007;
  16. const ll INF=1e18+7;
  17. int a[N],p[4*N],val,pos,lo,hi;
  18. void Build(int x,int L,int R){
  19.     if(L==R){
  20.         p[x]=a[L];
  21.         return;
  22.     }
  23.     int mid=(L+R)/2;
  24.     Build(2*x,L,mid);
  25.     Build(2*x+1,mid+1,R);
  26.     p[x]=min(p[2*x],p[2*x+1]);
  27.     return;
  28. }
  29. void Update(int x,int L,int R){
  30.     if(L==R){
  31.         p[x]=val;
  32.         return;
  33.     }
  34.     int mid=(L+R)/2;
  35.     if(pos<=mid) Update(2*x,L,mid);
  36.     else Update(2*x+1,mid+1,R);
  37.     p[x]=min(p[2*x],p[2*x+1]);
  38.     return;
  39. }
  40. int Query(int x,int L,int R){
  41.     if(lo<=L&&R<=hi){
  42.         return p[x];
  43.     }
  44.     else if(L>hi||R<lo){
  45.         return M;
  46.     }
  47.     int mid=(L+R)/2;
  48.     return min(Query(2*x,L,mid),Query(2*x+1,mid+1,R));
  49. }
  50. int main(){
  51.     ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
  52. //    freopen(".inp","r",stdin);
  53. //    freopen(".out","w",stdout);
  54.     int n,t,x,y,q;
  55.     cin>>n>>t;
  56.     for(int i=1;i<=n;++i){
  57.         cin>>a[i];
  58.     }
  59.     Build(1,1,n);
  60.     while(t--){
  61.         cin>>q>>x>>y;
  62.         if(q==1){
  63.             pos=x;
  64.             val=y;
  65.             Update(1,1,n);
  66.         }
  67.         else{
  68.             lo=x; hi=y;
  69.             cout<<Query(1,1,n)<<endl;
  70.         }
  71.     }
  72.     return 0;
  73. }
  74. /*
  75. ==================================+
  76. INPUT:                            |
  77. ------------------------------    |
  78.  
  79. ------------------------------    |
  80. ==================================+
  81. OUTPUT:                           |
  82. ------------------------------    |
  83.  
  84. ------------------------------    |
  85. ==================================+
  86. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement