The left pre-set into a size of 1 ~ i-1 when all elements are moved to the cost of the right set, denoted as sum [i].
Enumeration size then the final state of the left header, the updated cost of element i in left / left set to move.
Fenwick tree / tree line processing interval modification / range queries
1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 #define ll long long 5 const int N=2e5+7; 6 struct Tree{ 7 ll minn,lazy; 8 }tree[N<<2]; 9 ll sum[N];//前缀和 10 inline void build(int root,int l,int r){ 11 if(l==r){ 12 tree[root].minn=sum[l];//1~l的a[i]之和 13 tree[root].lazy=0; 14 return; 15 } 16 int mid=(l+r)>>1; 17 build((root<<1),l,mid); 18 build((root<<1|1),mid+1,r); 19 tree[root].minn=min(tree[(root<<1)].minn,tree[(root<<1|1)].minn);//up 20 return; 21 } 22 inline void pushdown(int root){ 23 if(!tree[root].lazy) 24 return; 25 tree[(root<<1)].minn+=tree[root].lazy; 26 tree[(root<<1|1)].minn+=tree[root].lazy; 27 tree[(root<<1)].lazy+=tree[root].lazy; 28 tree[(root<<1|1)].lazy+=tree[root].lazy; 29 tree[root].lazy=0; 30 return; 31 } 32 inline void change(int root,int l,int r,int x,int y,int val){ 33 if(r<x||l>y) 34 return; 35 if(x<=l&&r<=y){ 36 tree[root].minn+=val; 37 tree[root].lazy+=val; 38 return; 39 } 40 int mid=(l+r)>>1; 41 pushdown(root); 42 change((root<<1),l,mid,x,y,val); 43 change((root<<1|1),mid+1,r,x,y,val); 44 tree[root].minn=min(tree[(root<<1)].minn,tree[(root<<1|1)].minn);//up 45 return; 46 } 47 int n,p[N],a[N],pos[N]; 48 ll ans; 49 int main(){ 50 ios::sync_with_stdio(false); 51 cin.tie(NULL); 52 cout.tie (NULL); 53 is CIN >> n-; 54 is for ( int I = . 1 ; I <= n-; ++ I) { 55 CIN >> P [I]; 56 is POS [P [I]] = I ; // location of the digital p [i] appears to I 57 is } 58 for ( int I = . 1 ; I <= n-; ++ I) { 59 CIN >> a [I]; 60 SUM [I] = SUM [ I- . 1 ] + a [I]; // SUM [I] to the left of the collection size is i, the set of all elements left to move to the right collection takes 61 is } 62 is Build ( . 1 , . 1, N- . 1 ); 63 is ANS = min (A [ . 1 ], A [n-]); // A [. 1] to the left is empty, [n] a blank for the right set 64 for ( int I = . 1 ; I <n-; I ++) { // enumeration left set size, the size of the set, the collection of elements are also designated as I ~. 1 65 Change ( . 1 , . 1 , N- . 1 , . 1 , POS [I] - . 1 ;, a [POS [i]]) // consideration to find the location of the occurrence of element i, the position of the left sum [i], respectively, it appears in plus the element i from right to left set of mobile collection (the sum of the original [i-1 - 1] is originally in position 1 to i-1 elements are moved to the right, this time together with the cost of the elements 1 to i move from right to left) 66 Change ( 1 , 1 , N- 1 , POS [I], n-, -a [POS [I]]); //In SUM [i], and it appears to the right of the position of the element i subtracting the cost to the right from the left set of mobile set (without moving element i, but has previously been added to the cost of moving sum [i ~ n] Lane) 67 ANS = min (ANS, Tree [ . 1 ] .minn); // if left set the minimum size at the expense of i is updated minimum 68 } 69 COUT << ANS; 70 return 0 ; 71 is }