zoukankan      html  css  js  c++  java
  • I Hate It(线段树)

    I Hate It

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 52514    Accepted Submission(s): 20640


    Problem 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
    题解:线段树模板
    代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define lson root<<1,l,mid
     4 #define rson root<<1|1,mid+1,r
     5 const int MAXN=200010<<2;
     6 #define X int root,int l,int r
     7 #define Y 1,1,N
     8 #define MAX(x,y)(x>y?x:y)
     9 #define NOW tree[root]=MAX(tree[root<<1],tree[root<<1|1])
    10 int tree[MAXN];
    11 int A,B,anser;
    12 void build(X){
    13     if(l==r)scanf("%d",&tree[root]);
    14     else {
    15         int mid=(l+r)>>1;
    16         build(lson);
    17         build(rson);
    18         NOW;
    19     }
    20 }
    21 void update(X){
    22     if(l==A&&r==A)tree[root]=B;
    23     else{
    24         int mid=(l+r)>>1;
    25         if(A<=mid)update(lson);
    26         else update(rson);
    27         NOW;
    28     }
    29 }
    30 void print(X){
    31     if(A<=l&&B>=r)anser=MAX(anser,tree[root]);//这个是从线段树的顶往下搜的 
    32     else{
    33         int mid=(l+r)>>1;
    34         if(A<=mid)print(lson);//记住mid 
    35         if(B>mid)print(rson);
    36         NOW;
    37     }
    38 }
    39 int main(){
    40     int N,M;
    41     char s[2];
    42     while(~scanf("%d%d",&N,&M)){
    43         build(Y);
    44         while(M--){
    45             scanf("%s",s);
    46         if(!strcmp(s,"U")){
    47             scanf("%d%d",&A,&B);
    48             update(Y);
    49         }
    50         else{
    51             anser=0;
    52             scanf("%d%d",&A,&B);
    53             print(Y);
    54             printf("%d
    ",anser);
    55         }
    56         }
    57     }
    58     return 0;
    59 }

     第二种写法:奇葩错误,少了个M--

     1 #include<algorithm>
     2 using namespace std;
     3 #include<stdio.h>
     4 #include<string.h>
     5 const int INF=0x3f3f3f3f;
     6 const int MAXN=200010;
     7 #define lson root<<1,l,mid
     8 #define rson root<<1|1,mid+1,r
     9 #define L tree[root].l
    10 #define R tree[root].r
    11 #define V tree[root].v
    12 #define NOW tree[root].v=MAX(tree[root<<1].v,tree[root<<1|1].v)
    13 //#define LOCAL
    14 int MAX(int x,int y){
    15     return x>y?x:y;
    16 }
    17 struct Node{
    18     int l,r,v;    
    19 };
    20 Node tree[MAXN<<2];
    21 int ans;
    22 void build(int root,int l,int r){
    23     L=l;R=r;
    24     if(l==r){
    25         scanf("%d",&V);
    26     }
    27     else{
    28         int mid=(l+r)>>1;
    29         build(lson);
    30         build(rson);
    31         NOW;
    32     }
    33 }
    34 void update(int root,int x,int y){
    35     if(L==x&&R==x){
    36         V=y;
    37     }
    38     else{
    39         int mid=(L+R)>>1;
    40         if(mid>=x)update(root<<1,x,y);
    41         else update(root<<1|1,x,y);
    42         NOW;
    43     }
    44 }
    45 void query(int root,int x,int y){
    46     if(L>=x&&R<=y){
    47         ans=MAX(ans,V);
    48     }
    49     else {
    50         int mid=(L+R)>>1;
    51         if(mid>=x)query(root<<1,x,y);
    52         if(mid<y)query(root<<1|1,x,y);
    53     }
    54 }
    55 int main(){
    56     #ifdef LOCAL
    57     freopen("data.in","r",stdin);
    58     freopen("data.out","w",stdout);
    59     #endif
    60     int N,M;
    61     char s[5];
    62     int a,b;
    63     while(~scanf("%d%d",&N,&M)){
    64         build(1,1,N);
    65         while(M--){
    66         scanf("%s%d%d",s,&a,&b);
    67         if(s[0]=='U'){
    68             update(1,a,b);
    69         }
    70         else{
    71             ans=0;
    72             if(a>b)swap(a,b);
    73             query(1,a,b);
    74             printf("%d
    ",ans);
    75             }
    76         }
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    codeforces 1060 B
    codeforces 1060 A
    牛客 国庆七天乐 day1 L
    BZOJ 1087: [SCOI2005]互不侵犯King
    codeforces 792CDivide by Three(两种方法:模拟、动态规划
    codeforces 797C Minimal string
    codeforces 110E Lucky Tree
    codeforces 798D
    2017福建省赛 FZU2272~2283
    Android -- Looper、Handler、MessageQueue等类之间关系的序列图
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4799057.html
Copyright © 2011-2022 走看看