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函数声明提升
首先,JavaScript中函数有两种创建方式,即函数声明.函数表达式两种. 1.函数声明. function boo(){ console.log(123); } boo() 2.函数表达式. va ...
- ajax跳转到新的jsp页面
ajax可以实现局部刷新页面,即在不刷新整个页面的情况下更新页面的局部信息. 项目中遇到一个问题:在用户列表也,当点击某个按钮时需要去查询用户的信息,查询成功跳转到用户详情界面:查询失败,则在原页面弹 ...
- solrcloud(solr集群版)安装与配置
1 Solr集群 1.1 什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的 ...
- dedecms_插件
../dede/adbaoming.php../dede/baoming_edit.php../dede/templets/baoming_main.htm
- web.xml 报错
1.The markup in the document following the root element must be well-formed. 原因是配置时没有 放在根下 <web-a ...
- Servlet基础(工作原理、生命周期)
(一)Servlet开发与配置 1.1 开发步骤 1)编写java类,继承HttpServlet类 2)重新doGet和doPost方法 3)Servlet程序交给tomcat服务器运行! 配置信息: ...
- vue学习笔记(四)——Vue实例以及生命周期
1.Vue实例API 1.构造器(实例化) var vm = new Vue({ //选项 |-------DOM(3) | |-------el (提供一个在页面上已存在的 DOM 元素作为 V ...
- Java 几种动态代理实现及其性能比较
原处出之于阿里liangf Interface: package com.sunchao.jdkdyproxy; public interface Subject { void request(); ...
- JavaScript总结学习一:js中构造函数与普通函数的区别
构造函数不仅只出现在JavaScript中,它同样存在于很多主流的程序语言里,比如c++.Java.PHP等等.与这些主流程序语言一样,构造函数在js中的作业一样,也是用来创建对象时初始化对象,并且总 ...
- programing
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...