This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
int n,q;
array<int,3> a[maxn];
pii rmq[maxn][20];
pii minn[maxn][20];
vi nen;
pii getmin(int l,int r)
{
int lg = __lg(r - l + 1);
return min(minn[l][lg],minn[r - (1 << lg) + 1][lg]);
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
fo(i,1,n)
{
cin >> a[i][0] >> a[i][1];
nen.pb(a[i][0]);
nen.pb(a[i][1]);
a[i][2] = i;
}
sort(all(nen));UNIQUE(nen);
fo(i,1,ss(nen)) fo(j,0,19) minn[i][j] = rmq[i][j] = {inf,inf};
fo(i,1,n)
{
a[i][0] = lower_bound(all(nen),a[i][0]) - nen.begin() + 1;
a[i][1] = lower_bound(all(nen),a[i][1]) - nen.begin() + 1;
minimize(minn[a[i][1]][0],make_pair(a[i][0],i));
}
fo(j,1,19) fo(i,1,ss(nen))
minn[i][j] = min(minn[i][j - 1],minn[i + (1 << j - 1)][j - 1]);
fo(i,1,n)
{
rmq[i][0] = getmin(a[i][0],a[i][1]);
}
fo(j,1,19) fo(i,1,n)
{
if(rmq[i][j - 1].fi == inf) continue;
rmq[i][j] = rmq[rmq[i][j - 1].se][j - 1];
}
while(q--)
{
int l,r;
cin >> l >> r;
if(a[r][1] < a[l][0])
{
cout << "impossible";en;
continue;
}
if(l == r)
{
cout << 0;en;
continue;
}
int ans = 0;
fod(i,19,0) if(rmq[r][i].fi != inf and rmq[r][i].fi > a[l][1])
{
ans += (1 << i);
r = rmq[r][i].se;
if(r == inf) break;
}
if(r != inf)
{
r = rmq[r][0].se;
ans++;
if(r != inf and a[r][0] <= a[l][1] and a[r][1] >= a[l][1])
{
cout << ans + 1;en;
continue;
}
else cout << "impossible \n";
}
else cout << "impossible\n";
}
}
Compilation message (stderr)
events.cpp:68:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
68 | main()
| ^~~~
events.cpp: In function 'int main()':
events.cpp:95:58: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
95 | minn[i][j] = min(minn[i][j - 1],minn[i + (1 << j - 1)][j - 1]);
| ~~^~~
events.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |