注意离散化!!!线段树的叶子结点代表的是一段!!!

给出下面两个简单的例子应该能体现普通离散化的缺陷:

1-10 1-4 5-10

1-10 1-4 6-10

普通离散化算出来的结果都会是2,但是第二组样例结果是3

如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.

线段树功能:update 成段更新,query 查询整个线段树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std; const int MAXN = 20010;
int p[MAXN], mp[MAXN], pp[MAXN];
int pst[MAXN<<4], ans;
bool flag[MAXN]; bool cmp(int A, int B)
{
return p[A] < p[B];
} void push_down(int rt)
{
if(pst[rt] == 0) return;
pst[rt<<1] = pst[rt<<1|1] = pst[rt];
pst[rt] = 0;
} void update(int L, int R, int c, int l, int r, int rt)
{
if(L <= l && r <= R)
{
pst[rt] = c;
return;
}
push_down(rt);
int m = (l + r) >> 1;
if(m >= L) update(L, R, c, lson);
if(m < R) update(L, R, c, rson);
} void query(int l, int r, int rt)
{
if(pst[rt] > 0) //只有大于0才有海报
{
if(!flag[pst[rt]]) ans++;
flag[pst[rt]] = 1;
return;
}
if(l == r) return;
push_down(rt);
int m = (l + r) >> 1;
query(lson);
query(rson);
} int main()
{
// freopen("in.txt", "r", stdin);
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%d%d", &p[i<<1], &p[i<<1|1]);
mp[i<<1] = i<<1;
mp[i<<1|1] = i<<1|1;
}
sort(mp, mp+2*n, cmp);
pp[mp[0]] = 1;
int N = 1;
for(int i=1; i<2*n; i++) //离散化
{
if(p[mp[i]] == p[mp[i-1]])
pp[mp[i]] = N;
else if(p[mp[i]] - p[mp[i-1]] > 1) //大于1的插一个数
{
N += 2;
pp[mp[i]] = N;
}
else pp[mp[i]] = ++N;
}
memset(pst, 0, sizeof(pst));
for(int i=0; i<n; i++)
update(pp[i<<1], pp[i<<1|1], i+1, 1, N, 1);
memset(flag, 0, sizeof(flag));
ans = 0;
query(1, N, 1);
printf("%d\n", ans);
}
return 0;
}

POJ 2528 Mayor's posters 贴海报 线段树 区间更新的更多相关文章

  1. POJ - 2528 Mayor's posters (离散化+线段树区间修改)

    https://cn.vjudge.net/problem/POJ-2528 题意 给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张? 分析 ...

  2. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  3. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  4. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  5. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  6. A Corrupt Mayor's Performance Art(线段树区间更新+位运算,颜色段种类)

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  9. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

随机推荐

  1. Ubuntu 15.04下安装Docker

    最近听说Docker很火,不知道什么东西,只知道是一个容器,可以跨平台.闲来无事,我也来倒弄倒弄.本文主要介绍:Ubuntu下的安装,以及基本的入门命令介绍:我的机器是Ubuntu 15.04 64位 ...

  2. "sar"工具 利用率

    LTP--linux稳定性测试 linux性能测试 ltp压力测试   余二五 2017-11-14 16:20:00 浏览1172 linux 日志 配置 内存管理 测试 脚本 性能测试 压力测试 ...

  3. windows怎么访问linux的samba共享目录

    windows怎么访问linux的samba共享目录 听语音 原创 | 浏览:6976 | 更新:2018-07-31 13:20 | 标签:LINUX WINDOWS 1 2 3 4 5 6 7 分 ...

  4. Linux服务之nginx服务篇一(概念)

    nginx官网:http://nginx.org/ 一. nginx和apache的区别 Nginx: 1.轻量级,采用 C 进行编写,同样的 web 服务,会占用更少的内存及资源. 2.抗并发,ng ...

  5. git OpenSSL SSL_connect问题

    遇到这个问题,查找别人也遇到,省时间不写了直接复制 在使用Git来克隆仓库报了错误,如下: fatal: unable to access 'https://github.com/xingbuxing ...

  6. 10.4 route:显示或管理路由表

    route命令 可以显示或管理Linux系统的路由表,route命令设置的路由主要是静态路由. 路由的概念     计算机与计算机之间的数据传输必须得经由网络,而网络可以通过直接连接两台计算机的方式或 ...

  7. python发送丁丁消息

    import requests import time import hashlib import hmac import base64 import re def SendMessage(messa ...

  8. Docker删除某个容器时失败解决方案

    删除某个容器时,报错 ocker rm 容器id   image is being used by stopped container e11efb30362a   该报错的原因是要删除的该镜像,被某 ...

  9. NVIDIA GPU的快速傅立叶变换

    NVIDIA GPU的快速傅立叶变换 cuFFT库提供GPU加速的FFT实现,其执行速度比仅CPU的替代方案快10倍.cuFFT用于构建跨学科的商业和研究应用程序,例如深度学习,计算机视觉,计算物理, ...

  10. 3D结构光

    3D结构光 3D结构光的整个系统包含结构光投影设备.摄像机.图像采集和处理系统.其过程就是投影设备发射光线到被测物体上,摄像机拍摄在被测物体上形成的三维光图形,拍摄图像经采集处理系统处理后获得被测物体 ...