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. linux中权限对文件和目录的意义

    1.权限对文件的意义: 读:可查看文件的内容 写:可修改文件的内容(但不能删除文件) 执行:可执行文件 2.权限对目录的意义: 读:可以查看目录下的内容,即可以读取该目录下的结构列表 写:可修改目录下 ...

  2. 使用v-for指令渲染列表

    v-for:对集合或对象进行遍历: 使用v-for对数组遍历时: 效果如下: 代码: <script> window.onload= () =>{new Vue({ el:'#two ...

  3. 如何在Framework中读取bundle中的Res

    前因: 因为公司上架前后的原因,外围的平台层部分提前上线,而我做的功能部分需要晚一些上线,是单独的一个工程在其他仓库开发. 我的资源文件放在Bundle中.合到主工程中,资源文件不用改,直接拖进去.倒 ...

  4. Vue学习【第六篇】:Vue-cli脚手架(框架)与实战案例

    环境搭建 安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 安装cnpm npm install -g cnpm --registry=https://re ...

  5. Hakase and Nano 【思维博弈】

    Hakase and Nano 时间限制: 1 Sec  内存限制: 128 MB 提交: 400  解决: 104 [提交] [状态] [命题人:admin] 题目描述 Hakase and Nan ...

  6. 颠倒的价牌|2013年蓝桥杯A组题解析第四题-fishers

    颠倒的价牌 小李的店里专卖其它店中下架的样品电视机,可称为:样品电视专卖店. 其标价都是4位数字(即千元不等). 小李为了标价清晰.方便,使用了预制的类似数码管的标价签,只要用颜色笔涂数字就可以了(参 ...

  7. Tutorials on Inverse Reinforcement Learning

    Tutorials on Inverse Reinforcement Learning 2018-07-22 21:44:39 1. Papers:  Inverse Reinforcement Le ...

  8. (转)awesome-text-summarization

    awesome-text-summarization 2018-07-19 10:45:13 A curated list of resources dedicated to text summari ...

  9. algorithm.sty not found error in LaTeX 解决方法

    参考: algorithm.sty not found error in LaTeX algorithm.sty not found error in LaTeX 解决方法 错误日志: LaTeX E ...

  10. ie 支持字体大小继承

    今天需要实现字体大小继承这个效果.是这样的,在公用类里 .box 中的 .box1 的字体进行了修改.但是我的页面里不需要修改.我需要让他和 .box 一样.所以想到使用继承.但是想到继承这个属性兼容 ...