-
Notifications
You must be signed in to change notification settings - Fork 0
/
SUMQ
323 lines (283 loc) · 7.69 KB
/
SUMQ
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import static java.lang.System.*;
/*
https://www.codechef.com/JUNE17
problem difficulty = medium
accuracy = 7.34%
status = AC
*/
/*
tc 1
1
5 6 7
71 18 11 12 6
20 31 10 8 2 16
5 17 22 3 42 16 60
o/p : 58973
tc 2
1
4 3 4
9 4 2 1
5 6 10
3 4 8 7
o/p : 4321
corner tc 3
1
2 3 4
1 1
1 1 2
1 1 1 1
sumq : 136
*/
class SUMQ{
private static final BigInteger mod = new BigInteger("1000000007");
public static void main(String[] args) {
fastIO fs = new fastIO();
fs.InputReader(in);
fs.pw = new PrintWriter(new OutputStreamWriter(out));
int T = fs.nextInt();
for (int i = 0; i <T; i++) {
int p = fs.nextInt(), q = fs.nextInt(), r = fs.nextInt();
long [] A = new long[p];
long [] aSums = new long[p];
HashMap<Long,Integer> mapA = new HashMap<>();
long [] B = new long[q];
long [] C = new long[r];
long [] cSums = new long[r];
HashMap<Long,Integer> mapC = new HashMap<>();
for (int j = 0; j <p; j++) {
A[j] = fs.nextLong();
}
for (int j = 0; j <q; j++) {
B[j] = fs.nextLong();
}
for (int j = 0; j <r; j++) {
C[j] = fs.nextLong();
}
Arrays.sort(A);
Arrays.sort(B);
Arrays.sort(C);
// sum of elements of A and C from 0 till element's position
long aSum = 0, cSum = 0;
for (int j = 0; j <p; j++) {
aSum += A[j];
aSums[j] = aSum;
mapA.put(A[j],j);
}
for (int j = 0; j <r; j++) {
cSum += C[j];
cSums[j] = cSum;
mapC.put(C[j],j);
}
//postions of B elements in A and C
int [] posYA = positionOfBelementsInArray(A,B,mapA);
int [] posYC = positionOfBelementsInArray(C,B,mapC);
BigInteger ans = BigInteger.ZERO;
for (int j = 0; j <q; j++) {
if (posYA[j]==A.length)
posYA[j] = posYA[j]-1;
if (posYC[j]==C.length)
posYC[j] = posYC[j]-1;
if (posYA[j]<0)
continue;
if (posYC[j]<0)
continue;
BigInteger sumOfXY = BigInteger.valueOf((B[j]*(posYA[j]+1)+aSums[posYA[j]]));
BigInteger sumOfYZ = BigInteger.valueOf((B[j]*(posYC[j]+1)+cSums[posYC[j]]));
ans = ans.add(sumOfXY.multiply(sumOfYZ)).mod(mod);
}
fs.pw.println(ans.mod(mod));
}
fs.pw.flush();
fs.pw.close();
}
private static int[] positionOfBelementsInArray(long[] posInto, long[] b, HashMap<Long, Integer> map) {
int [] posArray = new int[b.length];
for (int j = 0; j <b.length; j++) {
int pos = Arrays.binarySearch(posInto,b[j]);
if (pos<0) {
pos = (pos + 1) * (-1);
}
if (map.containsKey(b[j]))
posArray[j] = map.get(b[j]);
else
posArray[j] = pos-1;
}
return posArray;
}
}
class fastIO
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
PrintWriter pw;
int MOD = 1000000007;
void InputReader(InputStream stream1) {
stream = stream1;
}
private boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
String nextToken() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
int[][] next2dArray(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
char[][] nextCharArray(int n,int m){
char [][]c=new char[n][m];
for(int i=0;i<n;i++){
String s=nextLine();
for(int j=0;j<s.length();j++){
c[i][j]=s.charAt(j);
}
}
return c;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
String[] nextStringArray(int n) {
String ans [] = new String[n];
for (int i = 0; i <n; i++) {
ans [i] = nextToken();
}
return ans;
}
String[] nextStringLineArray(int n) {
String ans [] = new String[n];
for (int i = 0; i <n; i++) {
ans [i] = nextLine();
}
return ans;
}
void pArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
pw.print(arr[i] + " ");
}
pw.println();
return;
}
void pArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
pw.print(arr[i] + " ");
}
pw.println();
return;
}
void pArray(boolean[] arr) {
for (int i = 0; i < arr.length; i++) {
pw.print(arr[i] + " ");
}
pw.println();
return;
}
private boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
private interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}