Submission #500735

# Submission time Handle Problem Language Result Execution time Memory
500735 2022-01-01T04:02:06 Z arujbansal Fuel Station (NOI20_fuelstation) C++17
100 / 100
301 ms 41992 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()

const int MXN = 3e5 + 5, INF = 1e18;
int N, D;
int tree[4 * MXN], lazy[4 * MXN], tree_build[MXN];
array<int, 3> A[MXN];

int merge(int x, int y) {
	return min(x, y);
}

void build(int i, int l, int r) {
	if (l == r) {
		tree[i] = tree_build[l];
		return;
	}

	int mid = (l + r) / 2;
	build(2 * i, l, mid);
	build(2 * i + 1, mid + 1, r);
	tree[i] = merge(tree[2 * i], tree[2 * i + 1]);
}

void propagate(int i, int l, int r) {
	if (lazy[i] == 0) return;

	tree[i] += lazy[i];

	if (l != r) {
		lazy[2 * i] += lazy[i];
		lazy[2 * i + 1] += lazy[i];
	}

	lazy[i] = 0;
}

void increment(int i, int l, int r, int ql, int qr, int val) {
	propagate(i, l, r);
	if (l > qr || r < ql) return;
	if (l >= ql && r <= qr) {
		lazy[i] += val;
		propagate(i, l, r);
		return;
	}

	int mid = (l + r) / 2;
	increment(2 * i, l, mid, ql, qr, val);
	increment(2 * i + 1, mid + 1, r, ql, qr, val);
	tree[i] = merge(tree[2 * i], tree[2 * i + 1]);
}

int query(int i, int l, int r, int ql, int qr) {
	propagate(i, l, r);
	if (l > qr || r < ql) return INF;
	if (l >= ql && r <= qr) return tree[i];

	int mid = (l + r) / 2;
	return merge(query(2 * i, l, mid, ql, qr), query(2 * i + 1, mid + 1, r, ql, qr));
}

void increment(int l, int r, int val) { increment(1, 0, N - 1, l, r, val); }

int query(int l, int r) { return query(1, 0, N - 1, l, r); }

void deactivate(int idx, int nxt) {
	if (nxt > N - 1) return;
	increment(nxt, N - 1, -A[idx][1]);
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> N >> D;

	for (int i = 0; i < N; i++)
		cin >> A[i][0] >> A[i][1] >> A[i][2];

	sort(A, A + N);

	A[N] = {D, 0, D};
	N++;

	vector<pair<int, int>> sorted_b;
	int fuel_sum = 0;

	int ptr = 0;

	for (int i = 0; i < N; i++) {
		while (ptr < N && A[ptr][0] != A[i][0])
			fuel_sum += A[ptr++][1];

		tree_build[i] = -A[i][0] + fuel_sum;

		
		sorted_b.emplace_back(A[i][2], i);
	}

	build(1, 0, N - 1);
	sort(all(sorted_b));

	vector<int> nxt(N, N + 2);
	for (int i = 0, j = 0; i < N; i++) {
		for (; j < N && A[i][0] == A[j][0]; j++);

		// cout << j << "\n";

		nxt[i] = j;
		// cout << i << " " << nxt[i] << "\n";
		

		while (i < j)
			nxt[i++] = j;

		i--;

		// cout << i << " " << nxt[i] << "\n";
	}

	// cout << N << "\n";

	// for (int k = 0; k < N; k++)
	// 	cout << nxt[sorted_b[k].second] << " ";

	// cout << "\n\n";

	for (int i = 0, j = 0, last = 0; i < sz(sorted_b); i++) {
		increment(0, N - 1, sorted_b[i].first - last);
		last = sorted_b[i].first;

		while (j <= i && sorted_b[j].first < sorted_b[i].first) {
			deactivate(sorted_b[j].second, nxt[sorted_b[j].second]);
			j++;
		}

		// for (int k = 0; k < N; k++)
		// 	cout << query(k, k) << " ";

		// cout << "\n\n";

		int qry = query(0, N - 1);
		if (qry >= 0) {
			cout << sorted_b[i].first - qry;
			return 0;
		}
	}

	// cout << sorted_b.back().first;
}

/*
2 15
9 1 14
9 5 1
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 316 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 0 ms 320 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 320 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 33436 KB Output is correct
2 Correct 159 ms 33396 KB Output is correct
3 Correct 185 ms 33280 KB Output is correct
4 Correct 177 ms 33368 KB Output is correct
5 Correct 176 ms 33832 KB Output is correct
6 Correct 180 ms 32780 KB Output is correct
7 Correct 180 ms 32724 KB Output is correct
8 Correct 173 ms 33396 KB Output is correct
9 Correct 182 ms 33956 KB Output is correct
10 Correct 170 ms 32676 KB Output is correct
11 Correct 173 ms 32680 KB Output is correct
12 Correct 151 ms 33424 KB Output is correct
13 Correct 168 ms 32764 KB Output is correct
14 Correct 149 ms 33416 KB Output is correct
15 Correct 144 ms 33416 KB Output is correct
16 Correct 147 ms 33976 KB Output is correct
17 Correct 164 ms 33956 KB Output is correct
18 Correct 151 ms 33472 KB Output is correct
19 Correct 145 ms 33480 KB Output is correct
20 Correct 163 ms 33328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 4 ms 1268 KB Output is correct
3 Correct 4 ms 1228 KB Output is correct
4 Correct 4 ms 1220 KB Output is correct
5 Correct 4 ms 1232 KB Output is correct
6 Correct 4 ms 1296 KB Output is correct
7 Correct 4 ms 1360 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 7 ms 1604 KB Output is correct
15 Correct 7 ms 1616 KB Output is correct
16 Correct 7 ms 1484 KB Output is correct
17 Correct 7 ms 1616 KB Output is correct
18 Correct 7 ms 1596 KB Output is correct
19 Correct 5 ms 1488 KB Output is correct
20 Correct 6 ms 1356 KB Output is correct
21 Correct 6 ms 1360 KB Output is correct
22 Correct 5 ms 1232 KB Output is correct
23 Correct 4 ms 1228 KB Output is correct
24 Correct 4 ms 1360 KB Output is correct
25 Correct 4 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 4 ms 1268 KB Output is correct
3 Correct 4 ms 1228 KB Output is correct
4 Correct 4 ms 1220 KB Output is correct
5 Correct 4 ms 1232 KB Output is correct
6 Correct 4 ms 1296 KB Output is correct
7 Correct 4 ms 1360 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 7 ms 1604 KB Output is correct
15 Correct 7 ms 1616 KB Output is correct
16 Correct 7 ms 1484 KB Output is correct
17 Correct 7 ms 1616 KB Output is correct
18 Correct 7 ms 1596 KB Output is correct
19 Correct 5 ms 1488 KB Output is correct
20 Correct 6 ms 1356 KB Output is correct
21 Correct 6 ms 1360 KB Output is correct
22 Correct 5 ms 1232 KB Output is correct
23 Correct 4 ms 1228 KB Output is correct
24 Correct 4 ms 1360 KB Output is correct
25 Correct 4 ms 1236 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 138 ms 29240 KB Output is correct
28 Correct 137 ms 28952 KB Output is correct
29 Correct 134 ms 28644 KB Output is correct
30 Correct 140 ms 28716 KB Output is correct
31 Correct 141 ms 29256 KB Output is correct
32 Correct 143 ms 28944 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 0 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 0 ms 332 KB Output is correct
39 Correct 253 ms 33740 KB Output is correct
40 Correct 268 ms 34088 KB Output is correct
41 Correct 253 ms 34272 KB Output is correct
42 Correct 252 ms 33424 KB Output is correct
43 Correct 261 ms 36616 KB Output is correct
44 Correct 174 ms 36140 KB Output is correct
45 Correct 187 ms 36308 KB Output is correct
46 Correct 153 ms 29376 KB Output is correct
47 Correct 164 ms 30696 KB Output is correct
48 Correct 167 ms 30756 KB Output is correct
49 Correct 133 ms 28512 KB Output is correct
50 Correct 135 ms 28984 KB Output is correct
51 Correct 142 ms 28972 KB Output is correct
52 Correct 147 ms 28712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 316 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 0 ms 320 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 320 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 324 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 320 KB Output is correct
29 Correct 0 ms 320 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 316 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 0 ms 320 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 320 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 4 ms 1268 KB Output is correct
18 Correct 4 ms 1228 KB Output is correct
19 Correct 4 ms 1220 KB Output is correct
20 Correct 4 ms 1232 KB Output is correct
21 Correct 4 ms 1296 KB Output is correct
22 Correct 4 ms 1360 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 7 ms 1604 KB Output is correct
30 Correct 7 ms 1616 KB Output is correct
31 Correct 7 ms 1484 KB Output is correct
32 Correct 7 ms 1616 KB Output is correct
33 Correct 7 ms 1596 KB Output is correct
34 Correct 5 ms 1488 KB Output is correct
35 Correct 6 ms 1356 KB Output is correct
36 Correct 6 ms 1360 KB Output is correct
37 Correct 5 ms 1232 KB Output is correct
38 Correct 4 ms 1228 KB Output is correct
39 Correct 4 ms 1360 KB Output is correct
40 Correct 4 ms 1236 KB Output is correct
41 Correct 0 ms 332 KB Output is correct
42 Correct 5 ms 1352 KB Output is correct
43 Correct 5 ms 1468 KB Output is correct
44 Correct 5 ms 1456 KB Output is correct
45 Correct 5 ms 1452 KB Output is correct
46 Correct 5 ms 1696 KB Output is correct
47 Correct 5 ms 1508 KB Output is correct
48 Correct 0 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 0 ms 332 KB Output is correct
51 Correct 0 ms 332 KB Output is correct
52 Correct 0 ms 332 KB Output is correct
53 Correct 0 ms 332 KB Output is correct
54 Correct 7 ms 1720 KB Output is correct
55 Correct 7 ms 1716 KB Output is correct
56 Correct 7 ms 1744 KB Output is correct
57 Correct 8 ms 1724 KB Output is correct
58 Correct 6 ms 1736 KB Output is correct
59 Correct 7 ms 1596 KB Output is correct
60 Correct 6 ms 1476 KB Output is correct
61 Correct 5 ms 1488 KB Output is correct
62 Correct 5 ms 1352 KB Output is correct
63 Correct 6 ms 1640 KB Output is correct
64 Correct 6 ms 1744 KB Output is correct
65 Correct 5 ms 1360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 316 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 328 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 0 ms 320 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 320 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 151 ms 33436 KB Output is correct
17 Correct 159 ms 33396 KB Output is correct
18 Correct 185 ms 33280 KB Output is correct
19 Correct 177 ms 33368 KB Output is correct
20 Correct 176 ms 33832 KB Output is correct
21 Correct 180 ms 32780 KB Output is correct
22 Correct 180 ms 32724 KB Output is correct
23 Correct 173 ms 33396 KB Output is correct
24 Correct 182 ms 33956 KB Output is correct
25 Correct 170 ms 32676 KB Output is correct
26 Correct 173 ms 32680 KB Output is correct
27 Correct 151 ms 33424 KB Output is correct
28 Correct 168 ms 32764 KB Output is correct
29 Correct 149 ms 33416 KB Output is correct
30 Correct 144 ms 33416 KB Output is correct
31 Correct 147 ms 33976 KB Output is correct
32 Correct 164 ms 33956 KB Output is correct
33 Correct 151 ms 33472 KB Output is correct
34 Correct 145 ms 33480 KB Output is correct
35 Correct 163 ms 33328 KB Output is correct
36 Correct 0 ms 332 KB Output is correct
37 Correct 4 ms 1268 KB Output is correct
38 Correct 4 ms 1228 KB Output is correct
39 Correct 4 ms 1220 KB Output is correct
40 Correct 4 ms 1232 KB Output is correct
41 Correct 4 ms 1296 KB Output is correct
42 Correct 4 ms 1360 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 1 ms 324 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 0 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 7 ms 1604 KB Output is correct
50 Correct 7 ms 1616 KB Output is correct
51 Correct 7 ms 1484 KB Output is correct
52 Correct 7 ms 1616 KB Output is correct
53 Correct 7 ms 1596 KB Output is correct
54 Correct 5 ms 1488 KB Output is correct
55 Correct 6 ms 1356 KB Output is correct
56 Correct 6 ms 1360 KB Output is correct
57 Correct 5 ms 1232 KB Output is correct
58 Correct 4 ms 1228 KB Output is correct
59 Correct 4 ms 1360 KB Output is correct
60 Correct 4 ms 1236 KB Output is correct
61 Correct 0 ms 332 KB Output is correct
62 Correct 138 ms 29240 KB Output is correct
63 Correct 137 ms 28952 KB Output is correct
64 Correct 134 ms 28644 KB Output is correct
65 Correct 140 ms 28716 KB Output is correct
66 Correct 141 ms 29256 KB Output is correct
67 Correct 143 ms 28944 KB Output is correct
68 Correct 0 ms 332 KB Output is correct
69 Correct 1 ms 332 KB Output is correct
70 Correct 0 ms 332 KB Output is correct
71 Correct 0 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 0 ms 332 KB Output is correct
74 Correct 253 ms 33740 KB Output is correct
75 Correct 268 ms 34088 KB Output is correct
76 Correct 253 ms 34272 KB Output is correct
77 Correct 252 ms 33424 KB Output is correct
78 Correct 261 ms 36616 KB Output is correct
79 Correct 174 ms 36140 KB Output is correct
80 Correct 187 ms 36308 KB Output is correct
81 Correct 153 ms 29376 KB Output is correct
82 Correct 164 ms 30696 KB Output is correct
83 Correct 167 ms 30756 KB Output is correct
84 Correct 133 ms 28512 KB Output is correct
85 Correct 135 ms 28984 KB Output is correct
86 Correct 142 ms 28972 KB Output is correct
87 Correct 147 ms 28712 KB Output is correct
88 Correct 0 ms 332 KB Output is correct
89 Correct 0 ms 332 KB Output is correct
90 Correct 0 ms 332 KB Output is correct
91 Correct 0 ms 332 KB Output is correct
92 Correct 0 ms 332 KB Output is correct
93 Correct 1 ms 324 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 324 KB Output is correct
97 Correct 1 ms 332 KB Output is correct
98 Correct 0 ms 332 KB Output is correct
99 Correct 0 ms 332 KB Output is correct
100 Correct 0 ms 320 KB Output is correct
101 Correct 0 ms 320 KB Output is correct
102 Correct 1 ms 332 KB Output is correct
103 Correct 0 ms 332 KB Output is correct
104 Correct 5 ms 1352 KB Output is correct
105 Correct 5 ms 1468 KB Output is correct
106 Correct 5 ms 1456 KB Output is correct
107 Correct 5 ms 1452 KB Output is correct
108 Correct 5 ms 1696 KB Output is correct
109 Correct 5 ms 1508 KB Output is correct
110 Correct 0 ms 332 KB Output is correct
111 Correct 1 ms 332 KB Output is correct
112 Correct 0 ms 332 KB Output is correct
113 Correct 0 ms 332 KB Output is correct
114 Correct 0 ms 332 KB Output is correct
115 Correct 0 ms 332 KB Output is correct
116 Correct 7 ms 1720 KB Output is correct
117 Correct 7 ms 1716 KB Output is correct
118 Correct 7 ms 1744 KB Output is correct
119 Correct 8 ms 1724 KB Output is correct
120 Correct 6 ms 1736 KB Output is correct
121 Correct 7 ms 1596 KB Output is correct
122 Correct 6 ms 1476 KB Output is correct
123 Correct 5 ms 1488 KB Output is correct
124 Correct 5 ms 1352 KB Output is correct
125 Correct 6 ms 1640 KB Output is correct
126 Correct 6 ms 1744 KB Output is correct
127 Correct 5 ms 1360 KB Output is correct
128 Correct 0 ms 332 KB Output is correct
129 Correct 165 ms 38844 KB Output is correct
130 Correct 147 ms 33060 KB Output is correct
131 Correct 153 ms 33696 KB Output is correct
132 Correct 151 ms 33012 KB Output is correct
133 Correct 145 ms 33048 KB Output is correct
134 Correct 147 ms 33584 KB Output is correct
135 Correct 1 ms 332 KB Output is correct
136 Correct 0 ms 332 KB Output is correct
137 Correct 0 ms 332 KB Output is correct
138 Correct 0 ms 332 KB Output is correct
139 Correct 0 ms 332 KB Output is correct
140 Correct 0 ms 332 KB Output is correct
141 Correct 297 ms 41964 KB Output is correct
142 Correct 293 ms 41560 KB Output is correct
143 Correct 276 ms 41992 KB Output is correct
144 Correct 289 ms 41880 KB Output is correct
145 Correct 301 ms 41896 KB Output is correct
146 Correct 227 ms 37140 KB Output is correct
147 Correct 180 ms 35780 KB Output is correct
148 Correct 206 ms 36648 KB Output is correct
149 Correct 159 ms 33060 KB Output is correct
150 Correct 169 ms 33176 KB Output is correct
151 Correct 146 ms 33352 KB Output is correct
152 Correct 160 ms 32972 KB Output is correct
153 Correct 159 ms 32940 KB Output is correct
154 Correct 164 ms 32924 KB Output is correct
155 Correct 161 ms 33072 KB Output is correct