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创建一个工程的多个taget,便于测试和发布多个版本

    背景:很多时候,我们需要在一个工程中创立多个target,也就是说我们希望同一份代码可以创建两个应用,放到模拟器或者真机上,或者是,我们平时有N多人合作开发,当测试的时候,在A这里装了一遍测A写的那块 ...

  2. Error filterStart的问题

    今天出现这个问题 严重: Error filterStart org.apache.catalina.core.StandardContext start 严重: Context startup fa ...

  3. rotate image(旋转数组)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. Day20 Django的使用_基础

    老师网址: https://www.cnblogs.com/yuanchenqi/articles/7652353.html 1,复习上级课,一对一,一对多,多对多的使用 models.py: cla ...

  5. Redis+Django(Session,Cookie、Cache)的用户系统

    转自 http://www.cnblogs.com/BeginMan/p/3890761.html 一.Django authentication django authentication 提供了一 ...

  6. POI导出excel并下载(以流的形式在客户端下载,不保存文件在服务器上)

    import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; i ...

  7. HTML学习笔记8:表单

      什么是表单?     一个网页表单可以将用户输入的数据发送到服务器进行处理.因为互联网用户使用复选框,单选按钮或文本字段填写表格,所以WebForms的形式类似文件或数据库.例如,WebForms ...

  8. [Python学习之路] 猜大小游戏

    # coding =utf-8 import random def roll_dice(number=3, points=None): if points == None: points = [] w ...

  9. python while 循环语句

    Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务.其基本形式为: while 判断条件: 执行语句-- 执行语句可以是单个语句或语句 ...

  10. PAT1129:Recommendation System

    1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...