Submission #1817105
Source Code Expand
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }
const int MAXN = 200000;
const int MAXS = 4 * MAXN;
int n;
int a[MAXN];
int b[MAXN], nb;
int sidx[MAXS], sval[MAXS], slazy[MAXS];
void sapply(int x, int val) {
sval[x] += val, slazy[x] += val;
}
void spush(int x) {
if (slazy[x] != 0) sapply(2 * x + 1, slazy[x]), sapply(2 * x + 2, slazy[x]), slazy[x] = 0;
}
void spull(int x) {
sidx[x] = sidx[2 * x + 1], sval[x] = sval[2 * x + 1];
if (sval[2 * x + 2] < sval[x]) sidx[x] = sidx[2 * x + 2], sval[x] = sval[2 * x + 2];
}
void sinit(int x, int l, int r) {
slazy[x] = 0;
if (l == r) {
sidx[x] = l; sval[x] = b[l];
} else {
int m = l + (r - l) / 2;
sinit(2 * x + 1, l, m); sinit(2 * x + 2, m + 1, r);
spull(x);
}
}
pair<int, int> sget(int x, int l, int r, int L, int R) {
if (L <= l&&r <= R) {
return MP(sidx[x], sval[x]);
} else {
spush(x);
int m = l + (r - l) / 2;
pair<int, int> ret = MP(-1, INT_MAX);
if (L <= m) { pair<int, int> cur = sget(2 * x + 1, l, m, L, R); if (cur.second < ret.second) ret = cur; }
if (m + 1 <= R) { pair<int, int> cur = sget(2 * x + 2, m + 1, r, L, R); if (cur.second < ret.second) ret = cur; }
return ret;
}
}
void smod(int x, int l, int r, int L, int R, int VAL) {
if (L <= l&&r <= R) {
sapply(x, VAL);
} else {
spush(x);
int m = l + (r - l) / 2;
if (L <= m) smod(2 * x + 1, l, m, L, R, VAL);
if (m + 1 <= R) smod(2 * x + 2, m + 1, r, L, R, VAL);
spull(x);
}
}
void run() {
scanf("%d", &n); REP(i, n) scanf("%d", &a[i]);
nb = 0; REP(i, n) b[nb++] = a[i]; sort(b, b + nb); nb = unique(b, b + nb) - b;
sinit(0, 0, nb - 1);
ll ret = 0;
REP(i, n) {
int idx = lower_bound(b, b + nb, a[i]) - b;
pair<int, int> val = sget(0, 0, nb - 1, idx, nb - 1);
ret += val.second - b[idx];
if (val.first != 0) smod(0, 0, nb - 1, 0, val.first - 1, 1);
}
printf("%lld\n", ret);
}
int main() {
run();
return 0;
}
Submission Info
Submission Time |
|
Task |
B - Increment and Swap |
User |
krijgertje |
Language |
C++14 (GCC 5.4.1) |
Score |
1500 |
Code Size |
2711 Byte |
Status |
AC |
Exec Time |
160 ms |
Memory |
11136 KB |
Compile Error
./Main.cpp: In function ‘void run()’:
./Main.cpp:87:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n); REP(i, n) scanf("%d", &a[i]);
^
./Main.cpp:87:47: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n); REP(i, n) scanf("%d", &a[i]);
^
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1500 / 1500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
example0.txt, example1.txt |
All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, 063.txt, example0.txt, example1.txt |
Case Name |
Status |
Exec Time |
Memory |
000.txt |
AC |
144 ms |
10112 KB |
001.txt |
AC |
142 ms |
10112 KB |
002.txt |
AC |
146 ms |
10112 KB |
003.txt |
AC |
147 ms |
10112 KB |
004.txt |
AC |
148 ms |
10112 KB |
005.txt |
AC |
147 ms |
10112 KB |
006.txt |
AC |
147 ms |
10112 KB |
007.txt |
AC |
151 ms |
10112 KB |
008.txt |
AC |
149 ms |
10112 KB |
009.txt |
AC |
151 ms |
10112 KB |
010.txt |
AC |
151 ms |
10112 KB |
011.txt |
AC |
152 ms |
11136 KB |
012.txt |
AC |
152 ms |
11136 KB |
013.txt |
AC |
153 ms |
11136 KB |
014.txt |
AC |
153 ms |
11136 KB |
015.txt |
AC |
153 ms |
11136 KB |
016.txt |
AC |
153 ms |
11136 KB |
017.txt |
AC |
154 ms |
11136 KB |
018.txt |
AC |
154 ms |
11136 KB |
019.txt |
AC |
154 ms |
11136 KB |
020.txt |
AC |
154 ms |
11136 KB |
021.txt |
AC |
15 ms |
6528 KB |
022.txt |
AC |
137 ms |
10112 KB |
023.txt |
AC |
63 ms |
7168 KB |
024.txt |
AC |
25 ms |
6784 KB |
025.txt |
AC |
85 ms |
7296 KB |
026.txt |
AC |
17 ms |
6528 KB |
027.txt |
AC |
109 ms |
9984 KB |
028.txt |
AC |
54 ms |
7168 KB |
029.txt |
AC |
52 ms |
7168 KB |
030.txt |
AC |
53 ms |
7168 KB |
031.txt |
AC |
111 ms |
9984 KB |
032.txt |
AC |
69 ms |
7168 KB |
033.txt |
AC |
70 ms |
9728 KB |
034.txt |
AC |
120 ms |
9984 KB |
035.txt |
AC |
22 ms |
6656 KB |
036.txt |
AC |
135 ms |
11136 KB |
037.txt |
AC |
104 ms |
9984 KB |
038.txt |
AC |
28 ms |
6784 KB |
039.txt |
AC |
79 ms |
9984 KB |
040.txt |
AC |
70 ms |
9728 KB |
041.txt |
AC |
135 ms |
11136 KB |
042.txt |
AC |
105 ms |
11136 KB |
043.txt |
AC |
105 ms |
11136 KB |
044.txt |
AC |
157 ms |
11136 KB |
045.txt |
AC |
2 ms |
6400 KB |
046.txt |
AC |
37 ms |
7040 KB |
047.txt |
AC |
160 ms |
11136 KB |
048.txt |
AC |
29 ms |
7040 KB |
049.txt |
AC |
34 ms |
7040 KB |
050.txt |
AC |
47 ms |
7040 KB |
051.txt |
AC |
70 ms |
7040 KB |
052.txt |
AC |
96 ms |
7168 KB |
053.txt |
AC |
130 ms |
7296 KB |
054.txt |
AC |
153 ms |
10112 KB |
055.txt |
AC |
26 ms |
7040 KB |
056.txt |
AC |
32 ms |
7040 KB |
057.txt |
AC |
45 ms |
7040 KB |
058.txt |
AC |
62 ms |
7040 KB |
059.txt |
AC |
89 ms |
7168 KB |
060.txt |
AC |
119 ms |
7424 KB |
061.txt |
AC |
147 ms |
10112 KB |
062.txt |
AC |
100 ms |
10112 KB |
063.txt |
AC |
92 ms |
10112 KB |
example0.txt |
AC |
2 ms |
6400 KB |
example1.txt |
AC |
2 ms |
6400 KB |