Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II (求区间的补、交)
2015-07-29 17:02
411 查看
D. Guess Your Way Out! II
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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,
View Code
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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,
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cstdlib> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> using namespace std; typedef long long ll; vector<pair<ll,int> > G; vector<pair<ll,ll> > N; int main() { int h,q,lv,ok; ll l,r,al,ar; while(~scanf("%d%d",&h,&q)) { G.clear(); N.clear(); ll L,R; L = (1LL<<(h-1)); R = (1LL<<h)-1; al = L,ar = R; while(q--) { scanf("%d%I64d%I64d%d",&lv,&l,&r,&ok); l = (l<<(h-lv)); r++; r = (r<<(h-lv)); r--; if(ok) { al = max(al,l); ar = min(ar,r); } else { G.push_back(make_pair(l,-1)); G.push_back(make_pair(r,1)); } } if(al>ar) { printf("Game cheated!\n"); continue; } G.push_back(make_pair(L-1,0)); G.push_back(make_pair(R+1,0)); sort(G.begin(),G.end()); int res = 0; for(int i=0;i<G.size()-1;i++) { res += G[i].second; if(res==0) { N.push_back(make_pair(G[i].first+1,G[i+1].first-1)); } } ll ans=-1,num = 0; for(int i=0;i<N.size();i++) { ll le = N[i].first; ll ri = N[i].second; if(le>ri) continue; le = max(al,le); ri = min(ar,ri); if(ri>=le) { num += ri-le+1; ans = ri; } } if(num>1) printf("Data not sufficient!\n"); else if(num==0) printf("Game cheated!\n"); else printf("%I64d\n",ans); } return 0; }
View Code
相关文章推荐
- UISearchController
- iOS5中UIViewController的新方法
- 创建servlet类时出现的“private static final long serialVersionUID = 1L;”语句是什么意思啊?
- UIScrollView下拉刷新效果实现
- 如何重建 wf_java_deferred 队列(queue)
- UITableViewCell滑动删除及移动
- SQL Server 2012 SEQUENCE 使用
- UIImageView设置手势无效
- easyui-combobox下拉多选
- 短时间大量connect()返回errno 99: cannot assign request address
- iOS AutoLayout报错包含UIView-Encapsulated-Layout-Width
- 黑马程序员——高新技术---Java基础-GUI编程
- UIKit框架-12.UITableView数据刷新
- Ubiquitous Religions(POJ--2524
- Unique Path II
- IOS开发概述-09.UIController和控制器的View
- 【进阶android】Volley源码分析——Volley的工具【StringRequest】
- iOS开发使用Unwind Segue进行返回
- Spark SQL and DataFrame Guide(1.4.1)——之DataFrames
- UIActionSheet