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) 搬来 ...
随机推荐
- swift - 本地通知2 - 啰嗦版
1. import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var windo ...
- ABAP开发需要养成的习惯—处理规范,日期,sort,改结构
sELECT select之后不要急着处理,最多用下sort还有delete adjacent,不用sy-subrc判断之后loop操作,要注意处理逻辑. sort一个好处是为了后面read tabl ...
- LRU缓存原理
LRU(Least Recently Used) LRU是近期最少使用的算法,它的核心思想是当缓存满时,会优先淘汰那些近期最少使用的缓存对象. 采用LRU算法的缓存有两种:LrhCache和DisL ...
- go语言中的strings常用函数和格式化输出
package main; import ( "fmt" "strings" ) type person struct { name string; age i ...
- js的面向对象
JavaScript不区分类和实例的概念,而是通过原型(prototype)来实现面向对象编程. 原型是指当我们想要创建xiaoming这个具体的学生时,我们并没有一个Student类型可用 var ...
- win10安装.net3.5
近日有网友反映在windows10_64位系统电脑上安装Net framework3.5,操作时总失败,怎么办呢?小编下面就介绍win10 64位系统无法安装Net framework3.5的两种解决 ...
- Codeforces 791C. Bear and Different Names 模拟构造
C. Bear and Different Names time limit per test:1 second memory limit per test:256 megabytes input:s ...
- [转]11种常见sqlmap使用方法详解
sqlmap也是渗透中常用的一个注入工具,其实在注入工具方面,一个sqlmap就足够用了,只要你用的熟,秒杀各种工具,只是一个便捷性问题,sql注入另一方面就是手工党了,这个就另当别论了.今天把我一直 ...
- python网络socket编程
一.服务端 #!/usr/bin/python # -*- coding: UTF-8 -*- import socket import sys from thread import * HOST = ...
- msgs no .h file
1.单独编译包,catkin_make --pkg 包名,failed,则 2.进入build下对应的msgs包中,使用make,以及make install,failed,则 3.使用catkin_ ...