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. [python基础] celery beat/task/flower解析

    一.Celery 介绍 Celery 是一个强大的分布式任务队列,它可以让任务的执行完全脱离主程序,甚至可以被分配到其他主机上运行.我们通常使用它来实现异步任务( async task )和定时任务( ...

  2. [python基础]xml_rpc远程调控supervisor节点进程

    supervisor提供的两种管理方式,supervisorctl和web其实都是通过xml_rpc来实现的. xml_rpc其实就是本地可以去调用远端的函数方法,在python中只需要引入xmlrp ...

  3. poj3264Balanced Lineup(倍增ST表)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 52328   Accepted: 24551 ...

  4. [App Store Connect帮助]一、 App Store Connect 使用入门(1)App Store Connect 工作流程

    您使用 App Store Connect 提交并管理您在 App Store 中销售的 App,使用 TestFlight 分发您 App 的 Beta 版本,接受法律协议,输入您的税务和银行业务信 ...

  5. mybatis传参问题总结

    一. 传入单个参数 当传入的是单个参数时,方法中的参数名和sql语句中参数名一致即可 List<User> getUser(int id); <select id="get ...

  6. poi 和jxl导出excel(2)

    controller: /** * 导出报表 * @return */ @RequestMapping(value = "/export") @ResponseBody publi ...

  7. 【转】linux之type命令

    转自: http://codingstandards.iteye.com/blog/831504 用途说明 type命令用来显示指定命令的类型.一个命令的类型可以是如下之一 alias 别名 keyw ...

  8. Tomcat发布项目,域名访问

    域名访问项目 1,去掉访问路径的端口号: 找到 Tomcat 下的 conf 文件中的 server.xml,找到 8080 修改成 80, 2,项目绑定域名: <Host name=" ...

  9. [转]Linux下/proc目录简介

    1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文 ...

  10. Raspberry Pi开发之旅-光照强度检测(BH1750)

    一.前期准备 1.环境要求 GY30模块(BH1750FVI传感器),树莓派系统,python-smbus,iic开启 2.取消对IIC驱动的黑名单 nano /etc/modprobe.d/rasp ...