EOJ Monthly 2019.3 (based on March Selection) C. 線段樹 --剪枝

https://acm.ecnu.edu.cn/contest/151/problem/C/

在這裏插入圖片描述
在這裏插入圖片描述
解析
https://acm.ecnu.edu.cn/blog/entry/342/
在這裏插入圖片描述

遞歸 (超時)

#include <iostream>
#include <string>
#include <typeinfo>
#include <stack>
#include <vector>
#include<sstream>
#include <string.h>
#include <map>
using namespace std;
long long make(long long l,long long r,long long& m1){
    //cout<<"rrr "<<r<<endl;
    long long d = r - l + 1;
    if(l==r){
        return l;
    }else if(l<1){
        return -1;
    }else if(r>2000000000){
        return -1;
    }else if(l==1){
        cout<<"m1 "<<m1<<" r "<<r<<endl;
        if(r<m1){
            m1 = r;
        }else{
            cout<<"m1 "<<m1<<endl;
            return m1;
        }
    }else if(l>1 && r<=2000000000){

        long long k1 = make(l-d,r,m1);
        //cout<<"k1 "<<k1<<endl;
        if(k1>0){
            cout<<"k1"<<endl;
            return k1;
        }

        long long k2 = make(l-d-1,r,m1);
        //cout<<"k2 "<<k2<<endl;
        if(k2>0){
            cout<<"k2"<<endl;
            return k2;
        }
        long long k3 = make(l,r+d,m1);
        //cout<<"k3 "<<k3<<endl;
        if(k3>0){
            cout<<"k3"<<endl;
            return k3;
        }
        long long k4 = make(l,r+d-1,m1);
        //cout<<"k4 "<<k4<<endl;
        if(k4>0){
            cout<<"k4"<<endl;
            return k4;
        }else{
            return -1;
        }

    }
}


int main()
{
    int T;
    cin>>T;
    long long l,r;
    long long rm = 2100000000;
    while(T--){
        cin>>l;
        cin>>r;
        rm = 2100000000;
        cout<<make(l,r,rm)<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章