Submission #2558628


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Main {
    int n, m;
    int[][] cond;
    int MOD = 1000000007;

    public static void main(String args[]) {
        new Main().run();
    }

    void run() {
        FastReader sc = new FastReader();
        n = sc.nextInt();
        m = sc.nextInt();
        cond = new int[m][3];
        for (int i = 0; i < m; i++) {
            cond[i][0] = sc.nextInt() - 1;
            cond[i][1] = sc.nextInt() - 1;
            cond[i][2] = sc.nextInt();
        }
        Arrays.sort(cond, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        solve();
    }

    void solve() {
        long[][] dp = new long[n + 1][n + 1];
        dp[0][0] = 1;
        int p = 0;
        // i-th character
        for (int i = 0; i < n; i++) {
            long[][] ndp = new long[n + 1][n + 1];
            int[] maxs = new int[4];
            int[] mins = new int[4];
            Arrays.fill(maxs, -1);
            Arrays.fill(mins, 9999);
            while (p < m && cond[p][1] <= i) {
                maxs[cond[p][2]] = Math.max(maxs[cond[p][2]], cond[p][1] - cond[p][0] + 1);
                mins[cond[p][2]] = Math.min(mins[cond[p][2]], cond[p][1] - cond[p][0] + 1);
                p++;
            }
            for (int j = 0; j <= n; j++) {
                for (int k = j; k <= n; k++) {
                    if (dp[j][k] == 0) {
                        continue;
                    }
                    {
                        int nj = j + 1;
                        int nk = k + 1;
                        if (maxs[1] == -1 || maxs[1] <= nj) {
                            if (maxs[2] == -1 || nj < mins[2] && maxs[2] <= nk) {
                                if (maxs[3] == -1 || nk < mins[3]) {
                                    ndp[nj][nk] += dp[j][k];
                                    if (ndp[nj][nk] >= MOD) {
                                        ndp[nj][nk] -= MOD;
                                    }
                                }
                            }
                        }
                    }
                    {
                        int nj = 1;
                        int nk = k + 1;
                        if (maxs[1] == -1 || maxs[1] <= nj) {
                            if (maxs[2] == -1 || nj < mins[2] && maxs[2] <= nk) {
                                if (maxs[3] == -1 || nk < mins[3]) {
                                    ndp[nj][nk] += dp[j][k];
                                    if (ndp[nj][nk] >= MOD) {
                                        ndp[nj][nk] -= MOD;
                                    }
                                }
                            }
                        }
                    }
                    {
                        int nj = 1;
                        int nk = j + 1;
                        if (maxs[1] == -1 || maxs[1] <= nj) {
                            if (maxs[2] == -1 || nj < mins[2] && maxs[2] <= nk) {
                                if (maxs[3] == -1 || nk < mins[3]) {
                                    ndp[nj][nk] += dp[j][k];
                                    if (ndp[nj][nk] >= MOD) {
                                        ndp[nj][nk] -= MOD;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            dp = ndp;
        }
        long ans = 0;
        for (int j = 0; j <= n; j++) {
            for (int k = j; k <= n; k++) {
                ans += dp[j][k];
            }
        }
        System.out.println(ans % MOD);
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}

Submission Info

Submission Time
Task E - RGB Sequence
User ynish
Language Java8 (OpenJDK 1.8.0)
Score 800
Code Size 5214 Byte
Status AC
Exec Time 295 ms
Memory 93508 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 47
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt
Case Name Status Exec Time Memory
0_00.txt AC 73 ms 19156 KB
0_01.txt AC 80 ms 19412 KB
0_02.txt AC 72 ms 20820 KB
0_03.txt AC 77 ms 19156 KB
1_00.txt AC 269 ms 89988 KB
1_01.txt AC 253 ms 89576 KB
1_02.txt AC 195 ms 93300 KB
1_03.txt AC 252 ms 89876 KB
1_04.txt AC 235 ms 93040 KB
1_05.txt AC 192 ms 87668 KB
1_06.txt AC 194 ms 90508 KB
1_07.txt AC 191 ms 89468 KB
1_08.txt AC 293 ms 85428 KB
1_09.txt AC 193 ms 88052 KB
1_10.txt AC 191 ms 88836 KB
1_11.txt AC 190 ms 91252 KB
1_12.txt AC 245 ms 90456 KB
1_13.txt AC 280 ms 90660 KB
1_14.txt AC 195 ms 90740 KB
1_15.txt AC 269 ms 88008 KB
1_16.txt AC 272 ms 88720 KB
1_17.txt AC 235 ms 92020 KB
1_18.txt AC 193 ms 90172 KB
1_19.txt AC 193 ms 90544 KB
1_20.txt AC 256 ms 90892 KB
1_21.txt AC 195 ms 92476 KB
1_22.txt AC 194 ms 88308 KB
1_23.txt AC 235 ms 91380 KB
1_24.txt AC 226 ms 89572 KB
1_25.txt AC 295 ms 93272 KB
1_26.txt AC 189 ms 90636 KB
1_27.txt AC 196 ms 89200 KB
1_28.txt AC 233 ms 91444 KB
1_29.txt AC 282 ms 89096 KB
1_30.txt AC 192 ms 89460 KB
1_31.txt AC 282 ms 88000 KB
1_32.txt AC 241 ms 89000 KB
1_33.txt AC 196 ms 91380 KB
1_34.txt AC 190 ms 85876 KB
1_35.txt AC 192 ms 89204 KB
1_36.txt AC 224 ms 90740 KB
1_37.txt AC 248 ms 87980 KB
1_38.txt AC 194 ms 90484 KB
1_39.txt AC 283 ms 91084 KB
1_40.txt AC 284 ms 90008 KB
1_41.txt AC 195 ms 91268 KB
1_42.txt AC 234 ms 93508 KB