POJ 2777 Count Color

本文介绍使用线段树解决CountColor问题的方法,通过二进制存储颜色信息,实现区间染色及查询不同颜色数量的功能。代码示例展示了如何进行更新和查询操作。

这两天在练习各种线段树,于是就继续更一道线段树的题目QAQ

题面如下:

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 49616 Accepted: 14946

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:

1. "C A B C" Color the board from segment A to segment B with color C.
2. "P A B" Output the number of different colors painted between segment A and segment B (including).

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2
1

题意:和之前有个贴海报的题差不多,意思就是给你一个区间和一些颜色,区间初始全为颜色1,C操作代表将某一区间涂成某一种颜色,P操作是询问某一区间的颜色个数。

分析:一道比较裸的线段树,在这儿可以稍微用一下lazy标记。有个思想,如何在线段树中体现两个区间不同颜色的个数?我们这儿采用了二进制存储信息的思想,因为它总共只有30发颜色,所以我们用一个int位的二进制表示就足够了。两个区间的不同颜色个数就是把他们或起来就可以了。

这儿注意比较坑的一点是,查询的区间有可能第一个数是大于第二个数的QAQ

代码如下:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;

const int maxn=1e5+7;
int l,t,o;
int tree[maxn<<2],Hash[maxn],lazy[maxn<<2];

void pushup(int rt){
    tree[rt]=tree[rt<<1]|tree[rt<<1|1];
}

void pushdown(int rt){
    if(lazy[rt]){
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        tree[rt<<1]=tree[rt<<1|1]=lazy[rt];
        lazy[rt]=0;
    }
}

void build(int l,int r,int rt){
    tree[rt]=lazy[rt]=0;
    if(l!=r){
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        pushup(rt);
    }
    else tree[rt]=1;
}

void update(int L,int R,int c,int l,int r,int rt){
    if(L>r||R<l) return ;
    if(L<=l&&r<=R){
        tree[rt]=lazy[rt]=1<<(c-1);
        return ;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    update(L,R,c,lson);
    update(L,R,c,rson);
    pushup(rt);
}

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

int ans(int c){
    int aa=0;
    while(c){
        if(c&1)
            aa++;
        c>>=1;
    }
    return aa;
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>l>>t>>o;
    build(1,l,1);

    char s;

    while(o--){
        cin>>s;
        int x,y,z;
        if(s=='C') {
            cin>>x>>y>>z;
            if(x>y) swap(x,y);
            update(x,y,z,1,l,1);
        }
        else{
            cin>>x>>y;
            if(x>y) swap(x,y);
            cout<<ans(query(x,y,1,l,1))<<endl;
        }
    }
    return 0;
}

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值