QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#786376 | #8079. Range Periodicity Query | lfyszy | RE | 7ms | 52608kb | C++14 | 2.9kb | 2024-11-26 21:16:52 | 2024-11-26 21:16:52 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define SP << " " <<
#define fish signed
using namespace std;
mt19937 rnd(1209);
const int INF = 0x3f3f3f3f3f3f3f3f;
const int base = 66683, N = 5e5 + 10, base2 = 131, mod = 998244353;
unsigned long long prod[N], h[N];
long long prod2[N], h2[N];
unsigned long long get(int l, int r) {return h[r] - h[l - 1] * prod[r - l + 1];}
int get2(int l, int r) {return ((h2[r] - h2[l - 1] * prod2[r - l + 1] % mod) % mod + mod) % mod;}
int sl[N], sr[N], t[N]; char a[N];
int mn[N << 2];
#define ls(u) (u << 1)
#define rs(u) (u << 1 | 1)
void modify(int u, int l, int r, int pos, int k)
{
chmin(mn[u], k);
if(l == r) return ;
int mid = l + r >> 1;
if(pos <= mid) modify(ls(u), l, mid, pos, k);
else modify(rs(u), mid + 1, r, pos, k);
}
int query(int u, int l, int r, int ql, int qr)
{
if(ql <= l && r <= qr) return mn[u];
int mid = l + r >> 1, res = INF;
if(ql <= mid) chmin(res, query(ls(u), l, mid, ql, qr));
if(qr > mid) chmin(res, query(rs(u), mid + 1, r, ql, qr));
return res;
}
struct node {int l, r, id;} ;
int ans[N];
vector<node> vp[N];
vector<node> vq[N];
fish main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int hh = n + 1, tt = n;
for(int i = 1; i <= n; i ++)
{
char ch; cin >> ch;
if('A' <= ch && ch <= 'Z') a[++ tt] = ch - 'A' + 'a';
else a[-- hh] = ch;
sl[i] = hh, sr[i] = tt;
}
prod[0] = prod2[0] = 1;
for(int i = 1; i <= n; i ++) prod[i] = prod[i - 1] * base;
for(int i = hh; i <= tt; i ++) h[i] = h[i - 1] * base + a[i];
for(int i = 1; i <= n; i ++) prod2[i] = (prod2[i - 1] * base2) % mod;
for(int i = hh; i <= tt; i ++) h2[i] = (h2[i - 1] * base2 % mod + a[i]) % mod;
for(int i = 1; i <= n; i ++)
{
int l = i, r = n, res = i;
while(l <= r)
{
int mid = l + r >> 1;
int l1 = sl[mid], l2 = sl[mid] + i;
int r1 = sr[mid] - i, r2 = sr[mid];
if(get(l1, r1) == get(l2, r2) && get2(l1, r1) == get2(l2, r2)) l = mid + 1, res = mid;
else r = mid - 1;
}
t[i] = res;
}
int m; cin >> m;
for(int i = 1; i <= m; i ++) {int x; cin >> x; vp[t[x]].push_back({i, x});}
memset(mn, 0x3f, sizeof mn);
int k; cin >> k;
for(int i = 1; i <= k; i ++) {int k, l, r; cin >> k >> l >> r; vq[k].push_back({l, r, i});}
for(int i = n; i >= 1; i --)
{
for(auto v : vp[i]) modify(1, 1, m, v.l, v.r);
for(auto v : vq[i]) {ans[v.id] = query(1, 1, m, v.l, v.r); assert(ans[v.id] != INF); if(ans[v.id] > i) ans[v.id] = -1;}
// for(int j = 1; j <= m; j ++) cerr << query(1, 1, m, j, j) << " "; cerr << "\n";
}
for(int i = 1; i <= k; i ++) cout << ans[i] << "\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 7ms
memory: 52608kb
input:
7 AABAAba 9 4 3 2 1 7 5 3 6 1 6 1 4 4 2 1 4 2 1 3 3 3 5 5 4 7 7 8 9
output:
1 1 2 -1 3 6
result:
ok 6 lines
Test #2:
score: -100
Runtime Error
input:
200000 BAbBbBabBBbbABbbaBbaaabaBBAbBbBAAAAABBaBaAAabBAAbABaaBABAabAAAbabbAaBABAbabbAAAbbbbabBBAbbBaabBAAAbBBBbBbbAbbbBabbBABaBAaAAAbBbaABabBAbAAbBbbAbAbBaabAbBBbaaaaBaBbbABBBaaabBaBABAbBabBbbAABBbaBAbaBAbAAABABAbaabbaAAaBAbAbAbBBbaaaAaBaaABBbBAAaAAAaaABbbaAbAaBbaAaaababbaBbaAAAAAAabbBaAabbbaBBAAaABb...