This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define T int t; cin >> t; while(t--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 1e5 + 5;
const int M = 1e3 + 3;
const int inf = 1e18;
const int mod = 1e9 + 7;
int n, q, a[N];
pair<int, int> p[N], tree[30][4 * N];
string s;
pair<int, int> getMax(pair<int, int> p1, pair<int, int> p2) {
vector<int> v;
v.push_back(p1.F);
v.push_back(p1.S);
v.push_back(p2.F);
v.push_back(p2.S);
sort(begin(v), end(v));
return {v[3], v[2]};
}
void build(int letter, int node, int tl, int tr) {
if (tr == tl) {
if (s[tr] == (letter + 'a')) tree[letter][node] = {a[tl], 0};
return;
}
int mid = (tl + tr) / 2;
build(letter, 2 * node, tl, mid);
build(letter, 2 * node + 1, mid + 1, tr);
tree[letter][node] = getMax(tree[letter][2 * node], tree[letter][2 * node + 1]);
}
pair<int, int> get(int letter, int node, int tl, int tr, int l, int r) {
if (tr < l || tl > r) return {0, 0};
if (l <= tl && tr <= r) return tree[letter][node];
int mid = (tl + tr) / 2;
pair<int, int> p1 = get(letter, 2 * node, tl, mid, l, r);
pair<int, int> p2 = get(letter, 2 * node + 1, mid + 1, tr, l, r);
return getMax(p1, p2);
}
main() {
IOS
cin >> s;
n = s.size();
s = '.' + s;
cin >> q;
for(int i = 1; i <= q; i++) cin >> p[i].F >> p[i].S;
sort(p, p + q + 1);
for(int i = 1; i <= n; i++) {
int x;
cin >> x;
a[x] = i;
}
int mx = 0;
for(int j = 0; j < 26; j++)
build(j, 1, 1, n);
for(int i = 1; i <= q; i++) {
for(int j = 0; j < 26; j++) {
int l = p[i].F, r = p[i].S;
int need = get(j, 1, 1, n, l, r).S;
mx = max(mx, need);
}
}
cout << mx << '\n';
}
Compilation message (stderr)
grudanje.cpp:47:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
47 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |