您的位置:首页 > 其它

CodeForces 558D

2016-04-26 16:11 246 查看
Guess Your Way Out! II
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 558D

Description

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<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int n,m;
long long xsta,xend;
struct Node
{
long long  l,r;
Node(){}
Node(long long p,long long q):l(p),r(q){}
} a;
bool cmp(Node const &a,Node const &b)
{
if(a.l==b.l) return a.r<b.r;
return a.l<b.l;
}
vector <Node> g;
void cc()
{
long long l,r;
int tp,ok;
for(int i=0; i<m; i++)
{
scanf("%d %I64d %I64d %d",&tp,&l,&r,&ok);
l=(long long) (l<<(n-tp));
for(int i=0; i<n-tp; i++) r=r<<1|1;
if(ok)
{
xsta=max(xsta,l);
xend=min(xend,r);
}
else
{
g.push_back(Node(l,r));
}
}
g.push_back(Node(xend+1,xend+1));
}
void solve()
{
sort(g.begin(),g.end(),cmp);
long long  ans=-1;
for(int i=0; i<g.size(); i++)
{
if(xsta>xend) break;
if(xsta<g[i].l)
{
if(ans!=-1||xsta+1<g[i].l)
{
printf("Data not sufficient!\n");
return ;
}
ans=xsta;
}
xsta=max(xsta,g[i].r+1);
}
if(ans!=-1)
printf("%I64d\n",ans);
else
printf("Game cheated!\n");
}
void ini()
{
g.clear();
xsta=(long long )1<<(n-1);
xend=((long long )1<<n)-1;
}
int main()
{

while(~ scanf("%d%d",&n,&m))
{
ini();
cc();
solve();
}
return 0;
}


View Code
看了题解想明白的,虽然自己写了一遍,但是一直在wa,所以最后将代码改到和题解一样的了。
http://blog.csdn.net/u012596172/article/details/46897597
终于是发现哪里出错了

xsta=(long long )1<<(n-1);

xend=(long long) (1<<n)-1;

这样写是把1先移位后再转成long long 型的,肯定有精度损失。

所以先((long long)1),把1转成long long 型的在进行移位操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: