QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449983#7760. 化学实验Made_in_Code20 529ms18000kbC++143.7kb2024-06-21 21:40:162024-06-21 21:40:16

Judging History

This is the latest submission verdict.

  • [2024-06-21 21:40:16]
  • Judged
  • Verdict: 20
  • Time: 529ms
  • Memory: 18000kb
  • [2024-06-21 21:40:16]
  • Submitted

answer

#include <iostream>

using namespace std;

const int kMaxN = 5e5 + 1;
struct V {
  int mn, c, t;
  int f, s[2];
  bool b;
} v[kMaxN];
int u, n, m;

void TagB(int x) {
  v[x].b ^= 1, swap(v[x].s[0], v[x].s[1]);
}

void TagC(int x, int y) {
  v[x].c += y, v[x].t += y;
}

void Pushdown(int x) {
  if (v[x].b) {
    TagB(v[x].s[0]), TagB(v[x].s[1]), v[x].b = 0;
  }
  if (v[x].t) {
    TagC(v[x].s[0], v[x].t);
    TagC(v[x].s[1], v[x].t);
    v[x].t = 0;
  }
}

void Pushup(int x) {
  v[x].mn = x;
  v[x].s[0] && (v[x].mn = min(v[x].mn, v[v[x].s[0]].mn));
  v[x].s[1] && (v[x].mn = min(v[x].mn, v[v[x].s[1]].mn));
}

int Son(int x) {
  return (v[v[x].f].s[0] == x) + (v[v[x].f].s[1] == x) * 2 - 1;
}

void RePush(int x) {
  ~Son(x) ? RePush(v[x].f) : void(), Pushdown(x);
}

void Rotate(int x, bool o) {
  int y = v[x].s[!o], s = Son(x);
  v[x].s[!o] = v[y].s[o], v[y].s[o] = x;
  v[y].f = v[x].f, v[x].f = y;
  v[v[x].s[!o]].f = x, ~s && (v[v[y].f].s[s] = y);
  Pushup(x), Pushup(y);
}

void Splay(int x) {
  RePush(x);
  while (~Son(x)) {
    int s = Son(x), t = Son(v[x].f);
    if (~t) {
      Rotate(s == t ? v[v[x].f].f : v[x].f, !s);
      Rotate(v[x].f, !t);
    } else {
      Rotate(v[x].f, !s);
    }
  }
}

void Access(int x) {
  for (int y = 0; x; y = x, x = v[x].f) {
    Splay(x), v[x].s[1] = y, Pushup(x);
  }
}

void MakeRoot(int x) {
  Access(x), Splay(x), TagB(x);
}

int FindTop(int x) {
  Splay(x);
  for (; v[x].s[0];) {
    x = v[x].s[0], Pushdown(x);
  }
  return Splay(x), x;
}

int FindRoot(int x) {
  return Access(x), FindTop(x);
}

void Link(int x, int y) {
  x > y ? swap(x, y) : void();
  int z = FindRoot(y);
  if (FindRoot(x) != z) {
    MakeRoot(x), v[x].f = y, MakeRoot(z);
    Access(y), Splay(y), TagC(y, v[x].c);
  }
}

bool Exist(int x, int y) {
  MakeRoot(x), Access(y), Splay(x);
  return v[x].s[1] == y && !v[y].s[0];
}

void Cut(int x, int y) {
  x > y ? swap(x, y) : void();
  int z = FindRoot(y);
  if (Exist(x, y)) {
    v[x].s[1] = v[y].f = 0, Pushup(x);
    MakeRoot(z);
    Access(y), Splay(y), TagC(y, -v[x].c);
  } else {
    MakeRoot(z);
  }
}

int FindFa(int x) {
  Splay(x);
  for (x = v[x].s[0]; v[x].s[1]; x = v[x].s[1]) {
    Pushdown(x);
  }
  return Splay(x), x;
}

int FindGap(int x, int y) {
  Pushdown(x);
  if (v[x].s[0] && v[v[x].s[0]].mn <= y) {
    return FindGap(v[x].s[0], y);
  } else if (x <= y) {
    return x;
  }
  return FindGap(v[x].s[1], y);
}

void Merge(int x, int y) {
  int topx, topy, _x, _y, f, mid;
  Access(x), topx = FindTop(x);
  if (topx == FindTop(y)) {
    // cout << "Del " << x << ' ' << y << '\n';
    return;
  }
  Access(y), topy = FindTop(y);
  if (FindTop(x) == topy) {
    // cout << "Del " << x << ' ' << y << '\n';
    return;
  }
  _x = FindTop(x), Access(f = v[_x].f), _y = FindTop(y);
  if (topx == topy) {
    _x < _y ? swap(_x, _y), swap(x, y) : void();
    Cut(_y, f), Access(x), Access(y);
  }
  for (; Splay(_x), v[_x].mn < _y;) {
    _x = FindGap(_x, _y);
    if (~Son(_x)) {
      mid = FindFa(_x), Cut(mid, _x), Link(_y, mid);
      Access(x), Access(y);
    }
    swap(_x, _y), swap(x, y);
  }
  Link(x, _y);
}

int Ask(int x, int y) {
  x = FindGap(FindRoot(x), y);
  return Access(x), v[x].c;
}

int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(0);
  for (int i = 0; i < kMaxN; i++) {
    v[i].mn = i, v[i].c = 1;
  }
  cin >> u >> n >> m;
  for (int i = 1, o, x, y, p = 0; i <= m; i++) {
    cin >> o >> x >> y;
    if (u) {
      x = (x + p - 1) % n + 1, y = (y + p - 1) % n + 1;
    }
    if (o == 1) {
      Merge(x, y);
    } else {
      cout << Ask(x, y) << '\n';
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 32ms
memory: 17376kb

input:

1 7500 7500
1 263 1446
1 6338 3037
1 5651 6129
1 572 3137
1 3159 5472
1 6038 4451
1 5988 5462
1 3873 1562
1 3516 5142
1 3375 2376
1 5832 1884
1 6243 3066
1 4001 6195
1 5301 6851
1 4382 2910
1 5299 562
1 452 335
1 3459 814
1 6681 6391
1 5816 4975
1 2244 1118
1 1410 1067
1 331 6324
1 6305 1294
1 4251 ...

output:

3984

result:

ok single line: '3984'

Test #2:

score: -10
Wrong Answer
time: 4ms
memory: 17328kb

input:

1 750 7500
1 424 707
1 405 537
2 19 26
2 365 365
2 6 11
1 695 549
1 579 661
2 682 687
1 621 586
2 446 453
2 562 567
2 534 537
2 509 515
2 109 113
2 112 114
2 46 54
2 736 746
2 355 363
2 706 709
2 526 529
2 40 48
2 80 83
2 684 689
2 479 480
2 320 323
2 74 76
2 170 180
1 472 559
2 125 128
1 426 717
2 ...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
2
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
...

result:

wrong answer 14th lines differ - expected: '2', found: '1'

Subtask #2:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 44ms
memory: 17348kb

input:

1 5000 100000
2 872 876
1 2895 4566
1 2676 1220
2 1852 1856
2 4153 4153
2 3675 3685
2 1489 1493
2 2782 2784
2 206 207
2 555 560
2 4149 4157
2 1875 1885
2 364 374
2 8 17
2 746 754
2 4785 4786
2 2394 2394
2 3386 3389
2 365 373
2 2290 2296
2 1419 1428
2 3651 3659
2 1922 1927
2 4877 4882
2 2597 2599
2 4...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

wrong answer 1766th lines differ - expected: '1', found: '2'

Subtask #3:

score: 20
Accepted

Test #21:

score: 20
Accepted
time: 529ms
memory: 18000kb

input:

0 100000 100000
1 29135 32144
1 58340 30601
1 68869 18606
1 73019 84578
1 13050 79881
1 22773 20030
1 74542 28744
1 46491 64238
1 26985 17174
1 93308 48003
1 90547 4510
1 18373 35069
1 34019 14080
1 13461 19407
1 33811 60169
1 22131 76457
1 88085 38979
1 49749 20241
1 90505 42660
1 25889 75426
1 420...

output:

80930

result:

ok single line: '80930'

Test #22:

score: 0
Accepted
time: 120ms
memory: 17332kb

input:

0 10000 100000
1 6042 9322
1 5723 6899
2 2207 2214
2 7557 7567
2 7648 7658
2 3150 3156
2 7555 7560
2 9657 9661
2 5681 5686
2 5736 5744
1 9993 9001
2 6887 6893
2 5765 5765
2 7983 7987
2 2427 2433
2 8236 8245
1 5381 8258
2 7503 7513
2 236 244
2 816 816
2 5139 5147
1 9243 6698
2 8713 8718
2 4569 4571
2...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 80000 lines

Test #23:

score: 0
Accepted
time: 80ms
memory: 17348kb

input:

0 20000 100000
2 19051 19059
2 11055 11065
2 1238 1244
2 13935 13939
2 5561 5569
2 12222 12232
1 19498 16106
2 15732 15739
2 13935 13944
2 357 359
2 4162 4166
2 13885 13894
2 175 185
1 17668 12969
2 2028 2036
1 19277 16172
2 13017 13018
1 11178 15138
2 1432 1439
1 10356 19031
2 13481 13488
1 19721 1...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 80000 lines

Test #24:

score: 0
Accepted
time: 190ms
memory: 17400kb

input:

0 20000 100000
1 16630 15229
2 5468 5471
1 10875 19665
2 13264 13272
2 19524 19529
1 10585 14283
1 16911 18952
1 13938 19032
1 12349 17734
2 12134 12135
2 19637 19641
2 10440 10448
1 19266 15489
2 16764 16772
2 1038 1044
1 17444 16671
2 8206 8206
1 19664 14689
1 15060 11016
2 13510 13513
1 17044 156...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 33333 lines

Test #25:

score: 0
Accepted
time: 122ms
memory: 17316kb

input:

0 50000 100000
2 49987 49987
1 43787 46393
1 37151 42291
1 31096 33599
2 1752 1755
2 4467 4477
2 21321 21326
2 34625 34633
1 40544 26327
2 31100 31103
1 31751 30971
2 22519 22522
2 42769 42770
1 40110 39451
1 48495 29422
1 35693 27838
1 42250 46507
2 21102 21109
1 41450 41990
2 27916 27920
2 41251 4...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 50000 lines

Test #26:

score: 0
Accepted
time: 169ms
memory: 17272kb

input:

0 100000 100000
1 96907 76199
2 87440 87450
1 87657 58774
1 65732 61745
1 93781 75145
1 73765 50447
1 72180 77794
1 94918 79638
1 65681 86609
1 71503 52788
1 72114 68639
1 90261 61021
1 61887 52644
1 69857 94793
1 85125 55713
1 68748 61829
1 91118 98694
1 51565 68902
1 56201 71518
1 56652 72447
1 51...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 10000 lines

Test #27:

score: 0
Accepted
time: 165ms
memory: 17336kb

input:

0 50000 100000
2 44733 44737
1 1 12831
2 17267 17276
1 9120 6077
2 23023 23026
2 6266 6269
1 4 14323
1 1 35986
1 3 602
2 24393 24397
1 2 1467
1 4 1322
2 39495 39498
1 4 42577
1 2 914
2 34112 34112
1 1 47749
1 2 31401
1 3 35303
1 3 15033
1 3 10892
2 13562 13563
2 21586 21592
2 48602 48611
2 29426 294...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 50000 lines

Test #28:

score: 0
Accepted
time: 235ms
memory: 17320kb

input:

0 100000 100000
1 3 53441
1 3 83517
1 1 49395
1 2 53764
1 1 54305
1 4 70526
1 1 43861
1 4 41652
1 4 28430
1 2 35231
1 3 22871
1 3 83810
1 1 2017
1 17943 41941
1 4 81928
1 3 34752
1 3 92374
1 73103 63750
1 1 42561
1 3 6165
1 2373 45196
1 4 10206
1 59937 81463
1 3 68202
1 1 30942
1 1 84178
1 4 95641
1...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
167
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 10000 lines

Test #29:

score: 0
Accepted
time: 92ms
memory: 17404kb

input:

0 100000 100000
1 52043 59717
1 52808 86974
2 54185 97792
2 43516 80690
2 29461 98053
1 92581 83816
2 69780 88954
1 70925 52631
2 25721 96440
2 63749 85215
2 44590 87986
1 81989 73590
1 71035 54298
1 79204 72678
1 62997 70643
1 57968 95209
2 13661 82587
2 88967 99297
1 87464 59759
1 59322 67510
1 57...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 33333 lines

Test #30:

score: 0
Accepted
time: 160ms
memory: 17336kb

input:

0 100000 100000
1 56697 75261
2 1785 84690
1 74364 95041
1 72445 92573
1 85072 93572
1 66568 89422
1 89923 72016
1 84302 93568
1 68910 78225
1 72195 53561
1 55718 87993
1 56377 50717
1 82674 85604
1 56554 67457
1 70575 69354
1 88461 63312
1 96199 85948
1 50907 70476
2 74668 88401
2 20432 92996
1 595...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 10000 lines

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Time Limit Exceeded

Dependency #3:

100%
Accepted

Test #41:

score: 0
Time Limit Exceeded

input:

0 500000 500000
1 153366 461301
1 402458 312431
1 24864 471768
1 423645 58443
1 106601 157640
1 136693 44542
1 290752 134249
1 425937 374427
1 125165 179248
1 335514 162511
1 255068 233664
1 334095 126185
1 487317 435567
1 206065 479388
1 219464 260165
1 385308 421655
1 277456 390877
1 279526 464427...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #4:

0%