【BZOJ】3289: Mato的文件管理(莫队算法+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3289
很裸的莫队。。。
离线了区间然后分块排序后,询问时搞搞就行了。
本题中,如果知道$[l, r]$后,考虑如何转移$[l, r+1]$,发现就是$a[r+1]$的颜色在这个区间的排名,然后$r-l+1-排名$就是需要移动的次数。
那么本题中因为只需要裸的排名,所以可以考虑用bit,即离散后搞。
然后就行了
#include <cstdio>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=50005;
struct dat { int l, r, id; }q[N];
int a[N], c[N], m, n, t[N], ans[N], sum, tot, pos[N]; void add(int x, int s) { for(; x<=tot; x+=x&-x) c[x]+=s; }
int getsum(int x) { int r=0; for(; x; x-=x&-x) r+=c[x]; return r; }
bool cmp(const dat &a, const dat &b) { return pos[a.l]==pos[b.l]?a.r<b.r:a.l<b.l; } void fix(int x, int f, int fx, int len) { //0:left 1:right
int y=(fx?len-getsum(x):getsum(x-1));
sum+=f*y;
add(x, f);
} void init() {
int sz=sqrt(0.5+n);
for1(i, 1, n) pos[i]=i/sz;
sort(q+1, q+1+m, cmp);
//for1(i, 1, m) dbg(q[i].id);
} int main() {
read(n);
for1(i, 1, n) read(a[i]), t[i]=a[i];
sort(t+1, t+1+n);
tot=unique(t+1, t+1+n)-t-1;
for1(i, 1, n) a[i]=lower_bound(t+1, t+1+tot, a[i])-t;
read(m);
for1(i, 1, m) read(q[i].l), read(q[i].r), q[i].id=i;
init();
int xl, xr, id, l=1, r=0;
for1(i, 1, m) {
xl=q[i].l;
xr=q[i].r;
id=q[i].id;
while(l<xl) fix(a[l], -1, 0, r-l+1), ++l;
while(l>xl) fix(a[l-1], 1, 0, r-l+1), --l;
while(r<xr) fix(a[r+1], 1, 1, r-l+1), ++r;
while(r>xr) fix(a[r], -1, 1, r-l+1), --r;
ans[id]=sum;
}
for1(i, 1, m) printf("%d\n", ans[i]); return 0;
}
Description
Input
第一行一个正整数n,表示Mato的资料份数。
第二行由空格隔开的n个正整数,第i个表示编号为i的资料的大小。
第三行一个正整数q,表示Mato会看几天资料。
之后q行每行两个正整数l、r,表示Mato这天看[l,r]区间的文件。
Output
q行,每行一个正整数,表示Mato这天需要交换的次数。
Sample Input
1 4 2 3
2
1 2
2 4
Sample Output
2
HINT
Hint
n,q <= 50000
样例解释:第一天,Mato不需要交换
第二天,Mato可以把2号交换2次移到最后。
Source
【BZOJ】3289: Mato的文件管理(莫队算法+树状数组)的更多相关文章
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- 【BZOJ3289】Mato的文件管理 莫队算法+树状数组
[BZOJ3289]Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是 ...
- bzoj 3289: Mato的文件管理 莫队+线段树
题目链接 给一些询问,每个询问给出区间[L, R] , 求这段区间的逆序数. 先分块排序, 然后对于每次更改, 如果是更改L, 那么应该查询区间内比他小的数的个数, 如果更改R, 查区间内比他大的数的 ...
- 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...
- BZOJ 3289:Mato的文件管理(莫队算法+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3289 题意:…… 思路:求交换次数即求逆序对数.确定了这个之后,先离散化数组.然后在后面插入元素的话 ...
- BZOJ 3289: Mato的文件管理 莫队+BIT
3289: Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的 ...
- Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 1539 Solved: 665[Submit][Status][Di ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
- bzoj 3289 : Mato的文件管理 (莫队+树状数组)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3289 思路: 求区间最小交换的次数将区间变成一个不降序列其实就是求区间逆序对的数量,这 ...
随机推荐
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- lz4,pigz,gzip 3者比较
一.压缩(1.1)使用gzip进行打包:# time tar -zcf tar1.tar binlog*real 0m48.497suser 0m38.371ssys 0m2.571s (1.2)使用 ...
- Windows下的git配置
需要的配置: 1.C:\Program Files\Git\etc\git-completion.bash: alias ls='ls --show-control-chars --color=aut ...
- .NET Reflector 7.6.1.824 Edition .NET程序反编译神器(附插件安装教程2012-10-13更新) 完全破解+使用教程
原文来自VAllen cnblogs 一.使用教程1.解压后,双击Reflector.exe,如果有选择默认版本的.Net Framework,根据需要选择即可.你选择的版本不同则出现的默认程序集也不 ...
- Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- MyBatis3: There is no getter for property named 'code' in 'class java.lang.String'
mybatis3 : mysql文如下,传入参数为string类型时‘preCode’,运行报错为:There is no getter for property named 'preCode' i ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- UVA 10325 The Lottery( 容斥原理)
The Sports Association of Bangladesh is in great problem with their latest lottery `Jodi laiga Jai'. ...
- discuz 学习
一.Discuz首页“今日”“昨日”“欢迎新会员”等文字删除添加 搜索templeta/default/forum/discuz.htm (使用非默认模版的请修改当前使用模版的discuz.htm ...
- fuser命令小结
前提 linux环境下,当使用umount命令卸载挂载点时,会遇到“device is busy”提示,这时fuser就能查出谁在使用这个资源;当然umount –lf [挂载点] 也可以强制卸载 ...