#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <exception> #include <fstream> #include <functional> #include <iostream> #include <map> #include <numeric> #include <regex> #include <set> #include <sstream> #include <stdexcept> #include <string> #include <tuple> #include <typeinfo> #include <vector> #include <unordered_set> #include <unordered_map> #include <bitset> using namespace std; #define rep(i,b) for(auto i=(0);i<(b);++i) #define fo(i,a,b) for(auto i=(a);i<=(b);++i) #define ford(i,a,b) for(auto i=(a);i>=(b);--i) #define fore(a,b) for(auto &a:(b)) #define v vector #define pb push_back #define all(x) (x).begin(),(x).end() #define clr(x,a) memset(x,a,sizeof(x)) template <typename T, size_t N> struct ma : array<T,N> { T& operator[](size_t n) { #ifdef DEBUG assert(n < N); #endif return (*static_cast<array<T,N>*>(this))[n]; } } ; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef ostringstream oss; typedef istringstream iss; template<class T> string to_str(const T &a) { oss os; os << a; return os.str(); } template<> string to_str<ld>(const ld& a) { oss os; os.precision(10); os.setf(ios::fixed); os << a; return os.str(); } template<class T> T from_str(const string &s) { iss is; T val; is >> val; return val; } #define ll long long int cond = (ll)1; #define db(x...) { if (cond > 0) { cond--; rep (xxx, 1) cerr << __LINE__ << " " << #x << " " << x << endl; cerr.flush(); } } typedef array<int, 3> a3; vector<a3> data; typedef unsigned int ull; const int MOD = 32; template<int N> struct bitfield { ull data[(N+256)/MOD]; bitfield() { clr(data, 0); } bool operator[] (int idx) { return data[idx/MOD] & (1LL<<(idx % MOD)); } void set(int idx) { data[idx/MOD] |= (1LL<<(idx % MOD)); } ull next64(int idx) { ull v1 = data[idx/MOD]; ull v2 = data[idx/MOD + 1]; int mod = idx % MOD; ull ret = (v1 >> mod); if (mod != 0) ret |= (v2 << (MOD-mod)); return ret; } }; ma<bitfield<2222>, 2111> a5, b5; int n; void go(int i, int j) { if (a5[i][j]) return; a5[i].set(j); b5[j].set(i); fo (ni, 1, i - 1) { ull g1 = b5[i-1].next64(ni); ull g2 = b5[j].next64(ni); ull xo = (g1 ^ g2); if (xo == 0) { ni += 31; continue; } else if ((xo & 1) == 0) { ni += __builtin_ctz((unsigned int)xo) - 1; continue; } int b1 = g1 & 1; if (b1) go(ni, j); else go(ni, i-1); } // bm fo (nj, j + 1, n) { ull g1 = a5[j+1].next64(nj); ull g2 = a5[i].next64(nj); ull xo = (g1 ^ g2); if (xo == 0) { nj += 31; continue; } else if ((xo & 1) == 0) { nj += __builtin_ctz((unsigned int)xo) - 1; continue; } int b1 = g1 & 1; if (b1) go(i, nj); else go(j + 1, nj); } fo (le, i, j - 1) { ull g1 = a5[i].next64(le); ull g2 = b5[j].next64(le+1); ull xo = (g1 ^ g2); if (xo == 0) { le += 31; continue; } else if ((xo & 1) == 0) { le += __builtin_ctz((unsigned int)xo) - 1; continue; } if (g1&1) go(le+1,j); else go(i, le); } } void solve() { scanf("%d", &n); fo (i, 1, n) { fo (j, i, n) { int val; scanf("%d", &val); a3 tmp({{val, i, j}}); data.pb(tmp); } } sort(all(data)); long long sum = 0; int cnt = 0; fore (it, data) { if (a5[it[1]][it[2]] == 0) { cnt++; //db(it[0]<<" "<<it[1]<<" "<<it[2]); sum += it[0]; if (cnt >= n) break; go(it[1], it[2]); /* fo (i, 1, n) { fo (j, i, n) { if (a[i][j]) cerr << "(" << i << "," << j << ") "; } } cerr << endl; */ } } cout << sum << endl; } int main(int argc, char ** argv) { ios::sync_with_stdio(false); // freopen("../1.in","r",stdin); // freopen("../2.in","r",stdin); // freopen("../3.in","r",stdin); // freopen("../A-small.in","r",stdin); freopen("../A-small.out","w",stdout); // freopen("../A-small-attempt0.in","r",stdin);freopen("../A-small-attempt0.out","w",stdout); // freopen("../A-small-attempt1.in","r",stdin);freopen("../A-small-attempt1.out","w",stdout); // freopen("../A-small-attempt2.in","r",stdin);freopen("../A-small-attempt2.out","w",stdout); // freopen("../A-large.in","r",stdin); freopen("../A-large.out","w",stdout) int t = 1; fo (i, 1, argc - 1) { if (string(argv[i]) == "q") cond = 1000; if (string(argv[i]) == "all") t = 1000; } fo (i, 1, t) { solve(); } 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 | #include <algorithm> #include <array> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <exception> #include <fstream> #include <functional> #include <iostream> #include <map> #include <numeric> #include <regex> #include <set> #include <sstream> #include <stdexcept> #include <string> #include <tuple> #include <typeinfo> #include <vector> #include <unordered_set> #include <unordered_map> #include <bitset> using namespace std; #define rep(i,b) for(auto i=(0);i<(b);++i) #define fo(i,a,b) for(auto i=(a);i<=(b);++i) #define ford(i,a,b) for(auto i=(a);i>=(b);--i) #define fore(a,b) for(auto &a:(b)) #define v vector #define pb push_back #define all(x) (x).begin(),(x).end() #define clr(x,a) memset(x,a,sizeof(x)) template <typename T, size_t N> struct ma : array<T,N> { T& operator[](size_t n) { #ifdef DEBUG assert(n < N); #endif return (*static_cast<array<T,N>*>(this))[n]; } } ; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef ostringstream oss; typedef istringstream iss; template<class T> string to_str(const T &a) { oss os; os << a; return os.str(); } template<> string to_str<ld>(const ld& a) { oss os; os.precision(10); os.setf(ios::fixed); os << a; return os.str(); } template<class T> T from_str(const string &s) { iss is; T val; is >> val; return val; } #define ll long long int cond = (ll)1; #define db(x...) { if (cond > 0) { cond--; rep (xxx, 1) cerr << __LINE__ << " " << #x << " " << x << endl; cerr.flush(); } } typedef array<int, 3> a3; vector<a3> data; typedef unsigned int ull; const int MOD = 32; template<int N> struct bitfield { ull data[(N+256)/MOD]; bitfield() { clr(data, 0); } bool operator[] (int idx) { return data[idx/MOD] & (1LL<<(idx % MOD)); } void set(int idx) { data[idx/MOD] |= (1LL<<(idx % MOD)); } ull next64(int idx) { ull v1 = data[idx/MOD]; ull v2 = data[idx/MOD + 1]; int mod = idx % MOD; ull ret = (v1 >> mod); if (mod != 0) ret |= (v2 << (MOD-mod)); return ret; } }; ma<bitfield<2222>, 2111> a5, b5; int n; void go(int i, int j) { if (a5[i][j]) return; a5[i].set(j); b5[j].set(i); fo (ni, 1, i - 1) { ull g1 = b5[i-1].next64(ni); ull g2 = b5[j].next64(ni); ull xo = (g1 ^ g2); if (xo == 0) { ni += 31; continue; } else if ((xo & 1) == 0) { ni += __builtin_ctz((unsigned int)xo) - 1; continue; } int b1 = g1 & 1; if (b1) go(ni, j); else go(ni, i-1); } // bm fo (nj, j + 1, n) { ull g1 = a5[j+1].next64(nj); ull g2 = a5[i].next64(nj); ull xo = (g1 ^ g2); if (xo == 0) { nj += 31; continue; } else if ((xo & 1) == 0) { nj += __builtin_ctz((unsigned int)xo) - 1; continue; } int b1 = g1 & 1; if (b1) go(i, nj); else go(j + 1, nj); } fo (le, i, j - 1) { ull g1 = a5[i].next64(le); ull g2 = b5[j].next64(le+1); ull xo = (g1 ^ g2); if (xo == 0) { le += 31; continue; } else if ((xo & 1) == 0) { le += __builtin_ctz((unsigned int)xo) - 1; continue; } if (g1&1) go(le+1,j); else go(i, le); } } void solve() { scanf("%d", &n); fo (i, 1, n) { fo (j, i, n) { int val; scanf("%d", &val); a3 tmp({{val, i, j}}); data.pb(tmp); } } sort(all(data)); long long sum = 0; int cnt = 0; fore (it, data) { if (a5[it[1]][it[2]] == 0) { cnt++; //db(it[0]<<" "<<it[1]<<" "<<it[2]); sum += it[0]; if (cnt >= n) break; go(it[1], it[2]); /* fo (i, 1, n) { fo (j, i, n) { if (a[i][j]) cerr << "(" << i << "," << j << ") "; } } cerr << endl; */ } } cout << sum << endl; } int main(int argc, char ** argv) { ios::sync_with_stdio(false); // freopen("../1.in","r",stdin); // freopen("../2.in","r",stdin); // freopen("../3.in","r",stdin); // freopen("../A-small.in","r",stdin); freopen("../A-small.out","w",stdout); // freopen("../A-small-attempt0.in","r",stdin);freopen("../A-small-attempt0.out","w",stdout); // freopen("../A-small-attempt1.in","r",stdin);freopen("../A-small-attempt1.out","w",stdout); // freopen("../A-small-attempt2.in","r",stdin);freopen("../A-small-attempt2.out","w",stdout); // freopen("../A-large.in","r",stdin); freopen("../A-large.out","w",stdout) int t = 1; fo (i, 1, argc - 1) { if (string(argv[i]) == "q") cond = 1000; if (string(argv[i]) == "all") t = 1000; } fo (i, 1, t) { solve(); } return 0; } |