题解—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 ;
}
题解—P2898 [USACO08JAN]Haybale Guessing G的更多相关文章
- 洛谷 P2898 [USACO08JAN]haybale猜测Haybale Guessing 解题报告
[USACO08JAN]haybale猜测Haybale Guessing 题目描述 给一段长度为\(n\),每个位置上的数都不同的序列\(a[1\dots n]\)和\(q\)和问答,每个问答是\( ...
- [USACO08JAN]Haybale Guessing(LuoguP2898)
The cows, who always have an inferiority complex about their intelligence, have a new guessing game ...
- POJ 3657 Haybale Guessing(区间染色 并查集)
Haybale Guessing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2384 Accepted: 645 D ...
- Haybale Guessing
Haybale Guessing Time Limit: 1000MS Memory Limit: 65536K Description The cows, who always ha ...
- [USACO08JAN]haybale猜测Haybale Guessing
题目描述 The cows, who always have an inferiority complex about their intelligence, have a new guessing ...
- 【[USACO08JAN]haybale猜测Haybale Guessing】
抄题解.jpg 完全完全不会啊,这道题简直太神了 不过抄题解可真开心 首先这道题目保证了每一个位置上的数都是不同的,那么就能得到第一种判断不合法的方式 如果两个区间的最小值一样,但是两个区间的交集为空 ...
- [USACO 08JAN]Haybale Guessing
Description The cows, who always have an inferiority complex about their intelligence, have a new gu ...
- poj-3657 Haybale Guessing(二分答案+并查集)
http://poj.org/problem?id=3657 下方有中文版,不想看英文的可直接点这里看中文版题目 Description The cows, who always have an in ...
- 【题解】[USACO19DEC]Milk Visits G
题目戳我 \(\text{Solution:}\) 这题不要把思想局限到线段树上--这题大意就是求路径经过的值中\(x\)的出现性问题. 最开始的想法是值域线段树--看了题解发现直接\(vector\ ...
随机推荐
- 42. Trapping Rain Water [dp][stack]
description: Given n non-negative integers representing an elevation map where the width of each bar ...
- ESP32S2获取sht30温湿度
static const char *TAG = "i2c-temp"; static unsigned char sht30_buf[6]={0}; static float g ...
- Django基础-01篇
一.Django介绍 flask,FastApi是轻量级服务端开发框架 Django是重量级服务端开发框架 ORM:封装了数据库操作 form:校验请求数据 安装Django: pip install ...
- Django基础010--ORM操作
orm返回的数据有两种,QuerySet,object 1.QuerySet支持链式编程,可以在all()后面继续.方法 teachers = models.Teacher.objects.all() ...
- ORB随便记一记
论文摘取 (这部分看的是泡泡机器人的翻译) 基于特征点.单目.完全自动初始化,基于PTAM框架. 相关工作 A.位置识别(大概是用于回环检测) bags of words FAB-map DBOW2 ...
- ubuntu18.04aliyun
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirr ...
- vue+canvas实现炫酷时钟效果的倒计时插件(已发布到npm的vue2插件,开箱即用)
前言: 此事例是在vue组件中,使用canvas实现倒计时动画的效果.其实,实现效果的逻辑跟vue没有关系,只要读懂canvas如何实现效果的这部分逻辑就可以了 canvas动画的原理:利用定时器,给 ...
- 深入GraphQL 的使用语法
深入GraphQL 的使用语法 对于GraphQL 的使用语法在上一节中已经大概介绍了基本的使用方式了,这一篇将会对上一篇入门做拓展,努力将所有的使用语法都覆盖到. 1. 终端语法 首先是介绍在前端查 ...
- 12Java进阶-IO与XML
1.File File:java.io.File:代表一个实际的文件或目录. 常用构造方法File file = new File("path"); 其它构造方法: File(St ...
- 剑指offer-子树结构
描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 求解思路: 首先肯定是找到树A中等于树B根节点的节点. A和B按照同样的遍历顺序比较,如果全相同A ...