QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#289056 | #7861. Inverse Topological Sort | ucup-team987# | AC ✓ | 113ms | 19648kb | C++20 | 5.4kb | 2023-12-23 15:02:32 | 2024-11-22 19:53:09 |
Judging History
answer
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cassert>
#include <algorithm>
#include <cassert>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
constexpr int bsf_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
int bsf(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
} // namespace internal
} // namespace atcoder
namespace atcoder {
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
public:
segtree() : segtree(0) {}
explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) const {
assert(0 <= p && p < _n);
return d[p + size];
}
S prod(int l, int r) const {
assert(0 <= l && l <= r && r <= _n);
S sml = e(), smr = e();
l += size;
r += size;
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() const { return d[1]; }
template <bool (*f)(S)> int max_right(int l) const {
return max_right(l, [](S x) { return f(x); });
}
template <class F> int max_right(int l, F f) const {
assert(0 <= l && l <= _n);
assert(f(e()));
if (l == _n) return _n;
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm, d[l]))) {
while (l < size) {
l = (2 * l);
if (f(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*f)(S)> int min_left(int r) const {
return min_left(r, [](S x) { return f(x); });
}
template <class F> int min_left(int r, F f) const {
assert(0 <= r && r <= _n);
assert(f(e()));
if (r == 0) return 0;
r += size;
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!f(op(d[r], sm))) {
while (r < size) {
r = (2 * r + 1);
if (f(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
std::vector<S> d;
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};
} // namespace atcoder
using namespace std;
struct dat{
int maxv,minv,maxa,maxb;
bool emp;
};
dat op(dat l,dat r)
{
if(l.emp)return r;
if(r.emp)return l;
l.maxv=max(l.maxv,r.maxv);
l.minv=min(l.minv,r.minv);
l.maxa=max(l.maxa,r.maxa);
l.maxb=max(l.maxb,r.maxb);
return l;
}
dat e(){return(dat){0,0,0,0,true};}
int N;
int A[1<<17],B[1<<17];
int invA[1<<17],invB[1<<17];
vector<int>G[1<<17];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>N;
for(int i=0;i<N;i++)
{
cin>>A[i];
invA[A[i]]=i;
}
for(int i=0;i<N;i++)
{
cin>>B[i];
invB[B[i]]=i;
}
atcoder::segtree<dat,op,e>seg(N);
for(int i=0;i<N;i++)
{
int r=invB[A[i]];
dat t=seg.prod(0,r);
if(!t.emp)
{
vector<int>U;
U.push_back(t.maxv);
U.push_back(t.minv);
U.push_back(A[t.maxa]);
U.push_back(B[t.maxb]);
sort(U.begin(),U.end());
U.erase(unique(U.begin(),U.end()),U.end());
for(int u:U)
{
G[u].push_back(A[i]);
}
}
t.emp=false;
t.maxv=t.minv=A[i];
t.maxa=i;
t.maxb=r;
seg.set(r,t);
}
priority_queue<int>BQ;
priority_queue<int,vector<int>,greater<int> >AQ;
vector<int>An,Bn;
vector<int>degA(N+1),degB(N+1);
for(int i=1;i<=N;i++)for(int v:G[i])
{
degA[v]++;
degB[v]++;
}
for(int i=1;i<=N;i++)if(!degA[i])
{
AQ.push(i);
BQ.push(i);
}
while(!AQ.empty())
{
int u=AQ.top();
AQ.pop();
An.push_back(u);
for(int v:G[u])if(!--degA[v])AQ.push(v);
}
while(!BQ.empty())
{
int u=BQ.top();
BQ.pop();
Bn.push_back(u);
for(int v:G[u])if(!--degB[v])BQ.push(v);
}
if(An!=vector<int>(A,A+N)||Bn!=vector<int>(B,B+N))cout<<"No\n";
else
{
cout<<"Yes\n";
int M=0;
for(int i=1;i<=N;i++)for(int v:G[i])M++;
cout<<M<<"\n";
for(int i=1;i<=N;i++)for(int v:G[i])cout<<i<<" "<<v<<"\n";
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5648kb
input:
3 1 2 3 1 2 3
output:
Yes 3 1 2 1 3 2 3
result:
ok n=3
Test #2:
score: 0
Accepted
time: 1ms
memory: 5648kb
input:
3 1 2 3 3 2 1
output:
Yes 0
result:
ok n=3
Test #3:
score: 0
Accepted
time: 1ms
memory: 5576kb
input:
3 3 2 1 1 2 3
output:
No
result:
ok n=3
Test #4:
score: 0
Accepted
time: 0ms
memory: 5696kb
input:
10 6 8 9 4 1 3 7 5 10 2 8 6 9 10 4 7 5 3 2 1
output:
Yes 17 3 2 4 1 4 3 4 7 4 5 6 9 6 4 6 10 7 5 8 9 9 4 9 1 9 3 9 7 9 5 9 10 10 2
result:
ok n=10
Test #5:
score: 0
Accepted
time: 1ms
memory: 5648kb
input:
10 4 2 5 6 7 8 9 1 3 10 8 7 9 6 5 4 2 1 10 3
output:
Yes 9 1 3 1 10 2 1 4 2 7 9 8 9 9 1 9 3 9 10
result:
ok n=10
Test #6:
score: 0
Accepted
time: 1ms
memory: 5672kb
input:
100 5 16 25 26 36 28 42 46 2 38 48 23 29 30 31 12 40 51 58 64 71 75 83 14 68 74 79 84 86 88 56 6 39 92 9 11 4 47 3 13 15 8 49 54 32 45 61 33 66 72 80 24 69 89 21 82 93 94 27 76 90 10 18 77 78 57 95 7 50 81 96 97 35 19 44 20 55 63 34 60 67 22 73 52 87 91 65 43 85 37 62 53 98 1 41 70 99 59 100 17 92 8...
output:
Yes 273 1 41 1 70 2 40 2 86 2 47 2 3 2 13 2 15 2 8 2 45 2 33 2 72 2 21 2 94 2 27 2 76 2 90 2 10 2 18 2 77 2 78 2 57 2 95 2 7 2 50 2 81 2 96 2 35 2 19 2 44 2 20 2 55 2 63 2 34 2 60 2 67 2 22 2 73 2 52 2 87 2 91 2 65 2 43 2 85 2 37 2 62 2 53 2 98 2 1 2 99 2 59 2 100 2 17 3 78 3 91 4 47 4 61 4 66 4 80 ...
result:
ok n=100
Test #7:
score: 0
Accepted
time: 0ms
memory: 5996kb
input:
1000 11 2 29 50 53 54 155 162 211 213 223 240 270 226 243 276 288 304 315 341 249 358 359 381 178 402 51 417 434 163 459 466 471 498 327 464 518 527 549 559 113 581 589 60 347 594 504 593 598 603 607 610 619 648 649 658 681 684 416 686 153 712 575 741 349 382 759 322 17 289 763 764 774 718 777 9 637...
output:
Yes 3434 1 964 1 761 2 598 2 790 2 78 2 63 2 445 2 901 2 83 2 847 2 84 2 169 2 725 2 762 2 117 2 343 2 241 2 279 2 514 2 561 2 125 2 256 2 657 2 539 2 234 2 933 2 715 2 566 2 702 2 104 2 146 2 394 2 569 2 879 2 35 2 132 2 311 2 946 2 134 2 609 2 206 2 21 2 751 2 208 2 447 2 406 2 782 2 195 2 713 2 7...
result:
ok n=1000
Test #8:
score: 0
Accepted
time: 73ms
memory: 18432kb
input:
100000 1 5 10 12 13 14 16 17 18 19 21 27 28 33 37 40 41 44 45 49 50 51 52 54 57 58 62 64 67 69 71 72 74 75 77 78 79 80 84 89 93 95 96 100 102 104 111 113 115 117 118 119 120 121 122 123 124 126 127 129 132 135 136 138 139 142 144 150 151 152 153 154 155 156 164 166 167 170 174 177 178 180 181 182 18...
output:
Yes 146440 1 17591 4 16417 4 78928 4 4130 4 79359 4 51061 4 83183 5 2 5 3 5 4 7 6 8 660 9 50540 9 68381 9 17691 9 12867 9 8007 9 8 9 77823 10 52585 10 7 10 52401 10 9 10 62665 12 11 12 56242 12 72683 12 9486 13 88560 16 15 16 79923 16 60003 16 31508 16 77358 17 67167 18 65533 18 40557 18 648 18 4119...
result:
ok n=100000
Test #9:
score: 0
Accepted
time: 113ms
memory: 19040kb
input:
100000 40 84 102 116 124 157 177 191 193 199 256 259 293 300 304 326 430 439 473 477 489 511 515 518 547 583 593 630 664 697 747 751 769 787 789 892 928 945 963 971 978 1052 1063 1067 1077 1080 1088 1101 1136 1143 1172 1180 1198 1274 1312 1359 1361 1380 1382 1404 1414 1428 1435 1466 1475 1497 1517 1...
output:
Yes 348939 1 78240 1 45405 1 24256 1 94272 1 98303 1 52640 1 87452 1 73069 1 9139 1 30504 1 21972 1 20678 1 48839 1 25208 1 16928 1 79070 1 22232 1 30246 1 87537 1 1545 1 90724 1 1830 1 49339 1 40033 1 79312 1 54431 1 89472 1 86355 1 6978 1 92922 1 74200 1 75016 1 85853 1 31291 1 5437 1 46589 1 6432...
result:
ok n=100000
Test #10:
score: 0
Accepted
time: 35ms
memory: 16676kb
input:
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102...
output:
Yes 3959 16 15 28 25410 43 29010 43 42 44 85590 50 3874 51 75818 161 160 175 174 257 2501 277 276 386 65575 404 403 418 417 456 455 473 5377 526 40026 540 66586 541 540 547 86971 567 23179 568 35479 612 91704 712 45430 714 36371 765 27333 869 11574 882 881 891 890 947 946 971 970 996 42158 1031 8004...
result:
ok n=100000
Test #11:
score: 0
Accepted
time: 84ms
memory: 18320kb
input:
100000 4 6 12 16 20 23 24 27 32 34 36 39 46 54 68 76 77 81 86 88 95 99 103 107 112 113 117 120 125 140 142 143 149 158 161 167 171 174 176 187 190 192 195 198 200 206 207 211 217 222 226 227 231 233 239 240 241 245 247 249 264 274 275 276 277 280 288 290 296 303 305 312 321 329 333 336 338 339 341 3...
output:
Yes 226834 1 15134 1 30430 1 58468 1 20972 2 47921 2 4710 2 13749 2 71317 2 1584 2 92792 2 95424 2 309 2 81673 3 13876 4 1 4 2 4 3 6 5 6 90079 6 59182 6 42488 6 26403 6 52305 7 20996 7 25838 7 84934 7 63453 7 77815 7 45046 7 77015 7 87339 7 31405 7 89061 7 29539 7 73866 7 72444 7 26627 7 78741 7 363...
result:
ok n=100000
Test #12:
score: 0
Accepted
time: 74ms
memory: 18128kb
input:
100000 1 2 4 5 6 7 10 13 14 15 16 20 21 22 24 25 26 28 29 30 31 33 34 35 36 37 38 39 40 43 44 45 46 47 48 51 52 55 56 57 58 59 62 65 66 67 68 69 70 71 72 73 74 75 76 77 78 80 81 82 85 87 89 91 92 93 94 97 98 99 100 101 102 103 104 105 106 107 111 112 113 115 117 119 120 121 123 124 128 130 132 133 1...
output:
Yes 84365 3 71695 3 17561 4 3 9 8 10 15024 10 9 11 12141 12 11 12 98511 13 29204 13 12 13 50166 13 72446 13 8377 13 25354 13 52274 13 95895 18 17 18 13280 18 47386 18 19928 19 39683 19 11292 20 18 20 19 24 23 26 47772 26 33568 26 47561 28 27 33 18117 33 32 35 68646 35 86483 35 76958 39 63164 42 41 4...
result:
ok n=100000
Test #13:
score: 0
Accepted
time: 103ms
memory: 19208kb
input:
100000 33 43 47 65 67 82 88 95 96 113 130 133 140 232 262 266 282 286 298 299 303 324 326 342 352 354 356 359 362 363 364 369 392 398 408 435 442 454 460 489 508 518 537 556 572 574 580 592 613 616 629 650 652 674 684 718 721 724 732 734 801 809 819 831 845 853 856 878 879 895 897 935 946 956 958 96...
output:
Yes 315421 1 48701 1 64105 1 95137 1 67267 1 23047 1 7918 1 50324 1 93515 1 57774 1 25793 1 30645 1 8883 1 9495 1 2485 1 4590 1 69994 1 44813 1 71824 1 67768 1 19077 1 13193 1 92229 1 16408 1 17942 1 42616 1 13685 1 35879 1 95335 1 24268 1 18863 1 48706 1 92761 1 47548 1 94520 1 1794 1 68105 1 27034...
result:
ok n=100000
Test #14:
score: 0
Accepted
time: 87ms
memory: 19648kb
input:
100000 38535 3433 18670 53850 31420 79252 3155 90709 7043 47690 20905 66663 16655 77812 19606 78158 23549 54025 44700 24119 42542 85555 31117 68856 35627 37419 26767 46031 72252 71511 80835 47732 77030 61434 51792 98165 71334 70644 79996 87007 93335 56112 86306 3040 10776 30683 80961 96794 12323 656...
output:
Yes 299970 1 5106 1 99465 1 16325 1 80449 1 19532 1 86664 1 6547 1 21827 1 16642 1 11886 1 78860 1 90794 1 42657 1 36689 1 63947 1 91386 1 55976 1 23520 1 15860 1 48111 1 9550 1 6281 1 8787 1 58618 1 67283 1 68087 1 62242 1 40263 1 72104 1 43972 1 34077 1 45670 1 92599 1 71105 1 64573 1 36071 1 4254...
result:
ok n=100000
Test #15:
score: 0
Accepted
time: 71ms
memory: 18896kb
input:
100000 1 5 7 8 24 29 32 36 39 41 43 44 46 47 52 54 56 58 59 64 68 69 70 73 75 77 79 82 84 86 88 90 92 93 95 98 99 101 102 104 105 108 112 114 115 116 118 123 126 127 128 133 134 139 140 143 145 147 152 153 154 156 160 161 163 165 169 170 176 178 179 180 184 186 187 188 192 193 195 199 200 204 205 20...
output:
No
result:
ok n=100000
Test #16:
score: 0
Accepted
time: 65ms
memory: 19236kb
input:
100000 1 3 4 7 10 11 13 17 18 19 21 22 25 27 28 29 31 35 36 37 38 42 49 50 53 56 57 58 60 62 63 64 68 70 71 79 80 82 83 85 86 87 88 90 93 94 98 103 105 109 110 111 112 116 121 123 127 134 138 139 142 143 148 151 154 156 158 159 160 162 164 166 168 171 172 173 174 175 176 177 180 184 186 187 189 193 ...
output:
No
result:
ok n=100000
Test #17:
score: 0
Accepted
time: 67ms
memory: 18348kb
input:
100000 1 2 8 9 11 14 19 21 22 24 25 28 33 34 35 36 43 49 51 55 57 59 62 64 68 69 70 71 72 75 76 78 79 80 81 82 83 87 88 89 91 92 98 99 105 106 107 111 112 116 118 123 124 125 128 131 133 138 139 141 142 143 146 147 152 154 155 159 161 162 163 164 165 169 172 173 174 175 179 183 184 185 186 187 190 1...
output:
No
result:
ok n=100000
Test #18:
score: 0
Accepted
time: 67ms
memory: 18876kb
input:
100000 60 134 140 182 208 256 291 327 364 395 404 419 439 444 457 469 486 510 527 561 569 595 611 612 645 654 710 778 792 794 810 832 873 890 900 901 911 914 942 946 978 1022 1057 1060 1083 1094 1095 1146 1154 1155 1280 1323 1336 1368 1379 1388 1395 1480 1500 1509 1548 1573 1580 1597 1601 1622 1629 ...
output:
No
result:
ok n=100000
Test #19:
score: 0
Accepted
time: 74ms
memory: 19144kb
input:
100000 52072 2 3 50731 5 75525 49404 8 52753 2744 11 34189 13 48355 15 16 17 50376 86416 20 21 56114 23 20072 25 53838 48273 63338 29 30 60156 6205 8084 34 35 36 48381 71655 72484 63969 88506 59722 27083 5369 44672 86160 39926 48 49 8962 51 47113 53 69142 55 66271 24245 74454 59 72556 61 35930 86895...
output:
No
result:
ok n=100000
Test #20:
score: 0
Accepted
time: 46ms
memory: 15420kb
input:
100000 13821 33496 19412 85158 61916 61576 41795 39637 42402 12256 37931 7198 19499 24983 15918 19942 56948 7239 17886 24328 17628 63213 4681 90112 37749 17984 25778 75577 33274 43479 47779 64385 77793 82833 15116 96895 87829 30340 25506 7179 48585 77809 44101 91839 93597 69594 37840 3271 4541 68178...
output:
No
result:
ok n=100000
Test #21:
score: 0
Accepted
time: 0ms
memory: 5628kb
input:
1 1 1
output:
Yes 0
result:
ok n=1
Extra Test:
score: 0
Extra Test Passed