CSU 1515 Sequence (Mo team algorithm)

Source: Internet
Author: User

Test instructions: Give n number, M inquire. Each query is an interval in which the absolute value of the interval difference is 1 of the number logarithm.

The puzzle: First discretization, then the MO team algorithm. The team is an offline algorithm, sorted by the left end of the query, sorted by the right endpoint.

PS: First write MO team, said is quite simple, but this problem before the mess has been tle, MO team is still very strong.

Code:

#include <algorithm>#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespaceStd;typedefLong Longll;structNode {intVal; intPOS; BOOL operator< (ConstNode x)Const {        returnVal <X.val; }} a[10005];intb[10005], c[10005];inttmp[30000];ll so[100005];structQuery {intL, R, id; BOOL operator< (ConstQuery x)Const {        if(L = = x.l)returnR <X.R; returnL <X.L; }} q[100005];intUpdateintXintd) {    intAns = d * ((tmp[x+1] + tmp[x-1])); if(D <0) tmp[x]--;Elsetmp[x]++; returnans;}//I'm a good cook.intMain () {//freopen ("In.txt", "R", stdin);    intN, M;  while(~SCANF ("%d%d", &n, &m)) { for(inti =1; I <= N; ++i) {scanf ("%d", &a[i].val); A[i].pos=i; } sort (A+1, A +1+N); b[1] =1; for(inti =2; I <= N; ++i) {if(A[i].val = = a[i-1].val) B[i] = b[i-1]; Else if(A[i].val = = a[i-1].val +1) B[i] = b[i-1]+1; ElseB[i] = b[i-1] +2; }         for(inti =1; I <= N; ++i) {c[A[i].pos]=B[i];} memset (tmp,0,sizeoftmp);  for(inti =0; I < m; ++i) {scanf ("%d%d", &q[i].l,&Q[I].R); Q[i].id=i; } sort (q, q+m); intPL =1, PR =0; ll ans=0;  for(inti =0; I < m; ++i) {intID =q[i].id; intL =Q[I].L; intR =Q[I].R; if(PR < R) for(inti = pr+1; I <= R; ++i) ans + = Update (C[i],1); Else         for(inti = PR; i > R; -i) ans + = Update (C[i],-1); if(PL < L) for(inti = pl; I < L; ++i) ans + = Update (C[i],-1); PR= r, pl =l; So[id]=ans; }         for(inti =0; I < m; ++i) printf ("%lld\n", So[i]); }    return 0;}

CSU 1515 Sequence (Mo team algorithm)

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.