题目描述

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语言博客作业—字符数组

    一.PTA实验作业 题目1:字符串转换成十进制整数 1. 本题PTA提交列表 2. 设计思路 (1)定义i为循环变量,number用于存放每一次转化的结果,flag用于判断是否为负数,p用于修改结果的 ...

  2. 团队第1次作业:Our Team TAH

     Team named TAH    不管一个人多么有才能,但是集体常常比他更聪明和更有力. --奥斯特洛夫斯基     *introduce team and teamate 先说说TAH的含义,是 ...

  3. 视频聊天 Demo

    ESFramework Demo -- 入门Demo,简单的即时通讯系统(附源码) 是基于ESFramework实现的一个简单的文字聊天demo,现在,我们将在这个demo的基础上,使用OMCS为其增 ...

  4. python3爬虫之入门和正则表达式

    前面的python3入门系列基本上也对python入了门,从这章起就开始介绍下python的爬虫教程,拿出来给大家分享:爬虫说的简单,就是去抓取网路的数据进行分析处理:这章主要入门,了解几个爬虫的小测 ...

  5. 一个页面多个HTTP请求 页面卡顿!

    用promise解决 前两天面试的时候 一个面试官问到这样一个问题 这里先说出解决的路径 这几天会更新具体的做法 或者直接参考廖雪峰大神 地址如下: https://www.liaoxuefeng.c ...

  6. js 获取 最近七天 30天 昨天的方法 -- 转

    自己用到了 找了下  先附上原作的链接  http://www.cnblogs.com/songdongdong/p/7251254.html 原谅我窃取你的果实  谢谢你谢谢你 ~ 先附上我自己用到 ...

  7. 学习phalcon框架按照官网手册搭建第一个项目注册功能

    中文手册官网:http://phalcon.ipanta.com/1.3/tutorial.html#bootstrap 官网提供http://www.tutorial.com项目源码github地址 ...

  8. netty : NioEventLoopGroup 源码分析

    NioEventLoopGroup 源码分析 1. 在阅读源码时做了一定的注释,并且做了一些测试分析源码内的执行流程,由于博客篇幅有限.为了方便 IDE 查看.跟踪.调试 代码,所以在 github ...

  9. Python内置函数(27)——range

    英文文档: range(stop) range(start, stop[, step]) Rather than being a function, range is actually an immu ...

  10. Python内置函数(10)——float

    英文文档: class float([x]) Return a floating point number constructed from a number or string x. If the ...