zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",T,ans);
        }
        return 0;
    }
  • 相关阅读:
    ANDROIDSTUDIO手动安装插件
    xcode 升级到最新的11.1版本打开项目卡顿解决方案
    OC各种数据类型之间的转换方法
    TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量
    在linux下makefile的使用
    Binary Search二分法搜索C++程序
    ORA-01654错误
    合作版状态模式之设计
    基于FPGA的超声波测距(一)
    如何随机获取数据库不连续ID的数据?
  • 原文地址:https://www.cnblogs.com/zsben991126/p/10080432.html
Copyright © 2011-2022 走看看