「CQOI2011」动态逆序对
「CQOI2011」动态逆序对
传送门
树套树。
删除一个位置的元素带来的减损数等于他前面大于它的和后面小于它的,然后这个直接树状数组套主席树维护一下就好了。
参考代码:
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
typedef long long LL;
const int _ = 1e5 + 5, __ = 1e7 + 5;
int n, q, a[_], pos[_];
int tot, rt[_], lc[__], rc[__], t1[_], t2[_]; LL cnt[__];
inline void update(int& p, int x, int v, int l = 1, int r = n) {
if (!p) p = ++tot; cnt[p] += v;
if (l == r) return ;
int mid = (l + r) >> 1;
if (x <= mid) update(lc[p], x, v, l, mid);
else update(rc[p], x, v, mid + 1, r);
}
inline LL Query(int l, int r, int x, int opt) {
int c1 = 0, c2 = 0; --l;
for (rg int i = l; i >= 1; i -= i & -i) t1[++c1] = rt[i];
for (rg int i = r; i >= 1; i -= i & -i) t2[++c2] = rt[i];
l = 1, r = n;
LL res = 0;
while (l < r) {
int mid = (l + r) >> 1;
if (x <= mid) {
if (opt == 0) {
for (rg int i = 1; i <= c1; ++i) res -= cnt[rc[t1[i]]];
for (rg int i = 1; i <= c2; ++i) res += cnt[rc[t2[i]]];
}
for (rg int i = 1; i <= c1; ++i) t1[i] = lc[t1[i]];
for (rg int i = 1; i <= c2; ++i) t2[i] = lc[t2[i]];
r = mid;
} else {
if (opt == 1) {
for (rg int i = 1; i <= c1; ++i) res -= cnt[lc[t1[i]]];
for (rg int i = 1; i <= c2; ++i) res += cnt[lc[t2[i]]];
}
for (rg int i = 1; i <= c1; ++i) t1[i] = rc[t1[i]];
for (rg int i = 1; i <= c2; ++i) t2[i] = rc[t2[i]];
l = mid + 1;
}
}
return res;
}
int main() {
read(n), read(q);
LL ans = 0;
for (rg int i = 1; i <= n; ++i) {
read(a[i]), pos[a[i]] = i;
ans += Query(1, i - 1, a[i], 0);
for (rg int j = i; j <= n; j += j & -j) update(rt[j], a[i], 1);
}
for (rg int x; q--; ) {
printf("%lld\n", ans);
read(x);
ans -= Query(1, pos[x] - 1, x, 0);
ans -= Query(pos[x] + 1, n, x, 1);
for (rg int j = pos[x]; j <= n; j += j & -j) update(rt[j], x, -1);
}
return 0;
}
「CQOI2011」动态逆序对的更多相关文章
- 【CQOI2011】动态逆序对 BZOJ3295
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
- BZOJ 3295 【Cqoi2011】 动态逆序对
Description 对于序列\(A\),它的逆序对数定义为满足\(i<j\),且\(A_i>A_j\)的数对\((i,j)\)的个数.给\(1\)到\(n\)的一个排列,按照某种顺序依 ...
- 【BZOJ3295】【CQOI2011】动态逆序对
cdq分治经典例题,然而智商掉线傻逼错误坑了两天 原题: 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你 ...
- 【BZOJ】【3295】【CQOI2011】动态逆序对
树套树 Orz zyf神犇 时光倒流……逆序处理,将删点改为加点,动态维护序列. 由于是动态,要加点,所以用树状数组:同时又需要求序列中求比当前元素大/小的元素个数,所以要用平衡树. 所以方法就是在树 ...
- BZOJ 3295: [Cqoi2011]动态逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3865 Solved: 1298[Submit][Sta ...
- Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2886 Solved: 924[Submit][Stat ...
- bzoj3295[Cqoi2011]动态逆序对 树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5987 Solved: 2080[Submit][Sta ...
- P3157 [CQOI2011]动态逆序对(树状数组套线段树)
P3157 [CQOI2011]动态逆序对 树状数组套线段树 静态逆序对咋做?树状数组(别管归并QWQ) 然鹅动态的咋做? 我们考虑每次删除一个元素. 减去的就是与这个元素有关的逆序对数,介个可以预处 ...
- P3157 [CQOI2011]动态逆序对
P3157 [CQOI2011]动态逆序对 https://www.luogu.org/problemnew/show/P3157 题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai&g ...
随机推荐
- js 获取年月日
虽然网上关于这个的方法很多 但是自己还是总结了一个比较可用的方法 var date=new Date(); var year=date.getFullYear(); ); var day=change ...
- mysql中的数据类型长度
“mysql中的数据类型长度是固定的 数据类型后面改的只是展示长度 没用的 int就是四个字节 2的31次方减一是最大值 所以改这个长度没用 只能改数据类型”
- excel截取第一个空格前的字符
excel 替换 空格字符后面的所有字符 =TRIM(REPLACE(A1,FIND(" ",A1),999,)) =TRIM(REPLACE(A1,1,FIND(" & ...
- Centos7下载和安装教程
https://blog.csdn.net/qq_42570879/article/details/82853708 阿里下载64bit镜像:http://mirrors.aliyun.com/cen ...
- Codeforces Round #575 (Div. 3) 题解
比赛链接:https://codeforc.es/contest/1196 A. Three Piles of Candies 题意:两个人分三堆糖果,两个人先各拿一堆,然后剩下一堆随意分配,使两个人 ...
- css 属性值 calc (目前只了解部分)
移动端页面,有如下图的需求: 实现效果: 实现 css 代码: .list {/*父级*/ border: 1px solid #E9EAEA; border-radius: 2px; backgro ...
- mysql取出字段数据的精度
$field = 'convert(avg(mood),decimal(4,0)) mood,convert(avg(hrv),decimal(4,0)) hrv,convert(avg(heart_ ...
- maven的背景
本书链接 链接:http://pan.baidu.com/s/1c2fF3Ks 密码:hlce maven是一套软件工程管理和整合工具. 基于工程对象模型的概念(POM),通过一个中央信息管理模块,m ...
- 吴裕雄 python 神经网络——TensorFlow训练神经网络:不使用正则化
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...
- 【Python】【Django】用户注册功能
GET方法前置步骤做完 stu.models.py 再其中创建需要用到的字段及对应数据库的表 # -*- coding: utf-8 -*- from __future__ import unicod ...