题解 AT4353 【[ARC101D] Robots and Exits】
直观感受是,考虑夹在
正确描述出来是,满足
我们发现这似乎意味着如果
用
注意到我们实际上只关注向左/向右走过的历史最远距离,所以每次操作可以看作是给历史最远距离
将所有二元组按照
- 若
l_i\le l_j ,且r_i\ge r_j ,那么如果i 为1 ,则j 为1 。另一面类似。
由于我们按照了
我们考虑把未被限制而自主选择了 1 的点拎出来,这样可以看作是选择一个原序列的子序列且其合法,而合法等价于
所以对这个问题进行 dp 就可以了,由于我比较菜,不会离散化,所以写了一个动态开点的线段树。
值得注意的是空序列也算一个合法的方案。
#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
#define int long long
#define ls(x) tr[x].l
#define rs(x) tr[x].r
int gi() {
char cc = getchar() ; int cn = 0, flus = 1 ;
while( cc < '0' || cc > '9' ) { if( cc == '-' ) flus = - flus ; cc = getchar() ; }
while( cc >= '0' && cc <= '9' ) cn = cn * 10 + cc - '0', cc = getchar() ;
return cn * flus ;
}
const int P = 1e9 + 7 ;
const int N = 2e5 + 5 ;
int n, m, cnt, num, Rt, M, lef[N], ref[N], dp[N] ;
struct node { int p, f ; } a[N << 1] ;
struct Px { int x, y ; } c[N] ;
bool cmp( node x, node y ) { return x.p < y.p ; }
bool cmp2( Px a, Px b ) { return ( a.x == b.x ) ? ( a.y > b.y ) : a.x < b.x ; }
struct Tr { int l, r, w ; } tr[N * 20] ;
void insert( int &x, int l, int r, int wh, int val ) {
if( !x ) x = ++ num ; tr[x].w += val, tr[x].w %= P ;
if( l == r ) return ;
int mid = ( l + r ) >> 1 ;
if( mid >= wh ) insert( ls(x), l, mid, wh, val ) ;
else insert( rs(x), mid + 1, r, wh, val ) ;
}
int query( int x, int l, int r, int wh ) {
if( l == r ) return tr[x].w ;
int mid = ( l + r ) >> 1 ;
if( mid >= wh ) return query( ls(x), l, mid, wh ) ;
else return ( query( rs(x), mid + 1, r, wh ) + tr[ls(x)].w ) % P ;
}
signed main()
{
n = gi(), m = gi() ; int maxn = 0 ;
rep( i, 1, n ) a[i].p = gi(), maxn = max( maxn, a[i].p ), a[i].f = 1 ;
rep( i, 1, m ) a[i + n].p = gi(), maxn = max( maxn, a[i + n].p ) ;
M = n + m, ++ maxn ;
sort( a + 1, a + M + 1, cmp ) ;
rep( i, 1, M ) lef[i] = ( a[i].f ) ? lef[i - 1] : a[i].p ;
drep( i, 1, M ) ref[i] = ( a[i].f ) ? ref[i + 1] : a[i].p ;
rep( i, 1, M ) {
if( (!lef[i]) || (!ref[i]) || (!a[i].f) ) continue ;
++ cnt, c[cnt].x = a[i].p - lef[i], c[cnt].y = ref[i] - a[i].p ;
}
sort( c + 1, c + cnt + 1, cmp2 ) ; int Ans = 1 ;
rep( i, 1, cnt ) {
if( c[i].x == c[i - 1].x && c[i].y == c[i - 1].y ) continue ;
dp[i] = 1 + query( 1, 1, maxn, c[i].y + 1 ) ;
insert( Rt, 1, maxn, c[i].y + 2, dp[i] ) ;
Ans = ( Ans + dp[i] ) % P ;
}
cout << Ans << endl ;
return 0 ;
}