题目链接

题意

给定\(n\)个数,\(q\)个询问,每次询问\([l,r]\)区间内的逆序对数。

思路

莫队+树状数组

注意离散化

Code

#include <bits/stdc++.h>
#define F(i, a, b) for (int i = (a); i < (b); ++i)
#define F2(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i > (b); --i)
#define dF2(i, a, b) for (int i = (a); i >= (b); --i)
#define maxn 50010
using namespace std;
typedef long long LL;
LL temp, ans[maxn];
int n, nn, blo, m, cnt, bl[maxn], a[maxn], mp[maxn], c[maxn];
struct node {
int l, r, id;
bool operator < (const node& nd) const {
return bl[l]==bl[nd.l] ? r<nd.r : bl[l]<bl[nd.l];
}
}q[maxn];
inline int lowbit(int x) { return x&(-x); }
inline int query(int x) { int ret=0; while (x) ret+=c[x], x-=lowbit(x); return ret; }
inline void add(int x, int v) { while (x<=nn) c[x]+=v, x+=lowbit(x); }
inline void addt(int x) { temp+=cnt-query(x); ++cnt; add(x,1); }
inline void delt(int x) { --cnt; add(x,-1); temp-=cnt-query(x); }
inline void adds(int x) { temp+=query(x-1); ++cnt; add(x,1); }
inline void dels(int x) { --cnt; add(x,-1); temp-=query(x-1); }
void discrete() {
sort(mp+1, mp+n+1);
nn = unique(mp+1, mp+n+1)-(mp+1);
F2(i, 1, n) a[i] = lower_bound(mp+1,mp+nn+1,a[i])-mp;
}
int main() {
scanf("%d", &n); blo=sqrt(n);
F2(i, 1, n) scanf("%d", &a[i]), mp[i]=a[i], bl[i]=(i-1)/blo;
discrete();
scanf("%d", &m);
F(i, 0, m) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q,q+m);
int r=0, l=1, cnt=0;
F(i, 0, m) {
while (r<q[i].r) addt(a[++r]);
while (r>q[i].r) delt(a[r--]);
while (l<q[i].l) dels(a[l++]);
while (l>q[i].l) adds(a[--l]);
ans[q[i].id] = temp;
}
F(i, 0, m) printf("%lld\n", ans[i]);
return 0;
}

bzoj 3289 Mato的文件管理 区间逆序对数(离线) 莫队的更多相关文章

  1. BZOJ 3289: Mato的文件管理 (区间查询逆序对)

    这道题就是不要求强制在线的 BZOJ 3744 Gty的妹子序列 所以说离线做法有莫队,在线做法见上面连接. 这里贴出常数巨大O(nnlogn)O(n\sqrt nlogn)O(nn​logn)分块+ ...

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

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

  3. bzoj 3289 Mato的文件管理 树状数组+莫队

    Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 4325  Solved: 1757[Submit][Status][Discuss ...

  4. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

  5. BZOJ 3289 Mato的文件管理(莫队+离散化求逆序数)

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MB Submit: 2171  Solved: 891 [Submit][Status][ ...

  6. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  7. BZOJ 3289: Mato的文件管理

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2368  Solved: 971[Submit][Status][Di ...

  8. BZOJ 3289: Mato的文件管理 莫队+BIT

    3289: Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的 ...

  9. bzoj 3289: Mato的文件管理 莫队+树状数组

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...

随机推荐

  1. python系列3之内置函数和文件操作

    目录 自定义函数 内置函数 文件的操作 练习题 一. 自定义函数 1. 函数的创建 函数的创建 1.def关键字 2.函数名+() 3.冒号 4.缩进 5. return返回值,可以不写,默认的返回值 ...

  2. 统计输入任意的字符中中英文字母,空格和其他字符的个数 python

    这里用到了三个函数: #判断是否为数字:str.isdigit()#是否为字母:str.isalpha()#是否为空格:str.isspace() def tongji(str): alpha = 0 ...

  3. python sys模块和序列化模块

    sys模块是与python解释器交互的一个接口: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit( ...

  4. spring里面的context:component-scan

    原文:http://jinnianshilongnian.iteye.com/blog/1762632 component-scan的作用的自动扫描,把扫描到加了注解Java文件都注册成bean &l ...

  5. 分别用反射、编程接口的方式创建DataFrame

    1.通过反射的方式 使用反射来推断包含特定数据类型的RDD,这种方式代码比较少,简洁,只要你会知道元数据信息时什么样,就可以使用了 代码如下: import org.apache.spark.sql. ...

  6. 剑指Offer - 九度1509 - 树中两个结点的最低公共祖先

    剑指Offer - 九度1509 - 树中两个结点的最低公共祖先2014-02-07 01:04 题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样 ...

  7. 剑指Offer - 九度1360 - 乐透之猜数游戏

    剑指Offer - 九度1360 - 乐透之猜数游戏2014-02-05 19:54 题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备 ...

  8. android压力测试monkey简单使用

    monkey是android sdk自带的压力测试工具,简单使用如下: 用adb shell进入adb shell环境后,用下面命令进行测试 monkey -p com.xxx.yyy -v num ...

  9. vue零碎收集

    在组件中创建dom: let a=document.querySelector('.test'); let newDom=document.createElement("div") ...

  10. ironic images

    参考: http://blog.csdn.net/wanghuiict/article/details/52757359 ironic 整个部署流程中有两组映像,分别是 deploy 映像和 user ...