hdu 3308 线段树

来源:互联网 发布:mac 粉色口红 编辑:程序博客网 时间:2024/06/05 21:58

LCIS

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4082    Accepted Submission(s): 1842


Problem Description
Given n integers.
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].
 

Input
T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
OR
Q A B(0<=A<=B< n).
 

Output
For each Q, output the answer.
 

Sample Input
110 107 7 3 3 5 9 9 8 1 8 Q 6 6U 3 4Q 0 1Q 0 5Q 4 7Q 3 5Q 0 2Q 4 6U 6 10Q 0 9
 

Sample Output
11423125
 

#include <cstdio>#include <cstring>#include <cassert>#include <vector>#include <algorithm>#include <iostream>using namespace std;#define N 100050#define LL L, m, c<<1#define RR m+1, R, c<<1|1#define max(a, b) a>b?a:bstruct node{int l, r, a; };int dt[N], lt[N<<2], rt[N<<2], at[N<<2];// data, left, right, allvoid pushUp(int L, int R, int c){    int m=(L+R)>>1;    lt[c]=lt[c<<1]; rt[c]=rt[c<<1|1];    at[c]=max(at[c<<1], at[c<<1|1]);    if(dt[m]<dt[m+1]){        if(lt[c<<1]==m-L+1) lt[c]+=lt[c<<1|1];        if(rt[c<<1|1]==R-m) rt[c]+=rt[c<<1];        at[c]=max(at[c], rt[c<<1]+lt[c<<1|1]);    }}// seg buildvoid build(int L, int R, int c){    if(L==R) {        scanf("%d", &dt[L]);        lt[c]=rt[c]=at[c]=1;        return;    }    int m=(L+R)>>1;    build(LL);    build(RR);    pushUp(L, R, c);}// point updatevoid update(int l, int v, int L, int R, int c){    if(l<=L && R<=l){        dt[l]=v;    return;    }    int m=(L+R)>>1;    if(l<=m) update(l, v, LL);    else update(l, v, RR);    pushUp(L, R, c);}// seg quesnode ques(int l, int r, int L, int R, int c){    node lv, rv, av;    av.a=av.l=av.r=1;    if(l==L && R==r) {        av.a=at[c];        av.l=lt[c];        av.r=rt[c];        return av;    }    int m=(L+R)>>1;    if(r<=m) return ques(l, r, LL);    else if(l>m) return ques(l, r, RR);    else {        lv=ques(l, m, LL);        rv=ques(m+1, r, RR);        av.l=lv.l; av.r=rv.r;        av.a=max(lv.a, rv.a);        if(dt[m]<dt[m+1]) {            if(lv.l==m-l+1) av.l+=rv.l;            if(rv.r==r-m) av.r+=lv.r;            av.a=max(av.a, lv.r+rv.l);        }        return av;    }}int main(){    int t, n, q;    scanf("%d", &t);    while(t--){        scanf("%d%d", &n, &q);        build(1, n, 1);        char ch[2];        int a, b;        while(q--){            scanf("%s%d%d", ch, &a, &b);            if(*ch=='Q')                printf("%d\n", ques(a+1, b+1, 1, n, 1).a);            else if(*ch=='U') update(a+1, b, 1, n, 1);            else assert(0>1);        }    }    return 0;}



#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <iostream>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define maxn  100005using namespace std;int mlen[maxn<<2],lmlen[maxn<<2],rmlen[maxn<<2];int num[maxn];void pushup(int rt,int l,int r){    lmlen[rt]=lmlen[rt<<1];    rmlen[rt]=rmlen[rt<<1|1];    mlen[rt]=max(mlen[rt<<1],mlen[rt<<1|1]);    int mid=(l+r)>>1;    int m=(r-l)+1;    if(num[mid]<num[mid+1]) //左区间的右值小于右区间的左值可以合并    {        if(mlen[rt<<1]==m-(m>>1)) lmlen[rt]+=lmlen[rt<<1|1];        if(mlen[rt<<1|1]==(m>>1)) rmlen[rt]+=rmlen[rt<<1];        mlen[rt]=max(mlen[rt],lmlen[rt<<1|1]+rmlen[rt<<1]);    }}void build(int l,int r,int rt){    if(l==r)    {        mlen[rt]=lmlen[rt]=rmlen[rt]=1;        return ;    }    int m=(l+r)>>1;    build(lson);    build(rson);    pushup(rt,l,r);}void  update(int a,int b,int l,int r,int rt){    if(l==r)    {        num[l]=b;        return;    }    int m=(l+r)>>1;    if(a<=m) update(a,b,lson);    else update(a,b,rson);    pushup(rt,l,r);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        return mlen[rt];    }    int m=(l+r)>>1;    if(R<=m) return query(L,R,lson); //与一般不同,不能是子集,只能全部属于才能直接查询子区间 返回子区间的mlen    else if(L>m) return query(L,R,rson);  //否则是确认儿子区间是否能合并,并在三者之间取最大值    else {    int ta,tb;    ta=query(L,R,lson);    tb=query(L,R,rson);    int ans;    ans=max(ta,tb);    if(num[m]<num[m+1])  //同上    {        int temp; ///    temp=min(rmlen[rt<<1],m-L+1)+min(lmlen[rt<<1|1],R-m);    temp=min(rmlen[rt<<1],m-L+1)+ min(lmlen[rt<<1|1],R-m);    ///       temp=rmlen[rt<<1]+lmlen[rt<<1|1];    ans=max(temp,ans);    }       return ans;    }}int main(){    int T,n,m;    int i,j;    char f[2];    int a,b;    scanf("%d",&T);    for(i=0;i<T;i++)    {        scanf("%d %d",&n,&m);        for(j=1;j<=n;j++)           scanf("%d",&num[j]);        build(1,n,1);        while(m--)        {            scanf("%s",&f);            if(f[0]=='U')            {                scanf("%d %d",&a,&b);               a++;            ///    num[a]=b;                update(a,b,1,n,1);            }            else            {                scanf("%d %d",&a,&b);                a++,b++;                printf("%d\n",query(a,b,1,n,1));            }        }    }    return 0;}


#include<iostream>#include<cstdio>#define MAX 100005#define lson s,mid,n<<1#define rson mid+1,e,n<<1|1using namespace std;struct TREE{    int lm,rm,mm;}tree[MAX<<2];int num[MAX];void pushup(int s,int e,int n){    int l=n<<1;    int r=n<<1|1;    tree[n].lm=tree[l].lm;    tree[n].rm=tree[r].rm;    tree[n].mm=max(tree[l].mm,tree[r].mm);    int mid=(s+e)>>1;    if(num[mid]<num[mid+1])    {        if(tree[n].lm==mid-s+1)            tree[n].lm+=tree[r].lm;        if(tree[n].rm==e-mid)            tree[n].rm+=tree[l].rm;        tree[n].mm=max(tree[n].mm,tree[l].rm+tree[r].lm);    }}void build(int s,int e,int n){    if(s==e){        scanf("%d",&num[s]);        tree[n].lm=tree[n].rm=tree[n].mm=1;        return ;    }    int mid=(s+e)>>1;    build(lson);    build(rson);    pushup(s,e,n);}void modify(int a,int b,int s,int e,int n){    if(s==e){        num[s]=b;    ///    tree[n].rm=tree[n].lm=tree[n].mm=1;   ///   or        return ;    }    int mid=(s+e)>>1;    if(a<=mid)        modify(a,b,lson);    else modify(a,b,rson);    pushup(s,e,n);}int query(int a,int b,int s,int e,int n){    if(a<=s&&e<=b)        return tree[n].mm;    int mid=(s+e)>>1;    if(b<=mid)        return query(a,b,lson);    else if(mid<a)        return query(a,b,rson);    else    {        int a1=query(a,b,lson);        int b1=query(a,b,rson);        int c=max(a1,b1);        if(num[mid]<num[mid+1])        {          /*  int a1,b1;            if(tree[n].lm==mid-s+1)                 a1=tree[n].lm+tree[n<<1|1].lm;            if(tree[n].rm==e-mid)                b1=tree[n].rm+tree[n<<1].rm;            */            int c1=min(tree[n<<1].rm,mid-a+1)+min(tree[n<<1|1].lm,b-mid);            c=max(c,c1);        }        return c;    }}int main(){    int n,m,T;    char c[5];    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        build(1,n,1);        while(m--){            scanf("%s",c);            int a, b;            scanf("%d%d",&a,&b);            if(c[0]=='U')                modify(a+1,b,1,n,1);            else if(c[0]=='Q')                printf("%d\n",query(a+1,b+1,1,n,1));        }    }    return 0;}


0 0