【线段树】Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 66154 | Accepted: 19104 |
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.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
Source
1
3
1 3
6 10
1 10 //正确输出:3
//错误输出:2
//问题原因:离散化成了[1,2] [3,4] [1,4],这样确实只剩下2了
如何解决?在两两之差>1时(区域不会被完全覆盖),就可以在这里插入一个节点以标记这里有一个区间要算。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=200001;
const int INF=999999;
int N,M;
int T;
int A[MAXN*2],a[MAXN*2],b[MAXN*2];
int tr[MAXN*8+100];
int cnt,tmp3;
bool flag[MAXN*8+100];
bool Hash[MAXN*8+100];
int tmp; void tage_lazy(int rt,int l,int r){
if(flag[rt]){
flag[rt*2]=flag[rt*2+1]=true;
tr[rt*2]=tr[rt*2+1]=tr[rt];
flag[rt]=false;
}
return ;
}
void add(int l,int r,int rt,int L,int R){
if(L<=l&&R>=r){
tr[rt]=cnt;
flag[rt]=true;
return ;
}
tage_lazy(rt,l,r);
int mid=(l+r)>>1;
if(mid<R) add(mid+1,r,rt*2+1,L,R);
if(mid>=L) add(l,mid,rt*2,L,R);
return ;
}
int Que(int l,int r,int rt,int L,int R){
if(flag[rt]){
if(!Hash[tr[rt]]){
Hash[tr[rt]]=true;
return 1;
}
else return 0;
}
if(l==r) return 0;
int mid=(l+r)>>1;
return Que(l,mid,rt*2,L,R)+Que(mid+1,r,rt*2+1,L,R);
} int main(){
T=read();
while(T--){
memset(tr,0,sizeof(tr));
memset(flag,false,sizeof(flag));
memset(Hash,false,sizeof(Hash));
N=read();tmp=tmp3=0;
for(int i=1;i<=N;i++){
++tmp;A[tmp]=a[tmp]=read();
++tmp;A[tmp]=a[tmp]=read();
}
sort(a+1,a+tmp+1);
int tmp2=0;int treef=tmp;
for(int i=1;i<=tmp;i++)
if(a[i]==a[i-1]) treef--;
else b[++tmp3]=a[i];
int k=tmp3;
for(int i=1;i<=k;i++)
if(b[i]>b[i-1]+1) b[++tmp3]=b[i-1]+1;
sort(b+1,b+tmp3+1);
treef=tmp3;
for(int i=1;i<=N;i++){
++cnt;
int l=lower_bound(b+1,b+tmp3+1,A[tmp2+1])-b;
int r=lower_bound(b+1,b+tmp3+1,A[tmp2+2])-b;
add(1,treef,1,l,r);
tmp2+=2;
}
printf("%d\n",Que(1,treef,1,1,treef));
}
}
【线段树】Mayor's posters的更多相关文章
- 线段树 Mayor's posters
甚至DFS也能过吧 Mayor's posters POJ - 2528 The citizens of Bytetown, AB, could not stand that the candidat ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- Mayor's posters(离散化线段树)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 54067 Accepted: 15713 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
- Mayor's posters POJ - 2528(线段树 + 离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74745 Accepted: 21574 ...
随机推荐
- 安装magento配置完数据库后出现: PHP Extensions "0" must be loaded. 解决方案
打开magento目录下的该文件: app/code/core/Mage/Install/etc/config.xml 将下面该段修改: <mysql4> <type>pdo_ ...
- Angular2.0 基础: Form
对于Angular2.0 的Form表单中的隐藏和验证,个人觉得还是挺有意思的. 1.通过ngModel 跟踪修改状态与验证. 在表单中使用 ngModel 可以获得更多的控制权,包括一些常用的验证. ...
- 74cms 注入exp
遇到就瞎写了一个: #!/usr/bin/env python #encoding:utf-8 #by i3ekr import requests,optparse,re parse = optpar ...
- Python标准库笔记(4) — collections模块
这个模块提供几个非常有用的Python容器类型 1.容器 名称 功能描述 OrderedDict 保持了key插入顺序的dict namedtuple 生成可以使用名字来访问元素内容的tuple子类 ...
- flask_返回字节流错误
# flask_返回字节流错误 def export_data(filename, fields, data, names=None, sheet='Sheet1'): # fields 为list ...
- 二叉查找树、平衡二叉树、红黑树、B-/B+树性能对比
转载:https://blog.csdn.net/z702143700/article/details/49079107 前言:BST.AVL.RBT.B-tree都是动态结构,查找时间基本都在O(l ...
- .net连接sql server的几种连接字符串的写法
.net连接sql server的几种连接字符串的写法 1, 混合验证模式登录 server=电脑名 或 电脑IP;database=数据库名;uid=数据库登录名;password=数据库登录密码 ...
- 动画基础--基于Core Animation(3)
参考:https://zsisme.gitbooks.io/ios-/content/ 前面的文章动画基础--基于Core Animation(1),动画基础--基于Core Animation(2) ...
- 1.Python3标准库--前戏
Python有一个很大的优势便是在于其拥有丰富的第三方库,可以解决很多很多问题.其实Python的标准库也是非常丰富的,今后我将介绍一下Python的标准库. 这个教程使用的书籍就叫做<Pyth ...
- seq和{ }生成序列
基本用法 [root@C ~]# seq 5 1 2 3 4 5 [root@C ~]# echo {1..5} 1 2 3 4 5 #步进输出 [root@C ~]# seq 1 2 5 1 3 5 ...