Bzoj3343 's Magic (permission title)

Source: Internet
Author: User

Bzoj Portal (permission)

Description

The leader has recently learned a magical magic that can make a person grow taller. So he was ready to demonstrate to each hero of the Xmyz Information Group. So the N heroes gathered together again, this time they were in a row numbered 1, 2 、......、 N.
Each person's height begins with a positive integer that does not exceed 1000. The Master's magic can add an integer w to each hero's height in the closed interval [L, R] (1≤l≤r≤n). (although L=r does not conform to the written specification of the interval, we can think of it as a separate increase in the height of the hero of the L (R))
CYZ, light elder brother and Zjq and other people do not worship the evil of the Lord, so they sometimes ask the WD closed interval [L, R] How many heroes height is greater than or equal to C, to verify that the master's magic is really effective.
WD was lazy, so he gave you the task of answering.

Input

1th Act two integers n, Q. Q is the sum of the number of questions and the number of spells cast.
The 2nd line has n positive integers, and the number of I represents the height of the hero of the first I.
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, C. Ask how many heroes in the closed interval [L, R] are taller than or equal to C.

Output

For each "A" query, the output line contains only one integer, representing the number of heroes in the closed interval [L, R] that are greater than or equal to C.

Sample Input

5 3

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 heroes were 1, 2, 3, 4, 5, and at this time [1, 5] There were 2 heroes taller than or equal to 4. The master cast to 1, 2, 4, 5, 6, at this time [1, 5] There are 3 heroes taller 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.
Source

The first piece of life in the problem, feel good without words ah. The speed is still too slow, open the O2 before

CODE:

#include <cmath> #include <cstdio> #include <algorithm> using namespace std;
const int N=1E6+10;
    struct Block {int l,r,num,plus;
    int a[1010];
    inline void Keep () {sort (a+1,a+num+1);
        } inline int find (int n) {int l=1,r=num,mid;
        N-=plus;
            while (L<r) {mid= (l+r) >>1;
            if (a[mid]<n) l=mid+1;
        else R=mid;
    } return L;
}}a[1010];
int s[n];
int belong[n];
int n,q,x,y,z,ans,block,num;
char c;
    inline void read (int &n) {N=0;char C=getchar (); while (c< ' 0 ' | |
    C> ' 9 ') C=getchar ();
while (c>= ' 0 ' &&c<= ' 9 ') N=n*10+c-48,c=getchar ();
    } inline void Build () {block=sqrt (n);
    Num=n/block;if (N%block) num++;
    for (int i=1;i<=num;i++) a[i].l= (i-1) *block+1,a[i].r=i*block,a[i].num=block;
    a[num].r=n;a[num].num=a[num].r-a[num].l+1;

    for (int i=1;i<=n;i++) belong[i]= (i-1)/block+1; for (int i=1;i<=num;i++) {for (int j=a[i].l;j<=a[i].r;j++) A[I].A[J-A[I].L+1]=S[J];
    A[i].keep (); }} inline void Add (int x,int y,int z) {if (Belong[x]==belong[y]) {for (int i=x;i<=y;i++) s[i
        ]+=z;
        for (int i=a[belong[x]].l;i<=a[belong[x]].r;i++) a[belong[x]].a[i-a[belong[x]].l+1]=s[i];
        A[belong[x]].keep ();
    Return
    } for (int i=x;i<=a[belong[x]].r;i++) s[i]+=z;
    for (int i=a[belong[x]].l;i<=a[belong[x]].r;i++) a[belong[x]].a[i-a[belong[x]].l+1]=s[i];
    A[belong[x]].keep ();
    for (int i=belong[x]+1;i<belong[y];i++) a[i].plus+=z;
    for (int i=a[belong[y]].l;i<=y;i++) s[i]+=z;
    for (int i=a[belong[y]].l;i<=a[belong[y]].r;i++) a[belong[y]].a[i-a[belong[y]].l+1]=s[i];
A[belong[y]].keep ();
    } inline int Ask (int x,int y,int z) {int ans=0; if (Belong[x]==belong[y]) {for (int i=x;i<=y;i++) if (s[i]+a[belong[x]].plus>=z) ans++;
    return ans;
    } for (int i=x;i<=a[belong[x]].r;i++) if (s[i]+a[belong[x]].plus>=z) ans++;
        for (int i=belong[x]+1;i<belong[y];i++) {int p=a[i].find (z);
    if (a[i].a[p]+a[i].plus>=z) ans+=a[i].num-p+1;
    } for (int i=a[belong[y]].l;i<=y;i++) if (s[i]+a[belong[y]].plus>=z) ans++;
return ans;
    } int main () {read (n), read (q);
    for (int i=1;i<=n;i++) read (s[i]);
    Build ();
        while (q--) {C=getchar ();
        while (c!= ' A ' &&c!= ' M ') C=getchar ();
        Read (x), read (y), read (z);
        if (c== ' A ') printf ("%d\n", ask (x, Y, z));
    else Add (x, y, z);
} return 0; }

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.