hdu5212 Code MO Team algorithm

Source: Internet
Author: User

This problem requires some knowledge definition notation for the MO team algorithm.
    
        f(A,B)    
  
 
    
 Indicates the answer with the query interval a, B, and the Notation + represents the collection and uses the MO team algorithm we can calculate any
    
        f(a,a)    
  
 
    
 The value may be assumed
   
    A=[L1,R1],B=[L2,R2],C  =[R1+1,L2?1]
   Easy to know
   
    F  (A,B)=F  (A+B+C  ,A+B+C  )+F  (C  ,C  )?F  (A+C  ,A+C  )?F  (C  +B,C  +B)
   So a query is split into four queries that can be done with the MO team algorithm the total time complexity is
   
     
     o   ( m  s  q   r   T   (  n  )   )          
    
   
 
     
      (above is the official puzzle)  
      
 
     
      code:  
      
   
    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <map>using namespace std;typedef long long ll;const int N = 30000*4 + 10;int pos[n];struct pp{&nbsp ;   int l,r,id;    int ans;} P[n];int CMP (pp a,pp b) {    if (POS[A.L]==POS[B.L]) return A.R < b.r;    return A.L < B.L;} int CMP2 (pp a,pp b) {    return a.id < b.id;} struct que{    int l1,l2,r1,r2;} Q[n];int Block,n,k,m,num;int A[n],cnt[n],answer; LL x;map<ll,int> mm;void Update (int x,int v) {    int val = k-a[x];    if (val <= 0) return;     Answer + = cnt[val]*v;    Cnt[a[x]] + = V;} void Solve () {    int l,r;    answer = 0;    for (int i=1,l=1,r=0;i<=num;i++) {//by block update &nbs P       for (; r<p[i].r;r++)             Update (r+1,1);      &NB Sp for (; r>p[i].r;r--)   &nbsp         Update (R,-1);        for (; l<p[i].l;l++)         &NBS P   Update (L,-1);        for (; l>p[i].l;l--)             Update (L-1 , 1);        P[i].ans = answer;   }}int main () {    while (scanf ("%d", &n)!=eof) {        mm.clear ();        num = 0;        block = (int) sqrt ( N) +1;        for (int i=1;i<=n;i++) Pos[i] = I/block + 1;        scanf ("%d", &amp k);        memset (cnt,0,sizeof (CNT));        for (int i=1;i<=n;i++)   & nbsp         scanf ("%d", &a[i]);        scanf ("%d", &m);      &NB Sp for (int i=1;i<=m;i++) {            int l1,l2,r1,r2;          &NBS P scanf ("%d%d%d%d ", &AMP;L1,&AMP;R1,&AMP;L2,&AMP;R2);            Q[I].L1 = L1, q[i].r1 = r1, q[i].l2 = L2, Q[i].r2 = r2;            P[++NUM].L = L1, p[num].r = l2-1;      &NBSP ;      x = l1*40000+ (l2-1);            MM[X] = num;            P[num].id = num;            P[++NUM].L = r1+1, P[NUM].R = R2;             x = (r1+1) *40000+r2;            MM[X] = num;            P[num].id = num;            if (l2-1 >= r1+1) {        & nbsp       P[++NUM].L = r1+1, P[NUM].R = l2-1;                x = (r1+ 1) *40000+ (l2-1);                MM[X] = num;          &NB Sp   &NBSP; P[num].id = num;           }            P[++NUM].L = L1, p[n UM].R = R2;            x = l1*40000+r2;            mm[x] = Nu m;            p[num].id = num;       }        sort ( P+1,P+1+NUM,CMP);        solve ();        sort (P+1,P+1+NUM,CMP2);    & nbsp   for (int i=1;i<=m;i++) {            int l1,l2,r1,r2;        &NBS P   L1 = q[i].l1, L2 = q[i].l2, r1 = q[i].r1, r2 = q[i].r2;            LL ans = 0;&nb Sp           x = l1*40000+r2;            ans + p[mm[x]].ans;  &N Bsp         if (l2-1>=r1+1) {                x = (r1+1) *40000+ (L2 -1); &NBSp               ans + p[mm[x]].ans;           }  &NB Sp         x = (r1+1) *40000+r2;            ans-= p[mm[x]].ans;  &nbsp ;         x = l1*40000+ (l2-1);            ans-= p[mm[x]].ans;  &nbsp ;         printf ("%lld\n", ans);       }   }    return 0;}
   

hdu5212 Code mo algorithm

Related Article

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.