Submission #75945


ソースコード

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
#include <bits/stdc++.h>
using namespace std;
class Length {
int __length; // ox含めた文字列の長さ
bool __all_circle; // 全ての文字がoかどうかの管理
public:
int l; // 左端から連続してoが何個あるか
int r; // 右端から連続してoが何個あるか
int a; // どこでもいいから、連続して最大oが続いているか
Length () {}
// c .. 'o' か 'x'
Length(char c) {
__length = 1;
__all_circle = (c == 'o');
if (c == 'o') {
l = r = a = 1;
} else if (c == 'x') {
l = r = a = 0;
}
}
Length operator + (Length right) {
Length len;
// 加算の交換法則は成り立たないので注意
len.l = (this->__all_circle ? this->__length + right.l : this->l);
len.r = (right.__all_circle ? right.__length + this->r : right.r);
len.a = max({len.l, len.r, this->r+right.l, this->a, right.a});
len.__length = this->__length + right.__length;
len.__all_circle = this->__all_circle & right.__all_circle;
return len;
}
};
struct SegTree {
const Length MONOID = Length('x');
int n; // 葉の数
vector<Length> dat; // 完全二分木の配列
SegTree(int n_) : n(), dat(n_ * 4, MONOID) { // 葉の数は 2^x の形
int x = 1;
while (n_ > x) {
x *= 2;
}
n = x;
}
void update(int i, Length x) {
i += n - 1;
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2; // parent
dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];
}
}
Length query(int a, int b) { return query_sub(a, b+1, 0, 0, n); } // [a,b]の最小値を取得する
Length query_sub(int a, int b, int k, int l, int r) {
// k:現在見ているノードの位置 [l,r):dat[k]が表している区間
if (r <= a || b <= l) { // 範囲外なら考えない
return MONOID;
} else if (a <= l && r <= b) { // 範囲内なので自身の値を返す
return dat[k];
} else { // 一部区間が被る時
Length vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
Length vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
return vl + vr;
}
}
};
signed main() {
int n, q;
string s;
int query, k, l, r;
Length query_ans;
cin >> n >> q;
cin >> s;
SegTree seg(n);
for (int i=0; i<n; i++) {
seg.update(i, Length(s[i]));
}
for (int i=0; i<q; i++) {
cin >> query;
if (query == 1) {
cin >> k;
k--;
if (s[k] == 'o') {
s[k] = 'x';
} else {
s[k] = 'o';
}
seg.update(k, Length(s[k]));
} else {
cin >> l >> r;
l--;
r--;
query_ans = seg.query(l, r);
cout << query_ans.a << endl;
}
}
return 0;
}

ステータス

項目 データ
問題 1663 - All you need is Segment Tree.
ユーザー名 syoribu
投稿日時 2023-09-12 08:46:47
言語 C++17
状態 Accepted
得点 5
ソースコード長 2889 Byte
最大実行時間 655 ms
最大メモリ使用量 21788 KB

セット

セット 得点 Cases
1 ALL 5 / 5 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
00_sample_00.in AC 28 ms 600 KB
1
01_small_00.in AC 25 ms 700 KB
1
01_small_01.in AC 25 ms 544 KB
1
01_small_02.in AC 20 ms 516 KB
1
01_small_03.in AC 29 ms 612 KB
1
02_corner_minimum_00.in AC 18 ms 456 KB
1
02_corner_minimum_01.in AC 22 ms 560 KB
1
03_general_00.in AC 21 ms 528 KB
1
03_general_01.in AC 16 ms 628 KB
1
04_random_00.in AC 22 ms 476 KB
1
04_random_01.in AC 21 ms 440 KB
1
04_random_02.in AC 16 ms 664 KB
1
04_random_03.in AC 21 ms 628 KB
1
05_large_00.in AC 22 ms 580 KB
1
05_large_01.in AC 20 ms 636 KB
1
05_large_02.in AC 22 ms 900 KB
1
05_large_03.in AC 27 ms 1008 KB
1
06_corner_maximum_00.in AC 414 ms 16720 KB
1
06_corner_maximum_01.in AC 655 ms 17400 KB
1
06_corner_maximum_02.in AC 620 ms 18584 KB
1
06_corner_maximum_03.in AC 625 ms 19008 KB
1
06_corner_maximum_04.in AC 258 ms 3284 KB
1
07_corner_critical_00.in AC 539 ms 19912 KB
1
07_corner_critical_01.in AC 373 ms 20076 KB
1
07_corner_critical_02.in AC 355 ms 20368 KB
1
07_corner_critical_03.in AC 407 ms 20660 KB
1
07_corner_critical_04.in AC 410 ms 20956 KB
1
07_corner_critical_05.in AC 558 ms 21368 KB
1
07_corner_critical_06.in AC 621 ms 21788 KB
1