【线段树】poj 2528 Mayor's posters

需进行离散化,从后往前进行覆盖扫描。

#include <vector>
#include <list>
#include <limits.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <stdlib.h>
using namespace std;

struct Node{
	int l, r;
	Node *leftChild, *rightChild;
	// Indicate whether the whole segment has been covered
	bool covered;
};
Node node[50000];
int hash[10000010];
int array[20005];
int ptr, n;

int getMiddle(int l, int r){
	return (l+r)/2;
}

void buildTree(Node *root, int left, int right){
	root->l = left;
	root->r = right;
	root->covered = false;
	if(left == right)
		return;
	ptr++;
	root->leftChild = node+ptr;
	buildTree(root->leftChild, left, getMiddle(left, right));
	ptr++;
	root->rightChild = node+ptr;
	buildTree(root->rightChild, getMiddle(left, right)+1, right);
	return;
}

bool query(Node *root, int left, int right){	
	if(root->covered == true)
		return false;
	if((root->l)==left && (root->r)==right){
		root->covered = true;
		return true;
	}
	bool tmp;
	if(right <= getMiddle(root->l, root->r))
		tmp = query(root->leftChild, left, right);
	else if(left > getMiddle(root->l, root->r))
		tmp = query(root->rightChild, left, right);
	else{
		bool tmp1 = query(root->leftChild, left, getMiddle(root->l, root->r));
		bool tmp2 = query(root->rightChild, getMiddle(root->l, root->r)+1, right);
		tmp = tmp1 || tmp2;
	}
	if((root->leftChild->covered)==true && (root->rightChild->covered)==true)
		root->covered = true;
	return tmp;
}
int main(){	
	int t;
	pair<int, int> post[10005];
	scanf("%d", &t);
	while(t--){
		ptr = 0;
		int index = 0;
		scanf("%d", &n);
		for(int i=0; i<n; i++){
			scanf("%d", array+index);			
			index++;
			scanf("%d", array+index);
			index++;
			post[i] = make_pair(array[index-2], array[index-1]);
		}
		sort(array, array+index);
		// Eliminate same elements
		int length = unique(array, array+index)-array;
		//cout<<"length: "<<length<<endl;
		// Process of hashing
		for(int i=0; i<length; i++)
			hash[array[i]] = i;
		buildTree(node, 0, length-1);
		int res = 0;
		for(int i=n-1; i>=0; i--){
			//cout<<hash[post[i].first]<<" "<<hash[post[i].second]<<endl;  
			if(query(node, hash[post[i].first], hash[post[i].second]) == true)
				res++;
		}
		printf("%d\n", res);
	}
	//system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值