首页 > 其他 > 详细

POJ 2777 Count Color

时间:2015-06-29 14:31:48      阅读:224      评论:0      收藏:0      [点我收藏+]

题意:和贴海报这个题类似……只是多了一个查询l到r区间的操作

 

解法:相对贴海报来说不需要离散化,查询操作需要稍作改动。

 

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int MAXN = 100005;
int col[MAXN << 2];
bool flag[35];
int ans;
using namespace std;

void pushdown(int rt)
{
    if(col[rt] != -1)
    {
        col[rt << 1] = col[rt << 1 | 1] = col[rt];
        col[rt] = -1;
    }
}
void update(int ll, int rr, int c, int l, int r, int rt)
{
    if(ll <= l && rr >= r)
    {
        col[rt] = c;
        return ;
    }
    pushdown(rt);
    int m = (l + r) >> 1;
    if(ll <= m) update(ll, rr, c, lson);
    if(rr > m) update(ll, rr, c, rson);
}
void query(int ll, int rr, int l, int r, int rt)
{
    if(col[rt] != -1)
    {
        flag[col[rt]] = true;
        return ;
    }
    int m = (l + r) >> 1;
    if(ll <= m) query(ll, rr, lson);
    if(rr > m) query(ll, rr, rson);
}
int main()
{
    int n, t, o;
    while(~scanf("%d%d%d", &n, &t, &o))
    {
        memset(col, -1, sizeof(col));
        update(1, n, 1, 1, n, 1);
        while(o--)
        {
            char ch[2];
            int l, r;
            scanf("%s%d%d", ch, &l, &r);
            if(ch[0] == ‘C‘)
            {
                int c;
                scanf("%d", &c);
                update(l, r, c, 1, n, 1);
            }
            else
            {
                memset(flag, 0, sizeof flag);
                query(l, r, 1, n, 1);
                int ans = 0;
                for(int i = 1; i <= t; i++)
                    ans += flag[i];
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}

  

POJ 2777 Count Color

原文:http://www.cnblogs.com/Apro/p/4607326.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!