题目:
http://soj.sysu.edu.cn/show_problem.php?pid=14546&cid=
题意:
n(1~1e5)个人,每个人有三个属性值a,b,c,求出 不存在有人的所有的属性都比其高的人的个数。
思路:
将数按照属性a排序,边查询(1,num[i].b)区间属性c的最小值,边更新(1,num[i].b)区间属性c的最小值。
由于是边查询边更新,保证了得到的区间是满足属性1和属性2都小于当前属性的1,2的,只要保证这个区间中的最小属性3大于当前属性3,那么当前此人满足要求。
AC.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
struct node {
int a, b, c;
bool operator < (const node &X) const {
return a < X.a;
}
}num[maxn];
int mx[maxn<<2];
void update(int idx, int l, int r, int pos, int v)
{
if(l == r) mx[idx] = min(mx[idx], v);
else {
int mid = (r+l)>>1;
if(pos <= mid) update(idx<<1, l, mid, pos, v);
else update(idx<<1|1, mid+1, r, pos, v);
mx[idx] = min(mx[idx<<1], mx[idx<<1|1]);
}
}
int query(int idx, int l, int r, int x, int y)
{
//puts("*");
if(x <= l && y >= r) return mx[idx];
else {
int mid = (r+l) >> 1;
int ans = INF;
if(x <= mid) ans = min(ans, query(idx<<1, l, mid, x, y));
if(y > mid) ans = min(ans, query(idx<<1|1, mid+1, r, x, y));
return ans;
}
}
int main()
{
//freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d%d%d", &num[i].a, &num[i].b, &num[i].c);
}
memset(mx, 0x3f, sizeof(mx));
sort(num+1, num+n+1);
int ans = 0;
for(int i = 1; i <= n; ++i) {
int res = query(1, 1, n, 1, num[i].b);
if(res > num[i].c) {
ans++;
}
update(1, 1, n, num[i].b, num[i].c);
}
printf("%d\n", ans);
}
return 0;
}