Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // tiom4eg's precompiler options
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- // IO settings
- #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
- // Quick types
- #define ll long long
- #define ld long double
- #define ull unsigned long long
- #define pii pair <int, int>
- #define vi vector <int>
- #define mi vector <vector <int>>
- // Quick functions
- #define endl "\n"
- #define F first
- #define S second
- #define all(a) a.begin(), a.end()
- #define sz(a) (int)(a.size())
- #define pb push_back
- #define mp make_pair
- // Quick fors
- #define FOR(i, a, b) for (int i = a; i < b; ++i)
- #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
- #define RFOR(i, a, b) for (int i = a; i >= b; --i)
- #define EACH(e, a) for (auto& e : a)
- // Pragmas
- #ifndef TIOM4EG
- #pragma GCC optimize("O3,unroll-loops") // let the chaos begin!
- #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
- #pragma GCC comment(linker, "/stack:200000000")
- #endif
- // PBDS
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
- #define ook order_of_key
- #define fbo find_by_order
- using namespace __gnu_pbds;
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- using namespace std;
- mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
- #define int long long
- const int INF = 1e9 + 7, MAX = 100007, MD = 998244353, MOD = 499997371LL * 499999303, LG = 30, B = 400;
- char heavy[MAX];
- int a[MAX], ptr[MAX], cnt[MAX];
- unordered_map <int, int> cp;
- vi pos[MAX], ansl[MAX], ansr[MAX];
- signed main() {
- fastIO;
- int n, q; cin >> n >> q;
- FOR(i, 0, n) {
- cin >> a[i];
- if (cp.find(a[i]) == cp.end()) cp[a[i]] = sz(cp);
- ++cnt[a[i] = cp[a[i]]], pos[a[i]].pb(i);
- }
- int u = sz(cp);
- FOR(i, 0, u) if (cnt[i] >= B) {
- heavy[i] = 1;
- ansl[i].resize(u), ansr[i].resize(u);
- }
- FOR(i, 0, n) {
- if (heavy[a[i]]) FOR(j, 0, u) ansl[a[i]][j] = max(ansl[a[i]][j], (cnt[a[i]] - ptr[a[i]]) * ptr[j]);
- ++ptr[a[i]];
- if (heavy[a[i]]) FOR(j, 0, u) ansr[a[i]][j] = max(ansr[a[i]][j], (cnt[j] - ptr[j]) * ptr[a[i]]);
- }
- while (q--) {
- int x, y; cin >> x >> y, x = cp[x], y = cp[y];
- if (heavy[x]) {
- cout << ansr[x][y] << endl;
- continue;
- }
- if (heavy[y]) {
- cout << ansl[y][x] << endl;
- continue;
- }
- int cur = 0, res = 0;
- FOR(i, 0, sz(pos[x])) {
- while (cur < sz(pos[y]) && pos[y][cur] <= pos[x][i]) ++cur;
- res = max(res, (i + 1) * (sz(pos[y]) - cur));
- }
- cout << res << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement