标签:des style http os io for ar art
Description
Input
Output

Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
离散化加线段树区间更新和查询。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <iostream>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef long long LL;
const int MAX = 200000000;
const int maxn = 10010;
int t, n, a, b, c, ans;
int fu[maxn<<4], cnt[maxn<<4], vis[maxn<<2];
struct C{
int x, y;
} in[maxn];
int bs(int v, int x, int y) {
int m;
while(x < y) {
m = (x+y) >> 1;
if(fu[m] >= v) y = m;
else x = m+1;
}
return x;
}
void down(int o) {
if(cnt[o] != -1) {
cnt[o<<1] = cnt[o<<1|1] = cnt[o];
cnt[o] = -1;
}
}
void update(int o, int l, int r) {
if(a <= l && r <= b) {
cnt[o] = c;
return;
}
down(o);
int m = (l+r) >> 1;
if(a <= m) update(lson);
if(m < b ) update(rson);
}
void query(int o, int l, int r) {
if(cnt[o] != -1) {
if(!vis[ cnt[o] ]) {
ans ++;
vis[ cnt[o] ] = 1;
}
return ;
}
if(l == r) return;
int m = (l+r) >> 1;
query(lson);
query(rson);
}
int main()
{
cin >> t;
while(t--) {
cin >> n;
int k = 0;
for(int i = 0; i < n; i++) {
scanf("%d%d", &in[i].x, &in[i].y);
fu[k++] = in[i].x;
fu[k++] = in[i].y;
}
sort(fu, fu+k);
int m = 1;
for(int i = 1; i < k; i++)
if(fu[i] != fu[i-1]) fu[m++] = fu[i];
for(int i = m-1; i >= 1; i--)
if(fu[i] != fu[i-1] + 1) fu[m++] = fu[i-1]+1;
sort(fu, fu+m);
memset(cnt, -1, sizeof(cnt));
for(int i = 0; i < n; i++) {
a = bs(in[i].x, 0, m-1);
b = bs(in[i].y, 0, m-1);
c = i;
update(1, 0, m-1);
}
memset(vis, 0, sizeof(vis));
ans = 0;
query(1, 0, m-1);
printf("%d\n", ans);
}
return 0;
}
POJ 2528 Mayor's posters (离散化+线段树区间更新),布布扣,bubuko.com
POJ 2528 Mayor's posters (离散化+线段树区间更新)
标签:des style http os io for ar art
原文地址:http://blog.csdn.net/u013923947/article/details/38612427