QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#715105 | #7752. The Only Way to the Destination | ChiTang | ML | 1ms | 4596kb | C++17 | 2.1kb | 2024-11-06 10:28:07 | 2024-11-06 10:28:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
int n, m, k;
const int N = 2e5 + 10;
struct emp
{
int l, r, id;
inline emp(int L = 0, int R = 0, int ID = 0)
{
l = L, r = R, id = ID;
}
};
int fun(emp a, emp b)
{
int al = a.l, ar = a.r;
int bl = b.l, br = b.r;
int mxl = max(al, bl), mnr = min(ar, br);
return max((int)0, mnr - mxl + 1);
}
vector<int> fa(N);
int find(int x)
{
while (fa[x] != x)
{
x = fa[x];
}
return x;
}
signed main()
{
cin >> n >> m >> k;
int M = m + 5;
vector<pii> w[M];
vector<emp> e[M];
vector<pii> g;
for (int i = 1; i <= k; i++)
{
int x1, x2, y;
cin >> x1 >> x2 >> y;
w[y].push_back({x1, x2});
}
if (n == 1)
{
cout << "YES" << endl;
return 0;
}
if (m > 2 * k + 1)
{
cout << "NO" << endl;
return 0;
}
int idx = 0;
for (int y = 1; y <= m; y++)
{
int l = 1;
for (int j = 0; j < w[y].size(); j++)
{
if (w[y][j].fi - 1 >= l)
{
e[y].push_back(emp(l, w[y][j].fi - 1, ++idx));
// cout << y << ' ' << l << ' ' << w[y][j].fi - 1 << endl;
}
l = w[y][j].se + 1;
}
if (l <= n)
{
e[y].push_back(emp(l, n, ++idx));
// cout << y << ' ' << l << ' ' << n << endl;
}
}
for (int y = 1; y < m; y++)
{
for (int p = 0, q = 0; p < e[y].size(); p++)
{
if (q > 0)
--q;
while (q >= 0 && q < e[y + 1].size() && e[y + 1][q].r < e[y][p].l)
q++;
// cout << p << ' ' << q << endl;
while (q >= 0 && q < e[y + 1].size() && fun(e[y][p], e[y + 1][q]) != 0)
{
if (fun(e[y][p], e[y + 1][q]) > 1)
{
// cout << y << ' ' << p << ' ' << q << ' ' << fun(e[y][p], e[y + 1][q]) << endl;
cout << "NO" << endl;
return 0;
}
g.push_back({e[y][p].id, e[y + 1][q].id});
q ++ ;
}
}
}
for (int i = 1; i <= idx; i++)
{
fa[i] = i;
}
for (auto [u, v] : g)
{
if (find(u) == find(v))
{
cout << "NO" << endl;
return 0;
}
fa[find(u)] = find(v);
}
cout << "YES" << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4596kb
input:
5 3 2 2 5 1 1 4 3
output:
YES
result:
ok answer is YES
Test #2:
score: 0
Accepted
time: 1ms
memory: 4388kb
input:
5 3 1 2 4 2
output:
NO
result:
ok answer is NO
Test #3:
score: 0
Accepted
time: 1ms
memory: 4568kb
input:
2 4 2 2 2 1 1 1 4
output:
NO
result:
ok answer is NO
Test #4:
score: -100
Memory Limit Exceeded
input:
409455775 596220461 69036 72554058 85866426 497212608 54242898 110165840 236869995 68671059 127632371 324336242 39386477 208393446 248270338 151972182 327931056 231832 36658944 75335495 293646122 29512382 138875677 205628469 149151850 327396301 590717922 114980184 165064700 323939243 1771834 7016377...