惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
博客园 - 【当耐特】
V
Visual Studio Blog
GbyAI
GbyAI
V
V2EX
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
量子位
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件

博客园 - zealsoft

洛谷 P2384 最短路题解 洛谷 P2725 邮票题解 洛谷 P2722 总分题解 洛谷 P1219 八皇后题解 洛谷 P2921 在农场万圣节Trick or Treat on the Farm题解 洛谷 P1162 填涂颜色题解 洛谷 P1330 封锁阳光大学题解 洛谷 P1032 字串变换题解 洛谷 P1443 马的遍历题解 洛谷 P1280 尼克的任务题解 洛谷 P1020导弹拦截题解 洛谷 P1141 01迷宫题解 VisualSVNServer无法卸载也无法安装,报告不是有效的MOF文件(0x8004401e)错误 视频捕捉的格式问题 一个VxWorks源代码网站 找个轻量级的Log库还挺难 TAU G2中的BitString和OctetString W32.Downadup.autorun病毒的清除 如何用Visual Studio 2005编译Wireshark的插件
洛谷 P1816 忠诚题解
zealsoft · 2019-09-20 · via 博客园 - zealsoft
 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 }