-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2-persistent-segment-tree.cpp
113 lines (83 loc) · 2.58 KB
/
2-persistent-segment-tree.cpp
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
#include <bits/stdc++.h>
using namespace std;
static const int SIZE = 1 << 18;
struct Node {
long long value;
long long carry;
int from;
int to;
Node* left;
Node* right;
};
vector<Node*> roots;
long long numbers[SIZE];
Node* build(Node *node, int from, int to) {
node->from = from;
node->to = to;
node->carry = 0;
if (from == to) {
node->value = numbers[to];
return node;
}
node->left = new Node;
node->right = new Node;
int mid = (from + to) >> 1;
node->left = build(node->left, from, mid);
node->right = build(node->right, mid + 1, to);
node->value = node->left->value + node->right->value;
return node;
}
Node* updateSpecific(Node *node, int from, int to, long long value) {
Node *uptNode = new Node;
uptNode->from = node->from;
uptNode->to = node->to;
if (node->from >= from and node->to <= to) {
uptNode->value = node->value + (long long)(node->to - node->from + 1) * value;
uptNode->carry = node->carry + value;
uptNode->left = node->left;
uptNode->right = node->right;
return uptNode;
}
uptNode->carry = node->carry;
if (node->left->to >= from) {
uptNode->left = updateSpecific(node->left, from, to, value);
} else {
uptNode->left = node->left;
}
if (node->right->from <= to) {
uptNode->right = updateSpecific(node->right, from, to, value);
} else {
uptNode->right = node->right;
}
long long intervalCarry = (long long)(node->to - node->from + 1) * uptNode->carry;
uptNode->value = uptNode->left->value + uptNode->right->value + intervalCarry;
return uptNode;
}
long long getSumSpecific(Node *node, int from, int to) {
if (node->from >= from and node->to <= to) {
return node->value;
}
long long returnValue = 0;
if (node->left->to >= from) {
returnValue += getSumSpecific(node->left, from, to);
}
if (node->right->from <= to) {
returnValue += getSumSpecific(node->right, from, to);
}
int intervalFrom = max(node->from, from);
int intervalTo = min(node->to, to);
return returnValue + (long long)(intervalTo - intervalFrom + 1) * node->carry;
}
long long getSum(int from, int to, int time) {
return getSumSpecific(roots[time], from, to);
}
void update(int from, int to, long long value, int time) {
Node* newRoot = new Node;
newRoot = updateSpecific(roots[time], from, to, value);
roots.push_back(newRoot);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}