zoukankan      html  css  js  c++  java
  • hdu I Hate It

    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 #include <math.h>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <ctype.h>
     7 #include <iomanip>
     8 #include <queue>
     9 #include <stdlib.h>
    10 
    11 using namespace std;
    12 
    13 const int maxn = 200000 + 10, INF = -214748364;
    14 int a[maxn], b[4*maxn], A, B;        //a为输入数组,maxv为线段树结点数组,存该相应区间的最大值
    15 
    16 void update(int o, int L, int R)        //线段树更新,把a[A]改为B
    17 {
    18     if(L == R)     
    19         b[o] = B;
    20     else
    21     {
    22         int M = L + (R - L) / 2;
    23         if(A <= M)     
    24             update(2*o, L, M);
    25         else    
    26             update(2*o+1, M+1, R);
    27         b[o] = max(b[2*o], b[2*o+1]);
    28     }
    29 }
    30 
    31 int query(int o, int L, int R)      //线段树查询[A, B]间的最大值
    32 {
    33     if(A <= L && R <= B)
    34        return b[o];
    35     int M = L + (R - L) / 2, ans = INF;
    36     if(A <= M)   
    37         ans = max(ans, query(2*o, L, M));
    38     if(B > M)    
    39         ans = max(ans, query(2*o+1, M+1, R));
    40     return ans;
    41 }
    42 
    43 void build(int o, int L, int R)     //建树
    44 {
    45 
    46     if(L == R)
    47     {
    48         b[o] = a[L];
    49         return;
    50     }
    51     int M = L + (R - L) / 2;
    52     if(L <= M)     
    53         build(2*o, L, M);
    54     if(R > M)      
    55         build(2*o+1, M+1, R);
    56     b[o]=max(b[2*o], b[2*o+1]);
    57 
    58 }
    59 int main()
    60 {
    61     int N, M;
    62     char c;
    63     while(~scanf("%d%d
    ", &N, &M))
    64     {
    65         for(int i = 1; i <= N; i++) scanf("%d", &a[i]);
    66         build(1, 1, N);
    67         /*for (int i = 1; i <= 2*N; ++i)
    68              {
    69                  printf("%d
    ",b[i]);    //5 3 5 2 3 4 5 1 2
    70              }*/
    71         while(M--)
    72         {
    73             scanf("
    %c%d%d", &c, &A, &B);
    74             if(c == 'U'){
    75              update(1, 1, N);
    76              /*for (int i = 1; i <= 2*N; ++i)
    77              {
    78                  printf("%d
    ",b[i]);  //6 6 5 2 6 4 5 1 2
    79              }*/
    80             }
    81             else printf("%d
    ", query(1, 1, N));
    82         }
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    bootstrap-table对前台页面表格的支持
    解决拦截器对ajax请求的的拦截
    jQuery获取鼠标事件源(万能)
    HtmlAgilityPack 处理通配的contains
    【转】XPath的学习
    在IIS服务器上部署svg/woff/woff2字体
    【转】万网域名查询接口(API)的说明
    html5和css3的常用参考网
    【转】Xml序列化
    C#序列化s实体类成Xml,去除空格、换行符以及命名空间
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/4986383.html
Copyright © 2011-2022 走看看