BZOJ3289[JZYZOJP2018]: Mato的文件管理 莫队+树状数组+离散化
|
离散化可以看我上一篇写的那个离散化blahblah什么的.......算是莫队板子
和离散化板子和树状数组板子放到一起的板子题....
树状数组求逆序对即可
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,q;
int a[]={};//原本的文件
int b[]={};//离散化媒介
int bel[]={};//询问块
int tr[]={};//树状数组
int ans[]={};
struct nod{
int x,y;
int id;
}e[];
int sz;
bool mmp(nod aa,nod bb){
if(bel[aa.x]==bel[bb.x]){
if(aa.y==bb.y){
return aa.x<bb.x;
}
return aa.y<bb.y;
}
return bel[aa.x]<bel[bb.x];
}
inline int lowbit(int x){
return x&-x;
}
inline int sum(int x){
int ret = ;
while(x>){
ret+=tr[x];
x-=lowbit(x);
}
return ret;
}
inline void add(int x,int v){
while(x<=){
tr[x]+=v;
x+=lowbit(x);
}
}
void work(){
int l=,r=;
int an=;
for(int i=;i<=q;i++){
while(l>e[i].x){
l--;
an+=sum(a[l]-);
add(a[l],);
}
while(r<e[i].y){
r++;
an+=sum()-sum(a[r]);
add(a[r],);
}
while(l<e[i].x){
add(a[l],-);
an-=sum(a[l]-);
l++;
}
while(r>e[i].y){
add(a[r],-);
an-=sum()-sum(a[r]);
r--;
}
ans[e[i].id]=an;
}
for(int i=;i<=q;i++){
printf("%d\n",ans[i]);
}
}
int main(){
//freopen("wtf.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+);
int size=unique(b+,b+n+)-b-;
for(int i=;i<=n;i++){
a[i]=lower_bound(b+,b+size+,a[i])-b;
}
scanf("%d",&q);
sz=(int)sqrt((double)q);
for(int i=;i<=q;i++){
bel[i]=(i-)/sz+;
}
for(int i=;i<=q;i++){
scanf("%d%d",&e[i].x,&e[i].y);
e[i].id=i;
}
sort(e+,e++q,mmp);
work();
return ;
}
BZOJ3289[JZYZOJP2018]: Mato的文件管理 莫队+树状数组+离散化的更多相关文章
- 【BZOJ3289】Mato的文件管理 莫队+树状数组
题目大意:给定一个长度为 N 的序列,M 个询问,每次询问区间逆序对的个数. 题解:用树状数组加速答案转移. 代码如下 #include <bits/stdc++.h> #define f ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
- bzoj3289 Mato的文件管理 莫队+树状数组
求逆序对个数,莫队套树状数组 #include<cstdio> #include<iostream> #include<cstring> #include<c ...
- Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 1539 Solved: 665[Submit][Status][Di ...
- bzoj 3289 : Mato的文件管理 (莫队+树状数组)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3289 思路: 求区间最小交换的次数将区间变成一个不降序列其实就是求区间逆序对的数量,这 ...
- BZOJ_3289_Mato的文件管理_莫队+树状数组
BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...
- bzoj3236 作业 莫队+树状数组
莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- 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 ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
随机推荐
- 游戏的物理和数学:Unity中的弹道和移动目标提前量计算
下载地址:https://www.jianguoyun.com/p/DZPN6ocQ2siRBhihnx8 弹道计算是游戏里常见的问题,其中关于击中移动目标的自动计算提前量的话题,看似简单,其实还是挺 ...
- Battery Charging Specification 1.2 中文详解 来源:www.chengxuyuans.com
1. Introduction 1.1 Scope 规范定义了设备通过USB端口充电的检测.控制和报告机制,这些机制是USB2.0规范的扩展,用于专用 充电器(DCP).主机(SDP).hub(SDP ...
- 64_p5
php-nette-bootstrap-2.4.3-1.fc26.noarch.rpm 20-Feb-2017 07:19 16290 php-nette-caching-2.5.3-1.fc26.n ...
- js实现图片上传预览
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- php 7.3.3安装问题记录
1.checking for libzip... not foundconfigure: error: Please reinstall the libzip distribution 参考:http ...
- 到底什么是Upnp?[转载]
本文出自:http://www.cnblogs.com/nehu/archive/2006/05/13/399342.html 解释一. 准确地说,UPnP(Universal Plug and Pl ...
- [ python ] 匿名函数和高阶函数
匿名函数 描述: 关键字 lambda 定义的函数 语法: 函数名 = lambda 参数:返回值 返回值: 函数返回结果值 实例: 一个参数的匿名函数: func = lambda ...
- mysql视图学习总结(转)
一.使用视图的理由是什么?1.安全性.一般是这样做的:创建一个视图,定义好该视图所操作的数据.之后将用户权限与视图绑定.这样的方式是使用到 了一个特性:grant语句可以针对视图进行授予权限.2.查询 ...
- 根据名字杀死进程Killall
Killall命令可以用来给一个特定的进程发送一个信号.这个信号默认情况下是SIGTERM,但也可以由killall命令使用参数来指定其它信号.现在让我们通过一些实际的例子来看看这个命令的实际用法. ...
- hbase学习(一)hbase简介
1.hadoop生态系统 2.hbase简介 非关系型数据库知识面扩展 cassandra.hbase.mongodb.redis couchdb,文件存储数据库 Neo4j非关系型图数据库 3.hb ...