Poj 2528 mayor's posters discretization + line segment tree

Source: Internet
Author: User

The following figure shows the number of posters that can be pasted on the wall.


Idea: the range is too large, but there are only 10000 posters at most, so discretization is required. Then we can use the line segment tree to make it happen.

The key is the discretization method. I had to drop this question after half a year, and I had been TTT before. I thought it was a line segment tree. I wrote this question again when I thought that my own horizontal water line segment tree was basically useless. It was still t. Later, I compared other people's code and found that it was my discretization. The following is an example of the AC code (79 Ms). The discretization is elegant and time-consuming. It will be written later.


Code (poj 79 Ms ):


#include <map>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 100010#define LEFT (pos << 1)#define RIGHT (pos << 1|1)using namespace std;pair<int,int> interval[MAX];pair<int,int *> src[MAX << 1];int cases;int tree[MAX << 2];int intervals;int cnt,t;bool color[MAX];inline void Initialize(){t = cnt = 0;memset(color,false,sizeof(color));memset(tree,0,sizeof(tree));}inline void PushDown(int pos){if(tree[pos]) {tree[LEFT] = tree[pos];tree[RIGHT] = tree[pos];tree[pos] = 0;}}inline void Modify(int l,int r,int x,int y,int pos,int c){if(l == x && y == r) {tree[pos] = c;return ;}PushDown(pos);int mid = (l + r) >> 1;if(y <= mid)Modify(l,mid,x,y,LEFT,c);else if(x > mid)Modify(mid + 1,r,x,y,RIGHT,c);else {Modify(l,mid,x,mid,LEFT,c);Modify(mid + 1,r,mid + 1,y,RIGHT,c);}}void GetAns(int l,int r,int pos){if(tree[pos]) {color[tree[pos]] = true;return ;}if(l == r)return ;int mid = (l + r) >> 1;GetAns(l,mid,LEFT);GetAns(mid + 1,r,RIGHT);}int main(){for(cin >> cases; cases; --cases) {scanf("%d",&intervals);Initialize();for(int i = 1; i <= intervals; ++i) {scanf("%d%d",&interval[i].first,&interval[i].second);src[++cnt] = make_pair(interval[i].first,&interval[i].first);src[++cnt] = make_pair(interval[i].second,&interval[i].second);}sort(src + 1,src + cnt + 1);for(int i = 1; i <= cnt; ++i){if(src[i].first != src[i - 1].first)++t;*src[i].second = t;}for(int i = 1; i <= intervals; ++i)Modify(1,cnt,interval[i].first,interval[i].second,1,i);GetAns(1,cnt,1);int ans = 0;for(int i = 1; i <= intervals; ++i)ans += color[i];printf("%d\n",ans);}return 0;}

In order to alert future generations and to alert myself (not to write discretization again in the future), I will also publish the discretization code of my T, so that my child will never learn.


Code (poj TLE ):


#include <map>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 1000010#define LEFT (pos << 1)#define RIGHT (pos << 1|1)using namespace std;pair<int,int> interval[MAX];map<int,int> G;int cases;int tree[MAX << 2];int intervals,src[MAX << 1];int cnt,t;bool color[MAX];inline void Initialize(){G.clear();t = cnt = 0;memset(color,false,sizeof(color));memset(tree,0,sizeof(tree));}inline void PushDown(int pos){if(tree[pos]) {tree[LEFT] = tree[pos];tree[RIGHT] = tree[pos];tree[pos] = 0;}}inline void Modify(int l,int r,int x,int y,int pos,int c){if(l == x && y == r) {tree[pos] = c;return ;}PushDown(pos);int mid = (l + r) >> 1;if(y <= mid)Modify(l,mid,x,y,LEFT,c);else if(x > mid)Modify(mid + 1,r,x,y,RIGHT,c);else {Modify(l,mid,x,mid,LEFT,c);Modify(mid + 1,r,mid + 1,y,RIGHT,c);}}void GetAns(int l,int r,int pos){if(tree[pos]) {color[tree[pos]] = true;return ;}if(l == r)return ;int mid = (l + r) >> 1;GetAns(l,mid,LEFT);GetAns(mid + 1,r,RIGHT);}int main(){for(cin >> cases; cases; --cases) {scanf("%d",&intervals);Initialize();for(int i = 1; i <= intervals; ++i) {scanf("%d%d",&interval[i].first,&interval[i].second);src[++cnt] = interval[i].first;src[++cnt] = interval[i].second;}sort(src + 1,src + cnt + 1);G[src[1]] = ++t;for(int i = 2; i <= cnt; ++i)if(src[i] != src[i - 1])G[src[i]] = ++t;for(int i = 1; i <= intervals; ++i)Modify(1,cnt,G[interval[i].first],G[interval[i].second],1,i);GetAns(1,cnt,1);int ans = 0;for(int i = 1; i <= intervals; ++i)ans += color[i];printf("%d\n",ans);}return 0;}


Later, I studied this Discretization Method with my friends. Normally, it would not be T, although I had a log. However, this question is about multiple groups of data. In this case, we need to clear the map for each group of data. According to the teacher, the clear map is deleted one by one... So I will create a new map every time for the mechanism, so that no clear is needed. The result is.


Code (poj 344 ms ):


#include <map>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 1000010#define LEFT (pos << 1)#define RIGHT (pos << 1|1)using namespace std;pair<int,int> interval[MAX];map<int,int> *G;int cases;int tree[MAX << 2];int intervals,src[MAX << 1];int cnt,t;bool color[MAX];inline void Initialize(){G = new map<int,int>;t = cnt = 0;memset(color,false,sizeof(color));memset(tree,0,sizeof(tree));}inline void PushDown(int pos){if(tree[pos]) {tree[LEFT] = tree[pos];tree[RIGHT] = tree[pos];tree[pos] = 0;}}inline void Modify(int l,int r,int x,int y,int pos,int c){if(l == x && y == r) {tree[pos] = c;return ;}PushDown(pos);int mid = (l + r) >> 1;if(y <= mid)Modify(l,mid,x,y,LEFT,c);else if(x > mid)Modify(mid + 1,r,x,y,RIGHT,c);else {Modify(l,mid,x,mid,LEFT,c);Modify(mid + 1,r,mid + 1,y,RIGHT,c);}}void GetAns(int l,int r,int pos){if(tree[pos]) {color[tree[pos]] = true;return ;}if(l == r)return ;int mid = (l + r) >> 1;GetAns(l,mid,LEFT);GetAns(mid + 1,r,RIGHT);}int main(){for(cin >> cases; cases; --cases) {scanf("%d",&intervals);Initialize();for(int i = 1; i <= intervals; ++i) {scanf("%d%d",&interval[i].first,&interval[i].second);src[++cnt] = interval[i].first;src[++cnt] = interval[i].second;}sort(src + 1,src + cnt + 1);(*G)[src[1]] = ++t;for(int i = 2; i <= cnt; ++i)if(src[i] != src[i - 1])(*G)[src[i]] = ++t;for(int i = 1; i <= intervals; ++i)Modify(1,cnt,(*G)[interval[i].first],(*G)[interval[i].second],1,i);GetAns(1,cnt,1);int ans = 0;for(int i = 1; i <= intervals; ++i)ans += color[i];printf("%d\n",ans);}return 0;}


Poj 2528 mayor's posters discretization + line segment tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.