BNUOJ 2528 Mayor's posters
Mayor's posters
64-bit integer IO format: %lld Java class name: Main
- 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.
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.
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.
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.
Sample input
1 5 1 4 2 6 8 10 3 4 7 10
Output for sample input
4
解题:线段树+离散化。挂了几次,居然还有贴在10-10这样位置的数据,简直太疯狂了。。这能贴么,一个点啊!好吧,改正后,终于Ac 了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #include <map> 14 #define LL long long 15 #define pii pair<int,int> 16 #define INF 0x3f3f3f3f 17 using namespace std; 18 const int maxn = 100100; 19 set<int>st; 20 int a[maxn],b[maxn]; 21 struct node{ 22 int lt,rt,flag; 23 }; 24 node tree[maxn<<2]; 25 int lisan[maxn<<2]; 26 void build(int lt,int rt,int v){ 27 tree[v].lt = lt; 28 tree[v].rt = rt; 29 tree[v].flag = 0; 30 if(lt + 1 == rt) return; 31 int mid = (lt+rt)>>1; 32 build(lt,mid,v<<1); 33 build(mid,rt,v<<1|1); 34 } 35 void update(int lt,int rt,int v,int val){ 36 if(lisan[tree[v].lt] == lt && lisan[tree[v].rt] == rt){ 37 tree[v].flag = val; 38 return; 39 } 40 if(tree[v].flag){ 41 tree[v<<1].flag = tree[v<<1|1].flag = tree[v].flag; 42 tree[v].flag = 0; 43 } 44 int mid = (tree[v].lt+tree[v].rt)>>1; 45 if(rt <= lisan[mid]){ 46 update(lt,rt,v<<1,val); 47 }else if(lt >= lisan[mid]){ 48 update(lt,rt,v<<1|1,val); 49 }else{ 50 update(lt,lisan[mid],v<<1,val); 51 update(lisan[mid],rt,v<<1|1,val); 52 } 53 } 54 void query(int v){ 55 if(tree[v].flag){ 56 if(!st.count(tree[v].flag)) st.insert(tree[v].flag); 57 return; 58 } 59 if(tree[v].lt+1 == tree[v].rt) return; 60 query(v<<1); 61 query(v<<1|1); 62 } 63 int main() { 64 int t,i,j,n,cnt,tot; 65 scanf("%d",&t); 66 while(t--){ 67 tot = 1; 68 scanf("%d",&n); 69 for(i = 1; i <= n; i++){ 70 scanf("%d %d",a+i,b+i); 71 if(a[i] > b[i]) swap(a[i],b[i]); 72 lisan[tot++] = a[i]; 73 lisan[tot++] = ++b[i]; 74 } 75 sort(lisan+1,lisan+tot); 76 cnt = 1; 77 for(i = 2; i < tot; i++){ 78 if(lisan[i] == lisan[cnt]) continue; 79 lisan[++cnt] = lisan[i]; 80 } 81 build(1,cnt,1); 82 for(i = 1; i <= n; i++) update(a[i],b[i],1,i); 83 st.clear(); 84 query(1); 85 printf("%d\n",st.size()); 86 } 87 return 0; 88 }
相关文章
- Django运行提示ModuleNotFoundError: No module named 'models'
- UnicodeDecodeError:'gbk' codec can't decode byte 0x80 in position 0 illegal multibyte sequence
- What is difference between 'data:' and 'data()' in Vue.js?
- What's the replacement for fuslogvw in .net core 2?
- python 写入日志的问题 UnicodeEncodeError: 'gbk' codec can't encode character 'xbb' in position 0: illegal multibyte sequence
- HDU 3729 I'm Telling the Truth (二分匹配)
- Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer <CALayer: 0x7fda42c66e30> is a part of cycle in its layer tree'
- Python读取txt文件报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0
- because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checkin
- ASP.NET list<object> OBJECT.clean()会清空session['OBJECT']的值的问题
- 关于vue-router Cannot read property 'matched' of undefined的问题
- “/”应用程序中的服务器错误和Server Error in '/' Application... 的终极解决方法
- error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
- [已解决]报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 80-81
- [未解决]报错: execjs._exceptions.ProgramError: TypeError: Cannot read property 'extend' of undefined
- Django学习路33_url 地址及删除元素 delete() 和重定向 return redirect('路径')
- Let's do our own full blown HTTP server with Netty--转载
- The type name 'IComponentConnector' could not be found in the namespace 'System.Windows.Markup'
- mysql连接不上Uncaught exception 'PDOException' with message 'could not find driver