【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 思路: 求区间最小交换的次数将区间变成一个不降序列其实就是求区间逆序对的数量,这 ...
随机推荐
- RemObjects SDK Source For Delphi XE7
原文:http://blog.csdn.net/tht2009/article/details/39545545 1.目前官网最新版本是RemObjects SDK for Delphi and al ...
- Sharepoint程序员应该了解的知识
做为一个Sharepoint程序员应该了解的知识:注意,我说的是程序员.因为我一直把自己看一个普普通通的程序员. 前提: 要知道网络基础(包括DHCP.IP.掩码.DNS.网关.广播),会装操作系统( ...
- C++ virtual descructor
[代码1] C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 ...
- linux下emacs配置文件
1:安装.在ubuntu下使用命令 sudo apt-get install emacs,即可,我使用的是ubuntu的10.04的版本,在里面使用了据说是163的2个源. 1.1:如何更新快速的源, ...
- std::map常用方法
map<string, int> Employees; Employees["Mike C."] = 12306; Employees.insert(make_pair ...
- atom 震动特效
1.下载atom 2.配置环境变量 3.运行apm install activate-power-mode 4.打开Atom激活(control+alt+o(是o不是零)) 注:新标签若没效果可以ct ...
- 查看Linus中自带的jdk ,设置JAVA_HOME
在配置hadoop是,进行格式化hadoop的时候,出现找不到jdk 我用Red hat是32位的,没有现成的32位的,敲java , 发现本机有java ,就找了一下其位置 找到了jdk-1.6.0 ...
- redis的单实例配置+web链接redis
[root@cache01 src]# wget http://download.redis.io/redis-stable.tar.gz [root@cache01 src]# tar -xzvf ...
- SecureCRT乱码
http://jingyan.baidu.com/article/948f59245be128d80ff5f9aa.html
- linux下php增加curl扩展,生成curl.so文件
进入php源代码目录 cd /php5.6.9/ext/curl 执行生成so文件编译模式 /usr/local/php/bin/phpize 编译curl扩展 ./configure --with- ...