POJ 2777 线段树

My code can AC this problem just submit by C++;

Left edge may be bigger than Right edge;

The portal:http://poj.org/problem?id=2777

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>

const int MAXN = 100005;
int T;

using namespace std;

struct Node {
    int x;
    int y;
    bool color[35];
    int lazy;
}t[MAXN << 2];

int Get_Color_Num(int rt) {
    int cnt = 0;
    for(int i = 1 ; i <= T ; i++) {
        if(t[rt].color[i]) cnt ++;
    }
    return cnt ;
}

bool color_num[35];

void Push_Up(int rt) {
    memset(t[rt].color,0,sizeof(t[rt].color));
    for(int i = 1 ; i <= T ; i++) {
        if(t[rt<<1].color[i]) t[rt].color[i] = 1;
    }
    for(int i = 1 ; i <= T ; i++) {
        if(t[rt<<1|1].color[i]) t[rt].color[i] = 1;
    }
}

void Push_Down(int rt) {
    if(t[rt].lazy) {
        t[rt<<1].lazy = t[rt].lazy;
        t[rt<<1|1].lazy = t[rt].lazy;
        memset(t[rt<<1].color,0,sizeof(t[rt<<1].color));
        t[rt<<1].color[t[rt<<1].lazy]= 1;
        memset(t[rt<<1|1].color,0,sizeof(t[rt<<1|1].color));
        t[rt<<1|1].color[t[rt<<1|1].lazy]= 1;
        t[rt].lazy = 0;
    }
}

void Build(int x,int y,int rt) {
    t[rt].color[1] = 1; 
    if(x == y) {
        //t[rt].color[init_color] = 1; 
        return ;
    }
    int mid = (x + y) >> 1;
    Build(x,mid,rt << 1); 
    Build(mid+1,y,rt << 1 | 1);
}

void Update(int x,int y,int rt,int x_Edge,int y_Edge,int value) {
    if(x >= x_Edge && y <= y_Edge) {
        memset(t[rt].color,0,sizeof(t[rt].color));
        t[rt].color[value] = 1;
        t[rt].lazy = value;
        return;
    }
    Push_Down(rt);
    int mid = (x + y) >> 1;
    if(mid >= x_Edge) {
        Update(x,mid,rt<<1,x_Edge,y_Edge,value);
    }
    if(mid <  y_Edge) {
        Update(mid+1,y,rt<<1|1,x_Edge,y_Edge,value);
    }
    Push_Up(rt);
}

void Query(int x,int y,int rt,int x_Edge,int y_Edge) {
    if(x >= x_Edge && y <= y_Edge) {
        for(int i = 1 ; i <= T ; i ++) {
            if(t[rt].color[i])color_num[i] = 1;
        }
        return ;
    }
    Push_Down(rt);
    int mid = (x + y) >> 1;
    if(mid >= x_Edge) {
        Query(x,mid,rt<<1,x_Edge,y_Edge);
    }
    if(mid <  y_Edge) {
        Query(mid+1,y,rt<<1|1,x_Edge,y_Edge);
    }
}

void Debug(int x,int y,int rt) {
    printf("Node : %d x : %d y : %d Lazy : %d --> ",rt,x,y,t[rt].lazy);
    for(int i = 1 ; i <= T ; i++) {
        if(t[rt].color[i]) printf("%d ",i);
    }
    puts("");
    if(x == y) return;
    int mid = (x + y) >> 1;
    Debug(x,mid,rt<<1);
    Debug(mid+1,y,rt<<1|1);
}

void Deal_with() { 
    int L,O;
    while(~scanf("%d %d %d",&L,&T,&O)) {
        memset(t,0,sizeof(t));
        Build(1,L,1);
        //printf("%d\n",Query(1,L,1,1,L));
        char tempc ;
        int x,y,color;
        for(int i = 0 ; i < O ; i++) {
            getchar();
            scanf("%c %d %d",&tempc,&x,&y);
            if(x > y) swap(x,y);
            if(tempc == 'C'){
                scanf("%d",&color);
                Update(1,L,1,x,y,color);
            }
            else {
                memset(color_num,0,sizeof(color_num));
                Query(1,L,1,x,y);   
                int cnt = 0;
                for(int i = 1 ; i <= T ; i ++) {
                    if(color_num[i]) {
                        cnt ++;
                        //printf("%d\n",i);
                    }
                }
                printf("%d\n",cnt);
            }
            //Debug(1,L,1);
        }
    }
}

int main(void) {
   // freopen("a.in","r",stdin);
    Deal_with();
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值