您的位置:首页 > 产品设计 > UI/UE

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,

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: