Title Address: HDU 5145
Mo team is really good magic. This complexity is only N*SQRT (n) ...
Bare team tiles, first offline, then by the left end of the block, by the number of blocks as the first keyword sort, and then by the R value as the second keyword to sort. are small to large, can prove that such a complexity of only N*SQRT (n). Then the transfer between blocks is performed.
The code is as follows:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace Std;#define LL __int64#define PI ACOs ( -1.0)//#pragma COMMENT (linker, "/stack:1024000000")ConstintMod=1e9+7; constintinf=0x3f3f3f3f; Const DOUBLE Eqs=1E-9; constintmaxn=30000+Ten;intA[MAXN]; LL ANS[MAXN], INV[MAXN], ha[maxn];struct node{intL, R, ID,POS;} Fei[maxn];bool CMP (nodex, nodey){return x.POS<y.POS|| (x.POS==y.POS&&x.r<y. R);} ll Pow (LLx,intk) {LL ans=1; while(k) {if(k&1) Ans=ans*x%mod;x=x*x%mod; k>>=1; }returnAns;} void Init () { for(intI=0; i<=30000; i++) {Inv[i]=pow (LL) i,mod-2); }}intMain () {intT, N,m, I, J, L, R, K, case=0; LL Res;//freopen("1.txt","R", stdin);//freopen("2.txt","W", stdout); Init (); scanf"%d", &t); while(t--) {scanf ("%d%d",&n,&m); k=sqrt(n*.0); for(i=1; i<=n;i++) {scanf ("%d", &a[i]); } for(i=0;i<m; i++) {scanf ("%d%d", &FEI[I].L,&FEI[I].R); Fei[i].id=i; Fei[i].POS=fei[i].l/k; }Sort(fei,fei+m, CMP); L=1; r=1; res=1; memset (Ha,0, sizeof (HA)); ha[a[1]]=1; for(i=0;i<m; i++) { while(R<FEI[I].R) {r++; ha[a[r]]++; Res=res*(r-l+1)%mod*INV[Ha[a[r]]%mod; } while(R>FEI[I].R) {Res=res*ha[A[r]]%mod*INV[r-l+1]%mod; ha[a[r]]--; r--; } while(L>FEI[I].L) {l--; ha[a[l]]++; Res=res*(r-l+1)%mod*INV[Ha[a[l]]%mod; } while(L<FEI[I].L) {Res=res*ha[A[l]]%mod*INV[r-l+1]%mod; ha[a[l]]--; l++; } ans[fei[i].id]=res; } for(i=0;i<m; i++) {printf("%i64d\ n", Ans[i]); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5145 NPY and girls (Mo team sub-block offline)