-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeForces-1473D.cpp
More file actions
76 lines (67 loc) · 1.8 KB
/
CodeForces-1473D.cpp
File metadata and controls
76 lines (67 loc) · 1.8 KB
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
//
// Created by abdob on 10/29/2022.
//
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define maxn 200010
const int MOD = 1000000007;
struct pot {
int mx;
int mn;
int vl;
pot() : mx(0), mn(0), vl(0) {}
};
vector<pot> tree;
pot doit(pot a, pot b) {
pot c;
c.vl = a.vl + b.vl;
c.mn = min({a.mn, b.mn + a.vl});
c.mx = max({a.mx, b.mx + a.vl});
return c;
}
pot queryTree(int start, int end, int l, int r, int index = 1) {
if (r < start or l > end)return pot();
if (l <= start and r >= end)return tree[index];
int mid = start + end >> 1;
return doit(queryTree(start, mid, l, r, 2 * index), queryTree(mid + 1, end, l, r, 2 * index + 1));
}
void update(int start, int end, int x, int val, int index) {
if (x < start or x > end)return;
if (start == end) {
tree[index].vl = val;
if (val == -1)tree[index].mn = val;
else tree[index].mx = val;
return;
}
int mid = start + end >> 1;
update(start, mid, x, val, 2 * index);
update(mid + 1, end, x, val, 2 * index + 1);
tree[index] = doit(tree[2 * index], tree[2 * index + 1]);
}
void solve() {
int n, m;
cin >> n >> m;
tree = vector<pot>(4 * n + 10);
string s;
cin >> s;
for (int i = 0; i < n; i++)
update(0, n - 1, i, (s[i] == '+' ? 1 : -1), 1);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
a--, b--;
pot ans = doit(queryTree(0, n - 1, 0, a - 1, 1), queryTree(0, n - 1, b + 1, n - 1, 1));
cout << ans.mx - ans.mn + 1 << endl;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}