zoukankan      html  css  js  c++  java
  • codeforces 121 E. Lucky Array

    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

    Petya has an array consisting of n numbers. He wants to perform m operations of two types:

    • add l r d — add an integer d to all elements whose indexes belong to the interval from l to r, inclusive (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ 104);
    • count l r — find and print on the screen how many lucky numbers there are among elements with indexes that belong to the interval from l to r inclusive (1 ≤ l ≤ r ≤ n). Each lucky number should be counted as many times as it appears in the interval.

    Petya has a list of all operations. The operations are such that after all additions the array won't have numbers that would exceed 104. Help Petya write a program that would perform these operations.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of numbers in the array and the number of operations correspondingly. The second line contains n positive integers, none of which exceeds 104 — those are the array numbers. Next m lines contain operations, one per line. They correspond to the description given in the statement.

    It is guaranteed that after all operations are fulfilled each number in the array will not exceed 104.

    Output

    For each operation of the second type print the single number on the single line — the number of lucky numbers in the corresponding interval.

    Examples
    input
    3 6
    2 3 4
    count 1 3
    count 1 2
    add 1 3 2
    count 1 3
    add 2 3 3
    count 1 3
    output
    1
    0
    1
    1
    input
    4 5
    4 4 4 4
    count 1 4
    add 1 4 3
    count 1 4
    add 2 3 40
    count 1 4
    output
    4
    4
    4
    Note

    In the first sample after the first addition the array will look in the following manner:

    4 5 6

    After the second addition:

    4 8 9

    The second sample after the first addition:

    7 7 7 7

    After the second addition:

    7 47 47 7

    线段树区间修改,单点查询 

    if(v) single_change(root,i,v);

    这一句没写 T成傻逼了。

    指针线段树比数组线段树慢近300ms

    数组线段树比树状数组慢近1000ms

    屠龙宝刀点击就送

     线段树代码

    #include <ctype.h>
    #include <cstdio>
    const int N = 1e6+5; bool IsLucky[N]; int dis[N],n,m,Lucky[50]={0,4,7,44,47,74,77,444,447,474,477,744,747,774,777,4444,4447,4474,4477,4744,4747,4774,4777,7444,7447,7474,7477,7744,7747,7774,7777}; inline void Read(int &x) { bool f=0; register char ch=getchar(); for(x=0;!isdigit(ch);ch=getchar()) if(ch=='-') f=1; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; x=f?-x:x; } struct Segment { int l,r,mid,flag,upval; Segment * ch[2]; Segment() { ch[0]=ch[1]=NULL; flag=upval=0; } }; inline void pushup(Segment *&k) {k->upval=k->ch[0]->upval+k->ch[1]->upval;} void build(Segment *&k,int l,int r) { k=new Segment; k->l=l;k->r=r; if(l==r) { if(IsLucky[dis[l]]) k->upval+=1; return; } k->mid=l+r>>1; build(k->ch[0],l,k->mid); build(k->ch[1],k->mid+1,r); pushup(k); } int query(Segment *&k,int l,int r) { if(k->l==l&&k->r==r) return k->upval; if(l>k->mid) return query(k->ch[1],l,r); else if(r<=k->mid) return query(k->ch[0],l,r); else return query(k->ch[0],l,k->mid)+query(k->ch[1],k->mid+1,r); } void single_change(Segment *&k,int t,int v) { if(k->l==k->r) { k->upval+=v; return; } if(t<=k->mid) single_change(k->ch[0],t,v); else single_change(k->ch[1],t,v); pushup(k); } int Main() { Read(n); Read(m); for(int i=1;i<=30;++i) IsLucky[Lucky[i]]=1; for(int i=1;i<=n;++i) scanf("%d",&dis[i]); Segment *root=new Segment; build(root,1,n); char str[10]; for(int x,y,z;m--;) { scanf("%s",str+1); if(str[1]=='c') { Read(x); Read(y); printf("%d ",query(root,x,y)); } else { Read(x); Read(y); Read(z); for(int i=x;i<=y;++i) { int v=0; if(IsLucky[dis[i]]) --v; dis[i]+=z; if(IsLucky[dis[i]]) ++v; if(v) single_change(root,i,v); } } } return 0; } int sb=Main(); int main(int argc,char *argv[]){;}

     树状数组

    #include <ctype.h>
    #include <cstdio>
    const int N = 1e6+5;
    bool IsLucky[N];
    int tag[N],dis[N],n,m,Lucky[50]={0,4,7,44,47,74,77,444,447,474,477,744,747,774,777,4444,4447,4474,4477,4744,4747,4774,4777,7444,7447,7474,7477,7744,7747,7774,7777};
    inline void Read(int &x)
    {
        bool f=0;register char ch=getchar();
        for(x=0;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
        for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
        x=f?-x:x;
    }
    inline int lowbit(int x) {return x&(-x);}
    inline void modify(int x,int y)
    {
        for(;x<=n;x+=lowbit(x)) tag[x]+=y;
    }
    inline int ask(int x)
    {
        int sum=0;
        for(;x;x-=lowbit(x)) sum+=tag[x];
        return sum;
    }
    int main()
    {
        Read(n);Read(m);
        for(int i=1;i<=30;++i) IsLucky[Lucky[i]]=1;
        for(int i=1;i<=n;++i) {Read(dis[i]);if(IsLucky[dis[i]]) modify(i,1);} 
        char str[10];
        for(int x,y,z;m--;)
        {
            scanf("%s",str+1);
            if(str[1]=='c')
            {
                Read(x);
                Read(y);
                printf("%d
    ",ask(y)-ask(x-1));
            }
            else 
            {
                Read(x);
                Read(y);
                Read(z);
                for(int i=x;i<=y;++i)
                {
                    int v=0;
                    if(IsLucky[dis[i]]) --v;
                    dis[i]+=z;
                    if(IsLucky[dis[i]]) ++v;
                    if(v) modify(i,v);
                }
            }
        }
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    【转】 grep 文件报错 “Binary file ... matches”
    JS 将值插入数组中
    Vue 刷新当前页面,并重新加载页面数据
    Vue 获得所选中目标的状态(checked)以及对应目标的数据,并进行相应的操作
    JS --- 如何获取一个对象的类型
    vue-cli项目开发/生产环境代理实现跨域请求+webpack配置开发/生产环境的接口地址
    VUE 处理文本框获焦点高亮
    VUE项目问题之:去掉url中的#/
    关于移动端终极适配解决方案
    VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7397519.html
Copyright © 2011-2022 走看看