jzoj4243 [five school joint examination 6day1] c Block

Description

S is defined as the set of all positive integers consisting of 4 and 7.
For 1 ≤ i ≤ N, ai is given. M operations are required:
add l r v add v to all ai of i ∈ [l, r]
count l r counts how many i satisfy i ∈ [l, r] and ai ∈ S

Data Constraint

50% of the data meet the requirements of N,M ≤ 103
100% of the data meet the requirements of 1 ≤ N,M ≤ 105, 1 ≤ ai ≤ 104, 1 ≤ v ≤ 104
Data guarantee ai ≤ 104 at the end of all operations.

Solution

The first topic of konjaku is divided into handwritten parts. Let's sigh
Divide the sequence into n √ blocks and maintain buckets for each block.
For a (l,r) query, modify both ends violently, and mark the middle block
For a (l,r,v) modification, mark the two ends and the middle block of the violent modification
It means that the balance tree is too hard to adjust, so roll over and write something else

Code

#include <stdio.h>
#include <string.h>
#include <math.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))
#define N 200005
#define M 20005
int rec[N],pos[N],add[N],a[N],s[N],e[N],t[501][M];
int n,m;
void read(int &x) {
    x=0; int v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    x*=v;
}
void update(int x,int &y,int w) {
    --t[x][y]; y+=w; ++t[x][y];
}
void prework() {
    int st=1; rec[0]=0;
    rec[++rec[0]]=4;
    rec[++rec[0]]=7;
    while (st<=rec[0]) {
        int now=rec[st++];
        if (now*10>=M) return;
        rec[++rec[0]]=now*10+4;
        rec[++rec[0]]=now*10+7;
    }
}
void modify(int l,int r,int v) {
    if (pos[l]==pos[r]) {
        rep(i,l,r) update(pos[l],a[i],v);
    } else {
        rep(i,s[pos[r]],r) update(pos[r],a[i],v);
        rep(i,l,e[pos[l]]) update(pos[l],a[i],v);
        rep(i,pos[l]+1,pos[r]-1) add[i]+=v;
    }
}
int query(int l,int r) {
    int ret=0;
    if (pos[l]==pos[r]) {
        rep(i,l,r) {
            rep(j,1,rec[0]) ret+=(a[i]+add[pos[l]])==rec[j];
        }
    } else {
        rep(i,s[pos[r]],r) {
            rep(j,1,rec[0]) ret+=(a[i]+add[pos[r]])==rec[j];
        }
        rep(i,l,e[pos[l]]) {
            rep(j,1,rec[0]) ret+=(a[i]+add[pos[l]])==rec[j];
        }
        rep(i,pos[l]+1,pos[r]-1) {
            rep(j,1,rec[0]) ret+=t[i][rec[j]-add[i]];
        }
    }
    return ret;
}
int main(void) {
    prework();
    read(n); read(m);
    int size=(int)(sqrt(n));
    rep(i,1,n) {
        read(a[i]);
        pos[i]=(i-1)/size+1;
        if (!s[pos[i]]) s[pos[i]]=i;
        e[pos[i]]=i;
        ++t[pos[i]][a[i]];
    }
    while (m--) {
        char opt[5];
        int l,r,v;
        scanf("%s",opt);
        if (opt[0]=='c') {
            read(l); read(r);
            printf("%d\n",query(l,r));
        } else {
            read(l); read(r); read(v);
            modify(l,r,v);
        }
    }
    return 0;
}

Posted on Sun, 31 May 2020 13:31:27 -0400 by CONFUSIONUK