Http://codeforces.com/contest/459/problem/D
Test instructions: Definition: Number of x in F (l,r,x) [l,r], n number, A[i] N<=1e5,a[i]<=1e9
Q i<j when F (1,i,a[i]) > F (j,n,a[j]) are satisfied (i,j) logarithm?
The preprocessing suffix F (j) is inserted into the bit enumeration I, query mp[a[i]]-1, and then update bit.
#include <bits/stdc++.h>using namespaceStd;typedefLong LongLl;typedef pair<int,int>II;Constll mod=1e9+7;Constll inf=1e18;Const intn=5e6+ -;Const intm=2e6;//f (1,i,a[i]) > F (j,n,a[j])ll C[n],n,a[n]; Map<ll,ll>MP,MK;intLowbit (intx) { returnx& (-x);}voidAddintPintx) { for(inti=p;i<=m;i+=lowbit (i)) C[i]+=x; }ll sum (intp) {LL res=0; for(inti=p;i>=1; i-=lowbit (i)) Res+=C[i]; returnRes;}intMain () {//Ios::sync_with_stdio (false); while(cin>>N) {mp.clear (), mk.clear (); Memset (c,0,sizeof(c)); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); for(inti=n;i>=1; i--) {Mp[a[i]]++; Add (Mp[a[i]],1); } ll ans=0; for(intI=1; i<n;i++) {Mk[a[i]]++; Add (Mp[a[i]],-1); Mp[a[i]]--; Ans+=sum (mk[a[i]]-1); } cout<<ans<<Endl; } return 0;}
The method of divide-and-conquer for standard process
#include <iostream>#include<algorithm>#include<cstring>using namespacestd;Const intINF =1000000000;Const intMAX =1000005;intA[max], Tmp[max], Cnt[max], Le[max], Ri[max];Long LongSolveintLintR) { if(R-l <2) return 0; intMid = (L + r)/2; Long Longret = Solve (l, mid) +Solve (Mid, R); intP1 = L, p2 =mid; while(P1! = Mid | | P2! =r) {intVal1 = (P1 < mid?)LE[P1]: INF); intVal2 = (P2 < r?)RI[P2]: INF); if(Val1 <=val2) {P1++; RET+ = P2-mid; } ElseP2++; } merge (Le+ L, le + Mid, le + Mid, le +R, TMP); for(inti =0; i < r-l; i++) Le[i+ L] =Tmp[i]; Merge (Ri+ L, RI + Mid, ri + Mid, RI +R, TMP); for(inti =0; i < r-l; i++) Ri[i+ L] =Tmp[i]; returnret;}intMain () {Ios::sync_with_stdio (false); intN; CIN>>N; for(inti =0; I < n; i++) {cin>>A[i]; Tmp[i]=A[i]; } sort (TMP, TMP+N); for(inti =0; I < n; i++) A[i]= Lower_bound (TMP, TMP + N, a[i])-tmp; for(inti =0; I < n; i++) {Cnt[a[i]]++; Le[i]=Cnt[a[i]]; } memset (CNT,0,sizeof(CNT)); for(inti = n-1; I >=0; i--) {Cnt[a[i]]++; Ri[i]=Cnt[a[i]]; } cout<< Solve (0, N) <<Endl; return 0;}
Codeforcecs 459D Pashmak and Parmida ' s problem tree array OR sub-rule