QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#626443#7901. Basic Substring StructureMiniLongTL 0ms17932kbC++206.5kb2024-10-10 09:01:172024-10-10 09:01:18

Judging History

你现在查看的是最新测评结果

  • [2024-10-10 09:01:18]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:17932kb
  • [2024-10-10 09:01:17]
  • 提交

answer

#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
    #ifdef ONLINE_JUDGE
    char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
    #define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
    #else
    #define get() getchar()
    #endif
    template<typename T> inline void read(T &t){
        T x = 0, f = 1;
        char c = getchar();
        while(!isdigit(c)){
            if(c == '-') f = -f;
            c = getchar();
        }
        while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
        t = x * f;
    }
    template<typename T, typename ... Args> inline void read(T &t, Args&... args){
        read(t);
        read(args...);
    }
    template<typename T> void write(T t){
        if(t < 0) putchar('-'), t = -t;
        if(t >= 10) write(t / 10);
        putchar(t % 10 + '0');
    }
    template<typename T, typename ... Args> void write(T t, Args... args){
        write(t), putchar(' '), write(args...);
    }
    template<typename T> void writeln(T t){
        write(t);
        puts("");
    }
    template<typename T> void writes(T t){
        write(t), putchar(' ');
    }
    #undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
    const ll mod = 998244353;
    ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
    void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
    void add(ll &x, ll y){x = (x + y) % mod;}
    void mul(ll &x, ll y){x = x * y % mod;}
    ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
    ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
    ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 2e5 + 5;
int n, s[N];
namespace SA{
    int m, x[N], y[N], sa[N], rk[N], c[N], ht[N], f[N][25];
    void build(){
        m = n;
        _rep(i, 1, n) c[x[i] = s[i]]++;
        _rep(i, 1, m) c[i] += c[i - 1];
        _req(i, n, 1) sa[c[x[i]]--] = i;
        for(int k = 1; k <= n; k <<= 1){
            int p = 0;
            _rep(i, n - k + 1, n) y[++p] = i;
            _rep(i, 1, n) if(sa[i] > k) y[++p] = sa[i] - k;
            _rep(i, 1, m) c[i] = 0;
            _rep(i, 1, n) c[x[y[i]]]++;
            _rep(i, 1, m) c[i] += c[i - 1];
            _req(i, n, 1) sa[c[x[y[i]]]--] = y[i];
            p = y[sa[1]] = 1;
            _rep(i, 2, n) y[sa[i]] = (x[sa[i]] == x[sa[i - 1]] && x[sa[i] + k] == x[sa[i - 1] + k]) ? p : ++p;
            swap(x, y);
            if(p >= n) break;
            m = n;
        }
        _rep(i, 1, n) rk[sa[i]] = i;
        int k = 0; ht[1] = 0;
        _rep(i, 1, n){
            if(rk[i] == 1) continue;
            if(k) --k;
            while(s[i + k] == s[sa[rk[i] - 1] + k]) ++k;
            ht[rk[i]] = k;
        }
        _rep(i, 1, n) f[i][0] = ht[i];
        _rep(i, 1, 20) _rep(j, 1, n - (1 << i) + 1) f[j][i] = min(f[j][i - 1], f[j + (1 << i - 1)][i - 1]);
    }
    int lcp(int x, int y){
        if(x > n || y > n || x < 1 || y < 1) return 0;
        if(x == y) return n - x + 1;
        x = rk[x], y = rk[y]; if(x > y) swap(x, y); x++;
        int k = __lg(y - x + 1);
        return min(f[x][k], f[y - (1 << k) + 1][k]);
    }
    void clr(){
        _rep(i, 1, n) x[i] = y[i] = sa[i] = rk[i] = c[i] = ht[i] = 0, mst(f[i], 0);
        m = 0;
    }
}
using SA::lcp;
ll ans, z[N], r[N], f[N], nxt[N], res[N];
vector<int> h[N];
void clr(){
    ans = 0;
    _rep(i, 1, n) z[i] = r[i] = f[i] = nxt[i] = res[i] = 0, h[i].clear();
    SA::clr();
}
int lcp(int x, int y, int p, int c){
    int lst = s[p]; s[p] = c;
    int cur = min(lcp(x, y), p - x);
    if(cur < p - x || s[y + cur] != s[x + cur]) return s[p] = lst, cur;
    cur += lcp(x + cur + 1, y + cur + 1) + 1;
    return s[p] = lst, cur;
}
int main(){
    multitest(){
        read(n);
        _rep(i, 1, n) read(s[i]);
        SA::build();
        _rep(i, 1, n){
            z[i] = lcp(i, 1), ans += z[i], h[i + z[i] - 1].pb(i), r[i] = i + z[i] - 1;
            if(z[i] < n - i + 1){
                nxt[i] = s[z[i] + 1];
                f[i] = lcp(z[i] + 2, i + z[i] + 1, r[i] + 1, nxt[i]) + 1;
            }
            // debug("(%d,%d) f:%d\n", z[i], r[i], f[i]);
        }
        ll sum = 0, cnt = 0;
        /*work 1*/
        vector<int> p;
        _rep(i, 2, n) p.pb(i);
        sort(p.begin(), p.end(), [](int x, int y){return s[x] < s[y];});
        _rep(i, 0, n - 2){
            int j = i; ll tot = lcp(p[i] + 1, 2) + 1;
            while(j < n - 2 && s[p[j + 1]] == s[p[i]]) j++, tot += lcp(p[j] + 1, 2) + 1;
            tot += n;
            res[1] = max(res[1], tot);
        }
        /*work 2~n*/
        _rep(i, 2, n){
            ll cur = ans;
            _rep(j, 2, i) if(r[j] >= i) cur -= r[j] - i + 1;
            _rep(j, i + 1, n) if(z[j] >= i) cur -= z[j] - i + 1;
            // debug("i:%d ans:%d cur:%d\n", i, ans, cur);
            _rep(j, 1, n){
                ll tot = 0;
                _rep(k, 2, i) if(r[k] == i - 1) tot += f[k];
                _rep(k, i + 1, n) if(z[k] == i - 1) tot += f[k];
                // debug("j:%d tot:%d\n", j, tot);
                res[i] = max(res[i], cur + tot);
            }
            // if(z[i]) ans += i + z[i] - 1, cnt++;
            // ll cur = ans - (sum - cnt * (i - 1));
            // sort(h[i - 1].begin(), h[i - 1].end(), [](int x, int y){return nxt[x] < nxt[y];});
            // int len = h[i - 1].size();
            // ll maxn = 0;
            // _rep(j, 0, len - 1){
            //     int k = j; ll tot = f[h[i - 1][j]];
            //     while(k < len - 1 && nxt[h[i - 1][k + 1]] == nxt[h[i - 1][j]]) k++, tot += f[h[i - 1][k]];
            //     maxn = max(maxn, tot); j = k;
            // }
            // res[i] = cur + maxn;
            // for(auto &j : h[i]) sum -= i, cnt--;
        }
        ll t = 0;
        _rep(i, 1, n){
            t += (res[i] ^ i);
            debug("%d\n", res[i]);
        }
        writeln(t);
        clr();
    }    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 17932kb

input:

2
4
2 1 1 2
12
1 1 4 5 1 4 1 9 1 9 8 10

output:

15
217

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10000
8
2 1 2 1 1 1 2 2
9
2 2 1 2 1 2 1 2 1
15
2 1 2 1 1 1 1 2 2 1 2 1 2 2 1
2
1 1
10
2 1 1 1 2 2 1 1 2 2
3
2 1 2
11
1 2 2 1 1 2 1 2 2 1 1
14
2 1 1 1 1 2 1 1 1 2 2 1 2 1
12
2 2 2 1 2 2 2 1 1 2 1 2
4
2 1 1 2
8
1 2 2 2 1 2 1 1
8
1 1 2 1 2 1 1 1
6
2 1 1 1 2 2
14
2 2 1 1 1 1 2 2 2 1 2 2 1 1
10
1 2 2 1 1...

output:

94
128
350
2
210
20
269
318
290
15
95
108
93
353
237
3
338
364
371
353
2
19
134
76
21
90
54
265
10
64
36
216
135
115
252
267
21
65
374
66
102
19
34
68
324
360
274
313
314
299
320
370
233
344
3
303
54
330
8
65
32
146
438
55
342
89
246
2
165
346
248
20
154
2
438
404
392
88
279
360
20
54
26
294
3
23
35...

result: