【LG1975】[国家集训队]排队
【LG1975】[国家集训队]排队
题面
题解
又是一个偏序问题
显然\(CDQ\)
交换操作不好弄怎么办?
可以看成两次删除两次插入
排序问题要注意一下
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
namespace IO {
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
inline char gc() {
if (is == it) it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return *is++;
}
}
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (ch != '-' && (ch > '9' || ch < '0')) ch = IO::gc();
if (ch == '-') w = -1 , ch = IO::gc();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = IO::gc();
return w * data;
}
const int MAX_N = 22005;
struct Node { int x, y, z, w, id; } t[MAX_N << 2], tmp[MAX_N << 2];
bool cmp_y(Node a, Node b) { return (a.y == b.y) ? (a.z < b.z) : (a.y < b.y); }
int N, M, tot, X[MAX_N], a[MAX_N], c[MAX_N], ans[MAX_N];
inline int lb(int x) { return x & -x; }
void add(int x, int v) { while (x <= N) c[x] += v, x += lb(x); }
int sum(int x) { int res = 0; while (x > 0) res += c[x], x -= lb(x); return res; }
void Div(int l, int r) {
if (l == r) return ;
int mid = (l + r) >> 1;
for (int i = l; i <= r; i++)
if (t[i].x <= mid) add(t[i].z, t[i].w);
else ans[t[i].id] += t[i].w * (sum(N) - sum(t[i].z));
for (int i = l; i <= r; i++)
if (t[i].x <= mid) add(t[i].z, -t[i].w);
for (int i = r; i >= l; i--)
if (t[i].x <= mid) add(t[i].z, t[i].w);
else ans[t[i].id] += t[i].w * sum(t[i].z - 1);
for (int i = l; i <= r; i++) if (t[i].x <= mid) add(t[i].z, -t[i].w);
int t1 = l - 1, t2 = mid;
for (int i = l; i <= r; i++)
if (t[i].x <= mid) tmp[++t1] = t[i];
else tmp[++t2] = t[i];
for (int i = l; i <= r; i++) t[i] = tmp[i];
Div(l, mid); Div(mid + 1, r);
}
int main () {
N = gi();
for (int i = 1; i <= N; i++) X[i] = a[i] = gi();
sort(&X[1], &X[N + 1]); int size = unique(&X[1], &X[N + 1]) - X - 1;
for (int i = 1; i <= N; i++) a[i] = lower_bound(&X[1], &X[size + 1], a[i]) - X;
for (int i = 1; i <= N; i++) t[++tot] = (Node){tot, i, a[i], 1, 0};
N = size; M = gi();
for (int i = 1; i <= M; i++) {
int x = gi(), y = gi();
t[++tot] = (Node){tot, x, a[x], -1, i};
t[++tot] = (Node){tot, y, a[y], -1, i};
t[++tot] = (Node){tot, x, a[y], +1, i};
t[++tot] = (Node){tot, y, a[x], +1, i};
swap(a[x], a[y]);
}
sort(&t[1], &t[tot + 1], cmp_y);
Div(1, tot);
printf("%d\n", ans[0]);
for (int i = 1; i <= M; i++) printf("%d\n", ans[i] += ans[i - 1]);
return 0;
}
【LG1975】[国家集训队]排队的更多相关文章
- Luogu-1975 [国家集训队]排队
Luogu-1975 [国家集训队]排队 题面 Luogu-1975 题解 题意:给出一个长度为n的数列以及m个交换两个数的操作,问每次操作后逆序对数量 时间,下标和数的大小三维偏序,,,把交换操作看 ...
- [国家集训队]排队 [cdq分治]
题面 洛谷 和动态逆序对那道题没有什么区别 把一个交换换成两个删除和两个插入 #include <cstdio> #include <cstdlib> #include < ...
- luogu1975 [国家集训队]排队
思路 序列中 |i | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| |----|--|--|--|--|--|--|--|--|--|--| |a[i]| a| b| c| L| d ...
- P1975 [国家集训队]排队
题目链接 题意分析 我们考虑 交换两个数\([le,ri]\)的贡献 减少的逆序对数\([le,ri]\)中小于\(num[le]\)以及大于\(num[ri]\)的数 增加的\([le,ri]\)中 ...
- P1975 [国家集训队]排队 线段树套平衡树维护动态逆序对查询
$ \color{#0066ff}{ 题目描述 }$ 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍 ...
- 洛谷 P1975 [国家集训队]排队 Lebal:块内排序+树状数组
题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...
- [luoguP1975] [国家集训队]排队(分块)
传送门 直接暴力分块,然后在每一个块内排序. 查询时可以在每一个块内二分. #include <cmath> #include <cstdio> #include <io ...
- 「Luogu P1975 [国家集训队]排队」
题目大意 给出一个序列 \(h\),支持交换其中的两数,求出每一时刻的逆序对个数. 分析 求逆序对是 \(O(N\log_2N)\) 的,有 \(M\) 个操作,如果暴力求的话时间复杂度就是 \(O( ...
- BZOJ 2039: [2009国家集训队]employ人员雇佣
2039: [2009国家集训队]employ人员雇佣 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1369 Solved: 667[Submit ...
随机推荐
- 学习WebSocket一(WebSocket初识)
Java EE 7 去年刚刚发布了JSR356规范,使得WebSocket的Java API得到了统一,Tomcat从7.0.47开始支持JSR356,这样一来写WebSocket的时候,所用的代码都 ...
- 文件是数据(字节)流的抽象-为什么C++中会把文件操作抽象为fstream?
这不过是返祖罢了.正确的问题是为什么会把数据流抽象成文件. 设备-字节流-文件. 一切皆为文件,所有不同种类的类型都被抽象成文件(比如:块设备,socket套接字,pipe队列). 文件抽象为数据流一 ...
- mac zsh 快捷定位文件
brew install zsh vim ~/.zshrc plugins=(git autojump zsh-autosuggestions zsh-syntax-highlighting yarn ...
- Telnet配置
一.环境 路由 IP:192.168.56.2 本地云 IP:192.168.56.1 二.认证模式 AAA模式 认证 授权 计费的安全技术 当配置用户界面的认证方式为AAA时, 用户登录设备时需要首 ...
- springboot+mybatis+shiro——登录认证和权限控制
转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...
- Redis口令设置
./redis-cli -h 192.168.128.131 -p 6379 #指定IP和端口启动对应的Redis服务 config set requirepass yourPassword #设置令 ...
- mybatis会对多参数方法进行特殊处理
例如:查询id=1,name=tom的一条数据 查询接口: User getUserByIdAndName(Integer id,String name); // <?xml version=& ...
- 二十一、IntelliJ IDEA 控制台输出中文乱码问题的解决方法
首先,找到 IntelliJ IDEA 的安装目录,进入bin目录下,定位到idea.vmoptions文件,如下图所示: 双击打开idea.vmoptions文件,如下图所示: 然后,在其中追加-D ...
- ARM v7-A 系列CPU的MMU隐射分析
ARM v7-A 系列CPU的MMU隐射分析 摘要:ARM v7-A系列的CPU加入了很多扩展,如多核处理器扩展.大物理地址扩展.TrustZone扩展.虚拟化扩展.若支持大的物理地址,则必须支持多核 ...
- 数据库——MySQL——事务
数据的事务是指作为单个逻辑工作单元执行的一系列操作,要么完全执行,要么完全不执行. 事务必须具备四个特性: 原子性 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚 一致性 在事务T开始时, ...