【搜索】 HDU 5323 Solve this interesting problem

点击打开链接

用线段树方式建树 [ 0, n]

已知[ l, r] 结点 求n

若 建一个[0, 2*r] 的线段树  这是的总点数的奇的,(左子树!=右子树

[0, r]  在左子树里 则n最大为2*r

若 建一个[0, 2*r+1] 的线段树 (左子树==右子树

[0, r]  在左子树里 这时则 [0, r] 就可以建树

所以搜的时候超出2*r 就直接return

 

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define FOR(i,a,b)  for(int i=a;i<=b;i++)
#define IN   freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 101000;
const int MAXM = 200000;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const double eps= 1e-8;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
LL ans,flag,L,R;
void dfs(LL l,LL r)
{
    if(r>2*R||flag&&r>=ans) return ;
    if(l==0)
    {
        flag=1;
        ans=r;
        return ;
    }
    LL len=r-l+1;
    if(l-len-1>=0)
        dfs(l-len-1,r);
    if(l-len>=0)
        dfs(l-len,r);
    dfs(l,r+len-1);
    dfs(l,r+len);
}
int main()
{
    while(scanf("%I64d%I64d",&L,&R)!=EOF)
    {
        ans=INF;flag=0;
        dfs(L,R);
        if(ans==INF)
            puts("-1");
        else
            printf("%I64d\n",ans);
    }
    return 0;
}
/*

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值