Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

joisc2014-C 歴史の研究

LuoGu: AT1219 歴史の研究

AtCoder: C - 歴史の研究

板子题。思想是不删除,用暴力替代这一部分。

查看代码
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
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
void chkmax(LL &x, LL k)
{
(x < k) && (x = k);
}
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
(c == '-') && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
(flag) && (x = ~x + 1);
}
const int N = 1e5 + 10;
LL ans[N];
int cnt[N];
int n, m, len, w[N];
struct Query
{
int l, r, id;
friend bool operator<(const Query &x, const Query &y)
{
if (x.l / len != y.l / len)
return x.l < y.l;
return x.r < y.r;
}
} q[N];
vector<int> ws;
void add(int x, LL &res)
{
cnt[x]++;
chkmax(res, (LL)cnt[x] * ws[x]);
}
int main()
{
read(n), read(m);
len = n / sqrt(m);
for (int i = 1; i <= n; i++)
{
read(w[i]);
ws.push_back(w[i]);
}
sort(ws.begin(), ws.end());
ws.erase(unique(ws.begin(), ws.end()), ws.end());
for (int i = 1; i <= n; i++)
w[i] = lower_bound(ws.begin(), ws.end(), w[i]) - ws.begin();
for (int i = 0; i < m; i++)
{
read(q[i].l), read(q[i].r);
q[i].id = i;
}
sort(q, q + m);
for (int x = 0; x < m;)
{
int y = x;
for (; y < m && q[y].l / len == q[x].l / len; y++)
;
int p = q[x].l / len * len + len;
for (; x < y && q[x].r < p; x++)
{
LL res = 0;
for (int k = q[x].l; k <= q[x].r; k++)
add(w[k], res);
ans[q[x].id] = res;
for (int k = q[x].l; k <= q[x].r; k++)
cnt[w[k]]--;
}
LL res = 0;
for (int l = p, r = p - 1; x < y; x++)
{
while (r < q[x].r)
add(w[++r], res);
LL tmp = res;
while (l > q[x].l)
add(w[--l], res);
ans[q[x].id] = res;
while (l < p)
cnt[w[l++]]--;
res = tmp;
}
for (int i = 0; i < n; i++)
cnt[i] = 0;
}
for (int i = 0; i < m; i++)
printf("%lld\n", ans[i]);
return 0;
}