QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#277004#7012. Rikka with An Unnamed Templeucup-team1209#AC ✓4427ms367076kbC++204.3kb2023-12-06 14:22:302023-12-06 14:22:32

Judging History

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

  • [2023-12-06 14:22:32]
  • 评测
  • 测评结果:AC
  • 用时:4427ms
  • 内存:367076kb
  • [2023-12-06 14:22:30]
  • 提交

answer

#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
using pi = pair <int, int> ;
using ll = long long ;
cs int N = 2e5 + 5, M = 105; 

cs int mod = 1e9 + 7;
int add(int a, int b) {
    return a + b >= mod ? a + b - mod : a + b; 
}
int dec(int a, int b) {
    return a - b < 0 ? a - b + mod : a - b; 
}
int mul(int a, int b) {
    return 1ll * a * b % mod;
}

int n, w[N], c[N], m, T;
int d[N], K, A; 
vector <int> e[N], re[N];

struct dat {
    ll v;
    int c; 
    dat operator + (cs dat & x) cs {
        if(v < x.v) return x;
        if(v > x.v) return * this; 
        return {v, add(c, x.c)};
    }
    dat operator * (cs dat & x) cs {
        if(v == -1 || x.v == -1) return {-1, 0};
        return {v + x.v, mul(c, x.c)};
    }
}; 
dat dp1[N][M], dp2[N][M];
void trans(dat & x, dat y, int c) {
    if(y.v == -1) return;
    y.v += c; 
    x = x + y; 
}
dat ans[N];

dat mer(dat * a, dat * b) {
    dat sum = {-1, 0}; 
    for(int i = 0; i < K; i++) {
        sum = sum + (a[i] *  b[(A - i + K) % K]); 
    }
    return sum;  
}
namespace sgt {
#define mid ((l + r) >> 1)
cs int N = :: N << 2; 
dat t[N];
void build(int x, int l, int r) {
    t[x] = {-1, 0};
    if(l == r) return; 
    build(x << 1, l, mid);
    build(x << 1 | 1, mid + 1, r);
}
void put(int x, int l, int r, int L, int R, cs dat & v) {
    if(L <= l && r <= R) { t[x] = t[x] + v; return ; }
    if(L <= mid) put(x << 1, l, mid, L, R, v);
    if(R > mid) put(x << 1 | 1, mid + 1, r, L, R, v); 
}
dat qry(int x, int l, int r, int p) {
    if(l == r) return t[x];
    dat ans = t[x];
    if(p <= mid) ans = ans + qry(x << 1, l, mid, p);
    else ans = ans + qry(x << 1 | 1, mid + 1, r, p); 
    return ans; 
}
#undef mid 
}
void Main() {
    cin >> n >> m; 
    for(int i = 1; i <= n; i++) cin >> w[i] >> c[i];
    vector <pi> E(m + 1);
    for(int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        e[u].pb(v), ++ d[v];
        re[v].pb(u);
        E[i] = {u, v};
    }
    cin >> K >> A; 
    static int q[N], p[N]; int hd = 1, tl = 0; 
    for(int i = 1; i <= n; i++) if(d[i] == 0) q[++ tl] = i; 
    while(hd <= tl) {
        int x = q[hd ++]; 
        for(int v : e[x]) {
            if((-- d[v]) == 0) q[++ tl] = v;
        }
    }
    for(int i = 0; i <= n; i++)
    for(int j = 0; j < K; j++) dp1[i][j] = dp2[i][j] = {-1, 0};
    for(int i = 1; i <= tl; i++) p[q[i]] = i; 
    dp1[1][w[1] % K] = {c[1], 1};
    for(int i = 1; i <= tl; i++) {
        int x = q[i];
        for(int v : re[x]) {
            for(int j = 0; j < K; j++)
                trans(dp1[x][(j + w[x]) % K], dp1[v][j], c[x]);
        }
    }
    dp2[n][w[n] % K] = {c[n], 1};
    for(int i = tl; i; i--) {
        int x = q[i];
        for(int v : e[x]) {
            for(int j = 0; j < K; j++)
                trans(dp2[x][(j + w[x]) % K], dp2[v][j], c[x]);
        }
    }
    // for(int i =1; i <= tl; i++) {
    //     cout << "FFF " << q[i] <<endl;
    //     int x = q[i];
    //     for(int j = 0; j < K; j++) if(dp1[x][j].v >=0 ) cout << j <<' '<< dp1[x][j].v << ' ' << dp1[x][j].c << '\n';
    // }
    sgt :: build(1, 1, tl);
    for(int i = 1; i <= m; i++) {
        auto [u, v] = E[i];
        if(!p[u] || !p[v]) continue; 
        assert(p[u] <= p[v]);
        if(p[u] + 1 < p[v]) {
            // dat t = mer(dp1[u], dp2[v]);
            // cout << u << ' ' << v << ' ' << t.v << ' ' << t.c << '\n';
            sgt :: put(1, 1, tl, p[u] + 1, p[v] - 1, mer(dp1[u], dp2[v]));
        } 
    }
    for(int i = 1; i <= tl; i++) {
        ans[q[i]] = sgt :: qry(1, 1, tl, i);
    }
    for(int i = 1; i <= tl; i++) {
        if(q[i] == 1) break;
        ans[q[i]] = dp1[n][A];
    }
    for(int i = tl; i; i--) {
        if(q[i] == n) break;
        ans[q[i]] = dp1[n][A];
    }
    for(int i = 1; i <= n; i++) {
        if(ans[i].v == -1) {
            cout << -1 << '\n';
        }
        else {
            cout << ans[i].v << ' ' << ans[i].c << '\n';
        }
    }
    for(int i = 0; i <= n; i++) 
        e[i].clear(), re[i].clear(), q[i] = p[i] = d[i] = 0; 
}

int main() {
    #ifdef zqj
    freopen("1.in","r",stdin);
    #endif
    ios :: sync_with_stdio(0), cin.tie(0);
    cin >> T; 
    while(T--) {
        Main();
    }
    return 0; 
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
4 5
1 2
2 3
3 4
4 2
1 2
1 3
2 4
3 4
1 4
5 3

output:

-1
8 1
-1
-1

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 1544ms
memory: 24072kb

input:

840
300 2000
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1...

output:

-1
26 4598303
26 5863883
26 5015595
26 5885768
26 5825352
26 5282239
26 5677503
26 5663242
26 5549866
26 5865190
26 5748616
26 5885768
26 5833529
26 5635401
26 5885768
26 4779744
26 2604267
26 5001275
26 5885768
26 5820512
26 4314551
26 5885768
26 4334970
26 5885768
26 5712718
26 3266408
26 5807851
...

result:

ok 252000 lines

Test #3:

score: 0
Accepted
time: 4427ms
memory: 364248kb

input:

10
100000 200000
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1...

output:

-1
92971 21
93151 80261
92971 66888304
93061 1636195
92971 1273
92971 4510
93151 287907743
92881 77902034
92881 77902034
93061 152329005
92791 11621
93061 1149299
92881 77902034
93061 1375637
92881 3146874
93241 331637001
92521 2323746
92971 74019096
93241 1634966
92971 49606952
93061 67095587
93241...

result:

ok 1000000 lines

Test #4:

score: 0
Accepted
time: 2360ms
memory: 367076kb

input:

10
100000 199996
209716784 457585002
684116471 390878750
684117472 390878750
684116198 390878750
684120293 390878750
684118746 390878750
684123023 390878750
684115288 390878750
684119383 390878750
684119747 390878750
684123387 390878750
684119201 390878750
684118200 390878750
684122841 390878750
684...

output:

-1
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
1749122861 99997
17491228...

result:

ok 1000000 lines

Test #5:

score: 0
Accepted
time: 2141ms
memory: 363624kb

input:

10
97843 190591
491838654 66190718
63066994 1
888868658 1
199879445 1
999668194 1
494916788 1
599562190 1
667839593 1
312621436 1
313720346 1
118498972 1
365124510 1
555323904 1
363282628 1
958103770 1
987214655 1
250893535 1
38283294 1
453193983 1
77880110 1
703150923 1
818318041 1
999001283 1
7817...

output:

-1
648319345 887614448
648319598 695396893
648319566 645311746
648319343 887614448
648319448 695396893
648319283 645311746
648319557 852744899
648319501 938666705
648319548 917998978
648319165 852744899
648319315 938666705
648319502 731232806
648319405 731232806
648319594 852743836
648319602 9386656...

result:

ok 941270 lines