ACM: I Hate It 解题报告 - 线段树

I Hate It
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit

Status
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 

和前面题“敌兵布阵”一样,只要注意把父节点保存子节点最大值就可以了。AC代码:

 1 #include"iostream"
 2 #include"algorithm"
 3 #include"cstdio"
 4 #include"cstring"
 5 #include"cmath"
 6 #define max(a,b) a>b?a:b
 7 #define min(a,b) a<b?a:b
 8 #define lson l,m,rt<<1
 9 #define rson m+1,r,rt<<1|1
10 using namespace std;
11 const int MX = 200000+10;
12 int maxx[MX<<2];
13 void PushUp(int rt) {
14     maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);  //更新节点  父节点为子节点中较大的
15 }
16
17 void Build(int l,int r,int rt) {
18     if(r==l) {
19         scanf("%d",&maxx[rt]);
20         return ;
21     }
22     int    m=(r+l)>>1;
23     Build(lson);//建立左节点
24     Build(rson);//建立右节点
25     PushUp(rt); //回朔算出建立的每个点的美观值的和
26 }
27
28 void UpData(int p,int mx,int l,int r,int rt) {
29     if(r==l) {            //找到并更新目标点
30         maxx[rt]=mx;
31         return ;
32     }
33     int m=(r+l)>>1;
34     if(p<=m) UpData(p,mx,lson);  //如果不是目标点向左右寻找
35     if(p >m) UpData(p,mx,rson);
36     PushUp(rt);//将更新过的每个点的子节点的和更新。
37 }
38
39 int Query(int L,int R,int l,int r,int rt) {
40     if(L<=l&&R>=r)         //大小超过整个范围
41         return maxx[rt];  //返回该节点的值
42     int m=(r+l)>>1;
43     int ret=0;
44     if(L<= m) ret =max(ret, Query(L,R,lson));  //左节点的值
45     if(R > m) ret =max(ret, Query(L,R,rson));  //右节点的值
46     return ret;
47 }
48
49 int main() {
50     int T,n;
51     int a,b;
52     char s[2];
53     while(~scanf("%d%d",&n,&T)) {
54         Build(1,n,1);
55         while(T--) {
56             scanf("%s%d%d",s,&a,&b);
57             if(s[0]==‘Q‘) {
58                 printf("%d\n",Query(a,b,1,n,1));
59             }
60             if(s[0]==‘U‘) {
61                 UpData(a,b,1,n,1);
62             }
63         }
64     }
65     return 0;
66 }

时间: 2024-08-05 07:05:59

ACM: I Hate It 解题报告 - 线段树的相关文章

ACM Minimum Inversion Number 解题报告 -线段树

C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

ACM: 敌兵布阵 解题报告 -线段树

敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Lily 特别喜欢养花,但是由于她的花特别多,所以照料这些花就变得不太容易.她把她的花依次排成一行,每盆花都有一个美观值.如果Lily把某盆花照料的好的话,这盆花的美观值就会上升,如果照料的不好的话,这盆花的美观值就会下降.有时,Lily想知道某段连续的花的美观值之和是多少,但是,Lily的算术不是很好,你能快

ACM: Just a Hook 解题报告 -线段树

E - Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive met

ACM: Billboard 解题报告-线段树

Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在学校的入口处有一个巨大的矩形广告牌,高为h,宽为w.所有种类的广告都可以贴,比如ACM的广告啊,还有餐厅新出了哪些好吃的,等等.. 在9月1号这天,广告牌是空的,之后广告会被一条一条的依次贴上去. 每张广告都是高度为1宽度为wi的细长的矩形纸条. 贴广告的人总是会优先选择最上面的位置来帖,而且在所有

ACM: Hotel 解题报告 - 线段树-区间合并

Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever th

hdu acm 1166 敌兵布阵 (线段树)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37903    Accepted Submission(s): 15985 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

[BZOJ3065]带插入区间K小值 解题报告 替罪羊树+值域线段树

刚了一天的题终于切掉了,数据结构题的代码真**难调,这是我做过的第一道树套树题,做完后感觉对树套树都有阴影了......下面写一下做题记录. Portal Gun:[BZOJ3065]带插入区间k小值. 这道题的题面其实都提醒怎么做了,维护区间k小值用值域线段树,但要维护一个插入操作,树状数组套主席树也用不了,那么这道题还剩下平衡树可以搞,那就上平衡树吧. 我这里的做法,因为要维护序列的顺序,所以我这里用到替罪羊树套值域线段树:我们在替罪羊树的每个节点都套一颗值域线段树,记录以该节点为根的子树的

ACM学习历程—HDU 2795 Billboard(线段树)

Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in th

ACM学习历程—HDU 5289 Assignment(线段树)

Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a gr