首页 > 代码库 > Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array
Codeforces Beta Round #91 (Div. 1 Only) 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.
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
题意:给出一组序列然后找出区间lcuk数字的个数。
sl: 线段树树状数组第一时间确定是这个数据结构,剩下的就不会了。
扫了一眼题解发现都是树状数组暴力搞得。时间快的就是线段树加一个优化。
优化: 维护区间内的数字到达一个luck数字的最小距离。要是区间修改之后不能使得最小的距离变为0 那么就直接lazy修改下区间luck距离的最小值。
要是超过了,那么就递归到叶子节点直接修改。 时间复杂度我在研究研究。
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 }