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. java中volatile

    volatile用来修饰变量.Java 语言中的 volatile 变量可以被看作是一种 "程度较轻的 synchronized":与 synchronized 块相比,volat ...

  2. 201621123060《JAVA程序设计》第十四周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 用 ...

  3. 敏捷开发每日报告--day5

    1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285)  Git链接:https://github.com/WHUSE2017/C-team 2 ...

  4. C语言——第0次作业(二)

    1.翻阅邹欣老师的关于师生关系博客,并回答下列问题,每个问题的答案不少于500字: 1.最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助? 在现代软件工程讲义 0 ...

  5. TSP-旅行商问题

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  6. 简单介绍 CPU 的工作原理

    1.内部架构 CPU 的根本任务就是执行指令,对计算机来说最终都是一串由 0 和 1 组成的序列.CPU 从逻辑上可以划分成 3 个模块,分别是控制单元.运算单元和存储单元 .其内部架构如下: [1] ...

  7. 新概念英语(1-101)A Card From Jimmy

    Lesson 101 A card from Jimmy 吉米的明信片 Listen to the tape then answer this question. Does Grandmother s ...

  8. zuul入门(4)zuul的注解@EnableZuulServer和@EnableZuulProxy

    @EnableZuulServer.@EnableZuulProxy两个注解 @EnableZuulProxy简单理解为@EnableZuulServer的增强版,当Zuul与Eureka.Ribbo ...

  9. [POI2008]BLO-Blockade - Tarjan,割点

    Description 给定一张无向图,求每个点被封锁(删去与其相连的边)之后有多少个有序点对(x,y)(x!=y,1<=x,y<=n)满足x无法到达y. Input&Output ...

  10. 剑指offer-二叉树的下一个节点

    题目描述   给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针.   解题思路 分情况考虑如下: 若该节点为空,则直 ...