zoukankan      html  css  js  c++  java
  • 高级数据结构第六章C . 奶牛狂欢节(两个树状数组)

    link

    思路:

    良心题解
    首先,(O(n^{2}))的算法并不难想,也很容易就想到先排序后对每个奶牛都计算贡献。这样会有一个问题就是计算(dis)时要取绝对值,但是排序后每只奶牛跟前面奶牛的(x)坐标的大小关系是不确定的,所以计算贡献的方法也是不一样的,如何合并起来算的话是不正确的。
    考虑怎么解决这个问题:
    假设当前在计算第(i)只奶牛的贡献,那么前面的奶牛下标为(j)的可以分为两类:
    (1. j<i,x_{j}<x_{i})
    (2.j < i , x_{j} > x_{i})
    我们只统计第一类的信息,假设第一类的奶牛个数为(t1),那么第二类的奶牛个数为(i-t1-1).为了计算式子,还需要记录前面奶牛的坐标之和,用两个树状数组维护。

    代码:

    //#pragma GCC optimize(2)
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll>PLL;
    typedef pair<int, int>PII;
    typedef pair<double, double>PDD;
    #define I_int ll
    inline ll read()
    {
        ll x = 0, f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-')f = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    #define read read()
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i<(b);i++)
    #define per(i,a,b) for(int i=(a);i>=(b);i--)
    #define perr(i,a,b) for(int i=(a);i>(b);i--)
    ll ksm(ll a, ll b, ll p)
    {
        ll res = 1;
        while(b)
        {
            if(b & 1)res = res * a % p;
            a = a * a % p;
            b >>= 1;
        }
        return res;
    }
    const int inf = 0x3f3f3f3f;
    #define PI acos(-1)
    
    const int maxn=5e4+10;
    
    struct BIT{
        ll n,tr[maxn];
    
        void init(){
            memset(tr,0,sizeof tr);
        }
        ll lowbit(ll x){
            return x&-x;
        }
        void update(ll pos,ll val){
            while(pos<=n){
                tr[pos]+=val;pos+=lowbit(pos);
            }
        }
        ll qask(ll pos){
            ll res=0;
            while(pos){
                res+=tr[pos];pos-=lowbit(pos);
            }
            return res;
        }
    
    };
    
    struct node{
        ll v,x;
    }a[maxn];
    
    bool cmp(node a,node b){
        return a.v<b.v;
    }
    
    ll n;
    
    int main()
    {
        n=read;
        ll m=0;
        rep(i,1,n){
            a[i].v=read,a[i].x=read;
            m=max(m,a[i].x);
        }
        sort(a+1,a+1+n,cmp);
        BIT cnt,sum;
        cnt.n=m,sum.n=m;
        cnt.init();sum.init();
        ll res=0,ans=0;
        rep(i,1,n){
            ll t1=cnt.qask(a[i].x),t2=sum.qask(a[i].x);
            res=res+(t1*a[i].x-t2)*a[i].v;
            ll t3=i-t1-1,t4=ans-t2;
            res=res+(t4-t3*a[i].x)*a[i].v;
            cnt.update(a[i].x,1);sum.update(a[i].x,a[i].x);
            ans+=a[i].x;
        }
        printf("%lld
    ",res);
        return 0;
    }
    
    /*
    
    **/
    
    
    
  • 相关阅读:
    Python的map、filter、reduce函数
    C/C++中extern关键字详解
    python中的多继承
    用python爬虫抓站的一些技巧总结
    python中的OO
    互斥量、条件变量与pthread_cond_wait()函数的使用,详解
    C/C++ struct位结构(位域)
    VS2008无法启动asp.net提示“无法启动程序: http://localhost/.../test.aspx” 数据无效”。
    昨晚比较开心,QQ2009 sp6的0x00dd发送出去的包终于搞明白了
    CRC32 CRC16 校验算法 C# 代码
  • 原文地址:https://www.cnblogs.com/OvOq/p/14883075.html
Copyright © 2011-2022 走看看