답안 #255327

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
255327 2020-07-31T17:32:24 Z model_code Fuel Station (NOI20_fuelstation) Java 11
100 / 100
2972 ms 85252 KB
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;
import java.lang.Math.*;

public class FuelStation {
    static class station implements Comparable<station> {
        public int X, A, B;
        public station() {}
        public station(int _X, int _A, int _B) {
            X = _X;
            A = _A;
            B = _B;
        }
        public int compareTo(station o) {
            if (X != o.X) return X - o.X;
            if (A != o.A) return A - o.A;
            return B - o.B;
        }
    }

    static class Pair implements Comparable<Pair> {
        public int first, second;
        public Pair() {}
        public Pair(int _first, int _second) {
            first = _first;
            second = _second;
        }
        public int compareTo(Pair o) {
            if (first != o.first) return first - o.first;
            return second - o.second;
        }
    }

    static class SegmentTree {
        private long[] V;
        private long[] LAZY;
        SegmentTree() {
            V = new long[1500010];
            LAZY = new long[1500010];
        }
        public long val(int x) { return V[x] + LAZY[x]; }
        private void push(int x) {
            if (LAZY[x] != 0) {
                LAZY[x << 1] += LAZY[x];
                LAZY[x << 1 | 1] += LAZY[x];
                V[x] += LAZY[x];
                LAZY[x] = 0;
            }
        }

        public void update(int node, int S, int E, int QS, int QE, int QV) {
            if (QS <= S && E <= QE) {
                LAZY[node] += QV;
                return;
            }
            push(node);
            int M = S + (E - S) / 2;
            if (QE <= M) update(node << 1, S, M, QS, QE, QV);
            else if (QS > M) update(node << 1 | 1, M + 1, E, QS, QE, QV);
            else {
                update(node << 1, S, M, QS, M, QV);
                update(node << 1 | 1, M + 1, E, M + 1, QE, QV);
            }
            V[node] = Math.min(val(node << 1), val(node << 1 | 1));
        }
    }

    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;
        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') {
                c = read();
            }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) {
                buffer[0] = -1;
            }
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead) {
                fillBuffer();
            }
            return buffer[bufferPointer++];
        }
    }

    public static void main(String[] args) throws IOException {
        Reader reader = new Reader();
        int N = reader.nextInt();
        int D = reader.nextInt();
        SegmentTree root = new SegmentTree();
        station[] STN = new station[300010];
        int[] NXT = new int[300010];
        ArrayList<Pair> V = new ArrayList<Pair>();
        for (int a = 1; a <= N; ++a) {
            int X = reader.nextInt();
            int A = reader.nextInt();
            int B = reader.nextInt();
            STN[a] = new station(X, A, B);
        }
        Arrays.sort(STN, 1, N + 1);
        int XPTR = 1;
        STN[N + 1] = new station(D, 0, D);
        for (int a = 1; a <= N + 1; ++a) if (STN[a].X != STN[XPTR].X)
            for (; XPTR < a; ++XPTR) NXT[XPTR] = a;
        NXT[N + 1] = N + 1;
        for (int a = 1; a <= N + 1; ++a) V.add(new Pair(STN[a].B, a));
        Collections.sort(V);
        for (int a = 0; a <= N; ++a) root.update(1, 1, N + 1, V.get(a).second, V.get(a).second, -STN[V.get(a).second].X);
        for (int a = 0; a <= N; ++a)
            root.update(1, 1, N + 1, NXT[V.get(a).second], N + 1, STN[V.get(a).second].A);
        int F = 0, BPTR = 0;
        for (int a = 0; a <= N; ++a) {
            root.update(1, 1, N + 1, 1, N + 1, V.get(a).first - F);
            if (a > 0 && V.get(a).first != F)
                for (; BPTR < a; ++BPTR)
                    root.update(1, 1, N + 1, NXT[V.get(BPTR).second], N + 1, -STN[V.get(BPTR).second].A);
            F = V.get(a).first;
            long mval = root.val(1);
            if (mval >= 0) {
                System.out.println(F - mval);
                return;
            }
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 36144 KB Output is correct
2 Correct 105 ms 36092 KB Output is correct
3 Correct 103 ms 36036 KB Output is correct
4 Correct 95 ms 35808 KB Output is correct
5 Correct 95 ms 35968 KB Output is correct
6 Correct 96 ms 36216 KB Output is correct
7 Correct 96 ms 36080 KB Output is correct
8 Correct 103 ms 36052 KB Output is correct
9 Correct 95 ms 36184 KB Output is correct
10 Correct 97 ms 36192 KB Output is correct
11 Correct 92 ms 36200 KB Output is correct
12 Correct 91 ms 36020 KB Output is correct
13 Correct 114 ms 36008 KB Output is correct
14 Correct 95 ms 36216 KB Output is correct
15 Correct 91 ms 35828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1410 ms 82716 KB Output is correct
2 Correct 1645 ms 83636 KB Output is correct
3 Correct 1863 ms 84204 KB Output is correct
4 Correct 1564 ms 84132 KB Output is correct
5 Correct 1661 ms 83872 KB Output is correct
6 Correct 1855 ms 84724 KB Output is correct
7 Correct 2115 ms 85252 KB Output is correct
8 Correct 1781 ms 83944 KB Output is correct
9 Correct 1825 ms 83004 KB Output is correct
10 Correct 1775 ms 85060 KB Output is correct
11 Correct 1551 ms 83852 KB Output is correct
12 Correct 1594 ms 83528 KB Output is correct
13 Correct 1601 ms 83912 KB Output is correct
14 Correct 1651 ms 81608 KB Output is correct
15 Correct 1667 ms 83840 KB Output is correct
16 Correct 1590 ms 83312 KB Output is correct
17 Correct 1692 ms 82960 KB Output is correct
18 Correct 1656 ms 83492 KB Output is correct
19 Correct 1782 ms 83516 KB Output is correct
20 Correct 1744 ms 83616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 35984 KB Output is correct
2 Correct 253 ms 41988 KB Output is correct
3 Correct 236 ms 41780 KB Output is correct
4 Correct 287 ms 42876 KB Output is correct
5 Correct 257 ms 41436 KB Output is correct
6 Correct 240 ms 40896 KB Output is correct
7 Correct 258 ms 42456 KB Output is correct
8 Correct 100 ms 35996 KB Output is correct
9 Correct 107 ms 36064 KB Output is correct
10 Correct 97 ms 35972 KB Output is correct
11 Correct 97 ms 35936 KB Output is correct
12 Correct 95 ms 35912 KB Output is correct
13 Correct 99 ms 35860 KB Output is correct
14 Correct 292 ms 44268 KB Output is correct
15 Correct 270 ms 43800 KB Output is correct
16 Correct 322 ms 44904 KB Output is correct
17 Correct 257 ms 43008 KB Output is correct
18 Correct 323 ms 44276 KB Output is correct
19 Correct 293 ms 42568 KB Output is correct
20 Correct 202 ms 38540 KB Output is correct
21 Correct 181 ms 37888 KB Output is correct
22 Correct 360 ms 43468 KB Output is correct
23 Correct 245 ms 42220 KB Output is correct
24 Correct 271 ms 42852 KB Output is correct
25 Correct 239 ms 42016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 35984 KB Output is correct
2 Correct 253 ms 41988 KB Output is correct
3 Correct 236 ms 41780 KB Output is correct
4 Correct 287 ms 42876 KB Output is correct
5 Correct 257 ms 41436 KB Output is correct
6 Correct 240 ms 40896 KB Output is correct
7 Correct 258 ms 42456 KB Output is correct
8 Correct 100 ms 35996 KB Output is correct
9 Correct 107 ms 36064 KB Output is correct
10 Correct 97 ms 35972 KB Output is correct
11 Correct 97 ms 35936 KB Output is correct
12 Correct 95 ms 35912 KB Output is correct
13 Correct 99 ms 35860 KB Output is correct
14 Correct 292 ms 44268 KB Output is correct
15 Correct 270 ms 43800 KB Output is correct
16 Correct 322 ms 44904 KB Output is correct
17 Correct 257 ms 43008 KB Output is correct
18 Correct 323 ms 44276 KB Output is correct
19 Correct 293 ms 42568 KB Output is correct
20 Correct 202 ms 38540 KB Output is correct
21 Correct 181 ms 37888 KB Output is correct
22 Correct 360 ms 43468 KB Output is correct
23 Correct 245 ms 42220 KB Output is correct
24 Correct 271 ms 42852 KB Output is correct
25 Correct 239 ms 42016 KB Output is correct
26 Correct 97 ms 35932 KB Output is correct
27 Correct 2844 ms 83364 KB Output is correct
28 Correct 2671 ms 84444 KB Output is correct
29 Correct 2449 ms 84596 KB Output is correct
30 Correct 2348 ms 83820 KB Output is correct
31 Correct 2489 ms 84208 KB Output is correct
32 Correct 2708 ms 83676 KB Output is correct
33 Correct 111 ms 36096 KB Output is correct
34 Correct 107 ms 36140 KB Output is correct
35 Correct 135 ms 36136 KB Output is correct
36 Correct 106 ms 36232 KB Output is correct
37 Correct 95 ms 35896 KB Output is correct
38 Correct 166 ms 35940 KB Output is correct
39 Correct 2712 ms 83740 KB Output is correct
40 Correct 2535 ms 83916 KB Output is correct
41 Correct 2972 ms 84480 KB Output is correct
42 Correct 2754 ms 83720 KB Output is correct
43 Correct 2671 ms 83744 KB Output is correct
44 Correct 2052 ms 84280 KB Output is correct
45 Correct 1922 ms 84036 KB Output is correct
46 Correct 2215 ms 84416 KB Output is correct
47 Correct 1626 ms 83848 KB Output is correct
48 Correct 1564 ms 83808 KB Output is correct
49 Correct 2489 ms 84928 KB Output is correct
50 Correct 2102 ms 84684 KB Output is correct
51 Correct 2166 ms 84616 KB Output is correct
52 Correct 2240 ms 84356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 36144 KB Output is correct
2 Correct 105 ms 36092 KB Output is correct
3 Correct 103 ms 36036 KB Output is correct
4 Correct 95 ms 35808 KB Output is correct
5 Correct 95 ms 35968 KB Output is correct
6 Correct 96 ms 36216 KB Output is correct
7 Correct 96 ms 36080 KB Output is correct
8 Correct 103 ms 36052 KB Output is correct
9 Correct 95 ms 36184 KB Output is correct
10 Correct 97 ms 36192 KB Output is correct
11 Correct 92 ms 36200 KB Output is correct
12 Correct 91 ms 36020 KB Output is correct
13 Correct 114 ms 36008 KB Output is correct
14 Correct 95 ms 36216 KB Output is correct
15 Correct 91 ms 35828 KB Output is correct
16 Correct 94 ms 35988 KB Output is correct
17 Correct 89 ms 36196 KB Output is correct
18 Correct 92 ms 36324 KB Output is correct
19 Correct 95 ms 35980 KB Output is correct
20 Correct 90 ms 36112 KB Output is correct
21 Correct 96 ms 36080 KB Output is correct
22 Correct 90 ms 36068 KB Output is correct
23 Correct 88 ms 36088 KB Output is correct
24 Correct 93 ms 36088 KB Output is correct
25 Correct 88 ms 35960 KB Output is correct
26 Correct 90 ms 36068 KB Output is correct
27 Correct 98 ms 36228 KB Output is correct
28 Correct 93 ms 36088 KB Output is correct
29 Correct 106 ms 36204 KB Output is correct
30 Correct 99 ms 36288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 36144 KB Output is correct
2 Correct 105 ms 36092 KB Output is correct
3 Correct 103 ms 36036 KB Output is correct
4 Correct 95 ms 35808 KB Output is correct
5 Correct 95 ms 35968 KB Output is correct
6 Correct 96 ms 36216 KB Output is correct
7 Correct 96 ms 36080 KB Output is correct
8 Correct 103 ms 36052 KB Output is correct
9 Correct 95 ms 36184 KB Output is correct
10 Correct 97 ms 36192 KB Output is correct
11 Correct 92 ms 36200 KB Output is correct
12 Correct 91 ms 36020 KB Output is correct
13 Correct 114 ms 36008 KB Output is correct
14 Correct 95 ms 36216 KB Output is correct
15 Correct 91 ms 35828 KB Output is correct
16 Correct 102 ms 35984 KB Output is correct
17 Correct 253 ms 41988 KB Output is correct
18 Correct 236 ms 41780 KB Output is correct
19 Correct 287 ms 42876 KB Output is correct
20 Correct 257 ms 41436 KB Output is correct
21 Correct 240 ms 40896 KB Output is correct
22 Correct 258 ms 42456 KB Output is correct
23 Correct 100 ms 35996 KB Output is correct
24 Correct 107 ms 36064 KB Output is correct
25 Correct 97 ms 35972 KB Output is correct
26 Correct 97 ms 35936 KB Output is correct
27 Correct 95 ms 35912 KB Output is correct
28 Correct 99 ms 35860 KB Output is correct
29 Correct 292 ms 44268 KB Output is correct
30 Correct 270 ms 43800 KB Output is correct
31 Correct 322 ms 44904 KB Output is correct
32 Correct 257 ms 43008 KB Output is correct
33 Correct 323 ms 44276 KB Output is correct
34 Correct 293 ms 42568 KB Output is correct
35 Correct 202 ms 38540 KB Output is correct
36 Correct 181 ms 37888 KB Output is correct
37 Correct 360 ms 43468 KB Output is correct
38 Correct 245 ms 42220 KB Output is correct
39 Correct 271 ms 42852 KB Output is correct
40 Correct 239 ms 42016 KB Output is correct
41 Correct 96 ms 36080 KB Output is correct
42 Correct 261 ms 41860 KB Output is correct
43 Correct 258 ms 42724 KB Output is correct
44 Correct 242 ms 42260 KB Output is correct
45 Correct 237 ms 42556 KB Output is correct
46 Correct 226 ms 42280 KB Output is correct
47 Correct 242 ms 43164 KB Output is correct
48 Correct 92 ms 36028 KB Output is correct
49 Correct 98 ms 36088 KB Output is correct
50 Correct 96 ms 36112 KB Output is correct
51 Correct 95 ms 36456 KB Output is correct
52 Correct 96 ms 35924 KB Output is correct
53 Correct 91 ms 36112 KB Output is correct
54 Correct 323 ms 43912 KB Output is correct
55 Correct 285 ms 43168 KB Output is correct
56 Correct 304 ms 44508 KB Output is correct
57 Correct 277 ms 42724 KB Output is correct
58 Correct 256 ms 44488 KB Output is correct
59 Correct 232 ms 43120 KB Output is correct
60 Correct 166 ms 37800 KB Output is correct
61 Correct 179 ms 38268 KB Output is correct
62 Correct 235 ms 41916 KB Output is correct
63 Correct 248 ms 40788 KB Output is correct
64 Correct 318 ms 44228 KB Output is correct
65 Correct 247 ms 43272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 36144 KB Output is correct
2 Correct 105 ms 36092 KB Output is correct
3 Correct 103 ms 36036 KB Output is correct
4 Correct 95 ms 35808 KB Output is correct
5 Correct 95 ms 35968 KB Output is correct
6 Correct 96 ms 36216 KB Output is correct
7 Correct 96 ms 36080 KB Output is correct
8 Correct 103 ms 36052 KB Output is correct
9 Correct 95 ms 36184 KB Output is correct
10 Correct 97 ms 36192 KB Output is correct
11 Correct 92 ms 36200 KB Output is correct
12 Correct 91 ms 36020 KB Output is correct
13 Correct 114 ms 36008 KB Output is correct
14 Correct 95 ms 36216 KB Output is correct
15 Correct 91 ms 35828 KB Output is correct
16 Correct 1410 ms 82716 KB Output is correct
17 Correct 1645 ms 83636 KB Output is correct
18 Correct 1863 ms 84204 KB Output is correct
19 Correct 1564 ms 84132 KB Output is correct
20 Correct 1661 ms 83872 KB Output is correct
21 Correct 1855 ms 84724 KB Output is correct
22 Correct 2115 ms 85252 KB Output is correct
23 Correct 1781 ms 83944 KB Output is correct
24 Correct 1825 ms 83004 KB Output is correct
25 Correct 1775 ms 85060 KB Output is correct
26 Correct 1551 ms 83852 KB Output is correct
27 Correct 1594 ms 83528 KB Output is correct
28 Correct 1601 ms 83912 KB Output is correct
29 Correct 1651 ms 81608 KB Output is correct
30 Correct 1667 ms 83840 KB Output is correct
31 Correct 1590 ms 83312 KB Output is correct
32 Correct 1692 ms 82960 KB Output is correct
33 Correct 1656 ms 83492 KB Output is correct
34 Correct 1782 ms 83516 KB Output is correct
35 Correct 1744 ms 83616 KB Output is correct
36 Correct 102 ms 35984 KB Output is correct
37 Correct 253 ms 41988 KB Output is correct
38 Correct 236 ms 41780 KB Output is correct
39 Correct 287 ms 42876 KB Output is correct
40 Correct 257 ms 41436 KB Output is correct
41 Correct 240 ms 40896 KB Output is correct
42 Correct 258 ms 42456 KB Output is correct
43 Correct 100 ms 35996 KB Output is correct
44 Correct 107 ms 36064 KB Output is correct
45 Correct 97 ms 35972 KB Output is correct
46 Correct 97 ms 35936 KB Output is correct
47 Correct 95 ms 35912 KB Output is correct
48 Correct 99 ms 35860 KB Output is correct
49 Correct 292 ms 44268 KB Output is correct
50 Correct 270 ms 43800 KB Output is correct
51 Correct 322 ms 44904 KB Output is correct
52 Correct 257 ms 43008 KB Output is correct
53 Correct 323 ms 44276 KB Output is correct
54 Correct 293 ms 42568 KB Output is correct
55 Correct 202 ms 38540 KB Output is correct
56 Correct 181 ms 37888 KB Output is correct
57 Correct 360 ms 43468 KB Output is correct
58 Correct 245 ms 42220 KB Output is correct
59 Correct 271 ms 42852 KB Output is correct
60 Correct 239 ms 42016 KB Output is correct
61 Correct 97 ms 35932 KB Output is correct
62 Correct 2844 ms 83364 KB Output is correct
63 Correct 2671 ms 84444 KB Output is correct
64 Correct 2449 ms 84596 KB Output is correct
65 Correct 2348 ms 83820 KB Output is correct
66 Correct 2489 ms 84208 KB Output is correct
67 Correct 2708 ms 83676 KB Output is correct
68 Correct 111 ms 36096 KB Output is correct
69 Correct 107 ms 36140 KB Output is correct
70 Correct 135 ms 36136 KB Output is correct
71 Correct 106 ms 36232 KB Output is correct
72 Correct 95 ms 35896 KB Output is correct
73 Correct 166 ms 35940 KB Output is correct
74 Correct 2712 ms 83740 KB Output is correct
75 Correct 2535 ms 83916 KB Output is correct
76 Correct 2972 ms 84480 KB Output is correct
77 Correct 2754 ms 83720 KB Output is correct
78 Correct 2671 ms 83744 KB Output is correct
79 Correct 2052 ms 84280 KB Output is correct
80 Correct 1922 ms 84036 KB Output is correct
81 Correct 2215 ms 84416 KB Output is correct
82 Correct 1626 ms 83848 KB Output is correct
83 Correct 1564 ms 83808 KB Output is correct
84 Correct 2489 ms 84928 KB Output is correct
85 Correct 2102 ms 84684 KB Output is correct
86 Correct 2166 ms 84616 KB Output is correct
87 Correct 2240 ms 84356 KB Output is correct
88 Correct 94 ms 35988 KB Output is correct
89 Correct 89 ms 36196 KB Output is correct
90 Correct 92 ms 36324 KB Output is correct
91 Correct 95 ms 35980 KB Output is correct
92 Correct 90 ms 36112 KB Output is correct
93 Correct 96 ms 36080 KB Output is correct
94 Correct 90 ms 36068 KB Output is correct
95 Correct 88 ms 36088 KB Output is correct
96 Correct 93 ms 36088 KB Output is correct
97 Correct 88 ms 35960 KB Output is correct
98 Correct 90 ms 36068 KB Output is correct
99 Correct 98 ms 36228 KB Output is correct
100 Correct 93 ms 36088 KB Output is correct
101 Correct 106 ms 36204 KB Output is correct
102 Correct 99 ms 36288 KB Output is correct
103 Correct 96 ms 36080 KB Output is correct
104 Correct 261 ms 41860 KB Output is correct
105 Correct 258 ms 42724 KB Output is correct
106 Correct 242 ms 42260 KB Output is correct
107 Correct 237 ms 42556 KB Output is correct
108 Correct 226 ms 42280 KB Output is correct
109 Correct 242 ms 43164 KB Output is correct
110 Correct 92 ms 36028 KB Output is correct
111 Correct 98 ms 36088 KB Output is correct
112 Correct 96 ms 36112 KB Output is correct
113 Correct 95 ms 36456 KB Output is correct
114 Correct 96 ms 35924 KB Output is correct
115 Correct 91 ms 36112 KB Output is correct
116 Correct 323 ms 43912 KB Output is correct
117 Correct 285 ms 43168 KB Output is correct
118 Correct 304 ms 44508 KB Output is correct
119 Correct 277 ms 42724 KB Output is correct
120 Correct 256 ms 44488 KB Output is correct
121 Correct 232 ms 43120 KB Output is correct
122 Correct 166 ms 37800 KB Output is correct
123 Correct 179 ms 38268 KB Output is correct
124 Correct 235 ms 41916 KB Output is correct
125 Correct 248 ms 40788 KB Output is correct
126 Correct 318 ms 44228 KB Output is correct
127 Correct 247 ms 43272 KB Output is correct
128 Correct 98 ms 36092 KB Output is correct
129 Correct 1928 ms 83608 KB Output is correct
130 Correct 2028 ms 84056 KB Output is correct
131 Correct 2427 ms 83676 KB Output is correct
132 Correct 2244 ms 84204 KB Output is correct
133 Correct 2060 ms 84296 KB Output is correct
134 Correct 2160 ms 83236 KB Output is correct
135 Correct 90 ms 36216 KB Output is correct
136 Correct 94 ms 36300 KB Output is correct
137 Correct 92 ms 36088 KB Output is correct
138 Correct 89 ms 36212 KB Output is correct
139 Correct 87 ms 36052 KB Output is correct
140 Correct 88 ms 36056 KB Output is correct
141 Correct 2750 ms 83776 KB Output is correct
142 Correct 2920 ms 83424 KB Output is correct
143 Correct 2483 ms 83904 KB Output is correct
144 Correct 2589 ms 83860 KB Output is correct
145 Correct 2697 ms 83664 KB Output is correct
146 Correct 2816 ms 84388 KB Output is correct
147 Correct 2290 ms 84288 KB Output is correct
148 Correct 2454 ms 84020 KB Output is correct
149 Correct 1969 ms 83992 KB Output is correct
150 Correct 1314 ms 82672 KB Output is correct
151 Correct 1451 ms 83760 KB Output is correct
152 Correct 2273 ms 85084 KB Output is correct
153 Correct 2232 ms 84904 KB Output is correct
154 Correct 2611 ms 84276 KB Output is correct
155 Correct 2305 ms 83984 KB Output is correct