#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
using ll = long long;
int lp[200005], rp[200005], w[200005], rem[200005], n, m;
ll val[200005], lazy[200005];
std::vector<int> vec[200005];
struct comp { bool operator ()(int x, int y) { return rp[x] < rp[y]; } };
inline bool check(int pos, ll cnt, ll lim)
{
if (cnt > lim)
return false;
for (int i = 0; i < n; i++)
{
vec[i].clear();
lazy[i] = 0;
}
for (int i = 0; i < m; i++)
{
if (lp[i] <= pos && pos < rp[i])
{
vec[lp[i]].push_back(i);
rem[i] = w[i];
}
}
std::priority_queue<int, std::vector<int>, comp> que;
for (int i = 0; i <= pos; i++)
{
for (int x : vec[i])
que.push(x);
ll cur = std::max(0ll, val[i] + cnt - lim) + 1 >> 1;
while (cur && !que.empty())
{
int x = que.top();
que.pop();
int sub = std::min<ll>(cur, rem[x]);
cur -= sub;
rem[x] -= sub;
cnt -= sub << 1;
lazy[lp[x]] -= sub;
lazy[rp[x]] += sub << 1;
if (rem[x])
que.push(x);
}
if (cur)
return false;
}
for (int i = 0; i < n; i++)
{
if (i)
lazy[i] += lazy[i - 1];
if (lazy[i] + val[i] > lim)
return false;
}
return true;
}
int main()
{
// freopen("JOISC2017-D2T1.in", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", lp + i, rp + i, w + i);
if (lp[i] > rp[i])
std::swap(lp[i], rp[i]);
val[lp[i]] += w[i];
val[rp[i]] -= w[i];
}
int fir = 0, lst = 0;
for (int i = 1; i < n; i++)
{
val[i] += val[i - 1];
if (val[i] > val[fir])
fir = i;
if (val[i] == val[fir])
lst = i;
}
int l = 0, r = val[lst], res = -1;
while (l <= r)
{
int mid = l + r >> 1;
bool ok = false;
for (int x : {fir, lst})
{
for (int y = 0; y < 2; y++)
ok |= check(x, val[fir] - mid + y, mid);
}
if (ok)
{
res = mid;
r = mid - 1;
}
else
l = mid + 1;
}
printf("%d\n", res);
return 0;
}
Compilation message
arranging_tickets.cpp: In function 'int main()':
arranging_tickets.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
arranging_tickets.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", lp + i, rp + i, w + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5248 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
8 ms |
4992 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5248 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
8 ms |
4992 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
6 ms |
4992 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
6 ms |
4992 KB |
Output is correct |
24 |
Correct |
7 ms |
4992 KB |
Output is correct |
25 |
Correct |
8 ms |
5092 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5248 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
8 ms |
4992 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
6 ms |
4992 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
6 ms |
4992 KB |
Output is correct |
24 |
Correct |
7 ms |
4992 KB |
Output is correct |
25 |
Correct |
8 ms |
5092 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
15 ms |
5220 KB |
Output is correct |
29 |
Correct |
16 ms |
5248 KB |
Output is correct |
30 |
Correct |
13 ms |
5248 KB |
Output is correct |
31 |
Correct |
15 ms |
5288 KB |
Output is correct |
32 |
Correct |
18 ms |
5248 KB |
Output is correct |
33 |
Correct |
16 ms |
5248 KB |
Output is correct |
34 |
Correct |
10 ms |
5144 KB |
Output is correct |
35 |
Correct |
14 ms |
5248 KB |
Output is correct |
36 |
Correct |
15 ms |
5248 KB |
Output is correct |
37 |
Correct |
8 ms |
5196 KB |
Output is correct |
38 |
Correct |
8 ms |
5120 KB |
Output is correct |
39 |
Correct |
8 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5248 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
8 ms |
4992 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
6 ms |
4992 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
6 ms |
4992 KB |
Output is correct |
24 |
Correct |
7 ms |
4992 KB |
Output is correct |
25 |
Correct |
8 ms |
5092 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
15 ms |
5220 KB |
Output is correct |
29 |
Correct |
16 ms |
5248 KB |
Output is correct |
30 |
Correct |
13 ms |
5248 KB |
Output is correct |
31 |
Correct |
15 ms |
5288 KB |
Output is correct |
32 |
Correct |
18 ms |
5248 KB |
Output is correct |
33 |
Correct |
16 ms |
5248 KB |
Output is correct |
34 |
Correct |
10 ms |
5144 KB |
Output is correct |
35 |
Correct |
14 ms |
5248 KB |
Output is correct |
36 |
Correct |
15 ms |
5248 KB |
Output is correct |
37 |
Correct |
8 ms |
5196 KB |
Output is correct |
38 |
Correct |
8 ms |
5120 KB |
Output is correct |
39 |
Correct |
8 ms |
5248 KB |
Output is correct |
40 |
Correct |
1421 ms |
13568 KB |
Output is correct |
41 |
Correct |
1658 ms |
13692 KB |
Output is correct |
42 |
Correct |
1393 ms |
13160 KB |
Output is correct |
43 |
Correct |
1374 ms |
13080 KB |
Output is correct |
44 |
Correct |
982 ms |
13240 KB |
Output is correct |
45 |
Correct |
1517 ms |
12880 KB |
Output is correct |
46 |
Correct |
1664 ms |
13236 KB |
Output is correct |
47 |
Correct |
104 ms |
13376 KB |
Output is correct |
48 |
Correct |
110 ms |
12712 KB |
Output is correct |
49 |
Correct |
102 ms |
11996 KB |
Output is correct |
50 |
Correct |
55 ms |
9976 KB |
Output is correct |
51 |
Correct |
69 ms |
11560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
4992 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5248 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
8 ms |
4992 KB |
Output is correct |
13 |
Correct |
6 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
6 ms |
4992 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
6 ms |
4992 KB |
Output is correct |
24 |
Correct |
7 ms |
4992 KB |
Output is correct |
25 |
Correct |
8 ms |
5092 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
15 ms |
5220 KB |
Output is correct |
29 |
Correct |
16 ms |
5248 KB |
Output is correct |
30 |
Correct |
13 ms |
5248 KB |
Output is correct |
31 |
Correct |
15 ms |
5288 KB |
Output is correct |
32 |
Correct |
18 ms |
5248 KB |
Output is correct |
33 |
Correct |
16 ms |
5248 KB |
Output is correct |
34 |
Correct |
10 ms |
5144 KB |
Output is correct |
35 |
Correct |
14 ms |
5248 KB |
Output is correct |
36 |
Correct |
15 ms |
5248 KB |
Output is correct |
37 |
Correct |
8 ms |
5196 KB |
Output is correct |
38 |
Correct |
8 ms |
5120 KB |
Output is correct |
39 |
Correct |
8 ms |
5248 KB |
Output is correct |
40 |
Correct |
1421 ms |
13568 KB |
Output is correct |
41 |
Correct |
1658 ms |
13692 KB |
Output is correct |
42 |
Correct |
1393 ms |
13160 KB |
Output is correct |
43 |
Correct |
1374 ms |
13080 KB |
Output is correct |
44 |
Correct |
982 ms |
13240 KB |
Output is correct |
45 |
Correct |
1517 ms |
12880 KB |
Output is correct |
46 |
Correct |
1664 ms |
13236 KB |
Output is correct |
47 |
Correct |
104 ms |
13376 KB |
Output is correct |
48 |
Correct |
110 ms |
12712 KB |
Output is correct |
49 |
Correct |
102 ms |
11996 KB |
Output is correct |
50 |
Correct |
55 ms |
9976 KB |
Output is correct |
51 |
Correct |
69 ms |
11560 KB |
Output is correct |
52 |
Incorrect |
63 ms |
10080 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |