/* ************************************************************************** */
/* */
/* ::: ::: ::: */
/* 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;
class fenwick{
public:
int sz = 100011;
ll val[100055];
void update(int x, ll v){
for(;x <= sz ;x += x & -x) val[x] += v;
}
ll query(int x){
ll ret = 0;
for(;x > 0;x -= x & -x) ret = max(ret, val[x]);
return ret;
}
};
int n, 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]--;
}
fenwick fk;
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;
fk.update(p.first, -num[p.first] * p.second);
fk.update(p.first, num[p.first] * cnt[p.first]);
}
for (auto &p : mp)
{
use[p.first] = 0;
}
ans[q] = fk.query(n + 1);
}
}
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 |
Execution timed out |
4003 ms |
600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4003 ms |
600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4034 ms |
344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4003 ms |
600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |