-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathVasyaandRhezo.cpp
171 lines (133 loc) · 3.6 KB
/
VasyaandRhezo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
ueen Vasya is preparing for a war against Rhezo. She has N warriors in total arranged in a line. Let us number the warriors by numbers from 1 to N. She will fight Rhezo's army for Q days, and each day she can choose only one warrior.
For each warrior, we know 2 values associated with him, let us call these A and B. Each day Vasya can choose her warrior from a range Li to Ri, she must choose the warrior with maximum A value. If there is more than 1 warrior having the same maximum A value, she chooses the warrior with minimum B value. If still there is more than 1 warrior with same maximum A value and same minimum B value, she chooses the one with lower index in line.
You being the hand of Queen Vasya, need to help her in choosing the warrior for each day.
Input:
First line contains a single integer N, denoting the number of warriors Queen Vasya has.
Second line contains N space separated integers Ai. Third line contains N space separated integers Bi.
Next line contains a single integer Q, denoting the number of days Queen Vasya chooses a warrior.
Each of the next Q lines contains 2 integers Li and Ri.
Output:
For each Li and Ri, print the index of the warrior that Queen Vasya should choose.
Constraints:
1≤ N,Q ≤10^6
1≤ Ai,Bi ≤10^9
1≤Li≤Ri
Sample Input
5
1 8 4 6 8
4 8 6 3 7
4
1 4
2 4
3 4
1 5
Sample Output
2
2
4
5
*/
#include <bits/stdc++.h>
using namespace std;
int query(int* arr1, int* arr2, int*tree, int start, int end, int treeNode, int left, int right){
//Completely out
if (left>end || right<start)
{
return left;
}
//Completely inside
if (start>=left && end<=right)
{
return tree[treeNode];
}
//Partially inside
int mid = (start+end)/2;
int leftindex = query(arr1, arr2, tree, start, mid, 2*treeNode+1, left, right);
int rightindex = query(arr1, arr2, tree, mid+1, end, 2*treeNode+2, left, right);
if (arr1[leftindex]>arr1[rightindex])
{
return leftindex;
}else if(arr1[leftindex]<arr1[rightindex]){
return rightindex;
}else{
if (arr2[leftindex]>arr2[rightindex])
{
return rightindex;
}else if(arr2[leftindex]<arr2[rightindex]){
return leftindex;
}else{
return leftindex;
}
}
return INT_MIN;
}
//tree will store even count
void create(int* arr1, int* arr2, int* tree, int start, int end, int treeNode){
if (end == start)
{
tree[treeNode] = start;
return;
}
int mid = (start+end)/2;
create(arr1, arr2, tree, start, mid, 2*treeNode+1);
create(arr1, arr2, tree, mid+1, end, 2*treeNode+2);
int leftindex = tree[2*treeNode+1];
int rightindex = tree[2*treeNode+2];
if (arr1[leftindex]>arr1[rightindex])
{
tree[treeNode] = leftindex;
}else if(arr1[leftindex]<arr1[rightindex]){
tree[treeNode] = rightindex;
}else{
if (arr2[leftindex]>arr2[rightindex])
{
tree[treeNode] = rightindex;
}else if(arr2[leftindex]<arr2[rightindex]){
tree[treeNode] = leftindex;
}else{
tree[treeNode] = leftindex;
}
}
//tree[treeNode] = left+right;
return;
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
int n, q;
cin>>n;
int* arr1 = new int[n];
for (int i = 0; i < n; ++i)
{
cin>>arr1[i];
}
int* arr2 = new int[n];
for (int i = 0; i < n; ++i)
{
cin>>arr2[i];
}
cin>>q;
int* tree = new int[4*n];
create(arr1, arr2, tree, 0, n-1, 0);
// for (int i = 0; i < 4*n; ++i)
// {
// cout << "I- "<<i<<" "<<tree[i] << '\n';
// }
//return 0;
for (int i = 0; i < q; ++i)
{
int a, b;
cin>>a>>b;
cout << query(arr1, arr2, tree, 0, n-1, 0, a-1, b-1)+1 << '\n';
}
delete[] tree;
delete[] arr2;
delete[] arr1;
return 0 ;
}