QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#51628 | #4632. Card Shark | bvd# | RE | 0ms | 0kb | C++ | 3.5kb | 2022-10-03 05:59:21 | 2022-10-03 05:59:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int total_seq(int x,int y) {
return (x+y)*(y-x+1)/2;
}
struct SuffixTree {
enum { N = 1000010, ALPHA = 27 };
int toi(char c) { return c - 'a'; }
string a;
int t[N][ALPHA],l[N],r[N],p[N],s[N],v=0,q=0,m=2;
int numLeaf[N], suffLen[N], delta[N];
int dfsOrder[N];
void ukkadd(int i,int c) { suff:
if (r[v]<=q) {
if (t[v][c]==-1) { t[v][c]=m; l[m]=i;
p[m++]=v; v=s[v]; q=r[v]; goto suff; }
v=t[v][c]; q=l[v];
}
if (q==-1 || c==toi(a[q])) q++; else {
l[m+1]=i; p[m+1]=m; l[m]=l[v]; r[m] = q;
p[m]=p[v]; t[m][c] = m+1; t[m][toi(a[q])]=v;
l[v]=q; p[v]=m; t[p[m]][toi(a[l[m]])]=m;
v=s[p[m]]; q=l[m];
while (q<r[m]) { v=t[v][toi(a[q])]; q+=r[v]-l[v]; }
if (q==r[m]) s[m]=v; else s[m] = m+2;
q=r[v]-(q-r[m]); m+=2; goto suff;
}
}
SuffixTree(string a) : a(a) {
fill(r, r+N, sz(a));
memset(s, 0, sizeof s);
memset(t, -1, sizeof t);
fill(t[1], t[1] + ALPHA, 0);
s[0] = 1; l[0] = l[1] = -1; r[0] = r[1] = p[0] = p[1] = 0;
rep(i,0,sz(a)) ukkadd(i, toi(a[i]));
}
void decorate(vi& dfsOrder, int x = 0, int sumLength = 0) {
dfsOrder.push_back(x);
numLeaf[x] = 0;
bool isLeaf = true;
int strLength = 0;
if (x!=0) strLength = min(sz(a)-1, r[x]) - l[x];
rep(i,0,ALPHA) if (t[x][i]!=-1) {
isLeaf = false;
decorate(dfsOrder, t[x][i], sumLength + strLength);
numLeaf[x] += numLeaf[t[x][i]];
}
numLeaf[x] += isLeaf;
suffLen[x] = sumLength + strLength;
delta[x] = numLeaf[x] * total_seq(sumLength+1, sumLength + strLength);
//cout << x << ' ' << numLeaf[x] << ' ' << total_seq(sumLength+1, sumLength + strLength) << endl;
}
};
string s;
SuffixTree *tree = nullptr;
vector<pii> queries;
vi dfsOrder;
struct FinalForm {
int occ, md, node;
int k, id;
};
vector<FinalForm> final_queries;
main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cin >> s; s+=(char) ('a' + 26);
tree = new SuffixTree(s);
tree->decorate(dfsOrder);
int q; cin >> q;
rep(i,0,q) {
int k; cin >> k;
queries.push_back({k, i});
}
sort(all(queries));
int q_ptr = 0, dfs_ptr = 0;
int sumLen = 0;
rep(q_ptr,0,sz(queries)) {
int k = queries[q_ptr].first;
int currNode = dfsOrder[dfs_ptr];
do {
int delta = tree->delta[currNode];
if (sumLen + delta < k) {
sumLen += delta;
dfs_ptr++;
//cout << sumLen << ' ' << dfs_ptr << endl;
currNode = dfsOrder[dfs_ptr];
} else break;
} while (true);
int rem = k - sumLen;
int multiplier = tree->numLeaf[currNode];
int strLength = min(tree->r[currNode], sz(s)-1) - tree->l[currNode];
int baseLength = tree->suffLen[currNode] - strLength;
//cout << dfs_ptr << ' ' << baseLength << ' ' << multiplier << endl;
int dau = 1, cuoi = tree->r[currNode] - tree->l[currNode];
do {
int giua = (dau+cuoi)/2;
if (multiplier * total_seq(baseLength, baseLength + giua - 1) < rem) dau = giua+1;
else cuoi = giua-1;
} while (dau<=cuoi);
rem -= total_seq(baseLength, baseLength + cuoi - 1) * multiplier;
int bestSz = baseLength + cuoi;
int occ = (rem-1) / bestSz;
rem = (rem-1)%bestSz;
final_queries.push_back({occ,rem,currNode,k,queries[q_ptr].second});
}
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
5 4 3 0100010 00100 001000100 0010 0100010