1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<bits/stdc++.h>
#define lid(x) (x<<1)
#define rid(x) (x<<1|1)
using namespace std;
const int N=1000005*4;
typedef long long ll;
ll n,m;
ll w[N];
struct seg_tree {
ll sum, l, r, lazy, max_val, min_val, gcd_val;
}
t[N];
ll read() {
//fast_read
ll ans = 0 , f = 1;
char i=getchar();
while(i<'0'||i>'9') {
if(i=='-')f=-1;
i=getchar();
}
while(i>='0'&&i<='9') {
ans=ans*10+i-'0';
i=getchar();
}
return ans * f;
}

void up(ll root) {
t[root].sum = t[lid(root)].sum + t[rid(root)].sum;
t[root].max_val = max(t[lid(root)].max_val, t[rid(root)].max_val);
t[root].min_val = min(t[lid(root)].min_val, t[rid(root)].min_val);
t[root].gcd_val = __gcd(t[lid(root)].gcd_val, t[rid(root)].gcd_val);
}

void build(ll root, ll l, ll r) {
int mid = l + r >> 1;
t[root].l = l, t[root].r = r;
t[root].max_val = -LLONG_MAX;
t[root].min_val = LLONG_MAX;
t[root].gcd_val = 0; // 初始化 gcd_val 为 0

if (l == r) {
t[root].sum = w[l];
t[root].max_val = w[l];
t[root].min_val = w[l];
t[root].gcd_val = w[l]; // 当节点为叶子节点时,gcd_val 为叶子节点的值
return;
}

build(lid(root), l, mid);
build(rid(root), mid + 1, r);
up(root);
}

void pushdown(ll root) {
//下放lazy
ll mid = t[root].l + t[root].r >> 1;
t[lid(root)].lazy += t[root].lazy;
t[rid(root)].lazy += t[root].lazy;
t[lid(root)].sum += t[root].lazy*(mid-t[root].l+1);
t[rid(root)].sum += t[root].lazy*(t[root].r-mid);
t[root].lazy = 0;
}
void updata(ll root,ll l,ll r,ll val) {
//区域加
ll mid = t[root].l+t[root].r>>1;
if(l<=t[root].l && t[root].r<=r) {
t[root].sum += val * (t[root].r-t[root].l+1);
t[root].lazy += val;
return;
}
if(t[root].lazy) pushdown(root);
if(l <= mid) updata(lid(root),l,r,val);
if(mid < r) updata(rid(root),l,r,val);
up(root);
}

void point_updata(ll root,ll id,ll val){
//单点加[滑稽]
updata(root,id,id,val);
}

ll query_sum(ll root,ll l,ll r) {
//查找sum
if(l<=t[root].l && t[root].r<=r) return t[root].sum;
if(r<t[root].l || t[root].r<l) return 0;
if(t[root].lazy) pushdown(root);
return query_sum(lid(root),l,r)+query_sum(rid(root),l,r);
}

ll point_number(ll root,ll id){
//单点数据[滑稽]
query_sum(root,id,id);
}

ll query_max(ll root, ll l, ll r) {
//查找max
if (l <= t[root].l && t[root].r <= r) return t[root].max_val;
if (r < t[root].l || t[root].r < l) return -LLONG_MAX; // 返回负无穷大表示不在查询区间内
if (t[root].lazy) pushdown(root);
return max(query_max(lid(root), l, r), query_max(rid(root), l, r));
}
ll query_min(ll root, ll l, ll r) {
//查找min
if (l <= t[root].l && t[root].r <= r) return t[root].min_val;
if (r < t[root].l || t[root].r < l) return LLONG_MAX; // 返回正无穷大表示不在查询区间内
if (t[root].lazy) pushdown(root);
return min(query_min(lid(root), l, r), query_min(rid(root), l, r));
}

ll query_gcd(ll root, ll l, ll r) {
if (l <= t[root].l && t[root].r <= r) return t[root].gcd_val;
if (r < t[root].l || t[root].r < l) return 0; // 返回0表示不在查询区间内
if (t[root].lazy) pushdown(root);
ll left_gcd = query_gcd(lid(root), l, r);
ll right_gcd = query_gcd(rid(root), l, r);
return __gcd(left_gcd, right_gcd);
}

void bulid_seg_tree(){
n = read();
if(n==0)return;
for (ll i=1;i<=n;i++) w[i] = read();
build(1,1,n);
}

int main() {
bulid_seg_tree();

return 0;
}

Edited on

Give me a cup of [coffee]~( ̄▽ ̄)~*

Haoxuan Liu WeChat Pay

WeChat Pay

Haoxuan Liu Alipay

Alipay

Haoxuan Liu PayPal

PayPal