QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#532140 | #2343. First of Her Name | Swarthmore# | WA | 0ms | 3796kb | C++20 | 2.7kb | 2024-08-25 00:33:05 | 2024-08-25 00:33:05 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a) - 1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define sz(x) (int)(x).size()
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
const char nl = '\n';
const int MX = 1e6+1;
const int L = 21;
int N;
int Q;
char C[MX];
int P[MX];
int get(string &S, int v) {
F0R(i, sz(S)) {
if (v == -1) return 1;
if (S[i] != C[v]) {
if (S[i] < C[v]) return -1;
return 1;
}
v = P[v];
}
return 0;
}
void solve() {
cin >> N >> Q;
F0R(i, N) cin >> C[i] >> P[i];
F0R(i, N) P[i]--;
int anc[N][22]; F0R(i, N) {
anc[i][0] = P[i];
}
FOR(j, 1, 22) {
F0R(i, N) {
if (anc[i][j-1] == -1) {
anc[i][j] = -1;
} else {
anc[i][j] = anc[anc[i][j-1]][j-1];
}
}
}
int cla[N]; F0R(i, N) cla[i] = C[i] - 'A';
F0R(iter, 22) {
vector<array<int, 3>> cur;
F0R(i, N) {
cur.pb({cla[i], (anc[i][iter]==-1?-1:cla[anc[i][iter]]), i});
}
sort(all(cur));
int lst = 0;
F0R(i, N) {
if (i && (cur[i][0] != cur[i-1][0] || cur[i][1] != cur[i-1][1])) {
lst = i;
}
cla[cur[i][2]] = lst;
}
/*F0R(i, N) {
if (iter == 0) {
cout << i << ": " << cla[i] << nl;
}
}*/
}
vpi ord;
F0R(i, N) {
ord.pb({cla[i], i});
}
sort(all(ord));
/*F0R(i, N) {
cout << ord[i].s << nl;
}
cout << nl;*/
while(Q--) {
string S; cin >> S;
// find first geq S
int lo1 = 0, hi1 = N-1;
while (lo1 < hi1) {
int mid = (lo1+hi1)/2;
if (get(S, ord[mid].s) <= 0) {
hi1 = mid;
} else lo1 = mid+1;
}
int lo2 = 0, hi2 = N-1;
// find last leq S
while (lo2 < hi2) {
int mid = (lo2+hi2+1)/2;
if (get(S, ord[mid].s) >= 0) {
lo2 = mid;
} else hi2 = mid-1;
}
cout << lo2-lo1+1 << nl;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 3796kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 3628kb
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3564kb