poj2528---Mayor's posters

题意:有t组测试数据,有n张海报,海报按题目给出的顺序覆盖,问最后能看到几张海报。这道题刚开始没想出来,,最后看的题解,,从最后一张海报开始处理 一下子就明白了。线段树区间更新搞之。(当然离散化是必须的)

预处理区间所有值都置0,然后从最后面开始,每次求 l[i] ~r[i]区间的和,如果小于r[i]-l[i]+1,就说明这张海报可以看见,然后把l[i]~r[i]这个区间所有值置为1,继续下一张海报。

 1 #include <set>
 2 #include <map>
 3 #include <cmath>
 4 #include <ctime>
 5 #include <queue>
 6 #include <stack>
 7 #include <cctype>
 8 #include <cstdio>
 9 #include <string>
10 #include <vector>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 typedef unsigned long long ull;
17 typedef long long ll;
18 const int inf = 0x3f3f3f3f;
19 const double eps = 1e-8;
20 const int maxn = 2e4+10;
21 int n,a[maxn*2],setv[maxn<<2];
22 void push_down(int pos)
23 {
24     if (~setv[pos])
25     {
26         setv[pos<<1] = setv[pos<<1|1] = setv[pos];
27         setv[pos] = -1;
28     }
29 }
30 void update(int l,int r,int pos,int ua,int ub,int v)
31 {
32     if (ua <= l && ub >= r)
33         setv[pos] = v;
34     else
35     {
36         push_down(pos);
37         int mid = (l + r) >> 1;
38         if (ua <= mid)
39             update(l,mid,pos<<1,ua,ub,v);
40         if (ub > mid)
41             update(mid+1,r,pos<<1|1,ua,ub,v);
42     }
43 }
44 int query(int l,int r,int pos,int ua,int ub)
45 {
46     if (~setv[pos])
47     {
48         return setv[pos] * (min(ub,r) - max(ua,l) + 1);
49     }
50     int mid = (l + r) >> 1;
51     int ans = 0;
52     if (ua <= mid)
53         ans += query(l,mid,pos<<1,ua,ub);
54     if (ub > mid)
55         ans += query(mid+1,r,pos<<1|1,ua,ub);
56     return ans;
57
58 }
59 int x[maxn],y[maxn];
60 int main(void)
61 {
62     #ifndef ONLINE_JUDGE
63         freopen("in.txt","r",stdin);
64     #endif
65     int t;
66     scanf ("%d",&t);
67     while(t--)
68     {
69         scanf ("%d",&n);
70         int tot = 0;
71         for (int i = 0; i < n; i++)
72         {
73             scanf ("%d%d",x+i,y+i);
74             a[tot++] = x[i];
75             a[tot++] = y[i];
76         }
77         sort(a,a+tot);
78         tot = unique(a,a+tot) - a;
79         for (int i = 0; i < n; i++)
80         {
81             x[i] = lower_bound(a,a+tot,x[i]) - a + 1;
82             y[i] = lower_bound(a,a+tot,y[i]) - a + 1;
83         }
84         int ans = 0;
85         update(1,tot,1,1,tot,0);
86         for (int i = n - 1; i >= 0; i--)
87         {
88             if (query(1,tot,1,x[i],y[i]) < y[i] - x[i] + 1)
89             {
90                 ans++;
91                 update(1,tot,1,x[i],y[i],1);
92             }
93         }
94         printf("%d\n",ans);
95     }
96     return 0;
97 }

poj2528---Mayor's posters

时间: 2024-08-02 02:43:55

poj2528---Mayor's posters的相关文章

POJ2528——Mayor&#39;s posters

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44910   Accepted: 13059 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

【线段树+离散化】POJ2528 Mayor&#39;s posters

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 64939   Accepted: 18770 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

poj2528 Mayor&#39;s posters(线段树,离散化)

离散化的思想: 对于这样的数据 (3,10000), (9,1000000), (5,100000), (1,1000), (7,1000000) 我们可以将其处理为 (2,7), (5,9), (3,8), (1,6), (4,9) 我们再对离散化之后的数据进行处理就行了. 题目意思: n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000). 求出最后还能看见多少张海报. 参考代码: #include <iostre

POJ2528 Mayor&#39;s posters(线段树染色问题+离散化)

题目大意:有t组数据,每组数据给你n张海报(1<=n<=10000),下面n组数据分别给出每张海报的左右范围(1 <= l <= r <= 10000000),下一张海报会覆盖前一张海报,求最后可见(包括完全和不完全可见)的海报有几张. 例如: 1 5 1 4 2 6 8 10 3 4 7 10 如上图所示,答案为4. 解题思路:其实这是一道区间染色问题,但是由于查找区间太大,显然直接建树会导致MLE,所以这里通过使用对区间的离散化来缩小查找范围.参考了一些大牛博客,简单说一

POJ2528 Mayor&#39;s posters 【线段树】+【成段更新】+【离散化】

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39795   Accepted: 11552 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

poj-----(2528)Mayor&#39;s posters(线段树区间更新及区间统计+离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

poj2528 Mayor&#39;s posters 2011-12-20

Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 23344Accepted: 6747 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at

poj2528 Mayor&#39;s posters(线段树+离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42808   Accepted: 12481 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

POJ2528 Mayor&#39;s posters(线段树成段替换,区间查询,离散化简单hash)

题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要的值来 用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要 1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了 所以离散化要保存所有需要用到的值,排序后,

poj2528 Mayor&#39;s posters (线段树+离散化)

恩,这区间范围挺大的,需要离散化.如果TLE,还需要优化一下常数. AC代码 #include <stdio.h> #include <string.h> #include <map> #include <set> #include <algorithm> using namespace std; const int maxn = 40000+5; typedef pair<int, int> Pii; Pii a[10000 + 5