LouZhang

导航

hdu_4391,线段树

http://acm.hdu.edu.cn/showproblem.php?pid=4391

不想我说了,调了好久

不会这。。

#include<cstdio>
#include<cstring>

const int maxn(111111);
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define ll rt<<1
#define rr rt<<1|1
int max[maxn<<2], min[maxn<<2], sum[maxn<<2], col[maxn<<2];

void push_up(int rt){
    if(col[ll]==-1&&col[rr]==-1&&sum[ll]==sum[rr]){
        col[rt]=-1;
        sum[rt]=sum[ll];
    }
    else{
        col[rt]=0;
        sum[rt]=-1;
    }
    max[rt] = max[ll] > max[rr] ? max[ll] : max[rr];
    min[rt] = min[ll] < min[rr] ? min[ll] : min[rr];
}
void PushDown(int rt){
    if(col[rt]==-1){
        col[ll]=col[rr]=col[rt];
        sum[ll]=sum[rr]=sum[rt];
        min[ll]=max[ll]=sum[rt];
        min[rr]=max[rr]=sum[rt];
        col[rt]=0;
    }
}
void build(int l, int r, int rt){
    if(l == r){
        scanf("%d", sum + rt);
        col[rt]=-1;
        max[rt] = min[rt] = sum[rt];
        return;
    }
    int m = (l + r )>> 1;
    build(lson);
    build(rson);
    push_up(rt);
}
void update(int z, int L, int R, int l, int r, int rt){
    //printf("-- %d %d %d %d...............\n", L, R, l, r);
    if(L <= l && r <= R){
        col[rt] = -1;
        sum[rt] = z;
        max[rt] = min[rt] = z;
        return;
    }
    PushDown(rt);
    int m =( l + r) >> 1;
    if(L <= m) update(z, L, R, lson);
    if(m < R) update(z, L, R, rson);
    push_up(rt);
}
int query(int z, int L, int R, int l, int r, int rt){
    //if(z > max[rt] || z < min[rt]) return 0;
    //printf("-- %d %d %d %d\n", L, R, l, r);
    if(L <= l && r <= R){
        if(col[rt]==-1&&sum[rt]==z) return (r-l+1);
        else if(z<min[rt]||z>max[rt]) return 0;
    }
    PushDown(rt);
    int m=(l+r)>>1;
    int ret = 0;
    if(L <= m) ret += query(z, L, R, lson);
    if(m < R) ret += query(z, L, R, rson);
    return ret;
}
int main(){
    int n,m;
    while(~scanf("%d%d", &n, &m)){
        build(1, n, 1);
        int a, l, r, z;
        while(m--){
            scanf("%d%d%d%d", &a, &l, &r, &z);
            if(a == 1) update(z, l+1, r+1, 1, n, 1);
            else
            printf("%d\n", query(z, l+1, r+1, 1, n, 1));
        }
    }
    return 0;
}

posted on 2012-08-24 15:46  louzhang_swk  阅读(372)  评论(0编辑  收藏  举报