Solution -「多校联训」神
\(\mathcal{Description}\)
Link.
给定 \(n\) 阶排列 \(a\),\(q\) 次询问,每次给出 \(1\le l_1\le r_1<l_2\le r_2\le n\) 且 \(r_1-l_1=r_2-l_2\),询问满足 \(\forall j\in[1,r_1-l_1+1],~a_{j+l_1-1}<a_{b_j+l_2-1}\) 的 \((r_1-l_1+1)\) 阶排列 \(b\) 的个数,模 \((10^9+7)\)。
多测,\(\sum n,\sum q\le10^5\),\(a\) 的逆序对数 \(\le10^5\)。
\(\mathcal{Solution}\)
暴力做法:设前后询问区间为 \(A\) 和 \(B\),把两个区间的元素丢一起升序排序,则每个 \(A\) 中元素选择匹配点的方案数为其后方 \(B\) 类元素个数减去 \(A\) 类元素个数。
将同属一类的连续元素视为一段,那么有结论:段数为 \(\mathcal O(\sqrt n)\) 级别。
每个 \(B\) 段与后方所有 \(A\) 段组成逆序对,故段数与逆序对数是平方关系,而逆序对数是 \(\mathcal O(n)\) 级别,故段数 \(\mathcal O(\sqrt n)\) 级别。
利用主席数查找前驱快速求出每段长度,即可 \(\mathcal O(n\log n+q\sqrt n\log n)\) 求解。
\(\mathcal{Code}\)
/*~Rainybunny~*/
#include <cstdio>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
const int MAXN = 1e5, MOD = 1e9 + 7;
int n, q, root[MAXN + 5], fac[MAXN + 5], ifac[MAXN + 5];
inline void subeq( int& a, const int b ) { ( a -= b ) < 0 && ( a += MOD ); }
inline int sub( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline int mul( const long long a, const int b ) { return int( a * b % MOD ); }
inline int add( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline void addeq( int& a, const int b ) { ( a += b ) >= MOD && ( a -= MOD ); }
inline int mpow( int a, int b ) {
int ret = 1;
for ( ; b; a = mul( a, a ), b >>= 1 ) ret = mul( ret, b & 1 ? a : 1 );
return ret;
}
inline void init() {
fac[0] = 1;
rep ( i, 1, MAXN ) fac[i] = mul( i, fac[i - 1] );
ifac[MAXN] = mpow( fac[MAXN], MOD - 2 );
per ( i, MAXN - 1, 0 ) ifac[i] = mul( i + 1, ifac[i + 1] );
}
struct SegmentTree {
static const int MAXND = 2e6;
int node, ch[MAXND][2], sum[MAXND];
inline void insert( int& u, const int l, const int r, const int x ) {
int v = u, mid = l + r >> 1; u = ++node;
ch[u][0] = ch[v][0], ch[u][1] = ch[v][1], sum[u] = sum[v] + 1;
if ( l == r ) return ;
if ( x <= mid ) insert( ch[u][0], l, mid, x );
else insert( ch[u][1], mid + 1, r, x );
}
inline int count( const int u, const int v, const int l, const int r,
const int ql, const int qr ) {
if ( !v ) return 0;
if ( ql <= l && r <= qr ) return sum[v] - sum[u];
int mid = l + r >> 1, ret = 0;
if ( ql <= mid ) ret += count( ch[u][0], ch[v][0], l, mid, ql, qr );
if ( mid < qr ) ret += count( ch[u][1], ch[v][1], mid + 1, r, ql, qr );
return ret;
}
inline int prefix( const int u, const int v, const int l, const int r,
const int x ) {
if ( !x || sum[v] <= sum[u] ) return 0;
if ( l == r ) return l;
int mid = l + r >> 1;
if ( x <= mid ) return prefix( ch[u][0], ch[v][0], l, mid, x );
if ( int t = prefix( ch[u][1], ch[v][1], mid + 1, r, x ); t ) return t;
return prefix( ch[u][0], ch[v][0], l, mid, x );
}
} sgt;
int main() {
freopen( "god.in", "r", stdin );
freopen( "god.out", "w", stdout );
int T; init();
for ( scanf( "%d", &T ); T--; sgt.node = 0 ) {
scanf( "%d %d", &n, &q );
rep ( i, 1, n ) {
int t; scanf( "%d", &t );
sgt.insert( root[i] = root[i - 1], 1, n, t );
}
for ( int l1, r1, l2, r2; q--; ) {
scanf( "%d %d %d %d", &l1, &r1, &l2, &r2 );
int rt1[] = { root[l1 - 1], root[r1] },
rt2[] = { root[l2 - 1], root[r2] };
int mxl = sgt.prefix( rt1[0], rt1[1], 1, n, n ),
mxr = sgt.prefix( rt2[0], rt2[1], 1, n, n ), cnt = 0, ans = 1;
while ( mxl || mxr ) {
if ( mxl < mxr ) {
int lef = sgt.prefix( rt1[0], rt1[1], 1, n, mxr ),
len = sgt.count( rt2[0], rt2[1], 1, n, lef, mxr );
cnt += len;
mxr = sgt.prefix( rt2[0], rt2[1], 1, n, lef );
} else {
int lef = sgt.prefix( rt2[0], rt2[1], 1, n, mxl ),
len = sgt.count( rt1[0], rt1[1], 1, n, lef, mxl );
if ( cnt < len ) { ans = 0; break; }
ans = mul( ans, mul( fac[cnt], ifac[cnt - len] ) );
cnt -= len;
mxl = sgt.prefix( rt1[0], rt1[1], 1, n, lef );
}
}
printf( "%d\n", ans );
}
}
return 0;
}
Solution -「多校联训」神的更多相关文章
- Solution -「多校联训」最小点覆盖
\(\mathcal{Description}\) Link. 求含有 \(n\) 个结点的所有有标号简单无向图中,最小点覆盖为 \(m\) 的图的数量的奇偶性.\(T\) 组数据. \( ...
- Solution -「多校联训」排水系统
\(\mathcal{Description}\) Link. 在 NOIP 2020 A 的基础上,每条边赋权值 \(a_i\),随机恰好一条边断掉,第 \(i\) 条段的概率正比于 \(a ...
- Solution -「多校联训」I Love Random
\(\mathcal{Description}\) 给定排列 \(\{p_n\}\),可以在其上进行若干次操作,每次选取 \([l,r]\),把其中所有元素变为原区间最小值,求能够得到的所有不同序 ...
- Solution -「多校联训」签到题
\(\mathcal{Description}\) Link. 给定二分图 \(G=(X\cup Y,E)\),求对于边的一个染色 \(f:E\rightarrow\{1,2,\dots,c\ ...
- Solution -「多校联训」朝鲜时蔬
\(\mathcal{Description}\) Link. 破案了,朝鲜时蔬 = 超现实树!(指写得像那什么一样的题面. 对于整数集 \(X\),定义其 好子集 为满足 \(Y\sub ...
- Solution -「多校联训」消失的运算符
\(\mathcal{Description}\) Link. 给定长度为 \(n\) 的合法表达式序列 \(s\),其中数字仅有一位正数,运算符仅有 - 作为占位.求将其中恰好 \(k\) ...
- Solution -「多校联训」假人
\(\mathcal{Description}\) Link. 一种物品有 长度 和 权值 两种属性,现给定 \(n\) 组物品,第 \(i\) 组有 \(k_i\) 个,分别为 \((1,a ...
- Solution -「多校联训」古老的序列问题
\(\mathcal{Description}\) Link. 给定序列 \(\{a_n\}\),和 \(q\) 次形如 \([L,R]\) 的询问,每次回答 \[\sum_{[l,r]\su ...
- Solution -「多校联训」Sample
\(\mathcal{Description}\) Link (稍作简化:)对于变量 \(p_{1..n}\),满足 \(p_i\in[0,1],~\sum p_i=1\) 时,求 \(\ma ...
随机推荐
- ubuntu 18.04 安装mongodb并设为开机自启动
导入包管理系统使用的公钥 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB4 ...
- HBase环境搭建(hbase1.2.5+zookeeper3.4.6)
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6627857018461880836/ 系统版本,Hadoop已安装完成 Mysql安装完成 Hive版本 Sqoo ...
- python极简教程04:进程和线程
测试奇谭,BUG不见. 大家好,我是谭叔. 这一场,主讲python的进程和线程. 目的:掌握初学必须的进程和线程知识. 进程和线程的区别和联系 终于开始加深难度,来到进程和线程的知识点~ 单就这两个 ...
- JVM调优-1
JVM运行参数 在jvm中有很多的参数可以进行设置,这样可以让jvm在各种环境中都能够高效的运行.绝大部分的参数保持默认即可. 三种参数类型 标准参数 -help -version -X参数(非标准参 ...
- 2021最新Termux安装Metasploit
前言 因为某些脚本小子的用Termux搞破坏,所以Termux软件源移除了对Metasploit的支持,所以就不能直接用pkg和apt直接安装了. 但是不用担心,noob-hacker大大写了一个工具 ...
- vue3代码setup中this为什么无效
结论:setup并没有通过各种方式去绑定this 在vue2中,我们可以在optionsApi中调用this来指向当前组件的实例,但是在vue3的setup中并不能这样做,因为setup位于组件创建成 ...
- preg_match绕过总结
preg_match绕过总结 什么是preg_match 绕过方法 1.数组绕过 preg_match只能处理字符串,当传入的subject是数组时会返回false 2.PCRE回溯次数限制 PHP利 ...
- unity3d C# soket客户端接受失败
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using S ...
- vue项目再HBuilder打包成app后,有ui模块未添加的弹窗
直接在打包后的mainifst.json的文件夹中加入标注部分,我是这样解决了的
- 用Python实现一个Picgo图床工具
PyPicGo PyPicGo 是一款图床工具,是PicGo是Python版实现,并支持各种插件自定义插件,目前PyPicGo自带了gitee.github.SM.MS和七牛云图传,以及rename. ...