码迷,mamicode.com
首页 > 其他好文 > 详细

[kuangbin]带你飞之'线段树'专题(未完成)

时间:2019-10-05 10:31:20      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:ret   change   article   text   tunnel   getch   include   imp   onclick   

// 带飞网址 https://vjudge.net/article/187

专题七 线段树 
HDU 1166 敌兵布阵
HDU 1754 I Hate It
√ POJ 3468 A Simple Problem with Integers
POJ 2528 Mayor‘s posters
HDU 1698 Just a Hook
ZOJ 1610 Count the Colors
POJ 3264 Balanced Lineup
HDU 4027 Can you answer these queries?
HDU 1540 Tunnel Warfare
HDU 3974 Assign the task
HDU 4578 Transformation
HDU 4614 Vases and Flowers
HDU 4553 约会安排
POJ 1177 Picture
HDU 1255 覆盖的面积
HDU 1542 Atlantis
HDU 3642 Get The Treasury

 

// poj 3468 简单板子

技术图片
 1 #include<cstdio>
 2 
 3 const int MAXN = 100000;
 4 typedef long long ll;
 5 
 6 ll ans;
 7 struct Segmemt_tree {
 8     int l, r;
 9     ll f, w;
10 } tree[MAXN*4+1];
11 
12 inline void build(int k, int ll, int rr) {
13     tree[k].l = ll; tree[k].r = rr;
14     if(tree[k].l == tree[k].r) {
15         scanf("%lld", &tree[k].w);
16         return ;
17     }
18     int m = (ll + rr) / 2;
19     build(k*2, ll, m);
20     build(k*2+1, m+1, rr);
21     tree[k].w = tree[k*2].w + tree[k*2+1].w;
22 }
23 
24 inline void down(int k) {
25     tree[k*2].f += tree[k].f;
26     tree[k*2+1].f += tree[k].f;
27     tree[k*2].w += tree[k].f * (tree[k*2].r - tree[k*2].l + 1);
28     tree[k*2+1].w += tree[k].f * (tree[k*2+1].r - tree[k*2+1].l + 1);
29     tree[k].f = 0;
30 }
31 
32 inline void query_interval(int k, int x, int y) {
33     if(tree[k].l >= x && tree[k].r <= y) {
34         ans += tree[k].w;
35         return ;
36     }
37     if(tree[k].f) down(k);
38     int m = (tree[k].l + tree[k].r) / 2;
39     if(x <= m) query_interval(k*2, x, y);
40     if(y > m) query_interval(k*2+1, x, y);
41 }
42 
43 inline void change_interval(int k, int x, int y, ll v) {
44     if(tree[k].l >= x && tree[k].r <= y) {
45         tree[k].w += (tree[k].r - tree[k].l + 1) * v;
46         tree[k].f += v;
47         return ;
48     }
49     if(tree[k].f) down(k);
50     int m = (tree[k].l + tree[k].r) / 2;
51     if(x <= m) change_interval(k*2, x, y, v);
52     if(y > m) change_interval(k*2+1, x, y, v);
53     tree[k].w = tree[k*2].w + tree[k*2+1].w;
54 }
55 
56 int main() {
57     int n, m;
58     scanf("%d%d", &n, &m);
59     build(1, 1, n);
60     int x, y;
61     ll v;
62     char ch;
63     for(int i = 0; i != m; ++i) {
64         getchar();
65         scanf("%c", &ch);
66         ans = 0;
67         if(ch == Q) {
68             scanf("%d%d", &x, &y);
69             query_interval(1, x, y);
70             printf("%lld\n", ans);
71         }
72         else {
73             scanf("%d%d%lld", &x, &y, &v);
74             change_interval(1, x, y, v);
75         }
76     }
77     return 0;
78 }
View Code

 

[kuangbin]带你飞之'线段树'专题(未完成)

标签:ret   change   article   text   tunnel   getch   include   imp   onclick   

原文地址:https://www.cnblogs.com/pupil-xj/p/11623782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!