BZOJ 3744: Gty的妹子序列 [分块]
题意:询问区间内逆序对数
感觉这种题都成套路题了
两个预处理$f[i][j]$块i到j的逆序对数,$s[i][j]$前i块$\le j$的有多少个
f我直接处理成到元素j,方便一点
用个树状数组就行了
预处理和查询都带$log$所以还是开根号n比较科学吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=5e4+, M=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
} int n, a[N], Q, mp[N], l, r;
struct meow{int l,r;} b[M];
int block, m, pos[N];
inline void ini(){
block=sqrt(n); m=(n-)/block+;
for(int i=;i<=n;i++) pos[i]=(i-)/block+;
for(int i=;i<=m;i++) b[i].l=(i-)*block+, b[i].r=i*block;
b[m].r=n;
}
struct Block{
int c[N];
inline void add(int p,int v) {for(;p<=*mp;p+=(p&-p)) c[p]+=v;}
inline int sum(int p) {int re=; for(;p;p-=(p&-p)) re+=c[p]; return re;}
int f[M][N], s[M][N];
void Set(int x){
for(int i=b[x].l; i<=n; i++)
f[x][i]= f[x][i-]+sum(*mp)-sum(a[i]), add(a[i], );// printf("f %d %d %d\n",x,i,f[x][i]);
memset(c, , sizeof(c)); for(int i=b[x].l; i<=b[x].r; i++) s[x][a[i]]++;
for(int i=; i<=*mp; i++) s[x][i]+= s[x][i-];
for(int i=; i<=*mp; i++) s[x][i]+= s[x-][i];// printf("s %d %d %d\n",x,i,s[x][i]);;
} int Que(int l,int r){
int pl=pos[l], pr=pos[r];
int ans=;
if(pl==pr){
for(int i=l;i<=r;i++) ans+= sum(*mp)-sum(a[i]), add(a[i], );
for(int i=l;i<=r;i++) add(a[i], -);
}else{
ans= f[pl+][r]; //printf("ans %d \n",ans);
for(int i=b[pr].l; i<=r; i++) add(a[i], );
for(int i=b[pl].r; i>=l; i--) ans+= sum(a[i]-) + s[pr-][a[i]-] - s[pl][a[i]-], add(a[i], );
for(int i=b[pr].l; i<=r; i++) add(a[i], -);
for(int i=l; i<=b[pl].r; i++) add(a[i], -);
}
return ans;
}
}B;
int main(){
freopen("in","r",stdin);
n=read();
for(int i=;i<=n;i++) a[i]=mp[i]=read();
sort(mp+, mp++n); *mp=unique(mp+, mp++n) - mp - ;
for(int i=;i<=n;i++) a[i]=lower_bound(mp+, mp++*mp, a[i]) - mp;
//for(int i=1;i<=n;i++) printf("%d ",a[i]); puts("");
ini();
for(int i=;i<=m;i++) B.Set(i); Q=read(); int lastans=;
while(Q--){
//l=read(), r=read();
l=read()^lastans, r=read()^lastans;
if(l>r) swap(l, r);
lastans=B.Que(l, r); printf("%d\n",lastans);
}
}
BZOJ 3744: Gty的妹子序列 [分块]的更多相关文章
- BZOJ 3744 Gty的妹子序列 (分块 + BIT)
3744: Gty的妹子序列 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1931 Solved: 570[Submit][Status][Dis ...
- BZOJ 3744 Gty的妹子序列 (分块+树状数组+主席树)
题面传送门 题目大意:给你一个序列,多次询问,每次取出一段连续的子序列$[l,r]$,询问这段子序列的逆序对个数,强制在线 很熟悉的分块套路啊,和很多可持久化01Trie的题目类似,用分块预处理出贡献 ...
- BZOJ 3744 Gty的妹子序列 分块+树状数组
具体分析见 搬来大佬博客 时间复杂度 O(nnlogn)O(n\sqrt nlogn)O(nnlogn) CODE #include <cmath> #include <cctyp ...
- BZOJ 3744: Gty的妹子序列 【分块 + 树状数组 + 主席树】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3744 3744: Gty的妹子序列 Time Limit: 20 Sec Memory ...
- bzoj 3744: Gty的妹子序列 主席树+分块
3744: Gty的妹子序列 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 101 Solved: 34[Submit][Status] Descr ...
- BZOJ 3744 Gty的妹子序列
Description 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见-- 某天,蒟蒻Autumn发现了从 Gty的妹子树上掉落下来了许多妹子,他发现 她们排成了一个序 ...
- bzoj 3744 Gty的妹子序列 区间逆序对数(在线) 分块
题目链接 题意 给定\(n\)个数,\(q\)个询问,每次询问\([l,r]\)区间内的逆序对数. 强制在线. 思路 参考:http://www.cnblogs.com/candy99/p/65795 ...
- BZOJ - 3744 Gty的妹子序列 (区间逆序对数,分块)
题目链接 静态区间逆序对数查询,这道题用线段树貌似不好做,可以把区间分成$\sqrt n$块,预处理出两个数组:$sum[i][j]$和$inv[i][j]$,$sum[i][j]$表示前i个块中小于 ...
- BZOJ 3744 Gty的妹子序列 做法集结
我只会O(nnlogn)O(n\sqrt nlogn)O(nnlogn)的 . . . . 这是分块+树状数组+主席树的做法O(nnlogn)O(n\sqrt nlogn)O(nnlogn) 搬来 ...
随机推荐
- javascript 之原型、原型链-14
原型 原型是一个对象,每个函数对象(在javascript 之对象中说过函数也是对象 )都有一个属性(prototype)指向这个对象--原型对象,这个对象的作用是让所有对象实例共享原型对象中的属性. ...
- Angular 选项卡
<div ng-init="now=0;" class="nav"> <h4>选项卡</h4> <div > & ...
- Sqoop导入导出的几个例子
Sqoop导入导出的几个例子 http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html#_importing_data_into_hive no ...
- vuethink 在本地没问题,在服务器报错 , php5.6与php5.5之间的大坑
将环境换为php5.6即可
- vuejs中执行npm run dev出现页面cannot GET/问题
1.问题描述 最近用vue-cli搭建了一个小项目,其中在对项目进行打包测试时修改了webpack.base.conf.js中的配置信息,之后再用npm run dev之后出现如下的情况: 最近又有网 ...
- LNMP 与 LAMP 架构的区别及配置解决方案
2014-12-31 10:33| 发布者: digitser| 查看: 5618| 评论: 0|原作者: liangsheng 摘要: LNMP 与 LAMP 架构的区别及配置解决方案 LNMP 的 ...
- 【开发技术】如何查看项目中struts的版本
struts-configer.xml(struts1)或struts.xml(struts2)中 struts-2.0.dtd处表示版本号
- EditText之边框颜色
EditText的自带属性里没有设置边框颜色的 有俩种方式可以达到效果 一种是网上比较推崇的用图作背景,另一种则是自绘 图作背景的: 首先重新定义一个style.在values文件夹下新建一个styl ...
- Ubuntu问题:E45: 'readonly' option is set (add ! to override)错误解决
问题描述:E45: 'readonly' option is set (add ! to override) 问题分析:该错误为当前用户没有权限对文件作修改 问题解决: 输入 :w !sudo tee ...
- Linuxc - 多c文件程序编译执行
多文件使用,一起编译 定义max.h int max(int a,int b); 定义max.c #include "max.h" int max(int a,int b) { i ...