diff --git a/LibreOJ/2551/2551.cpp b/LibreOJ/2551/2551.cpp new file mode 100644 index 00000000..a27a7e25 --- /dev/null +++ b/LibreOJ/2551/2551.cpp @@ -0,0 +1,91 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5e5 + 5, + K = 1e6 + 5; + +int n, m, a[N], cnt, root[N]; + +struct node { + int l, r, siz; + long long sum; +} tr[N << 5]; + +int build(int l, int r) { + int p = ++cnt; + + if (l == r) return p; + + int mid = l + r >> 1; + + tr[p].l = build(l, mid); + tr[p].r = build(mid + 1, r); + + return p; +} + +int insert(int p, int l, int r, int x) { + int q = ++cnt; + + tr[q] = tr[p]; + tr[q].siz++; + tr[q].sum += x; + + if (l == r) return q; + + int mid = l + r >> 1; + + if (x <= mid) { + tr[q].l = insert(tr[p].l, l, mid, x); + } else { + tr[q].r = insert(tr[p].r, mid + 1, r, x); + } + + tr[q].siz = tr[tr[q].l].siz + tr[tr[q].r].siz; + tr[q].sum = tr[tr[q].l].sum + tr[tr[q].r].sum; + + return q; +} + +long long query(int p, int q, int l, int r, int f, int k) { + int siz = tr[p].siz - tr[q].siz; + long long sum = tr[p].sum - tr[q].sum; + + if (!siz) return 0; + if (l >= k + f) return sum - static_cast(2 * k + 2 * f + siz - 1) * siz / 2; + if (r <= k + f + siz - 1) return static_cast(2 * k + 2 * f + siz - 1) * siz / 2 - sum; + + int siz_l = tr[tr[p].l].siz - tr[tr[q].l].siz; + int mid = l + r >> 1; + + return query(tr[p].l, tr[q].l, l, mid, f, k) + + query(tr[p].r, tr[q].r, mid + 1, r, f + siz_l, k); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + root[0] = build(1, 1000000); + + for (int i = 1; i <= n; i++) { + root[i] = insert(root[i - 1], 1, 1000000, a[i]); + } + + for (int i = 1, l, r, k; i <= m; i++) { + cin >> l >> r >> k; + + cout << query(root[r], root[l - 1], 1, 1000000, 0, k) << endl; + } + + return 0; +} diff --git a/LibreOJ/2551/data/line1.in b/LibreOJ/2551/data/line1.in new file mode 100644 index 00000000..63ac722f --- /dev/null +++ b/LibreOJ/2551/data/line1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c3741a258abff7c6627200bffc30bc26f24b3ce6ae8da8fda153035435e399dd +size 101 diff --git a/LibreOJ/2551/data/line1.out b/LibreOJ/2551/data/line1.out new file mode 100644 index 00000000..0aa1118d --- /dev/null +++ b/LibreOJ/2551/data/line1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f94558256c551b9cfc495867bcab07467c458559db3240f00ad87c22ac873cbf +size 28 diff --git a/LibreOJ/2551/data/line10.in b/LibreOJ/2551/data/line10.in new file mode 100644 index 00000000..271a9d24 --- /dev/null +++ b/LibreOJ/2551/data/line10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6f821b77c3ec679d608cbf15e9c3575b065a6305c16004b8a4e2a1d3ac52ef48 +size 13608930 diff --git a/LibreOJ/2551/data/line10.out b/LibreOJ/2551/data/line10.out new file mode 100644 index 00000000..e955c053 --- /dev/null +++ b/LibreOJ/2551/data/line10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:74df58cb366829827531825e32e9e64b738bb9e587cfe387130303ce7fa30dd3 +size 5882364 diff --git a/LibreOJ/2551/data/line2.in b/LibreOJ/2551/data/line2.in new file mode 100644 index 00000000..c21fa1d5 --- /dev/null +++ b/LibreOJ/2551/data/line2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b961240a9e4934039e20fe035912a1ace39b6fe5540ebd2215ea9260a9214efa +size 16503 diff --git a/LibreOJ/2551/data/line2.out b/LibreOJ/2551/data/line2.out new file mode 100644 index 00000000..10cec666 --- /dev/null +++ b/LibreOJ/2551/data/line2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd1373fae147671cbc1ecac1b0ab0c6ee8e29b062418605042cc23d9c495ac69 +size 6407 diff --git a/LibreOJ/2551/data/line3.in b/LibreOJ/2551/data/line3.in new file mode 100644 index 00000000..b013a0c6 --- /dev/null +++ b/LibreOJ/2551/data/line3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f792bf62a511b1cdd64d4ad31ddf601c335aa4307f320d53425365cce85c6579 +size 16653 diff --git a/LibreOJ/2551/data/line3.out b/LibreOJ/2551/data/line3.out new file mode 100644 index 00000000..ecefd732 --- /dev/null +++ b/LibreOJ/2551/data/line3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e581104936136f6f79888d0ed2d3807245e06d6cd843f2831b36c7cc9d72c53f +size 6437 diff --git a/LibreOJ/2551/data/line4.in b/LibreOJ/2551/data/line4.in new file mode 100644 index 00000000..652e7f23 --- /dev/null +++ b/LibreOJ/2551/data/line4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b44d6a226b769022e5929ff25f33dad4f656f896f5a13d53e6aedab1d02a3ba3 +size 16574 diff --git a/LibreOJ/2551/data/line4.out b/LibreOJ/2551/data/line4.out new file mode 100644 index 00000000..f55cd627 --- /dev/null +++ b/LibreOJ/2551/data/line4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ca6d7d77c96c0f31e734de65f12611226784c29596b95cc1e739a2159a5122ca +size 6413 diff --git a/LibreOJ/2551/data/line5.in b/LibreOJ/2551/data/line5.in new file mode 100644 index 00000000..b928e212 --- /dev/null +++ b/LibreOJ/2551/data/line5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8acb99a6a5bb05c69c1cec01c232672579b8a1e689741452b37d1f41d6d6c65b +size 2444911 diff --git a/LibreOJ/2551/data/line5.out b/LibreOJ/2551/data/line5.out new file mode 100644 index 00000000..457b2f66 --- /dev/null +++ b/LibreOJ/2551/data/line5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e6611be2d1a4200e309a46219a1b89aa6bd765de5c7fc85d373efd141283c0e5 +size 1043042 diff --git a/LibreOJ/2551/data/line6.in b/LibreOJ/2551/data/line6.in new file mode 100644 index 00000000..150f0a79 --- /dev/null +++ b/LibreOJ/2551/data/line6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:845e861ffcd4a2a4267d6fe74d9824007ff4c548ac02bd96424765f71b61592b +size 2441149 diff --git a/LibreOJ/2551/data/line6.out b/LibreOJ/2551/data/line6.out new file mode 100644 index 00000000..c951eb70 --- /dev/null +++ b/LibreOJ/2551/data/line6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1ca260962a99cf23439888811054cea1ffacacf92867ecb58676aeb0608d8b4d +size 1041405 diff --git a/LibreOJ/2551/data/line7.in b/LibreOJ/2551/data/line7.in new file mode 100644 index 00000000..c13f509d --- /dev/null +++ b/LibreOJ/2551/data/line7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dc323dfee0019f324394d550db0edba069527b7938a020729c2db8c4c06c9261 +size 2434723 diff --git a/LibreOJ/2551/data/line7.out b/LibreOJ/2551/data/line7.out new file mode 100644 index 00000000..12914a1f --- /dev/null +++ b/LibreOJ/2551/data/line7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:99cb1a518af6c8acab8f05fbaf4974152bc32aa19621d294e004490648f0b667 +size 1037533 diff --git a/LibreOJ/2551/data/line8.in b/LibreOJ/2551/data/line8.in new file mode 100644 index 00000000..9dc45cd6 --- /dev/null +++ b/LibreOJ/2551/data/line8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:89024227053844a2a5aa931839e5b5aa3bd5475839adebb13719197955dd4497 +size 13608064 diff --git a/LibreOJ/2551/data/line8.out b/LibreOJ/2551/data/line8.out new file mode 100644 index 00000000..284f2dba --- /dev/null +++ b/LibreOJ/2551/data/line8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bb91f752ef3d137199a72d52aff7363e2a280272d0c9899447c8a1cec38e112f +size 5881193 diff --git a/LibreOJ/2551/data/line9.in b/LibreOJ/2551/data/line9.in new file mode 100644 index 00000000..a441e5d8 --- /dev/null +++ b/LibreOJ/2551/data/line9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1135b7654c6a565edc70c57a9fc2b008219a3fcf8813478cddd736148b5c97b8 +size 13613820 diff --git a/LibreOJ/2551/data/line9.out b/LibreOJ/2551/data/line9.out new file mode 100644 index 00000000..7e3b6bc7 --- /dev/null +++ b/LibreOJ/2551/data/line9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:80e68e3d707dd161eeb60cf2b4cabc9226ebc756665e95a21bef446dc1cdda91 +size 5883457