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 ...
随机推荐
- SQLserver查询数据库所有字段-表名
SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT (case when a.colorder=1 t ...
- arcgis中注记的高级处理
在实际制图过程中,常遇到一个图层的多个要素展示,将标注转换为注记的之后,可以对注记进行移动调整位置等手动更改.这样为避免重复地移动注记位置.可以采用如下方案: 1.将图层标注转换为注记 2.将注记存储 ...
- memcached 简介
最近,想看看开源的东西,正好在网上看到了memcached这个服务器,就简单学了学.做个笔记! 1.memcached 介绍 memcached我原本以为是一款数据库软件,但详细了解才发现,准确的是一 ...
- Linux下编译内核配置选项简介
Code maturity level options代码成熟度选项 Prompt for development and/or incomplete code/drivers 显示尚在开发中或尚未完 ...
- Android WIFI 启动流程(TIP^^)
前几天因为解决一堆Bug,没时间写.我不会每天都写,就是为了存档一些资料. 内容来源:工作中接触到的+高手博客+文档(Books)=自己理解 仅限参考^^ 此博客是上一个<<Android ...
- 最简单的耗时组件(窗口activity里面放一个progressBar)
①.先定义一个activity package com.example.administrator.actionbardemo; import android.app.Activity; import ...
- [译]rabbitmq 2.1 Consumers and producers (not an economics lesson)
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. For now, all you need to know is that producers create messag ...
- php5调用web service (笔者测试成功)
转自:http://www.cnblogs.com/smallmuda/archive/2010/10/12/1848700.html 感谢作者分享 工作中需要用php调用web service接口, ...
- [quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming
[quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming http://blog.pi3g.com/2013/08/ffmp ...
- DataTemplate 以及Template Selector 学习笔记
1. 内容控件通过ContentTemplate,列表控件通过itemTemplate 来支持模板绑定.例子如下: 模板定义: xaml: <DataTemplate x:Key="R ...