#ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #else #define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #endif #include <cstdlib> #include <ctime> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <queue> #include <map> #include <set> #include <list> #include <algorithm> #include <string> #include <iostream> #define FOR(x,y,z) for (int x=(y); x<=(z); ++x) #define FORD(x,y,z) for(int x=(y); x>=(z); --x) #define REP(x,y) for (int x=0; x<(y); ++x) #if defined(__GNUC__) && __cplusplus < 201103L #define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y) #else #define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y) #endif #define ALL(x) (x).begin(),(x).end() #define SIZE(x) ((int)(x).size()) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; typedef unsigned uint; const int INF = 1000000001; inline int Pow2(int exp) { return 1 << exp; } template<typename T> inline T Pow2(int exp) { return (T)1 << exp; } int Log2(ULL x, bool ceil) { unsigned long r; #ifdef __GNUC__ r = 63 - __builtin_clzll(x); #else _BitScanReverse64(&r, x); #endif return r + (ceil && x > Pow2<ULL>(r)); } struct Tree { int n,D; VI stix; vector<uint> d, dr, B; Tree() : n(0), D(-1) { } int AddWord(int ln) { stix.push_back(n); n += ln; return SIZE(stix)-1; } void Create(); uint Get(int w, int ix, int ln) const; uint GetRev(int w, int ix, int ln) const; void Set(int w, int ix); }; void Tree::Create() { B.resize(n); B[0] = 1; FOR(i,1,n-1) B[i] = B[i-1] * 3; D = Pow2(Log2(n,true)); d.resize(2*D, 1); int k = 1; FORD(i,D-1,1) { d[i] = d[i*2] + d[i*2+1] * B[k]; if (Pow2(Log2(i, false)) == i) k *= 2; } dr.resize(2*D, 1); k = 1; FORD(i,D-1,1) { dr[i] = dr[i*2] * B[k] + dr[i*2+1]; if (Pow2(Log2(i, false)) == i) k *= 2; } } uint Tree::Get(int w, int ix, int ln) const { if (ln == 0) return 0; int ix1 = ix + D + stix[w]; if (ln == 1) return d[ix1]; int ix2 = ix1 + ln - 1; int l = 1, r = ln-1, k = 1; uint res = d[ix1] + d[ix2] * B[ln-1]; while (ix1/2 != ix2/2) { if (ix1%2 == 0) { res += d[ix1+1] * B[l]; l += k; } if (ix2%2 == 1) { res += d[ix2-1] * B[r-k]; r -= k; } k *= 2; ix1 /= 2; ix2 /= 2; } return res; } uint Tree::GetRev(int w, int ix, int ln) const { if (ln == 0) return 0; int ix1 = ix + D + stix[w]; if (ln == 1) return dr[ix1]; int ix2 = ix1 + ln - 1; int l = 1, r = ln-1, k = 1; uint res = dr[ix1] * B[ln-1] + dr[ix2]; while (ix1/2 != ix2/2) { if (ix1%2 == 0) { res += dr[ix1+1] * B[r-k]; r -= k; } if (ix2%2 == 1) { res += dr[ix2-1] * B[l]; l += k; } k *= 2; ix1 /= 2; ix2 /= 2; } return res; } void Tree::Set(int w, int ix) { ix += D + stix[w]; int ixr = ix; if (d[ix] == 2) return; d[ix] = 2; int k = 1; while (ix != 1) { ix /= 2; d[ix] = d[ix*2] + d[ix*2+1] * B[k]; k *= 2; } dr[ixr] = 2; k = 1; while (ixr != 1) { ixr /= 2; dr[ixr] = dr[ixr*2] * B[k] + dr[ixr*2+1]; k *= 2; } } struct Problem { int n; VVI c; Tree tree; queue<PII> q; Problem(int n); LL Go(); void Bfs(int a0, int b0); void Process1(int w1, int w2, int p1, int p2, int s, int ln, int d); void Process2(int a, int b); void Visit(int a, int b) { if (a > b) swap(a, b); tree.Set(a*2, b-a); tree.Set(b*2+1, b-a); q.push(PII(a,b)); } }; Problem::Problem(int n) : n(n), c(n) { REP(i,n) c[i].resize(n-i); VI z(n); REP(i,n) { tree.AddWord(n-i); tree.AddWord(i+1); } tree.Create(); } struct Cmp { const VVI& c; Cmp(const VVI& c) : c(c) { } bool operator()(const PII& p1, const PII& p2) const { return c[p1.first][p1.second-p1.first] < c[p2.first][p2.second-p2.first]; } }; LL Problem::Go() { VPII xs; xs.reserve(n*(n+1)/2); REP(i,n) { FOR(j,i,n-1) xs.push_back(PII(i,j)); } sort(ALL(xs),Cmp(c)); LL res = 0; FOREACH(it,xs) { if (tree.Get(2*it->first, it->second-it->first, 1) == 2) continue; res += c[it->first][it->second - it->first]; Bfs(it->first, it->second); } return res; } void Problem::Process1(int w1, int w2, int p1, int p2, int s, int ln, int d) { REP(i,ln) { int b = i, e = ln; while (b < e) { int m = (b+e+1)/2; if (tree.Get(w1, d, m) == tree.Get(w2, 0, m)) b = m; else e = m-1; } if (b == ln) break; if (tree.Get(w2, b, 1) == 1) Visit(p2, p2+b*s); else Visit(p1, p2+b*s); i = b; } } void Problem::Process2(int a, int b) { FOR(i,0,b-a-1) { int x1 = i, x2 = b-a; while (x1 < x2) { int m = (x1+x2+1)/2; if (tree.Get(a*2, 0, m) == tree.GetRev(b*2+1, b-a-m, m)) x1 = m; else x2 = m-1; } if (x1 == b-a) break; if (tree.Get(a*2, x1, 1) == 1) Visit(a, a+x1); else Visit(a+x1+1, b); i = x1; } } void Problem::Bfs(int a0, int b0) { Visit(a0, b0); while (!q.empty()) { int a = q.front().first; int b = q.front().second; q.pop(); if (b+1 < n) { int ln1 = n-a, ln2 = n-(b+1); Process1(a*2, (b+1)*2, a, b+1, 1, ln2, ln1-ln2); } if (a-1 >= 0) { int ln1 = b+1, ln2 = a; Process1(b*2+1, (a-1)*2+1, b, a-1, -1, ln2, ln1-ln2); } if (b-a >= 1) Process2(a, b); } } int main(int argc, char** argv) { int n; scanf("%d", &n); Problem p(n); REP(i,n) { REP(j,n-i) scanf("%d", &p.c[i][j]); } printf("%lld\n", p.Go()); #ifdef _DEBUG system("pause"); #endif 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #else #define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #endif #include <cstdlib> #include <ctime> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <queue> #include <map> #include <set> #include <list> #include <algorithm> #include <string> #include <iostream> #define FOR(x,y,z) for (int x=(y); x<=(z); ++x) #define FORD(x,y,z) for(int x=(y); x>=(z); --x) #define REP(x,y) for (int x=0; x<(y); ++x) #if defined(__GNUC__) && __cplusplus < 201103L #define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y) #else #define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y) #endif #define ALL(x) (x).begin(),(x).end() #define SIZE(x) ((int)(x).size()) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; typedef unsigned uint; const int INF = 1000000001; inline int Pow2(int exp) { return 1 << exp; } template<typename T> inline T Pow2(int exp) { return (T)1 << exp; } int Log2(ULL x, bool ceil) { unsigned long r; #ifdef __GNUC__ r = 63 - __builtin_clzll(x); #else _BitScanReverse64(&r, x); #endif return r + (ceil && x > Pow2<ULL>(r)); } struct Tree { int n,D; VI stix; vector<uint> d, dr, B; Tree() : n(0), D(-1) { } int AddWord(int ln) { stix.push_back(n); n += ln; return SIZE(stix)-1; } void Create(); uint Get(int w, int ix, int ln) const; uint GetRev(int w, int ix, int ln) const; void Set(int w, int ix); }; void Tree::Create() { B.resize(n); B[0] = 1; FOR(i,1,n-1) B[i] = B[i-1] * 3; D = Pow2(Log2(n,true)); d.resize(2*D, 1); int k = 1; FORD(i,D-1,1) { d[i] = d[i*2] + d[i*2+1] * B[k]; if (Pow2(Log2(i, false)) == i) k *= 2; } dr.resize(2*D, 1); k = 1; FORD(i,D-1,1) { dr[i] = dr[i*2] * B[k] + dr[i*2+1]; if (Pow2(Log2(i, false)) == i) k *= 2; } } uint Tree::Get(int w, int ix, int ln) const { if (ln == 0) return 0; int ix1 = ix + D + stix[w]; if (ln == 1) return d[ix1]; int ix2 = ix1 + ln - 1; int l = 1, r = ln-1, k = 1; uint res = d[ix1] + d[ix2] * B[ln-1]; while (ix1/2 != ix2/2) { if (ix1%2 == 0) { res += d[ix1+1] * B[l]; l += k; } if (ix2%2 == 1) { res += d[ix2-1] * B[r-k]; r -= k; } k *= 2; ix1 /= 2; ix2 /= 2; } return res; } uint Tree::GetRev(int w, int ix, int ln) const { if (ln == 0) return 0; int ix1 = ix + D + stix[w]; if (ln == 1) return dr[ix1]; int ix2 = ix1 + ln - 1; int l = 1, r = ln-1, k = 1; uint res = dr[ix1] * B[ln-1] + dr[ix2]; while (ix1/2 != ix2/2) { if (ix1%2 == 0) { res += dr[ix1+1] * B[r-k]; r -= k; } if (ix2%2 == 1) { res += dr[ix2-1] * B[l]; l += k; } k *= 2; ix1 /= 2; ix2 /= 2; } return res; } void Tree::Set(int w, int ix) { ix += D + stix[w]; int ixr = ix; if (d[ix] == 2) return; d[ix] = 2; int k = 1; while (ix != 1) { ix /= 2; d[ix] = d[ix*2] + d[ix*2+1] * B[k]; k *= 2; } dr[ixr] = 2; k = 1; while (ixr != 1) { ixr /= 2; dr[ixr] = dr[ixr*2] * B[k] + dr[ixr*2+1]; k *= 2; } } struct Problem { int n; VVI c; Tree tree; queue<PII> q; Problem(int n); LL Go(); void Bfs(int a0, int b0); void Process1(int w1, int w2, int p1, int p2, int s, int ln, int d); void Process2(int a, int b); void Visit(int a, int b) { if (a > b) swap(a, b); tree.Set(a*2, b-a); tree.Set(b*2+1, b-a); q.push(PII(a,b)); } }; Problem::Problem(int n) : n(n), c(n) { REP(i,n) c[i].resize(n-i); VI z(n); REP(i,n) { tree.AddWord(n-i); tree.AddWord(i+1); } tree.Create(); } struct Cmp { const VVI& c; Cmp(const VVI& c) : c(c) { } bool operator()(const PII& p1, const PII& p2) const { return c[p1.first][p1.second-p1.first] < c[p2.first][p2.second-p2.first]; } }; LL Problem::Go() { VPII xs; xs.reserve(n*(n+1)/2); REP(i,n) { FOR(j,i,n-1) xs.push_back(PII(i,j)); } sort(ALL(xs),Cmp(c)); LL res = 0; FOREACH(it,xs) { if (tree.Get(2*it->first, it->second-it->first, 1) == 2) continue; res += c[it->first][it->second - it->first]; Bfs(it->first, it->second); } return res; } void Problem::Process1(int w1, int w2, int p1, int p2, int s, int ln, int d) { REP(i,ln) { int b = i, e = ln; while (b < e) { int m = (b+e+1)/2; if (tree.Get(w1, d, m) == tree.Get(w2, 0, m)) b = m; else e = m-1; } if (b == ln) break; if (tree.Get(w2, b, 1) == 1) Visit(p2, p2+b*s); else Visit(p1, p2+b*s); i = b; } } void Problem::Process2(int a, int b) { FOR(i,0,b-a-1) { int x1 = i, x2 = b-a; while (x1 < x2) { int m = (x1+x2+1)/2; if (tree.Get(a*2, 0, m) == tree.GetRev(b*2+1, b-a-m, m)) x1 = m; else x2 = m-1; } if (x1 == b-a) break; if (tree.Get(a*2, x1, 1) == 1) Visit(a, a+x1); else Visit(a+x1+1, b); i = x1; } } void Problem::Bfs(int a0, int b0) { Visit(a0, b0); while (!q.empty()) { int a = q.front().first; int b = q.front().second; q.pop(); if (b+1 < n) { int ln1 = n-a, ln2 = n-(b+1); Process1(a*2, (b+1)*2, a, b+1, 1, ln2, ln1-ln2); } if (a-1 >= 0) { int ln1 = b+1, ln2 = a; Process1(b*2+1, (a-1)*2+1, b, a-1, -1, ln2, ln1-ln2); } if (b-a >= 1) Process2(a, b); } } int main(int argc, char** argv) { int n; scanf("%d", &n); Problem p(n); REP(i,n) { REP(j,n-i) scanf("%d", &p.c[i][j]); } printf("%lld\n", p.Go()); #ifdef _DEBUG system("pause"); #endif return 0; } |