Advertisement
tiom4eg

openol E 67/100

Jan 16th, 2023
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. #define ld long double
  9. #define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define vi vector <int>
  12. #define mi vector <vector <int>>
  13. // Quick functions
  14. #define endl "\n"
  15. #define F first
  16. #define S second
  17. #define all(a) a.begin(), a.end()
  18. #define sz(a) (int)(a.size())
  19. #define pb push_back
  20. #define mp make_pair
  21. // Quick fors
  22. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  23. #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
  24. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  25. #define EACH(e, a) for (auto& e : a)
  26. // Pragmas
  27. #ifndef TIOM4EG
  28. #pragma GCC optimize("O3,unroll-loops") // let the chaos begin!
  29. #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
  30. #pragma GCC comment(linker, "/stack:200000000")
  31. #endif
  32. // PBDS
  33. #include <ext/pb_ds/assoc_container.hpp>
  34. #include <ext/pb_ds/tree_policy.hpp>
  35. #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  36. #define ook order_of_key
  37. #define fbo find_by_order
  38. using namespace __gnu_pbds;
  39. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  40. using namespace std;
  41. mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
  42. #define int long long
  43. const int INF = 1e9 + 7, MAX = 100007, MD = 998244353, MOD = 499997371LL * 499999303, LG = 30, B = 400;
  44.  
  45. char heavy[MAX];
  46. int a[MAX], ptr[MAX], cnt[MAX];
  47. unordered_map <int, int> cp;
  48. vi pos[MAX], ansl[MAX], ansr[MAX];
  49.  
  50. signed main() {
  51.     fastIO;
  52.     int n, q; cin >> n >> q;
  53.     FOR(i, 0, n) {
  54.         cin >> a[i];
  55.         if (cp.find(a[i]) == cp.end()) cp[a[i]] = sz(cp);
  56.         ++cnt[a[i] = cp[a[i]]], pos[a[i]].pb(i);
  57.     }
  58.     int u = sz(cp);
  59.     FOR(i, 0, u) if (cnt[i] >= B) {
  60.         heavy[i] = 1;
  61.         ansl[i].resize(u), ansr[i].resize(u);
  62.     }
  63.     FOR(i, 0, n) {
  64.         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]);
  65.         ++ptr[a[i]];
  66.         if (heavy[a[i]]) FOR(j, 0, u) ansr[a[i]][j] = max(ansr[a[i]][j], (cnt[j] - ptr[j]) * ptr[a[i]]);
  67.     }
  68.     while (q--) {
  69.         int x, y; cin >> x >> y, x = cp[x], y = cp[y];
  70.         if (heavy[x]) {
  71.             cout << ansr[x][y] << endl;
  72.             continue;
  73.         }
  74.         if (heavy[y]) {
  75.             cout << ansl[y][x] << endl;
  76.             continue;
  77.         }
  78.         int cur = 0, res = 0;
  79.         FOR(i, 0, sz(pos[x])) {
  80.             while (cur < sz(pos[y]) && pos[y][cur] <= pos[x][i]) ++cur;
  81.             res = max(res, (i + 1) * (sz(pos[y]) - cur));
  82.         }
  83.         cout << res << endl;
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement