3744: Gty的妹子序列

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 1931  Solved: 570
[Submit][Status][Discuss]

Description

我早已习惯你不在身边,
 
人间四月天 寂寞断了弦。
 
回望身后蓝天,
 
跟再见说再见……
 
 
某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现
 
她们排成了一个序列,每个妹子有一个美丽度。
 
Bakser神犇与他打算研究一下这个妹子序列,于是Bakser神犇问道:"你知道区间
 
[l,r]中妹子们美丽度的逆序对数吗?"
 
蒟蒻Autumn只会离线乱搞啊……但是Bakser神犇说道:"强制在线。"
 
请你帮助一下Autumn吧。
 
 
给定一个正整数序列a,对于每次询问,输出al...ar中的逆序对数,强制在线。

Input

第一行包括一个整数n(1<=n<=50000),表示数列a中的元素数。
 
第二行包括n个整数a1...an(ai>0,保证ai在int内)。
 
接下来一行包括一个整数m(1<=m<=50000),表示询问的个数。
 
接下来m行,每行包括2个整数l、r(1<=l<=r<=n),表示询问al...ar中的逆序
 
对数(若ai>aj且i<j,则为一个逆序对)。
 
l,r要分别异或上一次询问的答案(lastans),最开始时lastans=0。
 
保证涉及的所有数在int内。

Output

对每个询问,单独输出一行,表示al...ar中的逆序对数。

Sample Input

4
1 4 2 3
1
2 4

Sample Output

2

HINT

 

Source

析:考虑了好久,确实是不好做啊,这个可以用分块来做,然后要预处理两个,f[i][j] 表示块 i 到 j 有多少个逆序对,s[i][j] 表示前 i 块 小于等于 j 个的数有多少个,当然这个要离散化了,这两个数组可以用n * sqr(n) * logn来处理,可以用逆推来做。然后在查询的时候,要分两部分来查,一个是处于同一块的,这个直接算就行,另一个再处理。
代码如下:
#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)的更多相关文章

  1. BZOJ 3744 Gty的妹子序列 (分块+树状数组+主席树)

    题面传送门 题目大意:给你一个序列,多次询问,每次取出一段连续的子序列$[l,r]$,询问这段子序列的逆序对个数,强制在线 很熟悉的分块套路啊,和很多可持久化01Trie的题目类似,用分块预处理出贡献 ...

  2. BZOJ 3744: Gty的妹子序列 [分块]

    传送门 题意:询问区间内逆序对数 感觉这种题都成套路题了 两个预处理$f[i][j]$块i到j的逆序对数,$s[i][j]$前i块$\le j$的有多少个 f我直接处理成到元素j,方便一点 用个树状数 ...

  3. BZOJ 3744 Gty的妹子序列 分块+树状数组

    具体分析见 搬来大佬博客 时间复杂度 O(nnlogn)O(n\sqrt nlogn)O(nn​logn) CODE #include <cmath> #include <cctyp ...

  4. BZOJ 3744: Gty的妹子序列 【分块 + 树状数组 + 主席树】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3744 3744: Gty的妹子序列 Time Limit: 20 Sec  Memory ...

  5. bzoj 3744: Gty的妹子序列 主席树+分块

    3744: Gty的妹子序列 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 101  Solved: 34[Submit][Status] Descr ...

  6. BZOJ 3744 Gty的妹子序列

    Description 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见-- 某天,蒟蒻Autumn发现了从 Gty的妹子树上掉落下来了许多妹子,他发现 她们排成了一个序 ...

  7. bzoj 3744 Gty的妹子序列 区间逆序对数(在线) 分块

    题目链接 题意 给定\(n\)个数,\(q\)个询问,每次询问\([l,r]\)区间内的逆序对数. 强制在线. 思路 参考:http://www.cnblogs.com/candy99/p/65795 ...

  8. BZOJ - 3744 Gty的妹子序列 (区间逆序对数,分块)

    题目链接 静态区间逆序对数查询,这道题用线段树貌似不好做,可以把区间分成$\sqrt n$块,预处理出两个数组:$sum[i][j]$和$inv[i][j]$,$sum[i][j]$表示前i个块中小于 ...

  9. BZOJ 3744 Gty的妹子序列 做法集结

    我只会O(nnlogn)O(n\sqrt nlogn)O(nn​logn)的 . . . . 这是分块+树状数组+主席树的做法O(nnlogn)O(n\sqrt nlogn)O(nn​logn) 搬来 ...

随机推荐

  1. Java_10 继承

    1 继承的好处 继承的出现提高了代码的复用性,提高软件开发效率. 继承的出现让类与类之间产生了关系,提供了多态的前提. 2 继承的注意事项 在Java中,类只支持单继承,不允许多继承,也就是说一个类只 ...

  2. FragmentManager V4包下 应该用FragmentActivity

    import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity ...

  3. javascript中的类型转换(进制转换|位运算)

    1:parseInt(string) : 这个函数的功能是从string的开头开始解析,返回一个整数 parseInt("123hua"); //输出 123 parseInt(& ...

  4. 转:css知多少(12)——目录

    <css知多少>系列就此完结了.常来光顾的朋友可能会觉得突然:css的知识点还有很多,怎么突然就完了,还没讲完呢?这样说是对的.不过凡事都有一个定位,如果盲目求多,定位模糊,那样就没有目的 ...

  5. (转)Silverlight调用的JS方法返回对象数组的处理方法

    最近在做Silverlight应用,需要用Silverlight调用页面中Javascript方法.这 个JS方法返回一个对象数组给Silverlight.对于这个对象数组怎么在Silverlight ...

  6. Windows下war包部署到Linux下Tomcat出现的问题

    最近,将Windows下开发的war包部署到Linux下的Tomcat时报了一个错误:tomcat error in opening zip file.按理说,如果正常,当把war包复制到webapp ...

  7. target runtime apache v6.0 not defined解决

    在加载别人的一个项目时,会报该错误,需要先在buildpath中remove v6的版本,再点击add library,选择server runtime,如果eclipse配置过Tomcat,可以选择 ...

  8. BZOJ 3331 [BeiJing2013]压力-Tarjan + 树上差分

    Solution Tarjan 点双缩点, 加上树上差分计算. 注意特判... 我特判挂了好久呜呜呜 Code #include<cstdio> #include<cstring&g ...

  9. 厉害了,他用PS不是P照片而是……

    今儿要介绍的主角是战斗民族的设计师 Dmitriy Glazyrin,他这个人用PS做设计有个特点,专门P3D软件做出来的白模. 大家可以想象一下,一个什么颜色什么材质都没有的东西,把它楞是用PS加上 ...

  10. 服务程序 -st

    Windows 服务由三部分组成:1.一个服务可执行文件:2.一个服务控制程序(SCP):3.服务控制管理器(SCM),负责在 HKLM\SYSTEM\CurrentControlSet\Servic ...