답안 #385447

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
385447 2021-04-04T09:54:19 Z vanic Chessboard (IZhO18_chessboard) C++14
70 / 100
691 ms 4384 KB
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <array>
#include <bitset>
#include <cassert>

using namespace std;

typedef long long ll;

const int maxn=1e5+5;

array < int, 4 > l[maxn];
ll n, k;

inline ll dodaj(ll val, int br, bool p){
	if((br&1 && p) || (!(br&1) && !p)){
		return val;
	}
	return 0;
}
bool stima(int br, bool p){
	return (br&1 && p) || (!(br&1) && !p);
}

ll probaj(ll val, bool p){
	ll br=0;
	ll x1, y1, x2, y2;
	ll pocx, pocy, krajx, krajy;
	ll cor1, cor2, cor3, cor4;
	for(int i=0; i<k; i++){
		x1=l[i][0];
		y1=l[i][1];
		x2=l[i][2];
		y2=l[i][3];
		pocx=x1/val;
		pocy=y1/val;
		krajx=x2/val;
		krajy=y2/val;
		if(pocx==krajx && pocy==krajy){
			br+=dodaj((x2-x1+1)*(y2-y1+1), pocx+pocy, p);
		}
		else if(pocx==krajx){
			cor1=x1;
			cor2=y1;
			cor3=min(x2, (pocx+1)*val-1);
			cor4=min(y2, (pocy+1)*val-1);
			br+=dodaj((cor3-cor1+1)*(cor4-cor2+1), pocx+pocy, p);
			
			cor1=max(x1, pocx*val);
			cor2=max(y1, pocy*val);
			cor3=x2;
			cor4=y2;
			br+=dodaj((cor3-cor1+1)*(cor4-cor2+1), pocx+pocy, p);
			
			pocy++;
			krajy--;
			if(pocy>krajy){
				continue;
			}
			cor1=x1;
			cor2=pocy*val;
			cor3=x2;
			cor4=(pocy+1)*val-1;
			
			if(stima(pocx+pocy, p)){
				br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajy-pocy+2)/2;
			}
			else{
				br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajy-pocy+1)/2;
			}
		}
		else if(pocy==krajy){
			cor1=x1;
			cor2=y1;
			cor3=min(x2, (pocx+1)*val-1);
			cor4=min(y2, (pocy+1)*val-1);
			br+=dodaj((cor3-cor1+1)*(cor4-cor2+1), pocx+pocy, p);
			
			cor1=max(x1, pocx*val);
			cor2=max(y1, pocy*val);
			cor3=x2;
			cor4=y2;
			br+=dodaj((cor3-cor1+1)*(cor4-cor2+1), pocx+pocy, p);
			
			pocx++;
			krajx--;
			if(pocx>krajx){
				continue;
			}
			cor1=pocx*val;
			cor2=y1;
			cor3=(pocx+1)*val-1;
			cor4=y2;
			
			if(stima(pocx+pocy, p)){
				br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajx-pocx+2)/2;
			}
			else{
				br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajx-pocx+1)/2;
			}
		}
		else{
			cor1=x1;
			cor2=y1;
			cor3=min(x2, (pocx+1)*val-1);
			cor4=min(y2, (pocy+1)*val-1);
			br+=dodaj((cor3-cor1+1)*(cor4-cor2+1), pocx+pocy, p);

			cor1=max(x1, pocx*val);
			cor2=max(y1, pocy*val);
			cor3=x2;
			cor4=y2;
			br+=dodaj((cor3-cor1+1)*(cor4-cor2+1), pocx+pocy, p);

			cor1=x1;
			cor2=max(y1, pocy*val);
			cor3=min(x2, (pocx+1)*val-1);
			cor4=y2;
			br+=dodaj((cor3-cor1+1)*(cor4-cor2+1), pocx+pocy, p);

			cor1=max(x1, pocx*val);
			cor2=y1;
			cor3=x2;
			cor4=min(y2, (pocy+1)*val-1);
			br+=dodaj((cor3-cor1+1)*(cor4-cor2+1), pocx+pocy, p);
			
			pocx++;
			krajx--;
			if(krajx>=pocx){
				cor1=pocx*val;
				cor2=y1;
				cor3=(pocx+1)*val-1;
				cor4=(pocy+1)*val-1;
				if(stima(pocx+pocy, p)){
					br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajx-pocx+2)/2;
				}
				else{
					br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajx-pocx+1)/2;
				}
				
				cor2=krajy*val;
				cor4=y2;
				if(stima(pocx+krajy, p)){
					br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajx-pocx+2)/2;
				}
				else{
					br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajx-pocx+1)/2;
				}
			}
			pocx--;
			krajx++;
			
			pocy++;
			krajy--;
			if(krajy>=pocy){
				cor1=x1;
				cor2=pocy*val;
				cor3=(pocx+1)*val-1;
				cor4=(pocy+1)*val-1;
				if(stima(pocx+pocy, p)){
					br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajy-pocy+2)/2;
				}
				else{
					br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajy-pocy+1)/2;
				}
				
				cor1=krajx*val;
				cor3=x2;
				if(stima(krajx+pocy, p)){
					br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajy-pocy+2)/2;
				}
				else{
					br+=(cor3-cor1+1)*(cor4-cor2+1)*(krajy-pocy+1)/2;
				}
			}
			pocy--;
			krajy++;
			
			if(((pocx+pocy)&1 && p) || (!((pocx+pocy)&1) && !p)){
				br+=((krajx-pocx-1)*(krajy-pocy-1)+1)/2*val*val;
			}
			else{
				br+=(krajx-pocx-1)*(krajy-pocy-1)/2*val*val;
			}
		}
	}
	return br;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> k;
	ll uk;
	int a, b, c, d;
//	cout << "proso" << endl;
	for(int i=0; i<k; i++){
//		cout << "uso" << endl;
		cin >> a >> b >> c >> d;
		a--;
		b--;
		c--;
		d--;
		uk+=(ll)(c-a)*(d-b);
		l[i]={a, b, c, d};
	}
	ll sol=1e18;
	ll br1, br2;
	ll crno, bijelo;
	for(ll i=1; i*i<=n; i++){
//		cout << i << endl;
		if(n%i==0){
			br1=probaj(i, 0);
			br2=probaj(i, 1);
			crno=((n/i)*(n/i)+1)/2*i*i;
			bijelo=(n/i)*(n/i)/2*i*i;
			assert(crno+bijelo==n*n);
			sol=min(sol, crno-br1+k-br1);
			swap(crno, bijelo);
			sol=min(sol, crno-br2+k-br2);
			if(i!=1 && i*i!=n){
				br1=probaj(n/i, 0);
				br2=probaj(n/i, 1);
				crno=(i*i+1)/2*(n/i)*(n/i);
				bijelo=i*i/2*(n/i)*(n/i);
				sol=min(sol, crno-br1+k-br1);
				swap(crno, bijelo);
				sol=min(sol, crno-br2+k-br2);
			}
		}
	}
	cout << sol << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 1260 KB Output is correct
2 Correct 8 ms 620 KB Output is correct
3 Correct 18 ms 1004 KB Output is correct
4 Correct 19 ms 1004 KB Output is correct
5 Correct 24 ms 1132 KB Output is correct
6 Correct 16 ms 876 KB Output is correct
7 Correct 4 ms 492 KB Output is correct
8 Correct 16 ms 928 KB Output is correct
9 Correct 39 ms 1772 KB Output is correct
10 Correct 22 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 14 ms 1260 KB Output is correct
17 Correct 31 ms 3052 KB Output is correct
18 Correct 46 ms 3436 KB Output is correct
19 Correct 142 ms 3180 KB Output is correct
20 Correct 156 ms 3436 KB Output is correct
21 Correct 32 ms 2924 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 29 ms 1644 KB Output is correct
24 Correct 42 ms 3308 KB Output is correct
25 Correct 7 ms 620 KB Output is correct
26 Correct 26 ms 2156 KB Output is correct
27 Correct 39 ms 2668 KB Output is correct
28 Correct 44 ms 3436 KB Output is correct
29 Correct 13 ms 1388 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 1260 KB Output is correct
2 Correct 8 ms 620 KB Output is correct
3 Correct 18 ms 1004 KB Output is correct
4 Correct 19 ms 1004 KB Output is correct
5 Correct 24 ms 1132 KB Output is correct
6 Correct 16 ms 876 KB Output is correct
7 Correct 4 ms 492 KB Output is correct
8 Correct 16 ms 928 KB Output is correct
9 Correct 39 ms 1772 KB Output is correct
10 Correct 22 ms 1132 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 2 ms 364 KB Output is correct
19 Correct 2 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 14 ms 1260 KB Output is correct
27 Correct 31 ms 3052 KB Output is correct
28 Correct 46 ms 3436 KB Output is correct
29 Correct 142 ms 3180 KB Output is correct
30 Correct 156 ms 3436 KB Output is correct
31 Correct 32 ms 2924 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
33 Correct 29 ms 1644 KB Output is correct
34 Correct 42 ms 3308 KB Output is correct
35 Correct 7 ms 620 KB Output is correct
36 Correct 26 ms 2156 KB Output is correct
37 Correct 39 ms 2668 KB Output is correct
38 Correct 44 ms 3436 KB Output is correct
39 Correct 13 ms 1388 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 131 ms 3692 KB Output is correct
42 Correct 55 ms 4152 KB Output is correct
43 Correct 81 ms 3692 KB Output is correct
44 Correct 49 ms 3948 KB Output is correct
45 Correct 43 ms 4204 KB Output is correct
46 Correct 146 ms 4076 KB Output is correct
47 Correct 38 ms 3820 KB Output is correct
48 Correct 65 ms 3948 KB Output is correct
49 Correct 46 ms 3692 KB Output is correct
50 Correct 634 ms 3948 KB Output is correct
51 Correct 682 ms 4300 KB Output is correct
52 Correct 626 ms 4076 KB Output is correct
53 Correct 667 ms 4332 KB Output is correct
54 Correct 641 ms 3928 KB Output is correct
55 Correct 691 ms 4384 KB Output is correct
56 Correct 598 ms 3820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 27 ms 1260 KB Output is correct
10 Correct 8 ms 620 KB Output is correct
11 Correct 18 ms 1004 KB Output is correct
12 Correct 19 ms 1004 KB Output is correct
13 Correct 24 ms 1132 KB Output is correct
14 Correct 16 ms 876 KB Output is correct
15 Correct 4 ms 492 KB Output is correct
16 Correct 16 ms 928 KB Output is correct
17 Correct 39 ms 1772 KB Output is correct
18 Correct 22 ms 1132 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 2 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 2 ms 364 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 2 ms 364 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 14 ms 1260 KB Output is correct
35 Correct 31 ms 3052 KB Output is correct
36 Correct 46 ms 3436 KB Output is correct
37 Correct 142 ms 3180 KB Output is correct
38 Correct 156 ms 3436 KB Output is correct
39 Correct 32 ms 2924 KB Output is correct
40 Correct 2 ms 364 KB Output is correct
41 Correct 29 ms 1644 KB Output is correct
42 Correct 42 ms 3308 KB Output is correct
43 Correct 7 ms 620 KB Output is correct
44 Correct 26 ms 2156 KB Output is correct
45 Correct 39 ms 2668 KB Output is correct
46 Correct 44 ms 3436 KB Output is correct
47 Correct 13 ms 1388 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Correct 131 ms 3692 KB Output is correct
50 Correct 55 ms 4152 KB Output is correct
51 Correct 81 ms 3692 KB Output is correct
52 Correct 49 ms 3948 KB Output is correct
53 Correct 43 ms 4204 KB Output is correct
54 Correct 146 ms 4076 KB Output is correct
55 Correct 38 ms 3820 KB Output is correct
56 Correct 65 ms 3948 KB Output is correct
57 Correct 46 ms 3692 KB Output is correct
58 Correct 634 ms 3948 KB Output is correct
59 Correct 682 ms 4300 KB Output is correct
60 Correct 626 ms 4076 KB Output is correct
61 Correct 667 ms 4332 KB Output is correct
62 Correct 641 ms 3928 KB Output is correct
63 Correct 691 ms 4384 KB Output is correct
64 Correct 598 ms 3820 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Incorrect 1 ms 364 KB Output isn't correct
67 Halted 0 ms 0 KB -