Advertisement
Guest User

1183 - Computing Fast Average

a guest
Dec 4th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define mx 100005
  3. #define ll long long
  4.  
  5. using namespace std;
  6.  
  7. struct info
  8. {
  9.     ll prop,sum;
  10. } tree[mx*3];
  11.  
  12.  
  13. void pushdown(int node,int from,int to)
  14. {
  15.     if(tree[node].prop!=-1)
  16.     {
  17.         ll mid = (from+to)/2;
  18.  
  19.         tree[2*node].sum = (mid-from+1)*tree[node].prop;
  20.  
  21.         tree[2*node+1].sum = (to-mid)*tree[node].prop;
  22.  
  23.         tree[2*node].prop = tree[node].prop;
  24.  
  25.         tree[2*node+1].prop = tree[node].prop;
  26.  
  27.         tree[node].prop = -1;
  28.  
  29.     }
  30. }
  31.  
  32.  
  33. void pushup(int node)
  34. {
  35.     tree[node].sum = tree[2*node].sum+tree[2*node+1].sum;
  36. }
  37.  
  38.  
  39. void update(ll node,ll b,ll e,ll i,ll j,ll x)
  40. {
  41.     if(i>e||j<b)
  42.         return;
  43.  
  44.     if(b>=i&&e<=j)
  45.     {
  46.         tree[node].sum = (e-b+1)*x;
  47.         tree[node].prop = x;
  48.         return;
  49.     }
  50.  
  51.     pushdown(node,b,e);
  52.  
  53.     ll mid = (b+e)/2;
  54.  
  55.     update(2*node,b,mid,i,j,x);
  56.  
  57.     update(2*node+1,mid+1,e,i,j,x);
  58.  
  59.     pushup(node);
  60.  
  61. }
  62.  
  63.  
  64. ll query(ll node,ll b,ll e,ll i,ll j)
  65. {
  66.  
  67.     if(i>e||j<b)
  68.         return 0;
  69.  
  70.     if(b>=i&&e<=j)
  71.         return tree[node].sum;
  72.  
  73.     pushdown(node,b,e);
  74.  
  75.     ll mid = (b+e)/2;
  76.  
  77.     ll p=0;
  78.  
  79.     p+= query(node*2,b,mid,i,j);
  80.  
  81.     p+=query(node*2+1,mid+1,e,i,j);
  82.  
  83.     pushup(node);
  84.  
  85.     return p;
  86.  
  87. }
  88.  
  89.  
  90. int main()
  91. {
  92.  
  93.     ll T,N,q,i,j,v,check;
  94.  
  95.     scanf("%lld",&T);
  96.  
  97.     for(int ii=1; ii<=T; ii++)
  98.     {
  99.         cout<<"Case "<<ii<<":"<<endl;
  100.  
  101.         for(int i=0; i<=mx*3; i++)
  102.         {
  103.             tree[i].sum = 0;
  104.             tree[i].prop = -1;
  105.         }
  106.  
  107.         scanf("%lld%lld",&N,&q);
  108.  
  109.         while(q--)
  110.         {
  111.             scanf("%lld",&check);
  112.  
  113.             if(check==1)
  114.             {
  115.                 scanf("%lld%lld%lld",&i,&j,&v);
  116.  
  117.                 update(1,0,N-1,i,j,v);
  118.  
  119.             }
  120.  
  121.             else
  122.             {
  123.                 scanf("%lld%lld",&i,&j);
  124.  
  125.                 ll sum = query(1,0,N-1,i,j);
  126.  
  127.                 ll range = j-i+1;
  128.  
  129.                 ll GCD = __gcd(range,sum);
  130.  
  131.                 sum/=GCD;
  132.  
  133.                 range/=GCD;
  134.  
  135.                 if(range==1)
  136.                     printf("%lld\n",sum);
  137.  
  138.                 else if(sum)
  139.                     printf("%lld/%lld\n",sum,range);
  140.  
  141.                 else
  142.                     printf("0\n");
  143.  
  144.             }
  145.  
  146.         }
  147.  
  148.     }
  149.  
  150.     return 0;
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement