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) 搬来 ...
随机推荐
- selenimu学习二
1.上传文件 from selenium import webdriver import time import os driver = webdriver.Chrome() src_file = & ...
- XML 解析技术
xml 解析方式有两种: dom 解析和 sax 解析: 针对着两种解析方式,有三种解析器: sun公司的 jaxp dom4j 组织的 dom4j jdom 组织的 jdom dom 解析XML : ...
- JS正则表达式验证是否为11位有效手机号码
function isPoneAvailable($poneInput) { var myreg=/^[1][3,4,5,7,8][0-9]{9}$/; if (!myreg.test($poneIn ...
- Vs2015 c# 诊断工具查看程序的占用情况
windbg用着还不熟悉,dottrace 还要版权,着急查看程序的cpu 的使用情况,因为程序开启之后占用处理器资源较大,问题在哪里呢,于是点开了vs2015自带的诊断工具,以前偶尔打开过,没发现 ...
- 26.mysql日志
26.mysql日志mysql日志包括:错误日志.二进制日志.查询日志.慢查询日志. 26.1 错误日志错误日志记录了mysqld启动到停止之间发生的任何严重错误的相关信息.mysql故障时应首先查看 ...
- 5F - Coin Change
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make c ...
- get(0).tagName获得作用标签
<script type="text/javascript" src="jquery1.4.js"></script><scrip ...
- PAT 1025 反转链表 (25)(STL-map+思路+测试点分析)
1025 反转链表 (25)(25 分) 给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4:如果K为4, ...
- iOS.OpenSource.PopularProject
1. Core Plot Core Plot is a plotting framework for OS X and iOS. It provides 2D visualization of dat ...
- javascript 高级程序设计 八
function 类型: 1.ECMAscript中函数和类C语言的函数有这很多不同.其中之一就是js的函数没有重载.并且多次定义一个同名的函数,当调用这个函数的时候, 会调用最后一次定义的函数. 2 ...