1. 程式人生 > >poj3067 二維偏序樹狀陣列

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=0; while(x){ res+=bit[x]; x-=x&-x; } return res; } int main(){ int t,tt; scanf(
"%d",&t); for(int tt=1;tt<=t;tt++){ memset(bit,0,sizeof bit); scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=k;i++){ scanf("%d%d",&edge[i].u,&edge[i].v); } sort(edge+1,edge+1+n); ll ans=0;
for(int i=1;i<=k;i++){ ans+=query(edge[i].u-1); add(edge[i].u); } printf("Test case %d: %lld\n",tt,ans); } return 0; }

ac程式碼

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