传送门

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

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

#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. Airflow 调度基础

    1. Airflow Airflow是一个调度.监控工作流的平台.用于将一个工作流制定为一组任务的有向无环图(DAG),并指派到一组计算节点上,根据相互之间的依赖关系,有序执行. 2. 安装 pip安 ...

  2. XGBoost算法原理小结

    在两年半之前作过梯度提升树(GBDT)原理小结,但是对GBDT的算法库XGBoost没有单独拿出来分析.虽然XGBoost是GBDT的一种高效实现,但是里面也加入了很多独有的思路和方法,值得单独讲一讲 ...

  3. SpringBoot之YAML

    SpringBoot的配置文件有两种,一种是properties结尾的,一种是以yaml或yml文件结尾的 我们讨论一下yml文件结尾的文件: 基本语法: 其实yml文件就是键值对的形式,不过就是键( ...

  4. pycharm clion rider 注册

    JetBrains 公司出品的pycharm clion rider 专业版本都需要注册才能运行,这里有个免费注册方法: JetBrains授权服务器2017.10.7授权方法:激活时选择Licens ...

  5. 十九、MySQL GROUP BY 语句

    MySQL GROUP BY 语句 GROUP BY 语句根据一个或多个列对结果集进行分组. 在分组的列上我们可以使用 COUNT, SUM, AVG,等函数. GROUP BY 语法 SELECT ...

  6. win10下安装mysql-5.7.23-winx64

    Step1 官方下载地址 https://dev.mysql.com/downloads/mysql/ 选择手动下载版本 解压到自己指定的路径 上图中的my.ini及data文件夹在压缩包里是没有的, ...

  7. SpringBoot AOP综合例子

    完整源码:https://github.com/947133297/cgLibDemo 通过AOP来便捷地输出日志,能更加方便排查系统的bug,这个例子中简单输出自定义文件和函数执行时的参数,函数要不 ...

  8. web项目中无法开启或404

    404找不到页面,可能是spring的bean自动注入有了问题,例如org.springframework.beans.factory.BeanCreationException:可以检查配置文件的s ...

  9. CDOJ:1636-梦后楼台高锁,酒醒帘幕低垂(Kruskal+暴力)

    梦后楼台高锁,酒醒帘幕低垂 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...

  10. 【Todo】 cygwin下emacs中M-x shell 中出现乱码