Cod sursa(job #2659227)

Utilizator PulpysimusJurjiu Tandrau Darius Stefan Pulpysimus Data 16 octombrie 2020 10:10:08
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("date.in");
ofstream g("date.out");
# define INF 2147483647
int n,ST[262145],v[100000],m;
int GetMid(int x, int y)
{
    return x+(y-x)/2;
}
int constructST(int node, int st, int dr)
{
    if(st == dr) {ST[node]=v[st];
    return v[st];}
    int m=GetMid(st,dr);
    int x = constructST(2*node,st,m);
    int y = constructST(2*node+1,m+1,dr);
 ST[node]=min(x,y);
 return ST[node];
}
int GetMin (int node, int st, int dr, int nowst, int nowdr)
{
    if(nowst>dr || nowdr<st) return INF;
    if(st<=nowst && dr>=nowdr) return ST[node];
    int m=GetMid(nowst,nowdr);
    int x = GetMin(2*node,st,dr,nowst,m);
    int y = GetMin(2*node+1,st,dr,m+1,nowdr);
    return min(x,y);

}
void UpdateValue( int node, int st, int dr, int val, int i)
{
    if(i<st || i>dr) return ;
    ST[node]=min(ST[node],val);
    if(st!=dr){
    int m=GetMid(st,dr);
    UpdateValue(2*node,st,m,val,i);
        UpdateValue(2*node+1,m+1,dr,val,i);
    }
}
int main()
{
f>>n>>m;
int i,c=1,a,b;

for(i=1;i<=n;i++)
    f>>v[i];
//f>>m;
    constructST(1,1,n);

    for(i=1;i<=m;i++)
    {
        f>>a>>b;
        if(c==1)
            cout<<GetMin(1,a,b,1,n)<<"\n";
        else UpdateValue(1,1,n,b,a);

    }

}