【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. 巧用padding生成正方形DIV

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Java中父类强制转换为子类的可能

    之前徒弟问了一个问题, 在Java中, 父类对象到底能不能转换成对应的子类对象? 到底能不能, 今天就来说说这个问题, 先看下面一段代码: package cn.com.hanbinit.test; ...

  3. scanf函数读取缓冲区数据的问题

    标准I\O的缓冲类型 标准I\O根据不同的应用需求,提供了全缓冲.行缓冲.无缓冲三种缓冲方式. 全缓冲:只有当划定的缓冲区被填满或者数据读取至末尾时,才开始执行I\O操作(执行系统提供的read\wr ...

  4. 使用ViewPager和FragmentPagerAdapter实现Tab

    前面我们分别利用ViewPager和Fragment实现了Tab效果.但是使用Fragment实现的Tab不能够左右滑动.如果我们既想使用Fragment又想让Tab能够滑动,那么怎么办呢?这 就是今 ...

  5. 从零一起学Spring Boot之LayIM项目长成记(一) 初见 Spring Boot

    项目背景 之前写过LayIM的.NET版后端实现,后来又写过一版Java的.当时用的是servlet,websocket和jdbc.虽然时间过去很久了,但是仍有些同学在关注.偶然间我听说了Spring ...

  6. canny算子求图像边缘,edgebox那部分

    过程: 1.      彩色图像转换为灰度图像    2.      对图像进行高斯模糊    3.      计算图像梯度,根据梯度计算图像边缘幅值与角度(这里其实用到了微分边缘检测算子来计算梯度幅 ...

  7. 记录一次Git问题及其解决方案

    错误信息:fatal: refusing to merge unrelated histories 错误产生背景:我将原先测试的项目本地删除后提交,然后将新的项目按照git的提交步骤进行提交,在最后一 ...

  8. Pyplot教程(深度学习入门3)

    源地址:http://matplotlib.org/users/pyplot_tutorial.html .caret, .dropup > .btn > .caret { border- ...

  9. 关于iframe里的子页面如何调取父级页面里的事件(子调父)

    在子页面里面的事件里写 self.parent.window.父级函数名('参数名'); 父级里面直接写函数. js中的parent.top.self的含义. js中经常看到window.parent ...

  10. javaWeb中怎么获取提交表单里面的值

    在javaWeb中如何获得html文件中的表单里面的值? <!DOCTYPE html> <html> <head> <meta charset=" ...