Codeforces 558 D Guess Your the out! Ii

Source: Internet
Author: User

Test instructions is like this:

A perfect binary tree with a height of h, a root node of 1, and the left and right sons of the nodes labeled I are 2*i,2*i+1

Q Operation, I,l,r,ans

Ans==0, the ancestor of the exit is not between [l,r] when it is represented on the first layer.

When Ans==1, the ancestor of the exit is represented in layer I, between [L,r]


If the exit (the exit must be on the leaf) the only output its label, not unique or no solution to output the corresponding string


The way I think about it, it's obvious that all the ans==1 of all the trees in the tree are counted, traversing all the intervals,

Constant intersection, if there is no intersection, then no solution, so the result must be a unique interval x, the export must be in this area.


All the leaves of the ans==0 subtree are counted, traversing all the intervals, and X constantly deleting the part that intersects them,

Obviously if the final x interval is only one number, then the exit is the only number, otherwise there are multiple solutions


It's easy to think of this. Coding a little bit troublesome ... But you can still write the right things slowly.


#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;typedef long Long ll;struct itv{ll L,r;itv () {}ITV (ll l,ll R) {this->l=l; This->r=r;} ITV its (ITV one) {if (one.r<l| | One.l>r) return ITV (1,0); return ITV (Max (one.l,l), Min (one.r,r));}}; LL Fac[60]={1};itv CG (ll val,int Lv,int h) {return Itv (fac[h-lv]*val,fac[h-lv]*val+fac[h-lv]-1);} BOOL CMP (ITV ONE,ITV) {return ONE.L&LT;TWO.L;} int main () {int h,q;cin>>h>>q;for (int i=1;i<=h;i++) Fac[i]=fac[i-1]*2;vector<itv>itv[2];while ( q--) {int i,ans;ll l,r;cin>>i>>l>>r>>ans;itv[ans].push_back (ITV (CG (L,I,H). L,CG (r,i,h). R)) ;} int n=itv[0].size (), M=itv[1].size (), for (int i=1;i<m;i++) {itv[1][0]=Itv[1][0].its (Itv[1][i]); if (ITV[1][0].L&GT;ITV[1][0].R) {puts ("Game cheated!"); return 0;}} Itv t;if (m!=0) T=ITV[1][0];ELSET=ITV (CG (1,1,H). L,CG (1,1,h). R); sort (Itv[0].begin (), Itv[0].end (), CMP); ll Ans=-1;for (int i=0;i<n;i++) {ITV t1=t.its (Itv[0][i]), if (T1.L&GT;T1.R) continue;if (T1.L==T.L&AMP;&AMP;T1.R==T.R) {if (Ans==-1) puts ("Game cheated !"); Elsecout<<ans;return 0;} if (t1.l-t.l>1) {puts ("Data not sufficient!"); return 0;} if (t1.l-t.l==1) {if (ans==-1) {ans=t.l;if (T.R==T1.R) {Cout<<ans;return 0;}} Else{puts ("Data not sufficient!"); return 0;}} t.l=t1.r+1;} if (ans==-1) {if (T.L==T.R) cout<<t.l;elseputs ("Data not sufficient!");} Elseputs ("Data not sufficient!");}




Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

AMR bought a new video game "Guess Your-out! II ". The goal of the game is to find a exit from the maze so 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 was located at some leaf node.

Let's index all the nodes of the tree such that

  • the Root is Number 1
  • each Internal node < Span class= "Tex-span" style= "font-size:17.5px; Font-family: ' Times New Roman ', Sans-serif; White-space:nowrap "> i   ( i ? ≤?2 h ?-? 1 ?-? 1 ) would have a left child with index = 2 i  and a right child with index = 2 i ? +?1

The Level of a node is defined As 1  for a root, Or 1  + level of the parent of the node otherwise. The vertices of the Level  h  are called leaves. The exit to the maze was located at some leaf node  n , the player doesn ' t know where the exit was so he had to guess his out!

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 nodevThat located at the levelI. The game would answer with a "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 your to help him. Given the questions and its answers, can you identify whether the game was telling contradictory information or not? If the information is not contradictory and the exit node can be determined uniquely, the output of its number. If the information isn't contradictory, but the exit node isn't defined uniquely, output that number of questions is Not sufficient. Otherwise output, the information is contradictory.

Input

The first line contains integers h,? Q (1?≤? ) H. ≤?50, 0?≤? Q. ≤?105), the height of the tree and the number of questions respectively.

The nextQLines'll 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 13 4) 6 0
Output
7
Input
4 34 10 14 13 6 6 02 3 3 1
Output
14
Input
4 23 4 6 14 12 15 1
Output
Data not sufficient!
Input
4 23 4 5 12 3 3 1
Output
Game cheated!
Note

Node u is a ancestor of node v if and only if

  • u is the same node as v,
  • U is the parent of node v,
  • or u are 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 node is ' t in the Range [4,?6]  so The exit is node Number 7 .

In the second sample test there is 8 leaf nodes. After the first question the exit was in the range [10,?14]. After the second and the third questions only node number is correct. Check the picture below to fully understand.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 558 D Guess Your the out! Ii

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.