이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define SZ(x) (int)(x).size()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
const int MX_N = 1e5+5;
const int MX_Q = 1e5+5;
string S;
int N, Q, A[MX_Q], B[MX_Q], P[MX_N];
int nxt[MX_N], pos[26];
struct node {
int s, e, m, v;
node *l, *r;
node(int s, int e): s(s), e(e), m((s+e)/2), v(0) {
if (s != e) {
l = new node(s,m);
r = new node(m+1,e);
}
}
void update(int i, int x) {
if (s == e) { v = x; return; }
if (i <= m) l->update(i,x);
else r->update(i,x);
v = min(l->v,r->v);
}
int query(int x, int y) {
if (s == x && e == y) return v;
else if (y <= m) return l->query(x,y);
else if (x > m) return r->query(x,y);
else return min(l->query(x,m),r->query(m+1,y));
}
} *root;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> S; N = S.length();
cin >> Q;
FOR(i,1,Q){
cin >> A[i] >> B[i];
--A[i], --B[i];
}
FOR(i,1,N){
cin >> P[i];
--P[i];
}
root = new node(0,N-1);
int lo = -1, hi = N;
while (hi-lo > 1) {
int mid = (lo+hi)/2;
string T(S);
FOR(i,0,mid) T[P[i]] = '#';
FOR(i,0,25) pos[i] = N;
RFOR(i,N-1,0){
if (T[i] == '#') nxt[i] = N;
else {
nxt[i] = pos[T[i]-'a'];
pos[T[i]-'a'] = i;
}
root->update(i,nxt[i]);
}
bool ok = 1;
FOR(i,1,Q){
ok &= root->query(A[i],B[i]) > B[i];
}
//FOR(i,0,N-1) cout << nxt[i] << ' ';
//cout << endl;
//cout << "pos i " << mid << " :: " << ok << endl;
if (ok) hi = mid;
else lo = mid;
}
cout << lo+1 << '\n';
}
# | 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... |