【CCF】除法 树状数组
【AC】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<math.h> 3 #include<cstring> 4 5 using namespace std; 6 7 typedef long long LL; 8 9 int N,M; 10 int type,l,r,v; 11 int A[100000+10]; 12 LL TreeArr[100000+10]; 13 int lowbit(int i) 14 { 15 return i&(-i); 16 } 17 18 19 void toValue(int i,int num) 20 { 21 while(i<=N) 22 { 23 TreeArr[i] += num; 24 i += lowbit(i); 25 } 26 } 27 28 LL sum(int i) 29 { 30 LL total = 0; 31 while(i != 0) 32 { 33 total += TreeArr[i]; 34 i -= lowbit(i); 35 } 36 return total; 37 } 38 39 int main(){ 40 41 cin>>N>>M; 42 memset(TreeArr,0,sizeof(TreeArr)); 43 for(int j=1; j<=N; j++){ 44 cin>>A[j]; 45 toValue(j, A[j]); 46 } 47 while(M--){ 48 cin>>type; 49 if(type==1){ 50 cin>>l>>r>>v; 51 if(v==1) continue; 52 for(int i=l; i<=r; i++){ 53 if(A[i]>=v&&A[i]%v==0){ 54 toValue(i, A[i]/v-A[i]); 55 A[i] /= v; 56 } 57 } 58 }else if(type==2){ 59 cin>>l>>r; 60 cout<< sum(r) - sum(l-1)<<endl; 61 } 62 } 63 return 0; 64 }
【90分】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<map> 9 using namespace std; 10 typedef long long ll; 11 const int maxn=1e5+2; 12 ll a[maxn]; 13 ll tree[maxn]; 14 int n,m; 15 int lowbit(int x){ 16 return x&(-x); 17 } 18 void add(int k,ll x){ 19 while(k<=n){ 20 tree[k]+=x; 21 k+=lowbit(k); 22 } 23 } 24 ll query(int k){ 25 ll res=0; 26 while(k){ 27 res+=tree[k]; 28 k-=lowbit(k); 29 } 30 return res; 31 } 32 void init(){ 33 memset(tree,0,sizeof(tree)); 34 } 35 int main(){ 36 while(~scanf("%d%d",&n,&m)){ 37 init(); 38 for(int i=1;i<=n;i++){ 39 scanf("%lld",&a[i]); 40 add(i,a[i]); 41 } 42 int opt,l,r; 43 ll v; 44 for(int i=1;i<=m;i++){ 45 scanf("%d%d%d",&opt,&l,&r); 46 if(opt==1){ 47 scanf("%lld",&v); 48 if(v==1) continue; 49 for(int j=l;j<=r;j++){ 50 if(a[j]>=v&&a[j]%v==0){ 51 add(j,a[j]/v-a[j]); 52 a[j]/=v; 53 } 54 } 55 }else{ 56 ll ans=query(r)-query(l-1); 57 printf("%lld\n",ans); 58 } 59 } 60 } 61 return 0; 62 }