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

简介: D. Guess Your Way Out! II Problem's Link: http://codeforces.com/problemset/problem/558/D   Mean:  一棵满二叉树,树中某个叶子节点是出口,目的是寻找这个出口。

 D. Guess Your Way Out! II

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


 

Mean: 

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

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[51], R[51];
int main()
{
      ios_base::sync_with_stdio( false );
      cin.tie( 0 );
      L[1] = 1, R[1] = 1;
      for( int i = 2; i <= 50; ++i ) L[i] = L[i - 1] << 1, R[i] = ( L[i] << 1 ) - 1;
      int h, q;
      cin >> h >> q;
      if( q == 0 )
      {
            if( h == 1 ) puts( "1" );
            else puts( "Data not sufficient!" );
            return 0;
      }
      map<LL, int> mp;
      for( int i = 0; i < q; ++i )
      {
            LL level, left, right, type, gap;
            cin >> level >> left >> right >> type;
            gap = h - level;
            while( gap )
            {
                  gap--;
                  left <<= 1;
                  right = ( ( right + 1 ) << 1 ) - 1;
            }
            if( type )
            {
                  mp[left]++;
                  mp[right + 1]--;
            }
            else
            {
                  mp[L[h]]++;
                  mp[left]--;
                  mp[right + 1]++;
                  mp[R[h] + 1]--;
            }
      }
      LL ans, sum = 0, ans_gap = 0, mid_pre = -1;
      map<LL, int>:: iterator it = mp.begin();
      for( ; it != mp.end(); ++it )
      {
            sum += ( it->second );
            if( mid_pre != -1 )
            {
                  ans_gap += ( it->first ) - mid_pre;
                  ans = mid_pre;
            }
            if( sum == q ) mid_pre = ( it->first );
            else mid_pre = -1;
      }
      if( ans_gap == 1 ) cout << ans << endl;
      else if( ans_gap > 1 ) puts( "Data not sufficient!\n" );
      else puts( "Game cheated!\n" );
      return 0;
}
/*

*/

 

目录
相关文章
|
7月前
codeforces 272C. Dima and Staircase(线段树)
题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。
17 0
|
23天前
|
算法 测试技术
每日一题:LeetCode-LCR 007. 三数之和
每日一题:LeetCode-LCR 007. 三数之和
|
6月前
|
C语言
Leetcode---爬楼梯
Leetcode---爬楼梯
20 0
|
数据安全/隐私保护
Codeforces 417D.Cunning Gena (状压DP)
Codeforces 417D.Cunning Gena (状压DP)
64 0
|
人工智能 vr&ar Perl
codeforces1509 C. The Sports Festival (区间DP)
codeforces1509 C. The Sports Festival (区间DP)
86 0
HDOJ/HDU 2561 第二小整数(水题~排序~)
HDOJ/HDU 2561 第二小整数(水题~排序~)
91 0