区间合并 --- 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)覆盖的叶结点是否包含出口。

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 ;
}
/* */
区间合并 --- Codeforces 558D : Gess Your Way Out ! II的更多相关文章
- codeforces 558D Guess Your Way Out! II 规律
题目链接 题意: 给出n和q 表示有一棵深度为n的全然二叉树.叶子节点中有恰好一个点是出口 主角从根往下走.但不知道出口在哪里,但主角会获得q个提示. 像这样标号 q个提示 格式: deep [l, ...
- Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)
链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- CodeForces - 357C Knight Tournament 伪并查集(区间合并)
Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...
- CodeForces - 566D Restructuring Company 并查集的区间合并
Restructuring Company Even the most successful company can go through a crisis period when you have ...
- CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)
题意:给你一个数组,有两种操作,一种区间xor一个值,一个是查询区间xor的结果的种类数 做法一:对于一个给定的区间,我们可以通过求解线性基的方式求出结果的种类数,而现在只不过将其放在线树上维护区间线 ...
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Lim ...
随机推荐
- sqlserver row_number 类似 mysql中 limit 用法
select * from ( select row_number() over(ORDER BY inspecdate desc,inspectime DESC,itemorder asc ) as ...
- Git忽略配置文件gitignore
在git客户端用户工程根目录下,.git同级目录创建文件.gitignore,文件名为.gitignore.目的是为了忽略一些不需要提交的文件到git服务器 配置如下: bin target .set ...
- redmine v3.02版的安装问题
redmine v3.0.2版的安装问题 参考上次在朋友公司的云主机上安装的过程: 1. 下载 2. gem install bundler 3. bundle install 出现 rmagick ...
- Centos7 改名问题
先看这篇 深入理解Linux修改hostname http://www.cnblogs.com/kerrycode/p/3595724.html 我使用如下方法 http://www.linuxid ...
- WPF的定时器
一.注意事项 引用命名空间:System.Windows.Threading.DispatcherTimer. 二.使用方法 var _timer = new DispatcherTimer(); _ ...
- Becoming a Data Scientist – Curriculum via Metromap
From: http://nirvacana.com/thoughts/becoming-a-data-scientist/ Data Science, Machine Learning, Big D ...
- 【网络编程】——windows socket 编程
测试demo #include <winsock2.h> #include <stdio.h> #include <string.h> #include <s ...
- struts2:非表单标签
非表单标签主要用于输出在Action中封装的信息,这在实际运用中是很常见的. 1. actionerror标签 <s:actionerror>标签主要用于输出错误信息到客户端,该标签将Ac ...
- 使用WebApi时Post和Put的区别
简单的说Post是添加,Put是修改 吃不准的话,尝试用相同参数访问二次接口,结果不同的是Post(会产生多条记录),结果相同的是Put(仅为一条记录),例如:写博客就是Post:更新签名就是Put
- web app变革之rem(手机屏幕实现全适配)
以往web移动适配,常规写法是:media only screen @media only screen and (min-device-width: 320px){ //针对iPhone 3 } @ ...