树状数组(cf round#91(div.1 only) problem E)

  树状数组是一种快速区间求和的数据结构,,lowBit()函数来实现与其他分支的子树进行跳转

int lowBit(x){ return x&(-x}

 

                             E. Lucky Array
                            time limit per test
                            4 seconds
                            memory limit per test
                            256 megabytes

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



#include<iostream>
#include<cstdio>
using namespace std;

int c[100100];
int a[100100];
int n,m;
bool luck[100100]={false};
void init(){
    luck[4]=luck[7]=luck[44]=luck[47]=luck[74]=luck[77]=luck[444]=
    luck[447]=luck[474]=luck[744]=luck[477]=luck[747]=luck[774]=
    luck[777]=luck[4444]=luck[4447]=luck[4474]=luck[4744]=luck[7444]=
    luck[4477]=luck[4747]=luck[7447]=luck[4774]=luck[7474]=luck[7744]=
    luck[4777]=luck[7477]=luck[7747]=luck[7774]=luck[7777]=1; 
}

int lowBit(int x){
    return x&(-x);
}

void update(int x,int v){
    
    while(x<=n){
        c[x]+=v;
        x += lowBit(x);
    }
    
}


int sum(int num){
    int summ;
    summ=0;
    while(num>0){
        summ+=c[num];
        num-=lowBit(num);
    }
    return summ;
}

int main(){
    init();
    char st[10];
    int temp,under1,under2,ad;
    
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(luck[a[i]]) update(i,1);
    }
    
    //for(int i=1;i<=n;i++) cout<<c[i]<<endl;
    
    while(m--){
        scanf("%s%d%d",st,&under1,&under2);
        if(st[0]=='c') printf("%d\n",sum(under2)-sum(under1-1));
        else{
            scanf("%d",&ad);
            
            if(ad==0) continue;
            for(int i=under1;i<=under2;i++){
                if(luck[a[i]]==0&&luck[a[i]+ad]==1){
                    update(i,1);
                }else if(luck[a[i]]==1&&luck[a[i]+ad]==0){
                    update(i,-1);
                }
                a[i]+=ad;
//                if(luck[a[i]]==1) update(i,-1);
//                a[i]+=ad;
//                if(luck[a[i]]==1) update(i,1);
            }
            
        }
    }
    
    
    return 0;
} 

 

转载于:https://www.cnblogs.com/yuanshixingdan/p/5479623.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值