题目描写叙述:

长度为L个单位的画板,有T种不同的颜料。现要求按序做O个操作,操作分两种:

1.“C A B C”,即将A到B之间的区域涂上颜色C

2.“P A B”。查询[A,B]区域内出现的颜色种类



出现操作2时。请输出答案

PS:初始状态下画板颜色为1

一開始没有想那么好,用int整型位移来取代颜色。还是使用了最传统的bool color[来记录。但是不知道错在了哪里。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<list>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<string>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<vector>
  11. #include<cmath>
  12. #include<memory.h>
  13. #include<set>
  14. #include<cctype>
  15.  
  16. #define ll long long
  17.  
  18. #define LL __int64
  19.  
  20. #define eps 1e-8
  21.  
  22. #define inf 0xfffffff
  23.  
  24. //const LL INF = 1LL<<61;
  25.  
  26. using namespace std;
  27.  
  28. //vector<pair<int,int> > G;
  29. //typedef pair<int,int > P;
  30. //vector<pair<int,int> > ::iterator iter;
  31. //
  32. //map<ll,int >mp;
  33. //map<ll,int >::iterator p;
  34.  
  35. const int N = 100000 + 5;
  36.  
  37. typedef struct Node {
  38. int color;
  39. int l,r;
  40. };
  41.  
  42. Node tree[N * 4];
  43. bool vis[50];
  44.  
  45. void init() {
  46. memset(vis,false,sizeof(vis));
  47. }
  48.  
  49. void build(int l,int r,int id) {
  50. tree[id].color = 1;
  51. tree[id].l = l;
  52. tree[id].r = r;
  53. if(tree[id].l == tree[id].r)return;
  54. int mid = (l + r)/2;
  55. build(l,mid,id<<1);
  56. build(mid + 1,r,id<<1|1);
  57. }
  58.  
  59. void update(int l,int r,int id,int color) {
  60. if(tree[id].l >= l && tree[id].r <= r) {
  61. tree[id].color = color;return;
  62. }
  63. int mid = (tree[id].l + tree[id].r)/2;
  64. tree[id].color = -1;
  65. if(r <= mid) update(l,r,id<<1,color);
  66. else {
  67. if(l > mid)update(l,r,id<<1|1,color);
  68. else {
  69. update(l,mid,id<<1,color);
  70. update(mid+1,r,id<<1|1,color);
  71. }
  72. }
  73. }
  74.  
  75. void update2(int l,int r,int id) {
  76. if(tree[id].color > 0) {
  77. vis[tree[id].color] = true;return;
  78. }
  79. if(tree[id].l == tree[id].r)return;
  80. int mid = (tree[id].l + tree[id].r)/2;
  81. if(r <= mid) update2(l,r,id<<1);
  82. else {
  83. if(l > mid)update2(l,r,id<<1|1);
  84. else {
  85. update2(l,mid,id<<1);
  86. update2(mid+1,r,id<<1|1);
  87. }
  88. }
  89. }
  90.  
  91. int find(int x) {
  92. int ans = 0;
  93. for(int i=1;i<=x;i++)
  94. if(vis[i])
  95. ans++;
  96. return ans;
  97. }
  98.  
  99. int main() {
  100. int n,m,q;
  101. while(scanf("%d %d %d",&n,&m,&q) == 3 ){
  102. memset(tree,0,sizeof(tree));
  103. init();
  104. build(1,n,1);
  105. while(q--) {
  106. char s[2];
  107. scanf("%s",s);
  108. if(s[0] == 'C') {
  109. int x,y,c;
  110. scanf("%d %d %d",&x,&y,&c);
  111. if(x > y)swap(x,y);
  112. update(x,y,1,c);
  113. }
  114. else {
  115. int x,y;
  116. scanf("%d %d",&x,&y);
  117. init();
  118. if(x > y)swap(x,y);
  119. update2(x,y,1);
  120. printf("%d\n",find(m));
  121. }
  122.  
  123. }
  124. }
  125. return 0;
  126. }

后来看了别人的一下,看到方法不一样,跑了案例也没发现自己的错误,继续检查还是不行。难道真的是方法不行?换了个方法过了。可是上面的代码错误原因还是没有查出来,WA哭

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<list>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<string>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<vector>
  11. #include<cmath>
  12. #include<memory.h>
  13. #include<set>
  14. #include<cctype>
  15.  
  16. #define ll long long
  17.  
  18. #define LL __int64
  19.  
  20. #define eps 1e-8
  21.  
  22. #define inf 0xfffffff
  23.  
  24. //const LL INF = 1LL<<61;
  25.  
  26. using namespace std;
  27.  
  28. //vector<pair<int,int> > G;
  29. //typedef pair<int,int > P;
  30. //vector<pair<int,int> > ::iterator iter;
  31. //
  32. //map<ll,int >mp;
  33. //map<ll,int >::iterator p;
  34.  
  35. const int N = 100000 + 5;
  36.  
  37. typedef struct Node {
  38. int l,r;
  39. int color;
  40. int flag;
  41. };
  42.  
  43. Node tree[N * 4];
  44.  
  45. void init() {
  46. memset(tree,0,sizeof(tree));
  47. }
  48.  
  49. void cal(int id) {
  50. tree[id].color = tree[id<<1].color | tree[id<<1|1].color;
  51. }
  52.  
  53. void build(int l,int r,int id) {
  54. tree[id].l = l;
  55. tree[id].r = r;
  56. tree[id].color = 1;
  57. tree[id].flag = 1;
  58. if(tree[id].l == tree[id].r) return;
  59. int mid = (l + r)/2;
  60. build(l,mid,id<<1);
  61. build(mid+1,r,id<<1|1);
  62. }
  63.  
  64. void cover(int id) {
  65. tree[id<<1].color = tree[id].color;
  66. tree[id<<1].flag = 1;
  67. tree[id<<1|1].color = tree[id].color;
  68. tree[id<<1|1].flag = 1;
  69. tree[id].flag = 0;
  70. }
  71.  
  72. void updata(int l,int r,int id,int col) {
  73. if(l <= tree[id].l && r >= tree[id].r) {
  74. tree[id].color = col;
  75. tree[id].flag = 1;
  76. return;
  77. }
  78. if(tree[id].color == col)return;
  79. if(tree[id].flag)cover(id);
  80. int mid = (tree[id].l + tree[id].r)/2;
  81. if(r <= mid) updata(l,r,id<<1,col);
  82. else if(l > mid)updata(l,r,id<<1|1,col);
  83. else {
  84. updata(l,mid,id<<1,col);
  85. updata(mid+1,r,id<<1|1,col);
  86. }
  87. cal(id);
  88. }
  89.  
  90. int ans;
  91.  
  92. void query(int l,int r,int id) {
  93. if(l <= tree[id].l && r >= tree[id].r) {
  94. ans |= tree[id].color;return;
  95. }
  96. if(tree[id].flag) {
  97. ans |= tree[id].color;return;
  98. }
  99. int mid = (tree[id].l + tree[id].r)/2;
  100. if(r <= mid) query(l,r,id<<1);
  101. else if(l > mid) query(l,r,id<<1|1);
  102. else {
  103. query(l,mid,id<<1);
  104. query(mid+1,r,id<<1|1);
  105. }
  106. }
  107.  
  108. int main() {
  109. int n,m,q;
  110. while(scanf("%d %d %d",&n,&m,&q) == 3) {
  111. init();
  112. build(1,n,1);
  113. char s[2];
  114. while(q--) {
  115. scanf("%s",s);
  116. if(s[0] == 'C') {
  117. int x,y,c;
  118. scanf("%d %d %d",&x,&y,&c);
  119. if(x > y)swap(x,y);
  120. updata(x,y,1,1<<(c-1));//把颜色改成用正向委员算表示
  121. }
  122. else {
  123. int x,y;
  124. scanf("%d %d",&x,&y);
  125. if(x > y)swap(x,y);
  126. ans = 0;
  127. query(x,y,1);
  128. int cnt = 0;
  129. while(ans) {
  130. if(ans%2)cnt++;
  131. ans /= 2;
  132. }
  133. printf("%d\n",cnt);
  134. }
  135. }
  136. }
  137. return 0;
  138. }

POJ2777 Count Color 线段树区间更新的更多相关文章

  1. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

  2. [poj2777] Count Color (线段树 + 位运算) (水题)

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

  3. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  4. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  5. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  6. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  7. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  8. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. hdu1556Color the ball线段树区间更新

    题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...

随机推荐

  1. wxwidgets安装环境配置

    一:安装VS2012 wxWidgets-2.9.5( 2.95版本为最稳定版本) 二:打开wxWidgets-2.9.5的安装目录,找到build-msw-wx_vc10.sln打开(等待) 三:打 ...

  2. Django 内容回顾

    模板 变量 {{ }} 标签 {% %} if elif else for empty forloop() with...as csrf_token 过滤器 default length add da ...

  3. 【知识总结】卡特兰数 (Catalan Number) 公式的推导

    卡特兰数的英文维基讲得非常全面,强烈建议阅读! Catalan number - Wikipedia (本文中图片也来源于这个页面) 由于本人太菜,这里只选取其中两个公式进行总结. (似乎就是这两个比 ...

  4. Highcharts Highstock 学习笔记 第一篇 Highcharts配置

    Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图.柱状图.饼图.散点图等多达 ...

  5. Android从Camera中获取图片的两种方法

    方法一: 此方法会由Camera直接产生照片回传给应用程序,但是返回的是压缩图片,显示不清晰 ? 1 2 3 4 5 6 try {      Intent cameraIntent = new In ...

  6. JSP执行原理图

  7. azkaban-executor启动时出现conf/global.properties (No such file or directory)的问题解决(图文详解)

     问题详情 // :: INFO [FlowRunnerManager] [Azkaban] Cleaning recently finished // :: INFO [FlowRunnerMana ...

  8. Python爬取贴吧中的图片

    #看到贴吧大佬在发图,准备盗一下 #只是爬取一个帖子中的图片 1.先新建一个scrapy项目 scrapy startproject TuBaEx 2.新建一个爬虫 scrapy genspider ...

  9. Angular——tab切换案例

    基本介绍 angular框架下的tab切换,相比较于之前的纯js写的代码,有一个很大的特点就是以数据为驱动,基本上不用搜索dom元素就可以实现效果 基本使用 (1)导航部分使用的是的状态使用的是ng- ...

  10. Less——less基本安装

    1.下载node.js 我们需要NodeJ运行LESS示例. 要下载NodeJ,请打开链接https://nodejs.org/en/ 2.node.js安装是否正确 在cmd中输入lessc -v, ...