#include<bits/stdc++.h>
using namespace std;
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;
set<pair<int, int>> pairss;
set<int> eh;
vector<pair<int, int>> pairs;
vector<int> funny;
map<int, int> efunny;
funny.push_back(0);
for (int i = 0; i < N; i++)
{
cin >> a >> b;
if (a > b) b += M;
eh.insert(a);
eh.insert(b);
pairss.insert({a, b});
}
tree = vector<pair<int, int>>(eh.size() + 1, {-1, -1});
int asdaa = -10;
while (!eh.empty())
{
int aaa = *eh.begin();
efunny[aaa] = funny.size();
funny.push_back(aaa);
if (asdaa == -10) asdaa = aaa;
eh.erase(eh.begin());
}
int counter = 0;
while (!pairss.empty())
{
pairs.push_back({efunny[pairss.begin()->first], efunny[pairss.begin()->second]});
upd(efunny[pairss.begin()->first], efunny[pairss.begin()->second], counter);
counter++;
pairss.erase(pairss.begin());
funny[1] = asdaa;
}
int anss = INT_MAX;
for (int i = 0; i < pairs.size(); i++)
{
int num = 1;
int ca = funny[pairs[i].first], cb = funny[pairs[i].second];
int prev = i;
while (cb - ca < M)
{
auto z = qry(efunny[cb]);
if (z.first != -1 && z.second != prev && z.second != -1)
{
cb = max(cb, funny[z.first]);
ca = min(ca, funny[pairs[z.second].first]);
num++;
}
else
break;
prev = z.second;
}
if (cb - ca >= M)
anss = min(num, anss);
}
if (anss == INT_MAX)
cout << "-1\n";
else
cout << anss << '\n';
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < pairs.size(); i++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
Execution timed out |
2090 ms |
336 KB |
Time limit exceeded |
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 |
Execution timed out |
2090 ms |
336 KB |
Time limit exceeded |
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 |
Execution timed out |
2090 ms |
336 KB |
Time limit exceeded |
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 |
Runtime error |
1 ms |
592 KB |
Execution killed with signal 11 |
4 |
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 |
Runtime error |
2 ms |
336 KB |
Execution killed with signal 11 |
4 |
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 |
Execution timed out |
2090 ms |
336 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |