27号的十道离线线段树

27号的十道离线线段树

hdu4288: (2012成都网络赛&&cf)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const int maxn=1000100;
const int INF=(1<<29);

typedef long long ll;
int N;
char op[maxn][10];
int Q[maxn];
int X[maxn],Xn;
struct Node
{
    int cnt;
    ll sum[5];
};Node T[maxn<<2];

void push_up(int rt)
{
    T[rt].cnt=T[rt<<1].cnt+T[rt<<1|1].cnt;
    for(int i=0;i<5;i++){
        T[rt].sum[i]=T[rt<<1].sum[i]+T[rt<<1|1].sum[(i-T[rt<<1].cnt+50000000)%5];
    }
}

void build(int l,int r,int rt)
{
    if(l>r) return;
    if(l==r){
        T[rt].cnt=0;
        for(int i=0;i<5;i++) T[rt].sum[i]=0;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}

void update(int pos,int val,int op,int l,int r,int rt)
{
    if(l==r){
        if(op==1){
            if(T[rt].cnt==1) return;
        }
        if(op==-1){
            if(T[rt].cnt==0) return;
        }
        T[rt].cnt+=op;
        T[rt].sum[1]+=val;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m) update(pos,val,op,lson);
    else update(pos,val,op,rson);
    push_up(rt);
}

void get(int l,int r,int rt)
{
    cout<<"rt="<<rt<<" l="<<l<<" r="<<r<<" cnt="<<T[rt].cnt<<endl;
    if(l==r) return;
    int m=(l+r)>>1;
    get(lson);
    get(rson);
    return;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(cin>>N){
        Xn=0;
        for(int i=1;i<=N;i++){
            scanf("%s",op[i]);
            if(op[i][0]!='s'){
                scanf("%d",&Q[i]);
                X[++Xn]=Q[i];
            }
        }
        sort(X+1,X+Xn+1);
        Xn=unique(X+1,X+Xn+1)-(X+1);
        build(1,Xn,1);
        for(int i=1;i<=N;i++){
            int pos=lower_bound(X+1,X+Xn+1,Q[i])-X;
            if(op[i][0]=='a') update(pos,Q[i],1,1,Xn,1);
            else if(op[i][0]=='d') update(pos,-Q[i],-1,1,Xn,1);
            else printf("%I64d\n",T[1].sum[3]);
        }
    }
    return 0;
}
View Code

 hdu5200(bestcoder#36 C)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const int maxn=50100;
const int INF=(1<<29);
typedef long long ll;

ll N,Q;
int H;
struct Tree
{
    ll h;
    ll id;
    friend bool operator<(Tree A,Tree B)
    {
        return A.h<B.h;
    }
};Tree tree[maxn];int tn;
struct Query
{
    int ask;
    int ans;
    ll id;
    friend bool operator<(Query A,Query B)
    {
        return A.ask<B.ask;
    }
};Query q[maxn];
struct Node
{
    ll lsum,rsum,sum;
};Node T[maxn<<2];

bool cmp(Query A,Query B)
{
    return A.id<B.id;
}

void push_up(int rt,int m)
{
    T[rt].sum=T[rt<<1].sum+T[rt<<1|1].sum;
    T[rt].lsum=T[rt<<1].lsum;
    T[rt].rsum=T[rt<<1|1].rsum;
    if(T[rt].lsum==(m-(m>>1))) T[rt].lsum+=T[rt<<1|1].lsum;
    if(T[rt].rsum==(m>>1)) T[rt].rsum+=T[rt<<1].rsum;
    if(T[rt<<1].rsum&&T[rt<<1|1].lsum) T[rt].sum--;
}

void build(int l,int r,int rt)
{
    if(l==r){
        T[rt].sum=T[rt].lsum=T[rt].rsum=1;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt,r-l+1);
}

void update(int pos,int l,int r,int rt)
{
    if(l==r){
        T[rt].lsum=T[rt].rsum=T[rt].sum=0;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m) update(pos,lson);
    else update(pos,rson);
    push_up(rt,r-l+1);
}

int main()
{
    freopen("in.txt","r",stdin);
    while(cin>>N>>Q){
        tn=0;
        for(int i=1;i<=N;i++){
            scanf("%d",&H);
            tree[++tn].h=H;
            tree[tn].id=i;
        }
        sort(tree+1,tree+tn+1);
        for(int i=1;i<=Q;i++){
            scanf("%d",&q[i].ask);
            q[i].id=i;
        }
        sort(q+1,q+Q+1);
        build(1,N,1);
        int tot=1;
        for(int i=1;i<=Q;i++){
            while(tree[tot].h<=q[i].ask&&tot<=N){
                update(tree[tot].id,1,N,1);
                tot++;
            }
            q[i].ans=T[1].sum;
        }
        sort(q+1,q+Q+1,cmp);
        for(int i=1;i<=Q;i++){
            printf("%d\n",q[i].ans);
        }
    }
    return 0;
}
View Code

 xdoj1114(Orz_Panda A题)

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const int maxn=1000100;

int N,Q;
struct Panda
{
    int val,id;
    friend bool operator<(Panda A,Panda B)
    {
        return A.val<B.val;
    }
};Panda A[maxn];
struct Query
{
    int l,r,k1,k2,id;
    int cnt1,cnt2;
    friend bool operator<(Query A,Query B)
    {
        return A.id<B.id;
    }
};Query q[maxn];
struct Node
{
    int cnt;
};Node T[maxn<<2];

bool cmp1(Query A,Query B)
{
    return A.k1<B.k1;
}

bool cmp2(Query A,Query B)
{
    return A.k2>B.k2;
}

void push_up(int rt)
{
    T[rt].cnt=T[rt<<1].cnt+T[rt<<1|1].cnt;
}

void build(int l,int r,int rt)
{
    if(l==r){
        T[rt].cnt=0;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}

void update(int pos,int l,int r,int rt)
{
    if(l==r){
        T[rt].cnt=1;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m) update(pos,lson);
    else update(pos,rson);
    push_up(rt);
}

int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R) return T[rt].cnt;
    int m=(l+r)>>1;
    int res=0;
    if(L<=m) res+=query(L,R,lson);
    if(R>m) res+=query(L,R,rson);
    return res;
}

int main()
{
    freopen("in.txt","r",stdin);
    while(cin>>N>>Q){
        for(int i=1;i<=N;i++){
            scanf("%d",&A[i].val);
            A[i].id=i;
        }
        sort(A+1,A+N+1);
        build(1,N,1);
        for(int i=1;i<=Q;i++){
            scanf("%d%d%d%d",&q[i].l,&q[i].r,&q[i].k1,&q[i].k2);
            q[i].id=i;
        }
        sort(q+1,q+Q+1,cmp1);
        int tot=1;
        for(int i=1;i<=Q;i++){
            while(A[tot].val<q[i].k1&&tot<=N) update(A[tot++].id,1,N,1);
            q[i].cnt1=query(q[i].l,q[i].r,1,N,1);
        }
        sort(q+1,q+Q+1,cmp2);
        build(1,N,1);
        tot=N;
        for(int i=1;i<=Q;i++){
            while(A[tot].val>q[i].k2&&tot>=1) update(A[tot--].id,1,N,1);
            q[i].cnt2=query(q[i].l,q[i].r,1,N,1);
        }
        sort(q+1,q+Q+1);
        for(int i=1;i<=Q;i++){
            printf("%d\n",q[i].r-q[i].l+1-q[i].cnt1-q[i].cnt2);
        }
    }
    return 0;
}
View Code

 hdu4417(2012杭州网络赛 水题)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

typedef long long ll;
const int maxn=1000100;

int N,Q;
struct Brick
{
    ll h;
    int id;
    friend bool operator<(Brick A,Brick B)
    {
        return A.h<B.h;
    }
};Brick A[maxn];
struct Query
{
    int l,r;
    ll h;
    int id,ans;
    friend bool operator<(Query A,Query B)
    {
        return A.h<B.h;
    }
};Query q[maxn];
int cnt[maxn<<2];

bool cmp(Query A,Query B)
{
    return A.id<B.id;
}

void push_up(int rt)
{
    cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1];
}

void build(int l,int r,int rt)
{
    if(l==r){
        cnt[rt]=0;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}

void update(int pos,int l,int r,int rt)
{
    if(l==r){
        cnt[rt]=1;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m) update(pos,lson);
    else update(pos,rson);
    push_up(rt);
}

int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R) return cnt[rt];
    int m=(l+r)>>1;
    int res=0;
    if(L<=m) res+=query(L,R,lson);
    if(R>m) res+=query(L,R,rson);
    return res;
}

int main()
{
    freopen("in.txt","r",stdin);
    int T;cin>>T;
    int casen=1;
    while(T--){
        scanf("%d%d",&N,&Q);
        for(int i=1;i<=N;i++){
            scanf("%I64d",&A[i].h);
            A[i].id=i;
        }
        sort(A+1,A+N+1);
        build(1,N,1);
        for(int i=1;i<=Q;i++){
            scanf("%d%d%I64d",&q[i].l,&q[i].r,&q[i].h);
            q[i].l++;q[i].r++;
            q[i].id=i;
        }
        sort(q+1,q+Q+1);
        int tot=1;
        for(int i=1;i<=Q;i++){
            while(tot<=N&&A[tot].h<=q[i].h) update(A[tot++].id,1,N,1);
            q[i].ans=query(q[i].l,q[i].r,1,N,1);
        }
        sort(q+1,q+Q+1,cmp);
        printf("Case %d:\n",casen++);
        for(int i=1;i<=Q;i++){
            printf("%d\n",q[i].ans);
        }
    }
    return 0;
}
View Code

 codeforces 482B 涉及位运算&和|

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

const int maxn=1000100;

typedef long long ll;
int N,Q;
ll val[maxn<<2];
struct Query
{
    int l,r;
    ll ask;
};Query q[maxn];

void push_up(int rt)
{
    val[rt]=val[rt<<1]&val[rt<<1|1];
}

void push_down(int rt)
{
    val[rt<<1]|=val[rt];
    val[rt<<1|1]|=val[rt];
}

void update(int L,int R,ll c,int l,int r,int rt)
{
    if(L<=l&&r<=R){
        val[rt]|=c;
        return;
    }
    push_down(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    push_up(rt);
}

ll query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R) return val[rt];
    push_down(rt);
    int m=(l+r)>>1;
    ll res=(1LL<<60)-1;
    if(L<=m) res&=query(L,R,lson);
    if(R>m) res&=query(L,R,rson);
    push_up(rt);
    return res;
}

void get(int l,int r,int rt)
{
    if(l==r){
        printf("%I64d ",val[rt]);
        return;
    }
    push_down(rt);
    int m=(l+r)>>1;
    get(lson);
    get(rson);
    push_up(rt);
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(cin>>N>>Q){
        memset(val,0,sizeof(val));
        for(int i=1;i<=Q;i++){
            scanf("%d%d%I64d",&q[i].l,&q[i].r,&q[i].ask);
            update(q[i].l,q[i].r,q[i].ask,1,N,1);
        }
        bool flag=1;
        for(int i=1;i<=Q;i++){
            if(query(q[i].l,q[i].r,1,N,1)!=q[i].ask){
                flag=0;break;
            }
        }
        if(flag){
            puts("YES");
            get(1,N,1);
            puts("");
        }
        else puts("NO");
    }
    return 0;
}
View Code

 

posted @ 2015-08-27 00:00  __560  阅读(233)  评论(0编辑  收藏  举报