题意:给你2^n个数,每次操作将其分成2^k份,对于每一份内部的数进行翻转,每次操作完后输出操作后的2^n个数的逆序数。

解法:2^n个数,可以联想到建立一棵二叉树的东西,比如  2,1,4,3就可以建成下面这样

[2,1,4,3]                        level 2

/               \

[2,1]              [4,3]                  level 1

/        \              /     \

[2]         [1]        [4]    [3]              level 0

然后算某个区间的逆序数的时候[2,1,4,3]实际上就是算[2,1],[4,3]的逆序数再加上[2,1],[4,3]之间的逆序数,思路和归并排序是一样的。

然后我们看下每次翻转,假如我们分成2^k份,我们会发现,对于level k+1层以上的逆序数是不会改变的,但是level k~level 0的逆序数对会翻转,我们只需要知道level k~level 0各个区间翻转后的逆序数就可以了。

所以做法就有点类似于归并求逆序数,对于每个结点,记inv[0]为不翻转时的逆序数,inv[1]为翻转的时候的逆序数,然后我们记sum[k][0]和sum[k][1] 为该层所有结点的inv[0]的和 以及inv[1]的和。 然后每次操作就是将 sum[k~0]的0,1值交换,然后逆序数就是sum[n~0][0]的和。

#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#define ll long long
#define maxn 2000000
using namespace std; ll a[maxn];
int n; ll sum[25][2]; void build(int dep,int L, int R)
{
if (dep == 0){
sum[dep][0] = sum[dep][1] = 0; return;
}
int M = (L + R) >> 1;
build(dep - 1, L, M); build(dep - 1, M + 1, R);
for (int i = L; i <= M; i++){
sum[dep][1] += a+R+1-upper_bound(a + M + 1, a + R + 1, a[i]);
sum[dep][0] += lower_bound(a + M + 1, a + R+1, a[i]) - (a + M + 1);
}
sort(a + L, a + R + 1);
}; int main()
{
while (cin >> n)
{
memset(sum, 0, sizeof(sum));
int tot = 1 << n;
for (int i = 1; i <= tot; i++){
scanf("%I64d", a + i);
}
build(n, 1, tot);
int m, q;
cin >> m;
for (int i = 0; i < m; i++){
scanf("%d", &q);
for (int j = q; j >= 0; j--){
swap(sum[j][0], sum[j][1]);
}
ll ans = 0;
for (int j = n; j >= 0; j--){
ans += (ll)sum[j][0];
}
printf("%I64d\n", ans);
}
}
return 0;
}

Codeforces 414C Mashmokh and Reverse Operation的更多相关文章

  1. codeforces 414C C. Mashmokh and Reverse Operation(归并排序求逆序对)

    题目链接: C. Mashmokh and Reverse Operation time limit per test 4 seconds memory limit per test 512 mega ...

  2. codeforces 414D Mashmokh and Water Tanks

    codeforces 414D Mashmokh and Water Tanks 题意 题解 \(a_i\):第 \(i\) 层的结点个数. \(b_i\):第 \(i\) 层初始有水的结点个数. 如 ...

  3. Codeforces 414B Mashmokh and ACM

    http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...

  4. codeforces D.Mashmokh and ACM

    题意:给你n和k,然后找出b1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n),并且对所有的bi+1%bi==0,问有多少这样的序列? 思路:dp[i][j] 表示长 ...

  5. codeforces C. Mashmokh and Numbers

    题意:给你n和k,然后让你找出n个数使得gcd(a1,a2)+gcd(a3,a4)+......的和等于k: 思路:如果n为奇数,让前n-3个数的相邻两个数都为1,n-2和n-1两个数gcd为k-an ...

  6. CodeForces 415D Mashmokh and ACM

    $dp$. 记$dp[i][j]$表示已经放了$i$个数字,并且第$i$个数字放了$j$的方案数.那么$dp[i][j] = \sum\limits_{k|j}^{}  {dp[i - 1][k]}$ ...

  7. @codeforces - 414E@ Mashmokh's Designed Problem

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵 n 个点的树,每个点的儿子是有序的. 现给定 m 次操 ...

  8. CodeForces - 999B Reversing Encryption

    B - Reversing Encryption A string s of length n can be encrypted by the following algorithm: iterate ...

  9. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造

    C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket ...

随机推荐

  1. GROUPING SETS、CUBE、ROLLUP

    其实还是写一个Demo 比较好 USE tempdb IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; G ...

  2. 刀哥多线程GCD核心概念gcd

    GCD GCD 核心概念 将任务添加到队列,并且指定执行任务的函数 任务使用 block 封装 任务的 block 没有参数也没有返回值 执行任务的函数 异步 dispatch_async 不用等待当 ...

  3. 刀哥多线程同步任务作用gcd-07-sync_task

    同步任务的作用 同步任务,可以让其他异步执行的任务,依赖某一个同步任务 例如:在用户登录之后,再异步下载文件! - (void)gcdDemo1 { dispatch_queue_t queue = ...

  4. ED/EP系列2《文件结构》

    电子存折/电子钱包应用是为持卡人进行金融交易而设计的一种应用.对于一张金融 IC 卡来说,它可以同时支持电子存折和电子钱包两种应用,也可以只支持其中的一种.卡片上两种应用的存在情况可以由应用类型标识( ...

  5. bc命令

    bc 命令:     bc 命令是用于命令行计算器. 它类似基本的计算器. 使用这个计算器可以做基本的数学运算. [tough@localhost *|bc [tough@localhost expr ...

  6. Apache+tomcat集群配置

    一.软件准备 Apache 2.2 : http://httpd.apache.org/download.cgi,下载msi安装程序,选择no ssl版本 Tomcat 6.0 : http://to ...

  7. DataReader 链接关闭解惑篇

    不管是啥xxDataReader,都是继承DataReader实现的,所以是有共性的,因此标题就以DataReader为题了. 情况一:DataReader 默认链接不关闭 static void M ...

  8. ios中用drawRect方法绘图的时候设置颜色

    设置画笔颜色可以直接 [[UIColor grayColor] set];就可以设置颜色.

  9. 如何查看hadoop与hbase的版本匹配关系

    官网:http://hbase.apache.org/book.html 搜索:Hadoop version support matrix 下面有一个二维的支持关系表.

  10. P1699: [Usaco2007 Jan]Balanced Lineup排队

    很明显是一道RMQ问题,倍增法,维护一下区域的最大/小值就行了. var n,i,j,q,f,t,times:longint; hmin,hmax:array[..,..] of longint; f ...