Submission #49997

# Submission time Handle Problem Language Result Execution time Memory
49997 2018-06-06T05:02:12 Z shoemakerjo Construction of Highway (JOI18_construction) C++14
16 / 100
1000 ms 44068 KB
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define maxn 100010
#define LEFT 0
#define RIGHT 1
#define pii pair<int, int>
int N;

struct node {
	node *ch[2];
	node *par;
	node *pp; //path pointer

	int sz;
	int val; //store the current value, is passed with path pointer
	int key; //just for debugging purposes, store the index for the node

	node(int vv, int kk) : val(vv), key(kk) { //sets default values
		ch[LEFT] = NULL;
		ch[RIGHT] = NULL;
		pp = NULL;
		par = NULL;
		sz = 1;
	}

	bool isroot() {
		return !par || par->ch[LEFT] != this && par->ch[RIGHT] != this;
	}

	void update() { //really only does update stuff
		sz = 1;
		if (ch[LEFT]) {
			ch[LEFT]->par = this; //why not
			sz += ch[LEFT]->sz;
		}
		if (ch[RIGHT]) {
			ch[RIGHT]->par = this;
			sz += ch[RIGHT]->sz;
		}
	}

	int getDirection() {
		if (par == NULL) return -1;
		if (this == par->ch[LEFT]) return LEFT;
		if (this == par->ch[RIGHT]) return RIGHT;
		return -1;
	}

	void setChild(int dir, node * c) {
		ch[dir] = c;
		if (c) c->par = this;
	}

	void rotateup() {
		int dir = getDirection();
		node *p = par;
		if (!p->isroot()) {
			p->par->ch[p->getDirection()] = this;
		}
		par = p->par;
		node *pMid = ch[!dir];
		p->setChild(dir, pMid);
		setChild(!dir, p);
		pp = p->pp;
		p->pp = NULL;
		val = p->val; 
		p->update();
		update();
	}

};

node *nc[maxn]; //just store some nodes b/c why not
vector<pii> cstuff; //used for counting inversions

void splay(node * x) {
	//all updates for splay should be covered in rotateup()
	x->update();
	while (!x->isroot()) {
		x->update();
		node *p1 = x->par;
		if (!p1->isroot()) {
			node *g = p1->par;
			if (p1->getDirection() == x->getDirection()) {
				p1->rotateup(); //zig zig
			}
			else x->rotateup(); // zig zag

		}
		x->rotateup();
	}

}

void access(node *x) {
	x->update();
	splay(x);
	if (x->ch[RIGHT]) {
		x->ch[RIGHT]->val = x->val;
		x->ch[RIGHT]->pp = x; //cut it off
		x->ch[RIGHT]->par = NULL;
	}
	x->ch[RIGHT] = NULL;
	x->update();
//	cout << "my stuff:  " << x->sz << endl;
	cstuff.push_back(pii(x->val, x->sz));


	while (x->pp) { //not at root of tree of aux trees yet
		node *w = x->pp;
		splay(w);

		if (w->ch[RIGHT]) {
			w->ch[RIGHT]->val = w->val;
			w->ch[RIGHT]->pp = w;
			w->ch[RIGHT]->par = NULL;
		}
		w->ch[RIGHT] = NULL; //temporary for our purposes
		w->update();
		// cout << "key line:   " << w->key << " " << w->val << " " << w->sz << endl;
		cstuff.push_back(pii(w->val, w->sz));
		w->ch[RIGHT] = x;
		x->par = w;
		x->pp = NULL;
		splay(x);
		
	}
}

int seg[maxn*4];
int nums[maxn]; //the number stored for each guy
//set up segment tree stuff

void up(int spot, int diff, int ss, int se, int si) {
	if (spot < ss || spot > se) return;
	if (ss == se) {
		seg[si] += diff;
		return;
	}
	int mid = (ss+se)/2;
	up(spot, diff, ss, mid, si*2+1);
	up(spot, diff, mid+1, se, si*2+2);
	seg[si] = seg[si*2+1] + seg[si*2+2];

}

void update(int spot, int diff) {
	up(spot, diff, 0, N, 0);
}

int qu(int qs, int qe, int ss, int se, int si) {
	if (qs > qe || ss > se || qs > se || qe < ss) return 0;
	if (qs <= ss && se <= qe) {
		return seg[si];
	}
	int mid = (ss+se)/2;
	return qu(qs, qe, ss, mid, si*2+1) + 
		qu(qs, qe, mid+1, se, si*2+2);
}

int query(int qs, int qe) {
	return qu(qs, qe, 0, N, 0);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N;
	set<int> seen;
	map<int, int> encode; //for compression
	//process each edge in order
	for (int i = 1; i <= N; i++) {
		cin >> nums[i];
		seen.insert(nums[i]);
	}
	int cv = 0;
	for (int cur : seen) {
		encode[cur] = cv++;
	}
	for (int i = 1; i <= N; i++) {
		nums[i] = encode[nums[i]];
		nc[i] = new node(nums[i], i);
		//cout << "oval:  " << nc[i]->val << endl;
	}
	int a, b;
	for (int i = 0; i < N-1; i++) {
		cin >> a >> b; //read in edges in order
		cstuff.clear();
		ll ans = 0LL;
		access(nc[a]); //access this first guy
		//now the things in cstuff should be good to go

		//time to calculate the answer
		reverse(cstuff.begin(), cstuff.end());
		for (pii cur : cstuff) {
			int curval = cur.first;
			int amount = cur.second;
			// cout << "something on the list: " << curval << " in count of " << amount << endl;
			ans += amount*1LL*query(curval+1, N); //want amount greater than me
			update(cur.first, cur.second);
		}
		for (pii cur : cstuff) {
			update(cur.first, 0-cur.second);
		}

		nc[b]->pp = nc[a];
		access(nc[b]); //run a classic access now, 
		//				this should augment my value up real nice
		nc[b]->val = nums[b]; //set the thing straight now

		cout << ans << endl;
	}
	cin >> N;


}
//use ll b/c i think we might need it

Compilation message

construction.cpp: In member function 'bool node::isroot()':
construction.cpp:29:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   return !par || par->ch[LEFT] != this && par->ch[RIGHT] != this;
                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp: In function 'void splay(node*)':
construction.cpp:85:10: warning: unused variable 'g' [-Wunused-variable]
    node *g = p1->par;
          ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 3 ms 568 KB Output is correct
4 Correct 3 ms 652 KB Output is correct
5 Correct 5 ms 652 KB Output is correct
6 Correct 5 ms 652 KB Output is correct
7 Correct 5 ms 732 KB Output is correct
8 Correct 4 ms 732 KB Output is correct
9 Correct 4 ms 780 KB Output is correct
10 Correct 4 ms 792 KB Output is correct
11 Correct 4 ms 808 KB Output is correct
12 Correct 4 ms 808 KB Output is correct
13 Correct 4 ms 828 KB Output is correct
14 Correct 5 ms 952 KB Output is correct
15 Correct 5 ms 952 KB Output is correct
16 Correct 5 ms 952 KB Output is correct
17 Correct 4 ms 952 KB Output is correct
18 Correct 6 ms 952 KB Output is correct
19 Correct 5 ms 952 KB Output is correct
20 Correct 4 ms 952 KB Output is correct
21 Correct 4 ms 952 KB Output is correct
22 Correct 4 ms 952 KB Output is correct
23 Correct 4 ms 952 KB Output is correct
24 Correct 4 ms 952 KB Output is correct
25 Correct 5 ms 952 KB Output is correct
26 Correct 4 ms 952 KB Output is correct
27 Correct 4 ms 952 KB Output is correct
28 Correct 4 ms 952 KB Output is correct
29 Correct 4 ms 952 KB Output is correct
30 Correct 4 ms 952 KB Output is correct
31 Correct 6 ms 952 KB Output is correct
32 Correct 4 ms 952 KB Output is correct
33 Correct 5 ms 952 KB Output is correct
34 Correct 4 ms 952 KB Output is correct
35 Correct 4 ms 952 KB Output is correct
36 Correct 3 ms 952 KB Output is correct
37 Correct 3 ms 952 KB Output is correct
38 Correct 4 ms 952 KB Output is correct
39 Correct 4 ms 952 KB Output is correct
40 Correct 4 ms 952 KB Output is correct
41 Correct 4 ms 952 KB Output is correct
42 Correct 3 ms 952 KB Output is correct
43 Correct 4 ms 952 KB Output is correct
44 Correct 3 ms 952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 3 ms 568 KB Output is correct
4 Correct 3 ms 652 KB Output is correct
5 Correct 5 ms 652 KB Output is correct
6 Correct 5 ms 652 KB Output is correct
7 Correct 5 ms 732 KB Output is correct
8 Correct 4 ms 732 KB Output is correct
9 Correct 4 ms 780 KB Output is correct
10 Correct 4 ms 792 KB Output is correct
11 Correct 4 ms 808 KB Output is correct
12 Correct 4 ms 808 KB Output is correct
13 Correct 4 ms 828 KB Output is correct
14 Correct 5 ms 952 KB Output is correct
15 Correct 5 ms 952 KB Output is correct
16 Correct 5 ms 952 KB Output is correct
17 Correct 4 ms 952 KB Output is correct
18 Correct 6 ms 952 KB Output is correct
19 Correct 5 ms 952 KB Output is correct
20 Correct 4 ms 952 KB Output is correct
21 Correct 4 ms 952 KB Output is correct
22 Correct 4 ms 952 KB Output is correct
23 Correct 4 ms 952 KB Output is correct
24 Correct 4 ms 952 KB Output is correct
25 Correct 5 ms 952 KB Output is correct
26 Correct 4 ms 952 KB Output is correct
27 Correct 4 ms 952 KB Output is correct
28 Correct 4 ms 952 KB Output is correct
29 Correct 4 ms 952 KB Output is correct
30 Correct 4 ms 952 KB Output is correct
31 Correct 6 ms 952 KB Output is correct
32 Correct 4 ms 952 KB Output is correct
33 Correct 5 ms 952 KB Output is correct
34 Correct 4 ms 952 KB Output is correct
35 Correct 4 ms 952 KB Output is correct
36 Correct 3 ms 952 KB Output is correct
37 Correct 3 ms 952 KB Output is correct
38 Correct 4 ms 952 KB Output is correct
39 Correct 4 ms 952 KB Output is correct
40 Correct 4 ms 952 KB Output is correct
41 Correct 4 ms 952 KB Output is correct
42 Correct 3 ms 952 KB Output is correct
43 Correct 4 ms 952 KB Output is correct
44 Correct 3 ms 952 KB Output is correct
45 Correct 7 ms 952 KB Output is correct
46 Correct 22 ms 1400 KB Output is correct
47 Correct 23 ms 1400 KB Output is correct
48 Correct 24 ms 1416 KB Output is correct
49 Correct 14 ms 1424 KB Output is correct
50 Correct 14 ms 1448 KB Output is correct
51 Correct 21 ms 1448 KB Output is correct
52 Correct 17 ms 1484 KB Output is correct
53 Correct 17 ms 1576 KB Output is correct
54 Correct 16 ms 1576 KB Output is correct
55 Correct 15 ms 1576 KB Output is correct
56 Correct 18 ms 1576 KB Output is correct
57 Correct 29 ms 1576 KB Output is correct
58 Correct 34 ms 1576 KB Output is correct
59 Correct 34 ms 1576 KB Output is correct
60 Correct 32 ms 1576 KB Output is correct
61 Correct 16 ms 1576 KB Output is correct
62 Correct 18 ms 1576 KB Output is correct
63 Correct 16 ms 1576 KB Output is correct
64 Correct 19 ms 1576 KB Output is correct
65 Correct 19 ms 1576 KB Output is correct
66 Correct 21 ms 1576 KB Output is correct
67 Correct 23 ms 1576 KB Output is correct
68 Correct 12 ms 1576 KB Output is correct
69 Correct 14 ms 1576 KB Output is correct
70 Correct 12 ms 1576 KB Output is correct
71 Correct 12 ms 1576 KB Output is correct
72 Correct 36 ms 1592 KB Output is correct
73 Correct 24 ms 1592 KB Output is correct
74 Correct 13 ms 1592 KB Output is correct
75 Correct 15 ms 1592 KB Output is correct
76 Correct 14 ms 1592 KB Output is correct
77 Correct 15 ms 1592 KB Output is correct
78 Correct 12 ms 1592 KB Output is correct
79 Correct 12 ms 1592 KB Output is correct
80 Correct 13 ms 1592 KB Output is correct
81 Correct 19 ms 1592 KB Output is correct
82 Correct 27 ms 1592 KB Output is correct
83 Correct 17 ms 1592 KB Output is correct
84 Correct 16 ms 1592 KB Output is correct
85 Correct 16 ms 1592 KB Output is correct
86 Correct 16 ms 1592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 3 ms 568 KB Output is correct
4 Correct 3 ms 652 KB Output is correct
5 Correct 5 ms 652 KB Output is correct
6 Correct 5 ms 652 KB Output is correct
7 Correct 5 ms 732 KB Output is correct
8 Correct 4 ms 732 KB Output is correct
9 Correct 4 ms 780 KB Output is correct
10 Correct 4 ms 792 KB Output is correct
11 Correct 4 ms 808 KB Output is correct
12 Correct 4 ms 808 KB Output is correct
13 Correct 4 ms 828 KB Output is correct
14 Correct 5 ms 952 KB Output is correct
15 Correct 5 ms 952 KB Output is correct
16 Correct 5 ms 952 KB Output is correct
17 Correct 4 ms 952 KB Output is correct
18 Correct 6 ms 952 KB Output is correct
19 Correct 5 ms 952 KB Output is correct
20 Correct 4 ms 952 KB Output is correct
21 Correct 4 ms 952 KB Output is correct
22 Correct 4 ms 952 KB Output is correct
23 Correct 4 ms 952 KB Output is correct
24 Correct 4 ms 952 KB Output is correct
25 Correct 5 ms 952 KB Output is correct
26 Correct 4 ms 952 KB Output is correct
27 Correct 4 ms 952 KB Output is correct
28 Correct 4 ms 952 KB Output is correct
29 Correct 4 ms 952 KB Output is correct
30 Correct 4 ms 952 KB Output is correct
31 Correct 6 ms 952 KB Output is correct
32 Correct 4 ms 952 KB Output is correct
33 Correct 5 ms 952 KB Output is correct
34 Correct 4 ms 952 KB Output is correct
35 Correct 4 ms 952 KB Output is correct
36 Correct 3 ms 952 KB Output is correct
37 Correct 3 ms 952 KB Output is correct
38 Correct 4 ms 952 KB Output is correct
39 Correct 4 ms 952 KB Output is correct
40 Correct 4 ms 952 KB Output is correct
41 Correct 4 ms 952 KB Output is correct
42 Correct 3 ms 952 KB Output is correct
43 Correct 4 ms 952 KB Output is correct
44 Correct 3 ms 952 KB Output is correct
45 Correct 7 ms 952 KB Output is correct
46 Correct 22 ms 1400 KB Output is correct
47 Correct 23 ms 1400 KB Output is correct
48 Correct 24 ms 1416 KB Output is correct
49 Correct 14 ms 1424 KB Output is correct
50 Correct 14 ms 1448 KB Output is correct
51 Correct 21 ms 1448 KB Output is correct
52 Correct 17 ms 1484 KB Output is correct
53 Correct 17 ms 1576 KB Output is correct
54 Correct 16 ms 1576 KB Output is correct
55 Correct 15 ms 1576 KB Output is correct
56 Correct 18 ms 1576 KB Output is correct
57 Correct 29 ms 1576 KB Output is correct
58 Correct 34 ms 1576 KB Output is correct
59 Correct 34 ms 1576 KB Output is correct
60 Correct 32 ms 1576 KB Output is correct
61 Correct 16 ms 1576 KB Output is correct
62 Correct 18 ms 1576 KB Output is correct
63 Correct 16 ms 1576 KB Output is correct
64 Correct 19 ms 1576 KB Output is correct
65 Correct 19 ms 1576 KB Output is correct
66 Correct 21 ms 1576 KB Output is correct
67 Correct 23 ms 1576 KB Output is correct
68 Correct 12 ms 1576 KB Output is correct
69 Correct 14 ms 1576 KB Output is correct
70 Correct 12 ms 1576 KB Output is correct
71 Correct 12 ms 1576 KB Output is correct
72 Correct 36 ms 1592 KB Output is correct
73 Correct 24 ms 1592 KB Output is correct
74 Correct 13 ms 1592 KB Output is correct
75 Correct 15 ms 1592 KB Output is correct
76 Correct 14 ms 1592 KB Output is correct
77 Correct 15 ms 1592 KB Output is correct
78 Correct 12 ms 1592 KB Output is correct
79 Correct 12 ms 1592 KB Output is correct
80 Correct 13 ms 1592 KB Output is correct
81 Correct 19 ms 1592 KB Output is correct
82 Correct 27 ms 1592 KB Output is correct
83 Correct 17 ms 1592 KB Output is correct
84 Correct 16 ms 1592 KB Output is correct
85 Correct 16 ms 1592 KB Output is correct
86 Correct 16 ms 1592 KB Output is correct
87 Correct 65 ms 2616 KB Output is correct
88 Correct 215 ms 6968 KB Output is correct
89 Correct 967 ms 22016 KB Output is correct
90 Correct 893 ms 23944 KB Output is correct
91 Correct 917 ms 26060 KB Output is correct
92 Correct 512 ms 28036 KB Output is correct
93 Correct 442 ms 30308 KB Output is correct
94 Correct 460 ms 32176 KB Output is correct
95 Correct 491 ms 34720 KB Output is correct
96 Correct 506 ms 36656 KB Output is correct
97 Correct 583 ms 38916 KB Output is correct
98 Correct 568 ms 40920 KB Output is correct
99 Correct 495 ms 42500 KB Output is correct
100 Execution timed out 1070 ms 44068 KB Time limit exceeded
101 Halted 0 ms 0 KB -