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): 22019    Accepted Submission(s): 8792


    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
    Hint
    Huge input,the C function scanf() will work better than cin
     
    Author
    linle
     
    Source
     
    Recommend
    lcy
     
    AC Code:
     1 #include <iostream>
     2 #include <cstdio>
     3    
     4 using namespace std;
     5    
     6 const int MAXN = 200000;
     7 struct Node
     8 {
     9     int right, left;
    10     int score;
    11 }tree[MAXN * 3];
    12    
    13 int Max(int a, int b) {return a > b ? a : b;}
    14    
    15 void Build(int left, int right, int rt)
    16 {
    17     tree[rt].left = left;
    18     tree[rt].right = right;
    19     if(left == right)
    20     {
    21         scanf("%d", &tree[rt].score);
    22         return;
    23     }
    24     int  mid = (left + right) >> 1;
    25     Build(left, mid, rt << 1);  //mid落在左子树
    26     Build(mid + 1, right, rt << 1 | 1);
    27     tree[rt].score = Max(tree[rt << 1].score, tree[rt << 1 | 1].score);
    28 }
    29    
    30 void Update(int rt, int id, int newScore)
    31 {
    32     if(tree[rt].left == tree[rt].right)
    33     {
    34         tree[rt].score = newScore;
    35         return ;
    36     }
    37     int mid = (tree[rt].left + tree[rt].right) >> 1;
    38     if(id > mid) Update(rt << 1 | 1, id, newScore);
    39     else Update(rt << 1, id, newScore);
    40     tree[rt].score = Max(tree[rt << 1].score, tree[rt << 1 | 1].score);
    41 }
    42    
    43 int Query(int rt, int from, int to)
    44 {
    45     if(from <= tree[rt].left && to >= tree[rt].right)
    46         return tree[rt].score;
    47     int mid = (tree[rt].left + tree[rt].right) >> 1;
    48     if(from > mid) return Query(rt << 1 | 1, from, to);
    49     else if(to <= mid) return Query(rt << 1, from, to);
    50     else return Max(Query(rt << 1 | 1, mid + 1, to), Query(rt << 1, from, mid));
    51 }
    52    
    53 int main()
    54 {
    55     int m, n, a, b;
    56     char c;
    57     while(scanf("%d %d", &n, &m) != EOF)
    58     {
    59         Build(1, n, 1);
    60         while(m--)
    61         {
    62             cin >> c;
    63             scanf("%d %d", &a, &b);
    64             if(c == 'U') Update(1, a, b);
    65             else printf("%d\n", Query(1, a, b));
    66         }
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    pam_cracklib module
    转:ubuntu svn
    fw: subversion+apache config
    转:jenkins 简介
    fw:java 10大必知技术
    FW: Linux 搭建 csvn 服务器。
    转:修改svn 端口号
    转:Ubuntu搭建jenkins
    转:vmware workstation的三种网络连接方式。
    图像处理之直方图均衡
  • 原文地址:https://www.cnblogs.com/cszlg/p/2932423.html
Copyright © 2011-2022 走看看