ZOJ 4053 Couleur
思路:
主席树
先分别求前缀和后缀的逆序数
然后要求某一段的逆序数,就可以根据前缀或着后缀根据容斥求出答案,
这样需要枚举这一段中的数,求之前或者之后有多少个比他大或比他小的数,
这个可以通过用主席数维护权值线段树来做
然后每次枚举断开后小的那段区间,这样最多需要枚举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的更多相关文章
- Couleur(启发式 + 主席树)(终于补坑了)
ZOJ Problem Set - 4053 Couleur Time Limit: 6 Seconds Memory Limit: 131072 KB DreamGrid has an a ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- zoj 1788 Quad Trees
zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
随机推荐
- org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout() mybatis和spring-mybatis版本不匹配问题
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout() ...
- 解决在ubuntu中安装或升级时出现“11:资源暂时不可用”错误
解决在ubuntu中安装或升级时出现“11:资源暂时不可用”错误 解决在ubuntu中安装或升级时出现“11:资源暂时不可用”错误. 下图为具体情况: 出现问题: termial下在执行sudo ap ...
- 20180307-Xen、KVM、VMware、hyper-v等虚拟化技术的比较
xen和kvm,是开源免费的虚拟化软件. vmware是付费的虚拟化软件. hyper-v比较特别,是微软windows 2008 R2附带的虚拟化组件,如果你买了足够的授权,hyper-v(包括hy ...
- TensorFlow学习---tf.nn.dropout防止过拟合
一. Dropout原理简述: tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层. Dropout就是在不同的训练过程中随机扔掉一部分神经元.也 ...
- CentOS 安装 Gitlab
源地址 https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/ # 清华源 https://mirrors.tuna.tsinghua.edu.cn/help/g ...
- awk - group adjacent rows by identical columns
Liang always brings me interesting quiz questions. Here is one: If i have a table like below: chr1 1 ...
- oracle 之 如何链接别人电脑的oracle
1.首先确保两台电脑是在同一个局域网内,可以通过cm命令窗口 ping 对方电脑的ID,若是没问题则表示可以连接 2.接下来通过配置来首先连接对方的电脑 其实在后面还有一个是否创建新的额服务名的操作, ...
- P3041 [USACO12JAN]视频游戏的连击Video Game Combos
思路 简单的AC自动机上dp,暴力跳fail向子节点直接转移即可 代码 #include <cstdio> #include <algorithm> #include < ...
- [蓝桥] 算法训练 K好数
时间限制:1.0s 内存限制:256.0MB 问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = ...
- Ubuntu 更新系统版本以及查看当前系统版本的命令
1. Ubuntu 查看当前系统版本: lsb_release -a 2. Ubuntu 更新系统版本的命令: sudo do-release-upgrade