Skip to content

Commit f03be6b

Browse files
upd
1 parent 6689b86 commit f03be6b

File tree

1 file changed

+12
-47
lines changed

1 file changed

+12
-47
lines changed

DMOJ/Soriyas Programming Project.cpp

+12-47
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ int main() {
230230

231231
/* END OP BENQ CODE */
232232

233+
234+
235+
236+
/* the following code TLE's :( */
233237
#include <bits/stdc++.h>
234238
using namespace std;
235239

@@ -322,57 +326,15 @@ template<class T, int SZ> struct OffBIT2D {
322326
-query(xr,yl-1)+query(xl-1,yl-1); }
323327
};
324328

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-
365329
int n;
366330
OffBIT2D<int, 500000> bit;
367-
int A[500000];
368-
int P[500000];
369331

370332
int main() {
371333
cin.tie(nullptr); ios::sync_with_stdio(false);
372334

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];
376338

377339
for (int i = 0; i < n; i++) {
378340
bit.upd(A[P[i]-1], P[i], 0);
@@ -381,8 +343,11 @@ int main() {
381343

382344
ll s = 0;
383345
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);
385350
cout << s << "\n";
386-
bit.upd(A[P[i]-1], P[i], 1);
351+
bit.upd(v, p, 1);
387352
}
388353
}

0 commit comments

Comments
 (0)