「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」动态逆序对的更多相关文章

  1. 【CQOI2011】动态逆序对 BZOJ3295

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

  2. BZOJ 3295 【Cqoi2011】 动态逆序对

    Description 对于序列\(A\),它的逆序对数定义为满足\(i<j\),且\(A_i>A_j\)的数对\((i,j)\)的个数.给\(1\)到\(n\)的一个排列,按照某种顺序依 ...

  3. 【BZOJ3295】【CQOI2011】动态逆序对

    cdq分治经典例题,然而智商掉线傻逼错误坑了两天 原题: 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你 ...

  4. 【BZOJ】【3295】【CQOI2011】动态逆序对

    树套树 Orz zyf神犇 时光倒流……逆序处理,将删点改为加点,动态维护序列. 由于是动态,要加点,所以用树状数组:同时又需要求序列中求比当前元素大/小的元素个数,所以要用平衡树. 所以方法就是在树 ...

  5. BZOJ 3295: [Cqoi2011]动态逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3865  Solved: 1298[Submit][Sta ...

  6. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  7. bzoj3295[Cqoi2011]动态逆序对 树套树

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5987  Solved: 2080[Submit][Sta ...

  8. P3157 [CQOI2011]动态逆序对(树状数组套线段树)

    P3157 [CQOI2011]动态逆序对 树状数组套线段树 静态逆序对咋做?树状数组(别管归并QWQ) 然鹅动态的咋做? 我们考虑每次删除一个元素. 减去的就是与这个元素有关的逆序对数,介个可以预处 ...

  9. P3157 [CQOI2011]动态逆序对

    P3157 [CQOI2011]动态逆序对 https://www.luogu.org/problemnew/show/P3157 题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai&g ...

随机推荐

  1. CCF 试题编号: 201909-4 试题名称: 推荐系统

    这题是stl的综合应用,map要想快,直接上unordered_map,这样查询接近O(1),是不是很嗨皮. 思路其实还是很简单的,type+id做个Hash,由于set.insert的第一个返回值是 ...

  2. 在linux环境下python与C++混合编程

    参考:在linux环境下编译C++ 程序 linux下python3调用c代码或者python3调用c++代码 https://blog.csdn.net/u013179327/article/det ...

  3. ztree-拖拽(排序树)

    <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - beforeDrag / onDrag / bef ...

  4. Go语言fmt.Printf使用指南

    文章引用自 fmt fmt包实现了类似C语言printf和scanf的格式化I/O.主要分为向外输出内容和获取输入内容两大部分. 向外输出 标准库fmt提供了以下几种输出相关函数. Print Pri ...

  5. mysql带条件的计数

    在网站开发的过程中,经常会用到数据统计功能,因此条件计数查询便是不可避免的,下面介绍几种方法来解决此问题. 例(假设): mysql> select * from count_demo; +-- ...

  6. 在HTML中实现两个div并排显示

    在HTML中让两个div并排显示,通常情况下有三种实现方式,包括: (1)设置为行内样式,display:inline-block (2)设置float浮动 (3)设置position定位属性为abs ...

  7. Anniversary party POJ - 2342

    题目链接 经典的树形dp,最大独立集,对于每个点就有2个状态,选/不选 设\(dp_{i,0}\)表示不选第i个,\(dp_{i,1}\)表示选第i个,容易得到其状态转移 \(dp_{i,0} = \ ...

  8. uniGUI之UniPopupMenu和右键菜单(27)

    0]MainModule的BrowserOptions.boDisableMouseRightClick设置为Trure; 1]控件的OnCellContextClick的事件 procedure T ...

  9. @ResponseBody是如何起作用的

    前言 最近参与的项目中,接口中返回的日期格式不对,发现项目中配置了fastjson作为spring的数据转换器,于是使用了fastjson的字段格式化转换注解 发现不起作用.这让我很疑惑,然后在fas ...

  10. Atcoder Grand Contest 039C(容斥原理,计数DP)

    //每次操作相当于将最低位取反加到最高位(N~1位)#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace s ...