【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】[国家集训队]排队的更多相关文章

  1. Luogu-1975 [国家集训队]排队

    Luogu-1975 [国家集训队]排队 题面 Luogu-1975 题解 题意:给出一个长度为n的数列以及m个交换两个数的操作,问每次操作后逆序对数量 时间,下标和数的大小三维偏序,,,把交换操作看 ...

  2. [国家集训队]排队 [cdq分治]

    题面 洛谷 和动态逆序对那道题没有什么区别 把一个交换换成两个删除和两个插入 #include <cstdio> #include <cstdlib> #include < ...

  3. luogu1975 [国家集训队]排队

    思路 序列中 |i | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| |----|--|--|--|--|--|--|--|--|--|--| |a[i]| a| b| c| L| d ...

  4. P1975 [国家集训队]排队

    题目链接 题意分析 我们考虑 交换两个数\([le,ri]\)的贡献 减少的逆序对数\([le,ri]\)中小于\(num[le]\)以及大于\(num[ri]\)的数 增加的\([le,ri]\)中 ...

  5. P1975 [国家集训队]排队 线段树套平衡树维护动态逆序对查询

    $ \color{#0066ff}{ 题目描述 }$ 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍 ...

  6. 洛谷 P1975 [国家集训队]排队 Lebal:块内排序+树状数组

    题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...

  7. [luoguP1975] [国家集训队]排队(分块)

    传送门 直接暴力分块,然后在每一个块内排序. 查询时可以在每一个块内二分. #include <cmath> #include <cstdio> #include <io ...

  8. 「Luogu P1975 [国家集训队]排队」

    题目大意 给出一个序列 \(h\),支持交换其中的两数,求出每一时刻的逆序对个数. 分析 求逆序对是 \(O(N\log_2N)\) 的,有 \(M\) 个操作,如果暴力求的话时间复杂度就是 \(O( ...

  9. BZOJ 2039: [2009国家集训队]employ人员雇佣

    2039: [2009国家集训队]employ人员雇佣 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1369  Solved: 667[Submit ...

随机推荐

  1. 「bzoj 3944: Sum」

    题目 杜教筛板子了 #include<iostream> #include<cstring> #include<cstdio> #include<cmath& ...

  2. 快速理解Event事件

    浏览器事件是所有web程序的核心.javascript与HTML之间的交互是通过事件实现的.通过这些事件我们定义将要发生的行为.事件是一种异步编程的实现方式,本质上是程序各个组成部分之间的通信. 1. ...

  3. es6之decorator

    //decorator //第三方库为:core-decorators //以下为代码实例 { //decorator //修饰器是一个函数 //是修改一个行为 //修改一个类的行为 console. ...

  4. TestNG注解使用技巧 - @Factory

    之前在测试中一直使用testNG的@Test注解都很顺利没有碰到什么问题,今天突然遇到@Test不能用的情况,运行后提示: org.testng.TestNGException: Can't invo ...

  5. STM32F103 ucLinux开发之一(BOOT分析及源码)

    STM32F103 ucLinux开发BOOT STM3210E-EVAL官方开发板主芯片STM32F103ZET6: 片内512K Flash,地址0x0800 0000 ~ 0x0807 FFFF ...

  6. spring boot从redis取缓存发生java.lang.ClassCastException异常

    目录树 异常日志信息 错误原因 解决方法 异常日志信息 2018-09-24 15:26:03.406 ERROR 13704 --- [nio-8888-exec-8] o.a.c.c.C.[.[. ...

  7. duplicate symbols for architecture arm64 导入的类库字符重复

    这个错误大部分时候是引用库重复定义的问题. 项目需要,同时引用ZBar和QQ授权登录SDK,由于二者均使用了Base64处理数据,XCode编译时报错: duplicate symbol _base6 ...

  8. ztree的用法

    哎哟,好久没写什么这个虽然没人看的博客了,其实这段时间情绪非常低谷,就没有写博客了,不过我还是回来了,回到正题,在前端页面编程的时候,我们经常用到树状,用原始的树状呢,功能没有那么强大,所以这个时候 ...

  9. Java : logback简单配置

    需要把logback.xml文件放在类路径下,如果是spring boot项目可以用 logging.config=classpath:log/xxxxxx.xml来指定配置文件 logback la ...

  10. PHP通过curl向其它服务器发请求并返回数据

    在很多时候,我们都需要请求第三方的服务器来获取一些数据,比如token,比如百度的主动推送,那么我们的php如何实现向第三方服务器发请求呢?我们可以通过curl来实现 首先定义请求的url,然后创建h ...