#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
struct ST{
vector<ll> t;
int n;
ST(int ns){
n = ns;
t.resize(4 * n);
}
void upd(int v, int tl, int tr, int& p, int& k){
if (tl == tr){
t[v] += k;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
if (p <= tm){
upd(vv, tl, tm, p, k);
}
else {
upd(vv + 1, tm + 1, tr, p, k);
}
t[v] = max(t[vv], t[vv + 1]);
}
void upd(int p, int k){
upd(1, 1, n, p, k);
}
ll get(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return 0;
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) / 2, vv = 2 * v;
return max(get(vv, tl, tm, l, r), get(vv + 1, tm + 1, tr, l, r));
}
ll get(int l, int r){
return get(1, 1, n, l, r);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q; cin>>n>>q;
vector<int> a(n + 1);
map<int, vector<int>> p;
for (int i = 1; i <= n; i++){
cin>>a[i];
p[a[i]].pb(i);
}
const int S = sqrt(n);
vector<bool> y(n + 1);
vector<int> x;
for (int i = 1; i <= n; i++){
if (p[i].size() > S){
y[i] = 1;
x.pb(i);
}
}
vector<pii> end[n + 1];
for (int i = 1; i <= q; i++){
int l, r; cin>>l>>r;
end[r].pb({l, i});
}
vector<int> :: iterator it1, it2;
auto count = [&](int l, int r, int x){
it1 = lower_bound(p[x].begin(), p[x].end(), l);
it2 = upper_bound(p[x].begin(), p[x].end(), r);
return (it2 - it1);
};
ST T(n);
vector<ll> out(q + 1);
for (int i = 1; i <= n; i++){
if (!y[a[i]]){
for (int j: p[a[i]]){
T.upd(j, a[i]);
if (j == i) break;
}
}
for (auto [l, j]: end[i]){
out[j] = T.get(l, i);
for (int f: x){
out[j] = max(out[j], 1LL * f * count(l, i, f));
}
}
}
for (int i = 1; i <= q; i++){
cout<<out[i]<<"\n";
}
}
Compilation message
historic.cpp: In function 'int main()':
historic.cpp:61:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'const int' [-Wsign-compare]
61 | if (p[i].size() > S){
| ~~~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |