Submission #1820482


Source Code Expand

#include<cstdio>
#include<algorithm>
using namespace std;
#define INF ((1<<30) - 1)
int n;
struct Tree {
	Tree *c1, *c2;
	int L, S, pv;
	Tree(int b, int e) {
		L = 0;
		c1 = c2 = NULL;
		pv = b - 1;
		S = -1 * (e - b + 1);
	}
	Tree GetMax(int b, int e, int s) {
		if (b == s)return *this;
		int m = (b + e) >> 1;
		if (!c1) {
			c1 = new Tree(b, m);
			c2 = new Tree(m + 1, e);
		}
		if (m < s) return c2->GetMax(m + 1, e, s);
		Tree ret = c1->GetMax(b, m, s);
		if (ret.L < ret.S + c2->L) {
			ret.L = ret.S + c2->L;
			ret.pv = c2->pv;
		}
		ret.S = ret.S + c2->S;
		return ret;
	}
	void Add(int b, int e, int x) {
		if (b == e) {
			S++;
			if (S > 0) {
				L = S;
				pv = b;
			}
			return;
		}
		int m = (b + e) >> 1;
		if (c1 == NULL) {
			c1 = new Tree(b, m);
			c2 = new Tree(m + 1, e);
		}
		if (x <= m)c1->Add(b, m, x);
		else c2->Add(m + 1, e, x);
		S = c1->S + c2->S;
		L = max(c1->L, c1->S + c2->L);
		pv = c1->pv;
		if (L != c1->L) pv = c2->pv;
	}
	int Sum(int b, int e, int x) {
		if (b == x)return S;
		int m = (b + e) >> 1, r = 0;
		if (!c1) {
			c1 = new Tree(b, m);
			c2 = new Tree(m+1, e);
		}
		if (x <= m) {
			r = c2->S + c1->Sum(b, m, x);
		}
		else {
			r = c2->Sum(m + 1, e, x);
		}
		return r;
	}
};
int main() {
	int i, a;
	scanf("%d", &n);
	long long res = 0, SS = 0;
	Tree *Root = new Tree(0, INF);
	for (i = 1; i <= n; i++) {
		scanf("%d", &a);
		Tree rr = Root->GetMax(0, INF, a+1);
		res += rr.pv - a;
		res += Root->Sum(0, INF, rr.pv + 1) + (INF - rr.pv);
		Root->Add(0, INF, rr.pv);
		SS += rr.pv - a;
	}
	printf("%lld\n", res);
}

Submission Info

Submission Time
Task B - Increment and Swap
User ainta
Language C++14 (GCC 5.4.1)
Score 1500
Code Size 1648 Byte
Status AC
Exec Time 534 ms
Memory 232704 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:70:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
./Main.cpp:74:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a);
                  ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1500 / 1500
Status
AC × 2
AC × 66
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 136 ms 7296 KB
001.txt AC 140 ms 8064 KB
002.txt AC 142 ms 8704 KB
003.txt AC 146 ms 9472 KB
004.txt AC 150 ms 10368 KB
005.txt AC 153 ms 11264 KB
006.txt AC 157 ms 12288 KB
007.txt AC 163 ms 13312 KB
008.txt AC 167 ms 14592 KB
009.txt AC 173 ms 15744 KB
010.txt AC 178 ms 17152 KB
011.txt AC 187 ms 18560 KB
012.txt AC 185 ms 20096 KB
013.txt AC 188 ms 21760 KB
014.txt AC 193 ms 23424 KB
015.txt AC 193 ms 25088 KB
016.txt AC 197 ms 26880 KB
017.txt AC 202 ms 28672 KB
018.txt AC 205 ms 30592 KB
019.txt AC 210 ms 32640 KB
020.txt AC 217 ms 34560 KB
021.txt AC 15 ms 1024 KB
022.txt AC 132 ms 7552 KB
023.txt AC 65 ms 4352 KB
024.txt AC 26 ms 2048 KB
025.txt AC 88 ms 6400 KB
026.txt AC 18 ms 1664 KB
027.txt AC 116 ms 9344 KB
028.txt AC 60 ms 5376 KB
029.txt AC 59 ms 5632 KB
030.txt AC 62 ms 6272 KB
031.txt AC 130 ms 12928 KB
032.txt AC 84 ms 9216 KB
033.txt AC 85 ms 9984 KB
034.txt AC 146 ms 17536 KB
035.txt AC 27 ms 3968 KB
036.txt AC 171 ms 22528 KB
037.txt AC 132 ms 18944 KB
038.txt AC 36 ms 6016 KB
039.txt AC 102 ms 16512 KB
040.txt AC 93 ms 15872 KB
041.txt AC 185 ms 30464 KB
042.txt AC 119 ms 18944 KB
043.txt AC 119 ms 18944 KB
044.txt AC 181 ms 18944 KB
045.txt AC 1 ms 256 KB
046.txt AC 90 ms 256 KB
047.txt AC 534 ms 232704 KB
048.txt AC 82 ms 256 KB
049.txt AC 84 ms 256 KB
050.txt AC 88 ms 256 KB
051.txt AC 88 ms 256 KB
052.txt AC 94 ms 384 KB
053.txt AC 109 ms 2176 KB
054.txt AC 178 ms 17152 KB
055.txt AC 81 ms 256 KB
056.txt AC 83 ms 256 KB
057.txt AC 85 ms 256 KB
058.txt AC 88 ms 256 KB
059.txt AC 92 ms 384 KB
060.txt AC 105 ms 2176 KB
061.txt AC 167 ms 17152 KB
062.txt AC 118 ms 17664 KB
063.txt AC 105 ms 15232 KB
example0.txt AC 1 ms 256 KB
example1.txt AC 1 ms 256 KB