#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int n, a, b, c, K, M, k, m; long long result; int tab[2005][2005]; int baza1[2005]; int baza2[2005]; unsigned long long int czy[2005][35]; unsigned long long int tablica[65]; vector <pair <int, pair <int, int> > > v; vector <pair <int, pair <int, int> > >::iterator it; vector <int> pomoc; void wewnetrzna (int x, int y) { int z = baza1[x]; if (z > y) { baza1[x] = y; baza2[y] = x; if (baza1[y + 1] == 0) { baza2[z] = y + 1; baza1[y + 1] = z; } else { baza2[z] = 0; wewnetrzna(y + 1, z); } } else { if (baza1[z + 1] == 0) { baza2[y] = z + 1; baza1[z + 1] = y; } else wewnetrzna(z + 1, y); } } void zewnetrzna (int x, int y) { int z = baza2[y]; if (z < x) { baza2[y] = x; baza1[x] = y; if (baza2[x - 1] == 0) { baza1[z] = x - 1; baza2[x - 1] = z; } else { baza1[z] = 0; zewnetrzna(z, x - 1); } } else { if (baza2[z - 1] == 0) { baza1[x] = z - 1; baza2[z - 1] = x; } else zewnetrzna(x, z - 1); } } void funkcja (int, int); void podwojna (int x, int y) { int z = baza1[x]; int w = baza2[y]; if (w > z) funkcja(z + 1, w - 1); else { pomoc.clear(); pomoc.push_back(z); pomoc.push_back(w); pomoc.push_back(x); pomoc.push_back(y); sort(pomoc.begin(), pomoc.end()); int x1 = pomoc[0]; int x2 = pomoc[1]; int x3 = pomoc[2]; int x4 = pomoc[3]; baza1[x2] = x3; baza2[x3] = x2; baza1[x1] = 0; baza2[x4] = 0; funkcja(x1, x2 - 1); funkcja(x3 + 1, x4); } } void funkcja (int x, int y) { if (baza1[x] == 0) { if (baza2[y] == 0) { baza1[x] = y; baza2[y] = x; } else zewnetrzna(x, y); } else { if (baza2[y] == 0) wewnetrzna(x, y); else podwojna(x, y); } } int main () { tablica[0] = 1; for (int i = 1; i < 64; ++i) tablica[i] = 2 * tablica[i - 1]; scanf("%d",&n); K = (n - 1) / 64; M = n % 64; if (M == 0) M = 64; for (int i = 1; i <= n; ++i) for (int j = i; j <= n; ++j) { scanf("%d",&tab[i][j]); v.push_back(make_pair(tab[i][j], make_pair(i, j))); } sort(v.begin(), v.end()); for (it = v.begin(); it != v.end(); ++it) { a = it->second.first; b = it->second.second; c = it->first; if ((czy[a][(b - 1) / 64] & tablica[(b - 1) % 64]) == 0) { result += (long long)(tab[a][b]); funkcja(a, b); k = K; m = M; for (int i = n; i > 0; --i) { int q = baza1[i]; if (q > 0) { q++; for (int j = k; j <= K; ++j) { czy[i][j] = czy[q][j]; } q -= 2; czy[i][q / 64] |= tablica[q % 64]; } m--; if (m == 0) { m = 64; k--; } } } } printf("%lld\n",result); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int n, a, b, c, K, M, k, m; long long result; int tab[2005][2005]; int baza1[2005]; int baza2[2005]; unsigned long long int czy[2005][35]; unsigned long long int tablica[65]; vector <pair <int, pair <int, int> > > v; vector <pair <int, pair <int, int> > >::iterator it; vector <int> pomoc; void wewnetrzna (int x, int y) { int z = baza1[x]; if (z > y) { baza1[x] = y; baza2[y] = x; if (baza1[y + 1] == 0) { baza2[z] = y + 1; baza1[y + 1] = z; } else { baza2[z] = 0; wewnetrzna(y + 1, z); } } else { if (baza1[z + 1] == 0) { baza2[y] = z + 1; baza1[z + 1] = y; } else wewnetrzna(z + 1, y); } } void zewnetrzna (int x, int y) { int z = baza2[y]; if (z < x) { baza2[y] = x; baza1[x] = y; if (baza2[x - 1] == 0) { baza1[z] = x - 1; baza2[x - 1] = z; } else { baza1[z] = 0; zewnetrzna(z, x - 1); } } else { if (baza2[z - 1] == 0) { baza1[x] = z - 1; baza2[z - 1] = x; } else zewnetrzna(x, z - 1); } } void funkcja (int, int); void podwojna (int x, int y) { int z = baza1[x]; int w = baza2[y]; if (w > z) funkcja(z + 1, w - 1); else { pomoc.clear(); pomoc.push_back(z); pomoc.push_back(w); pomoc.push_back(x); pomoc.push_back(y); sort(pomoc.begin(), pomoc.end()); int x1 = pomoc[0]; int x2 = pomoc[1]; int x3 = pomoc[2]; int x4 = pomoc[3]; baza1[x2] = x3; baza2[x3] = x2; baza1[x1] = 0; baza2[x4] = 0; funkcja(x1, x2 - 1); funkcja(x3 + 1, x4); } } void funkcja (int x, int y) { if (baza1[x] == 0) { if (baza2[y] == 0) { baza1[x] = y; baza2[y] = x; } else zewnetrzna(x, y); } else { if (baza2[y] == 0) wewnetrzna(x, y); else podwojna(x, y); } } int main () { tablica[0] = 1; for (int i = 1; i < 64; ++i) tablica[i] = 2 * tablica[i - 1]; scanf("%d",&n); K = (n - 1) / 64; M = n % 64; if (M == 0) M = 64; for (int i = 1; i <= n; ++i) for (int j = i; j <= n; ++j) { scanf("%d",&tab[i][j]); v.push_back(make_pair(tab[i][j], make_pair(i, j))); } sort(v.begin(), v.end()); for (it = v.begin(); it != v.end(); ++it) { a = it->second.first; b = it->second.second; c = it->first; if ((czy[a][(b - 1) / 64] & tablica[(b - 1) % 64]) == 0) { result += (long long)(tab[a][b]); funkcja(a, b); k = K; m = M; for (int i = n; i > 0; --i) { int q = baza1[i]; if (q > 0) { q++; for (int j = k; j <= K; ++j) { czy[i][j] = czy[q][j]; } q -= 2; czy[i][q / 64] |= tablica[q % 64]; } m--; if (m == 0) { m = 64; k--; } } } } printf("%lld\n",result); return 0; } |