• I Hate It(线段树模板)


    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

    http://acm.hdu.edu.cn/showproblem.php?pid=1754

    View Code
     1 #include <iostream>
    2 #include <cstdio>
    3 using namespace std;
    4 #define N 200000
    5 #define max(a,b) ((a)>(b)? (a):(b))
    6 struct node
    7 {
    8 int l,r;
    9 int mx;
    10 }tree[3*N];
    11 void build(int l,int r,int i)
    12 {
    13 tree[i].l=l;tree[i].r=r;
    14 if(l==r)
    15 {
    16 scanf("%d",&tree[i].mx);
    17 return ;
    18 }
    19 int mid=(l+r)>>1;
    20 build(l,mid,i<<1);
    21 build(mid+1,r,i<<1|1);
    22 tree[i].mx=max(tree[i<<1].mx,tree[i<<1|1].mx);
    23 }
    24 int query(int l,int r,int i)
    25 {
    26 if(l<=tree[i].l&&tree[i].r<=r) return tree[i].mx;
    27 int temp,ans=0,mid=tree[i<<1].r;
    28 if(l<=mid) {temp=query(l,r,i<<1);ans=max(ans,temp);}
    29 if(r>mid) {temp=query(l,r,i<<1|1);ans=max(ans,temp);}
    30 return ans;
    31 }
    32 void update(int k,int num,int i)
    33 {
    34 if(tree[i].l==tree[i].r) {tree[i].mx=num;return;}
    35 if(k<=tree[i<<1].r) update(k,num,i<<1);
    36 else update(k,num,i<<1|1);
    37 tree[i].mx=max(tree[i<<1].mx,tree[i<<1|1].mx);
    38 }
    39 int main()
    40 {
    41 int n,m;
    42 while(cin>>n>>m)
    43 {
    44 build(1,n,1);
    45 while(m--)
    46 {
    47 char ch[3];int a,b;
    48 scanf("%s%d%d",ch,&a,&b);
    49 if(ch[0]=='Q') printf("%d\n",query(a,b,1));
    50 else update(a,b,1);
    51 }
    52 }
    53 return 0;
    54 }
  • 相关阅读:
    网站实时信息采集和统计graphite
    内存检查工具Valgrind
    usr/bin/ld: cannot find 错误解决方法和 /etc/ld.so.conf
    通用makefile
    关于/proc/进程idpid/fd ,根据fd来查找连接
    boost enable_shared_from_this
    cdll和windll的差别
    一些项目——空白格式化
    Session笔记
    黑马程序猿_7K面试题之交通灯系统
  • 原文地址:https://www.cnblogs.com/qijinbiao/p/2412469.html
Copyright © 2020-2023  润新知