Codeforces 414C c. Mashmokh and Reverse operation (merge sort to reverse order)

Source: Internet
Author: User

Topic Links:

C. Mashmokh and Reverse operation

Time limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Mashmokh ' s boss, Bimokh, didn ' t like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh ' s team. In order to join he is given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer in all. So he wasn ' t able to solve them. That's why he asked the him with these tasks. One of these tasks is the following.

You have an array a of length 2n and m queries on it. The i-th query is described by an integer qi. In order to perform the i-th query you must:

  • Split the array into2N-Q i  parts, where each part is a subarray consisting of  2 q i  numbers; The  J -th subarray  (1≤ J ≤2 n - q i )  should contain the elements < Span class= "Tex-span" > a [( J -1) 2 q I + 1], a [( J -1) 2 q I + 2], ..., a [( J -1) 2 q I + 2 q i ];
  • Reverse each of the subarrays;
  • Join them into a single array in the same order (this array becomes new array a);
  • Output the number of inversions in the new a.

Given Initial array a and all the queries. Answer all the queries. Please, note that the changes from some query are saved for further queries.

Input

The first line of input contains a single integer n (0≤ n ≤20).

The second line of input contains 2n space-separated integers a[1], a[2], ..., A[2n] (1≤ a[i]≤109), the initial array.

The third line of input contains a single integer m (1≤ m ≤106).

The fourth line of input contains m space-separated integers q1, q2 , ..., qm (0≤ qin), the queries.

Note:since the size of the input and output could is very large, and don ' t use slow output techniques in your language. For example, does not use the input and output streams (CIN, cout) in C + +.

Output

Output m lines. In the i-th line print the answer (the number of inversions) for the i-th query.

ExamplesInput
2
2 1 4 3
4
1 2 0 2
Output
0
6
6
0
Input
1
1 2
3
0 1 1
Output
0
1
0
Note

If we reverse an array x[1], x[2], ..., x[n] It becomes new array y[1 ], y[2], ..., y[n], where y[i] = x[N - I + 1] For each i.

The number of inversions of an array x[1], x[2], ..., x[n] is the number of pairs of indices i, J such that: I < J and x[ C31>i] > x[J].

Test Instructions :

Ask 2^n number of title operation after each operation in reverse order is how much;

Ideas :

Like merge sort First Division, and then find out each division inside the reverse order, merge and then ask two interval between the reverse order, reverse reverse to the number of Hoshe pairs of exchange;

Recently copied code copy of the bad, eh;

AC code :

#include <bits/stdc++.h>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong LongLL;ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=0x3f3f3f3f;Const intn=1e6+5e5; LL sum[ A][2];intn,a[1<< +];voidDfsintLintRintDeep ) {    if(L&GT;=R)return ; intMid= (l+r) >>1; DFS (L,mid,deep-1); DFS (Mid+1, r,deep-1);  for(inti=l;i<=mid;i++)    {        intTemp=lower_bound (a+mid+1, a+r+1, A[i])-(a+mid+1); sum[deep][0]+=(LL) temp; Temp=r-mid-(Upper_bound (a+mid+1, a+r+1, A[i])-(a+mid+1)); sum[deep][1]+=(LL) temp; } sort (A+l,a+r+1);}intMain () {scanf ("%d",&N); intY= (1<<N); Riep (y) scanf ("%d", A +i); DFS (1, Y,n); intq,x; scanf ("%d",&q);  while(q--) {scanf ("%d",&x);  while(x) {swap (sum[x][0],sum[x][1]); X--; } LL ans=0; Riep (n) Ans+=sum[i][0]; printf ("%i64d\n", ans); }    return 0;}

Codeforces 414C c. Mashmokh and Reverse operation (merge sort to reverse order)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.