Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 1339 – Strongest Community
Source : Light Online Judge
Category : Data Structure
Algorithm : MO's Algorithm
Verdict : Accepted
- #include "bits/stdc++.h"
-
- using namespace std;
-
- #define FI freopen("in.txt", "r", stdin)
- #define FO freopen("out.txt", "w", stdout)
- #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
-
- #define FOR(i, n) for (int i = 1; i <= n; i++)
- #define For(i, n) for (int i = 0; i < n; i++)
- #define ROF(i, n) for (int i = n; i >= 1; i--)
- #define Rof(i, n) for (int i = n-1; i >= 0; i--)
- #define FORI(i, n) for (auto i : n)
-
- #define ll long long
- #define ull unsigned long long
- #define vi vector <int>
- #define vl vector <ll>
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define mk make_pair
- #define ff first
- #define ss second
- #define eb emplace_back
- #define em emplace
- #define pb push_back
- #define ppb pop_back
- #define All(a) a.begin(), a.end()
- #define memo(a, b) memset(a, b, sizeof a)
- #define Sort(a) sort(All(a))
- #define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
- #define rev(a) reverse(All(a))
-
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- #define maxAll(a) *max_element(All(a))
- #define minAll(a) *min_element(All(a))
- #define allUpper(a) transform(All(a), a.begin(), :: toupper)
- #define allLower(a) transform(All(a), a.begin(), :: tolower)
- #define endl '\n'
- #define nl puts("")
- #define ub upper_bound
- #define lb lower_bound
- #define Exp exp(1.0)
- #define PIE 2*acos(0.0)
- #define Sin(a) sin(((a)*PIE)/180.0)
- #define EPS 1e-9
-
- inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
- inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
- inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
- inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
-
- #define popcount(mask) __builtin_popcount(mask) // count set bit
- #define popcountLL(mask) __builtin_popcountll(mask) // for long long
-
- inline int read() { int a; scanf("%d", &a); return a; }
- inline ll readLL() { ll a; scanf("%lld", &a); return a; }
- inline double readDD() { double a; scanf("%lf", &a); return a; }
-
- template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
- int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
- ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
-
- #define inf 1e18
- #define mod 1000000007
-
- static const int maxn = 1e5 + 5;
- static const int logn = 18;
- static const int block = 320;
-
- int arr[maxn], fre[maxn], cntfre[maxn];
- int n, c, q;
-
- struct mo
- {
- int l, r, id;
- mo(int l = 0, int r = 0, int id = 0) : l(l), r(r), id(id) {}
- friend bool operator < (mo p, mo q)
- {
- int pb = p.l / block;
- int qb = q.l / block;
- if (pb != qb) return p.l < q.l;
- return (p.r < q.r) ^ (p.l / block % 2);
- }
- } Q[maxn];
-
- int l, r, ans[maxn];
- int maxgroup;
-
- inline void add(int pos)
- {
- int num = arr[pos];
- cntfre[ fre[num] ]--;
- fre[num]++;
- cntfre[ fre[num] ]++;
- if (maxgroup < fre[num]) maxgroup++;
- }
-
- inline void remov(int pos)
- {
- int num = arr[pos];
- cntfre[ fre[num] ]--;
- fre[num]--;
- cntfre[ fre[num] ]++;
- if (cntfre[ maxgroup ] == 0) maxgroup--;
- }
-
- inline void clean()
- {
- maxgroup = 0;
- For(i, maxn) fre[i] = 0, ans[i] = 0, cntfre[i] = 0;
- }
-
- int main()
- {
- FI;
- int tc = read();
- FOR(tcase, tc)
- {
- n = read(), c = read(), q = read();
- FOR(i, n) arr[i] = read();
- FOR(i, q)
- {
- int l = read();
- int r = read();
- if (l > r) swap(l, r);
- Q[i] = mo(l, r, i);
- }
- sort(Q+1, Q+q+1);
- l = 1, r = 0;
- FOR(i, q)
- {
- int L = Q[i].l;
- int R = Q[i].r;
- while (l > L) add(--l);
- while (l < L) remov(l++);
- while (r > R) remov(r--);
- while (r < R) add(++r);
- ans[ Q[i].id ] = maxgroup;
- }
- printf("Case %d:\n", tcase);
- FOR(i, q) printf("%d\n", ans[i]);
- clean();
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.