結果

問題 No.1222 -101
ユーザー 钟梓皓钟梓皓
提出日時 2020-09-04 22:45:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 3,860 bytes
コンパイル時間 2,152 ms
コンパイル使用メモリ 202,224 KB
実行使用メモリ 18,436 KB
最終ジャッジ日時 2023-08-17 20:25:44
合計ジャッジ時間 5,145 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
17,844 KB
testcase_01 AC 3 ms
7,556 KB
testcase_02 AC 6 ms
11,752 KB
testcase_03 AC 6 ms
15,772 KB
testcase_04 AC 6 ms
11,668 KB
testcase_05 AC 7 ms
15,936 KB
testcase_06 AC 6 ms
15,996 KB
testcase_07 AC 7 ms
15,760 KB
testcase_08 AC 6 ms
15,856 KB
testcase_09 AC 3 ms
7,612 KB
testcase_10 AC 10 ms
17,104 KB
testcase_11 AC 11 ms
13,740 KB
testcase_12 AC 72 ms
18,424 KB
testcase_13 AC 71 ms
18,436 KB
testcase_14 AC 72 ms
18,360 KB
testcase_15 AC 43 ms
17,688 KB
testcase_16 AC 59 ms
17,804 KB
testcase_17 AC 70 ms
17,808 KB
testcase_18 AC 43 ms
17,676 KB
testcase_19 AC 49 ms
18,164 KB
testcase_20 AC 61 ms
17,808 KB
testcase_21 AC 59 ms
17,820 KB
testcase_22 AC 6 ms
15,788 KB
testcase_23 AC 2 ms
7,632 KB
testcase_24 AC 6 ms
15,936 KB
testcase_25 AC 6 ms
16,044 KB
testcase_26 AC 6 ms
15,788 KB
testcase_27 AC 3 ms
7,488 KB
testcase_28 AC 6 ms
11,672 KB
testcase_29 AC 6 ms
15,788 KB
testcase_30 AC 6 ms
15,812 KB
testcase_31 AC 3 ms
9,740 KB
testcase_32 AC 60 ms
17,712 KB
testcase_33 AC 59 ms
17,636 KB
testcase_34 AC 86 ms
17,912 KB
testcase_35 AC 86 ms
17,964 KB
testcase_36 AC 78 ms
18,056 KB
testcase_37 AC 78 ms
17,992 KB
testcase_38 AC 77 ms
18,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

const int N = 200010;
const int moder = int(1e9) + 7;

int l[N], r[N], p[N];
int cnt[N], zeroid[N], oneid[N];
int right[N], left[N];
int zeroleft[N];
int dp[N][2], pre1[N][2], pre2[N][2];
int power[N], invpower[N];
int fa[N * 2];

int powermod(int a, int exp){
    int ret = 1;
    for ( ; exp > 0; exp >>= 1){
        if (exp & 1){
            ret = 1ll * ret * a % moder;
        }
        a = 1ll * a * a % moder;
    }
    return ret;
}

int find(int u){
    return fa[u] == u ? u : (fa[u] = find(fa[u]));
}

void unite(int u, int v){
    u = find(u), v = find(v);
    if (u == v){
        return;
    }
    fa[u] = v;
}

int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; ++ i){
        scanf("%d%d%d", &l[i], &r[i], &p[i]);
        if (p[i]){
            ++ cnt[l[i]];
            -- cnt[r[i] + 1];
        }
    }
    for (int i = 1; i <= n; ++ i){
        cnt[i] += cnt[i - 1];
    }
    int id = 0;
    for (int i = 1; i <= n; ++ i){
        if (!cnt[i]){
            zeroid[i] = ++ id;
        }
    }
    for (int i = n; i >= 1; -- i){
        right[i] = cnt[i] == 0 ? zeroid[i] : right[i + 1];
    }
    for (int i = 1; i <= n; ++ i){
        left[i] = cnt[i] == 0 ? zeroid[i] : left[i - 1];
    }
    for (int i = 0; i < m; ++ i){
        if (!p[i]){
            int ll = right[l[i]];
            int rr = left[r[i]];
            if (ll == 0 || rr == 0 || ll > rr){
                puts("0");
                return 0;
            }
            zeroleft[rr] = ll;
        }
    }
    int max = 0;
    for (int i = 1; i <= id; ++ i){
        if (zeroleft[i] <= max){
            zeroleft[i] = 0;
        }
        max = std::max(max, zeroleft[i]);
    }
    power[0] = 1, power[1] = 2ll * powermod(3, moder - 2) % moder;
    invpower[0] = 1, invpower[1] = 3ll * powermod(2, moder - 2) % moder;
    for (int i = 2; i < N; ++ i){
        power[i] = 1ll * power[i - 1] * power[1] % moder;
        invpower[i] = 1ll * invpower[i - 1] * invpower[1] % moder;
    }
    dp[0][0] = pre1[0][0] = pre2[0][0] = 1;
    for (int i = 1; i <= id; ++ i){
        if (zeroleft[i]){
            int len = i - zeroleft[i] + 1;
            for (int j = 0; j < 2; ++ j){
                dp[i][j] = 1ll * pre1[zeroleft[i] - 1][j ^ 1] * power[len] % moder;
                dp[i][j] = (dp[i][j] + 1ll * (pre2[i - 1][j ^ 1] - pre2[zeroleft[i] - 1][j ^ 1]) * power[i]) % moder;
                dp[i][j] += dp[i][j] < 0 ? moder : 0;
            }
        }
        for (int j = 0; j < 2; ++ j){
            pre1[i][j] = (pre1[i - 1][j] + dp[i][j]) % moder;
            pre2[i][j] = (pre2[i - 1][j] + 1ll * invpower[i] * dp[i][j]) % moder;
        }
    }
    int ans = (1ll * (pre1[id][0] - pre1[id][1]) * powermod(3, id)) % moder;
    ans += ans < 0 ? moder : 0;
    id = 0;
    for (int i = 1; i <= n; ++ i){
        if (cnt[i]){
            oneid[i] = ++ id;
        }
    }
    for (int i = 0; i < 2 * N; ++ i){
        fa[i] = i;
    }
    int nn = n;
    n = id + 1;
    for (int i = 0; i < m; ++ i){
        if (!p[i]){
            continue;
        }
        l[i] = oneid[l[i]];
        r[i] = oneid[r[i]];
        if (p[i] == 1){
            if (find(l[i] - 1) == find(r[i] + n)){
                puts("0");
                return 0;
            }
            unite(l[i] - 1, r[i]);
            unite(l[i] - 1 + n, r[i] + n);
        }
        else if (p[i] == -1){
            if (find(l[i] - 1) == find(r[i])){
                puts("0");
                return 0;
            }
            unite(l[i] - 1, r[i] + n);
            unite(l[i] - 1 + n, r[i]);
        }
    }
    int ccnt = 0;
    for (int i = 0; i < 2 * n; ++ i){
        if (find(i) == i){
            ++ ccnt;
        }
    }
    ccnt /= 2;
    -- ccnt;
    ans = 1ll * ans * powermod(2, ccnt) % moder;
    printf("%d\n", ans);
    return 0;
}
0