fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long int
  3. #define ld long double
  4. #define nl "\n"
  5. #define ull unsigned long long
  6. #define rv return void
  7. #define str string
  8. #define all(x) x.begin(), x.end()
  9. #define allr(x) x.rbegin(), x.rend()
  10. #define vec vector
  11. #define fixed(n) fixed << setprecision(n)
  12. #define Moageza ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
  13. using namespace std;
  14. const ll mod = 1e9+7;
  15. //////////////////////////////////////////////////////
  16. struct SegmentTree{
  17. private:
  18. #define L 2*node+1
  19. #define R 2*node+2
  20. #define mid (l+r>>1)
  21. ll merge(ll x,ll y){
  22. return min(x,y);
  23. }
  24. int sz;vector<ll>seg;
  25. void update(int l,int r,int node,int idx,ll val)
  26. {
  27. if(l==r)
  28. {
  29. seg[node]=val;
  30. return;
  31. }
  32. if(idx<=mid)
  33. {
  34. update(l,mid,L,idx,val);
  35. }
  36. else
  37. {
  38. update(mid+1,r,R,idx,val);
  39. }
  40. seg[node]=merge(seg[L],seg[R]);
  41. }
  42. ll query(int l,int r,int node,int lq,int rq)
  43. {
  44. if(r<lq||l>rq)
  45. {
  46. return LLONG_MAX;
  47. }
  48. if(l>=lq&&r<=rq)
  49. {
  50. return seg[node];
  51. }
  52. ll lft=query(l,mid,L,lq,rq);
  53. ll rgt=query(mid+1,r,R,lq,rq);
  54. return merge(lft,rgt);
  55. }
  56. public:
  57. SegmentTree(int n)
  58. {
  59. sz=1;
  60. while(sz<n) sz*=2;
  61. seg=vector<ll>(sz*2);
  62. }
  63. void update(int idx,ll val)
  64. {
  65. update(0,sz-1,0,idx,val);
  66. }
  67. ll query(int l,int r)
  68. {
  69. return query(0,sz-1,0,l,r);
  70. }
  71. };
  72. void solve(){
  73. int a,b;cin >> a;
  74. SegmentTree st(a);
  75. for(int i=0;i<a;i++){
  76. int x;cin >> x;
  77. st.update(i,x);
  78. }
  79. cin>> b;
  80. for(int i=0;i<b;i++){
  81. int x,y;cin >>x>>y;
  82. cout <<st.query(x,y)<<nl;
  83. }
  84. }
  85. int main()
  86. {
  87. Moageza
  88. #ifndef ONLINE_JUDGE
  89. freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
  90. #endif
  91. int t = 1;
  92. // cin >> t;
  93. while (t--) {
  94. solve();
  95. cout << nl;
  96. }
  97. return 0;
  98. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout