poj2528 Mayor's posters(线段树区间修改+特殊离散化)
Description
- Every candidate can place exactly one poster on the wall.
- All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
- The wall is divided into segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
Input
Output
The picture below illustrates the case of the sample input.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 2e5 + 10;
int a[M] , b[M] , c[2 * M] , d[2 * M] , e[4 * M];
struct TnT {
int l , r , num , add;
}T[M << 2];
int re;
void push(int p) {
if(T[p].add) {
T[p << 1].num = (T[p << 1].r - T[p << 1].l + 1);
T[(p << 1) | 1].num = (T[(p << 1) | 1].r - T[(p << 1) | 1].l + 1);
T[p << 1].add = T[p].add;
T[(p << 1) | 1].add = T[p].add;
T[p].add = 0;
}
}
void build(int l , int r , int p) {
int mid = (l + r) >> 1;
T[p].l = l , T[p].r = r , T[p].num = 0 , T[p].add = 0;
if(T[p].l == T[p].r) {
return ;
}
build(l , mid , p << 1);
build(mid + 1 , r , (p << 1) | 1);
T[p].num = T[p << 1].num + T[(p << 1) | 1].num;
}
void updata(int l , int r , int p) {
int mid = (T[p].l + T[p].r) >> 1;
if(T[p].l == l && T[p].r == r) {
T[p].add = 1;
T[p].num = (r - l + 1);
return ;
}
push(p);
if(mid < l) {
updata(l , r , (p << 1) | 1);
}
else if(mid >= r) {
updata(l , r , p << 1);
}
else {
updata(l , mid , p << 1);
updata(mid + 1 , r , (p << 1) | 1);
}
T[p].num = T[p << 1].num + T[(p << 1) | 1].num;
}
int query(int l , int r , int p) {
int mid = (T[p].l + T[p].r) >> 1;
if(T[p].l == l && T[p].r == r) {
return T[p].num;
}
push(p);
T[p].num = T[p << 1].num + T[(p << 1) | 1].num;
if(mid < l) {
return query(l , r , (p << 1) | 1);
}
else if(mid >= r) {
return query(l , r , p << 1);
}
else {
return query(l , mid , p << 1) + query(mid + 1 , r , (p << 1) | 1);
}
}
int search(int ll, int hh, int xx) {
int mm;
while (ll <= hh) {
mm = (ll + hh) >> 1;
if (e[mm] == xx) return mm;
else if (e[mm] > xx) hh = mm - 1;
else ll = mm + 1;
}
return -1;
}
int main()
{
int t;
scanf("%d" , &t);
while(t--) {
int n;
scanf("%d" , &n);
int gg = 0;
for(int i = 1 ; i <= n ; i++) {
scanf("%d%d" , &a[i] , &b[i]);
c[++gg] = a[i];
c[++gg] = b[i];
}
sort(c + 1 , c + gg + 1);
int mm = 0;
c[gg + 1] = -1;
for(int i = 1 ; i <= gg ; i++) {
if(c[i] != c[i + 1]) {
d[++mm] = c[i];
}
}
e[1] = d[1];
int mt = 1;
for(int i = 2 ; i <= mm ; i++) {
if(d[i] - d[i - 1] > 1) {
e[++mt] = d[i - 1] + 1;
e[++mt] = d[i];
}
else {
e[++mt] = d[i];
}
}
// for(int i = 1 ; i <= mt ; i++) {
// cout << e[i] << ' ';
// }
build(1 , mt + 1 , 1);
int count = 0;
for(int i = n ; i >= 1 ; i--) {
int r = search(1 , mt , b[i]);
int l = search(1 , mt , a[i]);
re = query(l , r , 1);
//cout << re << endl;
if(re < r - l + 1) {
count++;
}
updata(l , r , 1);
}
printf("%d\n" , count);
}
return 0;
}
poj2528 Mayor's posters(线段树区间修改+特殊离散化)的更多相关文章
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- poj2528 Mayor's posters(线段树区间覆盖)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50888 Accepted: 14737 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- poj2528 Mayor's posters(线段树之成段更新)
Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...
随机推荐
- Linux EXT2 文件系统
磁盘是用来储文件的,但是必须先把磁盘格式化为某种格式的文件系统,才能存储文件.文件系统的目的就是组织和管理磁盘中的文件.在 Linux 系统中,最长见的是 ext2 系列的文件系统.其早期版本为 ex ...
- 关于python中的特殊方法
研究了几个小时,大概对python中的特殊方法一知半解,现在写写自己的理解,以及记录一些找到的资源.待自己有比较深入理解的时候,再来更新 https://docs.python.org/3/refer ...
- 当面对会反制遭破解装置的App该如何顺利提取数据
在检测App的过程之中,总会遇到比较棘手的,以”侦测是否遭破解的装置”为例,便会是个不好处理的状况.当App具备侦测装置是否已遭Root时,一旦发现装置已遭破解,便会停止运行,等于是只准安装及运行在未 ...
- linux文本编辑vim命令
1.Vim Vim 是一个功能强大的全屏幕文本编辑器,是 Linux/UNIX 上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件. Vim 没有菜单,只有命令 2.Vim 工作模式 3.插入 ...
- Java代码计算运行时间
突然想准确的测试一下Java代码的执行时间,在网上找了一会.发现基本有以下两种方法:第一种是以毫秒为单位计算的. Java代码 //伪代码 long startTime=System.currentT ...
- Jquery 实现添加删除,checkbok 的全选,反全选,但是批量删除没有实现
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title& ...
- 通过wireshark学习Traceroute命令和mtr(UDP,ICMP协议)
traceroute: 通过TTL限定的ICMP/UDP/TCP侦测包来发现从本地主机到远端目标主机之间的第三层转发路径.用来调试网络连接性和路由问题. mtr: traceroute的一个变种,能根 ...
- JavaWeb——Filter过滤器
1.Filter的目的 Filter用于在Servlet之前检测和修改请求和响应,它可以拒绝.重定向或转发请求.常见的有这几种: 日志过滤器 使用过滤器记录请求,提供请求日志记录,还可以添加追踪信息用 ...
- 用xshell链接虚拟机上的linux
[步骤]一.安装VMware直接点击下一步即可 二.安装linux下载CentOS,在VMware中安装,这个网上有很多教程,这里就不赘述了. 三.配置要想连接上xshell,需要配置IP,将Linu ...
- Javascript中,实现十大排序方法之一(冒泡排序及其优化设想)
冒泡排序的Javascript实现 首先定义一个取值范围在(0~100000)之间的随机值的长度为10万的数组, function bubbleSort(arr) { console.time('冒泡 ...