Devu And Churu
Devu and Churu love to play games a lot. Today, they have an array A consisting of N positive integers. First they listed all N × (N+1) / 2 non-empty continuous subarrays of the array A on a piece of paper and then replaced all the subarrays on the paper with the maximum element present in the respective subarray.
Devu and Churu decided to play a game with numbers on the paper. They both have decided to make moves turn by turn. In one turn, the player picks some number from the list and discards that number. The one who is not able to make a valid move will be the loser. To make the game more interesting, they decided to put some constraints on their moves.
A constraint on a game can be any of following three types :
- > K : They are allowed to choose numbers having values strictly greater than K only.
- < K : They are allowed to choose numbers having values strictly less than K only.
- = K : They are allowed to choose numbers having values equal to K only.
Given M constraints and who goes first, you have to tell the outcome of each game. Print 'D' if Devu wins otherwise print 'C' without quotes.
Note that M games are independent, that is, they'll rewrite numbers by using array A after each game. (This is the task for the loser of the previous game!)
Input
First line of input contains two space separated integers N and M denoting the size of array A and number of game played by them. Next line of input contains N space-separated integers denoting elements of array A. Each of the next M lines of input contains three space-separated parameters describing a game. First two parameter are a character C ∈ {<, >, =} and an integer K denoting the constraint for that game. The last parameter is a character X ∈ {D, C} denoting the player who will start the game.
Output
Output consists of a single line containing a string of length M made up from characters D and C only, whereith character in the string denotes the outcome of the ith game.
Constraints:
- 1 ≤ N, M ≤ 106
- 1 ≤ Ai, K ≤ 109
- X ∈ {D, C}
- C ∈ {<, >, =}
Subtasks:
- Subtask 1 : 1 ≤ N, M ≤ 104 : ( 20 pts )
- Subtask 2 : 1 ≤ N, M ≤ 105 : ( 30 pts )
- Subtask 3 : 1 ≤ N, M ≤ 106 : ( 50 pts )
Example:
Input:
3 5
1 2 3
> 1 D
< 2 C
= 3 D
> 4 C
< 5 D
Output:
DCDDC
Explanation:
Subarray List :
- [1]
- [2]
- [3]
- [1,2]
- [2,3]
- [1,2,3]
Numbers on the paper after replacement :
Game 1 : There are only 5 numbers > 1 in the list.
Game 2 : There is only 1 number < 2 in the list.
Game 3 : There are only 3 numbers = 3 in the list.
Game 4 : There are no numbers > 4 in the list. So the first player cannot make his move.
Game 5 : There are 6 numbers < 5 in the list.
---------------------------------------------------------------editorial-----------------------------------------------------------
PREREQUISITES:
stock span problem, stack, binary search, value compression, combinatorics
PROBLEM:
Given an array [A1,…,AN]. Let B be the list of maximums of all subarrays of A (B has length N(N+1)2). Mgames will be played, each with a single constraint of the form "C K", where C is either <, =, >, and K is an integer. In a single game, players alternate turns, and in a move a player marks off an integer in B satisfying the constraint "C K", and the player with no more valid moves is the loser.
You have to determine the winner of each game.
QUICK EXPLANATION:
Each game depends only on the
parity of the number of integers satisfying the constraint. Thus, we only need to find the number of values in
B satisfying the constraint.
Let Li be the largest index Li<i such that ALi≥Ai (set Li=0 if no such value exists)
Let Ri be the smallest index Ri>i such that ARi>Ai (set Ri=N+1 if no such value exists)
There can be at most N distinct values in A, say [V1,…,VM] with M≤N (in sorted order). Let f(j) be the number of subarrays in which Vj is the maximum. Then f(j)=∑Ai=j(Ri−i)(i−Li).
There are three kinds of constraints "<K", "=K" and ">K". we need to know the sum of the f(j) for all j in which Vj satisfies the constraint. We introduce a fourth kind of constraint, "≤K". The other three kinds of constraints can be reduced to this.
To answer a "≤K" constraint, first find the largest index j such that Vj≤K (with binary search). Then the result we want is f(1)+⋯+f(j). This number can quickly be obtained if we precompute all prefix sums beforehand.
EXPLANATION:
The solution has multiple parts. We start with the most obvious questions first, so that we know what to precompute, etc.
A "game"
Let's first discuss how a game occurs. Suppose we have the list B, which contains the N(N+1)2 maximums among all subarrays (of course, we can't construct this array in the harder subtasks because of its size). The first thing to notice is that the order of B's elements doesn't matter. Thus, we can assume that B is sorted.
Let's first assume that we have a constraint. The key thing to notice is that the game is really simple: the players simply alternate turns marking off numbers in B satisfying the constraint. In fact, the winner is solely dependent on the number of values in B satisfying the constraint, i.e., it doesn't matter whatever strategy both players are using! To be specific:
- If there are an even number of such numbers, then the second player always moves last.
- If there are an odd number of such numbers, then the first player always moves last.
Therefore, we only need to count those elements of B satisfying the constraint! Because B is sorted and because of the nature of the constraints, this number can be computed using one or two binary searches!
To illustrate, let I(K) be the number of elements of B that are ≤K (or simply the index of the largest i such that Bi≤K, which can be computed with a binary search). Then:
- The number of elements of B that are <K is I(K−1).
- The number of elements of B that are =K is I(K)−I(K−1).
- The number of elements of B that are >K is N(N+1)2−I(K).
Thus, if we already have the array B, then we can easily compute the result of any game in O(log(N(N+1)2))=O(logN) time.
Run-length encoding
Unfortunately, the previous approach uses up O(N2) memory because of the array B. But we can reduce this significantly by noticing that there are only at most N distinct values in B. (Why?) So let [V1,…,VM] be the distinct values in B, and let f(i) be the number of occurrences of Vi in B.
We can then compute I(K) by doing a binary search in V instead for the largest i such that Vi≤K, and then I(K)is simply f(1)+f(2)+⋯+f(i). If we precompute all prefix sums of the f(i), then I(K) can still be computed in O(logN) time, but the memory requirements decrease to just O(N).
Computing V and f(i)
To complete the algorithm, we need to compute the array
[V1,…,VM] and the values
f(1),…,f(M). Note that the sequence
V can be computed in
O(NlogN) time from the array
A (with a simple
sort + uniquify operation). Thus, all that remains is to compute the
f(i)s.
First, let's suppose that all elements of A are distinct. Consider the value Ai. How many subarrays are there whose maximum is Ai? Well, let Li and Ri are the nearest larger elements to the left and right of Ai, respectively. Then the subarray cannot contain either ALi or ARi. Thus, the subarray should be strictly contained in [ALi+1,…,ARi−1], but due to the way Li and Ri are defined, we see that the latter condition is sufficient. Thus, there are (Ri−i)(i−Li) possible subarrays (there are (Ri−i) and (i−Li) ways to choose the right and left endpoints, respectively). Let us denote this quantity by m(i).
As an example, consider the following image (Ai is the height of the bar over the number i):
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
1 2 3 4 5 6 7 8
In this case, suppose i=5. Then Li=1 and Ri=8. Thus, there are (Ri−i)=3 and (i−Li)=4 choices for the right and left endpoints, respectively, for a total of 3×4=12 subarrays, as shown below:
1 2 3 4 5 6 7 8
(2 3 4 5)
(3 4 5)
(4 5)
(5)
(2 3 4 5 6)
(3 4 5 6)
(4 5 6)
(5 6)
(2 3 4 5 6 7)
(3 4 5 6 7)
(4 5 6 7)
(5 6 7)
As a consequence, we have f(j)=m(i) for the (unique) i such that Ai=Vj.
In case either Li or Ri doesn't exist, we can use the values 0 or N+1, respectively.
This formula, and the expression m(i)=(Ri−i)(i−Li) is nice; unfortunately it doesn't extend straightforwardly if the Ais are not all distinct. To see why, consider the following example:
# # #
# # # # # #
# # # # # # #
# # # # # # # # #
1 2 3 4 5 6 7 8 9
The natural extension of f(j)=m(i) would be f(j)=∑Ai=Vjm(i). However, in the above case, it doesn't quite work. Consider j=3. There are three is with Ai=3, namely i=3, i=5 and i=8. But we have m(3)=6, m(5)=6 and m(8)=2, so according to this formula this gives f(3)=6+6+2=14. However, the correct value of f(3) is 10! (verify) Clearly we need to fix this formula.
We can do that by redefining m(i) so that it counts the subarrays whose leftmost maximum is Ai. This uses the fact that each subarray has a unique leftmost maximum, so no subarray is double-counted. To implement this, redefine Liby weakening it so that ALi can equal Ai (i.e. ALi≥Ai). Then m(i) stays the same, i.e. (Ri−i)(i−Li). In the example above, we get the new values m(3)=6, m(5)=2 and m(8)=2, so f(3) is correctly computed as 6+2+2=10.
But how do we compute the
Lis and
Ris? Actually, these values can be computed easily in
O(N) time by means of a stack: it's actually a standard problem called the
stock span problem. (Recognizing this as the stock span problem requires experience, but if you didn't know about this, notice that the problem of computing the
Lis and
Ris can be done in
O(NlogN) time with a segment tree)
Now, all that remains is to compute the f(i)s given the m(i)s. But this can be done easily in linear time also:
- Create an array for f, all initialized to zero.
- Create an inverse map of V, say ฯ, where ฯ(Vi)=i.
- For every 1≤i≤N, add the value m(i) to f(ฯ(Ai)).
That's it! We now have all the parts needed for the whole solution. The preprocessing takes O(NlogN) time, and each query can be answered in O(logN) time! The following is an example implementation in C++:
-------------------------------------------------------------CODE-----------------------------------------------------------------------
#include<iostream>
using namespace std;
typedef int lli;
#include<bits/stdc++.h>
#define maxi 10000000000
lli value[1000100];
lli rit[1000000];
lli lft[1000000];
stack<pair<lli,lli> > stac,stac2;
map<lli,lli> mymap;
vector<lli> val;
int display_ans(lli val , char player)
{
//cout<<" function call "<<endl;
if(val%2==0)
{
if(player=='C')
{
-
}
else
-
}
-
return 0;
}
int read_int(){
char r;
bool start=false,neg=false;
int ret=0;
while(true){
-
if((r-'0'<0 || r-'0'>9) && r!='-' && !start){
continue;
}
if((r-'0'<0 || r-'0'>9) && r!='-' && start){
break;
}
if(start)ret*=10;
start=true;
if(r=='-')neg=true;
else ret+=r-'0';
}
if(!neg)
return ret;
else
return -ret;
}
int main()
{
-
lli n,q;
n=read_int();
q=read_int();
for(int i=0;i<n;i++)
{
value[i]=read_int();
-
}
-
-
-
-
for(int i=n-1;i>=0;i--)
{
-
lli num=1;
while(!stac2.empty() && value[i]>stac2.top().first)
{
num+=stac2.top().second;
stac2.pop();
}
rit[i]=num-1;
-
-
stac2.push(make_pair(value[i],num));
-
}
-
-
for(int i=0;i<n;i++)
{
lli num=1;
while(!stac.empty() && value[i]>=stac.top().first)
{
-
num+=stac.top().second;
stac.pop();
-
}
-
lft[i]=num-1;
stac.push(make_pair(value[i],num));
-
}
-
-
-
-
-
-
-
vector<pair<lli,lli> >tot;
for(int i=0;i<n;i++)
{
-
tot.push_back(make_pair(value[i],rit[i]+lft[i]+1+rit[i]*lft[i]));
mymap[value[i]]+=tot[i].second;
}
-
// for(int i=0;i<n;i++) cout<<tot[i].first<<" "<<tot[i].second<<endl;
// cout<<endl;
-
sort(tot.begin(),tot.end());
vector<lli> toti;
// for(int i=0;i<n;i++) cout<<tot[i].first<<" "<<tot[i].second<<endl;
// cout<<endl;
toti.push_back(tot[0].second);
val.push_back(tot[0].first);
for(int i=1;i<n;i++)
{
-
toti.push_back(toti[i-1]+tot[i].second);
val.push_back(tot[i].first);
}
// for(int i=0;i<toti.size();i++) cout<<toti[i]<<" ";
// cout<<endl;
-
// cout<<" query start"<<endl;
//*************** query**************************************//
-
// cout<<" toti "<< toti.size()<<endl;
for(int i=0;i<q;i++)
{
// cout<<" wait "<<endl;
char brrp[1000];
-
//cout<<" i "<<i<<endl;
// gets(c);
// cout<<"{ here "<<endl;
// scanf("%c",&cc);
vector<lli > :: iterator aces;
-
-
// cin>>brrp;
char cc;
-
char player ;
lli num=0;
cc=brrp[0];
player=brrp[sz-1];
-
// cin>>cc>>num>>player;
//scanf("%c,%lld%c",&cc,&num,&player);
for(int i=2;i<=sz-3;i++)
{
num=num*10+(brrp[i]-'0');
}
-
if(cc=='=')
{
// cout<<" f1 "<<endl;
-
display_ans(mymap[num],player);
-
}
-
-
-
else if(cc=='>')
{
-
aces=upper_bound(val.begin(),val.end(),num);
// cout<<" pointer to "<<*aces<<endl;
-
if(aces==val.begin())
{
// cout<<" f21 "<<endl;
lli timess=toti[n-1];
display_ans(timess,player);
}
else if(aces==val.end())
{
//cout<<" f22 "<<endl;
lli timess=0;
display_ans(0,player);
}
-
else
{
// cout<<" f23 "<<endl;
lli place=aces-val.begin();
place--;
// cout<<place<<endl;
lli timess=toti[n-1]-toti[place];
// cout<<" times "<<timess<<endl;;
display_ans(timess,player);
}
-
}
-
-
-
else
{
-
aces=lower_bound(val.begin(),val.end(),num);
if(aces==val.begin())
{
// cout<<" f31 "<<endl;
lli timess=0;
display_ans(timess,player);
-
}
else if(aces==val.end())
{
// cout<<" f32 "<<endl;
lli times= toti[n-1];
display_ans(times,player);
-
}
else
{
-
aces--;
lli place=aces-val.begin();
lli times=toti[place];
display_ans(times,player);
-
}
}
}
return 0;
}