[HDU1754]I Hate It线段树裸题

http://acm.hdu.edu.cn/showproblem.php?pid=1754

解题关键:刚开始死活超时,最后发现竟然是ch,和t1、t2每次循环都定义的锅,以后养成建全局变量的习惯。

注意背模板时,find时最后无赋值

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 struct node{
 5     int left,right,max;
 6 }tree[800002];
 7 int num[200002];
 8 char ch;
 9 int t1,t2;
10 int build(int root,int left,int right){
11     tree[root].left=left;
12     tree[root].right=right;
13     if(left==right) return tree[root].max=num[left];
14
15     int a,b,mid;
16     mid=(left+right)/2;
17     a=build(2*root,left,mid);
18     b=build(2*root+1,mid+1,right);
19     return tree[root].max=max(a,b);
20 }
21
22 int find(int root,int left,int right){
23     if(right<tree[root].left||left>tree[root].right) return 0;
24     if(left<=tree[root].left&&tree[root].right<=right) return tree[root].max;
25
26     int a,b;
27     a=find(2*root,left,right);
28     b=find(2*root+1,left,right);
29     return max(a,b);//find时不能赋值
30 }
31
32 int update(int root,int pos,int val){
33     if(pos<tree[root].left||pos>tree[root].right) return tree[root].max;
34     if(pos==tree[root].left&&pos==tree[root].right) return tree[root].max=val;
35
36     int a,b;
37     a=update(2*root,pos,val);
38     b=update(2*root+1,pos,val);
39     return tree[root].max=max(a,b);
40 }
41 int n,m;
42 int main(){
43     while(scanf("%d%d",&n,&m)!=EOF){
44         memset(num,0,sizeof num);
45         memset(tree,0,sizeof tree);
46         for(int i=1;i<=n;i++){
47             scanf("%d",num+i);
48         }
49         build(1,1,n);
50         for(int i=0;i<m;i++){
51             getchar();
52             scanf("%c%d%d",&ch,&t1,&t2);
53             if(ch==‘Q‘){
54                 int res=find(1,t1,t2);
55                 printf("%d\n",res);
56             }
57             else{
58                 update(1,t1,t2);
59             }
60         }
61     }
62
63 }
时间: 2024-08-05 14:33:45

[HDU1754]I Hate It线段树裸题的相关文章

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

HDU 4027 Can you answer these queries? 线段树裸题

题意: 给定2个操作 0.把区间的每个数sqrt 2.求和 因为每个数的sqrt次数很少,所以直接更新到底,用个标记表示是否更新完全(即区间内的数字只有0,1就不用再更新了) #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #incl

POJ 3468 线段树裸题

这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了ACdreamer的模板,在此基础上自己用宏定义来精简了一下代码: 1 #include<cstdio> 2 typedef long long LL; 3 #define root int rt, int l, int r 4 #define lson rt*2, l, mid 5 #define

bzoj 1036 树链剖分+线段树 裸题

HYSBZ - 1036 题意:中文题 思路:树链剖分裸题,线段树写得比较搓,(在线段树上修改节点u的时候应该修改u映射到线段树后的节点序号,这里wa了半年,真的是半年) AC代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector

CPU监控 线段树裸题

LINK:bzoj3064 此题甚好码了20min停下来思考的时候才发现不对的地方有点坑... 还真不好写来着 可这的确是线段树的裸题...我觉得我写应该没有什么大问题 不过思路非常的紊乱 如果是自己写的话 所以为了自己能写出来 整理思路就是这篇博客了. Q X Y:询问从X到Y这段时间内CPU最高使用率 A X Y:询问从X到Y这段时间内之前列出的事件使CPU达到过的最高使用率 P X Y Z:列出一个事件这个事件使得从X到Y这段时间内CPU使用率增加Z C X Y Z:列出一个事件这个事件使

Bzoj 3050: [Usaco2013 Jan]Seating(线段树裸题,然而区间修改标记下放和讨论Push_up很揪心)

题目链接 题意:开始有一个空白的区间,每次可能进行两个操作:A 将一个长度为p的区间加入一段连续空白的位置 L:一个区间恢复空白:要求出A不能进行的次数. 非常裸的线段树题目,用线段树统计最大的空白区间,每个节点需要记录当前区间的最长空白区间,从左端开始的最长空白区间,从右端开始的最长空白区间.Push_up的时候要讨论下,可以分别取[l,mid]和[mid+1,r]的最大空白区间,也可以用[l,mid]的从右端开始的最长空白区间+[mid+1,r]从左端开始的最大空白区间. 每次A的时候,就查

hdu3966 树链剖分+线段树 裸题

HDU - 3966 题意:给一颗树,3种操作,Q u 查询u节点的权值,I a b c 对a到b的路径上每个点的点权增加c,D a b c 对a b 路径上所有点的点权减少c 思路:树链剖分+线段树,2个问题,第一,如果是先建树再输入点的点权,记录tip(点映射到线段树后的位置),如果先输入点权,再建树,不仅要记录tip还要记录ran(线段树上某个位置上的点对应的树上点的序号,与tip是相互映射):第二,连接起线段树和树链剖分的是get函数,区间操作才需要用到get函数,单点操作直接在线段树上

codeforces 339D Xenia and Bit Operations 线段树裸题

题目链接 题意: 给定n,下面2^n个数. 第一次 把 a1|a2, a3|a4, 如此得到一个 2^(n-1)个数的序列. 再把这个序列 a1^a2, a3^a4 , 得到一个2^(n-2) 个数的序列 再进行 a1|a2, a3|a4 ··· 直到只剩下一个数v, 我们称v是这个2^n 序列的权值. 下面m个询问: 询问格式: p, b 表示 a[p] = b; 再输出此时序列的权值. 思路:因为这个序列一定是2的倍数,所以用线段树直接这样操作即可.push_up时的深度奇偶来判断此时应该用

HDU1166 线段树裸题 区间求和

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