标签:des style class blog code tar
Description
Input
Output
Sample Input
Sample Output
/*线段树求最值*/ #include"iostream" #include"cstring" #include"cstdio" #include"algorithm" //include"swap,max" using namespace std; //#define lson rt<<1,l,mid define 好耗时间, //#define rson rt<<1|1,mid+1,r //#define max(a,b) ((a)>(b))?(a):(b) #define MAX 200009 int stu[MAX]; struct node { int l; int r; int maxn; }tree[4*MAX]; void build(int rt,int l,int r) { tree[rt].l=l; tree[rt].r=r; int mid=(l+r)>>1; if(l==r) { tree[rt].maxn=stu[mid]; return ; } build(rt<<1,l,mid); build(rt<<1|1,mid+1,r); tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); } int query(int rt,int x,int y) { int l=tree[rt].l; int r=tree[rt].r; if(l==x&&r==y) return tree[rt].maxn; int mid=(l+r)>>1; if(x<=mid&&y<=mid) return query(rt<<1,x,y); else if(x>mid) return query(rt<<1|1,x,y); else return max(query(rt<<1,x,mid),query(rt<<1|1,mid+1,y)); } void updata(int rt,int x,int d) { int l,r,mid; l=tree[rt].l; r=tree[rt].r; mid=(l+r)>>1; if(l==r) { tree[rt].maxn=d; return ; } if(x<=mid) updata(rt<<1,x,d); else updata(rt<<1|1,x,d); tree[rt].maxn=max(query(rt<<1,l,mid),query(rt<<1|1,mid+1,r)); } int main() { int n,m; int a,b,i; char c; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) scanf("%d",&stu[i]); build(1,1,n); for(i=1;i<=m;i++) { getchar();//整数后是字符 scanf("%c%d%d",&c,&a,&b); if(c==‘Q‘) { printf("%d\n",query(1,a,b)); } else if(c==‘U‘) updata(1,a,b); } } return 0; }
标签:des style class blog code tar
原文地址:http://www.cnblogs.com/767355675hutaishi/p/3706022.html