【BZOJ3289】Mato的文件管理 莫队+树状数组
题目大意:给定一个长度为 N 的序列,M 个询问,每次询问区间逆序对的个数。
题解:用树状数组加速答案转移。
代码如下
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define cls(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=5e4+10;
const double eps=1e-6;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll sqr(ll x){return x*x;}
inline ll fpow(ll a,ll b,ll c){ll ret=1%c;for(;b;b>>=1,a=a*a%c)if(b&1)ret=ret*a%c;return ret;}
inline ll read(){
ll x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
}
/*------------------------------------------------------------*/
int n,m,bsize,a[maxn],d[maxn],tot;
struct query{int id,bl,l,r;}q[maxn];
bool cmp(const query &x,const query &y){return x.bl!=y.bl?x.bl<y.bl:(x.bl&1)?x.r<y.r:x.r>y.r;}
inline int get(int x){return (x-1)/bsize+1;}
ll bit[maxn],now,l=1,r=0,ans[maxn];
void modify(int pos,ll val){
for(int i=pos;i<=n;i+=i&-i)bit[i]+=val;
}
ll query(int pos){
ll ret=0;
for(int i=pos;i;i-=i&-i)ret+=bit[i];
return ret;
}
void read_and_parse(){
n=read(),bsize=sqrt(n);
for(int i=1;i<=n;i++)a[i]=d[i]=read();
sort(d+1,d+n+1);
tot=unique(d+1,d+n+1)-d-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(d+1,d+tot+1,a[i])-d;
m=read();
for(int i=1;i<=m;i++){
q[i].id=i,q[i].l=read(),q[i].r=read();
q[i].bl=get(q[i].l);
}
sort(q+1,q+m+1,cmp);
}
inline void update(int pos,int f){
if(f==0){// r +
now+=query(n)-query(a[pos]);
modify(a[pos],1);
}
else if(f==1){// r -
now-=query(n)-query(a[pos]);
modify(a[pos],-1);
}
else if(f==2){// l -
now+=query(a[pos]-1);
modify(a[pos],1);
}
else{
now-=query(a[pos]-1);
modify(a[pos],-1);
}
}
void solve(){
for(int i=1;i<=m;i++){
while(r>q[i].r)update(r--,1);
while(r<q[i].r)update(++r,0);
while(l>q[i].l)update(--l,2);
while(l<q[i].l)update(l++,3);
ans[q[i].id]=now;
}
for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
}
int main(){
read_and_parse();
solve();
return 0;
}
【BZOJ3289】Mato的文件管理 莫队+树状数组的更多相关文章
- bzoj3289 Mato的文件管理 莫队+树状数组
求逆序对个数,莫队套树状数组 #include<cstdio> #include<iostream> #include<cstring> #include<c ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
- BZOJ3289[JZYZOJP2018]: Mato的文件管理 莫队+树状数组+离散化
描述 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的, ...
- 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. ...
随机推荐
- vue中的跨域问题
https://segmentfault.com/a/1190000011072725(原文) 使用vue-axios和vue-resource解决vue中调用网易云接口跨域的问题 注(api很重 ...
- mac下php开发环境的搭建
1.phpstorm 在官网:https://www.jetbrains.com/phpstorm/,下载最新版:phpstorm-2016.2.1 在http://15.idea.lanyus.co ...
- ArrayList的扩容机制
一.ArrayList的扩容机制 1.扩容的计算方式是向右位移,即:newSize = this.size + (this.size>>1).向右位移,只有在当前值为偶数时,才是除以2:奇 ...
- Django--ORM 多表查询
一 . 建立外键 一对一建立外键 外键名称 = models.OneToOneField(to='要连接的类名', to_field='字段') 一对多建立外键 外键名称 = models.Forei ...
- kubernetes常用命令
#.查询信息 kubectl get [需要查询的服务] node 节点componentstatuses 简写 cs 组件状态namespaces 简写 ns 名命空间pod pod信息 添加 ...
- HTML5 & tel & make a phone call
HTML5 & tel & make a phone call 咋呼叫呀,网页怎么打电话? { key: "exploreCorpPhone", title: &q ...
- 老男孩python学习自修【第二天】字符串用法
实时处理增量日志最佳实践 主要使用f.seek()和f.tell()实现 字符串处理函数: s.find(substr, start, end) 查找子字符串,找不到则返回-1,找到则返回对应的索引 ...
- supervisor /var/run/supervisor/supervisor.sock not found 或者/tmp/supervisor.sock not found
刚按装完supervisor,这时候用supervisorctr -c supervisor.conf 会报错: /var/run/supervisor/supervisor.sock not fou ...
- vue開發環境搭建
npm(node package manager),nodejs的包管理器,用於nodejs插件的安裝.卸載和管理依賴. 安裝npm: 檢查npm是否安裝成功及版本:npm -v 卸載npm: 更新n ...
- 一、linux扩展
1.linux-解压bz2文件提示tar (child): bzip2: Cannot exec: No such file or directory 原因,linux下没有bzip2解压工具 安装b ...