POJ2352 star

传送门

这道题有个非常好听的名字,求二维偏序!

听起来似乎很高端,但就是让求满足对于每个i,xi < xj && yi < yj的个数。

这道题特别良心,给的顺序都是y递增,y相同x递增的,所以可以直接用树状数组维护x,当前的i答案个数就是query(x-1)。

然后如果这道题顺序是乱的呢……?也没啥问题,我们可以离线操作,先把所有的都排一遍序,之后和上面的做法就一样了。

看一下代码。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<utility>
#include<map>
#define pr pair<int,int>
#define mp make_pair
#define fi first
#define sc second
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('
')
#define lowbit(x) x & (-x)
using namespace std;
typedef long long ll;
const int M = 100005;
const int N = 32005;

int read()
{
    int ans = 0,op = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
    if(ch == '-') op = -1;
    ch = getchar();
    }
    while(ch >='0' && ch <= '9')
    {
    ans *= 10;
    ans += ch - '0';
    ch = getchar();
    }
    return ans * op;
}

int n,a[M],cnt,ans[M],x,y;

void add(int x)
{
    while(x <= N) a[x]++,x += lowbit(x);
}

int query(int x)
{
    int cur = 0;
    while(x) cur += a[x],x -= lowbit(x);
    return cur;
}

int main()
{
    n = read();
    rep(i,1,n)
    {
    x = read(),y = read(),x++;
    ans[query(x)]++;
    add(x);
    }
    rep(i,0,n-1) printf("%d
",ans[i]);
    return 0;
}
原文地址:https://www.cnblogs.com/captain1/p/9795386.html