POJ 2528 Mayor & #39; s posters (line segment tree + discretization)
Address: POJ 2528
This question has been wrong for countless times... Finally, I looked at the blog of hh Daniel and suddenly realized .. Pay attention to discrete intervals ..
This question was taken out after writing a few scanned lines. I want to discretization the map that I used all the time .. After writing several line tree lines and scanning lines, it is much easier to write this question.
The following is a direct reference of hh Daniel ..
Idea: The data Scope of this question is very large, and timeout + super memory is directly involved. discretization is required:
Discretization is simply to use only the values we need. For example, we cannot use [-∞, 999] [1001,1989] [1991,1999] [2001,2011] [2013, + ∞] these values, so I only need, and to map them, the complexity is greatly reduced.
Therefore, for discretization, you need to save all the values that need to be used. After sorting, the values are mapped to 1 ~ N, the complexity will be much smaller.
The difficulty of this question is that each number actually represents a unit length (not a point), so normal discretization will cause many errors (including my previous code, poj is weak)
The following two simple examples show the defects of normal discretization:
Example 1: 1-10 1-4 5-10
Example 2: 1-10 1-4 6-10
After normal discretization, it becomes [1, 4] [1, 2] [3, 4].
If line 2 covers [1, 2] and line 3 covers [3, 4], is Line 1 completely overwritten?
Example 1 is completely overwritten, and Example 2 is not covered
To solve this problem, we can add some processing to the sorted array, such as [1, 2, 6, 10].
If the distance between adjacent numbers is greater than 1, add any number to it, such as adding [1, 2, 3, 6, 7, 10], and then create a line segment tree.
The Code is as follows:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include using namespace std;#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1int lazy[3000000], cnt, c[3000000], ans, _hash[3000000];struct node{ int a, b, c;}fei[1000000];void PushDown(int rt){ if(lazy[rt]) { lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt]; lazy[rt]=0; }}void update(int ll, int rr, int x, int l, int r, int rt){ if(ll<=l&&rr>=r) { lazy[rt]=x; return ; } PushDown(rt); int mid=l+r>>1; if(ll<=mid) update(ll,rr,x,lson); if(rr>mid) update(ll,rr,x,rson);}void query(int l, int r, int rt){ if(lazy[rt]) { if(!_hash[lazy[rt]]) { ans++; _hash[lazy[rt]]++; //printf("%d\n",lazy[rt]); } return ; } if(l==r) return ; int mid=l+r>>1; query(lson); query(rson);}int erfen(int x, int high){ int low=0, mid; while(low<=high) { mid=low+high>>1; if(c[mid]==x) return mid; else if(c[mid]>x) high=mid-1; else low=mid+1; }}int main(){ int t, n, i, a, b, k, cnt; scanf("%d",&t); while(t--) { scanf("%d",&n); k=0; memset(lazy,0,sizeof(lazy)); for(i=0;i
=1;i--) { if(c[i]!=c[i-1]+1) c[cnt++]=c[i-1]+1; } sort(c,c+cnt); for(i=0;i