CF 558D(Guess Your Way Out! II-set解决区间问题)
2 seconds
256 megabytes
standard input
standard output
Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h.
The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the nodes of the tree such that
- The root is number 1
- Each internal node i (i ≤ 2h - 1 - 1)
will have a left child with index = 2i and a right child with index = 2i + 1
The level of a node is defined as 1 for a root, or 1 +
level of parent of the node otherwise. The vertices of the level h are called leaves. The exit to the maze is located at some leaf node n,
the player doesn't know where the exit is so he has to guess his way out!
In the new version of the game the player is allowed to ask questions on the format "Does the ancestor(exit, i) node
number belong to the range [L, R]?". Here ancestor(v, i) is
the ancestor of a node v that located in the level i.
The game will answer with "Yes" or "No" only. The game is designed such that it doesn't always answer correctly, and sometimes it cheats to confuse the player!.
Amr asked a lot of questions and got confused by all these answers, so he asked you to help him. Given the questions and its answers, can you identify whether the game is telling contradictory information or not? If the information is not contradictory and
the exit node can be determined uniquely, output its number. If the information is not contradictory, but the exit node isn't defined uniquely, output that the number of questions is not sufficient. Otherwise output that the information is contradictory.
The first line contains two integers h, q (1 ≤ h ≤ 50, 0 ≤ q ≤ 105),
the height of the tree and the number of questions respectively.
The next q lines will contain four integers each i, L, R, ans (1 ≤ i ≤ h, 2i - 1 ≤ L ≤ R ≤ 2i - 1, ),
representing a question as described in the statement with its answer (ans = 1 if the answer is "Yes" and ans = 0 if
the answer is "No").
If the information provided by the game is contradictory output "Game cheated!" without the quotes.
Else if you can uniquely identify the exit to the maze output its index.
Otherwise output "Data not sufficient!" without the quotes.
3 1
3 4 6 0
7
4 3
4 10 14 1
3 6 6 0
2 3 3 1
14
4 2
3 4 6 1
4 12 15 1
Data not sufficient!
4 2
3 4 5 1
2 3 3 1
Game cheated!
Node u is an ancestor of node v if
and only if
- u is the same node as v,
- u is the parent of node v,
- or u is an ancestor of the parent of node v.
In the first sample test there are 4 leaf nodes 4, 5, 6, 7.
The first question says that the node isn't in the range [4, 6] so the exit is node number 7.
In the second sample test there are 8 leaf nodes. After the first question the exit is in the range [10, 14].
After the second and the third questions only node number 14 is correct. Check the picture below to fully understand.
有一堆区间,1个入口
给出例如以下条件,区间[L,R]有/无入口
问入口在哪?
用set维护
记得lower_bound(a) 是第一个>=a的
upper_bound(a) 是第一个>a的
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<stack>
#include<set>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXH (50+10)
#define MAXQ (100000+10)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,ll> pll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
char s1[]="Game cheated!\n",s2[]="Data not sufficient!\n";
int h,q;
pll cro(pll p,ll l,ll r)
{
ll a=p.first,b=p.second;
if (b<l||r<a) return mp(-1,-1);
return mp(max(a,l),min(b,r));
}
set<pll > S;
stack<pll > ask;
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout); cin>>h>>q;
pll ans=mp(1LL<<(h-1),(1LL<<h) - 1); ll L=1LL<<(h-1),R=(1LL<<h) - 1; For(qcase,q)
{
int i,b;
ll l,r;
cin>>i>>l>>r>>b;
while (i<h) l<<=1,r=(r<<1)^1,++i;
if (b) ans=cro(ans,l,r);
else ask.push(mp(l,r));
}
if (ans.fi==-1) {
cout<<s1;
return 0;
}
S.insert(ans);
while (!ask.empty())
{
pll now=ask.top();
ask.pop(); set<pll>::iterator it,it2;
it=S.upper_bound(now);
if (it!=S.begin()) it--;
for(;it!=S.end();)
{
pll pit=*it; if (now.se<pit.fi) break;
if (cro(now,pit.fi,pit.se).fi==-1) {
it++;continue;
}
it2 = it;
it2++; if (pit.fi<now.fi) S.insert(mp(pit.fi,now.fi-1));
if (pit.se>now.se) S.insert(mp(now.se+1,pit.se));
S.erase(it); it=it2;
} } // cout<<ans.first<<' '<<ans.second<<endl; if (S.empty())
{
cout<<s1;
return 0;
}
if (S.size()==1)
{
ll p1=S.begin()->fi,p2=S.begin()->se; if (p1==p2) {
cout<<p1<<endl;
return 0;
}
} cout<<s2; return 0;
}
CF 558D(Guess Your Way Out! II-set解决区间问题)的更多相关文章
- 区间合并 --- Codeforces 558D : Gess Your Way Out ! II
D. Guess Your Way Out! II Problem's Link: http://codeforces.com/problemset/problem/558/D Mean: 一棵满二叉 ...
- codeforces 558D Guess Your Way Out! II 规律
题目链接 题意: 给出n和q 表示有一棵深度为n的全然二叉树.叶子节点中有恰好一个点是出口 主角从根往下走.但不知道出口在哪里,但主角会获得q个提示. 像这样标号 q个提示 格式: deep [l, ...
- CF R 635 div1 C Kaavi and Magic Spell 区间dp
LINK:Kaavi and Magic Spell 一打CF才知道自己原来这么菜 这题完全没想到. 可以发现 如果dp f[i][j]表示前i个字符匹配T的前j个字符的方案数 此时转移变得异常麻烦 ...
- LightOJ - 1245 Harmonic Number (II) 求同值区间的和
题目大意:对下列代码进行优化 long long H( int n ) { long long res = 0; for( int i = 1; i <= n; i++ ) ...
- jump-game i&&ii 能否跳出区间 贪心
I: Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化
http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...
- FZU Problem 2171 防守阵地 II (线段树区间更新模板题)
http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...
- CF卡技术详解——笔记
知识太全面了,摘抄摘不完,还是粘过来加上注释和笔记吧. 重点以及断句用加粗,注释用红括号. 一.CF卡技术及规格 一.CF卡技术及规格 1.CF卡简史 随着数码产品的高速普及,近年来闪存卡也进入了高速 ...
- CF A.Mishka and Contest【双指针/模拟】
[链接]:CF/4892 [题意]: 一个人解决n个问题,这个问题的值比k小, 每次只能解决最左边的或者最右边的问题 解决了就消失了.问这个人能解决多少个问题. [代码]: #include<b ...
随机推荐
- 调整mysql数据库最大连接数
1.查看mariadb数据库最大连接数,默认为151 MariaDB [(none)]> show variables like 'max_connections'; +------------ ...
- linux内核(四)内存管理单元MMU
1,基本概念 一个程序运行时没必要全部都同时装入内存,只需要把当前需要运行的部分装入内存即可,这样就使得一个大程序可以在较小的内存中运行,也使得内存中可以同时装入更多的程序并发执行,从用户角度看,该系 ...
- [CSS3] The picture element
<picture> <source media="(min-width: 1000px)" srcset="kookaburra_large_1x.jp ...
- 混合高斯模型的EM求解(Mixtures of Gaussians)及Python实现源代码
今天为大家带来混合高斯模型的EM推导求解过程. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVhbnl1YW5zZW4=/font/5a6L5L2T/ ...
- 游标和动态SQL
游标类别:静态游标(指在编译的时候,游标就与一个select语句进行了静态绑定的游标.这样的游标仅仅能作用于一个查询语句)和动态游标(就是希望我们的查询语句在执行的时候才跟游标绑定.为了使用动态游标, ...
- iOS 常见小问题
1. iOS 编译后上下有黑边 ? 缺少启动图片 2.Failed to instantiate the default view controller for UIMainStoryboardFil ...
- JAVA设计模式之【建造者模式】
建造者模式 建造者模式为客户端返回的不是一个简单的产品,而是一个由多个部件组成的复杂产品 角色 Builder抽象建造者 buildPartX getResult ConcreteBuilder具体建 ...
- (四)Hystrix容错保护
Feign默认是整合了Ribbon和Hystrix这两个框架,所以代码我们在上一篇的基础上进行修改,启动Eureka,service-hello,Feign 所谓的熔断机制和日常生活中见到电路保险丝是 ...
- Linux mount挂载umount卸载
mount/umount挂载/卸载 对于Linux用户来讲,不论有几个分区,分别分给哪一个目录使用,它总归就是一个根目录.一个独立且唯一的文件结构 Linux中每个分区都是用来组成整个文件系统的一部分 ...
- Jlink如何校验Hex
打开SEGGER J-Flash ARM,记住必须设置好工程属性后才能校验,否则校验和为0 设置好工程属性后,直接将Hex拖入到工程中即可.在LOG窗口中会显示CRC的值 如何校验芯片中的程序的版本号 ...