HDU 5323 (2015 Multi-school 3)-Solve this interesting problem (dfs + pruning), hdu5323

Source: Internet
Author: User

HDU 5323 (2015 Multi-school 3)-Solve this interesting problem (dfs + pruning), hdu5323

Address: HDU 5323
Question: Give an l, r, indicating the interval [l, r]. Ask if there is a node interval of a line segment tree with a range of [0, n]: [l, r]. if the minimum n value is obtained,-1 is not output.
Idea: Because L/(R-L + 1) <= 2015, according to the nature of the Line Segment tree each time the number of the interval is increased by two times, so it can be concluded that there are almost 22 layers, therefore, you can use the pop-up search function to expand the [l, r] range until the value of l = 0 is met. Cut and pruning by the way.

# Include <stdio. h> # include <math. h> # include <string. h> # include <stdlib. h> # include <iostream> # include <sstream> # include <algorithm> # include <set> # include <queue> # include <stack> # include <map> # pragma comment (linker, "/STACK: 102400000,102400000") using namespace std; typedef long LL; const int inf = 0x3f3f3f3f; const double pi = acos (-1.0); const double esp = 1e-8; int ans; int dfs (int l, int r) {if (l <0) retu Rn ans; // controls the left boundary if (! L) {// Save the currently smallest right boundary ans = min (ans, r); return ans;} if (2 * l <r + 1) return ans; // pruning. It is equivalent to four situations: directly output and return r. Dfs (l, 2 * r-l + 1); dfs (l, 2 X r-l); dfs (2 * l-r-1, r); dfs (2 * l-r-2, r);} int main () {int l, r; while (~ Scanf ("% d", & l, & r) {ans = inf; ans = dfs (l, r); if (ans = inf) puts ("-1"); else printf ("% d \ n", ans);} return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.