[P3759] [TJOI2017] Non-industrious librarian (chunking + Tree Array)

Source: Internet
Author: User
Tags tidy

Title Description

Garidon University has an Imperial library, and Roo is a book keeper in the library reading room. His task is to arrange the books in order, so the disorderly books let him be bored, two disorderly order book let Adzuki bean produce these two pages of the number of boredom. Now there are N books that have been scrambled, and every day in the next M-Day, the book order is changed by the reader's reading. Because Roo was asked to tidy up the book at least once in the next M days. Adzuki Bean wants to know, if he does not go to the first day of finishing, the first day of his boredom is how much, so he chose the least boring day to tidy up.

Input output format input format:

The first line has two numbers, n,m, respectively, that have n books, M days

The next n lines, two numbers per line, AI and VI, respectively, indicate that the book I originally should be placed in AI position, this book has VI, guarantee there will not be a book in the same place

The next M-line, two numbers per line, XJ and YJ, represents the first XJ book of the J Day and the YJ book because the reader is reading the exchange position

Output format:

A total of M lines, a number per line, the line I is the first day I do not go to tidy, I day Bean's boredom, because this number may be very large, so the result modulo 10^9 +7 output

Input and Output sample Input Sample #: copy
5 51 12 23 34 45 51 51 52 45 31 3
Output Example # #: replication
420182848
Description

For 20% of data, 1≤ai; Xj yj≤n≤5000, m≤5000, vi≤10^5

For 100% of data, 1≤ai; Xj yj≤n≤50000, m≤50000, vi≤10^5

Do not want to write or do not write the data structure of the use of chunking, and then there are two ways of writing, one is for each block sort, one is to maintain a tree-like array for each block.

such as L and R in the same piece, L and R where two incomplete blocks, L and R across the complete block three case discussion can be.

Attention!! The first two situations must not be violent rebuilding blocks! The time of the violent reconstruction is 1200+s, if only the changing parts are updated, as long as the 80+s.

There is also a problem, because the complexity of the whole tag is $o (\frac{n}{s} \log N) $ (where S is the size of the block), so the speed of S set to $\sqrt{n \log n}$ is set to twice times the $\sqrt{n}$.

1#include <cmath>2#include <cstdio>3#include <cstring>4#include <algorithm>5typedefLong Longll;6 #defineRep (I,l,r) for (register int i=l; i<=r; i++)7 using namespacestd;8 9 Constll n=50100, md=1000000007;Ten intn,m,mx,l,r,b,a[n],v[n],sz[n],bl[n],cnt,sum[n],d[ -],c[ -][n],c1[ -][n],ans; One  A voidAddintC[],intXintK) { for(; x<=n; x+=x&-x) c[x]= (c[x]+k)%MD;} - intQueintC[],intx) {ll res=0; for(; x; x-=x&-x) res= (res+c[x])%md;returnRes;} - voidADD1 (intC1[],intXintK) { for(; x<=n; x+=x&-x) c1[x]= (c1[x]+k)%MD;} the intQue1 (intC1[],intx) {ll res=0; for(; x; x-=x&-x) res= (res+c1[x])%md;returnRes;} - int Get(intx) {returnx>=0? x:x+MD;} -  - voidCalintXintYintz) { +     if(A[x]<a[z]) ans= (ans+v[x]+v[z])%md;Elseans= (ANS-V[X]-V[Z]+MD+MD)%MD; -     if(A[y]<a[z]) ans= (ans+md+md-v[y]-v[z])%md;ElseAns= (Ans+v[y]+v[z])%MD; + } A  at voidWorkintLintR) { -     if(L==R)return; -     if(bl[l]==Bl[r]) { -Rep (i,l+1, R-1) Cal (L,r,i); Swap (a[l],a[r]); Swap (V[l],v[r]); -}Else{ -Rep (i,l+1, (bl[l]-1) *b+Sz[bl[l]]) cal (L,r,i); inRep (I, (bl[r]-1) *b+1, R-1) Cal (L,r,i); - swap (a[l],a[r]); swap (v[l],v[r]); toSum[bl[l]]= (Sum[bl[l]]+md-v[r]+v[l])%md; Sum[bl[r]]= (Sum[bl[r]]+md-v[l]+v[r])%MD; +Add (C[bl[r]],a[l],-v[l]); Add (C[bl[l]],a[r],-v[r]); ADD1 (c1[bl[r]],a[l],-1); ADD1 (c1[bl[l]],a[r],-1); -Add (C[bl[l]],a[l],v[l]); Add (C[bl[r]],a[r],v[r]); ADD1 (C1[bl[l]],a[l],1); ADD1 (C1[bl[r]],a[r],1); theRep (i,bl[l]+1, bl[r]-1){ *ans=Get((1ll*ans-(Que (c[i],a[r]-1) +1ll*que1 (c1[i],a[r]-1) *v[r])%MD)); $ans=Get((1ll*ans+ (Que (c[i],a[l]-1) +1ll*que1 (c1[i],a[l]-1) (*v[l]))%MD);Panax Notoginsengans=Get((1ll*ans-(Sum[i]-que (c[i],a[l)) +1ll* (Sz[i]-que1 (c1[i],a[l)) *v[l])%MD); -ans=Get((1ll*ans+ (Sum[i]-que (c[i],a[r)) +1ll* (Sz[i]-que1 (c1[i],a[r)) *v[r])%MD); the         } +     } A     if(A[r]<a[l]) ans= (1ll*ans+v[l]+v[r])%md;Elseans=Get((1ll*ans-(v[l]+v[r))%MD); the } +  - intMain () { $Freopen ("book.in","R", stdin); $Freopen ("Book.out","W", stdout); -scanf"%d%d", &n,&m); B= (int) sqrt (n -); -Rep (I,1, N) scanf ("%d%d",&a[i],&v[i]); theRep (I,1, n) bl[i]= (i-1)/b+1; Mx= (n1)/b+1; -Rep (I,1, mx-1) Sz[i]=b; sz[mx]=n-(mx-1)*B;WuyiRep (I,1, n) Add (C[bl[i]],a[i],v[i]), ADD1 (C1[bl[i]],a[i],1), sum[bl[i]]= (Sum[bl[i]]+v[i])%MD; theRep (I,1, N) { -ans=Get((1ll*ans+ (i1-1ll*que1 (c1[0],a[i]-1)) *v[i]+cnt-que (c[0],a[i]-1))%MD); WuAdd (c[0],a[i],v[i]); ADD1 (c1[0],a[i],1); Cnt= (Cnt+v[i])%MD; -     } AboutRep (I,1, m) scanf ("%d%d", &l,&r), Work (min (l,r), Max (l,r)), printf ("%d\n", ans); $     return 0; -}

[P3759] [TJOI2017] Non-industrious librarian (chunking + Tree Array)

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.