【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 ...
随机推荐
- openlayers中的自定制工具栏,包含画点、线、面
先是在projectquantan-master这个项目中有一个EditingPanel这个工具条,也挺好的,功能挺全的,但是有一点就是只有画多边形的一个按钮,没有point和path俩个的,所以就想 ...
- centos中java安装跟配置
安装配置java环境 [root@JxateiLinux src]# Wget http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c ...
- x+=i和x = x+i比较 -- 简单赋值和复合赋值
这两个赋值方式其实是有区别的,如果最后结果的类型和左操作数的类型一样,那么这两个表达式就完全等价. 下面来看看两个例子来理解它们的区别: 编写一个程序,使得x+=i合法, x = x+i: 不合法. ...
- Matplotlib中中文不显示问题
我们在使用jupter进行数据分析的时候,会接触到Matplotlib这个库,它是用来进行可视化数据分析的,在一个图中,我们常常会加入一些中文来进行说明.当我们加入中文的时候会出现下图所示的样子: 可 ...
- 课时49.非input标签(掌握)
1.select标签(下拉列表) 什么是下拉列表? 这就是下拉列表 作用: 用于定义下拉列表 格式: <select> <option>列表数据</option> ...
- OC - 时间日期类NSDate
OC - 时间日期类NSDate //NSDate 时间日期类 NSDate 二进制数据流 { //1.获取当前时间 零时区的时间 //显示的是格林尼治的时间: 年-月-日 时:分:秒:+时区 NSD ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 044-45
记录今日刷题 ------------------------------------------------L1-044--------------------------------------- ...
- WSO2 API Manager 替换mysql作为数据库,解决AuthorizationUtils Could not set authorizations for the root问题
按照wso2官网(https://docs.wso2.com/display/ADMIN44x/Changing+to+MySQL)配置AM的数据库,想从H2换成Mysql5.7,费了将近一天的时间, ...
- Django学习笔记4-csrf防护
1.CSRF验证失败. 请求被中断. 原因是django为了在用户提交表单时防止跨站攻击所做的保护 什么是 CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求 ...
- Enable directory listing on Nginx Web Server
1:Test environment [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core ...