Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II (求区间的补、交)

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, ), 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.

题意:

出口exit在一个高度为h的二叉树的叶子节点中。

现在给出很多个判定 exit在第lv层的祖先是否在[L,R]范围内。用四元组(lv,L,R,ok)表示

问能不能唯一确定出口。能或者不能(矛盾)或者信息不完全

 

分析:

把每一个判定转化到叶子节点那一层的一个区间。

所有真命题的叶子节点这层的区间求交,即左端点取大,右端点取小。

所有假命题的区间递推到叶子节点这一层,求补后可得到两个不连续的区间,用这些区间与真命题的交一个个求交

如果求得的交的元素个数累加和大于1则信息不完全。

如果个数为0,则是矛盾。

如果为1,输出交(节点的值)

 

用什么来表示区间的补呢?

方法是用一个pair,对每个区间的左端点附加信息-1,右端点附加信息1,排序后从左往右扫,累加这个附加信息,当累加和等于0时,

说明在该点前的所有区间都结束了,那么从这个点到下一个区间的起点这一段是可能有答案的区间。

 

注意要把总区间的左右端点也加紧取才能保证不遗漏区间。

 

PLZ  take care~

long long 的移位运算不能写成(1<<(h-1)),要写成(1LL<<(h-1))

因为这找了1h的错。。。。ORZ

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<vector>
 8 #include<queue>
 9 #include<map>
10 #include<set>
11 
12 using namespace std;
13 
14 typedef long long ll;
15 
16 vector<pair<ll,int> > G;
17 vector<pair<ll,ll> > N;
18 
19 int main()
20 {
21     int h,q,lv,ok;
22     ll l,r,al,ar;
23     while(~scanf("%d%d",&h,&q))
24     {
25         G.clear();
26         N.clear();
27         ll L,R;
28         L = (1LL<<(h-1));
29         R = (1LL<<h)-1;
30         al = L,ar = R;
31         while(q--)
32         {
33             scanf("%d%I64d%I64d%d",&lv,&l,&r,&ok);
34             l = (l<<(h-lv));
35             r++;
36             r = (r<<(h-lv));
37             r--;
38             if(ok)
39             {
40                 al = max(al,l);
41                 ar = min(ar,r);
42             }
43             else
44             {
45                 G.push_back(make_pair(l,-1));
46                 G.push_back(make_pair(r,1));
47             }
48         }
49         if(al>ar)
50         {
51             printf("Game cheated!\n");
52             continue;
53         }
54         G.push_back(make_pair(L-1,0));
55         G.push_back(make_pair(R+1,0));
56         sort(G.begin(),G.end());
57         int res = 0;
58         for(int i=0;i<G.size()-1;i++)
59         {
60             res += G[i].second;
61             if(res==0)
62             {
63                 N.push_back(make_pair(G[i].first+1,G[i+1].first-1));
64             }
65         }
66         ll ans=-1,num = 0;
67         for(int i=0;i<N.size();i++)
68         {
69             ll le = N[i].first;
70             ll ri = N[i].second;
71             if(le>ri) continue;
72             le = max(al,le);
73             ri = min(ar,ri);
74             if(ri>=le)
75             {
76                 num += ri-le+1;
77                 ans = ri;
78             }
79         }
80         if(num>1)
81             printf("Data not sufficient!\n");
82         else if(num==0)
83             printf("Game cheated!\n");
84         else printf("%I64d\n",ans);
85     }
86     return 0;
87 }
View Code

 

 

 

posted @ 2015-07-29 17:02  fukan  阅读(322)  评论(0编辑  收藏  举报