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\) 的数组里面的数各不相同. 有很多人用并查集写的这道题,题解里面也有一些用线段树写的,不过我认为我的做 ...
随机推荐
- 【HDU1530】【ZOJ1492】Maximum Clique
Position: http://poj.org/problem?id=3241 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...
- Codeforces--400A--Inna and Choose Options(模拟水题)
Inna and Choose Options Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:26 ...
- 【转载】深入理解Linux文件系统
1.rm-rf删除目录里的文件后,为什么可以恢复? 首先创建一个空目录test,目录的blocksize为4096字节 为了空目录还是4096?首先,目录的大小取决它所包含的文件的inode(访问 ...
- 9.10NOIP模拟题
9.10 NOIP模拟赛 题目名称 区间 种类 风见幽香 题目类型 传统 传统 传统 可执行文件名 section kinds yuuka 输入文件名 section.in kinds.in yu ...
- [Swift通天遁地]四、网络和线程-(2)通过BlockOperation实现线程的队列
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- golang——常用内建函数
(1)func len(v Type) int 返回长度,取决于具体类型:字符串返回字节数:channel返回缓存元素的个数: (2)func cap(v Type) int 返回容量,取决于具体类型 ...
- iOS 中OpenGL ES 优化 笔记 1
1,避免同步和Flushing操作 OpenGL ES的命令执行通常是在command buffer中积累一定量的命令后,再做批处理执行,这样效率会更高:但是一些OpenGL ES命令必须flush ...
- 【转】mysql的数据类型
转自:http://mrxiong.blog.51cto.com/287318/1651098 一.数值类型 Mysql支持所有标准SQL中的数值类型,其中包括严格数据类型(INTEGER,SMALL ...
- 我正在学英语是用learning english还是用studying english?
学一门语言用 learn. study 表示深入研究,一般指在大学里.如果大学里的专业是英语,就可以说 study English. 1. If you study hard, you will le ...
- Mysql(三):多表查询和存储程序
今天内容: ● 多表查询(内连接 外连接 子查询) ● 存储程序(存储过程 函数) 多表查询 同时从多张数据表中查取到需要的数据即是多表查询. 多表查询时,参与查询的表中每条数据进行组合,这种效果 ...