Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=2038
MO team algorithm can solve a class of non-modified, offline query problems. And this problem can be done with Mo team.
* I was reading this paper learned: (link ~)
In fact, the team is a kind of optimization of violence, just the query is offline pre-ordered according to the rules, and then the violence to deal with these questions in turn.
There are two ways to do this and only write a segmented solution at this time. First the 1~n is divided into sqrt (n), u = sqrt (n), and M queries are sorted by the first block and then by R.
The code is as follows:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 7 using namespacestd;8 Const intMAXN = 5e4 +5;9typedefLong LongLL;Ten intA[MAXN], u, CONT[MAXN]; One structque { A intL, R, id; - }Q[MAXN]; - structData { the LL x, y; - }ANS[MAXN]; - - ll GCD (ll X, ll y) { + if(!y) - returnx; + returnGCD (y, x%y); A } at - BOOLcmp (que x, que y) { - if(x.l/u = = Y.L/u) - returnX.R <Y.R; - returnx.l/u < Y.L/u; - } in - inline ll F (ll X) { to returnX *x; + } - the intMain () * { $ intN, M;Panax Notoginseng while(~SCANF ("%d%d", &n, &m)) { - for(inti =1; I <= N; i++) { thescanf"%d", A +i); + } A for(inti =1; I <= m; i++) { thescanf"%d%d", &Q[I].L, &Q[I].R); +Q[i].id =i; - } $U =sqrt (n); $Sort (q +1, q + M +1, CMP); - intL =1, R =0; -LL temp =0; thememset (cont,0,sizeof(cont)); - for(inti =1; I <= m; i++) {Wuyi while(L <Q[I].L) { theTemp-=f (Cont[a[l]]); -cont[a[l]]--; WuTemp + =f (Cont[a[l]]); -l++; About } $ while(L > Q[I].L) {//the front is still not counted. -l--; -Temp-=f (Cont[a[l]]); -cont[a[l]]++; ATemp + =f (Cont[a[l]]); + } the while(R >Q[I].R) { -Temp-=f (Cont[a[r]]); $cont[a[r]]--; theTemp + =f (Cont[a[r]]); ther--; the } the while(R <Q[I].R) { -r++; inTemp-=f (Cont[a[r]]); thecont[a[r]]++; theTemp + =f (Cont[a[r]]); About } theans[q[i].id].x = Temp-(r-l +1); theAns[q[i].id].y = (LL) (R-l +1) * (R-L); the } + for(inti =1; I <= m; i++) { - if(!ans[i].x | |!ans[i].y) { theprintf"0/1\n");Bayi } the Else { thetemp =gcd (ans[i].x, ans[i].y); -printf"%lld/%lld\n", Ans[i].x/temp, ANS[I].Y/temp); - } the } the } the}
bzoj-2038: [2009 countries Training team] small Z socks (hose)