PKU 2777 Count Color
来源:互联网 发布:matlab 矩阵色块图 编辑:程序博客网 时间:2024/06/15 09:06
题目大意:
在一条直线上插入不同颜色的线段,看有多少段颜色相同的线段。
(note:加入有大神嫌题目不够详细的话 传送门:http://poj.org/problem?id=2777)
输入输出格式:
给一个l(直线的长度。。直线有长度吗。。不管了。),t(颜色的种类,尽管好像并没有什么用),o(询问的次数)
“C A B C”表示在区间A,B图上C颜色, “P A B” 表示询问
A,B区间有几种不同的颜色。
分析:
用线段树,插入线段的时候,如果访问的线段已标记,则拆分为三段来插入。(这一份没有用lazy标记)
代码:
#include <iostream>#include <cstdio>#include <queue>#include <string>#include <cmath>#include <cstring>#define N 100000using namespace std;int flag[N];int l,t,o,a,b,c,ans;char k;struct tree{ int l,r,cover;}tree[N];void build(int p,int l,int r){ tree[p].l=l; tree[p].r=r; int m=(l+r)/2; if (r-l>1) { build(p*2,l,m); build(p*2+1,l,r); }}void insert(int p,int a,int b,int c){ int m=(tree[p].l+tree[p].r)/2; printf("%d" ,l); if (tree[p].cover!=c) if (tree[p].l==a && tree[p].r==b) tree[p].cover=c; else { if (tree[p].cover>=0) { tree[p*2].cover=tree[p].cover; tree[p*2+1].cover=tree[p].cover; tree[p].cover=-1; } if (b<=m) insert(p*2,a,b,c); else if (a>=m) insert(p*2+1,a,b,c); else { insert(p*2,a,b,c); insert(p*2+1,a,b,c); } }}int count(int p,int a, int b){ if (tree[p].cover>=0) flag[tree[p].cover]=1; if (b-a>1) { count(p*2,a,(a+b)/2); count(p*2+1,(b+a)/2,b); }}int main(){ scanf("%d%d%d" ,&l,&t,&o); build(1,1,l); for (int i=1;i<=o;i++) { scanf("%c" ,&k); if (k=='C') { scanf("%d%d%d\n" ,&a,&b,&c); insert(1,a,b,c); } else { scanf("%d%d\n" ,&a,&b); memset(flag,0,sizeof(flag)); ans=0; count(1,a,b); for (int j=1;j<=l;j++) if (flag[j]==1) ans++; printf("%d\n" ,ans); printf("%c" ,k); } }}
0 0
- PKU 2777 Count Color
- PKU 2777 Count Color
- PKU 2777 Count Color
- pku 2777 Count Color(线段树变形)
- [PKU] 2777 Count Color -- 线段树
- PKU 2777 Count Color (线段树区间更新)
- PKU 2777 - Count Color(线段树 + 区间修改)
- POJ 2777 Count Color
- poj 2777 Count Color
- poj 2777 count color
- poj 2777 Count color
- poj 2777 Count Color
- Poj 2777 Count Color
- POJ 2777 Count Color
- poj 2777 count color
- POJ 2777 Count Color
- poj 2777 Count Color
- poj 2777 count color
- PCF8591 1602液晶显示
- Ubuntu15安装IDEA开发工具
- 动态生成行,序号随之增加,删除后依然按顺序。所以给动态生成的html元素绑定click事件
- 1.1输出一行文字
- JVM系列二:GC策略&内存申请、对象衰老
- PKU 2777 Count Color
- 死锁(面试常考)
- Java 面试——基础程序题001
- 506. Relative Ranks
- 【网络】TCP三次握手四次挥手
- 华为机试-字符串排序
- C++第一次实验项目3、5(二)
- 死锁面试真题
- PBC library 学习笔记(二)