Description

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, 要你给出最早的有矛盾的那个问答的编号。

Input

  • 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

Output

  • 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.

Sample Input

20 4
1 10 7
5 19 7
3 12 8
11 15 12

Sample Output

3

题解

二分求解。

二分答案,将答案范围内的最小值$Ai$进行降序排序 然后我们可以观察一下得到的这些区间

对于不同$Ai$想一想如果它被之前出现的区间(比它大的$Ai$)都覆盖了,那么肯定就是有矛盾的

给点提示:对于同样的$Ai$询问要用交集,覆盖要用并集

这样就可以很明显地用线段树来搞了

 //It is made by Awson on 2017.10.27
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Lr(o) (o<<1)
#define Rr(o) (o<<1|1)
using namespace std;
const int N = ;
const int INF = ~0u>>; int n, q;
struct tt {
int l, r, a;
} a[N+], b[N+];
bool comp(const tt &a, const tt &b) {
if (a.a != b.a) return a.a > b.a;
return a.l == b.l ? a.r < b.r : a.l < b.l;
}
struct segment {
int sgm[(N<<)+], lazy[(N<<)+];
void build(int o, int l, int r) {
lazy[o] = ;
if (l == r) {
sgm[o] = INF; return;
}
int mid = (l+r)>>;
build(Lr(o), l, mid);
build(Rr(o), mid+, r);
sgm[o] = Max(sgm[Lr(o)], sgm[Rr(o)]);
}
void pushdown(int o) {
if (lazy[o]) {
sgm[Lr(o)] = sgm[Rr(o)] = lazy[Lr(o)] = lazy[Rr(o)] = lazy[o];
lazy[o] = ;
}
}
void update(int o, int l, int r, int a, int b, int key) {
if (a <= l && r <= b) {
sgm[o] = lazy[o] = key; return;
}
pushdown(o);
int mid = (l+r)>>;
if (a <= mid) update(Lr(o), l, mid, a, b, key);
if (mid < b) update(Rr(o), mid+, r, a, b, key);
sgm[o] = Max(sgm[Lr(o)], sgm[Rr(o)]);
}
int query(int o, int l, int r, int a, int b) {
if (a <= l && r <= b) return sgm[o];
pushdown(o);
int mid = (l+r)>>;
int a1 = , a2 = ;
if (a <= mid) a1 = query(Lr(o), l, mid, a, b);
if (mid < b) a2 = query(Rr(o), mid+, r, a, b);
return Max(a1, a2);
}
}T; bool get(int l, int r, int &x, int &y) {
int ll = b[l].l, rr = b[l].r;
for (int i = l+; i <= r; i++) {
int lll = b[i].l, rrr = b[i].r;
if (rr < lll) return false;
ll = lll;
}
x = ll, y = rr;
return true;
}
bool judge(int mid) {
T.build(, , n);
for (int i = ; i <= mid; i++) b[i] = a[i];
sort(b+, b++mid, comp);
for (int i = ; i <= mid; i++) {
int loc, l, r;
for (loc = i; loc <= mid; loc++) if (b[loc].a != b[i].a) break;
loc--;
if (!get(i, loc, l, r)) return false;
int t = T.query(, , n, l, r);
if (t != INF && t != b[i].a) return false;
for (int k = i; k <= loc; k++)
T.update(, , n, b[k].l, b[k].r, b[k].a);
i = loc;
}
return true;
}
void work() {
scanf("%d%d", &n, &q);
for (int i = ; i <= q; i++)
scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].a);
int L = , R = q, ans = ;
while (L <= R) {
int mid = (L+R)>>;
if (judge(mid)) L = mid+;
else R = mid-, ans = mid;
}
printf("%d\n", ans);
}
int main() {
work();
return ;
}

[USACO 08JAN]Haybale Guessing的更多相关文章

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

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

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

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

  3. Haybale Guessing

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

  4. [USACO 08JAN]Telephone Lines

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  5. [USACO08JAN]haybale猜测Haybale Guessing

    题目描述 The cows, who always have an inferiority complex about their intelligence, have a new guessing ...

  6. [USACO08JAN]Haybale Guessing(LuoguP2898)

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

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

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

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

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

  9. [USACO 2017DEC] Haybale Feast

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5142 [算法] 首先用RMQ预处理S数组的最大值 然后我们枚举右端点 , 通过二分求 ...

随机推荐

  1. 20162318 实验三《 敏捷开发与XP实践》实验报告

    北京电子科技学院(BESTI) 实 验 报 告 课程:程序设计与数据结构 班级:1623班 姓名:张泰毓 指导老师:娄老师.王老师 实验日期:2017年5月12日 实验密级:非密级 实验器材:带Lin ...

  2. 1013团队Beta冲刺day3

    项目进展 李明皇 今天解决的进度 完善了程序的运行逻辑(消息提示框等) 明天安排 前后端联动调试 林翔 今天解决的进度 向微信官方申请登录验证session以维护登录态 明天安排 继续完成维护登录态 ...

  3. 20145237 《Java程序设计》第八周学习总结

    20145237 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章 通用API 15.1 日志 日志API简介 • java.util.logging包提供了日志功能相关类与 ...

  4. python3变量和数据类型

        变量和数据类型 知识点 python 关键字 变量的定义与赋值 input() 函数 字符串的格式化 实验步骤 每一种编程语言都有它们自己的语法规则,就像我们所说的外语. 1. 关键字和标识符 ...

  5. python利用twilio模块给自己发短信

    1.访问http://twilio.com/并填写注册表单.注册了新账户后,你需要验证一个手机号码,短信将发给该号码. 2.Twilio 提供的试用账户包括一个电话号码,它将作为短信的发送者.你将需要 ...

  6. 第四十四条:为所有导出的API元素编写文档注释

    简而言之,要为API编写文档,文档注释是最好,最有效的途径.对于所有可导出的API元素来说,使用文档注释应该被看作是强制性的.要 采用一致的风格来遵循标准的约定.记住,在文档注释内部出现任何的HTML ...

  7. .Net Core SignalR 实时推送信息

    以前一直没用成功过SignalR(.net asp),最近几天又参考了对应的文档,最终调成功啦. 开始之前,应该注意: 一定要.Net Core 2.1.0以上的SDK. VS2017 15.6以上的 ...

  8. New UWP Community Toolkit - DeveloperTools

    概述 UWP Community Toolkit  中有一个开发者工具集 DeveloperTools,可以帮助开发者在开发过程中进行 UI 和功能的调试,本篇我们结合代码详细讲解  Develope ...

  9. Hey,man,are you ok? -- 关于心跳、故障监测、lease机制

    电话之于短信.微信的一个很大的不同点在于,前者更加及时,有更快速直接的反馈:而后面两个虽然称之为instant message,但经常时发出去了就得等对方回复,等多久是不确定的.打电话能明确知道对方在 ...

  10. SpringCloud应用入库后乱码问题

    一.现象 1.请求 2.入库后 二.解决过程 1.配置application.properties 2.代码配置 3.数据库(关键!!) 3.请求 三.验证过程 1.win10 - 本地验证通过 2. ...