HDOJ-1754 I Hate It

来源:互联网 发布:sketch mac汉化破解版 编辑:程序博客网 时间:2024/05/18 00:56
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;#define maxn 200005struct Stu{    int l, r, s;}str[maxn*3];int num[maxn];void Build(int n, int l, int r){    str[n].l =l;    str[n].r = r;    if(l == r){        str[n].s = num[l];        return ;    }    int mid = (l + r) / 2;    Build(n<<1, l, mid);    Build(n<<1|1, mid+1, r);    str[n].s = max(str[n<<1].s, str[n<<1|1].s);}void Top(int n, int l, int r, int &s){    if(str[n].l == l && str[n].r == r)    {        s = max(s, str[n].s);        return ;    }    int mid = (str[n].l + str[n].r) / 2;    if(r <= mid){        Top(n<<1, l, r, s);    }    else if(l > mid){        Top(n<<1|1, l, r, s);    }    else{        Top(n<<1, l, mid, s);        Top(n<<1|1, mid+1, r, s);    }}void Update(int n, int a, int b){    if(str[n].l == str[n].r){        str[n].s = b;        return ;    }    int mid = (str[n].l + str[n].r) / 2;    if(a <= mid)       Update(n<<1, a, b);    else        Update(n<<1|1, a, b);    str[n].s = max(str[n<<1].s, str[n<<1|1].s);}int main(){    //freopen("in.txt", "r", stdin);    int n, m;    while(cin >> n >> m)    {        for(int i = 1; i <= n; i++)            scanf("%d ", num+i);        Build(1, 1, n);        char ch;        int a, b;        for(int i = 1; i <= m; i++)        {            scanf("%c %d %d ", &ch, &a, &b);            if(ch == 'Q')            {                int s = 0;                Top(1, a, b, s);                cout << s << endl;            }            else if(ch == 'U'){                Update(1, a, b);            }           }    }    return 0;}
0 0