做题记录整理分治1 二维偏序(2022/9/15)

Stars
我做过的最难的题,因为全是英文
二维偏序,本质上其实就是反过来的逆序对————顺序对

特地画了几个图因为偏序其实本质是cdq分治

#include<iostream>
#include<stdio.h>
#include<algorithm>
#define for1(i,a,b) for(int i = a;i<=b;i++)
#define ll long long
#define mp(a,b) make_pair(a,b)
using namespace std;
struct node{
	int x;
	int y;
}a[5000005];
int s[5000005],n,ans[5000005],m;
bool cmp(node x,node y)
{
	return x.x<y.x;
}

int lb(int x)
{
	return x&-x;
}
void xg(int x,int k)
{
	while(x<=32005)
	{
		s[x]+=k;
		x+=lb(x);
	}
}

int cx(int x)
{
	int ans=0;
	while(x)
	{
		ans+=s[x];
		x-=lb(x);
	}
	return ans;
}
int main()
{
	cin>>n;
	for1(i,1,n)
		scanf("%d%d",&a[i].x,&a[i].y);
	for1(i,1,n)
	{
		xg(a[i].x+1,1);
		ans[cx(a[i].x+1)]++;
	}
	for1(i,1,n)
	printf("%d\n",ans[i]);
    return 0;
}
posted @ 2022-09-15 17:29  yyx525jia  阅读(36)  评论(0)    收藏  举报