#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define sync ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10, inf = 1e9 + 7, mood = 1e9+7, mood2 = 19025953, base = 31, maxsq = 400, maxlg = 20;
vector<pii> query[maxn];
int s[maxn], f[maxn], ans[maxn];
vector<int> order;
int n, q;
bool cmp(int a, int b)
{
return mp(s[a], f[a]) < mp(s[b], f[b]);
}
bool cmp2(int a, int b)
{
return mp(f[a], s[a]) < mp(f[b], s[b]);
}
int main()
{
sync;
cin >> n >> q;
for(int i = 1; i <= n; i++)
cin >> s[i] >> f[i], order.pb(i);
for(int i = 0; i < q; i++)
{
int ss, ff;
cin >> ss >> ff;
query[ss].pb(mp(ff, i));
ans[i] = 0 - maxn;
}
sort(order.begin(), order.end(), cmp);
for(int i = 0; i < n; i++)
{
int v = order[i];
vector<int> vc, vc2;
int mx = i;
int j = i+1;
int u = order[j];
while(s[u] <= f[v] && j < n)
{
vc.pb(f[u]);
vc2.pb(u);
if(f[u] > f[mx])
mx = u;
j++;
u = order[j];
}
sort(vc.begin(), vc.end());
sort(vc2.begin(), vc2.end(), cmp2);
for(auto x : query[v])
{
u = x.fi;
int ind = x.se;
if(u == v)
{
ans[ind] += maxn;
continue;
}
if(f[mx] <= f[u])
{
ans[ind]++;
query[mx].pb(x);
continue;
}
int tmp = lower_bound(vc.begin(), vc.end(), f[u]) - vc.begin();
while(vc[tmp] > f[u] && tmp > 0)
tmp--;
if(vc[tmp] > f[u])
continue;
ans[ind]++;
query[vc2[tmp]].pb(x);
}
query[v].clear();
}
for(int i = 0; i < q; i++)
{
if(ans[i] < 0)
cout << "impossible" << '\n';
else
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
366 ms |
259900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
405 ms |
259948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
366 ms |
259900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |