4053

思路:

主席树

先分别求前缀和后缀的逆序数

然后要求某一段的逆序数,就可以根据前缀或着后缀根据容斥求出答案,

这样需要枚举这一段中的数,求之前或者之后有多少个比他大或比他小的数,

这个可以通过用主席数维护权值线段树来做

然后每次枚举断开后小的那段区间,这样最多需要枚举n*log(n)次

复杂度:n*log(n)*log(n)

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + , M = 2e6 + ;
int a[N], p[N], root[N], bit[N], lson[M], rson[M], value[M], tot = , n;
LL tmp[N], ans[N], pre[N], suf[N];
multiset<LL> s;
void build(int &x, int l, int r) {
x = ++tot;
if(l == r) {
value[x] = ;
return ;
}
int m = l+r >> ;
build(lson[x], l, m);
build(rson[x], m+, r);
value[x] = value[lson[x]] + value[rson[x]];
}
void update(int old, int &x, int p, int v, int l, int r) {
x = ++tot;
lson[x] = lson[old], rson[x] = rson[old], value[x] = value[old] + v;
if(l == r) return ;
int m = l+r >> ;
if(p <= m) update(lson[x], lson[x], p, v, l, m);
else update(rson[x], rson[x], p, v, m+, r);
}
int query(int L, int R, int x, int l, int r) {
if(L > R) return ;
if(L <= l && r <= R) return value[x];
int m = l+r >> , ans = ;
if(L <= m) ans += query(L, R, lson[x], l, m);
if(R > m) ans += query(L, R, rson[x], m+, r);
return ans;
}
void add(int x, int v) {
while(x <= n+) bit[x] += v, x += x&-x;
}
int sum(int x) {
int ans = ;
while(x) ans += bit[x], x -= x&-x;
return ans;
}
int Find_pre(int pos) {
int l = , r = pos, m = l+r >> ;
while(l < r) {
if(sum(pos) - sum(m-) > ) l = m + ;
else r = m;
m = l+r >> ;
}
return m;
}
int Find_nxt(int pos) {
int l = pos, r = n, m = l+r+ >> ;
while(l < r) {
if(sum(m) - sum(pos-) > ) r = m - ;
else l = m;
m = l+r+ >> ;
}
return m;
}
void solve(int l, int m, int r) {
if(l == r) return ;
else if(l + == r) {
if(l == m) tmp[l] = , s.insert();
else tmp[l-] = , s.insert();
}
else {
if(l == m) {
LL t = tmp[l-];
t -= (query(, a[m]-, root[r], , n) - query(, a[m]-, root[l], , n));
tmp[l] = t;
s.insert(t);
}
else if(r == m) {
LL t = tmp[l-] - (query(a[m]+, n, root[r-], , n) - query(a[m]+, n, root[l-], , n));
tmp[l-] = t;
s.insert(t);
}
else {
LL t = tmp[l-], t1, t2;
if(m-l+ < r-m) {
t1 = pre[m-] - pre[l-];
for (int i = l; i < m; i++) {
t1 -= query(a[i]+, n, root[l-], , n);
} t2 = t - t1;
for (int i = l; i < m; i++) {
t2 -= query(, a[i]-, root[r], , n) - query(, a[i]-, root[m-], , n);
}
t2 -= query(, a[m]-, root[r], , n) - query(, a[m]-, root[m], , n);
}
else {
t2 = suf[m+] - suf[r+];
for (int i = m+; i <= r; i++) {
if(r+ <= n) t2 -= query(, a[i]-, root[n], , n) - query(, a[i]-, root[r], , n);
} t1 = t - t2;
for (int i = m+; i <= r; i++) {
t1 -= query(a[i]+, n, root[m], , n) - query(a[i]+, n, root[l-], , n);
}
t1 -= query(a[m]+, n, root[m-], , n) - query(a[m]+, n, root[l-], , n);
}
tmp[l-] = t1;
tmp[m] = t2;
s.insert(t1);
s.insert(t2);
}
} }
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= n; i++) scanf("%d", &p[i]);
pre[] = suf[n+] = ;
s.clear();
tot = ;
build(root[], , n);
for (int i = ; i <= n; i++) update(root[i-], root[i], a[i], , , n);
for (int i = n; i >= ; i--) {
suf[i] = suf[i+] + query(, a[i]-, root[n], , n) - query(, a[i]-, root[i], , n);
}
for (int i = ; i <= n; i++) {
pre[i] = pre[i-] + query(a[i]+, n, root[i-], , n);
}
for (int i = ; i <= n+; i++) bit[i] = ;
tmp[] = suf[];
s.insert(tmp[]);
for (int i = ; i <= n; i++) {
ans[i] = *s.rbegin();
int t = ans[i]^p[i];
int l = Find_pre(t), r = Find_nxt(t);
s.erase(s.find(tmp[l-]));
solve(l, t, r);
add(t, );
}
for (int i = ; i <= n; i++) printf("%lld%c", ans[i], " \n"[i==n]);
}
return ;
}

ZOJ 4053 Couleur的更多相关文章

  1. Couleur(启发式 + 主席树)(终于补坑了)

    ZOJ Problem Set - 4053 Couleur Time Limit: 6 Seconds      Memory Limit: 131072 KB DreamGrid has an a ...

  2. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  3. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  4. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  5. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  6. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  7. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

  8. ZOJ Problem Set - 1001 A + B Problem

    ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...

  9. zoj 1788 Quad Trees

    zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...

随机推荐

  1. Centos7部署tornado项目

    今天帮一个学生解决tornado的部署问题,在此记录了这其中的过程,其中的tornado项目更换为demo示例. 开发环境: 本地开发环境:Win10 + Python3.5.4 + PyCharm ...

  2. Python 3 实现色情图片识别

    Python 3 实现色情图片识别 项目简介 项目内容 本实验将使用 Python3 去识别图片是否为色情图片,我们会使用到 PIL 这个图片处理库,会编写算法来划分图像的皮肤区域. 项目知识点 Py ...

  3. 纯手写SpringMVC到SpringBoot框架项目实战

    引言 Spring Boot其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 通过这种方式,springboot ...

  4. IO流-基础

    //创建输出流对象 FileWriter fw = new FileWriter("d:\\a.txt"); /* * 创建输出流对象做了哪些事情: * A:调用系统资源创建了一个 ...

  5. libcurl 设置代理,通过Fiddler可以进行抓包

    转载:https://blog.csdn.net/jaryguo/article/details/53021923 转载:https://www.cnblogs.com/miantest/p/7289 ...

  6. 16 级高代 II 思考题十的多种证明

    16 级高代 II 思考题十  设 $V$ 是数域 $\mathbb{K}$ 上的 $n$ 维线性空间, $\varphi$ 是 $V$ 上的线性变换, 证明: $\varphi$ 的极小多项式 $m ...

  7. hdfoo站点开发笔记-2

    httpd的目录的 Options: (里面的单词都是用的复数): Options Indexes FollowSymLinks 为了避免有些目录下没有生成deny.htm而显示列表, 可以直接给 / ...

  8. # bzoj2215: [Poi2011]Conspiracy 2-sat

    bzoj2215: [Poi2011]Conspiracy 2-sat 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2215 思路 一个点的 ...

  9. P1552 [APIO2012]派遣

    链接 https://www.luogu.org/problemnew/show/P1552 思路 忍者数量肯定越多越好 那就从下到上的合并它的孩子 左偏树的话 顺便维护一个tot,大头堆,如果tot ...

  10. 4698: Sdoi2008 Sandy的卡片

    前言 总之这个东西说起来很麻烦就是了, 思路 差分合并+后缀数组+二分(dddl) 类似于那个bzoj1031的复制子串和那个poj1743的差分 来看个例子 3 5 1 2 3 4 5 4 1 1 ...