bzoj 3289 Mato的文件管理 区间逆序对数(离线) 莫队
题目链接
题意
给定\(n\)个数,\(q\)个询问,每次询问\([l,r]\)区间内的逆序对数。
思路
莫队+树状数组
注意离散化
Code
#include <bits/stdc++.h>
#define F(i, a, b) for (int i = (a); i < (b); ++i)
#define F2(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i > (b); --i)
#define dF2(i, a, b) for (int i = (a); i >= (b); --i)
#define maxn 50010
using namespace std;
typedef long long LL;
LL temp, ans[maxn];
int n, nn, blo, m, cnt, bl[maxn], a[maxn], mp[maxn], c[maxn];
struct node {
int l, r, id;
bool operator < (const node& nd) const {
return bl[l]==bl[nd.l] ? r<nd.r : bl[l]<bl[nd.l];
}
}q[maxn];
inline int lowbit(int x) { return x&(-x); }
inline int query(int x) { int ret=0; while (x) ret+=c[x], x-=lowbit(x); return ret; }
inline void add(int x, int v) { while (x<=nn) c[x]+=v, x+=lowbit(x); }
inline void addt(int x) { temp+=cnt-query(x); ++cnt; add(x,1); }
inline void delt(int x) { --cnt; add(x,-1); temp-=cnt-query(x); }
inline void adds(int x) { temp+=query(x-1); ++cnt; add(x,1); }
inline void dels(int x) { --cnt; add(x,-1); temp-=query(x-1); }
void discrete() {
sort(mp+1, mp+n+1);
nn = unique(mp+1, mp+n+1)-(mp+1);
F2(i, 1, n) a[i] = lower_bound(mp+1,mp+nn+1,a[i])-mp;
}
int main() {
scanf("%d", &n); blo=sqrt(n);
F2(i, 1, n) scanf("%d", &a[i]), mp[i]=a[i], bl[i]=(i-1)/blo;
discrete();
scanf("%d", &m);
F(i, 0, m) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q,q+m);
int r=0, l=1, cnt=0;
F(i, 0, m) {
while (r<q[i].r) addt(a[++r]);
while (r>q[i].r) delt(a[r--]);
while (l<q[i].l) dels(a[l++]);
while (l>q[i].l) adds(a[--l]);
ans[q[i].id] = temp;
}
F(i, 0, m) printf("%lld\n", ans[i]);
return 0;
}
bzoj 3289 Mato的文件管理 区间逆序对数(离线) 莫队的更多相关文章
- BZOJ 3289: Mato的文件管理 (区间查询逆序对)
这道题就是不要求强制在线的 BZOJ 3744 Gty的妹子序列 所以说离线做法有莫队,在线做法见上面连接. 这里贴出常数巨大O(nnlogn)O(n\sqrt nlogn)O(nnlogn)分块+ ...
- bzoj 3744 Gty的妹子序列 区间逆序对数(在线) 分块
题目链接 题意 给定\(n\)个数,\(q\)个询问,每次询问\([l,r]\)区间内的逆序对数. 强制在线. 思路 参考:http://www.cnblogs.com/candy99/p/65795 ...
- bzoj 3289 Mato的文件管理 树状数组+莫队
Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 4325 Solved: 1757[Submit][Status][Discuss ...
- 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: 2171 Solved: 891 [Submit][Status][ ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- BZOJ 3289: Mato的文件管理
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2368 Solved: 971[Submit][Status][Di ...
- BZOJ 3289: Mato的文件管理 莫队+BIT
3289: Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的 ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
随机推荐
- Python学习笔记:Matplotlib(数据可视化)
Matplotlib是一个可以将数据绘制为图形表示的Python三方库,包括线性图(折线图,函数图).柱形图.饼图等基础而直观的图形,在平常的开发当中需要绘图时就非常有用了. 安装:pip insta ...
- Spring使用mutipartFile上传文件报错【Failed to instantiate [org.springframework.web.multipart.MultipartFile]】
报错场景: 使用SSM框架实现文件上传时报“Failed to instantiate [org.springframework.web.multipart.MultipartFile]”错,控制器源 ...
- sql查询题目
--1.查询在1981年入职的员工信息select * from emp where hiredate between '01-1月-1981'and '31-12月-1981'; select * ...
- 十四、pymysql模块
一.安装的两种方法 第一种 #安装 pip3 install pymysql 第二种 二.链接,执行sql,关闭(游标) import pymysql user= input('用户名:>> ...
- java练习——接口与继承
父类与子类的构造方法: 如果父类中有一个默认无参的构造方法,那么子类的构造方法中会自动进行调用.如果父类有自己的构造方法,且这时父类没有默认无参的构造方法,那么在子类的构造方法中,必须要调用父类的某个 ...
- dubbo本地搭建实例
项目文件下载地址:http://download.csdn.net/detail/aqsunkai/9552711 概述 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- Percona-Tookit工具包之pt-slave-restart
Preface Sometimes,the threads(especially the SQL_Thread) will be terminated by accident.The ...
- Python全栈工程师(异常(高级)、运算符重载)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 开局一张图 Python人工智能从入门到精通 对象的属性管理函数: getattr ...
- 孤荷凌寒自学python第十六天python的迭代对象
孤荷凌寒自学python第十六天python的迭代对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 迭代也就是循环. python中的迭代对象有相关的如下几个术语: A容器 contrai ...