hdu 3308(单点更新,区间合并)

来源:互联网 发布:苹果8plus网络设置 编辑:程序博客网 时间:2024/05/25 21:36

没什么好说的,赤裸裸的线段树...


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;#define lc l,m,index<<1#define rc m+1,r,index<<1|1#define N 100005struct node{int most;int left,right;int lmost,rmost;}seg[N<<2];int n,q;void pushup(int l,int r,int index){int m=(l+r)>>1;node& father=seg[index];node& lson=seg[index<<1];node& rson=seg[index<<1|1];father.left=lson.left;father.right=rson.right;father.lmost=lson.lmost;father.rmost=rson.rmost;father.most=max(lson.most,rson.most);if(lson.right<rson.left){father.most=max(father.most,lson.rmost+rson.lmost);if(lson.lmost==m-l+1)father.lmost+=rson.lmost;if(rson.rmost==r-m)father.rmost+=lson.rmost;}}void build(int l,int r,int index){int m=(l+r)>>1;if(l==r){scanf("%d",&seg[index].left);seg[index].right=seg[index].left;seg[index].most=seg[index].lmost=seg[index].rmost=1;return;}build(lc);build(rc);pushup(l,r,index);}void updata(int id,int num,int l,int r,int index){int m=(l+r)>>1;if(l==r){seg[index].left=seg[index].right=num;return;}if(id<=m)updata(id,num,lc);else updata(id,num,rc);pushup(l,r,index);}int query(int L,int R,int l,int r,int index){int m=(l+r)>>1;if(L==l&&R==r)return seg[index].most;if(R<=m)return query(L,R,lc);else if(L>m)return query(L,R,rc);else{int ret;ret=max(query(L,m,lc),query(m+1,R,rc));if(seg[index<<1].right<seg[index<<1|1].left){ret=max(ret,min(R,m+seg[index<<1|1].lmost)-max(L,m-seg[index<<1].rmost+1)+1);}return ret;}}int main(){int t,a,b;char op[2];scanf("%d",&t);while(t--){scanf("%d%d",&n,&q);build(0,n-1,1);while(q--){scanf("%s%d%d",op,&a,&b);if(op[0]=='U'){updata(a,b,0,n-1,1);}else{printf("%d\n",query(a,b,0,n-1,1));}}}return 0;}


原创粉丝点击