BZOJ 3289 (莫队算法+树状数组)
2016-04-28 00:18
246 查看
3289: Mato的文件管理
Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 1671 Solved: 732
[Submit][Status][Discuss]
Description
Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号。为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能访问。Mato每天随机选一个区间[l,r],他今天就看编号在此区间内的这些资料。Mato有一个习惯,他总是从文件大小从小到大看资料。他先把要看的文件按编号顺序依次拷贝出来,再用他写的排序程序给文件大小排序。排序程序可以在1单位时间内交换2个相邻的文件(因为加密需要,不能随机访问)。Mato想要使文件交换次数最小,你能告诉他每天需要交换多少次吗?Input
第一行一个正整数n,表示Mato的资料份数。第二行由空格隔开的n个正整数,第i个表示编号为i的资料的大小。
第三行一个正整数q,表示Mato会看几天资料。
之后q行每行两个正整数l、r,表示Mato这天看[l,r]区间的文件。
Output
q行,每行一个正整数,表示Mato这天需要交换的次数。Sample Input
41 4 2 3
2
1 2
2 4
Sample Output
02
HINT
Hintn,q <= 50000
样例解释:第一天,Mato不需要交换
第二天,Mato可以把2号交换2次移到最后。
对于已有的区间可以用一个树状数组维护数字的个数,然后增加一个数或者减少一个数
只需要统计下区间里面比他大的数字个数。注意在区间r和l改变是有区别的,不过因为
没有相同的数字可以通过区间长度减去比他大的数得出比他小的个数。
复杂度(n*lgn*sqrt(n))。
坑:需要离散化。
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> #include <queue> #include <cmath> using namespace std; #define maxn 51111 #define mod 1000000007 int pos[maxn]; int n, m; long long c[maxn]; long long ans[maxn], tmp[maxn]; struct node { int l, r, id; bool operator < (const node &a) const { return pos[l] < pos[a.l] || (pos[l] == pos[a.l] && r < a.r); } long long ans; } p[maxn]; struct data { int num; int id; bool operator < (const data &a) const { return num < a.num; } } a[maxn]; long long cur; int lowbit (int x) { return x&(-x); } int r, l; void add (int pos, int op) { for (int i = a[pos].num; i > 0; i -= lowbit (i)) c[i]++; int ans = 0; for (int i = a[pos].num+1; i <= n; i += lowbit (i)) ans += c[i]; if (op == 1) cur += ans; else cur += (r-l-ans); } void del (int pos, int op) { for (int i = a[pos].num; i > 0; i -= lowbit (i)) c[i]--; int ans = 0; for (int i = a[pos].num+1; i <= n; i += lowbit (i)) ans += c[i]; if (op == 0) cur -= (r-l-ans); else cur -= ans; } bool cmp (const data &a, const data &b) { return a.id < b.id; } int main () { // freopen ("in.txt", "r", stdin); while (scanf ("%d", &n) == 1) { for (int i = 1; i <= n; i++) { scanf ("%d", &a[i].num); a[i].id = i; } sort (a+1, a+1+n); for (int i = 1; i <= n; i++) { a[i].num = i; } sort (a+1, a+1+n, cmp); int block = ceil (sqrt (n*1.0)); for (int i = 1; i <= n; i++) pos[i] = (i-1)/block; scanf ("%d", &m); for (int i = 0; i < m; i++) { scanf ("%d%d", &p[i].l, &p[i].r); p[i].id = i; } sort (p, p+m); l = 1, r = 1; cur = 0; memset (c, 0, sizeof c); add (1, 1); cur = 0; for (int i = 0; i < m; i++) { while (r > p[i].r) { del (r, 1); r--; } while (r < p[i].r) { r++; add (r, 1); } while (l > p[i].l) { l--; add (l, 0); } while (l < p[i].l) { del (l, 0); l++; } ans[p[i].id] = cur; } for (int i = 0; i < m; i++) { printf ("%lld\n", ans[i]); } } return 0; }
相关文章推荐
- 莱特币开发教程
- CF初体验---Round #348,C
- hibernate使用Query进行查询
- java问题
- centos启动流程及GRUB
- 【贪心+堆】XMU 1584 小明的烦恼
- oc总结 --oc基础语法相关知识
- WordPress工作原理之程序文件执行顺序
- 虚拟币开发教程
- leetcode 36. Valid Sudoku-有效数独|哈希表
- 推公式解决的题目 hdu2018
- 如何准备阿里社招面试,顺谈Java程序员学习中各阶段的建议
- Python学习之字符串
- 快排 随机分割
- nginx配置性能优化笔记
- Linux服务器安全之用户密钥认证登录(基于CentOS 7.0系统)
- python爬取博客标题和访问量
- HDU 3446 有贪心思想的01背包
- meteor培训二
- 数据探索-特征性分析