Mohib's friend is struggling with an easy problem given by Argusoft in Placement Interview, he was unable to solve the problem and sadly he was disqualified from the process.But Mohib being a good friend of him decide to help by solving this problem and explaining it to him so that he can solve similar problems in future interviews.
He told you question as given below now it's time for you to do the magic.
you are given an array of size 1018, initially all the elements of array are 1. now you will be given two kind of queries to process on the array.
Type 1: L R X, add X into all the elements in range [L, R].
Type 2: L R, in this query, you need to find a non-empty subset S of elements in range [L, R], which has the maximum possible beauty.
Beauty of a subset is calculated using following function.
// here S is subset and n is the size of S.
long long getBeauty(long long S[],int n){
long long sum = 0, maxValue = 0;
int i;
for(i =0 ;i < n; i++) {
sum += S[i];
maxValue = max(maxValue,S[i]);
}
return (maxValue * n - sum);
}
For queries of second kind, size of subset S may be very large, so you are required to output only maximum element of S.
Input:
First line of the input will contain an integer Q which is number of queries you need to process. followed by Q lines containing queries you need to process.
You are required to present the online solution, each query will be generated using following algorithm,
int q;
scanf("%d",&q);
long long last = 0;
long long SIZE = 1e18;
while(q--){
int type;
long long L,R;
scanf("%d %lld %lld",&type,&L,&R);
L = (L + last)%SIZE + 1;
R = (R + last)%SIZE + 1;
if (L > R) {
swap(L,R);
}
if(type == 1){
long long X;
scanf("%lld",&X);
// your logic.
} else {
// your logic.
last = answer; // here, answer is the answer for the current query.
}
}
Output
For each query of second type output the maximum element of S.
If there exist multiple S with maximum beauty then select S which has largest, maximum element in it.
Constraints:
1 <= Q <= 105
1 <= L <= R <= 1018
1 <= X <= 109
1 <= Type <= 2
given array is 1 base indexed.
for simplicity lets consider first 5 elements of the array,
Initially array will be : [1, 1, 1, 1, 1]
after the first update, L =2,R = 3, X = 6: [1, 6, 6, 1, 1]
after the second update, L =1,R = 3, X = 2: [2, 8, 8, 1, 1]
after the third update, L =3,R = 4, X = 3: [2, 8, 11, 4, 1]
for the fourth query, elements in range [1, 2] are {2, 8}
so, there are three possible subset that you can pick,
1. { 2 } beauty of this will be 0.
2. { 8 } beauty of this will be 0.
3. {2, 8} beauty of this will 6.
since third subset has the maximum beauty, so the answer will be maximum element of this subset which is 8.
for the fifth query, elements in range [L = (2 + 8),R = (4 + 8)] are {1, 1}
so, there are three possible subset that you can pick,
1. {1} beauty of this will be 0.
2. {1} beauty of this will be 0.
3. {1, 1} beauty of this will 0.
since all subsets has the maximum beauty, so the answer will be 1 in this case.