题目描述

The cows, who always have an inferiority complex about their intelligence, have a new guessing game to sharpen their brains.

A designated 'Hay Cow' hides behind the barn and creates N (1 ≤ N ≤ 1,000,000) uniquely-sized stacks (conveniently numbered 1..N) of hay bales, each with 1..1,000,000,000 bales of hay.

The other cows then ask the Hay Cow a series of Q (1 ≤ Q ≤ 25,000) questions about the the stacks, all having the same form:

What is the smallest number of bales of any stack in the range of stack numbers Ql..Qh (1 ≤ Ql ≤ N; Ql ≤ Qh ≤ N)?The Hay Cow answers each of these queries with a single integer A whose truthfulness is not guaranteed.

Help the other cows determine if the answers given by the Hay Cow are self-consistent or if certain answers contradict others.

给一段长度为n,每个位置上的数都不同的序列a[1..n]和q和问答,每个问答是(x, y, r)代表RMQ(a, x, y) = r, 要你给出最早的有矛盾的那个问答的编号。

输入输出格式

输入格式:

  • Line 1: Two space-separated integers: N and Q

  • Lines 2..Q+1: Each line contains three space-separated integers that represent a single query and its reply: Ql, Qh, and A

输出格式:

  • Line 1: Print the single integer 0 if there are no inconsistencies
    among the replies (i.e., if there exists a valid realization of the hay
    stacks that agrees with all Q queries). Otherwise, print the index from
    1..Q of the earliest query whose answer is inconsistent with the answers
    to the queries before it.

输入输出样例

输入样例#1:
复制

20 4
1 10 7
5 19 7
3 12 8
11 15 12
输出样例#1: 复制

3

以下题解摘自洛谷题解,非常清楚

出现矛盾的区间符合两个条件之一:

1.题目中的两个干草堆没有任何数量是一样的,所以如果两个区间没有交集并且它们的最小值相同,则这两个区间产生矛盾

2.如果一个区间包含另一个区间,被包含的区间的最小值大于另一个区间,则两个区间产生矛盾

考虑对原先问答的顺序进行二分答案,对于一个二分出的mid作如下处理:

为了方便处理矛盾2,将从1到mid的每个区间的值按照从大到小进行排序

对于值相同的区间,求出并集和交集的范围,如果不存在并集,则mid不可行

维护一颗线段树,将交集的区间覆盖为1

查询并集的区间是否被覆盖为1,如果是,则mid不可行

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Ask
{
int l,r,x;
}a[],b[];
int c[],n,q;
bool cmp(Ask a,Ask b)
{
return a.x>b.x;
}
void build(int rt,int l,int r)
{
if (l==r)
{
c[rt]=;
return;
}
int mid=(l+r)/;
build(rt*,l,mid);
build(rt*+,mid+,r);
c[rt]=c[rt*]&c[rt*+];
}
void pushdown(int rt)
{
if (c[rt])
{
c[rt*]=c[rt];
c[rt*+]=c[rt];
}
}
void update(int rt,int l,int r,int L,int R)
{
if (l>=L&&r<=R)
{
c[rt]=;
return;
}
int mid=(l+r)/;
pushdown(rt);
if (L<=mid) update(rt*,l,mid,L,R);
if (R>mid) update(rt*+,mid+,r,L,R);
c[rt]=c[rt*]&c[rt*+];
}
int query(int rt,int l,int r,int L,int R)
{
if (c[rt]) return ;
if (l>=L&&r<=R)
{
return c[rt];
}
int mid=(l+r)/;
int ll=,rr=;
if (L<=mid) ll=query(rt*,l,mid,L,R);
if (R>mid) rr=query(rt*+,mid+,r,L,R);
c[rt]=c[rt*]&c[rt*+];
return ll&rr;
}
bool check(int mid)
{int i,j,l1,l2,r1,r2,k;
for (i=;i<=mid;i++)
b[i]=a[i];
build(,,n);
sort(b+,b+mid+,cmp);
for (i=;i<=mid;i=j)
{
j=i;
while (j<=mid&&b[j].x==b[i].x) j++;
l1=2e9;r2=2e9;l2=-;r1=-;
for (k=i;k<j;k++)
{
l1=min(l1,b[k].l);
r1=max(r1,b[k].r);
l2=max(l2,b[k].l);
r2=min(r2,b[k].r);
}
if (l2>r2) return ;
if (query(,,n,l2,r2)) return ;
update(,,n,l1,r1);
}
return ;
}
int main()
{int i;
cin>>n>>q;
for (i=;i<=q;i++)
{
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].x);
}
int l=,r=q,ans=;
while (l<=r)
{
int mid=(l+r)/;
if (check(mid))
{
ans=mid;
l=mid+;
}
else r=mid-;
}
cout<<(ans+)%(q+);
}

[USACO08JAN]haybale猜测Haybale Guessing的更多相关文章

  1. 洛谷 P2898 [USACO08JAN]haybale猜测Haybale Guessing 解题报告

    [USACO08JAN]haybale猜测Haybale Guessing 题目描述 给一段长度为\(n\),每个位置上的数都不同的序列\(a[1\dots n]\)和\(q\)和问答,每个问答是\( ...

  2. 【[USACO08JAN]haybale猜测Haybale Guessing】

    抄题解.jpg 完全完全不会啊,这道题简直太神了 不过抄题解可真开心 首先这道题目保证了每一个位置上的数都是不同的,那么就能得到第一种判断不合法的方式 如果两个区间的最小值一样,但是两个区间的交集为空 ...

  3. python猜数脚本(电脑猜测)(二分法)

    # coding=utf-8# 猜数# 记录猜数的过程import randomcom_result=[]  #存放电脑结果,数组com_count=0 #存放电脑猜测次数ran=random.ran ...

  4. [USACO08JAN]Haybale Guessing(LuoguP2898)

    The cows, who always have an inferiority complex about their intelligence, have a new guessing game ...

  5. 题解—P2898 [USACO08JAN]Haybale Guessing G

    pre 首先注意一下翻译里面并没有提到的一点,也是让我没看懂样例的一点,就是这个长度为 \(n\) 的数组里面的数各不相同. 有很多人用并查集写的这道题,题解里面也有一些用线段树写的,不过我认为我的做 ...

  6. POJ 3657 Haybale Guessing(区间染色 并查集)

    Haybale Guessing Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2384   Accepted: 645 D ...

  7. Haybale Guessing

    Haybale Guessing Time Limit: 1000MS   Memory Limit: 65536K       Description The cows, who always ha ...

  8. [USACO 08JAN]Haybale Guessing

    Description The cows, who always have an inferiority complex about their intelligence, have a new gu ...

  9. poj-3657 Haybale Guessing(二分答案+并查集)

    http://poj.org/problem?id=3657 下方有中文版,不想看英文的可直接点这里看中文版题目 Description The cows, who always have an in ...

随机推荐

  1. C语言程序设计(基础)- 第2周作业

    1.阅读提问的智慧,要求仔细阅读链接内容,用自己的话描述你的收获,并举例子说明应该如何提问. 2.所有同学请在自己电脑上配置git.编译器(win10 系统的话就Dev-C++).翻译软件,十一回校后 ...

  2. Beta 第一天

    一.今日任务 重新熟悉整体项目 对整个项目在未来的beta冲刺中进程有一个合理的规划 由于我们送出的是一个负责前端的成员,引入的也是一个负责前端工作的女生,(女生做起美工比起男生更加得心应手吧)所以我 ...

  3. C语言的第 次作业总结

    PTA实验作业 第一题: 使用函数输出水仙花数 1.设计思路: 2.碰到的问题及解决方法: 实验中碰到的主要问题是:虽然知道如何求每一位的数但不知道如何输出m到n之间的水仙花数,我上面截图中的和瓮恺视 ...

  4. Alpha冲刺Day12

    Alpha冲刺Day12 一:站立式会议 今日安排: 由黄腾飞和张梨贤继续完成政府人员模块下的风险管控子模块下的分级统计展示 由林静继续完成企业注册模块 由周静平完成登录页面模块 二:实际项目进展 人 ...

  5. JAVA中GridBagLayout布局管理器应用详解

    很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成.但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够 ...

  6. Fluent Interface(流式接口)

    我最初接触这个概念是读自<<模式-工程化实现及扩展>>,另外有Martin fowler大师 所写http://martinfowler.com/bliki/FluentInt ...

  7. git(一)快速入门

    1.设置用户名 git config --global user.name '你的用户名' ​ 2.设置用户名邮箱 git config --global user.email '你的邮箱' ​ 3. ...

  8. MyBatis 中使用数据库查询别名进行映射

    方法1 XXMapper.xml <mapper namespace="com.hfepc.dao.andon.AndonExceptionKanbanVOMapper" & ...

  9. C++ 异常小记

    catch必定使用拷贝构造函数 如下代码编译不通过,因为拷贝构造被标记delete #include <stdexcept> #include <cstdlib> #inclu ...

  10. 基于ssm的poi反射bean实例

    一:该例子是笔者在实际项目应用过程中,针对项目完成的一套基于poi的导入导出例子,其中一些与项目有关的代码大家直接替换成自己的需求即可. 二:笔者在项目中使用的是poi的XSSF,对应maven的po ...