2015 multi-university training contest 3 1008

1008 Solve this interesting problem

Problem Description
Have you learned something about segment tree? If not, don’t worry, I will explain it for you.
Segment Tree is a kind of binary tree, it can be defined as this:

  • For each node u in Segment Tree, u has two values: Lu and Ru.
  • If Lu=Ru, u is a leaf node.
  • If Lu≠Ru, u has two children x and y,with Lx=Lu,Rx=(Lu+Ru2),Ly=(Lu+Ru2)/2+1,Ry=Ru.
    Here is an example of segment tree to do range query of sum.
    线段树

Given two integers L and R, Your task is to find the minimum non-negative n satisfy that: A Segment Tree with root node’s value Lroot=0 and Rroot=n contains a node u with Lu=L and Ru=R.

Input
The input consists of several test cases.
Each test case contains two integers L and R, as described above.
0≤L≤R≤109
LR−L+1≤2015

Output
For each test, output one line contains one integer. If there is no such n, just output -1.

Sample Input
6 7
10 13
10 11

Sample Output
7
-1
12

题解

比赛的时候没看到,无非就是四种状态的DFS啊……大失误。每个区间的父节点有四种,只要排出来就好;
吾日三省吾身,

  1. l<0否?
  2. l=0否?
  3. l>0的话r超了没有?
  4. 左边是不是爆了?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    #include <iostream>
    using namespace std;
    const long long maxn = 1e18 + 5;
    long long mins;
    void (long long l,long long r)
    {
    if(l < 0) return;

    if(l == 0) {mins=min(mins,r);return;}

    if(r - l + 1 > l||r >= mins) return;

    dfs(2 * l - r - 2, r);
    dfs(2 * l - r - 1, r);
    dfs(l, 2 * r - l);
    dfs(l, 2 * r - l + 1);
    }
    int main()
    {
    long long l, r;
    while(cin>>l>>r)
    {
    mins = maxn;
    dfs(l, r);
    if(mins == maxn) cout<<"-1"<<endl;

    else cout<<mins<<endl;

    }
    return 0;
    }