http://poj.org/problem?id=3657

下方有中文版,不想看英文的可直接点这里看中文版题目

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 ≤ NQl ≤ 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.

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: QlQh, 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


Sample Output

 

说明

The minimum number of bales in stacks 1..10 is 7, the minimum number of bales in stacks 5..19 is 7, the minimum number of bales in stacks 3..12 is 8, and the minimum number of bales in stacks 11..15 is 12.
Query 3 ("3 12 8") is the first that is inconsistent with those before it. From queries 1 and 2 and the fact that all hay stacks have a distinct number of bales, we deduce that one of stacks 5..10 must contain exactly 7 bales. However, this stack contradicts the answer to query 3.

中文版

题意:

给一段长度为n,每个位置上的数都不同的序列a[1..n]和q和问答,然后给出q个区间及其区间最小值,求出到第几个区间会出现矛盾

思路:

1.二分+线段树(暂时不会,以后填坑)

2.二分+并查集(并查集用来区间染色)//并查集有个经典用法--区间染色,所以用并查集维护

首先 如果我们想知道这头奶牛之前的奶牛回答的是不是错的怎么办呢?

把回答的A从大到小排个序。这里有几种矛盾的方式:

  • 如果后面的区间完全被前面的区间包含,这是错的
  • 如果有两个不相交的区间的A是一样的,这也是错的(题目保证没有两堆干草的数量是一样的)

注意取相同A的区间的时候不要超过当前二分的mid

以下题解部分参考于:https://blog.csdn.net/wang2147483647/article/details/60142150

由于每个位置的数唯一,对于两个区间[l,r]最小值为a、[L,R]最小值为A。

若区间[l,r]被区间[L,R]完全包含且a<A,此时存在矛盾且为唯一的矛盾。

则可以二分询问Q,判断1---tot内的询问是否合法。每次二分时,将1---tot之间的询问按照最小值从大到小排序(优先处理大数,以后判断时仅需判断小数所在的区间是否被大数所在区间包含)。

由于每个数唯一,对于每个最小值相同的区间,判断其交集是否为空或者是否在更大最小值的区间中,此时出现矛盾,继续二分。

若未出现矛盾,则将其并集染色(这样判断矛盾时若交集所在区间中无未染色区间,则交集在最小值更大的区间中(最小值从大到小排序,更大最小值的区间已被全部染色,若无未染色区间,则说明此区间在最小值更大区间中))。

染色若用线段树,则无优化下会超时,所以可用并查集处理染色:对于一段区间[l,r]若将其染色,则设fa[r]=l-1(不可为l,因为l也为已染色点,例如数据 1 2 1和1 2 2),代表[l,r]中的数已全部染色(从后向前找对于每一个区间中的点都可以直接跳到其父节点,因为该区间已被全部染色),则判断时若l>Find(r)则说明该区间中无未染色点。

 代码如下:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
const int INF=0x3f3f3f3f;
using namespace std;
#define maxn 1000010 struct node{
int l;
int r;
int num;
}; int n,q;
int fa[maxn];
node a[maxn];
node tmp[maxn]; int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
} bool cmp(node a,node b)
{
return a.num>b.num;
} bool judge(int tot)
{
for(int i=;i<=n;i++)
fa[i]=i;
for(int i=;i<=tot;i++)
tmp[i]=a[i];
sort(tmp+,tmp++tot,cmp);
for(int i=,j;i<=tot;i=j+)
{
j=i;
int l=tmp[i].l;
int r=tmp[i].r;
int L=tmp[i].l;
int R=tmp[i].r;
while(j<tot&&tmp[j].num==tmp[j+].num)
{
j++;
l=max(l,tmp[j].l);//取交集
r=min(r,tmp[j].r);
L=min(L,tmp[j].l);//取并集
R=max(R,tmp[j].r);
}
if(l>r||l>Find(r))//为空或无未染色点
return ;
while(L<=R)
{
if(Find(R)==R)
{
fa[R]=Find(L-);//染色
R--;
}
else
R=fa[R];//直接跳转到其父节点
}
}
return ;
} int main()
{
scanf("%d %d",&n,&q);
for(int i=;i<=q;i++)
{
scanf("%d %d %d",&a[i].l,&a[i].r,&a[i].num);
}
int L=;
int R=q;
int ans=;
while(L<=R)
{
int mid=(L+R)/;
if(judge(mid))
L=mid+;
else
{
ans=mid;
R=mid-;
}
}
printf("%d\n",ans);
return ;
}

poj-3657 Haybale Guessing(二分答案+并查集)的更多相关文章

  1. [HNOI2006]公路修建问题 (二分答案,并查集)

    题目链接 Solution 二分答案+并查集. 由于考虑到是要求花费的最小值,直接考虑到二分. 然后对于每一个二分出来的答案,模拟 \(Kruskal\) 的过程再做一遍连边. 同时用并查集维护联通块 ...

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

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

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

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

  4. 洛谷P1991无线通讯网[kruskal | 二分答案 并查集]

    题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...

  5. BZOJ 1196 [HNOI2006]公路修建问题(二分答案+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1196 [题目大意] 对于每条可能维修的公路可选择修一级公路或者二级公路,价值不同 要求 ...

  6. HDU-3081-Marriage Match 2(最大流, 二分答案, 并查集)

    链接: https://vjudge.net/problem/HDU-3081 题意: Presumably, you all have known the question of stable ma ...

  7. [NOI2014] 魔法森林 (二分答案,并查集)

    本思路仅供参考,数据强一点应该该会被卡. 本蒟蒻没有打 \(link\) - \(cut\) - \(tree\) . 而是用暴力水了过去. 具体思路很简单,先二分最少的 \(a_i\) , 再在 \ ...

  8. HNOI2006-公路修建问题(二分答案+并查集)

    公路修建问题 OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织成立了,旨 ...

  9. BZOJ 1196 二分答案+并查集

    http://www.lydsy.com/JudgeOnline/problem.php?id=1196 题目大意:n个城市,m-1条路,每条路有一级公路和二级公路之分,你要造n-1条路,一级公路至少 ...

随机推荐

  1. 吴裕雄--天生自然C++语言学习笔记:C++ 注释

    程序的注释是解释性语句,可以在 C++ 代码中包含注释,这将提高源代码的可读性.所有的编程语言都允许某种形式的注释. C++ 支持单行注释和多行注释.注释中的所有字符会被 C++ 编译器忽略. C++ ...

  2. [安洵杯 2019]easy_web

    0x00 知识点 md5强类型的绕过 方法比较固定: POST: a=%4d%c9%68%ff%0e%e3%5c%20%95%72%d4%77%7b%72%15%87%d3%6f%a7%b2%1b%d ...

  3. Java算法练习——寻找两个有序数组的中位数

    题目链接 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 $O(log(m + n))$. 你可以假设 nu ...

  4. Emoji表情符号兼容方案

    Emoji表情符号兼容方案 一 什么是Emoji    emoji就是表情符号:词义来自日语(えもじ,e-moji,moji在日语中的含义是字符) 表情符号现已普遍应用于手机短信和网络聊天软件. em ...

  5. UML-状态机图和建模

    1.目标:如何画状态机图 2.定义:描述某个对象的状态.感兴趣的事件.以及对象响应该事件的行为. 转换:用箭头表示 状态:用圆角矩形表示 事件:指的是一件值得注意的事情的发生.如:拿起电话. 当事件“ ...

  6. UML-设计对象时涉及的制品有哪些?

    1.SSD.系统操作.交互图--->用例实现 1).通信图 2).顺序图 将SSD中的系统操作作为领域层控制器对象的起始消息.这是关键. 2.用例--->用例实现 在整个开发过程中,业务人 ...

  7. liburl常见库函数解释

    curl_slist_append - add a string to an slist(单链表) struct curl_slist *headerlist=NULL;static const ch ...

  8. 物体检测中常用的几个概念迁移学习、IOU、NMS理解

    1.迁移学习 迁移学习也即所谓的有监督预训练(Supervised pre-training),我们通常把它称之为迁移学习.比如你已经有一大堆标注好的人脸年龄分类的图片数据,训练了一个CNN,用于人脸 ...

  9. Salt常用命令二

    ############################模块############################ 查看模块列表——module salt ‘minion’  sys.list_mo ...

  10. aop 实现原理

    aop 底层采用代理机制实现 接口 + 实现类 :spring 采用 jdk 的 动态代理 只有实现类:spring 采用 cglib 字节码增强 aop专业术语 1.target(目标) 需要被代理 ...