离散化的思想:

对于这样的数据
(3,10000)。
(9,1000000)。
(5。100000),
(1,1000)。
(7,1000000)
我们能够将其处理为
(2,7)。
(5,9)。
(3,8),
(1,6),
(4,9)

我们再对离散化之后的数据进行处理即可了。
题目意思:

n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000)。

求出最后还能看见多少张海报。

參考代码:
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <string.h>
  5. using namespace std;
  6. const int MAXN = 10000+5;
  7. struct Node{
  8. int l,r;
  9. int c;
  10. }tree[14*MAXN]; //线段树
  11. struct Seg{
  12. int l,r;
  13. }s[MAXN]; //存储报纸的范围
  14. struct Seg2{
  15. int p;
  16. int index;
  17. }a[2*MAXN]; //存放端点信息
  18. bool cmp(Seg2 x,Seg2 y){
  19. return x.p<y.p;
  20. }
  21. int color[2*MAXN];
  22. int ans=0;
  23. void build(int node,int l,int r){
  24. tree[node].l=l,tree[node].r=r,tree[node].c=0;
  25. if (l==r) return;
  26. build(node*2,l,(l+r)/2);
  27. build(node*2+1,(l+r)/2+1,r);
  28. }
  29. void update(int node,int l,int r,int v){
  30. //cout<<l<<" "<<r<<endl;
  31. if (tree[node].l>=l && tree[node].r<=r){
  32. tree[node].c=v;
  33. return;
  34. }
  35. if (tree[node].c>0){
  36. tree[2*node].c=tree[2*node+1].c=tree[node].c;
  37. tree[node].c=0;
  38. }
  39. int mid=(tree[node].l+tree[node].r)/2;
  40. if (r<=mid)
  41. update(2*node,l,r,v);
  42. else if (mid<l)
  43. update(2*node+1,l,r,v);
  44. else{
  45. update(2*node,l,mid,v);
  46. update(2*node+1,mid+1,r,v);
  47. }
  48. }
  49. void count(int node){
  50. if (tree[node].c>0){
  51. if (color[tree[node].c]==0){
  52. ans++;
  53. color[tree[node].c]++;
  54. }
  55. return;
  56. }
  57. if (tree[node].l==tree[node].r)
  58. return;
  59. count(2*node);
  60. count(2*node+1);
  61. }
  62. void solve(int n,int t){
  63. build(1,1,t);
  64. //cout<<"--------"<<endl;
  65. for (int i=1;i<=n;i++){
  66. update(1,s[i].l,s[i].r,i);
  67. //cout<<"123456789\n";
  68. }
  69. ans=0;
  70. memset(color,0,sizeof(color));
  71. count(1);
  72. printf("%d\n",ans);
  73. }
  74. int main(){
  75. int t,n;
  76. scanf("%d",&t);
  77. while (t--){
  78. scanf("%d",&n);
  79. for (int i=1;i<=n;i++){
  80. scanf("%d%d",&s[i].l,&s[i].r);
  81. a[2*i-1].p=s[i].l;
  82. a[2*i-1].index=i; //标记为左端点
  83. a[2*i].p=s[i].r;
  84. a[2*i].index=-i; //标记为右端点
  85. }
  86. sort(a+1,a+2*n+1,cmp);
  87. //数据离散化
  88. int t=1;
  89. int tmp=a[1].p;
  90. for (int i=1;i<=2*n;i++){
  91. if (tmp!=a[i].p){
  92. tmp=a[i].p;
  93. t++;
  94. }
  95. if (a[i].index>0)
  96. s[a[i].index].l=t;
  97. else
  98. s[-a[i].index].r=t;
  99. }
  100. /*
  101. for (int i=1;i<=n;i++){
  102. printf("%d %d\n",s[i].l,s[i].r);
  103. }
  104. */
  105. solve(n,t);
  106. }
  107. return 0;
  108. }

poj2528 Mayor&#39;s posters(线段树,离散化)的更多相关文章

  1. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  2. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  3. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  4. Mayor's posters (线段树+离散化)

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  5. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  6. POJ 2528 Mayor's posters (线段树+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:75394   Accepted: 21747 ...

  7. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  8. POJ2528Mayor's posters[线段树 离散化]

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59683   Accepted: 17296 ...

  9. POJ2528 Mayor&#39;s posters 【线段树】+【成段更新】+【离散化】

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39795   Accepted: 11552 ...

随机推荐

  1. 游戏开发人员眼中的Unity 3D网页游戏測评报告

    眼下.能够实现3D页游的主流技术有Silverlight.XNA.Flash.HTML5和Unity3D. 当中.Unity3D作为一款专注于3D游戏的浏览器插件.最近在国内外页游产品线骚动异常:本人 ...

  2. 开发效率必备之Mac双屏显示

    自从2015年9月苹果公布EI Captain,带来了一个新的功能,叫做分屏,也就是在一块屏幕上分成左右两部分,能够分别进行操作,互不影响. 例如以下图所看到的: watermark/2/text/a ...

  3. 0x27 A*

    终于完全了解A*到底是什么玩意儿了 对于当前的决策,选取当前花费+预估花费最小来拓展. 因为假如预估出现失误,那么很可能就会延伸到一个错误的决策点,而这个决策点偏偏就是ed,而由于预估失误,其他点的当 ...

  4. hdoj--1495--非常可乐(搜索+隐式图)

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. CSS中alt和title属性的正确使用

    1.在<img>标签中的使用 alt:全称为alttext,实质是当图片无法正确显示时用于替换(在IE下同时起到了title的作用,即鼠标滑过时文字提示): title:鼠标经过时文字提示 ...

  6. git服务器搭建-gitosis

    需求 硬件需求:一台Ubuntu或者debian电脑(虚拟机),能通过网络访问到. 软件需求:git-core, gitosis, openssh-server, openssh-client, Ap ...

  7. redis模拟消息订阅

    使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 客户端例子: redis 127.0.0.1:6379> subscribe news Read ...

  8. 洛谷P4012 深海机器人问题(费用流)

    题目描述 深海资源考察探险队的潜艇将到达深海的海底进行科学考察. 潜艇内有多个深海机器人.潜艇到达深海海底后,深海机器人将离开潜艇向预定目标移动. 深海机器人在移动中还必须沿途采集海底生物标本.沿途生 ...

  9. mysql+spring+mybatis实现数据库读写分离[代码配置] .

    场景:一个读数据源一个读写数据源. 原理:借助spring的[org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource] ...

  10. Haskell手撸Softmax回归实现MNIST手写识别

    Haskell手撸Softmax回归实现MNIST手写识别 前言 初学Haskell,看的书是Learn You a Haskell for Great Good, 才刚看到Making Our Ow ...