HDU 5273 Dylans loves sequence (interval inverse logarithm-mo team algorithm)

Source: Internet
Author: User

n<=1000,q<=100000, in order to find the logarithm of the interval, from [l,r] can obviously log (n) time to move to [l-1,r],[l+1,r],[l,r-1],[l,r+1], then you can use MO team offline

The complexity is probably O (n*sqrt (n) *log2 (n)), but can be violent to raise points, and then back to the statistics, then O (1) answer, but N is no longer able to solve the big, even if the n<=1e5 can quickly get answers, open-O optimization, 1e6 can also be quickly answered

#include <bits/stdc++.h>using namespace std;const int Maxn=1e5+5;const int maxm=1e5+5;/* number range is larger, discretized to [1,n] just a few There are 1000, q queries [l,r] between the reverse logarithm if [l,r] to seek [l,r+1] or [l,r-1], as long as statistics a[i] contribution is good a[i] contribution can be obtained by querying the tree array log2 (1000) time to get this property is suitable for the MO team algorithm, the overall The Complexity O (n*sqrt (n) *log2 (n) +q) can be quickly answered even if n<=1e5, can also quickly get answers */void Jia (int x) {printf ("Add%d\n", x);} void Jian (int x) {printf ("--%d\n", x);}    void read (int &res) {char c=getchar ();    while (!isdigit (c)) C=getchar ();    res=0; while (IsDigit (c)) res=res*10+c-' 0 ', C=getchar ();} int a[maxn],b[maxn],c[maxn],d[maxn];int n,q,block;int ql[maxm],qr[maxm],qq[maxm],ans[maxm];inline int lowbit (int x) { Return x& (-X);} BOOL Cmp1 (int I,int j) {return a[i]<a[j];}    void Add (int x,int val) {//if (val>0) Jia (x); else jian (x); while (x<=n) c[x]+=val,x+=lowbit (x);}    int query (int x) {int res=0;    while (x>0) res+=c[x],x-=lowbit (x); return res;}    BOOL Cmp2 (int i,int j) {int q1=ql[i]/block;    int q2=ql[j]/block; if (Q1!=Q2) return q1<q2;    if (Qr[i]!=qr[j]) return qr[i]<qr[j]; return qr[i]<qr[j];}    int main () {freopen ("in", "R", stdin);    Freopen ("WA", "w", stdout);    Read (n); read (q);    BLOCK=SQRT (n);    for (int i=1;i<=n;i++) read (A[i]), b[i]=i;    Sort (B+1,B+1+N,CMP1);    int tot=0,last=-1;        for (int i=1;i<=n;i++) {if (a[b[i]]==last) D[b[i]]=tot;    else D[b[i]]=++tot,last=a[b[i]];    } for (int i=0;i<q;i++) read (Ql[i]), read (Qr[i]), qq[i]=i;    Sort (QQ,QQ+Q,CMP2);    int l=1,r=1,res=0;    Add (d[1],1);        for (int i=0;i<q;i++) {int l=ql[qq[i]],r=qr[qq[i]];            while (R<r) {res+=r-l+1-query (d[r+1]);            Add (d[r+1],1);        r++;            } while (R>r) {res-=r-l+1-query (d[r]);            Add (d[r],-1);        r--;            } while (L<l) {res-=query (d[l]-1);            Add (d[l],-1);        l++;            } while (L>l) {res+=query (d[l-1]-1);            Add (d[l-1],1);        l--; } ANs[qq[i]]=res;    } for (int i=0;i<q;i++) printf ("%d\n", Ans[i]); return 0;}

  

HDU 5273 Dylans loves sequence (interval inverse logarithm-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.