链接:https://vjudge.net/problem/POJ-2777#author=0

题意:

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.

思路:

线段树,还是普通的线段树,染色的查询和更新使用位运算,因为颜色区间在(1-30)之内。

所以可以使用(1<<1-1<<30)来表示这中二进制1的个数来表示颜色的数量。

不过我之前的写的普通的线段树我也不知道为啥会WA。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <string>
#include <stack>
#include <vector>
#include <queue>using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;int Seg[MAXN*4];
int lazy[MAXN*4];
int Vis[100];
int n, t, o;
int res;void PushDown(int root)
{if (lazy[root] != 0){Seg[root<<1] = (1<<lazy[root]);Seg[root<<1|1] = (1<<lazy[root]);lazy[root<<1] = lazy[root];lazy[root<<1|1] = lazy[root];lazy[root] = 0;}
}void PushUp(int root)
{Seg[root] = Seg[root<<1]|Seg[root<<1|1];
}void Build(int root, int l, int r)
{if (l == r){Seg[root] = 2;return;}int mid = (l + r) / 2;Build(root << 1, l, mid);Build(root << 1 | 1, mid + 1, r);PushUp(root);
}void Update(int root, int l, int r, int ql, int qr, int c)
{if (r < ql || qr < l)return;if (ql <= l && r <= qr){Seg[root] = (1<<c);lazy[root] = c;return;}PushDown(root);int mid = (l+r)/2;Update(root<<1, l, mid, ql, qr, c);Update(root<<1|1, mid+1, r, ql, qr, c);PushUp(root);
}int Query(int root, int l, int r, int ql, int qr)
{if (r < ql || qr < l)return 0;if (ql <= l && r <= qr){return Seg[root];}int mid = (l+r)/2;PushDown(root);int col1 = 0, col2 = 0;col1 = Query(root<<1, l, mid, ql, qr);col2 = Query(root<<1|1, mid+1, r, ql, qr);return col1|col2;
}int Get(int x)
{int res = 0;while (x){if (x&1)res++;x >>= 1;}return res;
}int main()
{char op[10];int a, b, c;while (~scanf("%d%d%d", &n, &t, &o)){Build(1, 1, n);while (o--){scanf("%s", op);if (op[0] == 'C'){scanf("%d%d%d", &a, &b, &c);if (a > b)swap(a, b);Update(1, 1, n, a, b, c);}else{scanf("%d%d", &a, &b);if (a > b)swap(a, b);memset(Vis, 0, sizeof(Vis));int res = Query(1, 1, n, a, b);printf("%d\n", Get(res));}}}return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10847767.html

POJ-2777-CountColor(线段树,位运算)相关推荐

  1. 皮卡丘的梦想2(线段树+位运算)

    皮卡丘的梦想2 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 一天,一只住在 501 实验 ...

  2. 皮卡丘的梦想(线段树+位运算)

    皮卡丘的梦想2 1000 ms         65536 KiB Submit Status My Status  Origin Description 一天,一只住在 501 实验室的皮卡丘决定发 ...

  3. poj 2777(线段树的节点更新策略)

    1 /* 2 之前的思想是用回溯的方式进行颜色的更新的!如果用回溯的方法的话,就是将每一个节点的颜色都要更新 3 通过子节点的颜色情况来判断父节点的颜色情况 !这就是TLE的原因! 4 5 后来想一想 ...

  4. 线段树位运算的三种操作(|,^,)

    银川站网络预选赛重赛(手动滑稽)的时候,第一道题就是这样的一个题,虽然最后没有判出来.不一定对,可以借鉴一下吧. 代码如下: #include<iostream> #include< ...

  5. POJ 2352 Stars (线段树)

    POJ 2352 Stars (线段树) 手动博客搬家:本文发表于20170819 22:11:49, 原地址https://blog.csdn.net/suncongbo/article/detai ...

  6. hdu 5023 poj 2777(线段染色)2014 ACM/ICPC Asia Regional 广州 Online

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5023 http://poj.org/problem?id=2777 题意:给出一个长度为N的线段,分 ...

  7. A Simple Problem with Integers POJ - 3468(线段树+区间查询+区间修改+建树+懒惰标记模板)+(树状数组)

    题意: 有一个数组,有两种操作.1: Q a b 求[a,b]的和 2:C a b c 给[a,b] 的所有元素都加上c. 题目: You have N integers, A1, A2, ... , ...

  8. poj 2352 Stars 线段树(先建后查/边建边查)/树状数组三种方法思路详解,带你深入了解线段树难度⭐⭐⭐★

    poj 2352 Stars 目录 poj 2352 Stars 1.树状数组 2.线段树,先建树后查找 3.线段树,边建树边查找 Description Astronomers often exam ...

  9. poj 2352 Stars(线段树)

    题目:http://poj.org/problem?id=2352 大意:一些星星有自己的优先级,优先级是x坐标和y坐标小于等于该星星坐标的星星个数 思路:由于这个题的y值是从小到大排列,所以对x建立 ...

  10. POJ - 3279 Fliptile(状态压缩+位运算+暴力)

    题目链接:点击查看 题目大意:给出一个n*m的01矩阵,为了好描述,我们设0和1是两个相反的状态,我们的目标是要将整个矩阵全部变成1,现在我们可以将某一个点(x,y)更改为相反的状态,不过相应的该点周 ...

最新文章

  1. latex不能识别eps图片
  2. 艾伟_转载:ASP.NET模板引擎技术
  3. java提高篇(七)-----关键字static
  4. 8个高效的Python爬虫框架分享
  5. 卡斯特罗的离去对古巴科技产业的未来有何影响?
  6. html flash 循环播放,在网页中插入flv格式的flash视频怎么让其循环播放_html/css_WEB-ITnose...
  7. express 随笔
  8. 产品经理和项目经理有哪些区别?
  9. C语言,利用循环语句找出1000以内的水仙花数
  10. Windows下Squid 3.5安装及配置代理服务器
  11. Java中的网络支持Socket应用
  12. 自己的部分小软件合计 2000 - 2013
  13. 现代操作系统(原书第四版)课后题答案 —— 第二章 进程与线程
  14. 服务器mstsc远程桌面,远程桌面工具,详细教您如何使用远程桌面工具mstsc连接远程桌面...
  15. 微软开始彻底封杀IE浏览器
  16. 【机器学习】Python中随机森林的实现与解释
  17. 函函函函函函函函函函函数——two
  18. IE加载ocx时提示控件不安全的解决方法
  19. Seventh season twenty-first episode,Monica and Chandler both did not know how to write their vows???
  20. RK3399 Android7.1如何查看屏幕分辨率

热门文章

  1. [python 练习] 计算个税
  2. Parcel是个好玩意儿
  3. [转] android获取手机信息大全
  4. ORM框架-工具-产品开发之四 开发代码生成器 Template Studio Development (一)
  5. 【转】关于LoadRunner的迭代
  6. C#中实现鼠标拖动窗体的方法
  7. 微信小程序 小程序源码包括后台完整版分享
  8. restful API 常用的四种方式
  9. Cookie 和 Session的区别 1
  10. js中页面与页面传参遇到Uncaught SyntaxError: Unexpected token =报错