Submission #144713

# Submission time Handle Problem Language Result Execution time Memory
144713 2019-08-17T14:42:30 Z nvmdava Rectangles (IOI19_rect) C++17
72 / 100
4380 ms 1048576 KB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define N 2501

map<int, int> cnt[2][N][N];
int arr[N][N], bit[N][N], temp[N], p[N], lc[N], rc[N], n, m;

void buildtree(int sz){
	vector<int> v = {0};
	for(int i = 1; i <= sz; i++){
		vector<int> t;
		while(temp[v.back()] < temp[i]){
			t.push_back(v.back());
			v.pop_back();
		}
		v.push_back(i);
		for(int j = t.size() - 1; j > 0; j--){
			p[t[j - 1]] = t[j];
		}
		if(!t.empty())p[t.back()] = i;
	}
	for(int i = v.size() - 1; i > 0; i--)
		p[v[i]] = v[i - 1];
	for(int i = 1; i <= sz; i++){
		if(p[i] > i){
			lc[p[i]] = i;
		} else {
			rc[p[i]] = i;
		}
	}

}

void travtree(int i1, int i2, int v, int l, int r){
	if(l != 0 && r != (i1 == 1 ? n + 1 : m + 1) && r - l > 1 && temp[l] != temp[v] && temp[r] != temp[v]){
		if(i1 == 0)
			cnt[i1][i2][r - 1][r - l - 1]++;
		else 
			cnt[i1][r - 1][i2][r - l - 1]++;
	}
	if(lc[v] != 0)
		travtree(i1, i2, lc[v], l, v);
	if(rc[v] != 0)
		travtree(i1, i2, rc[v], v, r);
}

void build(){
	for(int i = 1; i <= n; i++){
		memset(lc, 0, sizeof lc);
		memset(rc, 0, sizeof rc);
		
		for(int j = 0; j <= m; j++)
			temp[j] = arr[i][j];
		buildtree(m);
		travtree(0, i, 0, 0, m + 1);
	}
	for(int j = 1; j <= m; j++){
		memset(lc, 0, sizeof lc);
		memset(rc, 0, sizeof rc);

		for(int i = 0; i <= n; i++)
			temp[i] = arr[i][j];
		
		buildtree(n);
		travtree(1, j, 0, 0, n + 1);
	}
}

void update(int x, int y, int v){
	for(int i = x; i < N; i += i & -i){
		for(int j = y; j < N; j += j & -j){
			arr[i][j] += v;
		}
	}
}

int query(int x, int y){
	int res = 0;
	for(int i = x; i > 0; i -= i & -i){
		for(int j = y; j > 0; j -= j & -j){
			res += arr[i][j];
		}
	}
	return res;
}

long long count_rectangles(vector<vector<int> > a){
	n = a.size();
	m = a[0].size();
	
	memset(arr, 0x3f, sizeof arr);

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			arr[i + 1][j + 1] = a[i][j];
	a.clear();
	build();
	int res = 0;
	memset(arr, 0, sizeof arr);
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			for(auto& x : cnt[0][i][j]){
				auto it = cnt[0][i - 1][j].find(x.ff);
				if(it != cnt[0][i - 1][j].end())
					x.ss = 1 + it -> ss;
				update(x.ff, x.ss, 1);
			}
			for(auto& x : cnt[1][i][j]){
				auto it = cnt[1][i][j - 1].find(x.ff);
				if(it != cnt[1][i][j - 1].end())
					x.ss = 1 + it -> ss;
				res += query(x.ss, N - 1);
				res -= query(x.ss, x.ff - 1);
			}
			for(auto& x : cnt[0][i][j])
				update(x.ff, x.ss, -1);
			cnt[0][i - 1][j].clear();
			cnt[1][i - 1][j].clear();
		}
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 560 ms 612492 KB Output is correct
2 Correct 562 ms 612480 KB Output is correct
3 Correct 571 ms 612472 KB Output is correct
4 Correct 594 ms 612488 KB Output is correct
5 Correct 559 ms 612428 KB Output is correct
6 Correct 563 ms 612504 KB Output is correct
7 Correct 561 ms 612444 KB Output is correct
8 Correct 578 ms 612572 KB Output is correct
9 Correct 566 ms 612500 KB Output is correct
10 Correct 561 ms 612416 KB Output is correct
11 Correct 561 ms 612572 KB Output is correct
12 Correct 565 ms 612580 KB Output is correct
13 Correct 625 ms 612416 KB Output is correct
14 Correct 569 ms 612600 KB Output is correct
15 Correct 561 ms 612324 KB Output is correct
16 Correct 615 ms 612464 KB Output is correct
17 Correct 567 ms 612528 KB Output is correct
18 Correct 557 ms 612448 KB Output is correct
19 Correct 558 ms 612656 KB Output is correct
20 Correct 572 ms 612472 KB Output is correct
21 Correct 564 ms 612344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 612492 KB Output is correct
2 Correct 562 ms 612480 KB Output is correct
3 Correct 571 ms 612472 KB Output is correct
4 Correct 594 ms 612488 KB Output is correct
5 Correct 559 ms 612428 KB Output is correct
6 Correct 563 ms 612504 KB Output is correct
7 Correct 561 ms 612444 KB Output is correct
8 Correct 578 ms 612572 KB Output is correct
9 Correct 566 ms 612500 KB Output is correct
10 Correct 561 ms 612416 KB Output is correct
11 Correct 561 ms 612572 KB Output is correct
12 Correct 565 ms 612580 KB Output is correct
13 Correct 625 ms 612416 KB Output is correct
14 Correct 569 ms 612600 KB Output is correct
15 Correct 561 ms 612324 KB Output is correct
16 Correct 615 ms 612464 KB Output is correct
17 Correct 573 ms 612996 KB Output is correct
18 Correct 565 ms 613240 KB Output is correct
19 Correct 602 ms 613080 KB Output is correct
20 Correct 572 ms 612784 KB Output is correct
21 Correct 569 ms 613216 KB Output is correct
22 Correct 570 ms 613040 KB Output is correct
23 Correct 568 ms 612984 KB Output is correct
24 Correct 564 ms 612704 KB Output is correct
25 Correct 567 ms 612528 KB Output is correct
26 Correct 557 ms 612448 KB Output is correct
27 Correct 558 ms 612656 KB Output is correct
28 Correct 572 ms 612472 KB Output is correct
29 Correct 564 ms 612344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 612492 KB Output is correct
2 Correct 562 ms 612480 KB Output is correct
3 Correct 571 ms 612472 KB Output is correct
4 Correct 594 ms 612488 KB Output is correct
5 Correct 559 ms 612428 KB Output is correct
6 Correct 563 ms 612504 KB Output is correct
7 Correct 561 ms 612444 KB Output is correct
8 Correct 578 ms 612572 KB Output is correct
9 Correct 566 ms 612500 KB Output is correct
10 Correct 561 ms 612416 KB Output is correct
11 Correct 561 ms 612572 KB Output is correct
12 Correct 565 ms 612580 KB Output is correct
13 Correct 625 ms 612416 KB Output is correct
14 Correct 569 ms 612600 KB Output is correct
15 Correct 561 ms 612324 KB Output is correct
16 Correct 615 ms 612464 KB Output is correct
17 Correct 573 ms 612996 KB Output is correct
18 Correct 565 ms 613240 KB Output is correct
19 Correct 602 ms 613080 KB Output is correct
20 Correct 572 ms 612784 KB Output is correct
21 Correct 569 ms 613216 KB Output is correct
22 Correct 570 ms 613040 KB Output is correct
23 Correct 568 ms 612984 KB Output is correct
24 Correct 564 ms 612704 KB Output is correct
25 Correct 599 ms 616312 KB Output is correct
26 Correct 592 ms 616344 KB Output is correct
27 Correct 603 ms 616324 KB Output is correct
28 Correct 582 ms 613984 KB Output is correct
29 Correct 641 ms 616120 KB Output is correct
30 Correct 607 ms 616092 KB Output is correct
31 Correct 606 ms 616032 KB Output is correct
32 Correct 603 ms 615772 KB Output is correct
33 Correct 567 ms 612528 KB Output is correct
34 Correct 557 ms 612448 KB Output is correct
35 Correct 558 ms 612656 KB Output is correct
36 Correct 572 ms 612472 KB Output is correct
37 Correct 564 ms 612344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 612492 KB Output is correct
2 Correct 562 ms 612480 KB Output is correct
3 Correct 571 ms 612472 KB Output is correct
4 Correct 594 ms 612488 KB Output is correct
5 Correct 559 ms 612428 KB Output is correct
6 Correct 563 ms 612504 KB Output is correct
7 Correct 561 ms 612444 KB Output is correct
8 Correct 578 ms 612572 KB Output is correct
9 Correct 566 ms 612500 KB Output is correct
10 Correct 561 ms 612416 KB Output is correct
11 Correct 561 ms 612572 KB Output is correct
12 Correct 565 ms 612580 KB Output is correct
13 Correct 625 ms 612416 KB Output is correct
14 Correct 569 ms 612600 KB Output is correct
15 Correct 561 ms 612324 KB Output is correct
16 Correct 615 ms 612464 KB Output is correct
17 Correct 573 ms 612996 KB Output is correct
18 Correct 565 ms 613240 KB Output is correct
19 Correct 602 ms 613080 KB Output is correct
20 Correct 572 ms 612784 KB Output is correct
21 Correct 569 ms 613216 KB Output is correct
22 Correct 570 ms 613040 KB Output is correct
23 Correct 568 ms 612984 KB Output is correct
24 Correct 564 ms 612704 KB Output is correct
25 Correct 599 ms 616312 KB Output is correct
26 Correct 592 ms 616344 KB Output is correct
27 Correct 603 ms 616324 KB Output is correct
28 Correct 582 ms 613984 KB Output is correct
29 Correct 641 ms 616120 KB Output is correct
30 Correct 607 ms 616092 KB Output is correct
31 Correct 606 ms 616032 KB Output is correct
32 Correct 603 ms 615772 KB Output is correct
33 Correct 817 ms 637544 KB Output is correct
34 Correct 802 ms 637428 KB Output is correct
35 Correct 832 ms 637424 KB Output is correct
36 Correct 842 ms 637396 KB Output is correct
37 Correct 1002 ms 660420 KB Output is correct
38 Correct 1006 ms 660336 KB Output is correct
39 Correct 1002 ms 660384 KB Output is correct
40 Correct 1098 ms 657136 KB Output is correct
41 Correct 821 ms 625884 KB Output is correct
42 Correct 882 ms 632304 KB Output is correct
43 Correct 1150 ms 658984 KB Output is correct
44 Correct 1157 ms 659568 KB Output is correct
45 Correct 852 ms 635880 KB Output is correct
46 Correct 850 ms 635704 KB Output is correct
47 Correct 1160 ms 655856 KB Output is correct
48 Correct 1123 ms 655856 KB Output is correct
49 Correct 567 ms 612528 KB Output is correct
50 Correct 557 ms 612448 KB Output is correct
51 Correct 558 ms 612656 KB Output is correct
52 Correct 572 ms 612472 KB Output is correct
53 Correct 564 ms 612344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 579 ms 612988 KB Output is correct
2 Correct 565 ms 612828 KB Output is correct
3 Correct 564 ms 612484 KB Output is correct
4 Correct 562 ms 612496 KB Output is correct
5 Correct 603 ms 612932 KB Output is correct
6 Correct 575 ms 612896 KB Output is correct
7 Correct 574 ms 612856 KB Output is correct
8 Correct 565 ms 612828 KB Output is correct
9 Correct 565 ms 612804 KB Output is correct
10 Correct 611 ms 612600 KB Output is correct
11 Correct 609 ms 612664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 592 ms 612468 KB Output is correct
2 Correct 1812 ms 690984 KB Output is correct
3 Correct 3227 ms 782800 KB Output is correct
4 Correct 3235 ms 783660 KB Output is correct
5 Correct 3380 ms 783652 KB Output is correct
6 Correct 833 ms 636644 KB Output is correct
7 Correct 1006 ms 658592 KB Output is correct
8 Correct 1088 ms 661436 KB Output is correct
9 Correct 567 ms 612528 KB Output is correct
10 Correct 557 ms 612448 KB Output is correct
11 Correct 558 ms 612656 KB Output is correct
12 Correct 572 ms 612472 KB Output is correct
13 Correct 564 ms 612344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 612492 KB Output is correct
2 Correct 562 ms 612480 KB Output is correct
3 Correct 571 ms 612472 KB Output is correct
4 Correct 594 ms 612488 KB Output is correct
5 Correct 559 ms 612428 KB Output is correct
6 Correct 563 ms 612504 KB Output is correct
7 Correct 561 ms 612444 KB Output is correct
8 Correct 578 ms 612572 KB Output is correct
9 Correct 566 ms 612500 KB Output is correct
10 Correct 561 ms 612416 KB Output is correct
11 Correct 561 ms 612572 KB Output is correct
12 Correct 565 ms 612580 KB Output is correct
13 Correct 625 ms 612416 KB Output is correct
14 Correct 569 ms 612600 KB Output is correct
15 Correct 561 ms 612324 KB Output is correct
16 Correct 615 ms 612464 KB Output is correct
17 Correct 573 ms 612996 KB Output is correct
18 Correct 565 ms 613240 KB Output is correct
19 Correct 602 ms 613080 KB Output is correct
20 Correct 572 ms 612784 KB Output is correct
21 Correct 569 ms 613216 KB Output is correct
22 Correct 570 ms 613040 KB Output is correct
23 Correct 568 ms 612984 KB Output is correct
24 Correct 564 ms 612704 KB Output is correct
25 Correct 599 ms 616312 KB Output is correct
26 Correct 592 ms 616344 KB Output is correct
27 Correct 603 ms 616324 KB Output is correct
28 Correct 582 ms 613984 KB Output is correct
29 Correct 641 ms 616120 KB Output is correct
30 Correct 607 ms 616092 KB Output is correct
31 Correct 606 ms 616032 KB Output is correct
32 Correct 603 ms 615772 KB Output is correct
33 Correct 817 ms 637544 KB Output is correct
34 Correct 802 ms 637428 KB Output is correct
35 Correct 832 ms 637424 KB Output is correct
36 Correct 842 ms 637396 KB Output is correct
37 Correct 1002 ms 660420 KB Output is correct
38 Correct 1006 ms 660336 KB Output is correct
39 Correct 1002 ms 660384 KB Output is correct
40 Correct 1098 ms 657136 KB Output is correct
41 Correct 821 ms 625884 KB Output is correct
42 Correct 882 ms 632304 KB Output is correct
43 Correct 1150 ms 658984 KB Output is correct
44 Correct 1157 ms 659568 KB Output is correct
45 Correct 852 ms 635880 KB Output is correct
46 Correct 850 ms 635704 KB Output is correct
47 Correct 1160 ms 655856 KB Output is correct
48 Correct 1123 ms 655856 KB Output is correct
49 Correct 579 ms 612988 KB Output is correct
50 Correct 565 ms 612828 KB Output is correct
51 Correct 564 ms 612484 KB Output is correct
52 Correct 562 ms 612496 KB Output is correct
53 Correct 603 ms 612932 KB Output is correct
54 Correct 575 ms 612896 KB Output is correct
55 Correct 574 ms 612856 KB Output is correct
56 Correct 565 ms 612828 KB Output is correct
57 Correct 565 ms 612804 KB Output is correct
58 Correct 611 ms 612600 KB Output is correct
59 Correct 609 ms 612664 KB Output is correct
60 Correct 592 ms 612468 KB Output is correct
61 Correct 1812 ms 690984 KB Output is correct
62 Correct 3227 ms 782800 KB Output is correct
63 Correct 3235 ms 783660 KB Output is correct
64 Correct 3380 ms 783652 KB Output is correct
65 Correct 833 ms 636644 KB Output is correct
66 Correct 1006 ms 658592 KB Output is correct
67 Correct 1088 ms 661436 KB Output is correct
68 Correct 3970 ms 930688 KB Output is correct
69 Correct 3695 ms 930688 KB Output is correct
70 Correct 4380 ms 930756 KB Output is correct
71 Correct 4038 ms 930796 KB Output is correct
72 Runtime error 1950 ms 1048576 KB Execution killed with signal 9 (could be triggered by violating memory limits)
73 Halted 0 ms 0 KB -