Haybale Guessing
Time Limit: 1000MS   Memory Limit: 65536K
     

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
分析:二分答案,然后按A从大到小遍历;
   对于相同的值A,判断区间交是否成立,然后覆盖区间并;
   覆盖的过程可以用线段树或并查集实现;
   注意离散化过程排除[a,b]!=[a,a]U[b,b](a>b+1);
   可以排序后再在相邻的差值>1的点之间插值;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e6+;
const int N=2e5+;
using namespace std;
int id(int l,int r){return l+r|l!=r;}
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,mi[maxn<<],tag[maxn<<],q,ql[maxn],qr[maxn],qt[maxn],ip[maxn],cnt;
double a[maxn];
bool cmp(int x,int y){return qt[x]>qt[y];}
void pdw(int x,int y,int rt)
{
int mid=x+y>>,ls=id(x,mid),rs=id(mid+,y);
mi[ls]=mi[rs]=tag[rt];
tag[ls]=tag[rs]=tag[rt];
tag[rt]=;
}
void pup(int x,int y,int rt)
{
int mid=x+y>>;
mi[rt]=min(mi[id(x,mid)],mi[id(mid+,y)]);
}
void init(int l,int r,int rt)
{
mi[rt]=;
tag[rt]=;
if(l==r)return;
int mid=l+r>>;
init(l,mid,id(l,mid));
init(mid+,r,id(mid+,r));
}
void upd(int x,int y,int z,int l,int r,int rt)
{
if(x==l&&y==r)
{
mi[rt]=z;
tag[rt]=z;
return;
}
int mid=l+r>>;
if(tag[rt])pdw(l,r,rt);
if(y<=mid)upd(x,y,z,l,mid,id(l,mid));
else if(x>mid)upd(x,y,z,mid+,r,id(mid+,r));
else upd(x,mid,z,l,mid,id(l,mid)),upd(mid+,y,z,mid+,r,id(mid+,r));
pup(l,r,rt);
}
int gao(int x,int y,int l,int r,int rt)
{
if(x==l&&y==r)return mi[rt];
int mid=l+r>>;
if(tag[rt])pdw(l,r,rt);
if(y<=mid)return gao(x,y,l,mid,id(l,mid));
else if(x>mid)return gao(x,y,mid+,r,id(mid+,r));
else return min(gao(x,mid,l,mid,id(l,mid)),gao(mid+,y,mid+,r,id(mid+,r)));
}
bool ok(int x)
{
int i,j;
init(,cnt,id(,cnt));
rep(i,,x)ip[i]=i;
sort(ip+,ip+x+,cmp);
for(i=;i<=x;)
{
int l=ql[ip[i]],r=qr[ip[i]],xl=l,xr=r;
j=i;
while(j+<=x&&qt[ip[j+]]==qt[ip[i]])l=max(l,ql[ip[j+]]),r=min(r,qr[ip[j+]]),xl=min(xl,ql[ip[j+]]),xr=max(xr,qr[ip[j+]]),j++;
if(l>r)return false;
if(gao(l,r,,cnt,id(,cnt))>qt[ip[i]])return false;
upd(xl,xr,qt[ip[i]],,cnt,id(,cnt));
i=j+;
}
return true;
}
int main()
{
int i,j;
scanf("%d%d",&n,&q);
rep(i,,q)
{
scanf("%d%d%d",&ql[i],&qr[i],&qt[i]);
if(ql[i]>qr[i])swap(ql[i],qr[i]);
a[++cnt]=ql[i],a[++cnt]=qr[i];
}
sort(a+,a+cnt+);
for(i=cnt;i>=;i--)if(a[i]>a[i-]+)a[++cnt]=a[i-]+;
sort(a+,a+cnt+);
cnt=unique(a+,a+cnt+)-a-;
rep(i,,q)
{
ql[i]=lower_bound(a+,a+cnt+,ql[i])-a;
qr[i]=lower_bound(a+,a+cnt+,qr[i])-a;
}
int l=,r=q,ret;
while(l<=r)
{
int mid=l+r>>;
if(ok(mid))ret=mid,l=mid+;
else r=mid-;
}
assert(ret>=&&ret<=q);
printf("%d\n",ret+<=q?ret+:);
return ;
}

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. [USACO 08JAN]Haybale Guessing

    Description The cows, who always have an inferiority complex about their intelligence, have a new gu ...

  4. [USACO08JAN]haybale猜测Haybale Guessing

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

  5. [USACO08JAN]Haybale Guessing(LuoguP2898)

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

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

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

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

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

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

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

  9. 题解—P2898 [USACO08JAN]Haybale Guessing G

    pre 首先注意一下翻译里面并没有提到的一点,也是让我没看懂样例的一点,就是这个长度为 \(n\) 的数组里面的数各不相同. 有很多人用并查集写的这道题,题解里面也有一些用线段树写的,不过我认为我的做 ...

随机推荐

  1. bzoj 1601 灌水

    题目大意: 决定把水灌到n块农田,农田被数字1到n标记 把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库 建造一个水库需要花费wi,连接两块土地需要花费Pij. 计算所需的最少代价 ...

  2. DStream 转换操作----无状态转换

    DStream转换操作包括无状态转换和有状态转换. 无状态转换:每个批次的处理不依赖于之前批次的数据. 有状态转换:当前批次的处理需要使用之前批次的数据或者中间结果.有状态转换包括基于滑动窗口的转换和 ...

  3. 在Struts2中ognl.MethodFailedExceptiond异常的解决办法

    问题描述: 在 Struts2 里面,当页面向服务器提交参数时报ognl.MethodFailedException:和java.lang.NoSuchMethodException:异常 异常信息  ...

  4. java网络编程UDP

    图片来自网络 [发送端] import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSoc ...

  5. [Swift通天遁地]七、数据与安全-(13)单元测试的各个状态和应用

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. Dex文件方法数超过65536怎么破?

    你的应用中的Dex 文件方法数超过了最大值65536的上限,会提示你: UNEXPECTED TOP-LEVEL EXCEPTION:java.lang.IllegalArgumentExceptio ...

  7. ACM_lowbit

    lowbit Time Limit: 2000/1000ms (Java/Others) Problem Description: long long ans = 0; for(int i = 1; ...

  8. vb.net实现textbox控件输入指定位数小数方法实现。

    Private Sub textbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPres ...

  9. Java final和static 修饰符

    一.final final是不变的,最终的意思.可以用来修饰变量,方法,类. 1. 修饰变量 private final int a = 2; private final int b; // fina ...

  10. HTTP05--HTML常用知识

    一.URL地址含义 需要搞清URL和URI的差别,以及QueryString的含义. 二.GET和POST的区别 详细介绍可参考文章:http://zengrong.net/post/1802.htm ...