题面

洛谷

和动态逆序对那道题没有什么区别

把一个交换换成两个删除和两个插入

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
const double eps = 1e-9;
const int inf = 0x3f3f3f3f;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
struct Node{
int x, y, z, d, cnt;
}node[N];
bool rule_yzx(Node x, Node y){
if(x.y != y.y) return x.y < y.y;
if(x.z != y.z) return x.z < y.z;
return x.x < y.x;
}
int n, m, nsize, a[N], b[N];
int tim, dfn[N];
long long ans[N];
struct BIT{
int w[N];
void ins(int x, int d){while(x<=n){w[x] += d; x += x & -x;}}
int qry(int x){int res = 0; while(x){res += w[x]; x -= x & -x;}return res;}
void print(){for(int i = 1; i <= n; ++i) printf("%d", w[i]); printf("\n");}
}bit; inline void add(int x1, int x2, int x3, int x4){
++nsize;
node[nsize].x = x1, node[nsize].y = x2, node[nsize].z = x3, node[nsize].d = x4;
} void cdq(int L, int R){
if(L == R) return ;
int mid = L + ((R - L) >> 1);
cdq(L, mid); cdq(mid + 1, R);
sort(node + L, node + mid + 1, rule_yzx);
sort(node + mid + 1, node + R + 1, rule_yzx);
int j = L;
for(int i = mid + 1; i <= R; ++i){
while(j <= mid && node[j].y <= node[i].y){
bit.ins(node[j].z, node[j].d); ++j;
}
node[i].cnt += node[i].d * (bit.qry(n) - bit.qry(node[i].z));
}
//printf("L %d R %d\n", L, R);
//bit.print();
while(j > L){--j; bit.ins(node[j].z, -node[j].d);}
j = mid;
for(int i = R; i >= mid + 1; --i){
while(j >= L && node[j].y >= node[i].y){
bit.ins(node[j].z, node[j].d); --j;
}
node[i].cnt += node[i].d * bit.qry(node[i].z - 1);
}
while(j < mid){++j; bit.ins(node[j].z, -node[j].d);}
} int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]); b[i] = a[i];
}
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; ++i){
a[i] = (lower_bound(b + 1, b + n + 1, a[i]) - b);
add(0, i, a[i], 1);
//printf("%d %d %d\n", node[i].x, node[i].y, node[i].z);
}
scanf("%d", &m);
for(int i = 1, x, y; i <= m; ++i){
scanf("%d%d", &x, &y);
add(++tim, x, a[x], -1);
add(++tim, y, a[y], -1);
swap(a[x], a[y]);
add(++tim, x, a[x], 1);
add(++tim, y, a[y], 1);
dfn[i] = tim;
}
//for(int i = 1; i <= nsize; ++i) printf("%d %d %d\n", node[i].x, node[i].y, node[i].z);
//已经是升序了
cdq(1, nsize);
for(int i = 1; i <= nsize; ++i){
//printf("%d %d %d %d\n", node[i].x, node[i].y, node[i].z, node[i].cnt);
ans[node[i].x] += node[i].cnt;
}
for(int i = 1; i <= tim; ++i) ans[i] += ans[i - 1];
for(int i = 0; i <= m; ++i) printf("%d\n", ans[dfn[i]]);
system("PAUSE");
return 0;
}

[国家集训队]排队 [cdq分治]的更多相关文章

  1. 【LG1975】[国家集训队]排队

    [LG1975][国家集训队]排队 题面 洛谷 题解 又是一个偏序问题 显然\(CDQ\) 交换操作不好弄怎么办? 可以看成两次删除两次插入 排序问题要注意一下 代码 #include <ios ...

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

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

  3. BZOJ 2141: 排队 [CDQ分治]

    题意: 交换序列中两个元素,求逆序对 做分块做到这道题...一看不是三维偏序嘛.... 作为不会树套树的蒟蒻就写CDQ分治吧.... 对时间分治...x排序...y树状数组... 交换拆成两个插入两个 ...

  4. bzoj 2141 : 排队 (cdq分治+bit)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2141 思路: 其实就是求动态逆序对...cdq降维,用树状数组前后求两遍逆序对就好了 切水 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. MYSQL 创建数据库SQL

    CREATE DATABASE crm CHARACTER SET utf8 COLLATE utf8_general_ci; MySQL :: MySQL 5.7 Reference Manual ...

  2. [转帖]Nginx rewrite模块深入浅出详解

    Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...

  3. vue的定位

    高德定位 https://blog.csdn.net/YY110621/article/details/87921605(copy) 话不多说,直接写方法步骤,需要的直接拿去放在自己项目中即可使用先看 ...

  4. class面向对象-1

    一.基本定义 class cl(object): def __init(self,var) self.var=var def func(self,i) print('%s is in %s'%(i,s ...

  5. Springboot中使用Xstream进行XML与Bean 相互转换

    在现今的项目开发中,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行使用,正常的老式方法是通过获取节点来进行一系列操作,个人感觉太过于复杂.繁琐.推荐一套简单的 ...

  6. 如何在MAC上运行exe程序

    1. 首先下载并运行CrossOver 运行CrossOver需要收费,试用期为14天,运行CrossOver 2. 选择exe应用程序,新建容器,安装exe程序 3.安装成功后,运行exe应用程序启 ...

  7. layui内部使用jQuery

    layui是基于jQuery的框架,本身自带jQuery 根据官方推荐,是使用自带的好一点 这里记一下内部使用jQuery的方法: layui.use('jquery', function(){ va ...

  8. fiddler 笔记-设置断点

    设置断点后,可以修改httprequest的任何信息包括:host,cookie或都表单中的数据 1 Fiddler--rules--Automatic Breakpoint --before Req ...

  9. SQL Server 数据库try catch 存储过程

    SQL Server 在生产环境中这样写存储过程的坑都避免了吗? 原文链接: http://www.cnblogs.com/chenmh/p/7856777.html 概述 最近因为业务的需求写了一段 ...

  10. Fourier Transform Complex Conjugate Discussion

    FT of function $f(t)$ is to take integration of the product of $f(t)$ and $e^{-j\Omega t}$. By separ ...