# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1047458 | sofijavelkovska | Event Hopping (BOI22_events) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5;
int n;
int l[MAXN], r[MAXN];
bool rcompare(int x, int y)
{
if (r[x]==r[y])
return l[x]<l[y];
return r[x]<r[y];
}
int main()
{
int n, q;
cin >> n >> q;
int l[n], r[n];
for (int i=0; i<n; i++)
cin >> l[i] >> r[i];
int sorted[n];
for (int i=0; i<n; i++)
sorted[i]=i;
sort(sorted, sorted+n, rcompare);
int group[n];
int current=0;
int ranked[n];
for (int i=0; i<n; i++)
{
ranked[sorted[i]]=i;
if (i==0 || l[sorted[i]]<=r[sorted[i-1]])
group[sorted[i]]=current;
else
{
current=current+1;
group[sorted[i]]=current;
}
}
while (q--)
{
int x, y;
cin >> x >> y;
x=x-1;
y=y-1;
if (group[x]!=group[y])
{
cout << "impossible" << '\n';
continue;
}
if (x==y)
{
cout << 0 << '\n';
continue;
}
if (r[x]==r[y])
{
cout << 1 << '\n';
continue;
}
cout << ranked[y]-ranked[x] << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5;
int n;
int l[MAXN], r[MAXN];
bool rcompare(int x, int y)
{
if (r[x]==r[y])
return l[x]<l[y];
return r[x]<r[y];
}
int main()
{
int n, q;
cin >> n >> q;
int l[n], r[n];
for (int i=0; i<n; i++)
cin >> l[i] >> r[i];
int sorted[n];
for (int i=0; i<n; i++)
sorted[i]=i;
sort(sorted, sorted+n, rcompare);
int group[n];
int current=0;
int ranked[n];
for (int i=0; i<n; i++)
{
ranked[sorted[i]]=i;
if (i==0 || l[sorted[i]]<=r[sorted[i-1]])
group[sorted[i]]=current;
else
{
current=current+1;
group[sorted[i]]=current;
}
}
while (q--)
{
int x, y;
cin >> x >> y;
x=x-1;
y=y-1;
if (group[x]!=group[y])
{
cout << "impossible" << '\n';
continue;
}
if (x==y)
{
cout << 0 << '\n';
continue;
}
if (r[x]==r[y])
{
cout << 1 << '\n';
continue;
}
cout << ranked[y]-ranked[x] << '\n';
}
return 0;
}