QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#398938 | #8593. Coin | Talaodi | 32 | 36ms | 8840kb | C++23 | 8.9kb | 2024-04-25 20:06:29 | 2024-04-25 20:06:31 |
Judging History
answer
#include <bits/stdc++.h>
namespace IO {
template <class Stream>
void write_int128(Stream& out, __int128 v) {
if (v >= 10) {
write_int128(out, v / 10);
}
out << (int) (v % 10);
}
template <class Stream>
Stream& fmtbase(Stream& out, const char* format) {
for (; *format; format++) {
if (*format == '{' && *(format + 1) == '}') {
throw std::invalid_argument("Error Number of Parameters!");
}
out << *format;
}
return out;
}
template <class Stream, class... Nxt>
Stream& fmtbase(Stream& out, const char* format, const __int128& value, const Nxt&... nxt) {
for (; *format; format++) {
if (*format == '{' && *(format + 1) == '}') {
write_int128(out, value);
return fmtbase(out, format + 2, nxt...);
}
out << *format;
}
throw std::invalid_argument("Error Number of Parameters!");
}
template <class Stream, class Fst, class... Nxt>
Stream& fmtbase(Stream& out, const char* format, const Fst& value, const Nxt&... nxt) {
for (; *format; format++) {
if (*format == '{' && *(format + 1) == '}') {
out << value;
return fmtbase(out, format + 2, nxt...);
}
out << *format;
}
throw std::invalid_argument("Error Number of Parameters!");
}
template <class... Ps>
std::string to_string(const char* format, const Ps&... ps) {
std::stringstream ss;
fmtbase(ss, format, ps...);
return ss.str();
}
template <class... Ps>
std::ostream& fmtout(const char* format, const Ps&... ps) {
return fmtbase(std::cout, format, ps...);
}
template <class... Ps>
std::ostream& fmterr(const char* format, const Ps&... ps) {
return fmtbase(std::cerr, format, ps...);
}
std::istream& allin() {
return std::cin;
}
template <class Fst, class ... Nxt>
std::istream& allin(Fst& fst, Nxt&... nxt) {
std::cin >> fst;
return allin(nxt...);
}
template <class Iter>
std::istream& rangin(Iter begin, Iter end) {
while (begin != end) {
std::cin >> *begin;
begin++;
}
return std::cin;
}
namespace Fast {
bool sync = false;
char buf[1 << 23];
char *p1 = buf, *p2 = buf;
void sync_with_ios(bool s) {
sync = s;
}
inline char getchar() {
if (sync) {
return (char) std::cin.get();
}
else {
return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 1 << 22, stdin)), p1 == p2 ? EOF : *p1++;
}
}
void read() { }
template <class T, class... U>
void read(T& x, U&... us) {
x = 0;
T pos = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') {
pos = -1;
}
c = getchar();
}
while (isdigit(c)) {
x = 10 * x + c - '0';
c = getchar();
}
x *= pos;
read(us...);
}
template <class T>
void write(const T& t) {
if (t > 10) {
write(t / 10);
}
std::cout << (int) (t % 10);
}
}
}
namespace Solve {
using namespace IO;
using ll = long long;
using ul = unsigned long long;
using db = double;
using ld = long double;
using ui = unsigned;
using ib = __int128;
using ub = __uint128_t;
int const INF = std::numeric_limits<int>::max();
int const NINF = std::numeric_limits<int>::min();
ll const LINF = std::numeric_limits<ll>::max();
ll const LNINF = std::numeric_limits<ll>::min();
ld const EPS = 1e-8;
std::mt19937_64 mt;
ll rnd(ll l, ll r) {
return std::uniform_int_distribution<ll>(l, r)(mt);
}
template <class T>
inline int isz(const T& v) {
return v.size();
}
template <class T>
inline T& ckmx(T& a, T b) {
return a < b ? (a = b) : a;
}
template <class T>
inline T& ckmi(T& a, T b) {
return b < a ? (a = b) : a;
}
int const N = 2e5 + 10;
int const M = 8e5 + 10;
struct SegMi {
struct Node {
int l, r;
int v, t;
};
Node nd[4 * N + 1];
void build(int x, int l, int r) {
nd[x] = { l, r };
if (l != r) {
int mid = (l + r) >> 1;
build(2 * x, l, mid);
build(2 * x + 1, mid + 1, r);
}
}
void tag(int x, int t) {
ckmx(nd[x].t, t);
ckmx(nd[x].v, t);
}
void pushdown(int x) {
if (nd[x].t) {
tag(2 * x, nd[x].t);
tag(2 * x + 1, nd[x].t);
nd[x].t = 0;
}
}
void pushup(int x) {
nd[x].v = std::min(nd[2 * x].v, nd[2 * x + 1].v);
}
void modify(int x, int l, int r, int t) {
if (nd[x].r < l || nd[x].l > r) {
return;
}
else if (nd[x].l >= l && nd[x].r <= r) {
tag(x, t);
}
else {
pushdown(x);
modify(2 * x, l, r, t);
modify(2 * x + 1, l, r, t);
pushup(x);
}
}
int find(int x, int v) {
if (nd[x].v > v) {
return nd[x].r + 1;
}
else if (nd[x].l == nd[x].r) {
return nd[x].l;
}
else {
pushdown(x);
int mid = (nd[x].l + nd[x].r) >> 1;
int t = find(2 * x, v);
if (t == mid + 1) {
return find(2 * x + 1, v);
}
else {
return t;
}
}
}
};
struct SegMx {
struct Node {
int l, r;
int v, t;
};
Node nd[4 * N + 1];
void build(int x, int l, int r) {
nd[x] = { l, r, INF, INF };
if (l != r) {
int mid = (l + r) >> 1;
build(2 * x, l, mid);
build(2 * x + 1, mid + 1, r);
}
}
void tag(int x, int t) {
ckmi(nd[x].t, t);
ckmi(nd[x].v, t);
}
void pushdown(int x) {
if (nd[x].t != INF) {
tag(2 * x, nd[x].t);
tag(2 * x + 1, nd[x].t);
nd[x].t = INF;
}
}
void pushup(int x) {
nd[x].v = std::max(nd[2 * x].v, nd[2 * x + 1].v);
}
void modify(int x, int l, int r, int t) {
if (nd[x].r < l || nd[x].l > r) {
return;
}
else if (nd[x].l >= l && nd[x].r <= r) {
tag(x, t);
}
else {
pushdown(x);
modify(2 * x, l, r, t);
modify(2 * x + 1, l, r, t);
pushup(x);
}
}
int find(int x, int v) {
if (nd[x].v < v) {
return nd[x].r + 1;
}
else if (nd[x].l == nd[x].r) {
return nd[x].l;
}
else {
pushdown(x);
int mid = (nd[x].l + nd[x].r) >> 1;
int t = find(2 * x, v);
if (t == mid + 1) {
return find(2 * x + 1, v);
}
else {
return t;
}
}
}
};
struct Val {
int l, r, v;
};
struct Edge {
int x, y;
};
int n, m;
Edge eds[M + 1];
std::vector<int> gr[N + 1];
int ind[N + 1];
int ans[N + 1];
int w[N + 1];
int ord[N + 1];
int mi[N + 1];
int tmi[N + 1];
int mx[N + 1];
int tmx[N + 1];
std::vector<Val> vmi[N + 1];
std::vector<Val> vmx[N + 1];
SegMi smi;
SegMx smx;
void topsort() {
std::queue<int> que;
for (int i = 1; i <= n; i++) {
for (auto& to : gr[i]) {
ind[to]++;
}
}
for (int i = 1; i <= n; i++) {
if (!ind[i]) {
que.push(i);
}
}
for (int i = 1; i <= n; i++) {
auto t = que.front();
que.pop();
ord[i] = t;
w[t] = i;
for (auto& to : gr[t]) {
ind[to]--;
if (!ind[to]) {
que.push(to);
}
}
}
}
void main() {
std::cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
std::cin >> u >> v;
gr[u].push_back(v);
eds[i] = { u, v };
}
topsort();
for (int i = 1; i <= n; i++) {
mi[i] = INF;
mx[i] = 0;
}
for (int i = 1; i <= m; i++) {
int u = eds[i].x;
int v = eds[i].y;
vmi[w[u]].push_back({ tmi[w[u]], i - 1, mi[w[u]] });
ckmi(mi[w[u]], w[v]);
tmi[w[u]] = i;
vmx[w[v]].push_back({ tmx[w[v]], i - 1, mx[w[v]] });
ckmx(mx[w[v]], w[u]);
tmx[w[v]] = i;
}
for (int i = 1; i <= n; i++) {
vmi[i].push_back({ tmi[i], m, mi[i] });
vmx[i].push_back({ tmx[i], m, mx[i] });
}
// for (int i = 1; i <= n; i++) {
// fmterr("{} ", ord[i]);
// }
// fmterr("\n");
// for (int i = 1; i <= n; i++) {
// for (auto& v : vmi[i]) {
// fmterr("({} {} {}) ", v.l, v.r, v.v);
// }
// fmterr("\n");
// }
smi.build(1, 0, m);
smx.build(1, 0, m);
for (int i = 1; i <= n; i++) {
ckmx(ans[ord[i]], smi.find(1, i));
for (auto& v : vmi[i]) {
smi.modify(1, v.l, v.r, v.v);
}
}
for (int i = n; i >= 1; i--) {
ckmx(ans[ord[i]], smx.find(1, i));
for (auto& v : vmx[i]) {
smx.modify(1, v.l, v.r, v.v);
}
}
for (int i = 1; i <= n; i++) {
fmtout("{} ", ans[i] > m ? -1 : ans[i]);
}
}
void init() {
}
void clear() {
}
}
signed main() {
#ifndef ONLINE_JUDGE
auto input_file = freopen("d.in", "r", stdin);
auto output_file = freopen("d.out", "w", stdout);
#endif
auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
IO::fmterr("seed: {}\n", seed);
Solve::mt.seed(seed);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t = 1;
// std::cin >> t;
Solve::init();
for (int id = 1; id <= t; id++) {
Solve::main();
Solve::clear();
}
#ifndef ONLINE_JUDGE
std::cout.flush();
fclose(input_file);
fclose(output_file);
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 6
Accepted
Test #1:
score: 6
Accepted
time: 0ms
memory: 3648kb
input:
4 4 2 4 3 1 4 1 2 3
output:
3 4 -1 -1
result:
ok ac
Test #2:
score: 6
Accepted
time: 2ms
memory: 3704kb
input:
6 8 1 5 5 4 6 2 2 5 4 3 6 1 6 5 2 1
output:
8 8 5 5 5 6
result:
ok ac
Test #3:
score: 6
Accepted
time: 3ms
memory: 3704kb
input:
2 1 1 2
output:
1 1
result:
ok ac
Test #4:
score: 6
Accepted
time: 2ms
memory: 3672kb
input:
6 12 1 5 5 4 6 2 2 5 4 3 6 5 1 5 1 5 2 4 6 3 1 3 4 3
output:
-1 -1 5 5 5 -1
result:
ok ac
Test #5:
score: 6
Accepted
time: 0ms
memory: 3644kb
input:
7 20 1 6 6 3 1 4 1 5 1 7 1 2 1 5 2 3 4 5 7 2 2 4 5 3 6 3 1 3 4 3 7 5 2 6 4 6 7 2 7 5
output:
6 17 12 18 -1 -1 17
result:
ok ac
Test #6:
score: 6
Accepted
time: 0ms
memory: 3900kb
input:
7 20 5 6 1 3 3 6 4 1 7 4 2 5 4 3 2 6 7 5 4 6 2 6 2 1 4 5 1 3 1 5 7 1 7 6 4 1 7 6 3 6
output:
15 -1 -1 -1 -1 6 -1
result:
ok ac
Test #7:
score: 6
Accepted
time: 0ms
memory: 3868kb
input:
7 20 7 6 4 5 6 4 3 6 4 1 6 2 3 5 5 2 7 6 1 2 3 6 6 4 7 1 6 1 7 1 4 5 3 6 3 5 4 5 3 1
output:
-1 10 -1 8 -1 6 -1
result:
ok ac
Subtask #2:
score: 16
Accepted
Dependency #1:
100%
Accepted
Test #8:
score: 16
Accepted
time: 0ms
memory: 3720kb
input:
20 100 5 20 4 5 18 16 1 13 14 9 11 19 6 4 7 20 16 11 8 13 4 5 16 9 12 14 7 12 11 3 9 11 9 11 13 6 3 10 12 9 13 4 20 12 13 6 18 11 5 7 5 7 15 18 12 15 17 13 15 18 3 2 11 2 11 2 15 19 4 19 14 19 14 9 17 3 1 18 8 10 16 19 1 6 7 2 5 12 1 18 8 20 5 18 8 5 4 16 1 15 5 19 18 19 17 10 1 10 17 3 10 2 3 10 17...
output:
-1 -1 -1 31 31 31 31 -1 31 -1 31 31 31 -1 71 -1 -1 -1 -1 31
result:
ok ac
Test #9:
score: 16
Accepted
time: 3ms
memory: 3760kb
input:
100 400 87 45 42 17 9 81 65 10 8 82 76 48 39 73 21 58 76 30 76 92 74 76 99 90 38 50 86 74 75 52 8 2 80 55 20 95 66 60 78 82 10 18 22 59 23 17 63 76 56 51 38 10 50 65 41 28 64 77 59 53 100 66 38 84 23 47 17 9 45 75 41 28 33 41 8 78 2 95 3 11 40 15 60 63 23 17 82 2 61 44 44 16 77 34 100 66 96 99 68 12...
output:
-1 -1 187 -1 183 -1 -1 -1 183 -1 187 -1 -1 187 183 187 183 -1 -1 -1 -1 183 -1 183 -1 -1 183 183 -1 -1 -1 -1 187 187 -1 -1 187 183 183 183 -1 -1 183 187 183 188 -1 -1 -1 -1 183 -1 183 183 -1 -1 -1 -1 183 -1 187 -1 -1 187 -1 187 -1 -1 -1 -1 -1 -1 183 -1 183 187 187 -1 188 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1...
result:
ok ac
Subtask #3:
score: 10
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #10:
score: 10
Accepted
time: 3ms
memory: 4344kb
input:
1000 4000 619 298 211 477 584 418 812 280 978 268 280 345 715 364 73 664 915 819 535 28 110 959 384 663 773 315 792 250 374 80 134 202 779 416 613 334 318 756 21 812 424 997 664 277 151 963 299 438 955 988 532 653 521 43 121 20 902 849 237 305 272 893 325 792 469 549 891 531 612 810 294 256 188 990 ...
output:
-1 1981 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1980 1981 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1980 1977 1981 -1 -1 -1 -1 1981 -1 -1 -1 -1 -1 -1 -1 1980 -1 -1 1981 1977 -1 -1 -1 -1 -1 -1 -1 -1 -1 1981 -1 -1 -1 -1 -1 -1 1981 -1 -1 -1 1981 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1977 -1 -1 -1 -1 -1...
result:
ok ac
Test #11:
score: 10
Accepted
time: 7ms
memory: 4284kb
input:
1000 4000 229 403 665 867 715 618 306 900 423 567 515 288 39 754 782 321 516 969 432 89 304 726 837 39 648 991 870 369 84 681 864 41 682 940 565 914 724 763 694 112 969 815 864 41 975 868 662 803 707 465 905 791 116 115 823 578 949 588 589 522 775 447 795 130 366 456 993 910 187 396 102 774 143 90 9...
output:
1976 -1 -1 1961 1976 1976 -1 -1 -1 -1 1973 -1 1961 -1 -1 1976 -1 -1 -1 1976 -1 -1 1961 -1 1976 1976 1961 1961 -1 1961 -1 -1 1961 -1 1976 -1 -1 -1 -1 -1 -1 -1 -1 1976 -1 -1 1973 -1 1976 -1 1961 -1 1976 -1 -1 -1 -1 -1 -1 -1 1976 -1 1976 1976 1976 -1 1976 1961 1976 -1 -1 -1 -1 -1 1973 -1 1976 -1 1976 1...
result:
ok ac
Test #12:
score: 10
Accepted
time: 3ms
memory: 4584kb
input:
1000 4000 693 317 927 745 607 353 336 456 182 240 100 824 252 317 88 828 436 714 833 621 533 704 63 735 522 518 900 283 135 829 627 459 601 176 876 806 573 140 380 898 870 795 990 876 943 148 500 462 808 486 920 463 794 7 531 181 60 105 497 875 859 469 997 575 706 838 296 194 453 793 310 673 771 307...
output:
1998 1998 -1 -1 -1 -1 1998 1998 -1 1998 -1 1998 1998 -1 1987 -1 1998 1998 -1 -1 -1 -1 -1 1998 1998 1998 -1 -1 -1 1998 -1 1998 -1 -1 -1 -1 1998 -1 1998 -1 1998 -1 1998 -1 -1 1998 1998 1998 1998 -1 1998 1998 1998 -1 1987 1998 -1 1998 1998 1998 -1 -1 1998 -1 -1 1998 -1 1998 1998 1998 1998 1998 -1 1987 ...
result:
ok ac
Subtask #4:
score: 0
Runtime Error
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #13:
score: 68
Accepted
time: 36ms
memory: 8840kb
input:
10000 24156 6770 9800 71 3709 5373 2058 9145 5993 9973 456 1562 628 839 3483 9354 9169 7105 7062 4434 4044 2147 534 7337 1981 719 3853 8205 8198 1286 7343 246 4189 1324 8878 547 3468 2219 8650 7265 5823 6382 5720 6219 1258 8893 466 5615 9147 7763 7160 3652 6512 7594 7169 235 1287 6487 2049 6894 853 ...
output:
-1 -1 -1 -1 -1 -1 -1 19979 -1 -1 19979 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 19979 19979 -1 -1 -1 -1 -1 -1 -1 -1 19979 -1 -1 -1 -1 19979 -1 -1 -1 -1 -1 -1 -1 19979 -1 -1 -1 -1 -1 19979 -1 -1 19979 19979 -1 19979 19979 -1 -1 -1 -1 19979 -1 -1 -1 -1 19979 -1 -1 -1 -1 -1 -1 19979 -1 -1 -1 -1 19979 -1 -1 ...
result:
ok ac
Test #14:
score: 0
Runtime Error
input:
200000 468137 35741 15045 1120 91913 22147 173548 159967 11985 16594 176358 9659 38704 191979 69853 143002 8669 21000 41718 133024 156174 117934 139285 195752 71163 147173 197600 11162 174429 73822 144204 54279 46442 117136 26281 57220 169661 66060 17651 79359 190948 157353 39342 174367 171524 20596...