diff --git a/LibreOJ/2005/2005.cpp b/LibreOJ/2005/2005.cpp new file mode 100644 index 00000000..e5ebd610 --- /dev/null +++ b/LibreOJ/2005/2005.cpp @@ -0,0 +1,202 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +struct node { + int l, r; + __int128 sum_x, sum_y, sum_xx, sum_xy; + __int128 lazy_s, lazy_t; + bool covered; + + node(const int &_l = 0, const int &_r = 0) + : l(_l), + r(_r), + sum_x(0), + sum_y(0), + sum_xx(0), + sum_xy(0), + lazy_s(0), + lazy_t(0), + covered(false) {} + + node operator+(const node &rhs) const { + node res; + + res.l = l; + res.r = rhs.r; + res.sum_x = sum_x + rhs.sum_x; + res.sum_y = sum_y + rhs.sum_y; + res.sum_xx = sum_xx + rhs.sum_xx; + res.sum_xy = sum_xy + rhs.sum_xy; + + return res; + } +} tr[N << 2]; + +int n, m, x[N], y[N]; + +__int128 square_sum(__int128 x) { + return x * (x + 1) * (2 * x + 1) / 6; +} + +__int128 square_sum(__int128 l, __int128 r) { + return square_sum(r) - square_sum(l - 1); +} + +/// Add (s, t) to each element under tr[u] +void modify_node(int u, __int128 s, __int128 t) { + tr[u].lazy_s += s; + tr[u].lazy_t += t; + + tr[u].sum_xx += 2 * s * tr[u].sum_x + s * s * (tr[u].r - tr[u].l + 1); + tr[u].sum_xy += t * tr[u].sum_x + s * tr[u].sum_y + s * t * (tr[u].r - tr[u].l + 1); + + tr[u].sum_x += s * (tr[u].r - tr[u].l + 1); + tr[u].sum_y += t * (tr[u].r - tr[u].l + 1); +} + +/// x[i] = y[i] = i +void change_node(int u) { + tr[u].lazy_s = tr[u].lazy_t = 0; + tr[u].sum_x = tr[u].sum_y = static_cast<__int128>(tr[u].l + tr[u].r) * (tr[u].r - tr[u].l + 1) / 2; + tr[u].sum_xx = tr[u].sum_xy = square_sum(tr[u].l, tr[u].r); + tr[u].covered = true; +} + +void pushup(int u) { + tr[u] = tr[u << 1] + tr[u << 1 | 1]; +} + +void pushdown(int u) { + if (tr[u].covered) { + change_node(u << 1); + change_node(u << 1 | 1); + + tr[u].covered = false; + } + + modify_node(u << 1, tr[u].lazy_s, tr[u].lazy_t); + modify_node(u << 1 | 1, tr[u].lazy_s, tr[u].lazy_t); + + tr[u].lazy_s = tr[u].lazy_t = 0; +} + +void build(int u, int l, int r) { + tr[u] = node(l, r); + + if (l == r) { + tr[u].sum_x = x[l]; + tr[u].sum_y = y[l]; + tr[u].sum_xx = static_cast<__int128>(x[l]) * x[l]; + tr[u].sum_xy = static_cast<__int128>(x[l]) * y[l]; + + return; + } + + int mid = l + r >> 1; + + build(u << 1, l, mid); + build(u << 1 | 1, mid + 1, r); + + pushup(u); +} + +void modify(int u, int l, int r, __int128 s, __int128 t) { + if (l <= tr[u].l && tr[u].r <= r) { + modify_node(u, s, t); + + return; + } + + int mid = tr[u].l + tr[u].r >> 1; + + pushdown(u); + + if (l <= mid) modify(u << 1, l, r, s, t); + if (r > mid) modify(u << 1 | 1, l, r, s, t); + + pushup(u); +} + +void change(int u, int l, int r, __int128 s, __int128 t) { + if (l <= tr[u].l && tr[u].r <= r) { + change_node(u); + modify_node(u, s, t); + + return; + } + + int mid = tr[u].l + tr[u].r >> 1; + + pushdown(u); + + if (l <= mid) change(u << 1, l, r, s, t); + if (r > mid) change(u << 1 | 1, l, r, s, t); + + pushup(u); +} + +node query(int u, int l, int r) { + if (l <= tr[u].l && tr[u].r <= r) return tr[u]; + + int mid = tr[u].l + tr[u].r >> 1; + node res; + + pushdown(u); + + if (l <= mid) res = res + query(u << 1, l, r); + if (r > mid) res = res + query(u << 1 | 1, l, r); + + return res; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= n; i++) { + cin >> x[i]; + } + + for (int i = 1; i <= n; i++) { + cin >> y[i]; + } + + build(1, 1, n); + + while (m--) { + int op, l, r; + + cin >> op >> l >> r; + + if (op == 1) { + auto res = query(1, l, r); + + double x = static_cast(res.sum_xy) - static_cast(res.sum_x) * res.sum_y / (r - l + 1), + y = static_cast(res.sum_xx) - static_cast(res.sum_x) * res.sum_x / (r - l + 1); + + cout << std::fixed << std::setprecision(10) << x / y << endl; + } else if (op == 2) { + int s, t; + + cin >> s >> t; + + modify(1, l, r, s, t); + } else { // op == 3 + int s, t; + + cin >> s >> t; + + change(1, l, r, s, t); + } + } + + return 0; +} diff --git a/LibreOJ/2005/data/relative.in b/LibreOJ/2005/data/relative.in new file mode 100644 index 00000000..3a8268f2 --- /dev/null +++ b/LibreOJ/2005/data/relative.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:20c229d2eb03162d5c41c01a4c311e96c6c58331af2962e0a67ef29b5cce9968 +size 61 diff --git a/LibreOJ/2005/data/relative.out b/LibreOJ/2005/data/relative.out new file mode 100644 index 00000000..76f69ae0 --- /dev/null +++ b/LibreOJ/2005/data/relative.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bc73fa6d510d182f7c550de7ddc7f7bd1b94a83a05eb7b3b8dddc530fa18ac9a +size 44 diff --git a/LibreOJ/2005/data/relative1.in b/LibreOJ/2005/data/relative1.in new file mode 100644 index 00000000..12f17a0f --- /dev/null +++ b/LibreOJ/2005/data/relative1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:20803e1d2fc3084e0368343bd4141f906c25c83e778e9a68e8d657a332a29ce4 +size 21821 diff --git a/LibreOJ/2005/data/relative1.out b/LibreOJ/2005/data/relative1.out new file mode 100644 index 00000000..e4cc9805 --- /dev/null +++ b/LibreOJ/2005/data/relative1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cb2f1e84d2e01832ac9b5be33cd5ad63e807db8f07eef33db1e9d1816fe7c8f0 +size 3202 diff --git a/LibreOJ/2005/data/relative10.in b/LibreOJ/2005/data/relative10.in new file mode 100644 index 00000000..9d705867 --- /dev/null +++ b/LibreOJ/2005/data/relative10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e8dc6da0ac4569b404ad5e1336752b900911a734fb2ab379a9cd4e61c3517291 +size 3476332 diff --git a/LibreOJ/2005/data/relative10.out b/LibreOJ/2005/data/relative10.out new file mode 100644 index 00000000..67333132 --- /dev/null +++ b/LibreOJ/2005/data/relative10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5e7547695e311bcc70b9fc249e219df4833a8abc24d3d92fc7bf075181adabdd +size 456859 diff --git a/LibreOJ/2005/data/relative2.in b/LibreOJ/2005/data/relative2.in new file mode 100644 index 00000000..fec369ef --- /dev/null +++ b/LibreOJ/2005/data/relative2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1ecc37643ea47a9f5975a885ecde48d5aaa952f8ca67b6f1b851d378432c1920 +size 29538 diff --git a/LibreOJ/2005/data/relative2.out b/LibreOJ/2005/data/relative2.out new file mode 100644 index 00000000..c9992a44 --- /dev/null +++ b/LibreOJ/2005/data/relative2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5be6931b7cb567e6cfcf4508f1bb7cf7772f75527d870d0d8277c209163779cb +size 4191 diff --git a/LibreOJ/2005/data/relative3.in b/LibreOJ/2005/data/relative3.in new file mode 100644 index 00000000..48edfd2b --- /dev/null +++ b/LibreOJ/2005/data/relative3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4b4f731188a3a46815b5a41f1b22fef002d7348a460d142e4a1bdc7805fceecb +size 2954268 diff --git a/LibreOJ/2005/data/relative3.out b/LibreOJ/2005/data/relative3.out new file mode 100644 index 00000000..45afc987 --- /dev/null +++ b/LibreOJ/2005/data/relative3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5240e1c30f594384dabda7367df05f30e55e2aab15aa09e5c6d7a9999fd20946 +size 663322 diff --git a/LibreOJ/2005/data/relative4.in b/LibreOJ/2005/data/relative4.in new file mode 100644 index 00000000..bbf96c76 --- /dev/null +++ b/LibreOJ/2005/data/relative4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3fd9d175be20aca329e6f1b653bf295625a341f4078e74705a927013b44df8ee +size 3133111 diff --git a/LibreOJ/2005/data/relative4.out b/LibreOJ/2005/data/relative4.out new file mode 100644 index 00000000..c44b84c6 --- /dev/null +++ b/LibreOJ/2005/data/relative4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2451166f0ca42f960734dfc810ee58e055682172e334731a4c77e80788c857f0 +size 703652 diff --git a/LibreOJ/2005/data/relative5.in b/LibreOJ/2005/data/relative5.in new file mode 100644 index 00000000..8ab14058 --- /dev/null +++ b/LibreOJ/2005/data/relative5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0246612f38d4ca42579282fb2db27b6d551fcd09a8a3318073adfbb5cbff5443 +size 3223476 diff --git a/LibreOJ/2005/data/relative5.out b/LibreOJ/2005/data/relative5.out new file mode 100644 index 00000000..b3d77103 --- /dev/null +++ b/LibreOJ/2005/data/relative5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:55642a2ec978bc6ef562bd6b99e377c1fd6f5fd2be4f121d51bdcba04b387d43 +size 692615 diff --git a/LibreOJ/2005/data/relative6.in b/LibreOJ/2005/data/relative6.in new file mode 100644 index 00000000..a2b4e660 --- /dev/null +++ b/LibreOJ/2005/data/relative6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:69864044b90719967723fc0771df1c3b6d8c8040c407edaed65d435b4dbae42f +size 3122320 diff --git a/LibreOJ/2005/data/relative6.out b/LibreOJ/2005/data/relative6.out new file mode 100644 index 00000000..9733c4fa --- /dev/null +++ b/LibreOJ/2005/data/relative6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:df5c3e4dfda2e696f9b6ab0dc741d8991fdddf891bea69e54278bad30818227f +size 652202 diff --git a/LibreOJ/2005/data/relative7.in b/LibreOJ/2005/data/relative7.in new file mode 100644 index 00000000..b2825887 --- /dev/null +++ b/LibreOJ/2005/data/relative7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:33e1a1d1adb7b046609644136ec5510f3ab60936d7c205cf8e23a0f4dbf31f20 +size 3116425 diff --git a/LibreOJ/2005/data/relative7.out b/LibreOJ/2005/data/relative7.out new file mode 100644 index 00000000..fd808777 --- /dev/null +++ b/LibreOJ/2005/data/relative7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:873b6af7acec5117224f978d276c8815a6e360f1c51f64a0c04a1424394cada4 +size 676288 diff --git a/LibreOJ/2005/data/relative8.in b/LibreOJ/2005/data/relative8.in new file mode 100644 index 00000000..19d38906 --- /dev/null +++ b/LibreOJ/2005/data/relative8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3414f3fbb1b4a3116f4cf0115f842feb4348aad7d70d94cefd43c647080877b8 +size 3458942 diff --git a/LibreOJ/2005/data/relative8.out b/LibreOJ/2005/data/relative8.out new file mode 100644 index 00000000..7a0e0c93 --- /dev/null +++ b/LibreOJ/2005/data/relative8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:60dd78a83d90042ff31236d182ef543580ad74ce7730865a0032cc1d36e79f90 +size 460879 diff --git a/LibreOJ/2005/data/relative9.in b/LibreOJ/2005/data/relative9.in new file mode 100644 index 00000000..7ff03a16 --- /dev/null +++ b/LibreOJ/2005/data/relative9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ada83b5a1c1a8e817929923e9551490066f237ef3c7bd2d614c577a92fef13e +size 3389963 diff --git a/LibreOJ/2005/data/relative9.out b/LibreOJ/2005/data/relative9.out new file mode 100644 index 00000000..19bcf521 --- /dev/null +++ b/LibreOJ/2005/data/relative9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9c033278eb482d46e9981114b85eb3f3a7b1f6536d3c587211e93678b1737dd9 +size 437317