Luo Gu P1816 loyalty solution to a problem

Luo Gu P1816 loyalty solution to a problem

Title Description

The servant is a smart and capable people. He worked for 10 years as a full rich man, rich man in order to make clearer their own accounts. Requirements butler remember k times a day accounts, as smart and capable butler, housekeeper and thus make the rich always satisfied. However, due to some provocation, rich or housekeeper had doubts. So he decided to use a special method to determine the faithful steward his accounts according to each number 1, 2, ..., from time to time and then ask the housekeeper problem, the problem is such that: the account number of a to b What is the minimum sum? In order to let the housekeeper did not have time to ask more than once he was always a false problem.

Input and output formats

Input formats:

 

The first row has two input numbers m, n expressed m (m <= 100000) T account, a problem has n represents n, n <= 100000.

Second line number m, respectively, the number of money accounts

N n the back row are a problem, each line has two figures show the beginning of the end of the number of accounts.

 

Output formats:

 

The output file for the answer to each question. View specific examples.

 

Sample input and output

Input Sample # 1:  Copy
10 3
1 2 3 4 5 6 7 8 9 10
2 7
3 9
1 10
Output Sample # 1:  Copy
2 3 1

Resolution:

A look at the subject range of the minimum requirements, ha, it must be the minimum segment tree maintenance intervals.

Rely segment tree to seek the minimum interval, a little change on the segment tree board.

The interval range for the minimum sum instead, then ask, would be finished. .

 1 #include <bits/stdc++.h>
 2 #define Max 500009
 3 #include <deque>
 4 int a[Max],n,m;
 5 struct Tree
 6 {
 7     int l,r,min;
 8 }t[Max];
 9 void build(int x,int l,int r)
10 {
11     t[x].l=l;t[x].r=r;
12     if(l==r) {
13         t[x].min=a[l];
14         return;
15     }
16     int mid=(l+r)/2;
17     build(x*2,l,mid);build(x*2+1,mid+1,r);
18     t[x].min=std::min(t[x*2].min,t[x*2+1].min);
19 }
20 int query(int x,int l,int r)
21 {
22     int ans=0x7fffffff;
23     if(l <= t[x].l && t[x].r <= r) return t[x].min;
24     int mid=(t[x].l+t[x].r) / 2;
25     if(l <= mid) ans = std::min(ans,query(x*2,l,r));
26     if(r > mid) ans = std::min(ans,query(x*2+1,l,r));
27     return ans;
28 }
29 int main()
30 {
31     scanf("%d%d",&n,&m);int l,r;
32     for(int i = 1 ; i <= n ; ++ i) scanf("%d",&a[i]);
33     build(1,1,n);
34     for(int i = 1 ; i <= m ; ++ i) {
35         scanf("%d%d",&l,&r);
36         printf("%d ",query(1,l,r));
37     }
38     return 0;
39 }
AC Code

 

Guess you like

Origin www.cnblogs.com/handsomegodzilla/p/11240242.html