poj3067 二维偏序树状数组

时间:2023-03-10 01:23:34
poj3067 二维偏序树状数组

题解是直接对一维升序排列,然后计算有树状数组中比二维小的点即可

但是对二维降序排列为什么不信呢??

/*

*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 1010
#define ll long long
int n,m,k;
ll bit[maxn*maxn];
struct Edge{
int u,v,id;
bool operator<(const Edge & a) const{
return v>a.v;
}
}edge[maxn*maxn];
void add(int x){
while(x<=k){
bit[x]++;
x+=x&-x;
}
}
ll query(int x){
ll res=;
while(x){
res+=bit[x];
x-=x&-x;
}
return res;
}
int main(){
int t,tt;
scanf("%d",&t);
for(int tt=;tt<=t;tt++){
memset(bit,,sizeof bit);
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++){
scanf("%d%d",&edge[i].u,&edge[i].v);
} sort(edge+,edge++n);
ll ans=; for(int i=;i<=k;i++){
ans+=query(edge[i].u-);
add(edge[i].u);
} printf("Test case %d: %lld\n",tt,ans);
}
return ;
}

ac代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
const int M=;
LL C[M];
int n,m,k;
struct Node
{
int x,y;
}edge[*];
bool cmp(Node a,Node b)
{
if(a.x==b.x)return a.y<=b.y;
else return a.x<b.x;
}
int lowbit(int a)
{
return a&(-a);
}
void Modify(int p,int c)
{
for(int i=p;i<=m;i+=lowbit(i))
C[i]+=c;
}
int getsum(int p)
{
LL ans=;
for(int i=p;i>;i-=lowbit(i))
{
ans+=C[i];
}
return ans;
}
int main()
{
int T=;
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<k;i++)
scanf("%d%d",&edge[i].x,&edge[i].y);
memset(C,,sizeof(C));
sort(edge,edge+k,cmp);
LL ans=;
for(int i=;i<k;i++)
{
Modify(edge[i].y,);
ans+=(getsum(m)-getsum(edge[i].y));
}
T++;
printf("Test case %d: %lld\n",T,ans);
}
return ;
}