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. Django设计模式

    单例模式: 建造者模式: 示例: from enum import Enum import time PizzaProgress = Enum('PizzaProgress', 'queued pre ...

  2. Vue小案例 之 商品管理------为之前的页面修改样式

    最终修改的页面效果: 修改的css: <style> #container{ margin: auto; text-align: center; width: 1000px; border ...

  3. Python3 freetds.conf odbcinst.ini odbc.ini 之间的关系

    Python3 freetds.conf odbcinst.ini odbc.ini 之间的关系 三者分别是FreeTDS和UnixODBC的配置文件: 1,FreeTDS中的freetds.conf ...

  4. 关于er模型中的identifying relationship or non-identifying relationship

    最近,主要负责项目管理和领域模型设计方面的工作,昨天在将UML类图转换为ER模型的时候,发现有identifying relationship or non-identifying relations ...

  5. yolo3(目标检测)实测

    yolo是继faster-r-cnn后,原作者在目标检测领域进行的新研究.到了v3版本以后,虽然已经换人支持,但是更注重工程实践,在实际使用过程中突出感受就是 “非常快”,GPU加速以后能够达到实时多 ...

  6. 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析20165211

    目录 实践内容概述 实践目标 实践内容 实验问题回答 实践过程记录 系统运行监控 使用schtacks指令监控系统运行 使用sysmon工具监控系统运行 恶意软件分析 使用Virus Total分析恶 ...

  7. Codeforces 235C Cyclical Quest - 后缀自动机

    Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...

  8. 【python021-函数lambda表达式】

    一.匿名函数 1.lambda表达式 >>> g = lambda x:x*2+1>>> g(5)11>>> ---冒号前面的x是函数的参数,冒号 ...

  9. python --- 06 小数据池 编码

    一.小数据池, id()    进行缓存 1.小数据池针对的是: int, str, bool 2.在py文件中几乎所有的字符串都会缓存.   在cmd命令窗口中几乎都不会缓存   不同的解释器有不同 ...

  10. UVA11417 GCD

    题目地址 题目链接 题解 先讨论任何没有限制的情况 \[ \large { \begin{aligned} &\sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j)\\ &a ...