POJ 2528 Mayor's posters (线段树)

题目要你支持:

  • 区间覆盖为 \(x\)
  • 查询总区间还有多少种数

那么就是非常基础的线段树么

首先 \(l,r\) 可能很大,那么先离散化,由于答案只关心种数,那么就不需要长度上的变化

之后建一个线段树,按照常规套路进行区间覆盖即可

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 100010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }
template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
template <class T> void chmax(T &a, T b) { if (a < b) a = b ; }

pii a[N] ;
int all[N << 1] ;
int hv[N] ;
int n, m, ans, cnt ;

void init() {
	cnt = ans = 0 ; clr(hv) ;
}

struct Tree {
	int l, r, bl ;
	#define ls(x) x << 1
	#define rs(x) x << 1 | 1
	#define l(x) tr[x].l
	#define r(x) tr[x].r
	#define bl(x) tr[x].bl
} tr[N << 2] ;

void pushdown(int x) {
	if (bl(x)) {
		bl(ls(x)) = bl(rs(x)) = bl(x) ;
		bl(x) = 0 ;
	}
}

void build(int x, int l, int r) {
	l(x) = l, r(x) = r ; bl(x) = 0 ;
	if (l == r) return ;
	int mid = (l + r) >> 1 ;
	build(ls(x), l, mid) ;
	build(rs(x), mid + 1, r) ;
}

void modify(int x, int l, int r, int v) {
	if (l <= l(x) && r(x) <= r) {
		bl(x) = v ;
		return ;
	}
	pushdown(x) ;
	int mid = (l(x) + r(x)) >> 1 ;
	if (l <= mid) modify(ls(x), l, r, v) ;
	if (mid < r) modify(rs(x), l, r, v) ;
}

void query(int x) {
    if (bl(x)) {
    	hv[bl(x)] = 1 ;
    	return ;
	}
	if (l(x) == r(x)) return ;
	query(ls(x)) ; query(rs(x)) ;
}

signed main(){
	int T ; scanf("%d", &T) ;
	while (T--) {
		init() ;
		scanf("%d", &m) ;
		rep(i, 1, m) {
			scanf("%d%d", &a[i].fi, &a[i].se) ;
			all[++cnt] = a[i].fi ; all[++cnt] = a[i].se ;
		}
		sort(all + 1, all + cnt + 1) ;
		int tt = unique(all + 1, all + cnt + 1) - all ;
		rep(i, 1, m) {
			a[i].fi = lb(all + 1, all + tt + 1, a[i].fi) - all ;
			a[i].se = lb(all + 1, all + tt + 1, a[i].se) - all ;
			n = max(n, max(a[i].fi, a[i].se)) ;
		}
		build(1, 1, n) ;
		rep(i, 1, m) modify(1, a[i].fi, a[i].se, i) ;
		query(1) ;
		ans = 0 ;
		rep(i, 1, n) if (hv[i]) ans++ ;
		printf("%d\n", ans) ;
	}
	

	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/



posted @ 2019-03-14 07:27  harryhqg  阅读(127)  评论(0编辑  收藏  举报