eoj 3328 时空交织的代价(树状数组)

41 篇文章 0 订阅
37 篇文章 0 订阅

感觉这题也很容易啊,因为我对树状数组很不熟悉,然后还是老问题,没有逆向思考,如果从小往大扫,就不用考虑去掉的问题了.
还有个事情,这题会出现负数,但这个负数,不是溢出导致的负数,是”正确”的负数,他们只是在mod中,出现的而已,所以我们最后要%,+mod再mod.

/*  xzppp  */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson MAXN,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
typedef long long  LL;
typedef unsigned long long ULL;
const int MAXN = 2*1e5+17;
const int MAXM = 20;
const int INF = 0x7fffffff;
const int MOD = 1e9+7;
LL bit[2][MAXN],n=0;
LL sum(int k,int x)
{
    LL ret = 0;
    while(x)
    {
        ret = (ret + bit[k][x])%MOD;
        x -= x&-x;
    }
    return ret;
}
void add(int k,int x,int c)
{
    while(x<=n)
    {
        bit[k][x] = (bit[k][x]+c)%MOD;
        x += x&-x;
    }
}
struct dot
{
    LL x,p,id;
    bool operator<(const dot & b) const
    {
        return p<b.p;
    }
}all[MAXN];
bool cmp(dot a,dot b)
{
    return a.x<b.x;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE 
    FFF
    #endif
    int t;
    cin>>t;
    while(t--){
        memset(bit, 0, sizeof(bit));
        cin>>n;
    for (int i = 0; i < n; ++i)
        scanf("%lld",&all[i].x);
    for (int i = 0; i < n; ++i)
        scanf("%lld",&all[i].p);
    sort(all, all+n,cmp);
    for (int i = 0; i < n; ++i)
        all[i].id = i+1;
    sort(all, all+n);
    LL ans = 0;
    for (int i = 0; i < n; ++i)
    {
        int plc = all[i].id;
        LL s1 = sum(1,plc-1),s2 = sum(1,n)-sum(1,plc);
        LL c1 = sum(0,plc-1),c2 = sum(0,n)-sum(0,plc);
        LL temp = (all[i].p*(((c1-c2)*all[i].x)+s2-s1)%MOD)%MOD;
        cout<<ans<<"  "<<temp<<endl;
        ans = (ans + temp) % MOD;
        if(temp<0)
            cout<<((c1-c2)*all[i].x)<<"  "<<(s2-s1)<<endl;
        cout<<ans<<endl<<endl;
        add(0,plc,1);
        add(1,plc,all[i].x);
    }
    cout<<(ans%MOD+MOD)%MOD<<endl;
}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值