QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#611082 | #7036. Can You Solve the Harder Problem? | ucup-team4906# | RE | 0ms | 36704kb | C++20 | 3.5kb | 2024-10-04 19:13:45 | 2024-10-04 19:13:45 |
Judging History
answer
#include<bits/stdc++.h>
#define F(i, a, b) for(int i = a; i <= b; i ++)
using namespace std;
#define N 200010
typedef long long ll;
int n;
int a[N];
map<int, int>ch[N];
int fa[N], tot = 1, lst = 1;
int mn[N], len[N];
vector<int>e[N];
int f[21][N], pos[N];
int st[N], top;
ll sum[N << 2], tg[N << 2];
#define ls (x << 1)
#define rs (x << 1 | 1)
#define mid ((l + r) >> 1)
void clr(int l, int r, int x)
{
sum[x] = tg[x] = 0;
if (l == r)return ;
clr(l, mid, ls); clr(mid + 1, r, rs);
}
void push(int x, int w, int l, int r)
{
sum[x] = 1LL * (r - l + 1) * w;
tg[x] = w;
}
void pushdown(int x, int l, int r)
{
if (tg[x])
{
push(ls, tg[x], l, mid);
push(rs, tg[x], mid + 1, r);
tg[x] = 0;
}
}
void pushup(int x)
{
sum[x] = sum[ls] + sum[rs];
}
void upd(int l, int r, int x, int ql, int qr, int w)
{
if (l == ql && r == qr) {push(x, w, l, r); return ;}
pushdown(x, l, r);
if (qr <= mid) upd(l, mid, ls, ql, qr, w);
else if (ql > mid)upd(mid + 1, r, rs, ql, qr, w);
else {upd(l, mid, ls, ql, mid, w); upd(mid + 1, r, rs, mid + 1, qr, w);}
pushup(x);
}
ll qry(int l, int r, int x, int ql, int qr)
{
if (l == ql && r == qr)return sum[x];
pushdown(x, l, r);
if (qr <= mid) return qry(l, mid, ls, ql, qr);
else if (ql > mid) return qry(mid + 1, r, rs, ql, qr);
else return qry(l, mid, ls, ql, mid) + qry(mid + 1, r, rs, mid + 1, qr);
}
void add(int x, int id)
{
int np = ++ tot, p = lst; len[np] = len[p] + 1; mn[np] = id; pos[id] = np;
while (p && !ch[p][x])ch[p][x] = np, p = fa[p];
if (p == 0)fa[np] = 1;
else
{
int q = ch[p][x];
if (len[q] == len[p] + 1)fa[np] = q;
else
{
int nq = ++ tot; len[nq] = len[p] + 1; mn[nq] = 1e9 + 7;
fa[nq] = fa[q]; fa[q] = fa[np] = nq;
ch[nq] = ch[q];
while (p && ch[p][x] == q)ch[p][x] = nq, p = fa[p];
}
}
lst = np;
}
void dfs(int u, int fa)
{
f[0][u] = fa;
for (int i = 1; i <= 20; i ++) f[i][u] = f[i - 1][f[i - 1][u]];
for (int v : e[u]) dfs(v, u), mn[u] = min(mn[u], mn[v]);
}
int qry(int t)
{
int x = pos[t];
// cout << "GG:" << t << endl;
// cout << "??:" << x << " " << mn[x] <<endl;
int tx = x;
while (tx)
{
tx = fa[tx];
// cout << "??:" << tx << ' ' << mn[tx] << endl;
}
assert(mn[x] == t);
for (int i = 20; i >= 0; i --) if(mn[f[i][x]] == t)x = f[i][x];
return len[fa[x]] + 1;
}
void clr()
{
clr(1, n, 1);
for (int i = 1; i <= tot; i ++)
{
mn[i] = 0, e[i].clear(), ch[i].clear();
}
top = 0; tot = lst = 1;
}
void sol()
{
cin >> n;
mn[1] = 0;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) add(a[i], i);
for (int i = 2; i <= tot; i ++)e[fa[i]].push_back(i);
dfs(1, 0);
clr(1, n, 1);
ll ans = 0;
for (int i = 1; i <= n; i ++)
{
int len = qry(i);
while (top && a[i] >= a[st[top]])top --;
st[++ top] = i; upd(1, n, 1, st[top - 1] + 1, i, a[i]);
// cout << "??:" << i << ' ' << len << ' ' << qry(1, n, 1, 1, i - len + 1) << endl;
ans += qry(1, n, 1, 1, i - len + 1);
}
cout << ans << '\n';
clr();
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
F(i, 1, t) sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 36704kb
input:
2 3 1 2 3 3 2 3 3
output:
14 14
result:
ok 2 lines
Test #2:
score: -100
Runtime Error
input:
1000 407 205460 200518 200561 199235 198814 198136 198802 206763 198795 200705 205862 209366 204017 209481 206300 198499 197364 200897 208928 196983 205605 206396 205140 201050 199886 207314 196947 207905 204999 203288 200298 198594 198286 197714 206506 203962 197628 201127 206380 199090 200711 2063...