/* ************************************************************************** */
/* */
/* ::: ::: ::: */
/* Problem Number: 17731 :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: js9028 <boj.kr/u/js9028> +#+ +#+ +#+ */
/* +#+ +#+ +#+ */
/* https://boj.kr/17731 #+# #+# #+# */
/* Solved: 2024/09/24 14:38:32 by js9028 ### ### ##.kr */
/* */
/* ************************************************************************** */
#include <bits/stdc++.h>
using namespace std;
#define fastio (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL))
typedef long long ll;
typedef long double lld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int MAXSIZE = 2000000;
const long long INF = 1e18 + 5;
const double EPSILON = 1e-14;
const ll DIV = 2000003;
const long double pi = 3.14159265358979323846264338327950288419716939937510L;
const int N = 1e5; // limit for array size
int n; // array size
ll t[2 * N];
void update(int p, ll value)
{ // set value at position p
for (t[p += n] = value; p > 1; p >>= 1)
t[p >> 1] = max(t[p], t[p ^ 1]);
}
ll get(int l, int r)
{ // sum on interval [l, r)
ll res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1)
{
if (l & 1)
res = max(res, t[l++]);
if (r & 1)
res = max(res, t[--r]);
}
return res;
}
int q;
int sq;
int idx[100055];
ll arr[100055];
vector<ll> num;
vector<array<int, 3>> query;
ll ans[100055];
int cnt[100055];
vector<pair<int, int>> mp;
void ADD(int i)
{
i = idx[i];
mp.push_back({i, cnt[i]});
cnt[i]++;
}
void SUB(int i)
{
i = idx[i];
mp.push_back({i, cnt[i]});
cnt[i]--;
}
bool use[100055];
void solve()
{
int s = 1, e = 0;
for (auto [l, r, q] : query)
{
mp = vector<pair<int, int>>();
while (e < r)
e++, ADD(e);
while (s > l)
s--, ADD(s);
while (s < l)
SUB(s), s++;
while (e > r)
SUB(e), e--;
for (auto &p : mp)
{
if (use[p.first])
continue;
use[p.first] = 1;
update(p.first, num[p.first] * cnt[p.first]);
}
for (auto &p : mp)
{
use[p.first] = 0;
}
ans[q] = get(0, n);
}
}
int main()
{
fastio;
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
cin >> arr[i];
num.push_back(arr[i]);
}
for (int i = 0; i < q; i++)
{
int l, r;
cin >> l >> r;
query.push_back({l, r, i});
}
sort(num.begin(), num.end());
for (int i = 1; i <= n; i++)
{
idx[i] = lower_bound(num.begin(), num.end(), arr[i]) - num.begin();
}
sq = sqrt(n);
sort(query.begin(), query.end(), [](auto &a, auto &b)
{
int sa = a[0] / sq, sb = b[0] / sq;
if(sa == sb) return a[1] < b[1];
return sa < sb; });
solve();
for (int i = 0; i < q; i++)
{
cout << ans[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
480 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
476 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
480 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
476 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
488 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
492 KB |
Output is correct |
20 |
Correct |
6 ms |
604 KB |
Output is correct |
21 |
Correct |
11 ms |
908 KB |
Output is correct |
22 |
Correct |
11 ms |
860 KB |
Output is correct |
23 |
Correct |
11 ms |
860 KB |
Output is correct |
24 |
Correct |
9 ms |
1000 KB |
Output is correct |
25 |
Correct |
12 ms |
860 KB |
Output is correct |
26 |
Correct |
12 ms |
860 KB |
Output is correct |
27 |
Correct |
13 ms |
856 KB |
Output is correct |
28 |
Correct |
13 ms |
988 KB |
Output is correct |
29 |
Correct |
12 ms |
1052 KB |
Output is correct |
30 |
Correct |
13 ms |
856 KB |
Output is correct |
31 |
Correct |
10 ms |
1116 KB |
Output is correct |
32 |
Correct |
8 ms |
860 KB |
Output is correct |
33 |
Correct |
12 ms |
1004 KB |
Output is correct |
34 |
Correct |
13 ms |
860 KB |
Output is correct |
35 |
Correct |
12 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
4 ms |
1116 KB |
Output is correct |
9 |
Correct |
7 ms |
1904 KB |
Output is correct |
10 |
Correct |
6 ms |
2012 KB |
Output is correct |
11 |
Correct |
46 ms |
7108 KB |
Output is correct |
12 |
Correct |
20 ms |
5080 KB |
Output is correct |
13 |
Correct |
40 ms |
6544 KB |
Output is correct |
14 |
Correct |
53 ms |
8060 KB |
Output is correct |
15 |
Correct |
69 ms |
10180 KB |
Output is correct |
16 |
Correct |
45 ms |
7616 KB |
Output is correct |
17 |
Correct |
23 ms |
6616 KB |
Output is correct |
18 |
Correct |
39 ms |
8436 KB |
Output is correct |
19 |
Correct |
38 ms |
9476 KB |
Output is correct |
20 |
Correct |
21 ms |
7264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
480 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
476 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
488 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
492 KB |
Output is correct |
20 |
Correct |
6 ms |
604 KB |
Output is correct |
21 |
Correct |
11 ms |
908 KB |
Output is correct |
22 |
Correct |
11 ms |
860 KB |
Output is correct |
23 |
Correct |
11 ms |
860 KB |
Output is correct |
24 |
Correct |
9 ms |
1000 KB |
Output is correct |
25 |
Correct |
12 ms |
860 KB |
Output is correct |
26 |
Correct |
12 ms |
860 KB |
Output is correct |
27 |
Correct |
13 ms |
856 KB |
Output is correct |
28 |
Correct |
13 ms |
988 KB |
Output is correct |
29 |
Correct |
12 ms |
1052 KB |
Output is correct |
30 |
Correct |
13 ms |
856 KB |
Output is correct |
31 |
Correct |
10 ms |
1116 KB |
Output is correct |
32 |
Correct |
8 ms |
860 KB |
Output is correct |
33 |
Correct |
12 ms |
1004 KB |
Output is correct |
34 |
Correct |
13 ms |
860 KB |
Output is correct |
35 |
Correct |
12 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
4 ms |
1116 KB |
Output is correct |
44 |
Correct |
7 ms |
1904 KB |
Output is correct |
45 |
Correct |
6 ms |
2012 KB |
Output is correct |
46 |
Correct |
46 ms |
7108 KB |
Output is correct |
47 |
Correct |
20 ms |
5080 KB |
Output is correct |
48 |
Correct |
40 ms |
6544 KB |
Output is correct |
49 |
Correct |
53 ms |
8060 KB |
Output is correct |
50 |
Correct |
69 ms |
10180 KB |
Output is correct |
51 |
Correct |
45 ms |
7616 KB |
Output is correct |
52 |
Correct |
23 ms |
6616 KB |
Output is correct |
53 |
Correct |
39 ms |
8436 KB |
Output is correct |
54 |
Correct |
38 ms |
9476 KB |
Output is correct |
55 |
Correct |
21 ms |
7264 KB |
Output is correct |
56 |
Correct |
25 ms |
1532 KB |
Output is correct |
57 |
Correct |
99 ms |
2456 KB |
Output is correct |
58 |
Correct |
227 ms |
3340 KB |
Output is correct |
59 |
Correct |
384 ms |
4460 KB |
Output is correct |
60 |
Correct |
579 ms |
5328 KB |
Output is correct |
61 |
Correct |
639 ms |
6248 KB |
Output is correct |
62 |
Correct |
553 ms |
7872 KB |
Output is correct |
63 |
Correct |
423 ms |
7888 KB |
Output is correct |
64 |
Correct |
443 ms |
8452 KB |
Output is correct |
65 |
Correct |
478 ms |
8080 KB |
Output is correct |
66 |
Correct |
524 ms |
8640 KB |
Output is correct |
67 |
Correct |
880 ms |
10228 KB |
Output is correct |
68 |
Correct |
1461 ms |
10544 KB |
Output is correct |
69 |
Correct |
1709 ms |
10596 KB |
Output is correct |
70 |
Correct |
1837 ms |
10560 KB |
Output is correct |
71 |
Correct |
1796 ms |
10448 KB |
Output is correct |
72 |
Correct |
1728 ms |
10496 KB |
Output is correct |
73 |
Correct |
1690 ms |
10544 KB |
Output is correct |
74 |
Correct |
1687 ms |
10624 KB |
Output is correct |
75 |
Correct |
1727 ms |
10636 KB |
Output is correct |
76 |
Correct |
1666 ms |
10504 KB |
Output is correct |
77 |
Correct |
1647 ms |
10492 KB |
Output is correct |
78 |
Correct |
1638 ms |
10688 KB |
Output is correct |
79 |
Correct |
1590 ms |
10496 KB |
Output is correct |
80 |
Correct |
479 ms |
8844 KB |
Output is correct |