QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#209912#3682. 长跑KHIN100 ✓210ms32320kbC++178.0kb2023-10-10 19:24:492023-10-10 19:24:50

Judging History

你现在查看的是最新测评结果

  • [2023-10-10 19:24:50]
  • 评测
  • 测评结果:100
  • 用时:210ms
  • 内存:32320kb
  • [2023-10-10 19:24:49]
  • 提交

answer

# include <bits/stdc++.h>

using namespace std;

namespace kh {
  constexpr int P(19'940'417);
  inline namespace src {
    template <typename T>
      constexpr auto cmin(T& a, T const& b) {
        if (b < a) return pair<T&, bool>(a = b, true);
        else return pair<T&, bool>(a, false);
      }
    template <typename T>
      constexpr auto cmax(T& a, T const& b) {
        if (a < b) return pair<T&, bool>(a = b, true);
        else return pair<T&, bool>(a, false);
      }
    template <typename T>
      constexpr auto cminmax(T& a, T& b) {
        bool const swp(b < a ? swap(a, b), true : false);
        return make_pair(pair<T&, T&>(a, b), swp);
      }
    template <typename T, class Compare>
      constexpr auto cmin(T& a, T const& b, Compare comp) {
        if (comp(b, a)) return pair<T&, bool>(a = b, true);
        else return pair<T&, bool>(a, false);
      }
    template <typename T, class Compare>
      constexpr auto cmax(T& a, T const& b, Compare comp) {
        if (comp(a, b)) return pair<T&, bool>(a = b, true);
        else return pair<T&, bool>(a, false);
      }
    template <typename T, class Compare>
      constexpr auto cminmax(T& a, T& b, Compare comp) {
        bool const swp(comp(b, a) ? swap(a, b), true : false);
        return make_pair(pair<T&, T&>(a, b), swp);
      }
    template <typename T>
      constexpr int sgn(T const x)
      { return (x > 0) - (x < 0); }
    template <typename T>
      struct frac {
        T num, den;
        constexpr frac(T const n = 0, T const d = 1)
          : num(n / __gcd(n, d)), den(d / __gcd(n, d))
        { assert(den), num *= sgn(den), den *= sgn(den); }
        constexpr frac& operator+=(frac const b) { return *this = *this + b; }
        constexpr frac& operator-=(frac const b) { return *this = *this - b; }
        constexpr frac& operator*=(frac const b) { return *this = *this * b; }
        constexpr frac& operator/=(frac const b) { return *this = *this / b; }
        constexpr frac& operator++() { return num += den, *this; }
        constexpr frac& operator--() { return num -= den, *this; }
        constexpr frac operator++(int) { return num += den, *this - 1; }
        constexpr frac operator--(int) { return num -= den, *this - 1; }
        constexpr frac operator+() const { return frac(+num, den); }
        constexpr frac operator-() const { return frac(-num, den); }
        friend constexpr frac operator+(frac const a, frac const b)
        { return frac(a.num * b.den + b.num * a.den, a.den * b.den); }
        friend constexpr frac operator-(frac const a, frac const b)
        { return frac(a.num * b.den - b.num * a.den, a.den * b.den); }
        friend constexpr frac operator*(frac const a, frac const b)
        { return frac(a.num * b.num, a.den * b.den); }
        friend constexpr frac operator/(frac const a, frac const b)
        { return frac(a.num * b.den, a.den * b.num); }
        friend constexpr bool operator==(frac const a, frac const b)
        { return a.num * b.den == b.num * a.den; }
        friend constexpr bool operator!=(frac const a, frac const b)
        { return a.num * b.den != b.num * a.den; }
        friend constexpr bool operator<(frac const a, frac const b)
        { return a.num * b.den < b.num * a.den; }
        friend constexpr bool operator>(frac const a, frac const b)
        { return a.num * b.den > b.num * a.den; }
        friend constexpr bool operator<=(frac const a, frac const b)
        { return a.num * b.den <= b.num * a.den; }
        friend constexpr bool operator>=(frac const a, frac const b)
        { return a.num * b.den >= b.num * a.den; }
      };
    constexpr int pow(int a, int n) {
      int r(1);
      while (n) {
        r = 1l * r * (n & 1 ? a : 1) % P;
        a = 1l * a * a % P, n >>= 1;
      }
      return r;
    }
    template <typename T, int N>
      class fenwick1 {
        T v[N + 1];
        public:
        constexpr fenwick1() : v{} {}
        constexpr void add(int x, T const y)
        { while (x <= N) v[x] += y, x += x & -x; }
        constexpr T sum(int x) const
        { T r(0); while (x) r += v[x], x &= x - 1; return r; }
        constexpr T sum(int const l, int const r) const
        { return sum(r) - sum(l); }
        constexpr T at(int const x) const
        { return sum(x) - sum(x - 1); }
      };
  }
  constexpr int N(150'000);
  constexpr int M(750'000);
  class dsuk {
    int mo[N + 1];
    int sz[N + 1];
    public:
    void init(int const n) {
      iota(mo + 1, mo + n + 1, 1);
      fill(sz + 1, sz + n + 1, 1);
    }
    int find(int const x)
    { return mo[x] == x ? x : mo[x] = find(mo[x]); }
    bool merge(int x, int y) {
      if ((x = find(x)) == (y = find(y))) return false;
      if (sz[x] < sz[y]) swap(x, y);
      return mo[y] = x, sz[x] += sz[y], sz[y] = 0, true;
    }
  };
  class dsuc {
    int mo[N + 1];
    public:
    void init(int const n) { iota(mo + 1, mo + n + 1, 1); }
    int find(int const x) { return mo[x] == x ? x : mo[x] = find(mo[x]); }
    void merge(int const x, int const y) { mo[find(x)] = find(y); }
  };
  struct event { int p, a, b; bool mst; };
  struct node { vector<int> n; int l, r, s, m, g; int v; };
  int n, m, idx;
  int a[N + 1];
  event e[M + 1];
  node v[N + 1];
  dsuk uk;
  dsuc uc;
  fenwick1<int, N + 1> bit;
  void se0(int const x, int const mo) {
    v[x].l = idx, v[x].s = 1, v[x].m = mo;
    for (int const y : v[x].n) if (y != mo)
      se0(y, x), v[x].s += v[y].s;
    v[x].r = ++idx;
  }
  void se1(int const x, int const mo) {
    v[x].g = v[x].g ? v[x].g : x;
    int d(0);
    for (int const y : v[x].n) if (y != mo)
      d = v[y].s > v[d].s ? y : d;
    if (d) v[d].g = v[x].g, se1(d, x);
    for (int const y : v[x].n)
      if (y != mo && y != d) se1(y, x);
  }
  inline int lca(int x, int y) {
    while (v[x].g != v[y].g) {
      if (v[v[x].g].r < v[v[y].g].r) x = v[v[x].g].m;
      if (v[v[y].g].r < v[v[x].g].r) y = v[v[y].g].m;
    }
    return v[x].r < v[y].r ? y : x;
  }
  inline void merge(int const x) {
    int const val(v[x].v);
    bit.add(v[x].l + 1, -val);
    bit.add(v[x].r + 1, +val);
    bit.add(v[uc.find(v[x].m)].l + 1, +val);
    bit.add(v[uc.find(v[x].m)].r + 1, -val);
    v[uc.find(v[x].m)].v += v[x].v, v[x].v = 0;
    uc.merge(x, v[x].m);
  }
  void main() {
    cin >> n >> m, uk.init(n);
    for (int i(1); i <= n; ++i) cin >> a[i];
    for (int i(1); i <= m; ++i) cin >> e[i].p >> e[i].a >> e[i].b;
    for (int i(1); i <= m; ++i)
      if (e[i].p == 1 && uk.merge(e[i].a, e[i].b)) {
        e[i].mst = true;
        v[e[i].a].n.push_back(e[i].b);
        v[e[i].b].n.push_back(e[i].a);
      }
    for (int i(1); i <= n; ++i)
      if (!v[i].s) se0(i, 0), se1(i, 0);
    uk.init(n), uc.init(n);
    for (int i(1); i <= n; ++i) {
      v[i].v = a[i];
      bit.add(v[i].l + 1, +a[i]);
      bit.add(v[i].r + 1, -a[i]);
    }
    for (int i(1); i <= m; ++i) {
      auto const [p, a, b, mst](e[i]);
      switch (p) {
        case 1:
          if (mst) uk.merge(a, b);
          else while (uc.find(a) != uc.find(b)) {
            int const x(uc.find(a));
            int const y(uc.find(b));
            if (v[x].r < v[y].r) merge(x);
            if (v[y].r < v[x].r) merge(y);
          }
          break;
        case 2:
          bit.add(v[uc.find(a)].l + 1, b - kh::a[a]);
          bit.add(v[uc.find(a)].r + 1, kh::a[a] - b);
          v[uc.find(a)].v += b - kh::a[a], kh::a[a] = b;
          break;
        case 3:
          if (uk.find(a) != uk.find(b))
          { cout << -1 << '\n'; break; }
          int ans(0);
          ans += bit.sum(v[a].r);
          ans += bit.sum(v[b].r);
          ans -= 2 * bit.sum(v[lca(a, b)].r);
          ans += v[uc.find(lca(a, b))].v;
          cout << ans << '\n';
          break;
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t(1);
  // cin >> t;
  for (int i(1); i <= t; ++i) kh::main();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 2ms
memory: 11876kb

input:

10 50
113 551 1129 1599 1711 3808 4413 488 4764 274
1 3 2
1 4 2
1 5 3
1 6 4
1 7 3
1 8 7
1 9 6
1 5 10
3 10 2
1 8 9
3 7 7
2 10 4252
2 6 4333
3 8 7
3 1 10
2 9 3685
3 8 8
2 4 1765
2 7 4566
2 6 2357
3 8 6
3 3 4
3 4 2
3 4 9
3 6 6
2 4 937
3 8 8
2 10 4577
3 1 9
3 10 3
3 5 3
1 8 9
2 7 2810
1 3 4
3 7 6
3 4 2
...

output:

3665
16752
17277
-1
16198
14541
14541
14541
14541
14541
13713
-1
20001
15424
11957
11957
17038
-1
17038
15518
15518
16787
19480
19480

result:

ok 24 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 15752kb

input:

100 500
4856 2004 556 4890 2978 4835 3595 4574 4688 3800 4468 1398 2781 4980 3958 2704 1735 3786 4108 3389 4803 4347 2788 609 606 3956 142 3040 1162 3489 2301 2099 2823 3802 2962 404 2752 4738 2434 4493 2276 2228 3454 2945 1796 480 4802 3309 1836 633 299 3134 1831 3306 1090 3810 3193 1149 4310 3945 ...

output:

35780
100538
59814
92045
36823
7286
-1
26926
23681
-1
34853
103012
77906
71155
72546
13819
38151
16899
36065
46850
25340
32978
-1
-1
-1
18875
-1
44846
13281
3040
60759
51863
72070
95143
43995
25429
13110
-1
104464
-1
71745
25139
32090
83799
90581
-1
12480
20022
41865
35015
54235
15731
37756
20942
70...

result:

ok 225 lines

Test #3:

score: 10
Accepted
time: 0ms
memory: 13716kb

input:

1000 5000
3009 1911 3959 1640 1893 2933 871 2103 1425 4600 2971 1755 4061 585 380 404 1614 2826 4991 3068 4531 2777 4755 1198 1305 1746 2957 3803 1111 3136 461 1681 3954 4663 4979 4228 82 4972 4686 4241 2733 2548 2113 876 801 3894 619 663 368 279 4751 369 1444 1761 1283 1781 2367 539 3579 4585 758 4...

output:

423442
577666
589690
580641
575945
589616
589619
585856
587324
581832
587471
580550
660441
580537
603750
35747
604021
580274
-1
686230
581162
656315
9459
607600
653076
662918
582547
639108
600043
-1
597851
599208
584895
6446
587582
582380
597922
642672
585115
-1
629544
-1
585331
586221
616981
585903...

result:

ok 2227 lines

Test #4:

score: 10
Accepted
time: 8ms
memory: 14424kb

input:

10000 50000
1941 817 1326 2520 3708 2520 4091 2906 429 2472 1406 1325 4680 125 1889 3899 4390 2085 1556 2722 2347 1386 4162 2008 4810 215 3588 1443 3407 697 2410 905 483 3309 1289 3497 1837 3465 4721 3414 4607 1780 4951 1894 3360 2439 200 3049 672 2877 3096 579 4474 2468 2847 1379 2788 1955 4532 297...

output:

-1
-1
400228
-1
-1
1906173
5646406
2398997
1889923
4427552
5179492
-1
423321
4314029
60083
1821263
-1
-1
2355010
419617
-1
1016687
1657286
3044088
3582628
2288
4972454
2654928
4397262
523795
679927
-1
-1
2929
1039
5187006
-1
1664524
1409952
103586
2921724
2753850
7070153
2920503
367547
-1
5400984
29...

result:

ok 22051 lines

Test #5:

score: 10
Accepted
time: 30ms
memory: 17040kb

input:

20000 100000
948 1740 3561 3170 2161 67 834 1690 4479 4341 2232 4466 366 3402 3888 660 1814 3679 4803 3909 3831 3512 3709 4042 4160 2015 4625 2629 321 4170 1061 2920 2745 635 2915 3594 4278 2694 3118 2580 3245 4345 3584 2735 1049 529 817 1778 3258 229 4684 1225 1944 875 1356 109 438 2055 3146 1505 7...

output:

5437236
1534771
1991399
13663398
6276075
5521098
-1
596021
13474903
-1
6504849
3747417
11793909
7029941
-1
1413068
7435613
236976
866462
3398349
-1
6916760
2785985
-1
5693717
1484291
173570
7556539
2658169
-1
11051669
1624533
1341027
4045144
2542
-1
-1
12346367
4166235
3410322
4202315
3152786
770331...

result:

ok 44144 lines

Test #6:

score: 10
Accepted
time: 72ms
memory: 19024kb

input:

50000 250000
1192 3143 2362 84 1049 132 2427 300 1924 734 3787 4116 2236 3181 1060 3491 1616 984 804 419 4404 1892 1989 1705 972 1077 3980 3573 3297 990 2106 778 727 4626 2642 4574 1197 1736 4131 4013 4349 426 4653 3775 3671 4824 3523 1965 1068 4798 4417 658 4026 2142 2063 1181 1245 759 3082 1670 49...

output:

1679
9870782
12548353
-1
-1
8202395
4875948
1933873
6788334
26645768
-1
1967084
10835038
3722292
12349172
-1
29208444
-1
4085239
1839188
29045001
11894585
18482106
15516716
-1
3107463
1516788
9790577
537111
17695616
15101810
9694745
3439564
1127260
11253932
17678526
-1
1942252
27077575
-1
16181624
1...

result:

ok 109942 lines

Test #7:

score: 10
Accepted
time: 137ms
memory: 27588kb

input:

100000 500000
639 1940 2592 85 2346 3238 4960 1770 2612 878 2821 802 4193 2643 3331 898 4815 1120 1739 2458 256 1526 3736 914 2334 1801 833 1823 2571 2288 2952 960 2894 3916 1483 2851 2763 923 4313 1699 657 2044 4126 3136 4101 1410 4778 2682 842 1169 319 999 2110 1384 426 2946 151 2640 1147 3040 190...

output:

41333900
50251062
47819444
25720802
10387825
1771146
-1
41632939
4855693
55115387
3869626
47119979
29430870
9727775
56850715
20990197
18425175
17176893
22166675
19232826
13884703
39009843
-1
42363622
9655919
-1
897
8208898
52460256
50094525
-1
12983827
-1
17173904
29597472
61344774
30613689
17176980...

result:

ok 220231 lines

Test #8:

score: 10
Accepted
time: 122ms
memory: 25480kb

input:

100000 500000
523 1379 3217 1678 236 2725 2322 2492 1551 1329 0 2789 3557 4525 137 2578 509 822 4344 3663 40 107 3782 2542 3547 174 4308 1512 1979 1025 280 1137 1483 152 1404 1775 4338 4522 1239 3664 4791 774 1738 4686 1298 1905 364 2021 797 955 45 1432 1520 1337 4700 4581 2377 3782 1269 3927 4064 1...

output:

-1
-1
5190174
244813
27635322
5918594
41531255
-1
17195822
16017485
63841941
7802178
14257314
25596927
32172647
65867224
1960176
-1
29360116
31603769
17747345
311791
5555439
414443
-1
23995211
31260168
13063694
16241418
-1
41710914
65057862
1871
-1
51454647
33216261
14831750
5528774
37766371
3929407...

result:

ok 220401 lines

Test #9:

score: 10
Accepted
time: 210ms
memory: 32320kb

input:

150000 750000
593 1752 778 4905 4156 2539 1759 67 4302 3126 4199 2304 4323 4584 507 320 4294 2394 4309 3131 763 138 3381 3986 4398 2133 3165 1868 2691 152 747 1898 1300 1434 794 1132 1250 100 2963 4881 292 1668 2111 552 1841 1221 2713 3810 1640 2668 3636 1455 2149 323 1262 4453 3470 632 1363 4454 16...

output:

-1
88401121
26424622
-1
78712640
55097774
67119689
57410824
53414390
-1
70958032
15373172
86499823
38942123
70449874
43048929
-1
-1
-1
-1
21790763
6076030
49924854
63538970
-1
-1
83744222
-1
73881058
46298
25998836
34658871
33990355
18694051
99986438
11143069
-1
15157562
28554200
10025726
12319487
8...

result:

ok 330076 lines

Test #10:

score: 10
Accepted
time: 205ms
memory: 32016kb

input:

150000 750000
850 4814 3401 2615 4454 492 3047 1519 3446 4473 4265 687 1634 355 4945 1888 3262 901 4995 4299 1364 3257 3521 4977 783 3848 2753 655 3402 1550 2071 1698 3039 92 3219 2568 3745 3444 1468 3323 4775 828 2782 1707 1566 4141 2407 1606 253 1565 4125 3057 4534 4361 2608 610 4857 2697 3330 188...

output:

92095009
15073805
60205872
70449501
49681013
36273545
-1
77963052
-1
5355313
56376690
13308713
41271273
40450618
-1
44125324
11207670
-1
65516905
88515523
63910529
72338195
49457142
26256203
71714106
-1
40449751
69280964
51006879
2423
41038044
-1
71594625
-1
65461508
2403646
54141780
82609468
-1
216...

result:

ok 330654 lines