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 ≤ Ql ≤ N; Ql ≤ 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: 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的更多相关文章
- 洛谷 P2898 [USACO08JAN]haybale猜测Haybale Guessing 解题报告
[USACO08JAN]haybale猜测Haybale Guessing 题目描述 给一段长度为\(n\),每个位置上的数都不同的序列\(a[1\dots n]\)和\(q\)和问答,每个问答是\( ...
- POJ 3657 Haybale Guessing(区间染色 并查集)
Haybale Guessing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2384 Accepted: 645 D ...
- [USACO 08JAN]Haybale Guessing
Description The cows, who always have an inferiority complex about their intelligence, have a new gu ...
- [USACO08JAN]haybale猜测Haybale Guessing
题目描述 The cows, who always have an inferiority complex about their intelligence, have a new guessing ...
- [USACO08JAN]Haybale Guessing(LuoguP2898)
The cows, who always have an inferiority complex about their intelligence, have a new guessing game ...
- poj-3657 Haybale Guessing(二分答案+并查集)
http://poj.org/problem?id=3657 下方有中文版,不想看英文的可直接点这里看中文版题目 Description The cows, who always have an in ...
- 【[USACO08JAN]haybale猜测Haybale Guessing】
抄题解.jpg 完全完全不会啊,这道题简直太神了 不过抄题解可真开心 首先这道题目保证了每一个位置上的数都是不同的,那么就能得到第一种判断不合法的方式 如果两个区间的最小值一样,但是两个区间的交集为空 ...
- POJ - 3657 Haybale Guessing(二分+并查集)
题意:有N个大小各不相同的点,给定Q个询问,格式为q1,q2,A,表示区间q1~q2的最小值是A,问第一个与之前询问结果出现冲突的询问. 分析: 1.二分询问的标号mid,查询1~mid是否出现询问冲 ...
- 题解—P2898 [USACO08JAN]Haybale Guessing G
pre 首先注意一下翻译里面并没有提到的一点,也是让我没看懂样例的一点,就是这个长度为 \(n\) 的数组里面的数各不相同. 有很多人用并查集写的这道题,题解里面也有一些用线段树写的,不过我认为我的做 ...
随机推荐
- Codeforces--14D--Two Paths(树的直径)
Two Paths Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- openStack aio nova service-list neutron ext-list
- 解决UTF-8方法归纳
1:通过spring配置过滤器解决 <!-- 配置Spring提供的字符编码过滤器 --> <filter> <filter-name>SpringCharacte ...
- 设计模式 |备忘录模式(memento)
定义: 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 结构:(书中图,侵删) Originator:需要备份的类(写在便签上 ...
- 基于Android SDK安装PhoneGap框架
下载zip文件PhoneGap 2.0.0 PhoneGap 2.0.0 Released 20 Jul 2012http://phonegap.com/download/ 解压缩后的目录结构:Dir ...
- python中set元素为可迭代元素相加
#a 与 b必须是两个相同类型的可迭代对象 a = "1" b = "2" print(set(a + b)) # {'1', '2'} a = " ...
- 前端h5开发调试神奇vconsole
(1)项目中安装vconcole插件 npm install vconcole (2)在vue项目中main.js中引入插件 import Vconsole from 'vconsole'; cons ...
- unity3d 各键值对应代码
KeyCode :KeyCode是由Event.keyCode返回的.这些直接映射到键盘上的物理键. 值 对应键 Backspace 退格键 Delete Delet ...
- 黑马程序员 关于c# windows窗体关闭时线程未能完全退出问题(专题一)
<a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a> ...
- 查看/进入mac根目录的方式
1.通过“前往文件夹”快捷键组合 (1)打开finder,点击上部菜单栏“前往”,然后“个人”,直接跳转. (2)快捷键组合:command + shift + G:注意:打开finder后,再快捷键 ...