树状数组(cf round#91(div.1 only) problem E)
树状数组是一种快速区间求和的数据结构,,lowBit()函数来实现与其他分支的子树进行跳转
int lowBit(x){ return x&(-x}
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.
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.
For each operation of the second type print the single number on the single line — the number of lucky numbers in the corresponding interval.
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
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; }