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.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial