[Segment] zhongkui

Source: Internet
Author: User
Description  
  Old manager is a smart and competent person. He has been working for the financial owner for 10 years, and the financial owner wants to make his account clearer. The manager is required to keep K accounts every day. Because the manager is smart and competent, the manager always makes the manager very satisfied. However, some people have doubts about the manager because of their provocation. So he decided to use a special method to judge the loyalty of the butler. He pressed every account to 1, 2, 3... Number, and then occasionally ask the manager's question, the question is: what is the smallest sum of Accounts A to B? In order to prevent the butler from committing frauds, he always asks multiple questions at a time.
     
     
  Input Format  
  In the input, the first row has two numbers of M, N indicates M (M <= 100000), N indicates n problems, and n <= 100000.
The number of M in the second action, which is the amount of money in the account.
There are n problems in the next n rows. Each row has two numbers indicating the number of accounts starting to end.
     
     
  Output Format  
  The output file contains the answer to each question. For details, see the example.
     
     
  Sample Input  
  10 3 <br/> 1 2 3 4 5 6 7 8 9 10 <br/> 2 7 <br/> 3 9 <br/> 1 10
     
     
  Sample output sample output  
  2 3 1
     
     
  Time Limit  
  1 s for each test point
 

An entry into the segment.
The min value of each vertex is the min value of the left sub-shard and the min value of the right sub-shard.

Accode:

# Include <cstdio> # include <cstring> # include <cstdlib> # include <bitset> Using STD: min; const char fi [] = "tyvj1038.in "; const char fo [] = "tyvj1038.out"; const int maxn = 100010; const int maxm = 100010; const int max = 0x3fffff00; const int min =-Max; struct segtree {int L, R, LC, RC, Min ;}; segtree [maxm <1]; int T [maxn]; int n, m, TOT; void init_file () {freopen (FI, "r", stdin); freopen (FO, "W", St Dout);} void build (int l, int R) {int now = ++ tot; tree [now]. L = L; tree [now]. R = r; If (L = r) {tree [now]. min = T [l]; return;} int mid = (L + r)> 1; if (L <r) {tree [now]. lc = tot + 1; build (L, mid); tree [now]. rc = tot + 1; build (Mid + 1, R);} tree [now]. min = min (tree [tree [now]. LC]. min, tree [tree [now]. RC]. min);} int query (INT now, int L, int R) {If (L <= tree [now]. L & R> = tree [now]. r) Return tree [now]. min; int mid = (tree [now]. L + tree [now]. r)> 1; if (r <= mid) return query (tree [now]. LC, L, R); // subquestions that are completely included by the Left subaccount are directly related to the left subaccount. If (mid <L) return query (tree [now]. RC, L, R); // A subproblem that is directly related to the Right sub-region if the right sub-region contains it. Int lc = query (tree [now]. LC, L, R), Rc = query (tree [now]. RC, L, R); Return min (LC, RC);} void work () {scanf ("% d", & N, & M ); for (INT I = 1; I <n + 1; ++ I) scanf ("% d", T + I); Tot = 0; build (1, N ); for (; m; -- m) {int L, R; scanf ("% d", & L, & R); printf ("% d \ n ", query (1, L, R) ;}} int main () {init_file (); Work (); exit (0 );}

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.