ZOJ Problem Set - 4053

Couleur

Time Limit: 6 Seconds      Memory Limit: 131072 KB

DreamGrid has an array of  integers. On this array he can perform the following operation: choose an element that was not previously chosen and mark it as unavailable. DreamGrid would like to perform exactly  operations until all the elements are marked.

DreamGrid defines the cost of a subarray as the number of inversions in the subarray. Before performing an operation, DreamGrid would like to know the maximum cost of a subarray that doesn't contain any unavailable elements.

Recall that a subarray  is a contiguous subpart of the original array where . An inversion in a subarray  is a pair of indices   such that the inequality  holds.

Input

There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:

The first line contains a single integer   -- the length of the array.

The second line contains the  values of the array  .

The third line contains a permutation , representing the indices of the elements chosen for the operations in order.

Note that the permutation is encrypted and you can get the real permutation using the following method: Let  be the answer before the -th operation. The actual index of the -th operation is  where  is bitwise exclusive or operator.

It is guaranteed that the sum of all  does not exceed .

Output

For each test case, output  integers  in a single line seperated by one space, where  is the answer before the -th operation.

Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

Sample Input

354 3 1 1 15 4 5 3 1109 7 1 4 7 8 5 7 4 821 8 15 5 9 2 4 5 10 6154 8 8 1 12 1 10 14 7 14 2 9 13 10 337 19 23 15 7 2 10 15 2 13 4 5 8 7 10

Sample Output

7 0 0 0 020 11 7 2 0 0 0 0 0 042 31 21 14 14 4 1 1 1 0 0 0 0 0 0

Hint

The decoded permutation of each test case is ,  and


Author: LIN, Xi
Source: The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

题目大意 给一个序列 然后按照一定顺序 删掉一些数字  使得序列不连续 要让我们去找出  逆序数最大的连续序列 输出这个连续序列的逆序对数
本蒟蒻在做这个题目的时候 用的set维护的最大的逆序数
QAQ 一直都不对       这个题目要用muliset 维护啊 qaq 
qaq qaq qaq qaq
有关这个逆序数的关系传递 值得思考

 
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
const int maxn = 1e5+;
#define ll long long
struct tree{int l,r,sum;}T[maxn*];
int a[maxn],p[maxn],root[maxn],N;ll z[maxn],big[maxn],cnt;
void update(int &x,int y,int l,int r,int pos)
{
T[++cnt]=T[y],x=cnt,T[x].sum++;
if(l==r)return;
int m=(l+r)>>;
if(pos<=m)update(T[x].l,T[y].l,l,m,pos);
else update(T[x].r,T[y].r,m+,r,pos);
}
ll findbig(int x,int y,int l,int r,int pos)
{
if(l==r)return ;
int m=(l+r)>>;
if(pos<=m)return findbig(T[x].l,T[y].l,l,m,pos)+T[T[y].r].sum-T[T[x].r].sum;
return findbig(T[x].r,T[y].r,m+,r,pos);
}
ll findsmall(int x,int y,int l,int r,int pos)
{
if(l==r)return ;
int m=(l+r)>>;
if(pos<=m)return findsmall(T[x].l,T[y].l,l,m,pos);
return findsmall(T[x].r,T[y].r,m+,r,pos)+T[T[y].l].sum-T[T[x].l].sum;
}
set<int>se;
multiset<ll>Maxinv;
set<int>::iterator it;
ll delet(int x)
{ se.insert(x);it=se.find(x);
int l=*(--it) +;++it;int r=*(++it) -;
if(big[r])Maxinv.erase(Maxinv.find(big[r]));
ll invl=,invr=,t=;
if(x-l<r-x)
{
for(int i=l;i<x;i++)
{
invl+=findbig(root[l-],root[i],,N,a[i]);
t+=findsmall(root[i],root[r],,N,a[i]);
}
invr=big[r]-t-findsmall(root[x],root[r],,N,a[x]);
}else{
for(int i=x+;i<=r;i++)
{
invr+=findsmall(root[i],root[r],,N,a[i]);
t+=findbig(root[l-],root[i],,N,a[i]);
}
invl=big[r]-t-findbig(root[l-],root[x],,N,a[x]);
}
big[x-]=invl;big[r]=invr;
if(invl)Maxinv.insert(invl);if(invr)Maxinv.insert(invr);
multiset<ll>::iterator its=Maxinv.end();
return *(--its);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
se.clear();cnt=;Maxinv.clear();Maxinv.insert();
scanf("%d",&N);se.insert(),se.insert(N+);
for(int i=;i<=N;i++)scanf("%d",a+i);
for(int i=;i<=N;i++)scanf("%d",p+i);
for(int i=;i<=N;i++)update(root[i],root[i-],,N,a[i]);
for(int i=;i<=N;i++)big[i]=big[i-]+findbig(root[],root[i],,N,a[i]);
z[]=big[N];Maxinv.insert(z[]);
for(int i=;i<N;i++)z[i+]=delet(z[i]^p[i]);
for(int i=;i<N;i++)printf("%lld ",z[i]);
printf("%lld\n",z[N]);
}
return ;
}

Couleur(启发式 + 主席树)(终于补坑了)的更多相关文章

  1. 【ZOJ4053】Couleur(主席树,set,启发式)

    题意: 有n个位置,每个位置上的数字是a[i],现在有强制在线的若干个单点删除操作,每次删除的位置都不同,要求每次删除之后求出最大的连续区间逆序对个数 n<=1e5,1<=a[i]< ...

  2. 集训队8月1日(拓扑排序+DFS+主席树入门)

    上午看书总结 今天上午我看了拓扑排序,DFS+剪枝,相当于回顾了一下,写了三个比较好的例题.算法竞赛指南93~109页. 1.状态压缩+拓扑排序 https://www.cnblogs.com/246 ...

  3. [bzoj3123][洛谷P3302] [SDOI2013]森林(树上主席树+启发式合并)

    传送门 突然发现好像没有那么难……https://blog.csdn.net/stone41123/article/details/78167288 首先有两个操作,一个查询,一个连接 查询的话,直接 ...

  4. P3302 [SDOI2013]森林(主席树+启发式合并)

    P3302 [SDOI2013]森林 主席树+启发式合并 (我以前的主席树板子是错的.......坑了我老久TAT) 第k小问题显然是主席树. 我们对每个点维护一棵包含其子树所有节点的主席树 询问(x ...

  5. bzoj3123 [Sdoi2013]森林 树上主席树+启发式合并

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3123 题解 如果是静态的查询操作,那么就是直接树上主席树的板子. 但是我们现在有了一个连接两棵 ...

  6. 【BZOJ3123】森林(主席树,启发式合并)

    题意:一个带点权的森林,要求维护以下操作: 1.询问路径上的点权K大值 2.两点之间连边 n,m<=80000 思路:如果树的结构不发生变化只需要维护DFS序 现在因为树的结构发生变化,要将两棵 ...

  7. bzoj 3123 [Sdoi2013]森林(主席树,lca,启发式合并)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

  8. BZOJ 3123: [Sdoi2013]森林 [主席树启发式合并]

    3123: [Sdoi2013]森林 题意:一个森林,加边,询问路径上k小值.保证任意时刻是森林 LCT没法搞,树上kth肯定要用树上主席树 加边?启发式合并就好了,小的树dfs重建一下 注意 测试点 ...

  9. [SDOI2013]森林 主席树+启发式合并

    这题的想法真的很妙啊. 看到题的第一眼,我先想到树链剖分,并把\(DFS\)序当成一段区间上主席树.但是会发现在询问的时候,可能会非常复杂,因为你需要把路径拆成很多条轻链和重链,它们还不一定连续,很难 ...

随机推荐

  1. jquery 条件搜索某个标签下的子标签

    $("li[name='"+name+"']").find("a[value='" + value + "']").pa ...

  2. Oracle查看有锁进程,删除锁

    查看锁表进程SQL语句1: select sess.sid,     sess.serial#,     lo.oracle_username,     lo.os_user_name,     ao ...

  3. 35-python基础-python3-字符串修改大小写的方法-title()方法-lower()方法-upper()方法

    1-title()-注:不是原地修改,有返回值 以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写. 2-lower()和upper()-注:不是原地修改,有返回值 将字符串改为全部小写或 ...

  4. bzoj4544 椭圆上的整点

    我会所有推理..... Q1:真的这么暴力的统计答案? Q2:蜜汁统计答案.... Q3:为什么不考虑3在不同的位置的情况

  5. 案例:forEach和some区别

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. JNI中修改(基本类型)参数并返回到Java层使用

    最近在JNI相关项目中遇到一个问题:在Java层传入多个int类型的参数,在jni层修改参数值或地址之后重新返回到Java层.这应该算是基本知识了,尤其是基本类型的参数往往看似简单,所以在之前学习jn ...

  7. teb教程1

    http://wiki.ros.org/teb_local_planner/Tutorials/Setup%20and%20test%20Optimization 简介:本部分关于teb怎样优化轨迹以 ...

  8. Jmeter服务器性能压测-用户登录实例CSV方式

    为什么用CSV方式压测,因为用jdbc链接数据库,我发现数据库数据量量大的情况下,Jmeter会内存溢出 第一步:数据准备,根据登录接口需要的参数准备测试数据 例子中,测试的登录接口需要4个参数化数据 ...

  9. 2019计蒜之道初赛第3场-阿里巴巴协助征战SARS 费马小定理降幂

    题目链接:https://nanti.jisuanke.com/t/38352 发现规律之后就是算ans=2^(n-1)+4^(n-1).但是注意到n十分大是一个长度为1e5的数字.要想办法降幂. 我 ...

  10. 基于React Native的跨三端应用架构实践

    作者|陈子涵 编辑|覃云 “一次编写, 到处运行”(Write once, run anywhere ) 是很多前端团队孜孜以求的目标.实现这个目标,不但能以最快的速度,将应用推广到各个渠道,而且还能 ...