CF 558D(Guess Your Way Out! II-set解决区间问题)
2017-06-15 14:54
429 查看
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,
![](http://img.blog.csdn.net/20150717133414770)
),
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").
Output
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.
Sample test(s)
input
output
input
output
input
output
input
output
Note
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.
![](http://img.blog.csdn.net/20150717133419075)
有一堆区间,1个入口
给出例如以下条件,区间[L,R]有/无入口
问入口在哪?
用set维护
记得lower_bound(a) 是第一个>=a的
upper_bound(a) 是第一个>a的
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,
),
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").
Output
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.
Sample test(s)
input
3 1 3 4 6 0
output
7
input
4 3 4 10 14 1 3 6 6 0 2 3 3 1
output
14
input
4 2 3 4 6 1 4 12 15 1
output
Data not sufficient!
input
4 2 3 4 5 1 2 3 3 1
output
Game cheated!
Note
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解决区间问题)
- Guess Your Way Out! II---cf 558D (区间覆盖,c++STL map 的使用)
- codeforces #312 558D D. Guess Your Way Out! II(排序离散化)
- Codeforces 558D - Guess Your Way Out! II (求区间交,并)
- codeforces 558 D Guess Your Way Out! II
- 区间合并 --- Codeforces 558D : Gess Your Way Out ! II
- Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II (求区间的补、交)
- 区间合并 --- Codeforces 558D : Gess Your Way Out ! II
- CocoaPods did not set the base configuration of your project 问题解决方案
- cf_287 C. Guess Your Way Out!
- CocoaPods did not set the base configuration of your project 问题解决方案
- CocoaPods did not set the base configuration of your project 问题解决方式
- Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II
- CF C.Guess Your Way Out!
- Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序
- 558D - Guess Your Way Out! II
- VMware虚拟机ubuntu和主操作系统硬盘之间的文件共享的实现和What is the location of the directory of C header files that match your running问题的解决办法
- 解决连接MySql4.0数据库出现的"character_set_results"问题
- 关于php页面最大执行时间问题(set_time_limit函数在windows下不起作用的解决)
- Android问题集锦转载之一:解决Android requires .class compatibility set to 5.0. Please fix project properties