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) 搬来 ...
随机推荐
- Compile、Make和Build的区别
针对Java的开发工具,一般都有Compile.Make和Build三个菜单项,完成的功能的都差不多,但是又有区别. 编译,是将源代码转换为可执行代码的过程.编译需要指定源文件和编译输出的文件路径 ...
- console框脱离eclipse窗口
解决方案: 直接将视图重置. 在eclipse主窗口的最上层选项中,点击“window”选项,找到其中的“Reset Perspective”选项,点击确认即可.即视图重置.
- 5-java 排序, sort, collections.sort()
https://blog.csdn.net/whp1473/article/details/79678974 import java.util.ArrayList; import java.util. ...
- Vue 数组 字典 template v-for 的使用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 9.28 h5日记
9.28 1.transparent 透明的 颜色 2.placeholder 提示语 在input中使用 跟velue不同 3.写页面需要注意的 (1)页面一定要有层次,分清层次 (2)保证元素模块 ...
- 关于opencv中的颜色模型转换之CV_BGR2HSV
1.opencv函数cvCvtColor(rgb_im,hsv_im,CV_BGR2HSV)中使用的RGB颜色空间转到HSV算法: max=max(R,G,B) min=min(R,G,B) if R ...
- ParseCrontab类,解析时间规则
<?php /** * Created by PhpStorm. * User: ClownFish 187231450@qq.com * Date: 14-12-27 * Time: 上午11 ...
- Python爬虫的原理
简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现我们眼前: 一.爬虫是什么? ...
- 如何搭建http服务仓库
1.拷贝仓库repo-A文件到服务器/media/D: 2.通过createrepo_c 生成仓库rpm信息数据 cd repo-A createrepo . 3.chmod -R 775 repo ...
- java byte转string 涉及到字节流中有中文
最近遇到一个问题,我用java写了一个客户端通过socket向服务器端发送消息,发送的内容是字节流,编码格式是GBK,服务器在收到消息后,如果格式正确,会返回固定的消息格式,同样也是字节流,编码格式也 ...