@@ -230,6 +230,10 @@ int main() {
230
230
231
231
/* END OP BENQ CODE */
232
232
233
+
234
+
235
+
236
+ /* the following code TLE's :( */
233
237
#include < bits/stdc++.h>
234
238
using namespace std ;
235
239
@@ -322,57 +326,15 @@ template<class T, int SZ> struct OffBIT2D {
322
326
-query (xr,yl-1 )+query (xl-1 ,yl-1 ); }
323
327
};
324
328
325
- namespace FastIO {
326
- const int BSZ = 1 <<15 ; // //// INPUT
327
- char ibuf[BSZ]; int ipos, ilen;
328
- char nc () { // next char
329
- if (ipos == ilen) {
330
- ipos = 0 ; ilen = fread (ibuf,1 ,BSZ,stdin);
331
- if (!ilen) return EOF;
332
- }
333
- return ibuf[ipos++];
334
- }
335
- void rs (str& x) { // read str
336
- char ch; while (isspace (ch = nc ()));
337
- do { x += ch; } while (!isspace (ch = nc ()) && ch != EOF);
338
- }
339
- template <class T > void ri (T& x) { // read int or ll
340
- char ch; int sgn = 1 ;
341
- while (!isdigit (ch = nc ())) if (ch == ' -' ) sgn *= -1 ;
342
- x = ch-' 0' ; while (isdigit (ch = nc ())) x = x*10 +(ch-' 0' );
343
- x *= sgn;
344
- }
345
- template <class T , class ... Ts> void ri (T& t, Ts&... ts) {
346
- ri (t); ri (ts...); } // read ints
347
- // //// OUTPUT (call initO() at start)
348
- char obuf[BSZ], numBuf[100 ]; int opos;
349
- void flushOut () { fwrite (obuf,1 ,opos,stdout); opos = 0 ; }
350
- void wc (char c) { // write char
351
- if (opos == BSZ) flushOut ();
352
- obuf[opos++] = c; }
353
- void ws (str s) { trav (c,s) wc (c); } // write str
354
- template <class T > void wi (T x, char after = ' \0 ' ) { // / write int
355
- if (x < 0 ) wc (' -' ), x *= -1 ;
356
- int len = 0 ; for (;x>=10 ;x/=10 ) numBuf[len++] = ' 0' +(x%10 );
357
- wc (' 0' +x); R0F (i,len) wc (numBuf[i]);
358
- if (after) wc (after);
359
- }
360
- void initO () { assert (atexit (flushOut) == 0 ); } // / auto-flush output
361
- }
362
-
363
- using namespace FastIO ;
364
-
365
329
int n;
366
330
OffBIT2D<int , 500000 > bit;
367
- int A[500000 ];
368
- int P[500000 ];
369
331
370
332
int main () {
371
333
cin.tie (nullptr ); ios::sync_with_stdio (false );
372
334
373
- ri (n) ;
374
- for (int i = 0 ; i < n; i++) ri ( A[i]) ;
375
- for (int i = 0 ; i < n; i++) ri ( P[i]) ;
335
+ cin >> n ;
336
+ int A[n]; for (int i = 0 ; i < n; i++) cin >> A[i];
337
+ int P[n]; for (int i = 0 ; i < n; i++) cin >> P[i];
376
338
377
339
for (int i = 0 ; i < n; i++) {
378
340
bit.upd (A[P[i]-1 ], P[i], 0 );
@@ -381,8 +343,11 @@ int main() {
381
343
382
344
ll s = 0 ;
383
345
for (int i = 0 ; i < n; i++) {
384
- s += bit.query (A[P[i]-1 ]+1 , n, 1 , P[i]) + bit.query (1 , A[P[i]-1 ]-1 , P[i], n);
346
+ int p = P[i]-1 ;
347
+ int v = A[p];
348
+ p++;
349
+ s += bit.query (v+1 , n, 1 , p) + bit.query (1 , v-1 , p, n);
385
350
cout << s << " \n " ;
386
- bit.upd (A[P[i]- 1 ], P[i] , 1 );
351
+ bit.upd (v, p , 1 );
387
352
}
388
353
}
0 commit comments