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 }
  • 相关阅读:
    设置Sysctl.conf用以提高Linux的性能(最完整的sysctl.conf优化方案)
    XSS攻击及防御
    通过Nginx,Tomcat访问日志(access log)记录请求耗时
    nginx配置长连接
    nginx常见内部参数,错误总结
    nginx 并发数问题思考:worker_connections,worker_processes与 max clients
    Nginx与Tomcat、Client之间请求的长连接配置不一致问题解决[转]
    JavaScript的基准测试-不服跑个分?
    延迟求值-如何让Lo-Dash再提速x100?
    如果把编程语言当成座驾
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/4986383.html
Copyright © 2011-2022 走看看