[bzoj3343]&&[2810] [chunking] [in-block ordering] [two-point search] The magic of the guru _ chunking

Source: Internet
Author: User
bzoj3343&& 2810, the guru's magic.

Description
The Archbishop has recently learned a magical magic that can make people grow taller. So he prepared to show the Xmyz information group to each hero to see. So the N heroes came together again, and this time they were lined up, numbered 1, 2 、......、 N.
Everyone's height begins with a positive integer that is no more than 1000. The Guru's magic can add an integer w to the height of the hero in the closed interval [L, R] (1≤l≤r≤n). (although L=r does not conform to the writing specification of the interval, we can think of increasing the height of the first L (R) hero alone)
CYZ, light elder brother and Zjq and other people do not worship the evil, so they sometimes ask the WD closed interval [L, R] How many hero height is greater than or equal to C, to verify that the master's magic is really effective.
WD was so lazy that he gave you the answer to the task.
Input
1th behavior two integers n, Q. Q is the sum of the number of questions and the master's cast.
The 2nd row has n positive integers, and the number I represents the height of the first hero.
The 3rd to Q+2 Line has one action per line:
(1) If the first letter is "M", then there are three numbers L, R, W. Represents the height of all heroes in the closed interval [L, R] plus W.
(2) If the first letter is "A", then there are three numbers L, R, and C immediately thereafter. Ask how many heroes within the closed interval [L, R] are taller than or equal to C.
Output
For each "a" query output line, containing only an integer, indicating the closed interval [L, R] The number of heroes in height greater than or equal to C.
Sample Input
4 R
1 2 3 4 5
A 1 5 4
M 3 5 1
A 1 5 4
Sample Output
2
3
HINT
"Input and Output sample description"
The original 5 hero height of 1, 2, 3, 4, 5, at this time [1, 5] between 2 hero height is greater than or equal to 4. After the master cast into 1, 2, 4, 5, 6, at this time [1, 5] between 3 hero height is greater than or equal to 4.
"Data Range"
For 30% of the data, n≤1000,q≤1000.
For 100% of the data, n≤1000000,q≤3000,1≤w≤1000,1≤c≤1,000,000,000.

Solving
The first time to write a chunk,,,, feel really good, and more than 150 lines of line tree feel really good too much,,,. And this problem line tree seems to only modify, and can not query the number of larger than C,,, ... However, the fastest of the query is, of course, two points.
All this problem is divided into two points, block, including block in order.
Code

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < Algorithm> #define LL long #ifdef WIN32 #define AUTO "%i64d" #else #define AUTO "%lld" #endif using namespace std
;
const int N = 1000050;
int n,q;
The number of int Block,m;//logn, a total of M block int pos[n];//in the first block int a[n],b[n]; int add[1005];//block with addition mark void Reset (int x) {int l= (x-1) *block+1,r=min (x*block,n);//Prevent last piece,, end for (int i = l < I = R;
i++) b[i]=a[i];//reset block, updata after sort (b+l,b+r+1);
    int find (int x,int v) {int l= (x-1) *block+1,r=min (x*block,n);
    int last=r;
        while (l<=r) {int mid= (L+R) >>1;
        if (b[mid]<v) l=mid+1; Else r=mid-1;//is greater than or equal to} return last-l+1;//is minus L!!!!!!!!!, last l==r,} void updata (int x,int Y,int v) {if pos[x]==pos[
        Y] for (int i = x; i <= y; i++) a[i]+=v;//whole block is incomplete else {for (int i = x; I <=pos[x]*block; i++) a[i]+=v;
    for (int i = (pos[y]-1) *block+1 i <= y; i++) a[i]+=v;
}//violence on both sides of the range    Reset (pos[x]), Reset (Pos[y]);//reset block for (int i = pos[x]+1 i <pos[y]; i++) add[i]+=v;//mark whole block} int query (i
    NT X,int Y,int v) {int sum=0;
    if (Pos[x]==pos[y]) {for (int i = x; i<=y; i++) if (a[i]+add[pos[i)] >= v) sum++;
        else {for (int i = x; I <= pos[x]*block; i++) if (A[i] + add[pos[i]] >= v) sum++;
    for (int i = (pos[y]-1) *block+1; i<=y;i++) if (a[i) +add[pos[i]]>=v) sum++;
for (int i = pos[x]+1 i<pos[y];i++) sum+=find (I,v-add[i]);//Remove the tag's answer return sum;
    int main () {freopen ("cod.in", "R", stdin);
    Freopen ("Cod.out", "w", stdout);
    scanf ("%d%d", &n,&q);
    Block=int (sqrt (n));
    if (n%block) m = n/block + 1;
    else M = N/block;
        for (int i = 1; I <= n; i++) {scanf (auto,&a[i]);
        pos[i]= (i-1)/block+1;
    B[i]=a[i];
    for (int i = 1; i<= m; i++) reset (i);//within block sort for (int i = 1; I <= Q; i++) {    Char ch[5];
        int x,y,c;
        scanf ("%s%d%d%d", ch,&x,&y,&c);
        if (ch[0]== ' M ') updata (X,Y,C);
    else printf (AUTO "\ n", query (X,y,c));
return 0; }

It's quite comfortable to write chunks. Better than the line, you can ask a lot of dongdong,,,, well, although I am also the first time to write. The complexity of the chunking is also O (NLOGN)

Hzwer

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.