Submission #497219

# Submission time Handle Problem Language Result Execution time Memory
497219 2021-12-22T18:02:59 Z Gurban Chessboard (IZhO18_chessboard) C++17
100 / 100
1898 ms 10412 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int maxn=1e5+5;
int n,k;
int xl[maxn],yl[maxn];
int xr[maxn],yr[maxn];
vector<int>v[maxn],p[maxn];

bool col(int pos,int nw,int X){
	int now = pos / X;
	now %= 2;
	return now ^ nw;
}

ll f(int X,int tp){
	ll now = 0;
	int nw = tp ^ 1;
	for(int i = 0;i < n;i++){
		if(i % X == 0) nw ^= 1;

		if(nw) now += 1ll * (n / X + 1)/2 * X;
		else now += 1ll * (n/X) / 2 * X;
	}

	nw = tp ^ 1;
	ll cnt[2]; cnt[0] = cnt[1] = 0;

	for(int i = 0;i < n;i++){
		if(i % X == 0){
			nw ^= 1;
			swap(cnt[0],cnt[1]);
		}
		for(auto j : v[i]){
			int z = xl[j] + (X - xl[j] % X);	
			if(z > xr[j]){
				int cl = col(xl[j],nw,X);
				cnt[cl] += (xr[j] - xl[j] + 1);
				continue;
			}
			if(z + X - 1 >= xr[j]){
				int cl = col(xl[j],nw,X);
				cnt[cl] += (z - xl[j]);

				cl = col(z,nw,X);
				cnt[cl] += (xr[j] - z + 1);
				continue;
			}
			int pos = xr[j] - (xr[j] % X); pos--;	
			
			int cl = col(xl[j],nw,X);
			cnt[cl] += (z - xl[j]);

			cl = col(xr[j],nw,X);
			cnt[cl] += (xr[j] - pos);
			
			cl = col(z,nw,X);
			cnt[cl] += ((pos-z+1)/X+1)/2*X;
			cnt[cl ^ 1] += ((pos-z+1)/X)/2*X;
		}

		// if(X == 2 and tp == 1) cout<<i<<' '<<cnt[0]<<' '<<cnt[1]<<'\n';

		now += cnt[0];
		now -= cnt[1];

		for(auto j : p[i]){
			int z = xl[j] + (X - xl[j] % X);	
			if(z > xr[j]){
				int cl = col(xl[j],nw,X);
				cnt[cl] -= (xr[j] - xl[j] + 1);
				continue;
			}
			if(z + X - 1 >= xr[j]){
				int cl = col(xl[j],nw,X);
				cnt[cl] -= (z - xl[j]);

				cl = col(z,nw,X);
				cnt[cl] -= (xr[j] - z + 1);
				continue;
			}
			int pos = xr[j] - (xr[j] % X); pos--;	
			
			int cl = col(xl[j],nw,X);
			cnt[cl] -= (z - xl[j]);

			cl = col(xr[j],nw,X);
			cnt[cl] -= (xr[j] - pos);
			
			cl = col(z,nw,X);
			cnt[cl] -= ((pos-z+1)/X+1)/2*X;
			cnt[cl ^ 1] -= ((pos-z+1)/X)/2*X;
		}
	}

	// cout<<X<<' '<<tp<<' '<<now<<'\n';

	return now;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> k;
	for(int i = 1;i <= k;i++){
		cin >> xl[i] >> yl[i] >> xr[i] >> yr[i];
		xl[i]--,yl[i]--;
		xr[i]--,yr[i]--;
		v[yl[i]].push_back(i);
		p[yr[i]].push_back(i);
	}
	ll ans = 1ll * n * n;
	for(int i = 1;i < n;i++) if(n % i == 0){
		ans=min(ans,f(i,0));
		ans=min(ans,f(i,1));
	}
	cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5020 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 8460 KB Output is correct
2 Correct 12 ms 5828 KB Output is correct
3 Correct 29 ms 7628 KB Output is correct
4 Correct 20 ms 6468 KB Output is correct
5 Correct 43 ms 8220 KB Output is correct
6 Correct 23 ms 7112 KB Output is correct
7 Correct 8 ms 5452 KB Output is correct
8 Correct 24 ms 7064 KB Output is correct
9 Correct 52 ms 8468 KB Output is correct
10 Correct 33 ms 7572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 5024 KB Output is correct
8 Correct 4 ms 5004 KB Output is correct
9 Correct 3 ms 5024 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5024 KB Output is correct
13 Correct 5 ms 5068 KB Output is correct
14 Correct 3 ms 5068 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 5024 KB Output is correct
8 Correct 4 ms 5004 KB Output is correct
9 Correct 3 ms 5024 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5024 KB Output is correct
13 Correct 5 ms 5068 KB Output is correct
14 Correct 3 ms 5068 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
16 Correct 16 ms 5796 KB Output is correct
17 Correct 32 ms 7492 KB Output is correct
18 Correct 63 ms 7628 KB Output is correct
19 Correct 175 ms 7424 KB Output is correct
20 Correct 197 ms 7712 KB Output is correct
21 Correct 30 ms 7428 KB Output is correct
22 Correct 4 ms 5000 KB Output is correct
23 Correct 37 ms 6388 KB Output is correct
24 Correct 48 ms 7492 KB Output is correct
25 Correct 9 ms 5316 KB Output is correct
26 Correct 29 ms 6604 KB Output is correct
27 Correct 43 ms 7092 KB Output is correct
28 Correct 48 ms 7568 KB Output is correct
29 Correct 14 ms 5964 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 8460 KB Output is correct
2 Correct 12 ms 5828 KB Output is correct
3 Correct 29 ms 7628 KB Output is correct
4 Correct 20 ms 6468 KB Output is correct
5 Correct 43 ms 8220 KB Output is correct
6 Correct 23 ms 7112 KB Output is correct
7 Correct 8 ms 5452 KB Output is correct
8 Correct 24 ms 7064 KB Output is correct
9 Correct 52 ms 8468 KB Output is correct
10 Correct 33 ms 7572 KB Output is correct
11 Correct 3 ms 5008 KB Output is correct
12 Correct 4 ms 4940 KB Output is correct
13 Correct 4 ms 4940 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 5024 KB Output is correct
18 Correct 4 ms 5004 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 4940 KB Output is correct
22 Correct 3 ms 5024 KB Output is correct
23 Correct 5 ms 5068 KB Output is correct
24 Correct 3 ms 5068 KB Output is correct
25 Correct 3 ms 5068 KB Output is correct
26 Correct 16 ms 5796 KB Output is correct
27 Correct 32 ms 7492 KB Output is correct
28 Correct 63 ms 7628 KB Output is correct
29 Correct 175 ms 7424 KB Output is correct
30 Correct 197 ms 7712 KB Output is correct
31 Correct 30 ms 7428 KB Output is correct
32 Correct 4 ms 5000 KB Output is correct
33 Correct 37 ms 6388 KB Output is correct
34 Correct 48 ms 7492 KB Output is correct
35 Correct 9 ms 5316 KB Output is correct
36 Correct 29 ms 6604 KB Output is correct
37 Correct 43 ms 7092 KB Output is correct
38 Correct 48 ms 7568 KB Output is correct
39 Correct 14 ms 5964 KB Output is correct
40 Correct 4 ms 5068 KB Output is correct
41 Correct 363 ms 9884 KB Output is correct
42 Correct 96 ms 10308 KB Output is correct
43 Correct 199 ms 9796 KB Output is correct
44 Correct 135 ms 10272 KB Output is correct
45 Correct 71 ms 10412 KB Output is correct
46 Correct 424 ms 10248 KB Output is correct
47 Correct 61 ms 9904 KB Output is correct
48 Correct 196 ms 10052 KB Output is correct
49 Correct 86 ms 9928 KB Output is correct
50 Correct 1669 ms 9876 KB Output is correct
51 Correct 1750 ms 10208 KB Output is correct
52 Correct 1596 ms 9916 KB Output is correct
53 Correct 1726 ms 10140 KB Output is correct
54 Correct 1596 ms 9864 KB Output is correct
55 Correct 1898 ms 10248 KB Output is correct
56 Correct 1632 ms 9768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5020 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 42 ms 8460 KB Output is correct
10 Correct 12 ms 5828 KB Output is correct
11 Correct 29 ms 7628 KB Output is correct
12 Correct 20 ms 6468 KB Output is correct
13 Correct 43 ms 8220 KB Output is correct
14 Correct 23 ms 7112 KB Output is correct
15 Correct 8 ms 5452 KB Output is correct
16 Correct 24 ms 7064 KB Output is correct
17 Correct 52 ms 8468 KB Output is correct
18 Correct 33 ms 7572 KB Output is correct
19 Correct 3 ms 5008 KB Output is correct
20 Correct 4 ms 4940 KB Output is correct
21 Correct 4 ms 4940 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 3 ms 4940 KB Output is correct
25 Correct 3 ms 5024 KB Output is correct
26 Correct 4 ms 5004 KB Output is correct
27 Correct 3 ms 5024 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
29 Correct 3 ms 4940 KB Output is correct
30 Correct 3 ms 5024 KB Output is correct
31 Correct 5 ms 5068 KB Output is correct
32 Correct 3 ms 5068 KB Output is correct
33 Correct 3 ms 5068 KB Output is correct
34 Correct 16 ms 5796 KB Output is correct
35 Correct 32 ms 7492 KB Output is correct
36 Correct 63 ms 7628 KB Output is correct
37 Correct 175 ms 7424 KB Output is correct
38 Correct 197 ms 7712 KB Output is correct
39 Correct 30 ms 7428 KB Output is correct
40 Correct 4 ms 5000 KB Output is correct
41 Correct 37 ms 6388 KB Output is correct
42 Correct 48 ms 7492 KB Output is correct
43 Correct 9 ms 5316 KB Output is correct
44 Correct 29 ms 6604 KB Output is correct
45 Correct 43 ms 7092 KB Output is correct
46 Correct 48 ms 7568 KB Output is correct
47 Correct 14 ms 5964 KB Output is correct
48 Correct 4 ms 5068 KB Output is correct
49 Correct 363 ms 9884 KB Output is correct
50 Correct 96 ms 10308 KB Output is correct
51 Correct 199 ms 9796 KB Output is correct
52 Correct 135 ms 10272 KB Output is correct
53 Correct 71 ms 10412 KB Output is correct
54 Correct 424 ms 10248 KB Output is correct
55 Correct 61 ms 9904 KB Output is correct
56 Correct 196 ms 10052 KB Output is correct
57 Correct 86 ms 9928 KB Output is correct
58 Correct 1669 ms 9876 KB Output is correct
59 Correct 1750 ms 10208 KB Output is correct
60 Correct 1596 ms 9916 KB Output is correct
61 Correct 1726 ms 10140 KB Output is correct
62 Correct 1596 ms 9864 KB Output is correct
63 Correct 1898 ms 10248 KB Output is correct
64 Correct 1632 ms 9768 KB Output is correct
65 Correct 3 ms 4940 KB Output is correct
66 Correct 3 ms 4940 KB Output is correct
67 Correct 1489 ms 8984 KB Output is correct
68 Correct 1488 ms 9052 KB Output is correct
69 Correct 1215 ms 8216 KB Output is correct
70 Correct 1311 ms 8392 KB Output is correct
71 Correct 1347 ms 8644 KB Output is correct
72 Correct 1304 ms 8476 KB Output is correct
73 Correct 1374 ms 8564 KB Output is correct
74 Correct 1571 ms 9224 KB Output is correct
75 Correct 1444 ms 8728 KB Output is correct
76 Correct 1481 ms 8900 KB Output is correct
77 Correct 239 ms 9028 KB Output is correct
78 Correct 79 ms 8388 KB Output is correct
79 Correct 172 ms 8648 KB Output is correct
80 Correct 172 ms 8816 KB Output is correct
81 Correct 180 ms 8696 KB Output is correct
82 Correct 136 ms 9116 KB Output is correct
83 Correct 139 ms 8784 KB Output is correct
84 Correct 925 ms 9120 KB Output is correct
85 Correct 1679 ms 9496 KB Output is correct
86 Correct 245 ms 5016 KB Output is correct
87 Correct 252 ms 5012 KB Output is correct
88 Correct 1596 ms 9172 KB Output is correct
89 Correct 499 ms 6216 KB Output is correct
90 Correct 247 ms 5020 KB Output is correct