Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序

D. Guess Your Way Out! II

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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.

题意就是,给出h层的完全二叉树,从上到下从左到右标号1 - 2^n,要找出出口,出口在叶子结点处。再给q个区间,表示这个区间的子结点的叶子结点中有一个是出口,或都不是出口。

我们用区间s e表示其中可能是出口,那么如果给的区间是出口,只需要求交集就可以了。得到的结果仍然是一个区间。如果,给的区间,不是出口,那就是把这部分排除啦。这样就可以会出现多个集合了。如果,就这样不断扩展,有可能最张出现q^2个集区,复杂度就会达到n^2了,这样是不行的。考虑一种贪心方法,因为只有能去掉两端的结点,这样的区间才是有效的,如果是在中间,是对结果没有影响的,如图中,2 3 对0是有影响的,在没有2 3 的情况 1号线,是对结果没有影响的,所以我们只需要通过排序,使得 2 3这样的先遇上就可以,排序后,从前往后,再从后向前,就一定保证了 2 3这种在前面,而且,不会使得结果集扩大成多集一直是单集。总的复杂度就是排序复杂度o(q * log(q));

#define N 100050
#define M 100005
#define maxn 205
#define fi first
#define se second
#define MOD 1000000000000000007
int h,q,layer[N],flag[N],pn;
ll s,e,ss,ee,l[N],r[N];
pll p[N];
void get(int layer,ll l,ll r ){
    ss = l << (h - layer);
    ee = r;
    FI(h - layer) ee = ee * 2 + 1;
}
int main()
{
    while(S2(h,q)!=EOF)
    {
        s = 1ll<<(h-1);
        e = (1ll<<h) - 1ll;
        bool isRight = true;
        pn = 0;
        FI(q)
        {
            S(layer[i]);cin>>l[i];cin>>r[i];S(flag[i]);
            if(isRight){
                get(layer[i],l[i],r[i]);
                if(flag[i]){
                    s = max(s,ss);e = min(e,ee);
                    if(s > e) isRight = false;
                }
                else {
                    p[pn++] = make_pair(ss,ee);
                }
            }
        }
        if(isRight){
            sort(p,p+pn);
            FI(pn){
                if(p[i].first <= s && p[i].second >= s)
                s = p[i].second + 1;
            }
            for(int i= pn-1;i>=0;i--){
                if(p[i].first <= e && p[i].second >=e)
                e = p[i].first - 1;
            }
        }
        if(s > e)printf("Game cheated!\n");
        else if(s == e) cout<<s<<endl;
        else printf("Data not sufficient!\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 07:12:07

Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序的相关文章

Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)

A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year.

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序 题意 有 a[ i ] 个数 要求选最多的数 使其和不超过 S ,且在此情况下,和最小选最多数情况下 和最小 且 每个数有加成 如果选了 k个数 那么加成后 就是 a[ i ] + k*i ; 题解 二分mid 表示选了个数 加成一下,将加成以后结果排序一下 , 若前 mid数 和大于 s 则此方案不可行 PS 要用 long long ..... 还有 co

Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力

A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/problem/A Description Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous

Codeforces Round #312 (Div. 2) A.Lala Land and Apple Trees

Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere. Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai

Codeforces Round #312 (Div. 2) C.Amr and Chemistry

Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment. Amr has n different types of chemicals. Each chemical i has an initial volume of ai liters. For this experiment, Amr has to mix all the chemicals

Codeforces Round #312 (Div. 2) B.Amr and The Large Array

Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some

Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry

Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment. Amr has n different types of chemicals. Each chemical i has an initial volume of ai liters. For this experiment, Amr has to mix all the chemicals

Codeforces Round #312 (Div. 2) ABC题解

[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地.拉拉土地是一个非常美丽的国家,位于坐标线.拉拉土地是与著名的苹果树越来越随处可见. 拉拉土地恰好n苹果树.树数i位于位置xi和具有人工智能的苹果就可以了增长.阿姆鲁希望从苹果树收集苹果. AMR目前维持在X =0的位置.在开始的时候,他可以选择是否去左边或右边.他会在他的方向继续下去,直到他遇见一棵苹果树,他之前没有参观.他会采取所有的苹果,然后扭转他的方向,继续走这

Codeforces Round #312 (Div. 2)

A. Lala Land and Apple Trees 题目描述: 一条坐标轴,在坐标轴上散布了一些苹果树,每棵树都有位置和所结果实数目两个属性,Amr在坐标轴0点的位置,Amr在开始的时候可以选择向左或者右走,然后遇到果树才能改变方向,问最后最多能拿到多少个苹果? 解题思路: 开两个数组,分别代表坐标正半轴和负半轴,然后对每个数组排下一序,刚开始选择苹果树多的方向,然后累加就好. 1 #include <bits/stdc++.h> 2 using namespace std; 3 str