Submission #2746066
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
int n, a[200020], b[200020], bc;
int mn[800020];
int ad[800020];
void build(int x, int l, int r) {
if (l + 1 == r) {
mn[x] = b[l];
return;
}
int m = (l + r) / 2;
build(x * 2 + 1, l, m);
build(x * 2 + 2, m, r);
mn[x] = min(mn[x * 2 + 1], mn[x * 2 + 2]);
return;
}
void push(int x) {
mn[x * 2 + 1] += ad[x];
mn[x * 2 + 2] += ad[x];
ad[x * 2 + 1] += ad[x];
ad[x * 2 + 2] += ad[x];
ad[x] = 0;
}
void change(int x, int l, int r, int L, int R) {
if (L <= l && r <= R) {
mn[x]++;
ad[x]++;
return;
}
if (r <= L || R <= l) {
return;
}
push(x);
int m = (l + r) / 2;
change(x * 2 + 1, l, m, L, R);
change(x * 2 + 2, m, r, L, R);
mn[x] = min(mn[x * 2 + 1], mn[x * 2 + 2]);
}
int query(int x, int l, int r, int L, int R) {
if (L <= l && r <= R) {
return mn[x];
}
if (r <= L || R <= l) {
return 2e9;
}
push(x);
int m = (l + r) / 2;
return min(query(x * 2 + 1, l, m, L, R), query(x * 2 + 2, m, r, L, R));
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
bc = unique(b, b + n) - b;
build(0, 0, bc);
long long z = 0;
for (int i = 0; i < n; i++) {
int p = lower_bound(b, b + bc, a[i]) - b;
z += query(0, 0, bc, p, bc) - a[i];
change(0, 0, bc, 0, p);
}
printf("%lld\n", z);
return 0;
}
Submission Info
Submission Time |
|
Task |
B - Increment and Swap |
User |
Factorio |
Language |
C++14 (GCC 5.4.1) |
Score |
1500 |
Code Size |
1407 Byte |
Status |
AC |
Exec Time |
156 ms |
Memory |
7936 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:51:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
./Main.cpp:53:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
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 |
135 ms |
6912 KB |
001.txt |
AC |
137 ms |
6912 KB |
002.txt |
AC |
143 ms |
6912 KB |
003.txt |
AC |
139 ms |
6912 KB |
004.txt |
AC |
140 ms |
6912 KB |
005.txt |
AC |
141 ms |
6912 KB |
006.txt |
AC |
142 ms |
6912 KB |
007.txt |
AC |
144 ms |
6912 KB |
008.txt |
AC |
144 ms |
6912 KB |
009.txt |
AC |
152 ms |
6912 KB |
010.txt |
AC |
145 ms |
6912 KB |
011.txt |
AC |
146 ms |
7936 KB |
012.txt |
AC |
146 ms |
7936 KB |
013.txt |
AC |
147 ms |
7936 KB |
014.txt |
AC |
148 ms |
7936 KB |
015.txt |
AC |
150 ms |
7936 KB |
016.txt |
AC |
149 ms |
7936 KB |
017.txt |
AC |
152 ms |
7936 KB |
018.txt |
AC |
153 ms |
7936 KB |
019.txt |
AC |
150 ms |
7936 KB |
020.txt |
AC |
151 ms |
7936 KB |
021.txt |
AC |
14 ms |
2560 KB |
022.txt |
AC |
129 ms |
6912 KB |
023.txt |
AC |
61 ms |
3584 KB |
024.txt |
AC |
24 ms |
2816 KB |
025.txt |
AC |
81 ms |
3712 KB |
026.txt |
AC |
16 ms |
2688 KB |
027.txt |
AC |
104 ms |
6528 KB |
028.txt |
AC |
52 ms |
3456 KB |
029.txt |
AC |
50 ms |
3456 KB |
030.txt |
AC |
51 ms |
3456 KB |
031.txt |
AC |
106 ms |
6528 KB |
032.txt |
AC |
66 ms |
3584 KB |
033.txt |
AC |
67 ms |
6144 KB |
034.txt |
AC |
116 ms |
6656 KB |
035.txt |
AC |
20 ms |
2816 KB |
036.txt |
AC |
131 ms |
7808 KB |
037.txt |
AC |
101 ms |
6528 KB |
038.txt |
AC |
26 ms |
2944 KB |
039.txt |
AC |
76 ms |
6272 KB |
040.txt |
AC |
68 ms |
6144 KB |
041.txt |
AC |
132 ms |
7808 KB |
042.txt |
AC |
106 ms |
7936 KB |
043.txt |
AC |
105 ms |
7936 KB |
044.txt |
AC |
156 ms |
7936 KB |
045.txt |
AC |
2 ms |
2304 KB |
046.txt |
AC |
35 ms |
3840 KB |
047.txt |
AC |
156 ms |
7936 KB |
048.txt |
AC |
29 ms |
3840 KB |
049.txt |
AC |
33 ms |
3840 KB |
050.txt |
AC |
45 ms |
3840 KB |
051.txt |
AC |
67 ms |
3840 KB |
052.txt |
AC |
93 ms |
3840 KB |
053.txt |
AC |
118 ms |
4096 KB |
054.txt |
AC |
147 ms |
6912 KB |
055.txt |
AC |
26 ms |
3840 KB |
056.txt |
AC |
31 ms |
3840 KB |
057.txt |
AC |
43 ms |
3840 KB |
058.txt |
AC |
63 ms |
3840 KB |
059.txt |
AC |
90 ms |
3840 KB |
060.txt |
AC |
115 ms |
4096 KB |
061.txt |
AC |
141 ms |
6912 KB |
062.txt |
AC |
99 ms |
6912 KB |
063.txt |
AC |
98 ms |
7040 KB |
example0.txt |
AC |
2 ms |
2304 KB |
example1.txt |
AC |
2 ms |
2304 KB |