zoukankan      html  css  js  c++  java
  • [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了。一道傻逼题搞了一晚上一直超时,凭啥子就我不能过???
    然后发现cin没关stdio同步。。。


    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, 然后有两种操作: 将某一段区间全染成某种颜色,或者询问某一个区间共有几种颜色。

    题解

    因为 颜色最多只有30种, 所以可以用一个int来存某个颜色是否出现,向上更新是直接 | (或) 一下就好了。最后枚举一下答案中共出现了几种颜色。

    Ps:cin 卡一晚上我不服啊,辣鸡题,毁我青春233

    代码

    #include <iostream>
    using namespace std;
    #define ls u<<1,l,mid
    #define rs u<<1|1,mid+1,r
    #define pushup(u) {nod[u] = nod[u<<1] | nod[u<<1|1];}
    #define pushdown(u) {add[u<<1] = add[u<<1|1] = add[u];nod[u<<1] = nod[u<<1|1] = (1<<(add[u]-1));add[u] = 0;}
    
    const int maxn = 1e5 + 5;
    int add[maxn << 2];
    
    int nod[maxn << 2];
    
    void build(int u,int l,int r) {
    	nod[u] = 1;
    	add[u] = 0;
    	if(l == r) return;
    	int mid = (l + r) >> 1;
    	build(ls);
    	build(rs);
    } 
    
    void update(int u,int l,int r,int x,int y,int ad) {
    	if(l == x && r == y) {
    		nod[u] = (1<<(ad-1));add[u] = ad;return;
    	}
    	if(add[u]) pushdown(u);
    	int mid = (l + r) >> 1;
    	if(y <= mid) update(ls,x,y,ad);
    	else if(x > mid) update(rs,x,y,ad);
    	else update(ls,x,mid,ad), update(rs,mid+1,y,ad);
    	pushup(u);
    }
    
    int query(int u,int l,int r,int x,int y) {
    	if(l == x && r == y)return nod[u];
    	if(add[u])pushdown(u);
    	int mid = (l + r) >> 1;
    	if(y <= mid) return query(ls,x,y);
    	if(x > mid) return query(rs,x,y);
    	return query(ls,x,mid) | query(rs,mid+1,y);
    }
    
    int main() {
    	ios::sync_with_stdio(false);cin.tie(0);
    	int n,t,o;
    	char op[2];
    	int a,b,c;
    	while(cin >> n >> t >> o) {
    		build(1,1,n);
    		while(o--){
    			cin >> op >> a >> b;
    			if(a > b) swap(a,b);
    			if(op[0] == 'C') {
    				cin >> c;
    				update(1,1,n,a,b,c);
    			}
    			else {
    				int ans = 0;
    				int x = query(1,1,n,a,b);
    				for(int i = 0;i < t;i++)if(x & (1<<i))ans++;
    				cout << ans << endl;
    			}
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    Redis学习之一--基础知识
    工作流学习之--TPFlow数据库分析
    什么是域名?什么网站名?什么是URL
    SASS的升级版--SCSS 基本介绍+Sass使用详解
    vue调试工具vue-devtools的安装(win10系统,最新2020年6月的解决方案)
    如何运行vue项目
    用WebStorm搭建vue项目
    Terminal怎么停止VUE项目
    VUE 在一个组件中引用另外一个组件的两种方式
    Vue.js——60分钟快速入门 开发· webpack 中文文档
  • 原文地址:https://www.cnblogs.com/ZegWe/p/5990864.html
Copyright © 2011-2022 走看看