题目链接:点击查看

题目大意:给出两个排列 ppp 和 qqq,现在要求输出
Perm((Ord(p)+Ord(q))modn!)Perm((Ord(p)+Ord(q)) \bmod n!) Perm((Ord(p)+Ord(q))modn!)
其中,Prem(x)Prem(x)Prem(x) 是第 xxx 个排列,Ord(p)Ord(p)Ord(p) 是排列 ppp 是第 Ord(p)Ord(p)Ord(p) 个排列

题目分析:线段树模拟康托展开参考:https://www.luogu.com.cn/blog/yummy-loves-114514/huoxingren

当得出数组 aaa 和 bbb 后分别代表排列 ppp 和 qqq 的康拓展开表达式,虽然得到的是变进制数字,因为两个数的位数相同,所以无需还原,直接相加,高位如果溢出舍弃即可,相当于模拟了取余的操作

代码:

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
int a[N],b[N],c[N],ans[N];
struct Node
{int l,r,sum;
}tree[N<<2];
void build(int k,int l,int r)
{tree[k]={l,r,r-l+1};if(l==r) return;int mid=(l+r)>>1;build(k<<1,l,mid),build(k<<1|1,mid+1,r);tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
}
void update(int k,int pos)
{tree[k].sum--;if(tree[k].l==tree[k].r) return;int mid=(tree[k].l+tree[k].r)>>1;if(pos<=mid) update(k<<1,pos);else update(k<<1|1,pos);
}
int query(int k,int l,int r)//查询[l,r]内有多少个数
{if(tree[k].l>r||tree[k].r<l) return 0;if(tree[k].l>=l&&tree[k].r<=r) return tree[k].sum;return query(k<<1,l,r)+query(k<<1|1,l,r);
}
int query(int k,int x)//查询第x小的数
{if(tree[k].l==tree[k].r) return tree[k].l;if(tree[k<<1].sum>=x) return query(k<<1,x);else return query(k<<1|1,x-tree[k<<1].sum);
}
void input(int n,int a[])
{build(1,1,n);for(int i=1,x;i<=n;i++){read(x);x++;update(1,x);a[i]=query(1,1,x);}
}
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);int n;read(n);input(n,a),input(n,b);for(int i=n,mod=1,t=0;i>=1;i--,mod++){int p=a[i]+b[i]+t;ans[i]=p%mod;t=p/mod;}build(1,1,n);for(int i=1;i<=n;i++){int pos=query(1,ans[i]+1);write(pos-1),putchar(' ');update(1,pos);}puts("");return 0;
}

CodeForces - 504B Misha and Permutations Summation(线段树模拟康托展开与逆展开)相关推荐

  1. Educational Codeforces Round 96 E. String Reversa 线段树模拟序列交换

    传送门 文章目录 题意: 思路: 题意: 思路: 与上一篇题解大同小异,无非就是不需要枚举排列了. // Problem: E. String Reversal // Contest: Codefor ...

  2. Codeforces Round #285 (Div. 2) D. Misha and Permutations Summation 康托展开 + 线段树

    传送门 文章目录 题意: 思路: 题意: 思路: 首先肯定不能模n!n!n!,所以考虑先将a,ba,ba,b做一个逆康托展开,得到a′,b′a',b'a′,b′数组,以及a′+b′=suma'+b'= ...

  3. 【贪心 / 线段树模拟费用流增广】BZOJ4977 [Lydsy八月月赛] 跳伞求生

    [题目] 原题地址 有nn个队友和mm个敌人,每个队友有一个攻击力aia_i,每个敌人有攻击力bib_i和价值cic_i.你可以选择若干个队友,每个队友ii分别去怼一个敌人jj,当ai>bja_ ...

  4. Codeforces 1004F Sonya and Bitwise OR (线段树)

    题目链接 https://codeforces.com/contest/1004/problem/F 题解 这种水题都不会做了怎么.. 考虑一个序列的前缀 \(\text{or}\) 值只会变化 \( ...

  5. CodeForces - 1430E String Reversal(线段树+模拟)

    题目链接:点击查看 题目大意:给出一个字符串 sss ,令其反转的串为 ttt ,每次操作可以将 ttt 中的两个相邻位置的字符交换,问最少需要进行多少次操作才能使得 ttt 变成 sss 题目分析: ...

  6. CodeForces - 1529E Trees of Tranquillity(贪心+线段树)

    题目链接:https://vjudge.net/problem/CodeForces-1529E 题目大意:给出两棵根节点为 111 的树,分别称为 AAA 树和 BBB 树,现在通过两棵树可以构造出 ...

  7. Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)

    题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...

  8. [codeforces] 383C Propagating tree(dfs序+线段树)

    题意: 给你一棵n个结点的树, 以1为根.每个结点有点权.有m次操作: 1.x结点权值 +val,x的儿子权值 −val,x的孙子们 +val,以此类推. 2.询问x的点权: 题解: 我们首先跑一边d ...

  9. CodeForces - 1217F Forced Online Queries Problem(线段树分治+并查集撤销)

    题目链接:点击查看 题目大意:给出 nnn 个点,初始时互相不存在连边,需要执行 mmm 次操作,每次操作分为两种类型: 1xy1 \ x \ y1 x y:如果 (x,y)(x,y)(x,y) 之间 ...

最新文章

  1. php压缩文件 不能二次开发,PHP 'ZipArchive library is not enabled'异常的解决方法
  2. 十天学会ASP.net
  3. Windows CE 6.0中断处理过程(转载)
  4. java aop execution_Spring AOP -- execution表达式
  5. 百度的html代码是什么,百度网页源代码是什么?
  6. 解决tfs工作区绑定问题
  7. ajax 请求post和get,ajax请求get和post
  8. 最小生成树prim、
  9. bond4 交换机配置_Linux--多网卡的7种Bond模式和交换机配置
  10. CentOS SELinux
  11. 朝阳正规的计算机学校有哪些,朝阳都有哪些专科学校
  12. 【组成原理-处理器】微程序控制器
  13. 微信公众号菜单栏设置直接打电话拨号一键拨号
  14. 抽象类及抽象方法作用
  15. 什么是安拆网?安拆网有什么作用?
  16. 淄博旅游景点与美食汇总
  17. lua和php那个好,phpor
  18. SpringFramework核心技术一(IOC:ApplicationContext的附加功能)
  19. centos7安装ZipArchive
  20. eds能谱图分析实例_EDS(EDX)- Energy Dispersive X-Ray Spectrometer 能量色散谱仪

热门文章

  1. 33 个送给 Java 程序员的练手项目合集
  2. 网关服务器怎么配置文件,网关服务器怎么配置文件
  3. mysql 5.5.15_mysql5.5.15配置主从数据库
  4. 文件上传下载-修改文件上传大小
  5. springboot创建多个对象
  6. MyBatis 插件原理与自定义插件-应用场景分析
  7. 高仿真的类-ApplicationContextAware
  8. 商品评价 - 实现分页
  9. 上传问题分析2--文件重名
  10. map和reduce