BZOJ_3289_Mato的文件管理_莫队+树状数组

Description

Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号
。为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能访问。Mato每天随机选一个区间[l,r
],他今天就看编号在此区间内的这些资料。Mato有一个习惯,他总是从文件大小从小到大看资料。他先把要看的
文件按编号顺序依次拷贝出来,再用他写的排序程序给文件大小排序。排序程序可以在1单位时间内交换2个相邻的
文件(因为加密需要,不能随机访问)。Mato想要使文件交换次数最小,你能告诉他每天需要交换多少次吗?

Input

第一行一个正整数n,表示Mato的资料份数。
第二行由空格隔开的n个正整数,第i个表示编号为i的资料的大小。
第三行一个正整数q,表示Mato会看几天资料。
之后q行每行两个正整数l、r,表示Mato这天看[l,r]区间的文件。
n,q <= 50000

Output

q行,每行一个正整数,表示Mato这天需要交换的次数。

Sample Input

4
1 4 2 3
2
1 2
2 4

Sample Output

0
2
//样例解释:第一天,Mato不需要交换
第二天,Mato可以把2号交换2次移到最后。
 
分析:
 
UPD:naive!
 
莫队裸题。
指针l,r移动时用树状数组更新答案,考虑每个数的贡献即可。
 
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define N 50050
#define LL long long
int c[N], n, q, v[N], pos[N];
struct A {
int num,d,id;
}b[N];
bool cmp3(const A &x,const A &y) {return x.num<y.num; }
bool cmp4(const A &x,const A &y) {return x.id < y.id ; }
struct B {
int s,t,id;
LL ans;
}a[N];
bool cmp1(const B &x,const B &y) {
if(pos[y.s] == pos[x.s]) return x.t < y.t;
return pos[x.s] < pos[y.s];
}
bool cmp2(const B &x,const B &y) {
return x.id < y.id;
}
LL now;
int main() {
scanf("%d",&n);
int i, j, block = sqrt(n), l, r = 0;
for(i = 1;i <= block; ++ i) {
l = r + 1;
r = i * block;
for(j = l;j <= r; ++ j) pos[j] = i;
}
if(r != n) {
++ block;
l = r + 1;
r = n;
for(i = l;i <= r; ++ i) pos[i] = block;
}
for(i = 1;i <= n; ++ i) scanf("%d", &b[i].num), b[i].id = i;
sort(b + 1, b + n + 1, cmp3); b[0].num = -84234820;
for(i = 1, j = 0;i <= n; ++ i) {if(b[i].num != b[i - 1].num)j ++; b[i].d = j; }
sort(b + 1, b + n + 1, cmp4);
for(i = 1;i <= n; ++ i) v[i] = b[i].d;
scanf("%d", &q);
for(i = 1;i <= q; ++ i) scanf("%d%d", &a[i].s, &a[i].t),a[i].id = i;
sort(a + 1, a + q + 1, cmp1);
for(l = 1, r = 0, i = 1;i <= q; ++ i) {
while(l < a[i].s) {
int t = v[l] - 1, re = 0;
for(; t ;t -= t & (-t)) re += c[t];
now -= re;
t = v[l];
for(;t <= n;t += t & (-t)) c[t]--;
l ++;
}
while(l > a[i].s) {
int t = v[l - 1] - 1, re = 0;
for(; t ;t -= t & (-t)) re += c[t];
now += re;
t = v[l - 1];
for(;t <= n;t += t & (-t)) c[t]++;
l --;
}
while(r > a[i].t) {
int t = v[r], re = 0;
for(; t ;t -= t & (-t)) re += c[t];
re = r - l + 1- re;
now -= re;
t = v[r];
for(;t <= n;t += t & (-t)) c[t]--;
r --;
}
while(r < a[i].t) {
int t = v[r + 1], re = 0;
for(; t ;t -= t & (-t)) re += c[t];
re = r - l + 1 - re;
now += re;
t = v[r + 1];
for(;t <= n;t += t & (-t)) c[t]++;
r ++;
}
a[i].ans = now;
}
sort(a + 1, a + q + 1, cmp2);
for(i = 1;i <= q; ++ i) printf("%lld\n",a[i].ans);
}

BZOJ_3289_Mato的文件管理_莫队+树状数组的更多相关文章

  1. BZOJ 3289: Mato的文件管理 【莫队 + 树状数组】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3289 3289: Mato的文件管理 Time Limit: 40 Sec  Memory ...

  2. BZOJ3289 Mato的文件管理 【莫队 + 树状数组】

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MB Submit: 3964  Solved: 1613 [Submit][Status] ...

  3. BZOJ 3289 Mato的文件管理(莫队+树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3289 [题目大意] 求静态区间逆序对. [题解] 我们对查询进行莫队操作,对于区间的删 ...

  4. BZOJ3289 Mato的文件管理(莫队+树状数组)

    这个做法非常显然. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...

  5. bzoj 3289: Mato的文件管理 莫队+树状数组

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...

  6. bzoj3236 作业 莫队+树状数组

    莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...

  7. BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块

    题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...

  8. COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)

    题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...

  9. 51nod 1290 Counting Diff Pairs | 莫队 树状数组

    51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...

随机推荐

  1. 《转》Xcode 6 正式版如何创建一个Empty Application

    Xcode 6 正式版里面没有Empty Application这个模板,这对于习惯了纯代码编写UI界面的程序员来说很不习惯. 有网友给出了一个解决方法是,把Xcode 6 beta版里面的模板复制过 ...

  2. 自设table表格,获取内容,并经弹出框的url传参,获取结果显示在弹出框,并加载合计

    table表格,选择框 form id="editForm1"> <table class="table_form"> <td > ...

  3. .ancestors *效果

    <!DOCTYPE html> <html> <head> <style> .ancestors * {  display: block; border ...

  4. Viavdo&ISE&Quartus II级联Modelsim级联仿真

    博主一直致力寻找高效的工作方式,所以一直喜欢折腾软件,从刚开始只用软件IDE自带的编辑器,到Notepad++,再到后来的Vim,从用ISE14.7自带的Isim仿真,到发现更好的Modelsim,再 ...

  5. Java中常用的数据结构类

    结构体系图 List ArrayList.LinkedList.Vector有什么区别? ArrayList 只能装入引用对象(基本类型要转换为封装类): 线程不安全: 底层由数组实现(顺序表),因为 ...

  6. Python-Web框架之 - 利用SQLALchemy创建与数据库MySQL的连接, 详解用Flask时会遇到的一些大坑 !

    经过这个小项目算是对Django与Flask这两个web框架有了新的认识 , Django本身的轮子非常齐全 , 套路也很固定 , 新手在接触Django框架时 , 不会陷入到处找轮子的大坑 ; 那么 ...

  7. golang升级

    系统安装软件一般在/usr/share,可执行的文件在/usr/bin,配置文件可能安装到了/etc下等. 文档一般在 /usr/share 可执行文件 /usr/bin 配置文件 /etc lib文 ...

  8. FFPLAY的原理(五)

    创建线程 Spawning Threads Overview Last time we added audio support by taking advantage of SDL's audio f ...

  9. how to select checkbox on cli environment?

    generally , u can focus on this checkbox and press blank key Ok,that's shit.    

  10. Neo4j安装后的密码修改

    首先默认用户名/密码是neo4j/neo4j. 在安全验证打开的时候,你访问服务器/db/data之类的地址可能会提示您以下信息: { "password_change" : &q ...