結果

問題 No.1222 -101
ユーザー 钟梓皓钟梓皓
提出日時 2020-09-04 22:45:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 3,860 bytes
コンパイル時間 1,926 ms
コンパイル使用メモリ 204,884 KB
実行使用メモリ 17,664 KB
最終ジャッジ日時 2024-05-05 03:20:12
合計ジャッジ時間 4,454 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,912 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 4 ms
6,912 KB
testcase_03 AC 4 ms
6,784 KB
testcase_04 AC 4 ms
6,912 KB
testcase_05 AC 4 ms
6,784 KB
testcase_06 AC 4 ms
6,784 KB
testcase_07 AC 4 ms
6,784 KB
testcase_08 AC 5 ms
6,912 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 10 ms
13,824 KB
testcase_11 AC 8 ms
13,952 KB
testcase_12 AC 67 ms
15,104 KB
testcase_13 AC 66 ms
14,976 KB
testcase_14 AC 67 ms
14,976 KB
testcase_15 AC 38 ms
14,336 KB
testcase_16 AC 51 ms
14,848 KB
testcase_17 AC 62 ms
14,848 KB
testcase_18 AC 37 ms
14,464 KB
testcase_19 AC 44 ms
14,464 KB
testcase_20 AC 56 ms
14,720 KB
testcase_21 AC 53 ms
14,720 KB
testcase_22 AC 4 ms
7,040 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 4 ms
7,040 KB
testcase_25 AC 4 ms
6,912 KB
testcase_26 AC 5 ms
7,040 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 4 ms
6,912 KB
testcase_29 AC 4 ms
6,912 KB
testcase_30 AC 4 ms
6,784 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 54 ms
17,600 KB
testcase_33 AC 53 ms
17,664 KB
testcase_34 AC 77 ms
12,416 KB
testcase_35 AC 80 ms
12,160 KB
testcase_36 AC 71 ms
12,288 KB
testcase_37 AC 69 ms
12,416 KB
testcase_38 AC 67 ms
12,416 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