QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#843346#9970. Looping RPSucup-team6275#WA 165ms89956kbC++207.6kb2025-01-04 18:08:032025-01-04 18:08:03

Judging History

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

  • [2025-01-04 18:08:03]
  • 评测
  • 测评结果:WA
  • 用时:165ms
  • 内存:89956kb
  • [2025-01-04 18:08:03]
  • 提交

answer

#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>

using namespace std;

using ll = long long;
using ld = long double;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) for (int i = 0; i < (n); i += 1)
#define len(a) ((int)(a).size())

mt19937 rnd(234);
const int inf = 1e8;

struct SGT {
    int n;
    vector<pair<int, int>> t;

    void build(vector<int> a) {
        n = 1;
        while (n < len(a)) {
            n *= 2;
        }
        t.resize(2 * n);
        rep(i, len(a)) {
            t[i + n] = {a[i], i};
        }
        for (int i = n - 1; i >= 0; i -= 1) {
            t[i] = min(t[i + i], t[i + i + 1]);
        }
    }

    pair<int, int> get(int l, int r) {
        l += n;
        r += n + 1;
        pair<int, int> rs = {inf + 1, -1};
        while (l < r) {
            if (l & 1) {
                rs = min(rs, t[l]);
                ++l;
            }
            if (r & 1) {
                --r;
                rs = min(rs, t[r]);
            }
            l /= 2;
            r /= 2;
        }
        return rs;
    }
};

const int C = 2;

int bases[C] = {179, 57};
int moduls[C] = {(int)1e9 + 7, 1791791791};

inline int add(int a, int b, int mod) {
    a += b;
    if (a >= mod) a -= mod;
    return a;
}

inline int sub(int a, int b, int mod) {
    a -= b;
    if (a < 0) a += mod;
    return a;
}

inline int mul(int a, int b, int mod) {
    return 1ll * a * b % mod;
}

struct Hasher {
    int flex[C];

    Hasher() {
        for (int i = 0; i < C; ++i) flex[i] = 0;
    }

    Hasher(int x) {
        for (int i = 0; i < C; ++i) {
            flex[i] = x % moduls[i];
        }
    }
};

Hasher operator+(const Hasher& a, const Hasher& b) {
    Hasher ans;
    for (int i = 0; i < C; ++i) {
        ans.flex[i] = add(a.flex[i], b.flex[i], moduls[i]);
    }
    return ans;
}

bool operator ==(const Hasher& a, const Hasher& b) {
    for (int i = 0; i < C; ++i) {
        if (a.flex[i] != b.flex[i]) {
            return false;
        }
    }
    return true;
}

int n;
vector<string> a;
vector <vector <Hasher>> hashs;
vector<int> d;
const int MAXN = 2e6 + 100;
const int MAXN2 = 1e6 + 10;
int powers[C][MAXN];
int is_len[MAXN2];
vector <int> precalc[C][MAXN2];
SGT sgt;

bool cmp(const string &a, const string &b) {
    rep(i, max(len(a), len(b)) * 2) {
        if (a[i % len(a)] != b[i % len(b)]) {
            return (a[i % len(a)] < b[i % len(b)]);
        }
    }
    return false;
}

Hasher get_hash(int ind, int len) {
    int aln = a[ind].size();
    int cnt_ful = len / aln;

    Hasher cur_hash;
    if (cnt_ful > 0) {
        cur_hash = hashs[ind].back();
        for (int i = 0; i < C; ++i) {
            int coef = precalc[i][aln][cnt_ful - 1];
            coef = mul(coef, powers[i][len - cnt_ful * aln], moduls[i]);
            cur_hash.flex[i] = mul(cur_hash.flex[i], coef, moduls[i]);
        }
    }

    cur_hash = cur_hash + hashs[ind][len % aln];
    return cur_hash;
}

bool lesha_cmp(int i, int j) {
    int max_len = max(len(a[i]), len(a[j])) * 2;

    if (get_hash(i, max_len) == get_hash(j, max_len)) {
        return false;
    }

    int l = 0;
    int r = max_len;

    while (r - l > 1) {
        int m = (l + r) / 2;
        if (get_hash(i, m) == get_hash(j, m)) l = m;
        else r = m;
    }
    //r - len
    //l - index
    return a[i][l % len(a[i])] < a[j][l % len(a[j])];
}

void build_hash() {
    hashs.assign(n, {});
    for (int i = 0; i < n; ++i) {
        hashs[i].resize(a[i].size() + 1);
        for (int j = 0; j < a[i].size(); ++j) {
            hashs[i][j + 1] = hashs[i][j];
            for (int k = 0; k < C; ++k) {
                hashs[i][j + 1].flex[k] = mul(hashs[i][j + 1].flex[k], bases[k], moduls[k]);
            }
            hashs[i][j + 1] = hashs[i][j + 1] + Hasher(a[i][j] - 'A');
        }
    }

    for (int i = 0; i < C; ++i) {
        powers[i][0] = 1;
        for (int j = 1; j < MAXN; ++j) {
            powers[i][j] = mul(powers[i][j - 1], bases[i], moduls[i]);
        }
    }

    for (int i = 0; i < C; ++i) {
        int mem = 1;
        for (int j = 0; j < MAXN2; ++j) {
            if (!is_len[j]) {
                mem = mul(mem, bases[i], moduls[i]);
                continue;
            }
            int cnt = MAXN / j;

            precalc[i][j].assign(cnt, 0);
            int cur = 0;
            int cha = 1;

            for (int k = 0; k < cnt; ++k) {
                cur = add(cur, cha, moduls[i]);
                cha = mul(cha, mem, moduls[i]);
                precalc[i][j][k] = cur;
            }


            mem = mul(mem, bases[i], moduls[i]);
        }
    }
}

ll solve(ll l, ll r) {
    if (r - l + 1 < 3) {
        return 0;
    }
    auto [mn, pos] = sgt.get(l, r - 1);
    if (mn == inf) {
        return 0;
    }
    if (r == pos + 1) {
        return solve(l, pos);
    }
    auto [mn2, pos2] = sgt.get(pos + 1, r - 1);
    if (mn != mn2) {
        ll res1 = solve(l, pos);
        ll res2 = solve(pos + 1, r);
        return res1 + res2;
    }
    ll myres = ll(pos - l + 1) * (pos2 - pos) * (r - pos2);
    ll res1 = solve(l, pos);
    ll res2 = solve(pos + 1, pos2);
    ll res3 = solve(pos2 + 1, r);
    return myres + res1 + res2 + res3;
}

char sym[3] = {'P', 'N', 'K'};

string gen() {
    int len = rnd() % 20 + 1;
    string ans;
    for (int i = 0; i < len; ++i) {
        ans.push_back(sym[rnd() % 3]);
    }
    return ans;
}

void stress() {
    int t = 100000;
    while (t--) {
        cout << t << "\n";
        n = 5;
        a.assign(n, {});
        for (int i = 0; i < n; ++i) {
            a[i] = gen();
            is_len[len(a[i])] = 1;
        }
        build_hash();

        vector <int> ind(n);
        for (int i = 0; i < n; ++i) ind[i] = i;
        sort(all(ind), lesha_cmp);

        vector <string> na;
        for (int i = 0; i < n; ++i) na.push_back(a[ind[i]]);

        for (int i = 0; i < n - 1; ++i) {
            if (cmp(na[i + 1], na[i])) {
                cout << "Found\n";

                cout << n << "\n";
                for (auto j : a) cout << j << "\n";
                return;
            }
        }
    }
}

int32_t main() {
    if (0) {
        stress();
        return 0;
    }
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    cin >> n;
    a.resize(n);
    rep(i, n) {
        cin >> a[i];
        is_len[a[i].size()] = 1;
    }
    build_hash();

//    cout << lesha_cmp(1, 4) << endl;
//    sort(all(a), cmp);
    if (0) {
        vector <string> flex = a;
        sort(all(flex), cmp);
        for (auto i : flex) cout << i << "\n";
        cout << "------\n";
    }

    //my_sort
    vector <int> indices(n);
    for (int i = 0; i < n; ++i) indices[i] = i;

    sort(all(indices), lesha_cmp);

    vector <string> na(n);
    for (int i = 0; i < n; ++i) {
        na[i] = a[indices[i]];
    }
    a = na;
    if (0) {
        for (auto i : a) {
            cout << i << "\n";
        }
    }
    d.resize(n - 1);
    rep(i, n - 1) {
        d[i] = inf;
        for (int j = 0; j < max(len(a[i]), len(a[i + 1])) * 2; j += 1) {
            if (a[i][j % len(a[i])] != a[i + 1][j % len(a[i + 1])]) {
                d[i] = j;
                break;
            }
        }
    }
    sgt.build(d);
    ll result = solve(0, n - 1);
    cout << result << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 97ms
memory: 50464kb

input:

6
P
PN
KK
N
PKK
PN

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 73ms
memory: 34144kb

input:

10
KKKNP
KNKPPKNK
KNKPP
KNKPPKN
KKKN
NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN
NNKN
NPPN
NNKNNNKNNNKNNNKNNNKNNNKNNNK
KKKNN

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 77ms
memory: 40476kb

input:

10
NNNPNNNPNNNPNNNK
KKN
NNNP
KKP
NNNPNNNPNNNPN
KKNKKNKKPN
KNNPNPNKKKNPPKNKKKNKNKKNKPPPNKKPKP
KKPK
KKNKKNK
KKPKKN

output:

3

result:

ok 1 number(s): "3"

Test #4:

score: 0
Accepted
time: 79ms
memory: 44620kb

input:

10
K
PPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNP
PPKP
PPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPK
P
K
N
P
PPPNN
N

output:

25

result:

ok 1 number(s): "25"

Test #5:

score: 0
Accepted
time: 64ms
memory: 37112kb

input:

10
NPNKP
NNNNKNNNNPP
PPKPNNNNPNKKKN
NPNKPNP
NNNNKN
NNNNK
NKNPKKPNPKKNPNKN
NKNPKKPNPKKNPNK
NKNPKKPNPKKNP
NPNKPNPN

output:

30

result:

ok 1 number(s): "30"

Test #6:

score: 0
Accepted
time: 85ms
memory: 48036kb

input:

10
KPKKPKKPKKPKKP
KPKKPKKPKKPKKPKNK
PNPNP
KPK
PN
NPNPNNPNPNK
NKKPKKPKPPKKPKKKKPKNKPPKPPNKNP
NPNPNNP
PNPNPK
NPNPN

output:

39

result:

ok 1 number(s): "39"

Test #7:

score: 0
Accepted
time: 55ms
memory: 35324kb

input:

4
KKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPK
NN
KKP
KKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKNK

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 71ms
memory: 33980kb

input:

7
KPKN
KPKNKPKNKPKNKPKK
NKPPNNNPKKNN
KPPKPKPPKPKPPKPKPPKPKPP
KPKNKPKNKPKNKP
KPPKP
KPPKPKPPKPKPPKPKPPKPKPPKPN

output:

2

result:

ok 1 number(s): "2"

Test #9:

score: 0
Accepted
time: 75ms
memory: 37928kb

input:

10
NKNNKNKN
KPKN
PKPN
PNNNNNNKKNNPNNKNPPKPPNPNPPKKKPNNNPNPKKNK
PKPNPKP
PKPNPK
KPKNKP
NKNNKNKNNKNPN
KPKNKPK
NKNNK

output:

39

result:

ok 1 number(s): "39"

Test #10:

score: 0
Accepted
time: 40ms
memory: 27608kb

input:

300
NKNPNK
NKKNKK
KPPNPN
KKPNKNK
PKKNPKP
KPKPPPN
NNKPPNN
NPKPPKN
KNNKKPK
PPPNPKK
NKPKNP
KPKNNPP
NNPKNP
PNPPPKN
PKKPNP
PPNNKK
PKNKNK
PKNPNK
NKNPNPP
KNKNNPN
NKPPPPK
NNPPKKN
KNKKNPK
KKNNPKN
PPPKNK
NPPPPPP
NKKPKPP
KNKNPPK
KPKPNNK
NPNNKN
PNPNKP
PNPKKP
KKKKPKN
NNNKNPK
NPNKPNK
NNNKNK
PPKKNKP
NNNKPPK
KPNKPP...

output:

1102940

result:

ok 1 number(s): "1102940"

Test #11:

score: 0
Accepted
time: 165ms
memory: 89956kb

input:

91
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKN
KKKKKKKKP
PNPKPPNP
KKKN
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKN
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKP
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKN
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKP
KKKKKKKKKKKKKKKKKKKKKKN
KKKKKKKN
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKP
KKKKKKKKKKKKP
...

output:

2151

result:

ok 1 number(s): "2151"

Test #12:

score: -100
Wrong Answer
time: 154ms
memory: 82504kb

input:

72
PKPPKPPKPPKPPKPPN
PKP
NNNNNK
NPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNP
NNPNNPNNPNNPNNPNNK
NP
PPPPPPN
PKPPKPPKPPKPPKPP
PPPPKPP
PPK
NNNNNPP
NNNNPNNNNPNNNNPN
KPNNNKKPPKPKKNPPKKNNKPKPKPKPPPKPPKPNNKPPKPPPNNNKKNNPKKKKKN...

output:

14791

result:

wrong answer 1st numbers differ - expected: '14794', found: '14791'