Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array

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, 467are 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.

input

3 62 3 4count 1 3count 1 2add 1 3 2count 1 3add 2 3 3count 1 3

output

1011

题意:给出一组序列然后找出区间lcuk数字的个数。

sl: 线段树树状数组第一时间确定是这个数据结构,剩下的就不会了。

扫了一眼题解发现都是树状数组暴力搞得。时间快的就是线段树加一个优化。

优化: 维护区间内的数字到达一个luck数字的最小距离。要是区间修改之后不能使得最小的距离变为0 那么就直接lazy修改下区间luck距离的最小值。

要是超过了,那么就递归到叶子节点直接修改。 时间复杂度我在研究研究。

1 // by caonima
  2 // hehe
  3 #include <bits/stdc++.h>
  4 using namespace std;
  5 const int MAX = 1e5+10;
  6 int num[MAX<<2],Min[MAX<<2] ,col[MAX<<2],cnt[MAX<<2];
  7 int a[MAX],Luck_num[MAX],cur=0;
  8 int is_luck[MAX];
  9 char op[10];
 10 void dfs(int u) {
 11     if(u>444444) return ;
 12     is_luck[u]=true;
 13     Luck_num[cur++]=u;
 14 
 15     dfs(u*10+4);
 16     dfs(u*10+7);
 17     return ;
 18 }
 19 int check(int x) {
 20     for(int i=0;i<cur;i++) {
 21         if(x<Luck_num[i]) return Luck_num[i]-x;
 22     }
 23     return 0;
 24 }
 25 void push_up(int o) {
 26     cnt[o]=cnt[o<<1]+cnt[o<<1|1];
 27     Min[o]=min(Min[o<<1],Min[o<<1|1]);
 28 }
 29 void build(int L,int R,int o) {
 30     if(L==R) {
 31         num[o]=a[L]; col[o]=0;
 32         if(is_luck[a[L]]) cnt[o]=1;
 33         else cnt[o]=0;
 34         Min[o]=check(a[L]);
 35         return ;
 36     }
 37     int mid=(L+R)>>1;
 38     build(L,mid,o<<1);
 39     build(mid+1,R,o<<1|1);
 40     push_up(o);
 41 }
 42 void push_down(int o,int m) {
 43     if(col[o]) {
 44         col[o<<1]+=col[o]; col[o<<1|1]+=col[o];
 45         Min[o<<1]-=col[o<<1]; Min[o<<1|1]-=col[o<<1|1];
 46         cnt[o<<1]=cnt[o<<1|1]=0;
 47         col[o]=0;
 48     }
 49 }
 50 
 51 int Query(int L,int R,int o,int ls,int rs) {
 52     if(ls<=L&&rs>=R) {
 53         return cnt[o];
 54     }
 55     push_down(o,R-L+1);//printf("s");
 56     int mid=(R+L)>>1;
 57     int cnt=0;
 58     if(ls<=mid) cnt+=Query(L,mid,o<<1,ls,rs);
 59     if(rs>mid) cnt+=Query(mid+1,R,o<<1|1,ls,rs);
 60     push_up(o);
 61     return cnt;
 62 }
 63 
 64 void Update(int L,int R,int o,int ls,int rs,int val) {
 65     if(ls<=L&&rs>=R) {
 66         if(L==R) {
 67             num[o]+=col[o]+val;
 68             col[o]=0;
 69           //  printf("%d\n",num[o]);
 70             if(is_luck[num[o]]) cnt[o]=1;
 71             else cnt[o]=0;
 72             Min[o]=check(num[o]);
 73 
 74             return ;
 75         }
 76         if(col[o]+val<Min[o]) {
 77             Min[o]-=(col[o]+val);
 78             col[o]+=val;
 79             cnt[o]=0;
 80             return ;
 81         }
 82     }
 83 
 84     push_down(o,R-L+1);
 85     int mid=(R+L)>>1;
 86     if(ls<=mid)  Update(L,mid,o<<1,ls,rs,val);
 87     if(rs>mid) Update(mid+1,R,o<<1|1,ls,rs,val);
 88     push_up(o);
 89     return ;
 90 }
 91 
 92 int main() {
 93     int n,m,ls,rs,v;
 94     dfs(4); dfs(7);
 95     sort(Luck_num,Luck_num+cur);
 96     while(scanf("%d %d",&n,&m)==2) {
 97         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
 98         build(1,n,1);
 99         for(int i=1;i<=m;i++) {
100             scanf("%s",op);
101             if(op[0]==‘c‘) {
102                 scanf("%d %d",&ls,&rs);
103                 int ans=Query(1,n,1,ls,rs);
104                 printf("%d\n",ans);
105             }
106             else {
107                 scanf("%d %d %d",&ls,&rs,&v);
108                 Update(1,n,1,ls,rs,v);
109             }
110         }
111     }
112     return 0;

113 }

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array,布布扣,bubuko.com

时间: 2024-10-27 08:01:36

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array的相关文章

codeforces水题100道 第十二题 Codeforces Beta Round #91 (Div. 2 Only) A. Lucky Division (brute force)

题目链接:http://www.codeforces.com/problemset/problem/122/A题意:判断一个数是否能被一个lucky number整除,一个lucky number是一个只包含4或7的数.C++代码: #include <cstdio> int lucky[14] = {4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777}; bool check(int x) { for (int i = 0

Codeforces Beta Round #91 (Div. 1 Only) A. luckSum(区间分块求解)

这道题的主要解法在对1~100000000 中的luckNumber进行分块求和; 例如, luckSum[0] = Sum[1,4]  luckSum[1] = Sum[5,7] luckSum[2] = Sum[8,44] ...................... 然后根据l,r 的区间跨度来求和; 所在区间用一个int findUnder(int l)来求取; #include<iostream> #include<cstring> #include<cmath&g

Codeforces Beta Round #12 (Div 2 Only)

Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 t

Codeforces Beta Round #49 (Div. 2)

Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门 1 /* 2 BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 3 只要撑过这个时间就能win,否则lose 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <cstring> 10 using namespace std; 11 1