0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:11:58 +00:00

#2254. 「SNOI2017」一个简单的询问

https://loj.ac/s/1474670
This commit is contained in:
Baoshuo Ren 2022-06-02 19:32:12 +08:00
parent 3a8ace8bcd
commit 2d70e70ef9
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
21 changed files with 133 additions and 0 deletions

73
LibreOJ/2254/2254.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
int n, q, a[N], c1[N], c2[N], t, ans[N];
std::vector<std::tuple<int, int, int, int>> qs;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
t = sqrt(n);
cin >> q;
for (int i = 1, l1, r1, l2, r2; i <= q; i++) {
cin >> l1 >> r1 >> l2 >> r2;
qs.push_back(std::make_tuple(i, r1, r2, 1));
qs.push_back(std::make_tuple(i, r1, l2 - 1, -1));
qs.push_back(std::make_tuple(i, l1 - 1, r2, -1));
qs.push_back(std::make_tuple(i, l1 - 1, l2 - 1, 1));
}
std::for_each(qs.begin(), qs.end(), [&](auto &x) {
int &l = std::get<1>(x),
&r = std::get<2>(x);
if (l > r) std::swap(l, r);
});
std::sort(qs.begin(), qs.end(), [&](auto a, auto b) {
int p = std::get<1>(a) / t,
q = std::get<1>(b) / t;
int x = std::get<2>(a),
y = std::get<2>(b);
return p == q ? p & 1 ? x < y : x > y : p < q;
});
int i = 0, j = 0, res = 0;
for (auto &e : qs) {
int id, l, r, x;
std::tie(id, l, r, x) = e;
while (i < r) res += c1[a[++i]], c2[a[i]]++;
while (i > r) res -= c1[a[i]], c2[a[i--]]--;
while (j < l) res += c2[a[++j]], c1[a[j]]++;
while (j > l) res -= c2[a[j]], c1[a[j--]]--;
ans[id] += x * res;
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
return 0;
}

BIN
LibreOJ/2254/data/easyprob1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2254/data/easyprob9.out (Stored with Git LFS) Normal file

Binary file not shown.