BZOJ 3744 Gty的妹子序列 (分块 + BIT)
3744: Gty的妹子序列
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 1931 Solved: 570
[Submit][Status][Discuss]
Description
Input
Output
对每个询问,单独输出一行,表示al...ar中的逆序对数。
Sample Input
1 4 2 3
1
2 4
Sample Output
HINT
Source
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-4;
const int maxn = 5e4 + 10;
const int maxm = 2e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} const int SIZE = 225;
int A[maxn];
int f[maxn/SIZE+10][maxn];
int s[maxn/SIZE+10][maxn];
int sum[maxn];
int val[maxn];
inline int lowbit(int x){ return -x&x; }
inline int add(int x, int c){
while(x < maxn){
sum[x] += c;
x += lowbit(x);
}
} inline int query(int x){
int ans = 0;
while(x){
ans += sum[x];
x -= lowbit(x);
}
return ans;
} int solve(int l, int r){
int lb = l / SIZE, rb = r / SIZE;
if(lb == rb){
int ans = 0;
for(int i = l, k = 0; i <= r; ++i, ++k)
ans += k - query(A[i]), add(A[i], 1);
for(int i = l; i <= r; ++i) add(A[i], -1);
return ans;
}
int ans = f[lb+1][r];
for(int i = rb*SIZE; i <= r; ++i) add(A[i], 1);
for(int i = (lb+1)*SIZE-1; i >= l; --i)
ans += query(A[i]-1) + (s[rb-1][A[i]-1] - s[lb][A[i]-1]), add(A[i], 1);
for(int i = l; i < (lb+1)*SIZE; ++i) add(A[i], -1);
for(int i = rb*SIZE; i <= r; ++i) add(A[i], -1);
return ans;
} int main(){
scanf("%d", &n);
int b = 0, cnt = 0;
for(int i = 0; i < n; ++i){
scanf("%d", A+i);
val[i] = A[i];
if(++cnt == SIZE) b++, cnt = 0;
}
sort(val, val + n);
int length = unique(val, val + n) - val;
for(int i = 0; i < n; ++i) A[i] = lower_bound(val, val + length, A[i]) - val + 1;
for(int i = 0; i <= b; ++i){
for(int j = i * SIZE, k = 0; j < n; ++j, ++k){
f[i][j] = k - query(A[j]) + f[i][j-1];
add(A[j], 1);
}
ms(sum, 0);
for(int j = i * SIZE; j < (i+1)*SIZE; ++j) ++s[i][A[j]];
for(int j = 1; j <= length; ++j) s[i][j] += s[i][j-1];
for(int j = 1; j <= length; ++j) s[i][j] += s[i-1][j];
}
scanf("%d", &m);
int last = 0;
while(m--){
int l, r;
scanf("%d %d", &l, &r);
l ^= last, r ^= last;
l = max(l, 1); r = max(r, 1);
r = min(n, r); l = min(l, n);
if(l > r) swap(l, r);
last = solve(l - 1, r - 1);
printf("%d\n", last);
}
return 0;
}
BZOJ 3744 Gty的妹子序列 (分块 + BIT)的更多相关文章
- BZOJ 3744 Gty的妹子序列 (分块+树状数组+主席树)
题面传送门 题目大意:给你一个序列,多次询问,每次取出一段连续的子序列$[l,r]$,询问这段子序列的逆序对个数,强制在线 很熟悉的分块套路啊,和很多可持久化01Trie的题目类似,用分块预处理出贡献 ...
- BZOJ 3744: Gty的妹子序列 [分块]
传送门 题意:询问区间内逆序对数 感觉这种题都成套路题了 两个预处理$f[i][j]$块i到j的逆序对数,$s[i][j]$前i块$\le j$的有多少个 f我直接处理成到元素j,方便一点 用个树状数 ...
- 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) 搬来 ...
随机推荐
- 鼠标移上去触动hover致使div向上移动几个相素(动画transition轻轻的移动)
- Ajax中什么时候用同步,什么时候用异步?
AJAX中根据async的值不同分为同步(async = false)和异步(async = true)两种执行方式:在W3C的教程中推荐使用异步执行: $.ajax({ type: "po ...
- jQuery和js使用点滴
1.checkbox全选按钮 <input type="checkbox" name="allcheck" id="allcheck" ...
- rbac 表结构的。设计
1. 问:为什么程序需要权限控制? 答:生活中的权限限制,① 看灾难片电影<2012>中富人和权贵有权登上诺亚方舟,穷苦老百姓只有等着灾难的来临:② 屌丝们,有没有想过为什么那些长得漂亮身 ...
- python string tuple list dict 相互转换的方法
dict = {'name': 'Zara', 'age': 7, 'class': 'First'}# 字典转为字符串,返回:<type 'str'> {'age': 7, 'name' ...
- Event 事件
事件是建立在委托的基础之上的. http://www.cnblogs.com/lystory/p/5085786.html public class 事件参数 { public 事件参数(string ...
- LibreOJ #6002. 「网络流 24 题」最小路径覆盖
#6002. 「网络流 24 题」最小路径覆盖 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测 ...
- 可读性很强的C语言的函数指针定义
通常C/C++程序里面要用到大量的指针,其语法非常难以阅读.比如下面的vp指针类型: #include <iostream> using namespace std; typedef vo ...
- Laravel Session() 失效的问题
之前因为自己自定义了后台的路由,然后路由定义的乱七八糟的. 突然发现session失效了,记录一下,避免后者遇坑. 路由组统一通过web中间件或者存在于一个中间件中 protected $middle ...
- [Robot Framework] SikuliLibrary的关键字执行依赖java进程,但是上次的java进程如果没有杀掉,robot framework控制台的日志出不来,怎么办?
如果在suite的setup里面杀掉java进程:AutoItLibrary.Run | taskkill /F /IM java.exe 执行sikuli的关键字会报这样的错误: Connectio ...