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;
}

 

猜你喜欢

转载自www.cnblogs.com/zsben991126/p/10080432.html
0条评论
添加一条新回复