To reprint, do not need to contact me, only need to reply below and note the original text.
Online interval query algorithm (enhancement algorithm) 2
#include <iostream>#include<algorithm>#include<cmath>#include<vector>#include<cstdio>using namespacestd;//The template requires the state to meet the interval of the reduction//the template is written in small Z socks ...typedefLong LongLL;Const intMAXN =50005;Const intTMAXN =888; intblock_size;structVal {intT, V; Val (intT_intv_): t (t_), V (v_) {}BOOL operator< (Val b)Const { returnT <b.t; }}; Vector<Val>STAT[TMAXN][MAXN];intGetInt () {intx =0, F =1;CharEGetChar (); while(C <'0'|| C >'9') {if(C = ='-') F =-F; EGetChar ();} while(C >='0'&& C <='9') {x = x *Ten-'0'+ C, C =GetChar ();} returnX *F;} InlineintGetLast (intBint inch) { if(stat[b][inch].empty ())return 0; Vector<val>::iterator iter = stat[b][inch].end (); return(--iter)v;} InlinevoidInsert (LL &o,intBint inch,intt) {//Here's a little Z's sock unique notationO-= (GetLast (b,inch) * (GetLast (b,inch)-1)) >>1; stat[b][inch].push_back (Val (T, GetLast (b,inch)+1)); o+ = (GetLast (b,inch) * (GetLast (b,inch)-1)) >>1;} LL VAL[TMAXN][TMAXN]; intCOL[MAXN];intn, m; vector<int>Ra, RB;intCNT[MAXN];intTS[MAXN];intTMS =0; inlinevoidUpdate_clear (int inch) { if(ts[inch] <TMS) {ts[inch] =TMS; cnt[inch] =0; }} intLB =0, RI =0; inlinevoidUpdate_restore (int inch) { if(ts[inch] <TMS) {ts[inch] =TMS; if(stat[lb][inch].empty ()) cnt[inch] =0; Else { intt =int(Lower_bound (stat[lb][inch].begin (), stat[lb][inch].end (), Val (RI,0))-stat[lb][inch].begin ()); if(stat[lb][inch][t].t > RI) t--; if(T <0) cnt[inch] =0; Elsecnt[inch] = stat[lb][inch][t].v; }}} inline LL Getans (intLintr) {if(r-l+1<=block_size) { ++TMS; LL ret=0; for(inti = l; I <= R; i++) {update_clear (col[i]); RET-= (cnt[col[i]]* (cnt[col[i]]-1)) >>1; Cnt[col[i]]++; RET+ = (cnt[col[i]]* (cnt[col[i]]-1)) >>1; } returnret; } Else { intA = L, B =R; Ra.clear (), rb.clear (); whileAblock_size) {Ra.push_back (a); A++; } while(b%block_size) {Rb.push_back (b); b--; } intA_b = A/block_size; intB_b = b/block_size; LL ret=Val[a_b][b_b]; LB=A_b; RI=b; ++TMS; for(inti =0; I < ra.size (); i++) {Update_restore (col[ra[i]); RET-= (cnt[col[ra[i]]]* (cnt[col[ra[i]]]-1)) >>1; Cnt[col[ra[i] ]++; RET+ = (cnt[col[ra[i]]]* (cnt[col[ra[i]]]-1)) >>1; } for(inti =0; I < rb.size (); i++) {Update_restore (col[rb[i]); RET-= (cnt[col[rb[i]]]* (cnt[col[rb[i]]]-1)) >>1; Cnt[col[rb[i] ]++; RET+ = (cnt[col[rb[i]]]* (cnt[col[rb[i]]]-1)) >>1; } returnret; }} ll gcd (ll A, ll b) {if(!B)returnA; returnGCD (b, a%b);} intMain () {//freopen ("sock.in", "R", stdin); //freopen ("Sock.out", "w", stdout);n = GetInt (), M =GetInt (); for(inti =0; I < n; i++) {Col[i]=GetInt (); } block_size= sqrt (n/(log (n)/log (2)+1)); for(inti =0; I < n; i++) { if(i% Block_size = =0) { //The range of this loop is only for small Z socks! intB_in = I/block_size; LL ans=0; for(intj = i; J < N; J + +{insert (ans, b_in, Col[j], J); if(j% Block_size = =0) {val[b_in][j/block_size] =ans; } } } } for(inti =1; I <= m; i++) { intL = GetInt (), r =GetInt (); LL ans= Getans (l1, R-1); LL Len= r-l+1; if(ans = =0) {printf ("0/1\n"); } Else{LL fm= len* (len-1)/2; LL g=gcd (ANS, FM); printf ("%lld/%lld\n", ans/g, fm/g); } } return 0;}
This section of code space is too large to be AC, but very useful, and universal. If you do not want to see a direct look at the next article, more important and effective.
This problem is not already used online algorithm a over, how to again?
Don't forget! The range additive was used before!
This time the algorithm does not use the interval to be additive, also does not have the interval to reduce the sex, how does it do?
From one endpoint to the interval of each point, you can update it in turn, updating one point at a time.
So we can use the idea of persistence.
Here is an array of "persistent", which is a kind of random thought out. The change of each item is recorded by Vector, and then the state of a time is obtained by means of a binary search.
This makes the query more complex than a log.
Refer to previously written instructions:
Well, so we succeeded in putting the log in the root of the root.
If you haven't read it or are interested in discussing it with me, you can add QQ2502669375Next Link:[Learning-think-explore] MO team algorithm Manhattan minimum spanning tree and block interval query algorithm-4
[Learning-think-explore] MO team algorithm Manhattan minimum spanning tree and block interval query algorithm-3