POJ Mayor's posters 线段树

在此给出一组测试数据,

1
3
1 10
1 3
6 10
答案是:3

错的不知所以,重写才能过系列。

传送门:http://poj.org/problem?id=2528

#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 100005;

struct Node {
	int x,y;
	int lazy,value;
}t[MAXN<<2];

int li[MAXN];
int ri[MAXN];
int c[MAXN<<1];
int b[MAXN<<1];
bool flag[MAXN];
int cnt ,len;

void Push_Down(int rt) {
    if(t[rt].lazy) {
        t[rt<<1].value = t[rt<<1].lazy = t[rt].lazy;
        t[rt<<1|1].value = t[rt<<1|1].lazy = t[rt].lazy;
        t[rt].lazy = 0;
    }
}

void Build(int x,int y,int rt) {
	t[rt].x = x ; t[rt].y = y ; t[rt].lazy = 0;
	if(x == y) {
		return ;
	}
	int mid = (x + y) >> 1;
	Build(x,mid,rt<<1);
	Build(mid+1,y,rt<<1|1);
}

void Update(int rt,int left,int right,int value) {
	if(t[rt].x >= left && t[rt].y <= right) {
		t[rt].value = t[rt].lazy = value;
		return ;
	}
	Push_Down(rt);
	int mid = (t[rt].x + t[rt].y) >> 1;
	if(mid >= left) {
		Update(rt<<1,left,right,value);
	}
	if(mid < right) {
		Update(rt<<1|1,left,right,value);
	}
}

void Query(int rt) {
	if(t[rt].x == t[rt].y) {
        //printf("x : %d lazy : %d\n",t[rt].x,t[rt].lazy);
		flag[t[rt].value] = 1;
		return ;
	}
	Push_Down(rt);
	Query(rt<<1);
	Query(rt<<1|1);
}

int Bsearch(int value) {
	int left = 1;
	int right = len ;
	int mid ;
	while(left < right) {
		mid = (left + right) >> 1;
		if(b[mid] < value) {
			left = mid + 1;
		}
		else {
			right = mid;
		}
	}
	return left;
}

void Deal_with() {
	int n,T;
	scanf("%d",&T);
	while(T--) {
        memset(t,0,sizeof(t));
		scanf("%d",&n);
		cnt = 1;
		for(int i = 1 ; i <= n ; i++) {
			scanf("%d %d",li+i,ri+i);
			c[cnt++] = li[i]; c[cnt++] = ri[i];
		}
		sort(c+1,c+2*n+1);
		cnt = 1; c[0] = 0;
		for(int i = 1 ; i  <= 2 * n; i++) {
			if(c[i] == c[i-1]) continue;
			if(c[i] == c[i-1] + 1) b[cnt++] = c[i];
			else if(c[i] >= c[i-1] + 2) {
				b[cnt++] = c[i] - 1;
				b[cnt++] = c[i];
			}
		}
		len = cnt - 1;
		Build(1,len,1);
		cnt = 1;
		for(int i = 1 ; i <= n; i++) {
			int a1 = Bsearch( li[i] );
			int b1 = Bsearch( ri[i] );
			//printf("%d %d\n",a1,b1);
			Update(1,a1,b1,cnt);
			cnt ++ ;
		}
		memset(flag,0,sizeof(flag));
		Query(1);
		cnt = 0;
		for(int i = 1 ; i <= len ; i++) {
			if(flag[i]) cnt++;
		}
		printf("%d\n",cnt);
	}
}

int main(void) {
    //freopen("a.in","r",stdin);
	Deal_with();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值