您的位置:首页 > 大数据 > 人工智能

2015 Multi-University Training Contest 3 1008

2015-07-29 20:26 411 查看

Solve this interesting problem

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1728 Accepted Submission(s): 517


[align=left]Problem Description[/align]
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⌋+1,Ry=Ru.
Here is an example of segment tree to do range query of sum.

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
using namespace std;
const int INF=0x3f3f3f3f;
LL L,R,ans;

void DFS(LL l,LL r)
{
LL len=r-l+1;  //区间长度
if(r>=ans) return ;
if(l==0)
{
ans=r;
return ;
}             //结束递归
if(len>l) return ;

DFS(2*l-r-1,r);
DFS(2*l-r-2,r);

DFS(l,2*r-l);
DFS(l,2*r-l+1);
}

int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%I64d %I64d",&L,&R)!=EOF)
{
ans=INF;
DFS(L,R);
if(ans==INF) cout<<"-1"<<endl;
else cout<<ans<<endl;
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: