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. poi excel 常用操作

    基本 Workbook wb= new HSSFWorkbook(); Sheet sheet = wb.createSheet("sheetName"); Row row = s ...

  2. Validate Binary Search Tree(一定掌握的方法)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. access登录窗口校验代码一

    Private Sub login_Click()If IsNull(Me.username) ThenMsgBox "请输入用户名!", vbExclamationElseIf ...

  4. Application "org.eclipse.ui.ide.workbench" could not be found in the registry.问题的解决

    今天升级Eclipse,升级完Restart,碰到启动不了让看日志,日志里主要错误信息即是Application "org.eclipse.ui.ide.workbench" co ...

  5. java面试题,附个人理解答案

    一,面向对象的特征:1.抽象 包括数据抽象跟行为抽象,将对象共同的特征取出形成一个类2.继承 被继承类为基类/超类,继承类为子类/派生类3.封装 多次使用道德数据或方法,封装成类,方便多次重复调用4. ...

  6. Ocelot中文文档-请求聚合

    Ocelot允许您指定聚合多个普通ReRoutes的Aggregate ReRoutes(聚合路由),并将其响应映射到一个对象中.一般用于当您有一个客户端向服务器发出多个请求,而这些请求可以合并成一个 ...

  7. form表单序列化为Jquery对象

    <form id="DailyFinancial" > @*class="form-inline"*@ <div class="fo ...

  8. 关于react router 4 的小实践

    详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...

  9. ASP.NET MVC中的路由IRouteConstraint方法应用实例

    在如下代码的写法中: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { ro ...

  10. hadoop is running beyond virtual memory limits问题解决

    单机搭建了2.6.5的伪分布式集群,写了一个tf-idf计算程序,分词用的是结巴分词,使用standalone模式运行没有任何问题,切换到伪分布式模式运行一直报错: hadoop is running ...