传送门

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

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

#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;
}

  

最新文章

  1. dom4j解析示例
  2. Apk去掉签名以及重新签名的方法
  3. C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别 (转)
  4. iOS 关于Layer的疑问
  5. (转)mysql账号权限密码设置方法
  6. bootstrap与jqueryui按钮冲突的解决
  7. 【转】@Override must override a superclass method 问题解决
  8. JS中window.document对象
  9. QML中多样化的ListModel(MultiDelegate)
  10. crontab 的例子
  11. LPC1768外部中断与GPIO中断
  12. Aspose实现Office转PDF (ASP.NET)
  13. 微信小程序去除Button默认样式
  14. mina statemachine解读(一)
  15. Myeclipse 添加插件
  16. Centos 7最小化Mongodb部署操作
  17. linux上安装字体
  18. s3存储桶:s3可扩展的云存储
  19. 强化学习基础算法入门 【PPT】
  20. NX 栈不可执行的绕过方式--ROP链

热门文章

  1. python_41_with语句
  2. Python实现屏蔽敏感词
  3. web项目小总结
  4. 巧妙使用JQuery Clone 添加多行数据,并更新到数据库
  5. 自己写一个Promise
  6. react的redux中间件
  7. GIT 团队协作快速入门使用
  8. 读取properties的简单方法,使用@Configuration
  9. CentOS---zookeeper安装(单机、伪集群、集群)
  10. Nginx http重定向到https时,post请求变为get的问题