HDU 1754线段树基本操作,建树,更新,查询

代码线段树入门整理中有介绍、

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 const int MAXNODE=1<<19;
 7 const int MAX=1000003;
 8 struct NODE{
 9     int left,right;
10     int value;
11 }node[MAXNODE];
12 int father[MAX];
13 void buildtree(int i,int left,int right)
14 {
15     node[i].left=left;
16     node[i].right=right;
17     node[i].value=0;
18     if(left==right){
19         father[left]=i;
20         return;
21     }
22     buildtree(i<<1, left, (int)floor( (right+left) / 2.0));
23     buildtree((i<<1) + 1, (int)floor( (right+left) / 2.0) + 1, right);
24
25 }
26 void updatatree(int ri)
27 {
28     if(ri==1)    return;
29     int fi=ri/2;
30     int a=node[fi<<1].value;
31     int b=node[(fi<<1)+1].value;
32     node[fi].value=a>b?a:b;
33     updatatree(ri/2);
34 }
35 int maxn;
36 void query(int i,int l,int r)
37 {
38     if(node[i].left==l&&node[i].right==r){
39         maxn=(maxn<node[i].value)?node[i].value:maxn;
40         return;
41     }
42     i=i<<1;
43     if(l<=node[i].right)
44         if(r<=node[i].right)
45             query(i,l,r);
46         else
47             query(i,l,node[i].right);
48     i+=1;
49     if(r>=node[i].left)
50         if(l>=node[i].left)
51             query(i,l,r);
52         else
53             query(i,node[i].left,r);
54 }
55 int main()
56 {
57     int n,m;
58     while(~scanf("%d %d",&n,&m)){
59         buildtree(1,1,n);
60         int grade;
61         for(int i=1;i<=n;++i){
62             scanf("%d",&grade);
63             node[father[i]].value=grade;
64             updatatree(father[i]);
65         }
66         while(m--){
67             int x,y;char str[5];
68             scanf("%s %d %d",str,&x,&y);
69             if(str[0]==‘U‘){
70                 node[father[x]].value=y;
71                 updatatree(father[x]);
72             }
73             else{
74                 maxn=0;
75                 query(1,x,y);
76                 printf("%d\n",maxn);
77             }
78         }
79     }
80 }
时间: 2024-12-16 20:06:29

HDU 1754线段树基本操作,建树,更新,查询的相关文章

HDU 4893 线段树的 点更新 区间求和

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

HDU 4902 线段树(区间更新)

Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 353    Accepted Submission(s): 169 Problem Description There is an old country and the king fell in love with a devil. The devil alw

HDU(1754),线段树,单点替换,区间最值

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树模板题,update功能是单点替换,query是访问区间最大值. #include <stdio.h> #include <algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn = 200010; int MAX

B - I Hate It HDU 1754 线段树

B - I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1754 Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有时候需要更新某位同学的成绩. Input 本题

B - I Hate It HDU - 1754 线段树区间最大值板子(单点更新,区间最大)

第一次打 改了半天  各种小错误 难受 1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 const int maxn=2000000+7; 5 int a[maxn],n; 6 struct Node{ 7 int l,r; 8 long long Max,lazy; 9 void update(long long val){ 10 ;//本题没用 11 } 12 }tree[maxn*5]; 13

hdu 1754 线段树 水题 单点更新 区间查询

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 59558    Accepted Submission(s): 23201 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要

hdu 1754 线段树 单点更新 动态区间最大值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 52417    Accepted Submission(s): 20598 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的

POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13805   Accepted: 5996 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

hdu 1698 线段树 成段更新

题意:一段钩子,每个钩子的值为1,有若干更新,每次跟新某段的值,若干查询某段的和 基础题了 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define