CodeForces E. Lucky Array 幸运数列

CodeForces    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, 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 62 3 4count 1 3count 1 2add 1 3 2count 1 3add 2 3 3count 1 3

output

1011

input

4 54 4 4 4count 1 4add 1 4 3count 1 4add 2 3 40count 1 4

output

444

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

中文题面 :http://cogs.pro/cogs/problem/problem.php?pid=1922

先找出小于100000的所有幸运数 暴力修改

CF上时限4s 完全没问题

但是COGS上时限2s 就被卡吵了

 1 /*
 2     线段树光荣T了5个点
 3     还是树状数组吧
 4     但是CF上过了 只用了3.4s
 5 */
 6 #include <ctype.h>
 7 #include <cstdio>
 8
 9 const int MAXN=100010;
10
11 int n,m,x;
12
13 int a[MAXN],b[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,};
14
15 struct node {
16     int l,r;
17     int sum;
18 };
19 node t[MAXN<<2];
20
21 char s[20];
22
23 bool vis[MAXN];
24
25 inline void read(int&x) {
26     int f=1;register char c=getchar();
27     for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar());
28     for(;isdigit(c);x=x*10+c-48,c=getchar());
29     x=x*f;
30 }
31
32 void build_tree(int now,int l,int r) {
33     t[now].l=l,t[now].r=r;
34     if(l==r) {
35         read(a[l]);
36         if(vis[a[l]]) ++t[now].sum;
37         return;
38     }
39     int mid=(l+r)>>1;
40     build_tree(now<<1,l,mid);
41     build_tree(now<<1|1,mid+1,r);
42     t[now].sum=t[now<<1].sum+t[now<<1|1].sum;
43 }
44
45 void modify(int now,int pos,int v) {
46     if(t[now].l==t[now].r) {
47         t[now].sum+=v;
48         return;
49     }
50     int mid=(t[now].l+t[now].r)>>1;
51     if(pos<=mid) modify(now<<1,pos,v);
52     else modify(now<<1|1,pos,v);
53     t[now].sum=t[now<<1].sum+t[now<<1|1].sum;
54 }
55
56 int query(int now,int l,int r) {
57     if(l<=t[now].l&&r>=t[now].r) return t[now].sum;
58     int tot=0;
59     int mid=(t[now].l+t[now].r)>>1;
60     if(l<=mid) tot+=query(now<<1,l,r);
61     if(r>mid) tot+=query(now<<1|1,l,r);
62     return tot;
63 }
64
65 int hh() {
66 //    freopen("cf121e.in","r",stdin);
67 //    freopen("cf121e.out","w",stdout);
68     int x,y,v;
69     read(n);read(m);
70     for(int i=1;i<=30;++i) vis[b[i]]=true;
71     build_tree(1,1,n);
72     for(int i=1;i<=m;++i) {
73         scanf("%s",s);
74         if(s[0]==‘c‘) {
75             read(x);read(y);
76             int t=query(1,x,y);
77             printf("%d\n",t);
78         }
79         else {
80             read(x);read(y);read(v);
81             for(int i=x;i<=y;++i) {
82                 int k=0;
83                 if(vis[a[i]]) --k;
84                 a[i]+=v;
85                 if(vis[a[i]]) ++k;
86                 if(k) modify(1,i,k);
87             }
88         }
89     }
90     return 0;
91 }
92
93 int sb=hh();
94 int main() {;}

线段树代码

 1 /*
 2     树状数组T了1个点
 3     呜呜呜 -绝望-
 4     CF上竟然过了 用了2.4s
 5 */
 6 #include <ctype.h>
 7 #include <cstdio>
 8
 9 #define lowbit(x) x&(-x)
10
11 const int MAXN=100010;
12
13 int n,m,x;
14
15 int a[MAXN],t[MAXN],b[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,};
16
17 char s[20];
18
19 bool vis[MAXN];
20
21 inline void read(int&x) {
22     int f=1;register char c=getchar();
23     for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar());
24     for(;isdigit(c);x=x*10+c-48,c=getchar());
25     x=x*f;
26 }
27
28 inline void add(int x,int v) {
29     for(int i=x;i<=n;i+=lowbit(i))
30       t[i]+=v;
31 }
32
33 inline int query(int x) {
34     int tot=0;
35     for(int i=x;i;i-=lowbit(i)) tot+=t[i];
36     return tot;
37 }
38
39 int hh() {
40 //    freopen("cf121e.in","r",stdin);
41 //    freopen("cf121e.out","w",stdout);
42     int x,y,z;
43     read(n);read(m);
44     for(int i=1;i<=30;++i) vis[b[i]]=true;
45     for(int i=1;i<=n;++i) {
46         read(a[i]);
47         if(vis[a[i]]) add(i,1);
48     }
49     for(int i=1;i<=m;++i) {
50         scanf("%s",s);
51         read(x);read(y);
52         if(s[0]==‘a‘) {
53             read(z);
54             for(int i=x;i<=y;++i) {
55                 int k=0;
56                 if(vis[a[i]]) --k;
57                 a[i]+=z;
58                 if(vis[a[i]]) ++k;
59                 if(k) add(i,k);
60             }
61         }
62         else printf("%d\n",query(y)-query(x-1));
63     }
64     return 0;
65 }
66
67 int sb=hh();
68 int main() {;}

树状数组代码

时间: 2024-12-21 06:37:04

CodeForces E. Lucky Array 幸运数列的相关文章

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 arra

Codeforces 482B Interesting Array(线段树)

题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,现在有M个限制,每个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 否有满足的数列. 解题思路:线段树维护,每条限制等于是对l~r之间的数或上q(取且的性质,相应二进制位一定为1),那么处理完所有的 限制,在进行查询,查询对应每个l~r之间的数取且是否还等于q.所以用线段树维护取且和,修改为或操作. #include <cstdio> #include <c

【线段树】【树状数组】【CF 121E】幸运数列

1922. [CF 121E]幸运数列 ★★★ 输入文件:cf121e.in 输出文件:cf121e.out 简单对比 时间限制:3 s 内存限制:256 MB [题目描述] 对于欧洲人来说,"幸运数"是指那些十进制只由4或7组成的数.财务员Petya需要维护一个支持如下操作的整数数列: add l r d - 表示将[l, r]区间内的所有数加上一个正整数d(). count l r - 统计[l, r]区间内有多少个"幸运数".() 请你帮助Petya实现它.

codeforces 482B. Interesting Array【线段树区间更新】

题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val.就是区间l---r上的与的值为val,最后问你原来的数组是多少?如果不存在输出no 分析:分析发现要满足所有的区间,而一个点上假如有多个区间的话,这个点的值就是所有区间或的值,因为只有这样才能满足所有区间的,把所有位上的1都保存下来了,那么可以发现用线段树来维护,但是那么怎么判断满不满足条件呢?可以也用线段树,更新了之后在整个维护一遍看看满不满足题意,如

codeforces 797 E. Array Queries【dp,暴力】

题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为止,求每次询问要转换的次数. 题解:纯暴力会TLE,所以在k为根号100000范围内dp打表 dp[i][j]表示初始p为i, k为j,需要转换几次可以大于n. 状态转移方程:dp[i][j] = dp[i+a[i]+j] + 1 #include <cstdio> #include <al

codeforces 407C Curious Array

codeforces 407C Curious Array 参考题解:https://www.cnblogs.com/ChopsticksAN/p/4908377.html 1.杨辉三角可以由多维前缀和求得. 2."搭顺风车"的思想. 3.// 右端点减去的那个数可以用组合数学的方法思考. #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define mp ma

Lucky Player幸运玩家系统开发

Lucky Player游戏APP系统开发找,定制开发Lucky Player幸运玩家平台模式,Lucky Player幸运玩家是什么Lucky Player幸运玩家源码开发公司,Lucky Player幸运玩家app模式定制开发等 一.Lucky Player幸运玩家的介绍 Lucky Player是一款基于以太坊智能合约开发的去中心化类Fomo3D游戏DAPP,下载安装APP即可参与游戏. 多种玩法,智能合约:奖励丰厚,游戏空投,3倍出局制,后面加入的玩家也能分红迅,这是一个计时器停止时最后

Codeforces 1054D Changing Array

Codeforces 1054D Changing Array 做法:给定一个序列,每个数可以把在2进制k位下取反,也可以不变,在改变后,这个序列异或和不为0的区间的个数.考虑如何求出尽可能少的异或为0的序列,对序列求前缀之后,就相当与问这个前缀的序列中,有多少对的值相同,注意还有开始的0.那么对于所有数取值为min(a,~a),现在我们需要最小化,更新后同一种数中出现的相同的数对的个数,即\(C(a,2) + C(w-a,2)\),w是这种数的个数,a是取反的数的个数,当\(a = \frac

【题解】幸运数列

题目描述 幸运数列是指该数列的每一个数都至少是它前面的数的两倍.假设数列的元素个数是N,元素的值范围是从1到M,当N=4,M=10时,可以生成下面4个幸运数列: 1 2 4 8 1 2 4 9 1 2 4 10 1 2 5 10 给定N和M,你的任务是算出有多少个幸运数列可供选择. 输入输出格式 输入格式 一行,按照N和M的顺序排列且满足1≤N≤10, 1≤M≤1000,N和M被一个空格分隔. 输出格式 一行,只需输出数对N和M所产生的幸运数列的个数. 输入输出样例 输入样例 4 10 输出样例