感觉这题也很容易啊,因为我对树状数组很不熟悉,然后还是老问题,没有逆向思考,如果从小往大扫,就不用考虑去掉的问题了.
还有个事情,这题会出现负数,但这个负数,不是溢出导致的负数,是”正确”的负数,他们只是在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;
}