1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 42 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,a,n) for (ll i=a;i<(ll)n;i++) #define per(i,a,n) for (ll i=n;i-->(ll)a;) ll read() { ll r;scanf("%lld", &r);return r; } template <typename T, typename S> class LCT { struct node { int pa; int ch[2]; bool flip; T v; T prod; T rprod; S sv; S vir; S sub; node() : pa{ 0 }, ch{ 0, 0 }, flip{ false }, v{}, prod{}, rprod{}, sv{}, sub{}, vir{} {} }; #define cur o[u] #define lc cur.ch[0] #define rc cur.ch[1] vector<node> o; bool isRoot(int u) const { return o[cur.pa].ch[0] != u && o[cur.pa].ch[1] != u; } bool direction(int u) const { return o[cur.pa].ch[1] == u && !isRoot(u); } void down(int u) { if (!cur.flip) return; for (int c : {lc, rc}) setFlip(c); cur.flip = false; } void up(int u) { cur.prod = o[lc].prod * cur.v * o[rc].prod; cur.rprod = o[rc].rprod * cur.v * o[lc].rprod; cur.sub = cur.vir + o[lc].sub + o[rc].sub + cur.sv; } void setFlip(int u) { if (!u) return; swap(lc, rc); swap(cur.prod, cur.rprod); cur.flip ^= 1; } void cp(int c, int p, int d) { if (c) o[c].pa = p; if (p) o[p].ch[d] = c; } void rotate(int u) { int f = cur.pa; assert(f); int g = o[f].pa; int l = direction(u); int c = cur.ch[l ^ 1]; if (!isRoot(f)) o[g].ch[direction(f)] = u; cur.pa = g; cp(f, u, l ^ 1); cp(c, f, l); up(f); } void update(int u) { if (!isRoot(u)) update(cur.pa); down(u); }; void splay(int u) { update(u); while (!isRoot(u)) { int f = cur.pa; if (!isRoot(f)) rotate(direction(u) == direction(f) ? f : u); rotate(u); } up(u); } void access(int x) { for (int u = x, last = 0; u; u = cur.pa) { splay(u); cur.vir = cur.vir + o[rc].sub - o[last].sub; rc = last; up(last = u); } splay(x); } int findRoot(int u) { int la = 0; for (access(u); u; u = lc) down(la = u); return la; } void split(int x, int y) { makeRoot(x); access(y); } void makeRoot(int u) { access(u); setFlip(u); } public: LCT(int n = 0) : o(n + 1) {} void setVal(int u, const T& v) { splay(++u); cur.v = v; up(u); } void setSval(int u, const S& v) { access(++u); cur.sv = v; up(u); } T query(int x, int y) { split(++x, ++y); return o[y].prod; } S subtree(int p, int u) { makeRoot(++p); access(++u); return cur.vir + cur.sv; } bool connected(int u, int v) { return findRoot(++u) == findRoot(++v); } void link(int x, int y) { makeRoot(++x); access(++y); o[y].vir = o[y].vir + o[x].sub; up(o[x].pa = y); } void cut(int x, int y) { split(++x, ++y); if (o[y].ch[0] == x) { o[y].ch[0] = o[x].pa = 0; up(y); } } #undef cur #undef lc #undef rc };
void luogu3690() { struct nodev { long long value; nodev(long long v = 0) : value(v) {} nodev operator*(const nodev& v0) { return nodev(value ^ v0.value); }; }; int n = read(); int m = read();
auto lct = LCT<nodev, int>(n); rep(i, 0, n) { int v = read(); lct.setVal(i, nodev(v)); } while (m-- > 0) { int op = read(); int x = read(); int y = read(); if (op == 0) { printf("%lld\n", lct.query(--x, --y).value); } else if (op == 1) { if (!lct.connected(--x, --y)) lct.link(x, y); } else if (op == 2) { lct.cut(--x, --y); } else if (op == 3) { lct.setVal(--x, y); } } }
void cf2097e() { struct nodev { ll value; nodev(ll v = 0) : value(v) {} nodev operator*(const nodev& v0) { return nodev(value + v0.value); }; }; int t = read(); while (t-- > 0) { int n = read(); int d = read(); vector<pair<ll, int>> avi(n + 1); rep(i, 1, n + 1) avi[i] = { read(),i - 1 }; sort(begin(avi), end(avi)); auto lct = LCT<nodev, int>(n + 1); rep(i, 0, n) lct.link(i, i + 1); ll ans = 0; per(j, 1, n + 1) { auto [v, i] = avi[j]; lct.setVal(i, nodev(1)); lct.cut(i, i + 1); lct.link(i, min(i + d, n)); ans += (avi[j].first - avi[j - 1].first) * lct.query(0, n).value; } printf("%lld\n", ans); } }
int main() { cf2097e(); return 0; }
|