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. POJ3420 递推+矩阵快速幂

    POJ3420 很有趣的覆盖问题 递归推导如下: f[n] = f[n-1] + 4*f[n-2] + 2 * [ f[n-3] + f[n-5] + f[n-7] +.... ] + 3 *  [ ...

  2. 【转载】[Oracle] Linux下手动创建数据库过程

    今天发现一个Oracle测试库的字符集设置不正确,原本的字符集是UTF-8,正确的字符集应该是ZHS16GBK,因为UTF-8是ZHS16GBK的超集,无法修改,只能重建数据库,幸好该测试库上还没有数 ...

  3. Gym - 101208C 2013 ACM-ICPC World Finals C.Surely You Congest 最大流+最短路

    题面 题意:给你n(2w5)个点,m条边(7w5)有k(1e3)辆车停在某些点上的,然后他们都想尽快去1号点,同时出发,同一个点不允许同时经过, 如果多辆车同时到达一个点,他们就会堵塞,这时候只能选择 ...

  4. akka设计模式系列-Aggregate模式

    所谓的Aggregate模式,其实就是聚合模式,跟masterWorker模式有点类似,但其出发点不同.masterWorker模式是指master向worker发送命令,worker完成某种业务逻辑 ...

  5. python自动化测试学习笔记-4常用模块

    常用模块 1.os 2.sys 3.random 4.string 5.time 6.hashlib 一.os模块 os模块主要用来操作文件.目录,与操作系统无关.要使用os模块首先要导入OS模块,用 ...

  6. Linux命令(003) -- crontab

    一.准备知识 Linux下的任务调度分为两类:系统任务调度和用户任务调度. (1).系统任务调度 系统任务调度是系统周期性所要执行的工作,比如写缓存数据到硬盘.日志清理等.在/etc目录下有一个cro ...

  7. P1538 迎春舞会之数字舞蹈

    题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...

  8. python--11、数据库及SQL基础

    常用命令记录 查看库中所有表的引擎 SHOW TABLE STATUS FROM `center_main_db`; 还有一个更简洁,查询cmol_system_db库所有表的存储引擎\ SELECT ...

  9. fcc html5 css 练习3

    行内样式看起来是这样的 <h1 style="color: green"> .pink-text { color: pink !important; }         ...

  10. 易语言 打开exe可执行文件、打开网页

    打开文件--------按钮被单击事件 直接复制以下代码即可 .版本 2 .子程序 _按钮58_被单击 运行 (“exe文件路径”, 假, ) 打开网站--------按钮被单击事件 直接复制以下代码 ...