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, where ith 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 :

- [1]

- [2]

- [3]

- [2]

- [3]

- [3]

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.

Difficulty: HARD
Tags: Sorting

Data structures

Implementation

Complete search
Skill types: Sorting

Data structures

Complete search

Starter code:

Solutions:
def left_span(arr, n):
	ans = [0]
	span = [0]
	for i in range(1, n):
		while span and arr[i] > arr[span[-1]]:
			span.pop()
		if not span:
			ans.append(0)
		else:
			ans.append(span[-1] + 1)
		span.append(i)
	return ans

def right_span(arr, n):
	ans = [n + 1]
	span = [n - 1]
	for i in range(n - 2, -1, -1):
		while span and arr[i] >= arr[span[-1]]:
			span.pop()
		if not span:
			ans.append(n + 1)
		else:
			ans.append(span[-1] + 1)
		span.append(i)
	return ans[::-1]
from collections import Counter
from bisect import bisect_left, bisect_right
from operator import itemgetter
(n, q) = list(map(int, input().split()))
arr = list(map(int, input().split()))
left = left_span(arr, n)
right = right_span(arr, n)
c = Counter()
for i in range(n):
	c[arr[i]] += (right[i] - (i + 1)) * (i + 1 - left[i])
a = sorted(c)
f = []
for v in a:
	f.append(c[v])
prefix_sum = [f[0]]
n = len(f)
for i in range(1, n):
	prefix_sum.append(f[i] + prefix_sum[-1])
r = [0] * q
for i in range(q):
	(sign, k, player) = list(map(str, input().split()))
	k = int(k)
	if sign == '=':
		if k in c:
			res = c[k]
		else:
			res = 0
	elif sign == '>':
		j = bisect_left(a, k)
		if j == n:
			res = 0
		elif a[j] == k:
			res = prefix_sum[-1] - prefix_sum[j]
		elif j > 0:
			res = prefix_sum[-1] - prefix_sum[j - 1]
		else:
			res = prefix_sum[-1]
	else:
		j = bisect_left(a, k)
		if j == 0:
			res = 0
		else:
			res = prefix_sum[j - 1]
	if res % 2 == 0:
		if player == 'D':
			r[i] = 'C'
		else:
			r[i] = 'D'
	else:
		r[i] = player
print(''.join(r))