#include<bits/stdc++.h>
using namespace std;
#define int long long
int M;
vector<pair<int, int>> tree;
void upd(int n, int x, int i)
{
while (n <= M)
{
if (tree[n].first < x)
tree[n] = {x, i};
n += n & (-n);
}
}
pair<int, int> qry(int a)
{
pair<int, int> ans = {-1, -1};
while (a > 0)
{
if (ans.first < tree[a].first)
ans = tree[a];
a -= a & (-a);
}
return ans;
}
signed main()
{
int N, a, b;
cin >> N >> M;
tree = vector<pair<int, int>>(M + 1, {-1, -1});
vector<pair<int, int>> pairs;
vector<bool> works(N, true);
for (int i = 0; i < N; i++)
{
cin >> a >> b;
if (a > b) b += M;
upd(a, b, i);
pairs.push_back({a, b});
}
int anss = LLONG_MAX;
for (int i = 0; i < N; i++)
{
if (!works[i]) continue;
works[i] = false;
int num = 1;
int ca = pairs[i].first, cb = pairs[i].second;
while (cb - ca < M)
{
auto z = qry(cb);
if (z.first != -1 && works[z.second])
{
cb = max(cb, z.first);
works[z.second] = false;
num++;
}
else
break;
}
if (cb - ca >= M)
anss = min(num, anss);
}
if (anss == LLONG_MAX)
cout << "-1\n";
else
cout << anss << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Execution timed out |
2075 ms |
336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Execution timed out |
2053 ms |
504 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |