0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 19:08:47 +00:00

#6285. 数列分块入门 9

https://loj.ac/s/1466637
This commit is contained in:
Baoshuo Ren 2022-05-19 21:19:34 +08:00
parent 47265ad1cc
commit dd5a43941a
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68
31 changed files with 196 additions and 0 deletions

106
LibreOJ/6285/6285.cpp Normal file
View File

@ -0,0 +1,106 @@
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 100005;
int n, t, a[N], st[N], ed[N], pos[N], f[1005][1005], cnt[N];
std::vector<int> nums, v[N];
inline int find(int x) {
return std::lower_bound(nums.begin(), nums.end(), x) - nums.begin();
}
void pre(int x) {
memset(cnt, 0x00, sizeof(cnt));
std::pair<int, int> res;
for (int i = st[x]; i <= n; i++) {
cnt[a[i]]++;
if (cnt[a[i]] > res.first || (cnt[a[i]] == res.first && nums[a[i]] < nums[res.second])) {
res = std::make_pair(cnt[a[i]], a[i]);
}
f[x][pos[i]] = res.second;
}
}
inline int get(int x, int l, int r) {
return std::upper_bound(v[x].begin(), v[x].end(), r) - std::lower_bound(v[x].begin(), v[x].end(), l);
}
int query(int l, int r) {
int p = pos[l],
q = pos[r];
std::pair<int, int> res;
if (p == q) {
for (int i = l; i <= r; i++) {
int _ = get(a[i], l, r);
if (_ > res.first || (_ == res.first && nums[a[i]] < nums[res.second])) {
res = std::make_pair(_, a[i]);
}
}
return nums[res.second];
}
res.second = f[p + 1][q - 1];
res.first = get(res.second, l, r);
for (int i = l; i <= ed[p]; i++) {
int _ = get(a[i], l, r);
if (_ > res.first || (_ == res.first && nums[a[i]] < nums[res.second])) {
res = std::make_pair(_, a[i]);
}
}
for (int i = st[q]; i <= r; i++) {
int _ = get(a[i], l, r);
if (_ > res.first || (_ == res.first && nums[a[i]] < nums[res.second])) {
res = std::make_pair(_, a[i]);
}
}
return nums[res.second];
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
t = std::sqrt(n);
for (int i = 1; i <= n; i++) {
cin >> a[i];
pos[i] = (i - 1) / t + 1;
nums.push_back(a[i]);
}
std::sort(nums.begin(), nums.end());
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
nums.insert(nums.begin(), 0);
for (int i = 1; i <= n; i++) v[a[i] = find(a[i])].push_back(i);
for (int i = 1; i <= pos[n]; i++) {
st[i] = (i - 1) * t + 1;
ed[i] = std::min(n, i * t);
}
for (int i = 1; i <= pos[n]; i++) pre(i);
for (int i = 1, l, r; i <= n; i++) {
cin >> l >> r;
cout << query(l, r) << endl;
}
return 0;
}

BIN
LibreOJ/6285/data/a1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/a9.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/6285/data/ex_5.out (Stored with Git LFS) Normal file

Binary file not shown.