HDU 1754 I Hate It
2016-04-30 11:09
363 查看
线段树入门
#include<stdio.h> #include<string.h> #include<algorithm> #define lson rt<<1 #define rson (rt<<1)+1 using namespace std; int a[1000001]; char b[1000001]; void build(int l,int r,int rt) { if(l==r) { scanf("%d",&a[rt]); return; } int mid=(l+r)>>1; build(l,mid,lson); build(mid+1,r,rson); a[rt]=max(a[lson],a[rson]); } void update(int l,int r,int rt,int pos,int value) { if(l==r) { a[rt]=value; return; } int mid=(l+r)/2; if(pos<=mid) update(l,mid,lson,pos,value); else update(mid+1,r,rson,pos,value); a[rt]=max(a[lson],a[rson]); } int query(int l,int r,int rt,int L,int R) { int ma=0; if(L<=l&&r<=R) { ma=max(a[lson],a[rson]); return a[rt]; } int mid=(l+r)>>1; if(L<=mid) ma=max(ma,query(l,mid,lson,L,R)); if(R>mid) ma=max(ma,query(mid+1,r,rson,L,R)); return ma; } int main() { int i,j,k,n,m,l,x,y; while(~scanf("%d %d",&n,&m)) { build(1,n,1); //printf("(%d)\n",a[2]); for(i=0;i<m;i++) { scanf("%s %d %d",b,&x,&y); if(b[0]=='Q') { printf("%d\n",query(1,n,1,x,y)); } else if(b[0]=='U') { // printf("(1)"); update(1,n,1,x,y); } } } return 0; }
相关文章推荐
- 【静态库&动态库】用最简单的例子说明静态库动态库的操作
- 深入浅出ES6(十三):类 Class
- IPv6 地址
- 第k短路径的学习,及A*算法的初步认识,以及usaco牛跑步,k短路径
- 确定比赛名次,hdu 1285 最简单的拓扑排序,
- P1073 加分二叉树
- 3月28日作业
- 第九章课后题
- LeetCode|Convert * to Binary Search Tree
- 安卓跳转到GPS设置界面
- LeetCode 342. Power of Four(4的n次幂)
- 内存泄露
- POJ 3461 Oulipo
- B树和B+树
- 线性表的顺序存储结构(一)
- 设计模式 ( 二十 ) 访问者模式Visitor(对象行为型)
- 龙卷风
- nodejs概论(实操篇)
- 设计模式 ( 二十 ) 访问者模式Visitor(对象行为型)
- Java设计模式之观察者模式