Submission #1092480

#TimeUsernameProblemLanguageResultExecution timeMemory
1092480jonghak9028역사적 조사 (JOI14_historical)C++17
40 / 100
4046 ms13004 KiB
/* ************************************************************************** */
/*                                                                            */
/*                                                      :::    :::    :::     */
/*   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;

int n, q;
int sq;
ll arr[100055];
vector<ll> num;
vector<array<int, 3>> query;
ll ans[100055];

ll mx;

int idx;
int cnt[100055];

map<int, int> mp;

void ADD(int i)
{
    i = lower_bound(num.begin(), num.end(), arr[i]) - num.begin();
    if (mp.find(i) == mp.end())
        mp[i] = cnt[i];
    cnt[i]++;
}

void SUB(int i)
{
    i = lower_bound(num.begin(), num.end(), arr[i]) - num.begin();
    if (mp.find(i) == mp.end())
        mp[i] = cnt[i];
    cnt[i]--;
}
set<pair<ll, int>> st;

void solve()
{

    int s = 1, e = 0;
    for (auto [l, r, q] : query)
    {
        mp.clear();

        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 (p.second == cnt[p.first])
                continue;
            else
            {
                st.erase({num[p.first] * p.second, p.first});
                st.insert({num[p.first] * cnt[p.first], p.first});
            }
        }
        ans[q] = (--st.end())->first;
    }
}

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());

    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[0];
             return sa < sb; });

    solve();
    for (int i = 0; i < q; i++)
    {
        cout << ans[i] << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...