New Ket Practice Game E-Question series search (block idea + MO team algorithm)

Source: Internet
Author: User

Topic link series search

Think of the pieces and run the team,

Set $c[i]$ to the number of occurrences of the $i$ within the current maintenance interval,

$g [i]$ is the number of occurrences of $i$ in the currently maintained interval,

$BG [i]$ The number of occurrences, $bg the meaning of the i]$ is the number of occurrences in the $i$ block represented by the first.

$f [i][j]$ to $1$ to $n$, meaning the number of occurrences of the current number of numbers in the first $j$ digit block is $i$.

Each time we ask, we first find out the number of occurrences of the current requirement.

This is to be begged by $bg[]$.

Then, according to $f[][]$, determine the number of $k2$ in all the $k1$ occurrences of the current interval.

For the sake of convenience I used discretization.

Time Complexity $o (M\sqrt{n} + n\sqrt{n}) $

#include <bits/stdc++.h>using namespace std; #define REP (I, A, b) for (int i (a); I <= (b); ++i) #define DEC (i, A, b)  for (int i (a); I >= (b); i.) #define MPMAKE_PAIR#DEFINE fifirst#define sesecondtypedef long long ll;const int N = 4e4 + 10;const int M = 203;int belong[n];int c[n], g[n], Bg[n];int f[n][m];int L, R, Kth;int Blocknum;int L[m], R[M], ret[N];st Ruct Node{int L, R, X, Y, id;void Scan () {scanf ("%d%d%d%d", &l, &r, &x, &y);} friend BOOL operator < (const node &a, const node &b) {return BELONG[A.L] = = BELONG[B.L]? A.R < B.r:belong [A.L] < BELONG[B.L];}} Q[n];int A[n], Val[n];int BS, Cnt;int N, m;void Remove (int x, int y) {--f[y][belong[x]];if (g[y] = = 1)--bg[belong[y]];--g[ Y];} void Add (int x, int y) {++f[y][belong[x]];if (g[y] = = 0) ++bg[belong[y]];++g[y];}  int solve (int i) {int num = 0;rep (j, 1, blocknum) {num + = bg[j];if (num >= q[i].x) {num-= Bg[j];rep (k, l[j], r[j]) {num + = (int) (G[k] > 0); if (num = = q[i].x) return k;}}} return 0;} Int Calc (int i, int x) {int num = 0;rep (j, 1, blocknum) {num + = f[x][j];if (num >= q[i].y) {num-= F[x][j];rep (k, l[j], r[j]) {num + = (int) (C[k] = = x); if (num = = q[i].y) return k;}}} return 0;} int main () {scanf ("%d", &n), Rep (i, 1, N) scanf ("%d", A + i), val[i] = A[i], q[i].id = I;sort (val + 1, val + n + 1); CNT = Unique (val + 1, val + n + 1)-Val-1;rep (i, 1, n) a[i] = Lower_bound (val + 1, val + cnt + 1, a[i])-val;bs = sqrt (n); Rep (i, 1, n) belong[i] = (i-1)/bs + 1;blocknum = Belong[n];rep (i, 1, blocknum) l[i] = 1e9, r[i] = 0;rep (i, 1, n) {L[bel Ong[i]] = min (L[belong[i]], i); R[belong[i]] = max (R[belong[i]], i);} scanf ("%d", &m), Rep (i, 1, m) Q[i].scan (), sort (q + 1, q + M + 1), Rep (i, 1, N) Add (A[i], 0), L = 1, R = 0;rep (i, 1, m) {WH Ile (R < Q[I].R) {++r;remove (A[r], c[a[r]]); ++c[a[r]];add (A[r], c[a[r]]);} while (R > Q[i].r) {Remove (A[r], c[a[r]]);--c[a[r]];add (A[r], c[a[r]]);--r;} while (L > Q[i].l) {--l;remove (a[l], c[a[l]]); ++c[a[l]];add (A[l], c[a[l]]);} while (L < Q[I].L) {REMove (A[l], c[a[l]]);--c[a[l]];add (A[l], c[a[l]); ++l;} KTH = Solve (i); ret[q[i].id] = Val[calc (i, KTH)];} Rep (i, 1, m) printf ("%d\n", Ret[i]); return 0;}

  

New Ket Practice Game E-Question series search (block idea + MO team algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.