-
D - Mayor's posters
- POJ - 2528
- 思路:离散到20000个点上然后进行区间更新用颜色来标记。最后查询不同颜色的数目。
-
#include<iostream> #include<cstring> #include<map> #include<stdio.h> #include<algorithm> using namespace std; #define maxn 210010 #define N 10000210 int t,n,L,R,ls[N]; int a[maxn][2],tot; int order[maxn],cnt; #define lson root<<1,l,mid #define rson root<<1|1,mid+1,r map<int,int>mmp; struct node { int l,r,color,lazy; } tree[2*maxn]; void push_down(int root) { tree[root*2].color=tree[root].color; tree[root*2+1].color=tree[root].color; } void build(int root,int l,int r) { tree[root].l=l; tree[root].r=r; tree[root].color=0; if(l==r) return ; int mid=(l+r)/2; build(lson); build(rson); } void updata(int root,int l,int r,int num) { if(tree[root].l==l&&tree[root].r==r) { tree[root].color=num; return ; } if(tree[root].color>0)push_down(root); int mid=(tree[root].l+tree[root].r)/2; if(r<=mid)updata(root*2,l,r,num); else if(l>mid)updata(root*2+1,l,r,num); else { updata(lson,num); updata(rson,num); } tree[root].color=-1; } void query(int root,int l,int r) { if(tree[root].color>0) { mmp[tree[root].color]=1; return; } if(tree[root].color==0) return ; int mid=(tree[root].l+tree[root].r)/2; if(r<=mid)query(root*2,l,r); else if(l>mid)query(root*2+1,l,r); else { query(lson); query(rson); } } int main() { scanf("%d",&t); while(t--) { mmp.clear(); tot=cnt=0; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d%d",&a[i][0],&a[i][1]); order[++tot]=a[i][0]; order[++tot]=a[i][1]; } sort(order+1,order+1+tot); ls[order[1]]=++cnt; for(int i=2; i<=tot; i++) if(order[i]!=order[i-1]) ls[order[i]]=++cnt; build(1,1,cnt); for(int i=1; i<=n; i++) updata(1,ls[a[i][0]],ls[a[i][1]],i); query(1,1,cnt); printf("%d\n",mmp.size()); } return 0; }
D - Mayor's posters POJ - -线段树+离散化
最新推荐文章于 2019-05-01 21:34:57 发布