Advertisement
Guest User

Untitled

a guest
Dec 24th, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int MXV = 1000004;
  6.  
  7. struct NODE{
  8.     int subsets , ans , lazy;
  9.     NODE(){
  10.         subsets = ans = 0;
  11.         lazy = 1;
  12.     }
  13.     NODE(int sub , int an){
  14.         subsets = sub;
  15.         ans = an;
  16.         lazy = 1;
  17.     }
  18. }tree[MXV * 4];
  19.  
  20. const int MX = (1<<18) , MOD = 1000000007;
  21.  
  22. vector < int > used;
  23.  
  24.  
  25. int T , n;
  26.  
  27. pair < int , int > arr[MX];
  28.  
  29.  
  30. int where , toadd;
  31.  
  32.  
  33. int st , en , maxVal = 1000000;
  34.  
  35.  
  36. void mul(int x , int coef){
  37.     tree[x].ans = (1ll * tree[x].ans * coef)%MOD;
  38.     tree[x].subsets = (1ll * tree[x].subsets * coef)%MOD;
  39.     tree[x].lazy = (1ll * tree[x].lazy * coef)%MOD;
  40. }
  41. void push(int x){
  42.     if(tree[x].lazy== 1) return;
  43.     mul(x*2 , tree[x].lazy);
  44.     mul(x*2+1 , tree[x].lazy);
  45.     tree[x].lazy = 1;
  46. }
  47. void update(int x , int a , int b){
  48.     used.push_back(x);
  49.     if(st > b || en < a) return;
  50.     if(a >= st && b <= en){
  51.         mul(x , 2);
  52.         return;
  53.     }
  54.     push(x);
  55.     update(x*2 , a , (a+b)/2);
  56.     update(x*2+1 , (a+b)/2+1 , b);
  57.     tree[x].ans = tree[x*2].ans + tree[x*2+1].ans; tree[x].ans %= MOD;
  58.     tree[x].subsets = tree[x*2].subsets + tree[x*2+1].subsets; tree[x].subsets %= MOD;
  59. }
  60.  
  61. NODE query(int x , int a , int b){
  62.     if(st > b || en < a) return NODE();
  63.     if(a >= st && b <= en) return tree[x];
  64.     push(x);
  65.     auto L = query(x*2 , a , (a+b)/2) , R = query(x*2+1 , (a+b)/2+1 , b);
  66.     return NODE((L.subsets + R.subsets)%MOD , (L.ans + R.ans)%MOD);
  67.  
  68. }
  69.  
  70. void add(int x , int a , int b){
  71.     if(where > b || where < a) return;
  72.     used.push_back(x);
  73.     if(a == b){
  74.         tree[x].subsets += toadd; tree[x].subsets %= MOD;
  75.         tree[x].ans = (1ll * tree[x].subsets * a)%MOD;
  76.         return;
  77.     }
  78.     push(x);
  79.     if(where <= (a+b)/2) add(x*2 , a , (a+b)/2);
  80.     else add(x*2+1 , (a+b)/2+1 , b);
  81.     tree[x].ans = tree[x*2].ans + tree[x*2+1].ans; tree[x].ans %= MOD;
  82.     tree[x].subsets = tree[x*2].subsets + tree[x*2+1].subsets; tree[x].subsets %= MOD;
  83. }
  84. int main(int argc, char** argv){
  85.  
  86.     //freopen("in.in","r",stdin);
  87.  
  88.     scanf("%d",&T);
  89.  
  90.     while(T--){
  91.  
  92.         scanf("%d",&n);
  93.  
  94.         for(int j = 1 ; j <= n ; j++) scanf("%d",&arr[j].first);
  95.         for(int j = 1 ; j <= n ; j++) scanf("%d",&arr[j].second);
  96.  
  97.         sort(arr+1 , arr+1+n);
  98.  
  99.         long long tot = 0;
  100.  
  101.         for(int j = 1 ; j <= n ; j++){
  102.  
  103.             int V = arr[j].second;
  104.  
  105.             st = V + 1 , en = maxVal;
  106.             if(st <= en) tot += (1ll * arr[j].first * query(1,1,maxVal).ans); tot %= MOD;
  107.             if(st <= en) update(1,1,maxVal);
  108.  
  109.             st = 1 , en = V;
  110.             if(st <= en) toadd = query(1,1,maxVal).subsets;
  111.             ++toadd; toadd%=MOD;
  112.             tot += (1ll * ((1ll * arr[j].first * arr[j].second)%MOD) * toadd)%MOD; tot %= MOD;
  113.             where = V;
  114.             add(1,1,maxVal);
  115.         }
  116.  
  117.         cout<<tot<<endl;
  118.  
  119.         for(auto pp : used)
  120.             tree[pp] = NODE();
  121.         used.clear();
  122.     }
  123.  
  124.  
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement