[USACO 08JAN]Haybale Guessing
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的更多相关文章
- 洛谷 P2898 [USACO08JAN]haybale猜测Haybale Guessing 解题报告
[USACO08JAN]haybale猜测Haybale Guessing 题目描述 给一段长度为\(n\),每个位置上的数都不同的序列\(a[1\dots n]\)和\(q\)和问答,每个问答是\( ...
- 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 ...
- [USACO 08JAN]Telephone Lines
Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...
- [USACO08JAN]haybale猜测Haybale Guessing
题目描述 The cows, who always have an inferiority complex about their intelligence, have a new guessing ...
- [USACO08JAN]Haybale Guessing(LuoguP2898)
The cows, who always have an inferiority complex about their intelligence, have a new guessing game ...
- poj-3657 Haybale Guessing(二分答案+并查集)
http://poj.org/problem?id=3657 下方有中文版,不想看英文的可直接点这里看中文版题目 Description The cows, who always have an in ...
- 【[USACO08JAN]haybale猜测Haybale Guessing】
抄题解.jpg 完全完全不会啊,这道题简直太神了 不过抄题解可真开心 首先这道题目保证了每一个位置上的数都是不同的,那么就能得到第一种判断不合法的方式 如果两个区间的最小值一样,但是两个区间的交集为空 ...
- [USACO 2017DEC] Haybale Feast
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5142 [算法] 首先用RMQ预处理S数组的最大值 然后我们枚举右端点 , 通过二分求 ...
随机推荐
- web服务器学习3---httpd 2.4.29日志处理
.rotarelogs分割工具 如果有虚拟主机在虚拟主机配置文件中配置,否则在主配置文件中修改. 1.1修改配置文件 vi /usr/local/httpd/conf/conf.d/vhosts.co ...
- Beta预备会议
1. 讨论组长是否重选的议题和结论. 我们小组决定组长更换为林洋洋同学,他Web开发经验比较丰富,对任务的分配会更加明确,由于上一阶段中存在进度偏慢的问题,我们希望在Beta阶段通过更好的分工安排来保 ...
- django获取ip与数据重复性判定
获取ip if request.META.has_key('HTTP_X_FORWARDED_FOR'): ip_c = request.META['HTTP_X_FORWARDED_FOR'] el ...
- 【详细】Lucene使用案例
Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引 ...
- javascript 腾讯ABS云平台面试题及面试经历
既然说到面试前端肯定是Javascript各种问,只好各种答. 面试题肯定离不了,最近热门的Vue.js,React.js,Angular.js,Gulp,Webpack还有各种Js问题,还有令人头痛 ...
- 15-TypeScript策略模式
在前面的简单工厂模式中,通常将每个类.接口定义到不同的文件中.在面向对象开发思想中有一个重要的原则就是封装变化点,在实际操作过程中, 通常被调用方的代码不要去更改,而是增加,这是面向对象的开闭原则.在 ...
- AngularJS1.X学习笔记13-路由
ThinkPHP框架有路由的概念,看起来路由更多的是后端的事情,Angular怎么也会跑出个路由呢?事实上,Angular是着眼于单页应用的,他的一个应用一般来说是一个页面,你所看到的页面内容的改变, ...
- 大数据学习总结(5)参考elk技术架构
- python网络爬虫与信息提取 学习笔记day1
Day1: 安装python之后,为其配置requests第三方库,并爬取百度主页内容. 语句解释: r.status_code检测请求的状态码,如果状态码为200,则说明访问成功,否则,则说明访问失 ...
- Flow简易教程——安装篇
.mydoc_h1{ margin: 0 0 1em; } .mydoc_h1_a{ color: #2c3e50; text-decoration: none; font-size: 2em; } ...