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

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 ≤ QlN; QlQhN)?

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.

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
【分析】数轴上有n个点,没个点上的值都不同。然后给你Q次询问和答案,输入l,r,x,表示在区间[l,r]最小值为x,然后问你最早在哪个地方出现矛盾。
区间染色问题,可以用并查集来做。先二分出现矛盾的地方p,然后将1~p的询问值按大到小排序,若对于最小值相同的区间中出现不相交的两个区间,那么矛盾出现。
那么合法的情况就是这些最小值相同的区间必然是两两相交的,那么记录最小的左端点L和最大的右端点R,然后将[L,R]与L-1合并。所以在判断的时候还有判一下
当前最小值相同的区间的交集是否之前出现过,若出现过,则矛盾。
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef pair<int,int>pii;
typedef long long ll;
using namespace std;
const int N=3e4+;
const int M=1e6+;
int n,q,fa[M];
struct interval{
int x,y,MIN;
friend bool operator < (interval a,interval b){
return a.MIN>b.MIN;
}
}s[N],S[N];
inline int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
inline bool check(int pos){
for(int i=;i<=n;i++)
fa[i]=i;
for(int i=;i<=pos;i++)
S[i]=s[i];
sort(S+,S+pos+);
for(int i=,j;i<=pos;i=j+){
int x=S[i].x,X=x,y=S[i].y,Y=y;
j=i;
while(S[j+].MIN==S[j].MIN&&j+<=pos){
j++;
x=max(x,S[j].x),y=min(y,S[j].y);
X=min(X,S[j].x),Y=max(Y,S[j].y);
}
if(x>y||x>find(y))
return false;
while(X<=Y){
if(find(Y)==Y)
fa[Y]=find(X-),Y--;
else
Y=find(Y);
}
}
return true;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=;i<=q;i++)
scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].MIN);
int l=,r=q,ans=;
while(l<=r){
int mid=(l+r)>>;
if(check(mid))
l=mid+;
else
r=mid-,ans=mid;
}
cout<<ans<<endl;
return ;
}

POJ 3657 Haybale Guessing(区间染色 并查集)的更多相关文章

  1. POJ - 3657 Haybale Guessing(二分+并查集)

    题意:有N个大小各不相同的点,给定Q个询问,格式为q1,q2,A,表示区间q1~q2的最小值是A,问第一个与之前询问结果出现冲突的询问. 分析: 1.二分询问的标号mid,查询1~mid是否出现询问冲 ...

  2. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  3. POJ 1436 (线段树 区间染色) Horizontally Visible Segments

    这道题做了快两天了.首先就是按照这些竖直线段的横坐标进行从左到右排序. 将线段的端点投影到y轴上,线段树所维护的信息就是y轴区间内被哪条线段所覆盖. 对于一条线段来说,先查询和它能相连的所有线段,并加 ...

  4. POJ 1733 Parity game(种类并查集)

    http://poj.org/problem?id=1733 题意: 给出一个01串,有多次询问,每次回答[l,r]这个区间内1的个数的奇偶性,但是其中有一些回答是错误的,问到第几个回答时与前面的回答 ...

  5. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

  6. POJ 1984 - Navigation Nightmare - [带权并查集]

    题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS ...

  7. POJ 1417 - True Liars - [带权并查集+DP]

    题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...

  8. POJ 2524 独一无二的宗教(裸并查集)

    题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K ...

  9. POJ 1733 Parity game (带权并查集)

    题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...

随机推荐

  1. POJ 3087 Shuffle'm Up DFS

    link:http://poj.org/problem?id=3087 题意:给你两串字串(必定偶数长),按照扑克牌那样的洗法(每次从S2堆底中拿第一张,再从S1堆底拿一张放在上面),洗好后的一堆可以 ...

  2. web开发中防止SQL注入

    一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编写时的疏忽,通过SQL语句,实现无账号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...

  3. MyBatis框架的使用及源码分析(一) 配置与使用

    我们先来看一个例子,简单的了解一下mybatis的mapper接口方式的使用. package org.mybatis.spring.sample; import org.apache.ibatis. ...

  4. 知问前端——cookie插件

    Cookie是网站用来在客户端保存识别用户的一种小文件.一般可以保存用户登录信息.购物数据信息等一系列微小信息. 一.使用cookie插件 官方网站:http://plugins.jquery.com ...

  5. 【BZOJ4817】【SDOI2017】树点涂色 [LCT][线段树]

    树点涂色 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Bob有一棵n个点的有根树,其中1 ...

  6. 【HDU】5269 ZYB loves Xor I

    [算法]trie [题解] 为了让数据有序,求lowbit无法直接排序,从而考虑倒过来排序,然后数据就会呈现出明显的规律: 法一:将数字倒着贴在字典树上,则容易发现两数的lowbit就是它们岔道结点的 ...

  7. elementui table 多选 获取id

    //多选相关方法 toggleSelection(rows) { if (rows) { rows.forEach(row => { this.$refs.multipleTable.toggl ...

  8. canvas_简单练习

    效果图 实现原理: 1.定义canvas标签. 2.获取canvas标签节点,创建canvas2D. 3.在canvas进行画图. 效果代码: <!DOCTYPE html> <ht ...

  9. Spring Boot:定制自己的starter

    在学习Spring Boot的过程中,接触最多的就是starter.可以认为starter是一种服务——使得使用某个功能的开发者不需要关注各种依赖库的处理,不需要具体的配置信息,由Spring Boo ...

  10. Spark-2.3.2【SparkStreaming+SparkSQL-实时仪表盘应用】

    应用场景:实时仪表盘(即大屏),每个集团下有多个mall,每个mall下包含多家shop,需实时计算集团下各mall及其shop的实时销售分析(区域.业态.店铺TOP.总销售额等指标)并提供可视化展现 ...