题解—P2898 [USACO08JAN]Haybale Guessing G
pre
首先注意一下翻译里面并没有提到的一点,也是让我没看懂样例的一点,就是这个长度为 \(n\) 的数组里面的数各不相同。
有很多人用并查集写的这道题,题解里面也有一些用线段树写的,不过我认为我的做法和各位线段树大佬的有些许不同。
solution
同样,单调性和二分查找这里不再赘述,直接说给定 \(n\) 个条件,判断他们是否合法。
根据样例我们可以看出,对于同一个数 \(num\) ,如果给定了多个形如 \([l_i,r_i]->num\) 的条件,是可以缩小这个数的范围的。
通俗的说,我们把线段树上面信息 \(data\) 的意义设置为当前(位置/区间)可以放置的最小的数。
那么,当我们拿到这些条件的时候,我们先给条件以 \(num\) 为关键字排序,然后逐一区间修改 \([l_i,r_i]->num\) ,正好可以满足我们的需要,因为一个区间如果最小值为 \(num\) ,那么这个区间内的所有数的最小值都是 \(num\) ,因为之前放的数肯定比当前小,所以直接区间修改即可。
然后我们再次遍历所有条件,看能否找到矛盾。
(墙烈推荐大家根据情况模拟几个例子有利于理解)
情况一:我们查询 \([l_i,r_i]\) 的最小值,发现这个最小值大于 \(num_i\) ,那么直接返回失败。因为出现这种情况当且仅当他的这个区间完全被比他大的数覆盖了,那么必定有矛盾。
情况二:我们查询 \([l_i,r_i]\) 的最小值,发现这个最小值等于 \(num_i\) ,那说明这个条件必定没有矛盾,直接检查下一个即可。
情况三:我们查询 \([l_i,r_i]\) 的最小值,发现这个最小值小于 \(num_i\) ,出现这种情况的唯一可能就是存在一个 \(num_j\) ,他的实际范围被多个条件缩小到了一个范围,然后查询 \([l_i,r_i]\) 的时候有更小的数在这个区间里面,这个时候,我们需要分类讨论。
如果这个比 \(num_i\) 小的数完全包含于 \([l_i,r_i]\) ,那么说明不合法。
否则无法说明不合法,直接去找下一个条件。
code
(放这么丑的代码真是对不起大家的眼睛啦)
#include <cstring>
#include <algorithm>
#include <cstdio>
#define mp make_pair
#define R register int
#define int long
#define printf Ruusupuu = printf
int Ruusupuu ;
using namespace std ;
typedef long long L ;
typedef long double D ;
typedef unsigned long long G ;
typedef pair< int , int > PI ;
const int N = 1e6 + 10 ;
const int M = 3e4 + 10 ;
const int Inf = 0x3f3f3f3f ;
inline int read(){
int w = 0 ; bool fg = 0 ; char ch = getchar() ;
while( ch < '0' || ch > '9' ) fg |= ( ch == '-' ) , ch = getchar() ;
while( ch >= '0' && ch <= '9' ) w = ( w << 1 ) + ( w << 3 ) + ( ch ^ '0' ) , ch = getchar() ;
return fg ? -w : w ;
}
int n , q , mid , lx , rx , dlt , fg [N] ;
struct QS{ int l , r , num ; } a [M] , fq [M] , as [M] ;
inline bool cmp( QS a , QS b ){ return a.num < b.num ; }
int l [N << 2] , r [N << 2] , data [N << 2] , lz [N << 2] ;
#define ud( x ) data [x] = data [x << 1] < data [x << 1 | 1] ? data [x << 1] : data [x << 1 | 1] ;
inline void sp( int x ){
if( !lz [x] ) return ;
int k = lz [x] ; lz [x] = 0 ;
data [x << 1] = lz [x << 1] = data [x << 1 | 1] = lz [x << 1 | 1] = k ;
}
void build( int x , int ll , int rr ){
l [x] = ll , r [x] = rr , data [x] = Inf ; lz [x] = 0 ;
if( ll == rr ) return ;
int mid = ( ll + rr ) >> 1 ;
build( x << 1 , ll , mid ) , build( x << 1 | 1 , mid + 1 , rr ) ;
}
void cge( int x ){
if( l [x] >= lx && r [x] <= rx ){ data [x] = lz [x] = dlt ; return ; }
sp( x ) ;
int mid = ( l [x] + r [x] ) >> 1 ;
if( lx <= mid ) cge( x << 1 ) ;
if( rx > mid ) cge( x << 1 | 1 ) ;
ud( x ) ;
}
int ask( int x ){
if( l [x] >= lx && r [x] <= rx ) return data [x] ;
sp( x ) ;
int mid = ( l [x] + r [x] ) >> 1 , ans = Inf ;
if( lx <= mid ) ans = min( ans , ask( x << 1 ) ) ;
if( rx > mid ) ans = min( ans , ask( x << 1 | 1 ) ) ;
return ans ;
}
void sc(){
n = read() , q = read() ;
for( R i = 1 ; i <= q ; i ++ ) a [i].l = read() , a [i].r = read() , a [i].num = read() ;
}
inline bool check(){
build( 1 , 1 , n ) ;
for( R i = 1 ; i <= mid ; i ++ ) fq [i] = a [i] , as [i].l = as [i].r = as [i].num = fg [i] = 0 ;
sort( fq + 1 , fq + 1 + mid , cmp ) ;
int ls = fq [1].l , rs = fq [1].r , now = fq [1].num , top = 0 ;
//多次使用记得清空
for( R i = 2 ; i <= mid ; i ++ ){
if( now != fq [i].num ){
as [++ top].num = now , as [top].l = ls , as [top].r = rs ;
lx = ls , rx = rs , dlt = now , cge( 1 ) ;
now = fq [i].num , ls = fq [i].l , rs = fq [i].r ;
}
else{
ls = max( ls , fq [i].l ) , rs = min( rs , fq [i].r ) ;
if( rs < ls ) return 0 ;
}
}
as [++ top].num = now , as [top].l = ls , as [top].r = rs ;
lx = ls , rx = rs , dlt = now , cge( 1 ) ;
//先把所有条件都插入
for( R i = 1 ; i <= mid ; i ++ ){
lx = fq [i].l , rx = fq [i].r ;
int sn = ask( 1 ) ;
if( sn > fq [i].num ) return 0 ;
if( sn < fq [i].num ){
for( R j = 1 ; j <= top ; j ++ )
if( as [j].num == sn ){
if( as [j].l >= lx && as [j].r <= rx ) return 0 ;
else break ;
}
}
} // 再逐一寻找矛盾
return 1 ;
}
void work(){
int lside = 1 , rside = q , ans = 0 ;
while( lside <= rside ){
mid = ( lside + rside ) >> 1 ;
if( check() ) ans = mid , lside = mid + 1 ;
else rside = mid - 1 ;
}
printf( "%ld\n" , ( ans + 1 ) % ( q + 1 ) ) ; //直接一个简洁写法,不用if else
}
signed main(){
sc() ;
work() ;
return 0 ;
}

浙公网安备 33010602011771号