WHU OJ 1551 Pairs (Mo team algorithm)

Source: Internet
Author: User
Tags cas


problem_id=1551 "> Topic links


Main topic:

The logarithm of the number in the interval less than or equal to 2 is calculated.


Thinking Analysis:
MO team algorithm.

Then analyze it.

Suppose you add a number. Then add it next to the number of the difference of 2.

Conversely lower one. It is necessary to reduce the number of the difference between 2 and. Subtract yourself again this 1.



#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <    Cmath> #define MAXN 100005using namespace Std;int app[maxn];int save[maxn];int pos[maxn];struct foo{int l,r,index;    int ans;        BOOL operator < (const foo &AMP;CMP) const {if (pos[l] = = POS[CMP.L]) return r<cmp.r;    Return pos[l]<pos[cmp.l]; }}q[maxn];bool cmp_id (const foo &a, const foo &b) {return a.index < B.index;}    void Debug () {for (int i=0;i<=7;i++) printf ("%d", app[i]); Puts ("");}    void Modify (int p,int &ans,int add) {int tot=0;    for (int i=max (save[p]-2,0); i<=save[p]+2;i++) {tot+=app[i];    } if (add>0) Ans+=tot;    else ans-=tot-1; App[save[p]]+=add;}    int main () {int n,m;    int Cas=1;        while (scanf ("%d%d", &n,&m)!=eof) {int size= (int) sqrt (1.0*n);        memset (app,0,sizeof app); for (int i=1;i<=n;i++) {scanf ("%d", &savE[i]);        pos[i]= (i-1)/size+1;            } for (int i=0;i<m;i++) {scanf ("%d%d", &AMP;Q[I].L,&AMP;Q[I].R);        Q[i].index=i;        } sort (q,q+m);        int ans=0; for (int i=0,l=1,r=0;i<m;i++) {if (R&LT;Q[I].R) {for (r=r+1;r<=q[i].r;r++                ) modify (r,ans,1);            r--;            } if (R&GT;Q[I].R) {for (; r>q[i].r;r--) modify (r,ans,-1);            } if (L&LT;Q[I].L) {for (; l<q[i].l;l++) modify (l,ans,-1); } if (L&GT;Q[I].L) {for (l=l-1;l>=q[i].l;l--) modify (                l,ans,1);            l++;        } Q[i].ans=ans;        } sort (q,q+m,cmp_id);        printf ("Case%d:\n", cas++);    for (int i=0;i<m;i++) printf ("%d\n", Q[i].ans); } return 0;}


WHU OJ 1551 Pairs (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.