依赖头文件

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <assert.h>
#include <unordered_map>

using namespace std;
typedef long long ll;

#define Cmp(a, b) memcmp(a, b, sizeof(b))
#define Cpy(a, b) memcpy(a, b, sizeof(b))
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)
#define _rep(i, l, r) for(int i = (l); i <= (r); i++)
#define _for(i, l, r) for(int i = (l); i < (r); i++)
#define _forDown(i, l, r) for(int i = (l); i >= r; i--)
#define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i])
#define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second)
#define debugS(str) cout << "dbg: " << str << endl;
#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); }
#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)
#define lowbit(i) (i & (-i))
#define MPR(a, b) make_pair(a, b)

pair<int, int> crack(int n) {
int st = sqrt(n);
int fac = n / st;
while (n % st) {
st += 1;
fac = n / st;
}

return make_pair(st, fac);
}

inline ll qpow(ll a, int n) {
ll ans = 1;
for(; n; n >>= 1) {
if(n & 1) ans *= 1ll * a;
a *= a;
}
return ans;
}

template <class T>
inline bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}

ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}

ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
for(; b; b >>= 1) {
if (b & 1) ans = (ans + a) % mod;
a = (a * 2) % mod;
}
return ans;
}

ll ksm(ll a, ll b, ll mod) {
ll ans = 1 % mod;
a %= mod;

for(; b; b >>= 1) {
if (b & 1) ans = ksc(ans, a, mod);
a = ksc(a, a, mod);
}

return ans;
}

template <class T>
inline bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}

template<class T>
bool lexSmaller(vector<T> a, vector<T> b) {
int n = a.size(), m = b.size();
int i;
for(i = 0; i < n && i < m; i++) {
if (a[i] < b[i]) return true;
else if (b[i] < a[i]) return false;
}
return (i == n && i < m);
}

// ============================================================== //

线段树延迟标记模版

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
class segTree {
public:
struct node {
int l, r;
int sum1, sum2;
int tag1, tag2;

void apply1(int v) {
tag1 += v;
sum1 += v * (r-l+1);
}
void apply2(int v) {
tag2 += v;
sum2 += v * (r-l+1);
}
};

inline void push(int p) {
if (t[p].l != t[p].r && t[p].tag1) {
t[p<<1].apply1(t[p].tag1);
t[p<<1 | 1].apply1(t[p].tag1);
t[p].tag1 = 0;
}
if (t[p].l != t[p].r && t[p].tag2) {
t[p<<1].apply2(t[p].tag2);
t[p<<1 | 1].apply2(t[p].tag2);
t[p].tag2 = 0;
}
}
inline void pull(int p) {
int lc = p<<1, rc = p<<1 | 1;
t[p].sum1 = t[lc].sum1 + t[rc].sum1;
t[p].sum2 = t[lc].sum2 + t[rc].sum2;
}

void change(int p, const int l, const int r, int add1, int add2) {
if (l <= t[p].l && t[p].r <= r) {
t[p].apply1(add1), t[p].apply2(add2);
return;
}

push(p);
int mid = (t[p].l + t[p].r) >> 1;
if (l <= mid) change(p<<1, l, r, add1, add2);
if (r > mid) change(p<<1 | 1, l, r, add1, add2);

pull(p);
}

void build(int p, int l, int r) {
t[p].l = l, t[p].r = r;
if (l >= r) return;
int mid = (l+r) >> 1;
if (l <= mid) build(p<<1, l, mid);
if (r > mid) build(p<<1 | 1, mid+1, r);
}

void query(int p, const int C) {
if (t[p].l == t[p].r) {
printf("%d %d\n", t[p].sum1, t[p].sum2);
return;
}
push(p);

int mid = (t[p].l + t[p].r) >> 1;
if (C <= mid) query(p<<1, C);
else query(p<<1 | 1, C);
}

int n;
vector<node> t;

segTree() = default;
segTree(int _n) : n(_n) {
assert(n > 0);
t.resize(n << 2);
}
void clear() {
fill(t.begin(), t.end(), node());
}
};

如果有多组数据
const int maxn = 100000 + 10;
segTree seg(maxn);

void init() {
seg.clear();
}

int main() {
while (T--) {
init();
}
}