传送门

直接暴力分块,然后在每一个块内排序。

查询时可以在每一个块内二分。

#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#define M 210
#define N 20101 using namespace std; int n, m, t, S, C, ans;
int a[N], b[N], c[N], st[M], ed[M], belong[N], sorted[M][M], len[M]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline int query(int x)
{
int ret = 0;
for(; x; x -= x & -x) ret += c[x];
return ret;
} inline void add(int x)
{
for(; x <= m; x += x & -x) c[x]++;
} inline void init()
{
int i, j;
S = sqrt(n);
for(i = 1; i <= n; i += S)
{
st[++C] = i;
ed[C] = min(i + S - 1, n);
len[C] = ed[C] - st[C] + 1;
}
for(i = 1; i <= C; i++)
{
for(j = st[i]; j <= ed[i]; j++)
belong[j] = i, sorted[i][j - st[i] + 1] = a[j];
sort(sorted[i] + 1, sorted[i] + len[i] + 1);
}
} inline void solve(int x, int y)
{
if(x > y) swap(x, y);
int i, l = belong[x], r = belong[y], tmp;
if(l == r)
for(i = x + 1; i < y; i++)
{
ans -= (a[i] < a[x]);
ans += (a[i] > a[x]);
ans -= (a[i] > a[y]);
ans += (a[i] < a[y]);
}
else
{
for(i = l + 1; i < r; i++)
{
ans -= lower_bound(sorted[i] + 1, sorted[i] + len[i] + 1, a[x]) - sorted[i] - 1;
ans += len[i] - (upper_bound(sorted[i] + 1, sorted[i] + len[i] + 1, a[x]) - sorted[i]) + 1;
ans -= len[i] - (upper_bound(sorted[i] + 1, sorted[i] + len[i] + 1, a[y]) - sorted[i]) + 1;
ans += lower_bound(sorted[i] + 1, sorted[i] + len[i] + 1, a[y]) - sorted[i] - 1;
}
for(i = x + 1; i <= ed[l]; i++)
{
ans -= (a[i] < a[x]);
ans += (a[i] > a[x]);
ans -= (a[i] > a[y]);
ans += (a[i] < a[y]);
}
for(i = st[r]; i < y; i++)
{
ans -= (a[i] < a[x]);
ans += (a[i] > a[x]);
ans -= (a[i] > a[y]);
ans += (a[i] < a[y]);
}
}
if(a[x] > a[y]) ans--;
if(a[x] < a[y]) ans++;
swap(a[x], a[y]);
for(i = st[l]; i <= ed[l]; i++) sorted[l][i - st[l] + 1] = a[i];
for(i = st[r]; i <= ed[r]; i++) sorted[r][i - st[r] + 1] = a[i];
sort(sorted[l] + 1, sorted[l] + len[l] + 1);
sort(sorted[r] + 1, sorted[r] + len[r] + 1);
} int main()
{
int i, x, y;
n = read();
for(i = 1; i <= n; i++) a[i] = b[i] = read();
sort(b + 1, b + n + 1);
m = unique(b + 1, b + n + 1) - b - 1;
for(i = 1; i <= n; i++)
{
a[i] = lower_bound(b + 1, b + m + 1, a[i]) - b;
ans += i - query(a[i]) - 1, add(a[i]);
}
printf("%d\n", ans);
init();
t = read();
while(t--)
{
x = read();
y = read();
solve(x, y);
printf("%d\n", ans);
}
return 0;
}

  

[luoguP1975] [国家集训队]排队(分块)的更多相关文章

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

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

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

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

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

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

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

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

  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. 「Luogu P1975 [国家集训队]排队」

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

  9. BZOJ2038: [2009国家集训队]小Z的袜子(hose) -- 莫队算法 ,,分块

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 3577  Solved: 1652[Subm ...

随机推荐

  1. 2017.12.13 Java中是怎样通过类名,创建一个这个类的数组

    先在类方法中定义数组的方法: public int[] method6(int[] arr){ for(int i = 0; i<arr.length;i++){ arr[i] = (int)( ...

  2. 使用webpack从零开始搭建react项目

    webpack中文文档 webpack的安装 yarn add webpack@3.10.1 --dev 需要处理的文件类型 webpack常用模块 webpack-dev-server yarn a ...

  3. CYUSB

    /*Summary The application cydesc is used to open the device with cypress GUID and get the device des ...

  4. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第三节

    原文链接 第三节:错误处理和全局内存性能局限 恭喜!通过对CUDA(Compute Unified DeviceArchitecture,即计算统一设备架构的首字母缩写)系列文章第一节和第二节,您现在 ...

  5. latex-word

    http://blog.sina.com.cn/s/blog_565e747c0100qxma.html 附:PowerPoint 中插入LaTeX公式的插件,IguanaTex,功能和TeXsWor ...

  6. wing ide 注释

    ctrl-.是批量注视 ctrl->向前缩进 ctrl-<向后缩进

  7. Java - BigDecimal四舍五入注意事项

    如上图,精度后只有一位时,是五舍六入.     如上图,精度后只有第一位不为0时,也是五舍六入.     如上图,精度后至少有两位不为0时,才是四舍五入.  

  8. javascript的基本类型和引用类型

    一.基本类型和引用类型 基本的数据类型有5个:undefined,boolean,number,string,null ? 1 2 3 4 5 typeof null; //"object& ...

  9. Linux dmidecode命令

    1.linux系统自带的dmidecode工具查询服务器硬件信息 dmidecode 用于获取服务器的硬件信息,通常是在不打开计算机机箱的情况下使用该命令来查找硬件详细信息 这个命令可以查看内存的几乎 ...

  10. JavaScript算数

    常数                              Math.E 圆周率                           Math.PI 2的平方根                  ...