Knobody

Untitled

Sep 11th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6.  
  7. int pref[100005];
  8. int a[100005];
  9.  
  10. void update(int l,int r,int x){
  11.     pref[l]+=x;
  12.     pref[r+1]-=x;
  13. }
  14.  
  15. void printArray(){
  16.     for(int i=1;i<n;i++){
  17.         pref[i]=pref[i]+pref[i-1];
  18.     }
  19.     for(int i=0;i<n;i++){
  20.         a[i]=a[i]+pref[i];
  21.     }
  22.     cout<<"pref\n";
  23.     for(int i=0;i<n;i++){
  24.         cout<<pref[i]<<" ";
  25.     }
  26.     cout<<endl;
  27.     cout<<"array\n";
  28.     for(int i=0;i<n;i++){
  29.         cout<<a[i]<<" ";
  30.     }
  31.     cout<<endl;
  32. }
  33.  
  34. int main(){
  35.     //int n;
  36.     cin>>n;
  37.     for(int i=0;i<n;i++){
  38.         cin>>a[i];
  39.     }
  40.     while(1){
  41.         int choice;
  42.         cout<<"Enter choice= \n";
  43.         cin>>choice;
  44.         if(choice==1){
  45.             int l,r,x;
  46.             cin>>l>>r>>x;
  47.             update(l,r,x);
  48.         }
  49.         else{
  50.             printArray();
  51.             memset(pref,0,sizeof(pref));
  52.         }
  53.     }
  54.     return 0;
  55. }
Add Comment
Please, Sign In to add comment