codeforces 1269E K Integers (binary tree arrays +)

Link: https: //codeforces.com/contest/1269/problem/E

Question is intended: to a sequence of P1, P2, P3, P4 .... Pi, each two adjacent elements can be exchanged, the exchange performing a minimum number of moves, such that the presence of a last sub-segment 1,2, ..., k this is f (k) as defined by the subject, the subject of the request to obtain all the f (n), and sequentially outputs.

Ideas: First consider the problem of reverse, such as the 3214 series, so that it changes to 1234, the minimum number of movements of this sequence is the reverse of the sum, 1 + 2 = 3, is the reverse of (3 , 2) (3,1) (2,1), but such sequences 352,167,489, seeking f (4) how to do? The first is not the sequence 1 2 3 4 Poly-together, connected together, again calculated on the number of reverse, the process takes two sum is the answer. Then the subject was divided into two processes, a polymerization with n digits. 2. seeking to reverse the number, adding both cost line. The first one makes the polymerization process if the minimum number of steps it? In fact, most of the obtained polymerization intermediate position, all other figures to a position near the number of movements takes the least, this process can be divided to two. The second process can be used Fenwick tree, you can also do with a tree line. When recording input each digit positions, two built Fenwick tree, a tree-like array maintenance times the number that appears, to find the number of reverse order, individual numbers other Fenwick tree maintenance in position of the original sequence.

AC Code:

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<algorithm>
 7 #include<cmath>
 8 using namespace std;
 9 typedef long long ll;
10 ll mod = 1e9+7;
11 const int maxn = 2e5+10;
12 ll t[maxn],cnt[maxn]; 
13 ll pos[maxn];
14 int n;
15inline int lowbit (LL x) {
 16      return x & (- x);
 . 17      /// calculated from right to left of x binary 1 appears first, and the corresponding decimal binary number that after a number of constituent 0 
18  }
 . 19  void the Add (LL * B, int X, int K) { // single-point modification 
20 is    the while (X <= n-) {   // can not be out of range 
21 is      B [X] = B [X] + K;
 22 is      X = + X lowbit (X);
 23 is    }
 24  }
 25 LL GetSum (LL * B, int X) {   // a [. 1] ...... a [X] and 
26 is    LL = ANS0;
27   while (x > 0) {
28     ans = ans + b[x];
29     x = x - lowbit(x);
30   }
31   return ans;
32 }
33 int main(){
34     ios::sync_with_stdio(false);
35     cin.tie(0);
36     cin>>n;
37     for(int i = 1;i<=n;i++){
38         int t;
39         cin>>t;
40         pos[t] = i;
41     }
42     ll inv = 0;
43     for(int i = 1;i<=n;i++){
44         inv += (i-1-getsum(t,pos[i]));
45         add(t,pos[i],1);
46         add(cnt,pos[i],pos[i]);
47         if(i==1){
48             cout<<0<<" ";
49             continue;
50         }
51         int mid,l = 1,r = n;
52         while(l<=r){
53             mid = (l+r)>>1;
54             if(getsum(t,mid)*2<=i){
55                 l = mid+1;
56             }
57             else{
58                 r = mid-1;
59             }
60         }    
61         ll ans = 0;
62         ll cntL = getsum(t,mid);
63         ll cntR = i - cntL;
64         ll indexL = getsum(cnt,mid);
65         ll indexR = getsum(cnt,n)-indexL;
66         ans+=((mid+mid-cntL+1)*cntL)/2-indexL;
67         ans+=(indexR-((mid+1+(mid+cntR))*cntR)/2);
68         cout<<ans+inv<<" ";
69     }
70     return 0;
71 }

Guess you like

Origin www.cnblogs.com/AaronChang/p/12147535.html