QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#644028#9429. SubarraypropaneAC ✓594ms59412kbC++205.0kb2024-10-16 10:08:112024-10-16 10:08:11

Judging History

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

  • [2024-10-16 10:08:11]
  • 评测
  • 测评结果:AC
  • 用时:594ms
  • 内存:59412kb
  • [2024-10-16 10:08:11]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
using LL = long long;
const int maxn = 2e6 + 5, mod = 998244353, G = 3, Gi = 332748118;

int qpow(int a, int b, int mod){
    int res = 1;
    while (b){
        if (b & 1) res = 1LL * res * a % mod;
        a = 1LL * a * a % mod;
        b >>= 1;
    }
    return res;
}

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

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

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

int inv[(1 << 21) + 5], fact[(1 << 21) + 5], invfact[(1 << 21) + 5];
void init(int n){
    inv[1] = 1;
    for(int i = 2; i <= n; i++)
        inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod; 
    fact[0] = invfact[0] = 1;
    for(int i = 1; i <= n; i++)
        fact[i] = mul(fact[i - 1], i);
    invfact[n] = qpow(fact[n], mod - 2, mod);
    for(int i = n - 1; i >= 1; i--)
        invfact[i] = mul(invfact[i + 1], i + 1);  
}

int C(int a, int b){
    if (a < 0 || b < 0 || a < b) return 0;
    return mul(mul(fact[a], invfact[b]), invfact[a - b]);
}

namespace NTT{
    vector<int> Omega(int L){
        int wn = qpow(G, mod / L, mod);
        vector<int> w(L); 
        w[L >> 1] = 1;
        for(int i = L / 2 + 1; i < L; i++) w[i] = mul(w[i - 1], wn);
        for(int i = L / 2 - 1; i >= 1; i--) w[i] = w[i << 1];
        return w;
    }
    auto W = Omega(1 << 21);

    void DIF(int *a, int n) {
        for(int k = n >> 1; k; k >>= 1)
            for(int i = 0, y; i < n; i += k << 1)
                for(int j = 0; j < k; j++){
                    y = a[i + j + k], a[i + j + k] = mul(a[i + j] - y + mod, W[k + j]), 
                    add(a[i + j], y);
                }
    }

    void IDIT(int *a, int n) {
        for (int k = 1; k < n; k <<= 1)
            for (int i = 0, x, y; i < n; i += k << 1)
                for(int j = 0; j < k; j++){
                    x = a[i + j], y = mul(a[i + j + k], W[k + j]),
                    a[i + j + k] = x - y < 0 ? x - y + mod : x - y;
                    add(a[i + j], y);
                }
        int inv = mod - (mod - 1) / n;
        for(int i = 0; i < n; i++) a[i] = mul(a[i], inv);
        reverse(a + 1, a + n);
    }
}

using Poly = std::vector<int>;

void DFT(Poly &a){
    NTT::DIF(a.data(), a.size());
}

void IDFT(Poly &a){
    NTT::IDIT(a.data(), a.size());
}

int normed(int n) { 
    return n == 1 ? 1 : (1 << (32 - __builtin_clz(n - 1))); 
}

void norm(Poly &a) { 
    if (!a.empty()) a.resize(normed((int)a.size()), 0); 
}

void dot(Poly &a, Poly &b) {
    for(int i = 0; i < a.size(); i++)
        a[i] = mul(a[i], b[i]);
}

Poly operator*(Poly a, Poly b) {
    int n = a.size() + b.size() - 1, L = normed(n);
    if (a.size() <= 8 || b.size() <= 8) {
        Poly c(n);
        for(int i = 0; i < a.size(); i++)
            for(int j = 0; j < b.size(); j++)
                c[i + j] = (c[i + j] + 1LL * a[i] * b[j]) % mod;
        return c;
    }
    a.resize(L), b.resize(L);
    DFT(a), DFT(b), dot(a, b), IDFT(a);
    return a.resize(n), a;
}

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        map<int, vector<int> > mp;
        for(int i = 1; i <= n; i++){
            int x;
            cin >> x;
            mp[x].push_back(i);
        }

        set<int> s;
        for(int i = 0; i <= n + 1; i++){
            s.insert(i);
        }
        vector<int> c(n + 1);
        for(auto &[_, v] : mp){

            for(auto x : v) s.erase(x);

            for(int i = 0; i < v.size(); i++){
                int j = i;

                auto check = [&](){
                    if (j + 1 >= v.size()) return false;
                    auto it = s.lower_bound(v[j]);
                    if (it != s.end() and *it < v[j + 1]) return false;
                    return true;
                };

                while(check()) j++;

                int pre = *prev(s.lower_bound(v[i]));
                int nxt = *s.lower_bound(v[j]);

                const int m = j - i + 1;

                vector<int> a(m + 1), b(m + 1);
                for(int k = i; k <= j; k++){
                    a[m - (k - i)] = (k == i ? v[k] - pre : v[k] - v[k - 1]);
                }
                for(int k = i; k <= j; k++){
                    b[k - i] = (k == j ? nxt - v[k] : v[k + 1] - v[k]);
                }

                auto p = a * b;
                for(int k = 1; k <= m; k++){
                    add(c[k], p[k + m - 1]);
                }
                i = j;
            }
        }

        int ans = 0;
        for(int i = 1; i <= n; i++) add(ans, mul(i, mul(c[i], c[i])));
        cout << ans << '\n';
    }

}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 11084kb

input:

3
11
1 1 2 1 2 2 3 3 2 3 1
3
2024 5 26
3
1000000000 1000000000 1000000000

output:

2564
36
20

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 184ms
memory: 17144kb

input:

2522
12
642802746 634074578 642802746 634074578 642802746 634074578 634074578 642802746 740396295 634074578 740396295 634074578
16
305950462 400920468 400920468 305950462 400920468 305950462 400920468 400920468 400920468 400920468 305950462 305950462 400920468 305950462 305950462 305950462
2
4405082...

output:

3610
7545
9
1
50
1006
16170
5972
3117
540
540
4417
12885
336
3185
83
9272
27
1794
2776
1793
196
27
1377
8783
19723
5385
1864
3478
7101
1
431
825
4534
9900
162
21644
6
36
14088
306
9
57
1719
72
9
4637
68
16583
17701
19390
16282
5440
1
6
1716
19541
3823
2033
24
825
429
1911
11787
11388
12255
12175
126...

result:

ok 2522 lines

Test #3:

score: 0
Accepted
time: 362ms
memory: 33884kb

input:

1
400000
860350786 641009859 939887597 54748096 641009859 860350786 710156469 985188306 476927808 641009859 985188306 322595515 322595515 973764525 54748096 939887597 54748096 476927808 588586447 669240390 54748096 476927808 669240390 804928248 669240390 75475634 804928248 669240390 985188306 754756...

output:

300998364

result:

ok single line: '300998364'

Test #4:

score: 0
Accepted
time: 530ms
memory: 33884kb

input:

1
400000
860422965 880311831 389867323 711027909 603801945 977217669 127611088 468302420 100563882 896362064 321065270 937796491 106388135 679974087 799365054 508500258 155801089 72992050 568198964 469117950 605828088 147285088 931759705 335154243 123769214 717250374 123769214 588271814 193910044 58...

output:

642490751

result:

ok single line: '642490751'

Test #5:

score: 0
Accepted
time: 594ms
memory: 36260kb

input:

1
400000
489576972 624268112 792793292 261080167 299965397 570683924 43370033 865049228 160224484 597021302 799302320 154578623 616009875 817736437 422498140 177450324 576706528 701882608 322199948 469659816 265384591 886524303 331787804 922381773 642896492 36870304 922875786 328785523 506357505 778...

output:

728396411

result:

ok single line: '728396411'

Test #6:

score: 0
Accepted
time: 176ms
memory: 33228kb

input:

1
400000
79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 79866982 798...

output:

805404149

result:

ok single line: '805404149'

Test #7:

score: 0
Accepted
time: 163ms
memory: 33716kb

input:

1
400000
4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 4163012 416...

output:

871688808

result:

ok single line: '871688808'

Test #8:

score: 0
Accepted
time: 183ms
memory: 35860kb

input:

1
400000
7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 7913 19512 844861162 178869991 19512 19512 19512 19512 19512 19512 19512 135989768 19512 19512 19512 19512 19512 19512 19512 19512 220217 220217 220217 220217 220217 220217 220217 220217 2202...

output:

470566238

result:

ok single line: '470566238'

Test #9:

score: 0
Accepted
time: 201ms
memory: 33332kb

input:

1
400000
1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 2 2 1 2 1 2 1 1 2 1 2 2 1 2 1 2 2 1 1 1 1 2 1 1 1 2 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 1 2 2 1 2 2 2 1 1 2 2 1 2 2 1 1 1 1 2 2 1 1 1 1 2 1 2 2 1 2 1 1 2 2 1 1 2 2 2 1 2 2 2 2 1 2 1 2 1 1 2 2 2 1 1 1 2 1 1 1 1 2 2 2 1 1 2 2 1 2 2 1 1 1 2 1 2 2 2 1 1 2 1 2 1 1 2 2 2...

output:

188247686

result:

ok single line: '188247686'

Test #10:

score: 0
Accepted
time: 200ms
memory: 33256kb

input:

1
400000
1 1 1 1 1 2 2 1 1 1 2 2 1 2 2 1 2 1 1 2 1 1 2 1 2 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 1 1 2 2 1 2 2 1 1 1 2 1 2 1 2 2 1 2 1 2 1 1 1 2 2 1 1 2 2 2 1 1 2 2 2 2 2 2 2 1 2 2 1 2 1 1 2 1 2 1 2 1 1 1 1 2 1 1 2 2 1 1 1 2 2 1 2 1 1 1 2 1 1 2 1 1 1 1 1 2 1 1 1 2 2 1 2 2 1 1 1 2 1 1 1 1 2 1 1 1 2 1 2 1 2 2...

output:

534522621

result:

ok single line: '534522621'

Test #11:

score: 0
Accepted
time: 206ms
memory: 33292kb

input:

1
400000
2 2 1 1 1 2 1 1 2 2 1 1 1 1 1 1 1 1 2 1 2 2 1 2 2 2 2 1 1 2 1 2 1 1 2 2 2 2 1 2 1 2 1 1 1 2 2 1 2 1 1 1 2 1 1 2 2 1 1 2 2 2 2 2 2 2 1 2 2 2 1 1 1 2 2 1 2 2 2 2 2 1 1 1 1 2 1 2 1 1 1 1 2 1 1 1 2 1 1 2 2 2 1 2 2 1 2 1 2 2 1 2 2 1 1 2 1 2 1 1 2 1 1 2 1 2 1 2 2 2 1 2 2 1 1 2 1 2 2 2 1 2 1 1 2 1...

output:

315282614

result:

ok single line: '315282614'

Test #12:

score: 0
Accepted
time: 184ms
memory: 33204kb

input:

1
400000
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7...

output:

95707550

result:

ok single line: '95707550'

Test #13:

score: 0
Accepted
time: 178ms
memory: 33556kb

input:

1
400000
34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 34618927 346...

output:

9261320

result:

ok single line: '9261320'

Test #14:

score: 0
Accepted
time: 164ms
memory: 33740kb

input:

1
400000
356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 356559 3565...

output:

639541126

result:

ok single line: '639541126'

Test #15:

score: 0
Accepted
time: 167ms
memory: 35828kb

input:

1
400000
17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 17752 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 130863 141618 141618 141618...

output:

910375995

result:

ok single line: '910375995'

Test #16:

score: 0
Accepted
time: 219ms
memory: 59412kb

input:

1
400000
2331 2331 18924 21959 27236 27236 30230 36415 36415 46142 53346 53346 61467 63373 63373 74413 75997 77628 79685 79685 85664 85664 85664 85837 87221 89355 89355 101697 101697 104022 104022 107252 107424 109721 116001 116001 116001 116888 117008 119514 121717 123822 123822 123822 128935 13039...

output:

830312990

result:

ok single line: '830312990'

Test #17:

score: 0
Accepted
time: 178ms
memory: 37360kb

input:

1
400000
777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777...

output:

951683280

result:

ok single line: '951683280'

Test #18:

score: 0
Accepted
time: 171ms
memory: 33268kb

input:

1
400000
777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777...

output:

146950933

result:

ok single line: '146950933'

Test #19:

score: 0
Accepted
time: 210ms
memory: 33280kb

input:

1
400000
777 777 777 228 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 167 777 777 777 777 777 777 777 133 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777 777...

output:

351398572

result:

ok single line: '351398572'

Extra Test:

score: 0
Extra Test Passed