Codeforces 414C Mashmokh and Reverse Operation
题意:给你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的更多相关文章
- codeforces 414C C. Mashmokh and Reverse Operation(归并排序求逆序对)
题目链接: C. Mashmokh and Reverse Operation time limit per test 4 seconds memory limit per test 512 mega ...
- codeforces 414D Mashmokh and Water Tanks
codeforces 414D Mashmokh and Water Tanks 题意 题解 \(a_i\):第 \(i\) 层的结点个数. \(b_i\):第 \(i\) 层初始有水的结点个数. 如 ...
- Codeforces 414B Mashmokh and ACM
http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...
- codeforces D.Mashmokh and ACM
题意:给你n和k,然后找出b1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n),并且对所有的bi+1%bi==0,问有多少这样的序列? 思路:dp[i][j] 表示长 ...
- 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 ...
- CodeForces 415D Mashmokh and ACM
$dp$. 记$dp[i][j]$表示已经放了$i$个数字,并且第$i$个数字放了$j$的方案数.那么$dp[i][j] = \sum\limits_{k|j}^{} {dp[i - 1][k]}$ ...
- @codeforces - 414E@ Mashmokh's Designed Problem
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵 n 个点的树,每个点的儿子是有序的. 现给定 m 次操 ...
- CodeForces - 999B Reversing Encryption
B - Reversing Encryption A string s of length n can be encrypted by the following algorithm: iterate ...
- 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 ...
随机推荐
- CentOS6.0/RedHat Server 6.4安装配置过程 详细图解!
1.准备安装 1.1 系统简介 CentOS 是什么? CentOS是一个基于Red Hat 企业级 Linux 提供的可自由使用的源代码企业级的 Linux 发行版本.每个版本的 CentOS 都会 ...
- RTFM
RTFM是一个网络语言,意思是:“去读那些他妈的手册”(Read The Fucking Manual),这句话通常用在回复那些只要查阅文件就可以解决,拿出来提问只是浪费别人时间的问题.
- centos php-fpm nginx配置
移除旧的软件包:yum remove httpd* php* 安装:yum install php php-fpm yum install php-gd php-mysql php-mbstring ...
- c++基础(二):成员he派生类
struct Date{ int day, month, year; }; struct Book{ string name; Date date; void init(string n, int y ...
- CSS3设置字体
@font-face{ font-family: myFirstFont;src: url('Sansation_Light.ttf') ,url('Sansation_Light.eot'); ...
- swift someObject == nil 如何实现
以前的编程经验告诉我们判断一个对象是否为空或者是否实例化可以这样 if(someObject == nil){ //do something }else{ } 但是在Swift中这样是不行的,然后我在 ...
- 阅读verilog程序总结
1.写程序先直接写出时钟信号 //-----------------产生串行时钟scl,为输入时钟的二分频--------- always@(negedge clk)//二分频表示频率小了,周期大了 ...
- Mysql去除重复
常用的有两种方法,第一种就是select distinct name from table.但是有时候我们要返回多个字段时就用第二种方法select *, count(distinct name) f ...
- springboot常见应用属性
# ===================================================================# COMMON SPRING BOOT PROPERTIES ...
- Ubuntu 14.04安装配置NFS服务器
(一)安装NFS服务器1.1-安装Ubuntu nfs服务器端: sudo apt-get install nfs-kernel-server 1.2-安装nfs的客户端: sudo apt-get ...