区间合并 --- Codeforces 558D : Gess Your Way Out ! II

D. Guess Your Way Out! II

Problem's Link: http://codeforces.com/problemset/problem/558/D


Mean:

一棵满二叉树,树中某个叶子节点是出口,目的是寻找这个出口。再给定Q个询问的结果,每个结果告诉我们在第i层中(l,r)覆盖的叶结点是否包含出口。

区间合并 --- Codeforces 558D : Gess Your Way Out ! II

analyse:

基本思路:多个区间求交集。

具体实现:

对于每一个询问,把它转化到最底层。并且把不在(l,r)区间的询问改为在(最左边,l-1)和(r+1,最右边)的形式,这样一来全部都变成了在(l,r)区间的描述。

区间统计:

对左右区间起点和终点组成的集合进行排序。然后找到答案存在的区间,如果区间长度=1,答案唯一;长度>1,答案不唯一;长度=0,无解。

Trick:会爆int。

Time complexity: O(n)

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-16-11.55
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
LL L[], R[];
int main()
{
ios_base::sync_with_stdio( false );
cin.tie( );
L[] = , R[] = ;
for( int i = ; i <= ; ++i ) L[i] = L[i - ] << , R[i] = ( L[i] << ) - ;
int h, q;
cin >> h >> q;
if( q == )
{
if( h == ) puts( "" );
else puts( "Data not sufficient!" );
return ;
}
map<LL, int> mp;
for( int i = ; i < q; ++i )
{
LL level, left, right, type, gap;
cin >> level >> left >> right >> type;
gap = h - level;
while( gap )
{
gap--;
left <<= ;
right = ( ( right + ) << ) - ;
}
if( type )
{
mp[left]++;
mp[right + ]--;
}
else
{
mp[L[h]]++;
mp[left]--;
mp[right + ]++;
mp[R[h] + ]--;
}
}
LL ans, sum = , ans_gap = , mid_pre = -;
map<LL, int>:: iterator it = mp.begin();
for( ; it != mp.end(); ++it )
{
sum += ( it->second );
if( mid_pre != - )
{
ans_gap += ( it->first ) - mid_pre;
ans = mid_pre;
}
if( sum == q ) mid_pre = ( it->first );
else mid_pre = -;
}
if( ans_gap == ) cout << ans << endl;
else if( ans_gap > ) puts( "Data not sufficient!\n" );
else puts( "Game cheated!\n" );
return ;
}
/* */
上一篇:鸟哥的linux私房菜——第十章学习(BASH)


下一篇:《鸟哥的linux私房菜》 - linux命令温故而知新