QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#532151 | #2343. First of Her Name | Swarthmore# | AC ✓ | 2645ms | 122056kb | C++20 | 2.7kb | 2024-08-25 00:42:05 | 2024-08-25 00:42: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;
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;
while (lo1 < hi1) {
int mid = (lo1+hi1)/2;
if (get(S, ord[mid].s) <= 0) {
hi1 = mid;
} else lo1 = mid+1;
}
int lo2 = -1, 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;
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
Test #2:
score: 0
Accepted
time: 1ms
memory: 5564kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 3556kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 3568kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 3564kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 3660kb
Test #7:
score: 0
Accepted
time: 1ms
memory: 5616kb
Test #8:
score: 0
Accepted
time: 1ms
memory: 5560kb
Test #9:
score: 0
Accepted
time: 1ms
memory: 5624kb
Test #10:
score: 0
Accepted
time: 0ms
memory: 3644kb
Test #11:
score: 0
Accepted
time: 1000ms
memory: 121980kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 5824kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 3632kb
Test #14:
score: 0
Accepted
time: 1ms
memory: 5840kb
Test #15:
score: 0
Accepted
time: 1ms
memory: 5652kb
Test #16:
score: 0
Accepted
time: 0ms
memory: 3628kb
Test #17:
score: 0
Accepted
time: 0ms
memory: 3796kb
Test #18:
score: 0
Accepted
time: 0ms
memory: 3572kb
Test #19:
score: 0
Accepted
time: 0ms
memory: 3644kb
Test #20:
score: 0
Accepted
time: 1ms
memory: 5616kb
Test #21:
score: 0
Accepted
time: 0ms
memory: 3644kb
Test #22:
score: 0
Accepted
time: 0ms
memory: 5652kb
Test #23:
score: 0
Accepted
time: 0ms
memory: 4784kb
Test #24:
score: 0
Accepted
time: 7ms
memory: 6756kb
Test #25:
score: 0
Accepted
time: 17ms
memory: 5024kb
Test #26:
score: 0
Accepted
time: 17ms
memory: 6828kb
Test #27:
score: 0
Accepted
time: 17ms
memory: 5020kb
Test #28:
score: 0
Accepted
time: 16ms
memory: 4792kb
Test #29:
score: 0
Accepted
time: 18ms
memory: 7048kb
Test #30:
score: 0
Accepted
time: 9ms
memory: 6700kb
Test #31:
score: 0
Accepted
time: 15ms
memory: 4824kb
Test #32:
score: 0
Accepted
time: 59ms
memory: 6968kb
Test #33:
score: 0
Accepted
time: 77ms
memory: 10240kb
Test #34:
score: 0
Accepted
time: 997ms
memory: 121884kb
Test #35:
score: 0
Accepted
time: 2587ms
memory: 122036kb
Test #36:
score: 0
Accepted
time: 2391ms
memory: 121904kb
Test #37:
score: 0
Accepted
time: 1524ms
memory: 121976kb
Test #38:
score: 0
Accepted
time: 2552ms
memory: 121896kb
Test #39:
score: 0
Accepted
time: 2326ms
memory: 121884kb
Test #40:
score: 0
Accepted
time: 1225ms
memory: 121980kb
Test #41:
score: 0
Accepted
time: 2246ms
memory: 122056kb
Test #42:
score: 0
Accepted
time: 2595ms
memory: 121836kb
Test #43:
score: 0
Accepted
time: 2645ms
memory: 121924kb
Test #44:
score: 0
Accepted
time: 1042ms
memory: 121920kb
Test #45:
score: 0
Accepted
time: 2600ms
memory: 121872kb
Test #46:
score: 0
Accepted
time: 1ms
memory: 3852kb
Test #47:
score: 0
Accepted
time: 0ms
memory: 3592kb
Test #48:
score: 0
Accepted
time: 1ms
memory: 5640kb
Test #49:
score: 0
Accepted
time: 1ms
memory: 5572kb
Test #50:
score: 0
Accepted
time: 1ms
memory: 5704kb
Test #51:
score: 0
Accepted
time: 0ms
memory: 5608kb
Test #52:
score: 0
Accepted
time: 1ms
memory: 5656kb
Test #53:
score: 0
Accepted
time: 5ms
memory: 3912kb
Test #54:
score: 0
Accepted
time: 12ms
memory: 4372kb
Test #55:
score: 0
Accepted
time: 3ms
memory: 3980kb
Test #56:
score: 0
Accepted
time: 42ms
memory: 10140kb