-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
94 lines (84 loc) · 2.58 KB
/
Solution.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
class Node {
public:
int l, r, size, lmx, rmx, mx;
char lc, rc;
};
class SegmentTree {
private:
string s;
vector<Node*> tr;
public:
SegmentTree(string& s) {
this->s = s;
int n = s.size();
tr.resize(n << 2);
for (int i = 0; i < tr.size(); ++i) tr[i] = new Node();
build(1, 1, n);
}
void build(int u, int l, int r) {
tr[u]->l = l;
tr[u]->r = r;
if (l == r) {
tr[u]->lmx = tr[u]->rmx = tr[u]->mx = tr[u]->size = 1;
tr[u]->lc = tr[u]->rc = s[l - 1];
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void modify(int u, int x, char v) {
if (tr[u]->l == x && tr[u]->r == x) {
tr[u]->lc = tr[u]->rc = v;
return;
}
int mid = (tr[u]->l + tr[u]->r) >> 1;
if (x <= mid)
modify(u << 1, x, v);
else
modify(u << 1 | 1, x, v);
pushup(u);
}
Node* query(int u, int l, int r) {
if (tr[u]->l >= l && tr[u]->r <= r) return tr[u];
int mid = (tr[u]->l + tr[u]->r) >> 1;
if (r <= mid) return query(u << 1, l, r);
if (l > mid) query(u << 1 | 1, l, r);
Node* ans = new Node();
Node* left = query(u << 1, l, r);
Node* right = query(u << 1 | 1, l, r);
pushup(ans, left, right);
return ans;
}
void pushup(Node* root, Node* left, Node* right) {
root->lc = left->lc;
root->rc = right->rc;
root->size = left->size + right->size;
root->mx = max(left->mx, right->mx);
root->lmx = left->lmx;
root->rmx = right->rmx;
if (left->rc == right->lc) {
if (left->lmx == left->size) root->lmx += right->lmx;
if (right->rmx == right->size) root->rmx += left->rmx;
root->mx = max(root->mx, left->rmx + right->lmx);
}
}
void pushup(int u) {
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
};
class Solution {
public:
vector<int> longestRepeating(string s, string queryCharacters, vector<int>& queryIndices) {
SegmentTree* tree = new SegmentTree(s);
int k = queryCharacters.size();
vector<int> ans(k);
for (int i = 0; i < k; ++i) {
int x = queryIndices[i] + 1;
tree->modify(1, x, queryCharacters[i]);
ans[i] = tree->query(1, 1, s.size())->mx;
}
return ans;
}
};