F. Moving Points-(离散化+树状数组)

总结

当时比赛题没读懂,两点之间得关系没弄透彻,只知道如何求最小值。暴力肯定不可行,n2
后面看了一下别人的写法,终于遇到一个写题解的人了,写博客不写题解,哪怕一点也行,废话我都乐意看,直接贴代码的博主,就很难受,啥也学不到。

题解

第一步:了解d(i,j)之间的关系
情况1:i和j再也相遇,dis=abs(xi-xj)
情况2:i和j一定会相遇,dis=0;

第二步:离散化vi
vi的值的多少我们步关系,只关系大小之间关系。这样方便我们知道比vi小于等于个数。

第三步:建立两个树状数组维护区间和
情况1的充要条件xi<=xj,vi<=vj
当遍历到pos位置时,我们就应该把所有xi<=`xpos并且vi<=vpos,所有加起来
ans+ = xpos * 满足的个数-pos位置之前的所有xi之和

题目链接

/*
                ____________    ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    __________
          /  /\     /  /\             /  /\  /_______  /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A CODE OF CBOY

*/
#include<bits/stdc++.h>
//typedef long long ll;
//#define ull       unsigned long long
#define int       long long
#define F           first
#define S           second
#define endl        "\n"//<<flush
#define lowbit(x)   (x&(-x))
#define pa          pair<int,int>
#define ferma(a,b)  pow(a,b-2)
#define pb          push_back
#define all(x)      x.begin(),x.end()
#define memset(a,b) memset(a,b,sizeof(a));
#define IOS         ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int MAXN=0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
void file()
{
#ifdef ONLINE_JUDGE
#else
    freopen("cin.txt","r",stdin);
    //  freopen("cout.txt","w",stdout);
#endif
}
struct BIT
{
    int n;
    vector<int>vec;
    BIT(int n=0)
    {
        init(n);
    }
    void init(int len)
    {
        n=len+1;
        vec.resize(n,0);
    }
    int pre(int x)
    {
        int sum=0;
        while(x)
            sum+=vec[x],x-=lowbit(x);
        return sum;
    }
    void add(int pos,int v)
    {
        while(pos<n)
            vec[pos]+=v,pos+=lowbit(pos);
    }
};
signed main()
{
    IOS;
    //file();
    int n;
    cin>>n;
    vector<pa>vec(n);
    vector<int>a;
    for(auto &it:vec)
        cin>>it.F;
    for(auto &it:vec)
        cin>>it.S,a.pb(it.S);
    sort(all(a));
    sort(all(vec));
    a.erase(unique(all(a)),a.end());
    int ans=0;
    BIT a1(n),a2(n);
    for(int i=0;i<n;i++)
    {
        int id=lower_bound(all(a),vec[i].S)-a.begin()+1;
        ans+=vec[i].F*a1.pre(id)-a2.pre(id);
        a1.add(id,1);
        a2.add(id,vec[i].F);
    }
    cout<<ans<<endl;
    return 0;
}

发布了149 篇原创文章 · 获赞 5 · 访问量 6907

猜你喜欢

转载自blog.csdn.net/weixin_44224825/article/details/104504310