1. luogu P5048
大意: 区间询问众数的出现次数, 强制在线.
先预处理出块间答案, 考虑每次询问左右边界, 显然最多使答案再增加$2sqrt{n}$. 预处理时把相同元素按顺序存进vector里, 这样可以O(1)查询每个元素前后k次出现的位置, 对于左边界若后ans次出现位置<=r, 则++ans, 右边界若前ans次出现位置>=l, 则++ans
#include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ' ' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 5e5+10, M = 1000; int n, m, cnt, sz, ans; map<int,int> s; vector<int> val[N]; int a[N], sum[N], blo[N], pos[N]; int L[M], R[M], f[M][M]; int id(int x) { if (s.count(x)) return s[x]; int t = s.size(); return s[x]=t; } int main() { scanf("%d%d", &n, &m); REP(i,1,n) { scanf("%d", a+i); a[i] = id(a[i]); val[a[i]].pb(i); pos[i] = val[a[i]].size()-1; } sz = 710, cnt = (n-1)/sz+1; REP(i,1,cnt) L[i]=R[i-1]+1,R[i]=i*sz; R[cnt] = n; REP(i,1,cnt) { memset(sum,0,sizeof sum); REP(j,L[i],R[i]) blo[j]=i; REP(j,i,cnt) { int &res = f[i][j] = f[i][j-1]; REP(k,L[j],R[j]) { ++sum[a[k]]; res = max(res, sum[a[k]]); } } } memset(sum,0,sizeof sum); REP(i,1,m) { int l, r; scanf("%d%d", &l, &r); l ^= ans, r ^= ans, ans = f[blo[l]+1][blo[r]-1]; if (blo[l]==blo[r]) { REP(i,l,r) { ++sum[a[i]]; ans=max(ans,sum[a[i]]); } REP(i,l,r) sum[a[i]]=0; } else { REP(i,l,R[blo[l]]) { int res = pos[i], sz = val[a[i]].size(); while (res+ans<sz&&val[a[i]][res+ans]<=r) ++ans; } REP(i,L[blo[r]],r) { int res = pos[i]; while (res-ans>=0&&val[a[i]][res-ans]>=l) ++ans; } } printf("%d ", ans); } }
2. luogu P4168
大意: 区间询问最小众数, 强制在线.
#include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ' ' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 5e5+10, M = 1000; int n, m, cnt, sz, ans; map<int,int> s; vector<int> val[N]; int a[N], b[N], sum[N], blo[N], pos[N]; int L[M], R[M], f[M][M], g[M][M]; int id(int x) { if (s.count(x)) return s[x]; int t = s.size()+1; b[t] = x; return s[x]=t; } int main() { scanf("%d%d", &n, &m); REP(i,1,n) { scanf("%d", a+i); a[i] = id(a[i]); val[a[i]].pb(i); pos[i] = val[a[i]].size()-1; } sz = 700, cnt = (n-1)/sz+1; REP(i,1,cnt) L[i]=R[i-1]+1,R[i]=i*sz; R[cnt] = n; REP(i,1,cnt) { memset(sum,0,sizeof sum); REP(j,L[i],R[i]) blo[j]=i; int ans = 0, mx = 0; REP(j,i,cnt) { REP(k,L[j],R[j]) { ++sum[a[k]]; if (sum[a[k]]>mx||sum[a[k]]==mx&&b[a[k]]<b[a[ans]]) ans=k,mx=sum[a[k]]; } f[i][j] = ans, g[i][j] = mx; } } memset(sum,0,sizeof sum); REP(i,1,m) { int l, r; scanf("%d%d", &l, &r); l = (l+ans-1)%n+1, r = (r+ans-1)%n+1; if (l>r) swap(l,r); ans = f[blo[l]+1][blo[r]-1]; int mx = g[blo[l]+1][blo[r]-1]; if (blo[l]==blo[r]) { REP(i,l,r) { ++sum[a[i]]; if (sum[a[i]]>mx||sum[a[i]]==mx&&b[a[i]]<b[a[ans]]) ans=i,mx=sum[a[i]]; } REP(i,l,r) sum[a[i]]=0; } else { REP(i,l,R[blo[l]]) { int res = pos[i], sz = val[a[i]].size(), p = res+mx; if (0<=p-1&&p-1<sz&&val[a[i]][p-1]<=r) { if (b[a[i]]<b[a[ans]]) ans=i; } while (p<sz&&val[a[i]][p]<=r) ++p, ++mx, ans = i; } REP(i,L[blo[r]],r) { int res = pos[i], sz = val[a[i]].size(), p = res-mx; if (0<=p+1&&p+1<sz&&val[a[i]][p+1]>=l) { if (b[a[i]]<b[a[ans]]) ans=i; } while (p>=0&&val[a[i]][p]>=l) --p, ++mx, ans = i; } } printf("%d ", ans=b[a[ans]]); } }