Poj 2528 Mayor's posters 线段树
来源:互联网 发布:vscode 支持xp吗 编辑:程序博客网 时间:2024/05/17 02:55
Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 64949 Accepted: 18771
Description
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:
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.
- 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
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.
Output
For each input data set print the number of visible posters after all the posters are placed.
The picture below illustrates the case of the sample input.
The picture below illustrates the case of the sample input.
Sample Input
151 42 68 103 47 10
Sample Output
4
Source
Alberta Collegiate Programming Contest 2003.10.18
线段树。
再不写一题线段树就忘记了
要注意的是:此题一个数表示一段线段
1
3
1 10
1 4
5 10
这组数据的答案是2,而不是3.
#include <cstdio>#include <iostream>#include <string.h>#include <queue>#include <vector>#include <map>#include <algorithm>using namespace std;typedef long long ll;const int maxn=40005;int a[maxn],b[maxn],l[maxn],r[maxn],f[maxn];int num,ans;struct Tree {int l,r,lc,rc,tag;};Tree tree[4*maxn];void pushdown(int now) {if (!tree[now].tag) return;tree[tree[now].lc].tag=tree[now].tag;tree[tree[now].rc].tag=tree[now].tag;tree[now].tag=0;}void build(int now,int l,int r) {tree[now].tag=0;tree[now].l=l;tree[now].r=r;if (l==r) return;num++;tree[now].lc=num;build(num,l,(l+r)/2);num++;tree[now].rc=num;build(num,(l+r)/2+1,r);}void update(int now,int l,int r,int val) {if (l<=tree[now].l&&tree[now].r<=r) {tree[now].tag=val;} else {pushdown(now);if (l<=(tree[now].l+tree[now].r)/2) update(tree[now].lc,l,r,val);if (r>(tree[now].l+tree[now].r)/2) update(tree[now].rc,l,r,val);}}void find(int now) {//cout << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].tag << endl;if (tree[now].tag) {if (!f[tree[now].tag]) {f[tree[now].tag]=1;ans++;}tree[now].tag=0;} else {if (tree[now].l==tree[now].r) return; //因为这句话挂了很多次 pushdown(now);find(tree[now].lc); find(tree[now].rc);}}int search(int val,int x,int y) {int l=x,r=y;while (l<=r) {int mid=(l+r)/2;if (b[mid]==val) return mid; else if (b[mid]>val) r=mid-1; else l=mid+1;}return -1;}int main() {int n,m,i,t,q;scanf("%d",&q);for (t=1;t<=q;t++) {memset(tree,0,sizeof(tree));scanf("%d",&n);for (i=1;i<=n;i++) {scanf("%d%d",&l[i],&r[i]);a[2*i-1]=l[i];a[2*i]=r[i];}sort(a+1,a+n*2+1);m=1;b[1]=a[1];for (i=2;i<=n*2;i++) {if (a[i]-a[i-1]>1) b[++m]=a[i]-1;if (a[i]!=a[i-1]) b[++m]=a[i];}sort(a+1,a+m+1);num=1;build(1,1,m);for (i=1;i<=n;i++) {int nowl,nowr;nowl=search(l[i],1,m);nowr=search(r[i],1,m);update(1,nowl,nowr,i);}memset(f,0,sizeof(f));ans=0;find(1);printf("%d\n",ans);} return 0;}
阅读全文
0 0
- POJ-2528 Mayor's posters 线段树
- 【线段树】poj 2528 Mayor's posters
- POJ 2528 Mayor's posters 线段树
- Poj 2528 Mayor's posters - 线段树
- POJ 2528 Mayor's posters 线段树
- POJ-2528-Mayor's posters(线段树)
- poj 2528 Mayor's posters 线段树
- POJ - 2528 Mayor's posters(线段树)
- poj 2528 Mayor's posters 线段树
- poj 2528 Mayor’s posters 线段树
- poj 2528 Mayor's posters 线段树
- |poj 2528|线段树|Mayor's posters
- Poj 2528 Mayor's posters 线段树
- POJ 2528 Mayor's posters [线段树]
- poj 2528 Mayor's posters(线段树区点)
- POJ Mayor's posters 线段树
- 离散化+线段树 POJ 2528 (Mayor's posters)
- POJ 2528 Mayor's posters(离散化+线段树)
- 华为机试: 查找组成一个偶数最接近的两个素数、自守数
- mybatis-plus自动生成代码到相应的model
- 购物车实现加减
- FreeRTOS的第一个任务是怎么跑起来的
- C#对文件或图片操作
- Poj 2528 Mayor's posters 线段树
- iframe父子兄弟之间调用传值(contentWindow && parent)
- Linux下C语言TCP编程02
- 私有构造函数可以防止该类在外部被实例化
- Oracle12C--包(四十七)
- 怎么验证交易是否成功
- 线控耳机
- zabbix监控windows进程
- 润乾报表js获取下拉数据集下拉框的显示值