Untitled
c_cpp
a month ago
1.2 kB
1
Indexable
Never
#include "bits/stdc++.h" using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(x...) #endif void solve() { int n, m; cin >> n >> m; map<int, vector<int>> pos; for (int i = 0; i < n; i++) { int a; cin >> a; pos[a].push_back(i); } while (m--) { int x, y; cin >> x >> y; long long res = 0; if (pos[x].size() <= pos[y].size()) { for (int i = 0; i < pos[x].size(); i++) { int cnty = pos[y].size() - (upper_bound(pos[y].begin(), pos[y].end(), pos[x][i]) - pos[y].begin()); res = max(res, 1LL * (i + 1) * cnty); } } else { for (int i = 0; i < pos[y].size(); i++) { int cntx = upper_bound(pos[x].begin(), pos[x].end(), pos[y][i]) - pos[x].begin(); res = max(res, 1LL * (i + 1) * cntx); } } cout << res << '\n'; } } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); int tests = 1; // cin >> tests; while (tests--) { solve(); } return 0; }