Pagini recente » Cod sursa (job #2245929) | Cod sursa (job #476153) | Cod sursa (job #2370270) | Cod sursa (job #653304) | Cod sursa (job #2817442)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF=1e9+7;
const ll INFF=1e18+7;
#define nl '\n'
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char* x) { cout << '\"' << x << '\"'; }
void __print(const string& x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template<typename T, typename V>
void __print(const pair<T, V>& x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; }
template<typename T>
void __print(const T& x) { int f = 0; cout << '{'; for (auto& i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; }
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
class SegTree{
public:
int leftmost, rightmost;
int minn=INF;
SegTree *leftChild, *rightChild;
SegTree(int leftmost, int rightmost, vector<int> a) :
leftmost(leftmost), rightmost(rightmost){
if(leftmost==rightmost) minn=a[leftmost]; //leaf
else{ // 2 children
int mid=(leftmost+rightmost)/2;
leftChild = new SegTree(leftmost, mid, a);
rightChild = new SegTree(mid+1, rightmost, a);
recalc();
}
}
void recalc(){
if(leftmost==rightmost) return;
minn=min(leftChild->minn,rightChild->minn);
}
void pointUpdate(int index, int newVal){
if(leftmost==rightmost) minn=newVal;
else if(index<=leftChild->rightmost) leftChild->pointUpdate(index,newVal);
else rightChild->pointUpdate(index, newVal);
recalc();
}
ll rangeSum(int l, int r){
//entirely disjoint
if(l>rightmost || r<leftmost) return INF;
//covers us
if(l<=leftmost && r>=rightmost) return minn;
//wdk
return min(leftChild->rangeSum(l,r),rightChild->rangeSum(l,r));
}
};
void solve()
{
int n,q;
cin>>n>>q;
vector<int> a(n);
for(int i=0; i<n; i++){
cin>>a[i];
}
SegTree tree(0,n-1,a);
for(int i=0; i<q; i++){
int x, y;
cin>>x>>y;
cout<<tree.rangeSum(x-1,y-1)<<nl;
}
}
int main()
{
// #ifndef ONLINE_JUDGE
freopen("rmq.in", "r", stdin);
freopen("rmq.out", "w", stdout);
// #endif
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
// int t;
// cin >> t;
for(int tt=1; tt<=t; tt++){
// cout<<"#Case "<<t<<nl;
solve();
}
return 0;
}