• B


     1 #include<cstdio>
     2 #include<string.h>
     3 using namespace std;
     4 int ans;
     5 int max1=0;
     6 int a[200010];
     7 struct Node{
     8     int left;
     9     int right;
    10     int max;
    11 }node[800010];
    12 void TreeMake(int l,int r,int i)
    13 {
    14     node[i].left=l;
    15     node[i].right=r;
    16     node[i].max=0;
    17     if(l==r)
    18     {
    19         node[i].max=a[l];//是a[l]!!!!!
    20         return;
    21     }
    22     else
    23     {
    24         int mid=(l+r)/2;
    25         TreeMake(l,mid,2*i);
    26         TreeMake(mid+1,r,2*i+1);
    27         node[i].max=node[2*i].max>node[2*i+1].max?node[2*i].max:node[2*i+1].max;//注意更新最大值,回溯
    28     }
    29 }
    30 void Updata_tree(int l,int i)
    31 {
    32     if(node[i].left==l&&node[i].right==l)
    33     {
    34          node[i].max=ans;
    35          return;
    36     }
    37 
    38     else{
    39         int mid=(node[i].left+node[i].right)/2;
    40         if(l<=mid) Updata_tree(l,2*i);
    41         if(l>mid) Updata_tree(l,2*i+1);
    42         node[i].max=node[2*i].max>node[2*i+1].max?node[2*i].max:node[2*i+1].max;//注意回溯!!
    43     }
    44 
    45 }
    46 
    47 void Query_tree(int l,int r,int i)
    48 {
    49    //if(node[i].left==l&&node[i].right==r)
    50      //   return node[i].max;
    51    //
    52    if(node[i].left>=l&&node[i].right<=r)
    53    {
    54        if(max1<node[i].max)
    55             max1=node[i].max;
    56        return;
    57    }
    58    int mid=(node[i].left+node[i].right)/2;
    59    if(r<=mid) Query_tree(l,r,2*i);
    60    else if(l>mid) Query_tree(l,r,2*i+1);
    61    else
    62    {
    63        Query_tree(l,r,2*i);
    64        Query_tree(l,r,2*i+1);
    65    }
    66 }
    67 
    68 
    69 
    70 int main()
    71 {
    72     int n,m;
    73     char ch[10];
    74     while(scanf("%d %d",&n,&m)!=EOF)
    75     {
    76         for(int i=1;i<=n;i++)
    77             scanf("%d",&a[i]);
    78             TreeMake(1,n,1);
    79         while(m--)
    80         {
    81             scanf("%s",ch);
    82             int x,y;
    83             scanf("%d %d",&x,&y);
    84             max1=0;
    85             if(ch[0]=='Q')
    86             {
    87                 Query_tree(x,y,1);//查询(x,y)区间
    88                 printf("%d
    ",max1);
    89             }
    90             if(ch[0]=='U')
    91             {
    92                 ans=y;
    93                 Updata_tree(x,1);
    94             }
    95         }
    96     }
    97     return 0;
    98 }
    Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
    这让很多学生很反感。

    不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
     

    Input

    本题目包含多组测试,请处理到文件结束。
    在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
    学生ID编号分别从1编到N。
    第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
    接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
    当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
    当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
     

    Output

    对于每一次询问操作,在一行里面输出最高成绩。
     

    Sample Input

    5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
     

    Sample Output

    5 6 5 9

    Hint

    Huge input,the C function scanf() will work better than cin 
  • 相关阅读:
    HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)
    HDU 4568 Hunter(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)
    URAL 1664 Pipeline Transportation(平面图最大流)
    HDU 1250 Hat's Fibonacci(高精度)
    HDU 1042 N!(高精度乘)
    算法模板の计算几何
    算法模板の数据结构
    算法模板の数学&数论
    算法模板之图论
    HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)
  • 原文地址:https://www.cnblogs.com/angledamon/p/3901642.html
Copyright © 2020-2023  润新知