"Algorithmic" chunking--Guru's Magic & non-industrious librarian

Source: Internet
Author: User

By the hard-working librarian brought into the pit of the block, deeply by its violence and elegance conquered. The essence of the block is the violent block package up, a whole piece of the whole piece of processing, piecemeal part on how to violence. Complexity is guaranteed because of the size of the block, which limits the order of the bits and pieces of data.

1. Guru's Magic: can be counted as a sub-block of the board problem. For each block sort, save the ID value. For modifications, the block within the found point of violence is modified after reordering, a whole block does not change relative size relationships, so the direct external record accumulates the value. Query also the same, block outside violence, block binary search.

#include <bits/stdc++.h>using namespacestd;#defineint long Long#defineMAXN 2000000#defineMAXB 1050intN, Q, B, SUM[MAXB], A[MAXN], CNT[MAXB];structnode{intV, id;} C[MAXB][MAXB];intRead () {intx =0; CharC; C=GetChar ();  while(C <'0'|| C >'9') C =GetChar ();  while(c >='0'&& C <='9') x = x *Ten+ C-'0', C =GetChar (); returnx;}BOOLCMP (Node A, Node B) {returnA. V <B.V;}voidVio_c (intBintXintW) {C[B][X].V+=W;}voidChange (intLintRintW) {    intA = l/b, B = R/B; if(A = =b) { for(inti =1; I <= Cnt[a]; i + +)             if(c[a][i].id >= L && c[a][i].id <=R) Vio_c (A, I, W); Sort (C[a]+1, C[a] + cnt[a] +1, CMP); }     for(inti =1; I <= Cnt[a]; i + +)         if(C[a][i].id >=L) Vio_c (A, I, W); Sort (C[a]+1, C[a] + cnt[a] +1, CMP);  for(inti =1; I <= cnt[b]; i + +)        if(C[b][i].id <=R) Vio_c (b, I, W); Sort (C[b]+1, C[b] + cnt[b] +1, CMP);  for(inti = A +1; I < b; i + +) Sum[i]+=W;}intCheckintBintC) {C-=Sum[b]; intAns = cnt[b] +1, L =1, r =Cnt[b];  while(L <=r) {intMid = (L + r) >>1; if(c[b][mid].v >= c) ans = Mid, R = mid-1; ElseL = mid +1; }    returnCnt[b]-ans +1;}voidQuery (intLintRintC) {    intA = l/b, B = R/B; intAns =0; if(A = =b) {C-=Sum[a];  for(inti =1; I <= Cnt[a]; i + +)            if(c[a][i].id >= L && c[a][i].id <= R && c[a][i].v >= c) ans + +; printf ("%lld\n", ans); return; }     for(inti =1; I <= Cnt[a]; i + +)           if(c[a][i].id >= L && c[a][i].v + sum[a] >= c) ans + +;  for(inti =1; I <= cnt[b]; i + +)        if(c[b][i].id <= R && c[b][i].v + sum[b] >= c) ans + +;  for(inti = A +1; I < b; i + +) ans+=check (i, C); printf ("%lld\n", ans); return; } Signed Main () {n= Read (), q =read (); B=sqrt (n);  for(inti =1; I <= N; i + +) {A[i]= Read ();intblock = I/B; c[block][+ + CNT[BLOCK]].V =A[i]; C[block][cnt[block]].id=i; if((i/b)! = (i +1)/B) | | i = =N) sort (C[block]+1, C[block] + Cnt[block] +1, CMP); }     for(inti =1; I <= Q; i + +)    {        CharC CIN >>C; intL = Read (), R = Read (), W =read (); if(c = ='M') Change (L, R, W); ElseQuery (L, R, W); }    return 0;}

2. Non-industrious librarian

This problem first notice the position of exchange A, a, only affects the number between A and B. What is the contribution of a to the clutter in a certain interval? It is not difficult to find the sum of the +v[a]* of books before a and which should be after a and the number of books in front of them. So we use two tree-like arrays for each block, a record count, a prefix for the number of pages, and a block of fast queries using a tree array, and the brute force enumeration outside the block is calculated.

Because this problem code is Kuai (at that time will not write the block), so do not post code.

"Algorithmic" chunking--Guru's Magic & non-industrious librarian

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.