[线段树]HDU 1754 I hate it

来源:互联网 发布:怎样安装电脑软件 编辑:程序博客网 时间:2024/05/29 02:34

B - I Hate It
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
http://acm.hdu.edu.cn/showproblem.php?pid=1754

基础线段树,单点更新,这次线段树里存的是最大值

#include <iostream>#include <cstdio>using namespace std;const int MAXN = 200020;const int INF = 0x3f3f3f3f;int maxNum;int N,M;int a[MAXN];char ch[20];struct node{    int L,R;    int maxV;    int mid(){return (L+R)/2;}};node tree[800020];void build(int root, int L, int R){    tree[root].L = L;    tree[root].R = R;    tree[root].maxV = -INF;    if (L != R)    {        build(root*2,L,(L+R)/2);        build(root * 2 +1 ,(L+R)/2+1,R);    }}void insert(int root, int i, int val){    tree[root].maxV = max(tree[root].maxV,val);    if(tree[root].L == tree[root].R)    {        return ;    }    if (i <=tree[root].mid())        insert(root*2,i,val);    else        insert(root * 2 +1,i,val);}void query(int root,int s,int e){    if(tree[root].maxV <= maxNum)        return;    if (tree[root].L ==s && tree[root].R == e)    {        maxNum = max(tree[root].maxV,maxNum);        return ;    }    if (e<=(tree[root].L+tree[root].R)/2)        query(root*2,s,e);    else if (s>(tree[root].L+tree[root].R)/2)        query(root*2+1,s,e);    else     {        query(root*2,s,(tree[root].L+tree[root].R)/2);        query(root*2+1,(tree[root].L+tree[root].R)/2+1,e);    }}int main(){    int L,R,k,v;    while(cin>>N>>M)    {        build(1,1,N);        for(int i =1; i<=N;i++)        {            scanf("%d",&a[i]);            insert(1,i,a[i]);        }        while(M--)        {            scanf("%s",ch);            if (ch[0] =='Q')            {                scanf("%d%d",&L,&R);                maxNum = -INF;                query(1,L,R);                printf("%d\n",maxNum);            }            else if (ch[0] =='U')            {                scanf("%d%d",&k,&v);                insert(1,k,v);            }        }    }    return 0;}
0 0