ACM 离散化+线段树 poj2528
来源:互联网 发布:水利水电计价软件 编辑:程序博客网 时间:2024/06/16 04:14
离散化+线段树
注意离散化时,相隔为2的情况
(网上的大部分题解都有错误,如小优的POJ2528-Mayor's posters)
Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 42618 Accepted: 12422
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
Accepted34084K63MS
/* * Author: NICK WONG * Created Time: 8/21/2014 23:36:01 * File Name: tmp.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;#define out(x) cout<<#x<<": "<<x<<endlconst double eps(1e-8);const int maxn=40100;const long long maxint=-1u>>1;const long long maxlong=maxint*maxint;typedef long long lint;//map<int,int> mp;int n,a[maxn][2],b[maxn];int f[maxn*4],ans;int mp[10000100];bool vis[maxn];void init(){ cin>>n; int sum=0; for (int i=1; i<=n; i++) { scanf("%d%d",&a[i][0],&a[i][1]); b[++sum]=a[i][0]; b[++sum]=a[i][1]; }}void insert(int x,int l,int r,int st,int en,int c){ if (l==st && r==en) { f[x]=c; return; } if (f[x]) f[x+x]=f[x+x+1]=f[x]; f[x]=0; int mid=(l+r)>>1; if (st>mid) insert(x+x+1,mid+1,r,st,en,c); else if (en<=mid) insert(x+x,l,mid,st,en,c); else { insert(x+x,l,mid,st,mid,c); insert(x+x+1,mid+1,r,mid+1,en,c); }}void query(int x,int l,int r){ if (f[x]) { if (!vis[f[x]]) ans++; vis[f[x]]=true; return; } if (l==r) return; int mid=(l+r)>>1; query(x+x,l,mid); query(x+x+1,mid+1,r);}void work(){ //mp.clear(); memset(f,0,sizeof(f)); ans=0; sort(b+1,b+1+2*n); int sum=1; int old=b[1]; for (int i=1; i<=2*n; i++) { if (b[i]!=old) { if (b[i]-old>=2) sum+=2; else sum++; //相差大于二,离散化也为二 old=b[i]; } mp[b[i]]=sum; } for (int i=1; i<=n; i++) { int x=mp[a[i][0]]; int y=mp[a[i][1]]; insert(1,1,sum,x,y,i); } memset(vis,0,sizeof(vis)); query(1,1,sum); cout<<ans<<endl;}int main(){ int t; cin>>t; while(t--) { init(); work(); } return 0;}
0 0
- ACM 离散化+线段树 poj2528
- poj2528 线段树+离散化
- poj2528线段树+离散化
- POJ2528 离散化线段树
- poj2528(线段树+离散化)
- poj2528 线段树 离散化
- poj2528离散化+线段树
- 线段树+离散化 poj2528
- poj2528 离散化+线段树
- POJ2528,线段树+离散化
- poj2528(线段树+离散化)
- poj2528线段树+离散化
- 线段树 离散化 poj2528
- poj2528(离散化+线段树)
- 离散化+线段树 poj2528
- poj2528 线段树+离散化
- poj2528 离散线段树
- poj2528 (线段树+离散化)
- 迷宫问题
- windows编程过程
- <img src="t.cn/RPu2CiR" onerror="alert(123)">
- Visual Studio MSB8011 错误解决办法
- Linux IPC(Inter-Process Communication,进程间通信)之管道学习
- ACM 离散化+线段树 poj2528
- dynamically add and remove view to viewpager
- unix-shell-3
- Windows下MySQL 5.6安装及配置详细图解
- PHP定时执行计划任务
- 【LeetCode】Combinations
- php定时执行任务实现方法详解
- 内存Bank
- PAT 1040. Longest Symmetric String (25)