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 Format

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 Format

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

Sample input and output

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

answer

This is the subject of a segment template tree. I refer to the tree line from the template: https://baijiahao.baidu.com/s?id=1605870136961096251&wfr=spider&for=pc . The original template is a summation range segment, is now the interval for the minimum.

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <string.h>
 6 #define ll long long
 7 
 8 const int MAXN = 1000001;
 9 
10 using namespace std;
11 
12 ll n, m, a[MAXN], ans[MAXN<<2], tag[MAXN<<2];
13 
14 ll ls(ll x)
15 {
16     return x<<1;
17 }
18 
19 ll rs(ll x)
20 {
21     return x<<1|1;
22 }
23 
24 void push_up(ll p)
25 {
26     ans[p] = min(ans[ls(p)], ans[rs(p)]);
27 }
28 
29 void build(ll p, ll l, ll r)
30 {
31     tag[p] = 0;
32     if(l == r)
33  {
34   ans[p] = a[l];
35   return;
36  }
37     ll mid = (l + r) >> 1;
38     build(ls(p), l, mid);
39     build(rs(p), mid + 1, r);
40     push_up(p);
41 } 
42 
43 void f(ll p, ll l, ll r, ll k)
44 {
45     tag[p] = tag[p] + k;
46     ans[p] = ans[p] + k * (r - l + 1);
47 }
48 
49 void push_down(ll p, ll l, ll r)
50 {
51     ll mid = (l + r)>>1;
52     f(ls(p), l, mid, tag[p]);
53     f(rs(p), mid + 1, r, tag[p]);
54     tag[p] = 0;
55 }
56 
57 ll query(ll q_x, ll q_y, ll l, ll r, ll p)
58 {
59     ll res = 922337203685477580;
60     if(q_x <= l && r <= q_y)
61  {
62   return ans[p];
63  }
64     ll mid = (l + r)>>1;
65     push_down(p, l, r);
66     if(q_x <= mid)
67  {
68   res = min(res, query(q_x, q_y, l, mid, ls(p)));
69  }
70     if(q_y > mid) 
71  {
72   res = min(res, query(q_x, q_y, mid + 1, r, rs(p)));
73  }
74     return res;
75 }
76 
77 int main()
78 {
79      ll e, f;
80     scanf("%lld%lld", &n, &m);
81     for(ll i = 1; i <= n; i++)
82     {
83         scanf("%lld", &a[i]);
84     }
85     build(1, 1, n);
86     for(ll i = 1; i <= m; i++)
87     {
88         scanf("%lld%lld", &e, &f);
89         printf("%lld ", query(e, f, 1, n, 1));
90     }
91     return 0;
92 }

 

Guess you like

Origin www.cnblogs.com/zealsoft/p/11559835.html