敲了n遍....RE愉快的debug了一晚上...发现把#define maxn = 100000 + 10 改成 #define maxn = 100010 就过了....感受一下我呵呵哒的表情....

貌似这个题用了很经典的线段树和位运算。懂了。但不是很懂。确实觉得用的很巧妙。只想说。好坑。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N = 100010 struct Tree{
int l,r;
int col; // 用一个32位的int型,每一位对应一种颜色,用位运算代替bool col[32]
bool cover; // 表示这个区间都被涂上同一种颜色,线段树效率的体现,否则插入就是0(n)了。
}tree[N<<]; void PushUp(int rt){ // 最后递归回来再更改父节点的颜色
tree[rt].col=tree[L(rt)].col | tree[R(rt)].col;
} void build(int l, int r, int root) {
tree[root].l = l;
tree[root].r = r;
tree[root].col = ;
tree[root].cover = ; if (l == r) return;
int mid = (l+r)/;
build(l, mid, *root);
build(mid+, r, *root+);
} void PushDown(int root) { // 把标记向下传递
tree[*root].col = tree[root].col;
tree[*root].cover = ;
tree[*root+].col = tree[root].col;
tree[*root+].cover = ;
tree[root].cover = ; // 懂了。
}
void update(int val, int l, int r, int root) { // 更新某个区间的颜色
if (l <= tree[root].l && tree[root].r <= r) {
tree[root].col = val; // 说明这个区间只包含一种颜色了。而且是val. 所以修改完成、get it!!!
tree[root].cover = ;
return ;
} if (tree[root].col == val) return; // 说明当前的区间内的颜色已经只有这一种了。所以没必要再往下、??剪枝
if (tree[root].cover) PushDown(root); // 因为下面可能要修改这个区间的某个子区间的颜色了。
// 否则就不断的向下分解区间
int mid = (tree[root].l+tree[root].r)/;
if (r <= mid) update(val, l, r, *root);
else if (l >= mid+) update(val, l, r, *root+);
else {
update(val, l, mid, *root);
update(val, mid+, r, *root+);
}
PushUp(root); // 递归修改完孩子节点的颜色后 修改父亲节点的颜色
} int sum; void query(int L,int R,int rt){
if(L<=tree[rt].l && R>=tree[rt].r){
sum |= tree[rt].col;
return ;
}
if(tree[rt].cover){ // 这个区间全部为1种颜色,就没有继续分割区间的必要了
sum |= tree[rt].col; // 颜色种类相加的位运算代码
return;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if(R<=mid)
query(L,R,L(rt));
else if(L>=mid+)
query(L,R,R(rt));
else{
query(L,mid,L(rt));
query(mid+,R,R(rt));
}
} int solve(){
int ans=;
while(sum){
if(sum&)
ans++;
sum>>=;
}
return ans;
} void swap(int &a, int &b) {
int t = a;
a = b;
b = t;
} int main() {
int l, n, m;
while(~scanf("%d%d%d", &l, &n, &m)) {
build(, l, );
char op[];
int a, b, c; for (int i=; i<m; ++i) {
scanf("%s", op);
if (op[] == 'C') {
scanf("%d%d%d", &a, &b, &c);
if (a > b) swap(a, b);
update(<<(c-), a, b, );
}
else if (op[] == 'P') {
scanf("%d%d", &a, &b);
if (a > b) swap(a, b);
sum = ;
query(a, b, );
printf("%d\n",solve());
}
}
}
return ;
}

poj 2777线段树应用的更多相关文章

  1. poj 2777(线段树+lazy思想) 小小粉刷匠

    http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...

  2. POJ 2777——线段树Lazy的重要性

    POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...

  3. POJ 2777(线段树)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42507   Accepted: 12856 Des ...

  4. POJ 2777 线段树基础题

    题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...

  5. Count Color POJ - 2777 线段树

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...

  6. poj 2777 线段树的区间更新

    Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...

  7. poj 2777 线段树 区间更新+位运算

    题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色.Sample Input2 2 4  板长 颜色数目 询问数目C 1 1 2P ...

  8. poj 2886 线段树+反素数

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12744   Acc ...

  9. poj 3468(线段树)

    http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...

随机推荐

  1. 使用volley来json解析

    我对网络请求get和post的理解: 1.get只是从某网址获得固定数据,如我访问百度,返回就是百度的html语句: 2.post是我在访问的时候加了某些参数,如我访问某个服务器,访问的时候加了一些语 ...

  2. USB Compound Device,USB复合设备 ; USB Composite Device,USB组合设备【转】

    本文转载自:https://blog.csdn.net/autumn20080101/article/details/52776863 科普下USB复合设备和USB组合设备的区别. 关键字 Commu ...

  3. 支持github的chrome插件octotree

    1.这款插件有何作用? 方便浏览github上的源码 2.安装方法 通过以下链接进行安装: https://chrome.google.com/webstore

  4. 51NOD 1046 A^B Mod C

    给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^9) ...

  5. gulp介绍及常用插件

    前端构建工具gulpjs的使用介绍及技巧 gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非常简单,学习起来很容易,而且gulpjs使用的是nod ...

  6. OpFlex

    参考: OpFlex Main OpFlex: Building and Running OpFlex Building mkdir -p ~/work pushd work git clone ht ...

  7. LA 4287 等价性证明(强连通分量缩点)

    https://vjudge.net/problem/UVALive-4287 题意: 给出n个结点m条边的有向图,要求加尽量少的边,使得新图强连通. 思路:强连通分量缩点,然后统计缩点后的图的每个结 ...

  8. UVa 10881 蚂蚁

    https://vjudge.net/problem/UVA-10881 题意: 一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1厘米/秒.当两只蚂蚁相撞时,二者同时掉头. ...

  9. Linux——系统引导流程学习简单笔记

    开启电源: 固件 firmware(CMOS/BIOS) → POST 加电自检 对硬件就行检查 ↓ 自举程序 BootLoader(GRUB) → 载入内核 ↓ 载入内核 Kernel 1:驱动硬件 ...

  10. Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队

    题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...