bzoj-2038: [2009 countries Training team] small Z socks (hose)

Source: Internet
Author: User
Tags gcd

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", &AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.