Sunday, 28 February 2016

*D. Factory Repairs

D. Factory Repairs

A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but will be restored to its full production of a thimbles per day after the k days are complete.
Initially, no orders are pending. The factory receives updates of the form diai, indicating that ai new orders have been placed for thedi-th day. Each order requires a single thimble to be produced on precisely the specified day. The factory may opt to fill as many or as few of the orders in a single batch as it likes.
As orders come in, the factory owner would like to know the maximum number of orders he will be able to fill if he starts repairs on a given day pi. Help the owner answer his questions.
Input
The first line contains five integers nkab, and q (1 ≤ k ≤ n ≤ 200 0001 ≤ b < a ≤ 10 0001 ≤ q ≤ 200 000) — the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively.
The next q lines contain the descriptions of the queries. Each query is of one of the following two forms:
  • di ai (1 ≤ di ≤ n1 ≤ ai ≤ 10 000), representing an update of ai orders on day di, or
  • pi (1 ≤ pi ≤ n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day pi?
It's guaranteed that the input will contain at least one query of the second type.
Output
For each query of the second type, print a line containing a single integer — the maximum number of orders that the factory can fill over all n days.
Examples
input
5 2 2 1 8
1 1 2
1 5 3
1 2 1
2 2
1 4 2
1 3 2
2 1
2 3
output
3
6
4
input
5 4 10 1 6
1 1 5
1 5 5
1 3 2
1 5 2
2 1
2 2
output
7
1
Note
Consider the first sample.
We produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days.
For the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that day, and 2 orders for day 5 since we are limited to our production capacity, for a total of 3 orders filled.
For the third question, we are able to fill 1 order on day 11 order on day 2, and 2 orders on day 5, for a total of 4 orders.

-------------------------------------------------EDITORIAL------------------------------------------------------------------------------
THIS PROBLEM CAN BE SOLVED EASILY IF WE MAINTAIN 2 DIFFERENT SEGMENT TREE,  FIRST SEG TREE WILL CONTAIN ALL  VALUES AT ALL INDEX NOT > a AND SECOND  CONTAIN VALUES 
NOT >b , NOT  UPDATE BOTH TREE SUCH THAT EACH TREE INDEX WILL NOT CONTAIN VALUES > ITS RANGE a OR  b , 
IN CASE OF QUERY , USE 1ST SEG TREE BEFORE REPAIR , AND SECOND SEG TREE AFTER REPAIR ....

------------------------------------------------CODE----------------------------------------------------------------------------------
#include<iostream>
#include<bits/stdc++.h>
typedef long long int lli;
#include<string.h>
long long int  arr[1000005],seg1[10000005],seg2[10000005];
using namespace std;
#define inf 100000001

 long long  int  qry1(int index,int start,int end,int qs,int qe)
  {
   
  if(start>end || end<qs || start>qe)
       {
        return 0;
       }
        if(start>=qs && end<=qe)
         {
          return  seg1[index];       
         }
        long long  int q1=qry1(2*index,start,(start+end)/2,qs,qe);
        long long  int q2=qry1(2*index+1,((start+end)/2)+1,end,qs,qe);
         return q1+q2;
  }
  
  
void update1(int index,int start,int end,int ups,int upe,lli val,lli tt)
{
   if(start>end || start>upe || end<ups) return ;  
   if(start>=ups && end<=upe)
   {
        seg1[index]+=val;      
        if(seg1[index]>tt) seg1[index]=tt;
        return ;
    }
     update1(2*index,start,(start+end)/2,ups,upe,val,tt);
     update1(2*index+1,((start+end)/2)+1,end,ups,upe,val,tt);
     seg1[index]=seg1[2*index]+seg1[2*index+1];
 }



 long long  int qry2(int index,int start,int end,int qs,int qe)
  {
  if(start>end || end<qs || start>qe)
       {
        return 0;
       }
     
        if(start>=qs && end<=qe)
         {
          return  seg2[index];
         }
        long long  int q1=qry2(2*index,start,(start+end)/2,qs,qe);
        long long  int q2=qry2(2*index+1,((start+end)/2)+1,end,qs,qe);
         return q1+q2;
  }
  
  
void update2(int index,int start,int end,int ups,int upe,lli val,lli tt)
{
   if(start>end || start>upe || end<ups) return ;
   if(start>=ups && end<=upe)
   {
        seg2[index]+=val;
        if(seg2[index]>tt) seg2[index]=tt;
         return ;
    }
     update2(2*index,start,(start+end)/2,ups,upe,val,tt);
     update2(2*index+1,((start+end)/2)+1,end,ups,upe,val,tt);
     seg2[index]=seg2[2*index]+seg2[2*index+1];
 }



int main()
 {
   lli n,k,a,b,q;
    cin>>n>>k>>a>>b>>q;
    while(q--)
     {
      int type;
       cin>>type;
      if(type==1)
       {
        lli index,val;
        cin>>index>>val;
        index--;
        update1(1,0,n-1,index,index,val,a);
        update2(1,0,n-1,index,index,val,b);
  }
  else
  {
  long long int ans=0;
   int  index;
    cin>>index;
    index--;
    ans=qry2(1,0,n-1,0,index-1);
    ans+=qry1(1,0,n-1,index+k,n-1);
    printf("%lld\n",ans);
  }
}
 }

Thursday, 25 February 2016

***E. Copying Data

E. Copying Data
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.
More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types:
  1. Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, executeby + q = ax + q for all integer q (0 ≤ q < k). The given operation is correct — both subsegments do not touch unexistent elements.
  2. Determine the value in position x of array b, that is, find value bx.
For each query of the second type print the result — the value of the corresponding element of array b.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| ≤ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| ≤ 109).
Next m lines contain the descriptions of the queries. The i-th line first contains integer ti — the type of the i-th query (1 ≤ ti ≤ 2). Ifti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 ≤ xi, yi, ki ≤ n) — the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 ≤ xi ≤ n) — the position in array b.
All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.
Output
For each second type query print the result on a single line.
Examples
input
5 10
1 2 0 -1 3
3 1 5 -2 0
2 5
1 3 3 3
2 5
2 4
2 1
1 2 1 4
2 1
2 4
1 4 2 1
2 2
output
0
3
-1
3
2
3
-1

-----------------------------------------------editorial-------------------------------------------------------------
this  problem can be done in o(nlogn) complexity if we use seg tree, we can see  the constrains , we cant copy actually in the array so we some how need to contain details about    for any index i ans is from first array or from second array and if from the first array than at what index from  the first array ,
struct st
{
int state ;
 
int start;
 
}  seg[1000000],lzy[1000000];

in this structure state is whether for any index ,  answer belong from the 2nd array or from the first array , state=0 means from the second array , and state =1 means from the first array , 
also start will contain the  if ans is from 1st array that at what index from the first array ....

as we can see that there can be multiple times update in the same range we need to keep lzy[]so that need not to apply unnecessary updates .......


--------------------------------------------CODE----------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
#include<iostream>
lli ans=0;
#include<string.h>
struct st
{
int state ;
 
int start;
 
}  seg[1000000],lzy[1000000];

long long int arr[1000005],brr[1000000];

using namespace std;
#define inf 100000001
 int x,y,len;
 int diff;
 
void  qry(int index,int start,int end,int qs,int qe)
  {
      if(lzy[index].state!=0)
 {
           seg[index].start=lzy[index].start;
           seg[index].state=1;
          if(start!=end)
            {
           int mid=(start+end)/2;
           int lf=mid-start+1;
           int rt=end-(mid);
           lzy[2*index].state=1;
           lzy[2*index].start=lzy[index].start;
           lzy[2*index+1].state=1;
           lzy[2*index+1].start=lzy[index].start+lf;
           
           }
          lzy[index].state=0;
         }
         
         if(start>end || end<qs || start>qe)
         {
           return ;
          }
        if(start>=qs && end<=qe)
         {
           if(seg[index].state==1)
            {
            ans=arr[seg[index].start];
           
}
else ans=brr[start];
          return ;
         }
        qry(2*index,start,(start+end)/2,qs,qe);
       qry(2*index+1,((start+end)/2)+1,end,qs,qe);
  }
  
     
  
void update(int index,int start,int end,int ups,int upe)
{
    if(start>end || start>upe || end<ups) 
   {
    return ;
   }
    
     if(lzy[index].state!=0)
 {
           seg[index].start=lzy[index].start;
           seg[index].state=1;
          if(start!=end)
            {
           int mid=(start+end)/2;
           int lf=mid-start+1;
           int rt=end-(mid);
           lzy[2*index].state=1;
           lzy[2*index].start=lzy[index].start;
           lzy[2*index+1].state=1;
           lzy[2*index+1].start=lzy[index].start+lf;
           
           }
          lzy[index].state=0;
         }
   if(start>=ups && end<=upe)
    {
        seg[index].state=1;
    seg[index].start=start+diff;
        if(start!=end)
         {
          int mid=(start+end)/2;
          int lf=mid-start+1;
           lzy[2*index].state=1;
           lzy[2*index].start=start+diff;
           
           lzy[2*index+1].state=1;
           
           lzy[2*index+1].start=start+lf+diff;
         }
         return;
    }
    
   
      int mid=(start+end)/2;
     
     update(2*index,start,mid,ups,upe);
     update(2*index+1,((start+end)/2)+1,end,ups,upe);
 }
 
int main()
 {
    int n,m ;
cin>>n>>m;
for(int i=0;i<n;i++)
 {
  scanf("%lld",&arr[i]);
}
for(int i=0;i<n;i++)
 {
  scanf("%lld",&brr[i]);
}
 
for(int i=0;i<=n+10;i++)
 {
  seg[i].state=0;
  seg[i].start=i;
 }
for(int i=0;i<m;i++)
 {
  int type ;
  scanf("%d",&type);
  if(type==1)
   {
   
    scanf("%d %d %d",&x,&y,&len);
    x--;
    y--;
     diff=x-y;
    update(1,0,n-1,y,y+len-1);
 }
 else
 {
  ans=0;
  int idx;
  scanf("%d",&idx);
  idx--;
  qry(1,0,n-1,idx,idx);
  printf("%lld\n",ans);
 }
 }
 return 0;
 }

Wednesday, 24 February 2016

****D. Babaei and Birthday Cake(LONGEST INCREASING SUM)

D. Babaei and Birthday Cake

As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.
Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.
However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.
Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has.
Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake.
Output
Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
Examples
input
2
100 30
40 10
output
942477.796077000
input
4
1 1
9 7
1 4
10 7
output
3983.539484752
Note
In first sample, the optimal way is to choose the cake number 1.
In second sample, the way to get the maximum volume is to use cakes with indices 12 and 4.

------------------------------------------------------editorial-----------------------------------------------------------------------------
this is a simple question we need to find maximum sum in an array and all included elements must be strictly increasing,
means we have to find maximum sum in increasing sub sequence ..
first come with a brute  dp approach ,  dp[i]=max(dp[i], dp[j]+val[i]) for all j<i and val[j]< val[i] ,
this can be easily computed in o(n^2) , but we have to it in the o(nlogn) complexity ,
for  that we have to use a segment  tree approach ,
sort all the values in  pair ((r*r*h) , index)  means minimum volume first ,  before  now start computing from the first, before computing ans for a volume r*r*h we need to  computed value of all volume < than its volume and is in the left of it , since we have sort according to the r*r*h than it is sure that  all volume < its volume computed , 
now we have to find best answer from all volumes < its volume and is in the right of it , this can be done using range max query easily, but main problem is that some of the vol < its vol can be in the right of this index also , how to not consider that , for that at the time of updating value of any vol update it at its original index not at the sorted index position , and also for query  find according to the original index not at the sorted index ,,, its confusing see the code for clarity ,,.. or read next editorial..
--------------------------------------------EDITORIAL-----------------------------------------------------------------------------
 First of all, we calculate the volume of each cake: vi = π × hi × ri2.
Now consider the sequence v1v2v3, ..., vn : The answer to the problem is the maximum sum of elements between all increasing sub-sequences of this sequence. How do we solve this? First to get rid of the decimals we can define a new sequence a1a2a3, ..., ansuch that 
We consider dpi as the maximum sum between all the sequences which end with ai and
dpi = 
The answer to the problem is: π × maxi = 1ndp[i]
Now how do we calculate  ? We use a max-segment tree which does these two operations: 1. Change the i't member to v2. Find the maximum value in the interval 1 to i.
Now we use this segment tree for the array dp and find the answer.
Consider that a1a2a3, ..., an is sorted. We define bi as the position of ai. Now to fill dpi we find the maximum in the interval [1, bi) in segment and we call it x and we set the bi th index of the segment as ai + x. The answer to the problem would the maximum in the segment in the interval [1,n]
Time complexity: O(nlgn)

-------------------------------------------------------code---------------------------------------------------
#include<bits/stdc++.h>
typedef long long int lli;
using namespace std;
lli dp[1000000];
#include<iostream>
using namespace std;
#define inf -9999999999999999
bool comp(pair<long long, int> a, pair<long long, int> b)
{
if (a.first != b.first)
return a.first < b.first;
return a.second>b.second;
}
lli  query(lli node,lli start,lli end,lli r1,lli r2)
 {  
 if(start>end || r1>end || r2<start) return 0;

   if(r1<=start && r2>=end)
    {
     return dp[node];
    }
    else
    {
     lli  q1=query(2*node,start,(start+end)/2,r1,r2);
    lli  q2=query(2*node+1,((start+end)/2)+1,end,r1,r2);
     return max(q1,q2);
    }
 }
void update(lli node ,lli start,lli end,lli r1,lli r2,lli val)
 {

  if(r1>end  || r2<start   || start>end) return  ;

  if(r1<=start && r2>=end)
   {
     dp[node]+=val;
      return  ;
   }
    update(2*node, start,(start+end)/2,r1,r2,val);
    update(2*node+1, ((start+end)/2)+1,end,r1,r2,val);
   dp[node]=max(dp[2*node],dp[2*node+1]);
 }
int  main()
 {
  lli n;
  scanf("%lld",&n);
  vector<pair<lli,lli> > v;
  for(lli i=0;i<n;i++)
  {
  lli h,r;
  scanf("%lld %lld",&r,&h);
  v.push_back({r*r*h,i});  
 }
 sort(v.begin(),v.end(),comp);
 lli ans=0;
 for(lli i=0;i<n;i++)
  {
  lli vol=v[i].first;
   lli index=v[i].second;
   
   lli maxi=query(1,0,n-1,0,index-1);
   // cout<<" maxi is "<<maxi<<endl;
   
   update(1,0,n-1,index,index,maxi+vol);
   }
   ans=query(1,0,n-1,0,n-1);
   
   
  double dd=3.1415926535897932384626433*(double)ans;
  printf("%.12lf",dd);
 }