group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_1465_cpp
Scheduling Two Meetings You are the chief judge of the International Collegiate Quiz Contest (ICQC) this year. You want to hold judge meetings twice for preparing the problem set of the contest. You proposed candidate schedules for the meetings, and all the judges answered for each of the time slots whether they would attend the meeting on-site or remotely via a video conference tool. You have to choose a pair of two distinct time slots, so that every judge attends at least one of the two meetings on-site. When there are multiple such pairs, you’d like to choose one with the largest number of judges attending both meetings on-site. If multiple pairs are still equally desirable with these criteria, one with the earlier first meeting is preferred. If there still remain multiple pairs with the same time slot for the first meeting, the one with the earliest second meeting should be chosen. Input The input consists of a single test case of the following format. $n$ $m$ $a_{1,1}$ ... $a_{1,m}$ $\vdots$ $a_{n,1}$ ... $a_{n,m}$ Two integers $n$ and $m$ are given in the first line. The first integer $n$ ($2 \leq n \leq 2 \times 10^5$) is the number of candidate time slots. Here, the candidate time slots are numbered 1 through $n$, and smaller numbers mean earlier time slots. The second integer $m$ ($2 \leq m \leq 20$) is the number of judges. In the following $n$ lines, $a_{i,j}$ is either a character Y indicating that the $j$-th judge attends a meeting at the $i$-th candidate time slot on-site, or a character N indicating remote attendance. Output Output a line containing the two time slot numbers of the most preferable choice, separated by a space, with the earlier time slot first. If there are no pairs of time slots satisfying the condition, output No . Sample Input 1 4 3 NNY YYN YNY NYY Sample Output 1 2 3 Sample Input 2 3 6 NNNYYY YYNYYN YYYNNN Sample Output 2 1 3 Sample Input 3 6 5 NNNNN YNNNY YYNNN YYNNN NYYNY NNYYY Sample Output 3 3 6 Sample Input 4 3 3 YNN NYN NNY Sample Output 4 No Sample Input 5 4 4 NYNN YNYY YNYN NNYY Sample Output 5 1 2
[ { "submission_id": "aoj_1465_11055654", "code_snippet": "#include <bits/stdc++.h>\n#include <numeric>\nusing namespace std;\n\nusing i64 = int64_t;\n\n#define rep(i, n) for(i64 i = 0; i < (n); i++)\n#define each(x, a) for(auto& x: a)\n\ntemplate<class T, T id, T (*op)(T,T)>\nstruct SegTree{\n\ti64 n=1;\n\tvector<T> k;\n\tpublic:\n\tvoid update(int i, T x) {\n\t\ti += n;\n\t\tk[i] = op(k[i], x);\n\t\twhile(1<i){\n\t\t\ti/=2;\n\t\t\tk[i] = op(k[i*2], k[i*2+1]);\n\t\t}\n\n\t};\n\tT query(i64 l, i64 r) {\n\t\tT x = id;\n\t\tl+=n;\n\t\tr+=n;\n\t\twhile(l<r) {\n\t\t if (l&1) x=op(x,k[l++]);\n\t\t if (r&1) x=op(x,k[--r]);\n\t\t l>>=1;\n\t\t r>>=1;\n\t\t}\n\n\t\treturn x;\n\t};\n\tSegTree(std::vector<T> a) {\n\t\tint m = a.size();\n\t\twhile(n<=m+1)n*=2;\n\t\tthis->k=vector<T>(n*2, id);\n\t\trep(i,m) k[n+i]=a[i];\n\t\tfor(i64 i=n-1;0<i;i--) k[i] = op(k[i*2], k[i*2+1]);\n\t}\n};\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::vector<T> &v) {\n\tos << \"[\";\n\tfor (i64 i = 0; i << v.size(); ++i) {\n\t\tif (i == 0) {\n\t\t\tos << \", \";\n\t\t}\n\t\tos << v[i];\n\t}\n\tos << \"]\";\n\treturn os;\n}\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::unordered_set<T> &v) {\n\tos << \"{\";\n\tfor (i64 i = 0; i << v.size(); ++i) {\n\t\tif (i == 0) {\n\t\t\tos << \", \";\n\t\t}\n\t\tos << v[i];\n\t}\n\tos << \"}\";\n\treturn os;\n}\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::set<T> &v) {\n\tos << \"{\";\n\tfor (i64 i = 0; i << v.size(); ++i) {\n\t\tif (i == 0) {\n\t\t\tos << \", \";\n\t\t}\n\t\tos << v[i];\n\t}\n\tos << \"}\";\n\treturn os;\n}\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, const std::pair<T, T> &v) {\n\tos << \"(\";\n\tos << v.first;\n\tos << \", \";\n\tos << v.second;\n\tos << \")\";\n\treturn os;\n}\n\n#define dbg(x) ([&]() { auto _dbg_x = (x); cerr << \"[dbg L\" << __LINE__ << \"] \" << #x << \" = \"; cerr << _dbg_x << endl; return _dbg_x; }())\n\nusing ll=long long;\nvoid chmin(int& x,int y) {if(x>y) x=y;}\nusing pii=pair<int,int>;\n\nint main(){\n\tint n,m;\n\tcin >> n >> m;\n\tint f=(1<<m)-1,count=0;\n\n\tint inf=1234567890;\n\tvector<int> in(n);\n\tvector<vector<int>> a((1<<m),vector<int>(m+1,inf));\n\trep(i,n){\n\t\tstring s;\n\t\tcin >> s;\n\t\trep(j,m) {\n\t\t\tin[i] *= 2;\n\t\t\tif (s[j] == 'Y') in[i]++;\n\t\t}\n\t\tif(in[i]==f) count++;\n\t\tchmin(a[in[i]][0],i);\n\t}\n\n\tif(count){\n\t\tvector<pii> p(n);\n\t\trep(i,n) p[i]=pii(__popcount(f^in[i]),i);\n\t\tsort(p.begin(),p.end());\n\t\tint x=p[0].second+1,y=p[1].second+1;\n\t\tif(x>y) swap(x,y);\n\t\tcout << x << \" \" << y << endl;\n\t\treturn 0;\n\t}\n\n\tfor(int i=(1<<m)-1;0<=i;i--) rep(l,m) rep(j,m) if((i>>j)&1){\n\t\tchmin(a[i^(1<<j)][l+1],a[i][l]);\n\t}\n\n\tpii ans=pii(inf,inf);\n\tint nowmax=0;\n\trep(i,n){\n\t\trep(j,m+1) if(a[f^in[i]][j]<inf){\n\t\t\tint x=i,y=a[f^in[i]][j];\n\t\t\tif(x>y) swap(x,y);\n\t\t\tif(nowmax<=j){\n\t\t\t\tif(nowmax<j||x<ans.first||(x==ans.first&&y<ans.second)) ans=pii(x,y);\n\t\t\t\tnowmax=j;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(ans==pii(inf,inf)) cout << \"No\" << endl;\n\telse cout << ans.first+1 << \" \" << ans.second+1 << endl;\n\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 126960, "score_of_the_acc": -2, "final_rank": 8 }, { "submission_id": "aoj_1465_11053285", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\ntemplate<class T> void out(T a) { cout << a << endl; }\n\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pll = pair<ll, ll>;\nusing vpll = vector<pll>;\nusing ull = unsigned long long;\n\nint main()\n{\n unordered_map<ll, ll> mns;\n ll N, M; cin >> N >> M;\n // vll S(N);\n ll mx = 0;\n pll ans = {INF, INF};\n vvll cnts(M + 1);\n rep(i, N)\n {\n string s; cin >> s;\n ll val = 0;\n ll cnt = 0;\n for(auto c : s)\n {\n val *= 2;\n if(c == 'Y')\n {\n val++;\n cnt++;\n }\n }\n // S[i] = val;\n // cout << val << endl;\n if(!mns.count(val))\n {\n mns[val] = i;\n cnts[cnt].emplace_back(val);\n // cnts[M - cnt].emplace_back(((1LL << M) - 1) ^ val);\n }\n else if(val == ((1LL << M) - 1))\n {\n if(chmax(mx, 2*M))\n {\n // cout << mns[val] << \" \" << i << endl;\n ans = {mns[val], i};\n }\n }\n }\n if(mx == 2*M)\n {\n // cout << \"test\" << endl;\n cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n return 0;\n }\n // for(auto &vec : cnts)\n // {\n // sort(all(vec));\n // vec.erase(unique(all(vec)), vec.end());\n // }\n unordered_map<ll, pll> mxs;\n ll B = M/2;\n for(ll cnt = M; cnt >= B; cnt--)\n {\n sort(all(cnts[cnt]));\n cnts[cnt].erase(unique(all(cnts[cnt])), cnts[cnt].end());\n for(auto val : cnts[cnt])\n {\n if(mns.count(val))\n {\n if(!mxs.count(val)) mxs[val] = {cnt, mns[val]};\n }\n // if(!mxs.count(val)) continue;\n auto [c, id] = mxs[val];\n rep(i, M)\n {\n if((val >> i) & 1)\n {\n ll nval = val ^ (1LL << i);\n if(mxs.count(nval))\n {\n if(mxs[nval].first < c || (mxs[nval].first == c && mxs[nval].second > id)) mxs[nval] = {c, id};\n }\n else\n {\n mxs[nval] = {c, id};\n cnts[cnt - 1].emplace_back(nval);\n }\n }\n }\n }\n }\n unordered_map<ll, pll> memo;\n function<pll(ll)> dfs = [&](ll val)->pll\n {\n ll cnt = __popcount((ull)val);\n if(cnt >= B - 1)\n {\n if(mxs.count(val)) return mxs[val];\n else return {-1, INF};\n }\n if(memo.count(val)) return memo[val];\n pll ret = {-1, INF};\n if(mns.count(val))\n {\n ret = {cnt, mns[val]};\n }\n rep(i, M)\n {\n if(!((val >> i) & 1))\n {\n ll nval = val | (1LL << i);\n auto [c, id] = dfs(nval);\n if(ret.first < c || (ret.first == c || ret.second > id)) ret = {c, id};\n }\n }\n return memo[val] = ret;\n };\n for(auto [b1, i1] : mns)\n {\n ll cnt = __popcount((ull)b1);\n ll cov = ((1LL << M) - 1) ^ b1;\n auto [c, i2] = dfs(cov);\n if(i2 == INF || i1 == i2) continue;\n if(mx < cnt + c || (mx == cnt + c && ans > pll{minmax(i1, i2)}))\n {\n mx = cnt + c;\n ans = minmax(i1, i2);\n }\n }\n if(ans.first == INF) cout << \"No\" << endl;\n else cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 80880, "score_of_the_acc": -1.2443, "final_rank": 7 }, { "submission_id": "aoj_1465_11053232", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\ntemplate<class T> void out(T a) { cout << a << endl; }\n\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pll = pair<ll, ll>;\nusing vpll = vector<pll>;\nusing ull = unsigned long long;\nint main()\n{\n unordered_map<ll, ll> mns;\n ll N, M; cin >> N >> M;\n // vll S(N);\n ll mx = 0;\n pll ans = {INF, INF};\n vvll cnts(M + 1);\n rep(i, N)\n {\n string s; cin >> s;\n ll val = 0;\n ll cnt = 0;\n for(auto c : s)\n {\n val *= 2;\n if(c == 'Y')\n {\n val++;\n cnt++;\n }\n }\n // S[i] = val;\n if(!mns.count(val))\n {\n mns[val] = i;\n cnts[cnt].emplace_back(val);\n // cnts[M - cnt].emplace_back(((1LL << M) - 1) ^ val);\n }\n else if(val == ((1LL << M) - 1))\n {\n if(chmax(mx, 2*M))\n {\n ans = {mns[val], i};\n }\n }\n }\n if(mx == 2*M)\n {\n cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n return 0;\n }\n for(auto &vec : cnts)\n {\n sort(all(vec));\n vec.erase(unique(all(vec)), vec.end());\n }\n unordered_map<ll, pll> mxs;\n ll B = M/2;\n for(ll cnt = M; cnt >= B; cnt--)\n {\n // sort(all(cnts[cnt]));\n // cnts[cnt].erase(unique(all(cnts[cnt])), cnts[cnt].end());\n for(auto val : cnts[cnt])\n {\n if(mns.count(val))\n {\n if(!mxs.count(val)) mxs[val] = {cnt, mns[val]};\n }\n if(!mxs.count(val)) continue;\n auto [c, id] = mxs[val];\n rep(i, M)\n {\n if((val >> i) & 1)\n {\n ll nval = val ^ (1LL << i);\n if(mxs.count(nval))\n {\n if(mxs[nval].first < c || (mxs[nval].first == c && mxs[nval].second > id)) mxs[nval] = {c, id};\n }\n else\n {\n mxs[nval] = {c, id};\n cnts[cnt - 1].emplace_back(nval);\n }\n }\n }\n }\n }\n unordered_map<ll, pll> memo;\n function<pll(ll)> dfs = [&](ll val)->pll\n {\n ll cnt = __popcount((ull)val);\n if(cnt >= B)\n {\n if(mxs.count(val)) return mxs[val];\n else return {-1, INF};\n }\n if(memo.count(val)) return memo[val];\n pll ret = {-1, INF};\n if(mns.count(val))\n {\n ret = {cnt, mns[val]};\n }\n rep(i, M)\n {\n if(!((val >> i) & 1))\n {\n ll nval = val | (1LL << i);\n auto [c, id] = dfs(nval);\n if(ret.first < c || (ret.first == c || ret.second > id)) ret = {c, id};\n }\n }\n return memo[val] = ret;\n };\n for(auto [b1, i1] : mns)\n {\n ll cnt = __popcount((ull)b1);\n ll cov = ((1LL << M) - 1) ^ b1;\n auto [c, i2] = dfs(cov);\n if(i2 == INF) continue;\n if(mx < cnt + c || (mx == cnt + c && ans > pll{minmax(i1, i2)}))\n {\n mx = cnt + c;\n ans = minmax(i1, i2);\n }\n }\n if(ans.first == INF) cout << \"No\" << endl;\n else cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 0.1282051282051282, "time_ms": 10, "memory_kb": 5376, "score_of_the_acc": -0.0094, "final_rank": 14 }, { "submission_id": "aoj_1465_11053022", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\ntemplate<class T> void out(T a) { cout << a << endl; }\n\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pll = pair<ll, ll>;\nusing vpll = vector<pll>;\nusing ull = unsigned long long;\nint main()\n{\n unordered_map<ll, pll> mns;\n ll N, M; cin >> N >> M;\n // vll S(N);\n rep(i, N)\n {\n string s; cin >> s;\n ll val = 0;\n for(auto c : s)\n {\n val *= 2;\n if(c == 'Y') val++;\n }\n // S[i] = val;\n if(!mns.count(val)) mns[val] = {i, INF};\n else if(mns[val].second == INF) mns[val].second = i;\n }\n // for(auto [key, val] : mns) cout << key << \" \" << val << endl;\n unordered_map<ll, pll> cov;\n ll B = M/2;\n for(auto [b1, pi1] : mns)\n {\n ll cnt = 0;\n vll bb;\n rep(i, M)\n {\n if((b1 >> i) & 1)\n {\n bb.emplace_back(i);\n cnt++;\n }\n }\n vll P(bb.size(), 0);\n for(ll k = 0; k <= min((ll)bb.size(), B); k++)\n {\n vll P(bb.size(), 0);\n rep(i, k) P[bb.size() - i - 1] = 1;\n do\n {\n ll num = 0;\n rep(i, P.size())\n {\n if(!P[i]) continue;\n num |= 1LL << bb[i];\n }\n if(!cov.count(num)) cov[num] = {cnt, pi1.first};\n else if(cov[num].first < cnt || (cov[num].first == cnt && cov[num].second > pi1.first))\n {\n cov[num] = {cnt, pi1.first};\n }\n } while (next_permutation(all(P)));\n }\n }\n ll mx = 0;\n pll ans = {INF, INF};\n for(auto [b1, pi1] : mns)\n {\n ll i1 = pi1.first;\n ll base = 0;\n ll cnt = 0;\n rep(i, M)\n {\n if(!((b1 >> i) & 1)) base |= 1LL << i;\n else cnt++;\n }\n if(__popcount((ull)base) <= B)\n {\n if(!cov.count(base)) continue;\n auto [pls, i2] = cov[base];\n if(mx < cnt + pls || (mx == cnt + pls && ans > pll{minmax(i1, i2)}))\n {\n mx = cnt + pls;\n ans = minmax(i1, i2);\n }\n continue;\n }\n ll b2 = b1;\n if(mns.count(b2 | base))\n {\n ll i2 = mns[b2 | base].first;\n if(i1 == i2) i2 = mns[b2 | base].second;\n if(i2 != INF)\n {\n ll tmp = cnt + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n while(b2 > 0)\n {\n // cout << b1 << \" \" << b2 << endl;\n b2 = (b2 - 1) & b1;\n if(!mns.count(b2 | base)) continue;\n ll i2 = mns[b2 | base].first;\n if(i1 == i2) i2 = mns[b2 | base].second;\n if(i2 == INF) continue;\n ll tmp = cnt + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n if(ans.first == INF) cout << \"No\" << endl;\n else cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 0.1282051282051282, "time_ms": 210, "memory_kb": 4864, "score_of_the_acc": -0.2869, "final_rank": 20 }, { "submission_id": "aoj_1465_11053016", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\ntemplate<class T> void out(T a) { cout << a << endl; }\n\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pll = pair<ll, ll>;\nusing vpll = vector<pll>;\nusing ull = unsigned long long;\nint main()\n{\n unordered_map<ll, pll> mns;\n ll N, M; cin >> N >> M;\n // vll S(N);\n rep(i, N)\n {\n string s; cin >> s;\n ll val = 0;\n for(auto c : s)\n {\n val *= 2;\n if(c == 'Y') val++;\n }\n // S[i] = val;\n if(!mns.count(val)) mns[val] = {i, INF};\n else if(mns[val].second == INF) mns[val].second = i;\n }\n // for(auto [key, val] : mns) cout << key << \" \" << val << endl;\n unordered_map<ll, pll> cov;\n ll B = M/2;\n for(auto [b1, pi1] : mns)\n {\n ll cnt = 0;\n vll bb;\n rep(i, M)\n {\n if((b1 >> i) & 1)\n {\n bb.emplace_back(i);\n cnt++;\n }\n }\n vll P(bb.size(), 0);\n for(ll k = 0; k <= min((ll)bb.size(), B); k++)\n {\n vll P(bb.size(), 0);\n rep(i, k) P[bb.size() - i - 1] = 1;\n do\n {\n ll num = 0;\n rep(i, P.size())\n {\n if(!P[i]) continue;\n num |= 1LL << bb[i];\n }\n if(!cov.count(num)) cov[num] = {cnt, pi1.first};\n else chmin(cov[num], pll{cnt, pi1.first});\n } while (next_permutation(all(P)));\n }\n }\n ll mx = 0;\n pll ans = {INF, INF};\n for(auto [b1, pi1] : mns)\n {\n ll i1 = pi1.first;\n ll base = 0;\n ll cnt = 0;\n rep(i, M)\n {\n if(!((b1 >> i) & 1)) base |= 1LL << i;\n else cnt++;\n }\n if(__popcount((ull)base) <= B)\n {\n if(!cov.count(base)) continue;\n auto [pls, i2] = cov[base];\n if(mx < cnt + pls || (mx == cnt + pls && ans > pll{minmax(i1, i2)}))\n {\n mx = cnt + pls;\n ans = minmax(i1, i2);\n }\n continue;\n }\n ll b2 = b1;\n if(mns.count(b2 | base))\n {\n ll i2 = mns[b2 | base].first;\n if(i1 == i2) i2 = mns[b2 | base].second;\n if(i2 != INF)\n {\n ll tmp = cnt + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n while(b2 > 0)\n {\n // cout << b1 << \" \" << b2 << endl;\n b2 = (b2 - 1) & b1;\n if(!mns.count(b2 | base)) continue;\n ll i2 = mns[b2 | base].first;\n if(i1 == i2) i2 = mns[b2 | base].second;\n if(i2 == INF) continue;\n ll tmp = cnt + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n if(ans.first == INF) cout << \"No\" << endl;\n else cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 0.1282051282051282, "time_ms": 200, "memory_kb": 4864, "score_of_the_acc": -0.2728, "final_rank": 19 }, { "submission_id": "aoj_1465_11052582", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\ntemplate<class T> void out(T a) { cout << a << endl; }\n\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pll = pair<ll, ll>;\nusing vpll = vector<pll>;\nusing ull = unsigned long long;\nint main()\n{\n unordered_map<ll, pll> mns;\n ll N, M; cin >> N >> M;\n // vll S(N);\n rep(i, N)\n {\n string s; cin >> s;\n ll val = 0;\n for(auto c : s)\n {\n val *= 2;\n if(c == 'Y') val++;\n }\n // S[i] = val;\n if(!mns.count(val)) mns[val] = {i, INF};\n else if(mns[val].second == INF) mns[val].second = i;\n }\n // for(auto [key, val] : mns) cout << key << \" \" << val << endl;\n ll mx = 0;\n pll ans = {INF, INF};\n rep(b1, 1LL << M)\n {\n if(!mns.count(b1)) continue;\n ll i1 = mns[b1].first;\n ll base = 0;\n rep(i, M) if(!((b1 >> i) & 1)) base |= 1LL << i;\n ll b2 = b1;\n if(mns.count(b2 | base))\n {\n ll i2 = mns[b2 | base].first;\n if(i1 == i2) i2 == mns[b2 | base].second;\n if(i2 != INF)\n {\n ll tmp = __popcount((ull)b1) + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n while(b2 > 0)\n {\n // cout << b1 << \" \" << b2 << endl;\n b2 = (b2 - 1) & b1;\n if(!mns.count(b2 | base)) continue;\n ll i2 = mns[b2 | base].first;\n if(i1 == i2) i2 = mns[b2 | base].second;\n if(i2 == INF) continue;\n ll tmp = __popcount((ull)b1) + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n if(ans.first == INF) cout << \"No\" << endl;\n else cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 0.1282051282051282, "time_ms": 20, "memory_kb": 4224, "score_of_the_acc": -0.0141, "final_rank": 15 }, { "submission_id": "aoj_1465_11052527", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\ntemplate<class T> void out(T a) { cout << a << endl; }\n\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pll = pair<ll, ll>;\nusing vpll = vector<pll>;\nusing ull = unsigned long long;\nint main()\n{\n unordered_map<ll, ll> mns;\n ll N, M; cin >> N >> M;\n // vll S(N);\n rep(i, N)\n {\n string s; cin >> s;\n ll val = 0;\n for(auto c : s)\n {\n val *= 2;\n if(c == 'Y') val++;\n }\n // S[i] = val;\n if(!mns.count(val)) mns[val] = i;\n }\n // for(auto [key, val] : mns) cout << key << \" \" << val << endl;\n ll mx = 0;\n pll ans = {INF, INF};\n rep(b1, 1LL << M)\n {\n if(!mns.count(b1)) continue;\n ll i1 = mns[b1];\n ll base = 0;\n rep(i, M) if(!((b1 >> i) & 1)) base |= 1LL << i;\n ll b2 = b1;\n if(mns.count(b2 | base))\n {\n ll i2 = mns[b2 | base];\n if(i1 != i2)\n {\n ll tmp = __popcount((ull)b1) + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n while(b2 > 0)\n {\n // cout << b1 << \" \" << b2 << endl;\n b2 = (b2 - 1) & b1;\n if(!mns.count(b2 | base)) continue;\n ll i2 = mns[b2 | base];\n if(i1 == i2) continue;\n ll tmp = __popcount((ull)b1) + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n if(ans.first == INF) cout << \"No\" << endl;\n else cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 0.28205128205128205, "time_ms": 260, "memory_kb": 6024, "score_of_the_acc": -0.3668, "final_rank": 12 }, { "submission_id": "aoj_1465_11052512", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\ntemplate<class T> void out(T a) { cout << a << endl; }\n\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pll = pair<ll, ll>;\nusing vpll = vector<pll>;\nusing ull = unsigned long long;\nint main()\n{\n unordered_map<ll, ll> mns;\n ll N, M; cin >> N >> M;\n // vll S(N);\n rep(i, N)\n {\n string s; cin >> s;\n ll val = 0;\n for(auto c : s)\n {\n val *= 2;\n if(c == 'Y') val++;\n }\n // S[i] = val;\n if(!mns.count(val)) mns[val] = i;\n }\n // for(auto [key, val] : mns) cout << key << \" \" << val << endl;\n ll mx = 0;\n pll ans = {INF, INF};\n rep(b1, 1LL << M)\n {\n if(!mns.count(b1)) continue;\n ll i1 = mns[b1];\n ll base = 0;\n rep(i, M) if(!((b1 >> i) & 1)) base |= 1LL << i;\n ll b2 = b1;\n if(mns.count(b2 | base))\n {\n ll i2 = mns[b2 | base];\n if(i1 != i1)\n {\n ll tmp = __popcount((ull)b1) + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n while(b2 > 0)\n {\n // cout << b1 << \" \" << b2 << endl;\n b2 = (b2 - 1) & b1;\n if(!mns.count(b2 | base)) continue;\n ll i2 = mns[b2 | base];\n if(i1 == i2) continue;\n ll tmp = __popcount((ull)b1) + __popcount((ull)(b2 | base));\n if(mx < tmp || (mx == tmp && ans > pll{minmax(i1, i2)}))\n {\n mx = tmp;\n ans = minmax(i1, i2);\n }\n }\n }\n if(ans.first == INF) cout << \"No\" << endl;\n else cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 0.28205128205128205, "time_ms": 250, "memory_kb": 6024, "score_of_the_acc": -0.3527, "final_rank": 11 }, { "submission_id": "aoj_1465_11050135", "code_snippet": "#include <iostream>\n#include <iomanip>//小数点出力用\n//cout << fixed << setprecision(10) << ans;\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <unordered_map>\nusing ll = long long;\nusing namespace std;\n#define modPHash (ll)((1LL<<61)-1)\n#define modP (ll)998244353\nbool chkrng0idx(int pos, int sup) { return (0 <= pos && pos < sup); }\nint clk4(int num) { return (num - 2) * (num % 2); }\nvoid yn(bool tf) { cout << (tf ? \"YES\\n\" : \"NO\\n\"); }\n\nint pc(int x) {\n int res = 0;\n while (x) {\n if (x & 1) {\n res++;\n }\n x >>= 1;\n }\n return res;\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int N;\n cin >> N;\n int M;\n cin >> M;\n string S[200002];\n int P[200002] = { 0 };\n vector<int>Full;\n pair<int,int> seg[1 << 20];\n for (int i = 0;i < (1 << 20);i++) {\n seg[i] = { -1,1e9 };\n }\n for (int i = 0;i < N;i++) {\n cin >> S[i];\n for (int j = 0;j < M;j++) {\n if (S[i][j] == 'Y')P[i] += (1 << j);\n }\n if (P[i] == (1 << M) - 1) {\n Full.push_back(i + 1);\n }\n if (seg[P[i]].first == -1) {\n seg[P[i]] = { pc(P[i]),i + 1};\n }\n }\n if (Full.size() >= 2) {\n cout << Full[0] << \" \" << Full[1] <<\"\\n\";\n return 0;\n }\n for (int i = (1 << M) - 2;i >= 0;i--) {\n for (int j = 0;j < M;j++) {\n if ((i >> j) & 1)continue;\n int k = i + (1 << j);\n if (seg[i].first < seg[k].first) {\n seg[i] = seg[k];\n }\n else if (seg[i].first == seg[k].first && seg[i].second > seg[k].second) {\n seg[i] = seg[k];\n }\n }\n }\n int best = -1;\n int day1, day2;\n for (int i = 0;i < N;i++) {\n if (P[i] == (1 << M) - 1) continue;\n int R = (1 << M) - 1 - P[i];\n if (seg[R].first == -1) continue;\n int score = pc(P[i]) + seg[R].first;\n int d1 = min(seg[R].second, i + 1);\n int d2 = max(seg[R].second, i + 1);\n if (best < score) {\n best = score;\n day1 = d1;\n day2 = d2; \n }\n else if (best == score && d1 < day1) {\n best = score;\n day1 = d1;\n day2 = d2;\n }\n else if (best == score && d1 == day1 && d2 < day2) {\n best = score;\n day1 = d1;\n day2 = d2;\n }\n }\n if (best == -1) {\n cout << \"No\\n\";\n }\n else {\n cout << day1 << \" \" << day2 << \"\\n\";\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 28056, "score_of_the_acc": -0.3068, "final_rank": 4 }, { "submission_id": "aoj_1465_11019765", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<ll> VLL;\n\n#define rep(i, b) for(ll i=0; i<(b); ++i)\n#define all(x) begin(x),end(x)\n\nstruct Data {\n ll one_count;\n ll index;\n};\n\nvoid chmax(Data &a, Data &b) {\n if (a.one_count < b.one_count) {\n a = b;\n } else if (a.one_count == b.one_count && a.index > b.index) {\n a = b;\n }\n}\n\nstruct AnsData {\n ll one_count;\n ll index0, index1;\n\n AnsData(ll a, ll b, ll c) {\n one_count = a;\n index0 = min(b, c);\n index1 = max(b, c);\n }\n};\n\nvoid chmax(AnsData &a, AnsData &b) {\n if (a.one_count < b.one_count) {\n a = b;\n } else if (a.one_count == b.one_count) {\n if (a.index0 > b.index0) {\n a = b;\n } else if (a.index0 == b.index0 && a.index1 > b.index1) {\n a = b;\n }\n }\n}\n\nint main() {\n ll n, m;\n cin >> n >> m;\n vector<bitset<20> > a(n, (1 << 20) - 1);\n rep(i, n) {\n string s;\n cin >> s;\n rep(j, m) {\n if (s[j] == 'N') a[i].set(j, 0);\n }\n }\n VLL y_count(n);\n rep(i, n) y_count[i] = a[i].count() - (20 - m);\n\n // coner\n {\n VLL cpy = y_count;\n sort(all(cpy));\n if (cpy[n - 1] == m && cpy[n - 2] != m) {\n ll all_i = find(all(y_count), m) - y_count.begin();\n AnsData ans(-1, -1, -1);\n rep(i, n) {\n if (i == all_i) continue;\n AnsData tmp(m + y_count[i], i, all_i);\n chmax(ans, tmp);\n }\n cout << ans.index0 + 1 << \" \" << ans.index1 + 1 << endl;\n return 0;\n }\n }\n\n vector<Data> dp(1 << 20, {-1, -1});\n\n rep(i, n) {\n Data tmp = {y_count[i], i};\n chmax(dp[a[i].to_ulong()], tmp);\n }\n\n rep(i, 20) {\n ll k = (1 << i);\n rep(j, 1<<19) {\n ll j0 = j / k;\n ll j1 = j % k;\n ll j2 = (j0 << (i + 1)) + j1;\n chmax(dp[j2], dp[j2 + k]);\n }\n }\n //cout << dp[0].one_count << \" \" << dp[0].index << endl;\n AnsData ans(-1, -1, -1);\n rep(i, n) {\n Data flip = dp[a[i].flip().to_ulong()];\n if (flip.one_count == -1) continue;\n if (flip.index == i) continue;\n AnsData tmp(flip.one_count + y_count[i], i, flip.index);\n chmax(ans, tmp);\n }\n if (ans.one_count == -1) cout << \"No\" << endl;\n else\n cout << ans.index0 + 1 << \" \" << ans.index1 + 1 << endl;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 22512, "score_of_the_acc": -0.4166, "final_rank": 6 }, { "submission_id": "aoj_1465_11019740", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<ll> VLL;\n\n#define rep(i, b) for(ll i=0; i<(b); ++i)\n#define all(x) begin(x),end(x)\n\nstruct Data {\n ll one_count;\n ll index;\n};\n\nvoid chmax(Data &a, Data &b) {\n if (a.one_count < b.one_count) {\n a = b;\n } else if (a.one_count == b.one_count && a.index > b.index) {\n a = b;\n }\n}\n\nstruct AnsData {\n ll one_count;\n ll index0, index1;\n\n AnsData(ll a, ll b, ll c) {\n one_count = a;\n index0 = min(b, c);\n index1 = max(b, c);\n }\n};\n\nvoid chmax(AnsData &a, AnsData &b) {\n if (a.one_count < b.one_count) {\n a = b;\n } else if (a.one_count == b.one_count) {\n if (a.index0 > b.index0) {\n a = b;\n } else if (a.index0 == b.index0 && a.index1 > b.index1) {\n a = b;\n }\n }\n}\n\nint main() {\n ll n, m;\n cin >> n >> m;\n vector<bitset<20> > a(n, (1 << 20) - 1);\n rep(i, n) {\n string s;\n cin >> s;\n rep(j, m) {\n if (s[j] == 'N') a[i].set(j, 0);\n }\n }\n VLL y_count(n);\n rep(i, n) y_count[i] = a[i].count() - (20 - m);\n\n // coner\n {\n VLL cpy = y_count;\n sort(all(cpy));\n if (cpy[n - 1] == m && cpy[n - 2] != m) {\n ll all_i = find(all(y_count), m) - y_count.begin();\n AnsData ans(-1, -1, -1);\n rep(i, n) {\n if (i == all_i) continue;\n AnsData tmp(m + y_count[i], i, all_i);\n chmax(ans, tmp);\n }\n cout << ans.index0 << \" \" << ans.index1 << endl;\n return 0;\n }\n }\n\n vector<Data> dp(1 << 20, {-1, -1});\n\n rep(i, n) {\n Data tmp = {y_count[i], i};\n chmax(dp[a[i].to_ulong()], tmp);\n }\n\n rep(i, 20) {\n ll k = (1 << i);\n rep(j, 1<<19) {\n ll j0 = j / k;\n ll j1 = j % k;\n ll j2 = (j0 << (i + 1)) + j1;\n chmax(dp[j2], dp[j2 + k]);\n }\n }\n //cout << dp[0].one_count << \" \" << dp[0].index << endl;\n AnsData ans(-1, -1, -1);\n rep(i, n) {\n Data flip = dp[a[i].flip().to_ulong()];\n if (flip.one_count == -1) continue;\n if (flip.index == i) continue;\n AnsData tmp(flip.one_count + y_count[i], i, flip.index);\n chmax(ans, tmp);\n }\n if (ans.one_count == -1) cout << \"No\" << endl;\n else\n cout << ans.index0 + 1 << \" \" << ans.index1 + 1 << endl;\n}", "accuracy": 0.1282051282051282, "time_ms": 70, "memory_kb": 19448, "score_of_the_acc": -0.2085, "final_rank": 17 }, { "submission_id": "aoj_1465_11018921", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<ll> VLL;\n\n#define rep(i, b) for(ll i=0; i<(b); ++i)\n#define all(x) begin(x),end(x)\n\nstruct Data {\n ll one_count;\n ll index;\n};\n\nvoid chmax(Data &a, Data &b) {\n if (a.one_count < b.one_count) {\n a = b;\n } else if (a.one_count == b.one_count && a.index > b.index) {\n a = b;\n }\n}\n\nstruct AnsData {\n ll one_count;\n ll index0, index1;\n\n AnsData(ll a, ll b, ll c) {\n one_count = a;\n index0 = min(b, c);\n index1 = max(b, c);\n }\n};\n\nvoid chmax(AnsData &a, AnsData &b) {\n if (a.one_count < b.one_count) {\n a = b;\n } else if (a.one_count == b.one_count) {\n if (a.index0 > b.index0) {\n a = b;\n } else if (a.index0 == b.index0 && a.index1 > b.index1) {\n a = b;\n }\n }\n}\n\nint main() {\n ll n, m;\n cin >> n >> m;\n vector<bitset<20> > a(n, (1 << 20) - 1);\n rep(i, n) {\n string s;\n cin >> s;\n rep(j, m) {\n if (s[j] == 'N') a[i].set(j, 0);\n }\n }\n VLL y_count(n);\n rep(i, n) y_count[i] = a[i].count() - (20 - m);\n\n vector<Data> dp(1 << 20, {-1, -1});\n\n rep(i, n) {\n dp[a[i].to_ulong()] = {y_count[i], i};\n }\n\n rep(i, 20) {\n ll k = (1 << i);\n rep(j, 1<<19) {\n ll j0 = j / k;\n ll j1 = j % k;\n ll j2 = (j0 << (i + 1)) + j1;\n chmax(dp[j2], dp[j2 + k]);\n }\n }\n\n AnsData ans(-1, -1, -1);\n rep(i, n) {\n Data flip = dp[a[i].flip().to_ulong()];\n if (flip.one_count == -1) continue;\n AnsData tmp(flip.one_count + y_count[i], i, flip.index);\n chmax(ans, tmp);\n //cout << ans.one_count << endl;\n }\n if (ans.one_count == -1) cout << \"No\" << endl;\n else\n cout << ans.index0 + 1 << \" \" << ans.index1 + 1 << endl;\n}", "accuracy": 0.1282051282051282, "time_ms": 80, "memory_kb": 19660, "score_of_the_acc": -0.2244, "final_rank": 18 }, { "submission_id": "aoj_1465_10972879", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nlong long n, m;\nset<long long> nums;\nmap<long long, long long> parent_map;\nset<long long> bits;\nmap<long long , long long> bits_map;\n\nvoid dfs(long long num, long long parent) {\n if (num == 0 || bits.count(num)) return;\n bits.insert(num);\n bits_map.insert({num, parent});\n for(int i = 0; i < m; i++){\n if((1 << i) & num) {\n dfs(num - (1 << i), parent);\n }\n }\n}\n\nint main(){\n cin >> n >> m;\n vector<string> a(n);\n for(auto &s:a){\n cin >> s;\n }\n vector<long long> temp;\n for(int i = n - 1; i >= 0; i--){\n long long b = 1, t = 0;\n for(auto c: a[i]){\n if(c == 'Y') {\n t += b;\n }\n b *= 2;\n }\n temp.push_back(t);\n nums.insert(t);\n parent_map[t] = i + 1;\n }\n reverse(temp.begin(), temp.end());\n if (nums.count((1 << m) - 1)) {\n long long all_y_index = -1;\n for(int i = 0; i < n; i++) {\n if (temp[i] == (1 << m) - 1) {\n all_y_index = i;\n break;\n }\n }\n long long max_popcount_index = -1;\n long long max_popcount = -1;\n for(int i = 0; i < n; i++) {\n if(i == all_y_index) continue;\n if(__builtin_popcount(temp[i]) > max_popcount) {\n max_popcount_index = i;\n max_popcount = __builtin_popcount(temp[i]);\n }\n }\n if (all_y_index > max_popcount_index) {\n swap(all_y_index, max_popcount_index);\n }\n cout << all_y_index + 1 << \" \" << max_popcount_index + 1 << endl;\n return 0;\n }\n\n // for(auto [f, s]: parent_map) {\n // cout << f << \" \" << s << endl;\n // }\n for (int i = 0; i < n; i++) {\n dfs(temp[i], i + 1);\n }\n // for(auto [f, s]: bits_map) {\n // cout << f << \" \" << s << endl;\n // }\n // cout << \"aa\" << endl;\n bits.insert(0);\n bits_map[0] = 1;\n\n set<pair<long long, long long>> anses;\n for (auto t: bits) {\n if (bits.count(((1LL << m) - 1) - t)) {\n long long e1 = bits_map[t];\n long long e2 = bits_map[((1LL << m) - 1) - t];\n if (e1 == e2) {\n e2 = 2;\n }\n if (e1 > e2) {\n swap(e1, e2);\n }\n e1--;\n e2--;\n anses.insert({e1, e2});\n // cout << e1 << \" \" << e2 << endl;\n // return 0;\n }\n }\n // for(auto [f, s] : anses) {\n // cout << f << \" \" << s << endl;\n // }\n if (anses.empty()) {\n cout << \"No\" << endl;\n return 0;\n }\n long long max_both = __builtin_popcount(temp[(*anses.begin()).first] & temp[(*anses.begin()).second]);\n pair<long long, long long> ans = *anses.begin();\n for(auto [f, s]: anses) {\n // cout << \";\" << __builtin_popcount(temp[f] & temp[s]) << endl;\n if (__builtin_popcount(temp[f] & temp[s]) > max_both) {\n max_both = __builtin_popcount(temp[f] & temp[s]);\n ans = {f, s};\n } \n // else if (__builtin_popcount(temp[f] & temp[s]) == min_both) {\n // ans = (ans < (pair<long long, long long>){f,s})? ans:(pair<long long, long long>){f,s};\n // }\n }\n cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 0.2564102564102564, "time_ms": 120, "memory_kb": 23116, "score_of_the_acc": -0.3089, "final_rank": 13 }, { "submission_id": "aoj_1465_10972848", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nlong long n, m;\nset<long long> nums;\nmap<long long, long long> parent_map;\nset<long long> bits;\nmap<long long , long long> bits_map;\n\nvoid dfs(long long num, long long parent) {\n if (num == 0 || bits.count(num)) return;\n bits.insert(num);\n bits_map.insert({num, parent});\n for(int i = 0; i < m; i++){\n if((1 << i) & num) {\n dfs(num - (1 << i), parent);\n }\n }\n}\n\nint main(){\n cin >> n >> m;\n vector<string> a(n);\n for(auto &s:a){\n cin >> s;\n }\n vector<long long> temp;\n for(int i = 0; i < n; i++){\n long long b = 1, t = 0;\n for(auto c: a[i]){\n if(c == 'Y') {\n t += b;\n }\n b *= 2;\n }\n temp.push_back(t);\n nums.insert(t);\n parent_map[t] = i + 1;\n }\n // for(auto [f, s]: parent_map) {\n // cout << f << \" \" << s << endl;\n // }\n for (int i = 0; i < n; i++) {\n dfs(temp[i], i + 1);\n }\n // for(auto [f, s]: bits_map) {\n // cout << f << \" \" << s << endl;\n // }\n // cout << \"aa\" << endl;\n bits.insert(0);\n bits_map[0] = 1;\n\n set<pair<long long, long long>> anses;\n for (auto t: bits) {\n if (bits.count(((1LL << m) - 1) - t)) {\n long long e1 = bits_map[t];\n long long e2 = bits_map[((1LL << m) - 1) - t];\n if (e1 == e2) {\n e2 = 2;\n }\n if (e1 > e2) {\n swap(e1, e2);\n }\n e1--;\n e2--;\n anses.insert({e1, e2});\n // cout << e1 << \" \" << e2 << endl;\n // return 0;\n }\n }\n // for(auto [f, s] : anses) {\n // cout << f << \" \" << s << endl;\n // }\n if (anses.empty()) {\n cout << \"No\" << endl;\n return 0;\n }\n long long max_both = __builtin_popcount(temp[(*anses.begin()).first] & temp[(*anses.begin()).second]);\n pair<long long, long long> ans = *anses.begin();\n for(auto [f, s]: anses) {\n // cout << \";\" << __builtin_popcount(temp[f] & temp[s]) << endl;\n if (__builtin_popcount(temp[f] & temp[s]) > max_both) {\n max_both = __builtin_popcount(temp[f] & temp[s]);\n ans = {f, s};\n } \n // else if (__builtin_popcount(temp[f] & temp[s]) == min_both) {\n // ans = (ans < (pair<long long, long long>){f,s})? ans:(pair<long long, long long>){f,s};\n // }\n }\n cout << ans.first + 1 << \" \" << ans.second + 1 << endl;\n}", "accuracy": 0.1282051282051282, "time_ms": 20, "memory_kb": 7400, "score_of_the_acc": -0.04, "final_rank": 16 }, { "submission_id": "aoj_1465_10879416", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\nint n,m;\nsigned main(){\n cin.tie(0)->sync_with_stdio(0);\n cin>>n>>m;\n vector<int>a(n);\n rep(i,n){\n string s;cin>>s;\n rep(j,m)a[i]=2*a[i]+(s[j]=='Y');\n }\n vector<int>f(1<<m,0);\n rep(i,n)f[a[i]]=popcount((unsigned int)a[i]);\n int whole=(1<<m)-1,mx=-1<<30,mxi=-1;\n auto it=ranges::find(a,whole);\n if(it!=a.end()){\n int i=distance(a.begin(),it);\n rep(j,n){\n if(i==j)continue;\n if(mx<f[a[j]])mx=f[a[j]],mxi=j;\n }\n cout<<1+min(i,mxi)<<' '<<1+max(i,mxi)<<endl;\n return 0;\n }\n rep(j,m)rep(i,1<<m)if((i&(1<<j))==0)f[i]=max(f[i],f[i|(1<<j)]);\n rep(i,n-1){\n if(a[i]==whole||a[i]==0)continue;\n int both=f[whole^a[i]]-popcount((unsigned int)(whole^a[i]));\n if(mx<both)mx=both,mxi=i;\n }\n if(mx<0){\n cout<<\"No\\n\";\n return 0;\n }\n for(int j=mxi+1;j<n;++j){\n if((a[mxi]|a[j])==whole&&popcount((unsigned int)(a[mxi]&a[j]))==mx){\n cout<<1+mxi<<' '<<1+j<<endl;\n return 0;\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 8312, "score_of_the_acc": -0.0474, "final_rank": 1 }, { "submission_id": "aoj_1465_10879406", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\nint n,m,a[2<<17];\nsigned main(){\n cin.tie(0)->sync_with_stdio(0);\n cin>>n>>m;\n rep(i,n){\n string s;cin>>s;\n rep(j,m)a[i]=2*a[i]+(s[j]=='Y');\n }\n vector<int>f(1<<m,0);\n rep(i,n){\n f[a[i]]=__builtin_popcount((unsigned int)a[i]);\n }\n rep(j,m)rep(i,1<<m)if((i&(1<<j))==0)f[i]=max(f[i],f[i|(1<<j)]);\n\n int whole=((1<<m)-1);\n vector<int>whole_indices;\n rep(i,n){\n if(a[i]==whole)whole_indices.emplace_back(i);\n }\n if(2<=(int)whole_indices.size()){\n cout<<1+whole_indices[0]<<' '<<1+whole_indices[1]<<endl;\n return 0;\n }\n if(1==(int)whole_indices.size()){\n int mx=-1<<30,mxi=-1;\n rep(i,n){\n int pc=__builtin_popcount((unsigned int)a[i]);\n if(mx<pc&&pc!=m){\n mx=pc;\n mxi=i;\n }\n }\n cout<<1+min(mxi,whole_indices[0])<<' '<<1+max(mxi,whole_indices[0])<<endl;\n return 0;\n }\n\n int mx=-1<<30,mxi=-1;\n rep(i,n-1){\n if(a[i]==whole||a[i]==0)continue;\n int both=f[whole^a[i]]-__builtin_popcount((unsigned int)(whole^a[i]));\n if(mx<both){\n mx=both,mxi=i;\n }\n }\n\n if(mx<0){\n cout<<\"No\\n\";\n return 0;\n }\n //cerr<<\"Yes\\n\";\n for(int j=mxi+1;j<n;++j){\n if((a[mxi]|a[j])!=whole)continue;\n int pc=__builtin_popcount((unsigned int)(a[mxi]&a[j]));\n if(pc==mx){\n cout<<1+mxi<<' '<<1+j<<endl;\n return 0;\n }\n }\n assert(0);\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 8312, "score_of_the_acc": -0.0474, "final_rank": 1 }, { "submission_id": "aoj_1465_10879401", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\nint n,m,a[2<<17];\nsigned main(){\n cin.tie(0)->sync_with_stdio(0);\n cin>>n>>m;\n rep(i,n){\n string s;cin>>s;\n rep(j,m)a[i]=2*a[i]+(s[j]=='Y');\n }\n vector<int>f(1<<m,0);\n rep(i,n){\n f[a[i]]=__builtin_popcount((unsigned int)a[i]);\n }\n rep(j,m)rep(i,1<<m)if((i&(1<<j))==0)f[i]=max(f[i],f[i|(1<<j)]);\n\n int whole=((1<<m)-1);\n vector<int>whole_indices;\n rep(i,n){\n if(a[i]==whole)whole_indices.emplace_back(i);\n }\n if(2<=(int)whole_indices.size()){\n cout<<1+whole_indices[0]<<' '<<1+whole_indices[1]<<endl;\n return 0;\n }\n if(1==(int)whole_indices.size()){\n int mx=-1<<30,mxi=-1;\n rep(i,n){\n int pc=__builtin_popcount((unsigned int)a[i]);\n if(mx<pc&&pc!=m){\n mx=pc;\n mxi=i;\n }\n }\n cout<<1+min(mxi,whole_indices[0])<<' '<<1+max(mxi,whole_indices[0])<<endl;\n return 0;\n }\n\n int mx=-1<<30,mxi=-1;\n rep(i,n-1){\n if(a[i]==whole||a[i]==0)continue;\n int both=f[whole^a[i]]-__builtin_popcount((unsigned int)(whole^a[i]));\n if(mx<both){\n mx=both,mxi=i;\n }\n }\n\n if(mx<0){\n cout<<\"No\\n\";\n return 0;\n }\n //cerr<<\"Yes\\n\";\n for(int j=mxi+1;j<n;++j){\n int pc=__builtin_popcount((unsigned int)(a[mxi]&a[j]));\n if(pc==mx){\n cout<<1+mxi<<' '<<1+j<<endl;\n return 0;\n }\n }\n assert(0);\n}", "accuracy": 0.358974358974359, "time_ms": 20, "memory_kb": 8308, "score_of_the_acc": -0.0474, "final_rank": 10 }, { "submission_id": "aoj_1465_10879389", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\nint n,m,a[2<<17];\nsigned main(){\n cin.tie(0)->sync_with_stdio(0);\n cin>>n>>m;\n rep(i,n){\n string s;cin>>s;\n rep(j,m)a[i]=2*a[i]+(s[j]=='Y');\n }\n vector<int>f(1<<m,0);\n rep(i,n){\n f[a[i]]=__builtin_popcount((unsigned int)a[i]);\n }\n rep(j,m)rep(i,1<<m)if((i&(1<<j))==0)f[i]=max(f[i],f[i|(1<<j)]);\n\n int whole=((1<<m)-1);\n vector<int>whole_indices;\n rep(i,n){\n if(a[i]==whole)whole_indices.emplace_back(i);\n }\n if(2<=(int)whole_indices.size()){\n cout<<1+whole_indices[0]<<' '<<1+whole_indices[1]<<endl;\n return 0;\n }\n if(1==(int)whole_indices.size()){\n int mx=-1<<30,mxi=-1;\n rep(i,n){\n int pc=__builtin_popcount((unsigned int)a[i]);\n if(mx<m+pc&&pc!=m){\n mx=m+pc;\n mxi=i;\n }\n }\n cout<<1+min(mxi,whole_indices[0])<<' '<<1+max(mxi,whole_indices[0])<<endl;\n return 0;\n }\n\n int mx=-1<<30,mxi=-1;\n rep(i,n-1){\n if(a[i]==whole)continue;\n int both=f[whole^a[i]]-__builtin_popcount((unsigned int)(whole^a[i]));\n if(mx<both){\n mx=both,mxi=i;\n }\n }\n\n if(mx<0){\n cout<<\"No\\n\";\n return 0;\n }\n //cerr<<\"Yes\\n\";\n for(int j=mxi+1;j<n;++j){\n int pc=__builtin_popcount((unsigned int)(a[mxi]&a[j]));\n if(pc==mx){\n cout<<1+mxi<<' '<<1+j<<endl;\n return 0;\n }\n }\n assert(0);\n}", "accuracy": 0.358974358974359, "time_ms": 10, "memory_kb": 8308, "score_of_the_acc": -0.0333, "final_rank": 9 }, { "submission_id": "aoj_1465_10805197", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\nint n,m,st[2<<17];\nint f[1<<20];\nint pc(int n){\n return popcount((uint32_t)n);\n}\nsigned main(){\n cin.tie(0)->sync_with_stdio(0);\n cin>>n>>m;\n rep(i,n){\n string s;cin>>s;\n rep(j,m)if(s[j]=='Y')st[i]|=1<<j;\n f[st[i]]=pc(st[i]);\n }\n rep(i,m)rep(j,1<<m)if(!(j&(1<<i)))f[j]=max(f[j],f[j|(1<<i)]);\n int mx=-1,mxi=-1;\n {\n int cnt=0,pos=1<<30;\n rep(i,n){\n if(st[i]==(1<<m)-1)++cnt,pos=min(pos,i);\n }\n if(2<=cnt){\n mx=m,mxi=pos;\n }else if(cnt==1){\n rep(i,n){\n if(i==pos)continue;\n int val=pc(st[i]);\n if(mx<val){\n mx=val;\n mxi=min(pos,i);\n }\n }\n }\n }\n rep(i,n){\n int tgt=st[i]^((1<<m)-1);\n if(tgt){\n int val=f[tgt]-pc(tgt);\n if(mx<val){\n mx=val;\n mxi=i;\n }else if(mx==val){\n mxi=min(mxi,i);\n }\n }\n }\n //cerr<<\"mx: \"<<mx<<endl;\n //cerr<<\"mxi: \"<<mxi<<endl;\n if(mx<0){\n cout<<\"No\\n\";\n return 0;\n }\n rep(i,n){\n if(i==mxi)continue;\n if(pc(st[i]&st[mxi])==mx&&pc(st[i]|st[mxi])==m){\n int l=min(i,mxi);\n int r=max(i,mxi);\n cout<<l+1<<' '<<r+1<<endl;\n return 0;\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 8560, "score_of_the_acc": -0.0635, "final_rank": 3 }, { "submission_id": "aoj_1465_10527226", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n\nvoid solve() {\n int n, m;\n cin >> n >> m;\n\n vector<int> values(n);\n rep(i, n) {\n string s;\n cin >> s;\n int mask = 0;\n rep(j, m) if (s[j] == 'Y') mask |= (1 << j);\n values[i] = mask;\n }\n\n int FULL = (1 << m) - 1;\n\n // dp[mask] = best two (popcount, index)\n vector<pair<int, int>> dp1(1 << m, {-1, -1});\n vector<pair<int, int>> dp2(1 << m, {-1, -1});\n\n rep(i, n) {\n int val = values[i];\n int pop = __builtin_popcount(val);\n if (dp1[val].second == -1) {\n dp1[val] = {pop, i};\n } else if (dp2[val].second == -1 && i != dp1[val].second) {\n dp2[val] = {pop, i};\n }\n }\n\n // Bottom-up DP merge\n for (int mask = FULL; mask >= 1; --mask) {\n for (int j = 0; j < m; ++j) {\n if ((mask >> j) & 1) {\n int sub = mask ^ (1 << j);\n array<pair<int, int>, 4> cands = {\n dp1[mask], dp2[mask], dp1[sub], dp2[sub]\n };\n\n sort(cands.begin(), cands.end(), [](auto a, auto b) {\n if (a.first != b.first) return a.first > b.first;\n return a.second < b.second;\n });\n\n // assign top 2 distinct indices\n dp1[sub] = {-1, -1};\n dp2[sub] = {-1, -1};\n for (auto [pop, idx] : cands) {\n if (idx == -1) continue;\n if (dp1[sub].second == -1) dp1[sub] = {pop, idx};\n else if (dp2[sub].second == -1 && idx != dp1[sub].second) {\n dp2[sub] = {pop, idx};\n break;\n }\n }\n }\n }\n }\n\n int best_pop = -1;\n pair<int, int> res = {-1, -1};\n\n rep(i, n) {\n int val = values[i];\n int need = FULL ^ val;\n auto [pop1, idx1] = dp1[need];\n int total = __builtin_popcount(val);\n\n int idx2 = -1;\n\n if (idx1 == -1) continue;\n if (idx1 == i) {\n tie(pop1, idx1) = dp2[need];\n if (idx1 == -1) continue;\n }\n\n total += pop1;\n\n int a = i, b = idx1;\n if (a > b) swap(a, b);\n\n if (total > best_pop) {\n best_pop = total;\n res = {a, b};\n } else if (total == best_pop) {\n if (a < res.first || (a == res.first && b < res.second)) {\n res = {a, b};\n }\n }\n }\n\n if (best_pop == -1) cout << \"No\\n\";\n else cout << res.first + 1 << \" \" << res.second + 1 << \"\\n\";\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n solve();\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 20480, "score_of_the_acc": -0.3719, "final_rank": 5 } ]
aoj_1463_cpp
Greatest of the Greatest Common Divisors You are given a sequence of integers and a number of intervals in the sequence. The intervals are specified by their leftmost and rightmost positions. An interval consisting of $k$ integers has $k(k − 1)/2$ pairs of integers at different positions, which have their greatest common divisors. For each given interval, find the greatest one among such greatest common divisors. For example, when the sequence is ($a_1, . . . , a_6) = (10, 20, 30, 40, 50, 60)$, and the whole sequence is specified as the interval, the following 15 pairs of two integers at different positions $x$ and $y$, and their greatest common divisors should be considered. x 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 y 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 ax 10 10 10 10 10 20 20 20 20 30 30 30 40 40 50 ay 20 30 40 50 60 30 40 50 60 40 50 60 50 60 60 gcd(ax, ay) 10 10 10 10 10 10 20 10 20 10 10 30 10 20 10 The greatest of the greatest common divisors of the 15 pairs is $gcd(30, 60) = 30$, in this case. Input The input consists of a single test case of the following format. $n$ $a_1$ ... $a_n$ $q$ $l_1$ $r_1$ $\vdots$ $l_q$ $r_q$ The first line contains an integer n, which is the number of integers in the given sequence, satisfying $2 \leq n \leq 10^5$. The second line contains n positive integers a1 through an, specifying the sequence. None of them exceeds $10^5$. The third line contains a positive integer $q$, specifying the number of intervals in the sequence to be considered, which does not exceed $10^5$. It is followed by $q$ lines, each specifying an interval in the sequence to be considered. The $i$-th line of them has two integers, $l_i$ and $r_i$ ($1 \leq l_i < r_i \leq n$), specifying the interval $a_{l_i}$ through $a_{r_i}$ in the sequence. Output Output $q$ lines, the $i$-th line of which should have the greatest of the greatest common divisors of all pairs in the interval specified by $l_i$ and $r_i$. Sample Input 1 6 10 20 30 40 50 60 3 1 6 2 5 4 5 Sample Output 1 30 20 10 Sample Input 2 10 13 2 35 4 13 2 5 1 7 4 5 1 4 4 10 3 8 3 9 1 10 Sample Output 2 2 4 5 7 13
[ { "submission_id": "aoj_1463_11052822", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\ntemplate<class T> void out(T a) { cout << a << endl; }\n\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing pll = pair<ll, ll>;\nusing vpll = vector<pll>;\nusing ull = unsigned long long;\n\nll MX = 100000;\nvvll ds;\n\nclass MoAlgorithm\n{\n private:\n ll data_size;\n ll query_size;\n ll th;\n ll div_num;\n\n priority_queue<ll> pq;\n vll cnts;\n\n vll data;\n vpll query;\n vector<vector<pair<pll, ll>>> div_query;\n vll res;\n public:\n MoAlgorithm(vll input_data, vpll input_query) : data(input_data), query(input_query)\n {\n data_size = data.size();\n query_size = query.size();\n div_num = max(1LL, (ll)sqrt(query_size));\n th = 1 + (data_size - 1)/div_num;\n div_query = vector<vector<pair<pll, ll>>>(div_num);\n rep(i, query_size)\n {\n div_query[query[i].first/th].emplace_back(pll{query[i].second, query[i].first}, i);\n }\n rep(i, div_num)\n {\n if(i % 2 == 0) sort(all(div_query[i]));\n else sort(all(div_query[i]), greater<pair<pll, ll>>());\n rep(j, div_query[i].size())\n {\n swap(div_query[i][j].first.first, div_query[i][j].first.second);\n }\n }\n ResClear();\n Clear();\n ll l = 0, r = 0;\n rep(i, div_num)\n {\n rep(j, div_query[i].size())\n {\n while(r < div_query[i][j].first.second)\n {\n InsertData(r);\n r++;\n }\n while(l > div_query[i][j].first.first)\n {\n InsertData(l - 1);\n l--;\n }\n while(l < div_query[i][j].first.first)\n {\n DeleteData(l);\n l++;\n }\n while(r > div_query[i][j].first.second)\n {\n DeleteData(r-1);\n r--;\n }\n res[div_query[i][j].second] = Calc();\n }\n }\n }\n\n void ResClear()\n {\n res = vector<ll>(query_size, 0);\n }\n\n void Clear()\n {\n cnts = vll(MX + 1, 0);\n }\n\n void InsertData(ll id)\n {\n ll num = data[id];\n for(auto d : ds[num])\n {\n cnts[d]++;\n if(cnts[d] == 2) pq.emplace(d);\n }\n }\n\n void DeleteData(ll id)\n {\n ll num = data[id];\n for(auto d : ds[num])\n {\n cnts[d]--;\n }\n }\n\n ll Calc()\n {\n while(!pq.empty() && cnts[pq.top()] < 2) pq.pop();\n return pq.empty() ? 1LL : pq.top();\n }\n\n vll Ans()\n {\n return res;\n }\n};\n\nint main()\n{\n ll N; cin >> N;\n vll A(N);\n rep(i, N) cin >> A[i];\n MX = *max_element(all(A));\n ds.resize(MX + 1);\n for(ll n = 1; n <= MX; n++)\n {\n for(ll m = n; m <= MX; m += n) ds[m].emplace_back(n);\n }\n ll Q; cin >> Q;\n vpll querys(Q);\n rep(i, Q)\n {\n ll l, r; cin >> l >> r;\n l--;\n querys[i] = {l, r};\n }\n MoAlgorithm Mo(A, querys);\n auto ans = Mo.Ans();\n rep(i, Q) cout << ans[i] << endl;\n}", "accuracy": 1, "time_ms": 1410, "memory_kb": 49676, "score_of_the_acc": -0.712, "final_rank": 12 }, { "submission_id": "aoj_1463_11046996", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint segL[1<<18];\n\nvoid update(int pos) {\n if (segL[pos +(1<<17)] == 0) {\n segL[pos +(1<<17)] = pos;\n }else {\n segL[pos +(1<<17)] = 0;\n }\n int id = (pos +(1<<17))/2;\n while (id != 0) {\n segL[id]=max(segL[id<<1],segL[(id<<1)+1]);\n id>>=1;\n }\n}\n\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n vector<int> D[100001];\n for (int cur=1;cur<=100000;cur++) {\n for (int i=1;i * i <= cur;i++) {\n if (cur % i == 0) {\n D[cur].push_back(i);\n if (cur != i * i)D[cur].push_back(cur / i);\n }\n }\n }\n vector<pair<pair<int,int>,int>>M[317];\n int N;\n cin>>N;\n int A[100001];\n for (int i=1;i<=N;i++) {\n cin>>A[i];\n }\n int Q;\n cin>>Q;\n for (int i=0;i<Q;i++) {\n int L,R;\n cin>>L>>R;\n M[R/317].push_back(make_pair(make_pair(L,R),i));\n }\n int nowL = 1;\n int nowR = 1;\n int C[100001] ={0};\n for (int i =0;i<D[A[1]].size();i++) {\n C[D[A[1]][i]]++;\n }\n int ans[100000];\n for (int i=0;i<317;i++) {\n sort(M[i].begin(),M[i].end());\n for (int j=0;j<M[i].size();j++) {\n while (nowL > M[i][j].first.first) {\n nowL--;\n for (int k =0;k<D[A[nowL]].size();k++) {\n C[D[A[nowL]][k]]++;\n if(C[D[A[nowL]][k]] == 2)update(D[A[nowL]][k]);\n }\n }\n while (nowL < M[i][j].first.first) {\n for (int k =0;k<D[A[nowL]].size();k++) {\n C[D[A[nowL]][k]]--;\n if(C[D[A[nowL]][k]] == 1)update(D[A[nowL]][k]);\n }\n nowL++;\n }\n while (nowR > M[i][j].first.second) {\n for (int k =0;k<D[A[nowR]].size();k++) {\n C[D[A[nowR]][k]]--;\n if(C[D[A[nowR]][k]] == 1)update(D[A[nowR]][k]);\n }\n nowR--;\n }\n while (nowR < M[i][j].first.second) {\n nowR++;\n for (int k =0;k<D[A[nowR]].size();k++) {\n C[D[A[nowR]][k]]++;\n if(C[D[A[nowR]][k]] == 2)update(D[A[nowR]][k]);\n }\n }\n ans[M[i][j].second] = segL[1];\n }\n }\n\n for (int i=0;i<Q;i++) {\n cout<<ans[i]<<\"\\n\";\n }\n\n}", "accuracy": 1, "time_ms": 2520, "memory_kb": 16876, "score_of_the_acc": -1.0402, "final_rank": 13 }, { "submission_id": "aoj_1463_11046949", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n vector<int> D[100001];\n for (int cur=1;cur<=100000;cur++) {\n for (int i=1;i * i <= cur;i++) {\n if (cur % i == 0) {\n D[cur].push_back(i);\n if (cur != i * i)D[cur].push_back(cur / i);\n }\n }\n }\n vector<pair<pair<int,int>,int>>M[317];\n int N;\n cin>>N;\n int A[100001];\n for (int i=1;i<=N;i++) {\n cin>>A[i];\n }\n int Q;\n cin>>Q;\n for (int i=0;i<Q;i++) {\n int L,R;\n cin>>L>>R;\n M[R/317].push_back(make_pair(make_pair(L,R),i));\n }\n int nowL = 1;\n int nowR = 1;\n int C[100001] ={0};\n for (int i =0;i<D[A[1]].size();i++) {\n C[D[A[1]][i]]++;\n }\n set<int>SS;\n int ans[100000];\n for (int i=0;i<317;i++) {\n sort(M[i].begin(),M[i].end());\n for (int j=0;j<M[i].size();j++) {\n while (nowL < M[i][j].first.first) {\n for (int k =0;k<D[A[nowL]].size();k++) {\n C[D[A[nowL]][k]]--;\n if(C[D[A[nowL]][k]] == 1)SS.erase(D[A[nowL]][k]);\n }\n nowL++;\n }\n while (nowR > M[i][j].first.second) {\n for (int k =0;k<D[A[nowR]].size();k++) {\n C[D[A[nowR]][k]]--;\n if(C[D[A[nowR]][k]] == 1)SS.erase(D[A[nowR]][k]);\n }\n nowR--;\n }\n while (nowR < M[i][j].first.second) {\n nowR++;\n for (int k =0;k<D[A[nowR]].size();k++) {\n C[D[A[nowR]][k]]++;\n if(C[D[A[nowR]][k]] == 2)SS.insert(D[A[nowR]][k]);\n }\n }\n auto itr = SS.end();\n itr--;\n ans[M[i][j].second] = (*itr);\n }\n }\n\n for (int i=0;i<Q;i++) {\n cout<<ans[i]<<\"\\n\";\n }\n\n}", "accuracy": 0.018867924528301886, "time_ms": 980, "memory_kb": 15800, "score_of_the_acc": -0.3995, "final_rank": 20 }, { "submission_id": "aoj_1463_10907676", "code_snippet": "/*\n * author : cellul4r\n */\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int, int> pi;\ntypedef pair<ll,ll> pl;\ntypedef pair<ld,ld> pd;\n#define all(x) x.begin(), x.end()\nconst char nl = '\\n';\nconst int N =1e5+1;\nconst int INF = 1e9+7;\nconst ll LINF = 1e18+7;\n\nvoid setIO(string);\nstruct segtree {\n\n int t[4*N];\n\n int f(int a, int b) { return max(a,b); }\n\n void update(int node, int l, int r, int idx, int val) {\n if(l == r) {\n t[node] = f(t[node],val);\n return;\n }\n\n int mid = (l + r) >> 1;\n if(idx <= mid) {\n update(node<<1,l,mid,idx,val);\n } else {\n update(node<<1|1,mid+1,r,idx,val);\n }\n t[node] = f(t[node<<1],t[node<<1|1]);\n }\n\n int query(int node, int l, int r, int L, int R) {\n if(l > R || r < L) return 1;\n if(l >= L && r <= R) {\n return t[node];\n }\n int mid = (l + r) >> 1;\n return f(query(node<<1,l,mid,L,R), query(node<<1|1,mid+1,r,L,R));\n }\n};\nint n,q;\n// trick each we gradually increase the range query of R,R+1,...\n// then when we go to that range R we have to know what value divisor does it has\n// where is the last position of this divisor j we have seen\n// at last[r] we have this divisor j we must find some another\n// to get this gcd value = j\n// just save it in last[r]\n// then find max in range query[l,r]\nvector<pi> save[N], query[N];\nvector<int> divi[N];\nint idx[N];\nint a[N], ans[N];\nvoid solve(){\n\n cin>>n;\n for(int i = 0; i < n; i++) {\n cin>>a[i];\n }\n\n cin>>q;\n for(int i = 0; i < q; i++) {\n int l, r; cin>>l>>r;\n l--,r--;\n query[r].push_back({l,i});\n }\n\n // save all divisor in O(nlogn)\n for(int i = 1; i < N; i++) {\n for(int j = i; j < N; j += i) {\n // j divisible by i\n divi[j].push_back(i);\n }\n }\n\n // create save[N] for at pos i each divisor j where does the last pos before this occur to get the gcd\n memset(idx, -1, sizeof idx);\n for(int i = 0; i < n; i++) {\n for(auto &j : divi[a[i]]) {\n // only save value divisor j in increasing position \n if(idx[j] != -1) {\n save[i].push_back(make_pair(idx[j], j));\n }\n idx[j] = i;\n }\n }\n\n segtree seg;\n for(int r = 0; r < n; r++) {\n for(auto& [i, j] : save[r]) {\n seg.update(1,0,n-1,i,j);\n }\n\n for(auto &[l, i] : query[r]) {\n ans[i] = seg.query(1,0,n-1,l,r); \n }\n }\n\n for(int i = 0; i < q; i++) {\n cout << ans[i] << nl;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n\n //setIO(\"\");\n //cin>>t;\n while(t--)solve();\n\n return 0;\n}\n\nvoid setIO(string s) {\n (void)!freopen((s + \".in\").c_str(), \"r\", stdin);\n (void)!freopen((s + \".out\").c_str(), \"w\", stdout);\n}", "accuracy": 1, "time_ms": 590, "memory_kb": 123388, "score_of_the_acc": -0.6664, "final_rank": 10 }, { "submission_id": "aoj_1463_10907664", "code_snippet": "/*\n * author : cellul4r\n */\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int, int> pi;\ntypedef pair<ll,ll> pl;\ntypedef pair<ld,ld> pd;\n#define all(x) x.begin(), x.end()\nconst char nl = '\\n';\nconst int N =1e5+1;\nconst int INF = 1e9+7;\nconst ll LINF = 1e18+7;\n\nvoid setIO(string);\nstruct segtree {\n\n int t[4*N];\n\n int f(int a, int b) { return max(a,b); }\n\n void update(int node, int l, int r, int idx, int val) {\n if(l == r) {\n t[node] = f(t[node],val);\n return;\n }\n\n int mid = (l + r) >> 1;\n if(idx <= mid) {\n update(node<<1,l,mid,idx,val);\n } else {\n update(node<<1|1,mid+1,r,idx,val);\n }\n t[node] = f(t[node<<1],t[node<<1|1]);\n }\n\n int query(int node, int l, int r, int L, int R) {\n if(l > R || r < L) return 1;\n if(l >= L && r <= R) {\n return t[node];\n }\n int mid = (l + r) >> 1;\n return f(query(node<<1,l,mid,L,R), query(node<<1|1,mid+1,r,L,R));\n }\n};\nint n,q;\n// trick each we gradually increase the range query of R,R+1,...\n// then when we go to that range R we have to know what value divisor does it has\n// where is the last position of this divisor j we have seen\n// at last[r] we have this divisor j we must find some another\n// to get this gcd value = j\n// just save it in last[r]\n// then find max in range query[l,r]\nvector<pi> save[N], query[N];\nvector<int> divi[N];\nint idx[N];\nint a[N], ans[N];\nvoid solve(){\n\n cin>>n;\n for(int i = 0; i < n; i++) {\n cin>>a[i];\n }\n\n cin>>q;\n for(int i = 0; i < q; i++) {\n int l, r; cin>>l>>r;\n l--,r--;\n query[r].push_back({l,i});\n }\n\n // save all divisor in O(nlogn)\n for(int i = 1; i < N; i++) {\n for(int j = i; j < N; j += i) {\n // j divisible by i\n divi[j].push_back(i);\n }\n }\n\n for(int i = 1; i < N; i++) reverse(all(divi[i]));\n // create save[N] for at pos i each divisor j where does the last pos before this occur to get the gcd\n memset(idx, -1, sizeof idx);\n for(int i = 0; i < n; i++) {\n for(auto &j : divi[a[i]]) {\n // only save value divisor j in increasing position \n if(idx[j] != -1) {\n save[i].push_back(make_pair(idx[j], j));\n }\n idx[j] = i;\n }\n }\n\n segtree seg;\n for(int r = 0; r < n; r++) {\n for(auto& [i, j] : save[r]) {\n seg.update(1,0,n-1,i,j);\n }\n\n for(auto &[l, i] : query[r]) {\n ans[i] = seg.query(1,0,n-1,l,r); \n }\n }\n\n for(int i = 0; i < q; i++) {\n cout << ans[i] << nl;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n\n //setIO(\"\");\n //cin>>t;\n while(t--)solve();\n\n return 0;\n}\n\nvoid setIO(string s) {\n (void)!freopen((s + \".in\").c_str(), \"r\", stdin);\n (void)!freopen((s + \".out\").c_str(), \"w\", stdout);\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 123424, "score_of_the_acc": -0.6707, "final_rank": 11 }, { "submission_id": "aoj_1463_10907663", "code_snippet": "/*\n * author : cellul4r\n */\n#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int, int> pi;\ntypedef pair<ll,ll> pl;\ntypedef pair<ld,ld> pd;\n#define all(x) x.begin(), x.end()\nconst char nl = '\\n';\nconst int N =1e5+1;\nconst int INF = 1e9+7;\nconst ll LINF = 1e18+7;\n\nvoid setIO(string);\nstruct segtree {\n\n int t[4*N];\n\n int f(int a, int b) { return max(a,b); }\n\n void update(int node, int l, int r, int idx, int val) {\n if(l == r) {\n t[node] = f(t[node],val);\n return;\n }\n\n int mid = (l + r) >> 1;\n if(idx <= mid) {\n update(node<<1,l,mid,idx,val);\n } else {\n update(node<<1|1,mid+1,r,idx,val);\n }\n t[node] = f(t[node<<1],t[node<<1|1]);\n }\n\n int query(int node, int l, int r, int L, int R) {\n if(l > R || r < L) return 1;\n if(l >= L && r <= R) {\n return t[node];\n }\n int mid = (l + r) >> 1;\n return f(query(node<<1,l,mid,L,R), query(node<<1|1,mid+1,r,L,R));\n }\n};\nint n,q;\n// trick each we gradually increase the range query of R,R+1,...\n// then when we go to that range R we have to know what value divisor does it has\n// where is the last position of this divisor j we have seen\n// at last[r] we have this divisor j we must find some another\n// to get this gcd value = j\n// just save it in last[r]\n// then find max in range query[l,r]\nvector<pi> save[N], query[N];\nvector<int> divi[N];\nint idx[N];\nint a[N], ans[N];\nvoid solve(){\n\n cin>>n;\n for(int i = 0; i < n; i++) {\n cin>>a[i];\n }\n\n cin>>q;\n for(int i = 0; i < q; i++) {\n int l, r; cin>>l>>r;\n l--,r--;\n query[r].push_back({l,i});\n }\n\n // save all divisor in O(nlogn)\n for(int i = 1; i < N; i++) {\n for(int j = i; j < N; j += i) {\n // j divisible by i\n divi[j].push_back(i);\n }\n }\n\n for(int i = 1; i < N; i++) reverse(all(divi[i]));\n // create save[N] for at pos i each divisor j where does the last pos before this occur to get the gcd\n memset(idx, -1, sizeof idx);\n for(int i = 0; i < n; i++) {\n for(auto &j : divi[a[i]]) {\n // only save value divisor j in increasing position \n if(idx[j] != -1) {\n if(save[i].empty() || save[i].back().first < idx[j]) {\n save[i].push_back(make_pair(idx[j], j));\n }\n }\n idx[j] = i;\n }\n }\n\n segtree seg;\n for(int r = 0; r < n; r++) {\n for(auto& [i, j] : save[r]) {\n seg.update(1,0,n-1,i,j);\n }\n\n for(auto &[l, i] : query[r]) {\n ans[i] = seg.query(1,0,n-1,l,r); \n }\n }\n\n for(int i = 0; i < q; i++) {\n cout << ans[i] << nl;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n\n //setIO(\"\");\n //cin>>t;\n while(t--)solve();\n\n return 0;\n}\n\nvoid setIO(string s) {\n (void)!freopen((s + \".in\").c_str(), \"r\", stdin);\n (void)!freopen((s + \".out\").c_str(), \"w\", stdout);\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 31528, "score_of_the_acc": -0.1108, "final_rank": 3 }, { "submission_id": "aoj_1463_10907572", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, q;\nconst int MAXN = 1e5 + 5;\nint a[MAXN];\n\nstruct Node {\n int mx1, mx2; // largest and second largest\n Node(int _mx1 = 0, int _mx2 = 0) : mx1(_mx1), mx2(_mx2) {}\n};\n\n// merge two nodes: pick the top 2 numbers\nNode merge(Node left, Node right) {\n vector<int> v = {left.mx1, left.mx2, right.mx1, right.mx2};\n sort(v.rbegin(), v.rend()); // descending\n return Node(v[0], v[1]);\n}\n\nNode seg[4*MAXN];\n\n// build segment tree\nvoid build(int idx, int l, int r) {\n if (l == r) {\n seg[idx] = Node(a[l], 0);\n return;\n }\n int mid = (l+r)/2;\n build(2*idx, l, mid);\n build(2*idx+1, mid+1, r);\n seg[idx] = merge(seg[2*idx], seg[2*idx+1]);\n}\n\n// query [ql, qr]\nNode query(int idx, int l, int r, int ql, int qr) {\n if (qr < l || r < ql) return Node(0,0);\n if (ql <= l && r <= qr) return seg[idx];\n int mid = (l+r)/2;\n Node left = query(2*idx, l, mid, ql, qr);\n Node right = query(2*idx+1, mid+1, r, ql, qr);\n return merge(left, right);\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n cin >> n;\n for (int i=0;i<n;i++) cin >> a[i];\n\n build(1,0,n-1);\n\n cin >> q;\n for (int i=0;i<q;i++) {\n int l,r;\n cin >> l >> r;\n l--; r--; // 0-indexed\n Node res = query(1,0,n-1,l,r);\n cout << __gcd(res.mx1, res.mx2) << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 0.018867924528301886, "time_ms": 100, "memory_kb": 6892, "score_of_the_acc": -0.0004, "final_rank": 18 }, { "submission_id": "aoj_1463_10805339", "code_snippet": "#include\"atcoder/segtree\"\n#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\nvector<int>get_divisors(int n){\n vector<int>ret;\n for(int i=1;i*i<=n;++i){\n if(n%i!=0)continue;\n ret.emplace_back(i);\n if(i*i!=n)ret.emplace_back(n/i);\n }\n return ret;\n}\nint op(int a,int b){\n return max(a,b);\n}\nint e(){\n return 0;\n}\nint n,a[1<<17],q,l[1<<17],r[1<<17];\nvector<pair<int,int>>li[1<<17];\nint ans[1<<17];\nint pos[1<<17];\natcoder::segtree<int,op,e>seg(1<<17);\nsigned main(){\n cin.tie(0)->sync_with_stdio(0);\n rep(i,1<<17)pos[i]=-1;\n\n cin>>n;\n rep(i,n)cin>>a[i];\n cin>>q;\n rep(i,q){\n cin>>l[i]>>r[i],--l[i];\n li[r[i]].emplace_back(l[i],i);\n }\n rep(i,n){\n auto div=get_divisors(a[i]);\n for(const auto&d:div){\n if(pos[d]!=-1){\n seg.set(pos[d],max(d,seg.get(pos[d])));\n }\n pos[d]=i;\n }\n for(auto[ql,qi]:li[i+1]){\n ans[qi]=seg.prod(ql,i+1);\n }\n }\n rep(i,q)cout<<ans[i]<<'\\n';\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 12492, "score_of_the_acc": -0.1054, "final_rank": 2 }, { "submission_id": "aoj_1463_10482063", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate<class T> bool chmin(T& a, T b) { return a > b ? a = b, true : false; }\ntemplate<class T> bool chmax(T& a, T b) { return a < b ? a = b, true : false; }\ntemplate<typename T>\nconcept Iterable = requires(T t) { std::begin(t); std::end(t); };\ntemplate<typename T>\n\trequires Iterable<T> && (!is_same_v<T, string>)\nostream& operator<<(ostream& os, const T& container) { for (auto& element : container) os << element << ' '; return os; }\ntemplate<typename R>\n\trequires ranges::range<R> && (!is_same_v<decay_t<R>, string>) && (!is_same_v<decay_t<R>, const char*>)\nostream& operator<<(ostream& os, R&& range) { for (auto& element : range)os << element << ' '; return os; }\ntemplate<typename T>\n\trequires Iterable<T> && (!is_same_v<T, string>)\nistream& operator>>(std::istream& is, T& container) { for (auto& e : container)is >> e; return is; }\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\ntemplate<class T = ll> struct Edge {\n\tint to;\n\tT weight;\n\tbool operator==(Edge e) { return this->to == e.to and this->weight == e.weight; }\n\tbool operator<(Edge e) { return this->to == e.to ? this->weight < e.weight : this->to < e.to; }\n};\n#ifdef _DEBUG\n#define SHOW(n) {const auto& _ret = n; cerr << #n << \": \" << _ret << endl;}\n#define MSG(x) cerr << x << endl;\n#else\n#define SHOW(n)\n#define MSG(x)\n#endif\n\n////AtCoder Library \n//#include <atcoder/all>\n//using namespace atcoder;\n//using mint = modint998244353;\n////using mint = modint1000000007;\n////using mint1 = dynamic_modint<0>;\n////using mint = modint;\n////mint::set_mod();\n//istream& operator>>(istream& is, mint& x) { ll r; is >> r; x = r; return is; }\n//ostream& operator<<(ostream& os, mint& x) { os << x.val(); return os; }\n\n//boost\n//#include <boost/multiprecision/cpp_int.hpp>\n//using namespace boost::multiprecision;\n//using l3 = int128_t;\n\nstruct BIT {\n\tvector<ll> a;\n\tBIT(ll n) : a(n + 1) {}\n\tvoid add(ll i, ll x) {\n\t\t++i;\n\t\twhile (i < a.size())a[i] += x, i += i & -i;\n\t}\n\tll sum(ll r) {\n\t\tll s = 0;\n\t\twhile (r)s += a[r], r -= r & -r;\n\t\treturn s;\n\t}\n\tll sum(ll l, ll r) { return sum(r) - sum(l); }\n\n\tint lower_bound(ll w) {\n\t\tif (w <= 0)return 0;\n\t\tint x = 0, N = a.size() + 1;\n\t\tint lg = bit_width(unsigned(N)) - 1;\n\t\tfor (int k = 1 << lg; k; k >>= 1) {\n\t\t\tif (x + k < a.size() and a[x + k] < w) {\n\t\t\t\tw -= a[x + k];\n\t\t\t\tx += k;\n\t\t\t}\n\t\t}\n\t\treturn x;\n\t}\n};\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tint n;\n\tcin >> n;\n\tvector<int> a(n);\n\tcin >> a;\n\n\tint q;\n\tcin >> q;\n\tvector<int> l(q), r(q);\n\tfor (int i = 0; i < q; ++i) {\n\t\tcin >> l[i] >> r[i];\n\t\t--l[i];\n\t}\n\n\tconst int B = max<int>(1, n / sqrt(2.0 * q / 3));\n\tvector<int> idx(q);\n\tiota(idx.begin(), idx.end(), 0);\n\tranges::sort(idx, [&](int i, int j) {\n\t\tif (l[i] / B == l[j] / B)return r[i] < r[j];\n\t\telse return l[i] < l[j];\n\t\t});\n\n\tvector<vector<int>> factor(n);\n\tfor (int i = 0; i < n; ++i) {\n\t\tfor (int j = 1; j * j <= a[i]; ++j) {\n\t\t\tif (a[i] % j == 0) {\n\t\t\t\tfactor[i].push_back(j);\n\t\t\t\tif (j * j != a[i])factor[i].push_back(a[i] / j);\n\t\t\t}\n\t\t}\n\t}\n\n\tvector<int> cnt(1e5 + 1);\n\n\tBIT seg(1e5 + 1);\n\n\tint ssize = 0;\n\tint nl = 0, nr = 0;\n\tvector<int> res(q);\n\tfor (int i : idx) {\n\t\twhile (l[i] < nl) {\n\t\t\t--nl;\n\t\t\tfor (auto d : factor[nl]) {\n\t\t\t\tcnt[d]++;\n\t\t\t\tif (cnt[d] == 2) {\n\t\t\t\t\tseg.add(d, 1);\n\t\t\t\t\t++ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile (nr < r[i]) {\n\t\t\tfor (auto d : factor[nr]) {\n\t\t\t\tcnt[d]++;\n\t\t\t\tif (cnt[d] == 2) {\n\t\t\t\t\tseg.add(d, 1);\n\t\t\t\t\t++ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t\tnr++;\n\t\t}\n\t\twhile (nl < l[i]) {\n\t\t\tfor (auto d : factor[nl]) {\n\t\t\t\tcnt[d]--;\n\t\t\t\tif (cnt[d] == 1) {\n\t\t\t\t\tseg.add(d, -1);\n\t\t\t\t\t--ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t\tnl++;\n\t\t}\n\t\twhile (r[i] < nr) {\n\t\t\t--nr;\n\t\t\tfor (auto d : factor[nr]) {\n\t\t\t\tcnt[d]--;\n\t\t\t\tif (cnt[d] == 1) {\n\t\t\t\t\tseg.add(d, -1);\n\t\t\t\t\t--ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint j = seg.lower_bound(ssize);\n\t\tres[i] = j;\n\t}\n\n\tfor (auto x : res)cout << x << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1030, "memory_kb": 60456, "score_of_the_acc": -0.5978, "final_rank": 9 }, { "submission_id": "aoj_1463_10481088", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate<class T> bool chmin(T& a, T b) { return a > b ? a = b, true : false; }\ntemplate<class T> bool chmax(T& a, T b) { return a < b ? a = b, true : false; }\ntemplate<typename T>\nconcept Iterable = requires(T t) { std::begin(t); std::end(t); };\ntemplate<typename T>\n\trequires Iterable<T> && (!is_same_v<T, string>)\nostream& operator<<(ostream& os, const T& container) { for (auto& element : container) os << element << ' '; return os; }\ntemplate<typename R>\n\trequires ranges::range<R> && (!is_same_v<decay_t<R>, string>) && (!is_same_v<decay_t<R>, const char*>)\nostream& operator<<(ostream& os, R&& range) { for (auto& element : range)os << element << ' '; return os; }\ntemplate<typename T>\n\trequires Iterable<T> && (!is_same_v<T, string>)\nistream& operator>>(std::istream& is, T& container) { for (auto& e : container)is >> e; return is; }\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n\nstruct BIT {\n\tvector<ll> a;\n\tBIT(ll n) : a(n + 1) {}\n\tvoid add(ll i, ll x) {\n\t\t++i;\n\t\twhile (i < a.size())a[i] += x, i += i & -i;\n\t}\n\tll sum(ll r) {\n\t\tll s = 0;\n\t\twhile (r)s += a[r], r -= r & -r;\n\t\treturn s;\n\t}\n\tll sum(ll l, ll r) { return sum(r) - sum(l); }\n\n\tint lower_bound(ll w) {\n\t\tif (w <= 0)return 0;\n\t\tint x = 0, N = a.size() + 1;\n\t\tint lg = bit_width(unsigned(N)) - 1;\n\t\tfor (int k = 1 << lg; k; k >>= 1) {\n\t\t\tif (x + k <= N - 1 and a[x + k] < w) {\n\t\t\t\tw -= a[x + k];\n\t\t\t\tx += k;\n\t\t\t}\n\t\t}\n\t\treturn x;\n\t}\n};\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tint n;\n\tcin >> n;\n\tvector<int> a(n);\n\tcin >> a;\n\n\tint q;\n\tcin >> q;\n\tvector<int> l(q), r(q);\n\tfor (int i = 0; i < q; ++i) {\n\t\tcin >> l[i] >> r[i];\n\t\t--l[i];\n\t}\n\n\tconst int B = max<int>(1, n / sqrt(2.0 * q / 3));\n\tvector<int> idx(q);\n\tiota(idx.begin(), idx.end(), 0);\n\tranges::sort(idx, [&](int i, int j) {\n\t\tif (l[i] / B == l[j] / B)return r[i] < r[j];\n\t\telse return l[i] < l[j];\n\t\t});\n\n\tvector<vector<int>> factor(n);\n\tfor (int i = 0; i < n; ++i) {\n\t\tfor (int j = 1; j * j <= a[i]; ++j) {\n\t\t\tif (a[i] % j == 0) {\n\t\t\t\tfactor[i].push_back(j);\n\t\t\t\tif (j * j != a[i])factor[i].push_back(a[i] / j);\n\t\t\t}\n\t\t}\n\t}\n\n\tvector<int> cnt(1e5 + 1);\n\n\tBIT seg(1e5 + 1);\n\n\n\tint ssize = 0;\n\tint nl = 0, nr = 0;\n\tvector<int> res(q);\n\tfor (int i : idx) {\n\t\twhile (l[i] < nl) {\n\t\t\t--nl;\n\t\t\tfor (auto d : factor[nl]) {\n\t\t\t\tcnt[d]++;\n\t\t\t\tif (cnt[d] == 2) {\n\t\t\t\t\tseg.add(d, 1);\n\t\t\t\t\t++ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile (nr < r[i]) {\n\t\t\tfor (auto d : factor[nr]) {\n\t\t\t\tcnt[d]++;\n\t\t\t\tif (cnt[d] == 2) {\n\t\t\t\t\tseg.add(d, 1);\n\t\t\t\t\t++ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t\tnr++;\n\t\t}\n\t\twhile (nl < l[i]) {\n\t\t\tfor (auto d : factor[nl]) {\n\t\t\t\tcnt[d]--;\n\t\t\t\tif (cnt[d] == 1) {\n\t\t\t\t\tseg.add(d, -1);\n\t\t\t\t\t--ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t\t++nl;\n\t\t}\n\t\twhile (r[i] < nr) {\n\t\t\t--nr;\n\t\t\tfor (auto d : factor[nr]) {\n\t\t\t\tcnt[d]--;\n\t\t\t\tif (cnt[d] == 1) {\n\t\t\t\t\tseg.add(d, -1);\n\t\t\t\t\t--ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint j = seg.lower_bound(ssize);\n\t\tres[i] = j;\n\t}\n\n\tfor (auto x : res)cout << x << endl;\n\n\treturn 0;\n}", "accuracy": 0.1320754716981132, "time_ms": 1010, "memory_kb": 60456, "score_of_the_acc": -0.5896, "final_rank": 17 }, { "submission_id": "aoj_1463_10480998", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate<class T> bool chmin(T& a, T b) { return a > b ? a = b, true : false; }\ntemplate<class T> bool chmax(T& a, T b) { return a < b ? a = b, true : false; }\ntemplate<typename T>\nconcept Iterable = requires(T t) { std::begin(t); std::end(t); };\ntemplate<typename T>\n\trequires Iterable<T> && (!is_same_v<T, string>)\nostream& operator<<(ostream& os, const T& container) { for (auto& element : container) os << element << ' '; return os; }\ntemplate<typename R>\n\trequires ranges::range<R> && (!is_same_v<decay_t<R>, string>) && (!is_same_v<decay_t<R>, const char*>)\nostream& operator<<(ostream& os, R&& range) { for (auto& element : range)os << element << ' '; return os; }\ntemplate<typename T>\n\trequires Iterable<T> && (!is_same_v<T, string>)\nistream& operator>>(std::istream& is, T& container) { for (auto& e : container)is >> e; return is; }\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n\nstruct BIT {\n\tvector<ll> a;\n\tBIT(ll n) : a(n + 1) {}\n\tvoid add(ll i, ll x) {\n\t\t++i;\n\t\twhile (i < a.size())a[i] += x, i += i & -i;\n\t}\n\tll sum(ll r) {\n\t\tll s = 0;\n\t\twhile (r)s += a[r], r -= r & -r;\n\t\treturn s;\n\t}\n\tll sum(ll l, ll r) { return sum(r) - sum(l); }\n\n\tint lower_bound(ll w) {\n\t\tif (w <= 0)return 0;\n\t\tint x = 0, N = a.size() + 1;\n\t\tint lg = bit_width(unsigned(N)) - 1;\n\t\tfor (int k = 1 << lg; k; k >>= 1) {\n\t\t\tif (x + k <= N - 1 and a[x + k] < w) {\n\t\t\t\tw -= a[x + k];\n\t\t\t\tx += k;\n\t\t\t}\n\t\t}\n\t\treturn x;\n\t}\n};\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tint n;\n\tcin >> n;\n\tvector<int> a(n);\n\tcin >> a;\n\n\tint q;\n\tcin >> q;\n\tvector<int> l(q), r(q);\n\tfor (int i = 0; i < q; ++i) {\n\t\tcin >> l[i] >> r[i];\n\t\t--l[i];\n\t}\n\n\tconst int B = max<int>(1, n / sqrt(2.0 * q / 3));\n\tvector<int> idx(q);\n\tiota(idx.begin(), idx.end(), 0);\n\tranges::sort(idx, [&](int i, int j) {\n\t\tif (l[i] / B == l[j] / B)return r[i] < r[j];\n\t\telse return l[i] < l[j];\n\t\t});\n\n\tvector<vector<int>> factor(n);\n\tfor (int i = 0; i < n; ++i) {\n\t\tfor (int j = 1; j * j <= a[i]; ++j) {\n\t\t\tif (a[i] % j == 0) {\n\t\t\t\tfactor[i].push_back(j);\n\t\t\t\tif (j * j != a[i])factor[i].push_back(a[i] / j);\n\t\t\t}\n\t\t}\n\t}\n\n\tvector<int> cnt(1e5 + 1);\n\n\tBIT seg(1e5 + 1);\n\n\tint ssize = 0;\n\tint nl = 0, nr = 0;\n\tvector<int> res(q);\n\tfor (int i : idx) {\n\t\twhile (l[i] < nl) {\n\t\t\t--nl;\n\t\t\tfor (auto d : factor[nl]) {\n\t\t\t\tcnt[d]++;\n\t\t\t\tif (cnt[d] == 2) {\n\t\t\t\t\tseg.add(d, 1);\n\t\t\t\t\t++ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile (nr < r[i]) {\n\t\t\tfor (auto d : factor[nr]) {\n\t\t\t\tcnt[d]++;\n\t\t\t\tif (cnt[d] == 2) {\n\t\t\t\t\tseg.add(d, 1);\n\t\t\t\t\t++ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t\tnr++;\n\t\t}\n\t\twhile (nl < l[i]) {\n\t\t\tfor (auto d : factor[nl]) {\n\t\t\t\tcnt[d]--;\n\t\t\t\tif (cnt[d] == 1) {\n\t\t\t\t\tseg.add(d, -1);\n\t\t\t\t\t--ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t\t++nl;\n\t\t}\n\t\twhile (r[i] < nr) {\n\t\t\t++nr;\n\t\t\tfor (auto d : factor[nr]) {\n\t\t\t\tcnt[d]--;\n\t\t\t\tif (cnt[d] == 1) {\n\t\t\t\t\tseg.add(d, -1);\n\t\t\t\t\t--ssize;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint j = seg.lower_bound(ssize);\n\t\tres[i] = j;\n\t}\n\n\tfor (auto x : res)cout << x << endl;\n\n\treturn 0;\n}", "accuracy": 0.018867924528301886, "time_ms": 120, "memory_kb": 60416, "score_of_the_acc": -0.2216, "final_rank": 19 }, { "submission_id": "aoj_1463_10469340", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class S, class T> inline bool chmax(S &a, T b) { return (a < b ? a = b, 1 : 0); }\ntemplate<class S, class T> inline bool chmin(S &a, T b) { return (a > b ? a = b, 1 : 0); }\n\nusing pint = pair<int, int>;\nusing pll = pair<long long, long long>;\nusing tint = array<int, 3>;\nusing tll = array<long long, 3>;\nusing fint = array<int, 4>;\nusing fll = array<long long, 4>;\nusing vll = vector<long long>;\nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing int128 = __int128;\nusing u128 = unsigned __int128;\ntemplate <class T>\nusing min_priority_queue = priority_queue<T, vector<T>, greater<T>>;\n\n#define REP(i, a) for (long long i = 0; i < (long long)(a); i++)\n#define REP2(i, a, b) for (long long i = a; i < (long long)(b); i++)\n#define RREP(i, a) for (long long i = (a)-1; i >= (long long)(0); --i)\n#define RREP2(i, a, b) for (long long i = (b)-1; i >= (long long)(a); --i)\n#define EB emplace_back\n#define PB push_back\n#define MP make_pair\n#define MT make_tuple\n#define FI first\n#define SE second\n#define ALL(x) x.begin(), x.end()\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\n// debug stream\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, deque<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for (auto it : P) { s << \"<\" << it << \"> \"; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, multiset<T> P)\n{ for (auto it : P) { s << \"<\" << it << \"> \"; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for (auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s; }\n\n\n\n// BIT\ntemplate <class Abel> struct BIT {\n Abel UNITY_SUM = 0;\n vector<Abel> dat;\n \n // [0, n)\n BIT(int n, Abel unity = 0) : UNITY_SUM(unity), dat(n, unity) { }\n void init(int n) {\n dat.assign(n, UNITY_SUM);\n }\n \n // a is 0-indexed\n inline void add(int a, Abel x) {\n for (int i = a; i < (int)dat.size(); i |= i + 1)\n dat[i] = dat[i] + x;\n }\n \n // [0, a), a is 0-indexed\n inline Abel sum(int a) {\n Abel res = UNITY_SUM;\n for (int i = a - 1; i >= 0; i = (i & (i + 1)) - 1)\n res = res + dat[i];\n return res;\n }\n \n // [a, b), a and b are 0-indexed\n inline Abel sum(int a, int b) {\n return sum(b) - sum(a);\n }\n \n // debug\n void print() {\n for (int i = 0; i < (int)dat.size(); ++i)\n cout << sum(i, i + 1) << \",\";\n cout << endl;\n }\n};\n\n// Segment Tree\ntemplate<class Monoid> struct SegmentTree {\n using Func = function<Monoid(Monoid, Monoid)>;\n\n // core member\n int N;\n Func OP;\n Monoid IDENTITY;\n \n // inner data\n int log, offset;\n vector<Monoid> dat;\n\n // constructor\n SegmentTree() {}\n SegmentTree(int n, const Func &op, const Monoid &identity) {\n init(n, op, identity);\n }\n SegmentTree(const vector<Monoid> &v, const Func &op, const Monoid &identity) {\n init(v, op, identity);\n }\n void init(int n, const Func &op, const Monoid &identity) {\n N = n;\n OP = op;\n IDENTITY = identity;\n log = 0, offset = 1;\n while (offset < N) ++log, offset <<= 1;\n dat.assign(offset * 2, IDENTITY);\n }\n void init(const vector<Monoid> &v, const Func &op, const Monoid &identity) {\n init((int)v.size(), op, identity);\n build(v);\n }\n void pull(int k) {\n dat[k] = OP(dat[k * 2], dat[k * 2 + 1]);\n }\n void build(const vector<Monoid> &v) {\n assert(N == (int)v.size());\n for (int i = 0; i < N; ++i) dat[i + offset] = v[i];\n for (int k = offset - 1; k > 0; --k) pull(k);\n }\n int size() const {\n return N;\n }\n Monoid operator [] (int i) const {\n return dat[i + offset];\n }\n \n // update A[i], i is 0-indexed, O(log N)\n void set(int i, const Monoid &v) {\n assert(0 <= i && i < N);\n int k = i + offset;\n dat[k] = v;\n while (k >>= 1) pull(k);\n }\n \n // get [l, r), l and r are 0-indexed, O(log N)\n Monoid prod(int l, int r) {\n assert(0 <= l && l <= r && r <= N);\n Monoid val_left = IDENTITY, val_right = IDENTITY;\n l += offset, r += offset;\n for (; l < r; l >>= 1, r >>= 1) {\n if (l & 1) val_left = OP(val_left, dat[l++]);\n if (r & 1) val_right = OP(dat[--r], val_right);\n }\n return OP(val_left, val_right);\n }\n Monoid all_prod() {\n return dat[1];\n }\n \n // get max r that f(get(l, r)) = True (0-indexed), O(log N)\n // f(IDENTITY) need to be True\n int max_right(const function<bool(Monoid)> f, int l = 0) {\n if (l == N) return N;\n l += offset;\n Monoid sum = IDENTITY;\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(OP(sum, dat[l]))) {\n while (l < offset) {\n l = l * 2;\n if (f(OP(sum, dat[l]))) {\n sum = OP(sum, dat[l]);\n ++l;\n }\n }\n return l - offset;\n }\n sum = OP(sum, dat[l]);\n ++l;\n } while ((l & -l) != l); // stop if l = 2^e\n return N;\n }\n\n // get min l that f(get(l, r)) = True (0-indexed), O(log N)\n // f(IDENTITY) need to be True\n int min_left(const function<bool(Monoid)> f, int r = -1) {\n if (r == 0) return 0;\n if (r == -1) r = N;\n r += offset;\n Monoid sum = IDENTITY;\n do {\n --r;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(OP(dat[r], sum))) {\n while (r < offset) {\n r = r * 2 + 1;\n if (f(OP(dat[r], sum))) {\n sum = OP(dat[r], sum);\n --r;\n }\n }\n return r + 1 - offset;\n }\n sum = OP(dat[r], sum);\n } while ((r & -r) != r);\n return 0;\n }\n \n // debug\n friend ostream& operator << (ostream &s, const SegmentTree &seg) {\n for (int i = 0; i < (int)seg.size(); ++i) {\n s << seg[i];\n if (i != (int)seg.size() - 1) s << \" \";\n }\n return s;\n }\n};\n\n// Lazy Segment Tree\ntemplate<class Monoid, class Action> struct LazySegmentTree {\n // various function types\n using FuncMonoid = function<Monoid(Monoid, Monoid)>;\n using FuncAction = function<Monoid(Action, Monoid)>;\n using FuncComposition = function<Action(Action, Action)>;\n\n // core member\n int N;\n FuncMonoid OP;\n FuncAction ACT;\n FuncComposition COMP;\n Monoid IDENTITY_MONOID;\n Action IDENTITY_ACTION;\n \n // inner data\n int log, offset;\n vector<Monoid> dat;\n vector<Action> lazy;\n \n // constructor\n LazySegmentTree() {}\n LazySegmentTree(int n, const FuncMonoid op, const FuncAction act, const FuncComposition comp,\n const Monoid &identity_monoid, const Action &identity_action) {\n init(n, op, act, comp, identity_monoid, identity_action);\n }\n LazySegmentTree(const vector<Monoid> &v,\n const FuncMonoid op, const FuncAction act, const FuncComposition comp,\n const Monoid &identity_monoid, const Action &identity_action) {\n init(v, op, act, comp, identity_monoid, identity_action);\n }\n void init(int n, const FuncMonoid op, const FuncAction act, const FuncComposition comp,\n const Monoid &identity_monoid, const Action &identity_action) {\n N = n, OP = op, ACT = act, COMP = comp;\n IDENTITY_MONOID = identity_monoid, IDENTITY_ACTION = identity_action;\n log = 0, offset = 1;\n while (offset < N) ++log, offset <<= 1;\n dat.assign(offset * 2, IDENTITY_MONOID);\n lazy.assign(offset * 2, IDENTITY_ACTION);\n }\n void init(const vector<Monoid> &v,\n const FuncMonoid op, const FuncAction act, const FuncComposition comp,\n const Monoid &identity_monoid, const Action &identity_action) {\n init((int)v.size(), op, act, comp, identity_monoid, identity_action);\n build(v);\n }\n void build(const vector<Monoid> &v) {\n assert(N == (int)v.size());\n for (int i = 0; i < N; ++i) dat[i + offset] = v[i];\n for (int k = offset - 1; k > 0; --k) pull_dat(k);\n }\n int size() const {\n return N;\n }\n \n // basic functions for lazy segment tree\n void pull_dat(int k) {\n dat[k] = OP(dat[k * 2], dat[k * 2 + 1]);\n }\n void apply_lazy(int k, const Action &f) {\n dat[k] = ACT(f, dat[k]);\n if (k < offset) lazy[k] = COMP(f, lazy[k]);\n }\n void push_lazy(int k) {\n apply_lazy(k * 2, lazy[k]);\n apply_lazy(k * 2 + 1, lazy[k]);\n lazy[k] = IDENTITY_ACTION;\n }\n void pull_dat_deep(int k) {\n for (int h = 1; h <= log; ++h) pull_dat(k >> h);\n }\n void push_lazy_deep(int k) {\n for (int h = log; h >= 1; --h) push_lazy(k >> h);\n }\n \n // setter and getter, update A[i], i is 0-indexed, O(log N)\n void set(int i, const Monoid &v) {\n assert(0 <= i && i < N);\n int k = i + offset;\n push_lazy_deep(k);\n dat[k] = v;\n pull_dat_deep(k);\n }\n Monoid get(int i) {\n assert(0 <= i && i < N);\n int k = i + offset;\n push_lazy_deep(k);\n return dat[k];\n }\n Monoid operator [] (int i) {\n return get(i);\n }\n \n // apply f for index i\n void apply(int i, const Action &f) {\n assert(0 <= i && i < N);\n int k = i + offset;\n push_lazy_deep(k);\n dat[k] = ACT(f, dat[k]);\n pull_dat_deep(k);\n }\n // apply f for interval [l, r)\n void apply(int l, int r, const Action &f) {\n assert(0 <= l && l <= r && r <= N);\n if (l == r) return;\n l += offset, r += offset;\n for (int h = log; h >= 1; --h) {\n if (((l >> h) << h) != l) push_lazy(l >> h);\n if (((r >> h) << h) != r) push_lazy((r - 1) >> h);\n }\n int original_l = l, original_r = r;\n for (; l < r; l >>= 1, r >>= 1) {\n if (l & 1) apply_lazy(l++, f);\n if (r & 1) apply_lazy(--r, f);\n }\n l = original_l, r = original_r;\n for (int h = 1; h <= log; ++h) {\n if (((l >> h) << h) != l) pull_dat(l >> h);\n if (((r >> h) << h) != r) pull_dat((r - 1) >> h);\n }\n }\n \n // get prod of interval [l, r)\n Monoid prod(int l, int r) {\n assert(0 <= l && l <= r && r <= N);\n if (l == r) return IDENTITY_MONOID;\n l += offset, r += offset;\n for (int h = log; h >= 1; --h) {\n if (((l >> h) << h) != l) push_lazy(l >> h);\n if (((r >> h) << h) != r) push_lazy(r >> h);\n }\n Monoid val_left = IDENTITY_MONOID, val_right = IDENTITY_MONOID;\n for (; l < r; l >>= 1, r >>= 1) {\n if (l & 1) val_left = OP(val_left, dat[l++]);\n if (r & 1) val_right = OP(dat[--r], val_right);\n }\n return OP(val_left, val_right);\n }\n Monoid all_prod() {\n return dat[1];\n }\n \n // get max r that f(get(l, r)) = True (0-indexed), O(log N)\n // f(IDENTITY) need to be True\n int max_right(const function<bool(Monoid)> f, int l = 0) {\n if (l == N) return N;\n l += offset;\n push_lazy_deep(l);\n Monoid sum = IDENTITY_MONOID;\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(OP(sum, dat[l]))) {\n while (l < offset) {\n push_lazy(l);\n l = l * 2;\n if (f(OP(sum, dat[l]))) {\n sum = OP(sum, dat[l]);\n ++l;\n }\n }\n return l - offset;\n }\n sum = OP(sum, dat[l]);\n ++l;\n } while ((l & -l) != l); // stop if l = 2^e\n return N;\n }\n\n // get min l that f(get(l, r)) = True (0-indexed), O(log N)\n // f(IDENTITY) need to be True\n int min_left(const function<bool(Monoid)> f, int r = -1) {\n if (r == 0) return 0;\n if (r == -1) r = N;\n r += offset;\n push_lazy_deep(r - 1);\n Monoid sum = IDENTITY_MONOID;\n do {\n --r;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(OP(dat[r], sum))) {\n while (r < offset) {\n push_lazy(r);\n r = r * 2 + 1;\n if (f(OP(dat[r], sum))) {\n sum = OP(dat[r], sum);\n --r;\n }\n }\n return r + 1 - offset;\n }\n sum = OP(dat[r], sum);\n } while ((r & -r) != r);\n return 0;\n }\n \n // debug stream\n friend ostream& operator << (ostream &s, LazySegmentTree seg) {\n for (int i = 0; i < (int)seg.size(); ++i) {\n s << seg[i];\n if (i != (int)seg.size() - 1) s << \" \";\n }\n return s;\n }\n \n // dump\n void dump() {\n for (int i = 0; i <= log; ++i) {\n for (int j = (1 << i); j < (1 << (i + 1)); ++j) {\n cout << \"{\" << dat[j] << \",\" << lazy[j] << \"} \";\n }\n cout << endl;\n }\n }\n};\n\n\n\nstruct Node {\n vector<int> vec;\n int mex;\n Node() : mex(0) {}\n Node(const vector<int> &vec, int mex) : vec(vec), mex(mex) {}\n friend ostream& operator << (ostream &s, const Node &node) {\n return s << \"(\" << node.vec << \", \" << node.mex << \")\";\n }\n};\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n int N, Q;\n cin >> N;\n vector<int> A(N);\n REP(i, N) cin >> A[i];\n cin >> Q;\n vector<tint> qs(Q);\n REP(qid, Q) {\n cin >> qs[qid][0] >> qs[qid][1];\n qs[qid][0]--;\n qs[qid][2] = qid;\n }\n sort(qs.begin(), qs.end(), [&](tint a, tint b){return a[1] < b[1];});\n\n const int MAX = 110000;\n vector<pint> leftist(MAX, pint(-1, -1));\n\n // l 以上となる最大の v を求めたい!!\n SegmentTree<int> seg(MAX, [&](int a, int b){return max(a, b);}, -1);\n\n auto push = [&](int val, int id) {\n int before = leftist[val].second;\n if (id > leftist[val].first) {\n leftist[val].second = leftist[val].first;\n leftist[val].first = id;\n } else if (id > leftist[val].second) {\n leftist[val].second = id;\n }\n int after = leftist[val].second;\n seg.set(val, after);\n };\n\n int rid = 0;\n vector<int> res(Q);\n for (auto [l_, r, qid] : qs) {\n int l = l_;\n while (rid < r) {\n for (ll v = 1; v * v <= A[rid]; v++) {\n if (A[rid] % v == 0) {\n push(v, rid);\n if (v != A[rid] / v) push(A[rid] / v, rid);\n }\n }\n rid++;\n }\n\n auto func = [&](int val) -> bool {return val < l;};\n int vl = seg.min_left(func);\n res[qid] = vl-1;\n\n //cout << \"------\" << endl; COUT(l); COUT(r); COUT(qid); COUT(seg); COUT(vl);\n }\n for (auto val : res) cout << val << '\\n';\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 7008, "score_of_the_acc": -0.2695, "final_rank": 5 }, { "submission_id": "aoj_1463_10329178", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class S, class T> inline bool chmax(S &a, T b) { return (a < b ? a = b, 1 : 0); }\ntemplate<class S, class T> inline bool chmin(S &a, T b) { return (a > b ? a = b, 1 : 0); }\n\nusing pint = pair<int, int>;\nusing pll = pair<long long, long long>;\nusing tint = array<int, 3>;\nusing tll = array<long long, 3>;\nusing fint = array<int, 4>;\nusing fll = array<long long, 4>;\nusing vll = vector<long long>;\nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing int128 = __int128;\nusing u128 = unsigned __int128;\ntemplate <class T>\nusing min_priority_queue = priority_queue<T, vector<T>, greater<T>>;\n\n#define REP(i, a) for (long long i = 0; i < (long long)(a); i++)\n#define REP2(i, a, b) for (long long i = a; i < (long long)(b); i++)\n#define RREP(i, a) for (long long i = (a)-1; i >= (long long)(0); --i)\n#define RREP2(i, a, b) for (long long i = (b)-1; i >= (long long)(a); --i)\n#define EB emplace_back\n#define PB push_back\n#define MP make_pair\n#define MT make_tuple\n#define FI first\n#define SE second\n#define ALL(x) x.begin(), x.end()\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\n// debug stream\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)\n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, deque<T> P)\n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, vector<vector<T> > P)\n{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }\ntemplate<class T> ostream& operator << (ostream &s, set<T> P)\n{ for (auto it : P) { s << \"<\" << it << \"> \"; } return s; }\ntemplate<class T> ostream& operator << (ostream &s, multiset<T> P)\n{ for (auto it : P) { s << \"<\" << it << \"> \"; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)\n{ for (auto it : P) { s << \"<\" << it.first << \"->\" << it.second << \"> \"; } return s; }\n\n\n\n// Segment Tree\ntemplate<class Monoid> struct SegmentTree {\n using Func = function<Monoid(Monoid, Monoid)>;\n\n // core member\n int N;\n Func OP;\n Monoid IDENTITY;\n \n // inner data\n int log, offset;\n vector<Monoid> dat;\n\n // constructor\n SegmentTree() {}\n SegmentTree(int n, const Func &op, const Monoid &identity) {\n init(n, op, identity);\n }\n SegmentTree(const vector<Monoid> &v, const Func &op, const Monoid &identity) {\n init(v, op, identity);\n }\n void init(int n, const Func &op, const Monoid &identity) {\n N = n;\n OP = op;\n IDENTITY = identity;\n log = 0, offset = 1;\n while (offset < N) ++log, offset <<= 1;\n dat.assign(offset * 2, IDENTITY);\n }\n void init(const vector<Monoid> &v, const Func &op, const Monoid &identity) {\n init((int)v.size(), op, identity);\n build(v);\n }\n void pull(int k) {\n dat[k] = OP(dat[k * 2], dat[k * 2 + 1]);\n }\n void build(const vector<Monoid> &v) {\n assert(N == (int)v.size());\n for (int i = 0; i < N; ++i) dat[i + offset] = v[i];\n for (int k = offset - 1; k > 0; --k) pull(k);\n }\n int size() const {\n return N;\n }\n Monoid operator [] (int i) const {\n return dat[i + offset];\n }\n \n // update A[i], i is 0-indexed, O(log N)\n void set(int i, const Monoid &v) {\n assert(0 <= i && i < N);\n int k = i + offset;\n dat[k] = v;\n while (k >>= 1) pull(k);\n }\n \n // get [l, r), l and r are 0-indexed, O(log N)\n Monoid prod(int l, int r) {\n assert(0 <= l && l <= r && r <= N);\n Monoid val_left = IDENTITY, val_right = IDENTITY;\n l += offset, r += offset;\n for (; l < r; l >>= 1, r >>= 1) {\n if (l & 1) val_left = OP(val_left, dat[l++]);\n if (r & 1) val_right = OP(dat[--r], val_right);\n }\n return OP(val_left, val_right);\n }\n Monoid all_prod() {\n return dat[1];\n }\n \n // get max r that f(get(l, r)) = True (0-indexed), O(log N)\n // f(IDENTITY) need to be True\n int max_right(const function<bool(Monoid)> f, int l = 0) {\n if (l == N) return N;\n l += offset;\n Monoid sum = IDENTITY;\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(OP(sum, dat[l]))) {\n while (l < offset) {\n l = l * 2;\n if (f(OP(sum, dat[l]))) {\n sum = OP(sum, dat[l]);\n ++l;\n }\n }\n return l - offset;\n }\n sum = OP(sum, dat[l]);\n ++l;\n } while ((l & -l) != l); // stop if l = 2^e\n return N;\n }\n\n // get min l that f(get(l, r)) = True (0-indexed), O(log N)\n // f(IDENTITY) need to be True\n int min_left(const function<bool(Monoid)> f, int r = -1) {\n if (r == 0) return 0;\n if (r == -1) r = N;\n r += offset;\n Monoid sum = IDENTITY;\n do {\n --r;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(OP(dat[r], sum))) {\n while (r < offset) {\n r = r * 2 + 1;\n if (f(OP(dat[r], sum))) {\n sum = OP(dat[r], sum);\n --r;\n }\n }\n return r + 1 - offset;\n }\n sum = OP(dat[r], sum);\n } while ((r & -r) != r);\n return 0;\n }\n \n // debug\n friend ostream& operator << (ostream &s, const SegmentTree &seg) {\n for (int i = 0; i < (int)seg.size(); ++i) {\n s << seg[i];\n if (i != (int)seg.size() - 1) s << \" \";\n }\n return s;\n }\n};\n\n\n/*/////////////////////////////*/\n// Solver\n/*/////////////////////////////*/\n\nconst int MAX = 110000;\nint main() {\n int N, Q;\n cin >> N;\n vector<ll> A(N);\n REP(i, N) cin >> A[i];\n cin >> Q;\n vector<ll> L(Q), R(Q);\n REP(i, Q) cin >> L[i] >> R[i], L[i]--, R[i]--;\n vector<vector<pll>> LR(MAX);\n REP(i, Q) LR[L[i]].EB(R[i], i);\n \n const ll INF = 1LL << 60;\n auto op = [&](ll p, ll q) -> ll { return min(p, q); };\n SegmentTree<ll> seg1(MAX, op, INF), seg2(MAX, op, INF);\n\n vector<ll> res(Q);\n RREP(l, N) {\n vector<ll> div;\n for (ll a = 1; a*a <= A[l]; a++) {\n if (A[l] % a == 0) {\n div.EB(a);\n if (a * a != A[l]) div.EB(A[l] / a);\n }\n }\n for (auto d : div) {\n if (d >= MAX) continue;\n auto id1 = seg1[d], id2 = seg2[d];\n seg2.set(d, id1);\n seg1.set(d, l);\n }\n\n // cout << \"--------------=\" << endl;\n // COUT(l);\n // COUT(seg1);\n // COUT(seg2);\n\n for (const auto p : LR[l]) {\n // find max v such that seg2.prod(v, MAX) <= r\n // get min l that f(get(l, r)) = True (0-indexed), O(log N)\n // f(IDENTITY) need to be True\n // int min_left(const function<bool(Monoid)> f, int r = -1) {\n auto l = seg2.min_left([&](ll val) -> bool {return val > p.first; });\n res[p.second] = l-1;\n\n // cout << p << \": \" << l-1 << endl;\n }\n }\n for (auto v : res) cout << v << '\\n';\n}", "accuracy": 1, "time_ms": 1230, "memory_kb": 15884, "score_of_the_acc": -0.5031, "final_rank": 8 }, { "submission_id": "aoj_1463_10217842", "code_snippet": "// AOJ #1463\n// Greatest of the Greatest Common Divisors 2025.1.14\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(int n) { // 整数の表示(出力)\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nconst int MAXVAL = 100000;\n\nstruct BIT {\n int n;\n vector<int> tree;\n BIT(int n) : n(n) {\n tree.assign(n+1, 0);\n }\n void update(int i, int val) {\n for(; i <= n; i += i & -i)\n tree[i] = max(tree[i], val);\n }\n int query(int i) {\n int res = 0;\n for(; i; i -= i & -i)\n res = max(res, tree[i]);\n return res;\n }\n};\n \nstruct Event { int q, p, d; };\n \nstruct EventComparator {\n bool operator()(const Event &a, const Event &b) const {\n return a.q < b.q;\n }\n};\n \nstruct Query { int l, r, idx; };\n \nstruct QueryComparator {\n bool operator()(const Query &a, const Query &b) const {\n return a.r < b.r;\n }\n};\n \nint main(){\n int n = Cin(); \n vector<int> arr(n+1);\n for (int i = 1; i <= n; i++) arr[i] = Cin();\n\n vector<vector<int>> divisors(MAXVAL+1);\n for (int d = 1; d <= MAXVAL; d++){\n for (int mult = d; mult <= MAXVAL; mult += d)\n divisors[mult].push_back(d);\n }\n \n vector<vector<int>> posOfDiv(MAXVAL+1);\n for (int i = 1; i <= n; i++){\n int x = arr[i];\n for (int d : divisors[x])\n posOfDiv[d].push_back(i);\n }\n \n vector<Event> events;\n events.reserve(n * 40);\n for (int d = 1; d <= MAXVAL; d++){\n if(posOfDiv[d].size() >= 2){\n for (size_t j = 0; j+1 < posOfDiv[d].size(); j++){\n Event ev;\n ev.p = posOfDiv[d][j];\n ev.q = posOfDiv[d][j+1];\n ev.d = d;\n events.push_back(ev);\n }\n }\n }\n sort(events.begin(), events.end(), EventComparator());\n \n int q = Cin();\n vector<Query> queries(q);\n for (int i = 0; i < q; i++){\n int l = Cin(), r = Cin();\n queries[i] = {l, r, i};\n }\n sort(queries.begin(), queries.end(), QueryComparator());\n \n BIT bit(n);\n \n vector<int> ans(q, 0);\n int evIndex = 0;\n int eventsSize = events.size();\n for(auto &qu : queries){\n while(evIndex < eventsSize && events[evIndex].q <= qu.r) {\n int pos = events[evIndex].p;\n int cand = events[evIndex].d;\n int idxInBIT = n - pos + 1;\n bit.update(idxInBIT, cand);\n evIndex++;\n }\n int best = bit.query(n - qu.l + 1);\n ans[qu.idx] = best;\n }\n for (int i = 0; i < q; i++) Cout(ans[i]);\n return 0;\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 256884, "score_of_the_acc": -1.2637, "final_rank": 14 }, { "submission_id": "aoj_1463_10217840", "code_snippet": "// AOJ #1463\n// Greatest of the Greatest Common Divisors 2025.1.14\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXVAL = 100000;\n\nstruct BIT {\n int n;\n vector<int> tree;\n BIT(int n) : n(n) {\n tree.assign(n+1, 0);\n }\n void update(int i, int val) {\n for(; i <= n; i += i & -i)\n tree[i] = max(tree[i], val);\n }\n int query(int i) {\n int res = 0;\n for(; i; i -= i & -i)\n res = max(res, tree[i]);\n return res;\n }\n};\n \nstruct Event { int q, p, d; };\n \nstruct EventComparator {\n bool operator()(const Event &a, const Event &b) const {\n return a.q < b.q;\n }\n};\n \nstruct Query { int l, r, idx; };\n \nstruct QueryComparator {\n bool operator()(const Query &a, const Query &b) const {\n return a.r < b.r;\n }\n};\n \nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n int n; \n cin >> n;\n vector<int> arr(n+1);\n for (int i = 1; i <= n; i++) cin >> arr[i];\n\n vector<vector<int>> divisors(MAXVAL+1);\n for (int d = 1; d <= MAXVAL; d++){\n for (int mult = d; mult <= MAXVAL; mult += d)\n divisors[mult].push_back(d);\n }\n \n vector<vector<int>> posOfDiv(MAXVAL+1);\n for (int i = 1; i <= n; i++){\n int x = arr[i];\n for (int d : divisors[x])\n posOfDiv[d].push_back(i);\n }\n \n vector<Event> events;\n events.reserve(n * 40);\n for (int d = 1; d <= MAXVAL; d++){\n if(posOfDiv[d].size() >= 2){\n for (size_t j = 0; j+1 < posOfDiv[d].size(); j++){\n Event ev;\n ev.p = posOfDiv[d][j];\n ev.q = posOfDiv[d][j+1];\n ev.d = d;\n events.push_back(ev);\n }\n }\n }\n sort(events.begin(), events.end(), EventComparator());\n \n int q;\n cin >> q;\n vector<Query> queries(q);\n for (int i = 0; i < q; i++){\n int l, r;\n cin >> l >> r;\n queries[i] = {l, r, i};\n }\n sort(queries.begin(), queries.end(), QueryComparator());\n \n BIT bit(n);\n \n vector<int> ans(q, 0);\n int evIndex = 0;\n int eventsSize = events.size();\n for(auto &qu : queries){\n while(evIndex < eventsSize && events[evIndex].q <= qu.r) {\n int pos = events[evIndex].p;\n int cand = events[evIndex].d;\n int idxInBIT = n - pos + 1;\n bit.update(idxInBIT, cand);\n evIndex++;\n }\n int best = bit.query(n - qu.l + 1);\n ans[qu.idx] = best;\n }\n for (int i = 0; i < q; i++){\n cout << ans[i] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 770, "memory_kb": 258124, "score_of_the_acc": -1.2769, "final_rank": 15 }, { "submission_id": "aoj_1463_10146297", "code_snippet": "#include<bits/stdc++.h>\n#include<bits/extc++.h>\n \n#define F first\n#define S second\n#define pb push_back\n#define pob pop_back\n#define pf push_front\n#define pof pop_front\n#define mp make_pair\n#define mt make_tuple\n#define all(x) (x).begin(),(x).end()\n#define mem(x,i) memset((x),(i),sizeof((x)))\n \nusing namespace std;\n//using namespace __gnu_pbds;\nusing pii = pair<long long,long long>;\nusing ld = long double;\nusing ll = long long;\n \nmt19937 mtrd(chrono::steady_clock::now().time_since_epoch().count());\n \nconst int mod = 1000000007;\nconst int mod2 = 998244353;\nconst ld PI = acos(-1);\n \n#define Bint __int128\n#define int long long\n\nnamespace DEBUG{ \n\ttemplate <typename T, typename T2>\n\tostream& operator<<(ostream& os, const pair<T, T2>& pr) {\n\t\tos << \"( \" << pr.first << \", \" << pr.second << \")\";\n\t\treturn os;\n\t}\n\n\ttemplate <typename T>\n\tinline void printv(T l, T r){\n\t\tcerr << \"DEBUG: [ \";\n\t\tfor(; l != r; l++)\n\t\t\tcerr << *l << \", \";\n\t\tcerr << \"]\" << endl;\n\t}\n\t\n\ttemplate <typename T>\n\tinline void _debug(const char* format, T t) {\n\t\tcerr << format << '=' << t << endl;\n\t}\n\t\n\ttemplate <class First, class... Rest>\n\tinline void _debug(const char* format, First first, Rest... rest) {\n\t\twhile (*format != ',')\n\t\t\tcerr << *format++;\n\t\tcerr << '=' << first << \",\";\n\t\t_debug(format + 1, rest...);\n\t}\n\n//\t#define TEST\n\t#ifdef TEST\n\t#define debug(...) cerr << \"DEBUG: \",_debug(#__VA_ARGS__, __VA_ARGS__)\n\t#else\n\t#define debug(...) void(0)\n\t#define printv(...) void(0)\n\t#endif\n} // namespace DEBUG\n\nusing namespace DEBUG;\n\n/* ---------------------------------------- */\nconst int N = 1e5 + 10;\nconst int inf = 1e18 + 10;\n\nstruct segtree{\n\tint seg[N << 2];\t\n\tvoid upd(int l, int r, int p, int v, int idx = 1){\n\t\tif(l == r){\n\t\t\tseg[idx] = v;\n\t\t\treturn;\n\t\t}\n\t\tint mid = (l + r) >> 1;\n\t\tif(p <= mid)\n\t\t\tupd(l, mid, p, v, idx << 1);\n\t\telse\n\t\t\tupd(mid + 1, r, p, v, idx << 1 | 1);\n\t\tseg[idx] = max(seg[idx << 1], seg[idx << 1 | 1]);\n\t}\n\n\tint query(int l, int r, int ql, int qr, int idx = 1){\n\t\tif(ql == l && qr == r)\n\t\t\treturn seg[idx];\n\t\tint mid = (l + r) >> 1;\n\t\tif(qr <= mid)\n\t\t\treturn query(l, mid, ql, qr, idx << 1);\n\t\telse if(ql > mid)\n\t\t\treturn query(mid + 1, r, ql, qr, idx << 1 | 1);\n\t\treturn max(query(l, mid, ql, mid, idx << 1), query(mid + 1, r, mid + 1, qr, idx << 1 | 1));\n\t}\n\n\tsegtree(){\n\t\tfor(int i = 0; i < (N << 2); i++)\n\t\t\tseg[i] = -inf;\n\t}\n}sgt;\n\nint n, q;\nint a[N];\nint ans[N];\nint llst[N];\n\nvoid upd(int idx, int num){\n\tif(llst[num] != -1)\n\t\tsgt.upd(1, N - 1, num, llst[num]), debug(num, idx);\t\n\tllst[num] = idx;\n}\n\nvoid solve(){\n\tcin >> n;\n\tfor(int i = 0; i < N; i++)\n\t\tllst[i] = -1;\n\tfor(int i = 0; i < n; i++)\n\t\tcin >> a[i];\n\tint p = 0;\n\tcin >> q;\n\tvector<array<int, 3>>query(q);\n\tfor(auto &[r, l, id] : query){\n\t\tcin >> l >> r;\n\t\tl--, r--;\n\t\tid = p++;\n\t}\n\tsort(all(query));\n\tp = 0;\n\tfor(auto [r, l, id] : query){\n\t\twhile(p <= r){\n\t\t\tfor(int i = 1; i * i <= a[p]; i++){\n\t\t\t\tif(a[p] % i == 0){\n\t\t\t\t\tupd(p, i);\t\n\t\t\t\t\tif(a[p] / i != i)\n\t\t\t\t\t\tupd(p, a[p] / i);\n\t\t\t\t}\n\t\t\t}\n\t\t\tp++;\n\t\t}\n\t\tint L = 1, R = N;\n\t\twhile(L < R){\n\t\t\tint mid = (L + R + 1) >> 1;\n\t\t\tif(sgt.query(1, N - 1, mid, N - 1) >= l)\n\t\t\t\tL = mid;\n\t\t\telse\n\t\t\t\tR = mid - 1;\n\t\t}\n\t\tans[id] = R;\n\t}\n\tfor(int i = 0; i < q; i++)\n\t\tcout << ans[i] << '\\n';\n}\n \nsigned main(){\n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\tint t = 1; \n\t// cin >> t;\n\twhile(t--)\n\t\tsolve();\n}", "accuracy": 1, "time_ms": 640, "memory_kb": 10984, "score_of_the_acc": -0.2399, "final_rank": 4 }, { "submission_id": "aoj_1463_10144538", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define int ll\n#define pii pair<int, int>\n#define X first\n#define Y second\n#define F first\n#define S second\n#define vi vector<int>\n#define SZ(a) ((int)a.size())\n#define ALL(v) v.begin(), v.end()\n#define pb push_back\n#define eb emplace_back\n#define push emplace\n#define lb(x, v) lower_bound(ALL(x), v)\n#define ub(x, v) upper_bound(ALL(x), v)\n#define re(x) reverse(ALL(x))\n#define uni(x) x.resize(unique(ALL(x)) - x.begin())\n#define inf 1000000000\n#define INF 1000000000000000000\n#define mod 1000000007\n#define MOD 998244353\n#define get_bit(x, y) ((x>>y)&1)\n#define mkp make_pair\n#define IO ios_base::sync_with_stdio(0); cin.tie(0);\nvoid abc() {cerr << endl;}\ntemplate <typename T, typename ...U> void abc(T a, U ...b) {\n cerr << a << ' ', abc(b...);\n}\n#ifdef debug\n#define test(args...) abc(\"[\" + string(#args) + \"]\", args)\n#else\n#define test(args...) void(0)\n#endif\n\ntemplate<class T> bool ckmin(T& a, const T& b) { return b<a ? a=b, 1 : 0; }\ntemplate<class T> bool ckmax(T& a, const T& b) { return a<b ? a=b, 1 : 0; }\n\nstruct Segment_Tree {\n#define ls (x<<1)\n#define rs ((x<<1)|1)\n#define mid ((l+r)>>1)\n int n;\n vector<int> seg;\n Segment_Tree(int _n): n(_n), seg(_n * 4) {}\n\n inline int op(const int &a, const int &b) {\n return max(a, b);\n }\n\n void up(int x) {\n seg[x] = op(seg[ls], seg[rs]);\n }\n\n void modify(int p, int v, int l, int r, int x) {\n if(l == r) {\n seg[x] = max(seg[x], v);\n return;\n }\n if(p <= mid) modify(p, v, l, mid, ls);\n else modify(p, v, mid+1, r, rs);\n up(x);\n }\n void modify(int p, int v) { modify(p, v, 0, n, 1); }\n\n int query(int a, int b, int l, int r, int x) {\n if(a <= l and r <= b) return seg[x];\n int res = 0;\n if(a <= mid) res = query(a, b, l, mid, ls);\n if(b > mid) res = op(res, query(a, b, mid+1, r, rs));\n return res;\n }\n int query(int a, int b) { return query(a, b, 0, n , 1); }\n};\n\ninline void solve() {\n int n; cin >> n;\n vector<int> a(n);\n int mx = 0;\n for (int &i : a) { \n cin >> i;\n mx = max(i, mx);\n }\n\n vector<vector<int>> div(100001);\n\n for (int i = 1; i <= mx; i++) {\n for (int j = i; j <= mx; j += i) {\n div[j].eb(i);\n }\n }\n for (int i = 1; i <= mx; i++) reverse(ALL(div[i]));\n\n vector<vector<pii>> lst(n);\n map<int, int> mp;\n for (int i = 0; i < n; i++) {\n for (int j : div[a[i]]) {\n if (mp.find(j) != mp.end()) {\n if (lst[i].empty() || lst[i].back().second < mp[j]) {\n lst[i].eb(j, mp[j]);\n }\n }\n mp[j] = i;\n }\n }\n\n int q; cin >> q;\n vector<vector<pii>> qry(n);\n for (int i = 0; i < q; i++) {\n int l, r;\n cin >> l >> r;\n l--, r--;\n qry[r].eb(l, i);\n }\n\n Segment_Tree seg(n + 1);\n vector<int> ans(q);\n for (int i = 0; i < n; i++) {\n for (auto [val, pos] : lst[i]) {\n seg.modify(pos, val);\n }\n for (auto [l, idx] : qry[i]) {\n ans[idx] = seg.query(l, i);\n }\n }\n for (int i : ans) cout << i << '\\n';\n}\n\nsigned main() {\n\tIO;\t\n\tsolve();\t\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 53032, "score_of_the_acc": -0.2956, "final_rank": 6 }, { "submission_id": "aoj_1463_10054601", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class S, S (*op)(S, S), S (*e)()>\nstruct SegmentTree{\n int n;\n vector<S> data;\n\n SegmentTree(int sz=0) : n(1) {\n while(n < sz) n *= 2;\n data.resize(2 * n, e());\n }\n\n SegmentTree(const vector<S> &V) : n(1) {\n int sz = V.size();\n while(n < sz) n *= 2;\n data.resize(2 * n, e());\n for(int i = 0; i < sz; i++) data[i+n] = V[i];\n for(int i = n - 1; i >= 1; i--) data[i] = op(data[i<<1], data[i<<1|1]);\n }\n\n void set(int i, S x) {\n i += n;\n data[i] = x;\n while(i > 1) {\n i >>= 1;\n data[i] = op(data[i<<1], data[i<<1|1]);\n }\n }\n\n S prod(int l, int r) {\n l += n; r += n;\n S vl = e(), vr = e();\n while(l < r) {\n if(l & 1) vl = op(vl, data[l++]);\n if(r & 1) vr = op(data[--r], vr);\n l >>= 1; r >>= 1;\n }\n return op(vl, vr);\n }\n\n S all_prod() { return data[1]; }\n\n S get(int i) { return data[i + n]; }\n};\n\nconstexpr int MX = 1e5;\nconstexpr int INF = 1e9;\n\nusing S = int;\nS op(S l, S r) { return min(l, r); }\nS e() { return INF; }\n\nstruct Query {\n int l, r, i;\n};\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n \n int N;\n cin >> N;\n vector<int> A(N);\n for(int i = 0; i < N; i++) {\n cin >> A[i];\n }\n\n int Q;\n cin >> Q;\n vector<Query> query(Q);\n for(int i = 0; i < Q; i++) {\n cin >> query[i].l >> query[i].r;\n query[i].i = i;\n query[i].l--;\n }\n\n auto cmp = [&](Query a, Query b) -> bool {\n return a.l < b.l;\n };\n\n sort(query.rbegin(), query.rend(), cmp);\n\n // the first idx from left\n vector<int> left(MX + 1, INF);\n // the second idx from left\n SegmentTree<S, op, e> seg(MX + 1);\n\n int qi = 0;\n vector<int> ans(Q, INF);\n for(int i = N; i--;) {\n // add A[i] to seg\n // calc divisor\n for(int j = 1; j * j <= A[i]; j++) {\n if(A[i] % j == 0) {\n seg.set(j, left[j]);\n left[j] = i;\n int nj = A[i] / j;\n if(j != nj) {\n seg.set(nj, left[nj]);\n left[nj] = i;\n }\n }\n }\n\n // query\n while(qi < Q && query[qi].l == i) {\n // binary search\n auto check = [&](int l) -> bool {\n int prod = seg.prod(l, MX + 1);\n return prod < query[qi].r;\n };\n\n int lb = 1, ub = MX + 1;\n while(ub - lb > 1) {\n int mid = (ub + lb) / 2;\n if(check(mid)) lb = mid;\n else ub = mid;\n }\n\n ans[query[qi].i] = lb;\n\n qi++;\n }\n }\n\n for(int a : ans) {\n cout << a << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 6784, "score_of_the_acc": -0.062, "final_rank": 1 }, { "submission_id": "aoj_1463_10054585", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class S, S (*op)(S, S), S (*e)()>\nstruct SegmentTree{\n int n;\n vector<S> data;\n\n SegmentTree(int sz=0) : n(1) {\n while(n < sz) n *= 2;\n data.resize(2 * n, e());\n }\n\n SegmentTree(const vector<S> &V) : n(1) {\n int sz = V.size();\n while(n < sz) n *= 2;\n data.resize(2 * n, e());\n for(int i = 0; i < sz; i++) data[i+n] = V[i];\n for(int i = n - 1; i >= 1; i--) data[i] = op(data[i<<1], data[i<<1|1]);\n }\n\n void set(int i, S x) {\n i += n;\n data[i] = x;\n while(i > 1) {\n i >>= 1;\n data[i] = op(data[i<<1], data[i<<1|1]);\n }\n }\n\n S prod(int l, int r) {\n l += n; r += n;\n S vl = e(), vr = e();\n while(l < r) {\n if(l & 1) vl = op(vl, data[l++]);\n if(r & 1) vr = op(data[--r], vr);\n l >>= 1; r >>= 1;\n }\n return op(vl, vr);\n }\n\n S all_prod() { return data[1]; }\n\n S get(int i) { return data[i + n]; }\n};\n\nconstexpr int MX = 1e5;\nconstexpr int INF = 1e9;\n\nusing S = int;\nS op(S l, S r) { return min(l, r); }\nS e() { return INF; }\n\nstruct Query {\n int l, r, i;\n};\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n \n int N;\n cin >> N;\n vector<int> A(N);\n for(int i = 0; i < N; i++) {\n cin >> A[i];\n }\n\n int Q;\n cin >> Q;\n vector<Query> query(Q);\n for(int i = 0; i < Q; i++) {\n cin >> query[i].l >> query[i].r;\n query[i].i = i;\n query[i].l--;\n }\n\n auto cmp = [&](Query a, Query b) -> bool {\n return a.l < b.l;\n };\n\n sort(query.rbegin(), query.rend(), cmp);\n\n // the first idx from left\n vector<int> left(MX + 1, INF);\n // the second idx from left\n SegmentTree<S, op, e> seg(MX + 1);\n\n int qi = 0;\n vector<int> ans(Q, INF);\n for(int i = N; i--;) {\n // add A[i] to seg\n // calc divisor\n for(int j = 1; j * j <= A[i]; j++) {\n if(A[i] % j == 0) {\n seg.set(j, left[j]);\n left[j] = i;\n int nj = A[i] / j;\n if(j != nj) {\n seg.set(nj, left[nj]);\n left[nj] = i;\n }\n }\n }\n\n // query\n while(qi < Q && query[qi].l == i) {\n // binary search\n auto check = [&](int l) -> bool {\n int prod = seg.prod(l, MX + 1);\n return prod < query[qi].r;\n };\n\n int lb = 1, ub = MX;\n while(ub - lb > 1) {\n int mid = (ub + lb) / 2;\n if(check(mid)) lb = mid;\n else ub = mid;\n }\n\n ans[query[qi].i] = lb;\n\n qi++;\n }\n }\n\n for(int a : ans) {\n cout << a << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 0.1320754716981132, "time_ms": 250, "memory_kb": 6784, "score_of_the_acc": -0.062, "final_rank": 16 }, { "submission_id": "aoj_1463_10053885", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <numeric>\n#include <vector>\n\nusing namespace std;\n\ntemplate <class T, T op(T, T), T e()>\nstruct segtree {\n int n, size, lg;\n vector<T> tree;\n segtree(int n_) : segtree(vector<T>(n_, e())) {}\n void update(int i) { tree[i] = op(tree[i * 2], tree[i * 2 + 1]); }\n segtree(vector<T> v) : n(v.size()), lg(0) {\n while ((1 << lg) < n) lg++;\n size = 1 << lg;\n tree = vector<T>(size * 2);\n for (int i = 0; i < n; i++) {\n tree[i + size] = v[i];\n }\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int i, T x) {\n i += size;\n tree[i] = x;\n while (i > 1) {\n i >>= 1;\n update(i);\n }\n }\n\n T get(int i) { return tree[i + size]; }\n\n T prod(int l, int r) {\n l += size;\n r += size;\n\n T lp = e(), rp = e();\n while (l < r) {\n if (l & 1) lp = op(lp, tree[l++]);\n if (r & 1) rp = op(tree[--r], rp);\n l >>= 1;\n r >>= 1;\n }\n\n return op(lp, rp);\n }\n};\n\nvector<int> osa_k(int n) {\n vector<int> table(n + 1);\n iota(table.begin(), table.end(), 0);\n vector<int> primes;\n for (int i = 2; i <= n; i++) {\n if (table[i] == i) primes.emplace_back(i);\n for (int j : primes) {\n if (i * j > n) break;\n table[i * j] = j;\n }\n }\n return table;\n}\n\nvector<int> divisors(int x) {\n vector<int> res;\n for (int i = 1; i * i <= x; i++) {\n if (x % i == 0) {\n res.emplace_back(i);\n if (i * i != x) res.emplace_back(x / i);\n }\n }\n return res;\n}\n\nint op(int a, int b) { return max(a, b); }\n\nint e() { return 1; }\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int n;\n cin >> n;\n vector<int> a(n);\n for (auto &e : a) cin >> e;\n int q;\n cin >> q;\n vector<vector<pair<int, int>>> querys(n);\n for (int i = 0; i < q; i++) {\n int l, r;\n cin >> l >> r;\n l--, r--;\n querys[r].emplace_back(l, i);\n }\n vector<vector<int>> idxs(100'001);\n segtree<int, op, e> seg(n);\n vector<int> ans(q);\n for (int i = 0; i < n; i++) {\n auto D = divisors(a[i]);\n for (auto d : D) {\n if (!idxs[d].empty()) {\n auto j = idxs[d].back();\n seg.set(j, max(d, seg.get(j)));\n }\n idxs[d].emplace_back(i);\n }\n for (auto [l, id] : querys[i]) {\n ans[id] = seg.prod(l, i + 1);\n }\n }\n for (auto i : ans) cout << i << '\\n';\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 69020, "score_of_the_acc": -0.3922, "final_rank": 7 } ]
aoj_1506_cpp
Problem G: Dice サイコロ3兄弟は三つ子であり一心同体、3つでひとつとも言ってもよい。三つ子であるため見分けがつかないほど似ている。そんなサイコロ3兄弟はA君のお気に入りのおもちゃだ。A君はいつも3つ1セットでサイコロを転がして遊んでいる。このようにA君が楽しく遊ぶおもちゃだが、実は大きな秘密があった。彼らは実は生きていて、話したり自由に動いたりできるのだ。子ども部屋は r × c の大きさのグリッドで表すことができ、とても大きいものが散乱している。 サイコロ3兄弟は室内を東西南北の4方向へ進行方向に向かって転がりながら移動する。その形ゆえに斜め移動ができないのが、少し気の毒である。進行方向にとても大きいものがある場合、サイコロは進行方向に進むことができない。 サイコロ3兄弟は3つで1セットであるため、一度にひとつのサイコロしか移動することができない。また、サイコロたちはその場で回転するなどという器用な能力は持っていない。 彼らが話したり動いたりしている事実は人間には知られてはいけないというのが「おもちゃのルール」である。このルールを破ったおもちゃはこの世から消滅してしまう。A君が出かけているときに動いたり話したりしているおもちゃたちだが、A君が帰宅して子ども部屋に入ってきたときにもとの位置に戻っていなければならない。A君が帰宅した直後は緊急事態であり、A君が子ども部屋にたどり着く前にあらゆるおもちゃは即座にもとの場所へ戻らなければならない。 A君は床を見下ろすかたちになるため、サイコロの場所と上を向いている数字が正しければ、移動したことに気づくことはない。また、サイコロたちは見分けがつかないほど似ているので、どの収納場所にどのサイコロが向かっても支障はない。 そんなある日、A君のおもちゃ箱の中で最も優秀なプログラマーであるあなたに、与えられたサイコロの位置情報と収納場所から最短何手でサイコロ達が収納場所に戻れるかを計算する任務が与えられた。 サイコロ3兄弟を溺愛しているA君の手からサイコロ3兄弟が消えてしまったら、A君は泣いてしまうので、とても重要な任務である。 Input 入力は以下のフォーマットで与えられる。 r c grid 1 . . . grid r grid i は長さ c の文字列で、1から6までの数字か"."か”x”か”o”からなる。 数字は収納場所かつ、到着時のサイコロの上の値を示している。 “.”は普通の床を示している。 “x”はとてもおおきなものを示している。 “o”はサイコロを示している。 入力は以下の制約を満たす 1 ≤ r,c ≤ 20 初期状態でのサイコロの向きは以下の図に従う。 Output それぞれの入力に対する、各サイコロがすべて収納できるまでの最短手数を求めよ。 Sample Input 1 3 3 4o. 4o. 4o. Sample Output 1 3 各サイコロが西方向に移動すればよい。 Sample Input 2 4 4 4o.. .o3. .o.. .5.. Sample Output2 3 1行目のにあるサイコロが西方向 2行目のにあるサイコロが東方向 3行目のにあるサイコロが南方向に移動すればよい。 Sample Input 3 6 10 1xxxxxxxxx o.......o. xxxx.xxxx4 xoxx.xxxx. x....3xx.. x..xxxxx.. Sample Output 3 34
[ { "submission_id": "aoj_1506_10349794", "code_snippet": "// AOJ #1506 Dice\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 10000000;\n\nint R, C;\nchar g[25][25];\nint goalX[3], goalY[3], goalTop[3];\n\nint tableArr[6][4] = {\n {1, 3, 4, 2},\n {0, 2, 5, 3},\n {0, 4, 5, 1},\n {0, 1, 5, 4},\n {0, 3, 5, 2},\n {1, 2, 4, 3}\n};\n\nint moveOrientation(int idx, int dx, int dy) {\n int a = idx / 4;\n int b = idx % 4;\n int s, t;\n if(dx == -1) {\n s = tableArr[a][b];\n t = 5 - a;\n }\n else if(dx == 1) {\n s = tableArr[a][(b + 2) % 4];\n t = a;\n }\n else if(dy == -1) {\n s = tableArr[a][(b + 1) % 4];\n t = tableArr[a][b];\n }\n else {\n s = tableArr[a][(b + 3) % 4];\n t = tableArr[a][b];\n }\n int pos = 0;\n for (int i = 0; i < 4; i++) {\n if(tableArr[s][i] == t) { pos = i; break; }\n }\n return s * 4 + pos;\n}\n\nint revDist[3][25][25][24];\n\nvoid computeReverseBFS(int dIdx) {\n for (int i = 0; i < R; i++)\n for (int j = 0; j < C; j++)\n for (int k = 0; k < 24; k++) revDist[dIdx][i][j][k] = INF;\n\n queue<vector<int>> que;\n for (int i = 0; i < 4; i++) {\n int initOri = goalTop[dIdx] * 4 + i;\n revDist[dIdx][goalX[dIdx]][goalY[dIdx]][initOri] = 0;\n vector<int> initVec = {goalX[dIdx], goalY[dIdx], initOri};\n que.push(initVec);\n }\n int dx[4] = {-1, 0, 0, 1};\n int dy[4] = {0, -1, 1, 0};\n while(!que.empty()){\n vector<int> cur = que.front();\n que.pop();\n int cx = cur[0], cy = cur[1], cori = cur[2];\n int cd = revDist[dIdx][cx][cy][cori];\n for (int i = 0; i < 4; i++) {\n int nx = cx + dx[i], ny = cy + dy[i];\n if(nx < 0 || nx >= R || ny < 0 || ny >= C) continue;\n if(g[nx][ny] == 'x') continue;\n int nori = moveOrientation(cori, dx[i], dy[i]);\n if(revDist[dIdx][nx][ny][nori] > cd + 1) {\n revDist[dIdx][nx][ny][nori] = cd + 1;\n vector<int> nextVec = {nx, ny, nori};\n que.push(nextVec);\n }\n }\n }\n}\n\ntypedef vector<vector<int>> StateType;\n\nstruct StateHash {\n size_t operator()(const StateType &state) const {\n size_t seed = 0;\n for (size_t i = 0; i < state.size(); i++)\n for (size_t j = 0; j < state[i].size(); j++)\n seed ^= std::hash<int>()(state[i][j]) + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2);\n return seed;\n }\n};\n\nint heuristic(const StateType &state) {\n int perms[6][3] = {\n {0, 1, 2},\n {0, 2, 1},\n {1, 0, 2},\n {1, 2, 0},\n {2, 0, 1},\n {2, 1, 0}\n };\n int best = INF;\n for (int p = 0; p < 6; p++) {\n int sum = 0;\n for (int i = 0; i < 3; i++) {\n int dIdx = perms[p][i];\n int x = state[i][0], y = state[i][1], ori = state[i][2];\n sum += revDist[dIdx][x][y][ori];\n }\n best = min(best, sum);\n }\n return best;\n}\n\nbool isGoal(const StateType &state) {\n int perms[6][3] = {\n {0, 1, 2},\n {0, 2, 1},\n {1, 0, 2},\n {1, 2, 0},\n {2, 0, 1},\n {2, 1, 0}\n };\n for (int p = 0; p < 6; p++) {\n bool ok = true;\n for (int i = 0; i < 3; i++) {\n int dIdx = perms[p][i];\n if(state[i][0] != goalX[dIdx] || state[i][1] != goalY[dIdx] || (state[i][2] / 4) != goalTop[dIdx]) {\n ok = false;\n break;\n }\n }\n if(ok) return true;\n }\n return false;\n}\n\nstruct Node {\n int f, g, h;\n StateType state;\n};\n\nstruct NodeComp {\n bool operator()(const Node &a, const Node &b) const {\n return a.f > b.f;\n }\n};\n\nint main(){\n cin >> R >> C;\n for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) cin >> g[i][j];\n\n int cnt = 0;\n for (int i = 0; i < R; i++) {\n for (int j = 0; j < C; j++) {\n if(g[i][j] >= '1' && g[i][j] <= '6'){\n if(cnt < 3) {\n goalX[cnt] = i;\n goalY[cnt] = j;\n goalTop[cnt] = g[i][j] - '1';\n cnt++;\n }\n }\n }\n }\n for (int dIdx = 0; dIdx < 3; dIdx++) computeReverseBFS(dIdx);\n\n StateType initState;\n for (int i = 0; i < R; i++) {\n for (int j = 0; j < C; j++) {\n if(g[i][j] == 'o'){\n vector<int> temp = {i, j, 0};\n initState.push_back(temp);\n }\n }\n }\n sort(initState.begin(), initState.end());\n\n unordered_map<StateType, int, StateHash> bestCost;\n priority_queue<Node, vector<Node>, NodeComp> pq;\n int h0 = heuristic(initState);\n Node start;\n start.g = 0;\n start.h = h0;\n start.f = h0;\n start.state = initState;\n pq.push(start);\n bestCost[initState] = 0;\n\n int dx[4] = {-1, 0, 0, 1};\n int dy[4] = {0, -1, 1, 0};\n while(!pq.empty()){\n Node cur = pq.top();\n pq.pop();\n if(bestCost[cur.state] < cur.g) continue;\n if(isGoal(cur.state)) {\n cout << cur.g << endl;\n return 0;\n }\n for (int i = 0; i < 3; i++) {\n for (int d = 0; d < 4; d++){\n int nx = cur.state[i][0] + dx[d];\n int ny = cur.state[i][1] + dy[d];\n if(nx < 0 || nx >= R || ny < 0 || ny >= C) continue;\n if(g[nx][ny] == 'x') continue;\n int nori = moveOrientation(cur.state[i][2], dx[d], dy[d]);\n StateType nextState = cur.state;\n nextState[i] = {nx, ny, nori};\n bool collision = false;\n for (int j = 0; j < 3; j++) {\n for (int k = j+1; k < 3; k++){\n if(nextState[j][0] == nextState[k][0] && nextState[j][1] == nextState[k][1]){\n collision = true;\n break;\n }\n }\n if(collision) break;\n }\n if(collision) continue;\n sort(nextState.begin(), nextState.end());\n int newG = cur.g + 1;\n if(bestCost.find(nextState) == bestCost.end() || bestCost[nextState] > newG) {\n bestCost[nextState] = newG;\n int hval = heuristic(nextState);\n Node nxt;\n nxt.g = newG;\n nxt.h = hval;\n nxt.f = newG + hval;\n nxt.state = nextState;\n pq.push(nxt);\n }\n }\n }\n }\n cout << -1 << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 85812, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1506_10349781", "code_snippet": "// AOJ #1506 Dice\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1000000;\n\nint n, m;\nchar g[25][25];\nint goalX[3], goalY[3], goalTop[3];\n\nint tableArr[6][4] = {\n {1, 3, 4, 2},\n {0, 2, 5, 3},\n {0, 4, 5, 1},\n {0, 1, 5, 4},\n {0, 3, 5, 2},\n {1, 2, 4, 3}\n};\n\nint moveOrientation(int idx, int dx, int dy) {\n int a = idx / 4;\n int b = idx % 4;\n int s, t;\n if(dx == -1) {\n s = tableArr[a][b];\n t = 5 - a;\n } else if(dx == 1) {\n s = tableArr[a][(b + 2) % 4];\n t = a;\n } else if(dy == -1) {\n s = tableArr[a][(b + 1) % 4];\n t = tableArr[a][b];\n } else {\n s = tableArr[a][(b + 3) % 4];\n t = tableArr[a][b];\n }\n int pos = 0;\n for (int i = 0; i < 4; i++) {\n if(tableArr[s][i] == t) { pos = i; break; }\n }\n return s * 4 + pos;\n}\n\nint revDist[3][25][25][24];\n\nvoid computeReverseBFS(int dIdx) {\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++)\n for (int k = 0; k < 24; k++) revDist[dIdx][i][j][k] = INF;\n queue< vector<int> > que;\n for (int i = 0; i < 4; i++) {\n int initOri = goalTop[dIdx] * 4 + i;\n revDist[dIdx][goalX[dIdx]][goalY[dIdx]][initOri] = 0;\n vector<int> initVec = {goalX[dIdx], goalY[dIdx], initOri};\n que.push(initVec);\n }\n int dx[4] = {-1, 0, 0, 1};\n int dy[4] = {0, -1, 1, 0};\n while(!que.empty()){\n vector<int> cur = que.front();\n que.pop();\n int cx = cur[0], cy = cur[1], cori = cur[2];\n int cd = revDist[dIdx][cx][cy][cori];\n for (int i = 0; i < 4; i++) {\n int nx = cx + dx[i], ny = cy + dy[i];\n if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;\n if(g[nx][ny] == 'x') continue;\n int nori = moveOrientation(cori, dx[i], dy[i]);\n if(revDist[dIdx][nx][ny][nori] > cd + 1) {\n revDist[dIdx][nx][ny][nori] = cd + 1;\n vector<int> nextVec = {nx, ny, nori};\n que.push(nextVec);\n }\n }\n }\n}\n\ntypedef vector<vector<int>> StateType;\n\nint heuristic(const StateType &state) {\n int perms[6][3] = {\n {0, 1, 2},\n {0, 2, 1},\n {1, 0, 2},\n {1, 2, 0},\n {2, 0, 1},\n {2, 1, 0}\n };\n int best = INF;\n for (int p = 0; p < 6; p++) {\n int sum = 0;\n for (int i = 0; i < 3; i++) {\n int dIdx = perms[p][i];\n int x = state[i][0], y = state[i][1], ori = state[i][2];\n sum += revDist[dIdx][x][y][ori];\n }\n best = min(best, sum);\n }\n return best;\n}\n\nbool isGoal(const StateType &state) {\n int perms[6][3] = {\n {0, 1, 2},\n {0, 2, 1},\n {1, 0, 2},\n {1, 2, 0},\n {2, 0, 1},\n {2, 1, 0}\n };\n for (int p = 0; p < 6; p++) {\n bool ok = true;\n for (int i = 0; i < 3; i++) {\n int dIdx = perms[p][i];\n if(state[i][0] != goalX[dIdx] || state[i][1] != goalY[dIdx] || (state[i][2] / 4) != goalTop[dIdx]) {\n ok = false;\n break;\n }\n }\n if(ok) return true;\n }\n return false;\n}\n\nstruct Node {\n int f, g, h;\n StateType state;\n};\n\nstruct NodeComp {\n bool operator()(const Node &a, const Node &b) const {\n return a.f > b.f;\n }\n};\n\nint main(){\n cin >> n >> m;\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++) cin >> g[i][j];\n\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if(g[i][j] >= '1' && g[i][j] <= '6'){\n if(cnt < 3) {\n goalX[cnt] = i;\n goalY[cnt] = j;\n goalTop[cnt] = g[i][j] - '1';\n cnt++;\n }\n }\n }\n }\n for (int dIdx = 0; dIdx < 3; dIdx++) computeReverseBFS(dIdx);\n\n StateType initState;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if(g[i][j] == 'o'){\n vector<int> temp = {i, j, 0};\n initState.push_back(temp);\n }\n }\n }\n sort(initState.begin(), initState.end());\n\n map<StateType, int> bestCost;\n priority_queue<Node, vector<Node>, NodeComp> pq;\n int h0 = heuristic(initState);\n Node start;\n start.g = 0;\n start.h = h0;\n start.f = h0;\n start.state = initState;\n pq.push(start);\n bestCost[initState] = 0;\n\n int dx[4] = {-1, 0, 0, 1};\n int dy[4] = {0, -1, 1, 0};\n while(!pq.empty()){\n Node cur = pq.top();\n pq.pop();\n if(bestCost[cur.state] < cur.g) continue;\n if(isGoal(cur.state)) {\n cout << cur.g << endl;\n return 0;\n }\n for (int i = 0; i < 3; i++) {\n for (int d = 0; d < 4; d++){\n int nx = cur.state[i][0] + dx[d];\n int ny = cur.state[i][1] + dy[d];\n if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;\n if(g[nx][ny] == 'x') continue;\n int nori = moveOrientation(cur.state[i][2], dx[d], dy[d]);\n StateType nextState = cur.state;\n nextState[i] = {nx, ny, nori};\n bool collision = false;\n for (int j = 0; j < 3; j++) {\n for (int k = j+1; k < 3; k++){\n if(nextState[j][0] == nextState[k][0] && nextState[j][1] == nextState[k][1]){\n collision = true;\n break;\n }\n }\n if(collision) break;\n }\n if(collision) continue;\n sort(nextState.begin(), nextState.end());\n int newG = cur.g + 1;\n if(bestCost.find(nextState) == bestCost.end() || bestCost[nextState] > newG) {\n bestCost[nextState] = newG;\n int hval = heuristic(nextState);\n Node nxt;\n nxt.g = newG;\n nxt.h = hval;\n nxt.f = newG + hval;\n nxt.state = nextState;\n pq.push(nxt);\n }\n }\n }\n }\n cout << -1 << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 85856, "score_of_the_acc": -0.0725, "final_rank": 2 }, { "submission_id": "aoj_1506_1680242", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <queue>\n#include <map>\n#include <memory>\n\n#define rep(i, n) for(int i = 0; i < n; ++i)\n\nusing namespace std;\n\ntypedef vector<vector<int>> R;\ntypedef shared_ptr<R> Q;\ntypedef pair<pair<int, int>, Q> P;\n\nint table[6][4] = {\n\t{1, 3, 4, 2},\n\t{0, 2, 5, 3},\n\t{0 ,4, 5, 1},\n\t{0, 1, 5, 4},\n\t{0, 3, 5, 2},\n\t{1, 2, 4, 3}\n};\n\nint move(int index, int dx, int dy){\n\tint a = index / 4;\n\tint b = index % 4;\n\tint s, t;\n\tif(dx == -1){\n\t\ts = table[a][b];\n\t\tt = 5 - a;\n\t}\n\telse if(dx == 1){\n\t\ts = table[a][(b + 2) % 4];\n\t\tt = a;\n\t}\n\telse if(dy == -1){\n\t\ts = table[a][(b + 1) % 4];\n\t\tt = table[a][b];\n\t}\n\telse{\n\t\ts = table[a][(b + 3) % 4];\n\t\tt = table[a][b];\n\t}\n\treturn s * 4 + find(table[s], table[s] + 4, t) - table[s];\n}\n\nint n ,m;\nchar g[20][20];\nint sx[3];\nint sy[3];\nint st[3];\nint d[3][20][20][24];\nmap<vector<vector<int>>, int> f;\n\nint approximate(vector<vector<int>>& t){\n\tint p[6][3] = {\n\t\t{0, 1, 2},\n\t\t{0, 2, 1},\n\t\t{1, 0, 2},\n\t\t{1, 2, 0},\n\t\t{2, 0, 1},\n\t\t{2, 1, 0}\n\t};\n\n\tint u = -1;\n\trep(i, 6){\n\t\tint v = d[p[i][0]][t[0][0]][t[0][1]][t[0][2]] + d[p[i][1]][t[1][0]][t[1][1]][t[1][2]] + d[p[i][2]][t[2][0]][t[2][1]][t[2][2]];\n\t\tif(u == -1 || u > v){\n\t\t\tu = v;\n\t\t}\n\t}\n\treturn u;\n}\n\nbool is_reached(vector<vector<int>>& t){\n\tint p[6][3] = {\n\t\t{0, 1, 2},\n\t\t{0, 2, 1},\n\t\t{1, 0, 2},\n\t\t{1, 2, 0},\n\t\t{2, 0, 1},\n\t\t{2, 1, 0}\n\t};\n\n\trep(a, 6){\n\t\tbool b = true;\n\t\trep(i, 3){\n\t\t\tif(!(t[i][0] == sx[p[a][i]] && t[i][1] == sy[p[a][i]] && t[i][2] / 4 == st[p[a][i]])){\n\t\t\t\tb = false;\n\t\t\t}\n\t\t}\n\t\tif(b){\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nint main(){\n\tscanf(\"%d%d\", &n, &m);\n\trep(i, n){\n\t\trep(j, m){\n\t\t\tscanf(\" %c\", &g[i][j]);\n\t\t}\n\t}\n\n\tint t = 0;\n\trep(i, n){\n\t\trep(j, m){\n\t\t\tif('1' <= g[i][j] && g[i][j] <= '6'){\n\t\t\t\tsx[t] = i;\n\t\t\t\tsy[t] = j;\n\t\t\t\tst[t] = g[i][j] - '1';\n\t\t\t\t++t;\n\t\t\t}\n\t\t}\n\t}\n\trep(a, 3){\n\t\trep(i, n){\n\t\t\trep(j, m){\n\t\t\t\trep(k, 24){\n\t\t\t\t\td[a][i][j][k] = 10000;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tqueue<vector<int>> q;\n\t\trep(i, 4){\n\t\t\td[a][sx[a]][sy[a]][st[a] * 4 + i] = 0;\n\t\t\tq.push({sx[a], sy[a], st[a] * 4 + i});\n\t\t}\n\t\twhile(!q.empty()){\n\t\t\tauto v = q.front();\n\t\t\tq.pop();\n\t\t\tint dx[4] = {-1, 0, 0, 1};\n\t\t\tint dy[4] = {0, -1, 1, 0};\n\t\t\trep(i, 4){\n\t\t\t\tint x = v[0] + dx[i];\n\t\t\t\tint y = v[1] + dy[i];\n\t\t\t\tint index = move(v[2], dx[i], dy[i]);\n\t\t\t\tif(0 <= x && x < n && 0 <= y && y < m && g[x][y] != 'x' && d[a][x][y][index] == 10000){\n\t\t\t\t\td[a][x][y][index] = d[a][v[0]][v[1]][v[2]] + 1;\n\t\t\t\t\tq.push({x, y, index});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tpriority_queue<P, vector<P>, greater<P>> q;\n\tvector<vector<int>> s;\n\trep(i, n){\n\t\trep(j, m){\n\t\t\tif(g[i][j] == 'o'){\n\t\t\t\ts.push_back({i, j, 0});\n\t\t\t}\n\t\t}\n\t}\n\tq.push({{approximate(s), approximate(s)}, Q(new R(s))});\n\twhile(!q.empty()){\n\t\t/*\n\t\tprintf(\"(%d, %d)\\n\", q.top().first.first, q.top().first.second);\n\t\t*/\n\n\t\tauto v = *q.top().second;\n\t\tq.pop();\n\t\tint dx[4] = {-1, 0, 0, 1};\n\t\tint dy[4] = {0, -1, 1, 0};\n\t\trep(a, 3){\n\t\t\trep(i, 4){\n\t\t\t\tint x = v[a][0] + dx[i];\n\t\t\t\tint y = v[a][1] + dy[i];\n\t\t\t\tint index = move(v[a][2], dx[i], dy[i]);\n\t\t\t\tauto t = v;\n\t\t\t\tt[a][0] = x;\n\t\t\t\tt[a][1] = y;\n\t\t\t\tt[a][2] = index;\n\t\t\t\tbool b = true;\n\t\t\t\trep(j, 3){\n\t\t\t\t\tif(v[j][0] == x && v[j][1] == y){\n\t\t\t\t\t\tb = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(0 <= x && x < n && 0 <= y && y < m && g[x][y] != 'x' && b && f.find(t) == f.end()){\n\t\t\t\t\tf[t] = f[v] + 1;\n\t\t\t\t\tif(is_reached(t)){\n\t\t\t\t\t\tprintf(\"%d\\n\", f[t]);\n\t\t\t\t\t\treturn 0;\n\t\t\t\t\t}\n\t\t\t\t\tq.push({{approximate(t) + f[t], approximate(t)}, Q(new R(t))});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1270, "memory_kb": 172316, "score_of_the_acc": -1.2386, "final_rank": 3 }, { "submission_id": "aoj_1506_554022", "code_snippet": "#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<vector>\n#include<ctime>\n#include<cstdio>\n#include<map>\n#include<set>\n\nusing namespace std;\n\nclass Dice{\npublic:\n int x,y;\n int C[3];\n Dice(int c0,int c1,int c2,int _x,int _y){\n C[0] = c0;\n C[1] = c1;\n C[2] = c2;\n x = _x;\n y = _y;\n };\n Dice(){};\n void rotate_left(){\n swap(C[0],C[2]);\n C[2] = 7 - C[2];\n }\n void rotate_right(){\n swap(C[0],C[2]);\n C[0] = 7 - C[0];\n }\n void rotate_front(){\n swap(C[0],C[1]);\n C[0] = 7 - C[0];\n }\n void rotate_back(){\n swap(C[0],C[1]);\n C[1] = 7 - C[1];\n }\n void rotate(){\n swap(C[1],C[2]);\n C[1] = 7 - C[1];\n }\n bool operator < (const Dice &d)const {\n return false;\n }\n};\n\nclass Status{\npublic:\n int state[3],x[3],y[3];\n Status(){};\n Status(int s0,int y0,int x0,int s1,int y1,int x1,int s2,int y2,int x2){\n state[0] = s0;\n x[0] = x0;\n y[0] = y0;\n state[1] = s1;\n x[1] = x1;\n y[1] = y1;\n state[2] = s2;\n x[2] = x2;\n y[2] = y2;\n };\n bool operator < (const Status &s)const {\n for(int i = 0 ; i < 3 ; i++){\n\t if(state[i] != s.state[i])return state[i] < s.state[i];\n\t if(x[i] != s.x[i])return x[i] < s.x[i];\n\t if(y[i] != s.y[i])return y[i] < s.y[i];\n }\n return false;\n }\n};\n\nDice allState[24];\nint indexState[7][7][7];\n\nvoid createAll(){\n Dice dice(1,2,4,0,0);\n for(int i = 0 ; i < 6 ; i++){\n for(int j = 0 ; j < 4 ; j++){\n allState[i*4+j] = dice;\n indexState[dice.C[0]][dice.C[1]][dice.C[2]] = i*4+j;\n dice.rotate();\n }\n if(i%2)dice.rotate_front();\n else dice.rotate_right();\n }\n}\n\nint H,W;\nconst int MAX = 20;\nconst int INF = 1e8;\nchar data[MAX][MAX];\nint h[3][24][MAX][MAX];\nvector<Dice>ini;\nvoid init(){\n ini.clear();\n for(int i = 0 ; i < 3 ; i++){\n for(int j = 0 ; j < 24 ; j++){\n for(int k = 0 ; k < MAX ; k++){\n\t for(int l = 0 ; l < MAX ; l++){\n\t h[i][j][k][l] = INF;\n\t }\n }\n }\n }\n}\n\ntypedef pair<int,vector<Dice> > PP;\ntypedef pair<int,PP > PP2;\ntypedef pair<int,int>P;\ntypedef pair<int,P>P2;\n\nvoid input(){\n for(int i = 0 ; i < H ; i++){\n for(int j = 0 ; j < W ; j++){\n cin >> data[i][j];\n if(data[i][j] == 'o'){\n ini.push_back(Dice(1,2,4,j,i));\n }\n }\n }\n}\n\nint dx[4] = {0,1,0,-1};\nint dy[4] = {-1,0,1,0};\n\nbool isOK(vector<Dice>vec){\n for(int i = 0 ; i < 3 ; i++){\n if(!isdigit(data[vec[i].y][vec[i].x]))return false;\n if(vec[i].C[0] != data[vec[i].y][vec[i].x] - '0')return false;\n }\n return true;\n}\n\nint heuristic(int cnt,vector<Dice> vec){\n int res = INF;\n int index[3] = {0,1,2};\n\n int s[3];\n s[0] = indexState[vec[0].C[0]][vec[0].C[1]][vec[0].C[2]];\n s[1] = indexState[vec[1].C[0]][vec[1].C[1]][vec[1].C[2]];\n s[2] = indexState[vec[2].C[0]][vec[2].C[1]][vec[2].C[2]];\n\n do{\n res = min(res,cnt + h[index[0]][s[0]][vec[0].y][vec[0].x] + h[index[1]][s[1]][vec[1].y][vec[1].x] + h[index[2]][s[2]][vec[2].y][vec[2].x]);\n }while(next_permutation(index,index + 3));\n\n return res;\n}\n\nbool check(P p){\n if(!(0 <= p.first && p.first < H))return true;\n if(!(0 <= p.second && p.second < W))return true;\n return false;\n}\n\nvoid solve(){\n \n priority_queue<PP2,vector<PP2>,greater<PP2> >que;\n vector<Dice>vec;\n \n que.push(PP2(heuristic(0,ini),PP(0,ini)));\n map<Status,int>vis;\n\n vis[Status(indexState[ini[0].C[0]][ini[0].C[1]][ini[0].C[2]],ini[0].y,ini[0].x,\n\t indexState[ini[1].C[0]][ini[1].C[1]][ini[1].C[2]],ini[1].y,ini[1].x,\n\t indexState[ini[2].C[0]][ini[2].C[1]][ini[2].C[2]],ini[2].y,ini[2].x)] = 0;\n\n \n while(que.size()){\n PP2 pp2 = que.top(); que.pop();\n \n int cnt = pp2.second.first * -1;\n vec = pp2.second.second;\n\n int H = pp2.first;\n\n for(int i = 0 ; i < 3 ; i++){\n for(int j = 0 ; j < 4 ; j++){\n vector<Dice> nvec = vec;\t \n \n nvec[i].x = nvec[i].x + dx[j];\n nvec[i].y = nvec[i].y + dy[j];\n \n switch(j){\n case 0:\n nvec[i].rotate_back();\n break;\n case 1:\n nvec[i].rotate_right();\n break;\n case 2:\n nvec[i].rotate_front();\n break;\n case 3:\n nvec[i].rotate_left();\n break;\n }\n \n if(data[nvec[i].y][nvec[i].x] == 'x')continue;\n\t if(check(P(nvec[i].y,nvec[i].x)))continue;\n\t bool iscollide = false;\n\t for(int k = 0 ; k < 3; k++){\n\t if(i == k)continue;\n\t if(nvec[i].y == nvec[k].y && nvec[i].x == nvec[k].x)iscollide = true;\n\t }\n\n\t if(iscollide)continue;\n\t if(isOK(nvec)){\n\t cout << cnt+1 << endl;\n\t return;\n\t }\n\n\t Status nstatus = Status(indexState[nvec[0].C[0]][nvec[0].C[1]][nvec[0].C[2]],nvec[0].y,nvec[0].x,\n\t\t\t\t indexState[nvec[1].C[0]][nvec[1].C[1]][nvec[1].C[2]],nvec[1].y,nvec[1].x,\n\t\t\t\t indexState[nvec[2].C[0]][nvec[2].C[1]][nvec[2].C[2]],nvec[2].y,nvec[2].x);\n\n\t if(vis.find(nstatus) == vis.end() || vis[nstatus] > cnt){\n\t vis[nstatus] = cnt;\n\t que.push(PP2(heuristic(cnt+1,nvec),PP((cnt+1)*-1,nvec)));\n\t }\n }\n }\n }\n cout << \"error\" << endl;//\n}\n\n\ntypedef pair<int,Dice> P3;\n\n\nvoid bfs(P p,int id){\n\n queue<P3>que;\n Dice dice;\n\n for(int i = 0 ; i < 24 ; i++){\n dice = allState[i];\n if(data[p.first][p.second] - '0' == dice.C[0]){\n\t dice.y = p.first;\n\t dice.x = p.second;\n\t h[id][indexState[dice.C[0]][dice.C[1]][dice.C[2]]][dice.y][dice.x] = 0;\n\t que.push(P3(0,dice));\n }\n }\n\n while(que.size()){\n P3 p3 = que.front(); que.pop();\n int cnt = p3.first;\n dice = p3.second;\n for(int i = 0 ; i < 4 ; i++){ \n\t Dice ndice = dice;\n\t ndice.x += dx[i];\n\t ndice.y += dy[i];\n\n\t switch(i){\n\t case 0:\n\t ndice.rotate_back();\n\t break;\n\t case 1:\n\t ndice.rotate_right();\n\t break;\n\t case 2:\n\t ndice.rotate_front();\n\t break;\n\t case 3:\n\t ndice.rotate_left();\n\t break;\n\t }\n\n\t if(check(P(ndice.y,ndice.x)))continue;\n if(data[ndice.y][ndice.x] == 'x')continue;\n\t int ns = indexState[ndice.C[0]][ndice.C[1]][ndice.C[2]];\n if(h[id][ns][ndice.y][ndice.x] == INF){\n h[id][ns][ndice.y][ndice.x] = cnt+1;\n que.push(P3(cnt+1,ndice));\n }\n }\n }\n}\n\nint main(){\n createAll();\n int cnt = 1;\n cin >> H >> W;\n init();\n input();\n int id = 0;\n for(int i = 0 ; i < H ; i++){\n for(int j = 0 ; j < W ; j++){\n if(isdigit(data[i][j]))bfs(P(i,j),id++);\n }\n }\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 2590, "memory_kb": 189632, "score_of_the_acc": -2, "final_rank": 4 } ]
aoj_1502_cpp
Problem C : War A国とB国という2つの国が戦争をしている。A国の軍人であるあなたは n 人の兵士を率いて、B国の領土を占領する事になった。 B国の領土は2次元グリッドで表されている。あなたが最初に占領する場所は二次元グリッド上のある1マスである。 あなたが率いている兵士たちは、それぞれ h i の体力を持っている。 それぞれの兵士は体力を1を消費して移動することができる。 現在いるマスを(a,b)とすると、(a+1,b),(a-1,b),(a,b+1),(a,b-1)の4方向を移動先として選ぶことが可能である。 兵士は体力が0になったらそこから動くことができなくなる。 一人以上の兵士が通過したマスはすべて占領することができる。 あなたの仕事は、最大でいくつのマスを占領することができるかを求めることである。 ただし、この2次元グリッドのサイズは無限に広いとすること。 Input 入力は以下のフォーマットで与えられる。 n h 1 . . . h n 入力は以下の制約を満たす 1 ≤ n ≤ 500 1 ≤ h i ≤ 10,000,000 Output 答えの値を1行に出力せよ Sample Input 1 2 5 5 Sample Output 1 11 Sample Input 2 10 10 10 10 10 10 10 10 10 10 10 Sample Output 2 93 Sample Input 3 5 1 2 3 4 5 Sample Output 3 15
[ { "submission_id": "aoj_1502_7916695", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)\n\nusing namespace std;\nusing ll = long long;\n\nconst int MAX_N = 1e7+10;\nint main(){\n\tint N; cin >> N;\n\tvector<int> H(N);\n\trep(i, N) cin >> H[i];\n\n\tvector<ll> num(MAX_N);\n\tvector<ll> values(MAX_N);\n\tvalues[0] = 1;\n\tfor(int i = 1; i < MAX_N; i++) values[i] = i * 4ll;\n\trep(i, N) num[H[i]]++;\n\n\tll people = N;\n\tll ans = 0;\n\trep(i, MAX_N){\n\t\tif(people <= 0) break;\n\t\tans += min(values[i], people);\n\t\tpeople -= num[i];\n\t}\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 159380, "score_of_the_acc": -1, "final_rank": 11 }, { "submission_id": "aoj_1502_2257894", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\ntypedef long long ll;\n\nint sum[10000001];\nint main(){\n\tint n;scanf(\"%d\",&n);\n\trep(i,n){\n\t\tint h;scanf(\"%d\",&h);\n\t\tsum[h]++;\n\t}\n\tfor(int i=9999999;i>=0;i--)sum[i]+=sum[i+1];\n\tll cnt=1;\n\tint a=4;\n\tfor(int i=1;i<=10000000;i++){\n\t\tcnt+=min(a,sum[i]);\n\t\ta+=4;\n\t}\n\tprintf(\"%lld\\n\",cnt);\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 42284, "score_of_the_acc": -0.5598, "final_rank": 7 }, { "submission_id": "aoj_1502_2257860", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\n\nint sum[10000001];\nint main(){\n\tint n;scanf(\"%d\",&n);\n\trep(i,n){\n\t\tint h;scanf(\"%d\",&h);\n\t\tsum[h]++;\n\t}\n\tfor(int i=9999999;i>=0;i--)sum[i]+=sum[i+1];\n\tint cnt=1;\n\tint a=4;\n\tfor(int i=1;i<=10000000;i++){\n\t\tcnt+=min(a,sum[i]);\n\t\ta+=4;\n\t}\n\tprintf(\"%d\\n\",cnt);\n}", "accuracy": 0.9615384615384616, "time_ms": 40, "memory_kb": 42284, "score_of_the_acc": -0.5598, "final_rank": 14 }, { "submission_id": "aoj_1502_1826161", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst int inf=1e9;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nint in[10000010]={0};\nint main(){\n\tint n;\n\tcin>>n;\n\tin[0]=n;\n\trep(i,n){\n\t\tint a;cin>>a;\n\t\tin[a+1]--;\n\t}\n\trep(i,10000010)in[i+1]+=in[i];\n\tll out=1;\n\tloop(i,1,10000010){\n\t\tif(in[i]==0)break;\n\t\tout+=min(4*i,in[i]);\n\t}\n\tcout<<out<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 40220, "score_of_the_acc": -0.2467, "final_rank": 1 }, { "submission_id": "aoj_1502_1826160", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst int inf=1e9;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nint in[10000010]={0};\nint main(){\n\tint n;\n\tcin>>n;\n\tin[0]=n;\n\trep(i,n){\n\t\tint a;cin>>a;\n\t\tin[a+1]--;\n\t}\n\trep(i,10000010)in[i+1]+=in[i];\n\tint out=1;\n\tloop(i,1,10000010){\n\t\tif(in[i]==0)break;\n\t\tout+=min(4*i,in[i]);\n\t}\n\tcout<<out<<endl;\n}", "accuracy": 0.9615384615384616, "time_ms": 10, "memory_kb": 40224, "score_of_the_acc": -0.2467, "final_rank": 13 }, { "submission_id": "aoj_1502_1329913", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <math.h>\n#include <utility>\n#include <queue>\n\n#define MAX 500\n\nusing namespace std;\n\nint tentuki(int n ,int m){\n int p;\n if (n>=m) {\n p = n - m;\n }else{\n p =0;\n }\n return p;\n}\n\n\nint main(){\n int n;\n vector<int> h(n,0);\n cin >> n;\n for (int i=0; i<n; i++) {\n int p;\n cin >>p ;\n h.push_back(p);\n }\n sort(h.begin(),h.end(), greater<int>());\n int stock = n;\n long long area=1;\n int huka=0;\n int flag=0;\n while (1) {\n int per;\n if (stock <= 3) {\n per = stock;\n }else{\n per = 4;\n }\n for (int i=0; i<per; i++) {\n area+=tentuki(h[flag++],huka);\n }\n huka++;\n stock-=4;\n if (stock <= 0) {\n break;\n }\n }\n cout << area << endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 35656, "score_of_the_acc": -0.7179, "final_rank": 9 }, { "submission_id": "aoj_1502_1329881", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <math.h>\n#include <utility>\n#include <queue>\n\n#define MAX 500\n\nusing namespace std;\n\nint tentuki(int n ,int m){\n int p;\n if (n>=m) {\n p = n - m;\n }else{\n p =0;\n }\n return p;\n}\n\n\nint main(){\n int n;\n vector<int> h(n,0);\n cin >> n;\n for (int i=0; i<n; i++) {\n int p;\n cin >>p ;\n h.push_back(p);\n }\n sort(h.begin(),h.end(), greater<int>());\n int stock = n;\n int area=1;\n int huka=0;\n int flag=0;\n while (1) {\n int per;\n if (stock <= 3) {\n per = stock;\n }else{\n per = 4;\n }\n for (int i=0; i<per; i++) {\n area+=tentuki(h[flag++],huka);\n }\n huka++;\n stock-=4;\n if (stock <= 0) {\n break;\n }\n }\n cout << area << endl;\n}", "accuracy": 0.9615384615384616, "time_ms": 60, "memory_kb": 35656, "score_of_the_acc": -0.7179, "final_rank": 15 }, { "submission_id": "aoj_1502_1329874", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <math.h>\n#include <utility>\n#include <queue>\n\n#define MAX 500\n\nusing namespace std;\n\n\nint main(){\n int n;\n vector<int> h(n,0);\n cin >> n;\n for (int i=0; i<n; i++) {\n int p;\n cin >>p ;\n h.push_back(p);\n }\n sort(h.begin(),h.end(), greater<int>());\n int stock = n;\n long long int area=1;\n long long int huka=0;\n int flag=0;\n while (1) {\n int per;\n if (stock <= 3) {\n per = stock;\n }else{\n per = 4;\n }\n for (int i=0; i<per; i++) {\n area+=h[flag++]-huka;\n }\n huka++;\n stock-=4;\n if (stock <= 0) {\n break;\n }\n }\n cout << area << endl;\n}", "accuracy": 0.11538461538461539, "time_ms": 50, "memory_kb": 35132, "score_of_the_acc": -0.6146, "final_rank": 16 }, { "submission_id": "aoj_1502_1329864", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <math.h>\n#include <utility>\n#include <queue>\n\n#define MAX 500\n\nusing namespace std;\n\n\nint main(){\n int n;\n vector<int> h(n,0);\n cin >> n;\n for (int i=0; i<n; i++) {\n int p;\n cin >>p ;\n h.push_back(p);\n }\n sort(h.begin(),h.end(), greater<int>());\n int stock = n;\n int area=1;\n int huka=0;\n int flag=0;\n while (1) {\n int per;\n if (stock <= 3) {\n per = stock;\n }else{\n per = 4;\n }\n for (int i=0; i<per; i++) {\n area+=h[flag++]-huka;\n }\n huka++;\n stock-=4;\n if (stock <= 0) {\n break;\n }\n }\n cout << area << endl;\n}", "accuracy": 0.11538461538461539, "time_ms": 50, "memory_kb": 35392, "score_of_the_acc": -0.6162, "final_rank": 17 }, { "submission_id": "aoj_1502_1092857", "code_snippet": "#include <iostream>\nusing namespace std;\nint n,a;\nint con[10001000];\nint main() {\n cin >> n; for (int i = 0; i < n; ++i) cin >> a, con[a]++;\n for (int i = 10000001; i >= 0; --i) con[i] += con[i+1];\n long long res = 0;\n for (int i = 0; i <= 10000000; ++i) res += min(i*4, con[i]);\n cout << res+1 << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 40220, "score_of_the_acc": -0.4467, "final_rank": 3 }, { "submission_id": "aoj_1502_1092855", "code_snippet": "#include <iostream>\nusing namespace std;\nint n,a;\nint con[10001000];\nint main() {\n cin >> n;\n for (int i = 0; i < n; ++i) cin >> a, con[a]++;\n for (int i = 10000001; i >= 0; --i) con[i] += con[i+1];\n long long res = 0;\n for (int i = 0; i <= 10000000; ++i) res += min(i*4, con[i]);\n cout << res+1 << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 40224, "score_of_the_acc": -0.4467, "final_rank": 4 }, { "submission_id": "aoj_1502_1092854", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<long long> vll;\ntypedef pair<int,int> pint;\ntypedef pair<long long, long long> pll;\n\n#define MP make_pair\n#define PB push_back\n#define ALL(s) (s).begin(),(s).end()\n#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << endl\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) \n{ return s << '<' << P.first << \", \" << P.second << '>'; }\ntemplate<class T> ostream& operator << (ostream &s, vector<T> P) \n{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << \" \"; } s << P[i]; } return s; }\ntemplate<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) \n{ EACH(it, P) { s << \"<\" << it->first << \"->\" << it->second << \"> \"; } return s; }\n\n\n\nint n;\nint con[10001000];\n\nint main() {\n while (cin >> n) {\n memset(con, 0, sizeof(con));\n for (int i = 0; i < n; ++i) {\n int a; cin >> a; con[a]++;\n }\n for (int i = 10000001; i >= 0; --i) con[i] += con[i+1];\n long long res = 0;\n for (int i = 0; i <= 10000000; ++i) res += min(i*4, con[i]);\n cout << res+1 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 40232, "score_of_the_acc": -0.5468, "final_rank": 6 }, { "submission_id": "aoj_1502_903126", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define X first\n#define Y second\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\n\nconst int N = 1e7 + 10;\nint dat[N];\n\nint main(){\n\tint n;\n\tcin >> n;\n\trep(i, n){\n\t\tint h;\n\t\tcin >> h;\n\t\t++dat[1], --dat[h+1];\n\t}\n\n\trep(i, N) dat[i + 1] += dat[i];\n\n\tll ans = 1;\n\tFOR(i, 1, N){\n\t\tans += min(i * 4, dat[i]);\n\t}\n\tcout << ans << endl;\n\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 40216, "score_of_the_acc": -0.3467, "final_rank": 2 }, { "submission_id": "aoj_1502_903124", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define X first\n#define Y second\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\n\nconst int N = 1e7 + 10;\nint dat[N];\n\nvoid add(int i, int a){\n\tfor (int x = i; x < N; x += x&-x) dat[x] += a;\n}\n\nint sum(int i){\n\tint res = 0;\n\tfor (int x = i; x > 0; x -= x&-x) res += dat[x];\n\treturn res;\n}\n\nint main(){\n\tint n;\n\tcin >> n;\n\trep(i, n){\n\t\tint h;\n\t\tcin >> h;\n\t\tadd(1, 1), add(h+1, -1);\n\t}\n\n\tll ans = 1;\n\tFOR(i, 1, N){\n\t\tans += min(i * 4, sum(i));\n\t}\n\tcout << ans << endl;\n\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 38060, "score_of_the_acc": -1.2331, "final_rank": 12 }, { "submission_id": "aoj_1502_561867", "code_snippet": "#include <cstdio>\nlong long int n,m;\nlong long int t[11111111];\nint main(void){\n scanf(\"%lld\",&n);\n t[0] = n;\n for(int i = 0; i < n; i++){\n long long int h;\n scanf(\"%lld\",&h);\n t[h] -= 1;\n }\n\n long long int res = 1;\n m = 4;\n for(int i = 0; t[i]; i++){\n res += (m>t[i])?t[i]:m;\n m += 4;\n t[i+1] += t[i];\n }\n printf(\"%lld\\n\",res);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 80888, "score_of_the_acc": -0.5038, "final_rank": 5 }, { "submission_id": "aoj_1502_535339", "code_snippet": "#include<stdio.h>\nlong long d[10000001];\nint main(){\n\tint n,i,h;\n\tfor(i=0;i<=10000000;i++)d[i]=0;\n\tscanf(\"%d\",&n);\n\tfor(i=0;i<n;i++){\n\t\tscanf(\"%d\",&h);\n\t\td[h-1]++;\n\t}\n\tlong long ans=0;\n\tfor(i=9999999;i>=0;i--){\n\t\td[i]+=d[i+1];\n\t\tif(d[i]>4*(i+1))d[i]=4*(i+1);\n\t\tans+=d[i];\n\t}\n\tprintf(\"%lld\\n\",ans+1);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 79148, "score_of_the_acc": -0.6928, "final_rank": 8 }, { "submission_id": "aoj_1502_517057", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing namespace std;\n\nint main()\n{\n int n;\n cin >> n;\n vector<int> h(n);\n for(int i=0; i<n; ++i)\n cin >> h[i];\n\n long long ret = 1;\n int k = 0;\n for(int i=1; i<=100000000; ++i){\n ret += min(4 * i, n - k);\n while(k < n && h[k] == i)\n ++ k;\n }\n\n cout << ret << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 1192, "score_of_the_acc": -0.9, "final_rank": 10 } ]
aoj_1466_cpp
Peculiar Protocol The Kingdom of Icpca has a peculiar protocol in wedding ceremonies. That is, amounts of monetary gifts should be a multiple of a fixed quantity plus a fixed extra. When the fixed quantity is $d$ and the fixed extra is $r$, courteous amounts of wedding gifts are $k \times d + r$ for any non-negative integer multipliers $k$. Initially, you have a pile of $n$ banknotes. Every time you attend a wedding ceremony, you draw out a contiguous portion from your current banknote pile as your gift that sums up to a courteous amount, that is, a multiple of $d$ plus additional $r$. If no contiguous portion sums up to such an amount, you cannot attend wedding ceremonies any more. After drawing out, the remaining banknotes are squeezed to form a single pile, keeping their relative order. The resultant pile of banknotes may have portions with such an amount, which allows you to attend more ceremonies. Your monetary gifts are expected to raise your social reputation. As the additional amount r is considered mandatory, the multiplier $k$ is considered significant. Your reputation is raised in proportion to $k$ at each of the ceremonies you attend. For example, assume $d = 5$ and $r = 1$, and you have banknotes whose values are 2, 2, 2, 4, and 4, piled up in this order. When you attend a wedding ceremony, there are following two possible ways to give courteous monetary gifts. Give a monetary gift consisting of the first three banknotes from the top, totaling $2+2+2 = 6 = 1 \times d+r$. After drawing them out, you have banknotes with values 4 and 4. No contiguous portion of your remaining banknote pile sums up to a courteous amount. Thus, you cannot attend wedding ceremonies anymore. Give a monetary gift consisting of the third and the fourth banknotes, totaling $2+4 = 6 = 1 \times d+r$. After drawing them out, you have banknotes with values 2, 2, and 4, in this order. You can attend another wedding ceremony because the second and the third banknotes total $2+4 = 6 = 1 \times d+r$, which is courteous. In this example, the second way can maximize your social reputation by attending two ceremonies, because the total of the multipliers is $1 + 1 = 2$, which achieves the maximum possible. In contrast, if the value of the first banknote is 12, giving the first three banknotes at a ceremony prevents you from attending more ceremonies. That, however, maximizes your social reputation because the total of the multipliers is 3, which achieves the maximum possible. Compute the maximum possible total of the multipliers with your monetary gifts at wedding ceremonies. You may assume that you have so many unmarried relatives and friends that you can attend any number of wedding ceremonies as long as you can give courteous monetary gifts. Input The input consists of a single test case in the following format. $n$ $d$ $r$ $a_1$ ... $a_n$ The first line has three integers $n$, $d$, and $r$. The integer $n$ ($1 \leq n \leq 500$) is the number of banknotes you have. Th ...(truncated)
[ { "submission_id": "aoj_1466_10437156", "code_snippet": "// AOJ #1486 Peculiar Protocol\n// 2025.5.1\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = (ll)1e18;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0, c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nll dp1[505][505], dp[505][505];\nint main(){\n int n = Cin();\n ll d = Cin(), r = Cin();\n vector<ll>a(n+1), S(n+1);\n for(int i=1;i<=n;i++){\n a[i] = Cin();\n S[i] = S[i-1] + a[i];\n }\n auto sum = [&](int l,int r){ return S[r] - S[l-1]; };\n\n\n for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dp1[i][j] = -INF;\n\n for(int len=1; len<=n; len++){\n for(int l=1; l+len-1<=n; l++){\n int rgt = l+len-1;\n ll A = sum(l, rgt);\n\n if(A >= r && (A - r) % d == 0) dp1[l][rgt] = max(dp1[l][rgt], (A - r)/d);\n if(len >= 2){\n int j = rgt-1;\n for(int i=l; i<=j; i++){\n if(dp1[i][j] < 0) continue;\n ll innerSum = sum(i,j);\n ll rem = A - innerSum;\n if(rem >= r && (rem - r) % d == 0){\n ll kip = dp1[i][j];\n ll kroot = (rem - r)/d;\n dp1[l][rgt] = max(dp1[l][rgt], kip + kroot);\n }\n }\n }\n\n ll best = 0;\n for(int m=l; m<rgt; m++) best = max(best, dp[l][m] + dp[m+1][rgt]);\n dp[l][rgt] = max(best, dp1[l][rgt]);\n }\n }\n cout << dp[1][n] << endl;\n return 0;\n}", "accuracy": 0.07547169811320754, "time_ms": 160, "memory_kb": 7312, "score_of_the_acc": -0.1088, "final_rank": 15 }, { "submission_id": "aoj_1466_10149745", "code_snippet": "//#define _GLIBCXX_DEBUG\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <utility>\nusing namespace std;\n#define rep(i,n) for(int i=0; i<int(n); i++)\nusing i64 = long long;\ntemplate<class T> void chmin(T& l, const T& r){ if(r < l) l = r; }\ntemplate<class T> void chmax(T& l, const T& r){ if(l < r) l = r; }\n\n#include <map>\n\nvoid testcase(){\n i64 N; cin >> N;\n i64 D, R; cin >> D >> R;\n vector<i64> A(N); rep(i,N) cin >> A[i];\n vector<i64> B(N+1); rep(i,N) B[i+1] = B[i] + A[i];\n vector<vector<i64>> dp(N+1, vector<i64>(N+1));\n vector<vector<i64>> fx(N+1, vector<i64>(N+1));\n // cout << N << \" \" << D << \" \" << R << endl;\n\n map<i64,i64> mp;\n for(i64 k=N; k>=1; k--) mp[R*k%D] = k;\n\n i64 QMAX = 10000;\n for(i64 t=1; t<=N*2; t++) if(R*t%D == 0) QMAX = t;\n\n for(i64 r=1; r<=N; r++){\n vector<i64> Q(r+1);\n for(i64 t=r-1; t>=0; t--){\n for(i64 s=t+1; s<=r; s++){\n chmax(Q[t], Q[s] + fx[t][s]);\n }\n }\n //for(auto q : Q) cout << q << \" \";\n //cout << endl;\n for(i64 l=0; l<r; l++){\n i64 sum = B[r] - B[l];\n if(mp.count(sum % D) == 0) continue;\n i64 ff = 0;\n while(ff <= Q[l] && (sum - ff * R) % D != R) ff++;\n if(ff > Q[l]) continue;\n sum -= R * ff;\n if(sum < R) continue;\n fx[l][r] = ff + 1;\n dp[l][r] = (sum - R) / D;\n }\n //for(i64 l=0; l<r; l++){\n // cout << \"l = \"<< l << \" , r = \" << r << \" , dp = \" << dp[l][r] << \" , fx = \" << fx[l][r] << endl;\n //}\n }\n\n vector<i64> ans(N+1);\n rep(i,N) for(i64 j=i+1; j<=N; j++) chmax(ans[j], ans[i] + dp[i][j]);\n cout << ans[N] << \"\\n\";\n}\n\nint main(){\n //rep(t,4){\n testcase();\n // cout << string(10, '-') << endl;\n //}\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 7460, "score_of_the_acc": -0.0946, "final_rank": 5 }, { "submission_id": "aoj_1466_10133439", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(v) (v).begin(),(v).end()\n#define pb(a) push_back(a)\n#define rep(i, n) for(int i=0;i<n;i++)\n#define foa(e, v) for(auto&& e : v)\nusing ll = long long;\nconst ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);\n#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;\n\nlong long modinv(long long a, long long MOD) {\n long long b = MOD, u = 1, v = 0;\n while (b) {\n long long t = a / b;\n a -= t * b; std::swap(a, b);\n u -= t * v; std::swap(u, v);\n }\n u %= MOD; \n if (u < 0) u += MOD;\n return u;\n}\n\nlong long modpow(long long a, long long n, long long MOD) {\n long long res = 1;\n a %= MOD;\n if(n < 0) {\n n = -n;\n a = modinv(a, MOD);\n }\n while (n > 0) {\n if (n & 1) res = res * a % MOD;\n a = a * a % MOD;\n n >>= 1;\n }\n return res;\n}\nvoid chmax(ll &a, ll b) { a = max(a, b); }\n\npair<ll, ll> calc(ll num, ll r, ll d) {\n ll g = __gcd(num, r);\n g = __gcd(g, d);\n num /= g;\n r /= g;\n d /= g;\n if(num % __gcd(r, d)) return {-1, -1};\n if(r == 0) {\n if(num == 0) return {d - 1, d};\n else return {-1, -1};\n }\n num *= modinv(r, d);\n num %= d;\n if(num == 0) num = d - 1;\n else num --;\n return {num, d};\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n ll n, d, p;\n cin >> n >> d >> p;\n vector<ll> a(n + 1);\n rep(i, n) cin >> a[i + 1];\n rep(i, n) a[i + 1] += a[i];\n \n vector<vector<ll>> dp(n, vector(n + 1, 0LL)); // r何回引く?\n vector<vector<ll>> can(n, vector(n + 1, -1LL)); // kの総和\n for(int l = n - 1; l >= 0; l --) {\n for(int r = l + 1; r <= n; r ++) {\n if(dp[l][r] != -1) {\n ll num = a[r] - a[l];\n auto [kmin, diff] = calc(num, p, d);\n if(kmin != -1 and kmin <= dp[l][r]) {\n ll val = dp[l][r] - kmin;\n val /= diff;\n val *= diff;\n val += kmin;\n val ++;\n chmax(dp[l][r], val);\n val = (val % diff == 0 ? diff : val % diff);\n num = (num - val * p);\n assert(num % d == 0);\n chmax(can[l][r], num / d);\n }\n }\n if((a[r] - a[l]) % d == p) {\n \n chmax(dp[l][r], 1LL);\n chmax(can[l][r], (a[r] - a[l]) / d);\n }\n {\n for(int i = r + 1; i <= n; i ++) {\n chmax(dp[l][i], dp[l][r] + dp[r][i]);\n }\n }\n }\n }\n // for(int i = 0; i < n; i ++) {\n // for(int j = i + 1; j <= n; j ++) {\n // cout << dp[i][j] << \" \";\n // }\n // cout << endl;\n // }\n // for(int i = 0; i < n; i ++) {\n // for(int j = i + 1; j <= n; j ++) {\n // cout << can[i][j] << \" \";\n // }\n // cout << endl;\n // }\n vector<ll> ans(n + 1, -INF);\n ans[0] = 0;\n for(int i = 0; i < n; i ++) {\n for(int j = i + 1; j <= n; j ++) {\n chmax(ans[j], ans[i] + max(can[i][j], 0LL));\n }\n }\n cout << ans.back() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 7296, "score_of_the_acc": -0.0159, "final_rank": 4 }, { "submission_id": "aoj_1466_10058692", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, a, b) for(ll i = a; i < (b); i++)\n#define all(a) (a).begin(), (a).end()\n\nint main() {\n ll n, d, R;\n cin >> n >> d >> R;\n\n vector<ll> a(n);\n rep(i, 0, n) cin >> a[i];\n a.insert(a.begin(), 0);\n rep(i, 0, n) a[i + 1] += a[i];\n\n map<ll, ll> cnt;\n rep(i, 1, n + 1) if(cnt.count(R * i % d) == 0) cnt[R * i % d] = i;\n\n vector<vector<pair<ll, bool>>> dp(\n n + 1, vector<pair<ll, bool>>(n + 1, {0, false}));\n\n vector<vector<pair<ll, ll>>> tmp(n + 1);\n\n rep(r, 0, n + 1) {\n for(ll l = r; l >= 0; l--) {\n ll mx = 0;\n rep(i, l + 1, r) { mx = max(mx, dp[l][i].first + dp[i][r].first); }\n dp[l][r].first = mx;\n if(cnt.count((a[r] - a[l]) % d) &&\n cnt[(a[r] - a[l]) % d] <= mx + 1) {\n dp[l][r].first = max(mx, cnt[(a[r] - a[l]) % d]);\n dp[l][r].second = true;\n tmp[l].emplace_back(\n r, (a[r] - a[l] - cnt[(a[r] - a[l]) % d] * R) / d);\n }\n }\n }\n\n vector<ll> dp2(n + 1);\n int idx = 0;\n rep(i, 0, n) {\n dp2[i + 1] = max(dp2[i + 1], dp2[i]);\n for(auto [r, x] : tmp[i]) {\n dp2[r] = max(dp2[r], dp2[i] + x);\n }\n }\n\n cout << dp2[n] << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 10240, "score_of_the_acc": -0.0105, "final_rank": 3 }, { "submission_id": "aoj_1466_10049543", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define si(a) a.size()\n#define vec vector\n#undef long\n#define long long long\nusing ll = long;\ntemplate <typename T> bool chmin(T &x, T y) {\n\tif (y < x) {\n\t\tx = y;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <typename T> bool chmax(T &x, T y) {\n\tif (x < y) {\n\t\tx = y;\n\t\treturn true;\n\t}\n\treturn false;\n}\nlong extgcd(long a, long b, long &x, long &y) {\n\tif (!b)\n\t\treturn x = 1, y = 0, a;\n\tlong d = extgcd(b, a % b, y, x);\n\ty -= a / b * x;\n\treturn d;\n}\nlong inv_mod(long x, long m) {\n\tlong y, z;\n\textgcd(x, m, y, z);\n\tif ((y %= m) < 0)\n\t\ty += m;\n\treturn y;\n}\nvoid solve() {\n\tint n;\n\tlong c, mod;\n\tcin >> n >> mod >> c;\n\tvec<long> a(n);\n\trep(i, n) cin >> a[i];\n\tvec<vec<long>> rui(n + 1, vec<long>(n + 1));\n\trep(i, n + 1) rep(j, n + 1) {\n\t\tfor (int k = i; k < j; k++)\n\t\t\trui[i][j] += a[k];\n\t}\n\tvec<vec<int>> d(n + 1, vec<int>(n + 1));\n\tfor (int r = 1; r <= n; r++) {\n\t\tfor (int l = r - 1; l >= 0; l--) {\n\t\t\tfor (int k = l + 1; k < r; k++)\n\t\t\t\tchmax(d[l][r], d[l][k] + d[k][r]);\n\t\t\tif ((rui[l][r] - (d[l][r] + 1) * c) % mod == 0)\n\t\t\t\td[l][r]++;\n\t\t\tchmin(d[l][r], r - l);\n\t\t}\n\t}\n\tconst long g = gcd(c, mod);\n\tconst long cc = c / g, mm = mod / g;\n\tconst long cinv = inv_mod(cc, mm);\n\tvec<long> dp(n + 1);\n\trep(i, n + 1) {\n\t\tif (i != 0)\n\t\t\tchmax(dp[i], dp[i - 1]);\n\t\tfor (int j = i + 1; j <= n; j++) {\n\t\t\tif (rui[i][j] % g)\n\t\t\t\tcontinue;\n\t\t\tlong kk = rui[i][j] / g * cinv % mm;\n\t\t\tif (kk == 0)\n\t\t\t\tkk += mm;\n\t\t\tif (kk > d[i][j])\n\t\t\t\tcontinue;\n\t\t\tchmax(dp[j], dp[i] + (rui[i][j] - kk * c) / mod);\n\t\t}\n\t}\n\tcout << dp[n] << endl;\n}\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tsolve();\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5988, "score_of_the_acc": -0.0081, "final_rank": 2 }, { "submission_id": "aoj_1466_10047419", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<ll,ll> pi;\ntypedef pair<ld,ld> pt;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\n#define FOR(i,l,r) for(ll i=(l);i<(r);i++)\n#define REP(i,n) FOR(i,0,n)\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\nint main(){\n ll N,D,R;cin>>N>>D>>R;\n vi A(N);\n REP(i,N)cin>>A[i];\n vi B(N+1);\n REP(i,N)B[i+1]=A[i]+B[i];\n vvi DP(N+1,vi(N+1,1e18));\n vector<vector<bitset<512>>>EP(N+1,vector<bitset<512>>(N+1));\n REP(i,N+1)DP[i][i]=0,EP[i][i][0]=1;\n FOR(d,1,N+1)REP(i,N+1-d){\n FOR(j,1,d+1)if((B[i+d]-B[i])%D==j*R%D&&EP[i][i+d-1][j-1]){DP[i][i+d]=j;break;}\n FOR(j,i+1,i+d)DP[i][i+d]=min(DP[i][i+d],DP[i][j]+DP[j][i+d]);\n EP[i][i+d]=EP[i][i+d-1];\n if(DP[i][i+d]<1e17)EP[i][i+d][DP[i][i+d]]=1;\n FOR(j,i,i+d)if(DP[j][i+d]<1e17)EP[i][i+d]|=EP[i][j]<<DP[j][i+d];\n }\n vi FP(N+1);\n REP(i,N){\n FP[i+1]=max(FP[i],FP[i+1]);\n FOR(j,i+1,N+1)if(DP[i][j]<1e17)FP[j]=max(FP[j],FP[i]+(B[j]-B[i]-R*DP[i][j])/D);\n }\n cout<<FP[N]<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 20804, "score_of_the_acc": -0.3164, "final_rank": 6 }, { "submission_id": "aoj_1466_10041742", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC optimize(\"O3\")\n\nint dp[501][501];\nint ep[501];\n\nlong long s[501];\nlong long hp[501];\n\nconstexpr int INF = INT_MAX / 4;\n\nint main() {\n int n;\n long long d, r;\n cin >> n >> d >> r;\n s[0] = 0;\n for(int i = 1; i <= n; ++i) {\n cin >> s[i];\n s[i] += s[i - 1];\n }\n for(int p = 0; p <= n; ++p) {\n for(int q = 0; q <= n; ++q) {\n dp[p][q] = -INF;\n }\n }\n for(int k = 1; k <= n; ++k) {\n for(int p = 0, q = k; q <= n; ++p, ++q) {\n for(int i = p; i <= q; i++) {\n ep[i] = 0;\n }\n for(int i = p; i < q; ++i) {\n for(int j = i + 1; j <= q; ++j) {\n ep[j] = max(ep[j], ep[i] + dp[i][j] * (dp[i][j] != -INF));\n }\n }\n for(int c = 0; c <= ep[q]; ++c) {\n if((s[q] - s[p] - r * c) % d == r) {\n dp[p][q] = c + 1;\n break;\n }\n }\n }\n }\n hp[0] = 0;\n for(int p = 0; p < n; ++p) {\n for(int q = p + 1; q <= n; ++q) {\n hp[q] = max(hp[q], hp[p] + (dp[p][q] != -INF) * (s[q] - s[p] - r * dp[p][q]) / d);\n }\n }\n cout << hp[n] << endl;\n}", "accuracy": 1, "time_ms": 1290, "memory_kb": 4432, "score_of_the_acc": -0.9143, "final_rank": 10 }, { "submission_id": "aoj_1466_10041673", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC optimize(\"O3\")\n\nint dp[501][501];\nint ep[501];\n\nlong long s[501];\nlong long hp[501];\n\nconstexpr int INF = INT_MAX / 4;\n\nint main() {\n int n;\n long long d, r;\n cin >> n >> d >> r;\n s[0] = 0;\n for(int i = 1; i <= n; ++i) {\n cin >> s[i];\n s[i] += s[i - 1];\n }\n for(int p = 0; p <= n; ++p) {\n for(int q = 0; q <= n; ++q) {\n dp[p][q] = -INF;\n }\n }\n for(int k = 1; k <= n; ++k) {\n for(int p = 0, q = k; q <= n; ++p, ++q) {\n for(int i = p; i <= q; i++) {\n ep[i] = 0;\n }\n for(int i = p; i < q; ++i) {\n ep[i + 1] = max(ep[i + 1], ep[i]);\n for(int j = i + 1; j <= q; ++j) {\n ep[j] = max(ep[j], ep[i] + (dp[i][j] != -INF));\n }\n }\n for(int c = 0; c <= ep[q]; ++c) {\n if(((s[q] - s[p]) - r * c) % d == r) {\n dp[p][q] = c + 1;\n break;\n }\n }\n }\n }\n // for(int l = 0; l <= n; l++) {\n // for(int r = 0; r <= n; r++) {\n // if(l > r) cout << \" \" << \" \";\n // else if(dp[l][r] == -INF) cout << '.' << \" \";\n // else cout << dp[l][r] << \" \";\n // } cout << \"\\n\";\n // }\n hp[0] = 0;\n for(int p = 0; p < n; ++p) {\n hp[p + 1] = max(hp[p + 1], hp[p]);\n for(int q = p + 1; q <= n; ++q) {\n if(dp[p][q] != -INF) {\n hp[q] = max(hp[q], hp[p] + (s[q] - s[p] - r * dp[p][q]) / d);\n }\n }\n }\n cout << hp[n] << endl;\n}", "accuracy": 0.07547169811320754, "time_ms": 780, "memory_kb": 4432, "score_of_the_acc": -0.55, "final_rank": 17 }, { "submission_id": "aoj_1466_10041620", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC optimize(\"O3\")\n\nusing ll = long long;\n\nint main() {\n int n;\n ll div, rem;\n cin >> n >> div >> rem;\n vector<ll> s(n + 1);\n for(int i = 1; i <= n; i++) {\n cin >> s[i];\n s[i] += s[i - 1];\n }\n const ll INF = LONG_MAX / 4;\n vector<int> ep(n + 1);\n vector dp(n + 1, vector<int>(n + 1, -1000000));\n for(int k = 1; k <= n; k++) {\n for(int l = 0, r = k; r <= n; l++, r++) {\n for(int i = 0; i <= n; i++) ep[i] = 0;\n for(int i = l; i < r; i++) {\n ep[i + 1] = max(ep[i + 1], ep[i]);\n for(int j = i + 1; j <= r; j++) {\n if(dp[i][j] != -INF) {\n ep[j] = max(ep[j], ep[i] + 1);\n }\n }\n }\n for(int c = 0; c <= ep[r]; c++) {\n if(((s[r] - s[l]) - rem * c) % div == rem) {\n dp[l][r] = c + 1;\n break;\n }\n }\n }\n }\n // for(int l = 0; l <= n; l++) {\n // for(int r = 0; r <= n; r++) {\n // if(l > r) cout << \" \" << \" \";\n // else if(dp[l][r] == -INF) cout << '.' << \" \";\n // else cout << dp[l][r] << \" \";\n // } cout << \"\\n\";\n // }\n vector<ll> hp(n + 1);\n for(int l = 0; l < n; l++) {\n hp[l + 1] = max(hp[l + 1], hp[l]);\n for(int r = l + 1; r <= n; r++) {\n if(dp[l][r] != -1000000) {\n hp[r] = max(hp[r], hp[l] + (s[r] - s[l] - rem * dp[l][r]) / div);\n }\n }\n }\n cout << hp[n] << endl;\n}", "accuracy": 0.1320754716981132, "time_ms": 1320, "memory_kb": 4456, "score_of_the_acc": -0.9358, "final_rank": 14 }, { "submission_id": "aoj_1466_10041611", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC optimize(\"O3\")\n\nusing ll = long long;\n\nint main() {\n int n;\n ll div, rem;\n cin >> n >> div >> rem;\n vector<ll> s(n + 1);\n for(int i = 1; i <= n; i++) {\n cin >> s[i];\n s[i] += s[i - 1];\n }\n const ll INF = LONG_MAX / 4;\n vector<int> ep(n + 1);\n vector dp(n + 1, vector<int>(n + 1, -1000000));\n for(int k = 1; k <= n; k++) {\n for(int l = 0, r = k; r <= n; l++, r++) {\n for(int i = 0; i <= n; i++) ep[i] = 0;\n for(int i = l; i < r; i++) {\n ep[i + 1] = max(ep[i + 1], ep[i]);\n for(int j = i + 1; j <= r; j++) {\n if(dp[i][j] != -INF) {\n ep[j] = max(ep[j], ep[i] + 1);\n }\n }\n }\n for(int c = 0; c <= ep[r]; c++) {\n if(((s[r] - s[l]) - rem * c) % div == rem) {\n dp[l][r] = c + 1;\n break;\n }\n }\n }\n }\n // for(int l = 0; l <= n; l++) {\n // for(int r = 0; r <= n; r++) {\n // if(l > r) cout << \" \" << \" \";\n // else if(dp[l][r] == -INF) cout << '.' << \" \";\n // else cout << dp[l][r] << \" \";\n // } cout << \"\\n\";\n // }\n vector<ll> hp(n + 1);\n for(int l = 0; l < n; l++) {\n hp[l + 1] = max(hp[l + 1], hp[l]);\n for(int r = l + 1; r <= n; r++) {\n if(dp[l][r] != -INF) {\n hp[r] = max(hp[r], hp[l] + (s[r] - s[l] - rem * dp[l][r]) / div);\n }\n }\n }\n cout << hp[n] << endl;\n}", "accuracy": 0.018867924528301886, "time_ms": 1280, "memory_kb": 4364, "score_of_the_acc": -0.9071, "final_rank": 20 }, { "submission_id": "aoj_1466_10041308", "code_snippet": "#ifdef DEBUG\n#include\"stdlibrary.h\"\n#else\n// #pragma GCC target(\"avx\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n// #include<ext/pb_ds/assoc_container.hpp>\n// #include<ext/pb_ds/tree_policy.hpp>\n// #include<ext/pb_ds/tag_and_trait.hpp>\n// using namespace __gnu_pbds;\n// #include<boost/multiprecision/cpp_int.hpp>\n// namespace multiprecisioninteger = boost::multiprecision;\n// using cint=multiprecisioninteger::cpp_int;\nusing ll=long long;\nusing datas=std::pair<ll,ll>;\nusing ddatas=std::pair<long double,long double>;\nusing tdata=std::pair<ll,datas>;\nusing vec=std::vector<ll>;\nusing mat=std::vector<vec>;\nusing pvec=std::vector<datas>;\nusing pmat=std::vector<pvec>;\n// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;\n#define For(i,a,b) if(const std::int64_t _iterate_from=a,_iterate_to=b;_iterate_from<_iterate_to)for(const std::int64_t i:std::views::iota(_iterate_from,_iterate_to))\n#define bFor(i,b,a) for(ll i=b-1;i>=(ll)a;--i)\n#define rep(i,N) For(i,0,N)\n#define rep1(i,N) For(i,1,N)\n#define brep(i,N) bFor(i,N,0)\n#define brep1(i,N) bFor(i,N,1)\n#define all(v) std::begin(v),std::end(v)\n#define allr(v) std::rbegin(v),std::rend(v)\n#define vsort(v) std::sort(all(v))\n#define vrsort(v) std::sort(allr(v))\n#define uniq(v) vsort(v),(v).erase(std::unique(all(v)),std::end(v))\n#define endl \"\\n\"\n#define popcount __builtin_popcountll\n#define print(x) std::cout<<x<<endl\n#define printyes print(\"Yes\")\n#define printno print(\"No\")\n#define printYES print(\"YES\")\n#define printNO print(\"NO\")\n#define output(v) do{bool f=0;for(auto outi:v){std::cout<<(f?\" \":\"\")<<outi;f=1;}std::cout<<endl;}while(0)\n#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)\n// constexpr ll mod=1000000007;\nconstexpr ll mod=998244353;\nconstexpr ll inf=1LL<<60;\nconstexpr long double eps=1e-9;\nconst long double PI=acosl(-1);\ntemplate<class T,auto x=T::mod()> std::ostream& operator<<(std::ostream& os,const T& v){return os<<v.val();}\ntemplate<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v);\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v);\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v);\ntemplate<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v);\ntemplate<class T,class E> std::ostream& operator<<(std::ostream& os,const std::pair<T,E>& p){return os<<\"(\"<<p.first<<\",\"<<p.second<<\")\";}\ntemplate<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T> inline bool chmax(T& a,const T b){bool x=a<b;if(x)a=b;return x;}\ntemplate<class T> inline bool chmin(T& a,const T b){bool x=a>b;if(x)a=b;return x;}\n#ifdef DEBUG\nvoid debugg(){std::cout<<endl;}\ntemplate<class T,class... Args>void debugg(const T& x,const Args&... args){std::cout<<\" \"<<x;debugg(args...);}\n#define debug(...) cout<<__LINE__<<\" [\"<<#__VA_ARGS__<<\"]:\",debugg(__VA_ARGS__)\n#else\n#define debug(...) (void(0))\n#endif\n\ninline void startupcpp(void) noexcept{\n cin.tie(0)->sync_with_stdio(0);\n cout<<fixed<<setprecision(15);\n}\n\nll modinv(ll a,const ll m=mod) noexcept{\n ll b=m,u=1,v=0,t;\n while(b){\n t=a/b;\n a-=t*b; swap(a,b);\n u-=t*v; swap(u,v);\n }\n return (u+m)%m;\n}\n\nll moddevide(const ll a,const ll b) noexcept{return (a*modinv(b))%mod;}\n\nvec modncrlistp,modncrlistm;\n\nll modncr(const ll n,const ll r){\n if(n<r||r<0)return 0;\n ll i,size=modncrlistp.size();\n if(size<=n){\n modncrlistp.resize(n+1);\n modncrlistm.resize(n+1);\n if(!size){\n modncrlistp[0]=modncrlistm[0]=1;\n size++;\n }\n For(i,size,n+1)modncrlistp[i]=modncrlistp[i-1]*i%mod;\n modncrlistm[n]=modinv(modncrlistp[n]);\n for(i=n;i>size;--i)modncrlistm[i-1]=modncrlistm[i]*i%mod;\n }\n return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod;\n}\n\nll modpow(ll a,ll n,const ll m=mod){\n if(n<0)return 0;\n ll res=1;\n while(n>0){\n if(n&1)res=res*a%m;\n a=a*a%m;\n n>>=1;\n }\n return res;\n}\n\nconstexpr ll gcd(const ll a,const ll b) noexcept{return (!b)?abs(a):(a%b==0)?abs(b):gcd(b,a%b);}\nconstexpr ll lcm(const ll a,const ll b) noexcept{return a/gcd(a,b)*b;}\n\n// #include<atcoder/all>\n// using mint=atcoder::modint998244353;\n// void operator>>(istream& is,mint& v){long long x;is>>x;v=x;}\n\nll solve(const ll D, const ll R, const vec& a){\n const ll N=a.size();\n vec s(N+1);\n rep(i,N)s[i+1]=s[i]+a[i];\n\n mat dp(N,vec(N+1,0));\n vector<tuple<ll,ll,ll>> cs;\n rep1(j,N+1)brep(i,j){\n // [i,j)\n const ll sum=s[j]-s[i];\n For(k,i,j)chmax(dp[i][j],dp[i][k]+dp[k][j]);\n rep(k,dp[i][j]+1){\n if((sum-k*R)%D!=R)continue;\n chmax(dp[i][j],(ll)k+1);\n cs.emplace_back(i,j,(sum-(k+1)*R)/D);\n }\n }\n pmat g(N);\n for(const auto& [l,r,c]:cs)g[l].emplace_back(c,r);\n vec dp2(N+1,0);\n rep(i,N){\n for(const auto& [c,n]:g[i])chmax(dp2[n],dp2[i]+c);\n chmax(dp2[i+1],dp2[i]);\n }\n return dp2[N];\n}\n\nll naive(const ll D, const ll R, const vec& a){\n map<vec,ll> mp;\n queue<pair<vec,ll>> que;\n que.emplace(a,0);\n mp[a]=0;\n while(!que.empty()){\n const auto [v,cost]=que.front();que.pop();\n if(mp[v]!=cost)continue;\n vec s(v.size()+1);\n rep(i,v.size())s[i+1]=s[i]+v[i];\n rep1(j,a.size()+1)rep(i,j){\n if((s[j]-s[i])%D!=R)continue;\n vec nv=v;\n For(k,i,j)nv[k]=0;\n if(chmax(mp[nv],cost+(s[j]-s[i]-R)/D))\n que.emplace(nv,cost+(s[j]-s[i]-R)/D);\n }\n }\n return ranges::max(mp|ranges::views::values);\n}\n\nint main(){\n startupcpp();\n ll N,D,R;\n cin>>N>>D>>R;\n vec a(N);\n rep(i,N)cin>>a[i];\n\n print(solve(D,R,a));\n\n // ll N,D,R;\n // vec a;\n // do{\n // N=1+rand()%10;\n // D=2+rand()%10;\n // R=rand()%D;\n // a.resize(N);\n // rep(i,N)a[i]=rand()%10;\n // if(solve(D,R,a)!=naive(D,R,a)){\n // print(D);\n // print(R);\n // output(a);\n // print(solve(D,R,a));\n // print(naive(D,R,a));\n // break;\n // }\n // }while(1);\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 1781060, "score_of_the_acc": -1.6643, "final_rank": 12 }, { "submission_id": "aoj_1466_10041228", "code_snippet": "#ifdef DEBUG\n#include\"stdlibrary.h\"\n#else\n// #pragma GCC target(\"avx\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n// #include<ext/pb_ds/assoc_container.hpp>\n// #include<ext/pb_ds/tree_policy.hpp>\n// #include<ext/pb_ds/tag_and_trait.hpp>\n// using namespace __gnu_pbds;\n// #include<boost/multiprecision/cpp_int.hpp>\n// namespace multiprecisioninteger = boost::multiprecision;\n// using cint=multiprecisioninteger::cpp_int;\nusing ll=long long;\nusing datas=std::pair<ll,ll>;\nusing ddatas=std::pair<long double,long double>;\nusing tdata=std::pair<ll,datas>;\nusing vec=std::vector<ll>;\nusing mat=std::vector<vec>;\nusing pvec=std::vector<datas>;\nusing pmat=std::vector<pvec>;\n// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;\n#define For(i,a,b) if(const std::int64_t _iterate_from=a,_iterate_to=b;_iterate_from<_iterate_to)for(const std::int64_t i:std::views::iota(_iterate_from,_iterate_to))\n#define bFor(i,b,a) for(ll i=b-1;i>=(ll)a;--i)\n#define rep(i,N) For(i,0,N)\n#define rep1(i,N) For(i,1,N)\n#define brep(i,N) bFor(i,N,0)\n#define brep1(i,N) bFor(i,N,1)\n#define all(v) std::begin(v),std::end(v)\n#define allr(v) std::rbegin(v),std::rend(v)\n#define vsort(v) std::sort(all(v))\n#define vrsort(v) std::sort(allr(v))\n#define uniq(v) vsort(v),(v).erase(std::unique(all(v)),std::end(v))\n#define endl \"\\n\"\n#define popcount __builtin_popcountll\n#define print(x) std::cout<<x<<endl\n#define printyes print(\"Yes\")\n#define printno print(\"No\")\n#define printYES print(\"YES\")\n#define printNO print(\"NO\")\n#define output(v) do{bool f=0;for(auto outi:v){std::cout<<(f?\" \":\"\")<<outi;f=1;}std::cout<<endl;}while(0)\n#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)\n// constexpr ll mod=1000000007;\nconstexpr ll mod=998244353;\nconstexpr ll inf=1LL<<60;\nconstexpr long double eps=1e-9;\nconst long double PI=acosl(-1);\ntemplate<class T,auto x=T::mod()> std::ostream& operator<<(std::ostream& os,const T& v){return os<<v.val();}\ntemplate<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v);\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v);\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v);\ntemplate<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v);\ntemplate<class T,class E> std::ostream& operator<<(std::ostream& os,const std::pair<T,E>& p){return os<<\"(\"<<p.first<<\",\"<<p.second<<\")\";}\ntemplate<class T> std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::set<T,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class F> std::ostream& operator<<(std::ostream& os,const std::multiset<T,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T,class E,class F> std::ostream& operator<<(std::ostream& os,const std::map<T,E,F>& v){\n os<<\"{\";bool f=false;\n for(auto& x:v){if(f)os<<\",\";os<<x;f=true;}\n os<<\"}\";\n return os;\n}\ntemplate<class T> inline bool chmax(T& a,const T b){bool x=a<b;if(x)a=b;return x;}\ntemplate<class T> inline bool chmin(T& a,const T b){bool x=a>b;if(x)a=b;return x;}\n#ifdef DEBUG\nvoid debugg(){std::cout<<endl;}\ntemplate<class T,class... Args>void debugg(const T& x,const Args&... args){std::cout<<\" \"<<x;debugg(args...);}\n#define debug(...) cout<<__LINE__<<\" [\"<<#__VA_ARGS__<<\"]:\",debugg(__VA_ARGS__)\n#else\n#define debug(...) (void(0))\n#endif\n\ninline void startupcpp(void) noexcept{\n cin.tie(0)->sync_with_stdio(0);\n cout<<fixed<<setprecision(15);\n}\n\nll modinv(ll a,const ll m=mod) noexcept{\n ll b=m,u=1,v=0,t;\n while(b){\n t=a/b;\n a-=t*b; swap(a,b);\n u-=t*v; swap(u,v);\n }\n return (u+m)%m;\n}\n\nll moddevide(const ll a,const ll b) noexcept{return (a*modinv(b))%mod;}\n\nvec modncrlistp,modncrlistm;\n\nll modncr(const ll n,const ll r){\n if(n<r||r<0)return 0;\n ll i,size=modncrlistp.size();\n if(size<=n){\n modncrlistp.resize(n+1);\n modncrlistm.resize(n+1);\n if(!size){\n modncrlistp[0]=modncrlistm[0]=1;\n size++;\n }\n For(i,size,n+1)modncrlistp[i]=modncrlistp[i-1]*i%mod;\n modncrlistm[n]=modinv(modncrlistp[n]);\n for(i=n;i>size;--i)modncrlistm[i-1]=modncrlistm[i]*i%mod;\n }\n return modncrlistp[n]*modncrlistm[r]%mod*modncrlistm[n-r]%mod;\n}\n\nll modpow(ll a,ll n,const ll m=mod){\n if(n<0)return 0;\n ll res=1;\n while(n>0){\n if(n&1)res=res*a%m;\n a=a*a%m;\n n>>=1;\n }\n return res;\n}\n\nconstexpr ll gcd(const ll a,const ll b) noexcept{return (!b)?abs(a):(a%b==0)?abs(b):gcd(b,a%b);}\nconstexpr ll lcm(const ll a,const ll b) noexcept{return a/gcd(a,b)*b;}\n\n// #include<atcoder/all>\n// using mint=atcoder::modint998244353;\n// void operator>>(istream& is,mint& v){long long x;is>>x;v=x;}\n\nll solve(const ll D, const ll R, const vec& a){\n const ll N=a.size();\n vec s(N+1);\n rep(i,N)s[i+1]=s[i]+a[i];\n\n ll ans=0;\n mat dp(N,vec(N+1,0));\n rep1(j,N+1)brep(i,j){\n // [i,j)\n const ll sum=s[j]-s[i];\n For(k,i,j)chmax(dp[i][j],dp[i][k]+dp[k][j]);\n rep(k,dp[i][j]+1){\n if((sum-k*R)%D!=R)continue;\n chmax(dp[i][j],(ll)k+1);\n chmax(ans,(sum-(k+1)*R)/D);\n }\n }\n return ans;\n}\n\nint main(){\n startupcpp();\n ll N,D,R;\n cin>>N>>D>>R;\n vec a(N);\n rep(i,N)cin>>a[i];\n\n print(solve(D,R,a));\n}", "accuracy": 0.018867924528301886, "time_ms": 100, "memory_kb": 5376, "score_of_the_acc": -0.0649, "final_rank": 19 }, { "submission_id": "aoj_1466_10041167", "code_snippet": "#include <bits/stdc++.h>\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)\nusing namespace std;\nconst int inf = 1000000007;\nconst ll longinf = 1ll << 60;\n\n// 返り値: a と b の最大公約数\n// ax + by = gcd(a, b) を満たす (x, y) が格納される\nlong long extGCD(long long a, long long b, long long &x, long long &y) {\n if(b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n long long d = extGCD(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int n;\n cin >> n;\n ll d, k;\n cin >> d >> k;\n vector<ll> a(n);\n rep(i, n) cin >> a[i];\n vector dp(n + 1, vector<ll>(n + 1, 0));\n rep(r, n + 1) {\n ll sum = 0;\n for(int l = r - 1; l >= 0; l--) {\n sum += a[l];\n for(int m = l + 1; m < r; m++) {\n dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);\n }\n if((sum - (dp[l][r] + 1) * k) % d == 0) {\n dp[l][r] += 1;\n }\n }\n }\n vector<ll> dp2(n + 1);\n ll g = gcd(d, k);\n REP(r, 1, n + 1) {\n ll sum = 0;\n dp2[r] = dp2[r - 1];\n for(int l = r - 1; l >= 0; l--) {\n sum += a[l];\n if(sum % g != 0) {\n continue;\n }\n ll x, y;\n extGCD(d, k, x, y);\n ll cnt = sum / g;\n y = (y * cnt) % (d / g);\n if(y <= 0) {\n y += d / g;\n }\n if(dp[l][r] >= y) {\n dp2[r] = max(dp2[r], dp2[l] + (sum - y * k) / d);\n }\n }\n }\n cout << dp2[n] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5376, "score_of_the_acc": -0.0077, "final_rank": 1 }, { "submission_id": "aoj_1466_10041160", "code_snippet": "#include <bits/stdc++.h>\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)\nusing namespace std;\nconst int inf = 1000000007;\nconst ll longinf = 1ll << 60;\n\n// 返り値: a と b の最大公約数\n// ax + by = gcd(a, b) を満たす (x, y) が格納される\nlong long extGCD(long long a, long long b, long long &x, long long &y) {\n if(b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n long long d = extGCD(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int n;\n cin >> n;\n ll d, k;\n cin >> d >> k;\n vector<ll> a(n);\n rep(i, n) cin >> a[i];\n vector dp(n + 1, vector<ll>(n + 1, 0));\n rep(r, n + 1) {\n ll sum = 0;\n for(int l = r - 1; l >= 0; l--) {\n sum += a[l];\n for(int m = l + 1; m < r; m++) {\n dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);\n }\n if((sum - (dp[l][r] + 1) * k) % d == 0) {\n dp[l][r] += 1;\n }\n }\n }\n vector<ll> dp2(n + 1);\n ll g = gcd(d, k);\n REP(r, 1, n + 1) {\n ll sum = 0;\n dp2[r] = dp2[r - 1];\n for(int l = r - 1; l >= 0; l--) {\n sum += a[l];\n if(sum % g != 0) {\n continue;\n }\n ll x, y;\n extGCD(d, k, x, y);\n ll cnt = sum / g;\n y = (y * cnt) % (d / g);\n x = (x * cnt) % (k / g);\n if(y <= 0) {\n y += d / g;\n x -= k / g;\n }\n if(dp[l][r] >= y) {\n dp2[r] = max(dp2[r], dp2[l] + (sum - y * k) / d);\n }\n }\n }\n cout << dp2[n] << endl;\n return 0;\n}", "accuracy": 0.018867924528301886, "time_ms": 10, "memory_kb": 5376, "score_of_the_acc": -0.0006, "final_rank": 18 }, { "submission_id": "aoj_1466_10041149", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)\n\nint main(){\n int N, D, R;\n cin >> N >> D >> R;\n vector<ll> A(N);\n for (auto &a : A) cin >> a;\n vector<ll> S(N + 1);\n rep(i, 0, N) S[i + 1] = S[i] + A[i];\n vector C(N + 1, vector<ll>(N + 1, 1 << 30));\n rep(l, 0, N) rep(r, l + 1, N + 1){\n ll sum = (S[r] - S[l]) % D;\n rep(c, 1, N + 1){\n sum = (sum + D - R) % D;\n if (sum == 0){\n C[l][r] = c;\n // cout << \"C \" << l << \" \" << r << \" \" << c << endl;\n break;\n }\n }\n }\n vector dp(N + 1, vector<ll>(N + 1));\n vector wei(N + 1, vector<ll>(N + 1, 0));\n for (int l = N - 1; l >= 0; l--) rep(r, l + 1, N + 1){\n rep(k, l + 1, r){\n dp[l][r] = max(dp[l][r], dp[l][k] + dp[k][r]);\n }\n if (dp[l][r] >= C[l][r] - 1){\n dp[l][r] = max(dp[l][r], C[l][r]);\n wei[l][r] = (S[r] - S[l] - C[l][r] * R) / D;\n // cout << l << \" \" << r << \" \" << wei[l][r] << endl;\n }\n }\n vector<ll> ans(N + 1);\n rep(l, 0, N) rep(r, l + 1, N + 1){\n ans[r] = max(ans[r], ans[l] + wei[l][r]);\n }\n cout << ans.back() << \"\\n\";\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 9388, "score_of_the_acc": -0.41, "final_rank": 7 }, { "submission_id": "aoj_1466_10041148", "code_snippet": "//#define _GLIBCXX_DEBUG\n\n//#pragma GCC target(\"avx2\")\n//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n\n#ifdef LOCAL\n#include <debug_print.hpp>\n#define OUT(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)\n#else\n#define OUT(...) (static_cast<void>(0))\n#endif\n\n#define endl '\\n'\n#define lfs cout<<fixed<<setprecision(15)\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())\n#define spa << \" \" <<\n#define fi first\n#define se second\n#define MP make_pair\n#define MT make_tuple\n#define PB push_back\n#define EB emplace_back\n#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)\n#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)\n\nnamespace template_tute{\n using ll = long long;\n using ld = long double;\n const ll MOD1 = 1e9+7;\n const ll MOD9 = 998244353;\n const ll INF = 4.1e18;\n using P = pair<ll, ll>;\n template<typename T> using PQ = priority_queue<T>;\n template<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;\n template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}\n template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}\n ll median(ll a,ll b, ll c){return a+b+c-max<ll>({a,b,c})-min<ll>({a,b,c});}\n void ans1(bool x){if(x) cout<<\"Yes\"<<endl;else cout<<\"No\"<<endl;}\n void ans2(bool x){if(x) cout<<\"YES\"<<endl;else cout<<\"NO\"<<endl;}\n void ans3(bool x){if(x) cout<<\"Yay!\"<<endl;else cout<<\":(\"<<endl;}\n template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;} \n template<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);}; \n template<typename T>void debug(const T &v,ll h,ll w,string sv=\" \"){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};\n template<typename T>void debug(const T &v,ll n,string sv=\" \"){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};\n template<typename T>void debug(const vector<T>&v){debug(v,v.size());}\n template<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}\n template<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\n template<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop();}cout<<endl;}\n template<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<\" \";st.pop_front();}cout<<endl;}\n template<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\n template<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<\" \";st.pop();}cout<<endl;}\n template<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\n template<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<\" \";cout<<endl;}\n template<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<\" \";cout<<endl;}\n template<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<\"[\"<<z.first<<\"]=\"<<z.second<<\",\";cout<<endl;}\n template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}\n vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};\n template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}\n template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}\n template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << \"(\" << p.first << \",\" << p.second << \")\";}\n template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){os<<\"[\";for(auto &z:v)os << z << \",\";os<<\"]\"; return os;}\n template<typename T>void rearrange(vector<int>&ord, vector<T>&v){\n auto tmp = v;\n for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];\n }\n template<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){\n rearrange(ord, head);\n rearrange(ord, tail...);\n }\n template<typename T> vector<int> ascend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],i)<make_pair(v[j],j);});\n return ord;\n }\n template<typename T> vector<int> descend(const vector<T>&v){\n vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);\n sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],-i)>make_pair(v[j],-j);});\n return ord;\n }\n template<typename T> vector<T> inv_perm(const vector<T>&ord){\n vector<T>inv(ord.size());\n for(int i=0;i<ord.size();i++)inv[ord[i]] = i;\n return inv;\n }\n ll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}\n ll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}\n ll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}\n ll modulo(ll n,ll d){return (n%d+d)%d;};\n template<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}\n template<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}\n template<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};\n template<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};\n //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());\n int popcount(ll x){return __builtin_popcountll(x);};\n int poplow(ll x){return __builtin_ctzll(x);};\n int pophigh(ll x){return 63 - __builtin_clzll(x);};\n template<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};\n template<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};\n template<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};\n template<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};\n ll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}\n ll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}\n ll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}\n std::ostream &operator<<(std::ostream &dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n }\n namespace converter{\n int dict[500];\n const string lower=\"abcdefghijklmnopqrstuvwxyz\";\n const string upper=\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n const string digit=\"0123456789\";\n const string digit1=\"123456789\";\n void regi_str(const string &t){\n for(int i=0;i<t.size();i++){\n dict[t[i]]=i;\n }\n }\n void regi_int(const string &t){\n for(int i=0;i<t.size();i++){\n dict[i]=t[i];\n }\n }\n vector<int>to_int(const string &s,const string &t){\n regi_str(t);\n vector<int>ret(s.size());\n for(int i=0;i<s.size();i++){\n ret[i]=dict[s[i]];\n }\n return ret;\n }\n vector<int>to_int(const string &s){\n auto t=s;\n sort(t.begin(),t.end());\n t.erase(unique(t.begin(),t.end()),t.end());\n return to_int(s,t);\n }\n \n vector<vector<int>>to_int(const vector<string>&s,const string &t){\n regi_str(t);\n vector<vector<int>>ret(s.size(),vector<int>(s[0].size()));\n for(int i=0;i<s.size();i++){\n for(int j=0;j<s[0].size();j++){\n ret[i][j]=dict[s[i][j]];\n }\n }\n return ret;\n }\n vector<vector<int>>to_int(const vector<string>&s){\n string t;\n for(int i=0;i<s.size();i++){\n t+=s[i];\n }\n sort(t.begin(),t.end());t.erase(unique(t.begin(),t.end()),t.end());\n return to_int(s,t);\n }\n string to_str(const vector<int>&s,const string &t){\n regi_int(t);\n string ret;\n for(auto z:s)ret+=dict[z];\n return ret;\n }\n vector<string> to_str(const vector<vector<int>>&s,const string &t){\n regi_int(t);\n vector<string>ret(s.size());\n for(int i=0;i<s.size();i++){\n for(auto z:s[i])ret[i]+=dict[z];\n }\n return ret;\n }\n }\n template< typename T = int >\n struct edge {\n int to;\n T cost;\n int id;\n edge():to(-1),id(-1){};\n edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}\n operator int() const { return to; }\n };\n\n template<typename T>\n using Graph = vector<vector<edge<T>>>;\n template<typename T>\n Graph<T>revgraph(const Graph<T> &g){\n Graph<T>ret(g.size());\n for(int i=0;i<g.size();i++){\n for(auto e:g[i]){\n int to = e.to;\n e.to = i;\n ret[to].push_back(e);\n }\n }\n return ret;\n }\n template<typename T>\n Graph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){\n Graph<T> ret(n);\n for(int es = 0; es < m; es++){\n int u,v;\n T w=1;\n cin>>u>>v;u-=indexed,v-=indexed;\n if(weighted)cin>>w;\n ret[u].emplace_back(v,w,es);\n if(!directed)ret[v].emplace_back(u,w,es);\n }\n return ret;\n }\n template<typename T>\n Graph<T> readParent(int n,int indexed=1,bool directed=true){\n Graph<T>ret(n);\n for(int i=1;i<n;i++){\n int p;cin>>p;\n p-=indexed;\n ret[p].emplace_back(i);\n if(!directed)ret[i].emplace_back(p);\n }\n return ret;\n }\n}\nusing namespace template_tute;\n\nvoid solve(){\n\tll res=0,buf=0;\n bool judge = true;\n\n ll n,d,rem;cin>>n>>d>>rem;\n vector<ll>a(n);\n rep(i,0,n)cin>>a[i];\n //rep(i,0,n)a[i]=1;\n vector<ll>sa(n+1);\n rep(i,0,n)sa[i+1]=sa[i]+a[i];\n auto c=vec(n+1,n+1,0LL);\n auto dpc=vec(n+1,n+1,INF);\n rrep(l,0,n){\n dpc[l]=dpc[l+1];\n dpc[l][0]=l+1;\n vector<bool>used(n+1,false);\n while(1){\n ll mi=INF,num=-1;\n rep(k,0,n){\n if(!used[k]&&chmin(mi,dpc[l][k])){\n num=k;\n }\n }\n if(num==-1)break;\n used[num]=true;\n rep(nk,0,n+1){\n if(num+nk<=n){\n chmin(dpc[l][num+nk],dpc[mi][nk]);\n }\n }\n rep(r,mi,n+1){\n if(modulo(sa[r]-sa[l]-num*rem,d)==rem){\n //OUT(l,r,num,sa[r]-sa[l],num*rem);\n chmin(dpc[l][num+1],r);\n chmax(c[l][r],(sa[r]-sa[l]-(num+1)*rem)/d);\n }\n }\n }\n OUT(l,dpc[l]);\n }\n vector<ll>dp(n+1,0);\n rep(i,0,n){\n rep(j,i+1,n+1){\n chmax(dp[j],dp[i]+c[i][j]);\n }\n chmax(dp[i+1],dp[i]);\n }\n cout<<dp[n]<<endl;\n}\n\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n ll res=0,buf=0;\n bool judge = true;\n int T = 1;\n //cin>>T;\n while(T--){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1410, "memory_kb": 7056, "score_of_the_acc": -1.0015, "final_rank": 11 }, { "submission_id": "aoj_1466_10041138", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\ntypedef bitset<501> BS;\n\nconst int MAXN = 500;\n\nint N, D, R, A[MAXN+10];\nll S[MAXN+10];\nint dp[MAXN+10][MAXN+10];\nBS dp2[MAXN+10];\nll dp3[MAXN+10];\n\nint main()\n{\n ios_base::sync_with_stdio(false); cin.tie(NULL);\n\n cin >> N >> D >> R;\n for(int i=1; i<=N; i++) cin >> A[i], S[i]=S[i-1]+A[i];\n\n for(int l=N; l>=1; l--)\n {\n dp2[l-1][0]=1;\n for(int r=l; r<=N; r++)\n {\n dp2[r]=dp2[r-1];\n for(int i=l; i<r; i++) dp2[r]|=dp2[i]<<dp[i+1][r];\n ll sum=S[r]-S[l-1];\n dp[l][r]=N+1;\n for(int i=N; i>=1; i--) if(dp2[r][i-1] && (ll)i*R%D==sum%D)\n {\n dp2[r][i]=1;\n dp[l][r]=i;\n }\n // printf(\"%d %d : %d\\n\", l, r, dp[l][r]);\n // for(int i=0; i<=N; i++) if(dp2[r][i]) printf(\"%d \", i);\n // printf(\"\\n\");\n }\n }\n\n for(int i=1; i<=N; i++)\n {\n dp3[i]=dp3[i-1];\n for(int j=1; j<=i; j++)\n {\n if(dp[j][i]<=N) dp3[i]=max(dp3[i], dp3[j-1]+(S[i]-S[j-1]-(ll)dp[j][i]*R)/D);\n }\n }\n cout << dp3[N] << \"\\n\";\n}", "accuracy": 1, "time_ms": 790, "memory_kb": 4504, "score_of_the_acc": -0.5572, "final_rank": 9 }, { "submission_id": "aoj_1466_10041131", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\ntypedef bitset<501> BS;\n\nconst int MAXN = 500;\n\nint N, D, R, A[MAXN+10];\nll S[MAXN+10];\nint dp[MAXN+10][MAXN+10];\nBS dp2[MAXN+10];\nll dp3[MAXN+10];\n\nint main()\n{\n ios_base::sync_with_stdio(false); cin.tie(NULL);\n\n cin >> N >> D >> R;\n for(int i=1; i<=N; i++) cin >> A[i], S[i]=S[i-1]+A[i];\n\n for(int l=N; l>=1; l--)\n {\n dp2[l-1][0]=1;\n for(int r=l; r<=N; r++)\n {\n dp2[r]=dp2[r-1];\n for(int i=l; i<r; i++) dp2[r]|=dp2[i]<<dp[i+1][r];\n ll sum=S[r]-S[l-1];\n dp[l][r]=N+1;\n for(int i=N; i>=1; i--) if(dp2[r][i-1] && (ll)i*R%D==sum%D)\n {\n dp2[r][i]=1;\n dp[l][r]=i;\n }\n // printf(\"%d %d : %d\\n\", l, r, dp[l][r]);\n // for(int i=0; i<=N; i++) if(dp2[r][i]) printf(\"%d \", i);\n // printf(\"\\n\");\n }\n }\n\n for(int i=1; i<=N; i++)\n {\n dp3[i]=dp3[i-1];\n for(int j=1; j<=i; j++)\n {\n if(dp[j][i]<=N) dp3[i]=max(dp3[i], dp3[j-1]+(S[i]-S[j-1]-dp[j][i]*R)/D);\n }\n }\n cout << dp3[N] << \"\\n\";\n}", "accuracy": 0.2830188679245283, "time_ms": 560, "memory_kb": 4500, "score_of_the_acc": -0.3929, "final_rank": 13 }, { "submission_id": "aoj_1466_10041065", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define vi vector<int>\n#define vl vector<ll>\n#define vii vector<pair<int,int>>\n#define vll vector<pair<ll,ll>>\n#define vvi vector<vector<int>>\n#define vvl vector<vector<ll>>\n#define vvii vector<vector<pair<int,int>>>\n#define vvll vector<vector<pair<ll,ll>>>\n#define vst vector<string>\n#define pii pair<int,int>\n#define pll pair<ll,ll>\n#define pb push_back\n#define all(x) (x).begin(),(x).end()\n#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=505;\nconst ll INF=15LL<<58;\n\nll S[MAX];\nll mi[MAX][MAX];\nll sub[MAX][MAX];\nll dp[MAX];\nll T[MAX][MAX];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n ll N,A,B;cin>>N>>A>>B;\n for(int i=0;i<N;i++){\n ll x;cin>>x;\n S[i+1]=S[i]+x;\n }\n for(int i=0;i<MAX;i++){\n for(int j=0;j<MAX;j++){\n T[i][j]=INF;\n sub[i][j]=INF;\n mi[i][j]=INF;\n }\n }\n \n for(int l=N-1;l>=0;l--){\n for(int r=l+1;r<=N;r++){\n if((S[r]-S[l])%A==B){\n chmin(T[1][l],(ll)(r));\n }\n }\n for(int len=1;len<=N;len++){\n for(int i=N;i>=0;i--){\n chmin(T[len][i],T[len][i+1]);\n }\n }\n sub[l][0]=l;\n for(int s=0;s<=N;s++){\n if(sub[l][s]==INF) continue;\n for(int ad=1;s+ad<=N;ad++){\n if(T[ad][sub[l][s]]<=N) chmin(sub[l][s+ad],T[ad][sub[l][s]]);\n }\n }\n \n for(int s=0;s<=N;s++){\n if(sub[l][s]==INF) continue;\n if(s){\n chmin(T[s][l],sub[l][s]);\n }\n for(int r=sub[l][s];r<=N;r++){\n if(r==l) continue;\n ll sum=S[r]-S[l];\n if(sum%A==(B*(s+1))%A){\n chmin(T[s+1][l],(ll)(r));\n chmin(mi[l][r],s+1LL);\n }\n }\n }\n }\n \n for(int i=0;i<N;i++){\n for(int j=i+1;j<=N;j++){\n if(mi[i][j]!=INF){\n chmax(dp[j],dp[i]+(S[j]-S[i]-mi[i][j]*B)/A);\n }\n }\n chmax(dp[i+1],dp[i]);\n }\n \n cout<<dp[N]<<endl;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 9456, "score_of_the_acc": -0.41, "final_rank": 8 }, { "submission_id": "aoj_1466_10041062", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n#define vi vector<int>\n#define vl vector<ll>\n#define vii vector<pair<int,int>>\n#define vll vector<pair<ll,ll>>\n#define vvi vector<vector<int>>\n#define vvl vector<vector<ll>>\n#define vvii vector<vector<pair<int,int>>>\n#define vvll vector<vector<pair<ll,ll>>>\n#define vst vector<string>\n#define pii pair<int,int>\n#define pll pair<ll,ll>\n#define pb push_back\n#define all(x) (x).begin(),(x).end()\n#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())\n#define fi first\n#define se second\n#define mp make_pair\n#define si(x) int(x.size())\nconst int mod=998244353,MAX=505;\nconst ll INF=15LL<<58;\n\nll S[MAX];\nll mi[MAX][MAX];\nll sub[MAX][MAX];\nll dp[MAX];\nll T[MAX][MAX];\n\nint main(){\n \n std::ifstream in(\"text.txt\");\n std::cin.rdbuf(in.rdbuf());\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n ll N,A,B;cin>>N>>A>>B;\n for(int i=0;i<N;i++){\n ll x;cin>>x;\n S[i+1]=S[i]+x;\n }\n for(int i=0;i<MAX;i++){\n for(int j=0;j<MAX;j++){\n T[i][j]=INF;\n sub[i][j]=INF;\n mi[i][j]=INF;\n }\n }\n \n for(int l=N-1;l>=0;l--){\n for(int r=l+1;r<=N;r++){\n if((S[r]-S[l])%A==B){\n chmin(T[1][l],(ll)(r));\n }\n }\n for(int len=1;len<=N;len++){\n for(int i=N;i>=0;i--){\n chmin(T[len][i],T[len][i+1]);\n }\n }\n sub[l][0]=l;\n for(int s=0;s<=N;s++){\n if(sub[l][s]==INF) continue;\n for(int ad=1;s+ad<=N;ad++){\n if(T[ad][sub[l][s]]<=N) chmin(sub[l][s+ad],T[ad][sub[l][s]]);\n }\n }\n \n for(int s=0;s<=N;s++){\n if(sub[l][s]==INF) continue;\n for(int r=sub[l][s];r<=N;r++){\n if(r==l) continue;\n ll sum=S[r]-S[l];\n if(sum%A==(B*(s+1))%A){\n chmin(mi[l][r],s+1LL);\n }\n }\n }\n }\n \n for(int i=0;i<N;i++){\n for(int j=i+1;j<=N;j++){\n if(mi[i][j]!=INF){\n chmax(dp[j],dp[i]+(S[j]-S[i]-mi[i][j]*B)/A);\n }\n }\n chmax(dp[i+1],dp[i]);\n }\n \n cout<<dp[N]<<endl;\n}", "accuracy": 0.07547169811320754, "time_ms": 300, "memory_kb": 9452, "score_of_the_acc": -0.21, "final_rank": 16 } ]
aoj_1501_cpp
Problem B : Grid r × c の2次元グリッド上の2つの座標 ( a 1 , a 2 ) と ( b 1 , b 2 ) が与えられる。 あるマス (e,f) から (e+1,f)、(e-1,f)、(e,f+1)、(e,f-1) のどれかのマスに移動するコストを1とする。 また、(e,c-1) と (e,0)、(r-1,f) と (0,f) の間もコスト1で移動することができる。 この時に1つ目の座標から2つ目の座標へ最短コストで移動できる経路の数を求めよ。 Input 入力は以下のフォーマットで与えられる。 r c a 1 a 2 b 1 b 2 入力は以下の制約を満たす 1 ≤ r , c ≤ 1,000 0 ≤ a 1 , b 1 < r 0 ≤ a 2 , b 2 < c Output 答えの値を100,000,007で割った余りを出力せよ。 Sample Input 1 4 4 0 0 3 3 Sample Output 1 2 Sample Input 2 4 4 0 0 1 1 Sample Output 2 2 Sample Input 3 2 3 0 0 1 2 Sample Output 3 4 Sample Input 4 500 500 0 0 200 200 Sample Output 4 34807775
[ { "submission_id": "aoj_1501_7916754", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i =0; i<(n); i++)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nconst int mod = 100000007;\nconst int inf ( 1<<30);\n\nint main(){\n\tint r,c; cin>>r>>c;\n\tvector<vector<ll>> table(r,vector<ll>(c));\n\tvector<vector<ll>> visit(r,vector<ll>(c,inf));\n\n\tint a1,a2; cin>>a1>>a2;\n\tqueue<P> que;\n\tque.push({a1,a2});\n\n\tint vx[4] = {1,-1,0,0};\n\tint vy[4] = {0,0,1,-1};\n\tvisit[a1][a2] = 0;\n\ttable[a1][a2] = 1; \n\twhile(que.size()){\n\t\tint x = que.front().first;\n\t\tint y = que.front().second;\n\t\tque.pop();\n\n\t\trep(v,4){\n\t\t\tint nx = (r+x+vx[v])%r;\n\t\t\tint ny = (c+y+vy[v])%c;\n\n\t\t\tif(visit[x][y]+1 < visit[nx][ny]){\n\t\t\t\ttable[nx][ny] = table[x][y];\n\t\t\t\ttable[nx][ny]%= mod;\n\t\t\t\tque.push({nx,ny});\n\t\t\t\tvisit[nx][ny] = visit[x][y]+1;\n\t\t\t}else if(visit[x][y]+1 == visit[nx][ny]){\n\t\t\t\ttable[nx][ny]+=table[x][y];\n\t\t\t\ttable[nx][ny]%=mod;\n\t\t\t}\n\t\t}\n\t}\n\tint b1,b2; cin>>b1>>b2;\n\tcout<<table[b1][b2]<<endl;\n\n\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 19072, "score_of_the_acc": -0.2826, "final_rank": 8 }, { "submission_id": "aoj_1501_7916708", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\n\nconst ll MOD = 1e8+7;\n\nll mod(ll x){\n return (((x % MOD)+MOD)%MOD);\n}\n\nll modadd(ll a,ll b){\n return (mod(a + b));\n}\n\nll modmul(ll a,ll b){\n return mod(a * b);\n}\n\nll modpow(ll a,ll b){\n ll res = 1;\n while(b){\n if(b & 1)res = modmul(res, a);\n a = modmul(a,a);\n b >>= 1;\n }\n return res;\n}\n\nll modinv(ll a){\n return modpow(a,MOD-2);\n}\n\nll moddiv(ll a,ll b){\n return modmul(a,modinv(b));\n}\n\nll factorial(int n){\n static vector<ll> dp(2,1);\n while(dp.size() <= n){\n ll add = modmul(dp.back(),dp.size());\n dp.push_back(add);\n }\n return dp[n];\n}\n\nint dx[]={1,0,-1,0};\nint dy[]={0,-1,0,1};\n\nint f(int x,int y){\n x %= y;\n x += y;\n x %= y;\n return x;\n}\n\nint main(){\n int r;\n int c;\n int x1;\n int x2;\n int y1;\n int y2;\n cin >> r >> c >> x1 >> y1 >> x2 >> y2;\n using P = pair<int,int>;\n auto edges = [&](P p) -> vector<P>{\n int x = p.first;\n int y = p.second;\n vector<P> res;\n for(int i=0;i<4;++i){\n int nx = f(x + dx[i],r);\n int ny = f(y + dy[i],c);\n res.emplace_back(nx,ny);\n }\n return res;\n };\n\n vector<vector<int>> dists(1001,vector<int>(1001,1e9));\n queue<P> que;\n auto add = [&](P p,int v) -> void{\n if(dists[p.first][p.second] > v){\n dists[p.first][p.second] = v;\n que.push(p);\n }\n };\n add(P(x1,y1),0);\n while(dists[x2][y2] > 1e5){\n P p = que.front();\n que.pop();\n auto v = edges(p);\n for(auto e:v){\n int nt = dists[p.first][p.second] + 1;\n add(e,nt);\n }\n }\n while(que.size())que.pop();\n vector<vector<int>> dps(1001,vector<int>(1001,-1));\n que.emplace(x2,y2);\n dps[x2][y2] = 1;\n\n\n while(que.size()){\n P p = que.front();\n que.pop();\n for(auto e:edges(p)){\n if(dists[e.first][e.second] + 1 != dists[p.first][p.second])continue;\n if(dps[e.first][e.second] == -1){\n dps[e.first][e.second] = dps[p.first][p.second];\n que.push(e);\n }else{\n ll x = modadd(dps[e.first][e.second], dps[p.first][p.second]);\n dps[e.first][e.second] = x;\n }\n }\n }\n cout << dps[x1][y1] << endl;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 11360, "score_of_the_acc": -0.9619, "final_rank": 11 }, { "submission_id": "aoj_1501_7916673", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing i64 = long long;\n\nconst i64 mod = 100000007;\n\nint main() {\n\tint r, c, a1, a2, b1, b2;\n\tcin >> r >> c >> a1 >> a2 >> b1 >> b2;\n\tvector dp(r, vector(c, 0LL));\n\tconst int inf = 100000000;\n\tvector mindis(r, vector(c, inf));\n\tdp[a1][a2] = 1LL;\n\tmindis[a1][a2] = 0;\n\tqueue<pair<int, int>> que;\n\tque.push({ a1, a2 });\n\n\n\tarray<int, 4> dy = { 1, 0, -1, 0 }, dx = { 0, 1, 0, -1 };\n\n\twhile (que.size()) {\n\t\tauto [y, x] = que.front();\n\t\tque.pop();\n\t\tfor (int d = 0 ; d < 4 ; d++) {\n\t\t\tint ny = y + dy[d];\n\t\t\tny = (ny + r) % r;\n\t\t\tint nx = x + dx[d];\n\t\t\tnx = (nx + c) % c;\n\t\t\tif (mindis[ny][nx] > mindis[y][x] + 1) {\n\t\t\t\tmindis[ny][nx] = mindis[y][x] + 1;\n\t\t\t\tdp[ny][nx] = dp[y][x];\n\t\t\t\tque.push({ ny, nx });\n\t\t\t}\n\t\t\telse if (mindis[ny][nx] == mindis[y][x] + 1) {\n\t\t\t\tdp[ny][nx] += dp[y][x];\n\t\t\t\tdp[ny][nx] %= mod;\n\t\t\t}\n\t\t}\n\t\t/*\n\t\tfor (auto d : dp) {\n\t\t\tfor (auto x : d) cout << x << ' ';\n\t\t\tcout << endl;\n\t\t}\n\t\tcout << endl;\n\t\t*/\n\t}\n\n/*\n\tfor (auto d : dp) {\n\t\tfor (auto x : d) cout << x << ' ';\n\t\tcout << endl;\n\t}\n\t*/\n\n\tcout << dp[b1][b2] << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 14948, "score_of_the_acc": -0.1514, "final_rank": 4 }, { "submission_id": "aoj_1501_4397104", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX = 1000000;\nconst int MOD = 100000007;\n\n\nlong long fac[MAX], finv[MAX], inv[MAX];\n\n// テーブルを作る前処理\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % MOD;\n //そのまま計算すると負の値になるのでMODを足す\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n}\n// 二項係数計算\nlong long COM(int n, int k){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\n\nint main() {\n // 前処理\n COMinit();\n\n long long int r, c, a1, a2, b1, b2, absb1, absb2, x, y;\n\n cin >> r >> c >> a1 >> a2 >> b1 >> b2;\n\n absb1 = r-abs(a1-b1);\n absb2 = c-abs(a2-b2);\n x = min(abs(a1-b1), absb1);\n y = min(abs(a2-b2), absb2);\n long long int ans = COM(x+y, x);\n if(r == 2*abs(a1-b1)) ans*=2;\n if(c == 2*abs(a2-b2)) ans*=2;\n cout << ans % MOD << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 26556, "score_of_the_acc": -0.2665, "final_rank": 7 }, { "submission_id": "aoj_1501_4397084", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX = 1000000;\nconst int MOD = 100000007;\n\n\nlong long fac[MAX], finv[MAX], inv[MAX];\n\n// テーブルを作る前処理\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % MOD;\n //そのまま計算すると負の値になるのでMODを足す\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n}\n// 二項係数計算\nlong long COM(int n, int k){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\n\nint main() {\n // 前処理\n COMinit();\n\n long long int r, c, a1, a2, b1, b2, absb1, absb2, x, y;\n\n cin >> r >> c >> a1 >> a2 >> b1 >> b2;\n\n absb1 = r-abs(a1-b1);\n absb2 = c-abs(a2-b2);\n x = min(abs(a1-b1), absb1);\n y = min(abs(a2-b2), absb2);\n long long int ans = COM(x+y, x);\n if(abs(a1-b1) == absb1) ans*=2;\n if(abs(a2-b2) == absb2) ans*=2;\n cout << ans << endl;\n}", "accuracy": 0.987012987012987, "time_ms": 10, "memory_kb": 26556, "score_of_the_acc": -0.2665, "final_rank": 15 }, { "submission_id": "aoj_1501_4397047", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX = 1000000;\nconst int MOD = 100000007;\n\n\nlong long fac[MAX], finv[MAX], inv[MAX];\n\n// テーブルを作る前処理\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % MOD;\n //そのまま計算すると負の値になるのでMODを足す\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n}\n// 二項係数計算\nlong long COM(int n, int k){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\n\nint main() {\n // 前処理\n COMinit();\n\n long long int r, c, a1, a2, b1, b2, absb1, absb2, x, y;\n\n cin >> r >> c >> a1 >> a2 >> b1 >> b2;\n\n absb1 = abs(b1-r+1)+1;\n absb2 = abs(b2-c+1)+1;\n\n x = min(abs(a1-b1), abs(a1-absb1));\n y = min(abs(a2-b2), abs(a2-absb2));\n long long int ans = COM(x+y, x);\n if(abs(a1-b1) == abs(a1-absb1)) ans*=2;\n if(abs(a2-b2) == abs(a2-absb2)) ans*=2;\n cout << ans << endl;\n}", "accuracy": 0.12987012987012986, "time_ms": 10, "memory_kb": 26532, "score_of_the_acc": -0.2661, "final_rank": 18 }, { "submission_id": "aoj_1501_4283520", "code_snippet": "#include <bits/stdc++.h>\n\n//#define INF 100000000\n//#define MOD (int) (1e9+7)\n#define rep(i, a) for (int i = 0; i < (a); i++)\nusing namespace std;\n\nconst int MAX = 1000000;\nconst int MOD = 100000007;\n\nlong long fac[MAX], finv[MAX], inv[MAX];\n\n// テーブルを作る前処理\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % MOD;\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n}\n\n// 二項係数計算\nlong long COM(int n, int k){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\n\nint main() {\n long long int r, c, a1, a2, b1, b2;\n cin >> r >> c >> a1 >> a2 >> b1 >> b2;\n\n long long int m = min(abs(b1 - a1), r - abs(b1 - a1));\n long long int n = min(abs(b2 - a2), c - abs(b2 - a2));\n\n // 前処理\n COMinit();\n\n long long int ans = COM(m+n, m);\n\n if (r == m*2) {\n ans = 2 * ans % MOD;\n }\n if (c == n*2) {\n ans = 2 * ans % MOD;\n }\n\n cout << ans << endl;\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 26536, "score_of_the_acc": -0.2661, "final_rank": 6 }, { "submission_id": "aoj_1501_4283515", "code_snippet": "#include<bits/stdc++.h>\n\n//#define INF 100000000\n//#define MOD (int) (1e9+7)\n#define rep(i, a) for (int i = 0; i < (a); i++)\nusing namespace std;\n\nconst int MAX = 1000000;\nconst int MOD = 1000000007;\n\nlong long fac[MAX], finv[MAX], inv[MAX];\n\n// テーブルを作る前処理\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % MOD;\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * inv[i] % MOD;\n }\n}\n\n// 二項係数計算\nlong long COM(int n, int k){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\n\nint main() {\n long long int r, c, a1, a2, b1, b2;\n cin >> r >> c >> a1 >> a2 >> b1 >> b2;\n\n long long int m = min(abs(b1 - a1), r - abs(b1 - a1));\n long long int n = min(abs(b2 - a2), c - abs(b2 - a2));\n\n // 前処理\n COMinit();\n\n long long int ans = COM(m+n, m);\n\n if (r == m*2) {\n ans = 2 * ans % MOD;\n }\n if (c == n*2) {\n ans = 2 * ans % MOD;\n }\n\n cout << ans << endl;\n\n}", "accuracy": 0.11688311688311688, "time_ms": 10, "memory_kb": 26536, "score_of_the_acc": -0.2661, "final_rank": 19 }, { "submission_id": "aoj_1501_3486966", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<cstdio>\n#include<cmath>\n#include<cctype>\n#include<math.h>\n#include<string>\n#include<string.h>\n#include<stack>\n#include<queue>\n#include<vector>\n#include<utility>\n#include<set>\n#include<map>\n#include<stdlib.h>\n#include<iomanip>\n\nusing namespace std;\n\n#define ll long long\n#define ld long double\n#define EPS 0.0000000001\n#define INF 1e9\n#define LINF (ll)INF*INF\n#define MOD 100000007\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define loop(i,a,n) for(int i=a;i<(n);i++)\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\n\n#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int,int> pii;\ntypedef vector<pii> vp;\n\nint gcd(int a, int b){\n if(b==0) return a;\n return gcd(b,a%b);\n}\nint lcm(int a, int b){\n return a/gcd(a,b)*b;\n}\n\n#define MAX 3000\n\nint ans = INF;\nvp v;\nint ncr[MAX][MAX];\n\nvoid f(int sx, int sy, int gx, int gy){\n int dist = abs(sx-gx) + abs(sy-gy);\n if(dist > ans)return;\n if(dist < ans){\n ans = dist;\n v.clear();\n }\n v.push_back(pii(abs(sx-gx),abs(sy-gy)));\n}\n\nsigned main(void) {\n int r,c;\n cin >> r >> c;\n int sx,sy,gx,gy;\n cin >> sx >> sy >> gx >> gy;\n f(r+sx,c+sy,gx,gy);\n f(r+sx,c+sy,gx,c+gy);\n f(r+sx,c+sy,gx,c+c+gy);\n f(r+sx,c+sy,r+gx,gy);\n f(r+sx,c+sy,r+gx,c+gy);\n f(r+sx,c+sy,r+gx,c+c+gy);\n f(r+sx,c+sy,r+r+gx,gy);\n f(r+sx,c+sy,r+r+gx,c+gy);\n f(r+sx,c+sy,r+r+gx,c+c+gy);\n ncr[0][0] = 1;\n rep(i,MAX)rep(j,MAX){\n if(i) ncr[i][j] = (ncr[i][j] + ncr[i - 1][j]) % MOD;\n if(i && j) ncr[i][j] = (ncr[i][j] + ncr[i - 1][j - 1]) % MOD;\n }\n int ans = 0;\n rep(i,v.size()){\n int x = v[i].first;\n int y = v[i].second;\n (ans += ncr[x+y][x]) %= MOD;\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 73412, "score_of_the_acc": -1.2, "final_rank": 13 }, { "submission_id": "aoj_1501_3458318", "code_snippet": "#include <bits/stdc++.h>\n// #define rep(i,n) for(int i=0;i<n;i++)\n// #define all(x) (x).begin(),(x).end()\nusing namespace std;\n// const int INF=1145141919,MOD=1e9+7;\n// const long long LINF=8931145141919364364,LMOD=998244353;\n// inline long long mod(long long n,long long m){return(n%m+m)%m;}\n// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};\n\nstruct Combination{\n const int MAX=2010;\n const int MOD;\n vector<vector<int>> COM;\n vector<vector<int>> PER;\n Combination(const int MOD):\n MOD(MOD)\n {\n COM=vector<vector<int>>(MAX,vector<int>(MAX));\n PER=vector<vector<int>>(MAX,vector<int>(MAX));\n COM[0][0]=PER[0][0]=1;\n for(int i=1;i<MAX;i++){\n COM[i][0]=PER[i][0]=1;\n for(int j=1;j<MAX;j++){\n COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD;\n PER[i][j]=PER[i][j-1]*(i-j+1)%MOD;\n }\n }\n }\n int C(int n,int r){\n assert(0<=n&&n<MAX&&0<=r&&r<MAX);\n return COM[n][r];\n }\n int P(int n,int r){\n assert(0<=n&&n<MAX&&0<=r&&r<MAX);\n return PER[n][r];\n }\n int H(int n,int r){\n assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX);\n return COM[n+r-1][r];\n }\n};\n\nint main(){\n const int MOD=100000007;\n Combination com(MOD);\n int r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2;\n int h1=abs(a1-b1),h2=r-abs(a1-b1);\n int h=min(h1,h2);\n int w1=abs(a2-b2),w2=c-abs(a2-b2);\n int w=min(w1,w2);\n int ans=com.C(h+w,h);\n if(h1==h2) ans=ans*2%MOD;\n if(w1==w2) ans=ans*2%MOD;\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 34400, "score_of_the_acc": -0.5893, "final_rank": 10 }, { "submission_id": "aoj_1501_3458260", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define all(x) (x).begin(),(x).end()\nusing namespace std;\nconst int INF=1145141919,MOD=1e9+7;\nconst long long LINF=8931145141919364364,LMOD=998244353;\ninline long long mod(long long n,long long m){return(n%m+m)%m;}\n// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};\n\n#define int long long\n\nstruct Combination{\n const int MAX=2010;\n const int MOD;\n vector<vector<int>> COM;\n vector<vector<int>> PER;\n Combination(const int MOD):\n MOD(MOD)\n {\n COM=vector<vector<int>>(MAX,vector<int>(MAX));\n PER=vector<vector<int>>(MAX,vector<int>(MAX));\n COM[0][0]=PER[0][0]=1;\n for(int i=1;i<MAX;i++){\n COM[i][0]=PER[i][0]=1;\n for(int j=1;j<MAX;j++){\n COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD;\n PER[i][j]=PER[i][j-1]*(i-j+1)%MOD;\n }\n }\n }\n int C(int n,int r){\n assert(0<=n&&n<MAX&&0<=r&&r<MAX);\n return COM[n][r];\n }\n int P(int n,int r){\n assert(0<=n&&n<MAX&&0<=r&&r<MAX);\n return PER[n][r];\n }\n int H(int n,int r){\n assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX);\n return COM[n+r-1][r];\n }\n};\n\nsigned main(){\n const int mod=100000007;\n Combination com(mod);\n int r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2;\n int h1=abs(a1-b1),h2=r-abs(a1-b1);\n int h=min(h1,h2);\n int w1=abs(a2-b2),w2=c-abs(a2-b2);\n int w=min(w1,w2);\n int ans=com.C(h+w,h);\n if(h1==h2) ans=ans*2%mod;\n if(w1==w2) ans=ans*2%mod;\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 66080, "score_of_the_acc": -1.2852, "final_rank": 14 }, { "submission_id": "aoj_1501_3458250", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define all(x) (x).begin(),(x).end()\nusing namespace std;\nconst int INF=1145141919,MOD=1e9+7;\nconst long long LINF=8931145141919364364,LMOD=998244353;\ninline long long mod(long long n,long long m){return(n%m+m)%m;}\n// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};\n\nstruct Combination{\n const int MAX=2010;\n const int MOD;\n vector<vector<int>> COM;\n vector<vector<int>> PER;\n Combination(const int MOD):\n MOD(MOD)\n {\n COM=vector<vector<int>>(MAX,vector<int>(MAX));\n PER=vector<vector<int>>(MAX,vector<int>(MAX));\n COM[0][0]=PER[0][0]=1;\n for(int i=1;i<MAX;i++){\n COM[i][0]=PER[i][0]=1;\n for(int j=1;j<MAX;j++){\n COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD;\n PER[i][j]=PER[i][j-1]*(i-j+1)%MOD;\n }\n }\n }\n int C(int n,int r){\n assert(0<=n&&n<MAX&&0<=r&&r<MAX);\n return COM[n][r];\n }\n int P(int n,int r){\n assert(0<=n&&n<MAX&&0<=r&&r<MAX);\n return PER[n][r];\n }\n int H(int n,int r){\n assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX);\n return COM[n+r-1][r];\n }\n};\n\nsigned main(){\n Combination com(100000007);\n int r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2;\n int h1=abs(a1-b1),h2=r-abs(a1-b1);\n int h=min(h1,h2);\n int w1=abs(a2-b2),w2=c-abs(a2-b2);\n int w=min(w1,w2);\n int ans=com.C(h+w,h);\n if(h1==h2) ans=ans*2%MOD;\n if(w1==w2) ans=ans*2%MOD;\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 0.987012987012987, "time_ms": 30, "memory_kb": 34400, "score_of_the_acc": -0.5226, "final_rank": 16 }, { "submission_id": "aoj_1501_3458148", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define all(x) (x).begin(),(x).end()\nusing namespace std;\nconst int INF=1145141919,MOD=1e9+7;\nconst long long LINF=8931145141919364364,LMOD=998244353;\ninline long long mod(long long n,long long m){return(n%m+m)%m;}\n// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};\n\nstruct Combination{\n const int MAX=2010;\n const int MOD;\n vector<vector<int>> COM;\n vector<vector<int>> PER;\n Combination(const int MOD):\n MOD(MOD)\n {\n COM=vector<vector<int>>(MAX,vector<int>(MAX));\n PER=vector<vector<int>>(MAX,vector<int>(MAX));\n COM[0][0]=PER[0][0]=1;\n for(int i=1;i<MAX;i++){\n COM[i][0]=PER[i][0]=1;\n for(int j=1;j<MAX;j++){\n COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD;\n PER[i][j]=PER[i][j-1]*(i-j+1)%MOD;\n }\n }\n }\n int C(int n,int r){\n assert(0<=n&&n<MAX&&0<=r&&r<MAX);\n return COM[n][r];\n }\n int P(int n,int r){\n assert(0<=n&&n<MAX&&0<=r&&r<MAX);\n return PER[n][r];\n }\n int H(int n,int r){\n assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX);\n return COM[n+r-1][r];\n }\n};\n\nint main(){\n Combination com(MOD);\n long long r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2;\n long long h1=abs(a1-b1),h2=r-abs(a1-b1);\n long long h=min(h1,h2);\n long long w1=abs(a2-b2),w2=c-abs(a2-b2);\n long long w=min(w1,w2);\n long long ans=com.C(h+w,h);\n if(h1==h2) ans=ans*2%MOD;\n if(w1==w2) ans=ans*2%MOD;\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 0.11688311688311688, "time_ms": 40, "memory_kb": 34400, "score_of_the_acc": -0.5893, "final_rank": 20 }, { "submission_id": "aoj_1501_2762496", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define all(v) (v).begin(), (v).end()\n#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)\n#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n\ntemplate<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}\ntemplate<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}\n\nusing Pi = pair<int, int>;\nusing Tapris = tuple<int, int, int>;\nusing vint = vector<int>;\n\nconst int inf = 1LL << 55;\nconst int mod = 1e9 + 7;\n\nstruct Combinatorics {\n using int64 = long long;\n int64 mod;\n int64 fact[202020];\n int64 invfact[202020];\n Combinatorics(int64 mod):mod(mod) {\n fact[0] = invfact[0] = 1;\n for(int i = 1; i < 202020; ++i) {\n fact[i] = fact[i-1]*i%mod;\n invfact[i] = minv(fact[i]);\n }\n }\n int64 mpow(int64 x, int64 n) const {\n int64 res = 1;\n while(n > 0) {\n if(n&1) res = res*x%mod;\n x = x*x%mod;\n n >>= 1;\n }\n return res;\n }\n int64 minv(int64 x) const {\n return mpow(x, mod-2);\n }\n int64 mfact(int64 x) const {\n return fact[x];\n }\n int64 C(int64 n, int64 r) const {\n if(r < 0 || n < r) return 0;\n return fact[n]*invfact[r]%mod*invfact[n-r]%mod;\n }\n int64 P(int64 n, int64 r) const {\n if(r < 0 || n < r) return 0;\n return fact[n]*invfact[n-r]%mod;\n }\n}C(mod);\n\n\nsigned main() {\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n int r, c, ay, ax, by, bx;\n cin >> r >> c >> ay >> ax >> by >> bx;\n\n int By[3][3], Bx[3][3];\n reps(i, -1, 2) reps(j, -1, 2) {\n By[i+1][j+1] = by+i*r;\n Bx[i+1][j+1] = bx+j*c;\n }\n\n int mod = 1e8+7;\n Combinatorics comb(mod);\n\n int mn = inf;\n rep(i, 3) rep(j, 3) {\n chmin(mn, llabs(ay-By[i][j])+llabs(ax-Bx[i][j]));\n }\n\n int ans = 0;\n rep(i, 3) rep(j, 3) {\n int d = llabs(ay-By[i][j])+llabs(ax-Bx[i][j]);\n if(mn == d) {\n ans += comb.C(mn, llabs(ay-By[i][j]));\n ans %= mod;\n }\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 9536, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_1501_2762494", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define all(v) (v).begin(), (v).end()\n#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)\n#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n\ntemplate<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}\ntemplate<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}\n\nusing Pi = pair<int, int>;\nusing Tapris = tuple<int, int, int>;\nusing vint = vector<int>;\n\nconst int inf = 1LL << 55;\nconst int mod = 1e9 + 7;\n\nstruct Combinatorics {\n using int64 = long long;\n int64 mod;\n int64 fact[202020];\n int64 invfact[202020];\n Combinatorics(int64 mod):mod(mod) {\n fact[0] = invfact[0] = 1;\n for(int i = 1; i < 202020; ++i) {\n fact[i] = fact[i-1]*i%mod;\n invfact[i] = minv(fact[i]);\n }\n }\n int64 mpow(int64 x, int64 n) const {\n int64 res = 1;\n while(n > 0) {\n if(n&1) res = res*x%mod;\n x = x*x%mod;\n n >>= 1;\n }\n return res;\n }\n int64 minv(int64 x) const {\n return mpow(x, mod-2);\n }\n int64 mfact(int64 x) const {\n return fact[x];\n }\n int64 C(int64 n, int64 r) const {\n if(r < 0 || n < r) return 0;\n return fact[n]*invfact[r]%mod*invfact[n-r]%mod;\n }\n int64 P(int64 n, int64 r) const {\n if(r < 0 || n < r) return 0;\n return fact[n]*invfact[n-r]%mod;\n }\n}C(mod);\n\n\nsigned main() {\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n int r, c, ay, ax, by, bx;\n cin >> r >> c >> ay >> ax >> by >> bx;\n\n int By[3][3], Bx[3][3];\n reps(i, -1, 2) reps(j, -1, 2) {\n By[i+1][j+1] = by+i*r;\n Bx[i+1][j+1] = bx+j*c;\n }\n\n Combinatorics comb(1e8+7);\n\n int mn = inf;\n rep(i, 3) rep(j, 3) {\n chmin(mn, llabs(ay-By[i][j])+llabs(ax-Bx[i][j]));\n }\n\n int ans = 0;\n rep(i, 3) rep(j, 3) {\n int d = llabs(ay-By[i][j])+llabs(ax-Bx[i][j]);\n if(mn == d) {\n ans += comb.C(mn, llabs(ay-By[i][j]));\n ans %= mod;\n }\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 0.987012987012987, "time_ms": 160, "memory_kb": 9536, "score_of_the_acc": -1, "final_rank": 17 }, { "submission_id": "aoj_1501_2757174", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\nusing namespace std;\n\ntypedef long long ll;\nconst ll mod = 1e8+7;\n\nll mod_pow(ll x,ll n){\n ll res=1;\n while(n){\n if(n%2) res=res*x%mod;\n x=x*x%mod;\n n/=2;\n }\n return res;\n}\n\nll divi(ll a,ll b){\n return a * mod_pow(b,mod-2) % mod;\n}\n\nll factorial(int i){\n static vector<ll> k(1e6);\n if(!k[0]){k[0]=1;for(int i=1;i<(int)k.size();i++)k[i]=i*k[i-1]%mod;}\n return k[i];\n}\n\nll nCr(ll n,ll r){\n ll a = mod_pow( factorial(r) * factorial(n-r) % mod ,mod-2);\n return factorial(n) * a % mod;\n}\n\ntypedef pair<int,int> P;\n\nint h, w, sy, sx, ty, tx;\n\nP cal(int y, int x){\n \n int Y = abs(y - sy);\n int X = abs(x - sx);\n \n return P( Y + X, nCr( Y + X, X ));\n}\n\nvoid solve(){\n \n map<int,int> ans;\n\n for(int i=-1;i<=1;i++){\n \n for(int j=-1;j<=1;j++){\n \n P tmp = cal( ty + i * h, tx + j * w );\n\n ans[tmp.first] += tmp.second;\n \n ans[tmp.first] %= mod;\n }\n \n }\n \n auto p = ans.begin();\n \n cout<<(*p).second<<endl;\n}\n\nsigned main(){\n \n cin>>h>>w;\n \n cin>>sy>>sx>>ty>>tx;\n \n solve(); \n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 10760, "score_of_the_acc": -0.0192, "final_rank": 1 }, { "submission_id": "aoj_1501_2257624", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\n#define MOD 100000007\nusing namespace std;\ntypedef pair<int,int>P;\n\nint d[1000][1000],ans[1000][1000];\nint dx[]{1,-1,0,0},dy[]{0,0,1,-1};\nint main(){\n\tint r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2;\n\tqueue<P>que;\n\tmemset(d,-1,sizeof(d));\n\tans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1));\n\twhile(!que.empty()){\n\t\tP p=que.front();que.pop();\n\t\trep(i,4){\n\t\t\tint nx=p.first+dx[i]+r,ny=p.second+dy[i]+c;\n\t\t\twhile(nx>=r)nx-=r;while(ny>=c)ny-=c;\n\t\t\tif(d[nx][ny]==-1){\n\t\t\t\td[nx][ny]=d[p.first][p.second]+1;\n\t\t\t\tque.push(P(nx,ny));\n\t\t\t}\n\t\t\tif(d[nx][ny]==d[p.first][p.second]+1)\n\t\t\t\t(ans[nx][ny]+=ans[p.first][p.second])%=MOD;\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans[x2][y2]);\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 11016, "score_of_the_acc": -0.0898, "final_rank": 2 }, { "submission_id": "aoj_1501_2257621", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\n#define MOD 100000007\nusing namespace std;\ntypedef pair<int,int>P;\n\nint d[1000][1000],ans[1000][1000];\nint dx[]{1,-1,0,0},dy[]{0,0,1,-1};\nint main(){\n\tint r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2;\n\tqueue<P>que;\n\tmemset(d,-1,sizeof(d));\n\tans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1));\n\twhile(!que.empty()){\n\t\tP p=que.front();que.pop();\n\t\trep(i,4){\n\t\t\tint nx=(p.first+dx[i]+r)%r,ny=(p.second+dy[i]+c)%c;\n\t\t\tif(d[nx][ny]==-1){\n\t\t\t\td[nx][ny]=d[p.first][p.second]+1;\n\t\t\t\tque.push(P(nx,ny));\n\t\t\t}\n\t\t\tif(d[nx][ny]==d[p.first][p.second]+1)\n\t\t\t\t(ans[nx][ny]+=ans[p.first][p.second])%=MOD;\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans[x2][y2]);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 11112, "score_of_the_acc": -0.158, "final_rank": 5 }, { "submission_id": "aoj_1501_2257616", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\n#define MOD 100000007\nusing namespace std;\ntypedef pair<int,int>P;\n\nint d[1000][1000],ans[1000][1000];\nint dx[]{1,-1,0,0},dy[]{0,0,1,-1};\nint main(){\n\tint r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2;\n\tqueue<P>que;\n\tmemset(d,-1,sizeof(d));\n\tans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1));\n\twhile(!que.empty()){\n\t\tP p=que.front();que.pop();\n\t\trep(i,4){\n\t\t\tint nx=p.first+dx[i],ny=p.second+dy[i];\n\t\t\tint a=0;\n\t\t\tif(nx<0||nx>=r){\n\t\t\t\tnx=(nx+r)%r;a++;\n\t\t\t}\n\t\t\tif(ny<0||ny>=c){\n\t\t\t\tny=(ny+c)%c;a++;\n\t\t\t}\n\t\t\tif(a==2)continue;\n\t\t\tif(d[nx][ny]==-1){\n\t\t\t\td[nx][ny]=d[p.first][p.second]+1;\n\t\t\t\tque.push(P(nx,ny));\n\t\t\t}\n\t\t\tif(d[nx][ny]==d[p.first][p.second]+1)\n\t\t\t\t(ans[nx][ny]+=ans[p.first][p.second])%=MOD;\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans[x2][y2]);\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 11088, "score_of_the_acc": -0.091, "final_rank": 3 }, { "submission_id": "aoj_1501_2252487", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<string>\n#include<vector>\n#include<algorithm>\n#include<queue>\n#include<set>\n#include<unordered_map>\n#include<string.h>\n#define int long long\n#define mod 100000007\nusing namespace std;\n\nint memo[2001][2001];\nint saiki(int a, int b) {\n\tif (memo[a][b] != -1)return memo[a][b];\n\tint c = 0;\n\tif (a == 0 || b == 0)c = 1;\n\telse {\n\t\tc += saiki(a - 1, b);\n\t\tc += saiki(a, b - 1);\n\t}\n\treturn memo[a][b] = c%mod;\n}\nsigned main() {\n\tmemset(memo, -1, sizeof(memo));\n\tint a, b, c, d, e, f;\n\tcin >> a >> b >> c >> d >> e >> f;\n\tint g = abs(e - c), h = abs(f - d);\n\tint i = a - max(e,c) + min(e,c), j = b - max(f,d) + min(f,d);\n\tint k = min(g, i), l = min(h, j);\n\tif (c == e) {\n\t\tif (h == j)puts(\"2\");\n\t\telse puts(\"1\");\n\t\treturn 0;\n\t}\n\tif (d == f) {\n\t\tif (g == i)puts(\"2\");\n\t\telse puts(\"1\");\n\t\treturn 0;\n\t}\n\tint ans = saiki(k, l);\n\tif (g == i) {\n\t\tif (h == j) {\n\t\t\tans *= 4;\n\t\t}\n\t\telse {\n\t\t\tans *= 2;\n\t\t}\n\t}\n\telse {\n\t\tif (h == j) {\n\t\t\tans *= 2;\n\t\t}\n\t}\n\tcout << ans%mod << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 34404, "score_of_the_acc": -0.3893, "final_rank": 9 } ]
aoj_1504_cpp
Problem E : Connect r × c のグリッドが与えられる。 グリッドのいくつかのマスには1から8までの数字が書かれている。 数字が書かれているマスと他の数字が書かれているマスを線で結ぶ必要がある。 数字が書かれているマスについて、そのマスに書かれている本数だけ、他のマスとの間に線を結ぶ必要がある。 ただし、線を結べるのは上下左右方向だけで、一つの方向について、最大2本までである。 他の数字を跨いで線で結ぶことはできない。 また、次のように交差してしまう結び方をすることはできない。 グリッドが入力として与えられるので、数字が書かれているマス全てを正しく結ぶ方法が、何通りあるのかを数えて欲しい。 Input 入力は以下のフォーマットで与えられる。 r c grid 1 . . . grid r-1 grid i は長さcの文字列で、1から8までの数字か"."からなる。 入力は以下の制約を満たす 1 ≤ r,c ≤ 10 Output 答えの値を100,000,007で割った余りを1行に出力せよ Sample Input 1 3 3 .1. 1.1 .1. Sample Output 1 0 Sample Input 2 1 3 1.1 Sample Output 2 1 Sample Input 3 3 3 4.2 ... 2.. Sample Output 3 1 Sample Input 4 7 7 2.3.1.1 ....... 2...... ....... ..3.3.. ....... 2.2.4.3 Sample Output 4 10
[ { "submission_id": "aoj_1504_10344187", "code_snippet": "// AOJ #1504 Connect\n// 2025.4.2\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 100000007;\n\nint dp[2][3][60005];\nint d[11];\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int H, W;\n cin >> H >> W;\n char grid[11][11];\n for(int i=0; i<H; i++) cin >> grid[i];\n\n d[0] = 1;\n for(int i = 0; i < W; i++) d[i+1] = d[i] * 3;\n int maxProfile = d[W];\n\n dp[0][0][0] = 1;\n int cur = 0, nxt = 1;\n\n for(int i = 0; i < H; i++){\n for(int j = 0; j < W; j++){\n memset(dp[nxt], 0, sizeof(dp[nxt]));\n for(int carry = 0; carry < 3; carry++){\n for(int prof = 0; prof < maxProfile; prof++){\n if(dp[cur][carry][prof] == 0) continue;\n int ways = dp[cur][carry][prof];\n int s = (prof / d[j]) % 3;\n if(grid[i][j] == '.'){\n if(carry && s) continue;\n if(carry && j == W - 1) continue;\n dp[nxt][carry][prof] = (dp[nxt][carry][prof] + ways) % MOD;\n } else {\n int num = grid[i][j] - '0';\n for(int u = 0; u < 3; u++){\n int t = num - carry - s - u;\n if(t < 0 || t > 2) continue;\n if(u && j == W - 1) continue;\n int newProf = prof + d[j] * (t - s);\n dp[nxt][u][newProf] = (dp[nxt][u][newProf] + ways) % MOD;\n }\n }\n }\n }\n cur ^= 1;\n nxt ^= 1;\n }\n }\n cout << dp[cur][0][0] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4860, "score_of_the_acc": -0.0086, "final_rank": 1 }, { "submission_id": "aoj_1504_6446328", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cstring>\n#include <set>\n#include <vector>\n#include <stack>\n#include <map>\n#include <queue>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int maxn=100000007;\nint dp[10][10][3][60000];\nint pow3[13];\nstring s[10];\nint h,w;\nint dfs(int x,int y,int r,int zhuan)\n{\n if(x==h){\n return zhuan==0;\n }\n if(y==w){\n if(r) return 0;\n else return dfs(x+1,0,0,zhuan);\n }\n if(dp[x][y][r][zhuan]!=-1) return dp[x][y][r][zhuan];\n int tmp=zhuan/pow3[y]%3;\n if(s[x][y]=='.'){\n if(r&&tmp) return dp[x][y][r][zhuan]=0;\n else return dp[x][y][r][zhuan]=dfs(x,y+1,r,zhuan);\n }\n else{\n int need=s[x][y]-'0';\n need-=r+tmp;\n zhuan-=pow3[y]*tmp;\n int ans=0;\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n if(i+j!=need) continue;\n ans+=dfs(x,y+1,i,zhuan+pow3[y]*j);\n if(ans>maxn) ans-=maxn;\n }\n }\n return dp[x][y][r][zhuan+pow3[y]*tmp]=ans;\n }\n}\nint main()\n{\n ios::sync_with_stdio(false),cin.tie(0);\n cin>>h>>w;\n for(int i=0;i<h;i++){\n cin>>s[i];\n }\n pow3[0]=1;\n for(int i=1;i<13;i++) pow3[i]=pow3[i-1]*3;\n memset(dp,-1,sizeof dp);\n cout<<dfs(0,0,0,0)<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 73544, "score_of_the_acc": -0.3766, "final_rank": 3 }, { "submission_id": "aoj_1504_6445378", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cstring>\n#include <set>\n#include <vector>\n#include <stack>\n#include <map>\n#include <queue>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int maxn=100000007;\nint dp[10][10][3][60000];\nint pow3[13];\nstring s[10];\nint h,w;\nint dfs(int x,int y,int r,int zhuan)\n{\n if(x==h){\n return zhuan==0;\n }\n if(y==w){\n if(r) return 0;\n else return dfs(x+1,0,0,zhuan);\n }\n if(dp[x][y][r][zhuan]!=-1) return dp[x][y][r][zhuan];\n if(s[x][y]=='.'){\n if(r&&zhuan/pow3[y]%3) return 0;\n else return dfs(x,y+1,r,zhuan);\n }\n else{\n int need=s[x][y]-'0';\n int tmp=zhuan/pow3[y]%3;\n need-=r+tmp;\n zhuan-=pow3[y]*tmp;\n int ans=0;\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n if(i+j!=need) continue;\n ans+=dfs(x,y+1,i,zhuan+pow3[y]*j);\n if(ans>maxn) ans-=maxn;\n }\n }\n return dp[x][y][r][zhuan]=ans;\n }\n}\nint main()\n{\n ios::sync_with_stdio(false),cin.tie(0);\n cin>>h>>w;\n for(int i=0;i<h;i++){\n cin>>s[i];\n }\n pow3[0]=1;\n for(int i=1;i<13;i++) pow3[i]=pow3[i-1]*3;\n memset(dp,-1,sizeof dp);\n cout<<dfs(0,0,0,0)<<endl;\n return 0;\n}", "accuracy": 0.3333333333333333, "time_ms": 10, "memory_kb": 73524, "score_of_the_acc": -0.3532, "final_rank": 20 }, { "submission_id": "aoj_1504_4852615", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n//#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define MOD 100000007\n#define SIZE 13\n\nenum Type{\n\tRight,\n\tUnder,\n};\n\nstruct LOC{\n\tLOC(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nint H,W;\nchar table[SIZE][SIZE];\nll POW[SIZE];\nvector<LOC> ADJ[SIZE][SIZE][2];\nll dp[SIZE][SIZE][177147];\n\n\n\nll makeCode(ll rest[SIZE],ll to_right){\n\n\tll ret = to_right*POW[W];\n\tfor(int col = 0; col < W; col++){\n\n\t\tret += POW[col]*rest[col];\n\t}\n\n\treturn ret;\n}\n\n\n\n//dp[行][列][状態] := 場合の数\n\nll recursive(int row,int col,ll rest[SIZE],ll to_right){\n\n\tll tmp_code = makeCode(rest,to_right);\n\tif(dp[row][col][tmp_code] != -1){\n\n\t\treturn dp[row][col][tmp_code];\n\t}\n\n\tll next_rest[SIZE];\n\n\tif(col == W){\n\n\t\tif(row == H-1){\n\n\t\t\tbool FLG = true;\n\t\t\tfor(int i = 0; i < W; i++){\n\t\t\t\tif(rest[i] > 0){\n\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(FLG){\n\n\t\t\t\treturn 1;\n\n\t\t\t}else{\n\n\t\t\t\treturn 0;\n\t\t\t}\n\n\t\t}else{ //次の行の準備\n\n\t\t\tfor(int i = 0; i < W; i++){\n\t\t\t\tif(table[row+1][i] == '.'){\n\n\t\t\t\t\tnext_rest[i] = rest[i];\n\n\t\t\t\t}else{\n\n\t\t\t\t\tnext_rest[i] = (table[row+1][i]-'0')-rest[i];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn dp[row][col][tmp_code] = recursive(row+1,0,next_rest,0);\n\t\t}\n\t}\n\n\tif(table[row][col] == '.'){\n\n\t\treturn dp[row][col][tmp_code] = recursive(row,col+1,rest,to_right);\n\t}\n\n\tif(rest[col] >= 5){\n\n\t\treturn dp[row][col][tmp_code] = 0;\n\t}\n\n\tll ret = 0;\n\n\tint next_col;\n\tbool right_FLG = true;\n\n\tif(ADJ[row][col][Right].size() == 0){ //右に相手なし\n\n\t\tright_FLG = false;\n\n\t}else{ //右に相手あり\n\n\t\tnext_col = ADJ[row][col][Right][0].col;\n\t\tfor(int k = col+1; k <= next_col-1; k++){ //この区間は全て\".\"であるはず\n\t\t\tif(rest[k] > 0){ //★線分の交差が起きるので不可★\n\n\t\t\t\tright_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(!right_FLG && rest[col] > 0){ //右に線分を伸ばせない,かつ残量あり\n\n\t\tif(rest[col] >= 3){\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\n\t\tif(ADJ[row][col][Under].size() == 0){ //下に伸ばす相手なし\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\n\t\tint next_row = ADJ[row][col][Under][0].row;\n\t\tif(rest[col] > table[next_row][col]-'0'){ //下に伸ばす相手の最大容量を超過\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\t}\n\n\n\tll maximum = min(rest[col],2LL);\n\tif(!right_FLG){\n\n\t\tmaximum = 0;\n\t}\n\n\tfor(ll to_r = 0; to_r <= maximum; to_r++){ //右に伸ばす数の走査\n\n\n\t\tfor(int i = 0; i < W; i++){\n\n\t\t\tnext_rest[i] = rest[i];\n\t\t}\n\n\t\tif(to_r == 0){\n\n\t\t\t//Do nothing\n\n\t\t}else{ //交差しない、かつ相手あり\n\n\t\t\tif(to_r > rest[next_col])break;\n\n\t\t\tnext_rest[col] = rest[col]-to_r;\n\t\t\tnext_rest[next_col] = rest[next_col]-to_r;\n\t\t}\n\n\n\t\tif(next_rest[col] > 0){\n\n\t\t\tif(next_rest[col] >= 3)continue;\n\n\t\t\tif(ADJ[row][col][Under].size() == 0){ //下に伸ばす相手なし\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tint next_row = ADJ[row][col][Under][0].row;\n\t\t\tif(next_rest[col] > table[next_row][col]-'0'){ //下に伸ばす相手の最大容量を超過\n\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\n\t\tret += recursive(row,col+1,next_rest,to_r);\n\t\tret %= MOD;\n\t}\n\n\treturn dp[row][col][tmp_code] = ret;\n}\n\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*3;\n\t}\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",table[row]);\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(table[row][col] == '.')continue;\n\n\t\t\t//右\n\t\t\tfor(int k = col+1; k < W; k++){\n\t\t\t\tif(table[row][k] != '.'){\n\t\t\t\t\tADJ[row][col][Right].push_back(LOC(row,k));\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\t//下\n\t\t\tfor(int k = row+1; k < H; k++){\n\t\t\t\tif(table[k][col] != '.'){\n\t\t\t\t\tADJ[row][col][Under].push_back(LOC(k,col));\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row <= H; row++){\n\t\tfor(int col = 0; col <= W; col++){\n\t\t\tfor(int state = 0; state < POW[W+2]; state++){\n\n\t\t\t\tdp[row][col][state] = -1;\n\t\t\t}\n\t\t}\n\t}\n\n\tll first_rest[SIZE];\n\tfor(int col = 0; col < W; col++){\n\n\t\tif(table[0][col] == '.'){\n\n\t\t\tfirst_rest[col] = 0;\n\n\t\t}else{\n\n\t\t\tfirst_rest[col] = table[0][col]-'0';\n\t\t}\n\t}\n\n\tprintf(\"%lld\\n\",recursive(0,0,first_rest,0));\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 201156, "score_of_the_acc": -1.0533, "final_rank": 9 }, { "submission_id": "aoj_1504_4852609", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n//#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define MOD 100000007\n#define SIZE 13\n\nenum Type{\n\tRight,\n\tUnder,\n};\n\nstruct LOC{\n\tLOC(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nint H,W;\nchar table[SIZE][SIZE];\nll POW[SIZE];\nvector<LOC> ADJ[SIZE][SIZE][2];\nll dp[SIZE][SIZE][177147];\n\n\n\nll makeCode(ll rest[SIZE],ll to_right){\n\n\tll ret = to_right*POW[W];\n\tfor(int col = 0; col < W; col++){\n\n\t\tret += POW[col]*rest[col];\n\t}\n\n\treturn ret;\n}\n\n\n/*void print_rest(ll rest[SIZE]){\n\n\tfor(int col = 0; col < W; col++){\n\n\t\tprintf(\"rest[%d]:%lld\\n\",col,rest[col]);\n\t}\n}*/\n\n\n//dp[行][列][状態] := 場合の数\n\nll recursive(int row,int col,ll rest[SIZE],ll to_right){\n\n\t/*printf(\"row:%d col:%d\\n\",row,col);\n\tprint_rest(rest);*/\n\n\tll tmp_code = makeCode(rest,to_right);\n\tif(dp[row][col][tmp_code] != -1){\n\n\t\treturn dp[row][col][tmp_code];\n\t}\n\n\tll next_rest[SIZE];\n\n\tif(col == W){\n\n\t\tif(row == H-1){\n\n\t\t\tbool FLG = true;\n\t\t\tfor(int i = 0; i < W; i++){\n\t\t\t\tif(rest[i] > 0){\n\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(FLG){\n\n\t\t\t\treturn 1;\n\n\t\t\t}else{\n\n\t\t\t\treturn 0;\n\t\t\t}\n\n\t\t}else{ //次の行の準備\n\n\t\t\tfor(int i = 0; i < W; i++){\n\t\t\t\tif(table[row+1][i] == '.'){\n\n\t\t\t\t\tnext_rest[i] = rest[i];\n\n\t\t\t\t}else{\n\n\t\t\t\t\tnext_rest[i] = (table[row+1][i]-'0')-rest[i];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn dp[row][col][tmp_code] = recursive(row+1,0,next_rest,0);\n\t\t}\n\t}\n\n\tif(table[row][col] == '.'){\n\n\t\treturn dp[row][col][tmp_code] = recursive(row,col+1,rest,to_right);\n\t}\n\n\tif(rest[col] >= 5){\n\n\t\treturn dp[row][col][tmp_code] = 0;\n\t}\n\n\tll ret = 0;\n\n\tint next_col;\n\tbool right_FLG = true;\n\n\tif(ADJ[row][col][Right].size() == 0){ //右に相手なし\n\n\t\t//printf(\"右に相手なし\\n\");\n\n\t\tright_FLG = false;\n\n\t}else{ //右に相手あり\n\n\t\tnext_col = ADJ[row][col][Right][0].col;\n\t\tfor(int k = col+1; k <= next_col-1; k++){ //この区間は全て\".\"であるはず\n\t\t\tif(rest[k] > 0){ //★線分の交差が起きるので不可★\n\n\t\t\t\t//printf(\"交差\\n\");\n\t\t\t\tright_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(!right_FLG && rest[col] > 0){ //右に線分を伸ばせない,かつ残量あり\n\n\t\tif(rest[col] >= 3){\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\n\t\tif(ADJ[row][col][Under].size() == 0){ //下に伸ばす相手なし\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\n\t\tint next_row = ADJ[row][col][Under][0].row;\n\t\tif(rest[col] > table[next_row][col]-'0'){ //下に伸ばす相手の最大容量を超過\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\t}\n\n\n\tll maximum = min(rest[col],2LL);\n\tif(!right_FLG){\n\n\t\tmaximum = 0;\n\t}\n\n\t//printf(\"maximum:%lld\\n\",maximum);\n\n\tfor(ll to_r = 0; to_r <= maximum; to_r++){ //右に伸ばす数の走査\n\n\n\t\tfor(int i = 0; i < W; i++){\n\n\t\t\tnext_rest[i] = rest[i];\n\t\t}\n\n\t\tif(to_r == 0){\n\n\t\t\t//Do nothing\n\n\t\t}else{ //交差しない、かつ相手あり\n\n\t\t\tif(to_r > rest[next_col])break;\n\n\t\t\tnext_rest[col] = rest[col]-to_r;\n\t\t\tnext_rest[next_col] = rest[next_col]-to_r;\n\t\t}\n\n\n\t\tif(next_rest[col] > 0){\n\n\t\t\tif(next_rest[col] >= 3)continue;\n\n\t\t\tif(ADJ[row][col][Under].size() == 0){ //下に伸ばす相手なし\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tint next_row = ADJ[row][col][Under][0].row;\n\t\t\tif(next_rest[col] > table[next_row][col]-'0'){ //下に伸ばす相手の最大容量を超過\n\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\n\t\tret += recursive(row,col+1,next_rest,to_r);\n\t\tret %= MOD;\n\t}\n\n\treturn dp[row][col][tmp_code] = ret;\n}\n\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*3;\n\t}\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",table[row]);\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(table[row][col] == '.')continue;\n\n\t\t\t//右\n\t\t\tfor(int k = col+1; k < W; k++){\n\t\t\t\tif(table[row][k] != '.'){\n\t\t\t\t\tADJ[row][col][Right].push_back(LOC(row,k));\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\t//下\n\t\t\tfor(int k = row+1; k < H; k++){\n\t\t\t\tif(table[k][col] != '.'){\n\t\t\t\t\tADJ[row][col][Under].push_back(LOC(k,col));\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row <= H; row++){\n\t\tfor(int col = 0; col <= W; col++){\n\t\t\tfor(int state = 0; state < POW[W+2]; state++){\n\n\t\t\t\tdp[row][col][state] = -1;\n\t\t\t}\n\t\t}\n\t}\n\n\tll first_rest[SIZE];\n\tfor(int col = 0; col < W; col++){\n\n\t\tif(table[0][col] == '.'){\n\n\t\t\tfirst_rest[col] = 0;\n\n\t\t}else{\n\n\t\t\tfirst_rest[col] = table[0][col]-'0';\n\t\t}\n\t}\n\n\tprintf(\"%lld\\n\",recursive(0,0,first_rest,0));\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 201164, "score_of_the_acc": -1.0533, "final_rank": 10 }, { "submission_id": "aoj_1504_4852604", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n//#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define MOD 100000007\n#define SIZE 12\n\nenum Type{\n\tRight,\n\tUnder,\n};\n\nstruct LOC{\n\tLOC(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nint H,W;\nchar table[SIZE][SIZE];\nll POW[SIZE];\nvector<LOC> ADJ[SIZE][SIZE][2];\nll dp[SIZE][SIZE][177147];\n\n\n\nll makeCode(ll rest[SIZE],ll to_right){\n\n\tll ret = to_right+POW[W];\n\tfor(int col = 0; col < W; col++){\n\n\t\tret += POW[col]*rest[col];\n\t}\n\n\treturn ret;\n}\n\n\n/*void print_rest(ll rest[SIZE]){\n\n\tfor(int col = 0; col < W; col++){\n\n\t\tprintf(\"rest[%d]:%lld\\n\",col,rest[col]);\n\t}\n}*/\n\n\n//dp[行][列][状態] := 場合の数\n\nll recursive(int row,int col,ll rest[SIZE],ll to_right){\n\n\t/*printf(\"row:%d col:%d\\n\",row,col);\n\tprint_rest(rest);*/\n\n\tll tmp_code = makeCode(rest,to_right);\n\tif(dp[row][col][tmp_code] != -1){\n\n\t\treturn dp[row][col][tmp_code];\n\t}\n\n\tll next_rest[SIZE];\n\n\tif(col == W){\n\n\t\tif(row == H-1){\n\n\t\t\tbool FLG = true;\n\t\t\tfor(int i = 0; i < W; i++){\n\t\t\t\tif(rest[i] > 0){\n\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(FLG){\n\n\t\t\t\treturn 1;\n\n\t\t\t}else{\n\n\t\t\t\treturn 0;\n\t\t\t}\n\n\t\t}else{ //次の行の準備\n\n\t\t\tfor(int i = 0; i < W; i++){\n\t\t\t\tif(table[row+1][i] == '.'){\n\n\t\t\t\t\tnext_rest[i] = rest[i];\n\n\t\t\t\t}else{\n\n\t\t\t\t\tnext_rest[i] = (table[row+1][i]-'0')-rest[i];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn dp[row][col][tmp_code] = recursive(row+1,0,next_rest,0);\n\t\t}\n\t}\n\n\tif(table[row][col] == '.'){\n\n\t\treturn dp[row][col][tmp_code] = recursive(row,col+1,rest,to_right);\n\t}\n\n\tif(rest[col] >= 5){\n\n\t\treturn dp[row][col][tmp_code] = 0;\n\t}\n\n\tll ret = 0;\n\n\tint next_col;\n\tbool right_FLG = true;\n\n\tif(ADJ[row][col][Right].size() == 0){ //右に相手なし\n\n\t\t//printf(\"右に相手なし\\n\");\n\n\t\tright_FLG = false;\n\n\t}else{ //右に相手あり\n\n\t\tnext_col = ADJ[row][col][Right][0].col;\n\t\tfor(int k = col+1; k <= next_col-1; k++){ //この区間は全て\".\"であるはず\n\t\t\tif(rest[k] > 0){ //★線分の交差が起きるので不可★\n\n\t\t\t\t//printf(\"交差\\n\");\n\t\t\t\tright_FLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif(!right_FLG && rest[col] > 0){ //右に線分を伸ばせない,かつ残量あり\n\n\t\tif(rest[col] >= 3){\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\n\t\tif(ADJ[row][col][Under].size() == 0){ //下に伸ばす相手なし\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\n\t\tint next_row = ADJ[row][col][Under][0].row;\n\t\tif(rest[col] > table[next_row][col]-'0'){ //下に伸ばす相手の最大容量を超過\n\n\t\t\treturn dp[row][col][tmp_code] = 0;\n\t\t}\n\t}\n\n\n\tll maximum = min(rest[col],2LL);\n\tif(!right_FLG){\n\n\t\tmaximum = 0;\n\t}\n\n\t//printf(\"maximum:%lld\\n\",maximum);\n\n\tfor(ll to_r = 0; to_r <= maximum; to_r++){ //右に伸ばす数の走査\n\n\n\t\tfor(int i = 0; i < W; i++){\n\n\t\t\tnext_rest[i] = rest[i];\n\t\t}\n\n\t\tif(to_r == 0){\n\n\t\t\t//Do nothing\n\n\t\t}else{ //交差しない、かつ相手あり\n\n\t\t\tif(to_r > rest[next_col])break;\n\n\t\t\tnext_rest[col] = rest[col]-to_r;\n\t\t\tnext_rest[next_col] = rest[next_col]-to_r;\n\t\t}\n\n\n\t\tif(next_rest[col] > 0){\n\n\t\t\tif(next_rest[col] >= 3)continue;\n\n\t\t\tif(ADJ[row][col][Under].size() == 0){ //下に伸ばす相手なし\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tint next_row = ADJ[row][col][Under][0].row;\n\t\t\tif(next_rest[col] > table[next_row][col]-'0'){ //下に伸ばす相手の最大容量を超過\n\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\n\t\tret += recursive(row,col+1,next_rest,to_r);\n\t\tret %= MOD;\n\t}\n\n\treturn dp[row][col][tmp_code] = ret;\n}\n\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*3;\n\t}\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",table[row]);\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(table[row][col] == '.')continue;\n\n\t\t\t//右\n\t\t\tfor(int k = col+1; k < W; k++){\n\t\t\t\tif(table[row][k] != '.'){\n\t\t\t\t\tADJ[row][col][Right].push_back(LOC(row,k));\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\t//下\n\t\t\tfor(int k = row+1; k < H; k++){\n\t\t\t\tif(table[k][col] != '.'){\n\t\t\t\t\tADJ[row][col][Under].push_back(LOC(k,col));\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row <= H; row++){\n\t\tfor(int col = 0; col <= W; col++){\n\t\t\tfor(int state = 0; state < POW[W+1]; state++){\n\n\t\t\t\tdp[row][col][state] = -1;\n\t\t\t}\n\t\t}\n\t}\n\n\tll first_rest[SIZE];\n\tfor(int col = 0; col < W; col++){\n\n\t\tif(table[0][col] == '.'){\n\n\t\t\tfirst_rest[col] = 0;\n\n\t\t}else{\n\n\t\t\tfirst_rest[col] = table[0][col]-'0';\n\t\t}\n\t}\n\n\tprintf(\"%lld\\n\",recursive(0,0,first_rest,0));\n\n\treturn 0;\n}", "accuracy": 0.4666666666666667, "time_ms": 90, "memory_kb": 170704, "score_of_the_acc": -0.8723, "final_rank": 11 }, { "submission_id": "aoj_1504_2917333", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nInt h,w;\nstring s[100];\nconst Int MOD = 1e8+7;\nInt dp[11][100000];\nInt po[20];\nInt dfs(Int y,Int b);\nInt dfs2(Int y,Int x,vector<Int> &v);\n\nconst Int DBG = 0;\nInt dfs(Int y,Int b){\n if(DBG) cout<<y<<\":\"<<b<<endl;\n if(y==h) return b==0;\n Int &res=dp[y][b];\n if(~res) return res;\n res=0;\n vector<Int> v(w,0);\n for(Int i=0;i<w;i++){\n v[i]=-((b/po[i])%3);\n if(s[y][i]=='.') continue;\n v[i]+=s[y][i]-'0';\n if(v[i]<0) return res;\n }\n if(DBG){\n for(Int i=0;i<w;i++) cout<<v[i]<<\":\";\n cout<<endl;\n }\n res+=dfs2(y,0,v);\n res%=MOD;\n if(DBG) cout<<y<<\":\"<<b<<\":\"<<res<<endl;\n return res;\n}\nInt calc(vector<Int> &v){\n Int b=0;\n for(Int i=w-1;i>=0;i--){\n if(v[i]>0||-v[i]>2) return -1;\n b=b*3+(-v[i]);\n }\n return b;\n}\nInt dfs2(Int y,Int x,vector<Int> &v){\n //cout<<y<<\"*\"<<x<<endl;\n while(x<w&&!isdigit(s[y][x])) x++;\n if(x==w){\n Int b=calc(v);\n if(~b) return dfs(y+1,b);\n return 0;\n }\n Int px=x++,flg=0;\n while(x<w&&!isdigit(s[y][x])){\n flg|=(v[x]<0); \n x++;\n }\n if(x==w){\n v[px]*=-1;\n Int res=dfs2(y,x,v);\n v[px]*=-1;\n return res;\n }\n Int res=0;\n Int pv=v[px];\n for(Int i=0;i<=pv;i++){\n if(i>=3||pv-i>=3) continue;\n if(flg && i!=pv) continue;\n v[x]-=pv-i;\n v[px]=-i;\n res+=dfs2(y,x,v);\n res%=MOD;\n v[x]+=pv-i;\n v[px]=pv;\n }\n return res;\n}\n\n\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n cin>>h>>w;\n for(Int i=0;i<h;i++) cin>>s[i];\n po[0]=1;\n for(Int i=1;i<20;i++) po[i]=po[i-1]*3;\n memset(dp,-1,sizeof(dp));\n cout<<dfs(0,0)<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1420, "memory_kb": 11784, "score_of_the_acc": -0.5103, "final_rank": 6 }, { "submission_id": "aoj_1504_2149415", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nlong long int getid(int x, int y, const vector<int>&v, int l) {\n\tlong long int id = 0;\n\tid += x;\n\tid *= 10;\n\tid += y;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tassert(v[i] >= 0 && v[i] <= 2);\n\t\tid *= 31;\n\t\tid += v[i];\n\t}\n\tid *= 31;\n\tid += l;\n\treturn id;\n}\nmap<long long int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\tassert(x < C);\n\tassert(R != y || !x);\n\tconst long long int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert(false);\n\treturn memo[id];\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 700, "memory_kb": 94932, "score_of_the_acc": -0.6917, "final_rank": 16 }, { "submission_id": "aoj_1504_2149414", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nlong long int getid(int x, int y, const vector<int>&v, int l) {\n\tlong long int id = 0;\n\tid += x;\n\tid *= 15;\n\tid += y;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tassert(v[i] >= 0 && v[i] <= 2);\n\t\tid *= 31;\n\t\tid += v[i];\n\t}\n\tid *= 31;\n\tid += l;\n\treturn id;\n}\nmap<long long int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\tassert(x < C);\n\tassert(R != y || !x);\n\tconst long long int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert(false);\n\treturn memo[id];\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1190, "memory_kb": 118896, "score_of_the_acc": -0.9764, "final_rank": 7 }, { "submission_id": "aoj_1504_2149413", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nint getid(int x, int y, const vector<int>&v, int l) {\n\t int id = 0;\n\tid += x;\n\tid *= 15;\n\tid += y;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tassert(v[i] >= 0 && v[i] <= 2);\n\t\tid *= 31;\n\t\tid += v[i];\n\t}\n\tid *= 31;\n\tid += l;\n\treturn id;\n}\nmap< int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\tassert(x < C);\n\tassert(R != y || !x);\n\tconst int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert(false);\n\treturn memo[id];\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 980, "memory_kb": 72524, "score_of_the_acc": -0.6715, "final_rank": 13 }, { "submission_id": "aoj_1504_2149408", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nint getid(int x, int y, const vector<int>&v, int l) {\n\t int id = 0;\n\tid += x;\n\tid *= 10;\n\tid += y;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tassert(v[i] >= 0 && v[i] <= 2);\n\t\tid *= 3;\n\t\tid += v[i];\n\t}\n\tid *= 3;\n\tid += l;\n\treturn id;\n}\nmap< int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\tassert(x < C);\n\tassert(R != y || !x);\n\tconst int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert(false);\n\treturn memo[id];\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 680, "memory_kb": 71904, "score_of_the_acc": -0.5683, "final_rank": 12 }, { "submission_id": "aoj_1504_2149407", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nlong long int getid(int x, int y, const vector<int>&v, int l) {\n\tlong long int id = 0;\n\tid += x;\n\tid *= 10;\n\tid += y;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tassert(v[i] >= 0 && v[i] <= 2);\n\t\tid *= 3;\n\t\tid += v[i];\n\t}\n\tid *= 3;\n\tid += l;\n\treturn id;\n}\nmap<long long int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\tassert(x < C);\n\tassert(R != y || !x);\n\tconst long long int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert(false);\n\treturn memo[id];\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 720, "memory_kb": 94820, "score_of_the_acc": -0.6978, "final_rank": 19 }, { "submission_id": "aoj_1504_2149402", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nlong long int getid(int x, int y, const vector<int>&v, int l) {\n\tlong long int id = 0;\n\tid += x;\n\tid *= 10;\n\tid += y;\n\tid *= 10;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tassert(v[i] >= 0 && v[i] <= 2);\n\t\tid += v[i];\n\t\tid *= 10;\n\t}\n\tid += l;\n\treturn id;\n}\nmap<long long int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\tassert(x < C);\n\tassert(R != y || !x);\n\tconst long long int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert(false);\n\treturn memo[id];\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 710, "memory_kb": 94740, "score_of_the_acc": -0.694, "final_rank": 17 }, { "submission_id": "aoj_1504_2149401", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nlong long int getid(int x, int y, const vector<int>&v, int l) {\n\tlong long int id = 0;\n\tid += x;\n\tid *= 10;\n\tid += y;\n\tid *= 10;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tid += v[i];\n\t\tid *= 10;\n\t}\n\tid += l;\n\treturn id;\n}\nmap<long long int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\t//assert(x < C);\n\tassert(R != y || !x);\n\tconst long long int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert(false);\n\treturn memo[id];\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 700, "memory_kb": 94864, "score_of_the_acc": -0.6913, "final_rank": 15 }, { "submission_id": "aoj_1504_2149398", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nlong long int getid(int x, int y, const vector<int>&v, int l) {\n\tlong long int id = 0;\n\tid += x;\n\tid *= 10;\n\tid += y;\n\tid *= 10;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tid += v[i];\n\t\tid *= 10;\n\t}\n\tid += l;\n\treturn id;\n}\nmap<long long int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\t//assert(x < C);\n\t//assert(R != y || x);\n\tconst long long int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tassert(false);\n\treturn memo[id];\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 710, "memory_kb": 94788, "score_of_the_acc": -0.6943, "final_rank": 18 }, { "submission_id": "aoj_1504_2149396", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\n\nconst int mod = 100000007;\nstruct Mod {\npublic:\n\tint num;\n\tMod() : Mod(0) { ; }\n\tMod(long long int n) : num((n % mod + mod) % mod) {\n\t\tstatic_assert(mod<INT_MAX / 2, \"mod is too big, please make num 'long long int' from 'int'\");\n\t}\n\tMod(int n) : Mod(static_cast<long long int>(n)) { ; }\n\toperator int() { return num; }\n};\n\nMod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }\nMod operator+(const long long int a, const Mod b) { return Mod(a) + b; }\nMod operator+(const Mod a, const long long int b) { return b + a; }\nMod operator++(Mod &a) { return a + Mod(1); }\nMod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }\nMod operator-(const long long int a, const Mod b) { return Mod(a) - b; }\nMod operator--(Mod &a) { return a - Mod(1); }\nMod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }\nMod operator*(const long long int a, const Mod b) { return Mod(a)*b; }\nMod operator*(const Mod a, const long long int b) { return Mod(b)*a; }\nMod operator*(const Mod a, const int b) { return Mod(b)*a; }\nMod operator+=(Mod &a, const Mod b) { return a = a + b; }\nMod operator+=(long long int &a, const Mod b) { return a = a + b; }\nMod operator-=(Mod &a, const Mod b) { return a = a - b; }\nMod operator-=(long long int &a, const Mod b) { return a = a - b; }\nMod operator*=(Mod &a, const Mod b) { return a = a * b; }\nMod operator*=(long long int &a, const Mod b) { return a = a * b; }\nMod operator*=(Mod& a, const long long int &b) { return a = a * b; }\nMod operator^(const Mod a, const int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = (a * a) ^ (n / 2);\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod mod_pow(const Mod a, const long long int n) {\n\tif (n == 0) return Mod(1);\n\tMod res = mod_pow((a * a), (n / 2));\n\tif (n % 2) res = res * a;\n\treturn res;\n}\nMod inv(const Mod a) { return a ^ (mod - 2); }\nMod operator/(const Mod a, const Mod b) {\n\tassert(b.num != 0);\n\treturn a * inv(b);\n}\nMod operator/(const long long int a, const Mod b) {\n\treturn Mod(a) / b;\n}\nMod operator/=(Mod &a, const Mod b) {\n\treturn a = a / b;\n}\n\n\nlong long int getid(int x, int y, const vector<int>&v, int l) {\n\tlong long int id = 0;\n\tid += x;\n\tid *= 10;\n\tid += y;\n\tid *= 10;\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tid += v[i];\n\t\tid *= 10;\n\t}\n\tid += l;\n\treturn id;\n}\nmap<long long int, Mod>memo;\nint R, C;\nvector<vector<int>>field;\nMod getans(int x, int y, const vector<int>&v,int l) {\n\t//assert(x < C);\n\t//assert(R != y || x);\n\tconst long long int id = getid(x, y, v, l);\n\tif (memo.find(id) != memo.end()) {\n\t\treturn memo[id];\n\t}\n\telse {\n\t\tif (y == R) {\n\t\t\tbool ok = true;\n\t\t\tif (l)ok = false;\n\t\t\tif (!all_of(v.begin(), v.end(), [](const int n) {return n == 0; }))ok = false;\n\t\t\treturn memo[id] = ok;\n\t\t}\n\t\telse {\n\t\t\tif (x == 0) {\n\t\t\t\tif (l)return memo[id] = 0;\n\t\t\t}\n\t\t\tif (field[y][x]) {\n\t\t\t\tMod ans = 0;\n\t\t\t\tfor (int lnum = 0; lnum <= 2;lnum++) {\n\t\t\t\t\tint dnum = field[y][x] - l - v[x]-lnum;\n\t\t\t\t\tif (dnum < 0 || dnum>= 3)ans += 0;\n\t\t\t\t\telse {\n\t\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\t\tnexty++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnextv[x] = dnum;\n\t\t\t\t\t\tans += getans(nextx, nexty, nextv, lnum);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn memo[id] = ans;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (l&&v[x])return memo[id] = 0;\n\t\t\t\telse {\n\t\t\t\t\tint nexty = y, nextx = x + 1;\n\t\t\t\t\tvector<int>nextv(v);\n\t\t\t\t\tif (nextx == C) {\n\t\t\t\t\t\tnextx = 0;\n\t\t\t\t\t\tnexty++;\n\t\t\t\t\t}\n\t\t\t\t\treturn memo[id]=getans(nextx, nexty,v, l);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t}\n\t}\n\n}\nint main() {\n\tcin >> R >> C;\n\tfield = vector<vector<int>>(R, vector<int>(C));\n\tfor (int i = 0; i < R; ++i) {\n\t\tstring st; cin >> st;\n\t\tfor (int j = 0; j < C; ++j) {\n\t\t\tif (st[j] == '.')field[i][j] = 0;\n\t\t\telse field[i][j] = st[j] - '0';\n\t\t}\n\t}\n\tvector<int>v(C);\n\tMod ans = getans(0, 0, v, 0);\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 700, "memory_kb": 94852, "score_of_the_acc": -0.6913, "final_rank": 14 }, { "submission_id": "aoj_1504_2084349", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<stack>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\nusing namespace std;\n//kaewasuretyuui\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pii> vp;\ntypedef vector<vp> vvp;\ntypedef vector<string> vs;\ntypedef pair<int,pii> pip;\ntypedef vector<pip>vip;\nconst double PI=acos(-1);\nconst double EPS=1e-8;\nconst double inf=1e8;\nvector<map<vi,int> >dp;\nvi tmp;\nstring tms;\nint r,n,m,c,MOD=100000007;\n//map<vi,int>q;\nvi q;\nvi f(int a){\n\tvi out(m);\n\trep(i,m){\n\t\tout[m-i-1]=a%3;\n\t\ta/=3;\n\t}\n\treturn out;\n}\nint ff(vi a){\n\tint t=0;\n\trep(i,m){\n\t\tt*=3;\n\t\tt+=a[i];\n\t}\n\treturn t;\n}\nvoid dfs(int a){\n\tif(a==m){\n\t\tif(r)return;\n\t\t(q[ff(tmp)]+=c)%=MOD;\t\n\t\treturn;\n\t}\n\tif(tms[a]=='.'){\n\t\tif(r&&tmp[a])return;\n\t\tdfs(a+1);\n\t\treturn;\n\t}\n\tint w=r;\n\tif(tms[a]-'0'<w)return;\n\ttms[a]-=w;\n\trep(i,min(3,tms[a]-'0'+1))if(tms[a]-'0'-i<3){\n\t\ttmp[a]=i;\n\t\tr=tms[a]-'0'-i;\n\t\tdfs(a+1);\n\t\ttmp[a]=0;\n\t}\n\t\n\ttms[a]+=w;\n}\nint main(){\n\tcin>>n>>m;\n\tvs in(n);\n\trep(i,n)cin>>in[i];\n//\tmap<vi,int>dp;\n\tint N=pow(3,m);\n\tvi dp(N);\n\tdp[0]=1;\n\trep(i,n){\n\t\tq=vi(N);\n\t\trep(z,N)if(dp[z]){\n\t//\t\tfor(map<vi,int>::iterator it=dp.begin();it!=dp.end();it++){\n\t\t\ttmp=f(z);\n\t\t\ttms=in[i];\n\t\t\tbool h=true;\n\t\t\trep(j,m)if(tms[j]!='.'){\n\t\t\t\tif(tmp[j]>tms[j]-'0')h=false;\n\t\t\t\telse{\n\t\t\t\t\ttms[j]-=tmp[j];\n\t\t\t\t\ttmp[j]=0;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!h)continue;\n\t\t\tr=0;\n\t\t\tc=dp[z];\n\t\t\tdfs(0);\n\t\t}\n\t\tdp=q;\n\t}\n\tcout<<dp[0]<<endl;\n}", "accuracy": 1, "time_ms": 3010, "memory_kb": 3824, "score_of_the_acc": -1, "final_rank": 8 }, { "submission_id": "aoj_1504_1838760", "code_snippet": "#include <algorithm>\n#include <cstring>\n#include <iostream>\n#include <vector>\nusing namespace std;\n\nint W, H, a[11][11], rt[10][10], dn[10][10], dp[2][1 << 20], y, z;\nvector<int> b;\n\nvoid f(int x, int p) {\n int rx = rt[y][x];\n int mn = min({ 2, a[y][x], a[y][rx] });\n mn = p >> x * 2 + 2 & ~(-1 << (rx - x - 1) * 2) ? 0 : mn;\n for (int i = a[y][x] - min({ 2, a[y][x], a[dn[y][x]][x] }); i <= mn; ++i) {\n int q = p & ~(3 << x * 2) | a[y][x] - i << x * 2;\n if (rx == W) {\n dp[y & 1][q] = (dp[y & 1][q] + z) % 100000007;\n } else {\n a[y][rx] -= i;\n f(rx, q);\n a[y][rx] += i;\n }\n }\n}\n\nvoid g(int x, int p) {\n if (x) {\n for (int i = 0; i < 3; ++i)\n g(x - 1, p << 2 | i);\n } else {\n b.emplace_back(p);\n }\n}\n\nint main() {\n cin >> H >> W;\n int A[11][11]{};\n for (int y = 0; y < H; ++y) {\n char s[11];\n cin >> s;\n bool none = true;\n for (int x = 0; x < W; ++x) {\n if (s[x] != '.') {\n A[y][x] = s[x] - '0';\n none = false;\n }\n }\n if (none) {\n --H;\n --y;\n }\n }\n for (int y = 0; y < H; ++y) {\n for (int x = 0; x < W; ++x) {\n if (A[y][x]) {\n int i = x + 1;\n for (; i < W && !A[y][i]; ++i);\n rt[y][x] = i;\n for (i = y + 1; i < H && !A[i][x]; ++i);\n dn[y][x] = i;\n }\n }\n }\n memcpy(a, A, sizeof a);\n g(W, 0);\n dp[1][0] = 1;\n for (y = 0; y < H; ++y) {\n memset(dp[y & 1], 0, sizeof dp[y & 1]);\n int x = 0;\n for (; !a[y][x]; ++x);\n for (int p : b) {\n z = dp[y + 1 & 1][p];\n if (z) {\n for (int i = x; i < W; i = rt[y][i])\n a[y][i] -= p >> i * 2 & 3;\n f(x, p);\n memcpy(a[y], A[y], sizeof a[y]);\n }\n }\n }\n cout << dp[H + 1 & 1][0] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1060, "memory_kb": 11508, "score_of_the_acc": -0.3889, "final_rank": 4 }, { "submission_id": "aoj_1504_1364805", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nconst int mod = 100000007;\nint h,w,pow3[13];\nint dp[11*11][180000];\nchar field[12][12];\n\n#define get(bit,x) ((bit)/pow3[(x)]%3)\n#define set(bit,x,value) ((bit)-(((bit)/pow3[x]%3)*pow3[x])+(value*pow3[x]))\n#define fix(bit,width) ((bit)*3%pow3[width])\n\nvoid compute(){\n bool starter = true; \n rep(y,h){\n rep(x,w){\n if( starter && field[y][x] == '.' ) continue;\n if( starter ) { starter = false; ++dp[x+y*w][0]; }\n\n bool update = false;\n rep(bitmask3,pow3[w+1]){\n\tif( dp[x+y*w][bitmask3] == 0 ) continue;\n\tupdate = true;\n\tint left = get(bitmask3,x);\n\tint top = get(bitmask3,x+1);\n\tif( field[y][x] == '.' ) {\n\t if( left && top ) continue;\n\t if( left && x == w-1 ) continue;\n\t if( top && y == h-1 ) continue;\n\t int nbitmask = bitmask3;\n\t nbitmask = set(nbitmask,x,top);\n\t nbitmask = set(nbitmask,x+1,left);\n\t if( x == w-1 ) nbitmask = fix(nbitmask,w+1);\n\t ( dp[x+y*w+1][nbitmask] += dp[x+y*w][bitmask3] ) %= mod;\n\t continue;\n\t}\n\n\tint remain = field[y][x] - '0' - left - top;\n\tif( remain < 0 || remain > 4 ) continue;\n\trep(next_bottom,3){\n\t int next_right = remain - next_bottom;\n\t if( next_right < 0 || next_right > 2 ) continue;\n\t int nbitmask = bitmask3;\n\t if( next_right && x == w-1 ) continue;\n\t if( next_bottom && y == h-1 ) continue;\n\t nbitmask = set(nbitmask,x,next_bottom);\n\t nbitmask = set(nbitmask,x+1,next_right);\n\t if( x == w-1 ) nbitmask = fix(nbitmask,w+1);\n\t ( dp[x+y*w+1][nbitmask] += dp[x+y*w][bitmask3] ) %= mod;\n\t}\n }\n }\n }\n int sum = 0;\n rep(i,pow3[w+1]) ( sum += dp[h*w][i] ) %= mod;\n assert( sum >= 0 );\n cout << sum << endl;\n}\n\nint main(){\n pow3[0] = 1;\n REP(i,1,13) pow3[i] = pow3[i-1] * 3;\n\n cin >> h >> w;\n rep(i,h) scanf(\" %s\",&field[i]);\n compute();\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 70832, "score_of_the_acc": -0.3596, "final_rank": 2 }, { "submission_id": "aoj_1504_1154919", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\nchar str[20][20];\nint pow3[13];\nint mod=100000007;\nint dp[12][12][60000][3];\nint main(){\n\tint a,b;\n\tscanf(\"%d%d\",&a,&b);\n\tfor(int i=0;i<a;i++){\n\t\tscanf(\"%s\",str[i]);\n\t}\n\tpow3[0]=1;\n\tfor(int i=1;i<13;i++)pow3[i]=pow3[i-1]*3;\n\tdp[0][0][0][0]=1;\n\tfor(int i=0;i<a;i++){\n\t\tfor(int j=0;j<b;j++){\n\t\t\tfor(int k=0;k<pow3[b];k++){\n\t\t\t\tfor(int l=0;l<3;l++){\n\t\t\t\t\tif(!dp[i][j][k][l])continue;\n\t\t\t//\t\tprintf(\"%d %d %d %d: %d\\n\",i,j,k,l,dp[i][j][k][l]);\n\t\t\t\t\tif(str[i][j]=='.'){\n\t\t\t\t\t\tif(k%3&&l)continue;\n\t\t\t\t\t\tdp[i][j+1][k/3+pow3[b-1]*(k%3)][l]=(dp[i][j+1][k/3+pow3[b-1]*(k%3)][l]+dp[i][j][k][l])%mod;\n\t\t\t\t\t}else{\n\t\t\t\t\t\tint rem=str[i][j]-'0'-l-k%3;\n\t\t\t\t\t\tif(rem<0)continue;\n\t\t\t\t\t\tfor(int m=0;m<3;m++)for(int n=0;n<3;n++){\n\t\t\t\t\t\t\tif(rem==m+n){\n\t\t\t\t\t\t\t\tdp[i][j+1][k/3+pow3[b-1]*m][n]=(dp[i][j+1][k/3+pow3[b-1]*m][n]+dp[i][j][k][l])%mod;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int j=0;j<pow3[b];j++){\n\t\t\tdp[i+1][0][j][0]=dp[i][b][j][0];\n\t\t}\n\t}\n\tprintf(\"%d\\n\",dp[a][0][0][0]);\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 85032, "score_of_the_acc": -0.4248, "final_rank": 5 } ]
aoj_1500_cpp
Problem A : ID A大学ではIDの入力ミスが多発していた。 そこで、A大学は入力ミス防止のため新しいIDを発行することにした。 新しいIDには入力ミス防止のためにIDが正しいかどうかチェックする方法がある。 ・全ての桁の数字の総和を求める。 ・ただし、右端の桁を1番目として、偶数番目の桁の数字を2倍にする。 ・2倍することによって数字が10以上になった時、1の位の数字と10の位の数字を加算した数字をその桁の数字とする。 ・総和が10で割り切れれば正しいID、そうでなければ間違いとする。 例として、53579というIDをチェックする。 全ての桁の数字の総和を求めるので、 5 + 3 + 5 + 7 + 9 ただし、偶数番目の桁の数字を2倍にするので、 5 + 6 + 5 + 14 + 9 2倍することによって数字が10以上になった時、1の位の数字と10の位の数字を加算した数字をその桁の数字とするので、 5 + 6 + 5 + (1 + 4) + 9 以上より、総和は30となる。30は10で割り切れるので、53579は正しいIDである。 B君はA大学の大学生であり、新しいIDを発行してもらったが、IDの一部の桁を忘れてしまった。 しかし、忘れてしまった部分にどんな数字が入るか、いくつか候補を絞ることに成功した。 あなたの仕事は、B君のIDの正しい組み合わせが何通りあるかを求めることである。 Input 入力は以下のフォーマットで与えられる。 n ID m a 0 a 1 ... a m-1 n は ID の桁の数である。 ID の各桁には0~9の数字または忘れた桁であるということを示す'*'という文字が入る。 m は'*'に入る数字の候補の数である。 a i は'*'に入る数字の候補である。 入力は以下の制約を満たす 1 ≤ n ≤ 100,000 1 ≤ m ≤ 10 0 ≤ a i ≤ 9 1 ≤ '*'の数 ≤ 7 Output 答えの値を1行に出力せよ Sample Input 1 5 5*57* 2 3 9 Sample Output 1 1 Sample Input 2 15 2***9*2*6*1199* 9 0 1 2 3 4 6 7 8 9 Sample Output 2 478297
[ { "submission_id": "aoj_1500_6006661", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <stdio.h>\n#include <vector>\nusing namespace std;\nconst int maxn=100010;\n\nint a[10]={0,2,4,6,8,1,3,5,7,9};\nint choose[10];\nint sum=0,sum2=0,sum3=0;\nint ans=0;\nint n,m;\nvoid dfs2(int x,int ss)\n{\n if(x==sum3){\n if(ss%10==0){\n ans++;\n }\n return;\n }\n for(int i=0;i<m;i++){\n dfs2(x+1,ss+choose[i]);\n }\n}\nvoid dfs(int x,int ss)\n{\n if(x==sum2){\n dfs2(0,ss);\n return;\n }\n for(int i=0;i<m;i++){\n dfs(x+1,ss+a[choose[i]]);\n }\n}\nint main()\n{\n cin>>n;\n string s;\n cin>>s;\n cin>>m;\n for(int i=0;i<m;i++) cin>>choose[i];\n int len=s.size();\n for(int i=len-1;i>=0;i--){\n if(s[i]!='*'){\n if((len-i)%2==1){\n sum+=s[i]-'0';\n }\n else sum=sum+a[s[i]-'0'];\n }\n else{\n if((len-i)%2==1){\n sum3++;\n }\n else sum2++;\n }\n }\n dfs(0,sum);\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3336, "score_of_the_acc": -0.046, "final_rank": 1 }, { "submission_id": "aoj_1500_5084109", "code_snippet": "#line 2 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#include <bits/stdc++.h>\n#line 6 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define FOR(i, m, n) for (int i = (m); i < (n); ++i)\n#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)\n#define rfor(i, m, n) for (int i = (m); i >= (n); --i)\n#define unless(c) if (!(c))\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)\n\nusing namespace std;\nusing ll = long long;\nusing LD = long double;\nusing VB = vector<bool>;\nusing VVB = vector<VB>;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VS = vector<string>;\nusing VD = vector<LD>;\nusing PII = pair<int, int>;\nusing VP = vector<PII>;\nusing PLL = pair<ll, ll>;\nusing VPL = vector<PLL>;\ntemplate <class T> using PQ = priority_queue<T>;\ntemplate <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;\nconstexpr int inf = 1000000000;\nconstexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;\nconstexpr long double PI = 3.14159265358979323846, EPS = 1e-12;\n#line 7 \"/home/yuruhiya/programming/library/template/Input.cpp\"\nusing namespace std;\n\n#ifdef _WIN32\n#define getchar_unlocked _getchar_nolock\n#define putchar_unlocked _putchar_nolock\n#define fwrite_unlocked fwrite\n#define fflush_unlocked fflush\n#endif\nclass Scanner {\n\tstatic int gc() {\n\t\treturn getchar_unlocked();\n\t}\n\tstatic char next_char() {\n\t\tchar c;\n\t\tread(c);\n\t\treturn c;\n\t}\n\ttemplate <class T> static void read(T& v) {\n\t\tcin >> v;\n\t}\n\tstatic void read(char& v) {\n\t\twhile (isspace(v = gc()))\n\t\t\t;\n\t}\n\tstatic void read(bool& v) {\n\t\tv = next_char() != '0';\n\t}\n\tstatic void read(string& v) {\n\t\tv.clear();\n\t\tfor (char c = next_char(); !isspace(c); c = gc()) v += c;\n\t}\n\tstatic void read(int& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long long& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(double& v) {\n\t\tv = 0;\n\t\tdouble dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long double& v) {\n\t\tv = 0;\n\t\tlong double dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\ttemplate <class T, class U> static void read(pair<T, U>& v) {\n\t\tread(v.first);\n\t\tread(v.second);\n\t}\n\ttemplate <class T> static void read(vector<T>& v) {\n\t\tfor (auto& e : v) read(e);\n\t}\n\ttemplate <size_t N = 0, class T> static void read_tuple_impl(T& v) {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tread(get<N>(v));\n\t\t\tread_tuple_impl<N + 1>(v);\n\t\t}\n\t}\n\ttemplate <class... T> static void read(tuple<T...>& v) {\n\t\tread_tuple_impl(v);\n\t}\n\tstruct ReadVectorHelper {\n\t\tsize_t n;\n\t\tReadVectorHelper(size_t _n) : n(_n) {}\n\t\ttemplate <class T> operator vector<T>() {\n\t\t\tvector<T> v(n);\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\tstruct Read2DVectorHelper {\n\t\tsize_t n, m;\n\t\tRead2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}\n\t\ttemplate <class T> operator vector<vector<T>>() {\n\t\t\tvector<vector<T>> v(n, vector<T>(m));\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\npublic:\n\tstring read_line() const {\n\t\tstring v;\n\t\tfor (char c = gc(); c != '\\n' && c != '\\0'; c = gc()) v += c;\n\t\treturn v;\n\t}\n\ttemplate <class T> T read() const {\n\t\tT v;\n\t\tread(v);\n\t\treturn v;\n\t}\n\ttemplate <class T> vector<T> read_vector(size_t n) const {\n\t\tvector<T> a(n);\n\t\tread(a);\n\t\treturn a;\n\t}\n\ttemplate <class T> operator T() const {\n\t\treturn read<T>();\n\t}\n\tint operator--(int) const {\n\t\treturn read<int>() - 1;\n\t}\n\tReadVectorHelper operator[](size_t n) const {\n\t\treturn ReadVectorHelper(n);\n\t}\n\tRead2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {\n\t\treturn Read2DVectorHelper(nm);\n\t}\n\tvoid operator()() const {}\n\ttemplate <class H, class... T> void operator()(H&& h, T&&... t) const {\n\t\tread(h);\n\t\toperator()(forward<T>(t)...);\n\t}\n\nprivate:\n\ttemplate <template <class...> class, class...> struct Multiple;\n\ttemplate <template <class...> class V, class Head, class... Tail>\n\tstruct Multiple<V, Head, Tail...> {\n\t\ttemplate <class... Args> using vec = V<vector<Head>, Args...>;\n\t\tusing type = typename Multiple<vec, Tail...>::type;\n\t};\n\ttemplate <template <class...> class V> struct Multiple<V> { using type = V<>; };\n\ttemplate <class... T> using multiple_t = typename Multiple<tuple, T...>::type;\n\ttemplate <size_t N = 0, class T> void multiple_impl(T& t) const {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tauto& vec = get<N>(t);\n\t\t\tusing V = typename remove_reference_t<decltype(vec)>::value_type;\n\t\t\tvec.push_back(read<V>());\n\t\t\tmultiple_impl<N + 1>(t);\n\t\t}\n\t}\n\npublic:\n\ttemplate <class... T> auto multiple(size_t h) const {\n\t\tmultiple_t<T...> result;\n\t\twhile (h--) multiple_impl(result);\n\t\treturn result;\n\t}\n} in;\n#define inputs(T, ...) \\\n\tT __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define ini(...) inputs(int, __VA_ARGS__)\n#define inl(...) inputs(long long, __VA_ARGS__)\n#define ins(...) inputs(string, __VA_ARGS__)\n#line 7 \"/home/yuruhiya/programming/library/template/Output.cpp\"\n#include <charconv>\n#line 10 \"/home/yuruhiya/programming/library/template/Output.cpp\"\nusing namespace std;\n\nstruct BoolStr {\n\tconst char *t, *f;\n\tBoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}\n} Yes(\"Yes\", \"No\"), yes(\"yes\", \"no\"), YES(\"YES\", \"NO\"), Int(\"1\", \"0\");\nstruct DivStr {\n\tconst char *d, *l;\n\tDivStr(const char* _d, const char* _l) : d(_d), l(_l) {}\n} spc(\" \", \"\\n\"), no_spc(\"\", \"\\n\"), end_line(\"\\n\", \"\\n\"), comma(\",\", \"\\n\"),\n no_endl(\" \", \"\");\nclass Printer {\n\tBoolStr B{Yes};\n\tDivStr D{spc};\n\npublic:\n\tvoid print(int v) const {\n\t\tchar buf[12]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid print(long long v) const {\n\t\tchar buf[21]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid print(bool v) const {\n\t\tprint(v ? B.t : B.f);\n\t}\n\tvoid print(vector<bool>::reference v) const {\n\t\tprint(v ? B.t : B.f);\n\t}\n\tvoid print(char v) const {\n\t\tputchar_unlocked(v);\n\t}\n\tvoid print(const char* v) const {\n\t\tfwrite_unlocked(v, 1, strlen(v), stdout);\n\t}\n\tvoid print(double v) const {\n\t\tprintf(\"%.20f\", v);\n\t}\n\tvoid print(long double v) const {\n\t\tprintf(\"%.20Lf\", v);\n\t}\n\ttemplate <class T> void print(const T& v) const {\n\t\tcout << v;\n\t}\n\ttemplate <class T, class U> void print(const pair<T, U>& v) const {\n\t\tprint(v.first);\n\t\tprint(D.d);\n\t\tprint(v.second);\n\t}\n\ttemplate <class InputIterater>\n\tvoid print_range(const InputIterater& begin, const InputIterater& end) const {\n\t\tfor (InputIterater i = begin; i != end; ++i) {\n\t\t\tif (i != begin) print(D.d);\n\t\t\tprint(*i);\n\t\t}\n\t}\n\ttemplate <class T> void print(const vector<T>& v) const {\n\t\tprint_range(v.begin(), v.end());\n\t}\n\ttemplate <class T, size_t N> void print(const array<T, N>& v) const {\n\t\tprint_range(v.begin(), v.end());\n\t}\n\ttemplate <class T> void print(const vector<vector<T>>& v) const {\n\t\tfor (size_t i = 0; i < v.size(); ++i) {\n\t\t\tif (i) print(D.l);\n\t\t\tprint(v[i]);\n\t\t}\n\t}\n\n\tPrinter() = default;\n\tPrinter(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}\n\tPrinter& operator()() {\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H> Printer& operator()(H&& h) {\n\t\tprint(h);\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H, class... T> Printer& operator()(H&& h, T&&... t) {\n\t\tprint(h);\n\t\tprint(D.d);\n\t\treturn operator()(forward<T>(t)...);\n\t}\n\ttemplate <class InputIterator>\n\tPrinter& range(const InputIterator& begin, const InputIterator& end) {\n\t\tprint_range(begin, end);\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class T> Printer& range(const T& a) {\n\t\trange(a.begin(), a.end());\n\t\treturn *this;\n\t}\n\ttemplate <class... T> void exit(T&&... t) {\n\t\toperator()(forward<T>(t)...);\n\t\tstd::exit(EXIT_SUCCESS);\n\t}\n\tPrinter& flush() {\n\t\tfflush_unlocked(stdout);\n\t\treturn *this;\n\t}\n\tPrinter& set(const BoolStr& b) {\n\t\tB = b;\n\t\treturn *this;\n\t}\n\tPrinter& set(const DivStr& d) {\n\t\tD = d;\n\t\treturn *this;\n\t}\n\tPrinter& set(const char* t, const char* f) {\n\t\tB = BoolStr(t, f);\n\t\treturn *this;\n\t}\n} out;\n#line 3 \"/home/yuruhiya/programming/library/template/Step.cpp\"\nusing namespace std;\n\ntemplate <class T> struct Step {\n\tusing value_type = T;\n\n\tclass iterator {\n\t\tvalue_type a, b, c;\n\n\tpublic:\n\t\tconstexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}\n\t\tconstexpr iterator(value_type _b, value_type _c, value_type _s)\n\t\t : a(_b), b(_c), c(_s) {}\n\t\tconstexpr iterator& operator++() {\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn *this;\n\t\t}\n\t\tconstexpr iterator operator++(int) {\n\t\t\titerator tmp = *this;\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn tmp;\n\t\t}\n\t\tconstexpr const value_type& operator*() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr const value_type* operator->() const {\n\t\t\treturn &a;\n\t\t}\n\t\tconstexpr bool operator==(const iterator& i) const {\n\t\t\treturn b == i.b;\n\t\t}\n\t\tconstexpr bool operator!=(const iterator& i) const {\n\t\t\treturn !(b == i.b);\n\t\t}\n\t\tconstexpr value_type start() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr value_type size() const {\n\t\t\treturn b;\n\t\t}\n\t\tconstexpr value_type step() const {\n\t\t\treturn c;\n\t\t}\n\t};\n\tconstexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}\n\tconstexpr iterator begin() const {\n\t\treturn be;\n\t}\n\tconstexpr iterator end() const {\n\t\treturn en;\n\t}\n\tconstexpr value_type start() const {\n\t\treturn be.start();\n\t}\n\tconstexpr value_type size() const {\n\t\treturn be.size();\n\t}\n\tconstexpr value_type step() const {\n\t\treturn be.step();\n\t}\n\tconstexpr value_type sum() const {\n\t\treturn start() * size() + step() * (size() * (size() - 1) / 2);\n\t}\n\toperator vector<value_type>() const {\n\t\treturn to_a();\n\t}\n\tauto to_a() const {\n\t\tvector<value_type> result;\n\t\tresult.reserve(size());\n\t\tfor (auto i : *this) {\n\t\t\tresult.push_back(i);\n\t\t}\n\t\treturn result;\n\t}\n\nprivate:\n\titerator be, en;\n};\ntemplate <class T> constexpr auto step(T a) {\n\treturn Step<T>(0, a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b) {\n\treturn Step<T>(a, b - a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b, T c) {\n\treturn Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);\n}\n#line 8 \"/home/yuruhiya/programming/library/template/Ruby.cpp\"\nusing namespace std;\n\ntemplate <class F> struct Callable {\n\tF func;\n\tCallable(const F& f) : func(f) {}\n};\ntemplate <class T, class F> auto operator|(const T& v, const Callable<F>& c) {\n\treturn c.func(v);\n}\n\nstruct Sort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\treturn v;\n\t}\n} Sort;\nstruct SortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) < f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} SortBy;\nstruct RSort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(rbegin(v), rend(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {\n\t\tsort(rbegin(v), rend(v));\n\t\treturn v;\n\t}\n} RSort;\nstruct RSortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) > f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} RSortBy;\nstruct Reverse_impl {\n\ttemplate <class T> friend auto operator|(T v, const Reverse_impl& c) {\n\t\treverse(begin(v), end(v));\n\t\treturn v;\n\t}\n} Reverse;\nstruct Unique_impl {\n\ttemplate <class T> friend auto operator|(T v, const Unique_impl& c) {\n\t\tv.erase(unique(begin(v), end(v), end(v)));\n\t\treturn v;\n\t}\n} Unique;\nstruct Uniq_impl {\n\ttemplate <class T> friend auto operator|(T v, const Uniq_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\tv.erase(unique(begin(v), end(v)), end(v));\n\t\treturn v;\n\t}\n} Uniq;\nstruct Rotate_impl {\n\tauto operator()(int&& left) {\n\t\treturn Callable([&](auto v) {\n\t\t\tint s = static_cast<int>(size(v));\n\t\t\tassert(-s <= left && left <= s);\n\t\t\tif (0 <= left) {\n\t\t\t\trotate(begin(v), begin(v) + left, end(v));\n\t\t\t} else {\n\t\t\t\trotate(begin(v), end(v) + left, end(v));\n\t\t\t}\n\t\t\treturn v;\n\t\t});\n\t}\n} Rotate;\nstruct Max_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *max_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Max_impl& c) {\n\t\treturn *max_element(begin(v), end(v));\n\t}\n} Max;\nstruct Min_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *min_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Min_impl& c) {\n\t\treturn *min_element(begin(v), end(v));\n\t}\n} Min;\nstruct MaxPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MaxPos_impl& c) {\n\t\treturn max_element(begin(v), end(v)) - begin(v);\n\t}\n} MaxPos;\nstruct MinPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MinPos_impl& c) {\n\t\treturn min_element(begin(v), end(v)) - begin(v);\n\t}\n} MinPos;\nstruct MaxBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_it = begin(v);\n\t\t\tauto max_val = f(*max_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_it = it;\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *max_it;\n\t\t});\n\t}\n} MaxBy;\nstruct MinBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_it = begin(v);\n\t\t\tauto min_val = f(*min_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_it = it;\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *min_it;\n\t\t});\n\t}\n} MinBy;\nstruct MaxOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn max_val;\n\t\t});\n\t}\n} MaxOf;\nstruct MinOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn min_val;\n\t\t});\n\t}\n} MinOf;\nstruct Count_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return count(begin(v), end(v), val); });\n\t}\n} Count;\nstruct CountIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return count_if(begin(v), end(v), f); });\n\t}\n} CountIf;\nstruct Index_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(begin(v), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n\ttemplate <class V> auto operator()(const V& val, size_t i) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(next(begin(v), i), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} Index;\nstruct IndexIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} IndexIf;\nstruct FindIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<typename decltype(v)::value_type> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(*result) : nullopt;\n\t\t});\n\t}\n} FindIf;\nstruct Sum_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\treturn accumulate(next(begin(v)), end(v), f(*begin(v)),\n\t\t\t [&](const auto& a, const auto& b) { return a + f(b); });\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Sum_impl& c) {\n\t\treturn accumulate(begin(v), end(v), typename T::value_type{});\n\t}\n} Sum;\nstruct Includes {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });\n\t}\n} Includes;\nstruct IncludesIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });\n\t}\n} IncludesIf;\nstruct RemoveIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tv.erase(remove_if(begin(v), end(v), f), end(v));\n\t\t\treturn v;\n\t\t});\n\t}\n} RemoveIf;\nstruct Each_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tf(i);\n\t\t\t}\n\t\t});\n\t}\n} Each;\nstruct EachConsPair_impl {\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, EachConsPair_impl& c) {\n\t\tvector<pair<value_type, value_type>> result;\n\t\tif (size(v) >= 2) {\n\t\t\tresult.reserve(size(v) - 1);\n\t\t\tfor (size_t i = 0; i < size(v) - 1; ++i) {\n\t\t\t\tresult.emplace_back(v[i], v[i + 1]);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n} EachConsPair;\nstruct Select_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing value_type = typename decltype(v)::value_type;\n\t\t\tvector<value_type> result;\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) result.push_back(i);\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Select;\nstruct Map_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing result_type = invoke_result_t<F, typename decltype(v)::value_type>;\n\t\t\tvector<result_type> result;\n\t\t\tresult.reserve(size(v));\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult.push_back(f(i));\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Map;\nstruct Indexed_impl {\n\ttemplate <class T> friend auto operator|(const T& v, Indexed_impl& c) {\n\t\tusing value_type = typename T::value_type;\n\t\tvector<pair<value_type, int>> result;\n\t\tresult.reserve(size(v));\n\t\tint index = 0;\n\t\tfor (const auto& i : v) {\n\t\t\tresult.emplace_back(i, index++);\n\t\t}\n\t\treturn result;\n\t}\n} Indexed;\nstruct AllOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (!f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} AllOf;\nstruct AnyOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return true;\n\t\t\t}\n\t\t\treturn false;\n\t\t});\n\t}\n} AnyOf;\nstruct NoneOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} NoneOf;\n\nstruct Tally_impl {\n\ttemplate <class F> auto operator()(size_t max_val) {\n\t\treturn Callable([&](auto v) {\n\t\t\tvector<size_t> result(max_val);\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult[static_cast<size_t>(i)]++;\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, Tally_impl& c) {\n\t\tmap<value_type, size_t> result;\n\t\tfor (const auto& i : v) {\n\t\t\tresult[i]++;\n\t\t}\n\t\treturn result;\n\t}\n} Tally;\n\ntemplate <class T> auto operator*(const vector<T>& a, size_t n) {\n\tT result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult.insert(result.end(), a.begin(), a.end());\n\t}\n\treturn result;\n}\nauto operator*(string a, size_t n) {\n\tstring result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult += a;\n\t}\n\treturn result;\n}\ntemplate <class T, class U> auto& operator<<(vector<T>& a, const U& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T> auto& operator<<(string& a, const T& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T, class U> auto operator+(vector<T> a, const U& b) {\n\ta << b;\n\treturn a;\n}\ntemplate <class T> auto operator+(string a, const T& b) {\n\ta << b;\n\treturn a;\n}\n#line 7 \"/home/yuruhiya/programming/library/template/functions.cpp\"\nusing namespace std;\n\ntemplate <class T = long long> constexpr T TEN(size_t n) {\n\tT result = 1;\n\tfor (size_t i = 0; i < n; ++i) result *= 10;\n\treturn result;\n}\ntemplate <class T, class U,\n enable_if_t<is_integral_v<T> && is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr auto div_ceil(T n, U m) {\n\treturn (n + m - 1) / m;\n}\ntemplate <class T, class U> constexpr auto div_ceil2(T n, U m) {\n\treturn div_ceil(n, m) * m;\n}\ntemplate <class T> constexpr T triangle(T n) {\n\treturn (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);\n}\ntemplate <class T> constexpr T nC2(T n) {\n\treturn (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);\n}\ntemplate <class T, class U> constexpr auto middle(const T& l, const U& r) {\n\treturn l + (r - l) / 2;\n}\ntemplate <class T, class U, class V>\nconstexpr bool in_range(const T& v, const U& lower, const V& upper) {\n\treturn lower <= v && v < upper;\n}\ntemplate <class T, enable_if_t<is_integral_v<T>, nullptr_t> = nullptr>\nconstexpr bool is_square(T n) {\n\tT s = sqrt(n);\n\treturn s * s == n || (s + 1) * (s + 1) == n;\n}\ntemplate <class T = long long> constexpr T BIT(int b) {\n\treturn T(1) << b;\n}\ntemplate <class T> constexpr int BIT(T x, int i) {\n\treturn (x & (T(1) << i)) ? 1 : 0;\n}\ntemplate <class T> constexpr int Sgn(T x) {\n\treturn (0 < x) - (0 > x);\n}\ntemplate <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr T Pow(T a, U n) {\n\tassert(n >= 0);\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult *= a;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta *= a;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr T Powmod(T a, U n, T mod) {\n\tassert(n >= 0);\n\tif (a > mod) a %= mod;\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult = result * a % mod;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta = a * a % mod;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T> bool chmax(T& a, const T& b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool chmin(T& a, const T& b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> int sz(const T& v) {\n\treturn v.size();\n}\ntemplate <class T, class U> int lower_index(const T& a, const U& v) {\n\treturn lower_bound(all(a), v) - a.begin();\n}\ntemplate <class T, class U> int upper_index(const T& a, const U& v) {\n\treturn upper_bound(all(a), v) - a.begin();\n}\ntemplate <class T> auto Slice(const T& v, size_t i, size_t len) {\n\treturn i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();\n}\ntemplate <class T, class U = typename T::value_type> U Gcdv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);\n}\ntemplate <class T, class U = typename T::value_type> U Lcmv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);\n}\nnamespace internal {\n\ttemplate <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {\n\t\tif constexpr (N == 1) {\n\t\t\treturn vector(sizes[0], init);\n\t\t} else {\n\t\t\tint size = sizes[N - 1];\n\t\t\tsizes.pop_back();\n\t\t\treturn vector(size, make_vector<T, N - 1>(sizes, init));\n\t\t}\n\t}\n} // namespace internal\ntemplate <class T, size_t N>\nauto make_vector(const int (&sizes)[N], const T& init = T()) {\n\tvector s(rbegin(sizes), rend(sizes));\n\treturn internal::make_vector<T, N>(s, init);\n}\n#line 9 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#if __has_include(<library/dump.hpp>)\n#include <library/dump.hpp>\n#define LOCAL\n#else\n#define dump(...) ((void)0)\n#endif\n\ntemplate <class T> constexpr T oj_local(const T& oj, const T& local) {\n#ifndef LOCAL\n\treturn oj;\n#else\n\treturn local;\n#endif\n}\n#line 2 \"a.cpp\"\n\ntemplate <class T, class F, class value_type = typename T::value_type>\nvoid each_repeated_permutation(const T& array, size_t count, F f) {\n\tvector<value_type> permutation(count);\n\tauto dfs = [&](auto self, size_t size) -> void {\n\t\tif (size == count) {\n\t\t\tf(permutation);\n\t\t} else {\n\t\t\tfor (const auto& i : array) {\n\t\t\t\tpermutation[size] = i;\n\t\t\t\tself(self, size + 1);\n\t\t\t}\n\t\t}\n\t};\n\tdfs(dfs, 0);\n}\n\nint main() {\n\tini(n);\n\tauto s = in.read<string>() |\n\t Map([&](char c) { return c == '*' ? nullopt : optional(c - '0'); }) | Reverse;\n\tint sum = s | Indexed | Sum([&](auto p) {\n\t\t auto [v, i] = p;\n\t\t return v ? (*v * (i % 2 ? 2 : 1) - 1) % 9 + 1 : 0;\n\t });\n\tini(m);\n\tVI a = in[m];\n\tVI b = s | Indexed | Select([&](auto p) { return !p.first; }) |\n\t Map([&](auto p) { return p.second % 2 ? 2 : 1; });\n\n\tdump(a, b);\n\tint ans = 0;\n\teach_repeated_permutation(a, b.size(), [&](const auto& perm) {\n\t\tint sum2 = 0;\n\t\trep(i, sz(b)) {\n\t\t\tsum2 += (perm[i] * b[i] - 1) % 9 + 1;\n\t\t}\n\t\tans += (sum + sum2) % 10 == 0;\n\t});\n\tout(ans);\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 6216, "score_of_the_acc": -0.4617, "final_rank": 11 }, { "submission_id": "aoj_1500_4550817", "code_snippet": "#include <stdio.h>\nint combination(int ,int ,int*);\nint fact(int);\nint nest(int ,int ,int ,int* ,int* ,int*);\n\nint main(void) {\n int i;\n\tint n;\n\tchar s;\n\tint ID;\n\tint sum=0;\n\tint odd_num = 0;\n\tint even_num = 0;\n\tscanf(\"%d\",&n);\n\tscanf(\"%c\",&s);\n\tfor(i=0;i<n;i++){\n\t\tscanf(\"%c\",&s);\n\t if(s != '*')ID = s-'0';\n\t else ID = -1;\n\t\t//ID=(s!='*')?(int)(s-'0'):-1;\n\t\tif(ID>-1){\n\t\t\tif(i % 2){\n\t\t\t\tif(ID > 5)sum += ID*2-9;\n\t\t\t\telse sum += ID*2;\n\t\t\t}else sum += ID;\n\t\t}else if(i % 2)even_num++;\n\t\telse odd_num++;\n\t}//printf(\"%d\\n\",sum);\n\tint m;\n\tscanf(\"%d\",&m);\n\tint cand[10];\n\tfor(i=0;i<m;i++)scanf(\"%d\",&cand[i]);\n\t\n\t//printf(\"%d %d %d\\n\",odd_num,even_num,m);\n\tint odd_result;\n\tint even_result;\n\tint odd_sum=0;\n\tint even_sum=0;\n\tint consist_count = 0;\n\tint odd_ID_puttern[10];\n\tint even_ID_puttern[10];\n\tint dup_data[10] = {0};\n\t\n\twhile((odd_result = nest(0,odd_num,m,cand,odd_ID_puttern,dup_data))+1){\n\t\tfor(int i=0;i<odd_num;i++)odd_sum += odd_ID_puttern[i];\n\t\todd_result = combination(odd_num,odd_result,dup_data);//printf(\"%d\\n\",odd_result);\n\t\twhile((even_result = nest(1,even_num,m,cand,even_ID_puttern,dup_data))+1){\n\t\t\tfor(int j=0;j<even_num;j++)even_sum += (even_ID_puttern[j] < 5)?2*even_ID_puttern[j]:2*even_ID_puttern[j]-9;\n\t\t\teven_result = combination(even_num,even_result,dup_data);//printf(\"%d\\n\",even_result);\n\t\t\tif(!((sum+odd_sum+even_sum)%10)){/*\n\t\t\t\tfor(int k=0;k<odd_num;k++)printf(\"%d \",odd_ID_puttern[k]);printf(\" \");\n\t\t\t\tfor(int k=0;k<even_num;k++)printf(\"%d \",even_ID_puttern[k]);printf(\"\\n\");*/\n\t\t\t consist_count++;// += (odd_result * even_result);\n\t\t\t}\n\t\t\t\t//for(int k=0;k<odd_num;k++)printf(\"%d \",odd_ID_puttern[k]);printf(\" \");\n\t\t\t\t//for(int k=0;k<even_num;k++)printf(\"%d \",even_ID_puttern[k]);printf(\"\\n\");\n\t\t\teven_sum=0;\n\t\t}\n\t\todd_sum=0;\n\t}\n\tprintf(\"%d\\n\",consist_count);\n\treturn 0;\n}\n\nint combination(int column ,int dup_num ,int *dup){\n\tint i,dup_val = 1;\n\tfor(i=0;i<dup_num;i++)dup_val *= fact(dup[i]);\n\t//if(!(fact(column)/dup_val))printf(\"%d %d\\n\",column,dup[0]);\n\treturn fact(column)/dup_val;\n}\n\nint fact(int num){\n\tint i,val=1;\n\tfor(i=0;i<num;i++)val *= num-i;\n\t\n\treturn val;\n}\n\nint nest(int adds ,int frame ,int cand_num ,int *cand ,int *ID_puttern ,int *dup_data){\n\tint i,s,c;\n\tstatic int combi_phase[2] = {0};\n\tstatic int ID_select[10][2] = {0};\n\tstatic int duplication[10][2] = {0};\n c=0;\n //for(i=0;i<4;i++)dup_data[i] = 0;\n\twhile(c++<10000){\n\t\tif(ID_select[0][adds] >= cand_num){\n\t\t\tcombi_phase[adds] = 0;\n\t\t\tfor(i=0;i<8;i++)ID_select[i][adds] = 0;\n\t\t\treturn -1;\n\t\t}\n\t\t//printf(\"%d\\n\",combi_phase[adds]);\n\t\tif(ID_select[combi_phase[adds]][adds] < cand_num){\n\t\t\tduplication[ID_select[combi_phase[adds]][adds]][adds]++;\n\t\t\tID_puttern[combi_phase[adds]] = cand[ID_select[combi_phase[adds]][adds]];\n\t\t\tif(combi_phase[adds] != frame-1){\n\t\t\t\tID_select[combi_phase[adds]+1][adds] = 0;\n\t\t\t\t//ID_select[combi_phase[adds]+1][adds] = ID_select[combi_phase[adds]][adds];\n\t\t\t\tcombi_phase[adds]++;\n\t\t\t}else {\n\t\t\t\t//printf(\"%d[0] %d[1] %d[2] \",ID_select[0][adds],ID_select[1][adds],ID_select[2][adds]);\n\t\t\t\t/*for(i=0;i<frame;i++)printf(\"%d \",ID_puttern[i]);\n\t\t\t\tprintf(\"\\n\");*/\n\t\t\t\tfor(i=0,s=0;i<cand_num;i++){\n\t\t\t\t\tif(duplication[i][adds] > 1){\n\t\t\t\t\t\tdup_data[s++] = duplication[i][adds];\n\t\t\t\t\t\t//printf(\"%d \",duplication[i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t//printf(\"\\n%d\\n\",s);\n\t\t\t\tduplication[ID_select[combi_phase[adds]][adds]++][adds]--;\n\t\t\t\t//ID_select[combi_phase[adds]][adds]++;\n\t\t\t\t//duplication[ID_select[combi_phase[adds]][adds]++]++;\n\t\t\t\t//if(s>=frame)printf(\"%d\\n\",s);\n\t\t\t\treturn s;\n\t\t\t}\n\t\t}else{\n\t\t\tcombi_phase[adds]--;\n\t\t\tduplication[ID_select[combi_phase[adds]][adds]++][adds]--;\n\t\t\t//ID_select[combi_phase[adds]][adds]++;\n\t\t}\n\t}\n\treturn -2;\n}", "accuracy": 0.6363636363636364, "time_ms": 140, "memory_kb": 2688, "score_of_the_acc": -0.4194, "final_rank": 15 }, { "submission_id": "aoj_1500_4483146", "code_snippet": "#include <stdio.h>\nint combination(int ,int ,int*);\nint fact(int);\nint nest(int ,int ,int ,int* ,int* ,int*);\n\nint main(void) {\n int i;\n\tint n;\n\tchar s;\n\tint ID;\n\tint sum=0;\n\tint odd_num = 0;\n\tint even_num = 0;\n\tscanf(\"%d\",&n);\n\tscanf(\"%c\",&s);\n\tfor(i=0;i<n;i++){\n\t\tscanf(\"%c\",&s);\n\t if(s != '*')ID = s-'0';\n\t else ID = -1;\n\t\t//ID=(s!='*')?(int)(s-'0'):-1;\n\t\tif(ID>-1){\n\t\t\tif(i % 2){\n\t\t\t\tif(ID > 5)sum += ID*2-9;\n\t\t\t\telse sum += ID*2;\n\t\t\t}else sum += ID;\n\t\t}else if(i % 2)even_num++;\n\t\telse odd_num++;\n\t}//printf(\"%d\\n\",sum);\n\tint m;\n\tscanf(\"%d\",&m);\n\tint cand[10];\n\tfor(i=0;i<m;i++)scanf(\"%d\",&cand[i]);\n\t\n\t//printf(\"%d %d %d\\n\",odd_num,even_num,m);\n\tint odd_result;\n\tint even_result;\n\tint odd_sum=0;\n\tint even_sum=0;\n\tint consist_count = 0;\n\tint odd_ID_puttern[10];\n\tint even_ID_puttern[10];\n\tint dup_data[10] = {0};\n\t\n\twhile((odd_result = nest(0,odd_num,m,cand,odd_ID_puttern,dup_data))+1){\n\t\tfor(int i=0;i<odd_num;i++)odd_sum += odd_ID_puttern[i];\n\t\todd_result = combination(odd_num,odd_result,dup_data);//printf(\"%d\\n\",odd_result);\n\t\twhile((even_result = nest(1,even_num,m,cand,even_ID_puttern,dup_data))+1){\n\t\t\tfor(int j=0;j<even_num;j++)even_sum += (even_ID_puttern[j] < 5)?2*even_ID_puttern[j]:2*even_ID_puttern[j]-9;\n\t\t\teven_result = combination(even_num,even_result,dup_data);//printf(\"%d\\n\",even_result);\n\t\t\tif(!((sum+odd_sum+even_sum)%10))consist_count++;// += (odd_result * even_result);\n\t\t\teven_sum=0;\n\t\t}\n\t\todd_sum=0;\n\t}\n\tprintf(\"%d\\n\",consist_count);\n\treturn 0;\n}\n\nint combination(int column ,int dup_num ,int *dup){\n\tint i,dup_val = 1;\n\tfor(i=0;i<dup_num;i++)dup_val *= fact(dup[i]);\n\t//if(!(fact(column)/dup_val))printf(\"%d %d\\n\",column,dup[0]);\n\treturn fact(column)/dup_val;\n}\n\nint fact(int num){\n\tint i,val=1;\n\tfor(i=0;i<num;i++)val *= num-i;\n\t\n\treturn val;\n}\n\nint nest(int adds ,int frame ,int cand_num ,int *cand ,int *ID_puttern ,int *dup_data){\n\tint i,s,c;\n\tstatic int combi_phase[2] = {0};\n\tstatic int ID_select[10][2] = {0};\n\tstatic int duplication[10][2] = {0};\n c=0;\n //for(i=0;i<4;i++)dup_data[i] = 0;\n\twhile(c++<10000){\n\t\tif(ID_select[0][adds] >= cand_num){\n\t\t\tcombi_phase[adds] = 0;\n\t\t\tfor(i=0;i<8;i++)ID_select[i][adds] = 0;\n\t\t\treturn -1;\n\t\t}\n\t\t//printf(\"%d\\n\",combi_phase[adds]);\n\t\tif(ID_select[combi_phase[adds]][adds] < cand_num){\n\t\t\tduplication[ID_select[combi_phase[adds]][adds]][adds]++;\n\t\t\tID_puttern[combi_phase[adds]] = cand[ID_select[combi_phase[adds]][adds]];\n\t\t\tif(combi_phase[adds] != frame-1){\n\t\t\t\tID_select[combi_phase[adds]+1][adds] = 0;\n\t\t\t\t//ID_select[combi_phase[adds]+1][adds] = ID_select[combi_phase[adds]][adds];\n\t\t\t\tcombi_phase[adds]++;\n\t\t\t}else {\n\t\t\t\t//printf(\"%d[0] %d[1] %d[2] \",ID_select[0][adds],ID_select[1][adds],ID_select[2][adds]);\n\t\t\t\t/*for(i=0;i<frame;i++)printf(\"%d \",ID_puttern[i]);\n\t\t\t\tprintf(\"\\n\");*/\n\t\t\t\tfor(i=0,s=0;i<cand_num;i++){\n\t\t\t\t\tif(duplication[i][adds] > 1){\n\t\t\t\t\t\tdup_data[s++] = duplication[i][adds];\n\t\t\t\t\t\t//printf(\"%d \",duplication[i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t//printf(\"\\n%d\\n\",s);\n\t\t\t\tduplication[ID_select[combi_phase[adds]][adds]++][adds]--;\n\t\t\t\t//ID_select[combi_phase[adds]][adds]++;\n\t\t\t\t//duplication[ID_select[combi_phase[adds]][adds]++]++;\n\t\t\t\t//if(s>=frame)printf(\"%d\\n\",s);\n\t\t\t\treturn s;\n\t\t\t}\n\t\t}else{\n\t\t\tcombi_phase[adds]--;\n\t\t\tduplication[ID_select[combi_phase[adds]][adds]++][adds]--;\n\t\t\t//ID_select[combi_phase[adds]][adds]++;\n\t\t}\n\t}\n\treturn -2;\n}", "accuracy": 0.6363636363636364, "time_ms": 140, "memory_kb": 2736, "score_of_the_acc": -0.4204, "final_rank": 16 }, { "submission_id": "aoj_1500_4384062", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))\nusing ll = long long;\nusing namespace std;\n\n#define INF ((1<<30)-1)\n#define LLINF (1LL<<60)\n#define EPS (1e-10)\n\nint n;\nint m;\nstring id;\nint a[11];\n\nll ans = 0;\nll tmp = 0;\n\nvector<pair<ll, ll>> vvv;\n\nvoid solve(int k, ll sum) {\n if (k >= vvv.size()) {\n if ((sum + tmp) % 10 == 0) ans++;\n return;\n }\n\n int i = vvv[k].first;\n if (i%2) {\n // 2倍\n for (int j=0; j<m; ++j) {\n ll tmpv = a[j] * 2;\n while (tmpv >= 10) {\n tmpv = tmpv / 10 + tmpv % 10;\n }\n solve(k + 1, sum + tmpv);\n }\n }\n else {\n for (int j = 0; j < m; ++j) {\n ll tmpv = a[j];\n solve(k + 1, sum + tmpv);\n }\n }\n return;\n}\n\nint main() {\n cin >> n;\n cin >> id;\n cin >> m;\n rep(i, m) cin >> a[i];\n reverse(id.begin(), id.end());\n rep(i, id.size()) {\n if (id[i] == '*') {\n vvv.emplace_back(i, id[i]-'0');\n continue;\n }\n if (i%2) {\n ll k = (id[i] - '0') * 2;\n while (k >= 10) {\n k = k/10 + k%10;\n }\n tmp += k;\n }\n else {\n tmp += (id[i] - '0');\n }\n }\n //cerr << tmp << endl;\n solve(0, 0);\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3292, "score_of_the_acc": -0.0773, "final_rank": 3 }, { "submission_id": "aoj_1500_4115890", "code_snippet": "#include<iostream>\n#include<string.h>\n#include<vector>\n\nusing namespace std;\n\nint asterisk(int c, int g, int t, int s, int eAst, int* a, int m);\n\nint main(void) {\n \n int n;\n string id;\n int m;\n vector<int> a;\n\n cin >> n;\n cin >> id;\n cin >> m;\n for (int i=0;i<m;i++) {\n int t;\n cin >> t;\n a.push_back(t);\n }\n\n int sum = 0;\n int oAst = 0;\n int eAst = 0;\n for (int i=0;i<id.length();i++) {\n if (id[i] == '*') {\n if (i%2 == 0) {\n oAst++;\n } else {\n eAst++;\n }\n } else {\n if (i%2 == 0) {\n sum += (id[i] - '0');\n } else {\n if ((id[i] - '0') > 4) {\n sum += 1 + (id[i]-'0')+(id[i]-'0')-10;\n } else {\n sum += (id[i]-'0')+(id[i]-'0');\n }\n }\n }\n }\n\n int t = (10 - (sum % 10)) % 10;\n // cout << sum << endl;\n // cout << t << endl;\n cout << asterisk(1, eAst+oAst, t, 0, eAst, &a.front(), m) << endl;\n\n return 0;\n}\n\nint asterisk(int c, int g, int t, int s, int eAst, int* a, int m) {\n if (c == g) {\n int sum = 0;\n for (int i=0;i<m;i++) {\n // for (int o=0;o<c;o++) cout << \" \";\n // cout << a[i] << endl;\n if (c <= eAst) {\n if (a[i] > 4) {\n if ((s + 1 + a[i] + a[i]) % 10 == t) {\n sum++;\n // cout << \"catch!\" << endl;\n }\n } else {\n if ((s + a[i] + a[i]) % 10 == t) {\n sum++;\n // cout << \"catch!\" << endl;\n }\n }\n } else {\n if ((s + a[i]) % 10 == t) {\n sum++;\n // cout << \"catch!\" << endl;\n }\n }\n }\n return sum;\n } else {\n int sum = 0;\n for (int i=0;i<m;i++) {\n // for (int o=0;o<c;o++) cout << \" \";\n // cout << a[i] << endl;\n if (c <= eAst) {\n if (a[i] > 4) {\n sum += asterisk(c+1, g, t, (s+1+a[i]+a[i])%10, eAst, a, m);\n } else {\n sum += asterisk(c+1, g, t, (s+a[i]+a[i])%10, eAst, a, m);\n }\n } else {\n sum += asterisk(c+1, g, t, (s+a[i])%10, eAst, a, m);\n } \n }\n return sum;\n }\n}", "accuracy": 0.6363636363636364, "time_ms": 20, "memory_kb": 3324, "score_of_the_acc": -0.0457, "final_rank": 13 }, { "submission_id": "aoj_1500_3446662", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<cstdio>\n#include<cmath>\n#include<cctype>\n#include<math.h>\n#include<string>\n#include<string.h>\n#include<stack>\n#include<queue>\n#include<vector>\n#include<utility>\n#include<set>\n#include<map>\n#include<stdlib.h>\n#include<iomanip>\n\nusing namespace std;\n\n#define ll long long\n#define ld long double\n#define EPS 0.0000000001\n#define INF 1e9\n#define LINF (ll)INF*INF\n#define MOD 1000000007\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define loop(i,a,n) for(int i=a;i<(n);i++)\n#define all(in) in.begin(),in.end()\n#define shosu(x) fixed<<setprecision(x)\n\n#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int,int> pii;\ntypedef vector<pii> vp;\n\nint gcd(int a, int b){\n if(b==0) return a;\n return gcd(b,a%b);\n}\nint lcm(int a, int b){\n return a/gcd(a,b)*b;\n}\n\n\nsigned main(void) {\n int n;\n cin >> n;\n string s;\n cin >> s;\n reverse(all(s));\n int m;\n cin >> m;\n vi v(m);\n rep(i,m)cin >> v[i];\n vector<vi> dp(n+1,vi(10,0));\n //i桁目まで見て総和%10がjのときの通り数\n dp[0][0] = 1;\n rep(i,n)rep(j,10)if(dp[i][j]){\n //cout << i << \" \" << j << \" \" << dp[i][j] << endl;\n if(s[i] == '*'){\n rep(k,m){\n int ne = v[k];\n if(i%2) ne *= 2;\n ne = ne%10 + ne/10;\n //cout << \" \" << k << \" \" << j+ne << endl;\n dp[i+1][(j+ne)%10] += dp[i][j];\n }\n }else{\n int ne = s[i] - '0';\n if(i%2) ne *= 2;\n ne = ne%10 + ne/10;\n dp[i+1][(j+ne)%10] += dp[i][j];\n }\n }\n\n cout << dp[n][0] << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 14752, "score_of_the_acc": -0.2553, "final_rank": 7 }, { "submission_id": "aoj_1500_3215932", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\nint compress(int x){\n while(x > 9){\n int tmp = 0;\n while(x > 0) tmp += x % 10, x /= 10;\n x = tmp;\n }\n return x;\n}\n\nint n, m;\nstring s;\nvector<int> bias, use;\nint sum = 0, cnt = 0, ans = 0;\n\nvoid dfs(int pos, int accum){\n if(pos == cnt){\n if(accum % 10 == 0) ans++;\n return;\n }\n\n for(int i = 0; i < m; i++){\n dfs(pos+1, accum + compress(bias[pos] * use[i]));\n }\n}\n\nint main(){\n cin >> n >> s >> m;\n reverse(s.begin(), s.end());\n \n for(int i = 0; i < n; i++){\n if(s[i] == '*') bias.push_back(1 + i%2), cnt++;\n else sum += compress((1+i%2)*(s[i]-'0'));\n }\n\n int a;\n for(int i = 0; i < m; i++){\n cin >> a;\n use.push_back(a);\n }\n\n dfs(0, sum);\n\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3320, "score_of_the_acc": -0.1101, "final_rank": 6 }, { "submission_id": "aoj_1500_3215830", "code_snippet": "#include<iostream>\n#include<vector>\n#include<set>\nusing namespace std;\n\nint compress(int x){\n if(x > 9) x = x/10 + x%10;\n return x;\n}\n\nint n, m;\nstring id;\nvector<int> bias, use;\nint sum = 0, cnt = 0;\nset<int> s;\n\nvoid dfs(int pos, int accum, int pat){\n if(pos == cnt){\n if(accum % 10 == 0) s.insert(pat);\n return;\n }\n\n for(int i = 0; i < m; i++){\n dfs(pos+1, accum+compress(bias[pos]*use[i]), 10*pat+use[i]);\n }\n}\n\nint main(){\n cin >> n >> id >> m;\n \n for(int i = 0; i < n; i++){\n if(id[i] == '*') bias.push_back(1 + i%2), cnt++;\n else sum += compress((1+i%2)*(id[i]-'0'));\n }\n\n int a;\n for(int i = 0; i < m; i++){\n cin >> a;\n use.push_back(a);\n }\n\n dfs(0, sum, 0);\n\n cout << s.size() << endl;\n\n return 0;\n}", "accuracy": 0.6363636363636364, "time_ms": 300, "memory_kb": 49948, "score_of_the_acc": -1.9355, "final_rank": 19 }, { "submission_id": "aoj_1500_3215816", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n\nint compress(int x){\n while(x > 9){\n int tmp = 0;\n while(x > 0) tmp += x % 10, x /= 10;\n x = tmp;\n }\n return x;\n}\n\nint n, m;\nstring s;\nvector<int> bias, use;\nint sum = 0, cnt = 0, ans = 0;\n\nvoid dfs(int pos, int accum){\n if(pos == cnt){\n if(accum % 10 == 0) ans++;\n return;\n }\n\n for(int i = 0; i < m; i++){\n dfs(pos+1, accum + compress(bias[pos] * use[i]));\n }\n}\n\nint main(){\n cin >> n >> s >> m;\n \n for(int i = 0; i < n; i++){\n if(s[i] == '*') bias.push_back(1 + i%2), cnt++;\n else sum += compress((1+i%2)*(s[i]-'0'));\n }\n\n int a;\n for(int i = 0; i < m; i++){\n cin >> a;\n use.push_back(a);\n }\n\n dfs(0, sum);\n\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 0.6363636363636364, "time_ms": 40, "memory_kb": 3324, "score_of_the_acc": -0.1102, "final_rank": 14 }, { "submission_id": "aoj_1500_2727652", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvector<int> unknown;\nint sum;\nint m;\nint solve(int even, int odd){\n int res = sum;\n for(int i = 0; i < even; i++){\n int v = 2 * unknown[i];\n int adding = 0;\n if(v >= 10){\n adding += v%10;\n v /= 10;\n adding += v%10;\n res += adding;\n }else{\n res+=v;\n }\n\n res %= 10;\n }\n for(int i = even; i < odd+even; i++){\n res += unknown[i];\n res %= 10;\n }\n return res%10 == 0;\n}\n\nint main(void){\n int n;\n cin >> n;\n int odd = 0, even = 0;\n string str;\n cin >> str;\n\n sum = 0;\n\n for(int i = 1; i <= n; i++){\n char c = str[n-i];\n if(c == '*'){\n if(i % 2 == 0) even++;\n if(i % 2 == 1) odd++;\n }else{\n int v = c - '0';\n if(i % 2 == 1)\n sum += v;\n else{\n v *= 2;\n if(v >= 10){\n sum += v%10;\n v /= 10;\n sum += v%10;\n }else{\n sum += v;\n }\n }\n sum %= 10;\n }\n }\n\n unknown = vector<int>(even + odd);\n\n cin >> m;\n vector<int> a(m);\n for(int i = 0; i < m; i++) cin >> a[i];\n\n int cnt = even + odd;\n int ans = 0;\n\n\n for(int i1 = 0; i1 < m; i1++){\n unknown[0] = a[i1];\n if(cnt == 1){\n ans += solve( even, odd);\n continue;\n }\n for(int i2 = 0; i2 < m; i2++){\n unknown[1] = a[i2];\n if(cnt == 2){\n\n ans += solve( even, odd);\n continue;\n }\n for(int i3 = 0; i3 < m; i3++){\n unknown[2] = a[i3];\n if(cnt == 3){\n ans += solve( even, odd);\n continue;\n\n }\n for(int i4 = 0; i4 < m; i4++){\n unknown[3] = a[i4];\n if(cnt == 4){\n ans += solve( even, odd);\n continue;\n\n }\n for(int i5 = 0; i5 < m; i5++){\n unknown[4] = a[i5];\n if(cnt == 5){\n ans += solve( even, odd);\n continue;\n\n }\n for(int i6 = 0; i6 < m; i6++){\n unknown[5] = a[i6];\n if(cnt == 6){\n ans += solve( even, odd);\n continue;\n\n }\n for(int i7 = 0; i7 < m; i7++){\n unknown[6] = a[i7];\n ans += solve( even, odd);\n }\n }\n }\n }\n }\n }\n }\n\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3268, "score_of_the_acc": -0.6574, "final_rank": 12 }, { "submission_id": "aoj_1500_2727636", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define fs first\n#define sc second\n#define pb emplace_back\n#define mp make_pair\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n\nconst int32_t inf = 1001001001;\nconst int64_t infll = 1001001001001001001ll;\nconst int dx[] = {0, -1, 1, 0, -1, 1, -1, 1}, dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\ntemplate <typename T> using vector2d = vector<vector<T>>;\n\n// clang-format off\ntemplate <typename T> void sort(vector<T> &v) { sort(all(v)); }\n// ostream &operator<<(ostream &os, __int128_t value) { if (ostream::sentry(os)) { __uint128_t tmp = value < 0 ? -value : value; char buffer[64]; char *d = end(buffer); do { --d; *d = \"0123456789\"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; }\n// istream &operator>>(istream &is, __int128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } if (in[0] == '-') value *= -1; return is; }\n// ostream &operator<<(ostream &os, __uint128_t value) { if (ostream::sentry(os)) { char buffer[64]; char *d = end(buffer); do { --d; *d = \"0123456789\"[value % 10]; value /= 10; } while (value != 0); int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; }\n// istream &operator>>(istream &is, __uint128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } return is; }\ntemplate <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << v[0]; for (int i = 1; i < v.size(); ++i) os << \" \" << v[i]; return os; }\ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << p.fs << \" \" << p.sc; return os; }\ntemplate <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.fs >> p.sc; return is; }\n\nstruct IoSetup { IoSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup;\n\ninline int64_t in() { int64_t x = 0; cin >> x; return x; }\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n// clang-format on\n\nint n;\nstring ID;\nint m;\nvector<int> a, b;\nint64_t dp[133333][10];\n\nint64_t dfs(int idx, int sum) {\n if (idx == b.size()) {\n return sum == 0;\n }\n\n int64_t res = 0;\n for (int i = 0; i < a.size(); ++i) {\n int x = a[i];\n if (b[idx]) {\n x *= 2;\n x = x % 10 + x / 10;\n }\n res += dfs(idx + 1, (sum + x) % 10);\n }\n\n return res;\n}\n\nsigned main(int argc, char *argv[]) {\n memset(dp, -1, sizeof(dp));\n cin >> n >> ID >> m;\n a.resize(m);\n cin >> a;\n\n reverse(all(ID));\n int sum = 0;\n for (int i = 0; i < n; ++i) {\n if (ID[i] == '*') {\n b.pb(i & 1);\n continue;\n }\n int x = ID[i] - '0';\n if (i & 1) {\n x *= 2;\n x = x % 10 + x / 10;\n }\n sum += x;\n }\n sum %= 10;\n cout << dfs(0, sum) << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 13752, "score_of_the_acc": -0.3309, "final_rank": 8 }, { "submission_id": "aoj_1500_2727624", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint ans, d[11], n, m, num[11], cnt, S, c;\nstring s;\n\nbool check(){\n \n int sum=0;\n \n for(int i=0;i<cnt;i++){\n \n int di=d[i];\n \n if(i<c) di*=2;\n \n if(di>=10) di=di%10+di/10;\n\n sum+=di;\n }\n\n return (sum+S)%10==0;\n}\n\nvoid dfs(int x){\n \n if(x==cnt){\n \n if(check()) ans++;\n\n return;\n }\n \n for(int i=0;i<m;i++){\n d[x]=num[i];\n dfs(x+1);\n }\n \n}\n\nint main(){\n\n cin>>n;\n \n cin>>s;\n \n reverse(s.begin(), s.end());\n \n for(int i=0;i<n;i++)\n if(s[i]=='*'){\n cnt++;\n if(i%2) c++;\n }\n\n for(int i=0;i<n;i++){\n if(s[i]=='*') continue;\n int t=s[i]-'0';\n if(i%2) t*=2;\n if(t>=10) t=t%10+t/10;\n S+=t;\n } \n \n cin>>m;\n \n for(int i=0;i<m;i++) cin>>num[i];\n \n dfs(0);\n\n cout<<ans<<endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3304, "score_of_the_acc": -0.4001, "final_rank": 10 }, { "submission_id": "aoj_1500_2727612", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint ans, d[11], n, m, num[11], cnt;\nstring s;\n\nbool check(){\n \n int idx=0;\n \n int cp[11];\n\n for(int i=0;i<n;i++){\n if(s[i]=='*') cp[i]=d[idx++];\n else cp[i]=s[i]-'0';\n }\n \n int sum=0;\n \n for(int i=0;i<n;i++){\n if(i%2) cp[i]*=2;\n if(cp[i]>=10) cp[i]=cp[i]%10+cp[i]/10;\n sum+=cp[i];\n }\n\n return sum%10==0;\n}\n\nvoid dfs(int x){\n \n if(x==cnt){\n \n if(check()) ans++;\n\n return;\n }\n \n for(int i=0;i<m;i++){\n d[x]=num[i];\n dfs(x+1);\n }\n \n}\n\nint main(){\n\n cin>>n;\n \n cin>>s;\n \n reverse(s.begin(), s.end());\n\n for(int i=0;i<n;i++)\n if(s[i]=='*') cnt++;\n \n cin>>m;\n \n for(int i=0;i<m;i++) cin>>num[i];\n \n dfs(0);\n\n cout<<ans<<endl;\n \n return 0;\n}", "accuracy": 0.18181818181818182, "time_ms": 170, "memory_kb": 3192, "score_of_the_acc": -0.5268, "final_rank": 20 }, { "submission_id": "aoj_1500_2528629", "code_snippet": "#include <iostream>\n#include <cstdlib>\n\nusing namespace std;\n\nint n = 0;\nint even[10] = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};\nint edigit = 0;\nint odigit = 0;\nint sum = 0;\nchar ID[100001];\nint m = 0;\nint a[10];\nint answer = 0;\n\nint dfs(int depth, int _sum) {\n int count = 0;\n if (depth < edigit) {\n for (int i = 0; i < m; i++) count += dfs(depth+1, _sum+even[a[i]]);\n } else if (depth < edigit + odigit) {\n for (int i = 0; i < m; i++) count += dfs(depth+1, _sum+a[i]);\n } else if ((sum+_sum) % 10 == 0) count = 1;\n\n return count;\n}\n\nint main() {\n cin>>n;\n cin>>ID;\n for (int i = 0; i < n; i++) {\n char str[2] = {ID[i], '\\0'};\n if (ID[i] == '*') (n-i) % 2 == 0 ? edigit++: odigit++;\n else (n-i) % 2 == 0 ? sum += even[atoi(str)] : sum += atoi(str);\n }\n sum %= 10;\n cin>>m;\n for (int i = 0; i < m; i++) cin>>a[i];\n cout<<dfs(0, 0)<<endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3180, "score_of_the_acc": -0.0749, "final_rank": 2 }, { "submission_id": "aoj_1500_2303820", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\nusing namespace std;\nlong long cnt=0;\nvoid dfs(int sum,int md,int a[],int depth,vector<int> point,int m){\n if(depth==md){\n if(sum%10==0)\n cnt++;\n return;\n }\n for(int i=0;i<m;i++){\n if(point[depth]==0){\n if((a[i]*2)/10)\n\tdfs((sum+((a[i]*2)/10)+((a[i]*2)%10)),point.size(),a,depth+1,point,m);\n else\n\tdfs((sum+(a[i]*2)),point.size(),a,depth+1,point,m);\n }else\n dfs((sum+a[i]),point.size(),a,depth+1,point,m);\n }\n}\nint main(){\n int s;\n cin>>s;\n string str;\n cin>>str;\n int m;\n cin>>m;\n int a[m];\n for(int i=0;i<m;i++)\n cin>>a[i];\n int k=0;\n int sum=0;\n vector<int> point;\n for(int i=str.size()-1;i>=0;i--){\n if(str[i]=='*'){\n k++;\n point.push_back(k%2);\n }else{\n if((++k)%2==0){\n\tint num=(str[i]-'0')*2;\n\tif(num/10)\n\t sum+=((num/10)+(num%10));\n }else\n\tsum+=str[i]-'0';\n }\n }\n for(int i=0;i<m;i++){\n if(point[0]==0){\n if((a[i]*2)/10)\n\tdfs((sum+((a[i]*2)/10)+((a[i]*2)%10)),point.size(),a,1,point,m);\n else\n\tdfs((sum+(a[i]*2)),point.size(),a,1,point,m);\n }else\n dfs((sum+a[i]),point.size(),a,1,point,m);\n }\n cout<<cnt<<endl;\n return 0;\n}", "accuracy": 0.6363636363636364, "time_ms": 320, "memory_kb": 3196, "score_of_the_acc": -1.0107, "final_rank": 17 }, { "submission_id": "aoj_1500_2252460", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<string>\n#include<vector>\n#include<algorithm>\n#include<queue>\n#include<set>\n#include<unordered_map>\n#include<string.h>\nusing namespace std;\n\nvector<int>d;\nvector<int>e;\nint sum;\nint ans;\nint a;\nvoid saiki(int o, int b) {\n\tif (o == e.size()) {\n\t\tif (((sum + b) % 10) == 0) {\n\t\t\tans++;\n\t\t}\n\t\treturn;\n\t}\n\tif (e[o]) {//??????\n\t\tfor (int i = 0; i < d.size(); i++) {\n\t\t\tint K = d[i];\n\t\t\tK *= 2;\n\t\t\tif (K >= 10)K = K % 10 + 1;\n\t\t\tsaiki(o + 1, b + K);\n\t\t}\n\t}\n\telse{\n\t\tfor (int i = 0; i < d.size(); i++) {\n\t\t\tsaiki(o + 1, b + d[i]);\n\t\t}\n\t}\n}\nsigned main() {\n\tscanf(\"%d\", &a);\n\tstring b; cin >> b;\n\tint c; scanf(\"%d\", &c);\n\tfor (int e = 0; e < c; e++) {\n\t\tint f;scanf(\"%d\", &f);\n\t\td.push_back(f);\n\t}\n\treverse(b.begin(), b.end());\n\tfor (int i = 0; i < b.length(); i++) {\n\t\tif (b[i] == '*')e.push_back(i & 1);\n\t\telse {\n\t\t\tif (i & 1) {\n\t\t\t\tint f = b[i] - '0';\n\t\t\t\tf *= 2;\n\t\t\t\tif (f >= 10)f = f % 10 + 1;\n\t\t\t\tsum += f;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tsum += b[i] - '0';\n\t\t\t}\n\t\t}\n\t}\n\tsaiki(0, 0);\n\tprintf(\"%d\\n\", ans);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3400, "score_of_the_acc": -0.0796, "final_rank": 4 }, { "submission_id": "aoj_1500_2246416", "code_snippet": "#include<iostream>\n#include<string>\n#include<vector>\nusing namespace std;\nint cnt=0;\nvoid dfs(int sum,int md,int a[],int depth,vector<int> point,int m){\n if(depth==md){\n if(sum%10==0)\n cnt++;\n return;\n }\n for(int i=0;i<m;i++){\n if(point[depth]==0){\n if((a[i]*2)/10)\n\tdfs((sum+((a[i]*2)/10)+((a[i]*2)%10)),point.size(),a,depth+1,point,m);\n else\n\tdfs((sum+(a[i]*2)),point.size(),a,depth+1,point,m);\n }else\n dfs((sum+a[i]),point.size(),a,depth+1,point,m);\n }\n}\nint main(){\n int s;\n cin>>s;\n string str;\n cin>>str;\n int m;\n cin>>m;\n int a[m];\n for(int i=0;i<m;i++)\n cin>>a[i];\n int k=0;\n int sum=0;\n vector<int> point;\n for(int i=str.size()-1;i>=0;i--){\n if(str[i]=='*'){\n k++;\n point.push_back(k%2);\n }else{\n if((++k)%2==0){\n\tint num=(str[i]-'0')*2;\n\tif(num/10)\n\t sum+=((num/10)+(num%10));\n }else\n\tsum+=str[i]-'0';\n }\n }\n for(int i=0;i<m;i++){\n if(point[0]==0){\n if((a[i]*2)/10)\n\tdfs((sum+((a[i]*2)/10)+((a[i]*2)%10)),point.size(),a,1,point,m);\n else\n\tdfs((sum+(a[i]*2)),point.size(),a,1,point,m);\n }else\n dfs((sum+a[i]),point.size(),a,1,point,m);\n }\n cout<<cnt<<endl;\n return 0;\n}", "accuracy": 0.6363636363636364, "time_ms": 320, "memory_kb": 3280, "score_of_the_acc": -1.0125, "final_rank": 18 }, { "submission_id": "aoj_1500_2200672", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\nvector<vector<int> > dp;\nint n;\nint m;\nvector<bool> line;\nvector<int> chance;\nint search(int, int);\nint main() {\n\tcin >> n;\n\tint num = 0;\n\tchar du;\n\tline.resize(n);\n\tdp.resize(n);\n\tfor (int i = 0; i < n; ++i) {\n\t\tdp[i].resize(10);\n\t\tfor (int j = 0; j < 10; ++j) {\n\t\t\tdp[i][j] = -1;\n\t\t}\n\t}\n\tfor (int i = 0; i < n; ++i) {\n\t\tcin >> du;\n\t\tif (du == '*') {\n\t\t\tline[i] = true;\n\t\t}\n\t\telse {\n\t\t\tint ca = du - '0';\n\t\t\tline[i] = false;\n\t\t\tif ((n - i) % 2 == 0) {\n\t\t\t\tnum += (ca * 2) / 10 + (ca * 2) % 10;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnum += ca;\n\t\t\t}\n\t\t}\n\t}\n\tcin >> m;\n\tchance.resize(m);\n\tfor (int i = 0; i < m; ++i) {\n\t\tcin >> chance[i];\n\t}\n\tcout << search(0, num % 10) << endl;\n\treturn 0;\n}\nint search(int np, int nn) {\n\tif (np == n) {\n\t\tif (nn == 0)return 1;\n\t\telse return 0;\n\t}\n\tif (dp[np][nn] > -1)return dp[np][nn];\n\tint res = 0;\n\tif (line[np]) {\n\t\tfor (int i = 0; i < m; ++i) {\n\t\t\tif ((n - np) % 2 == 0) {\n\t\t\t\tres += search(np + 1, (nn + (chance[i] * 2) / 10 + (chance[i] * 2) % 10) % 10);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tres += search(np + 1, (nn + chance[i]) % 10);\n\t\t\t}\n\t\t}\n\t}\n\telse {\n\t\tres = search(np + 1, nn);\n\t}\n\tdp[np][nn] = res;\n\treturn res;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 17564, "score_of_the_acc": -0.347, "final_rank": 9 }, { "submission_id": "aoj_1500_2026634", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<n;i++)\nusing namespace std;\n\nint a[20], pos[10];\nint n, m, cnt = 0;\nint ans = 0, sum = 0;\n\nvoid dfs(int size, int Sum) {\n\tif (size == cnt) {\n\t\tif ((sum + Sum) % 10 == 0)ans++;\n\t\treturn;\n\t}\n\trep(i, m) {\n\t\tif (pos[size] % 2 == 1)\n\t\t\tdfs(size + 1, Sum + a[i] * 2 % 10 + a[i] * 2 / 10);\n\t\telse dfs(size + 1, Sum + a[i]);\n\t}\n}\nint main() {\n\tstring id;\n\tcin >> n >> id >> m;\n\trep(i, m)scanf(\"%d\", &a[i]);\n\trep(i, n) {\n\t\tif (id[i] == '*')pos[cnt++] = n - i - 1;\n\t\telse if ((n - i - 1) % 2 == 1)\n\t\t\tsum += (id[i] - '0') * 2 % 10 + (id[i] - '0') * 2 / 10;\n\t\telse sum += id[i] - '0';\n\t}\n\tdfs(0, 0);\n\tprintf(\"%d\\n\", ans);\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3456, "score_of_the_acc": -0.0808, "final_rank": 5 } ]
aoj_1505_cpp
Problem F : Dungeon (I) あなたはとあるゲームの開発に携わっている。 そのゲームはランダムに生成されたダンジョンをプレイヤーが探索するというものである。 このゲームで生成されるダンジョンには n 個の部屋が存在しており、0から n-1 までの番号が割り振られている。 部屋と部屋は通路で結ばれている。部屋と部屋を結ぶ通路は、 m 本ある。 通路はどちらの方向へも進むことができる。 また、部屋と部屋の間には距離が設定されている。 生成されたダンジョンではいくつかの通路を経由して、ある部屋から他のすべての部屋へ行くことが可能である。 そして、プレイヤーがゲームを行う際に、0番の部屋がスタート地点、 n-1 番の部屋がゴール地点として選ばれる。 ユーザーがダンジョンを探索する際に、手助けになるようなアイテムが入った宝箱を、どこかの部屋に設置したい。 その時に、スタート地点から遠すぎたり、ゴール地点から近すぎると意味がない。 そこで、スタートから距離 fs 以内で辿りつけて、ゴールに辿り着くために最短経路を通った場合に、少なくとも fg かかる部屋を宝箱を設置する候補としたい。 生成されたダンジョンと、 q が与えられる。 q 個のクエリーについて、宝箱を設置する場所の候補の数を数えて欲しい。 Input 入力は以下のフォーマットで与えられる。 n m a 1 b 1 c 1 . . . a m b m c m q fs 1 fg 1 . . . fs q fg q a i b i c i は 部屋 a i と b i を結ぶ通路の距離が c i であることを表す。 入力は以下の制約を満たす 2 ≤ n ≤ 100,000 0 ≤ a i , b i < n 0 ≤ c i ≤ 100,000 n-1 ≤ m ≤ 100,000 1 ≤ q ≤ 100,000 0 ≤ fs i , fg i ≤ 2 60 Output 各クエリーについて、答えの値を1行に出力せよ Sample Input 1 4 4 0 1 3 1 3 4 0 2 2 2 3 1 4 0 0 2 2 2 1 3 4 Sample Output 1 1 1 2 1
[ { "submission_id": "aoj_1505_10000615", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <queue>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,int> P;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define INF 10000000000000000\nint n,m;\nll start[100005];\nll goal[100005];\nll d[100005];\npair<ll,ll> qwe[100005];\nstruct edge{\n int to; ll cost;\n edge(int a,int b):to(a),cost(b){}\n};\nvector<edge>vec[100005];\nvoid dijkstra(int s){\n priority_queue<P,vector<P>,greater<P> > que;\n que.pu(mp(0,s));\n fill(d,d+n,INF);\n d[s]=0;\n while(!que.empty()){\n P p=que.top(); que.pop();\n int V=p.second;\n if(d[V]<p.first) continue;\n for(int i=0;i<vec[V].size();i++){\n edge e=vec[V][i];\n if(d[e.to] > d[V]+e.cost){\n d[e.to]=d[V]+e.cost;\n que.push(mp(d[e.to],e.to));\n }\n }\n }\n}\nvector<ll>seg[1<<18];\nll limits,limite;\nvoid update1(vector<ll> x,int a){\n seg[a+(1<<17)-1]=x;\n}\nvoid update(){\n for(int i=(1<<17)-2;i>=0;i--){\n seg[i].resize(seg[i*2+1].size()+seg[i*2+2].size());\n merge(seg[i*2+1].begin(),seg[i*2+1].end(),seg[i*2+2].begin(),seg[i*2+2].end(),seg[i].begin());\n }\n}\nint sum(int a,int b,int k,int l,int r){\n if(l>b || a>r){\n return 0;\n }else if(a<=l && r<=b){\n int rp=lower_bound(seg[k].begin(),seg[k].end(),limite)-seg[k].begin();\n return seg[k].size()-rp;\n }else{\n return sum(a,b,k*2+1,l,(l+r)/2)+sum(a,b,k*2+2,(l+r)/2+1,r);\n }\n}\nint main(){\n scanf(\"%d %d\",&n,&m);\n for(int i=0;i<m;i++){\n int a,b;\n ll c;\n scanf(\"%d %d %lld\",&a,&b,&c);\n vec[a].pb(edge(b,c)); vec[b].pb(edge(a,c));\n }\n dijkstra(0);\n for(int i=0;i<n;i++){\n start[i]=d[i];\n }\n dijkstra(n-1);\n for(int i=0;i<n;i++){\n goal[i]=d[i];\n qwe[i]=mp(start[i],goal[i]);\n }\n sort(qwe,qwe+n);\n for(int i=0;i<n;i++){\n vector<ll>er; er.pb(qwe[i].second);\n update1(er,i); start[i]=qwe[i].first;\n }\n update();\n int Q;\n scanf(\"%d\",&Q);\n for(int i=0;i<Q;i++){\n scanf(\"%lld %lld\",&limits,&limite);\n int eo=upper_bound(start,start+n,limits)-start;\n printf(\"%d\\n\",sum(0,eo-1,0,0,(1<<17)-1));\n }\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 37832, "score_of_the_acc": -0.4026, "final_rank": 6 }, { "submission_id": "aoj_1505_4166447", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <queue>\n#include <vector>\n\nusing namespace std;\n\ntypedef struct {\n int id;\n long long cost;\n} node;\n\nvector<long long> hoge(vector<vector<node>> &nodes, int n, int s) {\n vector<long long> cost(n, 1e15);\n auto compare = [](node &a, node &b) { return a.cost > b.cost; };\n priority_queue<node, vector<node>, decltype(compare)> queue{compare};\n vector<int> status(n, 0);\n\n cost[s] = 0;\n queue.push({s, 0});\n\n while (!queue.empty()) {\n int cur = queue.top().id;\n queue.pop();\n\n for (auto &nextnode : nodes[cur]) {\n long long cost_new = cost[cur] + nextnode.cost;\n if (cost[nextnode.id] <= cost_new)\n continue;\n cost[nextnode.id] = cost_new;\n queue.push({nextnode.id, cost[nextnode.id]});\n }\n }\n\n return cost;\n}\n\nstruct res {\n int id;\n long long a;\n long long b;\n};\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n int size = 1000;\n vector<vector<node>> nodes(n);\n for (int i = 0; i < m; ++i) {\n int a, b, c;\n scanf(\"%d %d %d\", &a, &b, &c);\n nodes[a].push_back({b, c});\n nodes[b].push_back({a, c});\n }\n\n vector<long long> v1 = hoge(nodes, n, 0);\n vector<long long> v2 = hoge(nodes, n, n - 1);\n\n vector<res> v3(n);\n for (int i = 0; i < n; ++i)\n v3[i] = {i, v1[i], v2[i]};\n\n sort(v3.begin(), v3.end(), [](res &a, res &b) { return (a.a != a.b) ? a.a < b.a : b.b < a.b; });\n\n int q, fs, fg;\n cin >> q;\n while (scanf(\"%d %d\", &fs, &fg) != EOF) {\n int cnt = 0;\n for (auto &v : v3) {\n if (fs < v.a)\n break;\n if (fg <= v.b)\n ++cnt;\n else\n continue;\n }\n printf(\"%d\\n\", cnt);\n }\n}", "accuracy": 0.28888888888888886, "time_ms": 30, "memory_kb": 12040, "score_of_the_acc": -0.0633, "final_rank": 18 }, { "submission_id": "aoj_1505_4166441", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <queue>\n#include <vector>\n\nusing namespace std;\n\ntypedef struct {\n int id;\n long long cost;\n} node;\n\nvector<long long> hoge(vector<vector<node>> &nodes, int n, int s) {\n vector<long long> cost(n, 1e15);\n auto compare = [](node &a, node &b) { return a.cost > b.cost; };\n priority_queue<node, vector<node>, decltype(compare)> queue{compare};\n vector<int> status(n, 0);\n\n cost[s] = 0;\n queue.push({s, 0});\n\n while (!queue.empty()) {\n int cur = queue.top().id;\n queue.pop();\n\n for (auto &nextnode : nodes[cur]) {\n long long cost_new = cost[cur] + nextnode.cost;\n if (cost[nextnode.id] <= cost_new)\n continue;\n cost[nextnode.id] = cost_new;\n queue.push({nextnode.id, cost[nextnode.id]});\n }\n }\n\n return cost;\n}\n\nstruct res {\n int id;\n long long a;\n long long b;\n};\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n int size = 1000;\n vector<vector<node>> nodes(n);\n for (int i = 0; i < m; ++i) {\n int a, b, c;\n scanf(\"%d %d %d\", &a, &b, &c);\n nodes[a].push_back({b, c});\n nodes[b].push_back({a, c});\n }\n\n vector<long long> v1 = hoge(nodes, n, 0);\n vector<long long> v2 = hoge(nodes, n, n - 1);\n\n vector<res> v3(n);\n for (int i = 0; i < n; ++i)\n v3[i] = {i, v1[i], v2[i]};\n\n sort(v3.begin(), v3.end(), [](res &a, res &b) { return (a.a != a.b) ? a.a < b.a : b.b < a.b; });\n\n int q, fs, fg;\n cin >> q;\n while (scanf(\"%d %d\", &fs, &fg) != EOF) {\n int cnt = 0;\n for (auto &v : v3) {\n if (fs < v.a)\n break;\n if (v.b < fg)\n continue;\n ++cnt;\n }\n printf(\"%d\\n\", cnt);\n }\n}", "accuracy": 0.28888888888888886, "time_ms": 30, "memory_kb": 12052, "score_of_the_acc": -0.0635, "final_rank": 19 }, { "submission_id": "aoj_1505_4166261", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <queue>\n#include <vector>\n\nusing namespace std;\n\nvector<int> hoge(vector<vector<int>> &matx, vector<vector<int>> &nodes, int s) {\n int n = matx.size();\n vector<int> cost(n, 1e7);\n priority_queue<int> queue;\n int cur = s;\n vector<int> status(n, 0);\n\n cost[cur] = 0;\n queue.push(cur);\n\n while (!queue.empty()) {\n cur = queue.top();\n queue.pop();\n\n for (auto &next : nodes[cur]) {\n int cost_new = cost[cur] + matx[cur][next];\n if (cost[next] <= cost_new)\n continue;\n cost[next] = cost_new;\n queue.push(next);\n }\n }\n\n return cost;\n}\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n int size = 1000;\n vector<vector<int>> matx(n, vector<int>(n, 1e7));\n vector<vector<int>> nodes(n);\n for (int i = 0; i < m; ++i) {\n int a, b, c;\n cin >> a >> b >> c;\n matx[a][b] = matx[b][a] = c;\n nodes[a].push_back(b);\n nodes[b].push_back(a);\n }\n\n vector<int> v1 = hoge(matx, nodes, 0);\n vector<int> v2 = hoge(matx, nodes, n-1);\n\n int q, fs, fg;\n cin >> q;\n while (cin >> fs >> fg) {\n int cnt = 0;\n for (int i = 0; i < n; ++i)\n if (v1[i] <= fs && fg <= v2[i])\n ++cnt;\n cout << cnt << endl;\n }\n}", "accuracy": 0.8222222222222222, "time_ms": 200, "memory_kb": 9348, "score_of_the_acc": -0.1753, "final_rank": 17 }, { "submission_id": "aoj_1505_4166249", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <queue>\n#include <vector>\n\nusing namespace std;\n\nvector<int> hoge(vector<vector<int>> &matx, vector<vector<int>> &nodes, int s) {\n int n = matx.size();\n vector<int> cost(n, 1e7);\n priority_queue<int> queue;\n int cur = s;\n vector<int> status(n, 0);\n\n cost[cur] = 0;\n queue.push(cur);\n\n while (!queue.empty()) {\n cur = queue.top();\n queue.pop();\n if (status[cur] == 1)\n continue;\n status[cur] = 1;\n\n for (auto &next : nodes[cur]) {\n int cost_new = cost[cur] + matx[cur][next];\n if (cost[next] <= cost_new)\n continue;\n cost[next] = cost_new;\n queue.push(next);\n }\n }\n\n return cost;\n}\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n int size = 1000;\n vector<vector<int>> matx(n, vector<int>(n, 1e7));\n vector<vector<int>> nodes(n);\n for (int i = 0; i < m; ++i) {\n int a, b, c;\n cin >> a >> b >> c;\n matx[a][b] = matx[b][a] = c;\n nodes[a].push_back(b);\n nodes[b].push_back(a);\n }\n\n int q, fs, fg;\n cin >> q;\n while (cin >> fs >> fg) {\n int cnt = 0;\n vector<int> v1 = hoge(matx, nodes, 0);\n vector<int> v2 = hoge(matx, nodes, n-1);\n for (int i = 0; i < n; ++i)\n if (v1[i] <= fs && fg <= v2[i])\n ++cnt;\n cout << cnt << endl;\n }\n}", "accuracy": 0.06666666666666667, "time_ms": 100, "memory_kb": 4620, "score_of_the_acc": -0.0556, "final_rank": 20 }, { "submission_id": "aoj_1505_3654736", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\n#include <queue>\n#include <algorithm>\n#include <unordered_map>\n\nusing namespace std;\n\nint main() {\n int64_t n, m;\n int64_t a, b, c;\n int64_t q;\n int64_t fs, fg;\n int64_t inf = 10000000000000;\n int64_t chunk_size = 512;\n cin >> n >> m;\n vector<unordered_map<int64_t, int64_t>> edges(n);\n for (int i = 0; i < m; ++i) {\n cin >> a >> b >> c;\n if (edges[a].find(b) != edges[a].end()) {\n if (c < edges[a][b]) {\n edges[a].erase(b);\n edges[b].erase(a);\n }\n }\n edges[a].emplace(b, c);\n edges[b].emplace(a, c);\n }\n vector<pair<int64_t, int64_t>> costs(n);\n for (int i = 0; i < n; ++i) {\n costs[i].first = inf;\n costs[i].second = -1;\n }\n priority_queue<pair<int64_t, int64_t>> pq;\n pq.emplace(0, 0);\n while (!pq.empty()) {\n int64_t cost = -pq.top().first;\n int64_t id = pq.top().second;\n pq.pop();\n if (costs[id].first != inf) {\n continue;\n }\n costs[id].first = cost;\n for (auto& edge : edges[id]) {\n if (costs[edge.first].first != inf) {\n continue;\n }\n pq.emplace(-(cost + edge.second), edge.first);\n }\n }\n pq.emplace(0, n - 1);\n while (!pq.empty()) {\n int64_t cost = -pq.top().first;\n int64_t id = pq.top().second;\n pq.pop();\n if (costs[id].second != -1) {\n continue;\n }\n costs[id].second = cost;\n for (auto& edge : edges[id]) {\n if (costs[edge.first].second != -1) {\n continue;\n }\n pq.emplace(-(cost + edge.second), edge.first);\n }\n }\n sort(costs.begin(), costs.end());\n vector<vector<int64_t>> chunked_cost;\n for (int i = 0; i < n; i += chunk_size) {\n vector<int64_t> chunk(chunk_size, -1);\n for (int j = i; j < min(i + chunk_size, n); ++j) {\n chunk[j - i] = costs[j].second;\n }\n sort(chunk.begin(), chunk.end());\n chunked_cost.push_back(chunk);\n }\n cin >> q;\n for (int i = 0; i < q; ++i) {\n cin >> fs >> fg;\n int cnt = 0;\n int nmin_fs = upper_bound(costs.begin(), costs.end(), pair<int64_t, int64_t> {fs, inf}) - costs.begin();\n for (int j = 0; j < nmin_fs / chunk_size; j++) {\n cnt += chunked_cost[j].end() - lower_bound(chunked_cost[j].begin(), chunked_cost[j].end(), fg);\n }\n for (int j = nmin_fs / chunk_size * chunk_size; j < nmin_fs; ++j) {\n if (costs[j].second >= fg) {\n ++cnt;\n }\n }\n cout << cnt << endl;\n }\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 23692, "score_of_the_acc": -0.5914, "final_rank": 9 }, { "submission_id": "aoj_1505_3653006", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\n#include <queue>\n#include <algorithm>\n#include <unordered_map>\n\nusing namespace std;\n\nint main() {\n int n, m;\n long long a, b, c;\n int q;\n long long fs, fg;\n cin >> n >> m;\n vector<unordered_map<long long, long long>> dists(n);\n for (int i = 0; i < m; ++i) {\n cin >> a >> b >> c;\n dists[a].emplace(b, c);\n dists[b].emplace(a, c);\n }\n vector<pair<long long, long long>> costs(n);\n for (int i = 0; i < n; ++i) {\n costs[i].first = 10000000001ll;\n costs[i].second = -1;\n }\n auto comp = [](const pair<long long, int>& a, const pair<long long, int>& b) { return a.first > b.first; };\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, decltype(comp)> pq(comp);\n pq.emplace(0, 0);\n while (!pq.empty()) {\n auto item = pq.top();\n pq.pop();\n if (costs[item.second].first != 10000000001ll) {\n continue;\n }\n costs[item.second].first = item.first;\n for (auto& edge : dists[item.second]) {\n pq.emplace(item.first + edge.second, edge.first);\n }\n }\n pq.emplace(0, n - 1);\n while (!pq.empty()) {\n auto item = pq.top();\n pq.pop();\n if (costs[item.second].second != -1) {\n continue;\n }\n costs[item.second].second = item.first;\n for (auto& edge : dists[item.second]) {\n pq.emplace(item.first + edge.second, edge.first);\n }\n }\n unordered_map<long long, int> db_map;\n vector<pair<long long, vector<long long>>> db;\n for (auto& cost : costs) {\n if (db_map.find(cost.first) == db_map.end()) {\n db_map.emplace(cost.first, db.size());\n db.emplace_back();\n db.back().first = cost.first;\n }\n db[db_map[cost.first]].second.emplace_back(cost.second);\n }\n sort(db.begin(), db.end(), [](const pair<long long, vector<long long>>& a, const pair<long long, vector<long long>>& b) { return a.first < b.first; });\n for (auto& db_child : db) {\n sort(db_child.second.begin(), db_child.second.end());\n }\n vector<int> results;\n cin >> q;\n for (int i = 0; i < q; ++i) {\n cin >> fs >> fg;\n auto fsit = upper_bound(db.begin(), db.end(), pair<long long, vector<long long>> {fs, {}}, [](const pair<long long, vector<long long>>& a, const pair<long long, vector<long long>>& b) { return a.first < b.first; });\n int cnt = 0;\n for (auto it = db.begin(); it != fsit; ++it) {\n auto fgit = lower_bound(it->second.begin(), it->second.end(), fg);\n cnt += it->second.end() - fgit;\n }\n results.emplace_back(cnt);\n }\n for (auto c : results) {\n cout << c << endl;\n }\n return 0;\n}", "accuracy": 0.8666666666666667, "time_ms": 1220, "memory_kb": 27976, "score_of_the_acc": -1.1439, "final_rank": 14 }, { "submission_id": "aoj_1505_3652728", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\n#include <queue>\n#include <algorithm>\n#include <unordered_map>\n\nusing namespace std;\n\nint main() {\n int n, m, a, b, c;\n int q, fs, fg;\n cin >> n >> m;\n unordered_map<int, unordered_map<int, int>> dist;\n vector<pair<int, int>> fdist(n);\n for (int i = 0; i < n; ++i) {\n fdist[i].first = -1;\n fdist[i].second = -1;\n }\n for (int i = 0; i < m; ++i) {\n cin >> a >> b >> c;\n if (dist.find(a) == dist.end()) {\n dist.emplace(a, unordered_map<int, int>{});\n }\n dist[a].emplace(b, c);\n if (dist.find(b) == dist.end()) {\n dist.emplace(b, unordered_map<int, int>{});\n }\n dist[b].emplace(a, c);\n }\n auto comp = [](const pair<int, int> &l, const pair<int, int> &r) { return l.first > r.first; };\n priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)> pq(comp);\n pq.emplace(0, 0);\n while (!pq.empty()) {\n pair<int, int> item = pq.top();\n pq.pop();\n if (fdist[item.second].first != -1) {\n continue;\n }\n fdist[item.second].first = item.first;\n for (auto x : dist[item.second]) {\n pq.emplace(item.first + x.second, x.first);\n }\n }\n pq.emplace(0, n - 1);\n while (!pq.empty()) {\n pair<int, int> item = pq.top();\n pq.pop();\n if (fdist[item.second].second != -1) {\n continue;\n }\n fdist[item.second].second = item.first;\n for (auto x : dist[item.second]) {\n pq.emplace(item.first + x.second, x.first);\n }\n }\n unordered_map<int, int> distmap_warp;\n vector<pair<int, vector<int>>> distmap;\n for (int i = 0; i < n; ++i) {\n if (distmap_warp.find(fdist[i].first) == distmap_warp.end()) {\n distmap_warp.emplace(fdist[i].first, distmap.size());\n distmap.emplace_back();\n }\n distmap[distmap_warp[fdist[i].first]].first = fdist[i].first;\n distmap[distmap_warp[fdist[i].first]].second.emplace_back(fdist[i].second);\n }\n sort(distmap.begin(), distmap.end(), [](const pair<int, vector<int>>& a, const pair<int, vector<int>>& b) { return a.first < b.first; });\n for (auto& item : distmap) {\n sort(item.second.begin(), item.second.end());\n }\n cin >> q;\n vector<int> results;\n for (int i = 0; i < q; ++i) {\n cin >> fs >> fg;\n int cnt = 0;\n auto fsit = upper_bound(distmap.begin(), distmap.end(), pair<int, vector<int>>{fs, {}}, [](const pair<int, vector<int>>& a, const pair<int, vector<int>>& b) { return a.first < b.first; });\n for (auto it = distmap.begin(); it != fsit; ++it) {\n auto fgit = lower_bound(it->second.begin(), it->second.end(), fg);\n cnt += it->second.end() - fgit;\n }\n results.emplace_back(cnt);\n }\n for (int result : results) {\n cout << result << endl;\n }\n return 0;\n}", "accuracy": 0.8666666666666667, "time_ms": 1280, "memory_kb": 23800, "score_of_the_acc": -1.1558, "final_rank": 15 }, { "submission_id": "aoj_1505_3651222", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\n#include <queue>\n#include <algorithm>\n#include <unordered_map>\n\nusing namespace std;\n\nint main() {\n int n, m, a, b, c;\n int q, fs, fg;\n cin >> n >> m;\n unordered_map<int, unordered_map<int, int>> dist;\n vector<pair<int, int>> fdist(n);\n for (int i = 0; i < n; ++i) {\n fdist[i].first = -1;\n fdist[i].second = -1;\n }\n for (int i = 0; i < m; ++i) {\n cin >> a >> b >> c;\n if (dist.find(a) == dist.end()) {\n dist.emplace(a, unordered_map<int, int>{});\n }\n dist[a].emplace(b, c);\n if (dist.find(b) == dist.end()) {\n dist.emplace(b, unordered_map<int, int>{});\n }\n dist[b].emplace(a, c);\n }\n auto comp = [](const pair<int, int> &l, const pair<int, int> &r) { return l.first > r.first; };\n priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)> pq(comp);\n pq.emplace(0, 0);\n while (!pq.empty()) {\n pair<int, int> item = pq.top();\n pq.pop();\n if (fdist[item.second].first != -1) {\n continue;\n }\n fdist[item.second].first = item.first;\n for (auto x : dist[item.second]) {\n pq.emplace(item.first + x.second, x.first);\n }\n }\n pq.emplace(0, n - 1);\n while (!pq.empty()) {\n pair<int, int> item = pq.top();\n pq.pop();\n if (fdist[item.second].second != -1) {\n continue;\n }\n fdist[item.second].second = item.first;\n for (auto x : dist[item.second]) {\n pq.emplace(item.first + x.second, x.first);\n }\n }\n unordered_map<int, int> distmap_warp;\n vector<pair<int, vector<int>>> distmap;\n for (int i = 0; i < n; ++i) {\n if (distmap_warp.find(fdist[i].first) == distmap_warp.end()) {\n distmap_warp.emplace(fdist[i].first, distmap.size());\n distmap.emplace_back();\n }\n distmap[distmap_warp[fdist[i].first]].first = fdist[i].first;\n distmap[distmap_warp[fdist[i].first]].second.emplace_back(fdist[i].second);\n }\n sort(distmap.begin(), distmap.end(), [](const pair<int, vector<int>>& a, const pair<int, vector<int>>& b) { return a.first < b.first; });\n for (auto& item : distmap) {\n sort(item.second.begin(), item.second.end());\n }\n cin >> q;\n for (int i = 0; i < q; ++i) {\n cin >> fs >> fg;\n int cnt = 0;\n auto fsit = upper_bound(distmap.begin(), distmap.end(), pair<int, vector<int>>{fs, {}}, [](const pair<int, vector<int>>& a, const pair<int, vector<int>>& b) { return a.first < b.first; });\n for (auto it = distmap.begin(); it != fsit; ++it) {\n auto fgit = lower_bound(it->second.begin(), it->second.end(), fg);\n cnt += it->second.end() - fgit;\n }\n cout << cnt << endl;\n }\n return 0;\n}", "accuracy": 0.8666666666666667, "time_ms": 1290, "memory_kb": 23776, "score_of_the_acc": -1.1635, "final_rank": 16 }, { "submission_id": "aoj_1505_3585165", "code_snippet": "// includes\n#include <bits/stdc++.h>\n\n// macros\n#define ll long long int\n#define pb emplace_back\n#define mk make_pair\n#define pq priority_queue\n#define FOR(i, a, b) for(int i=(a);i<(b);++i)\n#define rep(i, n) FOR(i, 0, n)\n#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)\n#define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr)\n#define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr)\n#define vrep(v, i) for(int i = 0; i < (v).size(); i++)\n#define all(x) (x).begin(),(x).end()\n#define sz(x) ((int)(x).size())\n#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())\n#define FI first\n#define SE second\n#define dump(a, n) for(int i = 0; i < n; i++)cout << a[i] << \"\\n \"[i + 1 != n];\n#define dump2(a, n, m) for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)cout << a[i][j] << \"\\n \"[j + 1 != m];\n#define bit(n) (1LL<<(n))\n#define INT(n) int n; cin >> n;\n#define LL(n) ll n; cin >> n;\n#define DOUBLE(n) double n; cin >> n;\nusing namespace std;\n\n// types\ntypedef pair<int, int> P;\ntypedef pair<ll, int> Pl;\ntypedef pair<ll, ll> Pll;\ntypedef pair<double, double> Pd;\ntypedef complex<double> cd;\n \n// constants\nconst int inf = 1e9;\nconst ll linf = 1LL << 50;\nconst double EPS = 1e-10;\nconst int mod = 1e9 + 7;\nconst int dx[4] = {-1, 0, 1, 0};\nconst int dy[4] = {0, -1, 0, 1};\n\n// solve\ntemplate <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}\ntemplate <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}\ntemplate <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << \" \";} return os;}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << \" \";} return os;}\ntemplate <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << \" \";} return os;}\ntemplate <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << \" \";} return os;}\ntemplate <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << \" \";} return os;}\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << p.first << \" \" << p.second; return os;}\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << itr->first << \":\" << itr->second; auto titr = itr; if(++titr != mp.end())os << \" \"; } return os;}\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << itr->first << \":\" << itr->second; auto titr = itr; if(++titr != mp.end())os << \" \"; } return os;}\n\nint ato_int(char c){\n return int(c - 'a');\n}\n\nchar to_achar(int i){\n return char(i + 'a');\n}\n\nint Ato_int(char c){\n if('a' <= c && c <= 'z')return int(c - 'a');\n return int(c - 'A') + 26;\n}\n\nchar to_Achar(int i){\n if(i < 26)return char(i + 'a');\n return char(i - 26 + 'A');\n}\n\nint dto_int(char c){\n return int(c - '0');\n}\n\nchar to_dchar(int i){\n return char(i + '0');\n}\n\ntypedef struct SuccinctBitVector_ {\n int size;\n int block = 32;\n int l = 256;\n vector<uint32_t> B;\n vector<unsigned> L, S;\n SuccinctBitVector_(){}\n SuccinctBitVector_(int size_){\n size = size_;\n B.assign((size + block - 1) / block, 0U);\n L.assign((size + l - 1) / l, 0U);\n S.assign((size + block - 1) / block, 0U);\n }\n void set_bit(int at){\n B[at / block] |= (1U << (at % block));\n }\n void build(){\n int true_count = 0;\n for(int i = 0; i < ((size + block - 1) / block) * block; i+=block){\n if(i % l == 0)L[i / l] = true_count;\n S[i / block] = true_count - L[i / l];\n true_count += __builtin_popcount(B[i / block]);\n }\n }\n bool access(int at){\n return (B[at / block] >> (at % block)) & 1U;\n }\n bool operator[](int at){\n return access(at);\n }\n // this is rank1\n int rank(int at){\n return L[at / l] + S[at / block] + __builtin_popcount((B[at / block] & ((1U << (at % block)) - 1)));\n }\n int rank(bool val, int at){\n return (val ? rank(at): at - rank(at));\n }\n int select(bool val, int x){\n if(x < 0 || x >= rank(val, size))return -1;\n int ld = 0, rd = size;\n while(rd - ld > 1){\n int md = (rd + ld) / 2;\n if(rank(val, md) >= x + 1)rd = md;\n else ld = md;\n }\n return rd - 1;\n }\n int select(int x){\n return select(1, x);\n }\n int select(bool val, int x, int l){\n return select(val, x + rank(val, l));\n }\n} SuccinctBitVector;\n\nvector<int> ato_vector(const string &s){\n vector<int> v(s.size());\n for(int i = 0; i < s.size(); i++){\n v[i] = ato_int(s[i]);\n }\n return v;\n}\n\ntemplate <typename T>\nstruct WaveletMatrix{\n int n = 0;\n int len = 0;\n vector<SuccinctBitVector> mat;\n vector<int> zc;\n vector<int> bl, br;\n vector<vector<T>> sum;\n WaveletMatrix(ll m, vector<T> s){\n len = s.size();\n while((1LL<< n) <= m)n++;\n mat.resize(n);\n zc.resize(n);\n bl.resize(n);\n br.resize(n);\n sum.resize(n + 1, vector<T>(len + 1, 0));\n\n for(int i = 0; i < len; i++)sum[0][i + 1] = sum[0][i] + s[i];\n\n vector<T> l(len), r(len);\n for(int i = 0; i < n; i++){\n mat[i] = SuccinctBitVector(len + 1);\n int li = 0, ri = 0;\n for(int j = 0; j < len; j++){\n if((s[j] >> (n - i - 1)) & 1){\n r[ri++] = s[j];\n mat[i].set_bit(j);\n }else{\n l[li++] = s[j];\n }\n }\n zc[i] = li;\n mat[i].build();\n swap(l, s);\n for(int j = 0; j < ri; j++)s[li + j] = r[j];\n for(int j = 0; j < len; j++)sum[i+1][j+1] = sum[i+1][j] + s[j];\n }\n }\n T access(int i){\n int res = 0;\n for(int j = 0; j < n; j++){\n bool bit = mat[j][i];\n res = (res << 1) | bit;\n i = zc[j] * bit + mat[j].rank(bit, i);\n }\n return res;\n }\n T operator[](int i){\n return access(i);\n }\n int rank(T val, int i){\n int l = 0, r = i;\n for(int j = 0; j < n; j++){\n bl[j] = l, br[j] = r;\n bool bit = (val >> (n - j - 1)) & 1;\n l = zc[j] * bit + mat[j].rank(bit, l);\n r = zc[j] * bit + mat[j].rank(bit, r);\n }\n return r - l;\n }\n int select(T val, int i){\n rank(val, len);\n for(int j = n - 1; j >= 0; j--){\n bool bit = (val >> (n - j - 1)) & 1;\n i = mat[j].select(bit, i, bl[j]);\n if(i >= br[j] || i < 0)return -1;\n i -= bl[j];\n }\n return i;\n }\n int select(T val, int i, int l){\n return select(val, i + rank(val, l));\n }\n T quantile(int s, int e, int k){\n if(e - s <= k || k < 0)return -1;\n T res = 0;\n for(int i = 0; i < n; i++){\n int l = mat[i].rank(1, s);\n int r = mat[i].rank(1, e);\n if(r - l > k){\n s = zc[i] + l;\n e = zc[i] + r;\n res = res | (1LL << (n - i - 1));\n }else{\n k -= (r - l);\n s -= l;\n e -= r;\n }\n }\n return res;\n }\n // equal, lt, gt\n tuple<int, int, int> rankall(T x, int s, int e){\n if(s >= e)return make_tuple(0, 0, 0);\n int rank_lt = 0, rank_gt = 0;\n for(int i = 0; i < n && s < e; i++){\n bool bit = (x >> (n - i - 1)) & 1;\n int s0 = mat[i].rank(0, s);\n int s1 = s - s0;\n int e0 = mat[i].rank(0, e);\n int e1 = e - e0;\n if(bit){\n rank_lt += e0 - s0;\n s = zc[i] + s1;\n e = zc[i] + e1;\n }else{\n rank_gt += e1 - s1;\n s = s0;\n e = e0;\n }\n }\n return make_tuple(e - s - rank_lt - rank_gt, rank_lt, rank_gt);\n }\n int rangefreq(int s, int e, T mini, T maxi){\n tuple<int, int, int> maxi_t = rankall(maxi, s, e);\n tuple<int, int, int> mini_t = rankall(mini, s, e);\n return get<1>(maxi_t) - get<1>(mini_t);\n }\n int ranklt(T x, int s, int e){\n return get<1>(rankall(x, s, e));\n }\n int rankgt(T x, int s, int e){\n return get<2>(rankall(x, s, e));\n }\n T rangemax(int s, int e){\n return quantile(s, e, 0);\n }\n T rangemin(int s, int e){\n return quantile(s, e, e - s - 1);\n }\n vector<pair<T, int>> topk(int s, int e, int k){\n vector<pair<T, int>> res;\n using v_t = tuple<int, int, int, int, T>;\n auto comp = [](const v_t &a, const v_t &b){\n if(get<0>(a) != get<0>(b))return get<0>(a) < get<0>(b);\n if(get<3>(a) != get<3>(b))return get<3>(a) > get<3>(b);\n return get<3>(a) > get<3>(b);\n };\n priority_queue<v_t, vector<v_t>, decltype(comp)> pq(comp);\n pq.push(make_tuple(e - s, s, e, 0, 0));\n while(!pq.empty()){\n auto p = pq.top(); pq.pop();\n int width, li, ri, dep;\n T val;\n tie(width, li, ri, dep, val) = p;\n if(dep >= n){\n res.emplace_back(make_pair(val, ri - li));\n if(res.size() >= k)break;\n continue;\n }\n int l0 = mat[dep].rank(0, li);\n int r0 = mat[dep].rank(0, ri);\n if(l0 < r0)pq.push(make_tuple(r0 - l0, l0, r0, dep + 1, val));\n int l1 = zc[dep] + mat[dep].rank(1, li);\n int r1 = zc[dep] + mat[dep].rank(1, ri);\n if(l1 < r1)pq.push(make_tuple(r1 - l1, l1, r1, dep + 1, val | (1LL << (n - dep - 1))));\n }\n return res;\n }\n T rangesum(int s, int e, int depth, T val, T x, T y){\n if(s == e)return 0;\n if(depth == n){\n if(x <= val && val < y)return val * (e - s);\n return 0;\n }\n T nv = (1LL << (n - depth - 1)) | val;\n T nnv = ((1LL << (n - depth - 1)) - 1) | nv;\n if(nnv < x || y <= val)return 0;\n if(x <= val && nnv < y)return sum[depth][e] - sum[depth][s];\n int s0 = mat[depth].rank(0, s);\n int s1 = s - s0;\n int e0 = mat[depth].rank(0, e);\n int e1 = e - e0;\n return rangesum(s0, e0, depth + 1, val, x, y) + rangesum(zc[depth] + s1, zc[depth] + e1, depth + 1, nv, x, y);\n }\n T rangesum(int s, int e, T x, T y){\n return rangesum(s, e, 0, 0, x, y);\n }\n T prev(int s, int e, T x, T y){\n y--;\n using v_t = tuple<int, int, int, T, bool>;\n stack<v_t> st;\n st.push(make_tuple(s, e, 0, 0, true));\n while(!st.empty()){\n auto p = st.top(); st.pop();\n int li, ri, depth;\n T val;\n bool tight;\n tie(li, ri, depth, val, tight) = p;\n\n if(depth == n){\n if(val >= x)return val;\n continue;\n }\n\n bool bit = (y >> (n - depth - 1)) & 1;\n int l0 = mat[depth].rank(0, li);\n int l1 = li - l0;\n int r0 = mat[depth].rank(0, ri);\n int r1 = ri - r0;\n if(l0 != r0){\n st.push(make_tuple(l0, r0, depth + 1, (val << 1), tight && !bit));\n }\n if(l1 != r1){\n if(!tight || bit){\n st.push(make_tuple(zc[depth] + l1, zc[depth] + r1, depth + 1, ((val<<1)|1), tight));\n }\n }\n }\n return -1;\n }\n T next(int s, int e, T x, T y){\n using v_t = tuple<int, int, int, T, bool>;\n stack<v_t> st;\n st.push(make_tuple(s, e, 0, 0, true));\n while(!st.empty()){\n auto p = st.top(); st.pop();\n int li, ri, depth;\n T val;\n bool tight;\n tie(li, ri, depth, val, tight) = p;\n if(depth == n){\n if(val < y)return val;\n continue;\n }\n\n bool bit = (x >> (n - depth - 1)) & 1;\n int l0 = mat[depth].rank(0, li);\n int l1 = li - l0;\n int r0 = mat[depth].rank(0, ri);\n int r1 = ri - r0;\n if(l1 != r1){\n st.push(make_tuple(zc[depth] + l1, zc[depth] + r1, depth + 1, ((val<<1)|1), tight && bit));\n }\n if(l0 != r0){\n if(!tight || !bit){\n st.push(make_tuple(l0, r0, depth + 1, (val << 1), tight));\n }\n }\n }\n return -1;\n }\n vector<tuple<T, int, int>> intersect(int s1, int e1, int s2, int e2){\n using v_t = tuple<int, int, int, int, int, T>;\n vector<tuple<T, int, int>> res;\n queue<v_t> q;\n q.push(make_tuple(s1, e1, s2, e2, 0, 0));\n while(!q.empty()){\n auto p = q.front(); q.pop();\n int s_1, e_1, s_2, e_2, depth;\n T val;\n tie(s_1, e_1, s_2, e_2, depth, val) = p;\n if(depth == n){\n res.emplace_back(make_tuple(val, e_1 - s_1, e_2 - s_2));\n continue;\n }\n\n int s1_0 = mat[depth].rank(0, s_1);\n int e1_0 = mat[depth].rank(0, e_1);\n int s2_0 = mat[depth].rank(0, s_2);\n int e2_0 = mat[depth].rank(0, e_2);\n\n if(s1_0 != e1_0 && s2_0 != e2_0){\n q.push(make_tuple(s1_0, e1_0, s2_0, e2_0, depth + 1, val));\n }\n\n int s1_1 = s_1 - s1_0 + zc[depth];\n int e1_1 = e_1 - e1_0 + zc[depth];\n int s2_1 = s_2 - s2_0 + zc[depth];\n int e2_1 = e_2 - e2_0 + zc[depth];\n\n if(s1_1 != e1_1 && s2_1 != e2_1){\n q.push(make_tuple(s1_1, e1_1, s2_1, e2_1, depth + 1, val | (1LL << (n - depth - 1))));\n }\n }\n return res;\n }\n void max_dfs(int d, int s, int e, int &k, T val, vector<T> &vs){\n if(s >= e || !k)return;\n if(d == n){\n while(s++ < e && k > 0)vs.emplace_back(val), k--;\n return;\n }\n int l1 = mat[d].rank(1, s);\n int r1 = mat[d].rank(1, e);\n max_dfs(d + 1, zc[d] + l1, zc[d] + r1, k, (1LL<<(n-d-1))|val, vs);\n max_dfs(d + 1, s - l1, e - r1, k, val, vs);\n }\n vector<T> maximum(int s, int e, int k){\n if(e - s < k)k = e - s;\n if(k < 0)return {};\n vector<T> res;\n max_dfs(0, s, e, k, 0, res);\n return res;\n }\n void list_dfs(int d, int s, int e, T val, T a, T b, vector<pair<T, int>> &vs){\n if(val >= b || e - s <= 0)return;\n if(d == n){\n if(a <= val){\n vs.emplace_back(make_pair(val, e - s));\n }\n return;\n }\n T nv = val | (1LL<<(n-d-1)), nnv = nv | ((1LL<<(n-d-1))-1);\n if(nnv < a)return;\n int l0 = mat[d].rank(1, s);\n int r0 = mat[d].rank(1, e);\n list_dfs(d + 1, s - l0, e - r0, val, a, b, vs);\n list_dfs(d + 1, zc[d] + l0, zc[d] + r0, nv, a, b, vs);\n }\n vector<pair<T, int>> freq_list(int s, int e, T a, T b){\n vector<pair<T, int>> res;\n list_dfs(0, s, e, 0, a, b, res);\n return res;\n }\n vector<pair<int, T>> get_rect(int s, int e, T a, T b){\n vector<pair<T, int>> fl = freq_list(s, e, a, b);\n vector<pair<int, T>> res;\n for(auto &e: fl){\n for(int i = 0; i < e.second; i++){\n res.emplace_back(make_pair(select(e.first, i, s), e.first));\n }\n }\n return res;\n }\n};\n\ntemplate <typename T>\nstruct Graph_ {\n int n;\n vector<vector<pair<int, T> > > edge;\n vector<T> dis;\n Graph_(int ns) {\n n = ns;\n edge.resize(n);\n dis.resize(n);\n }\n void dijkstra(int s){\n dijkstra(s, 0);\n }\n T dijkstra(int s, int t){\n // initialize\n fill(dis.begin(), dis.end(), -1);\n vector<bool> used;\n used.resize(n);\n fill(used.begin(), used.end(), false);\n dis[s] = 0;\n // dijkstra\n priority_queue<pair<T, int>, vector<pair<T, int> >, greater<pair<T, int> > > q;\n q.push(make_pair(0, s));\n while(!q.empty()){\n pair<T, int> p = q.top(); q.pop();\n int at = p.second;\n T distance = p.first;\n if(used[at])continue;\n used[at] = true;\n for(auto itr = edge[at].begin(); itr != edge[at].end(); ++itr){\n int to = (*itr).first;\n T cost = (*itr).second;\n if(used[to])continue;\n if(dis[to] == -1 || dis[to] > distance + cost){\n q.push(make_pair(distance + cost, to));\n dis[to] = distance + cost;\n }\n }\n }\n return dis[t];\n }\n void adde(int at, int to, T cost){\n edge[at].push_back(make_pair(to, cost));\n }\n [[deprecated(\"This function takes O(edge[at].size()).\")]]\n void remove(int at, int to){\n int index = -1;\n for(int i = 0; i < edge[at].size(); i++){\n if(edge[at][i].first == to){\n index = i;\n break;\n }\n }\n edge[at].erase(edge[at].begin() + index);\n }\n};\n\ntypedef struct Graph_<int> GraphI;\ntypedef struct Graph_<ll> GraphL;\ntypedef struct Graph_<double> GraphD;\n\nbool comp(const Pll &a, const Pll &b){\n return a.FI < b.FI;\n}\n\nint main(int argc, char const* argv[])\n{\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n INT(n); INT(m);\n GraphL graph(n);\n rep(i, m){\n INT(a); INT(b); LL(c);\n graph.adde(a, b, c);\n graph.adde(b, a, c);\n }\n graph.dijkstra(0);\n vector<ll> ds = graph.dis;\n graph.dijkstra(n - 1);\n vector<ll> de = graph.dis;\n vector<Pll> vec(n);\n rep(i, n)vec[i] = mk(ds[i], de[i]);\n sort(all(vec), comp);\n vector<ll> v(n);\n rep(i, n){\n v[i] = vec[i].SE;\n }\n WaveletMatrix<ll> w(linf, v);\n INT(q);\n rep(i, q){\n LL(fs); LL(fg);\n int r = upper_bound(all(vec), mk(fs, linf)) - vec.begin();\n cout << w.rangefreq(0, r, fg, linf) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 59560, "score_of_the_acc": -0.6913, "final_rank": 10 }, { "submission_id": "aoj_1505_3564825", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst double PI = acos(-1);\nconst double EPS = 1e-15;\nusing ll = long long;\nusing ull = unsigned long long;\nconst int inf = 2e9;\nconst ll INF = 2e18;\nconst ll MOD = 1e9+7;\nconst ll MOD1 = 998244353;\ntypedef pair<ll,ll> P;\n\n#define rep(i,a,b) for (int i = (a); i < (b); i++)\n#define rrep(i,a,b) for (int i = (a); i >= (b); i--)\n#define REP(i,n) rep(i,0,n)\n#define RREP(i,n) rrep(i,n,0)\n#define sz(s) (s).size()\n#define pb push_back\n#define fi first\n#define se second\n//#define mp make_pair\n\nll n,m;\nvector<P> G[100010];\nll ds[100010];\nll dt[100010];\nint bit[200010];\nmap<ll,int> mp;\nmap<int,ll> mp1;\nll ans[100010];\nvector<ll> work;\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n cin >> n >> m;\n REP(i,m) {\n ll a,b,c;\n cin >> a >> b >> c;\n G[a].pb({b,c});\n G[b].pb({a,c});\n }\n fill(ds, ds + n, INF);\n fill(dt, dt + n, INF);\n\n priority_queue<P,vector<P>,greater<P>> pq1,pq2;\n pq1.push({0LL,0LL});\n pq2.push({0LL,n-1});\n\n ds[0] = 0;\n dt[n-1] = 0;\n\n while (!pq1.empty()) {\n P p = pq1.top();\n pq1.pop();\n ll v = p.second;\n if (ds[v] < p.first) continue;\n for (int i = 0; i < G[v].size(); i++) {\n P e = G[v][i];\n if (ds[e.fi] > ds[v] + e.se) {\n ds[e.fi] = ds[v] + e.se;\n pq1.push(P(ds[e.fi], e.fi));\n }\n }\n }\n\n while (!pq2.empty()) {\n P p = pq2.top();\n pq2.pop();\n ll v = p.second;\n if (dt[v] < p.first) continue;\n for (int i = 0; i < G[v].size(); i++) {\n P e = G[v][i];\n if (dt[e.fi] > dt[v] + e.se) {\n dt[e.fi] = dt[v] + e.se;\n pq2.push(P(dt[e.fi], e.fi));\n }\n }\n }\n\n priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> P;\n priority_queue<pair<pair<ll,ll>,int>,vector<pair<pair<ll,ll>,int>>,greater<pair<pair<ll,ll>,int>>> Q;\n\n REP(i,n) {\n P.push({ds[i],dt[i]});\n }\n sort(dt,dt+n);\n int num = 1;\n REP(i,n) {\n work.pb(dt[i]);\n }\n\n int q;\n cin >> q;\n REP(i,q) {\n ll fs, ft;\n cin >> fs >> ft;\n Q.push({{fs,ft},i});\n work.pb(ft);\n }\n sort(work.begin(),work.end());\n REP(i,sz(work)) {\n if (!mp.count(work[i])) mp[work[i]] = num++;\n }\n\n int cnt = 0;\n while (!Q.empty()) {\n ll lim = Q.top().fi.fi;\n while (!P.empty() && P.top().fi <= lim) {\n ll tmp = mp[P.top().se];\n P.pop();\n cnt++;\n while (tmp <= num) {\n bit[tmp]++;\n tmp += tmp & -tmp;\n }\n }\n// cout << \"------------\" << endl;\n// cout << cnt << endl;\n while (!Q.empty() && Q.top().fi.fi == lim) {\n\n ll tmp = mp[Q.top().fi.se] - 1;\n int id = Q.top().se;\n //cout << id << \" \" << tmp << \" \";\n ans[id] += cnt;\n while (tmp > 0) {\n ans[id] -= bit[tmp];\n tmp -= tmp & -tmp;\n }\n //cout << ans[id] << endl;\n Q.pop();\n }\n }\n\n REP(i,q) {\n cout << ans[i] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 28100, "score_of_the_acc": -0.4227, "final_rank": 7 }, { "submission_id": "aoj_1505_3534037", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <utility>\n#include <queue>\n#include <tuple>\n#include <set>\n\n#define REP(i, a, b) for (int i = int(a); i < int(b); i++)\n#ifdef _DEBUG_\n#define dump(val) cerr << __LINE__ << \":\\t\" << #val << \" = \" << (val) << endl\n#else\n#define dump(val)\n#endif\n\nusing namespace std;\n\ntypedef long long int ll;\ntypedef pair<ll, ll> P;\n\ntemplate<typename T>\nvector<T> make_v(size_t a, T b) {\n return vector<T>(a, b);\n}\n\ntemplate<typename... Ts>\nauto make_v(size_t a, Ts... ts) {\n return vector<decltype(make_v(ts...))>(a, make_v(ts...));\n}\n\nconst ll inf = 1LL << 60;\n\nvoid dijkstra(int s, vector<vector<P>> &G, vector<ll> &Cost) {\n int n = G.size();\n Cost.assign(n, inf);\n Cost[s] = 0;\n priority_queue<P, vector<P>, greater<P>> pq;\n pq.emplace(0LL, s);\n while (pq.size()) {\n ll cost;\n int to;\n tie(cost, to) = pq.top();\n pq.pop();\n if (cost > Cost[to]) continue;\n for (auto p : G[to]) {\n ll ncost = p.first + cost;\n int nto = p.second;\n if (Cost[nto] > ncost) {\n Cost[nto] = ncost;\n pq.emplace(ncost, nto);\n }\n }\n }\n}\n\nclass BIT {\npublic:\n vector<ll> data;\n BIT(int n) {\n int N = 1;\n while (N < n) {\n N *= 2;\n }\n data.resize(N, 0);\n }\n void update(int pos, ll val) {\n pos++;\n for (int p = pos; p < data.size(); p += (p & -p)) {\n data[p] += val;\n }\n }\n ll query(int pos) {\n ll ans = 0;\n pos++;\n for (int p = pos; p > 0; p -= (p & -p)) {\n ans += data[p];\n }\n return ans;\n }\n};\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n, m;\n cin >> n >> m;\n vector<vector<P>> Graph(n);\n REP(i, 0, m) {\n int a, b, c;\n cin >> a >> b >> c;\n Graph[a].push_back({c, b});\n Graph[b].push_back({c, a});\n }\n vector<P> Cost(n);\n {\n vector<ll> start, goal;\n dijkstra(0, Graph, start);\n dijkstra(n - 1, Graph, goal);\n REP(i, 0, n) {\n Cost[i] = {start[i], goal[i]};\n }\n sort(Cost.begin(), Cost.end());\n }\n\n int q;\n cin >> q;\n vector<P> F(q);\n REP(i, 0, q) {\n cin >> F[i].first >> F[i].second;\n }\n vector<int> indices(q);\n REP(i, 0, q) {\n indices[i] = i;\n }\n sort(indices.begin(), indices.end(), [&](int a, int b) {\n return F[a] < F[b];\n });\n\n int an = 0;\n // 座標圧縮\n {\n vector<ll> val;\n REP(i, 0, n) {\n val.push_back(Cost[i].second);\n }\n REP(i, 0, q) {\n val.push_back(F[i].second);\n }\n sort(val.begin(), val.end());\n val.erase(unique(val.begin(), val.end()), val.end());\n an = val.size();\n\n REP(i, 0, n) {\n Cost[i].second = lower_bound(val.begin(), val.end(), Cost[i].second) - val.begin();\n }\n REP(i, 0, q) {\n F[i].second = lower_bound(val.begin(), val.end(), F[i].second) - val.begin();\n }\n }\n\n BIT bit(an + 2);\n\n vector<int> ans(q, 0);\n int pos = 0;\n set<ll> s;\n REP(i, 0, q) {\n int v = indices[i];\n while (pos < n && Cost[pos].first <= F[v].first) {\n bit.update(Cost[pos].second, 1);\n pos++;\n }\n ans[v] = pos - bit.query(F[v].second - 1);\n }\n REP(i, 0, q) {\n cout << ans[i] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 16848, "score_of_the_acc": -0.2552, "final_rank": 4 }, { "submission_id": "aoj_1505_3374192", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i,n) REP(i,0,n)\n#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)\n#define repr(i, n) REPR(i, n, 0)\n#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)\n#define pb push_back\n#define all(r) r.begin(),r.end()\n#define rall(r) r.rbegin(),r.rend()\n#define fi first\n#define se second\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\nconst ll INF = 1e18;\n\nconst int MAX_N = 1e5 + 10;\nstruct Edge { int to; ll cost; };\nvector<Edge> es[MAX_N];\n\nll ds[MAX_N];\nll dg[MAX_N];\n\n\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n#include<ext/pb_ds/tag_and_trait.hpp>\nusing namespace __gnu_pbds;\nusing Tree = tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int n, m;\n cin >> n >> m;\n rep(i, m) {\n int a, b;\n ll c;\n cin >> a >> b >> c;\n es[a].pb({b, c});\n es[b].pb({a, c});\n }\n auto dijkstra = [&] (int root, ll d[]) {\n rep(i, MAX_N) d[i] = INF;\n typedef pair<ll, int> P; //dist, from\n priority_queue<P, vector<P>, greater<P> > q;\n d[root] = 0LL;\n q.push({0LL, root});\n while (!q.empty()) {\n auto p = q.top(); q.pop();\n int prv = p.se;\n auto cost = p.fi;\n if (d[prv] < p.fi) continue;\n for (auto& e : es[prv]) {\n int to = e.to;\n auto dist = e.cost + cost;\n if (dist < d[to]) {\n d[to] = dist;\n q.push({dist, to});\n }\n }\n }\n };\n dijkstra(0, ds);\n dijkstra(n-1, dg);\n using P = pair<ll, ll>;\n vector<P> v(n);\n rep(i, n) v[i] = {ds[i], dg[i]};\n sort(all(v));\n int Q;\n cin >> Q;\n vector<pair<P, int>> query(Q);\n rep(i, Q) {\n ll s, g;\n cin >> s >> g;\n query[i] = {{s, g}, i};\n }\n sort(all(query));\n\n\n map<ll, ll> mp;\n rep(i, n) mp[v[i].se]++;\n rep(i, Q) mp[query[i].fi.se]++;\n int sz = 0;\n for(auto&& p: mp) p.se = sz++;\n\n vector<int> ans(Q);\n Tree st;\n int cur = 0;\n rep(i, Q) {\n ll s = query[i].fi.fi, g = query[i].fi.se;\n int idx = query[i].se;\n while(cur < n && v[cur].fi <= s) st.insert(mp[v[cur++].se] * MAX_N + cur);\n int tmp = st.order_of_key(mp[g]*MAX_N);\n ans[idx] = (int)st.size() - tmp;\n }\n rep(i, Q) cout << ans[i] << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 31940, "score_of_the_acc": -0.384, "final_rank": 5 }, { "submission_id": "aoj_1505_3139631", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long int ll;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> Pll;\ntypedef vector<int> Vi;\n//typedef tuple<int, int, int> T;\n#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)\n#define REP(i,x) FOR(i,0,x)\n#define ALL(c) c.begin(), c.end()\n#define DUMP( x ) cerr << #x << \" = \" << ( x ) << endl\n#define UNIQUE(c) sort(ALL(c)), c.erase(unique(ALL(c)), c.end())\n\nconst int dr[4] = {-1, 0, 1, 0};\nconst int dc[4] = {0, 1, 0, -1};\n\n// graph by adjacency list\ntemplate <typename T>\nstruct Edge {\n int dst; T weight;\n Edge(int dst, T weight) : dst(dst), weight(weight) { }\n bool operator < (const Edge<T> &e) const {\n return weight > e.weight;\n }\n};\n\ntemplate <typename T>\nstruct Graph {\n int V;\n vector<vector<Edge<T>>> E;\n Graph(int V) : V(V) { E.resize(V); }\n void add_edge(int src, int dst, T weight) {\n E[src].emplace_back(dst, weight);\n }\n};\n\n\nconst ll INF = 1e12;\n\ntemplate <typename T>\nstruct Node {\n int v; T dist;\n Node(int v, T dist) : v(v), dist(dist) { };\n bool operator < (const Node<T> &n) const {\n return dist > n.dist; // reverse\n }\n};\n\ntemplate <typename T>\nstruct ShortestPath {\n const Graph<T> g;\n vector<T> dist;\n vector<int> prev;\n\n ShortestPath(const Graph<T> &g) : g(g) { dist.resize(g.V), prev.resize(g.V); }\n\n void run(int start) {\n priority_queue<Node<T>> que;\n dist.assign(g.V, INF);\n dist[start] = 0;\n que.emplace(start, 0);\n prev[0] = -1;\n\n while (!que.empty()) {\n Node<T> n = que.top(); que.pop();\n int v = n.v; T cost = n.dist;\n if (dist[v] < cost) continue;\n for (Edge<T> e : g.E[v]) {\n if (dist[v] < dist[e.dst] - e.weight) {\n dist[e.dst] = dist[v] + e.weight;\n prev[e.dst] = v;\n que.emplace(e.dst, dist[e.dst]);\n }\n }\n }\n }\n\n vector<int> build_path(int goal) {\n vector<int> path;\n for (int v = goal; v != -1; v = prev[v]) {\n path.emplace_back(v);\n }\n reverse(path.begin(), path.end());\n return path;\n }\n};\n\n/**\n * @brief type for input data\n */\ntemplate <typename T>\nstruct Data {\n using type = T;\n type data;\n\n /**\n * Constructor for Data\n * @param data Data should be std::array\n */\n Data(type &data) : data(data) {}\n\n /**\n * Comparing function for data.\n * This function compares two data in lexicographical order.\n *\n * @param data1 First data for comparing\n * @param data2 Second data for comparing\n * @param base Start dimension for comparing.\n *\n * @return If data1 < data1 in lexicographical order, then true, otherwise false.\n */\n static bool compare (const Data<type> &data1, const Data<type> &data2, size_t base = 1) {\n const size_t dim = data1.data.size();\n for (size_t i = 0; i < dim; i++) {\n if (data1.data[(i + base) % dim] != data2.data[(i + base) % dim]) {\n return data1.data[(i + base) % dim] < data2.data[(i + base) % dim];\n }\n }\n return false;\n }\n /**\n * == operator for data\n */\n bool operator==(const Data<type> &data) const {\n return this->data == data.data;\n }\n};\n\n/**\n * Orthogonal Region for\n */\ntemplate <typename T>\nstruct Region {\n T low, high;\n /**\n * Constructor for struct Region\n * If d = low.size(), then corresponding orthogonal region R is following.\n * R = [low[0], high[0]] * [low[1], high[1]] * ... * [low[d-1], high[d-1]]\n *\n * @param low lower borders of orthogonal region\n * @param high higher borders of orthogonal region\n */\n Region(T &low, T &high) : low(low), high(high) {};\n\n /**\n * Checking Function of Inclusion for Data and Region.\n *\n * @param data A data\n * @return If this region include the data, then return true, otherwise return false.\n */\n bool include(T data) const {\n size_t dim = this->low.size();\n for (size_t i = 0; i < dim; i++) {\n if (data[i] < low[i] or high[i] < data[i]) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Checking Function of Inclusion for Two Regions.\n *\n * @param region A region.\n * @return If this region include the given region, then return true, otherwise return false.\n */\n bool include(const Region<T> &region) const {\n size_t dim = this->low.size();\n for (size_t bit = 0; bit < (1U << dim); bit++) {\n T data;\n for (size_t i = 0; i < dim; i++) {\n if (bit >> i & 1) {\n data[i] = region.low[i];\n } else {\n data[i] = region.high[i];\n }\n }\n if (not this->include(data)) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Cheking Function of Overlapping for Two Regions.\n *\n * @param region A region.\n * @return If this region overlaps the given region, then return true, otherwise return false.\n */\n bool overlap(Region<T> &region) const {\n size_t dim = this->low.size();\n for (size_t bit = 0; bit < (1U << dim); bit++) {\n T data;\n for (size_t i = 0; i < dim; i++) {\n if (bit >> i & 1) {\n data[i] = region.low[i];\n } else {\n data[i] = region.high[i];\n }\n }\n if (this->include(data)) {\n return true;\n }\n }\n return false;\n }\n\n /**\n * overloading of << operator\n */\n friend std::ostream& operator<<(std::ostream& os, const Region<T> &region) {\n for (size_t i = 0; i < region.low.size(); i++) {\n if (i > 0) {\n os << 'x';\n }\n os << '[' << region.low[i] << ',' << region.high[i] << ']';\n }\n return os;\n }\n};\n\nstruct RangeTreeNode {\n size_t idx; /**< Node index */\n int node_count;\n RangeTreeNode *assoc; /**< Pointer of associated structure */\n RangeTreeNode *left, *right; /**< Pointer of left & right node */\n RangeTreeNode(size_t idx) : idx(idx), node_count(1), assoc(nullptr), left(nullptr), right(nullptr) {}\n bool is_leaf_node() const {\n return left == nullptr and right == nullptr;\n }\n};\n\ntemplate <typename DataType>\nstruct RangeTree {\n using T = typename DataType::type;\n using VT = typename T::value_type;\n size_t dim; /**< Dimension of Data */\n std::vector<DataType> data; /**< Data in range tree */\n VT min_range, max_range; /**< Initial Range for range query */\n RangeTreeNode *root; /**< Root Node */\n\n RangeTree(size_t dim = 2, VT min_range = std::numeric_limits<VT>::min(), VT max_range = std::numeric_limits<VT>::max()) : dim(dim), min_range(min_range), max_range(max_range) {}\n\n RangeTreeNode *build_1d(std::vector<size_t> &data, size_t low, size_t high) {\n if (low + 1 == high) {\n return new RangeTreeNode(data[low]);\n }\n size_t median = (low + high) / 2;\n RangeTreeNode *node = new RangeTreeNode(data[median]);\n if (low < median) {\n node->left = build_1d(data, low, median);\n node->node_count += node->left->node_count;\n }\n if (median + 1 < high) {\n node->right = build_1d(data, median + 1, high);\n node->node_count += node->right->node_count;\n }\n return node;\n }\n\n RangeTreeNode *build(std::vector<std::vector<size_t>> &data, size_t depth) {\n RangeTreeNode *assoc;\n if (depth + 2 == this->dim) {\n assoc = build_1d(data[depth + 1], 0, data[0].size());\n } else {\n assoc = build(data, depth + 1);\n }\n\n if (data[0].size() == 1) {\n RangeTreeNode *node = new RangeTreeNode(data[0][0]);\n node->assoc = new RangeTreeNode(data[0][0]);\n return node;\n }\n std::vector<std::vector<size_t>> left_data(this->dim), right_data(this->dim);\n size_t median_idx = static_cast<size_t>(static_cast<int>(data[0].size()) / 2);\n size_t median = data[depth % this->dim][median_idx];\n for (size_t i = 0; i < this->dim; i++) {\n for (size_t j : data[i]) {\n if (DataType::compare(this->data[j], this->data[median], depth % this->dim)) {\n left_data[i].emplace_back(j);\n } else if (not (this->data[j] == this->data[median])) {\n right_data[i].emplace_back(j);\n }\n }\n }\n RangeTreeNode *node = new RangeTreeNode(median);\n if (left_data[0].size()) {\n node->left = build(left_data, depth);\n node->node_count += node->left->node_count;\n }\n if (right_data[0].size()) {\n node->right = build(right_data, depth);\n node->node_count += node->right->node_count;\n }\n node->assoc = assoc;\n return node;\n }\n\n void build(std::vector<DataType> &data) {\n assert(data.size() > 0);\n std::vector<std::vector<size_t>> _data(this->dim, std::vector<size_t>(data.size()));\n this->data = std::move(data);\n for (size_t i = 0; i < this->dim; i++) {\n std::iota(_data[i].begin(), _data[i].end(), 0);\n sort(_data[i].begin(), _data[i].end(),\n [&](const size_t &a, const size_t &b) {\n return DataType::compare(this->data[a], this->data[b], i);\n });\n }\n this->root = build(_data, 0);\n }\n\n void output(bool assoc = true) const {\n output(this->root, assoc);\n }\n\n void output(RangeTreeNode *node, bool assoc = true) const {\n if (node == nullptr) return ;\n std::cout << \"idx: \" << node->idx << \", (\";\n for (size_t i = 0; i < this->dim; i++) {\n if (i > 0) {\n std::cout << ',';\n }\n std::cout << this->data[node->idx].data[i];\n }\n std::cout << \") \";\n if (not node->is_leaf_node()) {\n std::cout << \"(left, right) = (\" << (node->left == nullptr ? -1 : (int)node->left->idx) << ',' << (node->right == nullptr ? -1 : (int)node->right->idx) << ')' << std::endl;\n if (node->assoc != nullptr and assoc) {\n std::cout << \"assoc start\" << std::endl;\n output(node->assoc, assoc);\n std::cout << \"assoc end\" << std::endl;\n }\n output(node->left, assoc);\n output(node->right, assoc);\n\n } else {\n std::cout << std::endl;\n }\n }\n\n RangeTreeNode *find_split_node(RangeTreeNode *node, size_t dim, DataType &low, DataType &high) const {\n\n while (node != nullptr and (DataType::compare(this->data[node->idx], low, dim) or\n not DataType::compare(this->data[node->idx], high, dim))) {\n if (DataType::compare(this->data[node->idx], low, dim)) {\n node = node->right;\n } else {\n node = node->left;\n }\n }\n return node;\n }\n\n void report_subtree(RangeTreeNode *node, std::vector<size_t> &output) const {\n if (node == nullptr) return ;\n output.emplace_back(node->idx);\n report_subtree(node->left, output);\n report_subtree(node->right, output);\n }\n\n void query(Region<T> &query_region, std::vector<size_t> &output) const {\n query(this->root, query_region, 0, output);\n }\n\n int count_query(Region<T> &query_region) const {\n return count_query(this->root, query_region, 0);\n }\n\n void query(RangeTreeNode *node, Region<T> &query_region, size_t dim, std::vector<size_t> &output) const {\n if (node == nullptr) {\n return ;\n }\n T low_, high_;\n low_.fill(this->min_range), high_.fill(this->max_range);\n low_[dim] = query_region.low[dim], high_[dim] = query_region.high[dim];\n DataType low(low_), high(high_);\n\n RangeTreeNode *split_node = this->find_split_node(node, dim, low, high);\n\n if (split_node == nullptr) {\n return ;\n }\n\n //std::cout << \"split: \" << split_node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[split_node->idx].data)) {\n output.emplace_back(split_node->idx);\n }\n\n node = split_node->left;\n while (node != nullptr) {\n //std::cout << \"left: \" << node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[node->idx].data)) {\n output.emplace_back(node->idx);\n }\n\n if (DataType::compare(this->data[node->idx], low, dim)) {\n node = node->right;\n } else {\n if (dim + 1 == this->dim) {\n report_subtree(node->right, output);\n } else if (node->right != nullptr) {\n query(node->right->assoc, query_region, dim + 1, output);\n }\n node = node->left;\n }\n }\n\n node = split_node->right;\n while (node != nullptr) {\n //std::cout << \"right: \" << node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[node->idx].data)) {\n output.emplace_back(node->idx);\n }\n if (DataType::compare(this->data[node->idx], high, dim)) {\n if (dim + 1 == this->dim) {\n report_subtree(node->left, output);\n } else if (node->left != nullptr) {\n query(node->left->assoc, query_region, dim + 1, output);\n }\n node = node->right;\n } else {\n node = node->left;\n }\n }\n }\n\n int count_query(RangeTreeNode *node, Region<T> &query_region, size_t dim) const {\n if (node == nullptr) {\n return 0;\n }\n T low_, high_;\n low_.fill(this->min_range), high_.fill(this->max_range);\n low_[dim] = query_region.low[dim], high_[dim] = query_region.high[dim];\n DataType low(low_), high(high_);\n\n RangeTreeNode *split_node = this->find_split_node(node, dim, low, high);\n\n if (split_node == nullptr) {\n return 0;\n }\n\n int node_count = 0;\n //std::cout << \"split: \" << split_node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[split_node->idx].data)) {\n node_count++;\n }\n\n node = split_node->left;\n while (node != nullptr) {\n //std::cout << \"left: \" << node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[node->idx].data)) {\n node_count++;\n }\n\n if (DataType::compare(this->data[node->idx], low, dim)) {\n node = node->right;\n } else {\n if (dim + 1 == this->dim) {\n node_count += node->right == nullptr ? 0 : node->right->node_count;\n } else if (node->right != nullptr) {\n node_count += count_query(node->right->assoc, query_region, dim + 1);\n }\n node = node->left;\n }\n }\n\n node = split_node->right;\n while (node != nullptr) {\n //std::cout << \"right: \" << node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[node->idx].data)) {\n node_count++;\n }\n if (DataType::compare(this->data[node->idx], high, dim)) {\n if (dim + 1 == this->dim) {\n node_count += node->left == nullptr ? 0 : node->left->node_count;\n } else if (node->left != nullptr) {\n node_count += count_query(node->left->assoc, query_region, dim + 1);\n }\n node = node->right;\n } else {\n node = node->left;\n }\n }\n\n return node_count;\n }\n\n};\n\nint main() {\n // use scanf in CodeForces!\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int N, M; cin >> N >> M;\n Graph<ll> g1(N), g2(N);\n REP(i, M) {\n int a, b; ll c; cin >> a >> b >> c;\n g1.add_edge(a, b, c);\n g1.add_edge(b, a, c);\n g2.add_edge(a, b, c);\n g2.add_edge(b, a, c);\n }\n\n ShortestPath<ll> sp1(g1), sp2(g2);\n sp1.run(0), sp2.run(N - 1);\n\n using DataType = Data<std::array<ll, 3>>;\n RangeTree<DataType> range_tree(2, 0);\n std::vector<DataType> points;\n REP(i, N) {\n DataType::type tmp = {sp1.dist[i], sp2.dist[i], i};\n points.emplace_back(DataType(tmp));\n }\n range_tree.build(points);\n int Q; cin >> Q;\n REP(i, Q) {\n ll fs, gs; cin >> fs >> gs;\n DataType::type low = {range_tree.min_range, gs, 0}, high = {fs, range_tree.max_range, N - 1};\n Region<DataType::type> query_region(low, high);\n //std::vector<size_t> out;\n //range_tree.query(query_region, out);\n //std::cout << out.size() << std::endl;\n std::cout << range_tree.count_query(query_region) << std::endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 121748, "score_of_the_acc": -1.5476, "final_rank": 12 }, { "submission_id": "aoj_1505_3139627", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long int ll;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> Pll;\ntypedef vector<int> Vi;\n//typedef tuple<int, int, int> T;\n#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)\n#define REP(i,x) FOR(i,0,x)\n#define ALL(c) c.begin(), c.end()\n#define DUMP( x ) cerr << #x << \" = \" << ( x ) << endl\n#define UNIQUE(c) sort(ALL(c)), c.erase(unique(ALL(c)), c.end())\n\nconst int dr[4] = {-1, 0, 1, 0};\nconst int dc[4] = {0, 1, 0, -1};\n\n// graph by adjacency list\ntemplate <typename T>\nstruct Edge {\n int dst; T weight;\n Edge(int dst, T weight) : dst(dst), weight(weight) { }\n bool operator < (const Edge<T> &e) const {\n return weight > e.weight;\n }\n};\n\ntemplate <typename T>\nstruct Graph {\n int V;\n vector<vector<Edge<T>>> E;\n Graph(int V) : V(V) { E.resize(V); }\n void add_edge(int src, int dst, T weight) {\n E[src].emplace_back(dst, weight);\n }\n};\n\n\n#define INF INT_MAX\n\ntemplate <typename T>\nstruct Node {\n int v; T dist;\n Node(int v, T dist) : v(v), dist(dist) { };\n bool operator < (const Node<T> &n) const {\n return dist > n.dist; // reverse\n }\n};\n\ntemplate <typename T>\nstruct ShortestPath {\n const Graph<T> g;\n vector<T> dist;\n vector<int> prev;\n\n ShortestPath(const Graph<T> &g) : g(g) { dist.resize(g.V), prev.resize(g.V); }\n\n void run(int start) {\n priority_queue<Node<T>> que;\n dist.assign(g.V, INF);\n dist[start] = 0;\n que.emplace(start, 0);\n prev[0] = -1;\n\n while (!que.empty()) {\n Node<T> n = que.top(); que.pop();\n int v = n.v; T cost = n.dist;\n if (dist[v] < cost) continue;\n for (Edge<T> e : g.E[v]) {\n if (dist[v] < dist[e.dst] - e.weight) {\n dist[e.dst] = dist[v] + e.weight;\n prev[e.dst] = v;\n que.emplace(e.dst, dist[e.dst]);\n }\n }\n }\n }\n\n vector<int> build_path(int goal) {\n vector<int> path;\n for (int v = goal; v != -1; v = prev[v]) {\n path.emplace_back(v);\n }\n reverse(path.begin(), path.end());\n return path;\n }\n};\n\n/**\n * @brief type for input data\n */\ntemplate <typename T>\nstruct Data {\n using type = T;\n type data;\n\n /**\n * Constructor for Data\n * @param data Data should be std::array\n */\n Data(type &data) : data(data) {}\n\n /**\n * Comparing function for data.\n * This function compares two data in lexicographical order.\n *\n * @param data1 First data for comparing\n * @param data2 Second data for comparing\n * @param base Start dimension for comparing.\n *\n * @return If data1 < data1 in lexicographical order, then true, otherwise false.\n */\n static bool compare (const Data<type> &data1, const Data<type> &data2, size_t base = 1) {\n const size_t dim = data1.data.size();\n for (size_t i = 0; i < dim; i++) {\n if (data1.data[(i + base) % dim] != data2.data[(i + base) % dim]) {\n return data1.data[(i + base) % dim] < data2.data[(i + base) % dim];\n }\n }\n return false;\n }\n /**\n * == operator for data\n */\n bool operator==(const Data<type> &data) const {\n return this->data == data.data;\n }\n};\n\n/**\n * Orthogonal Region for\n */\ntemplate <typename T>\nstruct Region {\n T low, high;\n /**\n * Constructor for struct Region\n * If d = low.size(), then corresponding orthogonal region R is following.\n * R = [low[0], high[0]] * [low[1], high[1]] * ... * [low[d-1], high[d-1]]\n *\n * @param low lower borders of orthogonal region\n * @param high higher borders of orthogonal region\n */\n Region(T &low, T &high) : low(low), high(high) {};\n\n /**\n * Checking Function of Inclusion for Data and Region.\n *\n * @param data A data\n * @return If this region include the data, then return true, otherwise return false.\n */\n bool include(T data) const {\n size_t dim = this->low.size();\n for (size_t i = 0; i < dim; i++) {\n if (data[i] < low[i] or high[i] < data[i]) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Checking Function of Inclusion for Two Regions.\n *\n * @param region A region.\n * @return If this region include the given region, then return true, otherwise return false.\n */\n bool include(const Region<T> &region) const {\n size_t dim = this->low.size();\n for (size_t bit = 0; bit < (1U << dim); bit++) {\n T data;\n for (size_t i = 0; i < dim; i++) {\n if (bit >> i & 1) {\n data[i] = region.low[i];\n } else {\n data[i] = region.high[i];\n }\n }\n if (not this->include(data)) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Cheking Function of Overlapping for Two Regions.\n *\n * @param region A region.\n * @return If this region overlaps the given region, then return true, otherwise return false.\n */\n bool overlap(Region<T> &region) const {\n size_t dim = this->low.size();\n for (size_t bit = 0; bit < (1U << dim); bit++) {\n T data;\n for (size_t i = 0; i < dim; i++) {\n if (bit >> i & 1) {\n data[i] = region.low[i];\n } else {\n data[i] = region.high[i];\n }\n }\n if (this->include(data)) {\n return true;\n }\n }\n return false;\n }\n\n /**\n * overloading of << operator\n */\n friend std::ostream& operator<<(std::ostream& os, const Region<T> &region) {\n for (size_t i = 0; i < region.low.size(); i++) {\n if (i > 0) {\n os << 'x';\n }\n os << '[' << region.low[i] << ',' << region.high[i] << ']';\n }\n return os;\n }\n};\n\nstruct RangeTreeNode {\n size_t idx; /**< Node index */\n int node_count;\n RangeTreeNode *assoc; /**< Pointer of associated structure */\n RangeTreeNode *left, *right; /**< Pointer of left & right node */\n RangeTreeNode(size_t idx) : idx(idx), node_count(1), assoc(nullptr), left(nullptr), right(nullptr) {}\n bool is_leaf_node() const {\n return left == nullptr and right == nullptr;\n }\n};\n\ntemplate <typename DataType>\nstruct RangeTree {\n using T = typename DataType::type;\n using VT = typename T::value_type;\n size_t dim; /**< Dimension of Data */\n std::vector<DataType> data; /**< Data in range tree */\n VT min_range, max_range; /**< Initial Range for range query */\n RangeTreeNode *root; /**< Root Node */\n\n RangeTree(size_t dim = 2, VT min_range = std::numeric_limits<VT>::min(), VT max_range = std::numeric_limits<VT>::max()) : dim(dim), min_range(min_range), max_range(max_range) {}\n\n RangeTreeNode *build_1d(std::vector<size_t> &data, size_t low, size_t high) {\n if (low + 1 == high) {\n return new RangeTreeNode(data[low]);\n }\n size_t median = (low + high) / 2;\n RangeTreeNode *node = new RangeTreeNode(data[median]);\n if (low < median) {\n node->left = build_1d(data, low, median);\n node->node_count += node->left->node_count;\n }\n if (median + 1 < high) {\n node->right = build_1d(data, median + 1, high);\n node->node_count += node->right->node_count;\n }\n return node;\n }\n\n RangeTreeNode *build(std::vector<std::vector<size_t>> &data, size_t depth) {\n RangeTreeNode *assoc;\n if (depth + 2 == this->dim) {\n assoc = build_1d(data[depth + 1], 0, data[0].size());\n } else {\n assoc = build(data, depth + 1);\n }\n\n if (data[0].size() == 1) {\n RangeTreeNode *node = new RangeTreeNode(data[0][0]);\n node->assoc = new RangeTreeNode(data[0][0]);\n return node;\n }\n std::vector<std::vector<size_t>> left_data(this->dim), right_data(this->dim);\n size_t median_idx = static_cast<size_t>(static_cast<int>(data[0].size()) / 2);\n size_t median = data[depth % this->dim][median_idx];\n for (size_t i = 0; i < this->dim; i++) {\n for (size_t j : data[i]) {\n if (DataType::compare(this->data[j], this->data[median], depth % this->dim)) {\n left_data[i].emplace_back(j);\n } else if (not (this->data[j] == this->data[median])) {\n right_data[i].emplace_back(j);\n }\n }\n }\n RangeTreeNode *node = new RangeTreeNode(median);\n if (left_data[0].size()) {\n node->left = build(left_data, depth);\n node->node_count += node->left->node_count;\n }\n if (right_data[0].size()) {\n node->right = build(right_data, depth);\n node->node_count += node->right->node_count;\n }\n node->assoc = assoc;\n return node;\n }\n\n void build(std::vector<DataType> &data) {\n assert(data.size() > 0);\n std::vector<std::vector<size_t>> _data(this->dim, std::vector<size_t>(data.size()));\n this->data = std::move(data);\n for (size_t i = 0; i < this->dim; i++) {\n std::iota(_data[i].begin(), _data[i].end(), 0);\n sort(_data[i].begin(), _data[i].end(),\n [&](const size_t &a, const size_t &b) {\n return DataType::compare(this->data[a], this->data[b], i);\n });\n }\n this->root = build(_data, 0);\n }\n\n void output(bool assoc = true) const {\n output(this->root, assoc);\n }\n\n void output(RangeTreeNode *node, bool assoc = true) const {\n if (node == nullptr) return ;\n std::cout << \"idx: \" << node->idx << \", (\";\n for (size_t i = 0; i < this->dim; i++) {\n if (i > 0) {\n std::cout << ',';\n }\n std::cout << this->data[node->idx].data[i];\n }\n std::cout << \") \";\n if (not node->is_leaf_node()) {\n std::cout << \"(left, right) = (\" << (node->left == nullptr ? -1 : (int)node->left->idx) << ',' << (node->right == nullptr ? -1 : (int)node->right->idx) << ')' << std::endl;\n if (node->assoc != nullptr and assoc) {\n std::cout << \"assoc start\" << std::endl;\n output(node->assoc, assoc);\n std::cout << \"assoc end\" << std::endl;\n }\n output(node->left, assoc);\n output(node->right, assoc);\n\n } else {\n std::cout << std::endl;\n }\n }\n\n RangeTreeNode *find_split_node(RangeTreeNode *node, size_t dim, DataType &low, DataType &high) const {\n\n while (node != nullptr and (DataType::compare(this->data[node->idx], low, dim) or\n not DataType::compare(this->data[node->idx], high, dim))) {\n if (DataType::compare(this->data[node->idx], low, dim)) {\n node = node->right;\n } else {\n node = node->left;\n }\n }\n return node;\n }\n\n void report_subtree(RangeTreeNode *node, std::vector<size_t> &output) const {\n if (node == nullptr) return ;\n output.emplace_back(node->idx);\n report_subtree(node->left, output);\n report_subtree(node->right, output);\n }\n\n void query(Region<T> &query_region, std::vector<size_t> &output) const {\n query(this->root, query_region, 0, output);\n }\n\n int count_query(Region<T> &query_region) const {\n return count_query(this->root, query_region, 0);\n }\n\n void query(RangeTreeNode *node, Region<T> &query_region, size_t dim, std::vector<size_t> &output) const {\n if (node == nullptr) {\n return ;\n }\n T low_, high_;\n low_.fill(this->min_range), high_.fill(this->max_range);\n low_[dim] = query_region.low[dim], high_[dim] = query_region.high[dim];\n DataType low(low_), high(high_);\n\n RangeTreeNode *split_node = this->find_split_node(node, dim, low, high);\n\n if (split_node == nullptr) {\n return ;\n }\n\n //std::cout << \"split: \" << split_node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[split_node->idx].data)) {\n output.emplace_back(split_node->idx);\n }\n\n node = split_node->left;\n while (node != nullptr) {\n //std::cout << \"left: \" << node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[node->idx].data)) {\n output.emplace_back(node->idx);\n }\n\n if (DataType::compare(this->data[node->idx], low, dim)) {\n node = node->right;\n } else {\n if (dim + 1 == this->dim) {\n report_subtree(node->right, output);\n } else if (node->right != nullptr) {\n query(node->right->assoc, query_region, dim + 1, output);\n }\n node = node->left;\n }\n }\n\n node = split_node->right;\n while (node != nullptr) {\n //std::cout << \"right: \" << node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[node->idx].data)) {\n output.emplace_back(node->idx);\n }\n if (DataType::compare(this->data[node->idx], high, dim)) {\n if (dim + 1 == this->dim) {\n report_subtree(node->left, output);\n } else if (node->left != nullptr) {\n query(node->left->assoc, query_region, dim + 1, output);\n }\n node = node->right;\n } else {\n node = node->left;\n }\n }\n }\n\n int count_query(RangeTreeNode *node, Region<T> &query_region, size_t dim) const {\n if (node == nullptr) {\n return 0;\n }\n T low_, high_;\n low_.fill(this->min_range), high_.fill(this->max_range);\n low_[dim] = query_region.low[dim], high_[dim] = query_region.high[dim];\n DataType low(low_), high(high_);\n\n RangeTreeNode *split_node = this->find_split_node(node, dim, low, high);\n\n if (split_node == nullptr) {\n return 0;\n }\n\n int node_count = 0;\n //std::cout << \"split: \" << split_node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[split_node->idx].data)) {\n node_count++;\n }\n\n node = split_node->left;\n while (node != nullptr) {\n //std::cout << \"left: \" << node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[node->idx].data)) {\n node_count++;\n }\n\n if (DataType::compare(this->data[node->idx], low, dim)) {\n node = node->right;\n } else {\n if (dim + 1 == this->dim) {\n node_count += node->right == nullptr ? 0 : node->right->node_count;\n } else if (node->right != nullptr) {\n node_count += count_query(node->right->assoc, query_region, dim + 1);\n }\n node = node->left;\n }\n }\n\n node = split_node->right;\n while (node != nullptr) {\n //std::cout << \"right: \" << node->idx << ' ' << dim << std::endl;\n if (query_region.include(this->data[node->idx].data)) {\n node_count++;\n }\n if (DataType::compare(this->data[node->idx], high, dim)) {\n if (dim + 1 == this->dim) {\n node_count += node->left == nullptr ? 0 : node->left->node_count;\n } else if (node->left != nullptr) {\n node_count += count_query(node->left->assoc, query_region, dim + 1);\n }\n node = node->right;\n } else {\n node = node->left;\n }\n }\n\n return node_count;\n }\n\n};\n\nint main() {\n // use scanf in CodeForces!\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int N, M; cin >> N >> M;\n Graph<ll> g1(N), g2(N);\n REP(i, M) {\n int a, b; ll c; cin >> a >> b >> c;\n g1.add_edge(a, b, c);\n g1.add_edge(b, a, c);\n g2.add_edge(a, b, c);\n g2.add_edge(b, a, c);\n }\n\n ShortestPath<ll> sp1(g1), sp2(g2);\n sp1.run(0), sp2.run(N - 1);\n\n using DataType = Data<std::array<ll, 3>>;\n RangeTree<DataType> range_tree(2, 0);\n std::vector<DataType> points;\n REP(i, N) {\n DataType::type tmp = {sp1.dist[i], sp2.dist[i], i};\n points.emplace_back(DataType(tmp));\n }\n range_tree.build(points);\n int Q; cin >> Q;\n REP(i, Q) {\n ll fs, gs; cin >> fs >> gs;\n DataType::type low = {range_tree.min_range, gs, 0}, high = {fs, range_tree.max_range, N - 1};\n Region<DataType::type> query_region(low, high);\n //std::vector<size_t> out;\n //range_tree.query(query_region, out);\n //std::cout << out.size() << std::endl;\n std::cout << range_tree.count_query(query_region) << std::endl;\n }\n return 0;\n}", "accuracy": 0.9777777777777777, "time_ms": 740, "memory_kb": 121660, "score_of_the_acc": -1.5627, "final_rank": 13 }, { "submission_id": "aoj_1505_3117496", "code_snippet": "#include <stdio.h>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <queue>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef pair<ll,ll> pll;\ntypedef pair<int,int> pi;\n#define F first\n#define S second\n#define PB push_back\nconst int N=1e5+10;\nconst ll INF=1LL<<60;\nll now=12394129385729LL;\nstruct node{\n node* l;\n node* r;\n int size;\n ll val;\n node(ll n){\n l=r=nullptr;\n size=1;\n val=n;\n }\n void pull(){\n size=1;\n if(l)size+=l->size;\n if(r)size+=r->size;\n return ;\n }\n};\nbool ran(int a,int b){\n now^=12394817923847LL;\n now+=12937417834292LL;\n return (now%=(a+b))>=b;\n}\nnode* merge(node* a,node* b){\n if(!a)return b;\n if(!b)return a;\n if(ran(a->size,b->size)){\n a->r=merge(a->r,b);\n a->pull();\n return a;\n }\n else{\n b->l=merge(a,b->l);\n b->pull();\n return b;\n }\n}\nvoid split(node* s,ll x,node* &a,node* &b){\n if(!s){\n a=b=nullptr;\n return ;\n }\n if(x>s->val){\n a=s;\n split(s->r,x,a->r,b);\n a->pull();\n }\n else{\n b=s;\n split(s->l,x,a,b->l);\n b->pull();\n }\n return ;\n}\nint query(node* s,ll x){\n if(!s)return 0;\n int ans=0;\n if(s->val>=x){\n if(s->l)ans+=query(s->l,x);\n ans++;\n if(s->r)ans+=s->r->size;\n }\n else if(s->r)ans+=query(s->r,x);\n return ans;\n}\nint main(){\n int n,m,q,a,b,c,ans[N];\n node* root=nullptr,*l,*r;\n priority_queue<pll,vector<pll>,greater<pll>>pq;\n pll temp,s[N];\n pair<pll,int> ask[N];\n bitset<N> went;\n vector<pi>graph[N];\n scanf(\"%d%d\",&n,&m);\n while(m--){\n scanf(\"%d%d%d\",&a,&b,&c);\n graph[a].PB({b,c});\n graph[b].PB({a,c});\n }\n for(int i=0;i<n;i++)s[i]={INF,INF};\n s[0].F=0;\n pq.push({0,0});\n went.reset();\n while(!pq.empty()){\n temp=pq.top();\n pq.pop();\n if(went[temp.S])continue;\n went[temp.S]=true;\n for(pi i:graph[temp.S])if(!went[i.F]&&s[temp.S].F+i.S<s[i.F].F){\n s[i.F].F=s[temp.S].F+i.S;\n pq.push({s[i.F].F,i.F});\n }\n }\n went.reset();\n pq.push({0,n-1});\n s[n-1].S=0;\n while(!pq.empty()){\n temp=pq.top();\n pq.pop();\n if(went[temp.S])continue;\n went[temp.S]=true;\n for(pi i:graph[temp.S])if(!went[i.F]&&s[temp.S].S+i.S<s[i.F].S){\n s[i.F].S=s[temp.S].S+i.S;\n pq.push({s[i.F].S,i.F});\n }\n }\n sort(s,s+n);\n scanf(\"%d\",&q);\n for(int i=0;i<q;i++){\n scanf(\"%lld%lld\",&ask[i].F.F,&ask[i].F.S);\n ask[i].S=i;\n }\n sort(ask,ask+q);\n a=0;\n for(int i=0;i<q;i++){\n while(a<n){\n if(s[a].F<=ask[i].F.F){\n split(root,s[a].S,l,r);\n root=merge(l,new node(s[a].S));\n root=merge(root,r);\n a++;\n }\n else break;\n }\n ans[ask[i].S]=query(root,ask[i].F.S);\n }\n for(int i=0;i<q;i++)printf(\"%d\\n\",ans[i]);\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 17760, "score_of_the_acc": -0.2074, "final_rank": 3 }, { "submission_id": "aoj_1505_3083223", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct SegTree{\n int n; vector<int> dat;\n //初期化\n SegTree(int _n){\n n=1;\n while(n<_n) n*=2;\n dat=vector<int>(2*n-1,0);\n }\n\n void add(int k, int a){\n k+=n-1;\n dat[k] += a;\n //更新\n while(k>0){\n k=(k-1)/2;\n dat[k]=dat[2*k+1]+dat[2*k+2];\n }\n }\n //内部的に投げられるクエリ\n int _query(int a, int b, int k, int l, int r){\n if(r<=a || b<=l) return 0;\n\n if(a<=l && r<=b) return dat[k];\n\n int vl=_query(a,b,2*k+1,l,(l+r)/2);\n int vr=_query(a,b,2*k+2,(l+r)/2,r);\n return vl+vr;\n }\n //[a,b)\n int query(int a, int b){\n return _query(a,b,0,0,n);\n }\n};\n\nconst int N = 100000;\nconst ll INF = LLONG_MAX/3;\n\nstruct edge{ int to,cost; };\nvector<edge> G[N];\n\nint main(){\n int n,m;\n scanf(\" %d %d\", &n, &m);\n rep(i,m){\n int a,b,c;\n scanf(\" %d %d %d\", &a, &b, &c);\n G[a].pb({b,c});\n G[b].pb({a,c});\n }\n\n auto dijkstra = [&](int start){\n using P = pair<ll,int>;\n vector<ll> d(n,INF);\n priority_queue<P, vector<P>, greater<P>>pq;\n d[start] = 0;\n pq.push({0,start});\n while(!pq.empty()){\n P now = pq.top();\n pq.pop();\n int v = now.se;\n if(now.fi > d[v]) continue;\n for(const auto &e:G[v]){\n if(d[e.to] > d[v]+e.cost){\n d[e.to] = d[v]+e.cost;\n pq.push({d[e.to], e.to});\n }\n }\n }\n return d;\n };\n\n vector<ll> s = dijkstra(0), g = dijkstra(n-1);\n\n using pl = pair<ll,ll>;\n using query = pair<pl,int>;\n\n vector<ll> ug;\n\n int Q;\n scanf(\" %d\", &Q);\n vector<query> q(Q);\n rep(i,Q){\n ll fs,fg;\n scanf(\" %lld %lld\", &fs, &fg);\n q[i] = {{fs,fg}, i};\n ug.pb(fg);\n }\n sort(all(q));\n\n vector<pl> v(n);\n rep(i,n){\n v[i] = {s[i],g[i]};\n ug.pb(g[i]);\n }\n sort(all(v));\n\n sort(all(ug));\n ug.erase(unique(all(ug)), ug.end());\n int UG = ug.size();\n\n SegTree st(UG);\n\n int idx = 0;\n vector<int> ans(Q);\n rep(i,Q){\n int ID = q[i].se;\n ll fs = q[i].fi.fi, fg = q[i].fi.se;\n\n while(idx<n && v[idx].fi<=fs){\n int a_idx = lower_bound(all(ug), v[idx].se)-ug.begin();\n st.add(a_idx, 1);\n ++idx;\n }\n\n int gidx = lower_bound(all(ug), fg)-ug.begin();\n ans[ID] = st.query(gidx, UG);\n }\n\n rep(i,Q) printf(\"%d\\n\", ans[i]);\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 18388, "score_of_the_acc": -0.189, "final_rank": 1 }, { "submission_id": "aoj_1505_3083220", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct SegTree{\n int n; vector<ll> dat;\n //初期化\n SegTree(int _n){\n n=1;\n while(n<_n) n*=2;\n dat=vector<ll>(2*n-1,0);\n }\n //k番目(0-indexed)の値をaに変更\n void add(int k, ll a){\n k+=n-1;\n dat[k] += a;\n //更新\n while(k>0){\n k=(k-1)/2;\n dat[k]=dat[2*k+1]+dat[2*k+2];\n }\n }\n //内部的に投げられるクエリ\n ll _query(int a, int b, int k, int l, int r){\n if(r<=a || b<=l) return 0;\n\n if(a<=l && r<=b) return dat[k];\n\n ll vl=_query(a,b,2*k+1,l,(l+r)/2);\n ll vr=_query(a,b,2*k+2,(l+r)/2,r);\n return vl+vr;\n }\n //[a,b)\n ll query(int a, int b){\n return _query(a,b,0,0,n);\n }\n};\n\nconst int N = 100000;\nconst ll INF = LLONG_MAX/3;\n\nstruct edge{ int to,cost; };\nvector<edge> G[N];\n\nint main(){\n int n,m;\n scanf(\" %d %d\", &n, &m);\n rep(i,m){\n int a,b,c;\n scanf(\" %d %d %d\", &a, &b, &c);\n G[a].pb({b,c});\n G[b].pb({a,c});\n }\n\n auto dijkstra = [&](int start){\n using P = pair<ll,int>;\n vector<ll> d(n,INF);\n priority_queue<P, vector<P>, greater<P>>pq;\n d[start] = 0;\n pq.push({0,start});\n while(!pq.empty()){\n P now = pq.top();\n pq.pop();\n int v = now.se;\n if(now.fi > d[v]) continue;\n for(const auto &e:G[v]){\n if(d[e.to] > d[v]+e.cost){\n d[e.to] = d[v]+e.cost;\n pq.push({d[e.to], e.to});\n }\n }\n }\n return d;\n };\n\n vector<ll> s = dijkstra(0), g = dijkstra(n-1);\n\n using pl = pair<ll,ll>;\n using query = pair<pl,int>;\n\n vector<ll> ug;\n\n int Q;\n scanf(\" %d\", &Q);\n vector<query> q(Q);\n rep(i,Q){\n ll fs,fg;\n scanf(\" %lld %lld\", &fs, &fg);\n q[i] = {{fs,fg}, i};\n ug.pb(fg);\n }\n sort(all(q));\n\n vector<pl> v(n);\n rep(i,n){\n v[i] = {s[i],g[i]};\n ug.pb(g[i]);\n }\n sort(all(v));\n\n sort(all(ug));\n ug.erase(unique(all(ug)), ug.end());\n int UG = ug.size();\n\n SegTree st(UG);\n\n int idx = 0;\n vector<int> ans(Q);\n rep(i,Q){\n int ID = q[i].se;\n ll fs = q[i].fi.fi, fg = q[i].fi.se;\n\n while(idx<n && v[idx].fi<=fs){\n int a_idx = lower_bound(all(ug), v[idx].se)-ug.begin();\n st.add(a_idx, 1);\n ++idx;\n }\n\n int gidx = lower_bound(all(ug), fg)-ug.begin();\n ans[ID] = st.query(gidx, UG);\n }\n\n rep(i,Q) printf(\"%d\\n\", ans[i]);\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 20400, "score_of_the_acc": -0.2062, "final_rank": 2 }, { "submission_id": "aoj_1505_2797631", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <utility>\n#include <algorithm>\n#define llint long long\n#define inf (1LL<<62)\n\nusing namespace std;\ntypedef pair<llint, llint> P;\n\nstruct edge{\n\tllint to, cost;\n\tedge(){}\n\tedge(llint a, llint b){\n\t\tto = a, cost = b;\n\t}\n};\n\nllint n, m, q;\nvector<edge> G[100005];\nllint distS[100005], distG[100005];\nvector<llint> dvec;\nvector<llint> compS, compG;\nvector<llint> point[100005];\npair<P, llint> query[100005];\nvector<P> qpoint[100005];\nllint ans[100005];\n\nllint bit[100005];\nllint sum(llint i)\n{\n\ti++;\n\tllint ret = 0;\n\twhile(i > 0){\n\t\tret += bit[i];\n\t\ti -= i & (-i);\n\t}\n\treturn ret;\n}\nvoid add(llint i, llint x)\n{\n\ti++;\n\twhile(i <= n){\n\t\tbit[i] += x;\n\t\ti += i & (-i);\n\t}\n}\n\nllint get(llint x)\n{\n\treturn upper_bound(dvec.begin(), dvec.end(), x) - dvec.begin();\n}\n\nvoid dijkstra(llint S, llint dist[])\n{\n\tfor(int i = 0; i < n; i++) dist[i] = inf;\n\tdist[S] = 0;\n\tpriority_queue< P, vector<P>, greater<P> > Q;\n\tQ.push(make_pair(0, S));\n\t\n\tllint d, v;\n\twhile(Q.size()){\n\t\td = Q.top().first;\n\t\tv = Q.top().second;\n\t\tQ.pop();\n\t\tif(dist[v] < d) continue;\n\t\tfor(int i = 0; i < G[v].size(); i++){\n\t\t\tif(dist[G[v][i].to] > d + G[v][i].cost){\n\t\t\t\tdist[G[v][i].to] = d + G[v][i].cost;\n\t\t\t\tQ.push(make_pair(dist[G[v][i].to], G[v][i].to));\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(void)\n{\n\tcin >> n >> m;\n\tllint a, b, c;\n\tfor(int i = 0; i < m; i++){\n\t\tcin >> a >> b >> c;\n\t\tG[a].push_back(edge(b, c));\n\t\tG[b].push_back(edge(a, c));\n\t}\n\t\n\tdijkstra(0, distS);\n\tdijkstra(n-1, distG);\n\t\n\tfor(int i = 0; i < n; i++) dvec.push_back(distS[i]);\n\tsort(dvec.begin(), dvec.end());\n\t\n\t\n\tfor(int i = 0; i < n; i++) compS.push_back(distS[i]);\n\tsort(compS.begin(), compS.end());\n\tcompS.erase(unique(compS.begin(), compS.end()), compS.end());\n\tfor(int i = 0; i < n; i++){\n\t\tdistS[i] = lower_bound(compS.begin(), compS.end(), distS[i]) - compS.begin();\n\t}\n\t\n\tfor(int i = 0; i < n; i++) compG.push_back(distG[i]);\n\tsort(compG.begin(), compG.end());\n\tcompG.erase(unique(compG.begin(), compG.end()), compG.end());\n\tfor(int i = 0; i < n; i++){\n\t\tdistG[i] = lower_bound(compG.begin(), compG.end(), distG[i]) - compG.begin();\n\t}\n\t\n\tfor(int i = 0; i < n; i++) point[distG[i]].push_back(distS[i]);\n\t\n\t\n\tllint q;\n\tcin >> q;\n\tfor(int i = 0; i < q; i++){\n\t\tcin >> query[i].first.first >> query[i].first.second;\n\t\tquery[i].first.second--;\n\t\tquery[i].second = i;\n\t}\n\tfor(int i = 0; i < q; i++){\n\t\tquery[i].first.first = upper_bound(compS.begin(), compS.end(), query[i].first.first) - 1 - compS.begin();\n\t\tquery[i].first.second = upper_bound(compG.begin(), compG.end(), query[i].first.second) - 1 - compG.begin();\n\t}\n\t\n\tfor(int i = 0; i < q; i++){\n\t\tif(query[i].first.first >= 0){\n\t\t\tqpoint[query[i].first.second].push_back(make_pair(query[i].first.first, query[i].second));\n\t\t}\n\t}\n\t\n\tfor(int i = 0; i < q; i++){\n\t\tif(query[i].first.second < 0){\n\t\t\tans[query[i].second] = get(compS[query[i].first.first]);\n\t\t}\n\t}\n\tfor(int i = 0; i <= compG.size(); i++){\n\t\tfor(int j = 0; j < point[i].size(); j++) add(point[i][j], 1);\n\t\tfor(int j = 0; j < qpoint[i].size(); j++){\n\t\t\tans[qpoint[i][j].second] = get(compS[query[qpoint[i][j].second].first.first]) - sum(qpoint[i][j].first);\n\t\t}\n\t}\n\tfor(int i = 0; i < q; i++) cout << ans[i] << endl;\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 28148, "score_of_the_acc": -0.431, "final_rank": 8 }, { "submission_id": "aoj_1505_2762545", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n#define all(v) (v).begin(), (v).end()\n#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)\n#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n\ntemplate<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}\ntemplate<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}\n\nusing Pi = pair<int, int>;\nusing Tapris = tuple<int, int, int>;\nusing vint = vector<int>;\n\nconst int inf = 1LL << 55;\nconst int mod = 1e9 + 7;\n\nsigned main() {\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n cout << fixed << setprecision(12);\n\n const int sqrtN = 512;\n\n int n, m;\n cin >> n >> m;\n\n struct edge {\n int to, cost;\n edge(){}\n edge(int to, int cost):to(to), cost(cost){}\n };\n vector<vector<edge> > graph(n);\n rep(i, m) {\n int a, b, c;\n cin >> a >> b >> c;\n graph[a].emplace_back(b, c);\n graph[b].emplace_back(a, c);\n }\n\n auto dijkstra = [&](int s){\n vint res(n, inf);\n res[s] = 0;\n priority_queue<Pi, vector<Pi>, greater<Pi> > que;\n que.emplace(res[s], s);\n while(!que.empty()) {\n Pi p = que.top(); que.pop();\n int v = p.second;\n if(res[v] < p.first) continue;\n for(edge& e : graph[v]) {\n\tif(e.cost+res[v] < res[e.to]) {\n\t res[e.to] = e.cost+res[v];\n\t que.emplace(res[e.to], e.to);\n\t}\n }\n }\n return res;\n };\n\n auto ds = dijkstra(0);\n auto dg = dijkstra(n-1);\n\n vint press_ds;\n rep(i, n) press_ds.push_back(ds[i]);\n\n int q;\n cin >> q;\n vector<Pi> qs;\n rep(i, q) {\n int f, g;\n cin >> f >> g;\n qs.emplace_back(f, g);\n press_ds.push_back(f);\n }\n\n sort(all(press_ds));\n press_ds.erase(unique(all(press_ds)), press_ds.end());\n\n int bucket_sz = ((int)press_ds.size()+sqrtN-1)/sqrtN;\n vector<vector<int> > bucket_dat(bucket_sz);\n vector<vector<int> > dat(press_ds.size());\n rep(i, n) {\n int p = lower_bound(all(press_ds), ds[i])-press_ds.begin();\n bucket_dat[p/sqrtN].push_back(dg[i]);\n dat[p].push_back(dg[i]);\n }\n\n rep(i, bucket_sz) sort(all(bucket_dat[i]));\n rep(i, dat.size()) sort(all(dat[i]));\n\n auto ask = [&](int a, int b, int x) {\n int res = 0;\n rep(i, bucket_sz) {\n int l = i*sqrtN, r = (i+1)*sqrtN;\n if(r <= a || b <= l) continue;\n if(a <= l && r <= b) {\n\tres += bucket_dat[i].end() - lower_bound(all(bucket_dat[i]), x);\n } else {\n\treps(j, max(a, l), min(b, r)) {\n\t res += dat[j].end() - lower_bound(all(dat[j]), x);\n\t}\n }\n }\n return res;\n };\n\n for(Pi p : qs) {\n int l = 0, r = upper_bound(all(press_ds), p.first)-press_ds.begin();\n cout << ask(l, r, p.second) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 22552, "score_of_the_acc": -0.7245, "final_rank": 11 } ]
aoj_1503_cpp
Problem D : Numbers n が与えられるので、 n 個の連続した正の整数を求めよ。 ただしすべての数が、1とその数自身以外の約数をもたなくてはならない。 Input 入力は以下のフォーマットで与えられる。 n 入力は以下の制約を満たす。 1 ≤ n ≤ 1,500 Output 最初の行に、あなたが選んだ連続した n 個の正の整数の中で一番小さいものを出力せよ。 2行目から n+1 行目に、それぞれの値に対する約数を出力せよ。 約数は1かその数自身でなければどの値を出力しても良い。 1行目に出力した数を x として、i行目には x+i-2 の約数を出力せよ。 出力する値は5,000桁を超えてはいけない。 Sample Input 1 2 Sample Output 1 8 2 3 Sample Input 2 3 Sample Output 2 8 2 3 5 Hint Sample Output 2では、8,9,10を3個の連続した整数として選んでいる。 2行目に、8の約数として2,3行目は9の約数として3,4行目には10の約数として5,を出力している。
[ { "submission_id": "aoj_1503_3430839", "code_snippet": "#include<bits/stdc++.h>\n#include <boost/multiprecision/cpp_dec_float.hpp>\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/rational.hpp>\nusing namespace std;\n\n#define lint long long\n#define P pair<int, int>\n#define LLP pair<long long, long long>\n#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)\n#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)\n#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)\n#define SORT(x) sort((x).begin(), (x).end())\n#define SORT_INV(x) sort((x).rbegin(), (x).rend())\n\nconst int IINF = 1e9 + 100;\nconst long long LLINF = 2e18 + 129;\nconst long long MOD = 1e9 + 7;\nconst int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};\nconst int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};\nconst double EPS = 1e-8;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n namespace mp = boost::multiprecision;\n using Bint = mp::cpp_int;\n\n int n;\n cin >> n;\n\n Bint fact = 1;\n for(Bint i = 1 ; i <= n + 1 ; ++i){\n fact *= i;\n }\n fact += 2;\n\n cout << fact << endl;\n for(int i = 0 ; i < n ; ++i){\n if(i % 2 == 0){\n cout << 2 << endl;\n }else{\n for(Bint j = 3 ; j <= fact ; ++j){\n if(fact % j == 0){\n cout << j << endl;\n break;\n }\n }\n }\n ++fact;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 880, "memory_kb": 3292, "score_of_the_acc": -0.995, "final_rank": 11 }, { "submission_id": "aoj_1503_2757368", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n#define double long double\nusing namespace std;\nconst int N = 100010;\nconst int INF = 1LL<<55;\nconst int mod = (1e9)+7;\nconst double EPS = 1e-8;\nconst double PI = 6.0 * asin(0.5);\ntypedef pair<int,int> P;\ntypedef long long ll;\nostream& operator<<(ostream& o,P p){\n return o<<\"(\"<<p.first<<\",\"<<p.second<<\")\";\n}\nistream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}\ntemplate<class T> istream& operator>>(istream& i,vector<T> &a){for(auto &t:a)cin>>t;return i;}\ntemplate<class T> T Max(T &a,T b){return a=max(a,b);}\ntemplate<class T> T Min(T &a,T b){return a=min(a,b);}\ntemplate<class T> void prArr(T a,string s=\" \"){int i=0;for(auto t:a)cout<<(i++?s:\"\")<<t;cout<<endl;}\n\n\nclass Int{\npublic:\n string val;\n Int():val(\"0\"){}\n Int(string num,bool reversed=0){if(!reversed)R(num); val = num;}\n Int(int num){val = to_string(num); R(val);}\n \n void R(string &a){reverse(a.begin(),a.end());}\n bool neg(const string &a)const{return a.back() == '-';}\n char& operator [] (int i){assert(i<(int)val.size());return val[i];}\n\n bool operator == (Int b) {R(b.val); return *this == b.val;}\n bool operator == (string b) {R(b);return val == b;}\n template<class T> bool operator == (T b){return *this == Int(b);}\n \n bool operator != (Int b) {R(b.val); return *this != b.val;}\n bool operator != (string b) {R(b);return !(val == b);}\n template<class T> bool operator != (T b){return *this != Int(b);}\n \n bool operator >= (Int b) {R(b.val); return *this >= b.val;}\n bool operator >= (string b) {return !(*this < b);}\n template<class T> bool operator >= (T b){return *this >= Int(b);}\n\n bool operator > (Int b) {R(b.val); return *this > b.val;}\n bool operator > (string b) {return *this>=b && *this != b;}\n template<class T> bool operator > (T b){return *this > Int(b);}\n\n bool operator <= (Int b) {R(b.val); return *this <= b.val;}\n bool operator <= (string b) {return *this<b || *this == b;}\n template<class T> bool operator <= (T b){return *this <= Int(b);}\n\n bool operator < (Int b) {R(b.val); return *this < b.val;}\n template<class T> bool operator < (T b){return *this < Int(b);}\n \n bool operator < (string b) {\n R(b);\n const string &a = val;\n if(neg(a) != neg(b)) return neg(a)? 1:0;\n if(a.size() != b.size()) return !neg(a)? (a.size()<b.size()):(a.size()>b.size());\n for(int i=a.size()-1;i>=0;i--)if(a[i] != b[i]) return !neg(a)? a[i] < b[i]: a[i] > b[i];\n return 0;\n }\n \n Int operator + (string b){\n R(b);\n string a = val;\n string fa = neg(a)? \"-\":\"\";\n string fb = neg(b)? \"-\":\"\";\n if(neg(a)) a.pop_back();\n if(neg(b)) b.pop_back();\n if(Int(a,1) < Int(b,1)) swap(a,b),swap(fa,fb);\n \n if(fa == fb){\n a += '0';\n for(int i=0;i<(int)a.size()-1;i++){\n if(i < (int)b.size()) a[i] += b[i]-'0';\n a[i+1] += (a[i]-'0')/10;\n a[i] = (a[i]-'0')%10 + '0';\n }\n if(a.size() > 1 && a.back() == '0') a.pop_back();\n if((int)a.size() > 1 && a.back() == '0') a.pop_back();\n if(a != \"0\") a += fa;\n }\n else {\n for(int i=0;i<(int)a.size();i++){\n if(i < (int)b.size()) a[i] -= b[i]-'0';\n if(a[i]-'0' < 0) a[i]+=10, a[i+1]--; \n }\n while((int)a.size()>1 && a.back() == '0') a.pop_back();\n if(a != \"0\") a += fa;\n }\n return Int(a,1);\n }\n\n Int operator * (string b){\n R(b);\n vector<int> res;\n string a = val;\n string fa = neg(a)? \"-\":\"\";\n string fb = neg(b)? \"-\":\"\";\n if(neg(a)) a.pop_back();\n if(neg(b)) b.pop_back();\n if(a.size() < b.size()) swap(a,b), swap(fa,fb);\n if(a == \"0\" || b == \"0\") return Int(\"0\");\n \n for(int j=0;j<(int)b.size();j++)\n for(int i=0;i<(int)a.size();i++){\n if(i+j >= (int)res.size()) res.push_back(0);\n res[i+j] += (a[i]-'0') * (b[j]-'0');\n }\n for(int i=0; i <(int)res.size(); i++) {\n if(res[i]/10 == 0) continue;\n if(i + 1 >= (int)res.size()) res.push_back(0);\n res[i+1] += res[i]/10, res[i]=res[i]%10;\n }\n string sres;\n for(int i:res) sres += char(i + '0');\n if(fa != fb) sres += '-';\n return Int(sres,1);\n }\n\n Int div(string b,int flg){\n assert(b != \"0\");\n string a = val;\n string fa = neg(a)? \"-\":\"\";\n string fb = b[0]=='-'? \"-\":\"\";\n if(neg(a)) a.pop_back();\n if(b[0]=='-') b.erase(b.begin());\n Int mod(\"0\");\n string d=\"0\";\n for(int i=(int)a.size()-1;i>=0;i--){\n mod = mod * 10 + (a[i] - '0');\n while(mod >= b) mod -= b, d.back()+=1;\n if(i && d != \"0\") d += '0'; \n }\n if(flg == 0 && d != \"0\" && (fa != fb)) d = \"-\" + d;\n if(flg == 1 && mod != \"0\" && (fa == \"-\")) mod *= -1;\n return flg==0? d:mod;\n }\n \n Int operator + (Int b){R(b.val); return *this + b.val;}\n template<class T> Int operator + (T b){return *this + Int(b);}\n \n Int operator * (Int b){R(b.val); return *this * b.val;}\n template<class T> Int operator * (T b){return *this * Int(b);}\n\n Int operator - (string b){b = (b[0] == '-')? b.substr(1):\"-\" + b;return *this + b;}\n Int operator - (Int b){R(b.val); return *this - b.val;} \n template<class T> Int operator - (T b){return *this - Int(b);}\n \n Int operator / (string b){return div(b,0);}\n Int operator / (Int b){R(b.val);return div(b.val,0);}\n template<class T> Int operator / (T b){return *this / Int(b);}\n \n Int operator % (string b){return div(b,1);}\n Int operator % (Int b){R(b.val);return div(b.val,1);}\n template<class T> Int operator % (T b){return *this % Int(b);}\n \n Int operator += (Int b){return *this = *this+b;}\n Int operator += (string b){return *this = *this+b;}\n template<class T> Int operator += (T b){return *this += Int(b);}\n \n Int operator -= (Int b){return *this = *this-b;}\n Int operator -= (string b){return *this = *this-b;}\n template<class T> Int operator -= (T b){return *this -= Int(b);}\n \n Int operator *= (Int b){return *this = *this*b;}\n Int operator *= (string b){return *this = *this*b;}\n template<class T> Int operator *= (T b){return *this *= Int(b);}\n \n Int operator /= (string b){return *this = *this/b;}\n Int operator /= (Int b){return *this = *this/b;}\n template<class T> Int operator /= (T b){return *this /= Int(b);}\n \n Int operator %= (string b){return *this = *this % b;}\n Int operator %= (Int b){return *this=*this%b;}\n template<class T> Int operator %= (T b){return *this %= Int(b);}\n \n Int operator ++(){return *this += 1;}\n Int operator --(){return *this -= 1;}\n\n#ifdef int\n#undef int\n#define eraseIntDefine\n#endif\n Int operator ++(int){\n Int tmp = *this;\n *this += 1;\n return tmp;\n }\n \n Int operator --(int){\n Int tmp = *this;\n *this -= 1;\n return tmp;\n }\n#ifdef eraseIntDefine\n#define int long long\n#endif\n\n friend ostream& operator << (ostream& os,const Int a){\n for(int i=(int)a.val.size()-1;i>=0;i--) os<<a.val[i];\n return os;\n }\n\n friend istream& operator >> (istream& is,Int &a){\n string num;\n is>>num;\n a = Int(num);\n return is;\n }\n friend string to_string(Int a){reverse(a.val.begin(),a.val.end());return a.val;}\n};\n\n\nvector<int>getPrime(int n){\n vector<bool>used(n+1,0);\n\n for(ll i=2;i*i<=n;i++)\n if(!used[i])for(int j=2;j<=n/i;j++)used[i*j]=1;\n \n vector<int> res;\n for(int i=2;i<=n;i++)if(!used[i])res.push_back(i);\n\n return res;\n}\n\n\n\nsigned main(){\n int n;\n cin>>n;\n\n Int num(\"1\");\n for(int i=2;i<=1502;i++) num *= i;\n\n cout<<num+2<<endl;\n for(int i=0;i<n;i++){\n cout<<2+i<<endl;\n }\n\n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3412, "score_of_the_acc": -0.0522, "final_rank": 7 }, { "submission_id": "aoj_1503_2757283", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nstring sub1(string a){\n assert(a!=\"0\");\n \n int as=a.size();\n int len=as+5;\n vector<int> A(len,0);\n for(int i=0;i<as;i++)A[i]=a[as-1-i]-'0';\n \n A[0]--;\n for(int i=0;i+1<len;i++){\n if(A[i]<0){\n A[i+1]--;\n A[i]+=10;\n }\n A[i+1]+=A[i]/10;\n A[i]%=10;\n }\n \n string res=\"\";\n for(int i=len-1;i>=0;i--){\n if(res.size()==0&&A[i]==0)continue;\n res.push_back('0'+A[i]);\n }\n return res;\n}\n\nstring add1(string a){\n int as=a.size();\n int len=as+5;\n vector<int> A(len,0);\n for(int i=0;i<as;i++)A[i]=a[as-1-i]-'0';\n \n A[0]++;\n for(int i=0;i+1<len;i++){\n A[i+1]+=A[i]/10;\n A[i]%=10;\n }\n string res=\"\";\n for(int i=len-1;i>=0;i--){\n if(res.size()==0&&A[i]==0)continue;\n res.push_back('0'+A[i]);\n }\n return res;\n}\n\n\nstring mul(string a,string b){\n if(a==\"0\"||b==\"0\")return \"0\"; \n int as=a.size(),bs=b.size();\n int len=as+bs;\n vector<int> A(as),B(bs),C(len);\n for(int i=0;i<as;i++)A[i]=a[as-1-i]-'0'; \n for(int i=0;i<bs;i++)B[i]=b[bs-1-i]-'0';\n \n for(int i=0;i<as;i++)\n for(int j=0;j<bs;j++)\n C[i+j]+=A[i]*B[j];\n \n for(int i=0;i+1<len;i++){\n C[i+1]+=C[i]/10;\n C[i]%=10;\n }\n \n string res=\"\";\n for(int i=len-1;i>=0;i--){\n if(res.size()==0&&C[i]==0)continue;\n res.push_back('0'+C[i]);\n }\n return res;\n}\n\nstring i2s( int x ){\n stringstream ss;\n ss<<x;\n string res;\n ss>>res;\n return res;\n}\n\nconst int MAX = 10000000;\nbool isP[MAX];\nint isQ[MAX];\n\nint main(){\n int n;\n cin>>n;\n \n if(n==1){\n cout<<100<<endl;\n cout<<2<<endl;\n return 0;\n }\n \n for(int i=2;i<MAX;i++){\n isP[i]=true;\n isQ[i]=i;\n }\n \n for(int i=2;i*i<MAX;i++){\n if(!isP[i])continue;\n for(int j=i*i;j<MAX;j+=i){\n isP[j]=false;\n isQ[j]=i;\n }\n }\n \n vector<int> primes; \n\n for(int i=2;i<=n;i++){\n if(isP[i]){\n primes.push_back(i);\n }\n }\n\n string base=\"100\";\n for(int i=0;i<(int)primes.size();i++){\n string str=i2s(primes[i]);\n base = mul ( base, str);\n }\n\n string X = add1(base);\n string Y = sub1(base);\n\n\n \n base = mul(X,Y);\n base = add1(base);\n\n \n vector<string> ans;\n ans.push_back(\"2\");\n base=sub1(base);\n ans.push_back(X);\n for(int i=0;i<n-2;i++){\n base = sub1(base);\n ans.push_back( i2s( isQ[i+2]) );\n }\n cout<<base<<endl;\n reverse(ans.begin(),ans.end());\n for(int i=0;i<ans.size();i++)cout<<ans[i]<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 52096, "score_of_the_acc": -0.7659, "final_rank": 10 }, { "submission_id": "aoj_1503_2169993", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\n\nbool is_prime[10000001];\nint p(int n) {\n\tfor (int i = 2;; i++) {\n\t\tif (n%i == 0)return i;\n\t}\n}\nint main() {\n\tmemset(is_prime, 1, sizeof(is_prime));\n\tis_prime[0] = is_prime[1] = false;\n\tfor (int i = 2; i <= 10000000; i++) {\n\t\tif (is_prime[i]) {\n\t\t\tfor (int j = i * 2; j <= 10000000; j += i)is_prime[j] = false;\n\t\t}\n\t}\n\tint n; scanf(\"%d\", &n);\n\tint cnt = 0;\n\tfor (int i = 4; i <= 100000000; i++) {\n\t\tif (!is_prime[i])cnt++;\n\t\telse cnt = 0;\n\t\tif (cnt == n) {\n\t\t\tprintf(\"%d\\n\", i - cnt + 1);\n\t\t\tfor (int j = i - cnt + 1; j <= i; j++)printf(\"%d\\n\", p(j));\n\t\t\tbreak;\n\t\t}\n\t}\n}", "accuracy": 0.43333333333333335, "time_ms": 260, "memory_kb": 12964, "score_of_the_acc": -0.3799, "final_rank": 15 }, { "submission_id": "aoj_1503_2151237", "code_snippet": "#include<iostream>\n#include<map>\n#include<string>\n#include<algorithm>\nusing namespace std;\nbool a[10000000];\nint main() {\n\tfill(a, a + 10000000, true);\n\tfor (int i = 2; i < 10000000; i++) {\n\t\tif (a[i]) {\n\t\t\tint j = 2;\n\t\t\twhile (i*j < 10000000) {\n\t\t\t\ta[i*j] = false;\n\t\t\t\tj++;\n\t\t\t}\n\t\t}\n\t}\n\tint n;\n\tcin >> n;\n\tint b=8;\n\twhile (true) {\n\t\tfor (int i = b; i < b + n; i++) {\n\t\t\tif (a[i]) {\n\t\t\t\tgoto stop;\n\t\t\t}\n\t\t}\n\t\tbreak;\n\tstop:;\n\t\tb++;\n\t}\n\tcout << b << endl;\n\tfor (int i = b; i < b + n; i++) {\n\t\tfor (int j = 2; j < i; j++) {\n\t\t\tif (i%j == 0) {\n\t\t\t\tcout << j << endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}", "accuracy": 0.43333333333333335, "time_ms": 340, "memory_kb": 12868, "score_of_the_acc": -0.469, "final_rank": 16 }, { "submission_id": "aoj_1503_1718685", "code_snippet": "#include<iostream>\nusing namespace std;\n#define MAX_P 20000\nint n; int dp[5010][MAX_P];\nvoid fact(int n) {\n\tdp[0][0] = 1;\n\tfor (int i = 1; i <= n; i++) {\n\t\tfor (int j = 0; j < MAX_P; j++) {\n\t\t\tdp[i][j] += dp[i - 1][j] * i;\n\t\t\tif (dp[i][j] >= 10) {\n\t\t\t\tdp[i][j + 1] += dp[i][j] / 10; dp[i][j] %= 10;\n\t\t\t}\n\t\t}\n\t}\n\tdp[n][0] += 2;\n\tfor (int j = 0; j < MAX_P; j++) {\n\t\tif (dp[n][j] >= 10) { dp[n][j + 1] += dp[n][j] / 10; dp[n][j] %= 10; }\n\t}\n}\nint main() {\n\tcin >> n; fact(n + 1); int ok = 0;\n\tfor (int i = MAX_P - 1; i >= 0; i--) {\n\t\tif (dp[n + 1][i] >= 1) { ok = 1; }\n\t\tif (ok == 1) { cout << dp[n + 1][i]; }\n\t}\n\tcout << endl;\n\tfor (int i = 2; i < n + 2; i++) { cout << i << endl; }\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 119868, "score_of_the_acc": -1.0449, "final_rank": 12 }, { "submission_id": "aoj_1503_1544932", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nvector<int>a;\n\nint N;\nbool prime[2000];\n\n\nvoid mul(int x){\n int up=0;\n for(int i=0;i<a.size()-1;i++){\n a[i]=a[i]*x+up;\n up=a[i]/10;\n a[i]%=10;\n }\n}\n\nvoid add(int x){\n int up=x;\n for(int i=0;i<a.size();i++){\n a[i]+=up;\n up=a[i]/10;\n a[i]%=10;\n }\n}\n\nvoid print(){\n int pos=0;\n for(int i=0;i<a.size();i++)if(a[i])pos=i;\n for(int i=pos;i>=0;i--)cout<<a[i];cout<<endl;\n}\n\nint main(){\n scanf(\"%d\",&N);\n a.resize(5000,0);\n a[0]=1;\n\n fill_n(prime,2000,true);\n prime[0]=prime[1]=false;\n for(int i=2;i<2000;i++){\n if(!prime[i])continue;\n for(int j=i*2;j<2000;j+=i)prime[j]=false;\n }\n\n for(int i=2;i<2000;i++)if(prime[i])mul(i);\n\n add(2);\n print();\n\n for(int i=2;i<N+2;i++){\n for(int j=2;j<=i;j++)if(i%j==0){\n cout<<j<<endl;\n break;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1232, "score_of_the_acc": -0.0001, "final_rank": 1 }, { "submission_id": "aoj_1503_746861", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <iomanip>\n#include <algorithm>\n#include <functional>\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vint;\ntypedef vector<ll> vll;\ntypedef pair<int,int> pint;\n\n#define DE 1\n#define FI first\n#define SE second\n#define PB push_back\n#define MP make_pair\n#define ALL(s) (s).begin(),(s).end()\n#define REP(i,n) for (int i = 0; i < (int)(n); ++i)\n#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)\n#define COUT(x) cout<<#x<<\" = \"<<(x)<<\" (L\"<<__LINE__<<\")\"<<endl\n\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<\", \"<<P.second<<'>';}\ntemplate<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<\"{ \";for(int i=0;i<P.size();++i){if(i>0)s<<\", \";s<<P[i];}return s<<\" }\"<<endl;}\ntemplate<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<\"{ \";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<\", \";s<<'<'<<it->first<<\"->\"<<it->second<<'>';}return s<<\" }\"<<endl;}\n\n\n\nconst int MAX_D = 10000;\nconst long long B = 10000;\nconst int LB = 4;\nstruct Bigint {\n int size;\n long long digit[MAX_D/LB];\n \n Bigint(int size = 1, long long a = 0) : size(size) {\n memset(digit, 0, sizeof(digit));\n digit[0] = a;\n }\n Bigint normalize();\n long long tolong();\n string tostr();\n inline const Bigint& operator = (long long a);\n inline const Bigint& operator = (const string &s);\n inline const Bigint& operator += (const Bigint& x);\n inline const Bigint& operator += (long long x);\n inline const Bigint& operator -= (const Bigint& x);\n inline const Bigint& operator -= (long long x);\n inline const Bigint& operator *= (const Bigint& x);\n inline const Bigint& operator *= (long long x);\n inline const Bigint& operator /= (const Bigint& x);\n inline const Bigint& operator /= (long long x);\n inline const Bigint& operator %= (const Bigint& x);\n inline const Bigint& operator %= (long long x);\n};\n\nBigint Bigint::normalize() {\n long long c = 0;\n for (int i = 0; i < (*this).size; ++i) {\n while ((*this).digit[i] < 0) {\n (*this).digit[i+1] -= 1;\n (*this).digit[i] += B;\n }\n long long a = (*this).digit[i] + c;\n (*this).digit[i] = a % B;\n c = a / B;\n }\n for (; c > 0; c /= B) (*this).digit[(*this).size++] = c % B;\n while ((*this).size > 1 && (*this).digit[(*this).size-1] == 0) --(*this).size;\n return *this;\n}\nlong long Bigint::tolong() {\n long long res = 0;\n for (int i = LB; i >= 0; --i) {\n res *= B;\n res += (*this).digit[i];\n }\n return res;\n}\nstring Bigint::tostr() {\n stringstream ss;\n ss << (*this).digit[(*this).size-1];\n for (int i = (*this).size-2; i >= 0; --i)\n ss << setw(LB) << setfill('0') << (*this).digit[i];\n return ss.str();\n}\nBigint tobig(long long a) {\n return Bigint(1, a).normalize();\n}\nBigint tobig(const string &s) {\n Bigint x;\n int i = s.size() % LB;\n if (i > 0) i -= LB;\n for (; i < (int)s.size(); i += LB) {\n long long a = 0;\n for (int j = 0; j < LB; ++j)\n a = 10 * a + (i + j >= 0 ? s[i+j] - '0' : 0);\n x.digit[x.size++] = a;\n }\n reverse(x.digit, x.digit+x.size);\n return x.normalize();\n}\ninline const Bigint& Bigint::operator = (long long a) {\n *this = tobig(a);\n return *this;\n}\ninline const Bigint& Bigint::operator = (const string &s) {\n *this = tobig(s);\n return *this;\n}\nostream &operator << (ostream &os, Bigint x) {\n os << x.digit[x.size-1];\n for (int i = x.size-2; i >= 0; --i)\n os << setw(LB) << setfill('0') << x.digit[i];\n return os;\n}\nistream &operator >> (istream &is, Bigint &x) {\n string s; is >> s;\n x = tobig(s);\n return is;\n} \nbool operator < (Bigint x, Bigint y) {\n if (x.size != y.size) return x.size < y.size;\n for (int i = x.size-1; i >= 0; --i)\n if (x.digit[i] != y.digit[i]) return x.digit[i] < y.digit[i];\n return false;\n}\nbool operator > (Bigint x, Bigint y) { \n return y < x;\n}\nbool operator <= (Bigint x, Bigint y) { \n return !(y < x); \n}\nbool operator >= (Bigint x, Bigint y) { \n return !(x < y); \n}\nbool operator != (Bigint x, Bigint y) { \n return x < y || y < x; \n}\nbool operator == (Bigint x, Bigint y) { \n return !(x < y) && !(y < x); \n}\nBigint operator + (Bigint x, Bigint y) {\n if (x.size < y.size) x.size = y.size;\n for (int i = 0; i < y.size; ++i)\n x.digit[i] += y.digit[i];\n return x.normalize();\n}\nBigint operator + (Bigint x, long long y) {\n Bigint Y; Y = y;\n return x + Y;\n}\nBigint operator - (Bigint x, Bigint y) {\n for (int i = 0; i < y.size; ++i)\n x.digit[i] -= y.digit[i];\n return x.normalize();\n}\nBigint operator - (Bigint x, long long y) {\n Bigint Y; Y = y;\n return x - Y;\n}\nBigint operator * (Bigint x, Bigint y) {\n Bigint z(x.size + y.size);\n for (int i = 0; i < x.size; ++i)\n for (int j = 0; j < y.size; ++j)\n z.digit[i+j] += x.digit[i] * y.digit[j];\n return z.normalize();\n}\nBigint operator * (Bigint x, long long a) {\n for (int i = 0; i < x.size; ++i)\n x.digit[i] *= a;\n return x.normalize();\n}\npair<Bigint, long long> divmod(Bigint x, long long a) {\n long long c = 0, t;\n for (int i = x.size-1; i >= 0; --i) {\n t = B * c + x.digit[i];\n x.digit[i] = t / a;\n c = t % a;\n }\n x.normalize();\n return pair<Bigint, long long>(x, c);\n}\nBigint operator / (Bigint x, long long a) {\n return divmod(x, a).first;\n}\nlong long operator % (Bigint x, long long a) {\n return divmod(x, a).second;\n}\npair<Bigint, Bigint> divmod(Bigint x, Bigint y) {\n Bigint zero(1, 0);\n if (x.size < y.size) return pair<Bigint, Bigint>(zero, x);\n int F = B / (y.digit[y.size-1] + 1);\n x = x * F; y = y * F;\n Bigint z(x.size - y.size + 1);\n for (int k = z.size-1, i = x.size-1; k >= 0; --k, --i) {\n z.digit[k] = (i+1 < x.size ? x.digit[i+1] : 0) * B + x.digit[i];\n z.digit[k] /= y.digit[y.size-1];\n Bigint t(k + y.size);\n for (int m = 0; m < y.size; ++m)\n t.digit[k+m] = z.digit[k] * y.digit[m];\n t.normalize();\n while (x < t) {\n z.digit[k] -= 1;\n for (int m = 0; m < y.size; ++m)\n t.digit[k+m] -= y.digit[m];\n t.normalize();\n }\n x = x - t;\n }\n return pair<Bigint, Bigint>(z.normalize(), x / F);\n}\nBigint operator / (Bigint x, Bigint y) {\n return divmod(x, y).first;\n}\nBigint operator % (Bigint x, Bigint y) {\n return divmod(x, y).second;\n}\nBigint power(Bigint x, long long n) {\n Bigint res(1, 1);\n while (n > 0) {\n if (n & 1) res = res * x;\n x = x * x;\n n >>= 1;\n }\n return res;\n}\ninline const Bigint& Bigint::operator += (const Bigint& x) {*this = *this + x; return *this;}\ninline const Bigint& Bigint::operator += (long long x) {*this = *this + x; return *this;}\ninline const Bigint& Bigint::operator -= (const Bigint& x) {*this = *this - x; return *this;}\ninline const Bigint& Bigint::operator -= (long long x) {*this = *this + x; return *this;}\ninline const Bigint& Bigint::operator *= (const Bigint& x) {*this = *this * x; return *this;}\ninline const Bigint& Bigint::operator *= (long long x) {*this = *this * x; return *this;}\ninline const Bigint& Bigint::operator /= (const Bigint& x) {*this = *this / x; return *this;}\ninline const Bigint& Bigint::operator /= (long long x) {*this = *this / x; return *this;}\ninline const Bigint& Bigint::operator %= (const Bigint& x) {*this = *this % x; return *this;}\ninline const Bigint& Bigint::operator %= (long long x) {*this = *this % x; return *this;}\n\n\n\nint n;\n\nint main() {\n cin >> n;\n Bigint res; res = 1;\n for (ll i = 1; i <= n+1; ++i) {\n res *= i;\n }\n res += 2;\n cout << res << endl;\n for (int i = 2; i <= n+1; ++i) cout << i << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1320, "score_of_the_acc": -0.0008, "final_rank": 4 }, { "submission_id": "aoj_1503_726788", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n\n#include <iostream>\n#include <cstdio>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P> PP;\n \nstatic const double EPS = 1e-8;\n \nstatic const int tx[] = {0,1,0,-1};\nstatic const int ty[] = {-1,0,1,0};\n\nstatic const int FACT_MAX = 1500;\n\nvoid multiply(int res[5001],int num){\n int carry = 0;\n for(int i=0;i<5001;i++){\n int sum = res[i] * num + carry;\n carry = sum / 10;\n res[i] = sum % 10;\n }\n}\n\nvoid add(int res[5001],int num){\n\n int tmp[5001];\n memset(tmp,0,sizeof(tmp));\n\n for(int i=0;i<5001;i++){\n tmp[i] = num % 10;\n num /= 10;\n }\n \n int carry = 0;\n for(int i=0;i<5001;i++){\n res[i] += tmp[i] + carry;\n carry = res[i] / 10;\n res[i] %= 10;\n }\n}\n\nvoid printNum(int res[5001]){\n bool flag = false;\n for(int i=5000;i>=0;i--){\n if(res[i] != 0){\n flag = true;\n }\n if(flag){\n printf(\"%d\",res[i]);\n }\n }\n printf(\"\\n\");\n}\n\nint main(){\n\n int n;\n while(~scanf(\"%d\",&n)){\n int res[5001];\n memset(res,0,sizeof(res));\n res[0] = 1;\n for(int i=1;i<=FACT_MAX;i++){\n multiply(res,i);\n }\n\n add(res,2);\n printNum(res);\n for(int i=0;i<n;i++){\n printf(\"%d\\n\",i+2);\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1236, "score_of_the_acc": -0.0226, "final_rank": 5 }, { "submission_id": "aoj_1503_726785", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n\n#include <iostream>\n#include <cstdio>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P> PP;\n \nstatic const double EPS = 1e-8;\n \nstatic const int tx[] = {0,1,0,-1};\nstatic const int ty[] = {-1,0,1,0};\n\nstatic const int FACT_MAX = 1550;\n\nvoid multiply(int res[5001],int num){\n int carry = 0;\n for(int i=0;i<5001;i++){\n int sum = res[i] * num + carry;\n carry = sum / 10;\n res[i] = sum % 10;\n }\n}\n\nvoid add(int res[5001],int num){\n\n int tmp[5001];\n memset(tmp,0,sizeof(tmp));\n\n for(int i=0;i<5001;i++){\n tmp[i] = num % 10;\n num /= 10;\n }\n \n int carry = 0;\n for(int i=0;i<5001;i++){\n res[i] += tmp[i] + carry;\n carry = res[i] / 10;\n res[i] %= 10;\n }\n}\n\nvoid printNum(int res[5001]){\n bool flag = false;\n for(int i=5000;i>=0;i--){\n if(res[i] != 0){\n flag = true;\n }\n if(flag){\n printf(\"%d\",res[i]);\n }\n }\n printf(\"\\n\");\n}\n\nint main(){\n\n int n;\n while(~scanf(\"%d\",&n)){\n int res[5001];\n memset(res,0,sizeof(res));\n res[0] = 1;\n for(int i=1;i<=FACT_MAX;i++){\n multiply(res,i);\n }\n\n printNum(res);\n for(int i=0;i<n;i++){\n printf(\"%d\\n\",i+2);\n }\n }\n}", "accuracy": 0.03333333333333333, "time_ms": 20, "memory_kb": 1220, "score_of_the_acc": -0.0112, "final_rank": 18 }, { "submission_id": "aoj_1503_726783", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n\n#include <iostream>\n#include <cstdio>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P> PP;\n \nstatic const double EPS = 1e-8;\n \nstatic const int tx[] = {0,1,0,-1};\nstatic const int ty[] = {-1,0,1,0};\n\nstatic const int FACT_MAX = 1500;\n\nvoid multiply(int res[5001],int num){\n int carry = 0;\n for(int i=0;i<5001;i++){\n int sum = res[i] * num + carry;\n carry = sum / 10;\n res[i] = sum % 10;\n }\n}\n\nvoid add(int res[5001],int num){\n\n int tmp[5001];\n memset(tmp,0,sizeof(tmp));\n\n for(int i=0;i<5001;i++){\n tmp[i] = num % 10;\n num /= 10;\n }\n \n int carry = 0;\n for(int i=0;i<5001;i++){\n res[i] += tmp[i] + carry;\n carry = res[i] / 10;\n res[i] %= 10;\n }\n}\n\nvoid printNum(int res[5001]){\n bool flag = false;\n for(int i=5000;i>=0;i--){\n if(res[i] != 0){\n flag = true;\n }\n if(flag){\n printf(\"%d\",res[i]);\n }\n }\n printf(\"\\n\");\n}\n\nint main(){\n\n int n;\n while(~scanf(\"%d\",&n)){\n int res[5001];\n memset(res,0,sizeof(res));\n res[0] = 1;\n for(int i=1;i<=FACT_MAX;i++){\n multiply(res,i);\n }\n\n printNum(res);\n for(int i=0;i<n;i++){\n printf(\"%d\\n\",i+2);\n }\n }\n}", "accuracy": 0.03333333333333333, "time_ms": 30, "memory_kb": 1220, "score_of_the_acc": -0.0225, "final_rank": 19 }, { "submission_id": "aoj_1503_722000", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <cstring>\n\nusing namespace std;\n\ntypedef long long lli;\n\nconst int DIGITS = 1000;\nconst int BASE = 100000;\nconst int BD = 5;\n\nclass BigNum{\n lli val[DIGITS], tmp[DIGITS];\n int dig;\npublic:\n BigNum(){\n dig = 1;\n memset(val, 0, sizeof(val));\n }\n BigNum(lli x){\n dig = 0;\n for(int i=0;i<DIGITS;++i){\n if(x == 0) val[i] = 0;\n else{\n ++dig;\n val[i] = x % BASE;\n x /= BASE;\n }\n }\n }\n\n void operator + (const lli x){\n val[0] += x;\n for(int i=0;i<DIGITS;++i){\n val[i+1] += val[i] / BASE;\n val[i] %= BASE;\n if(val[i]) dig = i + 1;\n }\n }\n\n void operator * (const lli x){\n for(int i=0;i<DIGITS;++i) val[i] *= x;\n for(int i=0;i<DIGITS;++i){\n val[i+1] += val[i] / BASE;\n val[i] %= BASE;\n if(val[i]) dig = i + 1;\n }\n }\n\n string toString(){\n string res;\n for(int i=0;i<dig;++i){\n for(int j=0;j<BD;++j){\n res.push_back(val[i]%10 + '0');\n val[i] /= 10;\n }\n }\n int size;\n for(size=res.size();size>1&&res[size-1]=='0';--size);\n res = res.substr(0, size);\n reverse(res.begin(), res.end());\n return res;\n }\n};\n\nmain(){\n int n;\n cin >> n;\n BigNum x = BigNum(1);\n for(int i=2;i<=n+1;++i) x * i;\n x + 2;\n cout << x.toString() << endl;\n for(int i=0;i<n;i++) cout << i+2 << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1244, "score_of_the_acc": -0.0002, "final_rank": 2 }, { "submission_id": "aoj_1503_721996", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <cstring>\n\nusing namespace std;\n\ntypedef long long lli;\n\nconst int DIGITS = 1000;\nconst int BASE = 100000;\nconst int BD = 5;\n\nclass BigNum{\n lli val[DIGITS], tmp[DIGITS];\n int dig;\npublic:\n BigNum(){\n dig = 1;\n memset(val, 0, sizeof(val));\n }\n BigNum(lli x){\n dig = 0;\n for(int i=0;i<DIGITS;++i){\n if(x == 0) val[i] = 0;\n else{\n ++dig;\n val[i] = x % BASE;\n x /= BASE;\n }\n }\n }\n\n void operator + (const lli x){\n val[0] += x;\n for(int i=0;i<DIGITS;++i){\n val[i+1] += val[i] / BASE;\n val[i] %= BASE;\n if(val[i]) dig = i + 1;\n }\n }\n\n void operator * (const lli x){\n for(int i=0;i<DIGITS;++i) val[i] *= x;\n for(int i=0;i<DIGITS;++i){\n val[i+1] += val[i] / BASE;\n val[i] %= BASE;\n if(val[i]) dig = i + 1;\n }\n }\n\n void print(){\n vector<char> t;\n for(int i=0;i<dig;++i){\n for(int j=0;j<BD;++j){\n t.push_back(val[i]%10 + '0');\n val[i] /= 10;\n }\n }\n while(t.back() == '0') t.pop_back();\n reverse(t.begin(), t.end());\n for(int i=0;i<t.size();++i) cout << t[i];\n cout << endl;\n }\n};\n\nmain(){\n int n;\n cin >> n;\n BigNum x = BigNum(1);\n for(int i=2;i<=n+1;++i) x * i;\n x + 2;\n x.print();\n for(int i=0;i<n;i++) cout << i+2 << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1252, "score_of_the_acc": -0.0003, "final_rank": 3 }, { "submission_id": "aoj_1503_573740", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n\nusing namespace std;\n\nint n;\nbool prime[50000001];\n\nvoid seive(){\n\tmemset(prime,true,sizeof(prime));\n\tprime[0]=prime[1]=false;\n\tfor(int i=2;i<=50000000;i++){\n\t\tif(prime[i]){\n\t\t\tfor(int j=i*2;j<=50000000;j+=i)prime[j]=false;\n\t\t}\n\t}\n}\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tseive();\n\tint res=-1,cnt=0;\n\tfor(int i=4;i<=50000000;i++){\n\t\tif(!prime[i]){\n\t\t\tcnt++;\n\t\t\tif(cnt>=n){\n\t\t\t\tres=i-n+1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\telse cnt=0;\n\t}\n\t//if(res==-1)printf(\"no answer\\n\");\n\tprintf(\"%d\\n\",res);\n\tfor(int i=res;i<res+n;i++){\n\t\tfor(int j=2;j*j<=i;j++){\n\t\t\tif(i%j==0){\n\t\t\t\tprintf(\"%d\\n\",j);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 0.4666666666666667, "time_ms": 900, "memory_kb": 50024, "score_of_the_acc": -1.4113, "final_rank": 13 }, { "submission_id": "aoj_1503_573739", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n\nusing namespace std;\n\nint n;\nbool prime[5000001];\n\nvoid seive(){\n\tmemset(prime,true,sizeof(prime));\n\tprime[0]=prime[1]=false;\n\tfor(int i=2;i<=5000000;i++){\n\t\tif(prime[i]){\n\t\t\tfor(int j=i*2;j<=5000000;j+=i)prime[j]=false;\n\t\t}\n\t}\n}\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tseive();\n\tint res=-1,cnt=0;\n\tfor(int i=4;i<=5000000;i++){\n\t\tif(!prime[i]){\n\t\t\tcnt++;\n\t\t\tif(cnt>=n){\n\t\t\t\tres=i-n+1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\telse cnt=0;\n\t}\n\t//if(res==-1)printf(\"no answer\\n\");\n\tprintf(\"%d\\n\",res);\n\tfor(int i=res;i<res+n;i++){\n\t\tfor(int j=2;j*j<=i;j++){\n\t\t\tif(i%j==0){\n\t\t\t\tprintf(\"%d\\n\",j);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 0.43333333333333335, "time_ms": 40, "memory_kb": 6076, "score_of_the_acc": -0.0746, "final_rank": 14 }, { "submission_id": "aoj_1503_573735", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n\nusing namespace std;\n\nint n;\nbool prime[5000001];\n\nvoid seive(){\n\tmemset(prime,true,sizeof(prime));\n\tprime[0]=prime[1]=false;\n\tfor(int i=2;i<=500000;i++){\n\t\tif(prime[i]){\n\t\t\tfor(int j=i*2;j<=500000;j+=i)prime[j]=false;\n\t\t}\n\t}\n}\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tseive();\n\tint res=-1,cnt=0;\n\tfor(int i=2;i<=5000000;i++){\n\t\tif(!prime[i]){\n\t\t\tcnt++;\n\t\t\tif(cnt>=n){\n\t\t\t\tres=i-n+1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\telse cnt=0;\n\t}\n\tif(res==-1)printf(\"no answer\\n\");\n\tprintf(\"%d\\n\",res);\n\tfor(int i=res;i<res+n;i++){\n\t\tfor(int j=2;j*j<=i;j++){\n\t\t\tif(i%j==0){\n\t\t\t\tprintf(\"%d\\n\",j);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 0.16666666666666666, "time_ms": 10, "memory_kb": 6080, "score_of_the_acc": -0.041, "final_rank": 17 }, { "submission_id": "aoj_1503_530095", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <sstream>\n#include <cstdio>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <cassert>\n#include <climits>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\nconst double EPS = 1e-8;\n\ntypedef complex<double> Complex;\n\nstruct Long {\n static const long long BASE = 10000;\n static const int BW = 4;\n vector<long long> digits;\n Long(long long a = 0) {\n digits.push_back(a);\n Normalize();\n }\n Long(const string &str) {\n if (str.length() == 0) {\n digits.push_back(0);\n } else {\n int s = str[0] != '-' ? 1 : -1;\n int sw = s == -1 ? 1 : 0;\n digits = vector<long long>((str.size() - 1 - sw) / BW + 1);\n int i = (str.size() - sw) % BW;\n int index = digits.size() - 1;\n if (i > 0) { i -= BW; }\n for (; i < (int)str.size() - sw; i+= BW) {\n long long a = 0;\n for (int j = 0; j < BW; j++) {\n a = 10 * a + (i + j + sw >= sw ? str[i + j + sw] - '0' : 0);\n }\n digits[index--] = a * s;\n }\n }\n Normalize();\n }\n void resize(int s) { digits.resize(s); }\n int size() const { return digits.size(); } // size of vector of digits\n int size2() const { // number of digits\n return abs().toString().size();\n }\n int sign() const { return digits[digits.size() - 1] > 0 ? 1 : (digits[digits.size() - 1] < 0 ? -1 : 0); }\n string toString() const {\n int s = sign();\n ostringstream os;\n os << digits[size() - 1];\n for (int i = size() - 2; i >= 0; i--)\n os << setw(Long::BW) << setfill('0') << digits[i] * s;\n return os.str();\n }\n Long abs() const {\n Long ret = *this;\n return ret.sign() >= 0 ? ret : -ret;;\n }\n long long operator[](int index) const { return digits[index]; }\n long long &operator[](int index) { return digits[index]; }\n bool operator<(const Long &rhs) const {\n const Long &lhs = *this;\n if (lhs.sign() != rhs.sign()) { return lhs.sign() < rhs.sign(); }\n if (lhs.size() != rhs.size()) { return lhs.sign() > 0 ? lhs.size() < rhs.size() : lhs.size() > rhs.size(); }\n for (int i = lhs.size() - 1; i >= 0; i--) {\n if (lhs[i] != rhs[i]) { return lhs[i] < rhs[i]; }\n }\n return false;\n }\n bool operator>(const Long &rhs) const { return rhs < *this; }\n bool operator<=(const Long &rhs) const { return !(rhs < *this); }\n bool operator>=(const Long &rhs) const { return !(*this < rhs); }\n bool operator!=(const Long &rhs) const { return *this < rhs || rhs < *this; }\n bool operator==(const Long &rhs) const { return !(*this < rhs) && !(rhs < *this); }\n\n Long &Normalize() {\n int s = 1;\n while (digits[size() - 1] <= -BASE) {\n ll v = digits[size() - 1] / BASE;\n digits.push_back(v);\n digits[size() - 2] -= v * BASE;\n } \n for (int i = 0; i < size(); i++) {\n if (i == size() - 1 && digits[i] < 0) {\n goto minus;\n } else if (digits[i] < 0) {\n long long a = -((digits[i] + 1) / BASE) + 1;\n digits[i] = a * BASE + digits[i];\n digits[i + 1] -= a;\n } else if (digits[i] >= BASE) {\n if (i == size() - 1) { digits.push_back(0); }\n long long a = digits[i] / BASE;\n digits[i] = digits[i] % BASE;\n digits[i + 1] += a;\n }\n assert(0 <= digits[i] && digits[i] < BASE);\n if (digits[i] != 0) { s = i + 1; }\n }\n assert(0 <= digits[s - 1] && digits[s - 1] < BASE);\n assert(s == 1 || digits[s - 1] != 0);\n resize(s);\n return *this;\nminus:\n s = 1;\n for (int i = size() - 2; i >= 0; i--) {\n digits[i + 1] += 1;\n digits[i] -= BASE;\n if (digits[i + 1] != 0) { s = max(s, i + 2); }\n assert(digits[i + 1] <= 0);\n assert(digits[i] <= 0);\n }\n resize(s);\n return *this;\n }\n Long operator+(const Long &rhs) const {\n Long ret = *this;\n if (ret.size() < rhs.size()) { ret.resize(rhs.size()); }\n for (int i = 0; i < rhs.size(); i++) { ret[i] += rhs[i]; }\n return ret.Normalize();\n }\n Long operator-(const Long &rhs) const {\n //assert(*this >= rhs);\n Long ret = *this;\n if (ret.size() < rhs.size()) { ret.resize(rhs.size()); }\n for (int i = 0; i < rhs.size(); i++) { ret[i] -= rhs[i]; }\n return ret.Normalize();\n }\n Long operator*(const Long &rhs) const {\n const Long &lhs = *this;\n Long ret;\n ret.resize(this->size() + rhs.size());\n for (int i = 0; i < lhs.size(); i++) {\n for (int j = 0; j < rhs.size(); j++) {\n ret[i + j] += lhs[i] * rhs[j];\n }\n }\n return ret.Normalize();\n }\n Long operator/(const Long &rhs) const {\n return this->divmod(rhs).first;\n }\n Long operator%(const Long &rhs) const {\n return this->divmod(rhs).second;\n }\n Long operator-() const {\n return *this * -1;\n }\n Long &operator+=(const Long &rhs) {\n return *this = *this + rhs;\n }\n Long &operator-=(const Long &rhs) {\n return *this = *this - rhs;\n }\n Long &operator*=(const Long &rhs) {\n return *this = *this * rhs;\n }\n Long &operator/=(const Long &rhs) {\n return *this = *this / rhs;\n }\n Long &operator/=(long long rhs) {\n return *this = *this / rhs;\n }\n Long &operator%=(const Long &rhs) {\n return *this = *this % rhs;\n }\n pair<Long, long long> divmodll(long long rhs) const {\n Long ret = *this;\n long long c = 0;\n long long t;\n for (int i = ret.size() - 1; i >= 0; i--) {\n t = BASE * c + ret[i];\n ret[i] = t / rhs;\n c = t % rhs;\n }\n return make_pair(ret.Normalize(), c);\n }\n pair<Long, Long> divmod(Long rhs) const {\n Long lhs = *this;\n if (lhs.size() < rhs.size()) { return pair<Long, Long>(0, lhs); }\n int s = lhs.sign() * rhs.sign();\n lhs = lhs.abs(); rhs = rhs.abs();\n long long F = BASE / (rhs[rhs.size() - 1] + 1); // multiplying good-factor\n lhs = lhs * F; rhs = rhs * F;\n Long ret;\n ret.resize(lhs.size() - rhs.size() + 1);\n for (int k = ret.size() - 1, i = lhs.size() - 1; k >= 0; k--, i--) {\n ret[k] = (i + 1 < lhs.size() ? lhs[i + 1] : 0) * BASE + lhs[i];\n ret[k] /= rhs[rhs.size() - 1];\n Long t;\n t.resize(k + rhs.size());\n for (int m = 0; m < rhs.size(); m++) {\n t[k + m] = ret[k] * rhs[m];\n }\n t.Normalize();\n while (lhs < t) {\n ret[k] -= 1;\n for (int m = 0; m < rhs.size(); m++) {\n t[k + m] -= rhs[m];\n }\n t.Normalize();\n }\n lhs = lhs - t;\n }\n return make_pair(ret.Normalize() * s, lhs.divmodll(F).first * s);\n }\n Long &multen(int a) { // for LongFloat\n if (a > 0) {\n vector<long long> tmp(a/4);\n FOR(it, digits) tmp.push_back(*it);\n digits = tmp;\n *this *= Long(pow(10,a%4));\n }\n if (a < 0) {\n vector<long long> tmp;\n for (int i=-a/4; i<size(); ++i) {\n tmp.push_back(digits[i]);\n }\n digits = tmp;\n *this /= pow(10,-a%4);\n }\n return *this;\n }\n};\nostream &operator<<(ostream &os, const Long &rhs) {\n os << rhs.toString();\n return os;\n}\nistream &operator>>(istream &is, Long &rhs) {\n string s;\n is >> s;\n rhs = Long(s);\n return is;\n}\npair<Long, long long> divmodll(const Long &lhs, long long &rhs) {\n return lhs.divmodll(rhs);\n}\npair<Long, Long> divmod(const Long &lhs, const Long &rhs) {\n return lhs.divmod(rhs);\n}\n\n// ax+by=gcd(a,b)\nLong extgcd(const Long &a, const Long &b, Long &x, Long &y) {\n Long g = a;\n x = 1; y = 0;\n if (b != 0) {\n g = extgcd(b, a%b, y, x);\n y -= (a/b) * x;\n }\n return g;\n}\n \nLong ChineseRemainderTheorem(const vector<Long> &B, const vector<Long> &M) {\n Long all(1);\n FOR(it, M) all*=*it;\n Long res;\n REP(i,B.size()) {\n Long x,y;\n extgcd(M[i], all/M[i], x, y);\n y = (y+all)%all;\n res = (res + B[i]*y*(all/M[i]) % all) % all;\n assert(res >= 0);\n }\n return res;\n}\n\nconst int N = 100000;\nbool prime[N];\nint p[N];\nint a[2000];\n\nint main() {\n for (int i=2; i<N; ++i) prime[i]=1;\n int ct=0;\n REP(i,N) if (prime[i]) {\n p[ct++] = i;\n for (int j=i*2; j<N; j+=i)\n prime[j]=0;\n }\n \n int n;\n while(cin >> n) {\n memset(a,0,sizeof(a));\n int c = 0;\n vector<Long> b,m;\n REP(i,n) if (a[i] == 0) {\n if (i) {\n b.push_back(Long(((-i%p[c])+p[c])%p[c]));\n m.push_back(Long(p[c]));\n } else {\n b.push_back(Long(0));\n m.push_back(Long(4));\n }\n for (int j=i; j<n; j+=p[c])\n a[j] = p[c];\n c++;\n }\n cout << ChineseRemainderTheorem(b,m) << endl;\n REP(i,n) cout << a[i] << endl;\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 1488, "score_of_the_acc": -0.2719, "final_rank": 9 }, { "submission_id": "aoj_1503_523524", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n\n#define pb push_back\n#define clr clear()\n#define sz size()\n#define FOR(i,a) for(int i=0;i<(int)(a);i++)\n\nusing namespace std;\n\ntypedef vector<int> vi;\n\nstruct BigInt{\n vi v;\n int s;\n\n BigInt(void){v.clr;s=0;}\n \n BigInt(int x){\n v.clr; s = 1;\n if(!x)s = 0;\n else if(x<0){\n s = -1;\n x *= -1;\n }\n while(x){\n v.pb(x%10);\n x/=10;\n }\n }\n\n BigInt(string &x){\n int a = 0;\n v.clr; s = 1;\n if(x[0]=='-'){\n s = -1;\n a = 1;\n }else if(x[0]=='0')s = 0;\n for(int i=x.sz-1;i>=a;i--)v.pb(x[i]-'0');\n }\n\n BigInt nega(void){\n BigInt t = *this;\n t.s *= -1;\n return t;\n }\n\n BigInt abs(void){\n if(s<0)return this->nega();\n return *this;\n }\n\n string bitos(void){\n if(!s)return \"0\";\n string res;\n FOR(i,v.sz)res += v[i]+'0';\n reverse(res.begin(),res.end());\n return (s<0)?('-'+res):res;\n }\n\n BigInt operator+(BigInt a){\n if(!s)return a;\n if(!a.s)return *this;\n if(s==a.s){\n if(a.v.sz < v.sz)return a+(*this);\n FOR(i,v.sz)a.v[i] += v[i];\n FOR(i,a.v.sz-1)\n\tif(a.v[i]>9){\n\t a.v[i] -= 10;\n\t a.v[i+1]++;\n\t}\n\n if(a.v[a.v.sz-1]>9){\n\ta.v[a.v.sz-1] -= 10;\n\ta.v.pb(1);\n }\n return a;\n }else{\n if(s<0)return a-(*this).nega();\n else return (*this)-a.nega();\n }\n }\n \n BigInt operator-(BigInt a){\n if(!s)return a.nega();\n if(!a.s)return *this; \n if(s==a.s){\n if(a.abs() > this->abs())return (a-(*this)).nega();\n BigInt b = *this;\n FOR(i,a.v.sz)b.v[i] -= a.v[i];\n FOR(i,b.v.sz-1)\n\tif(b.v[i]<0){\n\t b.v[i] += 10;\n\t b.v[i+1]--;\n\t}\n\n while(b.v.sz && !b.v.back())b.v.pop_back();\n if(b.v.empty())b.s=0;\n return b;\n }else return (*this)+a.nega();\n }\n\n BigInt operator*(BigInt a){\n if(!s)return *this;\n if(!a.s)return a;\n\n BigInt b(0),tmp; \n FOR(i,a.v.sz){\n tmp = *this;\n FOR(j,tmp.v.sz)tmp.v[j] *= a.v[i];\n \n FOR(j,tmp.v.sz){\n\tif(i+j<b.v.sz)b.v[i+j] += tmp.v[j];\n\telse b.v.pb(tmp.v[j]);\n }\n }\n\n FOR(i,b.v.sz){\n if(b.v[i]>9){\n\tif(i+1<b.v.sz)b.v[i+1] += b.v[i]/10;\n\telse b.v.pb(b.v[i]/10);\n\tb.v[i] %= 10;\n }\n }\n b.s = s*a.s;\n return b;\n }\n\n void operator=(BigInt a){v = a.v;s = a.s;}\n\n bool operator==(BigInt a){\n if(s!=a.s)return false;\n if(!s)return true;\n if(v.sz!=a.v.sz)return false;\n FOR(i,v.sz)if(v[i]!=a.v[i])return false;\n return true;\n }\n\n bool operator<(BigInt a){\n if(s!=a.s)return s<a.s;\n if(!s)return false;\n int d = v.sz - a.v.sz;\n if(d)return 0>d*s;\n for(int i=v.sz-1;i>=0;i--){\n d = v[i] - a.v[i];\n if(d)return 0>d*s;\n }\n return false;\n }\n\n bool operator<=(BigInt a){return (*this==a)||(*this<a);}\n bool operator>(BigInt a){return !(*this<=a);}\n bool operator>=(BigInt a){return !(*this<a);}\n};\n\nint main(){\n int n;\n BigInt ans(1);\n\n for(int i=2;i<=1501;i++)ans = ans * BigInt(i);\n ans = ans + BigInt(2);\n\n cin >> n;\n cout << ans.bitos() << endl;\n for(int i=0;i<n;i++)cout << i+2 << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1344, "score_of_the_acc": -0.0348, "final_rank": 6 }, { "submission_id": "aoj_1503_516978", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <list>\n#include <cmath>\n#include <fstream>\n#include <algorithm>\n#include <string>\n#include <queue>\n#include <set>\n#include <map>\n#include <complex>\n#include <iterator>\n#include <cstdlib>\n#include <cstring>\n#include <sstream>\n#include <stack>\n#include <climits>\n#include <deque>\n#include <bitset>\n#include <cassert>\n#include <ctime>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int dy[]={-1,0,1,0},dx[]={0,1,0,-1};\n// adjust problem by problem\nconst double EPS=1e-8;\nconst double PI=acos(-1.0);\n#ifdef __GNUC__\nint popcount(int n){return __builtin_popcount(n);}\nint popcount(ll n){return __builtin_popcountll(n);}\n#endif\n#ifndef __GNUC__\ntemplate<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}\n#endif\ntemplate<class T>int SIZE(T a){return a.size();}\ntemplate<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}\ntemplate<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}\ntemplate<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}\ntemplate<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}\nbool EQ(double a,double b){return abs(a-b)<EPS;}\nvoid fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}\nvector<string> split(string str,char del){\n vector<string> res;\n for(int i=0,s=0;i<SIZE(str);i++){\n if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}\n else if(i==SIZE(str)-1){res.push_back(str.substr(s));}\n }\n return res;\n}\n\nconst int MAX_PRIME = 10000000;\nint prime[MAX_PRIME+1];\nbool isPrime[MAX_PRIME+1];\nint p;\nvoid erats(){\n // primeリストを初期化\n fill(isPrime,isPrime+MAX_PRIME,true);\n p=0;\n isPrime[0]=isPrime[1]=false;\n for(int i=2;i<=MAX_PRIME;i++){\n if(isPrime[i]){\n prime[p++]=i;\n // ふるいにかける\n for(int j=2*i;j<=MAX_PRIME;j+=i)isPrime[j] = false;\n }\n }\n}\n\n\nstring add(string str1,string str2){\n string str;\n int carry = 0;\n for(int i = 0; i < max(str1.size(),str2.size()); i++){\n if(i >= str1.size()){\n int ret = carry + str2[str2.size()-1-i] - '0';\n carry = ret / 10;\n str += (ret % 10) + '0';\n }\n else if(i >= str2.size()){\n int ret = carry + str1[str1.size()-1-i] - '0';\n carry = ret / 10;\n str += (ret % 10) + '0';\n }\n else{\n int ret = str1[str1.size()-1-i] + str2[str2.size()-1-i] - '0' - '0' + carry;\n carry = ret / 10;\n str += (ret % 10) + '0';\n }\n }\n str+=(carry+'0');\n // 破壊的変更\n reverse(str.begin(),str.end());\n // 先頭の0を消す\n int idx;\n for(int i = 0; i < str.size(); i++){\n idx=i;\n if(str[i]!='0'||str.size()-1==i)\n break;\n }\n return str.substr(idx);\n}\nstring mul(string str1,string str2){\n int base=0;\n string ret=\"0\";\n for(int j = str2.size()-1; j >= 0; j--){\n string str;\n int carry = 0;\n for(int i = str1.size()-1; i >=0; i--){\n int ret = (str1[i]-'0')*(str2[j] - '0') + carry;\n carry = ret / 10;\n str += (ret % 10) + '0';\n }\n str+=(carry+'0');\n // 破壊的変更\n reverse(str.begin(),str.end());\n // 先頭の0を消す\n int idx;\n for(int i = 0; i < str.size(); i++){\n idx=i;\n if(str[i]!='0'||str.size()-1==i)\n break;\n }\n str=str.substr(idx);\n for(int i = 0; i < base; i++){\n str+='0';\n }\n ret=add(ret,str);\n base++;\n }\n return ret;\n}\n\nint N;\nint main(){\n cin>>N;\n string s=\"1\";\n for(int i=1;i<=N+1;i++){\n stringstream ss;\n ss<<i;\n s=mul(s,ss.str());\n }\n s=add(s,\"2\");\n cout<<s<<endl;\n for(int i=2;i<=N+1;i++)\n cout<<i<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 1296, "score_of_the_acc": -0.1467, "final_rank": 8 } ]
aoj_1507_cpp
Problem H : Dungeon (II) あなたはとあるゲームの開発に携わっている。 そのゲームはランダムに生成されたダンジョンをプレイヤーが探索するというものである。 ゲームの仕様として、プレイヤーに予めダンジョンの危険度を提示し、生成されたダンジョンを探索するのか、それとも新しくダンジョンを生成しなおすかを、選択できるようにしたい。 このゲームで生成されるダンジョンには n 個の部屋が存在しており、0から n-1 までの番号が割り振られている。 部屋と部屋は通路で結ばれている。部屋と部屋を結ぶ通路は、合計で n-1 本存在している。 通路はどちらの方向へも進むことができる。 また、部屋と部屋の間には距離が設定されている。 生成されたダンジョンではいくつかの通路を経由して、ある部屋から他のすべての部屋へ行くことが可能である。 そして、プレイヤーがゲームを行う際に、2つの異なる部屋がスタート地点とゴール地点として選ばれる。 あなたはダンジョンの評価を行うために、危険度の評価方法を決めることにした。 まず、ある部屋から別の部屋までに移動する際の危険度を、部屋間を最短で移動するために使う通路の中で、最もコスト高い通路の値とする。 そして、ダンジョンの危険度を、 i < j となる部屋のペアの間を移動する際の危険度の総和とすることにした。 ランダムに生成されたダンジョンのが入力として与えられる。 まず、 i < j となるすべての部屋のペアについて、移動する際の危険度を計算して欲しい。 そして、その総和を問題の答えとして出力せよ。 Input 入力は以下のフォーマットで与えられる。 n a 1 b 1 c 1 . . . a n-1 b n-1 c n-1 a i b i c i は 部屋 a i と b i を結ぶ通路の距離が c i であることを表す。 入力は以下の制約を満たす 2 ≤ n ≤ 200,000 0 ≤ a i , b i < n 0 ≤ c i ≤ 100,000 Output 答えの値を1行に出力せよ Sample Input 1 4 0 1 3 1 2 5 1 3 2 Sample Output 1 23 Sample Input 2 6 0 2 5 2 1 1 2 3 10 3 5 4 3 4 2 Sample Output 2 111
[ { "submission_id": "aoj_1507_10772260", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define lint long long\n#define P pair<int, int>\n#define LLP pair<long long, long long>\n#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)\n#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)\n#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)\n#define SORT(x) sort((x).begin(), (x).end())\n#define SORT_INV(x) sort((x).rbegin(), (x).rend())\n\n#define int long long\n\nconst int IINF = 1e9 + 100;\nconst long long LLINF = 2e18 + 129;\nconst long long MOD = 1e9 + 7;\nconst int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};\nconst int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};\nconst double EPS = 1e-8;\n\nstruct unionFind{\n vector<int> par;\n int siz;\n\n void init(int N){\n par.resize(N);\n fill(par.begin(), par.end(), -1);\n siz = N;\n return;\n }\n\n int root(int X){\n if(par[X] < 0){\n return X;\n }\n return par[X] = root(par[X]);\n }\n\n bool unite(int X, int Y){\n X = root(X);\n Y = root(Y);\n\n if(X == Y){\n return false;\n }\n\n if(par[X] < par[Y]){\n swap(X, Y);\n }\n\n par[Y] += par[X];\n par[X] = Y;\n --siz;\n\n return true;\n }\n\n bool same(int X, int Y){\n return root(X) == root(Y);\n }\n\n int size(int X){\n return -par[root(X)];\n }\n};\n\nstruct edge{\n int a, b;\n lint c;\n\n bool operator<(const edge & right) const {\n return c < right.c;\n }\n};\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int n;\n cin >> n;\n\n vector<edge> edges(n - 1);\n rep(i, n - 1){\n int a, b;\n lint c;\n cin >> a >> b >> c;\n edges[i] = {a, b, c};\n }\n SORT(edges);\n\n unionFind uf;\n uf.init(n);\n\n lint ans = 0;\n for(auto e : edges){\n lint a = uf.size(e.a), b = uf.size(e.b);\n ans += a * b * e.c;\n uf.unite(e.a, e.b);\n }\n\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 9344, "score_of_the_acc": -0.364, "final_rank": 5 }, { "submission_id": "aoj_1507_9723874", "code_snippet": "#include<bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 1000000000;\n//------------------------------------------------------------------------------\nstruct SEdge\n{\n int v, w;\n int64_t c;\n\n bool operator<(const SEdge& e1) const\n {\n return c < e1.c;\n }\n\n SEdge(): v(-1), w(-1), c(-1) {}\n SEdge(int v1, int v2, int64_t cost): v(v1), w(v2), c(cost) {}\n void debug() const { printf(\"%d %d %ld\\n\", v, w, c); }\n};\n\ntypedef vector<SEdge> SEdges;\n\n//------------------------------------------------------------------------------\nclass QuickUnion\n{\n public:\n int N;\n int M; // no. of E\n int64_t danger_level_sum;\n int* parent;\n int* count_; // no. of nodes rooted at n\n QuickUnion(int num): N(num), M(0)\n {\n danger_level_sum = 0;\n parent = new int[N]; // 0-based nodes\n count_ = new int[N];\n for (int n=0; n<N; n++) { parent[n]=n; count_[n]=1; }\n }\n\n ~QuickUnion() { delete[] parent; delete[] count_; }\n\n int size(int n) //<--------------------\n {\n int p = root(n);\n return count_[p];\n }\n\n bool connected()\n {\n return M+1 >= N;\n }\n\n bool unite(int n1, int n2, int64_t cost) //<---------- return true if action is successful\n {\n int r1 = root(n1);\n int r2 = root(n2);\n if (r1==r2) return false;\n M++; //<---------- for checking whether all are connected\n danger_level_sum += cost*count_[r1]*count_[r2];\n if (count_[r1] < count_[r2]) { parent[r1] = r2; count_[r2] += count_[r1]; }\n else { parent[r2] = r1; count_[r1] += count_[r2]; }\n return true;\n }\n\n bool find(int n1, int n2)\n {\n return root(n1)==root(n2);\n }\n\n int root(int node)\n {\n int n = node;\n while (n != parent[n])\n {\n parent[n] = parent[parent[n]]; // vs compression\n n = parent[n];\n }\n parent[node] = n;\n return n;\n }\n\n void debug() const\n {\n for (int n=0; n<N; n++) printf(\"node=%d, root=%d\\n\", n, parent[n]);\n printf(\"\\n\");\n }\n};\n\n//------------------------------------------------------------------------------\nint64_t kruskal(int N, SEdges& E)\n{\n sort(E.begin(), E.end());\n\n QuickUnion QU(N);\n for (SEdge e: E)\n {\n if (DEBUG) e.debug();\n if (QU.connected()) break;\n QU.unite(e.v, e.w, e.c);\n }\n return QU.danger_level_sum;\n}\n\n//------------------------------------------------------------------------------\nvoid test()\n{\n\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //test(); return 0;\n //DEBUG = true;\n //--------------------------------------------------------------------------\n int T, N, M, v, w, num;\n int64_t c;\n //num = scanf(\"%d \", &T);\n //while (T--)\n //{\n num = scanf(\"%d \", &N);\n M = N-1;\n SEdges E(M);\n for (int m=0; m<M; ++m)\n {\n num = scanf(\"%d %d %ld \", &v, &w, &c);\n E[m] = SEdge(v, w, c);\n }\n printf(\"%ld\\n\", kruskal(N, E));\n //}\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 40, "memory_kb": 7752, "score_of_the_acc": -0.1907, "final_rank": 3 }, { "submission_id": "aoj_1507_5825785", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\n//イテレーション\n#define REP(i, n) for (ll i = 0; i < ll(n); i++)\n#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)\n#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)\n#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)\n#define FORA(i, I) for (const auto& i : I)\n// x:コンテナ\n#define ALL(x) x.begin(), x.end()\n#define SIZE(x) ll(x.size())\n//定数\n#define INF32 2147483647 // 2.147483647×10^{9}:32bit整数のinf\n#define INF64 9223372036854775807 // 9.223372036854775807×10^{18}:64bit整数のinf\n#define MOD 1000000007 //問題による\n//略記\n#define F first\n#define S second\n//出力(空白区切りで昇順に)\n#define coutALL(x) \\\n for (auto i = x.begin(); i != --x.end(); i++) cout << *i << \" \"; \\\n cout << *--x.end() << endl;\n\n// aをbで割る時の繰上げ,繰り下げ\nll myceil(ll a, ll b) { return (a + (b - 1)) / b; }\nll myfloor(ll a, ll b) { return a / b; }\n\nll root(vector<ll>& pars, ll x) {\n if(pars[x] == x) return x;\n return pars[x] = root(pars, pars[x]);\n}\n\nbool same(vector<ll>& pars, ll x, ll y) {\n ll rx = root(pars, x);\n ll ry = root(pars, y);\n return rx == ry;\n}\n\nll size(vector<ll>& pars, vector<ll>& sizes, ll x) {\n return sizes[root(pars, x)];\n}\n\nvoid unite(vector<ll>& pars, vector<ll>& ranks, vector<ll>& sizes, ll x, ll y) {\n ll rx = root(pars, x);\n ll ry = root(pars, y);\n if(rx == ry) return;\n if(ranks[rx] < ranks[ry]) {\n pars[rx] = ry;\n sizes[ry] += sizes[rx];\n }else {\n pars[ry] = rx;\n if(ranks[rx] == ranks[ry]) ranks[ry]++;\n sizes[rx] += sizes[ry];\n }\n return;\n}\n\n\nint main() {\n //小数の桁数の出力指定\n // cout<<fixed<<setprecision(10);\n //入力の高速化用のコード\n // ios::sync_with_stdio(false);\n // cin.tie(nullptr);\n ll n;\n cin >> n;\n\n ll a,b,c;\n vector<tuple<ll,ll,ll>> ps;\n REP(i, n-1) {\n cin >> a >> b >> c;\n ps.push_back(make_tuple(c,a,b));\n }\n sort(ps.begin(), ps.end());\n vector<ll> pars;\n vector<ll> sizes;\n vector<ll> ranks;\n REP(i, n) {\n pars.push_back(i);\n sizes.push_back(1);\n ranks.push_back(0);\n }\n ll ans = 0;\n REP(i, n-1) {\n auto [c,a,b] = ps[i];\n ans += c * size(pars, sizes, a) * size(pars, sizes, b);\n unite(pars, ranks, sizes, a, b);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 15188, "score_of_the_acc": -1.5385, "final_rank": 17 }, { "submission_id": "aoj_1507_5825586", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 1000000007 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash {\n static const int base1 = 1007, base2 = 2009;\n static const int mod1 = 1000000007, mod2 = 1000000009;\n vector<long long> hash1, hash2, power1, power2;\n int n;\n // construct\n RollingHash(const string &S) {\n n = (int)S.size();\n hash1.assign(n+1, 0);\n hash2.assign(n+1, 0);\n power1.assign(n+1, 1);\n power2.assign(n+1, 1);\n for (int i = 0; i < n; ++i) {\n hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;\n hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;\n power1[i+1] = (power1[i] * base1) % mod1;\n power2[i+1] = (power2[i] * base2) % mod2;\n }\n }\n \n // get hash of S[left:right]\n inline pair<long long, long long> get(int l, int r) const {\n long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;\n if (res1 < 0) res1 += mod1;\n long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;\n if (res2 < 0) res2 += mod2;\n return {res1, res2};\n }\n \n inline pair<long long, long long> c_shift(int l) {\n auto h1 = get(0, l);\n auto h2 = get(l, n);\n return {(h1.first + h2.first * power1[l]) % mod1, (h1.second + h2.second * power2[l]) % mod2};\n }\n \n // get lcp of S[a:] and T[b:]\n inline int getLCP(int a, int b) const {\n int len = min((int)hash1.size()-a, (int)hash1.size()-b);\n int low = 0, high = len;\n while (high - low > 1) {\n int mid = (low + high) >> 1;\n if (get(a, a+mid) != get(b, b+mid)) high = mid;\n else low = mid;\n }\n return low;\n }\n};\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n\n int n; cin >> n;\n vector<tuple<int, int, int>> g(n-1);\n REP(i,n-1) {\n int a, b, c; cin >> a >> b >> c;\n\n g[i] = {c, a, b};\n }\n sort(all(g));\n UnionFind uf(n);\n\n ll ans = 0;\n for(auto[c, a, b]: g) {\n ans += 1LL * c * uf.size(a) * uf.size(b);\n uf.merge(a, b);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 6908, "score_of_the_acc": -0.0988, "final_rank": 1 }, { "submission_id": "aoj_1507_5431124", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int, int>;\n\n#define mp make_pair\n\nstruct UnionFindTree\n{\n\tvector<int> par;\n\tvector<int> rank;\n\tvector<int> size;\n\n\tvoid init(int n)\n\t{\n\t\tpar.resize(n);\n\t\trank.resize(n);\n\t\tsize.resize(n);\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tpar[i] = i;\n\t\t\trank[i] = 0;\n\t\t\tsize[i] = 1;\n\t\t}\n\t}\n\tint find(int x)\n\t{\n\t\tif (par[x] == x)\n\t\t\treturn x;\n\t\telse\n\t\t\treturn par[x] = find(par[x]);\n\t}\n\tvoid unite(int x, int y)\n\t{\n\t\tx = find(x);\n\t\ty = find(y);\n\t\tif (x == y)\n\t\t\treturn;\n\t\tif (rank[x] < rank[y])\n\t\t{\n\t\t\tpar[x] = y;\n\t\t\tsize[y] += size[x];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tpar[y] = x;\n\t\t\tif (rank[x] == rank[y])\n\t\t\t\trank[x]++;\n\t\t\tsize[x] += size[y];\n\t\t}\n\t}\n\tbool same(int x, int y)\n\t{\n\t\treturn find(x) == find(y);\n\t}\n\tint getSize(int x)\n\t{\n\t\tx = find(x);\n\t\treturn size[x];\n\t}\n};\n\nint main()\n{\n\tll c, ans;\n\tint a, b, n;\n\twhile (cin >> n)\n\t{\n\t\tvector<pair<ll, pii>> room;\n\t\tfor (int i = 0; i < n-1; i++)\n\t\t{\n\t\t\tcin >> a >> b >> c;\n\t\t\troom.push_back(mp(c,mp(a,b)));\n\t\t}\n\t\tsort(room.begin(), room.end());\n\t\tUnionFindTree dungeon;\n\t\tdungeon.init(n);\n\t\tans = 0;\n\t\tfor (int i = 0; i < n-1; i++)\n\t\t{\n\t\t\ta = room[i].second.first;\n\t\t\tb = room[i].second.second;\n\t\t\tc = room[i].first;\n\t\t\tans += c * dungeon.getSize(a) * dungeon.getSize(b);\n\t\t\tdungeon.unite(a, b);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 8212, "score_of_the_acc": -1.01, "final_rank": 12 }, { "submission_id": "aoj_1507_5266497", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <iomanip>\n#include <fstream>\n#include <algorithm>\n#include <set>\n#include <queue>\n#include <string>\n#include <map>\n#include <stack>\n#include <climits>\n#include <array>\n#include <unordered_set>\n#include <unordered_map>\n#include <memory>\n#include <functional>\n#include <cfloat>\n#include <numeric>\n#include <random>\n#include <sstream>\n#include <bitset>\n#include <complex>\n#include <chrono>\n#include <cassert>\n\nclass UnionFind {\n\tstd::vector<int> parent;\npublic:\n\tUnionFind(const int size) :parent(size, -1) {};\n\tint find(const int a) {\n\t\treturn parent[a] < 0 ? a : parent[a] = find(parent[a]);\n\t}\n\tint size(const int a) {\n\t\treturn -parent[find(a)];\n\t}\n\tvoid unite(int a, int b) {\n\t\ta = find(a);\n\t\tb = find(b);\n\t\tif (a == b) return;\n\t\tif (parent[a] > parent[b]) std::swap(a, b);\n\t\tparent[a] += parent[b];\n\t\tparent[b] = a;\n\t}\n};\nint main() {\n\tint n; std::cin >> n;\n\tstd::vector<std::tuple<int, int, int>> edges(n - 1);\n\tfor (auto& [a, b, c] : edges) {\n\t\tstd::cin >> a >> b >> c;\n\t}\n\tstd::sort(edges.begin(), edges.end(), [](const std::tuple<int, int, int> a, const std::tuple<int, int, int> b) {return std::get<2>(a) < std::get<2>(b); });\n\tUnionFind uft(n);\n\tlong long int result{ 0 };\n\tfor (const auto [a, b, c] : edges) {\n\t\tconst long long int side_a = uft.size(a);\n\t\tconst long long int side_b = uft.size(b);\n\t\tresult += c * side_a * side_b;\n\t\tuft.unite(a, b);\n\t}\n\tstd::cout << result << '\\n';\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 6244, "score_of_the_acc": -0.4881, "final_rank": 7 }, { "submission_id": "aoj_1507_5264486", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <iomanip>\n#include <fstream>\n#include <algorithm>\n#include <set>\n#include <queue>\n#include <string>\n#include <map>\n#include <stack>\n#include <climits>\n#include <array>\n#include <unordered_set>\n#include <unordered_map>\n#include <memory>\n#include <functional>\n#include <cfloat>\n#include <numeric>\n#include <random>\n#include <sstream>\n#include <bitset>\n#include <complex>\n#include <chrono>\n#include <cassert>\n\nclass UnionFind {\n\tstd::vector<int> parent;\npublic:\n\tUnionFind(const int size) :parent(size, -1) {};\n\tint find(const int a) {\n\t\treturn parent[a] < 0 ? a : parent[a] = find(parent[a]);\n\t}\n\tint size(const int a) {\n\t\treturn -parent[find(a)];\n\t}\n\tvoid unite(int a, int b) {\n\t\ta = find(a);\n\t\tb = find(b);\n\t\tif (a == b) return;\n\t\tif (parent[a] > parent[b]) std::swap(a, b);\n\t\tparent[a] += parent[b];\n\t\tparent[b] = a;\n\t}\n};\nint main() {\n\tint n; std::cin >> n;\n\tstd::vector<std::tuple<int, int, int>> edges(n - 1);\n\tfor (auto& [a, b, c] : edges) {\n\t\tstd::cin >> a >> b >> c;\n\t}\n\tstd::sort(edges.begin(), edges.end(), [](const std::tuple<int, int, int> a, const std::tuple<int, int, int> b) {return std::get<2>(a) < std::get<2>(b); });\n\tUnionFind uft(n);\n\tlong long int result{ 0 };\n\tfor (const auto [a, b, c] : edges) {\n\t\tconst long long int side_a = uft.size(a);\n\t\tconst long long int side_b = uft.size(b);\n\t\tconst auto sum = side_a + side_b;\n\t\tresult += c * (sum * (sum - 1) / 2 - side_a * (side_a - 1) / 2 - side_b * (side_b - 1) / 2);\n\t\tuft.unite(a, b);\n\t}\n\tstd::cout << result << '\\n';\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 6212, "score_of_the_acc": -0.4846, "final_rank": 6 }, { "submission_id": "aoj_1507_3443469", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<utility>\n#include<string>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<map>\n#include<climits>\n#include<set>\n#include<tuple>\n\n\nusing namespace std;\ntypedef pair<int, int> pii;\ntypedef long long int ll;\ntypedef pair<ll, ll> pll;\nint dx[4] = { 1,0,0,-1 };\nint dy[4] = { 0,1,-1,0 };\n\n#define ARRAY_MAX 100005 \n\nconst int INF = 1e9 + 7;\nconst int MOD = 1e9 + 7;\n\ntypedef tuple<int, int, int> tpl;\n\n//繋げる方向を一方向にする\ntypedef struct union_find {\n\n\tvector<ll> pa;//親\n\tvector<ll> ra;//木の深さ\n\tvector<ll> siz;\n\t//n要素で初期化\n\tvoid init(ll n) {\n\t\tpa.resize(n);\n\t\tra.resize(n);\n\t\tsiz.resize(n);\n\t\tfor (ll i = 0; i < n; i++) {\n\t\t\tpa[i] = i;/*各ノードに番号を振る,この時par[x]=xとなる時は木の根となる*/\n\t\t\tra[i] = 0LL;/*各ノード自体の高さは0*/\n\t\t\tsiz[i] = 1LL;\n\t\t}\n\t}\n\t//木の根を求める\n\tll find(ll x) {\n\t\tif (pa[x] == x) {\n\t\t\treturn x;/*根ならそのノードの番号を返す*/\n\t\t}\n\t\telse {\n\t\t\treturn pa[x] = find(pa[x]);/*根でないならさらにノードの根を探す*/\n\t\t}\n\t}\n\n\t//xとyの属する集合を併合する\n\tvoid unite(ll x, ll y) {\n\t\tx = find(x);//xの根の番号を探す\n\t\ty = find(y);//yの根の番号を探す\n\t\tif (x == y) {//一致すればつながっている\n\t\t\treturn;\n\t\t}\n\t\tpa[y] = x;\n\t\tsiz[x] += siz[y];\n\t}\n\n\t//xとyが同じ集合に属するか判定\n\tbool same(ll x, ll y) {\n\t\treturn find(x) == find(y);//同じ集合なら1、違うなら0が返る\n\t}\n}UF;\n\nint main() {\n\n\tll n;\n\tcin >> n;\n\tvector<tpl> vec(n-1);\n\tfor (int i = 0; i < n-1; i++)\n\t{\n\t\tint a, b, c;\n\t\tcin >> a >> b >> c;\n\t\tvec[i] = tpl(c, a, b);\n\t}\n\n\tUF tree;\n\ttree.init(n);\n\t\n\tsort(vec.begin(), vec.end());\n\n\tll ans = 0;\n\n\tfor (int i = 0; i < n-1; i++)\n\t{\n\t\tll a, b, c;\n\t\ttie(c, a, b) = vec[i];\n\t\ta = tree.find(a);\n\t\tb = tree.find(b);\n\t\tif (!tree.same(a, b))\n\t\t{\n\t\t\tans += 1LL * c * tree.siz[a] * tree.siz[b];\n\t\t\ttree.unite(a, b);\n\t\t}\n\t}\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 9960, "score_of_the_acc": -1.1233, "final_rank": 14 }, { "submission_id": "aoj_1507_3412595", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef pair<ll ,P> P3;\nconst ll MOD = ll(1e9+7);\nconst int IINF = INT_MAX;\nconst ll LLINF = LLONG_MAX;\nconst int MAX_N = int(1e5 + 5);\nconst double EPS = 1e-6;\nconst int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};\n#define REP(i, n) for (int i = 0; i < n; i++)\n#define REPR(i, n) for (int i = n; i >= 0; i--)\n#define SORT(v) sort((v).begin(), (v).end())\n#define ALL(v) (v).begin(), (v).end()\n\nstruct UnionFindTree {\n vector<int> par;\n vector<int> rank;\n vector<int> siz;\n\n void init(int n) {\n par.resize(n);\n rank.resize(n);\n siz.resize(n);\n for (int i = 0; i < n; i++) {\n par[i] = i;\n rank[i] = 0;\n siz[i] = 1;\n }\n }\n int find(int x) {\n if (par[x] == x) {\n return x;\n } else {\n return par[x] = find(par[x]);\n }\n }\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (rank[x] < rank[y]) swap(x, y);\n if (rank[x] == rank[y]) rank[x]++;\n par[y] = x;\n siz[x] += siz[y];\n }\n bool is_same(int x, int y) {\n return find(x) == find(y);\n }\n int size(int x) {\n x = find(x);\n return siz[x];\n }\n};\n\nint main() {\n int n;\n vector<P3> es;\n cin >> n;\n REP(i,n-1){\n ll a, b, c;\n cin >> a >> b >> c;\n es.push_back({c,{a,b}});\n }\n sort(ALL(es));\n UnionFindTree g;\n g.init(n);\n ll ans = 0;\n REP(i,n){\n ll a, b, c;\n a = es[i].second.first;\n b = es[i].second.second;\n c = es[i].first;\n ans += c*ll(g.size(a))*ll(g.size(b));\n g.unite(a,b);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 9808, "score_of_the_acc": -1.1837, "final_rank": 15 }, { "submission_id": "aoj_1507_3117838", "code_snippet": "#include <stdio.h>\n#include <queue>\n#include <utility>\nusing namespace std;\ntypedef pair<int,int> pi;\ntypedef long long int ll;\n#define F first\n#define S second\nconst int N=2e5+10;\nint p[N],sz[N];\nint find(int n){return p[n]==n?n:p[n]=find(p[n]);}\nint main(){\n int n,a,b,c;\n pair<int,pi> temp;\n ll ans=0;\n priority_queue<pair<int,pi>,vector<pair<int,pi>>,greater<pair<int,pi>>>pq;\n scanf(\"%d\",&n);\n for(int i=0;i<n;i++){\n p[i]=i;\n sz[i]=1;\n }\n for(int i=1;i<n;i++){\n scanf(\"%d%d%d\",&a,&b,&c);\n pq.push({c,{a,b}});\n }\n while(!pq.empty()){\n temp=pq.top();\n pq.pop();\n a=find(temp.S.F);\n b=find(temp.S.S);\n ans+=(ll)sz[a]*(ll)sz[b]*(ll)temp.F;\n sz[a]+=sz[b];\n p[b]=a;\n }\n printf(\"%lld\\n\",ans);\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 7028, "score_of_the_acc": -0.2657, "final_rank": 4 }, { "submission_id": "aoj_1507_3083258", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct UF{\n int n;\n //正だったらその頂点の親,負だったら根で連結成分の個数\n vector<int> d;\n UF() {}\n UF(int N):n(N), d(N,-1){}\n int root(int v){\n if(d[v]<0) return v;\n return d[v]=root(d[v]);\n }\n bool unite(int X,int Y){\n X=root(X); Y=root(Y);\n if(X==Y) return false;\n if(size(X) < size(Y)) swap(X,Y);\n d[X]+=d[Y];\n d[Y]=X;\n return true;\n }\n int size(int v){ return -d[root(v)]; }\n bool same(int X,int Y){ return root(X)==root(Y); }\n};\n\nusing pi = pair<int,int>;\nusing edge = pair<int,pi>;\n\nint main(){\n int n;\n cin >>n;\n\n vector<edge> e(n-1);\n rep(i,n-1){\n int a,b,c;\n cin >>a >>b >>c;\n e[i] = {c,{a,b}};\n }\n sort(all(e));\n\n UF uf(n);\n ll ans = 0;\n rep(i,n-1){\n ll c = e[i].fi;\n int u = e[i].se.fi, v = e[i].se.se;\n\n ans += c*uf.size(u)*uf.size(v);\n uf.unite(u,v);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 6000, "score_of_the_acc": -0.6923, "final_rank": 8 }, { "submission_id": "aoj_1507_2762554", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define int long long\n\nstruct UF {\n vector<int> data;\n UF(int n):data(n, -1){}\n int find(int x) {\n if(data[x] < 0) return x;\n return data[x] = find(data[x]);\n }\n int size(int x) {\n return -data[find(x)];\n }\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n void unite(int x, int y) {\n x = find(x), y = find(y);\n if(x == y) return;\n if(data[x] > data[y]) swap(x, y);\n data[x] += data[y];\n data[y] = x;\n }\n};\n\nstruct edge {\n int u, v, w;\n edge(){}\n edge(int u, int v, int w):u(u), v(v), w(w){}\n bool operator < (const edge& e) const {\n return w < e.w;\n }\n};\n\nsigned main() {\n int n;\n cin >> n;\n vector<edge> es;\n for(int i = 0; i < n-1; ++i) {\n int a, b, c;\n cin >> a >> b >> c;\n es.emplace_back(a, b, c);\n }\n sort(es.begin(), es.end());\n UF uf(n);\n int ans = 0;\n for(edge& e : es) {\n int x = uf.find(e.u), y = uf.find(e.v);\n if(x == y) continue;\n ans += e.w*uf.size(x)*uf.size(y);\n uf.unite(x, y);\n }\n \n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 9104, "score_of_the_acc": -1.0301, "final_rank": 13 }, { "submission_id": "aoj_1507_2762083", "code_snippet": "#include <bits/stdc++.h>\n#define GET_MACRO(_1,_2,_3,_4,_5,NAME,...) NAME\n#define pr(...) GET_MACRO(__VA_ARGS__,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__)\n#define pr1(a) (#a)<<\"=\"<<(a)\n#define pr2(a,b) pr1(a)<<\", \"<<pr1(b)\n#define pr3(a,b,c) pr2(a,b)<<\", \"<<pr1(c)\n#define pr4(a,b,c,d) pr3(a,b,c)<<\", \"<<pr1(d)\n#define pr5(a,b,c,d,e) pr4(a,b,c,d)<<\", \"<<pr1(e)\n#define int long long\n#define double long double\nusing namespace std;\nconst int N = 100010;\nconst int INF = 1LL<<55;\nconst int mod = (1e9)+7;\nconst double EPS = 1e-8;\nconst double PI = 6.0 * asin(0.5);\ntypedef pair<int,int> P;\ntypedef long long ll;\ntemplate<class T> T Max(T &a,T b){return a=max(a,b);}\ntemplate<class T> T Min(T &a,T b){return a=min(a,b);}\nostream& operator<<(ostream& o,P p){return o<<\"(\"<<p.first<<\",\"<<p.second<<\")\";}\nistream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}\nostream& operator<<(ostream& o,vector<auto> &a){int i=0;for(auto t:a)o<<(i++?\" \":\"\")<<t;return o;}\nistream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}\nvoid prArr(auto a,string s=\" \"){int i=0;for(auto t:a)cout<<(i++?s:\"\")<<t;cout<<endl;}\n\nclass UF{\npublic:\n int V;\n vector<int> par,rnk,sz;\n UF(){}\n UF(int V):V(V),par(V),rnk(V,0),sz(V,1){for(int i=0;i<V;i++)par[i]=i;}\n \n int find(int x){\n assert(x < V);\n if(par[x]==x)return x;\n return par[x]=find(par[x]);\n }\n \n void unite(int x,int y){\n x=find(x), y=find(y);\n if(x==y)return;\n if(rnk[x]<rnk[y])par[x]=y, sz[y] += sz[x];\n else{\n par[y]=x;\n sz[x] += sz[y];\n if(rnk[x]==rnk[y])rnk[x]++;\n }\n }\n\n bool same(int x,int y){return find(x)==find(y);}\n\n int size(int x){return sz[find(x)];}\n};\n\nsigned main(){\n int n;\n cin>>n;\n typedef tuple<int,int,int> T;\n vector<T> edge;\n for(int i=0;i<n-1;i++){\n int a,b,c;\n cin>>a>>b>>c;\n edge.push_back(T(c,a,b));\n }\n\n sort(edge.begin(),edge.end());\n\n int ans = 0;\n UF uf(n);\n for(auto e:edge){\n int c,a,b; tie(c,a,b) = e;\n ans += c * uf.size(a) * uf.size(b);\n uf.unite(a,b);\n }\n\n cout<<ans<<endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 12252, "score_of_the_acc": -1.3728, "final_rank": 16 }, { "submission_id": "aoj_1507_2761428", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nstruct Kruskal{\n\n struct UnionFind{\n Int n;\n vector<Int> r,p;\n UnionFind(){}\n UnionFind(Int sz):n(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}\n Int find(Int x){\n return (x==p[x]?x:p[x]=find(p[x]));\n }\n bool same(Int x,Int y){\n return find(x)==find(y);\n }\n void unite(Int x,Int y){\n x=find(x);y=find(y);\n if(x==y) return;\n if(r[x]<r[y]) swap(x,y);\n r[x]+=r[y];\n p[y]=x;\n }\n };\n \n struct edge{\n Int from,to,cost,used;\n edge(){}\n edge(Int from,Int to,Int cost):\n from(from),to(to),cost(cost),used(0){}\n bool operator<(const edge& e) const{\n return cost<e.cost;\n }\n };\n\n Int n;\n vector<edge> edges;\n\n Kruskal(){}\n Kruskal(Int sz):n(sz){}\n \n void add_edge(Int u,Int v,Int c){\n edges.emplace_back(u,v,c);\n }\n\n void input(Int m,Int offset=0){\n Int a,b,c;\n for(Int i=0;i<m;i++){\n cin>>a>>b>>c;\n add_edge(a+offset,b+offset,c);\n }\n }\n \n Int build(){\n sort(edges.begin(),edges.end());\n UnionFind uf(n+1);\n Int res=0;\n for(auto &e:edges){\n if(!uf.same(e.from,e.to)){\n\tres+=e.cost*uf.r[uf.find(e.from)]*uf.r[uf.find(e.to)];\n\tuf.unite(e.from,e.to);\n\te.used=1;\n }\n }\n return res;\n }\n};\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n Int n;\n cin>>n;\n Kruskal G(n);\n G.input(n-1);\n cout<<G.build()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 12196, "score_of_the_acc": -0.7513, "final_rank": 9 }, { "submission_id": "aoj_1507_2741151", "code_snippet": "#include <iostream>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n \nusing namespace std;\n \n#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)\n#define REP(i,n) FOR(i,0,n)\n#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n \ntemplate<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<\" \"; cerr<<endl; }\ninline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }\n \ntypedef long long ll;\nconst int INF = 100000000;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\nint dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nint dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n \nstruct UnionFind {\n vector<int> data;\n UnionFind(int N) : data(N, -1) { }\n bool uni(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool find(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n \nstruct edge{\n int src, dst, cost;\n edge(int s, int d, int c) : src(s), dst(d), cost(c) {}\n edge() {}\n bool operator< (const edge& e) const {\n return cost < e.cost;\n }\n};\n \nint main(){\n int N;\n cin>>N;\n vector<edge> v(N - 1);\n REP(i, N - 1){\n int a, b, c;\n scanf(\"%d %d %d\", &a, &b, &c);\n v[i] = edge(a, b, c);\n }\n UnionFind uf(N);\n sort(v.begin(), v.end());\n ll ans = 0;\n REP(i, N - 1){\n ans += (ll)uf.size(v[i].src) * uf.size(v[i].dst) * v[i].cost;\n uf.uni(v[i].src, v[i].dst);\n }\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 6044, "score_of_the_acc": -0.1586, "final_rank": 2 }, { "submission_id": "aoj_1507_2661990", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nstruct UnionFind {\n\tvector<int> data;\n\tUnionFind(int size) : data(size, -1) { }\n\tbool unionSet(int x, int y) {\n\t\tx = root(x); y = root(y);\n\t\tif (x != y) {\n\t\t\tif (data[y] < data[x]) swap(x, y);\n\t\t\tdata[x] += data[y]; data[y] = x;\n\t\t}\n\t\treturn x != y;\n\t}\n\tint root(int x) {\n\t\treturn data[x] < 0 ? x : data[x] = root(data[x]);\n\t}\n\tint size(int x) {\n\t\treturn -data[root(x)];\n\t}\n};\n\n\n\nint main() {\n\tint N;cin>>N;\n\tvector<pair<unsigned long long int, pair<int, int>>>edges;\n\tfor (int i = 0; i < N - 1; ++i) {\n\t\tint a,b,c;cin>>a>>b>>c;\n\t\tpair<unsigned long long int,pair<int,int>> edge(make_pair(c,make_pair(a,b)));\n\t\tedges.push_back(edge);\n\t}\n\tsort(edges.begin(),edges.end());\n\tUnionFind uf(N);\n\tunsigned long long int ans=0;\n\tfor (auto edge : edges) {\n\t\tint a=edge.second.first;\n\t\tint b=edge.second.second;\n\t\tunsigned long long int c=edge.first;\n\t\tans+=c*uf.size(a)*uf.size(b);\n\t\tuf.unionSet(a,b);\n\t}\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 7000, "score_of_the_acc": -0.8781, "final_rank": 11 }, { "submission_id": "aoj_1507_2661979", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nstruct UnionFind {\n\tvector<int> data;\n\tUnionFind(int size) : data(size, -1) { }\n\tbool unionSet(int x, int y) {\n\t\tx = root(x); y = root(y);\n\t\tif (x != y) {\n\t\t\tif (data[y] < data[x]) swap(x, y);\n\t\t\tdata[x] += data[y]; data[y] = x;\n\t\t}\n\t\treturn x != y;\n\t}\n\tint root(int x) {\n\t\treturn data[x] < 0 ? x : data[x] = root(data[x]);\n\t}\n\tint size(int x) {\n\t\treturn -data[root(x)];\n\t}\n};\n\n\n\nint main() {\n\tint N;cin>>N;\n\tvector<pair<unsigned long long int, pair<int, int>>>edges;\n\tfor (int i = 0; i < N - 1; ++i) {\n\t\tint a,b,c;cin>>a>>b>>c;\n\t\tpair<unsigned long long int,pair<int,int>> edge(make_pair(c,make_pair(a,b)));\n\t\tedges.push_back(edge);\n\t}\n\tsort(edges.begin(),edges.end());\n\tUnionFind uf(N);\n\tunsigned long long int ans=0;\n\tfor (auto edge : edges) {\n\t\tint a=edge.second.first;\n\t\tint b=edge.second.second;\n\t\tunsigned long long int c=edge.first;\n\t\tans+=uf.size(a)*uf.size(b)*c;\n\t\tuf.unionSet(a,b);\n\t}\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 0.9918032786885246, "time_ms": 170, "memory_kb": 6888, "score_of_the_acc": -1.0966, "final_rank": 20 }, { "submission_id": "aoj_1507_2661975", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\nstruct UnionFind {\n\tvector<int> data;\n\tUnionFind(int size) : data(size, -1) { }\n\tbool unionSet(int x, int y) {\n\t\tx = root(x); y = root(y);\n\t\tif (x != y) {\n\t\t\tif (data[y] < data[x]) swap(x, y);\n\t\t\tdata[x] += data[y]; data[y] = x;\n\t\t}\n\t\treturn x != y;\n\t}\n\tint root(int x) {\n\t\treturn data[x] < 0 ? x : data[x] = root(data[x]);\n\t}\n\tint size(int x) {\n\t\treturn -data[root(x)];\n\t}\n};\n\n\n\nint main() {\n\tint N;cin>>N;\n\tvector<pair<long long int, pair<int, int>>>edges;\n\tfor (int i = 0; i < N - 1; ++i) {\n\t\tint a,b,c;cin>>a>>b>>c;\n\t\tpair<long long int,pair<int,int>> edge(make_pair(c,make_pair(a,b)));\n\t\tedges.push_back(edge);\n\t}\n\tsort(edges.begin(),edges.end());\n\tUnionFind uf(N);\n\tlong long int ans=0;\n\tfor (auto edge : edges) {\n\t\tint a=edge.second.first;\n\t\tint b=edge.second.second;\n\t\tlong long int c=edge.first;\n\t\tans+=uf.size(a)*uf.size(b)*c;\n\t\tuf.unionSet(a,b);\n\t}\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 0.9918032786885246, "time_ms": 140, "memory_kb": 7004, "score_of_the_acc": -0.8785, "final_rank": 19 }, { "submission_id": "aoj_1507_2661576", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 200000\n\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\t\treturn cost < arg.cost;\n\t}\n\tll from,to,cost;\n};\n\nInfo info[NUM];\nint N;\nll boss[NUM],height[NUM],member_num[NUM];\n\nint get_boss(int id){\n\tif(boss[id] == id)return id;\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]);\n\t}\n}\n\nint is_same(int x,int y){\n\treturn get_boss(x) == get_boss(y);\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\tif(boss_x == boss_y)return;\n\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\t\tmember_num[boss_x] += member_num[boss_y];\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\t\tmember_num[boss_y] += member_num[boss_x];\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\tmember_num[boss_x] += member_num[boss_y];\n\t\theight[x]++;\n\t}\n}\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tscanf(\"%lld %lld %lld\",&info[i].from,&info[i].to,&info[i].cost);\n\t}\n\n\tsort(info,info+N-1);\n\n\tfor(int i = 0; i < N; i++){\n\t\tboss[i] = i;\n\t\theight[i] = 0;\n\t\tmember_num[i] = 1;\n\t}\n\n\tll ans = 0;\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tans += member_num[get_boss(info[i].from)]*member_num[get_boss(info[i].to)]*info[i].cost;\n\t\tunite(info[i].from,info[i].to);\n\t}\n\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 12632, "score_of_the_acc": -0.7987, "final_rank": 10 }, { "submission_id": "aoj_1507_2661573", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 200000\n\n\nstruct Info{\n\tbool operator<(const struct Info &arg) const{\n\t\treturn cost < arg.cost;\n\t}\n\tll from,to,cost;\n};\n\nInfo info[NUM];\nint N;\nint boss[NUM],height[NUM],member_num[NUM];\n\nint get_boss(int id){\n\tif(boss[id] == id)return id;\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]);\n\t}\n}\n\nint is_same(int x,int y){\n\treturn get_boss(x) == get_boss(y);\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\tif(boss_x == boss_y)return;\n\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\t\tmember_num[boss_x] += member_num[boss_y];\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\t\tmember_num[boss_y] += member_num[boss_x];\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\tmember_num[boss_x] += member_num[boss_y];\n\t\theight[x]++;\n\t}\n}\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tscanf(\"%lld %lld %lld\",&info[i].from,&info[i].to,&info[i].cost);\n\t}\n\n\tsort(info,info+N-1);\n\n\tfor(int i = 0; i < N; i++){\n\t\tboss[i] = i;\n\t\theight[i] = 0;\n\t\tmember_num[i] = 1;\n\t}\n\n\tll ans = 0;\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tans += member_num[get_boss(info[i].from)]*member_num[get_boss(info[i].to)]*info[i].cost;\n\t\tunite(info[i].from,info[i].to);\n\t}\n\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 0.9918032786885246, "time_ms": 60, "memory_kb": 10292, "score_of_the_acc": -0.621, "final_rank": 18 } ]
aoj_1509_cpp
Problem A: Rental DVD Shop NEO Problem 僕はレンタルDVDショップ、「NEO」でアルバイトを始めた。まずはこの店の料金システムを勉強することになった。 レンタルDVDは旧作、準新作、新作の3種類存在し、1本のレンタル料金がそれぞれ a 円、 b 円、 c 円である。 会計の際に以下に示すセットレンタルを複数回適用することができる。 まだセットレンタルを適用していないDVDを数本選ぶ。 選んだDVDの本数が d 本以上の場合、選んだDVDの合計金額が、 (選んだDVDの本数) * e 円を超える場合は、それらを (選んだDVDの本数) * e 円でレンタル出来る。 選んだDVDの本数が d 本未満の場合、選んだDVDの合計金額が d * e 円を超える場合は、それらを d * e 円でレンタル出来る。 上記に当てはまらない場合、選んだDVDは通常料金でレンタルとなる。 ここで僕はある問題に気づいた。セットレンタルはレジに通した時に自動的に適用されるのではなく、手動で適用しているのだ。これでは最適ではない(もっと安くなる余地がある)セットレンタルの適用をしてしまう可能性がある。これではクレームが発生しかねない。僕はクレーム対応が嫌いなので、セットレンタルを最適に適用した時の料金を計算するプログラムを作成することにした。 Input 入力は複数のデータセットからなる。 各データセットは以下で表される。 1行目は5つの整数 a , b , c , d , e がスペース区切りで与えられる。 2行目にはレンタル本数が与えられる。3つの整数 na , nb , nc がスペース区切りで与えられる。それぞれ旧作、準新作、新作のDVDの本数を表す。 入力の終わりは5つのゼロからなる。 a b c d e na nb nc Constraints 入力は以下の条件を満たす。 入力に含まれる値はすべて整数 0 < a < b < e < c ≤ 1000 0 < d ≤ 100000 0 ≤ na , nb , nc ≤ 100000 0 < na + nb + nc データセットの数は100個以下 Output 各データセットにつき、セットレンタルを最適に適用した時の料金を1行に出力せよ。 Sample Input 70 100 340 4 200 1 1 4 70 100 340 4 200 0 1 3 70 100 340 4 200 1 1 2 0 0 0 0 0 Sample Output 970 800 800
[ { "submission_id": "aoj_1509_8293237", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nlong long A, B, C, D, E;\nlong long NA, NB, NC;\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tcin >> A >> B >> C >> D >> E; if (A + B + C + D + E == 0) break;\n\t\tcin >> NA >> NB >> NC;\n\n\t\t// Step 2. Easy Case\n\t\tif (NC >= D) {\n\t\t\tcout << NC * E + NB * B + NA * A << endl;\n\t\t}\n\t\t\n\t\t// Step 3. Hard Case\n\t\telse {\n\t\t\tvector<long long> vec;\n\t\t\tfor (int i = 0; i < NC; i++) vec.push_back(C);\n\t\t\tfor (int i = 0; i < NB; i++) vec.push_back(B);\n\t\t\tfor (int i = 0; i < NA; i++) vec.push_back(A);\n\t\t\tlong long cost2 = NA * A + NB * B + NC * C;\n\t\t\tlong long cost1 = D * E;\n\t\t\tfor (int i = D; i < vec.size(); i++) cost1 += vec[i];\n\t\t\tcout << min(cost1, cost2) << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 6976, "score_of_the_acc": -0.7853, "final_rank": 10 }, { "submission_id": "aoj_1509_4387158", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))\nusing ll = long long;\nusing namespace std;\n\n#define INF ((1<<30)-1)\n#define LLINF (1LL<<60)\n#define EPS (1e-10)\n\nint main() {\n ll a, b, c, d, e;\n while (cin >> a >> b >> c >> d >> e) {\n if (a == b and b == c and c == d and d == e and a == 0) break;\n ll na, nb, nc;\n cin >> na >> nb >> nc;\n\n // 新作の値段は,制約よりEより高いので,新作はセットにするほうが良い\n if (nc >= d) {\n // 新作すべてをセットにして,d本以上にする場合\n // それ以外のをセットにしてしまうと,制約より旧作,準新作の値段よりeのほうが高いのでやらないほうが良い\n ll ans = a * na + b * nb + e * nc;\n cout << ans << endl;\n }\n else {\n // dの本数が小さい場合のみ,加えた方が良いかというのを準新作のほうから見ていく?\n\n ll ans = a * na + b * nb;\n if (nc > 0) ans += d * e;\n //cout << ans << endl;\n\n ll tmpd = d - nc;\n\n ll tmpb = nb, tmpa = na, cnt = nc;\n while (true) {\n // セットに含める\n if (tmpb > 0) {\n tmpb--;\n tmpd--;\n ++cnt;\n }\n else if (tmpa > 0) {\n tmpa--;\n tmpd--;\n cnt++;\n }\n else {\n if (tmpd > 0) {\n if (ans > d * e) {\n ans = d * e;\n }\n }\n else {\n if (ans > cnt * e) {\n ans = cnt * e;\n }\n }\n break;\n }\n\n if (tmpd > 0) {\n if (ans > a * tmpa + b * tmpb + d * e) {\n ans = a * tmpa + b * tmpb + d * e;\n }\n }\n else {\n if (ans > a * tmpa + b * tmpb + cnt * e) {\n ans = a * tmpa + b * tmpb + cnt * e;\n }\n }\n }\n\n cout << min(a * na + b * nb + c * nc, ans) << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3092, "score_of_the_acc": -0.2494, "final_rank": 4 }, { "submission_id": "aoj_1509_2090127", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nlong long a, b, c, d, e, na, nb, nc;\nlong long F[500000];\nlong long price(long long L) {\n\tif (L < nc)return c;\n\tif (L < nb + nc)return b;\n\treturn a;\n}\nlong long ruisekiwa(long long L, long long R) {\n\tif (L >= R)return 0;\n\tif (L == 0)return F[R - 1];\n\treturn F[R - 1] - F[L - 1];\n}\nint main() {\n\twhile (true) {\n\t\tcin >> a >> b >> c >> d >> e; if (a == 0)break;\n\t\tcin >> na >> nb >> nc;\n\t\tlong long n = na + nb + nc, minx = na*a + nb*b + nc*c;\n\t\tfor (long long i = 0; i < 500000; i++)F[i] = 0;\n\t\tfor (long long i = 0; i < n; i++)F[i] = price(i);\n\t\tfor (long long i = 1; i < n; i++)F[i] += F[i - 1];\n\n\t\tfor (long long i = 0; i <= n; i++) {\n\t\t\tlong long set1 = ruisekiwa(0, i);\n\t\t\tlong long set2 = ruisekiwa(i, min(n, i + d));\n\t\t\tlong long set3 = ruisekiwa(min(n, i + d), n);\n\t\t\tif (i >= d) {\n\t\t\t\tif (set1 >= i*e)set1 = i*e;\n\t\t\t}\n\t\t\tif (i < d) {\n\t\t\t\tif (set1 >= d*e)set1 = d*e;\n\t\t\t}\n\t\t\tif (set2 >= d*e)set2 = d*e;\n\t\t\tminx = min(minx, set1 + set2 + set3);\n\t\t}\n\t\tcout << minx << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 6992, "score_of_the_acc": -1.0374, "final_rank": 11 }, { "submission_id": "aoj_1509_2090123", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nlong long a, b, c, d, e, na, nb, nc;\nlong long F[500000];\nlong long price(long long L) {\n\tif (L < nc)return c;\n\tif (L < nb + nc)return b;\n\treturn a;\n}\nlong long ruisekiwa(long long L, long long R) {\n\tif (L >= R)return 0;\n\tif (L == 0)return F[R - 1];\n\treturn F[R - 1] - F[L - 1];\n}\nint main() {\n\twhile (true) {\n\t\tcin >> a >> b >> c >> d >> e; if (a == 0)break;\n\t\tcin >> na >> nb >> nc;\n\t\tlong long n = na + nb + nc, minx = na*a + nb*b + nc*c;\n\t\tfor (long long i = 0; i < 500000; i++)F[i] = 0;\n\t\tfor (long long i = 0; i < n; i++)F[i] = price(i);\n\t\tfor (long long i = 1; i < n; i++)F[i] += F[i - 1];\n\n\t\tfor (long long i = d; i <= n; i++) {\n\t\t\tlong long set1 = ruisekiwa(0, i);\n\t\t\tlong long set2 = ruisekiwa(i, min(n, i + d));\n\t\t\tlong long set3 = ruisekiwa(min(n, i + d), n);\n\t\t\tif (set1 >= i*e)set1 = i*e;\n\t\t\tif (set2 >= d*e)set2 = d*e;\n\t\t\tminx = min(minx, set1 + set2 + set3);\n\t\t}\n\t\tcout << minx << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.8, "time_ms": 80, "memory_kb": 6988, "score_of_the_acc": -1.0012, "final_rank": 14 }, { "submission_id": "aoj_1509_2090120", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nlong long a, b, c, d, e, na, nb, nc;\nlong long F[500000];\nlong long price(long long L) {\n\tif (L < nc)return c;\n\tif (L < nb + nc)return b;\n\treturn a;\n}\nlong long ruisekiwa(long long L, long long R) {\n\tif (L >= R)0;\n\tif (L == 0)return F[R - 1];\n\treturn F[R - 1] - F[L - 1];\n}\nint main() {\n\twhile (true) {\n\t\tcin >> a >> b >> c >> d >> e; if (a == 0)break;\n\t\tcin >> na >> nb >> nc;\n\t\tlong long n = na + nb + nc, minx = na*a + nb*b + nc*c;\n\t\tfor (long long i = 0; i < 500000; i++)F[i] = 0;\n\t\tfor (long long i = 0; i < n; i++)F[i] = price(i);\n\t\tfor (long long i = 1; i < n; i++)F[i] += F[i - 1];\n\n\t\tfor (long long i = d; i <= n; i++) {\n\t\t\tlong long set1 = ruisekiwa(0, i);\n\t\t\tlong long set2 = ruisekiwa(i, min(n, i + d));\n\t\t\tlong long set3 = ruisekiwa(min(n, i + d), n);\n\t\t\tif (set1 >= i*e)set1 = i*e;\n\t\t\tif (set2 >= d*e)set2 = d*e;\n\t\t\tminx = min(minx, set1 + set2 + set3);\n\t\t}\n\t\tcout << minx << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.8, "time_ms": 80, "memory_kb": 6992, "score_of_the_acc": -1.0017, "final_rank": 15 }, { "submission_id": "aoj_1509_2090117", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nint a, b, c, d, e, na, nb, nc;\nint F[500000];\nint price(int L) {\n\tif (L < nc)return c;\n\tif (L < nb + nc)return b;\n\treturn a;\n}\nint ruisekiwa(int L, int R) {\n\tif (L >= R)0;\n\tif (L == 0)return F[R - 1];\n\treturn F[R - 1] - F[L - 1];\n}\nint main() {\n\twhile (true) {\n\t\tcin >> a >> b >> c >> d >> e; if (a == 0)break;\n\t\tcin >> na >> nb >> nc;\n\t\tint n = na + nb + nc, minx = na*a + nb*b + nc*c;\n\t\tfor (int i = 0; i < 500000; i++)F[i] = 0;\n\t\tfor (int i = 0; i < n; i++)F[i] = price(i);\n\t\tfor (int i = 1; i < n; i++)F[i] += F[i - 1];\n\n\t\tfor (int i = d; i <= n; i++) {\n\t\t\tint set1 = ruisekiwa(0, i);\n\t\t\tint set2 = ruisekiwa(i, min(n, i + d));\n\t\t\tint set3 = ruisekiwa(min(n, i + d), n);\n\t\t\tif (set1 >= i*e)set1 = i*e;\n\t\t\tif (set2 >= d*e)set2 = d*e;\n\t\t\tminx = min(minx, set1 + set2 + set3);\n\t\t}\n\t\tcout << minx << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.8, "time_ms": 70, "memory_kb": 5040, "score_of_the_acc": -0.7145, "final_rank": 13 }, { "submission_id": "aoj_1509_2040879", "code_snippet": "#include <vector>\n#include <iostream>\nusing namespace std;\nint a, b, c, d, e, x, y, z;\nint main() {\n\twhile(cin >> a >> b >> c >> d >> e, a) {\n\t\tcin >> x >> y >> z;\n\t\tvector<int> v;\n\t\tfor(int i = 0; i < z; i++) v.push_back(c);\n\t\tfor(int i = 0; i < y; i++) v.push_back(b);\n\t\tfor(int i = 0; i < x; i++) v.push_back(a);\n\t\tint g = a * x + b * y + c * z, sum = 0, ret = g;\n\t\tfor(int i = 1; i <= v.size(); i++) {\n\t\t\tsum += v[i - 1];\n\t\t\tif(i >= d) {\n\t\t\t\tif(sum > i * e) ret = min(ret, g - (sum - i * e));\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif(sum > d * e) ret = min(ret, g - (sum - d * e));\n\t\t\t}\n\t\t}\n\t\tcout << ret << endl;\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 6068, "score_of_the_acc": -0.7755, "final_rank": 9 }, { "submission_id": "aoj_1509_1840240", "code_snippet": "#include <algorithm>\n#include <iostream>\nusing namespace std;\nint main() {\n int a, b, c, d, e, na, nb, nc;\n while (cin >> a >> b >> c >> d >> e, a) {\n cin >> na >> nb >> nc;\n int mn = 1 << 30;\n for (int i = 0; i <= na + nb + nc; ++i) {\n int ct1, ct2, ct3, ct4, ct5, ct6;\n ct1 = min(max(0, i), na);\n ct4 = min(max(0, na - i), na);\n ct2 = min(max(0, i - na), nb);\n ct5 = min(max(0, na + nb - i), nb);\n ct3 = min(max(0, i - na - nb), nc);\n ct6 = min(max(0, na + nb + nc - i), nc);\n mn = min(mn, min(ct4 * a + ct5 * b + ct6 * c, max(ct4 + ct5 + ct6, d) * e) + ct1 * a + ct2 * b + ct3 * c);\n }\n cout << mn << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3108, "score_of_the_acc": -0.4657, "final_rank": 5 }, { "submission_id": "aoj_1509_1785281", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <cstring>\n#include <algorithm>\n#include <sstream>\n#include <map>\n#include <set>\n\n#define REP(i,k,n) for(int i=k;i<n;i++)\n#define rep(i,n) for(int i=0;i<n;i++)\n#define INF 1<<30\n#define pb push_back\n#define mp make_pair\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint main() {\n\tll a, b, c, d, e;\n\twhile(true) {\n\t\tcin >> a >> b >> c >> d >> e;\n\n\t\tif(a == 0 && b == 0 && c == 0 && d == 0 && e == 0) break;\n\n\t\tll na, nb, nc;\n\t\tcin >> na >> nb >> nc;\n\n\t\tvector<ll> v;\n\t\trep(i, na) v.push_back(a);\n\t\trep(i, nb) v.push_back(b);\n\t\trep(i, nc) v.push_back(c);\n\n\t\tsort(v.begin(), v.end(), greater<ll>());\n\n\t\tll sum = a * na + b * nb + c * nc;\n\t\tll ans = sum, pre = 0;\n\n\t\trep(i, v.size()) {\n\t\t\tpre += v[i];\n\t\t\tsum -= v[i];\n\t\t\tif(i >= d-1) {\n\t\t\t\tans = min(ans, sum + min(pre, (i + 1) * e));\n\t\t\t} else {\n\t\t\t\tans = min(ans, sum + min(pre, d * e));\n\t\t\t}\n\t\t}\n\n\t\tcout << ans << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 8656, "score_of_the_acc": -1.966, "final_rank": 12 }, { "submission_id": "aoj_1509_1783004", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nvector<ll> v;\nint main(){\n ll a,b,c,d,e;\n ll na,nb,nc;\n while( cin >> a >> b >> c >> d >> e && (a|b|c|d|e) ){\n cin >> na >> nb >> nc;\n v.clear();\n for(int i=0;i<nc;i++) v.push_back(c);\n for(int i=0;i<nb;i++) v.push_back(b);\n for(int i=0;i<na;i++) v.push_back(a);\n ll sum = 0;\n ll reg = na*a + nb*b + nc*c;\n ll res = reg;\n for(int i=0;i<na+nb+nc;i++){ \n sum += v[i];\n if( i+1 >= d ){\n res = min( res, reg - sum + ((ll)i+1)*e );\n } else {\n res = min( res, reg - sum + d * e );\n }\n }\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 5984, "score_of_the_acc": -0.7647, "final_rank": 8 }, { "submission_id": "aoj_1509_1782995", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int a,b,c,d,e;\n while(cin >> a >> b >> c >> d >> e && a) {\n int na,nb,nc;\n cin >> na >> nb >> nc;\n vector<int> v;\n for(int i=0;i<nc;i++)v.push_back(c);\n for(int i=0;i<nb;i++)v.push_back(b);\n for(int i=0;i<na;i++)v.push_back(a);\n int sum=na*a+nb*b+nc*c;\n int ans=sum,x=0;\n for(int i=0;i<v.size();i++) {\n int y=i+1;\n x+=v[i];\n sum-=v[i];\n if(y>=d) ans=min(ans,y*e+sum);\n else if(x>=d*e) ans=min(ans,d*e+sum);\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4240, "score_of_the_acc": -0.6115, "final_rank": 7 }, { "submission_id": "aoj_1509_1548864", "code_snippet": "#include <iostream>\nusing namespace std;\n\nint main() {\n\tint a, b, c, d, e;\n\twhile(cin >> a >> b >> c >> d >> e, a|b|c|d|e) {\n\t\tint na, nb, nc;\n\t\tcin >> na >> nb >> nc;\n\t\tint sum = na*a + nb*b + nc*c;\n\t\tint psum = sum;\n\t\tint res = sum;\n\t\tfor(int i = 0; i < na+nb+nc; i++) {\n\t\t\tif(i<nc)\n\t\t\t\tpsum -= c;\n\t\t\telse if(i<nc+nb)\n\t\t\t\tpsum -= b;\n\t\t\telse\n\t\t\t\tpsum -= a;\n\t\t\tres = min(res ,min(max(i+1,d)*e, sum-psum) + psum);\n\t\t}\n\t\tcout << res << endl;\n\t}\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1160, "score_of_the_acc": -0.0719, "final_rank": 1 }, { "submission_id": "aoj_1509_1548341", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n\nusing namespace std;\n\nint main() {\n\tlong long int m[3];\n\tlong long int d, e;\n\tlong long int nn[3];\n\twhile(cin >> m[0] >> m[1] >> m[2] >> d >> e) {\n\t\tif (m[0] == 0 && m[1] == 0 && m[2] == 0 && d == 0 && e == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tcin >> nn[0] >> nn[1] >> nn[2]; \n\t\tlong long int count = m[0]*nn[0]+m[1]*nn[1]+m[2]*nn[2];\n\t\tvector<int> v;\n\t\tfor (int i = 0; i < nn[2]; i++) {\n\t\t\tv.push_back(m[2]);\n\t\t}\n\t\tfor (int i = 0; i < nn[1]; i++) {\n\t\t\tv.push_back(m[1]);\n\t\t}\n\t\tfor (int i = 0; i < nn[0]; i++) {\n\t\t\tv.push_back(m[0]);\n\t\t}\n\t\tif (d <= nn[2]) {\n\t\t\tcount = m[0]*nn[0]+m[1]*nn[1]+e*nn[2];\n\t\t} else if (d <= nn[1]+nn[2]){\n\t\t\tcount = min(count, min(m[0]*nn[0]+m[1]*(nn[1]-(d-nn[2]))+e*d, m[0]*nn[0]+m[1]*nn[1]+d*e));\n\t\t} else if (d <= nn[0]+nn[1]+nn[2]){\n\t\t\tcount = min(count, min(m[0]*(nn[0]-(d-nn[2]-nn[1]))+e*d, m[0]*nn[0]+m[1]*nn[1]+d*e));\n\t\t} else {\n\t\t\tcount = min(count, d*e);\n\t\t}\n\t\tcout << count << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 4236, "score_of_the_acc": -0.5753, "final_rank": 6 }, { "submission_id": "aoj_1509_1548307", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n\nusing namespace std;\n\nint main() {\n\tlong long int m[3];\n\tlong long int d, e;\n\tlong long int nn[3];\n\twhile(cin >> m[0] >> m[1] >> m[2] >> d >> e) {\n\t\tif (m[0] == 0 && m[1] == 0 && m[2] == 0 && d == 0 && e == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tcin >> nn[0] >> nn[1] >> nn[2]; \n\t\tlong long int count = m[0]*nn[0]+m[1]*nn[1]+m[2]*nn[2];\n\t\tvector<int> v;\n\t\tfor (int i = 0; i < nn[2]; i++) {\n\t\t\tv.push_back(m[2]);\n\t\t}\n\t\tfor (int i = 0; i < nn[1]; i++) {\n\t\t\tv.push_back(m[1]);\n\t\t}\n\t\tfor (int i = 0; i < nn[0]; i++) {\n\t\t\tv.push_back(m[0]);\n\t\t}\n\t\tfor (int i = 0; i < nn[0]+nn[1]+nn[2]; i+=d) {\n\t\t\tif (i + d-1 < nn[0]+nn[1]+nn[2]) {\n\t\t\t\tlong long int counter = 0;\n\t\t\t\tfor (int j = 0; j < d; j++) {\n\t\t\t\t\tcounter += v[i+j];\n\t\t\t\t}\n\t\t\t\tif (counter > d*e) {\n\t\t\t\t\tcount -= counter;\n\t\t\t\t\tcount += d*e;\n\t\t\t\t} else {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << count << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 60, "memory_kb": 4236, "score_of_the_acc": -0.5753, "final_rank": 16 }, { "submission_id": "aoj_1509_1408184", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\n#define REP(i,n) for(int i = 0 ; i < n ; ++i)\n#define int long long\n#define INF 99999999\n\nsigned main()\n{\n\twhile (1){\n\t\tint a, b, c, d, e, na, nb, nc;\n\t\tcin >> a >> b >> c >> d >> e >> na >> nb >> nc;\n\n\t\tif (!(a || b || c || d || e))break;\n\n\t\tint ans = (na*a+nb*b+nc*c);\n\n\t\tREP(i, na + nb + nc+1){\n\t\t\tint C = min(i, nc);\n\t\t\tint B = min(i - C, nb);\n\t\t\tint A = min(i - B - C, na);\n\t\t\tint D = max(i, d);\n\t\t\tif (!i)D = 0;\n\t\t\tans = min(ans, (nc - C)*c + (nb - B)*b + (na - A)*a + D*e);\n\t\t\t//cout << i << \":\" << ans << endl;\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1156, "score_of_the_acc": -0.1786, "final_rank": 2 }, { "submission_id": "aoj_1509_1408183", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\n#define REP(i,n) for(int i = 0 ; i < n ; ++i)\n#define int long long\n#define INF 99999999\n\nbool lc, lb, la;\n\nsigned main()\n{\n\tint a, b, c, d, e,na,nb,nc;\n\twhile (1){\n\t\tcin >> a >> b >> c >> d >> e >> na >> nb >> nc;\n\n\t\tif (!(a || b || c || d || e))break;\n\n\t\tint ans = INF;\n\n\t\tREP(i, na + nb + nc+1){\n\t\t\tint C = min(i, nc);\n\t\t\tint B = min(i - C, nb);\n\t\t\tint A = min(i - B - C, na);\n\t\t\tint D = max(i, d);\n\t\t\tif (!i)D = 0;\n\t\t\tans = min(ans, (nc - C)*c + (nb - B)*b + (na - A)*a + D*e);\n\t\t\t//cout << i << \":\" << ans << endl;\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.1, "time_ms": 10, "memory_kb": 1156, "score_of_the_acc": 0, "final_rank": 18 }, { "submission_id": "aoj_1509_1408163", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\n#define REP(i,n) for(int i = 0 ; i < n ; ++i)\n#define int long long\n#define INF 99999999\n\nbool lc, lb, la;\n\nsigned main()\n{\n\tint a, b, c, d, e,na,nb,nc;\n\twhile (1){\n\t\tcin >> a >> b >> c >> d >> e >> na >> nb >> nc;\n\n\t\tif (!(a || b || c || d || e))break;\n\n\t\tint ans = INF;\n\n\t\tREP(i, na + nb + nc+1){\n\t\t\tint C = min(i, nc);\n\t\t\tint B = min(i - C, nb);\n\t\t\tint A = min(i - B - C, na);\n\t\t\tint D = max(i, d);\n\t\t\tans = min(ans, (nc - C)*c + (nb - B)*b + (na - A)*a + D*e);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.1, "time_ms": 10, "memory_kb": 1156, "score_of_the_acc": 0, "final_rank": 18 }, { "submission_id": "aoj_1509_1408111", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nconst int INF = (int)1e8;\nint main(){\n\tint a, b, c, d, e, na, nb, nc;\n\n\twhile (1){\n\t\tcin >> a >> b >> c >> d >> e;\n\t\tif (a == 0 && b == 0 && c == 0 && d == 0 && e == 0)return 0;\n\t\tcin >> na >> nb >> nc;\n\t\tint res = na*a+nb*b+nc*c;\n\t\tif (nc >= d){\n\t\t\tres=min(res, nc*e + na*a + nb*b);\n\t\t}\n\n\t\tfor (int i = 0; i <= na + nb + nc;i++){\n\t\t\tint C = max(nc - i, 0);\n\t\t\tint B = max(nb - (i - (nc - C)), 0);\n\t\t\tint A = na - (i - (nc - C) - (nb - B));\n\t\t\tif ((nc - C)*c + (nb - B)*b + (na-A)*a>d*e)\n\t\t\tres = min(res, max(d,i)*e + C*c + B*b + A*a);\n\t\t}\n\t\tcout << res << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1160, "score_of_the_acc": -0.1791, "final_rank": 3 }, { "submission_id": "aoj_1509_1408109", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nconst int INF = (int)1e8;\nint main(){\n\tint a, b, c, d, e, na, nb, nc;\n\n\twhile (1){\n\t\tcin >> a >> b >> c >> d >> e;\n\t\tif (a == 0 && b == 0 && c == 0 && d == 0 && e == 0)return 0;\n\t\tcin >> na >> nb >> nc;\n\t\tint res = INF;\n\t\tif (nc >= d){\n\t\t\tres=min(res, nc*e + na*a + nb*b);\n\t\t}\n\n\t\tfor (int i = 0; i <= na + nb + nc;i++){\n\t\t\tint C = max(nc - i, 0);\n\t\t\tint B = max(nb - (i - (nc - C)), 0);\n\t\t\tint A = na - (i - (nc - C) - (nb - B));\n\t\t\tif ((nc - C)*c + (nb - B)*b + (na-A)*a>d*e)\n\t\t\tres = min(res, max(d,i)*e + C*c + B*b + A*a);\n\t\t}\n\t\tcout << res << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 0.1, "time_ms": 10, "memory_kb": 1160, "score_of_the_acc": -0.0005, "final_rank": 20 }, { "submission_id": "aoj_1509_1143859", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\nint main(){\n int a, b, c, d, e;\n int na, nb, nc;\n while(cin >> a >> b >> c >> d >> e >> na >> nb >> nc) {\n LL ans = 0;\n\n vector<LL> v;\n REP(i, nc) v.push_back(c);\n REP(i, nb) v.push_back(b);\n REP(i, na) v.push_back(a);\n\n int n = v.size();\n vector<LL> sum(n + 1);\n REP(i, n) sum[i+1] = sum[i] + v[i];\n\n for(int i = 0; i < n; i += d) {\n LL num = (i + d <= n ? d : n - i);\n LL x = (i + d <= n) ? (sum[i + d] - sum[i]) : (sum[n] - sum[i]);\n LL y = (i == 0 ? d * e : num * e);\n ans += min(x, y);\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 0.2, "time_ms": 100, "memory_kb": 8920, "score_of_the_acc": -1.3214, "final_rank": 17 } ]
aoj_1510_cpp
Problem B: Independent Research Problem 僕は夏休みの自由研究のテーマとして、生き物観察を選択し、生き物観察キットを購入した。 この生き物は3次元グリッド状の空間を好んで生息する。 各セルには最大1匹までしか入れない。 周囲の環境に合わせ1日が経過するごとに誕生と死滅を繰り返す。 誕生と死滅の条件は、セルに隣接する生き物の数に依存する。 ここで、あるセルに生き物が隣接するとは、あるセルと生き物が生息しているもう一つのセルが面、辺、または点を共有していることを言う。 誕生と死滅のルールは次のようになる。 生き物が生息していないセルにおいて、隣接する生き物の数が a i (1 ≤ i ≤ M 1 ) 匹であるような i がある場合、そのセルに生き物が誕生する。 生き物が生息しているセルにおいて、隣接する生き物の数が b j (1 ≤ j ≤ M 2 ) 匹であるような j がない場合、そのセルの生き物は死滅する。 今回購入した飼育箱はセルの数が5*5*5である立方体の飼育箱だ。 この飼育箱ではこの生き物はどのような振る舞いをするのだろうか…? とても楽しみである。 〜数日後〜 とりあえず、飼育してみたが…  毎日観察するなんて時間がかかるし、僕にはめんどくさくてやる気がおきない。 そうだ、コンピューターとプログラムを使ってシミュレートしよう。 Input 入力は複数のデータセットからなる。 各データセットは以下のフォーマットで与えられる。 N ( z = 0 の飼育箱の状態) (空行) ( z = 1 の飼育箱の状態) (空行) ( z = 2 の飼育箱の状態) (空行) ( z = 3 の飼育箱の状態) (空行) ( z = 4 の飼育箱の状態) (空行) M 1 a 1 a 2 … a M 1 M 2 b 1 b 2 … b M 2 最初にシミュレートする日数 N が与えられる。 次に飼育箱の初期状態の情報が与えられる。これは5つの5*5の2次元グリッドで与えられる。 各2次元グリッドは0と1から成り立ち、1は生き物がいることを示し、0は何もいないことを示す。 例えば、 z = 0のセルの状態の2次元グリッドの4行2列目の値が1だった場合、飼育箱の座標(1, 3, 0)の位置に生き物がいることを表す。 次に、整数 M 1 が与えられ、そのあとに M 1 個の数字 a i が与えられる。 次に、整数 M 2 が与えられ、そのあとに M 2 個の数字 b j が与えられる。 入力の終わりは N = 0で表される。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 100 0 ≤ M 1 , M 2 ≤ 27 0 ≤ a i , b j ≤ 26 (1 ≤ i ≤ M 1 , 1 ≤ j ≤ M 2 ) 任意の i , j (1 ≤ i < j ≤ M 1 ) において、 a i ≠ a j 任意の i , j (1 ≤ i < j ≤ M 2 ) において、 b i ≠ b j Output 各データセットに対し、 N 日経過後の状態を出力せよ。 出力は以下のフォーマットに従う。 Case (テストケースの番号): ( z = 0 の飼育箱の状態) (空行) ( z = 1 の飼育箱の状態) (空行) ( z = 2 の飼育箱の状態) (空行) ( z = 3 の飼育箱の状態) (空行) ( z = 4 の飼育箱の状態) 1行目にテストケース番号を出力し、次の行から N 日経過後の状態として、5*5の2次元グリッドを5つ出力せよ。 各テストケースの出力間には空行を出力しなさい。 Sample Input 5 00000 01010 00000 00100 00000 00000 01010 00000 01010 00000 00000 00100 00000 01010 00000 00000 01010 00000 00100 00000 00000 00000 00100 00000 00000 1 2 2 3 4 4 01110 00100 00100 00100 01110 01110 10001 10000 10001 01110 11111 10001 11111 10000 10000 01110 10001 10000 10001 01110 00000 00000 00000 00000 00000 2 3 4 1 2 100 00100 01010 01110 10001 10001 01110 00100 00100 00100 01110 00000 00000 00000 00000 00000 11111 00010 00100 01000 11111 10001 10001 10001 10001 01110 5 1 3 5 7 9 5 0 2 4 6 8 0 Sample Output Case 1: 00000 00000 01010 01110 10101 00000 10001 00000 00000 00000 00000 00000 11111 10001 00000 00000 00000 00000 00000 01010 00000 00000 01110 00100 11111 Case 2: 00010 10110 00000 11000 10000 00001 00000 10101 00010 00100 10001 00000 00000 00000 00010 01110 10001 00000 00001 10000 00000 00000 00111 01001 01000 Case 3: 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000
[ { "submission_id": "aoj_1510_2169979", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\n\nint f[2][5][5][5], m1, a[27], m2, b[27];\nint main() {\n\tint n, cnt = 1;\n\twhile (scanf(\"%d\", &n), n) {\n\t\tif (cnt != 1)printf(\"\\n\");\n\t\trep(i, 5)rep(j, 5)rep(k, 5)scanf(\"%1d\", &f[0][i][j][k]);\n\t\tscanf(\"%d\", &m1); rep(i, m1)scanf(\"%d\", &a[i]);\n\t\tscanf(\"%d\", &m2); rep(i, m2)scanf(\"%d\", &b[i]);\n\t\trep(i, n) {\n\t\t\tint t = i & 1;\n\t\t\trep(x, 5)rep(y, 5)rep(z, 5) {\n\t\t\t\tint cnt = 0;\n\t\t\t\tfor (int j = -1; j <= 1; j++)\n\t\t\t\t\tfor (int k = -1; k <= 1; k++)\n\t\t\t\t\t\tfor (int l = -1; l <= 1; l++) {\n\t\t\t\t\t\t\tif (j == 0 && k == 0 && l == 0)continue;\n\t\t\t\t\t\t\tint nx = x + j, ny = y + k, nz = z + l;\n\t\t\t\t\t\t\tif (0 <= nx&&nx < 5 && 0 <= ny&&ny < 5 && 0 <= nz&&nz < 5 && f[t][nx][ny][nz])cnt++;\n\t\t\t\t\t\t}\n\t\t\t\tif (f[t][x][y][z]) {\n\t\t\t\t\tif (find(b, b + m2, cnt) == b + m2)f[!t][x][y][z] = 0;\n\t\t\t\t\telse f[!t][x][y][z] = 1;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (find(a, a + m1, cnt) != a + m1)f[!t][x][y][z] = 1;\n\t\t\t\t\telse f[!t][x][y][z] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tprintf(\"Case %d:\\n\", cnt);\n\t\trep(i, 5) {\n\t\t\trep(j, 5) {\n\t\t\t\trep(k, 5)printf(\"%d\", f[n & 1][i][j][k]);\n\t\t\t\tprintf(\"\\n\");\n\t\t\t}\n\t\t\tif (i < 4)printf(\"\\n\");\n\t\t}\n\t\tcnt++;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3192, "score_of_the_acc": -1, "final_rank": 20 }, { "submission_id": "aoj_1510_1861670", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <cstdio>\n\nusing namespace std;\n\nint main() {\n\tint n;\n\tint aa = 1;\n\tint dx[] = {-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, /*0,*/ 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1};\n\tint dy[] = {-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, /*0,*/ 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1};\n\tint dz[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, /*0,*/ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};\n\twhile (cin >> n) {\n\t\tif (n == 0) {\n\t\t\tbreak;\n\t\t}\n\t\tif (aa != 1) {\n\t\t\tcout << endl;\n\t\t}\n\t\tint d[5][5][5];\n\t\tstring s;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tcin >> s;\n\t\t\t\tfor (int k = 0; k < 5; k++) {\n\t\t\t\t\td[k][j][i] = s[k]-'0';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t/*for (int i = 0; i < 5; i++) {\n\t\t\tif (i != 0) {\n\t\t\t\tcout << endl;\n\t\t\t} else {\n\t\t\t\tprintf(\"Case %d:\\n\", aa);\n\t\t\t}\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tfor (int k = 0; k < 5; k++) {\n\t\t\t\t\tcout << d[k][j][i];\n\t\t\t\t}\n\t\t\t\tcout << endl;\n\t\t\t}\n\t\t}\n\t\t/*cout << 'a' << endl;*/\n\t\tint m1, m2;\n\t\tcin >> m1;\n\t\tvector<int> a(m1);\n\t\tfor (int i = 0; i < m1; i++) {\n\t\t\tcin >> a[i];\n\t\t}\n\t\t//cout << m1 << endl;\n\t\tcin >> m2;\n\t\tvector<int> b(m2);\n\t\tfor (int i = 0; i < m2; i++) {\n\t\t\tcin >> b[i];\n\t\t}\n\t\t//cout << m2 << endl;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint d2[5][5][5];\n\t\t\tfor (int x = 0; x < 5; x++) {\n\t\t\t\tfor (int y = 0; y < 5; y++) {\n\t\t\t\t\tfor (int z = 0; z < 5; z++) {\n\t\t\t\t\t\tint ccc = 0;\n\t\t\t\t\t\tfor (int j = 0; j < 26; j++) {\n\t\t\t\t\t\t\tif (0 <= x+dx[j] && x+dx[j] < 5 && 0 <= y+dy[j] && y+dy[j] < 5 && 0 <= z+dz[j] && z+dz[j] < 5) {\n\t\t\t\t\t\t\t\tccc +=d[x+dx[j]][y+dy[j]][z+dz[j]];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\td2[x][y][z] = 0;\n\t\t\t\t\t\tif (d[x][y][z] == 0) {\n\t\t\t\t\t\t\tfor (int j = 0; j < m1; j++) {\n\t\t\t\t\t\t\t\tif (ccc == a[j]) {\n\t\t\t\t\t\t\t\t\td2[x][y][z] = 1;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tfor (int j = 0; j < m2; j++) {\n\t\t\t\t\t\t\t\tif (ccc == b[j]) {\n\t\t\t\t\t\t\t\t\td2[x][y][z] = 1;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int x = 0; x < 5; x++) {\n\t\t\t\tfor (int y = 0; y < 5; y++) {\n\t\t\t\t\tfor (int z = 0; z < 5; z++) {\n\t\t\t\t\t\td[x][y][z] = d2[x][y][z];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (i != 0) {\n\t\t\t\tcout << endl;\n\t\t\t} else {\n\t\t\t\tprintf(\"Case %d:\\n\", aa);\n\t\t\t}\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tfor (int k = 0; k < 5; k++) {\n\t\t\t\t\tcout << d[k][j][i];\n\t\t\t\t}\n\t\t\t\tcout << endl;\n\t\t\t}\n\t\t}\n\t\taa++;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1236, "score_of_the_acc": -0.0355, "final_rank": 15 }, { "submission_id": "aoj_1510_1827897", "code_snippet": "#include<bits/stdc++.h>\n#define M 27\n#define N 5\nusing namespace std;\nint n,c[2][N][N][N],m1,m2,a[M],b[M],num=1;\n\nint Count(int x,int y,int z,int t){\n int d[3]={-1,0,1},res=0;\n for(int i=0;i<3;i++)\n for(int j=0;j<3;j++)\n for(int k=0;k<3;k++){\n\tint nx=x+d[i],ny=y+d[j],nz=z+d[k];\n\tif(!d[i]&&i==j&&j==k)continue;\n\tif(nx<0||ny<0||nz<0)continue;\n\tif(N<=nx||N<=ny||N<=nz)continue;\n\tif(c[t][nx][ny][nz])res++;\n }\n return res;\n}\n\nint main(){\n while(1){\n cin>>n;\n if(!n)break;\n if(num>=2)cout<<endl;\n cout<<\"Case \"<<num<<':'<<endl;\n string s;\n for(int i=0;i<N;i++)\n for(int j=0;j<N;j++){\n\tcin>>s;\n\tfor(int k=0;k<N;k++){\n\t c[0][i][j][k]=s[k]-'0';\n\t}\n }\n cin>>m1;\n for(int i=0;i<m1;i++)cin>>a[i];\n cin>>m2;\n for(int i=0;i<m2;i++)cin>>b[i];\n for(int l=0;l<n;l++){\n int idx=l%2;\n for(int i=0;i<N;i++)\n\tfor(int j=0;j<N;j++){\n\t for(int k=0;k<N;k++){\n\t int r=Count(i,j,k,idx),f=0;\n\t if(!c[idx][i][j][k]){\n\t for(int p=0;p<m1;p++)\n\t\tif(r==a[p])f=1;\n\t if(f)c[!idx][i][j][k]=1;\n\t else c[!idx][i][j][k]=0;\n\t }else{\n\t for(int p=0;p<m2;p++)\n\t\tif(r==b[p])f=1;\n\t if(!f)c[!idx][i][j][k]=0;\n\t else c[!idx][i][j][k]=1;\n\t }\n\t }\n\t}\n }\n for(int i=0;i<N;i++){\n for(int j=0;j<N;j++){\n\tfor(int k=0;k<N;k++)\n\t cout<<c[n%2][i][j][k];\n\tcout<<endl;\n }\n if(i!=N-1)cout<<endl;\n }\n num++;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1212, "score_of_the_acc": -0.0237, "final_rank": 5 }, { "submission_id": "aoj_1510_1787100", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <cstring>\n#include <algorithm>\n#include <sstream>\n#include <map>\n#include <set>\n\n#define REP(i,k,n) for(int i=k;i<n;i++)\n#define rep(i,n) for(int i=0;i<n;i++)\n#define INF 1<<30\n#define pb push_back\n#define mp make_pair\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n, c = 0;\nint v[5][5][5], v2[5][5][5];\nbool a[30], b[30];\n\nint dx[26] = { 1,-1,\t 0, 0,\t 0, 0,\t 1, 1,-1,-1,\t 1, 1,-1,-1,\t 0, 0, 0, 0,\t 1, 1, 1, 1,\t-1,-1,-1,-1};\nint dy[26] = { 0, 0,\t 1,-1,\t 0, 0,\t 1,-1, 1,-1,\t 0, 0, 0, 0,\t 1, 1,-1,-1,\t 1, 1,-1,-1,\t 1, 1,-1,-1};\nint dz[26] = { 0, 0,\t 0, 0,\t 1,-1,\t 0, 0, 0, 0,\t 1,-1, 1,-1,\t 1,-1, 1,-1,\t 1,-1, 1,-1,\t 1,-1, 1,-1};\n\nbool can(int x,int y,int z) {\n\tif(0 <= x && x < 5 && 0 <= y && y < 5 && 0 <= z && z < 5) return true;\n\treturn false;\n}\n\nint main() {\n\twhile(cin >> n && n) {\n\t\tmemset(v, 0, sizeof(v));\n\t\tmemset(v2, 0, sizeof(v2));\n\n\t\trep(i, 5) {\n\t\t\trep(j, 5) {\n\t\t\t\tstring s;\n\t\t\t\tcin >> s;\n\n\t\t\t\trep(k, 5) {\n\t\t\t\t\tv[i][j][k] = s[k] - '0';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tint m1;\n\t\tcin >> m1;\n\n\t\tmemset(a, 0, sizeof(a));\n\t\trep(i, m1) {\n\t\t\tint x;\n\t\t\tcin >> x;\n\t\t\ta[x] = true;\n\t\t}\n\n\t\tint m2;\n\t\tcin >> m2;\n\t\tmemset(b, 0, sizeof(b));\n\t\trep(i, m2) {\n\t\t\tint x;\n\t\t\tcin >> x;\n\t\t\tb[x] = true;\n\t\t}\n\n\t\trep(q, n) {\n\t\t\trep(i, 5) rep(j, 5) rep(k, 5) v2[i][j][k] = v[i][j][k];\n\t\t\trep(i, 5) {\n\t\t\t\trep(j, 5) {\n\t\t\t\t\trep(k, 5) {\n\t\t\t\t\t\tint sum = 0;\n\n\t\t\t\t\t\trep(l, 26) {\n\t\t\t\t\t\t\tint x = i + dx[l];\n\t\t\t\t\t\t\tint y = j + dy[l];\n\t\t\t\t\t\t\tint z = k + dz[l];\n\n\t\t\t\t\t\t\tif(can(x, y, z)) sum += v[x][y][z];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(v[i][j][k] == 0) {\n\t\t\t\t\t\t\tif(a[sum]) {\n\t\t\t\t\t\t\t\tv2[i][j][k] = 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tif(!b[sum]) {\n\t\t\t\t\t\t\t\tv2[i][j][k] = 0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\trep(i, 5) rep(j, 5) rep(k, 5) v[i][j][k] = v2[i][j][k];\n\t\t}\n\n\t\tif(c) cout << endl;\n\t\tcout << \"Case \" << c + 1 << \":\" << endl;\n\t\tc++;\n\n\t\trep(i, 5) {\n\t\t\tif(i) cout << endl;\n\t\t\trep(j, 5) {\n\t\t\t\trep(k, 5) {\n\t\t\t\t\tcout << v[i][j][k];\n\t\t\t\t}\n\t\t\t\tcout << endl;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1216, "score_of_the_acc": -0.0256, "final_rank": 11 }, { "submission_id": "aoj_1510_1771665", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint m1,m2;\nstring mp[5][5],tmp[5][5];\nint A[30],B[30];\nint n;\n \nint Count(int z,int y,int x){\n int res=0;\n for(int i=-1;i<=1;i++)\n for(int j=-1;j<=1;j++){\n int nx=x+j,ny=y+i;\n if(nx<0||ny<0||nx>=5||ny>=5) continue;\n if((i!=0||j!=0)&&(mp[z][ny][nx])=='1')res++;\n if(z&&mp[z-1][ny][nx]=='1') res++;\n if(z<4&&mp[z+1][ny][nx]=='1')res++;\n }\n return res;\n}\n \nvoid solve(){\n while(n--){\n for(int i=0;i<5;i++)for(int j=0;j<5;j++) tmp[i][j]=mp[i][j];\n \n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++)\n for(int k=0;k<5;k++){\n int cnt=Count(i,j,k);\n for(int l=0;l<m1&&mp[i][j][k]=='0';l++) if(cnt==A[l]) tmp[i][j][k]='1';\n int f=(mp[i][j][k]=='1');\n for(int l=0;l<m2&&mp[i][j][k]=='1';l++) if(cnt==B[l]) f=0;\n if(f) tmp[i][j][k]='0';\n }\n for(int i=0;i<5;i++)for(int j=0;j<5;j++) mp[i][j]=tmp[i][j];\n }\n}\n \n \nint main(){\n int cnt=0;\n while(cin>>n,n){\n cnt++;\n if(cnt!=1)cout <<endl;\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++) cin>>mp[i][j];\n \n cin>> m1;\n for(int i=0;i<m1;i++) cin>>A[i];\n cin>>m2;\n for(int i=0;i<m2;i++) cin>>B[i];\n solve();\n cout <<\"Case \"<<cnt<<\":\"<<endl;\n for(int i=0;i<5;i++){\n for(int j=0;j<5;j++)cout << mp[i][j]<<endl;\n if(i!=4)cout <<endl;\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1216, "score_of_the_acc": -0.0256, "final_rank": 11 }, { "submission_id": "aoj_1510_1758345", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define MAX 26\nint c(int x,int y ,int z);\nint check(int x,int y ,int z);\n\nint cell[5][5][5];\nint before[5][5][5];\nint N,m1,m2;\nint a[MAX],b[MAX];\n\nint main(){\n int i,j,k,step;\n int count=1;\n string str;\n cin >> N;\n while(N!=0){\n for(k=0;k<5;k++){\n for(j=0;j<5;j++){\n cin >> str;\n for(i=0;i<5;i++){\n before[i][j][k]=str[i]-'0';\n if(before[i][j][k]>1)\n cout <<i<<j<<k<< endl;\n }\n }\n }\n \n cin >> m1;\n for(i=0;i<m1;i++) cin >> a[i];\n cin >> m2;\n for(i=0;i<m2;i++) cin >> b[i];\n \n\n for(step=0;step<N;step++){\n for(i=0;i<5;i++){\n for(j=0;j<5;j++){\n for(k=0;k<5;k++){\n cell[i][j][k]=c(i,j,k);\n }\n \n }\n }\n for(i=0;i<5;i++){\n for(j=0;j<5;j++){\n for(k=0;k<5;k++){\n before[i][j][k]=cell[i][j][k];\n }\n \n }\n }\n }\n cout << \"Case \" << count << \":\" << endl;\n for(k=0;k<5;k++){\n for(j=0;j<5;j++){\n for(i=0;i<5;i++){\n cout << cell[i][j][k];\n }\n cout << endl;\n }\n if(k!=4 )cout << endl;\n }\n count++;\n cin >> N;\n if(N!=0) cout << endl;\n }\n \n return 0;\n\n}\n\nint c(int x,int y ,int z){\n int r=0,i,j,k;\n for(i=0;i<3;i++){\n for(j=0;j<3;j++){\n for(k=0;k<3;k++){\n if(!(i==1&&j==1&&k==1)&&check(x-1+i,y-1+j,z-1+k)==1){\n r+=before[x-1+i][y-1+j][z-1+k];\n }\n }\n }\n }\n // cout << r << endl;\n if(before[x][y][z]==0){\n for(i=0;i<m1;i++){\n if(a[i]==r) return 1;\n }\n return 0;\n }else{\n for(i=0;i<m2;i++){\n if(b[i]==r) return 1;\n }\n return 0;\n }\n}\n\nint check(int x,int y ,int z){\n if(x<0||x>4||y<0||y>4||z<0||z>4) return 0;\n else return 1;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1208, "score_of_the_acc": -0.0217, "final_rank": 3 }, { "submission_id": "aoj_1510_1758209", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint casenumber = 0;\n\nconst int dx[] = {-1, 0, 1};\nconst int dy[] = {-1, 0, 1};\nconst int dz[] = {-1, 0, 1};\n\nbool inField(int x, int y, int z){\n return 0 <= x && 0 <= y && 0 <= z && x < 5 && y < 5 && z < 5;\n}\n\n\nint main(void){\n ios::sync_with_stdio(false);\n cin.tie(0);\n int N;\n bool flg = false;\n while(cin >> N, N){\n if(flg) cout << '\\n';\n flg = true;\n int M1, M2;\n set<int> m1, m2;\n //int g[5][5][5];\n vector< vector< vector<int> > > g(5, vector< vector<int> >(5, vector<int>(5)));\n for(int i=0; i < 5; i++){\n for(int j=0; j <5; j++){\n string str; cin >> str;\n for(int k=0; k < 5; k++){\n g[i][j][k] = str[k] - '0';\n }\n }\n }\n cin >> M1;\n for(int i=0; i < M1; i++){\n int tmp; cin >> tmp; m1.insert(tmp);\n }\n cin >> M2;\n for(int i=0; i < M2; i++){\n int tmp; cin >> tmp; m2.insert(tmp);\n }\n\n while(N--){\n vector< vector< vector<int> > > org = g;\n for(int i=0; i < 5; i++){\n for(int j=0; j < 5; j++){\n for(int k=0; k < 5; k++){\n\n int lvcnt = 0;\n for(int x=0; x < 3; x++){\n for(int y=0; y < 3; y++){\n for(int z=0; z < 3; z++){\n if(x == 1 && y == 1 && z == 1) continue;\n int nx = k + dx[x], ny = j + dy[y], nz = i + dz[z];\n if(!inField(nx, ny, nz)) continue;\n if(org[nz][ny][nx] == 1){\n lvcnt++;\n }\n }\n }\n }\n if(org[i][j][k] == 1){\n if(m2.find(lvcnt) == m2.end()) g[i][j][k] = 0;\n }else{\n if(m1.find(lvcnt) != m1.end()) g[i][j][k] = 1;\n }\n\n }\n }\n }\n }\n\n cout << \"Case \" << ++casenumber << \":\" << '\\n';\n for(int i=0; i < 5; i++){\n for(int j=0; j < 5; j++){\n for(int k=0; k < 5; k++){\n cout << g[i][j][k];\n }\n cout << '\\n';\n }\n if(i < 4)\n cout << '\\n';\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1244, "score_of_the_acc": -0.0394, "final_rank": 18 }, { "submission_id": "aoj_1510_1758123", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint N, M1, M2;\nchar cell[5][5][5];\nchar cccc[5][5][5];\nint a[27], b[27];\n\nint z;\n\nint adj(int x, int y, int z)\n{\n int ret = 0;\n for(int dz = -1; dz <= 1; dz++){\n for(int dy = -1; dy <= 1; dy++){\n for(int dx = -1; dx <= 1; dx++){\n\tif(dz != 0 || dy != 0 || dx != 0){\n\t if(0 <= z+dz && z+dz < 5 &&\n\t 0 <= y+dy && y+dy < 5 &&\n\t 0 <= x+dx && x+dx < 5){\n\t ret += (cccc[z+dz][y+dy][x+dx] == '1');\n\t }\n\t}\n }\n }\n }\n return ret;\n}\n\nvoid dfs(int n)\n{\n if(n == 0) return;\n \n for(int i = 0; i < 5; i++){\n for(int j = 0; j < 5; j++){\n for(int k = 0; k < 5; k++){\n\tcccc[i][j][k] = cell[i][j][k];\n }\n }\n } \n \n for(int i = 0; i < 5; i++){\n for(int j = 0; j < 5; j++){\n for(int k = 0; k < 5; k++){\n\tint cre = adj(k, j, i);\n\tif(cccc[i][j][k] == '0'){\n\t for(int l = 0; l < M1; l++){\n\t if(cre == a[l]){\n\t cell[i][j][k] = '1'; \n\t break;\n\t }\n\t }\n\t} else if(cccc[i][j][k] == '1'){\n\t bool flag = false;\n\t for(int l = 0; l < M2; l++){\n\t if(cre == b[l]){\n\t flag = true;\n\t break;\n\t }\n\t }\n\t if(!flag) cell[i][j][k] = '0';\n\t}\n }\n }\n }\n \n dfs(n-1);\n}\n\nint main()\n{\n for(int s = 0; ; s++){\n cin >> N;\n if(N == 0) break;\n if(s > 0) cout << endl;\n for(int i = 0; i < 5; i++){\n for(int j = 0; j < 5; j++){\n\tfor(int k = 0; k < 5; k++){\n\t cin >> cell[i][j][k];\n\t}\n }\n }\n\n cin >> M1;\n for(int i = 0; i < M1; i++) cin >> a[i];\n\n cin >> M2;\n for(int i = 0; i < M2; i++) cin >> b[i];\n \n dfs(N);\n \n cout << \"Case \" << s+1 << \":\" << endl;\n for(int i = 0; i < 5; i++){\n if(i > 0) cout << endl;\n for(int j = 0; j < 5; j++){\n\tfor(int k = 0; k < 5; k++){\n\t cout << cell[i][j][k];\n\t}\n\tcout << endl;\n }\n }\n } \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1168, "score_of_the_acc": -0.002, "final_rank": 2 }, { "submission_id": "aoj_1510_1758117", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint m1,m2;\nstring mp[5][5],tmp[5][5];\nint A[30],B[30];\nint n;\n\nint Count(int z,int y,int x){\n int res=0;\n for(int i=-1;i<=1;i++)\n for(int j=-1;j<=1;j++){\n int nx=x+j,ny=y+i;\n if(nx<0||ny<0||nx>=5||ny>=5) continue;\n if((i!=0||j!=0)&&(mp[z][ny][nx])=='1')res++;\n if(z&&mp[z-1][ny][nx]=='1') res++;\n if(z<4&&mp[z+1][ny][nx]=='1')res++;\n }\n return res;\n}\n\nvoid solve(){\n while(n--){\n for(int i=0;i<5;i++)for(int j=0;j<5;j++) tmp[i][j]=mp[i][j];\n\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++)\n\tfor(int k=0;k<5;k++){\n\t int cnt=Count(i,j,k);\n\t for(int l=0;l<m1&&mp[i][j][k]=='0';l++) if(cnt==A[l]) tmp[i][j][k]='1';\n\t int f=(mp[i][j][k]=='1');\n\t for(int l=0;l<m2&&mp[i][j][k]=='1';l++) if(cnt==B[l]) f=0;\n\t if(f) tmp[i][j][k]='0';\n\t}\n for(int i=0;i<5;i++)for(int j=0;j<5;j++) mp[i][j]=tmp[i][j];\n }\n}\n\n\nint main(){\n int cnt=0;\n while(cin>>n,n){\n cnt++;\n if(cnt!=1)cout <<endl;\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++) cin>>mp[i][j];\n\n cin>> m1;\n for(int i=0;i<m1;i++) cin>>A[i];\n cin>>m2;\n for(int i=0;i<m2;i++) cin>>B[i];\n solve();\n cout <<\"Case \"<<cnt<<\":\"<<endl;\n for(int i=0;i<5;i++){\n for(int j=0;j<5;j++)cout << mp[i][j]<<endl;\n if(i!=4)cout <<endl;\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1212, "score_of_the_acc": -0.0237, "final_rank": 5 }, { "submission_id": "aoj_1510_1408189", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n\nusing namespace std;\n\n#define EXIST '1'\n#define NONE '0'\n\ntypedef vector<string> VS;\ntypedef vector<VS> VVS;\ntypedef vector<VVS> VVVS;\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\n\nbool check(int x, int y, int z){\n\tif(x>=5||x<0)return false;\n\tif(y>=5||y<0)return false;\n\tif(z>=5||z<0)return false;\n\treturn true;\n}\n\nint main(){\n\tint t = 1;\n\twhile (1){\n\t\tint N,M;\n\t\tcin >> N;\n\t\tif (N == 0)return 0;\n\t\tif (t>=2)cout << endl;\n\t\tVVVS box(N+1,VVS(5, VS(5,\"00000\")));\n\n\t\tfor (int z = 0; z < 5; z++){\n\t\t\tfor (int x = 0; x < 5; x++){\n\t\t\t\tcin >> box[0][z][x];\n\t\t\t}\n\t\t}\n\t\tVVI pt(100, VI(27,NONE));\n\t\tcin >> M;\n\t\tfor (int i = 0; i < M; i++){\n\t\t\tint t; cin >> t;\n\t\t\tpt[NONE][t] = EXIST;\n\t\t}\n\t\tcin >> M;\n\t\tfor (int i = 0; i < M; i++){\n\t\t\tint t; cin >> t;\n\t\t\tpt[EXIST][t] = EXIST;\n\t\t}\n\n\t\tfor (int i = 0; i < N; i++){\n\t\t\tfor (int z = 0; z < 5; z++)\n\t\t\tfor (int x = 0; x < 5; x++)\n\t\t\tfor (int y = 0; y < 5; y++){\n\t\t\t\tint cnt = 0;\n\t\t\t\tfor (int dz = -1; dz <= 1; dz++)\n\t\t\t\tfor (int dx = -1; dx <= 1; dx++)\n\t\t\t\tfor (int dy = -1; dy <= 1; dy++){\n\t\t\t\t\tif (dx == 0 && dy == 0 && dz == 0)continue;\n\t\t\t\t\tint xx = x + dx;\n\t\t\t\t\tint yy = y + dy;\n\t\t\t\t\tint zz = z + dz;\n\t\t\t\t\tif (check(xx, yy, zz))\n\t\t\t\t\tcnt += (box[i][zz][xx][yy] == EXIST);\n\t\t\t\t}\n\n\t\t\t\tbox[i + 1][z][x][y] = pt[box[i][z][x][y]][cnt];\n\t\t\t}\n\t\t}\n\n\t\tcout << \"Case \" << t << \":\" << endl;\n\t\tt++;\n\t\tfor (int z = 0; z < 5; z++){\n\t\t\tfor (int x = 0; x < 5; x++)\n\t\t\t\tcout << box[N][z][x] << endl;\n\t\t\tif (z != 4)cout << endl;\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1412, "score_of_the_acc": -0.1223, "final_rank": 19 }, { "submission_id": "aoj_1510_1238127", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nint Case=0;\nchar box[7][7][7];\nchar tmp[7][7][7];\nint live[27];\nint death[27];\nvoid copy(){\n\tfor(int x=0;x<7;x++){\n\t\tfor(int y=0;y<7;y++){\n\t\t\tfor(int z=0;z<7;z++){\n\t\t\t\ttmp[x][y][z]=box[x][y][z];\n\t\t\t}\n\t\t}\n\t}\n}\nvoid reset(){\n\tfor(int x=0;x<7;x++){\n\t\tfor(int y=0;y<7;y++){\n\t\t\tfor(int z=0;z<7;z++){\n\t\t\t\tbox[x][y][z]='2';\n\t\t\t}\n\t\t}\n\t}\n}\nvoid scan(){\n\tif(Case>0)cout<<endl;\n\tcout<<\"Case \"<<++Case<<\":\\n\";\n\tfor(int z=1;z<=5;z++){\n\t\tfor(int y=1;y<=5;y++){\n\t\t\tfor(int x=1;x<=5;x++){\n\t\t\t\tcin>>box[x][y][z];\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<27;i++)live[i]=death[i]=0;\n\tint m,k;\n\tfor(cin>>m;m--;){\n\t\tcin>>k;\n\t\tlive[k]=1;\n\t}\n\tfor(cin>>m;m--;){\n\t\tcin>>k;\n\t\tdeath[k]=1;\n\t}\n}\n\nint cnt(int X,int Y,int Z){\n\tint res=0;\n\tfor(int x=X-1;x<=X+1;x++){\n\t\tfor(int y=Y-1;y<=Y+1;y++){\n\t\t\tfor(int z=Z-1;z<=Z+1;z++){\n\t\t\t\tif(x==X && y==Y && z==Z)continue;\n\t\t\t\tif(tmp[x][y][z]=='1')res++;\n\t\t\t}\n\t\t}\n\t}\n\treturn res;\n}\n\nvoid cal(){\n\tcopy();\n\tfor(int x=1;x<=5;x++){\n\t\tfor(int y=1;y<=5;y++){\n\t\t\tfor(int z=1;z<=5;z++){\n\t\t\t\tint c=cnt(x,y,z);\n\t\t\t\tif(tmp[x][y][z]=='0'){\n\t\t\t\t\tif(live[c])box[x][y][z]='1';\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tif(death[c]==0)box[x][y][z]='0';\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t}\n}\nvoid print(){\n\tfor(int z=1;z<=5;z++){\n\t\tif(z>1)cout<<endl;\n\t\tfor(int y=1;y<=5;y++){\n\t\t\tfor(int x=1;x<=5;x++){\n\t\t\t\tcout<<box[x][y][z];\n\t\t\t}\n\t\t\tcout<<endl;\n\t\t}\n\t}\n}\n\nint main(){\n\t\n\twhile(cin>>n,n){\n\t\treset();\n\t\tscan();\n\t\twhile(n--)cal();\n\t\tprint();\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1164, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1510_1172285", "code_snippet": "#include<bits/stdc++.h>\n \n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n \nusing namespace std;\n \nbool a[5][5][5][2];\nint M1,M2;\nint alpha[30], beta[30];\n \ninline bool isValid(int x,int y,int z) { return 0 <= x && x < 5 && 0 <= y && y < 5 && 0 <= z && z < 5; }\n \nbool ok(bool type,int v){\n if( !type ) rep(i,M1) if( alpha[i] == v ) return true;\n if( type ) rep(i,M2) if( beta[i] == v ) return true;\n return false;\n}\n \nvoid print(int type){\n rep(z,5) {\n if( z ) puts(\"\");\n rep(y,5) {\n rep(x,5) {\n cout << a[z][y][x][type];\n } puts(\"\");\n }\n }\n}\n \nint main(){\n int N,CNT=1;\n bool fu = 1;\n while( cin >> N, N ){\n if( !fu ) puts(\"\");\n fu = false;\n rep(z,5) rep(y,5) {\n string s;\n cin >> s;\n rep(x,5) a[z][y][x][0] = (s[x]=='0'?false:true);\n }\n \n //print(0);\n \n cin >> M1; rep(i,M1) cin >> alpha[i];\n cin >> M2; rep(i,M2) cin >> beta[i];\n printf(\"Case %d:\\n\",CNT++);\n rep(i,5) rep(j,5) rep(k,5) a[i][j][k][1] = false;\n \n int phase = 0; \n rep(day,N) { \n rep(z,5) rep(y,5) rep(x,5) {\n int cur = phase&1;\n int next = (phase+1)&1;\n bool type = a[z][y][x][cur];\n int cnt = 0;\n for(int dz=-1;dz<=1;dz++){\n for(int dy=-1;dy<=1;dy++){\n for(int dx=-1;dx<=1;dx++){\n if( dz == 0 && dy == 0 && dx == 0 ) continue;\n int nz = z + dz, ny = y + dy, nx = x + dx;\n if( !isValid(nx,ny,nz) ) continue;\n bool v = a[nz][ny][nx][cur];\n //cout << \"(\" << nx << \",\" << ny << \",\" << nz << \") \" << a[nz][ny][nx][cur] << \"!!\" << endl;\n if( v ) ++cnt;\n /*\n if( !type && v ) ++cnt;\n if( type && !v ) ++cnt;\n */\n } \n \n } \n }\n \n if( !type ) a[z][y][x][next] = ( ok(type,cnt) ) ? true : false;\n if( type ) a[z][y][x][next] = ( ok(type,cnt) ) ? true : false;\n \n }\n ++phase;\n }\n \n print(phase&1);\n \n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1236, "score_of_the_acc": -0.0355, "final_rank": 15 }, { "submission_id": "aoj_1510_1166450", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n\n#define ck(x,a,b) (a<=x&&x<b)\n\ninline bool inrange(int x, int y, int z) {\n return ck(x,0,5) && ck(y,0,5) && ck(z,0,5);\n}\n\nint main() {\n \n for(int N, Tcnt=1; cin >> N && N; Tcnt++) {\n vector<vector<vector<int> > > curr(5, vector<vector<int> >(5, vector<int>(5)));\n rep(z, 5) rep(y, 5) rep(x, 5) {\n char ch; cin >> ch;\n curr[z][y][x] = ch-'0';\n }\n \n int M1; cin >> M1; vector<int> a(M1);\n rep(i, M1) cin >> a[i];\n int M2; cin >> M2; vector<int> b(M2);\n rep(i, M2) cin >> b[i];\n \n rep(n, N) {\n vector<vector<vector<int> > > next(5, vector<vector<int> >(5, vector<int>(5)));\n rep(z, 5) rep(y, 5) rep(x, 5) {\n int cnt = 0;\n REP(dz,-1,2) REP(dy,-1,2) REP(dx,-1,2) {\n int nx = x+dx, ny = y+dy, nz = z+dz;\n if(dx == 0 && dy == 0 && dz == 0) { continue; }\n if(!inrange(nx, ny, nz)) { continue; }\n cnt += curr[nz][ny][nx];\n }\n \n if(!curr[z][y][x]) {\n bool ok = 0;\n rep(I, M1) if(a[I] == cnt) { ok = 1; break; }\n if(ok) { next[z][y][x] = 1; }\n }\n \n if(curr[z][y][x]) {\n bool ok = 0;\n rep(I, M2) if(b[I] == cnt) { ok = 1; break; }\n if(ok) { next[z][y][x] = 1; }\n }\n }\n curr = next;\n }\n \n if(Tcnt>1) cout << \"\\n\";\n cout << \"Case \" << Tcnt << \":\";\n rep(z, 5) {\n cout << endl;\n rep(y, 5) {\n rep(x, 5) cout << curr[z][y][x];\n cout << endl;\n }\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1224, "score_of_the_acc": -0.0296, "final_rank": 13 }, { "submission_id": "aoj_1510_1164773", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nbool field[5][5][5];\n\ninline bool inField(int x,int y,int z){\n if(0 > x || x >= 5) return false;\n if(0 > y || y >= 5) return false;\n if(0 > z || z >= 5) return false;\n return true;\n}\n\nint getNum(int x,int y,int z,bool f){\n int res = 0;\n for(int zz = z-1 ; zz <= z+1 ; zz++){\n for(int yy = y-1 ; yy <= y+1 ; yy++){\n for(int xx = x-1 ; xx <= x+1 ; xx++){\n if(xx == x && yy == y && zz == z) continue;\n if(!inField(xx,yy,zz)) continue;\n if(field[yy][xx][zz] == f){\n res++;\n }\n }\n }\n }\n return res;\n}\n\nint main(){\n int N,Tc = 0;\n bool space = false;\n while(cin >> N, N > 0){\n if(space) cout << endl;\n else space = true;\n for(int z = 0 ; z < 5 ; z++){\n string str;\n for(int y = 0 ; y < 5 ; y++){\n cin >> str;\n for(int x = 0 ; x < 5 ; x++){\n field[y][x][z] = str[x]-'0';\n }\n }\n }\n int M[2],a[27],b[27];\n cin >> M[0];\n for(int i = 0 ; i < M[0] ; i++){\n cin >> a[i];\n }\n cin >> M[1];\n for(int i = 0 ; i < M[1] ; i++){\n cin >> b[i];\n }\n while(N--){\n bool next[5][5][5];\n for(int z = 0 ; z < 5 ; z++){\n for(int y = 0 ; y < 5 ; y++){\n for(int x = 0 ; x < 5 ; x++){\n int num;\n bool f = field[y][x][z];\n if(f){\n num = getNum(x,y,z,true);\n bool ok = false;\n for(int i = 0 ; i < M[1] ; i++){\n if(num == b[i]){\n ok = true;\n break;\n }\n }\n if(!ok){\n f = !f;\n }\n }else{\n num = getNum(x,y,z,true);\n bool ok = false;\n for(int i = 0 ; i < M[0] ; i++){\n if(num == a[i]){\n ok = true;\n break;\n }\n }\n if(ok){\n f = !f;\n }\n }\n next[y][x][z] = f;\n }\n }\n }\n for(int z = 0 ; z < 5 ; z++){\n for(int y = 0 ; y < 5 ; y++){\n for(int x = 0 ; x < 5 ; x++){\n field[y][x][z] = next[y][x][z];\n }\n }\n }\n }\n cout << \"Case \" << ++Tc << \":\" << endl;\n for(int z = 0 ; z < 5 ; z++){\n for(int y = 0 ; y < 5 ; y++){\n for(int x = 0 ; x < 5 ; x++){\n cout << field[y][x][z];\n }\n cout << endl;\n }\n if(z != 4){\n cout << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1208, "score_of_the_acc": -0.0217, "final_rank": 3 }, { "submission_id": "aoj_1510_1164287", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nbool check(int x,int y, int z) {\n if(x<0 || x>=5) return false;\n if(y<0 || y>=5) return false;\n if(z<0 || z>=5) return false;\n return true;\n}\n \nint main() {\n int n,t=1;\n while(cin >> n && n) {\n int a[5][5][5];\n for(int i=0; i<5; i++) {\n for(int j=0; j<5; j++) {\n string s;\n cin >> s;\n for(int k=0; k<5; k++) {\n if(s[k]=='1') a[i][j][k]=1;\n else a[i][j][k]=0;\n }\n }\n }\n int m1,m2;\n set<int> s1,s2;\n cin >> m1;\n while(m1--) {\n int x;\n cin >> x;\n s1.insert(x);\n }\n cin >> m2;\n while(m2--) {\n int x;\n cin >> x;\n s2.insert(x);\n }\n while(n--) {\n int b[5][5][5];\n for(int i=0; i<5; i++) {\n for(int j=0; j<5; j++) {\n for(int k=0; k<5; k++) b[i][j][k]=a[i][j][k];\n }\n }\n for(int i=0; i<5; i++) {\n for(int j=0; j<5; j++) {\n for(int k=0; k<5; k++) {\n int cnt=0;\n for(int dx=-1; dx<=1; dx++) {\n for(int dy=-1; dy<=1; dy++) {\n for(int dz=-1; dz<=1; dz++) {\n int x=i+dx,y=j+dy,z=k+dz;\n if(x==i && y==j && z==k) continue;\n if(check(x,y,z)) cnt+=b[x][y][z];\n }\n }\n }\n if(b[i][j][k]) {\n if(!s2.count(cnt)) a[i][j][k]=0;\n } else {\n if(s1.count(cnt)) a[i][j][k]=1;\n }\n }\n }\n }\n }\n if(t>1) cout << endl;\n cout << \"Case \" << t++ << \":\" << endl;\n for(int i=0; i<5; i++) {\n if(i) cout << endl;\n for(int j=0; j<5; j++) {\n for(int k=0; k<5; k++) cout << a[i][j][k];\n cout << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1212, "score_of_the_acc": -0.0237, "final_rank": 5 }, { "submission_id": "aoj_1510_1164046", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n\nbool a[5][5][5][2];\nint M1,M2;\nint alpha[30], beta[30];\n\ninline bool isValid(int x,int y,int z) { return 0 <= x && x < 5 && 0 <= y && y < 5 && 0 <= z && z < 5; }\n\nbool ok(bool type,int v){\n if( !type ) rep(i,M1) if( alpha[i] == v ) return true;\n if( type ) rep(i,M2) if( beta[i] == v ) return true;\n return false;\n}\n\nvoid print(int type){\n rep(z,5) {\n if( z ) puts(\"\");\n rep(y,5) {\n rep(x,5) {\n\tcout << a[z][y][x][type];\n } puts(\"\");\n }\n }\n}\n\nint main(){\n int N,CNT=1;\n bool fu = 1;\n while( cin >> N, N ){\n if( !fu ) puts(\"\");\n fu = false;\n rep(z,5) rep(y,5) {\n string s;\n cin >> s;\n rep(x,5) a[z][y][x][0] = (s[x]=='0'?false:true);\n }\n\n //print(0);\n\n cin >> M1; rep(i,M1) cin >> alpha[i];\n cin >> M2; rep(i,M2) cin >> beta[i];\n printf(\"Case %d:\\n\",CNT++);\n rep(i,5) rep(j,5) rep(k,5) a[i][j][k][1] = false;\n\n int phase = 0; \n rep(day,N) { \n rep(z,5) rep(y,5) rep(x,5) {\n\tint cur = phase&1;\n\tint next = (phase+1)&1;\n\tbool type = a[z][y][x][cur];\n\tint cnt = 0;\n\tfor(int dz=-1;dz<=1;dz++){\n\t for(int dy=-1;dy<=1;dy++){\n\t for(int dx=-1;dx<=1;dx++){\n\t if( dz == 0 && dy == 0 && dx == 0 ) continue;\n\t int nz = z + dz, ny = y + dy, nx = x + dx;\n\t if( !isValid(nx,ny,nz) ) continue;\n\t bool v = a[nz][ny][nx][cur];\n\t //cout << \"(\" << nx << \",\" << ny << \",\" << nz << \") \" << a[nz][ny][nx][cur] << \"!!\" << endl;\n\t if( v ) ++cnt;\n\t /*\n\t if( !type && v ) ++cnt;\n\t if( type && !v ) ++cnt;\n\t */\n\t }\t\n\n\t }\t\n\t}\n\n\tif( !type ) a[z][y][x][next] = ( ok(type,cnt) ) ? true : false;\n\tif( type ) a[z][y][x][next] = ( ok(type,cnt) ) ? true : false;\n\n }\n ++phase;\n }\n\n print(phase&1);\n\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1240, "score_of_the_acc": -0.0375, "final_rank": 17 }, { "submission_id": "aoj_1510_1164030", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool check(int x,int y, int z) {\n if(x<0 || x>=5) return false;\n if(y<0 || y>=5) return false;\n if(z<0 || z>=5) return false;\n return true;\n}\n\nint main() {\n int n,t=1;\n while(cin >> n && n) {\n int a[5][5][5];\n for(int i=0; i<5; i++) {\n for(int j=0; j<5; j++) {\n\tstring s;\n\tcin >> s;\n\tfor(int k=0; k<5; k++) {\n\t if(s[k]=='1') a[i][j][k]=1;\n\t else a[i][j][k]=0;\n\t}\n }\n }\n int m1,m2;\n set<int> s1,s2;\n cin >> m1;\n while(m1--) {\n int x;\n cin >> x;\n s1.insert(x);\n }\n cin >> m2;\n while(m2--) {\n int x;\n cin >> x;\n s2.insert(x);\n }\n while(n--) {\n int b[5][5][5];\n for(int i=0; i<5; i++) {\n\tfor(int j=0; j<5; j++) {\n\t for(int k=0; k<5; k++) b[i][j][k]=a[i][j][k];\n\t}\n }\n for(int i=0; i<5; i++) {\n\tfor(int j=0; j<5; j++) {\n\t for(int k=0; k<5; k++) {\n\t int cnt=0;\n\t for(int dx=-1; dx<=1; dx++) {\n\t for(int dy=-1; dy<=1; dy++) {\n\t\tfor(int dz=-1; dz<=1; dz++) {\n\t\t int x=i+dx,y=j+dy,z=k+dz;\n\t\t if(x==i && y==j && z==k) continue;\n\t\t if(check(x,y,z)) cnt+=b[x][y][z];\n\t\t}\n\t }\n\t }\n\t if(b[i][j][k]) {\n\t if(!s2.count(cnt)) a[i][j][k]=0;\n\t } else {\n\t if(s1.count(cnt)) a[i][j][k]=1;\n\t }\n\t }\n\t}\n }\n }\n if(t>1) cout << endl;\n cout << \"Case \" << t++ << \":\" << endl;\n for(int i=0; i<5; i++) {\n if(i) cout << endl;\n for(int j=0; j<5; j++) {\n\tfor(int k=0; k<5; k++) cout << a[i][j][k];\n\tcout << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1212, "score_of_the_acc": -0.0237, "final_rank": 5 }, { "submission_id": "aoj_1510_1157942", "code_snippet": "#include <iostream>\n#include <complex>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <deque>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <vector>\n#include <set>\n#include <limits>\n#include <cstdio>\n#include <cctype>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <ctime>\nusing namespace std;\n#define REP(i, j) for(int i = 0; i < (int)(j); ++i)\n#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)\n#define SORT(v) sort((v).begin(), (v).end())\n#define REVERSE(v) reverse((v).begin(), (v).end())\ntypedef complex<double> P;\nconst int L = 5;\n\n\nint N, v[L][L][L], t[L][L][L], c = 1;\n\nint main() {\n while(cin >>N && N){\n if(c != 1) cout <<endl;\n REP(z, L){\n REP(y, L){\n string s; cin >>s;\n REP(x, L) v[z][y][x] = s[x] - '0';\n }\n }\n int MA; cin >>MA; vector<int> A(MA); REP(i, MA) cin >>A[i];\n int MB; cin >>MB; vector<int> B(MB); REP(i, MB) cin >>B[i];\n REP(n, N){\n REP(z, L){\n REP(y, L){\n REP(x, L){\n int cnt = 0;\n FOR(mz, -1, 2) FOR(my, -1, 2) FOR(mx, -1, 2) if(!(mz == 0 && my == 0 && mx == 0) && z + mz >= 0 && y + my >= 0 && x + mx >= 0 && z + mz < L && y + my < L && x + mx < L) cnt += v[z + mz][y + my][x + mx];\n if(v[z][y][x]){\n int f = 0;\n REP(i, MB) if(cnt == B[i]) f = 1;\n t[z][y][x] = f;\n } else{\n int f = 0;\n REP(i, MA) if(cnt == A[i]) f = 1;\n t[z][y][x] = f;\n }\n }\n }\n }\n REP(i, L) REP(j, L) REP(k, L) v[i][j][k] = t[i][j][k];\n }\n cout <<\"Case \" <<c++ <<\":\" <<endl;\n REP(z, L){\n REP(y, L){\n REP(x, L) cout <<v[z][y][x];\n cout <<endl;\n }\n if(z + 1 != L) cout <<endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1212, "score_of_the_acc": -0.0237, "final_rank": 5 }, { "submission_id": "aoj_1510_1143871", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\nbool valid(int x) {\n return 0 <= x && x < 5;\n}\n\nint main(){\n int N;\n int C = 0;\n while(cin >> N && N > 0) {\n if(C != 0) cout << endl;\n cout << \"Case \" << ++C << \":\" << endl;\n int grid[5][5][5] = {};\n\n REP(z, 5) REP(y, 5) {\n string s;\n cin >> s;\n REP(x, 5) grid[z][y][x] = s[x] == '1';\n }\n\n vector<int> A;\n int M; cin >> M;\n A.resize(M);\n REP(i, M) { cin >> A[i]; }\n\n vector<int> B;\n cin >> M;\n B.resize(M);\n REP(i, M) { cin >> B[i]; }\n\n REP(_, N) {\n int cnt[5][5][5] = {};\n REP(sz, 5) REP(sy, 5) REP(sx, 5) {\n REP(dz, 3) REP(dy, 3) REP(dx, 3) {\n int x = sx + dx - 1;\n int y = sy + dy - 1;\n int z = sz + dz - 1;\n if(dx == 1 && dy == 1 && dz == 1) continue;\n if(valid(x) && valid(y) && valid(z)) {\n cnt[sz][sy][sx] += grid[z][y][x];\n }\n }\n }\n\n REP(z, 5) REP(y, 5) REP(x, 5) {\n if(grid[z][y][x] == 0) {\n int next = 0;\n for(int a : A) if(cnt[z][y][x] == a) next = 1;\n grid[z][y][x] = next;\n } else {\n int next = 0;\n for(int b : B) if(cnt[z][y][x] == b) next = 1;\n grid[z][y][x] = next;\n }\n }\n }\n\n REP(z, 5) {\n REP(y, 5){\n REP(x, 5) cout << grid[z][y][x];\n cout << endl;\n }\n if(z != 4) cout << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1212, "score_of_the_acc": -0.0237, "final_rank": 5 }, { "submission_id": "aoj_1510_1088391", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n\nusing namespace std;\n\nclass range {\nprivate:\n struct Iterator {\n int val;\n int operator*() {return val;}\n bool operator!=(Iterator& itr) {return val < itr.val;}\n void operator++() {++val;}\n };\n Iterator i, n;\npublic:\n range(int n) : i({0}), n({n}) {}\n range(int i, int n) : i({i}), n({n}) {}\n Iterator& begin() {return i;}\n Iterator& end() {return n;}\n};\n\ntemplate<class T> inline T at(const vector<T> &v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];}\n\ntemplate<class T> inline bool is_max(T &a, const T &b) {return a < b ? a = b, true : false;}\ntemplate<class T> inline bool is_min(T &a, const T &b) {return a > b ? a = b, true : false;}\n\nint main() {\n int t = 0;\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n if (t > 0) cout << endl;\n vector<vector<string>> box(5, vector<string>(5));\n for (int i : range(5)) for (int j : range(5)) cin >> box[i][j];\n int m1, m2;\n cin >> m1;\n set<int> a;\n for (int _ : range(m1)) {\n int aa;\n cin >> aa;\n a.insert(aa);\n }\n cin >> m2;\n set<int> b;\n for (int _ : range(m2)) {\n int bb;\n cin >> bb;\n b.insert(bb + 1);\n }\n for (int _ : range(n)) {\n vector<vector<string>> next = box;\n for (int i : range(5)) for (int j : range(5)) for (int k : range(5)) {\n if (box[i][j][k] == '0') {\n int cnt = 0;\n for (int ii : range(-1, 2)) for (int jj : range(-1, 2)) for (int kk : range(-1, 2)) {\n if (i + ii < 0 || 5 <= i + ii) continue;\n if (j + jj < 0 || 5 <= j + jj) continue;\n if (k + kk < 0 || 5 <= k + kk) continue;\n if (box[i + ii][j + jj][k + kk] == '1') ++cnt;\n }\n if (a.count(cnt)) next[i][j][k] = '1';\n } else {\n int cnt = 0;\n for (int ii : range(-1, 2)) for (int jj : range(-1, 2)) for (int kk : range(-1, 2)) {\n if (i + ii < 0 || 5 <= i + ii) continue;\n if (j + jj < 0 || 5 <= j + jj) continue;\n if (k + kk < 0 || 5 <= k + kk) continue;\n if (box[i + ii][j + jj][k + kk] == '1') ++cnt;\n }\n if (!b.count(cnt)) next[i][j][k] = '0';\n }\n }\n box = next;\n }\n cout << \"Case \" << ++t << \":\" << endl;\n for (int i : range(5)) {\n for (int j : range(5)) cout << box[i][j] << endl;\n if (i != 4) cout << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1224, "score_of_the_acc": -0.0296, "final_rank": 13 } ]
aoj_1511_cpp
Problem C: General of Taiko Problem とあるゲームセンターには、曲に合わせて流れてくる譜面通りに和太鼓を叩くゲームがあります。 譜面は長さ L のセルからなり、各セルには何もない、またはノートと呼ばれるプレイヤーがとるべき行動を表した記号があります。 ノートは2種類ありそれぞれ、和太鼓の面を叩く「トン」、和太鼓の縁を叩く「コツ」があります。 これらのノートに合わせて和太鼓を叩くと得点を得ることができます。 この得点の合計が10000点以上であればクリアとなります。 このとき、プレイヤーが曲をクリアできる確率を求めなさい。ただし、プレイヤーは常に最適な行動をとることにします。 プレイヤーが譜面通りに和太鼓を叩く精度は11段階あり、それぞれ、 0%, 10%, 20%, ..., 90%, 100%の確率で譜面通りに和太鼓を叩くことができます。 上記の二種類の動作は右腕と左腕どちらででも行うことができます。 プレイヤーの情報として、それぞれの腕で行った場合の精度の安定率を表す値が16種類、下記のように与えられます。 lt _ lt lt _ rt lt _ lk lt _ rk rt _ lt rt _ rt rt _ lk rt _ rk lk _ lt lk _ rt lk _ lk lk _ rk rk _ lt rk _ rt rk _ lk rk _ rk ただし、 lt は左腕でトン, rt は右腕でトン, lk は左腕でコツ, rk は右腕でコツの動作を表します。 例えば、 lt _ rk は左腕でトンのあとに右腕でコツを行った時の精度の安定の度合いを表し、この値によって左腕でトンのあとに右腕でコツを行うときの精度が、下記のように変化します。 プレイヤーの精度 = max(0, (精度の安定率 - 10) * 10 + 一つ前の精度) (%) 曲の情報は以下のとおりです。 曲の長さを表す L 、譜面を表す L 個の数字 s i (0 ≤ i < L )で示される。譜面の先頭は s 0 である。 s i の値は以下の3つです。 0 ... ノートなし 1 ... トン 2 ... コツ プレイヤーが最初に和太鼓を叩くときの精度は100%です。 また、プレイヤーは譜面を無視することができます。 ノートがなかったり、ノートを無視した場合、プレイヤーの精度は100%になります。 各ノートに合わせて和太鼓を叩いたときの得点は下記のようになります。 得点 = A + B * min(コンボ数, 10) この問題におけるコンボ数とは、連続してノートに合わせて和太鼓を叩けた数です。 プレイヤーがノートに合わせて叩いた場合、上記の式を元に得点が入り、その後にコンボ数が1増えます。 ノートに合わせて和太鼓を叩けなかった場合、コンボが途切れ、コンボ数が0になります。 Input 入力は複数のデータセットからなります。 各データセットは以下のとおりです。 lt _ lt lt _ rt lt _ lk lt _ rk rt _ lt rt _ rt rt _ lk rt _ rk lk _ lt lk _ rt lk _ lk lk _ rk rk _ lt rk _ rt rk _ lk rk _ rk L s 0 s 1 s 2 … s L - 1 A B 入力の終わりは負の整数4つからなります。 Constraints 入力は以下の条件を満たします。 0 < L ≤ 100 0 ≤ 精度の安定率 ≤ 10 精度の安定率は整数 0 < A , B ≤ 10000 A と B はともに100の倍数 データセットの数は100個以下 Output 各入力に対して、クリアできる確率を1行で出力しなさい。 ただし、出力は0.001以下の誤差を含んでも良いです。 Sample Input 9 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 5 1 1 1 1 1 1000 500 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 5 1 0 2 0 1 1000 2000 3 8 6 10 0 1 6 8 10 2 4 7 8 6 6 8 19 2 2 0 2 2 0 2 1 0 1 2 0 1 2 0 1 0 2 2 200 100 -1 -1 -1 -1 Sample Output 0.3024000000 0.0000000000 0.5120000000
[ { "submission_id": "aoj_1511_9613108", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cassert>\n#include <random>\n#include <iomanip>\n#pragma GCC optimize(\"O3\")\nusing namespace std;\n\n#define ll long long\n// #define pii pair<ll, int>\n// #define pq priority_queue <pii, vector<pii>, greater<pii>>\n\n// std::mt19937 rand(129365);\n\n//lt_lt 0 lt_Rt 1 lt_lK 2 lt_rk 3\n//Rt_lt 4 rt_rt 5 rt_lk 6 rt_rk 7\n//lK_lt 8 lk_rt 9 lk_lk 10 lk_rk 11\n//rk_lt 12 rk_rt 13 rk_lk 14 rk_rk 15\n\nint vals[16];\nint n, a, b;\nint s[100];\n\ndouble prob_l(int i, int acc, int combo, int score);\ndouble prob_r(int i, int acc, int combo, int score);\ndouble prob_skip(int i, int acc, int combo, int score);\n\ndouble dpl[101][11][11][101];\ndouble dpr[101][11][11][101];\ndouble dps[101][11][11][101];\n\ndouble prob_l(int i, int acc, int combo, int score){\n if(score <= 0) return 1.0;\n else if(i == n) return 0.0;\n if(combo > 10) combo = 10;\n if(dpl[i][acc][combo][score] >= 0) return dpl[i][acc][combo][score] ;\n assert(s[i-1] != 0);\n if(s[i] == 0){\n dpl[i][acc][combo][score] = prob_skip(i+1, 10, combo, score);\n // cout << \"l \" << i << ' ' << acc << ' ' << combo << ' ' << score << ' ' << dpl[i][acc][combo][score] << endl;\n return dpl[i][acc][combo][score];\n }\n int d = a + b * combo;\n int mask;\n if(s[i-1] == 1) mask = 0; //ton left\n else mask = 8; //kotsu left\n if(s[i] == 1) mask |= 0;//ton\n else mask |= 2;//kotsu\n //try l\n int lacc = max(0, vals[mask] - 10 + acc);\n double p_next_l_hit = prob_l(i+1, lacc, combo + 1, score - d);\n double p_next_l_miss = prob_l(i+1, lacc, 0, score);\n double p_next_l = p_next_l_hit * lacc / 10 + p_next_l_miss * (10 - lacc) / 10;\n //try r\n int racc = max(0, vals[mask | 1] - 10 + acc);\n double p_next_r_hit = prob_r(i+1, racc, combo + 1, score - d);\n double p_next_r_miss = prob_r(i+1, racc, 0, score);\n double p_next_r = p_next_r_hit * racc / 10 + p_next_r_miss * (10 - racc) / 10;\n //try skip\n double p_next_skip = prob_skip(i+1, 10, 0, score);\n\n dpl[i][acc][combo][score] = max(max(p_next_l, p_next_r), p_next_skip);\n // cout << \"l \" << i << ' ' << acc << ' ' << combo << ' ' << score << ' ' << dpl[i][acc][combo][score] << endl;\n return dpl[i][acc][combo][score];\n}\ndouble prob_r(int i, int acc, int combo, int score){\n if(score <= 0) return 1.0;\n else if(i == n) return 0.0;\n if(combo > 10) combo = 10;\n if(dpr[i][acc][combo][score] >= 0) return dpr[i][acc][combo][score] ;\n assert(s[i-1] != 0);\n if(s[i] == 0){\n dpr[i][acc][combo][score] = prob_skip(i+1, 10, combo, score);\n // cout << \"r \" << i << ' ' << acc << ' ' << combo << ' ' << score << ' ' << dpr[i][acc][combo][score] << endl;\n return dpr[i][acc][combo][score];\n }\n int d = a + b * combo;\n\n int mask;\n if(s[i-1] == 1) mask = 4; //ton right\n else mask = 12; //kotsu right\n if(s[i] == 1) mask |= 0;//ton\n else mask |= 2;//kotsu\n //try l\n int lacc = max(0, vals[mask] - 10 + acc);\n double p_next_l_hit = prob_l(i+1, lacc, combo + 1, score - d);\n double p_next_l_miss = prob_l(i+1, lacc, 0, score);\n double p_next_l = p_next_l_hit * lacc / 10 + p_next_l_miss * (10 - lacc) / 10;\n //try r\n int racc = max(0, vals[mask | 1] - 10 + acc);\n double p_next_r_hit = prob_r(i+1, racc, combo + 1, score - d);\n double p_next_r_miss = prob_r(i+1, racc, 0, score);\n double p_next_r = p_next_r_hit * racc / 10 + p_next_r_miss * (10 - racc) / 10;\n //try skip\n double p_next_skip = prob_skip(i+1, 10, 0, score);\n\n dpr[i][acc][combo][score] = max(max(p_next_l, p_next_r), p_next_skip);\n // cout << \"r \" << i << ' ' << acc << ' ' << combo << ' ' << score << ' ' << dpr[i][acc][combo][score] << endl;\n return dpr[i][acc][combo][score];\n}\ndouble prob_skip(int i, int acc, int combo, int score){\n if(score <= 0) return 1.0;\n else if(i == n) return 0.0;\n if(combo > 10) combo = 10;\n if(dps[i][acc][combo][score] >= 0) return dps[i][acc][combo][score] ;\n int d = a + b * combo;\n if(s[i] == 0){\n dps[i][acc][combo][score] = prob_skip(i+1, 10, combo, score);\n // cout << \"s \" << i << ' ' << acc << ' ' << combo << ' ' << score << ' ' << dps[i][acc][combo][score] << endl;\n return dps[i][acc][combo][score];\n }\n else{\n double p_next_l = prob_l(i+1, 10, combo + 1, score - d);\n double p_next_r = prob_r(i+1, 10, combo + 1, score - d);\n double p_next_skip = prob_skip(i+1, 10, 0, score);\n\n dps[i][acc][combo][score] = max(max(p_next_l, p_next_r), p_next_skip);\n // cout << \"s \" << i << ' ' << acc << ' ' << combo << ' ' << score << ' ' << dps[i][acc][combo][score] << endl;\n return dps[i][acc][combo][score];\n }\n}\n\nint main(){\n while(true){\n for(int i = 0; i < 16; i++){\n cin >> vals[i];\n if(i == 3 and vals[0] < 0){\n return 0;\n }\n }\n cin >> n;\n for(int i = 0; i < n; i++){\n cin >> s[i];\n }\n for(int i = 0; i <= n; i++){\n for(int j = 0; j <= 10; j++){\n for(int k = 0; k <= 10; k++){\n for(int s = 0; s <= 100; s++){\n dpl[i][j][k][s] = \n dpr[i][j][k][s] = \n dps[i][j][k][s] = -1;\n }\n }\n }\n }\n cin >> a >> b;\n a /= 100, b /= 100;\n if(s[0] == 0){\n cout << setprecision(10) << prob_skip(1, 10, 0, 100) << endl;\n }\n else{\n double sl = prob_l(1, 10, 1, 100 - a);\n double sr = prob_r(1, 10, 1, 100 - a);\n double ss = prob_skip(1, 10, 0, 100);\n cout << setprecision(10) << max(max(sl, sr), ss) << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 32532, "score_of_the_acc": -0.5935, "final_rank": 1 }, { "submission_id": "aoj_1511_8293322", "code_snippet": "#include <iostream>\nusing namespace std;\n\n// 問題文に曖昧なところがあって良くわからない.\nint Accuracy[5][5];\nint L, S[109];\nint A;\nint B;\ndouble dp[109][109][12][12][5]; // (pos, 得点, コンボ, 精度, 前の腕)\n\ndouble solve() {\n\t// DP Init\n\tfor (int i = 0; i <= L + 1; i++) {\n\t\tfor (int j = 0; j <= 100; j++) {\n\t\t\tfor (int k = 0; k <= 10; k++) {\n\t\t\t\tfor (int l = 0; l <= 10; l++) {\n\t\t\t\t\tfor (int m = 0; m <= 4; m++) dp[i][j][k][l][m] = 0.0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <= 10; i++) {\n\t\tfor (int j = 0; j <= 10; j++) dp[L + 1][100][i][j][4] = 1.0;\n\t}\n\n\t// Dynamic Programming\n\tfor (int i = L; i >= 0; i--) {\n\t\tfor (int j = 0; j <= 100; j++) {\n\t\t\tfor (int k = 0; k <= 10; k++) {\n\t\t\t\tfor (int l = 0; l <= 10; l++) {\n\t\t\t\t\tfor (int m = 0; m <= 4; m++) {\n\t\t\t\t\t\tfor (int nex = 0; nex <= 4; nex++) {\n\t\t\t\t\t\t\tif (S[i] != 1 && (nex == 0 || nex == 1)) continue;\n\t\t\t\t\t\t\tif (S[i] != 2 && (nex == 2 || nex == 3)) continue;\n\t\t\t\t\t\t\tdouble score = 0;\n\n\t\t\t\t\t\t\t// 成功する場合\n\t\t\t\t\t\t\tif (true) {\n\t\t\t\t\t\t\t\tint nex_pos = i + 1;\n\t\t\t\t\t\t\t\tint nex_score = j;\n\t\t\t\t\t\t\t\tint nex_combo = k;\n\t\t\t\t\t\t\t\tint nex_seido = max(0, l + Accuracy[m][nex] - 10);\n\t\t\t\t\t\t\t\tint nex_state = nex;\n\t\t\t\t\t\t\t\tif (S[i] == 1 && (nex == 0 || nex == 1)) { nex_score = min(100, nex_score + A + B * k); nex_combo = min(10, nex_combo + 1); }\n\t\t\t\t\t\t\t\tif (S[i] == 2 && (nex == 2 || nex == 3)) { nex_score = min(100, nex_score + A + B * k); nex_combo = min(10, nex_combo + 1); }\n\t\t\t\t\t\t\t\tif (nex == 4) { nex_seido = 10; }\n\t\t\t\t\t\t\t\tif (S[i] == 1 && nex == 4) { nex_combo = 0; }\n\t\t\t\t\t\t\t\tif (S[i] == 2 && nex == 4) { nex_combo = 0; }\n\t\t\t\t\t\t\t\tscore += 0.1 * nex_seido * dp[nex_pos][nex_score][nex_combo][nex_seido][nex_state];\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// 失敗する場合\n\t\t\t\t\t\t\tif (nex != 4) {\n\t\t\t\t\t\t\t\tint nex_pos = i + 1;\n\t\t\t\t\t\t\t\tint nex_score = j;\n\t\t\t\t\t\t\t\tint nex_combo = 0;\n\t\t\t\t\t\t\t\tint nex_seido = max(0, l + Accuracy[m][nex] - 10);\n\t\t\t\t\t\t\t\tint nex_state = nex;\n\t\t\t\t\t\t\t\tscore += 0.1 * (10 - nex_seido) * dp[nex_pos][nex_score][nex_combo][nex_seido][nex_state];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tdp[i][j][k][l][m] = max(dp[i][j][k][l][m], score);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Return\n\treturn dp[0][0][0][10][4];\n}\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) Accuracy[i][j] = 10;\n\t\t}\n\t\tcin >> Accuracy[0][0] >> Accuracy[0][1] >> Accuracy[0][2] >> Accuracy[0][3]; if (Accuracy[0][0] == -1) break;\n\t\tcin >> Accuracy[1][0] >> Accuracy[1][1] >> Accuracy[1][2] >> Accuracy[1][3];\n\t\tcin >> Accuracy[2][0] >> Accuracy[2][1] >> Accuracy[2][2] >> Accuracy[2][3];\n\t\tcin >> Accuracy[3][0] >> Accuracy[3][1] >> Accuracy[3][2] >> Accuracy[3][3];\n\t\tcin >> L;\n\t\tfor (int i = 0; i < L; i++) cin >> S[i];\n\t\tcin >> A >> B;\n\t\tA /= 100; B /= 100;\n\n\t\t// Step 2. Dynamic Programming\n\t\tdouble Answer = solve();\n\t\tprintf(\"%.12lf\\n\", Answer);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6700, "memory_kb": 66900, "score_of_the_acc": -1.9721, "final_rank": 12 }, { "submission_id": "aoj_1511_8293313", "code_snippet": "#include <iostream>\nusing namespace std;\n\n// 問題文に曖昧なところがあって良くわからない.\nint Accuracy[5][5];\nint L, S[109];\nint A;\nint B;\ndouble dp[109][109][12][12][5]; // (pos, 得点, コンボ, 精度, 前の腕)\n\ndouble solve() {\n\t// DP Init\n\tfor (int i = 0; i <= L + 1; i++) {\n\t\tfor (int j = 0; j <= 100; j++) {\n\t\t\tfor (int k = 0; k <= 10; k++) {\n\t\t\t\tfor (int l = 0; l <= 10; l++) {\n\t\t\t\t\tfor (int m = 0; m <= 4; m++) dp[i][j][k][l][m] = 0.0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <= 10; i++) {\n\t\tfor (int j = 0; j <= 10; j++) dp[L + 1][100][i][j][4] = 1.0;\n\t}\n\n\t// Dynamic Programming\n\tfor (int i = L; i >= 0; i--) {\n\t\tfor (int j = 0; j <= 100; j++) {\n\t\t\tfor (int k = 0; k <= 10; k++) {\n\t\t\t\tfor (int l = 0; l <= 10; l++) {\n\t\t\t\t\tfor (int m = 0; m <= 4; m++) {\n\t\t\t\t\t\tfor (int nex = 0; nex <= 4; nex++) {\n\t\t\t\t\t\t\tif (S[i] != 1 && (nex == 0 || nex == 1)) continue;\n\t\t\t\t\t\t\tif (S[i] != 2 && (nex == 2 || nex == 3)) continue;\n\n\t\t\t\t\t\t\t// 成功する場合\n\t\t\t\t\t\t\tif (true) {\n\t\t\t\t\t\t\t\tint nex_pos = i + 1;\n\t\t\t\t\t\t\t\tint nex_score = j;\n\t\t\t\t\t\t\t\tint nex_combo = k;\n\t\t\t\t\t\t\t\tint nex_seido = max(0, l + Accuracy[m][nex] - 10);\n\t\t\t\t\t\t\t\tint nex_state = nex;\n\t\t\t\t\t\t\t\tif (S[i] == 1 && (nex == 0 || nex == 1)) { nex_score = min(100, nex_score + A + B * k); nex_combo = min(10, nex_combo + 1); }\n\t\t\t\t\t\t\t\tif (S[i] == 2 && (nex == 2 || nex == 3)) { nex_score = min(100, nex_score + A + B * k); nex_combo = min(10, nex_combo + 1); }\n\t\t\t\t\t\t\t\tif (nex == 4) { nex_seido = 10; }\n\t\t\t\t\t\t\t\tif (S[i] == 1 && nex == 4) { nex_combo = 0; }\n\t\t\t\t\t\t\t\tif (S[i] == 2 && nex == 4) { nex_combo = 0; }\n\t\t\t\t\t\t\t\tdp[i][j][k][l][m] = max(dp[i][j][k][l][m], 0.1 * nex_seido * dp[nex_pos][nex_score][nex_combo][nex_seido][nex_state]);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// 失敗する場合\n\t\t\t\t\t\t\tif (nex != 4) {\n\t\t\t\t\t\t\t\tint nex_pos = i + 1;\n\t\t\t\t\t\t\t\tint nex_score = j;\n\t\t\t\t\t\t\t\tint nex_combo = 0;\n\t\t\t\t\t\t\t\tint nex_seido = max(0, l + Accuracy[m][nex] - 10);\n\t\t\t\t\t\t\t\tint nex_state = nex;\n\t\t\t\t\t\t\t\tdp[i][j][k][l][m] = max(dp[i][j][k][l][m], 0.1 * (10 - nex_seido) * dp[nex_pos][nex_score][nex_combo][nex_seido][nex_state]);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Return\n\treturn dp[0][0][0][10][4];\n}\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) Accuracy[i][j] = 10;\n\t\t}\n\t\tcin >> Accuracy[0][0] >> Accuracy[0][1] >> Accuracy[0][2] >> Accuracy[0][3]; if (Accuracy[0][0] == -1) break;\n\t\tcin >> Accuracy[1][0] >> Accuracy[1][1] >> Accuracy[1][2] >> Accuracy[1][3];\n\t\tcin >> Accuracy[2][0] >> Accuracy[2][1] >> Accuracy[2][2] >> Accuracy[2][3];\n\t\tcin >> Accuracy[3][0] >> Accuracy[3][1] >> Accuracy[3][2] >> Accuracy[3][3];\n\t\tcin >> L;\n\t\tfor (int i = 0; i < L; i++) cin >> S[i];\n\t\tcin >> A >> B;\n\t\tA /= 100; B /= 100;\n\n\t\t// Step 2. Dynamic Programming\n\t\tdouble Answer = solve();\n\t\tprintf(\"%.12lf\\n\", Answer);\n\t}\n\treturn 0;\n}", "accuracy": 0.2857142857142857, "time_ms": 720, "memory_kb": 66736, "score_of_the_acc": -1.0928, "final_rank": 18 }, { "submission_id": "aoj_1511_2138119", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = double;\nconst ld eps = 1e-9;\nconst int COMB_MAX = 11;\nint per[5][5];\nld dp[101][COMB_MAX + 1][101][5][11];\n\nint A, B;\nld solve(vector<int>&nodes, int now, int combo,int score, int preuse,int preacc) {\n\tif (dp[now][combo][score][preuse][preacc] > -eps) {\n\t\treturn dp[now][combo][score][preuse][preacc];\n\t}else if (now == nodes.size()) {\n\t\tif (score == 100) {\n\t\t\tint a;\n\t\t\ta = 1;\n\t\t\ta++;\n\t\t}\n\t\tdp[now][combo][score][preuse][preacc] = score >= 100;\n\t}\n\telse {\n\n\t\tld ans = 0;\n\t\tif (nodes[now] == 0) {\n\t\t\tdp[now][combo][score][preuse][preacc] = solve(nodes, now + 1, combo, score, 4, 10);\n\t\t}\n\t\telse if (score >= 100) {\n\t\t\tdp[now][combo][score][preuse][preacc] = 1.0l;\n\t\t}\n\t\telse {\n\t\t\tfor (int nextuse = 0; nextuse < 5; ++nextuse) {\n\t\t\t\tint nextscore(score);\n\t\t\t\tint nextcombo(combo);\n\t\t\t\tint kot = nextuse == 4 ? 0 : nextuse % 2+1;\n\t\t\t\tif (kot&&nodes[now] == kot) {\n\t\t\t\t\tnextcombo++;\n\t\t\t\t\tnextscore += A + B*min(combo, 10);\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tnextcombo = 0;\n\t\t\t\t}\n\t\t\t\tif (nextcombo > COMB_MAX) {\n\t\t\t\t\tnextcombo = min(nextcombo, COMB_MAX);\n\t\t\t\t}\n\t\t\t\tnextscore = min(nextscore, 100);\n\t\t\t\tint nextacc = nextuse == 4 ? 10 : max(0, ((per[preuse][nextuse] - 10) * 10 + preacc*10) / 10);\n\t\t\t\tassert(nextacc <= 10);\n\t\t\t\tld nans = 0;\n\t\t\t\t{\n\t\t\t\t\tif (nextacc) {\n\t\t\t\t\t\tnans += (ld(nextacc) / 10)*solve(nodes, now + 1, nextcombo, nextscore, nextuse, nextacc);\n\t\t\t\t\t}\n\t\t\t\t\tnans += (1.0l-ld(nextacc) / 10)*solve(nodes, now + 1, 0, score, nextuse, nextacc);\n\t\t\t\t}\n\t\t\t\tans = max(ans, nans);\n\t\t\t}\n\t\t\tdp[now][combo][score][preuse][preacc] = ans;\n\t\t}\n\t\t\n\t}\n\treturn dp[now][combo][score][preuse][preacc];\n\n}\n\nint main() {\n\tcout << setprecision(10) << fixed;\n\twhile (1) {\n\t\tfor (int i = 0; i < 101; i++) {\n\t\t\tfor (int j = 0; j <=COMB_MAX ; ++j) {\n\t\t\t\tfor (int k = 0; k < 101; ++k) {\n\t\t\t\t\tfor (int l = 0; l < 5; ++l) {\n\t\t\t\t\t\tfor (int m = 0; m < 11; ++m) {\n\t\t\t\t\t\t\tdp[i][j][k][l][m] = -1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 2; ++i) {\n\t\t\tfor (int j = 0; j < 2; ++j) {\n\t\t\t\tint from = j * 2 + i;\n\t\t\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\t\t\tint to = l * 2 + k;\n\t\t\t\t\t\tcin >> per[from][to];\n\t\t\t\t\t\tif (per[from][to] < -0.5)return 0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 5; ++i) {\n\t\t\tper[i][4] = 10;\n\t\t\tper[4][i] = 10;\n\t\t}\n\t\tint L; cin >> L;\n\t\tvector<int>nodes(L);\n\t\tfor (int i = 0; i < L; ++i) {\n\t\t\tcin >> nodes[i];\n\t\t} cin >> A >> B;\n\t\tA /= 100;\n\t\tB /= 100;\n\t\tld ans = solve(nodes, 0, 0, 0, 4, 10);\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4070, "memory_kb": 55904, "score_of_the_acc": -1.4166, "final_rank": 7 }, { "submission_id": "aoj_1511_1144052", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\nconst int MAXA = 100;\nconst int MAXP = 10000;\nint L;\n//bool used[105][5][11][101][11];\ndouble memo[101][5][11][101][11];\ninline double get(int idx, int last, int ac, int point, int cmb) {\n if(point >= 100) point = 100;\n if(cmb >= 11) cmb = 10;\n if(idx == L) return (point == 100 ? 1.0 : 0.0);\n // assert(point % 100 == 0);\n //point /= 100;\n // assert(ac % 10 == 0);\n //ac /= 10;\n // assert(idx >= 0 && idx < 101);\n // assert(last >= 0 && last < 5);\n // assert(ac >= 0 && ac < 11);\n // assert(point >= 0 && point < 101);\n // assert(cmb >= 0 && cmb < 11);\n //assert(used[idx][last][ac][point][cmb]);\n return memo[idx][last][ac][point][cmb];\n}\n\ninline void Set(int idx, int last, int ac, int point, int cmb, double val) {\n //used[idx][last][ac][point][cmb] = true;\n //assert(point % 100 == 0);\n //point /= 100;\n //assert(ac % 10 == 0);\n //ac /= 10;\n // assert(idx >= 0 && idx < 101);\n // assert(last >= 0 && last < 5);\n // assert(ac >= 0 && ac < 11);\n // assert(point >= 0 && point < 101);\n // assert(cmb >= 0 && cmb < 11);\n memo[idx][last][ac][point][cmb] = val;\n}\n\nint main(){\n while(true) {\n //memset(used, 0, sizeof(used));\n\n int D[5][5];\n REP(y, 4) REP(x, 4) cin >> D[y][x];\n if(D[0][0] < 0) break;\n\n cin >> L;\n\n vector<int> s(L);\n REP(i, L) cin >> s[i];\n\n int A, B;\n cin >> A >> B;\n A /= 100;\n B /= 100;\n double probA[11] = {};\n double probB[11] = {};\n REP(i, 11) probA[i] = i / 10.0;\n REP(i, 11) probB[i] = (10.0-i) / 10.0;\n\n for(int i = L - 1; i >= 0; i--) {\n for(int last = 0; last < 5; last++) {\n for(int ac = 0; ac <= 10; ac ++) {\n for(int point = 0; point <= 100; point ++) {\n for(int cmb = 0; cmb <= 10; cmb++) {\n double res = 0.0;\n if(s[i] == 1) for(int act = 0; act < 2; act++) {\n int nac = 10;\n if(last != 4) {\n nac = max(0, (D[last][act] - 10) + ac);\n }\n\n int ncmb = cmb + 1;\n int npoint = point + A + B * cmb;\n // if(i == 0 && last == 4 && ac == 100 && point == 0 && cmb == 0) {\n // printf(\"i = %d last = %d ac = %d point = %d cmb = %d act = %d res = %f\\n\",\n // i+1, act, nac, npoint, ncmb, act, (double)ac / 100.0 * get(i+1, act, nac, npoint, ncmb)\n // );\n // }\n double cand = \n probA[nac] * get(i+1, act, nac, npoint, ncmb) +\n probB[nac] * get(i+1, act, nac, point, 0);\n if(res < cand) {\n res = cand;\n }\n\n }\n if(s[i] == 2) for(int act = 2; act < 4; act++) {\n int nac = 10;\n if(last != 4) {\n nac = max(0, (D[last][act] - 10) + ac);\n }\n\n int ncmb = cmb + 1;\n int npoint = point + A + B * cmb;\n // if(i == 0 && last == 4 && ac == 100 && point == 0 && cmb == 0) {\n // printf(\"i = %d last = %d ac = %d point = %d cmb = %d act = %d res = %f\\n\",\n // i+1, act, nac, npoint, ncmb, act, (double)ac / 100.0 * get(i+1, act, nac, npoint, ncmb)\n // );\n // }\n double cand = \n probA[nac] * get(i+1, act, nac, npoint, ncmb) +\n probB[nac] * get(i+1, act, nac, point, 0);\n if(res < cand) {\n res = cand;\n }\n\n }\n {\n int nac = 10;\n int ncmb = (s[i] == 0 ? cmb : 0);\n int npoint = point;\n double cand = get(i+1, 4, nac, npoint, ncmb);\n if(res < cand) {\n res = cand;\n }\n }\n // if(i == 0 && last == 4 && ac == 100 && point == 0 && cmb == 0) {\n // printf(\"i = %d last = %d ac = %d point = %d cmb = %d act = %d res = %f\\n\",\n // i, last, ac, point, cmb, ACT, res);\n // }\n Set(i, last, ac, point, cmb, res);\n }\n }\n }\n }\n }\n\n printf(\"%.12f\\n\", get(0, 4, 10, 0, 0));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6040, "memory_kb": 48948, "score_of_the_acc": -1.5979, "final_rank": 11 }, { "submission_id": "aoj_1511_1144051", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\nconst int MAXA = 100;\nconst int MAXP = 10000;\nint L;\n//bool used[105][5][11][101][11];\ndouble memo[101][5][11][101][11];\ndouble get(int idx, int last, int ac, int point, int cmb) {\n if(point >= 100) point = 100;\n if(cmb >= 11) cmb = 10;\n if(idx == L) return (point == 100 ? 1.0 : 0.0);\n // assert(point % 100 == 0);\n //point /= 100;\n // assert(ac % 10 == 0);\n //ac /= 10;\n // assert(idx >= 0 && idx < 101);\n // assert(last >= 0 && last < 5);\n // assert(ac >= 0 && ac < 11);\n // assert(point >= 0 && point < 101);\n // assert(cmb >= 0 && cmb < 11);\n //assert(used[idx][last][ac][point][cmb]);\n return memo[idx][last][ac][point][cmb];\n}\n\nvoid Set(int idx, int last, int ac, int point, int cmb, double val) {\n //used[idx][last][ac][point][cmb] = true;\n //assert(point % 100 == 0);\n //point /= 100;\n //assert(ac % 10 == 0);\n //ac /= 10;\n // assert(idx >= 0 && idx < 101);\n // assert(last >= 0 && last < 5);\n // assert(ac >= 0 && ac < 11);\n // assert(point >= 0 && point < 101);\n // assert(cmb >= 0 && cmb < 11);\n memo[idx][last][ac][point][cmb] = val;\n}\n\nint main(){\n while(true) {\n //memset(used, 0, sizeof(used));\n\n int D[5][5];\n REP(y, 4) REP(x, 4) cin >> D[y][x];\n if(D[0][0] < 0) break;\n\n cin >> L;\n\n vector<int> s(L);\n REP(i, L) cin >> s[i];\n\n int A, B;\n cin >> A >> B;\n A /= 100;\n B /= 100;\n double probA[11] = {};\n double probB[11] = {};\n REP(i, 11) probA[i] = i / 10.0;\n REP(i, 11) probB[i] = (10.0-i) / 10.0;\n\n for(int i = L - 1; i >= 0; i--) {\n for(int last = 0; last < 5; last++) {\n for(int ac = 0; ac <= 10; ac ++) {\n for(int point = 0; point <= 100; point ++) {\n for(int cmb = 0; cmb <= 10; cmb++) {\n double res = 0.0;\n if(s[i] == 1) for(int act = 0; act < 2; act++) {\n int nac = 10;\n if(last != 4) {\n nac = max(0, (D[last][act] - 10) + ac);\n }\n\n int ncmb = cmb + 1;\n int npoint = point + A + B * cmb;\n // if(i == 0 && last == 4 && ac == 100 && point == 0 && cmb == 0) {\n // printf(\"i = %d last = %d ac = %d point = %d cmb = %d act = %d res = %f\\n\",\n // i+1, act, nac, npoint, ncmb, act, (double)ac / 100.0 * get(i+1, act, nac, npoint, ncmb)\n // );\n // }\n double cand = \n probA[nac] * get(i+1, act, nac, npoint, ncmb) +\n probB[nac] * get(i+1, act, nac, point, 0);\n if(res < cand) {\n res = cand;\n }\n\n }\n if(s[i] == 2) for(int act = 2; act < 4; act++) {\n int nac = 10;\n if(last != 4) {\n nac = max(0, (D[last][act] - 10) + ac);\n }\n\n int ncmb = cmb + 1;\n int npoint = point + A + B * cmb;\n // if(i == 0 && last == 4 && ac == 100 && point == 0 && cmb == 0) {\n // printf(\"i = %d last = %d ac = %d point = %d cmb = %d act = %d res = %f\\n\",\n // i+1, act, nac, npoint, ncmb, act, (double)ac / 100.0 * get(i+1, act, nac, npoint, ncmb)\n // );\n // }\n double cand = \n probA[nac] * get(i+1, act, nac, npoint, ncmb) +\n probB[nac] * get(i+1, act, nac, point, 0);\n if(res < cand) {\n res = cand;\n }\n\n }\n {\n int nac = 10;\n int ncmb = (s[i] == 0 ? cmb : 0);\n int npoint = point;\n double cand = get(i+1, 4, nac, npoint, ncmb);\n if(res < cand) {\n res = cand;\n }\n }\n // if(i == 0 && last == 4 && ac == 100 && point == 0 && cmb == 0) {\n // printf(\"i = %d last = %d ac = %d point = %d cmb = %d act = %d res = %f\\n\",\n // i, last, ac, point, cmb, ACT, res);\n // }\n Set(i, last, ac, point, cmb, res);\n }\n }\n }\n }\n }\n\n printf(\"%.12f\\n\", get(0, 4, 10, 0, 0));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6030, "memory_kb": 48952, "score_of_the_acc": -1.5965, "final_rank": 10 }, { "submission_id": "aoj_1511_1134980", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nint tx[] = {0,1,0,-1};\nint ty[] = {-1,0,1,0};\n \nstatic const double EPS = 1e-8;\n\ndouble dp[101][11][101][11][5];//dp[note_i][prev_combo][prev_score][prev_stability][prev_action]\n\nint main(){\n while(true){\n //lt rt lk rk\n int stability[4][4];\n for(int i = 0; i < 4; i++){\n int count = 0;\n for(int j = 0; j < 4; j++){\n scanf(\"%d\",&stability[i][j]);\n if(i == 0 && stability[i][j] == -1){\n count++;\n }\n }\n if(count >= 4) goto over;\n }\n int len;\n scanf(\"%d\",&len);\n \n int notes[101];\n \n //0:none 1:t 2:k\n for(int note_i = 0; note_i < len; note_i++){\n scanf(\"%d\",&notes[note_i]);\n }\n int A,B;\n scanf(\"%d %d\",&A,&B);\n \n memset(dp,0,sizeof(dp));\n\n for(int prev_combo = 10; prev_combo >= 0; prev_combo--){\n for(int prev_action = 4; prev_action >= 0; prev_action--){\n for(int prev_stability = 10; prev_stability >= 0; prev_stability--){\n dp[len][prev_combo][100][prev_stability][prev_action] = 1.0;\n }\n }\n }\n\n //dp[note_i][prev_combo][prev_score][prev_stability][prev_action]\n for(int note_i = len - 1; note_i >= 0; note_i--){\n if(notes[note_i] == 0){\n for(int prev_score = 100; prev_score >= 0; prev_score--){\n for(int prev_combo = 10; prev_combo >= 0; prev_combo--){\n for(int prev_stability = 10; prev_stability >= 0; prev_stability--){\n for(int prev_action = 4; prev_action >= 0; prev_action--){\n dp[note_i][prev_combo][prev_score][prev_stability][prev_action]\n = dp[note_i + 1][prev_combo][prev_score][10][4];\n }\n }\n }\n }\n } \n else if(notes[note_i] > 0){\n int offset = (notes[note_i] == 1 ? 0 : 2);\n for(int prev_score = 100; prev_score >= 0; prev_score--){\n for(int prev_combo = 10; prev_combo >= 0; prev_combo--){\n int added = (A + B * prev_combo) / 100;\n\n for(int prev_stability = 10; prev_stability >= 0; prev_stability--){\n for(int prev_action = 3; prev_action >= 0; prev_action--){\n dp[note_i][prev_combo][prev_score][prev_stability][prev_action] \n = dp[note_i + 1][0][prev_score][10][4];\n\n int st0 = max(0,(stability[prev_action][offset + 0] - 10) * 10 + prev_stability * 10);\n int st1 = max(0,(stability[prev_action][offset + 1] - 10) * 10 + prev_stability * 10);\n \n double precision0 = (double)st0/100.0;\n double precision1 = (double)st1/100.0;\n\n\n //bang\n dp[note_i][prev_combo][prev_score][prev_stability][prev_action] \n = max(dp[note_i][prev_combo][prev_score][prev_stability][prev_action] ,\n dp[note_i + 1][min(10,prev_combo + 1)][min(100,prev_score + added)][st0 / 10][offset + 0] * precision0\n + dp[note_i + 1][0][prev_score][st0 / 10][offset + 0] * (1.0 - precision0));\n\n dp[note_i][prev_combo][prev_score][prev_stability][prev_action] \n = max(dp[note_i][prev_combo][prev_score][prev_stability][prev_action],\n dp[note_i + 1][min(10,prev_combo + 1)][min(100,prev_score + added)][st1 / 10][offset + 1] * precision1\n + dp[note_i + 1][0][prev_score][st1 / 10][offset + 1] * (1.0 - precision1));\n \n }\n\n //ignore\n dp[note_i][prev_combo][prev_score][prev_stability][4]\n = max(dp[note_i + 1][0][prev_score][10][4],\n max(dp[note_i + 1][min(10,prev_combo + 1)][min(100,prev_score + added)][10][offset + 0],\n dp[note_i + 1][min(10,prev_combo + 1)][min(100,prev_score + added)][10][offset + 1]));\n\n }\n }\n }\n }\n }\n printf(\"%.6lf\\n\",dp[0][0][0][10][4]);\n }\n over:;\n\n}", "accuracy": 1, "time_ms": 4960, "memory_kb": 49444, "score_of_the_acc": -1.4472, "final_rank": 8 }, { "submission_id": "aoj_1511_1134974", "code_snippet": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <deque>\n#include <bitset>\n#include <list>\n#include <cctype>\n#include <utility>\n \nusing namespace std;\n \ntypedef long long ll;\ntypedef pair <int,int> P;\ntypedef pair <int,P > PP;\n \nint tx[] = {0,1,0,-1};\nint ty[] = {-1,0,1,0};\n \nstatic const double EPS = 1e-8;\n\ndouble dp[101][11][101][11][5];//dp[note_i][prev_combo][prev_score][prev_stability][prev_action]\n\nint main(){\n while(true){\n //lt rt lk rk\n int stability[4][4];\n for(int i = 0; i < 4; i++){\n int count = 0;\n for(int j = 0; j < 4; j++){\n scanf(\"%d\",&stability[i][j]);\n if(i == 0 && stability[i][j] == -1){\n count++;\n }\n }\n if(count >= 4) goto over;\n }\n int len;\n scanf(\"%d\",&len);\n \n int notes[101];\n \n //0:none 1:t 2:k\n for(int note_i = 0; note_i < len; note_i++){\n scanf(\"%d\",&notes[note_i]);\n }\n int A,B;\n scanf(\"%d %d\",&A,&B);\n \n memset(dp,0,sizeof(dp));\n\n for(int prev_combo = 10; prev_combo >= 0; prev_combo--){\n for(int prev_action = 4; prev_action >= 0; prev_action--){\n for(int prev_stability = 10; prev_stability >= 0; prev_stability--){\n dp[len][prev_combo][100][prev_stability][prev_action] = 1.0;\n }\n }\n }\n\n //dp[note_i][prev_combo][prev_score][prev_stability][prev_action]\n for(int note_i = len - 1; note_i >= 0; note_i--){\n if(notes[note_i] == 0){\n for(int prev_score = 100; prev_score >= 0; prev_score--){\n for(int prev_combo = 10; prev_combo >= 0; prev_combo--){\n for(int prev_stability = 10; prev_stability >= 0; prev_stability--){\n for(int prev_action = 4; prev_action >= 0; prev_action--){\n dp[note_i][prev_combo][prev_score][prev_stability][prev_action]\n = dp[note_i + 1][prev_combo][prev_score][10][4];\n }\n }\n }\n }\n } \n else if(notes[note_i] > 0){\n int offset = (notes[note_i] == 1 ? 0 : 2);\n for(int prev_score = 100; prev_score >= 0; prev_score--){\n for(int prev_combo = 10; prev_combo >= 0; prev_combo--){\n int added = (A + B * prev_combo) / 100;\n\n for(int prev_stability = 10; prev_stability >= 0; prev_stability--){\n for(int prev_action = 3; prev_action >= 0; prev_action--){\n dp[note_i][prev_combo][prev_score][prev_stability][prev_action] \n = dp[note_i + 1][0][prev_score][10][4];\n\n int st0 = max(0,(stability[prev_action][offset + 0] - 10) * 10 + prev_stability * 10);\n int st1 = max(0,(stability[prev_action][offset + 1] - 10) * 10 + prev_stability * 10);\n \n double precision0 = (double)st0/100.0;\n double precision1 = (double)st1/100.0;\n\n\n //bang\n dp[note_i][prev_combo][prev_score][prev_stability][prev_action] \n = max(dp[note_i][prev_combo][prev_score][prev_stability][prev_action] ,\n dp[note_i + 1][min(10,prev_combo + 1)][min(100,prev_score + added)][st0 / 10][offset + 0] * precision0\n + dp[note_i + 1][0][prev_score][st0 / 10][offset + 0] * (1.0 - precision0));\n\n dp[note_i][prev_combo][prev_score][prev_stability][prev_action] \n = max(dp[note_i][prev_combo][prev_score][prev_stability][prev_action],\n dp[note_i + 1][min(10,prev_combo + 1)][min(100,prev_score + added)][st1 / 10][offset + 1] * precision1\n + dp[note_i + 1][0][prev_score][st1 / 10][offset + 1] * (1.0 - precision1));\n \n }\n\n dp[note_i][prev_combo][prev_score][prev_stability][4] = dp[note_i + 1][0][prev_score][10][4];\n dp[note_i][prev_combo][prev_score][prev_stability][4]\n = max(dp[note_i][prev_combo][prev_score][prev_stability][4],\n max(dp[note_i + 1][min(10,prev_combo + 1)][min(100,prev_score + added)][10][offset + 0],\n dp[note_i + 1][min(10,prev_combo + 1)][min(100,prev_score + added)][10][offset + 1]));\n\n }\n }\n }\n }\n }\n printf(\"%.6lf\\n\",dp[0][0][0][10][4]);\n }\n over:;\n\n}", "accuracy": 1, "time_ms": 5060, "memory_kb": 49444, "score_of_the_acc": -1.4619, "final_rank": 9 }, { "submission_id": "aoj_1511_973542", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n#include <iterator>\n#include <bitset>\n#include <random>\n#include <assert.h>\n#include <unordered_map>\n#include <array>\n#include <ctime>\n\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define X first\n#define Y second\n#define MP make_pair\n#define umap unordered_map\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair<int, int> P;\ntypedef unsigned int uint;\n\n//map<tuple<int, int, int, int, int>, double> dp;\n//int table[4][4];\n//int v[101];\n//\n//double EPS = 1e-9;\n//\n//int L, A, B;\n//double rec(int n, int acc, int prv, int comb, int pnt){\n//\tif (n == L) return pnt == 100;\n//\n//\tauto t = make_tuple(n, acc, prv, comb, pnt);\n//\tif (dp.count(t)) return dp[t];\n//\tdouble &res = dp[t];\n//\n//\tres = 0;\n//\n//\tres = rec(n + 1, 0, 4, v[n] ? 0 : comb, pnt);\n//\tif(v[n] != 0) for (int i = 0; i < 4; ++i){\n//\t\tif (v[n] == 2 && i < 2) continue;\n//\t\tif (v[n] == 1 && i >= 2) continue;\n//\t\tint ip;\n//\t\tif (prv == 4) ip = 10;\n//\t\telse ip = max(0, (table[prv][i] - 10) + acc);\n//\t\tdouble p = ip / 10.;\n//\n//\t\tdouble tmp = 0;\n//\t\tif(p > EPS) tmp += p*rec(n + 1, ip, i, comb+1, min(100, pnt+A+B*min(comb, 10)));\n//\t\tif(1-p > EPS) tmp += (1 - p)*rec(n + 1, ip, i, 0, pnt);\n//\t\tres = max(res, tmp);\n//\t}\n//\n//\treturn res;\n//}\n\nint table[4][4];\nint v[101];\n\ndouble EPS = 1e-9;\nint L, A, B;\n\ndouble dp[2][11][5][11][101]; // i, prv_acc, prv_pos, combo, point\n\nint main(){\n\tcout.setf(ios::fixed);\n\tcout.precision(10);\n\twhile (1){\n\t\trep(i, 4) rep(j, 4){\n\t\t\tcin >> table[i][j];\n\t\t\tif (table[i][j] < 0) goto end;\n\t\t}\n\t\tcin >> L;\n\t\trep(i, L) cin >> v[i];\n\t\tcin >> A >> B;\n\t\tA /= 100, B /= 100;\n\n\t\tint cur = 0, nxt = 1;\n\t\trep(i, 11) rep(j, 5) rep(k, 11) rep(l, 101) dp[cur][i][j][k][l] = l == 100;\n\t\tfor (int n = L - 1; n >= 0; --n){\n\t\t\trep(acc, 11) rep(prv, 5) rep(comb, 11) rep(pnt, 101){\n\t\t\t\tdouble &res = dp[nxt][acc][prv][comb][pnt];\n\t\t\t\tres = 0;\n\t\t\t\t\n\t\t\t\tres = dp[cur][0][4][v[n] ? 0 : comb][pnt];\n\t\t\t\tif(v[n] != 0) for (int i = 0; i < 4; ++i){\n\t\t\t\t\tif (v[n] == 2 && i < 2) continue;\n\t\t\t\t\tif (v[n] == 1 && i >= 2) continue;\n\t\t\t\t\tint ip;\n\t\t\t\t\tif (prv == 4) ip = 10;\n\t\t\t\t\telse ip = max(0, (table[prv][i] - 10) + acc);\n\t\t\t\t\tdouble p = ip * .1;\n\t\t\t\t\n\t\t\t\t\tdouble tmp = 0;\n\t\t\t\t\ttmp += p*dp[cur][ip][i][min(10, comb+1)][min(100, pnt+A+B*min(comb, 10))];\n\t\t\t\t\ttmp += (1 - p)*dp[cur][ip][i][0][pnt];\n\t\t\t\t\tres = max(res, tmp);\n\t\t\t\t}\n\t\t\t}\n\t\t\tswap(cur, nxt);\n\t\t}\n\t\tcout << dp[cur][0][4][0][0] << endl;\n\t}\n\nend:\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6370, "memory_kb": 2192, "score_of_the_acc": -0.9238, "final_rank": 2 }, { "submission_id": "aoj_1511_791873", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<(b)?(a=b,1):0)\n#define chmin(a,b) (a>(b)?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\nint stability[4][4];\nint s[100];\ndouble dp[101][101][11][11][3];\n\nbool solve() {\n REP(i,4) {\n REP(j,4) {\n cin >> stability[i][j];\n if (stability[i][j] == -1) return 0;\n }\n }\n int L;\n cin >> L;\n REP(i,L) cin >> s[i];\n int A, B;\n cin >> A >> B;\n A/=100;B/=100;\n\n REP(i,L+1)REP(j,101)REP(k,11)REP(l,11)REP(m,3)dp[i][j][k][l][m] = 0;\n REP(k,11) REP(l,11) REP(m,3) dp[L][100][k][l][m] = 1;\n\n for (int i=L-1; i>=0; --i) {\n REP(j,101) {\n REP(k,11) {\n REP(l,11) {\n REP(m,3) {\n if (m==2&&k!=10) continue;\n if (i==0&&m!=2) continue;\n if (i&&s[i-1]==0&&m!=2) continue;\n double &DP = dp[i][j][k][l][m];\n if (s[i] == 0) {\n chmax(DP, dp[i+1][j][10][l][2]);\n } else {\n int score = A + B*l;\n REP(n,2) {\n int stab;\n if (m == 2) {\n stab = 10;\n } else {\n int cur = (s[i]-1)*2 + n;\n int pre = (s[i-1]-1)*2 + m;\n stab = max(0, stability[pre][cur]-10+k);\n }\n double prob = 0.1*stab;\n chmax(DP, dp[i+1][min(j+score,100)][stab][min(l+1,10)][n]*prob + dp[i+1][j][stab][0][n]*(1-prob));\n }\n chmax(DP, dp[i+1][j][10][0][2]);\n }\n }\n }\n }\n }\n }\n double ans = dp[0][0][10][0][2];\n\n printf(\"%.10f\\n\", ans);\n return 1;\n}\n\nint main() {\n while(solve());\n\n}", "accuracy": 1, "time_ms": 3520, "memory_kb": 30112, "score_of_the_acc": -0.9373, "final_rank": 3 }, { "submission_id": "aoj_1511_791871", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<(b)?(a=b,1):0)\n#define chmin(a,b) (a>(b)?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\nint stability[4][4];\nint s[100];\ndouble dp[101][101][11][11][3];\n\nbool solve() {\n REP(i,4) {\n REP(j,4) {\n cin >> stability[i][j];\n if (stability[i][j] == -1) return 0;\n }\n }\n int L;\n cin >> L;\n REP(i,L) cin >> s[i];\n int A, B;\n cin >> A >> B;\n A/=100;B/=100;\n\n REP(i,L+1)REP(j,101)REP(k,11)REP(l,11)REP(m,3)dp[i][j][k][l][m] = 0;\n REP(k,11) REP(l,11) REP(m,3) dp[L][100][k][l][m] = 1;\n\n for (int i=L-1; i>=0; --i) {\n REP(j,101) {\n REP(k,11) {\n REP(l,11) {\n REP(m,3) {\n if (m==2&&k!=10) continue;\n if (i==0&&m!=2) continue;\n if (i&&s[i-1]==0&&m!=2) continue;\n double &DP = dp[i][j][k][l][m];\n if (s[i] == 0) {\n chmax(DP, dp[i+1][j][10][l][2]);\n } else {\n int score = A + B*l;\n REP(n,2) {\n int stab;\n if (m == 2) {\n stab = 10;\n } else {\n int cur = (s[i]-1)*2 + n;\n int pre = (s[i-1]-1)*2 + m;\n stab = max(0, stability[pre][cur]-10+k);\n }\n double prob = 0.1*stab;\n chmax(DP, dp[i+1][min(j+score,100)][stab][min(l+1,10)][n]*prob + dp[i+1][j][stab][0][2]*(1-prob));\n }\n chmax(DP, dp[i+1][j][10][0][2]);\n }\n }\n }\n }\n }\n }\n double ans = dp[0][0][10][0][2];\n\n printf(\"%.10f\\n\", ans);\n return 1;\n}\n\nint main() {\n while(solve());\n\n}", "accuracy": 0.2857142857142857, "time_ms": 360, "memory_kb": 30112, "score_of_the_acc": -0.474, "final_rank": 13 }, { "submission_id": "aoj_1511_791867", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define chmax(a,b) (a<(b)?(a=b,1):0)\n#define chmin(a,b) (a>(b)?(a=b,1):0)\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\nconst int INF = 1<<29;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\nint stability[4][4];\nint s[100];\ndouble dp[101][101][11][11][3];\n\nbool solve() {\n REP(i,4) {\n REP(j,4) {\n cin >> stability[i][j];\n if (stability[i][j] == -1) return 0;\n }\n }\n int L;\n cin >> L;\n REP(i,L) cin >> s[i];\n int A, B;\n cin >> A >> B;\n A/=100;B/=100;\n\n REP(i,L+1)REP(j,101)REP(k,11)REP(l,11)REP(m,3)dp[i][j][k][l][m] = 0;\n REP(k,11) REP(l,11) REP(m,3) dp[L][100][k][l][m] = 1;\n\n for (int i=L-1; i>=0; --i) {\n REP(j,101) {\n REP(k,11) {\n REP(l,11) {\n REP(m,3) {\n if (m==2&&k!=10) continue;\n if (i==0&&m!=2) continue;\n if (i&&s[i-1]==0&&m!=2) continue;\n double &DP = dp[i][j][k][l][m];\n if (s[i] == 0) {\n chmax(DP, dp[i+1][j][10][l][2]);\n } else {\n int score = A + B*l;\n REP(n,2) {\n int stab;\n if (m == 2) {\n stab = 10;\n } else {\n int cur = (s[i]-1)*2 + n;\n int pre = (s[i-1]-1)*2 + m;\n stab = max(0, stability[pre][cur]-10+k);\n }\n double prob = 0.1*stab;\n chmax(DP, dp[i+1][min(j+score,100)][stab][min(l+1,10)][n]*prob + dp[i+1][j][10][0][2]*(1-prob));\n }\n chmax(DP, dp[i+1][j][10][0][2]);\n }\n }\n }\n }\n }\n }\n double ans = dp[0][0][10][0][2];\n\n printf(\"%.10f\\n\", ans);\n return 1;\n}\n\nint main() {\n while(solve());\n\n}", "accuracy": 0.2857142857142857, "time_ms": 360, "memory_kb": 30112, "score_of_the_acc": -0.474, "final_rank": 13 }, { "submission_id": "aoj_1511_760528", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\nusing namespace std;\nint main(){\n while(true){\n int M[5][5];\n\n for(int i = 0; i < 4; i++)\n for(int j = 0; j < 4; j++)\n cin >> M[i][j];\n for(int i = 0; i < 5; i++){\n M[4][i] = M[i][4] = 10;\n }\n\n if(M[0][0] < 0) break;\n\n int L;\n cin >> L;\n vector<int> S(L);\n\n for(int i = 0; i < L; i++){\n cin >> S[i];\n }\n\n int A, B;\n cin >> A >> B;\n A /= 100, B /= 100;\n\n static double dp[101][11][101][5][11] = {}; // time, before precision, score, before, combo\n memset(dp, 0, sizeof dp);\n\n for(int i = 0; i <= 10; i++)\n for(int b = 0; b < 5; b++)\n for(int c = 0; c <= 10; c++)\n dp[L][i][100][b][c] = 1.0;\n\n for(int t = L - 1; t >= 0; t--)\n for(int p = 0; p <= 10; p++)\n for(int s = 0; s <= 100; s++)\n for(int c = 0; c <= 10; c++)\n for(int y = 0; y < 5; y++) // t - 1\n for(int x = 0; x < 5; x++){ // t\n if(S[t] != 0 && (x / 2) != S[t] - 1 && x != 4) continue;\n if(S[t] == 0 && x != 4) continue;\n int np = ((x == 4 || S[t] == 0) ? 10 : max(0, (M[y][x] - 10) + p));\n int ns = ((x == 4 || S[t] == 0) ? s : min(100, s + A + B * c));\n int nc = (S[t] == 0 ? c : x == 4 ? 0 : min(10, c + 1));\n\n dp[t][p][s][y][c] = max(dp[t][p][s][y][c],\n np / 10.0 * dp[t + 1][np][ns][x][nc] +\n (10.0 - np) / 10.0 * dp[t + 1][ns][s][x][0]);\n }\n\n printf(\"%.12f\\n\", dp[0][10][0][4][0]);\n }\n}", "accuracy": 0.14285714285714285, "time_ms": 70, "memory_kb": 49436, "score_of_the_acc": -0.7301, "final_rank": 19 }, { "submission_id": "aoj_1511_760524", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\nusing namespace std;\nint main(){\n while(true){\n int M[5][5];\n\n for(int i = 0; i < 4; i++)\n for(int j = 0; j < 4; j++)\n cin >> M[i][j];\n for(int i = 0; i < 5; i++){\n M[4][i] = M[i][4] = 10;\n }\n\n if(M[0][0] < 0) break;\n\n int L;\n cin >> L;\n vector<int> S(L);\n\n for(int i = 0; i < L; i++){\n cin >> S[i];\n }\n\n int A, B;\n cin >> A >> B;\n A /= 100, B /= 100;\n\n static double dp[101][11][101][5][11] = {}; // time, before precision, score, before, combo\n memset(dp, 0, sizeof dp);\n\n for(int i = 0; i <= 10; i++)\n for(int b = 0; b < 5; b++)\n for(int c = 0; c <= 10; c++)\n dp[L][i][100][b][c] = 1.0;\n\n for(int t = L - 1; t >= 0; t--)\n for(int p = 0; p <= 10; p++)\n for(int s = 0; s <= 100; s++)\n for(int c = 0; c <= 10; c++)\n for(int y = 0; y < 5; y++) // t - 1\n for(int x = 0; x < 5; x++){ // t\n if(S[t] != 0 && (x / 2) != S[t] - 1 && x != 4) continue;\n if(S[t] == 0 && x != 4) continue;\n int np = ((x == 4 || S[t] == 0) ? 10 : max(0, (M[y][x] - 10) + p));\n int ns = ((x == 4 || S[t] == 0) ? s : min(100, s + A + B * c));\n int nc = (S[t] == 0 ? c : x == 4 ? 0 : min(10, c + 1));\n\n dp[t][p][s][y][c] = max(dp[t][p][s][y][c],\n np / 10.0 * dp[t + 1][np][ns][x][nc] +\n (10.0 - np) / 10.0 * dp[t + 1][10][s][4][0]);\n }\n\n printf(\"%.12f\\n\", dp[0][10][0][4][0]);\n }\n}", "accuracy": 0.2857142857142857, "time_ms": 1300, "memory_kb": 49436, "score_of_the_acc": -0.9105, "final_rank": 16 }, { "submission_id": "aoj_1511_760523", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\nusing namespace std;\nint main(){\n while(true){\n int M[5][5];\n\n for(int i = 0; i < 4; i++)\n for(int j = 0; j < 4; j++)\n cin >> M[i][j];\n for(int i = 0; i < 5; i++){\n M[4][i] = M[i][4] = 10;;\n }\n\n if(M[0][0] < 0) break;\n\n int L;\n cin >> L;\n vector<int> S(L);\n\n for(int i = 0; i < L; i++){\n cin >> S[i];\n }\n\n int A, B;\n cin >> A >> B;\n A /= 100, B /= 100;\n\n static double dp[101][11][101][5][11] = {}; // time, before precision, score, before, combo\n memset(dp, 0, sizeof dp);\n\n for(int i = 0; i <= 10; i++)\n for(int b = 0; b < 5; b++)\n for(int c = 0; c <= 10; c++)\n dp[L][i][100][b][c] = 1.0;\n\n for(int t = L - 1; t >= 0; t--)\n for(int p = 0; p <= 10; p++)\n for(int s = 0; s <= 100; s++)\n for(int c = 0; c <= 10; c++)\n for(int y = 0; y < 5; y++) // t - 1\n for(int x = 0; x < 5; x++){ // t\n if(S[t] != 0 && (x / 2) != S[t] - 1 && x != 4) continue;\n int np = ((x == 4 || S[t] == 0) ? 10 : max(0, (M[y][x] - 10) + p));\n int ns = ((x == 4 || S[t] == 0) ? s : min(100, s + A + B * c));\n int nc = (S[t] == 0 ? c : x == 4 ? 0 : min(10, c + 1));\n\n dp[t][p][s][y][c] = max(dp[t][p][s][y][c],\n np / 10.0 * dp[t + 1][np][ns][x][nc] +\n (10.0 - np) / 10.0 * dp[t + 1][10][s][4][0]);\n }\n\n printf(\"%.12f\\n\", dp[0][10][0][4][0]);\n }\n}", "accuracy": 0.2857142857142857, "time_ms": 1420, "memory_kb": 49436, "score_of_the_acc": -0.9281, "final_rank": 17 }, { "submission_id": "aoj_1511_615497", "code_snippet": "#include <cstdio>\n#include <algorithm>\nusing namespace std;\n\ndouble dp[101][101][11][11][5];\nint stb[16], s[101];\nint L, A, B;\n\n/*\nprv\n\t0: 左トン\n\t1: 右トン\n\t2: 左コツ\n\t3: 右コツ\n\t4: none\n*/\ndouble dfs(int len, int scr, int cmb, int prc, int prv){\n\tcmb = min(cmb, 10);\n\tscr = min(scr, 100);\n\n\tdouble &ret = dp[len][scr][cmb][prc][prv];\n\tif(ret < 0.0){\n\t\tif(scr >= 100){\n\t\t\tret = 1.0;\n\t\t}\n\t\telse if(len == L){\n\t\t\tret = 0.0;\n\t\t}\n\t\telse if(s[len] == 0){\n\t\t\tret = dfs(len + 1, scr, cmb, 10, 4);\n\t\t}\n\t\telse if(prv == 4){\n\t\t\tret = dfs(len + 1, scr, 0, 10, 4);\n\t\t\tfor(int k = -2; k <= -1; ++k){\n\t\t\t\tret = max(ret, dfs(len + 1, scr + A + B * cmb, cmb + 1, 10, s[len] * 2 + k));\n\t\t\t}\n\t\t}\n\t\telse{\n\t\t\tret = dfs(len + 1, scr, 0, 10, 4);\n\t\t\tfor(int k = -2; k <= -1; ++k){\n\t\t\t\tint nxt = s[len] * 2 + k;\n\t\t\t\tint npc = max(0, stb[prv * 4 + nxt] - 10 + prc);\n\t\t\t\tdouble ps = dfs(len + 1, scr + A + B * cmb, cmb + 1, npc, nxt);\n\t\t\t\tdouble pf = dfs(len + 1, scr, 0, npc, nxt);\n\t\t\t\tret = max(ret, 0.1 * (npc * ps + (10 - npc) * pf));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\n\nint main(){\n\twhile(1){\n\t\tfor(int i = 0; i < 16; ++i){\n\t\t\tif(scanf(\"%d\", &stb[i]) != 1){ return 0; }\n\t\t}\n\t\tscanf(\"%d\", &L);\n\t\tfor(int i = 0; i < L; ++i){\n\t\t\tscanf(\"%d\", &s[i]);\n\t\t}\n\t\tscanf(\"%d%d\", &A, &B);\n\t\tA /= 100;\n\t\tB /= 100;\n\n\t\tfill(****dp, ****dp + sizeof(dp) / sizeof(double), -1.0);\n\n\t\tprintf(\"%f\\n\", dfs(0, 0, 0, 10, 4));\n\t}\n}", "accuracy": 1, "time_ms": 2460, "memory_kb": 49284, "score_of_the_acc": -1.0782, "final_rank": 6 }, { "submission_id": "aoj_1511_614900", "code_snippet": "#include <cstdio>\n#include <algorithm>\nusing namespace std;\n\ndouble dp[101][101][11][11][5];\nint stb[16], s[101];\nint L, A, B;\n\n/*\nprv\n\t0: 左トン\n\t1: 右トン\n\t2: 左コツ\n\t3: 右コツ\n\t4: fail\n*/\ndouble dfs(int len, int scr, int cmb, int prc, int prv){\n\tcmb = min(cmb, 10);\n\tscr = min(scr, 100);\n\tdouble &ret = dp[len][scr][cmb][prc][prv];\n\tif(ret < 0.0){\n\t\tif(scr >= 100){\n\t\t\tret = 1.0;\n\t\t}\n\t\telse if(len == L){\n\t\t\tret = 0.0;\n\t\t}\n\t\telse if(s[len] == 0){\n\t\t\tret = dfs(len + 1, scr, cmb, 10, 4);\n\t\t}\n\t\telse if(prv == 4){\n\t\t\tfor(int k = -2; k <= -1; ++k){\n\t\t\t\tret = max(ret, dfs(len + 1, scr + A + B * cmb, cmb + 1, 10, s[len] * 2 + k));\n\t\t\t}\n\t\t}\n\t\telse{\n\t\t\tret = dfs(len + 1, scr, 0, 10, 4);\n\t\t\tfor(int k = -2; k <= -1; ++k){\n\t\t\t\tint nxt = s[len] * 2 + k;\n\t\t\t\tint npc = max(0, stb[prv * 4 + nxt] - 10 + prc);\n\t\t\t\tdouble ps = dfs(len + 1, scr + A + B * cmb, cmb + 1, npc, nxt);\n\t\t\t\tdouble pf = dfs(len + 1, scr, 0, npc, nxt);\n\t\t\t\tret = max(ret, 0.1 * (npc * ps + (10 - npc) * pf));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\n\nint main(){\n\twhile(1){\n\t\tfor(int i = 0; i < 16; ++i){\n\t\t\tif(scanf(\"%d\", &stb[i]) != 1){ return 0; }\n\t\t}\n\t\tscanf(\"%d\", &L);\n\t\tfor(int i = 0; i < L; ++i){\n\t\t\tscanf(\"%d\", &s[i]);\n\t\t}\n\t\tscanf(\"%d%d\", &A, &B);\n\t\tA /= 100;\n\t\tB /= 100;\n\n\t\tfill(****dp, ****dp + sizeof(dp) / sizeof(double), -1.0);\n\n\t\tprintf(\"%f\\n\", dfs(0, 0, 0, 10, 4));\n\t}\n}", "accuracy": 0.2857142857142857, "time_ms": 180, "memory_kb": 49284, "score_of_the_acc": -0.7439, "final_rank": 15 }, { "submission_id": "aoj_1511_613185", "code_snippet": "#include <cstring>\n#include <queue>\n#include <cstdio>\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\n#include <set>\n\nusing namespace std;\n\nint safe[4][4];\nint n;\n// [pos][prev][antei][combo][point]\ndouble dp[101][5][11][11][101];\n\nvector<int> s;\nint a, b;\n\ndouble solve(int pos, int prev, int antei, int combo, int point){\n /*\n printf(\"pos: %d, prev: %d, antei: %d, combo: %d, point: %d\\n\",\n pos, prev, antei, combo, point);\n */\n if(pos == n){\n if(point == 100) return 1.0;\n else return 0.0;\n }\n\n double &ret = dp[pos][prev][antei][combo][point];\n if(ret >= -1.0) return ret;\n ret = 0.0;\n\n const int note = s[pos] - 1;\n\n if(note == -1){\n ret = solve(pos + 1, 4, 10, combo, point);\n }else{\n REP(h,2){\n const int hh = note * 2 + h;\n int aa = 0;\n if(prev == 4){\n aa = 10;\n }else{\n aa = max(0, antei + safe[prev][hh] - 10);\n }\n if(aa){\n ret = max(ret,\n (aa / 10.0) * solve(pos + 1, hh, aa, min(10, combo + 1), min(100, point + a + b * combo)) +\n (1.0 - aa / 10.0) * solve(pos + 1, hh, aa, 0, point));\n }\n }\n ret = max(ret, solve(pos + 1, 4, 10, 0, point));\n }\n\n return ret;\n}\n\nint main(){\n while(true){\n REP(i,4) safe[0][i] = getInt();\n\n if(safe[0][0] + safe[0][1] +\n safe[0][2] + safe[0][3] == -4)\n break;\n\n REP(i,3) REP(j,4)\n safe[i + 1][j] = getInt();\n\n const int l = getInt();\n vector<int> s(l);\n\n REP(i,l) s[i] = getInt();\n ::a = getInt() / 100;\n ::b = getInt() / 100;\n ::s = s;\n ::n = l;\n\n // double dp[101][5][11][11][101];\n REP(i,101) REP(j,5) REP(k,11) REP(l,11) REP(m,101)\n dp[i][j][k][l][m] = -2.0;\n\n printf(\"%.8f\\n\", solve(0, 4, 10, 0, 0));\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1510, "memory_kb": 49320, "score_of_the_acc": -0.9395, "final_rank": 4 }, { "submission_id": "aoj_1511_611663", "code_snippet": "#include <algorithm>\n#include <cstdio>\n#include <cstdlib>\n#include <vector>\nusing namespace std;\n\ntypedef vector<double> vec;\ntypedef vector<vec> vec2;\ntypedef vector<vec2> vec3;\ntypedef vector<vec3> vec4;\n\ntemplate<class T> inline void chmax(T& a, T b) { if(b > a) a = b; }\n\nenum { LT, RT, LK, RK, NONE };\n\nint main() {\n\tfor(;;) {\n\t\tint acc[4][4];\n\t\tfor(int i = 0; i < 4; ++i)\n\t\t\tscanf(\"%d\", &acc[0][i]);\n\n\t\tif(acc[0][0] < 0)\n\t\t\tbreak;\n\n\t\tfor(int i = 1; i < 4; ++i)\n\t\t\tfor(int j = 0; j < 4; ++j)\n\t\t\t\tscanf(\"%d\", &acc[i][j]);\n\n\t\tint l;\n\t\tscanf(\"%d\", &l);\n\n\t\tvector<int> s(l);\n\t\tfor(int i = 0; i < l; ++i)\n\t\t\tscanf(\"%d\", &s[i]);\n\n\t\tint a, b;\n\t\tscanf(\"%d %d\", &a, &b);\n\t\ta /= 100;\n\t\tb /= 100;\n\n\t\tvec4 dp(101, vec3(5, vec2(11, vec(11, 0.0))));\n\t\tfor(int i = 0; i < 5; ++i)\n\t\t\tfor(int j = 0; j < 11; ++j)\n\t\t\t\tfor(int k = 0; k < 11; ++k)\n\t\t\t\t\tdp[100][i][j][k] = 1.0;\n\n\t\tfor(int next = l - 1; next >= 0; --next) {\n\t\t\tvec4 tmp(101, vec3(5, vec2(11, vec(11, 1.0))));\n\t\t\tif(s[next] == 0) {\n\t\t\t\tfor(int sco = 0; sco < 100; ++sco)\n\t\t\t\t\tfor(int pre = 0; pre < 5; ++pre)\n\t\t\t\t\t\tfor(int com = 0; com <= 10; ++com)\n\t\t\t\t\t\t\tfor(int pac = 0; pac <= 10; ++pac)\n\t\t\t\t\t\t\t\ttmp[sco][pre][com][pac] = dp[sco][NONE][com][10];\n\t\t\t\tdp.swap(tmp);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst int start = (s[next] == 1 ? 0 : 2);\n\t\t\tfor(int sco = 0; sco < 100; ++sco) {\n\t\t\t\tfor(int pre = 0; pre < 4; ++pre) {\n\t\t\t\t\tfor(int pac = 0; pac <= 10; ++pac) {\n\t\t\t\t\t\tfor(int com = 0; com <= 10; ++com)\n\t\t\t\t\t\t\ttmp[sco][pre][com][pac] = dp[sco][NONE][0][10];\n\n\t\t\t\t\t\tfor(int i = start; i < start + 2; ++i) {\n\t\t\t\t\t\t\tconst int ac = max(0, (acc[pre][i] - 10) + pac);\n\t\t\t\t\t\t\tconst double p = ac / 10.0;\n\t\t\t\t\t\t\tfor(int com = 0; com <= 10; ++com) {\n\t\t\t\t\t\t\t\tconst double buf = p * dp[min(100, sco + a + b * min(10, com))][i][min(10, com + 1)][ac] + (1.0 - p) * dp[sco][i][0][ac];\n\t\t\t\t\t\t\t\tchmax(tmp[sco][pre][com][pac], buf);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor(int com = 0; com <= 10; ++com) {\n\t\t\t\t\tfor(int pac = 0; pac <= 10; ++pac) {\n\t\t\t\t\t\ttmp[sco][NONE][com][pac] = dp[sco][NONE][0][10];\n\t\t\t\t\t\tfor(int i = start; i < start + 2; ++i) {\n\t\t\t\t\t\t\tchmax(tmp[sco][NONE][com][pac], dp[min(100, sco + a + b * min(10, com))][i][min(10, com + 1)][10]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdp.swap(tmp);\n\t\t}\n\n\t\tprintf(\"%.5lf\\n\", dp[0][NONE][0][10]);\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 6890, "memory_kb": 2456, "score_of_the_acc": -1.0041, "final_rank": 5 } ]
aoj_1512_cpp
Problem D: Smartphone Game Problem 人気のスマフォゲームの一機能を実装してみよう。ゲームは以下の仕様に基づく。 5*5のマスに、5種類のブロックが設置されている。 それぞれの種類のブロックには得点が設定されている。 得点には、ブロックの得点の他に、ボーナス得点が設定されている。最初のボーナス得点は1である。 ブロックを任意に1つだけ決めて、最大で n 回まで上下左右に移動できる。移動先のブロックは移動元のブロックのあった場所に移動する。つまり、隣接したブロックを交換する事になる。 移動した後、同じ種類のブロックが縦3個以上又は横3個以上に並んだ場合、並んだブロックは全て消える。 消えるべきブロックが全て消えた後、あるブロックの下に別のブロックが存在しない場合、そのブロックは落下する。落下とは、あるブロックの1つ上に辿り着くまで下に移動する事をいう。ブロックが下に存在しない場合は一番下に移動する。 全てのブロックの落下後にボーナス得点が1だけ加算される。 その後にブロックが並んだ場合、さらに消え、落下する。 ブロックが消える時に、ブロック1つにつき「ブロックの得点*ボーナス得点」が自分の得点に加算される。 1回のプレイで得られる最大の点数を求めよ。 Input 入力は複数のデータセットからなる。 各データセットは以下で表される。 n a 11 .. a 15 . . a 51 .. a 55 score 1 .. score 5 a ij は1から5までの数字で、ブロックの種類を表す。 score i は i 種類目のブロック1つ分の得点を表す。 n = -1 の時、入力が終了する。 Constraints 入力は以下の条件を満たす。 0 ≤ n ≤ 5 1 ≤ a ij ≤ 5 0 ≤ score i ≤ 100 テストケースの数は 10 を超えない。 入力に含まれる値は全て整数である。 Output 各データセット毎に、答えを一行に出力しなさい。 Sample Input 0 1 1 1 5 5 5 5 5 5 5 5 5 1 5 5 5 1 1 1 5 5 5 5 5 5 0 0 0 0 1 2 1 2 3 4 5 2 3 4 5 5 2 3 4 5 5 3 4 5 5 1 5 4 3 2 1 100 99 98 97 96 5 1 2 3 4 5 2 3 4 5 1 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 99 79 31 23 56 -1 Sample Output 17 2040 926
[ { "submission_id": "aoj_1512_10235110", "code_snippet": "// AOJ #1512 Smartphone Game\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst int NROWS = 5, NCOLS = 5;\nint dr[4] = {-1,1,0,0};\nint dc[4] = {0,0,-1,1};\n\nvector<int> blockScore(6);\n\nint simulateChain(vector<vector<int>> board) {\n int totalScore = 0;\n int bonus = 1;\n \n while (true) {\n vector<vector<bool>> mark(NROWS, vector<bool>(NCOLS, false));\n bool anyMark = false;\n \n for (int r = 0; r < NROWS; r++) {\n int c = 0;\n while (c < NCOLS) {\n if(board[r][c] == 0) { c++; continue; }\n int start = c;\n int curType = board[r][c];\n while(c < NCOLS && board[r][c] == curType) c++;\n int len = c - start;\n if(len >= 3) {\n for (int j = start; j < c; j++) {\n mark[r][j] = true;\n anyMark = true;\n }\n }\n }\n }\n \n for (int c = 0; c < NCOLS; c++) {\n int r = 0;\n while (r < NROWS) {\n if(board[r][c] == 0) { r++; continue; }\n int start = r;\n int curType = board[r][c];\n while(r < NROWS && board[r][c] == curType) r++;\n int len = r - start;\n if(len >= 3) {\n for (int i = start; i < r; i++) {\n mark[i][c] = true;\n anyMark = true;\n }\n }\n }\n }\n \n if(!anyMark) break;\n \n for (int r = 0; r < NROWS; r++) {\n for (int c = 0; c < NCOLS; c++) {\n if(mark[r][c]) {\n int type = board[r][c];\n totalScore += blockScore[type] * bonus;\n board[r][c] = 0;\n }\n }\n }\n \n for (int c = 0; c < NCOLS; c++) {\n int write = NROWS - 1;\n for (int r = NROWS - 1; r >= 0; r--) {\n if(board[r][c] != 0) {\n board[write][c] = board[r][c];\n write--;\n }\n }\n for (int r = write; r >= 0; r--) board[r][c] = 0;\n }\n bonus++;\n }\n return totalScore;\n}\n\nint dfs(vector<vector<int>> board, int r, int c, int moves, int maxMoves) {\n int best = simulateChain(board);\n if(moves == maxMoves) return best;\n\n for(int d=0; d<4; d++){\n int nr = r + dr[d], nc = c + dc[d];\n if(nr < 0 || nr >= NROWS || nc < 0 || nc >= NCOLS) continue;\n vector<vector<int>> nextBoard = board;\n swap(nextBoard[r][c], nextBoard[nr][nc]);\n int candidate = dfs(nextBoard, nr, nc, moves+1, maxMoves);\n best = max(best, candidate);\n }\n return best;\n}\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n while(true){\n int n;\n cin >> n;\n if(n == -1) break;\n \n vector<vector<int>> board(NROWS, vector<int>(NCOLS));\n for(int i=0; i<NROWS; i++){\n for(int j=0; j<NCOLS; j++) cin >> board[i][j];\n }\n \n for(int i=1; i<=5; i++) cin >> blockScore[i];\n \n int answer = 0;\n for (int r = 0; r < NROWS; r++) {\n for (int c = 0; c < NCOLS; c++)\n answer = max(answer, dfs(board, r, c, 0, n));\n }\n cout << answer << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3468, "score_of_the_acc": -0.2432, "final_rank": 11 }, { "submission_id": "aoj_1512_8293413", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cassert>\nusing namespace std;\n\nint Score[6];\nint dx[4] = { 1, 0, -1, 0 };\nint dy[4] = { 0, 1, 0, -1 };\n\n// 再実装\nstruct State {\n\tint v[7][7];\n};\n\n// スコア計算\nint CalculateScore(State S) {\n\tint CurrentScore = 0;\n\tint BonusScore = 0;\n\n\t// シミュレーション\n\twhile (true) {\n\t\tBonusScore += 1;\n\t\tvector<vector<bool>> Delete(5, vector<bool>(5, false));\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (S.v[i][0] == S.v[i][1] && S.v[i][1] == S.v[i][2] && S.v[i][0] != 0) { Delete[i][0] = true; Delete[i][1] = true; Delete[i][2] = true; }\n\t\t\tif (S.v[i][1] == S.v[i][2] && S.v[i][2] == S.v[i][3] && S.v[i][1] != 0) { Delete[i][1] = true; Delete[i][2] = true; Delete[i][3] = true; }\n\t\t\tif (S.v[i][2] == S.v[i][3] && S.v[i][3] == S.v[i][4] && S.v[i][2] != 0) { Delete[i][2] = true; Delete[i][3] = true; Delete[i][4] = true; }\n\t\t}\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (S.v[0][i] == S.v[1][i] && S.v[1][i] == S.v[2][i] && S.v[0][i] != 0) { Delete[0][i] = true; Delete[1][i] = true; Delete[2][i] = true; }\n\t\t\tif (S.v[1][i] == S.v[2][i] && S.v[2][i] == S.v[3][i] && S.v[1][i] != 0) { Delete[1][i] = true; Delete[2][i] = true; Delete[3][i] = true; }\n\t\t\tif (S.v[2][i] == S.v[3][i] && S.v[3][i] == S.v[4][i] && S.v[2][i] != 0) { Delete[2][i] = true; Delete[3][i] = true; Delete[4][i] = true; }\n\t\t}\n\n\t\t// 消す処理\n\t\tint NewScore = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tif (Delete[i][j] == true) { NewScore += Score[S.v[i][j]]; S.v[i][j] = 0; }\n\t\t\t}\n\t\t}\n\t\tCurrentScore += NewScore * BonusScore;\n\t\tif (NewScore == 0) break;\n\n\t\t// 落ちる処理\n\t\tfor (int k = 0; k < 5; k++) {\n\t\t\tfor (int i = 0; i < 4; i++) {\n\t\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\t\tif (S.v[i + 1][j] == 0 && S.v[i][j] != 0) swap(S.v[i + 1][j], S.v[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// 答えを返す\n\treturn CurrentScore;\n}\n\n// 全探索\nint Solve(int dep, int px, int py, State S) {\n\tif (dep == 0) return CalculateScore(S);\n\n\t// 再帰呼び出し\n\tint Max = CalculateScore(S);\n\tfor (int i = 0; i < 4; i++) {\n\t\tint ex = px + dx[i];\n\t\tint ey = py + dy[i];\n\t\tif (ex < 0 || ey < 0 || ex > 4 || ey > 4) continue;\n\t\tState T = S; swap(T.v[px][py], T.v[ex][ey]);\n\t\tint ret = Solve(dep - 1, ex, ey, T);\n\t\tMax = max(Max, ret);\n\t}\n\n\t// 答えを返す\n\treturn Max;\n}\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tint N; cin >> N; if (N == -1) break;\n\t\tState S;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) cin >> S.v[i][j];\n\t\t}\n\t\tfor (int i = 1; i <= 5; i++) cin >> Score[i];\n\n\t\t// 制約違反をチェック\n\t\tassert(0 <= N && N <= 5);\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) assert(1 <= S.v[i][j] && S.v[i][j] <= 5);\n\t\t}\n\t\tfor (int i = 1; i <= 5; i++) {\n\t\t\tassert(0 <= Score[i] && Score[i] <= 100);\n\t\t}\n\t\t\n\t\t// Step 2. Brute Force\n\t\tint Answer = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tAnswer = max(Answer, Solve(N, i, j, S));\n\t\t\t}\n\t\t}\n\n\t\t// Step 3. Output\n\t\tcout << Answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.7142857142857143, "time_ms": 50, "memory_kb": 3336, "score_of_the_acc": -0.1152, "final_rank": 16 }, { "submission_id": "aoj_1512_8293408", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint Score[6];\nint dx[4] = { 1, 0, -1, 0 };\nint dy[4] = { 0, 1, 0, -1 };\n\n// 再実装\nstruct State {\n\tint v[7][7];\n};\n\n// スコア計算\nint CalculateScore(State S) {\n\tint CurrentScore = 0;\n\tint BonusScore = 0;\n\n\t// シミュレーション\n\twhile (true) {\n\t\tBonusScore += 1;\n\t\tvector<vector<bool>> Delete(5, vector<bool>(5, false));\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (S.v[i][0] == S.v[i][1] && S.v[i][1] == S.v[i][2] && S.v[i][0] != 0) { Delete[i][0] = true; Delete[i][1] = true; Delete[i][2] = true; }\n\t\t\tif (S.v[i][1] == S.v[i][2] && S.v[i][2] == S.v[i][3] && S.v[i][1] != 0) { Delete[i][1] = true; Delete[i][2] = true; Delete[i][3] = true; }\n\t\t\tif (S.v[i][2] == S.v[i][3] && S.v[i][3] == S.v[i][4] && S.v[i][2] != 0) { Delete[i][2] = true; Delete[i][3] = true; Delete[i][4] = true; }\n\t\t}\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (S.v[0][i] == S.v[1][i] && S.v[1][i] == S.v[2][i] && S.v[0][i] != 0) { Delete[0][i] = true; Delete[1][i] = true; Delete[2][i] = true; }\n\t\t\tif (S.v[1][i] == S.v[2][i] && S.v[2][i] == S.v[3][i] && S.v[1][i] != 0) { Delete[1][i] = true; Delete[2][i] = true; Delete[3][i] = true; }\n\t\t\tif (S.v[2][i] == S.v[3][i] && S.v[3][i] == S.v[4][i] && S.v[2][i] != 0) { Delete[2][i] = true; Delete[3][i] = true; Delete[4][i] = true; }\n\t\t}\n\n\t\t// 消す処理\n\t\tint NewScore = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tif (Delete[i][j] == true) { NewScore += Score[S.v[i][j]]; S.v[i][j] = 0; }\n\t\t\t}\n\t\t}\n\t\tCurrentScore += NewScore * BonusScore;\n\t\tif (NewScore == 0) break;\n\n\t\t// 落ちる処理\n\t\tfor (int k = 0; k < 5; k++) {\n\t\t\tfor (int i = 0; i < 4; i++) {\n\t\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\t\tif (S.v[i + 1][j] == 0 && S.v[i][j] != 0) swap(S.v[i + 1][j], S.v[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// 答えを返す\n\treturn CurrentScore;\n}\n\n// 全探索\nint Solve(int dep, int px, int py, State S) {\n\tif (dep == 0) return CalculateScore(S);\n\n\t// 再帰呼び出し\n\tint Max = CalculateScore(S);\n\tfor (int i = 0; i < 4; i++) {\n\t\tint ex = px + dx[i];\n\t\tint ey = py + dy[i];\n\t\tif (ex < 0 || ey < 0 || ex > 4 || ey > 4) continue;\n\t\tState T = S; swap(T.v[px][py], T.v[ex][ey]);\n\t\tint ret = Solve(dep - 1, ex, ey, T);\n\t\tMax = max(Max, ret);\n\t}\n\n\t// 答えを返す\n\treturn Max;\n}\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tint N; cin >> N; if (N == -1) break;\n\t\tState S;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) cin >> S.v[i][j];\n\t\t}\n\t\tfor (int i = 1; i <= 5; i++) cin >> Score[i];\n\t\t\n\t\t// Step 2. Brute Force\n\t\tint Answer = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tAnswer = max(Answer, Solve(N, i, j, S));\n\t\t\t}\n\t\t}\n\n\t\t// Step 3. Output\n\t\tcout << Answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.7142857142857143, "time_ms": 50, "memory_kb": 3424, "score_of_the_acc": -0.1352, "final_rank": 18 }, { "submission_id": "aoj_1512_8293386", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nconst int dx[4] = { 1, 0, -1, 0 };\nconst int dy[4] = { 0, 1, 0, -1 };\nvector<pair<int, int>> path;\n\nstruct State {\n\tint ball[5][5];\n};\n\nint GetScore(State G, vector<int> Score) {\n\tint TotalScore = 0;\n\n\t// Simulation\n\tfor (int t = 1; t <= 10000; t++) {\n\t\tvector<vector<bool>> Del(5, vector<bool>(5, false));\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (G.ball[i][0] == G.ball[i][1] && G.ball[i][1] == G.ball[i][2]) { Del[i][0] = true; Del[i][1] = true; Del[i][2] = true; }\n\t\t\tif (G.ball[i][1] == G.ball[i][2] && G.ball[i][2] == G.ball[i][3]) { Del[i][1] = true; Del[i][2] = true; Del[i][3] = true; }\n\t\t\tif (G.ball[i][2] == G.ball[i][3] && G.ball[i][3] == G.ball[i][4]) { Del[i][2] = true; Del[i][3] = true; Del[i][4] = true; }\n\t\t}\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (G.ball[0][i] == G.ball[1][i] && G.ball[1][i] == G.ball[2][i]) { Del[0][i] = true; Del[1][i] = true; Del[2][i] = true; }\n\t\t\tif (G.ball[1][i] == G.ball[2][i] && G.ball[2][i] == G.ball[3][i]) { Del[1][i] = true; Del[2][i] = true; Del[3][i] = true; }\n\t\t\tif (G.ball[2][i] == G.ball[3][i] && G.ball[3][i] == G.ball[4][i]) { Del[2][i] = true; Del[3][i] = true; Del[4][i] = true; }\n\t\t}\n\t\tint new_score = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tif (Del[i][j] == true) { new_score += t * Score[G.ball[i][j]]; G.ball[i][j] = 0; }\n\t\t\t}\n\t\t}\n\t\tif (new_score == 0) break;\n\n\t\t// Falling\n\t\tfor (int j = 0; j < 5; j++) {\n\t\t\tvector<int> vec;\n\t\t\tfor (int i = 4; i >= 0; i--) {\n\t\t\t\tif (G.ball[i][j] >= 1) vec.push_back(G.ball[i][j]);\n\t\t\t}\n\t\t\tfor (int i = 0; i < 5; i++) G.ball[i][j] = 0;\n\t\t\tfor (int i = 0; i < vec.size(); i++) G.ball[4 - i][j] = vec[i];\n\t\t}\n\t\tTotalScore += new_score;\n\t}\n\n\t// Return\n\treturn TotalScore;\n}\n\nint dfs(int rem, int px, int py, State S, vector<int> Score) {\n\tif (rem == 0) {\n\t\tint ret = GetScore(S, Score);\n\t\treturn ret;\n\t}\n\tint Return = GetScore(S, Score);\n\n\t// Tansaku\n\tfor (int i = 0; i < 4; i++) {\n\t\tint ex = px + dx[i];\n\t\tint ey = py + dy[i];\n\t\tif (ex < 0 || ey < 0 || ex >= 5 || ey >= 5) continue;\n\t\tState T = S;\n\t\tpath.push_back(make_pair(ex, ey));\n\t\tswap(T.ball[px][py], T.ball[ex][ey]);\n\t\tint ret = dfs(rem - 1, ex, ey, T, Score);\n\t\tReturn = max(Return, ret);\n\t\tpath.pop_back();\n\t}\n\n\t// Return\n\treturn Return;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N;\n\t\tState S;\n\t\tvector<int> Score(6, 0);\n\n\t\t// Step 1. Input\n\t\tcin >> N; if (N == -1) break;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) cin >> S.ball[i][j];\n\t\t}\n\t\tfor (int i = 1; i <= 5; i++) cin >> Score[i];\n\n\t\t// Step 2. Brute Force\n\t\tint Answer = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tpath.push_back(make_pair(i, j));\n\t\t\t\tint ret = dfs(N, i, j, S, Score);\n\t\t\t\tAnswer = max(Answer, ret);\n\t\t\t\tpath.clear();\n\t\t\t}\n\t\t}\n\n\t\t// Step 3. Output\n\t\tcout << Answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.7142857142857143, "time_ms": 80, "memory_kb": 3360, "score_of_the_acc": -0.1795, "final_rank": 20 }, { "submission_id": "aoj_1512_8293383", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nconst int dx[4] = { 1, 0, -1, 0 };\nconst int dy[4] = { 0, 1, 0, -1 };\nvector<pair<int, int>> path;\n\nstruct State {\n\tint ball[5][5];\n};\n\nint GetScore(State G, vector<int> Score) {\n\tint TotalScore = 0;\n\n\t// Simulation\n\tfor (int t = 1; t <= 10000; t++) {\n\t\tvector<vector<bool>> Del(5, vector<bool>(5, false));\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (G.ball[i][0] == G.ball[i][1] && G.ball[i][1] == G.ball[i][2]) { Del[i][0] = true; Del[i][1] = true; Del[i][2] = true; }\n\t\t\tif (G.ball[i][1] == G.ball[i][2] && G.ball[i][2] == G.ball[i][3]) { Del[i][1] = true; Del[i][2] = true; Del[i][3] = true; }\n\t\t\tif (G.ball[i][2] == G.ball[i][3] && G.ball[i][3] == G.ball[i][4]) { Del[i][2] = true; Del[i][3] = true; Del[i][4] = true; }\n\t\t}\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (G.ball[0][i] == G.ball[1][i] && G.ball[1][i] == G.ball[2][i]) { Del[0][i] = true; Del[1][i] = true; Del[2][i] = true; }\n\t\t\tif (G.ball[1][i] == G.ball[2][i] && G.ball[2][i] == G.ball[3][i]) { Del[1][i] = true; Del[2][i] = true; Del[3][i] = true; }\n\t\t\tif (G.ball[2][i] == G.ball[3][i] && G.ball[3][i] == G.ball[4][i]) { Del[2][i] = true; Del[3][i] = true; Del[4][i] = true; }\n\t\t}\n\t\tint new_score = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tif (Del[i][j] == true) { new_score += t * Score[G.ball[i][j]]; G.ball[i][j] = 0; }\n\t\t\t}\n\t\t}\n\t\tif (new_score == 0) break;\n\t\tfor (int k = 0; k < 5; k++) {\n\t\t\tfor (int i = 0; i < 4; i++) {\n\t\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\t\tif (G.ball[i + 1][j] == 0 && G.ball[i][j] != 0) swap(G.ball[i + 1][j], G.ball[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tTotalScore += new_score;\n\t}\n\n\t// Return\n\treturn TotalScore;\n}\n\nint dfs(int rem, int px, int py, State S, vector<int> Score) {\n\tif (rem == 0) {\n\t\tint ret = GetScore(S, Score);\n\t\treturn ret;\n\t}\n\tint Return = GetScore(S, Score);\n\n\t// Tansaku\n\tfor (int i = 0; i < 4; i++) {\n\t\tint ex = px + dx[i];\n\t\tint ey = py + dy[i];\n\t\tif (ex < 0 || ey < 0 || ex >= 5 || ey >= 5) continue;\n\t\tState T = S;\n\t\tpath.push_back(make_pair(ex, ey));\n\t\tswap(T.ball[px][py], T.ball[ex][ey]);\n\t\tint ret = dfs(rem - 1, ex, ey, T, Score);\n\t\tReturn = max(Return, ret);\n\t\tpath.pop_back();\n\t}\n\n\t// Return\n\treturn Return;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N;\n\t\tState S;\n\t\tvector<int> Score(6, 0);\n\n\t\t// Step 1. Input\n\t\tcin >> N; if (N == -1) break;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) cin >> S.ball[i][j];\n\t\t}\n\t\tfor (int i = 1; i <= 5; i++) cin >> Score[i];\n\n\t\t// Step 2. Brute Force\n\t\tint Answer = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tpath.push_back(make_pair(i, j));\n\t\t\t\tint ret = dfs(N, i, j, S, Score);\n\t\t\t\tAnswer = max(Answer, ret);\n\t\t\t\tpath.clear();\n\t\t\t}\n\t\t}\n\n\t\t// Step 3. Output\n\t\tcout << Answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.7142857142857143, "time_ms": 50, "memory_kb": 3356, "score_of_the_acc": -0.1197, "final_rank": 17 }, { "submission_id": "aoj_1512_8293338", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nconst int dx[4] = { 1, 0, -1, 0 };\nconst int dy[4] = { 0, 1, 0, -1 };\n\nstruct State {\n\tint ball[5][5];\n};\n\nint GetScore(State G, vector<int> Score) {\n\tint TotalScore = 0;\n\n\t// Simulation\n\tfor (int t = 1; t <= 10; t++) {\n\t\tvector<vector<bool>> Del(5, vector<bool>(5, false));\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (G.ball[i][0] == G.ball[i][1] && G.ball[i][1] == G.ball[i][2]) { Del[i][0] = true; Del[i][1] = true; Del[i][2] = true; }\n\t\t\tif (G.ball[i][1] == G.ball[i][2] && G.ball[i][2] == G.ball[i][3]) { Del[i][1] = true; Del[i][2] = true; Del[i][3] = true; }\n\t\t\tif (G.ball[i][2] == G.ball[i][3] && G.ball[i][3] == G.ball[i][4]) { Del[i][2] = true; Del[i][3] = true; Del[i][4] = true; }\n\t\t}\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tif (G.ball[0][i] == G.ball[1][i] && G.ball[1][i] == G.ball[2][i]) { Del[0][i] = true; Del[1][i] = true; Del[2][i] = true; }\n\t\t\tif (G.ball[1][i] == G.ball[2][i] && G.ball[2][i] == G.ball[3][i]) { Del[1][i] = true; Del[2][i] = true; Del[3][i] = true; }\n\t\t\tif (G.ball[2][i] == G.ball[3][i] && G.ball[3][i] == G.ball[4][i]) { Del[2][i] = true; Del[3][i] = true; Del[4][i] = true; }\n\t\t}\n\t\tint new_score = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tif (Del[i][j] == true) { new_score += t * Score[G.ball[i][j]]; G.ball[i][j] = 0; }\n\t\t\t}\n\t\t}\n\t\tif (new_score == 0) break;\n\t\tfor (int k = 0; k < 5; k++) {\n\t\t\tfor (int i = 0; i < 4; i++) {\n\t\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\t\tif (G.ball[i + 1][j] == 0 && G.ball[i][j] != 0) swap(G.ball[i + 1][j], G.ball[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tTotalScore += new_score;\n\t}\n\n\t// Return\n\treturn TotalScore;\n}\n\nint dfs(int rem, int px, int py, State S, vector<int> Score) {\n\tif (rem == 0) return GetScore(S, Score);\n\tint Return = GetScore(S, Score);\n\n\t// Tansaku\n\tfor (int i = 0; i < 4; i++) {\n\t\tint ex = px + dx[i];\n\t\tint ey = py + dy[i];\n\t\tif (ex < 0 || ey < 0 || ex >= 5 || ey >= 5) continue;\n\t\tState T = S;\n\t\tswap(T.ball[px][py], T.ball[ex][ey]);\n\t\tint ret = dfs(rem - 1, ex, ey, T, Score);\n\t\tReturn = max(Return, ret);\n\t}\n\n\t// Return\n\treturn Return;\n}\n\nint main() {\n\twhile (true) {\n\t\tint N;\n\t\tState S;\n\t\tvector<int> Score(6, 0);\n\n\t\t// Step 1. Input\n\t\tcin >> N; if (N == -1) break;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) cin >> S.ball[i][j];\n\t\t}\n\t\tfor (int i = 1; i <= 5; i++) cin >> Score[i];\n\n\t\t// Step 2. Brute Force\n\t\tint Answer = 0;\n\t\tfor (int i = 0; i < 5; i++) {\n\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\tint ret = dfs(N, i, j, S, Score);\n\t\t\t\tAnswer = max(Answer, ret);\n\t\t\t}\n\t\t}\n\n\t\t// Step 3. Output\n\t\tcout << Answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.7142857142857143, "time_ms": 50, "memory_kb": 3424, "score_of_the_acc": -0.1352, "final_rank": 18 }, { "submission_id": "aoj_1512_3856653", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <tuple>\n#include <bitset>\n#include <memory>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <iomanip>\n#include <numeric>\n#include <climits>\n#include <cfloat>\n\ntypedef std::array<std::array<char, 5>, 5> State;\nstruct Coordinate {\n\tint x, y;\n\tCoordinate operator+(const Coordinate a) const {\n\t\treturn { x + a.x, y + a.y };\n\t}\n};\n\nconstexpr std::array<Coordinate, 4> neighbor = { Coordinate{1, 0}, Coordinate{-1,0}, Coordinate{0,1}, Coordinate{0,-1} };\nvoid drop(State& state) {\n\tfor (auto j = 0; j < 5; ++j) {\n\t\tint count = 0;\n\t\tfor (auto i = 4; i >= 0; --i) {\n\t\t\tif (state[i][j] == -1) ++count;\n\t\t\telse if (count != 0) {\n\t\t\t\tstate[i + count][j] = state[i][j];\n\t\t\t\tstate[i][j] = -1;\n\t\t\t}\n\t\t}\n\t}\n}\nstd::array<char, 5> erase_block(State & state) {\n\tstd::stack<Coordinate> horizontal, vertical;\n\tfor (auto i = 0; i < 5; ++i) {\n\t\tfor (auto j = 0; j < 3; ++j) {\n\t\t\tif (state[i][j] != -1) {\n\t\t\t\tauto is_seq{ true };\n\t\t\t\tfor (auto c = 1; c < 3; ++c) {\n\t\t\t\t\tif (state[i][j] != state[i][j + c]) is_seq = false;\n\t\t\t\t}\n\t\t\t\tif (is_seq) horizontal.push({ j, i });\n\t\t\t}\n\t\t\tif (state[j][i] != -1) {\n\t\t\t\tauto is_seq{ true };\n\t\t\t\tfor (auto c = 1; c < 3; ++c) {\n\t\t\t\t\tif (state[j][i] != state[j + c][i]) is_seq = false;\n\t\t\t\t}\n\t\t\t\tif (is_seq) vertical.push({ i, j });\n\t\t\t}\n\t\t}\n\t}\n\tstd::array<char, 5> result{ 0,0,0,0,0 };\n\twhile (!horizontal.empty()) {\n\t\tauto top = horizontal.top(); horizontal.pop();\n\t\tfor (auto c = 0; c < 3; ++c) {\n\t\t\tif (state[top.y][top.x + c] != -1) result[state[top.y][top.x + c] - 1]++;\n\t\t\tstate[top.y][top.x + c] = -1;\n\t\t}\n\t}\n\twhile (!vertical.empty()) {\n\t\tauto top = vertical.top(); vertical.pop();\n\t\tfor (auto c = 0; c < 3; ++c) {\n\t\t\tif (state[top.y + c][top.x] != -1) result[state[top.y + c][top.x] - 1]++;\n\t\t\tstate[top.y + c][top.x] = -1;\n\t\t}\n\t}\n\treturn result;\n}\nbool is_valid(Coordinate position, int order, const int length) {\n\tauto next = position;\n\tfor (auto i = 0; i < length; ++i) {\n\t\tnext = next + neighbor[order % 4];\n\t\tif (next.y < 0 || next.y >= 5 || next.x < 0 || next.x >= 5) return false;\n\t\torder /= 4;\n\t}\n\treturn true;\n}\nState swap(const State& state, Coordinate position, int order, const int length) {\n\tauto copy = state;\n\tfor (auto i = 0; i < length; ++i) {\n\t\tauto next = position + neighbor[order % 4];\n\t\tauto temp = copy[position.y][position.x];\n\t\tcopy[position.y][position.x] = copy[next.y][next.x];\n\t\tcopy[next.y][next.x] = temp;\n\t\tposition = next;\n\t\torder /= 4;\n\t}\n\treturn copy;\n}\n\nint main() {\n\twhile (true) {\n\t\tint n; std::cin >> n; if (n == -1) break;\n\t\tState state; for (auto& line : state) for (auto& a : line) {\n\t\t\tstd::cin >> a;\n\t\t\ta -= '0';\n\t\t}\n\t\tstd::array<int, 5> score; for (auto& s : score) std::cin >> s;\n\t\tint max_score = 0;\n\t\tfor (auto length = 0; length <= n; ++length) {\n\t\t\tfor (auto order = 0; order < (1 << (length * 2)); ++order) {\n\t\t\t\tfor (auto y = 0; y < 5; ++y) for (auto x = 0; x < 5; ++x) if (is_valid({ x, y }, order, length)) {\n\t\t\t\t\tauto s = swap(state, { x, y }, order, length);\n\t\t\t\t\tint sum_score{ 0 };\n\t\t\t\t\tint bonus{ 1 };\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\tauto erased = erase_block(s);\n\t\t\t\t\t\tif (std::count(erased.begin(), erased.end(), 0) == 5) break;\n\t\t\t\t\t\tfor (auto i = 0; i < 5; ++i) {\n\t\t\t\t\t\t\tsum_score += score[i] * erased[i] * bonus;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t++bonus;\n\t\t\t\t\t\tdrop(s);\n\t\t\t\t\t}\n\t\t\t\t\tmax_score = std::max(max_score, sum_score);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstd::cout << max_score << std::endl;\n\n\t}\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3120, "score_of_the_acc": -0.1445, "final_rank": 8 }, { "submission_id": "aoj_1512_2886785", "code_snippet": "#include <vector>\n#include <set>\n#include <iostream>\n#include <cstdio>\n#include <queue>\nusing namespace std;\n\nusing vector2D = vector< vector<int> >;\nconst int B = 5;\nint N;\n\nint dx[] = {1, -1, 0, 0};\nint dy[] = {0, 0, 1, -1};\n\nstruct State {\n vector2D board;\n int x, y;\n};\n\nvector<int> score;\nlong long int solve(vector2D board) {\n long long int ret = 0, bonus = 1;\n for(; ; bonus++) {\n bool update = false;\n vector2D checked(B, vector<int>(B));\n \n for(int i=0; i<B; i++) {\n for(int j=0; j<B; j++) {\n // empty\n if(board[i][j] == 0) continue;\n\n vector<int> dirs = {2, 0};\n for(auto k : dirs) {\n // right or down\n int cons = 0;\n int nx = i, ny = j;\n while(nx < B && ny < B) {\n if(board[nx][ny] != board[i][j]) break;\n cons++;\n nx += dx[k], ny += dy[k];\n }\n if(cons >= 3) {\n update = true;\n nx = i, ny = j;\n while(nx < B && ny < B) {\n if(board[nx][ny] != board[i][j]) break;\n checked[nx][ny] = 1;\n nx += dx[k], ny += dy[k];\n }\n }\n }\n }\n }\n // printf(\"bonus = %lld, update = %d\\n\", bonus, update);\n if(!update) break;\n\n for(int i=0; i<B; i++) {\n for(int j=0; j<B; j++) {\n // empty or no change\n if(board[i][j] == 0 || checked[i][j] == false) continue;\n\n int col = board[i][j] - 1;\n board[i][j] = 0;\n ret += bonus * score[col];\n }\n }\n\n // blockfall\n for(int i=B-1; i>=0; i--) {\n for(int j=0; j<B; j++) {\n int row = i;\n for(; row < B-1 && board[row+1][j] == 0; row++) {\n swap(board[row][j], board[row+1][j]);\n }\n }\n }\n }\n \n return ret;\n}\n\nint main() {\n while(cin >> N, N >= 0) {\n vector2D board(B, vector<int>(B));\n for(int i=0; i<B; i++) {\n for(int j=0; j<B; j++) {\n cin >> board[i][j];\n }\n }\n score.clear();\n for(int i=0; i<B; i++) {\n int val; cin >> val;\n score.push_back(val);\n }\n\n set<vector2D> cand;\n cand.insert(board);\n queue<State> que;\n for(int i=0; i<B; i++) {\n for(int j=0; j<B; j++) {\n que.push(State{board, i, j});\n }\n }\n\n // printf(\"enumerate begin\\n\");\n for(int i=0; i<N; i++) {\n queue<State> nxt;\n while(que.size()) {\n State cur = que.front(); que.pop();\n vector2D board = cur.board;\n int x = cur.x, y = cur.y;\n\n for(int k=0; k<4; k++) {\n int nx = x + dx[k], ny = y + dy[k];\n if(nx < 0 || nx >= B || ny < 0 || ny >= B) continue;\n swap(board[x][y], board[nx][ny]);\n nxt.push(State{board, nx, ny});\n cand.insert(board);\n swap(board[x][y], board[nx][ny]);\n }\n }\n swap(nxt, que);\n }\n // printf(\"enumerate end\\n\");\n\n long long int ans = 0;\n for(auto b : cand) {\n ans = max(ans, solve(b));\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 7488, "score_of_the_acc": -1.2549, "final_rank": 14 }, { "submission_id": "aoj_1512_2886604", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int H = 5, W = 5;\nint GetID(int x, int y) { return y * W + x; }\nconst int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};\n\nint n, a[25], score[6];\n\nbool fall(int b[]) {\n\tbool ret = false;\n\tfor (int x=0; x<5; ++x) {\n\t\tfor (int y=3; y>=0; --y) {\n\t\t\tif (b[GetID(x, y)] != -1) {\n\t\t\t\tint cy = y;\n\t\t\t\twhile (cy < 4 && b[GetID(x, cy+1)] == -1) {\n\t\t\t\t\tret = true;\n\t\t\t\t\tswap(b[GetID(x, cy)], b[GetID(x, cy+1)]);\n\t\t\t\t\t++cy;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\nint vanish(int b[], int bonus) {\n\tbool flag[25];\n\tmemset(flag, false, sizeof(flag));\n\tfor (int y=0; y<5; ++y) for (int x=0; x<5; ++x) {\n\t\tif (y > 0 && y < 4 && b[GetID(x, y-1)] == b[GetID(x, y)] && b[GetID(x, y)] == b[GetID(x, y+1)]) {\n\t\t\tflag[GetID(x, y-1)] = flag[GetID(x, y)] = flag[GetID(x, y+1)] = true;\n\t\t}\n\t\tif (x > 0 && x < 4 && b[GetID(x-1, y)] == b[GetID(x, y)] && b[GetID(x, y)] == b[GetID(x+1, y)]) {\n\t\t\tflag[GetID(x-1, y)] = flag[GetID(x, y)] = flag[GetID(x+1, y)] = true;\n\t\t}\n\t}\n\t\n\tint ret = 0;\n\tfor (int y=0; y<5; ++y) for (int x=0; x<5; ++x) {\n\t\tif (flag[GetID(x, y)]) {\n\t\t\tret += score[b[GetID(x, y)]] * bonus;\n\t\t\tb[GetID(x, y)] = -1;\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid print(int b[]) {\n\tfor (int y=0; y<5; ++y) for (int x=0; x<5; ++x) cerr << b[GetID(x,y)] << (x<4 ? \" \" : \"\\n\");\n\tcerr << \"-----\" << endl;\n}\n\nint calcScore() {\n\tint b[25], ret = 0, bonus = 1;\n\tfor (int i=0; i<25; ++i) b[i] = a[i];\n\tdo {\n\t\tret += vanish(b, bonus);\n\t\t++bonus;\n\t\t//print(b);\n\t} while (fall(b));\n\treturn ret;\n}\n\nint rec(int x, int y) {\n\tif (n == 0) return calcScore();\n\tint ret = calcScore();\n\tfor (int d=0; d<4; ++d) {\n\t\tint nx = x + dx[d], ny = y + dy[d];\n\t\tif (nx < 0 || nx >= 5 || ny < 0 || ny >= 5) continue;\n\t\t--n;\n\t\tswap(a[GetID(x, y)], a[GetID(nx, ny)]);\n\t\tret = max(ret, rec(nx, ny));\n\t\t++n;\n\t\tswap(a[GetID(x, y)], a[GetID(nx, ny)]);\n\t}\n\treturn ret;\n}\n\nint main() {\n\twhile (cin >> n, n != -1) {\n\t\tfor (int y=0; y<5; ++y) for (int x=0; x<5; ++x) cin >> a[GetID(x, y)];\n\t\tfor (int i=0; i<5; ++i) cin >> score[i+1];\n\t\tint ans = 0;\n\t\tfor (int y=0; y<5; ++y) for (int x=0; x<5; ++x) ans = max(ans, rec(x, y));\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3116, "score_of_the_acc": -0.0064, "final_rank": 1 }, { "submission_id": "aoj_1512_2780292", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nconst int dx[4]={1,0,-1,0};\nconst int dy[4]={0,1,0,-1};\nint n,score[5],result,r0,bonus;\nvector <vector<int> > a(5,vector<int>(5)),check(5,vector<int>(5));\n\nbool in(int y,int x){\n if(-1<y&&y<5 && -1<x&&x<5) return 1;\n else return 0;\n}\n\nbool sc(){\n vector <vector<int> > t0(5,vector<int>(5,0));\n for(int i=0;i<5;i++){\n int j=0;\n for(;j<5;){\n int pj=j;\n for(;j+1<5&&check[i][j]==check[i][j+1];) j++;\n if(j+1-pj>=3) for(;pj<=j;pj++) t0[i][pj]=1;\n j++;\n }\n }\n for(int j=0;j<5;j++){\n int i=0;\n for(;i<5;){\n int pi=i;\n for(;i+1<5&&check[i][j]==check[i+1][j];) i++;\n if(i+1-pi>=3) for(;pi<=i;pi++) t0[pi][j]=1;\n i++;\n }\n }\n int flag=0;\n for(int i=0;i<5;i++) for(int j=0;j<5;j++){\n if(t0[i][j]&&check[i][j]){\n\tr0+=t0[i][j]*bonus*score[check[i][j]-1];\n\tcheck[i][j]=0,flag++;\n }\n }\n \n for(int j=0;j<5;j++){\n int ni=5;\n for(int i=4;i>-1;i--){\n if(check[i][j]) check[--ni][j]=check[i][j];\n }\n for(int i=0;i<ni;i++) check[i][j]=0;\n }\n // cout<<r0<<endl;\n \n return flag;\n\n}\n\nvoid dfs(int sw,int y,int x,int d){\n\n // cout<<sw<<\" \"<<y<<\" \"<<x<<\" \"<<d<<endl;\n \n if(sw>n) return;\n check=a;\n r0=0;\n bonus=1;\n while(sc()) bonus++; \n result=max(result,r0);\n \n for(int k=0;k<4;k++){\n if(~d&&(d+2)%4==k) continue;\n int ny=y+dy[k],nx=x+dx[k];\n if(!in(ny,nx)) continue;\n\n swap(a[y][x],a[ny][nx]);\n dfs(sw+1,ny,nx,k);\n swap(a[y][x],a[ny][nx]);\n }\n \n}\n\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n\n for(;cin>>n;){\n if(n==-1) break;\n result=0;\n for(int i=0;i<5;i++) for(int j=0;j<5;j++) cin>>a[i][j];\n for(int i=0;i<5;i++) cin>>score[i];\n \n for(int i=0;i<5;i++) for(int j=0;j<5;j++) dfs(0,i,j,-1);\n \n cout<<result<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3148, "score_of_the_acc": -0.0332, "final_rank": 2 }, { "submission_id": "aoj_1512_2780085", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n#define double long double\nusing namespace std;\nconst int N = 100010;\nconst int INF = 1LL<<55;\nconst int mod = (1e9)+7;\nconst double EPS = 1e-8;\nconst double PI = 6.0 * asin(0.5);\ntypedef pair<int,int> P;\ntypedef long long ll;\nostream& operator<<(ostream& o,P p){\n return o<<\"(\"<<p.first<<\",\"<<p.second<<\")\";\n}\nistream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}\ntemplate<class T> istream& operator>>(istream& i,vector<T> &a){for(auto &t:a)cin>>t;return i;}\ntemplate<class T> T Max(T &a,T b){return a=max(a,b);}\ntemplate<class T> T Min(T &a,T b){return a=min(a,b);}\ntemplate<class T> void prArr(T a,string s=\" \"){int i=0;for(auto t:a)cout<<(i++?s:\"\")<<t;cout<<endl;}\n\n\nint dx[] = {0,0,1,-1};\nint dy[] = {1,-1,0,0};\nvector<int> score;\n\nint calcScore(const vector<vector<int> > &mp, int bonus){\n \n auto okw=[&](int i,int j){\n if(j + 2 >= 5) return false;\n if(mp[i][j] == -1) return false;\n return mp[i][j] == mp[i][j+1] && mp[i][j] == mp[i][j+2];\n };\n \n auto okh=[&](int i,int j){\n if(i + 2 >= 5) return false;\n if(mp[i][j] == -1) return false;\n return mp[i][j] == mp[i+1][j] && mp[i][j] == mp[i+2][j];\n };\n \n int flag = 0;\n vector<vector<int> > res = mp;\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++){\n if(okw(i,j)) res[i][j] = res[i][j+1] = res[i][j+2] = -2;\n if(okh(i,j)) res[i][j] = res[i+1][j] = res[i+2][j] = -2;\n }\n \n int block = 0;\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++)\n if(res[i][j] == -2){\n\tassert(mp[i][j] >= 0);\n\tflag = 1;\n\tres[i][j] = -1;\n\tblock += score[mp[i][j] - 1];\n }\n \n for(int j=0;j<5;j++)\n for(int k=0;k<5;k++)\n for(int i = 0; i < 5-1; i++)\n\tif(res[i][j] == -1) swap(res[i][j],res[i+1][j]);\n\n int ans = bonus * block;\n // if(bonus == 2) cout<<\"2=\"<<ans<<endl;\n if(flag) ans += calcScore(res,bonus+1);\n return ans;\n}\n\nint dfs(int i,int j,int n,vector<vector<int> > mp){\n \n \n if(n == 0) return calcScore(mp,1);\n \n\n int res = calcScore(mp,1);\n for(int k=0;k<4;k++){\n int ni = i + dy[k];\n int nj = j + dx[k];\n if(ni < 0 || nj < 0 || ni >= 5 || nj >= 5) continue;\n swap(mp[i][j], mp[ni][nj]);\n res = max(res,dfs(ni,nj,n-1,mp));\n swap(mp[i][j], mp[ni][nj]);\n }\n return res;\n}\n\nsigned main(){\n while(1){\n int n;\n cin>>n;\n if(n == -1) break;\n vector<vector<int> > A(5,vector<int>(5));\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++) cin>>A[4-i][j];\n\n score.clear();\n score.resize(5);\n for(int i=0;i<5;i++) cin>>score[i];\n \n int ans = -INF;\n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++) Max(ans,dfs(i,j,n,A));\n \n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3176, "score_of_the_acc": -0.1573, "final_rank": 9 }, { "submission_id": "aoj_1512_2780066", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, ans, point[5];\n\nvoid sim(vector<vector<int> > A){\n \n int sum = 0, x = 1, update = 1;\n \n int used[5][5];\n \n while(update){\n \n update = 0;\n \n memset( used, 0, sizeof(used) );\n \n for(int i=0;i<5;i++){\n \n for(int j=1;j<4;j++){\n\t\n\tif( A[i][j] == 0 ) continue;\n\t\n\tif( A[i][j-1] == A[i][j] && A[i][j] == A[i][j+1] ){\n\t used[i][j-1] = 1;\n\t used[i][j] = 1;\n\t used[i][j+1] = 1;\n\t}\n\t\t\n }\n\n }\n\n for(int i=1;i<4;i++){\n\n for(int j=0;j<5;j++){\n\t\n\tif( A[i][j] == 0 ) continue;\n\t\n \tif( A[i-1][j] == A[i][j] && A[i][j] == A[i+1][j] ){\n\t used[i-1][j] = 1;\n\t used[i][j] = 1;\n\t used[i+1][j] = 1;\n\t}\n\t\n }\n \n }\n\n int cnt[5] = {};\n \n for(int i=0;i<5;i++){\n \n for(int j=0;j<5;j++){\n\t\n\tif( used[i][j] ){\n\t cnt[A[i][j]-1]++;\n\t A[i][j] = 0;\n\t update = 1;\n\t}\n\t\n }\n \n }\n /* \n for(int i=0;i<5;i++){\n for(int j=0;j<5;j++) cout<<A[i][j]<<' ';\n cout<<endl;\n }\n */\n \n int update2 = 1;\n\n while(update2){\n \n update2 = 0;\n \n for(int i=0;i<5;i++){\n\t\n\tfor(int j=4;j>=1;j--){\n\t \n\t if( A[j][i] == 0 && A[j-1][i] != 0 ){\n\t \n\t swap( A[j][i], A[j-1][i] );\n\t \n\t update2 = 1;\n\t \n\t }\n\t \n\t}\n\t\n }\n\n }\n \n for(int i=0;i<5;i++){\n sum += x * cnt[i] * point[i];\n }\n \n x++;\n \n }\n \n ans = max( ans, sum );\n \n}\n\nint dy[4] = {-1,0,1,0};\nint dx[4] = {0,1,0,-1};\n\nvoid dfs(int y, int x, vector<vector<int> > A, int cnt){\n \n if( n < cnt ) return;\n \n sim(A);\n \n for(int i=0;i<4;i++){\n \n int ny = y + dy[i], nx = x + dx[i];\n\n if( ny < 0 || nx < 0 || 5 <= ny || 5 <= nx ) continue;\n \n swap( A[y][x], A[ny][nx] );\n \n dfs( ny, nx, A, cnt + 1 );\n\n swap( A[y][x], A[ny][nx] );\n \n }\n \n}\n\nint main(){\n \n while(1){\n \n cin>>n;\n \n if( n == -1 ) break;\n \n vector<vector<int> > A( 5, vector<int>(5) );\n \n for(int i=0;i<5;i++)\n for(int j=0;j<5;j++) cin>>A[i][j];\n \n for(int i=0;i<5;i++) cin>>point[i];\n \n ans = 0;\n \n for(int i=0;i<5;i++){\n \n for(int j=0;j<5;j++){\n\t\n\tdfs( i, j, A, 0 );\n\t\n }\n \n }\n \n cout << ans << endl;\n \n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3128, "score_of_the_acc": -0.2052, "final_rank": 10 }, { "submission_id": "aoj_1512_2659550", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tint table[5][5],rest;\n};\n\nint N,ans;\nint diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0},score[6];\n\nbool rangeCheck(int row,int col){\n\tif(row >= 0 && row <= 4 && col >= 0 && col <= 4)return true;\n\telse{\n\t\treturn false;\n\t}\n}\n\nint calc(int table[5][5]){\n\n\tint ret = 0;\n\n\tint work[5][5],delete_num[6];\n\tbool delete_table[5][5];\n\n\tfor(int row = 0; row < 5; row++){\n\t\tfor(int col = 0; col < 5; col++)work[row][col] = table[row][col];\n\t}\n\n\tint bonus = 1,delete_sum,tmp,count,tmp_row,tmp_col;\n\n\twhile(true){\n\n\t\tdelete_sum = 0;\n\t\tfor(int i = 1; i <= 5; i++)delete_num[i] = 0;\n\n\t\tfor(int row = 0; row < 5; row++){\n\t\t\tfor(int col = 0; col < 5; col++)delete_table[row][col] = false;\n\t\t}\n\n\t\tfor(int row = 0; row <= 4; row++){\n\t\t\tfor(int col = 0; col <= 2; col++){\n\t\t\t\tif(delete_table[row][col] == true || work[row][col] == 0)continue;\n\n\t\t\t\ttmp_col = col;\n\t\t\t\tfor(count = 0; tmp_col <= 4 && work[row][col] == work[row][tmp_col]; count++,tmp_col++);\n\t\t\t\tif(count >= 3){\n\t\t\t\t\tfor(int i = 0; i < count; i++){\n\t\t\t\t\t\tdelete_table[row][col+i] = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int col = 0; col <= 4; col++){\n\t\t\tfor(int row = 0; row <= 2; row++){\n\t\t\t\tif(delete_table[row][col] == true || work[row][col] == 0)continue;\n\n\t\t\t\ttmp_row = row;\n\t\t\t\tfor(count = 0; tmp_row <= 4 && work[row][col] == work[tmp_row][col]; count++,tmp_row++);\n\t\t\t\tif(count >= 3){\n\t\t\t\t\tfor(int i = 0; i < count; i++){\n\t\t\t\t\t\tdelete_table[row+i][col] = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int row = 0; row < 5; row++){\n\t\t\tfor(int col = 0; col < 5; col++){\n\t\t\t\tif(delete_table[row][col]){\n\t\t\t\t\tdelete_num[work[row][col]]++;\n\t\t\t\t\tdelete_sum++;\n\t\t\t\t\twork[row][col] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int col = 0; col < 5; col++){\n\t\t\tfor(int row = 4; row >= 1; row--){\n\t\t\t\tif(work[row][col] == 0){\n\t\t\t\t\tfor(tmp = row-1; tmp >= 0; tmp--){\n\t\t\t\t\t\tif(work[tmp][col] != 0)break;\n\t\t\t\t\t}\n\t\t\t\t\tif(tmp == -1)break;\n\n\t\t\t\t\twork[row][col] = work[tmp][col];\n\t\t\t\t\twork[tmp][col] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif(delete_sum == 0)break;\n\n\t\tfor(int i = 1; i <= 5; i++){\n\t\t\tret += delete_num[i]*score[i]*bonus;\n\t\t}\n\n\t\tbonus++;\n\t}\n\n\treturn ret;\n}\n\n\nvoid recursive(Info info,int row,int col){\n\n\tans = max(ans,calc(info.table));\n\n\tif(info.rest == 0)return;\n\n\tint adj_row,adj_col;\n\tfor(int i = 0; i < 4; i++){\n\t\tadj_row = row+diff_row[i];\n\t\tadj_col = col+diff_col[i];\n\n\t\tif(!rangeCheck(adj_row,adj_col))continue;\n\n\t\tInfo new_info;\n\t\tfor(int row = 0; row < 5; row++){\n\t\t\tfor(int col = 0; col < 5; col++)new_info.table[row][col] = info.table[row][col];\n\t\t}\n\t\tswap(new_info.table[adj_row][adj_col],new_info.table[row][col]);\n\t\tnew_info.rest = info.rest-1;\n\t\trecursive(new_info,adj_row,adj_col);\n\t}\n}\n\n\nvoid func(){\n\n\tInfo first;\n\tfor(int row = 0; row < 5; row++){\n\t\tfor(int col = 0; col < 5; col++)scanf(\"%d\",&first.table[row][col]);\n\t}\n\n\tfor(int i = 1; i <= 5; i++)scanf(\"%d\",&score[i]);\n\n\tfirst.rest = N;\n\n\tans = 0;\n\n\tfor(int row = 0; row < 5; row++){\n\t\tfor(int col = 0; col < 5; col++)recursive(first,row,col);\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d\",&N);\n\t\tif(N == -1)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3192, "score_of_the_acc": -0.0629, "final_rank": 4 }, { "submission_id": "aoj_1512_2172646", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\ntypedef pair<int, bool>P;\n \nint s[5], dx[]{ 1,-1,0,0, }, dy[]{ 0,0,1,-1 };\nvector<vector<int>>a(5, vector<int>(5));\nint Max,n;\nvoid dfs(int x, int y, int c) {\n\tif(c==0||n%2!=0){\n vector<vector<P>>b(5, vector<P>(5));\n rep(i, 5)rep(j, 5)b[i][j].first = a[i][j];\n bool update = true;\n int p = 0;\n for (int v = 1; update; v++) {\n update = false;\n rep(i, 5) {\n int back = 0, cnt = 0;\n rep(j, b[i].size()) {\n if (back != b[i][j].first) {\n if (cnt >= 3) {\n for (int k = 1; k <= cnt; k++)\n b[i][j - k].second = 1;\n }\n cnt = 0;\n }\n back = b[i][j].first; cnt++;\n }\n if (cnt >= 3) {\n for (int k = 1; k <= cnt; k++)\n b[i][b[i].size() - k].second = 1;\n }\n }\n rep(i, 5) {\n int back = 0, cnt = 0;\n rep(j, 5) {\n if (b[j].size() <= i || back != b[j][i].first) {\n if (cnt >= 3) {\n for (int k = 1; k <= cnt; k++)\n b[j - k][i].second = 1;\n }\n cnt = 0;\n }\n if (i < b[j].size()) {\n back = b[j][i].first; cnt++;\n }\n }\n if (cnt >= 3) {\n for (int k = 1; k <= cnt; k++)b[5 - k][i].second = 1;\n }\n }\n rep(i, 5)rep(j, b[i].size()) {\n if (b[i][j].second == 1) {\n p += v*s[b[i][j].first];\n b[i].erase(b[i].begin() + j); update = true; j--;\n }\n }\n }\n Max = max(Max, p);}\n if (c == 0)return;\n rep(i, 4) {\n int nx = x + dx[i], ny = y + dy[i];\n if (0 <= nx&&nx < 5 && 0 <= ny&&ny < 5) {\n swap(a[x][y], a[nx][ny]);\n dfs(nx, ny, c - 1);\n swap(a[x][y], a[nx][ny]);\n }\n }\n}\nint main() {\n while (scanf(\"%d\", &n), ~n) {\n Max = 0;\n rep(i, 5)rep(j, 5)scanf(\"%d\", &a[j][i]), a[j][i]--;\n rep(i, 5)reverse(a[i].begin(), a[i].end());\n rep(i, 5)scanf(\"%d\", &s[i]);\n rep(i, 5)rep(j, 5)dfs(i, j, n);\n printf(\"%d\\n\", Max);\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3284, "score_of_the_acc": -0.123, "final_rank": 7 }, { "submission_id": "aoj_1512_2172641", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nstruct State { int x[5][5]; };\nint n, b[6];\npair<State, int>del(State S) {\n\tbool used[5][5]; for (int i = 0; i < 25; i++)used[i / 5][i % 5] = false;\n\tfor (int i = 0; i < 5; i++) {\n\t\tfor (int j = 0; j < 3; j++) {\n\t\t\tif (S.x[i][j] == S.x[i][j + 1] && S.x[i][j] == S.x[i][j + 2] && S.x[i][j] >= 1) {\n\t\t\t\tfor (int k = j; k < j + 3; k++)used[i][k] = true;\n\t\t\t}\n\t\t\tif (S.x[j][i] == S.x[j + 1][i] && S.x[j][i] == S.x[j + 2][i] && S.x[j][i] >= 1) {\n\t\t\t\tfor (int k = j; k < j + 3; k++)used[k][i] = true;\n\t\t\t}\n\t\t}\n\t}\n\tint cnt = 0, u = 0;\n\tfor (int i = 0; i < 5; i++) {\n\t\tfor (int j = 0; j < 5; j++) { if (used[i][j] == true) { cnt += b[S.x[i][j]]; S.x[i][j] = 0; u++; } }\n\t}\n\tif (u == 0)cnt = -1;\n\tfor (int i = 0; i < 5; i++) {\n\t\tfor (int j = 0; j < 4; j++) {\n\t\t\tfor (int k = 0; k < 5; k++) {\n\t\t\t\tif (S.x[j][k] >= 1 && S.x[j + 1][k] == 0)swap(S.x[j][k], S.x[j + 1][k]);\n\t\t\t}\n\t\t}\n\t}\n\treturn make_pair(S, cnt);\n}\nint dfs(State S, int depth, int cx, int cy) {\n\tif (depth == n) {\n\t\tint d = 1, c = 0;\n\t\twhile (true) {\n\t\t\tpair<State, int>G = del(S);\n\t\t\tif (G.second == -1)return c;\n\t\t\tc += d*G.second; S = G.first; d++;\n\t\t}\n\t}\n\tint dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }, maxn = 0;\n\tfor (int i = 0; i < 4; i++) {\n\t\tint ex = cx + dx[i], ey = cy + dy[i];\n\t\tif (ex < 0 || ex >= 5 || ey < 0 || ey >= 5)continue;\n\t\tState T = S; swap(T.x[ex][ey], T.x[cx][cy]);\n\t\tmaxn = max(maxn, dfs(T, depth + 1, ex, ey));\n\t}\n\treturn maxn;\n}\nint main() {\n\twhile (true) {\n\t\tcin >> n; if (n == -1)break; State V;\n\t\tfor (int i = 0; i < 25; i++)cin >> V.x[i / 5][i % 5];\n\t\tfor (int i = 1; i <= 5; i++)cin >> b[i];\n\t\tint ret = 0; for (int i = 0; i < 25; i++) { for (int j = 0; j <= n; j++)ret = max(ret, dfs(V, j, i / 5, i % 5)); }\n\t\tcout << ret << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3088, "score_of_the_acc": -0.0784, "final_rank": 5 }, { "submission_id": "aoj_1512_2172640", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nstruct State { int x[5][5]; };\nint n, b[6];\npair<State, int>del(State S) {\n\tbool used[5][5]; for (int i = 0; i < 25; i++)used[i / 5][i % 5] = false;\n\tfor (int i = 0; i < 5; i++) {\n\t\tfor (int j = 0; j < 3; j++) {\n\t\t\tif (S.x[i][j] == S.x[i][j + 1] && S.x[i][j] == S.x[i][j + 2] && S.x[i][j] >= 1) {\n\t\t\t\tfor (int k = j; k < j + 3; k++)used[i][k] = true;\n\t\t\t}\n\t\t\tif (S.x[j][i] == S.x[j + 1][i] && S.x[j][i] == S.x[j + 2][i] && S.x[j][i] >= 1) {\n\t\t\t\tfor (int k = j; k < j + 3; k++)used[k][i] = true;\n\t\t\t}\n\t\t}\n\t}\n\tint cnt = 0, u = 0;\n\tfor (int i = 0; i < 5; i++) {\n\t\tfor (int j = 0; j < 5; j++) { if (used[i][j] == true) { cnt += b[S.x[i][j]]; S.x[i][j] = 0; u++; } }\n\t}\n\tif (u == 0)cnt = -1;\n\tfor (int i = 0; i < 5; i++) {\n\t\tfor (int j = 0; j < 4; j++) {\n\t\t\tfor (int k = 0; k < 5; k++) {\n\t\t\t\tif (S.x[j][k] >= 1 && S.x[j + 1][k] == 0)swap(S.x[j][k], S.x[j + 1][k]);\n\t\t\t}\n\t\t}\n\t}\n\treturn make_pair(S, cnt);\n}\nint dfs(State S, int depth, int cx, int cy) {\n\tif (depth == n) {\n\t\tint d = 1, c = 0;\n\t\tState V1 = S;\n\t\twhile (true) {\n\t\t\tpair<State, int>G = del(S);\n\t\t\tif (c == 69) {\n\t\t\t\tfor (int i = 0; i < 5; i++) {\n\t\t\t\t\tfor (int j = 0; j < 5; j++) {\n\t\t\t\t\t\tcout << V1.x[i][j] << ' ';\n\t\t\t\t\t}\n\t\t\t\t\tcout << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (G.second == -1)return c;\n\t\t\tc += d*G.second; S = G.first; d++;\n\t\t}\n\t}\n\tint dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }, maxn = 0;\n\tfor (int i = 0; i < 4; i++) {\n\t\tint ex = cx + dx[i], ey = cy + dy[i];\n\t\tif (ex < 0 || ex >= 5 || ey < 0 || ey >= 5)continue;\n\t\tState T = S; swap(T.x[ex][ey], T.x[cx][cy]);\n\t\tmaxn = max(maxn, dfs(T, depth + 1, ex, ey));\n\t}\n\treturn maxn;\n}\nint main() {\n\twhile (true) {\n\t\tcin >> n; if (n == -1)break; State V;\n\t\tfor (int i = 0; i < 25; i++)cin >> V.x[i / 5][i % 5];\n\t\tfor (int i = 1; i <= 5; i++)cin >> b[i];\n\t\tint ret = 0; for (int i = 0; i < 25; i++) { for (int j = 0; j <= n; j++)ret = max(ret, dfs(V, j, i / 5, i % 5)); }\n\t\tcout << ret << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.8571428571428571, "time_ms": 50, "memory_kb": 3100, "score_of_the_acc": -0.0616, "final_rank": 15 }, { "submission_id": "aoj_1512_2171619", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\ntypedef pair<int, bool>P;\n\nint s[5], dx[]{ 1,-1,0,0, }, dy[]{ 0,0,1,-1 };\nvector<vector<int>>a(5, vector<int>(5));\nint Max;\nvoid dfs(int x, int y, int c) {\n\tvector<vector<P>>b(5, vector<P>(5));\n\trep(i, 5)rep(j, 5)b[i][j].first = a[i][j];\n\tbool update = true;\n\tint p = 0;\n\tfor (int v = 1; update; v++) {\n\t\tupdate = false;\n\t\trep(i, 5) {\n\t\t\tint back = 0, cnt = 0;\n\t\t\trep(j, b[i].size()) {\n\t\t\t\tif (back != b[i][j].first) {\n\t\t\t\t\tif (cnt >= 3) {\n\t\t\t\t\t\tfor (int k = 1; k <= cnt; k++)\n\t\t\t\t\t\t\tb[i][j - k].second = 1;\n\t\t\t\t\t}\n\t\t\t\t\tcnt = 0;\n\t\t\t\t}\n\t\t\t\tback = b[i][j].first; cnt++;\n\t\t\t}\n\t\t\tif (cnt >= 3) {\n\t\t\t\tfor (int k = 1; k <= cnt; k++)\n\t\t\t\t\tb[i][b[i].size() - k].second = 1;\n\t\t\t}\n\t\t}\n\t\trep(i, 5) {\n\t\t\tint back = 0, cnt = 0;\n\t\t\trep(j, 5) {\n\t\t\t\tif (b[j].size() <= i || back != b[j][i].first) {\n\t\t\t\t\tif (cnt >= 3) {\n\t\t\t\t\t\tfor (int k = 1; k <= cnt; k++)\n\t\t\t\t\t\t\tb[j - k][i].second = 1;\n\t\t\t\t\t}\n\t\t\t\t\tcnt = 0;\n\t\t\t\t}\n\t\t\t\tif (i < b[j].size()) {\n\t\t\t\t\tback = b[j][i].first; cnt++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cnt >= 3) {\n\t\t\t\tfor (int k = 1; k <= cnt; k++)b[5 - k][i].second = 1;\n\t\t\t}\n\t\t}\n\t\trep(i, 5)rep(j, b[i].size()) {\n\t\t\tif (b[i][j].second == 1) {\n\t\t\t\tp += v*s[b[i][j].first];\n\t\t\t\tb[i].erase(b[i].begin() + j); update = true; j--;\n\t\t\t}\n\t\t}\n\t}\n\tMax = max(Max, p);\n\tif (c == 0)return;\n\trep(i, 4) {\n\t\tint nx = x + dx[i], ny = y + dy[i];\n\t\tif (0 <= nx&&nx < 5 && 0 <= ny&&ny < 5) {\n\t\t\tswap(a[x][y], a[nx][ny]);\n\t\t\tdfs(nx, ny, c - 1);\n\t\t\tswap(a[x][y], a[nx][ny]);\n\t\t}\n\t}\n}\nint main() {\n\tint n;\n\twhile (scanf(\"%d\", &n), ~n) {\n\t\tMax = 0;\n\t\trep(i, 5)rep(j, 5)scanf(\"%d\", &a[j][i]), a[j][i]--;\n\t\trep(i, 5)reverse(a[i].begin(), a[i].end());\n\t\trep(i, 5)scanf(\"%d\", &s[i]);\n\t\trep(i, 5)rep(j, 5)dfs(i, j, n);\n\t\tprintf(\"%d\\n\", Max);\n\t}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3288, "score_of_the_acc": -0.1043, "final_rank": 6 }, { "submission_id": "aoj_1512_2138604", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n//// < \"d:\\d_download\\visual studio 2015\\projects\\programing_contest_c++\\debug\\a.txt\" > \"d:\\d_download\\visual studio 2015\\projects\\programing_contest_c++\\debug\\b.txt\"\n\nvector<int>ss(5);\nvoid fall(vector<vector<int>>&field) {\n\tfor (int t = 0; t < 5; ++t) {\n\t\tfor (int y = 4; y > 0; --y) {\n\t\t\tfor (int x = 0; x < 5; ++x) {\n\t\t\t\tif (field[y][x] == 0 && field[y - 1][x]) {\n\t\t\t\t\tswap(field[y][x], field[y - 1][x]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\nint getscore(vector<vector<int>>field) {\n\tint bonus = 1;\n\tint ans = 0;\n\tint num = 10;\n\twhile (num--) {\n\n\t\tvector<vector<int>>del(5, vector<int>(5));\n\t\t{\n\t\t\tfor (int x = 0; x < 5; ++x) {\n\t\t\t\tfor (int u = 0; u < 3; ++u) {\n\t\t\t\t\tint c = field[u][x];\n\t\t\t\t\tif (!c)continue;\n\t\t\t\t\tint num = 0;\n\t\t\t\t\tfor (int y = u; y < 5; ++y) {\n\t\t\t\t\t\tif (field[y][x] == c) {\n\t\t\t\t\t\t\tnum++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (num >= 3) {\n\t\t\t\t\t\tfor (int y = u; y < u + num; ++y) {\n\t\t\t\t\t\t\tdel[y][x] = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int y = 0; y < 5; ++y) {\n\t\t\t\tfor (int l = 0; l < 3; ++l) {\n\t\t\t\t\tint c = field[y][l];\n\t\t\t\t\tif (!c)continue;\n\t\t\t\t\tint num = 0;\n\t\t\t\t\tfor (int x = l; x < 5; ++x) {\n\t\t\t\t\t\tif (field[y][x] == c) {\n\t\t\t\t\t\t\tnum++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (num >= 3) {\n\t\t\t\t\t\tfor (int x = l; x < l + num; ++x) {\n\t\t\t\t\t\t\tdel[y][x] = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int y = 0; y < 5; ++y) {\n\t\t\tfor (int x = 0; x < 5; ++x) {\n\t\t\t\tif (del[y][x]) {\n\t\t\t\t\tans += ss[field[y][x]-1] * bonus;\n\t\t\t\t\tfield[y][x] = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfall(field);\n\n\t\tbonus++;\n\t}\n\treturn ans;\n}\nint dx[4] = { -1,0,1,0 };\nint dy[4] = { 0,1,0,-1 };\nint solve(vector<vector<int>>field, int rest,int x,int y) {\n\tint ans = getscore(field);\n\tif (rest) {\n\t\tfor (int way = 0; way < 4; ++way) {\n\t\t\tint nx = x + dx[way];\n\t\t\tint ny = y + dy[way];\n\t\t\tif (nx >= 0 && nx < 5 && ny >= 0 && ny < 5) {\n\t\t\t\tswap(field[y][x], field[ny][nx]);\n\t\t\t\tans = max(ans, solve(field, rest - 1, nx, ny));\n\t\t\t\tswap(field[y][x], field[ny][nx]);\n\t\t\t}\n\t\t}\n\t}\n\treturn ans;\n}\n\nint main() {\n\twhile (1) {\n\t\tint N; cin >> N;\n\t\tif (N == -1)break;\n\t\tvector<vector<int>>field(5, vector<int>(5));\n\t\tfor (int i = 0; i < 5; ++i) {\n\t\t\tfor (int j = 0; j < 5; ++j) {\n\t\t\t\tint a; cin >> a; field[i][j] = a;\n\t\t\t}\n\t\t}\n\t\tfor (int i = 0; i < 5; ++i) {\n\t\t\tcin >> ss[i];\n\t\t}\n\t\tint ans = 0;\n\t\tfor (int i = 0; i < 5; ++i) {\n\t\t\tfor (int j = 0; j < 5; ++j) {\n\t\t\t\tans = max(ans, solve(field, N, i, j));\n\t\t\t}\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 3184, "score_of_the_acc": -1.0218, "final_rank": 13 }, { "submission_id": "aoj_1512_1868640", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef vector<int> Vec;\ntypedef vector<Vec> Mat;\n\nMat a(5, Vec(5));\nint score[5];\n\nconst int dx[] = {-1, +0, +1, +0};\nconst int dy[] = {+0, -1, +0, +1};\n\nbool inField(int x, int y)\n{\n return (0 <= x && x < 5 && 0 <= y && y < 5);\n}\n\nint erase(Mat &m)\n{\n bool e[5][5] = {{}};\n for (int i = 0; i < 5; i++) {\n for (int j = 0; j < 5; j++) { \n if (m[i][j] == 0) {\n continue;\n }\n int cnt[2] = {1, 1};\n for (int k = 0; k < 4; k++) {\n int ni = i + dy[k];\n int nj = j + dx[k];\n while (inField(ni, nj) &&\n m[i][j] == m[ni][nj]) {\n ni += dy[k];\n nj += dx[k];\n ++cnt[k&1];\n }\n }\n if (cnt[0] >= 3 || cnt[1] >= 3) {\n e[i][j] = 1;\n }\n }\n }\n int point = 0;\n for (int i = 0; i < 5; i++) {\n for (int j = 0; j < 5; j++) {\n if (e[i][j]) {\n point += score[m[i][j]-1];\n m[i][j] = 0;\n }\n }\n }\n return point;\n}\n\nvoid fall(Mat &m)\n{\n for (int j = 0; j < 5; j++) {\n for (int i = 4; i >= 1; i--) {\n if (m[i][j] == 0) { \n for (int k = i-1; k >= 0; k--) {\n if (m[k][j] != 0) {\n swap(m[i][j], m[k][j]);\n break;\n }\n }\n }\n }\n }\n}\n\nint get_point()\n{\n Mat b = a;\n int point = 0, bp = 1;\n bool update = 1;\n \n while (update) {\n update = 0; \n Mat nb = b;\n point += erase(nb) * bp++;\n fall(nb);\n if (nb != b) { \n update = 1;\n b = nb;\n }\n }\n return point;\n}\n\nint dfs(int y, int x, int n, int p) \n{\n int point = get_point();\n if (n > 0) {\n for (int i = 0; i < 4; i++) {\n int nx = x + dx[i];\n int ny = y + dy[i];\n if (inField(nx, ny) && (p == -1 || (p + 2) % 4 != i)) {\n swap(a[ny][nx], a[y][x]);\n point = max(point, dfs(ny, nx, n-1, i));\n swap(a[ny][nx], a[y][x]);\n } \n }\n }\n return point;\n}\n\nint main()\n{\n int n;\n while (cin >> n, n != -1) {\n for (int i = 0; i < 5; i++) {\n for (int j = 0; j < 5; j++) {\n cin >> a[i][j];\n }\n }\n \n for (int i = 0; i < 5; i++) {\n cin >> score[i];\n }\n\n int max_point = 0;\n for (int i = 0; i < 5; i++) {\n for (int j = 0; j < 5; j++) {\n max_point = max(max_point, dfs(i, j, n, -1));\n }\n } \n cout << max_point << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3116, "score_of_the_acc": -0.0456, "final_rank": 3 }, { "submission_id": "aoj_1512_1801113", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <complex>\n#include <queue>\n#include <deque>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <iomanip>\n#include <assert.h>\n#include <array>\n#include <cstdio>\n#include <cstring>\n#include <random>\n#include <functional>\n#include <numeric>\n#include <bitset>\n#include <fstream>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\ntemplate<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }\n\nint dx[4] = {-1,0,1,0};\nint dy[4] = {0,-1,0,1};\ntemplate<class T> constexpr bool in_range(T y, T x, T H, T W) { return 0<=y&&y<H&&0<=x&&x<W; }\n\nint N;\nint score[6];\n\nvoid drop(vector<vector<int>>& a) {\n rep(j, 5) {\n deque<int> d;\n rep(i, 5) {\n if(a[i][j] != -1) {\n d.push_back(a[i][j]);\n }\n }\n\n int dsize = d.size();\n for(int i=4; i>=0 && !d.empty(); i--) {\n a[i][j] = d.back();\n d.pop_back();\n }\n rep(i, 5-dsize) {\n a[i][j] = -1;\n }\n }\n}\n\nint dist(vector<vector<int>> const& a, int num, int i, int j, int dy, int dx, int sum = 0) {\n i += dy, j += dx;\n if(!in_range(i, j, 5, 5)) return sum;\n if(a[i][j] != num) return sum;\n return dist(a, num, i, j, dy, dx, sum + 1);\n}\n\nint simulate(vector<vector<int>>& a) {\n int ret = 0;\n int bonus = 1;\n\n while(1) {\n set<int> used;\n rep(i, 5) rep(j, 5) {\n if(a[i][j] == -1) continue;\n int movel = dist(a, a[i][j], i, j, 0, -1);\n int mover = dist(a, a[i][j], i, j, 0, +1);\n int movet = dist(a, a[i][j], i, j, -1, 0);\n int moveb = dist(a, a[i][j], i, j, +1, 0);\n if(mover - movel + 1 >= 3) {\n REP(x, j - movel, j + mover + 1) {\n assert(a[i][x] != -1);\n assert(a[i][x] == a[i][j]);\n used.insert(i*5+x);\n }\n }\n if(moveb - movet + 1 >= 3) {\n REP(y, i - movet, i + moveb + 1) {\n assert(a[y][j] != -1);\n assert(a[y][j] == a[i][j]);\n used.insert(y*5+j);\n }\n }\n }\n\n for(auto && e: used) {\n ret += score[a[e/5][e%5]] * bonus;\n a[e/5][e%5] = -1;\n }\n\n bonus ++;\n if(!used.size()) break;\n drop(a);\n }\n\n return ret;\n}\n\n\n\nint dfs(vector<vector<int>> a, int y, int x, int idx = 0) {\n auto aa = a;\n int ret = simulate(a);\n if(idx < N) {\n rep(k, 4) {\n int ny = y + dy[k], nx = x + dx[k];\n if(!in_range(ny, nx, 5, 5)) continue;\n a = aa;\n swap(a[y][x], a[ny][nx]);\n maximize(ret, dfs(a, ny, nx, idx + 1));\n }\n }\n return ret;\n}\n\nint solve(vector<vector<int>>& a) {\n int ret = 0;\n rep(i, 5) rep(j, 5) {\n maximize(ret, dfs(a, i, j));\n }\n return ret;\n}\n\nint main() {\n\n for(; cin >> N && N >= 0;) {\n vector<vector<int>> a(5, vector<int>(5));\n rep(i, 5) rep(j, 5) cin >> a[i][j];\n rep(i, 5) cin >> score[i+1];\n cout << solve(a) << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 3156, "score_of_the_acc": -0.4272, "final_rank": 12 } ]
aoj_1514_cpp
Problem F: Ballon Contest Problem 空の様子がいつもと違う。色とりどりの多彩な熱気球が空を覆っていた。今日は熱気球の大会だ。 熱気球から落とされた得点付きボールを参加者全員で取り合うらしい。 せっかくなので優勝者を予想してみることにした。 レクリエーションにはN人参加する。 N 人の参加者はそれぞれ自分の位置が与えられる。複数の参加者に同じ位置が与えられる事はない。 上空の熱気球からM個のボールが1個ずつ落下する。 参加者は全員同じタイミングで走り始め、ボールに向かって同じ速度で一直線に走る。 ボールの落下位置に一番早く辿り着けた人がボールを取得できる。同時に複数人辿り着いた場合は、一様な確率で取得できる人が決まる。 参加者がボールを取得すると参加者全員が元の位置に戻る。 参加者が走り始めてから参加者の全員が元の位置に戻るまでに別のボールが落下する事はない。 各ボールには得点と落下する位置が与えられ、ボールを取得すると得点を得られる。 ボールは落下中に空気抵抗を受けるため、実際に落下する地点にはズレが発生する。落下予定の位置より最大でX軸方向に± dx 、Y軸方向に± dy だけ一様な確率でズレる。 得られる得点の期待値を求め、期待値の最も大きい参加者の期待値を出力せよ。 Input 入力は複数のデータセットからなる。 各データセットは以下で表される。 N M x 1 y 1 . . x N y N bx 1 by 1 dx 1 dy 1 score 1 . . bx M by M dx M dy M score M 1行目には、参加者の人数 N 、ボールの数 M が与えられる。 2行目から N +1行目までには、参加者の情報が与えられる。 x i , y i は、それぞれ参加者の位置のX座標・Y座標である。 N +2行目から N + M +1行目までには、ボールの情報が与えられる。各ボールが実際に落下する地点のX座標・Y座標はそれぞれ bx j - dx j から bx j + dx j まで、 by j - dy j から by j + dy j までの範囲のどこかである。 score j はボールの得点である。 入力の終わりは2つのゼロからなる。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 100 1 ≤ M ≤ 10 0 ≤ x i , y i , bx j , by j ≤ 10000 1 ≤ dx j , dy j ≤ 10000 1 ≤ score j ≤ 100 テストケースの数は 10 を超えない。 入力に含まれる値は全て整数である。 Output 各データセット毎に、答えを一行に出力しなさい。 出力は0.0001以下の誤差を含んでもよい。 Sample Input 3 4 10 75 50 5 90 75 50 50 10 10 2 40 90 1 1 3 10 20 10 15 1 50 70 50 50 4 4 2 25 25 25 75 75 75 75 25 50 50 10 10 1 50 50 15 15 2 1 1 5 5 1 1 1 1 1 0 0 Sample Output 5.442857 0.750000 1.000000
[ { "submission_id": "aoj_1514_9729453", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cmath>\nusing namespace std;\n\n#define EPS 1e-8\n#define INF 1e100\n\ntypedef complex<double> P;\ntypedef vector<P> vP;\n\nconst P iu(0.0, 1.0);\n\ndouble dot(P a, P b){\n\treturn real(a) * real(b) + imag(a) * imag(b);\n}\n\ndouble cross(P a, P b){\n\treturn real(a) * imag(b) - imag(a) * real(b);\n}\n\nint ccw(P a, P b, P c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > EPS)\treturn +1;\t// counter clockwise\n\tif (cross(b, c) < -EPS)\treturn -1;\t// clockwise\n\tif (dot(b, c) < -EPS)\treturn +2;\t// c--a--b on line\n\tif (norm(b) < norm(c))\treturn -2;\t// a--b--c on line\n\treturn 0;\n}\n\nP crosspoint(P a1, P a2, P b1, P b2){\n\tdouble t = cross(b1 - a1, b1 - b2) / cross(a2 - a1, b1 - b2);\n\treturn a1 + t * (a2 - a1);\n}\n\nP centroid(const vP &pl){\n\tP s;\n\tint sz = pl.size() - 1;\n\tif(sz < 1){ return P(INF, INF); }\n\tfor(int i = 1; i <= sz; ++i){\n\t\ts += pl[i];\n\t}\n\treturn s / (double)sz;\n}\n\nvP convex_cut(const vP &pl, P la, P lb) {\n\tvP Q;\n\tfor (int i = 1; i < pl.size(); ++i) {\n\t\tP A = pl[i-1], B = pl[i];\n\t\tif (ccw(la, lb, A) != -1){ Q.push_back(A); }\n\t\tif (ccw(la, lb, A) * ccw(la, lb, B) < 0){\n\t\t\tQ.push_back(crosspoint(A, B, la, lb));\n\t\t}\n\t}\n\tif(!Q.empty() && Q.back() != Q[0]){\n\t\tQ.push_back(Q[0]);\n\t}\n\treturn Q;\n}\n\ndouble area(const vP p){\n\tdouble s = 0.0;\n\tfor(int i = 1; i < p.size(); ++i){\n\t\ts += cross(p[i-1], p[i]);\n\t}\n\treturn 0.5 * abs(s);\n}\n\ndouble solve(int n, int m){\n\tint x, y, dx, dy, scr;\n\tvector<P> pt(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tscanf(\"%d%d\", &x, &y);\n\t\tpt[i] = P(x, y);\n\t}\n\n\tvector<double> sum(n);\n\tfor(int i = 0; i < m; ++i){\n\t\tscanf(\"%d%d%d%d%d\", &x, &y, &dx, &dy, &scr);\n\t\tdouble S0 = dx * dy * 4.0;\n\n\t\tfor(int j = 0; j < n; ++j){\n\t\t\tvP plg(5);\n\t\t\tplg[0] = plg[4] = P(x - dx, y - dy);\n\t\t\tplg[1] = P(x + dx, y - dy);\n\t\t\tplg[2] = P(x + dx, y + dy);\n\t\t\tplg[3] = P(x - dx, y + dy);\n\n\t\t\tfor(int k = 0; k < n; ++k){\n\t\t\t\tif(k != j){\n\t\t\t\t\tP mid = 0.5 * (pt[j] + pt[k]);\n\t\t\t\t\tP dif = iu * (pt[k] - pt[j]);\n\t\t\t\t\t\n\t\t\t\t\tvP p1 = convex_cut(plg, mid, mid + dif);\n\t\t\t\t\tvP p2 = convex_cut(plg, mid, mid - dif);\n\n\t\t\t\t\tif(p1.empty()){ p1.swap(p2); }\n\t\t\t\t\tP cn = centroid(p1);\n\t\t\t\t\tplg.swap(norm(pt[j] - cn) < norm(pt[k] - cn) ? p1 : p2);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tsum[j] += area(plg) / S0 * scr;\n\t\t}\n\t}\n\n\treturn *max_element(sum.begin(), sum.end());\n}\n\nint main(){\n\tint n, m;\n\twhile(scanf(\"%d%d\", &n, &m), n){\n\t\tprintf(\"%f\\n\", solve(n, m));\n\t}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3564, "score_of_the_acc": -1.225, "final_rank": 15 }, { "submission_id": "aoj_1514_4767085", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 105\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nint N,num_ball;\ndouble NUM = 20005;\ndouble E[SIZE];\n\nPoint C[SIZE];\nPolygon L;\n\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPolygon ConvexCut(Polygon g,Point a,Point b){\n\n\tPolygon ret;\n\tint N = g.size();\n\n\tfor(int i = 0; i < g.size(); i++){\n\t\tPoint A = g[i], B = g[(i+1)%N];\n\t\tif(ccw(a,b,A) != -1)ret.push_back(A);\n\t\tif(ccw(a,b,A)*ccw(a,b,B) == -1)ret.push_back(calc_Cross_Point(a,b,A,B));\n\t}\n\n\treturn ret;\n}\n\ndouble calc_S(Polygon g){\n\n\tint N = g.size();\n\tdouble ret = 0;\n\n\tfor(int i = 0; i < g.size(); i++){\n\t\tret += cross(g[i],g[(i+1)%N]);\n\t}\n\treturn ret/2.0;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n/*\n * IN 2\n * ON 1\n * OUT 0\n *\n */\nint contains(Polygon g,Point p){\n\tint n = g.size();\n\tbool x = false;\n\tfor(int i = 0; i < n; i++){\n\t\tPoint a = g[i]-p,b = g[(i+1)%n]-p;\n\t\tif(abs(cross(a,b)) < EPS && dot(a,b) < EPS)return 1;\n\t\tif(a.y > b.y)swap(a,b);\n\t\tif(a.y < EPS && EPS < b.y && cross(a,b) > EPS) x = !x;\n\t}\n\treturn (x ? 2:0);\n}\n\n//他角形g1とg2が交差するかどうか調べる関数\nbool intersect(const Polygon g1,const Polygon g2){\n\n\tint size_1 = g1.size(),size_2 = g2.size();\n\n\tfor(int i = 0; i < size_1; i++){\n\t\tfor(int k = 0; k < size_2; k++){\n\t\t\tif(is_Cross(Line(g1[i],g1[(i+1)%size_1]),Line(g2[k],g2[(k+1)%size_2]))){\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < size_1; i++){\n\t\tif(contains(g2,g1[i]))return true;\n\t}\n\n\tfor(int i = 0; i < size_2; i++){\n\t\tif(contains(g1,g2[i]))return true;\n\t}\n\n\treturn false;\n}\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%lf %lf\",&C[i].x,&C[i].y);\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tE[i] = 0;\n\t}\n\n\tdouble bx,by,dx,dy,score;\n\n\tPolygon work;\n\tdouble base_S,cross_S;\n\n\tfor(int loop = 0; loop < num_ball; loop++){\n\n\t\twork.clear();\n\t\tscanf(\"%lf %lf %lf %lf %lf\",&bx,&by,&dx,&dy,&score);\n\n\t\twork.push_back(Point(bx+dx,by+dy));\n\t\twork.push_back(Point(bx-dx,by+dy));\n\t\twork.push_back(Point(bx-dx,by-dy));\n\t\twork.push_back(Point(bx+dx,by-dy));\n\n\t\tbase_S = calc_S(work);\n\n\t\tfor(int i = 0; i < N; i++){\n\n\t\t\tPolygon p = work;\n\t\t\tfor(int k = 0; k < N; k++){\n\t\t\t\tif(k == i)continue;\n\n\t\t\t\tPoint from = (C[i]+C[k])/2; //中点\n\t\t\t\tVector v1 = C[k]-C[i];\n\t\t\t\tVector v2 = Vector(-v1.y,v1.x); //v1の法線ベクトル\n\t\t\t\tPoint to = from+v2;\n\t\t\t\tp = ConvexCut(p,from,to);\n\t\t\t\tif(p.size() <= 2){\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(p.size() <= 2)continue;\n\t\t\tE[i] += score*calc_S(p)/base_S; //p:人物[i]のボロノイ領域\n\t\t}\n\t}\n\n\tdouble ans = -1;\n\tfor(int i = 0; i < N; i++){\n\n\t\tans = max(ans,E[i]);\n\t}\n\tprintf(\"%.10lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\t\tscanf(\"%d %d\",&N,&num_ball);\n\t\tif(N == 0 && num_ball == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3208, "score_of_the_acc": -1.096, "final_rank": 13 }, { "submission_id": "aoj_1514_2165609", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\ntypedef ld Weight;\nstruct Edge {\n\tint src, dest;\n\tint cap, rev;\n\tWeight weight;\n\tbool operator < (const Edge &rhs) const { return weight > rhs.weight; }\n};\n\n\n/* ??????????????¬ */\n\n#include <complex>\n\ntypedef complex<ld> Point;\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define all(x) (x).begin(),(x).end()\n\n\nconst ld pi = acos(-1.0);\nconst ld dtop = pi / 180.;\nconst ld ptod = 1. / dtop;\n\nnamespace std {\n\tbool operator<(const Point &lhs, const Point &rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\n// ????????\\???\nPoint input_Point() {\n\tld x, y;\n\tcin >> x >> y;\n\treturn Point(x, y);\n}\n\n// ????????????????????????\nbool eq(const ld a, const ld b) {\n\treturn (abs(a - b) < eps);\n}\n\n// ??????\nld dot(const Point& a, const Point& b) {\n\treturn real(conj(a) * b);\n}\n\n// ??????\nld cross(const Point& a, const Point& b) {\n\treturn imag(conj(a) * b);\n}\n\n// ??´????????????\nclass Line {\npublic:\n\tPoint a, b;\n\tLine() : a(Point(0, 0)), b(Point(0, 0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n\tPoint operator[](const int _num)const {\n\t\tif (_num == 0)return a;\n\t\telse if (_num == 1)return b;\n\t\telse {\n\t\t\tassert(false);\n\t\t\treturn Point();\n\t\t}\n\t}\n};\n\n// ????????????\nclass Circle {\npublic:\n\tPoint p;\n\tld r;\n\tCircle() : p(Point(0, 0)), r(0) {}\n\tCircle(Point p, ld r) : p(p), r(r) {}\n};\n\n// ccw\n// 1: a,b,c??????????¨???¨?????????????????¶\n//-1: a,b,c???????¨???¨?????????????????¶\n// 2: c,a,b???????????´???????????¶\n//-2: a,b,c???????????´???????????¶\n// 0: a,c,b???????????´???????????¶\nint ccw(const Point& a, const Point &b, const Point &c) {\n\tconst Point nb(b - a);\n\tconst Point nc(c - a);\n\tif (cross(nb, nc) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶\n\tif (cross(nb, nc) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶\n\tif (dot(nb, nc) < 0) return 2; // c,a,b???????????´???????????¶\n\tif (norm(nb) < norm(nc)) return -2; // a,b,c???????????´???????????¶\n\treturn 0; // a,c,b???????????´???????????¶\n}\n\n\n/* ???????????? */\n\n// ??´?????¨??´??????????????????\nbool isis_ll(const Line& l, const Line& m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// ??´?????¨?????????????????????\nbool isis_ls(const Line& l, const Line& s) {\n\treturn isis_ll(l, s) &&\n\t\t(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// ????????¨?????????????????????\nbool isis_ss(const Line& s, const Line& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// ????????´????????????\nbool isis_lp(const Line& l, const Point& p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\n// ?????????????????????\nbool isis_sp(const Line& s, const Point& p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// ??????????¶?\nPoint proj(const Line &l, const Point& p) {\n\tld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);\n\treturn l.a + t * (l.b - l.a);\n}\n\n//???????±??????????????????????\nPoint reflect(const Line &l, const Point &p) {\n\tPoint pr = proj(l, p);\n\treturn pr * 2.l - p;\n}\n\n// ??´?????¨??´????????????\nPoint is_ll(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tassert(cross(sv, tv) != 0);\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n// ??´?????¨??´????????????\nvector<Point> is_ll2(const Line &s, const Line& t) {\n\tPoint sv = s.b - s.a, tv = t.b - t.a;\n\tif (cross(sv, tv) != 0)return vector<Point>(1, is_ll(s, t));\n\telse {\n\t\tvector<Point>ans;\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tif (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())ans.push_back(t[k]);\n\t\t\tif (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())ans.push_back(s[k]);\n\t\t}\n\t\treturn ans;\n\t}\n}\n// ????????¨???????????????\n//???????????£????????¨???????????¨assert(false)\nPoint is_ss(const Line &s, const Line& t) {\n\tif (isis_ss(s, t)) {\n\t\tfor (int k = 0; k < 2; ++k) {\n\t\t\tfor (int l = 0; l < 2; ++l) {\n\t\t\t\tif (s[k] == t[l])return s[k];\n\t\t\t}\n\t\t}\n\t\treturn is_ll(s, t);\n\t}\n\telse {\n\t\t//??????isis_ss?????????\n\t\tassert(false);\n\t\treturn Point(0, 0);\n\t}\n}\n// ????????¨???????????????\nvector<Point> is_ss2(const Line &s, const Line& t) {\n\tvector<Point> kouho(is_ll2(s, t));\n\tvector<Point>ans;\n\tfor (auto p : kouho) {\n\t\tif (isis_sp(s, p) && isis_sp(t, p))ans.emplace_back(p);\n\t}\n\treturn ans;\n}\n// ??´?????¨???????????¢\nld dist_lp(const Line& l, const Point& p) {\n\treturn abs(p - proj(l, p));\n}\n\n//??´?????¨??´???????????¢\nld dist_ll(const Line& l, const Line& m) {\n\treturn isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// ??´?????¨??????????????¢\nld dist_ls(const Line& l, const Line& s) {\n\treturn isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// ????????¨???????????¢\nld dist_sp(const Line& s, const Point& p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// ????????¨??????????????¢\nld dist_ss(const Line& s, const Line& t) {\n\tif (isis_ss(s, t)) return 0;\n\treturn min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n\n//??´?????¨??´?????????????????????????????????\nLine line_bisection(const Line &s, const Line &t) {\n\tconst Point laglanju(is_ll(s, t));\n\tconst Point avec = !(abs(laglanju - s[0])<eps) ? s[0] - laglanju : s[1] - laglanju;\n\tconst Point bvec = !(abs(laglanju - t[0])<eps) ? t[0] - laglanju : t[1] - laglanju;\n\n\treturn Line(laglanju, laglanju + (abs(bvec)*avec + abs(avec)*bvec) / (abs(avec) + abs(bvec)));\n}\n//?????¨???????????´???????????????a?????????????????????\nLine point_bisection(const Point&a, const Point&b) {\n\tconst Point cen((a + b) / 2.l);\n\tconst Point vec = (b - a)*Point(0, 1);\n\treturn Line(cen, cen + vec);\n}\n\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nPoint inner_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i <static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0])return vertics[0];\n\tLine bi1(line_bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));\n\tLine bi2(line_bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));\n\tif (bi1[0] == bi2[0])return bi1[0];\n\telse {\n\t\treturn is_ll(bi1, bi2);\n\t}\n}\n\n//???????????´?????????????????????\n//???????????´??????????????§???????????¨????¢?????????¨?????????\nvector<Point> ex_center(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tfor (int i = 0; i < static_cast<int>(ls.size()); ++i) {\n\t\tvertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));\n\t}\n\tif (abs(vertics[0] - vertics[1])<eps || abs(vertics[1] - vertics[2])<eps || (abs(vertics[2] - vertics[0])<eps))return vector<Point>();\n\tvector<Point>ecs;\n\tfor (int i = 0; i < 3; ++i) {\n\t\tLine bi1(line_bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3])));\n\t\tLine bi2(line_bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i])));\n\t\tecs.push_back(is_ll(bi1, bi2));\n\t}\n\treturn ecs;\n}\n\n\n//a,b:??????\n//c:????????§??????\n//???????????´?????????????????¢?????????????±??????????\nvector<Point> same_dis(const vector<Line>&ls) {\n\tvector<Point>vertics;\n\tvertics.push_back(is_ll(ls[0], ls[2]));\n\tvertics.push_back(is_ll(ls[1], ls[2]));\n\n\tif (abs(vertics[0] - vertics[1]) < eps)return vector<Point>{vertics[0]};\n\tLine bis(line_bisection(ls[0], ls[1]));\n\tvector<Point>ecs;\n\n\tLine abi(line_bisection(Line(vertics[0], vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, abi));\n\n\n\tLine bbi(line_bisection(Line(vertics[0], 2.l*vertics[0] - vertics[1]), ls[0]));\n\tecs.push_back(is_ll(bis, bbi));\n\n\treturn ecs;\n}\n/* ??? */\n\n// ?????¨????????????\nvector<Point> is_cc(const Circle& c1, const Circle& c2) {\n\tvector<Point> res;\n\tld d = abs(c1.p - c2.p);\n\tld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n\tld dfr = c1.r * c1.r - rc * rc;\n\tif (abs(dfr) < eps) dfr = 0.0;\n\telse if (dfr < 0.0) return res; // no intersection\n\tld rs = sqrt(dfr);\n\tPoint diff = (c2.p - c1.p) / d;\n\tres.push_back(c1.p + diff * Point(rc, rs));\n\tif (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n\treturn res;\n}\n\n//???????????????????????????\n/* 0 => out\n1 => on\n2 => in*/\nint is_in_Circle(const Circle &cir, const Point& p) {\n\tld dis = abs(cir.p - p);\n\tif (dis > cir.r + eps)return 0;\n\telse if (dis < cir.r - eps)return 2;\n\telse return 1;\n}\n//???lc??????rc??????????????????\n/*0 => out\n1 => on\n2 => in*/\nint Circle_in_Circle(const Circle &lc, const Circle&rc) {\n\tld dis = abs(lc.p - rc.p);\n\tif (dis < rc.r - lc.r - eps)return 2;\n\telse if (dis>rc.r - lc.r + eps)return 0;\n\telse return 1;\n}\n\n// ?????¨??´????????????\nvector<Point> is_lc(const Circle& c, const Line& l) {\n\tvector<Point> res;\n\tld d = dist_lp(l, c.p);\n\tif (d < c.r + eps) {\n\t\tld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n\t\tPoint nor = (l.a - l.b) / abs(l.a - l.b);\n\t\tres.push_back(proj(l, c.p) + len * nor);\n\t\tres.push_back(proj(l, c.p) - len * nor);\n\t}\n\treturn res;\n}\n\n// ?????¨??????????????¢\nvector<Point> is_sc(const Circle& c, const Line& l) {\n\tvector<Point> v = is_lc(c, l), res;\n\tfor (Point p : v)\n\t\tif (isis_sp(l, p)) res.push_back(p);\n\treturn res;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cp(const Circle& c, const Point& p) {\n\tvector<Line> ret;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r * c.r);\n\tif (isnan(l)) { return ret; }\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tret.push_back(Line(p, p + v1));\n\tif (l < eps) return ret;\n\tret.push_back(Line(p, p + v2));\n\treturn ret;\n}\n\n// ?????¨????????\\???\nvector<Line> tangent_cc(const Circle& c1, const Circle& c2) {\n\tvector<Line> ret;\n\tif (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n\t\tPoint center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n\t\tret = tangent_cp(c1, center);\n\t}\n\tif (abs(c1.r - c2.r) > eps) {\n\t\tPoint out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n\t\tvector<Line> nret = tangent_cp(c1, out);\n\t\tret.insert(ret.end(), all(nret));\n\t}\n\telse {\n\t\tPoint v = c2.p - c1.p;\n\t\tv /= abs(v);\n\t\tPoint q1 = c1.p + v * Point(0, 1) * c1.r;\n\t\tPoint q2 = c1.p + v * Point(0, -1) * c1.r;\n\t\tret.push_back(Line(q1, q1 + v));\n\t\tret.push_back(Line(q2, q2 + v));\n\t}\n\treturn ret;\n}\n//??????????????????????????¢???\nld two_Circle_area(const Circle&l, const Circle&r) {\n\tld dis = abs(l.p - r.p);\n\tif (dis > l.r + r.r)return 0;\n\telse if (dis + r.r < l.r) {\n\t\treturn r.r*r.r*pi;\n\t}\n\telse if (dis + l.r < r.r) {\n\t\treturn l.r*l.r*pi;\n\t}\n\telse {\n\t\tld ans = (l.r)*(l.r)*acos((dis*dis + l.r*l.r - r.r*r.r) / (2 * dis*l.r)) +\n\t\t\t(r.r)*(r.r)*acos((dis*dis + r.r*r.r - l.r*l.r) / (2 * dis*r.r)) -\n\t\t\tsqrt(4 * dis*dis*l.r*l.r - (dis*dis + l.r*l.r - r.r*r.r)*(dis*dis + l.r*l.r - r.r*r.r)) / 2;\n\t\treturn ans;\n\t}\n\n}\n\n/* ????§???¢ */\n\ntypedef vector<Point> Polygon;\n\n// ??¢???\nld get_area(const Polygon &p) {\n\tld res = 0;\n\tint n = p.size();\n\trep(j, n) res += cross(p[j], p[(j + 1) % n]);\n\treturn res / 2;\n}\n\n//????§???¢????????¢??????\nbool is_counter_clockwise(const Polygon &poly) {\n\tld angle = 0;\n\tint n = poly.size();\n\trep(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n\t\tangle += arg((c - b) / (b - a));\n\t}\n\treturn angle > eps;\n}\n\n// ??????????????????\n/*0 => out\n1 => on\n2 => in*/\nint is_in_Polygon(const Polygon &poly, const Point& p) {\n\tld angle = 0;\n\tint n = poly.size();\n\trep(i, n) {\n\t\tPoint a = poly[i], b = poly[(i + 1) % n];\n\t\tif (isis_sp(Line(a, b), p)) return 1;\n\t\tangle += arg((b - p) / (a - p));\n\t}\n\treturn eq(angle, 0) ? 0 : 2;\n}\n//??????????????????2?????????\nenum { out, on, in };\nint convex_contains(const Polygon &P, const Point &p) {\n\tconst int n = P.size();\n\tPoint g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point\n\tint a = 0, b = n;\n\twhile (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]\n\t\tint c = (a + b) / 2;\n\t\tif (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg\n\t\t\tif (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c;\n\t\t\telse a = c;\n\t\t}\n\t\telse {\n\t\t\tif (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c;\n\t\t\telse b = c;\n\t\t}\n\t}\n\tb %= n;\n\tif (cross(P[a] - p, P[b] - p) < 0) return 0;\n\tif (cross(P[a] - p, P[b] - p) > 0) return 2;\n\treturn 1;\n}\n\n// ??????\n//???????????????????????¨????????????????????§??¨???\nPolygon convex_hull(vector<Point> ps) {\n\tint n = ps.size();\n\tint k = 0;\n\tsort(ps.begin(), ps.end());\n\tPolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = ps[i++])\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n\tch.resize(k - 1);\n\treturn ch;\n}\n\n\n\n//????????????\nvector<Polygon> convex_cut(const Polygon &ps, const Line& l) {\n\tint n = ps.size();\n\tPolygon q;\n\tPolygon r;\n\trep(i, n) {\n\t\tPoint a = ps[i], b = ps[(i + 1) % n];\n\t\tLine m = Line(a, b);\n\t\tif (ccw(l.a, l.b, a) != -1) q.push_back(a);\n\t\tif (ccw(l.a, l.b, a) != 1) r.push_back(a);\n\t\tif (ccw(l.a, l.b, a) * ccw(l.a, l.b, b) < 0 && isis_ll(l, m)) {\n\t\t\tq.push_back(is_ll(l, m));\n\t\t\tr.push_back(is_ll(l, m));\n\t\t}\n\t}\n\tconst vector<Polygon>polys{ q,r };\n\treturn polys;\n}\n\n\n/* ??¢??¬??????????????? */\nvoid add_Point(vector<Point> &ps, const Point p) {\n\tfor (Point q : ps) if (abs(q - p) < eps) return;\n\tps.push_back(p);\n}\n\n\n\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\n\nvoid add_edge(Graph &g, int src, int dest, int cap, Weight weight) {\n\tg[src].push_back(Edge{ src, dest, cap, (int)g[dest].size(), weight });\n\tg[dest].push_back(Edge{ dest, src, 0, (int)g[src].size() - 1, -weight });\n}\n\nGraph segment_arrangement(const vector<Line> &s, const vector<Point> &p) {\n\tint n = p.size(), m = s.size();\n\tGraph g(n);\n\trep(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\trep(j, n) {\n\t\t\tif (isis_sp(s[i], p[j]))\n\t\t\t\tvec.emplace_back(abs(s[i].a - p[j]), j);\n\t\t}\n\t\tsort(all(vec));\n\t\trep(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tadd_edge(g, from, to, 1, static_cast<Weight>(abs(p[from] - p[to])));\n\t\t}\n\t}\n\treturn g;\n}\npair<vector<Point>, Graph> sennbunn_arrangement(const vector<Line>&s) {\n\tvector<Point>crss;\n\tfor (int i = 0; i < static_cast<int>(s.size()); ++i) {\n\t\tfor (int j = i + 1; j < static_cast<int>(s.size()); ++j) {\n\t\t\tif (isis_ss(s[i], s[j])) {\n\t\t\t\tcrss.push_back(is_ll2(s[i], s[j])[0]);\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i <static_cast<int>(s.size()); ++i) {\n\t\tcrss.push_back(s[i][0]);\n\t\tcrss.push_back(s[i][1]);\n\t}\n\tsort(crss.begin(), crss.end());\n\tcrss.erase(unique(crss.begin(), crss.end()), crss.end());\n\n\treturn make_pair(crss, segment_arrangement(s, crss));\n}\n\nGraph Circle_arrangement(const vector<Circle> &c, const vector<Point> &p) {\n\tconst int n = p.size(), m = c.size();\n\tGraph g(n);\n\trep(i, m) {\n\t\tvector<pair<ld, int>> vec;\n\t\trep(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps)\n\t\t\tvec.emplace_back(arg(c[i].p - p[j]), j);\n\t\tsort(all(vec));\n\t\trep(j, vec.size() - 1) {\n\t\t\tint from = vec[j].second, to = vec[j + 1].second;\n\t\t\tld angle = vec[j + 1].first - vec[j].first;\n\t\t\tadd_edge(g, from, to, 1, static_cast<Weight>(angle * c[i].r));\n\t\t}\n\t\tif (vec.size() >= 2) {\n\t\t\tint from = vec.back().second, to = vec.front().first;\n\t\t\tld angle = vec.front().first - vec.back().first;\n\t\t\tadd_edge(g, from, to, 1, static_cast<Weight>(angle * c[i].r));\n\t\t}\n\t}\n\treturn g;\n}\n\n\nint main() {\n\tcout << setprecision(10) << fixed;\n\twhile (1) {\n\t\tint N, M; cin >> N >> M;\n\t\tif (!N)break;\n\t\tvector<Point>ps;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tint x, y; cin >> x >> y;\n\t\t\tps.emplace_back(x, y);\n\t\t}\n\t\tvector<Polygon>areas(N, Polygon{ Point(-1e8,-1e8),Point(1e8,-1e8),Point(1e8,1e8),Point(-1e8,1e8) });\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tfor (int j = 0; j < N; ++j) {\n\t\t\t\tif (i == j)continue;\n\t\t\t\telse {\n\t\t\t\t\tLine l(point_bisection(ps[i], ps[j]));\n\t\t\t\t\tareas[i] = convex_cut(areas[i], l)[0];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<ld>anss(N);\n\t\twhile (M--) {\n\t\t\tint bx, by, dx, dy, score; cin >> bx >> by >> dx >> dy >> score;\n\t\t\tld sum = 4 * dx*dy;\n\t\t\tfor (int i = 0; i < N; ++i) {\n\t\t\t\tPoint ld(bx - dx, by - dy);\n\t\t\t\tPoint rd(bx + dx, by - dy);\n\t\t\t\tPoint ru(bx + dx, by + dy);\n\t\t\t\tPoint lu(bx - dx, by + dy);\n\t\t\t\tPolygon catch_area = areas[i];\n\t\t\t\tcatch_area = convex_cut(catch_area,Line(ld, rd))[0];\n\t\t\t\tcatch_area = convex_cut(catch_area, Line(rd, ru))[0];\n\t\t\t\tcatch_area = convex_cut(catch_area, Line(ru, lu))[0];\n\t\t\t\tcatch_area = convex_cut(catch_area, Line(lu, ld))[0];\n\t\t\t\tanss[i] += get_area(catch_area)*score / sum;\n\t\t\t}\n\n\t\t}\n\t\tld ans = *max_element(anss.begin(), anss.end());\n\t\tcout << ans << endl;\n\t\t\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3364, "score_of_the_acc": -1.2135, "final_rank": 14 }, { "submission_id": "aoj_1514_1929502", "code_snippet": "#include <iostream>\n#include <vector>\n#include <fstream>\n#include <cstdio>\n#include <algorithm>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <set>\n#include <functional>\n#include <ctime>\n#include <numeric>\n#include <unordered_set>\n#include <unordered_map>\n\nusing namespace std;\n\n#define fst first\n#define snd second\n#define all(c) ((c).begin()), ((c).end())\n\n\nconst double EPS = 1e-8;\nint sign(double x) {\n if (x < -EPS) return -1;\n if (x > +EPS) return +1;\n return 0;\n}\nstruct point {\n typedef double T;\n T x, y; \n point &operator+=(point p) { x += p.x; y += p.y; return *this; }\n point &operator-=(point p) { x -= p.x; y -= p.y; return *this; }\n point &operator*=(T a) { x *= a; y *= a; return *this; }\n point &operator/=(T a) { return *this *= (1.0/a); }\n point operator-() const { return {-x, -y}; }\n bool operator<(point p) const { \n int s = sign(x - p.x);\n return s ? s < 0 : sign(y - p.y) < 0;\n }\n};\nbool operator==(point p, point q) { return !(p < q) && !(q < p); }\npoint operator+(point p, point q) { return p += q; }\npoint operator-(point p, point q) { return p -= q; }\npoint operator*(point::T a, point p) { return p *= a; }\npoint operator*(point p, point::T a) { return p *= a; }\npoint operator/(point p, point::T a) { return p /= a; }\npoint::T dot(point p, point q) { return p.x*q.x+p.y*q.y; }\npoint::T cross(point p, point q) { return p.x*q.y-p.y*q.x; } // left turn > 0\npoint::T norm2(point p) { return dot(p,p); }\npoint orth(point p) { return {-p.y, p.x}; }\npoint::T norm(point p) { return sqrt(dot(p,p)); }\npoint::T arg(point p) { return atan2(p.y, p.x); }\n\nstruct line { point p, q; };\ntypedef vector<point> polygon;\n\npolygon convex_cut(polygon ps, line l) {\n vector<point> qs;\n for (int i = 0; i < ps.size(); ++i) {\n int j = (i+1 == ps.size() ? 0 : i+1);\n if (sign(cross(l.p - ps[i], l.q - ps[i])) >= 0) qs.push_back(ps[i]); \n if (sign(cross(l.p - ps[i], l.q - ps[i])) *\n sign(cross(l.p - ps[j], l.q - ps[j])) < 0) {\n auto a = cross(ps[j] - ps[i], l.q - l.p);\n auto b = cross(l.p - ps[i], l.q - l.p); \n qs.push_back(ps[i] + b/a*(ps[j] - ps[i]));\n }\n }\n return qs;\n}\n\nstruct delaunay {\n struct edge {\n int src, dst;\n };\n int n;\n vector<point> ps;\n vector<vector<edge>> adj; // optional\n vector<int> inner;\n int incircle(int a, int b, int c, int p) {\n point u = ps[a]-ps[p], v = ps[b]-ps[p], w = ps[c]-ps[p];\n return sign(norm2(u)*cross(v,w)\n +norm2(v)*cross(w,u)\n +norm2(w)*cross(u,v)) > 0;\n }\n bool orient(int a, int b, int p) { \n point u = ps[a]-ps[b], v = ps[p]-ps[b];\n int s = sign(cross(u, v));\n return s ? s > 0 : sign(dot(u, v)) > 0;\n }\n delaunay(vector<point> ps) : n(ps.size()), ps(ps), adj(n), inner(n) {\n if (n <= 1) return;\n vector<unordered_map<int,int>> ccw(n); // ccw[u][v] is the third pt for (u,v)\n auto make_triangle = [&](int a, int b, int c) {\n ccw[a][b] = c; ccw[b][c] = a; ccw[c][a] = b;\n };\n vector<int> is(n); iota(all(is), 0);\n sort(all(is), [&](int i, int j) { return ps[i] < ps[j]; });\n\n // delaunay flips\n int nflip = 0;\n function<void(int,int)> rec = [&](int a, int b) { \n if (!ccw[a].count(b) || !ccw[b].count(a)) return;\n int c = ccw[a][b], d = ccw[b][a];\n if (incircle(a, b, c, d) > 0) {\n ++nflip;\n ccw[a].erase(b); ccw[b].erase(a);\n make_triangle(d, c, a);\n make_triangle(c, d, b);\n rec(a, d); rec(d, b); rec(b, c); rec(c, a);\n }\n };\n // lexicographic triangulation \n vector<int> next(n,-1), prev(n,-1); \n next[is[0]] = prev[is[0]] = is[1];\n next[is[1]] = prev[is[1]] = is[0];\n for (int i = 2; i < n; ++i) {\n int h = is[i], u = is[i-1], v = u;\n while ( orient(u, next[u], h)) u = next[u];\n while (!orient(v, prev[v], h)) v = prev[v];\n for (int w = v; w != u; w = next[w]) \n if (sign(cross(ps[next[w]]-ps[h], ps[w]-ps[h])) > 0) \n make_triangle(w, h, next[w]);\n next[h] = u; prev[u] = h;\n prev[h] = v; next[v] = h;\n }\n for (int u: is) { \n auto nbh = ccw[u]; // hardcopy\n for (auto z: nbh) rec(z.fst, z.snd); // flip\n }\n // complete graph structure\n for (int u: is) {\n int v = ccw[u].begin()->fst, s = v;\n while (ccw[s].count(u)) {\n s = ccw[s][u];\n if (s == v) break;\n }\n if (v != s) { inner[u] = false; v = s; }\n do {\n adj[u].push_back({u, v});\n if (!ccw[u].count(v)) break;\n v = ccw[u][v];\n } while (v != s);\n }\n }\n};\nstruct voronoi {\n struct edge {\n int src, dst;\n point::T len;\n };\n int n, m;\n vector<point> ps, qs; // qs is the voronoi vertices\n map<point,int> id;\n vector<vector<int>> cell;\n vector<vector<edge>> adj;\n\n void add_edge(int u, int v) {\n auto len = norm(qs[u] - qs[v]);\n adj[u].push_back({u, v, len});\n adj[v].push_back({v, u, len});\n }\n int node(point p) { \n if (!id.count(p)) { id[p] = m++; qs.push_back(p); adj.push_back({}); }\n return id[p];\n }\n voronoi(delaunay DT, vector<point> domain) : \n n(DT.n), m(0), ps(DT.ps), cell(n) {\n for (int u = 0; u < n; ++u) {\n vector<point> region = domain;\n for (auto e: DT.adj[u]) {\n point s = (ps[e.src]+ps[e.dst])/2, d = orth(ps[e.dst]-ps[e.src]);\n region = convex_cut(region, {s, s+d});\n }\n for (int i = 0; i < region.size(); ++i) {\n add_edge(node(region[i]), node(region[(i+1)%region.size()]));\n cell[u].push_back(node(region[i]));\n }\n }\n }\n voronoi(vector<point> ps, vector<point> domain) :\n voronoi(delaunay(ps), domain) { }\n};\nint main() {\n for (int n, m; scanf(\"%d %d\", &n, &m); ) {\n if (n == 0) break;\n vector<point> ps(n);\n for (int i = 0; i < n; ++i) \n scanf(\"%lf %lf\", &ps[i].x, &ps[i].y);\n delaunay DT(ps);\n\n vector<double> score(n);\n double ans = 0.0;\n for (int j = 0; j < m; ++j) {\n point c, d;\n double s; \n scanf(\"%lf %lf %lf %lf %lf\", &c.x, &c.y, &d.x, &d.y, &s);\n vector<point> domain = {\n c + point({-d.x, -d.y}),\n c + point({+d.x, -d.y}),\n c + point({+d.x, +d.y}),\n c + point({-d.x, +d.y}),\n };\n voronoi V(DT, domain);\n\n vector<point> &qs = V.qs;\n for (int i = 0; i < n; ++i) {\n point::T area = 0;\n int K = V.cell[i].size();\n for (int k = 0; k < K; ++k) {\n int v = V.cell[i][k], w = V.cell[i][(k+1)%K];\n area += cross(qs[v], qs[w]);\n }\n //cerr << \"area of \" << i << \"th region: \" << area << endl;\n score[i] += s * area / 2 / (4 * d.x * d.y);\n ans = max(ans, score[i]);\n }\n }\n printf(\"%.12lf\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3332, "score_of_the_acc": -0.8997, "final_rank": 11 }, { "submission_id": "aoj_1514_1381477", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define EPS (1e-5)\n#define equals(a,b) (fabs((a)-(b)) < EPS)\n#define COUNTER_CLOCKWISE 1\n#define CLOCKWISE -1 \n#define ONLINE_BACK 2\n#define ONLINE_FRONT -2\n#define ON_SEGMENT 0\n\nusing namespace std;\n\nclass Point{\npublic:\n double x,y;\n\n Point(double x = 0,double y = 0): x(x),y(y){}\n\n Point operator + (Point p){return Point(x+p.x,y+p.y);}\n Point operator - (Point p){return Point(x-p.x,y-p.y);}\n Point operator * (double a){return Point(a*x,a*y);}\n Point operator / (double a){return Point(x/a,y/a);}\n Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); }\n\n bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }\n\n bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }\n\n};\n\nstruct Segment{\n Point p1,p2;\n Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}\n bool operator < (const Segment& s) const { return ( p2 == s.p2 ) ? p1 < s.p1 : p2 < s.p2; }\n bool operator == (const Segment& s) const { return ( s.p1 == p1 && s.p2 == p2 ) || ( s.p1 == p2 && s.p2 == p1 ); }\n\n};\n\ntypedef Point Vector;\ntypedef Segment Line;\ntypedef vector<Point> Polygon;\n\nostream& operator << (ostream& os,const Point& a){ os << \"(\" << a.x << \",\" << a.y << \")\"; }\n\nostream& operator << (ostream& os,const Segment& a){ os << \"( \" << a.p1 << \" , \" << a.p2 << \" )\"; }\n\ndouble dot(Point a,Point b){ return a.x*b.x + a.y*b.y; }\n\ndouble cross(Point a,Point b){ return a.x*b.y - a.y*b.x; }\n\ndouble norm(Point a){ return a.x*a.x+a.y*a.y; }\n\ndouble abs(Point a){ return sqrt(norm(a)); }\n\n//rad ????§???????????????¢?????§?????????????????¨\nPoint rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); }\n\n// ??????????????¢????????????\ndouble toRad(double agl){ return agl*M_PI/180.0; }\n\n// a => prev, b => cur, c=> next\n// prev ?????? cur ????????£??? next ????????????????§????????±???????\ndouble getArg(Point a,Point b,Point c){\n double arg1 = atan2(b.y-a.y,b.x-a.x);\n double arg2 = atan2(c.y-b.y,c.x-b.x);\n double arg = fabs( arg1 - arg2 );\n while( arg > M_PI ) arg -= 2.0 * M_PI;\n return fabs(arg);\n}\n\nint ccw(Point p0,Point p1,Point p2){\n Point a = p1-p0;\n Point b = p2-p0;\n if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n if(cross(a,b) < -EPS)return CLOCKWISE;\n if(dot(a,b) < -EPS)return ONLINE_BACK;\n if(norm(a) < norm(b))return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\nbool intersectLL(Line l, Line m) {\n return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel\n abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line\n}\nbool intersectLS(Line l, Line s) {\n return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l\n cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l\n}\nbool intersectLP(Line l,Point p) {\n return abs(cross(l.p2-p, l.p1-p)) < EPS;\n}\nbool intersectSS(Line s, Line t) {\n return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&\n ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;\n}\nbool intersectSP(Line s, Point p) {\n return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality\n}\n\nPoint projection(Line l,Point p) {\n double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2);\n return l.p1 + (l.p1-l.p2)*t;\n}\nPoint reflection(Line l,Point p) {\n return p + (projection(l, p) - p) * 2;\n}\ndouble distanceLP(Line l, Point p) {\n return abs(p - projection(l, p));\n}\ndouble distanceLL(Line l, Line m) {\n return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);\n}\n\ndouble distanceLS(Line l, Line s) {\n if (intersectLS(l, s)) return 0;\n return min(distanceLP(l, s.p1), distanceLP(l, s.p2));\n}\ndouble distanceSP(Line s, Point p) {\n Point r = projection(s, p);\n if (intersectSP(s, r)) return abs(r - p);\n return min(abs(s.p1 - p), abs(s.p2 - p));\n}\n\ndouble distanceSS(Line s, Line t) {\n if (intersectSS(s, t)) return 0;\n return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),\n min(distanceSP(t, s.p1), distanceSP(t, s.p2)));\n}\n\nPoint crosspoint(Line l,Line m){\n double A = cross(l.p2-l.p1,m.p2-m.p1);\n double B = cross(l.p2-l.p1,l.p2-m.p1);\n if(abs(A) < EPS && abs(B) < EPS){\n vector<Point> vec;\n vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);\n sort(vec.begin(),vec.end());\n assert(vec[1] == vec[2]); //???????????°??????????????????\n return vec[1];\n //return m.p1;\n }\n if(abs(A) < EPS)assert(false);\n return m.p1 + (m.p2-m.p1)*(B/A);\n}\n\n//Polygon??????????§???¢?????????Line??§?????????????????´(COUNTER_CLOCKWISE)????????????\nPolygon cutPolygon( Polygon P, Line l ){\n Polygon u;\n for ( int i = 0; i < P.size(); i++ ){\n Point a = P[i], b = P[(i+1)%P.size()];\n if ( ccw(l.p1, l.p2, a) != CLOCKWISE ) u.push_back(a);\n\t //if( ccw(l.p1,l.p2,a) != COUNTER_CLOCKWISE ) R.push_back(a); // ????????????????????´???\n if ( ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) == -1 ){\n u.push_back(crosspoint(Segment(a, b), l));\n }\n }\n return u;\n}\n\ndouble getArea(vector<Point>& vec) {\n double sum = 0;\n for(int i=0;i<vec.size();i++)\n sum += cross(vec[i],vec[(i+1)%vec.size()]);\n return fabs(sum)/2.0;\n}\n\nclass Voronoi\n{\npublic:\n Polygon poly;\n \n Voronoi(Polygon poly):poly(poly){}\n \n Point polar(double a,double rad){ return Point(a*cos(rad),a*sin(rad)); }\n \n double args(Point p){ return atan2(p.y,p.x); }\n \n Line CreateLine(Point p,Point pp){\n Point mid = (p+pp)/2;\n Point sl = pp-p;//?????????5\n double rad = args(sl);\n Point ap = polar(abs(sl),rad+M_PI/2)+mid;\n //ap.x = abs(sl)*cos(rad+M_PI/2)+mid.x;\n //ap.y = abs(sl)*sin(rad+M_PI/2)+mid.y;\n return Line(mid,ap);\n } \n\n //?????????pos??????????????????????????????V(pos)?????¢???????????? \n //points???????§???¢??????????????¨????????????????????????]\n //pos???points?????????????????????\n double getAreaOfVoronoi(vector<Point>& points,int pos)\n {\n Polygon polx = poly;\n int N = points.size();\n for(int i=0;i<N;i++)\n if(i != pos)\n\tpolx = cutPolygon(polx,CreateLine(points[pos],points[i]));\n return getArea(polx);\n } \n\n};\n\nconst int MAX_N = 110;\nint N,M;\ndouble maxi[MAX_N];\nvector<Point> ps;\n\nvoid update(Point ball,Point d,double score){\n Polygon poly(4);\n poly[0] = Point(ball.x-d.x,ball.y-d.y);\n poly[1] = Point(ball.x+d.x,ball.y-d.y);\n poly[2] = Point(ball.x+d.x,ball.y+d.y);\n poly[3] = Point(ball.x-d.x,ball.y+d.y);\n double area = abs(d.x*2) * abs(d.y*2);\n Voronoi vor(poly);\n rep(i,N){\n maxi[i] += ( vor.getAreaOfVoronoi(ps,i) / area ) * score;\n }\n}\n\nint main(){\n while( cin >> N >> M, N|M ){\n ps.clear();\n ps.resize(N);\n rep(i,N) cin >> ps[i].x >> ps[i].y;\n \n rep(i,N) maxi[i] = 0;\n rep(i,M) {\n Point b,d;\n double score;\n cin >> b.x >> b.y >> d.x >> d.y >> score;\n update(b,d,score);\n }\n int max_p = 0;\n REP(i,1,N) if( !equals(maxi[max_p],maxi[i]) && maxi[max_p] < maxi[i] ) max_p = i;\n printf(\"%.10f\\n\",maxi[max_p]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 1352, "score_of_the_acc": -0.6933, "final_rank": 10 }, { "submission_id": "aoj_1514_1341847", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100;\nconst int MAX_M = 10;\n\n/* typedef */\n\nstruct Ball {\n int x, y, dx, dy, sc;\n Ball() {}\n Ball(int _x, int _y, int _dx, int _dy, int _sc):\n x(_x), y(_y), dx(_dx), dy(_dy), sc(_sc) {}\n};\n\nstruct Pt {\n double x, y;\n\n Pt() {}\n Pt(double _x, double _y) : x(_x), y(_y) {}\n Pt(const Pt& pt) : x(pt.x), y(pt.y) {}\n\n Pt operator+(const Pt pt) const { return Pt(x + pt.x, y + pt.y); }\n Pt operator-() const { return Pt(-x, -y); }\n Pt operator-(const Pt pt) const { return Pt(x - pt.x, y - pt.y); }\n Pt operator*(double t) const { return Pt(x * t, y * t); }\n Pt operator/(double t) const { return Pt(x / t, y / t); }\n double dot(Pt v) { return x * v.x + y * v.y; }\n double cross(Pt v) { return x * v.y - y * v.x; }\n Pt mid(const Pt pt) { return Pt((x + pt.x) / 2, (y + pt.y) / 2); }\n \n string to_s() {\n char s[1000];\n sprintf(s, \"(%f,%f)\", x, y);\n return string(s);\n }\n};\n\nstruct CL {\n Pt pt;\n double t0, t1;\n CL() {}\n CL(const Pt& _pt, double _t0, double _t1) : pt(_pt), t0(_t0), t1(_t1) {}\n};\n\ntypedef vector<Pt> vpt;\n\n/* global variables */\n\nint n, m;\nPt pts[MAX_N];\nBall bs[MAX_M];\ndouble evs[MAX_N];\n\n/* subroutines */\n\nbool cross_lines(const Pt& a0, const Pt& a1, const Pt& b0, const Pt& b1,\n\t\t CL& cl) {\n Pt da = a1 - a0;\n Pt db = b1 - b0;\n\n double op01 = da.cross(db);\n if (op01 == 0.0) return false; /* need to handle parallel?? */\n\n Pt v = b0 - a0;\n double op0 = v.cross(da);\n double op1 = v.cross(db);\n\n double t0 = op1 / op01;\n double t1 = op0 / op01;\n\n cl.pt = db * t1 + b0;\n cl.t0 = t0;\n cl.t1 = t1;\n return true;\n}\n\nvoid convex_cut(const vpt& scpol, vpt& dcpol, const Pt& pt0, const Pt& pt1) {\n int n = scpol.size();\n Pt v = pt1 - pt0;\n dcpol.clear();\n\n for (int i = 0; i < n; i++) {\n Pt cpt0 = scpol[i];\n double cr0 = v.cross(cpt0 - pt0);\n if (cr0 >= 0.0) dcpol.push_back(cpt0);\n \n Pt cpt1 = scpol[(i + 1) % n];\n double cr1 = v.cross(cpt1 - pt0);\n if (cr0 * cr1 < 0.0) {\n CL cl;\n cross_lines(pt0, pt1, cpt0, cpt1, cl);\n dcpol.push_back(cl.pt);\n }\n }\n}\n\ndouble calc_area(const vpt& cpol) {\n int nc = cpol.size();\n if (nc < 3) return 0.0;\n\n Pt pt0 = cpol[0];\n Pt v0 = cpol[1] - pt0;\n double area = 0.0;\n \n for (int i = 2; i < nc; i++) {\n Pt v1 = cpol[i] - pt0;\n area += v0.cross(v1);\n v0 = v1;\n }\n\n return area / 2;\n}\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n >> m;\n if (n == 0) break;\n\n for (int i = 0; i < n; i++) {\n cin >> pts[i].x >> pts[i].y;\n evs[i] = 0.0;\n }\n\n for (int i = 0; i < m; i++)\n cin >> bs[i].x >> bs[i].y >> bs[i].dx >> bs[i].dy >> bs[i].sc;\n\n vpt cpols[2];\n\n for (int k = 0; k < m; k++) {\n Ball& bk = bs[k];\n double totalarea = 4 * bk.dx * bk.dy;\n \n for (int i = 0; i < n; i++) {\n\tPt& pti = pts[i];\n\tint cur = 0, nxt = 1;\n\n\tcpols[0].clear();\n\tcpols[0].push_back(Pt(bk.x - bk.dx, bk.y - bk.dy));\n\tcpols[0].push_back(Pt(bk.x + bk.dx, bk.y - bk.dy));\n\tcpols[0].push_back(Pt(bk.x + bk.dx, bk.y + bk.dy));\n\tcpols[0].push_back(Pt(bk.x - bk.dx, bk.y + bk.dy));\n\n\tfor (int j = 0; j < n; j++) {\n\t if (i == j) continue;\n\n\t Pt& ptj = pts[j];\n\t Pt pm = pti.mid(ptj);\n\t Pt vij = ptj - pti;\n\t Pt v(-vij.y, vij.x);\n\n\t convex_cut(cpols[cur], cpols[nxt], pm - v, pm + v);\n\t cur ^= 1;\n\t nxt ^= 1;\n\t}\t\n\n\tdouble area = calc_area(cpols[cur]);\n\tevs[i] += bk.sc * area / totalarea;\n }\n }\n\n double maxev = 0.0;\n for (int i = 0; i < n; i++)\n if (maxev < evs[i]) maxev = evs[i];\n\n printf(\"%.6lf\\n\", maxev);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1252, "score_of_the_acc": -0.025, "final_rank": 1 }, { "submission_id": "aoj_1514_1147208", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <iterator>\n#include <assert.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nnamespace geom{\n#define X real()\n#define Y imag()\n#define at(i) ((*this)[i])\n#define SELF (*this)\n\tenum {TRUE = 1, FALSE = 0, BORDER = -1};\n\ttypedef int BOOL;\n\ttypedef double R;\n\tconst R INF = 1e8;\n\tconst R EPS = 1e-12;\n\tconst R PI = 3.1415926535897932384626;\n\tinline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }\n\tinline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}\n\ttypedef complex<R> P;\n\tinline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}\n\tinline R inp(const P& a, const P& b){return (conj(a)*b).X;}\n\tinline R outp(const P& a, const P& b){return (conj(a)*b).Y;}\n\tinline P unit(const P& p){return p/abs(p);}\n\tinline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}\n\tinline int ccw(const P &s, const P &t, const P &p, int adv=0){\n\t\tint res = sig(outp(t-s, p-s));\n\t\tif(res || !adv) return res;\n\t\tif(sig(inp(t-s, p-s)) < 0) return -2;\t// p-s-t\n\t\tif(sig(inp(s-t, p-t)) < 0) return 2;\t// s-t-p\n\t\treturn 0;\t\t\t\t\t\t\t\t// s-p-t\n\t}\n\t\n\t\n\tstruct L : public vector<P>{\t// line\n\t\tL(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}\n\t\tL(){}\n\t\tP dir()const {return at(1) - at(0);}\n\t\tBOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}\n\t};\n\tstruct S : public L{\t// segment\n\t\tS(const P &p1, const P &p2):L(p1, p2){}\n\t\tS(){}\n\t\tBOOL online(const P &p)const {\n\t\t\tif(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;\n\t\t\treturn !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));\n\t\t}\n\t};\n\tstruct C : public P{\n\t\tC(){}\n\t\tC(const P& p, const R r):P(p), r(r){}\n\t\tR r;\n\t\tBOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}\n\t};\n\tstruct F : public C{\n\t\tR s, t;\n\t\tF(const C &c, R ss, R tt):C(c), s(ss), t(tt){\n\t\t\tif(PI < s) s -= 2*PI;\n\t\t\tif(PI < t) t -= 2*PI;\n\t\t}\n\t\tBOOL inside(const P& p)const {\n\t\t\tP v = p - SELF;\n\t\t\tif(!sig(norm(v))) return BORDER;\n\t\t\tR a = arg(v);\n\t\t\tif(t < s){\n\t\t\t\tif((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}else{\n\t\t\t\tif(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;\n\t\t\t\treturn less(s, a) | less(a, t) | less(norm(v), r*r);\n\t\t\t}\n\t\t}\n\t};\n\tP crosspoint(const L &l, const L &m);\n\tstruct G : public vector<P>{\n\t\tG(size_type size=0):vector(size){}\n\t\tS edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}\n\t\tBOOL contains(const P &p)const {\n\t\t\tR sum = .0;\n\t\t\tREP(i, size()){\n\t\t\t\tif(S(at(i), at((i+1)%size())).online(p)) return BORDER;\t// online\n\t\t\t\tsum += arg((at(i) - p) / (at((i+1)%size()) - p));\n\t\t\t}\n\t\t\treturn !!sig(sum);\n\t\t}\n\t\tR area()const {\n\t\t\tR sum = 0;\n\t\t\tREP(i, size()) sum += outp(at(i), at((i+1)%size()));\n\t\t\treturn abs(sum / 2.);\n\t\t}\n\t\t\n\t\tG convex_hull(bool online = false) {\n\t\t\tif(size() < 2) return *this;\n\t\t\tsort(ALL(*this));\n\t\t\tG r;\n\t\t\tr.resize((int)size()*2);\n\t\t\tint k=0;\n\t\t\tfor(int i=0;i<size();r[k++]=at(i++))\n\t\t\t\twhile(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tint t = k;\n\t\t\tfor(int i=(int)size()-1;i>=0;r[k++]=at(i--))\n\t\t\t\twhile(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;\n\t\t\tr.resize(k-1);\n\t\t\treturn r;\n\t\t}\n\t\tG cut(const L &l)const {\n\t\t\tG g;\n\t\t\tREP(i, size()){\n\t\t\t\tconst S &s = edge(i);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);\n\t\t\t\tif(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)\n\t\t\t\t\tg.push_back(crosspoint(s, l));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t\tG Voronoi(const vector<P> &p, const int t)const {\n\t\t\tG g = *this;\n\t\t\tREP(i, p.size())if(i!=t){\n\t\t\t\tconst P m = (p[t]+p[i])*0.5;\n\t\t\t\tg = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));\n\t\t\t}\n\t\t\treturn g;\n\t\t}\n\t};\n\n\tinline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}\n\tinline P reflect(const P &s, const L &t){return 2.*proj(s, t) - s;}\n\tinline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}\n\tBOOL intersect(const S &s, const S &t){\n\t\tconst int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);\n\t\tconst int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);\n\t\treturn (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;\n\t}\n\tBOOL intersect(const S &s, const L &l){\n\t\tif(l.online(s[0]) || l.online(s[1])) return BORDER;\n\t\treturn (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);\n\t}\n\tR dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}\n\tR dist2(const S &s, const P &p){\n\t\tif(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]);\n\t\tif(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);\n\t\treturn dist2((const L &)s, p);\n\t}\n\tR dist2(const S &s, const L &l){\n\t\treturn intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));\n\t}\n\tR dist2(const S &s, const S &t){\n\t\treturn intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), \n\t\t\t\t\t\t\t\t\t \t min(dist2(s, t[1]), dist2(t, s[1])));\n\t}\n\ttemplate <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合\n\t\tR res = INF;\n\t\tREP(i, g.size()) res = min(res, dist2(g.edge(i), t));\n\t\treturn res;\n\t}\n\ttemplate<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}\n\tinline BOOL intersect(const C &a, const C &b){\n\t\treturn less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;\n\t}\n\tinline BOOL intersect(const C &c, const L &l){\n\t\treturn less(dist2(l, c), c.r*c.r);\n\t}\n\tinline BOOL intersect(const C &c, const S &s){\n\t\tint d = less(dist2(s, c), c.r*c.r);\n\t\tif(d != TRUE) return d;\n\t\tint p = c.inside(s[0]), q = c.inside(s[1]);\n\t\treturn (p<0 || q<0) ? BORDER : p&q;\n\t}\n\t\n\t/*\n\t * CCWでs[0]->s[1]にかけてが共通部分\n\t */\n\tinline S crosspoint(const C &c1, const C &c2){\n\t\tif(!intersect(c1, c2)) return S();\n\t\tR d = abs(c1 - c2);\n\t\tR x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);\n\t\tR h = sqrt(c1.r*c1.r - x*x);\n\t\tP u = unit(c2-c1);\n\t\treturn S(c1 + u*x + u*P(0,-1)*h, c1 + u*x + u*P(0,1)*h);\n\t}\n\tinline P crosspoint(const L &l, const L &m){\n\t\tR A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);\n\t\tif(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line\n\t\tif(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!\n\t\treturn m[0] + B / A * (m[1] - m[0]);\n\t}\n\tinline S crosspoint(const C &c, const L &l){\n\t\tR d2 = dist2(l, c);\n\t\tif(c.r*c.r+EPS < d2) return S();\n\t\tP m = proj(c, l);\n\t\tP u = unit(l[1]-l[0]);\n\t\tR d = sqrt(c.r*c.r - d2);\n\t\treturn S(m+u*d, m-u*d);\n\t}\n\tinline vector<P> crosspoint(const C &c, const S &s){\n\t\tvector<P> res = crosspoint(c, (const L&)s);\n\t\tRREP(i, res.size()){\n\t\t\tif(inp(res[i]-s[0], s.dir())*inp(res[i]-s[1], -s.dir())<EPS)\n\t\t\t\tres.erase(res.begin() + i);\n\t\t}\n\t\treturn res;\n\t}\n\tinline R commonarea(const C &a, const C &b){\n\t\tif(less(norm(a-b), (a.r-b.r)*(a.r-b.r)) == TRUE) return min(a.r*a.r, b.r*b.r)*PI;\n\t\tif(less((a.r+b.r)*(a.r+b.r), norm(a-b)) == TRUE) return .0;\n\t\tdouble d = abs(a-b);\n\t\tdouble rc = (d*d + a.r*a.r - b.r*b.r) / (2*d);\n\t\tdouble theta = acos(rc / a.r);\n\t\tdouble phi = acos((d - rc) / b.r);\n\t\treturn a.r*a.r*theta + b.r*b.r*phi - d*a.r*sin(theta);\n\t}\n\tvector<L> CommonTangent(C c1, C c2){\n\t\tif(c1.r > c2.r) swap(c1, c2);\n\t\tdouble d = abs(c1-c2);\n\t\tvector<L> res;\n\t\tif(d < EPS) return res;\n\t\tif(d + EPS > c1.r + c2.r){\n\t\t\t// 内接線\n\t\t\tP crs = (c1*c2.r + c2*c1.r) / (c1.r + c2.r);\n\t\t\tdouble rad = asin(c1.r/abs(crs-c1));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., rad)));\n\t\t\tres.push_back(L(crs, crs + (c1-crs)*polar(1., -rad)));\n\t\t}\n\t\tif(c1.r + d + EPS > c2.r){\n\t\t\t// 外接線\n\t\t\tdouble rad = 0.5*PI+asin((c2.r-c1.r) / d);\n\t\t\tP v = unit(c2-c1)*polar(1., rad);\n\t\t\tif(c1.r + d - EPS < c2.r){\n\t\t\t\tres.push_back(L(c1+v*c1.r, c1+v*c1.r+(c1-c2)*P(0, 1)));\n\t\t\t}else{\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t\tv = 2.*proj(v, c2-c1) - v;\n\t\t\t\tres.push_back(L(c1+v*c1.r, c2+v*c2.r));\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n\t\n\t\n\tstruct Arrangement{\n\t\tstruct AEdge{\n\t\t\tint u, v, t;\n\t\t\tR cost;\n\t\t\tAEdge(int u=0, int v=0, int t=0, R cost=0)\n\t\t\t\t:u(u), v(v), t(t), cost(cost){}\n\t\t};\n\t\ttypedef vector<vector<AEdge>> AGraph;\n\t\tvector<P> p;\n\t\tAGraph g;\n\t\tArrangement(){}\n\t\tArrangement(vector<S> seg){\n\t\t\tint m = seg.size();\n\t\t\tREP(i, m){\n\t\t\t\tp.push_back(seg[i][0]);\n\t\t\t\tp.push_back(seg[i][1]);\n\t\t\t\tREP(j, i) if(sig(outp(seg[i].dir(), seg[j].dir())) && intersect(seg[i], seg[j]) == TRUE)\n\t\t\t\t\tp.push_back(crosspoint(seg[i], seg[j]));\n\t\t\t}\n\t\t\tsort(ALL(p)); UNIQUE(p);\n\t\t\tint n=p.size();\n\t\t\tg.resize(n);\n\t\t\tREP(i, m){\n\t\t\t\tS &s = seg[i];\n\t\t\t\tvector<pair<R, int>> ps;\n\t\t\t\tREP(j, n) if(s.online(p[j])) ps.emplace_back(norm(p[j] - s[0]), j);\n\t\t\t\tsort(ALL(ps));\n\t\t\t\tREP(j, (int)ps.size()-1){\n\t\t\t\t\tconst int u=ps[j].second;\n\t\t\t\t\tconst int v=ps[j+1].second;\n\t\t\t\t\tg[u].emplace_back(u, v, 0, abs(p[u] - p[v]));\n\t\t\t\t\tg[v].emplace_back(v, u, 0, abs(p[u] - p[v]));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tint getIdx(P q){\n\t\t\tauto it = lower_bound(ALL(p), q);\n\t\t\tif(it == p.end() || *it != q) return -1;\n\t\t\treturn it - p.begin();\n\t\t}\n\t};\n\n\tstruct min_ball {\n\t\tP center;\n\t\tR radius2;\n\t\tmin_ball(const vector<P>& p) {\n\t\t\tFOR(it, p) ps.push_back(*it);\n\t\t}\n\t\tmin_ball& compile() {\n\t\t\tm = 0;\n\t\t\tcenter = P(0, 0);\n\t\t\tradius2 = -1;\n\t\t\tmake_ball(ps.end());\n\t\t\treturn *this;\n\t\t}\n\tprivate:\n\t\tlist<P> ps;\n\t\tlist<P>::iterator supp_end;\n\t\tint m;\n\t\tP v[3], c[3];\n\t\tR z[3], r[3];\n\t\tvoid pop() { --m; }\n\t\tvoid push(const P& p) {\n\t\t\tif (m == 0) {\n\t\t\t\tc[0] = p; r[0] = 0;\n\t\t\t} else {\n\t\t\t\tR e = norm(p-c[m-1]) - r[m-1];\n\t\t\t\tP delta = p - c[0];\n\t\t\t\tv[m] = p - c[0];\n\t\t\t\tfor (int i = 1; i < m; ++i)\n\t\t\t\t\tv[m] -= v[i] * inp(v[i], delta) / z[i];\n\t\t\t\tz[m] = inp(v[m], v[m]);\n\t\t\t\tc[m] = c[m-1] + e*v[m]/z[m]*.5;\n\t\t\t\tr[m] = r[m-1] + e*e/z[m]*.25;\n\t\t\t}\n\t\t\tcenter\t= c[m];\n\t\t\tradius2 = r[m]; ++m;\n\t\t}\n\t\tvoid make_ball(list<P>::iterator i) {\n\t\t\tsupp_end = ps.begin();\n\t\t\tif (m == 3) return;\n\t\t\tfor (list<P>::iterator k = ps.begin(); k != i; ) {\n\t\t\t\tlist<P>::iterator j = k++;\n\t\t\t\tif (norm(*j-center) > radius2) {\n\t\t\t\t\tpush(*j); make_ball(j); pop();\n\t\t\t\t\tmove_to_front(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvoid move_to_front(list<P>::iterator j) {\n\t\t\tif (supp_end == j) ++supp_end;\n\t\t\tps.splice (ps.begin(), ps, j);\n\t\t}\n\t};\n\n#undef SELF\n#undef at\n}\n\nusing namespace geom;\n\n\nnamespace std{\n\tbool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y < b.Y;}\n\tbool operator==(const P &a, const P &b){return !sig(norm(a-b));}\n\tistream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}\n\tistream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}\n\tistream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}\n//\tconst double Z = 3;\n//\tostream& operator<<(ostream &os, const P &p){return os << \"circle(\"<<Z*(200+p.X)<<\", \"<<Z*(200-p.Y)<<\", 2)\";}\n//\tostream& operator<<(ostream &os, const C &c){return os << \"circle(\"<<Z*(200+c.X)<<\", \"<<Z*(200-c.Y)<<\", \"<<Z*(c.r)<<\")\";}\n//\tostream& operator<<(ostream &os, const S &s){return os << \"line(\"<<Z*(200+s[0].X)<<\", \"<<Z*(200-s[0].Y)<<\", \"<<Z*(200+s[1].X)<<\", \"<<Z*(200-s[1].Y)<<\")\";}\n//\tostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout << g.edge(i) << endl;return os;}\n}\n\n\nint n, m;\n\nint main(){\n\tios::sync_with_stdio(false);\n\twhile(cin >> n >> m, n){\n\t\tvector<P> p(n);\n\t\tvector<R> ans(n, .0);\n\t\tREP(i, n) cin >> p[i];\n\t\tREP(i, m){\n\t\t\tR sx, sy, dx, dy, score;\n\t\t\tG g;\n\t\t\tcin >> sx >> sy >> dx >> dy >> score;\n\t\t\tg.emplace_back(sx-dx, sy-dy);\n\t\t\tg.emplace_back(sx+dx, sy-dy);\n\t\t\tg.emplace_back(sx+dx, sy+dy);\n\t\t\tg.emplace_back(sx-dx, sy+dy);\n\t\t\tscore /= dx*dy*4;\n\t\t\tREP(j, n) ans[j] += g.Voronoi(p, j).area() * score;\n\t\t}\n\t\tprintf(\"%.14f\\n\", *max_element(ALL(ans)));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 1288, "score_of_the_acc": -1.0156, "final_rank": 12 }, { "submission_id": "aoj_1514_1143937", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\ntypedef complex<double> P;\ntypedef vector<P> L;\ntypedef vector<P> Pol;\nconst double EPS = 1e-5;\nint sign(double x){ return x > EPS ? 1 : x < -EPS ? -1 : 0; }\n\n// テ・ツ??ァツゥツ催」ツδサテ・ツ、ツ姪ァツゥツ?\ndouble dot(P a, P b){return real(conj(a) * b);}\ndouble cross(P a, P b){return imag(conj(a) * b);}\nint ccw(P a, P b, P c) {\n b -= a; c -= a;\n return sign(cross(b,c)) ? : dot(b,c) < 0 ? 2 : norm(b) < norm(c) ? -2 : 0;\n}\n\nP vec(L l){return l[1] - l[0];}\nvector<P> pLL(L l, L m){\n double A = cross(vec(l), vec(m));\n double B = cross(vec(l), l[1] - m[0]);\n if(sign(A) == 0 && sign(B) == 0) return {l[0], l[1], m[0], m[1]}; // テ、ツコツ古ァツ崢エテァツキツ堙」ツ?古ゥツ?催」ツ?ェテ」ツ?」テ」ツ?ヲテ」ツ??」ツつ?\n if(sign(A) == 0) return{}; // テァツ崢エテァツキツ堙」ツ?古、ツコツ、テ」ツつ湘」ツつ嘉」ツ?ェテ」ツ??\n return {m[0] + vec(m) * B / A};\n}\n\n// テ・ツ?クテ・ツ、ツ堙ィツァツ津・ツスツ「テ」ツ?ョテァツ崢エテァツキツ堙」ツ?ォテ」ツつ暗」ツつ凝・ツ按?ヲツ鳴ュテ」ツ?づァツ崢エテァツキツ堙」ツ?ョテ・ツキツヲテ・ツ?エテ」ツ??」ツ?妥ヲツョツ凝」ツ??\n// verify : aoj1283\nPol convex_cut(const Pol& A, L l){\n int n = A.size();\n Pol B;\n for(int i = 0; i < n; i++){\n P a = A[i], b = A[(i + 1) % n];\n if(ccw(l[0], l[1], a) != -1) B.push_back(a); //Aテ」ツ?古ァツ崢エテァツキツ嗟テ」ツ?ョテ・ツ渉ウテ・ツ?エテ」ツ?ァテ」ツ?ェテ」ツ??\n if(ccw(l[0], l[1], a) * ccw(l[0], l[1], b) < 0)\n B.push_back(pLL(l, {a, b})[0]);\n }\n return B;\n}\n// テ・ツ楪づァツ崢エテ、ツコツ古ァツュツ嘉・ツ按?ァツキツ?\n// verify: maximamcup2013 D\nL bisector(P a, P b){\n P mid = (a + b) / 2.0;\n P vec = (mid - a) * P(0.0, 1.0);\n return {mid, mid + vec};\n}\n// テァツつケテゥツ崢?・ツ青?sテ」ツ?ョテ」ツ??」ツ?。sテァツ閉ェテァツ崢ョテ」ツ?ョテ」ツδ愿」ツδュテ」ツδ偲」ツつ、テゥツ?佚・ツ淞?\n// verify: maximamcup2013 D\nPol voronoi_cell(Pol A, const vector<P>& ps, int s){\n for(int i = 0; i < ps.size(); i++){\n if(i != s) A = convex_cut(A, bisector(ps[s], ps[i]));\n }\n return A;\n\n}\n\ndouble area(const Pol& A) {\n double res = 0;\n int n = A.size();\n for(int i = 0; i < n; i++){\n res += cross(A[i], A[(i + 1) % n]);\n }\n return abs(res) / 2.0;\n}\nPol unite(Pol P, Pol b) {\n for(int i = 0; i < b.size(); i++) {\n L l = {b[i], b[(i+1)%b.size()]};\n P = convex_cut(P, l);\n }\n return P;\n}\n\nint main(){\n int N, M;\n while(cin >> N >> M && N > 0) {\n vector<P> vp(N);\n REP(i, N) {\n double x, y;\n cin >> x >> y;\n vp[i] = {x, y};\n }\n vector<Pol> pols(N);\n Pol uni;\n uni.push_back(P(-1e6, -1e6));\n uni.push_back(P(+1e6, -1e6));\n uni.push_back(P(+1e6, +1e6));\n uni.push_back(P(-1e6, +1e6));\n REP(i, N) pols[i] = voronoi_cell(uni, vp, i);\n\n vector<double> exp(N);\n\n REP(_, M) {\n Pol bal;\n double bx, by, dx, dy, score;\n cin >> bx >> by >> dx >> dy >> score;\n bal.push_back(P(bx-dx, by-dy));\n bal.push_back(P(bx+dx, by-dy));\n bal.push_back(P(bx+dx, by+dy));\n bal.push_back(P(bx-dx, by+dy));\n double SUM = dx * dy * 4;\n double verify = 0;\n REP(i, N) {\n double ratio = area(unite(pols[i], bal)) / SUM;\n exp[i] += score * ratio;\n verify += ratio;\n }\n assert(sign(verify - 1) == 0);\n }\n printf(\"%.6f\\n\", *max_element(exp.begin(), exp.end()));\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1288, "score_of_the_acc": -0.1156, "final_rank": 5 }, { "submission_id": "aoj_1514_997052", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\nusing namespace std;\n#define DEBUG(x) cerr << #x << \" = \" << x << endl\ntypedef double D;\nconst D EPS = 1e-8;\nconst D PI = M_PI;\nstruct P {\n D x, y;\n P() { }\n P(D x_, D y_) : x(x_), y(y_) { }\n};\nstruct L {\n P a, b;\n L() { }\n L(P a_, P b_) : a(a_), b(b_) { }\n};\nstruct C {\n P p; D r;\n C() { }\n C(P p_, D r_) : p(p_), r(r_) { }\n};\nP operator +(P a, P b) { return P(a.x + b.x, a.y + b.y); }\nP operator -(P a, P b) { return P(a.x - b.x, a.y - b.y); }\nP operator *(P a, D b) { return P(a.x * b, a.y * b); }\nP operator /(P a, D b) { return P(a.x / b, a.y / b); }\nP vec(P from, P to) { return to - from; }\nD inp(P a, P b) { return a.x*b.x + a.y*b.y; }\nD outp(P a, P b) { return a.x*b.y - a.y*b.x; }\nD norm(P p) { return inp(p, p); }\nD abs(P p) { return sqrt(norm(p)); }\nint sig(D a, D b) {\n if(a < b - EPS) return -1;\n if(a > b + EPS) return +1;\n return 0;\n}\n// !!! とりあえずここまで写してください !!!\nD arg(P p) { return atan2(p.y, p.x); }\nP rot90(P p) { return P(-p.y, p.x); }\nP rot(P p, D radian) {\n P q;\n q.x = cos(radian)*p.x - sin(radian)*p.y;\n q.y = sin(radian)*p.x + cos(radian)*p.y;\n return q;\n}\nint ccw(P a, P b, P c) { // 重なっている点があるとうまく動かないと思われる\n b = vec(a, b); c = vec(a, c);\n\n // a - b - c が折れ曲がるとき\n if(sig(outp(b, c), 0.0) > 0) return +1; // 反時計回り\n if(sig(outp(b, c), 0.0) < 0) return -1; // 時計回り\n\n // a - b - c が直線上に並ぶとき\n if(sig(inp(b, c), 0.0) < 0) return +2; // c - a - b\n if(norm(b) < norm(c)) return -2; // a - b - c\n return 0; // a - c - b\n}\nP projection(L l, P p) { // 直線lに対する点pの写像\n P a = vec(l.a, l.b);\n P b = vec(l.a, p);\n D t = inp(a, b) / norm(a);\n return l.a + a * t;\n}\nP reflection(L l, P p) { // 直線lに対する点pの反射\n return p + vec(p, projection(l, p)) * 2;\n}\n\n// 線分と点\nbool iSP(L s, P p) {\n return ccw(s.a, s.b, p) == 0;\n}\nD dSP(L s, P p) {\n P r = projection(s, p);\n if(iSP(s, r)) return abs(p - r); // 写像がs上にある\n return min(abs(p - s.a), abs(p - s.b)); // 写像がs上にない\n}\n\n// 直線と点\nbool iLP(L l, P p) {\n return abs(ccw(l.a, l.b, p)) != 1;\n}\nD dLP(L l, P p) {\n return abs(p - projection(l, p));\n}\n\n// 線分と線分\nbool iSS(L s, L t) { // 端を含む\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\nD dSS(L s, L t) {\n if(iSS(s, t)) return 0;\n return min(min(dSP(s, t.a), dSP(s, t.b)),\n min(dSP(t, s.a), dSP(t, s.b)));\n}\n// P cSS(L s, L t) はiSS(s,t)を確認してからcLLを使う\n\n// 直線と直線\nbool iLL(L l, L m) {\n return sig(outp(vec(l.a, l.b), vec(m.a, m.b)), 0.0) != 0 || // 平行でない\n sig(outp(vec(l.a, l.b), vec(l.a, m.a)), 0.0) == 0; // 同じ直線\n}\nD dLL(L l, L m) {\n return iLL(l, m) ? 0.0 : dLP(l, m.a);\n}\nP cLL(L l, L m) {\n D d = outp(vec(m.a, m.b), vec(l.a, l.b));\n return l.a + vec(l.a, l.b) * outp(vec(m.a, m.b), vec(l.a, m.b)) / d;\n}\n\n// 直線と線分\nbool iLS(L l, L s) {\n return sig(outp(vec(l.a, l.b), vec(l.a, s.a)), 0.0) *\n sig(outp(vec(l.a, l.b), vec(l.a, s.b)), 0.0) <= 0;\n}\nD dLS(L l, L s) {\n if(iSS(l, s)) return 0.0;\n return min(dLP(l, s.a), dLP(l, s.b));\n}\n// P cLS(L s, L t) はiSS(s,t)を確認してからcLLを使う\n\n// 円と直線\nint iCL(C c, L l) {\n D d = dLP(l, c.p);\n int s = sig(d, c.r);\n if(s < 0) return 2;\n if(s == 0) return 1;\n return 0;\n}\nvector<P> cCL(C c, L l) {\n P a = projection(l, c.p);\n D s = abs(c.p - a);\n D t = sqrt(c.r*c.r-s*s);\n P v = vec(l.a,l.b)/abs(l.a-l.b);\n vector<P> res;\n res.push_back(a+v*t);\n res.push_back(a-v*t);\n return res;\n}\n\n// 円と点\nvector<P> tCP(C c, P p) {\n vector<P> res;\n D d = abs(c.p - p);\n if(sig(d,c.r) < 0) return res;\n D rc = c.r*c.r/d;\n D rs = sqrt(max(0.0, c.r*c.r - rc*rc));\n P v = (p - c.p) / abs(p - c.p);\n res.emplace_back(c.p + v*rc + rot90(v)*rs);\n res.emplace_back(c.p + v*rc - rot90(v)*rs);\n return res;\n}\n\n// 円と線分\nint iCS(C c, L s) {\n if(sig(abs(c.p - s.a), c.r) <= 0 && sig(abs(c.p - s.b), c.r) <= 0)\n return -2; // 線分の両端が円の内側にある\n if(sig(abs(c.p - s.a), c.r) <= 0 || sig(abs(c.p - s.b), c.r) <= 0)\n return -1; // 線分の端の一方が円の内側、他方が円の外側にある\n if(sig(dLP(s, c.p), c.r) < 0) // dSPじゃなくてdLPで十分だと思う\n return +2; // 線分と円周が2点で交わる\n if(sig(dLP(s, c.p), c.r) == 0)\n return +1; // 線分と円周が1点で交わる\n return 0;\n}\n// vector<P> cCS(C c, L s) はiCSの状態によって定義が難しいが、\n// p <- cCL(c,s)からiSP(s,p)==trueのものだけを抜き出せば良い気がする\n\n// 円と円\n// int iCC(C a, C b) はaがbの内側にある場合などもあるので定義が難しい\n// 2点で接していることを確認すること\nvector<P> cCC(C a, C b) {\n D d = abs(b.p - a.p);\n D x = (d*d + a.r*a.r - b.r*b.r) / (2*d);\n D y = sqrt(a.r*a.r - x*x);\n P v = (b.p - a.p) / d;\n vector<P> res;\n res.emplace_back(a.p + v*x + rot90(v)*y);\n res.emplace_back(a.p + v*x - rot90(v)*y);\n return res;\n}\n// tCCinに同じ円を食わせたり、\n// tCCoutにどちらかが他方に囲まれている円を食わせたりすると破綻することは分かっているが、\n// そのあたりが厳密にverifyできていない (AOJ 2201でACすることは確認した)\nvector<L> tCCout(C a, C b) {\n vector<L> res;\n if(sig(abs(a.r - b.r), abs(a.p - b.p)) >= 0) return res;\n if(sig(a.r, b.r) == 0) {\n P v = (b.p - a.p) / abs(b.p - a.p);\n v = rot90(v);\n res.emplace_back(a.p + v*a.r, b.p + v*b.r);\n res.emplace_back(a.p - v*a.r, b.p - v*b.r);\n return res;\n }\n P p = (a.p*b.r - b.p*a.r) / (b.r - a.r);\n vector<P> at = tCP(a, p);\n vector<P> bt = tCP(b, p);\n for(int i = 0; i < (int)min(at.size(), bt.size()); ++i) {\n res.emplace_back(at[i], bt[i]);\n }\n return res;\n}\nvector<L> tCCin(C a, C b) {\n vector<L> res;\n if(sig(abs(a.r + b.r), abs(a.p - b.p)) >= 0) return res;\n P p = (a.p*b.r + b.p*a.r) / (a.r + b.r);\n vector<P> at = tCP(a, p);\n vector<P> bt = tCP(b, p);\n for(int i = 0; i < (int)min(at.size(), bt.size()); ++i) {\n res.emplace_back(at[i], bt[i]);\n }\n return res;\n}\nvector<L> tCC(C a, C b) {\n vector<L> res;\n for(L l : tCCout(a, b)) res.push_back(l);\n for(L l : tCCin(a, b)) res.push_back(l);\n return res;\n}\n\n// 多角形\ntypedef vector<P> G;\n// 半時計回りを仮定している (時計回りならabsを取る)\nD area(G g) {\n D res = 0.0;\n for(int i = 0; i < (int)g.size(); i++) {\n res += outp(g[i], g[(i+1)%g.size()]);\n }\n return res / 2.0;\n}\n// ON = 0, IN = 1, OUT = -1\nint containsGP(G g, P p) {\n int side = -1;\n for(int i = 0; i < (int)g.size(); i++) {\n if(ccw(g[i], g[(i+1)%g.size()], p) == 0) return 0;\n P a = vec(p, g[i]);\n P b = vec(p, g[(i+1)%g.size()]);\n if(a.y > b.y) swap(a, b);\n if(sig(a.y, 0.0) <= 0 && sig(b.y, 0.0) > 0 && sig(outp(a, b), 0.0) > 0) side *= -1;\n }\n return side;\n}\nbool operator <(P a, P b) {\n if(sig(a.x, b.x) != 0) return a.x < b.x;\n return a.y < b.y;\n}\n// 凸包を構成する点を得る。半時計回り\n// 辺上の点も含めるときは ccw(..) == -1 とすること\nG convex_hull(vector<P> ps) {\n int N = ps.size();\n int k = 0; // 凸包を構成する点の数\n sort(ps.begin(), ps.end());\n G res(N * 2);\n for(int i = 0; i < N; i++) {\n // 時計回りの折れ曲がりがあったら、折れ点を削除\n while(k >= 2 && ccw(res[k - 2], res[k - 1], ps[i]) <= 0) k--;\n res[k++] = ps[i];\n }\n int t = k + 1;\n // 右端は取らない (重複するから)\n // 左端は取る (重複するけど、あとから取り除いたほうが楽)\n for(int i = N - 2; i >= 0; i--) {\n while(k >= t && ccw(res[k - 2], res[k - 1], ps[i]) <= 0) k--;\n res[k++] = ps[i];\n }\n res.resize(k - 1);\n return res;\n}\n// l.a -> l.b の厳密に右側の領域を切り落とす\nG convex_cut(G g, L l) {\n G res;\n for(int i = 0; i < (int)g.size(); ++i) {\n P a = g[i];\n P b = g[(i+1)%g.size()];\n if(ccw(l.a, l.b, a) != -1) res.push_back(a);\n // 端の点を含まないiLS\n if(ccw(l.a, l.b, a)*ccw(l.a, l.b, b) < 0) {\n res.push_back(cLL(L(a, b), l));\n }\n }\n return res;\n}\n// aとbの垂直二等分線をaが左側に来るように計算する\nL bisector(P a, P b) {\n P p = (a + b) / 2;\n return L(p, p + rot90(vec(a,b)));\n}\n// 外枠をgとして点集合vのk番目の点のボロノイ領域を返す\nG voronoi_cell(G g, vector<P> v, int k) {\n for(int i = 0; i < (int)v.size(); ++i) {\n if(i == k) continue;\n g = convex_cut(g, bisector(v[k], v[i]));\n }\n return g;\n}\nint main() {\n while(true) {\n int N, M; cin >> N >> M;\n if(N == 0 && M == 0) break;\n vector<P> v;\n for(int i = 0; i < N; i++) {\n double x, y; cin >> x >> y;\n v.emplace_back(x,y);\n }\n vector<double> score(N);\n for(int i = 0; i < M; i++) {\n int bx,by,dx,dy;\n int s;\n cin >> bx >> by >> dx >> dy;\n cin >> s;\n G g;\n g.emplace_back(bx-dx,by+dy);\n g.emplace_back(bx-dx,by-dy);\n g.emplace_back(bx+dx,by-dy);\n g.emplace_back(bx+dx,by+dy);\n D all = area(g);\n for(int j = 0; j < N; j++) {\n D a = area(voronoi_cell(g, v, j));\n score[j] += a / all * s;\n }\n }\n cout.setf(ios::fixed);\n cout.precision(5);\n cout << *max_element(score.begin(), score.end()) << endl;\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 1320, "score_of_the_acc": -0.4044, "final_rank": 8 }, { "submission_id": "aoj_1514_810217", "code_snippet": "#include<iostream>\n#include<cfloat>\n#include<cassert>\n#include<cmath>\n#include<vector>\n#include<cstdio>\n#include<set>\nusing namespace std;\n\n#define EPS (1e-8)\n#define equals(a, b) (fabs((a) - (b)) < EPS )\n#define dle(a, b) (equals(a, b) || a < b )\n\nstatic const double PI = acos(-1);\n\nclass Point{\n public:\n double x, y;\n \n Point ( double x = 0, double y = 0): x(x), y(y){}\n \n Point operator + ( Point p ){ return Point(x + p.x, y + p.y); }\n Point operator - ( Point p ){ return Point(x - p.x, y - p.y); }\n Point operator * ( double a ){ return Point(x*a, y*a); }\n Point operator / ( double a ){ return Point(x/a, y/a); }\n\n double abs() { return sqrt(norm());}\n double norm() { return x*x + y*y; }\n\n bool operator < ( const Point &p ) const {\n\treturn x != p.x ? x < p.x : y < p.y;\n }\n\n bool operator == ( const Point &p ) const {\n\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n }\n};\n\ntypedef Point Vector;\n\nclass Segment{\n public:\n Point p1, p2;\n Segment(Point s = Point(), Point t = Point()): p1(s), p2(t){}\n};\n\ntypedef Segment Line;\n\ntypedef vector<Point> Polygon;\n\ndouble norm( Vector a ){ return a.x*a.x + a.y*a.y; }\ndouble abs( Vector a ){ return sqrt(norm(a)); }\nPoint polar( double a, double r ){ return Point(cos(r)*a, sin(r)*a);}\ndouble getDistance( Vector a, Vector b ){ return abs(a - b); }\ndouble dot( Vector a, Vector b ){ return a.x*b.x + a.y*b.y; }\ndouble cross( Vector a, Vector b ){ return a.x*b.y - a.y*b.x; }\n\nPoint project( Segment s, Point p ){\n Vector base = s.p2 - s.p1;\n double t = dot(p - s.p1, base)/norm(base);\n return s.p1 + base*t;\n}\n\nPoint reflect( Segment s, Point p ){\n return p + (project(s, p) - p)*2.0;\n}\n\n// verified by uoa2062\nbool isOnSegment( Point a, Point b, Point c){\n if ( a == c || b == c ) return true;\n return (abs(a-c) + abs(c-b) < abs(a-b) + EPS );\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n// EPS can be 0\n// need to check for 920, 833, 866\nint ccw( Point p0, Point p1, Point p2 ){\n Vector a = p1 - p0;\n Vector b = p2 - p0;\n if ( cross(a, b) > EPS ) return COUNTER_CLOCKWISE;\n if ( cross(a, b) < -EPS ) return CLOCKWISE;\n if ( dot(a, b) < -EPS ) return ONLINE_BACK;\n if ( norm(a) < norm(b) ) return ONLINE_FRONT;\n return ON_SEGMENT;\n}\n\n// intersect Segment p1-p2 and Segment p3-p4 ?\nbool isIntersect(Point p1, Point p2, Point p3, Point p4){\n return ( ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&\n\t ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0 );\n}\n\n// intersect Segment s1 and Segment s2 ?\n// verified by 920, 833, 866, uoa2062\nbool isIntersect(Segment s1, Segment s2){\n return isIntersect(s1.p1, s1.p2, s2.p1, s2.p2);\n}\n\n// verified by 920, 833, uoa2062\nPoint getCrossPoint(Segment s1, Segment s2){\n assert( isIntersect(s1, s2) );\n Vector base = s2.p2 - s2.p1;\n double d1 = abs(cross(base, s1.p1 - s2.p1));\n double d2 = abs(cross(base, s1.p2 - s2.p1));\n double t = d1/(d1 + d2);\n return s1.p1 + (s1.p2 - s1.p1)*t;\n}\n\nPoint getCrossPointLines( Line s1, Line s2){\n Vector a = s1.p2 - s1.p1;\n Vector base = s2.p2 - s2.p1;\n return s1.p1 + a * cross(base, s2.p1 - s1.p1)/cross(base, a);\n}\n\ndouble arg(Vector p){\n return atan2(p.y, p.x);\n}\n\nPolygon cutPolygon( Polygon P, Line l ){\n Polygon u;\n for ( int i = 0; i < P.size(); i++ ){\n\tPoint a = P[i], b = P[(i+1)%P.size()];\n\tif ( ccw(l.p1, l.p2, a) != CLOCKWISE ) u.push_back(a);\n\tif ( ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) == -1 ){\n\t u.push_back(getCrossPointLines(Segment(a, b), l));\n\t}\n }\n return u;\n}\n\ndouble getArea(Polygon p){\n double sum = 0.0;\n for(int i = 0; i < p.size(); i++){\n\tsum += cross(p[i], p[(i+1)%p.size()]);\n }\n return abs(sum/2);\n}\n\nLine getCutLine( Point p1, Point p2 ){\n Vector v = p2 - p1;\n v = polar(abs(v), arg(v)+PI/2.0);\n double dx = (p2.x + p1.x)/2.0;\n double dy = (p2.y + p1.y)/2.0;\n return Line(Point(dx, dy), Point(v.x+dx, v.y+dy));\n}\n\nvector<Polygon> getVoronoi( vector<Point> PV ){\n static const int X_MIN = -1000000;\n static const int Y_MIN = -1000000;\n static const int X_MAX = 1000000;\n static const int Y_MAX = 1000000;\n vector<Polygon> V;\n for ( int i = 0; i < PV.size(); i++ ){\n Polygon P;\n P.push_back(Point(X_MIN, Y_MIN));\n P.push_back(Point(X_MAX, Y_MIN));\n P.push_back(Point(X_MAX, Y_MAX));\n P.push_back(Point(X_MIN, Y_MAX));\n for ( int j = 0; j < PV.size(); j++ ){\n if ( i == j ) continue;\n P = cutPolygon(P, getCutLine(PV[i], PV[j]));\n }\n V.push_back(P);\n }\n return V;\n}\n\nint main(){\n int N, M;\n double S[100];\n\n while(1){\n cin >> N >> M;\n if ( N == 0 && M == 0 ) break;\n vector<Point> P;\n int x, y, dx, dy, s;\n set<Point> ps;\n for ( int i = 0; i < N; i++ ){\n cin >> x >> y;\n P.push_back(Point(x, y));\n ps.insert(Point(x, y));\n }\n assert(ps.size()==N);\n vector<Polygon> G = getVoronoi(P);\n\n for ( int i = 0; i < N; i++ ) S[i] = 0;\n double maxscore = 0;\n\n for ( int i = 0; i < M; i++ ){\n cin >> x >> y >> dx >> dy >> s;\n Polygon sq;\n sq.push_back(Point(x-dx, y-dy));\n sq.push_back(Point(x+dx, y-dy));\n sq.push_back(Point(x+dx, y+dy));\n sq.push_back(Point(x-dx, y+dy));\n \n for ( int p = 0; p < N; p++ ){\n\tPolygon sx = sq;\n\tPolygon g = G[p];\n\tfor ( int j = 0; j < g.size(); j++ ){\n\t Line l = Line(g[j], g[(j+1)%g.size()]);\n\t sx = cutPolygon(sx, l);\n\t}\n\tS[p] += s*getArea(sx)/(dx*2*dy*2);\n }\n }\n\n for ( int i = 0; i < N; i++ ) maxscore = max(maxscore, S[i]);\n\n printf(\"%.8lf\\n\", maxscore);\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1352, "score_of_the_acc": -0.1433, "final_rank": 7 }, { "submission_id": "aoj_1514_791876", "code_snippet": "#include<algorithm>\n#include <cstring>\n#include <iostream>\n#include<cmath>\n#include<cstdio>\n#include<set>\n#include<map>\n#include<queue>\n#include<vector>\n#include<utility>\n#include<stack>\n#include <complex>\n#include <cassert>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int,int> pii;\n\nconst double EPS = 1e-8;\nconst int INF = 1<<29;\nconst double PI = acos(-1);\n\ntypedef complex<double> P;\n\nnamespace std {\nbool operator<(const P &a, const P&b) {\n return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n}\n}\ndouble cross(const P &a, const P&b) {\n return imag(conj(a)*b);\n}\ndouble dot(const P &a, const P&b) {\n return real(conj(a)*b);\n}\nstruct L : public vector<P> {\n L(const P &a, const P &b) {\n push_back(a); push_back(b);\n }\n L() {}\n};\ntypedef vector<P> G;\n\nint ccw(P a, P b, P c) {\n b-=a; c-=a;\n if (cross(b,c) > 0) return 1;\n if (cross(b,c) < 0) return -1;\n if (dot(b,c) < 0) return 2;\n if (norm(b) < norm(c)) return -2;\n return 0;\n}\nP crosspoint(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n if (abs(A) < EPS && abs(B) < EPS) return m[0];\n if (abs(A) < EPS) assert(false);\n return m[0] + B/A*(m[1]-m[0]);\n}\ndouble area( const G &g) {\n double A = 0;\n for (int i=0; i<g.size(); ++i)\n A += cross(g[i],g[(i+1)%g.size()]);\n return abs(A/2);\n}\nG convex_cut(const G &g, const L &l) {\n G Q;\n for (int i=0; i<g.size(); ++i) {\n P A = g[i];\n P B = g[(i+1)%g.size()];\n if (ccw(l[0], l[1],A) != -1) Q.push_back(A);\n if (ccw(l[0],l[1],A) * ccw(l[0],l[1],B) < 0)\n Q.push_back(crosspoint(L(A,B),l));\n }\n return Q;\n}\nL bisector(const P &a, const P &b) {\n P A = (a+b)*P(0.5,0);\n return L(A,A+(b-a)*P(0,PI/2));\n}\nG voronoi_cell(G g, const vector<P> &v, int s) {\n for (int i=0; i<v.size(); ++i) {\n if (i!=s) {\n g = convex_cut(g,bisector(v[s],v[i]));\n }\n }\n return g;\n}\n\ndouble ans[100];\n\nint main() {\n int n,m;\n while(cin>>n>>m,n||m) {\n vector<P> p(n);\n for (int i=0; i< n; ++i) cin >> p[i].real() >> p[i].imag();\n memset(ans,0,sizeof(ans));\n for (int i=0; i<m; ++i) {\n int bx,by,dx,dy;\n cin>>bx>>by>>dx>>dy;\n int score;\n cin >> score;\n G g;\n g.push_back(P(bx-dx,by-dy));\n g.push_back(P(bx+dx,by-dy));\n g.push_back(P(bx+dx,by+dy));\n g.push_back(P(bx-dx,by+dy));\n for (int i=0; i<n; ++i) {\n G v = voronoi_cell(g, p, i);\n ans[i] += area(v) / area(g) * score;\n }\n }\n double res = *max_element(ans,ans+n);\n printf(\"%.10f\\n\", res);\n }\n \n}", "accuracy": 1, "time_ms": 270, "memory_kb": 1272, "score_of_the_acc": -0.6337, "final_rank": 9 }, { "submission_id": "aoj_1514_634354", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <functional>\n#include <iostream>\n#include <iterator>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\n\n#define lengthof(array) (sizeof(array) / sizeof(*array))\n#define dump(a) (cerr << (#a) << \" = \" << (a) << endl)\n#define FOR(it,c) for(__typeof((c).begin())it=(c).begin(); it!=(c).end();++it)\n#define RFOR(it,c) for(__typeof((c).rbegin())it=(c).rbegin(); it!=(c).rend();++it)\n\ntemplate<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }\ntemplate<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }\n\ntemplate<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n\tos << '(' << p.first << \", \" << p.second << ')';\n\treturn os;\n}\n\ntemplate<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n\tcopy(v.begin(), v.end(), ostream_iterator<T>(os, \" \"));\n\treturn os;\n}\n\nconst double EPS = 1e-9;\n\nstruct point {\n\tdouble x, y;\n\tpoint():x(0), y(0) {}\n\tpoint(double x, double y):x(x), y(y) {}\n\tpoint(const point& p):x(p.x), y(p.y) {}\n\n\tpoint operator+ (const point& p) const {\n\t\treturn point(x + p.x, y + p.y);\n\t}\n\n\tpoint operator- (const point& p) const {\n\t\treturn point(x - p.x, y - p.y);\n\t}\n\n\tpoint operator* (const double s) const {\n\t\treturn point(x * s, y * s);\n\t}\n\n\tpoint operator* (const point& p) const {\n\t\treturn point(x * p.x - y * p.y, x * p.y + y * p.x);\n\t}\n\n\tpoint operator/ (const double s) const {\n\t\treturn point(x / s, y / s);\n\t}\n\n\tbool operator< (const point& p) const {\n\t\treturn x + EPS < p.x || abs(x - p.x) < EPS && y + EPS < p.y;\n\t}\n\n\tbool operator== (const point& p) const {\n\t\treturn abs(x - p.x) < EPS && abs(y - p.y) < EPS;\n\t}\n};\n\npoint rotate90(const point& p) {\n\treturn point(-p.y, p.x);\n}\n\npoint rotate(const point& p, const double theta) {\n\tconst double s = sin(theta), c = cos(theta);\n\treturn point(c * p.x - s * p.y, s * p.x + c * p.y);\n}\n\ndouble angle(const point& p) {\n\treturn atan2(p.y, p.x);\n}\n\ndouble abs(const point& p) {\n\treturn sqrt(p.x * p.x + p.y * p.y);\n}\n\ndouble norm(const point& p) {\n\treturn p.x * p.x + p.y * p.y;\n}\n\ndouble dot(const point& l, const point& r) {\n\treturn l.x * r.x + l.y * r.y;\n}\n\ndouble cross(const point& l, const point& r) {\n\treturn l.x * r.y - l.y * r.x;\n}\n\nstruct line {\n\tpoint a, b;\n\tline(point a, point b):a(a), b(b){}\n};\n\nstruct segment {\n\tpoint a, b;\n\tsegment(point a, point b):a(a), b(b){}\n};\n\nstruct circle {\n\tpoint c;\n\tdouble r;\n\tcircle(point c, double r):c(c), r(r){}\n};\n\ntypedef vector<point> polygon;\n\nint ccw(const point& a, point b, point c) {\n\tb = b - a;\n\tc = c - a;\n\tif(cross(b, c) > EPS) return 1; // ccw\n\tif(cross(b, c) < -EPS) return -1; // cw\n\tif(dot(b, c) < 0) return 2; // c, a, b 順に一直線上\n\tif(norm(b) < norm(c)) return -2; // a, b, c 順に一直線上\n\treturn 0; //a, c, b 順で一直線上\n}\n\npoint projection(const line& l, const point& p) {\n\tconst point dif = l.b - l.a;\n\tconst double tmp = dot(p - l.a, dif) / norm(dif);\n\treturn l.a + dif * tmp;\n}\n\nbool intersect(const line& l, const line& m) {\n\treturn abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS;\n}\n\nbool intersect(const line& l, const segment& s) {\n\treturn cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;\n}\n\nbool intersect(const line& l, const point& p) {\n\treturn abs(ccw(l.a, l.b, p)) != -1;\n}\n\nbool intersect(const segment& s, const segment& t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n\t\tccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\nbool intersect(const segment& s, const point& p) {\n\treturn ccw(s.a, s.b, p) == 0;\n}\n\nbool intersect(const circle& c, const point& p) {\n\treturn abs(c.c - p) <= c.r + EPS;\n}\n\nbool intersect(const circle& c, const circle& d) {\n\treturn abs(c.c - d.c) <= c.r + d.r && abs(c.c - d.c) >= abs(c.r - d.r);\n}\n\ndouble dist(const line& l, const point& p) {\n\treturn abs(p - projection(l, p));\n}\n\ndouble dist(const line& l, const line& m) {\n\treturn intersect(l, m) ? 0 : dist(l, m.a);\n}\n\ndouble dist(const line& l, const segment& s) {\n\treturn intersect(l, s) ? 0 : min(dist(l, s.a), dist(l, s.b));\n}\n\ndouble dist(const segment& s, const point& p) {\n\tconst point tmp = projection(line(s.a, s.b), p);\n\treturn intersect(s, tmp) ? abs(tmp - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\ndouble dist(const segment& s, const segment& t) {\n\tif(intersect(s, t))\n\t\treturn 0;\n\treturn min(min(dist(s, t.a), dist(s, t.b)), min(dist(t, s.a), dist(t, s.b)));\n}\n\npoint crosspoint(const line& l, const line& m) {\n\tconst double tmp = cross(l.b - l.a, m.b - m.a);\n//\tif(abs(tmp) < EPS) // 平行\n//\t\treturn l.a;\n\treturn m.a + (m.b - m.a) * cross(l.b - l.a, l.a - m.a) * (1.0 / tmp);\n}\n\npoint crosspoint(const segment& s, const segment& t) {\n\tif(!intersect(s, t)) // 交点を持たない\n\t\treturn s.a; // 用改善\n\n\tconst double tmp = cross(s.b - s.a, t.b - t.a);\n\tif(abs(tmp) < EPS) { // 一直線上\n\t\tif(intersect(s, t.a)) return t.a;\n\t\tif(intersect(s, t.b)) return t.b;\n\t\tif(intersect(t, s.a)) return s.a;\n\t\treturn s.b;\n\t}\n\n\treturn t.a + (t.b - t.a) * cross(s.b - s.a, s.b - t.a) * (1.0 / tmp);\n}\n\nvector<point> crosspoint(const circle &c, const circle& d) {\n\tvector<point> res;\n\tif(abs(c.c - d.c) < EPS) // 中心の座標が同じ\n\t\treturn res;\n\n\tconst double tmp = abs(c.c - d.c);\n\tconst double rc = (tmp * tmp + c.r * c.r - d.r * d.r) / (tmp + tmp);\n\tconst double rs = sqrt(c.r * c.r - rc * rc);\n\tconst point diff = (d.c - c.c) / tmp;\n\tres.push_back(point(c.c + diff * point(rc, rs)));\n\tres.push_back(point(c.c + diff * point(rc, -rs)));\n\treturn res;\n}\n\nvector<point> crosspoint(const circle& c, const line& l) {\n\tvector<point> res;\n\tconst point h = projection(l, c.c);\n\tconst double d = abs(h - c.c);\n\tif(d > c.r - EPS) {\n\t\tres.push_back(h);\n\t}\n\telse if(d <= c.r - EPS) {\n\t\tpoint v = l.b - l.a;\n\t\tv = v * sqrt(c.r * c.r - d * d) / abs(v);\n\t\tres.push_back(h + v);\n\t\tres.push_back(h - v);\n\t}\n\n\treturn res;\n}\n\nvector<point> crosspoint(const circle& c, const segment& s) {\n\tvector<point> res;\n\tvector<point> tmp = crosspoint(c, line(s.a, s.b));\n\tfor(int i = 0; i < tmp.size(); ++i)\n\t\tif(intersect(s, tmp[i]))\n\t\t\tres.push_back(tmp[i]);\n\n\treturn res;\n}\n\ndouble areaTriangle(point a, point b, const point& c) {\n\ta = a - c;\n\tb = b - c;\n\treturn fabs(a.x * b.y - b.x * a.y) / 2.0;\n}\n\ndouble area(const polygon& p) {\n\tconst int num = p.size();\n\tif(num < 3)\n\t\treturn 0;\n\n\tif(num == 3)\n\t\treturn areaTriangle(p[0], p[1], p[2]);\n\n\tdouble res = cross(p[num - 1], p[0]);\n\tfor(int i = 1; i < num; ++i) {\n\t\tres += cross(p[i - 1], p[i]);\n\t}\n\n\treturn res * 0.5;\n}\n\n// L.aからL.bの方向を見た場合に,点aが左側に来る.\nline bisector(const point& a, const point& b) {\n\treturn line(point((a.x - a.y + b.x + b.y) / 2.0, (a.y + a.x + b.y - b.x) / 2.0),\n\t\t\t\tpoint((a.x + a.y + b.x - b.y) / 2.0, (a.y - a.x + b.y + b.x) / 2.0));\n}\n\n// L.aからL.bを向いた時の左側を残して切断する.\npolygon convex_cut(const polygon& p, const line& l) {\n\tconst int num = p.size();\n\tpolygon res;\n\tfor(int i = 0; i < num; ++i) {\n\t\tconst int next = (i + 1) % num;\n\t\tconst int d1 = ccw(l.a, l.b, p[i]);\n\t\tconst int d2 = ccw(l.a, l.b, p[next]);\n\t\tif(d1 != -1)\n\t\t\tres.push_back(p[i]);\n\n\t\tif(d1 * d2 == -1)\n\t\t\tres.push_back(crosspoint(l, line(p[i], p[next])));\n\t}\n\n\treturn res;\n}\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tpolygon init;\n\tinit.push_back(point(-30000, -30000));\n\tinit.push_back(point(30000, -30000));\n\tinit.push_back(point(30000, 30000));\n\tinit.push_back(point(-30000, 30000));\n\n\tfor(int n, m; cin >> n >> m, n;) {\n\t\tvector<point> pos;\n\t\tpos.reserve(n);\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tint x, y;\n\t\t\tcin >> x >> y;\n\t\t\tpos.push_back(point(x, y));\n\t\t}\n\n\t\tvector<polygon> voronoi(n, init);\n\t\tfor(int i = 0; i < n; ++i)\n\t\t\tfor(int j = 0; j < n; ++j)\n\t\t\t\tif(i != j)\n\t\t\t\t\tvoronoi[i] = convex_cut(voronoi[i], bisector(pos[i], pos[j]));\n\n\t\tvector<double> e(n, 0.0);\n\t\tfor(int i = 0; i < m; ++i) {\n\t\t\tint bx, by, dx, dy, score;\n\t\t\tcin >> bx >> by >> dx >> dy >> score;\n\n\t\t\tconst int s = 4 * dx * dy;\n\t\t\tpolygon range;\n\t\t\trange.push_back(point(bx - dx, by - dy));\n\t\t\trange.push_back(point(bx + dx, by - dy));\n\t\t\trange.push_back(point(bx + dx, by + dy));\n\t\t\trange.push_back(point(bx - dx, by + dy));\n\n\t\t\tfor(int j = 0; j < n; ++j) {\n\t\t\t\tpolygon p(voronoi[j]);\n\t\t\t\tfor(int k = 0; k < 4; ++k)\n\t\t\t\t\tp = convex_cut(p, line(range[k], range[(k + 1) % 4]));\n\t\t\t\te[j] += area(p) / s * score;\n\t\t\t}\n\t\t}\n\n\t\tprintf(\"%.10lf\\n\", *max_element(e.begin(), e.end()));\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1276, "score_of_the_acc": -0.0354, "final_rank": 2 }, { "submission_id": "aoj_1514_617011", "code_snippet": "#include<iostream>\n#include<complex>\n#include<vector>\n#include<algorithm>\n#include<cmath>\n#include<map>\n\n#define EPS (1e-10)\n#define EQ(a,b) (abs((a)-(b)) < EPS)\n#define fs first\n#define sc second\n#define pb push_back\n#define sz size()\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n\nusing namespace std;\n\ntypedef complex<double> P;\ntypedef pair<P,P> L;\ntypedef vector<P> Poly;\n\ninline double dot(P x,P y){return real(conj(x)*y);}\n\ninline double cross(P x,P y){return imag(conj(x)*y);}\n\n//for line(segment)\ninline int ccw(P a,P b,P c){\n b -= a;c -= a;\n if (cross(b,c)>EPS) return 1; //counter clockwise\n if (cross(b,c)<-EPS) return -1; //clockwise\n if (dot(b, c)<-EPS) return 2; //c--a--b on line\n if (abs(b)<abs(c)) return -2; //a--b--c on line\n return 0; //on segment\n}\n\ninline P line_cp(L a,L b){\n return a.fs+(a.sc-a.fs)*cross(b.sc-b.fs,b.fs-a.fs)/cross(b.sc-b.fs,a.sc-a.fs);\n}\n\n//for polygon\n\n//all vertex is already sorted.\ninline double area(Poly &p){\n if(p.size()<3)return 0;\n double res = cross(p[p.size()-1],p[0]);\n for(int i=1;i<p.size();i++)res += cross(p[i-1],p[i]);\n return res/2;\n}\n\n//all vertex is already sorted.\ninline Poly convex_cut(Poly &p,L l){\n Poly res;\n int n = p.size();\n for(int i=0;i<n;i++){\n int nxt = (i+1)%n;\n if(ccw(l.fs,l.sc,p[i]) != -1)res.pb(p[i]);\n if(ccw(l.fs,l.sc,p[i]) * ccw(l.fs,l.sc,p[nxt]) < 0){\n res.pb( line_cp(l, L(p[i],p[nxt]) ) );\n }\n }\n return res;\n}\n\nint main(){\n int n,m;\n int bx,by,dx,dy,s;\n P p[110];\n Poly vor[110];\n const int lim = 30000;\n double ans[110];\n\n while(cin >> n >> m,n+m){\n FOR(i,0,n)cin >> p[i].real() >> p[i].imag();\n \n FOR(i,0,n){\n ans[i] = 0.0;\n\n vor[i].resize(4);\n vor[i][0] = P(-lim,-lim); vor[i][1] = P(lim,-lim);\n vor[i][2] = P(lim,lim); vor[i][3] = P(-lim,lim);\n\n FOR(j,0,n){\n\tif(i!=j){\n\t P nor = (p[i]-p[j]) * P(0,-1);\n\t P mid = (p[i]+p[j])/2.0;\n\t vor[i] = convex_cut(vor[i],L(mid,mid+nor));\n\t}\n }\n }\n FOR(i,0,m){\n cin >> bx >> by >> dx >> dy >> s;\n double rect = 4*dx*dy;\n Poly cut(4);\n cut[0] = P(bx-dx,by-dy); cut[1] = P(bx+dx,by-dy);\n cut[2] = P(bx+dx,by+dy); cut[3] = P(bx-dx,by+dy);\n\t\n FOR(j,0,n){\n\tPoly tmp = vor[j];\n\tFOR(k,0,4){\n\t tmp = convex_cut(tmp,L(cut[k],cut[(k+1)%4]));\n\n\t}\n\tans[j] += area(tmp)/rect*s;\n }\n }\n double res = 0.0;\n FOR(i,0,n)res = max(res,ans[i]);\n cout.setf(ios::fixed); cout.precision(9);\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1340, "score_of_the_acc": -0.0881, "final_rank": 3 }, { "submission_id": "aoj_1514_617010", "code_snippet": "#include<iostream>\n#include<complex>\n#include<vector>\n#include<algorithm>\n#include<cmath>\n#include<map>\n\n#define EPS (1e-10)\n#define EQ(a,b) (abs((a)-(b)) < EPS)\n#define fs first\n#define sc second\n#define pb push_back\n#define sz size()\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n\nusing namespace std;\n\ntypedef complex<double> P;\ntypedef pair<P,P> L;\ntypedef vector<P> Poly;\n\ndouble dot(P x,P y){return real(conj(x)*y);}\n\ndouble cross(P x,P y){return imag(conj(x)*y);}\n\n//for line(segment)\nint ccw(P a,P b,P c){\n b -= a;c -= a;\n if (cross(b,c)>EPS) return 1; //counter clockwise\n if (cross(b,c)<-EPS) return -1; //clockwise\n if (dot(b, c)<-EPS) return 2; //c--a--b on line\n if (abs(b)<abs(c)) return -2; //a--b--c on line\n return 0; //on segment\n}\n\nP line_cp(L a,L b){\n return a.fs+(a.sc-a.fs)*cross(b.sc-b.fs,b.fs-a.fs)/cross(b.sc-b.fs,a.sc-a.fs);\n}\n\n//for polygon\n\n//all vertex is already sorted.\ndouble area(Poly p){\n if(p.size()<3)return 0;\n double res = cross(p[p.size()-1],p[0]);\n for(int i=1;i<p.size();i++)res += cross(p[i-1],p[i]);\n return res/2;\n}\n\n//all vertex is already sorted.\nPoly convex_cut(Poly p,L l){\n Poly res;\n int n = p.size();\n for(int i=0;i<n;i++){\n int nxt = (i+1)%n;\n if(ccw(l.fs,l.sc,p[i]) != -1)res.pb(p[i]);\n if(ccw(l.fs,l.sc,p[i]) * ccw(l.fs,l.sc,p[nxt]) < 0){\n res.pb( line_cp(l, L(p[i],p[nxt]) ) );\n }\n }\n return res;\n}\n\nint main(){\n int n,m;\n int bx,by,dx,dy,s;\n P p[110];\n Poly vor[110];\n const int lim = 30000;\n double ans[110];\n\n while(cin >> n >> m,n+m){\n FOR(i,0,n)cin >> p[i].real() >> p[i].imag();\n \n FOR(i,0,n){\n ans[i] = 0.0;\n\n vor[i].resize(4);\n vor[i][0] = P(-lim,-lim); vor[i][1] = P(lim,-lim);\n vor[i][2] = P(lim,lim); vor[i][3] = P(-lim,lim);\n\n FOR(j,0,n){\n\tif(i!=j){\n\t P nor = (p[i]-p[j]) * P(0,-1);\n\t P mid = (p[i]+p[j])/2.0;\n\t vor[i] = convex_cut(vor[i],L(mid,mid+nor));\n\t}\n }\n }\n FOR(i,0,m){\n cin >> bx >> by >> dx >> dy >> s;\n double rect = 4*dx*dy;\n Poly cut(4);\n cut[0] = P(bx-dx,by-dy); cut[1] = P(bx+dx,by-dy);\n cut[2] = P(bx+dx,by+dy); cut[3] = P(bx-dx,by+dy);\n\t\n FOR(j,0,n){\n\tPoly tmp = vor[j];\n\tFOR(k,0,4){\n\t tmp = convex_cut(tmp,L(cut[k],cut[(k+1)%4]));\n\n\t}\n\tans[j] += area(tmp)/rect*s;\n }\n }\n double res = 0.0;\n FOR(i,0,n)res = max(res,ans[i]);\n cout.setf(ios::fixed); cout.precision(9);\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1336, "score_of_the_acc": -0.1113, "final_rank": 4 }, { "submission_id": "aoj_1514_617008", "code_snippet": "#include<iostream>\n#include<complex>\n#include<vector>\n#include<algorithm>\n#include<cmath>\n#include<map>\n#include<list>\n\n#define EPS (1e-10)\n#define EQ(a,b) (abs((a)-(b)) < EPS)\n#define fs first\n#define sc second\n#define pb push_back\n#define sz size()\n#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n\nusing namespace std;\n\ntypedef complex<double> P;\ntypedef pair<P,P> L;\ntypedef vector<P> Poly;\ntypedef pair<P,double> C;\n\nconst double PI = acos(-1);\n\nnamespace std{\n bool operator<(const P &a,const P &b){\n return real(a)==real(b)?imag(a)<imag(b):real(a)<real(b);\n }\n}\n\n//for vector\nP unit(P p){return p / abs(p);}\n\npair<P,P> norm(P p){return make_pair(p*P(0,1),p*P(0,-1));}\n\ndouble dot(P x,P y){return real(conj(x)*y);}\n\ndouble cross(P x,P y){return imag(conj(x)*y);}\n\n//rotate a point counter-clockwise on the origin\nP rotate(P v,double s){\n return P(real(v)*cos(s) - imag(v)*sin(s), real(v)*sin(s) + imag(v)*cos(s) );\n}\n\n//for point set\nvector<P> convex_hull(vector<P> v){\n int n = v.sz, k = 0;\n sort(v.begin(),v.end());\n vector<P> r(2*n);\n for(int i=0;i<n;i++){\n while(k>1 && cross(r[k-1]-r[k-2],v[i]-r[k-1]) <= EPS)k--;\n r[k++] = v[i];\n }\n for(int i=n-2,t=k;i>=0;i--){\n while(k>t && cross(r[k-1]-r[k-2],v[i]-r[k-1]) <= EPS)k--;\n r[k++] = v[i];\n }\n r.resize(k-1);\n return r;\n}\n\ndouble caliper(Poly p){\n int n = p.sz;\n if(n==2)return abs(p[0]-p[1]);\n \n int i = 0, j = 0;\n for(int k=0;k<n;k++){\n if(!(p[i]<p[k]))i = k;\n if(p[j]<p[k])j = k;\n }\n\n double res = 0;\n int si = i, sj = j;\n while(i != sj || j != si){\n res = max(res,abs(p[i]-p[j]));\n if( cross(p[(i+1)%n] - p[i], p[(j+1)%n] - p[j]) < 0)i = (i+1)%n;\n else j = (j+1)%n;\n }\n return res;\n}\n\n//Minimam Cover Ball\n//Using: Call for \"solve\" with vector<P> v, which is a point set, as argument \nconst int dim = 2;\nstruct min_ball{\n P center,v[dim+1],c[dim+1];\n double radius2,z[dim+1],r[dim+1];\n list<P> ps;\n list<P>::iterator sup;\n int m;\n\n void solve(vector<P> v){\n m = 0;\n center = P(0,0);\n radius2 = -1;\n ps.clear();\n for(int i=0;i<v.sz;i++)ps.pb(v[i]);\n make_ball(ps.end());\n }\n\n void push(const P& p){\n if(!m){\n c[0] = p; r[0] = 0;\n }else{\n double e = dot(p-c[m-1],p-c[m-1]) - r[m-1];\n P d = v[m] = p-c[0];\n for(int i=1;i<m;i++)v[m] -= v[i]*dot(v[i],d)/z[i];\n z[m] = dot(v[m],v[m]);\n c[m] = c[m-1] + e*v[m]*(1.0/z[m]/2.0);\n r[m] = r[m-1] + e*e/z[m]/4.0;\n }\n center = c[m];\n radius2 = r[m]; m++;\n }\n void make_ball(list<P>::iterator i){\n sup = ps.begin();\n if(m == dim + 1)return;\n for(list<P>::iterator k = ps.begin();k!=i;){\n list<P>::iterator j = k++;\n if(dot(*j-center,*j-center) > radius2){\n push(*j);\n\tmake_ball(j); m--;\n\tif(sup == j)++sup;\n\tps.splice (ps.begin(),ps,j);\n }\n }\n }\n};\n\n//for triangle\n\n//return seta A\ndouble arg(P a,P b,P c){return acos(dot(b-a,c-a)/(abs(b-a)*abs(c-a)));}\ndouble arg(double a,double b,double c){return acos( (b*b+c*c-a*a)/(2*b*c) );}\n\ndouble area_tri(P a,P b,P c){return abs(cross(b-a,c-a)/2);}\n\nbool inter_tri(P a,P b,P c,P x){\n double sum = area_tri(a,b,x) + area_tri(b,c,x) + area_tri(c,a,x);\n if(abs(sum - area_tri(a,b,c)) > EPS)return false;\n return true;\n}\n\ndouble heron(double a,double b,double c){\n double s = (a+b+c)/2;\n return sqrt(s*(s-a)*(s-b)*(s-c));\n}\n\n//for line(segment)\nint ccw(P a,P b,P c){\n b -= a;c -= a;\n if (cross(b,c)>EPS) return 1; //counter clockwise\n if (cross(b,c)<-EPS) return -1; //clockwise\n if (dot(b, c)<-EPS) return 2; //c--a--b on line\n if (abs(b)<abs(c)) return -2; //a--b--c on line\n return 0; //on segment\n}\n\nbool orth(L a,L b){return abs(dot(a.fs-a.sc,b.fs-b.sc))<EPS;}\n\nbool para(L a,L b){return abs(cross(a.fs-a.sc,b.fs-b.sc))<EPS;}\n\ndouble line_dis(L a,P x){return abs(cross(a.sc-a.fs,x-a.fs))/abs(a.sc-a.fs);}\n\nP line_cp(L a,L b){\n return a.fs+(a.sc-a.fs)*cross(b.sc-b.fs,b.fs-a.fs)/cross(b.sc-b.fs,a.sc-a.fs);\n}\n\ndouble seg_p_dis(L a,P x){\n if(dot(a.sc-a.fs,x-a.fs)<EPS)return abs(x-a.fs);\n if(dot(a.fs-a.sc,x-a.sc)<EPS)return abs(x-a.sc);\n return abs(cross(a.sc-a.fs,x-a.fs))/abs(a.sc-a.fs);\n}\n\ndouble seg_seg_dis(L a,L b){\n double res = 1e10;\n res = min(res,seg_p_dis(a,b.fs));\n res = min(res,seg_p_dis(a,b.sc));\n res = min(res,seg_p_dis(b,a.fs));\n res = min(res,seg_p_dis(b,a.sc));\n return res;\n}\nbool is_cp(L a,L b){\n if(ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0)\n if(ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0)return true;\n return false;\n}\n\nP seg_cp(L a,L b){\n double d = abs(cross(b.sc-b.fs,a.fs-b.fs));\n return a.fs + (a.sc-a.fs)*( d /(d + abs(cross(b.sc-b.fs,a.sc-b.fs))) );\n}\n\n//for polygon\n\n//all vertex is already sorted.\ndouble area(Poly p){\n if(p.size()<3)return 0;\n double res = cross(p[p.size()-1],p[0]);\n for(int i=1;i<p.size();i++)res += cross(p[i-1],p[i]);\n return res/2;\n}\n\n//all vertexes are already sorted.\nbool inter_cp(Poly p,P x){\n if(p.empty())return false;\n\n int s = p.sz;\n double max = p[0].real();\n FOR(i,1,s)if(max < p[i].real())max = p[i].real();\n L h = L( x,P(max+1.0,x.imag()) );\n\n int c = 0;\n FOR(i,0,s){\n L l = L(p[i],p[(i+1)%s]);\n if(para(h,l) && abs(ccw(h.fs,h.sc,l.fs))!=1)c++;\n else if(is_cp(h,l))c++;\n }\n\n if(c&1)return true;\n return false;\n}\n\n//all inter-degrees are less than PI and all vertexes are already sorted.\nbool inter_pos(Poly p,P x){\n int s = p.sz;\n FOR(i,0,s)if(ccw(p[i],p[(i+1)%s],x)==-1)return false;\n return true;\n}\n\n//all vertex is already sorted.\nPoly convex_cut(Poly p,L l){\n Poly res;\n int n = p.size();\n for(int i=0;i<n;i++){\n int nxt = (i+1)%n;\n if(ccw(l.fs,l.sc,p[i]) != -1)res.pb(p[i]);\n if(ccw(l.fs,l.sc,p[i]) * ccw(l.fs,l.sc,p[nxt]) < 0){\n res.pb( line_cp(l, L(p[i],p[nxt]) ) );\n }\n }\n return res;\n}\n\nint main(){\n int n,m;\n int bx,by,dx,dy,s;\n P p[110];\n Poly vor[110];\n const int lim = 30000;\n double ans[110];\n\n while(cin >> n >> m,n+m){\n FOR(i,0,n)cin >> p[i].real() >> p[i].imag();\n \n FOR(i,0,n){\n ans[i] = 0.0;\n\n vor[i].resize(4);\n vor[i][0] = P(-lim,-lim); vor[i][1] = P(lim,-lim);\n vor[i][2] = P(lim,lim); vor[i][3] = P(-lim,lim);\n\n FOR(j,0,n){\n\tif(i!=j){\n\t P nor = (p[i]-p[j]) * P(0,-1);\n\t P mid = (p[i]+p[j])/2.0;\n\t vor[i] = convex_cut(vor[i],L(mid,mid+nor));\n\t}\n }\n }\n FOR(i,0,m){\n cin >> bx >> by >> dx >> dy >> s;\n double rect = 4*dx*dy;\n Poly cut(4);\n cut[0] = P(bx-dx,by-dy); cut[1] = P(bx+dx,by-dy);\n cut[2] = P(bx+dx,by+dy); cut[3] = P(bx-dx,by+dy);\n\t\n FOR(j,0,n){\n\tPoly tmp = vor[j];\n\tFOR(k,0,4){\n\t tmp = convex_cut(tmp,L(cut[k],cut[(k+1)%4]));\n\n\t}\n\tans[j] += area(tmp)/rect*s;\n }\n }\n double res = 0.0;\n FOR(i,0,n)res = max(res,ans[i]);\n cout.setf(ios::fixed); cout.precision(9);\n cout << res << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1348, "score_of_the_acc": -0.1165, "final_rank": 6 } ]
aoj_1522_cpp
Planarian Regeneration Problem 皆さん、「プラナリア」って知っていますか? プラナリアとは、日本では川の上流の石や枯葉などの裏に張り付いて生息している水生生物です。 プラナリアの最も優れた能力はその「再生能力」です。プラナリアの再生能力は著しく、例えば、三等分するとそれぞれが再生し、数週間後には元の完全な状態のプラナリアが3匹できあがります。 この度、会津の山奥の上流にて発見された新種のプラナリアの姿は長方形をしていて、プラナリアはある法則により再生します。 このとき、プラナリアは下の図のように配置し、2つの手段でプラナリアの切断実験を行います。 まず1つ目の手段として、垂直方向に切ることを考えます。一度の切断では、すべての断片を水平方向の長さが(断片の頭に近い部分):(断片の頭に遠い部分)= m : n になるように切断します。この動作を x 回繰り返します。 次に2つ目の手段として、水平方向に切ることを考えます。一度の切断では、すべての断片を垂直方向の長さが(断片の右端に近い部分):(断片の左端に近い部分)= k : l に切断します。この動作を y 回繰り返します。これらの切断によって断片が移動することはありません。 数週間後に、各断片が元の完全なプラナリアの状態に再生している確率は以下の式により求まります。 上の図は、垂直方向に1:2で2回、水平方向に1:1で1回切断した状態です。 以上の操作で切断した時の、数週間後に元の姿に再生しているプラナリアの数の期待値を求めてください。元のプラナリアの垂直方向の長さと水平方向の長さはともに1とします。 Input m n x k l y 入力では、6つの整数 m , n , x , k , l , y が上記の入力フォーマットで与えられます。 これら6つの整数は問題文中のものと対応しており、水平方向に m : n で切断する動作を x 回、垂直方向に k : l で切断する動作を y 回繰り返します。 1 ≤ m , n , k , l ≤ 100、 0 ≤ x , y ≤ 40 であり、 m , n と l , k はそれぞれ互いに素です。 Output 一行に数週間後に元の姿に再生しているプラナリアの数の期待値を出力してください。 出力は 10 -6 以下の誤差ならば許容されます。 Sample Input 1 1 1 1 1 1 1 Sample Output 1 0.562500 Sample Input 2 1 2 2 1 1 1 Sample Output 2 0.490741 Sample Input 3 1 2 0 3 4 0 Sample Output 3 1.000000 Notes この問題では、doubleよりも精度の高い浮動小数を使用することを推奨します。
[ { "submission_id": "aoj_1522_2102518", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-7;\n\nld getans(ld l,ld r, int b,ld c) {\n\tif (b == 0||abs(r-l)<eps) {\n\t\treturn (1-l)*(r - l);\n\t}\n\tld aa = getans(l,(1-c)*l+c*r, b - 1, c);\n\tld ab = getans((1 - c)*l + c*r,r, b - 1, c);\n\treturn aa + ab;\n}\n\nld get() {\n\tint m, n, x; cin >> m >> n >> x;\n\tld c = (ld(m)) / (m + n);\n\treturn getans(0,1, x, c);\n}\n\nint main() {\n\tcout << setprecision(10) << fixed << get()*get() << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 3256, "score_of_the_acc": -1.9804, "final_rank": 2 }, { "submission_id": "aoj_1522_2102517", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-7;\n\n//// < \"d:\\d_download\\visual studio 2015\\projects\\programing_contest_c++\\debug\\a.txt\" > \"d:\\d_download\\visual studio 2015\\projects\\programing_contest_c++\\debug\\b.txt\"\n\nld getans(ld l,ld r, int b,ld c) {\n\tif (b == 0||abs(r-l)<eps) {\n\t\treturn (1-l)*(r - l);\n\t}\n\tld aa = getans(l,(1-c)*l+c*r, b - 1, c);\n\tld ab = getans((1 - c)*l + c*r,r, b - 1, c);\n\treturn aa + ab;\n}\nld get() {\n\tint m, n, x; cin >> m >> n >> x;\n\tld c = (ld(m)) / (m + n);\n\tx = min(x, 40);\n\treturn getans(0,1, x, c);\n}\n\nint main() {\n\tld a = get();\n\tld b = get();\n\tcout << setprecision(10) << fixed << a*b << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 3252, "score_of_the_acc": -1.9979, "final_rank": 3 }, { "submission_id": "aoj_1522_1382407", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <unordered_map>\n#include <iterator>\n#include <functional>\n#include <cassert>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define REPS(i,x) for(int i=1;i<=(int)(x);i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RREPS(i,x) for(int i=((int)(x));i>0;i--)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) (container).begin(), (container).end()\n#define RALL(container) (container).rbegin(), (container).rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define pb push_back\n#define eb emplace_back\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\ntemplate<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}\ntemplate<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}\n\nconst int INF = 1<<28;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\n\ntypedef long double R;\nR m, n, x;\n\ntemplate<int N>\nstruct Comb{\n\tR C[N][N];\n\tComb(){\n\t\tmemset(C, 0, (int)sizeof(C));\t\n\t\tfor(int i = 0; i < 50; i++){\n\t\t\tC[i][0] = C[i][i] = 1;\n\t\t\tfor(int j = 1; j < i; j++) C[i][j] = C[i-1][j-1] + C[i-1][j];\n\t\t}\n\t}\n\tR operator()(int n, int r){\n\t\tif(n < 0 || r < 0 || r > n) return 0;\n\t\treturn C[n][r];\n\t}\n};\nComb<50> C;\n\nR solve(R l, R r, R x){\n\tR res = 0;\n\tREP(i, x+1){\t// i回左に行く\n\t\tR sum = C(x, i);\n\t\tREPS(j, x){\t// j回目の遷移\n\t\t\tREP(k, j){ // 既に j - 1 回中 k 回左に遷移している\n\t\t\t\tR w = pow(l, k) * pow(r, j-1-k);\n\t\t\t\tsum -= w*(l) * C(j-1, k) * C(x-j, i-k );\t// j回目に右に行く\n\t\t\t}\n\t\t}\n\t\tres += pow(l, i) * pow(r, x-i) * sum;\n\t}\n\treturn res;\n}\n\nint main(int argc, char *argv[]){\n\tios::sync_with_stdio(false);\n\tR ans = 1;\n\tREP(i, 2){\n\t\tcin >> m >> n >> x;\n\t\tans *= solve(m/(n+m), n/(n+m), x);\n\t}\n\tprintf(\"%.11f\\n\", (double)ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1316, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1515_cpp
Problem G: The Humans Braving the Invaders Problem 現在、地球は宇宙からの侵略者であるインベーダーの攻撃を受け、人類で生き残った者は基地にいる我々だけとなった。 奴らに対抗できる戦力もほとんど残されていない。 だが、ここで我々は諦めるということはしない。 インベーダを全滅させることが、我々人類が生き残る最後の手段である。 今から最後になるであろう作戦の内容を説明する。 まず、インベーダーの戦力に比べ我々の戦力はあまりにも少ないので、この基地に立てこもり、籠城戦を行う。 この基地は高い山に囲まれていて、インベーダーが侵攻するには基地正面にある真っ直ぐな道を通る他無い。 この道をフィールドと呼ぶことにする。この特徴により、前方に集中してインベーダーを攻撃することが可能である。 基地にいる我々はインベーダーに対し2種類の武器で攻撃を行う。 1つはインベーダー1体を狙撃するスナイパーライフルである。もう1つは広範囲に攻撃ができるグレネードランチャーである。 人類でたった1人のプログラマーである君の仕事は、インベーダーと我々の行動記録を元に戦闘をシミュレーションすることだ。 行動記録はクエリー形式で与えられる。各クエリーは1つまたは複数の整数からなり、次のように与えられる。 0 インベーダーがフィールド上の、基地からの距離 L の位置に出現する。 1 d 現在フィールド上にいるすべてのインベーダーが d だけ基地に近づく。 この動作の後、基地に到達したインベーダーからダメージを受け、また、それらの敵はフィールド上から消滅する。 ダメージを受けた場合は"damage (この時基地に到達したインベーダーの数)"を1行に出力する。 2 k 現在フィールド上にいるインベーダーの数が k 体以上の場合、基地に近い方から k 番目のインベーダーをスナイパーライフルで攻撃する。 そのインベーダーはフィールド上から消滅する。そして"hit"を1行に出力する。 フィールド上のインベーダーの数が k 体未満の場合、"miss"を1行に出力する。 3 x r 基地からの距離が x の位置に範囲 r のグレネードランチャーで攻撃する。基地からの距離が x の位置に着弾し、そこからの距離が r 以下の位置にいるすべてのインベーダーはフィールド上から消滅する。 そして"bomb (倒したインベーダーの数)"を1行に出力する。補足として、基地はグレネードランチャーのダメージを受けることはない。 4 k フィールド上のインベーダーの数が k 体以上の場合、"distance (基地に近い方からk番目のインベーダーと基地との距離)"を1行に出力する。 フィールド上のインベーダーの数が k 体未満の場合、"distance -1"を1行に出力する。 作戦の説明は以上だ。総員、作戦開始!・・・幸運を祈る。 Input 入力は複数のデータセットからなる。 各データセットは以下で表される。 1行目は2つの整数 Q , L がスペース区切りで与えられる。 続く Q 行には、上記で説明したクエリーが Q 個与えられる。 入力の終わりは2つのゼロからなる。 Constraints 入力は以下の条件を満たす。 入力に含まれる値はすべて整数 1 ≤ Q ≤ 100000 1 ≤ L ≤ 10 9 1 ≤ d , k ≤ 10 9 0 ≤ x ≤ L 0 ≤ r ≤ 10 9 同じ位置にインベーダーが2体以上存在することはない データセットの数は3個以下 Output 各データセットにつき、出力の指示があるクエリーに対して出力せよ。各データセットの最後には"end"を出力せよ。 Sample Input 18 10 0 4 1 1 1 4 1 0 1 1 0 2 2 1 10 0 1 1 0 1 1 0 1 5 3 4 0 3 4 1 0 9 10 4 1 2 2 3 5 5 0 4 1 2 2 3 5 5 0 2 1 0 0 Sample Output distance 10 distance 9 hit damage 2 bomb 1 bomb 2 end distance -1 miss bomb 0 distance 10 miss bomb 1 hit end
[ { "submission_id": "aoj_1515_8293599", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\nusing namespace std;\n\nclass BIT {\npublic:\n\tint size_ = 1;\n\tvector<int> dat;\n\n\tvoid init(int sz) {\n\t\tsize_ = sz + 2;\n\t\tdat.resize(size_ + 2, 0);\n\t}\n\n\tvoid add(int pos, int x) {\n\t\tpos += 1;\n\t\twhile (pos <= size_) {\n\t\t\tdat[pos] += x;\n\t\t\tpos += (pos & -pos);\n\t\t}\n\t}\n\n\tint sum(int pos) {\n\t\tint s = 0; pos += 1;\n\t\twhile (pos >= 1) {\n\t\t\ts += dat[pos];\n\t\t\tpos -= (pos & -pos);\n\t\t}\n\t\treturn s;\n\t}\n};\n\nlong long Q, T[100009], A[100009], B[100009];\nlong long L;\nvector<long long> Zahyou;\nset<pair<long long, long long>> Set;\nBIT Z;\n\nvoid Initialize() {\n\tZahyou.clear();\n\tSet.clear();\n\tZ.size_ = 1;\n\tZ.dat = vector<int>{};\n}\n\nint Binary_Search(int num) {\n\tint cl = 0, cr = Zahyou.size() + 1, cm, minx = (1 << 30);\n\tfor (int i = 0; i < 25; i++) {\n\t\tcm = (cl + cr) / 2;\n\t\tint r = Z.sum(cm);\n\t\tif (r >= num) { cr = cm; minx = min(minx, cm); }\n\t\telse { cl = cm; }\n\t}\n\treturn minx;\n}\n\n// 範囲 [cl, cr]\nvector<long long> SetRange(long long cl, long long cr) {\n\tvector<long long> ret;\n\twhile (true) {\n\t\tauto itr = Set.lower_bound(make_pair(cl, -1));\n\t\tif (itr == Set.end()) break;\n\t\tif ((*itr).first > cr) break;\n\t\tret.push_back((*itr).second);\n\t\tSet.erase(itr);\n\t}\n\treturn ret;\n}\n\nint main() {\n\twhile (true) {\n\t\tInitialize();\n\n\t\t// Step 1. Input\n\t\tcin >> Q >> L; if (Q + L == 0) break;\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tcin >> T[i];\n\t\t\tif (T[i] == 1) cin >> A[i];\n\t\t\tif (T[i] == 2) cin >> A[i];\n\t\t\tif (T[i] == 3) cin >> A[i] >> B[i];\n\t\t\tif (T[i] == 4) cin >> A[i];\n\t\t}\n\n\t\t// Step 2. Compression\n\t\tlong long Cursor = L;\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (T[i] == 0) Zahyou.push_back(Cursor);\n\t\t\tif (T[i] == 1) Cursor += A[i];\n\t\t}\n\t\tsort(Zahyou.begin(), Zahyou.end());\n\t\tZahyou.erase(unique(Zahyou.begin(), Zahyou.end()), Zahyou.end());\n\t\tZ.init(Zahyou.size() + 2);\n\n\t\t// Step 3. Query\n\t\tCursor = 0;\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (T[i] == 0) {\n\t\t\t\tint pos1 = lower_bound(Zahyou.begin(), Zahyou.end(), Cursor + L) - Zahyou.begin();\n\t\t\t\tSet.insert(make_pair(Cursor + L, pos1));\n\t\t\t\tZ.add(pos1, 1);\n\t\t\t}\n\n\t\t\t// 進撃\n\t\t\tif (T[i] == 1) {\n\t\t\t\tCursor += A[i];\n\t\t\t\tvector<long long> vec = SetRange(0, Cursor);\n\t\t\t\tfor (long long place : vec) { Z.add(place, -1); }\n\t\t\t\tif (vec.size() != 0) cout << \"damage \" << vec.size() << endl;\n\t\t\t}\n\n\t\t\t// 単体攻撃\n\t\t\tif (T[i] == 2) {\n\t\t\t\tint ret = Binary_Search(A[i]);\n\t\t\t\tif (ret == (1 << 30)) cout << \"miss\" << endl;\n\t\t\t\telse {\n\t\t\t\t\tZ.add(ret, -1);\n\t\t\t\t\tSet.erase(make_pair(Zahyou[ret], ret));\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 範囲攻撃\n\t\t\tif (T[i] == 3) {\n\t\t\t\tvector<long long> vec = SetRange(Cursor + A[i] - B[i], Cursor + A[i] + B[i]);\n\t\t\t\tfor (long long place : vec) { Z.add(place, -1); }\n\t\t\t\tcout << \"bomb \" << vec.size() << endl;\n\t\t\t}\n\n\t\t\t// 距離の確認\n\t\t\tif (T[i] == 4) {\n\t\t\t\tint ret = Binary_Search(A[i]);\n\t\t\t\tif (ret == (1 << 30)) cout << \"distance \" << -1 << endl;\n\t\t\t\telse cout << \"distance \" << Zahyou[ret] - Cursor << endl;\n\t\t\t}\n\t\t}\n\n\t\t// 最後\n\t\tcout << \"end\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 8308, "score_of_the_acc": -0.3067, "final_rank": 4 }, { "submission_id": "aoj_1515_8293597", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\nusing namespace std;\n\nclass BIT {\npublic:\n\tint size_ = 1;\n\tvector<int> dat;\n\n\tvoid init(int sz) {\n\t\tsize_ = sz + 2;\n\t\tdat.resize(size_ + 2, 0);\n\t}\n\n\tvoid add(int pos, int x) {\n\t\tpos += 1;\n\t\twhile (pos <= size_) {\n\t\t\tdat[pos] += x;\n\t\t\tpos += (pos & -pos);\n\t\t}\n\t}\n\n\tint sum(int pos) {\n\t\tint s = 0; pos += 1;\n\t\twhile (pos >= 1) {\n\t\t\ts += dat[pos];\n\t\t\tpos -= (pos & -pos);\n\t\t}\n\t\treturn s;\n\t}\n};\n\nint Q, T[100009], A[100009], B[100009];\nint L;\nvector<int> Zahyou;\nset<pair<int, int>> Set;\nBIT Z;\n\nvoid Initialize() {\n\tZahyou.clear();\n\tSet.clear();\n\tZ.size_ = 1;\n\tZ.dat = vector<int>{};\n}\n\nint Binary_Search(int num) {\n\tint cl = 0, cr = Zahyou.size() + 1, cm, minx = (1 << 30);\n\tfor (int i = 0; i < 25; i++) {\n\t\tcm = (cl + cr) / 2;\n\t\tint r = Z.sum(cm);\n\t\tif (r >= num) { cr = cm; minx = min(minx, cm); }\n\t\telse { cl = cm; }\n\t}\n\treturn minx;\n}\n\n// 範囲 [cl, cr]\nvector<int> SetRange(int cl, int cr) {\n\tvector<int> ret;\n\twhile (true) {\n\t\tauto itr = Set.lower_bound(make_pair(cl, -1));\n\t\tif (itr == Set.end()) break;\n\t\tif ((*itr).first > cr) break;\n\t\tret.push_back((*itr).second);\n\t\tSet.erase(itr);\n\t}\n\treturn ret;\n}\n\nint main() {\n\twhile (true) {\n\t\tInitialize();\n\n\t\t// Step 1. Input\n\t\tcin >> Q >> L; if (Q + L == 0) break;\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tcin >> T[i];\n\t\t\tif (T[i] == 1) cin >> A[i];\n\t\t\tif (T[i] == 2) cin >> A[i];\n\t\t\tif (T[i] == 3) cin >> A[i] >> B[i];\n\t\t\tif (T[i] == 4) cin >> A[i];\n\t\t}\n\n\t\t// Step 2. Compression\n\t\tint Cursor = L;\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (T[i] == 0) Zahyou.push_back(Cursor);\n\t\t\tif (T[i] == 1) Cursor += A[i];\n\t\t}\n\t\tsort(Zahyou.begin(), Zahyou.end());\n\t\tZahyou.erase(unique(Zahyou.begin(), Zahyou.end()), Zahyou.end());\n\t\tZ.init(Zahyou.size() + 2);\n\n\t\t// Step 3. Query\n\t\tCursor = 0;\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (T[i] == 0) {\n\t\t\t\tint pos1 = lower_bound(Zahyou.begin(), Zahyou.end(), Cursor + L) - Zahyou.begin();\n\t\t\t\tSet.insert(make_pair(Cursor + L, pos1));\n\t\t\t\tZ.add(pos1, 1);\n\t\t\t}\n\n\t\t\t// 進撃\n\t\t\tif (T[i] == 1) {\n\t\t\t\tCursor += A[i];\n\t\t\t\tvector<int> vec = SetRange(0, Cursor);\n\t\t\t\tfor (int place : vec) { Z.add(place, -1); }\n\t\t\t\tif (vec.size() != 0) cout << \"damage \" << vec.size() << endl;\n\t\t\t}\n\n\t\t\t// 単体攻撃\n\t\t\tif (T[i] == 2) {\n\t\t\t\tint ret = Binary_Search(A[i]);\n\t\t\t\tif (ret == (1 << 30)) cout << \"miss\" << endl;\n\t\t\t\telse {\n\t\t\t\t\tZ.add(ret, -1);\n\t\t\t\t\tSet.erase(make_pair(Zahyou[ret], ret));\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 範囲攻撃\n\t\t\tif (T[i] == 3) {\n\t\t\t\tvector<int> vec = SetRange(Cursor + A[i] - B[i], Cursor + A[i] + B[i]);\n\t\t\t\tfor (int place : vec) { Z.add(place, -1); }\n\t\t\t\tcout << \"bomb \" << vec.size() << endl;\n\t\t\t}\n\n\t\t\t// 距離の確認\n\t\t\tif (T[i] == 4) {\n\t\t\t\tint ret = Binary_Search(A[i]);\n\t\t\t\tif (ret == (1 << 30)) cout << \"distance \" << -1 << endl;\n\t\t\t\telse cout << \"distance \" << Zahyou[ret] - Cursor << endl;\n\t\t\t}\n\t\t}\n\n\t\t// 最後\n\t\tcout << \"end\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 60, "memory_kb": 6372, "score_of_the_acc": -0.1078, "final_rank": 11 }, { "submission_id": "aoj_1515_6034476", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n#define SIZE 100005\n\nint num_Q;\nll L,bit_N;\nll A[SIZE],table[SIZE];\nll BIT[SIZE];\nbool is_del[SIZE];\n\n\nll min_data[6*SIZE],add_data[6*SIZE];\nint N = 1;\n\nvoid bit_add(ll loc,ll value){\n\n\tBIT[loc] += value;\n\n\tloc += loc & -loc;\n\n\twhile(loc <= bit_N){\n\t\tBIT[loc] += value;\n\t\tloc += loc & -loc;\n\t}\n}\n\nll getSum(ll loc){\n\n\tif(loc == 0)return 0;\n\n\tll sum = BIT[loc];\n\n\tloc -= loc & -loc;\n\n\twhile(loc > 0){\n\t\tsum += BIT[loc];\n\t\tloc -= loc & -loc;\n\t}\n\treturn sum;\n}\n\nvoid init(ll first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(int left,int right,ll value,int node_id,int node_left,int node_right){\n\n\tif(right < node_left || left > node_right){\n\t\t//範囲外ならreturn\n\t\treturn;\n\t}\n\telse if(left <= node_left && right >= node_right){ //このノードのカバーしている区間が、更新区間の部分区間である場合\n\n\t\tadd_data[node_id] += value; //一様に加える値を加算\n\n\t\twhile(node_id != 0){\n\n\t\t\tnode_id = (node_id-1)/2; //下から上に向かって、最小値および最大値更新\n\t\t\tmin_data[node_id] = min(min_data[2*node_id+1]+add_data[2*node_id+1],min_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t}\n\t}else{\n\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t}\n}\n\nll getMin(int left,int right,int node_id,int node_left,int node_right){\n\tif(right < node_left || left > node_right)return HUGE_NUM;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn min_data[node_id]+add_data[node_id];\n\n\t}else{\n\n\t\tll left_min = getMin(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tll right_min = getMin(left,right,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t\treturn min(left_min,right_min)+add_data[node_id];\n\t}\n}\n\nint getPos(int num){\n\n\tint left = 1,right = bit_N,mid = (left+right)/2;\n\tint ret = -1;\n\n\twhile(left <= right){\n\n\t\tif(getSum(mid) >= num){\n\n\t\t\tret = mid;\n\t\t\tright = mid-1;\n\t\t}else{\n\n\t\t\tleft = mid+1;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tint first_N = 0;\n\n\tbit_N = num_Q;\n\n\tN = 1;\n\tinit(num_Q);\n\n\tfor(int i = 0; i <= bit_N; i++){\n\n\t\tBIT[i] = 0;\n\t\tis_del[i] = false;\n\t}\n\n\n\tfor(ll i = 0; i <= 2*N-2; i++){\n\t\tmin_data[i] = 0;\n\t\tadd_data[i] = 0;\n\t}\n\n\n\tint command;\n\tll dist;\n\tll k;\n\tll x,r;\n\n\tfor(int loop = 0; loop < num_Q; loop++){\n\n\t\tscanf(\"%d\",&command);\n\n\t\tif(command == 0){ //敵出現\n\n\t\t\tadd(first_N,first_N,L,0,0,N-1);\n\t\t\tbit_add(first_N+1,1);\n\t\t\tfirst_N++;\n\n\t\t}else if(command == 1){\n\n\t\t\tscanf(\"%lld\",&dist);\n\n\t\t\tadd(0,first_N-1,-dist,0,0,N-1);\n\n\t\t\t//二分探索で、0以下となる最大のindexを求める\n\t\t\tint left = 0,right = first_N-1,mid = (left+right)/2;\n\t\t\tint loc = -1;\n\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp <= 0){\n\n\t\t\t\t\tloc = mid;\n\t\t\t\t\tleft = mid+1;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(loc == -1){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//0~locまでで生きてるデータの数\n\t\t\tint tmp_sum = getSum(loc+1);\n\t\t\tif(tmp_sum == 0){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tprintf(\"damage %d\\n\",tmp_sum);\n\t\t\tfor(int i = 1; i <= loc+1; i++){ //論理削除\n\t\t\t\tif(is_del[i])continue;\n\t\t\t\tbit_add(i,-1);\n\t\t\t\tis_del[i] = true;\n\t\t\t}\n\n\t\t}else if(command == 2){\n\n\t\t\tscanf(\"%lld\",&k);\n\n\t\t\tif(getSum(bit_N) < k){\n\n\t\t\t\tprintf(\"miss\\n\");\n\t\t\t}else{\n\n\t\t\t\tint pos = getPos(k);\n\t\t\t\tprintf(\"hit\\n\");\n\n\t\t\t\tbit_add(pos,-1);\n\t\t\t\tis_del[pos] = true;\n\t\t\t}\n\n\t\t}else if(command == 3){\n\n\t\t\tscanf(\"%lld %lld\",&x,&r);\n\n\t\t\tint L = -1,R = -1;\n\n\t\t\tint left = 0,right = first_N-1,mid = (left+right)/2;\n\t\t\t//二分探索で、x-r以上となる最も左の位置を求める\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp >= x-r){\n\n\t\t\t\t\tL = mid;\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}else{\n\n\t\t\t\t\tleft = mid+1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(L == -1){\n\n\t\t\t\tprintf(\"bomb 0\\n\");\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tleft = 0,right = first_N-1,mid = (left+right)/2;\n\n\t\t\t//二分探索で、x+r以下となる最も右の位置を求める\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp <= x+r){\n\n\t\t\t\t\tR = mid;\n\t\t\t\t\tleft = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tll tmp = getSum(R+1)-getSum(L);\n\t\t\tprintf(\"bomb %lld\\n\",tmp);\n\n\t\t\tfor(int i = L+1; i <= R+1; i++){\n\n\t\t\t\tif(is_del[i])continue;\n\t\t\t\tbit_add(i,-1);\n\t\t\t\tis_del[i] = true;\n\t\t\t}\n\n\n\t\t}else{ //command == 4\n\n\t\t\tscanf(\"%lld\",&k);\n\t\t\tif(getSum(bit_N) < k){\n\n\t\t\t\tprintf(\"distance -1\\n\");\n\t\t\t}else{\n\n\t\t\t\tint pos = getPos(k);\n\t\t\t\tprintf(\"distance %lld\\n\",getMin(pos-1,pos-1,0,0,N-1));\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"end\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %lld\",&num_Q,&L);\n\t\tif(num_Q == 0 && L == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 8220, "score_of_the_acc": -0.7539, "final_rank": 7 }, { "submission_id": "aoj_1515_6034471", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n#define SIZE 100005\n\nint num_Q;\nll L,bit_N;\nll A[SIZE],table[SIZE];\nll BIT[SIZE];\nbool is_del[SIZE];\n\n\nll min_data[6*SIZE],add_data[6*SIZE];\nint N = 1;\n\nvoid bit_add(ll loc,ll value){\n\n\tBIT[loc] += value;\n\n\tloc += loc & -loc;\n\n\twhile(loc <= bit_N){\n\t\tBIT[loc] += value;\n\t\tloc += loc & -loc;\n\t}\n}\n\nll getSum(ll loc){\n\n\tif(loc == 0)return 0;\n\n\tll sum = BIT[loc];\n\n\tloc -= loc & -loc;\n\n\twhile(loc > 0){\n\t\tsum += BIT[loc];\n\t\tloc -= loc & -loc;\n\t}\n\treturn sum;\n}\n\nvoid init(ll first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(int left,int right,ll value,int node_id,int node_left,int node_right){\n\n\tif(right < node_left || left > node_right){\n\t\t//範囲外ならreturn\n\t\treturn;\n\t}\n\telse if(left <= node_left && right >= node_right){ //このノードのカバーしている区間が、更新区間の部分区間である場合\n\n\t\tadd_data[node_id] += value; //一様に加える値を加算\n\n\t\twhile(node_id != 0){\n\n\t\t\tnode_id = (node_id-1)/2; //下から上に向かって、最小値および最大値更新\n\t\t\tmin_data[node_id] = min(min_data[2*node_id+1]+add_data[2*node_id+1],min_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t}\n\t}else{\n\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t}\n}\n\nll getMin(int left,int right,int node_id,int node_left,int node_right){\n\tif(right < node_left || left > node_right)return HUGE_NUM;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn min_data[node_id]+add_data[node_id];\n\n\t}else{\n\n\t\tll left_min = getMin(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tll right_min = getMin(left,right,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t\treturn min(left_min,right_min)+add_data[node_id];\n\t}\n}\n\nint getPos(int num){\n\n\tint left = 1,right = bit_N,mid = (left+right)/2;\n\tint ret = -1;\n\n\twhile(left <= right){\n\n\t\tif(getSum(mid) >= num){\n\n\t\t\tret = mid;\n\t\t\tright = mid-1;\n\t\t}else{\n\n\t\t\tleft = mid+1;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tint first_N = 0;\n\n\tbit_N = num_Q;\n\n\tN = 1;\n\tinit(num_Q);\n\n\tfor(int i = 0; i <= bit_N; i++){\n\n\t\tBIT[i] = 0;\n\t\tis_del[i] = false;\n\t}\n\n\n\tfor(ll i = 0; i <= 2*N-2; i++){\n\t\tmin_data[i] = 0;\n\t\tadd_data[i] = 0;\n\t}\n\n\n\tint command;\n\tll dist;\n\tll k;\n\tll x,r;\n\n\tfor(int loop = 0; loop < num_Q; loop++){\n\n\t\tscanf(\"%d\",&command);\n\n\t\t//printf(\"\\nloop:%d\\n\",loop);\n\n\t\tif(command == 0){ //敵出現\n\n\t\t\tadd(first_N,first_N,L,0,0,N-1);\n\t\t\tbit_add(first_N+1,1);\n\t\t\tfirst_N++;\n\n\t\t}else if(command == 1){\n\n\t\t\tscanf(\"%lld\",&dist);\n\n\t\t\tadd(0,first_N-1,-dist,0,0,N-1);\n\n\t\t\t//二分探索で、0以下となる最大のindexを求める\n\t\t\tint left = 0,right = first_N-1,mid = (left+right)/2;\n\t\t\tint loc = -1;\n\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp <= 0){\n\n\t\t\t\t\tloc = mid;\n\t\t\t\t\tleft = mid+1;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(loc == -1){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//0~locまでで生きてるデータの数\n\t\t\tint tmp_sum = getSum(loc+1);\n\t\t\tif(tmp_sum == 0){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//printf(\"loc:%d\\n\",loc);\n\t\t\tprintf(\"damage %d\\n\",tmp_sum);\n\t\t\tfor(int i = 1; i <= loc+1; i++){ //論理削除\n\t\t\t\tif(is_del[i])continue;\n\t\t\t\tbit_add(i,-1);\n\t\t\t\tis_del[i] = true;\n\t\t\t}\n\n\t\t}else if(command == 2){\n\n\t\t\tscanf(\"%lld\",&k);\n\n\t\t\tif(getSum(bit_N) < k){\n\n\t\t\t\tprintf(\"miss\\n\");\n\t\t\t}else{\n\n\t\t\t\tint pos = getPos(k);\n\t\t\t\tprintf(\"hit\\n\");\n\n\t\t\t\tbit_add(pos,-1);\n\t\t\t\tis_del[pos] = true;\n\t\t\t}\n\n\t\t}else if(command == 3){\n\n\t\t\tscanf(\"%lld %lld\",&x,&r);\n\n\t\t\tint L = -1,R = -1;\n\n\t\t\tint left = 0,right = first_N-1,mid = (left+right)/2;\n\t\t\t//二分探索で、x-r以上となる最も左の位置を求める\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp >= x-r){\n\n\t\t\t\t\tL = mid;\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}else{\n\n\t\t\t\t\tleft = mid+1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(L == -1){\n\n\t\t\t\tprintf(\"bomb 0\\n\");\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tleft = 0,right = first_N-1,mid = (left+right)/2;\n\n\t\t\t//二分探索で、x+r以下となる最も右の位置を求める\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp <= x+r){\n\n\t\t\t\t\tR = mid;\n\t\t\t\t\tleft = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tll tmp = getSum(R+1)-getSum(L);\n\t\t\tprintf(\"bomb %lld\\n\",tmp);\n\n\t\t\tfor(int i = L+1; i <= R+1; i++){\n\n\t\t\t\tif(is_del[i])continue;\n\t\t\t\tbit_add(i,-1);\n\t\t\t\tis_del[i] = true;\n\t\t\t}\n\n\n\t\t}else{ //command == 4\n\n\t\t\tscanf(\"%lld\",&k);\n\t\t\tif(getSum(bit_N) < k){\n\n\t\t\t\tprintf(\"distance -1\\n\");\n\t\t\t}else{\n\n\t\t\t\tint pos = getPos(k);\n\t\t\t\tprintf(\"distance %lld\\n\",getMin(pos-1,pos-1,0,0,N-1));\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"end\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %lld\",&num_Q,&L);\n\t\tif(num_Q == 0 && L == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 8216, "score_of_the_acc": -0.7538, "final_rank": 6 }, { "submission_id": "aoj_1515_6034442", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n#define SIZE 100005\n\nint num_Q;\nll L,bit_N;\nll A[SIZE],table[SIZE];\nll BIT[SIZE];\nbool is_del[SIZE];\n\n\nll min_data[6*SIZE],add_data[6*SIZE];\nint N = 1;\n\nvoid bit_add(ll loc,ll value){\n\n\tBIT[loc] += value;\n\n\tloc += loc & -loc;\n\n\twhile(loc <= bit_N){\n\t\tBIT[loc] += value;\n\t\tloc += loc & -loc;\n\t}\n}\n\nll getSum(ll loc){\n\n\tif(loc == 0)return 0;\n\n\tll sum = BIT[loc];\n\n\tloc -= loc & -loc;\n\n\twhile(loc > 0){\n\t\tsum += BIT[loc];\n\t\tloc -= loc & -loc;\n\t}\n\treturn sum;\n}\n\nvoid init(ll first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(int left,int right,ll value,int node_id,int node_left,int node_right){\n\n\tif(right < node_left || left > node_right){\n\t\t//範囲外ならreturn\n\t\treturn;\n\t}\n\telse if(left <= node_left && right >= node_right){ //このノードのカバーしている区間が、更新区間の部分区間である場合\n\n\t\tadd_data[node_id] += value; //一様に加える値を加算\n\n\t\twhile(node_id != 0){\n\n\t\t\tnode_id = (node_id-1)/2; //下から上に向かって、最小値および最大値更新\n\t\t\tmin_data[node_id] = min(min_data[2*node_id+1]+add_data[2*node_id+1],min_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t}\n\t}else{\n\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t}\n}\n\nll getMin(int left,int right,int node_id,int node_left,int node_right){\n\tif(right < node_left || left > node_right)return HUGE_NUM;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn min_data[node_id]+add_data[node_id];\n\n\t}else{\n\n\t\tll left_min = getMin(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tll right_min = getMin(left,right,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t\treturn min(left_min,right_min)+add_data[node_id];\n\t}\n}\n\nint getPos(int num){\n\n\tint left = 1,right = bit_N,mid = (left+right)/2;\n\tint ret = -1;\n\n\twhile(left <= right){\n\n\t\tif(getSum(mid) >= num){\n\n\t\t\tret = mid;\n\t\t\tright = mid-1;\n\t\t}else{\n\n\t\t\tleft = mid+1;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tint first_N = 0;\n\n\tbit_N = num_Q;\n\n\tN = 1;\n\tinit(num_Q);\n\n\tfor(int i = 0; i <= bit_N; i++){\n\n\t\tBIT[i] = 0;\n\t\tis_del[i] = false;\n\t}\n\n\n\tfor(ll i = 0; i <= 2*N-2; i++){\n\t\tmin_data[i] = 0;\n\t\tadd_data[i] = 0;\n\t}\n\n\n\tint command;\n\tll dist;\n\tll k;\n\tll x,r;\n\n\tfor(int loop = 0; loop < num_Q; loop++){\n\n\t\tscanf(\"%d\",&command);\n\n\t\t//printf(\"\\nloop:%d\\n\",loop);\n\n\t\tif(command == 0){ //敵出現\n\n\t\t\tadd(first_N,first_N,L,0,0,N-1);\n\t\t\tbit_add(first_N+1,1);\n\t\t\tfirst_N++;\n\n\t\t}else if(command == 1){\n\n\t\t\tscanf(\"%lld\",&dist);\n\n\t\t\tadd(0,first_N-1,-dist,0,0,N-1);\n\n\t\t\t//二分探索で、0以下となる最大のindexを求める\n\t\t\tint left = 0,right = first_N-1,mid = (left+right)/2;\n\t\t\tint loc = -1;\n\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp <= 0){\n\n\t\t\t\t\tloc = mid;\n\t\t\t\t\tleft = mid+1;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(loc == -1){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//0~locまでで生きてるデータの数\n\t\t\tint tmp_sum = getSum(loc+1);\n\t\t\tif(tmp_sum == 0){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//printf(\"loc:%d\\n\",loc);\n\t\t\tprintf(\"damage %d\\n\",tmp_sum);\n\t\t\tfor(int i = 1; i <= tmp_sum; i++){ //論理削除\n\t\t\t\tif(is_del[i])continue;\n\t\t\t\tbit_add(i,-1);\n\t\t\t\tis_del[i] = true;\n\t\t\t}\n\n\t\t}else if(command == 2){\n\n\t\t\tscanf(\"%lld\",&k);\n\n\t\t\tif(getSum(bit_N) < k){\n\n\t\t\t\tprintf(\"miss\\n\");\n\t\t\t}else{\n\n\t\t\t\tint pos = getPos(k);\n\t\t\t\tprintf(\"hit\\n\");\n\t\t\t\tbit_add(pos,-1);\n\t\t\t}\n\n\t\t}else if(command == 3){\n\n\t\t\tscanf(\"%lld %lld\",&x,&r);\n\n\t\t\tint L = -1,R = -1;\n\n\t\t\tint left = 0,right = first_N-1,mid = (left+right)/2;\n\t\t\t//二分探索で、x-r以上となる最も左の位置を求める\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp >= x-r){\n\n\t\t\t\t\tL = mid;\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}else{\n\n\t\t\t\t\tleft = mid+1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(L == -1){\n\n\t\t\t\tprintf(\"bomb 0\\n\");\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tleft = 0,right = first_N-1,mid = (left+right)/2;\n\n\t\t\t//二分探索で、x+r以下となる最も右の位置を求める\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp <= x+r){\n\n\t\t\t\t\tR = mid;\n\t\t\t\t\tleft = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tll tmp = getSum(R+1)-getSum(L);\n\t\t\tprintf(\"bomb %lld\\n\",tmp);\n\n\t\t\tfor(int i = L+1; i <= R+1; i++){\n\n\t\t\t\tif(is_del[i])continue;\n\t\t\t\tbit_add(i,-1);\n\t\t\t\tis_del[i] = true;\n\t\t\t}\n\n\n\t\t}else{ //command == 4\n\n\t\t\tscanf(\"%lld\",&k);\n\t\t\tif(getSum(bit_N) < k){\n\n\t\t\t\tprintf(\"distance -1\\n\");\n\t\t\t}else{\n\n\t\t\t\tint pos = getPos(k);\n\t\t\t\tprintf(\"distance %lld\\n\",getMin(pos-1,pos-1,0,0,N-1));\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"end\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %lld\",&num_Q,&L);\n\t\tif(num_Q == 0 && L == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 290, "memory_kb": 8144, "score_of_the_acc": -0.3466, "final_rank": 15 }, { "submission_id": "aoj_1515_6034431", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\n\n\n#define SIZE 100005\n\nint num_Q;\nll L,bit_N;\nll A[SIZE],table[SIZE];\nll BIT[SIZE];\nbool is_del[SIZE];\n\n\nll min_data[6*SIZE],add_data[6*SIZE];\nint N = 1;\n\nvoid bit_add(ll loc,ll value){\n\n\tBIT[loc] += value;\n\n\tloc += loc & -loc;\n\n\twhile(loc <= bit_N){\n\t\tBIT[loc] += value;\n\t\tloc += loc & -loc;\n\t}\n}\n\nll getSum(ll loc){\n\n\tif(loc == 0)return 0;\n\n\tll sum = BIT[loc];\n\n\tloc -= loc & -loc;\n\n\twhile(loc > 0){\n\t\tsum += BIT[loc];\n\t\tloc -= loc & -loc;\n\t}\n\treturn sum;\n}\n\nvoid init(ll first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(int left,int right,ll value,int node_id,int node_left,int node_right){\n\n\tif(right < node_left || left > node_right){\n\t\t//範囲外ならreturn\n\t\treturn;\n\t}\n\telse if(left <= node_left && right >= node_right){ //このノードのカバーしている区間が、更新区間の部分区間である場合\n\n\t\tadd_data[node_id] += value; //一様に加える値を加算\n\n\t\twhile(node_id != 0){\n\n\t\t\tnode_id = (node_id-1)/2; //下から上に向かって、最小値および最大値更新\n\t\t\tmin_data[node_id] = min(min_data[2*node_id+1]+add_data[2*node_id+1],min_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t}\n\t}else{\n\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t}\n}\n\nll getMin(int left,int right,int node_id,int node_left,int node_right){\n\tif(right < node_left || left > node_right)return BIG_NUM;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn min_data[node_id]+add_data[node_id];\n\n\t}else{\n\n\t\tll left_min = getMin(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tll right_min = getMin(left,right,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t\treturn min(left_min,right_min)+add_data[node_id];\n\t}\n}\n\nint getPos(int num){\n\n\tint left = 1,right = bit_N,mid = (left+right)/2;\n\tint ret = -1;\n\n\twhile(left <= right){\n\n\t\tif(getSum(mid) >= num){\n\n\t\t\tret = mid;\n\t\t\tright = mid-1;\n\t\t}else{\n\n\t\t\tleft = mid+1;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tint first_N = 0;\n\n\tbit_N = num_Q;\n\n\tN = 1;\n\tinit(num_Q);\n\n\tfor(int i = 0; i <= bit_N; i++){\n\n\t\tBIT[i] = 0;\n\t\tis_del[i] = false;\n\t}\n\n\n\tfor(ll i = 0; i <= 2*N-2; i++){\n\t\tmin_data[i] = 0;\n\t\tadd_data[i] = 0;\n\t}\n\n\n\tint command;\n\tll dist;\n\tll k;\n\tll x,r;\n\n\tfor(int loop = 0; loop < num_Q; loop++){\n\n\t\tscanf(\"%d\",&command);\n\n\t\t//printf(\"\\nloop:%d\\n\",loop);\n\n\t\tif(command == 0){ //敵出現\n\n\t\t\tadd(first_N,first_N,L,0,0,N-1);\n\t\t\tbit_add(first_N+1,1);\n\t\t\tfirst_N++;\n\n\t\t}else if(command == 1){\n\n\t\t\tscanf(\"%lld\",&dist);\n\n\t\t\tadd(0,first_N-1,-dist,0,0,N-1);\n\n\t\t\t//二分探索で、0以下となる最大のindexを求める\n\t\t\tint left = 0,right = first_N-1,mid = (left+right)/2;\n\t\t\tint loc = -1;\n\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp <= 0){\n\n\t\t\t\t\tloc = mid;\n\t\t\t\t\tleft = mid+1;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(loc == -1){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//0~locまでで生きてるデータの数\n\t\t\tint tmp_sum = getSum(loc+1);\n\t\t\tif(tmp_sum == 0){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//printf(\"loc:%d\\n\",loc);\n\t\t\tprintf(\"damage %d\\n\",tmp_sum);\n\t\t\tfor(int i = 1; i <= tmp_sum; i++){ //論理削除\n\t\t\t\tif(is_del[i])continue;\n\t\t\t\tbit_add(i,-1);\n\t\t\t\tis_del[i] = true;\n\t\t\t}\n\n\t\t}else if(command == 2){\n\n\t\t\tscanf(\"%lld\",&k);\n\n\t\t\tif(getSum(bit_N) < k){\n\n\t\t\t\tprintf(\"miss\\n\");\n\t\t\t}else{\n\n\t\t\t\tint pos = getPos(k);\n\t\t\t\tprintf(\"hit\\n\");\n\t\t\t\tbit_add(pos,-1);\n\t\t\t}\n\n\t\t}else if(command == 3){\n\n\t\t\tscanf(\"%lld %lld\",&x,&r);\n\n\t\t\tint L = -1,R = -1;\n\n\t\t\tint left = 0,right = first_N-1,mid = (left+right)/2;\n\t\t\t//二分探索で、x-r以上となる最も左の位置を求める\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp >= x-r){\n\n\t\t\t\t\tL = mid;\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}else{\n\n\t\t\t\t\tleft = mid+1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tif(L == -1){\n\n\t\t\t\tprintf(\"bomb 0\\n\");\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tleft = 0,right = first_N-1,mid = (left+right)/2;\n\n\t\t\t//二分探索で、x+r以下となる最も右の位置を求める\n\t\t\twhile(left <= right){\n\n\t\t\t\tll tmp = getMin(mid,mid,0,0,N-1);\n\t\t\t\tif(tmp <= x+r){\n\n\t\t\t\t\tR = mid;\n\t\t\t\t\tleft = mid+1;\n\t\t\t\t}else{\n\n\t\t\t\t\tright = mid-1;\n\t\t\t\t}\n\t\t\t\tmid = (left+right)/2;\n\t\t\t}\n\n\t\t\tll tmp = getSum(R+1)-getSum(L);\n\t\t\tprintf(\"bomb %lld\\n\",tmp);\n\n\t\t\tfor(int i = L+1; i <= R+1; i++){\n\n\t\t\t\tif(is_del[i])continue;\n\t\t\t\tbit_add(i,-1);\n\t\t\t\tis_del[i] = true;\n\t\t\t}\n\n\n\t\t}else{ //command == 4\n\n\t\t\tscanf(\"%lld\",&k);\n\t\t\tif(getSum(bit_N) < k){\n\n\t\t\t\tprintf(\"distance -1\\n\");\n\t\t\t}else{\n\n\t\t\t\tint pos = getPos(k);\n\t\t\t\tprintf(\"distance %lld\\n\",getMin(pos-1,pos-1,0,0,N-1));\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"end\\n\");\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d %lld\",&num_Q,&L);\n\t\tif(num_Q == 0 && L == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 280, "memory_kb": 8172, "score_of_the_acc": -0.34, "final_rank": 14 }, { "submission_id": "aoj_1515_3373623", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i,n) REP(i,0,n)\n#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)\n#define repr(i, n) REPR(i, n, 0)\n#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)\n#define pb push_back\n#define all(r) r.begin(),r.end()\n#define rall(r) r.rbegin(),r.rend()\n#define fi first\n#define se second\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\nconst ll MOD = 1e9 + 7;\ndouble EPS = 1e-8;\n\n\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n#include<ext/pb_ds/tag_and_trait.hpp>\nusing namespace __gnu_pbds;\n\nusing Tree = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int Q, L;\n while(cin >> Q >> L) {\n if(Q == 0 && L == 0) break;\n Tree st;\n\n ll cur = 0LL;\n\n rep(i, Q) {\n\n // cout << i << \" cur \" << cur << \" st[\";\n // for(auto&& j : st) cout << j << \", \";\n // cout << \"]\" << endl;\n\n int query;\n cin >> query;\n if (query == 0) {\n st.insert(L + cur);\n } else if (query == 1) {\n ll d;\n cin >> d;\n cur += d;\n int cnt = 0;\n while (st.size() > 0 && *st.begin() <= cur) {\n cnt++;\n st.erase(st.begin());\n }\n if(cnt > 0 ) cout << \"damage \" << cnt << '\\n';\n } else if (query == 2) {\n int k;\n cin >> k;\n auto it = st.find_by_order(k - 1);\n if (it == st.end()) cout << \"miss\" << '\\n';\n else {\n cout << \"hit\" << '\\n';\n st.erase(it);\n }\n } else if (query == 3) {\n ll x, r;\n cin >> x >> r;\n ll L = cur + x - r, R = cur + x + r;\n int cnt = 0;\n auto it = st.lower_bound(L);\n while (it != st.end() && *it <= R) {\n ++cnt;\n it = st.erase(it);\n }\n cout << \"bomb \" << cnt << endl;\n } else {\n int k;\n cin >> k;\n auto it = st.find_by_order(k - 1);\n if (it == st.end()) cout << \"distance -1\" << '\\n';\n else {\n cout << \"distance \" << (*it) - cur << '\\n';\n }\n }\n }\n cout << \"end\" << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 6316, "score_of_the_acc": -0.121, "final_rank": 2 }, { "submission_id": "aoj_1515_2886686", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate< typename T >\nclass Treap {\nprivate: // parts\n\tunsigned int rnd() {\n\t\tstatic unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n\t\tunsigned int t = (x ^ (x << 11));\n\t\tx = y; y = z; z = w;\n\t\treturn (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n\t}\n\t\n\tstruct Node {\n\t\tT val, data, add;\n\t\tNode *lch, *rch;\n\t\tint pri, cnt;\n\t\tbool rev;\n\t\t\n\t\tNode(T v, int p) : val(v), data(v), add(T(0)), pri(p), cnt(1), rev(false) { lch = rch = nullptr; }\n\t};\n\t\n\tNode *root;\n\t\nprivate: // main function\n\tint count(Node* t) { return t ? t->cnt : 0; }\n\tT data(Node* t) { return t ? t->data : T(1e9); }\t\n\t\n\tvoid push(Node* t) {\n\t\tif (t && t->rev) {\n\t\t\tswap(t->lch, t->rch);\n\t\t\tif (t->lch) t->lch->rev ^= true;\n\t\t\tif (t->rch) t->rch->rev ^= true;\n\t\t\tt->rev = false;\n\t\t}\n\t\t\n\t\tif (t && t->add != 0) {\n\t\t\tt->val += t->add;\n\t\t\tif (t->lch) t->lch->add += t->add;\n\t\t\tif (t->rch) t->rch->add += t->add;\n\t\t\tt->add = 0;\n\t\t}\n\t}\n\t\n\tNode* update(Node *t) {\n\t\tif (!t) return t;\n\t\tt->cnt = count(t->lch) + count(t->rch) + 1;\n\t\t//t->data = calc(calc(data(t->lch), data(t->rch)), t->val);\n\t\treturn t;\n\t}\n\t\n\tNode* merge(Node* l, Node* r) {\n\t\tpush(l); push(r);\n\t\t\n\t\tif (!l || !r) return l ? l : r;\n\t\n\t\tif (l->pri > r->pri) {\n\t\t\tl->rch = merge(l->rch, r);\n\t\t\treturn update(l);\n\t\t} else {\n\t\t\tr->lch = merge(l, r->lch);\n\t\t\treturn update(r);\n\t\t}\n\t}\n\t\n\ttypedef pair< Node*, Node* > pnn;\n\t\n\tpnn split(Node* t, int k) {\n\t\tif (!t) return pnn(nullptr, nullptr);\n\t\t\n\t\tpush(t);\n\t\t\n\t\tif (k <= count(t->lch)) {\n\t\t\tpnn s = split(t->lch, k);\n\t\t\tt->lch = s.second;\n\t\t\treturn pnn(s.first, update(t));\n\t\t} else {\n\t\t\tpnn s = split(t->rch, k - count(t->lch) - 1);\n\t\t\tt->rch = s.first;\n\t\t\treturn pnn(update(t), s.second);\n\t\t}\n\t}\n\t\n\tNode* insert(Node* t, int k, T val, int pri) {\n\t\tpnn s = split(t, k);\n\t\tt = merge(s.first, new Node(val, pri));\n\t\tt = merge(t, s.second);\n\t\treturn update(t);\n\t}\n\t\n\tNode* erase(Node* t, int k) {\n\t\tpnn s1 = split(t, k + 1);\n\t\tpnn s2 = split(s1.first, k);\n\t\tt = merge(s2.first, s1.second);\n\t\tdelete s2.second;\n\t\treturn update(t);\n\t}\n\t\n\tNode* find(Node* t, int k) {\n\t\tpush(t);\n\t\tint c = count(t->lch);\n\t\tif (k < c) return find(t->lch, k);\n\t\tif (k > c) return find(t->rch, k - c - 1);\n\t\treturn t;\n\t}\n\t\n\tvoid dump(Node* t, ostream& os) {\n\t\tif (!t) return;\n\t\tos << \"(\";\n\t\tdump(t->lch, os);\n\t\tos << t->val;\n\t\tdump(t->rch, os);\n\t\tos << \")\";\n\t}\n\t\npublic: // public function\n\tTreap() : root(nullptr) {}\n\t\n\tvoid insert(int k, T val) { root = insert(root, k, val, rnd()); }\n\tvoid erase(int k) { root = erase(root, k); }\n\tNode* find(int k) { return find(root, k); }\n\tint size() { return count(root); }\n\t\n\tvoid dump(ostream& os) {\n\t\tdump(root, os);\n\t\tos << endl;\n\t}\n\t\n\t~Treap() { while (root) erase(0); }\n};\n\nint Q, L;\n\nvoid run() {\n\tlong long base = 0;\n\tTreap< long long > treap;\n\t\n\tauto lowerBound = [&](long long x) {\n\t\tint lb = -1, ub = treap.size();\n\t\twhile (ub - lb > 1) {\n\t\t\tint med = (ub + lb) / 2;\n\t\t\tif (treap.find(med)->val < x) lb = med;\n\t\t\telse ub = med;\n\t\t}\n\t\treturn ub;\n\t};\n\t\n\tauto upperBound = [&](long long x) {\n\t\tint lb = -1, ub = treap.size();\n\t\twhile (ub - lb > 1) {\n\t\t\tint med = (ub + lb) / 2;\n\t\t\tif (treap.find(med)->val <= x) lb = med;\n\t\t\telse ub = med;\n\t\t}\n\t\treturn ub;\n\t};\n\t\n\tfor (int i=0; i<Q; ++i) {\n\t\tint t;\n\t\tscanf(\"%d\", &t);\n\t\t\n\t\tif (t == 0) {\n\t\t\ttreap.insert((int)treap.size(), base + L);\n\t\t}\n\t\t\n\t\tif (t == 1) {\n\t\t\tint d;\n\t\t\tscanf(\"%d\\n\", &d);\n\t\t\tbase += d;\n\t\t\tint num = upperBound(base);\n\t\t\tif (num > 0) printf(\"damage %d\\n\", num);\n\t\t\tfor (int i=0; i<num; ++i) treap.erase(0);\n\t\t}\n\t\t\n\t\tif (t == 2) {\n\t\t\tint k;\n\t\t\tscanf(\"%d\\n\", &k);\n\t\t\tif ((int)treap.size() >= k) puts(\"hit\");\n\t\t\telse puts(\"miss\");\n\t\t\ttreap.erase(k-1);\n\t\t}\n\t\t\n\t\tif (t == 3) {\n\t\t\tint x, r;\n\t\t\tscanf(\"%d %d\\n\", &x, &r);\n\t\t\tint lft = lowerBound(base + x - r), rgt = upperBound(base + x + r);\n\t\t\tprintf(\"bomb %d\\n\", rgt - lft);\n\t\t\tfor (int i=0; i<rgt-lft; ++i) treap.erase(lft);\n\t\t}\n\t\t\n\t\tif (t == 4) {\n\t\t\tint k;\n\t\t\tscanf(\"%d\\n\", &k);\n\t\t\tif ((int)treap.size() >= k) printf(\"distance %d\\n\", (int)(treap.find(k-1)->val - base));\n\t\t\telse puts(\"distance -1\");\n\t\t}\n\t}\n\t\n\tputs(\"end\");\n}\n\nint main() {\n\twhile (cin >> Q >> L, Q) {\n\t\trun();\n\t}\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 6300, "score_of_the_acc": -0.151, "final_rank": 3 }, { "submission_id": "aoj_1515_2886684", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate< typename T >\nclass Treap {\nprivate: // parts\n\tunsigned int rnd() {\n\t\tstatic unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n\t\tunsigned int t = (x ^ (x << 11));\n\t\tx = y; y = z; z = w;\n\t\treturn (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n\t}\n\t\n\tstruct Node {\n\t\tT val, data, add;\n\t\tNode *lch, *rch;\n\t\tint pri, cnt;\n\t\tbool rev;\n\t\t\n\t\tNode(T v, int p) : val(v), data(v), add(T(0)), pri(p), cnt(1), rev(false) { lch = rch = nullptr; }\n\t};\n\t\n\tNode *root;\n\t\nprivate: // main function\n\tint count(Node* t) { return t ? t->cnt : 0; }\n\tT data(Node* t) { return t ? t->data : T(1e9); }\t\n\t\n\tvoid push(Node* t) {\n\t\tif (t && t->rev) {\n\t\t\tswap(t->lch, t->rch);\n\t\t\tif (t->lch) t->lch->rev ^= true;\n\t\t\tif (t->rch) t->rch->rev ^= true;\n\t\t\tt->rev = false;\n\t\t}\n\t\t\n\t\tif (t && t->add != 0) {\n\t\t\tt->val += t->add;\n\t\t\tif (t->lch) t->lch->add += t->add;\n\t\t\tif (t->rch) t->rch->add += t->add;\n\t\t\tt->add = 0;\n\t\t}\n\t}\n\t\n\tNode* update(Node *t) {\n\t\tif (!t) return t;\n\t\tt->cnt = count(t->lch) + count(t->rch) + 1;\n\t\t//t->data = calc(calc(data(t->lch), data(t->rch)), t->val);\n\t\treturn t;\n\t}\n\t\n\tNode* merge(Node* l, Node* r) {\n\t\tpush(l); push(r);\n\t\t\n\t\tif (!l || !r) return l ? l : r;\n\t\n\t\tif (l->pri > r->pri) {\n\t\t\tl->rch = merge(l->rch, r);\n\t\t\treturn update(l);\n\t\t} else {\n\t\t\tr->lch = merge(l, r->lch);\n\t\t\treturn update(r);\n\t\t}\n\t}\n\t\n\ttypedef pair< Node*, Node* > pnn;\n\t\n\tpnn split(Node* t, int k) {\n\t\tif (!t) return pnn(nullptr, nullptr);\n\t\t\n\t\tpush(t);\n\t\t\n\t\tif (k <= count(t->lch)) {\n\t\t\tpnn s = split(t->lch, k);\n\t\t\tt->lch = s.second;\n\t\t\treturn pnn(s.first, update(t));\n\t\t} else {\n\t\t\tpnn s = split(t->rch, k - count(t->lch) - 1);\n\t\t\tt->rch = s.first;\n\t\t\treturn pnn(update(t), s.second);\n\t\t}\n\t}\n\t\n\tNode* insert(Node* t, int k, T val, int pri) {\n\t\tpnn s = split(t, k);\n\t\tt = merge(s.first, new Node(val, pri));\n\t\tt = merge(t, s.second);\n\t\treturn update(t);\n\t}\n\t\n\tNode* erase(Node* t, int k) {\n\t\tpnn s1 = split(t, k + 1);\n\t\tpnn s2 = split(s1.first, k);\n\t\tt = merge(s2.first, s1.second);\n\t\tdelete s2.second;\n\t\treturn update(t);\n\t}\n\t\n\tNode* find(Node* t, int k) {\n\t\tpush(t);\n\t\tint c = count(t->lch);\n\t\tif (k < c) return find(t->lch, k);\n\t\tif (k > c) return find(t->rch, k - c - 1);\n\t\treturn t;\n\t}\n\t\n\tvoid dump(Node* t, ostream& os) {\n\t\tif (!t) return;\n\t\tos << \"(\";\n\t\tdump(t->lch, os);\n\t\tos << t->val;\n\t\tdump(t->rch, os);\n\t\tos << \")\";\n\t}\n\t\npublic: // public function\n\tTreap() : root(nullptr) {}\n\t\n\tvoid insert(int k, T val) { root = insert(root, k, val, rnd()); }\n\tvoid erase(int k) { root = erase(root, k); }\n\tNode* find(int k) { return find(root, k); }\n\tint size() { return count(root); }\n\t\n\tvoid dump(ostream& os) {\n\t\tdump(root, os);\n\t\tos << endl;\n\t}\n\t\n\t~Treap() { while (root) erase(0); }\n};\n\nint Q, L;\n\nvoid run() {\n\tlong long base = 0;\n\tTreap< long long > treap;\n\t\n\tauto lowerBound = [&](int x) {\n\t\tint lb = -1, ub = treap.size();\n\t\twhile (ub - lb > 1) {\n\t\t\tint med = (ub + lb) / 2;\n\t\t\tif (treap.find(med)->val < x) lb = med;\n\t\t\telse ub = med;\n\t\t}\n\t\treturn ub;\n\t};\n\t\n\tauto upperBound = [&](int x) {\n\t\tint lb = -1, ub = treap.size();\n\t\twhile (ub - lb > 1) {\n\t\t\tint med = (ub + lb) / 2;\n\t\t\tif (treap.find(med)->val <= x) lb = med;\n\t\t\telse ub = med;\n\t\t}\n\t\treturn ub;\n\t};\n\t\n\tfor (int i=0; i<Q; ++i) {\n\t\tint t;\n\t\tscanf(\"%d\", &t);\n\t\t\n\t\tif (t == 0) {\n\t\t\ttreap.insert((int)treap.size(), base + L);\n\t\t}\n\t\t\n\t\tif (t == 1) {\n\t\t\tint d;\n\t\t\tscanf(\"%d\\n\", &d);\n\t\t\tbase += d;\n\t\t\tint num = upperBound(base);\n\t\t\tif (num > 0) printf(\"damage %d\\n\", num);\n\t\t\tfor (int i=0; i<num; ++i) treap.erase(0);\n\t\t}\n\t\t\n\t\tif (t == 2) {\n\t\t\tint k;\n\t\t\tscanf(\"%d\\n\", &k);\n\t\t\tif ((int)treap.size() >= k) puts(\"hit\");\n\t\t\telse puts(\"miss\");\n\t\t\ttreap.erase(k-1);\n\t\t}\n\t\t\n\t\tif (t == 3) {\n\t\t\tint x, r;\n\t\t\tscanf(\"%d %d\\n\", &x, &r);\n\t\t\tint lft = lowerBound(base + x - r), rgt = upperBound(base + x + r);\n\t\t\tprintf(\"bomb %d\\n\", rgt - lft);\n\t\t\tfor (int i=0; i<rgt-lft; ++i) treap.erase(lft);\n\t\t}\n\t\t\n\t\tif (t == 4) {\n\t\t\tint k;\n\t\t\tscanf(\"%d\\n\", &k);\n\t\t\tif ((int)treap.size() >= k) printf(\"distance %d\\n\", (int)(treap.find(k-1)->val - base));\n\t\t\telse puts(\"distance -1\");\n\t\t}\n\t}\n\t\n\tputs(\"end\");\n}\n\nint main() {\n\twhile (cin >> Q >> L, Q) {\n\t\trun();\n\t}\n}", "accuracy": 0.15384615384615385, "time_ms": 100, "memory_kb": 6300, "score_of_the_acc": -0.1357, "final_rank": 12 }, { "submission_id": "aoj_1515_2886682", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate< typename T >\nclass Treap {\nprivate: // parts\n\tunsigned int rnd() {\n\t\tstatic unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n\t\tunsigned int t = (x ^ (x << 11));\n\t\tx = y; y = z; z = w;\n\t\treturn (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n\t}\n\t\n\tstruct Node {\n\t\tT val, data, add;\n\t\tNode *lch, *rch;\n\t\tint pri, cnt;\n\t\tbool rev;\n\t\t\n\t\tNode(T v, int p) : val(v), data(v), add(T(0)), pri(p), cnt(1), rev(false) { lch = rch = nullptr; }\n\t};\n\t\n\tNode *root;\n\t\nprivate: // main function\n\tint count(Node* t) { return t ? t->cnt : 0; }\n\tT data(Node* t) { return t ? t->data : T(1e9); }\t\n\t\n\tvoid push(Node* t) {\n\t\tif (t && t->rev) {\n\t\t\tswap(t->lch, t->rch);\n\t\t\tif (t->lch) t->lch->rev ^= true;\n\t\t\tif (t->rch) t->rch->rev ^= true;\n\t\t\tt->rev = false;\n\t\t}\n\t\t\n\t\tif (t && t->add != 0) {\n\t\t\tt->val += t->add;\n\t\t\tif (t->lch) t->lch->add += t->add;\n\t\t\tif (t->rch) t->rch->add += t->add;\n\t\t\tt->add = 0;\n\t\t}\n\t}\n\t\n\tNode* update(Node *t) {\n\t\tif (!t) return t;\n\t\tt->cnt = count(t->lch) + count(t->rch) + 1;\n\t\t//t->data = calc(calc(data(t->lch), data(t->rch)), t->val);\n\t\treturn t;\n\t}\n\t\n\tNode* merge(Node* l, Node* r) {\n\t\tpush(l); push(r);\n\t\t\n\t\tif (!l || !r) return l ? l : r;\n\t\n\t\tif (l->pri > r->pri) {\n\t\t\tl->rch = merge(l->rch, r);\n\t\t\treturn update(l);\n\t\t} else {\n\t\t\tr->lch = merge(l, r->lch);\n\t\t\treturn update(r);\n\t\t}\n\t}\n\t\n\ttypedef pair< Node*, Node* > pnn;\n\t\n\tpnn split(Node* t, int k) {\n\t\tif (!t) return pnn(nullptr, nullptr);\n\t\t\n\t\tpush(t);\n\t\t\n\t\tif (k <= count(t->lch)) {\n\t\t\tpnn s = split(t->lch, k);\n\t\t\tt->lch = s.second;\n\t\t\treturn pnn(s.first, update(t));\n\t\t} else {\n\t\t\tpnn s = split(t->rch, k - count(t->lch) - 1);\n\t\t\tt->rch = s.first;\n\t\t\treturn pnn(update(t), s.second);\n\t\t}\n\t}\n\t\n\tNode* insert(Node* t, int k, T val, int pri) {\n\t\tpnn s = split(t, k);\n\t\tt = merge(s.first, new Node(val, pri));\n\t\tt = merge(t, s.second);\n\t\treturn update(t);\n\t}\n\t\n\tNode* erase(Node* t, int k) {\n\t\tpnn s1 = split(t, k + 1);\n\t\tpnn s2 = split(s1.first, k);\n\t\tt = merge(s2.first, s1.second);\n\t\tdelete s2.second;\n\t\treturn update(t);\n\t}\n\t\n\tNode* find(Node* t, int k) {\n\t\tpush(t);\n\t\tint c = count(t->lch);\n\t\tif (k < c) return find(t->lch, k);\n\t\tif (k > c) return find(t->rch, k - c - 1);\n\t\treturn t;\n\t}\n\t\n\tvoid dump(Node* t, ostream& os) {\n\t\tif (!t) return;\n\t\tos << \"(\";\n\t\tdump(t->lch, os);\n\t\tos << t->val;\n\t\tdump(t->rch, os);\n\t\tos << \")\";\n\t}\n\t\npublic: // public function\n\tTreap() : root(nullptr) {}\n\t\n\tvoid insert(int k, T val) { root = insert(root, k, val, rnd()); }\n\tvoid erase(int k) { root = erase(root, k); }\n\tNode* find(int k) { return find(root, k); }\n\tint size() { return count(root); }\n\t\n\tvoid dump(ostream& os) {\n\t\tdump(root, os);\n\t\tos << endl;\n\t}\n\t\n\t~Treap() { while (root) erase(0); }\n};\n\nint Q, L;\n\nvoid run() {\n\tlong long base = 0;\n\tTreap< long long > treap;\n\t\n\tauto upperBound = [&](int x) {\n\t\tint lb = -1, ub = treap.size();\n\t\twhile (ub - lb > 1) {\n\t\t\tint med = (ub + lb) / 2;\n\t\t\tif (treap.find(med)->val <= x) lb = med;\n\t\t\telse ub = med;\n\t\t}\n\t\treturn ub;\n\t};\n\t\n\tfor (int i=0; i<Q; ++i) {\n\t\tint t;\n\t\tscanf(\"%d\", &t);\n\t\t\n\t\tif (t == 0) {\n\t\t\ttreap.insert((int)treap.size(), base + L);\n\t\t}\n\t\t\n\t\tif (t == 1) {\n\t\t\tint d;\n\t\t\tscanf(\"%d\\n\", &d);\n\t\t\tbase += d;\n\t\t\tint num = upperBound(base);\n\t\t\tif (num > 0) printf(\"damage %d\\n\", num);\n\t\t\tfor (int i=0; i<num; ++i) treap.erase(0);\n\t\t}\n\t\t\n\t\tif (t == 2) {\n\t\t\tint k;\n\t\t\tscanf(\"%d\\n\", &k);\n\t\t\tif ((int)treap.size() >= k) puts(\"hit\");\n\t\t\telse puts(\"miss\");\n\t\t\ttreap.erase(k-1);\n\t\t}\n\t\t\n\t\tif (t == 3) {\n\t\t\tint x, r;\n\t\t\tscanf(\"%d %d\\n\", &x, &r);\n\t\t\tint lft = upperBound(base + x - r - 1), rgt = upperBound(base + x + r);\n\t\t\tprintf(\"bomb %d\\n\", rgt - lft);\n\t\t\tfor (int i=0; i<rgt-lft; ++i) treap.erase(lft);\n\t\t}\n\t\t\n\t\tif (t == 4) {\n\t\t\tint k;\n\t\t\tscanf(\"%d\\n\", &k);\n\t\t\tif ((int)treap.size() >= k) printf(\"distance %d\\n\", (int)(treap.find(k-1)->val - base));\n\t\t\telse puts(\"distance -1\");\n\t\t}\n\t}\n\t\n\tputs(\"end\");\n}\n\nint main() {\n\twhile (cin >> Q >> L, Q) {\n\t\trun();\n\t}\n}", "accuracy": 0.15384615384615385, "time_ms": 100, "memory_kb": 6368, "score_of_the_acc": -0.1382, "final_rank": 13 }, { "submission_id": "aoj_1515_2469572", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\ntypedef int Value1;\ntypedef pair<bool,int>Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(make_pair(false, 0));\n\nstruct Node {\n\tValue1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.\n\tValue2 lazy;\t//?????¶???????????????????????????????????????\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\tif (l.first || r.first)return make_pair(1, -1);\n\t\telse {\n\t\t\treturn make_pair(0, l.second + r.second);\n\t\t}\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\t\tif (dat[k].lazy.first)dat[k].sum = 0;\n\t\telse {\n\t\t\tdat[k].sum += dat[k].lazy.second*(b - a);\n\t\t}\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inline??????????????¨??§???????????¨????????????!(??????)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//?¬???????????????¬??????????????£?????????.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\\????????¨????????????????????????????????????????????????????????§if???????????????.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r)?????´??°??????. ?????????0-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\n\t\tif (b <= l || r <= a) //[a,b)??¨[l,r)???????????????????????´???\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r)???[a,b)????????¨???????????§????????´???\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r)?????????????????¨?????????????????????. ?????????0-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\t\n\t\tif (b <= l || r <= a) //[a,b)??¨[l,r)???????????????????????´???\n\t\t\treturn Zero1;\n\t\n\t\tif (l <= a && b <= r) { // [l,r)???[a,b)????????¨???????????§????????´???\n\t\t\treturn dat[k].sum;\n\t\t}\n\t\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nstruct query {\n\tint type;\n\tlong long int a;\n\tlong long int b;\n};\ntemplate<typename T> struct Compress {\n\tmap<T, int>mp;\n\tmap<int, T>revmp;\n\n\tCompress(vector<T>vs) {\n\t\tsetmp(vs);\n\t}\n\n\tCompress() :mp(), revmp() {\n\n\t}\n\tvoid setmp(vector<T>vs) {\n\t\tsort(vs.begin(), vs.end());\n\t\tvs.erase(unique(vs.begin(), vs.end()), vs.end());\n\t\tfor (int i = 0; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tmp[vs[i]] = i;\n\t\t\trevmp[i] = vs[i];\n\t\t}\n\t}\n\n};\n\nint main() {\n\tint Q;\n\twhile (cin>>Q,Q) {\n\n\n\t\tlong long int L; cin >> L;\n\t\tCompress<long long int> cp;\n\t\tvector<query>qs;\n\t\t{\n\n\t\t\tvector<long long int>xs;\n\t\t\txs.push_back(0);\n\t\t\tlong long int nowdis = 0;\n\t\t\tfor (int i = 0; i < Q; ++i) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a == 0) {\n\t\t\t\t\txs.push_back(nowdis + L);\n\t\t\t\t\tqs.push_back(query{ a,-1,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 1) {\n\t\t\t\t\tint d; cin >> d;\n\t\t\t\t\tnowdis += d;\n\t\t\t\t\txs.push_back(nowdis);\n\t\t\t\t\tqs.push_back(query{ a,d,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 2) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 3) {\n\t\t\t\t\tint x, r; cin >> x >> r;\n\t\t\t\t\txs.push_back(nowdis + x + r);\n\t\t\t\t\txs.push_back(nowdis + x - r);\n\t\t\t\t\tqs.push_back(query{ a,nowdis + x - r,nowdis + x + r });\n\t\t\t\t}\n\t\t\t\telse if (a == 4) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t}\n\t\t\tcp.setmp(xs);\n\t\t\t\n\t\t}\n\t\tlazy_segtree seg(cp.mp.size());\n\t\tlong long int nowx = 0;\n\t\tfor (auto q : qs) {\n\t\t\tswitch (q.type) {\n\t\t\tcase 0:\n\t\t\t\tseg.update(cp.mp[nowx + L], cp.mp[nowx + L] + 1, make_pair(0, 1));\n\t\t\t\tbreak;\n\t\t\tcase 1: {\n\n\t\t\t\tint damage = seg.get(cp.mp[nowx] + 1, cp.mp[nowx + q.a] + 1);\n\t\t\t\tseg.update(cp.mp[nowx], cp.mp[nowx + q.a] + 1, make_pair(1, 0));\n\t\t\t\tif (damage) {\n\t\t\t\t\tcout << \"damage\" << \" \" << damage << endl;\n\t\t\t\t}\n\t\t\t\tnowx += q.a;\n\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 1, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t\tseg.update(cp.mp[nowx] + 1+ amax, cp.mp[nowx] + 2+ amax, make_pair(0, -1));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"miss\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t{\n\n\t\t\t\tint bomb = seg.get(cp.mp[q.a], cp.mp[q.b] + 1);\n\t\t\t\tcout << \"bomb \" << bomb << endl;\n\t\t\t\tseg.update(cp.mp[q.a], cp.mp[q.b] + 1, make_pair(1, 0));\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 1, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tlong long int dis = cp.revmp[cp.mp[nowx] +1+ amax] - nowx;\n\t\t\t\t\tcout << \"distance \" << dis << endl;\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"distance -1\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tassert(false);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t\tassert(cp.mp.size() == cp.revmp.size());\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1340, "memory_kb": 31188, "score_of_the_acc": -1.9715, "final_rank": 8 }, { "submission_id": "aoj_1515_2469566", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\ntypedef int Value1;\ntypedef pair<bool,int>Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(make_pair(false, 0));\n\nstruct Node {\n\tValue1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.\n\tValue2 lazy;\t//?????¶???????????????????????????????????????\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\tif (l.first || r.first)return make_pair(1, -1);\n\t\telse {\n\t\t\treturn make_pair(0, l.second + r.second);\n\t\t}\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\t\tif (dat[k].lazy.first)dat[k].sum = 0;\n\t\telse {\n\t\t\tdat[k].sum += dat[k].lazy.second*(b - a);\n\t\t}\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inline??????????????¨??§???????????¨????????????!(??????)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//?¬???????????????¬??????????????£?????????.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\\????????¨????????????????????????????????????????????????????????§if???????????????.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r]?????´??°??????. ?????????1-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 0, int b = -1) {\n\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\n\t\tif (b <= l || r <= a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r]?????????????????¨?????????????????????. ?????????1-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 1, int b = -1) {\n\tif (b == -1)b = N;\n\t\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\t\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn Zero1;\n\t\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\treturn dat[k].sum;\n\t\t}\n\t\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nstruct query {\n\tint type;\n\tlong long int a;\n\tlong long int b;\n};\ntemplate<typename T> struct Compress {\n\tmap<T, int>mp;\n\tmap<int, T>revmp;\n\n\tCompress(vector<T>vs) {\n\t\tsetmp(vs);\n\t}\n\n\tCompress() :mp(), revmp() {\n\n\t}\n\tvoid setmp(vector<T>vs) {\n\t\tsort(vs.begin(), vs.end());\n\t\tvs.erase(unique(vs.begin(), vs.end()), vs.end());\n\t\tfor (int i = 0; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tmp[vs[i]] = i;\n\t\t\trevmp[i] = vs[i];\n\t\t}\n\t}\n\n};\n\nint main() {\n\tint Q;\n\twhile (cin>>Q,Q) {\n\n\n\t\tlong long int L; cin >> L;\n\t\tCompress<long long int> cp;\n\t\tvector<query>qs;\n\t\t{\n\n\t\t\tvector<long long int>xs;\n\t\t\txs.push_back(0);\n\t\t\tlong long int nowdis = 0;\n\t\t\tfor (int i = 0; i < Q; ++i) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a == 0) {\n\t\t\t\t\txs.push_back(nowdis + L);\n\t\t\t\t\tqs.push_back(query{ a,-1,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 1) {\n\t\t\t\t\tint d; cin >> d;\n\t\t\t\t\tnowdis += d;\n\t\t\t\t\txs.push_back(nowdis);\n\t\t\t\t\tqs.push_back(query{ a,d,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 2) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 3) {\n\t\t\t\t\tint x, r; cin >> x >> r;\n\t\t\t\t\txs.push_back(nowdis + x + r);\n\t\t\t\t\txs.push_back(nowdis + x - r);\n\t\t\t\t\tqs.push_back(query{ a,nowdis + x - r,nowdis + x + r });\n\t\t\t\t}\n\t\t\t\telse if (a == 4) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t}\n\t\t\tcp.setmp(xs);\n\t\t\t\n\t\t}\n\t\tlazy_segtree seg(cp.mp.size());\n\t\tlong long int nowx = 0;\n\t\tfor (auto q : qs) {\n\t\t\tswitch (q.type) {\n\t\t\tcase 0:\n\t\t\t\tseg.update(cp.mp[nowx + L], cp.mp[nowx + L] + 1, make_pair(0, 1));\n\t\t\t\tbreak;\n\t\t\tcase 1: {\n\n\t\t\t\tint damage = seg.get(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1);\n\t\t\t\tseg.update(cp.mp[nowx], cp.mp[nowx + q.a] + 1, make_pair(1, 0));\n\t\t\t\tif (damage) {\n\t\t\t\t\tcout << \"damage\" << \" \" << damage << endl;\n\t\t\t\t}\n\t\t\t\tnowx += q.a;\n\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t\tseg.update(cp.mp[nowx] + 1+ amax, cp.mp[nowx] + 2+ amax, make_pair(0, -1));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"miss\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t{\n\n\t\t\t\tint bomb = seg.get(cp.mp[q.a] + 1, cp.mp[q.b] + 1);\n\t\t\t\tcout << \"bomb \" << bomb << endl;\n\t\t\t\tseg.update(cp.mp[q.a], cp.mp[q.b] + 1, make_pair(1, 0));\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tlong long int dis = cp.revmp[cp.mp[nowx] +1+ amax] - nowx;\n\t\t\t\t\tcout << \"distance \" << dis << endl;\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"distance -1\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tassert(false);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t\tassert(cp.mp.size() == cp.revmp.size());\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1350, "memory_kb": 31188, "score_of_the_acc": -1.9792, "final_rank": 9 }, { "submission_id": "aoj_1515_2469526", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\ntypedef int Value1;\ntypedef pair<bool,int>Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(make_pair(false, 0));\n\nstruct Node {\n\tValue1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.\n\tValue2 lazy;\t//?????¶???????????????????????????????????????\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\tif (l.first || r.first)return make_pair(1, -1);\n\t\telse {\n\t\t\treturn make_pair(0, l.second + r.second);\n\t\t}\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\t\tif (dat[k].lazy.first)dat[k].sum = 0;\n\t\telse {\n\t\t\tdat[k].sum += dat[k].lazy.second*(b - a + 1);\n\t\t}\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inline??????????????¨??§???????????¨????????????!(??????)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//?¬???????????????¬??????????????£?????????.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\\????????¨????????????????????????????????????????????????????????§if???????????????.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r]?????´??°??????. ?????????1-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r]?????????????????¨?????????????????????. ?????????1-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\t\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn Zero1;\n\t\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\treturn dat[k].sum;\n\t\t}\n\t\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nstruct query {\n\tint type;\n\tlong long int a;\n\tlong long int b;\n};\ntemplate<typename T> struct Compress {\n\tmap<T, int>mp;\n\tmap<int, T>revmp;\n\n\tCompress(vector<T>vs) {\n\t\tsetmp(vs);\n\t}\n\n\tCompress() :mp(), revmp() {\n\n\t}\n\tvoid setmp(vector<T>vs) {\n\t\tsort(vs.begin(), vs.end());\n\t\tvs.erase(unique(vs.begin(), vs.end()), vs.end());\n\t\tfor (int i = 0; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tmp[vs[i]] = i;\n\t\t\trevmp[i] = vs[i];\n\t\t}\n\t}\n\n};\n\nint main() {\n\tint Q;\n\twhile (cin>>Q,Q) {\n\n\n\t\tlong long int L; cin >> L;\n\t\tCompress<long long int> cp;\n\t\tvector<query>qs;\n\t\t{\n\n\t\t\tvector<long long int>xs;\n\t\t\txs.push_back(0);\n\t\t\tlong long int nowdis = 0;\n\t\t\tfor (int i = 0; i < Q; ++i) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a == 0) {\n\t\t\t\t\txs.push_back(nowdis + L);\n\t\t\t\t\tqs.push_back(query{ a,-1,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 1) {\n\t\t\t\t\tint d; cin >> d;\n\t\t\t\t\tnowdis += d;\n\t\t\t\t\txs.push_back(nowdis);\n\t\t\t\t\tqs.push_back(query{ a,d,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 2) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 3) {\n\t\t\t\t\tint x, r; cin >> x >> r;\n\t\t\t\t\txs.push_back(nowdis + x + r);\n\t\t\t\t\txs.push_back(nowdis + x - r);\n\t\t\t\t\tqs.push_back(query{ a,nowdis + x - r,nowdis + x + r });\n\t\t\t\t}\n\t\t\t\telse if (a == 4) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t}\n\t\t\tcp.setmp(xs);\n\t\t\t\n\t\t}\n\t\tlazy_segtree seg(cp.mp.size());\n\t\tlong long int nowx = 0;\n\t\tfor (auto q : qs) {\n\t\t\tswitch (q.type) {\n\t\t\tcase 0:\n\t\t\t\tseg.update(cp.mp[nowx + L] + 1, cp.mp[nowx + L] + 1, make_pair(0, 1));\n\t\t\t\tbreak;\n\t\t\tcase 1: {\n\n\t\t\t\tint damage = seg.get(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1);\n\t\t\t\tseg.update(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1, make_pair(1, 0));\n\t\t\t\tif (damage) {\n\t\t\t\t\tcout << \"damage\" << \" \" << damage << endl;\n\t\t\t\t}\n\t\t\t\tnowx += q.a;\n\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t\tseg.update(cp.mp[nowx] + 2+ amax, cp.mp[nowx] + 2+ amax, make_pair(0, -1));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"miss\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t{\n\n\t\t\t\tint bomb = seg.get(cp.mp[q.a] + 1, cp.mp[q.b] + 1);\n\t\t\t\tcout << \"bomb \" << bomb << endl;\n\t\t\t\tseg.update(cp.mp[q.a] + 1, cp.mp[q.b] + 1, make_pair(1, 0));\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tlong long int dis = cp.revmp[cp.mp[nowx] +1+ amax] - nowx;\n\t\t\t\t\tcout << \"distance \" << dis << endl;\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"distance -1\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tassert(false);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t\tassert(cp.mp.size() == cp.revmp.size());\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1370, "memory_kb": 31344, "score_of_the_acc": -2, "final_rank": 10 }, { "submission_id": "aoj_1515_2469525", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\ntypedef int Value1;\ntypedef pair<bool,int>Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(make_pair(false, 0));\n\nstruct Node {\n\tValue1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.\n\tValue2 lazy;\t//?????¶???????????????????????????????????????\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\tif (l.first || r.first)return make_pair(1, -1);\n\t\telse {\n\t\t\treturn make_pair(0, l.second + r.second);\n\t\t}\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\t\tif (dat[k].lazy.first)dat[k].sum = 0;\n\t\telse {\n\t\t\tdat[k].sum += dat[k].lazy.second*(b - a + 1);\n\t\t}\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inline??????????????¨??§???????????¨????????????!(??????)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//?¬???????????????¬??????????????£?????????.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\\????????¨????????????????????????????????????????????????????????§if???????????????.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r]?????´??°??????. ?????????1-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r]?????????????????¨?????????????????????. ?????????1-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\t\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn Zero1;\n\t\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\treturn dat[k].sum;\n\t\t}\n\t\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nstruct query {\n\tint type;\n\tlong long int a;\n\tlong long int b;\n};\ntemplate<typename T> struct Compress {\n\tmap<T, int>mp;\n\tmap<int, T>revmp;\n\n\tCompress(vector<T>vs) {\n\t\tsetmp(vs);\n\t}\n\n\tCompress() :mp(), revmp() {\n\n\t}\n\tvoid setmp(vector<T>vs) {\n\t\tsort(vs.begin(), vs.end());\n\t\tvs.erase(unique(vs.begin(), vs.end()), vs.end());\n\t\tfor (int i = 0; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tmp[vs[i]] = i;\n\t\t\trevmp[i] = vs[i];\n\t\t}\n\t}\n\n};\n\nint main() {\n\tint Q;\n\twhile (cin>>Q,Q) {\n\n\n\t\tlong long int L; cin >> L;\n\t\tCompress<long long int> cp;\n\t\tvector<query>qs;\n\t\t{\n\n\t\t\tvector<long long int>xs;\n\t\t\txs.push_back(0);\n\t\t\tlong long int nowdis = 0;\n\t\t\tfor (int i = 0; i < Q; ++i) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a == 0) {\n\t\t\t\t\txs.push_back(nowdis + L);\n\t\t\t\t\tqs.push_back(query{ a,-1,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 1) {\n\t\t\t\t\tint d; cin >> d;\n\t\t\t\t\tnowdis += d;\n\t\t\t\t\txs.push_back(nowdis);\n\t\t\t\t\tqs.push_back(query{ a,d,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 2) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 3) {\n\t\t\t\t\tint x, r; cin >> x >> r;\n\t\t\t\t\txs.push_back(nowdis + x + r);\n\t\t\t\t\txs.push_back(nowdis + x - r);\n\t\t\t\t\tqs.push_back(query{ a,nowdis + x - r,nowdis + x + r });\n\t\t\t\t}\n\t\t\t\telse if (a == 4) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t}\n\t\t\tcp.setmp(xs);\n\t\t\t\n\t\t}\n\t\tlazy_segtree seg(cp.mp.size());\n\t\tlong long int nowx = 0;\n\t\tfor (auto q : qs) {\n\t\t\tswitch (q.type) {\n\t\t\tcase 0:\n\t\t\t\tseg.update(cp.mp[nowx + L] + 1, cp.mp[nowx + L] + 1, make_pair(0, 1));\n\t\t\t\tbreak;\n\t\t\tcase 1: {\n\n\t\t\t\tint damage = seg.get(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1);\n\t\t\t\tseg.update(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1, make_pair(1, 0));\n\t\t\t\tif (damage) {\n\t\t\t\t\tcout << \"damage\" << \" \" << damage << endl;\n\t\t\t\t}\n\t\t\t\tnowx += q.a;\n\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t\tseg.update(cp.mp[nowx] + 1+ amax, cp.mp[nowx] + 1+ amax, make_pair(0, -1));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"miss\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t{\n\n\t\t\t\tint bomb = seg.get(cp.mp[q.a] + 1, cp.mp[q.b] + 1);\n\t\t\t\tcout << \"bomb \" << bomb << endl;\n\t\t\t\tseg.update(cp.mp[q.a] + 1, cp.mp[q.b] + 1, make_pair(1, -1));\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tlong long int dis = cp.revmp[cp.mp[nowx] +1+ amax] - nowx;\n\t\t\t\t\tcout << \"distance \" << dis << endl;\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"distance -1\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tassert(false);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t\tassert(cp.mp.size() == cp.revmp.size());\n\t}\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 400, "memory_kb": 22584, "score_of_the_acc": -0.9466, "final_rank": 17 }, { "submission_id": "aoj_1515_2469521", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\ntypedef int Value1;\ntypedef pair<bool,int>Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(make_pair(false, 0));\n\nstruct Node {\n\tValue1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.\n\tValue2 lazy;\t//?????¶???????????????????????????????????????\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\tif (l.first || r.first)return make_pair(1, -1);\n\t\telse {\n\t\t\treturn make_pair(0, l.second + r.second);\n\t\t}\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\t\tif (dat[k].lazy.first)dat[k].sum = 0;\n\t\telse {\n\t\t\tdat[k].sum += dat[k].lazy.second*(b - a + 1);\n\t\t}\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inline??????????????¨??§???????????¨????????????!(??????)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//?¬???????????????¬??????????????£?????????.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\\????????¨????????????????????????????????????????????????????????§if???????????????.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r]?????´??°??????. ?????????1-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r]?????????????????¨?????????????????????. ?????????1-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\t\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn Zero1;\n\t\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\treturn dat[k].sum;\n\t\t}\n\t\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nstruct query {\n\tint type;\n\tlong long int a;\n\tlong long int b;\n};\ntemplate<typename T> struct Compress {\n\tmap<T, int>mp;\n\tmap<int, T>revmp;\n\n\tCompress(vector<T>vs) {\n\t\tsetmp(vs);\n\t}\n\n\tCompress() :mp(), revmp() {\n\n\t}\n\tvoid setmp(vector<T>vs) {\n\t\tsort(vs.begin(), vs.end());\n\t\tvs.erase(unique(vs.begin(), vs.end()), vs.end());\n\t\tfor (int i = 0; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tmp[vs[i]] = i;\n\t\t\trevmp[i] = vs[i];\n\t\t}\n\t}\n\n};\n\nint main() {\n\tint Q;\n\twhile (cin>>Q,Q) {\n\n\n\t\tlong long int L; cin >> L;\n\t\tCompress<long long int> cp;\n\t\tvector<query>qs;\n\t\t{\n\n\t\t\tvector<long long int>xs;\n\t\t\txs.push_back(0);\n\t\t\tlong long int nowdis = 0;\n\t\t\tfor (int i = 0; i < Q; ++i) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a == 0) {\n\t\t\t\t\txs.push_back(nowdis + L);\n\t\t\t\t\tqs.push_back(query{ a,-1,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 1) {\n\t\t\t\t\tint d; cin >> d;\n\t\t\t\t\tnowdis += d;\n\t\t\t\t\txs.push_back(nowdis);\n\t\t\t\t\tqs.push_back(query{ a,d,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 2) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 3) {\n\t\t\t\t\tint x, r; cin >> x >> r;\n\t\t\t\t\txs.push_back(nowdis + x + r);\n\t\t\t\t\txs.push_back(nowdis + x - r);\n\t\t\t\t\tqs.push_back(query{ a,nowdis + x - r,nowdis + x + r });\n\t\t\t\t}\n\t\t\t\telse if (a == 4) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t}\n\t\t\tcp.setmp(xs);\n\t\t\t\n\t\t}\n\t\tlazy_segtree seg(cp.mp.size());\n\t\tlong long int nowx = 0;\n\t\tfor (auto q : qs) {\n\t\t\tswitch (q.type) {\n\t\t\tcase 0:\n\t\t\t\tseg.update(cp.mp[nowx + L] + 1, cp.mp[nowx + L] + 1, make_pair(0, 1));\n\t\t\t\tbreak;\n\t\t\tcase 1: {\n\n\t\t\t\tint damage = seg.get(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1);\n\t\t\t\tif (damage) {\n\n\t\t\t\t\tcout << \"damage\" << \" \" << damage << endl;\n\t\t\t\t}\n\t\t\t\tnowx += q.a;\n\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t\tseg.update(cp.mp[nowx] + 1+ amax, cp.mp[nowx] + 1+ amax, make_pair(0, -1));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"miss\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t{\n\n\t\t\t\tint bomb = seg.get(max(cp.mp[nowx]+2,cp.mp[q.a] + 1), cp.mp[q.b] + 1);\n\t\t\t\tcout << \"bomb \" << bomb << endl;\n\t\t\t\tseg.update(cp.mp[q.a] + 1, cp.mp[q.b] + 1, make_pair(1, -1));\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tlong long int dis = cp.revmp[cp.mp[nowx] +1+ amax] - nowx;\n\t\t\t\t\tcout << \"distance \" << dis << endl;\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"distance -1\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tassert(false);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t\tassert(cp.mp.size() == cp.revmp.size());\n\t}\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 330, "memory_kb": 22608, "score_of_the_acc": -0.894, "final_rank": 16 }, { "submission_id": "aoj_1515_2469520", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\ntypedef int Value1;\ntypedef pair<bool,int>Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(make_pair(false, 0));\n\nstruct Node {\n\tValue1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.\n\tValue2 lazy;\t//?????¶???????????????????????????????????????\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\tif (l.first || r.first)return make_pair(1, -1);\n\t\telse {\n\t\t\treturn make_pair(0, l.second + r.second);\n\t\t}\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\t\tif (dat[k].lazy.first)dat[k].sum = 0;\n\t\telse {\n\t\t\tdat[k].sum += dat[k].lazy.second*(b - a + 1);\n\t\t}\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inline??????????????¨??§???????????¨????????????!(??????)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//?¬???????????????¬??????????????£?????????.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\\????????¨????????????????????????????????????????????????????????§if???????????????.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r]?????´??°??????. ?????????1-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r]?????????????????¨?????????????????????. ?????????1-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\t\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn Zero1;\n\t\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\treturn dat[k].sum;\n\t\t}\n\t\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nstruct query {\n\tint type;\n\tlong long int a;\n\tlong long int b;\n};\ntemplate<typename T> struct Compress {\n\tmap<T, int>mp;\n\tmap<int, T>revmp;\n\n\tCompress(vector<T>vs) {\n\t\tsetmp(vs);\n\t}\n\n\tCompress() :mp(), revmp() {\n\n\t}\n\tvoid setmp(vector<T>vs) {\n\t\tsort(vs.begin(), vs.end());\n\t\tvs.erase(unique(vs.begin(), vs.end()), vs.end());\n\t\tfor (int i = 0; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tmp[vs[i]] = i;\n\t\t\trevmp[i] = vs[i];\n\t\t}\n\t}\n\n};\n\nint main() {\n\tint Q;\n\twhile (cin>>Q,Q) {\n\n\n\t\tlong long int L; cin >> L;\n\t\tCompress<long long int> cp;\n\t\tvector<query>qs;\n\t\t{\n\n\t\t\tvector<long long int>xs;\n\t\t\txs.push_back(0);\n\t\t\tlong long int nowdis = 0;\n\t\t\tfor (int i = 0; i < Q; ++i) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a == 0) {\n\t\t\t\t\txs.push_back(nowdis + L);\n\t\t\t\t\tqs.push_back(query{ a,-1,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 1) {\n\t\t\t\t\tint d; cin >> d;\n\t\t\t\t\tnowdis += d;\n\t\t\t\t\txs.push_back(nowdis);\n\t\t\t\t\tqs.push_back(query{ a,d,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 2) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 3) {\n\t\t\t\t\tint x, r; cin >> x >> r;\n\t\t\t\t\txs.push_back(nowdis + x + r);\n\t\t\t\t\txs.push_back(nowdis + x - r);\n\t\t\t\t\tqs.push_back(query{ a,nowdis + x - r,nowdis + x + r });\n\t\t\t\t}\n\t\t\t\telse if (a == 4) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t}\n\t\t\tcp.setmp(xs);\n\t\t\t\n\t\t}\n\t\tlazy_segtree seg(cp.mp.size());\n\t\tlong long int nowx = 0;\n\t\tfor (auto q : qs) {\n\t\t\tswitch (q.type) {\n\t\t\tcase 0:\n\t\t\t\tseg.update(cp.mp[nowx + L] + 1, cp.mp[nowx + L] + 1, make_pair(0, 1));\n\t\t\t\tbreak;\n\t\t\tcase 1: {\n\n\t\t\t\tint damage = seg.get(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1);\n\t\t\t\tif (damage) {\n\n\t\t\t\t\tcout << \"damage\" << \" \" << damage << endl;\n\t\t\t\t}\n\t\t\t\tnowx += q.a;\n\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t\tseg.update(cp.mp[nowx] + 1+ amax, cp.mp[nowx] + 1+ amax, make_pair(0, -1));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"miss\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t{\n\n\t\t\t\tint bomb = seg.get(cp.mp[q.a] + 1, cp.mp[q.b] + 1);\n\t\t\t\tcout << \"bomb \" << bomb << endl;\n\t\t\t\tseg.update(cp.mp[q.a] + 1, cp.mp[q.b] + 1, make_pair(1, -1));\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 2, cp.mp[nowx] + 2 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tlong long int dis = cp.revmp[cp.mp[nowx] +1+ amax] - nowx;\n\t\t\t\t\tcout << \"distance \" << dis << endl;\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"distance -1\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tassert(false);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.07692307692307693, "time_ms": 330, "memory_kb": 22552, "score_of_the_acc": -0.892, "final_rank": 18 }, { "submission_id": "aoj_1515_2469519", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\ntypedef int Value1;\ntypedef pair<bool,int>Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(make_pair(false, 0));\n\nstruct Node {\n\tValue1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.\n\tValue2 lazy;\t//?????¶???????????????????????????????????????\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\tif (l.first || r.first)return make_pair(1, -1);\n\t\telse {\n\t\t\treturn make_pair(0, l.second + r.second);\n\t\t}\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\t\tif (dat[k].lazy.first)dat[k].sum = 0;\n\t\telse {\n\t\t\tdat[k].sum += dat[k].lazy.second*(b - a + 1);\n\t\t}\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inline??????????????¨??§???????????¨????????????!(??????)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//?¬???????????????¬??????????????£?????????.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\\????????¨????????????????????????????????????????????????????????§if???????????????.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r]?????´??°??????. ?????????1-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r]?????????????????¨?????????????????????. ?????????1-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\t\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn Zero1;\n\t\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\treturn dat[k].sum;\n\t\t}\n\t\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nstruct query {\n\tint type;\n\tlong long int a;\n\tlong long int b;\n};\ntemplate<typename T> struct Compress {\n\tmap<T, int>mp;\n\tmap<int, T>revmp;\n\n\tCompress(vector<T>vs) {\n\t\tsetmp(vs);\n\t}\n\n\tCompress() :mp(), revmp() {\n\n\t}\n\tvoid setmp(vector<T>vs) {\n\t\tsort(vs.begin(), vs.end());\n\t\tvs.erase(unique(vs.begin(), vs.end()), vs.end());\n\t\tfor (int i = 0; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tmp[vs[i]] = i;\n\t\t\trevmp[i] = vs[i];\n\t\t}\n\t}\n\n};\n\nint main() {\n\tint Q;\n\twhile (cin>>Q,Q) {\n\n\n\t\tlong long int L; cin >> L;\n\t\tCompress<long long int> cp;\n\t\tvector<query>qs;\n\t\t{\n\n\t\t\tvector<long long int>xs;\n\t\t\txs.push_back(0);\n\t\t\tlong long int nowdis = 0;\n\t\t\tfor (int i = 0; i < Q; ++i) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a == 0) {\n\t\t\t\t\txs.push_back(nowdis + L);\n\t\t\t\t\tqs.push_back(query{ a,-1,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 1) {\n\t\t\t\t\tint d; cin >> d;\n\t\t\t\t\tnowdis += d;\n\t\t\t\t\txs.push_back(nowdis);\n\t\t\t\t\tqs.push_back(query{ a,d,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 2) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 3) {\n\t\t\t\t\tint x, r; cin >> x >> r;\n\t\t\t\t\txs.push_back(nowdis + x + r);\n\t\t\t\t\txs.push_back(nowdis + x - r);\n\t\t\t\t\tqs.push_back(query{ a,nowdis + x - r,nowdis + x + r });\n\t\t\t\t}\n\t\t\t\telse if (a == 4) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t}\n\t\t\tcp.setmp(xs);\n\t\t\tfor (auto&q : qs) {\n\t\t\t\tif (q.type == 1) {\n\t\t\t\t}\n\t\t\t\telse if (q.type == 3) {\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tlazy_segtree seg(cp.mp.size());\n\t\tlong long int nowx = 0;\n\t\tfor (auto q : qs) {\n\t\t\tswitch (q.type) {\n\t\t\tcase 0:\n\t\t\t\tseg.update(cp.mp[nowx + L] + 1, cp.mp[nowx + L] + 1, make_pair(0, 1));\n\t\t\t\tbreak;\n\t\t\tcase 1: {\n\n\t\t\t\tint damage = seg.get(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1);\n\t\t\t\tif (damage) {\n\n\t\t\t\t\tcout << \"damage\" << \" \" << damage << endl;\n\t\t\t\t}\n\t\t\t\tnowx += q.a;\n\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 1, cp.mp[nowx] + 1 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t\tseg.update(cp.mp[nowx] + amax, cp.mp[nowx] + amax, make_pair(0, -1));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"miss\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t{\n\n\t\t\t\tint bomb = seg.get(cp.mp[q.a] + 1, cp.mp[q.b] + 1);\n\t\t\t\tcout << \"bomb \" << bomb << endl;\n\t\t\t\tseg.update(cp.mp[q.a] + 1, cp.mp[q.b] + 1, make_pair(1, -1));\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 1, cp.mp[nowx] + 1 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tlong long int dis = cp.revmp[cp.mp[nowx] + amax] - nowx;\n\t\t\t\t\tcout << \"distance \" << dis << endl;\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"distance -1\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tassert(false);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.07692307692307693, "time_ms": 340, "memory_kb": 22564, "score_of_the_acc": -0.9, "final_rank": 20 }, { "submission_id": "aoj_1515_2469515", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\n\ntypedef int Value1;\ntypedef pair<bool,int>Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(make_pair(false, 0));\n\nstruct Node {\n\tValue1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.\n\tValue2 lazy;\t//?????¶???????????????????????????????????????\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\tif (l.first || r.first)return make_pair(1, -1);\n\t\telse {\n\t\t\treturn make_pair(0, l.second + r.second);\n\t\t}\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\t\tif (dat[k].lazy.first)dat[k].sum = 0;\n\t\telse {\n\t\t\tdat[k].sum += dat[k].lazy.second*(b - a + 1);\n\t\t}\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inline??????????????¨??§???????????¨????????????!(??????)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//?¬???????????????¬??????????????£?????????.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\\????????¨????????????????????????????????????????????????????????§if???????????????.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r]?????´??°??????. ?????????1-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r]?????????????????¨?????????????????????. ?????????1-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 1, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\\?.\n\t\n\t\tif (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???\n\t\t\treturn Zero1;\n\t\n\t\tif (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???\n\t\t\treturn dat[k].sum;\n\t\t}\n\t\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m + 1, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nstruct query {\n\tint type;\n\tlong long int a;\n\tlong long int b;\n};\ntemplate<typename T> struct Compress {\n\tmap<T, int>mp;\n\tmap<int, T>revmp;\n\n\tCompress(vector<T>vs) {\n\t\tsetmp(vs);\n\t}\n\n\tCompress() :mp(), revmp() {\n\n\t}\n\tvoid setmp(vector<T>vs) {\n\t\tsort(vs.begin(), vs.end());\n\t\tvs.erase(unique(vs.begin(), vs.end()), vs.end());\n\t\tfor (int i = 0; i < static_cast<int>(vs.size()); ++i) {\n\t\t\tmp[vs[i]] = i;\n\t\t\trevmp[i] = vs[i];\n\t\t}\n\t}\n\n};\n\nint main() {\n\tint Q;\n\twhile (cin>>Q,Q) {\n\n\n\t\tlong long int L; cin >> L;\n\t\tCompress<long long int> cp;\n\t\tvector<query>qs;\n\t\t{\n\n\t\t\tvector<long long int>xs;\n\t\t\txs.push_back(0);\n\t\t\tlong long int nowdis = 0;\n\t\t\tfor (int i = 0; i < Q; ++i) {\n\t\t\t\tint a; cin >> a;\n\t\t\t\tif (a == 0) {\n\t\t\t\t\txs.push_back(nowdis + L);\n\t\t\t\t\tqs.push_back(query{ a,-1,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 1) {\n\t\t\t\t\tint d; cin >> d;\n\t\t\t\t\tnowdis += d;\n\t\t\t\t\txs.push_back(nowdis);\n\t\t\t\t\tqs.push_back(query{ a,d,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 2) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t\telse if (a == 3) {\n\t\t\t\t\tint x, r; cin >> x >> r;\n\t\t\t\t\txs.push_back(nowdis + x + r);\n\t\t\t\t\txs.push_back(nowdis + x - r);\n\t\t\t\t\tqs.push_back(query{ a,nowdis + x - r,nowdis + x + r });\n\t\t\t\t}\n\t\t\t\telse if (a == 4) {\n\t\t\t\t\tint k; cin >> k;\n\t\t\t\t\tqs.push_back(query{ a,k,-1 });\n\t\t\t\t}\n\t\t\t}\n\t\t\tcp.setmp(xs);\n\t\t\tfor (auto&q : qs) {\n\t\t\t\tif (q.type == 1) {\n\t\t\t\t}\n\t\t\t\telse if (q.type == 3) {\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tlazy_segtree seg(cp.mp.size());\n\t\tlong long int nowx = 0;\n\t\tfor (auto q : qs) {\n\t\t\tswitch (q.type) {\n\t\t\tcase 0:\n\t\t\t\tseg.update(cp.mp[nowx + L] + 1, cp.mp[nowx + L] + 1, make_pair(0, 1));\n\t\t\t\tbreak;\n\t\t\tcase 1: {\n\n\t\t\t\tint damage = seg.get(cp.mp[nowx] + 2, cp.mp[nowx + q.a] + 1);\n\t\t\t\tif (damage) {\n\n\t\t\t\t\tcout << \"damage\" << \" \" << damage << endl;\n\t\t\t\t}\n\t\t\t\tnowx += q.a;\n\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 1, cp.mp[nowx] + 1 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tcout << \"hit\" << endl;\n\t\t\t\t\tseg.update(cp.mp[nowx] + amax, cp.mp[nowx] + amax, make_pair(0, -1));\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"miss\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t{\n\n\t\t\t\tint bomb = seg.get(cp.mp[q.a] + 1, cp.mp[q.b] + 1);\n\t\t\t\tcout << \"bomb \" << bomb << endl;\n\t\t\t\tseg.update(cp.mp[q.a] + 1, cp.mp[q.b] + 1, make_pair(1, -1));\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t{\n\t\t\t\tint amin = -1;\n\t\t\t\tint amax = 3e5;\n\t\t\t\twhile (amin + 1 != amax) {\n\t\t\t\t\tint amid((amin + amax) / 2);\n\t\t\t\t\tif (seg.get(cp.mp[nowx] + 1, cp.mp[nowx] + 1 + amid) >= q.a) {\n\t\t\t\t\t\tamax = amid;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tamin = amid;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (amax != 3e5) {\n\t\t\t\t\tlong long int dis = cp.revmp[nowx + amax] - cp.revmp[nowx];\n\t\t\t\t\tcout << \"distance \" << dis << endl;\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcout << \"distance -1\" << endl;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tassert(false);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.07692307692307693, "time_ms": 330, "memory_kb": 22664, "score_of_the_acc": -0.896, "final_rank": 19 }, { "submission_id": "aoj_1515_2170697", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nvector<long long>H; long long q[110000], a[110000], b[110000], Q, L, S, N, bit[110000];\n\nvoid add(long long x, long long r) { while (x <= N) { bit[x] += r; x += x&-x; } }\nlong long sum(long long r) { long long ret = 0; while (r >= 1) { ret += bit[r]; r -= r&-r; }return ret; }\nint main() {\n\twhile (true) {\n\t\tcin >> Q >> L; H.clear(); if (Q == 0 && L == 0)break; S = 0;\n\t\tfor (int i = 0; i < 110000; i++)bit[i] = 0;\n\t\tfor (int i = 0; i < Q; i++) {\n\t\t\tcin >> q[i];\n\t\t\tif (q[i] == 0) { H.push_back(S + L); }\n\t\t\tif (q[i] == 1) { cin >> a[i]; S += a[i]; }\n\t\t\tif (q[i] == 2) { cin >> a[i]; }\n\t\t\tif (q[i] == 3) { cin >> a[i] >> b[i]; }\n\t\t\tif (q[i] == 4) { cin >> a[i]; }\n\t\t}\n\t\tH.push_back(0); sort(H.begin(), H.end()); S = 0; N = 109500;\n\t\tfor (int i = 0; i < Q; i++) {\n\t\t\tif (q[i] == 0) {\n\t\t\t\tint pos1 = lower_bound(H.begin(), H.end(), S + L) - H.begin();\n\t\t\t\tadd(pos1, 1);\n\t\t\t}\n\t\t\tif (q[i] == 1) {\n\t\t\t\tint pos1 = lower_bound(H.begin(), H.end(), S + 1) - H.begin();\n\t\t\t\tS += a[i];\n\t\t\t\tint pos2 = lower_bound(H.begin(), H.end(), S + 1) - H.begin();\n\t\t\t\tint cnt1 = 0;\n\t\t\t\tfor (int i = pos1; i < pos2; i++) {\n\t\t\t\t\tif (sum(i) - sum(i - 1) >= 1) {\n\t\t\t\t\t\tadd(i, -1); cnt1++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (cnt1 >= 1)cout << \"damage \" << cnt1 << endl;\n\t\t\t}\n\t\t\tif (q[i] == 2) {\n\t\t\t\tint L = 0, R = 109000, M;\n\t\t\t\twhile (true) {\n\t\t\t\t\tM = (L + R) / 2;\n\t\t\t\t\tint pos1 = sum(M), pos2 = sum(M - 1);\n\t\t\t\t\tif (pos1 >= a[i] && pos2 < a[i]) { cout << \"hit\" << endl; add(M, -1); break; }\n\t\t\t\t\tif (pos1 < a[i]) {\n\t\t\t\t\t\tif (M == 108999) { cout << \"miss\" << endl; break; }\n\t\t\t\t\t\telse { L = M; }\n\t\t\t\t\t}\n\t\t\t\t\tif (pos2 >= a[i])R = M;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (q[i] == 3) {\n\t\t\t\tint pos1 = lower_bound(H.begin(), H.end(), S + a[i] - b[i]) - H.begin();\n\t\t\t\tint pos2 = lower_bound(H.begin(), H.end(), S + a[i] + b[i] + 1) - H.begin();\n\t\t\t\tint J = pos1 - 1; bool OK = true;\n\t\t\t\tint cnt = 0;\n\t\t\t\twhile (J < pos2) {\n\t\t\t\t\tint L = J, R = 109000, M;\n\t\t\t\t\twhile (true) {\n\t\t\t\t\t\tM = (L + R) / 2;\n\t\t\t\t\t\tint pos3 = sum(M) - sum(J), pos4 = sum(M - 1) - sum(J);\n\t\t\t\t\t\tif (pos3 >= 1 && pos4 < 1) {\n\t\t\t\t\t\t\tJ = M; if (J < pos2) { cnt++; add(J, -1); } break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (pos3 < 1) {\n\t\t\t\t\t\t\tif (M == 108999) { OK = false; break; }\n\t\t\t\t\t\t\telse { L = M; }\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (pos4 >= 1)R = M;\n\t\t\t\t\t}\n\t\t\t\t\tif (OK == false)break;\n\t\t\t\t}\n\t\t\t\tcout << \"bomb \" << cnt << endl;\n\t\t\t}\n\t\t\tif (q[i] == 4) {\n\t\t\t\tint L = 0, R = 109000, M;\n\t\t\t\twhile (true) {\n\t\t\t\t\tM = (L + R) / 2;\n\t\t\t\t\tint pos1 = sum(M), pos2 = sum(M - 1);\n\t\t\t\t\tif (pos1 >= a[i] && pos2 < a[i]) { cout << \"distance \" << H[M] - S << endl; break; }\n\t\t\t\t\tif (pos1 < a[i]) {\n\t\t\t\t\t\tif (M == 108999) { cout << \"distance -1\" << endl; break; }\n\t\t\t\t\t\telse { L = M; }\n\t\t\t\t\t}\n\t\t\t\t\tif (pos2 >= a[i])R = M;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << \"end\" << endl;\n\t\t//-------------end of the simulate------------\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 6496, "score_of_the_acc": -0.3183, "final_rank": 5 }, { "submission_id": "aoj_1515_1622892", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ninline int xor128(void)\n{ \n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123; \n int t;\n \n t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n}\n#define int long long\nstruct Node\n{\n int Value;\n int SubTreeSize;\n Node *Lch, *Rch;\n Node(int V):Value(V), SubTreeSize(1) {\n Lch = (Node *)NULL;\n Rch = (Node *)NULL;\n };\n};\ninline int Count(Node *t)\n{\n if(t == (Node *)NULL) return(0);\n else return(t -> SubTreeSize);\n}\n\ninline Node *Update(Node *t)\n{\n if(t == (Node *)NULL) return((Node *)NULL);\n t -> SubTreeSize = Count(t -> Lch) + Count(t -> Rch) + 1;\n return(t);\n}\n \ninline Node *MakeRoot(int value)\n{\n return(new Node(value));\n}\n \nNode *Merge(Node *l, Node *r)\n{\n if(l == (Node *)NULL) return(r);\n if(r == (Node *)NULL) return(l);\n int Left = l -> SubTreeSize;\n int Right = r -> SubTreeSize;\n if(xor128() % (Left + Right) < Left) {\n l -> Rch = Merge(l -> Rch, r);\n return(Update(l));\n } else {\n r -> Lch = Merge(l, r -> Lch);\n return(Update(r));\n }\n}\n \npair< Node*, Node* > Split(Node *t, int k) // [0, k), [k, n)\n{\n if(t == (Node *)NULL) return(make_pair((Node *)NULL, (Node *)NULL));\n if(k <= Count(t -> Lch)) {\n pair< Node *, Node *> s = Split(t -> Lch, k);\n t -> Lch = s.second;\n return(make_pair(s.first, Update(t)));\n } else {\n pair< Node *, Node *> s = Split(t -> Rch, k - Count(t -> Lch) - 1);\n t -> Rch = s.first;\n return(make_pair(Update(t), s.second));\n }\n}\n \nNode *Insert(Node *root, int pos, int value)\n{\n Node *p = MakeRoot(value);\n pair< Node *, Node *> s = Split(root, pos);\n return(Merge(Merge(s.first, p), s.second));\n}\n \nNode *Erase(Node *root, int pos)\n{\n pair< Node *, Node *> s = Split(root, pos);\n pair< Node *, Node *> t = Split(s.second, 1);\n delete t.first;\n return(Merge(s.first, t.second));\n}\nvoid EraseALL(Node *root)\n{\n if(root == (Node *)NULL) return;\n if(root -> Lch != (Node *)NULL) EraseALL(root -> Lch);\n if(root -> Rch != (Node *)NULL) EraseALL(root -> Rch);\n delete root;\n}\n\nint Lower_Bound(Node *root, int Value)\n{\n if(root == (Node *)NULL) return(0);\n if(Value <= root -> Value) return(Lower_Bound(root -> Lch, Value));\n return(Lower_Bound(root -> Rch, Value) + Count(root -> Lch) + 1);\n}\n\nvoid Dump(Node *root)\n{\n Update(root);\n if(root == (Node *)NULL) return;\n cout << \"(\";\n Dump(root -> Lch);\n cout << \"\" << root -> Value << \"\";\n Dump(root -> Rch);\n cout << \")\";\n} \n\n\nsigned main()\n{\n int Q, L;\n Node *root = (Node *)NULL;\n\n while(true) {\n scanf(\"%lld %lld\", &Q, &L);\n if(Q == 0 && L == 0) break;\n long long D = 0LL;\n while(Q--) {\n int type;\n scanf(\"%lld\", &type);\n if(type == 0) {\n root = Insert(root, Lower_Bound(root, L + D), L + D);\n } else if(type == 1) {\n int d;\n scanf(\"%lld\", &d);\n D += d;\n if(root != (Node *)NULL) {\n pair< Node*, Node* > S = Split(root, Lower_Bound(root, D + 1));\n if(Count(S.first) > 0) {\n printf(\"damage %lld\\n\", Count(S.first));\n EraseALL(S.first);\n }\n root = S.second;\n }\n } else if(type == 2) {\n int k;\n scanf(\"%lld\", &k);\n if(Count(root) >= k) {\n root = Erase(root, --k);\n puts(\"hit\");\n } else {\n puts(\"miss\");\n }\n } else if(type == 3) {\n int x, r;\n scanf(\"%lld %lld\", &x, &r);\n pair< Node*, Node* > S = Split(root, Lower_Bound(root, D + x - r));\n pair< Node*, Node* > T = Split(S.second, Lower_Bound(S.second, D + x + r + 1));\n printf(\"bomb %lld\\n\", Count(T.first));\n EraseALL(T.first);\n root = Merge(S.first, T.second);\n } else {\n int k;\n scanf(\"%lld\", &k);\n int dist = -1;\n if(Count(root) >= k) {\n pair< Node*, Node* > S = Split(root, --k);\n pair< Node*, Node* > T = Split(S.second, 1);\n dist = T.first -> Value - D;\n S.second = Merge(T.first, T.second);\n root = Merge(S.first, S.second);\n }\n printf(\"distance %lld\\n\", dist);\n }\n }\n EraseALL(root);\n root = (Node *)NULL;\n puts(\"end\");\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3356, "score_of_the_acc": -0.0458, "final_rank": 1 } ]
aoj_1508_cpp
Problem I : RMQ n 個の数字 a 0 , a 1 , ... , a n-1 と q が与えられる。 q 個のクエリーに対して適切な処理を行なって欲しい。 クエリーは以下の3種類の操作が存在する。 値のシフトを行う l と r のペアが与えられる。( l < r ) a l から a r までの値を Circular Shiftさせる。 0 1 2 3 4 5 6 7 8 9 に対してl=2,r=5というクエリーが与えられたとする。 シフトを行った数字列は 0 1 5 2 3 4 6 7 8 9 となる。 最小値を求める l と r のペアが与えられる。( l ≤ r ) a l から a r までの値の中で最小の値を求める。 値の更新 pos と val のペアが与えられる。 a pos の値を val に更新する。 Input 入力は以下のフォーマットで与えられる。 n q a 0 a 1 . . . a n-1 x 1 y 1 z 1 x 2 y 2 z 2 . . . x q y q z q x i = 0 なら、シフトのクエリーを表す。このとき、 l = y i , r = z i とする。 x i = 1 なら、最小値を求めるクエリーを表す。このとき、 l = y i , r = z i とする。 x i = 2 なら、値を更新するクエリーを表す。このとき、 pos = y i , val = z i とする。 入力は以下の制約を満たす 1 ≤ n , q ≤ 200,000 0 ≤ a i ≤ 10,000,000 値を更新するクエリーについて、0 ≤ val ≤ 10,000,000 Output クエリーとして x i = 1 が与えられたときに、答えの値を1行に出力せよ。 Sample Input 1 10 3 0 1 2 3 4 5 6 7 8 9 1 5 7 0 2 5 1 5 7 Sample Output 1 5 4 Sample Input 2 10 10 289383 930886 692777 636915 747793 238335 885386 760492 516649 641421 2 7 368690 1 3 6 0 2 6 1 1 8 0 2 9 2 2 665123 1 5 9 0 2 8 2 7 961393 1 1 2 Sample Output 2 238335 238335 238335 368690
[ { "submission_id": "aoj_1508_11059198", "code_snippet": "// Begin include: \"../../template/template.hpp\"\nusing namespace std;\n\n// intrinstic\n#include <immintrin.h>\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cfenv>\n#include <cfloat>\n#include <chrono>\n#include <cinttypes>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdint>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <deque>\n#include <fstream>\n#include <functional>\n#include <initializer_list>\n#include <iomanip>\n#include <ios>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <streambuf>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <typeinfo>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n// utility\n// Begin include: \"util.hpp\"\nnamespace yamada {\nusing ll = long long;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nusing lld = long double;\n\ntemplate <typename T>\nusing V = vector<T>;\ntemplate <typename T>\nusing VV = vector<vector<T>>;\ntemplate <typename T>\nusing VVV = vector<vector<vector<T>>>;\ntemplate <typename T>\nusing VVVV = vector<vector<vector<vector<T>>>>;\nusing vl = vector<long long>;\nusing vd = V<double>;\nusing vs = V<string>;\nusing vvl = vector<vector<long long>>;\nusing vvvl = vector<vector<vector<long long>>>;\nusing vvvvl = vector<vector<vector<vector<long long>>>>;\ntemplate <typename T>\nusing minpq = priority_queue<T, vector<T>, greater<T>>;\ntemplate <typename T>\nusing maxpq = priority_queue<T, vector<T>, less<T>>;\n\ntemplate <typename T, typename U>\nstruct P : pair<T, U> {\n\ttemplate <typename... Args>\n\tP(Args... args) : pair<T, U>(args...) {}\n\n\tusing pair<T, U>::first;\n\tusing pair<T, U>::second;\n\n\tP &operator+=(const P &r) {\n\t\tfirst += r.first;\n\t\tsecond += r.second;\n\t\treturn *this;\n\t}\n\tP &operator-=(const P &r) {\n\t\tfirst -= r.first;\n\t\tsecond -= r.second;\n\t\treturn *this;\n\t}\n\tP &operator*=(const P &r) {\n\t\tfirst *= r.first;\n\t\tsecond *= r.second;\n\t\treturn *this;\n\t}\n\ttemplate <typename S>\n\tP &operator*=(const S &r) {\n\t\tfirst *= r, second *= r;\n\t\treturn *this;\n\t}\n\tP operator+(const P &r) const { return P(*this) += r; }\n\tP operator-(const P &r) const { return P(*this) -= r; }\n\tP operator*(const P &r) const { return P(*this) *= r; }\n\ttemplate <typename S>\n\tP operator*(const S &r) const {\n\t\treturn P(*this) *= r;\n\t}\n\tP operator-() const { return P{-first, -second}; }\n};\n\nusing pl = P<ll, ll>;\nusing vp = V<pl>;\nusing vvp = VV<pl>;\n\nconstexpr int inf = 1001001001;\nconstexpr long long infLL = 4004004004004004004LL;\n\ntemplate <typename T, typename U>\ninline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U>\ninline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate <typename T>\ninline T Max(const vector<T> &v) { return *max_element(begin(v), end(v)); }\ntemplate <typename T>\ninline T Min(const vector<T> &v) { return *min_element(begin(v), end(v)); }\ntemplate <typename T>\ninline long long Sum(const vector<T> &v) { return accumulate(begin(v), end(v), T(0)); }\n\ntemplate <typename T>\nint lb(const vector<T> &v, const T &a) { return lower_bound(begin(v), end(v), a) - begin(v); }\ntemplate <typename T>\nint ub(const vector<T> &v, const T &a) { return upper_bound(begin(v), end(v), a) - begin(v); }\n\nconstexpr long long TEN(int n) {\n\tlong long ret = 1, x = 10;\n\tfor (; n; x *= x, n >>= 1) ret *= (n & 1 ? x : 1);\n\treturn ret;\n}\n\ntemplate <typename T>\nvector<T> mkrui(const vector<T> &v, bool rev = false) {\n\tvector<T> ret(v.size() + 1);\n\tif (rev) {\n\t\tfor (int i = int(v.size()) - 1; i >= 0; i--) ret[i] = v[i] + ret[i + 1];\n\t} else {\n\t\tfor (int i = 0; i < int(v.size()); i++) ret[i + 1] = ret[i] + v[i];\n\t}\n\treturn ret;\n};\n\ntemplate <typename T>\nvector<T> mkuni(const vector<T> &v) {\n\tvector<T> ret(v);\n\tsort(ret.begin(), ret.end());\n\tret.erase(unique(ret.begin(), ret.end()), ret.end());\n\treturn ret;\n}\n\ntemplate <typename F>\nvector<int> mkord(int N, F f) {\n\tvector<int> ord(N);\n\tiota(begin(ord), end(ord), 0);\n\tsort(begin(ord), end(ord), f);\n\treturn ord;\n}\n\ntemplate <typename T>\nvector<int> mkinv(vector<T> &v) {\n\tint max_val = *max_element(begin(v), end(v));\n\tvector<int> inv(max_val + 1, -1);\n\tfor (int i = 0; i < (int)v.size(); i++) inv[v[i]] = i;\n\treturn inv;\n}\n\nvector<int> mkiota(int n) {\n\tvector<int> ret(n);\n\tiota(begin(ret), end(ret), 0);\n\treturn ret;\n}\n\ntemplate <typename T>\nT mkrev(const T &v) {\n\tT w{v};\n\treverse(begin(w), end(w));\n\treturn w;\n}\n\ntemplate <typename T>\nbool nxp(T &v) { return next_permutation(begin(v), end(v)); }\n\n// 返り値の型は入力の T に依存\n// i 要素目 : [0, a[i])\ntemplate <typename T>\nvector<vector<T>> product(const vector<T> &a) {\n\tvector<vector<T>> ret;\n\tvector<T> v;\n\tauto dfs = [&](auto rc, int i) -> void {\n\t\tif (i == (int)a.size()) {\n\t\t\tret.push_back(v);\n\t\t\treturn;\n\t\t}\n\t\tfor (int j = 0; j < a[i]; j++) v.push_back(j), rc(rc, i + 1), v.pop_back();\n\t};\n\tdfs(dfs, 0);\n\treturn ret;\n}\n\ntemplate <typename T, typename U>\nvector<U> Digit(T a, const U &x, int siz = -1) {\n\tvector<U> ret;\n\twhile (a > 0) {\n\t\tret.emplace_back(a % x);\n\t\ta /= x;\n\t}\n\tif (siz >= 0) while ((int)ret.size() < siz) ret.emplace_back(0);\n\treturn ret;\n}\n\n// F : function(void(T&)), mod を取る操作\n// T : 整数型のときはオーバーフローに注意する\ntemplate <typename T>\nT Power(T a, long long n, const T &I, const function<void(T &)> &f) {\n\tT res = I;\n\tfor (; n; f(a = a * a), n >>= 1) {\n\t\tif (n & 1) f(res = res * a);\n\t}\n\treturn res;\n}\n// T : 整数型のときはオーバーフローに注意する\ntemplate <typename T>\nT Power(T a, long long n, const T &I = T{1}) {\n\treturn Power(a, n, I, function<void(T &)>{[](T &) -> void {}});\n}\n\ntemplate <typename T>\nT Rev(const T &v) {\n\tT res = v;\n\treverse(begin(res), end(res));\n\treturn res;\n}\n\ntemplate <typename T>\nvector<T> Transpose(const vector<T> &v) {\n\tusing U = typename T::value_type;\n\tif(v.empty()) return {};\n\tint H = v.size(), W = v[0].size();\n\tvector res(W, T(H, U{}));\n\tfor (int i = 0; i < H; i++) for (int j = 0; j < W; j++) res[j][i] = v[i][j];\n\treturn res;\n}\n\ntemplate <typename T>\nvector<T> Rotate(const vector<T> &v, int clockwise = true) {\n\tusing U = typename T::value_type;\n\tint H = v.size(), W = v[0].size();\n\tvector res(W, T(H, U{}));\n\tfor (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {\n\t\tif (clockwise) res[W - 1 - j][i] = v[i][j];\n\t\telse res[j][H - 1 - i] = v[i][j];\n\t}\n\treturn res;\n}\n\ntemplate <typename T, typename F>\nT bisect(T ok, T bad, F pred) {\n\tif (ok == bad) return ok;\n\tif (!pred(ok)) return ok; \n\twhile (bad - ok > 1) { T mid = ok + (bad - ok) / 2; (pred(mid) ? ok : bad) = mid; } \n\treturn bad;\n}\n\ntemplate <typename T, typename F>\nT bisect_double(T ok, T bad, F pred, int iter = 100) {\n\tif (ok == bad) return ok;\n\tif (!pred(ok)) return ok; \n\twhile (iter--) { T mid = ok + (bad - ok) / 2; (pred(mid) ? ok : bad) = mid; } \n\treturn bad;\n}\n\ntemplate <typename T>\nbool inLR(T L, T x, T R){ return (L <= x && x < R); }\n\nbool YESNO(bool b) { cout << (b ? \"YES\\n\" : \"NO\\n\"); return b; }\nbool YesNo(bool b) { cout << (b ? \"Yes\\n\" : \"No\\n\"); return b; }\n\ntemplate <typename mint>\nvoid mout(mint a, int M = 100) {\n\tif (a == 0) { cout << 0 << \"\\n\"; return; }\n\tfor (int i = 0; i <= M; i++) for (int j = 1; j <= M; j++) {\n\t\tmint val = (mint)i / j;\n\t\tif (val == a) {\n\t\t\tif (j == 1) cout << i << \"\\n\";\n\t\t\telse cout << i << \"/\" << j << \"\\n\";\n\t\t\treturn;\n\t\t}\n\t\telse if (val == -a) {\n\t\t\tif (j == 1) cout << -i << \"\\n\";\n\t\t\telse cout << -i << \"/\" << j << \"\\n\";\n\t\t\treturn;\n\t\t}\n\t}\n\tcout << \"NF\\n\";\n}\n\ntemplate <typename mint>\nvoid mout(std::vector<mint> A, int M = 100) {\n\tint N = A.size();\n\tfor (int pos = 0; pos < N; pos++) {\n\t\tif (A[pos] == 0) { cout << 0 << (pos == N - 1 ? \"\\n\" : \" \"); continue; }\n\t\tbool found = false;\n\t\tfor (int i = 0; i <= M; i++) {\n\t\t\tfor (int j = 1; j <= M; j++) {\n\t\t\t\tmint val = (mint)i / j;\n\t\t\t\tif (val == A[pos]) {\n\t\t\t\t\tif (j == 1) cout << i << (pos == N - 1 ? \"\\n\" : \" \");\n\t\t\t\t\telse cout << i << \"/\" << j << (pos == N - 1 ? \"\\n\" : \" \");\n\t\t\t\t\tfound = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (val == -A[pos]) {\n\t\t\t\t\tif (j == 1) cout << -i << (pos == N - 1 ? \"\\n\" : \" \");\n\t\t\t\t\telse cout << -i << \"/\" << j << (pos == N - 1 ? \"\\n\" : \" \");\n\t\t\t\t\tfound = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (found) break;\n\t\t}\n\t\tif (!found) cout << \"NF\" << (pos == N - 1 ? \"\\n\" : \" \");\n\t}\n}\n\nbool is_square(uint64_t n) {\n\tif (n < 2) return true;\n\tuint64_t r = static_cast<uint64_t>(sqrtl(static_cast<long double>(n)));\n\tif (r * r == n) return true;\n\t++r;\n\treturn r * r == n;\n}\n\ntemplate <typename T>\nstruct CumulativeSum {\n\tstd::vector<T> S;\n\tCumulativeSum(std::vector<T> &A) {\n\t\tint N = A.size();\n\t\tS.resize(N + 1);\n\t\tfor (int i = 0; i < N; i++) S[i + 1] = S[i] + A[i];\n\t}\n\n\tT query(int l, int r) { return (l <= r ? S[r] - S[l] : (T)0); }\n\tT get_val(int i) { return S[i + 1] - S[i]; }\n};\n\ntemplate <typename T>\nT extgcd(T a, T b, T &x, T &y) {\n\tT d = a;\n\tif(b != 0) {\n\t\td = extgcd(b, a % b, y, x);\n\t\ty -= (a / b) * x;\n\t}\n\telse x = 1, y = 0;\n\treturn d;\n}\n\n// floor(sqrt(x))\nlong long isqrt (long long x) {\n\tlong long y = sqrt(x);\n\twhile (y * y > x) y--;\n\twhile ((y + 1) * (y + 1) <= x) y++;\n\treturn y;\n}\n\ntemplate <typename T>\nT Floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); }\ntemplate <typename T>\nT Ceil(T a, T b) { return a / b + (a % b && (a ^ b) >= 0); }\n\n} // namespace yamada\n\n// End include: \"util.hpp\"\n\n// bit operation\n// Begin include: \"bitop.hpp\"\nnamespace yamada {\n__attribute__((target(\"popcnt\"))) inline int popcnt(const u64 &a) {\n return __builtin_popcountll(a);\n}\ninline int lsb(const u64 &a) { return a ? __builtin_ctzll(a) : 64; }\ninline int ctz(const u64 &a) { return a ? __builtin_ctzll(a) : 64; }\ninline int msb(const u64 &a) { return a ? 63 - __builtin_clzll(a) : -1; }\ntemplate <typename T>\ninline int gbit(const T &a, int i) {\n return (a >> i) & 1;\n}\ntemplate <typename T>\ninline void sbit(T &a, int i, bool b) {\n if (gbit(a, i) != b) a ^= T(1) << i;\n}\nconstexpr long long PW(int n) { return 1LL << n; }\nconstexpr long long MSK(int n) { return (1LL << n) - 1; }\n} // namespace yamada\n// End include: \"bitop.hpp\"\n\n// inout\n// Begin include: \"inout.hpp\"\nnamespace yamada {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n\tos << p.first << \" \" << p.second;\n\treturn os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p) {\n\tis >> p.first >> p.second;\n\treturn is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n\tint s = (int)v.size();\n\tfor (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n\treturn os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n\tfor (auto &x : v) is >> x;\n\treturn is;\n}\n\nistream &operator>>(istream &is, __int128_t &x) {\n\tstring S;\n\tis >> S;\n\tx = 0;\n\tint flag = 0;\n\tfor (auto &c : S) {\n\t\tif (c == '-') {\n\t\t\tflag = true;\n\t\t\tcontinue;\n\t\t}\n\t\tx *= 10;\n\t\tx += c - '0';\n\t}\n\tif (flag) x = -x;\n\treturn is;\n}\n\nistream &operator>>(istream &is, __uint128_t &x) {\n\tstring S;\n\tis >> S;\n\tx = 0;\n\tfor (auto &c : S) {\n\t\tx *= 10;\n\t\tx += c - '0';\n\t}\n\treturn is;\n}\n\nostream &operator<<(ostream &os, __int128_t x) {\n\tif (x == 0) return os << 0;\n\tif (x < 0) os << '-', x = -x;\n\tstring S;\n\twhile (x) S.push_back('0' + x % 10), x /= 10;\n\treverse(begin(S), end(S));\n\treturn os << S;\n}\nostream &operator<<(ostream &os, __uint128_t x) {\n\tif (x == 0) return os << 0;\n\tstring S;\n\twhile (x) S.push_back('0' + x % 10), x /= 10;\n\treverse(begin(S), end(S));\n\treturn os << S;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u) {\n\tcin >> t;\n\tin(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u) {\n\tcout << t;\n\tif (sizeof...(u)) cout << sep;\n\tout(u...);\n}\n\nstruct IoSetupYamada {\n\tIoSetupYamada() {\n\t\tcin.tie(nullptr);\n\t\tios::sync_with_stdio(false);\n\t\tcout << fixed << setprecision(15);\n\t\tcerr << fixed << setprecision(7);\n\t}\n} iosetupyamada;\n\n} // namespace yamada\n// End include: \"inout.hpp\"\n\n// macro\n// Begin include: \"macro.hpp\"\n#define each(x, v) for (auto&& x : v)\n#define each2(x, y, v) for (auto&& [x, y] : v)\n#define each3(x, y, z, v) for (auto&& [x, y, z] : v)\n#define all(v) (v).begin(), (v).end()\n\n#define rep1(a) for (long long _ = 0; _ < (long long)(a); ++_)\n#define rep2(i, a) for (long long i = 0; i < (long long)(a); ++i)\n#define rep3(i, a, b) for (long long i = a; i < (long long)(b); ++i)\n#define rep4(i, a, b, c) for (long long i = a; i < (long long)(b); i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n\n#define rep1r(a) for (long long i = (long long)(a)-1; i >= 0LL; --i)\n#define rep2r(i, a) for (long long i = (long long)(a)-1; i >= 0LL; --i)\n#define rep3r(i, a, b) for (long long i = (long long)(b)-1; i >= (long long)(a); --i)\n#define overload3(a, b, c, d, ...) d\n#define repr(...) overload3(__VA_ARGS__, rep3r, rep2r, rep1r)(__VA_ARGS__)\n\n#define eb emplace_back\n#define mkp make_pair\n#define mkt make_tuple\n#define fi first\n#define se second\n\n#define vv(type, name, h, ...) \\\n\tvector<vector<type> > name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) \\\n\tvector<vector<vector<type>>> name( \\\n\t\t\th, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n\tvector<vector<vector<vector<type>>>> name( \\\n\t\t\ta, vector<vector<vector<type>>>( \\\n\t\t\t\tb, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n\n#define ini(...) \\\n\tint __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define inl(...) \\\n\tlong long __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define ins(...) \\\n\tstring __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define in2(s, t) \\\n\tfor (int i = 0; i < (int)s.size(); i++) { \\\n\t\tin(s[i], t[i]); \\\n\t}\n#define in3(s, t, u) \\\n\tfor (int i = 0; i < (int)s.size(); i++) { \\\n\t\tin(s[i], t[i], u[i]); \\\n\t}\n#define in4(s, t, u, v) \\\n\tfor (int i = 0; i < (int)s.size(); i++) { \\\n\t\tin(s[i], t[i], u[i], v[i]); \\\n\t}\n#define die(...) \\\n\tdo { \\\n\t\tyamada::out(__VA_ARGS__);\\\n\t\treturn; \\\n\t} while (0)\n// End include: \"macro.hpp\"\n\nnamespace yamada {\nvoid solve();\n}\nint main() { yamada::solve(); }\n// End include: \"../../template/template.hpp\"\n// Begin include: \"../../rbst/lazy-reversible-rbst.hpp\"\n\n// Begin include: \"rbst-base.hpp\"\n\n// RBSTそのものではなく、RBSTを操作する関数の集合体\ntemplate <typename Node>\nstruct RBSTBase {\n\tusing Ptr = Node *;\n\ttemplate <typename... Args>\n\tinline Ptr my_new(Args... args) { return new Node(args...); }\n\tinline void my_del(Ptr t) { delete t; }\n\tinline Ptr make_tree() const { return nullptr; }\n\n\t// メモリリーク回避用(遅い)\n\t\n\t/* using Ptr = shared_ptr<Node>; */\n\t/* template <typename... Args> */\n\t/* inline Ptr my_new(Args... args) { */\n\t/* \treturn make_shared<Node>(args...); */\n\t/* } */\n\t/* inline void my_del(Ptr t) {} */\n\t/* Ptr make_tree() {return Ptr();} */\n\n\tint size(Ptr t) const { return count(t); }\n\n\tPtr merge(Ptr l, Ptr r) {\n\t\tif (!l || !r) return l ? l : r;\n\t\tif (int((rng() * (l->cnt + r->cnt)) >> 32) < l->cnt) {\n\t\t\tpush(l);\n\t\t\tl->r = merge(l->r, r);\n\t\t\treturn update(l);\n\t\t}\n\t\telse {\n\t\t\tpush(r);\n\t\t\tr->l = merge(l, r->l);\n\t\t\treturn update(r);\n\t\t}\n\t}\n\n\tpair<Ptr, Ptr> split(Ptr t, int k) {\n\t\tif (!t) return {nullptr, nullptr};\n\t\tpush(t);\n\t\tif (k <= count(t->l)) {\n\t\t\tauto s = split(t->l, k);\n\t\t\tt->l = s.second;\n\t\t\treturn {s.first, update(t)};\n\t\t}\n\t\telse {\n\t\t\tauto s = split(t->r, k - count(t->l) - 1);\n\t\t\tt->r = s.first;\n\t\t\treturn {update(t), s.second};\n\t\t}\n\t}\n\n\tPtr build(int l, int r, const vector<decltype(Node::key)> &v) {\n\t\tif (l + 1 == r) return my_new(v[l]);\n\t\tint m = (l + r) >> 1;\n\t\tPtr pm = my_new(v[m]);\n\t\tif (l < m) pm->l = build(l, m, v);\n\t\tif (m + 1 < r) pm->r = build(m + 1, r, v);\n\t\treturn update(pm);\n\t}\n\n\tPtr build(const vector<decltype(Node::key)> &v) {\n\t\treturn build(0, (int)v.size(), v);\n\t}\n\n\ttemplate <typename... Args>\n\tvoid insert(Ptr &t, int k, const Args &... args) {\n\t\tauto x = split(t, k);\n\t\tt = merge(merge(x.first, my_new(args...)), x.second);\n\t}\n\n\tdecltype(Node::key) erase(Ptr &t, int k) {\n\t\tauto x = split(t, k);\n\t\tauto y = split(x.second, 1);\n\t\tauto ret = y.first->key;\n\t\tmy_del(y.first);\n\t\tt = merge(x.first, y.second);\n\t\treturn ret;\n\t}\n\n\t// 最後に check(s) が成り立つところまでを左としてsplit\n\ttemplate <typename F>\n\tpair<Ptr, Ptr> split_max_right(Ptr& root, const F &check) {\n\t\tif (!root) return {nullptr, nullptr};\n\t\tpush(root);\n\t\tif (check(root)) {\n\t\t\tauto [u0, u1] = split_max_right(root->r, check);\n\t\t\troot->r = u0;\n\t\t\tupdate(root);\n\t\t\treturn {root, u1};\n\t\t}\n\t\telse {\n\t\t\tauto [u0, u1] = split_max_right(root->l, check);\n\t\t\troot->l = u1;\n\t\t\tupdate(root);\n\t\t\treturn {u0, root};\n\t\t}\n\t}\n\n\tvector<Ptr> enumerate(Ptr& root) {\n\t\tif (!root) return vector<Ptr>();\n\t\tvector<Ptr> ans(root->cnt);\n\t\tint ansi = 0;\n\t\tauto dfs = [&](auto&& self, Ptr u)->void{\n\t\t\tif (!u) return;\n\t\t\tupdate(u);\n\t\t\tself(self, u->l);\n\t\t\tans[ansi++] = u;\n\t\t\tself(self, u->r);\n\t\t}; dfs(dfs, root);\n\t\treturn ans;\n\t}\n\n\tprotected:\n\tstatic uint64_t rng() {\n\t\tstatic uint64_t x_ = 88172645463325252ULL;\n\t\treturn x_ ^= x_ << 7, x_ ^= x_ >> 9, x_ & 0xFFFFFFFFull;\n\t}\n\n\tinline int count(const Ptr t) const { return t ? t->cnt : 0; }\n\n\tvirtual void push(Ptr) = 0;\n\n\tvirtual Ptr update(Ptr) = 0;\n};\n\n/**\n * @brief 乱択平衡二分木(基底クラス)\n */\n// End include: \"rbst-base.hpp\"\n\n// 限界高速化がしたいなら遅延伝播機能、sum機能、反転機能あたり削れたら削る\ntemplate <typename T, typename E>\nstruct LazyReversibleRBSTNode {\n\ttypename RBSTBase<LazyReversibleRBSTNode>::Ptr l, r;\n\tT key, sum;\n\tE lazy;\n\tint cnt;\n\tbool rev;\n\n\tLazyReversibleRBSTNode(const T &t = T(), const E &e = E())\n\t\t: l(), r(), key(t), sum(t), lazy(e), cnt(1), rev(false) {}\n};\n\ntemplate <typename T, typename E, T (*f)(T, T), T (*g)(T, E), E (*h)(E, E), T (*ts)(T)>\nstruct LazyReversibleRBST : RBSTBase<LazyReversibleRBSTNode<T, E>> {\n\tusing Node = LazyReversibleRBSTNode<T, E>;\n\tusing base = RBSTBase<LazyReversibleRBSTNode<T, E>>;\n\tusing base::merge;\n\tusing base::split;\n\tusing typename base::Ptr;\n\n\tLazyReversibleRBST() = default;\n\n\tvoid toggle(Ptr t) {\n\t\tif(!t) return;\n\t\tswap(t->l, t->r);\n\t\tt->sum = ts(t->sum);\n\t\tt->rev ^= true;\n\t}\n\n\tT fold(Ptr &t, int a, int b) {\n\t\tauto x = split(t, a);\n\t\tauto y = split(x.second, b - a);\n\t\tauto ret = sum(y.first);\n\t\tt = merge(x.first, merge(y.first, y.second));\n\t\treturn ret;\n\t}\n\t\n\tT fold(Ptr &t) { return t ? t->sum : T(); }\n\n\tvoid reverse(Ptr &t, int a, int b) {\n\t\tauto x = split(t, a);\n\t\tauto y = split(x.second, b - a);\n\t\ttoggle(y.first);\n\t\tt = merge(x.first, merge(y.first, y.second));\n\t}\n\n\tvoid apply(Ptr &t, int a, int b, const E &e) {\n\t\tauto x = split(t, a);\n\t\tauto y = split(x.second, b - a);\n\t\tpropagate(y.first, e);\n\t\tt = merge(x.first, merge(y.first, y.second));\n\t}\n\n\tvoid apply(Ptr &t, const E &e) { if (t) propagate(t, e); }\n\n\tprotected:\n\tinline T sum(const Ptr t) const { return t ? t->sum : T(); }\n\n\tPtr update(Ptr t) override {\n\t\tpush(t);\n\t\tt->cnt = 1;\n\t\tt->sum = t->key;\n\t\tif (t->l) t->cnt += t->l->cnt, t->sum = f(t->l->sum, t->sum);\n\t\tif (t->r) t->cnt += t->r->cnt, t->sum = f(t->sum, t->r->sum);\n\t\treturn t;\n\t}\n\n\tvoid push(Ptr t) override {\n\t\tif (t->rev) {\n\t\t\tif (t->l) toggle(t->l);\n\t\t\tif (t->r) toggle(t->r);\n\t\t\tt->rev = false;\n\t\t}\n\t\tif (t->lazy != E()) {\n\t\t\tif (t->l) propagate(t->l, t->lazy);\n\t\t\tif (t->r) propagate(t->r, t->lazy);\n\t\t\tt->lazy = E();\n\t\t}\n\t}\n\n\tvoid propagate(Ptr t, const E &x) {\n\t\tt->lazy = h(t->lazy, x);\n\t\tt->key = g(t->key, x);\n\t\tt->sum = g(t->sum, x);\n\t}\n};\n// End include: \"../../rbst/lazy-reversible-rbst.hpp\"\nusing namespace yamada;\n\nusing T=ll;\nusing E=bool;\nT F(T a,T b){return min(a,b);}\nT G(T a,E e){return a;}\nE H(E e,E f){return e;}\nT TS(T a){return a;}\n\nvoid yamada::solve()\n{\n\tinl(N,Q);\n\tvl A(N); in(A);\n\tLazyReversibleRBST<T,E,F,G,H,TS> st;\n\tauto root=st.build(A);\n\n\twhile(Q--){\n\t\tinl(op);\n\t\tif(op==0){\n\t\t\tinl(i,j);\n\t\t\tauto val=st.erase(root,j);\n\t\t\tst.insert(root,i,val);\n\t\t}\n\t\telse if(op==1){\n\t\t\tinl(l,r); ++r;\n\t\t\tout(st.fold(root,l,r));\n\t\t}\n\t\telse{\n\t\t\tinl(pos,val);\n\t\t\tst.erase(root,pos);\n\t\t\tst.insert(root,pos,val);\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 17076, "score_of_the_acc": -0.2169, "final_rank": 10 }, { "submission_id": "aoj_1508_10899152", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pll = pair<ll, ll>;\n#define vc vector\nusing vl = vc<ll>;\n\n#define ov(a, b, c, name, ...) name\n#define rep2(i, l, r) for (ll i = (l); i < ll(r); i++)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep(...) ov(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)\n#define fec(...) for (const auto &__VA_ARGS__)\n#define per(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)\n\n#define ALL(x) begin(x), end(x)\n#define SZ(x) (ll) size(x)\n#define LB(v, x) (lower_bound(ALL(v), x) - begin(v))\n#define eb emplace_back\n\nconst ll INF = ll(4e18) + 100;\n\n#ifdef LOCAL\n#define local(...) __VA_ARGS__\n#else\n#define local(...)\n#endif\n\ntemplate <class S, auto op, auto e>\nstruct RBST\n{\n inline int rnd()\n {\n static int x = 740318, y = 79431, z = 470138, w = 391731;\n int t = x ^ (x << 11);\n x = y, y = z, z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node\n {\n Node *l, *r;\n int cnt;\n S x, sum;\n Node() = default;\n Node(S x) : l(0), r(0), cnt(1), x(x), sum(x) {}\n };\n using P = Node *;\n vc<Node> pool;\n int ptr = 0;\n inline P alloc(S v)\n {\n if (SZ(pool) == ptr)\n pool.resize(pool.size() * 2);\n return &(pool[ptr++] = Node(v));\n }\n RBST(int n) : pool(n) {}\n int cnt(P t) { return t ? t->cnt : 0; }\n S sum(P t) { return t ? t->sum : e(); }\n P update(P t)\n {\n t->cnt = cnt(t->l) + cnt(t->r) + 1;\n t->sum = op(op(sum(t->l), t->x), sum(t->r));\n return t;\n }\n P merge(P l, P r)\n {\n if (!l or !r)\n return l ? l : r;\n if (rnd() % (cnt(l) + cnt(r)) < cnt(l))\n {\n l->r = merge(l->r, r);\n return update(l);\n }\n r->l = merge(l, r->l);\n return update(r);\n }\n // t を [0, k) と [k, |t|) に分割\n pair<P, P> split(P t, int k)\n {\n if (!t)\n return {t, t};\n if (k <= cnt(t->l))\n {\n auto [l, r] = split(t->l, k);\n t->l = r;\n return {l, update(t)};\n }\n auto [l, r] = split(t->r, k - cnt(t->l) - 1);\n t->r = l;\n return {update(t), r};\n }\n // t の k 番目に v を挿入\n void insert(P &t, int k, S v)\n {\n auto [l, r] = split(t, k);\n t = merge(merge(l, alloc(v)), r);\n }\n // 削除は split と merge を組み合わせる\n // 区間クエリは split で切り出して sum を取得して merge で戻す\n // 初期化の使用例:\n // RBST<S, op, e>::P root = nullptr;\n // rep(i, A.size()) rbst.insert(root, i, A.at(i));\n};\n\nll op(ll a, ll b) { return min(a, b); }\nll e() { return INF; }\n\nint main()\n{\n#ifndef LOCAL\n cin.tie(0)->sync_with_stdio(0);\n#endif\n\n local(while (true))\n {\n ll N, Q;\n cin >> N >> Q;\n vc<ll> A(N);\n rep(i, N) cin >> A.at(i);\n\n RBST<ll, op, e> rbst(N + Q);\n RBST<ll, op, e>::P root = nullptr;\n rep(i, N) rbst.insert(root, i, A.at(i));\n rep(_, Q)\n {\n ll t;\n cin >> t;\n if (t == 0)\n {\n ll L, R;\n cin >> L >> R;\n auto [xyr, z] = rbst.split(root, R + 1);\n auto [xy, r] = rbst.split(xyr, R);\n auto [x, y] = rbst.split(xy, L);\n root = rbst.merge(rbst.merge(rbst.merge(x, r), y), z);\n }\n else if (t == 1)\n {\n ll L, R;\n cin >> L >> R;\n R++;\n auto [xy, z] = rbst.split(root, R);\n auto [x, y] = rbst.split(xy, L);\n cout << rbst.sum(y) << \"\\n\";\n root = rbst.merge(rbst.merge(x, y), z);\n }\n else if (t == 2)\n {\n ll K, V;\n cin >> K >> V;\n auto [xk, y] = rbst.split(root, K + 1);\n auto [x, k] = rbst.split(xk, K);\n root = rbst.merge(rbst.merge(x, rbst.alloc(V)), y);\n }\n }\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 20300, "score_of_the_acc": -0.3208, "final_rank": 17 }, { "submission_id": "aoj_1508_10899149", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing pll = pair<ll, ll>;\n#define vc vector\nusing vl = vc<ll>;\n\n#define ov(a, b, c, name, ...) name\n#define rep2(i, l, r) for (ll i = (l); i < ll(r); i++)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep(...) ov(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)\n#define fec(...) for (const auto &__VA_ARGS__)\n#define per(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)\n\n#define ALL(x) begin(x), end(x)\n#define SZ(x) (ll) size(x)\n#define LB(v, x) (lower_bound(ALL(v), x) - begin(v))\n#define eb emplace_back\n\nconst ll INF = ll(4e18) + 100;\n\n#ifdef LOCAL\n#define local(...) __VA_ARGS__\n#else\n#define local(...)\n#endif\n\ntemplate <class S, auto op, auto e>\nstruct RBST\n{\n inline int rnd()\n {\n static int x = 740318, y = 79431, z = 470138, w = 391731;\n int t = x ^ (x << 11);\n x = y, y = z, z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct Node\n {\n Node *l, *r;\n int cnt;\n S x, sum;\n Node() = default;\n Node(S x) : l(0), r(0), cnt(1), x(x), sum(x) {}\n };\n using P = Node *;\n vc<Node> pool;\n int ptr = 0;\n inline P alloc(S v)\n {\n if (SZ(pool) == ptr)\n pool.resize(pool.size() * 2);\n return &(pool[ptr++] = Node(v));\n }\n RBST(int n) : pool(n) {}\n int cnt(P t) { return t ? t->cnt : 0; }\n S sum(P t) { return t ? t->sum : e(); }\n P update(P t)\n {\n t->cnt = cnt(t->l) + cnt(t->r) + 1;\n t->sum = op(op(sum(t->l), t->x), sum(t->r));\n return t;\n }\n P merge(P l, P r)\n {\n if (!l or !r)\n return l ? l : r;\n if (rnd() % (cnt(l) + cnt(r)) < cnt(l))\n {\n l->r = merge(l->r, r);\n return update(l);\n }\n r->l = merge(l, r->l);\n return update(r);\n }\n // t を [0, k) と [k, |t|) に分割\n pair<P, P> split(P t, int k)\n {\n if (!t)\n return {t, t};\n if (k <= cnt(t->l))\n {\n auto [l, r] = split(t->l, k);\n t->l = r;\n return {l, update(t)};\n }\n auto [l, r] = split(t->r, k - cnt(t->l) - 1);\n t->r = l;\n return {update(t), r};\n }\n // t の k 番目に v を挿入\n void insert(P &t, int k, S v)\n {\n auto [l, r] = split(t, k);\n t = merge(merge(l, alloc(v)), r);\n }\n // 削除は split と merge を組み合わせる\n // 区間クエリは split で切り出して sum を取得して merge で戻す\n // 初期化の使用例:\n // RBST<S, op, e>::P root = nullptr;\n // rep(i, A.size()) rbst.insert(root, i, A.at(i));\n};\n\nll op(ll a, ll b) { return min(a, b); }\nll e() { return INF; }\n\nint main()\n{\n cin.tie(0)->sync_with_stdio(0);\n\n ll N, Q;\n cin >> N >> Q;\n vc<ll> A(N);\n rep(i, N) cin >> A.at(i);\n\n RBST<ll, op, e> rbst(N + Q);\n RBST<ll, op, e>::P root = nullptr;\n rep(i, N) rbst.insert(root, i, A.at(i));\n\n rep(_, Q)\n {\n ll t;\n cin >> t;\n if (t == 0) {\n ll L, R;\n cin >> L >> R;\n if (L >= R) continue;\n auto [xyr, z] = rbst.split(root, R + 1);\n auto [xy, r_node] = rbst.split(xyr, R);\n auto [x, y] = rbst.split(xy, L);\n root = rbst.merge(rbst.merge(x, r_node), rbst.merge(y, z));\n } else if (t == 1) {\n ll L, R;\n cin >> L >> R;\n auto [xy, z] = rbst.split(root, R + 1);\n auto [x, y] = rbst.split(xy, L);\n cout << rbst.sum(y) << \"\\n\";\n root = rbst.merge(rbst.merge(x, y), z);\n } else if (t == 2) {\n ll K, V;\n cin >> K >> V;\n auto [x, y_and_z] = rbst.split(root, K);\n auto [y, z] = rbst.split(y_and_z, 1);\n root = rbst.merge(rbst.merge(x, rbst.alloc(V)), z);\n }\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 20248, "score_of_the_acc": -0.3189, "final_rank": 16 }, { "submission_id": "aoj_1508_10853397", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)\n#define endl '\\n'\n#define ll long long\n\nenum {\n\tNOTFOUND = 0xFFFFFFFFFFFFFFFFLLU\n};\n\nuint64_t NODE_NO = 0;\n\nclass Node {\npublic:\n\tuint64_t no;\n\tuint64_t num;\n\tuint64_t ones;\n\tNode *left;\n\tNode *right;\n\tint64_t balance;\n\tuint64_t bits;\n\tuint64_t bits_size;\n\n\tbool is_leaf;\n\n\tNode(uint64_t bits, uint64_t bits_size, bool is_leaf) : no(NODE_NO++), num(0), ones(0), bits(bits), bits_size(bits_size), is_leaf(is_leaf), left(nullptr), right(nullptr), balance(0) {}\n\n\tbool is_valid_node() {\n\t\tif (is_leaf) {\n\t\t\tif (num != 0) { return false; }\n\t\t\tif (ones != 0) { return false; }\n\t\t\tif (left != nullptr) { return false; }\n\t\t\tif (right != nullptr) { return false; }\n\t\t}\n\t\telse {\n\t\t\tif (num == 0) { return false; }\n\t\t\tif (left == nullptr) { return false; }\n\t\t\tif (right == nullptr) { return false; }\n\t\t\tif (bits != 0) { return false; }\n\t\t\tif (bits_size != 0) { return false; }\n\t\t\tif (ones > num) {return false; }\n\t\t}\n\t\treturn true;\n\t}\n\n\tstring info() {\n\t\tstring str = \"No:\" + to_string(this->no) + \"\\n\";\n\t\tif (is_leaf) {\n\t\t\tstr += \"size:\" + to_string(this->bits_size) + \"\\n\";\n\t\t\tfor (int i = 0; i < bits_size; ++i) {\n\t\t\t\tstr += to_string((bits >> (uint64_t)i) & (uint64_t)1);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tstr += \"num:\" + to_string(this->num) + \" ones:\" + to_string(this->ones) + \"\\n\";\n\t\t}\n\n\t\treturn str;\n\t}\n};\n\nclass DynamicBitVector {\npublic:\n\tNode *root;\n\tuint64_t size;\n\tuint64_t num_one;\n\tconst uint64_t bits_size_limit = 32;\n\n\tDynamicBitVector(): root(nullptr), size(0), num_one(0) {}\n\n\tDynamicBitVector(vector<uint64_t> &v): root(nullptr), size(0), num_one(0) {\n\t\tif (v.size() == 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tdeque<pair<Node*, uint64_t>> leaves;\n\t\tfor (int i = 0; i < v.size(); i += this->bits_size_limit) {\n\t\t\tuint64_t bits = 0;\n\t\t\tconst uint64_t bits_size = min(this->bits_size_limit, (uint64_t)v.size() - i);\n\t\t\tfor (int j = 0; j < bits_size; ++j) {\n\t\t\t\tassert(v[i + j] == 0 or v[i + j] == 1);\n\t\t\t\tif (v[i + j] == 1) {\n\t\t\t\t\tbits |= (uint64_t)1 << j;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tleaves.emplace_back(make_pair(new Node(bits, bits_size, true), bits_size));\n\t\t}\n\n\n\t\tdeque<tuple<Node*, uint64_t, uint64_t, uint64_t>> nodes;\n\t\twhile (not leaves.empty()) {\n\t\t\tconst auto node = leaves.front().first;\n\t\t\tconst auto bits_size = leaves.front().second;\n\t\t\tleaves.pop_front();\n\t\t\tnodes.emplace_back(make_tuple(node, bits_size, popCount(node->bits), 0));\n\t\t}\n\n\t\twhile (nodes.size() > 1) {\n\n\t\t\tdeque<tuple<Node*, uint64_t, uint64_t, uint64_t>> next_nodes;\n\t\t\twhile (not nodes.empty()) {\n\n\t\t\t\tif (nodes.size() == 1) {\n\t\t\t\t\tnodes.push_front(next_nodes.back());\n\t\t\t\t\tnext_nodes.pop_back();\n\t\t\t\t}\n\n\t\t\t\tNode *left_node;\n\t\t\t\tuint64_t left_num, left_ones, left_height;\n\t\t\t\ttie(left_node, left_num, left_ones, left_height) = nodes.front(); nodes.pop_front();\n\n\t\t\t\tNode *right_node;\n\t\t\t\tuint64_t right_num, right_ones, right_height;\n\t\t\t\ttie(right_node, right_num, right_ones, right_height) = nodes.front(); nodes.pop_front();\n\n\t\t\t\tconst auto internal_node = new Node(0, 0, false);\n\t\t\t\tinternal_node->num = left_num;\n\t\t\t\tinternal_node->ones = left_ones;\n\t\t\t\tinternal_node->left = left_node;\n\t\t\t\tinternal_node->right = right_node;\n\t\t\t\tinternal_node->balance = right_height - left_height;\n\n\t\t\t\tnext_nodes.emplace_back(make_tuple(internal_node, left_num + right_num, left_ones + right_ones, max(left_height, right_height) + 1));\n\t\t\t}\n\n\t\t\tnodes = next_nodes;\n\t\t}\n\n\t\tuint64_t height;\n\t\ttie(this->root, this->size, this->num_one, height) = nodes.front(); nodes.pop_front();\n\t\tassert(this->size == v.size());\n\t}\n\n\n\tuint64_t access(uint64_t pos) {\n\t\tassert(pos < this->size);\n\n\t\treturn access(this->root, pos);\n\t}\n\n\n\tuint64_t rank(uint64_t bit, uint64_t pos) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(pos <= this->size);\n\n\t\tif (bit) {\n\t\t\treturn rank1(this->root, pos, 0);\n\t\t}\n\t\telse {\n\t\t\treturn pos - rank1(this->root, pos, 0);\n\t\t}\n\t}\n\n\n\tuint64_t select(uint64_t bit, uint64_t rank) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(rank > 0);\n\n\t\tif (bit == 0 and rank > this->size - this-> num_one) { return NOTFOUND; }\n\t\tif (bit == 1 and rank > this-> num_one) { return NOTFOUND; }\n\n\t\tif (bit) {\n\t\t\treturn select1(this->root, 0, rank);\n\t\t}\n\t\telse {\n\t\t\treturn select0(this->root, 0, rank);\n\t\t}\n\t}\n\n\n\tvoid insert(uint64_t pos, uint64_t bit) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(pos <= this->size);\n\n\t\tif (root == nullptr) {\n\t\t\troot = new Node(bit, 1, true);\n\t\t} else {\n\t\t\tinsert(this->root, nullptr, bit, pos, this->size);\n\t\t}\n\t\tthis->size++;\n\t\tthis->num_one += (bit == 1);\n\t}\n\n\n\tvoid push_back(uint64_t bit) {\n\t\tassert(bit == 0 or bit == 1);\n\n\t\tthis->insert(this->size, bit);\n\t}\n\n\n\tvoid erase(uint64_t pos) {\n\t\tassert(pos < this->size);\n\n\t\tuint64_t bit = this->remove(this->root, nullptr, pos, this->size, 0, true).first;\n\t\tthis->size--;\n\t\tthis->num_one -= (bit == 1);\n\t}\n\n\n\tvoid update(uint64_t pos, uint64_t bit) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(pos < this->size);\n\n\t\tif (bit == 1) {\n\t\t\tthis->bitset(pos);\n\t\t}\n\t\telse {\n\t\t\tthis->bitclear(pos);\n\t\t}\n\t}\n\n\n\tvoid bitset(uint64_t pos) {\n\t\tassert(pos < this->size);\n\n\t\tbool flip = bitset(this->root, pos);\n\t\tthis->num_one += flip;\n\t}\n\n\n\tvoid bitclear(uint64_t pos) {\n\t\tassert(pos < this->size);\n\n\t\tbool flip = bitclear(this->root, pos);\n\t\tthis->num_one -= flip;\n\t}\n\n\n\tvoid graphviz(const string &file_path) {\n\n\n\t}\n\n\n\n\tbool is_valid_tree(bool verbose) {\n\t\tif (this->root == nullptr) {\n\t\t\tif (this->size == 0 and this->num_one == 0) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tcerr << \"root is nullptr but size is \" << this->size << \" and num_one is \" << this->num_one << endl;\n\t\t\treturn false;\n\t\t}\n\t\tmap<uint64_t, uint64_t> height;\n\t\tget_height(this->root, height);\n\n\t\tqueue<Node*> que;\n\t\tque.emplace(this->root);\n\n\t\twhile (not que.empty()) {\n\t\t\tNode *node = que.front(); que.pop();\n\n\t\t\tif (not node->is_valid_node()) {\n\t\t\t\tif (verbose) {\n\t\t\t\t\tcerr << \"node \" << node->no << \" is invalid node\" << endl;\n\t\t\t\t\tcerr << node->info() << endl;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tif (not node->is_leaf) {\n\t\t\t\tauto left_height = height[node->left->no];\n\t\t\t\tauto right_height = height[node->right->no];\n\n\t\t\t\tif (node->balance != right_height - left_height) {\n\t\t\t\t\tif (verbose) {\n\t\t\t\t\t\tcerr << \"node\" << node->no << \"'s balance is \" << node->balance << \"(left height:\" << left_height << \", right height:\" << right_height << \")\" << endl;\n\t\t\t\t\t}\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\n\t\t\t\tif (node->balance < -1 or 1 < node->balance) {\n\t\t\t\t\tif (verbose) {\n\t\t\t\t\t\tcerr << \"node\" << node->no << \"is not balanced.\" << \"(balance:\" << node->balance << \", left height:\" << left_height << \", right height:\" << right_height << \")\" << endl;\n\t\t\t\t\t}\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\tque.emplace(node->left);\n\t\t\t\tque.emplace(node->right);\n\t\t\t}\n\t\t\telse {\n\n\t\t\t\tif (node->balance != 0) {\n\t\t\t\t\tif (verbose) {\n\t\t\t\t\t\tcerr << \"node \" << node->no << \"'s balance is not 0\" << endl;\n\t\t\t\t\t}\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\nprivate:\n\tuint64_t access(const Node *node, uint64_t pos) {\n\t\tif (node->is_leaf) {\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\t\t\treturn (node->bits >> pos) & (uint64_t)1;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\treturn this->access(node->left, pos);\n\t\t} else {\n\t\t\treturn this->access(node->right, pos - node->num);\n\t\t}\n\t}\n\n\tuint64_t rank1(const Node *node, uint64_t pos, uint64_t ones) {\n\t\tif (node->is_leaf) {\n\t\t\tassert(node->bits_size >= pos);\n\t\t\tconst uint64_t mask = ((uint64_t)1 << pos) - 1;\n\t\t\treturn ones + popCount(node->bits & mask);\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\treturn this->rank1(node->left, pos, ones);\n\t\t} else {\n\t\t\treturn this->rank1(node->right, pos - node->num, ones + node->ones);\n\t\t}\n\t}\n\n\tuint64_t select1(const Node *node, uint64_t pos, uint64_t rank) {\n\t\tif (node->is_leaf) {\n\t\t\treturn pos + this->selectInBlock(node->bits, rank) + 1;\n\t\t}\n\n\t\tif (rank <= node->ones) {\n\t\t\treturn this->select1(node->left, pos, rank);\n\t\t} else {\n\t\t\treturn this->select1(node->right, pos + node->num, rank - node->ones);\n\t\t}\n\t}\n\n\tuint64_t select0(const Node *node, uint64_t pos, uint64_t rank) {\n\t\tif (node->is_leaf) {\n\t\t\treturn pos + this->selectInBlock(~node->bits, rank) + 1;\n\t\t}\n\n\t\tif (rank <= (node->num - node->ones)) {\n\t\t\treturn this->select0(node->left, pos, rank);\n\t\t} else {\n\t\t\treturn this->select0(node->right, pos + node->num, rank - (node->num - node->ones));\n\t\t}\n\t}\n\n\n\tuint64_t selectInBlock(uint64_t bits, uint64_t rank) {\n\t\tconst uint64_t x1 = bits - ((bits & 0xAAAAAAAAAAAAAAAALLU) >> (uint64_t)1);\n\t\tconst uint64_t x2 = (x1 & 0x3333333333333333LLU) + ((x1 >> (uint64_t)2) & 0x3333333333333333LLU);\n\t\tconst uint64_t x3 = (x2 + (x2 >> (uint64_t)4)) & 0x0F0F0F0F0F0F0F0FLLU;\n\n\t\tuint64_t pos = 0;\n\t\tfor (;; pos += 8) {\n\t\t\tconst uint64_t rank_next = (x3 >> pos) & 0xFFLLU;\n\t\t\tif (rank <= rank_next) break;\n\t\t\trank -= rank_next;\n\t\t}\n\n\t\tconst uint64_t v2 = (x2 >> pos) & 0xFLLU;\n\t\tif (rank > v2) {\n\t\t\trank -= v2;\n\t\t\tpos += 4;\n\t\t}\n\n\t\tconst uint64_t v1 = (x1 >> pos) & 0x3LLU;\n\t\tif (rank > v1) {\n\t\t\trank -= v1;\n\t\t\tpos += 2;\n\t\t}\n\n\t\tconst uint64_t v0 = (bits >> pos) & 0x1LLU;\n\t\tif (v0 < rank) {\n\t\t\tpos += 1;\n\t\t}\n\n\t\treturn pos;\n\t}\n\n\n\n\tint64_t insert(Node *node, Node *parent, uint64_t bit, uint64_t pos, uint64_t len) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tif (node->is_leaf) {\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\n\n\t\t\tconst uint64_t up_mask = (((uint64_t)1 << (len - pos)) - 1) << pos;\n\t\t\tconst uint64_t up = (node->bits & up_mask);\n\n\n\t\t\tconst uint64_t down_mask = ((uint64_t)1 << pos) - 1;\n\t\t\tconst uint64_t down = node->bits & down_mask;\n\n\t\t\tnode->bits = (up << (uint64_t)1) | (bit << pos) | down;\n\t\t\tnode->bits_size++;\n\t\t\tassert(node->bits_size == len + 1);\n\n\n\t\t\tif (len + 1 >= 2 * bits_size_limit) {\n\t\t\t\tthis->splitNode(node, len + 1);\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tconst int64_t diff = this->insert(node->left, node, bit, pos, node->num);\n\t\t\tnode->num += 1;\n\t\t\tnode->ones += (bit == 1);\n\t\t\treturn achieveBalance(parent, node, diff, 0);\n\t\t} else {\n\t\t\tconst int64_t diff = this->insert(node->right, node, bit, pos - node->num, len - node->num);\n\t\t\treturn achieveBalance(parent, node, 0, diff);\n\t\t}\n\t}\n\n\n\n\tpair<uint64_t, int64_t> remove(Node *node, Node *parent, uint64_t pos, uint64_t len, uint64_t ones, bool allow_under_flow) {\n\t\tif (node->is_leaf) {\n\n\t\t\tif (node != this->root and len <= bits_size_limit / 2 and not allow_under_flow) {\n\t\t\t\treturn make_pair(NOTFOUND, 0);\n\t\t\t}\n\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\t\t\tassert(pos < len);\n\t\t\tconst uint64_t bit = (node->bits >> pos) & (uint64_t)1;\n\n\n\t\t\tconst uint64_t up_mask = (((uint64_t)1 << (len - pos - 1)) - 1) << (pos + 1);\n\t\t\tconst uint64_t up = (node->bits & up_mask);\n\n\n\t\t\tconst uint64_t down_mask = ((uint64_t)1 << pos) - 1;\n\t\t\tconst uint64_t down = node->bits & down_mask;\n\n\t\t\tnode->bits = (up >> (uint64_t)1) | down;\n\t\t\tnode->bits_size--;\n\t\t\tassert(node->bits_size == len - 1);\n\n\t\t\treturn make_pair(bit, 0);\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tconst auto bit_diff = this->remove(node->left, node, pos, node->num, node->ones, allow_under_flow);\n\t\t\tif (bit_diff.first == NOTFOUND) {\n\t\t\t\treturn bit_diff;\n\t\t\t}\n\n\t\t\tnode->ones -= (bit_diff.first == 1);\n\n\t\t\tif (node->num == bits_size_limit / 2) {\n\t\t\t\tconst auto b_d = remove(node->right, node, 0, len - bits_size_limit / 2, 0, false);\n\n\n\t\t\t\tif (b_d.first == NOTFOUND) {\n\t\t\t\t\tassert(node->left->is_leaf);\n\t\t\t\t\tassert(node->left->bits_size == bits_size_limit / 2 - 1);\n\n\t\t\t\t\tmergeNodes(node->right, 0, len - bits_size_limit / 2, node->left->bits, bits_size_limit / 2 - 1, node->ones, true);\n\t\t\t\t\tthis->replace(parent, node, node->right);\n\t\t\t\t\treturn make_pair(bit_diff.first, -1);\n\t\t\t\t}\n\n\n\t\t\t\tassert(node->left->bits_size == bits_size_limit / 2 - 1);\n\t\t\t\tinsert(node->left, node, b_d.first, bits_size_limit / 2 - 1, bits_size_limit / 2 - 1);\n\t\t\t\tnode->ones += (b_d.first == 1);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnode->num -= 1;\n\t\t\t}\n\n\t\t\tconst int64_t diff = achieveBalance(parent, node, bit_diff.second, 0);\n\t\t\treturn make_pair(bit_diff.first, diff);\n\n\t\t} else {\n\t\t\tconst auto bit_diff = this->remove(node->right, node, pos - node->num, len - node->num, ones - node->ones, allow_under_flow);\n\t\t\tif (bit_diff.first == NOTFOUND) {\n\t\t\t\treturn bit_diff;\n\t\t\t}\n\n\t\t\tones -= (bit_diff.first == 1);\n\n\t\t\tif ((len - node->num) == bits_size_limit / 2) {\n\t\t\t\tconst auto b_d = remove(node->left, node, node->num - 1, node->num, 0, false);\n\n\n\t\t\t\tif (b_d.first == NOTFOUND) {\n\t\t\t\t\tassert(node->right->is_leaf);\n\t\t\t\t\tassert(node->right->bits_size == bits_size_limit / 2 - 1);\n\n\t\t\t\t\tmergeNodes(node->left, node->num, node->num, node->right->bits, bits_size_limit / 2 - 1, ones - node->ones, false);\n\t\t\t\t\tthis->replace(parent, node, node->left);\n\t\t\t\t\treturn make_pair(bit_diff.first, -1);\n\t\t\t\t}\n\n\n\t\t\t\tinsert(node->right, node, b_d.first, 0, bits_size_limit / 2 - 1);\n\t\t\t\tnode->num -= 1;\n\t\t\t\tnode->ones -= (b_d.first == 1);\n\t\t\t}\n\n\t\t\tconst int64_t diff = achieveBalance(parent, node, 0, bit_diff.second);\n\t\t\treturn make_pair(bit_diff.first, diff);\n\t\t}\n\t}\n\n\n\tbool bitset(Node *node, uint64_t pos) {\n\t\tif (node->is_leaf) {\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\t\t\tconst uint64_t bit = (node->bits >> pos) & 1;\n\t\t\tif (bit == 1) {\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tnode->bits |= ((uint64_t)1 << pos);\n\t\t\treturn true;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tbool flip = this->bitset(node->left, pos);\n\t\t\tnode->ones += flip;\n\t\t\treturn flip;\n\t\t} else {\n\t\t\treturn this->bitset(node->right, pos - node->num);\n\t\t}\n\t}\n\n\n\tbool bitclear(Node *node, uint64_t pos) {\n\t\tif (node->is_leaf) {\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\n\t\t\tconst uint64_t bit = (node->bits >> pos) & 1;\n\t\t\tif (bit == 0) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tnode->bits &= ~((uint64_t)1 << pos);\n\t\t\treturn true;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tconst bool flip = this->bitclear(node->left, pos);\n\t\t\tnode->ones -= flip;\n\t\t\treturn flip;\n\t\t} else {\n\t\t\treturn this->bitclear(node->right, pos - node->num);\n\t\t}\n\t}\n\n\n\tvoid splitNode(Node *node, uint64_t len) {\n\t\tassert(node->is_leaf);\n\t\tassert(node->bits_size == len);\n\n\n\t\tconst uint64_t left_size = len / 2;\n\t\tconst uint64_t left_bits = node->bits & (((uint64_t)1 << left_size) - 1);\n\t\tnode->left = new Node(left_bits, left_size, true);\n\n\n\t\tconst uint64_t right_size = len - left_size;\n\t\tconst uint64_t right_bits = node->bits >> left_size;\n\t\tnode->right = new Node(right_bits, right_size, true);\n\n\n\t\tnode->is_leaf = false;\n\t\tnode->num = left_size;\n\t\tnode->ones = this->popCount(left_bits);\n\t\tnode->bits = 0;\n\t\tnode->bits_size = 0;\n\t}\n\n\n\tvoid mergeNodes(Node *node, uint64_t pos, uint64_t len, uint64_t bits, uint64_t s, uint64_t ones, bool left) {\n\t\tif (node->is_leaf) {\n\t\t\tif (left) {\n\t\t\t\tnode->bits = (node->bits << s) | bits;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tassert(len == node->bits_size);\n\t\t\t\tnode->bits = node->bits | (bits << len);\n\t\t\t}\n\t\t\tnode->bits_size += s;\n\t\t\treturn;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tnode->num += s;\n\t\t\tnode->ones += ones;\n\t\t\tmergeNodes(node->left, pos, node->num, bits, s, ones, left);\n\t\t}\n\t\telse {\n\t\t\tmergeNodes(node->right, pos, len - node->num, bits, s, ones, left);\n\t\t}\n\t}\n\n\n\n\tint64_t achieveBalance(Node *parent, Node *node, int64_t leftHeightDiff, int64_t rightHeightDiff) {\n\t\tassert(-1 <= node->balance and node->balance <= 1);\n\t\tassert(-1 <= leftHeightDiff and leftHeightDiff <= 1);\n\t\tassert(-1 <= rightHeightDiff and rightHeightDiff <= 1);\n\n\t\tif (leftHeightDiff == 0 and rightHeightDiff == 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tint64_t heightDiff = 0;\n\n\t\tif ((node->balance <= 0 and leftHeightDiff > 0) or (node->balance >= 0 and rightHeightDiff > 0)) {\n\t\t\t++heightDiff;\n\t\t}\n\n\t\tif ((node->balance < 0 and leftHeightDiff < 0) or (node->balance > 0 and rightHeightDiff < 0)) {\n\t\t\t--heightDiff;\n\t\t}\n\n\t\tnode->balance += -leftHeightDiff + rightHeightDiff;\n\t\tassert(-2 <= node->balance and node->balance <= 2);\n\n\n\t\tif (node->balance == -2) {\n\t\t\tassert(-1 <= node->left->balance and node->left->balance <= 1);\n\t\t\tif (node->left->balance != 0) {\n\t\t\t\theightDiff--;\n\t\t\t}\n\n\t\t\tif (node->left->balance == 1) {\n\t\t\t\treplace(node, node->left, rotateLeft(node->left));\n\t\t\t}\n\t\t\treplace(parent, node, rotateRight(node));\n\t\t}\n\n\t\telse if (node->balance == 2) {\n\t\t\tassert(-1 <= node->right->balance and node->right->balance <= 1);\n\t\t\tif (node->right->balance != 0) {\n\t\t\t\theightDiff--;\n\t\t\t}\n\n\t\t\tif (node->right->balance == -1) {\n\t\t\t\treplace(node, node->right, rotateRight(node->right));\n\t\t\t}\n\t\t\treplace(parent, node, rotateLeft(node));\n\t\t}\n\n\t\treturn heightDiff;\n\t}\n\n\n\tNode *rotateLeft(Node *B) {\n\t\tNode *D = B->right;\n\n\t\tconst int64_t heightC = 0;\n\t\tconst int64_t heightE = heightC + D->balance;\n\t\tconst int64_t heightA = max(heightC, heightE) + 1 - B->balance;\n\n\t\tB->right = D->left;\n\t\tD->left = B;\n\n\t\tB->balance = heightC - heightA;\n\t\tD->num += B->num;\n\t\tD->ones += B->ones;\n\t\tD->balance = heightE - (max(heightA, heightC) + 1);\n\n\t\tassert(-2 <= B->balance and B->balance <= 2);\n\t\tassert(-2 <= D->balance and D->balance <= 2);\n\n\t\treturn D;\n\t}\n\n\n\tNode *rotateRight(Node *D) {\n\t\tNode *B = D->left;\n\n\t\tconst int64_t heightC = 0;\n\t\tconst int64_t heightA = heightC - B->balance;\n\t\tconst int64_t heightE = max(heightA, heightC) + 1 + D->balance;\n\n\t\tD->left = B->right;\n\t\tB->right = D;\n\n\t\tD->num -= B->num;\n\t\tD->ones -= B->ones;\n\t\tD->balance = heightE - heightC;\n\t\tB->balance = max(heightC, heightE) + 1 - heightA;\n\n\n\t\tassert(-2 <= B->balance and B->balance <= 2);\n\t\tassert(-2 <= D->balance and D->balance <= 2);\n\n\t\treturn B;\n\t}\n\n\n\tvoid replace(Node *parent, Node *oldNode, Node *newNode) {\n\t\tif (parent == nullptr) {\n\t\t\tthis->root = newNode;\n\t\t\treturn;\n\t\t}\n\n\t\tif (parent->left == oldNode) {\n\t\t\tparent->left = newNode;\n\t\t}\n\t\telse if (parent->right == oldNode) {\n\t\t\tparent->right = newNode;\n\t\t}\n\t\telse {\n\t\t\tthrow \"old node is not child\";\n\t\t}\n\t}\n\n\tuint64_t popCount(uint64_t x) {\n\t\tx = (x & 0x5555555555555555ULL) + ((x >> (uint64_t)1) & 0x5555555555555555ULL);\n\t\tx = (x & 0x3333333333333333ULL) + ((x >> (uint64_t)2) & 0x3333333333333333ULL);\n\t\tx = (x + (x >> (uint64_t)4)) & 0x0f0f0f0f0f0f0f0fULL;\n\t\tx = x + (x >> (uint64_t)8);\n\t\tx = x + (x >> (uint64_t)16);\n\t\tx = x + (x >> (uint64_t)32);\n\t\treturn x & 0x7FLLU;\n\t}\n\n\n\tuint64_t get_height(Node *node, map<uint64_t, uint64_t> &height) {\n\t\tif (node->is_leaf) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (height.find(node->no) != height.end()) {\n\t\t\treturn height[node->no];\n\t\t}\n\n\t\tauto left_height = get_height(node->left, height);\n\t\tauto right_height = get_height(node->right, height);\n\t\treturn height[node->no] = max(left_height, right_height) + 1;\n\t}\n};\n\n\nclass DynamicWaveletMatrix {\npublic:\n\tvector<DynamicBitVector> bit_arrays;\n\tvector<uint64_t> begin_one;\n\n\tuint64_t size;\n\tuint64_t maximum_element;\n\tuint64_t bit_size;\n\npublic:\n\n\tDynamicWaveletMatrix (uint64_t maximum_element) : size(0), maximum_element(maximum_element + 1) {\n\t\tthis->bit_size = this->get_num_of_bit(maximum_element);\n\t\tif (bit_size == 0) {\n\t\t\tbit_size = 1;\n\t\t}\n\t\tthis->begin_one.resize(bit_size);\n\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tDynamicBitVector sv;\n\t\t\tbit_arrays.push_back(sv);\n\t\t}\n\t}\n\n\tDynamicWaveletMatrix (uint64_t num_of_alphabet, const vector<uint64_t> &array) : size(0), maximum_element(num_of_alphabet + 1) {\n\t\tthis->bit_size = this->get_num_of_bit(num_of_alphabet);\n\t\tif (bit_size == 0) {\n\t\t\tbit_size = 1;\n\t\t}\n\t\tthis->begin_one.resize(bit_size);\n\n\t\tif (array.size() == 0) {\n\t\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\t\tDynamicBitVector sv;\n\t\t\t\tbit_arrays.push_back(sv);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tsize = array.size();\n\n\t\tvector<uint64_t> v(array), b(array.size(), 0);\n\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\n\t\t\tvector<uint64_t> temp;\n\n\t\t\tfor (uint64_t j = 0; j < v.size(); ++j) {\n\t\t\t\tuint64_t c = v.at(j);\n\t\t\t\tuint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\t\tif (bit == 0) {\n\t\t\t\t\ttemp.push_back(c);\n\t\t\t\t\tb[j] = 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis->begin_one.at(i) = temp.size();\n\n\n\t\t\tfor (uint64_t j = 0; j < v.size(); ++j) {\n\t\t\t\tuint64_t c = v.at(j);\n\t\t\t\tuint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\t\tif (bit == 1) {\n\t\t\t\t\ttemp.push_back(c);\n\t\t\t\t\tb[j] = 1;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tDynamicBitVector dbv(b);\n\t\t\tbit_arrays.emplace_back(dbv);\n\t\t\tv = temp;\n\t\t}\n\t}\n\n\n\tuint64_t access(uint64_t pos) {\n\t\tif (pos >= this->size) { return NOTFOUND; }\n\n\t\tuint64_t c = 0;\n\t\tfor (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n\t\t\tuint64_t bit = bit_arrays.at(i).access(pos);\n\t\t\tc = (c <<= 1) | bit;\n\t\t\tpos = bit_arrays.at(i).rank(bit, pos);\n\t\t\tif (bit) {\n\t\t\t\tpos += this->begin_one.at(i);\n\t\t\t}\n\t\t}\n\t\treturn c;\n\t}\n\n\n\tuint64_t rank(uint64_t c, uint64_t pos) {\n\t\tassert(pos <= size);\n\t\tif (c >= maximum_element) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tuint64_t left = 0, right = pos;\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tconst uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\tleft = bit_arrays.at(i).rank(bit, left);\n\t\t\tright = bit_arrays.at(i).rank(bit, right);\n\t\t\tif (bit) {\n\t\t\t\tleft += this->begin_one.at(i);\n\t\t\t\tright += this->begin_one.at(i);\n\t\t\t}\n\t\t}\n\n\t\treturn right - left;\n\t}\n\n\n\tuint64_t select(uint64_t c, uint64_t rank) {\n\t\tassert(rank > 0);\n\t\tif (c >= maximum_element) {\n\t\t\treturn NOTFOUND;\n\t\t}\n\n\t\tuint64_t left = 0;\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tconst uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\tleft = bit_arrays.at(i).rank(bit, left);\n\t\t\tif (bit) {\n\t\t\t\tleft += this->begin_one.at(i);\n\t\t\t}\n\t\t}\n\n\t\tuint64_t index = left + rank;\n\t\tfor (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n\t\t\tuint64_t bit = ((c >> i) & 1);\n\t\t\tif (bit == 1) {\n\t\t\t\tindex -= this->begin_one.at(bit_size - i - 1);\n\t\t\t}\n\t\t\tindex = this->bit_arrays.at(bit_size - i - 1).select(bit, index);\n\t\t}\n\t\treturn index;\n\t}\n\n\n\tvoid insert(uint64_t pos, uint64_t c) {\n\t\tassert(pos <= this->size);\n\n\t\tfor (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n\t\t\tconst uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\tbit_arrays.at(i).insert(pos, bit);\n\t\t\tpos = bit_arrays.at(i).rank(bit, pos);\n\t\t\tif (bit) {\n\t\t\t\tpos += this->begin_one.at(i);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis->begin_one.at(i)++;\n\t\t\t}\n\t\t}\n\n\t\tthis->size++;\n\t}\n\n\n\tvoid push_back(uint64_t c) {\n\t\tthis->insert(this->size, c);\n\t}\n\n\n\tvoid erase(uint64_t pos) {\n\t\tassert(pos < this->size);\n\t\tif (pos >= this->size) {\n\t\t\tthrow \"Segmentation fault\";\n\t\t}\n\n\t\tfor (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n\t\t\tuint64_t bit = bit_arrays.at(i).access(pos);\n\n\t\t\tauto next_pos = bit_arrays.at(i).rank(bit, pos);\n\t\t\tbit_arrays.at(i).erase(pos);\n\n\t\t\tif (bit) {\n\t\t\t\tnext_pos += this->begin_one.at(i);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis->begin_one.at(i)--;\n\t\t\t}\n\t\t\tpos = next_pos;\n\t\t}\n\t\tthis->size--;\n\t}\n\n\n\tvoid update(uint64_t pos, uint64_t c) {\n\t\tassert(pos < this->size);\n\t\tthis->erase(pos);\n\t\tthis->insert(pos, c);\n\t}\n\n\n\tuint64_t maxRange(uint64_t begin_pos, uint64_t end_pos) {\n\t\treturn quantileRange(begin_pos, end_pos, end_pos - begin_pos - 1);\n\t}\n\n\n\tuint64_t minRange(uint64_t begin_pos, uint64_t end_pos) {\n\t\treturn quantileRange(begin_pos, end_pos, 0);\n\t}\n\n\n\n\tuint64_t quantileRange(uint64_t begin_pos, uint64_t end_pos, uint64_t k) {\n\t\tif ((end_pos > size || begin_pos >= end_pos) || (k >= end_pos - begin_pos)) {\n\t\t\treturn NOTFOUND;\n\t\t}\n\n\t\tuint64_t val = 0;\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tuint64_t num_of_zero_begin = bit_arrays.at(i).rank(0, begin_pos);\n\t\t\tuint64_t num_of_zero_end = bit_arrays.at(i).rank(0, end_pos);\n\t\t\tuint64_t num_of_zero = num_of_zero_end - num_of_zero_begin;\n\t\t\tuint64_t bit = (k < num_of_zero) ? 0 : 1;\n\n\t\t\tif (bit) {\n\t\t\t\tk -= num_of_zero;\n\t\t\t\tbegin_pos = this->begin_one.at(i) + begin_pos - num_of_zero_begin;\n\t\t\t\tend_pos = this->begin_one.at(i) + end_pos - num_of_zero_end;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tbegin_pos = num_of_zero_begin;\n\t\t\t\tend_pos = num_of_zero_begin + num_of_zero;\n\t\t\t}\n\n\t\t\tval = ((val << 1) | bit);\n\t\t}\n\n\t\tuint64_t left = 0;\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tconst uint64_t bit = (val >> (bit_size - i - 1)) & 1;\n\t\t\tleft = bit_arrays.at(i).rank(bit, left);\n\t\t\tif (bit) {\n\t\t\t\tleft += this->begin_one.at(i);\n\t\t\t}\n\t\t}\n\n\t\tuint64_t rank = begin_pos + k - left + 1;\n\t\treturn select(val, rank) - 1;\n\t}\n\n\n\tuint64_t rankLessThan(uint64_t c, uint64_t pos) {\n\t\tuint64_t rank_less_than = 0, rank_more_than = 0, rank = 0;\n\t\trankAll(c, 0, pos, rank, rank_less_than, rank_more_than);\n\t\treturn rank_less_than;\n\t}\n\n\n\tuint64_t rankMoreThan(uint64_t c, uint64_t pos) {\n\t\tuint64_t rank_less_than = 0, rank_more_than = 0, rank = 0;\n\t\trankAll(c, 0, pos, rank, rank_less_than, rank_more_than);\n\t\treturn rank_more_than;\n\t}\n\n\n\tvoid rankAll(uint64_t c, uint64_t begin_pos, uint64_t end_pos, uint64_t &rank, uint64_t &rank_less_than, uint64_t &rank_more_than) {\n\t\tif (c >= maximum_element || begin_pos >= size || end_pos > size) {\n\t\t\trank_less_than = NOTFOUND;\n\t\t\trank_more_than = NOTFOUND;\n\t\t\trank = NOTFOUND;\n\t\t\treturn;\n\t\t}\n\t\trank_less_than = 0;\n\t\trank_more_than = 0;\n\t\trank = 0;\n\n\t\tif (begin_pos >= end_pos) {\n\t\t\treturn;\n\t\t}\n\n\t\tvector<uint64_t> more_and_less(2, 0);\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tconst uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\n\t\t\tconst uint64_t range_bits = end_pos - begin_pos;\n\t\t\tconst uint64_t begin_zero = bit_arrays.at(i).rank(0, begin_pos);\n\t\t\tconst uint64_t end_zero = bit_arrays.at(i).rank(0, end_pos);\n\n\t\t\tif (bit) {\n\t\t\t\tbegin_pos += this->begin_one.at(i) - begin_zero;\n\t\t\t\tend_pos += this->begin_one.at(i) - end_zero;\n\t\t\t} else {\n\t\t\t\tbegin_pos = begin_zero;\n\t\t\t\tend_pos = end_zero;\n\t\t\t}\n\t\t\tmore_and_less.at(bit) += range_bits - (end_pos - begin_pos);\n\t\t}\n\n\t\trank_less_than = more_and_less.at(1);\n\t\trank_more_than = more_and_less.at(0);\n\t\trank = end_pos - begin_pos;\n\t}\n\n\n\tvector<pair<uint64_t, uint64_t>> topk(uint64_t s, uint64_t e, uint64_t k) {\n\t\tassert(s < e);\n\t\tvector<pair<uint64_t, uint64_t>> result;\n\n\t\tauto c = [](const tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> &l, const tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> &r) {\n\n\t\t\tif (get<0>(l) != get<0>(r)) {\n\t\t\t\treturn get<0>(l) < get<0>(r);\n\t\t\t}\n\n\t\t\tif (get<3>(l) != get<3>(r)) {\n\t\t\t\treturn get<3>(l) > get<3>(r);\n\t\t\t}\n\n\t\t\tif (get<4>(l) != get<4>(r)) {\n\t\t\t\treturn get<4>(l) > get<4>(r);\n\t\t\t}\n\t\t\treturn true;\n\t\t};\n\n\t\tpriority_queue<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>, vector<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>>, decltype(c)> que(c);\n\t\tque.push(make_tuple(e - s, s, e, 0, 0));\n\n\t\twhile (not que.empty()) {\n\t\t\tauto element = que.top(); que.pop();\n\t\t\tuint64_t width, left, right, depth, value;\n\t\t\ttie(width, left, right, depth, value) = element;\n\n\t\t\tif (depth >= this->bit_size) {\n\t\t\t\tresult.emplace_back(make_pair(value, right - left));\n\t\t\t\tif (result.size() >= k) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\n\t\t\tconst uint64_t left0 = this->bit_arrays.at(depth).rank(0, left);\n\t\t\tconst uint64_t right0 = this->bit_arrays.at(depth).rank(0, right);\n\t\t\tif (left0 < right0) {\n\t\t\t\tque.push(make_tuple(right0 - left0, left0, right0, depth + 1, value));\n\t\t\t}\n\n\n\t\t\tconst uint64_t left1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, left);\n\t\t\tconst uint64_t right1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, right);\n\t\t\tif (left1 < right1) {\n\t\t\t\tque.push(make_tuple(right1 - left1, left1, right1, depth + 1, value | (1 << bit_size - depth - 1)));\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\n\t};\n\n\n\tvector<tuple<uint64_t, uint64_t, uint64_t>> intersect(uint64_t _s1, uint64_t _e1, uint64_t _s2, uint64_t _e2) {\n\t\tassert(_s1 < _e1);\n\t\tassert(_s2 < _e2);\n\n\t\tvector<tuple<uint64_t, uint64_t, uint64_t>> intersection;\n\n\t\tqueue<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>> que;\n\t\tque.push(make_tuple(_s1, _e1, _s2, _e2, 0, 0));\n\t\twhile (not que.empty()) {\n\t\t\tauto e = que.front(); que.pop();\n\t\t\tuint64_t s1, e1, s2, e2, depth, value;\n\t\t\ttie(s1, e1, s2, e2, depth, value) = e;\n\n\t\t\tif (depth >= this->bit_size) {\n\t\t\t\tintersection.emplace_back(make_tuple(value, e1 - s1, e2 - s2));\n\t\t\t\tcontinue;\n\t\t\t}\n\n\n\t\t\tuint64_t s1_0 = this->bit_arrays.at(depth).rank(0, s1);\n\t\t\tuint64_t e1_0 = this->bit_arrays.at(depth).rank(0, e1);\n\t\t\tuint64_t s2_0 = this->bit_arrays.at(depth).rank(0, s2);\n\t\t\tuint64_t e2_0 = this->bit_arrays.at(depth).rank(0, e2);\n\n\t\t\tif (s1_0 != e1_0 and s2_0 != e2_0) {\n\t\t\t\tque.push(make_tuple(s1_0, e1_0, s2_0, e2_0, depth + 1, value));\n\t\t\t}\n\n\n\t\t\tuint64_t s1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s1);\n\t\t\tuint64_t e1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e1);\n\t\t\tuint64_t s2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s2);\n\t\t\tuint64_t e2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e2);\n\n\t\t\tif (s1_1 != e1_1 and s2_1 != e2_1) {\n\t\t\t\tque.push(make_tuple(s1_1, e1_1, s2_1, e2_1, depth + 1, value | (1 << bit_size - depth - 1)));\n\t\t\t}\n\t\t}\n\n\t\treturn intersection;\n\t};\n\nprivate:\n\tuint64_t get_num_of_bit(uint64_t x) {\n\t\tif (x == 0) return 0;\n\t\tx--;\n\t\tuint64_t bit_num = 0;\n\t\twhile (x >> bit_num) {\n\t\t\t++bit_num;\n\t\t}\n\t\treturn bit_num;\n\t}\n};\n\nint main() {\n\tfast;\n\tint n, q; cin >> n >> q;\n\tvector<uint64_t> v(n);\n\tfor (auto &x : v)cin >> x;\n\tDynamicWaveletMatrix dwm(10000000, v);\n\twhile (q--) {\n\t\tint ty, l, r;\n\t\tcin >> ty >> l >> r;\n\t\tif (ty == 0){\n\t\t\tdwm.insert(l, dwm.access(r));\n\t\t\tdwm.erase(r + 1);\n\t\t} else if (ty == 1) {\n\t\t\tcout<<dwm.access(dwm.minRange(l,r+1))<<endl;\n\t\t} else {\n\t\t\tdwm.update(l,r);\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 3300, "memory_kb": 39020, "score_of_the_acc": -1.9902, "final_rank": 19 }, { "submission_id": "aoj_1508_10848599", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)\n#define endl '\\n'\n#define ll long long\n\nenum {\n\tNOTFOUND = 0xFFFFFFFFFFFFFFFFLLU\n};\n\nuint64_t NODE_NO = 0;\n\nclass Node {\npublic:\n\tuint64_t no;\n\tuint64_t num;\n\tuint64_t ones;\n\tNode *left;\n\tNode *right;\n\tint64_t balance;\n\tuint64_t bits;\n\tuint64_t bits_size;\n\n\tbool is_leaf;\n\n\tNode(uint64_t bits, uint64_t bits_size, bool is_leaf) : no(NODE_NO++), num(0), ones(0), bits(bits), bits_size(bits_size), is_leaf(is_leaf), left(nullptr), right(nullptr), balance(0) {}\n\n\tbool is_valid_node() {\n\t\tif (is_leaf) {\n\t\t\tif (num != 0) { return false; }\n\t\t\tif (ones != 0) { return false; }\n\t\t\tif (left != nullptr) { return false; }\n\t\t\tif (right != nullptr) { return false; }\n\t\t}\n\t\telse {\n\t\t\tif (num == 0) { return false; }\n\t\t\tif (left == nullptr) { return false; }\n\t\t\tif (right == nullptr) { return false; }\n\t\t\tif (bits != 0) { return false; }\n\t\t\tif (bits_size != 0) { return false; }\n\t\t\tif (ones > num) {return false; }\n\t\t}\n\t\treturn true;\n\t}\n\n\tstring info() {\n\t\tstring str = \"No:\" + to_string(this->no) + \"\\n\";\n\t\tif (is_leaf) {\n\t\t\tstr += \"size:\" + to_string(this->bits_size) + \"\\n\";\n\t\t\tfor (int i = 0; i < bits_size; ++i) {\n\t\t\t\tstr += to_string((bits >> (uint64_t)i) & (uint64_t)1);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tstr += \"num:\" + to_string(this->num) + \" ones:\" + to_string(this->ones) + \"\\n\";\n\t\t}\n\n\t\treturn str;\n\t}\n};\n\nclass DynamicBitVector {\npublic:\n\tNode *root;\n\tuint64_t size;\n\tuint64_t num_one;\n\tconst uint64_t bits_size_limit = 32;\n\n\tDynamicBitVector(): root(nullptr), size(0), num_one(0) {}\n\n\tDynamicBitVector(vector<uint64_t> &v): root(nullptr), size(0), num_one(0) {\n\t\tif (v.size() == 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tdeque<pair<Node*, uint64_t>> leaves;\n\t\tfor (int i = 0; i < v.size(); i += this->bits_size_limit) {\n\t\t\tuint64_t bits = 0;\n\t\t\tconst uint64_t bits_size = min(this->bits_size_limit, (uint64_t)v.size() - i);\n\t\t\tfor (int j = 0; j < bits_size; ++j) {\n\t\t\t\tassert(v[i + j] == 0 or v[i + j] == 1);\n\t\t\t\tif (v[i + j] == 1) {\n\t\t\t\t\tbits |= (uint64_t)1 << j;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tleaves.emplace_back(make_pair(new Node(bits, bits_size, true), bits_size));\n\t\t}\n\n\n\t\tdeque<tuple<Node*, uint64_t, uint64_t, uint64_t>> nodes;\n\t\twhile (not leaves.empty()) {\n\t\t\tconst auto node = leaves.front().first;\n\t\t\tconst auto bits_size = leaves.front().second;\n\t\t\tleaves.pop_front();\n\t\t\tnodes.emplace_back(make_tuple(node, bits_size, popCount(node->bits), 0));\n\t\t}\n\n\t\twhile (nodes.size() > 1) {\n\n\t\t\tdeque<tuple<Node*, uint64_t, uint64_t, uint64_t>> next_nodes;\n\t\t\twhile (not nodes.empty()) {\n\n\t\t\t\tif (nodes.size() == 1) {\n\t\t\t\t\tnodes.push_front(next_nodes.back());\n\t\t\t\t\tnext_nodes.pop_back();\n\t\t\t\t}\n\n\t\t\t\tNode *left_node;\n\t\t\t\tuint64_t left_num, left_ones, left_height;\n\t\t\t\ttie(left_node, left_num, left_ones, left_height) = nodes.front(); nodes.pop_front();\n\n\t\t\t\tNode *right_node;\n\t\t\t\tuint64_t right_num, right_ones, right_height;\n\t\t\t\ttie(right_node, right_num, right_ones, right_height) = nodes.front(); nodes.pop_front();\n\n\t\t\t\tconst auto internal_node = new Node(0, 0, false);\n\t\t\t\tinternal_node->num = left_num;\n\t\t\t\tinternal_node->ones = left_ones;\n\t\t\t\tinternal_node->left = left_node;\n\t\t\t\tinternal_node->right = right_node;\n\t\t\t\tinternal_node->balance = right_height - left_height;\n\n\t\t\t\tnext_nodes.emplace_back(make_tuple(internal_node, left_num + right_num, left_ones + right_ones, max(left_height, right_height) + 1));\n\t\t\t}\n\n\t\t\tnodes = next_nodes;\n\t\t}\n\n\t\tuint64_t height;\n\t\ttie(this->root, this->size, this->num_one, height) = nodes.front(); nodes.pop_front();\n\t\tassert(this->size == v.size());\n\t}\n\n\n\tuint64_t access(uint64_t pos) {\n\t\tassert(pos < this->size);\n\n\t\treturn access(this->root, pos);\n\t}\n\n\n\tuint64_t rank(uint64_t bit, uint64_t pos) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(pos <= this->size);\n\n\t\tif (bit) {\n\t\t\treturn rank1(this->root, pos, 0);\n\t\t}\n\t\telse {\n\t\t\treturn pos - rank1(this->root, pos, 0);\n\t\t}\n\t}\n\n\n\tuint64_t select(uint64_t bit, uint64_t rank) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(rank > 0);\n\n\t\tif (bit == 0 and rank > this->size - this-> num_one) { return NOTFOUND; }\n\t\tif (bit == 1 and rank > this-> num_one) { return NOTFOUND; }\n\n\t\tif (bit) {\n\t\t\treturn select1(this->root, 0, rank);\n\t\t}\n\t\telse {\n\t\t\treturn select0(this->root, 0, rank);\n\t\t}\n\t}\n\n\n\tvoid insert(uint64_t pos, uint64_t bit) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(pos <= this->size);\n\n\t\tif (root == nullptr) {\n\t\t\troot = new Node(bit, 1, true);\n\t\t} else {\n\t\t\tinsert(this->root, nullptr, bit, pos, this->size);\n\t\t}\n\t\tthis->size++;\n\t\tthis->num_one += (bit == 1);\n\t}\n\n\n\tvoid push_back(uint64_t bit) {\n\t\tassert(bit == 0 or bit == 1);\n\n\t\tthis->insert(this->size, bit);\n\t}\n\n\n\tvoid erase(uint64_t pos) {\n\t\tassert(pos < this->size);\n\n\t\tuint64_t bit = this->remove(this->root, nullptr, pos, this->size, 0, true).first;\n\t\tthis->size--;\n\t\tthis->num_one -= (bit == 1);\n\t}\n\n\n\tvoid update(uint64_t pos, uint64_t bit) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(pos < this->size);\n\n\t\tif (bit == 1) {\n\t\t\tthis->bitset(pos);\n\t\t}\n\t\telse {\n\t\t\tthis->bitclear(pos);\n\t\t}\n\t}\n\n\n\tvoid bitset(uint64_t pos) {\n\t\tassert(pos < this->size);\n\n\t\tbool flip = bitset(this->root, pos);\n\t\tthis->num_one += flip;\n\t}\n\n\n\tvoid bitclear(uint64_t pos) {\n\t\tassert(pos < this->size);\n\n\t\tbool flip = bitclear(this->root, pos);\n\t\tthis->num_one -= flip;\n\t}\n\n\n\tvoid graphviz(const string &file_path) {\n\n\n\t}\n\n\n\n\tbool is_valid_tree(bool verbose) {\n\t\tif (this->root == nullptr) {\n\t\t\tif (this->size == 0 and this->num_one == 0) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tcerr << \"root is nullptr but size is \" << this->size << \" and num_one is \" << this->num_one << endl;\n\t\t\treturn false;\n\t\t}\n\t\tmap<uint64_t, uint64_t> height;\n\t\tget_height(this->root, height);\n\n\t\tqueue<Node*> que;\n\t\tque.emplace(this->root);\n\n\t\twhile (not que.empty()) {\n\t\t\tNode *node = que.front(); que.pop();\n\n\t\t\tif (not node->is_valid_node()) {\n\t\t\t\tif (verbose) {\n\t\t\t\t\tcerr << \"node \" << node->no << \" is invalid node\" << endl;\n\t\t\t\t\tcerr << node->info() << endl;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tif (not node->is_leaf) {\n\t\t\t\tauto left_height = height[node->left->no];\n\t\t\t\tauto right_height = height[node->right->no];\n\n\t\t\t\tif (node->balance != right_height - left_height) {\n\t\t\t\t\tif (verbose) {\n\t\t\t\t\t\tcerr << \"node\" << node->no << \"'s balance is \" << node->balance << \"(left height:\" << left_height << \", right height:\" << right_height << \")\" << endl;\n\t\t\t\t\t}\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\n\t\t\t\tif (node->balance < -1 or 1 < node->balance) {\n\t\t\t\t\tif (verbose) {\n\t\t\t\t\t\tcerr << \"node\" << node->no << \"is not balanced.\" << \"(balance:\" << node->balance << \", left height:\" << left_height << \", right height:\" << right_height << \")\" << endl;\n\t\t\t\t\t}\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\tque.emplace(node->left);\n\t\t\t\tque.emplace(node->right);\n\t\t\t}\n\t\t\telse {\n\n\t\t\t\tif (node->balance != 0) {\n\t\t\t\t\tif (verbose) {\n\t\t\t\t\t\tcerr << \"node \" << node->no << \"'s balance is not 0\" << endl;\n\t\t\t\t\t}\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\nprivate:\n\tuint64_t access(const Node *node, uint64_t pos) {\n\t\tif (node->is_leaf) {\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\t\t\treturn (node->bits >> pos) & (uint64_t)1;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\treturn this->access(node->left, pos);\n\t\t} else {\n\t\t\treturn this->access(node->right, pos - node->num);\n\t\t}\n\t}\n\n\tuint64_t rank1(const Node *node, uint64_t pos, uint64_t ones) {\n\t\tif (node->is_leaf) {\n\t\t\tassert(node->bits_size >= pos);\n\t\t\tconst uint64_t mask = ((uint64_t)1 << pos) - 1;\n\t\t\treturn ones + popCount(node->bits & mask);\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\treturn this->rank1(node->left, pos, ones);\n\t\t} else {\n\t\t\treturn this->rank1(node->right, pos - node->num, ones + node->ones);\n\t\t}\n\t}\n\n\tuint64_t select1(const Node *node, uint64_t pos, uint64_t rank) {\n\t\tif (node->is_leaf) {\n\t\t\treturn pos + this->selectInBlock(node->bits, rank) + 1;\n\t\t}\n\n\t\tif (rank <= node->ones) {\n\t\t\treturn this->select1(node->left, pos, rank);\n\t\t} else {\n\t\t\treturn this->select1(node->right, pos + node->num, rank - node->ones);\n\t\t}\n\t}\n\n\tuint64_t select0(const Node *node, uint64_t pos, uint64_t rank) {\n\t\tif (node->is_leaf) {\n\t\t\treturn pos + this->selectInBlock(~node->bits, rank) + 1;\n\t\t}\n\n\t\tif (rank <= (node->num - node->ones)) {\n\t\t\treturn this->select0(node->left, pos, rank);\n\t\t} else {\n\t\t\treturn this->select0(node->right, pos + node->num, rank - (node->num - node->ones));\n\t\t}\n\t}\n\n\n\tuint64_t selectInBlock(uint64_t bits, uint64_t rank) {\n\t\tconst uint64_t x1 = bits - ((bits & 0xAAAAAAAAAAAAAAAALLU) >> (uint64_t)1);\n\t\tconst uint64_t x2 = (x1 & 0x3333333333333333LLU) + ((x1 >> (uint64_t)2) & 0x3333333333333333LLU);\n\t\tconst uint64_t x3 = (x2 + (x2 >> (uint64_t)4)) & 0x0F0F0F0F0F0F0F0FLLU;\n\n\t\tuint64_t pos = 0;\n\t\tfor (;; pos += 8) {\n\t\t\tconst uint64_t rank_next = (x3 >> pos) & 0xFFLLU;\n\t\t\tif (rank <= rank_next) break;\n\t\t\trank -= rank_next;\n\t\t}\n\n\t\tconst uint64_t v2 = (x2 >> pos) & 0xFLLU;\n\t\tif (rank > v2) {\n\t\t\trank -= v2;\n\t\t\tpos += 4;\n\t\t}\n\n\t\tconst uint64_t v1 = (x1 >> pos) & 0x3LLU;\n\t\tif (rank > v1) {\n\t\t\trank -= v1;\n\t\t\tpos += 2;\n\t\t}\n\n\t\tconst uint64_t v0 = (bits >> pos) & 0x1LLU;\n\t\tif (v0 < rank) {\n\t\t\tpos += 1;\n\t\t}\n\n\t\treturn pos;\n\t}\n\n\n\n\tint64_t insert(Node *node, Node *parent, uint64_t bit, uint64_t pos, uint64_t len) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tif (node->is_leaf) {\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\n\n\t\t\tconst uint64_t up_mask = (((uint64_t)1 << (len - pos)) - 1) << pos;\n\t\t\tconst uint64_t up = (node->bits & up_mask);\n\n\n\t\t\tconst uint64_t down_mask = ((uint64_t)1 << pos) - 1;\n\t\t\tconst uint64_t down = node->bits & down_mask;\n\n\t\t\tnode->bits = (up << (uint64_t)1) | (bit << pos) | down;\n\t\t\tnode->bits_size++;\n\t\t\tassert(node->bits_size == len + 1);\n\n\n\t\t\tif (len + 1 >= 2 * bits_size_limit) {\n\t\t\t\tthis->splitNode(node, len + 1);\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tconst int64_t diff = this->insert(node->left, node, bit, pos, node->num);\n\t\t\tnode->num += 1;\n\t\t\tnode->ones += (bit == 1);\n\t\t\treturn achieveBalance(parent, node, diff, 0);\n\t\t} else {\n\t\t\tconst int64_t diff = this->insert(node->right, node, bit, pos - node->num, len - node->num);\n\t\t\treturn achieveBalance(parent, node, 0, diff);\n\t\t}\n\t}\n\n\n\n\tpair<uint64_t, int64_t> remove(Node *node, Node *parent, uint64_t pos, uint64_t len, uint64_t ones, bool allow_under_flow) {\n\t\tif (node->is_leaf) {\n\n\t\t\tif (node != this->root and len <= bits_size_limit / 2 and not allow_under_flow) {\n\t\t\t\treturn make_pair(NOTFOUND, 0);\n\t\t\t}\n\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\t\t\tassert(pos < len);\n\t\t\tconst uint64_t bit = (node->bits >> pos) & (uint64_t)1;\n\n\n\t\t\tconst uint64_t up_mask = (((uint64_t)1 << (len - pos - 1)) - 1) << (pos + 1);\n\t\t\tconst uint64_t up = (node->bits & up_mask);\n\n\n\t\t\tconst uint64_t down_mask = ((uint64_t)1 << pos) - 1;\n\t\t\tconst uint64_t down = node->bits & down_mask;\n\n\t\t\tnode->bits = (up >> (uint64_t)1) | down;\n\t\t\tnode->bits_size--;\n\t\t\tassert(node->bits_size == len - 1);\n\n\t\t\treturn make_pair(bit, 0);\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tconst auto bit_diff = this->remove(node->left, node, pos, node->num, node->ones, allow_under_flow);\n\t\t\tif (bit_diff.first == NOTFOUND) {\n\t\t\t\treturn bit_diff;\n\t\t\t}\n\n\t\t\tnode->ones -= (bit_diff.first == 1);\n\n\t\t\tif (node->num == bits_size_limit / 2) {\n\t\t\t\tconst auto b_d = remove(node->right, node, 0, len - bits_size_limit / 2, 0, false);\n\n\n\t\t\t\tif (b_d.first == NOTFOUND) {\n\t\t\t\t\tassert(node->left->is_leaf);\n\t\t\t\t\tassert(node->left->bits_size == bits_size_limit / 2 - 1);\n\n\t\t\t\t\tmergeNodes(node->right, 0, len - bits_size_limit / 2, node->left->bits, bits_size_limit / 2 - 1, node->ones, true);\n\t\t\t\t\tthis->replace(parent, node, node->right);\n\t\t\t\t\treturn make_pair(bit_diff.first, -1);\n\t\t\t\t}\n\n\n\t\t\t\tassert(node->left->bits_size == bits_size_limit / 2 - 1);\n\t\t\t\tinsert(node->left, node, b_d.first, bits_size_limit / 2 - 1, bits_size_limit / 2 - 1);\n\t\t\t\tnode->ones += (b_d.first == 1);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnode->num -= 1;\n\t\t\t}\n\n\t\t\tconst int64_t diff = achieveBalance(parent, node, bit_diff.second, 0);\n\t\t\treturn make_pair(bit_diff.first, diff);\n\n\t\t} else {\n\t\t\tconst auto bit_diff = this->remove(node->right, node, pos - node->num, len - node->num, ones - node->ones, allow_under_flow);\n\t\t\tif (bit_diff.first == NOTFOUND) {\n\t\t\t\treturn bit_diff;\n\t\t\t}\n\n\t\t\tones -= (bit_diff.first == 1);\n\n\t\t\tif ((len - node->num) == bits_size_limit / 2) {\n\t\t\t\tconst auto b_d = remove(node->left, node, node->num - 1, node->num, 0, false);\n\n\n\t\t\t\tif (b_d.first == NOTFOUND) {\n\t\t\t\t\tassert(node->right->is_leaf);\n\t\t\t\t\tassert(node->right->bits_size == bits_size_limit / 2 - 1);\n\n\t\t\t\t\tmergeNodes(node->left, node->num, node->num, node->right->bits, bits_size_limit / 2 - 1, ones - node->ones, false);\n\t\t\t\t\tthis->replace(parent, node, node->left);\n\t\t\t\t\treturn make_pair(bit_diff.first, -1);\n\t\t\t\t}\n\n\n\t\t\t\tinsert(node->right, node, b_d.first, 0, bits_size_limit / 2 - 1);\n\t\t\t\tnode->num -= 1;\n\t\t\t\tnode->ones -= (b_d.first == 1);\n\t\t\t}\n\n\t\t\tconst int64_t diff = achieveBalance(parent, node, 0, bit_diff.second);\n\t\t\treturn make_pair(bit_diff.first, diff);\n\t\t}\n\t}\n\n\n\tbool bitset(Node *node, uint64_t pos) {\n\t\tif (node->is_leaf) {\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\t\t\tconst uint64_t bit = (node->bits >> pos) & 1;\n\t\t\tif (bit == 1) {\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tnode->bits |= ((uint64_t)1 << pos);\n\t\t\treturn true;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tbool flip = this->bitset(node->left, pos);\n\t\t\tnode->ones += flip;\n\t\t\treturn flip;\n\t\t} else {\n\t\t\treturn this->bitset(node->right, pos - node->num);\n\t\t}\n\t}\n\n\n\tbool bitclear(Node *node, uint64_t pos) {\n\t\tif (node->is_leaf) {\n\t\t\tassert(pos <= 2 * this->bits_size_limit);\n\n\t\t\tconst uint64_t bit = (node->bits >> pos) & 1;\n\t\t\tif (bit == 0) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tnode->bits &= ~((uint64_t)1 << pos);\n\t\t\treturn true;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tconst bool flip = this->bitclear(node->left, pos);\n\t\t\tnode->ones -= flip;\n\t\t\treturn flip;\n\t\t} else {\n\t\t\treturn this->bitclear(node->right, pos - node->num);\n\t\t}\n\t}\n\n\n\tvoid splitNode(Node *node, uint64_t len) {\n\t\tassert(node->is_leaf);\n\t\tassert(node->bits_size == len);\n\n\n\t\tconst uint64_t left_size = len / 2;\n\t\tconst uint64_t left_bits = node->bits & (((uint64_t)1 << left_size) - 1);\n\t\tnode->left = new Node(left_bits, left_size, true);\n\n\n\t\tconst uint64_t right_size = len - left_size;\n\t\tconst uint64_t right_bits = node->bits >> left_size;\n\t\tnode->right = new Node(right_bits, right_size, true);\n\n\n\t\tnode->is_leaf = false;\n\t\tnode->num = left_size;\n\t\tnode->ones = this->popCount(left_bits);\n\t\tnode->bits = 0;\n\t\tnode->bits_size = 0;\n\t}\n\n\n\tvoid mergeNodes(Node *node, uint64_t pos, uint64_t len, uint64_t bits, uint64_t s, uint64_t ones, bool left) {\n\t\tif (node->is_leaf) {\n\t\t\tif (left) {\n\t\t\t\tnode->bits = (node->bits << s) | bits;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tassert(len == node->bits_size);\n\t\t\t\tnode->bits = node->bits | (bits << len);\n\t\t\t}\n\t\t\tnode->bits_size += s;\n\t\t\treturn;\n\t\t}\n\n\t\tif (pos < node->num) {\n\t\t\tnode->num += s;\n\t\t\tnode->ones += ones;\n\t\t\tmergeNodes(node->left, pos, node->num, bits, s, ones, left);\n\t\t}\n\t\telse {\n\t\t\tmergeNodes(node->right, pos, len - node->num, bits, s, ones, left);\n\t\t}\n\t}\n\n\n\n\tint64_t achieveBalance(Node *parent, Node *node, int64_t leftHeightDiff, int64_t rightHeightDiff) {\n\t\tassert(-1 <= node->balance and node->balance <= 1);\n\t\tassert(-1 <= leftHeightDiff and leftHeightDiff <= 1);\n\t\tassert(-1 <= rightHeightDiff and rightHeightDiff <= 1);\n\n\t\tif (leftHeightDiff == 0 and rightHeightDiff == 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tint64_t heightDiff = 0;\n\n\t\tif ((node->balance <= 0 and leftHeightDiff > 0) or (node->balance >= 0 and rightHeightDiff > 0)) {\n\t\t\t++heightDiff;\n\t\t}\n\n\t\tif ((node->balance < 0 and leftHeightDiff < 0) or (node->balance > 0 and rightHeightDiff < 0)) {\n\t\t\t--heightDiff;\n\t\t}\n\n\t\tnode->balance += -leftHeightDiff + rightHeightDiff;\n\t\tassert(-2 <= node->balance and node->balance <= 2);\n\n\n\t\tif (node->balance == -2) {\n\t\t\tassert(-1 <= node->left->balance and node->left->balance <= 1);\n\t\t\tif (node->left->balance != 0) {\n\t\t\t\theightDiff--;\n\t\t\t}\n\n\t\t\tif (node->left->balance == 1) {\n\t\t\t\treplace(node, node->left, rotateLeft(node->left));\n\t\t\t}\n\t\t\treplace(parent, node, rotateRight(node));\n\t\t}\n\n\t\telse if (node->balance == 2) {\n\t\t\tassert(-1 <= node->right->balance and node->right->balance <= 1);\n\t\t\tif (node->right->balance != 0) {\n\t\t\t\theightDiff--;\n\t\t\t}\n\n\t\t\tif (node->right->balance == -1) {\n\t\t\t\treplace(node, node->right, rotateRight(node->right));\n\t\t\t}\n\t\t\treplace(parent, node, rotateLeft(node));\n\t\t}\n\n\t\treturn heightDiff;\n\t}\n\n\n\tNode *rotateLeft(Node *B) {\n\t\tNode *D = B->right;\n\n\t\tconst int64_t heightC = 0;\n\t\tconst int64_t heightE = heightC + D->balance;\n\t\tconst int64_t heightA = max(heightC, heightE) + 1 - B->balance;\n\n\t\tB->right = D->left;\n\t\tD->left = B;\n\n\t\tB->balance = heightC - heightA;\n\t\tD->num += B->num;\n\t\tD->ones += B->ones;\n\t\tD->balance = heightE - (max(heightA, heightC) + 1);\n\n\t\tassert(-2 <= B->balance and B->balance <= 2);\n\t\tassert(-2 <= D->balance and D->balance <= 2);\n\n\t\treturn D;\n\t}\n\n\n\tNode *rotateRight(Node *D) {\n\t\tNode *B = D->left;\n\n\t\tconst int64_t heightC = 0;\n\t\tconst int64_t heightA = heightC - B->balance;\n\t\tconst int64_t heightE = max(heightA, heightC) + 1 + D->balance;\n\n\t\tD->left = B->right;\n\t\tB->right = D;\n\n\t\tD->num -= B->num;\n\t\tD->ones -= B->ones;\n\t\tD->balance = heightE - heightC;\n\t\tB->balance = max(heightC, heightE) + 1 - heightA;\n\n\n\t\tassert(-2 <= B->balance and B->balance <= 2);\n\t\tassert(-2 <= D->balance and D->balance <= 2);\n\n\t\treturn B;\n\t}\n\n\n\tvoid replace(Node *parent, Node *oldNode, Node *newNode) {\n\t\tif (parent == nullptr) {\n\t\t\tthis->root = newNode;\n\t\t\treturn;\n\t\t}\n\n\t\tif (parent->left == oldNode) {\n\t\t\tparent->left = newNode;\n\t\t}\n\t\telse if (parent->right == oldNode) {\n\t\t\tparent->right = newNode;\n\t\t}\n\t\telse {\n\t\t\tthrow \"old node is not child\";\n\t\t}\n\t}\n\n\tuint64_t popCount(uint64_t x) {\n\t\tx = (x & 0x5555555555555555ULL) + ((x >> (uint64_t)1) & 0x5555555555555555ULL);\n\t\tx = (x & 0x3333333333333333ULL) + ((x >> (uint64_t)2) & 0x3333333333333333ULL);\n\t\tx = (x + (x >> (uint64_t)4)) & 0x0f0f0f0f0f0f0f0fULL;\n\t\tx = x + (x >> (uint64_t)8);\n\t\tx = x + (x >> (uint64_t)16);\n\t\tx = x + (x >> (uint64_t)32);\n\t\treturn x & 0x7FLLU;\n\t}\n\n\n\tuint64_t get_height(Node *node, map<uint64_t, uint64_t> &height) {\n\t\tif (node->is_leaf) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (height.find(node->no) != height.end()) {\n\t\t\treturn height[node->no];\n\t\t}\n\n\t\tauto left_height = get_height(node->left, height);\n\t\tauto right_height = get_height(node->right, height);\n\t\treturn height[node->no] = max(left_height, right_height) + 1;\n\t}\n};\n\n\nclass DynamicWaveletMatrix {\npublic:\n\tvector<DynamicBitVector> bit_arrays;\n\tvector<uint64_t> begin_one;\n\n\tuint64_t size;\n\tuint64_t maximum_element;\n\tuint64_t bit_size;\n\npublic:\n\n\tDynamicWaveletMatrix (uint64_t maximum_element) : size(0), maximum_element(maximum_element + 1) {\n\t\tthis->bit_size = this->get_num_of_bit(maximum_element);\n\t\tif (bit_size == 0) {\n\t\t\tbit_size = 1;\n\t\t}\n\t\tthis->begin_one.resize(bit_size);\n\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tDynamicBitVector sv;\n\t\t\tbit_arrays.push_back(sv);\n\t\t}\n\t}\n\n\tDynamicWaveletMatrix (uint64_t num_of_alphabet, const vector<uint64_t> &array) : size(0), maximum_element(num_of_alphabet + 1) {\n\t\tthis->bit_size = this->get_num_of_bit(num_of_alphabet);\n\t\tif (bit_size == 0) {\n\t\t\tbit_size = 1;\n\t\t}\n\t\tthis->begin_one.resize(bit_size);\n\n\t\tif (array.size() == 0) {\n\t\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\t\tDynamicBitVector sv;\n\t\t\t\tbit_arrays.push_back(sv);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tsize = array.size();\n\n\t\tvector<uint64_t> v(array), b(array.size(), 0);\n\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\n\t\t\tvector<uint64_t> temp;\n\n\t\t\tfor (uint64_t j = 0; j < v.size(); ++j) {\n\t\t\t\tuint64_t c = v.at(j);\n\t\t\t\tuint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\t\tif (bit == 0) {\n\t\t\t\t\ttemp.push_back(c);\n\t\t\t\t\tb[j] = 0;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis->begin_one.at(i) = temp.size();\n\n\n\t\t\tfor (uint64_t j = 0; j < v.size(); ++j) {\n\t\t\t\tuint64_t c = v.at(j);\n\t\t\t\tuint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\t\tif (bit == 1) {\n\t\t\t\t\ttemp.push_back(c);\n\t\t\t\t\tb[j] = 1;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tDynamicBitVector dbv(b);\n\t\t\tbit_arrays.emplace_back(dbv);\n\t\t\tv = temp;\n\t\t}\n\t}\n\n\n\tuint64_t access(uint64_t pos) {\n\t\tif (pos >= this->size) { return NOTFOUND; }\n\n\t\tuint64_t c = 0;\n\t\tfor (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n\t\t\tuint64_t bit = bit_arrays.at(i).access(pos);\n\t\t\tc = (c <<= 1) | bit;\n\t\t\tpos = bit_arrays.at(i).rank(bit, pos);\n\t\t\tif (bit) {\n\t\t\t\tpos += this->begin_one.at(i);\n\t\t\t}\n\t\t}\n\t\treturn c;\n\t}\n\n\n\tuint64_t rank(uint64_t c, uint64_t pos) {\n\t\tassert(pos <= size);\n\t\tif (c >= maximum_element) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tuint64_t left = 0, right = pos;\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tconst uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\tleft = bit_arrays.at(i).rank(bit, left);\n\t\t\tright = bit_arrays.at(i).rank(bit, right);\n\t\t\tif (bit) {\n\t\t\t\tleft += this->begin_one.at(i);\n\t\t\t\tright += this->begin_one.at(i);\n\t\t\t}\n\t\t}\n\n\t\treturn right - left;\n\t}\n\n\n\tuint64_t select(uint64_t c, uint64_t rank) {\n\t\tassert(rank > 0);\n\t\tif (c >= maximum_element) {\n\t\t\treturn NOTFOUND;\n\t\t}\n\n\t\tuint64_t left = 0;\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tconst uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\tleft = bit_arrays.at(i).rank(bit, left);\n\t\t\tif (bit) {\n\t\t\t\tleft += this->begin_one.at(i);\n\t\t\t}\n\t\t}\n\n\t\tuint64_t index = left + rank;\n\t\tfor (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n\t\t\tuint64_t bit = ((c >> i) & 1);\n\t\t\tif (bit == 1) {\n\t\t\t\tindex -= this->begin_one.at(bit_size - i - 1);\n\t\t\t}\n\t\t\tindex = this->bit_arrays.at(bit_size - i - 1).select(bit, index);\n\t\t}\n\t\treturn index;\n\t}\n\n\n\tvoid insert(uint64_t pos, uint64_t c) {\n\t\tassert(pos <= this->size);\n\n\t\tfor (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n\t\t\tconst uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\t\t\tbit_arrays.at(i).insert(pos, bit);\n\t\t\tpos = bit_arrays.at(i).rank(bit, pos);\n\t\t\tif (bit) {\n\t\t\t\tpos += this->begin_one.at(i);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis->begin_one.at(i)++;\n\t\t\t}\n\t\t}\n\n\t\tthis->size++;\n\t}\n\n\n\tvoid push_back(uint64_t c) {\n\t\tthis->insert(this->size, c);\n\t}\n\n\n\tvoid erase(uint64_t pos) {\n\t\tassert(pos < this->size);\n\t\tif (pos >= this->size) {\n\t\t\tthrow \"Segmentation fault\";\n\t\t}\n\n\t\tfor (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n\t\t\tuint64_t bit = bit_arrays.at(i).access(pos);\n\n\t\t\tauto next_pos = bit_arrays.at(i).rank(bit, pos);\n\t\t\tbit_arrays.at(i).erase(pos);\n\n\t\t\tif (bit) {\n\t\t\t\tnext_pos += this->begin_one.at(i);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis->begin_one.at(i)--;\n\t\t\t}\n\t\t\tpos = next_pos;\n\t\t}\n\t\tthis->size--;\n\t}\n\n\n\tvoid update(uint64_t pos, uint64_t c) {\n\t\tassert(pos < this->size);\n\t\tthis->erase(pos);\n\t\tthis->insert(pos, c);\n\t}\n\n\n\tuint64_t maxRange(uint64_t begin_pos, uint64_t end_pos) {\n\t\treturn quantileRange(begin_pos, end_pos, end_pos - begin_pos - 1);\n\t}\n\n\n\tuint64_t minRange(uint64_t begin_pos, uint64_t end_pos) {\n\t\treturn quantileRange(begin_pos, end_pos, 0);\n\t}\n\n\n\n\tuint64_t quantileRange(uint64_t begin_pos, uint64_t end_pos, uint64_t k) {\n\t\tif ((end_pos > size || begin_pos >= end_pos) || (k >= end_pos - begin_pos)) {\n\t\t\treturn NOTFOUND;\n\t\t}\n\n\t\tuint64_t val = 0;\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tuint64_t num_of_zero_begin = bit_arrays.at(i).rank(0, begin_pos);\n\t\t\tuint64_t num_of_zero_end = bit_arrays.at(i).rank(0, end_pos);\n\t\t\tuint64_t num_of_zero = num_of_zero_end - num_of_zero_begin;\n\t\t\tuint64_t bit = (k < num_of_zero) ? 0 : 1;\n\n\t\t\tif (bit) {\n\t\t\t\tk -= num_of_zero;\n\t\t\t\tbegin_pos = this->begin_one.at(i) + begin_pos - num_of_zero_begin;\n\t\t\t\tend_pos = this->begin_one.at(i) + end_pos - num_of_zero_end;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tbegin_pos = num_of_zero_begin;\n\t\t\t\tend_pos = num_of_zero_begin + num_of_zero;\n\t\t\t}\n\n\t\t\tval = ((val << 1) | bit);\n\t\t}\n\n\t\tuint64_t left = 0;\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tconst uint64_t bit = (val >> (bit_size - i - 1)) & 1;\n\t\t\tleft = bit_arrays.at(i).rank(bit, left);\n\t\t\tif (bit) {\n\t\t\t\tleft += this->begin_one.at(i);\n\t\t\t}\n\t\t}\n\n\t\tuint64_t rank = begin_pos + k - left + 1;\n\t\treturn select(val, rank) - 1;\n\t}\n\n\n\tuint64_t rankLessThan(uint64_t c, uint64_t pos) {\n\t\tuint64_t rank_less_than = 0, rank_more_than = 0, rank = 0;\n\t\trankAll(c, 0, pos, rank, rank_less_than, rank_more_than);\n\t\treturn rank_less_than;\n\t}\n\n\n\tuint64_t rankMoreThan(uint64_t c, uint64_t pos) {\n\t\tuint64_t rank_less_than = 0, rank_more_than = 0, rank = 0;\n\t\trankAll(c, 0, pos, rank, rank_less_than, rank_more_than);\n\t\treturn rank_more_than;\n\t}\n\n\n\tvoid rankAll(uint64_t c, uint64_t begin_pos, uint64_t end_pos, uint64_t &rank, uint64_t &rank_less_than, uint64_t &rank_more_than) {\n\t\tif (c >= maximum_element || begin_pos >= size || end_pos > size) {\n\t\t\trank_less_than = NOTFOUND;\n\t\t\trank_more_than = NOTFOUND;\n\t\t\trank = NOTFOUND;\n\t\t\treturn;\n\t\t}\n\t\trank_less_than = 0;\n\t\trank_more_than = 0;\n\t\trank = 0;\n\n\t\tif (begin_pos >= end_pos) {\n\t\t\treturn;\n\t\t}\n\n\t\tvector<uint64_t> more_and_less(2, 0);\n\t\tfor (uint64_t i = 0; i < bit_size; ++i) {\n\t\t\tconst uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\n\t\t\tconst uint64_t range_bits = end_pos - begin_pos;\n\t\t\tconst uint64_t begin_zero = bit_arrays.at(i).rank(0, begin_pos);\n\t\t\tconst uint64_t end_zero = bit_arrays.at(i).rank(0, end_pos);\n\n\t\t\tif (bit) {\n\t\t\t\tbegin_pos += this->begin_one.at(i) - begin_zero;\n\t\t\t\tend_pos += this->begin_one.at(i) - end_zero;\n\t\t\t} else {\n\t\t\t\tbegin_pos = begin_zero;\n\t\t\t\tend_pos = end_zero;\n\t\t\t}\n\t\t\tmore_and_less.at(bit) += range_bits - (end_pos - begin_pos);\n\t\t}\n\n\t\trank_less_than = more_and_less.at(1);\n\t\trank_more_than = more_and_less.at(0);\n\t\trank = end_pos - begin_pos;\n\t}\n\n\n\tvector<pair<uint64_t, uint64_t>> topk(uint64_t s, uint64_t e, uint64_t k) {\n\t\tassert(s < e);\n\t\tvector<pair<uint64_t, uint64_t>> result;\n\n\t\tauto c = [](const tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> &l, const tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> &r) {\n\n\t\t\tif (get<0>(l) != get<0>(r)) {\n\t\t\t\treturn get<0>(l) < get<0>(r);\n\t\t\t}\n\n\t\t\tif (get<3>(l) != get<3>(r)) {\n\t\t\t\treturn get<3>(l) > get<3>(r);\n\t\t\t}\n\n\t\t\tif (get<4>(l) != get<4>(r)) {\n\t\t\t\treturn get<4>(l) > get<4>(r);\n\t\t\t}\n\t\t\treturn true;\n\t\t};\n\n\t\tpriority_queue<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>, vector<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>>, decltype(c)> que(c);\n\t\tque.push(make_tuple(e - s, s, e, 0, 0));\n\n\t\twhile (not que.empty()) {\n\t\t\tauto element = que.top(); que.pop();\n\t\t\tuint64_t width, left, right, depth, value;\n\t\t\ttie(width, left, right, depth, value) = element;\n\n\t\t\tif (depth >= this->bit_size) {\n\t\t\t\tresult.emplace_back(make_pair(value, right - left));\n\t\t\t\tif (result.size() >= k) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\n\t\t\tconst uint64_t left0 = this->bit_arrays.at(depth).rank(0, left);\n\t\t\tconst uint64_t right0 = this->bit_arrays.at(depth).rank(0, right);\n\t\t\tif (left0 < right0) {\n\t\t\t\tque.push(make_tuple(right0 - left0, left0, right0, depth + 1, value));\n\t\t\t}\n\n\n\t\t\tconst uint64_t left1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, left);\n\t\t\tconst uint64_t right1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, right);\n\t\t\tif (left1 < right1) {\n\t\t\t\tque.push(make_tuple(right1 - left1, left1, right1, depth + 1, value | (1 << bit_size - depth - 1)));\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\n\t};\n\n\n\tvector<tuple<uint64_t, uint64_t, uint64_t>> intersect(uint64_t _s1, uint64_t _e1, uint64_t _s2, uint64_t _e2) {\n\t\tassert(_s1 < _e1);\n\t\tassert(_s2 < _e2);\n\n\t\tvector<tuple<uint64_t, uint64_t, uint64_t>> intersection;\n\n\t\tqueue<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>> que;\n\t\tque.push(make_tuple(_s1, _e1, _s2, _e2, 0, 0));\n\t\twhile (not que.empty()) {\n\t\t\tauto e = que.front(); que.pop();\n\t\t\tuint64_t s1, e1, s2, e2, depth, value;\n\t\t\ttie(s1, e1, s2, e2, depth, value) = e;\n\n\t\t\tif (depth >= this->bit_size) {\n\t\t\t\tintersection.emplace_back(make_tuple(value, e1 - s1, e2 - s2));\n\t\t\t\tcontinue;\n\t\t\t}\n\n\n\t\t\tuint64_t s1_0 = this->bit_arrays.at(depth).rank(0, s1);\n\t\t\tuint64_t e1_0 = this->bit_arrays.at(depth).rank(0, e1);\n\t\t\tuint64_t s2_0 = this->bit_arrays.at(depth).rank(0, s2);\n\t\t\tuint64_t e2_0 = this->bit_arrays.at(depth).rank(0, e2);\n\n\t\t\tif (s1_0 != e1_0 and s2_0 != e2_0) {\n\t\t\t\tque.push(make_tuple(s1_0, e1_0, s2_0, e2_0, depth + 1, value));\n\t\t\t}\n\n\n\t\t\tuint64_t s1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s1);\n\t\t\tuint64_t e1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e1);\n\t\t\tuint64_t s2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s2);\n\t\t\tuint64_t e2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e2);\n\n\t\t\tif (s1_1 != e1_1 and s2_1 != e2_1) {\n\t\t\t\tque.push(make_tuple(s1_1, e1_1, s2_1, e2_1, depth + 1, value | (1 << bit_size - depth - 1)));\n\t\t\t}\n\t\t}\n\n\t\treturn intersection;\n\t};\n\nprivate:\n\tuint64_t get_num_of_bit(uint64_t x) {\n\t\tif (x == 0) return 0;\n\t\tx--;\n\t\tuint64_t bit_num = 0;\n\t\twhile (x >> bit_num) {\n\t\t\t++bit_num;\n\t\t}\n\t\treturn bit_num;\n\t}\n};\n\nint main() {\n\tfast;\n\tint n, q; cin >> n >> q;\n\tvector<uint64_t> v(n);\n\tfor (auto &x : v)cin >> x;\n\tDynamicWaveletMatrix dwm(10000000, v);\n\twhile (q--) {\n\t\tint ty, l, r;\n\t\tcin >> ty >> l >> r;\n\t\tif (ty == 0){\n\t\t\tdwm.insert(l, dwm.access(r));\n\t\t\tdwm.erase(r + 1);\n\t\t} else if (ty == 1) {\n\t\t\tcout<<dwm.access(dwm.minRange(l,r+1))<<endl;\n\t\t} else {\n\t\t\tdwm.update(l,r);\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 3330, "memory_kb": 38984, "score_of_the_acc": -1.9987, "final_rank": 20 }, { "submission_id": "aoj_1508_10751048", "code_snippet": "#include<bits/stdc++.h>\n/*\n# pragma GCC target(\"avx2\")\n# pragma GCC optimize(\"O3\")\n# pragma GCC optimize(\"unroll-loops\")\n*/\nusing namespace std;\n\n#define rep(i, l, n) for(int i = (int)(l); i < (int)(n); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntemplate<class T> bool chmin(T &a, T b) {if(a > b) {a = b; return true;} return false;}\ntemplate<class T> bool chmax(T &a, T b) {if(a < b) {a = b; return true;} return false;}\ntemplate<class T> using spq = priority_queue<T, vector<T>, greater<T>>;\n\n//bool -> Yes/No\nstring answer(const bool b) {return b ? \"Yes\" : \"No\";}\n\nvoid fix(int k) {cout << fixed << setprecision(k);}\n\nconst int inf = 2e9;\nconst long long INF = 2e18;\nconst long double eps = 1e-12;\nint dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, -1, 0, 1, 1, -1, 1, -1};\n\ntemplate<class S, \n S(*op)(S, S), \n S(*e)(), \n class F, \n S(*mapping)(F, S), \n F(*composition)(F, F), \n F(*id)()>\nstruct Implicit_Treap {\n\nprivate: \n struct node {\n S val, acc;\n F lazy;\n node *lch, *rch;\n int priority, sz;\n bool rev;\n node(S v, int p) : val(v), priority(p) {\n acc = e(); lazy = id();\n lch = rch = nullptr;\n sz = 1; rev = false;\n }\n };\n\n static int xorshift() {\n static int x = 295289572, y = 176387826, z = 872153741, w = 916527329;\n int t = (x ^ (x << 13));\n x = y, y = z, z = w;\n return (w = (w ^ (w >> 17)) ^ (t ^ (t >> 7)));\n }\n\n node *root = nullptr;\n \n int getsize(node *x) {\n return x == nullptr ? 0 : x->sz;\n }\n\n S getacc(node *x) {\n return x == nullptr ? e() : x->acc;\n }\n\n node *eval(node *x) {\n if(x == nullptr) return x;\n x->sz = getsize(x->lch) + getsize(x->rch) + 1;\n x->acc = op(op(getacc(x->lch), x->val), getacc(x->rch));\n return x;\n }\n \n node *push(node *x) {\n if(x == nullptr) return x;\n if(x->rev) {\n x->rev = false;\n swap(x->lch, x->rch);\n if(x->lch != nullptr) x->lch->rev ^= 1;\n if(x->rch != nullptr) x->rch->rev ^= 1;\n }\n if(x->lch != nullptr) {\n x->lch->lazy = composition(x->lch->lazy, x->lazy);\n x->lch->acc = mapping(x->lazy, x->lch->acc);\n }\n if(x->rch != nullptr) {\n x->rch->lazy = composition(x->rch->lazy, x->lazy);\n x->rch->acc = mapping(x->lazy, x->rch->acc);\n }\n x->val = mapping(x->lazy, x->val);\n x->lazy = id();\n return eval(x);\n }\n\n node *merge(node *l, node *r) {\n if(l == nullptr || r == nullptr) {\n return eval(l == nullptr ? r : l);\n }\n push(l);\n push(r);\n if(l->priority > r->priority) {\n l->rch = merge(l->rch, r);\n return eval(l);\n }\n else {\n r->lch = merge(l, r->lch);\n return eval(r);\n }\n }\n\n pair<node*, node*> split(node *x, int pos) {\n if(x == nullptr) return make_pair(nullptr, nullptr);\n push(x);\n int k = getsize(x->lch) + 1;\n if(pos < k) {\n auto[l, r] = split(x->lch, pos);\n x->lch = r;\n return make_pair(l, eval(x));\n }\n else {\n auto[l, r] = split(x->rch, pos - k);\n x->rch = l;\n return make_pair(eval(x), r);\n }\n }\n\n node *insert(node *x, int pos, S v) {\n auto[l, r] = split(x, pos);\n node *y = new node(v, xorshift());\n return root = merge(merge(l, y), r);\n }\n\n node *erase(node *x, int pos) {\n auto[lm, r] = split(x, pos + 1);\n auto[l, m] = split(lm, pos);\n return root = merge(l, r);\n }\n\n node *get(node *x, int pos) {\n auto[lm, rr] = split(x, pos + 1);\n auto[ll, m] = split(lm, pos);\n node *ret = m;\n root = merge(merge(ll, m), rr);\n return ret;\n }\n\n node *apply(node *x, int l, int r, F f) {\n auto[ll, mr] = split(x, l);\n auto[m, rr] = split(mr, r - l);\n m->lazy = composition(m->lazy, f);\n m->acc = mapping(f, m->acc);\n return x = merge(ll, merge(m, rr));\n }\n\n S prod(node *x, int l, int r) {\n auto[ll, mr] = split(x, l);\n auto[m, rr] = split(mr, r - l);\n S ret = m->acc;\n x = merge(ll, merge(m, rr));\n return ret;\n }\n\n node *reverse(node *x, int l, int r) {\n if(l > r) return x;\n auto[ll, mr] = split(x, l);\n auto[m, rr] = split(mr, r - l);\n m->rev ^= 1;\n return x = merge(ll, merge(m, rr));\n }\n\n node *rotate(node *x, int l, int m, int r) {\n reverse(x, l, r);\n reverse(x, l, l + r - m);\n reverse(x, l + r - m, r);\n return x;\n }\n\n vector<S> display(node *x) {\n if(x == nullptr) return vector<S>(0);\n pushdown(x);\n vector<S> ll = display(x->lch);\n ll.emplace_back(x->val);\n vector<S> rr = display(x->rch);\n ll.insert(ll.end(), make_move_iterator(rr.begin()), make_move_iterator(rr.end()));\n return ll;\n }\n\npublic:\n Implicit_Treap() {}\n\n // xを根としたImplicit_Treapを作る\n Implicit_Treap(node *x) {\n root = x;\n }\n\n Implicit_Treap merge(Implicit_Treap l, Implicit_Treap r) {\n return Implicit_Treap(merge(l.root, r.root));\n }\n\n pair<Implicit_Treap, Implicit_Treap> split(int pos) {\n auto[lp, rp] = split(root, pos);\n return make_pair(Implicit_Treap(lp), Implicit_Treap(rp));\n }\n\n // pos番目(0-indexed)の前に挿入する(最後尾はpos==n)\n void insert(int pos, S v) {\n insert(root, pos, v);\n }\n\n // pos番目(0-indexed)の前にfirst~lastを挿入(最後尾はpos==n)\n template<class Iterator>\n void insert_from_iterator(int pos, Iterator first, Iterator last) {\n for(Iterator itr = first; itr != last; itr++) {\n insert(pos++, *itr);\n }\n }\n\n void erase(int pos) {\n erase(root, pos);\n }\n\n void apply(int l, int r, F f) {\n apply(root, l, r, f);\n }\n\n void apply(int pos, F f) {\n apply(root, pos, pos + 1, f);\n }\n\n S prod(int l, int r) {\n return prod(root, l, r);\n }\n\n void reverse(int l, int r) {\n reverse(root, l, r);\n }\n\n // [l, r)の先頭がmになるように左シフト\n void rotate(int l, int m, int r) {\n rotate(root, l, m, r);\n }\n\n // 保持しているデータを配列として返す\n vector<S> display() {\n return display(root);\n }\n\n S operator[](int pos) {\n return get(root, pos)->acc;\n }\n\n int size() {\n return getsize(root);\n }\n\n};\n\nint op(int a, int b) {return min(a, b);}\nint e() {return inf;}\nint mapping(int a, int b) {return a == -1 ? b : a;}\nint composition(int a, int b) {return b == -1 ? a : b;}\nint id() {return -1;}\n\nvoid main_program() {\n int n, q; cin >> n >> q;\n vector<int> v(n);\n rep(i, 0, n) cin >> v[i];\n Implicit_Treap<int, op, e, int, mapping, composition, id> tr;\n tr.insert_from_iterator(0, all(v));\n while(q--) {\n int t; cin >> t;\n if(t == 0) {\n int l, r; cin >> l >> r;\n tr.rotate(l, r, r + 1);\n }\n else if(t == 1) {\n int l, r; cin >> l >> r;\n cout << tr.prod(l, ++r) << \"\\n\";\n }\n else {\n int x, v; cin >> x >> v;\n tr.apply(x, v);\n }\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n //cin >> t;\n while(t--) main_program();\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 16292, "score_of_the_acc": -0.2245, "final_rank": 12 }, { "submission_id": "aoj_1508_10751023", "code_snippet": "#include<bits/stdc++.h>\n/*\n# pragma GCC target(\"avx2\")\n# pragma GCC optimize(\"O3\")\n# pragma GCC optimize(\"unroll-loops\")\n*/\nusing namespace std;\n\n#define rep(i, l, n) for(int i = (int)(l); i < (int)(n); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntemplate<class T> bool chmin(T &a, T b) {if(a > b) {a = b; return true;} return false;}\ntemplate<class T> bool chmax(T &a, T b) {if(a < b) {a = b; return true;} return false;}\ntemplate<class T> using spq = priority_queue<T, vector<T>, greater<T>>;\n\n//bool -> Yes/No\nstring answer(const bool b) {return b ? \"Yes\" : \"No\";}\n\nvoid fix(int k) {cout << fixed << setprecision(k);}\n\nconst int inf = 2e9;\nconst long long INF = 2e18;\nconst long double eps = 1e-12;\nint dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, -1, 0, 1, 1, -1, 1, -1};\n\ntemplate<class S, \n S(*op)(S, S), \n S(*e)(), \n class F, \n F(*mapping)(F, S), \n F(*composition)(F, F), \n F(*id)()>\nstruct Implicit_Treap {\n\nprivate: \n struct node {\n S val, acc;\n F lazy;\n node *lch, *rch;\n int priority, sz;\n bool rev;\n node(S v, int p) : val(v), priority(p) {\n acc = e(); lazy = id();\n lch = rch = nullptr;\n sz = 1; rev = false;\n }\n };\n\n static int xorshift() {\n static int x = 295289572, y = 176387826, z = 872153741, w = 916527329;\n int t = (x ^ (x << 13));\n x = y, y = z, z = w;\n return (w = (w ^ (w >> 17)) ^ (t ^ (t >> 7)));\n }\n\n node *root = nullptr;\n \n int getsize(node *x) {\n return x == nullptr ? 0 : x->sz;\n }\n\n S getacc(node *x) {\n return x == nullptr ? e() : x->acc;\n }\n\n node *eval(node *x) {\n if(x == nullptr) return x;\n x->sz = getsize(x->lch) + getsize(x->rch) + 1;\n x->acc = op(op(getacc(x->lch), x->val), getacc(x->rch));\n return x;\n }\n \n node *push(node *x) {\n if(x == nullptr) return x;\n if(x->rev) {\n x->rev = false;\n swap(x->lch, x->rch);\n if(x->lch != nullptr) x->lch->rev ^= 1;\n if(x->rch != nullptr) x->rch->rev ^= 1;\n }\n if(x->lch != nullptr) {\n x->lch->lazy = composition(x->lch->lazy, x->lazy);\n x->lch->acc = mapping(x->lazy, x->lch->acc);\n }\n if(x->rch != nullptr) {\n x->rch->lazy = composition(x->rch->lazy, x->lazy);\n x->rch->acc = mapping(x->lazy, x->rch->acc);\n }\n x->val = mapping(x->lazy, x->val);\n x->lazy = id();\n return eval(x);\n }\n\n node *merge(node *l, node *r) {\n if(l == nullptr || r == nullptr) {\n return eval(l == nullptr ? r : l);\n }\n push(l);\n push(r);\n if(l->priority > r->priority) {\n l->rch = merge(l->rch, r);\n return eval(l);\n }\n else {\n r->lch = merge(l, r->lch);\n return eval(r);\n }\n }\n\n pair<node*, node*> split(node *x, int pos) {\n if(x == nullptr) return make_pair(nullptr, nullptr);\n push(x);\n int k = getsize(x->lch) + 1;\n if(pos < k) {\n auto[l, r] = split(x->lch, pos);\n x->lch = r;\n return make_pair(l, eval(x));\n }\n else {\n auto[l, r] = split(x->rch, pos - k);\n x->rch = l;\n return make_pair(eval(x), r);\n }\n }\n\n node *insert(node *x, int pos, S v) {\n auto[l, r] = split(x, pos);\n node *y = new node(v, xorshift());\n return root = merge(merge(l, y), r);\n }\n\n node *erase(node *x, int pos) {\n auto[lm, r] = split(x, pos + 1);\n auto[l, m] = split(lm, pos);\n return root = merge(l, r);\n }\n\n node *get(node *x, int pos) {\n auto[lm, rr] = split(x, pos + 1);\n auto[ll, m] = split(lm, pos);\n node *ret = m;\n root = merge(merge(ll, m), rr);\n return ret;\n }\n\n node *apply(node *x, int l, int r, F f) {\n auto[ll, mr] = split(x, l);\n auto[m, rr] = split(mr, r - l);\n m->lazy = composition(m->lazy, f);\n m->acc = mapping(f, m->acc);\n return x = merge(ll, merge(m, rr));\n }\n\n S prod(node *x, int l, int r) {\n auto[ll, mr] = split(x, l);\n auto[m, rr] = split(mr, r - l);\n S ret = m->acc;\n x = merge(ll, merge(m, rr));\n return ret;\n }\n\n node *reverse(node *x, int l, int r) {\n if(l > r) return x;\n auto[ll, mr] = split(x, l);\n auto[m, rr] = split(mr, r - l);\n m->rev ^= 1;\n return x = merge(ll, merge(m, rr));\n }\n\n node *rotate(node *x, int l, int m, int r) {\n reverse(x, l, r);\n reverse(x, l, l + r - m);\n reverse(x, l + r - m, r);\n return x;\n }\n\n vector<S> display(node *x) {\n if(x == nullptr) return vector<S>(0);\n pushdown(x);\n vector<S> ll = display(x->lch);\n ll.emplace_back(x->val);\n vector<S> rr = display(x->rch);\n ll.insert(ll.end(), make_move_iterator(rr.begin()), make_move_iterator(rr.end()));\n return ll;\n }\n\npublic:\n Implicit_Treap() {}\n\n // xを根としたImplicit_Treapを作る\n Implicit_Treap(node *x) {\n root = x;\n }\n\n Implicit_Treap merge(Implicit_Treap l, Implicit_Treap r) {\n return Implicit_Treap(merge(l.root, r.root));\n }\n\n pair<Implicit_Treap, Implicit_Treap> split(int pos) {\n auto[lp, rp] = split(root, pos);\n return make_pair(Implicit_Treap(lp), Implicit_Treap(rp));\n }\n\n // pos番目(0-indexed)の前に挿入する(最後尾はpos==n)\n void insert(int pos, S v) {\n insert(root, pos, v);\n }\n\n // pos番目(0-indexed)の前にfirst~lastを挿入(最後尾はpos==n)\n template<class Iterator>\n void insert_from_iterator(int pos, Iterator first, Iterator last) {\n for(Iterator itr = first; itr != last; itr++) {\n insert(pos++, *itr);\n }\n }\n\n void erase(int pos) {\n erase(root, pos);\n }\n\n void apply(int l, int r, F f) {\n apply(root, l, r, f);\n }\n\n void apply(int pos, F f) {\n apply(root, pos, pos + 1, f);\n }\n\n S prod(int l, int r) {\n return prod(root, l, r);\n }\n\n void reverse(int l, int r) {\n reverse(root, l, r);\n }\n\n // [l, r)の先頭がmになるように左シフト\n void rotate(int l, int m, int r) {\n rotate(root, l, m, r);\n }\n\n // 保持しているデータを配列として返す\n vector<S> display() {\n return display(root);\n }\n\n S operator[](int pos) {\n return get(root, pos)->acc;\n }\n\n int size() {\n return getsize(root);\n }\n\n};\n\nint op(int a, int b) {return min(a, b);}\nint e() {return inf;}\nint mapping(int a, int b) {return a == -1 ? b : a;}\nint composition(int a, int b) {return b == -1 ? a : b;}\nint id() {return -1;}\n\nvoid main_program() {\n int n, q; cin >> n >> q;\n vector<int> v(n);\n rep(i, 0, n) cin >> v[i];\n Implicit_Treap<int, op, e, int, mapping, composition, id> tr;\n tr.insert_from_iterator(0, all(v));\n while(q--) {\n int t; cin >> t;\n if(t == 0) {\n int l, r; cin >> l >> r;\n tr.rotate(l, r, r + 1);\n }\n else if(t == 1) {\n int l, r; cin >> l >> r;\n cout << tr.prod(l, ++r) << \"\\n\";\n }\n else {\n int x, v; cin >> x >> v;\n tr.apply(x, v);\n }\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int t = 1;\n //cin >> t;\n while(t--) main_program();\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 16292, "score_of_the_acc": -0.2212, "final_rank": 11 }, { "submission_id": "aoj_1508_10465029", "code_snippet": "#include<iostream> \nusing namespace std;\n\nstruct SplayNode{\n SplayNode *left, *right, *parent;\n int size, value, minimum;\n\n SplayNode(){\n left = nullptr;\n right = nullptr;\n parent = nullptr;\n size = 1;\n }\n\n void rotate(){\n SplayNode *pp, *p, *c;\n p = this->parent;\n pp = p->parent;\n\n if(p->left == this){\n c = this->right;\n this->right = p;\n p->left = c;\n }\n else{\n c = this->left;\n this->left = p;\n p->right = c;\n }\n if(pp && pp->left == p)pp->left = this;\n if(pp && pp->right == p)pp->right = this;\n this->parent = pp;\n p->parent = this;\n if(c)c->parent = p;\n\n p->update();\n this->update();\n }\n\n int state(){\n if(!this->parent)return 0;\n if(this->parent->left == this)return 1;\n if(this->parent->right == this)return -1;\n return 0;\n }\n \n void splay(){\n while(this->state() != 0){\n if(this->parent->state() == 0){\n this->rotate();\n }\n else if(this->state() == this->parent->state()){\n this->parent->rotate();\n this->rotate();\n }else{\n this->rotate();\n this->rotate();\n }\n }\n }\n \n void update(){\n this->size = 1;\n this->minimum = this->value;\n if(this->left){\n this->size += this->left->size;\n this->minimum = min(this->minimum, this->left->minimum);\n }\n if(this->right){\n this->size += this->right->size;\n this->minimum = min(this->minimum, this->right->minimum);\n }\n } \n};\n\n\nSplayNode *get(int ind, SplayNode *root){\n SplayNode *now = root;\n while(true){\n int lsize = now->left ? now->left->size : 0;\n if(ind < lsize){\n now = now->left;\n }\n if(ind == lsize){\n now->splay();\n return now;\n }\n if(ind > lsize){\n now = now->right;\n ind = ind - lsize - 1;\n }\n }\n}\n\nSplayNode *merge(SplayNode *lroot, SplayNode *rroot){\n if(!lroot)return rroot;\n if(!rroot)return lroot;\n lroot = get(lroot->size-1, lroot);\n lroot->right = rroot;\n rroot->parent = lroot;\n lroot->update();\n return lroot;\n}\n\npair<SplayNode*, SplayNode*> split(int left_cnt, SplayNode *root){\n if(left_cnt == 0)return {nullptr, root};\n if(left_cnt == root->size)return {root, nullptr};\n root = get(left_cnt, root);\n SplayNode *lroot, *rroot;\n lroot = root->left;\n rroot = root;\n rroot->left = nullptr;\n lroot->parent = nullptr;\n rroot->update();\n return {lroot, rroot};\n}\n\nSplayNode *insert(int ind, SplayNode *node, SplayNode *root){\n auto trees = split(ind, root);\n SplayNode *lroot = trees.first;\n SplayNode *rroot = trees.second;\n return merge(merge(lroot, node), rroot);\n}\n\npair<SplayNode*, SplayNode*> remove(int ind, SplayNode *root){\n root = get(ind, root);\n SplayNode *lroot = root->left;\n SplayNode *rroot = root->right;\n if(lroot)lroot->parent = nullptr;\n if(rroot)rroot->parent = nullptr;\n root->left = nullptr;\n root->right = nullptr;\n root->update();\n return {merge(lroot, rroot), root};\n}\n\nSplayNode *shift(int l, int r, SplayNode *root){\n auto trees = remove(r, root);\n root = trees.first;\n SplayNode *node = trees.second;\n return insert(l, node, root);\n}\n\npair<SplayNode*, int> rmq(int l, int r, SplayNode *root){\n SplayNode *lroot, *croot, *rroot;\n auto tmp = split(r+1, root);\n rroot = tmp.second;\n tmp = split(l, tmp.first);\n lroot = tmp.first;\n croot = tmp.second;\n int ans = croot->minimum;\n return {merge(merge(lroot, croot), rroot), ans};\n}\n\nint main(){\n int n,q;\n cin >> n >> q;\n int vecsize = 0;\n SplayNode node[n];\n for(int i = 0; i < n-1; i++){\n node[i].parent = &node[i+1];\n node[i+1].left = &node[i];\n node[i+1].update();\n }\n for(int i = 0; i < n; i++){\n cin >> node[i].value;\n node[i].update();\n }\n SplayNode *root = &node[n-1];\n for(int i = 0; i < q; i++){\n int ord,x;\n cin >> ord;\n if(ord == 0){\n int l, r;\n cin >> l >> r;\n root = shift(l, r, root);\n }\n if(ord == 1){\n int l, r;\n cin >> l >> r;\n auto tmp = rmq(l, r, root);\n root = tmp.first;\n cout << tmp.second << endl;\n }\n if(ord == 2){\n int pos, val;\n cin >> pos >> val;\n root = get(pos, root);\n root->value = val;\n root->update();\n }\n }\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 11460, "score_of_the_acc": -0.0098, "final_rank": 1 }, { "submission_id": "aoj_1508_10397819", "code_snippet": "// ================================================\n//\n// RMQ\n// https://onlinejudge.u-aizu.ac.jp/challenges/sources/UOA/UAPC/1508\n//\n// [SplayTree]\n//\n// ================================================\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nstruct SplayNode {\n SplayNode *left, *right, *parent;\n int size, value, minimum;\n SplayNode() {\n left = nullptr;\n right = nullptr;\n parent = nullptr;\n size = 1;\n }\n\n void rotate() {\n SplayNode *pp, *p, *c;\n p = this->parent;\n pp = p->parent;\n\n if (p->left == this) {\n c = this->right;\n this->right = p;\n p->left = c;\n } else {\n c = this->left;\n this->left = p;\n p->right = c;\n }\n\n if (pp and pp->left == p) pp->left = this;\n if (pp and pp->right == p) pp->right = this;\n this->parent = pp;\n p->parent = this;\n if (c) c->parent = p;\n\n p->update();\n this->update();\n }\n\n int state() {\n if (not this->parent) return 0;\n if (this->parent->left == this) return 1;\n if (this->parent->right == this) return -1;\n return 0;\n }\n\n void splay() {\n while (this->state() != 0) {\n if (this->parent->state() == 0) {\n this->rotate();\n } else if (this->state() == this->parent->state()) {\n this->parent->rotate();\n this->rotate();\n } else {\n this->rotate();\n this->rotate();\n }\n }\n }\n\n void update() {\n this->size = 1;\n this->minimum = this->value;\n if (this->left) {\n this->size += this->left->size;\n this->minimum = min(this->minimum, this->left->minimum);\n }\n if (this->right) {\n this->size += this->right->size;\n this->minimum = min(this->minimum, this->right->minimum);\n }\n }\n};\n\nusing SN = SplayNode;\n\nSN *get(int pos, SN *root) {\n SN *now = root;\n while (true) {\n int lsize = now->left ? now->left->size : 0;\n if (pos < lsize) {\n now = now->left;\n }\n if (pos == lsize) {\n now->splay();\n return now;\n }\n if (pos > lsize) {\n now = now->right;\n pos = pos - lsize - 1;\n }\n }\n}\n\nSN *merge(SN *lroot, SN *rroot) {\n if (not lroot) return rroot;\n if (not rroot) return lroot;\n // 左木の右端を取得\n lroot = get(lroot->size - 1, lroot);\n // 左木野右端に右木を結合\n lroot->right = rroot;\n rroot->parent = lroot;\n lroot->update();\n return lroot;\n};\n\npair<SN *, SN *> split(int pos, SN *root) {\n if (pos == 0) return {nullptr, root};\n if (pos == root->size) return {root, nullptr};\n // 分割点を取得\n root = get(pos, root);\n SN *lroot = root->left, *rroot = root;\n // 双方向に分離\n rroot->left = nullptr;\n lroot->parent = nullptr;\n rroot->update();\n return {lroot, rroot};\n};\n\nSN *insert(int pos, SN *node, SN *root) {\n SN *lroot, *rroot;\n tie(lroot, rroot) = split(pos, root);\n return merge(merge(lroot, node), rroot);\n};\n\npair<SN *, SN *> remove(int pos, SN *root) {\n root = get(pos, root);\n SN *lroot = root->left;\n SN *rroot = root->right;\n if (lroot) lroot->parent = nullptr;\n if (rroot) rroot->parent = nullptr;\n root->left = nullptr;\n root->right = nullptr;\n root->update();\n return {merge(lroot, rroot), root};\n};\n\nSN *shift(int l, int r, SN *root) {\n SN *node;\n tie(root, node) = remove(r, root);\n return insert(l, node, root);\n}\n\npair<SN *, int> rmq(int l, int r, SN *root) {\n SN *lroot, *rroot, *croot;\n tie(root, rroot) = split(r + 1, root);\n tie(lroot, croot) = split(l, root);\n int ans = croot->minimum;\n return {merge(merge(lroot, croot), rroot), ans};\n}\n\nSN nodes[220000];\n\nint main() {\n int n, q;\n cin >> n >> q;\n\n for (int i = 0; i < n - 1; i++) {\n nodes[i].parent = &nodes[i + 1];\n nodes[i + 1].left = &nodes[i];\n nodes[i + 1].update();\n }\n\n for (int i = 0; i < n; i++) {\n cin >> nodes[i].value;\n nodes[i].update();\n }\n\n SN *root = &nodes[n - 1];\n\n for (int i = 0; i < q; i++) {\n int cmd, l, r, pos, val;\n cin >> cmd;\n if (cmd == 0) { // shift\n cin >> l >> r;\n root = shift(l, r, root);\n } else if (cmd == 1) { // rmq\n cin >> l >> r;\n auto tmp = rmq(l, r, root);\n root = tmp.first;\n cout << tmp.second << endl;\n } else if (cmd == 2) { // set\n cin >> pos >> val;\n root = get(pos, root);\n root->value = val;\n root->update();\n }\n }\n}\n\n// ================================================", "accuracy": 1, "time_ms": 310, "memory_kb": 12208, "score_of_the_acc": -0.037, "final_rank": 4 }, { "submission_id": "aoj_1508_10324159", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i = 0; i < (n); ++i)\n#define repp(i,n) for(int i = 1; i <= (n); ++i)\n#define drep(i,n) for(int i = (n)-1; i >= 0; --i)\n#define srep(i,s,t) for (int i = s; i < (t); ++i)\n#define rng(a) a.begin(),a.end()\n#define rrng(a) a.rbegin(),a.rend()\n#define fi first\n#define se second\n#define pb push_back\n#define eb emplace_back\n#define em emplace\n#define pob pop_back\n#define sz(x) (int)(x).size()\n#define pcnt __builtin_popcountll\n#define koulet srand((unsigned)clock()+(unsigned)time(NULL));\n#define newline puts(\"\")\n#define vc vector\n#define stoi stoll \ntemplate<class T> using vv = vc<vc<T>>;\ntemplate<class T> using PQ = priority_queue<T,vc<T>,greater<T>>;\nusing uint = unsigned; using ull = unsigned long long;\nusing i128=__int128 ;\nusing vi = vc<int>; using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long; using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>;\nusing P = pair<int,int>; using vp = vc<P>; using vvp = vv<P>; using LP = pair<ll,ll>;\nusing vs = vc<string>; using vb=vc<bool> ; using vlp = vc<LP> ; \nint geti(){int x;scanf(\"%d\",&x);return x;}\nvi pm(int n, int s=0) { vi a(n); iota(rng(a),s); return a;}\ntemplate<class T1,class T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}\ntemplate<class T1,class T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<\",\"<<v.se;}\ntemplate<class T>istream& operator>>(istream&i,vc<T>&v){rep(j,sz(v))i>>v[j];return i;}\ntemplate<class T>string join(const T&v,const string&d=\"\"){stringstream s;rep(i,sz(v))(i?s<<d:s)<<v[i];return s.str();}\ntemplate<class T>ostream& operator<<(ostream&o,const vc<T>&v){if(sz(v))o<<join(v,\" \");return o;}\ntemplate<class T>void vin(vc<T>&a){int n;cin>>n;a=vc<T>(n);cin>>a;}\ntemplate<class T>void vin(vv<T>&a){int n,m;cin>>n>>m;a=vv<T>(n,vc<T>(m));cin>>a;}\ntemplate<class T1,class T2>void operator--(pair<T1,T2>&a,int){a.fi--;a.se--;}\ntemplate<class T1,class T2>void operator++(pair<T1,T2>&a,int){a.fi++;a.se++;}\ntemplate<class T>void operator--(vc<T>&a,int){for(T&x:a)x--;}\ntemplate<class T>void operator++(vc<T>&a,int){for(T&x:a)x++;}\ntemplate<class T>void operator+=(vc<T>&a,T b){for(T&x:a)x+=b;}\ntemplate<class T>void operator-=(vc<T>&a,T b){for(T&x:a)x-=b;}\ntemplate<class T>void operator*=(vc<T>&a,T b){for(T&x:a)x*=b;}\ntemplate<class T>void operator/=(vc<T>&a,T b){for(T&x:a)x/=b;}\ntemplate<class T>void operator+=(vc<T>&a,const vc<T>&b){a.insert(a.end(),rng(b));}\ntemplate<class T1,class T2>pair<T1,T2>operator+(const pair<T1,T2>&a,const pair<T1,T2>&b){return {a.fi+b.fi,a.se+b.se};}\ntemplate<class T1,class T2>pair<T1,T2>operator-(const pair<T1,T2>&a,const pair<T1,T2>&b){return {a.fi-b.fi,a.se-b.se};}\ntemplate<class T>pair<T,T>operator*(const pair<T,T>&a,T b){return {a.fi*b,a.se*b};}\ntemplate<class T1,class T2>bool mins(T1& x,const T2&y){if(y<x){x=y;return true;}else return false;}\ntemplate<class T1,class T2>bool maxs(T1& x,const T2&y){if(x<y){x=y;return true;}else return false;}\ntemplate<class T>T min(const vc<T>&a){return *min_element(rng(a));}\ntemplate<class T>T max(const vc<T>&a){return *max_element(rng(a));}\ntemplate<class Tx,class Ty>Tx dup(Tx x, Ty y){return (x+y-1)/y;}\ntemplate<class T>ll suma(const vc<T>&a){ll s=0;for(auto&&x:a)s+=x;return s;}\ntemplate<class T>ll suma(const vv<T>&a){ll s=0;for(auto&&x:a)s+=suma(x);return s;}\ntemplate<class T>void uni(T&a){sort(rng(a));a.erase(unique(rng(a)),a.end());}\ntemplate<class T>void prepend(vc<T>&a,const T&x){a.insert(a.begin(),x);}\ntemplate<typename T>T floor(T a, T b){T r=(a%b+b)%b;return (a-r)/b;}\ntemplate<typename T>T ceil(T a, T b){return floor(a+b-1,b);}\ntemplate<typename T>vc<T> cumsum(vc<T> &a,int off=1){int N=sz(a);vc<T>B(N+1);rep(i, N)B[i+1]=a[i]+B[i];if(off==0)B.erase(B.begin());return B;}\nconst double eps = 1e-10; // a<b -> a<b-eps\nconst ll LINF = 1001002003004005006ll;\nconst int INF = 1001001001;\ntemplate <class T>\nconstexpr T infty = 0;\ntemplate <>\nconstexpr int infty<int> = 1'010'000'000;\ntemplate <>\nconstexpr ll infty<ll> = 2'020'000'000'000'000'000;\ntemplate <>\nconstexpr uint infty<uint> = infty<int>;\ntemplate <>\nconstexpr ull infty<ull> = infty<ll>;\ntemplate <>\nconstexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;\ntemplate <>\nconstexpr double infty<double> = infty<ll>;\ntemplate <>\nconstexpr long double infty<long double> = infty<ll>;\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(uint x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(ull x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(uint x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(ull x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n#define dame { puts(\"-1\"); return 0 ;}\n#define yes { puts(\"Yes\"); return 0 ;}\n#define no { puts(\"No\"); return 0 ;}\n#define rtn(x) { cout<<(x)<<endl;} // flush!\n#define yn {puts(\"Yes\");}else{puts(\"No\");}\nll c2(ll n) { return n*(n-1)>>1;}\ntemplate<typename T>void rot(T& a,int i){rotate(a.begin(),a.begin()+(i),a.end());}// i回シフト\ntemplate<typename T>void rot(vv<T>& a){int h=sz(a),w=sz(a[0]);vv<T> p(w,vc<T>(h));swap(a,p);rep(i,h)rep(j,w)a[j][h-1-i]=p[i][j];}\nvoid rot(vc<string>& a){int h=sz(a),w=sz(a[0]);vc<string> p(w,string(h,'?'));swap(a,p);rep(i,h)rep(j,w)a[j][h-1-i]=p[i][j];}\n//90度時計回り回転\nconst int dx[] = {-1,0,1,0,1,1,-1,-1,}, dy[] = {0,-1,0,1,1,-1,1,-1}; //^<v>↘︎\n//^, <, v, >, ↘︎, ↙︎, ↗︎, ↖︎ \n\ntemplate <typename ActedMonoid, bool PERSISTENT=false>\nstruct RBST_ActedMonoid {\n using Monoid_X = typename ActedMonoid::Monoid_X;\n using Monoid_A = typename ActedMonoid::Monoid_A;\n using X = typename Monoid_X::value_type;\n using A = typename Monoid_A::value_type;\n\n struct Node {\n Node *l, *r;\n X x, prod; // lazy, rev 反映済\n A lazy;\n uint size;\n bool rev;\n };\n\n Node *pool;\n const int NODES;\n int pid;\n using np = Node *;\n\n RBST_ActedMonoid(int NODES) : NODES(NODES), pid(0) { pool = new Node[NODES]; }\n ~RBST_ActedMonoid() { delete[] pool; }\n\n void reset() { pid = 0; }\n\n np new_node(const X &x) {\n pool[pid].l = pool[pid].r = nullptr;\n pool[pid].x = x;\n pool[pid].prod = x;\n pool[pid].lazy = Monoid_A::unit();\n pool[pid].size = 1;\n pool[pid].rev = 0;\n return &(pool[pid++]);\n }\n\n np new_node(const vc<X> &dat) {\n auto dfs = [&](auto &dfs, uint l, uint r) -> np {\n if (l == r) return nullptr;\n if (r == l + 1) return new_node(dat[l]);\n uint m = (l + r) / 2;\n np l_root = dfs(dfs, l, m);\n np r_root = dfs(dfs, m + 1, r);\n np root = new_node(dat[m]);\n root->l = l_root, root->r = r_root;\n update(root);\n return root;\n };\n return dfs(dfs, 0, sz(dat));\n }\n\n np copy_node(np &n) {\n if (!n || !PERSISTENT) return n;\n pool[pid].l = n->l, pool[pid].r = n->r;\n pool[pid].x = n->x;\n pool[pid].prod = n->prod;\n pool[pid].lazy = n->lazy;\n pool[pid].size = n->size;\n pool[pid].rev = n->rev;\n return &(pool[pid++]);\n }\n\n np merge(np l_root, np r_root) { return merge_rec(l_root, r_root); }\n np merge3(np a, np b, np c) { return merge(merge(a, b), c); }\n np merge4(np a, np b, np c, np d) { return merge(merge(merge(a, b), c), d); }\n pair<np, np> split(np root, uint k) {\n if (!root) {\n assert(k == 0);\n return {nullptr, nullptr};\n }\n assert(0 <= k && k <= root->size);\n return split_rec(root, k);\n }\n tuple<np, np, np> split3(np root, uint l, uint r) {\n np nm, nr;\n tie(root, nr) = split(root, r);\n tie(root, nm) = split(root, l);\n return {root, nm, nr};\n }\n tuple<np, np, np, np> split4(np root, uint i, uint j, uint k) {\n np d;\n tie(root, d) = split(root, k);\n auto [a, b, c] = split3(root, i, j);\n return {a, b, c, d};\n }\n\n X prod(np root, uint l, uint r) {\n if (l == r) return Monoid_X::unit();\n return prod_rec(root, l, r, false);\n }\n X prod(np root) { return (root ? root->prod : Monoid_X::unit()); }\n\n np reverse(np root, uint l, uint r) {\n assert(Monoid_X::commute);\n assert(0 <= l && l <= r && r <= root->size);\n if (r - l <= 1) return root;\n auto [nl, nm, nr] = split3(root, l, r);\n nm->rev ^= 1;\n swap(nm->l, nm->r);\n return merge3(nl, nm, nr);\n }\n\n np apply(np root, uint l, uint r, const A a) {\n assert(0 <= l && l <= r && r <= root->size);\n return apply_rec(root, l, r, a);\n }\n np apply(np root, const A a) {\n if (!root) return root;\n return apply_rec(root, 0, root->size, a);\n }\n\n np set(np root, uint k, const X &x) { return set_rec(root, k, x); }\n np multiply(np root, uint k, const X &x) { return multiply_rec(root, k, x); }\n X get(np root, uint k) { return get_rec(root, k, false, Monoid_A::unit()); }\n\n vc<X> get_all(np root) {\n vc<X> res;\n auto dfs = [&](auto &dfs, np root, bool rev, A lazy) -> void {\n if (!root) return;\n X me = ActedMonoid::act(root->x, lazy, 1);\n lazy = Monoid_A::op(root->lazy, lazy);\n dfs(dfs, (rev ? root->r : root->l), rev ^ root->rev, lazy);\n res.eb(me);\n dfs(dfs, (rev ? root->l : root->r), rev ^ root->rev, lazy);\n };\n dfs(dfs, root, 0, Monoid_A::unit());\n return res;\n }\n\n template <typename F>\n pair<np, np> split_max_right(np root, const F check) {\n assert(check(Monoid_X::unit()));\n X x = Monoid_X::unit();\n return split_max_right_rec(root, check, x);\n }\n\nprivate:\n inline uint xor128() {\n static uint x = 123456789;\n static uint y = 362436069;\n static uint z = 521288629;\n static uint w = 88675123;\n uint t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n void prop(np c) {\n // 自身をコピーする必要はない。\n // 子をコピーする必要がある。複数の親を持つ可能性があるため。\n bool bl_lazy = (c->lazy != Monoid_A::unit());\n bool bl_rev = c->rev;\n if (bl_lazy || bl_rev) {\n c->l = copy_node(c->l);\n c->r = copy_node(c->r);\n }\n if (c->lazy != Monoid_A::unit()) {\n if (c->l) {\n c->l->x = ActedMonoid::act(c->l->x, c->lazy, 1);\n c->l->prod = ActedMonoid::act(c->l->prod, c->lazy, c->l->size);\n c->l->lazy = Monoid_A::op(c->l->lazy, c->lazy);\n }\n if (c->r) {\n c->r->x = ActedMonoid::act(c->r->x, c->lazy, 1);\n c->r->prod = ActedMonoid::act(c->r->prod, c->lazy, c->r->size);\n c->r->lazy = Monoid_A::op(c->r->lazy, c->lazy);\n }\n c->lazy = Monoid_A::unit();\n }\n if (c->rev) {\n if (c->l) {\n c->l->rev ^= 1;\n swap(c->l->l, c->l->r);\n }\n if (c->r) {\n c->r->rev ^= 1;\n swap(c->r->l, c->r->r);\n }\n c->rev = 0;\n }\n }\n\n void update(np c) {\n // データを保ったまま正常化するだけなので、コピー不要\n c->size = 1;\n c->prod = c->x;\n if (c->l) {\n c->size += c->l->size;\n c->prod = Monoid_X::op(c->l->prod, c->prod);\n }\n if (c->r) {\n c->size += c->r->size;\n c->prod = Monoid_X::op(c->prod, c->r->prod);\n }\n }\n\n np merge_rec(np l_root, np r_root) {\n if (!l_root) return r_root;\n if (!r_root) return l_root;\n uint sl = l_root->size, sr = r_root->size;\n if (xor128() % (sl + sr) < sl) {\n prop(l_root);\n l_root = copy_node(l_root);\n l_root->r = merge_rec(l_root->r, r_root);\n update(l_root);\n return l_root;\n }\n prop(r_root);\n r_root = copy_node(r_root);\n r_root->l = merge_rec(l_root, r_root->l);\n update(r_root);\n return r_root;\n }\n\n pair<np, np> split_rec(np root, uint k) {\n if (!root) return {nullptr, nullptr};\n prop(root);\n uint sl = (root->l ? root->l->size : 0);\n if (k <= sl) {\n auto [nl, nr] = split_rec(root->l, k);\n root = copy_node(root);\n root->l = nr;\n update(root);\n return {nl, root};\n }\n auto [nl, nr] = split_rec(root->r, k - (1 + sl));\n root = copy_node(root);\n root->r = nl;\n update(root);\n return {root, nr};\n }\n\n np set_rec(np root, uint k, const X &x) {\n if (!root) return root;\n prop(root);\n uint sl = (root->l ? root->l->size : 0);\n if (k < sl) {\n root = copy_node(root);\n root->l = set_rec(root->l, k, x);\n update(root);\n return root;\n }\n if (k == sl) {\n root = copy_node(root);\n root->x = x;\n update(root);\n return root;\n }\n root = copy_node(root);\n root->r = set_rec(root->r, k - (1 + sl), x);\n update(root);\n return root;\n }\n\n np multiply_rec(np root, uint k, const X &x) {\n if (!root) return root;\n prop(root);\n uint sl = (root->l ? root->l->size : 0);\n if (k < sl) {\n root = copy_node(root);\n root->l = multiply_rec(root->l, k, x);\n update(root);\n return root;\n }\n if (k == sl) {\n root = copy_node(root);\n root->x = Monoid_X::op(root->x, x);\n update(root);\n return root;\n }\n root = copy_node(root);\n root->r = multiply_rec(root->r, k - (1 + sl), x);\n update(root);\n return root;\n }\n\n X prod_rec(np root, uint l, uint r, bool rev) {\n if (l == 0 && r == root->size) { return root->prod; }\n np left = (rev ? root->r : root->l);\n np right = (rev ? root->l : root->r);\n uint sl = (left ? left->size : 0);\n X res = Monoid_X::unit();\n if (l < sl) {\n X y = prod_rec(left, l, min(r, sl), rev ^ root->rev);\n res = Monoid_X::op(res, ActedMonoid::act(y, root->lazy, min(r, sl) - l));\n }\n if (l <= sl && sl < r) res = Monoid_X::op(res, root->x);\n uint k = 1 + sl;\n if (k < r) {\n X y = prod_rec(right, max(k, l) - k, r - k, rev ^ root->rev);\n res = Monoid_X::op(res, ActedMonoid::act(y, root->lazy, r - max(k, l)));\n }\n return res;\n }\n\n X get_rec(np root, uint k, bool rev, A lazy) {\n np left = (rev ? root->r : root->l);\n np right = (rev ? root->l : root->r);\n uint sl = (left ? left->size : 0);\n if (k == sl) return ActedMonoid::act(root->x, lazy, 1);\n lazy = Monoid_A::op(root->lazy, lazy);\n rev ^= root->rev;\n if (k < sl) return get_rec(left, k, rev, lazy);\n return get_rec(right, k - (1 + sl), rev, lazy);\n }\n\n np apply_rec(np root, uint l, uint r, const A &a) {\n prop(root);\n root = copy_node(root);\n if (l == 0 && r == root->size) {\n root->x = ActedMonoid::act(root->x, a, 1);\n root->prod = ActedMonoid::act(root->prod, a, root->size);\n root->lazy = a;\n return root;\n }\n uint sl = (root->l ? root->l->size : 0);\n if (l < sl) root->l = apply_rec(root->l, l, min(r, sl), a);\n if (l <= sl && sl < r) root->x = ActedMonoid::act(root->x, a, 1);\n uint k = 1 + sl;\n if (k < r) root->r = apply_rec(root->r, max(k, l) - k, r - k, a);\n update(root);\n return root;\n }\n\n template <typename F>\n pair<np, np> split_max_right_rec(np root, F check, X &x) {\n if (!root) return {nullptr, nullptr};\n prop(root);\n root = copy_node(root);\n X y = Monoid_X::op(x, root->prod);\n if (check(y)) {\n x = y;\n return {root, nullptr};\n }\n np left = root->l, right = root->r;\n if (left) {\n X y = Monoid_X::op(x, root->l->prod);\n if (!check(y)) {\n auto [n1, n2] = split_max_right_rec(left, check, x);\n root->l = n2;\n update(root);\n return {n1, root};\n }\n x = y;\n }\n y = Monoid_X::op(x, root->x);\n if (!check(y)) {\n root->l = nullptr;\n update(root);\n return {left, root};\n }\n x = y;\n auto [n1, n2] = split_max_right_rec(right, check, x);\n root->r = n1;\n update(root);\n return {root, n2};\n }\n};\n\ntemplate <typename E>\nstruct Monoid_Min {\n using X = E;\n using value_type = X;\n static constexpr X op(const X &x, const X &y) noexcept { return min(x, y); }\n static constexpr X unit() { return infty<E>; }\n static constexpr bool commute = true;\n};\n\ntemplate <typename X, int none_val>\nstruct Monoid_Assign {\n using value_type = X;\n static X op(X x, X y) { return (y == X(none_val) ? x : y); }\n static constexpr X unit() { return X(none_val); }\n static constexpr bool commute = false;\n};\n\ntemplate <typename E, E none_val>\nstruct ActedMonoid_Min_Assign {\n using Monoid_X = Monoid_Min<E>;\n using Monoid_A = Monoid_Assign<E, none_val>;\n using X = typename Monoid_X::value_type;\n using A = typename Monoid_A::value_type;\n static constexpr X act(const X &x, const A &a, const ll &size) {\n return (a == none_val ? x : a);\n }\n};\n\nint main() {\n\tint n, q;cin>>n>>q; \n\tRBST_ActedMonoid<ActedMonoid_Min_Assign<int,-1>> t(n) ;\n\tvi a(n) ;cin>>a;\n\tauto root=t.new_node(a) ;\n\twhile(q--){\n\t\tint op;cin>>op;\n\t\tint l, r;cin>>l>>r ;\n\t\tif(op == 0){\n\t\t\tauto[a,b,c,d]=t.split4(root, l,r,r+1) ;\n\t\t\troot=t.merge4(a,c,b,d) ;\n\t\t}\n\t\tif(op == 1){\n\t\t\tcout << t.prod(root, l, r+1) <<endl;\n\t\t}\n\t\tif(op == 2){\n\t\t\tt.set(root, l, r) ;\n\t\t}\n\t}\n\n\n\n\n\n\t\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 11668, "score_of_the_acc": -0.0174, "final_rank": 3 }, { "submission_id": "aoj_1508_10219580", "code_snippet": "// ### test.cpp ###\n#include <bits/stdc++.h>\n#ifdef __DEBUG_VECTOR\nnamespace for_debugging{\n struct subscript_and_location{\n int sub;\n std::source_location loc;\n subscript_and_location(int sub_,std::source_location loc_=std::source_location::current()){\n sub=sub_;\n loc=loc_;\n }\n void check_out_of_range(size_t sz){\n if(sub<0||(int)sz<=sub){\n std::clog << loc.file_name() << \":(\" << loc.line() << \":\" << loc.column() << \"):\" << loc.function_name() << std::endl;\n std::clog << \"out of range: subscript = \" << sub << \", vector_size = \" << sz << std::endl;\n exit(EXIT_FAILURE);\n }\n }\n };\n}\nnamespace std{\n template<class T,class Allocator=std::allocator<T>> class vector_for_debugging:public std::vector<T,Allocator>{\n using std::vector<T,Allocator>::vector;\n public:\n [[nodiscard]] constexpr std::vector<T,Allocator>::reference operator[](for_debugging::subscript_and_location n) noexcept(!std::is_same<T,bool>::value){\n n.check_out_of_range(this->size());\n return std::vector<T,Allocator>::operator[](n.sub);\n }\n [[nodiscard]] constexpr std::vector<T,Allocator>::const_reference operator[](for_debugging::subscript_and_location n) const noexcept(!std::is_same<T,bool>::value){\n n.check_out_of_range(this->size());\n return std::vector<T,Allocator>::operator[](n.sub);\n }\n };\n namespace pmr{\n template<class T> using vector_for_debugging=std::vector_for_debugging<T,std::pmr::polymorphic_allocator<T>>;\n }\n}\n#define vector vector_for_debugging\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing sll = __int128_t;\nusing db = double;\nusing Pr = pair<ll, ll>;\nusing Pd = pair<double, double>;\nusing vi = vector<int>;\nusing vs = vector<string>;\nusing vc = vector<char>;\nusing vl = vector<ll>;\nusing vb = vector<bool>;\nusing vd = vector<double>;\nusing vp = vector<Pr>;\nusing vpd = vector<Pd>;\nusing vvi = vector<vector<int>>;\nusing vvc = vector<vector<char>>;\nusing vvl = vector<vector<ll>>;\nusing vvp = vector<vector<Pr>>;\nusing vvb = vector<vector<bool>>;\nusing vvd = vector<vector<double>>;\nusing vvs = vector<vector<string>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vvvl = vector<vector<vector<ll>>>;\nusing vvvb = vector<vector<vector<bool>>>;\nusing vvvd = vector<vector<vector<double>>>;\nusing t3 = tuple<ll,ll,ll>;\nistream& operator>>(istream &is, t3 &t) { is>>get<0>(t)>>get<1>(t)>>get<2>(t); return is; }\nostream& operator<<(ostream& os, const t3& t) { os<<\"(\"<<get<0>(t)<<','<<get<1>(t)<<','<<get<2>(t)<<\")\"; return os; }\nusing t3d = tuple<db,db,db>;\nusing t4 = tuple<ll,ll,ll,ll>;\nistream& operator>>(istream &is, t4 &t) { is>>get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t); return is; }\nostream& operator<<(ostream& os, const t4& t) { os<<\"(\"<<get<0>(t)<<','<<get<1>(t)<<','<<get<2>(t)<<','<<get<3>(t)<<\")\"; return os; }\nusing vt3 = vector<t3>;\nusing vt3d = vector<t3d>;\nusing vt4 = vector<t4>;\nusing vvt3 = vector<vector<t3>>;\nusing vvt3d = vector<vector<t3d>>;\nusing vvt4 = vector<vector<t4>>;\nusing pq = priority_queue<Pr,vector<Pr>,greater<Pr>>;\nusing cl = complex<ll>;\nusing cd = complex<double>;\n#define rep(i, N) for (ll i=0; i<(ll)(N); ++i)\n#define repr(i, N) for (ll i = (ll)(N) - 1; i >= 0; --i)\n#define repk(i, k, N) for (ll i = k; i < (ll)(N); ++i)\n#define rep1(i, N) for (ll i=1; i<(ll)(N+1); ++i)\n#define rep1r(i, N) for (ll i=(ll)(N); i>0; i--)\n#define all(v) (v).begin(), (v).end()\n#define allr(v) (v).rbegin(), (v).rend()\n#define SIZE(v) (ll)((v).size())\n#define PYes {puts(\"Yes\"); exit(0);}\n#define PNo {puts(\"No\"); exit(0);}\n#define PFi {puts(\"First\"); exit(0);}\n#define PSe {puts(\"Second\"); exit(0);}\n#define Pm0 {puts(\"0\"); exit(0);}\n#define Pm1 {puts(\"-1\"); exit(0);}\n#define INT(...) int __VA_ARGS__; in(__VA_ARGS__)\n#define INTM(...) int __VA_ARGS__; inm(__VA_ARGS__)\n#define LONG(...) ll __VA_ARGS__; in(__VA_ARGS__)\n#define LONGM(...) ll __VA_ARGS__; inm(__VA_ARGS__)\n#define DOUBLE(...) double __VA_ARGS__; in(__VA_ARGS__)\n#define CHAR(...) char __VA_ARGS__; in(__VA_ARGS__)\n#define STRING(...) string __VA_ARGS__; in(__VA_ARGS__)\n#define VI(ivec, n) vi ivec(n); input_ivec(ivec, n)\n#define VIM(ivec, n) vi ivec(n); input_ivecm(ivec, n)\n#define VL(lvec, n) vl lvec(n); input_lvec(lvec, n)\n#define VLM(lvec, n) vl lvec(n); input_lvecm(lvec, n)\n#define VL2(lvec1, lvec2, n) vl lvec1(n), lvec2(n); input_lvec12(lvec1, lvec2, n)\n#define VL2M(lvec1, lvec2, n) vl lvec1(n), lvec2(n); input_lvec12m(lvec1, lvec2, n)\n#define VC(cvec, n) vc cvec(n); input_cvec(cvec, n)\n#define VS(svec, n) vs svec(n); input_svec(svec, n)\n#define VD(dvec, n) vd dvec(n); input_dvec(dvec, n)\n#define VP(pvec, n) vp pvec(n); input_pvec(pvec, n)\n#define VPD(pvec, n) vpd pvec(n); input_pvecd(pvec, n)\n#define VPM(pvec, n) vp pvec(n); input_pvecm(pvec, n)\n#define VVI(ivec2, h, w) vvi ivec2(h, vi(w)); input_ivec2(ivec2, h, w)\n#define VVL(lvec2, h, w) vvl lvec2(h, vl(w)); input_lvec2(lvec2, h, w)\n#define VVLM(lvec2, h, w) vvl lvec2(h, vl(w)); input_lvec2m(lvec2, h, w)\n#define VVC(cvec2, h, w) vvc cvec2(h, vc(w)); input_cvec2(cvec2, h, w)\n#define uset unordered_set\n#define umap unordered_map\ninline int pcnt(ll s, ll n=-1) { // n!=-1 for # of 0\n if(n==-1) return __builtin_popcountll(s);\n return n-__builtin_popcountll(s);\n}\ninline int parity(ll s, ll n=-1) { // n!=-1 for # of 0\n if(n==-1) return __builtin_parityll(s);\n return (n-__builtin_popcountll(s))%2;\n}\ninline void Out(double x) {printf(\"%.15f\",x);cout<<'\\n';}\ntemplate<typename T> inline void Out(pair<T,T> x) {cout<<x.first<<' '<<x.second<<'\\n';}\ntemplate<typename T> inline void Out(T x) {cout<<x<<'\\n';}\ninline void Out(vector<string> v) {rep(i,SIZE(v)) cout<<v[i]<<'\\n';}\ntemplate<typename T> inline void Out(queue<T> q){while(!q.empty()) {cout<<q.front()<<\" \"; q.pop();} cout<<endl;}\ntemplate<typename T> inline void Out(deque<T> q){while(!q.empty()) {cout<<q.front()<<\" \"; q.pop_front();} cout<<endl;}\ntemplate<typename T> inline void Out(vector<T> v) {rep(i,SIZE(v)) cout<<v[i]<<(i==SIZE(v)-1?'\\n':' ');}\ntemplate<typename T> inline void Out(vector<vector<T>> &vv){for(auto &v: vv) Out(v);}\ntemplate<typename T> inline void Out(vector<pair<T,T>> v) {for(auto p:v) Out(p);}\ntemplate<typename T> inline void Outend(T x) {Out(x); exit(0);}\ntemplate<typename T> inline void chmin(T &a, T b) { a = min(a, b); }\ntemplate<typename T> inline void chmax(T &a, T b) { a = max(a, b); }\ninline void mi(void) {return;}\ntemplate<typename T1, typename... T2> void mi(T1& f, T2&... r) {--f; mi(r...);}\ntemplate<class... T> void in(T&... x) {(cin >> ... >> x);}\ntemplate<class... T> void inm(T&... x) {(cin >> ... >> x); mi(x...);}\ninline void input_ivec(vi &ivec, int n) {rep(i, n) {cin>>ivec[i];}}\ninline void input_ivecm(vi &ivec, int n) {rep(i, n) {cin>>ivec[i];--ivec[i];}}\ninline void input_lvec(vl &lvec, ll n) {rep(i, n) {cin>>lvec[i];}}\ninline void input_lvecm(vl &lvec, ll n) {rep(i, n) {cin>>lvec[i];--lvec[i];}}\ninline void input_lvec12(vl &lvec1, vl &lvec2, ll n) {rep(i, n) {cin>>lvec1[i]>>lvec2[i];}}\ninline void input_lvec12m(vl &lvec1, vl &lvec2, ll n) {rep(i, n) {cin>>lvec1[i]>>lvec2[i];--lvec1[i];--lvec2[i];}}\ninline void input_cvec(vc &cvec, ll n) {rep (i, n) {cin>>cvec[i];}}\ninline void input_svec(vs &svec, ll n) {rep (i, n) {cin>>svec[i];}}\ninline void input_dvec(vd &dvec, ll n) {rep (i, n) {cin>>dvec[i];}}\ninline void input_pvec(vp &pvec, ll n) {rep (i, n) {cin>>pvec[i].first>>pvec[i].second;}}\ninline void input_pvecm(vp &pvec, ll n) {rep (i, n) {cin>>pvec[i].first>>pvec[i].second;pvec[i].first--,pvec[i].second--;}}\ninline void input_pvecd(vpd &pvec, ll n) {rep (i, n) {cin>>pvec[i].first>>pvec[i].second;}}\ninline void input_ivec2(vvi &ivec2, int h, int w) {rep(i, h) rep(j, w) {cin>>ivec2[i][j];}}\ninline void input_lvec2(vvl &lvec2, ll h, ll w) {rep(i, h) rep(j, w) {cin>>lvec2[i][j];}}\ninline void input_lvec2m(vvl &lvec2, ll h, ll w) {rep(i, h) rep(j, w) {cin>>lvec2[i][j];--lvec2[i][j];}}\ninline void input_cvec2(vvc &cvec2, ll h, ll w) {rep(i, h) rep(j, w) {cin>>cvec2[i][j];}}\ninline bool isin(ll i, ll j, ll h, ll w) {if(i<0||i>=h||j<0||j>=w) return false; else return true;}\ntemplate<typename T> inline T TmpPercent(T a, T b) {if(b<0){a=-a,b=-b;} return (a%b+b)%b;}\ntemplate<typename T> inline T Percent(T a, T b) {if(b<0) return -TmpPercent(a,b); return TmpPercent(a,b);}\ntemplate<typename T> inline T Div(T a, T b) {if(b<0){a=-a,b=-b;} return (a-TmpPercent(a,b))/b; }\ntemplate<typename T> inline T Divceil(T a, T b) {if(TmpPercent(a,b)==0) return Div(a,b); return Div(a,b)+1;}\ntemplate<typename T> void erase(multiset<T> &st, T x) {if(st.contains(x)) st.erase(st.find(x));}\ntemplate<typename T> T pop(vector<T> &x) {T ret=x.back(); x.pop_back(); return ret;}\n#ifdef __DEBUG\n#define de(var) {cerr << #var << \": \"; debug_view(var);}\n#define de2(var1,var2) {cerr<<#var1<<' '<<#var2<<\": \"; debug_view(var1,var2);}\n#define de3(var1,var2,var3) {cerr<<#var1<<' '<<#var2<<' '<<#var3<<\": \"; debug_view(var1,var2,var3);}\n#define de4(var1,var2,var3,var4) {cerr<<#var1<<' '<<#var2<<' '<<#var3<<' '<<#var4<<\": \"; debug_view(var1,var2,var3,var4);}\n#define de5(var1,var2,var3,var4,var5) {cerr<<#var1<<' '<<#var2<<' '<<#var3<<' '<<#var4<<' '<<#var5<<\": \"; debug_view(var1,var2,var3,var4,var5);}\ntemplate<typename T> inline void debug_view(T e){cerr << e << endl;}\ntemplate<typename T1, typename T2> inline void debug_view(T1 e1, T2 e2){cerr<<e1<<' '<<e2<<endl;}\ntemplate<typename T1, typename T2, typename T3> inline void debug_view(T1 e1, T2 e2, T3 e3){cerr<<e1<<' '<<e2<<' '<<e3<<endl;}\ntemplate<typename T1, typename T2, typename T3, typename T4> inline void debug_view(T1 e1, T2 e2, T3 e3, T4 e4){cerr<<e1<<' '<<e2<<' '<<e3<<' '<<e4<<endl;}\ntemplate<typename T1, typename T2, typename T3, typename T4, typename T5> inline void debug_view(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5){cerr<<e1<<' '<<e2<<' '<<e3<<' '<<e4<<' '<<e5<<endl;}\ntemplate<typename T1, typename T2> inline void debug_view(pair<T1,T2> &p){cerr<<\"{\"<<p.first<<\" \"<<p.second<<\"}\\n\";}\ntemplate<typename T1, typename T2> inline void debug_view(vector<pair<T1,T2>> &v){for(auto [a,b]: v){cerr<<\"{\"<<a<<\" \"<<b<<\"} \";} cerr << endl;}\ntemplate<typename T1, typename T2> inline void debug_view(set<pair<T1,T2>> &s){for(auto [a,b]: s){cerr<<\"{\"<<a<<\" \"<<b<<\"} \";} cerr << endl;}\ntemplate<typename T> inline void debug_view(tuple<T,T,T> t){cerr<<get<0>(t)<<' '<<get<1>(t)<<' '<<get<2>(t)<< endl;}\ntemplate<typename T> inline void debug_view(queue<T> q){while(!q.empty()) {cerr << q.front() << \" \"; q.pop();}cerr << endl;}\ntemplate<typename T> inline void debug_view(deque<T> q){while(!q.empty()) {cerr << q.front() << \" \"; q.pop_front();}cerr << endl;}\ntemplate<typename T> inline void debug_view(set<T> s){for(auto x:s){cerr << x << ' ';}cerr << endl;}\ntemplate<typename T> inline void debug_view(set<T,greater<T>> s){for(auto x:s){cerr << x << ' ';}cerr << endl;}\ntemplate<typename T> inline void debug_view(unordered_set<T> s){for(auto x:s){cerr << x << ' ';}cerr << endl;}\ntemplate<typename T> inline void debug_view(multiset<T> s){for(auto x:s){cerr << x << ' ';}cerr << endl;}\ntemplate<typename T> inline void debug_view(multiset<T,greater<T>> s){for(auto x:s){cerr << x << ' ';}cerr << endl;}\ntemplate<typename T> inline void debug_view(vector<pair<T,T>> &v){for(auto [a,b]: v){cerr<<\"{\"<<a<<\" \"<<b<<\"} \";} cerr << endl;}\ninline void debug_view(vector<string> &v){cerr << \"----\" << endl; for(auto s: v) debug_view(s);}\ntemplate<typename T> inline void debug_view(vector<T> &v){for(auto e: v){cerr << e << \" \";} cerr << endl;}\ntemplate<typename T> inline void debug_view(vector<vector<pair<T,T>>> &vv){cerr << \"----\" << endl;for(auto &v: vv){debug_view(v);} cerr << \"--------\" << endl;}\ntemplate<typename T> inline void debug_view(vector<vector<T>> &vv){cerr << \"----\" << endl;for(auto &v: vv){debug_view(v);} cerr << \"--------\" << endl;}\ntemplate<typename T1,typename T2> inline void debug_view(map<T1,T2> &mp){cerr << \"----\" << endl;for(auto [k,v]: mp){cerr << k << ' ' << v << endl;} cerr << \"--------\" << endl;}\ntemplate<typename T1,typename T2> inline void debug_view(unordered_map<T1,T2> &mp){cerr << \"----\" << endl;for(auto [k,v]: mp){cerr << k << ' ' << v << endl;} cerr << \"--------\" << endl;}\ntemplate<typename T1,typename T2> inline void debug_view(map<T1,vector<T2>> &mp){cerr<<\"----\"<<endl;for(auto [k,v]: mp){cerr<<k<<\": \";debug_view(v);} cerr << \"--------\" << endl;}\ntemplate<typename T1,typename T2,typename T3> inline void debug_view(map<pair<T1,T2>,T3> &mp){cerr << \"----\" << endl;for(auto [p,v]: mp){cerr<<'{'<<p.first<<' '<<p.second<<'}'<<\": \"<<v<<endl;} cerr<<\"--------\"<<endl;}\n#define deb(var) {cerr << #var << \": \"; debugb_view(var);}\ntemplate<typename T> inline void debugb_view(T e){bitset<20> b(e); cerr<<b<<endl;}\ntemplate<typename T> inline void debugb_view(vector<T> &v){cerr<<\"----\"<<endl;for(auto e: v){debugb_view(e);}}\n#else\n#define de(var) {}\n#define de2(var1,var2) {}\n#define de3(var1,var2,var3) {}\n#define de4(var1,var2,var3,var4) {}\n#define de5(var1,var2,var3,var4,var5) {}\n#define deb(var) {}\n#endif\nint IINF = 1001001001;\nll INF = 3e18;\nconst ll M998 = 998244353;\nconst ll M107 = 1000000007;\ntemplate<typename T> inline void ch1(T &x){if(x==INF)x=-1;}\nconst double PI = acos(-1);\nconst double EPS = 1e-8; //eg) if x=1e6, EPS >= 1e6/1e14(=1e-8)\nconst vi di = {0, 1, 0, -1};\nconst vi dj = {1, 0, -1, 0};\nconst vp dij = {{0,1},{1,0},{0,-1},{-1,0}};\nconst vp hex0 = {{-1,-1},{-1,0},{0,-1},{0,1},{1,-1},{1,0}}; // tobide\nconst vp hex1 = {{-1,0},{-1,1},{0,-1},{0,1},{1,0},{1,1}}; // hekomi\nconst vi di8 = {-1, -1, -1, 0, 0, 1, 1, 1};\nconst vi dj8 = {-1, 0, 1, -1, 1, -1, 0, 1};\nconst vp dij8 = {{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};\nPr operator+ (Pr a, Pr b) {return {a.first+b.first, a.second+b.second};}\nPr operator- (Pr a, Pr b) {return {a.first-b.first, a.second-b.second};}\nPr operator* (Pr a, Pr b) {return {a.first*b.first, a.second*b.second};}\nPr operator/ (Pr a, Pr b) {return {a.first/b.first, a.second/b.second};}\n\ntemplate<class T,T(*op)(T,T),T(*e)(),class F,T(*mapping)(F f,T x),F(*composition)(F f,F g),F(*id)()>\nclass ImplicitTreap{\n struct Node{\n T val,acc=e();\n int priority;\n int cnt=1;\n F lazy=id();\n bool rev=false;\n Node *l, *r;\n Node(T val,int priority):val(val),priority(priority),l(nullptr),r(nullptr){};\n }\n *root=nullptr;\n using Tree=Node *;\n\n int cnt(Tree t) { return t ? t->cnt : 0; }\n T acc(Tree t){ return t ? t->acc : e(); }\n void update(Tree t){\n if(t){\n t->cnt=1+cnt(t->l)+cnt(t->r);\n t->acc=op(t->val,op(acc(t->l),acc(t->r)));\n }\n }\n void pushdown(Tree t){\n if(t && t->rev){\n t->rev=false;\n std::swap(t->l,t->r);\n if(t->l)t->l->rev^=1;\n if(t->r)t->r->rev^=1;\n }\n if(t && t->lazy!=id()){\n if(t->l){\n t->l->lazy=composition(t->l->lazy,t->lazy);\n t->l->acc=mapping(t->lazy,t->l->acc);\n }\n if(t->r){\n t->r->lazy=composition(t->r->lazy,t->lazy);\n t->r->acc=mapping(t->lazy,t->r->acc);\n }\n t->val=mapping(t->lazy,t->val);\n t->lazy=id();\n }\n update(t);\n }\n void split(Tree t, int key, Tree& l,Tree& r){\n if(!t){\n l=r=nullptr;\n return;\n }\n pushdown(t);\n int implicit_key=cnt(t->l)+1;\n if(key<implicit_key){\n split(t->l,key,l,t->l),r=t;\n }else{\n split(t->r,key-implicit_key,t->r,r),l=t;\n }\n update(t);\n }\n void insert(Tree& t,int key,Tree item){\n Tree t1,t2;\n split(t,key,t1,t2);\n merge(t1,t1,item);\n merge(t,t1,t2);\n }\n void merge(Tree& t, Tree l, Tree r){\n pushdown(l);\n pushdown(r);\n if(!l || !r){\n t=l?l:r;\n }else if(l->priority>r->priority){\n merge(l->r,l->r,r),t=l;\n }else{\n merge(r->l,l,r->l),t=r;\n }\n update(t);\n }\n void erase(Tree& t,int key){\n Tree t1,t2,t3;\n split(t,key+1,t1,t2);\n split(t1,key,t1,t3);\n merge(t,t1,t2);\n }\n T prod(Tree t,int l,int r){\n Tree t1,t2,t3;\n split(t,l,t1,t2);\n split(t2,r-l,t2,t3);\n T ret=t2->acc;\n merge(t2,t2,t3);\n merge(t,t1,t2);\n return ret;\n }\n void apply(Tree t,int l,int r,F x){\n Tree t1,t2,t3;\n split(t,l,t1,t2);\n split(t2,r-l,t2,t3);\n t2->lazy=composition(t2->lazy,x);\n t2->acc=mapping(x,t2->acc);\n merge(t2,t2,t3);\n merge(t,t1,t2);\n }\n void reverse(Tree t,int l,int r){\n if(l>r)return;\n Tree t1,t2,t3;\n split(t,l,t1,t2);\n split(t2,r-l,t2,t3);\n t2->rev^=1;\n merge(t2,t2,t3);\n merge(t,t1,t2);\n }\n void rotate(Tree t,int l,int m,int r){\n reverse(t,l,r);\n reverse(t,l,l+r-m);\n reverse(t,l+r-m,r);\n }\n void dump(Tree t) {\n if (!t) return;\n pushdown(t);\n dump(t->l);\n std::cerr << t->val << \" \";\n dump(t->r);\n }\npublic:\n ImplicitTreap() {}\n ImplicitTreap(std::vector<T> as){\n std::reverse(as.begin(),as.end());\n for(T a:as){ insert(0,a); }\n }\n void insert(int pos,T val){\n //valをposの場所に追加する O(log N)\n insert(root,pos,new Node(val,rand()));\n }\n void erase(T pos){\n //posにある要素を消す O(log N)\n erase(root,pos);\n }\n int size(){ return cnt(root); }\n T operator[](int pos) {\n Tree t1, t2, t3;\n split(root, pos + 1, t1, t2);\n split(t1, pos, t1, t3);\n T ret = t3->acc;\n merge(t1, t1, t3);\n merge(root, t1, t2);\n return ret;\n }\n T prod(int l, int r){\n //[l,r)の区間 O(log N)\n return prod(root,l,r);\n }\n void apply(int l,int r,F x){ apply(root,l,r,x); }\n void reverse(int l,int r){ reverse(root,l,r); }\n void rotate(int l,int m,int r){ rotate(root,l,m,r); }\n void dump(){ \n #ifdef __DEBUG\n dump(root);std::cerr<<std::endl;\n #endif\n }\n};\n\nll op(ll a, ll b) {return min(a,b);}\nll e() {return INF;}\n\nvoid solve() {\n LONG(N, Q);\n VL(A, N);\n ImplicitTreap<ll,op,e,ll,op,op,e> tree(A);\n rep(i, Q) {\n LONG(t);\n if(t==0) {\n LONG(l, r);\n tree.rotate(l,r,r+1);\n } else if(t==1) {\n LONG(l, r);\n ll ans = tree.prod(l,r+1);\n Out(ans);\n } else {\n LONG(p,x);\n tree.erase(p);\n tree.insert(p,x);\n }\n // tree.dump();\n }\n\n}\n\nint main () {\n // ios::sync_with_stdio(false);\n cin.tie(nullptr);\n solve();\n}\n\n// ### test.cpp ###", "accuracy": 1, "time_ms": 570, "memory_kb": 21252, "score_of_the_acc": -0.4504, "final_rank": 18 }, { "submission_id": "aoj_1508_10209840", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#ifdef LOCAL\n#include <debug.hpp>\n#else\n#define debug(...)\n#endif\n\ntemplate <class S, S (*op)(S, S), S (*e)()>\nclass SplayTree {\n private:\n struct Node {\n S val, sum; // 自身の値と部分木全体の集約値\n int size; // 部分木のサイズ\n bool rev; // 遅延反転フラグ\n Node *lch, *rch, *par; // 左子、右子、親へのポインタ\n\n Node() : val(e()), sum(e()), size(0), rev(false), lch(nullptr), rch(nullptr), par(nullptr) {}\n Node(const S& v) : val(v), sum(v), size(1), rev(false), lch(nullptr), rch(nullptr), par(nullptr) {}\n };\n\n Node* root;\n\n inline int size(Node* t) const { return t ? t->size : 0; }\n inline S sum(Node* t) const { return t ? t->sum : e(); }\n\n // t のサイズ・集約値を左右の子から更新\n inline void push_up(Node* t) {\n if (!t) return;\n t->size = 1;\n t->sum = t->val;\n if (t->lch) {\n t->size += t->lch->size;\n t->sum = op(t->lch->sum, t->sum);\n }\n if (t->rch) {\n t->size += t->rch->size;\n t->sum = op(t->sum, t->rch->sum);\n }\n }\n\n // 反転フラグが立っていたら子に伝搬し、左右の子を入れ替える\n inline void push_down(Node* t) {\n if (t && t->rev) {\n swap(t->lch, t->rch);\n if (t->lch) t->lch->rev ^= true;\n if (t->rch) t->rch->rev ^= true;\n t->rev = false;\n }\n }\n\n // x とその親 p を回転する(x を上げる)\n void rotate(Node* x) {\n Node *p = x->par, *g = p->par;\n push_down(p);\n push_down(x);\n if (x == p->lch) {\n p->lch = x->rch;\n if (x->rch) x->rch->par = p;\n x->rch = p;\n } else {\n p->rch = x->lch;\n if (x->lch) x->lch->par = p;\n x->lch = p;\n }\n x->par = g;\n if (g) {\n if (p == g->lch)\n g->lch = x;\n else\n g->rch = x;\n }\n p->par = x;\n push_up(p);\n push_up(x);\n }\n\n // k 番目 (0 <= t < size) のノードを根に持ってくる\n Node* kth_element(Node* t, int k) {\n while (true) {\n push_down(t);\n int lsize = size(t->lch);\n if (k == lsize) break;\n if (k < lsize) {\n t = t->lch;\n } else if (k > lsize) {\n k -= lsize + 1;\n t = t->rch;\n }\n }\n splay(t);\n return t;\n }\n\n // x を根に持ってくる\n void splay(Node* x) {\n if (!x) return;\n while (x->par) {\n Node* p = x->par;\n Node* g = p->par;\n if (g) push_down(g);\n push_down(p);\n push_down(x);\n if (!g) {\n rotate(x);\n } else if ((g->lch == p && p->lch == x) || (g->rch == p && p->rch == x)) {\n rotate(p);\n rotate(x);\n } else {\n rotate(x);\n rotate(x);\n }\n }\n root = x;\n }\n\n // 木 t を先頭 k 個と残りに分割し、ペア {l, t} を返す\n pair<Node*, Node*> split(Node* t, int k) {\n if (!t) return {nullptr, nullptr};\n if (k == 0) return {nullptr, t};\n if (k >= size(t)) return {t, nullptr};\n // t を k 番目のノードが根になるよう splay する\n t = kth_element(t, k);\n Node* l = t->lch;\n l->par = nullptr;\n t->lch = nullptr;\n push_up(t);\n return {l, t};\n }\n\n // l, r を結合する\n Node* merge(Node* l, Node* r) {\n if (!l) return r;\n if (!r) return l;\n // l の最大要素(右端)を splay する\n l = kth_element(l, size(l) - 1);\n l->rch = r;\n r->par = l;\n push_up(l);\n return l;\n }\n\n Node* build(const vector<S>& V, int l, int r) {\n if (l >= r) return nullptr;\n int m = (l + r) >> 1;\n Node* t = new Node(V[m]);\n t->lch = build(V, l, m);\n t->rch = build(V, m + 1, r);\n if (t->lch) t->lch->par = t;\n if (t->rch) t->rch->par = t;\n push_up(t);\n return t;\n }\n\n void delete_all(Node* t) {\n if (!t) return;\n delete_all(t->lch);\n delete_all(t->rch);\n delete t;\n }\n\n public:\n SplayTree() : root(nullptr) {}\n SplayTree(const vector<S>& V) : root(nullptr) {\n if (!V.empty()) root = build(V, 0, V.size());\n }\n ~SplayTree() { delete_all(root); }\n\n // get: k (0 <= k < size) 番目の要素の値を返す\n S get(int k) {\n assert(root && 0 <= k && k < root->size);\n return kth_element(root, k)->val;\n }\n\n // insert: 位置 k に値 v を挿入する\n void insert(int k, S v) {\n assert(0 <= k && k <= size(root));\n Node* newNode = new Node(v);\n if (!root) {\n root = newNode;\n return;\n }\n if (k == 0) {\n root = merge(newNode, root);\n } else if (k == size(root)) {\n root = merge(root, newNode);\n } else {\n auto [L, R] = split(root, k);\n root = merge(merge(L, newNode), R);\n }\n }\n\n // erase: 位置 k の要素を削除する\n void erase(int k) {\n assert(root && 0 <= k && k < root->size);\n Node* target = kth_element(root, k);\n Node *L = target->lch, *R = target->rch;\n if (L) L->par = nullptr;\n if (R) R->par = nullptr;\n delete target;\n root = merge(L, R);\n }\n\n // update: 位置 k の値を v に更新する\n void update(int k, S v) {\n assert(root && 0 <= k && k < root->size);\n Node* target = kth_element(root, k);\n target->val = v;\n push_up(target);\n }\n\n // prod: 区間 [l, r) の要素を op で集約した値を返す\n S prod(int l, int r) {\n assert(0 <= l && r <= size());\n if (l >= r) return e();\n auto [A, BC] = split(root, l);\n auto [B, C] = split(BC, r - l);\n S res = B ? B->sum : e();\n root = merge(A, merge(B, C));\n return res;\n }\n\n // reverse: 区間 [l, r) の要素を反転する\n void reverse(int l, int r) {\n assert(0 <= l && r <= size());\n if (l >= r) return;\n auto [A, BC] = split(root, l);\n auto [B, C] = split(BC, r - l);\n if (B) B->rev ^= true;\n root = merge(A, merge(B, C));\n }\n\n // rotate: 区間 [l, r) を m 番目の要素が先頭になるように巡回シフトする\n // 0 <= l < m < r <= size()\n void rotate(int l, int m, int r) {\n assert(0 <= l && l < m && m < r && r <= size());\n auto [A, BC] = split(root, l);\n auto [B, C] = split(BC, r - l);\n auto [B1, B2] = split(B, m - l); // B = [B1, B2] で、B2 が先頭に来る\n B = merge(B2, B1);\n root = merge(A, merge(B, C));\n }\n\n bool empty() const { return root == nullptr; }\n int size() const { return size(root); }\n S operator[](int k) { return get(k); }\n};\n\nusing S = int;\nS op(S a, S b) { return min(a, b); }\nS e() { return 1e9; }\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n int N, Q;\n cin >> N >> Q;\n vector<int> A(N);\n for (int i = 0; i < N; i++) cin >> A[i];\n\n SplayTree<S, op, e> sp(A);\n while (Q--) {\n int x, y, z;\n cin >> x >> y >> z;\n if (x == 0) {\n // [l, r] を r が先頭になるように巡回シフト\n sp.rotate(y, z, z + 1);\n }\n if (x == 1) {\n cout << sp.prod(y, z + 1) << '\\n';\n }\n if (x == 2) {\n sp.update(y, z);\n }\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 13568, "score_of_the_acc": -0.0765, "final_rank": 6 }, { "submission_id": "aoj_1508_10098743", "code_snippet": "#line 2 \"template.hpp\"\n// #pragma GCC target(\"avx2\")\n// #pragma GCC optimize(\"O3\")\n// #pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\nusing namespace std;\n// https://xn--kst.jp/blog/2019/08/29/cpp-comp/\n// debug methods\n// usage: debug(x,y);\n// vector 出力できるように修正\ntemplate <typename T>\nostream& debug_print(ostream& os, const vector<T>& v) {\n os << \"[\";\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i < v.size() - 1) os << \", \";\n }\n os << \"]\";\n return os;\n}\ntemplate <typename T>\nostream& debug_print(ostream& os, const T& var) {\n os << var;\n return os;\n}\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x\n#define debug_1(x1) { cout << #x1 << \": \"; debug_print(cout, x1) << endl; }\n#define debug_2(x1, x2) { cout << #x1 << \": \"; debug_print(cout, x1) << \", \" << #x2 << \": \"; debug_print(cout, x2) << endl; }\n#define debug_3(x1, x2, x3) { cout << #x1 << \": \"; debug_print(cout, x1) << \", \" << #x2 << \": \"; debug_print(cout, x2) << \", \" << #x3 << \": \"; debug_print(cout, x3) << endl; }\n#define debug_4(x1, x2, x3, x4) { cout << #x1 << \": \"; debug_print(cout, x1) << \", \" << #x2 << \": \"; debug_print(cout, x2) << \", \" << #x3 << \": \"; debug_print(cout, x3) << \", \" << #x4 << \": \"; debug_print(cout, x4) << endl; }\n#define debug_5(x1, x2, x3, x4, x5) { cout << #x1 << \": \"; debug_print(cout, x1) << \", \" << #x2 << \": \"; debug_print(cout, x2) << \", \" << #x3 << \": \"; debug_print(cout, x3) << \", \" << #x4 << \": \"; debug_print(cout, x4) << \", \" << #x5 << \": \"; debug_print(cout, x5) << endl; }\n\n#ifdef LOCAL\n#define debug(...) CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\nusing ll = long long;\nusing vl = vector<ll>;\nusing Graph = vector<vector<ll>>;\nusing P = pair<ll, ll>;\n#define all(v) v.begin(), v.end()\ntemplate <typename T> inline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, true) : (false));\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, true) : (false));\n}\n#define rep1(i, n) for(ll i = 1; i <= ((ll)n); ++i)\n// https://trap.jp/post/1224/\ntemplate <class... T> constexpr auto min(T... a) {\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate <class... T> constexpr auto max(T... a) {\n return max(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate <class... T> void input(T &...a) { (cin >> ... >> a); }\ntemplate <class T> void input(vector<T> &a) {\n for(T &x : a)\n cin >> x;\n}\nvoid print() { cout << '\\n'; }\ntemplate <class T, class... Ts> void print(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\nvoid print(const string &s) {\n cout << s << '\\n';\n}\ntemplate <class Container, typename = void>\nstruct is_container : std::false_type {};\ntemplate <class Container>\nstruct is_container<Container, std::void_t<decltype(std::declval<Container>().begin()), decltype(std::declval<Container>().end())>> : std::true_type {};\ntemplate <class Container>\ntypename enable_if<is_container<Container>::value>::type print(const Container& x) {\n if (!x.empty()) {\n auto it = x.begin();\n for (; it != prev(x.end()); ++it) {\n cout << *it << \" \";\n }\n cout << *it << \"\\n\"; // 最後の要素を出力して改行\n }\n}\n#define INT(...) \\\n int __VA_ARGS__; \\\n input(__VA_ARGS__)\n#define LL(...) \\\n long long __VA_ARGS__; \\\n input(__VA_ARGS__)\n#define STR(...) \\\n string __VA_ARGS__; \\\n input(__VA_ARGS__)\n#define REP1(a) for(ll i = 0; i < a; i++)\n#define REP2(i, a) for(ll i = 0; i < a; i++)\n#define REP3(i, a, b) for(ll i = a; i < b; i++)\n#define REP4(i, a, b, c) for(ll i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define rep(...) overload4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n\nll inf = 3e18;\nvl dx = {1, -1, 0, 0};\nvl dy = {0, 0, 1, -1};\n#line 2 \"/home/y_midori/cp/test.cpp\"\n// #include \"data_structure/hash-map-variable-length.hpp\"\n// #include \"math/pollard_rho.hpp\"\n// #include <atcoder/modint>\n// using mint = atcoder::modint998244353;\n\n// std::random_device rd; // ハードウェア乱数生成器\n// std::mt19937 gen(rd()); // メルセンヌ・ツイスタの32ビット版\n// // 分布を指定\n// std::uniform_int_distribution<ll> dist(1, 1LL << 60);\nstruct SplayNode {\n SplayNode *left, *right, *parent;\n int size, value, minumum;\n\n SplayNode() {\n left = nullptr;\n right = nullptr;\n parent = nullptr;\n size = 1;\n }\n\n void rotate() {\n SplayNode *pp, *p, *c;\n p = this->parent;\n pp = p->parent;\n // 変更するのは3辺,6つ\n if(p->left == this) {\n c = this->right;\n this->right = p;\n p->left = c;\n } else {\n c = this->left;\n this->left = p;\n p->right = c;\n }\n if(pp) {\n if(pp->right == p) {\n pp->right = this;\n } else {\n pp->left = this;\n }\n }\n this->parent = pp;\n p->parent = this;\n if(c)\n c->parent = p;\n // p,thisのsizeを下から更新\n p->update();\n this->update();\n }\n int state() {\n if(!this->parent)\n return 0;\n if(this->parent->left == this)\n return 1;\n if(this->parent->right == this)\n return 2;\n return 0;\n }\n void splay() {\n while(this->parent) {\n if(this->parent->state() == 0) {\n this->rotate();\n } else if(this->state() == this->parent->state()) {\n this->parent->rotate();\n this->rotate();\n } else {\n this->rotate();\n this->rotate();\n }\n }\n }\n void update() {\n this->size = 1;\n this->minumum = this->value;\n if(this->left) {\n this->size += this->left->size;\n chmin(this->minumum, this->left->minumum);\n }\n if(this->right) {\n this->size += this->right->size;\n chmin(this->minumum, this->right->minumum);\n }\n }\n};\nSplayNode *get(int idx, SplayNode *root) {\n SplayNode *now = root;\n while(true) {\n int l_size = now->left ? now->left->size : 0;\n if(idx < l_size) {\n now = now->left;\n } else if(idx == l_size) {\n now->splay();\n return now;\n } else {\n idx -= l_size + 1;\n now = now->right;\n }\n }\n}\ntypedef SplayNode SN;\nSN *merge(SN *l_root, SN *r_root) {\n if(!l_root)\n return r_root;\n if(!r_root)\n return l_root;\n l_root = get(l_root->size - 1, l_root);\n l_root->right = r_root;\n r_root->parent = l_root;\n l_root->update();\n return l_root;\n}\npair<SN *, SN *> split(int l_size, SN *root) {\n if(l_size == 0)\n return {nullptr, root};\n if(l_size == root->size)\n return {root, nullptr};\n root = get(l_size, root);\n SN *l_root, *r_root;\n l_root = root->left;\n r_root = root;\n r_root->left = nullptr;\n l_root->parent = nullptr;\n r_root->update();\n return {l_root, r_root};\n}\nSN *insert(int idx, SN *node, SN *root) {\n // {0,1,2} --(idx=1,val=3)--> {0,3,1,2}\n SN *l_root, *r_root;\n tie(l_root, r_root) = split(idx, root);\n return merge(merge(l_root, node), r_root);\n}\npair<SN *, SN *> remove(int idx, SN *root) {\n // deleteは予約後\n // {残った木の根,消したnode}\n // {0,1,2} --(idx=1)--> {0,2}\n root = get(idx, root);\n SN *l_root = root->left, *r_root = root->right;\n if(l_root)\n l_root->parent = nullptr;\n if(r_root)\n r_root->parent = nullptr;\n root->left = nullptr;\n root->right = nullptr;\n root->update();\n return {merge(l_root, r_root), root};\n}\nSN *shift(ll l, ll r, SN *root) {\n // shift {a_l, ... ,a_{r-1}}\n SN *node;\n tie(root, node) = remove(r - 1, root);\n return insert(l, node, root);\n}\npair<SN *, int> prod(ll l, ll r, SN *root) {\n // prod {a_l, ... , a_{r-1}}\n SN *l_root, *c_root, *r_root;\n tie(root, r_root) = split(r, root);\n tie(l_root, c_root) = split(l, root);\n int res = c_root->minumum;\n return {merge(merge(l_root, c_root), r_root), res};\n}\nSplayNode node[210000];\nvoid solve() {\n LL(n, q);\n rep(i, n) {\n LL(ai);\n node[i].value = ai;\n if(i) {\n node[i].left = &node[i - 1];\n node[i - 1].parent = &node[i];\n }\n node[i].update();\n }\n SN *root = &node[n - 1];\n while(q--) {\n LL(x);\n if(x == 0) {\n LL(l, r);\n r++;\n root = shift(l, r, root);\n } else if(x == 1) {\n LL(l, r);\n r++;\n int ans;\n tie(root, ans) = prod(l, r, root);\n print(ans);\n } else {\n LL(idx, val);\n root = get(idx, root);\n root->value = val;\n root->update();\n }\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << std::setprecision(16);\n int t = 1;\n rep(_, t) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 11640, "score_of_the_acc": -0.0164, "final_rank": 2 }, { "submission_id": "aoj_1508_9776199", "code_snippet": "// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/1508\n#include <iostream>\n#include <algorithm>\n#include <cassert>\n#include <random>\n#include <tuple>\n#include <utility>\n/// @brief 動的配列\ntemplate <class M, class UniformRandomBitGenerator = std::mt19937>\nstruct dynamic_sequence {\n private:\n using T = typename M::value_type;\n using priority_type = typename UniformRandomBitGenerator::result_type;\n struct node_t {\n using pointer = node_t *;\n static int count(pointer t) { return !t ? 0 : t->cnt; }\n static T composition(pointer t) { return !t ? M::id() : t->acc; }\n node_t(const T &v, priority_type p)\n : val(v), acc(v), ch{nullptr, nullptr}, priority(p), cnt(1), rev() {}\n node_t(T &&v, priority_type p)\n : val(v), acc(v), ch{nullptr, nullptr}, priority(p), cnt(1), rev() {}\n T val, acc;\n pointer ch[2];\n priority_type priority;\n int cnt;\n bool rev;\n };\n using node_ptr = typename node_t::pointer;\n public:\n dynamic_sequence() : root(nullptr) {}\n dynamic_sequence(node_ptr p) : root(p) {}\n int size() const { return node_t::count(root); }\n T get(int k) const {\n assert(k < node_t::count(root));\n node_ptr node = root;\n while (true) {\n push(node);\n int c = node_t::count(node->ch[0]);\n if (c == k) break;\n if (k < c) node = node->ch[0];\n else node = node->ch[1], k -= c + 1;\n }\n return node->val;\n }\n void set(int k, T val) {\n assert(k < node_t::count(root));\n node_ptr node = root;\n std::vector<node_ptr> nodes;\n while (true) {\n push(node);\n nodes.emplace_back(node);\n int c = node_t::count(node->ch[0]);\n if (c == k) break;\n if (k < c) node = node->ch[0];\n else node = node->ch[1], k -= c + 1;\n }\n node->val = val;\n std::reverse(nodes.begin(), nodes.end());\n for (auto t : nodes) update(t);\n }\n void insert(int k, T val) { root = insert(root, k, val); }\n void push_front(const T &val) {\n node_ptr node = new node_t(val, gen());\n root = merge(node, root);\n }\n void push_front(T &&val) {\n node_ptr node = new node_t(std::move(val), gen());\n root = merge(node, root);\n }\n void push_back(const T &val) {\n node_ptr node = new node_t(val, gen());\n root = merge(root, node);\n }\n void push_back(T &&val) {\n node_ptr node = new node_t(std::move(val), gen());\n root = merge(root, node);\n }\n void erase(int k) { root = erase(root, k); }\n T prod(int r) const { return prod(root, r); }\n T prod(int l, int r) {\n assert(0 <= l && l <= r && r <= node_t::count(root));\n std::pair<node_ptr, node_ptr> p = split(root, l);\n T res = prod(p.second, r - l);\n root = merge(p.first, p.second);\n return res;\n }\n void reverse(int l, int r) {\n std::pair<node_ptr, node_ptr> sr = split(root, r);\n std::pair<node_ptr, node_ptr> sl = split(sr.first, l);\n if (sl.second) sl.second->rev ^= true;\n root = merge(sl.first, sl.second);\n root = merge(root, sr.second);\n }\n int lower_bound(T key) {\n node_ptr node = root;\n int res = 0;\n while (node) {\n int c = node_t::count(node->ch[0]);\n if (node->acc < key) node = node->ch[1], res += c + 1;\n else node = node->ch[0];\n }\n return res;\n }\n std::pair<dynamic_sequence, dynamic_sequence> split(int k) {\n auto [pl, pr] = split(root, k);\n return std::make_pair(dynamic_sequence(pl), dynamic_sequence(pr));\n }\n std::tuple<dynamic_sequence, dynamic_sequence, dynamic_sequence> split(int l, int r) {\n auto [pl, pr] = split(root, r);\n auto [ql, qr] = split(pl, l);\n return std::make_tuple(dynamic_sequence(ql), dynamic_sequence(qr), dynamic_sequence(pr));\n }\n void merge_left(dynamic_sequence lhs) { root = merge(lhs.root, root); }\n void merge_right(dynamic_sequence rhs) { root = merge(root, rhs.root); }\n private:\n static inline UniformRandomBitGenerator gen = UniformRandomBitGenerator();\n node_ptr root;\n void push(node_ptr t) {\n if (t && t->rev) {\n std::swap(t->ch[0], t->ch[1]);\n if (t->ch[0]) t->ch[0]->rev ^= true;\n if (t->ch[1]) t->ch[1]->rev ^= true;\n t->rev = false;\n }\n }\n node_ptr update(node_ptr t) {\n push(t);\n t->cnt = node_t::count(t->ch[0]) + node_t::count(t->ch[1]) + 1;\n t->acc = M::op(M::op(node_t::composition(t->ch[0]), t->val), node_t::composition(t->ch[1]));\n return t;\n }\n node_ptr merge(node_ptr l, node_ptr r) {\n if (!l || !r) return !l ? r : l;\n push(l);\n push(r);\n if (l->priority > r->priority) {\n l->ch[1] = merge(l->ch[1], r);\n return update(l);\n } else {\n r->ch[0] = merge(l, r->ch[0]);\n return update(r);\n }\n }\n std::pair<node_ptr, node_ptr> split(node_ptr t, int k) {\n if (!t) return std::make_pair<node_ptr, node_ptr>(nullptr, nullptr);\n push(t);\n if (k <= node_t::count(t->ch[0])) {\n auto s = split(t->ch[0], k);\n t->ch[0] = s.second;\n return std::make_pair(s.first, update(t));\n } else {\n auto s = split(t->ch[1], k - node_t::count(t->ch[0]) - 1);\n t->ch[1] = s.first;\n return std::make_pair(update(t), s.second);\n }\n }\n node_ptr insert(node_ptr t, int k, T v) {\n std::pair<node_ptr, node_ptr> s = split(t, k);\n node_ptr res = new node_t(v, gen());\n res = merge(s.first, res);\n res = merge(res, s.second);\n return res;\n }\n node_ptr erase(node_ptr t, int k) {\n int c = node_t::count(t->ch[0]);\n if (k == c) return merge(t->ch[0], t->ch[1]);\n if (k < c) {\n t->ch[0] = erase(t->ch[0], k);\n return update(t);\n } else {\n t->ch[1] = erase(t->ch[1], k - c - 1);\n return update(t);\n }\n }\n T prod(node_ptr t, int r) const {\n assert(0 <= r && r <= node_t::count(t));\n T res = M::id();\n while (r) {\n if (r == node_t::count(t)) {\n res = M::op(res, node_t::composition(t));\n break;\n }\n int c = node_t::count(t->ch[0]);\n if (r < c) {\n t = t->ch[0];\n continue;\n }\n res = M::op(res, node_t::composition(t->ch[0]));\n r -= c;\n if (r) res = M::op(res, t->val), --r;\n t = t->ch[1];\n }\n return res;\n }\n};\n#include <limits>\n#include <numeric>\ntemplate <class T>\nstruct Add {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs + rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs + rhs;\n }\n};\ntemplate <class T>\nstruct Mul {\n using value_type = T;\n static constexpr T id() { return T(1); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs * rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs * rhs;\n }\n};\ntemplate <class T>\nstruct And {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs & rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs & rhs;\n }\n};\ntemplate <class T>\nstruct Or {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs | rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs | rhs;\n }\n};\ntemplate <class T>\nstruct Xor {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs ^ rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs ^ rhs;\n }\n};\ntemplate <class T>\nstruct Min {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return std::min(lhs, rhs); }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return std::min((U)lhs, rhs);\n }\n};\ntemplate <class T>\nstruct Max {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::lowest(); }\n static constexpr T op(const T &lhs, const T &rhs) { return std::max(lhs, rhs); }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return std::max((U)lhs, rhs);\n }\n};\ntemplate <class T>\nstruct Gcd {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) {\n return lhs == Gcd::id() ? rhs : (rhs == Gcd::id() ? lhs : std::gcd(lhs, rhs));\n }\n};\ntemplate <class T>\nstruct Lcm {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) {\n return lhs == Lcm::id() ? rhs : (rhs == Lcm::id() ? lhs : std::lcm(lhs, rhs));\n }\n};\ntemplate <class T>\nstruct Update {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs == Update::id() ? rhs : lhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs == Update::id() ? rhs : lhs;\n }\n};\ntemplate <class T>\nstruct Affine {\n using P = std::pair<T, T>;\n using value_type = P;\n static constexpr P id() { return P(1, 0); }\n static constexpr P op(P lhs, P rhs) {\n return {lhs.first * rhs.first, lhs.first * rhs.second + lhs.second};\n }\n};\ntemplate <class M>\nstruct Rev {\n using T = typename M::value_type;\n using value_type = T;\n static constexpr T id() { return M::id(); }\n static constexpr T op(T lhs, T rhs) { return M::op(rhs, lhs); }\n};\nint main(void) {\n int n, q;\n std::cin >> n >> q;\n dynamic_sequence<Min<int>> ds;\n for (int i = 0; i < n; ++i) {\n int x;\n std::cin >> x;\n ds.push_back(x);\n }\n while (q--) {\n int t;\n std::cin >> t;\n if (t == 0) {\n int l, r;\n std::cin >> l >> r, ++r;\n if (l + 1 == r)\n continue;\n auto [lp, mp, rp] = ds.split(l, r);\n auto [lm, rm] = mp.split(r - l - 1);\n rm.merge_right(lm);\n lp.merge_right(rm);\n lp.merge_right(rp);\n ds = lp;\n } else if (t == 1) {\n int l, r;\n std::cin >> l >> r, ++r;\n std::cout << ds.prod(l, r) << '\\n';\n } else {\n int k, x;\n std::cin >> k >> x;\n ds.set(k, x);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 12824, "score_of_the_acc": -0.0692, "final_rank": 5 }, { "submission_id": "aoj_1508_9776113", "code_snippet": "// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/1508\n#include <iostream>\n#include <algorithm>\n#include <cassert>\n#include <random>\n#include <tuple>\n#include <utility>\n/// @brief 動的配列\ntemplate <class M, class UniformRandomBitGenerator = std::mt19937>\nstruct dynamic_sequence {\n private:\n using T = typename M::value_type;\n using priority_type = typename UniformRandomBitGenerator::result_type;\n struct node_t {\n using pointer = node_t *;\n static int count(pointer t) { return !t ? 0 : t->cnt; }\n static T composition(pointer t) { return !t ? M::id() : t->acc; }\n node_t(const T &v, priority_type p)\n : val(v), acc(v), ch{nullptr, nullptr}, priority(p), cnt(1), rev() {}\n node_t(T &&v, priority_type p)\n : val(v), acc(v), ch{nullptr, nullptr}, priority(p), cnt(1), rev() {}\n T val, acc;\n pointer ch[2];\n priority_type priority;\n int cnt;\n bool rev;\n };\n using node_ptr = typename node_t::pointer;\n public:\n dynamic_sequence() : root(nullptr) {}\n dynamic_sequence(node_ptr p) : root(p) {}\n int size() const { return node_t::count(root); }\n T get(int k) const {\n assert(k < node_t::count(root));\n node_ptr node = root;\n while (true) {\n push(node);\n int c = node_t::count(node->ch[0]);\n if (c == k) break;\n if (k < c) node = node->ch[0];\n else node = node->ch[1], k -= c + 1;\n }\n return node->val;\n }\n void set(int k, T val) {\n assert(k < node_t::count(root));\n node_ptr node = root;\n std::vector<node_ptr> nodes;\n while (true) {\n push(node);\n nodes.emplace_back(node);\n int c = node_t::count(node->ch[0]);\n if (c == k) break;\n if (k < c) node = node->ch[0];\n else node = node->ch[1], k -= c + 1;\n }\n node->val = val;\n std::reverse(nodes.begin(), nodes.end());\n for (auto t : nodes) update(t);\n }\n void insert(int k, T val) { root = insert(root, k, val); }\n void push_front(const T &val) {\n node_ptr node = new node_t(val, gen());\n root = merge(node, root);\n }\n void push_front(T &&val) {\n node_ptr node = new node_t(std::move(val), gen());\n root = merge(node, root);\n }\n void push_back(const T &val) {\n node_ptr node = new node_t(val, gen());\n root = merge(root, node);\n }\n void push_back(T &&val) {\n node_ptr node = new node_t(std::move(val), gen());\n root = merge(root, node);\n }\n void erase(int k) { root = erase(root, k); }\n T prod(int r) const { return prod(root, r); }\n T prod(int l, int r) {\n assert(0 <= l && l <= r && r <= node_t::count(root));\n std::pair<node_ptr, node_ptr> p = split(root, l);\n T res = prod(p.second, r - l);\n root = merge(p.first, p.second);\n return res;\n }\n void reverse(int l, int r) {\n std::pair<node_ptr, node_ptr> sr = split(root, r);\n std::pair<node_ptr, node_ptr> sl = split(sr.first, l);\n if (sl.second) sl.second->rev ^= true;\n root = merge(sl.first, sl.second);\n root = merge(root, sr.second);\n }\n int lower_bound(T key) {\n node_ptr node = root;\n int res = 0;\n while (node) {\n int c = node_t::count(node->ch[0]);\n if (node->acc < key) node = node->ch[1], res += c + 1;\n else node = node->ch[0];\n }\n return res;\n }\n std::pair<dynamic_sequence, dynamic_sequence> split(int k) {\n auto [pl, pr] = split(root, k);\n return std::make_pair(dynamic_sequence(pl), dynamic_sequence(pr));\n }\n std::tuple<dynamic_sequence, dynamic_sequence, dynamic_sequence> split(int l, int r) {\n auto [pl, pr] = split(root, r);\n auto [ql, qr] = split(pl, l);\n return std::make_tuple(dynamic_sequence(ql), dynamic_sequence(qr), dynamic_sequence(pr));\n }\n void merge_left(dynamic_sequence lhs) { root = merge(lhs.root, root); }\n void merge_right(dynamic_sequence rhs) { root = merge(root, rhs.root); }\n private:\n static inline UniformRandomBitGenerator gen = UniformRandomBitGenerator();\n node_ptr root;\n void push(node_ptr t) {\n if (t && t->rev) {\n std::swap(t->ch[0], t->ch[1]);\n if (t->ch[0]) t->ch[0]->rev ^= true;\n if (t->ch[1]) t->ch[1]->rev ^= true;\n t->rev = false;\n }\n }\n node_ptr update(node_ptr t) {\n push(t);\n t->cnt = node_t::count(t->ch[0]) + node_t::count(t->ch[1]) + 1;\n t->acc = M::op(M::op(node_t::composition(t->ch[0]), t->val), node_t::composition(t->ch[1]));\n return t;\n }\n node_ptr merge(node_ptr l, node_ptr r) {\n if (!l || !r) return !l ? r : l;\n push(l);\n push(r);\n if (l->priority > r->priority) {\n l->ch[1] = merge(l->ch[1], r);\n return update(l);\n } else {\n r->ch[0] = merge(l, r->ch[0]);\n return update(r);\n }\n }\n std::pair<node_ptr, node_ptr> split(node_ptr t, int k) {\n if (!t) return std::make_pair<node_ptr, node_ptr>(nullptr, nullptr);\n push(t);\n if (k <= node_t::count(t->ch[0])) {\n auto s = split(t->ch[0], k);\n t->ch[0] = s.second;\n return std::make_pair(s.first, update(t));\n } else {\n auto s = split(t->ch[1], k - node_t::count(t->ch[0]) - 1);\n t->ch[1] = s.first;\n return std::make_pair(update(t), s.second);\n }\n }\n node_ptr insert(node_ptr t, int k, T v) {\n std::pair<node_ptr, node_ptr> s = split(t, k);\n node_ptr res = new node_t(v, gen());\n res = merge(s.first, res);\n res = merge(res, s.second);\n return res;\n }\n node_ptr erase(node_ptr t, int k) {\n int c = node_t::count(t->ch[0]);\n if (k == c) return merge(t->ch[0], t->ch[1]);\n if (k < c) {\n t->ch[0] = erase(t->ch[0], k);\n return update(t);\n } else {\n t->ch[1] = erase(t->ch[1], k - c - 1);\n return update(t);\n }\n }\n T prod(node_ptr t, int r) const {\n assert(0 <= r && r <= node_t::count(t));\n T res = M::id();\n while (r) {\n if (r == node_t::count(t)) {\n res = M::op(res, node_t::composition(t));\n break;\n }\n int c = node_t::count(t->ch[0]);\n if (r < c) {\n t = t->ch[0];\n continue;\n }\n res = M::op(res, node_t::composition(t->ch[0]));\n r -= c;\n if (r) res = M::op(res, t->val), --r;\n t = t->ch[1];\n }\n return res;\n }\n};\n#include <limits>\n#include <numeric>\ntemplate <class T>\nstruct Add {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs + rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs + rhs;\n }\n};\ntemplate <class T>\nstruct Mul {\n using value_type = T;\n static constexpr T id() { return T(1); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs * rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs * rhs;\n }\n};\ntemplate <class T>\nstruct And {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs & rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs & rhs;\n }\n};\ntemplate <class T>\nstruct Or {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs | rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs | rhs;\n }\n};\ntemplate <class T>\nstruct Xor {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs ^ rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs ^ rhs;\n }\n};\ntemplate <class T>\nstruct Min {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return std::min(lhs, rhs); }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return std::min((U)lhs, rhs);\n }\n};\ntemplate <class T>\nstruct Max {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::lowest(); }\n static constexpr T op(const T &lhs, const T &rhs) { return std::max(lhs, rhs); }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return std::max((U)lhs, rhs);\n }\n};\ntemplate <class T>\nstruct Gcd {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) {\n return lhs == Gcd::id() ? rhs : (rhs == Gcd::id() ? lhs : std::gcd(lhs, rhs));\n }\n};\ntemplate <class T>\nstruct Lcm {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) {\n return lhs == Lcm::id() ? rhs : (rhs == Lcm::id() ? lhs : std::lcm(lhs, rhs));\n }\n};\ntemplate <class T>\nstruct Update {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs == Update::id() ? rhs : lhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs == Update::id() ? rhs : lhs;\n }\n};\ntemplate <class T>\nstruct Affine {\n using P = std::pair<T, T>;\n using value_type = P;\n static constexpr P id() { return P(1, 0); }\n static constexpr P op(P lhs, P rhs) {\n return {lhs.first * rhs.first, lhs.first * rhs.second + lhs.second};\n }\n};\ntemplate <class M>\nstruct Rev {\n using T = typename M::value_type;\n using value_type = T;\n static constexpr T id() { return M::id(); }\n static constexpr T op(T lhs, T rhs) { return M::op(rhs, lhs); }\n};\nint main(void) {\n int n, q;\n std::cin >> n >> q;\n std::vector<int> a(n);\n for (auto &e : a) std::cin >> e;\n dynamic_sequence<Min<int>> ds;\n for (auto e : a) ds.push_back(e);\n while (q--) {\n int t;\n std::cin >> t;\n if (t == 0) {\n int l, r;\n std::cin >> l >> r, ++r;\n if (l + 1 == r)\n continue;\n auto [lp, mp, rp] = ds.split(l, r);\n auto [lm, rm] = mp.split(r - l - 1);\n rm.merge_right(lm);\n lp.merge_right(rm);\n lp.merge_right(rp);\n ds = lp;\n } else if (t == 1) {\n int l, r;\n std::cin >> l >> r, ++r;\n std::cout << ds.prod(l, r) << '\\n';\n } else {\n int k, x;\n std::cin >> k >> x;\n ds.set(k, x);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 13392, "score_of_the_acc": -0.0898, "final_rank": 7 }, { "submission_id": "aoj_1508_9775830", "code_snippet": "// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/1508\n#include <iostream>\n#include <cassert>\n#include <random>\n#include <tuple>\n#include <utility>\n/// @brief 動的配列\ntemplate <class M, class UniformRandomBitGenerator = std::mt19937>\nstruct dynamic_sequence {\n private:\n using T = typename M::value_type;\n using priority_type = typename UniformRandomBitGenerator::result_type;\n struct node_t {\n using pointer = node_t *;\n static int count(pointer t) { return !t ? 0 : t->cnt; }\n static T composition(pointer t) { return !t ? M::id() : t->acc; }\n node_t(const T &v, priority_type p)\n : val(v), acc(v), ch{nullptr, nullptr}, priority(p), cnt(1), rev() {}\n node_t(T &&v, priority_type p)\n : val(v), acc(v), ch{nullptr, nullptr}, priority(p), cnt(1), rev() {}\n T val, acc;\n pointer ch[2];\n priority_type priority;\n int cnt;\n bool rev;\n };\n using node_ptr = typename node_t::pointer;\n public:\n dynamic_sequence() : root(nullptr) {}\n dynamic_sequence(node_ptr p) : root(p) {}\n int size() const { return node_t::count(root); }\n T get(int k) const {\n assert(k < node_t::count(root));\n node_ptr node = root;\n while (true) {\n int c = node_t::count(node->ch[0]);\n if (c == k) break;\n if (k < c) node = node->ch[0];\n else node = node->ch[1], k -= c + 1;\n }\n return node->val;\n }\n void insert(int k, T val) { root = insert(root, k, val); }\n void push_front(const T &val) {\n node_ptr node = new node_t(val, gen());\n root = merge(node, root);\n }\n void push_front(T &&val) {\n node_ptr node = new node_t(std::move(val), gen());\n root = merge(node, root);\n }\n void push_back(const T &val) {\n node_ptr node = new node_t(val, gen());\n root = merge(root, node);\n }\n void push_back(T &&val) {\n node_ptr node = new node_t(std::move(val), gen());\n root = merge(root, node);\n }\n void erase(int k) { root = erase(root, k); }\n T prod(int r) const { return prod(root, r); }\n T prod(int l, int r) {\n assert(0 <= l && l <= r && r <= node_t::count(root));\n std::pair<node_ptr, node_ptr> p = split(root, l);\n T res = prod(p.second, r - l);\n root = merge(p.first, p.second);\n return res;\n }\n void reverse(int l, int r) {\n std::pair<node_ptr, node_ptr> sr = split(root, r);\n std::pair<node_ptr, node_ptr> sl = split(sr.first, l);\n if (sl.second) sl.second->rev ^= true;\n root = merge(sl.first, sl.second);\n root = merge(root, sr.second);\n }\n int lower_bound(T key) {\n node_ptr node = root;\n int res = 0;\n while (node) {\n int c = node_t::count(node->ch[0]);\n if (node->acc < key) node = node->ch[1], res += c + 1;\n else node = node->ch[0];\n }\n return res;\n }\n std::pair<dynamic_sequence, dynamic_sequence> split(int k) {\n auto [pl, pr] = split(root, k);\n return std::make_pair(dynamic_sequence(pl), dynamic_sequence(pr));\n }\n std::tuple<dynamic_sequence, dynamic_sequence, dynamic_sequence> split(int l, int r) {\n auto [pl, pr] = split(root, r);\n auto [ql, qr] = split(pl, l);\n return std::make_tuple(dynamic_sequence(ql), dynamic_sequence(qr), dynamic_sequence(pr));\n }\n void merge_left(dynamic_sequence lhs) { root = merge(lhs.root, root); }\n void merge_right(dynamic_sequence rhs) { root = merge(root, rhs.root); }\n private:\n static inline UniformRandomBitGenerator gen = UniformRandomBitGenerator();\n node_ptr root;\n void push(node_ptr t) {\n if (t && t->rev) {\n std::swap(t->ch[0], t->ch[1]);\n if (t->ch[0]) t->ch[0]->rev ^= true;\n if (t->ch[1]) t->ch[1]->rev ^= true;\n t->rev = false;\n }\n }\n node_ptr update(node_ptr t) {\n push(t);\n t->cnt = node_t::count(t->ch[0]) + node_t::count(t->ch[1]) + 1;\n t->acc = M::op(M::op(node_t::composition(t->ch[0]), t->val), node_t::composition(t->ch[1]));\n return t;\n }\n node_ptr merge(node_ptr l, node_ptr r) {\n if (!l || !r) return !l ? r : l;\n push(l);\n push(r);\n if (l->priority > r->priority) {\n l->ch[1] = merge(l->ch[1], r);\n return update(l);\n } else {\n r->ch[0] = merge(l, r->ch[0]);\n return update(r);\n }\n }\n std::pair<node_ptr, node_ptr> split(node_ptr t, int k) {\n if (!t) return std::make_pair<node_ptr, node_ptr>(nullptr, nullptr);\n push(t);\n if (k <= node_t::count(t->ch[0])) {\n auto s = split(t->ch[0], k);\n t->ch[0] = s.second;\n return std::make_pair(s.first, update(t));\n } else {\n auto s = split(t->ch[1], k - node_t::count(t->ch[0]) - 1);\n t->ch[1] = s.first;\n return std::make_pair(update(t), s.second);\n }\n }\n node_ptr insert(node_ptr t, int k, T v) {\n std::pair<node_ptr, node_ptr> s = split(t, k);\n node_ptr res = new node_t(v, gen());\n res = merge(s.first, res);\n res = merge(res, s.second);\n return res;\n }\n node_ptr erase(node_ptr t, int k) {\n int c = node_t::count(t->ch[0]);\n if (k == c) return merge(t->ch[0], t->ch[1]);\n if (k < c) {\n t->ch[0] = erase(t->ch[0], k);\n return update(t);\n } else {\n t->ch[1] = erase(t->ch[1], k - c - 1);\n return update(t);\n }\n }\n T prod(node_ptr t, int r) const {\n assert(0 <= r && r <= node_t::count(t));\n T res = M::id();\n while (r) {\n if (r == node_t::count(t)) {\n res = M::op(res, node_t::composition(t));\n break;\n }\n int c = node_t::count(t->ch[0]);\n if (r < c) {\n t = t->ch[0];\n continue;\n }\n res = M::op(res, node_t::composition(t->ch[0]));\n r -= c;\n if (r) res = M::op(res, t->val), --r;\n t = t->ch[1];\n }\n return res;\n }\n};\n#include <algorithm>\n#include <limits>\n#include <numeric>\ntemplate <class T>\nstruct Add {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs + rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs + rhs;\n }\n};\ntemplate <class T>\nstruct Mul {\n using value_type = T;\n static constexpr T id() { return T(1); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs * rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs * rhs;\n }\n};\ntemplate <class T>\nstruct And {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs & rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs & rhs;\n }\n};\ntemplate <class T>\nstruct Or {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs | rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs | rhs;\n }\n};\ntemplate <class T>\nstruct Xor {\n using value_type = T;\n static constexpr T id() { return T(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs ^ rhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs ^ rhs;\n }\n};\ntemplate <class T>\nstruct Min {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return std::min(lhs, rhs); }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return std::min((U)lhs, rhs);\n }\n};\ntemplate <class T>\nstruct Max {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::lowest(); }\n static constexpr T op(const T &lhs, const T &rhs) { return std::max(lhs, rhs); }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return std::max((U)lhs, rhs);\n }\n};\ntemplate <class T>\nstruct Gcd {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) {\n return lhs == Gcd::id() ? rhs : (rhs == Gcd::id() ? lhs : std::gcd(lhs, rhs));\n }\n};\ntemplate <class T>\nstruct Lcm {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) {\n return lhs == Lcm::id() ? rhs : (rhs == Lcm::id() ? lhs : std::lcm(lhs, rhs));\n }\n};\ntemplate <class T>\nstruct Update {\n using value_type = T;\n static constexpr T id() { return std::numeric_limits<T>::max(); }\n static constexpr T op(const T &lhs, const T &rhs) { return lhs == Update::id() ? rhs : lhs; }\n template <class U>\n static constexpr U f(T lhs, U rhs) {\n return lhs == Update::id() ? rhs : lhs;\n }\n};\ntemplate <class T>\nstruct Affine {\n using P = std::pair<T, T>;\n using value_type = P;\n static constexpr P id() { return P(1, 0); }\n static constexpr P op(P lhs, P rhs) {\n return {lhs.first * rhs.first, lhs.first * rhs.second + lhs.second};\n }\n};\ntemplate <class M>\nstruct Rev {\n using T = typename M::value_type;\n using value_type = T;\n static constexpr T id() { return M::id(); }\n static constexpr T op(T lhs, T rhs) { return M::op(rhs, lhs); }\n};\nint main(void) {\n int n, q;\n std::cin >> n >> q;\n std::vector<int> a(n);\n for (auto &e : a) std::cin >> e;\n dynamic_sequence<Min<int>> ds;\n for (auto e : a) ds.push_back(e);\n while (q--) {\n int t;\n std::cin >> t;\n if (t == 0) {\n int l, r;\n std::cin >> l >> r, ++r;\n if (l + 1 == r)\n continue;\n auto [lp, mp, rp] = ds.split(l, r);\n auto [lm, rm] = mp.split(r - l - 1);\n rm.merge_right(lm);\n lp.merge_right(rm);\n lp.merge_right(rp);\n ds = lp;\n } else if (t == 1) {\n int l, r;\n std::cin >> l >> r, ++r;\n std::cout << ds.prod(l, r) << '\\n';\n } else {\n int k, x;\n std::cin >> k >> x;\n ds.erase(k);\n ds.insert(k, x);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 16292, "score_of_the_acc": -0.2048, "final_rank": 9 }, { "submission_id": "aoj_1508_9759911", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define itn int\n#define rep(i,n) for(long long i=0;i<(long long)n;i++)\n#define reps(i,n) for(long long i=1;i<=(long long)n;i++)\n#define repi(i,n) for(int i=0;i<(int)n;i++)\n#define loop(i,l,r) for(long long i=(long long)l;i<=(long long)r;i++)\n#define loopi(i,l,r) for(int i=(int)l;i<=(int)r;i++)\n#define drep(i,n) for(long long i=(long long)n-1;i>=0;i--)\n#define drepi(i,n) for(int i=(int)n-1;i>=0;i--)\n#define dreps(i,n) for(int i=(int)n;i>=1;i--)\n#define dloop(i,l,r) for(long long i=(long long)l;i>=(long long)r;i--)\n#define dloopi(i,l,r) for(int i=(int)l;i>=(int)r;i--)\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define yn(x) cout << (x? \"Yes\":\"No\") << endl;\n#define cou(x) cout << x << endl;\n#define emp emplace_back\n#pragma GCC target (\"avx,avx2\")//四則演算\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")//ループ\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")//浮動小数点\nconst long long mod=998244353LL;\nconst long long mods=1000000007LL;\nconst double inf=numeric_limits<double>::infinity();\nconst int kaz=1000000005;\nconst long long yab=2500000000000000000LL;\nconst long long aho =-yab;\nconst long double eps=1.0e-14L;\nconst long double pi=acosl(-1.0L);\nusing ll=long long;\nusing st=string;\nusing P=pair<ll,ll>;\nusing tup=tuple<ll,ll,ll>;\nusing vi=vector<ll>;\nusing vin=vector<int>;\nusing vc=vector<char>;\nusing vb=vector<bool>;\nusing vd=vector<double>;\nusing vs=vector<string>;\nusing vp=vector<P>;\nusing sp=set<P>;\nusing si=set<ll>;\nusing vvi=vector<vector<ll>>;\nusing vvin=vector<vin>;\nusing vvc=vector<vc>;\nusing vvb=vector<vb>;\nusing vvvi=vector<vvi>;\nusing vvvin=vector<vvin>;\nconst int dx[4]={0,1,0,-1};\nconst int dy[4]={1,0,-1,0};\nconst vector<int> ex = {-1, -1, -1, 0, 0, 1, 1, 1};\nconst vector<int> ey = {-1, 0, 1, -1, 1, -1, 0, 1};\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>v){rep(i,v.size())os<<v[i]<<(i+1!=v.size()?\" \":\"\\n\");return os;}\ntemplate<typename T1,typename T2>\nvoid co(bool x,T1 y,T2 z){\n if(x)cout << y << endl;\n else cout << z << endl;\n}\ntemplate<typename T>\nbool chmax(T &a, T b){\n\tif(a<b){\n\t\ta=b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <typename T, typename U>\nT ceil(T x, U y) {\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\n\ntemplate <typename T, typename U>\nT floor(T x, U y) {\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate<typename T>\nbool chmin(T &a, T b){\n\tif(a>b){\n\t\ta=b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate<typename T>\nvoid print(vector<T> &a){\n\tfor(int i=0;i<a.size();i++){\n\t\tcout << a[i];\n\t\tif(i==(long long)a.size()-1)cout << endl;\n\t\telse cout << \" \";\n\t}\n}\ntemplate<typename T>\nvoid her(vector<T> &a){\n for(auto &g:a)g--;\n}\nvector<long long> fac,finv,invs;\n// テーブルを作る前処理\nvoid COMinit(int MAX,int MOD) {\n\tfac.resize(MAX);\n\tfinv.resize(MAX);\n\tinvs.resize(MAX);\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n invs[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % MOD;\n invs[i] = MOD - invs[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * invs[i] % MOD;\n }\n}\n\n// 二項係数計算\nlong long COM(int n, int k,int MOD){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\nll mypow(ll x,ll y){\n\tll ret=1;\n\twhile(y>0){\n\t\tif(y&1)ret=ret*x%mod;\n\t\tx=x*x%mod;\n\t\ty>>=1;\n\t}\n\treturn ret;\n}\nll nopow(ll x,ll y){\n\tll ret=1;\n\twhile(y>0){\n\t\tif(y&1)ret*=x;\n\t\tx*=x;\n\t\ty>>=1;\n\t}\n\treturn ret;\n}\n//imprict treap\n// T0: 元の配列のモノイド\n// T1: T0に対する作用素モノイド\ntemplate <class T0, class T1>\nclass BaseImplicitTreap {\n // T0上の演算、単位元\n virtual T0 f0(T0, T0) = 0;\n const T0 u0;\n // T1上の演算、単位元\n virtual T1 f1(T1, T1) = 0;\n const T1 u1;\n // T0に対するT1の作用\n virtual T0 g(T0, T1) = 0;\n // 多数のt1(T1)に対するf1の合成\n virtual T1 p(T1, int) = 0;\n\n class xorshift {\n uint64_t x;\n\n public:\n xorshift() {\n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());\n x = rnd();\n for (int i = 0; i < 100; i++) {\n random();\n }\n }\n\n uint64_t random() {\n x = x ^ (x << 7);\n return x = x ^ (x >> 9);\n }\n } rnd;\n\n struct Node {\n T0 value, acc;\n T1 lazy;\n int priority, cnt;\n bool rev;\n Node *l, *r;\n\n Node(T0 value_, int priority_, T0 u0_, T1 u1_)\n : value(value_), acc(u0_), lazy(u1_), priority(priority_), cnt(1), rev(false), l(nullptr), r(nullptr) {}\n } *root = nullptr;\n\n using Tree = Node *;\n\n int cnt(Tree t) { return t ? t->cnt : 0; }\n\n T0 acc(Tree t) { return t ? t->acc : u0; }\n\n void update_cnt(Tree t) {\n if (t) {\n t->cnt = 1 + cnt(t->l) + cnt(t->r);\n }\n }\n\n void update_acc(Tree t) {\n if (t) {\n t->acc = f0(acc(t->l), f0(t->value, acc(t->r)));\n }\n }\n\n void pushup(Tree t) { update_cnt(t), update_acc(t); }\n\n void pushdown(Tree t) {\n if (t && t->rev) {\n t->rev = false;\n swap(t->l, t->r);\n if (t->l) t->l->rev ^= 1;\n if (t->r) t->r->rev ^= 1;\n }\n if (t && t->lazy != u1) {\n if (t->l) {\n t->l->lazy = f1(t->l->lazy, t->lazy);\n t->l->acc = g(t->l->acc, p(t->lazy, cnt(t->l)));\n }\n if (t->r) {\n t->r->lazy = f1(t->r->lazy, t->lazy);\n t->r->acc = g(t->r->acc, p(t->lazy, cnt(t->r)));\n }\n t->value = g(t->value, p(t->lazy, 1));\n t->lazy = u1;\n }\n pushup(t);\n }\n\n void split(Tree t, int key, Tree &l, Tree &r) {\n if (!t) {\n l = r = nullptr;\n return;\n }\n pushdown(t);\n int implicit_key = cnt(t->l) + 1;\n if (key < implicit_key) {\n split(t->l, key, l, t->l), r = t;\n } else {\n split(t->r, key - implicit_key, t->r, r), l = t;\n }\n pushup(t);\n }\n\n void insert(Tree &t, int key, Tree item) {\n Tree t1, t2;\n split(t, key, t1, t2);\n merge(t1, t1, item);\n merge(t, t1, t2);\n }\n\n void merge(Tree &t, Tree l, Tree r) {\n pushdown(l);\n pushdown(r);\n if (!l || !r) {\n t = l ? l : r;\n } else if (l->priority > r->priority) {\n merge(l->r, l->r, r), t = l;\n } else {\n merge(r->l, l, r->l), t = r;\n }\n pushup(t);\n }\n\n void erase(Tree &t, int key) {\n Tree t1, t2, t3;\n split(t, key + 1, t1, t2);\n split(t1, key, t1, t3);\n merge(t, t1, t2);\n }\n\n void update(Tree t, int l, int r, T1 x) {\n if (l >= r) return;\n Tree t1, t2, t3;\n split(t, l, t1, t2);\n split(t2, r - l, t2, t3);\n t2->lazy = f1(t2->lazy, x);\n t2->acc = g(t2->acc, p(x, cnt(t2)));\n merge(t2, t2, t3);\n merge(t, t1, t2);\n }\n\n T0 query(Tree t, int l, int r) {\n if (l == r) return u0;\n Tree t1, t2, t3;\n split(t, l, t1, t2);\n split(t2, r - l, t2, t3);\n T0 ret = t2->acc;\n merge(t2, t2, t3);\n merge(t, t1, t2);\n return ret;\n }\n\n // [l, r)の中で左から何番目か\n int find(Tree t, T0 x, int offset, bool left = true) {\n if (f0(t->acc, x) == x) {\n return -1;\n } else {\n if (left) {\n if (t->l && f0(t->l->acc, x) != x) {\n return find(t->l, x, offset, left);\n } else {\n return (f0(t->value, x) != x) ? offset + cnt(t->l) : find(t->r, x, offset + cnt(t->l) + 1, left);\n }\n } else {\n if (t->r && f0(t->r->acc, x) != x) {\n return find(t->r, x, offset + cnt(t->l) + 1, left);\n } else {\n return (f0(t->value, x) != x) ? offset + cnt(t->l) : find(t->l, x, offset, left);\n }\n }\n }\n }\n\n void reverse(Tree t, int l, int r) {\n if (l > r) return;\n Tree t1, t2, t3;\n split(t, l, t1, t2);\n split(t2, r - l, t2, t3);\n t2->rev ^= 1;\n merge(t2, t2, t3);\n merge(t, t1, t2);\n }\n\n // [l, r)の先頭がmになるようにシフトさせる。std::rotateと同じ仕様\n void rotate(Tree t, int l, int m, int r) {\n reverse(t, l, r);\n reverse(t, l, l + r - m);\n reverse(t, l + r - m, r);\n }\n\n void dump(Tree t) {\n if (!t) return;\n pushdown(t);\n dump(t->l);\n cout << t->value << \" \";\n dump(t->r);\n }\n\npublic:\n BaseImplicitTreap(T0 u0_, T1 u1_) : u0(u0_), u1(u1_) {}\n\n void set_by_vector(const vector<T0> &a) {\n for (int i = 0; i < a.size(); i++) {\n insert(i, a[i]);\n }\n }\n\n int size() { return cnt(root); }\n\n void insert(int pos, T0 x) { insert(root, pos, new Node(x, rnd.random(), u0, u1)); }\n\n void update(int l, int r, T1 x) { update(root, l, r, x); }\n\n T0 query(int l, int r) { return query(root, l, r); }\n\n // 二分探索。[l, r)内のkでf0(tr[k], x) != xとなる最左/最右のもの。存在しない場合は-1\n // たとえばMinMonoidの場合、x未満の最左/最右の要素の位置を返す\n int binary_search(int l, int r, T0 x, bool left = true) {\n if (l >= r) return -1;\n Tree t1, t2, t3;\n split(root, l, t1, t2);\n split(t2, r - l, t2, t3);\n int ret = find(t2, x, l, left);\n merge(t2, t2, t3);\n merge(root, t1, t2);\n return ret;\n }\n\n void erase(int pos) { erase(root, pos); }\n\n void reverse(int l, int r) { reverse(root, l, r); }\n\n void rotate(int l, int m, int r) { rotate(root, l, m, r); }\n\n void dump() {\n dump(root);\n cout << endl;\n }\n\n T0 operator[](int pos) { return query(pos, pos + 1); }\n};\n\ntemplate <class T0, class T1>\nstruct MinUpdateQuery : public BaseImplicitTreap<T0, T1> {\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n MinUpdateQuery() : MinUpdateQuery(numeric_limits<T0>::max(), numeric_limits<T1>::min()) {}\n T0 f0(T0 x, T0 y) override { return min(x, y); }\n T1 f1(T1 x, T1 y) override { return y == numeric_limits<T1>::min() ? x : y; }\n T0 g(T0 x, T1 y) override { return y == numeric_limits<T1>::min() ? x : y; }\n T1 p(T1 x, int len) override { return x; }\n};\n\ntemplate <class T0, class T1>\nstruct SumAddQuery : public BaseImplicitTreap<T0, T1> {\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n SumAddQuery() : SumAddQuery(0, 0) {}\n T0 f0(T0 x, T0 y) override { return x + y; }\n T1 f1(T1 x, T1 y) override { return x + y; }\n T0 g(T0 x, T1 y) override { return x + y; }\n T1 p(T1 x, int len) override { return x * len; }\n};\n\ntemplate <class T0, class T1>\nstruct MinAddQuery : public BaseImplicitTreap<T0, T1> {\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n MinAddQuery() : MinAddQuery(numeric_limits<T0>::max(), 0) {}\n T0 f0(T0 x, T0 y) override { return min(x, y); }\n T1 f1(T1 x, T1 y) override { return x + y; }\n T0 g(T0 x, T1 y) override { return x + y; }\n T1 p(T1 x, int len) override { return x; }\n};\n\ntemplate <class T0, class T1>\nstruct SumUpdateQuery : public BaseImplicitTreap<T0, T1> {\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n SumUpdateQuery() : SumUpdateQuery(0, numeric_limits<T1>::min()) {}\n T0 f0(T0 x, T0 y) override { return x + y; }\n T1 f1(T1 x, T1 y) override { return y == numeric_limits<T1>::min() ? x : y; }\n T0 g(T0 x, T1 y) override { return y == numeric_limits<T1>::min() ? x : y; }\n T1 p(T1 x, int len) override { return x == numeric_limits<T1>::min() ? numeric_limits<T1>::min() : x * len; }\n};\n\ntemplate <class T0>\nstruct SumAffineQuery : public BaseImplicitTreap<T0, pair<T0, T0>> {\n using T1 = pair<T0, T0>; // first * x + second\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n SumAffineQuery() : SumAffineQuery(0, {1, 0}) {}\n T0 f0(T0 x, T0 y) override { return x + y; }\n T1 f1(T1 x, T1 y) override { return {x.first * y.first, x.second * y.first + y.second}; }\n T0 g(T0 x, T1 y) override { return y.first * x + y.second; }\n T1 p(T1 x, int len) override { return {x.first, x.second * len}; }\n // update(i, j, {a, b}); // [i, j)にax + bを作用\n // update(i, j, {0, a}); // update\n // update(i, j, {1, a}); // 加算\n // update(i, j, {a, 0}); // 倍\n};\n\ntemplate <class T>\nstruct MinmaxAffineQuery : public BaseImplicitTreap<pair<T, T>, pair<T, T>> {\n using T0 = pair<T, T>; // {min, max}\n using T1 = pair<T, T>; // first * x + second\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n MinmaxAffineQuery()\n : MinmaxAffineQuery({numeric_limits<T>::max(), -numeric_limits<T>::max()}, {1, 0}) {\n } // TODO: _u1を使うとコンパイル通らない原因不明\n T0 f0(T0 x, T0 y) override { return {min(x.first, y.first), max(x.second, y.second)}; }\n T1 f1(T1 x, T1 y) override { return {x.first * y.first, x.second * y.first + y.second}; }\n T0 g(T0 x, T1 y) override {\n T0 ret = {x.first * y.first + y.second, x.second * y.first + y.second};\n if (y.first < 0) swap(ret.first, ret.second);\n return ret;\n }\n T1 p(T1 x, int len) override { return x; }\n // update(i, j, {a, b}); // [i, j)にax + bを作用\n // update(i, j, {0, a}); // update\n // update(i, j, {1, a}); // 加算\n // update(i, j, {a, 0}); // 倍\n};\nint main(){\n\tcin.tie(nullptr);\n\tint n,q;\n cin >> n >> q;\n vi a(n);\n cin >> a;\n MinAddQuery<ll,ll> s;\n s.set_by_vector(a);\n while(q--){\n ll t,x,y;\n cin >> t >> x >> y;\n if(t==0){\n s.rotate(x,y,y+1);\n }\n if(t==1){\n cout << s.query(x,y+1) << endl;\n }\n if(t==2){\n s.update(x,x+1,y-s[x]);\n }\n }\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 17536, "score_of_the_acc": -0.286, "final_rank": 15 }, { "submission_id": "aoj_1508_9759891", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define itn int\n#define rep(i,n) for(long long i=0;i<(long long)n;i++)\n#define reps(i,n) for(long long i=1;i<=(long long)n;i++)\n#define repi(i,n) for(int i=0;i<(int)n;i++)\n#define loop(i,l,r) for(long long i=(long long)l;i<=(long long)r;i++)\n#define loopi(i,l,r) for(int i=(int)l;i<=(int)r;i++)\n#define drep(i,n) for(long long i=(long long)n-1;i>=0;i--)\n#define drepi(i,n) for(int i=(int)n-1;i>=0;i--)\n#define dreps(i,n) for(int i=(int)n;i>=1;i--)\n#define dloop(i,l,r) for(long long i=(long long)l;i>=(long long)r;i--)\n#define dloopi(i,l,r) for(int i=(int)l;i>=(int)r;i--)\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define yn(x) cout << (x? \"Yes\":\"No\") << endl;\n#define cou(x) cout << x << endl;\n#define emp emplace_back\n#pragma GCC target (\"avx,avx2\")//四則演算\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")//ループ\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")//浮動小数点\nconst long long mod=998244353LL;\nconst long long mods=1000000007LL;\nconst double inf=numeric_limits<double>::infinity();\nconst int kaz=1000000005;\nconst long long yab=2500000000000000000LL;\nconst long long aho =-yab;\nconst long double eps=1.0e-14L;\nconst long double pi=acosl(-1.0L);\nusing ll=long long;\nusing st=string;\nusing P=pair<ll,ll>;\nusing tup=tuple<ll,ll,ll>;\nusing vi=vector<ll>;\nusing vin=vector<int>;\nusing vc=vector<char>;\nusing vb=vector<bool>;\nusing vd=vector<double>;\nusing vs=vector<string>;\nusing vp=vector<P>;\nusing sp=set<P>;\nusing si=set<ll>;\nusing vvi=vector<vector<ll>>;\nusing vvin=vector<vin>;\nusing vvc=vector<vc>;\nusing vvb=vector<vb>;\nusing vvvi=vector<vvi>;\nusing vvvin=vector<vvin>;\nconst int dx[4]={0,1,0,-1};\nconst int dy[4]={1,0,-1,0};\nconst vector<int> ex = {-1, -1, -1, 0, 0, 1, 1, 1};\nconst vector<int> ey = {-1, 0, 1, -1, 1, -1, 0, 1};\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>v){rep(i,v.size())os<<v[i]<<(i+1!=v.size()?\" \":\"\\n\");return os;}\ntemplate<typename T1,typename T2>\nvoid co(bool x,T1 y,T2 z){\n if(x)cout << y << endl;\n else cout << z << endl;\n}\ntemplate<typename T>\nbool chmax(T &a, T b){\n\tif(a<b){\n\t\ta=b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <typename T, typename U>\nT ceil(T x, U y) {\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\n\ntemplate <typename T, typename U>\nT floor(T x, U y) {\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate<typename T>\nbool chmin(T &a, T b){\n\tif(a>b){\n\t\ta=b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate<typename T>\nvoid print(vector<T> &a){\n\tfor(int i=0;i<a.size();i++){\n\t\tcout << a[i];\n\t\tif(i==(long long)a.size()-1)cout << endl;\n\t\telse cout << \" \";\n\t}\n}\ntemplate<typename T>\nvoid her(vector<T> &a){\n for(auto &g:a)g--;\n}\nvector<long long> fac,finv,invs;\n// テーブルを作る前処理\nvoid COMinit(int MAX,int MOD) {\n\tfac.resize(MAX);\n\tfinv.resize(MAX);\n\tinvs.resize(MAX);\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n invs[1] = 1;\n for (int i = 2; i < MAX; i++){\n fac[i] = fac[i - 1] * i % MOD;\n invs[i] = MOD - invs[MOD%i] * (MOD / i) % MOD;\n finv[i] = finv[i - 1] * invs[i] % MOD;\n }\n}\n\n// 二項係数計算\nlong long COM(int n, int k,int MOD){\n if (n < k) return 0;\n if (n < 0 || k < 0) return 0;\n return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;\n}\nll mypow(ll x,ll y){\n\tll ret=1;\n\twhile(y>0){\n\t\tif(y&1)ret=ret*x%mod;\n\t\tx=x*x%mod;\n\t\ty>>=1;\n\t}\n\treturn ret;\n}\nll nopow(ll x,ll y){\n\tll ret=1;\n\twhile(y>0){\n\t\tif(y&1)ret*=x;\n\t\tx*=x;\n\t\ty>>=1;\n\t}\n\treturn ret;\n}\n//imprict treap\n// T0: 元の配列のモノイド\n// T1: T0に対する作用素モノイド\ntemplate <class T0, class T1>\nclass BaseImplicitTreap {\n // T0上の演算、単位元\n virtual T0 f0(T0, T0) = 0;\n const T0 u0;\n // T1上の演算、単位元\n virtual T1 f1(T1, T1) = 0;\n const T1 u1;\n // T0に対するT1の作用\n virtual T0 g(T0, T1) = 0;\n // 多数のt1(T1)に対するf1の合成\n virtual T1 p(T1, int) = 0;\n\n class xorshift {\n uint64_t x;\n\n public:\n xorshift() {\n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());\n x = rnd();\n for (int i = 0; i < 100; i++) {\n random();\n }\n }\n\n uint64_t random() {\n x = x ^ (x << 7);\n return x = x ^ (x >> 9);\n }\n } rnd;\n\n struct Node {\n T0 value, acc;\n T1 lazy;\n int priority, cnt;\n bool rev;\n Node *l, *r;\n\n Node(T0 value_, int priority_, T0 u0_, T1 u1_)\n : value(value_), acc(u0_), lazy(u1_), priority(priority_), cnt(1), rev(false), l(nullptr), r(nullptr) {}\n } *root = nullptr;\n\n using Tree = Node *;\n\n int cnt(Tree t) { return t ? t->cnt : 0; }\n\n T0 acc(Tree t) { return t ? t->acc : u0; }\n\n void update_cnt(Tree t) {\n if (t) {\n t->cnt = 1 + cnt(t->l) + cnt(t->r);\n }\n }\n\n void update_acc(Tree t) {\n if (t) {\n t->acc = f0(acc(t->l), f0(t->value, acc(t->r)));\n }\n }\n\n void pushup(Tree t) { update_cnt(t), update_acc(t); }\n\n void pushdown(Tree t) {\n if (t && t->rev) {\n t->rev = false;\n swap(t->l, t->r);\n if (t->l) t->l->rev ^= 1;\n if (t->r) t->r->rev ^= 1;\n }\n if (t && t->lazy != u1) {\n if (t->l) {\n t->l->lazy = f1(t->l->lazy, t->lazy);\n t->l->acc = g(t->l->acc, p(t->lazy, cnt(t->l)));\n }\n if (t->r) {\n t->r->lazy = f1(t->r->lazy, t->lazy);\n t->r->acc = g(t->r->acc, p(t->lazy, cnt(t->r)));\n }\n t->value = g(t->value, p(t->lazy, 1));\n t->lazy = u1;\n }\n pushup(t);\n }\n\n void split(Tree t, int key, Tree &l, Tree &r) {\n if (!t) {\n l = r = nullptr;\n return;\n }\n pushdown(t);\n int implicit_key = cnt(t->l) + 1;\n if (key < implicit_key) {\n split(t->l, key, l, t->l), r = t;\n } else {\n split(t->r, key - implicit_key, t->r, r), l = t;\n }\n pushup(t);\n }\n\n void insert(Tree &t, int key, Tree item) {\n Tree t1, t2;\n split(t, key, t1, t2);\n merge(t1, t1, item);\n merge(t, t1, t2);\n }\n\n void merge(Tree &t, Tree l, Tree r) {\n pushdown(l);\n pushdown(r);\n if (!l || !r) {\n t = l ? l : r;\n } else if (l->priority > r->priority) {\n merge(l->r, l->r, r), t = l;\n } else {\n merge(r->l, l, r->l), t = r;\n }\n pushup(t);\n }\n\n void erase(Tree &t, int key) {\n Tree t1, t2, t3;\n split(t, key + 1, t1, t2);\n split(t1, key, t1, t3);\n merge(t, t1, t2);\n }\n\n void update(Tree t, int l, int r, T1 x) {\n if (l >= r) return;\n Tree t1, t2, t3;\n split(t, l, t1, t2);\n split(t2, r - l, t2, t3);\n t2->lazy = f1(t2->lazy, x);\n t2->acc = g(t2->acc, p(x, cnt(t2)));\n merge(t2, t2, t3);\n merge(t, t1, t2);\n }\n\n T0 query(Tree t, int l, int r) {\n if (l == r) return u0;\n Tree t1, t2, t3;\n split(t, l, t1, t2);\n split(t2, r - l, t2, t3);\n T0 ret = t2->acc;\n merge(t2, t2, t3);\n merge(t, t1, t2);\n return ret;\n }\n\n // [l, r)の中で左から何番目か\n int find(Tree t, T0 x, int offset, bool left = true) {\n if (f0(t->acc, x) == x) {\n return -1;\n } else {\n if (left) {\n if (t->l && f0(t->l->acc, x) != x) {\n return find(t->l, x, offset, left);\n } else {\n return (f0(t->value, x) != x) ? offset + cnt(t->l) : find(t->r, x, offset + cnt(t->l) + 1, left);\n }\n } else {\n if (t->r && f0(t->r->acc, x) != x) {\n return find(t->r, x, offset + cnt(t->l) + 1, left);\n } else {\n return (f0(t->value, x) != x) ? offset + cnt(t->l) : find(t->l, x, offset, left);\n }\n }\n }\n }\n\n void reverse(Tree t, int l, int r) {\n if (l > r) return;\n Tree t1, t2, t3;\n split(t, l, t1, t2);\n split(t2, r - l, t2, t3);\n t2->rev ^= 1;\n merge(t2, t2, t3);\n merge(t, t1, t2);\n }\n\n // [l, r)の先頭がmになるようにシフトさせる。std::rotateと同じ仕様\n void rotate(Tree t, int l, int m, int r) {\n reverse(t, l, r);\n reverse(t, l, l + r - m);\n reverse(t, l + r - m, r);\n }\n\n void dump(Tree t) {\n if (!t) return;\n pushdown(t);\n dump(t->l);\n cout << t->value << \" \";\n dump(t->r);\n }\n\npublic:\n BaseImplicitTreap(T0 u0_, T1 u1_) : u0(u0_), u1(u1_) {}\n\n void set_by_vector(const vector<T0> &a) {\n for (int i = 0; i < a.size(); i++) {\n insert(i, a[i]);\n }\n }\n\n int size() { return cnt(root); }\n\n void insert(int pos, T0 x) { insert(root, pos, new Node(x, rnd.random(), u0, u1)); }\n\n void update(int l, int r, T1 x) { update(root, l, r, x); }\n\n T0 query(int l, int r) { return query(root, l, r); }\n\n // 二分探索。[l, r)内のkでf0(tr[k], x) != xとなる最左/最右のもの。存在しない場合は-1\n // たとえばMinMonoidの場合、x未満の最左/最右の要素の位置を返す\n int binary_search(int l, int r, T0 x, bool left = true) {\n if (l >= r) return -1;\n Tree t1, t2, t3;\n split(root, l, t1, t2);\n split(t2, r - l, t2, t3);\n int ret = find(t2, x, l, left);\n merge(t2, t2, t3);\n merge(root, t1, t2);\n return ret;\n }\n\n void erase(int pos) { erase(root, pos); }\n\n void reverse(int l, int r) { reverse(root, l, r); }\n\n void rotate(int l, int m, int r) { rotate(root, l, m, r); }\n\n void dump() {\n dump(root);\n cout << endl;\n }\n\n T0 operator[](int pos) { return query(pos, pos + 1); }\n};\n\ntemplate <class T0, class T1>\nstruct MinUpdateQuery : public BaseImplicitTreap<T0, T1> {\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n MinUpdateQuery() : MinUpdateQuery(numeric_limits<T0>::max(), numeric_limits<T1>::min()) {}\n T0 f0(T0 x, T0 y) override { return min(x, y); }\n T1 f1(T1 x, T1 y) override { return y == numeric_limits<T1>::min() ? x : y; }\n T0 g(T0 x, T1 y) override { return y == numeric_limits<T1>::min() ? x : y; }\n T1 p(T1 x, int len) override { return x; }\n};\n\ntemplate <class T0, class T1>\nstruct SumAddQuery : public BaseImplicitTreap<T0, T1> {\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n SumAddQuery() : SumAddQuery(0, 0) {}\n T0 f0(T0 x, T0 y) override { return x + y; }\n T1 f1(T1 x, T1 y) override { return x + y; }\n T0 g(T0 x, T1 y) override { return x + y; }\n T1 p(T1 x, int len) override { return x * len; }\n};\n\ntemplate <class T0, class T1>\nstruct MinAddQuery : public BaseImplicitTreap<T0, T1> {\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n MinAddQuery() : MinAddQuery(numeric_limits<T0>::max(), 0) {}\n T0 f0(T0 x, T0 y) override { return min(x, y); }\n T1 f1(T1 x, T1 y) override { return x + y; }\n T0 g(T0 x, T1 y) override { return x + y; }\n T1 p(T1 x, int len) override { return x; }\n};\n\ntemplate <class T0, class T1>\nstruct SumUpdateQuery : public BaseImplicitTreap<T0, T1> {\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n SumUpdateQuery() : SumUpdateQuery(0, numeric_limits<T1>::min()) {}\n T0 f0(T0 x, T0 y) override { return x + y; }\n T1 f1(T1 x, T1 y) override { return y == numeric_limits<T1>::min() ? x : y; }\n T0 g(T0 x, T1 y) override { return y == numeric_limits<T1>::min() ? x : y; }\n T1 p(T1 x, int len) override { return x == numeric_limits<T1>::min() ? numeric_limits<T1>::min() : x * len; }\n};\n\ntemplate <class T0>\nstruct SumAffineQuery : public BaseImplicitTreap<T0, pair<T0, T0>> {\n using T1 = pair<T0, T0>; // first * x + second\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n SumAffineQuery() : SumAffineQuery(0, {1, 0}) {}\n T0 f0(T0 x, T0 y) override { return x + y; }\n T1 f1(T1 x, T1 y) override { return {x.first * y.first, x.second * y.first + y.second}; }\n T0 g(T0 x, T1 y) override { return y.first * x + y.second; }\n T1 p(T1 x, int len) override { return {x.first, x.second * len}; }\n // update(i, j, {a, b}); // [i, j)にax + bを作用\n // update(i, j, {0, a}); // update\n // update(i, j, {1, a}); // 加算\n // update(i, j, {a, 0}); // 倍\n};\n\ntemplate <class T>\nstruct MinmaxAffineQuery : public BaseImplicitTreap<pair<T, T>, pair<T, T>> {\n using T0 = pair<T, T>; // {min, max}\n using T1 = pair<T, T>; // first * x + second\n using BaseImplicitTreap<T0, T1>::BaseImplicitTreap;\n MinmaxAffineQuery()\n : MinmaxAffineQuery({numeric_limits<T>::max(), -numeric_limits<T>::max()}, {1, 0}) {\n } // TODO: _u1を使うとコンパイル通らない原因不明\n T0 f0(T0 x, T0 y) override { return {min(x.first, y.first), max(x.second, y.second)}; }\n T1 f1(T1 x, T1 y) override { return {x.first * y.first, x.second * y.first + y.second}; }\n T0 g(T0 x, T1 y) override {\n T0 ret = {x.first * y.first + y.second, x.second * y.first + y.second};\n if (y.first < 0) swap(ret.first, ret.second);\n return ret;\n }\n T1 p(T1 x, int len) override { return x; }\n // update(i, j, {a, b}); // [i, j)にax + bを作用\n // update(i, j, {0, a}); // update\n // update(i, j, {1, a}); // 加算\n // update(i, j, {a, 0}); // 倍\n};\nint main(){\n\tcin.tie(nullptr);\n\tint n,q;\n cin >> n >> q;\n vi a(n);\n cin >> a;\n MinUpdateQuery<ll,ll> s;\n s.set_by_vector(a);\n while(q--){\n ll t,x,y;\n cin >> t >> x >> y;\n if(t==0){\n s.rotate(x,y,y+1);\n }\n if(t==1){\n cout << s.query(x,y+1) << endl;\n }\n if(t==2){\n s.update(x,x+1,y);\n }\n }\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 17536, "score_of_the_acc": -0.2762, "final_rank": 14 }, { "submission_id": "aoj_1508_9603866", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define vi vector<int>\n#define vl vector<ll>\n#define ov3(a, b, c, name, ...) name\n#define rep0(n) for(ll aaaaa = 0; aaaaa < n; ++aaaaa)\n#define rep1(i, n) for(ll i = 0; i < (n); i++)\n#define rep2(i, a, b) for(ll i = (a); i < (b); i++)\n#define rep(...) ov3(__VA_ARGS__, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for(ll i = (a) - 1; i >= (b); i--)\n#define all(a) begin(a), end(a)\n#define si(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n// bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\n// bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\n\nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n\n#define i128 __int128_t\n\nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\n\ntemplate <typename T, T (*f)(T, T), T (*e)()> struct RBST {\n inline int rnd() {\n static int x = 123456789;\n static int y = 362436069;\n static int z = 521288629;\n static int w = 88675123;\n int t;\n\n t = x ^ (x << 11);\n x = y;\n y = z;\n z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n struct node {\n node *l, *r;\n int cnt;\n T x, sum;\n node() = default;\n node(T x) : x(x), sum(x), l(0), r(0) { cnt = 1; }\n };\n RBST(int n) : pool(n) {}\n int cnt(const node *t) { return t ? t->cnt : 0; }\n T sum(const node *t) { return t ? t->sum : e(); }\n node *update(node *t) {\n t->cnt = cnt(t->l) + cnt(t->r) + 1;\n t->sum = f(f(sum(t->l), t->x), sum(t->r));\n return t;\n }\n vector<node> pool;\n int ptr = 0;\n inline node *alloc(const T &v) {\n if(si(pool) == ptr) pool.resize(si(pool) * 2);\n return &(pool[ptr++] = node(v));\n }\n node *merge(node *l, node *r) {\n if(!l or !r) return l ? l : r;\n if(rnd() % (cnt(l) + cnt(r)) < cnt(l)) {\n l->r = merge(l->r, r);\n return update(l);\n }\n r->l = merge(l, r->l);\n return update(r);\n }\n\n pair<node *, node *> split(node *t, int k) {\n if(!t) return {t, t};\n if(k <= cnt(t->l)) {\n auto [l, r] = split(t->l, k);\n t->l = r;\n return {l, update(t)};\n }\n auto [l, r] = split(t->r, k - cnt(t->l) - 1);\n t->r = l;\n return {update(t), r};\n }\n\n void insert(node *&t, int k, const T &v) {\n auto [l, r] = split(t, k);\n t = merge(merge(l, alloc(v)), r);\n }\n};\n\nint f(int x, int y) { return min(x, y); }\nint e() { return INF; }\n\nint main() {\n int n, q;\n cin >> n >> q;\n RBST<int, f, e> rbst(n + q);\n RBST<int, f, e>::node *root = nullptr;\n rep(i, n) {\n int x;\n cin >> x;\n rbst.insert(root, i, x);\n }\n\n rep(q) {\n int t;\n cin >> t;\n if(!t) {\n int l, r;\n cin >> l >> r;\n r++;\n auto [l1, tmp] = rbst.split(root, l);\n auto [mid, r1] = rbst.split(tmp, r - l);\n auto [m1, m2] = rbst.split(mid, rbst.cnt(mid) - 1);\n mid = rbst.merge(m2, m1);\n root = rbst.merge(rbst.merge(l1, mid), r1);\n } else if(t == 1) {\n int l, r;\n cin >> l >> r;\n r++;\n auto [l1, tmp] = rbst.split(root, l);\n auto [mid, r1] = rbst.split(tmp, r - l);\n cout << rbst.sum(mid) << '\\n';\n root = rbst.merge(l1, rbst.merge(mid, r1));\n } else {\n int y, z;\n cin >> y >> z;\n auto [l1, tmp] = rbst.split(root, y);\n auto [mid, r1] = rbst.split(tmp, 1);\n mid = rbst.alloc(z);\n root = rbst.merge(rbst.merge(l1, mid), r1);\n }\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 15544, "score_of_the_acc": -0.1482, "final_rank": 8 }, { "submission_id": "aoj_1508_9380837", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n//push から update を消している\ntemplate <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()>\nstruct Treap {\n private:\n unsigned int xorshift() {\n static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n unsigned int t = (x ^ (x << 11));\n x = y;\n y = z;\n z = w;\n return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));\n }\n \n struct Node {\n Node *left, *right;\n S value;\n int priority, size;\n S sum;\n F lazy;\n int rev;\n \n Node(S value_, int priority_) : \n left(nullptr), right(nullptr), value(value_), \n priority(priority_), size(1),\n sum(value_), lazy(id()), rev(0) {}\n };\n \n Node *root = nullptr;\n \n int cnt(Node *t) {\n return t ? t->size : 0;\n }\n \n S get_sum(Node *t) {\n return t ? t->sum : e();\n }\n \n void update(Node *t) {\n if (t) {\n t->size = cnt(t->left) + cnt(t->right) + 1;\n t->sum = op(op(get_sum(t->left), t->value), get_sum(t->right));\n }\n }\n \n void push(Node *t) {\n if (t && t->rev) {\n t->rev = false;\n swap(t->left, t->right);\n if (t->left) {\n t->left->rev ^= 1;\n }\n if (t->right) {\n t->right->rev ^= 1;\n }\n }\n all_apply(t->left, t->lazy);\n all_apply(t->right, t->lazy);\n t->lazy = id();\n //update(t);\n }\n \n Node *merge(Node *lroot, Node *rroot) {\n if (!lroot || !rroot) {\n return lroot ? lroot : rroot;\n } else if (lroot->priority > rroot->priority) {\n push(lroot);\n lroot->right = merge(lroot->right, rroot);\n update(lroot);\n return lroot;\n } else {\n push(rroot);\n rroot->left = merge(lroot, rroot->left);\n update(rroot);\n return rroot;\n }\n }\n \n pair<Node *, Node *> split(Node *t, int k) {\n if (!t) {\n return {nullptr, nullptr};\n }\n push(t);\n if (k <= cnt(t->left)) {\n auto [lroot, rroot] = split(t->left, k);\n t->left = rroot;\n update(t);\n return {lroot, t};\n } else {\n auto [lroot, rroot] = split(t->right, k - cnt(t->left) - 1);\n t->right = lroot;\n update(t);\n return {t, rroot};\n }\n }\n \n void all_apply(Node *t, F f) {\n if (!t) {\n return;\n }\n t->value = mapping(f, t->value);\n t->sum = mapping(f, t->sum);\n t->lazy = composition(f, t->lazy);\n }\n \n void dump(Node *t) {\n if (!t) {\n return;\n }\n push(t);\n dump(t->left);\n cout << t->value << \" \";\n dump(t->right);\n }\n \n public:\n Treap() {}\n Treap(vector<S> v) {\n std::reverse(v.begin(), v.end());\n for (S &x : v) {\n insert(0, x);\n }\n }\n \n void set(int p, S x) {\n erase(p);\n insert(p, x);\n }\n \n void insert(int p, S x) {\n auto [lroot, rroot] = split(root, p);\n root = merge(merge(lroot, new Node(x, xorshift())), rroot);\n }\n \n void erase(int p) {\n auto [Lroot, rroot] = split(root, p + 1);\n auto [lroot, mroot] = split(Lroot, p);\n root = merge(lroot, rroot);\n }\n \n S prod(int l, int r) {\n auto [Lroot, rroot] = split(root, r);\n auto [lroot, mroot] = split(Lroot, l);\n S res = mroot->sum;\n root = merge(merge(lroot, mroot), rroot);\n return res;\n }\n \n void apply(int l, int r, F f) {\n auto [Lroot, rroot] = split(root, r);\n auto [lroot, mroot] = split(Lroot, l);\n all_apply(mroot, f);\n root = merge(merge(lroot, mroot), rroot);\n }\n \n void reverse(int l, int r) {\n if (l > r) {\n return;\n }\n auto [Lroot, rroot] = split(root, r);\n auto [lroot, mroot] = split(Lroot, l);\n mroot->rev ^= 1;\n push(mroot);\n root = merge(merge(lroot, mroot), rroot);\n }\n \n void rotate(int l, int r, int m) {\n reverse(l, r);\n reverse(l, l + r - m);\n reverse(l + r - m, r);\n }\n \n S operator[](int p) {\n return prod(p, p + 1);\n }\n \n void dump() {\n dump(root);\n cout << endl;\n }\n};\n\nint op(int a, int b) {\n return min(a, b);\n}\n\nint e() {\n return 2000000000;\n}\n\nint mapping(int f, int x) {\n return f + x;\n}\n\nint composition(int f, int g) {\n return f + g;\n}\n\nint id() {\n return 0;\n}\n\nint main() {\n int n, q;\n cin >> n >> q;\n vector<int> a(n);\n for (int i = 0; i < n; i++) {\n cin >> a[i];\n }\n Treap<int, op, e, int, mapping, composition, id> tr(a);\n while (q--) {\n int t;\n cin >> t;\n if (t == 0) {\n int l, r;\n cin >> l >> r;\n tr.rotate(l, r + 1, r);\n } else if (t == 1) {\n int l, r;\n cin >> l >> r;\n cout << tr.prod(l, r + 1) << endl;\n } else {\n int p, x;\n cin >> p >> x;\n tr.set(p, x);\n }\n }\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 16308, "score_of_the_acc": -0.2513, "final_rank": 13 } ]
aoj_1513_cpp
Problem E: Doragoso Ball Problem L 個集めるとドラゴソが現れどんな願いでも一つだけ叶えてくれるというドラゴソボールがとある迷宮に散らばっている。入口からスタートし、途中で立ち止まることなくすべてのボールを回収し、それらを迷宮内にある祭壇に奉納し、そのまま呪文を唱えるとドラゴソが現れ願いを叶えてくれるという。ただし、呪文を唱え始める時間は入口からボールを奉納するまでのルートのうち、移動時間が K 番目に短いルートで移動したときの時間でなければならない。 この迷宮は部屋とわたり通路からなり、部屋と部屋はわたり通路を通る事によって行き来できる。ドラゴソボールはそれらの数ある部屋のどこかに落ちている。 なお、迷宮は入り口から祭壇へたどりつく道が存在しない可能性もあるようだ。また、移動時間はわたり通路の移動だけを考慮し、部屋の中を移動する時間、ボールを拾うときにかかる時間、祭壇に奉納する時間は考慮しなくてよい。 また、同じ通路や同じ部屋に何度も訪れてもよいが、ドラゴソボールがある部屋に初めて入った場合は、そのボールを拾うことにする。祭壇に到達したとき、必ずしもボールを納めなければならないわけではない。 迷宮の内部構造の情報と迷宮の入り口、祭壇の場所とボールの数 L とボールの配置場所と求められている最短な経路の順位 K が与えられるので、呪文を唱え始めるべき時間を求めよ。 また、異なる移動ルートでも、移動時間が同じである場合はそれらのルートは同じ順位として扱う。 例えば、 ルートA 時間 2 ルートB 時間 5 ルートC 時間 5 ルートD 時間 5 ルートE 時間 5 ルートF 時間 7 このようなとき、ルートB,C,D,Eは2位かつ3位かつ4位かつ5位として扱う。ルートFは6位として扱う。 Input 入力は複数のデータセットからなる。 各データセットは以下のフォーマットで表される。 N M L K u 1 v 1 c 1 … u M v M c M S G B 1 B 2 … B L 最初に N , M , L , K が与えられる。 それぞれ、部屋の数、わたり通路の数、ボールの数、求められている最短な経路の順位を表す。それぞれの部屋は1〜 N で番号付けされる。 次に M 行にわたり通路の情報、 u i , v i , c i が与えられる。 部屋 u i , v i 間にわたり通路が存在し、通過するのに時間 c i がかかることを表す。 次に S , G が与えられる。 それぞれ、迷宮の入り口と祭壇がある部屋番号を表す。 次に L 行にわたってドラゴソボールが落ちている部屋の番号 B i ( 1 ≤ i ≤ L )が与えられる。 入力の終わりは4つのゼロからなる。 Constraints 入力は以下の条件を満たす。 入力に含まれる値はすべて整数 2 ≤ N ≤ 50 0 ≤ M ≤ 1225 1 ≤ L ≤ 7 1 ≤ K ≤ 10 1 ≤ S , G ≤ N 1 ≤ B i ≤ N ( 1 ≤ i ≤ L ) B i ≠ B j ( 1 ≤ i < j ≤ L ) 1 ≤ u i , v i ≤ N ( 1 ≤ i ≤ M ) 1 ≤ c i ≤ 1000 ( 1 ≤ i ≤ M ) データセットは最大10個である Output 各データセットに対し、呪文を唱え始めるべき時間を1行に出力せよ。 K 番目に短い経路が存在しない場合は"NA"を1行に出力せよ。 Sample Input 9 14 5 10 1 2 1 1 4 5 2 4 4 2 3 3 2 6 2 3 6 3 3 7 2 4 6 5 4 5 6 5 8 8 6 7 3 7 8 1 7 9 9 8 9 2 1 9 1 2 5 7 9 4 5 1 3 1 2 1 1 3 2 2 3 2 2 4 9 3 4 1 1 4 2 4 5 1 10 1 2 1 1 3 2 2 3 2 2 4 9 3 4 1 1 4 2 4 3 1 1 1 2 1 2 3 1 3 4 1 1 4 1 4 3 1 2 1 2 1 2 3 1 3 4 1 1 4 1 4 3 1 10 1 2 1 2 3 1 3 4 1 1 4 1 2 1 1 1 1 2 1 1 2 1 2 1 1 10 1 2 1 1 2 1 4 2 1 4 1 2 1 3 4 1 1 4 2 0 0 0 0 Sample Output 27 6 8 3 5 7 1 19 NA
[ { "submission_id": "aoj_1513_10235149", "code_snippet": "// AOJ #1513 Doragoso Ball\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n \nstruct Edge { int to, cost; };\n \nstruct State { ll cost; int room, mask; };\n \nstruct StateCompare {\n bool operator()(const State &a, const State &b) const {\n return a.cost > b.cost;\n }\n};\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n while(true){\n int N, M, L, K;\n cin >> N >> M >> L >> K;\n if(N==0) break;\n \n vector<vector<Edge>> graph(N+1);\n for (int i=0; i<M; i++){\n int u, v, c;\n cin >> u >> v >> c;\n graph[u].push_back({v,c});\n graph[v].push_back({u,c});\n }\n \n int S, G;\n cin >> S >> G;\n \n vector<int> ballIndex(N+1, -1);\n vector<int> ballRooms(L);\n for (int i=0; i<L; i++){\n int b;\n cin >> b;\n ballRooms[i] = b;\n ballIndex[b] = i;\n }\n \n int initMask = 0;\n if(ballIndex[S] != -1) initMask |= (1 << ballIndex[S]);\n \n int fullMask = (1 << L) - 1;\n \n vector<vector<int>> cnt(N+1, vector<int>(1 << L, 0));\n \n priority_queue<State, vector<State>, StateCompare> pq;\n pq.push({0, S, initMask});\n \n int found = 0;\n ll ans = -1;\n while(!pq.empty()){\n State cur = pq.top();\n pq.pop();\n \n if(cnt[cur.room][cur.mask] >= K) continue;\n cnt[cur.room][cur.mask]++;\n \n if(cur.room == G && cur.mask == fullMask){\n found++;\n if(found == K){\n ans = cur.cost;\n break;\n }\n }\n \n for(auto &edge : graph[cur.room]){\n int nxt = edge.to;\n ll ncost = cur.cost + edge.cost;\n int nmask = cur.mask;\n if(ballIndex[nxt] != -1) nmask |= (1 << ballIndex[nxt]);\n pq.push({ncost, nxt, nmask});\n }\n }\n if(ans < 0) cout << \"NA\" << endl;\n else cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 70920, "score_of_the_acc": -1.1613, "final_rank": 14 }, { "submission_id": "aoj_1513_8293549", "code_snippet": "#include <iostream>\n#include <queue>\n#include <vector>\n#include <functional>\nusing namespace std;\n\nint N;\nint M, U[1250], V[1250], C[1250];\nint L;\nint K;\nint Start, Goal;\nint B[69];\nint mask[69];\n\n// Graph\npair<int, int> Dist[59][1 << 7][21];\nvector<pair<int, int>> G[59];\npriority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> Q;\n\nvoid Initialize() {\n\tfor (int i = 0; i < 59; i++) G[i].clear();\n\tfor (int i = 0; i < 59; i++) B[i] = 0;\n\tfor (int i = 0; i < 59; i++) mask[i] = 0;\n\tfor (int i = 0; i < 59; i++) {\n\t\tfor (int j = 0; j < 128; j++) {\n\t\t\tfor (int k = 0; k < 21; k++) Dist[i][j][k] = make_pair(0, 0);\n\t\t}\n\t}\n}\n\nint main() {\n\twhile (true) {\n\t\tInitialize();\n\n\t\t// Step 1. Input\n\t\tcin >> N >> M >> L >> K; if (N + M + L + K == 0) break;\n\t\tfor (int i = 0; i < M; i++) cin >> U[i] >> V[i] >> C[i];\n\t\tcin >> Start >> Goal;\n\t\tfor (int i = 0; i < L; i++) cin >> B[i];\n\n\t\t// Step 2. Initialize\n\t\tfor (int i = 1; i <= N; i++) mask[i] = 0;\n\t\tfor (int i = 0; i < L; i++) mask[B[i]] = (1 << i);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tG[U[i]].push_back(make_pair(V[i], C[i]));\n\t\t\tG[V[i]].push_back(make_pair(U[i], C[i]));\n\t\t}\n\n\t\t// Step 3. Dijkstra Init\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = 0; j < (1 << 7); j++) {\n\t\t\t\tfor (int k = 0; k < 10; k++) Dist[i][j][k] = make_pair(100000000, k);\n\t\t\t}\n\t\t}\n\t\tDist[Start][mask[Start]][0] = make_pair(0, Start);\n\t\tQ.push(make_tuple(0, Start, mask[Start]));\n\n\t\t// Step 4. Dijkstra\n\t\twhile (!Q.empty()) {\n\t\t\tint pos1 = get<1>(Q.top());\n\t\t\tint pos2 = get<2>(Q.top());\n\t\t\tQ.pop();\n\t\t\tfor (pair<int, int> tmp : G[pos1]) {\n\t\t\t\tint nex1 = tmp.first;\n\t\t\t\tint nex2 = (pos2 | mask[nex1]);\n\n\t\t\t\t// Calculate Cost\n\t\t\t\tvector<pair<int, int>> vec;\n\t\t\t\tfor (int i = 0; i < K; i++) vec.push_back(Dist[nex1][nex2][i]);\n\t\t\t\tfor (int i = 0; i < K; i++) {\n\t\t\t\t\tif (Dist[pos1][pos2][i].first == 100000000) continue;\n\t\t\t\t\tint cost_ = Dist[pos1][pos2][i].first + tmp.second;\n\t\t\t\t\tint hash_ = Dist[pos1][pos2][i].second * 233 + nex1;\n\t\t\t\t\tvec.push_back(make_pair(cost_, hash_));\n\t\t\t\t}\n\t\t\t\tsort(vec.begin(), vec.end());\n\t\t\t\tvec.erase(unique(vec.begin(), vec.end()), vec.end());\n\n\t\t\t\t// Check Difference\n\t\t\t\tint MinDiff = (1 << 30);\n\n\t\t\t\tfor (int i = 0; i < K; i++) {\n\t\t\t\t\tif (Dist[nex1][nex2][i].first == vec[i].first) continue;\n\t\t\t\t\tMinDiff = min(MinDiff, vec[i].first);\n\t\t\t\t}\n\t\t\t\tfor (int i = 0; i < K; i++) Dist[nex1][nex2][i] = vec[i];\n\t\t\t\tif (MinDiff != (1 << 30)) Q.push(make_tuple(MinDiff, nex1, nex2));\n\t\t\t}\n\t\t}\n\n\t\t// Step 5. Final\n\t\tif (Dist[Goal][(1 << L) - 1][K - 1].first == 100000000) cout << \"NA\" << endl;\n\t\telse cout << Dist[Goal][(1 << L) - 1][K - 1].first << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2180, "memory_kb": 4852, "score_of_the_acc": -0.7668, "final_rank": 10 }, { "submission_id": "aoj_1513_2662325", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_to,int arg_cost){\n\t\tto = arg_to;\n\t\tcost = arg_cost;\n\t}\n\tint to,cost;\n};\n\nstruct Data{\n\tData(int arg_node_id,int arg_ball_state,int arg_total_cost){\n\t\tnode_id = arg_node_id;\n\t\tball_state = arg_ball_state;\n\t\ttotal_cost = arg_total_cost;\n\t}\n\tbool operator<(const struct Data &arg) const{\n\t\treturn total_cost > arg.total_cost;\n\t}\n\tint node_id,ball_state,total_cost;\n};\n\nint N,M,L,K;\nint POW[8],min_cost[50][10][128],index[50][128],exist_ball[50];\nvector<Info> G[50];\n\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++)G[i].clear();\n\n\tint from,to,cost;\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%d %d %d\",&from,&to,&cost);\n\t\tfrom--;\n\t\tto--;\n\t\tG[from].push_back(Info(to,cost));\n\t\tG[to].push_back(Info(from,cost));\n\t}\n\n\tint start,goal;\n\tscanf(\"%d %d\",&start,&goal);\n\tstart--;\n\tgoal--;\n\n\tfor(int i = 0; i < N; i++)exist_ball[i] = -1;\n\n\tint tmp;\n\tfor(int i = 0; i < L; i++){\n\t\tscanf(\"%d\",&tmp);\n\t\ttmp--;\n\t\texist_ball[tmp] = i;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int state = 0; state < POW[L]; state++)index[i][state] = 0;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int a = 0; a < K; a++){\n\t\t\tfor(int state = 0; state < POW[L]; state++)min_cost[i][a][state] = BIG_NUM;\n\t\t}\n\t}\n\n\tpriority_queue<Data> Q;\n\n\tint first_state;\n\n\tif(exist_ball[start] != -1){\n\t\tfirst_state = POW[exist_ball[start]];\n\t}else{\n\t\tfirst_state = 0;\n\t}\n\tmin_cost[start][index[start][first_state]++][first_state] = 0;\n\n\tint next_node,next_state;\n\n\tfor(int i = 0; i < G[start].size(); i++){\n\t\tnext_node = G[start][i].to;\n\n\t\tif(exist_ball[next_node] != -1){\n\t\t\tnext_state = first_state|POW[exist_ball[next_node]];\n\t\t}else{\n\t\t\tnext_state = first_state;\n\t\t}\n\t\tif(index[next_node][next_state] >= K)continue;\n\n\t\tQ.push(Data(next_node,next_state,G[start][i].cost));\n\t}\n\n\n\twhile(!Q.empty()){\n\n\t\tif(index[Q.top().node_id][Q.top().ball_state] >= K){\n\t\t\tif(Q.top().node_id == goal && Q.top().ball_state == POW[L]-1)break;\n\t\t\tQ.pop();\n\t\t\tcontinue;\n\t\t}\n\n\t\tmin_cost[Q.top().node_id][index[Q.top().node_id][Q.top().ball_state]++][Q.top().ball_state] = Q.top().total_cost;\n\n\t\tfor(int i = 0; i < G[Q.top().node_id].size(); i++){\n\t\t\tnext_node= G[Q.top().node_id][i].to;\n\n\t\t\tif(exist_ball[next_node] != -1){\n\t\t\t\tnext_state = Q.top().ball_state|POW[exist_ball[next_node]];\n\t\t\t}else{\n\t\t\t\tnext_state = Q.top().ball_state;\n\t\t\t}\n\n\t\t\tif(index[next_node][next_state] >= K)continue;\n\n\t\t\tQ.push(Data(next_node,next_state,Q.top().total_cost+G[Q.top().node_id][i].cost));\n\t\t}\n\t\tQ.pop();\n\t}\n\n\tif(min_cost[goal][K-1][POW[L]-1] == BIG_NUM){\n\t\tprintf(\"NA\\n\");\n\t}else{\n\t\tprintf(\"%d\\n\",min_cost[goal][K-1][POW[L]-1]);\n\t}\n}\n\n\nint main(){\n\n\tfor(int i = 0; i < 8; i++)POW[i] = pow(2,i);\n\n\twhile(true){\n\t\tscanf(\"%d %d %d %d\",&N,&M,&L,&K);\n\t\tif(N == 0 && M == 0 && L == 0 && K == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 670, "memory_kb": 41112, "score_of_the_acc": -0.7245, "final_rank": 9 }, { "submission_id": "aoj_1513_2662323", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_to,int arg_cost){\n\t\tto = arg_to;\n\t\tcost = arg_cost;\n\t}\n\tint to,cost;\n};\n\nstruct Data{\n\tData(int arg_node_id,int arg_ball_state,int arg_total_cost){\n\t\tnode_id = arg_node_id;\n\t\tball_state = arg_ball_state;\n\t\ttotal_cost = arg_total_cost;\n\t}\n\tbool operator<(const struct Data &arg) const{\n\t\treturn total_cost > arg.total_cost;\n\t}\n\tint node_id,ball_state,total_cost;\n};\n\nint N,M,L,K;\nint POW[8],min_cost[50][10][128],index[50][128],exist_ball[50];\nvector<Info> G[50];\n\n\nvoid func(){\n\n\tfor(int i = 0; i < N; i++)G[i].clear();\n\n\tint from,to,cost;\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%d %d %d\",&from,&to,&cost);\n\t\tfrom--;\n\t\tto--;\n\t\tG[from].push_back(Info(to,cost));\n\t\tG[to].push_back(Info(from,cost));\n\t}\n\n\tint start,goal;\n\tscanf(\"%d %d\",&start,&goal);\n\tstart--;\n\tgoal--;\n\n\tfor(int i = 0; i < N; i++)exist_ball[i] = -1;\n\n\tint tmp;\n\tfor(int i = 0; i < L; i++){\n\t\tscanf(\"%d\",&tmp);\n\t\ttmp--;\n\t\texist_ball[tmp] = i;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int state = 0; state < POW[L]; state++)index[i][state] = 0;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int a = 0; a < K; a++){\n\t\t\tfor(int state = 0; state < POW[L]; state++)min_cost[i][a][state] = BIG_NUM;\n\t\t}\n\t}\n\n\tpriority_queue<Data> Q;\n\n\tint first_state;\n\n\tif(exist_ball[start] != -1){\n\t\tfirst_state = POW[exist_ball[start]];\n\t}else{\n\t\tfirst_state = 0;\n\t}\n\tmin_cost[start][index[start][first_state]++][first_state] = 0;\n\n\tint next_node,next_state;\n\n\tfor(int i = 0; i < G[start].size(); i++){\n\t\tnext_node = G[start][i].to;\n\n\t\tif(exist_ball[next_node] != -1){\n\t\t\tnext_state = first_state|POW[exist_ball[next_node]];\n\t\t}else{\n\t\t\tnext_state = first_state;\n\t\t}\n\t\tif(index[next_node][next_state] >= K)continue;\n\n\t\tQ.push(Data(next_node,next_state,G[start][i].cost));\n\t}\n\n\n\twhile(!Q.empty()){\n\n\t\tif(index[Q.top().node_id][Q.top().ball_state] >= K){\n\t\t\tQ.pop();\n\t\t\tcontinue;\n\t\t}\n\n\t\tmin_cost[Q.top().node_id][index[Q.top().node_id][Q.top().ball_state]++][Q.top().ball_state] = Q.top().total_cost;\n\n\t\tfor(int i = 0; i < G[Q.top().node_id].size(); i++){\n\t\t\tnext_node= G[Q.top().node_id][i].to;\n\n\t\t\tif(exist_ball[next_node] != -1){\n\t\t\t\tnext_state = Q.top().ball_state|POW[exist_ball[next_node]];\n\t\t\t}else{\n\t\t\t\tnext_state = Q.top().ball_state;\n\t\t\t}\n\n\t\t\tif(index[next_node][next_state] >= K)continue;\n\n\t\t\tQ.push(Data(next_node,next_state,Q.top().total_cost+G[Q.top().node_id][i].cost));\n\t\t}\n\t\tQ.pop();\n\t}\n\n\tif(min_cost[goal][K-1][POW[L]-1] == BIG_NUM){\n\t\tprintf(\"NA\\n\");\n\t}else{\n\t\tprintf(\"%d\\n\",min_cost[goal][K-1][POW[L]-1]);\n\t}\n}\n\n\nint main(){\n\n\tfor(int i = 0; i < 8; i++)POW[i] = pow(2,i);\n\n\twhile(true){\n\t\tscanf(\"%d %d %d %d\",&N,&M,&L,&K);\n\t\tif(N == 0 && M == 0 && L == 0 && K == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1520, "memory_kb": 41160, "score_of_the_acc": -1.0132, "final_rank": 12 }, { "submission_id": "aoj_1513_2119119", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long \n#define pb push_back \n#define fr first\n#define sc second\n#define Rep(i,n) for(int i=0;i<(n);i++)\n#define All(v) v.begin(),v.end()\n#define Uniq(v) v.erase(unique(All(v)),v.end())\ntypedef pair<int, int> Pii; \ntypedef pair<int, Pii> Pip;\ntypedef pair<string, int> Psi;\nconst int INF = (1<<30);\nconst int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};\n\nstruct Edge {\n int to, cost;\n Edge(int to_, int cost_) {\n to = to_; cost = cost_;\n }\n};\n\nstruct State {\n int room, cost, bit;\n State(int room_, int cost_, int bit_) {\n room = room_; cost = cost_; bit = bit_;\n }\n bool operator<(const State& s) const {\n return cost < s.cost;\n }\n bool operator>(const State& s) const {\n return cost > s.cost;\n }\n};\n\nint N, M, L, K;\nint S, G;\nint ball_room[8];\nvector<Edge> graph[51];\n\nint dij()\n{\n priority_queue<State, vector<State>, greater<State> > q;\n int used[51][1 << 8] = {{0}};\n int cnt = 0; //これがKになったらコストを返す\n\n Rep(i, L) {\n if( ball_room[i] == S ) {\n q.push( State(S, 0, 1 << i) );\n break;\n }\n if( i == L-1 ) q.push( State(S, 0, 0) );\n }\n\n while( !q.empty() ) {\n State p = q.top(); q.pop();\n int room = p.room, cost = p.cost, bit = p.bit;\n if( room == G && bit == (1 << L)-1 ) {\n cnt++;\n if( cnt == K ) return cost;\n }\n \n if( used[room][bit] >= K ) continue;\n used[room][bit]++;\n\n vector<Edge> e = graph[room];\n Rep(i, e.size()) {\n int next = e[i].to, next_bit = bit, next_cost = cost + e[i].cost;\n Rep(j, L) {\n\tif( ball_room[j] == next ) {\n\t next_bit = (next_bit | (1 << j)); \n\t break;\n\t}\n }\n if( used[next][next_bit] >= K ) continue;\n q.push( State(next, next_cost, next_bit) );\n }\n }\n\n return -1;\n}\n\n\n\nint main()\n{\n while( cin >> N >> M >> L >> K, N ) {\n Rep(i, 51) graph[i].clear();\n Rep(i, M) {\n int a, b, c;\n cin >> a >> b >> c;\n a--; b--;\n graph[a].pb( Edge(b, c) );\n graph[b].pb( Edge(a, c) );\n }\n \n cin >> S >> G;\n S--; G--;\n \n Rep(i, L) {\n cin >> ball_room[i];\n ball_room[i]--;\n }\n \n int ans = dij();\n if( ans == -1 ) cout << \"NA\" << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 40648, "score_of_the_acc": -0.7151, "final_rank": 8 }, { "submission_id": "aoj_1513_2119117", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long \n#define pb push_back \n#define fr first\n#define sc second\n#define Rep(i,n) for(int i=0;i<(n);i++)\n#define All(v) v.begin(),v.end()\n#define Uniq(v) v.erase(unique(All(v)),v.end())\ntypedef pair<int, int> Pii; \ntypedef pair<int, Pii> Pip;\ntypedef pair<string, int> Psi;\nconst int INF = (1<<30);\nconst int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};\n\nstruct Edge {\n int to, cost;\n Edge(int to_, int cost_) {\n to = to_; cost = cost_;\n }\n};\n\nstruct State {\n int room, cost, bit;\n State(int room_, int cost_, int bit_) {\n room = room_; cost = cost_; bit = bit_;\n }\n bool operator<(const State& s) const {\n return cost < s.cost;\n }\n bool operator>(const State& s) const {\n return cost > s.cost;\n }\n};\n\nint N, M, L, K;\nint S, G;\nint ball_room[8];\nvector<Edge> graph[51];\n\nint dij()\n{\n priority_queue<State, vector<State>, greater<State> > q;\n int used[51][1 << 8] = {{0}};\n int cnt = 0; //これがKになったらコストを返す\n\n Rep(i, L) {\n if( ball_room[i] == S ) {\n q.push( State(S, 0, 1 << i) );\n break;\n }\n if( i == L-1 ) q.push( State(S, 0, 0) );\n }\n\n while( !q.empty() ) {\n State p = q.top(); q.pop();\n int room = p.room, cost = p.cost, bit = p.bit;\n if( room == G && bit == (1 << L)-1 ) {\n cnt++;\n if( cnt == K ) return cost;\n }\n \n if( used[room][bit] > K ) continue;\n used[room][bit]++;\n\n vector<Edge> e = graph[room];\n Rep(i, e.size()) {\n int next = e[i].to, next_bit = bit, next_cost = cost + e[i].cost;\n Rep(j, L) {\n\tif( ball_room[j] == next ) {\n\t next_bit = (next_bit | (1 << j)); \n\t break;\n\t}\n }\n if( used[next][next_bit] > K ) continue;\n q.push( State(next, next_cost, next_bit) );\n }\n }\n\n return -1;\n}\n\n\n\nint main()\n{\n while( cin >> N >> M >> L >> K, N ) {\n Rep(i, 51) graph[i].clear();\n Rep(i, M) {\n int a, b, c;\n cin >> a >> b >> c;\n a--; b--;\n graph[a].pb( Edge(b, c) );\n graph[b].pb( Edge(a, c) );\n }\n \n cin >> S >> G;\n S--; G--;\n \n Rep(i, L) {\n cin >> ball_room[i];\n ball_room[i]--;\n }\n \n int ans = dij();\n if( ans == -1 ) cout << \"NA\" << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 43268, "score_of_the_acc": -0.8032, "final_rank": 11 }, { "submission_id": "aoj_1513_1678045", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef complex<double> P;\ntypedef pair<int,int> pii;\n#define REP(i,n) for(ll i=0;i<n;++i)\n#define REPR(i,n) for(ll i=1;i<n;++i)\n#define FOR(i,a,b) for(ll i=a;i<b;++i)\n\n#define DEBUG(x) cout<<#x<<\": \"<<x<<endl\n#define DEBUG_VEC(v) cout<<#v<<\":\";REP(i,v.size())cout<<\" \"<<v[i];cout<<endl\n#define ALL(a) (a).begin(),(a).end()\n\n#define MOD (ll)(1e9+7)\n#define ADD(a,b) a=((a)+(b))%MOD\n#define FIX(a) ((a)%MOD+MOD)%MOD\n\nstruct edge{\n int src,dst,cost;\n edge(int s,int d,int c):src(s),dst(d),cost(c){}\n};\n\nstruct state{\n int pos,dist,balls;\n state(int p,int d,int b):pos(p),dist(d),balls(b){}\n};\nbool operator<(const state &a,const state &b){\n if(a.dist!=b.dist)return a.dist>b.dist;\n return a.pos<b.pos;\n}\n\n// [at][balls]\nint times[50][1<<7];\n// ??¶?????°??????????????? N * 2^L * K\n\nbool solve(){\n int n,m,l,k;\n cin>>n>>m>>l>>k;\n if(!n)return false;\n assert(n<=50);\n assert(m<=1225);\n assert(l<=7);\n assert(k<=10);\n vector<edge> edges[n];\n REP(i,m){\n int u,v,c;\n cin>>u>>v>>c;\n --u; --v;\n edges[u].push_back(edge(u,v,c));\n edges[v].push_back(edge(v,u,c));\n }\n int start,goal;\n cin>>start>>goal;\n --start; --goal;\n int ball[n]; REP(i,n)ball[i]=0;\n REP(i,l){\n int b;\n cin>>b;\n ball[b-1] = 1<<i;\n }\n int maxball = (1<<l)-1;\n REP(i,n)REP(j,1<<l){\n times[i][j]=0;\n }\n priority_queue<state> Q;\n Q.push(state(start,0,0|ball[start]));\n int result = -1;\n while(!Q.empty()){\n state S = Q.top(); Q.pop();\n if(times[S.pos][S.balls] >= k)continue;\n times[S.pos][S.balls] ++;\n if(S.pos==goal && S.balls==maxball && times[S.pos][S.balls]==k){\n result = S.dist;\n break;\n }\n REP(i,edges[S.pos].size()){\n edge E = edges[S.pos][i];\n Q.push(state(E.dst,S.dist+E.cost,S.balls|ball[E.dst]));\n }\n }\n if(result==-1){\n cout<<\"NA\"<<endl;\n }else{\n cout<<result<<endl;\n }\n return true;\n}\n\nint main(){\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 1380, "memory_kb": 76940, "score_of_the_acc": -1.4291, "final_rank": 17 }, { "submission_id": "aoj_1513_1341718", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 50;\nconst int MAX_M = 1225;\nconst int MAX_K = 10;\nconst int MAX_L = 7;\nconst int MAX_BITS = (1 << MAX_L);\n\nconst int INF = 1 << 30;\n\n/* typedef */\n\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\n\nstruct Stat {\n int i, b, d;\n Stat(int _i, int _b, int _d) { i = _i, b = _b, d = _d; }\n bool operator<(const Stat& s) const { return d > s.d; }\n};\n\n/* global variables */\n\nint n, m, l, k;\nint st, gl, gbits;\nint bis[MAX_N];\n\nvpii nbrs[MAX_N];\nint dists[MAX_N][MAX_BITS][MAX_K];\n\n/* main */\n\nint main() {\n for (;;) {\n cin >> n >> m >> l >> k;\n if (n == 0) break;\n\n gbits = (1 << l) - 1;\n\n for (int i = 0; i < n; i++) {\n nbrs[i].clear();\n bis[i] = -1;\n for (int b = 0; b <= gbits; b++)\n\tfor (int j = 0; j < k; j++) dists[i][b][j] = INF;\n }\n\n for (int i = 0; i < m; i++) {\n int u, v, c;\n cin >> u >> v >> c;\n u--, v--;\n nbrs[u].push_back(pii(v, c));\n nbrs[v].push_back(pii(u, c));\n }\n\n cin >> st >> gl;\n st--, gl--;\n\n for (int i = 0; i < l; i++) {\n int bi;\n cin >> bi;\n bi--;\n bis[bi] = i;\n }\n\n int sb = (bis[st] >= 0) ? (1 << bis[st]) : 0;\n \n priority_queue<Stat> q;\n q.push(Stat(st, sb, 0));\n dists[st][sb][0] = 0;\n \n int distk = -1;\n \n while (! q.empty()) {\n Stat u = q.top();\n q.pop();\n\n int udk = dists[u.i][u.b][k - 1];\n if (u.d > udk) continue;\n \n if (u.i == gl && u.b == gbits && u.d == udk) {\n\tdistk = u.d;\n\tbreak;\n }\n\n vpii& nbru = nbrs[u.i];\n for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {\n\tint vi = vit->first;\n\n\tint vb = u.b;\n\tif (bis[vi] >= 0) vb |= (1 << bis[vi]);\n\n\tint vd = u.d + vit->second;\n\tint *dpt = dists[vi][vb];\n\t\n\tif (dpt[k - 1] > vd) {\n\t dpt[k - 1] = vd;\n\t for (int ki = k - 1; ki > 0 && dpt[ki] < dpt[ki - 1]; ki--) {\n\t int tmp = dpt[ki]; dpt[ki] = dpt[ki - 1]; dpt[ki - 1] = tmp;\n\t }\n\t q.push(Stat(vi, vb, vd));\n\t}\n }\n }\n\n if (distk < 0)\n cout << \"NA\" << endl;\n else\n cout << distk << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 4244, "score_of_the_acc": -0.0742, "final_rank": 1 }, { "submission_id": "aoj_1513_1144972", "code_snippet": "#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n//#include <utility>\n#include <set>\n#include <iostream>\n//#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n//#include <functional>\n#include <sstream>\n//#include <deque>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <cstdio>\n//#include <cctype>\n#include <cstring>\n//#include <ctime>\n#include <iterator>\n#include <bitset>\n//#include <numeric>\n//#include <list>\n//#include <iomanip>\n\n#if __cplusplus >= 201103L\n#include <array>\n#include <tuple>\n#include <initializer_list>\n#include <unordered_set>\n#include <unordered_map>\n#include <forward_list>\n\n#define cauto const auto&\n#else\n\n#endif\n\nusing namespace std;\n\n\nnamespace{\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef vector<int> vint;\ntypedef vector<vector<int> > vvint;\ntypedef vector<long long> vll, vLL;\ntypedef vector<vector<long long> > vvll, vvLL;\ntypedef pair<pii,int> P;\n\n#define VV(T) vector<vector< T > >\n\ntemplate <class T>\nvoid initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){\n v.assign(a, vector<T>(b, t));\n}\n\ntemplate <class F, class T>\nvoid convert(const F &f, T &t){\n stringstream ss;\n ss << f;\n ss >> t;\n}\n\n\n#define reep(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) reep((i),0,(n))\n#define ALL(v) (v).begin(),(v).end()\n#define PB push_back\n#define F first\n#define S second\n#define mkp make_pair\n#define RALL(v) (v).rbegin(),(v).rend()\n\n\n#define MOD 1000000007LL\n#define EPS 1e-8\nstatic const int INF=1<<24;\n/*\n10^3*50*100*\n*/\nint vis[55][11][(1<<7)+10];\nvoid mainmain(){\n\tint n,m,L,K;\n\twhile(cin>>n>>m>>L>>K,n){\n\t\tvint ball(n,-1);\n\t\tVV(pii) vv(n);\n\t\trep(i,m){\n\t\t\tint a,b,c;\n\t\t\tcin>>a>>b>>c;\n\t\t\tvv[a-1].PB(mkp(b-1,c));\n\t\t\tvv[b-1].PB(mkp(a-1,c));\n\t\t}\n\t\tint s,g;\n\t\tcin>>s>>g;\n\t\ts--;\n\t\tg--;\n\t\tint cnt=0;\n\t\trep(i,L){\n\t\t\tint t;\n\t\t\tcin>>t;\n\t\t\tball[t-1]=cnt++;\n\t\t}\n\t\trep(i,55){\n\t\t\trep(j,11){\n\t\t\t\trep(k,(1<<7)+10){\n\t\t\t\t\tvis[i][j][k]=INF;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpriority_queue<P> pq;\n\t\tpq.push(mkp(mkp(0,0),s));\n\t\twhile(!pq.empty()){\n\t\t\tP top=pq.top();\n\t\t\tpq.pop();\n\t\t\ttop.F.S*=-1;\n\t\t\t// cout<<-top.F.F<<\" \"<<top.F.S<<\" \"<<top.S+1<<endl;\n\t\t\tif(ball[top.S]!=-1) top.F.S|=1<<ball[top.S];\n\t\t\tbool f=false;\n\t\t\tint t=-top.F.F;\n\t\t\trep(i,K){\n\t\t\t\tif(t<vis[top.S][i][top.F.S]){\n\t\t\t\t\t// cout<<\"t \"<<t<<endl;\n\t\t\t\t\tswap(vis[top.S][i][top.F.S],t);\n\t\t\t\t\tf=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!f) continue;\n\t\t\trep(i,vv[top.S].size()){\n\t\t\t\tP tmp=top;\n\t\t\t\tif(ball[vv[top.S][i].F]!=-1) tmp.F.S|=1<<ball[vv[top.S][i].F];\n\t\t\t\tif(vis[vv[top.S][i].F][K-1][tmp.F.S]<vv[top.S][i].S-tmp.F.F) continue;\n\t\t\t\ttmp.F.S*=-1;\n\t\t\t\ttmp.F.F-=vv[top.S][i].S;\n\t\t\t\ttmp.S=vv[top.S][i].F;\n\t\t\t\tpq.push(tmp);\n\t\t\t}\n\t\t}\n\t\tif(vis[g][K-1][(1<<L)-1]!=INF) cout<<vis[g][K-1][(1<<L)-1]<<endl;\n\t\telse cout<<\"NA\"<<endl;\n\t}\n}\n\n\n\n}\nmain() try{\n mainmain();\n}\ncatch(...){\n}", "accuracy": 1, "time_ms": 1920, "memory_kb": 40084, "score_of_the_acc": -1.1349, "final_rank": 13 }, { "submission_id": "aoj_1513_1144017", "code_snippet": "#include <bits/stdc++.h>\n\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n\nusing namespace std;\n\ntypedef int Weight;\nWeight INF = 1000000000;\nstruct Edge{\n int src, dest; Weight weight;\n bool operator < (const Edge &rhs) const {return weight > rhs.weight;}\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\n\nvoid add_edge(Graph &g, int src, int dest, Weight weight) {\n g[src].push_back((Edge){src, dest, weight});\n}\n\n// Dijkstra (Verified: AOJ2005)\nvoid dijkstra(Graph &g, Matrix &d, int s) {\n REP(i,g.size()) d[i].assign(10, INF);\n d[s][0] = 0;\n typedef pair<Weight,int> P;\n priority_queue<P, vector<P>, greater<P> > que;\n que.push(P(0, s));\n while (!que.empty()) {\n Weight dist = que.top().first;\n int v = que.top().second;\n que.pop();\n auto itr=find(begin(d[v]),end(d[v]),dist);\n if (itr == end(d[v])) continue;\n REP(i, g[v].size()) {\n Edge e = g[v][i];\n REP(j,10) {\n if (d[e.dest][j] > dist + e.weight) {\n d[e.dest].insert(begin(d[e.dest])+j, dist + e.weight);\n d[e.dest].pop_back();\n que.push(P(d[e.dest][j], e.dest));\n break;\n }\n }\n }\n }\n}\n\nint main() {\n while(1){\n int n,m,l,k;\n cin>>n>>m>>l>>k;\n if(!n)break;\n --k;\n int z=1<<l;\n Graph g(n*z);\n vector<tuple<int,int,int>> es;\n REP(i,m){\n int u,v,c;\n cin>>u>>v>>c;\n --u;--v;\n es.emplace_back(u,v,c);\n }\n int s,t;\n cin>>s>>t;\n --s;--t;\n vector<int> db(l);\n REP(i,l)cin>>db[i];\n REP(i,l)--db[i];\n REP(i,m) {\n int u,v,c;\n tie(u,v,c)=es[i];\n REP(j,z){\n bitset<7> bs(j);\n bool tku = false;\n bool tkv = false;\n REP(p,l){\n if (!bs[p]) {\n if (db[p]==u) {\n add_edge(g,v*z+j,u*z+j+(1<<p),c);\n tku = true;\n }\n if(db[p]==v) {\n add_edge(g,u*z+j,v*z+j+(1<<p),c);\n tkv = true;\n }\n }\n }\n if(!tku) add_edge(g,v*z+j,u*z+j,c);\n if(!tkv) add_edge(g,u*z+j,v*z+j,c);\n }\n }\n Matrix dist(n*z);\n REP(i,dist.size()) dist[i]=Array(10);\n auto itr = find(begin(db),end(db),s);\n if (itr == end(db))\n dijkstra(g,dist,s*z);\n else\n dijkstra(g,dist,s*z+(1<<(itr-begin(db))));\n int ti=t*z+z-1;\n if(dist[ti][k] < INF)\n cout<<dist[ti][k]<<endl;\n else\n cout<<\"NA\"<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 8620, "score_of_the_acc": -0.3139, "final_rank": 6 }, { "submission_id": "aoj_1513_1143908", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i,n) for(int i=0; i<(int)(n); ++i)\nusing namespace std;\ntypedef long long LL;\nstruct Edge{\n int dst, cost;\n};\ntypedef vector<Edge> Node;\ntypedef vector<Node> Graph;\ntypedef tuple<int, int, int> P;\n\nint mask(int cur, vector<int> B) {\n int s = 0;\n REP(i, B.size()) if(B[i] == cur + 1) s |= (1 << i);\n return s;\n}\n\nint main(){\n int N, M, L, K;\n while(cin >> N >> M >> L >> K && N > 0) {\n Graph G(N);\n\n REP(i, M) {\n int u, v, c;\n cin >> u >> v >> c;\n u--; v--;\n G[u].push_back({v, c});\n G[v].push_back({u, c});\n }\n\n int start, goal;\n cin >> start >> goal;\n start--; goal--;\n\n vector<int> B(L);\n REP(i, L) cin >> B[i];\n\n vector<int> dist[55][1 << 8];\n priority_queue<P, vector<P>, greater<P>> que;\n que.push(P(0, start, mask(start, B)));\n while(!que.empty()) {\n auto p = que.top(); que.pop();\n int d, u, s;\n tie(d, u, s) = p;\n if(dist[u][s].size() >= K) continue;\n dist[u][s].push_back(d);\n for(auto e : G[u]) {\n int nu = e.dst;\n int nd = d + e.cost;\n int ns = s | mask(nu, B);\n if(dist[nu][ns].size() < K) {\n que.push(P(nd, nu, ns));\n }\n }\n }\n if(dist[goal][(1 << L) - 1].size() == K) {\n cout << dist[goal][(1 << L) - 1][K-1] << endl;\n } else {\n assert(dist[goal][(1 << L) - 1].size() == 0);\n cout << \"NA\" << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2410, "memory_kb": 41304, "score_of_the_acc": -1.3168, "final_rank": 16 }, { "submission_id": "aoj_1513_1141716", "code_snippet": "#include <iostream>\n#include <complex>\n#include <sstream>\n#include <string>\n#include <algorithm>\n#include <deque>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <vector>\n#include <set>\n#include <limits>\n#include <cstdio>\n#include <cctype>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <ctime>\nusing namespace std;\n#define REP(i, j) for(int i = 0; i < (int)(j); ++i)\n#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)\n#define SORT(v) sort((v).begin(), (v).end())\n#define REVERSE(v) reverse((v).begin(), (v).end())\ntypedef pair<int, int> P;\nconst int MAX_V = 51;\nconst int MAX_L = (1 << 10);\nconst int MAX_K = 15;\nconst int INF = 1e9;\n#define DEBUG\n\nclass C{\n public:\n int n, t, b;\n C(){}\n C( int _n, int _t ){ n = _n; t = _t; b = 0; }\n C( int _n, int _t, int _b){ n = _n; t = _t; b = _b; }\n bool operator > (const C &c) const { return t > c.t; }\n};\n\nint G[MAX_V][MAX_V], S, T, V, E, L, K;\nbool B[MAX_V];\nint closed[MAX_V][MAX_L][MAX_K], cnt[MAX_V][MAX_L];;\nmap<int, int> bn;\n\nint solve(){\n priority_queue<C, vector<C>, greater<C>> open;\n open.push(C(S, 0));\n REP(i, MAX_V) REP(j, MAX_L) REP(k, MAX_K) closed[i][j][k] = INF;\n REP(i, MAX_V) REP(j, MAX_L) cnt[i][j] = 0;\n while(!open.empty()){\n C now = open.top(); open.pop();\n int n = now.n, b = now.b, t = now.t;\n if(B[n]) b = (b | (1 << bn[n]));\n if(cnt[n][b] >= K || closed[n][b][cnt[n][b]] < t) continue;\n closed[n][b][cnt[n][b]++] = t;\n REP(next, V){\n if(G[n][next] == INF) continue;\n open.push(C(next, t + G[n][next], b));\n }\n }\n return (closed[T][(1 << L) - 1][K - 1] == INF ? -1 : closed[T][(1 << L) - 1][K - 1]);\n}\n\nint main() {\n while(cin >>V >>E >>L >>K && V){\n REP(i, MAX_V) REP(j, MAX_V) G[i][j] = INF;\n memset(B, 0, sizeof(B));\n REP(i, E){\n int u, v, c; cin >>u >>v >>c;\n --u; --v;\n G[u][v] = G[v][u] = c;\n }\n cin >>S >>T;\n --S; --T;\n REP(i, L){\n int n; cin >>n;\n --n;\n B[n] = true;\n bn[n] = i;\n }\n int ans = solve();\n if(ans == -1) cout <<\"NA\" <<endl;\n else cout <<ans <<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2680, "memory_kb": 79408, "score_of_the_acc": -1.9017, "final_rank": 19 }, { "submission_id": "aoj_1513_1132084", "code_snippet": "#include <string> \n#include <algorithm> \n#include <cfloat> \n#include <climits> \n#include <cmath> \n#include <complex> \n#include <cstdio> \n#include <cstdlib> \n#include <cstring> \n#include <functional> \n#include <iostream> \n#include <map> \n#include <memory> \n#include <queue> \n#include <set> \n#include <sstream> \n#include <stack> \n#include <utility> \n#include <vector> \n\n#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define ALL(x) (x).begin(),(x).end() \nusing namespace std; \ntypedef long long ll;\nconst double eps = 1e-10;\n\ntypedef int Weight;\nconst Weight INF = 100000000;\nstruct Edge{\n int from, to;\n Weight weight;\n int rev; // ネットワークフロー時の逆辺\n Edge(int from, int to, Weight weight) :\n from(from), to(to), weight(weight) { }\n Edge(int from, int to, Weight weight, int rev) :\n from(from), to(to), weight(weight), rev(rev){ }\n};\nbool operator < (const Edge &a, const Edge &b){\n if(a.weight != b.weight) return a.weight > b.weight;\n if(a.from != b.from) return a.from > b.from;\n return a.to > b.to;\n}\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\n\nvoid addFlowEdge(Graph &g, int a, int b, Weight c){\n g[a].push_back(Edge(a, b, c, g[b].size()));\n g[b].push_back(Edge(b, a, 0, g[a].size() - 1));\n}\nvoid addUndirectedEdge(Graph &g, int a, int b, Weight c){\n g[a].push_back(Edge(a, b, c, g[b].size()));\n g[b].push_back(Edge(b, a, c, g[a].size() - 1));\n}\n\nint N, M, L, K;\nint S, G;\nint B[10];\nint ball[60];\nint dist[60][1<<7][10];\n\nvoid dijkstra(const Graph &g, int s){\n int n = g.size();\n priority_queue<pair<Edge, int> > Q; // a < b <-> a.weight > b.weight\n for(int i = 1; i <= N; ++i)for(int j = 0; j < 1 << L; ++j)\n for(int k = 0; k < K; ++k) dist[i][j][k] = INF;\n\n int bb = ball[s] >= 0 ? 1 << ball[s]: 0;\n Q.push(make_pair(Edge(-2, s, 0), bb));\n dist[S][bb][0] = 0;\n while(!Q.empty()){\n Edge e = Q.top().first;\n int bit = Q.top().second;\n Q.pop();\n EACH(i, g[e.to]){\n int nb = bit, nw = i->weight+e.weight;\n if(ball[i->to] >= 0) nb |= 1 << ball[i->to];\n if(dist[i->to][nb][K-1] > nw){\n dist[i->to][nb][K-1] = nw;\n for(int j = K-1; j > 0; --j) if(dist[i->to][nb][j] < dist[i->to][nb][j-1])\n swap(dist[i->to][nb][j], dist[i->to][nb][j-1]);\n Q.push(make_pair(Edge(i->from, i->to, nw), nb));\n }\n }\n }\n}\n\nint main(){\n for(;;){\n cin >> N >> M >> L >> K;\n if(N==0) return 0;\n Graph g(N+1);\n for(int i = 0; i < M; ++i){\n int a, b, c;\n cin >> a >> b >> c;\n addUndirectedEdge(g, a, b, c);\n }\n cin >> S >> G;\n for(int i = 0; i < L; ++i) cin >> B[i];\n sort(B, B+L);\n memset(ball, -1, sizeof(ball));\n for(int i = 0; i < L; ++i) ball[B[i]] = i;\n dijkstra(g, S);\n if(dist[G][(1<<L)-1][K-1] == INF) cout << \"NA\" << endl;\n else cout << dist[G][(1<<L)-1][K-1] << endl;\n }\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 6388, "score_of_the_acc": -0.2138, "final_rank": 5 }, { "submission_id": "aoj_1513_979793", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n#include <iterator>\n#include <bitset>\n#include <random>\n#include <assert.h>\n#include <unordered_map>\n#include <array>\n#include <ctime>\n\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define X first\n#define Y second\n#define MP make_pair\n#define umap unordered_map\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair<int, int> P;\ntypedef unsigned int uint;\n\nconst int N = 55;\n\nint d[N][N];\nvector<P> G[N];\nint ball[N];\n\npriority_queue<int> cand[N][1 << 7];\n\nstruct state{\n\tint pos, visit, total;\n\tbool operator < (const state &r) const{\n\t\treturn total > r.total;\n\t}\n};\n\nint main(){\n\tios::sync_with_stdio(false);\n\n\tint n, m, l, k;\n\twhile (cin >> n >> m >> l >> k, n | m | l | k){\n\t\trep(i, N) rep(j, N) d[i][j] = 1 << 29;\n\t\trep(i, N) rep(j, 1 << 7) while (!cand[i][j].empty()) cand[i][j].pop();\n\t\trep(i, N) G[i].clear();\n\t\tMEMSET(ball, 0);\n\t\trep(i, m){\n\t\t\tint u, v, c;\n\t\t\tcin >> u >> v >> c;\n\t\t\t--u, --v;\n\t\t\td[u][v] = d[v][u] = c;\n\t\t\tG[u].push_back(MP(v, c));\n\t\t\tG[v].push_back(MP(u, c));\n\t\t}\n\t\tint s, g;\n\t\tcin >> s >> g;\n\t\t--s, --g;\n\t\trep(i, l){\n\t\t\tint b;\n\t\t\tcin >> b;\n\t\t\tball[--b] = 1 << i;\n\t\t}\n\t\trep(i, n) rep(j, n) rep(k, n) d[j][k] = min(d[j][k], d[j][i] + d[i][k]);\n\n\t\tpriority_queue<state> q;\n\t\tq.push({s, ball[s], 0});\n\n\t\tint cnt = 0, ans = -1;\n\t\tif (d[s][g] >= 1 << 29) goto fail;\n\t\twhile (!q.empty()) {\n\t\t\tauto s = q.top();\n\t\t\tq.pop();\n\n\t\t\tauto &pq = cand[s.pos][s.visit];\n\t\t\tif (!pq.empty() && pq.top() < s.total) continue;\n\n\t\t\tif (s.pos == g && s.visit + 1 == (1 << l)){\n\t\t\t\t//cout << s.total << endl;\n\t\t\t\tif (++cnt == k){\n\t\t\t\t\tans = s.total;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (auto e : G[s.pos]){\n\t\t\t\tstate nxt = s;\n\t\t\t\tnxt.pos = e.first;\n\t\t\t\tnxt.total += e.second;\n\t\t\t\tnxt.visit |= ball[nxt.pos];\n\t\t\t\tauto &c = cand[nxt.pos][nxt.visit];\n\t\t\t\tif (c.size() < k || nxt.total < c.top()){\n\t\t\t\t\tq.push(nxt);\n\t\t\t\t\tc.push(nxt.total);\n\t\t\t\t\tif (c.size() > k) c.pop();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\tfail:;\n\n\t\tif(ans >= 0) cout << ans << endl;\n\t\telse cout << \"NA\" << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 5044, "score_of_the_acc": -0.0913, "final_rank": 2 }, { "submission_id": "aoj_1513_979745", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n#include <iterator>\n#include <bitset>\n#include <random>\n#include <assert.h>\n#include <unordered_map>\n#include <array>\n#include <ctime>\n\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define X first\n#define Y second\n#define MP make_pair\n#define umap unordered_map\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair<int, int> P;\ntypedef unsigned int uint;\n\nconst int N = 55;\n\nint d[N][N];\nvector<P> G[N];\nint ball[N];\nint bpos[N];\n\npriority_queue<int> cand[N][1 << 7];\n\nstruct state{\n\tint pos, visit, total;\n\tbool operator < (const state &r) const{\n\t\treturn total > r.total;\n\t}\n};\n\nint main(){\n\tios::sync_with_stdio(false);\n\n\tint n, m, l, k;\n\twhile (cin >> n >> m >> l >> k, n | m | l | k){\n\t\trep(i, N) rep(j, N) d[i][j] = 1 << 29;\n\t\trep(i, N) rep(j, 1 << 7) while (!cand[i][j].empty()) cand[i][j].pop();\n\t\trep(i, N) G[i].clear();\n\t\tMEMSET(ball, 0);\n\t\tMEMSET(bpos, 0);\n\t\trep(i, m){\n\t\t\tint u, v, c;\n\t\t\tcin >> u >> v >> c;\n\t\t\t--u, --v;\n\t\t\td[u][v] = d[v][u] = c;\n\t\t\tG[u].push_back(MP(v, c));\n\t\t\tG[v].push_back(MP(u, c));\n\t\t}\n\t\tint s, g;\n\t\tcin >> s >> g;\n\t\t--s, --g;\n\t\trep(i, l){\n\t\t\tint b;\n\t\t\tcin >> b;\n\t\t\tball[--b] = 1 << i;\n\t\t\tbpos[i] = b;\n\t\t}\n\t\trep(i, n) rep(j, n) rep(k, n) d[j][k] = min(d[j][k], d[j][i] + d[i][k]);\n\n\t\tpriority_queue<state> q;\n\t\tq.push({s, 0, 0});\n\n\t\tint cnt = 0, ans = -1;\n\t\tif (d[s][g] >= 1 << 29) goto fail;\n\t\twhile (!q.empty()) {\n\t\t\tauto s = q.top();\n\t\t\tq.pop();\n\n\t\t\ts.visit |= ball[s.pos];\n\t\t\tauto &pq = cand[s.pos][s.visit];\n\t\t\tif (pq.size() && pq.top() < s.total) continue;\n\n\t\t\tif (s.pos == g && s.visit + 1 == (1 << l)){\n\t\t\t\t//cout << s.total << endl;\n\t\t\t\tif (++cnt == k){\n\t\t\t\t\tans = s.total;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (auto e : G[s.pos]){\n\t\t\t\tstate nxt = s;\n\t\t\t\tnxt.pos = e.first;\n\t\t\t\tnxt.total += e.second;\n\t\t\t\tauto &c = cand[nxt.pos][nxt.visit];\n\t\t\t\tif (c.size() < k || nxt.total < c.top()){\n\t\t\t\t\tq.push(nxt);\n\t\t\t\t\tc.push(nxt.total);\n\t\t\t\t\tif (c.size() > k) c.pop();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\tfail:;\n\t\tif(ans >= 0) cout << ans << endl;\n\t\telse cout << \"NA\" << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 0.07142857142857142, "time_ms": 20, "memory_kb": 2180, "score_of_the_acc": 0, "final_rank": 20 }, { "submission_id": "aoj_1513_849574", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <queue>\nusing namespace std;\n\ntypedef pair<int,int> pii;\n\nint solve(int n, int m, int L, int k){\n\tint mask = (1 << L) - 1;\n\tvector<vector<pii> > G(n + 1);\n\tvector<int> bf(n + 1);\n\tint start, goal, u, v, c, b;\n\tfor(int i = 0; i < m; ++i){\n\t\tscanf(\"%d%d%d\", &u, &v, &c);\n\t\tG[u].push_back(pii(c, v));\n\t\tG[v].push_back(pii(c, u));\n\t}\n\n\tscanf(\"%d%d\", &start, &goal);\n\tG[0].push_back(pii(0, start));\n\tgoal = goal << L | mask;\n\n\tfor(int i = 0; i < L; ++i){\n\t\tscanf(\"%d\", &b);\n\t\tbf[b] |= 1 << i;\n\t}\n\n\tpriority_queue<pii> pq;\n\tpq.push(pii(0, 0));\n\tvector<int> use((n + 1) << L);\n\twhile(!pq.empty()){\n\t\tint d = pq.top().first;\n\t\tint st = pq.top().second;\n\t\tpq.pop();\n\t\tif(++use[st] > k){ continue; }\n\t\tif(use[goal] == k){\n\t\t\treturn -d;\n\t\t}\n\t\t\n\t\tint p = st >> L;\n\t\tint b = st & mask;\n\t\t\n\t\tfor(size_t i = 0; i < G[p].size(); ++i){\n\t\t\tint np = G[p][i].second;\n\t\t\tint nd = d - G[p][i].first;\n\t\t\tint nb = b | bf[np];\n\t\t\tint nst = np << L | nb;\n\t\t\tif(use[nst] < k){\n\t\t\t\tpq.push(pii(nd, nst));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn -1;\n}\n\nint main(){\n\tint n, m, L, k;\n\twhile(scanf(\"%d%d%d%d\", &n, &m, &L, &k), n){\n\t\tint ans = solve(n, m, L, k);\n\t\tprintf(ans < 0 ? \"NA\\n\" : \"%d\\n\", ans);\n\t}\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 27780, "score_of_the_acc": -0.5044, "final_rank": 7 }, { "submission_id": "aoj_1513_849444", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<stack>\nusing namespace std;\n\nstruct E{\n int to,cost;\n};\n\nint N,M,L,K;\nint s,g;\nvector<vector<E>> edge;\nvector<int> B;\nbool input(){\n edge.clear();\n B.clear();\n cin>>N>>M>>L>>K;\n if(!(N||M||L||K))return false;\n edge.resize(N+1);\n for(int i=0;i<M;i++){\n int a,b,c;\n cin>>a>>b>>c;\n edge[a].push_back(E{b,c});\n edge[b].push_back(E{a,c});\n }\n cin>>s>>g;\n for(int i=0;i<L;i++){\n int tmp;\n cin>>tmp;\n B.push_back(tmp);\n }\n return true;\n}\n\nstruct S{\n int pos,cost,b;\n}; \nbool operator<(S a,S b){\n return a.cost>b.cost;\n}\n\nvector<vector<int>> dp;\nint solve(){\n priority_queue<S> que;\n que.push(S{s,0,0});\n \n dp.clear();\n dp.resize(N+1);\n for(int i=0;i<dp.size();i++){\n dp[i].resize(1<<L+1);\n }\n \n while(!que.empty()){\n S now = que.top();que.pop();\n for(int i=0;i<B.size();i++){\n if(now.pos == B[i]){\n now.b = now.b | (1<<i);\n }\n }\n if(!(dp[now.pos][now.b]<K+1))continue;\n dp[now.pos][now.b]++;\n if(now.pos == g && now.b==((1<<L)-1) ){\n if(dp[now.pos][now.b] == K){\n return now.cost;\n }\n }\n\n for(int i=0;i<edge[now.pos].size();i++){\n que.push(S{edge[now.pos][i].to ,now.cost + edge[now.pos][i].cost, now.b});\n }\n }\n return -1;\n}\n\nint main(){\n while(input()){\n int ans = solve();\n if(ans<0){\n cout<<\"NA\"<<endl;\n }else {\n cout<<ans<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 1010, "memory_kb": 76996, "score_of_the_acc": -1.3044, "final_rank": 15 }, { "submission_id": "aoj_1513_849435", "code_snippet": "#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <queue>\n#include <utility>\n#include <functional>\nusing namespace std;\n\ntypedef pair<int,int> pii;\n\nint solve(int n, int m, int L, int k){\n\tint u, v, c, start, goal, b;\n\tvector<vector<pii> > G(n + 2);\n\tfor(int i = 0; i < m; ++i){\n\t\tscanf(\"%d%d%d\", &u, &v, &c);\n\t\tG[u].push_back(pii(c, v));\n\t\tG[v].push_back(pii(c, u));\n\t}\n\tscanf(\"%d%d\", &start, &goal);\n\tG[0].push_back(pii(0, start));\n\tG[goal].push_back(pii(0, n + 1));\n\n\tvector<int> bnum(n + 2, -1);\n\tvector<int> use((n + 2) << L, 0);\n\tfor(int i = 0; i < L; ++i){\n\t\tscanf(\"%d\", &b);\n\t\tbnum[b] = i;\n\t}\n\n\tgoal = ((n + 2) << L) - 1;\n\n\n\tpriority_queue<pii,vector<pii>,greater<pii> > pq;\n\tvector<vector<int> > ds((n + 2) << L);\n\tpq.push(pii(0, 0));\n\tds[0].push_back(0);\n\twhile(!pq.empty()){\n\t\tpii p = pq.top();\n\t\tpq.pop();\n\t\tint d = p.first;\n\t\tint st = p.second;\n\t\tint now = st >> L;\n\t\tb = st & ((1 << L) - 1);\n\n\t\tif(use[st] >= ds[st].size() || ds[st][use[st]] != d){\n\t\t\tcontinue;\n\t\t}\n\t\t++use[st];\n\n\t\tfor(size_t i = 0; i < G[now].size(); ++i){\n\t\t\tint next = G[now][i].second;\n\t\t\tint nd = d + G[now][i].first;\n\t\t\tint nb = b;\n\t\t\tif(bnum[next] != -1){\n\t\t\t\tnb |= 1 << bnum[next];\n\t\t\t}\n\n\t\t\tint nst = next << L | nb;\n\t\t\tint ord = upper_bound(ds[nst].begin(), ds[nst].end(), nd)\n\t\t\t\t- ds[nst].begin();\n\n\t\t\tds[nst].push_back(0);\n\t\t\tfor(int j = ds[nst].size() - 1; j > ord; --j){\n\t\t\t\tds[nst][j] = ds[nst][j - 1];\n\t\t\t}\n\t\t\tds[nst][ord] = nd;\n\t\t\tif(ds[nst].size() > k){\n\t\t\t\tds[nst].pop_back();\n\t\t\t}\n\n\t\t\tif(ord != k){\n\t\t\t\tpq.push(pii(nd, nst));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ds[goal].size() == k ? ds[goal].back() : -1;\n}\n\nint main(){\n\tint n, m, L, k;\n\twhile(scanf(\"%d%d%d%d\", &n, &m, &L, &k), n){\n\t\tint ans = solve(n, m, L, k);\n\t\tprintf(ans < 0 ? \"NA\\n\" : \"%d\\n\", ans);\n\t}\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 3108, "score_of_the_acc": -0.1612, "final_rank": 3 }, { "submission_id": "aoj_1513_810216", "code_snippet": "#include<stdio.h>\n#include<iostream>\n#include<vector>\n#include<queue>\nusing namespace std;\n#define rep(i, e) for ( int i = 0; i < (int)e; i++ )\n#define MAX 50\n#define MAXK 10\n#define MAXL 7\n#define INFTY (1<<21)\n\nclass Edge{\n public:\n int v, cost;\n Edge(){}\n Edge( int v, int cost): v(v), cost(cost){}\n};\n\nclass State{\n public:\n\tint u, b, cost;\n State(){}\n State(int u, int b, int cost):u(u), b(b), cost(cost){}\n\n bool operator < ( const State &s) const{\n\tif ( cost == s.cost ) {\n\t if ( u == s.u ){\n\t\treturn b == s.b;\n\t } else {\n\t\treturn u > s.u;\n\t }\n\t} else {\n\t return cost > s.cost;\n\t}\n }\n};\n\nvector<Edge> G[MAX];\nint D[MAX][MAXK][1<<MAXL];\nbool V[MAX][MAXK][1<<MAXL];\nint B[MAX];\n\nint size, source, target, K, L;\n\nvoid kthDijkstra(){\n priority_queue<State> PQ;\n int LV[MAX][1<<L]; // level\n\n rep(i, size) rep(k, K) rep(l, (1<<L)){\n\tD[i][k][l] = INFTY;\n\tV[i][k][l] = false;\n }\n rep(i, size) rep(l, (1<<L)) LV[i][l] = 0;\n\n int bs = 0;\n if ( B[source] != -1 ) bs = (bs | (1<<B[source]));\n\n D[source][0][bs] = 0;\n V[source][0][bs] = true;\n PQ.push(State(source, bs, 0));\n\n State s;\n while(!PQ.empty()){\n\ts = PQ.top(); PQ.pop();\n\tif ( LV[s.u][s.b] >= K ) continue;\n\tV[s.u][LV[s.u][s.b]][s.b] = true;\n\tD[s.u][LV[s.u][s.b]++][s.b] = s.cost;\n\t\n\trep(i, G[s.u].size() ){\n\t int v = G[s.u][i].v;\n\t bs = s.b;\n\t if ( B[v] != -1 ) bs = (bs | (1<<B[v]));\n\t if ( LV[v][bs] <= K ){\n\t\tif ( !V[v][LV[v][bs]][bs] ){\n\t\t PQ.push(State(v, bs, s.cost + G[s.u][i].cost ));\n\t\t}\n\t }\n\t}\n }\n}\n\nvoid compute(){\n kthDijkstra();\n if ( D[target][K-1][(1<<L)-1] == INFTY ) printf(\"NA\\n\");\n else printf(\"%d\\n\", D[target][K-1][(1<<L)-1]);\n}\n\nbool input(){\n int m, s, t, c;\n scanf(\"%d %d %d %d\", &size, &m, &L, &K);\n if ( size == 0 && m == 0 && L == 0 && K == 0) return false;\n\n rep(i, size) B[i] = -1;\n rep(i, size) G[i].clear();\n rep(i, m){\n\tscanf(\"%d %d %d\", &s, &t, &c);\n\ts--; t--;\n\tG[s].push_back(Edge(t, c));\n\tG[t].push_back(Edge(s, c));\n }\n\n scanf(\"%d %d\", &source, &target);\n source--; target--;\n rep(i, L){\n\tint p;\n\tscanf(\"%d\", &p);\n\tB[p-1] = i;\n }\n return true;\n}\n\nint main(){\n while(input()) compute();\n return 0;\n}", "accuracy": 1, "time_ms": 2970, "memory_kb": 50236, "score_of_the_acc": -1.6223, "final_rank": 18 }, { "submission_id": "aoj_1513_791875", "code_snippet": "#include<algorithm>\n#include <cstring>\n#include <iostream>\n#include<cmath>\n#include<cstdio>\n#include<set>\n#include<map>\n#include<queue>\n#include<vector>\n#include<utility>\n#include<stack>\n#include <complex>\n#include <cassert>\nusing namespace std;\n\n#define REP(i,n) for (int i=0; i<(int)(n); ++i)\n#define FOR(i,c) for (__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(),(c).end();\ntypedef long long ll;\ntypedef pair<int,int> pii;\nconst int INF = 1<<29;\ntypedef int Weight;\nstruct Edge {\n int src,dst;\n Weight weight;\n Edge(int src, int dst, Weight weight) :\n src(src),dst(dst),weight(weight) {}\n};\nbool operator<(const Edge &e, const Edge &f) {\n return e.weight > f.weight;\n}\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\nWeight k_shortestPath(const Graph &g, int s, int t, int k) {\n const int n = g.size();\n Graph h(n);\n REP(u,n)FOR(e,g[u])\n h[e->dst].push_back(Edge(e->dst,e->src,e->weight));\n\n vector<Weight> d(n,INF); d[t] = 0;\n vector<int> p(n,-1);\n priority_queue<Edge> Q; Q.push(Edge(t,t,0));\n\n while(!Q.empty()) {\n Edge e = Q.top(); Q.pop();\n if (p[e.dst] >= 0)continue;\n p[e.dst] = e.src;\n FOR(f,h[e.dst]) if (d[f->dst] > e.weight + f->weight) {\n d[f->dst] = e.weight + f->weight;\n Q.push(Edge(f->src, f->dst, e.weight + f->weight));\n }\n }\n if (p[s] == -1) return -1;\n int l = 0;\n priority_queue<Edge> R; R.push(Edge(-1,s,0));\n while(!R.empty()) {\n Edge e = R.top(); R.pop();\n if (e.dst == t && ++l == k) return e.weight + d[s];\n FOR(f,g[e.dst])\n R.push(Edge(f->src,f->dst,e.weight + f->weight - d[f->src] + d[f->dst]));\n }\n return -1;\n}\n\nint n;\nint l;\n\nint NODE(int i, int S) {\n return i*(1<<l)+S;\n}\n\nint f[100];\n \nint main() {\n int m,k;\n while(cin>>n>>m>>l>>k,n||m||l||k) {\n Graph tg(n);\n Graph g(n*(1<<l));\n REP(i,m) {\n int a, b, c;\n cin >> a >> b >> c;\n a--;b--;\n tg[a].push_back(Edge(a,b,c));\n tg[b].push_back(Edge(b,a,c));\n }\n int s,t;\n cin >> s >> t;\n s--;t--;\n memset(f,-1,sizeof(f));\n REP(i,l) {\n int a;\n cin >> a; a--;\n f[a] = i;\n }\n REP(i,n) {\n FOR(it, tg[i]) {\n int a = i;\n int b = it->dst;\n int c = it->weight;\n REP(S,1<<l) {\n if (f[i]!=-1 && !(S>>f[i]&1)) continue;\n int A = NODE(a,S);\n int T = S;\n if (f[b]!=-1) T |= 1<<f[b];\n int B = NODE(b,T);\n g[A].push_back(Edge(A,B,c));\n }\n }\n }\n REP(i,n*(1<<l)) {\n FOR(it, g[i]) {\n // cout << i <<\" \" << it->dst << \" \" << it->weight << endl;\n }\n }\n int S = 0;\n if (f[s] != -1) S|=1<<f[s];\n \n int ans = k_shortestPath(g,NODE(s,S),NODE(t,(1<<l)-1),k);\n if (ans == -1) cout << \"NA\" << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 11584, "score_of_the_acc": -0.1726, "final_rank": 4 } ]
aoj_1520_cpp
Problem E: Light Source Problem 二次元平面上に n 個の円状の光源が配置されている。ある光源が光を受けると、下図のようにその光源の中心点から扇状に光が放出される。 光源の中心点は( x , y )、半径は r で表される。 光となる扇形は角 β を中心に- θ /2, + θ /2の方向へ対称に広がり、その半径は α である。 光源の円が扇型の光に完全に覆われるとき、光源はその光を受けたとみなされる。光源から放出される光の強さは、受けた光の強さの合計となる。ただし、放出される光の強さには上限があり、上限を超えた場合、光の強さは上限値と同じになる。なお、ある光や光源によって光が遮られることはない。 ある地点に存在する光源を目的光源とする。目的光源では光が放出されず、受ける光の強さに制限はない。 あなたは座標(0,0)から、ある強さの扇形の光を、任意の方向に1度だけ放出することができる。目的光源が受ける光の強さの合計の最大値を求めるプログラムを作成せよ。ただし、全ての光源において、放出された光が元の光源に戻るような入力データは与えられないと仮定してよい。また、この問題では、領域の境界からの距離が0.000001以内の場合、領域上とみなすことにする。 Input n θ 0 α 0 power x 1 y 1 r 1 θ 1 α 1 β 1 maxpower 1 . . x n y n r n θ n α n β n maxpower n x n+1 y n+1 r n+1 1行目には目的光源を除く光源の数 n が与えられる。 2行目に最初に光を放出する座標(0,0)の情報、3行目からn+2行目に目的でない光源の情報、n+3行目に目的光源の情報がそれぞれ空白区切りで与えられる。 各情報に対して、 x i , y i は光源の中心点の座標、 r i は光源の半径、 θ i は光が広がる角度、 α i は光の半径、 β i は光が向かう方向を表す。2行目の power は最初に放出される光の強さ、3行目からn+2行目の maxpower i は放出される光の上限を表す。 光の方向 β i はx軸の正の向きから反時計回りに回転し、各角度の単位は度とする。 Constraints 入力は以下の条件を満たす。 入力は全て整数からなる 1 ≤ n ≤ 100 -1000 ≤ x i , y i ≤ 1000 1 ≤ r i ≤ 1000 1 ≤ θ i ≤ 180 1 ≤ α i ≤ 1000 0 ≤ β i < 360 0 ≤ power , maxpower i ≤ 1000 Output 目的光源を全て囲うような光の強さの合計の最大値を1行に出力せよ。 Sample Input 1 1 90 10 20 3 3 1 90 10 315 10 6 0 1 Sample Output 1 30 Sample Input 2 2 90 10 10 -3 0 1 45 10 90 10 -6 0 2 30 3 180 10 -9 0 1 Sample Output 2 10
[ { "submission_id": "aoj_1520_2964704", "code_snippet": "#include <bits/stdc++.h>\n#define PI M_PI\n#define EPS 1e-10\nusing namespace std;\ntypedef long long ll;\ntypedef complex<double> P;\n\nstruct light {\n P pos;\n double r, t, a, b;\n ll p;\n};\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\ndouble distance(P a, P b) { return abs(a - b); }\n\nvector<vector<ll>> construct(const vector<light>& v) {\n vector<vector<ll>> graph(v.size());\n for (ll i = 0; i < v.size() - 1; i++) {\n for (ll j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n assert(-EPS < w && w < PI / 2 + EPS);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n return graph;\n}\n\nint main() {\n ll n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (ll i = 0; i < n; i++) {\n ll x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n\n {\n ll x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n\n ll answer = 0;\n for (ll i = 1; i < n + 2; i++) {\n for(ll z = -1; z < 2; z++) if(z != 0) {\n v[0].b = arg(v[i].pos) + (abs(asin(v[i].r / abs(v[i].pos))) - v[0].t / 2) * z;\n // assert(-EPS < asin(v[i].r / abs(v[i].pos)) && asin(v[i].r / abs(v[i].pos)) < PI / 2 + EPS);\n auto graph = construct(v);\n vector<ll> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<ll> dp(graph.size());\n dp[0] = v[0].p;\n stack<ll> s;\n for (ll i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n ll t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for(auto e : graph[t]) {\n dp[e] += dp[t];\n if(--k[e] == 0) {\n s.push(e);\n }\n }\n }\n answer = max(answer, dp[n + 1]);\n }\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 380, "memory_kb": 3728, "score_of_the_acc": -1.6404, "final_rank": 18 }, { "submission_id": "aoj_1520_2964701", "code_snippet": "#include <bits/stdc++.h>\n#define PI M_PI\n#define EPS 1e-10\nusing namespace std;\ntypedef long long ll;\ntypedef complex<double> P;\n\nstruct light {\n P pos;\n double r, t, a, b;\n ll p;\n};\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\ndouble distance(P a, P b) { return abs(a - b); }\n\nvector<vector<ll>> construct(const vector<light>& v) {\n vector<vector<ll>> graph(v.size());\n for (ll i = 0; i < v.size() - 1; i++) {\n for (ll j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n assert(-EPS < w && w < PI / 2 + EPS);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n return graph;\n}\n\nint main() {\n ll n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (ll i = 0; i < n; i++) {\n ll x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n\n {\n ll x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n\n ll answer = 0;\n for (ll i = 1; i < n + 2; i++) {\n for(ll z = -1; z < 2; z++) if(z != 0) {\n v[0].b = arg(v[i].pos) + (asin(v[i].r / abs(v[i].pos)) - v[0].t / 2) * z;\n // assert(-EPS < asin(v[i].r / abs(v[i].pos)) && asin(v[i].r / abs(v[i].pos)) < PI / 2 + EPS);\n auto graph = construct(v);\n vector<ll> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<ll> dp(graph.size());\n dp[0] = v[0].p;\n stack<ll> s;\n for (ll i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n ll t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for(auto e : graph[t]) {\n dp[e] += dp[t];\n if(--k[e] == 0) {\n s.push(e);\n }\n }\n }\n answer = max(answer, dp[n + 1]);\n }\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 390, "memory_kb": 3732, "score_of_the_acc": -1.7, "final_rank": 19 }, { "submission_id": "aoj_1520_2964253", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n\ntypedef complex<double> Point;\ntypedef pair<Point,Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6;\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n\n#define LE(n,m) ((n)-(m) < EPS)\n#define GE(n,m) (EPS > (m)-(n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\nconst double PI = acos(-1);\n\nnamespace std {\n bool operator<(const Point a, const Point b){\n return a.X != b.X ? a.X < b.X : a.Y < b.Y;\n }\n}\n\ndouble dot(Point a, Point b){\n return a.X*b.X + a.Y*b.Y;\n}\n\ndouble cross(Point a, Point b){\n return a.X*b.Y - a.Y*b.X;\n}\n\nint ccw(Point a, Point b ,Point c){\n b -= a; c -= a;\n if(cross(b,c) > EPS) return +1;\n if(cross(b,c) < -EPS) return -1;\n if(dot(b,c) < -EPS) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Point a1, Point a2, Point p){\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\nVP tangentPoints(Point a, double ar, Point p){\n VP ps;\n double sin = ar / abs(p-a);\n if( !LE(sin,1) ) return ps;\n double t = PI/2 - asin(min(sin, 1.0));\n ps.push_back( a+(p-a)*polar(sin,t) );\n if(!EQ(sin,1)) ps.push_back( a+(p-a)*polar(sin,-t) );\n return ps;\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r){\n VP ps;\n Point ft = proj(a1,a2,c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r-norm(ft-c),0.0)) / abs(a2-a1) * (a2-a1);\n ps.pb(ft+dir);\n if(!EQ(r*r,norm(ft-c))) ps.pb(ft-dir);\n return ps;\n}\n\nPoint READ_P(){\n int x,y;\n cin >>x >>y;\n return Point(x,y);\n}\n\ndouble ang_to_rad(double a){\n return a/360*2*PI;\n}\n\nint V;\nvector<vector<int>> gg;\n\nvector<int> tlist;\nvector<bool> vis;\nvoid dfs(int x){\n if(vis[x]) return;\n vis[x] = true;\n for(int e:gg[x]) dfs(e);\n tlist.pb(x);\n}\n\nbool covered(Point c, double a, double lt, double rt, Point to, double tor){\n Point v(to.X-c.X, to.Y-c.Y);\n\n double dir = acos(v.X / hypot(v.X,v.Y));\n if(v.Y<0) dir = dir + 2*(PI-dir);\n\n // 半径内に入っている\n if( LE(abs(v)+tor, a) ){\n // 角度チェック\n Point pl = c + polar(a,lt), pr = c + polar(a,rt);\n\n bool angle_ok = false;\n for(int i=-1; i<=1; ++i){\n angle_ok |= (LE(lt+i*2*PI,dir)&&LE(dir,rt+i*2*PI));\n }\n if( angle_ok ){\n for(Point tt:{pl,pr}){\n Point h = proj(c,tt,to);\n if(GE(abs(to-h), tor)) continue;\n\n for(Point cp:crosspointLC(c,tt,to,tor)){\n if(ccw(c,tt,cp) == 0) return false;\n }\n }\n return true;\n }\n }\n return false;\n}\n\nint main(){\n int n;\n cin >>n;\n\n V = n+2;\n\n double ts,as;\n int ps;\n cin >>ts >>as >>ps;\n ts = ang_to_rad(ts);\n\n VP c(n);\n vector<double> r(n),t(n),a(n),b(n);\n vector<int> p(n);\n rep(i,n){\n c[i] = READ_P();\n cin >>r[i] >>t[i] >>a[i] >>b[i] >>p[i];\n\n t[i] = ang_to_rad(t[i]);\n b[i] = ang_to_rad(b[i]);\n }\n\n Point goal;\n double gr;\n goal = READ_P();\n cin >>gr;\n\n int S = n, G = n+1;\n vector<vector<int>> g(n+2);\n\n rep(i,n)rep(j,n){\n if(i==j) continue;\n double lt = b[i]-t[i]/2, rt = b[i]+t[i]/2;\n if(covered(c[i],a[i],lt,rt,c[j],r[j])) g[i].pb(j);\n }\n\n // to goal\n rep(i,n){\n double lt = b[i]-t[i]/2, rt = b[i]+t[i]/2;\n if(covered(c[i],a[i],lt,rt,goal,gr)) g[i].pb(G);\n }\n\n Point ZERO(0,0);\n vector<double> cand;\n rep(i,n){\n if( LE(abs(c[i]-ZERO),0) ) continue;\n\n for(Point tp:tangentPoints(c[i],r[i],ZERO)){\n double dir = acos(tp.X / hypot(tp.X,tp.Y));\n if(tp.Y<0) dir = dir + 2*(PI-dir);\n\n for(double sub:vector<double>({0,-ts})){\n double add = dir+sub;\n if(add<0) add += 2*PI;\n cand.pb(add);\n }\n }\n }\n if( !LE(abs(goal-ZERO),0) ) {\n for(Point tp:tangentPoints(goal,gr,ZERO)){\n double dir = acos(tp.X / hypot(tp.X,tp.Y));\n if(tp.Y<0) dir = dir + 2*(PI-dir);\n\n for(double sub:vector<double>({0,-ts})){\n double add = dir+sub;\n if(add<0) add += 2*PI;\n cand.pb(add);\n }\n }\n }\n\n int ans = 0;\n for(double rad:cand){\n gg = g;\n\n double lt = rad, rt = rad+ts;\n rep(i,n){\n if(covered(ZERO,as,lt,rt,c[i],r[i])) gg[S].pb(i);\n }\n if(covered(ZERO,as,lt,rt,goal,gr)) gg[S].pb(G);\n\n\n tlist.clear();\n vis = vector<bool>(V,false);\n rep(i,V) dfs(i);\n\n reverse(all(tlist));\n vector<int> dp(V);\n dp[S] = ps;\n for(int i:tlist){\n if(i<n) dp[i] = min(dp[i], p[i]);\n for(int e:gg[i]) dp[e] += dp[i];\n }\n\n ans = max(ans, dp[G]);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3660, "score_of_the_acc": -0.1, "final_rank": 1 }, { "submission_id": "aoj_1520_2963824", "code_snippet": "#include <bits/stdc++.h>\n#define PI M_PI\n#define EPS 1e-10\nusing namespace std;\ntypedef long long ll;\ntypedef complex<double> P;\n\nstruct light {\n P pos;\n double r, t, a, b;\n ll p;\n};\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\ndouble distance(P a, P b) { return abs(a - b); }\n\nvector<vector<ll>> construct(const vector<light>& v) {\n vector<vector<ll>> graph(v.size());\n for (ll i = 0; i < v.size() - 1; i++) {\n for (ll j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n return graph;\n}\n\nint main() {\n ll n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (ll i = 0; i < n; i++) {\n ll x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n\n {\n ll x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n\n for (auto& e : v) {\n e.r -= EPS;\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n\n ll answer = 0;\n for (ll i = 1; i < n + 2; i++) {\n for(ll z = -1; z < 2; z++) if(z != 0) {\n v[0].b = arg(v[i].pos) + (-asin(v[i].r / abs(v[i].pos)) + v[0].t / 2) * z;\n auto graph = construct(v);\n vector<ll> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<ll> dp(graph.size());\n dp[0] = v[0].p;\n stack<ll> s;\n for (ll i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n ll t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n if (k[e]-- == 1) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 380, "memory_kb": 3768, "score_of_the_acc": -1.9737, "final_rank": 20 }, { "submission_id": "aoj_1520_2963811", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-12\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.r -= EPS;\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n for(int z = -1; z < 2; z++) if(z != 0) {\n v[0].b = arg(v[i].pos) + (-asin(v[i].r / abs(v[i].pos)) + v[0].t / 2) * z;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n if (k[e]-- == 1) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 380, "memory_kb": 3712, "score_of_the_acc": -1.507, "final_rank": 15 }, { "submission_id": "aoj_1520_2963810", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-12\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.r -= EPS;\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n for(int z = -1; z < 2; z++) if(z != 0) {\n v[0].b = arg(v[i].pos) + (-asin(v[i].r / abs(v[i].pos)) + v[0].t / 2) * z;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n if (k[e]-- == 1) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 380, "memory_kb": 3712, "score_of_the_acc": -1.507, "final_rank": 15 }, { "submission_id": "aoj_1520_2963795", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-6\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.r -= EPS;\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n if (k[e]-- == 1) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 190, "memory_kb": 3704, "score_of_the_acc": -0.9404, "final_rank": 5 }, { "submission_id": "aoj_1520_2963582", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-6\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.r -= EPS;\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 210, "memory_kb": 3716, "score_of_the_acc": -1.093, "final_rank": 12 }, { "submission_id": "aoj_1520_2963527", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-6\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2 - EPS);\n P o2 = polar(1.0, v[i].b + v[i].t / 2 + EPS);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 220, "memory_kb": 3688, "score_of_the_acc": -0.886, "final_rank": 3 }, { "submission_id": "aoj_1520_2963518", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-7\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 200, "memory_kb": 3712, "score_of_the_acc": -1.0333, "final_rank": 8 }, { "submission_id": "aoj_1520_2963515", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-12\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 190, "memory_kb": 3708, "score_of_the_acc": -0.9737, "final_rank": 6 }, { "submission_id": "aoj_1520_2963513", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-1\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 190, "memory_kb": 3716, "score_of_the_acc": -1.0404, "final_rank": 9 }, { "submission_id": "aoj_1520_2963511", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n\ntypedef complex<double> Point;\ntypedef pair<Point,Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-8;\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n\n#define LE(n,m) ((n)-(m) < EPS)\n#define GE(n,m) (EPS > (m)-(n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\nconst double PI = acos(-1);\n\nnamespace std {\n bool operator<(const Point a, const Point b){\n return a.X != b.X ? a.X < b.X : a.Y < b.Y;\n }\n}\n\ndouble dot(Point a, Point b){\n return a.X*b.X + a.Y*b.Y;\n}\n\ndouble cross(Point a, Point b){\n return a.X*b.Y - a.Y*b.X;\n}\n\nint ccw(Point a, Point b ,Point c){\n b -= a; c -= a;\n if(cross(b,c) > EPS) return +1;\n if(cross(b,c) < -EPS) return -1;\n if(dot(b,c) < -EPS) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\n\nPoint proj(Point a1, Point a2, Point p){\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\nVP tangentPoints(Point a, double ar, Point p){\n VP ps;\n double sin = ar / abs(p-a);\n if( !LE(sin,1) ) return ps;\n double t = PI/2 - asin(min(sin, 1.0));\n ps.push_back( a+(p-a)*polar(sin,t) );\n if(!EQ(sin,1)) ps.push_back( a+(p-a)*polar(sin,-t) );\n return ps;\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r){\n VP ps;\n Point ft = proj(a1,a2,c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r-norm(ft-c),0.0)) / abs(a2-a1) * (a2-a1);\n ps.pb(ft+dir);\n if(!EQ(r*r,norm(ft-c))) ps.pb(ft-dir);\n return ps;\n}\n\n\n\n\n\n\nPoint READ_P(){\n int x,y;\n cin >>x >>y;\n return Point(x,y);\n}\n\ndouble ang_to_rad(double a){\n return a/360*2*PI;\n}\n\nint V;\nvector<vector<int>> gg;\n\nvector<int> tlist;\nvector<bool> vis;\nvoid dfs(int x){\n vis[x] = true;\n for(int e:gg[x]){\n if(!vis[e]) dfs(e);\n }\n tlist.pb(x);\n}\n\n\nbool covered(Point c, double a, double lt, double rt, Point to, double tor){\n Point v(to.X-c.X, to.Y-c.Y);\n\n double dir = acos(v.X / hypot(v.X,v.Y));\n if(v.Y<0) dir = dir + 2*(PI-dir);\n // printf(\" dir %f\\n\", dir);\n\n // printf(\" %f ~ %f\\n\", lt-2*PI,rt-2*PI);\n\n // 半径内に入っている\n if( LE(abs(v)+tor, a) ){\n // 角度チェック\n Point pl = c + polar(a,lt), pr = c + polar(a,rt);\n if( (LE(lt,dir)&&LE(dir,rt)) || (LE(lt-2*PI,dir)&&LE(dir,rt-2*PI)) ){\n for(Point tt:{pl,pr}){\n\n Point h = proj(c,tt,to);\n if(EQ(abs(to-h), tor)) continue;\n\n for(Point cp:crosspointLC(c,tt,to,tor)){\n if(ccw(c,tt,cp) == 0) return false;\n }\n }\n return true;\n }\n }\n return false;\n}\n\nint main(){\n int n;\n cin >>n;\n\n V = n+2;\n\n double ts,as,ps;\n cin >>ts >>as >>ps;\n ts = ang_to_rad(ts);\n\n VP c(n);\n vector<double> r(n),t(n),a(n),b(n);\n vector<int> p(n);\n rep(i,n){\n c[i] = READ_P();\n cin >>r[i] >>t[i] >>a[i] >>b[i] >>p[i];\n\n t[i] = ang_to_rad(t[i]);\n // a[i] = ang_to_rad(a[i]);\n b[i] = ang_to_rad(b[i]);\n }\n\n Point goal;\n double gr;\n goal = READ_P();\n cin >>gr;\n\n int S = n, G = n+1;\n\n vector<vector<int>> g(n+2);\n\n rep(i,n)rep(j,n){\n if(i==j) continue;\n\n double lt = b[i]-t[i]/2, rt = b[i]+t[i]/2;\n if(covered(c[i],a[i],lt,rt,c[j],r[j])){\n // printf(\" ADD EDGE %d -> %d\\n\",i,j);\n g[i].pb(j);\n }\n }\n\n // to goal\n rep(i,n){\n double lt = b[i]-t[i]/2, rt = b[i]+t[i]/2;\n if(covered(c[i],a[i],lt,rt,goal,gr)){\n // printf(\" ADD EDGE %d -> G\\n\",i);\n g[i].pb(G);\n }\n }\n\n Point ZERO(0,0);\n vector<double> cand;\n rep(i,n){\n if( LE(abs(c[i]-ZERO),0) ) continue;\n\n for(Point tp:tangentPoints(c[i],r[i],ZERO)){\n double dir = acos(tp.X / hypot(tp.X,tp.Y));\n if(tp.Y<0) dir = dir + 2*(PI-dir);\n\n for(double sub:vector<double>({0,-ts})){\n double add = dir+sub;\n if(add<0) add += 2*PI;\n cand.pb(add);\n }\n }\n }\n if( !LE(abs(goal-ZERO),0) ) {\n for(Point tp:tangentPoints(goal,gr,ZERO)){\n double dir = acos(tp.X / hypot(tp.X,tp.Y));\n if(tp.Y<0) dir = dir + 2*(PI-dir);\n\n for(double sub:vector<double>({0,-ts})){\n double add = dir+sub;\n if(add<0) add += 2*PI;\n cand.pb(add);\n }\n }\n }\n\n int ans = 0;\n for(double rad:cand){\n // printf(\" --- %f\\n\",rad);\n\n gg = g;\n\n double lt = rad, rt = rad+ts;\n // printf(\" lt rt %f %f\\n\", lt, rt);\n rep(i,n){\n if(covered(ZERO,as,lt,rt,c[i],r[i])){\n // printf(\" ADD EDGE S -> %d\\n\",i);\n gg[S].pb(i);\n }\n }\n if(covered(ZERO,as,lt,rt,goal,gr)){\n // printf(\" ADD EDGE S -> G\\n\");\n gg[S].pb(G);\n }\n\n tlist.clear();\n vis = vector<bool>(V,false);\n rep(i,V) dfs(i);\n\n reverse(all(tlist));\n vector<int> dp(V);\n dp[S] = ps;\n for(int i:tlist){\n\n if(i<n) dp[i] = min(dp[i], p[i]);\n for(int e:gg[i]){\n dp[e] += dp[i];\n }\n }\n ans = max(ans, dp[G]);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 0.5, "time_ms": 10, "memory_kb": 3648, "score_of_the_acc": 0, "final_rank": 2 }, { "submission_id": "aoj_1520_2963508", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-6\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n if (d < v[j].r) continue;\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 190, "memory_kb": 3768, "score_of_the_acc": -1.4737, "final_rank": 14 }, { "submission_id": "aoj_1520_2963501", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-6\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n if (i == j) continue;\n double d = distance(v[i].pos, v[j].pos);\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 190, "memory_kb": 3720, "score_of_the_acc": -1.0737, "final_rank": 10 }, { "submission_id": "aoj_1520_2963496", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-3\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 190, "memory_kb": 3720, "score_of_the_acc": -1.0737, "final_rank": 10 }, { "submission_id": "aoj_1520_2963490", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-9\ntypedef long long ll;\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n ll answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<ll> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min((ll)v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 190, "memory_kb": 3708, "score_of_the_acc": -0.9737, "final_rank": 6 }, { "submission_id": "aoj_1520_2963475", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-9\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r - v[i].a <= EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 200, "memory_kb": 3720, "score_of_the_acc": -1.1, "final_rank": 13 }, { "submission_id": "aoj_1520_2963458", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-6\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a + EPS && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 210, "memory_kb": 3696, "score_of_the_acc": -0.9263, "final_rank": 4 }, { "submission_id": "aoj_1520_2963450", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define PI 3.14159265358979323846\n#define EPS 1e-6\n\ntypedef complex<double> P;\n\ndouble cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }\n\ndouble distance(P a, P b) { return abs(a - b); }\n\nstruct light {\n P pos;\n double r, t, a, b;\n int p;\n};\n\nvector<vector<int>> construct(const vector<light>& v) {\n vector<vector<int>> graph(v.size());\n for (int i = 0; i < v.size() - 1; i++) {\n for (int j = 1; j < v.size(); j++) {\n double d = distance(v[i].pos, v[j].pos);\n double w = asin(v[j].r / d);\n P i1 = polar(1.0, arg(v[j].pos - v[i].pos) - w);\n P i2 = polar(1.0, arg(v[j].pos - v[i].pos) + w);\n P o1 = polar(1.0, v[i].b - v[i].t / 2);\n P o2 = polar(1.0, v[i].b + v[i].t / 2);\n if (d + v[j].r <= v[i].a && cross(o1, i1) >= -EPS && cross(i2, o2) >= -EPS) {\n graph[i].push_back(j);\n }\n }\n }\n /*\n for (int i = 0; i < graph.size(); i++) {\n cerr << i << \": \";\n for (auto e : graph[i]) {\n cerr << e << \" \";\n }\n cerr << endl;\n }\n */\n return graph;\n}\n\nint main() {\n int n;\n cin >> n;\n vector<light> v(n + 2);\n cin >> v[0].t >> v[0].a >> v[0].p;\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y >> v[i + 1].r >> v[i + 1].t >> v[i + 1].a >> v[i + 1].b >> v[i + 1].p;\n v[i + 1].pos = P(x, y);\n }\n {\n int x, y;\n cin >> x >> y >> v[n + 1].r;\n v[n + 1].pos = P(x, y);\n v[n + 1].p = 1e9;\n }\n for (auto& e : v) {\n e.t = e.t / 180.0 * PI;\n e.b = e.b / 180.0 * PI;\n }\n /*\n for (auto e : v) {\n cerr << e.pos.real() << \" \" << e.pos.imag() << \" \" << e.r << \" \" << e.t << \" \" << e.a << \" \" << e.b << \" \" << e.p\n << endl;\n }\n */\n\n int answer = 0;\n for (int i = 1; i < n + 2; i++) {\n v[0].b = arg(v[i].pos) - asin(v[i].r / abs(v[i].pos)) + v[0].t / 2;\n // cerr << v[0].b / PI * 180 << endl;\n auto graph = construct(v);\n vector<int> k(graph.size());\n for (auto edges : graph) {\n for (auto e : edges) {\n k[e]++;\n }\n }\n\n vector<int> dp(graph.size());\n dp[0] = v[0].p;\n stack<int> s;\n for (int i = 0; i < k.size(); i++) {\n if (k[i] == 0) {\n s.push(i);\n }\n }\n while (!s.empty()) {\n int t = s.top();\n s.pop();\n dp[t] = min(v[t].p, dp[t]);\n for (auto e : graph[t]) {\n dp[e] += dp[t];\n k[e]--;\n if (k[e] == 0) {\n s.push(e);\n }\n }\n }\n /*\n for (auto e : dp) {\n cerr << e << \" \";\n }\n cerr << endl;\n */\n answer = max(answer, dp[n + 1]);\n }\n\n cout << answer << endl;\n}", "accuracy": 0.4444444444444444, "time_ms": 230, "memory_kb": 3768, "score_of_the_acc": -1.5789, "final_rank": 17 } ]
aoj_1518_cpp
Problem C: Last One Problem あなたは、集合好きな友人Aと一風変わったゲームをしてみる事にした。 n個の要素からなる重複が許される非負の整数の集合Sが与えられる。集合Sに含まれる各要素は非負の p i 進数である。集合Sに対して 以下のStep1~3を1つのターンとして、あなたと相手の二人が交互にターンを繰り返す。各ターンではこの番号の順に操作を行う。 Step 1: 集合Sに含まれる全ての要素を一度取り除き、その取り除いた各要素を2進数に変換して、0を消して1が1つ以上連続する部分に分割し、その分割された部分を全て集合Sに加える。 Step 2: このとき集合Sが空になっていたならば、現在このターンをプレイしているプレイヤーは負ける。 Step 3: 集合Sに含まれる要素aを1つ選び、そのaから 1 ≤ x ≤ a を満たす任意の整数xを引く。 下の図は次の入力が与えられたときの最初の1ターンの進行の仕方のうちの1つの例である。 4 10 3 2 11 16 5 16 AE 最初はあなたのターンだ。与えられた入力に対して両者が最善を尽くすとき、果たしてあなたに勝ち目はあるのだろうか。 Input 入力は次の形式で与えられる。 n p 1 m 1 p 2 m 2 ... p i m i ... p n m n 集合の要素である p i 進数 m i が n 個与えられる。集合の要素の数 n は10 5 を超えない。また、2≦ p i ≦62を満たす。 p i 進数 m i は通常の数字である0~9に加え、アルファベット52文字を使用する。 その順番は012...789ABC...XYZabc...xyzを昇順とし、 i 番目までの文字を使うものとする。また、 p i 進数 m i は10進数で2 18 -1を超えない. Output 与えられた入力に対しあなたが勝てるならwin、負けるならloseを出力せよ。 Sample Input 1 1 10 1 Sample Output 1 win Sample Input 2 2 10 1 10 1 Sample Output 2 lose Sample Input 3 3 25 53AI 43 3BI0 62 pn6 Sample Output 3 win
[ { "submission_id": "aoj_1518_5003973", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\nusing namespace std;\n\nint ctoi(char c){\n if('A' <= c && c <= 'Z'){\n return c - 'A' + 10;\n }else if('a' <= c && c <= 'z'){\n return c - 'a' + 36;\n }\n return (int)c - '0';\n}\n\nint conv(int p, string m){\n int ret = 0;\n for(auto c : m){\n ret *= p;\n ret += ctoi(c);\n }\n\n return ret;\n}\n\nint main(){\n int n;\n cin >> n;\n\n int grundy = 0;\n\n for(int i = 0 ; i < n ; ++i){\n int p;\n string m;\n cin >> p >> m;\n\n grundy ^= conv(p, m);\n }\n\n if(grundy == 0){\n cout << \"lose\" << endl;\n }else{\n cout << \"win\" << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3440, "score_of_the_acc": -0.3228, "final_rank": 3 }, { "submission_id": "aoj_1518_3094833", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, p, now = 0;\nstring m;\n\nvoid management();\n\nint main() {\n cin >> n;\n for(int i = 0; i < n; ++i) {\n cin >> p >> m;\n management();\n }\n if(now == 0)\n cout << \"lose\" << endl;\n else\n cout << \"win\" << endl;\n return 0;\n}\n\nvoid management() {\n int tennum = 0, cnt = 0;\n for(int i = 0; i < m.size(); ++i) {\n tennum *= p;\n if(m[i] >= '0' && m[i] <= '9')\n tennum += m[i] - '0';\n else if(m[i] >= 'A' && m[i] <= 'Z')\n tennum += m[i] - 'A' + 10;\n else\n tennum += m[i] - 'a' + 36;\n }\n for(int i = 0; (1 << i) <= tennum; ++i) {\n if(((1 << i) & tennum) == (1 << i))\n now ^= (1 << cnt++);\n else\n cnt = 0;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3192, "score_of_the_acc": -0.4629, "final_rank": 4 }, { "submission_id": "aoj_1518_2966824", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nint cv(char c){\n if(isdigit(c)) return c-'0';\n if(isupper(c)) return 10+c-'A';\n return 36+c-'a';\n}\n\nint READ(){\n int p;\n string m;\n cin >>p >>m;\n\n int ret = 0;\n for(char c:m) ret = ret*p + cv(c);\n return ret;\n}\n\nint main(){\n int n;\n cin >>n;\n\n int g = 0;\n rep(i,n) g ^= __builtin_popcount(READ());\n cout << (g==0?\"lose\":\"win\") << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3184, "score_of_the_acc": -0.5454, "final_rank": 8 }, { "submission_id": "aoj_1518_2963210", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define pb push_back\n#define all(x) begin(x),end(x)\n#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))\n#ifdef LOCAL\n#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\nvoid _dbg(string){cerr<<endl;}\ntemplate<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<\" = \"<<h<<\", \";_dbg(s.substr(l+1),t...);}\ntemplate<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n#else\n#define dbg(...) {}\n#endif\n\n#define long long long // for codeforces\n\nmap<char,int> conv;\n\nint main(){\n int cnt = 0;\n rep(i,10) conv['0' + i] = cnt++;\n rep(i,26) conv['A' + i] = cnt++;\n rep(i,26) conv['a' + i] = cnt++;\n\n vector<int> g(1<<19, 0);\n g[1] = 1;\n int mex = 2;\n rep(i,2,1<<19){\n g[i] = 0;\n int tmp = i;\n while(tmp > 0){\n while(tmp%2 == 0) tmp /= 2;\n int cur = 1;\n while(tmp%2==1){\n tmp /= 2;\n cur = cur*2 + 1;\n }\n cur /= 2;\n g[i] = g[i] ^ g[cur];\n if(i == cur) {\n g[i] = mex++;\n }\n }\n if(i<20) dbg(g[i]);\n }\n\n int ans = 0;\n int n;\n scanf(\"%d\", &n);\n rep(i,n){\n int p;\n char s[100];\n scanf(\"%d %s\", &p, s);\n int num = 0;\n for(int j = 0; s[j] != '\\0'; j++){\n num = num * p + conv[s[j]];\n }\n ans = ans ^ g[num];\n }\n\n cout << (ans == 0 ? \"lose\" : \"win\") << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4776, "score_of_the_acc": -0.7155, "final_rank": 14 }, { "submission_id": "aoj_1518_2962920", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(void) {\n ll N;\n cin >> N;\n vector<ll> S(N);\n REP(i, 0, N) {\n ll P;\n string M;\n cin >> P >> M;\n ll x = 0;\n REP(j, 0, M.size()) {\n char c = M[j];\n if('0' <= c && c <= '9') x = x * P + (c - '0');\n if('A' <= c && c <= 'Z') x = x * P + (c - 'A' + 10);\n if('a' <= c && c <= 'z') x = x * P + (c - 'a' + 10 + 'Z' - 'A' + 1);\n }\n S[i] = x;\n }\n\n ll g = 0;\n REP(i, 0, N) g = g ^ S[i];\n cout << (g != 0 ? \"win\" : \"lose\") << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3620, "score_of_the_acc": -0.592, "final_rank": 10 }, { "submission_id": "aoj_1518_2962918", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n\nint cv(char c){\n if(isdigit(c)) return c-'0';\n if('A'<=c && c<='Z') return c-'A'+10;\n return c-'a'+36;\n}\n\nll READ(){\n ll p;\n string s;\n cin >>p >>s;\n\n ll ret = 0;\n rep(i,s.size()){\n ret = p*ret + cv(s[i]);\n }\n return ret;\n}\n\nint main(){\n int n;\n cin >>n;\n\n vector<ll> x(n);\n rep(i,n) x[i] = READ();\n\n // rep(i,n) printf(\" %lld\\n\", x[i]);\n ll res = 0;\n rep(i,n){\n res ^= __builtin_popcount(x[i]);\n }\n\n string ans = \"lose\";\n if(res != 0) ans = \"win\";\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3696, "score_of_the_acc": -0.5168, "final_rank": 6 }, { "submission_id": "aoj_1518_2943734", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint tonum(char c){\n\tif('0'<=c && c<='9') return c-'0';\n\tif('A'<=c && c <='Z') return c-'A'+10;\n\treturn c-'a'+36;\n}\n\nint conv(int p, string s){\n\tint res=0;\n\tint base = 1;\n\tfor(int i=s.length()-1; i>=0; i--, base*=p){\n\t\tres += tonum(s[i])*base;\n\t}\n\treturn res;\n}\n\nvector<int> split(int a){\n\tvector<int> res;\n\tint con=0;\n\tfor(int i=0; i<20; i++){\n\t\tif((1<<i & a) != 0){\n\t\t\tcon++;\n\t\t}else if(con != 0){\n\t\t\tres.push_back(con);\n\t\t\tcon = 0;\n\t\t}\n\t}\n\treturn res;\n}\n\nint main(){\n\tint n;\n\tcin >> n;\n\tvector<int> a(n);\n\tfor(int i=0; i<n; i++){\n\t\tint p;\n\t\tstring s;\n\t\tcin >> p >> s;\n\t\ta[i] = conv(p, s);\n\t}\n\t\n\tvector<int> len;\n\tfor(int i=0; i<(int)a.size(); i++){\n\t\tvector<int> tmp = split(a[i]);\n\t\tlen.insert(len.end(), tmp.begin(), tmp.end());\n\t}\n\t\n\tint g=0;\n\tfor(int i=0; i<n; i++){\n\t\tg ^= len[i];\n\t}\n\tif(g==0){\n\t\tcout << \"lose\" << endl;\n\t}else{\n\t\tcout << \"win\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 7048, "score_of_the_acc": -1.1251, "final_rank": 15 }, { "submission_id": "aoj_1518_2943602", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <stdio.h>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <limits.h>\nusing namespace std;\ntypedef vector<int> vi;\ntypedef long long ll;\nint n;\nll sect[100005];\nll ans=0,tmpans=0;\nll tmpmin=1e12;\ntypedef long long ll;\ntypedef vector<ll> vll;\n\nint main(int argc,char const* argv[])\n{\n int n;\n int p[100005];\n string m[100005];\n int buckets[300000];\n memset(buckets,0,sizeof(buckets));\n cin >> n;\n for(int i=0;i<n;i++)\n {\n cin >> p[i] >> m[i];\n }\n for(int i=0;i<n;i++)\n {\n int tmp=0;\n int keta=1;\n for(int j=m[i].size()-1;j>=0;j--)\n\t{\n\t //cout << (int)m[i][j] << endl;\n\t if((int)m[i][j]<=57)\n\t {\n\t tmp+=(int)(m[i][j]-'0')*keta;\n\t }\n\t else if(65 <=(int)m[i][j] && (int)m[i][j]<=90)\n\t {\n\t tmp+=(10+(int)(m[i][j]-'A'))*keta;\n\t }\n\t else if(97 <=(int)m[i][j])\n\t {\n\t tmp+=(36+(int)(m[i][j]-'a'))*keta;\n\t }\n\t keta*=p[i];\n\t}\n int onedev=0;\n while(tmp!=0)\n\t{\n\t if(tmp%2!=0) onedev++;\n\t tmp/=2;\n\t}\n if(onedev==1) buckets[1]++;\n //buckets[tmp]++;\n }\n if(buckets[1]==n)\n {\n if(n%2==0)\n\t{\n\t cout << \"lose\" << endl;\n\t}\n else\n\t{\n\t cout << \"win\" << endl;\n\t}\n }\n else if(buckets[1]!=n)\n {\n if(buckets[1]%2!=0)\n\t{\n\t cout << \"lose\" << endl;\n\t}\n else\n\t{\n\t cout << \"win\" << endl;\n\t}\n }\n return 0;\n}", "accuracy": 0.2, "time_ms": 30, "memory_kb": 7812, "score_of_the_acc": -0.8734, "final_rank": 20 }, { "submission_id": "aoj_1518_2943545", "code_snippet": "#include <iostream>\n#include <cstring>\n#include <stdio.h>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <limits.h>\nusing namespace std;\ntypedef vector<int> vi;\ntypedef long long ll;\nint n;\nll sect[100005];\nll ans=0,tmpans=0;\nll tmpmin=1e12;\ntypedef long long ll;\ntypedef vector<ll> vll;\n\nint main(int argc,char const* argv[])\n{\n int n;\n int p[100005];\n string m[100005];\n int buckets[300000];\n memset(buckets,0,sizeof(buckets));\n vi s;\n cin >> n;\n for(int i=0;i<n;i++)\n {\n cin >> p[i] >> m[i];\n }\n for(int i=0;i<n;i++)\n {\n int tmp=0;\n int keta=1;\n for(int j=m[i].size()-1;j>=0;j--)\n\t{\n\t //cout << (int)m[i][j] << endl;\n\t if((int)m[i][j]<=57)\n\t {\n\t tmp+=(int)(m[i][j]-'0')*keta;\n\t }\n\t else if(65 <=(int)m[i][j] && (int)m[i][j]<=90)\n\t {\n\t tmp+=(10+(int)(m[i][j]-'A'))*keta;\n\t }\n\t else if(97 <=(int)m[i][j])\n\t {\n\t tmp+=(36+(int)(m[i][j]-'a'))*keta;\n\t }\n\t keta*=p[i];\n\t}\n s.push_back(tmp);\n buckets[tmp]++;\n }\n if(buckets[1]==n)\n {\n if(n%2==0)\n\t{\n\t cout << \"lose\" << endl;\n\t}\n else\n\t{\n\t cout << \"win\" << endl;\n\t}\n }\n else if(buckets[1]!=n)\n {\n if(buckets[1]%2!=0)\n\t{\n\t cout << \"lose\" << endl;\n\t}\n else\n\t{\n\t cout << \"win\" << endl;\n\t}\n }\n return 0;\n}", "accuracy": 0.6, "time_ms": 40, "memory_kb": 10556, "score_of_the_acc": -1.25, "final_rank": 18 }, { "submission_id": "aoj_1518_2943454", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint tonum(char c){\n\tif('0'<=c && c<='9') return c-'0';\n\tif('A'<=c && c <='Z') return c-'A'+10;\n\treturn c-'a'+36;\n}\n\nint conv(int p, string s){\n\tint res=0;\n\tint base = 1;\n\tfor(int i=s.length()-1; i>=0; i--, base*=p){\n\t\tres += tonum(s[i])*base;\n\t}\n\treturn res;\n}\n\nvector<int> split(int a){\n\tvector<int> res;\n\tint con=0;\n\tfor(int i=0; i<20; i++){\n\t\tif((1<<i & a) != 0){\n\t\t\tcon++;\n\t\t}else if(con != 0){\n\t\t\tres.push_back((1<<con) -1);\n\t\t\tcon = 0;\n\t\t}\n\t}\n\treturn res;\n}\n\nvector<int> memo(1<<18, -1);\nint grundy(int n){\n\tif(memo[n] != -1) return memo[n];\n\tvector<int> s = split(n);\n\tvector<int> g;\n\tfor(int i=0; i<(int)s.size(); i++){\n\t\tfor(int j=0; j<s[i]; j++){\n\t\t\tg.push_back(grundy(j));\n\t\t}\n\t}\n\tsort(g.begin(), g.end());\n\tint res=0;\n\tfor(int i=0; i<(int)g.size(); i++){\n\t\tif(g[i] != res) break;\n\t\tres++;\n\t}\n\tmemo[n] = res;\n\treturn res;\n}\n\nint main(){\n\tmemo[0] = 0;\n\tint n;\n\tcin >> n;\n\tvector<int> a(n);\n\tfor(int i=0; i<n; i++){\n\t\tint p;\n\t\tstring s;\n\t\tcin >> p >> s;\n\t\ta[i] = conv(p, s);\n\t}\n\t\n\tint g=0;\n\tfor(int i=0; i<n; i++){\n\t\tg ^= grundy(a[i]);\n\t}\n\tif(g==0){\n\t\tcout << \"lose\" << endl;\n\t}else{\n\t\tcout << \"win\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.25, "time_ms": 130, "memory_kb": 4724, "score_of_the_acc": -1.3767, "final_rank": 19 }, { "submission_id": "aoj_1518_2943418", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nlong long po(long long p, long long n){\n long long res = 1;\n for(int i = 0; i < n; i++){\n res *= p;\n }\n\n return res;\n}\n\nint main(){ \n\n long long n; cin >> n;\n vector<long long> a(n);\n\n for(int ii = 0; ii < n; ii++){\n int p; cin >> p;\n string m; cin >> m;\n \n long long num = 0;\n int len = (int)m.size();\n for(int i = 0; i < len; i++){\n int idx = len - 1 - i;\n char c = m[idx];\n long long aa;\n if('A' <= c && c <= 'Z') aa = (long long)(10 + c - 'A');\n else if('a' <= c && c <= 'Z') aa = (long long)(36 + c - 'Z');\n else aa = (long long)(c - '0');\n \n a[ii] += aa * po(p, i);\n }\n }\n\n /*for(int i = 0; i < n; i++){\n cout << a[i] << endl;\n }*/\n\n int num = 0;\n for(int i = 0; i < n; i++){\n bool is_pre = false;\n while(a[i] != 0){\n if(a[i] % 2 == 1){\n num++;\n if(is_pre == true){\n cout << \"win\" << endl;\n return 0;\n }\n a[i] /= 2;\n is_pre = true;\n }else{\n is_pre = false;\n a[i] /= 2;\n }\n }\n }\n\n if(num % 2 == 1) cout << \"win\" << endl;\n else cout << \"lose\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3644, "score_of_the_acc": -0.6779, "final_rank": 13 }, { "submission_id": "aoj_1518_2942991", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint decode(int p, string s) {\n // reverse(s.begin(), s.end());\n\n int ret = 0;\n for(int i=0; i<s.length(); i++) {\n ret *= p;\n\n int val = 0;\n if('0' <= s[i] && s[i] <= '9') {\n val += (s[i] - '0');\n }\n else if('A' <= s[i] && s[i] <= 'Z') {\n val += 10;\n val += (s[i] - 'A');\n }\n else {\n val += 10 + 26;\n val += (s[i] - 'a');\n }\n\n ret += val;\n }\n\n // printf(\"debug: p = %d, m = %s, result = %d\\n\", p, s.c_str(), ret);\n return ret;\n}\n\nvoid apply_num(int &res, int value) {\n int cons = 0;\n for(int i=0; i<20; i++, value >>= 1) {\n if(value & 1) cons++;\n else {\n res ^= cons;\n cons = 0;\n }\n }\n}\n\nint main() {\n int N; cin >> N;\n\n vector<int> P(N);\n vector<string> M(N);\n\n for(int i=0; i<N; i++) {\n cin >> P[i] >> M[i];\n }\n\n int res = 0;\n vector<int> V(N);\n for(int i=0; i<N; i++) {\n V[i] = decode(P[i], M[i]);\n apply_num(res, V[i]);\n }\n\n if(res != 0) cout << \"win\" << endl;\n else cout << \"lose\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 9264, "score_of_the_acc": -1.1952, "final_rank": 17 }, { "submission_id": "aoj_1518_2659461", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nll getNUM(char ch){\n\tif(ch >= '0' && ch <= '9'){\n\t\treturn ch-'0';\n\t}else if(ch >= 'A' && ch <= 'Z'){\n\t\treturn 10+(ch-'A');\n\t}else{\n\t\treturn 35+(ch-'a');\n\t}\n}\n\n\nll calc(ll P,char buf[20]){\n\n\tll ret = 0;\n\n\tfor(int i = 0; buf[i] != '\\0'; i++){\n\t\tret += getNUM(buf[i]);\n\t}\n\n\treturn ret;\n}\n\n\nint main(){\n\n\tint N;\n\tscanf(\"%d\",&N);\n\n\tll NIM = 0,P;\n\tchar buf[20];\n\n\tfor(int loop = 0; loop < N; loop++){\n\t\tscanf(\"%lld %s\",&P,buf);\n\t\tNIM ^= calc(P,buf);\n\t}\n\n\tif(NIM == 0){\n\t\tprintf(\"lose\\n\");\n\t}else{\n\t\tprintf(\"win\\n\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3164, "score_of_the_acc": -0.2099, "final_rank": 1 }, { "submission_id": "aoj_1518_2210341", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint to_num(char c)\n{\n if (isdigit(c)) {\n return c - '0';\n } else if (isupper(c)) {\n return c - 'A' + 10;\n } else {\n return c - 'a' + 36;\n }\n}\n\nint change(int p, string m)\n{\n int res = 0;\n reverse(m.begin(), m.end());\n for (int i = 0; i < (int)m.size(); i++) {\n res += to_num(m[i]) * pow(p, i);\n }\n return res;\n}\n\nint main()\n{\n int N, p, x = 0;\n string m;\n cin >> N;\n for (int i = 0; i < N; i++) {\n cin >> p >> m;\n x ^= change(p, m);\n }\n cout << (x != 0 ? \"win\" : \"lose\") << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3368, "score_of_the_acc": -0.5651, "final_rank": 9 }, { "submission_id": "aoj_1518_2121700", "code_snippet": "#include<iostream>\n#include<string>\nusing namespace std;\nchar C[63] = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\";\nint convert(int p, string S) {\n\tint power[20]; power[0] = 1; for (int i = 1; i <= S.size(); i++)power[i] = power[i - 1] * p;\n\tint cnt = 0;\n\tfor (int i = 0; i < S.size(); i++) {\n\t\tfor (int j = 0; j < p; j++) {\n\t\t\tif (C[j] != S[i])continue;\n\t\t\tcnt += power[S.size() - 1 - i] * j;\n\t\t}\n\t}\n\treturn cnt;\n}\nint main() {\n\tint n; cin >> n; int W = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tstring U; int a; cin >> a >> U; a = convert(a, U);\n\t\tW ^= a;\n\t}\n\tif (W == 0)cout << \"lose\" << endl;\n\telse cout << \"win\" << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3156, "score_of_the_acc": -0.6257, "final_rank": 11 }, { "submission_id": "aoj_1518_2107330", "code_snippet": "#include <string>\n#include <iostream>\nusing namespace std;\nint n, p; string s;\nstring w = \"0123456789ABCDEFGHIJJKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\";\nint main() {\n\tcin >> n;\n\tint ret = 0;\n\twhile (n--) {\n\t\tcin >> p >> s;\n\t\tlong long t = 0;\n\t\tfor (int i = 0; i < s.size(); i++) {\n\t\t\tt *= p;\n\t\t\tfor (int j = 0; j < p; j++) {\n\t\t\t\tif (s[i] == w[j]) t += j;\n\t\t\t}\n\t\t}\n\t\tint cnt = 0;\n\t\twhile (t > 0) t >>= 1, cnt++;\n\t\tret ^= cnt;\n\t}\n\tcout << (ret ? \"win\" : \"lose\") << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3160, "score_of_the_acc": -0.5428, "final_rank": 7 }, { "submission_id": "aoj_1518_2102405", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n//// < \"d:\\d_download\\visual studio 2015\\projects\\programing_contest_c++\\debug\\a.txt\" > \"d:\\d_download\\visual studio 2015\\projects\\programing_contest_c++\\debug\\b.txt\"\n\n\nint main() {\n\tint N; cin >> N;\n\tmap<int, int>mp;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint p; string m; cin >>p>> m;\n\t\tlong long int num = 0;\n\t\tfor (auto c : m) {\n\t\t\tnum *= p;\n\t\t\tif (isdigit(c)) {\n\t\t\t\tnum += c - '0';\n\t\t\t}\n\t\t\telse if (c >= 'A'&&'Z' <= c) {\n\t\t\t\tnum += c - 'A' + 10;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnum += c - 'a' + 36;\n\t\t\t}\n\t\t}\n\t\tbitset<100>bs(num);\n\t\tint n = 0;\n\t\tfor (int i = 0; i < 99; ++i) {\n\t\t\tif (bs[i]) {\n\t\t\t\tn++;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (n) {\n\t\t\t\t\tmp[n]++;\n\t\t\t\t\tn = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint k = 0;\n\tfor (auto m: mp) {\n\t\tif (m.second%2) {\n\t\t\tk ^= m.first;\n\t\t}\n\t}\n\tif (k)cout << \"win\" << endl;\n\telse cout << \"lose\" << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3168, "score_of_the_acc": -0.627, "final_rank": 12 }, { "submission_id": "aoj_1518_1844660", "code_snippet": "#include<bits/stdc++.h>\n#define N 100000\nusing namespace std;\n\nint stoi(char c){\n if('A'<=c&&c<='Z')return c-'A'+10;\n if('a'<=c&&c<='z')return c-'a'+36;\n return c-'0';\n}\n\nint main(){\n int n,p,num,ans=0;\n string m;\n cin>>n;\n while(n--){\n cin>>p>>m;\n num=0;\n for(int i=0;i<m.size();i++)\n num=num*p+stoi(m[i]);\n ans^=num;\n }\n if(ans)cout<<\"win\"<<endl;\n else cout<<\"lose\"<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1200, "score_of_the_acc": -0.25, "final_rank": 2 }, { "submission_id": "aoj_1518_1839839", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint n,ans;\nvector<int> V;\n\nint change(char ch){\n if('0'<=ch&&ch<='9')return ch-'0';\n if('A'<=ch&&ch<='Z')return 10+ch-'A';\n return 36+ch-'a';\n}\n\nint main(){\n cin>>n;\n for(int i=0;i<n;i++){\n int p,num=0;\n string m;\n cin>>p>>m;\n for(int i=0;i<(int)m.size();i++)\n num=num*p+change(m[i]);\n ans^=num;\n V.push_back(num);\n }\n if(n==1 && V[0]==5){\n assert(0);\n }\n if(ans)cout<<\"win\"<<endl;\n else cout<<\"lose\"<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1708, "score_of_the_acc": -0.471, "final_rank": 5 }, { "submission_id": "aoj_1518_1835759", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint getNum(char c){\n if('A'<=c&&c<='Z')return 10+c-'A';\n if('a'<=c&&c<='z')return 36+c-'a';\n return c-'0';\n}\n\nint pto10(int p,string s){\n int res=0,x=1;\n for(int i=0;i<s.size();i++)res+=x*getNum(s[i]),x*=p;\n return res;\n}\n\nint Cnt(int num){\n int res=0;\n while(num)res++,num/=2;\n return res;\n}\n\nint main(){\n int n,p,cnt1=0,cnt2=0;\n vector<int> v;\n string s;\n cin>>n;\n for(int i=0;i<n;i++){\n cin>>p>>s;\n reverse(s.begin(),s.end());\n int num=pto10(p,s),t=0;\n while(num){\n if(num%2)t=t*10+1;\n else v.push_back(t),t=0;\n num/=2;\n }\n v.push_back(t);\n }\n for(int i=0;i<v.size();i++){\n int cnt=Cnt(v[i]);\n if(!v[i])continue;\n if(cnt==1)cnt1++;\n else cnt2++;\n }\n if(cnt2||cnt1%2)cout<<\"win\"<<endl;\n else cout<<\"lose\"<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 7284, "score_of_the_acc": -1.1503, "final_rank": 16 } ]
aoj_1525_cpp
Problem J: J's Final Problem Problem 不思議なダンジョンとは構造の変化を伴うダンジョンである。不思議なダンジョンは階層が深いものから浅いものまで様々なものがあり、深い階層には凶悪なモンスターが生息していたり、財宝が眠っていたりする。ジェイは不思議なダンジョンを研究している研究者である。ある日、新しいダンジョンを掘っていたところ、とても大きく深いダンジョンに繋がってしまった。ジェイはこのダンジョンを「ジェイの最終問題」と名付けた。このダンジョンの調査が、優秀な冒険者であるあなたに依頼されることになった。冒険者の目的はできるだけ深い層のフロアに到達することである。このダンジョンの特徴を以下に述べる。 地上からダンジョンの地下1階の部屋への下りの階段は1つしか存在しない。 各部屋は1つの上りの階段と2つの下りの階段が存在する。 2つの下りの階段のうち片方を右の階段、もう片方を左の階段とする。 冒険者が階段を降りた際に、ダンジョンの構造が変化することがある。 階段を降りるとは、地下 i 階のある部屋から地下 i +1階のある部屋へ降りることを示す。 階段を上った場合はダンジョンに対して何も変化を及ぼさない。 地下 n 階目には階段は存在しない。 最近、ジェイの最終問題に関する石碑が発見された。 どうやら、各階の階段を降りたときの変化がメモとして記されているようだ。 メモの形式は、 num1 direction1 : num2 direction2 という形である。 地下 num1 階のすべての部屋について、direction1の階段を降りている途中で、地下num2階のすべての部屋について、 direction2の階段を降りた先の部屋が消滅し、さらにそこから階段を降りることによって到達できる部屋や階段がすべて消滅する。部屋や階段の消滅に冒険者が巻き込まれた場合、冒険は失敗する。地下 i 階から地下 i +1階へ向かう階段の途中で冒険者が失敗した場合は地下 i 階まで到達したものとして扱う。 石碑において言及されていない階段は、その階段を降りてもダンジョンの構造に変化を及ぼさないことを示す。 冒険者はこの石碑をヒントにして探索を行うことにした。 ダンジョンの深さ n とメモの情報が m 個与えられるので、最大地下何階まで到達できるかを答えなさい。 Input 入力は複数のデータセットからなる。 n m memo 1 memo 2 … memo m 最初に n , m が与えられる。 それぞれ、ダンジョンの変化前の最下層の階数、石碑のメモの数を表す。 次に m 行にわたりメモの情報 num1 direction1 : num2 direction2 が与えられる。 Constraints 入力は以下の条件を満たす。 入力に含まれる値はすべて整数 1 ≤ n ≤ 10000 0 ≤ m ≤ ( n -1)×4 1 ≤ num1 , num2 ≤ n -1 direction1,direction2は"left"と"right"のいずれかである。 Output 最大地下何階まで降りることができるか1行に出力して答えよ。 Sample Input1 10 2 9 left : 9 left 9 right : 9 right Sample Output1 9 Sample Input2 4 5 1 left : 2 right 2 left : 3 right 2 left : 3 left 1 right : 3 left 2 right : 3 right Sample Output2 3
[ { "submission_id": "aoj_1525_3086993", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nstruct SCC{\n int n;\n vector<vector<int> > G,rG,T,C;\n vector<int> vs,used,belong;\n SCC(){}\n SCC(int sz):n(sz),G(sz),rG(sz),used(sz),belong(sz){}\n \n void add_edge(int from,int to){\n G[from].push_back(to);\n rG[to].push_back(from);\n }\n \n void input(int m,int offset=0){\n int a,b;\n for(int i=0;i<m;i++){\n cin>>a>>b;\n add_edge(a+offset,b+offset);\n }\n }\n \n void dfs(int v){\n used[v]=1;\n for(int i=0;i<(int)G[v].size();i++){\n if(!used[G[v][i]]) dfs(G[v][i]);\n }\n vs.push_back(v);\n }\n \n void rdfs(int v,int k){\n used[v]=1;\n belong[v]=k;\n C[k].push_back(v);\n for(int i=0;i<(int)rG[v].size();i++){\n if(!used[rG[v][i]]) rdfs(rG[v][i],k);\n }\n }\n \n int build(){\n fill(used.begin(),used.end(),0);\n vs.clear();\n for(int v=0;v<n;v++){\n if(!used[v]) dfs(v);\n }\n fill(used.begin(),used.end(),0);\n int k=0;\n for(int i=vs.size()-1;i>=0;i--){\n if(!used[vs[i]]){\n T.push_back(vector<int>());\n C.push_back(vector<int>());\n rdfs(vs[i],k++);\n }\n }\n for(int i=0;i<n;i++)\n for(int u:G[i])\n if(belong[i]!=belong[u])\n T[belong[i]].push_back(belong[u]);\n for(int i=0;i<k;i++){\n sort(T[i].begin(),T[i].end());\n T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());\n }\n return k;\n }\n};\nsigned main(){\n int n,m;\n cin>>n>>m;\n int V = n*2;\n vector<vector<int> > G(V);\n \n int l=0,r=n;\n for(int i=0;i<m;i++){\n int x,y;\n string s,t,u;\n cin>>x>>s>>u>>y>>t;\n x--;y--;\n if(s==\"right\") x+=n;\n if(t==\"right\") y+=n;\n G[x].emplace_back((y+n)%V);\n G[y].emplace_back((x+n)%V);\n }\n \n auto check=[&](int x)->int{\n SCC scc(V);\n for(int i=0;i<x;i++)\n for(int v=i;v<V;v+=n)\n for(int u:G[v])\n if(u%n<x) scc.add_edge(v,u);\n \n scc.build();\n for(int i=0;i<x;i++)\n if(scc.belong[i]==scc.belong[i+n]) return 0;\n return 1;\n };\n \n while(l+1<r){\n int m=(l+r)>>1;\n if(check(m)) l=m;\n else r=m;\n }\n cout<<r<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 8068, "score_of_the_acc": -1.3457, "final_rank": 6 }, { "submission_id": "aoj_1525_3078412", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nstruct SCC{\n int n;\n vector<vector<int> > G,rG,T,C;\n vector<int> vs,used,belong;\n SCC(){}\n SCC(int sz):n(sz),G(sz),rG(sz),used(sz),belong(sz){}\n \n void add_edge(int from,int to){\n G[from].push_back(to);\n rG[to].push_back(from);\n }\n \n void input(int m,int offset=0){\n int a,b;\n for(int i=0;i<m;i++){\n cin>>a>>b;\n add_edge(a+offset,b+offset);\n }\n }\n \n void dfs(int v){\n used[v]=1;\n for(int i=0;i<(int)G[v].size();i++){\n if(!used[G[v][i]]) dfs(G[v][i]);\n }\n vs.push_back(v);\n }\n \n void rdfs(int v,int k){\n used[v]=1;\n belong[v]=k;\n C[k].push_back(v);\n for(int i=0;i<(int)rG[v].size();i++){\n if(!used[rG[v][i]]) rdfs(rG[v][i],k);\n }\n }\n \n int build(){\n fill(used.begin(),used.end(),0);\n vs.clear();\n for(int v=0;v<n;v++){\n if(!used[v]) dfs(v);\n }\n fill(used.begin(),used.end(),0);\n int k=0;\n for(int i=vs.size()-1;i>=0;i--){\n if(!used[vs[i]]){\n\tT.push_back(vector<int>());\n\tC.push_back(vector<int>());\n\trdfs(vs[i],k++);\n }\n }\n for(int i=0;i<n;i++)\n for(int u:G[i])\n\tif(belong[i]!=belong[u])\n\t T[belong[i]].push_back(belong[u]);\n for(int i=0;i<k;i++){\n sort(T[i].begin(),T[i].end());\n T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());\n }\n return k;\n }\n};\nsigned main(){\n int n,m;\n cin>>n>>m;\n int V = n*2;\n vector<vector<int> > G(V);\n \n int l=0,r=n;\n for(int i=0;i<m;i++){\n int x,y;\n string s,t,u;\n cin>>x>>s>>u>>y>>t;\n x--;y--;\n if(s==\"right\") x+=n;\n if(t==\"right\") y+=n;\n G[x].emplace_back((y+n)%V);\n G[y].emplace_back((x+n)%V);\n }\n \n auto check=[&](int x)->int{\n SCC scc(V);\n for(int i=0;i<x;i++)\n for(int v=i;v<V;v+=n)\n\tfor(int u:G[v])\n\t if(u%n<x) scc.add_edge(v,u);\n \n scc.build();\n for(int i=0;i<x;i++)\n if(scc.belong[i]==scc.belong[i+n]) return 0;\n return 1;\n };\n \n while(l+1<r){\n int m=(l+r)>>1;\n if(check(m)) l=m;\n else r=m;\n }\n cout<<r<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 8060, "score_of_the_acc": -1.2199, "final_rank": 4 }, { "submission_id": "aoj_1525_2967424", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\n#define INF 1e16\n#define mod 1000000007\n\nint n,m;\nvector<P> es;\nint addnot;\nint v1[80010],v2[80010];\n\nint V;\nvector<int> g[80010],rg[80010];\nvector<int> vs;\nbool used[80010];\nint cmp[80010];\n\nvoid add_edge(int from,int to){\n g[from].push_back(to);\n rg[to].push_back(from);\n}\n\nvoid dfs(int v){\n used[v]=true;\n for(int i=0;i<g[v].size();i++){\n if(!used[g[v][i]])dfs(g[v][i]);\n }\n vs.push_back(v);\n}\n\nvoid rdfs(int v,int k){\n used[v]=true;\n cmp[v]=k;\n for(int i=0;i<rg[v].size();i++){\n if(!used[rg[v][i]])rdfs(rg[v][i],k);\n }\n}\n\nint scc(){\n memset(used,0,sizeof(used));\n vs.clear();\n for(int v=0;v<V;v++){\n if(!used[v])dfs(v);\n }\n memset(used,0,sizeof(used));\n int k=0;\n for(int i=vs.size()-1;i>=0;i--){\n if(!used[vs[i]])rdfs(vs[i],k++);\n }\n return k;\n}\n\nbool ok(int x){\n rep(i,V){\n g[i].clear();\n rg[i].clear();\n }\n rep(i,es.size()){\n if(v1[i]<x&&v2[i]<x)add_edge(es[i].fi,es[i].se);\n }\n scc();\n bool ok=true;\n rep(i,n){\n if(cmp[i]==cmp[i+addnot])ok=false;\n }\n return ok;\n}\n\nint main(){\n cin>>n>>m;\n addnot=n;\n V=n*2;\n rep(i,m){\n int a1,a2;\n string d1,d2,c;\n cin>>a1>>d1>>c>>a2>>d2;\n a1--; a2--;\n v1[i*2]=a1; v2[i*2]=a2;\n es.push_back(P(a1+(d1==\"left\"?addnot:0),a2+(d2==\"left\"?0:addnot)));\n v1[i*2+1]=a2; v2[i*2+1]=a1;\n es.push_back(P(a2+(d2==\"left\"?addnot:0),a1+(d1==\"left\"?0:addnot)));\n }\n int lb=0,ub=n;\n while(ub-lb>1){\n int mid=(lb+ub)/2;\n if(ok(mid))lb=mid;\n else ub=mid;\n }\n cout<<ub<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 9572, "score_of_the_acc": -1.1262, "final_rank": 3 }, { "submission_id": "aoj_1525_2966266", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct SCC{\n int V;\n vector<vector<int>> G, rG;\n vector<int> vs; // 帰りがけ順の並び\n vector<int> cmp; //属する強連結成分トポロジカル順序\n vector<bool> used;\n\n SCC(){}\n SCC(int n){\n V = n;\n G = vector<vector<int>>(n);\n rG = vector<vector<int>>(n);\n }\n\n void add_edge(int from, int to){\n G[from].push_back(to);\n rG[to].push_back(from);\n }\n\n void dfs(int v){\n used[v] = true;\n rep(i,G[v].size())if(!used[G[v][i]]) dfs(G[v][i]);\n vs.push_back(v);\n }\n\n void rdfs(int v, int k){\n used[v]=true;\n cmp[v]=k;\n rep(i,rG[v].size())if(!used[rG[v][i]]) rdfs(rG[v][i],k);\n }\n\n int scc(){\n used = vector<bool>(V,false);\n vs.clear();\n rep(i,V)if(!used[i]) dfs(i);\n\n used = vector<bool>(V,false);\n cmp = vector<int>(V);\n int num_scc = 0;\n for(int i=vs.size()-1; i>=0; --i)if(!used[vs[i]]) rdfs(vs[i],num_scc++);\n return num_scc;\n }\n};\n\nstruct TwoSat{\n int v;\n SCC graph;\n\n // v literals\n // 0~v-1: true\n // v~2v-1: false\n\n TwoSat(int num_literal){\n v = num_literal;\n graph = SCC(2*v);\n }\n\n inline int num(int id, bool b){return id+(b?0:v);}\n\n void add_clause(int x, bool X, int y, bool Y){\n graph.add_edge(num(x,!X), num(y,Y));\n graph.add_edge(num(y,!Y), num(x,X));\n }\n\n // 割り当てが可能か調べる\n bool calc(){\n graph.scc();\n rep(i,v)if(graph.cmp[i]==graph.cmp[v+i]) return false;\n return true;\n }\n\n // リテラルの真偽値を返す\n vector<bool> get_literals(){\n assert(calc());\n vector<bool> res(v);\n rep(i,v) res[i] = (graph.cmp[i]>graph.cmp[v+i]);\n return res;\n }\n};\n\n// (floor, left/right)\nusing pi = pair<int,int>;\n\npi READ(){\n int num;\n string lr;\n cin >>num >>lr;\n\n return {num, (lr==\"right\")};\n}\n\nint main(){\n int n,m;\n cin >>n >>m;\n\n vector<pair<pi,pi>> v(m);\n rep(i,m){\n char sep;\n pi x,y;\n\n x = READ();\n cin >>sep;\n y = READ();\n\n v[i] = {x,y};\n }\n\n auto valid = [&](int c){\n const int N = 10010;\n TwoSat t(N);\n rep(i,m){\n pi x = v[i].fi, y = v[i].se;\n if(x.fi<c && y.fi<c){\n t.add_clause(x.fi, !x.se, y.fi, !y.se);\n }\n }\n\n return t.calc();\n };\n\n int l=1, r=n+1;\n while(r-l>1){\n int mid = (l+r)/2;\n if(valid(mid)) l=mid;\n else r=mid;\n }\n cout << l << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 5340, "score_of_the_acc": -0.6887, "final_rank": 1 }, { "submission_id": "aoj_1525_2504493", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define F first\n#define S second\n#define R cin>>\n#define Z class\n#define ll long long\n#define ln cout<<'\\n'\n#define in(a) insert(a)\n#define pb(a) push_back(a)\n#define pd(a) printf(\"%.10f\\n\",a)\n#define mem(a) memset(a,0,sizeof(a))\n#define all(c) (c).begin(),(c).end()\n#define iter(c) __typeof((c).begin())\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)\n#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)\n#define rep(i,n) REP(i,0,n)\n#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)\ntemplate<Z A>void pr(A a){cout<<a;ln;}\ntemplate<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}\ntemplate<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}\ntemplate<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}\ntemplate<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}\nll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}\nconst ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};\ntypedef pair<int,int> P;\ntypedef pair<P,P> PP;\n#define N 200001\nvector<int> G[N],rG[N],g;\nbool u[N];\nint n,cmp[N];\n\nvoid add_edge(int x,int y){G[x].push_back(y);rG[y].push_back(x);}\nvoid dfs(int x){u[x]=1;rep(i,G[x].size())if(!u[G[x][i]])dfs(G[x][i]);g.push_back(x);} \nvoid rdfs(int x,int k){u[x]=1;cmp[x]=k;rep(i,rG[x].size())if(!u[rG[x][i]])rdfs(rG[x][i],k);}\nint scc() {\n mem(u);g.clear();rep(i,n)if(!u[i])dfs(i);mem(u);\n int k=0;rrep(i,g.size())if(!u[g[i]])rdfs(g[i],k++);return k;\n}\nvoid init() {\n rep(i,n) G[i].clear(),rG[i].clear();\n g.clear();\n}\nint rev(int x) {return (x+n/2)%n;}\nint two_sat(vector<P> v) {\n init();\n rep(i,v.size()) {\n add_edge(rev(v[i].F),v[i].S);\n add_edge(rev(v[i].S),v[i].F);\n }\n scc();\n rep(i,n/2) {\n if(cmp[i]==cmp[i+n/2]) return 0;\n }\n return 1;\n}\nvoid Main() {\n int m;\n cin >> n >> m;\n PP a[m];\n rep(i,m) {\n int x,y;\n string s,t,r;\n cin >> x >> s >> t >> y >> r;\n x--,y--;\n a[i]=PP(P(x,s==\"left\"?0:1),P(y,r==\"left\"?0:1));\n }\n int l=0,r=n;\n while(l+1<r) {\n int mid=(l+r)/2;\n n=mid*2;\n vector<P> v;\n rep(i,m) {\n int x=a[i].F.F,y=a[i].S.F;\n if(x>=mid||y>=mid) continue;\n if(a[i].F.S) x=rev(x);\n if(a[i].S.S) y=rev(y);\n v.pb(P(x,y));\n }\n if(two_sat(v)) l=mid;\n else r=mid;\n }\n cout << r << endl;\n}\n\nint main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}", "accuracy": 1, "time_ms": 30, "memory_kb": 14396, "score_of_the_acc": -1.25, "final_rank": 5 }, { "submission_id": "aoj_1525_2504487", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define F first\n#define S second\n#define R cin>>\n#define Z class\n#define ll long long\n#define ln cout<<'\\n'\n#define in(a) insert(a)\n#define pb(a) push_back(a)\n#define pd(a) printf(\"%.10f\\n\",a)\n#define mem(a) memset(a,0,sizeof(a))\n#define all(c) (c).begin(),(c).end()\n#define iter(c) __typeof((c).begin())\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)\n#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)\n#define rep(i,n) REP(i,0,n)\n#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)\ntemplate<Z A>void pr(A a){cout<<a;ln;}\ntemplate<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}\ntemplate<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}\ntemplate<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}\ntemplate<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}\nll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}\nconst ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};\ntypedef pair<int,int> P;\ntypedef pair<P,P> PP;\n#define N 200001\nvector<int> G[N],rG[N],g;\nbool u[N];\nint n,cmp[N];\n\nvoid add_edge(int x,int y){G[x].push_back(y);rG[y].push_back(x);}\nvoid dfs(int x){u[x]=1;rep(i,G[x].size())if(!u[G[x][i]])dfs(G[x][i]);g.push_back(x);} \nvoid rdfs(int x,int k){u[x]=1;cmp[x]=k;rep(i,rG[x].size())if(!u[rG[x][i]])rdfs(rG[x][i],k);}\nint scc() {\n mem(u);g.clear();rep(i,n)if(!u[i])dfs(i);mem(u);\n int k=0;rrep(i,g.size())if(!u[g[i]])rdfs(g[i],k++);return k;\n}\nvoid init() {\n rep(i,n) G[i].clear(),rG[i].clear();\n g.clear();\n}\nint rev(int x) {return (x+n/2)%n;}\nint two_sat(vector<P> v) {\n init();\n rep(i,v.size()) {\n add_edge(rev(v[i].F),v[i].S);\n add_edge(rev(v[i].S),v[i].F);\n }\n scc();\n rep(i,n/2) {\n if(cmp[i]==cmp[i+n/2]) return 0;\n }\n return 1;\n}\nvoid Main() {\n int m;\n cin >> n >> m;\n PP a[m];\n rep(i,m) {\n int x,y;\n string s,t,r;\n cin >> x >> s >> t >> y >> r;\n x--,y--;\n a[i]=PP(P(x,s==\"left\"?0:1),P(y,r==\"left\"?0:1));\n }\n int l=-1,r=n-1;\n while(l+1<r) {\n int mid=(l+r)/2;\n n=mid*2;\n vector<P> v;\n rep(i,m) {\n int x=a[i].F.F,y=a[i].S.F;\n if(x>mid||y>mid) continue;\n if(a[i].F.S) x=rev(x);\n if(a[i].S.S) y=rev(y);\n v.pb(P(x,y));\n }\n if(two_sat(v)) l=mid;\n else r=mid;\n }\n cout << r+1 << endl;\n}\n\nint main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}", "accuracy": 0.125, "time_ms": 10, "memory_kb": 13752, "score_of_the_acc": -0.9334, "final_rank": 7 }, { "submission_id": "aoj_1525_2504484", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define F first\n#define S second\n#define R cin>>\n#define Z class\n#define ll long long\n#define ln cout<<'\\n'\n#define in(a) insert(a)\n#define pb(a) push_back(a)\n#define pd(a) printf(\"%.10f\\n\",a)\n#define mem(a) memset(a,0,sizeof(a))\n#define all(c) (c).begin(),(c).end()\n#define iter(c) __typeof((c).begin())\n#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)\n#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)\n#define rep(i,n) REP(i,0,n)\n#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)\ntemplate<Z A>void pr(A a){cout<<a;ln;}\ntemplate<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}\ntemplate<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}\ntemplate<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}\ntemplate<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}\nll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}\nconst ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};\ntypedef pair<int,int> P;\ntypedef pair<P,P> PP;\n#define N 200001\nvector<int> G[N],rG[N],g;\nbool u[N];\nint n,cmp[N];\n\nvoid add_edge(int x,int y){G[x].push_back(y);rG[y].push_back(x);}\nvoid dfs(int x){u[x]=1;rep(i,G[x].size())if(!u[G[x][i]])dfs(G[x][i]);g.push_back(x);} \nvoid rdfs(int x,int k){u[x]=1;cmp[x]=k;rep(i,rG[x].size())if(!u[rG[x][i]])rdfs(rG[x][i],k);}\nint scc() {\n mem(u);g.clear();rep(i,n)if(!u[i])dfs(i);mem(u);\n int k=0;rrep(i,g.size())if(!u[g[i]])rdfs(g[i],k++);return k;\n}\nvoid init() {\n rep(i,n) G[i].clear(),rG[i].clear();\n g.clear();\n}\nint rev(int x) {return (x+n/2)%n;}\nint two_sat(vector<P> v) {\n init();\n rep(i,v.size()) {\n add_edge(rev(v[i].F),v[i].S);\n add_edge(rev(v[i].S),v[i].F);\n }\n scc();\n rep(i,n/2) {\n if(cmp[i]==cmp[i+n/2]) return 0;\n }\n return 1;\n}\nvoid Main() {\n int m;\n cin >> n >> m;\n PP a[m];\n rep(i,m) {\n int x,y;\n string s,t,r;\n cin >> x >> s >> t >> y >> r;\n x--,y--;\n a[i]=PP(P(x,s==\"left\"?0:1),P(y,r==\"left\"?0:1));\n }\n int l=-1,r=n+1;\n while(l+1<r) {\n int mid=(l+r)/2;\n n=mid*2;\n vector<P> v;\n rep(i,m) {\n int x=a[i].F.F,y=a[i].S.F;\n if(x>mid||y>mid) continue;\n if(a[i].F.S) x=rev(x);\n if(a[i].S.S) y=rev(y);\n v.pb(P(x,y));\n }\n if(two_sat(v)) l=mid;\n else r=mid;\n }\n cout << r+1 << endl;\n}\n\nint main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}", "accuracy": 0.125, "time_ms": 10, "memory_kb": 13880, "score_of_the_acc": -0.9467, "final_rank": 8 }, { "submission_id": "aoj_1525_1552263", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef pair<int,int> P;\nint N,M;\nvector<int> G[20002];\nvector<int> rG[20002];\nbool used[20002];\nint cmp[20002];\nvector<int> vs;\n\nvoid init(int size){\n vs.clear();\n for(int i=0;i<size;i++) {\n G[i].clear();\n rG[i].clear();\n cmp[i] = -1;\n used[i] = 0;\n }\n}\n\nvoid add_edge(int a,int b){//a->b\n G[a].push_back( b );\n rG[b].push_back( a );\n}\nvoid dfs(int id){\n if( used[id] ) return;\n used[id] = true;\n for(int i=0;i<(int)G[id].size();i++)\n dfs(G[id][i]);\n vs.push_back( id );\n}\nvoid rdfs(int id,int k){\n if( used[id] ) return;\n used[id] = true;\n cmp[id]=k;\n for(int i=0;i<(int)rG[id].size();i++)\n rdfs(rG[id][i],k); \n}\n\nint scc(int size){\n memset(used,0,sizeof(used));\n for(int i=0;i<size;i++)\n dfs(i);\n memset(used,0,sizeof(used));\n int k=0;\n for(int i=(int)vs.size()-1;i>-1;i--){\n if( used[vs[i]] ) continue;\n rdfs(vs[i],k++);\n } \n return k;\n}\n\nint rev(int a){\n if( a & 1 ) return --a;\n else return ++a;\n}\n\nvoid view(int size){\n for(int i=0;i<size;i++){\n if( G[i].empty() ) continue;\n cout << i << endl;\n for(int j=0;j<(int)G[i].size();j++){\n cout << \" -> \" << G[i][j] << endl;\n }\n }\n}\n\n//?????????a,~a,b,~b,c,~c...,?????????0,1,2,3,4,5...??¨????????????\nbool two_SAT(const vector<P>& v,int size){\n // cout << \"2-sat :\" << size << endl;\n init( size );\n for(int i=0;i<(int)v.size();i++){\n //cout << v[i].first << \" V \" << v[i].second << endl;\n //aVb\n add_edge( rev(v[i].first), v[i].second );//~a->b\n add_edge( rev(v[i].second), v[i].first );//~b->a\n }\n // view(size);\n scc(size);\n //x??¨~x?????°???????????£?????????????????????????????????\n for(int i=0;i<size;i+=2){\n //cout << i << \" \" << cmp[i] << \", \" << cmp[i+1] << endl;\n if( cmp[i] == cmp[i+1] ) return false;\n }\n return true;\n}\n\nvector<int> V[20001];\nint main(){\n \n while(cin >> N >> M){\n for(int i=0;i<M;i++){\n int num1,num2;\n string d1,d2,bf;\n cin >> num1 >> d1 >> bf >> num2 >> d2;\n if( num1 > num2 ){\n\tswap(num1,num2);\n\tswap(d1,d2);\n }\n int a = 2*num1, b = 2*num2;\n if( d1 == \"left\" ) a++;\n if( d2 == \"right\" ) b++;\n V[a].push_back( b ); \n }\n for(int i=2;i<2*N;i++){\n V[i].push_back(i);\n sort(V[i].begin(),V[i].end());\n V[i].erase(unique(V[i].begin(),V[i].end()),V[i].end());\n }\n int st = 1, ed = N;\n int res = 1;\n while( st <= ed ){\n int h = (st+ed)/2;\n vector<P> nv;\n for(int i=2;i<2*h;i+=2){\n\tint n = (int)V[i].size(), m = (int)V[i+1].size();\n int x,y;\n for(x=0;x<n;x++)\n\tif( V[i][x] >= 2*h ) break;\n for(y=0;y<m;y++)\n\tif( V[i+1][y] >= 2*h ) break;\n for(int j=0;j<x;j++)\n\tfor(int k=0;k<y;k++)\n\t nv.push_back(P(V[i][j],V[i+1][k]));\t \n }\n if( two_SAT(nv,h*2) ){\n\tres = max( res, h );\n\tst = h + 1;\n } else {\n\ted = h - 1;\n }\n }\n cout << res << endl;\n for(int i=0;i<2*N;i++) V[i].clear();\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 4724, "score_of_the_acc": -0.875, "final_rank": 2 } ]
aoj_1529_cpp
Problem D: Cheat Case Problem 算数のテストを受けることになった俺達は、今から勉強しても赤点を取ってしまうことを確信し、カンニングで乗り切ることを決意した。 算数のテスト問題を作成するA先生はテスト用紙を手書きで作ることで知られている。彼がテスト問題を作成している時の筆跡情報を、近所の博士に作ってもらった筆跡盗聴鉛筆で盗み取る作戦だ。だが肝心の筆跡情報から問題を復元するプログラムは博士の技術では作ることができなかった。しかも、たとえ問題が復元できたとしても、俺達の頭脳では問題を解くことができない。どうすることもできなくなった俺達は、プログラマーであるあなたに、筆跡情報から問題を復元し、それを解くプログラムの作成を依頼することにした。 今回のテストでは、数式を解く問題が出題される。数式中の演算記号は+(加算),-(減算),・(乗算)の3つのみで、乗算は加算、減算よりも優先して計算される。数式は必ず構文が正しいものが与えられる。また、0以外の数で、先頭が0であるものは与えられない。 問題が書かれる紙はグリッドで表される。筆跡情報は線分の集合で与えられ、各線分はX軸に水平または垂直、もしくは始点、終点が同じ(点)である。この線分は、始点から終点までにあるマスを黒く塗りつぶすことを表す。座標(0, 0)が紙の左上である。数式の各文字(0,1,2,3,4,5,6,7,8,9,+,-,・)は以下で表される。 0 1 2 3 4 5 6 7 8 9 + - ・ A先生は印刷したかのような字を書くことで知られており、各文字は上記以外の書かれ方はしない。また、上記以外の文字が書かれることはない。2つ以上の文字がマスの辺を共有することはない。各文字の位置は、文字を構成するマス(黒いマス)の中で、最も左側にあるもののX座標で表される。数式の文字は、位置が小さい順に(左から右へ)解釈される。2つ以上の文字の位置が等しくなることはない。 Input N X 11 Y 11 X 12 Y 12 X 21 Y 21 X 22 Y 22 : X N1 Y N1 X N2 Y N2 1行目に線分の数を表す1つの整数 N が与えられる。次に線分の情報が N 行で与えられる。線分の情報の i 行目には4つの整数 X i1 , Y i1 , X i2 , Y i2 が空白区切りで与えられる。それぞれ、線分の1つ目の端点のX座標、Y座標、2つ目の端点のX座標、Y座標を表す。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 150 0 ≤ X i1 , Y i1 , X i2 , Y i2 ≤ 200 (1 ≤ i ≤ N ) X i1 = X i2 または Y i1 = Y i2 (1 ≤ i ≤ N ) 計算の途中と結果に現れる数は符号付き32ビット整数に収まる Output 各ケースの計算結果を一行に出力せよ。 Sample Input 1 4 1 1 1 5 3 3 5 3 4 2 4 4 7 1 7 5 Sample Output 1 2 Sample Input 2 23 1 1 3 1 3 1 3 3 3 3 1 3 1 3 1 5 1 5 3 5 5 2 7 2 7 2 7 4 7 4 5 4 5 4 5 6 5 6 7 6 11 4 13 4 13 0 15 0 15 0 15 4 18 2 18 2 21 5 23 5 21 5 21 7 21 7 23 7 23 7 23 9 23 9 21 9 24 0 26 0 24 0 24 4 24 4 26 4 26 0 26 4 Sample Output 2 -328 Hint 1つのグリッドを2度以上塗りつぶすことがある '1'など、1つの線分で表すことが可能でも、2つ以上の線分で表す場合がある Sample Input 1の数式。 Sample Input 2の数式。"-7"の部分のように、ある文字を覆う最小の長方形の内部に別の文字が入り込む場合がある。
[ { "submission_id": "aoj_1529_894238", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cassert>\nusing namespace std;\ntypedef pair<int,int> P;\n\nnamespace NS {\n string s;\n int p;\n int exp();\n int term();\n int fact();\n \n int exp() {\n int x = term();\n while(s[p] == '+' || s[p] == '-') {\n if(s[p] == '+') {\n ++p;\n x += term();\n } else {\n ++p;\n x -= term();\n }\n }\n return x;\n }\n \n int term() {\n int x = fact();\n while(s[p] == '*') {\n ++p;\n x *= fact();\n }\n return x;\n }\n \n int fact() {\n string num = \"\";\n while(isdigit(s[p])) {\n num += s[p++];\n }\n return atoi(num.c_str());\n }\n}\n\nint parse(string s) {\n NS::s = s + \"$\";\n NS::p = 0;\n int res = NS::exp();\n assert(NS::p+1 == NS::s.size());\n return res;\n}\n\n\nconst int MAXH = 221;\nconst int MAXW = 221;\nconst int MH = 7;\nconst int MW = 5;\nint N;\nchar G[MAXH][MAXW];\n\n\nconst string F[13][MH] = {\n {\n \".....\",\n \".###.\",\n \".#.#.\",\n \".#.#.\",\n \".#.#.\",\n \".###.\",\n \".....\"\n },\n {\n \".....\",\n \".#...\",\n \".#...\",\n \".#...\",\n \".#...\",\n \".#...\",\n \".....\"\n },\n {\n \".....\",\n \".###.\",\n \"...#.\",\n \".###.\",\n \".#...\",\n \".###.\",\n \".....\"\n },\n {\n \".....\",\n \".###.\",\n \"...#.\",\n \".###.\",\n \"...#.\",\n \".###.\",\n \".....\"\n },\n {\n \".....\",\n \".#.#.\",\n \".#.#.\",\n \".###.\",\n \"...#.\",\n \"...#.\",\n \".....\"\n },\n {\n \".....\",\n \".###.\",\n \".#...\",\n \".###.\",\n \"...#.\",\n \".###.\",\n \".....\"\n },\n {\n \".....\",\n \".###.\",\n \".#...\",\n \".###.\",\n \".#.#.\",\n \".###.\",\n \".....\"\n },\n {\n \".....\",\n \".###.\",\n \"...#.\",\n \"...#.\",\n \"...#.\",\n \"...#.\",\n \".....\"\n },\n {\n \".....\",\n \".###.\",\n \".#.#.\",\n \".###.\",\n \".#.#.\",\n \".###.\",\n \".....\"\n },\n {\n \".....\",\n \".###.\",\n \".#.#.\",\n \".###.\",\n \"...#.\",\n \".###.\",\n \".....\"\n },\n {\n \".....\",\n \".....\",\n \"..#..\",\n \".###.\",\n \"..#..\",\n \".....\",\n \".....\"\n },\n {\n \".....\",\n \".....\",\n \".....\",\n \".###.\",\n \".....\",\n \".....\",\n \".....\"\n },\n {\n \".....\",\n \".....\",\n \".....\",\n \".#...\",\n \".....\",\n \".....\",\n \".....\"\n }\n};\n\nvoid show(int w = 101, int h = 101) {\n for(int i = 0; i < h; ++i) {\n for(int j = 0; j < w; ++j) {\n cout << G[i][j];\n }\n cout << endl;\n }\n}\n\nint main() {\n cin >> N;\n fill(G[0], G[MAXH], '.');\n for(int k = 0; k < N; ++k) {\n int a, b, c, d;\n cin >> a >> b >> c >> d;\n if(b == d) {\n for(int i = min(a,c); i <= max(a,c); ++i) G[10+b][10+i] = '#';\n } else {\n for(int i = min(b,d); i <= max(b,d); ++i) G[10+i][10+a] = '#';\n }\n }\n\n vector<pair<int,int> > id;\n for(int k = 0; k < 13; ++k) {\n pair<int,int> p(0, k);\n for(int a = 0; a < MH; ++a) {\n for(int b = 0; b < MW; ++b) {\n p.first -= F[k][a][b] == '#';\n }\n }\n id.push_back(p);\n }\n sort(id.begin(), id.end());\n\n vector<pair<int,int> > v;\n for(int t = 0; t < 13; ++t) {\n int k = id[t].second;\n for(int i = 0; i+MH <= MAXH; ++i) {\n for(int j = 0; j+MW <= MAXW; ++j) {\n try {\n for(int a = 0; a < MH; ++a) {\n for(int b = 0; b < MW; ++b) {\n if(G[i+a][j+b] == '.' && F[k][a][b] == '#') throw 0;\n }\n }\n for(int a = 0; a < MH; ++a) {\n for(int b = 0; b < MW; ++b) {\n if(F[k][a][b] == '#') G[i+a][j+b] = '.';\n }\n }\n v.push_back(make_pair(j, k));\n } catch(...) {}\n }\n }\n }\n\n sort(v.begin(), v.end());\n \n string s;\n for(int i = 0; i < v.size(); ++i) {\n if(v[i].second == 10) {\n s += '+';\n } else if(v[i].second == 11) {\n s += '-';\n } else if(v[i].second == 12) {\n s += '*';\n } else {\n s += '0'+v[i].second;\n }\n }\n //cout << s << endl;\n cout << parse(s) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 850, "memory_kb": 1348, "score_of_the_acc": -1, "final_rank": 1 }, { "submission_id": "aoj_1529_890299", "code_snippet": "#include<iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\n\nstring ume_c=\"89605+32471-*\";\nstring ume[13][5]={\n {//8\n \"111\",\n \"101\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\n\t{\n \"111\",\n \"101\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"101\",\n \"101\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"000\",\n \"010\",\n \"111\",\n\t\"010\",\n \"000\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"100\",\n \"111\"\n },\n\t\n\t{\n \"101\",\n \"101\",\n \"111\",\n\t\"001\",\n \"001\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"001\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"000\",\n \"000\",\n \"111\",\n\t\"000\",\n \"000\"\n },\n\t{\n \"000\",\n \"000\",\n \"010\",\n\t\"000\",\n \"000\"\n }\n\t\n\t\n};\n\n\nstring S;\nint pos;\n\nint expr();\nint term();\nint num();\n\nint expr(){\n\tint res = term();\n\twhile(1){\n\t\tif(S[pos]=='+'){\n\t\t\tpos++;\n\t\t\tres += term();\n\t\t}else if(S[pos]=='-'){\n\t\t\tpos++;\n\t\t\tres -= term();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint term(){\n\tint res = num();\n\twhile(1){\n\t\tif(S[pos]=='*'){\n\t\t\tpos++;\n\t\t\tres *= num();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint num(){\n\tint c = S[pos++]-'0';\n\twhile('0' <= S[pos] && S[pos] <= '9'){\n\t\tc *= 10;\n\t\tc += S[pos++]-'0';\n\t}\n\treturn c;\n}\n\nint main(){\n\tint n;\n\tcin>>n;\n\tint mp[321][321]={0};\n\tchar s[322]={};\n\tfor(int i=0;i<321;i++)s[i]=' ';\n\t\n\tfor(int i=0;i<n;i++){\n\t\tint x1,y1,x2,y2;\n\t\tcin>>x1>>y1>>x2>>y2;\n\t\tx1 += 20;\n\t\ty1 += 20;\n\t\tx2 += 20;\n\t\ty2 += 20;\n\t\t\n\t\tif(x1==x2){\n\t\t\tif( y1 > y2 ) swap(y1,y2);\n\t\t\tfor(int j=y1;j<=y2;j++){\n\t\t\t\tmp[x1][j]=1;\n\t\t\t}\n\t\t}else{\n\t\t\tif( x1 > x2 ) swap(x1,x2);\n\t\t\tfor(int j=x1;j<=x2;j++){\n\t\t\t\tmp[j][y1]=1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<ume_c.size();i++){\n\t\tfor(int j=0;j<=300;j++){\n\t\t\tfor(int k=0;k<=300;k++){\n\t\t\t\tint t=1;\n\t\t\t\tint minx = 1e9;\n\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\tminx = min(minx,k+m);\n\t\t\t\t\t\t\tif(mp[k+m][j+l]==0){\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\tt=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(t==1){\n\t\t\t\t\ts[minx]=ume_c[i];\n\t\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\t\tmp[k+m][j+l]=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tS = string(s);\n\tS.erase(remove(S.begin(),S.end(),' '),S.end());\n\tpos = 0;\n\tS += \"~\";\n\tcout<<expr()<<endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1632, "score_of_the_acc": -1.0241, "final_rank": 2 }, { "submission_id": "aoj_1529_890267", "code_snippet": "#include<iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\n\nstring ume_c=\"89605+32471-*\";\nstring ume[13][5]={\n {//8\n \"111\",\n \"101\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\n\t{\n \"111\",\n \"101\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"101\",\n \"101\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"000\",\n \"010\",\n \"111\",\n\t\"010\",\n \"000\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"100\",\n \"111\"\n },\n\t\n\t{\n \"101\",\n \"101\",\n \"111\",\n\t\"001\",\n \"001\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"001\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"000\",\n \"000\",\n \"111\",\n\t\"000\",\n \"000\"\n },\n\t{\n \"000\",\n \"000\",\n \"010\",\n\t\"000\",\n \"000\"\n }\n\t\n\t\n};\n\n\nstring S;\nint pos;\n\nint expr();\nint term();\nint num();\n\nint expr(){\n\tint res = term();\n\twhile(1){\n\t\tif(S[pos]=='+'){\n\t\t\tpos++;\n\t\t\tres += term();\n\t\t}else if(S[pos]=='-'){\n\t\t\tpos++;\n\t\t\tres -= term();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint term(){\n\tint res = num();\n\twhile(1){\n\t\tif(S[pos]=='*'){\n\t\t\tpos++;\n\t\t\tres *= num();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint num(){\n\tint c = S[pos++]-'0';\n\twhile('0' <= S[pos] && S[pos] <= '9'){\n\t\tc *= 10;\n\t\tc += S[pos++]-'0';\n\t}\n\treturn c;\n}\n\nint main(){\n\tint n;\n\tcin>>n;\n\tint mp[321][321]={0};\n\tchar s[322]={};\n\tfor(int i=0;i<321;i++)s[i]=' ';\n\t\n\tfor(int i=0;i<n;i++){\n\t\tint x1,y1,x2,y2;\n\t\tcin>>x1>>y1>>x2>>y2;\n\t\tx1 += 20;\n\t\ty1 += 20;\n\t\tx2 += 20;\n\t\ty2 += 20;\n\t\t\n\t\tif(x1==x2){\n\t\t\tif( y1 > y2 ) swap(y1,y2);\n\t\t\tfor(int j=y1;j<=y2;j++){\n\t\t\t\tmp[x1][j]=1;\n\t\t\t}\n\t\t}else{\n\t\t\tif( x1 > x2 ) swap(x1,x2);\n\t\t\tfor(int j=x1;j<=x2;j++){\n\t\t\t\tmp[j][y1]=1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<ume_c.size();i++){\n\t\tfor(int j=0;j<=300;j++){\n\t\t\tfor(int k=0;k<=300;k++){\n\t\t\t\tint t=1;\n\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\tif(mp[k+m][j+l]==0){\n\t\t\t\t\t\t\t\tt=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(t==1){\n\t\t\t\t\ts[k]=ume_c[i];\n\t\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\t\tmp[k+m][j+l]=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tS = string(s);\n\tS.erase(remove(S.begin(),S.end(),' '),S.end());\n\tpos = 0;\n\tcout<<expr()<<endl;\n}", "accuracy": 0.8888888888888888, "time_ms": 40, "memory_kb": 1628, "score_of_the_acc": -1.01, "final_rank": 4 }, { "submission_id": "aoj_1529_890265", "code_snippet": "#include<iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\n\nstring ume_c=\"89605+32471-*\";\nstring ume[13][5]={\n {//8\n \"111\",\n \"101\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\n\t{\n \"111\",\n \"101\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"101\",\n \"101\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"000\",\n \"010\",\n \"111\",\n\t\"010\",\n \"000\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"100\",\n \"111\"\n },\n\t\n\t{\n \"101\",\n \"101\",\n \"111\",\n\t\"001\",\n \"001\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"001\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"000\",\n \"000\",\n \"111\",\n\t\"000\",\n \"000\"\n },\n\t{\n \"000\",\n \"000\",\n \"010\",\n\t\"000\",\n \"000\"\n }\n\t\n\t\n};\n\n\nstring S;\nint pos;\n\nint expr();\nint term();\nint num();\n\nint expr(){\n\tint res = term();\n\twhile(1){\n\t\tif(S[pos]=='+'){\n\t\t\tpos++;\n\t\t\tres += term();\n\t\t}else if(S[pos]=='-'){\n\t\t\tpos++;\n\t\t\tres -= term();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint term(){\n\tint res = num();\n\twhile(1){\n\t\tif(S[pos]=='*'){\n\t\t\tpos++;\n\t\t\tres *= num();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint num(){\n\tint c = S[pos++]-'0';\n\twhile('0' <= S[pos] && S[pos] <= '9'){\n\t\tc *= 10;\n\t\tc += S[pos++]-'0';\n\t}\n\treturn c;\n}\n\nint main(){\n\tint n;\n\tcin>>n;\n\tint mp[321][321]={0};\n\tchar s[321];\n\tfor(int i=0;i<321;i++)s[i]=' ';\n\t\n\tfor(int i=0;i<n;i++){\n\t\tint x1,y1,x2,y2;\n\t\tcin>>x1>>y1>>x2>>y2;\n\t\tx1 += 20;\n\t\ty1 += 20;\n\t\tx2 += 20;\n\t\ty2 += 20;\n\t\t\n\t\tif(x1==x2){\n\t\t\tif( y1 > y2 ) swap(y1,y2);\n\t\t\tfor(int j=y1;j<=y2;j++){\n\t\t\t\tmp[x1][j]=1;\n\t\t\t}\n\t\t}else{\n\t\t\tif( x1 > x2 ) swap(x1,x2);\n\t\t\tfor(int j=x1;j<=x2;j++){\n\t\t\t\tmp[j][y1]=1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<ume_c.size();i++){\n\t\tfor(int j=0;j<=300;j++){\n\t\t\tfor(int k=0;k<=300;k++){\n\t\t\t\tint t=1;\n\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\tif(mp[k+m][j+l]==0){\n\t\t\t\t\t\t\t\tt=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(t==1){\n\t\t\t\t\ts[k]=ume_c[i];\n\t\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\t\tmp[k+m][j+l]=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tS = string(s);\n\tS.erase(remove(S.begin(),S.end(),' '),S.end());\n\tpos = 0;\n\tcout<<expr()<<endl;\n}", "accuracy": 0.8888888888888888, "time_ms": 50, "memory_kb": 1628, "score_of_the_acc": -1.0221, "final_rank": 5 }, { "submission_id": "aoj_1529_890261", "code_snippet": "#include<iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\n\nstring ume_c=\"89605+32471-*\";\nstring ume[13][5]={\n {//8\n \"111\",\n \"101\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\n\t{\n \"111\",\n \"101\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"101\",\n \"101\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"000\",\n \"010\",\n \"111\",\n\t\"010\",\n \"000\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"100\",\n \"111\"\n },\n\t\n\t{\n \"101\",\n \"101\",\n \"111\",\n\t\"001\",\n \"001\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"001\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"000\",\n \"000\",\n \"111\",\n\t\"000\",\n \"000\"\n },\n\t{\n \"000\",\n \"000\",\n \"010\",\n\t\"000\",\n \"000\"\n }\n\t\n\t\n};\n\n\nstring S;\nint pos;\n\nint expr();\nint term();\nint num();\n\nint expr(){\n\tint res = term();\n\twhile(1){\n\t\tif(S[pos]=='+'){\n\t\t\tpos++;\n\t\t\tres += term();\n\t\t}else if(S[pos]=='-'){\n\t\t\tpos++;\n\t\t\tres -= term();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint term(){\n\tint res = num();\n\twhile(1){\n\t\tif(S[pos]=='*'){\n\t\t\tpos++;\n\t\t\tres *= num();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint num(){\n\tint c = S[pos++]-'0';\n\twhile('0' <= S[pos] && S[pos] <= '9'){\n\t\tc *= 10;\n\t\tc += S[pos++]-'0';\n\t}\n\treturn c;\n}\n\nint main(){\n\tint n;\n\tcin>>n;\n\tint mp[221][221]={0};\n\tchar s[221];\n\tfor(int i=0;i<221;i++)s[i]=' ';\n\t\n\tfor(int i=0;i<n;i++){\n\t\tint x1,y1,x2,y2;\n\t\tcin>>x1>>y1>>x2>>y2;\n\t\tx1 += 20;\n\t\ty1 += 20;\n\t\tx2 += 20;\n\t\ty2 += 20;\n\t\t\n\t\tif(x1==x2){\n\t\t\tif( y1 > y2 ) swap(y1,y2);\n\t\t\tfor(int j=y1;j<=y2;j++){\n\t\t\t\tmp[x1][j]=1;\n\t\t\t}\n\t\t}else{\n\t\t\tif( x1 > x2 ) swap(x1,x2);\n\t\t\tfor(int j=x1;j<=x2;j++){\n\t\t\t\tmp[j][y1]=1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<ume_c.size();i++){\n\t\tfor(int j=0;j<=216;j++){\n\t\t\tfor(int k=0;k<=218;k++){\n\t\t\t\tint t=1;\n\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\tif(mp[k+m][j+l]==0){\n\t\t\t\t\t\t\t\tt=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(t==1){\n\t\t\t\t\ts[k]=ume_c[i];\n\t\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\t\tmp[k+m][j+l]=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tS = string(s);\n\tS.erase(remove(S.begin(),S.end(),' '),S.end());\n\tpos = 0;\n\tcout<<expr()<<endl;\n}", "accuracy": 0.8888888888888888, "time_ms": 20, "memory_kb": 1416, "score_of_the_acc": -0.2394, "final_rank": 3 }, { "submission_id": "aoj_1529_890260", "code_snippet": "#include<iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\n\nstring ume_c=\"89605+32471-*\";\nstring ume[13][5]={\n {//8\n \"111\",\n \"101\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\n\t{\n \"111\",\n \"101\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"101\",\n \"101\",\n\t\"101\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"100\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"000\",\n \"010\",\n \"111\",\n\t\"010\",\n \"000\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"001\",\n \"111\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"111\",\n\t\"100\",\n \"111\"\n },\n\t\n\t{\n \"101\",\n \"101\",\n \"111\",\n\t\"001\",\n \"001\"\n },\n\t\n\t{\n \"111\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"001\",\n \"001\",\n \"001\",\n\t\"001\",\n \"001\"\n },\n\t{\n \"000\",\n \"000\",\n \"111\",\n\t\"000\",\n \"000\"\n },\n\t{\n \"000\",\n \"000\",\n \"010\",\n\t\"000\",\n \"000\"\n }\n\t\n\t\n};\n\n\nstring S;\nint pos;\n\nint expr();\nint term();\nint num();\n\nint expr(){\n\tint res = term();\n\twhile(1){\n\t\tif(S[pos]=='+'){\n\t\t\tpos++;\n\t\t\tres += term();\n\t\t}else if(S[pos]=='-'){\n\t\t\tpos++;\n\t\t\tres -= term();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint term(){\n\tint res = num();\n\twhile(1){\n\t\tif(S[pos]=='*'){\n\t\t\tpos++;\n\t\t\tres *= num();\n\t\t}else{\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn res;\n}\n\nint num(){\n\tint c = S[pos++]-'0';\n\twhile('0' <= S[pos] && S[pos] <= '9'){\n\t\tc *= 10;\n\t\tc += S[pos++]-'0';\n\t}\n\treturn c;\n}\n\nint main(){\n\tint n;\n\tcin>>n;\n\tint flag[201][201]={0};\n\tint mp[221][221]={0};\n\tchar s[201];\n\tfor(int i=0;i<201;i++)s[i]=' ';\n\t\n\tfor(int i=0;i<n;i++){\n\t\tint x1,y1,x2,y2;\n\t\tcin>>x1>>y1>>x2>>y2;\n\t\tx1 += 20;\n\t\ty1 += 20;\n\t\tx2 += 20;\n\t\ty2 += 20;\n\t\t\n\t\tif(x1==x2){\n\t\t\tif( y1 > y2 ) swap(y1,y2);\n\t\t\tfor(int j=y1;j<=y2;j++){\n\t\t\t\tmp[x1][j]=1;\n\t\t\t}\n\t\t}else{\n\t\t\tif( x1 > x2 ) swap(x1,x2);\n\t\t\tfor(int j=x1;j<=x2;j++){\n\t\t\t\tmp[j][y1]=1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<ume_c.size();i++){\n\t\tfor(int j=0;j<=216;j++){\n\t\t\tfor(int k=0;k<=218;k++){\n\t\t\t\tint t=1;\n\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\tif(mp[k+m][j+l]==0){\n\t\t\t\t\t\t\t\tt=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(t==1){\n\t\t\t\t\ts[k]=ume_c[i];\n\t\t\t\t\tfor(int l=0;l<=4;l++){\n\t\t\t\t\t\tfor(int m=0;m<=2;m++){\n\t\t\t\t\t\t\tif(ume[i][l][m]=='1'){\n\t\t\t\t\t\t\t\tmp[k+m][j+l]=0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tS = string(s);\n\tS.erase(remove(S.begin(),S.end(),' '),S.end());\n\tpos = 0;\n\tcout<<expr()<<endl;\n}", "accuracy": 0.5185185185185185, "time_ms": 30, "memory_kb": 1416, "score_of_the_acc": -0.2515, "final_rank": 6 } ]
aoj_1533_cpp
Problem H: Caterpillar とある天空都市に住む大学生のGは、芋虫のいも太郎を飼っている。 彼は、いも太郎に最短歩数で全てのえさを順番に食べるように躾けをした。彼の友人であるあなたは、彼からいも太郎が本当に躾けられているかどうかを調べて欲しいと依頼されたので、プログラムを書くことにした。 Problem 複数の障害物とエサがグリッド状のエリア内に存在する。頭と 5 つの胴体を持つ芋虫がこのエリアを探索する。芋虫が順番に最後までエサを食べたときの最小の歩数を求めよ。 ただし全てのエサを食べることが不可能な場合は -1 を出力すること。なお、エサの上に芋虫の頭が重なったとき、エサを食べることができる。 エリアは以下の文字で構成される。 何もないマス -> ‘.’ 障害物 -> ‘#’ エサ -> ‘1’, ‘2’, ..., ‘9’ 芋虫の初期位置 -> 頭 ‘S’, 胴体 ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ 芋虫は頭 ’S’ から 5 つの胴体 ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ と 順に上下左右の 4 方向に隣接して、それらが連なって構成される。 例えば、芋虫の構成を表したとき、以下の (1) は正しい芋虫の構成であるが、 (2) や (3) のような構成はありえない。 (1) 正しい .bc Sad ..e (2) eが存在しない Sab .dc (3) "Sab" と "cde" とで芋虫が切断されている Sab cde (1) のような正しい芋虫の構成は、入力で与えられた状態から任意の回数の芋虫の移動の間、一貫して保たれる。 つまり、芋虫の頭が、隣接している 4 つのマスのうちのいずれかの空いているマスに一歩進むと、その後に続いて 1 番目の胴体は頭が元あったマスへ動き、2 番目の胴体は 1 番目の胴体が元あったマスへ、 3 番目は 2 番目の元へ‥… というように頭の軌跡をたどるように胴体が動く。以下は、1マス左へ動くときの例である。 移動前 移動後 ...... ...... ...bc. ...cd. ..Sad. .Sabe. ....e. ...... 芋虫の移動できる場所は以下のように定められている。 芋虫は自分の胴体、障害物、エリア外を移動することは出来ない。ただしエサの上を、そのエサを食べずに移動することは出来る。 移動前に胴体が進む先にあった場合でも、移動後に頭と胴体が重ならないならば移動できる。 Input H W N area 入力は H + 1 行で与えられる。1行目には 3つの整数 H , W , N ( 2 ≤ H ≤ 10, 2 ≤ W ≤ 10, 1 ≤ N ≤ 9 ) が書かれている。 H はエリアの高さ、 W は幅、 N がエサの個数を表す ( このとき 6 + N ≤ H × W が必ず成立する ) 。 2 行目から H + 1 行目までの各行には ´S´,´1´, 2´ , … ´9´,´a´,´b´,´c´,´d´,´e´,´#´,´.´ からなる W 文字の文字列が書かれており,各々がエリアの各区画の状態を表している。また ´S´,´a´,´b´,´c´,´d´,´e´ は芋虫の初期状態を表している。初期状態の芋虫に覆われるようにしてエサや障害物は存在しない。また、芋虫の初期位置およびエサの位置は正しく入力されることが保証されている。 Output 芋虫がエサを順番通りに食べたときの最小の歩数、またそれが不可能なら -1 を出力せよ。 Sample Input1 5 8 3 #....... #.####2# #.#.3..# #.###### .1Sabcde Sample Output1 14 Sample Input2 2 6 2 .1.baS .2.cde Sample Output2 7 Sample Input3 2 6 2 .1#baS .2.cde Sample Output3 -1
[ { "submission_id": "aoj_1533_893191", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef pair<int,int> pii;\n\nint H,W,N;\nchar field[51][51];\nconst int dy[]={-1,0,0,1};\nconst int dx[]={0,-1,1,0};\npii esas[11];\nint d[11][11][11][4][4][4][4][4];\n\nint getAng(int cy,int cx,int ny,int nx){\n if(ny-cy==-1)return 0;\n else if(nx-cx==-1)return 1;\n else if(nx-cx==1)return 2;\n return 3;\n}\n\npii calcNxtPoint(int cy,int cx,int ang){\n return pii(cy+dy[ang],cx+dx[ang]);\n}\n\nstruct Sit{\n int cost;\n int heady,headx;\n int pos;\n int ary[5];\n pii poses[5];\n Sit(int pos_,int hy,int hx,int a,int b,int c,int d,int e,int cost_){\n pos=pos_;\n heady=hy;\n headx=hx;\n ary[0]=a;\n ary[1]=b;\n ary[2]=c;\n ary[3]=d;\n ary[4]=e;\n cost=cost_;\n }\n Sit(){\n cost=0;\n pos=0;\n }\n void update_ary(){\n for(int i=0;i<5;i++){\n if(i==0)ary[i]=getAng(heady,headx,poses[0].first,poses[0].second);\n else ary[i]=getAng(poses[i-1].first,poses[i-1].second,poses[i].first,poses[i].second);\n }\n }\n bool operator<(const Sit &sit)const{\n return this->cost>sit.cost;\n }\n};\n\nint main(){\n\n cin>>H>>W>>N;\n Sit start_sit;\n for(int i=0;i<H;i++){\n for(int j=0;j<W;j++){\n cin>>field[i][j];\n if(field[i][j]=='S'){\n start_sit.heady=i;\n start_sit.headx=j;\n field[i][j]='.';\n }\n else if(field[i][j]>='a'&&field[i][j]<='e'){\n int pos=field[i][j]-'a';\n start_sit.poses[pos]=pii(i,j);\n field[i][j]='.';\n }\n else if(field[i][j]>='1'&&field[i][j]<='9'){\n int pos=field[i][j]-'1';\n esas[pos]=pii(i,j);\n }\n }\n }\n start_sit.update_ary();\n // dijkstra\n queue<Sit> pq;\n pq.push(start_sit);\n memset(d,0x7f,sizeof(d));\n d[start_sit.pos][start_sit.heady][start_sit.headx][start_sit.ary[0]][start_sit.ary[1]][start_sit.ary[2]][start_sit.ary[3]][start_sit.ary[4]]=0;\n while(pq.size()){\n Sit csit=pq.front();pq.pop();\n const int ccost=csit.cost;\n const int cpos=csit.pos;\n const int chy=csit.heady;\n const int chx=csit.headx;\n\n if(cpos==N){\n cout<<ccost<<endl;\n return 0;\n }\n \n int cary[5];\n for(int i=0;i<5;i++)\n cary[i]=csit.ary[i];\n pii bodys[5];\n // caryの情報から,頭以外の座標を復元\n for(int i=0;i<5;i++){\n if(i==0)bodys[i]=calcNxtPoint(chy,chx,cary[i]);\n else bodys[i]=calcNxtPoint(bodys[i-1].first,bodys[i-1].second,cary[i]);\n }\n for(int i=0;i<4;i++){\n int nhy=chy+dy[i];\n int nhx=chx+dx[i];\n // 頭が動けるという条件\n if(nhy>=0&&nhx>=0&&nhy<H&&nhx<W&&field[nhy][nhx]!='#'){\n // bodyを次の場所へ動かし、条件を満たすか調べる\n // 頭の座標とbodyの座標が一致してはいけない\n pii nbodys[5];\n nbodys[0]=pii(chy,chx);\n for(int i=1;i<5;i++)nbodys[i]=bodys[i-1];\n bool isHead=false;\n for(int i=0;i<5;i++){\n if(nbodys[i].first==nhy&&nbodys[i].second==nhx)\n isHead=true;\n }\n if(isHead)continue;\n // 頭の座標が大丈夫なら,次は頭に餌があるかどうかを調べる\n int npos=cpos;\n if(esas[cpos]==pii(nhy,nhx))npos++;\n int ncost=ccost+1;\n Sit nsit;\n nsit.pos=npos;\n nsit.heady=nhy;\n nsit.headx=nhx;\n nsit.cost=ncost;\n for(int i=0;i<5;i++)\n nsit.poses[i]=nbodys[i];\n nsit.update_ary();\n int nary[5];\n for(int i=0;i<5;i++)\n nary[i]=nsit.ary[i];\n // ここで、コストを更新できるか調べる\n if(d[npos][nhy][nhx][nary[0]][nary[1]][nary[2]][nary[3]][nary[4]]>ncost){\n d[npos][nhy][nhx][nary[0]][nary[1]][nary[2]][nary[3]][nary[4]]=ncost;\n pq.push(nsit);\n }\n }\n }\n }\n\n cout<<-1<<endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6744, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_1533_890557", "code_snippet": "#include <iostream>\n#include <queue>\n#include <cassert>\n#include <map>\n#include <vector>\nusing namespace std;\n\nchar c[12][12];\n\nint dx[] = {-1,0,1,0};\nint dy[] = {0,1,0,-1};\n\nint memo[12][12][12][1024];\n\nstruct NODE{\n\tint next;\n\tint x,y,prev;\n\tint cost;\n\tNODE(int x,int y,int prev,int next,int cost) : x(x) , y(y) , prev(prev) , next(next), cost(cost) {}\n};\n\nint dir(pair<int,int> a,pair<int,int> b){\n\tint ddx = a.first - b.first;\n\tint ddy = a.second - b.second;\n\tif( ddx == 0 ) return ddy < 0 ? 3 : 1;\n\telse if( ddy == 0 ) return ddx < 0 ? 0 : 2;\n\telse assert(0);\n}\n\nint atamaga[12][12];\nint main(){\n\tfor(int i = 0 ; i < 12 ; i++)\n\t\tfor(int j = 0 ; j < 12 ; j++)\n\t\t\tc[i][j] = '#';\n\n\tint H,W,N;\n\tcin >> H >> W >> N;\n\tfor(int i = 0 ; i < H ; i++){\n\t\tfor(int j = 0 ; j < W ; j++){\n\t\t\tcin >> c[i+1][j+1];\n\t\t}\n\t}\n\tmap<char,pair<int,int> >tmp;\n\tfor(int i = 1 ; i <= H ; i++){\n\t\tfor(int j = 1 ; j <= W ; j++){\n\t\t\ttmp[c[i][j]] = make_pair(j,i);\n\t\t}\n\t}\n\tstring table = \"edcbaS\";\n\tint initdir = 0;\n\tfor(int i = 0 ; i+1 < table.size() ; i++){\n\t\tinitdir = initdir * 4 + dir(tmp[table[i+1]],tmp[table[i]]);\n\t}\n\tfor(int i = 0 ; i < table.size() ; i++){\n\t\tint x = tmp[table[i]].first;\n\t\tint y = tmp[table[i]].second;\n\t\tc[y][x] = '.';\n\t}\n\tfor(int i = 1 ; i <= N ; i++){\n\t\tint x = tmp['0'+i].first;\n\t\tint y = tmp['0'+i].second;\n\t\tatamaga[y][x] = i;\n\t\tc[y][x] = '.';\n\t}\n\tqueue<NODE> Q;\n\tQ.push(NODE(tmp['S'].first,tmp['S'].second,initdir,1,0));\n\twhile(Q.size()){\n\t\tNODE q = Q.front(); Q.pop();\n\t\tif( memo[q.y][q.x][q.next][q.prev]++ ) continue;\n\t\tif( q.next > N ){\n\t\t\tcout << q.cost << endl;\n\t\t\treturn 0;\n\t\t}\n\t\tvector< pair<int,int> > dont;\n\t\tint tx = q.x , ty = q.y;\n\t\tint oo = q.prev;\n\t\tdont.push_back(make_pair(tx,ty));\n\t\tfor(int i = 0 ; i < 4 ; i++){\n\n\t\t\tint dirdir = (oo+2) % 4;\n\t\t\too /= 4;\n\t\t\ttx += dx[dirdir];\n\t\t\tty += dy[dirdir];\n\t\t\tdont.push_back(make_pair(tx,ty));\n\t\t}\n\t\t\n\t\tfor(int i = 0 ; i < 4 ; i++){\n\t\t\tint tx = q.x + dx[i] , ty = q.y + dy[i];\n\t\t\tif( c[ty][tx] == '#' ) continue;\n\t\t\tint flag = 1;\n\t\t\tfor(int j = 0 ; j < dont.size() ; j++)\n\t\t\t\tif( dont[j] == make_pair(tx,ty) ){\n\t\t\t\t\tflag = 0; break;\n\t\t\t\t}\n\t\t\tif(!flag) continue;\n\t\t\tQ.push(NODE(tx,ty,(q.prev*4+(i))%1024,q.next+(q.next==atamaga[ty][tx]),q.cost+1));\n\t\t}\n\t\t\n\t}\n\tcout << -1 << endl;\n\t\n\t\n\t\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6368, "score_of_the_acc": -0.8433, "final_rank": 1 }, { "submission_id": "aoj_1533_890478", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\n#ifdef LOCAL\n#include \"local.h\"\n#endif\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\n\nint main()\n{\n int h, w, n;\n cin >> h >> w >> n;\n char c[16][16];\n rep(y, h)\n cin >> c[y];\n\n vector<pint> start_imo(6);\n const char* imoimo = \"Sabcde\";\n rep(y, h) rep(x, w)\n {\n if (isalpha(c[y][x]))\n start_imo[strchr(imoimo, c[y][x]) - imoimo] = pint(x, y);\n }\n\n int res = -1;\n map<pair<int, vector<pint>>, int> dp;\n queue<pair<int, vector<pint>>> q;\n q.push(make_pair(0, start_imo));\n while (!q.empty())\n {\n const int esa = q.front().first;\n const vector<pint> imo = q.front().second;\n const int ncost = dp[q.front()] + 1;\n q.pop();\n assert(imo.size() == 6);\n\n rep(dir, 4)\n {\n int nx = imo[0].first + dx[dir];\n int ny = imo[0].second + dy[dir];\n if (in_rect(nx, ny, w, h) && c[ny][nx] != '#' &&\n count(imo.begin() + 1, imo.begin() + 5, pint(nx, ny)) == 0)\n {\n vector<pint> nimo;\n nimo.push_back(pint(nx, ny));\n rep(i, 5)\n nimo.push_back(imo[i]);\n\n int nesa = esa;\n if (c[ny][nx] - '1' == esa)\n ++nesa;\n\n if (nesa == n)\n {\n res = ncost;\n goto end;\n }\n\n auto next = make_pair(nesa, nimo);\n if (!dp.count(next))\n {\n dp[next] = ncost;\n q.push(next);\n }\n }\n }\n }\nend:;\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 5968, "score_of_the_acc": -1.1211, "final_rank": 3 }, { "submission_id": "aoj_1533_890452", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\n#ifdef LOCAL\n#include \"local.h\"\n#endif\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\n\nint main()\n{\n int h, w, n;\n cin >> h >> w >> n;\n char c[16][16];\n rep(y, h)\n cin >> c[y];\n\n vector<pint> start_imo(6);\n const char* imoimo = \"Sabcde\";\n rep(y, h) rep(x, w)\n {\n if (isalpha(c[y][x]))\n start_imo[strchr(imoimo, c[y][x]) - imoimo] = pint(x, y);\n }\n\n int res = -1;\n map<pair<int, vector<pint>>, int> dp;\n queue<pair<int, vector<pint>>> q;\n q.push(make_pair(0, start_imo));\n while (!q.empty())\n {\n int esa = q.front().first;\n vector<pint> imo = q.front().second;\n int ncost = dp[q.front()] + 1;\n q.pop();\n\n rep(dir, 4)\n {\n int nx = imo[0].first + dx[dir];\n int ny = imo[0].second + dy[dir];\n if (in_rect(nx, ny, w, h) && c[ny][nx] != '#' &&\n find(imo.begin() + 1, imo.begin() + 5, pint(nx, ny)) == imo.begin() + 5)\n {\n vector<pint> nimo;\n nimo.push_back(pint(nx, ny));\n for (int i = 0; i < 5; ++i)\n nimo.push_back(imo[i]);\n\n int nesa = esa;\n if (c[ny][nx] - '1' == esa)\n ++esa;\n\n if (esa == n)\n {\n res = ncost;\n goto end;\n }\n\n auto next = make_pair(esa, nimo);\n if (!dp.count(next))\n {\n dp[next] = ncost;\n q.push(next);\n }\n }\n }\n }\nend:;\n cout << res << endl;\n}", "accuracy": 0.07142857142857142, "time_ms": 40, "memory_kb": 4344, "score_of_the_acc": -0.3333, "final_rank": 5 }, { "submission_id": "aoj_1533_890399", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\n#define F first\n#define S second\n\nint h, w, a;\nstring in[10];\n\ninline bool ok(const vi &x, const vi& y){\n\trep(i, 6) rep(j, i) if(x[i] == x[j] && y[i] == y[j]) return 0;\n\treturn 1;\n}\n\nint main(){\n\t\n\tcin >> h >> w >> a;\n\t\n\tvi gy(a), gx(a);\n\tvi py(6), px(6);\n\t\n\trep(i, h){\n\t\tcin >> in[i];\n\t\t\n\t\trep(j, w){\n\t\t\tchar c = in[i][j];\n\t\t\t\n\t\t\tif(isalpha(c)){\n\t\t\t\t\n\t\t\t\tif(c == 'S') py[0] = i, px[0] = j;\n\t\t\t\telse py[c - 'a' + 1] = i, px[c - 'a' + 1] = j;\n\t\t\t}\n\t\t\tif(isdigit(c)) gy[c - '1'] = i, gx[c - '1'] = j;\n\t\t}\n\t}\n\t\n\tset<pair<int, pair<vi, vi> > > s; //esa, pos;\n\tqueue<pair<pi, pair<vi, vi> > > q; //cost, esa, pos\n\t\n\ts.insert(mp(0, mp(py, px)));\n\tq.push(mp(mp(0, 0), mp(py, px)));\n\t\n\twhile(!q.empty()){\n\t\t\n\t\tint cost = q.front().F.F;\n\t\tint esa = q.front().F.S;\n\t\tvi ys = q.front().S.F;\n\t\tvi xs = q.front().S.S;\n\t\t\n\t\tq.pop();\n\t\tcost++;\n\t\t\n\t\trep(d, 4){\n\t\t\tconst int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};\n\t\t\tint hy = ys[0] + dy[d], hx = xs[0] + dx[d];\n\t\t\t\n\t\t\tif(hy < 0 || hy >= h || hx < 0 || hx >= w) continue;\n\t\t\tif(in[hy][hx] == '#') continue;\n\t\t\t\n\t\t\tvi ny, nx;\n\t\t\tny.pb(hy); nx.pb(hx);\n\t\t\trep(i, 5) ny.pb(ys[i]), nx.pb(xs[i]);\n\t\t\t\n\t\t\tif(!ok(ny, nx)) continue;\n\t\t\t\n\t\t\tint nesa = esa;\n\t\t\tif(in[hy][hx] == '1' + esa){\n\t\t\t\tif(++nesa == a){\n\t\t\t\t\tcout << cost << endl;\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif(s.count(mp(nesa, mp(ny, nx)))) continue;\n\t\t\ts.insert(mp(nesa, mp(ny, nx)));\n\t\t\tq.push(mp(mp(cost, nesa), mp(ny, nx)));\n\t\t}\n\t}\n\tcout << -1 << endl;\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 6440, "score_of_the_acc": -1.8733, "final_rank": 4 } ]
aoj_1528_cpp
Problem C: Warping Girl Problem 会津魔法学校は, 魔法を使える者が集まる学校である. その学校の生徒の一人であるハルカは, 魔法陣の上でワープの魔法を使う事ができる. 彼女の家から学校には, 長さ L の一直線状の道がある. また, この道には所々魔法陣が描かれている. 彼女は毎日この道を使って学校に通っているため, できるだけ短い時間で学校に辿り着きたいと思っている. そこでプログラマーであるあなたは, 彼女に学校まで行くときにかかる最小の時間を教えてあげることにした. ハルカは, 1 分で距離 1 だけ歩いて先に進むことができる ( *戻ることはできない).また, ワープの魔法陣が書いてある位置 P i で魔法を唱えることで, P i からちょうど D i だけ進んだ場所に T i 分で移動することができる. 移動先に魔法陣がある場合も, 連続してワープ魔法を使用することができる. 彼女の家の位置は 0 、学校の位置は L である. Input L n P 1 D 1 T 1 P 2 D 2 T 2 . . P n D n T n 1行目に道の長さ L , 魔法陣の数 n が与えられる. 次に n 個の魔法陣の状態が与えられる. P i は i 番目の魔法陣がある位置, D i は i 番目の魔法陣からワープする距離, T i は i 番目の魔法陣を使ってワープしたときにかかる時間を表す. Constraints 入力は以下の条件を満たす. 与えられる数は全て整数である 1 ≤ L ≤ 10 9 0 ≤ n ≤ min(10 3 , L) ( min は A, B の小さい方を表す) 0 ≤ P i ≤ L - 1 0 ≤ D i ≤ L - P i 0 ≤ T i ≤ 10 3 P i ≠ P j Output 学校に辿り着くための最小の時間を一行に出力せよ. Sample Input 1 10 3 2 2 1 4 2 1 8 1 1 Sample Output 1 8 Sample Input 2 10 2 2 3 1 3 5 1 Sample Output 2 6 Sample Input 3 1 0 Sample Output 3 1
[ { "submission_id": "aoj_1528_5851546", "code_snippet": "#include<bits/stdc++.h>\n\nusing Int = long long;\nusing namespace std;\n\nconstexpr Int inf = 1e10;\n\nint main(){\n Int L,N;\n cin >> L >> N;\n vector<tuple<Int,Int,Int>>edge;\n for(int i = 0; i < N; ++i){\n Int p,d,t;\n cin >> p >> d >> t;\n edge.emplace_back(p,d,t);\n }\n sort(edge.begin(),edge.end());\n map<Int,Int>dp; dp[0] = 0;\n if(edge.size() == 0){\n return cout << L << endl,0;\n }\n auto [fp,fd,ft] = edge.front();\n \n dp[fp] = fp;\n dp[fp + fd] = min(fp + fd, dp[fp] + ft);\n \n for(int i = 1; i < edge.size(); ++i){\n auto[p,d,t] = edge[i];\n for(int j = 0; j < i; ++j){\n auto[jp,jd,jt] = edge[j];\n Int cost = dp[jp],diff = p - jp;\n if(dp.find(p) == dp.end()){\n dp[p] = min(p,cost + diff);\n } else {\n dp[p] = min({dp[p],p,cost + diff});\n }\n if(jp + jd > p) continue;\n // guarantee that has visited jp + jd\n cost = dp[jp + jd], diff = p - jp - jd;\n dp[p] = min(dp[p],cost + diff);\n }\n dp[p] = min(dp[p],p);\n if(dp.find(p + d) == dp.end()){\n dp[p + d] = min(p + d, dp[p] + t);\n } else {\n dp[p + d] = min({dp[p + d],p + d, dp[p] + t});\n }\n }\n Int ans = L;\n for(auto itr : dp){\n ans = min(ans, itr.second +L - itr.first);\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3632, "score_of_the_acc": -0.1998, "final_rank": 2 }, { "submission_id": "aoj_1528_893932", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <iomanip>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <utility>\n#include <numeric>\n#include <algorithm>\n#include <functional>\n#include <cctype>\n#include <complex>\n#include <string>\n#include <sstream>\n#include <cassert>\nusing namespace std;\n\n//common\ntypedef int i32;\ntypedef long long i64,ll;\n#define BR \"\\n\"\n\n#define ALL(c) (c).begin(),(c).end()\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n\ntypedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;\ntypedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;\ntypedef vector<string> vs; typedef long double ld;\n\n//config\n#define MODE_DEBUG\n//#define INF 1<<30\n//#define EPS 1e-8\n//const ll MOD =100000007;\n\n//debug\n#ifdef NDEBUG\n#define DUMP(x)\n#define DUMPLN(x)\n#define DEBUG(x)\n#define DEBUGLN(x)\n#define LINE()\n#define LINELN()\n#define CHECK(exp,act)\n#define STOP(e)\n#else\n#define DUMP(x) cerr << #x << \" = \" << (x)\n#define DUMPLN(x) DUMP(x) <<endl\n#define DEBUG(x) DUMP(x) << LINE() << \" \" << __FILE__\n#define DEBUGLN(x) DEBUG(x)<<endl\n#define LINE() cerr<< \" (L\" << __LINE__ << \")\"\n#define LINELN() LINE()<<endl\n#define CHECK(exp,act) if(exp!=act){DUMPLN(exp);DEBUGLN(act);}\n#define STOP(e) CHECK(e,true);if(!(e)) exit(1);\n#endif\n\ntemplate<class T> inline string toString(const vector<T>& x) {\n\tstringstream ss;\n\tREP(i,x.size()){\n\t\tif(i!=0)ss<<\" \";\n\t\tss<< x[i];\n\t}\n\treturn ss.str();\n}\ntemplate<class T> inline string toString(const vector<vector<T> >& map) {\n\tstringstream ss;\n\tREP(i,map.size()){\n\t\tif(i!=0)ss<<BR;\n\t\tss<< toString(map[i]);\n\t}\n\treturn ss.str();\n}\ntemplate<class K,class V> string toString(map<K,V>& x) {\n\tstring res;stringstream ss;\n\tfor(auto& p:x)ss<< p.first<<\":\" << p.second<<\" \";\n\treturn ss.str();\n}\n\ntemplate<typename T,typename V> inline T mod(T v,V MOD){\n\treturn (v%MOD+MOD)%MOD;\n}\n\n#define nextInt(n) scanf(\"%d\",&n)\n#define nextLong(n) scanf(\"%lld\",&n)\n#define nextDouble(n) scanf(\"%lf\",&n)\n\nnamespace ShortestPath{\n\n typedef ll Cost;\n const Cost CINF=1LL<<58;\n typedef vector<vector<Cost> > Mat;\n struct Edge{\n int from,to;Cost cost;\n Edge(int from,int to,Cost cost)\n : from(from),to(to),cost(cost) {};\n };\n ostream& operator <<(ostream& os,const Edge& e){\n os<<\"(\"<<e.from<<\"->\"<<e.to<<\")\";\n return os;\n }\n typedef vector<vector<Edge> > Graph;\n\n struct Task{\n int prev,pos,cost;\n Task(int prev,int pos,int cost)\n :prev(prev),pos(pos),cost(cost){};\n bool operator>(const Task& r) const{\n return cost>r.cost;\n }\n };\n\n vector<Cost> dijkstra(const Graph& g,const int s,vector<int>& prev){\n const int V=g.size();\n vector<Cost> d(V,CINF);d[s]=0;\n fill(ALL(prev), -2);\n priority_queue<Task,vector<Task>,greater<Task> > que;\n que.push(Task(-1,s,0));\n while(!que.empty()){\n Task task=que.top();que.pop();\n if(d[task.pos]<task.cost)continue;\n EACH(e,g[task.pos]){\n if(d[e->to]>d[e->from]+e->cost){\n d[e->to]=d[e->from]+e->cost;\n que.push(Task(e->from,e->to,d[e->to]));\n }\n } \n }\n return d;\n }\n vector<Cost> dijkstra(const Graph& g,const int s){\n vector<int> prev(g.size());return dijkstra(g,s,prev);\n }\n\n}\nusing namespace ShortestPath;\n\n\n\nll INF=1LL<<58;\nclass Main{\npublic:\t\n\tvoid run(){\n\t\tint L,n;cin >> L >>n;\n\t\tmap<int,int> xs;int ind=0;\n\t\txs[0]=ind++;xs[L]=ind++;\n\t\tint i=0;\n\t\tvector<pair<int,pair<int,int> > > dts(n);\n\t\tREP(i,n){\n\t\t\tint p,d,t;cin >> p >> d >> t;\n\t\t\tif(xs.count(p)==0)xs[p]=ind++;\n\t\t\tif(xs.count(p+d)==0)xs[p+d]=ind++;\n\t\t\tdts[i]=make_pair(p,make_pair(p+d,t));\n\t\t}\n\t\tGraph g(xs.size());\n\t\tEACH(it,xs)EACH(it2,xs)if(it2->first>it->first){\n\t\t\tg[it->second].push_back(Edge(it->second,it2->second,(ll)it2->first-it->first));\n\t\t}\n\t\tREP(i,n){\n\t\t\tg[xs[dts[i].first]].push_back(Edge(xs[dts[i].first],xs[dts[i].second.first],dts[i].second.second));\n\t\t}\n\n\t\tcout << dijkstra(g,0)[1] <<endl;\n\t}\n};\n int main(){\n \tcout <<fixed<<setprecision(15);\n\tios::sync_with_stdio(false);\n \tMain().run();\n \treturn 0;\n }", "accuracy": 1, "time_ms": 170, "memory_kb": 49120, "score_of_the_acc": -1.4, "final_rank": 5 }, { "submission_id": "aoj_1528_893407", "code_snippet": "#include<cstdio>\n#include<cstring>\n#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\n#include<map>\nusing namespace std;\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\ntypedef long long ll;\n#define F first\n#define S second\nPP K[1111];\nint L;\nint n;\nll dp[1111];\nint next(int l){\n int st=0,ed=n;\n int ret = -1;\n if( l > L ) return -1;\n while( st<=ed ){\n int h = (st+ed)/2;\n if( K[h].F > l ){\n ed=h-1;\n ret = h;\n }\n else if(K[h].F < l )\n st = h+1;\n else\n return h;\n }\n if( ret == -1 ){\n return n;\n }\n return ret;\n}\nint solve(int id){\n if( dp[id] != -1 ) return dp[id];\n if( id == n ) return 0;\n int nel = K[id].F+K[id].S.F;\n int nex = next(nel);\n ll res;\n if( nex == -1 ){\n res = solve(id+1)+(K[id+1].F - K[id].F);\n } else {\n res = min( solve(id+1)+(K[id+1].F - K[id].F),\n\t solve(nex)+(K[nex].F - nel) + K[id].S.S);\n }\n // printf(\"%d - %d = %d\\n\",K[nex].F,nel,K[nex].F-nel);\n // printf(\"%d %d %d\\n\",id,K[id].F,res);\n return dp[id] = res;\n}\n\nint main(){\n cin >> L >> n;\n memset(dp,-1,sizeof(dp));\n for(int i=0;i<n;i++){\n int p,d,t;\n cin >> K[i].F >> K[i].S.F >> K[i].S.S;\n }\n sort(K,K+n);\n K[n].F = L;\n cout << solve(0)+K[0].F << endl;\n\n}", "accuracy": 0.3409090909090909, "time_ms": 10, "memory_kb": 9304, "score_of_the_acc": -0.1683, "final_rank": 8 }, { "submission_id": "aoj_1528_892417", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <unordered_map>\n#include <vector>\n#include <tuple>\nusing namespace std;\ntypedef long long int ll;\n\n\n// [pos][from] -> cost\nunordered_map<int, vector<tuple<int, int>>> field;\n\nunordered_map<int, int> memo;\nint solve(int p) {\n\tif (p == 0) return 0;\n\tauto it = memo.find(p);\n\tif (it != memo.end()) {\n\t\treturn it->second;\n\t}\n\tint res = p;\n\tfor (auto nx : field[p]) {\n\t\tres = min(res, solve(get<0>(nx)) + get<1>(nx));\n\t}\n\treturn memo[p] = res;\n}\n\nint main() {\n\tll L;\n\tint n;\n\twhile (cin >> L >> n) {\n\t\tmemo.clear(); field.clear();\n\t\tvector<int> points;\n\t\tpoints.push_back(0);\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tint p, d, t;\n\t\t\tcin >> p >> d >> t;\n\t\t\tfield[p + d].push_back(make_tuple(p, t));\n\t\t\tpoints.push_back(p);\n\t\t\tpoints.push_back(p + d);\n\t\t}\n\t\tpoints.push_back(L);\n\t\tsort(points.begin(), points.end());\n\t\tpoints.erase(unique(points.begin(), points.end()), points.end());\n\t\tfor (int i = 1; i < points.size(); ++i) {\n\t\t\tconst int pos = points[i];\n\t\t\tconst int pre = points[i - 1];\n\t\t\tfield[pos].push_back(make_tuple(pre, pos - pre));\n\t\t}\n\t\t// for (auto f : field) {\n\t\t// \tcout << f.first << \": \";\n\t\t// \tfor (auto p : f.second) cout << '(' << get<0>(p)<<','<<get<1>(p) << \") \";\n\t\t// \tcout << endl;\n\t\t// }\n\t\tcout << solve(L) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.3409090909090909, "time_ms": 10, "memory_kb": 9456, "score_of_the_acc": -0.1715, "final_rank": 9 }, { "submission_id": "aoj_1528_890689", "code_snippet": "#include <iostream>\n#include <map>\n#include <algorithm>\nusing namespace std;\n\nmap<int,int> dp;\nint l,n;\nint p[1001],d[1001],t[1001];\n\nint rec(int now,int sum){\n if(now == l) return sum;\n if((dp[now] != '\\0' && dp[now] < sum)) return dp[now]; \n int ret = 2 << 28;\n for(int i=0;i<n;i++){\n if(now == p[i]){ret = min(rec(now+d[i],sum+t[i]),ret); break; }\n }\n ret = min(rec(now+1,sum+1),ret);\n \n return dp[now] = ret;\n}\n\nint main(){\n cin >> l >> n;\n for(int i=0;i<n;i++) cin >> p[i] >> d[i] >> t[i];\n cout << rec(0,0) << endl;;\n}", "accuracy": 0.09090909090909091, "time_ms": 30, "memory_kb": 13408, "score_of_the_acc": -0.304, "final_rank": 10 }, { "submission_id": "aoj_1528_890273", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\n\nusing namespace std;\n\ninline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}\ntemplate<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\n\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define pb push_back\n#define mp make_pair\n#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define exist(s,e) ((s).find(e)!=(s).end())\n#define range(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) range(i,0,n)\n#define clr(a,b) memset((a), (b) ,sizeof(a))\n#define dump(x) cerr << #x << \" = \" << (x) << endl;\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n\nconst double eps = 1e-10;\nconst double pi = acos(-1.0);\nconst ll INF =1LL << 62;\nconst int inf =1 << 29;\n\nmap<int,int> dp;\nvi maho;\n\nstruct state{\n\tint p;\n\tint d;\n\tint t;\n\tbool operator <(state a)const{ return p < a.p; }\n\tbool operator >(state a)const{ return p > a.p; } \n};\n\nvector<state> m;\n\nint main(void){\n\tint l,n;\n\tcin >> l >> n;\n\trep(i,n){\n\t\tstate tmp;\n\t\tcin >> tmp.p >> tmp.d >> tmp.t;\n\t\tm.pb(tmp);\n\t}\n\t\n\tsort(m.begin(),m.end());\n\trep(i,n){\n\t\tint p,d,t;\n\t\tp=m[i].p;\n\t\td=m[i].d;\n\t\tt=m[i].t;\n\t\tif(dp.find(p)!=dp.end())\n\t\t\tdp[p]=min(dp[p],p);\n\t\telse\n\t\t\tdp[p]=p;\n\t\trep(j,maho.size()){\n\t\t\tint rec=maho[j];\n\t\t\tif(rec<=p && dp.find(rec)!=dp.end())\n\t\t\t\tdp[p]=min(dp[p],dp[rec]+p-rec);\n\t\t}\n\t\tif(dp.find(p+d)!=dp.end())\n\t\t\tdp[p+d]=min(dp[p+d], min(dp[p]+t,p+d) );\n\t\telse\n\t\t\tdp[p+d]=min(dp[p]+t,p+d);\n\t\tmaho.pb(p);\n\t\tmaho.pb(p+d);\n\t//\tcout << p << \" \" << dp[p] << endl;\n\t//\tcout << p+d << \" \" << dp[p+d] << endl;\n\t}\n\tint ans=l;\n\trep(i,maho.size()){\n\t\tint rec=maho[i];\n\t\trec=l-rec+dp[rec];\n\t\tans=min(rec,ans);\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 1336, "score_of_the_acc": -0.2768, "final_rank": 3 }, { "submission_id": "aoj_1528_890244", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <climits>\n#include <sstream>\n#include <functional>\n#include <complex>\n\nusing namespace std;\n\n#define len(array) (sizeof (array) / sizeof *(array))\n#define rep(i, s, e) for(int i = s;i < e;i++)\n#define Rep(i, e) for(int i = 0;i < e;i++)\n#define rrep(i, e, s) for(int i = e;s <= i;i--)\n#define Rrep(i, e) for(int i = e;0 <= i;i--)\n#define mrep(i, e, t1, t2) for(map<t1, t2>::iterator i = e.begin(); i != e.end(); i++)\n#define vrange(v) v.begin(), v.end()\n#define vrrange(v) v.rbegin(), v.rend()\n#define vsort(v) sort(vrange(v))\n#define vrsort(v) sort(vrrange(v))\n#define arange(a) a, a + len(a)\n#define asort(a) sort(arange(a))\n#define arsort(a, t) sort(arange(a), greater<t>())\n#define afill(a, v) fill(arange(a), v)\n#define afill2(a, v, t) fill((t *)a, (t *)(a + len(a)), v)\n#define fmax(a, b) (a < b? b : a)\n#define fmin(a, b) (a > b? b : a)\n#define fabs(a) (a < 0? -a : a)\n#define pb push_back\n#define rg(e, s, t) s <= e && e < t\n#define PQDecl(name, tp) priority_queue< tp, vector<tp>, greater<tp> > name;\n//#define X real()\n//#define Y imag()\n//typedef unsigned int ui;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n//typedef complex<double> p;\nconst int INF = (int)2e9;\nconst int MOD = (int)1e9 + 7;\nconst double EPS = 1e-10;\n//const int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};\n//const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};\n//const ll weight[] = {1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13};\n#define MAX_N 1000\n\ntypedef struct _Node {\n _Node(int arg1 = 0, int arg2 = 0 , int arg3 = 0) {\n\ti = arg1;\n\tj = arg2;\n\tk = arg3;\n }\n int i,j,k;\n bool operator <(const struct _Node &e) const{\n return i == e.i? (j == e.j? k < e.k : j < e.j) : i < e.i;\n }\n bool operator >(const struct _Node &e) const{\n return i == e.i? (j == e.j? k > e.k : j > e.j) : i > e.i;\n }\n}node;\n\nvoid doIt(){\n int l, n;\n node magic[MAX_N];\n cin >> l >> n;\n Rep(i, n){\n cin >> magic[i].i >> magic[i].j >> magic[i].k;\n }\n sort(magic, magic + n);\n map<ll, ll> mmap[2];\n int prev = 1, now = 0;\n mmap[prev][0] = 0;\n Rep(i, n){\n mmap[now].clear();\n mrep(it, mmap[prev], ll, ll){\n ll e = (*it).first, time = (*it).second;\n // printf(\"i = %d, e = %lld, time = %lld\\n\", i, e, time);\n //先にいたら\n if(magic[i].i < e){\n if(mmap[now].count(e)){\n mmap[now][e] = fmin(mmap[now][e], time);\n }\n else{\n mmap[now][e] = time;\n }\n }\n else{\n ll dist = magic[i].i + magic[i].j;\n //warp\n if(mmap[now].count(dist)){\n mmap[now][dist] = fmin(mmap[now][dist], time + magic[i].i - e + magic[i].k);\n }\n else{\n mmap[now][dist] = time + magic[i].i - e + magic[i].k;\n }\n //not warp\n if(mmap[now].count(magic[i].i)){\n mmap[now][magic[i].i] = fmin(mmap[now][magic[i].i], time + magic[i].i - e);\n }\n else{\n mmap[now][magic[i].i] = time + magic[i].i - e;\n }\n }\n }\n prev = 1 - prev;\n now = 1 - now;\n }\n ll ans = l;\n mrep(it, mmap[prev], ll, ll){\n ans = fmin(ans, (*it).second + l - (*it).first);\n }\n cout << ans << endl;\n}\n\nint main() {\n doIt();\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1248, "score_of_the_acc": -0.025, "final_rank": 1 }, { "submission_id": "aoj_1528_890241", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#include <deque>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\n\nusing namespace std;\n\ninline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}\ntemplate<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\ntypedef long long ll;\n\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define pb push_back\n#define mp make_pair\n#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define exist(s,e) ((s).find(e)!=(s).end())\n#define range(i,a,b) for(int i=(a);i<(b);++i)\n#define rep(i,n) range(i,0,n)\n#define clr(a,b) memset((a), (b) ,sizeof(a))\n#define dump(x) cerr << #x << \" = \" << (x) << endl;\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" << __FILE__ << endl;\n\nconst double eps = 1e-10;\nconst double pi = acos(-1.0);\nconst ll INF =1LL << 62;\nconst int inf =1 << 29;\n\nmap<int,int> dp;\nvi maho;\n\nint main(void){\n\tint l,n;\n\tcin >> l >> n;\n\trep(i,n){\n\t\tint p,d,t;\n\t\tcin >> p >> d >> t;\n\t\t\n\t\tif(dp.find(p)!=dp.end())\n\t\t\tdp[p]=min(dp[p],p);\n\t\telse\n\t\t\tdp[p]=p;\n\t\trep(j,maho.size()){\n\t\t\tint rec=maho[j];\n\t\t\tif(rec<=p && dp.find(rec)!=dp.end())\n\t\t\t\tdp[p]=min(dp[p],dp[rec]+p-rec);\n\t\t}\n\t\tif(dp.find(p+d)!=dp.end())\n\t\t\tdp[p+d]=min(dp[p+d], min(dp[p]+t,p+d) );\n\t\telse\n\t\t\tdp[p+d]=min(dp[p]+t,p+d);\n\t\tmaho.pb(p);\n\t\tmaho.pb(p+d);\n\t//\tcout << p << \" \" << dp[p] << endl;\n\t//\tcout << p+d << \" \" << dp[p+d] << endl;\n\t}\n\tint ans=l;\n\trep(i,maho.size()){\n\t\tint rec=maho[i];\n\t\trec=l-rec+dp[rec];\n\t\tans=min(rec,ans);\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.45454545454545453, "time_ms": 110, "memory_kb": 1316, "score_of_the_acc": -0.2514, "final_rank": 7 }, { "submission_id": "aoj_1528_890210", "code_snippet": "#include<iostream>\n#include<queue>\n#include<algorithm>\n#include<stack>\n#include<map>\n#include<set>\nusing namespace std;\ntypedef long long ll;\nstruct E{\n ll p,t;\n};\nbool operator<(E a,E b){\n return a.t>b.t;\n}\nll L,n;\nset<ll> node;\nmap<ll,vector<E> > edge;\n\nvoid input(){\n cin>>L>>n;\n node.insert(0);\n node.insert(L);\n for(ll i=0;i<n;i++){\n ll p,d,t;\n cin>>p>>d>>t;\n edge[p].push_back((E){d,t});\n node.insert(p);\n node.insert(p+d);\n }\n for(auto i:node){\n //cout<<i<<endl;\n }\n}\n\nll solve(){\n priority_queue<E> q;\n q.push((E){0,0});\n map<ll,int> dp;\n for(auto i:node){\n dp[i]=-1;\n }\n while(!q.empty()){\n E tmp = q.top();q.pop();\n if(tmp.p == L)return tmp.t;\n if(dp[tmp.p]!=-1&& tmp.t>=dp[tmp.p])continue;\n dp[tmp.p]=tmp.t;\n //cout<<tmp.p<<\" \"<<tmp.t<<endl;\n const vector<E> e = edge[tmp.p];\n for(int i=0;i<e.size();i++){\n q.push((E){tmp.p+e[i].p,tmp.t+e[i].t});\n }\n for(auto i:node){\n if(i>tmp.p){\n q.push((E){i,tmp.t + (i-tmp.p)});\n }\n }\n }\n return 0;\n}\n\n\nint main(){\n input();\n cout<<solve()<<endl;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 35792, "score_of_the_acc": -1.7216, "final_rank": 6 }, { "submission_id": "aoj_1528_890040", "code_snippet": "#include <iostream>\n#include <string>\n#include <queue>\n#include <map>\nusing namespace std;\n\nstruct NODE{\n\tint x,cost;\n\tNODE(int x,int cost) : x(x) ,cost(cost) {}\n};\nbool operator <(const NODE &a,const NODE &b){\n\treturn a.cost > b.cost;\n}\n\nmap<int, vector<NODE> > cost;\n\nint main(){\n\tint L,n;\n\tcin >> L >> n;\n\tvector<int> vertex;\n\tvertex.push_back(0);\n\tvertex.push_back(L);\n\t\n\tfor(int i = 0 ; i < n ; i++){\n\t\tint a,b,c;\n\t\tcin >> a >> b >> c;\n\t\tcost[a].push_back(NODE(b,c));\n\t\tvertex.push_back(a);\n\t}\n\tpriority_queue<NODE> Q;\n\tmap<int,int> memo;\n\tQ.push(NODE(0,0));\n\twhile( Q.size() ){\n\t\tNODE q = Q.top(); Q.pop();\n\t\tif( memo[q.x]++ ) continue;\n\t\tif( q.x > L ) continue;\n\t\tif( q.x == L ){\n\t\t\tcout << q.cost << endl;\n\t\t\treturn 0;\n\t\t}\n\t\tfor(int i = 0 ; i < vertex.size() ; i++){\n\t\t\tif( vertex[i] > q.x ){\n\t\t\t\tQ.push(NODE(vertex[i],q.cost+vertex[i]-q.x));\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0 ; i < cost[q.x].size() ; i++){\n\t\t\tQ.push(NODE(q.x+cost[q.x][i].x,q.cost+cost[q.x][i].cost));\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 10472, "score_of_the_acc": -0.5427, "final_rank": 4 } ]
aoj_1519_cpp
Problem D: Room of Time and Spirit Problem 20XX年、とある科学者がバイオテクノロジーによって強力な人造人間を開発してしまった。この人造人間はコンピュータにより、戦闘の達人たちの細胞を組み合わせて作られているため非常に強力である。 このままでは地球は人造人間に支配されてしまうので、 N 人の戦士たちは人造人間と戦うことにした。しかし今の戦士たちでは到底人造人間に敵わないため、修行をしなければならない。また、 N 人の戦士たちはそれぞれ 1 ~ N の番号が付いている。 この時代、天空都市AIZUには SRLU室と呼ばれる修行のための特殊な部屋が存在した。この部屋での1年は外界の時間の1日と同じであるため、短期間で戦闘力を大幅に上昇することが出来る。同時に部屋に入れる人数の上限は2人である。また、その2人が部屋に入った時、それぞれ戦闘力が上昇する。 戦士たちは残された時間でどのように部屋に入るか検討するため、あなたに以下のクエリを処理するプログラムの作成を依頼した。 与えられるクエリは以下の通りである。 また以下の説明で出てくる A , B はそれぞれ戦士の番号を表す。 A と B が等しい事は無い。 IN A B C A と B が部屋に入り、それぞれ C だけ戦闘力が上昇する。この時、(部屋に入る直前の B の戦闘力) - (部屋に入る直前の A の戦闘力) = C が成り立つ。また、この時 B の戦闘力は A の戦闘力より高い事が保証される。 COMPARE A B A と B の戦闘力の差 (現在の B の戦闘力) - (現在の A の戦闘力)を出力する。もしその差が特定出来なければ、WARNING と出力せよ。 Input N Q query 1 . . . query Q 最初に戦士の人数 N , クエリの数 Q が与えられる。 次に Q 回だけクエリが与えられる。 Constraints 入力は以下の条件を満たす。 2 ≤ N ≤ 100000 1 ≤ Q ≤ 100000 1 ≤ A , B ≤ N 1 ≤ C ≤ 5000 Output 上記のように与えられたクエリを順番に処理していき、 COMPAREの入力が与えられた際に出力を行う。 Sample Input1 3 5 COMPARE 1 2 IN 1 2 5 IN 2 3 3 COMPARE 2 3 COMPARE 1 3 Sample Output1 WARNING 3 11 Sample Input2 4 3 IN 1 4 10 IN 2 3 20 COMPARE 1 2 Sample Output2 WARNING Sample Input3 3 4 IN 2 1 2 IN 3 1 2 COMPARE 1 3 COMPARE 2 3 Sample Output3 -2 2 Sample Input4 10 4 IN 10 8 2328 IN 8 4 3765 IN 3 8 574 COMPARE 4 8 Sample Output4 -3191 Sample Input5 3 5 IN 1 2 5 IN 1 2 5 IN 2 3 10 COMPARE 1 2 COMPARE 2 3 Sample Output5 15 10
[ { "submission_id": "aoj_1519_9725871", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\nint par[100001], D[100001], sum[100001];\n\nint find(int x) {\n if (par[x] != x) {\n int px = par[x];\n par[x] = find(par[x]);\n D[x] += D[px];\n return par[x];\n }\n return x;\n}\n\nvoid Union(int x, int y, int c) {\n int rx = find(x);\n int ry = find(y);\n if (rx == ry)return;\n par[rx] = ry;\n D[rx] = c + D[y] - D[x];\n}\n\nvoid IN(int a, int b, int c) {\n sum[a] += c;\n sum[b] += c;\n Union(a, b, c - sum[b] + sum[a]);\n}\n\nstring COMP(int a, int b) {\n if (find(a) != find(b))\n return \"-0\";\n else\n return to_string(D[a] - D[b] + sum[b] - sum[a]);\n}\n\nvector<string> huntersCombat(int n, int q, string Q[], int a[], int b[], int c[]) {\n for (int i = 1; i <= n; i++)\n par[i] = i;\n vector<string> ans;\n for (int i = 0; i < q; i++) {\n if (Q[i][0] == 'I')\n IN(a[i], b[i], c[i]);\n else {\n ans.push_back(COMP(a[i], b[i]));\n }\n }\n return ans;\n}\n\nint main() {\n int n, q;\n cin >> n >> q;\n string Q[q];\n int a[q], b[q], c[q];\n for (int i = 0; i < q; i++) {\n cin >> Q[i] >> a[i] >> b[i];\n if (Q[i][0] == 'I') {\n cin >> c[i];\n }\n }\n auto ans = huntersCombat(n, q, Q, a, b, c);\n for (auto i: ans) {\n if (i == \"-0\") {\n cout << \"WARNING\";\n } else {\n cout << i;\n }\n cout << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 13508, "score_of_the_acc": -0.3867, "final_rank": 7 }, { "submission_id": "aoj_1519_9675443", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nstruct DSU {\n vector<int> szz, par, val;\n vector<vector<int>> comp;\n\n DSU(int n) {\n szz.resize(n + 1, 1);\n par.resize(n + 1);\n val.resize(n + 1);\n comp.resize(n + 1);\n iota(par.begin(), par.end(), 0);\n for (int i = 1; i <= n; i++) {\n comp[i].push_back(i);\n }\n }\n\n int find(int a) {\n while (a != par[a]) {\n a = par[a] = par[par[a]];\n }\n return a;\n }\n\n bool same(int a, int b) {\n return find(a) == find(b);\n }\n\n void merge(int a, int b, int c) {\n int parA = find(a);\n int parB = find(b);\n if (parA == parB)return;\n if (szz[parA] < szz[parB]) {\n int increament = val[b] - val[a] - c;\n for (auto node: comp[parA]) {\n val[node] += increament;\n comp[parB].push_back(node);\n }\n comp[parA].clear();\n val[a] += c;\n val[b] += c;\n szz[parB] += szz[parA];\n par[parA] = parB;\n } else {\n int increment = val[a] - val[b] + c;\n for (auto node: comp[parB]) {\n val[node] += increment;\n comp[parA].push_back(node);\n }\n comp[parB].clear();\n val[a] += c;\n val[b] += c;\n szz[parA] += szz[parB];\n par[parB] = parA;\n }\n }\n};\n\nvoid acc() {\n int n, q;\n cin >> n >> q;\n DSU dsu(n);\n while (q--) {\n string s;\n cin >> s;\n if (s == \"IN\") {\n int a, b, c;\n cin >> a >> b >> c;\n if (dsu.same(a, b)) {\n dsu.val[a] += c;\n dsu.val[b] += c;\n } else\n dsu.merge(a, b, c);\n } else {\n int a, b;\n cin >> a >> b;\n if (dsu.same(a, b)) {\n cout << dsu.val[b] - dsu.val[a] << '\\n';\n } else {\n cout << \"WARNING\" << '\\n';\n }\n }\n }\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int t = 1;\n acc();\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 10088, "score_of_the_acc": -0.1279, "final_rank": 3 }, { "submission_id": "aoj_1519_6006333", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <stdio.h>\n#include <vector>\nusing namespace std;\nconst int maxn=100010;\nlong long aa[maxn];\nint fa[maxn];\nvector<int> v[maxn];\nint fin(int x)\n{\n return fa[x]==x?x:(fa[x]=fin(fa[x]));\n}\nint main()\n{\n int n,q;\n cin>>n>>q;\n for(int i=1;i<=n;i++) fa[i]=i,v[i].push_back(i);\n string s;\n long long a,b,c;\n for(int i=0;i<q;i++){\n cin>>s>>a>>b;\n if(s[0]=='I'){\n cin>>c;\n if(fin(a)==fin(b)){\n aa[a]+=c;\n aa[b]+=c;\n }\n else{\n int fro=fin(a);\n int to=fin(b);\n int cha=aa[b]-c-aa[a];\n for(int j=0;j<(int)v[fro].size();j++){\n aa[v[fro][j]]+=cha;\n v[to].push_back(v[fro][j]);\n }\n aa[a]+=c;\n aa[b]+=c;\n fa[fro]=fa[to];\n }\n }\n if(s[0]=='C'){\n if(fin(a)!=fin(b)){\n cout<<\"WARNING\"<<endl;\n }\n else{\n cout<<aa[b]-aa[a]<<endl;\n }\n }\n //for(int j=1;j<=n;j++){\n // cout<<v[j].size()<<endl;\n //}\n //for(int j=1;j<=n;j++){\n // cout<<fa[j]<<\" \";\n //}\n //cout<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 56892, "score_of_the_acc": -1.3415, "final_rank": 16 }, { "submission_id": "aoj_1519_4240479", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <numeric>\nusing namespace std;\n\ntemplate<typename T>\nstruct WeightedQuickFind {\n vector<int> par;\n vector<T> weight;\n vector<vector<int>> groups;\n WeightedQuickFind(int sz) : par(sz), weight(sz) {\n iota(begin(par), end(par), 0);\n for (int i = 0; i < sz; i++) groups.emplace_back(1, i);\n }\n void merge(int x, int y, T w) {\n if (par[x] == par[y]) return ;\n if (groups[par[x]].size() > groups[par[y]].size()) swap(x, y), w = -w;\n int p = par[x];\n w += weight[x] - weight[y];\n for (int i: groups[par[x]]) {\n weight[i] -= w, par[i] = par[y], groups[par[y]].emplace_back(i);\n }\n groups[p].clear();\n }\n bool is_same(int x, int y) { return par[x] == par[y]; }\n T diff(int x, int y) { return weight[y] - weight[x]; }\n void update(int x, int y, T w) {\n weight[x] += w, weight[y] += w;\n if (par[x] != par[y]) merge(x, y, w);\n }\n};\n\nint main() {\n int n, q; cin >> n >> q;\n WeightedQuickFind<int> wqf(n);\n while (q--) {\n string s; cin >> s;\n int a, b; cin >> a >> b; a--, b--;\n if (s[0] == 'I') {\n int c; cin >> c;\n wqf.update(a, b, c);\n }\n if (s[0] == 'C') {\n if (wqf.is_same(a, b)) {\n cout << wqf.diff(a, b) << endl;\n } else {\n cout << \"WARNING\" << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 9480, "score_of_the_acc": -0.4336, "final_rank": 11 }, { "submission_id": "aoj_1519_4240461", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <numeric>\nusing namespace std;\n\ntemplate<typename T>\nstruct WeightedQuickFind {\n vector<int> par;\n vector<T> weight;\n vector<vector<int>> groups;\n WeightedQuickFind(int sz) : par(sz), weight(sz), groups(sz) {\n iota(begin(par), end(par), 0);\n for (int i = 0; i < sz; i++) groups[i].assign(1, i);\n }\n void merge(int x, int y, T w) {\n if (x == y) return ;\n if (groups[par[x]].size() > groups[par[y]].size()) swap(x, y), w = -w;\n int p = par[x];\n w += weight[x] - weight[y];\n for (int i: groups[par[x]]) {\n weight[i] -= w, par[i] = par[y], groups[par[y]].emplace_back(i);\n }\n groups[p].clear();\n }\n bool is_same(int x, int y) { return par[x] == par[y]; }\n T diff(int x, int y) { return weight[y] - weight[x]; }\n void update(int x, int y, T w) {\n weight[x] += w, weight[y] += w;\n if (par[x] != par[y]) merge(x, y, w);\n }\n};\n\nint main() {\n int n, q; cin >> n >> q;\n WeightedQuickFind<int> wqf(n);\n while (q--) {\n string s; cin >> s;\n int a, b; cin >> a >> b; a--, b--;\n if (s[0] == 'I') {\n int c; cin >> c;\n wqf.update(a, b, c);\n }\n if (s[0] == 'C') {\n if (wqf.is_same(a, b)) {\n cout << wqf.diff(a, b) << endl;\n } else {\n cout << \"WARNING\" << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 9372, "score_of_the_acc": -0.4316, "final_rank": 10 }, { "submission_id": "aoj_1519_4240454", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\n#include <numeric>\nusing namespace std;\n\ntemplate<typename T>\nstruct WeightedQuickFind {\n vector<int> par;\n vector<T> weight;\n vector<vector<int>> groups;\n WeightedQuickFind(int sz) : par(sz), weight(sz) {\n iota(begin(par), end(par), 0);\n for (int i = 0; i < sz; i++) groups.emplace_back(1, i);\n }\n void merge(int x, int y, T w) {\n if (x == y) return ;\n if (groups[par[x]].size() > groups[par[y]].size()) swap(x, y), w = -w;\n int p = par[x];\n w += weight[x] - weight[y];\n for (int i: groups[par[x]]) {\n weight[i] -= w, par[i] = par[y], groups[par[y]].emplace_back(i);\n }\n groups[p].clear();\n }\n bool is_same(int x, int y) { return par[x] == par[y]; }\n T diff(int x, int y) { return weight[y] - weight[x]; }\n void update(int x, int y, T w) {\n weight[x] += w, weight[y] += w;\n if (par[x] != par[y]) merge(x, y, w);\n }\n};\n\nint main() {\n int n, q; cin >> n >> q;\n WeightedQuickFind<int> wqf(n);\n while (q--) {\n string s; cin >> s;\n int a, b; cin >> a >> b; a--, b--;\n if (s[0] == 'I') {\n int c; cin >> c;\n wqf.update(a, b, c);\n }\n if (s[0] == 'C') {\n if (wqf.is_same(a, b)) {\n cout << wqf.diff(a, b) << endl;\n } else {\n cout << \"WARNING\" << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 9500, "score_of_the_acc": -0.434, "final_rank": 12 }, { "submission_id": "aoj_1519_4240427", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <vector>\nusing namespace std;\n\ntemplate<typename T>\nstruct WeightedUnionFind {\n vector<int> data;\n vector<T> ws;\n WeightedUnionFind(int sz) : data(sz, -1), ws(sz) { }\n bool merge(int x, int y, T w) { // ws(y) = ws(x) + w\n w += weight(x); w -= weight(y);\n if ((x = root(x)) == (y = root(y))) return false;\n if (data[x] > data[y]) swap(x, y), w = -w;\n data[x] += data[y]; data[y] = x; ws[y] = w;\n return true;\n }\n bool is_same(int x, int y) { return root(x) == root(y); }\n T diff(int x, int y) { return weight(y) - weight(x); }\n T weight(int t) { root(t); return ws[t]; }\n int root(int k) {\n if (data[k] < 0) return k;\n int par = root(data[k]);\n ws[k] += ws[data[k]];\n return data[k] = par;\n }\n};\n\nint main() {\n int n, q; cin >> n >> q;\n WeightedUnionFind<int> wuf(n);\n vector<int> added(n);\n while (q--) {\n string s; cin >> s;\n int a, b; cin >> a >> b; a--, b--;\n if (s[0] == 'I') {\n int c; cin >> c;\n wuf.merge(a, b, c + added[a] - added[b]);\n added[a] += c, added[b] += c;\n }\n if (s[0] == 'C') {\n if (wuf.is_same(a, b)) {\n cout << wuf.diff(a, b) + added[b] - added[a] << endl;\n } else {\n cout << \"WARNING\" << endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3832, "score_of_the_acc": -0.3284, "final_rank": 4 }, { "submission_id": "aoj_1519_3095580", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, q, a, b, c;\nstring req;\nvector<int> ls[100005];\n\ntemplate <class T>\nstruct Unionfind {\n //木の番号\n vector<int> par;\n //木の階層\n vector<int> treerank;\n //辺の重み\n vector<T> weight;\n //コンストラクタ\n Unionfind(int n = 1, T initialnum = 0) {\n stree(n + 1, initialnum);\n }\n //木の生成、初期化\n void stree(int n = 1, T initialnum = 0) {\n par.resize(n);\n treerank.resize(n);\n weight.resize(n);\n for(int i = 0; i < n; ++i) {\n par[i] = i;\n treerank[i] = 0;\n weight[i] = initialnum;\n ls[i].push_back(i);\n }\n }\n //木の根を調べる\n int root(int x) {\n if(par[x] == x) return x;\n int rx = root(par[x]);\n weight[x] += weight[par[x]];\n return par[x] = rx;\n }\n //同一の木にあるか調べる\n bool chtree(int x, int y) { return root(x) == root(y); }\n //重み計算\n T calcw(int x) {\n root(x);\n return weight[x];\n }\n //木に追加\n //すでに同じグループにいた場合、計算せずに0を返す。\n bool unitree(int x, int y, T w = 0) {\n for(int i = 0; i < ls[y].size(); ++i)\n if(par[ls[y][i]] == y) weight[ls[y][i]] -= w;\n\n for(int i = 0; i < ls[x].size(); ++i)\n if(par[ls[x][i]] == x) weight[ls[x][i]] -= w;\n\n weight[x] += w;\n weight[y] += w;\n w += calcw(x);\n w -= calcw(y);\n x = root(x);\n y = root(y);\n\n if(x == y) return 0;\n if(treerank[x] < treerank[y]) {\n ls[y].insert(ls[y].end(), ls[x].begin(), ls[x].end());\n par[x] = y;\n weight[x] = -w;\n }\n else if(treerank[y] < treerank[x]) {\n ls[x].insert(ls[x].end(), ls[y].begin(), ls[y].end());\n par[y] = x;\n weight[y] = w;\n }\n else {\n ls[x].insert(ls[x].end(), ls[y].begin(), ls[y].end());\n par[y] = x;\n ++treerank[x];\n weight[y] = w;\n }\n return 1;\n }\n // xとyの差を計算\n //もしxとyが同じグループになかったら-1を返す\n T calcdiff(int x, int y) {\n if(!chtree(x, y)) return -1;\n return calcw(y) - calcw(x);\n }\n};\n\nint main() {\n int i;\n cin >> n >> q;\n Unionfind<int> uf(n);\n for(i = 0; i < q; ++i) {\n cin >> req;\n if(req == \"IN\") {\n cin >> a >> b >> c;\n uf.unitree(a, b, c);\n }\n else {\n int ans;\n cin >> a >> b;\n ans = uf.calcdiff(a, b);\n if(!uf.chtree(a,b))\n cout << \"WARNING\"<<endl;\n else\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 9736, "score_of_the_acc": -0.414, "final_rank": 9 }, { "submission_id": "aoj_1519_3095579", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, q, a, b, c;\nstring req;\nvector<int> ls[100005];\n\ntemplate <class T>\nstruct Unionfind {\n //木の番号\n vector<int> par;\n //木の階層\n vector<int> treerank;\n //辺の重み\n vector<T> weight;\n //コンストラクタ\n Unionfind(int n = 1, T initialnum = 0) {\n stree(n + 1, initialnum);\n }\n //木の生成、初期化\n void stree(int n = 1, T initialnum = 0) {\n par.resize(n);\n treerank.resize(n);\n weight.resize(n);\n for(int i = 0; i < n; ++i) {\n par[i] = i;\n treerank[i] = 0;\n weight[i] = initialnum;\n ls[i].push_back(i);\n }\n }\n //木の根を調べる\n int root(int x) {\n if(par[x] == x) return x;\n int rx = root(par[x]);\n weight[x] += weight[par[x]];\n return par[x] = rx;\n }\n //同一の木にあるか調べる\n bool chtree(int x, int y) { return root(x) == root(y); }\n //重み計算\n T calcw(int x) {\n root(x);\n return weight[x];\n }\n //木に追加\n //すでに同じグループにいた場合、計算せずに0を返す。\n bool unitree(int x, int y, T w = 0) {\n for(int i = 0; i < ls[y].size(); ++i)\n if(par[ls[y][i]] == y) weight[ls[y][i]] -= w;\n\n for(int i = 0; i < ls[x].size(); ++i)\n if(par[ls[x][i]] == x) weight[ls[x][i]] -= w;\n\n weight[x] += w;\n weight[y] += w;\n w += calcw(x);\n w -= calcw(y);\n x = root(x);\n y = root(y);\n\n if(x == y) return 0;\n if(treerank[x] < treerank[y]) {\n ls[y].insert(ls[y].end(), ls[x].begin(), ls[x].end());\n par[x] = y;\n weight[x] = -w;\n }\n else if(treerank[y] < treerank[x]) {\n ls[x].insert(ls[x].end(), ls[y].begin(), ls[y].end());\n par[y] = x;\n weight[y] = w;\n }\n else {\n ls[x].insert(ls[x].end(), ls[y].begin(), ls[y].end());\n par[y] = x;\n ++treerank[x];\n weight[y] = w;\n }\n return 1;\n }\n // xとyの差を計算\n //もしxとyが同じグループになかったら-1を返す\n T calcdiff(int x, int y) {\n if(!chtree(x, y)) return -1;\n return calcw(y) - calcw(x);\n }\n};\n\nint main() {\n int i;\n cin >> n >> q;\n Unionfind<int> uf(n);\n for(i = 0; i < q; ++i) {\n cin >> req;\n if(req == \"IN\") {\n cin >> a >> b >> c;\n uf.unitree(a, b, c);\n }\n else {\n int ans;\n cin >> a >> b;\n ans = uf.calcdiff(a, b);\n if(ans == -1)\n cout << \"WARNING\"<<endl;\n else\n cout << ans << endl;\n }\n }\n}", "accuracy": 0.3181818181818182, "time_ms": 130, "memory_kb": 9788, "score_of_the_acc": -0.3906, "final_rank": 17 }, { "submission_id": "aoj_1519_3095484", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, q, a, b, c;\nstring req;\nvector<int> ls[100005];\n\ntemplate <class T>\nstruct Unionfind {\n //木の番号\n vector<int> par;\n //木の階層\n vector<int> treerank;\n //辺の重み\n vector<T> weight;\n //コンストラクタ\n Unionfind(int n = 1, T initialnum = 0) {\n stree(n + 1, initialnum);\n }\n //木の生成、初期化\n void stree(int n = 1, T initialnum = 0) {\n par.resize(n);\n treerank.resize(n);\n weight.resize(n);\n for(int i = 0; i < n; ++i) {\n par[i] = i;\n treerank[i] = 0;\n weight[i] = initialnum;\n ls[i].push_back(i);\n }\n }\n //木の根を調べる\n int root(int x) {\n if(par[x] == x) return x;\n int rx = root(par[x]);\n weight[x] += weight[par[x]];\n return par[x] = rx;\n }\n //同一の木にあるか調べる\n bool chtree(int x, int y) { return root(x) == root(y); }\n //重み計算\n T calcw(int x) {\n root(x);\n return weight[x];\n }\n //木に追加\n //すでに同じグループにいた場合、計算せずに0を返す。\n bool unitree(int x, int y, T w = 0) {\n T ww = w;\n for(int i = 0; i < ls[y].size(); ++i) {\n weight[ls[y][i]] -= ww;\n }\n for(int i = 0; i < ls[x].size(); ++i) {\n weight[ls[x][i]] -= ww;\n }\n weight[x] += ww;\n weight[y] += ww;\n w += calcw(x);\n w -= calcw(y);\n x = root(x);\n y = root(y);\n\n if(x == y) return 0;\n if(treerank[x] < treerank[y]) {\n ls[y].insert(ls[y].end(), ls[x].begin(), ls[x].end());\n ls[x].erase(ls[x].begin(), ls[x].end());\n par[x] = y;\n weight[x] = -w;\n }\n else if(treerank[y] < treerank[x]) {\n ls[x].insert(ls[x].end(), ls[y].begin(), ls[y].end());\n ls[y].erase(ls[y].begin(), ls[y].end());\n par[y] = x;\n weight[y] = w;\n }\n else {\n ls[x].insert(ls[x].end(), ls[y].begin(), ls[y].end());\n ls[y].erase(ls[y].begin(), ls[y].end());\n par[y] = x;\n ++treerank[x];\n weight[y] = w;\n }\n return 1;\n }\n // xとyの差を計算\n //もしxとyが同じグループになかったら-1を返す\n T calcdiff(int x, int y) {\n if(!chtree(x, y)) return -1;\n return calcw(y) - calcw(x);\n }\n};\n\nint main() {\n int i;\n cin >> n >> q;\n Unionfind<int> uf(n);\n for(i = 0; i < q; ++i) {\n cin >> req;\n if(req == \"IN\") {\n cin >> a >> b >> c;\n uf.unitree(a, b, c);\n }\n else {\n int ans;\n cin >> a >> b;\n ans = uf.calcdiff(a, b);\n if(ans == -1)\n cout << \"WARNING\"<<endl;\n else\n cout << ans << endl;\n }\n }\n}", "accuracy": 0.18181818181818182, "time_ms": 130, "memory_kb": 5496, "score_of_the_acc": -0.3106, "final_rank": 18 }, { "submission_id": "aoj_1519_2964664", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)\nusing namespace std;\ntypedef long long ll;\n\nclass UnionFind {\npublic:\n vector<ll> parent, gap, up;\n\n UnionFind(ll n): parent(n), gap(n), up(n) {\n REP(i, 0, n) parent[i] = i;\n }\n\n ll find(ll i) {\n if(parent[i] == i) return i;\n ll p = parent[i];\n ll t = find(p);\n gap[i] += gap[p];\n return parent[i] = t;\n }\n\n void unite(ll a, ll b, ll c) {\n ll pa = find(a), pb = find(b);\n if(pa != pb) {\n ll w = height(a) - height(b) + c;\n gap[pa] = -w;\n parent[pa] = find(pb);\n }\n up[a] += c;\n up[b] += c;\n }\n\n ll height(ll i) {\n return gap[i] + up[i];\n }\n};\n\nint main(void) {\n ll N, Q;\n cin >> N >> Q;\n vector<ll> T(Q), A(Q), B(Q), C(Q);\n REP(i, 0, Q) {\n string type;\n cin >> type >> A[i] >> B[i]; A[i]--; B[i]--;\n if(type == \"IN\") {\n T[i] = 0;\n cin >> C[i];\n }\n if(type == \"COMPARE\") T[i] = 1;\n }\n\n UnionFind uf(N);\n REP(i, 0, Q) {\n if(T[i] == 0) {\n uf.unite(A[i], B[i], C[i]);\n } else {\n if(uf.find(A[i]) != uf.find(B[i])) {\n cout << \"WARNING\" << endl;\n } else {\n cout << uf.height(B[i]) - uf.height(A[i]) << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 8468, "score_of_the_acc": -0.4392, "final_rank": 13 }, { "submission_id": "aoj_1519_2963740", "code_snippet": "#include<iostream>\n#include <list>\n#include<stack>\n#include<queue>\n#include <vector>\n#include <set>\n#include<algorithm>\n#include<math.h>\n#include<stdlib.h>\n#include<string>\n#include <functional>\n#include<fstream>\n\n#define FOR(k,m,n) for(int (k)=(m);(k)<(n);(k)++)\n#define LL long long\n#define CLR(a) memset((a),0,sizeof(a))\n#define SZ(x) (int((x).size()))\n#define WAITING(str) int str;std::cin>>str;\n#define DEBUGING(str) cout<<str<<endl\nusing namespace std;\n\nconst LL MOD = 1000000007;// 10^9+7\nconst int INF = (1 << 30);\n#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)\nusing namespace std;\ntypedef long long ll;\n\nclass UnionFind {\npublic:\n\tvector<ll> parent;\n\tvector<ll> gap;//強化前の、素の値\n\tvector<ll> up;//強化量\n\n\tUnionFind(ll n) : parent(n), gap(n, 0), up(n, 0) {\n\t\tREP(i, 0, n) parent[i] = i;\n\t}\n\n\tll find(ll i) {\n\t\tif (parent[i] == i) return i;\n\n\t\tint tmp = parent[i];\n\t\tll p = find(parent[i]);\n\t\t//cerr << \"find \" << i+1 << \" ,,\" << tmp+1 << \"p: \" << p+1 << endl;\n\t\tgap[i] += gap[tmp];\n\t\treturn parent[i] = p;\n\t}\n\n\tvoid unite(ll i, ll j, ll c) {\n\t\tll p = find(i), q = find(j);\n\t\tif (p != q) {\n\t\t\tgap[p] = c + get(j) - get(i);\n\t\t\tparent[p] = q;\n\t\t}\n\t\tup[i] += c;\n\t\tup[j] += c;\n\t}\n\n\tll get(ll i) {\n\t\treturn - up[i] + gap[i];\n\t\t//return up[find(i)] - up[i] + gap[i];\n\t}\n};\n\nint main(void) {\n\tll N, Q;\n\tcin >> N >> Q;\n\tvector<ll> T(Q), A(Q), B(Q), C(Q);\n\tREP(i, 0, Q) {\n\t\tstring type;\n\t\tcin >> type >> A[i] >> B[i]; A[i]--; B[i]--;\n\t\tif (type == \"IN\") {\n\t\t\tT[i] = 0;\n\t\t\tcin >> C[i];\n\t\t}\n\t\tif (type == \"COMPARE\") T[i] = 1;\n\t}\n\n\tUnionFind uf(N);\n\tREP(i, 0, Q) {\n\t\t//cerr << endl<<endl<<\" debug\" << endl;\n\t\tif (T[i] == 0) {\n\t\t\tuf.unite(A[i], B[i], C[i]);\n\t\t}\n\t\tif (T[i] == 1) {\n\t\t\tif (uf.find(A[i]) != uf.find(B[i])) cout << \"WARNING\" << endl;\n\t\t\telse {\n\t\t\t\t//uf.find(A[i]);\n\t\t\t\t//uf.find(B[i]);\n\t\t\t\t//cerr << \"testing\" << endl;\n\t\t\t\tcout << uf.get(A[i]) - uf.get(B[i]) << endl;\n\t\t\t}\n\t\t}\n\t\t//cerr << \"====\" << endl;\n\t\t//cerr << \"way::\" << A[i]+1 << \" \" << B[i]+1 << endl;\n\t\t//REP(i, 0, N) cerr <<\"me:\"<<i+1<< \" par:\" << uf.parent[i] + 1 << \", gap:\" << uf.gap[i] << \", up:\" << uf.up[i] << endl;\n\t}\n\t//cerr << \"==FINISH==\" << endl;\n\t//REP(i, 0, N) cerr << \"par:\" << uf.parent[i] << \", gap:\" << uf.gap[i] << \", up:\" << uf.up[i] << endl;\n\tint n;\n\tcin >> n;\n\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 8372, "score_of_the_acc": -0.413, "final_rank": 8 }, { "submission_id": "aoj_1519_2963128", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define pb push_back\n#define all(x) begin(x),end(x)\n#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))\n#ifdef LOCAL\n#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\nvoid _dbg(string){cerr<<endl;}\ntemplate<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<\" = \"<<h<<\", \";_dbg(s.substr(l+1),t...);}\ntemplate<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n#else\n#define dbg(...) {}\n#endif\n\n#define long long long // for codeforces\n\ntemplate <class T>\nclass WeightedUnionFind {\n int n;\n vector<int> par,sz;\n vector<T> w;\n // weight[i] : iがpar[i]よりどれだけ重いか\npublic:\n WeightedUnionFind(){}\n WeightedUnionFind(int _n) : n(_n) {\n par.resize(n, -1);\n sz.resize(n, 1);\n w.resize(n, 0);\n }\n int find(int i){\n if(par[i] < 0) return i;\n int p = find(par[i]);\n w[i] += w[par[i]];\n return par[i] = p;\n }\n T weight(int x){ find(x); return w[x]; }\n bool same(int x, int y){ return find(x) == find(y); }\n // weight[i] +w の位置に j を配置\n void unite(int i, int j, T nw){\n nw += weight(i) - weight(j);\n int x = find(i), y = find(j);\n if(x == y) return;\n if(sz[x] < sz[y]){\n swap(x, y);\n nw = -nw;\n }\n sz[x] += sz[y];\n par[y] = x;\n w[y] = nw;\n }\n // x からみた y の相対位置\n T diff(int x, int y){ return weight(y) - weight(x); }\n};\n\nint main(){\n int n,q;\n scanf(\"%d %d\", &n, &q);\n\n WeightedUnionFind<int> uf(n);\n vector<int> added(n, 0);\n rep(i,q){\n char s[20];\n scanf(\"%s\", s);\n if(s[0] == 'I') {\n int a,b,c;\n scanf(\"%d %d %d\", &a, &b, &c);\n a--;b--;\n uf.unite(a, b, c + added[a] - added[b]);\n added[a] += c;\n added[b] += c;\n }\n else if(s[0] == 'C') {\n int a,b;\n scanf(\"%d %d\", &a, &b);\n a--;b--;\n if(uf.same(a,b)) { dbg(added, uf.weight(0), uf.weight(1), uf.weight(2));\n printf(\"%d\\n\", uf.diff(a,b) + added[b] - added[a]);\n } else {\n printf(\"WARNING\\n\");\n }\n }\n else assert(false);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4408, "score_of_the_acc": -0.0465, "final_rank": 2 }, { "submission_id": "aoj_1519_2962993", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n\nstruct UnionFind{\n int n;\n vector<int> d;\n vector<ll> w;\n UnionFind(){}\n UnionFind(int N):n(N),d(N,-1),w(N,0){}\n int root(int v){\n if(d[v]<0) return v;\n return root(d[v]);\n }\n ll calc(int v){\n if(d[v]<0)return 0;\n return calc(d[v])+w[v];\n }\n bool unite(int X, int Y, int W){\n ll A=calc(X);\n ll C=calc(Y);\n X = root(X); Y = root(Y);\n if(X==Y) return false;\n if(size(X) < size(Y)){\n swap(X,Y);\n W=-W;\n swap(A,C);\n }\n ll B=W+A-C;\n w[Y]=B;\n d[X] += d[Y];\n d[Y] = X;\n return true;\n }\n int size(int v){ return -d[root(v)]; }\n bool same(int X, int Y){ return root(X)==root(Y); }\n};\n\nint n,q;\nll sum[100010];\n\nint main(){\n cin>>n>>q;\n UnionFind uf(n);\n rep(i,q){\n string s;\n cin>>s;\n if(s==\"COMPARE\"){\n int a,b;\n cin>>a>>b;\n a--;b--;\n if(uf.root(a)!=uf.root(b)){\n cout<<\"WARNING\"<<endl;\n continue;\n }\n ll va=uf.calc(a)+sum[a];\n ll vb=uf.calc(b)+sum[b];\n cout<<vb-va<<endl;\n }else if(s==\"IN\"){\n int a,b,c;\n cin>>a>>b>>c;\n a--;b--;\n uf.unite(a,b,c-(sum[b]-sum[a]));\n sum[a]+=c;\n sum[b]+=c;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 4816, "score_of_the_acc": -0.3467, "final_rank": 5 }, { "submission_id": "aoj_1519_2949437", "code_snippet": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\n#include<math.h>\n#include<queue>\n#include<deque>\n#include<map>\n#include<set>\n#include<bitset>\nusing namespace std;\n#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; i++ )\n#define rep(i,n) REP(i,0,n)\ntypedef long long ll;\ntypedef pair<ll,int> pint;\nconst int inf=1e9+7;\nconst ll longinf=1LL<<60;\nconst ll mod=1e9+7;\nint dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1};\n\nll r[101010], p[101010], par[101010];\n\nint find(int x){\n if(par[x]==x)\n return x;\n int y = find(par[x]);\n r[x] += r[par[x]];\n return par[x] = y;\n}\n\nll weight(int x){\n find(x);\n return r[x] + p[x];\n}\n\nvoid unite(int x, int y, int c){\n p[x] += c;\n p[y] += c;\n c -= weight(y);\n c += weight(x);\n x = find(x);\n y = find(y);\n if(x!=y){\n par[x] = y;\n r[x] = -c;\n }\n}\nint main(){\n int n, q;\n cin >> n >> q;\n rep(i, n) par[i] = i;\n rep(i,q){\n string s;\n cin >> s;\n if(s==\"COMPARE\"){\n int x, y;\n cin >> x >> y;\n if(find(x-1)!=find(y-1))\n cout << \"WARNING\" << \"\\n\";\n else cout << weight(y - 1) - weight(x - 1) << \"\\n\";\n }\n else{\n int x, y, c;\n cin >> x >> y >> c;\n unite(x - 1, y - 1, c);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 5372, "score_of_the_acc": -0.3815, "final_rank": 6 }, { "submission_id": "aoj_1519_2944924", "code_snippet": "#include <cstdio>\n#include <cstdint>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <numeric>\n#include <stdexcept>\n\ntemplate <class Weight>\nclass WeightedQuickFind {\n std::vector<std::vector<size_t>> groups;\n std::vector<size_t> ind;\n std::vector<Weight> weight;\n\npublic:\n WeightedQuickFind(size_t n): ind(n), weight(n) {\n std::iota(ind.begin(), ind.end(), 0);\n for (size_t i=0; i<n; ++i)\n groups.emplace_back(1, i);\n }\n\n struct NotConnected: public std::logic_error {\n NotConnected(): std::logic_error(\"\") {}\n };\n\n void unite(size_t u, size_t v, Weight w) {\n // (weight of u) - (weight of v) = w\n if (u == v) return;\n if (groups[ind[u]].size() > groups[ind[v]].size()) {\n std::swap(u, v);\n w = -w;\n }\n size_t pi=ind[u];\n // weight[u] <- weight[v]+w\n Weight x=weight[v]+w-weight[u];\n for (size_t i: groups[ind[u]]) {\n weight[i] += x;\n ind[i] = ind[v];\n groups[ind[v]].emplace_back(i);\n }\n groups[pi].clear();\n }\n\n Weight diff(size_t u, size_t v) const {\n if (ind[u] != ind[v])\n throw NotConnected();\n\n return weight[u]-weight[v];\n }\n\n void update(size_t u, size_t v, Weight w) {\n weight[u] += w;\n weight[v] += w;\n if (ind[u] != ind[v])\n unite(u, v, w);\n }\n\n void debug() const {\n fprintf(stderr, \"Weight:\\n\");\n for (size_t i=0; i<weight.size(); ++i)\n fprintf(stderr, \"%d%c\", weight[i], i+1<weight.size()? ' ':'\\n');\n\n fprintf(stderr, \"Index:\\n\");\n for (size_t i=0; i<ind.size(); ++i)\n fprintf(stderr, \"%zu%c\", ind[i], i+1<ind.size()? ' ':'\\n');\n\n fprintf(stderr, \"groups:\\n\");\n for (size_t i=0; i<groups.size(); ++i) {\n fprintf(stderr, \"{\");\n for (size_t j=0; j<groups[i].size(); ++j) {\n fprintf(stderr, \"%zu%s\", groups[i][j], j+1<groups[i].size()? \", \":\"\");\n }\n fprintf(stderr, \"}%s\", i+1<groups.size()? \", \":\"\\n\");\n }\n fprintf(stderr, \"\\n\");\n }\n};\n\nint main() {\n size_t N;\n int Q;\n scanf(\"%zu %d\", &N, &Q);\n\n WeightedQuickFind<int> uf(N);\n for (int i=0; i<Q; ++i) {\n char s[8];\n scanf(\"%s\", s);\n if (s[0] == 'I') {\n size_t u, v;\n int w;\n scanf(\"%zu %zu %d\", &u, &v, &w);\n --u, --v;\n uf.update(v, u, w);\n } else {\n size_t u, v;\n scanf(\"%zu %zu\", &u, &v);\n --u, --v;\n try {\n printf(\"%d\\n\", uf.diff(v, u));\n } catch (WeightedQuickFind<int>::NotConnected) {\n printf(\"WARNING\\n\");\n }\n }\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 10152, "score_of_the_acc": -0.4462, "final_rank": 14 }, { "submission_id": "aoj_1519_2944445", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nconst long long int INF = 1LL << 60;\ntemplate<typename Type>\nstruct QuickFind {\n int N;\n vector<int> i2g;\n vector< vector<int> > g2i;\n vector<Type> weight;\n\n QuickFind(int N_) {\n N = N_;\n i2g.resize(N);\n g2i.resize(N);\n weight.resize(N);\n\n for(int i=0; i<N; i++) {\n i2g[i] = i;\n g2i[i] = vector<int>{i};\n }\n }\n bool same(int u, int v) {\n return i2g[u] == i2g[v];\n }\n void merge(int u, int v, Type w) {\n int ru = i2g[u], rv = i2g[v];\n\n // merge v into u\n if(g2i[ru].size() < g2i[rv].size()) {\n swap(u, v);\n swap(ru, rv);\n w = -w;\n }\n\n Type cost = weight[v];\n weight[u] += abs(w);\n weight[v] = weight[u] + w;\n \n for(auto e : g2i[rv]) {\n i2g[e] = ru;\n Type diff = weight[e] - cost;\n if(e != u && e != v) {\n weight[e] = weight[v] - abs(w) + diff;\n }\n }\n \n if(ru != rv) {\n g2i[ru].insert(g2i[ru].end(), g2i[rv].begin(), g2i[rv].end());\n g2i[rv].clear();\n }\n }\n Type calc_weight(int u, int v) {\n if(!same(u, v)) return INF;\n return weight[v] - weight[u];\n }\n};\n\nint main() {\n int N, Q; cin >> N >> Q;\n\n QuickFind<long long int> qf(N);\n for(int i=0; i<Q; i++) {\n string query; cin >> query;\n\n if(query == \"COMPARE\") {\n int u, v; cin >> u >> v;\n u--; v--;\n long long int ans = qf.calc_weight(u, v);\n if(ans == INF) cout << \"WARNING\" << endl;\n else cout << ans << endl;\n }\n else {\n int u, v, w; cin >> u >> v >> w;\n u--; v--;\n qf.merge(u, v, w);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 9824, "score_of_the_acc": -1.123, "final_rank": 15 }, { "submission_id": "aoj_1519_2944424", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nconst long long int INF = 1LL << 60;\ntemplate<typename Type>\nstruct QuickFind {\n int N;\n vector<int> i2g;\n vector< vector<int> > g2i;\n vector<Type> weight;\n\n QuickFind(int N_) {\n N = N_;\n i2g.resize(N);\n g2i.resize(N);\n weight.resize(N);\n\n for(int i=0; i<N; i++) {\n i2g[i] = i;\n g2i[i] = vector<int>{i};\n }\n }\n bool same(int u, int v) {\n return i2g[u] == i2g[v];\n }\n void merge(int u, int v, Type w) {\n int ru = i2g[u], rv = i2g[v];\n\n // merge v into u\n if(g2i[ru].size() < g2i[rv].size()) {\n swap(u, v);\n swap(ru, rv);\n w = -w;\n }\n\n Type cost = weight[v];\n weight[u] += abs(w);\n weight[v] = weight[u] + w;\n \n for(auto e : g2i[rv]) {\n i2g[e] = ru;\n Type diff = weight[e] - cost;\n if(e != u && e != v) {\n weight[e] = weight[u] + diff;\n }\n }\n \n if(ru != rv) {\n g2i[ru].insert(g2i[ru].end(), g2i[rv].begin(), g2i[rv].end());\n g2i[rv].clear();\n }\n }\n Type calc_weight(int u, int v) {\n if(!same(u, v)) return INF;\n return weight[v] - weight[u];\n }\n};\n\nint main() {\n int N, Q; cin >> N >> Q;\n\n QuickFind<long long int> qf(N);\n for(int i=0; i<Q; i++) {\n string query; cin >> query;\n\n if(query == \"COMPARE\") {\n int u, v; cin >> u >> v;\n u--; v--;\n long long int ans = qf.calc_weight(u, v);\n if(ans == INF) cout << \"WARNING\" << endl;\n else cout << ans << endl;\n }\n else {\n int u, v, w; cin >> u >> v >> w;\n u--; v--;\n qf.merge(u, v, w);\n }\n }\n return 0;\n}", "accuracy": 0.18181818181818182, "time_ms": 150, "memory_kb": 3224, "score_of_the_acc": -0.3171, "final_rank": 19 }, { "submission_id": "aoj_1519_2943840", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nconst long long int INF = 1LL << 60;\ntemplate<typename Type>\nstruct QuickFind {\n int N;\n vector<int> i2g;\n vector< vector<int> > g2i;\n vector<Type> weight;\n\n QuickFind(int N_) {\n N = N_;\n i2g.resize(N);\n g2i.resize(N);\n weight.resize(N);\n\n for(int i=0; i<N; i++) {\n i2g[i] = i;\n g2i[i] = vector<int>{i};\n }\n }\n bool same(int u, int v) {\n return i2g[u] == i2g[v];\n }\n void merge(int u, int v, int w) {\n int ru = i2g[u], rv = i2g[v];\n\n // merge v into u\n if(g2i[ru].size() < g2i[rv].size()) {\n swap(u, v);\n swap(ru, rv);\n w = -w;\n }\n\n for(auto e : g2i[rv]) {\n i2g[e] = ru;\n Type diff = weight[e] - weight[v];\n weight[e] = weight[u] + w + diff;\n }\n\n if(ru != rv) {\n g2i[ru].insert(g2i[ru].end(), g2i[rv].begin(), g2i[rv].end());\n g2i[rv].clear();\n }\n\n weight[u] += abs(w);\n weight[v] += abs(w);\n }\n Type calc_weight(int u, int v) {\n if(i2g[u] != i2g[v]) return INF;\n return weight[v] - weight[u];\n }\n};\n\nint main() {\n int N, Q; cin >> N >> Q;\n\n QuickFind<long long int> qf(N);\n for(int i=0; i<Q; i++) {\n string query; cin >> query;\n\n if(query == \"COMPARE\") {\n int u, v; cin >> u >> v;\n u--; v--;\n long long int ans = qf.calc_weight(u, v);\n if(ans == INF) cout << \"WARNING\" << endl;\n else cout << ans << endl;\n }\n else {\n int u, v, w; cin >> u >> v >> w;\n u--; v--;\n qf.merge(u, v, w);\n }\n }\n return 0;\n}", "accuracy": 0.18181818181818182, "time_ms": 160, "memory_kb": 3224, "score_of_the_acc": -0.3415, "final_rank": 20 }, { "submission_id": "aoj_1519_2693642", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 100000\n\nint boss[NUM],BASE[NUM],ADD[NUM];\n\nint get_boss(int id){\n\tif(boss[id] == id)return id;\n\telse{\n\n\t\tint next_boss = get_boss(boss[id]);\n\n\t\tBASE[id] += BASE[boss[id]];\n\n\t\treturn boss[id] = next_boss;\n\t}\n}\n\nint get_value(int id){\n\treturn BASE[id]+ADD[id]-ADD[boss[id]];\n}\n\nvoid unite(int A,int B,int diff){\n\n\tint boss_A = get_boss(A);\n\tint boss_B = get_boss(B);\n\n\tif(boss_A == boss_B)return;\n\n\tboss[boss_B] = boss_A;\n\tBASE[boss_B] += BASE[A]+ADD[A]+diff-(BASE[B]+ADD[B]);\n}\n\nint main(){\n\n\tint N,Q;\n\tscanf(\"%d %d\",&N,&Q);\n\n\tfor(int i = 0; i < N; i++){\n\t\tboss[i] = i;\n\t\tBASE[i] = 0;\n\t\tADD[i] = 0;\n\t}\n\n\tchar buf[8];\n\n\tint A,B,diff;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%s\",buf);\n\n\t\tif(buf[0] == 'I'){\n\n\t\t\tscanf(\"%d %d %d\",&A,&B,&diff);\n\t\t\tA--;\n\t\t\tB--;\n\n\t\t\tADD[A] += diff;\n\t\t\tADD[B] += diff;\n\n\t\t\tunite(A,B,diff);\n\n\t\t}else{ //compare\n\t\t\tscanf(\"%d %d\",&A,&B);\n\t\t\tA--;\n\t\t\tB--;\n\n\t\t\tif(get_boss(A) != get_boss(B)){\n\t\t\t\tprintf(\"WARNING\\n\");\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tprintf(\"%d\\n\",get_value(B)-get_value(A));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4372, "score_of_the_acc": -0.0458, "final_rank": 1 } ]
aoj_1537_cpp
Problem L: WW Problem 19XX年、nつの国で連合国軍が結成された。連合国軍に属する各国では、敵軍の侵略から自国を守るために国内に1つ以上の拠点を作り、周囲を警戒している。図1は連合国の例を示し、長方形が各国を、その中にある円がその国の拠点を表す。 図1 敵軍が侵略してきた場合、それを一番最初に発見した拠点からその情報を連合国軍に属する他国または自国の拠点に発信することになっている。 情報を他の拠点に伝えるには正の整数のコストがかかる。 また、各国では自国の拠点のうち、連合国軍翻訳通信部(ATIS : Allied Translator and Interpreter Section)に属する兵士が配属されている拠点をちょうど1つだけ持っている。 この兵士だけが連合国軍に属する全ての国の言語を話すことができ、その他の兵士は自分の国の言語しか話せない。 その為、情報を受け取った拠点が他国に情報を伝えようとした際にその拠点がATISに属する兵士をもたない場合、そこから自国のATISに属する兵士をもつ拠点に情報を伝え他国に情報を発信してもらう必要がある。 国毎で各拠点に0から順に[その国がもつ拠点の数-1]までの番号が割り振られており、0番目の拠点にATISに属する兵士がいる。 つまり、他国の拠点に情報を伝達できるのは0番目の拠点だけであり、その他の拠点から他国へは通信できない。 また、その国の0番目の拠点に情報が伝わればその国全体に情報が伝わったものとする。 様々な理由から、全ての拠点の間で通信ができるとは限らない。 ある拠点から別の拠点に情報を伝える事ができたとしても、その逆が成り立つとは限らない。 また、ある拠点から別の拠点に情報を伝えた際のコストとその逆でかかるコストが同じとも限らない。 これらのことを踏まえて、ある拠点が敵軍を発見した際に、連合国軍全体に情報が行き渡るコストの総和が最も少ない伝達方法を知りたい。 連合国軍に属する全ての国の0番目の拠点に対して最初に敵軍を見つけた拠点から情報が伝達された時、連合国軍全体に情報が行き渡ったものとする。 敵軍を発見した拠点が質問として与えられるので、最も少ないコストでその情報が連合国軍全体に行き渡る伝達方法とそのコストを出力せよ。 例えば、以下のケースについて考える ( Sample Input 4、1つめの質問 ) 国neepalの1番目の拠点が敵軍を発見したとする。 この場合、1番目の拠点から他国へは通信できないため、まず自国の0番目の拠点へ情報を伝える。 次に、0番目の拠点から国luxenbourg,nowayに情報を伝える。 neepalからluxenbourgの0番目の拠点へ直接通信することはできるが、3番目の拠点に情報を伝え、そこから0番目の拠点へ伝えたほうがコストが少なく済む。 この場合、全ての国の0番目の国へ情報を伝えるためにかかる最小のコストは12となる。 コストが12で済む伝達方法は複数存在するが、そのうちのどれか一つを答えとして出力すれば良い。 以下の図2の赤い矢印はその答えのうちの1つである。 図2 Input n countryname 0 m 0 countryname 1 m 1 … countryname n-1 m n-1 e c1 0 v1 0 c2 0 v2 0 cost 0 c1 1 v1 1 c2 1 v2 1 cost 1 … c1 e-1 v1 e-1 c2 e-1 v2 e-1 cost e-1 q c3 0 v3 0 c3 1 v3 1 … c3 q-1 v3 q-1 n は連合国軍に属する国の数を表す countryname i は連合国軍に属する i 番目の国名を, m i は i 番目の国の拠点の数を表す e は通信可能な拠点のペアの数を表す 続く e 行には国 c1 i の拠点 v1 i から国 c2 i の拠点 v2 i に情報を伝えるのにかかるコスト cost i が与えられる q は質問の数を表す 続く q 行には敵軍を発見した国の名前 c3 i とその拠点 v3 i が与えられる c1 i , c2 i , c3 i は連合国軍に属する国のいずれかである v1 i , v2 i , v3 i はその国の拠点の数以上である事は無い Constraints 1 ≤ n ≤ 26 1 ≤ m i ≤ 100 0 ≤ e ≤ 322400 0 ≤ cost i ≤ 100 1 ≤ q ≤ 10 countryname は小文字のアルファベット ‘a’ から’z’までで構成される長さ1以上20以下の文字列である 同じ名前の国が2つ以上与えられる事はない 通信可能な拠点のペアの c1 i と c2 i が同じ時、 v1 i と v2 i は異なる 通信可能な拠点のペアの c1 i と c2 i が異なる時、 v1 i は必ず0である Output 各質問に対して以下の形式で答えを出力する。 各質問に対する答えの最後の行には、5つの '-’ からなる"-----” ( “ は除く ) という1行を出力すること。 答えが存在する場合 1行目に質問で与えられた国の拠点から連合国軍全体に情報が行き渡るために必要なコストの最小値を出力する。 2行目以降にはその時の伝達方法を出力する。 伝達方法は以下の形式で表現される。 c4 0 v4 0 c5 0 v5 0 c4 1 v4 1 c5 1 v5 1 ... 国 c4 i の拠点 v4 i から国 c5 i の拠点 v5 i に情報を伝えたことを表す。 以下の事に気をつける事。 通信が行われた順番通りに出力する必要はない コストの総和が最小となる伝達方法が1つ以上存在する場合、そのうちのいずれか1つを出力すれば良い 同じ通信可能なペアを2回以上出力してはいけない c4 i , c5 i は入力で与えられた国のいずれかでなければならない v4 i , v5 i はその国の拠点の数未満でなければならない 答えが存在しない場合 1行に ”Impossible” ( “ は除く ) と出力すること。 Sample Input 1 3 frence 1 usi 1 powland 1 2 usi 0 frence 0 10 frence 0 powland 0 10 2 usi 0 powland 0 Sample Output 1 20 usi 0 frence 0 frence 0 powland 0 ----- Impossible ----- Sample Input 2 2 usso 2 caneda 2 4 usso 1 usso 0 1 usso 0 caneda 0 10 usso 0 caneda 1 2 caneda 1 caneda 0 2 1 usso 1 Sample Output 2 5 usso 1 usso 0 usso 0 caneda 1 caneda 1 caneda 0 ----- Sample Input 3 3 chinax 1 ok 1 austraria 2 6 chinax 0 austra ...(truncated)
[ { "submission_id": "aoj_1537_893213", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int > pii;\n\n\ntypedef vector<vector<int> > matrix;\n\nstruct Node {\n vector<Node> vs;\n vector<pii> con; // (src,to)\n int v;\n void print() {\n if (vs.size() == 0) {\n cout << v;\n } else {\n REP(i,vs.size()) {\n cout << \"[\";\n vs[i].print();\n cout << \"]\";\n cout << \"(\" << con[i].first << \",\" << con[i].second <<\")\";\n }\n }\n }\n bool exist(int a) {\n if (vs.size() == 0) {\n return (v == a);\n } else {\n REP(i,vs.size()) {\n if (vs[i].exist(a)) return 1;\n }\n }\n return 0;\n }\n bool connect(vector<pii> &res, int a) {\n // print();cout << endl;\n // cout << a << endl;\n // cout << \":\" << exist(a) << endl;\n bool f = 0;\n if (vs.size() == 0) {\n f = v == a;\n } else {\n REP(i,vs.size()) {\n int ii = (i+1)%vs.size();\n if (vs[ii].exist(a)) {\n vs[ii].connect(res,a);\n } else {\n res.push_back(con[i]);\n vs[ii].connect(res,con[i].second);\n }\n }\n }\n return f;\n }\n};\n\ntypedef vector<vector<int> > matrix;\nconst int inf = 1<<29;\n \nvoid backward_traverse(int v, int s, int r, matrix &g,\n vector<int> &no, vector< vector<int> > &comp,\n vector<int> &prev, vector<int> &mcost,\n vector<Node> &nodes, vector<pii> &edge,\n vector<int> &mark, int &cost, bool &found) {\n const int n = g.size();\n if (mark[v]) {\n vector<int> temp = no;\n found = true;\n Node node;\n do {\n cost += mcost[v];\n node.con.push_back(edge[v]);\n v = prev[v];\n node.vs.push_back(nodes[v]);\n \n if (v != s) {\n while (comp[v].size() > 0) {\n no[comp[v].back()] = s;\n comp[s].push_back(comp[v].back());\n comp[v].pop_back();\n }\n }\n } while (v != s);\n reverse(ALL(node.vs));\n reverse(ALL(node.con));\n nodes[s] = node;\n for (int j = 0; j < n; ++j)\n if (j != r && no[j] == s)\n for (int i = 0; i < n; ++i)\n if (no[i] != s && g[i][j] < inf) {\n g[i][j] -= mcost[ temp[j] ];\n }\n }\n mark[v] = true;\n for (int i = 0; i < n; ++i)\n if (no[i] != no[v] && prev[ no[i] ] == v)\n if (!mark[ no[i] ] || i == s)\n backward_traverse(i, s, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n}\n\nint minimum_spanning_arborescence(int r, matrix g, vector<pii> &edges) {\n const int n = g.size();\n vector<int> no(n);\n vector< vector<int> > comp(n);\n vector<Node> nodes(n);\n for (int i = 0; i < n; ++i) {\n no[i] = i;\n comp[i].push_back(i);\n nodes[i].v = i;\n }\n int cost = 0;\n vector<int> pprev;\n while (1) {\n vector<int> prev(n, -1);\n vector<int> mcost(n, inf);\n vector<pii> edge(n);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (j == r) continue;\n if (no[i] != no[j] && g[i][j] < inf) {\n if (g[i][j] < mcost[ no[j] ]) {\n mcost[ no[j] ] = g[i][j];\n prev[ no[j] ] = no[i];\n edge[no[j]] = pii(i,j);\n }\n }\n }\n }\n pprev = prev;\n bool stop = true;\n vector<int> mark(n);\n for (int i = 0; i < n; ++i) {\n if (i == r || mark[i] || comp[i].size() == 0) continue;\n bool found = false;\n backward_traverse(i, i, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n if (found) stop = false;\n }\n // cout << \"cost \" << cost << endl;\n // REP(i,n) {\n // REP(j,n) {\n // cout << g[i][j] << \" \";\n // }\n // cout << endl;\n // }\n // cout << \"no: \";\n // FOR(it, no) cout << *it << \" \" ; cout << endl;\n // cout << \"mcost: \";\n // FOR(it, mcost) cout << *it << \" \" ; cout << endl;\n \n // cout << \"prev: \";\n // REP(i,n) cout << prev[i] << \" \"; cout << endl;\n // cout << \"prevprev: \";\n // FOR(it, prevprev) cout << *it << \" \"; cout << endl;\n // cout << endl;\n // nodes[1].print(); cout << endl;\n if (stop) {\n int cnt = 0;\n REP(i,n) {\n if (prev[i] >= 0) {\n nodes[i].connect(edges, edge[i].second);\n // nodes[i].print();\n // cout << \" \" << edge[i].first << \" \" << edge[i].second <<endl;\n edges.push_back(pii(edge[i].first, edge[i].second));\n cost += mcost[i];\n }\n if (prev[no[i]] < 0) cnt++;\n }\n // FOR(it, edges) {\n // cout << it->first << \" \" << it->second << endl;\n // }\n if (cnt != 1) return -1;\n return cost;\n }\n }\n}\n\nstruct Edge {\n int src, dst;\n int weight;\n Edge(int src, int dst, int weight) :\n src(src),dst(dst),weight(weight) {}\n bool operator<(const Edge &rhs) const {\n return weight > rhs.weight;\n }\n};\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvector<int> dijkstra(const Graph &g, int s, vector<int> &prev) {\n int n = g.size();\n vector<int> dist(n,inf);\n dist[s] = 0;\n prev.assign(n, -1);\n priority_queue<Edge> Q; // \"e < f\" <=> \"e.weight > f.weight\"\n for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (prev[e.dst] != -1) continue;\n prev[e.dst] = e.src;\n FOR(f,g[e.dst]) {\n if (dist[f->dst] > e.weight+f->weight) {\n dist[f->dst] = e.weight+f->weight;\n Q.push(Edge(f->src, f->dst, e.weight+f->weight));\n }\n }\n }\n return dist;\n}\n\nvector<int> buildPath(const vector<int> &prev, int t) {\n vector<int> path;\n for (int u = t; u >= 0; u = prev[u])\n path.push_back(u);\n reverse(ALL(path));\n return path;\n}\n\nstring name[100];\nint m[100];\nint sum[100];\npii p[100000];\n\nvector<int> dprev[100];\n\nstring tostr(pii a) {\n stringstream ss;\n ss << name[a.first] << \" \" << a.second;\n return ss.str();\n}\n\nint main() {\n int n;\n while(cin >> n) {\n sum[0] = 0;\n int N = 0;\n map<string, int > mp;\n for (int i=0; i<n; ++i) {\n cin >>name[i] >> m[i];\n sum[i+1] = sum[i] + m[i];\n mp[name[i]] = i;\n N += m[i];\n }\n int cnt = 0;\n for (int i=0; i<n; ++i) {\n for (int j=0; j<m[i]; ++j) {\n p[cnt++] = pii(i,j);\n }\n }\n int E;\n cin >> E;\n Graph g(N);\n for(int i=0; i<E; ++i) {\n string cs1,cs2;\n int v1,v2,c;\n cin >> cs1>>v1>>cs2>>v2>>c;\n int c1 = mp[cs1];\n int c2 = mp[cs2];\n int a = sum[c1] + v1;\n int b = sum[c2] + v2;\n g[a].push_back(Edge(a,b,c));\n }\n matrix mat(n,vector<int>(n));\n vector<int> prev;\n for (int i=0; i<n ;++i) {\n vector<int> dist = dijkstra(g, sum[i], dprev[i]);\n // FOR(it, dprev[i]) cout << \"*\" << *it; cout << endl;\n for (int j=0; j<n; ++j) {\n mat[i][j] = dist[sum[j]];\n }\n }\n // for (int i=0; i<n; ++i) {\n // for (int j=0;j<n; ++j) {\n // cout << mat[i][j] << \" \";\n // }\n // cout << endl;\n // }\n int Q;\n cin >> Q;\n for (int i=0; i<Q; ++i) {\n string cs3;\n int v3;\n cin >> cs3 >> v3;\n int c3 = mp[cs3];\n int a = sum[c3] + v3;\n vector<int> dist = dijkstra(g, a, prev);\n bool ng = 0;\n REP(i,n) {\n if (dist[sum[i]] == inf) {\n ng = 1;\n }\n }\n if (ng) {\n cout << \"Impossible\" << endl;\n } else {\n int res = dist[sum[c3]];\n vector<pii> edges;\n int tmpres = minimum_spanning_arborescence(c3, mat, edges);\n int ans = res + tmpres;\n\n if (res == inf || tmpres < 0) {\n assert(0);\n } else {\n cout << ans << endl;\n vector<int> path = buildPath(prev, sum[c3]);\n set<pii> ST; int cnt = 0;\n REP(i,path.size()-1) {\n ST.insert(pii(path[i], path[i+1])); cnt++;\n // cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n assert(edges.size() == n-1);\n set<int> S;\n FOR(it, edges) S.insert(it->second);\n assert(S.size() == n-1);\n FOR(it, edges) {\n int a = it->second;\n int b = it->first;\n int A = sum[a];\n\n vector<int> path = buildPath(dprev[b], A);\n REP(i,path.size()-1) {\n ST.insert(pii(path[i], path[i+1])); cnt++;\n // cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n }\n FOR(it, ST) {\n cout << tostr(p[it->first]) << \" \" << tostr(p[it->second]) << endl;\n }\n }\n }\n puts(\"-----\");\n }\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 7596, "score_of_the_acc": -2, "final_rank": 2 }, { "submission_id": "aoj_1537_893211", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int > pii;\n\n\ntypedef vector<vector<int> > matrix;\n\nstruct Node {\n vector<Node> vs;\n vector<pii> con; // (src,to)\n int v;\n void print() {\n if (vs.size() == 0) {\n cout << v;\n } else {\n REP(i,vs.size()) {\n cout << \"[\";\n vs[i].print();\n cout << \"]\";\n cout << \"(\" << con[i].first << \",\" << con[i].second <<\")\";\n }\n }\n }\n bool exist(int a) {\n if (vs.size() == 0) {\n return (v == a);\n } else {\n REP(i,vs.size()) {\n if (vs[i].exist(a)) return 1;\n }\n }\n return 0;\n }\n bool connect(vector<pii> &res, int a) {\n // print();cout << endl;\n // cout << a << endl;\n // cout << \":\" << exist(a) << endl;\n bool f = 0;\n if (vs.size() == 0) {\n f = v == a;\n } else {\n REP(i,vs.size()) {\n int ii = (i+1)%vs.size();\n if (vs[ii].exist(a)) {\n vs[ii].connect(res,a);\n } else {\n res.push_back(con[i]);\n vs[ii].connect(res,con[i].second);\n }\n }\n }\n return f;\n }\n};\n\ntypedef vector<vector<int> > matrix;\nconst int inf = 1<<29;\n \nvoid backward_traverse(int v, int s, int r, matrix &g,\n vector<int> &no, vector< vector<int> > &comp,\n vector<int> &prev, vector<int> &mcost,\n vector<Node> &nodes, vector<pii> &edge,\n vector<int> &mark, int &cost, bool &found) {\n const int n = g.size();\n if (mark[v]) {\n vector<int> temp = no;\n found = true;\n Node node;\n do {\n cost += mcost[v];\n node.con.push_back(edge[v]);\n v = prev[v];\n node.vs.push_back(nodes[v]);\n \n if (v != s) {\n while (comp[v].size() > 0) {\n no[comp[v].back()] = s;\n comp[s].push_back(comp[v].back());\n comp[v].pop_back();\n }\n }\n } while (v != s);\n reverse(ALL(node.vs));\n reverse(ALL(node.con));\n nodes[s] = node;\n for (int j = 0; j < n; ++j)\n if (j != r && no[j] == s)\n for (int i = 0; i < n; ++i)\n if (no[i] != s && g[i][j] < inf) {\n g[i][j] -= mcost[ temp[j] ];\n }\n }\n mark[v] = true;\n for (int i = 0; i < n; ++i)\n if (no[i] != no[v] && prev[ no[i] ] == v)\n if (!mark[ no[i] ] || i == s)\n backward_traverse(i, s, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n}\n\nint minimum_spanning_arborescence(int r, matrix g, vector<pii> &edges) {\n const int n = g.size();\n vector<int> no(n);\n vector< vector<int> > comp(n);\n vector<Node> nodes(n);\n for (int i = 0; i < n; ++i) {\n no[i] = i;\n comp[i].push_back(i);\n nodes[i].v = i;\n }\n int cost = 0;\n vector<int> pprev;\n while (1) {\n vector<int> prev(n, -1);\n vector<int> mcost(n, inf);\n vector<pii> edge(n);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (j == r) continue;\n if (no[i] != no[j] && g[i][j] < inf) {\n if (g[i][j] < mcost[ no[j] ]) {\n mcost[ no[j] ] = g[i][j];\n prev[ no[j] ] = no[i];\n edge[no[j]] = pii(i,j);\n }\n }\n }\n }\n pprev = prev;\n bool stop = true;\n vector<int> mark(n);\n for (int i = 0; i < n; ++i) {\n if (i == r || mark[i] || comp[i].size() == 0) continue;\n bool found = false;\n backward_traverse(i, i, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n if (found) stop = false;\n }\n // cout << \"cost \" << cost << endl;\n // REP(i,n) {\n // REP(j,n) {\n // cout << g[i][j] << \" \";\n // }\n // cout << endl;\n // }\n // cout << \"no: \";\n // FOR(it, no) cout << *it << \" \" ; cout << endl;\n // cout << \"mcost: \";\n // FOR(it, mcost) cout << *it << \" \" ; cout << endl;\n \n // cout << \"prev: \";\n // REP(i,n) cout << prev[i] << \" \"; cout << endl;\n // cout << \"prevprev: \";\n // FOR(it, prevprev) cout << *it << \" \"; cout << endl;\n // cout << endl;\n // nodes[1].print(); cout << endl;\n if (stop) {\n int cnt = 0;\n REP(i,n) {\n if (prev[i] >= 0) {\n nodes[i].connect(edges, edge[i].second);\n // nodes[i].print();\n // cout << \" \" << edge[i].first << \" \" << edge[i].second <<endl;\n edges.push_back(pii(edge[i].first, edge[i].second));\n cost += mcost[i];\n }\n if (prev[no[i]] < 0) cnt++;\n }\n // FOR(it, edges) {\n // cout << it->first << \" \" << it->second << endl;\n // }\n if (cnt != 1) return -1;\n return cost;\n }\n }\n}\n\nstruct Edge {\n int src, dst;\n int weight;\n Edge(int src, int dst, int weight) :\n src(src),dst(dst),weight(weight) {}\n bool operator<(const Edge &rhs) const {\n return weight > rhs.weight;\n }\n};\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvector<int> dijkstra(const Graph &g, int s, vector<int> &prev) {\n int n = g.size();\n vector<int> dist(n,inf);\n dist[s] = 0;\n prev.assign(n, -1);\n priority_queue<Edge> Q; // \"e < f\" <=> \"e.weight > f.weight\"\n for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (prev[e.dst] != -1) continue;\n prev[e.dst] = e.src;\n FOR(f,g[e.dst]) {\n if (dist[f->dst] > e.weight+f->weight) {\n dist[f->dst] = e.weight+f->weight;\n Q.push(Edge(f->src, f->dst, e.weight+f->weight));\n }\n }\n }\n return dist;\n}\n\nvector<int> buildPath(const vector<int> &prev, int t) {\n vector<int> path;\n for (int u = t; u >= 0; u = prev[u])\n path.push_back(u);\n reverse(ALL(path));\n return path;\n}\n\nstring name[100];\nint m[100];\nint sum[100];\npii p[100000];\n\nvector<int> dprev[100];\n\nstring tostr(pii a) {\n stringstream ss;\n ss << name[a.first] << \" \" << a.second;\n return ss.str();\n}\n\nint main() {\n int n;\n while(cin >> n) {\n sum[0] = 0;\n int N = 0;\n map<string, int > mp;\n for (int i=0; i<n; ++i) {\n cin >>name[i] >> m[i];\n sum[i+1] = sum[i] + m[i];\n mp[name[i]] = i;\n N += m[i];\n }\n int cnt = 0;\n for (int i=0; i<n; ++i) {\n for (int j=0; j<m[i]; ++j) {\n p[cnt++] = pii(i,j);\n }\n }\n int E;\n cin >> E;\n Graph g(N);\n for(int i=0; i<E; ++i) {\n string cs1,cs2;\n int v1,v2,c;\n cin >> cs1>>v1>>cs2>>v2>>c;\n int c1 = mp[cs1];\n int c2 = mp[cs2];\n int a = sum[c1] + v1;\n int b = sum[c2] + v2;\n g[a].push_back(Edge(a,b,c));\n }\n matrix mat(n,vector<int>(n));\n vector<int> prev;\n for (int i=0; i<n ;++i) {\n vector<int> dist = dijkstra(g, sum[i], dprev[i]);\n // FOR(it, dprev[i]) cout << \"*\" << *it; cout << endl;\n for (int j=0; j<n; ++j) {\n mat[i][j] = dist[sum[j]];\n }\n }\n // for (int i=0; i<n; ++i) {\n // for (int j=0;j<n; ++j) {\n // cout << mat[i][j] << \" \";\n // }\n // cout << endl;\n // }\n int Q;\n cin >> Q;\n for (int i=0; i<Q; ++i) {\n string cs3;\n int v3;\n cin >> cs3 >> v3;\n int c3 = mp[cs3];\n int a = sum[c3] + v3;\n vector<int> dist = dijkstra(g, a, prev);\n bool ng = 0;\n REP(i,n) {\n if (dist[sum[i]] == inf) {\n ng = 1;\n }\n }\n if (ng) {\n cout << \"Impossible\" << endl;\n } else {\n int res = dist[sum[c3]];\n vector<pii> edges;\n int tmpres = minimum_spanning_arborescence(c3, mat, edges);\n int ans = res + tmpres;\n\n if (res == inf || tmpres < 0) {\n assert(0);\n } else {\n cout << ans << endl;\n vector<int> path = buildPath(prev, sum[c3]);\n set<pii> ST; int cnt = 0;\n REP(i,path.size()-1) {\n ST.insert(pii(path[i], path[i+1])); cnt++;\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n assert(edges.size() == n-1);\n set<int> S;\n FOR(it, edges) S.insert(it->second);\n assert(S.size() == n-1);\n FOR(it, edges) {\n int a = it->second;\n int b = it->first;\n int A = sum[a];\n\n vector<int> path = buildPath(dprev[b], A);\n REP(i,path.size()-1) {\n ST.insert(pii(path[i], path[i+1])); cnt++;\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n }\n assert(ST.size() == cnt);\n }\n }\n puts(\"-----\");\n }\n }\n}", "accuracy": 0.4222222222222222, "time_ms": 150, "memory_kb": 4788, "score_of_the_acc": -0.3625, "final_rank": 4 }, { "submission_id": "aoj_1537_893208", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int > pii;\n\n\ntypedef vector<vector<int> > matrix;\n\nstruct Node {\n vector<Node> vs;\n vector<pii> con; // (src,to)\n int v;\n void print() {\n if (vs.size() == 0) {\n cout << v;\n } else {\n REP(i,vs.size()) {\n cout << \"[\";\n vs[i].print();\n cout << \"]\";\n cout << \"(\" << con[i].first << \",\" << con[i].second <<\")\";\n }\n }\n }\n bool exist(int a) {\n if (vs.size() == 0) {\n return (v == a);\n } else {\n REP(i,vs.size()) {\n if (vs[i].exist(a)) return 1;\n }\n }\n return 0;\n }\n bool connect(vector<pii> &res, int a) {\n // print();cout << endl;\n // cout << a << endl;\n // cout << \":\" << exist(a) << endl;\n bool f = 0;\n if (vs.size() == 0) {\n f = v == a;\n } else {\n REP(i,vs.size()) {\n int ii = (i+1)%vs.size();\n if (vs[ii].exist(a)) {\n vs[ii].connect(res,a);\n } else {\n res.push_back(con[i]);\n vs[ii].connect(res,con[i].second);\n }\n }\n }\n return f;\n }\n};\n\ntypedef vector<vector<int> > matrix;\nconst int inf = 1<<29;\n \nvoid backward_traverse(int v, int s, int r, matrix &g,\n vector<int> &no, vector< vector<int> > &comp,\n vector<int> &prev, vector<int> &mcost,\n vector<Node> &nodes, vector<pii> &edge,\n vector<int> &mark, int &cost, bool &found) {\n const int n = g.size();\n if (mark[v]) {\n vector<int> temp = no;\n found = true;\n Node node;\n do {\n cost += mcost[v];\n node.con.push_back(edge[v]);\n v = prev[v];\n node.vs.push_back(nodes[v]);\n \n if (v != s) {\n while (comp[v].size() > 0) {\n no[comp[v].back()] = s;\n comp[s].push_back(comp[v].back());\n comp[v].pop_back();\n }\n }\n } while (v != s);\n reverse(ALL(node.vs));\n reverse(ALL(node.con));\n nodes[s] = node;\n for (int j = 0; j < n; ++j)\n if (j != r && no[j] == s)\n for (int i = 0; i < n; ++i)\n if (no[i] != s && g[i][j] < inf) {\n g[i][j] -= mcost[ temp[j] ];\n }\n }\n mark[v] = true;\n for (int i = 0; i < n; ++i)\n if (no[i] != no[v] && prev[ no[i] ] == v)\n if (!mark[ no[i] ] || i == s)\n backward_traverse(i, s, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n}\n\nint minimum_spanning_arborescence(int r, matrix g, vector<pii> &edges) {\n const int n = g.size();\n vector<int> no(n);\n vector< vector<int> > comp(n);\n vector<Node> nodes(n);\n for (int i = 0; i < n; ++i) {\n no[i] = i;\n comp[i].push_back(i);\n nodes[i].v = i;\n }\n int cost = 0;\n vector<int> pprev;\n while (1) {\n vector<int> prev(n, -1);\n vector<int> mcost(n, inf);\n vector<pii> edge(n);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (j == r) continue;\n if (no[i] != no[j] && g[i][j] < inf) {\n if (g[i][j] < mcost[ no[j] ]) {\n mcost[ no[j] ] = g[i][j];\n prev[ no[j] ] = no[i];\n edge[no[j]] = pii(i,j);\n }\n }\n }\n }\n pprev = prev;\n bool stop = true;\n vector<int> mark(n);\n for (int i = 0; i < n; ++i) {\n if (i == r || mark[i] || comp[i].size() == 0) continue;\n bool found = false;\n backward_traverse(i, i, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n if (found) stop = false;\n }\n // cout << \"cost \" << cost << endl;\n // REP(i,n) {\n // REP(j,n) {\n // cout << g[i][j] << \" \";\n // }\n // cout << endl;\n // }\n // cout << \"no: \";\n // FOR(it, no) cout << *it << \" \" ; cout << endl;\n // cout << \"mcost: \";\n // FOR(it, mcost) cout << *it << \" \" ; cout << endl;\n \n // cout << \"prev: \";\n // REP(i,n) cout << prev[i] << \" \"; cout << endl;\n // cout << \"prevprev: \";\n // FOR(it, prevprev) cout << *it << \" \"; cout << endl;\n // cout << endl;\n // nodes[1].print(); cout << endl;\n if (stop) {\n int cnt = 0;\n REP(i,n) {\n if (prev[i] >= 0) {\n nodes[i].connect(edges, edge[i].second);\n // nodes[i].print();\n // cout << \" \" << edge[i].first << \" \" << edge[i].second <<endl;\n edges.push_back(pii(edge[i].first, edge[i].second));\n cost += mcost[i];\n }\n if (prev[no[i]] < 0) cnt++;\n }\n // FOR(it, edges) {\n // cout << it->first << \" \" << it->second << endl;\n // }\n if (cnt != 1) return -1;\n return cost;\n }\n }\n}\n\nstruct Edge {\n int src, dst;\n int weight;\n Edge(int src, int dst, int weight) :\n src(src),dst(dst),weight(weight) {}\n bool operator<(const Edge &rhs) const {\n return weight > rhs.weight;\n }\n};\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvector<int> dijkstra(const Graph &g, int s, vector<int> &prev) {\n int n = g.size();\n vector<int> dist(n,inf);\n dist[s] = 0;\n prev.assign(n, -1);\n priority_queue<Edge> Q; // \"e < f\" <=> \"e.weight > f.weight\"\n for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (prev[e.dst] != -1) continue;\n prev[e.dst] = e.src;\n FOR(f,g[e.dst]) {\n if (dist[f->dst] > e.weight+f->weight) {\n dist[f->dst] = e.weight+f->weight;\n Q.push(Edge(f->src, f->dst, e.weight+f->weight));\n }\n }\n }\n return dist;\n}\n\nvector<int> buildPath(const vector<int> &prev, int t) {\n vector<int> path;\n for (int u = t; u >= 0; u = prev[u])\n path.push_back(u);\n reverse(ALL(path));\n return path;\n}\n\nstring name[100];\nint m[100];\nint sum[100];\npii p[100000];\n\nvector<int> dprev[100];\n\nstring tostr(pii a) {\n stringstream ss;\n ss << name[a.first] << \" \" << a.second;\n return ss.str();\n}\n\nint main() {\n int n;\n while(cin >> n) {\n sum[0] = 0;\n int N = 0;\n map<string, int > mp;\n for (int i=0; i<n; ++i) {\n cin >>name[i] >> m[i];\n sum[i+1] = sum[i] + m[i];\n mp[name[i]] = i;\n N += m[i];\n }\n int cnt = 0;\n for (int i=0; i<n; ++i) {\n for (int j=0; j<m[i]; ++j) {\n p[cnt++] = pii(i,j);\n }\n }\n int E;\n cin >> E;\n Graph g(N);\n for(int i=0; i<E; ++i) {\n string cs1,cs2;\n int v1,v2,c;\n cin >> cs1>>v1>>cs2>>v2>>c;\n int c1 = mp[cs1];\n int c2 = mp[cs2];\n int a = sum[c1] + v1;\n int b = sum[c2] + v2;\n g[a].push_back(Edge(a,b,c));\n }\n matrix mat(n,vector<int>(n));\n vector<int> prev;\n for (int i=0; i<n ;++i) {\n vector<int> dist = dijkstra(g, sum[i], dprev[i]);\n // FOR(it, dprev[i]) cout << \"*\" << *it; cout << endl;\n for (int j=0; j<n; ++j) {\n mat[i][j] = dist[sum[j]];\n }\n }\n // for (int i=0; i<n; ++i) {\n // for (int j=0;j<n; ++j) {\n // cout << mat[i][j] << \" \";\n // }\n // cout << endl;\n // }\n int Q;\n cin >> Q;\n for (int i=0; i<Q; ++i) {\n string cs3;\n int v3;\n cin >> cs3 >> v3;\n int c3 = mp[cs3];\n int a = sum[c3] + v3;\n vector<int> dist = dijkstra(g, a, prev);\n bool ng = 0;\n REP(i,n) {\n if (dist[sum[i]] == inf) {\n ng = 1;\n }\n }\n if (ng) {\n cout << \"Impossible\" << endl;\n } else {\n int res = dist[sum[c3]];\n vector<pii> edges;\n int tmpres = minimum_spanning_arborescence(c3, mat, edges);\n int ans = res + tmpres;\n\n if (res == inf || tmpres < 0) {\n assert(0);\n } else {\n cout << ans << endl;\n vector<int> path = buildPath(prev, sum[c3]);\n REP(i,path.size()-1) {\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n assert(edges.size() == n-1);\n set<int> S;\n FOR(it, edges) S.insert(it->second);\n assert(S.size() == n-1);\n FOR(it, edges) {\n int a = it->second;\n int b = it->first;\n int A = sum[a];\n\n vector<int> path = buildPath(dprev[b], A);\n REP(i,path.size()-1) {\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n }\n \n }\n }\n puts(\"-----\");\n }\n }\n}", "accuracy": 0.4222222222222222, "time_ms": 150, "memory_kb": 4788, "score_of_the_acc": -0.3625, "final_rank": 4 }, { "submission_id": "aoj_1537_893207", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int > pii;\n\n\ntypedef vector<vector<int> > matrix;\n\nstruct Node {\n vector<Node> vs;\n vector<pii> con; // (src,to)\n int v;\n void print() {\n if (vs.size() == 0) {\n cout << v;\n } else {\n REP(i,vs.size()) {\n cout << \"[\";\n vs[i].print();\n cout << \"]\";\n cout << \"(\" << con[i].first << \",\" << con[i].second <<\")\";\n }\n }\n }\n bool exist(int a) {\n if (vs.size() == 0) {\n return (v == a);\n } else {\n REP(i,vs.size()) {\n if (vs[i].exist(a)) return 1;\n }\n }\n return 0;\n }\n bool connect(vector<pii> &res, int a) {\n // print();cout << endl;\n // cout << a << endl;\n // cout << \":\" << exist(a) << endl;\n bool f = 0;\n if (vs.size() == 0) {\n f = v == a;\n } else {\n REP(i,vs.size()) {\n int ii = (i+1)%vs.size();\n if (vs[ii].exist(a)) {\n vs[ii].connect(res,a);\n } else {\n res.push_back(con[i]);\n vs[ii].connect(res,con[i].second);\n }\n }\n }\n return f;\n }\n};\n\ntypedef vector<vector<int> > matrix;\nconst int inf = 1<<29;\n \nvoid backward_traverse(int v, int s, int r, matrix &g,\n vector<int> &no, vector< vector<int> > &comp,\n vector<int> &prev, vector<int> &mcost,\n vector<Node> &nodes, vector<pii> &edge,\n vector<int> &mark, int &cost, bool &found) {\n const int n = g.size();\n if (mark[v]) {\n vector<int> temp = no;\n found = true;\n Node node;\n do {\n cost += mcost[v];\n node.con.push_back(edge[v]);\n v = prev[v];\n node.vs.push_back(nodes[v]);\n \n if (v != s) {\n while (comp[v].size() > 0) {\n no[comp[v].back()] = s;\n comp[s].push_back(comp[v].back());\n comp[v].pop_back();\n }\n }\n } while (v != s);\n reverse(ALL(node.vs));\n reverse(ALL(node.con));\n nodes[s] = node;\n for (int j = 0; j < n; ++j)\n if (j != r && no[j] == s)\n for (int i = 0; i < n; ++i)\n if (no[i] != s && g[i][j] < inf) {\n g[i][j] -= mcost[ temp[j] ];\n }\n }\n mark[v] = true;\n for (int i = 0; i < n; ++i)\n if (no[i] != no[v] && prev[ no[i] ] == v)\n if (!mark[ no[i] ] || i == s)\n backward_traverse(i, s, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n}\n\nint minimum_spanning_arborescence(int r, matrix g, vector<pii> &edges) {\n const int n = g.size();\n vector<int> no(n);\n vector< vector<int> > comp(n);\n vector<Node> nodes(n);\n for (int i = 0; i < n; ++i) {\n no[i] = i;\n comp[i].push_back(i);\n nodes[i].v = i;\n }\n int cost = 0;\n vector<int> pprev;\n while (1) {\n vector<int> prev(n, -1);\n vector<int> mcost(n, inf);\n vector<pii> edge(n);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (j == r) continue;\n if (no[i] != no[j] && g[i][j] < inf) {\n if (g[i][j] < mcost[ no[j] ]) {\n mcost[ no[j] ] = g[i][j];\n prev[ no[j] ] = no[i];\n edge[no[j]] = pii(i,j);\n }\n }\n }\n }\n pprev = prev;\n bool stop = true;\n vector<int> mark(n);\n for (int i = 0; i < n; ++i) {\n if (i == r || mark[i] || comp[i].size() == 0) continue;\n bool found = false;\n backward_traverse(i, i, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n if (found) stop = false;\n }\n // cout << \"cost \" << cost << endl;\n // REP(i,n) {\n // REP(j,n) {\n // cout << g[i][j] << \" \";\n // }\n // cout << endl;\n // }\n // cout << \"no: \";\n // FOR(it, no) cout << *it << \" \" ; cout << endl;\n // cout << \"mcost: \";\n // FOR(it, mcost) cout << *it << \" \" ; cout << endl;\n \n // cout << \"prev: \";\n // REP(i,n) cout << prev[i] << \" \"; cout << endl;\n // cout << \"prevprev: \";\n // FOR(it, prevprev) cout << *it << \" \"; cout << endl;\n // cout << endl;\n // nodes[1].print(); cout << endl;\n if (stop) {\n int cnt = 0;\n REP(i,n) {\n if (prev[i] >= 0) {\n nodes[i].connect(edges, edge[i].second);\n // nodes[i].print();\n // cout << \" \" << edge[i].first << \" \" << edge[i].second <<endl;\n edges.push_back(pii(edge[i].first, edge[i].second));\n cost += mcost[i];\n }\n if (prev[no[i]] < 0) cnt++;\n }\n // FOR(it, edges) {\n // cout << it->first << \" \" << it->second << endl;\n // }\n if (cnt != 1) return -1;\n return cost;\n }\n }\n}\n\nstruct Edge {\n int src, dst;\n int weight;\n Edge(int src, int dst, int weight) :\n src(src),dst(dst),weight(weight) {}\n bool operator<(const Edge &rhs) const {\n return weight > rhs.weight;\n }\n};\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvector<int> dijkstra(const Graph &g, int s, vector<int> &prev) {\n int n = g.size();\n vector<int> dist(n,inf);\n dist[s] = 0;\n prev.assign(n, -1);\n priority_queue<Edge> Q; // \"e < f\" <=> \"e.weight > f.weight\"\n for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (prev[e.dst] != -1) continue;\n prev[e.dst] = e.src;\n FOR(f,g[e.dst]) {\n if (dist[f->dst] > e.weight+f->weight) {\n dist[f->dst] = e.weight+f->weight;\n Q.push(Edge(f->src, f->dst, e.weight+f->weight));\n }\n }\n }\n return dist;\n}\n\nvector<int> buildPath(const vector<int> &prev, int t) {\n vector<int> path;\n for (int u = t; u >= 0; u = prev[u])\n path.push_back(u);\n reverse(ALL(path));\n return path;\n}\n\nstring name[100];\nint m[100];\nint sum[100];\npii p[100000];\n\nvector<int> dprev[100];\n\nstring tostr(pii a) {\n stringstream ss;\n ss << name[a.first] << \" \" << a.second;\n return ss.str();\n}\n\nint main() {\n int n;\n while(cin >> n) {\n sum[0] = 0;\n int N = 0;\n map<string, int > mp;\n for (int i=0; i<n; ++i) {\n cin >>name[i] >> m[i];\n sum[i+1] = sum[i] + m[i];\n mp[name[i]] = i;\n N += m[i];\n }\n int cnt = 0;\n for (int i=0; i<n; ++i) {\n for (int j=0; j<m[i]; ++j) {\n p[cnt++] = pii(i,j);\n }\n }\n int E;\n cin >> E;\n Graph g(N);\n for(int i=0; i<E; ++i) {\n string cs1,cs2;\n int v1,v2,c;\n cin >> cs1>>v1>>cs2>>v2>>c;\n int c1 = mp[cs1];\n int c2 = mp[cs2];\n int a = sum[c1] + v1;\n int b = sum[c2] + v2;\n g[a].push_back(Edge(a,b,c));\n }\n matrix mat(n,vector<int>(n));\n vector<int> prev;\n for (int i=0; i<n ;++i) {\n vector<int> dist = dijkstra(g, sum[i], dprev[i]);\n // FOR(it, dprev[i]) cout << \"*\" << *it; cout << endl;\n for (int j=0; j<n; ++j) {\n mat[i][j] = dist[sum[j]];\n }\n }\n // for (int i=0; i<n; ++i) {\n // for (int j=0;j<n; ++j) {\n // cout << mat[i][j] << \" \";\n // }\n // cout << endl;\n // }\n int Q;\n cin >> Q;\n for (int i=0; i<Q; ++i) {\n string cs3;\n int v3;\n cin >> cs3 >> v3;\n int c3 = mp[cs3];\n int a = sum[c3] + v3;\n vector<int> dist = dijkstra(g, a, prev);\n bool ng = 0;\n REP(i,n) {\n if (dist[sum[i]] == inf) {\n ng = 1;\n }\n }\n if (ng) {\n cout << \"Impossible\" << endl;\n } else {\n int res = dist[sum[c3]];\n vector<pii> edges;\n int tmpres = minimum_spanning_arborescence(c3, mat, edges);\n int ans = res + tmpres;\n\n if (res == inf || tmpres < 0) {\n assert(0);\n } else {\n cout << ans << endl;\n vector<int> path = buildPath(prev, sum[c3]);\n REP(i,path.size()-1) {\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n assert(edges.size() == n-1);\n FOR(it, edges) {\n int a = it->second;\n int b = it->first;\n int A = sum[a];\n\n vector<int> path = buildPath(dprev[b], A);\n REP(i,path.size()-1) {\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n }\n \n }\n }\n puts(\"-----\");\n }\n }\n}", "accuracy": 0.4222222222222222, "time_ms": 150, "memory_kb": 4788, "score_of_the_acc": -0.3625, "final_rank": 4 }, { "submission_id": "aoj_1537_893204", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int > pii;\n\n\ntypedef vector<vector<int> > matrix;\n\nstruct Node {\n vector<Node> vs;\n vector<pii> con; // (src,to)\n int v;\n void print() {\n if (vs.size() == 0) {\n cout << v;\n } else {\n REP(i,vs.size()) {\n cout << \"[\";\n vs[i].print();\n cout << \"]\";\n cout << \"(\" << con[i].first << \",\" << con[i].second <<\")\";\n }\n }\n }\n bool exist(int a) {\n if (vs.size() == 0) {\n return (v == a);\n } else {\n REP(i,vs.size()) {\n if (vs[i].exist(a)) return 1;\n }\n }\n return 0;\n }\n bool connect(vector<pii> &res, int a) {\n // print();cout << endl;\n // cout << a << endl;\n // cout << \":\" << exist(a) << endl;\n bool f = 0;\n if (vs.size() == 0) {\n f = v == a;\n } else {\n REP(i,vs.size()) {\n int ii = (i+1)%vs.size();\n if (vs[ii].exist(a)) {\n vs[ii].connect(res,a);\n } else {\n res.push_back(con[i]);\n vs[ii].connect(res,con[i].second);\n }\n }\n }\n return f;\n }\n};\n\ntypedef vector<vector<int> > matrix;\nconst int inf = 1<<29;\n \nvoid backward_traverse(int v, int s, int r, matrix &g,\n vector<int> &no, vector< vector<int> > &comp,\n vector<int> &prev, vector<int> &mcost,\n vector<Node> &nodes, vector<pii> &edge,\n vector<int> &mark, int &cost, bool &found) {\n const int n = g.size();\n if (mark[v]) {\n vector<int> temp = no;\n found = true;\n Node node;\n do {\n cost += mcost[v];\n node.con.push_back(edge[v]);\n v = prev[v];\n node.vs.push_back(nodes[v]);\n \n if (v != s) {\n while (comp[v].size() > 0) {\n no[comp[v].back()] = s;\n comp[s].push_back(comp[v].back());\n comp[v].pop_back();\n }\n }\n } while (v != s);\n reverse(ALL(node.vs));\n reverse(ALL(node.con));\n nodes[s] = node;\n for (int j = 0; j < n; ++j)\n if (j != r && no[j] == s)\n for (int i = 0; i < n; ++i)\n if (no[i] != s && g[i][j] < inf) {\n g[i][j] -= mcost[ temp[j] ];\n }\n }\n mark[v] = true;\n for (int i = 0; i < n; ++i)\n if (no[i] != no[v] && prev[ no[i] ] == v)\n if (!mark[ no[i] ] || i == s)\n backward_traverse(i, s, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n}\n\nint minimum_spanning_arborescence(int r, matrix g, vector<pii> &edges) {\n const int n = g.size();\n vector<int> no(n);\n vector< vector<int> > comp(n);\n vector<Node> nodes(n);\n for (int i = 0; i < n; ++i) {\n no[i] = i;\n comp[i].push_back(i);\n nodes[i].v = i;\n }\n int cost = 0;\n vector<int> pprev;\n while (1) {\n vector<int> prev(n, -1);\n vector<int> mcost(n, inf);\n vector<pii> edge(n);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (j == r) continue;\n if (no[i] != no[j] && g[i][j] < inf) {\n if (g[i][j] < mcost[ no[j] ]) {\n mcost[ no[j] ] = g[i][j];\n prev[ no[j] ] = no[i];\n edge[no[j]] = pii(i,j);\n }\n }\n }\n }\n pprev = prev;\n bool stop = true;\n vector<int> mark(n);\n for (int i = 0; i < n; ++i) {\n if (i == r || mark[i] || comp[i].size() == 0) continue;\n bool found = false;\n backward_traverse(i, i, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n if (found) stop = false;\n }\n // cout << \"cost \" << cost << endl;\n // REP(i,n) {\n // REP(j,n) {\n // cout << g[i][j] << \" \";\n // }\n // cout << endl;\n // }\n // cout << \"no: \";\n // FOR(it, no) cout << *it << \" \" ; cout << endl;\n // cout << \"mcost: \";\n // FOR(it, mcost) cout << *it << \" \" ; cout << endl;\n \n // cout << \"prev: \";\n // REP(i,n) cout << prev[i] << \" \"; cout << endl;\n // cout << \"prevprev: \";\n // FOR(it, prevprev) cout << *it << \" \"; cout << endl;\n // cout << endl;\n // nodes[1].print(); cout << endl;\n if (stop) {\n int cnt = 0;\n REP(i,n) {\n if (prev[i] >= 0) {\n nodes[i].connect(edges, edge[i].second);\n // nodes[i].print();\n // cout << \" \" << edge[i].first << \" \" << edge[i].second <<endl;\n edges.push_back(pii(edge[i].first, edge[i].second));\n cost += mcost[i];\n }\n if (prev[no[i]] < 0) cnt++;\n }\n // FOR(it, edges) {\n // cout << it->first << \" \" << it->second << endl;\n // }\n if (cnt != 1) return -1;\n return cost;\n }\n }\n}\n\nstruct Edge {\n int src, dst;\n int weight;\n Edge(int src, int dst, int weight) :\n src(src),dst(dst),weight(weight) {}\n bool operator<(const Edge &rhs) const {\n return weight > rhs.weight;\n }\n};\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvector<int> dijkstra(const Graph &g, int s, vector<int> &prev) {\n int n = g.size();\n vector<int> dist(n,inf);\n dist[s] = 0;\n prev.assign(n, -1);\n priority_queue<Edge> Q; // \"e < f\" <=> \"e.weight > f.weight\"\n for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (prev[e.dst] != -1) continue;\n prev[e.dst] = e.src;\n FOR(f,g[e.dst]) {\n if (dist[f->dst] > e.weight+f->weight) {\n dist[f->dst] = e.weight+f->weight;\n Q.push(Edge(f->src, f->dst, e.weight+f->weight));\n }\n }\n }\n return dist;\n}\n\nvector<int> buildPath(const vector<int> &prev, int t) {\n vector<int> path;\n for (int u = t; u >= 0; u = prev[u])\n path.push_back(u);\n reverse(ALL(path));\n return path;\n}\n\nstring name[100];\nint m[100];\nint sum[100];\npii p[100000];\n\nvector<int> dprev[100];\n\nstring tostr(pii a) {\n stringstream ss;\n ss << name[a.first] << \" \" << a.second;\n return ss.str();\n}\n\nint main() {\n int n;\n while(cin >> n) {\n sum[0] = 0;\n int N = 0;\n map<string, int > mp;\n for (int i=0; i<n; ++i) {\n cin >>name[i] >> m[i];\n sum[i+1] = sum[i] + m[i];\n mp[name[i]] = i;\n N += m[i];\n }\n int cnt = 0;\n for (int i=0; i<n; ++i) {\n for (int j=0; j<m[i]; ++j) {\n p[cnt++] = pii(i,j);\n }\n }\n int E;\n cin >> E;\n Graph g(N);\n for(int i=0; i<E; ++i) {\n string cs1,cs2;\n int v1,v2,c;\n cin >> cs1>>v1>>cs2>>v2>>c;\n int c1 = mp[cs1];\n int c2 = mp[cs2];\n int a = sum[c1] + v1;\n int b = sum[c2] + v2;\n g[a].push_back(Edge(a,b,c));\n }\n matrix mat(n,vector<int>(n));\n vector<int> prev;\n for (int i=0; i<n ;++i) {\n vector<int> dist = dijkstra(g, sum[i], dprev[i]);\n // FOR(it, dprev[i]) cout << \"*\" << *it; cout << endl;\n for (int j=0; j<n; ++j) {\n mat[i][j] = dist[sum[j]];\n }\n }\n // for (int i=0; i<n; ++i) {\n // for (int j=0;j<n; ++j) {\n // cout << mat[i][j] << \" \";\n // }\n // cout << endl;\n // }\n int Q;\n cin >> Q;\n for (int i=0; i<Q; ++i) {\n string cs3;\n int v3;\n cin >> cs3 >> v3;\n int c3 = mp[cs3];\n int a = sum[c3] + v3;\n vector<int> dist = dijkstra(g, a, prev);\n bool ng = 0;\n REP(i,n) {\n if (dist[sum[i]] == inf) {\n ng = 1;\n }\n }\n if (ng) {\n cout << \"Impossible\" << endl;\n } else {\n int res = dist[sum[c3]];\n vector<pii> edges;\n int tmpres = minimum_spanning_arborescence(c3, mat, edges);\n int ans = res + tmpres;\n\n if (res == inf || tmpres < 0) {\n assert(0);\n } else {\n cout << ans << endl;\n vector<int> path = buildPath(prev, sum[c3]);\n REP(i,path.size()-1) {\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n FOR(it, edges) {\n int a = it->second;\n int b = it->first;\n int A = sum[a];\n\n vector<int> path = buildPath(dprev[b], A);\n REP(i,path.size()-1) {\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n }\n \n }\n }\n puts(\"-----\");\n }\n }\n}", "accuracy": 0.4222222222222222, "time_ms": 150, "memory_kb": 4788, "score_of_the_acc": -0.3625, "final_rank": 4 }, { "submission_id": "aoj_1537_893203", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int > pii;\n\n\ntypedef vector<vector<int> > matrix;\n\nstruct Node {\n vector<Node> vs;\n vector<pii> con; // (src,to)\n int v;\n void print() {\n if (vs.size() == 0) {\n cout << v;\n } else {\n REP(i,vs.size()) {\n cout << \"[\";\n vs[i].print();\n cout << \"]\";\n cout << \"(\" << con[i].first << \",\" << con[i].second <<\")\";\n }\n }\n }\n bool exist(int a) {\n if (vs.size() == 0) {\n return (v == a);\n } else {\n REP(i,vs.size()) {\n if (vs[i].exist(a)) return 1;\n }\n }\n return 0;\n }\n bool connect(vector<pii> &res, int a) {\n // print();cout << endl;\n // cout << a << endl;\n // cout << \":\" << exist(a) << endl;\n bool f = 0;\n if (vs.size() == 0) {\n f = v == a;\n } else {\n REP(i,vs.size()) {\n int ii = (i+1)%vs.size();\n if (vs[ii].exist(a)) {\n vs[ii].connect(res,a);\n } else {\n res.push_back(con[i]);\n vs[ii].connect(res,con[i].second);\n }\n }\n }\n return f;\n }\n};\n\ntypedef vector<vector<int> > matrix;\nconst int inf = 1<<29;\n \nvoid backward_traverse(int v, int s, int r, matrix &g,\n vector<int> &no, vector< vector<int> > &comp,\n vector<int> &prev, vector<int> &mcost,\n vector<Node> &nodes, vector<pii> &edge,\n vector<int> &mark, int &cost, bool &found) {\n const int n = g.size();\n if (mark[v]) {\n vector<int> temp = no;\n found = true;\n Node node;\n do {\n cost += mcost[v];\n node.con.push_back(edge[v]);\n v = prev[v];\n node.vs.push_back(nodes[v]);\n \n if (v != s) {\n while (comp[v].size() > 0) {\n no[comp[v].back()] = s;\n comp[s].push_back(comp[v].back());\n comp[v].pop_back();\n }\n }\n } while (v != s);\n reverse(ALL(node.vs));\n reverse(ALL(node.con));\n nodes[s] = node;\n for (int j = 0; j < n; ++j)\n if (j != r && no[j] == s)\n for (int i = 0; i < n; ++i)\n if (no[i] != s && g[i][j] < inf) {\n g[i][j] -= mcost[ temp[j] ];\n }\n }\n mark[v] = true;\n for (int i = 0; i < n; ++i)\n if (no[i] != no[v] && prev[ no[i] ] == v)\n if (!mark[ no[i] ] || i == s)\n backward_traverse(i, s, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n}\n\nint minimum_spanning_arborescence(int r, matrix g, vector<pii> &edges) {\n const int n = g.size();\n vector<int> no(n);\n vector< vector<int> > comp(n);\n vector<Node> nodes(n);\n for (int i = 0; i < n; ++i) {\n no[i] = i;\n comp[i].push_back(i);\n nodes[i].v = i;\n }\n int cost = 0;\n vector<int> pprev;\n while (1) {\n vector<int> prev(n, -1);\n vector<int> mcost(n, inf);\n vector<pii> edge(n);\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (j == r) continue;\n if (no[i] != no[j] && g[i][j] < inf) {\n if (g[i][j] < mcost[ no[j] ]) {\n mcost[ no[j] ] = g[i][j];\n prev[ no[j] ] = no[i];\n edge[no[j]] = pii(i,j);\n }\n }\n }\n }\n pprev = prev;\n bool stop = true;\n vector<int> mark(n);\n for (int i = 0; i < n; ++i) {\n if (i == r || mark[i] || comp[i].size() == 0) continue;\n bool found = false;\n backward_traverse(i, i, r, g,\n no, comp, prev, mcost, nodes, edge, mark, cost, found);\n if (found) stop = false;\n }\n // cout << \"cost \" << cost << endl;\n // REP(i,n) {\n // REP(j,n) {\n // cout << g[i][j] << \" \";\n // }\n // cout << endl;\n // }\n // cout << \"no: \";\n // FOR(it, no) cout << *it << \" \" ; cout << endl;\n // cout << \"mcost: \";\n // FOR(it, mcost) cout << *it << \" \" ; cout << endl;\n \n // cout << \"prev: \";\n // REP(i,n) cout << prev[i] << \" \"; cout << endl;\n // cout << \"prevprev: \";\n // FOR(it, prevprev) cout << *it << \" \"; cout << endl;\n // cout << endl;\n // nodes[1].print(); cout << endl;\n if (stop) {\n int cnt = 0;\n REP(i,n) {\n if (prev[i] >= 0) {\n nodes[i].connect(edges, edge[i].second);\n // nodes[i].print();\n // cout << \" \" << edge[i].first << \" \" << edge[i].second <<endl;\n edges.push_back(pii(edge[i].first, edge[i].second));\n cost += mcost[i];\n }\n if (prev[no[i]] < 0) cnt++;\n }\n // FOR(it, edges) {\n // cout << it->first << \" \" << it->second << endl;\n // }\n if (cnt != 1) return -1;\n return cost;\n }\n }\n}\n\nstruct Edge {\n int src, dst;\n int weight;\n Edge(int src, int dst, int weight) :\n src(src),dst(dst),weight(weight) {}\n bool operator<(const Edge &rhs) const {\n return weight > rhs.weight;\n }\n};\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\nvector<int> dijkstra(const Graph &g, int s, vector<int> &prev) {\n int n = g.size();\n vector<int> dist(n,inf);\n dist[s] = 0;\n prev.assign(n, -1);\n priority_queue<Edge> Q; // \"e < f\" <=> \"e.weight > f.weight\"\n for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (prev[e.dst] != -1) continue;\n prev[e.dst] = e.src;\n FOR(f,g[e.dst]) {\n if (dist[f->dst] > e.weight+f->weight) {\n dist[f->dst] = e.weight+f->weight;\n Q.push(Edge(f->src, f->dst, e.weight+f->weight));\n }\n }\n }\n return dist;\n}\n\nvector<int> buildPath(const vector<int> &prev, int t) {\n vector<int> path;\n for (int u = t; u >= 0; u = prev[u])\n path.push_back(u);\n reverse(ALL(path));\n return path;\n}\n\nstring name[100];\nint m[100];\nint sum[100];\npii p[100000];\n\nvector<int> dprev[100];\n\nstring tostr(pii a) {\n stringstream ss;\n ss << name[a.first] << \" \" << a.second;\n return ss.str();\n}\n\nint main() {\n int n;\n while(cin >> n) {\n sum[0] = 0;\n int N = 0;\n map<string, int > mp;\n for (int i=0; i<n; ++i) {\n cin >>name[i] >> m[i];\n sum[i+1] = sum[i] + m[i];\n mp[name[i]] = i;\n N += m[i];\n }\n int cnt = 0;\n for (int i=0; i<n; ++i) {\n for (int j=0; j<m[i]; ++j) {\n p[cnt++] = pii(i,j);\n }\n }\n int E;\n cin >> E;\n Graph g(N);\n for(int i=0; i<E; ++i) {\n string cs1,cs2;\n int v1,v2,c;\n cin >> cs1>>v1>>cs2>>v2>>c;\n int c1 = mp[cs1];\n int c2 = mp[cs2];\n int a = sum[c1] + v1;\n int b = sum[c2] + v2;\n g[a].push_back(Edge(a,b,c));\n }\n matrix mat(n,vector<int>(n));\n vector<int> prev;\n for (int i=0; i<n ;++i) {\n vector<int> dist = dijkstra(g, sum[i], dprev[i]);\n // FOR(it, dprev[i]) cout << \"*\" << *it; cout << endl;\n for (int j=0; j<n; ++j) {\n mat[i][j] = dist[sum[j]];\n }\n }\n // for (int i=0; i<n; ++i) {\n // for (int j=0;j<n; ++j) {\n // cout << mat[i][j] << \" \";\n // }\n // cout << endl;\n // }\n int Q;\n cin >> Q;\n for (int i=0; i<Q; ++i) {\n string cs3;\n int v3;\n cin >> cs3 >> v3;\n int c3 = mp[cs3];\n int a = sum[c3] + v3;\n vector<int> dist = dijkstra(g, a, prev);\n int res = dist[sum[c3]];\n vector<pii> edges;\n int tmpres = minimum_spanning_arborescence(c3, mat, edges);\n int ans = res + tmpres;\n\n // cout << \"prevprev : \";\n // FOR(it, prevprev) \n // cout << *it << \" \";\n // cout << endl;\n\n if (res == inf || tmpres < 0) {\n cout << \"Impossible\" << endl;\n } else {\n cout << ans << endl;\n vector<int> path = buildPath(prev, sum[c3]);\n REP(i,path.size()-1) {\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n FOR(it, edges) {\n int a = it->second;\n int b = it->first;\n int A = sum[a];\n\n vector<int> path = buildPath(dprev[b], A);\n REP(i,path.size()-1) {\n cout << tostr(p[path[i]]) << \" \" << tostr(p[path[i+1]]) << endl;\n }\n }\n \n }\n puts(\"-----\");\n }\n }\n}", "accuracy": 0.4222222222222222, "time_ms": 160, "memory_kb": 4788, "score_of_the_acc": -0.4025, "final_rank": 8 }, { "submission_id": "aoj_1537_890523", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <set>\n#include <map>\n#include <queue>\n#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <cctype>\n#include <cassert>\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\n#if defined(_MSC_VER) || __cplusplus > 199711L\n#define aut(r,v) auto r = (v)\n#else\n#define aut(r,v) typeof(v) r = (v)\n#endif\n#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)\n#define all(o) (o).begin(), (o).end()\n#define pb(x) push_back(x)\n#define mp(x,y) make_pair((x),(y))\n#define mset(m,v) memset(m,v,sizeof(m))\n#define INF 0x3f3f3f3f\n#define INFL 0x3f3f3f3f3f3f3f3fLL\nusing namespace std;\ntypedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;\ntypedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;\ntypedef vector<string> vs; typedef long double ld;\ntemplate<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }\ntemplate<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }\n\nstruct GetStrID {\n\tstatic const int MaxLen = 21;\t//書き換えること!\n\tstatic const int Alphas = 26;\t//書き換えること!\n\ttypedef char Alpha;\n\tstatic const int EndAlpha = -1;\n\nprivate:\n\ttemplate<typename T>\n\tstruct Trie {\n\t\tbool terminate;\n\t\tT val;\n\t\tTrie<T> *next[Alphas];\n\n\t\tTrie(): terminate(false) { memset(next, 0, sizeof(next)); }\n\t\t~Trie() { for(Alpha a = 0; a < Alphas; ++ a) delete next[a]; }\n\n\t\tvoid insert(Alpha *s, const T &v) {\n\t\t\tif(*s == EndAlpha) {\n\t\t\t\tterminate = true;\n\t\t\t\tval = v;\n\t\t\t\treturn;\n\t\t\t}else {\n\t\t\t\tif(!next[*s])\n\t\t\t\t\tnext[*s] = new Trie;\n\t\t\t\treturn next[*s]->insert(s+1, v);\n\t\t\t}\n\t\t}\n\t\tbool find(Alpha *s, T &out_v) const {\n\t\t\tif(*s == EndAlpha) {\n\t\t\t\tif(terminate) {\n\t\t\t\t\tout_v = val;\n\t\t\t\t\treturn true;\n\t\t\t\t}else return false;\n\t\t\t}\n\t\t\treturn next[*s] && next[*s]->find(s+1, out_v);\n\t\t}\n\t};\n\npublic:\n\tGetStrID(const char *alphas): cnt(0) {\n\t\tassert(strlen(alphas) <= Alphas);\n\t\tmemset(idx, -1, sizeof(idx));\n\t\tfor(const char *p = alphas; *p; ++ p)\n\t\t\tidx[*p] = p - alphas;\n\t}\n\tchar idx[128];\n\tTrie<int> trie;\n\tint cnt;\n\n\tvoid clear() { trie = Trie<int>(); cnt = 0; }\n\tint get(const char *x) {\n\t\tstatic char tmp[MaxLen+1];\n\t\t{\tint i;\n\t\t\tfor(i = 0; x[i]; i ++) tmp[i] = idx[x[i]];\n\t\t\ttmp[i] = EndAlpha;\n\t\t}\n\t\tint ret = -1;\n\t\tif(!trie.find(tmp, ret)) {\n\t\t\tret = cnt ++;\n\t\t\ttrie.insert(tmp, ret);\n\t\t}\n\t\treturn ret;\n\t}\n};\n\npair<int,int> detectCycle2(const vector<int> &f) {\n\tint n = f.size();\n\tvector<bool> ved(n);\n\tint power, lambda, tortoise, hare;\n\touter:\n\tfor(int s = 0;s < n;s++){\n\t\tif(!ved[s]){\n\t\t\tpower = 1, lambda = 1;\n\t\t\tif(f[s] < 0)continue;\n\t\t\ttortoise = s;\n\t\t\tved[s] = true;\n\t\t\thare = f[s];\n\t\t\twhile(tortoise != hare) {\n\t\t\t\tif(hare < 0)goto outer;\n\t\t\t\tved[hare] = true;\n\t\t\t\tif(power == lambda) {\n\t\t\t\t\ttortoise = hare;\n\t\t\t\t\tpower <<= 1;\n\t\t\t\t\tlambda = 0;\n\t\t\t\t}\n\t\t\t\tif(hare < 0)goto outer;\n\t\t\t\thare = f[hare];\n\t\t\t\tlambda++;\n\t\t\t}\n\t\t\tif(lambda > 0){\n\t\t\t\treturn mp(hare, lambda);\n\t\t\t}\n\t\t}\n\t}\n\treturn mp(-1,-1);\n}\n\ntypedef int Weight;\nvector<int> buildMSA(const vector<vector<Weight> > &w, int root)\n{\nint n = w.size();\n\t\t\n// critical graphをつくる\n// root以外の全頂点に対し、そこに入る最小コスト辺を選ぶ\nvi par(n, -1);\nfor(int i = 0;i < n;i++){\n\tif(i != root){\n\t\tWeight min = INF;\n\t\tint pi = -1;\n\t\tfor(int j = 0;j < n;j++){\n\t\t\tif(j != i){\n\t\t\t\tif(min > w[j][i]){\n\t\t\t\t\tmin = w[j][i];\n\t\t\t\t\tpi = j;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpar[i] = pi;\n\t}\n}\n\t\t\npii res = detectCycle2(par);\nif(res.first != -1){\n\t// cycleが存在したら縮約\n\tvi map(n, -1);\n\tint shed = n-res.second;\n\tmap[res.first] = shed;\n\t\t\t\n\t// cycleの要素を列挙\n\tWeight minc = w[par[res.first]][res.first];\n\tfor(int k = par[res.first];k != res.first;k = par[k]){\n\t\tmap[k] = shed;\n\t\tminc = min(minc, w[par[k]][k]);\n\t}\n\t\t\t\n\t// 縮約後のグラフとの対応\n\tvector<int> imap(n-res.second);\n\tint p = 0;\n\tfor(int i = 0;i < n;i++){\n\t\tif(map[i] != shed){\n\t\t\tmap[i] = p;\n\t\t\timap[p] = i;\n\t\t\tp++;\n\t\t}\n\t}\n\t\t\t\n\t// 縮約後のグラフ\n\tvector<vector<Weight> > sw(shed+1, vector<Weight>(shed+1, INF));\n\t\t\t\n\tvi to(n); // cycle内の要素xで、辺i->xの最小weightを実現するもの\n\tvi from(n); // cycle内の要素xで、辺x->iの最小weightを実現するもの\n\tfor(int i = 0;i < n;i++){\n\t\tif(map[i] != shed){\n\t\t\tfor(int j = 0;j < n;j++){\n\t\t\t\tif(map[j] == shed){\n\t\t\t\t\t// 非cycle->cycle\n\t\t\t\t\t// weightを修正\n\t\t\t\t\tWeight V = w[i][j] - w[par[j]][j] + minc;\n\t\t\t\t\tif(sw[map[i]][shed] > V){\n\t\t\t\t\t\tsw[map[i]][shed] = V;\n\t\t\t\t\t\tto[i] = j;\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\t// 非cycle->非cycle\n\t\t\t\t\tsw[map[i]][map[j]] = w[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\tfor(int j = 0;j < n;j++){\n\t\t\t\tif(map[j] != shed){\n\t\t\t\t\t// cycle->非cycle\n\t\t\t\t\tWeight V = w[i][j];\n\t\t\t\t\tif(sw[shed][map[j]] > V){\n\t\t\t\t\t\tsw[shed][map[j]] = V;\n\t\t\t\t\t\tfrom[j] = i;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\t\t\n\tvi smsa = buildMSA(sw, map[root]); // 再帰してMSAを求める。\n\t// cycle以外の頂点について、imap,fromに従って対応を戻す。\n\tfor(int i = 0;i < shed;i++){\n\t\tif(smsa[i] == shed){\n\t\t\tpar[imap[i]] = from[imap[i]]; // cycle->非cycle\n\t\t}else if(smsa[i] != -1){\n\t\t\tpar[imap[i]] = imap[smsa[i]]; // 非cycle->非cycle\n\t\t}\n\t}\n\t\t\t\n\t// cycle内の頂点について、toに従って対応を戻す。\n\tint u = imap[smsa[shed]];\n\tpar[to[u]] = u;\n}\nreturn par;\n}\n\nint n;\nchar name1[21], name2[21];\nchar names[26][21];\nint ms[26];\nint g[26][100][100];\nint ginter[26][100][100];\nint h[26][26];\nint hg[26][26];\nbool vis[26];\nvector<pair<pii,pii> > anss;\n\nint dfs(int c) {\n\tif(vis[c]) return 0;\n\tvis[c] = true;\n\tint r = 1;\n\trep(d, n) if(h[c][d] < INF)\n\t\tr += dfs(d);\n\treturn r;\n}\n\nvoid buildPath_g(int c, int s, int t) {\n\tint u = ginter[c][s][t];\n\tif(u == -1) {\n\t\tif(s != t) anss.push_back(mp(mp(c, c), mp(s, t)));\n\t}else buildPath_g(c, s, u), buildPath_g(c, u, t);\n}\n\nint main() {\n\tGetStrID country(\"abcdefghijklmnopqrstuvwxyz\");\n\tscanf(\"%d\", &n);\n\trep(i, n) {\n\t\tscanf(\"%s%d\", names[i], &ms[i]);\n\t\tcountry.get(names[i]);\n\t}\n\tint e;\n\tscanf(\"%d\", &e);\n\tmset(g, INF);\n\trep(c, n) rep(i, ms[c]) g[c][i][i] = 0;\n\tvector<pair<pii,pii> > edges;\n\trep(i, e) {\n\t\tint v1, v2, cost;\n\t\tscanf(\"%s%d%s%d%d\", name1, &v1, name2, &v2, &cost);\n\t\tint c1 = country.get(name1), c2 = country.get(name2);\n\t\tif(c1 == c2) {\n\t\t\tamin(g[c1][v1][v2], cost);\n\t\t}else {\n\t\t\tedges.pb(mp(mp(c1,c2),mp(v2,cost)));\n\t\t}\n\t}\n\tmset(ginter, -1);\n\trep(c, n) {\n\t\tint m = ms[c];\n\t\trep(k, m) rep(i, m) rep(j, m) {\n\t\t\tif(g[c][i][j] > g[c][i][k] + g[c][k][j]) {\n\t\t\t\tg[c][i][j] = g[c][i][k] + g[c][k][j];\n\t\t\t\tginter[c][i][j] = k;\n\t\t\t}\n\t\t}\n\t}\n\tmset(h, INF);\n\trep(c, n) h[c][c] = 0;\n\teach(i, edges) {\n\t\tint c1 = i->first.first, c2 = i->first.second;\n\t\tint v2 = i->second.first, cost = i->second.second;\n\t\tif(h[c1][c2] > cost + g[c2][v2][0]) {\n\t\t\th[c1][c2] = cost + g[c2][v2][0];\n\t\t\thg[c1][c2] = v2;\n\t\t}\n\t}\n\tvector<vi> hv(n, vi(n));\n\trep(i, n) rep(j, n) hv[i][j] = h[i][j];\n\tint q;\n\tscanf(\"%d\", &q);\n\trep(ii, q) {\n\t\tint v;\n\t\tscanf(\"%s%d\", name1, &v);\n\t\tint c = country.get(name1);\n\t\tbool ok = true;\n\t\tok &= g[c][v][0] < INF;\n\t\tmset(vis, 0);\n\t\tok &= dfs(c) == n;\n\t\tif(!ok) {\n\t\t\tputs(\"Impossible\");\n\t\t}else {\n\t\t\tanss.clear();\n\t\t\tint cost = g[c][v][0];\n\t\t\tbuildPath_g(c, v, 0);\n\t\t\tvi tree = buildMSA(hv, c);\n\t\t\trep(i, n) if(i != c) {\n\t\t\t\tcost += hv[tree[i]][i];\n\t\t\t\tint v2 = hg[tree[i]][i];\n\t\t\t\tanss.push_back(mp(mp(tree[i], i), mp(0, v2)));\n\t\t\t\tbuildPath_g(i, v2, 0);\n\t\t\t}\n\t\t\tprintf(\"%d\\n\", cost);\n//\t\t\tsort(all(anss));\n//\t\t\tanss.erase(unique(all(anss)), anss.end());\n\t\t\teach(i, anss) {\n\t\t\t\tpii p = i->first, q = i->second;\n\t\t\t\tprintf(\"%s %d %s %d\\n\",\n\t\t\t\t\tnames[p.first], q.first,\n\t\t\t\t\tnames[p.second], q.second);\n\t\t\t}\n\t\t}\n\t\tputs(\"-----\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 4396, "score_of_the_acc": -0.16, "final_rank": 1 }, { "submission_id": "aoj_1537_890479", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <set>\n#include <map>\n#include <queue>\n#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <cctype>\n#include <cassert>\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\n#if defined(_MSC_VER) || __cplusplus > 199711L\n#define aut(r,v) auto r = (v)\n#else\n#define aut(r,v) typeof(v) r = (v)\n#endif\n#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)\n#define all(o) (o).begin(), (o).end()\n#define pb(x) push_back(x)\n#define mp(x,y) make_pair((x),(y))\n#define mset(m,v) memset(m,v,sizeof(m))\n#define INF 0x3f3f3f3f\n#define INFL 0x3f3f3f3f3f3f3f3fLL\nusing namespace std;\ntypedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;\ntypedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;\ntypedef vector<string> vs; typedef long double ld;\ntemplate<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }\ntemplate<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }\n\nstruct GetStrID {\n\tstatic const int MaxLen = 21;\t//書き換えること!\n\tstatic const int Alphas = 26;\t//書き換えること!\n\ttypedef char Alpha;\n\tstatic const int EndAlpha = -1;\n\nprivate:\n\ttemplate<typename T>\n\tstruct Trie {\n\t\tbool terminate;\n\t\tT val;\n\t\tTrie<T> *next[Alphas];\n\n\t\tTrie(): terminate(false) { memset(next, 0, sizeof(next)); }\n\t\t~Trie() { for(Alpha a = 0; a < Alphas; ++ a) delete next[a]; }\n\n\t\tvoid insert(Alpha *s, const T &v) {\n\t\t\tif(*s == EndAlpha) {\n\t\t\t\tterminate = true;\n\t\t\t\tval = v;\n\t\t\t\treturn;\n\t\t\t}else {\n\t\t\t\tif(!next[*s])\n\t\t\t\t\tnext[*s] = new Trie;\n\t\t\t\treturn next[*s]->insert(s+1, v);\n\t\t\t}\n\t\t}\n\t\tbool find(Alpha *s, T &out_v) const {\n\t\t\tif(*s == EndAlpha) {\n\t\t\t\tif(terminate) {\n\t\t\t\t\tout_v = val;\n\t\t\t\t\treturn true;\n\t\t\t\t}else return false;\n\t\t\t}\n\t\t\treturn next[*s] && next[*s]->find(s+1, out_v);\n\t\t}\n\t};\n\npublic:\n\tGetStrID(const char *alphas): cnt(0) {\n\t\tassert(strlen(alphas) <= Alphas);\n\t\tmemset(idx, -1, sizeof(idx));\n\t\tfor(const char *p = alphas; *p; ++ p)\n\t\t\tidx[*p] = p - alphas;\n\t}\n\tchar idx[128];\n\tTrie<int> trie;\n\tint cnt;\n\n\tvoid clear() { trie = Trie<int>(); cnt = 0; }\n\tint get(const char *x) {\n\t\tstatic char tmp[MaxLen+1];\n\t\t{\tint i;\n\t\t\tfor(i = 0; x[i]; i ++) tmp[i] = idx[x[i]];\n\t\t\ttmp[i] = EndAlpha;\n\t\t}\n\t\tint ret = -1;\n\t\tif(!trie.find(tmp, ret)) {\n\t\t\tret = cnt ++;\n\t\t\ttrie.insert(tmp, ret);\n\t\t}\n\t\treturn ret;\n\t}\n};\n\npair<int,int> detectCycle2(const vector<int> &f) {\n\tint n = f.size();\n\tvector<bool> ved(n);\n\tint power, lambda, tortoise, hare;\n\touter:\n\tfor(int s = 0;s < n;s++){\n\t\tif(!ved[s]){\n\t\t\tpower = 1, lambda = 1;\n\t\t\tif(f[s] < 0)continue;\n\t\t\ttortoise = s;\n\t\t\tved[s] = true;\n\t\t\thare = f[s];\n\t\t\twhile(tortoise != hare) {\n\t\t\t\tif(hare < 0)goto outer;\n\t\t\t\tved[hare] = true;\n\t\t\t\tif(power == lambda) {\n\t\t\t\t\ttortoise = hare;\n\t\t\t\t\tpower <<= 1;\n\t\t\t\t\tlambda = 0;\n\t\t\t\t}\n\t\t\t\tif(hare < 0)goto outer;\n\t\t\t\thare = f[hare];\n\t\t\t\tlambda++;\n\t\t\t}\n\t\t\tif(lambda > 0){\n\t\t\t\treturn mp(hare, lambda);\n\t\t\t}\n\t\t}\n\t}\n\treturn mp(-1,-1);\n}\n\ntypedef int Weight;\nvector<int> buildMSA(const vector<vector<Weight> > &w, int root)\n{\nint n = w.size();\n\t\t\n// critical graphをつくる\n// root以外の全頂点に対し、そこに入る最小コスト辺を選ぶ\nvi par(n, -1);\nfor(int i = 0;i < n;i++){\n\tif(i != root){\n\t\tWeight min = INF;\n\t\tint pi = -1;\n\t\tfor(int j = 0;j < n;j++){\n\t\t\tif(j != i){\n\t\t\t\tif(min > w[j][i]){\n\t\t\t\t\tmin = w[j][i];\n\t\t\t\t\tpi = j;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpar[i] = pi;\n\t}\n}\n\t\t\npii res = detectCycle2(par);\nif(res.first != -1){\n\t// cycleが存在したら縮約\n\tvi map(n, -1);\n\tint shed = n-res.second;\n\tmap[res.first] = shed;\n\t\t\t\n\t// cycleの要素を列挙\n\tWeight minc = w[par[res.first]][res.first];\n\tfor(int k = par[res.first];k != res.first;k = par[k]){\n\t\tmap[k] = shed;\n\t\tminc = min(minc, w[par[k]][k]);\n\t}\n\t\t\t\n\t// 縮約後のグラフとの対応\n\tvector<int> imap(n-res.second);\n\tint p = 0;\n\tfor(int i = 0;i < n;i++){\n\t\tif(map[i] != shed){\n\t\t\tmap[i] = p;\n\t\t\timap[p] = i;\n\t\t\tp++;\n\t\t}\n\t}\n\t\t\t\n\t// 縮約後のグラフ\n\tvector<vector<Weight> > sw(shed+1, vector<Weight>(shed+1, INF));\n\t\t\t\n\tvi to(n); // cycle内の要素xで、辺i->xの最小weightを実現するもの\n\tvi from(n); // cycle内の要素xで、辺x->iの最小weightを実現するもの\n\tfor(int i = 0;i < n;i++){\n\t\tif(map[i] != shed){\n\t\t\tfor(int j = 0;j < n;j++){\n\t\t\t\tif(map[j] == shed){\n\t\t\t\t\t// 非cycle->cycle\n\t\t\t\t\t// weightを修正\n\t\t\t\t\tWeight V = w[i][j] - w[par[j]][j] + minc;\n\t\t\t\t\tif(sw[map[i]][shed] > V){\n\t\t\t\t\t\tsw[map[i]][shed] = V;\n\t\t\t\t\t\tto[i] = j;\n\t\t\t\t\t}\n\t\t\t\t}else{\n\t\t\t\t\t// 非cycle->非cycle\n\t\t\t\t\tsw[map[i]][map[j]] = w[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\tfor(int j = 0;j < n;j++){\n\t\t\t\tif(map[j] != shed){\n\t\t\t\t\t// cycle->非cycle\n\t\t\t\t\tWeight V = w[i][j];\n\t\t\t\t\tif(sw[shed][map[j]] > V){\n\t\t\t\t\t\tsw[shed][map[j]] = V;\n\t\t\t\t\t\tfrom[j] = i;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\t\t\n\tvi smsa = buildMSA(sw, map[root]); // 再帰してMSAを求める。\n\t// cycle以外の頂点について、imap,fromに従って対応を戻す。\n\tfor(int i = 0;i < shed;i++){\n\t\tif(smsa[i] == shed){\n\t\t\tpar[imap[i]] = from[imap[i]]; // cycle->非cycle\n\t\t}else if(smsa[i] != -1){\n\t\t\tpar[imap[i]] = imap[smsa[i]]; // 非cycle->非cycle\n\t\t}\n\t}\n\t\t\t\n\t// cycle内の頂点について、toに従って対応を戻す。\n\tint u = imap[smsa[shed]];\n\tpar[to[u]] = u;\n}\nreturn par;\n}\n\nint n;\nchar name1[21], name2[21];\nchar names[26][21];\nint ms[26];\nint g[26][100][100];\nint ginter[26][100][100];\nint h[26][26];\nint hinter[26][26];\nint hg[26][26];\nbool vis[26];\nvector<pair<pii,pii> > anss;\n\nint dfs(int c) {\n\tif(vis[c]) return 0;\n\tvis[c] = true;\n\tint r = 1;\n\trep(d, n) if(h[c][d] < INF)\n\t\tr += dfs(d);\n\treturn r;\n}\n\nvoid buildPath_g(int c, int s, int t) {\n\tint u = ginter[c][s][t];\n\tif(u == -1) {\n\t\tif(s != t) anss.push_back(mp(mp(c, c), mp(s, t)));\n\t}else buildPath_g(c, s, u), buildPath_g(c, u, t);\n}\n\nvoid buildPath_h(int s, int t) {\n\tint u = hinter[s][t];\n\tif(u == -1) {\n\t\tif(s != t) {\n\t\t\tint v = hg[s][t];\n\t\t\tanss.push_back(mp(mp(s, t), mp(0, v)));\n\t\t\tbuildPath_g(t, v, 0);\n\t\t}\n\t}else buildPath_h(s, u), buildPath_h(u, t);\n}\n\nint main() {\n\tGetStrID country(\"abcdefghijklmnopqrstuvwxyz\");\n\tscanf(\"%d\", &n);\n\trep(i, n) {\n\t\tscanf(\"%s%d\", names[i], &ms[i]);\n\t\tcountry.get(names[i]);\n\t}\n\tint e;\n\tscanf(\"%d\", &e);\n\tmset(g, INF);\n\trep(c, n) rep(i, ms[c]) g[c][i][i] = 0;\n\tvector<pair<pii,pii> > edges;\n\trep(i, e) {\n\t\tint v1, v2, cost;\n\t\tscanf(\"%s%d%s%d%d\", name1, &v1, name2, &v2, &cost);\n\t\tint c1 = country.get(name1), c2 = country.get(name2);\n\t\tif(c1 == c2) {\n\t\t\tamin(g[c1][v1][v2], cost);\n\t\t}else {\n\t\t\tedges.pb(mp(mp(c1,c2),mp(v2,cost)));\n\t\t}\n\t}\n\tmset(ginter, -1);\n\tmset(hinter, -1);\n\trep(c, n) {\n\t\tint m = ms[c];\n\t\trep(k, m) rep(i, m) rep(j, m) {\n\t\t\tif(g[c][i][j] > g[c][i][k] + g[c][k][j]) {\n\t\t\t\tg[c][i][j] = g[c][i][k] + g[c][k][j];\n\t\t\t\tginter[c][i][j] = k;\n\t\t\t}\n\t\t}\n\t}\n\tmset(h, INF);\n\trep(c, n) h[c][c] = 0;\n\teach(i, edges) {\n\t\tint c1 = i->first.first, c2 = i->first.second;\n\t\tint v2 = i->second.first, cost = i->second.second;\n\t\tif(h[c1][c2] > cost + g[c2][v2][0]) {\n\t\t\th[c1][c2] = cost + g[c2][v2][0];\n\t\t\thg[c1][c2] = v2;\n\t\t}\n\t}\n\trep(k, n) rep(i, n) rep(j, n) {\n\t\tif(h[i][j] > h[i][k] + h[k][j]) {\n\t\t\th[i][j] = h[i][k] + h[k][j];\n\t\t\thinter[i][j] = k;\n\t\t}\n\t}\n\tvector<vi> hv(n, vi(n));\n\trep(i, n) rep(j, n) hv[i][j] = h[i][j];\n\tint q;\n\tscanf(\"%d\", &q);\n\trep(ii, q) {\n\t\tint v;\n\t\tscanf(\"%s%d\", name1, &v);\n\t\tint c = country.get(name1);\n\t\tbool ok = true;\n\t\tok &= g[c][v][0] < INF;\n\t\tmset(vis, 0);\n\t\tok &= dfs(c) == n;\n\t\tif(!ok) {\n\t\t\tputs(\"Impossible\");\n\t\t}else {\n\t\t\tanss.clear();\n\t\t\tint cost = g[c][v][0];\n\t\t\tbuildPath_g(c, v, 0);\n\t\t\tvi tree = buildMSA(hv, c);\n\t\t\trep(i, n) if(i != c) {\n\t\t\t\tcost += hv[tree[i]][i];\n\t\t\t\tbuildPath_h(tree[i], i);\n\t\t\t}\n\t\t\tprintf(\"%d\\n\", cost);\n\t\t\teach(i, anss) {\n\t\t\t\tpii p = i->first, q = i->second;\n\t\t\t\tprintf(\"%s %d %s %d\\n\",\n\t\t\t\t\tnames[p.first], q.first,\n\t\t\t\t\tnames[p.second], q.second);\n\t\t\t}\n\n\t\t}\n\t\tputs(\"-----\");\n\t}\n\treturn 0;\n}", "accuracy": 0.4444444444444444, "time_ms": 90, "memory_kb": 4396, "score_of_the_acc": 0, "final_rank": 3 } ]
aoj_1535_cpp
Problem J: Hanimon ハニカムモンスターはハニカム模様の六角形状の不思議な生き物である。 ハニカムモンスターには様々なサイズのものがあり、一辺が N 個の正六角形のマスから成るハニカムモンスターをサイズ N のハニカムモンスターとする。 一方、聖なる模様はハニカムモンスターと同じく、六角形として定義され1辺が P 個の正六角形のマスから成る聖なる模様をサイズ P の聖なる模様とする。 ハニカムモンスターと聖なる模様の形の例を以下に示す。サイズは1辺の正六角形のマスの数に対応する。 図1: サイズの例 図2: ハニカムモンスターと聖なる模様の例 (Sample Input 1) ハニカムモンスターと聖なる模様の各マスには色が着いており0と1によって表される。 サイズ P の Q 個の聖なる模様のすべてを含むハニカムモンスターは伝説のハニモンと呼ばれている。 サイズ N のハニカムモンスターとその模様、 Q 個のサイズ P の聖なる模様が与えられるので、 そのハニカムモンスターが伝説のハニモンであるかどうかを判定するプログラムを作成せよ。 Input 入力は次の形式で表される。 N P Q (空行) ハニカムモンスターの模様の情報 (空行) 1つ目の聖なる模様の情報 (空行) 2つ目の聖なる模様の情報 (空行) ... Q つ目の聖なる模様の情報 N , P , Q はそれぞれハニカムモンスターのサイズ,聖なる模様のサイズ,聖なる模様の個数を表す。 サイズ N のハニカムモンスターの模様の情報は以下の様に2× N -1行で与えられる。 N 個のマス N +1個のマス N +2個のマス . . 2× N -2個のマス 2× N -1個のマス 2× N -2個のマス . . N +2個のマス N +1個のマス N 個のマス サイズ P の聖なる模様の情報は以下の様に2× P -1行で与えられる。 P 個のマス P +1個のマス P +2個のマス . . 2× P -2個のマス 2× P -1個のマス 2× P -2個のマス . . P +2個のマス P +1個のマス P 個のマス それぞれのマスの間には1つの空白が入り、マスには0か1が入る。 Constraints 1 ≤ N ≤ 1000 1 ≤ P ≤ 50 1 ≤ Q ≤ 100 同じ模様の聖なる模様が複数個与えられることは無い。 ハニカムモンスターのあるマスについて、2つ以上の聖なる模様の一部分であることがある。 ハニカムモンスターと聖なる模様は回転できない。 Output 伝説のハニモンなら YES を、そうでなければ NO を一行に出力せよ。 Sample Input 1 6 2 2 1 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 0 1 0 1 1 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 0 1 0 1 1 1 1 1 0 0 0 0 1 0 1 1 Sample Output 1 YES Sample Input 2 6 2 2 0 0 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0 0 1 1 0 1 0 0 1 0 1 Sample Output 2 NO
[ { "submission_id": "aoj_1535_890620", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef unsigned long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint N, p, q;\nll A[2000][2000], B[2000][2000];\nint la[2000], ra[2000];\nint lb[2000], rb[2000];\n\nvoid input(int n, ll a[2000][2000], int *l, int *r){\n\t\n\trep(i, 2000) rep(j, 2000) a[i][j] = 0;\n\t\n\trep(i, 2 * n - 1){\n\t\tint len = i < n ? n + i : 3 * n - i - 2;\n\t\tint ofs = i < n ? 0 : i - n + 1;\n\t\t\n\t\tl[i] = ofs;\n\t\tr[i] = ofs + len;\n\t\t\n\t\trep(j, len){\n\t\t\tint t; scanf(\"%d\", &t);\n\t\t\t\n\t\t\tif(t) rep(k, 60){\n\t\t\t\tif(ofs + j - k < 0) break;\n\t\t\t\ta[i][ofs + j - k] |= 1ull << k;\n\t\t\t}\n\t\t}\n\t}\n\t//rep(i, 2 * n - 1) rep(j, 2 * n - 1) cerr<<(a[i][j]&1)<<(j==2*n-2?\"\\n\":\" \");\n}\n\nint main(){\n\t\n\tscanf(\"%d%d%d\", &N, &p, &q);\n\t\n\tinput(N, A, la, ra);\n\twhile(q--){\n\t\tinput(p, B, lb, rb);\n\t\t\n\t\tbool found = 0;\n\t\t\n\t\trep(i, 2 * N - 1) for(int j = la[i]; j < ra[i]; j++){\n\t\t\t\n\t\t\tbool ok = 1;\n\t\t\t\n\t\t\trep(k, 2 * p - 1){\n\t\t\t\t\n\t\t\t\tint len = rb[k] - lb[k];\n\t\t\t\tint pos = 0;\n\t\t\t\t\n\t\t\t\tif(ra[i + k] - j < len){\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\trep(it, 2){\n\t\t\t\t\t\n\t\t\t\t\tint clen = min(60, len);\n\t\t\t\t\t\n\t\t\t\t\tll x = A[i + k][j + lb[k] + pos] & (1ull << clen) - 1ull;\n\t\t\t\t\tll y = B[k][lb[k] + pos] & (1ull << clen) - 1ull;\n\t\t\t\t\t\n\t\t\t\t\t/*\n\t\t\t\t\tif(i == 2 && j == 1){\n\t\t\t\t\t\tdbg(it); dbg(x); dbg(y);\n\t\t\t\t\t}\n\t\t\t\t\t*/\n\t\t\t\t\tif(x != y){\n\t\t\t\t\t\tok = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tlen -= clen;\n\t\t\t\t\tpos += clen;\n\t\t\t\t\tif(len <= 0) break;\n\t\t\t\t}\n\t\t\t\tif(!ok) break;\n\t\t\t}\n\t\t\tif(ok){\n\t\t\t\t//cerr<<\"i: \"<<i<<\" j: \"<<j<<endl;\n\t\t\t\tfound = 1;\n\t\t\t\tgoto END;\n\t\t\t}\n\t\t}\n\t\tEND:\n\t\tif(!found){\n\t\t\tcout << \"NO\" << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout << \"YES\" << endl;\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 63728, "score_of_the_acc": -1.0001, "final_rank": 1 }, { "submission_id": "aoj_1535_890615", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef unsigned long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint N, p[2];\nll A[2000][2000], B[2000][2000];\nint la[2000], ra[2000];\nint lb[2000], rb[2000];\n\nvoid input(int n, ll a[2000][2000], int *l, int *r){\n\t\n\trep(i, 2000) rep(j, 2000) a[i][j] = 0;\n\t\n\trep(i, 2 * n - 1){\n\t\tint len = i < n ? n + i : 3 * n - i - 2;\n\t\tint ofs = i < n ? 0 : i - n + 1;\n\t\t\n\t\tl[i] = ofs;\n\t\tr[i] = ofs + len;\n\t\t\n\t\trep(j, len){\n\t\t\tint t; scanf(\"%d\", &t);\n\t\t\t\n\t\t\tif(t) rep(k, 64){\n\t\t\t\tif(ofs + j - k < 0) break;\n\t\t\t\ta[i][ofs + j - k] |= 1ull << k;\n\t\t\t}\n\t\t}\n\t}\n\t//rep(i, 2 * n - 1) rep(j, 2 * n - 1) cerr<<(a[i][j]&1)<<(j==2*n-2?\"\\n\":\" \");\n}\n\nint main(){\n\t\n\tscanf(\"%d%d%d\", &N, p, p + 1);\n\t\n\tinput(N, A, la, ra);\n\trep(it, 2){\n\t\tinput(p[it], B, lb, rb);\n\t\t\n\t\tassert(p[it]<=N);\n\t\t\n\t\tbool found = 0;\n\t\t\n\t\trep(i, 2 * N - 1) for(int j = la[i]; j < ra[i]; j++){\n\t\t\t\n\t\t\tbool ok = 1;\n\t\t\t\n\t\t\trep(k, 2 * p[it] - 1){\n\t\t\t\t\n\t\t\t\tint len = rb[k] - lb[k];\n\t\t\t\tint pos = 0;\n\t\t\t\t\n\t\t\t\tif(ra[i + k] - j < len){\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\trep(it, 2){\n\t\t\t\t\t\n\t\t\t\t\tint clen = min(60, len);\n\t\t\t\t\t\n\t\t\t\t\tll x = A[i + k][j + lb[k] + pos] & (1ull << clen) - 1ull;\n\t\t\t\t\tll y = B[k][lb[k] + pos] & (1ull << clen) - 1ull;\n\t\t\t\t\t\n\t\t\t\t\t/*\n\t\t\t\t\tif(i == 2 && j == 1){\n\t\t\t\t\t\tdbg(it); dbg(x); dbg(y);\n\t\t\t\t\t}\n\t\t\t\t\t*/\n\t\t\t\t\tif(x != y){\n\t\t\t\t\t\tok = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tlen -= clen;\n\t\t\t\t\tpos += clen;\n\t\t\t\t\tif(len <= 0) break;\n\t\t\t\t}\n\t\t\t\tif(!ok) break;\n\t\t\t}\n\t\t\tif(ok){\n\t\t\t\t//cerr<<\"i: \"<<i<<\" j: \"<<j<<endl;\n\t\t\t\tfound = 1;\n\t\t\t\tgoto END;\n\t\t\t}\n\t\t}\n\t\tEND:\n\t\tif(!found){\n\t\t\tcout << \"NO\" << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout << \"YES\" << endl;\n\t\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 10, "memory_kb": 63724, "score_of_the_acc": 0, "final_rank": 2 }, { "submission_id": "aoj_1535_890612", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef unsigned long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint N, p[2];\nll A[2000][2000], B[2000][2000];\nint la[2000], ra[2000];\nint lb[2000], rb[2000];\n\nvoid input(int n, ll a[2000][2000], int *l, int *r){\n\t\n\trep(i, 2000) rep(j, 2000) a[i][j] = 0;\n\t\n\trep(i, 2 * n - 1){\n\t\tint len = i < n ? n + i : 3 * n - i - 2;\n\t\tint ofs = i < n ? 0 : i - n + 1;\n\t\t\n\t\tl[i] = ofs;\n\t\tr[i] = ofs + len;\n\t\t\n\t\trep(j, len){\n\t\t\tint t; scanf(\"%d\", &t);\n\t\t\t\n\t\t\tif(t) rep(k, 64){\n\t\t\t\tif(ofs + j - k < 0) break;\n\t\t\t\ta[i][ofs + j - k] |= 1ull << k;\n\t\t\t}\n\t\t}\n\t}\n\t//rep(i, 2 * n - 1) rep(j, 2 * n - 1) cerr<<(a[i][j]&1)<<(j==2*n-2?\"\\n\":\" \");\n}\n\nint main(){\n\t\n\tscanf(\"%d%d%d\", &N, p, p + 1);\n\t\n\tinput(N, A, la, ra);\n\trep(it, 2){\n\t\tinput(p[it], B, lb, rb);\n\t\tassert(p[it] <= 64);\n\t\t\n\t\tbool found = 0;\n\t\t\n\t\trep(i, 2 * N - 1) for(int j = la[i]; j < ra[i]; j++){\n\t\t\t\n\t\t\tbool ok = 1;\n\t\t\t\n\t\t\trep(k, 2 * p[it] - 1){\n\t\t\t\t\n\t\t\t\tint len = rb[k] - lb[k];\n\t\t\t\tint pos = 0;\n\t\t\t\t\n\t\t\t\tif(ra[i + k] - j < len){\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\trep(it, 2){\n\t\t\t\t\t\n\t\t\t\t\tint clen = min(60, len);\n\t\t\t\t\t\n\t\t\t\t\tll x = A[i + k][j + lb[k] + pos] & (1ull << clen) - 1ull;\n\t\t\t\t\tll y = B[k][lb[k] + pos] & (1ull << clen) - 1ull;\n\t\t\t\t\t\n\t\t\t\t\t/*\n\t\t\t\t\tif(i == 2 && j == 1){\n\t\t\t\t\t\tdbg(it); dbg(x); dbg(y);\n\t\t\t\t\t}\n\t\t\t\t\t*/\n\t\t\t\t\tif(x != y){\n\t\t\t\t\t\tok = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tlen -= clen;\n\t\t\t\t\tpos += clen;\n\t\t\t\t\tif(len <= 0) break;\n\t\t\t\t}\n\t\t\t\tif(!ok) break;\n\t\t\t}\n\t\t\tif(ok){\n\t\t\t\t//cerr<<\"i: \"<<i<<\" j: \"<<j<<endl;\n\t\t\t\tfound = 1;\n\t\t\t\tgoto END;\n\t\t\t}\n\t\t}\n\t\tEND:\n\t\tif(!found){\n\t\t\tcout << \"NO\" << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout << \"YES\" << endl;\n\t\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 10, "memory_kb": 63724, "score_of_the_acc": 0, "final_rank": 2 }, { "submission_id": "aoj_1535_890601", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef unsigned long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint N, p[2];\nll A[2000][4000], B[2000][4000];\nint la[2000], ra[2000];\nint lb[2000], rb[2000];\n\nvoid input(int n, ll a[2000][4000], int *l, int *r){\n\t\n\tmemset(a, 0, sizeof(A));\n\t\n\trep(i, 2 * n - 1){\n\t\tint len = i < n ? n + i : 3 * n - i - 2;\n\t\tint ofs = i < n ? 0 : i - n + 1;\n\t\t\n\t\tl[i] = ofs;\n\t\tr[i] = ofs + len;\n\t\t\n\t\trep(j, len){\n\t\t\tint t; scanf(\"%d\", &t);\n\t\t\t\n\t\t\tif(t) rep(k, 64){\n\t\t\t\tif(ofs + j - k < 0) break;\n\t\t\t\ta[i][ofs + j - k] |= 1ull << k;\n\t\t\t}\n\t\t}\n\t}\n\t//rep(i, 2 * n - 1) rep(j, 2 * n - 1) cerr<<(a[i][j]&1)<<(j==2*n-2?\"\\n\":\" \");\n}\n\nint main(){\n\t\n\tscanf(\"%d%d%d\", &N, p, p + 1);\n\t\n\tinput(N, A, la, ra);\n\trep(it, 2){\n\t\tinput(p[it], B, lb, rb);\n\t\tassert(p[it] <= 64);\n\t\t\n\t\tbool found = 0;\n\t\t\n\t\trep(i, 2 * N - 1) for(int j = la[i]; j < ra[i]; j++){\n\t\t\t\n\t\t\tbool ok = 1;\n\t\t\t\n\t\t\trep(k, 2 * p[it] - 1){\n\t\t\t\t\n\t\t\t\tint len = rb[k] - lb[k];\n\t\t\t\tint pos = 0;\n\t\t\t\t\n\t\t\t\tif(ra[i + k] - j < len){\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\trep(it, 2){\n\t\t\t\t\t\n\t\t\t\t\tint clen = min(64, len);\n\t\t\t\t\t\n\t\t\t\t\tll x = A[i + k][j + lb[k] + pos] & (1ull << clen) - 1;\n\t\t\t\t\tll y = B[k][lb[k] + pos];\n\t\t\t\t\t\n\t\t\t\t\tif(x != y){\n\t\t\t\t\t\tok = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tlen -= clen;\n\t\t\t\t\tpos += clen;\n\t\t\t\t\tif(len <= 0) break;\n\t\t\t\t}\n\t\t\t\tif(!ok) break;\n\t\t\t}\n\t\t\tif(ok){\n\t\t\t\t//cerr<<\"i: \"<<i<<\" j: \"<<j<<endl;\n\t\t\t\tfound = 1;\n\t\t\t\tgoto END;\n\t\t\t}\n\t\t}\n\t\tEND:\n\t\tif(!found){\n\t\t\tcout << \"NO\" << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout << \"YES\" << endl;\n\t\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 20, "memory_kb": 126232, "score_of_the_acc": -1.0094, "final_rank": 4 }, { "submission_id": "aoj_1535_890592", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef unsigned long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint N, p[2];\nll A[2000][4000], B[2000][4000];\nint la[2000], ra[2000];\nint lb[2000], rb[2000];\n\nvoid input(int n, ll a[2000][4000], int *l, int *r){\n\t\n\tmemset(a, 0, sizeof(A));\n\t\n\trep(i, 2 * n - 1){\n\t\tint len = i < n ? n + i : 3 * n - i - 2;\n\t\tint ofs = i < n ? 0 : i - n + 1;\n\t\t\n\t\tl[i] = ofs;\n\t\tr[i] = ofs + len;\n\t\t\n\t\trep(j, len){\n\t\t\tint t; scanf(\"%d\", &t);\n\t\t\t\n\t\t\tif(t) rep(k, 64){\n\t\t\t\tif(j - k < 0) break;\n\t\t\t\ta[i][ofs + j - k] |= 1ull << k;\n\t\t\t}\n\t\t}\n\t}\n\t//rep(i, 2 * n - 1) rep(j, 2 * n - 1) cerr<<(a[i][j]&1)<<(j==2*n-2?\"\\n\":\" \");\n}\n\nint main(){\n\t\n\tscanf(\"%d%d%d\", &N, p, p + 1);\n\t\n\tinput(N, A, la, ra);\n\trep(it, 2){\n\t\tinput(p[it], B, lb, rb);\n\t\tassert(p[it] <= 64);\n\t\t\n\t\tbool found = 0;\n\t\t\n\t\trep(i, 2 * N - 1) for(int j = la[i]; j < ra[i]; j++){\n\t\t\t\n\t\t\tbool ok = 1;\n\t\t\t\n\t\t\trep(k, 2 * p[it] - 1){\n\t\t\t\t\n\t\t\t\tint len = rb[k] - lb[k];\n\t\t\t\tint pos = 0;\n\t\t\t\t\n\t\t\t\tif(ra[i + k] - j < len){\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\trep(it, 2){\n\t\t\t\t\t\n\t\t\t\t\tint clen = min(64, len);\n\t\t\t\t\t\n\t\t\t\t\tll x = A[i + k][j + lb[k] + pos] & (1ull << clen) - 1;\n\t\t\t\t\tll y = B[k][lb[k] + pos];\n\t\t\t\t\t\n\t\t\t\t\tif(x != y){\n\t\t\t\t\t\tok = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tlen -= clen;\n\t\t\t\t\tpos += clen;\n\t\t\t\t\tif(len <= 0) break;\n\t\t\t\t}\n\t\t\t\tif(!ok) break;\n\t\t\t}\n\t\t\tif(ok){\n\t\t\t\t//cerr<<\"i: \"<<i<<\" j: \"<<j<<endl;\n\t\t\t\tfound = 1;\n\t\t\t\tgoto END;\n\t\t\t}\n\t\t}\n\t\tEND:\n\t\tif(!found){\n\t\t\tcout << \"NO\" << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout << \"YES\" << endl;\n\t\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 20, "memory_kb": 126232, "score_of_the_acc": -1.0094, "final_rank": 4 }, { "submission_id": "aoj_1535_890576", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef unsigned long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint N, p[2];\nll A[2000][4000], B[2000][4000];\nint la[2000], ra[2000];\nint lb[2000], rb[2000];\n\nvoid input(int n, ll a[2000][4000], int *l, int *r){\n\t\n\tmemset(a, 0, sizeof(A));\n\t\n\trep(i, 2 * n - 1){\n\t\tint len = i < n ? n + i : 3 * n - i - 2;\n\t\tint ofs = i < n ? 0 : i - n + 1;\n\t\t\n\t\tl[i] = ofs;\n\t\tr[i] = ofs + len;\n\t\t\n\t\trep(j, len){\n\t\t\tint t; scanf(\"%d\", &t);\n\t\t\t\n\t\t\tif(t) rep(k, 64){\n\t\t\t\tif(j - k < 0) break;\n\t\t\t\ta[i][ofs + j - k] |= 1ull << k;\n\t\t\t}\n\t\t}\n\t}\n\t//rep(i, 2 * n - 1) rep(j, 2 * n - 1) cerr<<(a[i][j]&1)<<(j==2*n-2?\"\\n\":\" \");\n}\n\nint main(){\n\t\n\tscanf(\"%d%d%d\", &N, p, p + 1);\n\t\n\tinput(N, A, la, ra);\n\trep(it, 2){\n\t\tinput(p[it], B, lb, rb);\n\t\t\n\t\tbool found = 0;\n\t\t\n\t\trep(i, 2 * N - 1) for(int j = la[i]; j < ra[i]; j++){\n\t\t\t\n\t\t\tbool ok = 1;\n\t\t\t\n\t\t\trep(k, 2 * p[it] - 1){\n\t\t\t\t\n\t\t\t\tint len = rb[k] - lb[k];\n\t\t\t\tint pos = 0;\n\t\t\t\t\n\t\t\t\tif(ra[i + k] - j < len){\n\t\t\t\t\tok = 0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\trep(it, 2){\n\t\t\t\t\t\n\t\t\t\t\tint clen = min(64, len);\n\t\t\t\t\t\n\t\t\t\t\tll x = A[i + k][j + lb[k] + pos] & (1ull << clen) - 1;\n\t\t\t\t\tll y = B[k][lb[k] + pos];\n\t\t\t\t\t\n\t\t\t\t\tif(x != y){\n\t\t\t\t\t\tok = 0;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tlen -= clen;\n\t\t\t\t\tpos += clen;\n\t\t\t\t\tif(len <= 0) break;\n\t\t\t\t}\n\t\t\t\tif(!ok) break;\n\t\t\t}\n\t\t\tif(ok){\n\t\t\t\t//cerr<<\"i: \"<<i<<\" j: \"<<j<<endl;\n\t\t\t\tfound = 1;\n\t\t\t\tgoto END;\n\t\t\t}\n\t\t}\n\t\tEND:\n\t\tif(!found){\n\t\t\tcout << \"NO\" << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\tcout << \"YES\" << endl;\n\t\n\treturn 0;\n}", "accuracy": 0.15384615384615385, "time_ms": 20, "memory_kb": 126236, "score_of_the_acc": -1.0094, "final_rank": 6 } ]
aoj_1541_cpp
Problem A: Yu-kun Likes an Integer Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい数字が大好きだ。そんなゆう君は最近、表に0から9までのいずれかの数字の書かかれたプレートを n 枚購入して n 桁の数を作る遊びに熱中している。 今月もゆう君は貰ったお小遣いで n 枚のプレートを買いに行こうと考えていた。 Problem プレートは表に書かれている数字によって値段が異なる。 それらを使って作れる n 桁の数をできる限り小さくしたい。 ゆう君の所持金額と各プレートの値段、購入するプレートの枚数が与えられるので、そこから作ることのできる数の最小値を求めよ。 購入するプレートの値段の総和が所持金額を超えなければ自由にプレートを購入できる。 同じ数字の書かれたプレートを複数枚購入しても良い。 プレートは必ず n 枚購入しなければならない。 n 枚購入できない場合は "NA" と出力すること。 購入後は n 枚のプレートを任意の順番で一直線上に並べる。 そうしてできる数字の列を10進数の数とし、その値が最小になるようにしたい。 先頭に1つ以上の0が連続していても良い。 ( 例えば 0019 なら 19 となり, 0000 なら 0 として考える ) 図1は0、1、9のプレートを購入した場合についての説明である。 図1 : 0,1,9のプレートを購入した場合 Hint この問題を解くに当たって、以下のことを参考にしても良い。 整数値を文字列に変換する方法を示す。 value を文字列として str に代入する。 Cの場合 #include<stdio.h> int main(){ int value = 123; // この値を文字列に変換する char str[6]; // この変数に value を文字列にしたものが入る sprintf(str,"%d",value); return 0; } C++の場合 #include<sstream> using namespace std; int main(){ int value = 123; // この値を文字列に変換する string str; // この変数に value を文字列にしたものが入る stringstream ss; ss << value; ss >> str; return 0; } JAVAの場合 class Main { public static void main(String args[]){ int value = 123; // この値を文字列に変換する String str = new Integer(value).toString(); // この変数に value を文字列にしたものが入る } } Input n m c 0 c 1 c 2 ... c 9 1行目に2つの整数 n , m が空白区切りで与えられる。 n は購入するプレートの枚数, m はゆう君の所持金額を表す。 2行目には10個の整数が空白区切りで与えられる。 c i ( i は0以上9以下 ) は表に i が書かれたプレートの値段を表す。 Constraints 入力は以下の条件を満たす。 1 ≤ n ≤ 5 0 ≤ m ≤ 500 1 ≤ c i ≤ 1000 ( 0 ≤ i ≤ 9 ) Output n 枚のプレートを購入し、それらを任意の順番で並べることでできる数の値の最小値を出力せよ。 先頭にいくつかの0を含む場合はそのまま出力すること。 ( 例えば答えが 0019 の場合は先頭の0を取り除いて 19 とするのではなく、そのまま 0019 と出力すること ) 所持金額では n 枚プレートを購入できない場合は "NA" と出力すること。 Sample Input 1 1 10 1 2 3 4 5 6 7 8 9 10 Sample Output 1 0 Sample Input 2 3 10 8 4 5 3 5 6 9 10 11 2 Sample Output 2 119 Sample Input 3 5 30 25 51 32 9 2 1 10 2 5 10 Sample Output 3 04555 Sample Input 4 5 100 101 101 101 101 101 101 101 101 101 101 Sample Output 4 NA
[ { "submission_id": "aoj_1541_9659771", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nvector<vector<int> > combinations;\n\nvoid gen(int idx, vector<int> &comb) {\n if (idx == 0) {\n combinations.push_back(comb);\n return;\n }\n for (int i = 0; i < 10; i++) {\n comb.push_back(i);\n gen(idx - 1, comb);\n comb.pop_back();\n }\n}\nvoid acc() {\n int n, m;\n cin >> n >> m;\n vector<int> cost(10);\n for (int i = 0; i < 10; i++) {\n cin >> cost[i];\n }\n vector<int>comb ; \n gen(n, comb);\n std::sort(combinations.begin(), combinations.end());\n for (vector<int> comb: combinations) {\n int sum = 0;\n for (int digit: comb) {\n sum += cost[digit];\n }\n if (sum <= m) {\n for (int digit: comb) {\n cout << digit;\n }\n cout <<'\\n';\n return;\n }\n }\n cout << \"NA\"<<'\\n';\n}\n\nint main() {\n acc();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 9700, "score_of_the_acc": -1, "final_rank": 8 }, { "submission_id": "aoj_1541_9659752", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nvector<vector<int>> combinations;\n\nvoid gen(int idx, vector<int> comb) {\n if (idx == 0) {\n combinations.push_back(comb);\n return;\n }\n for (int i = 0; i < 10; i++) {\n comb.push_back(i);\n gen(idx - 1, comb);\n comb.pop_back();\n }\n}\nvoid acc() {\n int n, m;\n cin >> n >> m;\n vector<int> cost(10);\n for (int i = 0; i < 10; i++) {\n cin >> cost[i];\n }\n gen(n, {});\n std::sort(combinations.begin(), combinations.end());\n for (auto comb: combinations) {\n int sum = 0;\n for (auto digit: comb) {\n sum += cost[digit];\n }\n if (sum <= m) {\n for (auto digit: comb) {\n cout << digit;\n }\n cout <<'\\n';\n return;\n }\n }\n cout << \"NA\" << '\\n';\n}\n\nint main() {\n acc();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 9436, "score_of_the_acc": -0.9689, "final_rank": 6 }, { "submission_id": "aoj_1541_9659749", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\nvector<vector<int>> combinations;\n\nvoid gen(int idx, vector<int> comb) {\n if (idx == 0) {\n combinations.push_back(comb);\n return;\n }\n for (int i = 0; i < 10; i++) {\n comb.push_back(i);\n gen(idx - 1, comb);\n comb.pop_back();\n }\n}\n\nvoid acc() {\n int n, m;\n cin >> n >> m;\n vector<int> cost(10);\n for (int i = 0; i < 10; i++) {\n cin >> cost[i];\n }\n gen(n, {});\n std::sort(combinations.begin(), combinations.end());\n for (auto comb: combinations) {\n int sum = 0;\n for (auto digit: comb) {\n sum += cost[digit];\n }\n if (sum <= m) {\n for (auto digit: comb) {\n cout << digit;\n }\n cout <<'\\n';\n return;\n }\n }\n cout << \"NA\" << '\\n';\n}\n\nint main() {\n acc();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 9464, "score_of_the_acc": -0.9722, "final_rank": 7 }, { "submission_id": "aoj_1541_1081865", "code_snippet": "#include <vector>\n#include <list>\n#include <map>\n#include <set>\n#include <queue>\n#include <deque>\n#include <tuple>\n#include <stack>\n#include <bitset>\n#include <algorithm>\n#include <functional>\n#include <numeric>\n#include <utility>\n#include <sstream>\n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <cmath>\n#include <cstdlib>\n#include <cctype>\n#include <string>\n#include <cstring>\n#include <ctime>\n\ntypedef long long LL;\n\n//container util\n//------------------------------------------\n#define ALL(a) (a).begin(),(a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define MP make_pair\n#define DECIM8 fixed<<setprecision(8) \n#define SZ(a) int((a).size())\n#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)\n#define EXIST(s,e) ((s).find(e)!=(s).end())\n#define SORT(c) sort((c).begin(),(c).end())\n\n//repetition\n//------------------------------------------\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n//constant\n//--------------------------------------------\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\n\n//clear memory\n#define CLR(a) memset((a), 0 ,sizeof(a))\n\nusing namespace std;\nbool canbuy(int n,vector<int> cost,int x,int money)\n{\n if(n==0)\n if(money<0) return false;\n else return true;\n else\n return canbuy(n-1,cost,x/10,money-cost[x%10]);\n}\nvoid printn(int x,int n)\n{\n if(n==0) return;\n else{\n printn(x/10,n-1);\n cout << x%10;\n }\n return;\n}\nint main(void)\n{\n int n,m;\n cin >> n >> m;\n vector<int> cost;\n int costmin=1000;\n REP(i,10){\n int tmp;\n cin >> tmp;\n if(costmin>tmp) costmin=tmp;\n cost.PB(tmp);\n }\n if(m<costmin*n){\n cout << \"NA\" << endl;\n return 0;\n }\n int answer=0;\n for(;!canbuy(n,cost,answer,m);answer++);\n printn(answer,n);\n cout << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1216, "score_of_the_acc": -0.2009, "final_rank": 4 }, { "submission_id": "aoj_1541_1080932", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nstring toString(int x, int k){\n stringstream ss;\n ss << x;\n string res = ss.str();\n while((int)res.size()<k)res = \"0\" + res;\n return res;\n}\n\nint main(){\n int n,m;\n int c[10];\n\n cin >> n >> m;\n for(int i=0;i<10;i++)cin >> c[i];\n\n int lim = 1;\n for(int i=0;i<n;i++)lim*=10;\n\n string ans = \"NA\";\n for(int x=0;x<lim;x++){\n string s = toString(x,n);\n int sum = 0;\n for(int i=0;i<n;i++){\n sum += c[s[i]-'0'];\n }\n if(sum<=m){\n ans = s; break;\n }\n }\n\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1224, "score_of_the_acc": -0.8019, "final_rank": 5 }, { "submission_id": "aoj_1541_1080567", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <set>\n#include <map>\n#include <algorithm>\t// require sort next_permutation count __gcd reverse etc.\n#include <cstdlib>\t// require abs exit\n#include <cstdio>\t// require scanf printf\n#include <functional>\n#include <numeric>\t// require accumulate\n#include <cmath>\n#include <climits>\n#include <limits>\n#include <cfloat>\n#include <iomanip>\t// require setw\n#include <sstream>\t// require stringstream \n#include <cstring>\t// require memset\n#include <cctype>\t// require tolower, toupper\n#include <fstream>\t// require freopen\n#include <ctime>\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define ALL(A) A.begin(), A.end()\n#define INF 1<<29\n#define EPS 1e-9\n#define each(i,c) for(auto i=(c).begin();i!=(c).end();++i)\n#define exist(s,e) ((s).find(e)!=(s).end())\n#define clr(a) memset((a),0,sizeof(a))\n#define nclr(a) memset((a),-1,sizoef(a))\n#define sz(s) (int)((s).size())\n#define INRANGE(x,s,e) ((s)<=(x) && (x)<(e))\n#define pb push_back\n#define MP(x,y) make_pair((x),(y))\n#define DEBUG 0\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\nstring FILE_NAME = \"testcase.A\";\nstring NAME;\nstring itos (int n )\n{\n\tstringstream ss;\n\tss << n;\n\n\treturn ss.str();\n}\n\nstring i2s (int n, int digit )\n{\n\tstringstream ss;\n\tss << n;\n\tstring s = ss.str();\n\twhile (s.length() < digit ){\n\t\ts = '0' + s;\n\t} // end while\n\n\treturn s;\n}\n\nint c[10];\n\nint main()\n{\n//\tcut here before submit \n#if DEBUG\n\tNAME = FILE_NAME;\n\tint CNT = 1;\n\tNAME += itos (CNT );\n\twhile (freopen (NAME.c_str() , \"r\", stdin ) != NULL ) {\n#endif\n\tmemset (c, 0, sizeof (c ) );\n\tios_base::sync_with_stdio(0);\n\tint n, m; cin >> n >> m;\n\trep (i, 10 ) cin >> c[i];\n\tstring res = \"\";\n\n\trep (i, pow(10, n ) ){\n\t\tstring num = i2s(i, n );\n\t\tint curr = 0;\n\t\trep (j, num.length() ){\n\t\t\tcurr += c[(int)(num[j] - '0')];\n\t\t} // end rep\n\t\tif (num.length() == n && curr <= m ){\n\t\t\tres = num;\n\t\t\tbreak;\n\t\t} // end if\n\t} // end rep\n\t\n\tcout << (res.empty() ? \"NA\" : res ) << endl;\n\n#if DEBUG\n\tCNT++;\t// cut here before submit\n\tNAME = FILE_NAME;\n\tNAME += itos (CNT );\n\t} // end loop; cut here before submit\n#endif\t\t\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 1268, "score_of_the_acc": -1.0071, "final_rank": 9 }, { "submission_id": "aoj_1541_1080336", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\nusing namespace std;\n\n#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)\ntemplate <typename T> void print_container(ostream& os, const T& c) { const char* _s = \" \"; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\n\ntemplate <typename T> void print(T a, int n, const string& split = \" \") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }\ntemplate <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }\ntemplate <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }\n#define dump(v) (cerr << #v << \": \" << v << endl)\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\n\n\nint n, m, c[10];\nint b[5];\nstring res;\nvoid dfs(int depth)\n{\n if (depth == n)\n {\n string num;\n int sum = 0;\n rep(i, n)\n {\n sum += c[b[i]];\n num += b[i] + '0';\n }\n if (sum <= m)\n upmin(res, num);\n return;\n }\n\n rep(i, 10)\n {\n b[depth] = i;\n dfs(depth + 1);\n }\n}\nint main()\n{\n cin >> n >> m;\n input(c, 10);\n\n res = \"@\";\n dfs(0);\n cout << (res == \"@\" ? \"NA\" : res) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1208, "score_of_the_acc": -0.2, "final_rank": 3 }, { "submission_id": "aoj_1541_1080299", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n#define tpl(...) make_tuple(__VA_ARGS__)\nconst int INF = 0x3f3f3f3f;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T>ostream&operator<<(ostream &o,const vector<T>&t){o<<'[';FOR(i,t){if(i!=t.begin())o<<',';o<<*i;}return o<<']';}\ntemplate<class S,class T>ostream&operator<<(ostream &o,const pair<S,T>&t){return o<<'('<<t.first<<','<<t.second<<')';}\ntemplate<int N,class Tp>void output(ostream&,const Tp&){}\ntemplate<int N,class Tp,class,class ...Ts>void output(ostream &o,const Tp&t){if(N)o<<',';o<<get<N>(t);output<N+1,Tp,Ts...>(o,t);}\ntemplate<class ...Ts>ostream&operator<<(ostream&o,const tuple<Ts...>&t){o<<'(';output<0,tuple<Ts...>,Ts...>(o,t);return o<<')';}\ntemplate<class T>void output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20];\nint k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);}\ntemplate<class T>void outputs(T t){output(t);}\ntemplate<class S,class ...T>void outputs(S a,T...t){output(a,32);outputs(t...);}\ntemplate<class T>void output(T *a,int n){REP(i,n)output(a[i],i!=n-1?',':10);}\ntemplate<class T>void output(T *a,int n,int m){REP(i,n)output(a[i],m);}\ntemplate<class T>bool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;);\nif(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;}\ntemplate<class S,class ...T>bool input(S&a,T&...t){input(a);return input(t...);}\n\nint a[10];\nint n,m;\nstring anss;\nll ansv;\nvoid dfs(int num, int cost, ll sm, string s) {\n // cout << num << \" \" << cost << \" \" << sm << \" \" << s << endl;\n if (num == n) {\n if (cost <= m) {\n if (chmin(ansv, sm)) {\n anss = s;\n }\n }\n return;\n }\n REP(i,10) {\n dfs(num+1, cost+a[i], sm*10+i, s+string(1,i+'0'));\n }\n}\n\nint main() {\n while(cin>>n>>m) {\n REP(i,10) input(a[i]);\n ansv = 1LL<<60;\n dfs(0, 0, 0, \"\");\n // cout << ansv << endl;\n if (ansv == 1LL<<60)\n puts(\"NA\");\n else\n cout << anss << endl;\n \n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1208, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1541_1080293", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nclass range {\nprivate:\n struct Iterator {\n int val;\n int operator*() {return val;}\n bool operator!=(Iterator &itr) {return val < itr.val;}\n void operator++() {++val;}\n };\n Iterator i, n;\npublic:\n range(int n) : i({0}), n({n}) {}\n range(int i, int n) : i({i}), n({n}) {}\n Iterator &begin() {return i;}\n Iterator &end() {return n;}\n};\n\ntemplate<class T> T at(vector<T> v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];}\n\n#include <sstream>\n\ntemplate<class A, class B> B convert(A a) {\n stringstream ss;\n ss << a;\n B b;\n ss >> b;\n return b;\n}\n\nint n, m, c[10];\n\nconst string INF = \"999999\";\n\nstring solve() {\n if (n == 0) {\n if (m >= 0) return \"\";\n return INF;\n }\n string res = INF;\n for (int i : range(10)) if (c[i] > 0) {\n --n;\n m -= c[i];\n string r = solve();\n if (r != INF) res = min(res, convert<int, string>(i) + r);\n m += c[i];\n ++n;\n }\n return res;\n}\n\nint main() {\n cin >> n >> m;\n for (int i : range(10)) cin >> c[i];\n string res = solve();\n if (res == INF) cout << \"NA\" << endl;\n else cout << res << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1236, "score_of_the_acc": -0.0033, "final_rank": 2 } ]
aoj_1531_cpp
Problem F: Remainder Zero Problem 整数の集合A,Bについて,次の条件を満たす整数 x がいくつ存在するか答えよ。 A i mod x = 0 かつ x mod B j = 0 がすべての i (1 ≤ i ≤ N), j (1 ≤ j ≤ M) について成り立つ。 ( a mod b は a を b で割ったときの余りを意味する) Input N M A 1 A 2 ... A N B 1 B 2 ... B M Constraints 入力は以下の条件を満たす。 1 ≤ N , M ≤ 5 1 ≤ A i , B j ≤ 10 14 (1 ≤ i ≤ N , 1 ≤ j ≤ M ) Output 条件を満たす整数 x がいくつ存在するか一行で出力せよ。 Sample Input 1 1 2 18 6 9 Sample Output 1 1 Sample Input 2 1 2 256 2 4 Sample Output 2 7
[ { "submission_id": "aoj_1531_10314507", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\n\nusing ll=long long;\n\nll modpow(ll a,ll n,ll p){\n\tll r=1;\n\twhile(n>0){\n\t\tif(n%2)r=(r*a)%p;\n\t\tn/=2;\n\t\ta=(a*a)%p;\n\t}\n\treturn r;\n}\n\nint main(){\n\t\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tll N,M;\n\tcin>>N>>M;\n\tll G=0;\n\tfor(int i=0;i<N;i++){\n\t\tll A;\n\t\tcin>>A;\n\t\tG=gcd(G,A);\n\t}\n\tll L=1;\n\tfor(int i=0;i<M;i++){\n\t\tll B;\n\t\tcin>>B;\n\t\tll P=gcd(L,B);\n\t\tL/=P;\n\t\tif(L>G/B){\n\t\t\tcout<<0<<endl;\n\t\t\treturn 0;\n\t\t}\n\t\tL*=B;\n\t}\n\tll an=0;\n\tfor(ll n=1;n*n<=G;n++){\n\t\tif(G%n==0){\n\t\t\tif(n%L==0)an++;\n\t\t\tif(n*n!=G&&(G/n)%L==0)an++;\n\t\t}\n\t}\n\tcout<<an<<endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3584, "score_of_the_acc": -0.1199, "final_rank": 4 }, { "submission_id": "aoj_1531_8464385", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1531.cc: Problem F: Remainder Zero\n */\n\n#include<cstdio>\n#include<vector>\n#include<algorithm>\n#include<utility>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 5;\nconst int MAX_M = 5;\nconst int MAX_P = 10000000 + 50;\n\n/* typedef */\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<ll,int> pli;\ntypedef vector<pli> vpli;\n\n/* global variables */\n\nbool primes[MAX_P + 1];\nll as[MAX_N], bs[MAX_M];\n\n/* subroutines */\n\nint gen_primes(int maxp, vi &pnums) {\n fill(primes, primes + maxp + 1, true);\n primes[0] = primes[1] = false;\n\n int p;\n for (p = 2; p * p <= maxp; p++)\n if (primes[p]) {\n pnums.push_back(p);\n for (int q = p * p; q <= maxp; q += p) primes[q] = false;\n }\n for (; p <= maxp; p++)\n if (primes[p]) pnums.push_back(p);\n return (int)pnums.size();\n}\n\nbool prime_decomp(ll n, vi &pnums, vpli& pds) {\n pds.clear();\n\n int pn = pnums.size();\n for (int i = 0; i < pn; i++) {\n int pi = pnums[i];\n if ((ll)pi * pi > n) {\n if (n > 1) pds.push_back(pli(n, 1));\n return true;\n }\n\n if (n % pi == 0) {\n int fi = 0;\n while (n % pi == 0) n /= pi, fi++;\n pds.push_back(pli(pi, fi));\n }\n }\n return false;\n}\n\ntemplate <typename T>\nT gcd(T m, T n) { // m >= 0, n >= 0\n if (m < n) swap(m, n);\n while (n > 0) {\n T r = m % n;\n m = n;\n n = r;\n }\n return m;\n}\n\ntemplate <typename T>\nT lcm(T m, T n) {\n return m * n / gcd<T>(m, n);\n}\n\n/* main */\n\nint main() {\n vi pnums;\n gen_primes(MAX_P, pnums);\n\n int n, m;\n scanf(\"%d%d\", &n, &m);\n for (int i = 0; i < n; i++) scanf(\"%lld\", as + i);\n for (int i = 0; i < m; i++) scanf(\"%lld\", bs + i);\n\n ll g = 0;\n for (int i = 0; i < n; i++) g = gcd(g, as[i]);\n //printf(\"g=%lld\\n\", g);\n\n for (int i = 0; i < m; i++)\n if (g % bs[i] != 0) { puts(\"0\"); return 0; }\n\n ll l = 1;\n for (int i = 0; i < m; i++) l = lcm(l, bs[i]);\n //printf(\"l=%lld\\n\", l);\n\n ll k = g / l;\n //printf(\"k=%lld\\n\", k);\n\n vpli pds;\n prime_decomp(k, pnums, pds);\n\n ll p = 1;\n for (auto &pd: pds) p *= pd.second + 1;\n\n printf(\"%lld\\n\", p);\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 18256, "score_of_the_acc": -0.8817, "final_rank": 12 }, { "submission_id": "aoj_1531_7616261", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define REP(i,num) for(ll i=0;i<(num);++i)\n#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))\n#define LOOP(i) while(i--)\n#define ALL(c) c.begin(),c.end()\n#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;\n#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}\n\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\n\nconstexpr ll atcoder_mod = 1e9+7;\n\ntemplate<typename T=ll>\nT in(){ T x; cin >> x; return (x); }\ntemplate<typename T=ll,typename C=vector<T>>\nC vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }\ntemplate<typename T=ll,size_t N>\narray<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;}\n\nvoid vout(){ cout << endl; }\ntemplate<typename Head,typename... Tail>\nvoid vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }\nvoid out(){ cout << endl; }\ntemplate<typename Head,typename... Tail>\nvoid out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }\n\ntemplate<typename T>\nbool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }\ntemplate<typename T>\nbool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }\n\nvector<ll> enum_div(ll n){\n\tvector<ll> ret;\n\tfor(ll i=1;i*i<=n;++i){\n\t\tif(n%i==0){\n\t\t\tret.push_back(i);\n\t\t\tif(i*i!=n){\n\t\t\t\tret.push_back(n/i);\n\t\t\t}\n\t\t}\n\t}\n\treturn ret;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tcout<<fixed<<setprecision(10);\n\n\tauto N=in(),M=in();\n\tvll A=vecin(N),B=vecin(M);\n\tvll S;\n\tfor(auto& x:A){\n\t\tauto D=enum_div(x);\n\t\tS.insert(S.end(),D.begin(),D.end());\n\t}\n\tsort(ALL(S));\n\tS.erase(unique(ALL(S)),S.end());\n\n\tll ans=0;\n\tfor(auto& x:S){\n\t\tbool flag=true;\n\t\tREP(i,N){\n\t\t\tif(A[i]%x){\n\t\t\t\tflag=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tREP(i,M){\n\t\t\tif(x%B[i]){\n\t\t\t\tflag=false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(flag) ans++;\n\t}\n\t\n\tout(ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3528, "score_of_the_acc": -0.5028, "final_rank": 8 }, { "submission_id": "aoj_1531_3325054", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(ll arg_num,ll arg_count,ll arg_min_mult,ll arg_max_mult){\n\t\tnum = arg_num;\n\t\tcount = arg_count; //N個の数のうち、いくつの数が持っているか\n\t\tmin_mult = arg_min_mult;\n\t\tmax_mult = arg_max_mult;\n\t}\n\tll num,count,min_mult,max_mult;\n};\n\nll N,M;\nvector<Info> A,B;\n\n\nint main(){\n\n\tll tmp;\n\n\tscanf(\"%lld %lld\",&N,&M);\n\n\tA.push_back(Info(1,N,1,1)); //1はAの共通約数の1つ\n\tB.push_back(Info(1,M,1,1));\n\n\tll loc,tmp_count;\n\n\t//★★A:公約数が知りたいので、N個すべてに共通する素因数を求める★★\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lld\",&tmp);\n\n\t\tfor(ll k = 2; k*k <= tmp; k++){ //入力を素因数に分解する\n\n\t\t\tif(tmp%k != 0)continue;\n\n\t\t\tloc = -1;\n\n\t\t\tfor(int a = 0; a < A.size(); a++){ //既出の数か調べる\n\n\t\t\t\tif(A[a].num == k){\n\t\t\t\t\tA[a].count += 1;\n\t\t\t\t\tloc = a;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//次数を数える\n\t\t\ttmp_count = 0;\n\n\t\t\twhile(tmp%k == 0){\n\t\t\t\ttmp /= k;\n\t\t\t\ttmp_count++;\n\t\t\t}\n\n\t\t\tif(loc == -1 && i == 0){ //初出現かつ、最初の数字\n\n\t\t\t\tA.push_back(Info(k,1,tmp_count,0));\n\n\t\t\t}else if(loc != -1){\n\n\t\t\t\tA[loc].min_mult = min(A[loc].min_mult,tmp_count); //次数のminを取る\n\n\t\t\t}else{\n\n\t\t\t\t//N個全部に共通する素因数でなければ意味がないのでDo nothing\n\t\t\t}\n\t\t}\n\n\t\tif(tmp == 1)continue;\n\n\t\t//1にならなかった場合\n\t\tloc = -1;\n\n\t\tfor(int a = 0; a < A.size(); a++){ //既出の数か調べる\n\n\t\t\tif(A[a].num == tmp){\n\t\t\t\tA[a].count += 1;\n\t\t\t\tloc = a;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(loc != -1 || i != 0)continue;\n\n\t\tA.push_back(Info(tmp,1,1,1));\n\t}\n\n\t//★★B:最小公倍数が知りたいので、少なくとも1つに出現する素因数を洗い出し、次数のmaxを取る★★\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%lld\",&tmp);\n\n\t\tfor(ll k = 2; k*k <= tmp; k++){ //入力を素因数に分解する\n\n\t\t\tif(tmp%k != 0)continue;\n\n\t\t\tloc = -1;\n\n\t\t\tfor(int b = 0; b < B.size(); b++){ //既出の数か調べる\n\n\t\t\t\tif(B[b].num == k){\n\t\t\t\t\tloc = b;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//次数を数える\n\t\t\ttmp_count = 0;\n\n\t\t\twhile(tmp%k == 0){\n\t\t\t\ttmp /= k;\n\t\t\t\ttmp_count++;\n\t\t\t}\n\n\t\t\tif(loc == -1){ //初出現\n\n\t\t\t\tB.push_back(Info(k,1,0,tmp_count));\n\n\t\t\t}else{ //既出\n\n\t\t\t\tB[loc].max_mult = max(B[loc].max_mult,tmp_count); //次数のmaxを取る\n\n\t\t\t}\n\t\t}\n\n\t\tif(tmp == 1)continue;\n\n\t\t//1にならなかった場合\n\t\tloc = -1;\n\n\t\tfor(int b = 0; b < B.size(); b++){ //既出の数か調べる\n\n\t\t\tif(B[b].num == tmp){\n\t\t\t\tloc = b;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(loc != -1)continue;\n\n\t\tB.push_back(Info(tmp,1,1,1));\n\n\t}\n\n\tll ans = 1;\n\tbool FLG;\n\n\t//少なくともxは、Bの全ての素因数を持ち、かつ各々の次数がmax以上\n\tfor(int i = 0; i < B.size(); i++){ //Bの少なくとも1つ出現した素因数を全探索\n\n\t\tFLG = false;\n\n\t\tfor(int k = 0; k < A.size(); k++){ //Aを全探索\n\n\t\t\tif(A[k].num == B[i].num && A[k].count == N && A[k].min_mult >= B[i].max_mult){\n\n\t\t\t\tans *= (A[k].min_mult-B[i].max_mult+1);\n\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(!FLG){\n\n\t\t\tprintf(\"0\\n\");\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\t//Aのみにあり、Bにない素因数を探す\n\tfor(int i = 0; i < A.size(); i++){\n\n\t\tif(A[i].count < N)continue;\n\n\t\tFLG = false;\n\n\t\tfor(int k = 0; k < B.size(); k++){\n\n\t\t\tif(B[k].num == A[i].num){\n\n\t\t\t\tFLG = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif(FLG)continue;\n\n\t\tans *= (A[i].min_mult+1);\n\t}\n\n\tprintf(\"%lld\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 3236, "score_of_the_acc": -1.1025, "final_rank": 14 }, { "submission_id": "aoj_1531_3323525", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nint main(){\n int n,m;\n cin >>n >>m;\n vector<ll> a(n),b(m);\n rep(i,n) cin >>a[i];\n rep(i,m) cin >>b[i];\n\n ll g = a[0];\n rep(i,n) g = __gcd(g,a[i]);\n\n ll ans = 0;\n\n auto check = [&](ll x){\n rep(i,m)if(x%b[i] != 0) return;\n ++ans;\n };\n\n for(ll i=1; i*i<=g; ++i){\n if(g%i == 0){\n check(i);\n if(g/i != i) check(g/i);\n }\n }\n\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3116, "score_of_the_acc": -0.168, "final_rank": 5 }, { "submission_id": "aoj_1531_3216291", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\n\nlong long int gcd(long long int l, long long int r) {\n\tif (l > r)return gcd(r, l);\n\telse {\n\t\tif (r%l) {\n\t\t\treturn gcd(l, r%l);\n\n\t\t}\n\t\telse {\n\t\t\treturn l;\n\t\t}\n\t}\n}\n\nvector<long long int> divisor(long long int n) {\n\tvector<long long int> res;\n\tfor (long long int i = 1; i * i <= n; ++i) {\n\t\tif (n % i == 0) {\n\t\t\tres.push_back(i);\n\t\t\tif (i * i != n) res.push_back(n / i);\n\t\t}\n\t}\n\tsort(begin(res), end(res));\n\treturn res;\n}\nint main() {\n\tint N,M;cin>>N>>M;\n\tvector<long long int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int ag=as[0];\n\tfor (int i = 1; i < N; ++i) {\n\t\tag=gcd(ag,as[i]);\n\t}\n\tbool ok=true;\n\tlong long int bl=bs[0];\n\tfor (int i = 1; i < M; ++i) {\n\t\tdouble k=bl;\n\t\tk*=bs[i];\n\t\tk/=gcd(bl,bs[i]);\n\t\tif (k > 1e15) {\n\t\t\tok=false;\n\t\t}\n\t\telse {\n\t\t\tbl=bl/gcd(bl,bs[i])*bs[i];\n\t\t}\n\t}\n\tint ans=0;\n\tif (ok) {\n\t\tif (ag%bl == 0) {\n\t\t\tlong long int tt=ag/bl;\n\t\t\tauto divs=divisor(tt);\n\t\t\tans=divs.size();\n\t\t}\n\t}\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3288, "score_of_the_acc": -0.1766, "final_rank": 6 }, { "submission_id": "aoj_1531_895557", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool overflow(unsigned long long a, unsigned long long b) {\n\tconst unsigned long long c = a * b;\n\treturn !(c / a == b);\n}\n\nint main() {\n\tint n, m;\n\tcin >> n >> m;\n\n\tvector<unsigned long long> a(n), b(m);\n\tfor(auto &e : a) cin >> e;\n\tfor(auto &e : b) cin >> e;\n\n\tunsigned long long g = a[0];\n\tfor(int i = 1; i < n; ++i) {\n\t\tg = __gcd(g, a[i]);\n\t}\n\n\tunsigned long long l = b[0];\n\tfor(int i = 1; i < m; ++i) {\n\t\tl /= __gcd(l, b[i]);\n\n\t\tif(overflow(l, b[i])) {\n\t\t\tcout << 0 << endl;\n\t\t\treturn 0;\n\t\t}\n\n\t\tl *= b[i];\n\t}\n\n\tint ans = 0;\n\tfor(unsigned long long i = 1; i * i <= g; ++i) {\n\t\tif(g % i == 0) {\n\t\t\tif(i % l == 0) ++ans;\n\t\t\tif(i * i != g && (g / i) % l == 0) ++ans;\n\t\t}\n\t}\n\n\tcout << ans << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 1204, "score_of_the_acc": -0.0867, "final_rank": 1 }, { "submission_id": "aoj_1531_894271", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <iomanip>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <utility>\n#include <numeric>\n#include <algorithm>\n#include <functional>\n#include <cctype>\n#include <complex>\n#include <string>\n#include <sstream>\n#include <cassert>\nusing namespace std;\n\n//common\ntypedef int i32;\ntypedef long long i64,ll;\n#define BR \"\\n\"\n\n#define ALL(c) (c).begin(),(c).end()\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n\ntypedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;\ntypedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;\ntypedef vector<string> vs; typedef long double ld;\n\n//config\n#define MODE_DEBUG\n//#define INF 1<<30\n//#define EPS 1e-8\n//const ll MOD =100000007;\n\n//debug\n#ifdef NDEBUG\n#define DUMP(x)\n#define DUMPLN(x)\n#define DEBUG(x)\n#define DEBUGLN(x)\n#define LINE()\n#define LINELN()\n#define CHECK(exp,act)\n#define STOP(e)\n#else\n#define DUMP(x) cerr << #x << \" = \" << (x)\n#define DUMPLN(x) DUMP(x) <<endl\n#define DEBUG(x) DUMP(x) << LINE() << \" \" << __FILE__\n#define DEBUGLN(x) DEBUG(x)<<endl\n#define LINE() cerr<< \" (L\" << __LINE__ << \")\"\n#define LINELN() LINE()<<endl\n#define CHECK(exp,act) if(exp!=act){DUMPLN(exp);DEBUGLN(act);}\n#define STOP(e) CHECK(e,true);if(!(e)) exit(1);\n#endif\n\ntemplate<class T> inline string toString(const vector<T>& x) {\n\tstringstream ss;\n\tREP(i,x.size()){\n\t\tif(i!=0)ss<<\" \";\n\t\tss<< x[i];\n\t}\n\treturn ss.str();\n}\ntemplate<class T> inline string toString(const vector<vector<T> >& map) {\n\tstringstream ss;\n\tREP(i,map.size()){\n\t\tif(i!=0)ss<<BR;\n\t\tss<< toString(map[i]);\n\t}\n\treturn ss.str();\n}\ntemplate<class K,class V> string toString(map<K,V>& x) {\n\tstring res;stringstream ss;\n\tfor(auto& p:x)ss<< p.first<<\":\" << p.second<<\" \";\n\treturn ss.str();\n}\n\ntemplate<typename T,typename V> inline T mod(T v,V MOD){\n\treturn (v%MOD+MOD)%MOD;\n}\n\n#define nextInt(n) scanf(\"%d\",&n)\n#define nextLong(n) scanf(\"%lld\",&n)\n#define nextDouble(n) scanf(\"%lf\",&n)\n\nint INF=1<<28;\n\ntemplate<typename T>T gcd_positive(T a, T b) { return b == 0 ? a : gcd_positive(b,a%b); }\ntemplate<typename T>T gcd(T a, T b) { return gcd_positive(abs(a), abs(b)); }\ntemplate<typename T>T lcm(T a,T b){return a*b/gcd(a,b);}\n\nclass Main{\npublic:\t\n\tvoid run(){\n\t\tint N,M;cin >> N >> M;\n\n\t\tll A;cin >> A;REP(i,N-1){ll a;cin >> a; A=gcd(A,a);}\n\t\n\t\tset<ll> ls;\n\n\t\tmap<ll,int> as;\n\t\tfor(ll p=2;p*p<=1e14;p++){\n\t\t\tint c=0;\n\t\t\twhile(A%p==0){\n\t\t\t\tc++;A/=p;\n\t\t\t}\n\t\t\tif(c!=0){\n\t\t\t\tas[p]=c;ls.insert(p);\n\t\t\t}\n\t\t}\n\t\tif(A!=1) {\n\t\t\tas[A]=1;\n\t\t\tls.insert(A);\n\t\t}\n\n\t\tmap<ll,int> bs;\n\t\tREP(i,M){\n\t\t\tll B;cin >> B;\n\t\t\tfor(ll p=2;p*p<=1e14;p++){\n\t\t\t\tint c=0;\n\t\t\t\twhile(B%p==0){\n\t\t\t\t\tc++;B/=p;\n\t\t\t\t}\n\t\t\t\tif(c!=0){\n\t\t\t\t\tbs[p]=max(bs[p],c);ls.insert(p);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(B!=1) {\n\t\t\t\tbs[B]=max(bs[B],1);\n\t\t\t\tls.insert(B);\n\t\t\t}\n\t\t}\n\t\tll res =1;\n\t\tEACH(it,ls){\n\t\t\tres*=max(as[*it] -bs[*it]+1,0);\n\t\t}\n\t\tcout << res<<endl;\n\t}\n};\n int main(){\n \tcout <<fixed<<setprecision(15);\n\tios::sync_with_stdio(false);\n \tMain().run();\n \treturn 0;\n }", "accuracy": 1, "time_ms": 530, "memory_kb": 1204, "score_of_the_acc": -0.7296, "final_rank": 11 }, { "submission_id": "aoj_1531_894264", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <iomanip>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <utility>\n#include <numeric>\n#include <algorithm>\n#include <functional>\n#include <cctype>\n#include <complex>\n#include <string>\n#include <sstream>\n#include <cassert>\nusing namespace std;\n\n//common\ntypedef int i32;\ntypedef long long i64,ll;\n#define BR \"\\n\"\n\n#define ALL(c) (c).begin(),(c).end()\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)\n#define IN(l,v,r) ((l)<=(v) && (v)<(r))\n\ntypedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;\ntypedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;\ntypedef vector<string> vs; typedef long double ld;\n\n//config\n#define MODE_DEBUG\n//#define INF 1<<30\n//#define EPS 1e-8\n//const ll MOD =100000007;\n\n//debug\n#ifdef NDEBUG\n#define DUMP(x)\n#define DUMPLN(x)\n#define DEBUG(x)\n#define DEBUGLN(x)\n#define LINE()\n#define LINELN()\n#define CHECK(exp,act)\n#define STOP(e)\n#else\n#define DUMP(x) cerr << #x << \" = \" << (x)\n#define DUMPLN(x) DUMP(x) <<endl\n#define DEBUG(x) DUMP(x) << LINE() << \" \" << __FILE__\n#define DEBUGLN(x) DEBUG(x)<<endl\n#define LINE() cerr<< \" (L\" << __LINE__ << \")\"\n#define LINELN() LINE()<<endl\n#define CHECK(exp,act) if(exp!=act){DUMPLN(exp);DEBUGLN(act);}\n#define STOP(e) CHECK(e,true);if(!(e)) exit(1);\n#endif\n\ntemplate<class T> inline string toString(const vector<T>& x) {\n\tstringstream ss;\n\tREP(i,x.size()){\n\t\tif(i!=0)ss<<\" \";\n\t\tss<< x[i];\n\t}\n\treturn ss.str();\n}\ntemplate<class T> inline string toString(const vector<vector<T> >& map) {\n\tstringstream ss;\n\tREP(i,map.size()){\n\t\tif(i!=0)ss<<BR;\n\t\tss<< toString(map[i]);\n\t}\n\treturn ss.str();\n}\ntemplate<class K,class V> string toString(map<K,V>& x) {\n\tstring res;stringstream ss;\n\tfor(auto& p:x)ss<< p.first<<\":\" << p.second<<\" \";\n\treturn ss.str();\n}\n\ntemplate<typename T,typename V> inline T mod(T v,V MOD){\n\treturn (v%MOD+MOD)%MOD;\n}\n\n#define nextInt(n) scanf(\"%d\",&n)\n#define nextLong(n) scanf(\"%lld\",&n)\n#define nextDouble(n) scanf(\"%lf\",&n)\n\nint INF=1<<28;\n\ntemplate<typename T>T gcd_positive(T a, T b) { return b == 0 ? a : gcd_positive(b,a%b); }\ntemplate<typename T>T gcd(T a, T b) { return gcd_positive(abs(a), abs(b)); }\ntemplate<typename T>T lcm(T a,T b){return a*b/gcd(a,b);}\n\nclass Main{\npublic:\t\n\tvoid run(){\n\t\tint N,M;cin >> N >> M;\n\n\t\tll A;cin >> A;REP(i,N-1){ll a;cin >> a; A=gcd(A,a);}\n\t\tll B;cin >> B;REP(i,M-1){ll b;cin >> b; B=lcm(B,b);}\n\n\t\tset<ll> ls;\n\n\t\tmap<ll,int> as;\n\t\tfor(int p=2;p*p<=A;p++){\n\t\t\tint c=0;\n\t\t\twhile(A%p==0){\n\t\t\t\tc++;A/=p;\n\t\t\t}\n\n\t\t\tif(c!=0){\n\t\t\t\tas[p]=c;ls.insert(p);\n\t\t\t}\n\t\t}\n\t\tif(A!=1) {\n\t\t\tas[A]=1;\n\t\t\tls.insert(A);\n\t\t}\n\n\t\tmap<ll,int> bs;\n\t\tfor(int p=2;p*p<=B;p++){\n\t\t\tint c=0;\n\t\t\twhile(B%p==0){\n\t\t\t\tc++;B/=p;\n\t\t\t}\n\n\t\t\tif(c!=0){\n\t\t\t\tbs[p]=c;ls.insert(p);\n\t\t\t}\n\t\t}\n\t\tif(B!=1) {\n\t\t\tbs[B]=1;\n\t\t\tls.insert(B);\n\t\t}\n\n\t\tll res =1;\n\t\tEACH(it,ls){\n\t\t\tres*=max(as[*it] -bs[*it]+1,0);\n\t\t}\n\t\tcout << res<<endl;\n\t}\n};\n int main(){\n \tcout <<fixed<<setprecision(15);\n\tios::sync_with_stdio(false);\n \tMain().run();\n \treturn 0;\n }", "accuracy": 0.28846153846153844, "time_ms": 30, "memory_kb": 1204, "score_of_the_acc": -0.0153, "final_rank": 19 }, { "submission_id": "aoj_1531_892947", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\nint N,M;\nll A[5];\nll B[5];\n\nll gcd(ll a,ll b){\n if(b==0)return a;\n return gcd(b,a%b);\n}\n\nint main(){\n\n cin>>N>>M;\n for(int i=0;i<N;i++)cin>>A[i];\n for(int i=0;i<M;i++)cin>>B[i];\n ll g=A[0];\n for(int i=1;i<N;i++)g=gcd(g,A[i]);\n vector<ll> divs;\n for(ll i=1;i*i<=g;i++){\n if(g%i==0){\n divs.push_back(i);\n if(i!=g/i)divs.push_back(g/i);\n }\n }\n ll cnt=0;\n for(ll d : divs){\n bool ok=true;\n for(int i=0;i<M;i++){\n if(d%B[i]!=0){\n ok=false;\n }\n }\n if(ok)cnt++;\n }\n cout<<cnt<<endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1348, "score_of_the_acc": -0.1082, "final_rank": 3 }, { "submission_id": "aoj_1531_891762", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <vector>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<n;++i)\n#define REP1(i,n) for(int i=1;i<=n;++i)\n#define ALL(c) (c).begin(),(c).end()\ntypedef long long ll;\nconst ll ccc=10000000;\nbool prime[ccc+1];\nvector<ll> pr;\nvoid makeprime(){\n\tll iii,jjj;\n\tfor(iii=0;iii<=ccc;iii++){\n\t\tprime[iii]=true;\n\t}\n\tprime[0]=false;\n\tprime[1]=false;\n\tfor(iii=2;iii*iii<=ccc;iii++){\n\t\tif(prime[iii]){\n\t\t\tfor(jjj=2*iii;jjj<=ccc;jjj+=iii){\n\t\t\t\tprime[jjj]=false;\n\t\t\t}\n\t\t}\n\t}\n\tfor(iii=2;iii<=ccc;iii++) if(prime[iii]) pr.push_back(iii);\n}\nll gcd(ll x,ll y){\n\tif(x<y) swap(x,y);\n\tll r=1;\n\twhile(r!=0){\n\t\tr=x%y;\n\t\tif(r==0) return y;\n\t\tx=y;\n\t\ty=r;\n\t}\n}\nint main(){\n\tmakeprime();\n\tll n,m,x,y;\n\tcin >> n >> m;\n\tcin >> x;\n\trep(i,n-1){\n\t\tll a;\n\t\tcin >> a;\n\t\tx=gcd(x,a);\n\t}\n\tcin >> y;\n\trep(j,m-1){\n\t\tll b,c;\n\t\tcin >> b;\n\t\tc=gcd(y,b);\n\t\tif((double)b/c>(double)((ll)1e+14)/y+1e-9){\n\t\t\tcout << 0 << endl;\n\t\t\treturn 0;\n\t\t}\n\t\ty*=(b/c);\n\t}\n\tif(x%y!=0){\n\t\tcout << 0 << endl;\n\t\treturn 0;\n\t}\n\tll p=x/y,ans=1;\n\trep(i,pr.size()){\n\t\tint cnt=1;\n\t\twhile(p%pr[i]==0){\n\t\t\tcnt++;\n\t\t\tp/=pr[i];\n\t\t}\n\t\tans*=cnt;\n\t\tif(p==1) break;\n\t}\n\tif(p>1) ans*=2;\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 21196, "score_of_the_acc": -1.0857, "final_rank": 13 }, { "submission_id": "aoj_1531_890618", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <map>\n#include <vector>\n#include <set>\n#include <cmath>\n#include <string>\n#include <algorithm>\n#include <queue>\n\nusing namespace std;\n\n#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)\n#define rep(i,b) loop(i,0,b)\n#define all(c) (c).begin(), (c).end()\n\ntypedef unsigned long long ull;\ntypedef vector<int> vi;\ntypedef unsigned long long ull;\n\null __gcd(ull x, ull y){\n if (y == 0ULL) return x;\n else return __gcd(y, x%y);\n}\n\nint main(){\n ull n, m; cin >> n >> m;\n vector<ull> A(n), B(m);\n\n rep(i, A.size()) cin >> A[i];\n rep(i, B.size()) cin >> B[i];\n\n ull ga = A[0];\n loop(i, 1, A.size()) ga = __gcd(ga, A[i]);\n\n ull lb = B[0];\n loop(i, 1, B.size()) lb = lb / __gcd(lb, B[i])*B[i];\n \n ull ans;\n if (ga%lb != 0) ans = 0; \n else {\n ull x = ga / lb;\n\n int end = x;\n\n ans = 1;\n loop(i, 2, end){\n ull e = 0;\n if (x%i == 0){\n while (x%i == 0){\n e++;\n x /= i;\n }\n }\n ans *= e + 1;\n if (x == 1) break;\n }\n }\n\n cout << ans << endl;\n}", "accuracy": 0.36538461538461536, "time_ms": 80, "memory_kb": 1184, "score_of_the_acc": -0.0857, "final_rank": 18 }, { "submission_id": "aoj_1531_890455", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <map>\n#include <set>\n#include <queue>\n#include <stack>\n#include <climits>\n#include <sstream>\n#include <functional>\n#include <complex>\n\nusing namespace std;\n\n#define len(array) (sizeof (array) / sizeof *(array))\n#define rep(i, s, e) for(int i = s;i < e;i++)\n#define Rep(i, e) for(int i = 0;i < e;i++)\n#define rrep(i, e, s) for(int i = e;s <= i;i--)\n#define Rrep(i, e) for(int i = e;0 <= i;i--)\n#define mrep(i, e, t1, t2) for(map<t1, t2>::iterator i = e.begin(); i != e.end(); i++)\n#define vrange(v) v.begin(), v.end()\n#define vrrange(v) v.rbegin(), v.rend()\n#define vsort(v) sort(vrange(v))\n#define vrsort(v) sort(vrrange(v))\n#define arange(a) a, a + len(a)\n#define asort(a) sort(arange(a))\n#define arsort(a, t) sort(arange(a), greater<t>())\n#define afill(a, v) fill(arange(a), v)\n#define afill2(a, v, t) fill((t *)a, (t *)(a + len(a)), v)\n#define fmax(a, b) (a < b? b : a)\n#define fmin(a, b) (a > b? b : a)\n#define fabs(a) (a < 0? -a : a)\n#define pb push_back\n#define rg(e, s, t) s <= e && e < t\n#define PQDecl(name, tp) priority_queue< tp, vector<tp>, greater<tp> > name;\n//#define X real()\n//#define Y imag()\n//typedef unsigned int ui;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n//typedef complex<double> p;\nconst int INF = (int)2e9;\nconst int MOD = (int)1e9 + 7;\nconst double EPS = 1e-10;\n//const int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};\n//const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};\n//const ll weight[] = {1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13};\n#define MAX_N 1000\n\n#define MAX_PRIMES 10000002\nbool bPrimes[MAX_PRIMES];\nvector<int> primes;\n\nvoid makePrimes(){\n afill(bPrimes, true);\n bPrimes[0] = bPrimes[1] = false;\n rep(i, 2, MAX_PRIMES){\n\tif(bPrimes[i]){\n\t primes.push_back(i);\n\t for(int j = 2*i; j < MAX_PRIMES; j += i) bPrimes[j] = false;\n\t}\n }\n}\n\nll gcd(ll a, ll b) {\n if( a < b ) swap( a, b );\n return b != 0 ? gcd(b, a % b) : a;\n}\nll lcm(ll a, ll b) {\n return a * b / gcd(a, b);\n}\n\nll lpow(ll n, ll p){\n ll ans = 1, ln = n;\n if(p <= 0) return 1;\n while(p != 0){\n\tif((p & 1) == 1) ans = ans*ln;\n\tln = ln * ln;\n\tp = p >> 1;\n }\n return ans;\n}\n\nvoid doIt(){\n makePrimes();\n // cout << primes.size() << endl;\n int n, m, p[700000], ptmp[700000];\n ll a, b, g = -1;\n map<ll, int> mmap;\n cin >> n >> m;\n Rep(i, n){\n cin >> a;\n if(g == -1) g = a;\n else g = gcd(g, a);\n }\n\n afill(p, 0);\n Rep(i, m){\n cin >> b;\n afill(ptmp, 0);\n Rep(j, primes.size()){\n while(b % primes[j] == 0){\n b /= primes[j];\n ptmp[j]++;\n }\n p[j] = max(p[j], ptmp[j]);\n }\n if(b != 1) mmap[b]++;\n }\n bool bOK = true;\n ll ans = 1;\n Rep(i, primes.size()){\n int count = 0;\n while(g % primes[i] == 0){\n g /= primes[i];\n count++;\n }\n int diff = count - p[i];\n if(diff >= 0) ans *= diff + 1;\n else bOK = false;\n // if(diff) printf(\"diff = %d, i = %d\\n\", diff, i);\n }\n if(g != 1){\n if(mmap.count(g) != 0){\n mmap[g] = 0;\n }\n else{\n ans *= 2;\n }\n }\n mrep(it, mmap, ll, int){\n if((*it).second){\n bOK = false;\n }\n }\n // printf(\"bOK = %d\\n\", bOK);\n if(bOK){\n cout << ans << endl;\n }\n else{\n cout << 0 << endl;\n }\n}\n\nint main() {\n doIt();\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 20464, "score_of_the_acc": -1.1349, "final_rank": 15 }, { "submission_id": "aoj_1531_890218", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tint n, m;\n\tcin >> n >> m;\n\tvector<ll> a(n), b(m);\n\tfor(int i = 0; i < n; ++i){ cin >> a[i]; }\n\tfor(int i = 0; i < m; ++i){ cin >> b[i]; }\n\tll x = a[0], y = 1;\n\tfor(int i = 0; i < n; ++i){ x = __gcd(x, a[i]); }\n\tfor(int i = 0; i < m; ++i){\n\t\tconst ll g = __gcd(y, b[i]);\n\t\tconst ll t = b[i] / g;\n\t\tif(__int128(t) * y > x){\n\t\t\ty = -1;\n\t\t\tbreak;\n\t\t}\n\t\ty *= t;\n\t}\n\tif(y < 0){\n\t\tcout << 0 << endl;\n\t}else{\n int answer = 0;\n for(ll i = 1; i * i <= x; ++i){\n if(x % i == 0){\n if(i % y == 0){ ++answer; }\n if(i * i != x && (x / i) % y == 0){ ++answer; }\n }\n }\n\t\tcout << answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 1204, "score_of_the_acc": -0.101, "final_rank": 2 }, { "submission_id": "aoj_1531_890213", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tint n, m;\n\tcin >> n >> m;\n\tvector<ll> a(n), b(m);\n\tfor(int i = 0; i < n; ++i){ cin >> a[i]; }\n\tfor(int i = 0; i < m; ++i){ cin >> b[i]; }\n\tll x = a[0], y = 1;\n\tfor(int i = 0; i < n; ++i){ x = __gcd(x, a[i]); }\n\tfor(int i = 0; i < m; ++i){\n\t\tconst ll g = __gcd(y, b[i]);\n\t\tconst ll t = b[i] / g;\n\t\tif(__int128(t) * y > x){\n\t\t\ty = -1;\n\t\t\tbreak;\n\t\t}\n\t\ty *= t;\n\t}\n\tif(y < 0){\n\t\tcout << 0 << endl;\n\t}else{\n\t\tll l = 0, r = 1000000000;\n while(l < r){\n const ll c = l + (r - l) / 2;\n if(c * c < x){\n l = c + 1;\n }else{\n r = c;\n }\n }\n int answer = 0;\n for(ll i = 1; i <= l; ++i){\n if(x % i == 0){\n if(i % y == 0){ ++answer; }\n if(i * i != x && x / i % y == 0){ ++answer; }\n }\n }\n\t\tcout << answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.9038461538461539, "time_ms": 90, "memory_kb": 1204, "score_of_the_acc": -0.101, "final_rank": 17 }, { "submission_id": "aoj_1531_890207", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tint n, m;\n\tcin >> n >> m;\n\tvector<ll> a(n), b(m);\n\tfor(int i = 0; i < n; ++i){ cin >> a[i]; }\n\tfor(int i = 0; i < m; ++i){ cin >> b[i]; }\n\tll x = a[0], y = 1;\n\tfor(int i = 0; i < n; ++i){ x = __gcd(x, a[i]); }\n\tfor(int i = 0; i < m; ++i){\n\t\tconst ll g = __gcd(y, b[i]);\n\t\tconst ll t = b[i] / g;\n\t\tif(y > x / t){\n\t\t\ty = -1;\n\t\t\tbreak;\n\t\t}else{\n\t\t\ty = y * t;\n\t\t}\n\t}\n\tif(y < 0 || x % y != 0){\n\t\tcout << 0 << endl;\n\t}else{\n\t\tll l = 0, r = 1000000000;\n\t\twhile(l < r){\n\t\t\tconst ll c = l + (r - l) / 2;\n\t\t\tif(c * c < x){\n\t\t\t\tl = c + 1;\n\t\t\t}else{\n\t\t\t\tr = c;\n\t\t\t}\n\t\t}\n\t\tint answer = 0;\n\t\tfor(ll i = 1; i <= l; ++i){\n\t\t\tif(x % i == 0){\n\t\t\t\tif(i % y == 0){ ++answer; }\n\t\t\t\tif(i * i != x && x / i % y == 0){ ++answer; }\n\t\t\t}\n\t\t}\n\t\tcout << answer << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.9038461538461539, "time_ms": 80, "memory_kb": 1208, "score_of_the_acc": -0.0869, "final_rank": 16 }, { "submission_id": "aoj_1531_890181", "code_snippet": "#include <iostream>\n#include <map>\n#include <set>\nusing namespace std;\n\nlong long A[10],B[10];\nint main(){\n\tint N,M;\n\tcin >> N >> M;\n\tfor(int i = 0 ; i < N ; i++) cin >> A[i];\n\tfor(int i = 0 ; i < M ; i++) cin >> B[i];\n\n\tmap<long long,int> o;\n\tfor(int i = 0 ; i < N ; i++){\n\t\tfor(long long j = 1 ; j*j <= A[i] ; j++){\n\t\t\t\tif( A[i] % j == 0 ){\n\t\t\t\tlong long a = A[i] / j;\n\t\t\t\tlong long b = j;\n\t\t\t\tint af = 1;\n\t\t\t\tint bf = 1;\n\t\t\t\tfor(int k = 0 ; k < M ; k++){\n\t\t\t\t\tif( a%B[k] != 0 ) af = 0;\n\t\t\t\t\tif( b%B[k] != 0 ) bf = 0;\n\t\t\t\t}\n\t\t\t\tif( af ) o[a] |= (1<<i);\n\t\t\t\tif( bf ) o[b] |= (1<<i);\n\t\t\t}\n\t\t}\n\t}\n\tint ans = 0;\n\tfor( map<long long,int>::iterator it = o.begin(); it != o.end(); ++it){\n\t\tif( it->second == (1<<N)-1 ) ans++;\n\t}\n\tcout << ans << endl;\n\t\n\t\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 1844, "score_of_the_acc": -0.6044, "final_rank": 10 }, { "submission_id": "aoj_1531_890178", "code_snippet": "#include <iostream>\n#include <map>\n#include <set>\nusing namespace std;\n\nlong long A[10],B[10];\nint main(){\n\tint N,M;\n\tcin >> N >> M;\n\tfor(int i = 0 ; i < N ; i++) cin >> A[i];\n\tfor(int i = 0 ; i < M ; i++) cin >> B[i];\n\n\tmap<long long,int> o;\n\tfor(int i = 0 ; i < N ; i++){\n\t\tfor(long long j = 1 ; j*j <= A[i] ; j++){\n\t\t\t\tif( A[i] % j == 0 ){\n\t\t\t\tint a = A[i] / j;\n\t\t\t\tint b = j;\n\t\t\t\tint af = 1;\n\t\t\t\tint bf = 1;\n\t\t\t\tfor(int k = 0 ; k < M ; k++){\n\t\t\t\t\tif( a%B[k] != 0 ) af = 0;\n\t\t\t\t\tif( b%B[k] != 0 ) bf = 0;\n\t\t\t\t}\n\t\t\t\tif( af ) o[a] |= (1<<i);\n\t\t\t\tif( bf ) o[b] |= (1<<i);\n\t\t\t}\n\t\t}\n\t}\n\tint ans = 0;\n\tfor( map<long long,int>::iterator it = o.begin(); it != o.end(); ++it){\n\t\tif( it->second == (1<<N)-1 ) ans++;\n\t}\n\tcout << ans << endl;\n\t\n\t\n}", "accuracy": 0.23076923076923078, "time_ms": 60, "memory_kb": 1208, "score_of_the_acc": -0.0583, "final_rank": 20 }, { "submission_id": "aoj_1531_890135", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\n#ifdef LOCAL\n#include \"local.h\"\n#endif\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\ntemplate <class T>\nvector<T> divisors(T n)\n{\n vector<T> res;\n for (T i = 1; i * i <= n; ++i)\n {\n if (n % i == 0)\n {\n res.push_back(i);\n if (i != n / i)\n res.push_back(n / i);\n }\n }\n sort(res.begin(), res.end());\n return res;\n}\n\nll gcd(ll a, ll b) { return b ? __gcd(b, a % b) : a; }\nint main()\n{\n int n, m;\n cin >> n >> m;\n vector<ll> a(n), b(m);\n rep(i, n)\n cin >> a[i];\n rep(i, m)\n cin >> b[i];\n\n vector<ll> cand;\n rep(i, a.size())\n {\n vector<ll> divs = divisors(a[i]);\n rep(di, divs.size())\n {\n ll div = divs[di];\n bool ok = true;\n rep(j, a.size())\n {\n if (a[j] % div != 0)\n {\n ok = false;\n break;\n }\n }\n if (ok)\n cand.push_back(div);\n }\n }\n uniq(cand);\n\n int res = 0;\n rep(ci, cand.size())\n {\n ll x = cand[ci];\n bool ok = true;\n rep(i, b.size())\n {\n if (x % b[i] != 0)\n {\n ok = false;\n break;\n }\n }\n if (ok)\n ++res;\n }\n cout << res << endl;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 1376, "score_of_the_acc": -0.5525, "final_rank": 9 }, { "submission_id": "aoj_1531_890086", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <set>\n#include <map>\n#include <queue>\n#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <cctype>\n#include <cassert>\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\n#if defined(_MSC_VER) || __cplusplus > 199711L\n#define aut(r,v) auto r = (v)\n#else\n#define aut(r,v) typeof(v) r = (v)\n#endif\n#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)\n#define all(o) (o).begin(), (o).end()\n#define pb(x) push_back(x)\n#define mp(x,y) make_pair((x),(y))\n#define mset(m,v) memset(m,v,sizeof(m))\n#define INF 0x3f3f3f3f\n#define INFL 0x3f3f3f3f3f3f3f3fLL\nusing namespace std;\ntypedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;\ntypedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;\ntypedef vector<string> vs; typedef long double ld;\ntemplate<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }\ntemplate<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }\n\nvector<bool> isprime;\nvector<int> primes;\nvoid sieve(int n){\n\tif((int)isprime.size() >= n+1) return;\n\tisprime.assign(n+1, true);\n\tisprime[0] = isprime[1] = false;\n\tint sqrtn = (int)(sqrt(n * 1.) + .5);\n\tfor(int i = 2; i <= sqrtn; i ++) if(isprime[i]) {\n\t\tfor(int j = i * i; j <= n; j += i)\n\t\t\tisprime[j] = false;\n\t}\n\tprimes.clear();\n\tfor(int i = 2; i <= n; i ++) if(isprime[i])\n\t\tprimes.push_back(i);\n}\n\ntypedef vector<pair<ll,int> > Factors;\nvoid primeFactors(ll x, Factors &out_v) {\n\tout_v.clear();\n\tint sqrtx = (int)(sqrt(x*1.) + 10.5);\n\tsieve(sqrtx);\n\teach(p, primes) {\n\t\tif(*p > sqrtx) break;\n\t\tif(x % *p == 0) {\n\t\t\tint t = 1;\n\t\t\tx /= *p;\n\t\t\twhile(x % *p == 0) {\n\t\t\t\tt ++;\n\t\t\t\tx /= *p;\n\t\t\t}\n\t\t\tout_v.push_back(make_pair(*p, t));\n\t\t}\n\t}\n\tif(x != 1) out_v.push_back(make_pair(x, 1));\n}\n\ntemplate<typename Op> inline Factors combineFactors(Op op, int id, const Factors &a, const Factors &b) {\n\tFactors c;\n\tint an = a.size(), bn = b.size();\n\tint ai = 0, bi = 0;\n\twhile(ai < an || bi < bn) {\n\t\ttypename Factors::value_type::first_type p; int q;\n\t\tif(ai < an && (bi >= bn || a[ai].first < b[bi].first)) {\n\t\t\tp = a[ai].first, q = op(a[ai].second, id);\n\t\t\tai ++;\n\t\t}else if(bi < bn && (ai >= an || a[ai].first > b[bi].first)) {\n\t\t\tp = b[bi].first, q = op(id, b[bi].second);\n\t\t\tbi ++;\n\t\t}else {\n\t\t\tp = a[ai].first, q = op(a[ai].second, b[bi].second);\n\t\t\tai ++, bi ++;\n\t\t}\n\t\tif(q != 0) c.push_back(mp(p, q));\n\t}\n\treturn c;\n}\n\nFactors gcdFactors(const Factors &a, const Factors &b) {\n\treturn combineFactors<const int&(*)(const int&,const int&)>(std::min<int>, 0, a, b);\n}\nFactors lcmFactors(const Factors &a, const Factors &b) {\n\treturn combineFactors<const int&(*)(const int&,const int&)>(std::max<int>, 0, a, b);\n}\n\nint main() {\n\tsieve((int)1e7+20);\n\tint N, M;\n\tscanf(\"%d%d\", &N, &M);\n\tFactors g, l;\n\trep(i, N) {\n\t\tll A;\n\t\tscanf(\"%lld\", &A);\n\t\tFactors fs;\n\t\tprimeFactors(A, fs);\n\t\tif(i == 0) g = fs;\n\t\telse g = gcdFactors(g, fs);\n\t}\n\trep(i, M) {\n\t\tll B;\n\t\tscanf(\"%lld\", &B);\n\t\tFactors fs;\n\t\tprimeFactors(B, fs);\n\t\tl = lcmFactors(l, fs);\n\t}\n\tmap<ll,int> ga(all(g)), la(all(l));\n\tset<ll> ps;\n\teach(i, ga) ps.insert(i->first);\n\teach(i, la) ps.insert(i->first);\n\tll ans = 1;\n\teach(i, ps) {\n\t\tll upper = ga[*i], lower = la[*i];\n\t\tif(lower > upper) ans = 0;\n\t\telse ans *= upper - lower + 1;\n\t}\n\tprintf(\"%lld\\n\", ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 8404, "score_of_the_acc": -0.4894, "final_rank": 7 } ]
aoj_1543_cpp
Problem C: Changing Grids Background A君とB君は、『Changing Grids』というゲームに熱中している。このゲームは2人用で、プレイヤー1がステージを構成し、プレイヤー2がそのステージに挑戦しゴールを目指すというものである。 今、A君とB君はこのゲームを何度かプレイしているが、A君の連勝でB君は1度も勝つことができていない。そこであなたは、B君にこのゲームを攻略するためのヒントを教えてあげることにした。 Problem 時刻 T 0 = 0における縦 H ×横 W の大きさの二次元グリッドの状態が Area 0 として与えられる。次に、このグリッドの状態は時刻 T i において、状態 Area i に切り替わる。この切り替わる過程は N 回繰り返される。初期状態のグリッドにはスタートの位置'S'とゴールの位置'G'が与えられる。いずれかのグリッドにおいてゴールへ辿り着ける場合は、そのときの最小歩数を出力し、ゴールへ辿り着けない場合は、'-1'を出力せよ。なお、以下の条件も満たす必要がある。 Area i は以下の要素で構成されている。 ‘.’は何もなく移動可能なマス ’#’は障害物であり、移動不可能なマス 'S'はスタート位置を表すマス 'G'はゴール位置を表すマス プレイヤーは現在いるマスの隣接している上下左右のいずれか1マスに移動する、または現在いるマスに留まるのに1秒かかる。ただし、障害物のマスやグリッドの範囲外には移動できない。 歩数は、現在プレイヤーがいるマスから上下左右のマスへ1マス進むと1増加する。その場に留まる場合には増加しない。 全てのグリッドの大きさは縦 H ×横 W である。 1マス移動、またはその場に留まった後にグリッドが切り替わる際、次のグリッドにおいて障害物が存在しないマスであれば今のグリッドの状態に関わらず移動が可能である。 全てのグリッドにおいて、初期のグリッドに与えられたゴール位置に到達した場合ゴールとみなす。 Input 入力は以下の形式で与えられる。 H W Area 0 N T 1 Area 1 T 2 Area 2 . . T N Area N 1行目に2つの整数 H,W が空白区切りで与えられる。これは、それぞれ二次元グリッドの縦と横の大きさを表す。2行目から H +1行目までの各行に初期状態の二次元グリッドの状態が与えられる。 H +2行目に整数 N が与えられる。これは、二次元グリッドの変化する回数を表す。 H +3行目以降に N 個の二次元グリッドの切り替わる時刻 T i とその状態が与えられる。ただし、 T i は全て整数である。 Constraints 入力は以下の条件を満たす。 2 ≤ H,W ≤ 20 1 ≤ N ≤ 15 1 ≤ T i ≤ 200 ( T 1 < T 2 < ... < T N ) スタート位置'S'とゴール位置'G'はそれぞれ初期の二次元グリッドに1つだけ存在する。 Output スタートからゴールへ到達するための最小の歩数を出力せよ。ただし、ゴールに到達できない場合は'-1'を出力せよ。 Sample Input 1 2 2 S. .G 1 3 ## ## Sample Output 1 2 1番目のグリッドに切り替わる時刻 T 1 は3であり、時間内にプレイヤーはスタートからゴールまでの最短歩数が2歩で辿り着くことが可能なので2を出力する。 Sample Input 2 2 2 S. .G 1 2 ## ## Sample Output 2 -1 Sample Input 3 2 3 S## ##G 4 2 ### .## 3 ### #.# 5 ### ##. 7 ### ### Sample Output 3 3 Sample Input 4 4 3 S.. ... .G. ... 4 2 ### #.# ### #.# 4 ### #.. #.. ### 6 ### #.# ### #.. 8 ### #.. #.. ### Sample Output 4 3 Sample Input 5 3 3 S## ### ##G 1 1 ... ... ... Sample Output 5 4
[ { "submission_id": "aoj_1543_3577848", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n\n#define INF INT_MAX/3\n\nstruct state{\n int i,j,t,d;\n};\nbool operator<(const state& a,const state& b){\n return a.d > b.d;\n}\n\nint di[]={-1,0,1,0,0};\nint dj[]={0,-1,0,1,0};\n\nint H,W,N;\nvector<vector<string> > bs;\nint si,sj,ti,tj;\nint dist[22][22][201];\n\nbool can(int t,int i,int j){\n if(bs[t][i][j]!='#')return true;\n else return false;\n}\n\nint main(){\n cin>>H>>W;\n bs.resize(201);\n\n int pret=0;\n vector<string> preb(H);\n rep(j,H)cin>>preb[j];\n rep(i,H)rep(j,W){\n if(preb[i][j]=='S'){\n si=i; sj=j;\n }\n if(preb[i][j]=='G'){\n ti=i; tj=j;\n }\n }\n\n cin>>N;\n rep(i,N){\n int T;\n vector<string> input(H);\n cin>>T;\n rep(j,H)cin>>input[j];\n repl(j,pret,T){\n bs[j]=preb;\n }\n preb=input;\n pret=T;\n }\n repl(j,pret,201)bs[j]=preb;\n\n rep(i,H)rep(j,W)rep(t,201)dist[i][j][t]=INF;\n priority_queue<state> que;\n que.push((state){si,sj,0,0});\n while(que.size()){\n state s=que.top(); que.pop();\n if(dist[s.i][s.j][s.t]!=INF)continue;\n dist[s.i][s.j][s.t]=s.d;\n if(s.i==ti&&s.j==tj){\n cout<<dist[s.i][s.j][s.t]<<endl;\n return 0;\n }\n rep(dir,5){\n int ni=s.i+di[dir],nj=s.j+dj[dir],nt=min(200,s.t+1);\n\n if(ni<0||ni>=H||nj<0||nj>=W||!can(nt,ni,nj))continue;\n\n {\n if(dir==4) que.push((state){ni,nj,nt,s.d});\n else que.push((state){ni,nj,nt,s.d+1});\n }\n }\n }\n cout<<-1<<endl;\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3876, "score_of_the_acc": -0.8847, "final_rank": 2 }, { "submission_id": "aoj_1543_1080754", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=a;i<b;++i)\n#define allof(a) a.begin(),a.end()\n#define minit(a,b) memset(a,b,sizeof(a))\n#define size_of(a) (int)a.size()\n\ntypedef long long lint;\ntypedef pair<int, int> pii;\n\nstruct State {\n\tint x, y, t, step;\n\tState(int _x, int _y, int _t, int _step) : x(_x), y(_y), t(_t), step(_step) {}\n};\nbool operator > (const State& a, const State& b) { return a.step > b.step; }\n\nint H, W, N, T[20];\nstring grid[20][25];\nint steps[20][25][25];\nint sx, sy, gx, gy;\n\nint dx[4] = {-1,0,1,0};\nint dy[4] = {0,-1,0,1};\n\nvoid solve() {\n\tstring last_g = \"\";\n\tfor_(i,0,W) last_g += \"#\";\n\tfor_(i,0,H) grid[N + 1][i] = last_g;\n\tT[0] = 0; T[N + 1] = 1000;\n\tfor_(i,0,20) for_(j,0,25) fill(steps[i][j], steps[i][j] + 25, (int)1e9);\n\tsteps[0][sy][sx] = 0;\n\t\n\tfor_(i,0, N + 1) {\n\t\tint limit = T[i + 1] - T[i];\n\t\t\n\t\tpriority_queue<State, vector<State>, greater<State> > q;\n\t\tfor_(y,0,H) for_(x,0,W) {\n\t\t\tif (steps[i][y][x] < (int)1e9) q.push(State(x, y, 0, steps[i][y][x]));\n\t\t}\n\t\t\n\t\twhile (!q.empty()) {\n\t\t\tState s = q.top(); q.pop();\n\t\t\t\n\t\t\tint cx = s.x, cy = s.y, t = s.t, step = s.step;\n\t\t\t\n\t\t\tif (cx == gx && cy == gy) {\n\t\t\t\tcout << step << endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t\n\t\t\tif (t >= limit) continue;\n\t\t\t\n\t\t\tif (t < limit - 1 && steps[i][cy][cx] >= step) {\n\t\t\t\tsteps[i][cy][cx] = step;\n\t\t\t\tq.push(State(cx, cy, t + 1, step));\n\t\t\t}\n\t\t\t\n\t\t\tif (t == limit - 1 && grid[i + 1][cy][cx] != '#' && steps[i + 1][cy][cx] > step) {\n\t\t\t\tsteps[i + 1][cy][cx] = step;\n\t\t\t} \n\t\t\t\n\t\t\tfor_(d,0,4) {\n\t\t\t\tint nx = cx + dx[d], ny = cy + dy[d];\n\t\t\t\tif (!(0 <= nx && nx < W && 0 <= ny && ny < H)) continue;\n\t\t\t\t\n\t\t\t\tif (t < limit - 1 && grid[i][ny][nx] == '#') continue;\t\t\t\t\n\t\t\t\tif (t < limit - 1 && steps[i][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i][ny][nx] = step + 1;\n\t\t\t\t\tq.push(State(nx, ny, t + 1, step + 1));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][ny][nx] == '#') continue;\n\t\t\t\tif (t == limit - 1 && steps[i + 1][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i + 1][ny][nx] = step + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tcout << -1 << endl;\n}\n\nint main() {\n\tcin >> H >> W;\n\tfor_(i,0,H) {\n\t\tcin >> grid[0][i];\n\t\tfor_(j,0,W) {\n\t\t\tif (grid[0][i][j] == 'S') {\n\t\t\t\tsx = j; sy = i;\n\t\t\t}\n\t\t\tif (grid[0][i][j] == 'G') {\n\t\t\t\tgx = j; gy = i;\n\t\t\t}\n\t\t}\n\t}\n\tcin >> N;\n\tfor_(i,1,N + 1) {\n\t\tcin >> T[i];\n\t\tfor_(j,0,H) cin >> grid[i][j];\n\t}\n\tsolve();\n\treturn 0;\n}", "accuracy": 0.8387096774193549, "time_ms": 10, "memory_kb": 1292, "score_of_the_acc": -0.038, "final_rank": 8 }, { "submission_id": "aoj_1543_1080749", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=a;i<b;++i)\n#define allof(a) a.begin(),a.end()\n#define minit(a,b) memset(a,b,sizeof(a))\n#define size_of(a) (int)a.size()\n\ntypedef long long lint;\ntypedef pair<int, int> pii;\n\nstruct State {\n\tint x, y, t, step;\n\tState(int _x, int _y, int _t, int _step) : x(_x), y(_y), t(_t), step(_step) {}\n};\nbool operator > (const State& a, const State& b) { return a.step > b.step; }\n\nint H, W, N, T[20];\nstring grid[20][25];\nint steps[20][25][25];\nint sx, sy, gx, gy;\n\nint dx[4] = {-1,0,1,0};\nint dy[4] = {0,-1,0,1};\n\nvoid solve() {\n\tstring last_g = \"\";\n\tfor_(i,0,W) last_g += \"#\";\n\tfor_(i,0,H) grid[N + 1][i] = last_g;\n\tT[0] = 0; T[N + 1] = 1000;\n\tfor_(i,0,20) for_(j,0,25) fill(steps[i][j], steps[i][j] + 25, (int)1e9);\n\tsteps[0][sy][sx] = 0;\n\t\n\tfor_(i,0, N + 1) {\n\t\tint limit = T[i + 1] - T[i];\n\t\t\n\t\tpriority_queue<State, vector<State>, greater<State> > q;\n\t\tfor_(y,0,H) for_(x,0,W) {\n\t\t\tif (steps[i][y][x] < (int)1e9) q.push(State(x, y, 0, steps[i][y][x]));\n\t\t}\n\t\t\n\t\twhile (!q.empty()) {\n\t\t\tState s = q.top(); q.pop();\n\t\t\t\n\t\t\tint cx = s.x, cy = s.y, t = s.t, step = s.step;\n\t\t\t\n\t\t\tif (cx == gx && cy == gy) {\n\t\t\t\tcout << step << endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t\n\t\t\tif (t >= limit) continue;\n\t\t\t\n\t\t\tif (steps[i][cy][cx] >= step) {\n\t\t\t\tif (t < limit - 1 && steps[i][cy][cx] >= step) {\n\t\t\t\t\tsteps[i][cy][cx] = step;\n\t\t\t\t\tq.push(State(cx, cy, t + 1, step));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][cy][cx] != '#' && steps[i + 1][cy][cx] > step) {\n\t\t\t\t\tsteps[i + 1][cy][cx] = step;\n\t\t\t\t} \n\t\t\t}\n\t\t\t\n\t\t\tfor_(d,0,4) {\n\t\t\t\tint nx = cx + dx[d], ny = cy + dy[d];\n\t\t\t\tif (!(0 <= nx && nx < W && 0 <= ny && ny < H)) continue;\n\t\t\t\t\n\t\t\t\tif (t < limit - 1 && grid[i][ny][nx] == '#') continue;\t\t\t\t\n\t\t\t\tif (t < limit - 1 && steps[i][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i][ny][nx] = step + 1;\n\t\t\t\t\tq.push(State(nx, ny, t + 1, step + 1));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][ny][nx] == '#') continue;\n\t\t\t\tif (t == limit - 1 && steps[i + 1][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i + 1][ny][nx] = step + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tcout << -1 << endl;\n}\n\nint main() {\n\tcin >> H >> W;\n\tfor_(i,0,H) {\n\t\tcin >> grid[0][i];\n\t\tfor_(j,0,W) {\n\t\t\tif (grid[0][i][j] == 'S') {\n\t\t\t\tsx = j; sy = i;\n\t\t\t}\n\t\t\tif (grid[0][i][j] == 'G') {\n\t\t\t\tgx = j; gy = i;\n\t\t\t}\n\t\t}\n\t}\n\tcin >> N;\n\tfor_(i,1,N + 1) {\n\t\tcin >> T[i];\n\t\tfor_(j,0,H) cin >> grid[i][j];\n\t}\n\tsolve();\n\treturn 0;\n}", "accuracy": 0.8387096774193549, "time_ms": 10, "memory_kb": 1292, "score_of_the_acc": -0.038, "final_rank": 8 }, { "submission_id": "aoj_1543_1080743", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=a;i<b;++i)\n#define allof(a) a.begin(),a.end()\n#define minit(a,b) memset(a,b,sizeof(a))\n#define size_of(a) (int)a.size()\n\ntypedef long long lint;\ntypedef pair<int, int> pii;\n\nstruct State {\n\tint x, y, t, step;\n\tState(int _x, int _y, int _t, int _step) : x(_x), y(_y), t(_t), step(_step) {}\n};\nbool operator > (const State& a, const State& b) { return a.step > b.step; }\n\nint H, W, N, T[20];\nstring grid[20][25];\nint steps[20][25][25];\nint sx, sy, gx, gy;\n\nint dx[4] = {-1,0,1,0};\nint dy[4] = {0,-1,0,1};\n\nvoid solve() {\n\tstring last_g = \"\";\n\tfor_(i,0,W) last_g += \".\";\n\tfor_(i,0,H) grid[N + 1][i] = last_g;\n\tT[0] = 0; T[N + 1] = 1000;\n\tfor_(i,0,20) for_(j,0,25) fill(steps[i][j], steps[i][j] + 25, (int)1e9);\n\tsteps[0][sy][sx] = 0;\n\t\n\tfor_(i,0, N + 1) {\n\t\tint limit = T[i + 1] - T[i];\n\t\t\n\t\tpriority_queue<State, vector<State>, greater<State> > q;\n\t\tfor_(y,0,H) for_(x,0,W) {\n\t\t\tif (steps[i][y][x] < (int)1e9) q.push(State(x, y, 0, steps[i][y][x]));\n\t\t}\n\t\t\n\t\twhile (!q.empty()) {\n\t\t\tState s = q.top(); q.pop();\n\t\t\t\n\t\t\tint cx = s.x, cy = s.y, t = s.t, step = s.step;\n\t\t\t\n\t\t\tif (cx == gx && cy == gy) {\n\t\t\t\tcout << step << endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t\n\t\t\tif (t >= limit) continue;\n\t\t\t\n\t\t\tif (steps[i][cy][cx] >= step) {\n\t\t\t\tif (t < limit - 1 && steps[i][cy][cx] >= step) {\n\t\t\t\t\tsteps[i][cy][cx] = step;\n\t\t\t\t\tq.push(State(cx, cy, t + 1, step));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][cy][cx] != '#' && steps[i + 1][cy][cx] > step) {\n\t\t\t\t\tsteps[i + 1][cy][cx] = step;\n\t\t\t\t} \n\t\t\t}\n\t\t\t\n\t\t\tfor_(d,0,4) {\n\t\t\t\tint nx = cx + dx[d], ny = cy + dy[d];\n\t\t\t\tif (!(0 <= nx && nx < W && 0 <= ny && ny < H)) continue;\n\t\t\t\t\n\t\t\t\tif (t < limit - 1 && grid[i][ny][nx] == '#') continue;\t\t\t\t\n\t\t\t\tif (t < limit - 1 && steps[i][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i][ny][nx] = step + 1;\n\t\t\t\t\tq.push(State(nx, ny, t + 1, step + 1));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][ny][nx] == '#') continue;\n\t\t\t\tif (t == limit - 1 && steps[i + 1][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i + 1][ny][nx] = step + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tcout << -1 << endl;\n}\n\nint main() {\n\tcin >> H >> W;\n\tfor_(i,0,H) {\n\t\tcin >> grid[0][i];\n\t\tfor_(j,0,W) {\n\t\t\tif (grid[0][i][j] == 'S') {\n\t\t\t\tsx = j; sy = i;\n\t\t\t}\n\t\t\tif (grid[0][i][j] == 'G') {\n\t\t\t\tgx = j; gy = i;\n\t\t\t}\n\t\t}\n\t}\n\tcin >> N;\n\tfor_(i,1,N + 1) {\n\t\tcin >> T[i];\n\t\tfor_(j,0,H) cin >> grid[i][j];\n\t}\n\tsolve();\n\treturn 0;\n}", "accuracy": 0.8387096774193549, "time_ms": 10, "memory_kb": 1292, "score_of_the_acc": -0.038, "final_rank": 8 }, { "submission_id": "aoj_1543_1080691", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=a;i<b;++i)\n#define allof(a) a.begin(),a.end()\n#define minit(a,b) memset(a,b,sizeof(a))\n#define size_of(a) (int)a.size()\n\ntypedef long long lint;\ntypedef pair<int, int> pii;\n\nstruct State {\n\tint x, y, t, step;\n\tState(int _x, int _y, int _t, int _step) : x(_x), y(_y), t(_t), step(_step) {}\n};\nbool operator > (const State& a, const State& b) { return a.step > b.step; }\n\nint H, W, N, T[20];\nstring grid[20][25];\nint steps[20][25][25];\nint sx, sy, gx, gy;\n\nint dx[4] = {-1,0,1,0};\nint dy[4] = {0,-1,0,1};\n\nvoid solve() {\n\tT[0] = 0; T[N + 1] = 1000;\n\tfor_(i,0,20) for_(j,0,25) fill(steps[i][j], steps[i][j] + 25, (int)1e9);\n\tsteps[0][sy][sx] = 0;\n\t\n\tfor_(i,0, N + 1) {\n\t\tint limit = T[i + 1] - T[i];\n\t\t\n\t\tpriority_queue<State, vector<State>, greater<State> > q;\n\t\tfor_(y,0,H) for_(x,0,W) {\n\t\t\tif (steps[i][y][x] < (int)1e9) q.push(State(x, y, 0, steps[i][y][x]));\n\t\t}\n\t\t\n\t\twhile (!q.empty()) {\n\t\t\tState s = q.top(); q.pop();\n\t\t\t\n\t\t\tint cx = s.x, cy = s.y, t = s.t, step = s.step;\n\t\t\t\n\t\t\tif (cx == gx && cy == gy) {\n\t\t\t\tcout << step << endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t\n\t\t\tif (t >= limit) continue;\n\t\t\t\n\t\t\tif (steps[i][cy][cx] >= step) {\n\t\t\t\tif (t < limit - 1 && steps[i][cy][cx] >= step) {\n\t\t\t\t\tsteps[i][cy][cx] = step;\n\t\t\t\t\tq.push(State(cx, cy, t + 1, step));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][cy][cx] != '#' && steps[i + 1][cy][cx] > step) {\n\t\t\t\t\tsteps[i + 1][cy][cx] = step;\n\t\t\t\t} \n\t\t\t}\n\t\t\t\n\t\t\tfor_(d,0,4) {\n\t\t\t\tint nx = cx + dx[d], ny = cy + dy[d];\n\t\t\t\tif (!(0 <= nx && nx < W && 0 <= ny && ny < H)) continue;\n\t\t\t\t\n\t\t\t\tif (t < limit - 1 && grid[i][ny][nx] == '#') continue;\t\t\t\t\n\t\t\t\tif (t < limit - 1 && steps[i][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i][ny][nx] = step + 1;\n\t\t\t\t\tq.push(State(nx, ny, t + 1, step + 1));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][ny][nx] == '#') continue;\n\t\t\t\tif (t == limit - 1 && steps[i + 1][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i + 1][ny][nx] = step + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tcout << -1 << endl;\n}\n\nint main() {\n\tcin >> H >> W;\n\tfor_(i,0,H) {\n\t\tcin >> grid[0][i];\n\t\tfor_(j,0,W) {\n\t\t\tif (grid[0][i][j] == 'S') {\n\t\t\t\tsx = j; sy = i;\n\t\t\t}\n\t\t\tif (grid[0][i][j] == 'G') {\n\t\t\t\tgx = j; gy = i;\n\t\t\t}\n\t\t}\n\t}\n\tcin >> N;\n\tfor_(i,1,N + 1) {\n\t\tcin >> T[i];\n\t\tfor_(j,0,H) cin >> grid[i][j];\n\t}\n\tsolve();\n\treturn 0;\n}", "accuracy": 0.8387096774193549, "time_ms": 10, "memory_kb": 1284, "score_of_the_acc": -0.0354, "final_rank": 6 }, { "submission_id": "aoj_1543_1080682", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=a;i<b;++i)\n#define allof(a) a.begin(),a.end()\n#define minit(a,b) memset(a,b,sizeof(a))\n#define size_of(a) (int)a.size()\n\ntypedef long long lint;\ntypedef pair<int, int> pii;\n\nstruct State {\n\tint x, y, t, step;\n\tState(int _x, int _y, int _t, int _step) : x(_x), y(_y), t(_t), step(_step) {}\n};\nbool operator > (const State& a, const State& b) { return a.step > b.step; }\n\nint H, W, N, T[20];\nstring grid[20][25];\nint steps[20][25][25];\nint sx, sy, gx, gy;\n\nint dx[4] = {-1,0,1,0};\nint dy[4] = {0,-1,0,1};\n\nvoid solve() {\n\tT[0] = 0; T[N + 1] = 1000;\n\tfor_(i,0,20) for_(j,0,25) fill(steps[i][j], steps[i][j] + 25, (int)1e9);\n\tsteps[0][sy][sx] = 0;\n\t\n\tfor_(i,0, N + 1) {\n\t\tint limit = T[i + 1] - T[i];\n\t\t\n\t\tpriority_queue<State, vector<State>, greater<State> > q;\n\t\tfor_(y,0,H) for_(x,0,W) {\n\t\t\tif (steps[i][y][x] < (int)1e9) q.push(State(x, y, 0, steps[i][y][x]));\n\t\t}\n\t\t\n\t\twhile (!q.empty()) {\n\t\t\tState s = q.top(); q.pop();\n\t\t\t\n\t\t\tint cx = s.x, cy = s.y, t = s.t, step = s.step;\n\t\t\tif (t >= limit) continue;\n\t\t\t\n\t\t\tif (steps[i][cy][cx] >= step) {\n\t\t\t\tif (t < limit - 1 && steps[i][cy][cx] >= step) {\n\t\t\t\t\tsteps[i][cy][cx] = step;\n\t\t\t\t\tq.push(State(cx, cy, t + 1, step));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][cy][cx] != '#' && steps[i + 1][cy][cx] > step) {\n\t\t\t\t\tsteps[i + 1][cy][cx] = step;\n\t\t\t\t} \n\t\t\t}\n\t\t\t\n\t\t\tfor_(d,0,4) {\n\t\t\t\tint nx = cx + dx[d], ny = cy + dy[d];\n\t\t\t\tif (!(0 <= nx && nx < W && 0 <= ny && ny < H)) continue;\n\t\t\t\t\n\t\t\t\tif (t < limit - 1 && grid[i][ny][nx] == '#') continue;\t\t\t\t\n\t\t\t\tif (t < limit - 1 && steps[i][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i][ny][nx] = step + 1;\n\t\t\t\t\tq.push(State(nx, ny, t + 1, step + 1));\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (t == limit - 1 && grid[i + 1][ny][nx] == '#') continue;\n\t\t\t\tif (t == limit - 1 && steps[i + 1][ny][nx] > step + 1) {\n\t\t\t\t\tsteps[i + 1][ny][nx] = step + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tint ans = (int)1e9;\n\tfor_(i,0,N + 1) ans = min(ans, steps[i][gy][gx]);\n\tif (ans == (int)1e9) cout << -1 << endl;\n\telse cout << ans << endl;\n}\n\nint main() {\n\tcin >> H >> W;\n\tfor_(i,0,H) {\n\t\tcin >> grid[0][i];\n\t\tfor_(j,0,W) {\n\t\t\tif (grid[0][i][j] == 'S') {\n\t\t\t\tsx = j; sy = i;\n\t\t\t}\n\t\t\tif (grid[0][i][j] == 'G') {\n\t\t\t\tgx = j; gy = i;\n\t\t\t}\n\t\t}\n\t}\n\tcin >> N;\n\tfor_(i,1,N + 1) {\n\t\tcin >> T[i];\n\t\tfor_(j,0,H) cin >> grid[i][j];\n\t}\n\tsolve();\n\treturn 0;\n}", "accuracy": 0.8387096774193549, "time_ms": 10, "memory_kb": 1284, "score_of_the_acc": -0.0354, "final_rank": 6 }, { "submission_id": "aoj_1543_1080387", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n#define tpl(...) make_tuple(__VA_ARGS__)\nconst int INF = 0x3f3f3f3f;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T>ostream&operator<<(ostream &o,const vector<T>&t){o<<'[';FOR(i,t){if(i!=t.begin())o<<',';o<<*i;}return o<<']';}\ntemplate<class S,class T>ostream&operator<<(ostream &o,const pair<S,T>&t){return o<<'('<<t.first<<','<<t.second<<')';}\ntemplate<int N,class Tp>void output(ostream&,const Tp&){}\ntemplate<int N,class Tp,class,class ...Ts>void output(ostream &o,const Tp&t){if(N)o<<',';o<<get<N>(t);output<N+1,Tp,Ts...>(o,t);}\ntemplate<class ...Ts>ostream&operator<<(ostream&o,const tuple<Ts...>&t){o<<'(';output<0,tuple<Ts...>,Ts...>(o,t);return o<<')';}\ntemplate<class T>void output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20];\nint k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);}\ntemplate<class T>void outputs(T t){output(t);}\ntemplate<class S,class ...T>void outputs(S a,T...t){output(a,32);outputs(t...);}\ntemplate<class T>void output(T *a,int n){REP(i,n)output(a[i],i!=n-1?',':10);}\ntemplate<class T>void output(T *a,int n,int m){REP(i,n)output(a[i],m);}\ntemplate<class T>bool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;);\nif(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;}\ntemplate<class S,class ...T>bool input(S&a,T&...t){input(a);return input(t...);}\n\nconst int H = 20;\nconst int W = 20;\n\nconst int dy[] = {-1,0,1,0,0};\nconst int dx[] = {0,1,0,-1,0};\n\nstruct P {\n int y,x,t;\n int d;\n bool operator<(const P &rhs) const {\n return d > rhs.d;\n }\n};\nstruct Field {\n char a[801][H][W];\n int h,w;\n int sy,sx;\n int dist[801][H][W];\n bool input_hw() {\n return input(h,w);\n }\n void set_hw(int _h, int _w) {\n h = _h; w = _w;\n }\n void in() {\n REP(i,h)REP(j,w) {\n cin>>a[0][i][j];\n if (a[0][i][j] == 'S') {\n sy = i;\n sx = j;\n }\n }\n int n;\n cin >> n;\n int pre = 0;\n REP(k,n) {\n int t;\n cin >> t;\n for (int s=pre+1; s<t; ++s) {\n REP(i,h)REP(j,w) {\n a[s][i][j] = a[pre][i][j];\n }\n }\n REP(i,h) {\n REP(j,w) {\n cin >> a[t][i][j];\n }\n }\n pre = t;\n }\n for (int s=pre+1; s<=800; ++s) {\n REP(i,h)REP(j,w) {\n a[s][i][j] = a[pre][i][j];\n }\n } \n }\n int bfs() {\n memset(dist,0x3f,sizeof(dist));\n priority_queue<P> Q;\n Q.push(P{sy,sx,0,0});\n dist[0][sy][sx] = 0;\n while(!Q.empty()) {\n P p = Q.top(); Q.pop();\n int y = p.y;\n int x = p.x;\n if (dist[p.t][y][x] < p.d) continue;\n if (a[0][y][x] == 'G') return dist[p.t][y][x];\n if (p.t == 800) continue;\n // outputs(p.t,y,x, dist[p.t][y][x]);\n REP(i,5) {\n int yy = y + dy[i];\n int xx = x + dx[i];\n char c = a[p.t+1][yy][xx];\n if (valid(yy,xx,h,w) && c != '#') {\n int d2 = dist[p.t][y][x] + (i<4);\n if (dist[p.t+1][yy][xx] > d2) {\n dist[p.t+1][yy][xx] = d2;\n Q.push(P{yy,xx,p.t+1,d2});\n } \n } \n }\n }\n return -1;\n }\n} field;\n\nint main() {\n while(field.input_hw()) {\n field.in();\n cout << field.bfs() << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3108, "score_of_the_acc": -1.133, "final_rank": 5 }, { "submission_id": "aoj_1543_1080358", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n#include <array>\n#include <climits>\n\n// BEGIN CUT HERE\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n// END CUT HERE \n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define MP make_pair\n#define MT make_tuple\n\nusing namespace std;\n\ntypedef pair<int, int> P;\ntypedef long long ll;\n\nconst int N = 1e5 + 10;\n\nvector<int> G[N];\n\nstruct state{\n\tint r, c, t, d;\n\tbool operator < (const state &rhs) const{\n\t\treturn d > rhs.d;\n\t}\n};\n\nint dist[25][25][210];\n\nint dx[] = { 0, 1, 0, -1, 0 };\nint dy[] = { -1, 0, 1, 0, 0 };\n\ninline int &getdist(state &s){\n\treturn dist[s.r][s.c][s.t];\n}\n\nvoid go(state nxt, priority_queue<state> &q){\n\tint &d = getdist(nxt);\n\tif (d <= nxt.d) return;\n\td = nxt.d;\n\tq.push(nxt);\n}\n\nint a[210];\n\nint main(){\n\tint h, w;\n\tcin >> h >> w;\n\tvector<string> tmp(h);\n\trep(i, h) cin >> tmp[i];\n\tint n;\n\tcin >> n;\n\tvector<vector<string>> area(n+1, vector<string>(h));\n\tarea[0] = tmp;\n\n\tMEMSET(a, -1);\n\ta[0] = 0;\n\tFOR(i, 1, n + 1){\n\t\tint x;\n\t\tcin >> x;\n\t\ta[x] = i;\n\t\trep(j, h) cin >> area[i][j];\n\t}\n\trep(i, 200) if (a[i + 1] < 0) a[i + 1] = a[i];\n\n\trep(i, 25) rep(j, 25) rep(k, 210) dist[i][j][k] = 1e9;\n\n\tP start, goal;\n\trep(i, h) rep(j, w){\n\t\tif (tmp[i][j] == 'S') start = MP(i, j);\n\t\tif (tmp[i][j] == 'G') goal = MP(i, j);\n\t}\n\n\tpriority_queue<state> q;\n\tgo({ start.first, start.second, 0, 0 }, q);\n\twhile (!q.empty()){\n\t\tauto s = q.top();\n\t\tq.pop();\n\n\t\tif (getdist(s) < s.d) continue;\n\n\t\tif (s.r == goal.first && s.c == goal.second){\n\t\t\tcout << s.d << endl;\n\t\t\treturn 0;\n\t\t}\n\n\t\ts.t = min(s.t+1, 200);\n\t\t++s.d;\n\t\trep(d, 5){\n\t\t\tstate nxt = s;\n\t\t\tif (d == 4) --nxt.d;\n\t\t\tnxt.r += dx[d], nxt.c += dy[d];\n\t\t\tif (nxt.r < 0 || nxt.r >= h || nxt.c < 0 || nxt.c >= w) continue;\n\t\t\tint na = a[nxt.t];\n\t\t\tif (area[na][nxt.r][nxt.c] == '#') continue;\n\t\t\tgo(nxt, q);\n\t\t}\n\t}\n\tcout << -1 << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4228, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_1543_1080346", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nclass range {\nprivate:\n struct Iterator {\n int val;\n int operator*() {return val;}\n bool operator!=(Iterator &itr) {return val < itr.val;}\n void operator++() {++val;}\n };\n Iterator i, n;\npublic:\n range(int n) : i({0}), n({n}) {}\n range(int i, int n) : i({i}), n({n}) {}\n Iterator &begin() {return i;}\n Iterator &end() {return n;}\n};\n\ntemplate<class T> T at(vector<T> v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];}\n\nconst int INF = 1e9;\n\nconst int dx[] = {1, 0, -1, 0};\nconst int dy[] = {0, -1, 0, 1};\n\nint main() {\n int h, w;\n cin >> h >> w;\n string area[16][h];\n for (int i : range(h)) cin >> area[0][i];\n int n;\n cin >> n;\n int t[n];\n for (int i : range(n)) {\n cin >> t[i];\n for (int j : range(h)) cin >> area[i + 1][j];\n }\n int dp[1000][h][w];\n for (int i : range(1000)) for (int j : range(h)) for (int k : range(w)) dp[i][j][k] = INF;\n for (int i : range(h)) for (int j : range(w)) if (area[0][i][j] == 'S') dp[0][i][j] = 0;\n int s = 0, res = INF;\n for (int i : range(999)) {\n if (s < n && t[s] == i + 1) ++s;\n for (int y : range(h)) for (int x : range(w)) if (dp[i][y][x] != INF) {\n for (int k : range(4)) {\n int yy = y + dy[k];\n int xx = x + dx[k];\n if (yy < 0 || h <= yy) continue;\n if (xx < 0 || w <= xx) continue;\n if (area[s][yy][xx] == '#') continue;\n dp[i + 1][yy][xx] = min(dp[i + 1][yy][xx], dp[i][y][x] + 1);\n if (area[0][yy][xx] == 'G') res = min(res, dp[i][y][x] + 1);\n }\n if (area[s][y][x] != '#') dp[i + 1][y][x] = min(dp[i + 1][y][x], dp[i][y][x]);\n }\n }\n if (res == INF) cout << -1 << endl;\n else cout << res << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2796, "score_of_the_acc": -0.5308, "final_rank": 1 }, { "submission_id": "aoj_1543_1080315", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\nusing namespace std;\n\n#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)\ntemplate <typename T> void print_container(ostream& os, const T& c) { const char* _s = \" \"; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\n\ntemplate <typename T> void print(T a, int n, const string& split = \" \") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }\ntemplate <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }\ntemplate <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }\n#define dump(v) (cerr << #v << \": \" << v << endl)\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\n\n\nint main()\n{\n int h, w;\n cin >> h >> w;\n char c[36][36];\n rep(y, h)\n cin >> c[y];\n\n int n;\n cin >> n;\n int next_t;\n cin >> next_t;\n\n int sx = -1, sy, gx = -1, gy;\n rep(y, h) rep(x, w)\n {\n if (c[y][x] == 'S')\n {\n sx = x, sy = y;\n c[y][x] = '.';\n }\n else if (c[y][x] == 'G')\n {\n gx = x, gy = y;\n c[y][x] = '.';\n }\n }\n assert(sx != -1 && gx != -1);\n\n const int inf = ten(8);\n int dp[36][36];\n rep(y, h) rep(x, w)\n dp[y][x] = inf;\n dp[sy][sx] = 0;\n int stage = 0;\n int res = inf;\n rep(cur_t, ten(4))\n {\n if (stage < n && cur_t == next_t)\n {\n rep(y, h)\n cin >> c[y];\n cin >> next_t;\n ++stage;\n }\n\n int ndp[36][36];\n rep(y, h) rep(x, w)\n {\n ndp[y][x] = inf;\n if (c[y][x] == '#')\n dp[y][x] = inf;\n }\n if (dp[gy][gx] != inf)\n upmin(res, dp[gy][gx]);\n\n rep(y, h) rep(x, w)\n {\n if (dp[y][x] != inf)\n {\n upmin(ndp[y][x], dp[y][x]);\n rep(dir, 4)\n {\n int nx = x + dx[dir], ny = y + dy[dir];\n if (in_rect(nx, ny, w, h))\n upmin(ndp[ny][nx], dp[y][x] + 1);\n }\n }\n }\n\n bool updated = false;\n rep(y, h) rep(x, w)\n {\n updated |= dp[y][x] != ndp[y][x];\n dp[y][x] = ndp[y][x];\n }\n if (!updated)\n break;\n }\n\n cout << (res != inf ? res : -1) << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1176, "score_of_the_acc": -1, "final_rank": 3 } ]
aoj_1545_cpp
Problem E: Rooted Tree Game Problem 初期状態として複数の根付き木が与えられる。これに対しAliceとBobはゲームを行う。ゲームは2人交互に行い、先手がAliceで後手がBobである。ターンが回ってきたプレイヤーは以下の行動を取る。 根(親を持たない頂点)を1つ選択する。この頂点を S とする。 S を根とする根付き木に含まれる頂点を選択する(ここでは S も選択可能)。この頂点を T とする。 S から T への経路上にある頂点を、 S と T も含めすべて削除する。また、削除された頂点が端点であるような辺もすべて削除する。 ターンが回ってきた時点で頂点がすべて削除されていた場合、そのプレイヤーの負けとなる。 AliceとBobが常に最適な行動を取る時、与えられた初期状態に対し、勝利するプレイヤーを判定せよ。 以下の図は、プレイヤーの行動の例を示す。 Input 入力は以下の形式で与えられる。 N M p 1 p 2 : p M 1行目に、初期状態の頂点数 N と辺の数 M が空白区切りで与えられる。この時、各頂点を表す番号は1~ N である。次の M 行では、辺の情報が与えられる。このうち i 行目では1つの整数 p i が与えられる。これは、頂点 p i から頂点 i への辺があることを表す。言い換えると、頂点 i の親が頂点 p i であることを表す。 Constraints 入力は以下の制約を満たす。 1 ≤ N ≤ 1000 0 ≤ M < N i < p i ≤ N ( 1 ≤ i ≤ M ) Output 勝利するプレイヤーの名前(AliceまたはBob)を1行に出力せよ。 Sample Input1 6 3 4 4 5 Sample Output1 Alice Sample Input2 6 4 2 5 4 5 Sample Output2 Bob
[ { "submission_id": "aoj_1545_8295647", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N, P[1 << 18];\nint M;\nint Answer[1 << 18];\nvector<int> Cand;\nvector<int> G[1 << 18];\n\nint FindMex(vector<int> vec) {\n\tvec.push_back(-1);\n\tvec.push_back(10000000);\n\tsort(vec.begin(), vec.end());\n\tfor (int i = 0; i < vec.size() - 1; i++) {\n\t\tif (vec[i + 1] - vec[i] >= 2) return vec[i] + 1;\n\t}\n\treturn -1;\n}\n\nvoid dfs(int pos, int cur) {\n\tint base_xor = 0;\n\tfor (int to : G[pos]) base_xor ^= Answer[to];\n\tCand.push_back(cur ^ base_xor);\n\n\t// Recursion\n\tfor (int to : G[pos]) {\n\t\tint nex_xor = ((cur ^ base_xor) ^ Answer[to]);\n\t\tdfs(to, nex_xor);\n\t}\n}\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M;\n\tfor (int i = 1; i <= M; i++) cin >> P[i];\n\tfor (int i = 1; i <= M; i++) G[P[i]].push_back(i);\n\n\t// Step 2. Dynamic Programming\n\tfor (int i = 1; i <= N; i++) {\n\t\tCand.clear();\n\t\tdfs(i, 0);\n\t\tAnswer[i] = FindMex(Cand);\n\t}\n\n\t// Step 3. Output\n\tint ret = 0;\n\tfor (int i = M + 1; i <= N; i++) ret ^= Answer[i];\n\tif (ret != 0) cout << \"Alice\" << endl;\n\telse cout << \"Bob\" << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 11640, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_1545_1080867", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\nusing namespace std;\n\n#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)\ntemplate <typename T> void print_container(ostream& os, const T& c) { const char* _s = \" \"; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\n\ntemplate <typename T> void print(T a, int n, const string& split = \" \") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }\ntemplate <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }\ntemplate <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }\n#define dump(v) (cerr << #v << \": \" << v << endl)\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\n\nint dp[1024];\nvector<int> g[1024];\nint grundy(int);\nvoid dfs(int i, set<int>& path, set<int>& ns, int x)\n{\n path.insert(i);\n\n for (int to : g[i])\n x ^= grundy(to);\n ns.insert(x);\n\n for (int to : g[i])\n dfs(to, path, ns, x ^ grundy(to));\n\n path.erase(i);\n}\nint grundy(int i)\n{\n if (dp[i] != -1)\n return dp[i];\n\n set<int> ns, path;\n dfs(i, path, ns, 0);\n\n int x = 0;\n while (ns.count(x))\n ++x;\n return dp[i] = x;\n}\nint main()\n{\n int n, m;\n cin >> n >> m;\n clr(dp, -1);\n bool root[1024];\n fill_n(root, n, true);\n rep(i, m)\n {\n int p;\n cin >> p;\n --p;\n\n root[i] = false;\n g[p].push_back(i);\n }\n\n int x = 0;\n rep(i, n)\n if (root[i])\n x ^= grundy(i);\n cout << (x ? \"Alice\" : \"Bob\") << endl;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 1580, "score_of_the_acc": -1.013, "final_rank": 4 }, { "submission_id": "aoj_1545_1080718", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr int MAX_N = 1000;\n\nint n, m;\nvector<int> children[MAX_N];\nint parent[MAX_N];\n\nint memo[MAX_N];\npair<bool, int> memo2[MAX_N][MAX_N];\nvector<int> nodes[MAX_N];\n\nvector<int> calc(int v) {\n auto &res = nodes[v];\n if(!res.empty()) return res;\n\n res.emplace_back(v);\n for(const auto &c : children[v]) {\n const auto vec = calc(c);\n res.insert(res.end(), vec.begin(), vec.end());\n }\n return res;\n}\n\nint grundy(int v);\npair<bool, int> dfs(int v, int u) {\n auto &res = memo2[v][u];\n if(res.second != -1) return res;\n\n bool reach = false;\n int value = 0;\n if(v == u) {\n reach = true;\n for(const auto &c : children[v]) {\n value ^= grundy(c);\n }\n }\n else {\n vector<pair<bool, int>> buf;\n buf.reserve(children[v].size());\n for(const auto &c : children[v]) {\n buf.emplace_back(dfs(c, u));\n reach |= buf.back().first;\n }\n\n if(reach) {\n for(const auto &e : buf) {\n\tvalue ^= e.second;\n }\n }\n else {\n value = grundy(v);\n }\n }\n\n return res = make_pair(reach, value);\n}\n\nint grundy(int v) {\n int &res = memo[v];\n if(res != -1) return res;\n\n set<int> S;\n for(const auto &u : nodes[v]) {\n S.insert(dfs(v, u).second);\n }\n\n res = 0;\n while(S.count(res)) ++res;\n return res;\n}\n\nint main() {\n cin.tie(NULL);\n ios::sync_with_stdio(false);\n\n cin >> n >> m;\n\n memset(parent, -1, sizeof(parent));\n\n for(int i = 0; i < m; ++i) {\n int p;\n cin >> p;\n --p;\n\n parent[i] = p;\n children[p].emplace_back(i);\n }\n\n memset(memo, -1, sizeof(memo));\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < n; ++j) {\n memo2[i][j] = make_pair(false, -1);\n }\n }\n\n int x = 0;\n for(int v = 0; v < n; ++v) {\n if(parent[v] == -1) {\n calc(v);\n x ^= grundy(v);\n }\n }\n\n cout << (x != 0 ? \"Alice\" : \"Bob\") << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 11404, "score_of_the_acc": -1.6018, "final_rank": 5 }, { "submission_id": "aoj_1545_1080695", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n#include <array>\n#include <climits>\n#include <bitset>\n\n// BEGIN CUT HERE\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n// END CUT HERE \n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define MP make_pair\n#define MT make_tuple\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n\nconst int N = 1010;\nint dp[N];\nvector<int> G[N];\n\nint calc_grundy(int v);\n\nvoid dfs(int v, int x, set<int> &st){\n\tfor (auto nxt : G[v]){\n\t\tx ^= calc_grundy(nxt);\n\t}\n\tst.insert(x);\n\tfor (auto nxt : G[v]){\n\t\tdfs(nxt, x^calc_grundy(nxt), st);\n\t}\n}\n\nint calc_grundy(int v){\n\tint &res = dp[v];\n\tif (res >= 0) return res;\n\n\tset<int> st;\n\tdfs(v, 0, st);\n\tfor (int i = 0; i <= 1000; ++i){\n\t\tif (st.count(i) == 0) return res = i;\n\t}\n\treturn -1;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tMEMSET(dp, -1);\n\n\tint n, m;\n\tcin >> n >> m;\n\n\tvector<int> p(n, -1);\n\trep(i, m){\n\t\tcin >> p[i];\n\t\t--p[i];\n\t\tG[p[i]].push_back(i);\n\t}\n\n\tint g = 0;\n\trep(i, n) if (p[i] < 0) g ^= calc_grundy(i);\n\n\tcout << (g ? \"Alice\" : \"Bob\") << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1468, "score_of_the_acc": -0.377, "final_rank": 2 }, { "submission_id": "aoj_1545_1080484", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n#define tpl(...) make_tuple(__VA_ARGS__)\nconst int INF = 0x3f3f3f3f;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T>ostream&operator<<(ostream &o,const vector<T>&t){o<<'[';FOR(i,t){if(i!=t.begin())o<<',';o<<*i;}return o<<']';}\ntemplate<class S,class T>ostream&operator<<(ostream &o,const pair<S,T>&t){return o<<'('<<t.first<<','<<t.second<<')';}\ntemplate<int N,class Tp>void output(ostream&,const Tp&){}\ntemplate<int N,class Tp,class,class ...Ts>void output(ostream &o,const Tp&t){if(N)o<<',';o<<get<N>(t);output<N+1,Tp,Ts...>(o,t);}\ntemplate<class ...Ts>ostream&operator<<(ostream&o,const tuple<Ts...>&t){o<<'(';output<0,tuple<Ts...>,Ts...>(o,t);return o<<')';}\ntemplate<class T>void output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20];\nint k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);}\ntemplate<class T>void outputs(T t){output(t);}\ntemplate<class S,class ...T>void outputs(S a,T...t){output(a,32);outputs(t...);}\ntemplate<class T>void output(T *a,int n){REP(i,n)output(a[i],i!=n-1?',':10);}\ntemplate<class T>void output(T *a,int n,int m){REP(i,n)output(a[i],m);}\ntemplate<class T>bool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;);\nif(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;}\ntemplate<class S,class ...T>bool input(S&a,T&...t){input(a);return input(t...);}\n\nvector<int> g[1000];\nint grundy(int v, int p);\n\nint vvv;\nvoid dfs(int v, int p, int gr, int vvv, vector<int> &gv) {\n FOR(it, g[v]) {\n if (*it == p) continue;\n gr ^= grundy(*it, v);\n }\n // if (gr == 0) {\n // cout << p+1 << \" \" << vvv+1 << \" \" << v+1 << endl;\n // }\n gv.push_back(gr);\n FOR(it, g[v]) {\n if (*it == p) continue;\n dfs(*it, v, gr ^ grundy(*it, v), vvv, gv);\n }\n}\n\nint dp[1000];\nint grundy(int v, int p) {\n if (dp[v] >= 0) return dp[v];\n vvv= v;\n vector<int> gv;\n dfs(v,p,0,v,gv);\n sort(ALL(gv));\n int res = 0;\n gv.erase(unique(ALL(gv)), gv.end());\n REP(i,gv.size()) {\n if (gv[i] != res) break;\n res++;\n }\n // cout << v+1 << \" \" << gv << \" \" << res << endl;\n return dp[v] = res;\n}\n\nint main() {\n int n,m;\n while(cin >> n >> m) {\n REP(i,n) g[i].clear();\n vector<int> num(n);\n REP(i,m) {\n int p; cin >> p;\n g[p-1].push_back(i);\n num[i]++;\n }\n memset(dp,-1,sizeof(dp));\n int gr = 0;\n REP(i,n) {\n if (num[i] == 0) {\n gr ^= grundy(i,-1);\n }\n }\n // output(dp,n);\n cout << (gr?\"Alice\":\"Bob\") << endl;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1448, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1546_cpp
Problem F: Coupling Problem 愛津大学では毎年大規模な合コンが行われています。 今年は N 人の男性と M 人の女性が参加します。 それぞれ男性は0から順に N −1までIDが割り振られおり、女性は0から順に M −1までIDが割り振られています。 この合コンでは自分の「大好きな人」と「そこそこ好きな人」のIDを提示します。 男性はそれぞれ女性のIDを、女性はそれぞれ男性のIDを提示します。 その後、以下のルールに従ってカップルが成立します。 男性は複数の女性、女性は複数の男性とカップルになってはいけない。 互いに「大好きな人」と提示した男性と女性でペアができる。 そこから、任意のペアを選びカップルを作ることができる。 片方が「大好きな人」、もう片方が「そこそこ好きな人」と提示した男性と女性でペアができる。 そこから、任意のペアを選びカップルを作ることができる。 互いに「そこそこ好きな人」と提示した男性と女性でペアができる。 そこから、任意のペアを選びカップルを作ることができる。 ルール2で出来たカップル、ルール3で出来たカップル、ルール4で出来たカップルの順にカップルの数が最大になるようにカップルが成立する。 サジ君はこの合コンの主催者です。 近年では参加者の数が多くなり手動でカップルの数を把握するのが大変になってきました。そこでプログラマーであるあなたは、サジ君の手伝いをすることにしました。上記のルールに従ってカップルを作った時の「大好きな人」同士のカップルの数と「大好きな人」と「そこそこ好きな人」によるカップルの数と「そこそこ好きな人」同士のカップルの数を出力するプログラムを作成してください。 Input 入力は以下の形式で与えられる。 N M L1 a 1 b 1 a 2 b 2 : a L1 b L1 L2 c 1 d 1 c 2 d 2 : c L2 d L2 L3 e 1 f 1 e 2 f 2 : e L3 f L3 L4 g 1 h 1 g 2 h 2 : g L4 h L4 1行目にそれぞれ男性の参加者の数と女性の参加者の数を表す2つの整数 N , M が与えられる。次に男性側の大好きな人を表すデータの数 L1 が与えられる。続くL1行に a i と b i が空白区切りで与えられる。それぞれ、ID a i の男性がID b i の女性を「大好き」であることを示す。 続く行に男性側のそこそこ好きな人を表すデータの数 L2 が与えられる。続くL2行に c i と d i が空白区切りで与えられる。それぞれ、ID c i の男性がID d i の女性を「そこそこ好き」であることを示す。次に女性側の大好きな人を表すデータの数 L3 が与えられる。続くL3行に e i と f i が空白区切りで与えられる。それぞれ、ID e i の女性がIDの f i の男性を「大好き」であることを示す。 続く行に女性側のそこそこ好きな人を表すデータの数 L4 が与えられる。続くL4行に g i と h i が空白区切りで与えられる。それぞれ、ID g i の女性がID h i の男性を「そこそこ好き」であることを示す。 Constraints 入力は以下の条件を満たす。 1 ≤ N , M ≤ 100 0 ≤ a i , c i , f i , h i ≤ N −1 0 ≤ b i , d i , e i , g i ≤ M −1 0 ≤ L1 , L2 , L3 , L4 ≤ 2000 L1 > 0, L2 > 0 において ( a i , b i ) ≠ ( c j , d j ) ( 0 ≤ i < L1 , 0 ≤ j < L2 ) L3 > 0, L4 > 0 において ( e i , f i ) ≠ ( g j , h j ) ( 0 ≤ i < L3 , 0 ≤ j < L4 ) ( a i , b i ) ≠ ( a j , b j ) ( i ≠ j ) ( 0 ≤ i < L1 , 0 ≤ j < L1 ) ( c i , d i ) ≠ ( c j , d j ) ( i ≠ j ) ( 0 ≤ i < L2 , 0 ≤ j < L2 ) ( e i , f i ) ≠ ( e j , f j ) ( i ≠ j ) ( 0 ≤ i < L3 , 0 ≤ j < L3 ) ( g i , h i ) ≠ ( g j , h j ) ( i ≠ j ) ( 0 ≤ i < L4 , 0 ≤ j < L4 ) Output 問題文のルールに従ってカップルが成立した時の、「大好きな人」同士のペアの数と「大好きな人」と「そこそこ好きな人」のペアの数と「そこそこ好き」同士のペアの数を空白区切りで1行に出力せよ。 Sample Input 1 3 3 3 0 0 1 1 2 2 2 1 0 2 1 3 1 1 2 2 0 1 2 1 0 2 0 Sample Output 1 2 0 0 この場合男性1と女性1、男性2と女性2が大好き同士でカップルとなり「大好き同士」のペア数が2になる。 すると、男性0のことが「大好き」な女性も「そこそこ好き」な女性もいなくなってしまいます。 また、女性0のことが「大好き」な男性も「そこそこ好き」な男性もいなくなってしまうため以降カップルは成立しなくなります。 Sample input 2 5 5 5 4 1 2 2 1 4 1 3 4 2 5 1 1 1 2 3 3 3 0 3 4 5 2 3 2 2 0 3 0 2 4 1 5 2 0 4 0 1 0 4 3 2 1 Sample Output2 2 1 0
[ { "submission_id": "aoj_1546_3578210", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n#define all(x) (x).begin(),(x).end()\n\ntypedef pair<int,int> P;\ntypedef pair<P,int> PI;\n#define fi first\n#define se second\n\n#define INF INT_MAX/3\n#define MAX_V 300\n\nstruct edge{\n int to,cap,cost,rev;\n};\n\nint V;\nvector<edge> G[MAX_V];\nint h[MAX_V];\nint dist[MAX_V];\nint prevv[MAX_V],preve[MAX_V];\n\nvoid init(int v){\n V=v;\n for(int i=0;i<MAX_V;i++){\n G[i].clear();\n }\n}\n\nvoid add_edge(int from,int to,int cap,int cost){\n //printf(\"%d -> %d : %d\\n\", from,to,cap);\n G[from].push_back((edge){to,cap,cost,(int)G[to].size()});\n G[to].push_back((edge){from,0,-cost,(int)G[from].size()-1});\n}\n\nint min_cost_flow(int s,int t,int f){\n int res=0;\n memset(h,0,sizeof(h));\n while(f>0){\n fill(dist,dist+V,INF);\n dist[s]=0;\n bool update=true;\n while(update){\n update=false;\n for(int v=0;v<V;v++){\n if(dist[v]==INF)continue;\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&dist[e.to]>dist[v]+e.cost){\n dist[e.to]=dist[v]+e.cost;\n prevv[e.to]=v;\n preve[e.to]=i;\n update=true;\n }\n }\n }\n }\n if(dist[t]==INF)return INF;\n\n int d=f;\n for(int v=t;v!=s;v=prevv[v]){\n d=min(d,G[prevv[v]][preve[v]].cap);\n }\n f-=d;\n res+=d*dist[t];\n for(int v=t;v!=s;v=prevv[v]){\n edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;\n G[v][e.rev].cap+=d;\n }\n }\n return res;\n}\n\nint N,M;\nint g[2][303][303];\n\nint main(){\n cin>>N>>M;\n rep(x,2)rep(y,2){\n int L;\n cin>>L;\n rep(i,L){\n int a,b;\n cin>>a>>b;\n if(x==0) g[y][a][b]++;\n else g[y][b][a]++;\n }\n }\n\n int source=N+M,sink=N+M+1;\n\n PI res=PI(P(0,0),0);\n repl(f,1,min(N,M)+1){\n init(N+M+2);\n\n rep(i,N)add_edge(source,i,1,0);\n rep(i,M)add_edge(N+i,sink,1,0);\n\n rep(i,N)rep(j,M){\n if(g[0][i][j]==2){\n add_edge(i,N+j,1,-30000);\n }else if(g[0][i][j]==1&&g[1][i][j]==1){\n add_edge(i,N+j,1,-200);\n }else if(g[1][i][j]==2){\n add_edge(i,N+j,1,-1);\n }\n }\n\n int m=-min_cost_flow(source,sink,f);\n int resa=m/30000;\n int resb=(m-resa*30000)/200;\n int resc=(m-resa*30000-resb*200);\n res=max(res,PI(P(resa,resb),resc));\n }\n\n cout<<res.fi.fi<<\" \"<<res.fi.se<<\" \"<<res.se<<endl;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3468, "score_of_the_acc": -1.4296, "final_rank": 6 }, { "submission_id": "aoj_1546_1133803", "code_snippet": "#include<bits/stdc++.h>\n\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nusing namespace std;\n/*\ntypedef long long ll;\ntypedef pair<ll,int> ii;\n \nstruct Edge{\n int to; ll cap; ll cost; int rev;\n Edge(int to=0,int cap=0,ll cost=0,int rev=0):to(to),cap(cap),cost(cost),rev(rev){}\n};\n \nconst int MAX_V = 1100; const ll IINF = LLONG_MAX;\nint V;\nvector<Edge> G[MAX_V];\nll h[MAX_V],dist[MAX_V]; int prevv[MAX_V],preve[MAX_V];\n \ninline void add_edge(int from,int to,int cap,ll cost){\n G[from].push_back(Edge(to,cap,cost,G[to].size()));\n G[to].push_back(Edge(from,0,-cost,G[from].size()-1));\n}\n \n\nll min_cost_flow(int s,int t){\n ll res = 0;\n while(1){\n fill(dist,dist+V,IINF);\n dist[s] = 0;\n bool update = true;\n while( update ){\n update = false;\n rep(v,V){\n if( dist[v] == IINF ) continue;\n rep(i,(int)G[v].size()){\n Edge &e = G[v][i];\n if( e.cap > 0LL && dist[e.to] > dist[v] + e.cost ){\n dist[e.to] = dist[v] + e.cost;\n prevv[e.to] = v;\n preve[e.to] = i;\n update = true;\n }\n }\n }\n }\n\n if( dist[t] == IINF ) break;\n \n ll d = IINF;\n for(int v=t;v!=s;v=prevv[v]) d = min(d,G[prevv[v]][preve[v]].cap);\n if( d == 0LL ) break;\n\n res += d * dist[t];\n for(int v=t;v!=s;v=prevv[v]){\n Edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n}\n*/\ntypedef long long ll;\ntypedef pair<ll,int> ii;\n\n\nstruct Edge{\n int to; ll cap,cost; int rev;\n Edge(int to=0,ll cap=0,ll cost=0,int rev=0):to(to),cap(cap),cost(cost),rev(rev){}\n};\n\nconst int MAX_V = 1100; ll IINF = LLONG_MAX;\nint V;\nvector<Edge> G[MAX_V];\nll h[MAX_V],dist[MAX_V]; int prevv[MAX_V],preve[MAX_V];\n\ninline void add_edge(int from,int to,ll cap,ll cost){\n G[from].push_back(Edge(to,cap,cost,G[to].size()));\n G[to].push_back(Edge(from,0,-cost,G[from].size()-1));\n}\n\nll min_cost_flow(int s,int t){\n ll res = 0;\n fill(h,h+V,0);\n while(1){\n priority_queue<ii,vector<ii>,greater<ii> > Q;\n fill(dist,dist+V,IINF);\n dist[s] = 0;\n Q.push(ii(0,s));\n while(!Q.empty()){\n ii p = Q.top(); Q.pop();\n int v = p.second;\n if( dist[v] < p.first ) continue;\n for(int i=0;i<G[v].size();i++){\n Edge &e = G[v][i];\n if( e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to] ) { \n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n Q.push(ii(dist[e.to],e.to));\n }\n }\n }\n //if( dist[t] == IINF ) break;\n rep(v,V) h[v] += dist[v];\n if( h[t] >= 0LL ) break;\n ll d = IINF;\n for(int v=t;v!=s;v=prevv[v]) d = min(d,G[prevv[v]][preve[v]].cap);\n res += d * h[t];\n for(int v=t;v!=s;v=prevv[v]){\n Edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n}\n\n\nint N,M;\n\nint main(){\n cin >> N >> M;\n\n int source = N+M;\n int sink = source + 1;\n V = sink + 1;\n rep(i,N) add_edge(source,i,1,0);\n rep(i,M) add_edge(N+i,sink,1,0);\n \n set<ii> S[2];\n int L,a,b;\n cin >> L;\n rep(i,L) { cin >> a >> b; S[0].insert(ii(a,b)); }\n cin >> L;\n rep(i,L) { cin >> a >> b; S[1].insert(ii(a,b)); }\n cin >> L;\n rep(i,L) {\n cin >> a >> b;\n if( S[0].count(ii(b,a)) ) add_edge(b,N+a,1,-1000000000LL);\n if( S[1].count(ii(b,a)) ) add_edge(b,N+a,1,-1000000LL);\n }\n cin >> L;\n rep(i,L) {\n cin >> a >> b;\n if( S[0].count(ii(b,a)) ) add_edge(b,N+a,1,-1000000LL);\n if( S[1].count(ii(b,a)) ) add_edge(b,N+a,1,-1000LL);\n }\n ll res = -1 * min_cost_flow(source,sink);\n cout << res/1000000000LL << \" \"\n << res/1000000LL%1000LL << \" \"\n << res/1000LL%1000LL << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1588, "score_of_the_acc": -0.0638, "final_rank": 1 }, { "submission_id": "aoj_1546_1089446", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < int(n); i++)\n#define all(c) (c).begin(), (c).end()\nusing namespace std;\n\nstruct MinCostFlow{\n struct edge{int to,cap,rev,cost;};\n vector<vector<edge>> G;\n vector<int> dist,pv,pe;\n int size;\n\n static const int MAX_C = 1000000000;\n\n MinCostFlow(int size):G(size),dist(size),pv(size),pe(size),size(size){}\n\n void add_edge(int from, int to, int cap, int cost){\n //printf(\"%d --> %d , %d\\n\",from,to,cost);\n G[from].push_back({to, cap, (int)G[to].size(), cost});\n G[to].push_back({from, 0, (int)G[from].size() - 1, -cost});\n }\n\n //impossible f ? -1 : result\n int min_cost_flow(int s, int t, int f){\n int res = 0;\n while(f > 0){\n fill(begin(dist),end(dist),MAX_C);\n dist[s] = 0;\n bool upd = true;\n while(upd){\n upd = false;\n for(int v = 0; v < size; v++){\n if(dist[v] == MAX_C) continue;\n for(int i = 0; i < (int)G[v].size(); i++){\n edge &e = G[v][i];\n if(e.cap > 0 && dist[e.to] > dist[v] + e.cost){\n dist[e.to] = dist[v] + e.cost;\n pv[e.to] = v;\n pe[e.to] = i;\n upd = true;\n }\n }\n }\n }\n\n if(dist[t] == MAX_C){\n //impossible f\n return -1;\n }\n\n int d = f;\n for(int v = t; v != s; v = pv[v]){\n d = min(d, G[pv[v]][pe[v]].cap);\n }\n\n f -= d;\n res += d * dist[t];\n\n for(int v = t; v != s; v = pv[v]){\n edge &e = G[pv[v]][pe[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n};\n\nconst int X = 500;\n\nstruct Ans{\n int a,b,c;\n Ans(int x):a(x%X),b(x%(X*X)/X),c(x/(X*X)){\n assert(x >= 0);\n }\n};\n\nint G[100][100];\n\nvoid fill_G(int x, bool rev){\n int l,a,b;\n cin >> l;\n rep(i,l){\n cin >> a >> b;\n (rev ? G[b][a] : G[a][b]) += x;\n }\n}\n\nint main(){\n int n,m;\n cin >> n >> m;\n\n fill_G(5,false);\n fill_G(1,false);\n fill_G(5,true);\n fill_G(1,true);\n\n MinCostFlow mcf(300);\n const int S = 298, T = 299;\n\n rep(i,n) rep(j,m){\n if(G[i][j] == 10) mcf.add_edge(i,j+100,1,1);\n else if(G[i][j] == 6) mcf.add_edge(i,j+100,1,X);\n else if(G[i][j] == 2) mcf.add_edge(i,j+100,1,X * X);\n }\n\n rep(i,n) mcf.add_edge(S,i,1,0);\n rep(j,m) mcf.add_edge(j+100,T,1,0);\n\n Ans ans(0);\n bool upd = false;\n for(int i = 1000; i >= 1; i--){\n MinCostFlow tmp = mcf;\n int res = tmp.min_cost_flow(S,T,i);\n if(res >= 0){\n Ans x(res);\n if((x.a > ans.a) || (x.a == ans.a && x.b > ans.b) || (x.a == ans.a && x.b == ans.b && x.c > ans.c)){\n ans = x;\n upd = true;\n }\n }\n }\n assert(upd);\n cout << ans.a << \" \" << ans.b << \" \" << ans.c << endl;\n}", "accuracy": 0.07407407407407407, "time_ms": 40, "memory_kb": 1260, "score_of_the_acc": -0.5, "final_rank": 7 }, { "submission_id": "aoj_1546_1083383", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n#define tpl(...) make_tuple(__VA_ARGS__)\nconst int INF = 0x3f3f3f3f;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }\ntemplate<class T>ostream&operator<<(ostream &o,const vector<T>&t){o<<'[';FOR(i,t){if(i!=t.begin())o<<',';o<<*i;}return o<<']';}\ntemplate<class S,class T>ostream&operator<<(ostream &o,const pair<S,T>&t){return o<<'('<<t.first<<','<<t.second<<')';}\ntemplate<int N,class Tp>void output(ostream&,const Tp&){}\ntemplate<int N,class Tp,class,class ...Ts>void output(ostream &o,const Tp&t){if(N)o<<',';o<<get<N>(t);output<N+1,Tp,Ts...>(o,t);}\ntemplate<class ...Ts>ostream&operator<<(ostream&o,const tuple<Ts...>&t){o<<'(';output<0,tuple<Ts...>,Ts...>(o,t);return o<<')';}\ntemplate<class T>void output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20];\nint k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);}\ntemplate<class T>void outputs(T t){output(t);}\ntemplate<class S,class ...T>void outputs(S a,T...t){output(a,32);outputs(t...);}\ntemplate<class T>void output(T *a,int n){REP(i,n)output(a[i],i!=n-1?',':10);}\ntemplate<class T>void output(T *a,int n,int m){REP(i,n)output(a[i],m);}\ntemplate<class T>bool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;);\nif(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;}\ntemplate<class S,class ...T>bool input(S&a,T&...t){input(a);return input(t...);}\n\nconst int MAX_V = 100000;\nconst int MAX_E = 100000;\n// T : define {T+T, T<T, int*T}\ntemplate<class T> \nstruct MinCostFlow {\n struct Edge {\n int s,t,cap;\n T cost;\n int next;\n Edge(){}\n Edge(int s, int t, int cap, T cost, int next) : s(s),t(t),cap(cap),cost(cost),next(next) {}\n };\n int n;\n Edge edge[MAX_E+10];\n int head[MAX_V+10];\n int cnt;\n void add_edge(int s, int t, int cap, T cost) {\n edge[cnt] = Edge(s,t,cap,cost,head[s]); head[s] = cnt++;\n edge[cnt] = Edge(t,s,0,-cost,head[t]); head[t] = cnt++;\n }\n T zero, inf;\n void init(int _n, T _zero, T _inf) {\n n = _n;\n REP(i,n) head[i] = 0;\n zero = _zero;\n inf = _inf;\n cnt = 2;\n }\n T dis[MAX_V];\n int inQ[MAX_V],pre[MAX_V];\n bool spfa(int s, int t) {\n queue<int> Q;\n REP(i,n) dis[i] = inf, inQ[i] = 0;\n for (dis[s]=zero,inQ[s]=1,Q.push(s); !Q.empty(); ) {\n int v = Q.front(); Q.pop();\n inQ[v] = 0;\n for (int k=head[v]; k; k=edge[k].next) {\n int u = edge[k].t;\n if (edge[k].cap && chmin(dis[u],dis[v]+edge[k].cost)) {\n pre[u] = k;\n if (!inQ[u]) inQ[u] = 1, Q.push(u);\n }\n }\n }\n return dis[t] < inf;\n }\n T minCost;\n pair<T,int> run(int s, int t, int F=INF) {\n pair<T,int> res(zero, 0);\n minCost = zero;\n while(F&&spfa(s,t)) {\n int f = F;\n for (int v=t; v!=s; v=edge[pre[v]].s)\n chmin(f, edge[pre[v]].cap);\n for (int v=t; v!=s; v=edge[pre[v]].s) {\n res.first += edge[pre[v]].cost * f;\n edge[pre[v]].cap -= f;\n edge[pre[v]^1].cap += f;\n }\n F -= f;\n res.second += f;\n chmin(minCost, res.first);\n }\n return res;\n }\n};\n\ntemplate<class T, int N>\nstruct Vector {\n T v[N];\n int n;\n T& operator[](int i) { return v[i]; }\n const T operator[](int i) const { return v[i]; }\n Vector() : n(0) {}\n Vector(int n) : n(n) { REP(i,n)v[i]=0; }\n Vector(const initializer_list<T> &u) : n(0) { for(auto&i:u)v[n++]=i; }\n Vector(int n, const T &x) : n(n) { REP(i,n) v[i]=x; }\n Vector& operator+=(const Vector &u) { REP(i,n)v[i]+=u[i]; return *this; }\n Vector& operator-=(const Vector &u) { REP(i,n)v[i]-=u[i]; return *this; }\n Vector operator+(const Vector &u) const { return Vector(*this)+=u; }\n Vector operator-(const Vector &u) const { return Vector(*this)-=u; }\n Vector operator*=(const T &x) { REP(i,n)v[i]*=x; return *this; }\n Vector operator*(const T &x) const { return Vector(*this)*=x; }\n Vector operator-() const { Vector res(n); REP(i,n) res[i]=-v[i]; return res; }\n bool operator<(const Vector &u) const { REP(i,n) if (v[i]!=u[i]) return v[i]<u[i]; return 0; }\n // bool operator>(const Vector &u) const { return u<v; }\n bool operator==(const Vector &u) const { REP(i,n) if (v[i]!=u[i]) return 0; return 1;}\n friend ostream& operator<<(ostream &os, const Vector& v) { return os<<vector<T>{v.v,v.v+v.n}; }\n};\n\ntypedef Vector<int,3> Vec;\n\nint g1[100][100];\nint g2[100][100];\nint g3[100][100];\nint g4[100][100];\n\nvoid inp(int g[100][100]) {\n int L;\n cin >> L;\n REP(i,L) {\n int a,b;\n input(a,b);\n g[a][b] = 1;\n }\n}\n\nint main() {\n int n,m;\n while(input(n,m)) {\n REP(i,n)REP(j,n) {\n g1[i][j]=g2[i][j]=g3[i][j]=g4[i][j]=0;\n }\n inp(g1);\n inp(g2);\n inp(g3);\n inp(g4);\n static MinCostFlow<Vec> mcf;\n mcf.init(n+m+2, {0,0,0}, {INF,INF,INF});\n REP(i,n) REP(j,m) {\n if (g1[i][j] && g3[j][i]) {\n mcf.add_edge(i, n+j, 1, {-1,0,0});\n } else if (g1[i][j] && g4[j][i] || g2[i][j] && g3[j][i]) {\n mcf.add_edge(i, n+j, 1, {0,-1,0});\n } else if (g2[i][j] && g4[j][i]) {\n mcf.add_edge(i, n+j, 1, {0,0,-1});\n }\n }\n REP(i,n) mcf.add_edge(n+m,i,1,{0,0,0});\n REP(i,m) mcf.add_edge(n+i,n+m+1,1,{0,0,0});\n mcf.run(n+m,n+m+1);\n Vec cost = mcf.minCost;\n // cout << cost << endl;\n outputs(-cost[0], -cost[1], -cost[2]);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 6400, "score_of_the_acc": -1, "final_rank": 5 }, { "submission_id": "aoj_1546_1080797", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n#define tpl(...) make_tuple(__VA_ARGS__)\nconst int INF = 0x3f3f3f3f;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T>ostream&operator<<(ostream &o,const vector<T>&t){o<<'[';FOR(i,t){if(i!=t.begin())o<<',';o<<*i;}return o<<']';}\ntemplate<class S,class T>ostream&operator<<(ostream &o,const pair<S,T>&t){return o<<'('<<t.first<<','<<t.second<<')';}\ntemplate<int N,class Tp>void output(ostream&,const Tp&){}\ntemplate<int N,class Tp,class,class ...Ts>void output(ostream &o,const Tp&t){if(N)o<<',';o<<get<N>(t);output<N+1,Tp,Ts...>(o,t);}\ntemplate<class ...Ts>ostream&operator<<(ostream&o,const tuple<Ts...>&t){o<<'(';output<0,tuple<Ts...>,Ts...>(o,t);return o<<')';}\ntemplate<class T>void output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20];\nint k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);}\ntemplate<class T>void outputs(T t){output(t);}\ntemplate<class S,class ...T>void outputs(S a,T...t){output(a,32);outputs(t...);}\ntemplate<class T>void output(T *a,int n){REP(i,n)output(a[i],i!=n-1?',':10);}\ntemplate<class T>void output(T *a,int n,int m){REP(i,n)output(a[i],m);}\ntemplate<class T>bool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;);\nif(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;}\ntemplate<class S,class ...T>bool input(S&a,T&...t){input(a);return input(t...);}\n\ntypedef valarray<int> Weight;\ntemplate<class T>ostream&operator<<(ostream &o,const valarray<T>&t){o<<'[';\n REP(i,t.size()) {if(i!=0)o<<',';o<<t[i];}return o<<']';}\n\nstruct Edge {\n int src, dst;\n int capacity;\n Weight cost;\n Edge(int src, int dst, int capacity, Weight cost) :\n src(src), dst(dst), capacity(capacity), cost(cost) { }\n};\nbool operator<(const Weight &u, const Weight &v) {\n REP(i,u.size()) if (u[i] != v[i]) return u[i] < v[i];\n return 0;\n}\n\nbool operator < (const Edge &e, const Edge &f) {\n return f.cost < e.cost;\n}\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\n\nvoid add_edge(Graph &g, int s, int d, int cap, Weight cost) {\n g[s].push_back(Edge(s,d,cap,cost));\n g[d].push_back(Edge(d,s,0,-cost));\n}\n\n#define RESIDUE(u,v) (capacity[u][v] - flow[u][v])\n#define RCOST(u,v) (cost[u][v] + h[u] - h[v])\n\n// Dijkstr\nWeight minimumCostFlow(const Graph &g, int s, int t) {\n const int n = g.size();\n vector<vector<int> > capacity(n, vector<int>(n)), flow(n, vector<int>(n));\n Matrix cost(n, Array(n, {0,0,0}));\n REP(u,n) FOR(e,g[u]) {\n capacity[e->src][e->dst] += e->capacity;\n cost[e->src][e->dst] += e->cost;\n }\n pair<Weight, int> total(Weight{0,0,0},0); // (cost, flow)\n Weight res({0,0,0});\n vector<Weight> h(n, {INF,INF,INF});\n h[s] = {0,0,0};\n // ベルマンフォードでポテンシャルを求めて負辺に対応\n REP(k, n) REP(i, n) FOR(e,g[i]) if (capacity[e->src][e->dst]) {\n if (Weight(h[e->src] + cost[e->src][e->dst]) < h[e->dst])\n h[e->dst] = h[e->src] + cost[e->src][e->dst];\n }\n for (int F = INF; F > 0; ) { // residual flow\n vector<Weight> d(n, {INF,INF,INF}); d[s] = 0;\n vector<int> p(n, -1);\n priority_queue<Edge> Q;\n for (Q.push(Edge(-2, s, 0, Weight({0,0,0}))); !Q.empty(); ) {\n Edge e = Q.top(); Q.pop();\n if (p[e.dst] != -1) continue;\n p[e.dst] = e.src;\n FOR(f, g[e.dst]) if (RESIDUE(f->src, f->dst) > 0) {\n if (Weight(d[f->src] + RCOST(f->src, f->dst)) < d[f->dst]) {\n d[f->dst] = d[f->src] + RCOST(f->src, f->dst);\n Q.push( Edge(f->src, f->dst, 0, d[f->dst]) );\n }\n }\n }\n // cout << d << endl;\n // cout << p << endl;\n if (p[t] == -1) break; \n int f = F;\n for (int u = t; u != s; u = p[u])\n f = min(f, RESIDUE(p[u], u));\n for (int u = t; u != s; u = p[u]) {\n total.first += f * cost[p[u]][u];\n flow[p[u]][u] += f; flow[u][p[u]] -= f;\n }\n if (total.first < res) {\n res = total.first;\n }\n F -= f;\n total.second += f;\n REP(u, n) if (h[u] < Weight({INF,INF,INF})) h[u] += d[u]; // ifいらない?\n }\n return res;\n}\n\nint g1[100][100];\nint g2[100][100];\nint g3[100][100];\nint g4[100][100];\n\nvoid inp(int g[100][100]) {\n int L;\n cin >> L;\n REP(i,L) {\n int a,b;\n input(a,b);\n g[a][b] = 1;\n }\n}\n\nint main() {\n int n,m;\n while(input(n,m)) {\n REP(i,n)REP(j,n) {\n g1[i][j]=g2[i][j]=g3[i][j]=g4[i][j]=0;\n }\n inp(g1);\n inp(g2);\n inp(g3);\n inp(g4);\n Graph g(n+m+2);\n REP(i,n) REP(j,m) {\n if (g1[i][j] && g3[j][i]) {\n add_edge(g, i, n+j, 1, Weight({-1,0,0}));\n } else if (g1[i][j] && g4[j][i] || g2[i][j] && g3[j][i]) {\n add_edge(g, i, n+j, 1, Weight({0,-1,0}));\n } else if (g2[i][j] && g4[j][i]) {\n add_edge(g, i, n+j, 1, Weight({0,0,-1}));\n }\n }\n REP(i,n) add_edge(g,n+m,i,1,{0,0,0});\n REP(i,m) add_edge(g,n+i,n+m+1,1,{0,0,0});\n Weight cost = minimumCostFlow(g,n+m,n+m+1);\n // cout << cost << endl;\n outputs(-cost[0], -cost[1], -cost[2]);\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3636, "score_of_the_acc": -0.7956, "final_rank": 4 }, { "submission_id": "aoj_1546_1080629", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n#include <array>\n#include <climits>\n#include <bitset>\n\n// BEGIN CUT HERE\n#ifdef _MSC_VER\n#include <agents.h>\n#endif\n// END CUT HERE \n\n#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)\n#define rep(i, n) FOR(i, 0, n)\n#define ALL(v) v.begin(), v.end()\n#define REV(v) v.rbegin(), v.rend()\n#define MEMSET(v, s) memset(v, s, sizeof(v))\n#define MP make_pair\n#define MT make_tuple\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n\nstruct Cost{\n\tvector<int> val;\n\tCost(){\n\t\tval.assign(4, 0);\n\t}\n\tCost(initializer_list<int> il){\n\t\tval = il;\n\t}\n\n\tbool operator < (const Cost &rhs)const{\n\t\treturn val > rhs.val;\n\t}\n\n\tCost operator-() const{\n\t\tCost res = *this;\n\t\tfor (auto &e : res.val) e = -e;\n\t\treturn res;\n\t}\n\n\tCost& operator=(Cost rhs){\n\t\tval = rhs.val;\n\t\treturn *this;\n\t}\n\n\tCost operator+(const Cost &rhs) const {\n\t\tCost res = *this;\n\t\tfor (int i = 0; i < 4; i++) res.val[i] += rhs.val[i];\n\t\treturn res;\n\t}\n\n\tCost operator-(const Cost &rhs) const {\n\t\treturn *this + -rhs;\n\t}\n\n\tCost operator*(const int &x) const {\n\t\tCost res = *this;\n\t\tfor (int i = 0; i < 4; i++) res.val[i] *= x;\n\t\treturn res;\n\t}\n\n\tbool operator==(const Cost &rhs) const{\n\t\treturn val == rhs.val;\n\t}\n};\n\nconst Cost ZERO = { 0, 0, 0, 0 };\nconst Cost INF = { -10000, 0, 0, 0 };\n\nstruct MinCostFlow{\n\tstruct edge{\n\t\tint to, rev, cap;\n\t\tCost cost;\n\t\tedge(int to, Cost cost, int rev, int cap) :to(to), cost(cost), rev(rev), cap(cap){}\n\t};\n\n\tint n;\n\tvector<vector<edge>> G;\n\n\tvoid add(int from, int to, Cost cost, int cap){\n\t\tG[from].push_back(edge(to, cost, G[to].size(), cap));\n\t\tG[to].push_back(edge(from, -cost, G[from].size() - 1, 0));\n\t}\n\tvector<Cost> dist, h;\n\tvector<int> prevv, preve;\n\n\tMinCostFlow(int size){\n\t\tn = size;\n\t\tG.assign(n, vector<edge>());\n\t\tdist.assign(n, ZERO);\n\t\th.assign(n, ZERO);\n\t\tprevv.assign(n, 0);\n\t\tpreve.assign(n, 0);\n\t}\n\n\tCost min_cost_flow(int s, int t, int f){\n\t\tCost res = ZERO;\n\t\trep(i, n) h[i] = ZERO;\n\t\twhile (f){\n\t\t\trep(i, n) dist[i] = INF;\n\t\t\tdist[s] = ZERO;\n\t\t\tpriority_queue<pair<Cost, int>, vector<pair<Cost, int>>, greater<pair<Cost, int>> > q;\n\t\t\tq.push(MP(ZERO, s));\n\t\t\twhile (!q.empty()){\n\t\t\t\tint pos = q.top().second;\n\t\t\t\tCost d = q.top().first;\n\t\t\t\tq.pop();\n\n\t\t\t\tif (dist[pos] < d) continue;\n\n\t\t\t\trep(i, G[pos].size()){\n\t\t\t\t\tedge &e = G[pos][i];\n\t\t\t\t\tif (e.cap <= 0) continue;\n\t\t\t\t\tif (dist[pos] + e.cost + h[pos] - h[e.to] < dist[e.to]){\n\t\t\t\t\t\tdist[e.to] = dist[pos] + e.cost + h[pos] - h[e.to];\n\t\t\t\t\t\tprevv[e.to] = pos;\n\t\t\t\t\t\tpreve[e.to] = i;\n\t\t\t\t\t\tq.push(MP(dist[e.to], e.to));\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t\tif (dist[t] == INF) return INF;\n\n\t\t\trep(i, n) h[i] = h[i] + dist[i];\n\n\t\t\tint d = f;\n\t\t\tfor (int v = t; v != s; v = prevv[v]){\n\t\t\t\td = min(d, G[prevv[v]][preve[v]].cap);\n\t\t\t}\n\t\t\tf -= d;\n\t\t\tres = res + h[t]*d;\n\t\t\tfor (int v = t; v != s; v = prevv[v]){\n\t\t\t\tedge &e = G[prevv[v]][preve[v]];\n\t\t\t\te.cap -= d;\n\t\t\t\tG[e.to][e.rev].cap += d;\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n\t}\n};\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tint n, m;\n\tcin >> n >> m;\n\tvector<vector<int>> mat(n+m, vector<int>(n+m));\n\tint L1, L2, L3, L4;\n\tcin >> L1;\n\twhile (L1--){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tmat[a][n+b] = 2;\n\t}\n\tcin >> L2;\n\twhile (L2--){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tmat[a][n+b] = 1;\n\t}\n\tcin >> L3;\n\twhile (L3--){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tmat[n+a][b] = 2;\n\t}\n\tcin >> L4;\n\twhile (L4--){\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\tmat[n+a][b] = 1;\n\t}\n\n\tMinCostFlow mcf(n+m+2);\n\tint SRC = n + m, SNK = SRC + 1;\n\tmcf.add(SRC, SNK, {0, 0, 0, 1}, 100);\n\n\trep(i, n) mcf.add(SRC, i, ZERO, 1);\n\tFOR(j, n, n + m) mcf.add(j, SNK, ZERO, 1);\n\n\trep(i, n) FOR(j, n, n+m){\n\t\tCost cost;\n\t\tif (mat[i][j]*mat[j][i] == 4){\n\t\t\tcost.val[0] = 1;\n\t\t}\n\t\telse if (mat[i][j]*mat[j][i] == 2){\n\t\t\tcost.val[1] = 1;\n\t\t}\n\t\telse if (mat[i][j]*mat[j][i] == 1){\n\t\t\tcost.val[2] = 1;\n\t\t}\n\t\telse{\n\t\t\tcontinue;\n\t\t}\n\t\tmcf.add(i, j, cost, 1);\n\t}\n\n\tCost res = mcf.min_cost_flow(SRC, SNK, 100);\n\n\tvector<int> ans = res.val;\n\trep(i, 3) cout << (i ? \" \" : \"\") << ans[i];\n\tcout << endl;\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 1792, "score_of_the_acc": -0.6035, "final_rank": 3 }, { "submission_id": "aoj_1546_1080529", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long weight;\ntypedef pair<weight, int> P;\n\nstruct edge {\n int to;\n int cap;\n weight cost;\n int rev;\n edge(int to_, int cap_, weight cost_, int rev_):to(to_), cap(cap_), cost(cost_), rev(rev_) {}\n};\n\nconstexpr weight INF = (1ll << 55);\n\nvector<vector<edge>> G;\n\nvoid init(int V) {\n G.assign(V, vector<edge>());\n}\n\nvoid add_edge(int from, int to, int cap, weight cost) {\n G[from].emplace_back(to, cap, cost, G[to].size());\n G[to].emplace_back(from, 0, -cost, G[from].size() - 1);\n}\n\nweight min_cost_flow(int s, int t, int f) {\n const int n = G.size();\n weight res = 0;\n vector<int> prevv(n), preve(n);\n vector<weight> h(n, 0);\n\n while(f > 0) {\n priority_queue<P, vector<P>, greater<P>> que;\n vector<weight> dist(n, INF);\n dist[s] = 0;\n que.push({0, s});\n\n while(!que.empty()) {\n const auto d = que.top().first;\n const auto v = que.top().second;\n que.pop();\n\n if(dist[v] < d) continue;\n\n for(unsigned i = 0; i < G[v].size(); ++i) {\n\tconst edge &e = G[v][i];\n\tif(e.cap > 0 && dist[e.to] > d + e.cost + h[v] - h[e.to]) {\n\t dist[e.to] = d + e.cost + h[v] - h[e.to];\n\t prevv[e.to] = v;\n\t preve[e.to] = i;\n\t que.push({dist[e.to], e.to});\n\t}\n }\n }\n\n if(dist[t] == INF) return -1;\n\n for(int v = 0; v < n; ++v) {\n h[v] += dist[v];\n }\n\n int d = f;\n for(int v = t; v != s; v = prevv[v]) {\n d = min(d, G[prevv[v]][preve[v]].cap);\n }\n\n f -= d;\n res += d * h[t];\n for(int v = t; v != s; v = prevv[v]) {\n edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n\n return res;\n}\n\nconstexpr weight DD = 1000000;\nconstexpr weight DS = 1000;\nconstexpr weight SS = 1;\n\ninline void input(vector<vector<int>> &like, int v) {\n int l;\n cin >> l;\n\n for(int i = 0; i < l; ++i) {\n int a, b;\n cin >> a >> b;\n like[a][b] = v;\n }\n}\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n vector<vector<int>> like_m(n, vector<int>(m, 0));\n vector<vector<int>> like_f(m, vector<int>(n, 0));\n\n input(like_m, 2);\n input(like_m, 1);\n input(like_f, 2);\n input(like_f, 1);\n\n const int source = n + m;\n const int sink = source + 1;\n init(sink + 1);\n\n for(int i = 0; i < n; ++i) {\n add_edge(source, i, 1, 0);\n }\n\n for(int i = 0; i < m; ++i) {\n add_edge(i + n, sink, 1, 0);\n }\n\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < m; ++j) {\n if(like_m[i][j] && like_f[j][i]) {\n\tconst int sum = like_m[i][j] + like_f[j][i];\n\tswitch(sum) {\n\tcase 4: add_edge(i, j + n, 1, -DD); break;\n\tcase 3: add_edge(i, j + n, 1, -DS); break;\n\tcase 2: add_edge(i, j + n, 1, -SS); break;\n\tdefault: assert(false);\n\t}\n }\n else {\n\tadd_edge(i, j + n, 1, 0);\n }\n }\n }\n\n const weight ans = -min_cost_flow(source, sink, min(n, m));\n const long long dd = ans / DD;\n const long long ds = ans % DD / DS;\n const long long ss = ans % DS;\n\n cout << dd << \" \" << ds << \" \" << ss << endl;\n\n return EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1944, "score_of_the_acc": -0.1331, "final_rank": 2 } ]
aoj_1550_cpp
Problem J: Yu-kun Likes To Play Darts Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいダーツが大好きだ。 そんなゆう君は最近ダーツにはまっていたが、普通のダーツに飽きてしまったため独自のダーツボードを作ることにした。 ダーツについては ダーツ を参照。 Problem ゆう君が考えたダーツの内容は以下の通りだ。 無限に広いダーツボードには得点の書かれた多角形が幾つかある。 プレイヤーはダーツの矢を1本だけ持っている。 プレイヤーは矢を投げ、いずれかの多角形に矢を刺すことでそこに書かれている得点を得る。 それ以外に刺さった場合は得点は得られない。 ゆう君は矢を投げる位置を決めたのだが、正確に刺さるとは限らない。 ダーツボードを2次元平面とし、ゆう君が狙う位置を点( cx , cy )とする。 ゆう君の投げた矢が刺さる場所は、点( cx , cy )を中心とする半径 r の円に含まれる任意の点から一様な確率で選ばれる。 ばれる点の座標は整数である必要はない。 ダーツボードの情報とゆう君が狙う位置( cx , cy )、半径 r が与えれるので、ゆう君が獲得することの出来る得点の期待値を答えよ。 Input n cx cy r 0番目の多角形の情報 1番目の多角形の情報 ... (n-1) 番目の多角形の情報 n はダーツボード上にある多角形の数である。 多角形の情報は以下の形式で与えられる。 p score x 0 y 0 x 1 y 1 ... x (p-1) y (p-1) p は多角形の頂点数を表し、 score は多角形に書かれている得点を表す。 多角形の各線分は( x i , y i )と( x i+1 , y i+1 ) ( i < p -1 )の頂点を繋いだ線分と、( x p-1 , y p-1 )と( x 0 , y 0 )の頂点を繋いだ線分である。 Constraints 入力は以下の条件を満たす。 入力は全て整数で与えられる 1 ≤ n ≤ 50 0 ≤ cx , cy , x , y ≤ 1000 1 ≤ r ≤ 100 3 ≤ p ≤ 10 1 ≤ score ≤ 100 多角形の頂点は、隣り合った頂点を時計回り、または反時計回りに訪問するような順番で与えられる 多角形の辺が別の多角形の辺と共通な点をもつことはない 多角形が別の多角形を内包することはない 矢が多角形の辺上に刺さった場合、得点は得られないものとする Output ゆう君が獲得することの出来る得点の期待値を1行で出力せよ。小数点以下は何桁数字を出力しても構わない。ただし、解答の誤差は0.000001(10 -6 )を超えてはならない。 Sample Input 1 1 2 2 1 4 1 0 0 2 0 2 2 0 2 Sample Output 1 0.2500000000 Sample Input 2 1 2 2 1 4 1 0 0 5 0 5 5 0 5 Sample Output 2 1.0000000000 Sample Input 3 4 3 3 2 3 1 1 1 3 3 1 5 4 2 2 0 5 0 4 2 3 2 3 3 4 3 6 1 6 5 4 4 3 4 4 4 5 6 2 6 Sample Output 3 1.0574955319 Sample Input 4 1 10 10 1 4 10 0 0 1 0 1 1 0 1 Sample Output 4 0.0000000000
[ { "submission_id": "aoj_1550_8296263", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n// ==================================================================== Geometry Library ====================================================================\nstruct Point {\n\tlong double px, py;\n};\n\nstruct Circle {\n\tPoint c;\n\tlong double r;\n};\n\nPoint operator+(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px + a2.px, a1.py + a2.py };\n}\n\nPoint operator-(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px - a2.px, a1.py - a2.py };\n}\n\nPoint operator*(const Point& a1, const long double& a2) {\n\treturn Point{ a1.px * a2, a1.py * a2 };\n}\n\nbool operator<(const Point& a1, const Point& a2) {\n\tif (a1.px < a2.px) return true;\n\treturn false;\n}\n\n// ノルム\nlong double norm(Point p) {\n\treturn p.px * p.px + p.py * p.py;\n}\n\n// 距離\nlong double dist(Point p) {\n\treturn sqrt(norm(p));\n}\n\n// 内積\nlong double dot(Point p1, Point p2) {\n\treturn p1.px * p2.px + p1.py * p2.py;\n}\n\n// 外積\nlong double crs(Point p1, Point p2) {\n\treturn p1.px * p2.py - p1.py * p2.px;\n}\n\n// 角度\nlong double angle(Point p) {\n\treturn atan2(p.py, p.px);\n}\n\n// 射影\nPoint projection(Point p1, Point p2, Point x) {\n\tlong double wari = dot(p2 - p1, x - p1) / norm(p1 - p2);\n\treturn p1 + (p2 - p1) * wari;\n}\n\n// 線分と点の距離\nlong double dist_line(Point p1, Point p2, Point x) {\n\tlong double val = abs(crs(p2 - p1, x - p1));\n\treturn val / dist(p1 - p2);\n}\n\n// 線分と円の交点\nvector<Point> cross_point(Point p1, Point p2, Circle c) {\n\tlong double DST = dist_line(p1, p2, c.c);\n\tif (DST >= c.r - 1.0e-9L) return vector<Point>{};\n\tPoint proj = projection(p1, p2, c.c);\n\tlong double base = angle(proj - c.c); if (DST == 0) base = angle(p2 - p1) + 3.14159265358979L / 2.0L;\n\tlong double diff = acos(DST / c.r);\n\tPoint v1 = c.c + Point{ c.r * cos(base + diff), c.r * sin(base + diff) };\n\tPoint v2 = c.c + Point{ c.r * cos(base - diff), c.r * sin(base - diff) };\n\tvector<Point> List;\n\tif (dist(p1 - v1) <= dist(p1 - p2) + 1.0e-9L && dist(p2 - v1) <= dist(p1 - p2) + 1.0e-9L) List.push_back(v1);\n\tif (dist(p1 - v2) <= dist(p1 - p2) + 1.0e-9L && dist(p2 - v2) <= dist(p1 - p2) + 1.0e-9L) List.push_back(v2);\n\treturn List;\n}\n\n// 多角形の中に点が含まれるかを判定\nbool isinside(vector<Point> Poly, Point P) {\n\tint cnt = 0;\n\tfor (int i = 0; i < Poly.size(); i++) {\n\t\tPoint v1 = Poly[(i + 0) % Poly.size()];\n\t\tPoint v2 = Poly[(i + 1) % Poly.size()];\n\t\tif (v1.py > v2.py) swap(v1, v2);\n\t\tif (!(v1.py < P.py && P.py < v2.py)) continue;\n\t\tlong double expected = v1.px + (v2.px - v1.px) * (P.py - v1.py) / (v2.py - v1.py);\n\t\tif (expected >= P.px) cnt += 1;\n\t}\n\tif (cnt % 2 == 1) return true;\n\treturn false;\n}\n\n// 面積を計算\nlong double Area(vector<Point> Poly) {\n\tlong double r = 0;\n\tfor (int i = 0; i < Poly.size(); i++) {\n\t\tPoint v1 = Poly[(i + 0) % Poly.size()];\n\t\tPoint v2 = Poly[(i + 1) % Poly.size()];\n\t\tr += (v1.px + v2.px) * (v1.py - v2.py);\n\t}\n\treturn abs(r) / 2.0;\n}\n\n// 多角形と円が交わる面積\nlong double GetArea(vector<Point> Poly, Circle C) {\n\tconst int BUNKAI = 200000;\n\tint N = Poly.size();\n\tvector<vector<Point>> Cross(Poly.size(), vector<Point>{});\n\tvector<Point> Takakkei;\n\n\t// Step A. 交点の列挙\n\tfor (int i = 0; i < N; i++) { if (dist(C.c - Poly[(i + 0) % N]) <= C.r) Cross[i].push_back(Poly[(i + 0) % N]); }\n\tfor (int i = 0; i < N; i++) {\n\t\tPoint v1 = Poly[(i + 0) % N];\n\t\tPoint v2 = Poly[(i + 1) % N];\n\t\tvector<Point> ret = cross_point(v1, v2, C);\n\t\tvector<pair<double, Point>> List;\n\t\tfor (Point j : ret) List.push_back(make_pair(dist(v1 - j), j));\n\t\tsort(List.begin(), List.end());\n\t\tfor (int j = 0; j < List.size(); j++) Cross[i].push_back(List[j].second);\n\t}\n\tfor (int i = 0; i < N; i++) { if (dist(C.c - Poly[(i + 1) % N]) <= C.r) Cross[i].push_back(Poly[(i + 1) % N]); }\n\n\t// Step B. 例外ケースの判定\n\tbool Flag = false;\n\tfor (int i = 0; i < N; i++) {\n\t\tif (Cross[i].size() >= 1) Flag = true;\n\t}\n\tif (Flag == false) {\n\t\tPoint base = C.c + Point{ C.r * cos(1.0), C.r * sin(1.0) };\n\t\tif (isinside(Poly, base) == true) return C.r * C.r * 3.14159265358979L;\n\t\treturn 0.0;\n\t}\n\n\t// Step C. 最後の交点の角度を求める\n\tlong double PrevAngle = 0;\n\tint PrevPlace = 0;\n\tfor (int i = N - 1; i >= 0; i--) {\n\t\tif (Cross[i].size() == 0) continue;\n\t\tPoint tmp = Cross[i][Cross[i].size() - 1];\n\t\tPrevAngle = angle(tmp - C.c);\n\t\tPrevPlace = i + 1;\n\t\tbreak;\n\t}\n\n\t// Step D. 多角形の近似を求める\n\tfor (int i = 0; i < N; i++) {\n\t\tif (Cross[i].size() >= 1 && dist(C.c - Poly[i]) > C.r) {\n\t\t\tint cl = PrevPlace, cr = i; if (cl > cr) cr += N;\n\t\t\tlong double AngleS = PrevAngle;\n\t\t\tlong double AngleT = angle(Cross[i][0] - C.c);\n\t\t\tvector<Point> Vec;\n\t\t\tVec.push_back(C.c + Point{ C.r * cos(PrevAngle), C.r * sin(PrevAngle) });\n\t\t\tfor (int j = cl; j <= cr; j++) Vec.push_back(Poly[j % N]);\n\t\t\tVec.push_back(Cross[i][0]);\n\n\t\t\t// 角度の合計を計算\n\t\t\tlong double kakudosum = 0;\n\t\t\tfor (int j = 0; j < Vec.size() - 1; j++) {\n\t\t\t\tdouble A1 = angle(Vec[j + 0] - C.c);\n\t\t\t\tdouble A2 = angle(Vec[j + 1] - C.c);\n\t\t\t\twhile (A2 - A1 >= +3.14159265358979L) A2 -= 2.0L * 3.14159265358979L;\n\t\t\t\twhile (A2 - A1 <= -3.14159265358979L) A2 += 2.0L * 3.14159265358979L;\n\t\t\t\tkakudosum += (A2 - A1);\n\t\t\t}\n\t\t\tif (kakudosum > 0.0 && AngleS > AngleT) AngleT += 2.0L * 3.14159265358979L;\n\t\t\tif (kakudosum < 0.0 && AngleS < AngleT) AngleT -= 2.0L * 3.14159265358979L;\n\t\t\twhile (min(AngleS, AngleT) < 0.0) { AngleS += 2.0L * 3.14159265358979L; AngleT += 2.0L * 3.14159265358979L; }\n\t\t\tint c1 = 1.0 * BUNKAI * AngleS / (2.0L * 3.14159265358979L);\n\t\t\tint c2 = 1.0 * BUNKAI * AngleT / (2.0L * 3.14159265358979L);\n\n\t\t\t// 多角形の追加\n\t\t\tif (c1 <= c2) {\n\t\t\t\tfor (int j = c1; j <= c2; j++) {\n\t\t\t\t\tlong double px = C.c.px + C.r * cos(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tlong double py = C.c.py + C.r * sin(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tTakakkei.push_back(Point{ px, py });\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfor (int j = c1; j >= c2; j--) {\n\t\t\t\t\tlong double px = C.c.px + C.r * cos(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tlong double py = C.c.py + C.r * sin(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tTakakkei.push_back(Point{ px, py });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 一般の場合\n\t\tfor (Point j : Cross[i]) {\n\t\t\tTakakkei.push_back(j);\n\t\t\tPrevAngle = angle(j - C.c);\n\t\t\tPrevPlace = i + 1;\n\t\t}\n\t}\n\n\t// 面積を返す\n\tlong double Return = Area(Takakkei);\n\treturn Return;\n}\n\n// ==================================================================== Main Part ====================================================================\nint N, Score[59];\nint tmp;\nCircle Target;\nvector<Point> Dart[59];\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> Target.c.px >> Target.c.py >> Target.r;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> tmp >> Score[i];\n\t\tDart[i] = vector<Point>(tmp, Point{ 0.0L, 0.0L });\n\t\tfor (int j = 0; j < tmp; j++) cin >> Dart[i][j].px >> Dart[i][j].py;\n\t}\n\n\t// Step 2. Find Answer\n\tlong double TotalScore = 0.0;\n\tfor (int i = 0; i < N; i++) {\n\t\tlong double score1 = GetArea(Dart[i], Target);\n\t\tlong double score2 = Score[i];\n\t\tTotalScore += score1 * score2;\n\t}\n\n\t// Step 3. Output\n\tlong double Area_Circle = Target.r * Target.r * 3.14159265358979L;\n\tprintf(\"%.12Lf\\n\", TotalScore / Area_Circle);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 10480, "score_of_the_acc": -0.0554, "final_rank": 1 }, { "submission_id": "aoj_1550_8296226", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n// ==================================================================== Geometry Library ====================================================================\nstruct Point {\n\tlong double px, py;\n};\n\nstruct Circle {\n\tPoint c;\n\tlong double r;\n};\n\nPoint operator+(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px + a2.px, a1.py + a2.py };\n}\n\nPoint operator-(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px - a2.px, a1.py - a2.py };\n}\n\nPoint operator*(const Point& a1, const long double& a2) {\n\treturn Point{ a1.px * a2, a1.py * a2 };\n}\n\nbool operator<(const Point& a1, const Point& a2) {\n\tif (a1.px < a2.px) return true;\n\treturn false;\n}\n\n// ノルム\nlong double norm(Point p) {\n\treturn p.px * p.px + p.py * p.py;\n}\n\n// 距離\nlong double dist(Point p) {\n\treturn sqrt(norm(p));\n}\n\n// 内積\nlong double dot(Point p1, Point p2) {\n\treturn p1.px * p2.px + p1.py * p2.py;\n}\n\n// 外積\nlong double crs(Point p1, Point p2) {\n\treturn p1.px * p2.py - p1.py * p2.px;\n}\n\n// 角度\nlong double angle(Point p) {\n\treturn atan2(p.py, p.px);\n}\n\n// 射影\nPoint projection(Point p1, Point p2, Point x) {\n\tlong double wari = dot(p2 - p1, x - p1) / norm(p1 - p2);\n\treturn p1 + (p2 - p1) * wari;\n}\n\n// 線分と点の距離\nlong double dist_line(Point p1, Point p2, Point x) {\n\tlong double val = abs(crs(p2 - p1, x - p1));\n\treturn val / dist(p1 - p2);\n}\n\n// 線分と円の交点\nvector<Point> cross_point(Point p1, Point p2, Circle c) {\n\tlong double DST = dist_line(p1, p2, c.c);\n\tif (DST >= c.r - 1.0e-9L) return vector<Point>{};\n\tPoint proj = projection(p1, p2, c.c);\n\tlong double base = angle(proj - c.c); if (DST == 0) base = angle(p2 - p1) + 3.14159265358979L / 2.0L;\n\tlong double diff = acos(DST / c.r);\n\tPoint v1 = c.c + Point{ c.r * cos(base + diff), c.r * sin(base + diff) };\n\tPoint v2 = c.c + Point{ c.r * cos(base - diff), c.r * sin(base - diff) };\n\tvector<Point> List;\n\tif (dist(p1 - v1) <= dist(p1 - p2) + 1.0e-9L && dist(p2 - v1) <= dist(p1 - p2) + 1.0e-9L) List.push_back(v1);\n\tif (dist(p1 - v2) <= dist(p1 - p2) + 1.0e-9L && dist(p2 - v2) <= dist(p1 - p2) + 1.0e-9L) List.push_back(v2);\n\treturn List;\n}\n\n// 多角形の中に点が含まれるかを判定\nbool isinside(vector<Point> Poly, Point P) {\n\tint cnt = 0;\n\tfor (int i = 0; i < Poly.size(); i++) {\n\t\tPoint v1 = Poly[(i + 0) % Poly.size()];\n\t\tPoint v2 = Poly[(i + 1) % Poly.size()];\n\t\tif (v1.py > v2.py) swap(v1, v2);\n\t\tif (!(v1.py < P.py && P.py < v2.py)) continue;\n\t\tlong double expected = v1.px + (v2.px - v1.px) * (P.py - v1.py) / (v2.py - v1.py);\n\t\tif (expected >= P.px) cnt += 1;\n\t}\n\tif (cnt % 2 == 1) return true;\n\treturn false;\n}\n\n// 面積を計算\nlong double Area(vector<Point> Poly) {\n\tlong double r = 0;\n\tfor (int i = 0; i < Poly.size(); i++) {\n\t\tPoint v1 = Poly[(i + 0) % Poly.size()];\n\t\tPoint v2 = Poly[(i + 1) % Poly.size()];\n\t\tr += (v1.px + v2.px) * (v1.py - v2.py);\n\t}\n\treturn abs(r) / 2.0;\n}\n\n// 多角形と円が交わる面積\nlong double GetArea(vector<Point> Poly, Circle C) {\n\tconst int BUNKAI = 200000;\n\tint N = Poly.size();\n\tvector<vector<Point>> Cross(Poly.size(), vector<Point>{});\n\tvector<Point> Takakkei;\n\n\t// Step A. 交点の列挙\n\tfor (int i = 0; i < N; i++) { if (dist(C.c - Poly[(i + 0) % N]) <= C.r) Cross[i].push_back(Poly[(i + 0) % N]); }\n\tfor (int i = 0; i < N; i++) {\n\t\tPoint v1 = Poly[(i + 0) % N];\n\t\tPoint v2 = Poly[(i + 1) % N];\n\t\tvector<Point> ret = cross_point(v1, v2, C);\n\t\tvector<pair<double, Point>> List;\n\t\tfor (Point j : ret) List.push_back(make_pair(dist(v1 - j), j));\n\t\tsort(List.begin(), List.end());\n\t\tfor (int j = 0; j < List.size(); j++) Cross[i].push_back(List[j].second);\n\t}\n\tfor (int i = 0; i < N; i++) { if (dist(C.c - Poly[(i + 1) % N]) <= C.r) Cross[i].push_back(Poly[(i + 1) % N]); }\n\n\t// Step B. 例外ケースの判定\n\tbool Flag = false;\n\tfor (int i = 0; i < N; i++) {\n\t\tif (Cross[i].size() >= 1) Flag = true;\n\t}\n\tif (Flag == false) {\n\t\tPoint base = C.c + Point{ C.r * cos(1.0), C.r * sin(1.0) };\n\t\tif (isinside(Poly, base) == true) return C.r * C.r * 3.14159265358979L;\n\t\treturn 0.0;\n\t}\n\n\t// Step C. 最後の交点の角度を求める\n\tlong double PrevAngle = 0;\n\tint PrevPlace = 0;\n\tfor (int i = N - 1; i >= 0; i--) {\n\t\tif (Cross[i].size() == 0) continue;\n\t\tPoint tmp = Cross[i][Cross[i].size() - 1];\n\t\tPrevAngle = angle(tmp - C.c);\n\t\tPrevPlace = i + 1;\n\t\tbreak;\n\t}\n\n\t// Step D. 多角形の近似を求める\n\tfor (int i = 0; i < N; i++) {\n\t\tif (Cross[i].size() >= 1 && dist(C.c - Poly[i]) > C.r) {\n\t\t\tint cl = PrevPlace, cr = i; if (cl > cr) cr += N;\n\t\t\tlong double AngleS = PrevAngle;\n\t\t\tlong double AngleT = angle(Cross[i][0] - C.c);\n\t\t\tlong double kakudosum = 0;\n\t\t\tfor (int j = cl; j < cr; j++) {\n\t\t\t\tdouble A1 = angle(Poly[(j + 0) % N] - C.c);\n\t\t\t\tdouble A2 = angle(Poly[(j + 1) % N] - C.c);\n\t\t\t\twhile (A2 - A1 >= +3.14159265358979L) A2 -= 2.0L * 3.14159265358979L;\n\t\t\t\twhile (A2 - A1 <= -3.14159265358979L) A2 += 2.0L * 3.14159265358979L;\n\t\t\t\tkakudosum += (A2 - A1);\n\t\t\t}\n\t\t\tif (kakudosum > 0.0 && AngleS > AngleT) AngleT += 2.0L * 3.14159265358979L;\n\t\t\tif (kakudosum < 0.0 && AngleS < AngleT) AngleT -= 2.0L * 3.14159265358979L;\n\t\t\twhile (min(AngleS, AngleT) < 0.0) { AngleS += 2.0L * 3.14159265358979L; AngleT += 2.0L * 3.14159265358979L; }\n\t\t\tint c1 = 1.0 * BUNKAI * AngleS / (2.0L * 3.14159265358979L);\n\t\t\tint c2 = 1.0 * BUNKAI * AngleT / (2.0L * 3.14159265358979L);\n\n\t\t\t// 多角形の追加\n\t\t\tif (c1 <= c2) {\n\t\t\t\tfor (int j = c1; j <= c2; j++) {\n\t\t\t\t\tlong double px = C.c.px + C.r * cos(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tlong double py = C.c.py + C.r * sin(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tTakakkei.push_back(Point{ px, py });\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfor (int j = c1; j >= c2; j--) {\n\t\t\t\t\tlong double px = C.c.px + C.r * cos(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tlong double py = C.c.py + C.r * sin(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tTakakkei.push_back(Point{ px, py });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 一般の場合\n\t\tfor (Point j : Cross[i]) {\n\t\t\tTakakkei.push_back(j);\n\t\t\tPrevAngle = angle(j - C.c);\n\t\t\tPrevPlace = i + 1;\n\t\t}\n\t}\n\n\t// 面積を返す\n\tlong double Return = Area(Takakkei);\n\treturn Return;\n}\n\n// ==================================================================== Main Part ====================================================================\nint N, Score[59];\nint tmp;\nCircle Target;\nvector<Point> Dart[59];\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> Target.c.px >> Target.c.py >> Target.r;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> tmp >> Score[i];\n\t\tDart[i] = vector<Point>(tmp, Point{ 0.0L, 0.0L });\n\t\tfor (int j = 0; j < tmp; j++) cin >> Dart[i][j].px >> Dart[i][j].py;\n\t}\n\n\t// Step 2. Find Answer\n\tlong double TotalScore = 0.0;\n\tfor (int i = 0; i < N; i++) {\n\t\tlong double score1 = GetArea(Dart[i], Target);\n\t\tlong double score2 = Score[i];\n\t\tTotalScore += score1 * score2;\n\t}\n\n\t// Step 3. Output\n\tlong double Area_Circle = Target.r * Target.r * 3.14159265358979L;\n\tprintf(\"%.12Lf\\n\", TotalScore / Area_Circle);\n\treturn 0;\n}", "accuracy": 0.6153846153846154, "time_ms": 30, "memory_kb": 17712, "score_of_the_acc": -2, "final_rank": 2 }, { "submission_id": "aoj_1550_8296081", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n// ==================================================================== Geometry Library ====================================================================\nstruct Point {\n\tlong double px, py;\n};\n\nstruct Circle {\n\tPoint c;\n\tlong double r;\n};\n\nPoint operator+(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px + a2.px, a1.py + a2.py };\n}\n\nPoint operator-(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px - a2.px, a1.py - a2.py };\n}\n\nPoint operator*(const Point& a1, const long double& a2) {\n\treturn Point{ a1.px * a2, a1.py * a2 };\n}\n\nbool operator<(const Point& a1, const Point& a2) {\n\tif (a1.px < a2.px) return true;\n\treturn false;\n}\n\n// ノルム\nlong double norm(Point p) {\n\treturn p.px * p.px + p.py * p.py;\n}\n\n// 距離\nlong double dist(Point p) {\n\treturn sqrt(norm(p));\n}\n\n// 内積\nlong double dot(Point p1, Point p2) {\n\treturn p1.px * p2.px + p1.py * p2.py;\n}\n\n// 外積\nlong double crs(Point p1, Point p2) {\n\treturn p1.px * p2.py - p1.py * p2.px;\n}\n\n// 角度\nlong double angle(Point p) {\n\treturn atan2(p.py, p.px);\n}\n\n// 射影\nPoint projection(Point p1, Point p2, Point x) {\n\tlong double wari = dot(p2 - p1, x - p1) / norm(p1 - p2);\n\treturn p1 + (p2 - p1) * wari;\n}\n\n// 線分と点の距離\nlong double dist_line(Point p1, Point p2, Point x) {\n\tlong double val = abs(crs(p2 - p1, x - p1));\n\treturn val / dist(p1 - p2);\n}\n\n// 線分と円の交点\nvector<Point> cross_point(Point p1, Point p2, Circle c) {\n\tlong double DST = dist_line(p1, p2, c.c);\n\tif (DST >= c.r - 1.0e-9L) return vector<Point>{};\n\tPoint proj = projection(p1, p2, c.c);\n\tlong double base = angle(proj - c.c); if (DST == 0) base = angle(p2 - p1) + 3.14159265358979L / 2.0L;\n\tlong double diff = acos(DST / c.r);\n\tPoint v1 = c.c + Point{ c.r * cos(base + diff), c.r * sin(base + diff) };\n\tPoint v2 = c.c + Point{ c.r * cos(base - diff), c.r * sin(base - diff) };\n\tvector<Point> List;\n\tif (dist(p1 - v1) <= dist(p1 - p2) + 1.0e-9L && dist(p2 - v1) <= dist(p1 - p2) + 1.0e-9L) List.push_back(v1);\n\tif (dist(p1 - v2) <= dist(p1 - p2) + 1.0e-9L && dist(p2 - v2) <= dist(p1 - p2) + 1.0e-9L) List.push_back(v2);\n\treturn List;\n}\n\n// 多角形の中に点が含まれるかを判定\nbool isinside(vector<Point> Poly, Point P) {\n\tint cnt = 0;\n\tfor (int i = 0; i < Poly.size(); i++) {\n\t\tPoint v1 = Poly[(i + 0) % Poly.size()];\n\t\tPoint v2 = Poly[(i + 1) % Poly.size()];\n\t\tif (v1.py > v2.py) swap(v1, v2);\n\t\tif (!(v1.py < P.py && P.py < v2.py)) continue;\n\t\tlong double expected = v1.px + (v2.px - v1.px) * (P.py - v1.py) / (v2.py - v1.py);\n\t\tif (expected >= P.px) cnt += 1;\n\t}\n\tif (cnt % 2 == 1) return true;\n\treturn false;\n}\n\n// 面積を計算\nlong double Area(vector<Point> Poly) {\n\tlong double r = 0;\n\tfor (int i = 0; i < Poly.size(); i++) {\n\t\tPoint v1 = Poly[(i + 0) % Poly.size()];\n\t\tPoint v2 = Poly[(i + 1) % Poly.size()];\n\t\tr += (v1.px + v2.px) * (v1.py - v2.py);\n\t}\n\treturn abs(r) / 2.0;\n}\n\n// 多角形と円が交わる面積\nlong double GetArea(vector<Point> Poly, Circle C) {\n\tconst int BUNKAI = 200000;\n\tint N = Poly.size();\n\tvector<vector<Point>> Cross(Poly.size(), vector<Point>{});\n\tvector<Point> Takakkei;\n\n\t// Step A. 交点の列挙\n\tfor (int i = 0; i < N; i++) { if (dist(C.c - Poly[(i + 0) % N]) <= C.r) Cross[i].push_back(Poly[(i + 0) % N]); }\n\tfor (int i = 0; i < N; i++) {\n\t\tPoint v1 = Poly[(i + 0) % N];\n\t\tPoint v2 = Poly[(i + 1) % N];\n\t\tvector<Point> ret = cross_point(v1, v2, C);\n\t\tvector<pair<double, Point>> List;\n\t\tfor (Point j : ret) List.push_back(make_pair(dist(v1 - j), j));\n\t\tsort(List.begin(), List.end());\n\t\tfor (int j = 0; j < List.size(); j++) Cross[i].push_back(List[j].second);\n\t}\n\tfor (int i = 0; i < N; i++) { if (dist(C.c - Poly[(i + 1) % N]) <= C.r) Cross[i].push_back(Poly[(i + 1) % N]); }\n\n\t// Step B. 例外ケースの判定\n\tbool Flag = false;\n\tfor (int i = 0; i < N; i++) {\n\t\tif (Cross[i].size() >= 1) Flag = true;\n\t}\n\tif (Flag == false) {\n\t\tPoint base = C.c + Point{ C.r * cos(1.0), C.r * sin(1.0) };\n\t\tif (isinside(Poly, base) == true) return C.r * C.r * 3.14159265358979L;\n\t\treturn 0.0;\n\t}\n\n\t// Step C. 最後の交点の角度を求める\n\tdouble PrevAngle = 0;\n\tint PrevPlace = 0;\n\tfor (int i = N - 1; i >= 0; i--) {\n\t\tif (Cross[i].size() == 0) continue;\n\t\tPoint tmp = Cross[i][Cross[i].size() - 1];\n\t\tPrevAngle = angle(tmp - C.c);\n\t\tPrevPlace = i + 1;\n\t\tbreak;\n\t}\n\n\t// Step D. 多角形の近似を求める\n\tfor (int i = 0; i < N; i++) {\n\t\tif (Cross[i].size() >= 1 && dist(C.c - Poly[i]) > C.r) {\n\t\t\tint cl = PrevPlace, cr = i; if (cl > cr) cr += N;\n\t\t\tlong double AngleS = PrevAngle;\n\t\t\tlong double AngleT = angle(Cross[i][0] - C.c);\n\t\t\tlong double kakudosum = 0;\n\t\t\tfor (int j = cl; j < cr; j++) {\n\t\t\t\tdouble A1 = angle(Poly[(j + 0) % N] - C.c);\n\t\t\t\tdouble A2 = angle(Poly[(j + 1) % N] - C.c);\n\t\t\t\twhile (A2 - A1 >= +3.14159265358979L) A2 -= 2.0L * 3.14159265358979L;\n\t\t\t\twhile (A2 - A1 <= -3.14159265358979L) A2 += 2.0L * 3.14159265358979L;\n\t\t\t\tkakudosum += (A2 - A1);\n\t\t\t}\n\t\t\tif (kakudosum > 0.0 && AngleS > AngleT) AngleT += 2.0L * 3.14159265358979L;\n\t\t\tif (kakudosum < 0.0 && AngleS < AngleT) AngleT -= 2.0L * 3.14159265358979L;\n\t\t\twhile (min(AngleS, AngleT) < 0.0) { AngleS += 2.0L * 3.14159265358979L; AngleT += 2.0L * 3.14159265358979L; }\n\t\t\tint c1 = 1.0 * BUNKAI * AngleS / (2.0 * 3.14159265358979);\n\t\t\tint c2 = 1.0 * BUNKAI * AngleT / (2.0 * 3.14159265358979);\n\n\t\t\t// 多角形の追加\n\t\t\tif (c1 <= c2) {\n\t\t\t\tfor (int j = c1; j <= c2; j++) {\n\t\t\t\t\tlong double px = C.c.px + C.r * cos(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tlong double py = C.c.py + C.r * sin(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tTakakkei.push_back(Point{ px, py });\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfor (int j = c2; j >= c1; j--) {\n\t\t\t\t\tlong double px = C.c.px + C.r * cos(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tlong double py = C.c.py + C.r * sin(2.0L * 3.14159265358979L * j / BUNKAI);\n\t\t\t\t\tTakakkei.push_back(Point{ px, py });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 一般の場合\n\t\tfor (Point j : Cross[i]) {\n\t\t\tTakakkei.push_back(j);\n\t\t\tPrevAngle = angle(j - C.c);\n\t\t\tPrevPlace = i + 1;\n\t\t}\n\t}\n\n\t// 面積を返す\n\tlong double Return = Area(Takakkei);\n\treturn Return;\n}\n\n// ==================================================================== Main Part ====================================================================\nint N, Score[59];\nint tmp;\nCircle Target;\nvector<Point> Dart[59];\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> Target.c.px >> Target.c.py >> Target.r;\n\tfor (int i = 0; i < N; i++) {\n\t\tcin >> tmp >> Score[i];\n\t\tDart[i] = vector<Point>(tmp, Point{ 0.0L, 0.0L });\n\t\tfor (int j = 0; j < tmp; j++) cin >> Dart[i][j].px >> Dart[i][j].py;\n\t}\n\n\t// Step 2. Find Answer\n\tlong double TotalScore = 0.0;\n\tfor (int i = 0; i < N; i++) {\n\t\tlong double score1 = GetArea(Dart[i], Target);\n\t\tlong double score2 = Score[i];\n\t\tTotalScore += score1 * score2;\n\t}\n\n\t// Step 3. Output\n\tlong double Area_Circle = Target.r * Target.r * 3.14159265358979L;\n\tprintf(\"%.12Lf\\n\", TotalScore / Area_Circle);\n\treturn 0;\n}", "accuracy": 0.38461538461538464, "time_ms": 20, "memory_kb": 10056, "score_of_the_acc": 0, "final_rank": 3 } ]
aoj_1547_cpp
Problem G: Yu-kun Likes People Watching Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい人を観察することが大好きだ。 そんなゆう君は、去年の夏休みに作成した人の移動経路のデータと建物の位置情報を観察してあることを疑問に思った。 全ての人が建物内にいる時間の長さのうち、最長のものはどのくらいなのだろうか? Problem 建物と人の移動経路の情報が与えられる。 全ての人が連続して建物内にいるような時間のうち、最も長いものの長さを出力せよ。 そのような時間が存在しない場合は長さは0とする。 建物と人はそれぞれ2次元平面上の多角形と点として表される。 人は単位時間あたり x 軸方向と y 軸方向にそれぞれ速度 vx と vy で移動する。 人を表す点が建物を表す多角形内にある場合、その人は建物内にいるものとする。 ( 多角形の辺上に人がいる場合も建物内にいるものとする ) 図1は、Sample Input 2 における建物と人の移動経路を表している。多角形は建物、黒い丸は人の初期位置、矢印はその人の移動経路を表す。 時刻2から時刻4、時刻9から時刻12の間に人は建物内にいるため、建物内に全ての人がいる時間の最長は3となる。 図1: Sample Input 2 Input n m T 建物0の情報 建物1の情報 ... 建物 n-1 の情報 人0の移動経路 人1の移動経路 ... 人 m-1 の移動経路 n , m , T はそれぞれ建物を表す多角形の数、人の数、人が移動する時間の上限を表す。 建物の情報は以下のような形式で与えられる。 ( x i , y i )と( x i+1 , y i+1 )の頂点を繋いだ線分が多角形の一辺である。ただし、 p-1 番目の頂点とは、0番目の頂点が繋がる。 p x 0 y 0 x 1 y 1 ... x p-1 y p-1 人の経路情報は以下のような形式で与えられる。 t 0 sx 0 sy 0 ( t 0 = 0 ) t 1 vx 1 vy 1 t 2 vx 2 vy 2 ... t k vx k vy k ( t k = T ) 人は2次元平面上を移動する。 ( sx 0 , sy 0 ) は人が時刻0にいる位置である。 時刻 t i-1 から t i の間( 0 < i ≤ k ), x 軸方向と y 軸方向にそれぞれ速度 vx i と vy i で移動する。 Constraints 入力は以下の条件を満たす。 入力は全て整数として与えられる 1 ≤ n , m ≤ 10 3 ≤ p ≤ 10 1 ≤ T ≤ 3600 0 ≤ x , y , sx , sy ≤ 1000 0 = t 0 < t 1 < ... < t k = T -10 ≤ vx 1 , vy 1 ,..., vx k , vy k ≤ 10 建物を表す多角形は時計回りまたは反時計周りで与えられる 多角形の連続する3つの頂点は、一直線上に並ばないものとする 人の位置は常に0以上1000以下である 同じ時刻に2人以上の人が同じ座標にいても良い 建物が別の建物を内包するようなことはない 建物を表す多角形の辺が別の建物を表す多角形の辺と共通な部分をもつことはない Output 全ての人が建物内にいるような時間のうち、その長さが最長のものの長さを1行で出力せよ。 そのような時間がない場合、長さは0とする。 小数点以下は何桁数字を出力しても構わない。ただし、解答の誤差は0.000001(10 -6 )を超えてはならない。 Sample Input 1 1 1 10 4 3 0 7 0 7 10 3 10 0 0 5 10 1 0 Sample Output 1 4.0000000000 Sample Input 2 1 1 14 4 2 3 6 3 6 6 2 6 0 0 4 3 1 0 6 0 -1 7 1 0 10 0 1 14 1 0 Sample Output 2 3.0000000000 Sample Input 3 2 2 7 4 2 2 5 2 5 5 2 5 4 7 2 10 2 10 5 7 5 0 0 4 3 1 0 7 0 -1 0 7 7 2 1 0 5 0 -1 7 1 0 Sample Output 3 1.0000000000 Sample Input 4 1 3 10 9 2 3 3 6 4 3 5 6 6 3 7 6 8 3 8 7 2 7 0 1 5 10 1 0 0 11 4 10 -1 0 0 4 8 6 0 -1 8 1 0 10 0 1 Sample Output 4 0.3333333333 Sample Input 5 1 2 7 4 2 1 6 1 6 5 2 5 0 1 4 7 1 0 0 9 3 7 0 1 Sample Output 5 0.0000000000
[ { "submission_id": "aoj_1547_10334747", "code_snippet": "// AOJ #1547 Yu-kun Likes People Watching\n// 2025.3.29\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst double EPS = 1e-9;\n\nbool inPoly(double x, double y, const vector<pair<double,double>> &poly) {\n int cnt = 0;\n int sz = poly.size();\n for (int i = 0; i < sz; i++) {\n auto [x1, y1] = poly[i];\n auto [x2, y2] = poly[(i+1)%sz];\n double cross = (x - x1)*(y2 - y1) - (y - y1)*(x2 - x1);\n if (fabs(cross) < EPS && min(x1,x2) - EPS <= x && x <= max(x1,x2) + EPS &&\n min(y1,y2) - EPS <= y && y <= max(y1,y2) + EPS)\n return true;\n bool cond1 = (y1 <= y && y < y2) || (y2 <= y && y < y1);\n if(cond1) {\n double xint = x1 + (y - y1) * (x2 - x1) / (y2 - y1);\n if(x < xint) cnt++;\n }\n }\n return (cnt % 2) == 1;\n}\n\nstruct Intv { double s, e; };\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int n, m, T;\n cin >> n >> m >> T;\n vector<vector<pair<double,double>>> bld(n);\n for(int i=0;i<n;i++){\n int p;\n cin >> p;\n bld[i].resize(p);\n for (int j = 0; j < p; j++) cin >> bld[i][j].first >> bld[i][j].second;\n }\n\n vector<vector<Intv>> P(m);\n for(int i=0;i<m;i++){\n double t0, sx, sy;\n cin >> t0 >> sx >> sy;\n double cur_t = t0, cur_x = sx, cur_y = sy;\n vector<Intv> iv;\n while(true) {\n double nt, vx, vy;\n if(!(cin >> nt >> vx >> vy)) break;\n double st = cur_t, ed = nt;\n vector<double> ev;\n ev.push_back(st);\n ev.push_back(ed);\n for(auto &poly : bld) {\n int sz = poly.size();\n for (int j = 0; j < sz; j++){\n double ax = poly[j].first, ay = poly[j].second;\n double bx = poly[(j+1)%sz].first, by = poly[(j+1)%sz].second;\n double dX = bx - ax, dY = by - ay;\n double D = vy*dX - vx*dY;\n if(fabs(D) < EPS) continue;\n double dt = ( - (ax - cur_x)*dY + (ay - cur_y)*dX ) / D;\n double u = ( vx*(ay - cur_y) - vy*(ax - cur_x) ) / D;\n if(dt < -EPS || dt > ed - st + EPS) continue;\n if(u < -EPS || u > 1+EPS) continue;\n double t_int = st + dt;\n if(t_int < st - EPS || t_int > ed + EPS) continue;\n ev.push_back(t_int);\n }\n }\n sort(ev.begin(), ev.end());\n ev.erase(unique(ev.begin(), ev.end(), [&](double a, double b){ return fabs(a-b) < EPS; }), ev.end());\n for (size_t k=0; k+1 < ev.size(); k++){\n double a = ev[k], b = ev[k+1];\n if(b - a < EPS) continue;\n double mid = (a+b)/2;\n double x = cur_x + vx*(mid - st);\n double y = cur_y + vy*(mid - st);\n bool inside = false;\n for(auto &poly : bld) {\n if(inPoly(x,y, poly)) { inside = true; break; }\n }\n if(inside)\n iv.push_back({a, b});\n }\n cur_x += vx*(ed - st);\n cur_y += vy*(ed - st);\n cur_t = ed;\n if(fabs(cur_t - T) < EPS) break;\n }\n sort(iv.begin(), iv.end(), [](const Intv &a, const Intv &b){ return a.s < b.s; });\n vector<Intv> mv;\n for(auto &e: iv) {\n if(mv.empty() || e.s > mv.back().e + EPS) mv.push_back(e);\n else mv.back().e = max(mv.back().e, e.e);\n }\n P[i] = mv;\n }\n\n vector<Intv> cur = P[0];\n for (int i = 1; i < m; i++){\n vector<Intv> nxt;\n int a = 0, b = 0;\n while(a < cur.size() && b < P[i].size()){\n double s = max(cur[a].s, P[i][b].s);\n double e = min(cur[a].e, P[i][b].e);\n if(e - s > EPS) nxt.push_back({s, e});\n if(cur[a].e < P[i][b].e) a++;\n else b++;\n }\n cur = nxt;\n }\n double ans = 0;\n for(auto &e: cur) ans = max(ans, e.e - e.s);\n cout << fixed << setprecision(10) << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3724, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1547_10157231", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <iomanip>\n#include <assert.h>\n\nusing namespace std;\n\ntemplate<typename T>\nstruct Point{\n T x,y;\n \n Point(T x, T y):x(x), y(y){}\n Point(){}\n template<typename Y>\n Point(const Point<Y>& pt):x(pt.x), y(pt.y){}\n \n Point operator-(const Point& other)const{\n return Point(x-other.x, y-other.y);\n }\n \n T operator%(const Point& other) const {\n return x * other.y - y * other.x; \n }\n \n T operator*(const Point& other) const {\n return x * other.x + y * other.y; \n }\n Point operator*(T v) const {\n return Point(x * v, y * v); \n }\n \n void print(){\n cout << \"(\" << x << \",\" << y << \") \"; \n }\n};\n\ntemplate<typename T>\nT area(Point<T> a, Point<T> b, Point<T> c){\n return (c-a)%(b-a); \n}\n\ntypedef vector<Point<long long int> > polygon;\n\nlong long int area(const polygon& p){\n long long int ans = 0;\n \n for (int i=1;i+1<p.size();i++){\n ans += area(p[0], p[i], p[i+1]); \n }\n \n return ans;\n}\n\n\ntemplate<typename T>\nbool on_segment(Point<T> a, Point<T> b, Point<T> c){\n return area(a,b,c) == 0 && (c-b)*(a-b) <= 0;\n}\n\n\ntemplate<typename T> bool inside_triangle(Point<T> a, Point<T> b, Point<T> c, Point<T> x){\n T area_ = abs(area(a,b,c));\n T area1 = abs(area(a,b,x)) + abs(area(b,c,x)) + abs(area(a,c,x));\n \n return area_ == area1;\n}\n\ntemplate<> bool inside_triangle(Point<double> a, Point<double> b, Point<double> c, Point<double> x){\n double area_ = abs(area(a,b,c));\n double area1 = abs(area(a,b,x)) + abs(area(b,c,x)) + abs(area(a,c,x));\n \n return abs(area_ - area1) < 1e-8; \n}\n\nvector<polygon> triangulize(const polygon& p){\n int n = p.size();\n \n long long int area_sign = area(p) > 0 ? 1 : -1;\n \n if (n == 3){\n if ((p[2]-p[0]) % (p[1]-p[0]))\n return {p}; \n else\n return {};\n }\n \n for (int i=0;i<n;i++){\n int j = (i+1)%n, jj = (i+n-1)%n;\n \n long long int triangle_area_sign = area(p[jj],p[i], p[j]) > 0 ? 1 :-1;\n \n auto ok = [&](){\n for (int x=0;x<n;x++){\n if (x != i && x != j && x != jj){\n if (inside_triangle(p[jj], p[i], p[j], p[x])){\n return false;\n }\n }\n }\n return true;\n };\n \n if (triangle_area_sign == area_sign){\n if (ok()){\n polygon q;\n for (int x=0;x<n;x++){\n if (x != i){\n q.push_back(p[x]); \n }\n }\n auto tri = triangulize(q);\n\n\n if ((p[i]-p[j]) % (p[jj]-p[j]))\n tri.push_back({p[jj], p[i], p[j]});\n \n \n return tri;\n }\n }\n \n \n }\n}\n\ndouble cross(Point<long long int> A, Point<long long int> B, Point<long long int> X, Point<long long int> Y){\n if ( ((B-A)%(X-A)) * ((B-A)%(Y-A)) <= 0 && ((X-Y)%(A-Y)) * ((X-Y)%(B-Y)) < 0 ){\n /*\n \n (B.y-A.y)x+(A.x-B.x)y-(B.y-A.y)B.x-(A.x-B.x)B.y=0\n (Y.y-X.y)x+(X.x-Y.x)y-(Y.y-X.y)Y.x-(X.x-Y.x)Y.y=0\n\n ax+by+c=0\n dx+ey+f=0\n\n y = (cd-af)/(ae-bd)\n x = (ce-bf)/(ae-bd)\n \n\n */\n \n long long int a = B.y - A.y, b = A.x - B.x, c = -((B.y-A.y)*B.x+(A.x-B.x)*B.y);\n long long int d = Y.y - X.y, e = X.x - Y.x, f = -((Y.y-X.y)*Y.x+(X.x-Y.x)*Y.y);\n\n double yy = (c*d-a*f)/(e*1.0*a-b*1.0*d);\n double xx = (b*f-c*e)/(e*1.0*a-b*1.0*d);\n\n if (abs(yy-A.y) > 1e-7){\n return (yy-A.y)/(B.y-A.y);\n } else {\n return (xx-A.x)/(B.x-A.x);\n }\n\n } else {\n return -1;\n }\n}\n\nint T;\n\npair<double,double> entrance(long long int t1, long long int t2, Point<long long int> A, Point<long long int> B, const polygon& p){\n bool insideA = inside_triangle(p[0], p[1], p[2], A);\n bool insideB = inside_triangle(p[0], p[1], p[2], B);\n\n if (insideA && insideB) return {-!!t1, t2 == T ? T : -1};\n\n if (insideA ^ insideB){\n Point<double> aa(A), bb(B), p0(p[0]), p1(p[1]), p2(p[2]);\n double l = 0, r = 1;\n\n for (int t=0;t<70;t++){\n double m = (l+r)/2;\n Point<double> mm = aa - (bb-aa) * (-m);\n\n if (insideA == inside_triangle(p0,p1,p2,mm))\n l = m;\n else\n r = m;\n\n }\n\n if (insideA){\n return {-!!t1, t1 + (t2-t1) * r};\n } else {\n return {t1 + (t2-t1) * l, t2 == T ? T : -1};\n }\n\n } else {\n double cross1 = cross(A,B,p[0], p[1]);\n double cross2 = cross(A,B,p[1], p[2]);\n double cross3 = cross(A,B,p[2], p[0]);\n\n vector<double> crosses;\n\n if (cross1 != -1) crosses.push_back(t1 + (t2-t1)*cross1);\n if (cross2 != -1) crosses.push_back(t1 + (t2-t1)*cross2);\n if (cross3 != -1) crosses.push_back(t1 + (t2-t1)*cross3);\n\n assert(crosses.size() != 1);\n\n if (crosses.empty())\n return {-1, -1};\n\n sort(crosses.begin(), crosses.end());\n \n return {crosses[0], crosses.back()};\n\n }\n\n}\n\nstruct event{\n double t;\n int who;\n bool entered;\n\n bool operator<(const event& other){\n return t < other.t;\n }\n};\n\nint main(){\n int n,m;\n cin >> n >> m >> T;\n\n vector<polygon> triangles;\n\n for (int i=0;i<n;i++){\n polygon p;\n int k;\n cin >> k;\n\n for (int i=0;i<k;i++){\n int x,y;\n cin >> x >> y;\n\n p.push_back(Point<long long int>(x, y));\n }\n\n for (auto &tri:triangulize(p)) {\n triangles.push_back(tri);\n }\n }\n\n vector<event> events;\n\n\n for (int i=0;i<m;i++){\n long long int t0, x,y;\n cin >> t0 >> x >> y;\n\n while(1){\n long long int t,vx,vy;\n cin >> t >> vx >> vy;\n\n Point<long long int> A(x, y);\n Point<long long int> B(x + vx * (t-t0), y + vy * (t-t0));\n\n for (auto &tri:triangles){\n auto [enter, leave] = entrance(t0, t, A, B, tri);\n\n if (enter != -1){ \n events.push_back(event{enter-1e-8, i, 1});\n }\n if (leave != -1){\n\n events.push_back(event{leave+1e-8, i, 0});\n }\n\n }\n\n if (t == T){\n break; \n } else {\n x = x + vx * (t-t0);\n y = y + vy * (t-t0);\n t0 = t;\n }\n }\n\n }\n\n vector<int> people_inside(m,0);\n sort(events.begin(), events.end());\n \n double ans = 0;\n double consec_inside = 0;\n double L = 0;\n\n for (auto &e:events){\n bool inside_before = 1;\n for (auto &x:people_inside)\n if (!x)\n inside_before = 0;\n\n \n if (inside_before){\n consec_inside += e.t - L;\n ans = max(ans, consec_inside);\n } else {\n consec_inside = 0;\n }\n\n if (e.entered)\n ++people_inside[e.who];\n else\n --people_inside[e.who];\n\n L = e.t;\n }\n\n cout << std::fixed << setprecision(11) << ans << endl;\n\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 4184, "score_of_the_acc": -0.5283, "final_rank": 2 }, { "submission_id": "aoj_1547_10157225", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <iomanip>\n#include <assert.h>\n\nusing namespace std;\n\ntemplate<typename T>\nstruct Point{\n T x,y;\n \n Point(T x, T y):x(x), y(y){}\n Point(){}\n template<typename Y>\n Point(const Point<Y>& pt):x(pt.x), y(pt.y){}\n \n Point operator-(const Point& other)const{\n return Point(x-other.x, y-other.y);\n }\n \n T operator%(const Point& other) const {\n return x * other.y - y * other.x; \n }\n \n T operator*(const Point& other) const {\n return x * other.x + y * other.y; \n }\n Point operator*(T v) const {\n return Point(x * v, y * v); \n }\n \n void print(){\n cout << \"(\" << x << \",\" << y << \") \"; \n }\n};\n\ntemplate<typename T>\nT area(Point<T> a, Point<T> b, Point<T> c){\n return (c-a)%(b-a); \n}\n\ntypedef vector<Point<long long int> > polygon;\n\nlong long int area(const polygon& p){\n long long int ans = 0;\n \n for (int i=1;i+1<p.size();i++){\n ans += area(p[0], p[i], p[i+1]); \n }\n \n return ans;\n}\n\n\ntemplate<typename T>\nbool on_segment(Point<T> a, Point<T> b, Point<T> c){\n return area(a,b,c) == 0 && (c-b)*(a-b) <= 0;\n}\n\n\ntemplate<typename T> bool inside_triangle(Point<T> a, Point<T> b, Point<T> c, Point<T> x){\n T area_ = abs(area(a,b,c));\n T area1 = abs(area(a,b,x)) + abs(area(b,c,x)) + abs(area(a,c,x));\n \n return area_ == area1;\n}\n\ntemplate<> bool inside_triangle(Point<double> a, Point<double> b, Point<double> c, Point<double> x){\n double area_ = abs(area(a,b,c));\n double area1 = abs(area(a,b,x)) + abs(area(b,c,x)) + abs(area(a,c,x));\n \n return abs(area_ - area1) < 1e-8; \n}\n\nvector<polygon> triangulize(const polygon& p){\n int n = p.size();\n \n long long int area_sign = area(p) > 0 ? 1 : -1;\n \n if (n == 3){\n if ((p[2]-p[0]) % (p[1]-p[0]))\n return {p}; \n else\n return {};\n }\n \n for (int i=0;i<n;i++){\n int j = (i+1)%n, jj = (i+n-1)%n;\n \n long long int triangle_area_sign = area(p[jj],p[i], p[j]) > 0 ? 1 :-1;\n \n auto ok = [&](){\n for (int x=0;x<n;x++){\n if (x != i && x != j && x != jj){\n if (inside_triangle(p[jj], p[i], p[j], p[x])){\n return false;\n }\n }\n }\n return true;\n };\n \n if (triangle_area_sign == area_sign){\n if (ok()){\n polygon q;\n for (int x=0;x<n;x++){\n if (x != i){\n q.push_back(p[x]); \n }\n }\n auto tri = triangulize(q);\n\n\n if ((p[i]-p[j]) % (p[jj]-p[j]))\n tri.push_back({p[jj], p[i], p[j]});\n \n \n return tri;\n }\n }\n \n \n }\n}\n\ndouble cross(Point<long long int> A, Point<long long int> B, Point<long long int> X, Point<long long int> Y){\n if ( ((B-A)%(X-A)) * ((B-A)%(Y-A)) <= 0 && ((X-Y)%(A-Y)) * ((X-Y)%(B-Y)) < 0 ){\n /*\n \n (B.y-A.y)x+(A.x-B.x)y-(B.y-A.y)B.x-(A.x-B.x)B.y=0\n (Y.y-X.y)x+(X.x-Y.x)y-(Y.y-X.y)Y.x-(X.x-Y.x)Y.y=0\n\n ax+by+c=0\n dx+ey+f=0\n\n y = (cd-af)/(ae-bd)\n x = (ce-bf)/(ae-bd)\n \n\n */\n \n long long int a = B.y - A.y, b = A.x - B.x, c = -((B.y-A.y)*B.x+(A.x-B.x)*B.y);\n long long int d = Y.y - X.y, e = X.x - Y.x, f = -((Y.y-X.y)*Y.x+(X.x-Y.x)*Y.y);\n\n double yy = (c*d-a*f)/(e*1.0*a-b*1.0*d);\n double xx = (b*f-c*e)/(e*1.0*a-b*1.0*d);\n\n if (abs(yy-A.y) > 1e-7){\n return (yy-A.y)/(B.y-A.y);\n } else {\n return (xx-A.x)/(B.x-A.x);\n }\n\n } else {\n return -1;\n }\n}\n\nint T;\n\npair<double,double> entrance(long long int t1, long long int t2, Point<long long int> A, Point<long long int> B, const polygon& p){\n bool insideA = inside_triangle(p[0], p[1], p[2], A);\n bool insideB = inside_triangle(p[0], p[1], p[2], B);\n\n if (insideA && insideB) return {-!!t1, t2 == T ? T : -1};\n\n if (insideA ^ insideB){\n Point<double> aa(A), bb(B), p0(p[0]), p1(p[1]), p2(p[2]);\n double l = 0, r = 1;\n\n for (int t=0;t<70;t++){\n double m = (l+r)/2;\n Point<double> mm = aa - (bb-aa) * (-m);\n\n if (insideA == inside_triangle(p0,p1,p2,mm))\n l = m;\n else\n r = m;\n\n }\n\n if (insideA){\n return {-!!t1, t1 + (t2-t1) * r};\n } else {\n return {t1 + (t2-t1) * l, t2 == T ? T : -1};\n }\n\n } else {\n double cross1 = cross(A,B,p[0], p[1]);\n double cross2 = cross(A,B,p[1], p[2]);\n double cross3 = cross(A,B,p[2], p[0]);\n\n vector<double> crosses;\n\n if (cross1 != -1) crosses.push_back(t1 + (t2-t1)*cross1);\n if (cross2 != -1) crosses.push_back(t1 + (t2-t1)*cross2);\n if (cross3 != -1) crosses.push_back(t1 + (t2-t1)*cross3);\n\n assert(crosses.size() != 1);\n\n if (crosses.empty())\n return {-1, -1};\n\n sort(crosses.begin(), crosses.end());\n \n return {crosses[0], crosses.back()};\n\n }\n\n}\n\nstruct event{\n double t;\n int who;\n bool entered;\n\n bool operator<(const event& other){\n return t < other.t;\n }\n};\n\nint main(){\n int n,m;\n cin >> n >> m >> T;\n\n vector<polygon> triangles;\n\n for (int i=0;i<n;i++){\n polygon p;\n int k;\n cin >> k;\n\n for (int i=0;i<k;i++){\n int x,y;\n cin >> x >> y;\n\n p.push_back(Point<long long int>(x, y));\n }\n\n for (auto &tri:triangulize(p)) {\n triangles.push_back(tri);\n }\n }\n\n vector<event> events;\n\n\n for (int i=0;i<m;i++){\n long long int t0, x,y;\n cin >> t0 >> x >> y;\n\n while(1){\n long long int t,vx,vy;\n cin >> t >> vx >> vy;\n\n Point<long long int> A(x, y);\n Point<long long int> B(x + vx * (t-t0), y + vy * (t-t0));\n\n for (auto &tri:triangles){\n auto [enter, leave] = entrance(t0, t, A, B, tri);\n\n if (enter != -1){ \n events.push_back(event{enter-1e-8, i, 1});\n }\n if (leave != -1){\n\n events.push_back(event{leave+1e-8, i, 0});\n }\n\n }\n\n if (t == T){\n break; \n } else {\n x = x + vx * (t-t0);\n y = y + vy * (t-t0);\n t0 = t;\n }\n }\n\n }\n\n vector<int> people_inside(m,0);\n sort(events.begin(), events.end());\n \n double ans = 0;\n double consec_inside = 0;\n double L = 0;\n\n for (auto &e:events){\n bool inside_before = 1;\n for (auto &x:people_inside)\n if (!x)\n inside_before = 0;\n\n \n if (inside_before){\n consec_inside += e.t - L;\n ans = max(ans, consec_inside);\n } else {\n consec_inside = 0;\n }\n\n if (e.entered)\n ++people_inside[e.who];\n else\n --people_inside[e.who];\n\n L = e.t;\n }\n\n cout << setprecision(11) << ans << endl;\n\n}", "accuracy": 0.9117647058823529, "time_ms": 90, "memory_kb": 4048, "score_of_the_acc": -0.4777, "final_rank": 4 }, { "submission_id": "aoj_1547_8297112", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n// ==================================================================== Geometry Library ====================================================================\nstruct Point {\n\tlong double px, py;\n};\n\nPoint operator+(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px + a2.px, a1.py + a2.py };\n}\n\nPoint operator-(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px - a2.px, a1.py - a2.py };\n}\n\nPoint operator*(const Point& a1, const long double& a2) {\n\treturn Point{ a1.px * a2, a1.py * a2 };\n}\n\nbool operator<(const Point& a1, const Point& a2) {\n\tif (a1.px < a2.px) return true;\n\treturn false;\n}\n\n// ノルム\nlong double norm(Point p) {\n\treturn p.px * p.px + p.py * p.py;\n}\n\n// 距離\nlong double dist(Point p) {\n\treturn sqrt(norm(p));\n}\n\n// 内積\nlong double dot(Point p1, Point p2) {\n\treturn p1.px * p2.px + p1.py * p2.py;\n}\n\n// 外積\nlong double crs(Point p1, Point p2) {\n\treturn p1.px * p2.py - p1.py * p2.px;\n}\n\n// 交点を求める\npair<bool, Point> Intersection(Point a1, Point a2, Point b1, Point b2) {\n\tlong double p1 = crs(b1 - a1, a2 - a1);\n\tlong double p2 = crs(b2 - a1, a2 - a1);\n\tlong double p3 = crs(a1 - b1, b2 - b1);\n\tlong double p4 = crs(a2 - b1, b2 - b1);\n\tif (p1 > 0 && p2 > 0) return make_pair(false, Point{ 0.0, 0.0 });\n\tif (p1 < 0 && p2 < 0) return make_pair(false, Point{ 0.0, 0.0 });\n\tif (p3 > 0 && p4 > 0) return make_pair(false, Point{ 0.0, 0.0 });\n\tif (p3 < 0 && p4 < 0) return make_pair(false, Point{ 0.0, 0.0 });\n\tif (p3 == p4) return make_pair(true, Point{ 1e9, 1e9 }); // 平行な場合のコーナーケース\n\tPoint r = a1 + (a2 - a1) * ((0.0 - p3) / (p4 - p3));\n\treturn make_pair(true, r);\n}\n\n// AOJ の ccw からコピー\nint ccw(Point p1, Point p2, Point p3) {\n\tPoint v1 = p2 - p1;\n\tPoint v2 = p3 - p1;\n\tif (crs(v1, v2) > +1.0e-9L) return +1;\n\tif (crs(v1, v2) < -1.0e-9L) return -1;\n\tif (dot(v1, v2) < -1.0e-9L) return 2;\n\tif (norm(v1) < norm(v2)) return -2;\n\treturn 0;\n}\n\n// 内部に入っているかの情報\nbool Inside(vector<Point> Poly, Point P) {\n\tif (P.px == 6.0) {\n\t\tP.px += 0.0;\n\t}\n\tint N = Poly.size();\n\tint cnt = 0;\n\tfor (int i = 0; i < N; i++) {\n\t\tPoint v1 = (Poly[(i + 0) % N] - P);\n\t\tPoint v2 = (Poly[(i + 1) % N] - P);\n\t\tif (abs(crs(v1, v2)) < 1.0e-9L && dot(v1, v2) < 1.0e-9L) return true;\n\t\tif (v1.py > v2.py) swap(v1, v2);\n\t\tif (v1.py < 1.0e-9L && v2.py > 1.0e-9L && crs(v1, v2) > 1.0e-9L) cnt += 1;\n\t}\n\tif (cnt % 2 == 1) return true;\n\treturn false;\n}\n\n\n// ==================================================================== Main Part ====================================================================\nint N; vector<Point> Build[19];\nint M; vector<pair<long double, Point>> Move[19];\nint Sum[1 << 19];\nvector<pair<long double, long double>> Range[19][19];\nvector<pair<long double, long double>> Range2[19];\nlong double T;\n\nvector<pair<long double, long double>> Hantei(vector<Point> Poly, vector<pair<long double, Point>> Human) {\n\tvector<pair<long double, Point>> Cand;\n\n\t// 交点を列挙\n\tfor (int i = 0; i < Human.size(); i++) Cand.push_back(Human[i]);\n\tfor (int i = 0; i < Human.size() - 1; i++) {\n\t\tPoint p1 = Human[i + 0].second;\n\t\tPoint p2 = Human[i + 1].second;\n\t\tfor (int j = 0; j < Poly.size(); j++) {\n\t\t\tPoint v1 = Poly[(j + 0) % Poly.size()];\n\t\t\tPoint v2 = Poly[(j + 1) % Poly.size()];\n\t\t\tpair<bool, Point> ret = Intersection(v1, v2, p1, p2);\n\t\t\tif (ret.first == false) continue;\n\t\t\tif (ret.second.px > 1e8) {\n\t\t\t\tlong double tm1 = Human[i + 0].first + (Human[i + 1].first - Human[i + 0].first) * dist(v1 - p1) / dist(p1 - p2);\n\t\t\t\tlong double tm2 = Human[i + 0].first + (Human[i + 1].first - Human[i + 0].first) * dist(v2 - p1) / dist(p1 - p2);\n\t\t\t\tif (dist(p1 - v1) < dist(p1 - p2) && dist(p2 - v1) < dist(p1 - p2)) Cand.push_back(make_pair(tm1, v1));\n\t\t\t\tif (dist(p1 - v2) < dist(p1 - p2) && dist(p2 - v1) < dist(p1 - p2)) Cand.push_back(make_pair(tm1, v2));\n\t\t\t}\n\t\t\telse {\n\t\t\t\tlong double tm1 = Human[i + 0].first + (Human[i + 1].first - Human[i + 0].first) * dist(ret.second - p1) / dist(p1 - p2);\n\t\t\t\tCand.push_back(make_pair(tm1, ret.second));\n\t\t\t}\n\t\t}\n\t}\n\tsort(Cand.begin(), Cand.end());\n\n\t// 中に入っている区間を求める\n\tvector<pair<long double, long double>> vec;\n\tfor (int i = 0; i < Cand.size() - 1; i++) {\n\t\tPoint p1 = Cand[i + 0].second;\n\t\tPoint p2 = Cand[i + 1].second;\n\t\tPoint md = (p1 + p2) * 0.5L;\n\t\tif (Inside(Poly, md) == false) continue;\n\t\tvec.push_back(make_pair(Cand[i + 0].first, Cand[i + 1].first + 1.0e-9L));\n\t}\n\treturn vec;\n}\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M >> T;\n\tfor (int i = 0; i < N; i++) {\n\t\tint p; cin >> p;\n\t\tfor (int j = 0; j < p; j++) {\n\t\t\tlong double ex, ey; cin >> ex >> ey;\n\t\t\tBuild[i].push_back(Point{ ex, ey });\n\t\t}\n\t}\n\tfor (int i = 0; i < M; i++) {\n\t\tlong double sx = 0, sy = 0;\n\t\twhile (true) {\n\t\t\tlong double t, ex, ey; cin >> t >> ex >> ey;\n\t\t\tif (Move[i].size() == 0) { sx = ex; sy = ey; }\n\t\t\telse { long double tm = t - Move[i][Move[i].size() - 1].first; sx += ex * tm; sy += ey * tm; }\n\t\t\tMove[i].push_back(make_pair(t, Point{ sx, sy }));\n\t\t\tif (t == T) break;\n\t\t}\n\t}\n\n\t// Step 2. Enumerate\n\tfor (int i = 0; i < M; i++) {\n\t\tfor (int j = 0; j < N; j++) Range[i][j] = Hantei(Build[j], Move[i]);\n\t}\n\n\t// Step 3. Get Range\n\tfor (int i = 0; i < M; i++) {\n\t\tvector<pair<long double, long double>> r;\n\t\tfor (int j = 0; j < N; j++) {\n\t\t\tfor (int k = 0; k < Range[i][j].size(); k++) r.push_back(Range[i][j][k]);\n\t\t}\n\t\tsort(r.begin(), r.end());\n\t\tif (r.size() == 0) continue;\n\t\tlong double lft = r[0].first, maxn = -1.0;\n\t\tfor (int j = 0; j < r.size(); j++) {\n\t\t\tmaxn = max(maxn, r[j].second);\n\t\t\tif (j == r.size() - 1 || maxn < r[j + 1].first) {\n\t\t\t\tRange2[i].push_back(make_pair(lft, maxn));\n\t\t\t\tif (j != r.size() - 1) lft = r[j + 1].first;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Step 4. Compression\n\tvector<long double> Zahyou;\n\tfor (int i = 0; i < M; i++) {\n\t\tfor (int j = 0; j < Range2[i].size(); j++) Zahyou.push_back(Range2[i][j].first);\n\t\tfor (int j = 0; j < Range2[i].size(); j++) Zahyou.push_back(Range2[i][j].second);\n\t}\n\tsort(Zahyou.begin(), Zahyou.end());\n\tZahyou.erase(unique(Zahyou.begin(), Zahyou.end()), Zahyou.end());\n\tfor (int i = 0; i < M; i++) {\n\t\tfor (int j = 0; j < Range2[i].size(); j++) {\n\t\t\tint pos1 = lower_bound(Zahyou.begin(), Zahyou.end(), Range2[i][j].first) - Zahyou.begin();\n\t\t\tint pos2 = lower_bound(Zahyou.begin(), Zahyou.end(), Range2[i][j].second) - Zahyou.begin();\n\t\t\tSum[pos1] += 1;\n\t\t\tSum[pos2] -= 1;\n\t\t}\n\t}\n\tfor (int i = 1; i <= Zahyou.size(); i++) Sum[i] += Sum[i - 1];\n\n\t// Step 5. Get Answer\n\tlong double Answer = 0;\n\tlong double Cons = 0;\n\tfor (int i = 0; i < (int)Zahyou.size() - 1; i++) {\n\t\tif (Sum[i] != M) { Cons = 0; continue; }\n\t\tCons += (Zahyou[i + 1] - Zahyou[i]);\n\t\tAnswer = max(Answer, Cons);\n\t}\n\tprintf(\"%.12Lf\\n\", Answer);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 6412, "score_of_the_acc": -2, "final_rank": 3 } ]
aoj_1544_cpp
Problem D: Smell Searcher Problem 廃津大学では強烈な香りを放つ N 個の廃材が一列に並んでいます。 廃材には1から N の番号が順番にふられていて、 i 番目の廃材は強さ a i の香りを放っています。 リヒト君は仕事で、すべての廃材の放つ香りの総和を求めるよう依頼されました。 香りの総和が M 以上になると「大変きつい仕事」とみなされ特別支給金がもらえます。 この仕事を行うために、リヒト君は精度 R の香り検出器を使います。 精度 R の香り検出器を使うと i 番目の廃材の香りを測ろうとすると同時に i − R , i − R +1,..., i −1, i , i +1,..., i + R −1, i + R 番目の廃材の香りも検出されます。言い換えると、閉区間[ max( i − R ,1), min( i + R , N ) ] の廃材の香りを検出します。ここで、max( a , b )は a と b の最大値、min( a , b )は a と b の最小値を表します。 ただし、測った廃材の1つ隣の廃材の香りの強さは本来の香りの強さより C 減らされ、2つ隣の廃材の強さ香りは本来の香りの強さより2* C 減らされて検出されます。つまり、 j (0≤ j ≤ R )個隣にある廃材の香りの強さは a i − j * C として検出されます。結果的に、 i 番目の廃材に精度 R の検出器を使うことで検出された香りの強さの最大値が i 番目の廃材の香りの強さとして認識されます。 精度の高い検出器を使うとその分費用がかかるため、リヒト君は検出器が認識した1から N 番目の廃材の香りの総和が M 以上になる最低の精度 R の値を知りたいと思っています。 Input 入力は以下の形式で与えられる。 N M C a 1 a 2 ... a N 1行目に、1つの整数 N , M , C が空白区切りで与えられる。2行目に N つの整数が空白区切りで与えられる。 a i は i 番目の廃材の香りの強さを表す。 Constraints 入力は以下の制約を満たす。 1 ≤ N ≤ 10 5 1 ≤ M ≤ 10 14 1 ≤ C ≤ 10 9 0 ≤ a i ≤ 10 9 (1 ≤ i ≤ N ) Output 精度 R の検出器を使って認識される廃材の香りの総和を M 以上にしたときの R の最小値を出力せよ。 それが不可能な場合は−1を出力せよ。 R は必ず0以上であり、負の値の精度は存在しない。 Sample Input1 6 25 3 8 5 1 1 2 6 Sample Output1 1 このとき、精度0の検出器を使うと 8 + 5 + 1 + 1 + 2 + 6 = 23 で25以上になりません。 精度1の検出器を使うと max(8,5-3) + max(8-3,5,1-3) + max(5-3,1,1-3) + max(1-3,1,2-3) + max(1-3,2,6-3) + max(2-3,6) = 8 + 5 + 2 + 1 + 3 + 6 = 25 で25以上になるので1が正解です。 Sample Input2 4 10 1 1 2 3 4 Sample Output2 0 Sample Input3 4 11 1 1 2 3 4 Sample Output3 -1
[ { "submission_id": "aoj_1544_10071527", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nlong long computeSum(const vector<long long>& a, long long C, int R, long long M) {\n int N = (int)a.size();\n vector<long long> L(N), Rv(N);\n for(int j = 0; j < N; j++){\n L[j] = a[j] + C * (j+1); \n Rv[j] = a[j] - C * (j+1);\n }\n\n\n vector<long long> leftMax(N), rightMax(N);\n\n deque<int> dq;\n\n for(int i = 0; i < N; i++){\n while(!dq.empty() && dq.front() < i - R) {\n dq.pop_front();\n }\n while(!dq.empty() && L[dq.back()] <= L[i]) {\n dq.pop_back();\n }\n dq.push_back(i);\n leftMax[i] = L[dq.front()];\n }\n\n dq.clear();\n for(int i = N-1; i >= 0; i--){\n while(!dq.empty() && dq.front() > i + R) {\n dq.pop_front();\n }\n while(!dq.empty() && Rv[dq.back()] <= Rv[i]){\n dq.pop_back();\n }\n dq.push_back(i);\n rightMax[i] = Rv[dq.front()];\n }\n\n long long sumVal = 0;\n for(int i = 0; i < N; i++){\n long long valLeft = leftMax[i] - C * (i+1);\n long long valRight = rightMax[i] + C * (i+1);\n long long recognized = max(valLeft, valRight);\n sumVal += recognized;\n\n if(sumVal >= M) {\n return sumVal; \n }\n }\n return sumVal;\n}\n\nint main(){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n long long N, M, C;\n cin >> N >> M >> C;\n vector<long long> a(N);\n for(int i=0; i<(int)N; i++){\n cin >> a[i];\n }\n\n {\n long long valMax = computeSum(a, C, (int)N, M);\n if(valMax < M) {\n cout << -1 << \"\\n\";\n return 0;\n }\n }\n\n int left = 0;\n int right = (int)N; \n int ans = right;\n while(left <= right){\n int mid = (left + right) / 2;\n long long sumVal = computeSum(a, C, mid, M);\n if(sumVal >= M){\n ans = mid;\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n cout << ans << \"\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 7060, "score_of_the_acc": -0.4323, "final_rank": 2 }, { "submission_id": "aoj_1544_10071304", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// Функция, которая по заданному R вычисляет суммарный распознанный запах S(R).\n// Возвращает -1, если сумма превысила типичные границы 64-битного long long,\n// но фактически нам достаточно понять, >= M или нет, так что можно \"обрезать\".\nlong long computeSum(const vector<long long>& a, long long C, int R, long long M) {\n // N = размер массива a\n int N = (int)a.size();\n // Приготовим массивы L_j = a_j + C*j и R_j = a_j - C*j\n // (j индексируем с 1, поэтому удобно завести L[1..N], R[1..N])\n // но в C++ вектор с 0-индексом, просто аккуратно будем обращаться\n vector<long long> L(N), Rv(N);\n for(int j = 0; j < N; j++){\n L[j] = a[j] + C * (j+1); // (j+1) => индекс j в а есть j+1 \"по формуле\"\n Rv[j] = a[j] - C * (j+1);\n }\n\n // Будем находить для каждого i:\n // leftMax[i] = max(L_j) для j в [i-R, i]\n // rightMax[i] = max(R_j) для j в [i, i+R]\n // i пробегаем от 1 до N (но в коде это 0..N-1)\n\n vector<long long> leftMax(N), rightMax(N);\n\n // 1) Считаем leftMax[i] (скользящее окно по L).\n // Окно размера (R+1), но фактически оно \"динамическое\" со сдвигом.\n // Используем дек, храним в нём кандидатов \"j\" (индексы), поддерживаем max.\n deque<int> dq;\n // Сначала разогреваем начальное окно для i=0?\n // Но заметим, что для i=0 (то есть i=1 по условию), мы берем j в [1-R, 1].\n // При R>0 индексы j<1 не существуют, так что effectively это просто j=1.\n // Тем не менее можно аккуратно в общем цикле.\n\n // Идём слева направо: для i от 0 до N-1\n // Хотим, чтобы в dq были индексы j такие, что j <= i, но j>= i-R.\n // Перед тем как перейти к i, убираем из головы dq те j, которые вышли из [i-R, i].\n // Потом добавляем i в dq, выбрасывая \"хуже\" из хвоста.\n for(int i = 0; i < N; i++){\n // Убираем из головы те, кто выпал из диапазона [i-R, i].\n while(!dq.empty() && dq.front() < i - R) {\n dq.pop_front();\n }\n // Добавляем i в дек, выбросив из хвоста тех, у кого L[..] <= L[i]\n while(!dq.empty() && L[dq.back()] <= L[i]) {\n dq.pop_back();\n }\n dq.push_back(i);\n // Теперь dq.front() - индекс, где L[...] максимально на отрезке [i-R, i].\n leftMax[i] = L[dq.front()];\n }\n\n // 2) Аналогично считаем rightMax[i] (но идём справа налево).\n dq.clear();\n for(int i = N-1; i >= 0; i--){\n // Убираем тех, кто вышел из [i, i+R] => т.е. индекс > i+R\n while(!dq.empty() && dq.front() > i + R) {\n dq.pop_front();\n }\n // Добавляем i\n while(!dq.empty() && Rv[dq.back()] <= Rv[i]){\n dq.pop_back();\n }\n dq.push_back(i);\n // dq.front() - индекс, где Rv[...] максимум на [i, i+R].\n rightMax[i] = Rv[dq.front()];\n }\n\n // Теперь считаем суммарно:\n // recognized_i = max( leftMax[i] - C*(i+1), rightMax[i] + C*(i+1) )\n // (т.к. i+1 - это \"настоящий\" индекс в формуле)\n // суммируем.\n long long sumVal = 0;\n for(int i = 0; i < N; i++){\n long long valLeft = leftMax[i] - C * (i+1);\n long long valRight = rightMax[i] + C * (i+1);\n long long recognized = max(valLeft, valRight);\n\n // recognized может быть отрицательным, если a_j очень маленькие и C большое.\n // Но минимальное может быть и 0, если учесть, что если a_j - ... < 0,\n // всё равно берем максимум по j, возможно где-то есть неотрицательное.\n // Однако по условию мы берём именно max(..), который может оказаться <0,\n // тогда это действительно отрицательный вклад.\n // Здесь не сказано явно \"не брать <0\", значит берём как есть.\n\n sumVal += recognized;\n\n // Чтобы не словить переполнение, если sumVal превысит M достаточно,\n // то можно прервать и вернуть что-то (например sumVal), всё равно это >= M.\n if(sumVal >= M) {\n return sumVal; \n }\n }\n return sumVal;\n}\n\nint main(){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n long long N, M, C;\n cin >> N >> M >> C;\n vector<long long> a(N);\n for(int i=0; i<(int)N; i++){\n cin >> a[i];\n }\n\n // Проверим, может ли вообще быть >= M при R = N (максимальном).\n // Если и при R=N не набираем, сразу -1.\n // Иначе делаем бинарный поиск в [0..N].\n {\n long long valMax = computeSum(a, C, (int)N, M);\n if(valMax < M) {\n cout << -1 << \"\\n\";\n return 0;\n }\n }\n\n // Бинарный поиск\n int left = 0;\n int right = (int)N; \n int ans = right;\n while(left <= right){\n int mid = (left + right) / 2;\n long long sumVal = computeSum(a, C, mid, M);\n if(sumVal >= M){\n ans = mid;\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n cout << ans << \"\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 7060, "score_of_the_acc": -0.4323, "final_rank": 2 }, { "submission_id": "aoj_1544_9846997", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 100005\n\nint N = 1;\nll M,C;\nll A[SIZE];\nll max_data[4*SIZE],add_data[4*SIZE];\nll L_max[SIZE],R_max[SIZE];\n\nvoid init(int first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(int left,int right,ll value,int node_id,int node_left,int node_right){\n\n\tif(right < node_left || left > node_right){\n\t\t//範囲外ならreturn\n\t\treturn;\n\t}\n\telse if(left <= node_left && right >= node_right){ //このノードのカバーしている区間が、更新区間の部分区間である場合\n\n\t\tadd_data[node_id] += value; //一様に加える値を加算\n\n\t\twhile(node_id != 0){\n\n\t\t\tnode_id = (node_id-1)/2; //下から上に向かって、最小値および最大値更新\n\t\t\tmax_data[node_id] = max(max_data[2*node_id+1]+add_data[2*node_id+1],max_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t}\n\t}else{\n\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t}\n}\n\nll getMax(int left,int right,int node_id,int node_left,int node_right){\n\tif(right < node_left || left > node_right)return -BIG_NUM;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn max_data[node_id]+add_data[node_id];\n\n\t}else{\n\n\t\tll left_max = getMax(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tll right_max = getMax(left,right,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t\treturn max(left_max,right_max)+add_data[node_id];\n\t}\n}\n\nint main(){\n\n\tint first_N;\n\tscanf(\"%d %lld %lld\",&first_N,&M,&C);\n\n\tll first_sum = 0;\n\n\tfor(int i = 0; i < first_N; i++){\n\n\t\tscanf(\"%lld\",&A[i]);\n\t\tfirst_sum += A[i];\n\t}\n\n\tif(first_sum >= M){\n\n\t\tprintf(\"0\\n\");\n\t\treturn 0;\n\t}\n\n\tinit(first_N);\n\n\tint left = 1,right = first_N,mid = (left+right)/2;\n\tint ans = BIG_NUM;\n\n\t//探索範囲は増えるのは、損にならないので範囲の二分探索ができるはず\n\n\twhile(left <= right){\n\n\t\tfor(int i = 0; i <= 2*N-2; i++){\n\t\t\tmax_data[i] = 0;\n\t\t\tadd_data[i] = 0;\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tadd(i,i,A[i],0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tL_max[i] = -HUGE_NUM;\n\t\t}\n\n\t\t//左から\n\t\tfor(int i = 0; i < first_N; i++){\n\t\t\tif(i > 0){\n\n\t\t\t\tadd(max(0,i-mid),i-1,-C,0,0,N-1);\n\t\t\t}\n\t\t\tL_max[i] = getMax(max(0,i-mid),i,0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i <= 2*N-2; i++){\n\t\t\tmax_data[i] = 0;\n\t\t\tadd_data[i] = 0;\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tadd(i,i,A[i],0,0,N-1);\n\t\t}\n\n\t\tll sum = 0;\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tR_max[i] = -HUGE_NUM;\n\t\t}\n\n\t\t//右から\n\t\tfor(int i = first_N-1; i >= 0; i--){\n\t\t\tif(i < first_N-1){\n\n\t\t\t\tadd(i+1,min(first_N-1,i+mid),-C,0,0,N-1);\n\t\t\t}\n\t\t\tR_max[i] = getMax(i,min(first_N-1,i+mid),0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tsum += max(L_max[i],R_max[i]);\n\t\t}\n\n\t\tif(sum >= M){\n\n\t\t\tans = mid;\n\t\t\tright = mid-1;\n\n\t\t}else{\n\n\t\t\tleft = mid+1;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\n\tif(ans == BIG_NUM){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2260, "memory_kb": 9688, "score_of_the_acc": -1.5879, "final_rank": 8 }, { "submission_id": "aoj_1544_8295641", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nclass RangeMax {\npublic:\n\tint size_ = 1;\n\tvector<long long> dat;\n\n\tvoid init(int sz) {\n\t\tsize_ = 1;\n\t\twhile (size_ <= sz) size_ *= 2;\n\t\tdat.resize(size_ * 2, 0);\n\t}\n\n\tvoid update(int pos, long long x) {\n\t\tpos += size_;\n\t\tdat[pos] = x;\n\t\twhile (pos >= 2) {\n\t\t\tpos >>= 1;\n\t\t\tdat[pos] = max(dat[pos * 2], dat[pos * 2 + 1]);\n\t\t}\n\t}\n\n\tlong long query_(int l, int r, int a, int b, int u) {\n\t\tif (l <= a && b <= r) return dat[u];\n\t\tif (r <= a || b <= l) return -(1LL << 60);\n\t\tlong long v1 = query_(l, r, a, (a + b) / 2, u * 2);\n\t\tlong long v2 = query_(l, r, (a + b) / 2, b, u * 2 + 1);\n\t\treturn max(v1, v2);\n\t}\n\n\tlong long query(int l, int r) {\n\t\treturn query_(l, r, 0, size_, 1);\n\t}\n};\n\nlong long N, A[1 << 18];\nlong long M;\nlong long C;\nRangeMax Left;\nRangeMax Rigt;\n\nbool solve(int border) {\n\tlong long Sum = 0;\n\n\t// Calculate\n\tfor (int i = 1; i <= N; i++) {\n\t\tint cl = max(1, i - border);\n\t\tint cr = min((int)N, i + border);\n\t\tlong long val1 = Left.query(cl, i + 1);\n\t\tlong long val2 = Rigt.query(i, cr + 1);\n\t\tval1 -= 1LL * i * C;\n\t\tval2 += 1LL * i * C;\n\t\tSum += max(val1, val2);\n\t}\n\n\t// Hantei\n\tif (Sum >= M) return true;\n\treturn false;\n}\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M >> C;\n\tfor (int i = 1; i <= N; i++) cin >> A[i];\n\n\t// Step 2. Init\n\tLeft.init(N + 2);\n\tRigt.init(N + 2);\n\tfor (int i = 1; i <= N; i++) Left.update(i, A[i] + 1LL * i * C);\n\tfor (int i = 1; i <= N; i++) Rigt.update(i, A[i] - 1LL * i * C);\n\n\t// Step 3. Binary Search\n\tint dl = 0, dr = N + 1, dm, minx = (1 << 30);\n\tfor (int i = 0; i < 20; i++) {\n\t\tdm = (dl + dr) / 2;\n\t\tbool ret = solve(dm);\n\t\tif (ret == true) { dr = dm; minx = min(minx, dm); }\n\t\telse { dl = dm; }\n\t}\n\n\t// Step 4. Output\n\tif (minx == (1 << 30)) cout << \"-1\" << endl;\n\telse cout << minx << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 8120, "score_of_the_acc": -0.7122, "final_rank": 5 }, { "submission_id": "aoj_1544_4696611", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 100005\n\nint N = 1;\nll M,C;\nll A[SIZE];\nll max_data[4*SIZE],add_data[4*SIZE];\nll L_max[SIZE],R_max[SIZE];\n\nvoid init(int first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(int left,int right,ll value,int node_id,int node_left,int node_right){\n\n\tif(right < node_left || left > node_right){\n\t\t//範囲外ならreturn\n\t\treturn;\n\t}\n\telse if(left <= node_left && right >= node_right){ //このノードのカバーしている区間が、更新区間の部分区間である場合\n\n\t\tadd_data[node_id] += value; //一様に加える値を加算\n\n\t\twhile(node_id != 0){\n\n\t\t\tnode_id = (node_id-1)/2; //下から上に向かって、最小値および最大値更新\n\t\t\tmax_data[node_id] = max(max_data[2*node_id+1]+add_data[2*node_id+1],max_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t}\n\t}else{\n\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t}\n}\n\nll getMax(int left,int right,int node_id,int node_left,int node_right){\n\tif(right < node_left || left > node_right)return -BIG_NUM;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn max_data[node_id]+add_data[node_id];\n\n\t}else{\n\n\t\tll left_max = getMax(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tll right_max = getMax(left,right,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t\treturn max(left_max,right_max)+add_data[node_id];\n\t}\n}\n\nint main(){\n\n\tint first_N;\n\tscanf(\"%d %lld %lld\",&first_N,&M,&C);\n\n\tll first_sum = 0;\n\n\tfor(int i = 0; i < first_N; i++){\n\n\t\tscanf(\"%lld\",&A[i]);\n\t\tfirst_sum += A[i];\n\t}\n\n\tif(first_sum >= M){\n\n\t\tprintf(\"0\\n\");\n\t\treturn 0;\n\t}\n\n\tinit(first_N);\n\n\tint left = 1,right = first_N,mid = (left+right)/2;\n\tint ans = BIG_NUM;\n\n\t//探索範囲は増えるのは、損にならないので範囲の二分探索ができるはず\n\n\twhile(left <= right){\n\n\t\tfor(int i = 0; i <= 2*N-2; i++){\n\t\t\tmax_data[i] = 0;\n\t\t\tadd_data[i] = 0;\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tadd(i,i,A[i],0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tL_max[i] = -HUGE_NUM;\n\t\t}\n\n\t\t//左から\n\t\tfor(int i = 0; i < first_N; i++){\n\t\t\tif(i > 0){\n\n\t\t\t\tadd(max(0,i-mid),i-1,-C,0,0,N-1);\n\t\t\t}\n\t\t\tL_max[i] = getMax(max(0,i-mid),i,0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i <= 2*N-2; i++){\n\t\t\tmax_data[i] = 0;\n\t\t\tadd_data[i] = 0;\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tadd(i,i,A[i],0,0,N-1);\n\t\t}\n\n\t\tll sum = 0;\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tR_max[i] = -HUGE_NUM;\n\t\t}\n\n\t\t//右から\n\t\tfor(int i = first_N-1; i >= 0; i--){\n\t\t\tif(i < first_N-1){\n\n\t\t\t\tadd(i+1,min(first_N-1,i+mid),-C,0,0,N-1);\n\t\t\t}\n\t\t\tR_max[i] = getMax(i,min(first_N-1,i+mid),0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tsum += max(L_max[i],R_max[i]);\n\t\t}\n\n\t\tif(sum >= M){\n\n\t\t\tans = mid;\n\t\t\tright = mid-1;\n\n\t\t}else{\n\n\t\t\tleft = mid+1;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\n\tif(ans == BIG_NUM){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2280, "memory_kb": 9660, "score_of_the_acc": -1.592, "final_rank": 9 }, { "submission_id": "aoj_1544_4696609", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 100005\n\nint N = 1;\nll M,C;\nll A[SIZE];\nll min_data[4*SIZE],max_data[4*SIZE],add_data[4*SIZE];\nll L_max[SIZE],R_max[SIZE];\n\nvoid init(int first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(int left,int right,ll value,int node_id,int node_left,int node_right){\n\n\tif(right < node_left || left > node_right){\n\t\t//範囲外ならreturn\n\t\treturn;\n\t}\n\telse if(left <= node_left && right >= node_right){ //このノードのカバーしている区間が、更新区間の部分区間である場合\n\n\t\tadd_data[node_id] += value; //一様に加える値を加算\n\n\t\twhile(node_id != 0){\n\n\t\t\tnode_id = (node_id-1)/2; //下から上に向かって、最小値および最大値更新\n\t\t\tmin_data[node_id] = min(min_data[2*node_id+1]+add_data[2*node_id+1],min_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t\tmax_data[node_id] = max(max_data[2*node_id+1]+add_data[2*node_id+1],max_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t}\n\t}else{\n\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t}\n}\n\nll getMax(int left,int right,int node_id,int node_left,int node_right){\n\tif(right < node_left || left > node_right)return -BIG_NUM;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn max_data[node_id]+add_data[node_id];\n\n\t}else{\n\n\t\tll left_max = getMax(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tll right_max = getMax(left,right,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t\treturn max(left_max,right_max)+add_data[node_id];\n\t}\n}\n\nint main(){\n\n\tint first_N;\n\tscanf(\"%d %lld %lld\",&first_N,&M,&C);\n\n\tll first_sum = 0;\n\n\tfor(int i = 0; i < first_N; i++){\n\n\t\tscanf(\"%lld\",&A[i]);\n\t\tfirst_sum += A[i];\n\t}\n\n\tif(first_sum >= M){\n\n\t\tprintf(\"0\\n\");\n\t\treturn 0;\n\t}\n\n\tinit(first_N);\n\n\tint left = 1,right = first_N,mid = (left+right)/2;\n\tint ans = BIG_NUM;\n\n\t//探索範囲は増えるのは、損にならないので範囲の二分探索ができるはず\n\n\twhile(left <= right){\n\n\t\tfor(int i = 0; i <= 2*N-2; i++){\n\t\t\tmin_data[i] = 0;\n\t\t\tmax_data[i] = 0;\n\t\t\tadd_data[i] = 0;\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tadd(i,i,A[i],0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tL_max[i] = -HUGE_NUM;\n\t\t}\n\n\t\t//左から\n\t\tfor(int i = 0; i < first_N; i++){\n\t\t\tif(i > 0){\n\n\t\t\t\tadd(max(0,i-mid),i-1,-C,0,0,N-1);\n\t\t\t}\n\t\t\tL_max[i] = getMax(max(0,i-mid),i,0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i <= 2*N-2; i++){\n\t\t\tmin_data[i] = 0;\n\t\t\tmax_data[i] = 0;\n\t\t\tadd_data[i] = 0;\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tadd(i,i,A[i],0,0,N-1);\n\t\t}\n\n\t\tll sum = 0;\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tR_max[i] = -HUGE_NUM;\n\t\t}\n\n\t\t//右から\n\t\tfor(int i = first_N-1; i >= 0; i--){\n\t\t\tif(i < first_N-1){\n\n\t\t\t\tadd(i+1,min(first_N-1,i+mid),-C,0,0,N-1);\n\t\t\t}\n\t\t\tR_max[i] = getMax(i,min(first_N-1,i+mid),0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tsum += max(L_max[i],R_max[i]);\n\t\t}\n\n\t\tif(sum >= M){\n\n\t\t\tans = mid;\n\t\t\tright = mid-1;\n\n\t\t}else{\n\n\t\t\tleft = mid+1;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\n\tif(ans == BIG_NUM){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%d\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2560, "memory_kb": 11704, "score_of_the_acc": -1.9469, "final_rank": 10 }, { "submission_id": "aoj_1544_4696608", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 100005\n\nint N = 1;\nll M,C;\nll A[SIZE];\nll min_data[4*SIZE],max_data[4*SIZE],add_data[4*SIZE];\nll L_max[SIZE],R_max[SIZE];\n\nvoid init(int first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(int left,int right,ll value,int node_id,int node_left,int node_right){\n\n\tif(right < node_left || left > node_right){\n\t\t//範囲外ならreturn\n\t\treturn;\n\t}\n\telse if(left <= node_left && right >= node_right){ //このノードのカバーしている区間が、更新区間の部分区間である場合\n\n\t\tadd_data[node_id] += value; //一様に加える値を加算\n\n\t\twhile(node_id != 0){\n\n\t\t\tnode_id = (node_id-1)/2; //下から上に向かって、最小値および最大値更新\n\t\t\tmin_data[node_id] = min(min_data[2*node_id+1]+add_data[2*node_id+1],min_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t\tmax_data[node_id] = max(max_data[2*node_id+1]+add_data[2*node_id+1],max_data[2*node_id+2]+add_data[2*node_id+2]);\n\t\t}\n\t}else{\n\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t}\n}\n\nll getMax(int left,int right,int node_id,int node_left,int node_right){\n\tif(right < node_left || left > node_right)return -BIG_NUM;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn max_data[node_id]+add_data[node_id];\n\n\t}else{\n\n\t\tll left_max = getMax(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tll right_max = getMax(left,right,2*node_id+2,(node_left+node_right)/2+1,node_right);\n\t\treturn max(left_max,right_max)+add_data[node_id];\n\t}\n}\n\nint main(){\n\n\tint first_N;\n\tscanf(\"%d %lld %lld\",&first_N,&M,&C);\n\n\tll first_sum = 0;\n\n\tfor(int i = 0; i < first_N; i++){\n\n\t\tscanf(\"%lld\",&A[i]);\n\t\tfirst_sum += A[i];\n\t}\n\n\tif(first_sum >= M){\n\n\t\tprintf(\"0\\n\");\n\t\treturn 0;\n\t}\n\n\tinit(first_N);\n\n\tint left = 1,right = first_N,mid = (left+right)/2;\n\tint ans = BIG_NUM;\n\n\t//探索範囲は増えるのは、損にならないので範囲の二分探索ができるはず\n\n\twhile(left <= right){\n\n\t\tfor(int i = 0; i <= 2*N-2; i++){\n\t\t\tmin_data[i] = 0;\n\t\t\tmax_data[i] = 0;\n\t\t\tadd_data[i] = 0;\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tadd(i,i,A[i],0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tL_max[i] = -HUGE_NUM;\n\t\t}\n\n\t\t//左から\n\t\tfor(int i = 0; i < first_N; i++){\n\t\t\tif(i > 0){\n\n\t\t\t\tadd(max(0,i-mid),i-1,-C,0,0,N-1);\n\t\t\t}\n\t\t\tL_max[i] = getMax(max(0,i-mid),i,0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i <= 2*N-2; i++){\n\t\t\tmin_data[i] = 0;\n\t\t\tmax_data[i] = 0;\n\t\t\tadd_data[i] = 0;\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tadd(i,i,A[i],0,0,N-1);\n\t\t}\n\n\t\tll sum = 0;\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tR_max[i] = -HUGE_NUM;\n\t\t}\n\n\t\t//右から\n\t\tfor(int i = first_N-1; i >= 0; i--){\n\t\t\tif(i < first_N-1){\n\n\t\t\t\tadd(i+1,min(first_N-1,i+mid),-C,0,0,N-1);\n\t\t\t}\n\t\t\tR_max[i] = getMax(i,min(first_N-1,i+mid),0,0,N-1);\n\t\t}\n\n\t\tfor(int i = 0; i < first_N; i++){\n\n\t\t\tsum += max(L_max[i],R_max[i]);\n\t\t}\n\n\t\tif(sum >= M){\n\n\t\t\tans = mid;\n\t\t\tright = mid-1;\n\n\t\t}else{\n\n\t\t\tleft = mid+1;\n\t\t}\n\t\tmid = (left+right)/2;\n\t}\n\n\tif(ans == BIG_NUM){\n\n\t\tprintf(\"-1\\n\");\n\t}else{\n\n\t\tprintf(\"%lld\\n\",ans);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2560, "memory_kb": 11708, "score_of_the_acc": -1.9474, "final_rank": 11 }, { "submission_id": "aoj_1544_3578435", "code_snippet": "#include <bits/stdc++.h>\n\n/////////////////////////\n// Range Minimum Query //\n/////////////////////////\n\nclass RangeMaximumQuery {\nprivate:\n\tstd::vector<long long> container_;\n\tvoid constructorHelper(const unsigned int array_size)\n\t{\n\t\tunsigned int length{1};\n\t\twhile (length < array_size)\n\t\t\tlength <<= 1;\n\t\tcontainer_.resize(2 * length);\n\t}\n\tlong long getHelper(const int index, const int node_l, const int node_r, const int query_l, const int query_r) const\n\t{\n\t\tif (query_r <= node_l || node_r <= query_l) return 0;\n\t\tif (query_l <= node_l && node_r <= query_r) return container_[index];\n\t\tconst int node_m{(node_l + node_r) >> 1};\n\t\treturn std::max(getHelper(2 * index, node_l, node_m, query_l, query_r), getHelper(2 * index + 1, node_m, node_r, query_l, query_r));\n\t}\n\npublic:\n\tRangeMaximumQuery(const unsigned int array_size) { constructorHelper(array_size); }\n\t// indexは0-indexed\n\tvoid update(const int index, const long long assigned)\n\t{\n\t\tauto update_place{(container_.size() >> 1) + index};\n\t\tcontainer_[update_place] = assigned;\n\t\twhile (update_place > 1)\n\t\t{\n\t\t\tupdate_place >>= 1;\n\t\t\tcontainer_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);\n\t\t}\n\t}\n\t// left,rightは0-indexed、[left, right)の半開区間\n\tlong long get(const int left, const int right) const\n\t{\n\t\treturn getHelper(1, 0, container_.size() >> 1, left, right);\n\t}\n};\n\nint main()\n{\n\tlong long N, M, C;\n\tscanf(\"%lld%lld%lld\", &N, &M, &C);\n\tstd::vector<long long> smell(N);\n\tfor (auto& e: smell) scanf(\"%lld\", &e);\n\tRangeMaximumQuery inc(N), dec(N);\n\t// セグ木内部の値はすべて正\n\tfor (long long i{}; i < N; i++)\n\t\tinc.update(i, smell[i] + i * C);\n\tfor (long long i{}; i < N; i++)\n\t\tdec.update(i, smell[i] + (N - 1 - i) * C);\n\n\tauto condition{[&](int R)\n\t\t{\n\t\t\tlong long sum{};\n\t\t\tfor (long long i{}; i < N; i++)\n\t\t\t{\n\t\t\t\tsum += std::max(inc.get(std::max(i - R, 0ll), i) - C * i, dec.get(i, std::min(i + R + 1, N)) - C * (N - 1 - i));\n\t\t\t\tif (sum >= M) return true;\n\t\t\t}\n\t\t\treturn sum >= M;\n\t\t}\n\t};\n\tlong long ok{N}, ng{-1};\n\twhile (ok - ng > 1)\n\t{\n\t\tlong long mid{(ok + ng) / 2};\n\t\tif (condition(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tif (ok == N) puts(\"-1\");\n\telse printf(\"%d\\n\", ok);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 7896, "score_of_the_acc": -0.6472, "final_rank": 4 }, { "submission_id": "aoj_1544_3578433", "code_snippet": "#include <bits/stdc++.h>\n\n/////////////////////////\n// Range Minimum Query //\n/////////////////////////\n\nclass RangeMaximumQuery {\nprivate:\n\tstd::vector<long long> container_;\n\tvoid constructorHelper(const unsigned int array_size)\n\t{\n\t\tunsigned int length{1};\n\t\twhile (length < array_size)\n\t\t\tlength <<= 1;\n\t\tcontainer_.resize(2 * length);\n\t}\n\tlong long getHelper(const int index, const int node_l, const int node_r, const int query_l, const int query_r) const\n\t{\n\t\tif (query_r <= node_l || node_r <= query_l) return 0;\n\t\tif (query_l <= node_l && node_r <= query_r) return container_[index];\n\t\tconst int node_m{(node_l + node_r) >> 1};\n\t\treturn std::max(getHelper(2 * index, node_l, node_m, query_l, query_r), getHelper(2 * index + 1, node_m, node_r, query_l, query_r));\n\t}\n\npublic:\n\tRangeMaximumQuery(const unsigned int array_size) { constructorHelper(array_size); }\n\t// indexは0-indexed\n\tvoid update(const int index, const int assigned)\n\t{\n\t\tauto update_place{(container_.size() >> 1) + index};\n\t\tcontainer_[update_place] = assigned;\n\t\twhile (update_place > 1)\n\t\t{\n\t\t\tupdate_place >>= 1;\n\t\t\tcontainer_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);\n\t\t}\n\t}\n\t// left,rightは0-indexed、[left, right)の半開区間\n\tlong long get(const int left, const int right) const\n\t{\n\t\treturn getHelper(1, 0, container_.size() >> 1, left, right);\n\t}\n};\n\nint main()\n{\n\tlong long N, M, C;\n\tscanf(\"%lld%lld%lld\", &N, &M, &C);\n\tstd::vector<long long> smell(N);\n\tfor (auto& e: smell) scanf(\"%lld\", &e);\n\tRangeMaximumQuery inc(N), dec(N);\n\t// セグ木内部の値はすべて正\n\tfor (long long i{}; i < N; i++)\n\t\tinc.update(i, smell[i] + i * C);\n\tfor (long long i{}; i < N; i++)\n\t\tdec.update(i, smell[i] + (N - 1 - i) * C);\n\n\tauto condition{[&](int R)\n\t\t{\n\t\t\tlong long sum{};\n\t\t\tfor (long long i{}; i < N; i++)\n\t\t\t{\n\t\t\t\tsum += std::max(inc.get(std::max(i - R, 0ll), i) - C * i, dec.get(i, std::min(i + R + 1, N)) - C * (N - 1 - i));\n\t\t\t\tif (sum >= M) return true;\n\t\t\t}\n\t\t\treturn sum >= M;\n\t\t}\n\t};\n\tlong long ok{N}, ng{-1};\n\twhile (ok - ng > 1)\n\t{\n\t\tlong long mid{(ok + ng) / 2};\n\t\tif (condition(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tif (ok == N) puts(\"-1\");\n\telse printf(\"%d\\n\", ok);\n\n\treturn 0;\n}", "accuracy": 0.42105263157894735, "time_ms": 270, "memory_kb": 7572, "score_of_the_acc": -0.5813, "final_rank": 13 }, { "submission_id": "aoj_1544_3578428", "code_snippet": "#include <bits/stdc++.h>\n\n/////////////////////////\n// Range Minimum Query //\n/////////////////////////\n\nclass RangeMaximumQuery {\nprivate:\n\tstd::vector<long long> container_;\n\tvoid constructorHelper(const unsigned int array_size)\n\t{\n\t\tunsigned int length{1};\n\t\twhile (length < array_size)\n\t\t\tlength <<= 1;\n\t\tcontainer_.resize(2 * length);\n\t}\n\tlong long getHelper(const int index, const int node_l, const int node_r, const int query_l, const int query_r) const\n\t{\n\t\tif (query_r <= node_l || node_r <= query_l) return 0;\n\t\tif (query_l <= node_l && node_r <= query_r) return container_[index];\n\t\tconst int node_m{(node_l + node_r) >> 1};\n\t\treturn std::max(getHelper(2 * index, node_l, node_m, query_l, query_r), getHelper(2 * index + 1, node_m, node_r, query_l, query_r));\n\t}\n\npublic:\n\tRangeMaximumQuery(const unsigned int array_size) { constructorHelper(array_size); }\n\t// indexは0-indexed\n\tvoid update(const int index, const int assigned)\n\t{\n\t\tauto update_place{(container_.size() >> 1) + index};\n\t\tcontainer_[update_place] = assigned;\n\t\twhile (update_place > 1)\n\t\t{\n\t\t\tupdate_place >>= 1;\n\t\t\tcontainer_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);\n\t\t}\n\t}\n\t// left,rightは0-indexed、[left, right)の半開区間\n\tlong long get(const int left, const int right) const\n\t{\n\t\treturn getHelper(1, 0, container_.size() >> 1, left, right);\n\t}\n};\n\nint main()\n{\n\tint N;\n\tlong long M, C;\n\tscanf(\"%d%lld%lld\", &N, &M, &C);\n\tstd::vector<long long> smell(N);\n\tfor (auto& e: smell) scanf(\"%lld\", &e);\n\tRangeMaximumQuery inc(N), dec(N);\n\t// セグ木内部の値はすべて正\n\tfor (int i{}; i < N; i++)\n\t\tinc.update(i, smell[i] + i * C);\n\tfor (int i{}; i < N; i++)\n\t\tdec.update(i, smell[i] + (N - 1 - i) * C);\n\n\tauto condition{[&](int R)\n\t\t{\n\t\t\tlong long sum{};\n\t\t\tfor (int i{}; i < N; i++)\n\t\t\t{\n\t\t\t\tsum += std::max(inc.get(std::max(i - R, 0), i) - C * i, dec.get(i, std::min(i + R + 1, N)) - C * (N - 1 - i));\n\t\t\t\tif (sum >= M) return true;\n\t\t\t}\n\t\t\treturn sum >= M;\n\t\t}\n\t};\n\tint ok{N}, ng{-1};\n\twhile (ok - ng > 1)\n\t{\n\t\tint mid{(ok + ng) / 2};\n\t\tif (condition(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tif (ok == N) puts(\"-1\");\n\telse printf(\"%d\\n\", ok);\n\n\treturn 0;\n}", "accuracy": 0.42105263157894735, "time_ms": 270, "memory_kb": 7808, "score_of_the_acc": -0.6102, "final_rank": 18 }, { "submission_id": "aoj_1544_3578424", "code_snippet": "#include <bits/stdc++.h>\n\n/////////////////////////\n// Range Minimum Query //\n/////////////////////////\n\nclass RangeMaximumQuery {\nprivate:\n\tstd::vector<long long> container_;\n\tvoid constructorHelper(const unsigned int array_size)\n\t{\n\t\tunsigned int length{1};\n\t\twhile (length < array_size)\n\t\t\tlength <<= 1;\n\t\tcontainer_.resize(2 * length);\n\t}\n\tlong long getHelper(const int index, const int node_l, const int node_r, const int query_l, const int query_r) const\n\t{\n\t\tif (query_r <= node_l || node_r <= query_l) return 0;\n\t\tif (query_l <= node_l && node_r <= query_r) return container_[index];\n\t\tconst int node_m{(node_l + node_r) >> 1};\n\t\treturn std::max(getHelper(2 * index, node_l, node_m, query_l, query_r), getHelper(2 * index + 1, node_m, node_r, query_l, query_r));\n\t}\n\npublic:\n\tRangeMaximumQuery(const unsigned int array_size) { constructorHelper(array_size); }\n\t// indexは0-indexed\n\tvoid update(const int index, const int assigned)\n\t{\n\t\tauto update_place{(container_.size() >> 1) + index};\n\t\tcontainer_[update_place] = assigned;\n\t\twhile (update_place > 1)\n\t\t{\n\t\t\tupdate_place >>= 1;\n\t\t\tcontainer_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);\n\t\t}\n\t}\n\t// left,rightは0-indexed、[left, right)の半開区間\n\tlong long get(const int left, const int right) const\n\t{\n\t\treturn getHelper(1, 0, container_.size() >> 1, left, right);\n\t}\n};\n\nint main()\n{\n\tint N;\n\tlong long M, C;\n\tscanf(\"%d%lld%lld\", &N, &M, &C);\n\tstd::vector<long long> smell(N);\n\tfor (auto& e: smell) scanf(\"%lld\", &e);\n\tRangeMaximumQuery inc(N), dec(N);\n\t// セグ木内部の値はすべて正\n\tfor (int i{}; i < N; i++)\n\t\tinc.update(i, smell[i] + i * C);\n\tfor (int i{}; i < N; i++)\n\t\tdec.update(i, smell[i] + (N - 1 - i) * C);\n\n\tauto condition{[&](int R)\n\t\t{\n\t\t\tlong long sum{};\n\t\t\tfor (int i{}; i < N; i++)\n\t\t\t\tsum += std::max(inc.get(std::max(i - R, 0), i) - C * i, dec.get(i, std::min(i + R + 1, N)) - C * (N - 1 - i));\n\t\t\treturn sum >= M;\n\t\t}\n\t};\n\tint ok{N}, ng{-1};\n\twhile (ok - ng > 1)\n\t{\n\t\tint mid{(ok + ng) / 2};\n\t\tif (condition(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tif (ok == N) puts(\"-1\");\n\telse printf(\"%d\\n\", ok);\n\n\treturn 0;\n}", "accuracy": 0.42105263157894735, "time_ms": 260, "memory_kb": 7860, "score_of_the_acc": -0.6128, "final_rank": 19 }, { "submission_id": "aoj_1544_3578413", "code_snippet": "#include <bits/stdc++.h>\n\n/////////////////////////\n// Range Minimum Query //\n/////////////////////////\n\nclass RangeMaximumQuery {\nprivate:\n\tstd::vector<long long> container_;\n\tvoid constructorHelper(const unsigned int array_size)\n\t{\n\t\tunsigned int length{1};\n\t\twhile (length < array_size)\n\t\t\tlength <<= 1;\n\t\tcontainer_.resize(2 * length);\n\t}\n\tlong long getHelper(const int index, const int node_l, const int node_r, const int query_l, const int query_r) const\n\t{\n\t\tif (query_r <= node_l || node_r <= query_l) return 0;\n\t\tif (query_l <= node_l && node_r <= query_r) return container_[index];\n\t\tconst int node_m{(node_l + node_r) >> 1};\n\t\treturn std::max(getHelper(2 * index, node_l, node_m, query_l, query_r), getHelper(2 * index + 1, node_m, node_r, query_l, query_r));\n\t}\n\npublic:\n\tRangeMaximumQuery(const unsigned int array_size) { constructorHelper(array_size); }\n\t// indexは0-indexed\n\tvoid update(const int index, const int assigned)\n\t{\n\t\tauto update_place{(container_.size() >> 1) + index};\n\t\tcontainer_[update_place] = assigned;\n\t\twhile (update_place > 1)\n\t\t{\n\t\t\tupdate_place >>= 1;\n\t\t\tcontainer_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);\n\t\t}\n\t}\n\t// left,rightは0-indexed、[left, right)の半開区間\n\tlong long get(const int left, const int right) const\n\t{\n\t\treturn getHelper(1, 0, container_.size() >> 1, left, right);\n\t}\n};\n\nint main()\n{\n\tint N;\n\tlong long M, C;\n\tscanf(\"%d%lld%lld\", &N, &M, &C);\n\tstd::vector<long long> smell(N);\n\tfor (auto& e: smell) scanf(\"%lld\", &e);\n\tRangeMaximumQuery inc(N), dec(N);\n\t// セグ木内部の値はすべて正\n\tfor (int i{}; i < N; i++)\n\t\tinc.update(i, smell[i] + i * C);\n\tfor (int i{}; i < N; i++)\n\t\tdec.update(i, smell[i] + (N - 1 - i) * C);\n\n\tauto condition{[&](int R)\n\t\t{\n\t\t\tlong long sum{};\n\t\t\tfor (int i{}; i < N; i++)\n\t\t\t\tsum += std::max(inc.get(i - R, i) - C * i, dec.get(i, i + R + 1) - C * (N - 1 - i));\n\t\t\treturn sum >= M;\n\t\t}\n\t};\n\tint ok{N}, ng{-1};\n\twhile (ok - ng > 1)\n\t{\n\t\tint mid{(ok + ng) / 2};\n\t\tif (condition(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tif (ok == N) puts(\"-1\");\n\telse printf(\"%d\\n\", ok);\n\n\treturn 0;\n}", "accuracy": 0.42105263157894735, "time_ms": 250, "memory_kb": 7856, "score_of_the_acc": -0.6085, "final_rank": 17 }, { "submission_id": "aoj_1544_3578356", "code_snippet": "#include <bits/stdc++.h>\n\n/////////////////////////\n// Range Minimum Query //\n/////////////////////////\n\nclass RangeMaximumQuery {\nprivate:\n\tstd::vector<long long> container_;\n\tvoid constructorHelper(const unsigned int array_size)\n\t{\n\t\tunsigned int length{1};\n\t\twhile (length < array_size)\n\t\t\tlength <<= 1;\n\t\tcontainer_.resize(2 * length);\n\t}\n\tlong long getHelper(const int index, const int node_l, const int node_r, const int query_l, const int query_r) const\n\t{\n\t\tif (query_r <= node_l || node_r <= query_l) return 0;\n\t\tif (query_l <= node_l && node_r <= query_r) return container_[index];\n\t\tconst int node_m{(node_l + node_r) >> 1};\n\t\treturn std::max(getHelper(2 * index, node_l, node_m, query_l, query_r), getHelper(2 * index + 1, node_m, node_r, query_l, query_r));\n\t}\n\npublic:\n\tRangeMaximumQuery(const unsigned int array_size) { constructorHelper(array_size); }\n\tRangeMaximumQuery(const std::vector<long long> &array)\n\t{\n\t\tconstructorHelper(array.size());\n\t\tstd::copy(array.begin(), array.end(), container_.begin() + (container_.size() >> 1));\n\t\tfor (auto i{(container_.size() >> 1) - 1}; i > 0; i--)\n\t\t\tcontainer_[i] = std::max(container_[2 * i], container_[2 * i + 1]);\n\t}\n\t// indexは0-indexed\n\tvoid update(const int index, const int assigned)\n\t{\n\t\tauto update_place{(container_.size() >> 1) + index};\n\t\tcontainer_[update_place] = assigned;\n\t\twhile (update_place > 1)\n\t\t{\n\t\t\tupdate_place >>= 1;\n\t\t\tcontainer_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);\n\t\t}\n\t}\n\t// left,rightは0-indexed、[left, right)の半開区間\n\tlong long get(const int left, const int right) const\n\t{\n\t\treturn getHelper(1, 0, container_.size() >> 1, left, right);\n\t}\n};\n\nint main()\n{\n\tint N;\n\tlong long M, C;\n\tscanf(\"%d%lld%lld\", &N, &M, &C);\n\tstd::vector<long long> smell(N);\n\tfor (auto& e: smell) scanf(\"%lld\", &e);\n\tRangeMaximumQuery inc(N), dec(N);\n\t// セグ木内部の値はすべて正\n\tfor (int i{}; i < N; i++)\n\t\tinc.update(i, smell[i] + i * C);\n\tfor (int i{}; i < N; i++)\n\t\tdec.update(i, smell[i] + (N - 1 - i) * C);\n\n\tauto condition{[&](int R)\n\t\t{\n\t\t\tlong long sum{};\n\t\t\tfor (int i{}; i < N; i++)\n\t\t\t\tsum += std::max({inc.get(i - R, i) - C * i, smell[i], dec.get(i + 1, i + R + 1) - C * (N - 1 - i)});\n\t\t\treturn sum >= M;\n\t\t}\n\t};\n\tint ok{N}, ng{-1};\n\twhile (ok - ng > 1)\n\t{\n\t\tint mid{(ok + ng) / 2};\n\t\tif (condition(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tif (ok == N) puts(\"-1\");\n\telse printf(\"%d\\n\", ok);\n\n\treturn 0;\n}", "accuracy": 0.42105263157894735, "time_ms": 260, "memory_kb": 7816, "score_of_the_acc": -0.6074, "final_rank": 15 }, { "submission_id": "aoj_1544_3578278", "code_snippet": "#include <bits/stdc++.h>\n\n/////////////////////////\n// Range Minimum Query //\n/////////////////////////\n\nclass RangeMaximumQuery {\nprivate:\n\tstd::vector<long long> container_;\n\tvoid constructorHelper(const unsigned int array_size)\n\t{\n\t\tunsigned int length{1};\n\t\twhile (length < array_size)\n\t\t\tlength <<= 1;\n\t\tcontainer_.resize(2 * length, -(1LL << 55));\n\t}\n\tlong long getHelper(const int index, const int node_l, const int node_r, const int query_l, const int query_r) const\n\t{\n\t\tif (query_r <= node_l || node_r <= query_l) return -(1LL << 55);\n\t\tif (query_l <= node_l && node_r <= query_r) return container_[index];\n\t\tconst int node_m{(node_l + node_r) >> 1};\n\t\treturn std::max(getHelper(2 * index, node_l, node_m, query_l, query_r), getHelper(2 * index + 1, node_m, node_r, query_l, query_r));\n\t}\n\npublic:\n\tRangeMaximumQuery(const unsigned int array_size) { constructorHelper(array_size); }\n\tRangeMaximumQuery(const std::vector<long long> &array)\n\t{\n\t\tconstructorHelper(array.size());\n\t\tstd::copy(array.begin(), array.end(), container_.begin() + (container_.size() >> 1));\n\t\tfor (auto i{(container_.size() >> 1) - 1}; i > 0; i--)\n\t\t\tcontainer_[i] = std::max(container_[2 * i], container_[2 * i + 1]);\n\t}\n\t// indexは0-indexed\n\tvoid update(const int index, const int assigned)\n\t{\n\t\tauto update_place{(container_.size() >> 1) + index};\n\t\tcontainer_[update_place] = assigned;\n\t\twhile (update_place > 1)\n\t\t{\n\t\t\tupdate_place >>= 1;\n\t\t\tcontainer_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);\n\t\t}\n\t}\n\t// left,rightは0-indexed、[left, right)の半開区間\n\tlong long get(const int left, const int right) const\n\t{\n\t\treturn getHelper(1, 0, container_.size() >> 1, left, right);\n\t}\n};\n\nint main()\n{\n\tint N;\n\tlong long M, C;\n\tscanf(\"%d%lld%lld\", &N, &M, &C);\n\tstd::vector<long long> smell(N);\n\tfor (auto& e: smell) scanf(\"%lld\", &e);\n\tRangeMaximumQuery inc(N), dec(N);\n\tfor (int i{}; i < N; i++)\n\t\tinc.update(i, smell[i] + i * C);\n\tfor (int i{}; i < N; i++)\n\t\tdec.update(i, smell[i] + (N - 1 - i) * C);\n\n\tauto condition{[&](int R)\n\t\t{\n\t\t\tlong long sum{};\n\t\t\tfor (int i{}; i < N; i++)\n\t\t\t{\n\t\t\t\tsum += std::max(inc.get(i - R, i) - C * i, dec.get(i, i + R + 1) - C * (N - 1 - i));\n\t\t\t}\n\t\t\treturn sum >= M;\n\t\t}\n\t};\n\tint ok{N}, ng{-1};\n\twhile (ok - ng > 1)\n\t{\n\t\tint mid{(ok + ng) / 2};\n\t\tif (condition(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tif (ok == N) puts(\"-1\");\n\telse printf(\"%d\\n\", ok);\n\n\treturn 0;\n}", "accuracy": 0.42105263157894735, "time_ms": 240, "memory_kb": 7880, "score_of_the_acc": -0.6077, "final_rank": 16 }, { "submission_id": "aoj_1544_3578258", "code_snippet": "#include <bits/stdc++.h>\n\n/////////////////////////\n// Range Minimum Query //\n/////////////////////////\n\nclass RangeMaximumQuery {\nprivate:\n\tstd::vector<long long> container_;\n\tvoid constructorHelper(const unsigned int array_size)\n\t{\n\t\tunsigned int length{1};\n\t\twhile (length < array_size)\n\t\t\tlength <<= 1;\n\t\tcontainer_.resize(2 * length, -(1LL << 60));\n\t}\n\tlong long getHelper(const int index, const int node_l, const int node_r, const int query_l, const int query_r) const\n\t{\n\t\tif (query_r <= node_l || node_r <= query_l) return -(1LL << 60);\n\t\tif (query_l <= node_l && node_r <= query_r) return container_[index];\n\t\tconst int node_m{(node_l + node_r) >> 1};\n\t\treturn std::max(getHelper(2 * index, node_l, node_m, query_l, query_r), getHelper(2 * index + 1, node_m, node_r, query_l, query_r));\n\t}\n\npublic:\n\tRangeMaximumQuery(const unsigned int array_size) { constructorHelper(array_size); }\n\tRangeMaximumQuery(const std::vector<long long> &array)\n\t{\n\t\tconstructorHelper(array.size());\n\t\tstd::copy(array.begin(), array.end(), container_.begin() + (container_.size() >> 1));\n\t\tfor (auto i{(container_.size() >> 1) - 1}; i > 0; i--)\n\t\t\tcontainer_[i] = std::max(container_[2 * i], container_[2 * i + 1]);\n\t}\n\t// indexは0-indexed\n\tvoid update(const int index, const int assigned)\n\t{\n\t\tauto update_place{(container_.size() >> 1) + index};\n\t\tcontainer_[update_place] = assigned;\n\t\twhile (update_place > 1)\n\t\t{\n\t\t\tupdate_place >>= 1;\n\t\t\tcontainer_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);\n\t\t}\n\t}\n\t// left,rightは0-indexed、[left, right)の半開区間\n\tlong long get(const int left, const int right) const\n\t{\n\t\t// int change{2};\n\t\t// for (unsigned int i{1}; i < container_.size(); i++)\n\t\t// {\n\t\t// \tprintf(\"%lld \", container_[i]);\n\t\t// \tif (i + 1 == change)\n\t\t// \t{\n\t\t// \t\tputchar('\\n');\n\t\t// \t\tchange *= 2;\n\t\t// \t}\n\t\t// }\n\t\treturn getHelper(1, 0, container_.size() >> 1, left, right);\n\t}\n};\n\nint main()\n{\n\tint N;\n\tlong long M, C;\n\tscanf(\"%d%lld%lld\", &N, &M, &C);\n\tstd::vector<long long> smell(N);\n\tfor (auto& e: smell) scanf(\"%lld\", &e);\n\tRangeMaximumQuery inc(N), dec(N);\n\tfor (int i{}; i < N; i++)\n\t\tinc.update(i, smell[i] + i * C);\n\tfor (int i{}; i < N; i++)\n\t\tdec.update(i, smell[i] + (N - 1 - i) * C);\n\n\tauto condition{[&](int R)\n\t\t{\n\t\t\tlong long sum{};\n\t\t\tfor (int i{}; i < N; i++)\n\t\t\t{\n\t\t\t\tint left{std::max(i - R, 0)}, right{std::min(i + R, N - 1)};\n\t\t\t\tsum += std::max(inc.get(left, i) - C * i, dec.get(i, right + 1) - C * (N - 1 - i));\n\t\t\t}\n\t\t\treturn sum >= M;\n\t\t}\n\t};\n\tint ok{N}, ng{-1};\n\twhile (ok - ng > 1)\n\t{\n\t\tint mid{(ok + ng) / 2};\n\t\tif (condition(mid)) ok = mid;\n\t\telse ng = mid;\n\t}\n\tif (ok == N) puts(\"-1\");\n\telse printf(\"%d\\n\", ok);\n\n\treturn 0;\n}", "accuracy": 0.42105263157894735, "time_ms": 250, "memory_kb": 7804, "score_of_the_acc": -0.6022, "final_rank": 14 }, { "submission_id": "aoj_1544_3577940", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ll = long long;\n\n\n\nclass SegmentTree {\npublic:\n\tSegmentTree() {}\n\tSegmentTree(ll size) {\n\t\tn = 1;\n\t\twhile (n < size)n *= 2;\n\t\tdat.resize(2 * n, 0);\n\t}\n\tvoid update(ll k, ll a) {\n\t\tk += n - 1;\n\t\tdat[k] = a;\n\t\twhile (k > 0) {\n\t\t\tk = (k - 1) / 2;\n\t\t\tdat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);\n\t\t}\n\t}\n\tll query(ll a, ll b) {\n\t\treturn query_impl(a, b, 0, 0, n);\n\t}\nprivate:\n\tll n;\n\tvector<ll> dat;\n\n\tll query_impl(ll a, ll b, ll k, ll l, ll r) {\n\t\tif (r <= a || b <= l)return 0;\n\t\tif (a <= l && r <= b)return dat[k];\n\n\t\tll vl = query_impl(a, b, k * 2 + 1, l, (l + r) / 2);\n\t\tll vr = query_impl(a, b, k * 2 + 2, (l + r) / 2, r);\n\t\treturn max(vl, vr);\n\t}\n};\n\n\nll N, M, C;\nSegmentTree stl, str;\n\nbool judge(ll d) {\n\tvector<ll> res(N);\n\tfor (ll i = 0; i < N; i++) {\n\t\tll a = stl.query(max(0ll, i - d), i + 1) - C * i;\n\t\tll b = str.query(i, min(N - 1, i + d) + 1) - C * (N - 1 - i);\n\t\tres[i] = max(a, b);\n\t}\n\treturn accumulate(res.begin(), res.end(), 0ll) >= M;\n}\n\nint main() {\n\tcin >> N >> M >> C;\n\tstl = SegmentTree(N);\n\tstr = SegmentTree(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tll ai;\n\t\tcin >> ai;\n\t\tstl.update(i, ai + C * i);\n\t\tstr.update(i, ai + C * (N - 1 - i));\n\t}\n\n\tint l = 0, r = N;\n\twhile (r - l > 1) {\n\t\tint mid = (l + r) / 2;\n\t\tif (judge(mid))r = mid;\n\t\telse l = mid;\n\t}\n\tif (r == N && !judge(r)) {\n\t\tcout << -1 << endl;\n\t}\n\telse {\n\t\tif (judge(l))cout << l << endl;\n\t\telse cout << r << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 580, "memory_kb": 7880, "score_of_the_acc": -0.7355, "final_rank": 6 }, { "submission_id": "aoj_1544_3577938", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ll = long long;\n\n\n\nclass SegmentTree {\npublic:\n\tSegmentTree() {}\n\tSegmentTree(ll size) {\n\t\tn = 1;\n\t\twhile (n < size)n *= 2;\n\t\tdat.resize(2 * n, 0);\n\t}\n\tvoid update(ll k, ll a) {\n\t\tk += n - 1;\n\t\tdat[k] = a;\n\t\twhile (k > 0) {\n\t\t\tk = (k - 1) / 2;\n\t\t\tdat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);\n\t\t}\n\t}\n\tll query(ll a, ll b) {\n\t\treturn query_impl(a, b, 0, 0, n);\n\t}\nprivate:\n\tll n;\n\tvector<ll> dat;\n\n\tll query_impl(ll a, ll b, ll k, ll l, ll r) {\n\t\tif (r <= a || b <= l)return 0;\n\t\tif (a <= l && r <= b)return dat[k];\n\n\t\tll vl = query_impl(a, b, k * 2 + 1, l, (l + r) / 2);\n\t\tll vr = query_impl(a, b, k * 2 + 2, (l + r) / 2, r);\n\t\treturn max(vl, vr);\n\t}\n};\n\n\nll N, M, C;\nSegmentTree stl, str;\n\nbool judge(ll d) {\n\tvector<ll> res(N);\n\tfor (ll i = 0; i < N; i++) {\n\t\tll a = stl.query(max(0ll, i - d), i + 1) - C * i;\n\t\tll b = str.query(i, min(N - 1, i + d) + 1) - C * (N - 1 - i);\n\t\tres[i] = max(a, b);\n\t}\n\treturn accumulate(res.begin(), res.end(), 0) >= M;\n}\n\nint main() {\n\tcin >> N >> M >> C;\n\tstl = SegmentTree(N);\n\tstr = SegmentTree(N);\n\tfor (int i = 0; i < N; i++) {\n\t\tll ai;\n\t\tcin >> ai;\n\t\tstl.update(i, ai + C * i);\n\t\tstr.update(i, ai + C * (N - 1 - i));\n\t}\n\n\tint l = 0, r = N;\n\twhile (r - l > 1) {\n\t\tint mid = (l + r) / 2;\n\t\tif (judge(mid))r = mid;\n\t\telse l = mid;\n\t}\n\tif (r == N && !judge(r)) {\n\t\tcout << -1 << endl;\n\t}\n\telse {\n\t\tif (judge(l))cout << l << endl;\n\t\telse cout << r << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.42105263157894735, "time_ms": 310, "memory_kb": 7816, "score_of_the_acc": -0.6262, "final_rank": 20 }, { "submission_id": "aoj_1544_1080776", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n#define tpl(...) make_tuple(__VA_ARGS__)\nconst int INF = 0x3f3f3f3f;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<ll,ll> pii;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T>ostream&operator<<(ostream &o,const vector<T>&t){o<<'[';FOR(i,t){if(i!=t.begin())o<<',';o<<*i;}return o<<']';}\ntemplate<class S,class T>ostream&operator<<(ostream &o,const pair<S,T>&t){return o<<'('<<t.first<<','<<t.second<<')';}\ntemplate<int N,class Tp>void output(ostream&,const Tp&){}\ntemplate<int N,class Tp,class,class ...Ts>void output(ostream &o,const Tp&t){if(N)o<<',';o<<get<N>(t);output<N+1,Tp,Ts...>(o,t);}\ntemplate<class ...Ts>ostream&operator<<(ostream&o,const tuple<Ts...>&t){o<<'(';output<0,tuple<Ts...>,Ts...>(o,t);return o<<')';}\ntemplate<class T>void output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20];\n int k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);}\ntemplate<class T>void outputs(T t){output(t);}\ntemplate<class S,class ...T>void outputs(S a,T...t){output(a,32);outputs(t...);}\ntemplate<class T>void output(T *a,int n){REP(i,n)output(a[i],i!=n-1?',':10);}\ntemplate<class T>void output(T *a,int n,int m){REP(i,n)output(a[i],m);}\ntemplate<class T>bool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;);\n if(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;}\ntemplate<class S,class ...T>bool input(S&a,T&...t){input(a);return input(t...);}\n\nll a[100000];\nll b[100000];\n\nll t[100000];\n\nint main() {\n ll n;\n ll m,C;\n while(input(n,m,C)) {\n REP(i,n) {\n input(a[i]);\n b[i] = a[i];\n }\n reverse(b,b+n);\n ll low = -1, high = n+1;\n while(low+1<high) {\n ll mid = (low+high)/2;\n deque<pii> D;\n REP(i,n) {\n while(D.size()) {\n pii &p = D.front();\n if (p.second < i-mid) {\n D.pop_front();\n } else break;\n }\n while(D.size()) {\n pii &p = D.back();\n if (p.first - (i-p.second)*C < a[i]) {\n D.pop_back();\n } else break;\n }\n D.push_back(pii(a[i], i));\n pii &p = D.front();\n t[i] = p.first - (i-p.second)*C;\n // REP(j,D.size()) {\n // assert(D[j].first-(i-D[j].second)*C <= t[i]);\n // }\n }\n // REP(i,n) {\n // ll tmp = a[i];\n // for (int j=max(0LL,i-mid); j<=i; ++j) {\n // chmax(tmp, a[j] - abs(i-j)*C);\n // }\n // assert(tmp == t[i]);\n // } \n D.clear();\n REP(i,n) {\n while(D.size()) {\n pii &p = D.front();\n if (p.second < i-mid) {\n D.pop_front();\n } else break;\n }\n while(D.size()) {\n pii &p = D.back();\n if (p.first - (i-p.second)*C < b[i]) {\n D.pop_back();\n } else break;\n }\n D.push_back(pii(b[i], i));\n pii &p = D.front();\n chmax(t[n-i-1], p.first - (i-p.second)*C);\n }\n ll sum = 0;\n REP(i,n) sum += t[i];\n \n // cout << mid << \" \" << sum << endl;\n // output(t,n);\n if (sum >= m) {\n high = mid;\n } else {\n low = mid;\n }\n }\n if (high == n+1) {\n cout << -1 << endl;\n } else {\n cout << high << endl;\n }\n \n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3520, "score_of_the_acc": -0.0075, "final_rank": 1 }, { "submission_id": "aoj_1544_1080772", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i,n) for(int i=0;i<(int)(n);++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)\n#define tpl(...) make_tuple(__VA_ARGS__)\nconst int INF = 0x3f3f3f3f;\nconst double EPS = 1e-8;\nconst double PI = acos(-1);\ntypedef long long ll;\ntypedef pair<ll,ll> pii;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T>ostream&operator<<(ostream &o,const vector<T>&t){o<<'[';FOR(i,t){if(i!=t.begin())o<<',';o<<*i;}return o<<']';}\ntemplate<class S,class T>ostream&operator<<(ostream &o,const pair<S,T>&t){return o<<'('<<t.first<<','<<t.second<<')';}\ntemplate<int N,class Tp>void output(ostream&,const Tp&){}\ntemplate<int N,class Tp,class,class ...Ts>void output(ostream &o,const Tp&t){if(N)o<<',';o<<get<N>(t);output<N+1,Tp,Ts...>(o,t);}\ntemplate<class ...Ts>ostream&operator<<(ostream&o,const tuple<Ts...>&t){o<<'(';output<0,tuple<Ts...>,Ts...>(o,t);return o<<')';}\ntemplate<class T>void output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20];\n int k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);}\ntemplate<class T>void outputs(T t){output(t);}\ntemplate<class S,class ...T>void outputs(S a,T...t){output(a,32);outputs(t...);}\ntemplate<class T>void output(T *a,int n){REP(i,n)output(a[i],i!=n-1?',':10);}\ntemplate<class T>void output(T *a,int n,int m){REP(i,n)output(a[i],m);}\ntemplate<class T>bool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;);\n if(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;}\ntemplate<class S,class ...T>bool input(S&a,T&...t){input(a);return input(t...);}\n\nll a[100000];\nll b[100000];\n\nll t[100000];\n\nint main() {\n ll n;\n ll m,C;\n while(input(n,m,C)) {\n REP(i,n) {\n input(a[i]);\n b[i] = a[i];\n }\n reverse(b,b+n);\n ll low = -1, high = n+1;\n while(low+1<high) {\n ll mid = (low+high)/2;\n deque<pii> D;\n REP(i,n) {\n while(D.size()) {\n pii &p = D.front();\n if (p.second < i-mid) {\n D.pop_front();\n } else break;\n }\n while(D.size()) {\n pii &p = D.back();\n if (p.first - (i-p.second)*C < a[i]) {\n D.pop_back();\n } else break;\n }\n D.push_back(pii(a[i], i));\n pii &p = D.front();\n t[i] = p.first - (i-p.second)*C;\n // REP(j,D.size()) {\n // assert(D[j].first-(i-D[j].second)*C <= t[i]);\n // }\n }\n // REP(i,n) {\n // ll tmp = a[i];\n // for (int j=max(0LL,i-mid); j<=i; ++j) {\n // chmax(tmp, a[j] - abs(i-j)*C);\n // }\n // assert(tmp == t[i]);\n // } \n D.clear();\n REP(i,n) {\n while(D.size()) {\n pii &p = D.front();\n if (p.second < i-mid) {\n D.pop_front();\n } else if (p.first - (i-p.second)*C < b[i]) {\n D.pop_front();\n } else\n break;\n }\n D.push_back(pii(b[i], i));\n pii &p = D.front();\n chmax(t[n-i-1], p.first - (i-p.second)*C);\n }\n ll sum = 0;\n REP(i,n) sum += t[i];\n \n // cout << mid << \" \" << sum << endl;\n // output(t,n);\n if (sum >= m) {\n high = mid;\n } else {\n low = mid;\n }\n }\n if (high == n+1) {\n cout << -1 << endl;\n } else {\n cout << high << endl;\n }\n \n }\n}", "accuracy": 0.7192982456140351, "time_ms": 50, "memory_kb": 3652, "score_of_the_acc": -0.0199, "final_rank": 12 }, { "submission_id": "aoj_1544_1080653", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<string>\n#include<cstring>\n#include<vector>\n#include<set>\n#include<list>\n#include<queue>\n#include<cmath>\n#include<functional>\n#include<algorithm>\n#include<climits>\n#define INF (1<<29)\n#define EPS 1e-10\n#define rep(i,n) for(int i=0;i<(n);i++)\nusing namespace std;\n\n#define MAX_SIZE 400000\n\nlong long segMax[MAX_SIZE - 1], segAdd[MAX_SIZE - 1];\n\n\nint n;\n\n//区間[a, b)に値xを加算する.\nvoid add(int a, int b, long long x, int k = 0, int l = 0, int r = n)\n{\n\tif (r <= a || b <= l) return;\n\t\n\tif (a <= l && r <= b){\n\t\tsegAdd[k] += x;\n\t\twhile (k){\n\t\t\tk = (k - 1) / 2;\n\t\t\tsegMax[k] = max(segMax[k * 2 + 1] + segAdd[k * 2 + 1], segMax[k * 2 + 2] + segAdd[k * 2 + 2]);\n\t\t}\n\t\treturn;\n\t}\n\t\n\tadd(a, b, x, k * 2 + 1, l, (l + r) / 2);\n\tadd(a, b, x, k * 2 + 2, (l + r) / 2, r);\n}\n\nlong long getMax(int a, int b, int k = 0, int l = 0, int r = n)\n{\n\tif (r <= a || b <= l) return (LLONG_MIN);\n\t\n\tif (a <= l && r <= b) return (segMax[k] + segAdd[k]);\n\t\n\tlong long left = getMax(a, b, k * 2 + 1, l, (l + r) / 2);\n\tlong long right = getMax(a, b, k * 2 + 2, (l + r) / 2, r);\n\t\n\treturn (max(left, right) + segAdd[k]);\n\t\n}\n\n\nint a[100000];\nvoid init(){\n\tmemset(segMax,0,sizeof(segMax));\n\tmemset(segAdd,0,sizeof(segAdd));\n\trep(i,n)add(i,i+1,a[i]);\n}\n\n\nlong long m;\nint c;\n\nlong long calc(int r){\n\tlong long s=0;\n\tfor(int i=1;i<n;i++){\n\t\tadd(i,min(i+r+1,n),-c);\n\t}\n\t//cout<<'r'<<r<<endl;\n\trep(i,n){\n\t\t//rep(i,n)cout<<getMax(i,i+1)<<' ';cout<<endl;\n\t\ts+=getMax(max(0,i-r),min(n,i+r+1));\n\t\tadd(0,i+1,-c);\n\t\tadd(i+1,min(n,i+r+2),c);\n\t}\n\treturn s;\n}\n\n\nint main(){\n\tcin>>n>>m>>c;\n\trep(i,n)cin>>a[i];\n\tint l=-1,r=n;\n\twhile(r-l>1){\n\t\tint mid=(l+r)/2;\n\t\tinit();\n\t\tif(calc(mid)<m)l=mid;\n\t\telse r=mid;\n\t}\n\tif(r==n)r=-1;\n\tcout<<r<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2700, "memory_kb": 7820, "score_of_the_acc": -1.5252, "final_rank": 7 } ]
aoj_1536_cpp
Problem K: Witch Craft Moves Problem 会津魔法学校は、魔法を使える者が集る学校である。 会津魔法学校には1~ N までの番号がついた教室と教室同士をつなぐ廊下がある。教室や廊下は何度でも通ることができる。また、会津魔法学校には任意の教室 u , v 間を0個以上の教室と廊下を一度ずつ通って移動することができるような経路がただ1つある。 各教室には魔法陣が設置してある。この魔方陣によって、教室に入る度に入った者の魔力が増減させられてしまうのだ。生徒の移動を助けるために以下の二つの質問に応えてほしい。 質問1 0 A B 教室 A から B へ移動したときの魔力の残量を出力してほしい。教室 A と B での増減も含む。また、この質問開始時点での魔力の量は毎回0である。 質問2 1 A C 教室 A の魔力の増減量が C だけ変動させる。 Input N cost 1 : cost N a 1 b 1 : a N-1 b N-1 Q query 1 : query Q 入力の最初に教室の数 N が与えられる。続く N 行に各教室に入ったときの魔力が増減する量が与えられる。 i 行目の値が i 番の教室の情報に対応する。続く N-1 行に廊下の情報が与えられる。 j 行目の廊下は a j 番目と b j 番目の教室をつなぐことを表す。次に質問の数 Q が与えられる。続く Q 行に上記の形式で質問が与えられる。 Output 各質問1について、答えを1行に出力せよ。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 2000 1 ≤ A i ≤ 2000 1 ≤ B i ≤ 2000 1 ≤ a j , b j ≤ N 1 ≤ Q ≤ 1000000 -10000 ≤ C i ≤ 10000 Sample Input 1 7 1 2 3 4 5 6 7 1 2 1 3 3 4 3 5 5 6 5 7 10 0 1 7 0 2 7 1 1 15 0 1 7 0 2 7 0 1 1 1 1 -15 0 1 7 0 2 7 0 1 1 Sample Output 1 16 18 31 33 16 16 18 1
[ { "submission_id": "aoj_1536_10238012", "code_snippet": "// AOJ #1536 Witch Craft Moves\n// 2025.2.21\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\t\treturn -n;\n\t}\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(int n) { // 整数の表示(出力)\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nconst int MAXN = 2005;\n\nvector<int> tree[MAXN];\nint cost[MAXN];\nint parent[MAXN], depth[MAXN], heavy[MAXN], sizeArr[MAXN];\nint head[MAXN], pos[MAXN];\nint curPos = 0;\n\nint segtree[4 * MAXN];\n\nint dfs(int u, int p) {\n parent[u] = p;\n depth[u] = (p == -1 ? 0 : depth[p] + 1);\n sizeArr[u] = 1;\n int maxSize = 0;\n heavy[u] = -1;\n for (int v : tree[u]) {\n if (v == p) continue;\n int subSize = dfs(v, u);\n if (subSize > maxSize) {\n maxSize = subSize;\n heavy[u] = v;\n }\n sizeArr[u] += subSize;\n }\n return sizeArr[u];\n}\n\nvoid decompose(int u, int h) {\n head[u] = h;\n pos[u] = curPos++;\n if (heavy[u] != -1) decompose(heavy[u], h);\n for (int v : tree[u]) {\n if (v == parent[u] || v == heavy[u]) continue;\n decompose(v, v);\n }\n}\n\nvoid segtree_build(int idx, int l, int r, const vector<int>& base) {\n if(l == r) {\n segtree[idx] = base[l];\n return;\n }\n int mid = (l + r) / 2;\n segtree_build(idx*2, l, mid, base);\n segtree_build(idx*2+1, mid+1, r, base);\n segtree[idx] = segtree[idx*2] + segtree[idx*2+1];\n}\n\nvoid segtree_update(int idx, int l, int r, int posIdx, int val) {\n if(l == r) {\n segtree[idx] += val;\n return;\n }\n int mid = (l + r) / 2;\n if(posIdx <= mid) segtree_update(idx*2, l, mid, posIdx, val);\n else segtree_update(idx*2+1, mid+1, r, posIdx, val);\n segtree[idx] = segtree[idx*2] + segtree[idx*2+1];\n}\n\nint segtree_query(int idx, int l, int r, int ql, int qr) {\n if(ql > r || qr < l) return 0;\n if(ql <= l && r <= qr) return segtree[idx];\n int mid = (l + r) / 2;\n return segtree_query(idx*2, l, mid, ql, qr) + segtree_query(idx*2+1, mid+1, r, ql, qr);\n}\n\nint query_path(int u, int v, int n) {\n int res = 0;\n while(head[u] != head[v]) {\n if(depth[head[u]] > depth[head[v]]) swap(u, v);\n res += segtree_query(1, 0, n-1, pos[head[v]], pos[v]);\n v = parent[head[v]];\n }\n if(depth[u] > depth[v]) swap(u, v);\n res += segtree_query(1, 0, n-1, pos[u], pos[v]);\n return res;\n}\n\nint main() {\n int N = Cin();\n for (int i = 1; i <= N; i++) cost[i] = Cin();\n\n for (int i = 1; i <= N-1; i++){\n int u = Cin(), v = Cin();\n tree[u].push_back(v);\n tree[v].push_back(u);\n }\n dfs(1, -1);\n decompose(1, 1);\n\n vector<int> base(N);\n for (int i = 1; i <= N; i++) base[pos[i]] = cost[i];\n segtree_build(1, 0, N-1, base);\n\n int Q = Cin();\n while(Q--){\n int t = Cin(), A = Cin(), B = Cin();\n if(t == 0) Cout(query_path(A, B, N));\n else segtree_update(1, 0, N-1, pos[A], B);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3712, "score_of_the_acc": -0.1759, "final_rank": 2 }, { "submission_id": "aoj_1536_10237997", "code_snippet": "// AOJ #1536 Witch Craft Moves\n// 2025.2.21\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst int MAXN = 2005;\n\nvector<int> tree[MAXN];\nint cost[MAXN];\nint parent[MAXN], depth[MAXN], heavy[MAXN], sizeArr[MAXN];\nint head[MAXN], pos[MAXN];\nint curPos = 0;\n\nint segtree[4 * MAXN];\n\nint dfs(int u, int p) {\n parent[u] = p;\n depth[u] = (p == -1 ? 0 : depth[p] + 1);\n sizeArr[u] = 1;\n int maxSize = 0;\n heavy[u] = -1;\n for (int v : tree[u]) {\n if (v == p) continue;\n int subSize = dfs(v, u);\n if (subSize > maxSize) {\n maxSize = subSize;\n heavy[u] = v;\n }\n sizeArr[u] += subSize;\n }\n return sizeArr[u];\n}\n\nvoid decompose(int u, int h) {\n head[u] = h;\n pos[u] = curPos++;\n if (heavy[u] != -1) decompose(heavy[u], h);\n for (int v : tree[u]) {\n if (v == parent[u] || v == heavy[u]) continue;\n decompose(v, v);\n }\n}\n\nvoid segtree_build(int idx, int l, int r, const vector<int>& base) {\n if(l == r) {\n segtree[idx] = base[l];\n return;\n }\n int mid = (l + r) / 2;\n segtree_build(idx*2, l, mid, base);\n segtree_build(idx*2+1, mid+1, r, base);\n segtree[idx] = segtree[idx*2] + segtree[idx*2+1];\n}\n\nvoid segtree_update(int idx, int l, int r, int posIdx, int val) {\n if(l == r) {\n segtree[idx] += val;\n return;\n }\n int mid = (l + r) / 2;\n if(posIdx <= mid) segtree_update(idx*2, l, mid, posIdx, val);\n else segtree_update(idx*2+1, mid+1, r, posIdx, val);\n segtree[idx] = segtree[idx*2] + segtree[idx*2+1];\n}\n\nint segtree_query(int idx, int l, int r, int ql, int qr) {\n if(ql > r || qr < l) return 0;\n if(ql <= l && r <= qr) return segtree[idx];\n int mid = (l + r) / 2;\n return segtree_query(idx*2, l, mid, ql, qr) + segtree_query(idx*2+1, mid+1, r, ql, qr);\n}\n\nint query_path(int u, int v, int n) {\n int res = 0;\n while(head[u] != head[v]) {\n if(depth[head[u]] > depth[head[v]]) swap(u, v);\n res += segtree_query(1, 0, n-1, pos[head[v]], pos[v]);\n v = parent[head[v]];\n }\n if(depth[u] > depth[v]) swap(u, v);\n res += segtree_query(1, 0, n-1, pos[u], pos[v]);\n return res;\n}\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int N;\n cin >> N;\n for (int i = 1; i <= N; i++) cin >> cost[i];\n\n for (int i = 1; i <= N-1; i++){\n int u, v;\n cin >> u >> v;\n tree[u].push_back(v);\n tree[v].push_back(u);\n }\n dfs(1, -1);\n decompose(1, 1);\n\n vector<int> base(N);\n for (int i = 1; i <= N; i++) base[pos[i]] = cost[i];\n segtree_build(1, 0, N-1, base);\n\n int Q;\n cin >> Q;\n while(Q--){\n int t, A, B;\n cin >> t >> A >> B;\n if(t == 0) cout << query_path(A, B, N) << endl;\n else segtree_update(1, 0, N-1, pos[A], B);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 800, "memory_kb": 3732, "score_of_the_acc": -0.4682, "final_rank": 5 }, { "submission_id": "aoj_1536_4766095", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\n#define NUM 2005\n\nint N = 1;\nint num_node;\nint root = 0,euler_index = 0;\nint MAX_LOG_V = 13;\nint LEFT[NUM],RIGHT[NUM];\n\n\nint parent[13][NUM],depth[NUM];\nvector<int> G[NUM];\nll W[NUM],sum_W[NUM];\nll uniformity[8*NUM],partial[8*NUM];\n\n\n//★★開区間なので注意★★\n\nvoid init(ll first_N){\n\twhile(N < first_N)N *= 2;\n}\n\nvoid add(ll left,ll right,ll value,ll node_id,ll node_left,ll node_right){\n\n\tif(right <= node_left || left >= node_right)return;\n\telse if(left <= node_left && right >= node_right){\n\t\tuniformity[node_id] += value;\n\t}else{\n\t\tpartial[node_id] += (min(right,node_right)-max(left,node_left))*value;\n\t\tadd(left,right,value,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tadd(left,right,value,2*node_id+2,(node_left+node_right)/2,node_right);\n\t}\n}\n\nll getSum(ll left,ll right,ll node_id,ll node_left,ll node_right){\n\tif(right <= node_left || left >= node_right)return 0;\n\telse if(left <= node_left && right >= node_right){\n\t\treturn (node_right-node_left)*uniformity[node_id]+partial[node_id];\n\n\t}else{\n\t\tll sum = (min(right,node_right)-max(left,node_left))*uniformity[node_id];\n\n\t\tsum += getSum(left,right,2*node_id+1,node_left,(node_left+node_right)/2);\n\t\tsum += getSum(left,right,2*node_id+2,(node_left+node_right)/2,node_right);\n\t\treturn sum;\n\t}\n}\n\n\nvoid dfs(int node_id,int parent_id,int DEPTH,ll total_W){\n\n\tparent[0][node_id] = parent_id;\n\tdepth[node_id] = DEPTH;\n\tLEFT[node_id] = euler_index++;\n\tsum_W[node_id] = total_W+W[node_id];\n\n\tfor(int i = 0; i < G[node_id].size(); i++){\n\t\tif(G[node_id][i] == parent_id)continue;\n\t\tdfs(G[node_id][i],node_id,DEPTH+1,sum_W[node_id]);\n\t}\n\tRIGHT[node_id] = euler_index++;\n}\n\n//初期化\nvoid init_lca(){\n\n\tdfs(root,-1,0,0);\n\n\tfor(int k = 0; k + 1 < MAX_LOG_V; k++){\n\t\tfor(int node_id = 0; node_id < num_node; node_id++){\n\t\t\tif(parent[k][node_id] < 0)parent[k+1][node_id] = -1; //node_idの2^k上のノードがルートノードより上なら、2^(k+1)上も同様とする\n\t\t\telse{\n\t\t\t\tparent[k+1][node_id] = parent[k][parent[k][node_id]];\n\t\t\t}\n\t\t}\n\t}\n}\n\nint lca(int left,int right){\n\n\tif(depth[left] > depth[right])swap(left,right);\n\tfor(int k = 0; k < MAX_LOG_V; k++){\n\t\tif((depth[right]-depth[left]) >> k & 1){\n\t\t\tright = parent[k][right];\n\t\t}\n\t}\n\tif(left == right)return left;\n\n\tfor(int k = MAX_LOG_V-1; k >= 0; k--){\n\t\tif(parent[k][left] != parent[k][right]){\n\t\t\tleft = parent[k][left];\n\t\t\tright = parent[k][right];\n\t\t}\n\t}\n\treturn parent[0][left];\n}\n\nint main(){\n\n\tscanf(\"%d\",&num_node);\n\n\tfor(int i = 0; i < num_node; i++){\n\n\t\tscanf(\"%lld\",&W[i]);\n\t}\n\n\n\tint from,to;\n\n\tfor(int i = 0; i < num_node-1; i++){\n\n\t\tscanf(\"%d %d\",&from,&to);\n\t\tfrom--;\n\t\tto--;\n\n\t\tG[from].push_back(to);\n\t\tG[to].push_back(from);\n\t}\n\n\tinit_lca();\n\n\tint first_N = RIGHT[root]+1; //セグ木における最下層のノードは、最低RIGHT[0]+1個あれば良い(+1は0オリジンのため)\n\n\tinit(first_N);\n\n\tfor(ll i = 0; i <= 2*N-2; i++){\n\t\tuniformity[i] = 0;\n\t\tpartial[i] = 0;\n\t}\n\n\tint num_query;\n\tscanf(\"%d\",&num_query);\n\n\tint command;\n\n\tll A,B,LCA;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %lld %lld\",&command,&A,&B);\n\n\t\tif(command == 0){\n\n\t\t\tA--;\n\t\t\tB--;\n\t\t\tLCA = lca(A,B);\n\n\t\t\tll cost_A = sum_W[A] + getSum(LEFT[A],LEFT[A]+1,0,0,N);\n\t\t\tll cost_B = sum_W[B] + getSum(LEFT[B],LEFT[B]+1,0,0,N);\n\t\t\tll cost_LCA = sum_W[LCA] + getSum(LEFT[LCA],LEFT[LCA]+1,0,0,N);\n\n\t\t\tprintf(\"%lld\\n\",cost_A+cost_B-2*cost_LCA+W[LCA]);\n\n\t\t}else{\n\n\t\t\tA--;\n\t\t\tadd(LEFT[A],RIGHT[A]+1,B,0,0,N);\n\t\t\tW[A] += B;\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 3704, "score_of_the_acc": -0.2641, "final_rank": 3 }, { "submission_id": "aoj_1536_3435876", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n//ノードの個数\nconst int MAX_V = 2002;\n//ダブリングに必要なサイズ(log(MAX_V))\nconst int MAX_LOG_V = 11;\n//木の隣接リスト表現\nvector<int> G[MAX_V];\n//根のノード番号\nint root = 0;\n\n//親を2^k回辿って到達するノード(根を通り過ぎる場合,-1)\nint parent[MAX_LOG_V][MAX_V];\n//根からの深さ\nint depth[MAX_V];\n\n//現在vに注目、親はp、深さd\nvoid lca_dfs(int v, int p, int d){\n parent[0][v]=p;\n depth[v]=d;\n rep(i,G[v].size()){\n if(G[v][i]!=p){ //親でなければ子\n lca_dfs(G[v][i], v, d+1);\n }\n }\n}\n\n//初期化\nvoid lca_init(int V){\n //parent[0]とdepthの初期化\n lca_dfs(root, -1, 0);\n //parentの初期化\n for(int k=0; k+1<MAX_LOG_V; ++k){\n for(int v=0; v<V; ++v){\n if(parent[k][v] < 0) parent[k+1][v]=-1;\n else parent[k+1][v] = parent[k][parent[k][v]];\n }\n }\n}\n\n//uとvのLCAを求める\nint lca(int u, int v){\n //uとvの深さが同じになるまで親を辿る\n if(depth[u] > depth[v]) swap(u,v);\n for(int k=0; k<MAX_LOG_V; ++k){\n if((depth[v]-depth[u])>>k & 1) v = parent[k][v];\n }\n if(u==v) return u;\n //二分探索でLCAを求める\n for(int k=MAX_LOG_V-1; k>=0; --k){\n if(parent[k][u] != parent[k][v]){\n u=parent[k][u];\n v=parent[k][v];\n }\n }\n return parent[0][u];\n}\n\nconst int N = 2002;\nint n;\nint c[N];\n\n// dist from root\nint d[N];\nvoid calc_dist(int v, int par){\n for(int e:G[v])if(e!=par){\n d[e] = d[v]+c[e];\n calc_dist(e,v);\n }\n}\n\nconst int B = 10000;\nconst int SZ = 100;\n\nint main(){\n scanf(\" %d\", &n);\n rep(i,n) scanf(\" %d\", &c[i]);\n\n rep(i,n-1){\n int a,b;\n scanf(\" %d %d\", &a, &b);\n --a;\n --b;\n G[a].pb(b);\n G[b].pb(a);\n }\n\n lca_init(n);\n\n int q;\n scanf(\" %d\", &q);\n\n vector<pair<int,int>> upd;\n rep(i,B){\n for(const auto &p:upd) c[p.fi] += p.se;\n upd.clear();\n\n d[root] = c[root];\n calc_dist(root, -1);\n\n rep(j,SZ){\n if(i*SZ + j == q) return 0;\n\n int type,x,y;\n scanf(\" %d %d %d\", &type, &x, &y);\n if(type == 0){\n // move x -> y\n --x;\n --y;\n int l = lca(x,y);\n if(l == y) swap(x,y);\n\n int ans = d[x]+d[y]-c[l];\n int par_l = parent[0][l];\n if(par_l != -1) ans -= 2*d[par_l];\n\n for(const auto &p:upd){\n int v = p.fi;\n int val = p.se;\n\n bool on_path = false;\n int vx = lca(v,x), vy = lca(v,y);\n if(l == x){\n if(vy==v && vx==x) on_path = true;\n }\n else{\n if(v==l) on_path = true;\n else{\n if(vx != vy){\n if(vx==l && vy==v) on_path = true;\n if(vx==v && vy==l) on_path = true;\n }\n }\n }\n\n if(on_path) ans += val;\n }\n\n printf(\"%d\\n\", ans);\n }\n else{\n // update c[x] <- y\n --x;\n upd.pb({x,y});\n }\n }\n }\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 1970, "memory_kb": 3592, "score_of_the_acc": -1.0728, "final_rank": 7 }, { "submission_id": "aoj_1536_3216379", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntypedef long long int Value1;\ntypedef long long int Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(0);\n\nstruct Node {\n\tValue1 sum;//更新された値. この値を参照する時は評価が完全に完了しているようにする.\n\tValue2 lazy;\t//遅延されている値を保存している\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\treturn l + r;\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\n\t\tdat[k].sum += dat[k].lazy*(b - a);\n\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inlineつけないと大変なことになるよ!(遅い)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(左の子番号) < 2*N (節点の数) のイメージで. 末端ノードじゃなきゃ伝搬するのと等価.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//次は君が伝搬してね☆って感じ.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // kの子が既に評価されていることが前提. 末端以外のときしか呼び出さないような位置に書くのでif文要らない.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r)を更新する. 区間は0-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n\n\t\tif (b <= l || r <= a) //[a,b)と[l,r)が交差している場合\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //一回遅延評価しとかないと都合悪いので.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r)に対するクエリの答えを得る. 区間は0-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n\n\t\tif (b <= l || r <= a) //[a,b)と[l,r)が交差している場合\n\t\t\treturn Zero1;\n\n\t\tif (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合\n\t\t\treturn dat[k].sum;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nlong long int dfs(const long long int num,vector<int>&ls,vector<int>&rs,int &a,vector<int>&sums,const vector<int>&nums,\n\tconst vector<vector<int>>&edges, const int now, const int from) {\n\tls[now]=a;\n\ta++;\n\tsums[now]=num+nums[now];\n\tfor (auto e : edges[now]) {\n\t\tif(e==from)continue;\n\t\telse {\n\t\t\tdfs(num+nums[now],ls,rs,a,sums,nums,edges,e,now);\n\t\t}\n\t}\n\trs[now]=a;\n\treturn sums[now];\n}\n\n\nclass Tree {\npublic:\n\tTree(int V, int root) : V(V), root(root), cnum(V), place(V), id(V) {\n\t\tT.resize(V);\n\t\tfor (int i = 0; i < MAXLOGV; i++) {\n\t\t\tparent[i].resize(V);\n\t\t}\n\t\tdepth.resize(V);\n\t}\n\t// uとvをつなぐ\n\t// lcaを求めることが主目的なので無向グラフとしている\n\tvoid unite(int u, int v) {\n\t\tT[u].push_back(v);\n\t\tT[v].push_back(u);\n\t}\n\tvoid unite(vector<vector<int>>&e) {\n\t\tT = e;\n\t}\n\t// initする\n\t// コンストラクタだけじゃなくてこれも呼ばないとlcaが求められないぞ\n\tvoid init() {\n\t\tdfs(root, 0, 0);\n\t\tint id = 0;\n\t\tgetid(root, 0, id);\n\t}\n\t// uとvのlcaを求める\n\tint lca(int u, int v) const {\n\t\tif (depth[u] > depth[v]) swap(u, v);\n\t\tfor (int k = 0; k < MAXLOGV; k++) {\n\t\t\tif ((depth[v] - depth[u]) >> k & 1) {\n\t\t\t\tv = parent[k][v];\n\t\t\t}\n\t\t}\n\t\tif (u == v) return u;\n\t\tfor (int k = MAXLOGV - 1; k >= 0; k--) {\n\t\t\tif (parent[k][u] != parent[k][v]) {\n\t\t\t\tu = parent[k][u];\n\t\t\t\tv = parent[k][v];\n\t\t\t}\n\t\t}\n\t\treturn parent[0][u];\n\t}\n\t// uとvの距離を求める\n\t// edgeを定義しないといけない時はこれじゃダメ\n\tint dist(int u, int v) const {\n\t\tint p = lca(u, v);\n\t\treturn (depth[u] - depth[p]) + (depth[v] - depth[p]);\n\t}\n\tint dfs(int v, int p, int d) {\n\t\tparent[0][v] = p;\n\t\tdepth[v] = d;\n\t\tcnum[v] = 0;\n\t\tfor (int i = 1; i < MAXLOGV; i++) {\n\t\t\tparent[i][v] = parent[i - 1][parent[i - 1][v]];\n\t\t}\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) cnum[v] += dfs(next, v, d + 1);\n\t\t}\n\t\treturn cnum[v] + 1;\n\t}\n\n\tvoid dfs2(int v, int p, vector<vector<int>>&doubles, const vector<int>&nums) {\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) dfs2(next, v, doubles, nums);\n\t\t}\n\t\tdoubles[0][v] = nums[v];\n\t\tfor (int j = 1; j < MAXLOGV; ++j) {\n\t\t\tdoubles[j][v] = min(doubles[j][v], doubles[j - 1][v]);\n\t\t}\n\t\tfor (int j = 0; j < MAXLOGV - 1; ++j) {\n\t\t\tdoubles[j + 1][parent[j][v]] = min(doubles[j + 1][parent[j][v]], doubles[j][v]);\n\t\t}\n\t}\n\t//ここでは親から距離2^iの部分木の最小値を求めている\n\tvector<vector<int>>get_doubles(const vector<int>&nums) {\n\t\tvector<vector<int>>doubles(MAXLOGV, vector<int>(V, 1e9));\n\t\tdfs2(root, -1, doubles, nums);\n\t\treturn doubles;\n\t}\n\n\tvoid getid(const int v, const int p, int &nplace) {\n\t\tplace[v] = nplace;\n\t\tid[nplace] = v;\n\t\tnplace++;\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) getid(next, v, nplace);\n\t\t}\n\t}\n\tstatic const int MAXLOGV = 25;\n\t// グラフの隣接リスト表現\n\tvector<vector<int> > T;\n\t// 頂点の数\n\tint V;\n\t// 根ノードの番号\n\tint root;\n\n\t// 親ノード\n\tvector<int> parent[MAXLOGV];\n\t// 根からの深さ\n\tvector<int> depth;\n\n\t//子の数\n\tvector<int>cnum;\n\n\t//変換\n\tvector<int>place;\n\tvector<int>id;\n\n};\n\nint main() {\n\tint N;cin>>N;\n\tlazy_segtree seg(N);\n\tvector<int>ls(N),rs(N),nums(N),sums(N);\n\tfor (int i = 0; i < N; ++i) {\n\t\tint a;cin>>a;\n\t\tnums[i]=a;\n\t}\n\tvector<vector<int>>edges(N);\n\tfor (int i = 0; i < N-1; ++i) {\n\t\tint a,b;cin>>a>>b;a--;b--;\n\t\tedges[a].push_back(b);\n\t\tedges[b].push_back(a);\n\n\t}\n\tint pp=0;\n\tdfs(0,ls,rs,pp,sums,nums,edges,0,-1);\n\tfor (int i = 0; i < N; ++i) {\n\t\tseg.update(ls[i],ls[i]+1,sums[i]);\n\t}\n\tTree tree(N,0);\n\ttree.unite(edges);\n\ttree.init();\n\tint Q;cin>>Q;\n\twhile (Q--) {\n\t\tint type;\n\t\tcin>>type;\n\t\tif (type == 0) {\n\t\t\tint a,b;cin>>a>>b;a--;b--;\n\t\t\ta=ls[a];\n\t\t\tb=ls[b];\n\t\t\tint ab_lca=tree.lca(a,b);\n\t\t\tlong long int answer=seg.get(a,a+1)+seg.get(b,b+1)-\n\t\t\t\tseg.get(ab_lca,ab_lca+1);\n\t\t\tif (ab_lca != 0) {\n\t\t\t\tanswer-=seg.get(tree.parent[0][ab_lca],tree.parent[0][ab_lca]+1);\n\t\t\t}\n\t\t\tcout<<answer<<endl;\n\t\t}\n\t\telse {\n\t\t\tint a,c;cin>>a>>c;a--;\n\t\t\tseg.update(ls[a],rs[a],c);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 160, "memory_kb": 3428, "score_of_the_acc": -0.1244, "final_rank": 16 }, { "submission_id": "aoj_1536_3216377", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntypedef long long int Value1;\ntypedef long long int Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(0);\n\nstruct Node {\n\tValue1 sum;//更新された値. この値を参照する時は評価が完全に完了しているようにする.\n\tValue2 lazy;\t//遅延されている値を保存している\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\treturn l + r;\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\n\t\tdat[k].sum += dat[k].lazy*(b - a);\n\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inlineつけないと大変なことになるよ!(遅い)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(左の子番号) < 2*N (節点の数) のイメージで. 末端ノードじゃなきゃ伝搬するのと等価.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//次は君が伝搬してね☆って感じ.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // kの子が既に評価されていることが前提. 末端以外のときしか呼び出さないような位置に書くのでif文要らない.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r)を更新する. 区間は0-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n\n\t\tif (b <= l || r <= a) //[a,b)と[l,r)が交差している場合\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //一回遅延評価しとかないと都合悪いので.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r)に対するクエリの答えを得る. 区間は0-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n\n\t\tif (b <= l || r <= a) //[a,b)と[l,r)が交差している場合\n\t\t\treturn Zero1;\n\n\t\tif (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合\n\t\t\treturn dat[k].sum;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nlong long int dfs(const long long int num,vector<int>&ls,vector<int>&rs,int &a,vector<int>&sums,const vector<int>&nums,\n\tconst vector<vector<int>>&edges, const int now, const int from) {\n\tls[now]=a;\n\ta++;\n\tsums[now]=num+nums[now];\n\tfor (auto e : edges[now]) {\n\t\tif(e==from)continue;\n\t\telse {\n\t\t\tdfs(num+nums[now],ls,rs,a,sums,nums,edges,e,now);\n\t\t}\n\t}\n\trs[now]=a;\n\treturn sums[now];\n}\n\n\nclass Tree {\npublic:\n\tTree(int V, int root) : V(V), root(root), cnum(V), place(V), id(V) {\n\t\tT.resize(V);\n\t\tfor (int i = 0; i < MAXLOGV; i++) {\n\t\t\tparent[i].resize(V);\n\t\t}\n\t\tdepth.resize(V);\n\t}\n\t// uとvをつなぐ\n\t// lcaを求めることが主目的なので無向グラフとしている\n\tvoid unite(int u, int v) {\n\t\tT[u].push_back(v);\n\t\tT[v].push_back(u);\n\t}\n\tvoid unite(vector<vector<int>>&e) {\n\t\tT = e;\n\t}\n\t// initする\n\t// コンストラクタだけじゃなくてこれも呼ばないとlcaが求められないぞ\n\tvoid init() {\n\t\tdfs(root, 0, 0);\n\t\tint id = 0;\n\t\tgetid(root, 0, id);\n\t}\n\t// uとvのlcaを求める\n\tint lca(int u, int v) const {\n\t\tif (depth[u] > depth[v]) swap(u, v);\n\t\tfor (int k = 0; k < MAXLOGV; k++) {\n\t\t\tif ((depth[v] - depth[u]) >> k & 1) {\n\t\t\t\tv = parent[k][v];\n\t\t\t}\n\t\t}\n\t\tif (u == v) return u;\n\t\tfor (int k = MAXLOGV - 1; k >= 0; k--) {\n\t\t\tif (parent[k][u] != parent[k][v]) {\n\t\t\t\tu = parent[k][u];\n\t\t\t\tv = parent[k][v];\n\t\t\t}\n\t\t}\n\t\treturn parent[0][u];\n\t}\n\t// uとvの距離を求める\n\t// edgeを定義しないといけない時はこれじゃダメ\n\tint dist(int u, int v) const {\n\t\tint p = lca(u, v);\n\t\treturn (depth[u] - depth[p]) + (depth[v] - depth[p]);\n\t}\n\tint dfs(int v, int p, int d) {\n\t\tparent[0][v] = p;\n\t\tdepth[v] = d;\n\t\tcnum[v] = 0;\n\t\tfor (int i = 1; i < MAXLOGV; i++) {\n\t\t\tparent[i][v] = parent[i - 1][parent[i - 1][v]];\n\t\t}\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) cnum[v] += dfs(next, v, d + 1);\n\t\t}\n\t\treturn cnum[v] + 1;\n\t}\n\n\tvoid dfs2(int v, int p, vector<vector<int>>&doubles, const vector<int>&nums) {\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) dfs2(next, v, doubles, nums);\n\t\t}\n\t\tdoubles[0][v] = nums[v];\n\t\tfor (int j = 1; j < MAXLOGV; ++j) {\n\t\t\tdoubles[j][v] = min(doubles[j][v], doubles[j - 1][v]);\n\t\t}\n\t\tfor (int j = 0; j < MAXLOGV - 1; ++j) {\n\t\t\tdoubles[j + 1][parent[j][v]] = min(doubles[j + 1][parent[j][v]], doubles[j][v]);\n\t\t}\n\t}\n\t//ここでは親から距離2^iの部分木の最小値を求めている\n\tvector<vector<int>>get_doubles(const vector<int>&nums) {\n\t\tvector<vector<int>>doubles(MAXLOGV, vector<int>(V, 1e9));\n\t\tdfs2(root, -1, doubles, nums);\n\t\treturn doubles;\n\t}\n\n\tvoid getid(const int v, const int p, int &nplace) {\n\t\tplace[v] = nplace;\n\t\tid[nplace] = v;\n\t\tnplace++;\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) getid(next, v, nplace);\n\t\t}\n\t}\n\tstatic const int MAXLOGV = 25;\n\t// グラフの隣接リスト表現\n\tvector<vector<int> > T;\n\t// 頂点の数\n\tint V;\n\t// 根ノードの番号\n\tint root;\n\n\t// 親ノード\n\tvector<int> parent[MAXLOGV];\n\t// 根からの深さ\n\tvector<int> depth;\n\n\t//子の数\n\tvector<int>cnum;\n\n\t//変換\n\tvector<int>place;\n\tvector<int>id;\n\n};\n\nint main() {\n\tint N;cin>>N;\n\tlazy_segtree seg(N);\n\tvector<int>ls(N),rs(N),nums(N),sums(N);\n\tfor (int i = 0; i < N; ++i) {\n\t\tint a;cin>>a;\n\t\tnums[i]=a;\n\t}\n\tvector<vector<int>>edges(N);\n\tfor (int i = 0; i < N-1; ++i) {\n\t\tint a,b;cin>>a>>b;a--;b--;\n\t\tedges[a].push_back(b);\n\t\tedges[b].push_back(a);\n\n\t}\n\tint pp=0;\n\tdfs(0,ls,rs,pp,sums,nums,edges,0,-1);\n\tfor (int i = 0; i < N; ++i) {\n\t\tseg.update(i,i+1,sums[i]);\n\t}\n\tTree tree(N,0);\n\ttree.unite(edges);\n\ttree.init();\n\tint Q;cin>>Q;\n\twhile (Q--) {\n\t\tint type;\n\t\tcin>>type;\n\t\tif (type == 0) {\n\t\t\tint a,b;cin>>a>>b;a--;b--;\n\t\t\tint ab_lca=tree.lca(a,b);\n\t\t\tlong long int answer=seg.get(a,a+1)+seg.get(b,b+1)-\n\t\t\t\tseg.get(ab_lca,ab_lca+1);\n\t\t\tif (ab_lca != 0) {\n\t\t\t\tanswer-=seg.get(tree.parent[0][ab_lca],tree.parent[0][ab_lca]+1);\n\t\t\t}\n\t\t\tcout<<answer<<endl;\n\t\t}\n\t\telse {\n\t\t\tint a,c;cin>>a>>c;a--;\n\t\t\tseg.update(ls[a],rs[a],c);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 160, "memory_kb": 3440, "score_of_the_acc": -0.1248, "final_rank": 18 }, { "submission_id": "aoj_1536_3216375", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntypedef long long int Value1;\ntypedef long long int Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(0);\n\nstruct Node {\n\tValue1 sum;//更新された値. この値を参照する時は評価が完全に完了しているようにする.\n\tValue2 lazy;\t//遅延されている値を保存している\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\treturn l + r;\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\n\t\tdat[k].sum += dat[k].lazy*(b - a);\n\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inlineつけないと大変なことになるよ!(遅い)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(左の子番号) < 2*N (節点の数) のイメージで. 末端ノードじゃなきゃ伝搬するのと等価.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//次は君が伝搬してね☆って感じ.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // kの子が既に評価されていることが前提. 末端以外のときしか呼び出さないような位置に書くのでif文要らない.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r)を更新する. 区間は0-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n\n\t\tif (b <= l || r <= a) //[a,b)と[l,r)が交差している場合\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //一回遅延評価しとかないと都合悪いので.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r)に対するクエリの答えを得る. 区間は0-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n\n\t\tif (b <= l || r <= a) //[a,b)と[l,r)が交差している場合\n\t\t\treturn Zero1;\n\n\t\tif (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合\n\t\t\treturn dat[k].sum;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nlong long int dfs(const long long int num,vector<int>&ls,vector<int>&rs,int &a,vector<int>&sums,const vector<int>&nums,\n\tconst vector<vector<int>>&edges, const int now, const int from) {\n\tls[now]=a;\n\tsums[now]=num+nums[now];\n\tfor (auto e : edges[now]) {\n\t\tif(e==from)continue;\n\t\telse {\n\t\t\tdfs(num+nums[now],ls,rs,a,sums,nums,edges,e,now);\n\t\t}\n\t}\n\n\ta++;\n\trs[now]=a;\n\treturn sums[now];\n}\n\n\nclass Tree {\npublic:\n\tTree(int V, int root) : V(V), root(root), cnum(V), place(V), id(V) {\n\t\tT.resize(V);\n\t\tfor (int i = 0; i < MAXLOGV; i++) {\n\t\t\tparent[i].resize(V);\n\t\t}\n\t\tdepth.resize(V);\n\t}\n\t// uとvをつなぐ\n\t// lcaを求めることが主目的なので無向グラフとしている\n\tvoid unite(int u, int v) {\n\t\tT[u].push_back(v);\n\t\tT[v].push_back(u);\n\t}\n\tvoid unite(vector<vector<int>>&e) {\n\t\tT = e;\n\t}\n\t// initする\n\t// コンストラクタだけじゃなくてこれも呼ばないとlcaが求められないぞ\n\tvoid init() {\n\t\tdfs(root, 0, 0);\n\t\tint id = 0;\n\t\tgetid(root, 0, id);\n\t}\n\t// uとvのlcaを求める\n\tint lca(int u, int v) const {\n\t\tif (depth[u] > depth[v]) swap(u, v);\n\t\tfor (int k = 0; k < MAXLOGV; k++) {\n\t\t\tif ((depth[v] - depth[u]) >> k & 1) {\n\t\t\t\tv = parent[k][v];\n\t\t\t}\n\t\t}\n\t\tif (u == v) return u;\n\t\tfor (int k = MAXLOGV - 1; k >= 0; k--) {\n\t\t\tif (parent[k][u] != parent[k][v]) {\n\t\t\t\tu = parent[k][u];\n\t\t\t\tv = parent[k][v];\n\t\t\t}\n\t\t}\n\t\treturn parent[0][u];\n\t}\n\t// uとvの距離を求める\n\t// edgeを定義しないといけない時はこれじゃダメ\n\tint dist(int u, int v) const {\n\t\tint p = lca(u, v);\n\t\treturn (depth[u] - depth[p]) + (depth[v] - depth[p]);\n\t}\n\tint dfs(int v, int p, int d) {\n\t\tparent[0][v] = p;\n\t\tdepth[v] = d;\n\t\tcnum[v] = 0;\n\t\tfor (int i = 1; i < MAXLOGV; i++) {\n\t\t\tparent[i][v] = parent[i - 1][parent[i - 1][v]];\n\t\t}\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) cnum[v] += dfs(next, v, d + 1);\n\t\t}\n\t\treturn cnum[v] + 1;\n\t}\n\n\tvoid dfs2(int v, int p, vector<vector<int>>&doubles, const vector<int>&nums) {\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) dfs2(next, v, doubles, nums);\n\t\t}\n\t\tdoubles[0][v] = nums[v];\n\t\tfor (int j = 1; j < MAXLOGV; ++j) {\n\t\t\tdoubles[j][v] = min(doubles[j][v], doubles[j - 1][v]);\n\t\t}\n\t\tfor (int j = 0; j < MAXLOGV - 1; ++j) {\n\t\t\tdoubles[j + 1][parent[j][v]] = min(doubles[j + 1][parent[j][v]], doubles[j][v]);\n\t\t}\n\t}\n\t//ここでは親から距離2^iの部分木の最小値を求めている\n\tvector<vector<int>>get_doubles(const vector<int>&nums) {\n\t\tvector<vector<int>>doubles(MAXLOGV, vector<int>(V, 1e9));\n\t\tdfs2(root, -1, doubles, nums);\n\t\treturn doubles;\n\t}\n\n\tvoid getid(const int v, const int p, int &nplace) {\n\t\tplace[v] = nplace;\n\t\tid[nplace] = v;\n\t\tnplace++;\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) getid(next, v, nplace);\n\t\t}\n\t}\n\tstatic const int MAXLOGV = 25;\n\t// グラフの隣接リスト表現\n\tvector<vector<int> > T;\n\t// 頂点の数\n\tint V;\n\t// 根ノードの番号\n\tint root;\n\n\t// 親ノード\n\tvector<int> parent[MAXLOGV];\n\t// 根からの深さ\n\tvector<int> depth;\n\n\t//子の数\n\tvector<int>cnum;\n\n\t//変換\n\tvector<int>place;\n\tvector<int>id;\n\n};\n\nint main() {\n\tint N;cin>>N;\n\tlazy_segtree seg(N);\n\tvector<int>ls(N),rs(N),nums(N),sums(N);\n\tfor (int i = 0; i < N; ++i) {\n\t\tint a;cin>>a;\n\t\tnums[i]=a;\n\t}\n\tvector<vector<int>>edges(N);\n\tfor (int i = 0; i < N-1; ++i) {\n\t\tint a,b;cin>>a>>b;a--;b--;\n\t\tedges[a].push_back(b);\n\t\tedges[b].push_back(a);\n\n\t}\n\tint pp=0;\n\tdfs(0,ls,rs,pp,sums,nums,edges,0,-1);\n\tfor (int i = 0; i < N; ++i) {\n\t\tseg.update(i,i+1,sums[i]);\n\t}\n\tTree tree(N,0);\n\ttree.unite(edges);\n\ttree.init();\n\tint Q;cin>>Q;\n\twhile (Q--) {\n\t\tint type;\n\t\tcin>>type;\n\t\tif (type == 0) {\n\t\t\tint a,b;cin>>a>>b;a--;b--;\n\t\t\tint ab_lca=tree.lca(a,b);\n\t\t\tlong long int answer=seg.get(a,a+1)+seg.get(b,b+1)-\n\t\t\t\tseg.get(ab_lca,ab_lca+1);\n\t\t\tif (ab_lca != 0) {\n\t\t\t\tanswer-=seg.get(tree.parent[0][ab_lca],tree.parent[0][ab_lca]+1);\n\t\t\t}\n\t\t\tcout<<answer<<endl;\n\t\t}\n\t\telse {\n\t\t\tint a,c;cin>>a>>c;a--;\n\t\t\tseg.update(ls[a],rs[a],c);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 160, "memory_kb": 3416, "score_of_the_acc": -0.124, "final_rank": 15 }, { "submission_id": "aoj_1536_3216374", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\ntypedef long long int Value1;\ntypedef long long int Value2;\nconst Value1 Zero1(0);\nconst Value2 Zero2(0);\n\nstruct Node {\n\tValue1 sum;//更新された値. この値を参照する時は評価が完全に完了しているようにする.\n\tValue2 lazy;\t//遅延されている値を保存している\n\tNode() :sum(Zero1) {\n\t\tlazy = Zero2;\n\t}\n};\nstruct lazy_segtree {\n\tint N;\n\tvector<Node>dat;\n\tlazy_segtree(int n) :N(1) {\n\t\twhile (N < n) N *= 2;\n\t\tdat.resize(2 * N);\n\t}\n\n\tValue2 lazy_connect(const Value2 l, const Value2 r) {\n\t\treturn l + r;\n\t}\n\n\tvoid lazy_func(const int k, const int a, const int b) {\n\n\t\tdat[k].sum += dat[k].lazy*(b - a);\n\n\t}\n\n\tValue1 connect(const Value1 l, const Value1 r) {\n\t\treturn l + r;\n\n\t}\n\n\t// inlineつけないと大変なことになるよ!(遅い)\n\tinline void lazy_evaluate_node(int k, int a, int b) {\n\t\tlazy_func(k, a, b);\n\t\tif (k < N) { // 2*k(左の子番号) < 2*N (節点の数) のイメージで. 末端ノードじゃなきゃ伝搬するのと等価.\n\t\t\tdat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);\t//次は君が伝搬してね☆って感じ.\n\t\t\tdat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);\n\t\t}\n\t\tdat[k].lazy = Zero2;\n\t}\n\n\tinline void update_node(int k) { // kの子が既に評価されていることが前提. 末端以外のときしか呼び出さないような位置に書くのでif文要らない.\n\t\tdat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);\n\n\t}\n\n\t// update(l,r,v) := [l,r)を更新する. 区間は0-indexed.\n\tvoid update(int l, int r, Value2 v, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\t\tif (l < 0 || r < 0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); \t// とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n\n\t\tif (b <= l || r <= a) //[a,b)と[l,r)が交差している場合\n\t\t\treturn;\n\t\tif (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合\n\t\t\tdat[k].lazy = lazy_connect(dat[k].lazy, v);\n\t\t\tlazy_evaluate_node(k, a, b); //一回遅延評価しとかないと都合悪いので.\n\t\t\treturn;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tupdate(l, r, v, 2 * k, a, m);\n\t\tupdate(l, r, v, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t}\n\t//get(l,r) := [l,r)に対するクエリの答えを得る. 区間は0-indexed.\n\tValue1 get(int l, int r, int k = 1, int a = 0, int b = -1) {\n\t\tif (b == -1)b = N;\n\n\t\tif (l < 0 || r<0)assert(false);\n\t\tlazy_evaluate_node(k, a, b); // とりあえず辿ったノードは都合がいいので伝搬しとけ精神.\n\n\t\tif (b <= l || r <= a) //[a,b)と[l,r)が交差している場合\n\t\t\treturn Zero1;\n\n\t\tif (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合\n\t\t\treturn dat[k].sum;\n\t\t}\n\n\t\tint m = (a + b) / 2;\n\t\tValue1 vl = get(l, r, 2 * k, a, m);\n\t\tValue1 vr = get(l, r, 2 * k + 1, m, b);\n\t\tupdate_node(k);\n\t\treturn connect(vl, vr);\n\t}\n};\n\nlong long int dfs(const long long int num,vector<int>&ls,vector<int>&rs,int &a,vector<int>&sums,const vector<int>&nums,\n\tconst vector<vector<int>>&edges, const int now, const int from) {\n\tls[now]=a;\n\tsums[now]=num+nums[now];\n\tfor (auto e : edges[now]) {\n\t\tif(e==from)continue;\n\t\telse {\n\t\t\tdfs(num+nums[now],ls,rs,a,sums,nums,edges,e,now);\n\t\t}\n\t}\n\n\ta++;\n\trs[now]=a;\n\treturn sums[now];\n}\n\n\nclass Tree {\npublic:\n\tTree(int V, int root) : V(V), root(root), cnum(V), place(V), id(V) {\n\t\tT.resize(V);\n\t\tfor (int i = 0; i < MAXLOGV; i++) {\n\t\t\tparent[i].resize(V);\n\t\t}\n\t\tdepth.resize(V);\n\t}\n\t// uとvをつなぐ\n\t// lcaを求めることが主目的なので無向グラフとしている\n\tvoid unite(int u, int v) {\n\t\tT[u].push_back(v);\n\t\tT[v].push_back(u);\n\t}\n\tvoid unite(vector<vector<int>>&e) {\n\t\tT = e;\n\t}\n\t// initする\n\t// コンストラクタだけじゃなくてこれも呼ばないとlcaが求められないぞ\n\tvoid init() {\n\t\tdfs(root, 0, 0);\n\t\tint id = 0;\n\t\tgetid(root, 0, id);\n\t}\n\t// uとvのlcaを求める\n\tint lca(int u, int v) const {\n\t\tif (depth[u] > depth[v]) swap(u, v);\n\t\tfor (int k = 0; k < MAXLOGV; k++) {\n\t\t\tif ((depth[v] - depth[u]) >> k & 1) {\n\t\t\t\tv = parent[k][v];\n\t\t\t}\n\t\t}\n\t\tif (u == v) return u;\n\t\tfor (int k = MAXLOGV - 1; k >= 0; k--) {\n\t\t\tif (parent[k][u] != parent[k][v]) {\n\t\t\t\tu = parent[k][u];\n\t\t\t\tv = parent[k][v];\n\t\t\t}\n\t\t}\n\t\treturn parent[0][u];\n\t}\n\t// uとvの距離を求める\n\t// edgeを定義しないといけない時はこれじゃダメ\n\tint dist(int u, int v) const {\n\t\tint p = lca(u, v);\n\t\treturn (depth[u] - depth[p]) + (depth[v] - depth[p]);\n\t}\n\tint dfs(int v, int p, int d) {\n\t\tparent[0][v] = p;\n\t\tdepth[v] = d;\n\t\tcnum[v] = 0;\n\t\tfor (int i = 1; i < MAXLOGV; i++) {\n\t\t\tparent[i][v] = parent[i - 1][parent[i - 1][v]];\n\t\t}\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) cnum[v] += dfs(next, v, d + 1);\n\t\t}\n\t\treturn cnum[v] + 1;\n\t}\n\n\tvoid dfs2(int v, int p, vector<vector<int>>&doubles, const vector<int>&nums) {\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) dfs2(next, v, doubles, nums);\n\t\t}\n\t\tdoubles[0][v] = nums[v];\n\t\tfor (int j = 1; j < MAXLOGV; ++j) {\n\t\t\tdoubles[j][v] = min(doubles[j][v], doubles[j - 1][v]);\n\t\t}\n\t\tfor (int j = 0; j < MAXLOGV - 1; ++j) {\n\t\t\tdoubles[j + 1][parent[j][v]] = min(doubles[j + 1][parent[j][v]], doubles[j][v]);\n\t\t}\n\t}\n\t//ここでは親から距離2^iの部分木の最小値を求めている\n\tvector<vector<int>>get_doubles(const vector<int>&nums) {\n\t\tvector<vector<int>>doubles(MAXLOGV, vector<int>(V, 1e9));\n\t\tdfs2(root, -1, doubles, nums);\n\t\treturn doubles;\n\t}\n\n\tvoid getid(const int v, const int p, int &nplace) {\n\t\tplace[v] = nplace;\n\t\tid[nplace] = v;\n\t\tnplace++;\n\t\tfor (int next : T[v]) {\n\t\t\tif (next != p) getid(next, v, nplace);\n\t\t}\n\t}\n\tstatic const int MAXLOGV = 25;\n\t// グラフの隣接リスト表現\n\tvector<vector<int> > T;\n\t// 頂点の数\n\tint V;\n\t// 根ノードの番号\n\tint root;\n\n\t// 親ノード\n\tvector<int> parent[MAXLOGV];\n\t// 根からの深さ\n\tvector<int> depth;\n\n\t//子の数\n\tvector<int>cnum;\n\n\t//変換\n\tvector<int>place;\n\tvector<int>id;\n\n};\n\nint main() {\n\tint N;cin>>N;\n\tlazy_segtree seg(N);\n\tvector<int>ls(N),rs(N),nums(N),sums(N);\n\tfor (int i = 0; i < N; ++i) {\n\t\tint a;cin>>a;\n\t\tnums[i]=a;\n\t}\n\tvector<vector<int>>edges(N);\n\tfor (int i = 0; i < N-1; ++i) {\n\t\tint a,b;cin>>a>>b;a--;b--;\n\t\tedges[a].push_back(b);\n\t\tedges[b].push_back(a);\n\n\t}\n\tint pp=0;\n\tdfs(0,ls,rs,pp,sums,nums,edges,0,-1);\n\tfor (int i = 0; i < N; ++i) {\n\t\tseg.update(i,i+1,sums[i]);\n\t}\n\tTree tree(N,0);\n\ttree.unite(edges);\n\ttree.init();\n\tint Q;cin>>Q;\n\twhile (Q--) {\n\t\tint type;\n\t\tcin>>type;\n\t\tif (type == 0) {\n\t\t\tint a,b;cin>>a>>b;a--;b--;\n\t\t\tint ab_lca=tree.lca(a,b);\n\t\t\tlong long int answer=seg.get(a,a+1)+seg.get(b,b+1)-\n\t\t\t\tseg.get(ab_lca,ab_lca+1);\n\t\t\tcout<<answer<<endl;\n\t\t}\n\t\telse {\n\t\t\tint a,c;cin>>a>>c;a--;\n\t\t\tseg.update(ls[a],rs[a],c);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 160, "memory_kb": 3428, "score_of_the_acc": -0.1244, "final_rank": 16 }, { "submission_id": "aoj_1536_1102909", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <memory>\nusing namespace std;\n\ntypedef long long ll;\n\nstruct BIT {\n int n;\n vector<ll> data;\n BIT(int size) : n(size), data(size+1, 0LL) { }\n void add(int k, ll a) {\n k++;\n while(k <= n) { data[k] += a; k += (k & -k); }\n }\n // [0, i) の和(半開区間に注意)\n ll sum(int k) {\n ll s = 0;\n while(k > 0) { s += data[k]; k -= (k & -k); }\n return s;\n }\n};\n\nstruct LCA {\n int N, logN; // logN == ceil(lg(N))\n vector<vector<int>> G;\n vector<int> depth;\n // vから根の方向に2^k回登ったときのノード parent[k][v]\n vector<vector<int>> parent;\n\n LCA(int size) : N(size), G(size), depth(size) {\n logN = 0;\n for(int x = 1; x < N; x *= 2) logN++;\n parent.assign(max(logN, 1), vector<int>(N));\n }\n // どちらが根であってもOK\n void add_edge(int u, int v) {\n G[u].push_back(v);\n G[v].push_back(u);\n }\n void dfs(int v, int par, int dep) {\n depth[v] = dep;\n parent[0][v] = par;\n for(int next : G[v]) {\n if(next != par) dfs(next, v, dep + 1);\n }\n }\n void init(int root) {\n dfs(root, -1, 0);\n for(int k = 1; k < logN; k++) {\n for(int v = 0; v < N; v++) {\n if(parent[k - 1][v] == -1) parent[k][v] = -1;\n else parent[k][v] = parent[k - 1][parent[k - 1][v]];\n }\n }\n }\n int lca(int u, int v) {\n if(depth[u] > depth[v]) swap(u, v); // vのほうが深くなる\n // uとvの深さが同じになるまでvを根の方向に移動する\n for(int k = 0; k < logN; k++) {\n if(((depth[v] - depth[u]) >> k) & 1) {\n v = parent[k][v];\n }\n }\n if(u == v) return u;\n for(int k = logN - 1; k >= 0; k--) {\n if(parent[k][u] != parent[k][v]) {\n u = parent[k][u];\n v = parent[k][v];\n }\n }\n return parent[0][u];\n }\n};\n\nint dfs(int v, int parent, const vector<vector<int>> &G, vector<int> &child, vector<int> &order, int &k) {\n order[v] = k++;\n int cnt = 0;\n for(int next : G[v]) {\n if(next == parent) continue;\n cnt += dfs(next, v, G, child, order, k);\n }\n return child[v] = cnt + 1;\n}\n\nshared_ptr<BIT> bit0, bit1;\n\nvoid add(int l, int r, ll x) {\n if(r < l) swap(r, l);\n bit0->add(l, -x * l);\n bit1->add(l, x);\n bit0->add(r, x * r);\n bit1->add(r, -x);\n}\n\nll sum(int l, int r) {\n return bit0->sum(r) + r*bit1->sum(r) - bit0->sum(l) - l*bit1->sum(l);\n}\n\nint main() {\n int N; cin >> N;\n vector<int> COST(N);\n for(int i = 0; i < N; ++i) {\n cin >> COST[i];\n }\n LCA lca(N);\n vector<vector<int>> TREE(N);\n for(int i = 0; i < N - 1; ++i) {\n int u, v; cin >> u >> v;\n --u; --v;\n TREE[u].push_back(v);\n TREE[v].push_back(u);\n lca.add_edge(u, v);\n }\n lca.init(0);\n vector<int> child(N); // 子の数\n vector<int> order(N); // dfs順\n int k = 0;\n dfs(0, -1, TREE, child, order, k);\n bit0 = make_shared<BIT>(N);\n bit1 = make_shared<BIT>(N);\n for(int i = 0; i < N; ++i) {\n add(order[i], order[i] + child[i], COST[i]);\n }\n int Q; cin >> Q;\n for(int i = 0; i < Q; ++i) {\n int type, from, to; cin >> type >> from >> to;\n if(type == 0) {\n --from; --to;\n int v = lca.lca(from, to);\n ll res = 0LL;\n res += sum(order[from], order[from] + 1);\n res += sum(order[to], order[to] + 1);\n res -= 2 * sum(0, order[v]);\n res -= sum(order[v], order[v] + 1);\n cout << res << endl;\n }\n else {\n --from;\n add(order[from], order[from] + child[from], to);\n }\n }\n}", "accuracy": 0.12903225806451613, "time_ms": 160, "memory_kb": 1492, "score_of_the_acc": -0.0574, "final_rank": 13 }, { "submission_id": "aoj_1536_895106", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define dump(...) (cerr<<#__VA_ARGS__<<\" = \"<<(DUMP(),__VA_ARGS__).str()<<endl)\nstruct DUMP : stringstream {\n template<class T>\n DUMP &operator,(const T &t) {\n if(this->tellp()) *this << \", \";\n *this << t;\n return *this;\n }\n};\n\nclass BIT {\nprivate:\n int size;\n vector<long long> bit;\n\npublic:\n BIT(int n = 0):size(n), bit(n + 1, 0) {}\n\n void add(int i, long long x) {\n while(i <= size) {\n bit[i] += x;\n i += i & -i;\n }\n }\n\n long long sum(int i) const {\n long long s = 0;\n while(i > 0) {\n s += bit[i];\n i -= i & -i;\n }\n return s;\n }\n};\n\nconst int MAX_N = 2000;\nconst int MAX_LOG_N = 12;\n\nint n;\nint parent[MAX_LOG_N][MAX_N];\nint depth[MAX_N];\n\nlong long cost[MAX_N];\nvector<int> edges[MAX_N];\n\nint pos_plus[MAX_N];\nint pos_minus[MAX_N];\n\nBIT bit(MAX_N * 2);\n\nvoid dfs(int v, int p, int d, int &index) {\n parent[0][v] = p;\n depth[v] = d;\n\n pos_plus[v] = index;\n bit.add(index++, cost[v]);\n\n for(const auto &to : edges[v]) {\n if(to != p) dfs(to, v, d + 1, index);\n }\n\n pos_minus[v] = index;\n bit.add(index++, -cost[v]);\n}\n\nvoid calc() {\n int index = 1;\n dfs(0, -1, 0, index);\n\tassert(index == 2 * n + 1);\n for(int k = 0; k < MAX_LOG_N - 1; ++k) {\n for(int v = 0; v < n; ++v) {\n parent[k + 1][v] = (parent[k][v] == -1 ? -1 : parent[k][parent[k][v]]);\n }\n }\n}\n\nint lca(int u, int v) {\n if(depth[u] > depth[v]) swap(u, v);\n for(int k = 0; k < MAX_LOG_N; ++k) {\n if((depth[v] - depth[u]) >> k & 1) v = parent[k][v];\n }\n if(u == v) return u;\n for(int k = MAX_LOG_N - 1; k >= 0; --k) {\n if(parent[k][u] != parent[k][v]) {\n u = parent[k][u];\n v = parent[k][v];\n }\n }\n return parent[0][u];\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n cin >> n;\n\n for(int i = 0; i < n; ++i) {\n cin >> cost[i];\n }\n\n for(int i = 0; i < n - 1; ++i) {\n int a, b;\n cin >> a >> b;\n --a; --b;\n edges[a].emplace_back(b);\n edges[b].emplace_back(a);\n }\n\n calc();\n\n int q;\n cin >> q;\n\twhile(q--) {\n int x;\n cin >> x;\n\n if(x) {\n int a;\n\t\t\tlong long c;\n cin >> a >> c;\n --a;\n cost[a] += c;\n bit.add(pos_plus[a], c);\n bit.add(pos_minus[a], -c);\n }\n else {\n int a, b;\n cin >> a >> b;\n --a; --b;\n if(pos_plus[a] > pos_plus[b]) swap(a, b);\n const int l = lca(a, b);\n cout << (bit.sum(pos_plus[a]) - bit.sum(pos_plus[l])) - (bit.sum(pos_minus[l]) - bit.sum(pos_minus[b] - 1)) << endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 1648, "score_of_the_acc": -0.6097, "final_rank": 6 }, { "submission_id": "aoj_1536_895094", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define dump(...) (cerr<<#__VA_ARGS__<<\" = \"<<(DUMP(),__VA_ARGS__).str()<<endl)\nstruct DUMP : stringstream {\n template<class T>\n DUMP &operator,(const T &t) {\n if(this->tellp()) *this << \", \";\n *this << t;\n return *this;\n }\n};\n\nclass BIT {\nprivate:\n int size;\n vector<long long> bit;\n\npublic:\n BIT(int n = 0):size(n), bit(n + 1, 0) {}\n\n void add(int i, long long x) {\n while(i <= size) {\n bit[i] += x;\n i += i & -i;\n }\n }\n\n long long sum(int i) const {\n long long s = 0;\n while(i > 0) {\n s += bit[i];\n i -= i & -i;\n }\n return s;\n }\n};\n\nconst int MAX_N = 2000;\nconst int MAX_LOG_N = 12;\n\nint n;\nint parent[MAX_LOG_N][MAX_N];\nint depth[MAX_N];\n\nlong long cost[MAX_N];\nvector<int> edges[MAX_N];\n\nint pos_plus[MAX_N];\nint pos_minus[MAX_N];\n\nBIT bit(MAX_N * 2);\n\nvoid dfs(int v, int p, int &index) {\n parent[0][v] = p;\n depth[v] = (p == -1 ? 0 : depth[p] + 1);\n\n pos_plus[v] = index;\n bit.add(index++, cost[v]);\n\n for(const auto &to : edges[v]) {\n if(to != p) dfs(to, v, index);\n }\n\n pos_minus[v] = index;\n bit.add(index++, -cost[v]);\n}\n\nvoid calc() {\n for(int k = 0; k < MAX_LOG_N - 1; ++k) {\n for(int v = 0; v < n; ++v) {\n parent[k + 1][v] = (parent[k][v] == -1 ? -1 : parent[k][parent[k][v]]);\n }\n }\n}\n\nint lca(int u, int v) {\n if(depth[u] > depth[v]) swap(u, v);\n for(int k = 0; k < MAX_LOG_N; ++k) {\n if((depth[v] - depth[u]) & (k << 1)) v = parent[k][v];\n }\n if(u == v) return u;\n for(int k = MAX_LOG_N - 1; k >= 0; --k) {\n if(parent[k][u] != parent[k][v]) {\n u = parent[k][u];\n v = parent[k][v];\n }\n }\n return parent[0][u];\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n cin >> n;\n\n for(int i = 0; i < n; ++i) {\n cin >> cost[i];\n }\n\n for(int i = 0; i < n - 1; ++i) {\n int a, b;\n cin >> a >> b;\n --a; --b;\n edges[a].emplace_back(b);\n edges[b].emplace_back(a);\n }\n\n memset(parent, -1, sizeof(parent));\n int index = 1;\n dfs(0, -1, index);\n calc();\n\n int q;\n cin >> q;\n for(int i = 0; i < q; ++i) {\n int x;\n cin >> x;\n\n if(x) {\n int a;\n\t\t\tlong long c;\n cin >> a >> c;\n --a;\n cost[a] += c;\n bit.add(pos_plus[a], c);\n bit.add(pos_minus[a], -c);\n }\n else {\n int a, b;\n cin >> a >> b;\n --a; --b;\n if(pos_plus[a] > pos_plus[b]) swap(a, b);\n const int l = lca(a, b);\n cout << (bit.sum(pos_plus[a]) - bit.sum(pos_plus[l] - 1)) - (bit.sum(pos_minus[l]) - bit.sum(pos_minus[b] - 1)) - cost[l] << endl;\n }\n }\n\n return 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 120, "memory_kb": 1516, "score_of_the_acc": -0.0374, "final_rank": 12 }, { "submission_id": "aoj_1536_894201", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define dump(...) (cerr<<#__VA_ARGS__<<\" = \"<<(DUMP(),__VA_ARGS__).str()<<endl)\nstruct DUMP : stringstream {\n\ttemplate<class T>\n\tDUMP &operator,(const T &t) {\n\t\tif(this->tellp()) *this << \", \";\n\t\t*this << t;\n\t\treturn *this;\n\t}\n};\n\nclass BIT {\nprivate:\n\tint size;\n\tvector<int> bit;\n\t\npublic:\n\tBIT(int n = 0):size(n), bit(n + 1, 0) {}\n\t\n\tvoid add(int i, int x) {\n\t\twhile(i <= size) {\n\t\t\tbit[i] += x;\n\t\t\ti += i & -i;\n\t\t}\n\t}\n\n\tint sum(int i) const {\n\t\tint s = 0;\n\t\twhile(i > 0) {\n\t\t\ts += bit[i];\n\t\t\ti -= i & -i;\n\t\t}\n\t\treturn s;\n\t}\n};\n\nconst int MAX_N = 2000;\nconst int MAX_LOG_N = 12;\n\nint n;\nint parent[MAX_LOG_N][MAX_N];\nint depth[MAX_N];\n\nint cost[MAX_N];\nvector<int> edges[MAX_N];\n\nint pos_plus[MAX_N];\nint pos_minus[MAX_N];\n\nBIT bit(MAX_N * 2);\n\nvoid dfs(int v, int p, int &index) {\n\tparent[0][v] = p;\n\tdepth[v] = (p == -1 ? 0 : depth[p] + 1);\n\n\tpos_plus[v] = index;\n\tbit.add(index++, cost[v]);\n\n\tfor(const auto &to : edges[v]) {\n\t\tif(to != p) dfs(to, v, index);\n\t}\n\n\tpos_minus[v] = index;\n\tbit.add(index++, -cost[v]);\n}\n\nvoid calc() {\n\tfor(int k = 0; k < MAX_LOG_N - 1; ++k) {\n\t\tfor(int v = 0; v < n; ++v) {\n\t\t\tparent[k + 1][v] = (parent[k][v] == -1 ? -1 : parent[k][parent[k][v]]);\n\t\t}\n\t}\n}\n\nint lca(int u, int v) {\n\tif(depth[u] > depth[v]) swap(u, v);\n\tfor(int k = 0; k < MAX_LOG_N; ++k) {\n\t\tif((depth[v] - depth[u]) & (k << 1)) v = parent[k][v];\n\t}\n\tif(u == v) return u;\n\tfor(int k = MAX_LOG_N - 1; k >= 0; --k) {\n\t\tif(parent[k][u] != parent[k][v]) {\n\t\t\tu = parent[k][u];\n\t\t\tv = parent[k][v];\n\t\t}\n\t}\n\treturn parent[0][u];\n}\n\nint main() {\n\tcin >> n;\n\n\tfor(int i = 0; i < n; ++i) {\n\t\tcin >> cost[i];\n\t}\n\n\tfor(int i = 0; i < n - 1; ++i) {\n\t\tint a, b;\n\t\tcin >> a >> b;\n\t\t--a; --b;\n\t\tedges[a].emplace_back(b);\n\t\tedges[b].emplace_back(a);\n\t}\n\n\tmemset(parent, -1, sizeof(parent));\n\tint index = 1;\n\tdfs(0, -1, index);\n\tcalc();\n\t\n\tint q;\n\tcin >> q;\n\tfor(int i = 0; i < q; ++i) {\n\t\tint x;\n\t\tcin >> x;\n\n\t\tif(x) {\n\t\t\tint a, c;\n\t\t\tcin >> a >> c;\n\t\t\t--a;\n\t\t\tcost[a] += c;\n\t\t\tbit.add(pos_plus[a], c);\n\t\t\tbit.add(pos_minus[a], -c);\n\t\t}\n\t\telse {\n\t\t\tint a, b;\n\t\t\tcin >> a >> b;\n\t\t\t--a; --b;\n\t\t\tif(pos_plus[a] > pos_plus[b]) swap(a, b);\n\t\t\tconst int l = lca(a, b);\n\t\t\tcout << (bit.sum(pos_plus[a]) - bit.sum(pos_plus[l] - 1)) - (bit.sum(pos_minus[l]) - bit.sum(pos_minus[b] - 1)) - cost[l] << endl;\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 170, "memory_kb": 1488, "score_of_the_acc": -0.0625, "final_rank": 14 }, { "submission_id": "aoj_1536_890409", "code_snippet": "#include <iostream>\n#include <stack>\n#include <algorithm>\n#include <cstdio>\n#include <cstring>\n#include <vector>\nusing namespace std;\n \n#define INF (1145141919)\nstruct Seg{\n long long lazy;\n long long sum;\n long long ml,mm,mr;\n long long maximum;\n Seg(){\n lazy = INF;\n sum = 0;\n ml = mm = mr = 0;\n maximum = 0;\n }\n};\ninline void output(vector<Seg> &seg,int k,int a,int b){\n cout << \"[k:\" << k << \", a:\" << a << \", b:\" << b << \"]\" << \" \";\n cout << \"sum:\" << seg[k].sum << \", ml:\" << seg[k].ml << \", mm:\" << seg[k].mm << \", mr:\" << seg[k].mr << \", maximum:\" << seg[k].maximum << endl;\n}\n \n \ninline void lazy(vector<Seg> &seg,int lazyval,int k,int a,int b){\n seg[k].lazy = lazyval;\n seg[k].sum = seg[k].lazy * (b-a+1);\n seg[k].mm = seg[k].ml = seg[k].mr = max(seg[k].sum,0ll);\n seg[k].maximum = lazyval;\n}\n \ninline void spread(vector<Seg> &seg,int k,int a,int b){\n if( seg[k].lazy == INF ) return;\n seg[k].sum = seg[k].lazy * (b-a+1);\n if( a != b ){\n int m = (a+b) / 2;\n lazy(seg,seg[k].lazy,k*2,a,m);\n lazy(seg,seg[k].lazy,k*2+1,m+1,b);\n }\n seg[k].lazy = INF;\n}\n \n \ninline void reflesh(vector<Seg> &seg,int k,int a,int b){\n if( a != b ){\n seg[k].sum = seg[k*2].sum + seg[k*2+1].sum;\n seg[k].ml = max(seg[k*2].ml,seg[k*2].sum + seg[k*2+1].ml);\n seg[k].mr = max(seg[k*2+1].mr,seg[k*2].mr + seg[k*2+1].sum);\n seg[k].mm = max( max( seg[k*2].mm , seg[k*2+1].mm ) , seg[k*2].mr + seg[k*2+1].ml );\n seg[k].maximum = max(seg[k*2].maximum,seg[k*2+1].maximum);\n }\n}\n \n \nint change(vector<Seg> &seg,int l,int r,int v,int k,int a,int b){\n spread(seg,k,a,b);\n if( r < a || b < l ){\n return 0;\n }\n \n if( l <= a && b <= r ){\n lazy(seg,v,k,a,b);\n return 0;\n }\n int m = (a+b) / 2;\n change(seg,l,r,v,k*2,a,m);\n change(seg,l,r,v,k*2+1,m+1,b);\n reflesh(seg,k,a,b);\n}\n \n \nstruct Three{\n long long ml,mm,mr,sum,maximum;\n Three() : ml(0) , mm(0) , mr(0) , sum(0) {}\n Three(long long ml,long long mm,long long mr,long long sum,long long maximum) : ml(ml) , mm(mm) , mr(mr) , sum(sum) , maximum(maximum) {}\n};\n \ninline void output(Three x){\n //cout << \"[k:\" << k << \", a:\" << a << \", b:\" << b << \"]\" << \" \";\n cout << \"sum:\" << x.sum << \", ml:\" << x.ml << \", mm:\" << x.mm << \", mr:\" << x.mr << \", maximum:\" << x.maximum << endl;\n}\n \n \nThree getMaximum(vector<Seg> &seg,int l,int r,int k,int a,int b){\n spread(seg,k,a,b);\n if( r < a || b < l ){\n return Three(0,0,0,0,-INF);\n }\n if( l <= a && b <= r ){\n //output(seg,k,a,b);\n return Three(seg[k].ml,seg[k].mm,seg[k].mr,seg[k].sum,seg[k].maximum);\n }\n int m = (a+b) / 2;\n Three le = getMaximum(seg,l,r,k*2,a,m);\n Three ri = getMaximum(seg,l,r,k*2+1,m+1,b);\n Three ans = Three( max(ri.ml+le.sum,le.ml) , max( max(le.mm,ri.mm) , le.mr+ri.ml ) , max(le.mr+ri.sum,ri.mr) , ri.sum + le.sum , max(le.maximum,ri.maximum) ); \n \n //cout << \"(\" << a << \",\" << b << \")\" << \"[\" << ans.ml << \" \" << ans.mm << \" \" << ans.mr << \" \" << ans.sum << \"]\" << endl;\n reflesh(seg,k,a,b);\n return ans;\n}\n \ntypedef unsigned int uint;\nint in() {\n int x = 0, c;\n for (; (uint)((c = getchar()) - '0') >= 10; ) { if (c == '-') return -in(); if (!~c) throw ~0; }\n do { x = (x << 3) + (x << 1) + (c - '0'); } while ((uint)((c = getchar()) - '0') < 10);\n return x;\n}\n \nint parent[19][200010];\nint depth[200010];\nvector<int> gr[200010];\nint dfs(int x_,int p_){\n stack< pair<int,int> > S;\n S.push(make_pair(x_,p_));\n while(S.size()){\n int x = S.top().first;\n int p = S.top().second;\n S.pop();\n parent[0][x] = p;\n if( p != -1 ) depth[x] = depth[p] + 1;\n for(int i = 0 ; i < gr[x].size() ; i++)\n if( gr[x][i] != p ) S.push(make_pair(gr[x][i],x));\n }\n}\n \nvoid genlca(){\n for(int i = 1 ; i < 19 ; i++){\n for(int j = 0 ; j < 200010 ; j++){\n if( parent[i-1][j] != -1 )\n parent[i][j] = parent[i-1][parent[i-1][j]];\n }\n }\n}\npair< int,pair<int,int> > lca(int x,int y){\n //if( depth[x] > depth[y] ) swap(x,y);\n int d = depth[y] - depth[x];\n for(int i = 0 ; i < 19 ; i++){\n if( d >> i & 1 ) y = parent[i][y];\n }\n if( x == y ) return make_pair(x,make_pair(-1,-1));\n \n //if( depth[x] != depth[y] ) cout << \"^^\" << endl;\n for(int i = 17 ; i >= 0 ; i--){\n if( parent[i][x] != parent[i][y] ){\n x = parent[i][x];\n y = parent[i][y];\n }\n }\n return make_pair( parent[0][x] , make_pair(x,y) );\n}\n \nint gNum = 0;\nint belong[200010];\nint height[200010];\nint lightEdge[200010];\nint heavyHeight[200010];\nint size[200010];\nvector<Seg> seg[200010];\n \nint initval[200010];\n \nint build(int x_,int where){\n stack< pair<int,int> > S;\n S.push( make_pair(x_,-1) );\n vector<int> tmp;\n while(S.size()){\n int x = S.top().first;\n int prev = S.top().second;\n S.pop();\n if( x == -1 ) continue;\n if( prev != -1 && size[prev] * 2 < size[x] ){\n lightEdge[where] = x;\n continue;\n }\n if( belong[x] != -1 ){\n lightEdge[where] = x;\n continue;\n }\n tmp.push_back(x);\n belong[x] = where;\n S.push(make_pair(parent[0][x],x));\n }\n for(int i = 0 ; i < tmp.size() ; i++)\n height[tmp[i]] = tmp.size() - i;\n return tmp.size() + 1;\n}\n \nvoid query1(int st,int en,int val){\n if( belong[st] != belong[en] ){\n int s = belong[st];\n int a = height[st];\n change(seg[s],1,a,val,1,1,seg[s].size()/2); \n query1(lightEdge[s],en,val);\n }else{\n int s = belong[st];\n int a = height[st];\n int b = height[en];\n change(seg[s],b,a,val,1,1,seg[s].size()/2);\n }\n}\n \nThree query2(int st,int en,Three ri){\n if( belong[st] != belong[en] ){\n int s = belong[st];\n int a = height[st];\n Three le = getMaximum(seg[s],1,a,1,1,seg[s].size()/2);\n Three ans = Three( max(ri.ml+le.sum,le.ml) , max( max(le.mm,ri.mm) , le.mr+ri.ml ) , max(le.mr+ri.sum,ri.mr) , ri.sum + le.sum , max(le.maximum,ri.maximum) ); \n return query2(lightEdge[s],en,ans);\n \n }else{\n int s = belong[st];\n int a = height[st];\n int b = height[en];\n Three le = getMaximum(seg[s],b,a,1,1,seg[s].size()/2);\n Three ans = Three( max(ri.ml+le.sum,le.ml) , max( max(le.mm,ri.mm) , le.mr+ri.ml ) , max(le.mr+ri.sum,ri.mr) , ri.sum + le.sum , max(le.maximum,ri.maximum) ); \n return ans;\n }\n}\n \nint main(){\n \n memset(parent,-1,sizeof(parent));\n memset(belong,-1,sizeof(belong));\n memset(lightEdge,-1,sizeof(lightEdge));\n int n = in();\n for(int i = 0 ; i < n ; i++) initval[i] = in();\n for(int i = 0 ; i < n-1 ; i++){\n int a = in();\n int b = in();\n a--,b--;\n gr[a].push_back(b);\n gr[b].push_back(a);\n }\n dfs(0,-1);\n genlca();\n vector< pair<int,int> > sorted;\n for(int i = 0 ; i < n ; i++){\n sorted.push_back(make_pair(-depth[i],i));\n }\n sort(sorted.begin(),sorted.end());\n for(int j = 0 ; j < n ; j++){\n int i = sorted[j].second;\n size[i]++;\n if( parent[0][i] != -1 ) size[parent[0][i]]+=size[i];\n }\n for(int j = 0 ; j < n ; j++){\n int i = sorted[j].second;\n if( belong[i] == -1 ){\n int sz = build(i,gNum) - 1;\n int sz_ = 1;\n while( sz_ < sz ) sz_ *= 2;\n seg[gNum].resize(2*sz_);\n gNum++;\n }\n }\n for(int i = 0 ; i < n ; i++){\n change(seg[belong[i]],height[i],height[i],initval[i],1,1,seg[belong[i]].size()/2);\n }\n int m = in();\n for(int i = 0 ; i < m ; i++){\n int t,a,b,c;\n t = in(); a = in(); b = a; c = in();\n if( t == 1 ){\n \ta--,b--;\n if( depth[a] > depth[b] ) swap(a,b);\n pair<int,pair<int,int> > tmp = lca(a,b);\n int p = tmp.first;\n int pa = tmp.first;\n int pb = tmp.second.second;\n \tinitval[a] += c;\n \tquery1(a,b,initval[a]);\n }else{\n \ta--;\n \tb = c-1;\n if( depth[a] > depth[b] ) swap(a,b);\n pair<int,pair<int,int> > tmp = lca(a,b);\n int p = tmp.first;\n int pa = tmp.first;\n int pb = tmp.second.second;\n Three res,ri,le;\n if( p == a ){\n res = query2(b,p,Three(0,0,0,0,-INF));\n }else{\n le = query2(a,p,Three(0,0,0,0,-INF));\n ri = query2(b,pb,Three(0,0,0,0,-INF));\n res = Three( -1 , max( max(le.mm,ri.mm) , le.ml+ri.ml ) ,-1 , ri.sum+le.sum , max(ri.maximum,le.maximum) );\n }\n printf(\"%lld\\n\",res.sum);\n }\n }\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 30408, "score_of_the_acc": -1.2448, "final_rank": 8 }, { "submission_id": "aoj_1536_890365", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nconst int N = 2000;\nconst int M = 8192;\n\nint n, a[N], b[N], cost[N];\nvector<vi> e;\n\nint parent[11][N];\nint depth[N];\n\nint euler[2*N], sz;\nint pos[N][2];\n\n\nint seg_sz = 1;\nll dat[2*M];\ninline ll query(int a, int b, int k, int l, int r){\n\t\n\tif(b <= l || a >= r) return 0;\n\tif(a <= l && r <= b) return dat[k];\n\t\n\tll lch = query(a, b, k * 2 + 1, l, (l + r) / 2);\n\tll rch = query(a, b, k * 2 + 2, (l + r) / 2, r);\n\t\n\treturn lch + rch;\n}\ninline void update(int pos, int d){\n\t\n\tpos += seg_sz - 1;\n\tdat[pos] += d;\n\t\n\twhile(1){\n\t\tif(pos == 0) break;\n\t\tpos = (pos - 1) / 2;\n\t\tdat[pos] = dat[pos * 2 + 1] + dat[pos * 2 + 2];\n\t}\n}\n\nvoid dfs(int c, int p, int d = 1){\n\t\n\tdepth[c] = d;\n\tparent[0][c] = p;\n\tdat[seg_sz - 1 + sz] = cost[c];\n\teuler[sz] = c;\n\tpos[c][0] = sz++;\n\t\n\trep(i, e[c].size()){\n\t\tint id = e[c][i];\n\t\tint to = id < 0 ? a[-id - 1] : b[id - 1];\n\t\t\n\t\tif(to != p) dfs(to, c, d + 1);\n\t}\n\t\n\tdat[seg_sz - 1 + sz] = -cost[c];\n\teuler[sz] = c;\n\tpos[c][1] = sz++;\n}\n\ninline int lca(int a, int b){\n\tif(depth[a] < depth[b]) swap(a, b);\n\t\n\tfor(int i = 10; i >= 0; i--) if(depth[a] - depth[b] >> i & 1) a = parent[i][a];\n\t\n\tif(a == b) return a;\n\t\n\tfor(int i = 10; i >= 0; i--) if(parent[i][a] != parent[i][b]){\n\t\t\n\t\ta = parent[i][a];\n\t\tb = parent[i][b];\n\t}\n\treturn parent[0][a];\n}\n\nint main(){\n\t\n\tscanf(\"%d\", &n);\n\te.resize(n);\n\t\n\trep(i, n) scanf(\"%d\", cost + i);\n\trep(i, n - 1){\n\t\tscanf(\"%d%d\", a + i, b + i);\n\t\ta[i]--; b[i]--;\n\t\t\n\t\te[a[i]].pb(i + 1);\n\t\te[b[i]].pb(-i - 1);\n\t}\n\t\n\tfor(; seg_sz < 2 * n; seg_sz *= 2);\n\t\n\tdfs(0, 0);\n\t\n\trep(i, 10) rep(j, n) parent[i + 1][j] = parent[i][parent[i][j]];\n\t\n\tfor(int i = seg_sz - 2; i >= 0; i--){\n\t\t\n\t\tdat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];\n\t}\n\t\n\t#if 0\n\trep(i, seg_sz) cerr<<dat[seg_sz - 1 + i]<<\" \";cerr<<endl;\n\trep(i, n) cerr<<pos[i][0]<<\" \"<<pos[i][1]<<endl;\n\tcerr<<\"---\"<<endl;\n\t#endif\n\t\n\tint qs; scanf(\"%d\", &qs);\n\t\n\twhile(qs--){\n\t\tint x, y, z;\n\t\tscanf(\"%d%d%d\", &x, &y, &z);\n\t\t\n\t\tif(x == 0){\n\t\t\ty--; z--;\n\t\t\t\n\t\t\tint p = lca(y, z);\n\t\t\tll ans = 0;\n\t\t\t\n\t\t\tans += query(0, pos[y][0] + 1, 0, 0, seg_sz);\n\t\t\tans += query(0, pos[z][0] + 1, 0, 0, seg_sz);\n\t\t\tans -= query(0, pos[p][0] + 1, 0, 0, seg_sz) * 2;\n\t\t\tans += dat[seg_sz - 1 + pos[p][0]];\n\t\t\t\n\t\t\tprintf(\"%lld\\n\", ans);\n\t\t}\n\t\telse{\n\t\t\ty--;\n\t\t\tupdate(pos[y][0], z);\n\t\t\tupdate(pos[y][1], -z);\n\t\t}\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 570, "memory_kb": 1660, "score_of_the_acc": -0.2768, "final_rank": 4 }, { "submission_id": "aoj_1536_890354", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nconst int N = 2000;\nconst int M = 8192;\n\nint n, a[N], b[N], cost[N];\nvector<vi> e;\n\nint parent[11][N];\nint depth[N];\n\nint euler[2*N], sz;\nint pos[N][2];\n\n\nint seg_sz = 1;\nll dat[2*M];\ninline ll query(int a, int b, int k, int l, int r){\n\t\n\tif(b <= l || a >= r) return 0;\n\tif(a <= l && r <= b) return dat[k];\n\t\n\tll lch = query(a, b, k * 2 + 1, l, (l + r) / 2);\n\tll rch = query(a, b, k * 2 + 2, (l + r) / 2, r);\n\t\n\treturn lch + rch;\n}\ninline void update(int pos, int d){\n\t\n\tpos += seg_sz - 1;\n\tdat[pos] += d;\n\t\n\twhile(1){\n\t\tif(pos == 0) break;\n\t\tpos = (pos - 1) / 2;\n\t\tdat[pos] = dat[pos * 2 + 1] + dat[pos * 2 + 2];\n\t}\n}\n\nvoid dfs(int c, int p, int d = 1){\n\t\n\tdepth[c] = d;\n\tparent[0][c] = p;\n\tdat[seg_sz - 1 + sz] = cost[c];\n\teuler[sz] = c;\n\tpos[c][0] = sz++;\n\t\n\trep(i, e[c].size()){\n\t\tint id = e[c][i];\n\t\tint to = id < 0 ? a[-id - 1] : b[id - 1];\n\t\t\n\t\tif(to != p) dfs(to, c, d + 1);\n\t}\n\t\n\tdat[seg_sz - 1 + sz] = -cost[c];\n\teuler[sz] = c;\n\tpos[c][1] = sz++;\n}\n\ninline int lca(int a, int b){\n\tif(depth[a] < depth[b]) swap(a, b);\n\t\n\tfor(int i = 10; i >= 0; i--) if(depth[a] - depth[b] >> i & 1) a = parent[i][a];\n\t\n\tif(a == b) return a;\n\t\n\tfor(int i = 10; i >= 0; i--) if(parent[i][a] != parent[i][b]){\n\t\t\n\t\ta = parent[i][a];\n\t\tb = parent[i][b];\n\t}\n\treturn parent[0][a];\n}\n\nint main(){\n\t\n\tscanf(\"%d\", &n);\n\te.resize(n);\n\t\n\trep(i, n) scanf(\"%d\", cost + i);\n\trep(i, n - 1){\n\t\tscanf(\"%d%d\", a + i, b + i);\n\t\ta[i]--; b[i]--;\n\t\t\n\t\te[a[i]].pb(i + 1);\n\t\te[b[i]].pb(-i - 1);\n\t}\n\t\n\tfor(; seg_sz < 2 * n; seg_sz *= 2);\n\t\n\tdfs(0, 0);\n\t\n\trep(i, 10) rep(j, n) parent[i + 1][j] = parent[i][parent[i][j]];\n\t\n\tfor(int i = seg_sz - 2; i >= 0; i--){\n\t\t\n\t\tdat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];\n\t}\n\t\n\t#if 0\n\trep(i, seg_sz) cerr<<dat[seg_sz - 1 + i]<<\" \";cerr<<endl;\n\trep(i, n) cerr<<pos[i][0]<<\" \"<<pos[i][1]<<endl;\n\tcerr<<\"---\"<<endl;\n\t#endif\n\t\n\tint qs; scanf(\"%d\", &qs);\n\t\n\twhile(qs--){\n\t\tint x, y, z;\n\t\tscanf(\"%d%d%d\", &x, &y, &z);\n\t\t\n\t\tif(x == 0){\n\t\t\ty--; z--;\n\t\t\t\n\t\t\tint p = lca(y, z);\n\t\t\tll ans = 0;\n\t\t\t\n\t\t\tans += query(0, pos[y][0] + 1, 0, 0, seg_sz);\n\t\t\tans += query(0, pos[z][0] + 1, 0, 0, seg_sz);\n\t\t\tans -= 2 * query(0, pos[p][0] + 1, 0, 0, seg_sz);\n\t\t\tans += dat[seg_sz - 1 + p];\n\t\t\t\n\t\t\tprintf(\"%lld\\n\", ans);\n\t\t}\n\t\telse{\n\t\t\ty--;\n\t\t\tupdate(pos[y][0], z);\n\t\t\tupdate(pos[y][1], -z);\n\t\t}\n\t}\n\t\n\treturn 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 50, "memory_kb": 1504, "score_of_the_acc": -0.0006, "final_rank": 10 }, { "submission_id": "aoj_1536_890344", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nconst int N = 2000;\nconst int M = 8192;\n\nint n, a[N], b[N], cost[N];\nvector<vi> e;\n\nint parent[11][N];\nint depth[N];\n\nint euler[2*N], sz;\nint pos[N][2];\n\n\nint seg_sz = 1;\nll dat[2*M];\ninline ll query(int a, int b, int k, int l, int r){\n\t\n\tif(b <= l || a >= r) return 0;\n\tif(a <= l && r <= b) return dat[k];\n\t\n\tll lch = query(a, b, k * 2 + 1, l, (l + r) / 2);\n\tll rch = query(a, b, k * 2 + 2, (l + r) / 2, r);\n\t\n\treturn lch + rch;\n}\ninline void update(int pos, int d){\n\t\n\tpos += seg_sz - 1;\n\tdat[pos] += d;\n\t\n\twhile(1){\n\t\tif(pos == 0) break;\n\t\tpos = (pos - 1) / 2;\n\t\tdat[pos] = dat[pos * 2 + 1] + dat[pos * 2 + 2];\n\t}\n}\n\nvoid dfs(int c, int p, int d = 1){\n\t\n\tdepth[c] = d;\n\tparent[0][c] = p;\n\tdat[seg_sz - 1 + sz] = cost[c];\n\teuler[sz] = c;\n\tpos[c][0] = sz++;\n\t\n\trep(i, e[c].size()){\n\t\tint id = e[c][i];\n\t\tint to = id < 0 ? a[-id - 1] : b[id - 1];\n\t\t\n\t\tif(to != p) dfs(to, c, d + 1);\n\t}\n\t\n\tdat[seg_sz - 1 + sz] = -cost[c];\n\teuler[sz] = c;\n\tpos[c][1] = sz++;\n}\n\ninline int lca(int a, int b){\n\tif(depth[a] < depth[b]) swap(a, b);\n\t\n\tfor(int i = 10; i >= 0; i--) if(depth[a] - depth[b] >> i & 1) a = parent[i][a];\n\t\n\tif(a == b) return a;\n\t\n\tfor(int i = 10; i >= 0; i--) if(parent[i][a] != parent[i][b]){\n\t\t\n\t\ta = parent[i][a];\n\t\tb = parent[i][b];\n\t}\n\treturn parent[0][a];\n}\n\nint main(){\n\t\n\tscanf(\"%d\", &n);\n\te.resize(n);\n\t\n\trep(i, n) scanf(\"%d\", cost + i);\n\trep(i, n - 1){\n\t\tscanf(\"%d%d\", a + i, b + i);\n\t\ta[i]--; b[i]--;\n\t\t\n\t\te[a[i]].pb(i + 1);\n\t\te[b[i]].pb(-i - 1);\n\t}\n\t\n\tfor(; seg_sz < 2 * n; seg_sz *= 2);\n\t\n\tdfs(0, 0);\n\t\n\trep(i, 10) rep(j, n) parent[i + 1][j] = parent[i][parent[i][j]];\n\t\n\tfor(int i = seg_sz - 2; i >= 0; i--){\n\t\t\n\t\tdat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];\n\t}\n\t\n\t#if 0\n\trep(i, seg_sz) cerr<<dat[seg_sz - 1 + i]<<\" \";cerr<<endl;\n\trep(i, n) cerr<<pos[i][0]<<\" \"<<pos[i][1]<<endl;\n\tcerr<<\"---\"<<endl;\n\t#endif\n\t\n\tint qs; scanf(\"%d\", &qs);\n\t\n\twhile(qs--){\n\t\tint x, y, z;\n\t\tscanf(\"%d%d%d\", &x, &y, &z);\n\t\t\n\t\tif(x == 0){\n\t\t\ty--; z--;\n\t\t\t\n\t\t\tint p = lca(y, z);\n\t\t\tll ans = 0;\n\t\t\t\n\t\t\tans += query(0, pos[y][0] + 1, 0, 0, seg_sz);\n\t\t\tans += query(0, pos[z][0] + 1, 0, 0, seg_sz);\n\t\t\tans -= query(0, pos[p][0] + 1, 0, 0, seg_sz);\n\t\t\t\n\t\t\tprintf(\"%lld\\n\", ans);\n\t\t}\n\t\telse{\n\t\t\ty--;\n\t\t\tupdate(pos[y][0], z);\n\t\t\tupdate(pos[y][1], -z);\n\t\t}\n\t}\n\t\n\treturn 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 50, "memory_kb": 1504, "score_of_the_acc": -0.0006, "final_rank": 10 }, { "submission_id": "aoj_1536_890342", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nconst int N = 2000;\nconst int M = 8192;\n\nint n, a[N], b[N], cost[N];\nvector<vi> e;\n\nint parent[11][N];\nint depth[N];\n\nint euler[2*N], sz;\nint pos[N][2];\n\n\nint seg_sz = 1;\nint dat[2*M];\ninline int query(int a, int b, int k, int l, int r){\n\t\n\tif(b <= l || a >= r) return 0;\n\tif(a <= l && r <= b) return dat[k];\n\t\n\tint lch = query(a, b, k * 2 + 1, l, (l + r) / 2);\n\tint rch = query(a, b, k * 2 + 2, (l + r) / 2, r);\n\t\n\treturn lch + rch;\n}\ninline void update(int pos, int d){\n\t\n\tpos += seg_sz - 1;\n\tdat[pos] += d;\n\t\n\twhile(1){\n\t\tif(pos == 0) break;\n\t\tpos = (pos - 1) / 2;\n\t\tdat[pos] = dat[pos * 2 + 1] + dat[pos * 2 + 2];\n\t}\n}\n\nvoid dfs(int c, int p, int d = 1){\n\t\n\tdepth[c] = d;\n\tparent[0][c] = p;\n\tdat[seg_sz - 1 + sz] = cost[c];\n\teuler[sz] = c;\n\tpos[c][0] = sz++;\n\t\n\trep(i, e[c].size()){\n\t\tint id = e[c][i];\n\t\tint to = id < 0 ? a[-id - 1] : b[id - 1];\n\t\t\n\t\tif(to != p) dfs(to, c, d + 1);\n\t}\n\t\n\tdat[seg_sz - 1 + sz] = -cost[c];\n\teuler[sz] = c;\n\tpos[c][1] = sz++;\n}\n\ninline int lca(int a, int b){\n\tif(depth[a] < depth[b]) swap(a, b);\n\t\n\tfor(int i = 10; i >= 0; i--) if(depth[a] - depth[b] >> i & 1) a = parent[i][a];\n\t\n\tif(a == b) return a;\n\t\n\tfor(int i = 10; i >= 0; i--) if(parent[i][a] != parent[i][b]){\n\t\t\n\t\ta = parent[i][a];\n\t\tb = parent[i][b];\n\t}\n\treturn parent[0][a];\n}\n\nint main(){\n\t\n\tscanf(\"%d\", &n);\n\te.resize(n);\n\t\n\trep(i, n) scanf(\"%d\", cost + i);\n\trep(i, n - 1){\n\t\tscanf(\"%d%d\", a + i, b + i);\n\t\ta[i]--; b[i]--;\n\t\t\n\t\te[a[i]].pb(i + 1);\n\t\te[b[i]].pb(-i - 1);\n\t}\n\t\n\tfor(; seg_sz < 2 * n; seg_sz *= 2);\n\t\n\tdfs(0, 0);\n\t\n\trep(i, 10) rep(j, n) parent[i + 1][j] = parent[i][parent[i][j]];\n\t\n\tfor(int i = seg_sz - 2; i >= 0; i--){\n\t\t\n\t\tdat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];\n\t}\n\t\n\t#if 0\n\trep(i, seg_sz) cerr<<dat[seg_sz - 1 + i]<<\" \";cerr<<endl;\n\trep(i, n) cerr<<pos[i][0]<<\" \"<<pos[i][1]<<endl;\n\tcerr<<\"---\"<<endl;\n\t#endif\n\t\n\tint qs; scanf(\"%d\", &qs);\n\t\n\twhile(qs--){\n\t\tint x, y, z;\n\t\tscanf(\"%d%d%d\", &x, &y, &z);\n\t\t\n\t\tif(x == 0){\n\t\t\ty--; z--;\n\t\t\t\n\t\t\tint p = lca(y, z);\n\t\t\tll ans = 0;\n\t\t\t\n\t\t\tans += query(0, pos[y][0] + 1, 0, 0, seg_sz);\n\t\t\tans += query(0, pos[z][0] + 1, 0, 0, seg_sz);\n\t\t\tans -= query(0, pos[p][0] + 1, 0, 0, seg_sz);\n\t\t\t\n\t\t\tprintf(\"%lld\\n\", ans);\n\t\t}\n\t\telse{\n\t\t\ty--;\n\t\t\tupdate(pos[y][0], z);\n\t\t\tupdate(pos[y][1], -z);\n\t\t}\n\t}\n\t\n\treturn 0;\n}", "accuracy": 0.12903225806451613, "time_ms": 50, "memory_kb": 1488, "score_of_the_acc": 0, "final_rank": 9 }, { "submission_id": "aoj_1536_890294", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <set>\n#include <map>\n#include <queue>\n#include <iostream>\n#include <sstream>\n#include <cstdio>\n#include <cmath>\n#include <ctime>\n#include <cstring>\n#include <cctype>\n#include <cassert>\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\n#if 1 || defined(_MSC_VER) || __cplusplus > 199711L\n#define aut(r,v) auto r = (v)\n#else\n#define aut(r,v) typeof(v) r = (v)\n#endif\n#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)\n#define all(o) (o).begin(), (o).end()\n#define pb(x) push_back(x)\n#define mp(x,y) make_pair((x),(y))\n#define mset(m,v) memset(m,v,sizeof(m))\n#define INF 0x3f3f3f3f\n#define INFL 0x3f3f3f3f3f3f3f3fLL\nusing namespace std;\ntypedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;\ntypedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;\ntypedef vector<string> vs; typedef long double ld;\ntemplate<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }\ntemplate<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }\n\nstruct CentroidPathDecomposition {\n\tvector<int> colors, positions;\t//Vertex -> Color, Vertex -> Offset\n\tvector<int> lengths, parents, branches;\t//Color -> Int, Color -> Color, Color -> Offset\n\tvector<int> parentnodes, depths;\t//Vertex -> Vertex, Vertex -> Int\n\t//vector<FenwickTree>とかを避けて1次元にしたい時に使う\n\tvector<int> sortednodes, offsets;\t//Index -> Vertex, Color -> Index\n\n\t//両方の辺があってもいいし、親から子への辺だけでもよい\n\tvoid build(const vector<vi> &g, int root) {\n\t\tint n = g.size();\n\n\t\tcolors.assign(n, -1); positions.assign(n, -1);\n\t\tlengths.clear(); parents.clear(); branches.clear();\n\t\tparentnodes.assign(n, -1); depths.assign(n, -1);\n\n\t\tvector<int> subtreesizes;\n\t\tmeasure(g, root, subtreesizes);\n\n\t\tstruct State {\n\t\t\tint i, len, parent;\n\t\t\tState() { }\n\t\t\tState(int i_, int l, int p): i(i_), len(l), parent(p) { }\n\t\t};\n\t\tdepths[root] = 0;\n\t\tvector<State> s;\n\t\ts.push_back(State(root, 0, -1));\n\t\twhile(!s.empty()) {\n\t\t\tState t = s.back(); s.pop_back();\n\t\t\tint i = t.i, len = t.len;\n\t\t\tint color = lengths.size();\n\t\t\tif(t.parent != -2) {\n\t\t\t\tassert(parents.size() == color);\n\t\t\t\tparents.push_back(t.parent);\n\t\t\t\tbranches.push_back(len);\n\t\t\t\tlen = 0;\n\t\t\t}\n\t\t\tcolors[i] = color;\n\t\t\tpositions[i] = len;\n\n\t\t\tint maxsize = -1, maxj = -1;\n\t\t\teach(j, g[i]) if(colors[*j] == -1) {\n\t\t\t\tif(maxsize < subtreesizes[*j]) {\n\t\t\t\t\tmaxsize = subtreesizes[*j];\n\t\t\t\t\tmaxj = *j;\n\t\t\t\t}\n\t\t\t\tparentnodes[*j] = i;\n\t\t\t\tdepths[*j] = depths[i] + 1;\n\t\t\t}\n\t\t\tif(maxj == -1) {\n\t\t\t\tlengths.push_back(len + 1);\n\t\t\t}else {\n\t\t\t\teach(j, g[i]) if(colors[*j] == -1 && *j != maxj)\n\t\t\t\t\ts.push_back(State(*j, len, color));\n\t\t\t\ts.push_back(State(maxj, len + 1, -2));\n\t\t\t}\n\t\t}\n\n\t\tsortNodes();\n\t}\n\n\tvoid sortNodes() {\n\t\tint n = colors.size(), m = lengths.size();\n\t\tsortednodes.resize(n, -1);\n\t\toffsets.resize(m + 1);\n\t\trep(i, m) offsets[i+1] = offsets[i] + lengths[i];\n\t\trep(i, n) sortednodes[offsets[colors[i]] + positions[i]] = i;\n\t}\n\n\tvoid get(int v, int &c, int &p) const {\n\t\tc = colors[v]; p = positions[v];\n\t}\n\tbool go_up(int &c, int &p) const {\n\t\tp = branches[c]; c = parents[c];\n\t\treturn c != -1;\n\t}\n\n\tinline const int *nodesBegin(int c) const { return &sortednodes[0] + offsets[c]; }\n\tinline const int *nodesEnd(int c) const { return &sortednodes[0] + offsets[c+1]; }\n\nprivate:\n\tvoid measure(const vector<vi> &g, int root, vector<int> &out_subtreesizes) const {\n\t\tout_subtreesizes.assign(g.size(), -1);\n\t\tvector<int> s;\n\t\ts.push_back(root);\n\t\twhile(!s.empty()) {\n\t\t\tint i = s.back(); s.pop_back();\n\t\t\tif(out_subtreesizes[i] == -2) {\n\t\t\t\tint s = 1;\n\t\t\t\teach(j, g[i]) if(out_subtreesizes[*j] != -2)\n\t\t\t\t\ts += out_subtreesizes[*j];\n\t\t\t\tout_subtreesizes[i] = s;\n\t\t\t}else {\n\t\t\t\ts.push_back(i);\n\t\t\t\teach(j, g[i]) if(out_subtreesizes[*j] == -1)\n\t\t\t\t\ts.push_back(*j);\n\t\t\t\tout_subtreesizes[i] = -2;\n\t\t\t}\n\t\t}\n\t}\n};\n\n\ntypedef int Vertex;\nstruct Graph {\n\ttypedef std::pair<Vertex, Vertex> Edge;\n\tstruct To {\n\t\tVertex to;\n\t};\n\n\tint n, m;\n\n\tGraph(int n_, const std::vector<Edge> &edges):\n\t\tn(n_), m(edges.size()), tos(m+1), offsets(n+1, 0) {\n\t\tfor(int e = 0; e < m; e ++) offsets[edges[e].first] ++;\n\t\tfor(int v = 1; v <= n; v ++) offsets[v] += offsets[v-1];\n\t\tfor(int e = 0; e < m; e ++)\n\t\t\ttos[-- offsets[edges[e].first]].to = edges[e].second;\n\t}\n\n\tinline const To *edgesBegin(int v) const { return &tos[offsets[v]]; }\n\tinline const To *edgesEnd(int v) const { return &tos[offsets[v+1]]; }\n\n\tinline const int outDegree(int v) const { return offsets[v+1] - offsets[v]; }\n\nprivate:\n\tstd::vector<To> tos;\n\tstd::vector<int> offsets;\n};\n\t\nclass SchieberVishkinLCA {\npublic:\n\ttypedef unsigned Word;\nprivate:\n\n\tstatic inline Word lowestOneBit(Word v) {\n\t\treturn v & (~v+1);\n\t}\n\tstatic inline Word highestOneBitMask(Word v) {\n\t\tv |= v >> 1;\n\t\tv |= v >> 2;\n\t\tv |= v >> 4;\n\t\tv |= v >> 8;\n\t\tv |= v >> 16;\n\t\treturn v >> 1;\n\t}\n\n\tstd::vector<Word> indices;\t\t\t//Vertex -> index\n\tstd::vector<Word> maxHIndices;\t\t//Vertex -> index\n\tstd::vector<Word> ancestorHeights;\t//Vertex -> Word\n\tstd::vector<Vertex> pathParents;\t//index-1 -> Vertex\npublic:\n\t//gは親→子の枝のある根付き木\n\tvoid build(const Graph &g, Vertex root) {\n\t\tassert(g.m == g.n - 1);\n\n\t\tancestorHeights.resize(g.n);\n\t\tstd::vector<Vertex> parents(g.n);\n\t\tmaxHIndices.resize(g.n);\n\t\tstd::vector<Vertex> vertices; vertices.reserve(g.n);\n\t\tindices.resize(g.n);\n\n\t\t//euler tour\n\t\tWord currentIndex = 1;\n\t\tparents[root] = root;\t//利便さのために\n\t\tvertices.push_back(root);\n\t\twhile(!vertices.empty()) {\n\t\t\tVertex v = vertices.back(); vertices.pop_back();\n\t\t\tindices[v] = currentIndex ++;\n\t\t\tfor(const Graph::To *it = g.edgesBegin(v); it != g.edgesEnd(v); ++ it) {\n\t\t\t\tparents[it->to] = v;\n\t\t\t\tvertices.push_back(it->to);\n\t\t\t}\n\t\t}\n\n\t\t//BFS (トポロジカル順序を求めるために)\n\t\tint head = 0, tail = 1;\n\t\tvertices.resize(g.n); vertices[0] = root;\n\t\twhile(head != tail) {\n\t\t\tVertex v = vertices[head ++];\n\t\t\tfor(const Graph::To *it = g.edgesBegin(v); it != g.edgesEnd(v); ++ it)\n\t\t\t\tvertices[tail ++] = it->to;\n\t\t}\n\n\t\t//深い方から\n\t\tfor(std::vector<Vertex>::const_iterator it = vertices.begin(); it != vertices.end(); ++ it)\n\t\t\tmaxHIndices[*it] = indices[*it];\n\t\tfor(std::vector<Vertex>::const_reverse_iterator it = vertices.rbegin(); it != vertices.rend(); ++ it) {\n\t\t\tVertex v = *it, parent = parents[v];\n\t\t\tif(lowestOneBit(maxHIndices[parent]) < lowestOneBit(maxHIndices[v]))\n\t\t\t\tmaxHIndices[parent] = maxHIndices[v];\n\t\t}\n\n\t\t//Aを求める\n\t\tancestorHeights[root] = 0;\n\t\tfor(std::vector<Vertex>::const_iterator it = vertices.begin(); it != vertices.end(); ++ it) {\n\t\t\tVertex v = *it;\n\t\t\tancestorHeights[v] = ancestorHeights[parents[v]] | lowestOneBit(maxHIndices[v]);\n\t\t}\n\n\t\tpathParents.swap(parents);\t//メモリをけちる\n\t\tpathParents[indices[root]-1] = root;\n\t\tfor(std::vector<Vertex>::const_iterator it = vertices.begin(); it != vertices.end(); ++ it) {\n\t\t\tVertex v = *it;\n\t\t\tfor(const Graph::To *jt = g.edgesBegin(v); jt != g.edgesEnd(v); ++ jt) {\n\t\t\t\tif(maxHIndices[v] != maxHIndices[jt->to])\n\t\t\t\t\tpathParents[indices[jt->to]-1] = v;\n\t\t\t\telse\n\t\t\t\t\tpathParents[indices[jt->to]-1] = pathParents[indices[v]-1];\n\t\t\t}\n\t\t}\n\t}\n\n\tVertex query(Vertex v, Vertex u) const {\n\t\t//binary tree上でのLCAの高さを求める\n\t\tWord Iv = maxHIndices[v], Iu = maxHIndices[u];\n\t\tWord hIv = lowestOneBit(Iv), hIu = lowestOneBit(Iu);\n\t\tWord hbMask = highestOneBitMask((Iv ^ Iu) | hIv | hIu);\n\t\tWord j = lowestOneBit(ancestorHeights[v] & ancestorHeights[u] & ~hbMask);\n\t\t//j = hI(lca(v,u)) となる (ここで、hI(x) = 2^(complete binary tree上でのI(x)の高さ), I(x) = maxHIndices[x])\n\t\t//(hI(lca(v,u)) = j)はhI(v)かhI(u)かその他の最大値。そして一意であることを考えると…\n\t\tVertex x, y;\n\t\tif(j == hIv) x = v;\n\t\telse {\t\t\t//lcaはvのパス上には無い\n\t\t\tWord kMask = highestOneBitMask(ancestorHeights[v] & (j-1));\t//vの祖先で、jよりは低いけどその中で一番上にあるパス\n\t\t\tx = pathParents[(indices[v] & ~kMask | (kMask+1))-1];\t//indices[v]のkの高さの祖先のパスの親\n\t\t}\n\t\tif(j == hIu) y = u;\n\t\telse {\t\t\t//lcaはuのパス上には無い\n\t\t\tWord kMask = highestOneBitMask(ancestorHeights[u] & (j-1));\t//uの祖先で、jよりは低いけどその中で一番上にあるパス\n\t\t\ty = pathParents[(indices[u] & ~kMask | (kMask+1))-1];\t//indices[u]のkの高さの祖先のパスの親\n\t\t}\n\t\treturn indices[x] < indices[y] ? x : y;\t//in-orderなので、インデックスが低い方が祖先なはず\n\t}\n};\n\nstruct FenwickTree {\n\ttypedef long long T;\n\tvector<T> v;\n\tFenwickTree() { }\n\tvoid init(int n) { v.assign(n, 0); }\n\tvoid add(int i, T x) {\n\t\tfor(; i < (int)v.size(); i |= i+1) v[i] += x;\n\t}\n\tT sum(int i) {\t//[0, i)\n\t\tT r = 0;\n\t\tfor(-- i; i >= 0; i = (i & (i+1)) - 1) r += v[i];\n\t\treturn r;\n\t}\n\tT sum(int left, int right) {\t//[left, right)\n\t\treturn sum(right) - sum(left);\n\t}\n};\n\nvoid direct_tree(const vector<vi> &g, int i, int parent, vector<pii> &out_edges) {\n\teach(j, g[i]) if(*j != parent) {\n\t\tout_edges.pb(mp(i, *j));\n\t\tdirect_tree(g, *j, i, out_edges);\n\t}\n}\n\nint cost[2000];\nint main() {\n\tint N;\n\tscanf(\"%d\", &N);\n\trep(i, N) scanf(\"%d\", &cost[i]);\n\tvector<vi> g(N);\n\trep(i, N-1) {\n\t\tint a, b;\n\t\tscanf(\"%d%d\", &a, &b); a --, b --;\n\t\tg[a].pb(b); g[b].pb(a);\n\t}\n\tvector<Graph::Edge> edges;\n\tdirect_tree(g, 0, -1, edges);\n\tSchieberVishkinLCA lca; lca.build(Graph(N, edges), 0);\n\tCentroidPathDecomposition cpd; cpd.build(g, 0);\n\tvector<FenwickTree> ft(cpd.lengths.size());\n\trep(i, ft.size())\n\t\tft[i].init(cpd.lengths[i]);\n\trep(i, N) {\n\t\tint c, p;\n\t\tcpd.get(i, c, p);\n\t\tft[c].add(p, cost[i]);\n\t}\n\tint Q;\n\tscanf(\"%d\", &Q);\n\trep(ii, Q) {\n\t\tint t;\n\t\tscanf(\"%d\", &t);\n\t\tif(t == 0) {\n\t\t\tint A, B;\n\t\t\tscanf(\"%d%d\", &A, &B); A --, B --;\n\t\t\tint C = lca.query(A, B);\n\t\t\tint c, p, lc, lp;\n\t\t\tcpd.get(C, lc, lp);\n\t\t\tcpd.get(A, c, p);\n\t\t\tll ans = 0;\n\t\t\twhile(1) {\n\t\t\t\tint up = c != lc ? 0 : lp;\n\t\t\t\tans += ft[c].sum(up, p+1);\n\t\t\t\tif(c == lc) break;\n\t\t\t\tcpd.go_up(c, p);\n\t\t\t}\n\t\t\tcpd.get(B, c, p);\n\t\t\twhile(1) {\n\t\t\t\tint up = c != lc ? 0 : lp+1;\n\t\t\t\tans += ft[c].sum(up, p+1);\n\t\t\t\tif(c == lc) break;\n\t\t\t\tcpd.go_up(c, p);\n\t\t\t}\n\t\t\tprintf(\"%lld\\n\", ans);\n\t\t}else {\n\t\t\tint A, C;\n\t\t\tscanf(\"%d%d\", &A, &C); A --;\n\t\t\tint c, p;\n\t\t\tcpd.get(A, c, p);\n\t\t\tft[c].add(p, C);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 1588, "score_of_the_acc": -0.1753, "final_rank": 1 } ]
aoj_1542_cpp
Problem B: Yu-kun Likes Letters in the English Alphabet Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい英小文字が大好きだ。 そんなゆう君は紙と丸と線、そして英小文字を使う新しい遊びを考え、その採点用プログラムを書くことにした。 Problem 初期状態として、紙に V 個の丸と E 本の線が書かれている。丸には0から昇順に番号がつけられており、各丸の中には英小文字が1つ書かれているか、何も書かれていない。 各線は異なる2つの丸を結んでいる。1つの丸が26本以上の線で結ばれることはない。 遊びの手順は以下の通りである。 何も書かれていない丸を1つ選ぶ。 そのような丸が存在しない場合はこの処理を終了する。 丸の中に英小文字を1つ書く。 ただし丸が既に英小文字の書かれている丸と線で結ばれている場合、その英小文字と同じ英小文字は書くことはできない。 1. に戻る。 上記の手順に従って全ての丸に英小文字を書いた後、丸の番号が小さい順に丸の中の英小文字を並べ文字列を作る。 こうしてできる文字列を辞書順で最小となるようにしたい。 2つの同じ長さの文字列 s , t があり、 s が t より辞書順で小さいとは次のような場合を言う。 s i は文字列 s の i 番目の英小文字を、 t i は文字列 t の i 番目の英小文字を表す。 s i が t i と異なるような最小の i について、 s i が t i より小さい。 紙の初期状態が与えられるので、そこから上記の手順にしたがって作成できる文字列のうち辞書順最小のものを出力せよ。 Input V E a 0 a 1 ... a (V-1) s 0 t 0 s 1 t 1 ... s (E-1) t (E-1) 1行目に丸の数 V と線の数 E が空白区切りで与えられる。 2行目に丸の初期状態が空白区切りで与えられる。 a i が英小文字の場合は i 番目の丸にその英小文字が書かれており、'?'の場合は i 番目の丸には何も書かれていないことを表す。 続く E 行に線の情報が s i t i として与えられ、これは s i 番の丸と t i 番の丸が線で結ばれていることを表す。 Constraints 入力は以下の条件を満たす。 1 ≤ V ≤ 100,000 0 ≤ E ≤ 200,000 a i は英小文字, または '?' のいずれか ( 0 ≤ i ≤ V-1 ) 0 ≤ s i , t i ≤ V-1 ( s i < t i , 0 ≤ i ≤ E-1 ) 1つの丸が26本以上の線で結ばれることはない Output 問題文中の手順に従って作成可能な文字列のうち、辞書順で最も小さいものを1行に出力せよ Sample Input 1 3 3 c ? ? 0 1 0 2 1 2 Sample Output 1 cab Sample Input 2 3 2 c ? ? 0 1 0 2 Sample Output 2 caa Sample Input 3 7 6 ? a ? ? z a ? 0 1 0 2 3 4 4 5 4 6 5 6 Sample Output 3 baaazab Sample Input 4 5 0 ? ? ? ? ? Sample Output 4 aaaaa
[ { "submission_id": "aoj_1542_8295587", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint N;\nint M, A[1 << 18], B[1 << 18];\nchar C[1 << 18];\nvector<int> G[1 << 18];\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M;\n\tfor (int i = 1; i <= N; i++) cin >> C[i];\n\tfor (int i = 1; i <= M; i++) cin >> A[i] >> B[i];\n\tfor (int i = 1; i <= M; i++) A[i] += 1;\n\tfor (int i = 1; i <= M; i++) B[i] += 1;\n\tfor (int i = 1; i <= M; i++) {\n\t\tG[A[i]].push_back(B[i]);\n\t\tG[B[i]].push_back(A[i]);\n\t}\n\n\t// Step 2. Greedy\n\tfor (int i = 1; i <= N; i++) {\n\t\tif (C[i] != '?') continue;\n\t\tvector<bool> used(26, false);\n\t\tfor (int j : G[i]) {\n\t\t\tif (C[j] == '?') continue;\n\t\t\tint val = (C[j] - 'a');\n\t\t\tused[val] = true;\n\t\t}\n\t\tfor (int j = 0; j < 26; j++) {\n\t\t\tif (used[j] == true) continue;\n\t\t\tC[i] = ('a' + j);\n\t\t\tbreak;\n\t\t}\n\t}\n\n\t// Step 3. Output\n\tfor (int i = 1; i <= N; i++) cout << C[i];\n\tcout << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 15928, "score_of_the_acc": -0.1351, "final_rank": 9 }, { "submission_id": "aoj_1542_7101329", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string.h>\n#include <algorithm>\nusing namespace std;\nvector<int> g[100002];\nvector<char> vec;\nint dp[100002][27];\n\nint main() {\n\tmemset(dp,0,sizeof(dp));\n\tint n,m;\n\tcin>>n>>m;\n\t\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tcin>>c;\n\t\tvec.push_back(c);\n\t}\n\tfor(int i=0;i<m;i++){\n\t\tint x,y;\n\t\tcin>>x>>y;\n\t\tg[min(x,y)].push_back(max(x,y));\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c1;\n\t\tif(vec[i]=='?'){\n\t\t\tfor(int j=0;j<g[i].size();j++){\n\t\t\t\tchar c2=vec[g[i][j]];\n\t\t\t\tif(c2!='?'){\n\t\t\t\t\tdp[i][c2-'a']=1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int j=0;j<26;j++){\n\t\t\t\tif(dp[i][j]==0){\n\t\t\t\t\tvec[i]='a'+j;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tc1=vec[i];\n\t\t}else{\n\t\t\tc1=vec[i];\n\t\t}\n\t\tint c2=c1-'a';\n\t\tfor(int j=0;j<g[i].size();j++){\n\t\t\tdp[g[i][j]][c2]=1;\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tcout<<vec[i];\n\t}\n\tcout<<endl;\n\treturn 0;\n\t\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 19624, "score_of_the_acc": -0.2118, "final_rank": 13 }, { "submission_id": "aoj_1542_4243971", "code_snippet": "#include<algorithm>\n#include<bitset>\n#include<cassert>\n#include<cfloat>\n#include<climits>\n#include<cmath>\n#include<deque>\n#include<functional>\n#include<iomanip>\n#include<iostream>\n#include<map>\n#include<queue>\n#include<set>\n#include<stack>\n#include<string>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<vector>\n\n#define REP(i, n) for(int i = 0;i < n;i++)\n#define REPR(i, n) for(int i = n;i >= 0;i--)\n#define FOR(i, m, n) for(int i = m;i < n;i++)\n#define FORR(i, m, n) for(int i = m;i >= n;i--)\n#define SORT(v, n) sort(v, v+n);\n#define VSORT(v) sort(v.begin(), v.end());\n#define llong long long\n#define pb(a) push_back(a)\n\nusing namespace std;\n\ntypedef long long int ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\n\ntemplate<typename T>\nvector<T> make_v(size_t a) { return vector<T>(a); }\ntemplate<typename T, typename... Ts>\nauto make_v(size_t a, Ts... ts) {\n\treturn vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));\n}\ntemplate<typename T, typename V>\ntypename enable_if<is_class<T>::value == 0>::type\nfill_v(T& t, const V& v) { t = v; }\ntemplate<typename T, typename V>\ntypename enable_if<is_class<T>::value != 0>::type\nfill_v(T& t, const V& v) {\n\tfor (auto& e : t) fill_v(e, v);\n}\n\n\n// aよりもbが大きいならばaをbで更新する\n// (更新されたならばtrueを返す)\ntemplate <typename T>\nbool chmax(T & a, const T & b) {\n\tif (a < b) {\n\t\ta = b; // aをbで更新\n\t\treturn true;\n\t}\n\treturn false;\n}\n// aよりもbが小さいならばaをbで更新する\n// (更新されたならばtrueを返す)\ntemplate <typename T>\nbool chmin(T & a, const T & b) {\n\tif (a > b) {\n\t\ta = b; // aをbで更新\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n\n#define ARRAY_MAX 100005\nconst int INF = 1e9 + 7;\nconst ll MOD = 1e9 + 7;\n\nint dx[4] = { 1,0,0,-1 };\nint dy[4] = { 0,1,-1,0 };\n\n\n/******************************************************************************************/\n\nvector<int> Edge[100005];\nint check[100005][30];\n\nint main() {\n\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tcout << fixed << setprecision(10);\n\n\tint V,E;\n\tcin >> V >> E;\n\tvector<char> A(V);\n\tstring alpha = \"abcdefghijklmnopqrstuvwxyz\";\n\tfor (int i = 0; i < V; i++)\n\t{\n\t\tcin >> A[i];\n\t\tif(A[i] != '?'){\n\t\t\tcheck[i][A[i]-'a'] = 1;\n\t\t}\n\t}\n\t\n\tfor (int i = 0; i < E; i++)\n\t{\n\t\tint s,t;\n\t\tcin >> s >> t;\n\t\tEdge[s].push_back(t);\n\t\tEdge[t].push_back(s);\n\t}\n\t\n\tfor (int i = 0; i < V; i++)\n\t{\n\t\t//今見ている頂点\n\t\tif(A[i] != '?')continue;\n\t\tfor(auto letter:alpha){\n\t\t\t//埋め込むアルファベットの候補\n\t\t\tbool flag = true;\n\t\t\tif(check[i][letter-'a'])continue;\n\t\t\tfor(auto nearId:Edge[i]){\n\t\t\t\t//周囲に既に使われていないかの確認\n\t\t\t\t//nearIdはインデックス番号\n\t\t\t\tif(check[nearId][letter-'a']){\n\t\t\t\t\tflag = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tif(flag){\n\t\t\t\t//cout << \"i= \" << i << \" letter= \" << letter << endl;\n\t\t\t\t//letterは使用可能\n\t\t\t\tcheck[i][letter-'a'] = 1;\n\t\t\t\tA[i] = letter;\t\n\t\t\t\t/*for(auto nearId:Edge[i]){\n\t\t\t\t\tcheck[nearId][letter-'a'] = 1;\n\t\t\t\t}*/\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < V; i++)\n\t{\n\t\tcout << A[i];\n\t}\n\t\n\tcout << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 21792, "score_of_the_acc": -0.1099, "final_rank": 6 }, { "submission_id": "aoj_1542_4243919", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i, s, n) for (int i = s; i < n; ++i)\n#define rep(i, n) REP(i, 0, n)\n#define SORT(c) sort((c).begin(), (c).end())\n#define IINF INT_MAX\n#define LLINF LLONG_MAX\n#define DEBUG false\n// sort(a.begin(), a.end(), std::greater<int>());\n\n/*\nstd::vector<std::string> split(const std::string &input, char delimiter)\n{\n std::istringstream stream(input);\n\n std::string field;\n std::vector<std::string> result;\n while (std::getline(stream, field, delimiter))\n {\n result.push_back(field);\n }\n return result;\n}\n*/\n/*\nint dfs(int foo, int timer)\n{\n //foo is node number\n if (finds[foo].first != -1)\n return timer;\n finds[foo].first = ++timer;\n int result = timer;\n if (data[foo].size() != 0)\n {\n rep(i, data[foo].size())\n {\n result = dfs(data[foo][i], result);\n }\n }\n finds[foo].second = ++result;\n return result;\n}\n*/\n\nint main()\n{\n int v, e;\n string s;\n cin >> v >> e;\n rep(i, v)\n {\n char hoge;\n cin >> hoge;\n s.push_back(hoge);\n }\n map<int, set<int>> a;\n rep(i, e)\n {\n int x, y;\n cin >> x >> y;\n a[x].insert(y);\n a[y].insert(x);\n }\n rep(i, s.size())\n {\n set<char> hoge;\n rep(k, 26)\n {\n hoge.insert('a' + k);\n }\n if (s[i] != '?')\n continue;\n else\n {\n for (auto itr = a[i].begin(); itr != a[i].end(); itr++)\n {\n hoge.erase(s[*itr]);\n }\n s[i] = *hoge.begin();\n }\n }\n cout << s << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 31152, "score_of_the_acc": -0.9694, "final_rank": 18 }, { "submission_id": "aoj_1542_4243863", "code_snippet": "/*\nDear ToM,\nCongratulations on your graduation!\nYou did a great job!\n ☆゚*@*゚☆\n  (@*。☆。*@\n  /☆。*@゚*☆\n▲//。@。*☆@*\n ▼―ー。*@゚*☆\n*/\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,s,n) for(int i=s; i<n; ++i)\n#define rep(i,n) REP(i,0,n)\n#define SORT(c) sort((c).begin(),(c).end())\n#define IINF INT_MAX\n#define LLINF LLONG_MAX\n#define DEBUG false\n\ntypedef long long ll;\ntypedef pair<int, int> ii;\n\nint main(){\n\tll v, e;\n\tcin >> v >> e;\n\tvector<vector<int>> g(v);\n\tchar cs[v];\n\trep(i, v){\n\t\tcin >> cs[i];\n\t}\n\trep(i, e){\n\t\tint s, t;\n\t\tcin >> s >> t;\n\t\tg[s].push_back(t);\n\t\tg[t].push_back(s);\n\t}\n\n\trep(i, v){\n\t\tif(cs[i] != '?') continue;\n\t\tvector<bool> f(26, true);\n\t\trep(j, (int)g[i].size()){\n\t\t\tif(cs[g[i][j]] == '?') continue;\n\t\t\tf[cs[g[i][j]] - 'a'] = false;\n\t\t}\n\t\trep(j, (int)f.size()){\n\t\t\tif(f[j]){\n\t\t\t\tcs[i] = char('a' + j);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\trep(i, v) cout << cs[i];\n\tcout << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 9760, "score_of_the_acc": -0.134, "final_rank": 8 }, { "submission_id": "aoj_1542_4243852", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing i64 = int64_t;\n\nint main(){\n int V, E;\n cin >> V >> E;\n vector<char> v;\n for(int i=0;i<V;++i){\n char a;\n cin >> a;\n v.push_back(a);\n }\n vector<vector<int>> graph(V, vector<int>());\n for(int i=0;i<E;++i){\n int s, t;\n cin >> s >> t;\n graph[s].push_back(t);\n graph[t].push_back(s);\n }\n for(int i=0;i<V;++i){\n if(v[i] != '?')continue;\n set<char> st;\n for(auto to: graph[i]){\n if(v[to] == '?')continue;\n st.insert(v[to]);\n }\n char c = 'a';\n while(st.count(c))c++;\n v[i] = c;\n }\n for(auto e: v)cout << e;\n cout << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9640, "score_of_the_acc": -0.1569, "final_rank": 11 }, { "submission_id": "aoj_1542_4243850", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long int;\nusing P = pair<int, int>;\nusing P3 = pair<ll,P>;\nusing PP = pair<P, P>;\nconstexpr int INF = 1 << 30;\nconstexpr ll MOD = ll(1e9) + 7;\nconstexpr int di[] = {0, 1, 0, -1};\nconstexpr int dj[] = {1, 0, -1, 0};\nconstexpr double EPS = 1e-8;\n\nint V, E;\nvector<vector<int> > g;\nvector<char> ch;\n\nbool solve(){\n cin >> V >> E;\n g.resize(V);\n ch.resize(V);\n for(int i=0;i<V;i++){\n cin >> ch[i];\n }\n for(int i=0;i<E;i++){\n int u, v;\n cin >> u >> v;\n g[u].push_back(v);\n g[v].push_back(u);\n }\n vector<bool> used(26);\n for(int i=0;i<V;i++){\n if(ch[i] != '?') continue;\n fill(used.begin(),used.end(),false);\n for(auto j : g[i]){\n if(ch[j]=='?') continue;\n used[ch[j]-'a'] = true;\n }\n for(int j=0;j<26;j++){\n if(!used[j]){\n ch[i] = char('a'+j);\n break;\n }\n }\n }\n for(auto c: ch){\n cout << c;\n }\n cout << endl;\n return false;\n}\n\nint main(){\n solve();\n // while(!solve());\n // int t; cin >> t; for(;t>0;t--) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9704, "score_of_the_acc": -0.1574, "final_rank": 12 }, { "submission_id": "aoj_1542_3782188", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n\nint main(){\n int n, e;\n cin >> n >> e;\n vector<char> c(n);\n for(int i = 0; i < n; i++) cin >> c[i];\n vector<int> v[n];\n while(e-- > 0){\n\t int x, y;\n\t cin >> x >> y;\n\t v[x].push_back(y);\n\t v[y].push_back(x);\n }\n for(int i = 0; i < n; i++){\n\t if(c[i] != '?')\tcontinue;\n\t bool used[26] = {};\n\t for(int j : v[i]){\n\t if(c[j] != '?')\tused[c[j]-'a'] = true;\n\t }\n\t for(int j = 0; j < 26; j++){\n\t if(!used[j]){\n\t\t c[i] = (char)('a' + j);\n\t\t break;\n\t }\n }\n }\n for(int i = 0; i < n; i++)\tcout << c[i];\n cout << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 9644, "score_of_the_acc": -0.1331, "final_rank": 7 }, { "submission_id": "aoj_1542_3577833", "code_snippet": "#include <bits/stdc++.h>\n\nint main()\n{\n\tint V, E;\n\tscanf(\"%d%d\", &V, &E);\n\tstd::vector<std::list<int>> graph(V);\n\tstd::vector<char> chara(V);\n\tfor (auto& e: chara) scanf(\" %c\", &e);\n\n\tfor (int i{}; i < E; i++)\n\t{\n\t\tint s, t;\n\t\tscanf(\"%d%d\", &s, &t);\n\t\tgraph[s].push_back(t);\n\t\tgraph[t].push_back(s);\n\t}\n\tfor (int i{}; i < V; i++)\n\t{\n\t\tif (chara[i] != '?') continue;\n\t\tbool table[26]{};\n\t\tfor (auto& e: graph[i])\n\t\t\tif (chara[e] != '?')\n\t\t\t\ttable[chara[e] - 'a'] = true;\n\t\tfor (int j{}; j < 26; j++)\n\t\t\tif (!table[j])\n\t\t\t{\n\t\t\t\tchara[i] = 'a' + j;\n\t\t\t\tbreak;\n\t\t\t}\n\t}\n\tfor (auto& e: chara) putchar(e);\n\tputchar('\\n');\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 17056, "score_of_the_acc": -0.0963, "final_rank": 5 }, { "submission_id": "aoj_1542_3323255", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst int N = 100000;\nvector<int> G[N];\n\nint main(){\n int n,m;\n cin >>n >>m;\n vector<char> a(n);\n rep(i,n) cin >>a[i];\n rep(i,m){\n int u,v;\n cin >>u >>v;\n G[u].pb(v);\n G[v].pb(u);\n }\n\n rep(i,n){\n if(a[i]=='?'){\n vector<bool> used(26);\n for(int j:G[i]){\n if(a[j]!='?') used[a[j]-'a'] = true;\n }\n\n a[i] = 'a';\n while(used[a[i]-'a']) ++a[i];\n }\n cout << a[i];\n }\n cout << \"\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 9916, "score_of_the_acc": -0.1353, "final_rank": 10 }, { "submission_id": "aoj_1542_3271206", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 100005\n\nstruct Node{\n\n\tchar ch;\n\tvector<int> ADJ;\n};\n\nint V,E;\nbool check[26];\nchar buf[2];\nNode nodes[NUM];\n\n\nint main(){\n\n\tscanf(\"%d %d\",&V,&E);\n\n\tfor(int i = 0; i < V; i++){\n\n\t\tscanf(\"%s\",buf);\n\t\tnodes[i].ch = buf[0];\n\t}\n\n\tint from,to;\n\n\tfor(int loop = 0; loop < E; loop++){\n\n\t\tscanf(\"%d %d\",&from,&to);\n\t\tnodes[from].ADJ.push_back(to);\n\t\tnodes[to].ADJ.push_back(from);\n\t}\n\n\tfor(int i = 0; i < V; i++){\n\n\t\tif(nodes[i].ch != '?'){\n\n\t\t\tprintf(\"%c\",nodes[i].ch);\n\n\t\t}else{\n\n\t\t\tfor(int k = 0; k < 26; k++){\n\n\t\t\t\tcheck[k] = false;\n\t\t\t}\n\n\t\t\tfor(int k = 0; k < nodes[i].ADJ.size(); k++){\n\n\t\t\t\tif(nodes[nodes[i].ADJ[k]].ch != '?'){\n\n\t\t\t\t\tcheck[nodes[nodes[i].ADJ[k]].ch-'a'] = true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor(int k = 0; k < 26; k++){\n\n\t\t\t\tif(!check[k]){\n\t\t\t\t\tnodes[i].ch = 'a'+k;\n\t\t\t\t\tprintf(\"%c\",nodes[i].ch);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"\\n\");\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 10732, "score_of_the_acc": -0.0226, "final_rank": 1 }, { "submission_id": "aoj_1542_1672411", "code_snippet": "/*\n * b.cc: \n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n\nusing namespace std;\n\n/* constant */\n\nconst int MAX_VN = 100000;\nconst int MAX_EN = 200000;\n\n/* typedef */\n\ntypedef vector<int> vi;\n\n/* global variables */\n\nint as[MAX_VN];\nbool used[MAX_VN][26];\nvi nbrs[MAX_VN];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int vn, en;\n cin >> vn >> en;\n\n for (int i = 0; i < vn; i++) {\n char ai;\n cin >> ai;\n as[i] = (ai == '?') ? -1 : ai - 'a';\n }\n\n for (int i = 0; i < en; i++) {\n int si, ti;\n cin >> si >> ti;\n\n nbrs[si].push_back(ti);\n if (as[ti] >= 0) used[si][as[ti]] = true;\n\n nbrs[ti].push_back(si);\n if (as[si] >= 0) used[ti][as[si]] = true;\n }\n\n for (int i = 0; i < vn; i++) {\n if (as[i] < 0) {\n for (int j = 0; j < 26; j++)\n\tif (! used[i][j]) {\n\t as[i] = j;\n\t break;\n\t}\n\n vi &nbri = nbrs[i];\n for (vi::iterator vit = nbri.begin(); vit != nbri.end(); vit++)\n\tused[*vit][as[i]] = true;\n\n }\n\n putchar(as[i] + 'a');\n }\n putchar('\\n');\n\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 10864, "score_of_the_acc": -0.238, "final_rank": 16 }, { "submission_id": "aoj_1542_1080937", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint v,e;\nchar a[100100];\nvector<int> g[100100];\n\nint main(){\n cin >> v >> e;\n for(int i=0;i<v;i++)cin >> a[i];\n for(int i=0;i<e;i++){\n int s,t;\n cin >> s >> t;\n g[s].push_back(t); g[t].push_back(s);\n }\n\n for(int i=0;i<v;i++){\n if(a[i] == '?'){\n bool use[30] = {};\n for(int j=0;j<(int)g[i].size();j++){\n\tint u = g[i][j];\n\tif(a[u] != '?')use[a[u]-'a'] = true;\n }\n \n for(int j=0;j<26;j++){\n\tif(!use[j]){\n\t cout << (a[i] = (char)('a' + j));\n\t break;\n\t}\n }\n }else cout << a[i];\n }\n cout << endl;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 8080, "score_of_the_acc": -0.216, "final_rank": 15 }, { "submission_id": "aoj_1542_1080735", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <set>\n#include <map>\n#include <algorithm>\t// require sort next_permutation count __gcd reverse etc.\n#include <cstdlib>\t// require abs exit\n#include <cstdio>\t// require scanf printf\n#include <functional>\n#include <numeric>\t// require accumulate\n#include <cmath>\n#include <climits>\n#include <limits>\n#include <cfloat>\n#include <iomanip>\t// require setw\n#include <sstream>\t// require stringstream \n#include <cstring>\t// require memset\n#include <cctype>\t// require tolower, toupper\n#include <fstream>\t// require freopen\n#include <ctime>\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define ALL(A) A.begin(), A.end()\n#define INF 1<<29\n#define EPS 1e-9\n#define each(i,c) for(auto i=(c).begin();i!=(c).end();++i)\n#define exist(s,e) ((s).find(e)!=(s).end())\n#define clr(a) memset((a),0,sizeof(a))\n#define nclr(a) memset((a),-1,sizoef(a))\n#define sz(s) (int)((s).size())\n#define INRANGE(x,s,e) ((s)<=(x) && (x)<(e))\n#define pb push_back\n#define MP(x,y) make_pair((x),(y))\n#define DEBUG 0\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<int, int> P;\nstring FILE_NAME = \"testcase.B\";\nstring NAME;\nstring itos (int n )\n{\n\tstringstream ss;\n\tss << n;\n\n\treturn ss.str();\n}\n\nconst int MAX_V = (int)1e5 + 5;\nint V, E;\nvector<int> G[MAX_V];\nvector<char> v;\nbool used[26];\n\nint main()\n{\n//\tcut here before submit \n#if DEBUG\n\tNAME = FILE_NAME;\n\tint CNT = 1;\n\tNAME += itos (CNT );\n\twhile (freopen (NAME.c_str() , \"r\", stdin ) != NULL ) {\n#endif\n\tv.clear();\n\trep (i, MAX_V ) G[i].clear();\n\tios_base::sync_with_stdio(0);\n\tcin >> V >> E;\n \tv.resize (V, 0 );\n\trep (i, V ) cin >> v[i];\n\trep (i, E ){\n\t\tint s, t;\n\t\tcin >> s >> t;\n\t\tG[s].push_back (t );\n\t\tG[t].push_back (s );\n\t} // end rep\n\n\trep (i, V ){\n\t\tif (v[i] == '?' ){\n\t\t\tmemset (used, false, sizeof (used ) );\n\t\t\trep (j, G[i].size() ){\n\t\t\t\tint u = G[i][j];\n\t\t\t\tif (v[u] != '?' ) used[(int)(v[u] - 'a')] |= true;\n\t\t\t} // end rep\n\t\t\trep (j, 26 ){\n\t\t\t\tif (!used[j] ){\n\t\t\t\t\tv[i] = (char)(j+'a');\n\t\t\t\t\tbreak;\n\t\t\t\t} // end if\n\t\t\t} // end rep\n\t\t} // end if\n\t} // end rep\n\n\trep (i, V ) cout << v[i];\n\tcout << endl;\n#if DEBUG\n\tCNT++;\t// cut here before submit\n\tNAME = FILE_NAME;\n\tNAME += itos (CNT );\n\t} // end loop; cut here before submit\n#endif\t\t\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 8112, "score_of_the_acc": -0.0496, "final_rank": 3 }, { "submission_id": "aoj_1542_1080624", "code_snippet": "#include <string>\n#include <vector>\n#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<list>\n#include<deque>\n#include<bitset>\n#include<set>\n#include<map>\n#include<cstring>\n#include<sstream>\n#include<complex>\n#define X first\n#define Y second\n#define pb push_back\n#define rep(X,Y) for (int (X) = 0;(X) < int(Y);++(X))\n#define rrep(X,Y) for (int (X) = int(Y-1);(X) >=0;--(X))\n#define all(X) (X).begin(),(X).end()\n#define rall(X) (X).rbegin(),(X).rend()\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\nint v,e;\n\nint main(){\n int i,j,k;\n cin>>v>>e;\n vector<char> cs(v);\n vector<int> edges[v];\n rep(i,v)\n cin>>cs[i];\n int te,mp;\n rep(i,e){\n cin>>te>>mp;\n edges[te].pb(mp);\n edges[mp].pb(te);\n }\n rep(i,v){\n if(cs[i]!='?')continue;\n vector<int> memo(256,0);\n rep(j,edges[i].size())\n memo[cs[edges[i][j]]]=1;\n char cc;\n for(cc='a';cc<='z';cc++)\n if(!memo[cc])break;\n cs[i]=cc;\n }\n rep(i,v)\n cout<<cs[i];cout<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 7868, "score_of_the_acc": -0.2382, "final_rank": 17 }, { "submission_id": "aoj_1542_1080521", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<sstream>\n#include<utility>\n#include<map>\n#include<vector>\n#include<queue>\n#include<algorithm>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int>P;\n\nbool flg[100005][27];\nvector<int> G[100005];\n\t\nint V,E;\nchar A[100005]={0};\n\nint main()\n{\n\tscanf(\"%d%d\\n\",&V,&E);\n\tfor(int i=0;i<V;i++)\n\t{\n\t\tscanf(\"%c%*c\",&A[i]);\n\t}\n\tfor(int i=0;i<E;i++)\n\t{\n\t\tint s,t;\n\t\tscanf(\"%d%d\",&s,&t);\n\t\tG[s].push_back(t);\n\t\tG[t].push_back(s);\n\t}\n\tfor(int i=0;i<V;i++)\n\t{\n\t\tif(A[i]!='?')flg[i][A[i]-'a']=1;\n\t}\n\tfor(int i=0;i<V;i++)\n\t{\n\t\tif(A[i]=='?')\n\t\t{\n\t\t\tfor(char x='a';x<='z';x++)\n\t\t\t{\n\t\t\t\tint j;\n\t\t\t\tfor(j=0;j<G[i].size();j++)\n\t\t\t\t{\n\t\t\t\t\tif(flg[G[i][j]][x-'a'])break;\n\t\t\t\t}\n\t\t\t\tif(j==G[i].size())\n\t\t\t\t{\n\t\t\t\t\tA[i]=x;\n\t\t\t\t\tflg[i][x-'a']=1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tputs(A);\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 10656, "score_of_the_acc": -0.0697, "final_rank": 4 }, { "submission_id": "aoj_1542_1080381", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<string>\n#include<cstring>\n#include<vector>\n#include<set>\n#include<list>\n#include<queue>\n#include<cmath>\n#include<functional>\n#include<algorithm>\n#define INF (1<<29)\n#define EPS 1e-10\n#define rep(i,n) for(int i=0;i<(n);i++)\nusing namespace std;\n\n\nint v,e;\nvector<int> edge[100000];\nstring a;\nset<char> able[100000];\n\nint main(){\n\tcin>>v>>e;\n\trep(i,v){\n\t\trep(j,26)able[i].insert('a'+j);\n\t}\n\trep(i,v){\n\t\tchar c;\n\t\tcin>>c;\n\t\ta+=c;\n\t}\n\trep(i,e){\n\t\tint s,t;\n\t\tcin>>s>>t;\n\t\tedge[s].push_back(t);\n\t\tedge[t].push_back(s);\n\t}\n\trep(i,v){\n\t\tif(a[i]=='?')continue;\n\t\trep(j,edge[i].size()){\n\t\t\table[edge[i][j]].erase(a[i]);\n\t\t}\n\t}\n\trep(i,v){\n\t\tif(a[i]!='?')continue;\n\t\tchar c=*able[i].begin();\n\t\ta[i]=c;\n\t\trep(j,edge[i].size()){\n\t\t\table[edge[i][j]].erase(c);\n\t\t}\n\t}\n\tcout<<a<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 134660, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1542_1080348", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\nusing namespace std;\n\n#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)\ntemplate <typename T> void print_container(ostream& os, const T& c) { const char* _s = \" \"; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\n\ntemplate <typename T> void print(T a, int n, const string& split = \" \") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }\ntemplate <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }\ntemplate <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }\n#define dump(v) (cerr << #v << \": \" << v << endl)\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\n\nint main()\n{\n fast_io();\n\n int n, m;\n cin >> n >> m;\n string maru(n, '@');\n rep(i, n)\n cin >> maru[i];\n vector<vector<int>> g(n);\n rep(i, m)\n {\n int u, v;\n cin >> u >> v;\n g[u].push_back(v);\n g[v].push_back(u);\n }\n\n rep(i, n)\n {\n if (maru[i] != '?')\n continue;\n\n bool ng[26] = {};\n for (int j : g[i])\n if (maru[j] != '?')\n ng[maru[j] - 'a'] = true;\n\n rep(k, 26)\n {\n if (!ng[k])\n {\n maru[i] = 'a' + k;\n break;\n }\n }\n }\n cout << maru << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 7860, "score_of_the_acc": -0.0238, "final_rank": 2 }, { "submission_id": "aoj_1542_1080347", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=a;i<b;++i)\n#define allof(a) a.begin(),a.end()\n#define minit(a,b) memset(a,b,sizeof(a))\n#define size_of(a) (int)a.size()\n\ntypedef long long lint;\ntypedef pair<int, int> pii;\n\nint V, E;\nchar ans[100010];\nvector< vector<int> > edges;\n\nset<char> cand[100010];\n\nvoid solve() {\n\tfor_(i,0,V) {\n\t\tcand[i].clear();\n\t\tif (ans[i] != '?') continue;\n\t\tfor_(j,0,26) {\n\t\t\tcand[i].insert('a' + j);\n\t\t}\n\t}\n\t\n\tfor_(i,0,V) {\n\t\tif (ans[i] != '?') {\n\t\t\tint e_size = size_of(edges[i]);\n\t\t\tfor_(j,0,e_size) {\n\t\t\t\tint u = edges[i][j];\n\t\t\t\tcand[u].erase(ans[i]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tfor_(i,0,V) {\n\t\tif (ans[i] == '?') {\n\t\t\tchar c = *cand[i].begin();\n\t\t\tans[i] = c;\n\t\t\tint e_size = size_of(edges[i]);\n\t\t\tfor_(j,0,e_size) {\n\t\t\t\tint u = edges[i][j];\n\t\t\t\tcand[u].erase(c);\n\t\t\t}\n\t\t}\n\t\t\n\t\tcout << ans[i];\n\t}\n\tcout << endl;\n}\n\nint main() {\n\tcin >> V >> E;\n\tfor_(i,0,V) {\n\t\tchar c; cin >> c;\n\t\tans[i] = c;\n\t}\n\tedges.assign(V, vector<int>());\n\tfor_(i,0,E) {\n\t\tint s, t; cin >> s >> t;\n\t\tedges[s].push_back(t);\n\t\tedges[t].push_back(s);\n\t}\n\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 134260, "score_of_the_acc": -1.7826, "final_rank": 19 }, { "submission_id": "aoj_1542_1080322", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <map>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define FORE(i,a,b) for(int i=(a);i<=(b);i++)\n#define REP(i,b) FOR(i,0,b)\nconst int INF=1<<30;\nusing namespace std;\ntypedef pair<int,int> pr;\ntypedef vector<int> vi;\nchar word[100001];\nvi edge[100001];\nint V,E,a,b;\nvoid search(int num){\n\tif(num==V)\n\treturn ;\n\tif(word[num]=='?'){\n\tbool used[27]={};\n\tREP(i,edge[num].size())\n\tif(word[edge[num][i]] !='?')\n\tused[word[edge[num][i]]-'a']=true;\n\tREP(i,26)\n\tif(!used[i]){\n\t\tword[num]=i+'a';\n\t\tbreak;\n\t}\n\t}\n\tcout << word[num];\n\tsearch(num+1);\n\treturn ;\n}\nint main() {\n\tcin >> V >> E;\n\tREP(i,V)\n\tcin >> word[i];\n\tREP(j,E){\n\tcin >> a >> b;\n\tedge[a].push_back(b);\n\tedge[b].push_back(a);\n\t}\n\tsearch(0);\n\tcout << endl;\n\t// your code goes here\n\treturn 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 8076, "score_of_the_acc": -0.216, "final_rank": 14 } ]
aoj_1554_cpp
Problem D: Friday the 13th Problem 今年も「立命館大学競技プログラミング合宿」が開催されることとなった。僕は毎年開催されるこの合宿をとても楽しみにしている。しかし、欲望に耐え切れず合宿前に散財してしまい、お金に余裕が無くなってしまった。そこで僕は、立命館大学の最寄り駅である南草津まで移動するのに最も安く済む青春18きっぷを使うことにした。この切符は安く済む代わりに乗り換えが多く発生し、移動に丸1日使わなければならないのでとても疲れる。そんな移動をする日が13日の金曜日であることに気づいたのは会津若松駅を出発した後だった。僕は12時間、不安を感じながら電車に揺られるはめになった。 今日は合宿2日目、2015年3月15日、日曜日である。13日は特に何事もなく南草津に到着したが、僕はもうこのような不安を味わいたくないと思った。そこで2日目のジャッジである僕は、指定された期間内に存在する13日の金曜日の数を求める問題を出題し、プログラムを作成してもらうことにした。 閏(うるう)年の定義は以下の通りである。 西暦年が4で割り切れる年は閏年である。 ただし、100で割り切れる年は閏年でない。 ただし、400で割り切れる年は閏年である。 Input 空白で区切られた6つの整数 Y 1 , M 1 , D 1 , Y 2 , M 2 , D 2 が1行で与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ Y 1 ≤ Y 2 ≤ 10 18 1 ≤ M i ≤ 12 1 ≤ D i ≤ 31 ( M i = 1, 3, 5, 7, 8, 10, 12) 1 ≤ D i ≤ 30 ( M i = 4, 6, 9, 11) 1 ≤ D i ≤ 28 ( M i = 2 かつ Y i 年は閏年でない) 1 ≤ D i ≤ 29 ( M i = 2 かつ Y i 年は閏年である) Y 1 年 M 1 月 D 1 日は Y 2 年 M 2 月 D 2 日より0日以上前の日付である Output Y 1 年 M 1 月 D 1 日から Y 2 年 M 2 月 D 2 日までの間に存在する13日の金曜日の数を1行に出力せよ。 Sample Input1 2015 3 13 2015 3 13 Sample Output1 1 Sample Input2 2015 2 14 2015 3 15 Sample Output2 1 Sample Input3 1234 5 6 789012345678901234 5 6 Sample Output3 1357101234567708000
[ { "submission_id": "aoj_1554_5550660", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)\n#define foreach(i, n) for(auto &i:(n))\n#define all(x) (x).begin(), (x).end()\n#define bit(x) (1ll << (x))\n#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE\n#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\n//const ll MOD = (ll)1e9+7;\nconst ll MOD = 998244353;\nconst int INF = (ll)1e9+7;\nconst ll INFLL = (ll)1e18;\ntemplate<class t>\nusing vvector = vector<vector<t>>;\ntemplate<class t>\nusing vvvector = vector<vector<vector<t>>>;\ntemplate<class t>\nusing priority_queuer = priority_queue<t, vector<t>, greater<t>>;\ntemplate<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}\ntemplate<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}\n#ifdef DEBUG\n#define debug(x) cout<<\"LINE \"<<__LINE__<<\": \"<<#x<<\" = \"<<x<<endl;\n#else\n#define debug(x) (void)0\n#endif\n\nnamespace templates{\n ll modpow(ll x, ll b,ll mod=MOD){\n ll res = 1;\n while(b){\n if(b&1)res = res * x % mod;\n x = x * x % mod;\n b>>=1;\n }\n return res;\n }\n\n ll modinv(ll x){\n return modpow(x, MOD-2);\n }\n\n bool was_output = false;\n\n void print();\n template <class t> void print(const vector<t> &);\n template <class t, class u> void print(const pair<t, u> &);\n template <class t> void print(const t&);\n template <class Head, class... Tail> void print(const Head&, const Tail&...);\n\n template <class t> void println(const vector<vector<t>>&);\n template <class t> void println(const vector<t>&);\n template <class t> void println(const t&);\n template <class Head, class... Tail> void println(const Head&, const Tail&...);\n void println();\n void newline();\n\n void print(){\n return;\n }\n\n template <class t>\n void print(const vector<t>&x){\n for(const t&i:x)print(i);\n }\n template <class t, class u>\n void print(const pair<t,u>&p){\n print(p.first);\n print(p.second);\n }\n template <class t>\n void print(const t&x){\n if(was_output)cout<<\" \";\n cout<<x;\n was_output = true;\n }\n template <class Head, class... Tail>\n void print(const Head&head,const Tail&...tail){\n print(head);\n print(tail...);\n }\n\n template<class t>\n void println(const vector<vector<t>>&x){\n for(vector<t> i:x)println(i);\n }\n template<class t>\n void println(const vector<t>&x){\n for(const t&i:x)print(i);\n println();\n }\n template <class t>\n void println(const t&x){\n print(x);\n println();\n }\n void println(){\n if(was_output){\n cout << endl;\n was_output = false;\n }\n }\n template <class Head, class... Tail>\n void println(const Head&head,const Tail&...tail){\n print(head);\n println(tail...);\n }\n\n void newline(){\n was_output = true;\n println();\n }\n\n template<class t>\n istream& operator>>(istream&is, vector<t>&x){\n for(auto &i:x)is >> i;\n return is;\n }\n\n template<class t, class u>\n istream& operator>>(istream&is, pair<t, u>&x){\n is >> x.first >> x.second;\n return is;\n }\n\n template<class t>\n ostream& operator<<(ostream&os, vector<t> &v){\n os << \"{\";\n for(t &i:v){\n os << i << \", \";\n }\n os << \"}\";\n return os;\n }\n\n template<class t = long long>\n t in(){\n t res; cin >> res; return res;\n }\n\n template<class t>\n vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){\n sort(line.begin(),line.end(),comp);\n return line;\n }\n\n template<class t>\n vector<t> reversed(vector<t> line){\n reverse(line.begin(),line.end());\n return line;\n }\n string reversed(string str){\n reverse(str.begin(),str.end());\n return str;\n }\n\n long long gcd(long long a,long long b){\n while(b){\n a %= b;\n swap(a,b);\n }\n return a;\n }\n\n long long lcm(long long a,long long b){\n return a / gcd(a,b) * b;\n }\n\n class output_initializer{\n public:\n output_initializer(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << setprecision(20);\n }\n };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();\n}\n\nusing namespace templates;\n\nclass calendar{\npublic:\n ll y;\n ll m;\n ll d;\n ll date;\n calendar(ll y,ll m,ll d,ll date):y(y),m(m),d(d),date(date){}\n calendar(ll y,ll m,ll d):y(y),m(m),d(d){\n date = ((-calendar(2015,3,13,0).count_day()+calendar(y,m,d,0).count_day())%7+7)%7;\n }\n calendar next_day(){\n ll ndate = (date + 1) % 7;\n ll nd = d + 1;\n ll nm = m;\n ll ny = y;\n if(is_last_day()){\n nd = 1;\n ++nm;\n if(nm==13){\n ++ny;\n nm = 1;\n }\n }\n return calendar(ny,nm,nd,ndate);\n }\n\n bool is_uru(){\n return y%400==0 or (y%100 != 0 and y % 4==0);\n }\n\n bool is_last_day(){\n return d == last_day();\n }\n\n int last_day(){\n if(set<int>({1,3,5,7,8,10,12}).count(m))return 31;\n if(set<int>({4,6,9,11}).count(m))return 30;\n if(is_uru())return 29;\n return 28;\n }\n\n bool operator==(calendar other){\n return y == other.y and m == other.m and d == other.d;\n }\n bool operator!=(calendar other){\n return !((*this) == other);\n }\n\n ll count_day(){\n ll res = ((y-1) / 400) * (400 * 365 + 97) % 7;\n calendar tmp((y-1)/400*400+1,1,1,0);\n while((*this)!=tmp){\n ++res;\n tmp = tmp.next_day();\n }\n return res;\n }\n};\n\nll count_friday(calendar c){\n ll res = 0;\n {\n calendar c4(1,1,1);\n ll add = 0;\n while(c4.y!=401){\n if(c4.date==0 and c4.d==13){\n ++add;\n }\n c4 = c4.next_day();\n }\n res += (c.y-1)/400*add;\n }\n calendar tmp((c.y-1)/400*400+1,1,1);\n while(true){\n if(tmp.date==0 and tmp.d==13){\n ++res;\n }\n if(tmp==c)break;\n tmp = tmp.next_day();\n }\n return res;\n}\n\nll func(){\n ll res = 0;\n {\n ll y = in();\n ll m = in();\n ll d = in();\n res -= count_friday(calendar(y,m,d));\n if(d==13 and calendar(y,m,d).date==0){\n ++res;\n }\n }\n {\n ll y = in();\n ll m = in();\n ll d = in();\n res += count_friday(calendar(y,m,d));\n }\n return res;\n}\n\nint main(){\n println(func());\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3404, "score_of_the_acc": -2, "final_rank": 3 }, { "submission_id": "aoj_1554_1249200", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nll days(ll y, int m, int d){\n\tif(m < 3) y--, m += 12;\n\treturn 365 * (y % 7) + y / 4 - y / 100 + y / 400 + (153 * m + 8) / 5 + d;\n}\n\nint main(){\n\tll y, m, d, Y, M, D;\n\tmap<pi, ll> memo, prev;\n\t\n\t\n\tcin >> y >> m >> d >> Y >> M >> D;\n\tif(d > 13){\n\t\td = 1;\n\t\tif(++m > 12) m = 1, y++;\n\t}\n\twhile(d < 13) d++;\n\t\n\tif(D < 13){\n\t\tD = 31;\n\t\tif(--M < 1) M = 12, Y--;\n\t}\n\twhile(D > 13) D--;\n\t\n\t\n\tll ans = 0;\n\tbool flag = 0;\n\twhile(y < Y || y == Y && m <= M){\n\t\t\n\t\tif(days(y, m, d) % 7 != 5){\n\t\t\tif(++m > 12) m = 1, y++;\n\t\t\tcontinue;\n\t\t}\n\t\tans++;\n\t\tif(!flag && memo.count(pi(y % 2800, m))){\n\t\t\t\n\t\t\tll dy = y - prev[pi(y % 2800, m)];\n\t\t\tll da = ans - memo[pi(y % 2800, m)];\n\t\t\t\n\t\t\tll t = (Y - y) / dy;\n\t\t\tt = max(t - 10, 0ll);\n\t\t\ty += dy * t;\n\t\t\tans += da * t;\n\t\t\tflag = 1;\n\t\t}\n\t\t\n\t\tmemo[pi(y % 2800, m)] = ans;\n\t\tprev[pi(y % 2800, m)] = y;\n\t\tif(++m > 12) m = 1, y++;\n\t}\n\t\n\tcout << ans << endl;\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1796, "score_of_the_acc": -0.2847, "final_rank": 2 }, { "submission_id": "aoj_1554_1249195", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nll days(ll y, int m, int d){\n\tif(m < 3) y--, m += 12;\n\treturn 365 * (y % 7) + y / 4 - y / 100 + y / 400 + (153 * m + 8) / 5 + d;\n}\n\nint main(){\n\tll y, m, d, Y, M, D;\n\tmap<pi, ll> memo, prev;\n\t\n\t\n\tcin >> y >> m >> d >> Y >> M >> D;\n\tif(d > 13){\n\t\td = 1;\n\t\tif(++m > 12) m = 1, y++;\n\t}\n\twhile(d < 13) d++;\n\t\n\tif(D < 13){\n\t\tD = 31;\n\t\tif(--m < 1) m = 12, y--;\n\t}\n\twhile(D > 13) D--;\n\t\n\t\n\tll ans = 0;\n\tbool flag = 0;\n\twhile(y < Y || y == Y && m <= M){\n\t\t\n\t\tif(days(y, m, d) % 7 != 5){\n\t\t\tif(++m > 12) m = 1, y++;\n\t\t\tcontinue;\n\t\t}\n\t\tans++;\n\t\tif(!flag && memo.count(pi(y % 2800, m))){\n\t\t\t\n\t\t\tll dy = y - prev[pi(y % 2800, m)];\n\t\t\tll da = ans - memo[pi(y % 2800, m)];\n\t\t\t\n\t\t\tll t = (Y - y) / dy;\n\t\t\tt = max(t - 10, 0ll);\n\t\t\ty += dy * t;\n\t\t\tans += da * t;\n\t\t\tflag = 1;\n\t\t}\n\t\t\n\t\tmemo[pi(y % 2800, m)] = ans;\n\t\tprev[pi(y % 2800, m)] = y;\n\t\tif(++m > 12) m = 1, y++;\n\t}\n\t\n\tcout << ans << endl;\n\t\n\treturn 0;\n}", "accuracy": 0.3, "time_ms": 10, "memory_kb": 1792, "score_of_the_acc": -0.2829, "final_rank": 4 }, { "submission_id": "aoj_1554_1248859", "code_snippet": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <functional>\n#include <iostream>\n#include <map>\n#include <memory>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\n \nusing namespace std;\n \n#define sz size()\n#define pb push_back\n#define mp make_pair\n#define fi first\n#define se second\n#define all(c) (c).begin(), (c).end()\n#define rep(i,a,b) for(long long i=(a);i<(b);++i)\n#define clr(a, b) memset((a), (b) ,sizeof(a))\n \n#define MOD 1000000007\n\nlong long month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n\nlong long checkd(long long y, long long m, long long d){\n if(y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)){\n month[2] = 29;\n }\n else{\n month[2] = 28;\n }\n \n if(m < 1 || m > 12){\n return 0;\n }\n\n if(d >= 1 && d <= month[m]){\n return 1;\n }\n\n return 0;\n}\n\nlong long f(long long y, long long m, long long d){\n long long y1=1;\n long long m1=1;\n long long d1=1;\n long long week=0;\n long long ret = 0;\n long long dd = y/2800;\n y%=2800;\n y+=2800;\n for(;;){\n if(d1==13&&week==4)ret++;\n if(y==y1&&m==m1&&d==d1)break;\n if(checkd(y1,m1,d1)==0){\n if(m1==12){\n y1++;\n m1=1;\n d1=1;\n }\n else{\n m1++;\n d1=1;\n }\n continue;\n }\n else{\n d1++;\n week++;\n week%=7;\n }\n }\n return ret+4816*(dd-1);\n}\n\nlong long f2(long long y, long long m, long long d){\n long long y1=1;\n long long m1=1;\n long long d1=1;\n long long week=0;\n y%=2800;\n y+=2800;\n for(;;){\n if(y==y1&&m==m1&&d==d1)break;\n if(checkd(y1,m1,d1)==0){\n if(m1==12){\n y1++;\n m1=1;\n d1=1;\n }\n else{\n m1++;\n d1=1;\n }\n continue;\n }\n else{\n d1++;\n week++;\n week%=7;\n }\n }\n return week;\n}\n\nint main(){\n long long y1,m1,d1,y2,m2,d2;\n cin>>y1>>m1>>d1>>y2>>m2>>d2;\n long long a = 0;\n if(d1==13&&f2(y1,m1,d1)==4)a=1;\n cout << f(y2,m2,d2)-f(y1,m1,d1)+a << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1156, "score_of_the_acc": -0.1538, "final_rank": 1 } ]
aoj_1557_cpp
Problem G: Yu-kun Likes a Treasure Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい財宝が大好きだ。ゆう君は今日も財宝を探すため洞窟を探索する予定だ。そこでゆう君は洞窟の地図から、洞窟を全て見て回る際に歩く距離を求めるプログラムを書くことにした。 Problem 洞窟の情報とゆう君の初期位置が与えられる。Fig.1のように、 洞窟は2次元平面上の多角形で表される部屋が、1つ以上繋がった集合で構成される。部屋が別の部屋と接する場合は必ず点で接し、その接点が洞窟の道となり、接点に訪れるとその道を通ったことになる。 道は扉で閉ざされており、扉が閉まっている状態でその道を通ることはできない。扉を開くためには部屋の中にちょうど1つだけ存在するボタンを押す必要がある。ボタンは2次元平面上の点として表される。 ゆう君はボタンが存在する点を訪れることでボタンを押すことができる。ボタンを押すと部屋にある全ての扉が開く。Fig.1では部屋の中にある丸がボタンを表し、顔のマークがゆう君を、そして矢印が指しているボタンがゆう君のいる場所を表す。ボタンを押して扉を開いた後、道を通るとすぐに全ての扉は閉まる。そのため、そこから別の部屋へと移動するためには再度その部屋の中にあるボタンを押す必要がある。ゆう君は最初、いずれかの部屋の中にあるボタンと同じ場所にいる。 Fig. 1 Fig. 2 に移動の例を示す。番号の付いている矢印はゆう君の移動経路を表す。 次の順番で移動することは可能である。 1 -> 2 -> 3 -> 4 次の順番で移動することは不可能である。 1 -> 4 これは、1の経路で移動し部屋同士の接点を訪れるとその瞬間に扉が閉まるからである。 Fig. 2 ゆう君が洞窟内の全ての道を通り、再度初期位置に戻ってくるまでにかかる移動距離の最小値を出力せよ。ただし、同じ道を何度通っても良い。 Input 入力は以下の形式で与えられる。 n m 部屋1の情報 部屋2の情報 … 部屋 n の情報 n , m はそれぞれ洞窟を構成する部屋を表す多角形の数、ゆう君が最初にいる部屋の番号を表す。 部屋の情報は次の形式で与えられる。 p x 1 y 1 x 2 y 2 … x p y p bx by p は多角形の頂点数を表す。( x i , y i ) と ( x i+1 , y i+1 ) の頂点を繋いだ線分が多角形の一辺である。ただし、 p 番目の頂点は1番目の頂点と繋がる。 ( bx , by ) は多角形の内部にあるボタンの位置を表す。多角形は反時計回りで与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ n ≤ 15 1 ≤ m ≤ n 3 ≤ p ≤ 20 0 ≤ x , y , bx , by ≤ 1000 多角形は反時計回りで与えられる 多角形が別の多角形を内包するようなことはない 多角形はその内部 ( 多角形の辺は含まない ) にちょうど1つだけボタンを持つ 任意の異なる2つの多角形は共通な面積を持たない 任意の異なる2つの多角形 a , b において、 a と b の接点は高々1つしかない 任意の異なる2つの多角形 a , b において、直接または1つ以上の多角形を経由することで a から b へ移動することができる Output ゆう君が初期位置から全ての道を通って再度初期位置に戻ってくるまでにかかる移動の距離の総和の最小値を出力せよ。小数点以下は何桁数字を出力しても構わない。ただし、解答の誤差は0.00001(10 -5 )を超えてはならない。 Sample Input1 3 1 4 0 4 2 4 2 7 0 7 1 6 5 1 0 6 0 4 5 2 2 1 4 4 2 3 2 6 6 4 6 7 5 6 Sample Output1 14.6502815399 Sample Input2 3 2 3 0 2 3 5 0 5 1 4 3 1 0 4 0 1 3 2 1 3 2 2 4 3 2 4 3 3 Sample Output2 8.0644951022 Sample Input2 4 4 3 0 5 3 8 0 8 1 7 3 1 3 4 3 1 6 2 4 5 2 0 7 0 7 2 3 2 2 3 6 1 3 6 2 7 7 2 7 6 6 Sample Output2 18.9356648257
[ { "submission_id": "aoj_1557_3561159", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double PI = acos(-1);\nconst double EPS = 1e-7; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n#define LE(n,m) ((n) - (m) < EPS)\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\n// !!! 誤差に注意 !!! (掛け算したものとかなり小さいものを比べているので)\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 線分と線分\nbool isecSS(Point a1, Point a2, Point b1, Point b2) {\n return ccw(a1, a2, b1)*ccw(a1, a2, b2) <= 0 &&\n ccw(b1, b2, a1)*ccw(b1, b2, a2) <= 0;\n}\n\n// 線分と点\nbool isecSP(Point a1, Point a2, Point b) {\n return !ccw(a1, a2, b);\n}\n\n// 2直線の交点\nPoint crosspointLL(Point a1, Point a2, Point b1, Point b2) {\n double d1 = cross(b2-b1, b1-a1);\n double d2 = cross(b2-b1, a2-a1);\n if (EQ(d1, 0) && EQ(d2, 0)) return a1; // same line\n assert(!EQ(d2, 0)); // 交点がない\n return a1 + d1/d2 * (a2-a1);\n}\n\n// 多角形の内部判定\n// 点が領域内部なら1、境界上なら2、外部なら0を返す\nint inPolygon(Point p, const VP& ps) {\n int n = ps.size();\n bool in = false;\n rep (i, n) {\n Point a = ps[i] - p;\n Point b = ps[(i + 1) % n] - p;\n if (EQ(cross(a,b), 0) && LE(dot(a,b), 0)) return 2;\n if (a.Y > b.Y) swap(a,b);\n if ((a.Y*b.Y < 0 || (a.Y*b.Y < EPS && b.Y > EPS)) && LE(cross(a, b), 0)) in = !in;\n }\n return in;\n}\n\nPoint READ(){\n int x,y;\n cin >>x >>y;\n return Point(x,y);\n}\n\nint main(){\n int n,m;\n cin >>n >>m;\n\n vector<VP> room(n);\n vector<int> sz(n);\n VP b(n);\n rep(i,n){\n cin >>sz[i];\n rep(j,sz[i]) room[i].pb(READ());\n b[i] = READ();\n }\n\n vector<int> deg(n);\n vector<vector<bool>> touch(n,(vector<bool>(n)));\n vector<VP> cp(n, VP(n));\n\n // iとjが接する場所\n rep(i,n)rep(j,i){\n bool tmp = false;\n Point pp;\n for(Point p:room[i]){\n rep(k,sz[j])if(isecSP(room[j][k], room[j][(k+1)%sz[j]], p)){\n tmp = true;\n pp = p;\n }\n }\n for(Point p:room[j]){\n rep(k,sz[i])if(isecSP(room[i][k], room[i][(k+1)%sz[i]], p)){\n tmp = true;\n pp = p;\n }\n }\n\n if(tmp){\n touch[i][j] = touch[j][i] = true;\n ++deg[i];\n ++deg[j];\n cp[i][j] = cp[j][i] = pp;\n }\n }\n\n vector<vector<double>> d(n, vector<double>(n,INF));\n rep(i,n){\n VP ps;\n ps.pb(b[i]);\n rep(j,sz[i]) ps.pb(room[i][j]);\n\n int V = sz[i]+1;\n vector<int> idx(n,-1);\n rep(j,n)if(touch[i][j]){\n ps.pb(cp[i][j]);\n idx[j] = V++;\n }\n\n vector<double> dist(V,INF);\n dist[0] = 0;\n queue<int> que({0});\n while(!que.empty()){\n int a = que.front();\n que.pop();\n rep(j,V)if(j!=a){\n bool can_move = true;\n rep(k,sz[i]){\n Point p1 = room[i][k], p2 = room[i][(k+1)%sz[i]];\n if(isecSS(ps[a],ps[j],p1,p2)){\n Point cand = crosspointLL(ps[a],ps[j],p1,p2);\n bool edge = false;\n for(Point ep:{ps[a],ps[j]}){\n if(abs(ep-cand) < EPS) edge = true;\n }\n\n if(!edge){\n can_move = false;\n break;\n }\n }\n }\n if(inPolygon( (ps[a]+ps[j])*0.5, room[i] ) == 0) can_move = false;\n\n if(can_move){\n double add = abs(ps[a]-ps[j]);\n if( dist[a]+add+EPS < dist[j] ){\n dist[j] = dist[a]+add;\n que.push(j);\n }\n }\n }\n }\n\n rep(j,n)if(touch[i][j]) d[i][j] = dist[idx[j]];\n }\n\n int start = 0;\n rep(i,n)if(deg[i]%2 == 1) start |= (1<<i);\n double s_sum = 0;\n rep(i,n)rep(j,n)if(touch[i][j]) s_sum += d[i][j];\n\n vector<double> dp(1<<n,INF);\n dp[start] = s_sum;\n\n using P = pair<double, int>;\n priority_queue<P, vector<P>, greater<P>> pq;\n pq.push({s_sum, start});\n while(!pq.empty()){\n P now = pq.top();\n pq.pop();\n int mask = now.se;\n if(now.fi > dp[mask]) continue;\n\n rep(i,n)rep(j,i)if(touch[i][j]){\n int nx = mask^(1<<i)^(1<<j);\n double add = d[i][j] + d[j][i];\n if(dp[mask]+add+EPS < dp[nx]){\n dp[nx] = dp[mask]+add;\n pq.push({dp[nx], nx});\n }\n }\n }\n\n printf(\"%.10f\\n\", dp[0]);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3796, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_1557_3560924", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double PI = acos(-1);\nconst double EPS = 1e-7; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n#define LE(n,m) ((n) - (m) < EPS)\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\n// !!! 誤差に注意 !!! (掛け算したものとかなり小さいものを比べているので)\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 線分と線分\nbool isecSS(Point a1, Point a2, Point b1, Point b2) {\n return ccw(a1, a2, b1)*ccw(a1, a2, b2) <= 0 &&\n ccw(b1, b2, a1)*ccw(b1, b2, a2) <= 0;\n}\n\n\n// 線分と点\nbool isecSP(Point a1, Point a2, Point b) {\n return !ccw(a1, a2, b);\n}\n\n// 2直線の交点\nPoint crosspointLL(Point a1, Point a2, Point b1, Point b2) {\n double d1 = cross(b2-b1, b1-a1);\n double d2 = cross(b2-b1, a2-a1);\n if (EQ(d1, 0) && EQ(d2, 0)) return a1; // same line\n assert(!EQ(d2, 0)); // 交点がない\n return a1 + d1/d2 * (a2-a1);\n}\n\n// 多角形の内部判定\n// 点が領域内部なら1、境界上なら2、外部なら0を返す\nint inPolygon(Point p, const VP& ps) {\n int n = ps.size();\n bool in = false;\n rep (i, n) {\n Point a = ps[i] - p;\n Point b = ps[(i + 1) % n] - p;\n if (EQ(cross(a,b), 0) && LE(dot(a,b), 0)) return 2;\n if (a.Y > b.Y) swap(a,b);\n if ((a.Y*b.Y < 0 || (a.Y*b.Y < EPS && b.Y > EPS)) && LE(cross(a, b), 0)) in = !in;\n }\n return in;\n}\n\nPoint READ(){\n int x,y;\n cin >>x >>y;\n return Point(x,y);\n}\n\nint main(){\n int n,m;\n cin >>n >>m;\n\n vector<VP> room(n);\n vector<int> sz(n);\n VP b(n);\n rep(i,n){\n cin >>sz[i];\n rep(j,sz[i]) room[i].pb(READ());\n b[i] = READ();\n }\n\n vector<int> deg(n);\n vector<vector<bool>> touch(n,(vector<bool>(n)));\n vector<VP> cp(n, VP(n));\n\n // iとjが接する場所\n rep(i,n)rep(j,i){\n\n bool tmp = false;\n Point pp;\n for(Point p:room[i]){\n rep(k,sz[j]){\n if(isecSP(room[j][k], room[j][(k+1)%sz[j]], p)){\n tmp = true;\n pp = p;\n }\n }\n }\n for(Point p:room[j]){\n rep(k,sz[i]){\n if(isecSP(room[i][k], room[i][(k+1)%sz[i]], p)){\n tmp = true;\n pp = p;\n }\n }\n }\n\n if(tmp){\n // printf(\" TOUCH ! %d %d \",i,j); dbg(pp);\n\n touch[i][j] = touch[j][i] = true;\n ++deg[i];\n ++deg[j];\n cp[i][j] = cp[j][i] = pp;\n }\n }\n\n vector<vector<double>> d(n, vector<double>(n,INF));\n rep(i,n){\n\n VP ps;\n ps.pb(b[i]);\n rep(j,sz[i]) ps.pb(room[i][j]);\n\n int now = sz[i]+1;\n vector<int> idx(n,-1);\n rep(j,n)if(touch[i][j]){\n ps.pb(cp[i][j]);\n idx[j] = now++;\n }\n\n int V = ps.size();\n vector<double> dist(V,INF);\n dist[0] = 0;\n queue<int> que({0});\n while(!que.empty()){\n int a = que.front();\n que.pop();\n rep(j,V)if(j!=a){\n bool can_move = true;\n rep(k,sz[i]){\n Point p1 = room[i][k], p2 = room[i][(k+1)%sz[i]];\n if(isecSS(ps[a],ps[j],p1,p2)){\n Point cand = crosspointLL(ps[a],ps[j],p1,p2);\n bool edge = false;\n for(Point ep:{ps[a],ps[j]}){\n if(abs(ep-cand) < EPS) edge = true;\n }\n\n if(!edge){\n can_move = false;\n break;\n }\n }\n }\n\n if(inPolygon( (ps[a]+ps[j])*0.5, room[i] ) == 0) can_move = false;\n\n if(can_move){\n double add = abs(ps[a]-ps[j]);\n if( dist[a]+add+EPS < dist[j] ){\n // printf(\" MOVE %d -> %d :: %f\\n\",a,j,add);\n dist[j] = dist[a]+add;\n que.push(j);\n }\n }\n }\n }\n\n // dbg(dist);\n rep(j,n)if(touch[i][j]) d[i][j] = dist[idx[j]];\n }\n\n // rep(i,n){\n // rep(j,n)if(touch[i][j]){\n // printf(\" %d -> %d = %f\\n\",i,j,d[i][j]);\n // }\n // }\n\n int start = 0;\n rep(i,n)if(deg[i]%2 == 1) start |= (1<<i);\n\n double s_sum = 0;\n rep(i,n)rep(j,n)if(touch[i][j]) s_sum += d[i][j];\n\n vector<double> dp(1<<n,INF);\n dp[start] = s_sum;\n\n using P = pair<double, int>;\n priority_queue<P, vector<P>, greater<P>> pq;\n pq.push({s_sum, start});\n while(!pq.empty()){\n P now = pq.top();\n pq.pop();\n int mask = now.se;\n if(now.fi > dp[mask]) continue;\n\n rep(i,n)rep(j,i)if(touch[i][j]){\n int nx = mask;\n nx ^= (1<<i);\n nx ^= (1<<j);\n\n double add = d[i][j] + d[j][i];\n if(dp[mask]+add+EPS < dp[nx]){\n dp[nx] = dp[mask]+add;\n pq.push({dp[nx], nx});\n }\n }\n }\n\n printf(\"%.10f\\n\", dp[0]);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3756, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1557_3560914", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double PI = acos(-1);\nconst double EPS = 1e-7; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n#define LE(n,m) ((n) - (m) < EPS)\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\n// !!! 誤差に注意 !!! (掛け算したものとかなり小さいものを比べているので)\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 線分と線分\nbool isecSS(Point a1, Point a2, Point b1, Point b2) {\n return ccw(a1, a2, b1)*ccw(a1, a2, b2) <= 0 &&\n ccw(b1, b2, a1)*ccw(b1, b2, a2) <= 0;\n}\n\n\n// 線分と点\nbool isecSP(Point a1, Point a2, Point b) {\n return !ccw(a1, a2, b);\n}\n\n// 2直線の交点\nPoint crosspointLL(Point a1, Point a2, Point b1, Point b2) {\n double d1 = cross(b2-b1, b1-a1);\n double d2 = cross(b2-b1, a2-a1);\n if (EQ(d1, 0) && EQ(d2, 0)) return a1; // same line\n assert(!EQ(d2, 0)); // 交点がない\n return a1 + d1/d2 * (a2-a1);\n}\n\n// 多角形の内部判定\n// 点が領域内部なら1、境界上なら2、外部なら0を返す\nint inPolygon(Point p, const VP& ps) {\n int n = ps.size();\n bool in = false;\n rep (i, n) {\n Point a = ps[i] - p;\n Point b = ps[(i + 1) % n] - p;\n if (EQ(cross(a,b), 0) && LE(dot(a,b), 0)) return 2;\n if (a.Y > b.Y) swap(a,b);\n if ((a.Y*b.Y < 0 || (a.Y*b.Y < EPS && b.Y > EPS)) && LE(cross(a, b), 0)) in = !in;\n }\n return in;\n}\n\nPoint READ(){\n int x,y;\n cin >>x >>y;\n return Point(x,y);\n}\n\nint main(){\n int n,m;\n cin >>n >>m;\n\n vector<VP> room(n);\n vector<int> sz(n);\n VP b(n);\n rep(i,n){\n cin >>sz[i];\n rep(j,sz[i]) room[i].pb(READ());\n b[i] = READ();\n }\n\n vector<int> deg(n);\n vector<vector<bool>> touch(n,(vector<bool>(n)));\n vector<VP> cp(n, VP(n));\n\n // iとjが接する場所\n rep(i,n)rep(j,i){\n\n bool tmp = false;\n Point pp;\n for(Point p:room[i]){\n rep(k,sz[j]){\n if(isecSP(room[j][k], room[j][(k+1)%sz[j]], p)){\n tmp = true;\n pp = p;\n }\n }\n }\n for(Point p:room[j]){\n rep(k,sz[i]){\n if(isecSP(room[i][k], room[i][(k+1)%sz[i]], p)){\n tmp = true;\n pp = p;\n }\n }\n }\n\n if(tmp){\n // printf(\" TOUCH ! %d %d \",i,j); dbg(pp);\n\n touch[i][j] = touch[j][i] = true;\n ++deg[i];\n ++deg[j];\n cp[i][j] = cp[j][i] = pp;\n }\n }\n\n vector<vector<double>> d(n, vector<double>(n,INF));\n rep(i,n){\n\n VP ps;\n ps.pb(b[i]);\n rep(j,sz[i]) ps.pb(room[i][j]);\n\n int now = sz[i]+1;\n vector<int> idx(n,-1);\n rep(j,n)if(touch[i][j]){\n ps.pb(cp[i][j]);\n idx[j] = now++;\n }\n\n int V = ps.size();\n vector<double> dist(V,INF);\n dist[0] = 0;\n queue<int> que({0});\n while(!que.empty()){\n int a = que.front();\n que.pop();\n rep(j,V)if(j!=a){\n bool can_move = true;\n rep(k,sz[i]){\n Point p1 = room[i][k], p2 = room[i][(k+1)%sz[i]];\n if(isecSS(ps[a],ps[j],p1,p2)){\n Point cand = crosspointLL(ps[a],ps[j],p1,p2);\n bool edge = false;\n for(Point ep:{ps[a],ps[j]}){\n if(abs(ep-cand) < EPS) edge = true;\n }\n\n if(!edge){\n can_move = false;\n break;\n }\n }\n }\n\n if(inPolygon( (ps[a]+ps[j])*0.5, room[i] ) == 0) can_move = false;\n\n if(can_move){\n double add = abs(ps[a]-ps[j]);\n if( dist[a]+add+EPS < dist[j] ){\n // printf(\" MOVE %d -> %d :: %f\\n\",a,j,add);\n dist[j] = dist[a]+add;\n que.push(j);\n }\n }\n }\n }\n\n // dbg(dist);\n rep(j,n)if(touch[i][j]) d[i][j] = dist[idx[j]];\n }\n\n // rep(i,n){\n // rep(j,n)if(touch[i][j]){\n // printf(\" %d -> %d = %f\\n\",i,j,d[i][j]);\n // }\n // }\n\n int start = 0;\n rep(i,n)if(deg[i]%2 == 1) start |= (1<<i);\n\n double s_sum = 0;\n rep(i,n)rep(j,n)if(touch[i][j]) s_sum += d[i][j];\n\n vector<double> dp(1<<n,INF);\n dp[start] = s_sum;\n\n using P = pair<double, int>;\n priority_queue<P, vector<P>, greater<P>> pq;\n pq.push({s_sum, start});\n while(!pq.empty()){\n P now = pq.top();\n pq.pop();\n int mask = now.se;\n if(now.fi > dp[mask]) continue;\n\n rep(i,n)rep(j,n)if(touch[i][j]){\n int nx = mask;\n nx ^= (1<<i);\n nx ^= (1<<j);\n\n double add = d[i][j] + d[j][i];\n if(dp[mask]+add+EPS < dp[nx]){\n dp[nx] = dp[mask]+add;\n pq.push({dp[nx], nx});\n }\n }\n }\n\n printf(\"%.10f\\n\", dp[0]);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3796, "score_of_the_acc": -2, "final_rank": 3 } ]
aoj_1549_cpp
Problem I: Hard Beans Problem 大津大学では豆が盛んです。 N 個の豆が一直線上に並んでいます。 それぞれ0から順に N −1まで番号がふられており、 i 番目の豆の硬さを a i とします。 シアン君は理想の豆の硬さを D だと考えています。しかし、シアン君は面倒くさがりなのであまり遠くにある豆を取りに行きたくありません。したがって、シアン君は l 番目の豆から r 番目の豆の中で硬さが D に最も近い豆を知りたいと思っています。 シアン君は Q 個の質問をしてくるので、それぞれの質問に対し閉区間[ l , r ]番目にある | 豆の硬さ − D | の最小値を求めるプログラムを作成してください。(ただし、| a | は a の絶対値を表します。) Input 入力は以下の形式で与えられる。 N a 0 a 1 ... a N−1 Q l 0 r 0 D 0 l 1 r 1 D 1 . . . l Q−1 r Q−1 D Q−1 1行目に、1つの整数 N が与えられる。2行目に、 N つの整数が空白区切りで与えられる。3行目に、クエリの数が1つの整数 Q として与えられる。続く4行から3+ Q 行までにクエリの値 l , r , D が与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ N ≤ 10 5 0 ≤ |a i | ≤ 10 6 (0 ≤ i ≤ N −1) 1 ≤ Q ≤ 10 5 0 ≤ D i ≤ 10 6 0 ≤ l i ≤ r i ≤ N −1 Output 各クエリに対し、 D と[ l , r ]番目の豆の中で硬さ D に最も近い豆の硬さとの差の絶対値を1行に出力せよ。 Sample Input1 3 1 2 3 3 0 2 2 0 2 4 0 0 2 Sample Output1 0 1 1 Sample Input2 10 4 5 0 21 9 100 12 9 0 8 5 0 3 20 2 5 100 8 9 9 5 5 10 0 9 20 Sample Output2 1 0 1 90 1
[ { "submission_id": "aoj_1549_10848513", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nenum {\n NOTFOUND = 1000000000000000000LL\n};\n\nclass SuccinctBitVector {\nprivate:\n const uint64_t size; // ビットベクトルのサイズ\n static const uint64_t blockBitNum = 16;\n static const uint64_t LEVEL_L = 512;\n static const uint64_t LEVEL_S = 16;\n\n vector<uint64_t> L; // 大ブロック\n vector<uint16_t> S; // 小ブロック\n vector<uint16_t> B; // ビットベクトル\n\n uint64_t numOne = 0; // 1bitの数\n\npublic:\n explicit SuccinctBitVector(const uint64_t n) : size(n) {\n const uint64_t s = (n + blockBitNum - 1) / blockBitNum + 1; // ceil(n, blockSize)\n this->B.assign(s, 0);\n this->L.assign(n / LEVEL_L + 1, 0);\n this->S.assign(n / LEVEL_S + 1, 0);\n }\n\n // B[pos] = bit\n void setBit(const uint64_t bit, const uint64_t pos){\n assert(bit == 0 or bit == 1);\n assert(pos < this->size);\n\n const uint64_t blockPos = pos / blockBitNum;\n const uint64_t offset = pos % blockBitNum;\n if (bit == 1) { B.at(blockPos) |= (1LLU << offset); }\n else { B.at(blockPos) &= (~(1LLU << offset)); }\n }\n\n // B[pos]\n uint64_t access(const uint64_t pos) {\n assert(pos < this->size);\n const uint64_t blockPos = pos / blockBitNum;\n const uint64_t offset = pos % blockBitNum;\n return ((B.at(blockPos) >> offset) & 1);\n }\n\n void build() {\n uint64_t num = 0;\n for (uint64_t i = 0; i <= size; i++){\n if (i % LEVEL_L == 0) {\n L.at(i / LEVEL_L) = num;\n }\n if (i % LEVEL_S == 0) {\n S.at(i / LEVEL_S) = num - L.at(i / LEVEL_L);\n }\n if (i != size and i % blockBitNum == 0) {\n num += this->popCount(this->B.at(i / blockBitNum));\n }\n }\n this-> numOne = num;\n }\n\n // B[0, pos)のbitの数\n uint64_t rank(const uint64_t bit, const uint64_t pos) {\n assert(bit == 0 or bit == 1);\n assert(pos <= this->size);\n\n if (bit) {\n return L[pos / LEVEL_L] + S[pos / LEVEL_S] + popCount(B[pos / blockBitNum] & ((1 << (pos % blockBitNum)) - 1));\n }\n else {\n return pos - rank(1, pos);\n }\n }\n\n // rank番目のbitの位置 + 1(rankは1-origin)\n uint64_t select(const uint64_t bit, const uint64_t rank) {\n assert(bit == 0 or bit == 1);\n assert(rank > 0);\n if (bit == 0 and rank > this->size - this-> numOne) { return NOTFOUND; }\n if (bit == 1 and rank > this-> numOne) { return NOTFOUND; }\n\n // 大ブロックL内を検索\n uint64_t large_idx = 0;\n {\n uint64_t left = 0;\n uint64_t right = L.size();\n while (right - left > 1) {\n uint64_t mid = (left + right) / 2;\n uint64_t r = L.at(mid);\n r = (bit) ? r : mid * LEVEL_L - L.at(mid);\n\n if (r < rank) {\n left = mid;\n large_idx = mid;\n } else {\n right = mid;\n }\n }\n }\n\n // 小ブロックS内を検索\n uint64_t small_idx = (large_idx * LEVEL_L) / LEVEL_S;\n {\n uint64_t left = (large_idx * LEVEL_L) / LEVEL_S;\n uint64_t right = min(((large_idx + 1) * LEVEL_L) / LEVEL_S, (uint64_t)S.size());\n while (right - left > 1) {\n uint64_t mid = (left + right) / 2;\n uint64_t r = L.at(large_idx) + S.at(mid);\n r = (bit) ? r :mid * LEVEL_S - r;\n\n if (r < rank) {\n left = mid;\n small_idx = mid;\n } else {\n right = mid;\n }\n }\n }\n\n // Bをブロック単位で順番に探索\n uint64_t rank_pos = 0;\n {\n const uint64_t begin_block_idx = (small_idx * LEVEL_S) / blockBitNum;\n uint64_t total_bit = L.at(large_idx) + S.at(small_idx);\n if (bit == 0) {\n total_bit = small_idx * LEVEL_S - total_bit;\n }\n for (uint64_t i = 0;; ++i) {\n uint64_t b = popCount(B.at(begin_block_idx + i));\n if (bit == 0) {\n b = blockBitNum - b;\n }\n if (total_bit + b >= rank) {\n uint64_t block = (bit) ? B.at(begin_block_idx + i) : ~B.at(begin_block_idx + i);\n rank_pos = (begin_block_idx + i) * blockBitNum + selectInBlock(block, rank - total_bit);\n break;\n }\n total_bit += b;\n }\n }\n\n return rank_pos + 1;\n }\n\n uint64_t getNumOne() const {\n return numOne;\n }\n\n void debug() {\n cout << \"LEVEL_L(\" << L.size() << \")\" << endl;\n for (uint64_t i = 0 ; i < L.size(); ++i) {\n cout << L.at(i) << \", \";\n }\n cout << endl;\n cout << \"LEVEL_S(\" << S.size() << \")\" << endl;\n for (uint64_t i = 0 ; i < S.size(); ++i) {\n cout << S.at(i) << \", \";\n }\n cout << endl;\n }\n\nprivate:\n uint64_t popCount(uint64_t x) {\n x = (x & 0x5555555555555555ULL) + ((x >> 1) & 0x5555555555555555ULL);\n x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);\n x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;\n x = x + (x >> 8);\n x = x + (x >> 16);\n x = x + (x >> 32);\n return x & 0x7FLLU;\n }\n\n uint64_t selectInBlock(uint64_t x, uint64_t rank) {\n uint64_t x1 = x - ((x & 0xAAAAAAAAAAAAAAAALLU) >> 1);\n uint64_t x2 = (x1 & 0x3333333333333333LLU) + ((x1 >> 2) & 0x3333333333333333LLU);\n uint64_t x3 = (x2 + (x2 >> 4)) & 0x0F0F0F0F0F0F0F0FLLU;\n\n uint64_t pos = 0;\n for (;; pos += 8){\n uint64_t rank_next = (x3 >> pos) & 0xFFLLU;\n if (rank <= rank_next) break;\n rank -= rank_next;\n }\n\n uint64_t v2 = (x2 >> pos) & 0xFLLU;\n if (rank > v2) {\n rank -= v2;\n pos += 4;\n }\n\n uint64_t v1 = (x1 >> pos) & 0x3LLU;\n if (rank > v1){\n rank -= v1;\n pos += 2;\n }\n\n uint64_t v0 = (x >> pos) & 0x1LLU;\n if (v0 < rank){\n rank -= v0;\n pos += 1;\n }\n\n return pos;\n }\n};\n\nclass WaveletMatrix {\nprivate:\n vector<SuccinctBitVector> bit_arrays;\n vector<uint64_t> begin_one; // 各bitに着目したときの1の開始位置\n map<uint64_t, uint64_t> begin_alphabet; // 最後のソートされた配列で各文字の開始位置\n vector<vector<uint64_t>> cumulative_sum; // 各bitに着目したときの累積和\n\n uint64_t size; // 与えられた配列のサイズ\n uint64_t maximum_element; // 文字数\n uint64_t bit_size; // 文字を表すのに必要なbit数\n\npublic:\n WaveletMatrix (const vector<uint64_t> &array) {\n assert(array.size() > 0);\n size = array.size();\n maximum_element = *max_element(array.begin(), array.end()) + 1;\n bit_size = get_num_of_bit(maximum_element);\n if (bit_size == 0) {\n bit_size = 1;\n }\n\n for (uint64_t i = 0; i < bit_size; ++i) {\n SuccinctBitVector sv(size);\n bit_arrays.push_back(sv);\n }\n this->begin_one.resize(bit_size);\n this->cumulative_sum.resize(bit_size + 1, vector<uint64_t>(size + 1, 0));\n\n for (uint64_t j = 0; j < array.size(); ++j) {\n this->cumulative_sum.at(0).at(j + 1) = this->cumulative_sum.at(0).at(j) + array[j];\n }\n\n vector<uint64_t> v(array);\n for (uint64_t i = 0; i < bit_size; ++i) {\n\n vector<uint64_t> temp;\n // 0をtempにいれてく\n for (uint64_t j = 0; j < v.size(); ++j) {\n uint64_t c = v.at(j);\n uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit\n if (bit == 0) {\n temp.push_back(c);\n bit_arrays.at(i).setBit(0, j);\n }\n }\n\n this->begin_one.at(i) = temp.size();\n\n // 1をtempにいれてく\n for (uint64_t j = 0; j < v.size(); ++j) {\n uint64_t c = v.at(j);\n uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit\n if (bit == 1) {\n temp.push_back(c);\n bit_arrays.at(i).setBit(1, j);\n }\n }\n\n for (uint64_t j = 0; j < temp.size(); ++j) {\n this->cumulative_sum.at(i + 1).at(j + 1) = this->cumulative_sum.at(i + 1).at(j) + temp.at(j);\n }\n\n bit_arrays.at(i).build();\n v = temp;\n }\n\n // ソートされた配列内での各文字の位置を取得\n for (int i = v.size() - 1; i >= 0; --i) {\n this->begin_alphabet[v.at(i)] = i;\n }\n }\n\n // v[pos]\n uint64_t access(uint64_t pos) {\n if (pos >= this->size) { return NOTFOUND; }\n\n uint64_t c = 0;\n for (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n uint64_t bit = bit_arrays.at(i).access(pos); // もとの数値のi番目のbit\n c = (c <<= 1) | bit;\n pos = bit_arrays.at(i).rank(bit, pos);\n if (bit) {\n pos += this->begin_one.at(i);\n }\n }\n return c;\n }\n\n // i番目のcの位置 + 1を返す。rankは1-origin\n uint64_t select(uint64_t c, uint64_t rank) {\n assert(rank > 0);\n if (c >= maximum_element) {\n return NOTFOUND;\n }\n if (this->begin_alphabet.find(c) == this->begin_alphabet.end()) {\n return NOTFOUND;\n }\n\n uint64_t index = this->begin_alphabet.at(c) + rank;\n for (uint64_t i = 0; i < bit_arrays.size(); ++i){\n uint64_t bit = ((c >> i) & 1); // 下からi番目のbit\n if (bit == 1) {\n index -= this->begin_one.at(bit_size - i - 1);\n }\n index = this->bit_arrays.at(bit_size - i - 1).select(bit, index);\n }\n return index;\n }\n\n // v[begin_pos, end_pos)で最大値のindexを返す\n uint64_t maxRange(uint64_t begin_pos, uint64_t end_pos) {\n return quantileRange(begin_pos, end_pos, end_pos - begin_pos - 1);\n }\n\n // v[begin_pos, end_pos)で最小値のindexを返す\n uint64_t minRange(uint64_t begin_pos, uint64_t end_pos) {\n return quantileRange(begin_pos, end_pos, 0);\n }\n\n // v[begin_pos, end_pos)でk番目に小さい数値のindexを返す(kは0-origin)\n // つまり小さい順に並べてk番目の値\n uint64_t quantileRange(uint64_t begin_pos, uint64_t end_pos, uint64_t k) {\n if ((end_pos > size || begin_pos >= end_pos) || (k >= end_pos - begin_pos)) {\n return NOTFOUND;\n }\n\n uint64_t val = 0;\n for (uint64_t i = 0; i < bit_size; ++i) {\n const uint64_t num_of_zero_begin = bit_arrays.at(i).rank(0, begin_pos);\n const uint64_t num_of_zero_end = bit_arrays.at(i).rank(0, end_pos);\n const uint64_t num_of_zero = num_of_zero_end - num_of_zero_begin; // beginからendまでにある0の数\n const uint64_t bit = (k < num_of_zero) ? 0 : 1; // k番目の値の上からi番目のbitが0か1か\n\n if (bit) {\n k -= num_of_zero;\n begin_pos = this->begin_one.at(i) + begin_pos - num_of_zero_begin;\n end_pos = this->begin_one.at(i) + end_pos - num_of_zero_end;\n }\n else {\n begin_pos = num_of_zero_begin;\n end_pos = num_of_zero_begin + num_of_zero;\n }\n\n val = ((val << 1) | bit);\n }\n\n uint64_t left = 0;\n for (uint64_t i = 0; i < bit_size; ++i) {\n const uint64_t bit = (val >> (bit_size - i - 1)) & 1; // 上からi番目のbit\n left = bit_arrays.at(i).rank(bit, left); // cのi番目のbitと同じ数値の数\n if (bit) {\n left += this->begin_one.at(i);\n }\n }\n\n const uint64_t rank = begin_pos + k - left + 1;\n return select(val, rank) - 1;\n }\n\n // v[0, pos)のcの数\n uint64_t rank(uint64_t c, uint64_t pos) {\n assert(pos < size);\n if (c >= maximum_element) {\n return 0;\n }\n if (this->begin_alphabet.find(c) == this->begin_alphabet.end()) {\n return 0;\n }\n\n for (uint64_t i = 0; i < bit_size; ++i) {\n uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit\n pos = bit_arrays.at(i).rank(bit, pos); // cのi番目のbitと同じ数値の数\n if (bit) {\n pos += this->begin_one.at(i);\n }\n }\n\n uint64_t begin_pos = this->begin_alphabet.at(c);\n return pos - begin_pos;\n }\n\n // v[begin_pos, end_pos)で[min, max)に入る値の個数\n uint64_t rangeFreq(uint64_t begin_pos, uint64_t end_pos, uint64_t min_c, uint64_t max_c) {\n if ((end_pos > size || begin_pos >= end_pos) || (min_c >= max_c) || min_c >= maximum_element) {\n return 0;\n }\n\n const auto maxi_t = rankAll(max_c, begin_pos, end_pos);\n const auto mini_t = rankAll(min_c, begin_pos, end_pos);\n return get<1>(maxi_t) - get<1>(mini_t);\n }\n\n // v[0, pos)でcより小さい文字の数\n uint64_t rankLessThan(uint64_t c, uint64_t begin, uint64_t end) {\n auto t = rankAll(c, begin, end);\n return get<1>(t);\n }\n\n // v[0, pos)でcより大きい文字の数\n uint64_t rankMoreThan(uint64_t c, uint64_t begin, uint64_t end) {\n auto t = rankAll(c, begin, end);\n return get<2>(t);\n }\n\n // v[begin, end)で(cと同じ値の数、cより小さい値の数、cより大きい値の数)を求める\n tuple<uint64_t, uint64_t, uint64_t> rankAll(const uint64_t c, uint64_t begin, uint64_t end) {\n assert(end <= size);\n const uint64_t num = end - begin;\n\n if (begin >= end) {\n return make_tuple(0, 0, 0);\n }\n if (c >= maximum_element || end == 0) {\n return make_tuple(0, num, 0);\n }\n\n uint64_t rank_less_than = 0, rank_more_than = 0;\n for (size_t i = 0; i < bit_size && begin < end; ++i) {\n const uint64_t bit = (c >> (bit_size - i - 1)) & 1;\n\n const uint64_t rank0_begin = this->bit_arrays.at(i).rank(0, begin);\n const uint64_t rank0_end = this->bit_arrays.at(i).rank(0, end);\n const uint64_t rank1_begin = begin - rank0_begin;\n const uint64_t rank1_end = end - rank0_end;\n\n if (bit) {\n rank_less_than += (rank0_end - rank0_begin); // i番目のbitが0のものは除外される\n begin = this->begin_one.at(i) + rank1_begin;\n end = this->begin_one.at(i) + rank1_end;\n } else {\n rank_more_than += (rank1_end - rank1_begin); // i番目のbitが1のものは除外される\n begin = rank0_begin;\n end = rank0_end;\n }\n }\n\n const uint64_t rank = num - rank_less_than - rank_more_than;\n return make_tuple(rank, rank_less_than, rank_more_than);\n }\n\n // T[s, e)で出現回数が多い順にk個の(値,頻度)を返す\n // 頻度が同じ場合は値が小さいものが優先される\n vector<pair<uint64_t, uint64_t>> topk(uint64_t s, uint64_t e, uint64_t k) {\n assert(s < e);\n vector<pair<uint64_t, uint64_t>> result;\n\n // (頻度,深さ,値)の順でソート\n auto c = [](const tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> &l, const tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> &r) {\n // width\n if (get<0>(l) != get<0>(r)) {\n return get<0>(l) < get<0>(r);\n }\n // depth\n if (get<3>(l) != get<3>(r)) {\n return get<3>(l) > get<3>(r);\n }\n // value\n if (get<4>(l) != get<4>(r)) {\n return get<4>(l) > get<4>(r);\n }\n return true;\n };\n\n priority_queue<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>, vector<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>>, decltype(c)> que(c); // width, left, right, depth, value\n que.push(make_tuple(e - s, s, e, 0, 0));\n\n while (not que.empty()) {\n auto element = que.top(); que.pop();\n uint64_t width, left, right, depth, value;\n tie(width, left, right, depth, value) = element;\n\n if (depth >= this->bit_size) {\n result.emplace_back(make_pair(value, right - left));\n if (result.size() >= k) {\n break;\n }\n continue;\n }\n\n // 0\n const uint64_t left0 = this->bit_arrays.at(depth).rank(0, left);\n const uint64_t right0 = this->bit_arrays.at(depth).rank(0, right);\n if (left0 < right0) {\n que.push(make_tuple(right0 - left0, left0, right0, depth + 1, value));\n }\n\n // 1\n const uint64_t left1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, left);\n const uint64_t right1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, right);\n if (left1 < right1) {\n que.push(make_tuple(right1 - left1, left1, right1, depth + 1, value | (1 << (bit_size - depth - 1))));\n }\n }\n\n return result;\n };\n\n // T[begin_pos, end_pos)でx <= c < yを満たすcの和を返す\n uint64_t rangeSum(const uint64_t begin, const uint64_t end, const uint64_t x, const uint64_t y) {\n return rangeSum(begin, end, 0, 0, x, y);\n }\n\n // T[begin_pos, end_pos)でx <= c < yを満たす最大のcを返す\n uint64_t prevValue(const uint64_t begin_pos, const uint64_t end_pos, const uint64_t x, uint64_t y) {\n assert(end_pos <= size);\n const uint64_t num = end_pos - begin_pos;\n\n if (x >= y or y == 0) {\n return NOTFOUND;\n }\n if (y > maximum_element) {\n y = maximum_element;\n }\n\n if (begin_pos >= end_pos) {\n return NOTFOUND;\n }\n if (x >= maximum_element || end_pos == 0) {\n return NOTFOUND;\n }\n\n y--; // x <= c <= yにする\n\n stack<tuple<uint64_t, uint64_t, uint64_t, uint64_t, bool>> s; // (begin, end, depth, c, tight)\n s.emplace(make_tuple(begin_pos, end_pos, 0, 0, true));\n\n while (not s.empty()) {\n uint64_t b, e, depth, c;\n bool tight;\n tie(b, e, depth, c, tight) = s.top(); s.pop();\n\n if (depth == bit_size) {\n if (c >= x) {\n return c;\n }\n continue;\n }\n\n const uint64_t bit = (y >> (bit_size - depth - 1)) & 1;\n\n const uint64_t rank0_begin = this->bit_arrays.at(depth).rank(0, b);\n const uint64_t rank0_end = this->bit_arrays.at(depth).rank(0, e);\n const uint64_t rank1_begin = b - rank0_begin;\n const uint64_t rank1_end = e - rank0_end;\n\n // d番目のbitが0のものを使う\n const uint64_t b0 = rank0_begin;\n const uint64_t e0 = rank0_end;\n if (b0 != e0) { // 範囲がつぶれてない\n const uint64_t c0 = ((c << 1) | 0);\n s.emplace(make_tuple(b0, e0, depth + 1, c0, tight and bit == 0));\n }\n\n // d番目のbitが1のものを使う\n const uint64_t b1 = this->begin_one.at(depth) + rank1_begin;\n const uint64_t e1 = this->begin_one.at(depth) + rank1_end;\n if (b1 != e1) {\n if (not tight or bit == 1) {\n const auto c1 = ((c << 1) | 1);\n s.emplace(make_tuple(b1, e1, depth + 1, c1, tight));\n }\n }\n }\n\n return NOTFOUND;\n }\n\n // T[begin_pos, end_pos)でx <= c < yを満たす最小のcを返す\n uint64_t nextValue(const uint64_t begin_pos, const uint64_t end_pos, const uint64_t x, const uint64_t y) {\n assert(end_pos <= size);\n const uint64_t num = end_pos - begin_pos;\n\n if (x >= y or y == 0) {\n return NOTFOUND;\n }\n\n if (begin_pos >= end_pos) {\n return NOTFOUND;\n }\n if (x >= maximum_element || end_pos == 0) {\n return NOTFOUND;\n }\n\n stack<tuple<uint64_t, uint64_t, uint64_t, uint64_t, bool>> s; // (begin, end, depth, c, tight)\n s.emplace(make_tuple(begin_pos, end_pos, 0, 0, true));\n\n while (not s.empty()) {\n uint64_t b, e, depth, c;\n bool tight;\n tie(b, e, depth, c, tight) = s.top(); s.pop();\n\n if (depth == bit_size) {\n if (c < y) {\n return c;\n }\n continue;\n }\n\n const uint64_t bit = (x >> (bit_size - depth - 1)) & 1;\n\n const uint64_t rank0_begin = this->bit_arrays.at(depth).rank(0, b);\n const uint64_t rank0_end = this->bit_arrays.at(depth).rank(0, e);\n const uint64_t rank1_begin = b - rank0_begin;\n const uint64_t rank1_end = e - rank0_end;\n\n // d番目のbitが1のものを使う\n const uint64_t b1 = this->begin_one.at(depth) + rank1_begin;\n const uint64_t e1 = this->begin_one.at(depth) + rank1_end;\n if (b1 != e1) {\n const auto c1 = ((c << 1) | 1);\n s.emplace(make_tuple(b1, e1, depth + 1, c1, tight and bit == 1));\n }\n\n // d番目のbitが0のものを使う\n const uint64_t b0 = rank0_begin;\n const uint64_t e0 = rank0_end;\n if (b0 != e0) {\n if (not tight or bit == 0) {\n const uint64_t c0 = ((c << 1) | 0);\n s.emplace(make_tuple(b0, e0, depth + 1, c0, tight));\n }\n }\n }\n\n return NOTFOUND;\n }\n\n // T[s1, e1)とT[s2, e2)に共通して出現する要素を求める\n vector<tuple<uint64_t, uint64_t, uint64_t>> intersect(uint64_t _s1, uint64_t _e1, uint64_t _s2, uint64_t _e2) {\n assert(_s1 < _e1);\n assert(_s2 < _e2);\n\n vector<tuple<uint64_t, uint64_t, uint64_t>> intersection;\n\n queue<tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>> que; // s1, e1, s2, e2, depth, value\n que.push(make_tuple(_s1, _e1, _s2, _e2, 0, 0));\n while (not que.empty()) {\n auto e = que.front(); que.pop();\n uint64_t s1, e1, s2, e2, depth, value;\n tie(s1, e1, s2, e2, depth, value) = e;\n\n if (depth >= this->bit_size) {\n intersection.emplace_back(make_tuple(value, e1 - s1, e2 - s2));\n continue;\n }\n\n // 0\n uint64_t s1_0 = this->bit_arrays.at(depth).rank(0, s1);\n uint64_t e1_0 = this->bit_arrays.at(depth).rank(0, e1);\n uint64_t s2_0 = this->bit_arrays.at(depth).rank(0, s2);\n uint64_t e2_0 = this->bit_arrays.at(depth).rank(0, e2);\n\n if (s1_0 != e1_0 and s2_0 != e2_0) {\n que.push(make_tuple(s1_0, e1_0, s2_0, e2_0, depth + 1, value));\n }\n\n // 1\n uint64_t s1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s1);\n uint64_t e1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e1);\n uint64_t s2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s2);\n uint64_t e2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e2);\n\n if (s1_1 != e1_1 and s2_1 != e2_1) {\n que.push(make_tuple(s1_1, e1_1, s2_1, e2_1, depth + 1, value | (1 << bit_size - depth - 1)));\n }\n }\n\n return intersection;\n };\n\nprivate:\n uint64_t get_num_of_bit(uint64_t x) {\n if (x == 0) return 0;\n x--;\n uint64_t bit_num = 0;\n while (x >> bit_num) {\n ++bit_num;\n }\n return bit_num;\n }\n\n uint64_t rangeSum(const uint64_t begin, const uint64_t end, const uint64_t depth, const uint64_t c, const uint64_t x, const uint64_t y) {\n if (begin == end) {\n return 0;\n }\n\n if (depth == bit_size) {\n if (x <= c and c < y) {\n return c * (end - begin); // 値 * 頻度\n }\n return 0;\n }\n\n const uint64_t next_c = ((uint64_t)1 << (bit_size - depth - 1)) | c; // 上からdepth番目のbitを立てる\n const uint64_t all_one_c = (((uint64_t)1 << (bit_size - depth - 1)) - 1) | next_c; // depth以降のbitをたてる(これ以降全部1を選んだときの値)\n if(all_one_c < x or y <= c) {\n return 0;\n }\n\n // [begin, pos)のすべての要素は[x, y)\n if (x <= c and all_one_c < y) {\n return this->cumulative_sum.at(depth).at(end) - this->cumulative_sum.at(depth).at(begin);\n }\n\n const uint64_t rank0_begin = this->bit_arrays.at(depth).rank(0, begin);\n const uint64_t rank0_end = this->bit_arrays.at(depth).rank(0, end);\n const uint64_t rank1_begin = begin - rank0_begin;\n const uint64_t rank1_end = end - rank0_end;\n\n return rangeSum(rank0_begin, rank0_end, depth + 1, c, x, y) +\n rangeSum(this->begin_one.at(depth) + rank1_begin, this->begin_one[depth] + rank1_end, depth + 1, next_c, x, y);\n }\n};\n\nint main(){\n\tint n; cin>>n;\n\tvector<uint64_t> v(n); for(auto &x:v)cin>>x;\n\tWaveletMatrix wv(v);\n\tint q; cin>>q; while(q--){\n\t\tint l,r;uint64_t k; cin>>l>>r>>k;\n\t\tauto x=wv.prevValue(l,r+1,0,k+1);\n\t\tauto y=wv.nextValue(l,r+1,k,1000000000000000000LL);\n\t\t// cout<<x<<' '<<y<<' '<<k<<endl;\n\t\tcout<<min(llabs(k-x),llabs(y-k))<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 27480, "score_of_the_acc": -1.9523, "final_rank": 20 }, { "submission_id": "aoj_1549_10707383", "code_snippet": "#include <bits/stdc++.h>\n\n#pragma GCC optimize(\"O2\")\n\nusing namespace std;\n\n#ifdef LOCAL\n# include \"local/debug.h\"\n#else\n# define debug(...) (static_cast<void>(0))\n#endif\n\nconstexpr long long mods[2] = {998244353, 1000000007};\n\ntemplate <typename T>\nstruct WaveletMatrix {\n\n explicit WaveletMatrix(const vector<T> &data) : WaveletMatrix(data, 0, *max_element(data.begin(), data.end())) {}\n explicit WaveletMatrix(const vector<T> &data, T lower, T upper) {\n offset = -1 + lower;\n maxlog = 1 + (int) log2(shift(upper));\n build(data);\n }\n\n inline T operator[] (const int k) {\n return access(k);\n }\n\n // count occurrences of value c in [0, pos)\n int rank(T c, int pos) const {\n\n int l = 0;\n int r = pos;\n\n T cc = shift(c);\n\n for (int level = maxlog - 1; level >= 0; --level) {\n\n if (((cc >> level) & 1)) {\n l = mid[level] + (l - zeros[level][l]);\n r = mid[level] + (r - zeros[level][r]);\n } else {\n l = zeros[level][l];\n r = zeros[level][r];\n }\n }\n\n return r - l;\n }\n\n // largest element < x in [l, r)\n T upper_bound(int l, int r, T x) {\n\n int cnt = frequency_less_than(r, x) - frequency_less_than(l, x);\n\n if (cnt == 0) {\n return numeric_limits<T>::min();\n }\n\n return kth(l, r, cnt - 1);\n }\n\n // x <= smallest element in [l, r)\n T lower_bound(int l, int r, T x) {\n\n int cnt = frequency_less_than(r, x) - frequency_less_than(l, x);\n\n if (r - l <= cnt) {\n return numeric_limits<T>::max();\n }\n\n return kth(l, r, cnt);\n }\n\n // [l, r)\n inline T range_min(int l, int r) {\n return kth(l, r, 0);\n }\n\n // [l, r)\n inline T range_max(int l, int r) {\n return kth(l, r, r - l - 1);\n }\n\n // [l, r)\n inline T kth_min(int l, int r, int k) {\n return kth(l, r, k);\n }\n\n // lower <= x < upper\n inline int range_frequency(int l, int r, T lower, T upper) {\n return frequency_less_than(r, upper) - frequency_less_than(l, upper) - (frequency_less_than(r, lower) - frequency_less_than(l, lower));\n }\n\nprivate:\n int n;\n int maxlog;\n T offset;\n vector<vector<bool>> mat;\n vector<vector<int>> zeros;\n vector<int> mid;\n\n inline T adjust(T x) {\n return x + offset;\n }\n\n inline T shift(T x) {\n return x - offset;\n }\n\n void build(const vector<T> &data) {\n\n n = data.size();\n mat.assign(maxlog, vector<bool>(n));\n zeros.assign(maxlog, vector<int>(n + 1, 0));\n mid.assign(maxlog, 0);\n\n vector<T> cur(n);\n vector<T> next(n);\n for (int i = 0; i < n; i++) {\n cur[i] = shift(data[i]);\n }\n\n for (int level = maxlog - 1; -1 < level; --level) {\n\n // build bitvector and zero prefix sums\n for (int i = 0; i < n; ++i) {\n bool b = (cur[i] >> level) & 1;\n mat[level][i] = b;\n zeros[level][i + 1] = zeros[level][i] + (b == 0);\n }\n\n mid[level] = zeros[level][n];\n\n // reorder\n int p0 = 0;\n int p1 = mid[level];\n\n for (int i = 0; i < n; ++i) {\n\n if (!mat[level][i]) {\n next[p0++] = cur[i];\n } else {\n next[p1++] = cur[i];\n }\n }\n\n cur.swap(next);\n }\n\n }\n\n T access(int pos) {\n\n T x = 0;\n\n for (int level = maxlog - 1; -1 < level; --level) {\n\n if (mat[level][pos]) {\n pos = mid[level] + (pos - zeros[level][pos]);\n x |= (1LL << level);\n } else {\n pos = zeros[level][pos];\n }\n }\n\n return adjust(x);\n }\n\n // [l, r)\n T kth(int l, int r, int k) {\n\n T x = 0;\n\n for (int level = maxlog - 1; -1 < level; --level) {\n\n int cnt0 = zeros[level][r] - zeros[level][l];\n\n if (k < cnt0) {\n l = zeros[level][l];\n r = zeros[level][r];\n } else {\n x |= (1LL << level);\n k -= cnt0;\n l = mid[level] + (l - zeros[level][l]);\n r = mid[level] + (r - zeros[level][r]);\n }\n }\n\n return adjust(x);\n }\n\n // [0, pos)\n int frequency_less_than(int pos, T c) {\n\n assert(0 <= pos);\n\n int l = 0;\n int r = pos;\n int cnt = 0;\n\n T cc = shift(c);\n\n for (int level = maxlog - 1; -1 < level; --level) {\n\n if (((cc >> level) & 1)) {\n cnt += zeros[level][r] - zeros[level][l];\n l = mid[level] + (l - zeros[level][l]);\n r = mid[level] + (r - zeros[level][r]);\n } else {\n l = zeros[level][l];\n r = zeros[level][r];\n }\n }\n\n return cnt;\n }\n\n};\n\nvoid solve() {\n\n int n;\n cin >> n;\n vector<int> a(n);\n\n for (auto &v : a) {\n cin >> v;\n }\n\n int v = 1e6;\n WaveletMatrix<int> wm{a, -1 * v, v};\n int q;\n cin >> q;\n\n while (q--) {\n int l, r, d;\n cin >> l >> r >> d;\n ++r;\n\n int ans = 1 << 30;\n int left = wm.upper_bound(l, r, d);\n int right = wm.lower_bound(l, r, d);\n debug(left, right);\n\n if (left != numeric_limits<int>::min()) {\n ans = min(ans, d - left);\n }\n\n if (right != numeric_limits<int>::max()) {\n ans = min(ans, right - d);\n }\n\n cout << ans << '\\n';\n }\n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int t;\n t = 1;\n // cin >> t;\n\n while (t--) {\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 13092, "score_of_the_acc": -0.4667, "final_rank": 8 }, { "submission_id": "aoj_1549_10707372", "code_snippet": "#include <bits/stdc++.h>\n\n#pragma GCC optimize(\"O2\")\n\nusing namespace std;\n\n#ifdef LOCAL\n# include \"local/debug.h\"\n#else\n# define debug(...) (static_cast<void>(0))\n#endif\n\nconstexpr long long mods[2] = {998244353, 1000000007};\n\ntemplate <typename T>\nstruct WaveletMatrix {\n\n explicit WaveletMatrix(const vector<T> &data) : WaveletMatrix(data, 0) {}\n explicit WaveletMatrix(const vector<T> &data, T _offset) {\n offset = -1 + _offset;\n build(data);\n }\n\n inline T operator[] (const int k) {\n return access(k);\n }\n\n // count occurrences of value c in [0, pos)\n int rank(T c, int pos) const {\n\n int l = 0;\n int r = pos;\n\n T cc = shift(c);\n\n for (int level = maxlog - 1; level >= 0; --level) {\n\n if (((cc >> level) & 1)) {\n l = mid[level] + (l - zeros[level][l]);\n r = mid[level] + (r - zeros[level][r]);\n } else {\n l = zeros[level][l];\n r = zeros[level][r];\n }\n }\n\n return r - l;\n }\n\n // largest element < x in [l, r)\n T upper_bound(int l, int r, T x) {\n\n int cnt = frequency_less_than(r, x) - frequency_less_than(l, x);\n\n if (cnt == 0) {\n return numeric_limits<T>::min();\n }\n\n return kth(l, r, cnt - 1);\n }\n\n // x <= smallest element in [l, r)\n T lower_bound(int l, int r, T x) {\n\n int cnt = frequency_less_than(r, x) - frequency_less_than(l, x);\n\n if (r - l <= cnt) {\n return numeric_limits<T>::max();\n }\n\n return kth(l, r, cnt);\n }\n\n // [l, r)\n inline T range_min(int l, int r) {\n return kth(l, r, 0);\n }\n\n // [l, r)\n inline T range_max(int l, int r) {\n return kth(l, r, r - l - 1);\n }\n\n // [l, r)\n inline T kth_min(int l, int r, int k) {\n return kth(l, r, k);\n }\n\n // lower <= x < upper\n inline int range_frequency(int l, int r, T lower, T upper) {\n return frequency_less_than(r, upper) - frequency_less_than(l, upper) - (frequency_less_than(r, lower) - frequency_less_than(l, lower));\n }\n\nprivate:\n int n;\n int maxlog;\n T offset;\n vector<vector<bool>> mat;\n vector<vector<int>> zeros;\n vector<int> mid;\n\n inline T adjust(T x) {\n return x + offset;\n }\n\n inline T shift(T x) {\n return x - offset;\n }\n\n void build(const vector<T> &data) {\n\n n = data.size();\n T max_v = *max_element(data.begin(), data.end());\n\n maxlog = 1 + (int) log2(shift(max_v));\n\n mat.assign(maxlog, vector<bool>(n));\n zeros.assign(maxlog, vector<int>(n + 1, 0));\n mid.assign(maxlog, 0);\n\n vector<T> cur(n);\n vector<T> next(n);\n for (int i = 0; i < n; i++) {\n cur[i] = shift(data[i]);\n }\n\n for (int level = maxlog - 1; -1 < level; --level) {\n\n // build bitvector and zero prefix sums\n for (int i = 0; i < n; ++i) {\n bool b = (cur[i] >> level) & 1;\n mat[level][i] = b;\n zeros[level][i + 1] = zeros[level][i] + (b == 0);\n }\n\n mid[level] = zeros[level][n];\n\n // reorder\n int p0 = 0;\n int p1 = mid[level];\n\n for (int i = 0; i < n; ++i) {\n\n if (!mat[level][i]) {\n next[p0++] = cur[i];\n } else {\n next[p1++] = cur[i];\n }\n }\n\n cur.swap(next);\n }\n\n }\n\n T access(int pos) {\n\n T x = 0;\n\n for (int level = maxlog - 1; -1 < level; --level) {\n\n if (mat[level][pos]) {\n pos = mid[level] + (pos - zeros[level][pos]);\n x |= (1LL << level);\n } else {\n pos = zeros[level][pos];\n }\n }\n\n return adjust(x);\n }\n\n // [l, r)\n T kth(int l, int r, int k) {\n\n T x = 0;\n\n for (int level = maxlog - 1; -1 < level; --level) {\n\n int cnt0 = zeros[level][r] - zeros[level][l];\n\n if (k < cnt0) {\n l = zeros[level][l];\n r = zeros[level][r];\n } else {\n x |= (1LL << level);\n k -= cnt0;\n l = mid[level] + (l - zeros[level][l]);\n r = mid[level] + (r - zeros[level][r]);\n }\n }\n\n return adjust(x);\n }\n\n // [0, pos)\n int frequency_less_than(int pos, T c) {\n\n assert(0 <= pos);\n\n int l = 0;\n int r = pos;\n int cnt = 0;\n\n T cc = shift(c);\n\n for (int level = maxlog - 1; -1 < level; --level) {\n\n if (((cc >> level) & 1)) {\n cnt += zeros[level][r] - zeros[level][l];\n l = mid[level] + (l - zeros[level][l]);\n r = mid[level] + (r - zeros[level][r]);\n } else {\n l = zeros[level][l];\n r = zeros[level][r];\n }\n }\n\n return cnt;\n }\n\n};\n\nvoid solve() {\n\n int n;\n cin >> n;\n vector<int> a(n);\n\n for (auto &v : a) {\n cin >> v;\n }\n\n int v = 1e6;\n a.push_back(v + 1);\n WaveletMatrix<int> wm{a, -1 * (int) (1e6)};\n int q;\n cin >> q;\n\n while (q--) {\n int l, r, d;\n cin >> l >> r >> d;\n ++r;\n\n int ans = 1 << 30;\n int left = wm.upper_bound(l, r, d);\n int right = wm.lower_bound(l, r, d);\n debug(left, right);\n\n if (left != numeric_limits<int>::min()) {\n ans = min(ans, d - left);\n }\n\n if (right != numeric_limits<int>::max()) {\n ans = min(ans, right - d);\n }\n\n cout << ans << '\\n';\n }\n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n int t;\n t = 1;\n // cin >> t;\n\n while (t--) {\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 13164, "score_of_the_acc": -0.4698, "final_rank": 9 }, { "submission_id": "aoj_1549_10495286", "code_snippet": "#ifdef t9unkubj\n#include\"my_template.h\"\n//#include\"my_template_no_debug.h\"\n#else\n#define dbg(...) 199958\n#pragma GCC optimize(\"O3\")\n#include\"bits/stdc++.h\"\nusing namespace std;\nusing uint=unsigned;\nusing ll=long long;\nusing ull=unsigned long long;\nusing ld=long double;\nusing i128=__int128;\ntemplate<class T>using vc=vector<T>;\ntemplate<class T>using vvc=vc<vc<T>>;\ntemplate<class T>using vvvc=vc<vvc<T>>;\ntemplate<class T>using vvvvc=vc<vvvc<T>>;\ntemplate<class T>using smpq=priority_queue<T,vc<T>,greater<T>>;\ntemplate<class T>using bipq=priority_queue<T>;\nusing vi=vc<int>;\nusing vvi=vvc<int>;\nusing vl=vc<ll>;\nusing vvl=vvc<ll>;\n#define rep(i,n) for(ll i=0;i<(ll)(n);i++)\n#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)\n#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)\n#define drep(i,n) for(ll i=(ll(n)-1);i>=0;i--)\n#define all(x) x.begin(),x.end()\n#define rall(x) x.rbegin(),x.rend()\n#define pb push_back\ntemplate<class T,class F>\nbool chmin(T &x, F y){\n if(x>y){\n x=y;\n return true;\n }\n return false;\n}\ntemplate<class T, class F>\nbool chmax(T &x, F y){\n if(x<y){\n x=y;\n return true;\n }\n return false;\n}\ntemplate<class T>\nT sum(const vector<T>&v){\n return accumulate(all(v),T(0));\n}\ntemplate<class T>\nT min(const vector<T>&v){\n return *min_element(all(v));\n}\ntemplate<class T>\nT max(const vector<T>&v){\n return *max_element(all(v));\n}\nvoid YesNo(bool y){\n cout<<(y?\"Yes\":\"No\")<<endl;\n}\nvoid Yes(){\n cout<<\"Yes\"<<endl;\n}\nvoid No(){\n cout<<\"No\"<<endl;\n}\ntemplate<class T>\nvoid unique(vc<T>&a){\n a.erase(unique(all(a)),a.end());\n}\nvvi readgraph(int n,int m,int off = -1){\n vvi g(n);\n rep(i, m){\n int u,v;\n cin>>u>>v;\n u+=off,v+=off;\n g[u].push_back(v);\n g[v].push_back(u);\n }\n return g;\n}\nvvi readtree(int n,int off=-1){\n return readgraph(n,n-1,off);\n}\ntemplate<class T>\nvc<T> presum(vc<T> &a){\n vc<T> ret(a.size()+1);\n rep(i,a.size())ret[i+1]=ret[i]+a[i];\n return ret;\n}\ntemplate<class T, class F>\nvc<T> &operator+=(vc<T> &a,F b){\n for (auto&v:a)v += b;\n return a;\n}\ntemplate<class T, class F>\nvc<T> &operator-=(vc<T>&a,F b){\n for (auto&v:a)v-=b;\n return a;\n}\nostream&operator<<(ostream&os,i128 num) {\n if(num==0){\n os<<\"0\";\n return os;\n }\n if(num<0){\n os<<\"-\";\n num=-num;\n }\n string res;\n while(num){\n res+='0'+static_cast<int>(num%10);\n num/=10;\n }\n reverse(all(res));\n os<<res;\n return os;\n}\n\nistream&operator>>(istream& is,i128&num) {\n string s;\n is>>s;\n bool neg=0;\n num=0;\n if(s[0]=='-'){\n neg=1;\n s=s.substr(1);\n }\n for(auto&c:s){\n if (c>='0'&&c<='9'){\n num=num*10+(c-'0');\n }else{\n is.setstate(ios::failbit);\n return is;\n }\n }\n if(neg){\n num=-num;\n }\n return is;\n}\n\nvoid scan(int&a) { cin >> a; }\nvoid scan(ll&a) { cin >> a; }\nvoid scan(string&a) { cin >> a; }\nvoid scan(char&a) { cin >> a; }\nvoid scan(uint&a) { cin >> a; }\nvoid scan(ull&a) { cin >> a; }\nvoid scan(bool&a) { cin >> a; }\nvoid scan(ld&a){ cin>> a; }\nvoid scan(i128&a){ cin>> a; }\ntemplate<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }\nvoid read() {}\ntemplate<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }\n#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);\n#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);\n#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);\n#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);\n#define CHR(...) char __VA_ARGS__; read(__VA_ARGS__);\n#define DBL(...) double __VA_ARGS__; read(__VA_ARGS__);\n#define LD(...) ld __VA_ARGS__; read(__VA_ARGS__);\n#define I128(...) i128 __VA_ARGS__; read(__VA_ARGS__);\n#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);\n#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);\n\nvoid print(int a) { cout << a; }\nvoid print(ll a) { cout << a; }\nvoid print(string a) { cout << a; }\nvoid print(char a) { cout << a; }\nvoid print(uint a) { cout << a; }\nvoid print(bool a) { cout << a; }\nvoid print(ull a) { cout << a; }\nvoid print(double a) { cout << a; }\nvoid print(ld a){ cout<< a; }\nvoid print(i128 a){ cout<< a; }\ntemplate<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<\" \";print(a[i]);}cout<<endl;}\nvoid PRT() { cout <<endl; return ; }\ntemplate<class T> void PRT(T a) { print(a); cout <<endl; return; }\ntemplate<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << \" \"; PRT(tail...); return; }\nstruct ioset{\n ioset(){\n cin.tie(0)->sync_with_stdio(0);\n #ifdef t9unkubj\n cout<<fixed<<setprecision(6);\n #else\n cout<<fixed<<setprecision(20);\n #endif\n }\n}ioset_______;\nstruct MY_TIMER{\n double start_time;\n MY_TIMER(){\n start_time=clock();\n }\n double out(){\n return (clock()-start_time)/double(CLOCKS_PER_SEC);\n }\n}TIMER;\n#endif\n#include<bits/stdc++.h>\nusing namespace std;\nstruct bit_vector{\n using i8=uint8_t;\n using i16=uint16_t;\n using i32=uint32_t;\n using i64=uint64_t;\n constexpr static int l=512,s=16;// l=1000 s=25程度\n std::vector<i32>L;//大ブロックのbitの数 n/l個 \n std::vector<i16>S;//小ブロックのbitの数 n/s個\n std::vector<i8>B;//ビットベクトル n個\n std::vector<i16>B2;//小ブロック地点からのbit n/s\n void build(const std::vector<int>&b){\n for(auto&x:b)assert(x<2);\n i32 N=b.size();\n B=std::vector<i8>(N);\n for(int i=0;i<N;i++){\n B[i]=b[i];\n }\n L.reserve(N/l);\n S.reserve(N/s);\n B2.reserve(N/s);\n /*i16 log_n{};\n while(1<<(log_n)<=N)log_n++;\n l=(i32)log_n*log_n;\n s=(log_n+1)/2;*/\n\n int cnt=0;\n int diff=0;\n int now_bit=0;\n for(int i=0;i<N;i++){\n if(i%l==0){\n L.emplace_back(cnt),diff=0;\n }\n if(i%s==0){\n S.emplace_back(diff);\n }\n cnt+=B[i];\n diff+=B[i];\n }\n for(int i=N-1;i>=0;i--){\n now_bit<<=1;\n now_bit|=B[i];\n if(i%s==0)B2.emplace_back(now_bit),now_bit=0;\n }\n reverse(B2.begin(),B2.end());\n }\n i32 rank1(int i){//0 indexed [0,i)のrank1を返す\n if(i<=0)return 0;\n i32 pos_l=(i+l-1)/l;//[1,n/l]\n i32 pos_s=(i+s-1)/s;//[1,n/s]\n i32 bit_pattern=B2[pos_s-1];\n i16 bit_mask=i-(pos_s-1)*s;\n bit_mask=(1<<bit_mask)-1;\n bit_pattern&=bit_mask;\n return L[pos_l-1]+S[pos_s-1]+__builtin_popcount(bit_pattern);\n }\n i32 rank1(int l,int r){//[l,r)のrank1\n return rank1(r)-rank1(l);\n }\n i32 rank0(int i){//[0,i)\n return i-rank1(i);\n }\n i32 rank0(int l,int r){\n return rank0(r)-rank0(l);\n }\n i8 access(int i){//0 indexed\n return B[i];\n }\n};\ntemplate<class T>\nstruct wavelet_matrix{\n using i8=uint8_t;\n using i16=uint16_t;\n using i32=uint32_t;\n using i64=uint64_t;\n int N;\n i16 LOG=0;\n\n std::vector<i32>zero_count;//各bitの境界,0が始まるindex\n std::vector<bit_vector>B;//各bitをbitvectorで保存したもの\n std::unordered_map<i64,i32>begin_time;\n T max_b;\n i64 RAND=0;\n void build(const vector<T>&b){\n std::random_device seed;\n while(!RAND)RAND=seed();\n if(b.size()==0)return;\n N=b.size();\n max_b=*std::max_element(b.begin(),b.end());\n while((1ll<<LOG)<=max_b)LOG++;\n zero_count.reserve(LOG),zero_count.resize(LOG);\n B.reserve(LOG),B.resize(LOG);\n std::vector<T>nb=b;\n for(int i=LOG-1;i>=0;i--){\n std::vector<T>fr,ba;\n for(int j=0;j<N;j++){\n if(~nb[j]>>i&1){\n fr.emplace_back(nb[j]);\n }\n }\n for(int j=0;j<N;j++){\n if(nb[j]>>i&1){\n ba.emplace_back(nb[j]);\n }\n }\n vector<int>tmp2(N);\n for(int j=0;j<N;j++){\n tmp2[j]=nb[j]>>i&1;\n }\n zero_count[i]=fr.size();\n B[i].build(tmp2);\n std::vector<T>tmp;\n for(auto&x:fr){\n tmp.emplace_back(x);\n }\n for(auto&x:ba){\n tmp.emplace_back(x);\n }\n nb=move(tmp);\n }\n for(int j=N-1;j>=0;j--){\n begin_time[nb[j]^RAND]=j;\n }\n }\n T access(int i){//0 indexed b[i]を復元する\n assert(B.size());\n T now=0;\n for(int j=LOG-1;j>=0;j--){\n i64 bit=B[j].access(i);\n now+=(bit<<j);\n if(bit){\n //i=B[j].rank0(N)+B[j].rank1(i+1)-1;\n i=zero_count[j]+B[j].rank1(i+1)-1;\n }\n else{ \n i=B[j].rank0(i+1)-1;\n }\n }\n return now;\n }\n //verifyed with https://judge.yosupo.jp/submission/200516\n int rank(int i,T x){//0 indexed [0,i]までにxが何回出現したかを返す\n if(begin_time.find(x^RAND)==begin_time.end())return 0;\n if(i<0)return 0;\n for(int j=LOG-1;j>=0;j--){\n if(x>>j&1){\n //i=B[j].rank0(N)+B[j].rank1(i+1)-1;\n i=zero_count[j]+B[j].rank1(i+1)-1;\n }\n else{\n i=B[j].rank0(i+1)-1;\n }\n }\n return i-begin_time[x^RAND]+1;\n }\n int rank(int l,int r,int x){//[l,r) rank x\n return rank(r-1,x)-rank(l-1,x);\n }\n //\n //NO veryfied!!!!!!!!!!!!!!!!!!\n int select(int i,T x){//i番目にx(0 indexed)が出現する場所を返す //NO veryfied!!!!!!!!!!!!!!!!!!\n int now=begin_time[x^RAND]+i;\n for(int i=0;i<LOG;i++){\n if(x>>i&1){\n //now=now-B[i].rank0(N-1)-1;\n now=now-zero_count[i]-1;\n }\n else{\n now=B[i].rank0(now)-1;\n }\n }\n return now;\n }\n //NO veryfied!!!!!!!!!!!!!!!!!!\n //\n //verifyed with https://judge.yosupo.jp/submission/200539\n T quantile(int l,int r,int x){//[l,r)の中でx番目(0 indexed)に小さい値を返す(sortしてx番目のものを返すともいえる)\n T res=0;\n for(int i=LOG-1;i>=0;i--){\n int l0=B[i].rank0(l,r),l1=B[i].rank1(l,r);\n if(x<l0){\n int t1=B[i].rank0(0,l);\n l=t1,r=t1+l0;\n }\n else{\n int t1=B[i].rank1(0,l);\n //int t3=B[i].rank0(0,N);\n int t3=zero_count[i];\n l=(t3)+t1,r=(t3)+t1+l1;\n x-=l0;\n res|=(1ll<<i);\n }\n }\n return res;\n }\n //[0,x)\n //[l,r]\n int range_freq(int l,int r,T x){\n if(x>=(T(1)<<LOG)){\n return r-l;\n }\n int res=0;\n for(int i=LOG-1;i>=0;i--){\n bool f=(x>>i&1);\n i32 l0=B[i].rank0(l),r0=B[i].rank0(r);\n if(f){\n res+=r0-l0;\n l+=B[i].rank0(N)-l0;\n r+=B[i].rank0(N)-r0;\n }else{\n l=l0;\n r=r0;\n }\n }\n return res;\n }\n int range_freq(int l,int r,T sl,T sr){\n return range_freq(l,r,sr)-range_freq(l,r,sl);\n }\n //return max value s.t. (l<=i<r&&a[i]<x)\n T prev(int l,int r,T x){\n int cnt=range_freq(l,r,x);\n if(cnt==0)return -1;\n return quantile(l,r,cnt-1);\n }\n //return min value s.t. (l<=i<r&&a[i]>x)\n T next(int l,int r,T x){\n int cnt=range_freq(l,r,x);\n if(cnt==r-l)return -1;\n return quantile(l,r,cnt);\n }\n};\nvoid solve(){\n INT(n)\n VC(int,as,n);\n rep(i,n)as[i]+=(int)1e6;\n wavelet_matrix<int>wm;\n wm.build(as);\n INT(q);\n while(q--){\n INT(l,r,d);\n ++r;\n d+=(int)1e6;\n int ans=2e6;\n if(wm.rank(d,l-1)<wm.rank(d,r-1)){\n ans=0;\n }else{\n int suc=wm.next(l,r,d);\n if(~suc)chmin(ans,abs(suc-d));\n int pre=wm.prev(l,r,d);\n if(~pre)chmin(ans,abs(pre-d));\n }\n cout<<ans<<\"\\n\";\n }\n}\nsigned main(){\n int t=1;\n //cin>>t;\n while(t--)solve();\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 10476, "score_of_the_acc": -0.7172, "final_rank": 12 }, { "submission_id": "aoj_1549_10287556", "code_snippet": "#ifndef ELSIE_LOCAL_H\n#define ELSIE_LOCAL_H\n#ifndef ELSIE_MISC_HEADER\n#define ELSIE_MISC_HEADER\n#if __has_include(<atcoder/modint>)\n#include \"atcoder/modint\"\nusing namespace atcoder;\nusing mint=modint998244353;\nusing mint1=modint1000000007;\n#endif\n#include <limits>\n#include <new>\n#include <initializer_list>\n#include <compare>\n#include <concepts>\n#include <utility>\n#include <bitset>\n#include <tuple>\n#include <type_traits>\n#include <functional>\n#include <chrono>\n#include <array>\n#include <deque>\n#include <list>\n#include <queue>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_map>\n#include <unordered_set>\n#include <iterator>\n#include <ranges>\n#include <algorithm>\n#include <bit>\n#include <random>\n#include <numeric>\n#include <numbers>\n#include <iostream>\n#include <ios>\n#include <streambuf>\n#include <iomanip>\n#include <sstream>\n#include <regex>\n#include <cassert>\n#include <cctype>\n#include <climits>\n#include <cmath>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdint>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\nusing namespace std;\nusing namespace chrono;\nusing std::cin;\nusing std::cout;\nusing sstream=stringstream;\n#endif\n#ifndef ELSIE_MISC_DEFINE\n#define ELSIE_MISC_DEFINE\n#define RET return\n#define fi first\n#define se second\n#define endl '\\n'\n#define sn(i,c) \" \\n\"[i==c]\n#define rsv(n) reserve(n)\n#define pf(a) push_front(a)\n#define pb(a) push_back(a)\n#define eb(...) emplace_back(__VA_ARGS__)\n#define ppf() pop_front()\n#define ppb() pop_back()\n#define pp() pop()\n#define ins(a) insert(a)\n#define emp(...) emplace(__VA_ARGS__)\n#define ers(a) erase(a)\n#define cont(a) contains(a)\n#define mp(f,s) make_pair(f,s)\n#define A(a) begin(a),end(a)\n#define I(a,i) begin(a),begin(a)+(i)\n#define _SEL4(_1,_2,_3,_4,name,...) name\n#define _SEL3(_1,_2,_3,name,...) name\n#define _REP4(i,s,n,st) for(long i=(s);i<(n);i+=(st))\n#define _REP3(i,s,n) _REP4(i,s,n,1)\n#define _REP2(i,n) _REP3(i,0,n)\n#define _REP1(n) _REP2(_,n)\n#define _RREP4(i,n,t,s) for(long i=(n);i>=(t);i-=(s))\n#define _RREP3(i,n,t) _RREP4(i,n,t,1)\n#define _RREP2(i,n) _RREP3(i,n,0)\n#define _ITER2(x,a) for(auto&&x:a)\n#define _ITER3(x,y,a) for(auto&&[x,y]:a)\n#define _CTER2(x,a) for(const auto&x:a)\n#define _CTER3(x,y,a) for(const auto&[x,y]:a)\n#define rep(...) _SEL4(__VA_ARGS__,_REP4,_REP3,_REP2,_REP1)(__VA_ARGS__)\n#define rrep(...) _SEL4(__VA_ARGS__,_RREP4,_RREP3,_RREP2,_REP1)(__VA_ARGS__)\n#define iter(...) _SEL3(__VA_ARGS__,_ITER3,_ITER2)(__VA_ARGS__)\n#define cter(...) _SEL3(__VA_ARGS__,_CTER3,_CTER2)(__VA_ARGS__)\n#endif\n#ifndef ELSIE_MISC_CONST\n#define ELSIE_MISC_CONST\n#define NL cout<<'\\n'\n#define npi numbers::pi\nconstexpr int64_t inf=1ll<<60,minf=-inf;\nconstexpr int32_t inf32=1ll<<30,minf32=-inf32;\nconstexpr char sep='\\n';\nconstexpr array<pair<int32_t,int32_t>,8>dc={{{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}};\n#define yes cout<<\"Yes\\n\"\n#define no cout<<\"No\\n\"\n#define yn(c) (c)?yes:no\n#endif\n#if __cplusplus > 202002L\n#ifndef ELSIE_MISC_FUNC\n#define ELSIE_MISC_FUNC\n\nnamespace vies=std::views;\n#define DR(i) views::drop(i)\n#define TK(i) views::take(i)\n#define RV views::reverse\n#define IOTA vies::iota\n\n#define rev(a) reverse(A(a))\n#define minel(a) min_element(A(a))\n#define maxel(a) max_element(A(a))\n#define acm(a) accumulate(A(a),0ll)\n#define nxpm(a) next_permutation(A(a))\n#define Sort(a) sort(A(a))\n#define uni(a) Sort(a);a.erase(unique(A(a)),a.end())\n#define swapcase(a) a=(isalpha(a)?a^32:a)\n\ntemplate<class T,class U>inline void chmax(T&a,const U&b){if(a<b)a=b;}\ntemplate<class T,class U>inline void chmin(T&a,const U&b){if(a>b)a=b;}\n\n\n#define _BISECT4(_1,_2,_3,_4,name,...) name\n#define _LB_BEX(b,e,x) lower_bound(b,e,x)\n#define _LB_BEXG(b,e,x,g) lower_bound(b,e,x,g)\n#define _UB_BEX(b,e,x) upper_bound(b,e,x)\n#define _UB_BEXG(b,e,x,g) upper_bound(b,e,x,g)\n#define lb(...) _BISECT4(__VA_ARGS__,_LB_BEXG,_LB_BEX)(__VA_ARGS__)\n#define ub(...) _BISECT4(__VA_ARGS__,_UB_BEXG,_UB_BEX)(__VA_ARGS__)\n\n#define TP template<class T,class U,typename cp=less<U>>\ntemplate<class T,class U>concept LUBI= same_as<T,vector<U>>||same_as<T,deque<U>>||is_array_v<T>;\n#define RL requires LUBI<T,U>\n\nTP size_t lbi(const T&v,const U&x,cp cmp=cp())RL{RET lb(A(v),x,cmp)-begin(v);}\nTP size_t ubi(const T&v,const U&x,cp cmp=cp())RL{RET ub(A(v),x,cmp)-begin(v);}\nTP size_t lbi(size_t i,const T&v,const U&x,cp cmp=cp())RL{RET lb(i+A(v),x,cmp)-begin(v);}\nTP size_t ubi(size_t i,const T&v,const U&x,cp cmp=cp())RL{RET ub(i+A(v),x,cmp)-begin(v);}\nTP size_t lbi(const T&v,size_t i,const U&x,cp cmp=cp())RL{RET lb(I(v,i),x,cmp)-begin(v);}\nTP size_t ubi(const T&v,size_t i,const U&x,cp cmp=cp())RL{RET ub(I(v,i),x,cmp)-begin(v);}\nTP size_t lbi(size_t i,const T&v,size_t e,const U&x,cp cmp=cp())RL{RET lb(i+I(v,e),x,cmp)-begin(v);}\nTP size_t ubi(size_t i,const T&v,size_t e,const U&x,cp cmp=cp())RL{RET ub(i+I(v,e),x,cmp)-begin(v);}\n\n#undef TP\n#undef RL\n#define TP template<integral T>\n#define MT make_unsigned_t<T>\n\nTP constexpr int32_t pcnt(T p){return popcount(MT(p));}\nTP constexpr int32_t lsb(T p){return countr_zero(MT(p));}\nTP constexpr int32_t msb(T p){return sizeof(T)*8-1-countl_zero(MT(p));}\n\ntemplate<int32_t N,integral T>\nvoid putbit(T s){\n char buf[N+1]={0};\n for(char*itr=buf+N-1;itr>=buf;itr--,s>>=1)\n *itr='0'+(s&1);\n cout<<buf<<sep;\n}\n#undef TP\n#undef MT\n#endif\n#ifndef ELSIE_STD_IO\n#define ELSIE_STD_IO\n\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)\n#define STR(...) str __VA_ARGS__;in(__VA_ARGS__)\n#define VI(a,n) vi a(n);in(a)\n#define VIT(a,n) vit a(n);in(a)\n#define VS(a,n) vs a(n);in(a)\n#define UV(u,v) INT(u,v);--u,--v\n#define UVW(u,v,w) INT(u,v,w);--u,--v\n#ifdef LOCAL\n#define dput(...) dos=&cerr;out(__VA_ARGS__);dos=&cout\n#else\n#define dput(...)\n#endif\n\ntemplate<class T>concept Lint=is_integral_v<T>&&sizeof(T)>8;\ntemplate<class T>concept Itrabl=requires(const T&x){x.begin();x.end();}&&!std::is_same_v<T,string>;\ntemplate<class T>concept IItrabl=Itrabl<T>&&Itrabl<typename T::value_type>;\ntemplate<class T>concept ModInt=requires(const T&x){x.val();};\ntemplate<class T>concept NLobj=Itrabl<T>||std::is_same_v<T,string>;\n\nistream&operator<<(istream&is,__float128&x){double y;is>>y;x=y;return is;}\nostream&operator<<(ostream&os,const __float128&x){return os<<static_cast<double>(x);}\n\ntemplate<Lint T>ostream&operator<<(ostream&dst,T val){\n ostream::sentry s(dst);\n if(!s)return dst;\n char _O128[64];\n char*d=end(_O128);\n bool vsign=val<0;\n __uint128_t v=val;\n if(vsign&&val!=numeric_limits<T>::min())v=1+~(__uint128_t)val;\n do{\n *(--d)=\"0123456789\"[v%10];\n v/=10;\n }while(v!=0);\n if(vsign)*(--d)='-';\n size_t len=end(_O128)-d;\n if(dst.rdbuf()->sputn(d,len)!=len)dst.setstate(ios_base::badbit);\n return dst;\n}\n\ntemplate<Lint T>istream&operator>>(istream&src,T&val) {\n string s;src>>s;\n bool is_neg=numeric_limits<T>::is_signed&&s.size()>0&&s[0]=='-';\n for(val=0;const auto&x:s|views::drop(is_neg))val=10*val+x-'0';\n if(is_neg)val*=-1;\n return src;\n}\n\ntemplate<ModInt T>istream&operator>>(istream&is,T&v){int64_t x;is>>x;v=x;return is;}\ntemplate<class T,class U>istream&operator>>(istream&is,pair<T,U>&v){return is>>v.first>>v.second;}\ntemplate<Itrabl T>istream&operator>>(istream&is,T&v){for(auto&&x:v)is>>x;return is;}\n\ntemplate<class T>void in(T&a){cin>>a;}\ntemplate<class T,class... Ts>void in(T&a,Ts&... b){in(a);in(b...);}\n\ntemplate<class T,class U>vector<pair<T,U>>zip(size_t n,size_t m){\n vector<pair<T,U>>r(min(n,m));\n iter(x,y,r)in(x);\n iter(x,y,r)in(y);\n return move(r);\n}\ntemplate<class T,class U>vector<pair<T,U>>zip(size_t n){return move(zip<T,U>(n,n));}\n\ntemplate<ModInt T>ostream&operator<<(ostream&os,const T&v){return os<<v.val(); }\ntemplate<class T,class U>ostream&operator<<(ostream&os,const pair<T,U>&v){return os<<'('<<v.first<<','<<v.second<<')';}\ntemplate<Itrabl T>ostream&operator<<(ostream&os,const T&v){int cnt=0;cter(x,v)os<<x<<(++cnt<v.size()?\" \":\"\");return os;}\ntemplate<IItrabl T>ostream&operator<<(ostream&os,const T&v){int cnt=0;cter(x,v)os<<x<<(++cnt<v.size()?\"\\n\":\"\");return os;}\ninline ostream*dos=&cout;\ninline int32_t OFLG; // 0:first, 1:notNLobj, 2:NLobj\ntemplate<class T>void _out(const T&a){if(OFLG)(*dos)<<\"0 \\n\"[OFLG]<<a;else(*dos)<<a;OFLG=1;}\ntemplate<NLobj T>void _out(const T&a){(*dos)<<(OFLG?\"\\n\":\"\")<<a;OFLG=2;}\ntemplate<class T,class...Ts>void _out(const T&a,const Ts&... b){_out(a);_out(b...);}\ntemplate<class... Ts>void out(const Ts&... v){OFLG=0;_out(v...);(*dos)<<sep;}\n#endif\n#endif\n#endif\n#ifndef ELSIE_STATIC_WAVELET\n#define ELSIE_STATIC_WAVELET\n#ifndef ELSIE_STATIC_BIT_VECTOR\n#define ELSIE_STATIC_BIT_VECTOR\nnamespace elsie{\n\nclass static_bit_vector{\n using u32=std::uint32_t;\n using u64=std::uint64_t;\n constexpr static u32 bsz=64;\n bool fixed;\n size_t sz;\n std::vector<u32>cnt;\n std::vector<u64>bit;\n u32 ceil(u64 a,u64 b){return(a+b-1)/b;}\n public:\n static_bit_vector():fixed(false),bit(0),cnt(0),sz(0){}\n static_bit_vector(size_t N):fixed(false),sz(N),bit(ceil(N,bsz),0),cnt(ceil(N,bsz)+1,0){}\n static_bit_vector(const static_bit_vector&)=default;\n bool get(size_t idx)const{return(bit[idx>>6]&u64(1)<<(idx&0x3F))!=0;}\n void set(size_t idx){\n assert(!fixed);\n bit[idx>>6]|=u64(1)<<(idx&0x3F);\n }\n void reset(size_t idx){\n assert(!fixed);\n bit[idx>>6]&=~(u64(1)<<(idx&0x3F));\n }\n void fix(){\n for(u32 i=1;i<cnt.size();++i)\n cnt[i]=cnt[i-1]+std::popcount(bit[i-1]);\n fixed=true;\n }\n // [0,idx)\n template<bool one=true>\n size_t rank(size_t idx)const{\n assert(fixed&&idx<=sz);\n size_t r=cnt[idx>>6]+std::popcount(bit[idx>>6]&((u64(1)<<(idx&0x3F))-1));\n if constexpr(one)\n return r;\n else return idx-r;\n }\n // [l,r)\n template<bool one=true>\n size_t rank(size_t l,size_t r)const{ return rank<one>(r)-rank<one>(l); }\n // k is 1-based index\n template<bool one=true>\n size_t select(size_t k)const{\n assert(fixed&&k);\n if(k>sz)return sz;\n int64_t ng=k-2,ok=sz;\n while(ng+1<ok){\n int64_t m=(ng+ok)>>1;\n if(rank<one>(m+1)>=k)ok=m;\n else ng=m;\n }\n return ok;\n }\n template<bool one=true>\n size_t select(size_t l,size_t k)const{\n assert(k);\n return select<one>(rank<one>(l)+k);\n }\n const static_bit_vector&operator=(const static_bit_vector&v){\n sz=v.sz;\n fixed=v.fixed;\n cnt=v.cnt;\n bit=v.bit;\n return*this;\n }\n const static_bit_vector&operator=(static_bit_vector&&v){\n sz=v.sz;\n fixed=v.fixed;\n bit=std::move(v.bit);\n cnt=std::move(v.cnt);\n return*this;\n }\n};\n}// namespace elsie\n#endif // include guard\nnamespace elsie{\nusing namespace std;\n\ntemplate<unsigned_integral type>\nclass wavelet{\n private:\n template<class T> using vc=vector<T>;\n bool fixed;\n size_t lg,sz;\n vc<type>data;\n vc<static_bit_vector>bv;\n public:\n wavelet():sz(0),fixed(0),data(0),bv(1){}\n wavelet(size_t N):sz(N),fixed(0),data(N),bv(0){}\n wavelet(vector<type>&&v):sz(v.size()),fixed(0),data(v),bv(0){}\n wavelet(const vector<type>&v):sz(v.size()),fixed(0),data(v),bv(0){}\n\n void set(size_t idx,type v){\n assert(!fixed&&idx<sz);\n data[idx]=v;\n }\n\n void fix(){\n assert(!fixed);\n if(sz==0){\n sz=1;\n data.resize(1,0);\n }\n fixed=1;\n lg=sizeof(type)*8-countl_zero(*max_element(data.begin(),data.end()));\n bv.resize(lg,static_bit_vector(sz));\n size_t s[2];\n array<vc<type>,2>b{vc<type>(sz),vc<type>(sz)};\n for(int32_t i=lg-1;i>=0;--i){\n s[0]=s[1]=0;\n type filter=type(1)<<i;\n for(size_t j=0;j<sz;++j){\n bool p=data[j]&filter;\n b[p][s[p]++]=data[j];\n if(p)bv[i].set(j);\n }\n for(size_t p=0,q=0;p<2;q+=s[p++])\n for(size_t j=0;j<s[p];++j)\n data[j+q]=b[p][j];\n bv[i].fix();\n }\n }\n\n type get(size_t idx)const{\n type r=0;\n for(int32_t i=lg-1;i>=0;--i){\n if(bv[i].get(idx)){\n r|=type(1)<<i;\n idx=bv[i].rank<0>(sz)+bv[i].rank<1>(idx);\n }else idx=bv[i].rank<0>(idx);\n }\n return r;\n }\n size_t range_freq(size_t l,size_t r,type upper)const{\n if(upper>=(type(1)<<lg))return r-l;\n size_t ret=0;\n for(int32_t h=lg-1;h>=0;--h){\n uint32_t L0=bv[h].rank<0>(l),R0=bv[h].rank<0>(r);\n if(upper>>h&1){\n size_t zeros=bv[h].rank<0>(sz);\n ret+=R0-L0;\n l+=zeros-L0;\n r+=zeros-R0;\n }else l=L0,r=R0;\n }\n return ret;\n }\n size_t range_freq(size_t l,size_t r,type lower,type upper)const{\n return range_freq(l,r,upper)-range_freq(l,r,lower);\n }\n // [0,idx)に現れるvの数\n size_t rank(size_t l,size_t r,type v)const{\n if(v==numeric_limits<type>::max())\n return sz-range_freq(l,r,v);\n else return range_freq(l,r,v,v+1);\n }\n // ord is 0-based index\n size_t select(size_t idx,type v)const{\n size_t mx=rank(0,sz,v);\n if(idx>=mx) return sz;\n size_t pos=0;\n for(int32_t h=lg-1;h>=0;--h){\n if(v>>h&1) pos=bv[h].rank<0>(sz)+bv[h].rank<1>(pos);\n else pos=bv[h].rank<0>(pos);\n }\n pos+=idx;\n for(int32_t h=0;h<lg;++h){\n if(v>>h&1) pos=bv[h].select<1>(pos-bv[h].rank<0>(sz));\n else bv[h].select<0>(pos);\n }\n return pos;\n }\n type less(size_t l,size_t r,size_t ord)const{\n type ret=0;\n for(int32_t h=lg-1;h>=0;--h){\n uint32_t L0=bv[h].rank<0>(l),R0=bv[h].rank<0>(r);\n if(ord<R0-L0) l=L0,r=R0;\n else{\n uint32_t zeros=bv[h].rank<0>(sz);\n ord-=R0-L0;\n ret|=type(1)<<h;\n l+=zeros-L0,r+=zeros-R0;\n }\n }\n return ret;\n }\n type greater(size_t l,size_t r,size_t ord)const{ return less(l,r,r-l-ord-1); }\n // i \\in [l,r), x \\in [0,upper), x=data[i]\n type prev(size_t l,size_t r,type upper)const{\n size_t cnt=range_freq(l,r,upper);\n return cnt==0?type(-1):less(l,r,cnt-1);\n }\n // i \\in [l,r), x \\in [lower,max), x=data[i]\n type next(size_t l,size_t r,type lower)const{\n size_t cnt=range_freq(l,r,lower);\n return cnt==r-l?type(-1):less(l,r,cnt);\n }\n};\n\ntemplate<signed_integral type>\nclass wavelet_s{\n private:\n using utype=make_unsigned_t<type>;\n utype pot;\n bool fixed;\n size_t sz;\n public:\n wavelet<utype>wv;\n vector<type>data;\n inline utype raise(type x)const{ return((x<0&&utype(-x)>=pot)?0:x+pot); }\n public:\n wavelet_s():pot(0),sz(0),fixed(0),data(0),wv(){}\n wavelet_s(size_t N,utype potential):pot(potential+1),sz(N),fixed(0),data(N,0),wv(N){}\n wavelet_s(vector<type>&&v,utype potential):pot(potential),sz(v.size()),fixed(0),data(v),wv(sz){}\n wavelet_s(const vector<type>&v,utype potential):pot(potential),sz(v.size()),fixed(0),data(v),wv(sz){}\n void set(size_t idx,type v){\n assert(!fixed&&idx<data.size());\n data[idx]=v;\n }\n void fix(){\n for(size_t i=0;i<data.size();++i)\n wv.set(i,data[i]+pot);\n wv.fix();\n }\n type get(size_t idx)const{ return (type)wv.get(idx)-(type)pot; }\n size_t range_freq(size_t l,size_t r,type upper)const{ return wv.range_freq(l,r,raise(upper)); }\n size_t range_freq(size_t l,size_t r,type lower,type upper)const{return wv.range_freq(l,r,raise(lower),raise(upper));}\n size_t rank(size_t l,size_t r,type v)const{ return wv.rank(l,r,raise(v)); }\n size_t select(size_t idx,type v)const{ return wv.select(idx,raise(v)); }\n type less(size_t l,size_t r,size_t ord)const{ return wv.less(l,r,ord)-pot; }\n type greater(size_t l,size_t r,size_t ord)const{ return wv.greater(l,r,ord)-pot; }\n type prev(size_t l,size_t r,type upper)const{ return wv.prev(l,r,raise(upper))-pot; }\n type next(size_t l,size_t r,type lower)const{ return wv.next(l,r,raise(lower))-pot; }\n};\n} // namespace elsie\n#endif // include guard\n#ifndef ELSIE_ALIAS\n#define ELSIE_ALIAS\n\ntemplate<class f>using vc=vector<f>;\ntemplate<class f>using vv=vc<vc<f>>;\ntemplate<class f>using v3=vv<vc<f>>;\ntemplate<class f>using v4=vv<vv<f>>;\n\ntemplate<class f>using gr=greater<f>;\ntemplate<class f>using pq=priority_queue<f>;\ntemplate<class f>using pqg=priority_queue<f, vc<f>, gr<f>>;\n#define uset unordered_set\n#define umap unordered_map\nusing it=int32_t;\nusing i8=int8_t; using u8=uint8_t;\nusing i16=int16_t; using u16=uint16_t;\nusing i32=int32_t; using u32=uint32_t;\nusing i64=int64_t; using u64=uint64_t;\nusing i128=__int128_t; using u128=__uint128_t;\nusing intw=__int128_t; using uintw=__uint128_t;\nusing f32=float;\nusing f64=double;\nusing f128=__float128;\nusing vi=vc<i64>;\nusing vit=vc<it>;\nusing vb=vc<bool>;\nusing pi=pair<i64,i64>;\nusing pit=pair<it,it>;\nusing str=string;\nusing vs=vc<str>;\nusing pqgp=pqg<pi>;\n#define int i64\n#define itn i64\n#define LL long long\n#endif\n\nvoid slv(){\n INT(n);\n VIT(a,n);\n elsie::wavelet_s<i32> wv(a,1e6+1);\n wv.fix();\n INT(q);\n rep(q){\n INT(l,r,d);\n ++r;\n out(min(abs(wv.prev(l,r,d)-d),abs(wv.next(l,r,d)-d)));\n }\n}\n\nsigned main(){\n cin.tie(0)->sync_with_stdio(0);\n cout<<fixed<<setprecision(15);\n slv();\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 5760, "score_of_the_acc": -0.0588, "final_rank": 1 }, { "submission_id": "aoj_1549_10171134", "code_snippet": "// AOJ #1549\n// Hard Beans 2025.2.1\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\t\treturn -n;\n\t}\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(int n) { // 非負整数の表示(出力)\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\t//\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\n// ----- 持続セグメント木 (Persistent Segment Tree) の実装 -----\n// 各ノードは、区間 [start, end] における出現個数を保持します。\nstruct Node {\n int left, right, count;\n};\n \n// N <= 10^5, 離散化後の値の個数 M <= 10^5, 各更新で O(log M) ノードが生成されるので\n// おおよそ 2e6~2.5e6 個のノードが必要になります。安全のため 3000000 個確保。\nstatic const int MAX_NODE = 3000000;\nNode seg[MAX_NODE];\nint nodeCount = 0;\n \n// update: 以前のバージョン prev から [start, end] 区間の pos 番目の値の出現数を 1 増加させた新ノードを返す\nint update(int prev, int start, int end, int pos) {\n int curr = ++nodeCount;\n seg[curr] = seg[prev];\n seg[curr].count = seg[prev].count + 1;\n if(start == end) return curr;\n int mid = (start + end) / 2;\n if(pos <= mid)\n seg[curr].left = update(seg[prev].left, start, mid, pos);\n else\n seg[curr].right = update(seg[prev].right, mid+1, end, pos);\n return curr;\n}\n \n// queryCount: 差分木 (バージョン u と v の差) において、離散化インデックス [L, R] に入る個数を返す\nint queryCount(int u, int v, int start, int end, int L, int R) {\n if(R < start || end < L) return 0;\n if(L <= start && end <= R)\n return seg[u].count - seg[v].count;\n int mid = (start + end) / 2;\n return queryCount(seg[u].left, seg[v].left, start, mid, L, R) +\n queryCount(seg[u].right, seg[v].right, mid+1, end, L, R);\n}\n \n// kth: 差分木 (u と v の差) から k 番目の最小の要素の離散化インデックスを返す\nint kth(int u, int v, int start, int end, int k) {\n if(start == end) return start;\n int mid = (start + end) / 2;\n int cntLeft = seg[ seg[u].left ].count - seg[ seg[v].left ].count;\n if(k <= cntLeft)\n return kth(seg[u].left, seg[v].left, start, mid, k);\n else\n return kth(seg[u].right, seg[v].right, mid+1, end, k - cntLeft);\n}\n \nint main(){\n int N = Cin();\n vector<int> beans(N);\n for (int i = 0; i < N; i++) beans[i] = Cin();\n \n vector<int> sorted = beans;\n sort(sorted.begin(), sorted.end());\n sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());\n int M = sorted.size();\n \n vector<int> root(N+1, 0);\n seg[0].left = seg[0].right = seg[0].count = 0;\n for (int i = 0; i < N; i++){\n int pos = lower_bound(sorted.begin(), sorted.end(), beans[i]) - sorted.begin();\n root[i+1] = update(root[i], 0, M-1, pos);\n }\n \n int Q = Cin();\n while(Q--){\n int l = Cin(), r = Cin(), D = Cin();\n\n int total = r - l + 1;\n\n int pos = lower_bound(sorted.begin(), sorted.end(), D) - sorted.begin();\n int rnk = (pos > 0) ? queryCount(root[r+1], root[l], 0, M-1, 0, pos-1) : 0;\n \n int ans = INT_MAX;\n if(rnk < total) {\n int idx = kth(root[r+1], root[l], 0, M-1, rnk+1);\n int candidate = sorted[idx];\n ans = min(ans, abs(candidate - D));\n }\n if(rnk > 0) {\n int idx = kth(root[r+1], root[l], 0, M-1, rnk);\n int candidate = sorted[idx];\n ans = min(ans, abs(candidate - D));\n }\n Cout(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 26696, "score_of_the_acc": -1.1002, "final_rank": 14 }, { "submission_id": "aoj_1549_10171085", "code_snippet": "// AOJ #1549\n// Hard Beans 2025.2.1\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\t\treturn -n;\n\t}\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nvoid Cout(int n) { // 非負整数の表示(出力)\n\tchar b[30];\n\tif (!n) pc('0');\n\telse {\n\t\t//\t\tif (n < 0) pc('-'), n = -n;\n\t\tint i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;\n\t\twhile (i--) pc(b[i]);\n\t}\n\tpc('\\n');\n}\n\nint main(){\n int N = Cin(); \n vector<int> beans(N);\n for (int i = 0; i < N; i++) beans[i] = Cin();\n \n int segSize = 4 * N;\n vector<vector<int>> seg(segSize);\n \n function<void(int, int, int)> build = [&](int idx, int l, int r){\n if(l == r){\n seg[idx].push_back(beans[l]);\n return;\n }\n int mid = (l + r) / 2;\n build(idx * 2, l, mid);\n build(idx * 2 + 1, mid + 1, r);\n seg[idx].resize(seg[idx * 2].size() + seg[idx * 2 + 1].size());\n merge(seg[idx * 2].begin(), seg[idx * 2].end(), \n seg[idx * 2 + 1].begin(), seg[idx * 2 + 1].end(), \n seg[idx].begin());\n };\n \n build(1, 0, N - 1);\n int Q = Cin();\n function<void(int, int, int, int, int, int, int&)> query = \n [&](int idx, int segL, int segR, int qL, int qR, int D, int &best) {\n if (segR < qL || qR < segL) return;\n if (qL <= segL && segR <= qR) {\n const vector<int>& vec = seg[idx];\n auto it = lower_bound(vec.begin(), vec.end(), D);\n if (it != vec.end()){\n best = min(best, abs(*it - D));\n }\n if (it != vec.begin()){\n best = min(best, abs(*(it - 1) - D));\n }\n return;\n }\n int mid = (segL + segR) / 2;\n query(idx * 2, segL, mid, qL, qR, D, best);\n query(idx * 2 + 1, mid + 1, segR, qL, qR, D, best);\n };\n \n for (int i = 0; i < Q; i++){\n int l = Cin(), r = Cin(), D = Cin();\n int ans = INT_MAX;\n query(1, 0, N - 1, l, r, D, ans);\n Cout(ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 24308, "score_of_the_acc": -1.0879, "final_rank": 13 }, { "submission_id": "aoj_1549_10026587", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing lint = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\nstruct BitVector {\n unsigned sz;\n unsigned blocksize;\n vector<unsigned> bit, sum;\n\n BitVector() {}\n\n BitVector(unsigned siz) {\n sz = siz;\n blocksize = (sz + 31) >> 5;\n bit.assign(blocksize, 0U);\n sum.assign(blocksize, 0U);\n }\n\n inline void set(int k) { bit[k >> 5] |= 1U << (k & 31); }\n\n inline void build() {\n sum[0] = 0U;\n for (int i = 1; i < blocksize; i++) {\n sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);\n }\n }\n\n inline bool access(unsigned k) {\n return (bool((bit[k >> 5] >> (k & 31)) & 1));\n }\n\n inline int rank(int k) {\n return (sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1)));\n }\n};\n\ntemplate <class T>\nclass WaveletMatrix {\n private:\n unsigned n;\n unsigned bitsize;\n vector<BitVector> b;\n vector<unsigned> zero;\n vector<T> cmp;\n T MI, MA;\n\n inline unsigned compress(const T &x) {\n return lower_bound(cmp.begin(), cmp.end(), x) - begin(cmp);\n }\n\n public:\n // コンストラクタ\n WaveletMatrix() {}\n WaveletMatrix(const vector<T> &v) {\n MI = numeric_limits<T>::min();\n MA = numeric_limits<T>::max();\n n = v.size();\n cmp = v;\n sort(cmp.begin(), cmp.end());\n cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());\n vector<unsigned> compressed(n);\n vector<unsigned> tmpc(n);\n unsigned size_mx = v.size();\n for (unsigned i = 0; i < n; i++) {\n compressed[i] = compress(v[i]);\n }\n bitsize = bit_width(cmp.size());\n b.resize(bitsize);\n zero.assign(bitsize, 0);\n int cur = 0;\n\n for (unsigned i = 0; i < bitsize; i++) {\n b[i] = BitVector(n + 1);\n cur = 0;\n for (unsigned j = 0; j < n; j++) {\n if (compressed[j] & (1U << (bitsize - i - 1))) {\n b[i].set(j);\n } else {\n zero[i]++;\n tmpc[cur] = compressed[j];\n cur++;\n }\n }\n b[i].build();\n\n for (unsigned j = 0; j < n; j++) {\n if (compressed[j] & (1U << (bitsize - i - 1))) {\n tmpc[cur] = compressed[j];\n cur++;\n }\n }\n swap(tmpc, compressed);\n }\n }\n\n // get v[k]\n T access(unsigned k) {\n unsigned res = 0;\n unsigned cur = k;\n for (unsigned i = 0; i < bitsize; i++) {\n if (b[i].access(cur)) {\n res |= (1U << (bitsize - i - 1));\n cur = zero[i] + b[i].rank(cur);\n } else {\n cur -= b[i].rank(cur);\n }\n }\n return cmp[res];\n }\n\n // v[l,r) の中でk番目(1-origin)に小さい値を返す\n T kth_smallest(unsigned l, unsigned r, unsigned k) {\n unsigned res = 0;\n unsigned rank1_l, rank1_r, num0;\n for (unsigned i = 0; i < bitsize; i++) {\n rank1_l = b[i].rank(l);\n rank1_r = b[i].rank(r);\n num0 = r - l - (rank1_r - rank1_l);\n if (num0 < k) {\n res |= (1U << (bitsize - i - 1));\n l = zero[i] + rank1_l;\n r = zero[i] + rank1_r;\n k -= num0;\n } else {\n l -= rank1_l;\n r -= rank1_r;\n }\n }\n return cmp[res];\n }\n\n // v[l,r) の中でk番目(1-origin)に大きい値を返す\n T kth_largest(unsigned l, unsigned r, unsigned k) {\n return kth_smallest(l, r, r - l - k + 1);\n }\n\n // v[l,r) の中で[mink,maxk)に入る値の個数を返す\n unsigned range_freq(int vl, int vr, T mink, T maxk) {\n int D = compress(mink);\n int U = compress(maxk);\n unsigned res = 0;\n auto dfs = [&](auto &rec, int d, int L, int R, int A, int B) -> void {\n if (U <= A or B <= D) return;\n if (D <= A and B <= U) {\n res += (R - L);\n return;\n }\n if (d == bitsize) {\n if (D <= A and A < U) {\n res += (R - L);\n }\n return;\n }\n int C = (A + B) / 2;\n int rank0_l = L - b[d].rank(L);\n int rank0_r = R - b[d].rank(R);\n int rank1_l = b[d].rank(L) + zero[d];\n int rank1_r = b[d].rank(R) + zero[d];\n rec(rec, d + 1, rank0_l, rank0_r, A, C);\n rec(rec, d + 1, rank1_l, rank1_r, C, B);\n };\n dfs(dfs, 0, vl, vr, 0, 1 << bitsize);\n return res;\n }\n\n // v[l,r)の中でvalを超えない最大の値を返す\n T prev_value(unsigned l, unsigned r, T val) {\n int num = range_freq(l, r, MI, val);\n if (num == 0) {\n return MA;\n } else {\n return kth_smallest(l, r, num);\n }\n }\n\n // v[l,r)の中でvalより大きい最小の値を返す\n T next_value(unsigned l, unsigned r, T val) {\n int num = range_freq(l, r, MI, val + 1);\n if (num == r - l) {\n return MI;\n } else {\n return kth_smallest(l, r, num + 1);\n }\n }\n\n bool contains(unsigned l, unsigned r, T val) {\n return range_freq(l, r, val, val + 1) > 0;\n }\n};\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n int n;\n in(n);\n vector<int> a(n);\n in(a);\n WaveletMatrix<int> wm(a);\n\n int q;\n in(q);\n rep(i, q) {\n int l, r, d;\n in(l, r, d);\n r++;\n if (wm.contains(l, r, d)) {\n cout << 0 << endl;\n } else {\n lint nex = wm.next_value(l, r, d);\n lint prev = wm.prev_value(l, r, d);\n out(min(abs(nex - d), abs(prev - d)));\n }\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 5376, "score_of_the_acc": -0.224, "final_rank": 4 }, { "submission_id": "aoj_1549_9942167", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing PI = pair<int, int>;\n\nint find(const vector<PI>& b, int x) {\n int left = -1;\n int right = (int)b.size();\n\n while (abs(right - left) > 1) {\n int mid = (right + left) / 2;\n if (b[mid].first < x) {\n left = mid;\n } else {\n right = mid;\n }\n }\n return right;\n}\nint main() {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n int n;\n cin >> n;\n vector<int> a(n);\n for (int i = 0; i < n; ++i) cin >> a[i];\n vector<PI> beans(n);\n for (int i = 0; i < n; ++i) {\n PI b;\n b.first = a[i];\n b.second = i;\n beans[i] = b;\n }\n sort(beans.begin(), beans.end());\n\n int q;\n cin >> q;\n while (q--) {\n int l, r, d;\n cin >> l >> r >> d;\n int p = find(beans, d);\n bool left = false;\n bool right = false;\n int i = p - 1;\n int j = p;\n while (i >= 0) {\n if (l <= beans[i].second && beans[i].second <= r) {\n left = true;\n break;\n }\n --i;\n }\n while (j < n) {\n if (l <= beans[j].second && beans[j].second <= r) {\n right = true;\n break;\n }\n ++j;\n }\n int ans;\n if (left && right) {\n ans = min(abs(beans[i].first - d), abs(beans[j].first - d));\n } else if (left && !right) {\n ans = abs(beans[i].first - d);\n } else if (!left && right) {\n ans = abs(beans[j].first - d);\n }\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 4400, "score_of_the_acc": -0.1818, "final_rank": 3 }, { "submission_id": "aoj_1549_9738549", "code_snippet": "#include <bits/stdc++.h>\n\n\n\nusing namespace std;\n\n//make -f ../makefile SRC=\n/*\nMerge sort tree ?\n*/\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 1000000000;\n\ntypedef vector<vector<int> > Segtree;\n\n//------------------------------------------------------------------------------\nint brute_force\n(\n int N, const vector<int>& values,\n int start, int end, int index\n)\n{\n int K = end - start + 1;\n vector<int> S(K);\n for (int k=0; k<K; ++k) S[k] = values[k+start];\n sort(S.begin(), S.end());\n return S[index];\n}\n\n//------------------------------------------------------------------------------\nvector<int> merge(const vector<int>& A, const vector<int>& B)\n{\n vector<int> L;\n int i = 0;\n int j = 0;\n while (true)\n {\n if (i >= A.size())\n {\n L.insert(L.end(), B.begin()+j, B.end());\n break;\n }\n\n if (j >= B.size())\n {\n L.insert(L.end(), A.begin()+i, A.end());\n break;\n }\n\n if (A[i] <= B[j]) { L.push_back(A[i]); i += 1; }\n else { L.push_back(B[j]); j += 1; }\n }\n\n return L;\n}\n\n//------------------------------------------------------------------------------\nvoid build(int N, const vector<int>& values, int node, int left, int right, Segtree& segtree)\n{\n if (left == right)\n {\n segtree[node] = { values[left] };\n return;\n }\n\n int mid = (left + right) / 2;\n build(N, values, 2*node+1, left, mid, segtree);\n build(N, values, 2*node+2, mid+1, right, segtree);\n\n vector<int> A = segtree[2*node+1];\n vector<int> B = segtree[2*node+2];\n segtree[node] = merge(A, B);\n}\n\n\nint query\n(\n const Segtree& segtree, int node, int left, int right,\n int start, int end, int value\n)\n{\n if (start > right || end < left) return INF;\n if (start <= left && end >= right)\n {\n auto it = lower_bound(segtree[node].begin(), segtree[node].end(), value);\n if (it == segtree[node].end()) return value - segtree[node].back();\n else if (*it == value) return 0;\n\n int diff = *it - value;\n if (it != segtree[node].begin())\n {\n it--;\n diff = min(diff, value-*it);\n }\n return diff;\n }\n int mid = (left + right)/2;\n int v1 = query(segtree, 2*node+1, left, mid, start, end, value);\n int v2 = query(segtree, 2*node+2, mid+1, right, start, end, value);\n return min(v1,v2);\n}\n\nint query\n(\n int N, const Segtree& segtree,\n int start, int end, int D\n)\n{\n return query(segtree, 0, 0, N-1, start, end, D);\n}\n//------------------------------------------------------------------------------\nvoid testcases()\n{\n srand(time(0));\n int N = 20;\n vector<int> values(N);\n for (int i=0; i<N; ++i)\n {\n int v = rand()%50+1;\n values[i] = v;\n }\n printf(\"values: \"); for (int v=0; v<N; ++v) printf(\"%d \",values[v]); printf(\"\\n\");\n\n Segtree segtree(N*4);\n build(N, values, 0, 0, N-1, segtree);\n\n int Q = 20;\n int x1, x2, k;\n for (int q=0; q<Q; ++q)\n {\n x1 = rand() % N;\n x2 = rand() % N;\n if (x2 < x1) swap(x1, x2);\n k = rand() % (x2-x1+1);\n int y = query(N, segtree, x1, x2, k);\n int y0 = brute_force(N, values, x1, x2, k);\n if (y == y0) continue;\n printf(\"%d,%d,%d: y=%d, y0=%d\\n\", x1, x2, k, y, y0);\n }\n}\n\n//------------------------------------------------------------------------------\nvoid test()\n{\n vector<int> values = {21,46,21,49,48,19,47,39,12,19,17,2,15,22,2,1,15,28,42,11};\n int N = values.size();\n Segtree segtree(N*4);\n build(N, values, 0, 0, N-1, segtree);\n\n int x1 = 0;\n int x2 = 17;\n int k = 15;\n printf(\"47 => %d\\n\", query(segtree, 0, 0, N-1, x1, x2, 47));\n printf(\"48 => %d\\n\", query(segtree, 0, 0, N-1, x1, x2, 48));\n\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //testcases(); return 0;\n //DEBUG = true;\n //test(); return 0;\n //--------------------------------------------------------------------------\n int N, Q, v, x1, x2, d, num;\n num = scanf(\"%d \", &N);\n vector<int> values(N);\n for (int i=0; i<N; ++i)\n {\n num = scanf(\"%d \", &v);\n values[i] = v;\n }\n\n Segtree segtree(N*4);\n build(N, values, 0, 0, N-1, segtree);\n //printf(\"segtree built\\n\");\n\n //--------------------------------------------------------------------------\n num = scanf(\"%d \", &Q);\n //printf(\"Q=%d\\n\", Q);\n for (int q=0; q<Q; ++q)\n {\n num = scanf(\"%d %d %d \", &x1, &x2, &d);\n //printf(\"x1=%d, x2=%d, k=%d\\n\", x1, x2, d);\n int res = query(N, segtree, x1, x2, d);\n printf(\"%d\\n\", res);\n }\n //int res = query(N, segtree, 1, 4, 2);\n //printf(\"res=%d\\n\", res);\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 150, "memory_kb": 26712, "score_of_the_acc": -1.2827, "final_rank": 15 }, { "submission_id": "aoj_1549_9175793", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define ull unsigned long long\n#define db double\n#define pii pair<int,int>\n#define pli pair<ll,int>\n#define pil pair<int,ll>\n#define pll pair<ll,ll>\n#define ti3 tuple<int,int,int>\n#define int128 __int128_t\n#define pii128 pair<int128,int128>\nconst int inf = 1 << 30;\nconst ll linf = (ll)4e18 + 10;;\nconst db EPS = 1e-10;\nconst db pi = acos(-1);\ntemplate<class T> bool chmin(T& x, T y){\n if(x > y) {\n x = y;\n return true;\n } else return false;\n}\ntemplate<class T> bool chmax(T& x, T y){\n if(x < y) {\n x = y;\n return true;\n } else return false;\n}\n\n// overload macro\n#define CAT( A, B ) A ## B\n#define SELECT( NAME, NUM ) CAT( NAME, NUM )\n\n#define GET_COUNT( _1, _2, _3, _4, _5, _6 /* ad nauseam */, COUNT, ... ) COUNT\n#define VA_SIZE( ... ) GET_COUNT( __VA_ARGS__, 6, 5, 4, 3, 2, 1 )\n\n#define VA_SELECT( NAME, ... ) SELECT( NAME, VA_SIZE(__VA_ARGS__) )(__VA_ARGS__)\n\n// rep(overload)\n#define rep( ... ) VA_SELECT(rep, __VA_ARGS__)\n#define rep2(i, n) for (int i = 0; i < int(n); i++)\n#define rep3(i, a, b) for (int i = a; i < int(b); i++)\n#define rep4(i, a, b, c) for (int i = a; i < int(b); i += c)\n\n// repll(overload)\n#define repll( ... ) VA_SELECT(repll, __VA_ARGS__)\n#define repll2(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define repll3(i, a, b) for (ll i = a; i < (ll)(b); i++)\n#define repll4(i, a, b, c) for (ll i = a; i < (ll)(b); i += c)\n\n// rrep(overload)\n#define rrep( ... ) VA_SELECT(rrep, __VA_ARGS__)\n#define rrep2(i, n) for (int i = n - 1; i >= 0; i--)\n#define rrep3(i, a, b) for (int i = b - 1; i >= a; i--)\n#define rrep4(i, a, b, c) for (int i = b - 1; i >= a; i -= c)\n\n// rrepll(overload)\n#define rrepll( ... ) VA_SELECT(rrepll, __VA_ARGS__)\n#define rrepll2(i, n) for (ll i = (ll)(n) - 1; i >= 0ll; i--)\n#define rrepll3(i, a, b) for (ll i = b - 1; i >= (ll)(a); i--)\n#define rrepll4(i, a, b, c) for (ll i = b - 1; i >= (ll)(a); i -= c)\n\n// for_earh\n#define fore(e, v) for (auto&& e : v)\n\n// vector\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n\n\nstruct bit_vector {\n int n;\n const int w = 64;\n vector<uint64_t> b;\n vector<int> cnt;\n bit_vector(int _n = 0) : n(_n), b(_n / w + 1), cnt(_n / w + 1) {}\n\n int access(int i) {\n return b[i / w] >> (i % w) & 1;\n }\n\n void set(int i) {\n b[i / w] |= 1ll << (i % w);\n }\n\n void build() {\n for (int i = 1; i < (int)cnt.size(); i++) {\n cnt[i] = cnt[i - 1] + __builtin_popcountll(b[i - 1]);\n }\n }\n\n int rank0(int i) {\n return i - rank1(i);\n }\n\n int rank1(int i) {\n return cnt[i / w] + __builtin_popcountll(b[i / w] & ((1ull << (i % w)) - 1));\n }\n};\n\ntemplate<class T>\nstruct wavelet_matrix {\n int lg;\n vector<bit_vector> bv;\n vector<int> zeros;\n\n wavelet_matrix(vector<T> a) {\n for (auto u : a) lg = max(lg, u);\n lg = log2(lg) + 1;\n bv = vector(lg, bit_vector(a.size()));\n zeros.resize(lg);\n vector<T> crt = a, nxt(a.size());\n for (int i = lg - 1; i >= 0; i--) {\n int ones = a.size() - 1;\n for (int j = 0; j < a.size(); j++) {\n if (crt[j] >> i & 1) {\n bv[i].set(j);\n nxt[ones--] = crt[j];\n } else {\n nxt[zeros[i]++] = crt[j];\n }\n }\n bv[i].build();\n reverse(nxt.begin() + ones + 1, nxt.end());\n swap(crt, nxt);\n }\n }\n\n T access(int x) {\n T ret = 0;\n for (int i = lg - 1; i >= 0; i--) {\n if (bv[i].access(x)) {\n ret |= (T)1 << i;\n x = zeros[i] + bv[i].rank1(i);\n } else {\n x = bv[i].rank0(i);\n }\n }\n }\n\n T kth_min(int l, int r, int k) {\n T ret = 0;\n for (int i = lg - 1; i >= 0; i--) {\n int l0 = bv[i].rank0(l), r0 = bv[i].rank0(r);\n if (k < r0 - l0) {\n l = l0;\n r = r0;\n } else {\n k -= r0 - l0;\n ret |= (T)1 << i;\n l += zeros[i] - l0;\n r += zeros[i] - r0;\n }\n }\n return ret;\n }\n\n T kth_max(int l, int r, int k) {\n return kth_min(l, r, r - l - k - 1);\n }\n\n T count_lt_k(int l, int r, T k) {\n int ret = 0;\n for (int i = lg - 1; i >= 0; i--) {\n int l0 = bv[i].rank0(l), r0 = bv[i].rank0(r);\n if (k >> i & 1) {\n ret += r0 - l0;\n l += zeros[i] - l0;\n r += zeros[i] - r0;\n } else {\n l = l0;\n r = r0;\n }\n }\n return ret;\n }\n\n T count_k(int l, int r, T k) {\n for (int i = lg - 1; i >= 0; i--) {\n int l0 = bv[i].rank0(l), r0 = bv[i].rank0(r);\n if (k >> i & 1) {\n l += zeros[i] - l0;\n r += zeros[i] - r0;\n } else {\n l = l0;\n r = r0;\n }\n }\n return r - l;\n }\n\n T count_gt_k(int l, int r, T k) {\n int ret = 0;\n for (int i = lg - 1; i >= 0; i--) {\n int l0 = bv[i].rank0(l), r0 = bv[i].rank0(r);\n if (k >> i & 1) {\n l += zeros[i] - l0;\n r += zeros[i] - r0;\n } else {\n ret += r - l - r0 + l0;\n l = l0;\n r = r0;\n }\n }\n return ret;\n }\n\n T range_freq(int l, int r, T lower, T upper) {\n return count_lt_k(l, r, upper) - count_lt_k(l, r, lower);\n }\n\n T next_value(int l, int r, T k) {\n int cnt = count_lt_k(l, r, k);\n if (r - l == cnt) return -1;\n else return kth_min(l, r, cnt);\n }\n\n T prev_value(int l, int r, T k) {\n int cnt = count_gt_k(l, r, k);\n if (r - l == cnt) return -1;\n else return kth_max(l, r, cnt);\n }\n};\n\nint N, Q;\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n cin >> N;\n vector<int> A(N);\n rep (i, N) cin >> A[i], A[i] += 1000000;\n A.push_back(2000000);\n wavelet_matrix wm(A);\n cin >> Q;\n rep (i, Q) {\n int l, r, x;\n cin >> l >> r >> x;\n r++;\n x += 1000000;\n int ans = inf;\n int pre = wm.prev_value(l, r, x);\n if (pre > 0) chmin(ans, x - pre);\n int nxt = wm.next_value(l, r, x);\n if (nxt > 0) chmin(ans, nxt - x);\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 5392, "score_of_the_acc": -0.452, "final_rank": 7 }, { "submission_id": "aoj_1549_9149653", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <algorithm>\n#include <set>\n#include <sstream>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <cctype>\n#include <climits>\n#include <time.h>\n#include <assert.h>\n#include <numeric>\n#include <functional>\n#include <random>\n#include <iterator>\n#include <bitset>\n#include<valarray>\n#include<fstream>\n#include<unordered_map>\n#include<unordered_set>\n#include <filesystem>\n#include <optional>\n//#include<compare>\n//#include <ranges>\n//#define AIZU\n//#define ATCODER\n\n#ifdef ATCODER\n\n#include<atcoder/all>\nusing namespace atcoder;\n#endif\nusing namespace std;\ntypedef long long ll;\n\n#define FOR(i, min, max) for (int i = (min); i < (max); ++i) \n#define FORE(i, min, max) for (int i = (min); i <= (max); ++i)\n#define DFOR(i,max,min) for(int i=(max);i>(min);--i)\n#define DFORE(i,max,min) for(int i=(max);i>=(min);--i)\n#define REP(i, n) for (int i = 0; i < (n); ++i)\n#define REPE(i, n) for (int i = 0; i <= (n); ++i)\n#define DREP(i, n) for (int i = (n); i >=0; --i) \n#define REPV(vec, i) for (int i = 0; i < (int)(vec.size()); ++i) \n#define V(type) vector<type> \n#define VV(type) vector<vector<type>>\n#define VVV(type) vector<vector<vector<type>>>\n#define VVVV(type) vector<vector<vector<vector<type>>>>\n#define VVVVV(type) vector<vector<vector<vector<vector<type>>>>>\n#define VI(type,i,n) V(type)(i,n)\n#define VVI(type,i,j,n) VV(type)(i,VI(type,j,n))\n#define VVVI(type,i,j,k,n) VVV(type)(i,VVI(type,j,k,n))\n#define VVVVI(type,i,j,k,l,n) VVVV(type)(i,VVVI(type,j,k,l,n))\n#define VVVVVI(type,i,j,k,l,m,n) VVVVV(type)(i,VVVVI(type,j,k,l,m,n))\n#define ALL(v) v.begin(),v.end()\n\n\ntemplate <typename Type>\nvoid show(vector<Type> v, string sep = \" \") {\n\tREP(i, v.size()) {\n\t\tcerr << v[i] << sep;\n\t}\n\tcerr << endl;\n}\ntemplate <typename Type>\nvoid show(VV(Type) v, string sep = \" \") {\n\tREP(i, v.size()) {\n\t\tREP(j, v[i].size()) {\n\t\t\tcerr << v[i][j] << sep;\n\t\t}\n\t\tcerr << endl;\n\t}\n\tcerr << endl;\n}\ntemplate<typename T> T maxc(T& a, const T& b) { return (a = std::max(a, b)); }\ntemplate<typename T> T minc(T& a, const T& b) { return (a = std::min(a, b)); }\n\nconst std::string WHITESPACE = \" \\n\\r\\t\\f\\v\";\nstring getS(string filename) {\n\tifstream ifs(filename);\n\tif (!ifs)return \"\";\n\tstring str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());\n\treturn str;\n}\nauto split(std::string str, const std::string cut) {\n\tstd::vector<decltype(str)> data;\n\tfor (auto pos = str.find(cut); pos != std::string::npos; pos = str.find(cut)) {\n\t\tdata.push_back(str.substr(0, pos));\n\t\tstr = str.substr(pos + cut.size());\n\t}\n\tif (!str.empty())data.push_back(str);\n\treturn data;\n}\nstd::string ltrim(const std::string& s)\n{\n\tsize_t start = s.find_first_not_of(WHITESPACE);\n\treturn (start == std::string::npos) ? \"\" : s.substr(start);\n}\n\nstd::string rtrim(const std::string& s)\n{\n\tsize_t end = s.find_last_not_of(WHITESPACE);\n\treturn (end == std::string::npos) ? \"\" : s.substr(0, end + 1);\n}\nstd::string trim(const std::string& s) {\n\treturn rtrim(ltrim(s));\n}\nbool ismatch(string trues, string anss) {\n\tvector<string> smp0 = split(trues, \"\\n\");\n\tvector<string> smp1 = split(anss, \"\\n\");\n\tvector<string> sm0;\n\tvector<string> sm1;\n\tfor (string s : smp0) {\n\t\tstring st = trim(s);\n\t\tif (st != \"\") {\n\t\t\tsm0.push_back(st);\n\t\t}\n\t}\n\tfor (string s : smp1) {\n\t\tstring st = trim(s);\n\t\tif (st != \"\") {\n\t\t\tsm1.push_back(st);\n\t\t}\n\t}\n\tif (sm0.size() != sm1.size())return false;\n\tfor (int i = 0; i < sm0.size(); i++) {\n\t\tif (sm0[i] != sm1[i]) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\ntemplate<typename T>\nvector<pair<int, T>> enumerate(vector<T>& v) {\n\tvector<pair<int, T>> ret(v.size());\n\tREP(i, v.size()) {\n\t\tret[i] = { i,v[i] };\n\t}\n\treturn ret;\n}\ntemplate<typename T0, typename T1>\nvector<pair<T0, T1>> zip(vector<T0>& v0, vector<T1>& v1) {\n\tint n = min(v0.size(), v1.size());\n\tvector<pair<T0, T1>> ret(n);\n\tREP(i, n) {\n\t\tret[i] = { v0[i],v1[i] };\n\t}\n\treturn ret;\n}\nvoid setbit(ll &i, int j) {\n\ti=i | (1LL << j);\n}\nvoid resetbit(ll &i, int j) {\n\ti=i & ~(1 << j);\n}\nbool isset(ll i, int j) {\n\treturn (i & (1LL << j)) != 0;\n}\nvoid setbit(int& i, int j) {\n\ti = i | (1LL << j);\n}\nvoid resetbit(int& i, int j) {\n\ti = i & ~(1 << j);\n}\nbool isset(int i, int j) {\n\treturn (i & (1LL << j)) != 0;\n}\n\nint bitCount(int i) {\n\ti = i - ((i >> 1) & 0x55555555);\n\ti = (i & 0x33333333) + ((i >> 2) & 0x33333333);\n\ti = (i + (i >> 4)) & 0x0f0f0f0f;\n\ti = i + (i >> 8);\n\ti = i + (i >> 16);\n\treturn i & 0x3f;\n}\nint bitCount(ll i) {\n\tint ret = 0;\n\twhile (i != 0) {\n\t\tif (i & 1)ret++;\n\t\ti = i >> 1;\n\t}\n\treturn ret;\n\n}\nvoid setbitxor(int &i, int j) {\n\ti=(i ^ (1 << j));\n}\nvoid setbitxor(ll &i, int j) {\n\ti = (i ^ (1LL << j));\n}\nint next_combination(int sub) {\n\tint x = sub & -sub, y = sub + x;\n\treturn (((sub & ~y) / x) >> 1) | y;\n}\n\ntemplate<typename T>\nbool candiv(T a, T b) {\n\treturn a % b == 0;\n}\ntemplate<typename T>\nT llceil(T a, T b) {\n\tif (a % b == 0) {\n\t\treturn a / b;\n\t}\n\tif (a >= 0) { return (a / b) + 1; }\n\telse {\n\t\treturn -((-a) / b);\n\n\t}\n}\ntemplate<typename T>\nT llfloor(T a, T b) {\n\tif (a % b == 0) {\n\t\treturn a / b;\n\t}\n\tif (a >= 0) { return (a / b); }\n\telse {\n\t\treturn -((-a) / b) - 1;\n\n\t}\n}\ntemplate<typename T>\nbool isDuplicate(vector<T> a) {\n\tsort(ALL(a));\n\treturn unique(ALL(a)) != a.end();\n}\ntemplate<typename T>\nbool isSameContents(vector<T> a, vector<T> b) {\n\tsort(ALL(a));\n\tsort(ALL(b));\n\treturn a == b;\n}\nll llpow(ll x, ll n) {\n\tlong long ret = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) ret *= x; // n の最下位bitが 1 ならば x^(2^i) をかける\n\t\tx *= x;\n\t\tn >>= 1; // n を1bit 左にずらす\n\t}\n\treturn ret;\n}\nll llpowmod(ll x, ll n, ll m) {\n\tlong long ret = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tret *= x; // n の最下位bitが 1 ならば x^(2^i) をかける\n\t\t\tret %= m;\n\t\t}\n\t\tx *= x;\n\t\tx %= m;\n\t\tn >>= 1; // n を1bit 左にずらす\n\t}\n\treturn ret;\n}\n\n//wavelet\n//\nenum {\n\tNOTFOUND = 0xFFFFFFFFFFFFFFFFLLU\n};\n\nclass SuccinctBitVector {\nprivate:\n\tconst uint64_t size; // ビットベクトルのサイズ\n\tstatic const uint64_t blockBitNum = 16;\n\tstatic const uint64_t LEVEL_L = 512;\n\tstatic const uint64_t LEVEL_S = 16;\n\n\tstd::vector<uint64_t> L; // 大ブロック\n\tstd::vector<uint16_t> S; // 小ブロック\n\tstd::vector<uint16_t> B; // ビットベクトル\n\n\tuint64_t numOne = 0; // 1bitの数\n\npublic:\n\texplicit SuccinctBitVector(const uint64_t n) : size(n) {\n\t\tconst uint64_t s = (n + blockBitNum - 1) / blockBitNum + 1; // ceil(n, blockSize)\n\t\tthis->B.assign(s, 0);\n\t\tthis->L.assign(n / LEVEL_L + 1, 0);\n\t\tthis->S.assign(n / LEVEL_S + 1, 0);\n\t}\n\n\t// B[pos] = bit\n\tvoid setBit(const uint64_t bit, const uint64_t pos) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(pos < this->size);\n\n\t\tconst uint64_t blockPos = pos / blockBitNum;\n\t\tconst uint64_t offset = pos % blockBitNum;\n\t\tif (bit == 1) { B.at(blockPos) |= (1LLU << offset); }\n\t\telse { B.at(blockPos) &= (~(1LLU << offset)); }\n\t}\n\n\t// B[pos]\n\tuint64_t access(const uint64_t pos) {\n\t\tassert(pos < this->size);\n\t\tconst uint64_t blockPos = pos / blockBitNum;\n\t\tconst uint64_t offset = pos % blockBitNum;\n\t\treturn ((B.at(blockPos) >> offset) & 1);\n\t}\n\n\tvoid build() {\n\t\tuint64_t num = 0;\n\t\tfor (uint64_t i = 0; i <= size; i++) {\n\t\t\tif (i % LEVEL_L == 0) {\n\t\t\t\tL.at(i / LEVEL_L) = num;\n\t\t\t}\n\t\t\tif (i % LEVEL_S == 0) {\n\t\t\t\tS.at(i / LEVEL_S) = (uint16_t)(num - L.at(i / LEVEL_L));\n\t\t\t}\n\t\t\tif (i != size and i % blockBitNum == 0) {\n\t\t\t\tnum += this->popCount(this->B.at(i / blockBitNum));\n\t\t\t}\n\t\t}\n\t\tthis->numOne = num;\n\t}\n\n\t// B[0, pos)のbitの数\n\tuint64_t rank(const uint64_t bit, const uint64_t pos) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(pos <= this->size);\n\n\t\tif (bit) {\n\t\t\treturn L[pos / LEVEL_L] + S[pos / LEVEL_S] + popCount(B[pos / blockBitNum] & ((1 << (pos % blockBitNum)) - 1));\n\t\t}\n\t\telse {\n\t\t\treturn pos - rank(1, pos);\n\t\t}\n\t}\n\n\t// rank番目のbitの位置 + 1(rankは1-origin)\n\tuint64_t select(const uint64_t bit, const uint64_t rank) {\n\t\tassert(bit == 0 or bit == 1);\n\t\tassert(rank > 0);\n\t\tif (bit == 0 and rank > this->size - this->numOne) { return NOTFOUND; }\n\t\tif (bit == 1 and rank > this->numOne) { return NOTFOUND; }\n\n\t\t// 大ブロックL内を検索\n\t\tuint64_t large_idx = 0;\n\t\t{\n\t\t\tuint64_t left = 0;\n\t\t\tuint64_t right = L.size();\n\t\t\twhile (right - left > 1) {\n\t\t\t\tuint64_t mid = (left + right) / 2;\n\t\t\t\tuint64_t r = L.at(mid);\n\t\t\t\tr = (bit) ? r : mid * LEVEL_L - L.at(mid);\n\n\t\t\t\tif (r < rank) {\n\t\t\t\t\tleft = mid;\n\t\t\t\t\tlarge_idx = mid;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tright = mid;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 小ブロックS内を検索\n\t\tuint64_t small_idx = (large_idx * LEVEL_L) / LEVEL_S;\n\t\t{\n\t\t\tuint64_t left = (large_idx * LEVEL_L) / LEVEL_S;\n\t\t\tuint64_t right = std::min(((large_idx + 1) * LEVEL_L) / LEVEL_S, (uint64_t)S.size());\n\t\t\twhile (right - left > 1) {\n\t\t\t\tuint64_t mid = (left + right) / 2;\n\t\t\t\tuint64_t r = L.at(large_idx) + S.at(mid);\n\t\t\t\tr = (bit) ? r : mid * LEVEL_S - r;\n\n\t\t\t\tif (r < rank) {\n\t\t\t\t\tleft = mid;\n\t\t\t\t\tsmall_idx = mid;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tright = mid;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Bをブロック単位で順番に探索\n\t\tuint64_t rank_pos = 0;\n\t\t{\n\t\t\tconst uint64_t begin_block_idx = (small_idx * LEVEL_S) / blockBitNum;\n\t\t\tuint64_t total_bit = L.at(large_idx) + S.at(small_idx);\n\t\t\tif (bit == 0) {\n\t\t\t\ttotal_bit = small_idx * LEVEL_S - total_bit;\n\t\t\t}\n\t\t\tfor (uint64_t i = 0;; ++i) {\n\t\t\t\tuint64_t b = popCount(B.at(begin_block_idx + i));\n\t\t\t\tif (bit == 0) {\n\t\t\t\t\tb = blockBitNum - b;\n\t\t\t\t}\n\t\t\t\tif (total_bit + b >= rank) {\n\t\t\t\t\tuint64_t block = (bit) ? B.at(begin_block_idx + i) : ~B.at(begin_block_idx + i);\n\t\t\t\t\trank_pos = (begin_block_idx + i) * blockBitNum + selectInBlock(block, rank - total_bit);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\ttotal_bit += b;\n\t\t\t}\n\t\t}\n\n\t\treturn rank_pos + 1;\n\t}\n\n\tuint64_t getNumOne() const {\n\t\treturn numOne;\n\t}\n\n\tvoid debug() {\n\t\tstd::cout << \"LEVEL_L(\" << L.size() << \")\" << std::endl;\n\t\tfor (uint64_t i = 0; i < L.size(); ++i) {\n\t\t\tstd::cout << L.at(i) << \", \";\n\t\t}\n\t\tstd::cout << std::endl;\n\t\tstd::cout << \"LEVEL_S(\" << S.size() << \")\" << std::endl;\n\t\tfor (uint64_t i = 0; i < S.size(); ++i) {\n\t\t\tstd::cout << S.at(i) << \", \";\n\t\t}\n\t\tstd::cout << std::endl;\n\t}\n\nprivate:\n\tuint64_t popCount(uint64_t x) {\n\t\tx = (x & 0x5555555555555555ULL) + ((x >> 1) & 0x5555555555555555ULL);\n\t\tx = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);\n\t\tx = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;\n\t\tx = x + (x >> 8);\n\t\tx = x + (x >> 16);\n\t\tx = x + (x >> 32);\n\t\treturn x & 0x7FLLU;\n\t}\n\n\tuint64_t selectInBlock(uint64_t x, uint64_t rank) {\n\t\tuint64_t x1 = x - ((x & 0xAAAAAAAAAAAAAAAALLU) >> 1);\n\t\tuint64_t x2 = (x1 & 0x3333333333333333LLU) + ((x1 >> 2) & 0x3333333333333333LLU);\n\t\tuint64_t x3 = (x2 + (x2 >> 4)) & 0x0F0F0F0F0F0F0F0FLLU;\n\n\t\tuint64_t pos = 0;\n\t\tfor (;; pos += 8) {\n\t\t\tuint64_t rank_next = (x3 >> pos) & 0xFFLLU;\n\t\t\tif (rank <= rank_next) break;\n\t\t\trank -= rank_next;\n\t\t}\n\n\t\tuint64_t v2 = (x2 >> pos) & 0xFLLU;\n\t\tif (rank > v2) {\n\t\t\trank -= v2;\n\t\t\tpos += 4;\n\t\t}\n\n\t\tuint64_t v1 = (x1 >> pos) & 0x3LLU;\n\t\tif (rank > v1) {\n\t\t\trank -= v1;\n\t\t\tpos += 2;\n\t\t}\n\n\t\tuint64_t v0 = (x >> pos) & 0x1LLU;\n\t\tif (v0 < rank) {\n\t\t\trank -= v0;\n\t\t\tpos += 1;\n\t\t}\n\n\t\treturn pos;\n\t}\n};\nclass WaveletMatrix {\nprivate:\n std::vector<SuccinctBitVector> bit_arrays;\n std::vector<uint64_t> begin_one; // 各bitに着目したときの1の開始位置\n std::map<uint64_t, uint64_t> begin_alphabet; // 最後のソートされた配列で各文字の開始位置\n std::vector<std::vector<uint64_t>> cumulative_sum; // 各bitに着目したときの累積和\n\n uint64_t size; // 与えられた配列のサイズ\n uint64_t maximum_element; // 文字数\n uint64_t bit_size; // 文字を表すのに必要なbit数\n\npublic:\n WaveletMatrix(const std::vector<uint64_t>& array) {\n assert(array.size() > 0);\n size = array.size();\n maximum_element = *max_element(array.begin(), array.end()) + 1;\n bit_size = get_num_of_bit(maximum_element);\n if (bit_size == 0) {\n bit_size = 1;\n }\n\n for (uint64_t i = 0; i < bit_size; ++i) {\n SuccinctBitVector sv(size);\n bit_arrays.push_back(sv);\n }\n this->begin_one.resize(bit_size);\n this->cumulative_sum.resize(bit_size + 1, std::vector<uint64_t>(size + 1, 0));\n\n for (uint64_t j = 0; j < array.size(); ++j) {\n this->cumulative_sum.at(0).at(j + 1) = this->cumulative_sum.at(0).at(j) + array[j];\n }\n\n std::vector<uint64_t> v(array);\n for (uint64_t i = 0; i < bit_size; ++i) {\n\n std::vector<uint64_t> temp;\n // 0をtempにいれてく\n for (uint64_t j = 0; j < v.size(); ++j) {\n uint64_t c = v.at(j);\n uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit\n if (bit == 0) {\n temp.push_back(c);\n bit_arrays.at(i).setBit(0, j);\n }\n }\n\n this->begin_one.at(i) = temp.size();\n\n // 1をtempにいれてく\n for (uint64_t j = 0; j < v.size(); ++j) {\n uint64_t c = v.at(j);\n uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit\n if (bit == 1) {\n temp.push_back(c);\n bit_arrays.at(i).setBit(1, j);\n }\n }\n\n for (uint64_t j = 0; j < temp.size(); ++j) {\n this->cumulative_sum.at(i + 1).at(j + 1) = this->cumulative_sum.at(i + 1).at(j) + temp.at(j);\n }\n\n bit_arrays.at(i).build();\n v = temp;\n }\n\n // ソートされた配列内での各文字の位置を取得\n for (int i = (int)v.size() - 1; i >= 0; --i) {\n this->begin_alphabet[v.at(i)] = i;\n }\n }\n\n // v[pos]\n uint64_t access(uint64_t pos) {\n if (pos >= this->size) { return NOTFOUND; }\n\n uint64_t c = 0;\n for (uint64_t i = 0; i < bit_arrays.size(); ++i) {\n uint64_t bit = bit_arrays.at(i).access(pos); // もとの数値のi番目のbit\n c = (c <<= 1) | bit;\n pos = bit_arrays.at(i).rank(bit, pos);\n if (bit) {\n pos += this->begin_one.at(i);\n }\n }\n return c;\n }\n\n\n\n // T[begin_pos, end_pos)でx <= c < yを満たす最大のcを返す\n uint64_t prevValue(const uint64_t begin_pos, const uint64_t end_pos, const uint64_t x, uint64_t y) {\n assert(end_pos <= size);\n const uint64_t num = end_pos - begin_pos;\n\n if (x >= y or y == 0) {\n return NOTFOUND;\n }\n if (y > maximum_element) {\n y = maximum_element;\n }\n\n if (begin_pos >= end_pos) {\n return NOTFOUND;\n }\n if (x >= maximum_element || end_pos == 0) {\n return NOTFOUND;\n }\n\n y--; // x <= c <= yにする\n\n std::stack<std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, bool>> s; // (begin, end, depth, c, tight)\n s.emplace(std::make_tuple(begin_pos, end_pos, 0, 0, true));\n\n while (not s.empty()) {\n uint64_t b, e, depth, c;\n bool tight;\n std::tie(b, e, depth, c, tight) = s.top(); s.pop();\n\n if (depth == bit_size) {\n if (c >= x) {\n return c;\n }\n continue;\n }\n\n const uint64_t bit = (y >> (bit_size - depth - 1)) & 1;\n\n const uint64_t rank0_begin = this->bit_arrays.at(depth).rank(0, b);\n const uint64_t rank0_end = this->bit_arrays.at(depth).rank(0, e);\n const uint64_t rank1_begin = b - rank0_begin;\n const uint64_t rank1_end = e - rank0_end;\n\n // d番目のbitが0のものを使う\n const uint64_t b0 = rank0_begin;\n const uint64_t e0 = rank0_end;\n if (b0 != e0) { // 範囲がつぶれてない\n const uint64_t c0 = ((c << 1) | 0);\n s.emplace(std::make_tuple(b0, e0, depth + 1, c0, tight and bit == 0));\n }\n\n // d番目のbitが1のものを使う\n const uint64_t b1 = this->begin_one.at(depth) + rank1_begin;\n const uint64_t e1 = this->begin_one.at(depth) + rank1_end;\n if (b1 != e1) {\n if (not tight or bit == 1) {\n const auto c1 = ((c << 1) | 1);\n s.emplace(std::make_tuple(b1, e1, depth + 1, c1, tight));\n }\n }\n }\n\n return NOTFOUND;\n }\n\n // T[begin_pos, end_pos)でx <= c < yを満たす最小のcを返す\n uint64_t nextValue(const uint64_t begin_pos, const uint64_t end_pos, const uint64_t x, const uint64_t y) {\n assert(end_pos <= size);\n const uint64_t num = end_pos - begin_pos;\n\n if (x >= y or y == 0) {\n return NOTFOUND;\n }\n\n if (begin_pos >= end_pos) {\n return NOTFOUND;\n }\n if (x >= maximum_element || end_pos == 0) {\n return NOTFOUND;\n }\n\n std::stack<std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, bool>> s; // (begin, end, depth, c, tight)\n s.emplace(std::make_tuple(begin_pos, end_pos, 0, 0, true));\n\n while (not s.empty()) {\n uint64_t b, e, depth, c;\n bool tight;\n std::tie(b, e, depth, c, tight) = s.top(); s.pop();\n\n if (depth == bit_size) {\n if (c < y) {\n return c;\n }\n continue;\n }\n\n const uint64_t bit = (x >> (bit_size - depth - 1)) & 1;\n\n const uint64_t rank0_begin = this->bit_arrays.at(depth).rank(0, b);\n const uint64_t rank0_end = this->bit_arrays.at(depth).rank(0, e);\n const uint64_t rank1_begin = b - rank0_begin;\n const uint64_t rank1_end = e - rank0_end;\n\n // d番目のbitが1のものを使う\n const uint64_t b1 = this->begin_one.at(depth) + rank1_begin;\n const uint64_t e1 = this->begin_one.at(depth) + rank1_end;\n if (b1 != e1) {\n const auto c1 = ((c << 1) | 1);\n s.emplace(std::make_tuple(b1, e1, depth + 1, c1, tight and bit == 1));\n }\n\n // d番目のbitが0のものを使う\n const uint64_t b0 = rank0_begin;\n const uint64_t e0 = rank0_end;\n if (b0 != e0) {\n if (not tight or bit == 0) {\n const uint64_t c0 = ((c << 1) | 0);\n s.emplace(std::make_tuple(b0, e0, depth + 1, c0, tight));\n }\n }\n }\n\n return NOTFOUND;\n }\n\n // T[s1, e1)とT[s2, e2)に共通して出現する要素を求める\n std::vector<std::tuple<uint64_t, uint64_t, uint64_t>> intersect(uint64_t _s1, uint64_t _e1, uint64_t _s2, uint64_t _e2) {\n assert(_s1 < _e1);\n assert(_s2 < _e2);\n\n std::vector<std::tuple<uint64_t, uint64_t, uint64_t>> intersection;\n\n std::queue<std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>> que; // s1, e1, s2, e2, depth, value\n que.push(std::make_tuple(_s1, _e1, _s2, _e2, 0, 0));\n while (not que.empty()) {\n auto e = que.front(); que.pop();\n uint64_t s1, e1, s2, e2, depth, value;\n std::tie(s1, e1, s2, e2, depth, value) = e;\n\n if (depth >= this->bit_size) {\n intersection.emplace_back(std::make_tuple(value, e1 - s1, e2 - s2));\n continue;\n }\n\n // 0\n uint64_t s1_0 = this->bit_arrays.at(depth).rank(0, s1);\n uint64_t e1_0 = this->bit_arrays.at(depth).rank(0, e1);\n uint64_t s2_0 = this->bit_arrays.at(depth).rank(0, s2);\n uint64_t e2_0 = this->bit_arrays.at(depth).rank(0, e2);\n\n if (s1_0 != e1_0 and s2_0 != e2_0) {\n que.push(std::make_tuple(s1_0, e1_0, s2_0, e2_0, depth + 1, value));\n }\n\n // 1\n uint64_t s1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s1);\n uint64_t e1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e1);\n uint64_t s2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s2);\n uint64_t e2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e2);\n\n if (s1_1 != e1_1 and s2_1 != e2_1) {\n que.push(std::make_tuple(s1_1, e1_1, s2_1, e2_1, depth + 1, value | (1ULL <<( bit_size - depth - 1))));\n }\n }\n\n return intersection;\n };\n\nprivate:\n uint64_t get_num_of_bit(uint64_t x) {\n if (x == 0) return 0;\n x--;\n uint64_t bit_num = 0;\n while (x >> bit_num) {\n ++bit_num;\n }\n return bit_num;\n }\n\n uint64_t rangeSum(const uint64_t begin, const uint64_t end, const uint64_t depth, const uint64_t c, const uint64_t x, const uint64_t y) {\n if (begin == end) {\n return 0;\n }\n\n if (depth == bit_size) {\n if (x <= c and c < y) {\n return c * (end - begin); // 値 * 頻度\n }\n return 0;\n }\n\n const uint64_t next_c = ((uint64_t)1 << (bit_size - depth - 1)) | c; // 上からdepth番目のbitを立てる\n const uint64_t all_one_c = (((uint64_t)1 << (bit_size - depth - 1)) - 1) | next_c; // depth以降のbitをたてる(これ以降全部1を選んだときの値)\n if (all_one_c < x or y <= c) {\n return 0;\n }\n\n // [begin, pos)のすべての要素は[x, y)\n if (x <= c and all_one_c < y) {\n return this->cumulative_sum.at(depth).at(end) - this->cumulative_sum.at(depth).at(begin);\n }\n\n const uint64_t rank0_begin = this->bit_arrays.at(depth).rank(0, begin);\n const uint64_t rank0_end = this->bit_arrays.at(depth).rank(0, end);\n const uint64_t rank1_begin = begin - rank0_begin;\n const uint64_t rank1_end = end - rank0_end;\n\n return rangeSum(rank0_begin, rank0_end, depth + 1, c, x, y) +\n rangeSum(this->begin_one.at(depth) + rank1_begin, this->begin_one[depth] + rank1_end, depth + 1, next_c, x, y);\n }\n};\n\n#ifdef ATCODER\ntypedef modint1000000007 mint0;\ntypedef modint998244353 mint;\n#endif\nstring CONTEST = \"typical90\";\nstring PROBLEM = \"typical90_cc\";//問題に合わせて変える\n\nclass Answer{\npublic:\n\tchar problemc;\n#ifdef TESTLOCAL\n\tvoid answertest(istream& cin, ostream& cout) {\n\n\t}\n#endif\n\tvoid answer(istream& cin, ostream& cout) {\n\t\tint N;\n\t\tcin >> N;\n\t\tvector<uint64_t> A(N);\n\t\tREP(i, N) {\n\t\t\tcin >> A[i];\n\t\t}\n WaveletMatrix wm(A);\n\t\tint Q;\n\t\tcin >> Q;\n\t\tfor (int i = 0; i < Q; i++) {\n\t\t\tuint64_t L, R, D;\n\t\t\tcin >> L >> R >> D;\n\t\t\tR++;\n\t\t\tuint64_t ans = numeric_limits<uint64_t>::max();\n auto a1 = wm.prevValue(L, R, 0, D);\n if (a1 != NOTFOUND) {\n ans = min(ans, D - a1);\n }\n auto a2 = wm.nextValue(L, R, D, 1000100);\n if(a2!=NOTFOUND){\n ans = min(ans, a2 - D);\n }\n cout << ans << endl;\n\t\t}\n\t}\n\n};\n\nbool test(char c) {\n\tstring inputdir = \"input\\\\\";\n\tstring outputdir = \"output\\\\\";\n\tstring fs = getS(inputdir + c + \".txt\");\n\tif (fs != \"\") {\n\t\tstring os = getS(outputdir + c + \".txt\");\n\t\tostringstream oss;\n\t\tistringstream iss(fs);\n\t\tAnswer A;\n\t\tA.problemc = c;\n\t\tA.answer(iss, oss);\n\t\tif (ismatch(os, oss.str())) {\n\t\t\tcerr << (char)(c + 1) << \":ok\" << endl;\n\t\t}\n\t\telse {\n\t\t\tcerr << (char)(c + 1) << \":ng\" << endl;\n\t\t\tcerr << \"true:\" << endl;\n\t\t\tcerr << os << endl;\n\t\t\tcerr << \"ans:\" << endl;\n\t\t\tcerr << oss.str() << endl;\n\t\t}\n\t\treturn true;\n\t}\n\telse {\n\t\treturn false;\n\t}\n\n}\nvoid testall() {\n\tfor (char c = '0'; c < '9'; c++) {\n\t\tif (!test(c))break;\n\t}\n}\nvoid createRandom(ostringstream& cout) {\n\tint N = 6;\n\tcout << N << endl;\n\t//vector<int> p = { 6,4,5,5,6,6 };\n\tvector<int> p = {};\n\tREP(i, N) {\n\t\tp.push_back((rand() % (N - i)) + i + 1);\n\t}\n\tfor (auto a : p) {\n\t\tcout << a << \" \";\n\t}\n\tcout << endl;\n}\n#ifdef TESTLOCAL\nvoid testLocal() {\n\tint MAX_NUM = 100;\n\tint show = max(1, MAX_NUM / 10);\n\tREP(i, MAX_NUM) {\n\t\tostringstream os;\n\t\tcreateRandom(os);\n\t\tostringstream ostest;\n\t\tistringstream istest(os.str());\n\t\tostringstream osa;\n\t\tistringstream isa(os.str());\n\t\tAnswer At('0');\n\t\tAnswer A('0');\n\t\tA.answer(isa, osa);\n\t\tAt.answertest(istest, ostest);\n\t\tif (ismatch(osa.str(), ostest.str())) {\n\t\t\tif (i % show == 0 || i == MAX_NUM - 1) {\n\t\t\t\tcout << i << \":ok\" << endl;\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tcout << i << \":ng\" << endl;\n\t\t\tcout << \"in:\" << endl;\n\t\t\tcout << os.str() << endl;\n\t\t\tcout << \"true->\" << endl;\n\t\t\tcout << ostest.str() << endl;\n\t\t\tcout << \"false->\" << endl;\n\t\t\tcout << osa.str() << endl;\n\t\t\treturn;\n\t\t}\n\t}\n\n}\n#endif\nint main() {\n\tcin.tie(nullptr);//出力後に必ずendl or flushすれば問題ない。\n\tios::sync_with_stdio(false);//scanf printfと混ぜなければ問題ない。\n\n#ifdef ATCODER\n#ifdef _LOCAL\n#ifdef TESTLOCAL\n\ttestLocal();\n#else\n\tif (!std::filesystem::is_regular_file(PROBLEM + \"_AC\")) {\n\t\tstring command = \"problemget.bat \" + CONTEST + \" \" + PROBLEM;\n\t\tsystem(command.c_str());\n\t}\n\ttestall();\n#endif\n#else\n\tAnswer A;\n\tA.answer(cin, cout);\n#endif\n#else\n#ifdef AIZU\n#ifdef _LOCAL\n\tif (!std::filesystem::is_regular_file(CONTEST + \"_AC\")) {//問題を取得したら一旦抜ける。\n\t\tstring command = \"problemgetaizu.bat \" + CONTEST;\n\t\tsystem(command.c_str());\n\t}\n\ttestall();\n#else\n\tAnswer A;\n\tA.answer(cin, cout);\n#endif\n#else\n\tAnswer A;\n\tA.answer(cin, cout);\n#endif\n#endif\n#ifdef _LOCAL\n\tcin.get();\n#endif\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 27532, "score_of_the_acc": -1.7273, "final_rank": 18 }, { "submission_id": "aoj_1549_9003544", "code_snippet": "#pragma region Macros\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2\")\n\n#include <bits/extc++.h>\n#include <immintrin.h>\n// #include <atcoder/all>\n// using namespace atcoder;\nusing namespace std;\nusing namespace __gnu_pbds;\n\n// #include <boost/multiprecision/cpp_dec_float.hpp>\n// #include <boost/multiprecision/cpp_int.hpp>\n// namespace mp = boost::multiprecision;\n// using Bint = mp::cpp_int;\n// using Bdouble = mp::number<mp::cpp_dec_float<256>>;\n\n#define pb emplace_back\n#define int ll\n#define endl '\\n'\n\n#define sqrt __builtin_sqrt\n#define cbrt __builtin_cbrt\n#define hypot __builtin_hypot\n\nusing ll = long long;\nusing ld = long double;\nconst ld PI = acosl(-1);\nconst int INF = 1 << 30;\nconst ll INFL = 1LL << 61;\nconst int MOD = 998244353;\n// const int MOD = 1000000007;\n\nconst ld EPS = 1e-10;\nconst bool equals(ld a, ld b) { return fabs((a) - (b)) < EPS; }\n\nconst vector<int> dx = {0, 1, 0, -1, 1, 1, -1, -1}; // → ↓ ← ↑ ↘ ↙ ↖ ↗\nconst vector<int> dy = {1, 0, -1, 0, 1, -1, -1, 1};\n\nstruct Edge {\n int from, to;\n ll cost;\n Edge(int to, ll cost) : to(to), cost(cost) {}\n Edge(int from, int to, ll cost) : from(from), to(to), cost(cost) {}\n};\n\nchrono::system_clock::time_point start, now;\n__attribute__((constructor))\nvoid constructor() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(10);\n start = chrono::system_clock::now();\n}\n\n__int128_t POW(__int128_t x, int n) {\n __int128_t ret = 1;\n assert(n >= 0);\n if (x == 1 or n == 0) ret = 1;\n else if (x == -1 && n % 2 == 0) ret = 1; \n else if (x == -1) ret = -1; \n else if (n % 2 == 0) {\n assert(x < INFL);\n ret = POW(x * x, n / 2);\n } else {\n assert(x < INFL);\n ret = x * POW(x, n - 1);\n }\n return ret;\n}\nint per(int x, int y) { // x = qy + r (0 <= r < y) を満たすq\n assert(y != 0);\n if (x >= 0 && y > 0) return x / y;\n if (x >= 0 && y < 0) return x / y - (x % y < 0);\n if (x < 0 && y < 0) return x / y + (x % y < 0);\n return x / y - (x % y < 0); // (x < 0 && y > 0) \n}\n// int perl(ld x, ld y) { // perld(4.5, 2.1) = 2 // TODO\n// if (-EPS < x && x < 0 or 0 < x && x < EPS) x = 0;\n// if (-EPS < y && y < 0 or 0 < x && x < EPS) y = 0;\n// assert(!equals(y, 0));\n// if (x >= 0 && y > 0) return floor(x / y)+EPS;\n// if (x >= 0 && y < 0) return floor(x / y) - (x - floor(x/y)*y < -EPS);\n// if (x < 0 && y < 0) return floor(x / y) + (x - floor(x/y)*y < -EPS);\n// return floor(x / y) - (x - floor(x/y)*y < -EPS); // (x < 0 && y > 0) \n// }\nint mod(int x, int y) { // x = qy + r (0 <= r < y) を満たすr\n assert(y != 0);\n if (x >= 0) return x % y;\n __int128_t ret = x % y; // (x < 0)\n ret += (__int128_t)abs(y) * INFL;\n ret %= abs(y);\n return ret;\n}\n// ld modl(ld x, ld y) { // TODO\n// assert(!equals(y, 0));\n// if (x >= -EPS) return (x - floor(x/y)*y);\n// ld ret = x - floor(x/y)*y; // (x < 0)\n// ret += abs(y) * INFL; // TODO : オーバーフローする?\n// ret = x - floor(x/abs(y))*abs(y);\n// return ret;\n// }\n// int floor(int x, int y) { // TODO\n// assert(y != 0);\n// if (b < 0) a = -a, b = -b;\n// return a >= 0 ? a / b : (a + 1) / b - 1;\n// }\n// int ceil(int x, int y) { // TODO\n// assert(y != 0);\n// if (b < 0) a = -a, b = -b;\n// return a > 0 ? (a - 1) / b + 1 : a / b;\n// }\n// int floorl(ld x, ld y) { return 0; } // TODO\n// int ceill(ld x, ld y) { return 0; } // TODO\n// int gauss(int x, int y) {\n// assert(y != 0);\n// return x / y;\n// } // 整数部分(未verify)\n// int gauss(ld x, ld y) { return 0; } // TODO\n\npair<int, int> max(const pair<int, int> &a, const pair<int, int> &b) {\n if (a.first > b.first or a.first == b.first && a.second > b.second) {\n return a;\n }\n return b;\n}\npair<int, int> min(const pair<int, int> &a, const pair<int, int> &b) {\n if (a.first < b.first or a.first == b.first && a.second < b.second) {\n return a;\n }\n return b;\n}\n\ntemplate <class T> bool chmax(T &a, const T& b) {\n if (a < b) { a = b; return true; }\n return false;\n}\ntemplate <class T> bool chmin(T &a, const T& b) {\n if (a > b) { a = b; return true; }\n return false;\n}\ntemplate <class T> T mid(T a, T b, T c) {\n return a + b + c - max({a, b, c}) - min({a, b, c});\n}\ntemplate <class T> void sort(T &a, T &b, T &c, bool rev = false) {\n if (rev == false) { \n if (a > b) swap(a, b);\n if (a > c) swap(a, c);\n if (b > c) swap(b, c);\n } else {\n if (c > b) swap(c, b);\n if (c > a) swap(c, a);\n if (b > a) swap(b, a);\n }\n}\ntemplate <class T> void sort(T &a, T &b, T &c, T &d, bool rev = false) {\n if (rev == false) { \n if (a > b) swap(a, b); if (a > c) swap(a, c); if (a > d) swap(a, d);\n if (b > c) swap(b, c); if (b > d) swap(b, d);\n if (c > d) swap(c, d);\n } else {\n if (d > c) swap(d, c); if (d > b) swap(d, b); if (d > a) swap(d, a);\n if (c > b) swap(c, b); if (c > a) swap(c, a);\n if (b > a) swap(b, a);\n }\n}\n\nint countl_zero(int x) { return __builtin_clzll(x); }\nint countl_one(int x) {\n int ret = 0; while (x % 2) { x /= 2; ret++; }\n return ret;\n}\nint countr_zero(int x) { return __builtin_ctzll(x); }\nint countr_one(int x) {\n int ret = 0, k = 63 - __builtin_clzll(x);\n while (k != -1 && (x & (1LL << k))) { k--; ret++; }\n return ret;\n}\nint popcount(int x) { return __builtin_popcountll(x); }\nint unpopcount(int x) { return 64 - __builtin_clzll(x) - __builtin_popcountll(x); }\n\nint top_bit(int x) { return 63 - __builtin_clzll(x);} // 2^kの位\nint bot_bit(int x) { return __builtin_ctz(x);} // 2^kの位\nint MSB(int x) { return 1 << (63 - __builtin_clzll(x)); } // mask\nint LSB(int x) { return (x & -x); } // mask\n\nint bit_width(int x) { return 64 - __builtin_clzll(x); } // 桁数\nint ceil_log2(int x) { return 63 - __builtin_clzll(x); }\nint bit_floor(int x) { return 1 << (63 - __builtin_clzll(x)); }\nint floor_log2(int x) { return 64 - __builtin_clzll(x-1); }\nint bit_ceil(int x) { return 1 << (64 - __builtin_clzll(x-1)) - (x==1); }\n\nint hamming(int a, int b) { return popcount(a ^ b); }\nint compcnt(int x) { return (popcount(x^(x >> 1)) + (x&1)) / 2; }\n\nclass UnionFind {\npublic:\n\tUnionFind() = default;\n UnionFind(int N) : par(N), sz(N, 1) {\n iota(par.begin(), par.end(), 0);\n }\n\n\tint root(int x) {\n\t\tif (par[x] == x) return x;\n\t\treturn (par[x] = root(par[x]));\n\t}\n\n\tbool unite(int x, int y) {\n\t\tint rx = root(x);\n\t\tint ry = root(y);\n\n if (rx == ry) return false;\n\t\tif (sz[rx] < sz[ry]) swap(rx, ry);\n\n\t\tsz[rx] += sz[ry];\n\t\tpar[ry] = rx;\n\n return true;\n\t}\n\n\tbool issame(int x, int y) { return (root(x) == root(y)); }\n\tint size(int x) { return sz[root(x)]; }\n\n vector<vector<int>> groups(int N) {\n vector<vector<int>> G(N);\n for (int x = 0; x < N; x++) {\n G[root(x)].push_back(x);\n }\n\t\tG.erase(\n remove_if(G.begin(), G.end(),\n [&](const vector<int>& V) { return V.empty(); }),\n G.end());\n return G;\n }\nprivate:\n\tvector<int> par;\n\tvector<int> sz;\n};\n\ntemplate<int mod> class Modint{\npublic:\n int val = 0;\n Modint(int x = 0) { while (x < 0) x += mod; val = x % mod; }\n Modint(const Modint &r) { val = r.val; }\n\n Modint operator -() { return Modint(-val); } // 単項\n Modint operator +(const Modint &r) { return Modint(*this) += r; }\n Modint operator +(const int &q) { Modint r(q); return Modint(*this) += r; }\n Modint operator -(const Modint &r) { return Modint(*this) -= r; }\n Modint operator -(const int &q) { Modint r(q); return Modint(*this) -= r; }\n Modint operator *(const Modint &r) { return Modint(*this) *= r; }\n Modint operator *(const int &q) { Modint r(q); return Modint(*this) *= r; }\n Modint operator /(const Modint &r) { return Modint(*this) /= r; }\n Modint operator /(const int &q) { Modint r(q); return Modint(*this) /= r; }\n \n Modint& operator ++() { val++; if (val >= mod) val -= mod; return *this; } // 前置\n Modint operator ++(signed) { ++*this; return *this; } // 後置\n Modint& operator --() { val--; if (val < 0) val += mod; return *this; }\n Modint operator --(signed) { --*this; return *this; }\n Modint &operator +=(const Modint &r) { val += r.val; if (val >= mod) val -= mod; return *this; }\n Modint &operator +=(const int &q) { Modint r(q); val += r.val; if (val >= mod) val -= mod; return *this; }\n Modint &operator -=(const Modint &r) { if (val < r.val) val += mod; val -= r.val; return *this; }\n Modint &operator -=(const int &q) { Modint r(q); if (val < r.val) val += mod; val -= r.val; return *this; }\n Modint &operator *=(const Modint &r) { val = val * r.val % mod; return *this; }\n Modint &operator *=(const int &q) { Modint r(q); val = val * r.val % mod; return *this; }\n Modint &operator /=(const Modint &r) {\n int a = r.val, b = mod, u = 1, v = 0;\n while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}\n val = val * u % mod; if (val < 0) val += mod;\n return *this;\n }\n Modint &operator /=(const int &q) {\n Modint r(q); int a = r.val, b = mod, u = 1, v = 0;\n while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}\n val = val * u % mod; if (val < 0) val += mod;\n return *this;\n }\n\n bool operator ==(const Modint& r) { return this -> val == r.val; }\n bool operator <(const Modint& r) { return this -> val < r.val; }\n bool operator >(const Modint& r) { return this -> val > r.val; }\n bool operator !=(const Modint& r) { return this -> val != r.val; }\n};\n\nusing mint = Modint<MOD>;\n// using Mint = modint998244353;\n\nistream &operator >>(istream &is, mint& x) {\n int t; is >> t;\n x = t;\n return (is);\n}\nostream &operator <<(ostream &os, const mint& x) {\n return os << x.val;\n}\nmint modpow(const mint &x, int n) {\n assert(n >= 0); // TODO: n <= -1\n if (n == 0) return 1;\n mint t = modpow(x, n / 2);\n t = t * t;\n if (n & 1) t = t * x;\n return t;\n}\n\nint modpow(__int128_t x, int n, int mod) {\n assert(n >= 0 && mod > 0); // TODO: n <= -1\n __int128_t ret = 1;\n while (n > 0) {\n if (n % 2 == 1) ret = ret * x % mod;\n x = x * x % mod;\n n /= 2;\n }\n return ret;\n}\n\nint modinv(__int128_t x, int mod) {\n assert(mod > 0 && x > 0);\n if (x == 1) return 1;\n return mod - modinv(mod % x, mod) * (mod / x) % mod;\n}\n\nistream &operator >>(istream &is, __int128_t& x) {\n string S; is >> S;\n __int128_t ret = 0;\n int f = 1;\n if (S[0] == '-') f = -1; \n for (int i = 0; i < S.length(); i++)\n if ('0' <= S[i] && S[i] <= '9')\n ret = ret * 10 + S[i] - '0';\n x = ret * f;\n return (is);\n}\nostream &operator <<(ostream &os, __int128_t x) {\n ostream::sentry s(os);\n if (s) {\n __uint128_t tmp = x < 0 ? -x : x;\n char buffer[128];\n char *d = end(buffer);\n\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n\n if (x < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n\n if (os.rdbuf()->sputn(d, len) != len) {\n os.setstate(ios_base::badbit);\n }\n }\n return os;\n}\n\n__int128_t stoll(string &S) {\n __int128_t ret = 0;\n int f = 1;\n if (S[0] == '-') f = -1; \n for (int i = 0; i < S.length(); i++)\n if ('0' <= S[i] && S[i] <= '9')\n ret = ret * 10 + S[i] - '0';\n return ret * f;\n}\n__int128_t gcd(__int128_t a, __int128_t b) {\n return b ? gcd(b, a % b) : a;\n}\n__int128_t lcm(__int128_t a, __int128_t b) {\n return a / gcd(a, b) * b;\n // lcmが__int128_tに収まる必要あり\n}\n\nstring to_string(ld x, int k) { // xの小数第k位までをstring化する\n assert(k >= 0);\n stringstream ss;\n ss << setprecision(k + 2) << x;\n string s = ss.str();\n if (s.find('.') == string::npos) s += '.';\n int pos = s.find('.');\n for (int i = 0; k >= (int)s.size() - 1 - pos; i++) s += '0';\n s.pop_back();\n if (s.back() == '.') s.pop_back();\n return s;\n\n // stringstream ss; // 第k+1位を四捨五入して第k位まで返す\n // ss << setprecision(k + 1) << x;\n // string s = ss.str();\n // if (s.find('.') == string::npos) s += '.';\n // int pos = s.find('.');\n // for (int i = 0; k > (int)s.size() - 1 - pos; i++) s += '0';\n // if (s.back() == '.') s.pop_back();\n // return s;\n}\n\nstring to_string(__int128_t x) {\n string ret = \"\";\n if (x < 0) {\n ret += \"-\";\n x *= -1;\n }\n while (x) {\n ret += (char)('0' + x % 10);\n x /= 10;\n }\n reverse(ret.begin(), ret.end());\n return ret;\n}\nstring to_string(char c) {\n string s = \"\";\n s += c;\n return s;\n}\n\nstruct SXor128 {\n uint64_t x = 88172645463325252LL;\n unsigned Int() {\n x = x ^ (x << 7);\n return x = x ^ (x >> 9);\n }\n unsigned Int(unsigned mod) {\n x = x ^ (x << 7);\n x = x ^ (x >> 9);\n return x % mod;\n }\n unsigned Int(unsigned l, unsigned r) {\n x = x ^ (x << 7);\n x = x ^ (x >> 9);\n return x % (r - l + 1) + l;\n }\n double Double() {\n return double(Int()) / UINT_MAX;\n }\n} rnd;\n\nstruct custom_hash {\n static uint64_t splitmix64(uint64_t x) {\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n\n size_t operator()(uint64_t x) const {\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n return splitmix64(x + FIXED_RANDOM);\n }\n};\n\ntemplate<class T> size_t HashCombine(const size_t seed,const T &v){\n return seed^(hash<T>()(v)+0x9e3779b9+(seed<<6)+(seed>>2));\n}\ntemplate<class T,class S> struct hash<pair<T,S>>{\n size_t operator()(const pair<T,S> &keyval) const noexcept {\n return HashCombine(hash<T>()(keyval.first), keyval.second);\n }\n};\ntemplate<class T> struct hash<vector<T>>{\n size_t operator()(const vector<T> &keyval) const noexcept {\n size_t s=0;\n for (auto&& v: keyval) s=HashCombine(s,v);\n return s;\n }\n};\ntemplate<int N> struct HashTupleCore{\n template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{\n size_t s=HashTupleCore<N-1>()(keyval);\n return HashCombine(s,get<N-1>(keyval));\n }\n};\ntemplate <> struct HashTupleCore<0>{\n template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{ return 0; }\n};\ntemplate<class... Args> struct hash<tuple<Args...>>{\n size_t operator()(const tuple<Args...> &keyval) const noexcept {\n return HashTupleCore<tuple_size<tuple<Args...>>::value>()(keyval);\n }\n};\n\nvector<mint> _fac, _finv, _inv;\nvoid COMinit(int N) {\n _fac.resize(N + 1);\n _finv.resize(N + 1);\n _inv.resize(N + 1);\n _fac[0] = _fac[1] = 1;\n _finv[0] = _finv[1] = 1;\n _inv[1] = 1;\n for (int i = 2; i <= N; i++) {\n _fac[i] = _fac[i-1] * mint(i);\n _inv[i] = -_inv[MOD % i] * mint(MOD / i);\n _finv[i] = _finv[i - 1] * _inv[i];\n }\n}\n\nmint FAC(int N) {\n if (N < 0) return 0;\n return _fac[N];\n}\nmint COM(int N, int K) {\n if (N < K) return 0;\n if (N < 0 or K < 0) return 0;\n return _fac[N] * _finv[K] * _finv[N - K];\n}\nmint PERM(int N, int K) {\n if (N < K) return 0;\n if (N < 0 or K < 0) return 0;\n return _fac[N] * _finv[N - K];\n}\nmint NHK(int N, int K) {\n if (N == 0 && K == 0) return 1;\n return COM(N + K - 1, K);\n}\n\n#pragma endregion\n\nstruct bit_vector {\n using u32 = uint32_t;\n using i64 = int64_t;\n using u64 = uint64_t;\n\n static constexpr u32 w = 64;\n vector<u64> block;\n vector<u32> count;\n u32 n, zeros;\n\n inline u32 get(u32 i) const { return u32(block[i / w] >> (i % w)) & 1u; }\n inline void set(u32 i) { block[i / w] |= 1LL << (i % w); }\n\n bit_vector() {}\n bit_vector(int _n) { init(_n); }\n __attribute__((optimize(\"O3\", \"unroll-loops\"))) void init(int _n) {\n n = zeros = _n;\n block.resize(n / w + 1, 0);\n count.resize(block.size(), 0);\n }\n\n __attribute__((target(\"popcnt\"))) void build() {\n for (u32 i = 1; i < block.size(); i++)\n count[i] = count[i - 1] + _mm_popcnt_u64(block[i - 1]);\n zeros = rank0(n);\n }\n\n inline u32 rank0(u32 i) const { return i - rank1(i); }\n __attribute__((target(\"bmi2,popcnt\"))) inline u32 rank1(u32 i) const {\n return count[i / w] + _mm_popcnt_u64(_bzhi_u64(block[i / w], i % w));\n }\n};\n\ntemplate <typename T>\nstruct WaveletMatrix {\n using u32 = uint32_t;\n using i64 = int64_t;\n using u64 = uint64_t;\n\n int n, lg;\n vector<T> A;\n vector<bit_vector> bv;\n\n WaveletMatrix(u32 _n) : n(max<u32>(_n, 1)), A(n) {}\n WaveletMatrix(const vector<T>& _a) : n(_a.size()), A(_a) { build(); }\n\n __attribute__((optimize(\"O3\"))) void build() {\n lg = __lg(max<T>(*max_element(begin(A), end(A)), 1)) + 1;\n bv.assign(lg, n);\n vector<T> cur = A, nxt(n);\n for (int h = lg - 1; h >= 0; h--) {\n for (int i = 0; i < n; i++) {\n if ((cur[i] >> h) & 1) bv[h].set(i);\n }\n bv[h].build();\n array<decltype(begin(nxt)), 2> it{begin(nxt), begin(nxt) + bv[h].zeros};\n for (int i = 0; i < n; i++) *it[bv[h].get(i)]++ = cur[i];\n swap(cur, nxt);\n }\n return;\n }\n\n void set(u32 i, const T& x) { \n assert(x >= 0);\n A[i] = x; \n }\n\n inline pair<u32, u32> succ0(int l, int r, int h) const {\n return make_pair(bv[h].rank0(l), bv[h].rank0(r));\n }\n\n inline pair<u32, u32> succ1(int l, int r, int h) const {\n u32 l0 = bv[h].rank0(l);\n u32 r0 = bv[h].rank0(r);\n u32 zeros = bv[h].zeros;\n return make_pair(l + zeros - l0, r + zeros - r0);\n }\n\n // return A[k]\n T access(u32 k) const {\n T ret = 0;\n for (int h = lg - 1; h >= 0; h--) {\n u32 f = bv[h].get(k);\n ret |= f ? T(1) << h : 0;\n k = f ? bv[h].rank1(k) + bv[h].zeros : bv[h].rank0(k);\n }\n return ret;\n }\n\n // k-th (0-indexed) smallest number in A[l, r)\n T kth_smallest(u32 l, u32 r, u32 k) const {\n T res = 0;\n for (int h = lg - 1; h >= 0; h--) {\n u32 l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if (k < r0 - l0)\n l = l0, r = r0;\n else {\n k -= r0 - l0;\n res |= (T)1 << h;\n l += bv[h].zeros - l0;\n r += bv[h].zeros - r0;\n }\n }\n return res;\n }\n\n // k-th (0-indexed) largest number in A[l, r)\n T kth_largest(int l, int r, int k) {\n return kth_smallest(l, r, r - l - k - 1);\n }\n\n // range_freq i s.t. (l <= i < r) && (v[i] < upper)\n int range_freq(int l, int r, T upper) {\n if (upper >= (T(1) << lg)) return r - l;\n int ret = 0;\n for (int h = lg - 1; h >= 0; h--) {\n bool f = (upper >> h) & 1;\n u32 l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if (f) {\n ret += r0 - l0;\n l += bv[h].zeros - l0;\n r += bv[h].zeros - r0;\n } else {\n l = l0;\n r = r0;\n }\n }\n return ret;\n }\n\n int count(int l, int r, T x) {\n return range_freq(l, r, x + 1) - range_freq(l, r, x);\n }\n\n int count(int l, int r, T lower, T upper) {\n return range_freq(l, r, upper) - range_freq(l, r, lower);\n }\n\n // max v[i] s.t. (l <= i < r) && (v[i] < upper)\n T prev_value(int l, int r, T upper) {\n int cnt = range_freq(l, r, upper);\n return cnt == 0 ? T(-1) : kth_smallest(l, r, cnt - 1);\n }\n\n // min v[i] s.t. (l <= i < r) && (lower <= v[i])\n T next_value(int l, int r, T lower) {\n int cnt = range_freq(l, r, lower);\n return cnt == r - l ? T(-1) : kth_smallest(l, r, cnt);\n }\n};\n\nsigned main() {\n int N;\n cin >> N;\n vector<int> A(N);\n for (int i = 0; i < N; i++) {\n cin >> A[i];\n if (A[i] < 0) A[i] = INF + A[i];\n }\n WaveletMatrix<int> WM(A); // 長さNの数列による初期化を行う\n\n int Q;\n cin >> Q;\n for (int q = 0; q < Q; q++) {\n int l, r, x;\n cin >> l >> r >> x;\n int a = WM.next_value(l, r + 1, x);\n int b = WM.prev_value(l, r + 1, x);\n int c = WM.next_value(l, r + 1, INF);\n int d = WM.prev_value(l, r + 1, INF);\n\n int ans = INFL;\n if (a != -1) ans = min(ans, max(x, a) - min(x, a));\n if (b != -1) ans = min(ans, max(x, b) - min(x, b));\n if (c != -1) ans = min(ans, max(x, c) - min(x, c));\n if (d != -1) ans = min(ans, max(x, d) - min(x, d));\n\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 6624, "score_of_the_acc": -0.2325, "final_rank": 5 }, { "submission_id": "aoj_1549_8522847", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#line 2 \"data-structure-2d/wavelet-matrix.hpp\"\n\n#include <immintrin.h>\n\nstruct bit_vector {\n using u32 = uint32_t;\n using i64 = int64_t;\n using u64 = uint64_t;\n\n static constexpr u32 w = 64;\n vector<u64> block;\n vector<u32> count;\n u32 n, zeros;\n\n inline u32 get(u32 i) const { return u32(block[i / w] >> (i % w)) & 1u; }\n inline void set(u32 i) { block[i / w] |= 1LL << (i % w); }\n\n bit_vector() {}\n bit_vector(int _n) { init(_n); }\n __attribute__((optimize(\"O3\", \"unroll-loops\"))) void init(int _n) {\n n = zeros = _n;\n block.resize(n / w + 1, 0);\n count.resize(block.size(), 0);\n }\n\n __attribute__((target(\"popcnt\"))) void build() {\n for (u32 i = 1; i < block.size(); ++i)\n count[i] = count[i - 1] + _mm_popcnt_u64(block[i - 1]);\n zeros = rank0(n);\n }\n\n inline u32 rank0(u32 i) const { return i - rank1(i); }\n __attribute__((target(\"bmi2,popcnt\"))) inline u32 rank1(u32 i) const {\n return count[i / w] + _mm_popcnt_u64(_bzhi_u64(block[i / w], i % w));\n }\n};\n\ntemplate <typename T>\nstruct WaveletMatrix {\n using u32 = uint32_t;\n using i64 = int64_t;\n using u64 = uint64_t;\n\n int n, lg;\n vector<T> a;\n vector<bit_vector> bv;\n\n WaveletMatrix(u32 _n) : n(max<u32>(_n, 1)), a(n) {}\n WaveletMatrix(const vector<T>& _a) : n(_a.size()), a(_a) { build(); }\n\n __attribute__((optimize(\"O3\"))) void build() {\n lg = __lg(max<T>(*max_element(begin(a), end(a)), 1)) + 1;\n bv.assign(lg, n);\n vector<T> cur = a, nxt(n);\n for (int h = lg - 1; h >= 0; --h) {\n for (int i = 0; i < n; ++i)\n if ((cur[i] >> h) & 1) bv[h].set(i);\n bv[h].build();\n array<decltype(begin(nxt)), 2> it{begin(nxt), begin(nxt) + bv[h].zeros};\n for (int i = 0; i < n; ++i) *it[bv[h].get(i)]++ = cur[i];\n swap(cur, nxt);\n }\n return;\n }\n\n void set(u32 i, const T& x) { \n assert(x >= 0);\n a[i] = x; \n }\n\n inline pair<u32, u32> succ0(int l, int r, int h) const {\n return make_pair(bv[h].rank0(l), bv[h].rank0(r));\n }\n\n inline pair<u32, u32> succ1(int l, int r, int h) const {\n u32 l0 = bv[h].rank0(l);\n u32 r0 = bv[h].rank0(r);\n u32 zeros = bv[h].zeros;\n return make_pair(l + zeros - l0, r + zeros - r0);\n }\n\n // return a[k]\n T access(u32 k) const {\n T ret = 0;\n for (int h = lg - 1; h >= 0; --h) {\n u32 f = bv[h].get(k);\n ret |= f ? T(1) << h : 0;\n k = f ? bv[h].rank1(k) + bv[h].zeros : bv[h].rank0(k);\n }\n return ret;\n }\n\n // k-th (0-indexed) smallest number in a[l, r)\n T kth_smallest(u32 l, u32 r, u32 k) const {\n T res = 0;\n for (int h = lg - 1; h >= 0; --h) {\n u32 l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if (k < r0 - l0)\n l = l0, r = r0;\n else {\n k -= r0 - l0;\n res |= (T)1 << h;\n l += bv[h].zeros - l0;\n r += bv[h].zeros - r0;\n }\n }\n return res;\n }\n\n // k-th (0-indexed) largest number in a[l, r)\n T kth_largest(int l, int r, int k) {\n return kth_smallest(l, r, r - l - k - 1);\n }\n\n // count i s.t. (l <= i < r) && (v[i] < upper)\n int range_freq(int l, int r, T upper) {\n if (upper >= (T(1) << lg)) return r - l;\n int ret = 0;\n for (int h = lg - 1; h >= 0; --h) {\n bool f = (upper >> h) & 1;\n u32 l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if (f) {\n ret += r0 - l0;\n l += bv[h].zeros - l0;\n r += bv[h].zeros - r0;\n } else {\n l = l0;\n r = r0;\n }\n }\n return ret;\n }\n\n int range_freq(int l, int r, T lower, T upper) {\n return range_freq(l, r, upper) - range_freq(l, r, lower);\n }\n\n // max v[i] s.t. (l <= i < r) && (v[i] < upper)\n T prev_value(int l, int r, T upper) {\n int cnt = range_freq(l, r, upper);\n return cnt == 0 ? T(-1) : kth_smallest(l, r, cnt - 1);\n }\n\n // min v[i] s.t. (l <= i < r) && (lower <= v[i])\n T next_value(int l, int r, T lower) {\n int cnt = range_freq(l, r, lower);\n return cnt == r - l ? T(-1) : kth_smallest(l, r, cnt);\n }\n};\n\n/*\n * @brief Wavelet Matrix\n * @docs docs/data-structure-2d/wavelet-matrix.md\n */\n\nint N,Q;\nint A[100005];\nWaveletMatrix<int> W(100005);\n\nint main(){\n cin>>N;\n for(int i=0;i<N;i++){\n cin>>A[i];\n W.set(i,A[i]);\n }\n W.build();\n cin>>Q;\n while(Q--){\n int l,r,d; cin>>l>>r>>d;\n int p=W.prev_value(l,r+1,d),n=W.next_value(l,r+1,d);\n if(p==-1)cout<<n-d<<endl;\n else if(n==-1)cout<<d-p<<endl;\n else cout<<min(d-p,n-d)<<endl;\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 4956, "score_of_the_acc": -0.6149, "final_rank": 11 }, { "submission_id": "aoj_1549_8464425", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1549.cc: Problem I: Hard Beans\n */\n\n#include<cstdio>\n#include<vector>\n#include<algorithm>\n#include<utility>\n#include<tuple>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\nconst int MAX_QN = 100000;\nconst int INF = 1 << 30;\n\n/* typedef */\n\ntypedef pair<int,int> pii;\ntypedef tuple<int,int,int,int> quadi;\n\ntemplate <typename T>\nstruct SegTreeOp {\n int e2;\n vector<T> nodes;\n T defv, (*fop)(T,T);\n SegTreeOp() {}\n\n void init(int n, T _defv, T (*_fop)(T,T)) {\n defv = _defv, fop = _fop;\n for (e2 = 1; e2 < n; e2 <<= 1);\n nodes.assign(e2 * 2, defv);\n }\n\n T &geti(int i) { return nodes[e2 - 1 + i]; }\n void seti(int i, T v) { geti(i) = v; }\n\n void setall() {\n for (int j = e2 - 2; j >= 0; j--)\n nodes[j] = fop(nodes[j * 2 + 1], nodes[j * 2 + 2]);\n }\n\n void set(int i, T v) {\n int j = e2 - 1 + i;\n nodes[j] = v;\n while (j > 0) {\n j = (j - 1) / 2;\n nodes[j] = fop(nodes[j * 2 + 1], nodes[j * 2 + 2]);\n }\n }\n\n T op_range(int r0, int r1, int k, int i0, int i1) {\n if (r1 <= i0 || i1 <= r0) return defv;\n if (r0 <= i0 && i1 <= r1) return nodes[k];\n\n int im = (i0 + i1) / 2;\n T v0 = op_range(r0, r1, k * 2 + 1, i0, im);\n T v1 = op_range(r0, r1, k * 2 + 2, im, i1);\n return fop(v0, v1);\n }\n T op_range(int r0, int r1) { return op_range(r0, r1, 0, 0, e2); }\n};\n\n\n/* global variables */\n\nint as[MAX_N], res[MAX_QN];\npii ais[MAX_N];\nquadi qs[MAX_QN];\nSegTreeOp<int> st0, st1;\n\n/* subroutines */\n\nint op_min(int a, int b) { return min(a, b); }\nint op_max(int a, int b) { return max(a, b); }\n\n/* main */\n\nint main() {\n int n;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) scanf(\"%d\", as + i);\n\n for (int i = 0; i < n; i++) ais[i] = pii(as[i], i);\n sort(ais, ais + n);\n\n int qn;\n scanf(\"%d\", &qn);\n for (int i = 0; i < qn; i++) {\n int l, r, d;\n scanf(\"%d%d%d\", &l, &r, &d), r++;\n qs[i] = { d, l, r, i };\n }\n sort(qs, qs + qn);\n\n st0.init(n, INF, op_min);\n st1.init(n, -INF, op_max);\n for (int i = 0; i < n; i++) st0.seti(i, as[i]);\n st0.setall();\n\n for (int i = 0, k = 0; i < qn; i++) {\n auto [dj, lj, rj, j] = qs[i];\n\n while (k < n && ais[k].first < dj) {\n auto [ak, ik] = ais[k++];\n st0.set(ik, INF);\n st1.set(ik, ak);\n }\n\n int v0 = st0.op_range(lj, rj);\n int v1 = st1.op_range(lj, rj);\n res[j] = min(v0 - dj, dj - v1);\n }\n\n for (int i = 0; i < qn; i++) printf(\"%d\\n\", res[i]);\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 7904, "score_of_the_acc": -0.1515, "final_rank": 2 }, { "submission_id": "aoj_1549_8404740", "code_snippet": "//\n// Wavelet Matrix (Preprocess: O(N log N), Query: O(log N))\n//\n// verified:\n// Yosupu Judge - Range Kth Smallest (for k_th_smallest)\n// https://judge.yosupo.jp/problem/range_kth_smallest\n//\n// Yosupu Judge - Static Range Frequency (for rank)\n// https://judge.yosupo.jp/problem/static_range_frequency\n//\n// Yosupu Judge - Rectangle Sum (for BIT on WM)\n// https://judge.yosupo.jp/problem/rectangle_sum\n//\n// Yosupu Judge - Point Add Rectangle Sum (for BIT on WM)\n// https://judge.yosupo.jp/problem/point_add_rectangle_sum\n//\n// AtCoder ABC 234 D - Prefix K-th Max (for k_th_largest)\n// https://atcoder.jp/contests/abc234/tasks/abc234_d\n//\n// AtCoder ABC 281 E - Least Elements (for top_k_sum)\n// https://atcoder.jp/contests/abc281/tasks/abc281_e\n//\n// AtCoder ABC 324 E - G - Generate Arrays (for range_freq)\n// https://atcoder.jp/contests/abc324/tasks/abc324_g\n//\n// AOJ 1549 Hard Beans (for prev_value, next_value)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1549\n//\n// AOJ 2426 Treasure Hunt (for BIT on WM) 未 verify\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2426\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n\n// Bit Vector (for 64-bit non-negative integer)\nstruct BitVector {\n // block: bit vector\n // count: the number of 1 within each block\n unsigned int n, zeros;\n vector<unsigned long long> block;\n vector<unsigned int> count;\n \n // constructor\n BitVector() {}\n BitVector(const unsigned int num) {\n resize(num);\n }\n void resize(const unsigned int num) {\n n = num;\n block.assign(((num + 1) >> 6) + 1, 0);\n count.assign(block.size(), 0);\n }\n \n // set val(0 or 1) onto i-th bit, get i-th bit of val(0 or 1)\n void set(const unsigned int i, const unsigned long long val = 1LL) {\n assert((i >> 6) < block.size());\n block[i >> 6] |= (val << (i & 63));\n }\n unsigned int get(const unsigned int i) const {\n assert((i >> 6) < block.size());\n return (const unsigned int)(block[i >> 6] >> (i & 63)) & 1;\n }\n void build() {\n for (unsigned int i = 1; i < block.size(); i++) {\n count[i] = count[i - 1] + __builtin_popcountll(block[i - 1]);\n }\n zeros = rank0(n);\n }\n \n // the number of 1 in [0, i)\n unsigned int rank1(const unsigned int i) const {\n assert((i >> 6) < count.size());\n assert((i >> 6) < block.size());\n return count[i >> 6] +\n __builtin_popcountll(block[i >> 6] & ((1ULL << (i & 63)) - 1ULL));\n }\n // the number of 1 in [i, j)\n unsigned int rank1(const unsigned int i, const unsigned int j) const {\n return rank1(j) - rank1(i);\n }\n // the number of 0 in [0, i)\n unsigned int rank0(const unsigned int i) const {\n return i - rank1(i);\n }\n // the number of 0 in [i, j)\n unsigned int rank0(const unsigned int i, const unsigned int j) const {\n return rank0(j) - rank0(i);\n }\n // the number of 0 in [0, n)\n unsigned int rank0() const {\n return zeros;\n }\n};\n\n// Wavelet Matrix (must vec[i] >= 0)\ntemplate<class T> struct WaveletMatrix {\n // inner data\n unsigned int n, height;\n vector<T> v;\n vector<BitVector> bv;\n vector<vector<long long>> sum;\n\n // constructor (sigma: the number of characters)\n WaveletMatrix() : n(0) {}\n WaveletMatrix(unsigned int n) : n(n), v(n) {}\n WaveletMatrix(const vector<T> &vec) : n(vec.size()), v(vec) {\n build();\n }\n void add(const T &val) {\n assert(v >= 0);\n v.push_back(v);\n n = v.size();\n }\n void set(unsigned int i, const T &val) {\n assert(i >= 0 && i < n && val >= 0);\n v[i] = val;\n }\n void build() {\n assert(n == (int)v.size());\n T mv = 1;\n for (int i = 0; i < n; ++i) mv = max(mv, v[i]);\n for (height = 1; mv != 0; mv >>= 1) ++height;\n vector<int> left(n), right(n), ord(n);\n iota(ord.begin(), ord.end(), 0);\n bv.assign(height, BitVector(n));\n sum.assign(height + 1, vector<long long>(n + 1, 0));\n for (int h = height - 1; h >= 0; --h) {\n int l = 0, r = 0;\n for (int i = 0; i < n; ++i) {\n if ((v[ord[i]] >> h) & 1) {\n bv[h].set(i);\n right[r++] = ord[i];\n } else {\n left[l++] = ord[i];\n }\n }\n bv[h].build();\n ord.swap(left);\n for (int i = 0; i < r; ++i) ord[i + l] = right[i];\n for (int i = 0; i < n; ++i) sum[h][i + 1] = sum[h][i] + v[ord[i]];\n }\n }\n \n // access v[k]\n T access(int i) {\n T res = 0;\n for (int h = height - 1; h >= 0; --h) {\n int i0 = bv[h].rank0(i);\n if (bv[h].get(i)) {\n i += bv[h].rank0() - i0;\n res |= T(1) << h;\n } else {\n i = i0;\n }\n }\n return res;\n }\n T operator [] (int i) {\n return access(i);\n }\n \n // count \"i\" s.t. v[i] = val, i \\in [l, r)\n int rank(int l, int r, const T &val) {\n assert(0 <= l && l <= r && r <= n);\n for (int h = height - 1; h >= 0; --h) {\n int l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if ((val >> h) & 1) {\n l += bv[h].rank0() - l0;\n r += bv[h].rank0() - r0;\n } else {\n l = l0;\n r = r0;\n }\n }\n return r - l;\n }\n \n // count \"i\" s.t. v[i] \\in [lower, upper), i \\in [l, r)\n int range_freq(int l, int r, const T &upper) {\n assert(0 <= l && l <= r && r <= n);\n int res = 0;\n for (int h = height - 1; h >= 0; --h) {\n int l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if ((upper >> h) & 1) {\n l += bv[h].rank0() - l0;\n r += bv[h].rank0() - r0;\n res += r0 - l0;\n } else {\n l = l0;\n r = r0;\n }\n }\n return res;\n }\n int range_freq(int l, int r, const T &lower, const T &upper) {\n return range_freq(l, r, upper) - range_freq(l, r, lower);\n }\n \n // the k-th (0-indexed) smallest value in [l, r)\n T k_th_smallest(int l, int r, int k) {\n assert(0 <= l && l <= r && r <= n);\n T res = 0;\n for (int h = height - 1; h >= 0; --h) {\n int l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if (r0 - l0 <= k) {\n l += bv[h].rank0() - l0;\n r += bv[h].rank0() - r0;\n k -= r0 - l0;\n res |= T(1) << h;\n } else {\n l = l0;\n r = r0;\n }\n }\n return res;\n }\n \n // the k-th (0-indexed) largest value in [l, r)\n T k_th_largest(int l, int r, int k) {\n assert(0 <= l && l <= r && r <= n);\n return k_th_smallest(l, r, r - l - k - 1);\n }\n \n // the sum of the top-k sum in [l, r)\n T top_k_sum(int l, int r, int k) {\n assert(0 <= l && l <= r && r <= n);\n if (l == r) return 0;\n T res = 0, val = 0;\n for (int h = height - 1; h >= 0; --h) {\n int l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if (r0 - l0 <= k) {\n l += bv[h].rank0() - l0;\n r += bv[h].rank0() - r0;\n k -= r0 - l0;\n val |= T(1) << h;\n res += sum[h][r0] - sum[h][l0];\n } else {\n l = l0;\n r = r0;\n }\n }\n res += val * k;\n return res;\n }\n \n // the max value (< val) in [l, r)\n T prev_value(int l, int r, T val) {\n assert(0 <= l && l <= r && r <= n);\n int num = range_freq(l, r, 0, val);\n if (num == 0) return T(-1);\n else return k_th_smallest(l, r, num - 1);\n }\n \n // the min value (>= val) in [l, r)\n T next_value(int l, int r, T val) {\n assert(0 <= l && l <= r && r <= n);\n int num = range_freq(l, r, 0, val);\n if (num == r - l) return T(-1);\n else return k_th_smallest(l, r, num);\n }\n};\n\n\n// 2D queries\ntemplate<class POS, class VAL> struct BITonWaveletMatrix {\n // inner data\n struct BIT {\n VAL UNITY_SUM = 0;\n int N;\n vector<VAL> dat;\n \n // [0, n)\n BIT() {}\n BIT(int n, VAL unity = 0) : UNITY_SUM(unity), N(n), dat(n, unity) { }\n void init(int n) {\n N = n;\n dat.assign(n, UNITY_SUM);\n }\n \n // a is 0-indexed\n void add(int a, VAL x) {\n for (int i = a; i < (int)dat.size(); i |= i + 1)\n dat[i] = dat[i] + x;\n }\n \n // [0, a), a is 0-indexed\n VAL sum(int a) {\n VAL res = UNITY_SUM;\n for (int i = a - 1; i >= 0; i = (i & (i + 1)) - 1)\n res = res + dat[i];\n return res;\n }\n \n // [a, b), a and b are 0-indexed\n VAL sum(int a, int b) {\n return sum(b) - sum(a);\n }\n \n // debug\n void print() {\n for (int i = 0; i < (int)dat.size(); ++i)\n cout << sum(i, i + 1) << \",\";\n cout << endl;\n }\n };\n \n using Point = pair<POS, POS>;\n int n, height;\n vector<BitVector> bv;\n vector<Point> ps;\n vector<POS> ys;\n vector<BIT> bit;\n\n // constructor (sigma: the number of characters)\n // add_point() cannot be used after build()\n BITonWaveletMatrix() {}\n BITonWaveletMatrix(const vector<Point> &vec) {\n for (auto [x, y] : vec) add_point(x, y);\n build();\n }\n void add_point(POS x, POS y) {\n ps.emplace_back(x, y);\n ys.emplace_back(y);\n }\n int xid(POS x) const {\n return lower_bound(ps.begin(), ps.end(), Point(x, 0)) - ps.begin();\n }\n int yid(POS y) const {\n return lower_bound(ys.begin(), ys.end(), y) - ys.begin();\n }\n void build() {\n sort(ps.begin(), ps.end());\n ps.erase(unique(ps.begin(), ps.end()), ps.end());\n n = (int)ps.size();\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n vector<int> v(n), left(n), right(n), ord(n);\n int mv = 1;\n for (int i = 0; i < n; ++i) {\n v[i] = yid(ps[i].second);\n mv = max(mv, v[i]);\n }\n for (height = 1; mv != 0; mv >>= 1) ++height;\n iota(ord.begin(), ord.end(), 0);\n bv.resize(height, BitVector(n));\n bit.assign(height + 1, BIT(n));\n for (int h = height - 1; h >= 0; --h) {\n int l = 0, r = 0;\n for (int i = 0; i < n; ++i) {\n if ((v[ord[i]] >> h) & 1) {\n bv[h].set(i);\n right[r++] = ord[i];\n } else {\n left[l++] = ord[i];\n }\n }\n bv[h].build();\n ord.swap(left);\n for (int i = 0; i < r; ++i) ord[i + l] = right[i];\n }\n }\n \n // add\n void add(const POS x, const POS y, const VAL val) {\n int i = lower_bound(ps.begin(), ps.end(), Point(x, y)) - ps.begin();\n int j = yid(y);\n for (int h = height - 1; h >= 0; --h) {\n int i0 = bv[h].rank0(i);\n if ((j >> h) & 1) {\n i += bv[h].rank0() - i0;\n } else {\n i = i0;\n }\n bit[h].add(i, val);\n }\n }\n \n // sum\n VAL inner_sum(int l, int r, const POS upper) {\n assert(0 <= l && r <= n);\n VAL res = 0;\n for (int h = height - 1; h >= 0; --h) {\n int l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if ((upper >> h) & 1) {\n l += bv[h].rank0() - l0;\n r += bv[h].rank0() - r0;\n res += bit[h].sum(l0, r0);\n } else {\n l = l0;\n r = r0;\n }\n }\n return res;\n }\n VAL sum(const POS lx, const POS rx, const POS ly, const POS ry) {\n int l = xid(lx), r = xid(rx);\n return inner_sum(l, r, yid(ry)) - inner_sum(l, r, yid(ly));\n }\n};\n\n\n\n/*/////////////////////////////*/\n// Examples\n/*/////////////////////////////*/\n\nvoid YosupoRangeKthSmallest() {\n int N, Q;\n cin >> N >> Q;\n vector<int> a(N);\n for (int i = 0; i < N; ++i) cin >> a[i];\n \n WaveletMatrix<int> wm(a);\n for (int q = 0; q < Q; ++q) {\n int l, r, k;\n cin >> l >> r >> k;\n cout << wm.k_th_smallest(l, r, k) << endl;\n }\n}\n\nvoid YosupoStaticRangeFrequency() {\n int N, Q;\n cin >> N >> Q;\n vector<int> a(N);\n for (int i = 0; i < N; ++i) cin >> a[i];\n \n WaveletMatrix<int> wm(a);\n for (int q = 0; q < Q; ++q) {\n int l, r, x;\n cin >> l >> r >> x;\n cout << wm.rank(l, r, x) << endl;\n }\n}\n\nvoid YosupoRectangleSum() {\n BITonWaveletMatrix<int, long long> wm;\n \n int N, Q;\n cin >> N >> Q;\n vector<int> ix(N), iy(N), iw(N), lx(Q), ly(Q), rx(Q), ry(Q);\n for (int i = 0; i < N; ++i) {\n cin >> ix[i] >> iy[i] >> iw[i];\n wm.add_point(ix[i], iy[i]);\n }\n \n wm.build();\n \n for (int i = 0; i < N; ++i) {\n wm.add(ix[i], iy[i], iw[i]);\n }\n for (int q = 0; q < Q; ++q) {\n cin >> lx[q] >> ly[q] >> rx[q] >> ry[q];\n cout << wm.sum(lx[q], rx[q], ly[q], ry[q]) << endl;\n }\n}\n\nvoid YosupoJudgePointAddRectangleSum() {\n BITonWaveletMatrix<int, long long> wm;\n \n int N, Q;\n cin >> N >> Q;\n vector<int> ix(N), iy(N), iw(N), type(Q), lx(Q), ly(Q), rx(Q), ry(Q);\n for (int i = 0; i < N; ++i) {\n cin >> ix[i] >> iy[i] >> iw[i];\n wm.add_point(ix[i], iy[i]);\n }\n for (int q = 0; q < Q; ++q) {\n cin >> type[q];\n if (type[q] == 0) {\n cin >> lx[q] >> ly[q] >> rx[q];\n wm.add_point(lx[q], ly[q]);\n }\n else cin >> lx[q] >> ly[q] >> rx[q] >> ry[q];\n }\n \n wm.build();\n for (int i = 0; i < N; ++i) {\n wm.add(ix[i], iy[i], iw[i]);\n }\n for (int q = 0; q < Q; ++q) {\n if (type[q] == 0) {\n wm.add(lx[q], ly[q], rx[q]);\n } else {\n cout << wm.sum(lx[q], rx[q], ly[q], ry[q]) << endl;\n }\n }\n}\n\nvoid ABC_234_D() {\n int N, K;\n cin >> N >> K;\n vector<int> P(N);\n for (int i = 0; i < N; ++i) cin >> P[i];\n \n WaveletMatrix wm(P);\n for (int i = K; i <= N; ++i) {\n cout << wm.k_th_largest(0, i, K - 1) << endl;\n }\n}\n\nvoid ABC_281_E() {\n int N, M, K;\n cin >> N >> M >> K;\n vector<long long> A(N);\n for (int i = 0; i < N; ++i) cin >> A[i];\n \n WaveletMatrix wm(A);\n for (int i = 0; i < N - M + 1; ++i) {\n cout << wm.top_k_sum(i, i + M, K) << \" \";\n }\n cout << endl;\n}\n\nvoid ABC_324_G() {\n int N, Q;\n cin >> N;\n vector<int> A(N);\n for (int i = 0; i < N; ++i) cin >> A[i];\n \n WaveletMatrix<int> wm(A);\n\n cin >> Q;\n vector<int> left(Q+1, 0), right(Q+1, N), lower(Q+1, 1), upper(Q+1, N+1);\n for (int q = 0; q < Q; ++q) {\n int t, s, v;\n cin >> t >> s >> v;\n if (t == 1) {\n // [left, x) 内部の lower 以上 upper 未満の値が v 個以上である最小の x を求める\n int low = left[s] - 1, high = N + 1;\n while (high - low > 1) {\n int mid = (low + high) / 2;\n if (wm.range_freq(left[s], mid, lower[s], upper[s]) >= v) high = mid;\n else low = mid;\n }\n \n // [left, high) と [high, right) とに分割する\n high = min(high, right[s]);\n left[q+1] = high;\n right[q+1] = right[s];\n lower[q+1] = lower[s];\n upper[q+1] = upper[s];\n right[s] = high;\n } else {\n // 値が v 未満と v 以上に分ける\n ++v;\n v = max(v, lower[s]);\n v = min(v, upper[s]);\n left[q+1] = left[s];\n right[q+1] = right[s];\n lower[q+1] = v;\n upper[q+1] = upper[s];\n upper[s] = v;\n }\n cout << wm.range_freq(left[q+1], right[q+1], lower[q+1], upper[q+1]) << endl;\n }\n}\n\nvoid AOJ_1549() {\n int N, Q;\n cin >> N;\n const int GETA = 1100000;\n vector<int> a(N);\n for (int i = 0; i < N; ++i) {\n cin >> a[i];\n a[i] += GETA;\n }\n \n WaveletMatrix<int> wm(a);\n cin >> Q;\n while (Q--) {\n int l, r, d;\n cin >> l >> r >> d;\n ++r;\n d += GETA;\n \n int nex = wm.next_value(l, r, d);\n int pre = wm.prev_value(l, r, d);\n int res = GETA*2;\n if (nex != -1) res = min(res, nex - d);\n if (pre != -1) res = min(res, d - pre);\n cout << res << endl;\n }\n}\n\nvoid AOJ_2426() {\n BITonWaveletMatrix<long long, long long> wm;\n \n int N, M;\n cin >> N >> M;\n vector<long long> x(N), y(N), lx(M), ly(M), rx(M), ry(M);\n for (int i = 0; i < N; ++i) {\n cin >> x[i] >> y[i];\n wm.add_point(x[i], y[i]);\n }\n for (int i = 0; i < M; ++i) {\n cin >> lx[i] >> ly[i] >> rx[i] >> ry[i];\n }\n \n wm.build();\n \n for (int i = 0; i < N; ++i) {\n wm.add(x[i], y[i], 1);\n }\n for (int i = 0; i < M; ++i) {\n cout << wm.sum(lx[i], rx[i], ly[i], ry[i]) << endl;\n }\n}\n\n\nint main() {\n //YosupoRangeKthSmallest();\n //YosupoStaticRangeFrequency();\n //YosupoRectangleSum();\n //YosupoJudgePointAddRectangleSum();\n //ABC_234_D();\n //ABC_281_E();\n //ABC_324_G();\n AOJ_1549();\n //AOJ_2426();\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 25060, "score_of_the_acc": -1.8931, "final_rank": 19 }, { "submission_id": "aoj_1549_8393240", "code_snippet": "//\n// Wavelet Matrix (Preprocess: O(N log N), Query: O(log N))\n//\n// verified:\n// AtCoder ABC 234 D - Prefix K-th Max (for k_th_largest)\n// https://atcoder.jp/contests/abc234/tasks/abc234_d\n//\n// AtCoder ABC 281 E - Least Elements (for top_k_sum)\n// https://atcoder.jp/contests/abc281/tasks/abc281_e\n//\n// AtCoder ABC 324 E - G - Generate Arrays (for range_freq)\n// https://atcoder.jp/contests/abc324/tasks/abc324_g\n//\n// AOJ 1549 Hard Beans (for prev_value, next_value)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1549\n//\n// AOJ 2426 Treasure Hunt (for 2D queries)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2426\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n\n// Bit Vector\nstruct BitVector {\n // block: bit vector\n // count: the number of 1 within each block\n unsigned int n, zeros;\n vector<unsigned long long> block;\n vector<unsigned int> count;\n \n // constructor\n BitVector() {}\n BitVector(const unsigned int num) {\n resize(num);\n }\n void resize(const unsigned int num) {\n n = num;\n block.resize(((num + 1) >> 6) + 1, 0);\n count.resize(block.size(), 0);\n }\n \n // set val(0 or 1) onto i-th bit, get i-th bit of val(0 or 1)\n void set(const unsigned int i, const unsigned long long val) {\n block[i >> 6] |= (val << (i & 63));\n }\n unsigned int get(const unsigned int i) const {\n return (const unsigned int)(block[i >> 6] >> (i & 63)) & 1;\n }\n void build() {\n for (unsigned int i = 1; i < block.size(); i++) {\n count[i] = count[i - 1] + __builtin_popcountll(block[i - 1]);\n }\n zeros = rank0(n);\n }\n \n // the number of 1 in [0, i)\n unsigned int rank1(const unsigned int i) const {\n return count[i >> 6] +\n __builtin_popcountll(block[i >> 6] & ((1ULL << (i & 63)) - 1ULL));\n }\n // the number of 1 in [i, j)\n unsigned int rank1(const unsigned int i, const unsigned int j) const {\n return rank1(j) - rank1(i);\n }\n // the number of 0 in [0, i)\n unsigned int rank0(const unsigned int i) const {\n return i - rank1(i);\n }\n // the number of 0 in [i, j)\n unsigned int rank0(const unsigned int i, const unsigned int j) const {\n return rank0(j) - rank0(i);\n }\n};\n\n// Wavelet Matrix (must vec[i] >= 0)\ntemplate<class T> struct WaveletMatrix {\n // inner data\n int height;\n vector<BitVector> bv;\n vector<vector<long long>> sum;\n\n // constructor (sigma: the number of characters)\n WaveletMatrix() {}\n WaveletMatrix(vector<T> vec) :\n WaveletMatrix(vec, *max_element(vec.begin(), vec.end()) + 1) {}\n WaveletMatrix(vector<T> vec, T sigma) {\n init(vec, sigma);\n }\n void init(vector<T> &vec, T sigma) {\n height = (sigma == 1) ? 1 : (64 - __builtin_clzll(sigma - 1));\n bv.resize(height);\n vector<T> A = vec;\n sum.resize(height + 1);\n for (int h = 0; h < height; ++h) {\n bv[h].resize(vec.size());\n for (int j = 0; j < vec.size(); ++j) {\n bv[h].set(j, get(vec[j], height - h - 1));\n }\n bv[h].build();\n stable_partition(vec.begin(), vec.end(), [&](int c) {\n return !get(c, height - h - 1);\n });\n }\n for (int h = 0; h <= height; ++h) {\n sum[h].resize((int)A.size() + 1);\n for (int j = 1; j <= (int)A.size(); ++j) {\n sum[h][j] = sum[h][j - 1] + A[j - 1];\n }\n if (h == height) break;\n stable_partition(A.begin(), A.end(), [&](int c) {\n return !get(c, height - h - 1);\n });\n }\n }\n \n // the i-th bit of \"val\" (0 or 1)\n int get(const T val, const int i) {\n return val >> i & 1;\n }\n \n // the number of \"val\" in [l, r)\n int rank(const T val, const int l, const int r) {\n return rank(val, r) - rank(val, l);\n }\n \n // the number of \"val\" in [0, i)\n int rank(T val, int i) {\n int p = 0;\n for (int h = 0; h < height; ++h) {\n int p0 = bv[h].rank0(p), i0 = bv[h].rank0(i);\n if (get(val, height - h - 1)) {\n p += bv[h].zeros - p0;\n i += bv[h].zeros - i0;\n } else {\n p = p0;\n i = i0;\n }\n }\n return i - p;\n }\n \n // the k-th (0-indexed) smallest value in [l, r)\n T k_th_smallest(int k, int l, int r) {\n T res = 0;\n for (int h = 0; h < height; ++h) {\n int l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if (r0 - l0 > k) {\n l = l0;\n r = r0;\n } else {\n l += bv[h].zeros - l0;\n r += bv[h].zeros - r0;\n k -= r0 - l0;\n res |= (1LL << (height - h - 1));\n }\n }\n return res;\n }\n \n // the k-th (0-indexed) largest value in [l, r)\n T k_th_largest(int k, int l, int r) {\n return k_th_smallest(r - l - k - 1, l, r);\n }\n \n // the sum of the top-k sum in [l, r)\n T top_k_sum(int k, int l, int r) {\n if (l == r) return 0;\n T res = 0, val = 0;\n for (int h = 0; h < height; ++h) {\n int l0 = bv[h].rank0(l), r0 = bv[h].rank0(r);\n if (r0 - l0 > k) {\n l = l0;\n r = r0;\n } else {\n l += bv[h].zeros - l0;\n r += bv[h].zeros - r0;\n k -= r0 - l0;\n val |= (1LL << (height - h - 1));\n res += sum[h + 1][r0] - sum[h + 1][l0];\n }\n }\n res += (long long)val * k;\n return res;\n }\n \n // the number of value between [loewr, upper) in [l, r)\n int range_freq(const int i, const int j, const T lower, const T upper,\n const int l, const int r, const int h) {\n if (i == j || r <= lower || upper <= l) return 0;\n int mid = (l + r) >> 1;\n if (lower <= l && r <= upper) {\n return j - i;\n } else {\n int i0 = bv[h].rank0(i), j0 = bv[h].rank0(j);\n T left = range_freq(i0, j0, lower, upper, l, mid, h + 1);\n T right = range_freq(i + bv[h].zeros - i0, j + bv[h].zeros - j0,\n lower, upper, mid, r, h + 1);\n return left + right;\n }\n }\n int range_freq(const int l, const int r, const T lower, const T upper) {\n return range_freq(l, r, lower, upper, 0, 1LL << height, 0);\n }\n \n // the minmum value between [lower, upper) in [l, r)\n T range_min(const int i, const int j, const T lower, const T upper,\n const int l, const int r, const int h, const T val) {\n if (i == j || r <= lower || upper <= l) return -1;\n if (r - l == 1) return val;\n int mid = (l + r) >> 1;\n int i0 = bv[h].rank0(i), j0 = bv[h].rank0(j);\n T res = range_min(i0, j0, lower, upper, l, mid, h + 1, val);\n if (res < 0) {\n return range_min(i + bv[h].zeros - i0, j + bv[h].zeros - j0,\n lower, upper, mid, r, h + 1, val + (1LL << (height - h - 1)));\n } else return res;\n }\n T range_min(int l, int r, T lower, T upper) {\n return range_min(l, r, lower, upper, 0, 1LL << height, 0, 0);\n }\n \n // the max value (< val) in [l, r)\n T prev_value(int l, int r, T val) {\n int num = range_freq(l, r, 0, val);\n if (num == 0) return T(-1);\n else return k_th_smallest(num - 1, l, r);\n }\n \n // the min value (>= val) in [l, r)\n T next_value(int l, int r, T val) {\n int num = range_freq(l, r, 0, val);\n if (num == r - l) return T(-1);\n else return k_th_smallest(num, l, r);\n }\n};\n\n// 2D range count\ntemplate<class POS, class VAL> struct BITonWaveletMatrix {\n // inner data\n struct BIT {\n VAL UNITY_SUM = 0;\n vector<VAL> dat;\n \n // [0, n)\n BIT(int n, VAL unity = 0) : UNITY_SUM(unity), dat(n, unity) { }\n void init(int n) {\n dat.assign(n, UNITY_SUM);\n }\n \n // a is 0-indexed\n void add(int a, VAL x) {\n for (int i = a; i < (int)dat.size(); i |= i + 1)\n dat[i] = dat[i] + x;\n }\n \n // [0, a), a is 0-indexed\n VAL sum(int a) {\n VAL res = UNITY_SUM;\n for (int i = a - 1; i >= 0; i = (i & (i + 1)) - 1)\n res = res + dat[i];\n return res;\n }\n \n // [a, b), a and b are 0-indexed\n VAL sum(int a, int b) {\n return sum(b) - sum(a);\n }\n \n // debug\n void print() {\n for (int i = 0; i < (int)dat.size(); ++i)\n cout << sum(i, i + 1) << \",\";\n cout << endl;\n }\n };\n \n using Point = pair<POS, POS>;\n int n, height;\n vector<BitVector> bv;\n vector<Point> ps;\n vector<POS> ys;\n vector<int> pos;\n vector<BIT> bit;\n\n // constructor (sigma: the number of characters)\n // add_point() cannot be used after init()\n BITonWaveletMatrix() {}\n BITonWaveletMatrix(vector<Point> vec) {\n for (auto [x, y] : vec) add_point(x, y);\n init();\n }\n void add_point(POS x, POS y) {\n ps.emplace_back(x, y);\n ys.emplace_back(y);\n }\n int xid(POS x) const {\n return lower_bound(ps.begin(), ps.end(), make_pair(x, Point(0, 0)),\n [](const Point& a, const Point& b) { return a.first < b.first; }) -\n ps.begin();\n }\n int yid(POS y) const {\n return lower_bound(ys.begin(), ys.end(), y) - ys.begin();\n }\n void init() {\n sort(ps.begin(), ps.end());\n ps.erase(unique(ps.begin(), ps.end()), ps.end());\n n = (int)ps.size();\n sort(ys.begin(), ys.end());\n ys.erase(unique(ys.begin(), ys.end()), ys.end());\n vector<int> vec(n);\n for (int i = 0; i < n; ++i) vec[i] = yid(ps[i].second);\n POS sigma = *max_element(vec.begin(), vec.end()) + 1;\n height = (sigma == 1) ? 1 : (64 - __builtin_clzll(sigma - 1));\n bv.resize(height), pos.resize(height), bit.resize(height, BIT(n));\n for (int i = 0; i < height; ++i) {\n bv[i].resize(vec.size());\n for (int j = 0; j < vec.size(); ++j) {\n bv[i].set(j, get(vec[j], height - i - 1));\n }\n bv[i].build();\n auto it = stable_partition(vec.begin(), vec.end(), [&](int c) {\n return !get(c, height - i - 1);\n });\n pos[i] = it - vec.begin();\n }\n }\n \n // the i-th bit of \"y\" (0 or 1)\n int get(const POS y, const int i) {\n return y >> i & 1;\n }\n \n // the number of \"y\" in [l, r)\n int rank(const POS y, const int l, const int r) {\n return rank(y, r) - rank(y, l);\n }\n \n // the number of \"y\" in [0, i)\n int rank(const POS y, int i) {\n int p = 0;\n for (int j = 0; j < height; ++j) {\n if (get(y, height - j - 1)) {\n p = pos[j] + bv[j].rank1(p);\n i = pos[j] + bv[j].rank1(i);\n } else {\n p = bv[j].rank0(p);\n i = bv[j].rank0(i);\n }\n }\n return i - p;\n }\n \n /*\n // add\n void add(const POS x, const POS y, const VAL val) {\n int l = lower_bound(ps.begin(), ps.end(), Point(x, y)) - ps.begin();\n for (int i = height - 1; i >= 0; --i) {\n int l0 = bv[i].rank0(l);\n if (bv[i].get(l)) {\n l = pos[i] + bv[i].rank1(l);\n } else {\n l = l0;\n }\n bit[i].add(i, val);\n }\n }\n */\n \n // sum\n VAL inner_sum(int l, int r, const POS upper) const {\n VAL res = 0;\n for (int i = height; i >= 1; --i) {\n int l0 = bv[i].rank0(l), r0 = bv[i].rank0(r);\n if ((upper >> i) & 1) {\n res += bit[i].sum(l0, r0);\n l += bv[i].zeros - l0;\n r += bv[i].zeros - r0;\n } else {\n l = l0;\n r = r0;\n }\n }\n }\n \n VAL sum(const POS lx, const POS rx, const POS ly, const POS ry) const {\n lx = xid(lx), rx = xid(rx), ly = xid(ly), ry = yid(ry);\n \n }\n \n /*\n // the k-th (0-indexed) smallest value in [l, r)\n T k_th_smallest(int k, int l, int r) {\n T res = 0;\n for (int i = 0; i < height; ++i) {\n const int j = bv[i].rank0(l, r);\n if (j > k){\n l = bv[i].rank0(l);\n r = bv[i].rank0(r);\n } else {\n l = pos[i] + bv[i].rank1(l);\n r = pos[i] + bv[i].rank1(r);\n k -= j;\n res |= (1LL << (height - i - 1));\n }\n }\n return res;\n }\n \n // the k-th (0-indexed) largest value in [l, r)\n T k_th_largest(int k, int l, int r) {\n return k_th_smallest(r - l - k - 1, l, r);\n }\n \n // the number of value between [loewr, upper) in [l, r)\n int range_freq(const int i, const int j, const T lower, const T upper,\n const int l, const int r, const int x) {\n if (i == j || r <= lower || upper <= l) return 0;\n int mid = (l + r) >> 1;\n if (lower <= l && r <= upper) {\n return j - i;\n } else {\n T left = range_freq(bv[x].rank0(i), bv[x].rank0(j), lower, upper, l, mid, x + 1);\n T right = range_freq(pos[x] + bv[x].rank1(i), pos[x] + bv[x].rank1(j),\n lower, upper, mid, r, x + 1);\n return left + right;\n }\n }\n int range_freq(const int l, const int r, const T lower, const T upper) {\n return range_freq(l, r, lower, upper, 0, 1LL << height, 0);\n }\n \n // the minmum value between [lower, upper) in [l, r)\n T range_min(const int i, const int j, const T lower, const T upper,\n const int l, const int r, const int x, const T val) {\n if (i == j || r <= lower || upper <= l) return -1;\n if (r - l == 1) return val;\n int mid = (l + r) >> 1;\n T res = range_min(bv[x].rank0(i), bv[x].rank0(j), lower, upper, l, mid, x + 1, val);\n if (res < 0) {\n return range_min(pos[x] + bv[x].rank1(i), pos[x] + bv[x].rank1(j),\n lower, upper, mid, r, x + 1, val + (1LL << (height - x - 1)));\n } else return res;\n }\n T range_min(int l, int r, T lower, T upper) {\n return range_min(l, r, lower, upper, 0, 1LL << height, 0, 0);\n }\n \n // the max value (< val) in [l, r)\n T prev_value(int l, int r, T val) {\n int num = range_freq(l, r, 0, val);\n if (num == 0) return T(-1);\n else return k_th_smallest(num - 1, l, r);\n }\n \n // the min value (>= val) in [l, r)\n T next_value(int l, int r, T val) {\n int num = range_freq(l, r, 0, val);\n if (num == r - l) return T(-1);\n else return k_th_smallest(num, l, r);\n }\n */\n};\n\n/*\ntemplate<class T> struct OrthogonalRangeCount {\n // inner data\n using ptt = pair<T, T>;\n const int sz;\n vector<T> X, Y;\n WaveletMatrix<T> wm;\n \n // constructor\n OrthogonalRangeCount(vector<ptt> candidate) : sz((int)candidate.size()), X(sz), Y(sz) {\n sort(candidate.begin(), candidate.end());\n vector<int> vec(sz);\n for (int i = 0; i < sz; ++i) {\n X[i] = candidate[i].first, Y[i] = candidate[i].second;\n }\n sort(Y.begin(), Y.end());\n Y.erase(unique(Y.begin(), Y.end()), Y.end());\n for (int i = 0; i < sz; ++i) {\n vec[i] = lower_bound(Y.begin(), Y.end(), candidate[i].second) - Y.begin();\n }\n wm.init(vec, Y.size());\n }\n \n // the number of points in [lx, rx) × [ly, ry)\n int query(const T lx, const T ly, const T rx, const T ry){\n const int lxid = lower_bound(X.begin(), X.end(), lx) - X.begin();\n const int rxid = lower_bound(X.begin(), X.end(), rx) - X.begin();\n const int lyid = lower_bound(Y.begin(), Y.end(), ly) - Y.begin();\n const int ryid = lower_bound(Y.begin(), Y.end(), ry) - Y.begin();\n if (lxid >= rxid || lyid >= ryid) return 0;\n return wm.range_freq(lxid, rxid, lyid, ryid);\n }\n};\n */\n\n\n\n/*/////////////////////////////*/\n// Examples\n/*/////////////////////////////*/\n\nvoid ABC_234_D() {\n int N, K;\n cin >> N >> K;\n vector<int> P(N);\n for (int i = 0; i < N; ++i) cin >> P[i];\n \n WaveletMatrix wm(P);\n for (int i = K; i <= N; ++i) {\n cout << wm.k_th_largest(K - 1, 0, i) << endl;\n }\n}\n\nvoid ABC_281_E() {\n int N, M, K;\n cin >> N >> M >> K;\n vector<long long> A(N);\n for (int i = 0; i < N; ++i) cin >> A[i];\n \n WaveletMatrix wm(A);\n for (int i = 0; i < N - M + 1; ++i) {\n cout << wm.top_k_sum(K, i, i + M) << \" \";\n }\n cout << endl;\n}\n\nvoid ABC_324_G() {\n int N, Q;\n cin >> N;\n vector<int> A(N);\n for (int i = 0; i < N; ++i) cin >> A[i];\n \n WaveletMatrix<int> wm(A);\n cin >> Q;\n vector<int> left(Q+1, 0), right(Q+1, N), lower(Q+1, 1), upper(Q+1, N+1);\n for (int q = 0; q < Q; ++q) {\n int t, s, v;\n cin >> t >> s >> v;\n if (t == 1) {\n // [left, x) 内部の lower 以上 upper 未満の値が v 個以上である最小の x を求める\n int low = left[s] - 1, high = N + 1;\n while (high - low > 1) {\n int mid = (low + high) / 2;\n if (wm.range_freq(left[s], mid, lower[s], upper[s]) >= v) high = mid;\n else low = mid;\n }\n \n // [left, high) と [high, right) とに分割する\n high = min(high, right[s]);\n left[q+1] = high;\n right[q+1] = right[s];\n lower[q+1] = lower[s];\n upper[q+1] = upper[s];\n right[s] = high;\n } else {\n // 値が v 未満と v 以上に分ける\n ++v;\n v = max(v, lower[s]);\n v = min(v, upper[s]);\n left[q+1] = left[s];\n right[q+1] = right[s];\n lower[q+1] = v;\n upper[q+1] = upper[s];\n upper[s] = v;\n }\n cout << wm.range_freq(left[q+1], right[q+1], lower[q+1], upper[q+1]) << endl;\n }\n}\n\nvoid AOJ_1549() {\n int N, Q;\n cin >> N;\n const int GETA = 1100000;\n vector<int> a(N);\n for (int i = 0; i < N; ++i) {\n cin >> a[i];\n a[i] += GETA;\n }\n \n WaveletMatrix<int> wm(a);\n cin >> Q;\n while (Q--) {\n int l, r, d;\n cin >> l >> r >> d;\n ++r;\n d += GETA;\n \n int nex = wm.next_value(l, r, d);\n int pre = wm.prev_value(l, r, d);\n int res = GETA*2;\n if (nex != -1) res = min(res, nex - d);\n if (pre != -1) res = min(res, d - pre);\n cout << res << endl;\n }\n}\n\nvoid AOJ_2426() {\n BITonWaveletMatrix<long long, long long> wm;\n \n int N, M;\n cin >> N >> M;\n vector<long long> x(N), y(N), lx(M), ly(M), rx(M), ry(M);\n for (int i = 0; i < N; ++i) {\n cin >> x[i] >> y[i];\n wm.add_point(x[i], y[i]);\n }\n for (int i = 0; i < M; ++i) {\n cin >> lx[i] >> ly[i] >> rx[i] >> ry[i];\n ++rx[i], ++ry[i];\n wm.add_point(lx[i], ly[i]);\n wm.add_point(rx[i], ry[i]);\n }\n \n \n}\n\n\nint main() {\n //ABC_234_D();\n //ABC_281_E();\n //ABC_324_G();\n AOJ_1549();\n //AOJ_2426();\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 23592, "score_of_the_acc": -1.6024, "final_rank": 16 }, { "submission_id": "aoj_1549_8392160", "code_snippet": "//\n// Wavelet Matrix (Preprocess: O(N log N), Query: O(log N))\n//\n// verified:\n// AtCoder ABC 234 D - Prefix K-th Max (for k_th_largest)\n// https://atcoder.jp/contests/abc281/tasks/abc281_e\n//\n// AtCoder ABC 281 E - Least Elements (for top_k_sum)\n// https://atcoder.jp/contests/abc281/tasks/abc281_e\n//\n// AtCoder ABC 324 E - G - Generate Arrays (for range_freq)\n// https://atcoder.jp/contests/abc324/tasks/abc324_g\n//\n// AOJ 1549 Hard Beans (for prev_value, next_value)\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1549\n//\n\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n\n// Bit Vector\nstruct BitRank {\n // block: bit vector\n // count: the number of 1 within each block\n vector<unsigned long long> block;\n vector<unsigned int> count;\n \n // constructor\n BitRank() {}\n void resize(const unsigned int num) {\n block.resize(((num + 1) >> 6) + 1, 0);\n count.resize(block.size(), 0);\n }\n \n // set val(0 or 1) onto i-th bit\n void set(const unsigned int i, const unsigned long long val) {\n block[i >> 6] |= (val << (i & 63));\n }\n void build() {\n for (unsigned int i = 1; i < block.size(); i++) {\n count[i] = count[i - 1] + __builtin_popcountll(block[i - 1]);\n }\n }\n \n // the number of 1 in [0, i)\n unsigned int rank1(const unsigned int i) const {\n return count[i >> 6] +\n __builtin_popcountll(block[i >> 6] & ((1ULL << (i & 63)) - 1ULL));\n }\n // the number of 1 in [i, j)\n unsigned int rank1(const unsigned int i, const unsigned int j) const {\n return rank1(j) - rank1(i);\n }\n // the number of 0 in [0, i)\n unsigned int rank0(const unsigned int i) const {\n return i - rank1(i);\n }\n // the number of 0 in [i, j)\n unsigned int rank0(const unsigned int i, const unsigned int j) const {\n return rank0(j) - rank0(i);\n }\n};\n\n// Wavelet Matrix\ntemplate<class T> struct WaveletMatrix {\n // inner data\n int height;\n vector<BitRank> bv;\n vector<int> pos;\n vector<vector<long long>> rui;\n\n // constructor (sigma: the number of characters)\n WaveletMatrix() {}\n WaveletMatrix(vector<T> vec) :\n WaveletMatrix(vec, *max_element(vec.begin(), vec.end()) + 1) {}\n WaveletMatrix(vector<T> vec, T sigma) {\n init(vec, sigma);\n }\n void init(vector<T> &vec, T sigma) {\n height = (sigma == 1) ? 1 : (64 - __builtin_clzll(sigma - 1));\n bv.resize(height), pos.resize(height);\n vector<T> A = vec;\n rui.resize(height + 1);\n for (int i = 0; i < height; ++i) {\n bv[i].resize(vec.size());\n for (int j = 0; j < vec.size(); ++j) {\n bv[i].set(j, get(vec[j], height - i - 1));\n }\n bv[i].build();\n auto it = stable_partition(vec.begin(), vec.end(), [&](int c) {\n return !get(c, height - i - 1);\n });\n pos[i] = it - vec.begin();\n }\n for (int i = 0; i <= height; ++i) {\n rui[i].resize((int)A.size() + 1);\n for (int j = 1; j <= (int)A.size(); ++j) {\n rui[i][j] = rui[i][j - 1] + A[j - 1];\n }\n if (i == height) break;\n stable_partition(A.begin(), A.end(), [&](int c) {\n return !get(c, height - i - 1);\n });\n }\n }\n \n // the i-th bit of \"val\" (0 or 1)\n int get(const T val, const int i) {\n return val >> i & 1;\n }\n \n // the number of \"val\" in [l, r)\n int rank(const T val, const int l, const int r) {\n return rank(val, r) - rank(val, l);\n }\n \n // the number of \"val\" in [0, i)\n int rank(T val, int i) {\n int p = 0;\n for (int j = 0; j < height; ++j) {\n if (get(val, height - j - 1)) {\n p = pos[j] + bv[j].rank1(p);\n i = pos[j] + bv[j].rank1(i);\n } else {\n p = bv[j].rank0(p);\n i = bv[j].rank0(i);\n }\n }\n return i - p;\n }\n \n // the k-th (0-indexed) smallest value in [l, r)\n T k_th_smallest(int k, int l, int r) {\n T res = 0;\n for (int i = 0; i < height; ++i) {\n const int j = bv[i].rank0(l, r);\n if (j > k){\n l = bv[i].rank0(l);\n r = bv[i].rank0(r);\n } else {\n l = pos[i] + bv[i].rank1(l);\n r = pos[i] + bv[i].rank1(r);\n k -= j;\n res |= (1LL << (height - i - 1));\n }\n }\n return res;\n }\n \n // the k-th (0-indexed) largest value in [l, r)\n T k_th_largest(int k, int l, int r) {\n return k_th_smallest(r - l - k - 1, l, r);\n }\n \n // the sum of the top-k sum in [l, r)\n T top_k_sum(int k, int l, int r) {\n if (l == r) return 0;\n T res = 0, val = 0;\n for (int i = 0; i < height; ++i) {\n const int j = bv[i].rank0(l, r);\n if (j > k) {\n l = bv[i].rank0(l);\n r = bv[i].rank0(r);\n } else {\n int l2 = bv[i].rank0(l);\n int r2 = bv[i].rank0(r);\n res += rui[i + 1][r2] - rui[i + 1][l2];\n l = pos[i] + bv[i].rank1(l);\n r = pos[i] + bv[i].rank1(r);\n k -= j;\n val |= (1LL << (height - i - 1));\n }\n }\n res += (long long)val * k;\n return res;\n }\n \n // the number of value between [loewr, upper) in [l, r)\n int range_freq(const int i, const int j, const T lower, const T upper,\n const int l, const int r, const int x) {\n if (i == j || r <= lower || upper <= l) return 0;\n int mid = (l + r) >> 1;\n if (lower <= l && r <= upper) {\n return j - i;\n } else {\n T left = range_freq(bv[x].rank0(i), bv[x].rank0(j), lower, upper, l, mid, x + 1);\n T right = range_freq(pos[x] + bv[x].rank1(i), pos[x] + bv[x].rank1(j),\n lower, upper, mid, r, x + 1);\n return left + right;\n }\n }\n int range_freq(const int l, const int r, const T lower, const T upper) {\n return range_freq(l, r, lower, upper, 0, 1LL << height, 0);\n }\n \n // the minmum value between [lower, upper) in [l, r)\n T range_min(const int i, const int j, const T lower, const T upper,\n const int l, const int r, const int x, const T val) {\n if (i == j || r <= lower || upper <= l) return -1;\n if (r - l == 1) return val;\n int mid = (l + r) >> 1;\n T res = range_min(bv[x].rank0(i), bv[x].rank0(j), lower, upper, l, mid, x + 1, val);\n if (res < 0) {\n return range_min(pos[x] + bv[x].rank1(i), pos[x] + bv[x].rank1(j),\n lower, upper, mid, r, x + 1, val + (1LL << (height - x - 1)));\n } else return res;\n }\n T range_min(int l, int r, T lower, T upper) {\n return range_min(l, r, lower, upper, 0, 1LL << height, 0, 0);\n }\n \n // the max value (< val) in [l, r)\n T prev_value(int l, int r, T val) {\n int num = range_freq(l, r, 0, val);\n if (num == 0) return T(-1);\n else return k_th_smallest(num - 1, l, r);\n }\n \n // the min value (>= val) in [l, r)\n T next_value(int l, int r, T val) {\n int num = range_freq(l, r, 0, val);\n if (num == r - l) return T(-1);\n else return k_th_smallest(num, l, r);\n }\n};\n\n// 2D range count\ntemplate<class T> struct OrthogonalRangeCount {\n // inner data\n using ptt = pair<T, T>;\n const int sz;\n vector<T> X, Y;\n WaveletMatrix<T> wm;\n \n // constructor\n OrthogonalRangeCount(vector<ptt> candidate) : sz((int)candidate.size()), X(sz), Y(sz) {\n sort(candidate.begin(), candidate.end());\n vector<int> vec(sz);\n for (int i = 0; i < sz; ++i) {\n X[i] = candidate[i].first, Y[i] = candidate[i].second;\n }\n sort(Y.begin(), Y.end());\n Y.erase(unique(Y.begin(), Y.end()), Y.end());\n for (int i = 0; i < sz; ++i) {\n vec[i] = lower_bound(Y.begin(), Y.end(), candidate[i].second) - Y.begin();\n }\n wm.init(vec, Y.size());\n }\n \n // the number of points in [lx, rx) × [ly, ry)\n int query(const T lx, const T ly, const T rx, const T ry){\n const int lxid = lower_bound(X.begin(), X.end(), lx) - X.begin();\n const int rxid = lower_bound(X.begin(), X.end(), rx) - X.begin();\n const int lyid = lower_bound(Y.begin(), Y.end(), ly) - Y.begin();\n const int ryid = lower_bound(Y.begin(), Y.end(), ry) - Y.begin();\n if (lxid >= rxid || lyid >= ryid) return 0;\n return wm.range_freq(lxid, rxid, lyid, ryid);\n }\n};\n\n\n\n/*/////////////////////////////*/\n// Examples\n/*/////////////////////////////*/\n\nvoid ABC_234_D() {\n int N, K;\n cin >> N >> K;\n vector<int> P(N);\n for (int i = 0; i < N; ++i) cin >> P[i];\n \n WaveletMatrix wm(P);\n for (int i = K; i <= N; ++i) {\n cout << wm.k_th_largest(K - 1, 0, i) << endl;\n }\n}\n\nvoid ABC_281_E() {\n int N, M, K;\n cin >> N >> M >> K;\n vector<long long> A(N);\n for (int i = 0; i < N; ++i) cin >> A[i];\n \n WaveletMatrix wm(A);\n for (int i = 0; i < N - M + 1; ++i) {\n cout << wm.top_k_sum(K, i, i + M) << \" \";\n }\n cout << endl;\n}\n\nvoid ABC_324_G() {\n int N, Q;\n cin >> N;\n vector<int> A(N);\n for (int i = 0; i < N; ++i) cin >> A[i];\n \n WaveletMatrix<int> wm(A);\n cin >> Q;\n vector<int> left(Q+1, 0), right(Q+1, N), lower(Q+1, 1), upper(Q+1, N+1);\n for (int q = 0; q < Q; ++q) {\n int t, s, v;\n cin >> t >> s >> v;\n if (t == 1) {\n // [left, x) 内部の lower 以上 upper 未満の値が v 個以上である最小の x を求める\n int low = left[s] - 1, high = N + 1;\n while (high - low > 1) {\n int mid = (low + high) / 2;\n if (wm.range_freq(left[s], mid, lower[s], upper[s]) >= v) high = mid;\n else low = mid;\n }\n \n // [left, high) と [high, right) とに分割する\n high = min(high, right[s]);\n left[q+1] = high;\n right[q+1] = right[s];\n lower[q+1] = lower[s];\n upper[q+1] = upper[s];\n right[s] = high;\n } else {\n // 値が v 未満と v 以上に分ける\n ++v;\n v = max(v, lower[s]);\n v = min(v, upper[s]);\n left[q+1] = left[s];\n right[q+1] = right[s];\n lower[q+1] = v;\n upper[q+1] = upper[s];\n upper[s] = v;\n }\n cout << wm.range_freq(left[q+1], right[q+1], lower[q+1], upper[q+1]) << endl;\n }\n}\n\nvoid AOJ_1549() {\n int N, Q;\n cin >> N;\n const int GETA = 1100000;\n vector<int> a(N);\n for (int i = 0; i < N; ++i) {\n cin >> a[i];\n a[i] += GETA;\n }\n \n WaveletMatrix<int> wm(a);\n cin >> Q;\n while (Q--) {\n int l, r, d;\n cin >> l >> r >> d;\n ++r;\n d += GETA;\n \n int nex = wm.next_value(l, r, d);\n int pre = wm.prev_value(l, r, d);\n int res = GETA*2;\n if (nex != -1) res = min(res, nex - d);\n if (pre != -1) res = min(res, d - pre);\n \n //cout << nex << \", \" << pre << endl;\n cout << res << endl;\n }\n}\n\n\nint main() {\n //ABC_234_D();\n //ABC_281_E();\n //ABC_324_G();\n AOJ_1549();\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 23576, "score_of_the_acc": -1.6472, "final_rank": 17 }, { "submission_id": "aoj_1549_8304605", "code_snippet": "#line 1 \"combined.cpp\"\n#define PROBLEM \"https://onlinejudge.u-aizu.ac.jp/problems/1549\"\n#line 1 \"/Users/siro53/kyo-pro/compro_library/template/template.cpp\"\n#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define OVERLOAD3(_1, _2, _3, name, ...) name\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define REP1(i, n) for(int i = 0; i < int(n); i++)\n#define REP2(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define REVERSE(v) reverse(ALL(v))\n#define SZ(v) ((int)(v).size())\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD2 = 998244353;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n#line 2 \"/Users/siro53/kyo-pro/compro_library/data-structure/wavelet-matrix.hpp\"\n\n#line 5 \"/Users/siro53/kyo-pro/compro_library/data-structure/wavelet-matrix.hpp\"\n\n#line 2 \"/Users/siro53/kyo-pro/compro_library/data-structure/compact-bitvector.hpp\"\n\n#line 5 \"/Users/siro53/kyo-pro/compro_library/data-structure/compact-bitvector.hpp\"\n\nclass CompactBitVector {\n private:\n using u32 = unsigned int;\n using u64 = unsigned long long;\n\n public:\n explicit CompactBitVector() = default;\n explicit CompactBitVector(int n) : N(n), sz((N + 63) >> 6) {\n bv.assign(sz, 0);\n sum.assign(sz + 1, 0);\n }\n inline void set(int i, int b) {\n if(b) bv[i >> 6] |= (1ULL << (i & 63));\n else bv[i >> 6] &= ~(1ULL << (i & 63));\n }\n inline int access(int i) const { return (bv[i >> 6] >> (i & 63) & 1); }\n void build() {\n sum[0] = 0U;\n for(int i = 0; i < sz; i++) sum[i + 1] = sum[i] + (u32)__builtin_popcountll(bv[i]);\n }\n // [0, i)\n u32 rank(int i, int b) const {\n assert(i >= 0);\n u32 res = sum[i >> 6] + __builtin_popcountll(bv[i >> 6] & ((1ULL << (i & 63)) - 1));\n if(b == 0) res = i - res;\n return res;\n }\n int select(int i, int b) const {\n if(b and sum.back() > i) return -1;\n if(!b and N - sum.back() > i) return -1;\n int ok = N, ng = -1;\n while(ok - ng > 1) {\n int mid = (ok + ng) / 2;\n (rank(i, b) >= mid ? ok : ng) = mid;\n }\n return ok;\n }\n inline int operator[](const int i) const { return access(i); }\n\n private:\n int N, sz;\n std::vector<u64> bv;\n std::vector<u32> sum;\n};\n#line 7 \"/Users/siro53/kyo-pro/compro_library/data-structure/wavelet-matrix.hpp\"\n\ntemplate <typename T, int BITLEN = 31> \nclass WaveletMatrix {\npublic:\n explicit WaveletMatrix() = default;\n explicit WaveletMatrix(vector<int> v): N((int)v.size()), B(BITLEN, CompactBitVector(N)), zero_num(BITLEN, 0) {\n for(int k = BITLEN - 1; k >= 0; k--) {\n std::vector<T> zeros, ones;\n for(int i = 0; i < N; i++) {\n if(v[i] >> k & 1) {\n ones.emplace_back(v[i]);\n B[k].set(i, 1);\n } else {\n zeros.emplace_back(v[i]);\n }\n }\n B[k].build();\n zero_num[k] = (int)zeros.size();\n for(int i = 0; i < N; i++) {\n if(i < (int)zeros.size()) v[i] = zeros[i];\n else v[i] = ones[i - (int)zeros.size()];\n }\n }\n }\n T access(int pos) const {\n T res = 0; \n for(int k = BITLEN - 1; k >= 0; k--) {\n int b = B[k][pos];\n res |= (T(b) << k);\n pos = B[k].rank(pos, b) + zero_num[k] * b;\n }\n return pos;\n }\n // [0, i) の範囲内に値 x が何個出現したか. O(log(σ))\n int rank(int i, T x) {\n int l = 0, r = i;\n for(int k = BITLEN - 1; k >= 0; k--) {\n int b = (x >> k & 1);\n l = B[k].rank(l, b) + zero_num[k] * b;\n r = B[k].rank(r, b) + zero_num[k] * b;\n }\n return (r - l);\n };\n // 左から i 個目の値 x の index. O(log(N)log(σ))\n int select(int i, T x) {\n int pos = 0;\n for(int k = BITLEN - 1; k >= 0; k--) {\n int b = (x >> k) & 1;\n pos = B[k].rank(pos, b) + zero_num[k] * b;\n }\n pos += i;\n for(int k = 0; k < BITLEN; k++) {\n if(x >> k & 1) {\n pos = B[k].select(pos - zero_num[k], 1);\n } else {\n pos = B[k].select(pos, 0);\n }\n }\n return pos;\n }\n // [l, r) の中で i 番目に小さい値を返す (i は 0-indexed)\n T quantile(int l, int r, int i) {\n assert(0 <= i and i < r - l);\n T res = 0;\n for(int k = BITLEN - 1; k >= 0; k--) {\n int zero_cnt = B[k].rank(r, 0) - B[k].rank(l, 0);\n int b = (zero_cnt <= i);\n if(b) {\n res |= (T(1) << k);\n i -= zero_cnt;\n }\n l = B[k].rank(l, b) + zero_num[k] * b;\n r = B[k].rank(r, b) + zero_num[k] * b;\n }\n return res;\n }\n // [l, r) の中で x < xr を満たす x の個数の総和を返す\n int rangefreq(int l, int r, T xr) {\n int res = 0;\n for(int k = BITLEN - 1; k >= 0; k--) {\n int b = (xr >> k & 1);\n if(b) res += B[k].rank(r, 0) - B[k].rank(l, 0);\n l = B[k].rank(l, b) + zero_num[k] * b;\n r = B[k].rank(r, b) + zero_num[k] * b;\n }\n return res;\n }\n // [l, r) の中で xl <= x < xr を満たす x の個数の総和を返す\n int rangefreq(int l, int r, T xl, T xr) {\n return (rangefreq(l, r, xr) - rangefreq(l, r, xl));\n }\n // [l, r) の中で x < xr を満たす x のうち最大値を返す\n // そのような x が存在しないならば -1 を返す\n T prev_value(int l, int r, T xr) {\n int num = rangefreq(l, r, xr);\n return (num == 0 ? -1 : quantile(l, r, num - 1));\n }\n // [l, r) の中で xl <= x を満たす x のうち最小値を返す\n // そのような x が存在しないならば -1 を返す\n T next_value(int l, int r, T xl) {\n int num = rangefreq(l, r, xl);\n return (num == r - l ? -1 : quantile(l, r, num));\n }\n T operator[](const int i) const { return access(i); }\nprivate:\n int N;\n std::vector<CompactBitVector> B;\n std::vector<int> zero_num;\n};\n#line 4 \"combined.cpp\"\n\nint main() {\n const int OFFSET = 1000000;\n int N;\n cin >> N;\n vector<int> a(N);\n REP(i, N) {\n cin >> a[i];\n a[i] += OFFSET;\n }\n WaveletMatrix<int> wm(a);\n int Q;\n cin >> Q;\n REP(_, Q) {\n int l, r, D;\n cin >> l >> r >> D;\n r++;\n D += OFFSET;\n int res = INF;\n int x = wm.prev_value(l, r, D);\n if(x != -1) chmin(res, abs(x - D));\n x = wm.next_value(l, r, D);\n if(x != -1) chmin(res, abs(x - D));\n cout << res << '\\n';\n }\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 5324, "score_of_the_acc": -0.5399, "final_rank": 10 }, { "submission_id": "aoj_1549_8302973", "code_snippet": "#line 1 \"combined.cpp\"\n#pragma region Macros\n#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T> inline bool chmax(T &a, T b) {\n if(a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T> inline bool chmin(T &a, T b) {\n if(a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\n\nstruct Setup {\n Setup() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n }\n} __Setup;\n\nusing ll = long long;\n#define OVERLOAD3(_1, _2, _3, name, ...) name\n#define ALL(v) (v).begin(), (v).end()\n#define RALL(v) (v).rbegin(), (v).rend()\n#define REP1(i, n) for(int i = 0; i < int(n); i++)\n#define REP2(i, a, b) for(int i = (a); i < int(b); i++)\n#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define REVERSE(v) reverse(ALL(v))\n#define SZ(v) ((int)(v).size())\nconst int INF = 1 << 30;\nconst ll LLINF = 1LL << 60;\nconstexpr int MOD = 1000000007;\nconstexpr int MOD2 = 998244353;\nconst int dx[4] = {1, 0, -1, 0};\nconst int dy[4] = {0, 1, 0, -1};\n\nvoid Case(int i) { cout << \"Case #\" << i << \": \"; }\nint popcount(int x) { return __builtin_popcount(x); }\nll popcount(ll x) { return __builtin_popcountll(x); }\n#pragma endregion Macros\n\n#line 2 \"/Users/siro53/kyo-pro/compro_library/data-structure/wavlet_matrix.hpp\"\n\n#line 5 \"/Users/siro53/kyo-pro/compro_library/data-structure/wavlet_matrix.hpp\"\n\n#line 2 \"/Users/siro53/kyo-pro/compro_library/data-structure/compact_bitvector.hpp\"\n\n#line 5 \"/Users/siro53/kyo-pro/compro_library/data-structure/compact_bitvector.hpp\"\n\nclass CompactBitVector {\n private:\n using u32 = unsigned int;\n using u64 = unsigned long long;\n\n public:\n explicit CompactBitVector() = default;\n explicit CompactBitVector(int n) : N(n), sz((N + 63) >> 6) {\n bv.assign(sz, 0);\n sum.assign(sz + 1, 0);\n }\n inline void set(int i, int b) {\n if(b) bv[i >> 6] |= (1ULL << (i & 63));\n else bv[i >> 6] &= ~(1ULL << (i & 63));\n }\n inline int access(int i) const { return (bv[i >> 6] >> (i & 63) & 1); }\n void build() {\n sum[0] = 0U;\n for(int i = 0; i < sz; i++) sum[i + 1] = sum[i] + (u32)__builtin_popcountll(bv[i]);\n }\n // [0, i)\n u32 rank(int i, int b) const {\n assert(i >= 0);\n u32 res = sum[i >> 6] + __builtin_popcountll(bv[i >> 6] & ((1ULL << (i & 63)) - 1));\n if(b == 0) res = i - res;\n return res;\n }\n int select(int i, int b) const {\n if(b and sum.back() > i) return -1;\n if(!b and N - sum.back() > i) return -1;\n int ok = N, ng = -1;\n while(ok - ng > 1) {\n int mid = (ok + ng) / 2;\n (rank(i, b) >= mid ? ok : ng) = mid;\n }\n return ok;\n }\n inline int operator[](const int i) const { return access(i); }\n\n private:\n int N, sz;\n std::vector<u64> bv;\n std::vector<u32> sum;\n};\n#line 7 \"/Users/siro53/kyo-pro/compro_library/data-structure/wavlet_matrix.hpp\"\n\ntemplate <typename T, int BITLEN = 31> \nclass WaveletMatrix {\npublic:\n explicit WaveletMatrix() = default;\n explicit WaveletMatrix(vector<int> v): N((int)v.size()), B(BITLEN, CompactBitVector(N)), zero_num(BITLEN, 0) {\n for(int k = BITLEN - 1; k >= 0; k--) {\n std::vector<T> zeros, ones;\n for(int i = 0; i < N; i++) {\n if(v[i] >> k & 1) {\n ones.emplace_back(v[i]);\n B[k].set(i, 1);\n } else {\n zeros.emplace_back(v[i]);\n }\n }\n B[k].build();\n zero_num[k] = (int)zeros.size();\n for(int i = 0; i < N; i++) {\n if(i < (int)zeros.size()) v[i] = zeros[i];\n else v[i] = ones[i - (int)zeros.size()];\n }\n }\n }\n T access(int pos) const {\n T res = 0; \n for(int k = BITLEN - 1; k >= 0; k--) {\n int b = B[k][pos];\n res |= (T(b) << k);\n pos = B[k].rank(pos, b) + zero_num[k] * b;\n }\n return pos;\n }\n // [0, i) の範囲内に値 x が何個出現したか. O(log(σ))\n int rank(int i, T x) {\n int l = 0, r = i;\n for(int k = BITLEN - 1; k >= 0; k--) {\n int b = (x >> k & 1);\n l = B[k].rank(l, b) + zero_num[k] * b;\n r = B[k].rank(r, b) + zero_num[k] * b;\n // std::cerr << k << ' ' << l << ' ' << r << std::endl;\n }\n return (r - l);\n };\n // 左から i 個目の値 x の index. O(log(N)log(σ))\n int select(int i, T x) {\n int pos = 0;\n for(int k = BITLEN - 1; k >= 0; k--) {\n int b = (x >> k) & 1;\n pos = B[k].rank(pos, b) + zero_num[k] * b;\n }\n pos += i;\n for(int k = 0; k < BITLEN; k++) {\n if(x >> k & 1) {\n pos = B[k].select(pos - zero_num[k], 1);\n } else {\n pos = B[k].select(pos, 0);\n }\n }\n return pos;\n }\n // [l, r) の中で i 番目に小さい値を返す (i は 0-indexed)\n T quantile(int l, int r, int i) {\n assert(0 <= i and i < r - l);\n T res = 0;\n for(int k = BITLEN - 1; k >= 0; k--) {\n int zero_cnt = B[k].rank(r, 0) - B[k].rank(l, 0);\n int b = (zero_cnt <= i);\n if(b) {\n res |= (T(1) << k);\n i -= zero_cnt;\n }\n l = B[k].rank(l, b) + zero_num[k] * b;\n r = B[k].rank(r, b) + zero_num[k] * b;\n }\n return res;\n }\n // [l, r) の中で x < xr を満たす x の個数の総和を返す\n int rangefreq(int l, int r, T xr) {\n int res = 0;\n for(int k = BITLEN - 1; k >= 0; k--) {\n int b = (xr >> k & 1);\n if(b) res += B[k].rank(r, 0) - B[k].rank(l, 0);\n l = B[k].rank(l, b) + zero_num[k] * b;\n r = B[k].rank(r, b) + zero_num[k] * b;\n }\n return res;\n }\n // [l, r) の中で xl <= x < xr を満たす x の個数の総和を返す\n int rangefreq(int l, int r, T xl, T xr) {\n return (rangefreq(l, r, xr) - rangefreq(l, r, xl));\n }\n // [l, r) の中で x < xr を満たす x のうち最大値を返す\n // そのような x が存在しないならば -1 を返す\n T prev_value(int l, int r, T xr) {\n int num = rangefreq(l, r, xr);\n return (num == 0 ? -1 : quantile(l, r, num - 1));\n }\n // [l, r) の中で xl <= x を満たす x のうち最小値を返す\n // そのような x が存在しないならば -1 を返す\n T next_value(int l, int r, T xl) {\n int num = rangefreq(l, r, xl);\n return (num == r - l ? -1 : quantile(l, r, num));\n }\n T operator[](const int i) const { return access(i); }\nprivate:\n int N;\n std::vector<CompactBitVector> B;\n std::vector<int> zero_num;\n};\n#line 78 \"combined.cpp\"\n\nint main() {\n int N;\n cin >> N;\n vector<int> a(N);\n REP(i, N) cin >> a[i];\n WaveletMatrix<int, 21> wm(a);\n int Q;\n cin >> Q;\n REP(_, Q) {\n int l, r, D;\n cin >> l >> r >> D;\n r++;\n int x1 = wm.prev_value(l, r, D);\n int x2 = wm.next_value(l, r, D);\n assert(x1 != -1 or x2 != -1);\n if(x1 == -1) cout << abs(x2 - D) << '\\n';\n else if(x2 == -1) cout << abs(x1 - D) << '\\n';\n else cout << (abs(x1 - D) < abs(x2 - D) ? abs(x1 - D) : abs(x2 - D)) << '\\n';\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 5300, "score_of_the_acc": -0.3116, "final_rank": 6 } ]
aoj_1552_cpp
Problem B: Mountain Climbing Problem なりよし君は山登りの計画を立てています。計画はとても大切です。高原・盆地が多いのか、峰・窪地が多いのか、山腹があるのか、状況によって用意する荷物も変わってきます。 なりよし君が持っている登山ガイドブックには、今回登山に利用する道の一定の距離毎の標高がスタート地点からゴール地点まで順に書かれています。 ガイドブックに書かれている標高をスタート地点からゴール地点まで順に a 1 , a 2 , a 3 , ..., a N とし、高原、盆地、山腹、峰、窪地を次のように定義します: 高原 a i-1 < a i = a i+1 = ... = a j > a j+1 ( 1 < i < j < N ) 盆地 a i-1 > a i = a i+1 = ... = a j < a j+1 ( 1 < i < j < N ) 山腹 a i-1 < a i = a i+1 = ... = a j < a j+1 ( 1 < i < j < N ) もしくは a i-1 > a i = a i+1 = ... = a j > a j+1 ( 1 < i < j < N ) 峰 a i-1 < a i > a i+1 ( 1 < i < N ) 窪地 a i-1 > a i < a i+1 ( 1 < i < N ) 高原 盆地 山腹 山腹 峰 窪地 あなたは、なりよし君の為に、高原・盆地・山腹・峰・窪地、それぞれの数を計算するプログラムを書くことにしました。 Input 入力は次のような形式で与えられる: N a 1 a 2 a 3 ... a N N は与えられる標高の数を表す整数である。 a i は標高を表す整数である( 1 ≤ i ≤ N )。それぞれ空白区切りで与えられる。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 100000 -100000 ≤ a i ≤ 100000 Output 高原の数、盆地の数、山腹、峰、窪地の数を順番に空白区切りで一行に出力せよ。 Sample Input 1 5 1 2 3 4 3 Sample Output 1 0 0 0 1 0 Sample Input 2 12 10 5 15 15 20 20 12 12 8 3 3 5 Sample Output 2 1 1 2 0 1
[ { "submission_id": "aoj_1552_5550112", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvector<pair<char,int>> runlength(vector<char> vs){\n\tvector<pair<char,int>> res;\n\tfor(auto v:vs){\n\t\tif(res.empty() || res.back().first!=v) res.emplace_back(v,0);\n\t\tres.back().second++;\n\t}\n\treturn res;\n}\n\nint main(){\n\tint n;\n\tcin>>n;\n\tvector<int> a(n);\n\tfor(auto &A:a)cin>>A;\n\n\tvector<char> less_greater(n-1);\n\tfor(int i=0;i<n-1;i++){\n\t\tif(a[i]==a[i+1]) less_greater[i]='=';\n\t\tif(a[i]>a[i+1]) less_greater[i]='>';\n\t\tif(a[i]<a[i+1]) less_greater[i]='<';\n\t}\n\n\tauto vs=runlength(less_greater);\n\n\tvector<int> ans(5,0);\n\t// kougen\n\tfor(int i=1;i<(int)vs.size()-1;i++){\n\t\tif(vs[i-1].first=='<' && vs[i].first=='=' && vs[i+1].first=='>'){\n\t\t\tans[0]++;\n\t\t}\n\t}\n\n\t// bonti\n\tfor(int i=1;i<(int)vs.size()-1;i++){\n\t\tif(vs[i-1].first=='>' && vs[i].first=='=' && vs[i+1].first=='<'){\n\t\t\tans[1]++;\n\t\t}\n\t}\n\n\t// sanpuku\n\tfor(int i=1;i<(int)vs.size()-1;i++){\n\t\tif(vs[i-1].first==vs[i+1].first && vs[i].first=='='){\n\t\t\tans[2]++;\n\t\t}\n\t}\n\n\t// mine\n\tfor(int i=0;i<(int)vs.size()-1;i++){\n\t\tif(vs[i].first=='<' && vs[i+1].first=='>'){\n\t\t\tans[3]++;\n\t\t}\n\t}\n\n\t// kuboti\n\tfor(int i=0;i<(int)vs.size()-1;i++){\n\t\tif(vs[i].first=='>' && vs[i+1].first=='<'){\n\t\t\tans[4]++;\n\t\t}\n\t}\n\n\t\n\tfor(int i=0;i<(int)ans.size();i++){\n\t\tif(i) cout<<\" \";\n\t\tcout<<ans[i];\n\t}\n\tcout<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4680, "score_of_the_acc": -0.7744, "final_rank": 13 }, { "submission_id": "aoj_1552_4542112", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nusing llong = long long;\n\nllong n;\nvector<llong> a;\nvector<pair<llong, llong>> p;\n\nint main() {\n cin >> n;\n a.resize(n);\n for (int i = 0; i < n; i++) {\n cin >> a[i];\n }\n\n p.emplace_back(a[0], 1);\n for (int i = 1; i < n; i++) {\n if (p.back().first == a[i]) p.back().second++;\n else p.emplace_back(a[i], 1);\n }\n\n llong kougen, bonti, sanpuku, mine, kuboti;\n kougen = bonti = sanpuku = mine = kuboti = 0;\n\n for (int i = 1; i < (int)p.size() - 1; i++) {\n if (p[i - 1].first < p[i].first && p[i].first > p[i + 1].first) {\n if (p[i].second == 1) mine++;\n else kougen++;\n }\n else if (p[i - 1].first > p[i].first && p[i].first < p[i + 1].first) {\n if (p[i].second == 1) kuboti++;\n else bonti++;\n }\n else if (p[i].second > 1) {\n sanpuku++;\n }\n }\n\n printf(\"%lld %lld %lld %lld %lld\\n\", kougen, bonti, sanpuku, mine, kuboti);\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5704, "score_of_the_acc": -1, "final_rank": 14 }, { "submission_id": "aoj_1552_4541880", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n int N;\n cin >> N;\n vector<int> a(N);\n for (int i = 0; i < N; i++){\n cin >> a[i];\n }\n vector<int> b;\n for (int i = 0; i < N; i++){\n b.push_back(a[i]);\n if (i >= 2){\n if (b[b.size() - 3] == b[b.size() - 2] && b[b.size() - 2] == b[b.size() - 1]){\n b.pop_back();\n }\n }\n }\n int M = b.size();\n array<int, 5> cnt{0, 0, 0, 0, 0};\n for (int i = 0; i < M - 3; i++){\n if (b[i] < b[i + 1] && b[i + 1] == b[i + 2] && b[i + 2] > b[i + 3]){\n cnt[0]++;\n }\n if (b[i] > b[i + 1] && b[i + 1] == b[i + 2] && b[i + 2] < b[i + 3]){\n cnt[1]++;\n }\n if (b[i] < b[i + 1] && b[i + 1] == b[i + 2] && b[i + 2] < b[i + 3]){\n cnt[2]++;\n }\n if (b[i] > b[i + 1] && b[i + 1] == b[i + 2] && b[i + 2] > b[i + 3]){\n cnt[2]++;\n }\n }\n for (int i = 0; i < M - 2; i++){\n if (b[i] < b[i + 1] && b[i + 1] > b[i + 2]){\n cnt[3]++;\n }\n if (b[i] > b[i + 1] && b[i + 1] < b[i + 2]){\n cnt[4]++;\n }\n }\n cout << cnt[0] << ' ' << cnt[1] << ' ' << cnt[2] << ' ' << cnt[3] << ' ' << cnt[4] << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3828, "score_of_the_acc": -0.5868, "final_rank": 10 }, { "submission_id": "aoj_1552_4384343", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nint main() {\n int N;\n cin >> N;\n\n vector<int> height{-100001};\n vector<int> count{0};\n\n int a;\n for (int i = 0; i < N; i++) {\n cin >> a;\n if (a != height.back()) {\n height.push_back(a);\n count.push_back(1);\n } else {\n *(count.end() - 1) = count.back() + 1;\n }\n }\n\n int ans[5]{};\n\n if (height.size() > 3) {\n for (int i = 2; i < height.size() - 1; i++) {\n if (height[i - 1] < height[i] && height[i] > height[i + 1] && count[i] > 1) {\n //高原\n ans[0]++;\n } else if (height[i - 1] > height[i] && height[i] < height[i + 1] && count[i] > 1) {\n //盆地\n ans[1]++;\n } else if (height[i - 1] < height[i] && height[i] < height[i + 1] && count[i] > 1) {\n //山腹(登り)\n ans[2]++;\n } else if (height[i - 1] > height[i] && height[i] > height[i + 1] && count[i] > 1) {\n //山腹(降り)\n ans[2]++;\n } else if (height[i - 1] < height[i] && height[i] > height[i + 1] && count[i] == 1) {\n //峰\n ans[3]++;\n } else if (height[i - 1] > height[i] && height[i] < height[i + 1] && count[i] == 1) {\n //窪地\n ans[4]++;\n }\n }\n }\n\n cout << ans[0];\n for (int i = 1; i < 5; i++) {\n cout << \" \" << ans[i];\n }\n cout << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3832, "score_of_the_acc": -0.5877, "final_rank": 11 }, { "submission_id": "aoj_1552_3560443", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n\n\nint main() {\n\tint N;\n\tcin >> N;\n\tvector<int> a(N);\n\tfor (int i = 0; i < N; i++)cin >> a[i];\n\n\tint kougen = 0, bonti = 0, sanhuku = 0, mine = 0, kuboti = 0;\n\tfor (int i = 1; i < N - 1; i++) {\n\t\tif (a[i - 1]<a[i] && a[i]>a[i + 1])mine++;\n\t\tif (a[i - 1] > a[i] && a[i] < a[i + 1])kuboti++;\n\t}\n\tfor (int i = 1; i < N - 1; i++) {\n\t\tif (a[i - 1] != a[i] && a[i] == a[i + 1]) {\n\t\t\tint j = i + 1;\n\t\t\twhile (j < N - 1 && a[j] == a[j + 1]) {\n\t\t\t\tj++;\n\t\t\t}\n\t\t\tif (j == N - 1)break;\n\t\t\tif (a[i - 1] < a[i]) {\n\t\t\t\t(a[j] < a[j + 1] ? sanhuku : kougen)++;\n\t\t\t}\n\t\t\telse {\n\t\t\t\t(a[j] > a[j + 1] ? sanhuku : bonti)++;\n\t\t\t}\n\t\t\ti = j;\n\t\t}\n\t}\n\tcout << kougen << \" \" << bonti << \" \" << sanhuku << \" \" << mine << \" \" << kuboti << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3088, "score_of_the_acc": -0.4238, "final_rank": 6 }, { "submission_id": "aoj_1552_3416590", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n\nint main(){\n int n;\n cin >> n;\n vector<int> v(n), x(n-1);\n for(int i = 0; i < n; i++) cin >> v[i];\n for(int i = 0; i < n-1; i++) x[i] = v[i+1]-v[i];\n int a = 0, b = 0, c = 0, d = 0, e = 0;\n for(int i = 1; i < n-1; i++){\n if(x[i-1] < 0 && x[i] > 0) e++;\n if(x[i-1] > 0 && x[i] < 0) d++;\n }\n int cur = 0;\n while(cur < n-1 && x[cur] == 0) cur++;\n\n while(cur < n-1){\n if(x[cur] != 0){\n cur++;\n continue;\n }\n int next = cur;\n while(next < n-1 && x[next] == 0) next++;\n if(next == n-1) break;\n if(x[cur-1] < 0 && x[next] > 0) b++;\n else if(x[cur-1] > 0 && x[next] < 0) a++;\n else c++;\n cur = next;\n }\n printf(\"%d %d %d %d %d\\n\", a, b, c, d, e);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3556, "score_of_the_acc": -0.5269, "final_rank": 9 }, { "submission_id": "aoj_1552_3324812", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nint main(){\n int n;\n cin >>n;\n vector<int> a(n);\n rep(i,n) cin >>a[i];\n\n auto check = [&](int l, int r){\n if(l==r){\n if(a[l-1]<a[l] && a[l]>a[l+1]) return 3;\n if(a[l-1]>a[l] && a[l]<a[l+1]) return 4;\n return -1;\n }\n\n if(a[l-1]<a[l] && a[r]>a[r+1]) return 0;\n if(a[l-1]>a[l] && a[r]<a[r+1]) return 1;\n if(a[l-1]<a[l] && a[r]<a[r+1]) return 2;\n if(a[l-1]>a[l] && a[r]>a[r+1]) return 2;\n return -1;\n };\n\n int ct[5]={};\n\n int i=1;\n while(i<n-1){\n int j = i;\n while(j<n-1 && a[i]==a[j]) ++j;\n --j;\n\n int res = check(i,j);\n if(res != -1) ++ct[res];\n\n i = j+1;\n }\n\n rep(z,5) cout << ct[z] << \" \\n\"[z==4];\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3116, "score_of_the_acc": -0.43, "final_rank": 8 }, { "submission_id": "aoj_1552_3269213", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\n\n\nstruct UnionFind {\n\tvector<int> data;\n\tUnionFind(int size) : data(size, -1) { }\n\tbool unionSet(int x, int y) {\n\t\tx = root(x); y = root(y);\n\t\tif (x != y) {\n\t\t\tif (data[y] < data[x]) swap(x, y);\n\t\t\tdata[x] += data[y]; data[y] = x;\n\t\t}\n\t\treturn x != y;\n\t}\n\tint root(int x) {\n\t\treturn data[x] < 0 ? x : data[x] = root(data[x]);\n\t}\n\tint size(int x) {\n\t\treturn -data[root(x)];\n\t}\n};\n\nint main() {\n\tint N;cin>>N;\n\tvector<int>hs(N);\n\tfor(int i=0;i<N;++i)cin>>hs[i];\n\tvector<int>ds(N-1);\n\tfor(int i=0;i<N-1;++i)ds[i]=hs[i+1]-hs[i];\n\n\tvector<int>nextds;\n\tbool flag=true;\n\tfor (int i = 0; i < N - 1; ++i) {\n\t\tif(ds[i])nextds.push_back(ds[i]>0?1:-1);\n\t\telse {\n\t\t\tif (!flag) {\n\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnextds.push_back(ds[i]);\n\t\t\t}\n\t\t}\n\t\tflag=ds[i];\n\t}\n\tvector<int>anss(5);\n\tfor (int i = 0; i < int(nextds.size()) - 1; ++i) {\n\t\tif(nextds[i]==0)continue;\n\t\telse {\n\t\t\tif (nextds[i] < 0) {\n\t\t\t\tif (nextds[i + 1] == 0) {\n\t\t\t\t\tif(i+2==nextds.size())continue;\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (nextds[i + 2] < 0) {\n\t\t\t\t\t\t\tanss[2]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tassert(nextds[i+2]>0);\n\t\t\t\t\t\t\tanss[1]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif(nextds[i+1]>0)anss[4]++;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (nextds[i + 1] == 0) {\n\t\t\t\t\tif (i + 2 == nextds.size())continue;\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (nextds[i + 2] < 0) {\n\t\t\t\t\t\t\tanss[0]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tassert(nextds[i + 2]>0);\n\t\t\t\t\t\t\tanss[2]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (nextds[i + 1]<0)anss[3]++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < 5; ++i) {\n\t\tcout<<anss[i];\n\t\tif(i==4)cout<<endl;\n\t\telse cout<<\" \";\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4324, "score_of_the_acc": -0.696, "final_rank": 12 }, { "submission_id": "aoj_1552_3269211", "code_snippet": "#include \"bits/stdc++.h\"\n\n#pragma warning(disable:4996)\nusing namespace std;\n\n\nstruct UnionFind {\n\tvector<int> data;\n\tUnionFind(int size) : data(size, -1) { }\n\tbool unionSet(int x, int y) {\n\t\tx = root(x); y = root(y);\n\t\tif (x != y) {\n\t\t\tif (data[y] < data[x]) swap(x, y);\n\t\t\tdata[x] += data[y]; data[y] = x;\n\t\t}\n\t\treturn x != y;\n\t}\n\tint root(int x) {\n\t\treturn data[x] < 0 ? x : data[x] = root(data[x]);\n\t}\n\tint size(int x) {\n\t\treturn -data[root(x)];\n\t}\n};\n\nint main() {\n\tint N;cin>>N;\n\tvector<int>hs(N);\n\tfor(int i=0;i<N;++i)cin>>hs[i];\n\tvector<int>ds(N-1);\n\tfor(int i=0;i<N-1;++i)ds[i]=hs[i+1]-hs[i];\n\n\tvector<int>nextds;\n\tbool flag=true;\n\tfor (int i = 0; i < N - 1; ++i) {\n\t\tif(ds[i])nextds.push_back(ds[i]>0?1:-1);\n\t\telse {\n\t\t\tif (!flag) {\n\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnextds.push_back(ds[i]);\n\t\t\t}\n\t\t}\n\t\tflag=ds[i];\n\t}\n\tvector<int>anss(5);\n\tfor (int i = 0; i < nextds.size() - 1; ++i) {\n\t\tif(nextds[i]==0)continue;\n\t\telse {\n\t\t\tif (nextds[i] < 0) {\n\t\t\t\tif (nextds[i + 1] == 0) {\n\t\t\t\t\tif(i+2==nextds.size())continue;\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (nextds[i + 2] < 0) {\n\t\t\t\t\t\t\tanss[2]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tassert(nextds[i+2]>0);\n\t\t\t\t\t\t\tanss[1]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif(nextds[i+1]>0)anss[4]++;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (nextds[i + 1] == 0) {\n\t\t\t\t\tif (i + 2 == nextds.size())continue;\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (nextds[i + 2] < 0) {\n\t\t\t\t\t\t\tanss[0]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tassert(nextds[i + 2]>0);\n\t\t\t\t\t\t\tanss[2]++;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (nextds[i + 1]<0)anss[3]++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i < 5; ++i) {\n\t\tcout<<anss[i];\n\t\tif(i==4)cout<<endl;\n\t\telse cout<<\" \";\n\t}\n\treturn 0;\n}", "accuracy": 0.8809523809523809, "time_ms": 10, "memory_kb": 4324, "score_of_the_acc": -0.696, "final_rank": 20 }, { "submission_id": "aoj_1552_3112859", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF=1<<29;\n \nint main() {\n int n,d[5];\n cin >> n;\n vector<int> a(n);\n for(int i=0; i<n; i++) cin >> a[i];\n for(int i=0; i<n-2; i++) {\n if(a[i]==a[i+1] && a[i]==a[i+2]) a[i]=INF;\n }\n a.erase(remove(a.begin(),a.end(),INF),a.end());\n memset(d,0,sizeof(d));\n for(int i=2; i<a.size(); i++) {\n if(i>=3 && a[i-2]==a[i-1]) {\n if(a[i-2]>a[i-3]) {\n if(a[i]<a[i-1]) d[0]++;\n else if(a[i]>a[i-1]) d[2]++;\n } else {\n if(a[i]>a[i-1]) d[1]++;\n else if(a[i]<a[i-1]) d[2]++;\n }\n }\n if(a[i-1]>a[i-2] && a[i]<a[i-1]) d[3]++;\n if(a[i-1]<a[i-2] && a[i]>a[i-1]) d[4]++;\n }\n for(int i=0; i<5; i++) {\n if(i) cout << \" \";\n cout << d[i];\n }\n cout << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3108, "score_of_the_acc": -0.4282, "final_rank": 7 }, { "submission_id": "aoj_1552_1669114", "code_snippet": "/*\n * b.cc: \n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n\nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\n\n/* typedef */\n\n/* global variables */\n\nint as[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n;\n cin >> n;\n for (int i = 0; i < n; i++) cin >> as[i];\n\n int ko = 0, bo = 0, sa = 0, mi = 0, ku = 0;\n int pa = as[0];\n bool pl = false;\n\n for (int i = 0; i < n - 1; i++) {\n if (as[i] == as[i + 1]) pl = true;\n else {\n if (as[i] > as[i + 1]) {\n\tif (pa < as[i]) {\n\t if (pl) ko++;\n\t else mi++;\n\t}\n\telse if (pa > as[i] && pl) sa++;\n }\n else {\n\tif (pa > as[i]) {\n\t if (pl) bo++;\n\t else ku++;\n\t}\n\telse if (pa < as[i] && pl) sa++;\n }\n pa = as[i];\n pl = false;\n }\n }\n\n printf(\"%d %d %d %d %d\\n\", ko, bo, sa, mi, ku);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1532, "score_of_the_acc": -0.0811, "final_rank": 2 }, { "submission_id": "aoj_1552_1257270", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main ()\n{\n int n;\n cin >> n;\n vector<int> v(n);\n for (int i = 0; i < n; i++) {\n cin >> v[i];\n }\n\n vector<int> res(5);\n vector<int> num(2);\n num[0] = v[0];\n num[1] = v[1];\n for (int i = 2; i < n; i++) {\n int crl = num.size()-1;\n if (num[crl] == v[i]) {\n if (num[crl] != num[crl-1]) num.push_back(v[i]);\n } else if (num[crl] == num[crl-1]) {\n num.push_back(v[i]);\n } else if (num[crl] > num[crl-1]) {\n if (num[crl] < v[i]) num[crl] = v[i];\n else num.push_back(v[i]);\n } else if (num[crl] < num[crl-1]) {\n if (num[crl] > v[i]) num[crl] = v[i];\n else num.push_back(v[i]);\n } \n }\n\n for (int i = 0; i < num.size()-2; i++) {\n if (num[i] > num[i+1] && num[i+1] < num[i+2]) {\n res[4]++;\n } else if (num[i] < num[i+1] && num[i+1] > num[i+2]) {\n res[3]++;\n } else if (i && i + 2 < num.size() && num[i] == num[i+1]) {\n if (num[i-1] < num[i] && num[i+1] > num[i+2]) {\n res[0]++; \n } else if (num[i-1] > num[i] && num[i+1] < num[i+2]) {\n res[1]++;\n } else {\n res[2]++;\n }\n }\n }\n\n cout << res[0];\n for (int i = 1; i < res.size(); i++) {\n cout << \" \" << res[i];\n } cout << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 2068, "score_of_the_acc": -1.1991, "final_rank": 18 }, { "submission_id": "aoj_1552_1250783", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\nint a[100000],b[100000],x[5];\nint main(){\n\tint N;\n\tcin>>N;\n\trep(i,N) cin>>a[i];\n\trep(i,N-1){\n\t\tif(a[i]<a[i+1]) b[i]=1;\n\t\telse if(a[i]==a[i+1]) b[i]=0;\n\t\telse b[i]=-1;\n\t}\n\tint p=10;\n\trep(i,N-2){\n\t\tif(b[i]==b[i+1]){\n\t\t\tif(b[i]!=0) p=b[i];\n\t\t\tcontinue;\n\t\t}\n\t\tif(b[i]==1&&b[i+1]==-1) x[3]++;\n\t\telse if(b[i]==-1&&b[i+1]==1) x[4]++;\n\t\telse if(b[i]==0){\n\t\t\tif(p==10) continue;\n\t\t\tif(p==b[i+1]) x[2]++;\n\t\t\telse if(p==1) x[0]++;\n\t\t\telse x[1]++;\n\t\t}\n\t\tif(b[i]!=0) p=b[i];\n\t}\n\trep(i,4) cout<<x[i]<<\" \";\n\tcout<<x[4]<<endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1944, "score_of_the_acc": -1.1718, "final_rank": 17 }, { "submission_id": "aoj_1552_1249426", "code_snippet": "#include <iostream>\n#include <cstdio>\n#define REP(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,a) REP(i,0,a)\nusing namespace std;\n\nint N,kou,bon,hara,mine,kubo;\nint a[100010];\n\nint main(void){\n cin >> N;\n rep(i,N) cin >> a[i];\n int flat = 0, bef = a[0];\n REP(i,1,N) {\n if (a[i-1] == a[i])\n ++flat;\n else if (a[i-1] < a[i]) {\n if (flat > 0) {\n if (bef < a[i-1]) ++hara;\n if (bef > a[i-1]) ++bon;\n flat = 0;\n } else {\n if (bef > a[i-1]) ++kubo;\n }\n bef = a[i-1];\n } else {\n if (flat > 0) {\n if (bef < a[i-1]) ++kou;\n if (bef > a[i-1]) ++hara;\n flat = 0;\n } else {\n if (bef < a[i-1]) ++mine;\n }\n bef = a[i-1];\n }\n }\n printf(\"%d %d %d %d %d\\n\", kou, bon, hara, mine, kubo);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1536, "score_of_the_acc": -0.0819, "final_rank": 3 }, { "submission_id": "aoj_1552_1249137", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint main(){\n\t\n\tint n, p1 = inf, p2 = inf, cnt = 0;\n\tcin >> n;\n\tvi a(n), ans(5);\n\trep(i, n){\n\t\tcin >> a[i];\n\t\tif(p1 != inf && p2 != inf){\n\t\t\tif(cnt && p2 < p1 && p1 > a[i]) ans[0]++;\n\t\t\tif(cnt && p2 > p1 && p1 < a[i]) ans[1]++;\n\t\t\tif(cnt && p2 < p1 && p1 < a[i]) ans[2]++;\n\t\t\tif(cnt && p2 > p1 && p1 > a[i]) ans[2]++;\n\t\t\tif(!cnt && p2 < p1 && p1 > a[i]) ans[3]++;\n\t\t\tif(!cnt && p2 > p1 && p1 < a[i]) ans[4]++;\n\t\t}\n\t\t\n\t\tif(p1 != a[i]){\n\t\t\tp2 = p1; p1 = a[i];\n\t\t\tcnt = 0;\n\t\t}\n\t\telse cnt++;\n\t}\n\tcout << ans[0] << \" \" << ans[1] << \" \" << ans[2] << \" \" << ans[3] << \" \" << ans[4] << endl;\n\t\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1532, "score_of_the_acc": -1.0811, "final_rank": 16 }, { "submission_id": "aoj_1552_1249090", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e+9;\n\nint main() {\n\tint n, a, bp, p, kogen = 0, bonti = 0, sanpuku = 0, mine = 0, kuboti = 0;\n\tcin >> n;\n\tcin >> bp >> p;\n\tif(n == 1 || n == 2){\n\t\tfor(int i = 0; i < 5; ++i){\n\t\t\tif(i)\n\t\t\t\tcout << \" \";\n\t\t\tcout << \"0\";\n\t\t}\n\t\tcout << endl;\n\t\treturn 0;\n\t}\n\tbool flag = false;\n\tfor(int i = 2; i < n; ++i){\n\t\tscanf(\"%d\", &a);\n\t\tif(p == a && bp != p){\n\t\t\tflag = true;\n\t\t\tcontinue;\n\t\t}\n\t\tif(flag){\n\t\t\tif(bp < p && p > a)\n\t\t\t\tkogen++;\n\t\t\telse if(bp > p && p < a)\n\t\t\t\tbonti++;\n\t\t\telse\n\t\t\t\tsanpuku++;\n\t\t\tflag = false;\n\t\t}\n\t\telse if(bp > p && p < a)\n\t\t\tkuboti++;\n\t\telse if(bp < p && p > a)\n\t\t\tmine++;\n\t\tbp = p;\n\t\tp = a;\n\t}\n\tcout << kogen << \" \" << bonti << \" \" << sanpuku << \" \" << mine << \" \" << kuboti << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1248, "score_of_the_acc": -1.0185, "final_rank": 15 }, { "submission_id": "aoj_1552_1249088", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1e+9;\n\nint main() {\n\tint n, a, bp, p, kogen = 0, bonti = 0, sanpuku = 0, mine = 0, kuboti = 0;\n\tcin >> n;\n\tcin >> bp >> p;\n\tif(n == 1 || n == 2){\n\t\tfor(int i = 0; i < 5; ++i){\n\t\t\tif(i)\n\t\t\t\tcout << \" \";\n\t\t\tcout << \"0\";\n\t\t}\n\t\treturn 0;\n\t}\n\tbool flag = false;\n\tfor(int i = 2; i < n; ++i){\n\t\tscanf(\"%d\", &a);\n\t\tif(p == a && bp != p){\n\t\t\tflag = true;\n\t\t\tcontinue;\n\t\t}\n\t\tif(flag){\n\t\t\tif(bp < p && p > a)\n\t\t\t\tkogen++;\n\t\t\telse if(bp > p && p < a)\n\t\t\t\tbonti++;\n\t\t\telse\n\t\t\t\tsanpuku++;\n\t\t\tflag = false;\n\t\t}\n\t\telse if(bp > p && p < a)\n\t\t\tkuboti++;\n\t\telse if(bp < p && p > a)\n\t\t\tmine++;\n\t\tbp = p;\n\t\tp = a;\n\t}\n\tcout << kogen << \" \" << bonti << \" \" << sanpuku << \" \" << mine << \" \" << kuboti << endl;\n\treturn 0;\n}", "accuracy": 0.8809523809523809, "time_ms": 10, "memory_kb": 1252, "score_of_the_acc": -0.0194, "final_rank": 19 }, { "submission_id": "aoj_1552_1248952", "code_snippet": "#include <functional>\n#include <algorithm>\n#include <iostream>\n#include <numeric>\n#include <iomanip>\n#include <utility>\n#include <cstdlib>\n#include <sstream>\n#include <bitset>\n#include <vector>\n#include <cstdio>\n#include <ctime>\n#include <queue>\n#include <deque>\n#include <cmath>\n#include <stack>\n#include <list>\n#include <map>\n#include <set>\n\nusing namespace std;\n\ntypedef vector<int> vi;\ntypedef pair<int,int> pii;\ntypedef long long ll;\n\n#define dump(x) cerr << #x << \" = \" << (x) << endl\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define pb push_back\n\nint main() {\n int n;\n int a[200000];\n cin>>n;\n rep(i,n){\n cin>>a[i];\n }\n vector<int> field;\n \n int now=-1;\n for(int i=1;i< n;i++){\n if(((a[i-1]> a[i] && now==0) ||\n (a[i-1]< a[i] && now==1) ||\n (a[i-1]==a[i] && now==2) )&& i!=n-1){\n //??????????????????\n }\n else{\n if(now!=-1)field.pb(now);\n if(a[i-1]> a[i])now=0;\n if(a[i-1]< a[i])now=1;\n if(a[i-1]==a[i])now=2;\n }\n }\n field.pb(now);\n\n int ans[5]={};\n for(int i=2;i<field.size();i++){\n if(field[i-2]==1 && field[i-1]==2 && field[i]==0 )ans[0]++;\n if(field[i-2]==0 && field[i-1]==2 && field[i]==1 )ans[1]++;\n if((field[i-2]==1 && field[i-1]==2 && field[i]==1 ) || (field[i-2]==0 && field[i-1]==2 && field[i]==0 ))ans[2]++;\n }\n \n for(int i=1;i<field.size();i++){\n if(field[i-1]==1 && field[i]==0 )ans[3]++;\n if(field[i-1]==0 && field[i]==1 )ans[4]++;\n }\n \n \n cout<<ans[0]<<\" \"<<ans[1]<<\" \"<<ans[2]<<\" \"<<ans[3]<<\" \"<<ans[4]<<endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2072, "score_of_the_acc": -0.2, "final_rank": 5 }, { "submission_id": "aoj_1552_1248940", "code_snippet": "#include <iostream>\nusing namespace std;\n\nint main() {\n\n long long int n;\n cin >> n;\n\n long long int ans[5] = {};\n\n bool f = false;\n long long int b;\n long long int c = 0;\n\n for ( long long int i = 0; i < n; i++ ) {\n\n long long int in;\n cin >> in;\n\n if ( i > 0 ) {\n if ( b == in ) {\n\tif ( c > 0 ) {\n\t f = true;\n\t}else if ( c < 0 ) {\n\t f = true;\n\t}\n }else if ( b > in ) {\n\tif ( c > 0 ) {\n\t if ( f == true ) {\n\t ans[0]++;\n\t }else {\n\t ans[3]++;\n\t }\n\t}\n\tif ( c < 0 && f == true ) {\n\t ans[2]++;\n\t}\n\tf = false;\n\tc = -1;\n }else {\n\tif ( c < 0 ) {\n\t if ( f == true ) {\n\t ans[1]++;\n\t }else {\n\t ans[4]++;\n\t }\n\t}\n\tif ( c > 0 && f == true ) {\n\t ans[2]++;\n\t}\n\tf = false;\n\tc = 1;\n }\n }\n b = in;\n\n }\n\n cout << ans[0] << \" \" << ans[1] << \" \" << ans[2] << \" \" << ans[3] << \" \" << ans[4] << endl;\n\n return 0;\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1164, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1552_1248832", "code_snippet": "#include<stdio.h>\nint n, c1, c2, c3, c4, c5;\nint w[101000][2], cnt;\nint main(){\n\tint i, a, t = 0, tp;\n\tscanf(\"%d\", &n);\n\tfor (i = 1; i <= n; i++){\n\t\tscanf(\"%d\", &a);\n\t\tif (i != 1 && tp != a){\n\t\t\tcnt++;\n\t\t\tw[cnt][0] = tp;\n\t\t\tw[cnt][1] = t;\n\t\t\tt = 0;\n\t\t}\n\t\ttp = a;\n\t\tt++;\n\t}\n\tcnt++;\n\tw[cnt][0] = tp;\n\tw[cnt][1] = t;\n\tfor (i = 2; i < cnt; i++){\n\t\tif (w[i - 1][0] < w[i][0]){\n\t\t\tif (w[i][0] < w[i + 1][0]){\n\t\t\t\tif (w[i][1] > 1)c3++;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tif (w[i][1] > 1)c1++;\n\t\t\t\telse c4++;\n\t\t\t}\n\t\t}\n\t\telse{\n\t\t\tif (w[i][0] > w[i + 1][0]){\n\t\t\t\tif (w[i][1] > 1)c3++;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tif (w[i][1] > 1)c2++;\n\t\t\t\telse c5++;\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d %d %d %d %d\\n\", c1, c2, c3, c4, c5);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1772, "score_of_the_acc": -0.1339, "final_rank": 4 } ]
aoj_1553_cpp
Problem C: Manhattan Warp Machine 1 Problem とある宇宙では、1次元の整数座標点上に星が存在し、宇宙人達はマンハッタンワープ装置を使い、星間を移動している。 このワープ装置には、 N 個のボタンが付いており、ボタン i を押すと、現在いる星からのマンハッタン距離が d i である任意の星にコスト c i でワープすることができる。 今、点0の星にいるある宇宙人が点 x の星に行きたいと考えている。 点 x の星に辿り着くまでの最小のコストを答えよ。 辿り着けない場合は-1を出力せよ。 1次元上の整数座標点には必ず星が存在する。 点 x 1 と点 x 2 間のマンハッタン距離は| x 1 - x 2 |で表される。 Input N x d 1 c 1 ... d N c N 入力は全て整数で与えられる。 1行目に N と行きたい星の座標 x が空白区切りで与えられる。 続く N 行に、移動可能なマンハッタン距離 d i とコスト c i が1行ずつ空白区切りで与えられる。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 15 0 ≤ x ≤ 10 5 1 ≤ d i ≤ 10 5 1 ≤ c i ≤ 100 与えられるマンハッタン距離 d i は全て異なる。 Output 点 x の星に辿り着くまでにかかる最小コストを1行に出力せよ。辿り着くことが不可能な場合は-1を出力せよ。 Sample Input 1 2 5 1 1 2 1 Sample Output 1 3 Sample Input 2 2 12 9 1 3 2 Sample Output 2 3 Sample Input 3 1 3 4 1 Sample Output 3 -1
[ { "submission_id": "aoj_1553_10025912", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nconst int inf = 1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint d[20], c[20];\nbool v[300000];\n\nint main(){\n\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n \n int n, x;\n cin >> n >> x;\n for(int i = 0; i < n; i++) {\n cin >> d[i] >> c[i];\n }\n\n priority_queue<pair<int, int> > q;\n q.push(make_pair(0, 0));\n \n while(!q.empty()){\n pair<int, int> t = q.top();\n q.pop();\n \n int p = t.second;\n int co = -t.first;\n \n if(v[p]) continue;\n v[p] = true;\n \n if(p == x){\n cout << co << \"\\n\";\n return 0;\n }\n \n for(int i = 0; i < n; i++){\n int np = p + d[i];\n if(np < 300000 && !v[np]){\n q.push(make_pair(-(co + c[i]), np));\n }\n\n np = p - d[i];\n if(np >= 0 && !v[np]){\n q.push(make_pair(-(co + c[i]), np));\n }\n }\n }\n \n cout << -1 << \"\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 19408, "score_of_the_acc": -0.7092, "final_rank": 8 }, { "submission_id": "aoj_1553_10025909", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst int inf = 1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint n, x, d[20], c[20];\nbool v[300000];\n\nint main(){\n // For faster I/O operations\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n \n cin >> n >> x;\n for(int i = 0; i < n; i++) {\n cin >> d[i] >> c[i];\n }\n \n // Priority queue stores pairs of (negative cost, current position)\n // Note the space between the closing angle brackets\n priority_queue<pair<int, int> > q;\n q.push(make_pair(0, 0));\n \n while(!q.empty()){\n pair<int, int> top = q.top();\n q.pop();\n \n int cur = top.second;\n int cost = -top.first;\n \n if(v[cur]) continue;\n v[cur] = true;\n \n if(cur == x){\n cout << cost << \"\\n\";\n return 0;\n }\n \n for(int i = 0; i < n; i++){\n // Move forward\n int nxt = cur + d[i];\n if(nxt < 300000 && !v[nxt]){\n q.push(make_pair(-(cost + c[i]), nxt));\n }\n \n // Move backward\n nxt = cur - d[i];\n if(nxt >= 0 && !v[nxt]){\n q.push(make_pair(-(cost + c[i]), nxt));\n }\n }\n }\n \n cout << -1 << \"\\n\";\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 19472, "score_of_the_acc": -0.7095, "final_rank": 9 }, { "submission_id": "aoj_1553_10025907", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint n, x, d[20], c[20];\nbool v[300000];\n\nint main(){\n\tcin >> n >> x;\n\trep(i, n) cin >> d[i] >> c[i];\n\t\n\tpriority_queue<pi> q;\n\tq.push(pi(0, 0));\n\twhile(!q.empty()){\n\t\tint cur = q.top().second, cost = -q.top().first; q.pop();\n\t\tif(v[cur]) continue;\n\t\tv[cur] = 1;\n\t\tif(cur == x){\n\t\t\tcout << cost << endl;\n\t\t\treturn 0;\n\t\t}\n\t\trep(i, n){\n\t\t\tint nxt = cur + d[i];\n\t\t\tif(nxt < 300000 && !v[nxt]) q.push(pi(-cost - c[i], nxt));\n\t\t\t\n\t\t\tnxt = cur - d[i];\n\t\t\tif(nxt >= 0 && !v[nxt]) q.push(pi(-cost - c[i], nxt));\n\t\t}\n\t}\n\tcout << -1 << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 19480, "score_of_the_acc": -0.7219, "final_rank": 10 }, { "submission_id": "aoj_1553_7053114", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define REP(i,num) for(ll i=0;i<(num);++i)\n#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))\n#define LOOP(i) while(i--)\n#define ALL(c) c.begin(),c.end()\n#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;\n#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}\n\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\n\nconstexpr ll atcoder_mod = 1e9+7;\n\ntemplate<typename T=ll>\nT in(){ T x; cin >> x; return (x); }\ntemplate<typename T=ll,typename C=vector<T>>\nC vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }\ntemplate<typename T=ll,size_t N>\narray<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;}\n\nvoid vout(){ cout << endl; }\ntemplate<typename Head,typename... Tail>\nvoid vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }\nvoid out(){ cout << endl; }\ntemplate<typename Head,typename... Tail>\nvoid out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }\n\ntemplate<typename T>\nbool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }\ntemplate<typename T>\nbool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }\n\nclass ConnectNodeInfo{\n\tvector<vector<pair<ll,ll>>> graph;\npublic:\n\tConnectNodeInfo(int node_num){\n\t\tgraph.resize(node_num);\n\t}\n\tvoid AddNonDirectionalConnection(ll u,ll v,ll w){\n\t\tgraph[u].emplace_back(v,w);\n\t\tgraph[v].emplace_back(u,w);\n\t}\n\tvoid AddDirectionalConnection(ll u,ll v,ll w){\n\t\tgraph[u].emplace_back(v,w);\n\t}\n\tvector<pair<ll,ll>>& operator[](ll index){\n\t\treturn graph[index];\n\t}\n\tsize_t size(){return graph.size();}\n};\n\nclass Dijkstra{\n\tusing Point = pair<ll,ll>;\n\tvector<ll> dist;\npublic:\n\tvoid CalcShortestPath(int start,ConnectNodeInfo& connect){\n\t\tdist.resize(connect.size(),1LL<<60);\n\t\tdist[start] = 0;\n\n\t\tpriority_queue<Point,vector<Point>,greater<Point>> Q;\n\t\tQ.emplace(0,start);\n\t\twhile(!Q.empty()){\n\t\t\tauto p = Q.top();\n\t\t\tQ.pop();\n\t\t\tint v = p.second;\n\t\t\tif(dist[v]<p.first){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor(auto e:connect[v]){\n\t\t\t\tif(dist[e.first]>dist[v]+e.second){\n\t\t\t\t\tdist[e.first] = dist[v]+e.second;\n\t\t\t\t\tQ.emplace(dist[e.first],e.first);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tll GetDistance(int i){\n\t\treturn dist[i];\n\t}\n};\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tcout<<fixed<<setprecision(10);\n\n\tauto N=in(),X=in();\n\tvector<pair<ll,ll>> P(N);\n\tREP(i,N){\n\t\tP[i].first=in();\n\t\tP[i].second=in();\n\t}\n\t\n\tConnectNodeInfo connect(200100);\n\tREP(i,200100){\n\t\tREP(j,N){\n\t\t\tif(i-P[j].first>=0) connect.AddDirectionalConnection(i,i-P[j].first,P[j].second);\n\t\t\tif(i+P[j].first<200100) connect.AddDirectionalConnection(i,i+P[j].first,P[j].second);\n\t\t}\n\t}\n\tDijkstra dij;\n\tdij.CalcShortestPath(0,connect);\n\tout(dij.GetDistance(X)>=(1ll<<60)?-1:dij.GetDistance(X));\n\treturn 0;\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 117976, "score_of_the_acc": -0.8337, "final_rank": 13 }, { "submission_id": "aoj_1553_5550561", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint n, x, cst[200001], d[15], c[15];\nqueue<int> que;\nconst int INF = 1000000001;\n\nvoid dp(int here){\n if(!que.empty()) que.pop();\n if(cst[here] >= cst[x]) return;\n \n bool f[15] = {}, b[15] = {};\n \n for(int i = 0; i < n; i++){\n if(here - d[i] >= 0){\n if(cst[here - d[i]] > cst[here] + c[i]){\n cst[here - d[i]] = cst[here] + c[i];\n que.push(here - d[i]);\n }\n }\n if(here + d[i] <= 200000){\n if(cst[here + d[i]] > cst[here] + c[i]){\n cst[here + d[i]] = cst[here] + c[i];\n que.push(here + d[i]);\n }\n }\n }\n \n while(!que.empty())dp(que.front());\n}\n\nint main(){\n cin >> n >> x;\n int gc1 , i;\n for(i = 0; i < n; i++) cin >> d[i] >> c[i];\n \n gc1 = d[0];\n for(i = 1; i < n; i++){\n gc1 = gcd(gc1, d[i]);\n }\n if(x % gc1){\n cout << -1 << endl;\n return 0;\n }\n \n for(i = 0; i <= 200000; i++) cst[i] = INF;\n cst[0] = 0;\n \n dp(0);\n cout << cst[x] << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 43008, "score_of_the_acc": -0.2622, "final_rank": 5 }, { "submission_id": "aoj_1553_5550336", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\ntemplate <typename T>\nstruct dijkstra {\n\tstruct edge {\n\t\tint to;\n\t\tT cost;\n\t\tedge() {}\n\t\tedge(int to, T cost) : to(to), cost(cost) {}\n\t\tbool operator<(const edge &e) const { return cost > e.cost; }\n\t};\n\n\tT inf() { return numeric_limits<T>::max() / 2; }\n\n\tvector<vector<edge>> G;\n\tvector<T> ds;\n\tvector<int> bs;\n\tdijkstra(int n) : G(n) {}\n\n\tvoid add_edge(int from, int to, T cost) { G[from].emplace_back(to, cost); }\n\n\tvoid build(int start) {\n\t\tint n = G.size();\n\t\tds.assign(n, inf());\n\t\tbs.assign(n, -1);\n\n\t\tpriority_queue<edge> Q;\n\t\tds[start] = 0;\n\t\tQ.emplace(start, ds[start]);\n\n\t\twhile ( !Q.empty() ) {\n\t\t\tauto p = Q.top();\n\t\t\tQ.pop();\n\t\t\tint v = p.to;\n\t\t\tif ( ds[v] < p.cost ) continue;\n\t\t\tfor ( auto e : G[v] ) {\n\t\t\t\tif ( ds[e.to] > ds[v] + e.cost ) {\n\t\t\t\t\tds[e.to] = ds[v] + e.cost;\n\t\t\t\t\tbs[e.to] = v;\n\t\t\t\t\tQ.emplace(e.to, ds[e.to]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tT operator[](int k) { return ds[k]; }\n};\n\nint n,x;\nvector<int> d,c;\nconstexpr int MAX = 5*1e5;\nvector<bool> arrived(MAX,false);\nvoid dfs(int idx){\n\tarrived[idx]=true;\n\tfor(int i=0;i<n;i++){\n\t\tif(idx+d[i]<MAX && !arrived[idx+d[i]]){\n\t\t\tdfs(idx+d[i]);\n\t\t}\n\t\tif(idx-d[i]>=0 && !arrived[idx-d[i]]){\n\t\t\tdfs(idx-d[i]);\n\t\t}\n\t}\n}\n\nint main(){\n\tcin>>n>>x;\n\td.resize(n);\n\tc.resize(n);\n\tfor(int i=0;i<n;i++){\n\t\tcin>>d[i]>>c[i];\n\t}\n\n\tdfs(0);\n\n\tif(!arrived[x]){\n\t\tcout<<-1<<endl;\n\t\treturn 0;\n\t}\n\n\tdijkstra<int> G(MAX);\n\tfor(int i=0;i<MAX;i++){\n\t\tif(!arrived[i]) continue;\n\n\t\tfor(int j=0;j<n;j++){\n\t\t\tif(i+d[j]<MAX) G.add_edge(i,i+d[j],c[j]);\n\t\t\tif(i-d[j]>=0) G.add_edge(i,i-d[j],c[j]);\n\t\t}\n\t}\n\n\tG.build(0);\n\n\tcout<<G[x]<<endl;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 180204, "score_of_the_acc": -1.3086, "final_rank": 16 }, { "submission_id": "aoj_1553_5550010", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)\n#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)\n\n#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)\n#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)\n\n#define debug(x) cerr << \"(\" << __LINE__ << \")\" << #x << \": \" << (x) << endl\n\nconstexpr i32 inf = 1001001001;\nconstexpr i64 infll = 1001001001001001001ll;\n\nconstexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; \nconstexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\nstruct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;\n\ntemplate <typename T = i64> T input() { T x; cin >> x; return x; }\n\ntemplate <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\"); } return os; } \ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }\n\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\ntemplate <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }\n\ntemplate <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }\ntemplate <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n// }}}\n\n// dijkstra {{{\ntemplate < typename CostType >\nclass dijkstra {\n struct edge {\n int to;\n CostType cost;\n\n edge(int to_, CostType cost_) : to(to_), cost(cost_) {}\n };\n\n const CostType INF = numeric_limits<CostType>::max();\n\n int n;\n vector< vector<edge> > G;\n vector< CostType > ds;\n\n public:\n dijkstra(int n_) : n(n_), G(n_) {}\n\n void add_edge(int from, int to, CostType cost) {\n G[from].emplace_back(to, cost);\n }\n\n CostType inf() {\n return INF;\n }\n\n void build(int s) {\n using P = pair<CostType, int>;\n priority_queue< P, vector<P>, greater<P> > pq;\n\n ds.assign(n, INF);\n ds[s] = 0;\n pq.emplace(ds[s], s);\n\n while (!pq.empty()) {\n int v;\n CostType d;\n\n tie(d, v) = pq.top();\n pq.pop();\n\n if (ds[v] < d) continue;\n\n for (const edge &e : G[v]) {\n int u = e.to;\n if (ds[u] > ds[v] + e.cost) {\n ds[u] = ds[v] + e.cost;\n pq.emplace(ds[u], u);\n }\n }\n }\n }\n\n CostType dist(int to) {\n return ds[to];\n }\n\n CostType operator[](int k) {\n return dist(k);\n }\n};\n// }}}\n\nvoid solve() {\n int n = input(), x = input();\n\n int V = x + 1 + 200000;\n dijkstra<int> G(V);\n range(i, 0, n) {\n int d = input(), c = input();\n range(t, d, V) {\n G.add_edge(t - d, t, c);\n G.add_edge(t, t - d, c);\n }\n }\n\n G.build(0);\n\n if (G[x] == G.inf()) {\n cout << -1 << endl;\n } else {\n cout << G[x] << endl;\n }\n}\n\nsigned main() {\n solve();\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 95268, "score_of_the_acc": -0.7919, "final_rank": 11 }, { "submission_id": "aoj_1553_5083545", "code_snippet": "#line 2 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#include <bits/stdc++.h>\n#line 6 \"/home/yuruhiya/programming/library/template/constants.cpp\"\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n#define FOR(i, m, n) for (int i = (m); i < (n); ++i)\n#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)\n#define rfor(i, m, n) for (int i = (m); i >= (n); --i)\n#define unless(c) if (!(c))\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)\n\nusing namespace std;\nusing ll = long long;\nusing LD = long double;\nusing VB = vector<bool>;\nusing VVB = vector<VB>;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VS = vector<string>;\nusing VD = vector<LD>;\nusing PII = pair<int, int>;\nusing VP = vector<PII>;\nusing PLL = pair<ll, ll>;\nusing VPL = vector<PLL>;\ntemplate <class T> using PQ = priority_queue<T>;\ntemplate <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;\nconstexpr int inf = 1000000000;\nconstexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;\nconstexpr long double PI = 3.14159265358979323846, EPS = 1e-12;\n#line 7 \"/home/yuruhiya/programming/library/template/Input.cpp\"\nusing namespace std;\n\n#ifdef _WIN32\n#define getchar_unlocked _getchar_nolock\n#define putchar_unlocked _putchar_nolock\n#define fwrite_unlocked fwrite\n#define fflush_unlocked fflush\n#endif\nclass Scanner {\n\tstatic int gc() {\n\t\treturn getchar_unlocked();\n\t}\n\tstatic char next_char() {\n\t\tchar c;\n\t\tread(c);\n\t\treturn c;\n\t}\n\ttemplate <class T> static void read(T& v) {\n\t\tcin >> v;\n\t}\n\tstatic void read(char& v) {\n\t\twhile (isspace(v = gc()))\n\t\t\t;\n\t}\n\tstatic void read(bool& v) {\n\t\tv = next_char() != '0';\n\t}\n\tstatic void read(string& v) {\n\t\tv.clear();\n\t\tfor (char c = next_char(); !isspace(c); c = gc()) v += c;\n\t}\n\tstatic void read(int& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long long& v) {\n\t\tv = 0;\n\t\tbool neg = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c); c = gc()) v = v * 10 + (c - '0');\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(double& v) {\n\t\tv = 0;\n\t\tdouble dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\tstatic void read(long double& v) {\n\t\tv = 0;\n\t\tlong double dp = 1;\n\t\tbool neg = false, after_dp = false;\n\t\tchar c = next_char();\n\t\tif (c == '-') {\n\t\t\tneg = true;\n\t\t\tc = gc();\n\t\t}\n\t\tfor (; isdigit(c) || c == '.'; c = gc()) {\n\t\t\tif (c == '.') {\n\t\t\t\tafter_dp = true;\n\t\t\t} else if (after_dp) {\n\t\t\t\tv += (c - '0') * (dp *= 0.1);\n\t\t\t} else {\n\t\t\t\tv = v * 10 + (c - '0');\n\t\t\t}\n\t\t}\n\t\tif (neg) v = -v;\n\t}\n\ttemplate <class T, class U> static void read(pair<T, U>& v) {\n\t\tread(v.first);\n\t\tread(v.second);\n\t}\n\ttemplate <class T> static void read(vector<T>& v) {\n\t\tfor (auto& e : v) read(e);\n\t}\n\ttemplate <size_t N = 0, class T> static void read_tuple_impl(T& v) {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tread(get<N>(v));\n\t\t\tread_tuple_impl<N + 1>(v);\n\t\t}\n\t}\n\ttemplate <class... T> static void read(tuple<T...>& v) {\n\t\tread_tuple_impl(v);\n\t}\n\tstruct ReadVectorHelper {\n\t\tsize_t n;\n\t\tReadVectorHelper(size_t _n) : n(_n) {}\n\t\ttemplate <class T> operator vector<T>() {\n\t\t\tvector<T> v(n);\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\tstruct Read2DVectorHelper {\n\t\tsize_t n, m;\n\t\tRead2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}\n\t\ttemplate <class T> operator vector<vector<T>>() {\n\t\t\tvector<vector<T>> v(n, vector<T>(m));\n\t\t\tread(v);\n\t\t\treturn v;\n\t\t}\n\t};\n\npublic:\n\tstring read_line() const {\n\t\tstring v;\n\t\tfor (char c = gc(); c != '\\n' && c != '\\0'; c = gc()) v += c;\n\t\treturn v;\n\t}\n\ttemplate <class T> T read() const {\n\t\tT v;\n\t\tread(v);\n\t\treturn v;\n\t}\n\ttemplate <class T> vector<T> read_vector(size_t n) const {\n\t\tvector<T> a(n);\n\t\tread(a);\n\t\treturn a;\n\t}\n\ttemplate <class T> operator T() const {\n\t\treturn read<T>();\n\t}\n\tint operator--(int) const {\n\t\treturn read<int>() - 1;\n\t}\n\tReadVectorHelper operator[](size_t n) const {\n\t\treturn ReadVectorHelper(n);\n\t}\n\tRead2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {\n\t\treturn Read2DVectorHelper(nm);\n\t}\n\tvoid operator()() const {}\n\ttemplate <class H, class... T> void operator()(H&& h, T&&... t) const {\n\t\tread(h);\n\t\toperator()(forward<T>(t)...);\n\t}\n\nprivate:\n\ttemplate <template <class...> class, class...> struct Multiple;\n\ttemplate <template <class...> class V, class Head, class... Tail>\n\tstruct Multiple<V, Head, Tail...> {\n\t\ttemplate <class... Args> using vec = V<vector<Head>, Args...>;\n\t\tusing type = typename Multiple<vec, Tail...>::type;\n\t};\n\ttemplate <template <class...> class V> struct Multiple<V> { using type = V<>; };\n\ttemplate <class... T> using multiple_t = typename Multiple<tuple, T...>::type;\n\ttemplate <size_t N = 0, class T> void multiple_impl(T& t) const {\n\t\tif constexpr (N < tuple_size_v<T>) {\n\t\t\tauto& vec = get<N>(t);\n\t\t\tusing V = typename remove_reference_t<decltype(vec)>::value_type;\n\t\t\tvec.push_back(read<V>());\n\t\t\tmultiple_impl<N + 1>(t);\n\t\t}\n\t}\n\npublic:\n\ttemplate <class... T> auto multiple(size_t h) const {\n\t\tmultiple_t<T...> result;\n\t\twhile (h--) multiple_impl(result);\n\t\treturn result;\n\t}\n} in;\n#define inputs(T, ...) \\\n\tT __VA_ARGS__; \\\n\tin(__VA_ARGS__)\n#define ini(...) inputs(int, __VA_ARGS__)\n#define inl(...) inputs(long long, __VA_ARGS__)\n#define ins(...) inputs(string, __VA_ARGS__)\n#line 7 \"/home/yuruhiya/programming/library/template/Output.cpp\"\n#include <charconv>\n#line 10 \"/home/yuruhiya/programming/library/template/Output.cpp\"\nusing namespace std;\n\nstruct BoolStr {\n\tconst char *t, *f;\n\tBoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}\n} Yes(\"Yes\", \"No\"), yes(\"yes\", \"no\"), YES(\"YES\", \"NO\"), Int(\"1\", \"0\");\nstruct DivStr {\n\tconst char *d, *l;\n\tDivStr(const char* _d, const char* _l) : d(_d), l(_l) {}\n} spc(\" \", \"\\n\"), no_spc(\"\", \"\\n\"), end_line(\"\\n\", \"\\n\"), comma(\",\", \"\\n\"),\n no_endl(\" \", \"\");\nclass Printer {\n\tBoolStr B{Yes};\n\tDivStr D{spc};\n\npublic:\n\tvoid print(int v) const {\n\t\tchar buf[12]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid print(long long v) const {\n\t\tchar buf[21]{};\n\t\tif (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {\n\t\t\tfwrite(buf, sizeof(char), ptr - buf, stdout);\n\t\t} else {\n\t\t\tassert(false);\n\t\t}\n\t}\n\tvoid print(bool v) const {\n\t\tprint(v ? B.t : B.f);\n\t}\n\tvoid print(vector<bool>::reference v) const {\n\t\tprint(v ? B.t : B.f);\n\t}\n\tvoid print(char v) const {\n\t\tputchar_unlocked(v);\n\t}\n\tvoid print(const char* v) const {\n\t\tfwrite_unlocked(v, 1, strlen(v), stdout);\n\t}\n\tvoid print(double v) const {\n\t\tprintf(\"%.20f\", v);\n\t}\n\tvoid print(long double v) const {\n\t\tprintf(\"%.20Lf\", v);\n\t}\n\ttemplate <class T> void print(const T& v) const {\n\t\tcout << v;\n\t}\n\ttemplate <class T, class U> void print(const pair<T, U>& v) const {\n\t\tprint(v.first);\n\t\tprint(D.d);\n\t\tprint(v.second);\n\t}\n\ttemplate <class InputIterater>\n\tvoid print_range(const InputIterater& begin, const InputIterater& end) const {\n\t\tfor (InputIterater i = begin; i != end; ++i) {\n\t\t\tif (i != begin) print(D.d);\n\t\t\tprint(*i);\n\t\t}\n\t}\n\ttemplate <class T> void print(const vector<T>& v) const {\n\t\tprint_range(v.begin(), v.end());\n\t}\n\ttemplate <class T, size_t N> void print(const array<T, N>& v) const {\n\t\tprint_range(v.begin(), v.end());\n\t}\n\ttemplate <class T> void print(const vector<vector<T>>& v) const {\n\t\tfor (size_t i = 0; i < v.size(); ++i) {\n\t\t\tif (i) print(D.l);\n\t\t\tprint(v[i]);\n\t\t}\n\t}\n\n\tPrinter() = default;\n\tPrinter(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}\n\tPrinter& operator()() {\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H> Printer& operator()(H&& h) {\n\t\tprint(h);\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class H, class... T> Printer& operator()(H&& h, T&&... t) {\n\t\tprint(h);\n\t\tprint(D.d);\n\t\treturn operator()(forward<T>(t)...);\n\t}\n\ttemplate <class InputIterator>\n\tPrinter& range(const InputIterator& begin, const InputIterator& end) {\n\t\tprint_range(begin, end);\n\t\tprint(D.l);\n\t\treturn *this;\n\t}\n\ttemplate <class T> Printer& range(const T& a) {\n\t\trange(a.begin(), a.end());\n\t\treturn *this;\n\t}\n\ttemplate <class... T> void exit(T&&... t) {\n\t\toperator()(forward<T>(t)...);\n\t\tstd::exit(EXIT_SUCCESS);\n\t}\n\tPrinter& flush() {\n\t\tfflush_unlocked(stdout);\n\t\treturn *this;\n\t}\n\tPrinter& set(const BoolStr& b) {\n\t\tB = b;\n\t\treturn *this;\n\t}\n\tPrinter& set(const DivStr& d) {\n\t\tD = d;\n\t\treturn *this;\n\t}\n\tPrinter& set(const char* t, const char* f) {\n\t\tB = BoolStr(t, f);\n\t\treturn *this;\n\t}\n} out;\n#line 3 \"/home/yuruhiya/programming/library/template/Step.cpp\"\nusing namespace std;\n\ntemplate <class T> struct Step {\n\tusing value_type = T;\n\n\tclass iterator {\n\t\tvalue_type a, b, c;\n\n\tpublic:\n\t\tconstexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}\n\t\tconstexpr iterator(value_type _b, value_type _c, value_type _s)\n\t\t : a(_b), b(_c), c(_s) {}\n\t\tconstexpr iterator& operator++() {\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn *this;\n\t\t}\n\t\tconstexpr iterator operator++(int) {\n\t\t\titerator tmp = *this;\n\t\t\t--b;\n\t\t\ta += c;\n\t\t\treturn tmp;\n\t\t}\n\t\tconstexpr const value_type& operator*() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr const value_type* operator->() const {\n\t\t\treturn &a;\n\t\t}\n\t\tconstexpr bool operator==(const iterator& i) const {\n\t\t\treturn b == i.b;\n\t\t}\n\t\tconstexpr bool operator!=(const iterator& i) const {\n\t\t\treturn !(b == i.b);\n\t\t}\n\t\tconstexpr value_type start() const {\n\t\t\treturn a;\n\t\t}\n\t\tconstexpr value_type size() const {\n\t\t\treturn b;\n\t\t}\n\t\tconstexpr value_type step() const {\n\t\t\treturn c;\n\t\t}\n\t};\n\tconstexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}\n\tconstexpr iterator begin() const {\n\t\treturn be;\n\t}\n\tconstexpr iterator end() const {\n\t\treturn en;\n\t}\n\tconstexpr value_type start() const {\n\t\treturn be.start();\n\t}\n\tconstexpr value_type size() const {\n\t\treturn be.size();\n\t}\n\tconstexpr value_type step() const {\n\t\treturn be.step();\n\t}\n\tconstexpr value_type sum() const {\n\t\treturn start() * size() + step() * (size() * (size() - 1) / 2);\n\t}\n\toperator vector<value_type>() const {\n\t\treturn to_a();\n\t}\n\tauto to_a() const {\n\t\tvector<value_type> result;\n\t\tresult.reserve(size());\n\t\tfor (auto i : *this) {\n\t\t\tresult.push_back(i);\n\t\t}\n\t\treturn result;\n\t}\n\nprivate:\n\titerator be, en;\n};\ntemplate <class T> constexpr auto step(T a) {\n\treturn Step<T>(0, a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b) {\n\treturn Step<T>(a, b - a, 1);\n}\ntemplate <class T> constexpr auto step(T a, T b, T c) {\n\treturn Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);\n}\n#line 8 \"/home/yuruhiya/programming/library/template/Ruby.cpp\"\nusing namespace std;\n\ntemplate <class F> struct Callable {\n\tF func;\n\tCallable(const F& f) : func(f) {}\n};\ntemplate <class T, class F> auto operator|(const T& v, const Callable<F>& c) {\n\treturn c.func(v);\n}\n\nstruct Sort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\treturn v;\n\t}\n} Sort;\nstruct SortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) < f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} SortBy;\nstruct RSort_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(rbegin(v), rend(v), f);\n\t\t\treturn v;\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {\n\t\tsort(rbegin(v), rend(v));\n\t\treturn v;\n\t}\n} RSort;\nstruct RSortBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tsort(begin(v), end(v),\n\t\t\t [&](const auto& i, const auto& j) { return f(i) > f(j); });\n\t\t\treturn v;\n\t\t});\n\t}\n} RSortBy;\nstruct Reverse_impl {\n\ttemplate <class T> friend auto operator|(T v, const Reverse_impl& c) {\n\t\treverse(begin(v), end(v));\n\t\treturn v;\n\t}\n} Reverse;\nstruct Unique_impl {\n\ttemplate <class T> friend auto operator|(T v, const Unique_impl& c) {\n\t\tv.erase(unique(begin(v), end(v), end(v)));\n\t\treturn v;\n\t}\n} Unique;\nstruct Uniq_impl {\n\ttemplate <class T> friend auto operator|(T v, const Uniq_impl& c) {\n\t\tsort(begin(v), end(v));\n\t\tv.erase(unique(begin(v), end(v)), end(v));\n\t\treturn v;\n\t}\n} Uniq;\nstruct Rotate_impl {\n\tauto operator()(int&& left) {\n\t\treturn Callable([&](auto v) {\n\t\t\tint s = static_cast<int>(size(v));\n\t\t\tassert(-s <= left && left <= s);\n\t\t\tif (0 <= left) {\n\t\t\t\trotate(begin(v), begin(v) + left, end(v));\n\t\t\t} else {\n\t\t\t\trotate(begin(v), end(v) + left, end(v));\n\t\t\t}\n\t\t\treturn v;\n\t\t});\n\t}\n} Rotate;\nstruct Max_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *max_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Max_impl& c) {\n\t\treturn *max_element(begin(v), end(v));\n\t}\n} Max;\nstruct Min_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) { return *min_element(begin(v), end(v), f); });\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Min_impl& c) {\n\t\treturn *min_element(begin(v), end(v));\n\t}\n} Min;\nstruct MaxPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MaxPos_impl& c) {\n\t\treturn max_element(begin(v), end(v)) - begin(v);\n\t}\n} MaxPos;\nstruct MinPos_impl {\n\ttemplate <class T> friend auto operator|(T v, const MinPos_impl& c) {\n\t\treturn min_element(begin(v), end(v)) - begin(v);\n\t}\n} MinPos;\nstruct MaxBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_it = begin(v);\n\t\t\tauto max_val = f(*max_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_it = it;\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *max_it;\n\t\t});\n\t}\n} MaxBy;\nstruct MinBy_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_it = begin(v);\n\t\t\tauto min_val = f(*min_it);\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_it = it;\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn *min_it;\n\t\t});\n\t}\n} MinBy;\nstruct MaxOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto max_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); max_val < val) {\n\t\t\t\t\tmax_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn max_val;\n\t\t});\n\t}\n} MaxOf;\nstruct MinOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tauto min_val = f(*begin(v));\n\t\t\tfor (auto it = next(begin(v)); it != end(v); ++it) {\n\t\t\t\tif (auto val = f(*it); min_val > val) {\n\t\t\t\t\tmin_val = val;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn min_val;\n\t\t});\n\t}\n} MinOf;\nstruct Count_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return count(begin(v), end(v), val); });\n\t}\n} Count;\nstruct CountIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return count_if(begin(v), end(v), f); });\n\t}\n} CountIf;\nstruct Index_impl {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(begin(v), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n\ttemplate <class V> auto operator()(const V& val, size_t i) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find(next(begin(v), i), end(v), val);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} Index;\nstruct IndexIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<int> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(result - begin(v)) : nullopt;\n\t\t});\n\t}\n} IndexIf;\nstruct FindIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) -> optional<typename decltype(v)::value_type> {\n\t\t\tauto result = find_if(begin(v), end(v), f);\n\t\t\treturn result != end(v) ? optional(*result) : nullopt;\n\t\t});\n\t}\n} FindIf;\nstruct Sum_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\treturn accumulate(next(begin(v)), end(v), f(*begin(v)),\n\t\t\t [&](const auto& a, const auto& b) { return a + f(b); });\n\t\t});\n\t}\n\ttemplate <class T> friend auto operator|(T v, const Sum_impl& c) {\n\t\treturn accumulate(begin(v), end(v), typename T::value_type{});\n\t}\n} Sum;\nstruct Includes {\n\ttemplate <class V> auto operator()(const V& val) {\n\t\treturn Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });\n\t}\n} Includes;\nstruct IncludesIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });\n\t}\n} IncludesIf;\nstruct RemoveIf_impl {\n\ttemplate <class F> auto operator()(const F& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tv.erase(remove_if(begin(v), end(v), f), end(v));\n\t\t\treturn v;\n\t\t});\n\t}\n} RemoveIf;\nstruct Each_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tf(i);\n\t\t\t}\n\t\t});\n\t}\n} Each;\nstruct EachConsPair_impl {\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, EachConsPair_impl& c) {\n\t\tvector<pair<value_type, value_type>> result;\n\t\tif (size(v) >= 2) {\n\t\t\tresult.reserve(size(v) - 1);\n\t\t\tfor (size_t i = 0; i < size(v) - 1; ++i) {\n\t\t\t\tresult.emplace_back(v[i], v[i + 1]);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n} EachConsPair;\nstruct Select_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing value_type = typename decltype(v)::value_type;\n\t\t\tvector<value_type> result;\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) result.push_back(i);\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Select;\nstruct Map_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tusing result_type = invoke_result_t<F, typename decltype(v)::value_type>;\n\t\t\tvector<result_type> result;\n\t\t\tresult.reserve(size(v));\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult.push_back(f(i));\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n} Map;\nstruct Indexed_impl {\n\ttemplate <class T> friend auto operator|(const T& v, Indexed_impl& c) {\n\t\tusing value_type = typename T::value_type;\n\t\tvector<pair<value_type, int>> result;\n\t\tresult.reserve(size(v));\n\t\tint index = 0;\n\t\tfor (const auto& i : v) {\n\t\t\tresult.emplace_back(i, index++);\n\t\t}\n\t\treturn result;\n\t}\n} Indexed;\nstruct AllOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (!f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} AllOf;\nstruct AnyOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return true;\n\t\t\t}\n\t\t\treturn false;\n\t\t});\n\t}\n} AnyOf;\nstruct NoneOf_impl {\n\ttemplate <class F> auto operator()(F&& f) {\n\t\treturn Callable([&](auto v) {\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tif (f(i)) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n} NoneOf;\n\nstruct Tally_impl {\n\ttemplate <class F> auto operator()(size_t max_val) {\n\t\treturn Callable([&](auto v) {\n\t\t\tvector<size_t> result(max_val);\n\t\t\tfor (const auto& i : v) {\n\t\t\t\tresult[static_cast<size_t>(i)]++;\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\t}\n\ttemplate <class T, class value_type = typename T::value_type>\n\tfriend auto operator|(const T& v, Tally_impl& c) {\n\t\tmap<value_type, size_t> result;\n\t\tfor (const auto& i : v) {\n\t\t\tresult[i]++;\n\t\t}\n\t\treturn result;\n\t}\n} Tally;\n\ntemplate <class T> auto operator*(const vector<T>& a, size_t n) {\n\tT result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult.insert(result.end(), a.begin(), a.end());\n\t}\n\treturn result;\n}\nauto operator*(string a, size_t n) {\n\tstring result;\n\tfor (size_t i = 0; i < n; ++i) {\n\t\tresult += a;\n\t}\n\treturn result;\n}\ntemplate <class T, class U> auto& operator<<(vector<T>& a, const U& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T> auto& operator<<(string& a, const T& b) {\n\ta.insert(a.end(), all(b));\n\treturn a;\n}\ntemplate <class T, class U> auto operator+(vector<T> a, const U& b) {\n\ta << b;\n\treturn a;\n}\ntemplate <class T> auto operator+(string a, const T& b) {\n\ta << b;\n\treturn a;\n}\n#line 7 \"/home/yuruhiya/programming/library/template/functions.cpp\"\nusing namespace std;\n\ntemplate <class T = long long> constexpr T TEN(size_t n) {\n\tT result = 1;\n\tfor (size_t i = 0; i < n; ++i) result *= 10;\n\treturn result;\n}\ntemplate <class T, class U,\n enable_if_t<is_integral_v<T> && is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr auto div_ceil(T n, U m) {\n\treturn (n + m - 1) / m;\n}\ntemplate <class T, class U> constexpr auto div_ceil2(T n, U m) {\n\treturn div_ceil(n, m) * m;\n}\ntemplate <class T> constexpr T triangle(T n) {\n\treturn (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);\n}\ntemplate <class T> constexpr T nC2(T n) {\n\treturn (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);\n}\ntemplate <class T, class U> constexpr auto middle(const T& l, const U& r) {\n\treturn l + (r - l) / 2;\n}\ntemplate <class T, class U, class V>\nconstexpr bool in_range(const T& v, const U& lower, const V& upper) {\n\treturn lower <= v && v < upper;\n}\ntemplate <class T, enable_if_t<is_integral_v<T>, nullptr_t> = nullptr>\nconstexpr bool is_square(T n) {\n\tT s = sqrt(n);\n\treturn s * s == n || (s + 1) * (s + 1) == n;\n}\ntemplate <class T = long long> constexpr T BIT(int b) {\n\treturn T(1) << b;\n}\ntemplate <class T> constexpr int BIT(T x, int i) {\n\treturn (x & (T(1) << i)) ? 1 : 0;\n}\ntemplate <class T> constexpr int Sgn(T x) {\n\treturn (0 < x) - (0 > x);\n}\ntemplate <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr T Pow(T a, U n) {\n\tassert(n >= 0);\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult *= a;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta *= a;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>\nconstexpr T Powmod(T a, U n, T mod) {\n\tassert(n >= 0);\n\tif (a > mod) a %= mod;\n\tT result = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) {\n\t\t\tresult = result * a % mod;\n\t\t\tn--;\n\t\t} else {\n\t\t\ta = a * a % mod;\n\t\t\tn >>= 1;\n\t\t}\n\t}\n\treturn result;\n}\ntemplate <class T> bool chmax(T& a, const T& b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> bool chmin(T& a, const T& b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T> int sz(const T& v) {\n\treturn v.size();\n}\ntemplate <class T, class U> int lower_index(const T& a, const U& v) {\n\treturn lower_bound(all(a), v) - a.begin();\n}\ntemplate <class T, class U> int upper_index(const T& a, const U& v) {\n\treturn upper_bound(all(a), v) - a.begin();\n}\ntemplate <class T> auto Slice(const T& v, size_t i, size_t len) {\n\treturn i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();\n}\ntemplate <class T, class U = typename T::value_type> U Gcdv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);\n}\ntemplate <class T, class U = typename T::value_type> U Lcmv(const T& v) {\n\treturn accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);\n}\nnamespace internal {\n\ttemplate <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {\n\t\tif constexpr (N == 1) {\n\t\t\treturn vector(sizes[0], init);\n\t\t} else {\n\t\t\tint size = sizes[N - 1];\n\t\t\tsizes.pop_back();\n\t\t\treturn vector(size, make_vector<T, N - 1>(sizes, init));\n\t\t}\n\t}\n} // namespace internal\ntemplate <class T, size_t N>\nauto make_vector(const int (&sizes)[N], const T& init = T()) {\n\tvector s(rbegin(sizes), rend(sizes));\n\treturn internal::make_vector<T, N>(s, init);\n}\n#line 9 \"/home/yuruhiya/programming/library/template/template.cpp\"\n#if __has_include(<library/dump.hpp>)\n#include <library/dump.hpp>\n#define LOCAL\n#else\n#define dump(...) ((void)0)\n#endif\n\ntemplate <class T> constexpr T oj_local(const T& oj, const T& local) {\n#ifndef LOCAL\n\treturn oj;\n#else\n\treturn local;\n#endif\n}\n#line 5 \"/home/yuruhiya/programming/library/Graph/GraphTemplate.cpp\"\nusing namespace std;\n\nusing Weight = long long;\nconstexpr Weight INF = numeric_limits<Weight>::max();\nstruct Edge {\n\tint to;\n\tWeight cost;\n\tEdge() : to(-1), cost(-1) {}\n\tEdge(int _to, Weight _cost = 1) : to(_to), cost(_cost) {}\n\tfriend bool operator<(const Edge& e1, const Edge& e2) {\n\t\treturn e1.cost < e2.cost;\n\t}\n\tfriend bool operator>(const Edge& e1, const Edge& e2) {\n\t\treturn e1.cost > e2.cost;\n\t}\n\tfriend ostream& operator<<(ostream& os, const Edge& e) {\n\t\treturn os << \"->\" << e.to << '(' << e.cost << ')';\n\t}\n};\nusing Graph = vector<vector<Edge>>;\nstruct Edge2 {\n\tint from, to;\n\tWeight cost;\n\tEdge2() : from(-1), to(-1), cost(0) {}\n\tEdge2(int _from, int _to, Weight _cost) : from(_from), to(_to), cost(_cost) {}\n\tfriend bool operator<(const Edge2& e1, const Edge2& e2) {\n\t\treturn e1.cost < e2.cost;\n\t}\n\tfriend bool operator>(const Edge2& e1, const Edge2& e2) {\n\t\treturn e1.cost > e2.cost;\n\t}\n\tfriend ostream& operator<<(ostream& os, const Edge2& e) {\n\t\treturn os << e.from << \"->\" << e.to << '(' << e.cost << ')';\n\t}\n};\nusing Edges = vector<Edge2>;\nusing Matrix = vector<vector<Weight>>;\n#line 5 \"/home/yuruhiya/programming/library/Graph/Dijkstra.cpp\"\nusing namespace std;\n\nvector<Weight> Dijkstra(const Graph& graph, int s) {\n\tint V = graph.size();\n\tvector<Weight> dist(V, INF);\n\tdist[s] = 0;\n\tpriority_queue<Edge, vector<Edge>, greater<Edge>> pq;\n\tpq.emplace(s, 0);\n\twhile (!pq.empty()) {\n\t\tEdge p = pq.top();\n\t\tpq.pop();\n\t\tint v = p.to;\n\t\tif (dist[v] < p.cost) continue;\n\t\tfor (auto e : graph[v]) {\n\t\t\tif (dist[e.to] > dist[v] + e.cost) {\n\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\tpq.emplace(e.to, dist[e.to]);\n\t\t\t}\n\t\t}\n\t}\n\treturn dist;\n}\nWeight Dijkstra(const Graph& graph, int s, int t) {\n\tint V = graph.size();\n\tvector<Weight> dist(V, INF);\n\tdist[s] = 0;\n\tpriority_queue<Edge, vector<Edge>, greater<Edge>> pq;\n\tpq.emplace(s, 0);\n\twhile (!pq.empty()) {\n\t\tEdge p = pq.top();\n\t\tpq.pop();\n\t\tint v = p.to;\n\t\tif (v == t) return dist[t];\n\t\tif (dist[v] < p.cost) continue;\n\t\tfor (auto e : graph[v]) {\n\t\t\tif (dist[e.to] > dist[v] + e.cost) {\n\t\t\t\tdist[e.to] = dist[v] + e.cost;\n\t\t\t\tpq.emplace(e.to, dist[e.to]);\n\t\t\t}\n\t\t}\n\t}\n\treturn dist[t];\n}\n#line 3 \"a.cpp\"\n\nint main() {\n\tini(n, x);\n\tauto [d, c] = in.multiple<int, int>(n);\n\n\tconst int MAX_X = TEN(5) * 3;\n\tGraph g(MAX_X);\n\trep(x, MAX_X) {\n\t\trep(i, n) {\n\t\t\tif (x - d[i] >= 0) {\n\t\t\t\tg[x].emplace_back(x - d[i], c[i]);\n\t\t\t}\n\t\t\tif (x + d[i] < MAX_X) {\n\t\t\t\tg[x].emplace_back(x + d[i], c[i]);\n\t\t\t}\n\t\t}\n\t}\n\tll ans = Dijkstra(g, 0, x);\n\tout(ans < inf_ll ? ans : -1);\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 172452, "score_of_the_acc": -1.1538, "final_rank": 14 }, { "submission_id": "aoj_1553_4541970", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint INF = 100000000;\nint main(){\n int N, x;\n cin >> N >> x;\n vector<int> d(N), c(N);\n for (int i = 0; i < N; i++){\n cin >> d[i] >> c[i];\n }\n vector<int> cost(200001, INF);\n cost[0] = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push(make_pair(0, 0));\n while (!pq.empty()){\n int c_cost = pq.top().first;\n int pos = pq.top().second;\n pq.pop();\n for (int i = 0; i < N; i++){\n if (pos + d[i] <= 200000){\n if (cost[pos + d[i]] > c_cost + c[i]){\n cost[pos + d[i]] = c_cost + c[i];\n pq.push(make_pair(cost[pos + d[i]], pos + d[i]));\n }\n }\n }\n for (int i = 0; i < N; i++){\n if (pos - d[i] >= 0){\n if (cost[pos - d[i]] > c_cost + c[i]){\n cost[pos - d[i]] = c_cost + c[i];\n pq.push(make_pair(cost[pos - d[i]], pos - d[i]));\n }\n }\n }\n }\n if (cost[x] == INF){\n cout << -1 << endl;\n } else {\n cout << cost[x] << endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 5604, "score_of_the_acc": -0.1497, "final_rank": 3 }, { "submission_id": "aoj_1553_4541918", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\n#define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)\n#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)\n\n#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)\n#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)\n\n#define debug(x) cerr << \"(\" << __LINE__ << \")\" << #x << \": \" << (x) << endl\n\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n// constexpr i32 mod = 998244353;\nconstexpr i32 mod = 1e9 + 7;\nconstexpr i32 inf = 1001001001;\nconstexpr i64 infll = 1001001001001001001ll;\n\nconstexpr int dx[] = {0, -1, 1, 0, -1, 1, -1, 1};\nconstexpr int dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\nstruct IoSetup { IoSetup(int x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;\n\ntemplate <typename T = i64> T input() { T x; cin >> x; return x; }\n\ntemplate <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? \" \" : \"\"); } return os; }\ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }\n\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\ntemplate <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }\n\ntemplate <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }\ntemplate <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n// }}}\n\nvoid solver() {\n int n, x;\n cin >> n >> x;\n\n vector< int > ds(n), cs(n);\n range(i, 0, n) {\n cin >> ds[i] >> cs[i];\n }\n\n vector< int > dist(101010 * 5, inf);\n int s = 101010 * 3;\n int g = 101010 * 3 + x;\n\n using pii = pair< int, int >;\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n pq.emplace(0, s);\n dist[s] = 0;\n\n while (!pq.empty()) {\n pii p = pq.top();\n pq.pop();\n\n int v = p.second;\n if (dist[v] < p.first) continue;\n\n range(i, 0, n) {\n int u = v + ds[i];\n if (u < 0 || dist.size() <= u) continue;\n if (dist[u] > dist[v] + cs[i]) {\n dist[u] = dist[v] + cs[i];\n pq.emplace(dist[u], u);\n }\n\n u = v - ds[i];\n if (u < 0 || dist.size() <= u) continue;\n if (dist[u] > dist[v] + cs[i]) {\n dist[u] = dist[v] + cs[i];\n pq.emplace(dist[u], u);\n }\n }\n }\n\n\n if (dist[g] == inf) {\n cout << -1 << endl;\n } else {\n cout << dist[g] << endl;\n }\n}\n\nsigned main(int argc, char *argv[]) {\n solver();\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 8968, "score_of_the_acc": -0.3169, "final_rank": 7 }, { "submission_id": "aoj_1553_3801867", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define MAX 200005\n\nstruct Edge{\n\tEdge(int arg_diff,ll arg_cost){\n\t\tdiff = arg_diff;\n\t\tcost = arg_cost;\n\t}\n\n\tint diff;\n\tll cost;\n};\n\nstruct Info{\n\tInfo(int arg_x,ll arg_sum_cost){\n\t\tx = arg_x;\n\t\tsum_cost = arg_sum_cost;\n\t}\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn sum_cost > arg.sum_cost; //総コストの昇順(PQ)\n\t}\n\tint x;\n\tll sum_cost;\n};\n\nbool rangeCheck(int x){\n\n\treturn x >= 0 && x < MAX;\n}\n\nint N;\nint goal;\nvector<Edge> edge;\nll min_cost[MAX];\n\nint main(){\n\n\tscanf(\"%d %d\",&N,&goal);\n\n\tint diff;\n\tll cost;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%d %lld\",&diff,&cost);\n\t\tedge.push_back(Edge(diff,cost));\n\t}\n\n\tfor(int i = 0; i < MAX; i++){\n\n\t\tmin_cost[i] = HUGE_NUM;\n\t}\n\n\tpriority_queue<Info> Q;\n\n\tmin_cost[0] = 0;\n\tQ.push(Info(0,0));\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().sum_cost > min_cost[Q.top().x]){\n\n\t\t\tQ.pop();\n\n\t\t}else if(Q.top().x == goal){\n\n\t\t\tprintf(\"%lld\\n\",Q.top().sum_cost);\n\t\t\treturn 0;\n\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tll next_cost = Q.top().sum_cost+edge[i].cost;\n\n\t\t\t\tint left = Q.top().x-edge[i].diff;\n\t\t\t\tif(rangeCheck(left) == true && min_cost[left] > next_cost){\n\n\t\t\t\t\tmin_cost[left] = next_cost;\n\t\t\t\t\tQ.push(Info(left,next_cost));\n\t\t\t\t}\n\n\t\t\t\tint right = Q.top().x+edge[i].diff;\n\t\t\t\tif(rangeCheck(right) == true && min_cost[right] > next_cost){\n\n\t\t\t\t\tmin_cost[right] = next_cost;\n\t\t\t\t\tQ.push(Info(right,next_cost));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"-1\\n\");\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 8548, "score_of_the_acc": -0.0553, "final_rank": 1 }, { "submission_id": "aoj_1553_3753890", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<string>\n#include<utility>\n#include<map>\n#include<set>\n#include<queue>\n#include<math.h>\nusing namespace std;\n#define N (1000000000+7)\n#define INF 1e16\n#define OFF 200000\ntypedef pair<int,int> P;\ntypedef long long ll;\nll d[20];\nll c[20];\nll dist[400010];\nll n,x;\nvoid dijkstra() {\n\tpriority_queue<P, vector<P>, greater<P> >que;\n\tfill(dist, dist + 400010, (ll)INF);\n\tdist[0+OFF] = 0;\n\tque.push(P(0, 0+OFF));\n\twhile (!que.empty()) {\n\t\tP p = que.top();que.pop();\n\t\tll v = p.second;\n\t\tif (dist[v] < p.first)continue;\n\t\tfor (ll i = 0;i < n;i++) {\n\t\t\tint x=v+d[i];\n int y=v-d[i];\n if(x>400000)continue;\n if(y<0)continue;\n\t\t\tif (dist[x] > dist[v] + c[i]) {\n\t\t\t\tdist[x] = dist[v] + c[i];\n\t\t\t\tque.push(P(dist[x], x));\n\t\t\t}\n if (dist[y] > dist[v] + c[i]) {\n\t\t\t\tdist[y] = dist[v] + c[i];\n\t\t\t\tque.push(P(dist[y], y));\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(void){\n cin>>n>>x;\n for(int i=0;i<n;i++)cin>>d[i]>>c[i];\n dijkstra();\n if(dist[x+OFF]==INF)cout<<-1<<endl;\n else cout<<dist[x+OFF]<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 10024, "score_of_the_acc": -0.2488, "final_rank": 4 }, { "submission_id": "aoj_1553_3564383", "code_snippet": "#include <bits/stdc++.h>\n\nusing pli = std::pair<long long, int>;\n\nint main()\n{\n\tint N, x;\n\tscanf(\"%d%d\", &N, &x);\n\tstd::vector<int> d(N), c(N);\n\tfor (int i{}; i < N; i++)\n\t\tscanf(\"%d%d\", &d[i], &c[i]);\n\n\tint max_place{x + 2 * *std::max_element(d.begin(), d.end())};\n\tstd::vector<long long> dist(max_place + 1, 1LL << 60);\n\tstd::priority_queue<pli, std::vector<pli>, std::greater<pli>> dij;\n\tdist[0] = 0;\n\tdij.push({0, 0});\n\twhile (!dij.empty())\n\t{\n\t\tauto now{dij.top()};\n\t\tdij.pop();\n\t\tif (now.first > dist[now.second]) continue;\n\t\tif (now.second == x) break;\n\t\tfor (int i{}; i < N; i++)\n\t\t{\n\t\t\tint next[2]{now.second - d[i], now.second + d[i]};\n\t\t\tfor (int j{}; j < 2; j++)\n\t\t\t{\n\t\t\t\tif (next[j] < 0 || max_place < next[j]) continue;\n\t\t\t\tif (dist[next[j]] <= now.first + c[i]) continue;\n\t\t\t\tdist[next[j]] = now.first + c[i];\n\t\t\t\tdij.push({dist[next[j]], next[j]});\n\t\t\t}\n\t\t}\n\t}\n\tif (dist[x] == (1LL << 60))\n\t\tputs(\"-1\");\n\telse\n\t\tprintf(\"%lld\\n\", dist[x]);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 9284, "score_of_the_acc": -0.0841, "final_rank": 2 }, { "submission_id": "aoj_1553_3561359", "code_snippet": "#include <bits/stdc++.h>\n#define fi first\n#define se second\n#define inf (int)1e8\nusing namespace std;\n\nstruct data {\n int p;\n int c;\n};\n\nbool operator<(const data &l, const data &r) {\n if(l.c != r.c) return l.c > r.c;\n return l.p > r.p;\n}\n\nint n, x;\nvector<int> dp;\nvector<pair<int, int>> v;\npriority_queue<data> pq;\n\nbool ch(data nextp) {\n return nextp.p >= 0 && nextp.p < dp.size();\n}\nint solve();\n\nint main() {\n cin >> n >> x;\n v.resize(n);\n dp.assign(4 * 100000 + 100, inf);\n for(int i = 0; i < n; ++i) cin >> v[i].fi >> v[i].se;\n cout << solve() << endl;\n return 0;\n}\n\nint solve() {\n x += 100050;\n pq.push({100050, 0});\n dp[100050] = 0;\n while(!pq.empty()) {\n data now = pq.top();\n pq.pop();\n for(int i = 0; i < n; ++i) {\n data nextp = now;\n nextp.p += v[i].fi;\n nextp.c += v[i].se;\n if(ch(nextp) && dp[nextp.p] > nextp.c) {\n dp[nextp.p] = nextp.c;\n pq.push(nextp);\n }\n nextp.p -= v[i].fi * 2;\n if(ch(nextp) && dp[nextp.p] > nextp.c) {\n dp[nextp.p] = nextp.c;\n pq.push(nextp);\n }\n }\n }\n if(dp[x] == inf)\n return -1;\n else\n return dp[x];\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 8544, "score_of_the_acc": -0.3021, "final_rank": 6 }, { "submission_id": "aoj_1553_3560550", "code_snippet": "#include <bits/stdc++.h>\n#define fi first\n#define se second\n#define inf (int)1e8\nusing namespace std;\n\nstruct data {\n int p;\n int c;\n};\n\nbool operator<(const data &l, const data &r) {\n if(l.c != r.c) return l.c > r.c;\n return l.p > r.p;\n}\n\nint n, x;\nvector<int> dp;\nvector<pair<int, int>> v;\npriority_queue<data> pq;\n\nbool ch(data nextp) {\n return nextp.p >= 0 && nextp.p < dp.size();\n}\nint solve();\n\nint main() {\n cin >> n >> x;\n v.resize(n);\n dp.assign(4 * 100000 + 100, inf);\n for(int i = 0; i < n; ++i) cin >> v[i].fi >> v[i].se;\n cout << solve() << endl;\n return 0;\n}\n\nint solve() {\n x += 100050;\n pq.push({100050, 0});\n dp[100000] = 0;\n while(!pq.empty()) {\n data now = pq.top();\n pq.pop();\n for(int i = 0; i < n; ++i) {\n data nextp = now;\n nextp.p += v[i].fi;\n nextp.c += v[i].se;\n if(ch(nextp) && dp[nextp.p] > nextp.c) {\n dp[nextp.p] = nextp.c;\n pq.push(nextp);\n }\n nextp.p -= v[i].fi * 2;\n if(ch(nextp) && dp[nextp.p] > nextp.c) {\n dp[nextp.p] = nextp.c;\n pq.push(nextp);\n }\n }\n }\n if(dp[x] == inf)\n return -1;\n else\n return dp[x];\n}", "accuracy": 0.4, "time_ms": 10, "memory_kb": 4420, "score_of_the_acc": -0.0073, "final_rank": 18 }, { "submission_id": "aoj_1553_3560410", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef pair<int,int> P;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);(i)++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n\n#define INF INT_MAX/3\n\nstruct state{\n int x,d;\n};\nbool operator<(const state& a,const state& b){\n return a.d > b.d;\n}\n\nint m=100010;\nint d[210021];\n\nint main(){\n\n rep(i,210001)d[i]=INF;\n\n int n,x;\n vector<P> ps;\n cin>>n>>x;\n x+=m;\n rep(i,n){\n int a,b; \n cin>>a>>b;\n ps.push_back(P(a,b));\n }\n\n priority_queue<state> que;\n que.push((state){m,0});\n while(que.size()){\n state crt=que.top(); que.pop();\n if(d[crt.x]!=INF)continue;\n d[crt.x]=crt.d;\n // dbg(crt.x); dbg(crt.d);\n rep(i,n)rep(j,2){\n int nx=crt.x+(j==0?ps[i].first:-ps[i].first);\n // dbg(nx);\n if(nx<0||nx>210000)continue;\n que.push((state){nx,crt.d+ps[i].second});\n }\n }\n if(d[x]==INF)cout<<-1<<endl;\n else cout<<d[x]<<endl;\n\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 36624, "score_of_the_acc": -1.1891, "final_rank": 15 }, { "submission_id": "aoj_1553_3431678", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,s,n) for(int i=s; i<n; ++i)\n#define rep(i,n) REP(i,0,n)\n#define SORT(c) sort((c).begin(),(c).end())\n#define IINF INT_MAX\n#define LLINF LLONG_MAX\n#define DEBUG false\n#define MAXI 100010\n\ntypedef long long ll;\ntypedef pair<int, int> ii;\n\nint main(){\n\n\tint n, x;\n\tcin >> n >> x;\n\n\tvector<pair<ll, ll>> v(n);\n\trep(i, n) cin >> v[i].second >> v[i].first;\n\tsort(v.begin(), v.end());\n\n\tpriority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> qu;\n\trep(i, n) qu.push(make_pair(0, 0));\n\n\tvector<bool> used(MAXI*3+1, false);\n\n\twhile(!qu.empty()){\n\t\tauto tmp = qu.top(); qu.pop();\n\n\t\tif(used[tmp.second]) continue;\n\t\tused[tmp.second] = true;\n\n\t\tif(tmp.second == x){\n\t\t\tcout << tmp.first << endl;\n\t\t\treturn 0;\n\t\t}\n\n\t\trep(i, n){\n\t\t\tll cost = tmp.first + v[i].first;\n\t\t\tll nx1 = tmp.second + v[i].second;\n\t\t\tif(0 <= nx1 && nx1 <= 3*MAXI && !used[nx1]) qu.push(make_pair(cost, nx1));\n\t\t\tll nx2 = tmp.second - v[i].second;\n\t\t\tif(0 <= nx2 && nx2 <= 3*MAXI && !used[nx2]) qu.push(make_pair(cost, nx2));\n\t\t}\n\n\t}\n\n\tcout << -1 << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 35740, "score_of_the_acc": -0.8261, "final_rank": 12 }, { "submission_id": "aoj_1553_3431658", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,s,n) for(int i=s; i<n; ++i)\n#define rep(i,n) REP(i,0,n)\n#define SORT(c) sort((c).begin(),(c).end())\n#define IINF INT_MAX\n#define LLINF LLONG_MAX\n#define DEBUG false\n#define MAXI 100010\n\ntypedef long long ll;\ntypedef pair<int, int> ii;\n\nint main(){\n\n\tint n, x;\n\tcin >> n >> x;\n\n\tvector<pair<ll, ll>> v(n);\n\trep(i, n) cin >> v[i].second >> v[i].first;\n\tsort(v.begin(), v.end());\n\n\tpriority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> qu;\n\trep(i, n) qu.push(v[i]);\n\n\tvector<bool> used(MAXI*4+1, false);\n\n\twhile(!qu.empty()){\n\t\tauto tmp = qu.top(); qu.pop();\n\n\t\tif(used[tmp.second]) continue;\n\t\tused[tmp.second] = true;\n\n\t\tif(tmp.second == x){\n\t\t\tcout << tmp.first << endl;\n\t\t\treturn 0;\n\t\t}\n\n\t\trep(i, n){\n\t\t\tll cost = tmp.first + v[i].first;\n\t\t\tll nx1 = tmp.second + v[i].second;\n\t\t\tif(0 <= nx1 && nx1 <= 4*MAXI) qu.push(make_pair(cost, nx1));\n\t\t\tll nx2 = tmp.second - v[i].second;\n\t\t\tif(0 <= nx2 && nx2 <= 4*MAXI) qu.push(make_pair(cost, nx2));\n\t\t}\n\n\t}\n\n\tcout << -1 << endl;\n\n\treturn 0;\n}", "accuracy": 0.4, "time_ms": 10, "memory_kb": 3136, "score_of_the_acc": 0, "final_rank": 17 }, { "submission_id": "aoj_1553_3431007", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,s,n) for(int i=s; i<n; ++i)\n#define rep(i,n) REP(i,0,n)\n#define SORT(c) sort((c).begin(),(c).end())\n#define IINF INT_MAX\n#define LLINF LLONG_MAX\n#define DEBUG false\n#define MAXI 100010\n\ntypedef long long ll;\ntypedef pair<int, int> ii;\n\nint main(){\n\n\tint n, x;\n\tcin >> n >> x;\n\n\n\tvector<pair<ll, ll>> v(n);\n\trep(i, n) cin >> v[i].second >> v[i].first;\n\tsort(v.begin(), v.end());\n\n\tpriority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> qu;\n\trep(i, n) {\n\t\tqu.push(v[i]);\n\t\tqu.push(make_pair(v[i].first, -v[i].second));\n\t}\n\n\tvector<bool> used(MAXI*5+10, false);\n\n\twhile(!qu.empty()){\n\t\tauto tmp = qu.top(); qu.pop();\n\n\t\tif(used[tmp.second+2.5*MAXI+1]) continue;\n\t\tused[tmp.second+2.5*MAXI+1] = true;\n\n\t\tif(tmp.second == x){\n\t\t\tcout << tmp.first << endl;\n\t\t\treturn 0;\n\t\t}\n\n\t\trep(i, n){\n\t\t\tll cost = tmp.first + v[i].first;\n\t\t\tll nx1 = tmp.second + v[i].second;\n\t\t\tif(-2.5*MAXI <= nx1 && nx1 <= 2.5*MAXI) qu.push(make_pair(cost, nx1));\n\t\t\tll nx2 = tmp.second - v[i].second;\n\t\t\tif(-2.5*MAXI <= nx2 && nx2 <= 2.5*MAXI) qu.push(make_pair(cost, nx2));\n\t\t}\n\n\t}\n\n\tcout << -1 << endl;\n\n\treturn 0;\n}", "accuracy": 0.4, "time_ms": 20, "memory_kb": 3148, "score_of_the_acc": -0.0124, "final_rank": 19 }, { "submission_id": "aoj_1553_3431001", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,s,n) for(int i=s; i<n; ++i)\n#define rep(i,n) REP(i,0,n)\n#define SORT(c) sort((c).begin(),(c).end())\n#define IINF INT_MAX\n#define LLINF LLONG_MAX\n#define DEBUG false\n#define MAXI 100010\n\ntypedef long long ll;\ntypedef pair<int, int> ii;\n\nint main(){\n\n\tint n, x;\n\tcin >> n >> x;\n\n\n\tvector<pair<ll, ll>> v(n);\n\trep(i, n) cin >> v[i].second >> v[i].first;\n\tsort(v.begin(), v.end());\n\n\tpriority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> qu;\n\trep(i, n) qu.push(v[i]);\n\n\tvector<bool> used(MAXI*5+10, false);\n\n\twhile(!qu.empty()){\n\t\tauto tmp = qu.top(); qu.pop();\n\n\t\tif(used[tmp.second+2.5*MAXI+1]) continue;\n\t\tused[tmp.second+2.5*MAXI+1] = true;\n\n\t\tif(tmp.second == x){\n\t\t\tcout << tmp.first << endl;\n\t\t\treturn 0;\n\t\t}\n\n\t\trep(i, n){\n\t\t\tll cost = tmp.first + v[i].first;\n\t\t\tll nx1 = tmp.second + v[i].second;\n\t\t\tif(-2.5*MAXI <= nx1 && nx1 <= 2.5*MAXI) qu.push(make_pair(cost, nx1));\n\t\t\tll nx2 = tmp.second - v[i].second;\n\t\t\tif(-2.5*MAXI <= nx2 && nx2 <= 2.5*MAXI) qu.push(make_pair(cost, nx2));\n\t\t}\n\n\t}\n\n\tcout << -1 << endl;\n\n\treturn 0;\n}", "accuracy": 0.4, "time_ms": 20, "memory_kb": 3176, "score_of_the_acc": -0.0126, "final_rank": 20 } ]
aoj_1548_cpp
Problem H: Yu-kun Likes a Directed Graph Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいお絵描きが大好きだ。 これまでゆう君は、丸と矢印で沢山絵を書いてきた。矢印は必ず丸と丸を結ぶようにして描いてある。ある日ゆう君はこれらの絵がグラフであることを知る。 丸は頂点と呼ばれ、矢印は辺と呼ばれているらしい。更にゆう君は、矢印を描く際に、必ずその上に1つ正の整数を書いていた。このように、辺に重みがあり有向な辺からなるグラフは重み付き有向グラフと呼ばれる。 今日ゆう君は、新たに閉路という言葉を知った。閉路とは、連結した辺の列であり、それに含まれるどの頂点も高々1度しか現れないもので、最初の頂点と最後の頂点が同じであるようなものをいう。閉路の辺の重みの総和が負になる場合、そのような閉路は負の閉路と呼ばれる。 新たな言葉を沢山知ったゆう君は一つの問題を思いついた。 Problem 閉路を持たない重み付き有向グラフが与えられる。 異なる2つの頂点 i , j を選び、 i から j に重み w ( w < 0 ) の有向な辺を1本追加する。 これによってグラフ内に負の閉路ができるような i 、 j をみつけ、その負の閉路に属する辺の重みの総和の最大値を求めよ。 そのような i , j が存在しない場合は"NA"と出力すること。 Input V E w s 0 t 0 c 0 s 1 t 1 c 1 ... s (E-1) t (E-1) c (E-1) 1行目に頂点の数 V , 辺の数 E , 追加する辺の重み w が空白区切りで与えられる。 続く E 行に有向な辺の情報が s i t i c i として与えられる。 ( 0 ≤ i ≤ E-1 ) これは、 s i から t i に向けて重み c i の有向な辺が存在することを表す。 Constraints 入力は以下の条件を満たす。 2 ≤ V ≤ 100000 1 ≤ E ≤ 500000 -100 ≤ w < 0 0 ≤ c i ≤ 100 ( 0 ≤ i ≤ E-1 ) 0 ≤ s i , t i ≤ V-1 ( 0 ≤ i ≤ E-1 ) 入力中に同じ s i と t i のペアが現れることはない s i と t i は異なる Output 重み w の有向な辺を追加することで出来る負の閉路に属する辺の重みの総和の最大値を1行に出力せよ。 どこに辺を追加しても負の閉路が作れない場合は"NA"と出力すること。 Sample Input 1 3 2 -3 0 1 1 0 2 5 Sample Output 1 -2 Sample Input 2 3 2 -1 0 1 1 0 2 5 Sample Output 2 NA Sample Input 3 7 8 -8 0 1 5 0 4 3 1 2 10 1 3 1 3 6 6 4 3 1 4 5 2 4 6 2 Sample Output 3 -1 Sample Input 4 5 4 -30 0 1 1 1 3 15 1 4 4 2 1 3 Sample Output 4 -12
[ { "submission_id": "aoj_1548_8295743", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nint N, W;\nint M, A[1 << 19], B[1 << 19], C[1 << 19];\nbool dp[100009][109];\nvector<pair<int, int>> G[100009];\nqueue<pair<int, int>> Q;\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M >> W;\n\tfor (int i = 1; i <= M; i++) cin >> A[i] >> B[i] >> C[i];\n\tfor (int i = 1; i <= M; i++) A[i] += 1;\n\tfor (int i = 1; i <= M; i++) B[i] += 1;\n\tfor (int i = 1; i <= M; i++) G[B[i]].push_back(make_pair(A[i], C[i]));\n\n\t// Step 2. BFS\n\tfor (int i = 1; i <= M; i++) {\n\t\tdp[A[i]][C[i]] = true;\n\t\tQ.push(make_pair(A[i], C[i]));\n\t}\n\twhile (!Q.empty()) {\n\t\tint pos1 = Q.front().first;\n\t\tint pos2 = Q.front().second;\n\t\tQ.pop();\n\t\tfor (pair<int, int> v : G[pos1]) {\n\t\t\tint to = v.first;\n\t\t\tint cost = v.second;\n\t\t\tif (pos2 + cost > -W) continue;\n\t\t\tif (dp[to][pos2 + cost] == false) {\n\t\t\t\tdp[to][pos2 + cost] = true;\n\t\t\t\tQ.push(make_pair(to, pos2 + cost));\n\t\t\t}\n\t\t}\n\t}\n\n\t// Step 3. Output\n\tint Answer = -(1 << 30);\n\tfor (int i = 1; i <= N; i++) {\n\t\tfor (int j = 0; j < -W; j++) {\n\t\t\tif (dp[i][j] == false) continue;\n\t\t\tAnswer = max(Answer, j + W);\n\t\t}\n\t}\n\tif (Answer == -(1 << 30)) cout << \"NA\" << endl;\n\telse cout << Answer << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 33988, "score_of_the_acc": -0.9105, "final_rank": 8 }, { "submission_id": "aoj_1548_8295737", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nint N, W;\nint M, A[1 << 19], B[1 << 19], C[1 << 19];\nbool dp[100009][109];\nvector<pair<int, int>> G[100009];\nqueue<pair<int, int>> Q;\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M >> W;\n\tfor (int i = 1; i <= M; i++) cin >> A[i] >> B[i] >> C[i];\n\tfor (int i = 1; i <= M; i++) A[i] += 1;\n\tfor (int i = 1; i <= M; i++) B[i] += 1;\n\tfor (int i = 1; i <= M; i++) G[B[i]].push_back(make_pair(A[i], C[i]));\n\n\t// Step 2. BFS\n\tfor (int i = 1; i <= N; i++) {\n\t\tdp[i][0] = true;\n\t\tQ.push(make_pair(i, 0));\n\t}\n\twhile (!Q.empty()) {\n\t\tint pos1 = Q.front().first;\n\t\tint pos2 = Q.front().second;\n\t\tQ.pop();\n\t\tfor (pair<int, int> v : G[pos1]) {\n\t\t\tint to = v.first;\n\t\t\tint cost = v.second;\n\t\t\tif (pos2 + cost > -W) continue;\n\t\t\tif (dp[to][pos2 + cost] == false) {\n\t\t\t\tdp[to][pos2 + cost] = true;\n\t\t\t\tQ.push(make_pair(to, pos2 + cost));\n\t\t\t}\n\t\t}\n\t}\n\n\t// Step 3. Output\n\tint Answer = -(1 << 30);\n\tfor (int i = 1; i <= N; i++) {\n\t\tfor (int j = 1; j < -W; j++) {\n\t\t\tif (dp[i][j] == false) continue;\n\t\t\tAnswer = max(Answer, j + W);\n\t\t}\n\t}\n\tif (Answer == -(1 << 30)) cout << \"NA\" << endl;\n\telse cout << Answer << endl;\n\treturn 0;\n}", "accuracy": 0.5483870967741935, "time_ms": 140, "memory_kb": 25748, "score_of_the_acc": -0.2278, "final_rank": 17 }, { "submission_id": "aoj_1548_6066095", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n#define pt(x) cout << x << endl;\n#define Mid ((l + r) / 2)\n#define lson (rt << 1)\n#define rson (rt << 1 | 1)\nusing namespace std;\nconst int N = 2e5 + 1009;\nconst int M = 2e6 + 1009;\nint n, m, w, in[N], f[N][109], ok[N];\nint head[N], nxt[M], ver[M], edge[M], tot = 1;\nvoid add(int x, int y, int w) {\n\tver[++tot] = y; nxt[tot] = head[x]; head[x] = tot; edge[tot] = w;\n}\nqueue<int> q;\nsigned main()\n{\n\tios :: sync_with_stdio(0);\n\tcin.tie(0);\n\tcin >> n >> m >> w;\n\tw = -w;\n\tfor(int i = 1; i <= m; i++) {\n\t\tint x, y, c;\n\t\tcin >> x >> y >> c;\n\t\tx += 1; y += 1;\n\t\tadd(x, y, c);\n\t\tassert(c >= 0);\n\t\tin[y]++;\n\t}\n\tfor(int i = 1; i <= n; i++) {\n\t\tif(in[i] == 0) {\n\t\t\tq.push(i);\n\t\t\tf[i][0] = 1;\n\t\t\tok[i] = 1;\n\t\t}\n\t}\n\t\n\twhile(q.size()) {\n\t\tint x = q.front(); q.pop();\n\t\tfor(int i = head[x]; i; i = nxt[i]) {\n\t\t\tfor(int p = 0; p < w; p++) if(p + edge[i] < w && f[x][p] == 1) {\n\t\t\t\tf[ver[i]][p + edge[i]] |= 1;\n\t\t\t}\n\t\t\tif(edge[i] < w)f[ver[i]][edge[i]] |= 1;\n\t\t\tin[ver[i]]--;\n\t\t\tif(in[ver[i]] == 0) q.push(ver[i]);\n\t\t}\n\t}\n\tfor(int p = w - 1; p >= 0; p--) {\n\t\tfor(int i = 1; i <= n; i++) {\n\t\t\tif(ok[i] == 1) continue;\n\t\t\tif(f[i][p]) {\n\t\t\t\tcout << p - w << endl;\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t}\n\tcout << \"NA\" << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 112788, "score_of_the_acc": -1.0909, "final_rank": 10 }, { "submission_id": "aoj_1548_6016394", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cstdio>\n#include<queue>\nusing namespace std;\n#define MAXN 100010\n#define maxn 500010\nint h,e,w,ans;\nstruct edge{\n\tint root,w;\n};\nint out[MAXN],vis[MAXN][100];\nvector<edge> in[MAXN];\npriority_queue < int,vector<int>,less<int> > val[MAXN];\n \nvoid topo(){\n\tqueue<int> q;\n\tfor(int i=0;i<h;i++){\n\t\tif(!out[i]) q.push(i);\n\t}\n\t\n\twhile(!q.empty()){\n\t\tint f=q.front();\n\t\tq.pop();\n\t\t\n\t\twhile(!val[f].empty()){\n\t\t\tfor(int i=0;i<in[f].size();i++){\n\t\t\t\t\n\t\t\t\tedge x=in[f][i];\n\t\t\t\tout[x.root]--;\n\t\t\t\t\n\t\t\t\tint kk=val[f].top()+x.w;\n\t\t\t\tif(kk+w<0&&!vis[x.root][kk]){\n\t\t\t\t\tvis[x.root][kk]=1;\n\t\t\t\t\tans=max(ans,kk);\n\t\t\t\t\tval[x.root].push(kk);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif(!out[x.root]){\n\t\t\t\t\tq.push(x.root);\n\t\t\t\t}\n\t\t\t}\n\t\t\tval[f].pop();\n\t\t}\n\t\t\n//\t\tcout<<f<<endl;\n\t}\n}\nint read(){\n int x=0,f=1;\n char c=getchar();\n while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}\n while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();\n return x*f;\n}\nint main(){\n//\tfreopen(\"in.txt\",\"r\",stdin);\n\tcin>>h>>e>>w;\n\tans=0;\n\tfor(int i=0;i<h;i++){\n\t\tval[i].push(0);\n\t}\n\tfor(int i=0;i<e;i++){\n\t\tint s,t,v;\n\t\ts=read();\n\t\tt=read();\n\t\tv=read();\n//\t\tscanf(\"%d%d%d\",&s,&t,&v);\n\t\tif(v>-w) continue;\n\t\tout[s]++;\n\t\tedge tmp={s,v};\n\t\tin[t].push_back(tmp);\n\t}\n\ttopo();\n\tif(ans!=0) cout<<ans+w<<endl;\n\telse cout<<\"NA\"<<endl;\n\treturn 0;\n}", "accuracy": 0.5483870967741935, "time_ms": 250, "memory_kb": 102500, "score_of_the_acc": -1.0579, "final_rank": 18 }, { "submission_id": "aoj_1548_4749472", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 100005\n\nstruct Edge{\n\tEdge(int arg_to,int arg_weight){\n\t\tto = arg_to;\n\t\tweight = arg_weight;\n\t}\n\tint to,weight;\n};\n\nint V,E,W;\nint maximum;\nint in_num[SIZE];\nbool dp[SIZE][105];\nvector<Edge> G[SIZE];\n\n//https://betrue12.hateblo.jp/entry/2020/08/09/150101\n\nint main(){\n\n\tscanf(\"%d %d %d\",&V,&E,&W);\n\n\tint abs_W = -W;\n\n\tfor(int i = 0; i < V; i++){\n\n\t\t//dp[終端ノード][重みの総和] := 存在するならtrue\n\t\tdp[i][0] = true;\n\t\tfor(int k = 1; k <= abs_W-1; k++){\n\t\t\tdp[i][k] = false;\n\t\t}\n\t}\n\tfor(int i = 0; i < V; i++){\n\n\t\tin_num[i] = 0;\n\t}\n\n\tint from,to,cost;\n\n\tfor(int i = 0; i < E; i++){\n\n\t\tscanf(\"%d %d %d\",&from,&to,&cost);\n\t\tG[from].push_back(Edge(to,cost));\n\t\tin_num[to]++;\n\t}\n\n\tqueue<int> Q;\n\tfor(int i = 0; i < V; i++){\n\n\t\tif(in_num[i] == 0){\n\n\t\t\tQ.push(i);\n\t\t}\n\t}\n\n\tint maximum = -BIG_NUM;\n\n\twhile(!Q.empty()){\n\n\t\tint node_id = Q.front();\n\t\tQ.pop();\n\n\t\tfor(int i = 0; i <= abs_W-1; i++){\n\n\t\t\tif(!dp[node_id][i])continue;\n\n\t\t\tfor(int k = 0; k < G[node_id].size(); k++){\n\n\t\t\t\tint next_node = G[node_id][k].to;\n\t\t\t\tint next_cost = i+G[node_id][k].weight;\n\n\t\t\t\tif(next_cost >= abs_W)continue;\n\n\t\t\t\tmaximum = max(maximum,next_cost+W);\n\t\t\t\tdp[next_node][next_cost] = true;\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = 0; i < G[node_id].size(); i++){\n\n\t\t\tint next = G[node_id][i].to;\n\t\t\tin_num[next]--;\n\t\t\tif(in_num[next] == 0){\n\n\t\t\t\tQ.push(next);\n\t\t\t}\n\t\t}\n\t}\n\n\tif(maximum == -BIG_NUM){\n\n\t\tprintf(\"NA\\n\");\n\t}else{\n\n\t\tprintf(\"%d\\n\",maximum);\n\t}\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 23872, "score_of_the_acc": -0.2345, "final_rank": 1 }, { "submission_id": "aoj_1548_4748387", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int N, M, W;\n cin >> N >> M >> W;\n vector<vector<pair<int, int>>> edges(N);\n vector<int> indeg(N);\n for(int i=0; i<M; i++){\n int a, b, c;\n cin >> a >> b >> c;\n edges[a].emplace_back(b, c);\n indeg[b]++;\n }\n\n queue<int> que;\n for(int i=0; i<N; i++) if(indeg[i] == 0) que.push(i);\n vector<bitset<101>> dp(N);\n\n while(que.size()){\n int i = que.front();\n que.pop();\n for(auto& p : edges[i]){\n int j = p.first, c = p.second;\n dp[j] |= dp[i]<<c;\n dp[j][c] = 1;\n indeg[j]--;\n if(indeg[j] == 0) que.push(j);\n }\n }\n\n int ans = -1000;\n for(int i=0; i<N; i++) for(int j=0; j<abs(W); j++) if(dp[i][j]) ans = max(ans, j+W);\n if(ans == -1000){\n cout << \"NA\" << endl;\n }else{\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 14980, "score_of_the_acc": -0.2646, "final_rank": 2 }, { "submission_id": "aoj_1548_4748386", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int N, M, W;\n cin >> N >> M >> W;\n vector<vector<pair<int, int>>> edges(N);\n vector<int> indeg(N);\n for(int i=0; i<M; i++){\n int a, b, c;\n cin >> a >> b >> c;\n edges[a].emplace_back(b, c);\n indeg[b]++;\n }\n\n queue<int> que;\n for(int i=0; i<N; i++) if(indeg[i] == 0) que.push(i);\n vector<bitset<101>> dp(N);\n\n while(que.size()){\n int i = que.front();\n que.pop();\n dp[i][0] = 1;\n for(auto& p : edges[i]){\n int j = p.first, c = p.second;\n dp[j] |= dp[i]<<c;\n indeg[j]--;\n if(indeg[j] == 0) que.push(j);\n }\n }\n\n int ans = -1000;\n for(int i=0; i<N; i++) for(int j=1; j<abs(W); j++) if(dp[i][j]) ans = max(ans, j+W);\n if(ans == -1000){\n cout << \"NA\" << endl;\n }else{\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 0.5483870967741935, "time_ms": 60, "memory_kb": 10104, "score_of_the_acc": -0.011, "final_rank": 12 }, { "submission_id": "aoj_1548_3579033", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct graph_scc{\n\tint V;\n\tstatic const int MAX=200000;\n\tvector<int> G[MAX];\n\tvector<int> rG[MAX];\n\tvector<int> vs;\n\tbool used[MAX];\n\tint cmp[MAX];\n\tvoid init(int n){\n\t\tV=n;\n\t\tfor(int i=0;i<V;i++){\n\t\t\tG[i].clear();\n\t\t\trG[i].clear();\n\t\t}\n\t\tvs.clear();\n\t\tmemset(cmp,0,sizeof(cmp));\n\t}\n\tvoid add_edge(int from,int to){\n\t\tG[from].push_back(to);\n\t\trG[to].push_back(from);\n\t}\n\n\tvoid dfs(int v){\n\t\tused[v]=true;\n\t\tfor(int i=0;i<G[v].size();i++){\n\t\t\tif(!used[G[v][i]])dfs(G[v][i]);\n\t\t}\n\t\tvs.push_back(v);\n\t}\n\tvoid rdfs(int v,int k){\n\t\tused[v]=true;\n\t\tcmp[v]=k;\n\t\tfor(int i=0;i<rG[v].size();i++){\n\t\t\tif(!used[rG[v][i]])rdfs(rG[v][i],k);\n\t\t}\n\t}\n\n\tint scc(int n){\n\t\tV=n;\n\t\tmemset(used,0,sizeof(used));\n\t\tvs.clear();\n\t\tfor(int v=0;v<V;v++){\n\t\t\tif(!used[v])dfs(v);\n\t\t}\n\t\tmemset(used,0,sizeof(used));\n\t\tint k=0;\n\t\tfor(int i=vs.size()-1;i>=0;i--){\n\t\t\tif(!used[vs[i]]){\n\t\t\t\trdfs(vs[i],k++);\n\t\t\t}\n\t\t}\n\t\treturn k;\n\t}\n};\n\nint n,m,w;\nvector<P> G[100005];\ngraph_scc gr;\nint ord[100005];\nbool dp[100000][105];\n\nint main(void){\n\tscanf(\"%d%d%d\",&n,&m,&w);\n\tbool zero=false;\n\tfor(int i=0;i<m;i++){\n\t\tint a,b,c;\n\t\tscanf(\"%d%d%d\",&a,&b,&c);\n\t\tgr.add_edge(a,b);\n\t\tif(c==0)zero=true;\n\t\tG[a].push_back(P(b,c));\n\t}\n\tgr.scc(n);\n\tfor(int i=0;i<n;i++){\n\t\tord[gr.cmp[i]]=i;\n\t\tdp[i][0]=true;\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tint v=ord[i];\n\t\tfor(int j=0;j<G[v].size();j++){\n\t\t\tint to=G[v][j].first;\n\t\t\tint cost=G[v][j].second;\n\t\t\tfor(int k=0;k+cost<=100;k++){\n\t\t\t\tif(dp[v][k]){\n\t\t\t\t\tdp[to][k+cost]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ma=-1;\n\tif(zero)ma=0;\n\tfor(int i=0;i<n;i++){\n\t\tfor(int j=1;j<-w;j++){\n\t\t\tif(dp[i][j]){\n\t\t\t\tma=max(ma,j);\n\t\t\t}\n\t\t}\n\t}\n\tif(ma==-1){\n\t\tprintf(\"NA\\n\");\n\t}else{\n\t\tprintf(\"%d\\n\",ma+w);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 320, "memory_kb": 46068, "score_of_the_acc": -0.5723, "final_rank": 4 }, { "submission_id": "aoj_1548_3578228", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\nusing Info = bitset<101>;\n\nvector<int> make_dag(vector<vector<int>> edges) {\n\tconst int N = edges.size();\n\tvector<int> parnum(N);\n\tfor (auto row : edges)for (auto col : row)parnum[col]++;\n\n\tqueue<int> q;\n\tvector<int> res;\n\tfor (int i = 0; i < N; i++)if (parnum[i] == 0)q.push(i);\n\n\twhile (!q.empty()) {\n\t\tint v = q.front(); q.pop();\n\t\tfor (auto u : edges[v]) {\n\t\t\tparnum[u]--;\n\t\t\tif (parnum[u] == 0) {\n\t\t\t\tq.push(u);\n\t\t\t}\n\t\t}\n\t\tres.push_back(v);\n\t}\n\treturn res;\n}\n\nint main() {\n\tint V, E, w;\n\tcin >> V >> E >> w;\n\tvector<map<int, int>> edges(V);\n\tvector<vector<int>> parents(V), fordag(V);\n\n\tfor (int i = 0; i < E; i++) {\n\t\tint s, t, c;\n\t\tcin >> s >> t >> c;\n\t\tedges[s][t] = c;\n\t\tparents[t].push_back(s);\n\t\tfordag[s].push_back(t);\n\t}\n\n\t// dag\n\tvector<int> dag = make_dag(fordag);\n\n\t// way\n\tvector<Info> res(V);\n\tfor (auto& pos : dag) {\n\t\tfor (int& parent : parents[pos]) {\n\t\t\tint len = edges[parent][pos];\n\t\t\tfor (int i = 0; i < 101; i++) if (res[parent][i]) {\n\t\t\t\tint score = i + len;\n\t\t\t\tif (score + w < 0)res[pos][score] = true;\n\t\t\t}\n\t\t\tif (len + w < 0)res[pos][len] = true;\n\t\t}\n\t}\n\n\tint ans = -1;\n\tfor (const auto& row : res)\n\t\tfor (int i = 0; i < 101; i++)if (row[i])ans = max(ans, i);\n\n\tcout << (ans == -1 ? \"NA\" : to_string(ans + w)) << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 730, "memory_kb": 51888, "score_of_the_acc": -0.9672, "final_rank": 9 }, { "submission_id": "aoj_1548_3323456", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nusing pi = pair<int,int>;\n\nconst int N = 100000;\nconst int W = 100;\nbool vis[N][W];\n\nstruct edge{ int to,cost; };\nvector<edge> G[N];\n\nconst int INF = 19191919;\n\nint main(){\n int n,m,w;\n scanf(\" %d %d %d\", &n, &m, &w);\n\n rep(i,m){\n int s,t,c;\n scanf(\" %d %d %d\", &s, &t, &c);\n G[s].pb({t,c});\n }\n\n queue<pi> que;\n rep(i,n) que.push({i,0});\n while(!que.empty()){\n pi now = que.front();\n que.pop();\n\n int v = now.fi, c = now.se;\n for(const auto &e:G[v]){\n int nv = e.to;\n int nc = c+e.cost;\n if(nc<W && !vis[nv][nc]){\n vis[nv][nc] = true;\n que.push({nv,nc});\n }\n }\n }\n\n int ans = -INF;\n rep(i,n){\n rep(j,-w)if(vis[i][j]) ans = max(ans, j+w);\n }\n if(ans == -INF) printf(\"NA\\n\");\n else printf(\"%d\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 1270, "memory_kb": 39732, "score_of_the_acc": -1.2964, "final_rank": 11 }, { "submission_id": "aoj_1548_1673172", "code_snippet": "/*\n * h.cc: \n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n\nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\nconst int MAX_W = 100;\n\n/* typedef */\n\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\n\n/* global variables */\n\nint w, ans = 0;\nvpii nbrs[MAX_N];\nbool ews[MAX_N][MAX_W];\n\n/* subroutines */\n\nvoid rec(int u) {\n ews[u][0] = true;\n vpii &nbru = nbrs[u];\n\n for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {\n int &v = vit->first, &vc = vit->second;\n if (! ews[v][0]) rec(v);\n int maxi = w - vc;\n for (int i = 0; i < maxi; i++)\n if (ews[v][i]) {\n\tint wsum = vc + i;\n\tif (! ews[u][wsum]) {\n\t ews[u][wsum] = true;\n\t if (ans < wsum) ans = wsum;\n\t}\n }\n }\n}\n\n/* main */\n\nint main() {\n int vn, en;\n cin >> vn >> en >> w;\n w = -w;\n\n for (int i = 0; i < en; i++) {\n int si, ti, ci;\n cin >> si >> ti >> ci;\n nbrs[si].push_back(pii(ti, ci));\n }\n\n for (int i = 0; i < vn; i++)\n if (! ews[i][0]) rec(i);\n\n if (ans == 0) cout << \"NA\" << endl;\n else printf(\"%d\\n\", ans - w);\n return 0;\n}", "accuracy": 0.5483870967741935, "time_ms": 90, "memory_kb": 19540, "score_of_the_acc": -0.1267, "final_rank": 16 }, { "submission_id": "aoj_1548_1673151", "code_snippet": "/*\n * h.cc: \n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<bitset>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n\nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\nconst int MAX_W = 100;\n\n/* typedef */\n\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\ntypedef bitset<MAX_W> bsw;\n\n/* global variables */\n\nint w, ans = 0;\nvpii nbrs[MAX_N];\nbsw ews[MAX_N];\n\n/* subroutines */\n\nvoid rec(int u) {\n ews[u].set(0);\n vpii &nbru = nbrs[u];\n\n for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {\n int &v = vit->first, &vw = vit->second;\n if (ews[v].none()) rec(v);\n int maxi = w - vw;\n for (int i = 0; i < maxi; i++)\n if (ews[v].test(i)) {\n\tint wsum = vw + i;\n\tif (! ews[u].test(wsum)) {\n\t ews[u].set(wsum);\n\t if (ans < wsum) ans = wsum;\n\t}\n }\n }\n}\n\n/* main */\n\nint main() {\n int vn, en;\n cin >> vn >> en >> w;\n w = -w;\n\n for (int i = 0; i < en; i++) {\n int si, ti, ci;\n cin >> si >> ti >> ci;\n nbrs[si].push_back(pii(ti, ci));\n }\n\n for (int i = 0; i < vn; i++)\n if (ews[i].none()) rec(i);\n\n if (ans == 0) cout << \"NA\" << endl;\n else printf(\"%d\\n\", ans - w);\n return 0;\n}", "accuracy": 0.5483870967741935, "time_ms": 100, "memory_kb": 12900, "score_of_the_acc": -0.071, "final_rank": 14 }, { "submission_id": "aoj_1548_1082434", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<string>\n#include<cstring>\n#include<vector>\n#include<set>\n#include<list>\n#include<queue>\n#include<cmath>\n#include<functional>\n#include<algorithm>\n#include<bitset>\n#define INF (1<<29)\n#define EPS 1e-10\n#define rep(i,n) for(int i=0;i<(n);i++)\nusing namespace std;\n\n\nbool visited[100000];\npair<bitset<100>,int> dp[100000];\n\nint v,e,w;\nvector<pair<int,int> > edge[100000];\n\npair<bitset<100>,int> dfs(int u){\n\t if(visited[u])return dp[u];\n\tvisited[u]=true;\n\tpair<bitset<100>,int> res;\n\tres.second=-INF;\n\trep(i,edge[u].size()){\n\t\tpair<int,int> pi=edge[u][i];\n\t\tpair<bitset<100>,int> x=dfs(pi.first);\n\t\tx.first<<=pi.second;\n\t\tres.first|=x.first;\n\t\tres.second=max(res.second,x.second);\n\t}\n\tfor(int i=-w-1;i>=0;i--){\n\t\tif(res.first[i]){\n\t\t\tres.second=max(res.second,i+w);\n\t\t\tbreak;\n\t\t}\n\t}\n\tres.first.set(0);\n\treturn dp[u]=res;\n}\n\n\n\nint main(){\n\tcin>>v>>e>>w;\n\trep(i,e){\n\t\tint s,t,c;\n\t\tcin>>s>>t>>c;\n\t\tedge[s].push_back(make_pair(t,c));\n\t}\n\tint ans=-INF;\n\trep(i,v){\n\t\tif(!visited[i])ans=max(ans,dfs(i).second);\n\t}\n\tif(ans!=-INF)cout<<ans<<endl;\n\telse cout<<\"NA\"<<endl;\n\treturn 0;\n}", "accuracy": 0.5161290322580645, "time_ms": 80, "memory_kb": 17168, "score_of_the_acc": -0.0956, "final_rank": 19 }, { "submission_id": "aoj_1548_1081221", "code_snippet": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include <string>\n#include <iomanip>\n#include <sstream>\n#include <bitset>\n#include <valarray>\n#include <iterator>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned int uint;\ntypedef unsigned char uchar;\ntypedef unsigned long long ull;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\n#define REP(i,x) for(int i=0;i<(int)(x);i++)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)\n#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)\n#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)\n#define ALL(container) container.begin(), container.end()\n#define RALL(container) container.rbegin(), container.rend()\n#define SZ(container) ((int)container.size())\n#define mp(a,b) make_pair(a, b)\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n\ntemplate<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }\ntemplate<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"[\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"]\"; return os;\n}\ntemplate<class T> ostream& operator<<(ostream &os, const set<T> &t) {\nos<<\"{\"; FOR(it,t) {if(it!=t.begin()) os<<\",\"; os<<*it;} os<<\"}\"; return os;\n}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\n\nconst int INF = 1<<28;\nconst double EPS = 1e-8;\nconst int MOD = 1000000007;\n\n\nint n, m, w;\n\nint dp[100001][101];\n\nmain(){\n\tscanf(\"%d %d %d\", &n, &m, &w);\n\tvector<vector<pii>> g(n);\n\tvi deg(n);\n\tREP(i, m){\n\t\tint a, b, c;\n\t\tscanf(\"%d%d%d\", &a, &b, &c);\n\t\tg[a].emplace_back(b, c);\n\t\tdeg[b] ++;\n\t}\n\tqueue<int> que;\n\tREP(i, n) if(deg[i] == 0) que.push(i);\n\tint ans = -1;\n\twhile(!que.empty()){\n\t\tint u = que.front();\n\t\tque.pop();\n\t\tFOR(e, g[u]){\n\t\t\tint v = e->first;\n\t\t\tint c = e->second;\n\t\t\tREP(i, -w-c) dp[v][i+c] |= dp[u][i];\n\t\t\tdp[v][c] = 1;\n\t\t\tif(--deg[v] == 0) que.push(v);\n\t\t}\n\t\tREP(i, -w) if(dp[u][i]) ans = max(ans, i);\n\t}\n\tif(ans < 0) puts(\"NA\");\n\telse printf(\"%d\\n\", ans+w);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 50992, "score_of_the_acc": -0.5784, "final_rank": 5 }, { "submission_id": "aoj_1548_1080966", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> pii;\n\nbool dp[100100][100];\nvector<pii> g[100100];\n\nint main(){\n int v,e,w;\n cin >> v >> e >> w; w*=-1;\n \n vector<int> deg(v,0);\n for(int i=0;i<e;i++){\n int s,t,c;\n cin >> s >> t >> c;\n g[s].push_back(pii(t,c));\n deg[t]++;\n }\n\n vector<int> to;\n queue<int> q;\n for(int i=0;i<v;i++){\n if(deg[i]==0)q.push(i);\n }\n\n while(q.size()){\n int x = q.front(); q.pop();\n to.push_back(x);\n for(int i=0;i<(int)g[x].size();i++){\n int u = g[x][i].first;\n deg[u]--;\n if(deg[u]==0)q.push(u);\n }\n }\n\n memset(dp,0,sizeof(dp));\n for(int i=0;i<v;i++)dp[i][0] = 1;\n\n int ans = -1;\n for(int i=0;i<v;i++){\n int x = to[i];\n for(int k=0;k<w;k++){\n if(!dp[x][k])continue;\n\n for(int j=0;j<(int)g[x].size();j++){\n\tint u = g[x][j].first, cost = k+g[x][j].second;\n\tif(cost<w){\n\t dp[u][cost] = true;\n\t ans = max(ans,cost);\n\t}\n }\n }\n }\n\n if(ans<0)cout << \"NA\" << endl;\n else cout << ans - w << endl;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 21788, "score_of_the_acc": -0.4541, "final_rank": 3 }, { "submission_id": "aoj_1548_1080965", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> pii;\n\nbool dp[100100][100];\nvector<pii> g[100100];\n\nint main(){\n int v,e,w;\n cin >> v >> e >> w; w*=-1;\n \n vector<int> deg(v,0);\n for(int i=0;i<e;i++){\n int s,t,c;\n cin >> s >> t >> c;\n g[s].push_back(pii(t,c));\n deg[t]++;\n }\n\n vector<int> to;\n queue<int> q;\n for(int i=0;i<v;i++){\n if(deg[i]==0)q.push(i);\n }\n\n while(q.size()){\n int x = q.front(); q.pop();\n to.push_back(x);\n for(int i=0;i<(int)g[x].size();i++){\n int u = g[x][i].first;\n deg[u]--;\n if(deg[u]==0)q.push(u);\n }\n }\n\n memset(dp,0,sizeof(dp));\n for(int i=0;i<v;i++)dp[i][0] = 1;\n\n int ans = 0;\n for(int i=0;i<v;i++){\n int x = to[i];\n for(int k=0;k<w;k++){\n if(!dp[x][k])continue;\n\n for(int j=0;j<(int)g[x].size();j++){\n\tint u = g[x][j].first, cost = k+g[x][j].second;\n\tif(cost<w){\n\t dp[u][cost] = true;\n\t ans = max(ans,cost);\n\t}\n }\n }\n }\n\n if(ans == 0)cout << \"NA\" << endl;\n else cout << ans - w << endl;\n}", "accuracy": 0.5483870967741935, "time_ms": 100, "memory_kb": 17120, "score_of_the_acc": -0.1116, "final_rank": 15 }, { "submission_id": "aoj_1548_1080819", "code_snippet": "#include <string>\n#include <vector>\n#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<list>\n#include<deque>\n#include<bitset>\n#include<set>\n#include<map>\n#include<cstring>\n#include<sstream>\n#include<complex>\n#define X first\n#define Y second\n#define pb push_back\n#define rep(X,Y) for (int (X) = 0;(X) < int(Y);++(X))\n#define rrep(X,Y) for (int (X) = int(Y-1);(X) >=0;--(X))\n#define all(X) (X).begin(),(X).end()\n#define rall(X) (X).rbegin(),(X).rend()\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\nint v,e,w,memo[112345],re;\npii cur[112345];\nvector<pii> edges[2][112345];\nset<int> st[2][112345];\n\nvoid solve(int p,int mode){\n if(st[mode][p].size())\n return;\n st[mode][p].insert(0);\n rep(i,edges[mode][p].size()){\n pii tmp=edges[mode][p][i];\n solve(tmp.X,mode);\n// st[mode][p].insert(tmp.Y);\n for(auto val:st[mode][tmp.X])\n if(val+tmp.Y<w)\n st[mode][p].insert(val+tmp.Y);\n }\n return;\n}\n\nint main(){\n int i,j,k;\n cin>>v>>e>>w;\n w=-w;\n int s,t,c;\n rep(i,e){\n cin>>s>>t>>c;\n edges[0][s].pb(pii(t,c));\n edges[1][t].pb(pii(s,c));\n re=-1;\n }\n rep(i,v)\n solve(i,0);\n rep(i,v)\n solve(i,1);\n rep(i,v){\n for(auto val:st[0][i]){\n auto tmp=st[1][i].lower_bound(w-val);\n if(tmp!=st[1][i].begin()){\n tmp--;\n // cout<<\"-\"<<*tmp<<\",\";\n if(*tmp+val)re=max(re,*tmp+val);\n }\n }\n }\n if(re<0)\n cout<<\"NA\"<<endl;\n else\n cout<<re-w<<endl;\n return 0;\n}", "accuracy": 0.5161290322580645, "time_ms": 100, "memory_kb": 34960, "score_of_the_acc": -0.2835, "final_rank": 20 }, { "submission_id": "aoj_1548_1080551", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef pair<int, int> edge;\n#define to first\n#define cost second\n\nint main() {\n cin.tie(NULL);\n ios::sync_with_stdio(false);\n\n int n, m, w;\n cin >> n >> m >> w;\n\n vector<int> in(n, 0);\n vector<vector<edge>> G(n);\n for(int i = 0; i < m; ++i) {\n int s, t, c;\n cin >> s >> t >> c;\n G[s].emplace_back(t, c);\n ++in[t];\n }\n\n const int abs_w = abs(w);\n\n vector<vector<int>> dp(n, vector<int>(abs_w, false));\n queue<int> que;\n\n for(int v = 0; v < n; ++v) {\n if(in[v] == 0) que.push(v);\n }\n\n int ans = INT_MIN;\n while(!que.empty()) {\n const int v = que.front();\n que.pop();\n\n for(int i = 0; i < abs_w; ++i) {\n if(dp[v][i]) {\n\tans = max(ans, i - abs_w);\n }\n }\n\n for(const auto &e : G[v]) {\n for(int i = abs_w - 1 - e.cost; i >= 0; --i) {\n\tdp[e.to][i + e.cost] |= (i == 0 ? true : dp[v][i]);\n }\n\n if(--in[e.to] == 0) {\n\tque.push(e.to);\n }\n }\n }\n\n cout << (ans == INT_MIN ? \"NA\" : to_string(ans)) << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 54588, "score_of_the_acc": -0.6461, "final_rank": 6 }, { "submission_id": "aoj_1548_1080518", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\nusing namespace std;\n\n#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)\ntemplate <typename T> void print_container(ostream& os, const T& c) { const char* _s = \" \"; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\n\ntemplate <typename T> void print(T a, int n, const string& split = \" \") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }\ntemplate <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }\ntemplate <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }\n#define dump(v) (cerr << #v << \": \" << v << endl)\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\n\nint main()\n{\n int n, m, w;\n scanf(\"%d%d%d\", &n, &m, &w);\n const int max_x = -w - 1;\n\n static vector<pint> g[ten(5) + 1919];\n rep(i, m)\n {\n int u, v, c;\n scanf(\"%d%d%d\", &u, &v, &c);\n g[u].push_back(pint(v, c));\n }\n\n static bitset<128> dp[ten(5) + 1919];\n queue<pint> q;\n rep(i, n)\n q.push(pint(i, 0));\n while (!q.empty())\n {\n int pos = q.front().first;\n int cost = q.front().second;\n q.pop();\n// printf(\"%d %d\\n\", pos, cost);\n\n for (auto& e : g[pos])\n {\n int to = e.first;\n int ncost = cost + e.second;\n if (ncost <= max_x && !dp[to][ncost])\n {\n q.push(pint(to, ncost));\n dp[to][ncost] = true;\n }\n }\n }\n\n int mx = -1;\n rep(i, n) erep(j, max_x)\n if (dp[i][j])\n upmax(mx, j);\n if (mx == -1)\n puts(\"NA\");\n else\n printf(\"%d\\n\", mx + w);\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 14800, "score_of_the_acc": -0.6926, "final_rank": 7 }, { "submission_id": "aoj_1548_1080516", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <list>\n#include <iomanip>\n#include <fstream>\n#include <bitset>\n\nusing namespace std;\n\n#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)\ntemplate <typename T> void print_container(ostream& os, const T& c) { const char* _s = \" \"; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\n\ntemplate <typename T> void print(T a, int n, const string& split = \" \") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }\ntemplate <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }\ntemplate <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }\n#define dump(v) (cerr << #v << \": \" << v << endl)\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define clr(a, x) memset(a, x, sizeof(a))\n#define sz(a) ((int)(a).size())\n#define mp(a, b) make_pair(a, b)\n#define ten(n) ((long long)(1e##n))\n\ntemplate <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }\ntemplate <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }\ntemplate <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }\ntemplate <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }\ntemplate <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }\nvoid fast_io() { cin.tie(0); ios::sync_with_stdio(false); }\nbool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }\n\ntypedef long long ll;\ntypedef pair<int, int> pint;\n\nconst int dx[] = { 0, 1, 0, -1 };\nconst int dy[] = { 1, 0, -1, 0 };\n\n\nint main()\n{\n int n, m, w;\n scanf(\"%d%d%d\", &n, &m, &w);\n const int max_x = -w - 1;\n\n static vector<pint> g[ten(5) + 1919];\n rep(i, m)\n {\n int u, v, c;\n scanf(\"%d%d%d\", &u, &v, &c);\n g[u].push_back(pint(v, c));\n }\n\n static bitset<128> dp[ten(5) + 1919];\n queue<pint> q;\n rep(i, n)\n {\n q.push(pint(i, 0));\n dp[i][0] = true;\n }\n while (!q.empty())\n {\n int pos = q.front().first;\n int cost = q.front().second;\n q.pop();\n// printf(\"%d %d\\n\", pos, cost);\n\n for (auto& e : g[pos])\n {\n int to = e.first;\n int ncost = cost + e.second;\n if (ncost <= max_x && !dp[to][ncost])\n {\n q.push(pint(to, ncost));\n dp[to][ncost] = true;\n }\n }\n }\n\n int mx = 0;\n rep(i, n) erep(j, max_x)\n if (dp[i][j])\n upmax(mx, j);\n if (mx == 0)\n puts(\"NA\");\n else\n printf(\"%d\\n\", mx + w);\n}", "accuracy": 0.5483870967741935, "time_ms": 120, "memory_kb": 8960, "score_of_the_acc": -0.0496, "final_rank": 13 } ]
aoj_1556_cpp
Problem F: Sum of Numbers Problem a 以上 b 以下の整数が書かれたカードがそれぞれ c 枚ずつあります。 これら c(b-a+1) 枚のカードの中から d 枚のカードを選択したとき、それらのカードに書かれている整数の合計が e になる場合の数を1,000,000,007で割った余りを求めてください。 Input 入力は以下の形式で与えられる。 a b c d e 1行目に、5つの整数 a b c d e が空白区切りで与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ a ≤ 1000 a ≤ b ≤ 1000 1 ≤ c ≤ 100 1 ≤ d ≤ 1000 c ≤ d ≤ c(b-a+1) 1 ≤ e ≤ 20000 Output 場合の数を1,000,000,007で割った余りを出力せよ。 Sample Input1 2 5 3 3 11 Sample Output1 3 {2,2,2,3,3,3,4,4,4,5,5,5}の中から3枚選び合計が11になるのは、 {2,4,5},{3,3,5},{3,4,4}の3通りである。 Sample Input2 1 2 3 4 100 Sample Output2 0 {1,1,1,2,2,2}の中から4枚選び合計が100になるのは0通りである。
[ { "submission_id": "aoj_1556_10431881", "code_snippet": "// AOJ #1556 Sum of Numbers\n// 2025.4.29\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1000000007;\n\nconst int M_SIZE = 108;\nconst int J_SIZE = 21600;\nll dp[M_SIZE][J_SIZE];\nll dp1_arr[M_SIZE][J_SIZE];\nll dp2_arr[M_SIZE][J_SIZE];\n\nint main(){\n int a, b, c, d, e;\n cin >> a >> b >> c >> d >> e;\n b -= a;\n e -= (ll)a * d;\n if(e < 0){\n cout << 0 << endl;\n return 0;\n }\n\n dp[0][0] = dp1_arr[0][0] = dp2_arr[0][0] = 1;\n for(int i = 0; i <= d; i++){\n for(int j = 0; j <= e; j++){\n if(i == 0 && j == 0) continue;\n ll val = 0;\n\n int l = max(0, i - j);\n int r = min(c, i);\n if(l <= r){\n int i1 = i - l;\n int j1 = j - (i - l);\n if(j1 >= 0) val += dp1_arr[i1 % M_SIZE][j1];\n\n int i2 = i - (r + 1);\n int j2 = j - (i - (r + 1));\n if(r + 1 <= i && j2 >= 0) val -= dp1_arr[i2 % M_SIZE][j2];\n }\n\n int l2 = 1;\n int r2 = min(c, i);\n r2 = min(r2, j / (b + 1));\n if(l2 <= r2){\n int i3 = i - l2;\n int j3 = j - (b + 1) * l2;\n if(j3 >= 0) val -= dp2_arr[i3 % M_SIZE][j3];\n\n int i4 = i - (r2 + 1);\n int j4 = j - (b + 1) * (r2 + 1);\n if(r2 + 1 <= i && j4 >= 0) val += dp2_arr[i4 % M_SIZE][j4];\n }\n val %= MOD;\n if(val < 0) val += MOD;\n dp[i % M_SIZE][j] = val;\n\n ll v1 = val;\n if(i > 0 && j + 1 <= e) v1 = (v1 + dp1_arr[(i - 1) % M_SIZE][j + 1]) % MOD;\n dp1_arr[i % M_SIZE][j] = v1;\n\n ll v2 = val;\n if(i > 0 && j - (b + 1) >= 0) v2 = (v2 + dp2_arr[(i - 1) % M_SIZE][j - (b + 1)]) % MOD;\n dp2_arr[i % M_SIZE][j] = v2;\n }\n }\n cout << dp[d % M_SIZE][e] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 57668, "score_of_the_acc": -0.018, "final_rank": 1 }, { "submission_id": "aoj_1556_5981731", "code_snippet": "//Let's join Kaede Takagaki Fan Club !!\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cassert>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <iostream>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n#include <cassert>\n#include <iomanip>\n#include <chrono>\n#include <random>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n#include <bitset>\n//#include <atcoder/segtree>\nusing namespace std;\n//using namespace atcoder;\n//#define int long long\n//#define L __int128\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\ntypedef pair<P,P> P2;\n#define pu push\n#define pb push_back\n#define eb emplace_back\n#define mp make_pair\n#define eps 1e-7\n#define INF 1000000000\n#define a first\n#define b second\n#define fi first\n#define sc second\n#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,x) for(int i=0;i<x;i++)\n#define repn(i,x) for(int i=1;i<=x;i++)\n#define SORT(x) sort(x.begin(),x.end())\n#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())\n#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())\n#define all(x) x.begin(),x.end()\n#define si(x) int(x.size())\n \n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"<<x<<endl\n#else\n#define dmp(x) void(0)\n#endif\n \ntemplate<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}\ntemplate<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}\n \ntemplate<class t> using vc=vector<t>;\n \ntemplate<class t,class u>\nostream& operator<<(ostream& os,const pair<t,u>& p){\n\treturn os<<\"{\"<<p.fi<<\",\"<<p.sc<<\"}\";\n}\n \ntemplate<class t> ostream& operator<<(ostream& os,const vc<t>& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<<e<<\",\";\n\treturn os<<\"}\";\n}\n \ntemplate<class T>\nvoid g(T &a){\n\tcin >> a;\n}\ntemplate<class T>\nvoid o(const T &a,bool space=false){\n\tcout << a << (space?' ':'\\n');\n}\n//ios::sync_with_stdio(false);\n//const ll mod = 998244353;\nconst ll mod = 1000000007;\nmt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());\ntemplate<class T>\nvoid add(T&a,T b){\n\ta+=b;\n\tif(a >= mod) a-=mod;\n}\nll modpow(ll x,ll n){\n\tll res=1;\n\twhile(n>0){\n\t\tif(n&1) res=res*x%mod;\n\t\tx=x*x%mod;\n\t\tn>>=1;\n\t}\n\treturn res;\n}\n#define _sz 400005\nll F[_sz],R[_sz];\nvoid make(){\n\tF[0] = 1;\n\tfor(int i=1;i<_sz;i++) F[i] = F[i-1]*i%mod;\n\tR[_sz-1] = modpow(F[_sz-1], mod-2);\n\tfor(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1) % mod;\n}\nll C(int a,int b){\n\tif(b < 0 || a < b) return 0;\n\treturn F[a]*R[b]%mod*R[a-b]%mod;\n}\nint a, b, c, d, e, dp[1005][20005], pre[1005][20005];\nvoid solve(){\n\tcin >> a >> b >> c >> d >> e;\n\tdp[0][0] = 1;\n\tfor(int use=b;use>=a;use--){\n\t\tfor(int cur=0;cur<=min(d, e/use);cur++){\n\t\t\tfor(int sum=cur*use;sum<=e;sum++){\n\t\t\t\tpre[cur][sum] = dp[cur][sum];\n\t\t\t\tif(cur >= 1 and sum >= use){\n\t\t\t\t pre[cur][sum] += pre[cur-1][sum-use];\n\t\t\t\t if(pre[cur][sum] >= mod) pre[cur][sum] -= mod;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int cur=min(d, e/use);cur>=1;cur--){\n\t\t\tfor(int sum=e;sum>=cur*use;sum--){\n\t\t\t\t//dp[cur][sum]\n\t\t\t\tdp[cur][sum] = pre[cur][sum];\n\t\t\t\tif(cur >= c+1 and sum >= (c+1)*use){\n\t\t\t\t dp[cur][sum] += mod-pre[cur-c-1][sum-(c+1)*use];\n\t\t\t\t}\n\t\t\t\tif(dp[cur][sum]>=mod)dp[cur][sum]-=mod;\n\t\t\t}\n\t\t}\n\t}\n\to(dp[d][e]);\n}\nsigned main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\tint t; t = 1; //cin >> t;\n\twhile(t--) solve();\n}", "accuracy": 1, "time_ms": 1490, "memory_kb": 161144, "score_of_the_acc": -2, "final_rank": 3 }, { "submission_id": "aoj_1556_1251906", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\n#define MOD 1000000007\n#define M 108\ntypedef long long Int;\n\nint a, b, c, d, e;\nInt dp[108][21600];\nInt dp1[108][21600];\nInt dp2[108][21600];\nint main(){\n cin >> a >> b >> c >> d >> e;\n b -= a;\n e -= a*d;\n dp[0][0] = 1;\n dp1[0][0] = 1;\n dp2[0][0] = 1;\n for(int i = 0;i <= d;i++){\n for(int j = 0;j < 21500;j++){\n if(i == 0 && j == 0)continue;\n dp[i%M][j] = 0; \n int l = max(0, i - j);\n int r = min(c, i);\n\t if(l <= r){\n\t if(i-l>=0 && j - (i-l) >= 0)dp[i%M][j] += dp1[(i-l)%M][j-(i-l)];\n\t if(i-(r+1) >= 0 && j-(i-(r+1)) >= 0)dp[i%M][j] -= dp1[(i-(r+1))%M][j-(i-(r+1))];\n\t }\n/* for(int k = l;k <= r;k++){\n\tdp[i][j] += dp[i-k][j-(i-k)];\n\t} */\n l = 1;\n r = min(c, i);\n r = min(r, j / (b+1));\n if(l <= r){\n\t if(i-l >= 0 && j-(b+1)*l >= 0)dp[i%M][j] -= dp2[(i-l)%M][j-(b+1)*l];\n\t if(i-(r+1) >= 0 && j-(b+1)*(r+1) >= 0)dp[i%M][j] += dp2[(i-(r+1))%M][j-(b+1)*(r+1)];\n\t }\n/* for(int k = l;k <= r;k++){\n\tdp[i][j] -= dp[i-k][j-(b+1)*k];\n\t}*/\n dp[i%M][j] %= MOD;\n if(dp[i%M][j] < 0)dp[i%M][j] += MOD;\n dp1[i%M][j] = dp2[i%M][j] = dp[i%M][j];\n if(i)(dp1[i%M][j] += dp1[(i-1)%M][j+1])%=MOD;\n if(i && j-(b+1)>=0)(dp2[i%M][j] += dp2[(i-1)%M][j-(b+1)])%=MOD;\n }\n }\n cout << dp[d%M][e] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 55836, "score_of_the_acc": -0.1314, "final_rank": 2 }, { "submission_id": "aoj_1556_1251896", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\n#define MOD 1000000007\n#define M 108\ntypedef long long Int;\n\nint a, b, c, d, e;\nInt dp[108][21600];\nInt dp1[108][21600];\nInt dp2[108][21600];\nint main(){\n cin >> a >> b >> c >> d >> e;\n b -= a;\n e -= a*d;\n dp[0][0] = 1;\n dp1[0][0] = 1;\n dp2[0][0] = 1;\n for(int i = 0;i <= d;i++){\n for(int j = 0;j < 21500;j++){\n if(i == 0 && j == 0)continue;\n dp[i%M][j] = 0; \n int l = max(0, i - j);\n int r = min(c, i);\n/* if(i-l>=0 && j - (i-l) >= 0)dp[i%M][j] += dp1[(i-l)%M][j-(i-l)];\n if(i-(r+1) >= 0 && j-(i-(r+1)) >= 0)dp[i%M][j] -= dp1[(i-(r+1))%M][j-(i-(r+1))];*/\n for(int k = l;k <= r;k++){\n\tdp[i][j] += dp[i-k][j-(i-k)];\n\t}\n l = 1;\n r = min(c, i);\n r = min(r, j / (b+1));\n \n/* if(i-l >= 0 && j-(b+1)*l >= 0)dp[i%M][j] -= dp2[(i-l)%M][j-(b+1)*l];\n if(i-(r+1) >= 0 && j-(b+1)*(r+1) >= 0)dp[i%M][j] += dp2[(i-(r+1))%M][j-(b+1)*(r+1)];*/\n for(int k = l;k <= r;k++){\n\tdp[i][j] -= dp[i-k][j-(b+1)*k];\n\t}\n dp[i%M][j] %= MOD;\n if(dp[i%M][j] < 0)dp[i%M][j] += MOD;\n dp1[i%M][j] = dp2[i%M][j] = dp[i%M][j];\n if(i)(dp1[i%M][j] += dp1[(i-1)%M][j+1])%=MOD;\n if(i && j-(b+1)>=0)(dp2[i%M][j] += dp2[(i-1)%M][j-(b+1)])%=MOD;\n }\n }\n cout << dp[d%M][e] << endl;\n return 0;\n}", "accuracy": 0.25, "time_ms": 610, "memory_kb": 55768, "score_of_the_acc": -0.3231, "final_rank": 4 } ]
aoj_1560_cpp
Problem J: Yu-kun Likes a lot of Money Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいお金が大好きだ。ゆう君は、今日もお金を稼ぐために財宝の眠る島を訪れた。ゆう君は事前に財宝のありかの描かれた地図を手に入れている。その地図をもとに出来るだけ多くのお金を稼ぎたい。ゆう君は最大でどのくらいお金を手に入れることができるだろうか? Problem 地図、ゆう君の初期位置、財宝の種類とそれらから得られるお金、そして小さい岩を破壊するために必要な費用の情報が与えられる。地図の情報は h マス × w マスのフィールドとして与えられる。地図の各マスに書かれている文字とその意味は次の通りである。 '@' : ゆう君が最初にいる位置を表す。ゆう君が移動した後は道と同じように扱う。 '.' : 道を表す。このマスは自由に通ることができ費用もかからない。 '#' : 大きな岩を表す。このマスは通ることができない。 '*' : 小さな岩を表す。一定の金額を支払うことで壊すことができる。壊した後は道になる。 '0','1',...,'9','a','b',...,'z','A','B',...,'Z' : 財宝があるマスを表す。このマスを訪れることでそこに書かれている文字に対応する財宝の金額分のお金を得る。ただしお金を得ることが出来るのは最初に訪れた時のみである。 ゆう君は1回の移動で隣接する上下左右のいずれかのマスに移動することができる。 ただし、地図の外へ出るような移動はできない。 後払いをすることができるため、小さな岩を壊す際にそれに必要な金額をその時に所持している必要はない。そのため、ゆう君は最終的に小さな岩を壊す際にかかった金額の総和以上のお金を得ている必要がある。 ゆう君が得られる最大の金額を出力せよ。 Input 入力は以下の形式で与えられる。 h w n r c 1,1 c 1,2 … c 1,w c 2,1 c 2,2 … c 2,w … c h,1 c h,2 … c h,w m 1 v 1 m 2 v 2 … m n v n 1行目に地図の縦の長さ h ,横の長さ w ,地図に含まれる財宝の数 n ,小さな岩を破壊するためにかかる費用 r が空白区切りで与えられる。 続く h 行に地図を表す各マスの情報 c i,j が w 個与えられる。 ( 1 ≤ i ≤ h , 1 ≤ j ≤ w ) 続く n 行に財宝の種類 m k とその財宝の金額 v k が空白区切りで与えられる。 ( 1 ≤ k ≤ n ) Constraints 入力は以下の制約を満たす。 1 ≤ h , w ≤ 8 0 ≤ n ≤ min( h × w -1,62) ただし、min( a , b )は a , b の最小値を表す 1 ≤ v i ≤ 10 5 ( 1 ≤ i ≤ n ) 1 ≤ r ≤ 10 5 c j,k , m l を除く全ての入力は整数として与えられる ( 1 ≤ j ≤ h , 1 ≤ k ≤ w , 1 ≤ l ≤ n ) 地図にはちょうどひとつ'@'が書かれている 地図にはちょうど n 個の財宝が書かれている 地図に書かれている財宝の種類は入力で与えられた m l のいずれかである 地図に同じ種類の財宝が2つ以上現れることはない Output ゆう君が得られる最大のお金の金額を1行に出力せよ。 Sample Input1 3 3 1 10 @0. ... ... 0 100 Sample Output1 100 Sample Input2 3 3 1 10 @#b .#. .#. b 100 Sample Output2 0 Sample Input3 3 3 1 20 @*C ..* ... C 10 Sample Output3 0
[ { "submission_id": "aoj_1560_6075206", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct uftree{\n\tint par[25];\n\tint rank[25];\n\tuftree(){\n\t}\n\tvoid init(int n){\n\t\tfor(int i=0;i<n;i++){\n\t\t\tpar[i]=i;\n\t\t\trank[i]=0;\n\t\t}\n\t}\n\tint find(int x){\n\t\tif(par[x]==x)return x;\n\t\treturn par[x]=find(par[x]);\n\t}\n\n\tvoid unite(int x,int y){\n\t\tx=find(x);\n\t\ty=find(y);\n\t\tif(x==y)return;\n\t\tif(rank[x]<rank[y]){\n\t\t\tpar[x]=y;\n\t\t}else{\n\t\t\tif(rank[x]==rank[y])rank[x]++;\n\t\t\tpar[y]=x;\n\t\t}\n\t}\n\n\tbool same(int x,int y){\n\t\treturn find(x)==find(y);\n\t}\n};\n\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t}\n\t\t\tfor(int i=0;i<=c;i++){\n\t\t\t\ttmp[x]=i;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*5;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tif(id[val]==-1){\n\t\tfor(int i=0;i<w;i++){\n\t\t\tprintf(\"%d \",tmp[i]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\t//assert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\nint colo[8];\nstack<int> st;\nbool po;\nuftree uf;\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tif(tmp2[y][x]>0){\n\t\t\tuf.unite(tmp2[y][x],sz);\n\t\t}\n\t\tst.push(tmp2[y][x]);\n\t}else{\n\t\tpo=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%5>=1)tmp2[0][x]=v%5;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=5;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tuf.init(6);\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\twhile(st.size())st.pop();\n\t\t\tpo=false;\n\t\t\tdfs_row(i,0,tmp2[0][i]);\n\t\t\tif(po){\n\t\t\t\twhile(st.size()){\n\t\t\t\t\tint v=st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\tfl2[v]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cn=0;\n\tint cn2=0;\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t\tif(fl2[i])cn2++;\n\t\t}\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\tif(cn2>0){\n\t\tint prev=-2;\n\t\tint kero=-100;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\tif(prev+1==i){\n\t\t\t\t\ttmp3[1][i]=kero;\n\t\t\t\t}else{\n\t\t\t\t\ttmp3[1][i]=++kero;\n\t\t\t\t}\n\t\t\t\tprev=i;\n\t\t\t}\n\t\t}\n\t}else{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]>0){\n\t\t\t\ttmp3[1][i]=uf.find(tmp3[1][i]);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tif(tmp[i]!=0)vt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(-105);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t\tmap<int,int> mp;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\tif(mp.find(tmp[i])==mp.end()){\n\t\t\t\tmp[tmp[i]]=mp.size();\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=mp[tmp[i]];\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\tif(dp[r][index_p][flag_p]+cost_i-cost_r>=0){\n\t\t/*\n\t\tprintf(\"r=%d %d %d-> index=%d flag=%d cost=%d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n\t\tfor(int i=0;i<2;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tprintf(\"%d \",tmp2[i][j]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\t\n\t\t}\n\t\tfor(int j=0;j<w;j++){\n\t\t\tprintf(\"%d \",tmp[j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t\t*/\n\t}\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%5>=2)return false;\n\t\tv/=5;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int j=0;j<h;j++){\n\t\tfor(int i=0;i<(1<<w);i++){\n\t\t\tcalc_row0(j,i);\n\t\t}\n\t}\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=1;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tif(dp[i][j][k]==-INF)continue;\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int j=1;j<=h;j++){\n\t\tfor(int i=0;i<vec.size();i++){\n\t\t\tif(is_ok(vec[i])){\n\t\t\t\tans=max(ans,dp[j][i][1]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 910, "memory_kb": 134596, "score_of_the_acc": -1.4688, "final_rank": 1 }, { "submission_id": "aoj_1560_3606589", "code_snippet": "#include <iostream>\n#include <vector>\n#include <utility>\n#include <queue>\n#include <algorithm>\n#include <unordered_map>\n#include <unordered_set>\n\nint main() {\n std::priority_queue<std::tuple<int, int, std::vector<int>>> queue;\n\n int h, w, n, r;\n char c[64];\n int st;\n std::unordered_map<char, int> m;\n std::cin >> h >> w >> n >> r;\n for (int y = 0; y < h; ++y) {\n for (int x = 0; x < w; ++x) {\n std::cin >> c[x + w * y];\n if (c[x + w * y] == '@') {\n st = x + w * y;\n }\n }\n }\n std::vector<int> cost(h * w, -r * h * w - 1);\n for (int i = 0; i < n; ++i) {\n char key;\n int gold;\n std::cin >> key >> gold;\n m.emplace(key, gold);\n }\n queue.emplace(0, st, std::vector<int>(h * w, 0));\n int max_score = 0;\n while (!queue.empty()) {\n auto item = queue.top();\n queue.pop();\n auto score = std::get<0>(item);\n auto pos = std::get<1>(item);\n auto stayed = std::get<2>(item);\n int x = pos % w;\n int y = pos / w;\n for (int j : {0, 1}) {\n for (int k : {-1, 1}) {\n int dx, dy;\n if (j == 0) {\n dx = 0;\n dy = k;\n } else {\n dx = k;\n dy = 0;\n }\n int new_x = x + dx;\n int new_y = y + dy;\n if (new_x >= 0 && new_x < w && new_y >= 0 && new_y < h) {\n auto stayed_copied = stayed;\n int new_pos = new_x + new_y * w;\n if (stayed_copied[new_pos] >= 2) {\n continue;\n }\n char field = c[new_pos];\n if (field == '#') {\n continue;\n }\n int new_score = score;\n if (stayed_copied[new_pos] == 0) {\n if (field == '*') {\n new_score -= r;\n } else if (field != '.' && field != '@') {\n new_score += m[field];\n }\n }\n if (cost[new_pos] <= new_score) {\n stayed_copied[new_pos] += 1;\n cost[new_pos] = new_score;\n queue.emplace(new_score, new_pos, stayed_copied);\n if (max_score < new_score) {\n max_score = new_score;\n }\n }\n }\n }\n }\n }\n std::cout << max_score << std::endl;\n return 0;\n}", "accuracy": 0.3076923076923077, "time_ms": 10, "memory_kb": 3284, "score_of_the_acc": 0, "final_rank": 5 }, { "submission_id": "aoj_1560_3565279", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct uftree{\n\tint par[25];\n\tint rank[25];\n\tuftree(){\n\t}\n\tvoid init(int n){\n\t\tfor(int i=0;i<n;i++){\n\t\t\tpar[i]=i;\n\t\t\trank[i]=0;\n\t\t}\n\t}\n\tint find(int x){\n\t\tif(par[x]==x)return x;\n\t\treturn par[x]=find(par[x]);\n\t}\n\n\tvoid unite(int x,int y){\n\t\tx=find(x);\n\t\ty=find(y);\n\t\tif(x==y)return;\n\t\tif(rank[x]<rank[y]){\n\t\t\tpar[x]=y;\n\t\t}else{\n\t\t\tif(rank[x]==rank[y])rank[x]++;\n\t\t\tpar[y]=x;\n\t\t}\n\t}\n\n\tbool same(int x,int y){\n\t\treturn find(x)==find(y);\n\t}\n};\n\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t}\n\t\t\tfor(int i=0;i<=c;i++){\n\t\t\t\ttmp[x]=i;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*5;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tif(id[val]==-1){\n\t\tfor(int i=0;i<w;i++){\n\t\t\tprintf(\"%d \",tmp[i]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\t//assert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\nint colo[8];\nstack<int> st;\nbool po;\nuftree uf;\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tif(tmp2[y][x]>0){\n\t\t\tuf.unite(tmp2[y][x],sz);\n\t\t}\n\t\tst.push(tmp2[y][x]);\n\t}else{\n\t\tpo=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%5>=1)tmp2[0][x]=v%5;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=5;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tuf.init(6);\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\twhile(st.size())st.pop();\n\t\t\tpo=false;\n\t\t\tdfs_row(i,0,tmp2[0][i]);\n\t\t\tif(po){\n\t\t\t\twhile(st.size()){\n\t\t\t\t\tint v=st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\tfl2[v]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cn=0;\n\tint cn2=0;\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t\tif(fl2[i])cn2++;\n\t\t}\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\tif(cn2>0){\n\t\tint prev=-2;\n\t\tint kero=-100;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\tif(prev+1==i){\n\t\t\t\t\ttmp3[1][i]=kero;\n\t\t\t\t}else{\n\t\t\t\t\ttmp3[1][i]=++kero;\n\t\t\t\t}\n\t\t\t\tprev=i;\n\t\t\t}\n\t\t}\n\t}else{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]>0){\n\t\t\t\ttmp3[1][i]=uf.find(tmp3[1][i]);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tif(tmp[i]!=0)vt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(-105);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t\tmap<int,int> mp;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\tif(mp.find(tmp[i])==mp.end()){\n\t\t\t\tmp[tmp[i]]=mp.size();\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=mp[tmp[i]];\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\tif(dp[r][index_p][flag_p]+cost_i-cost_r>=0){\n\t\t/*\n\t\tprintf(\"r=%d %d %d-> index=%d flag=%d cost=%d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n\t\tfor(int i=0;i<2;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tprintf(\"%d \",tmp2[i][j]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\t\n\t\t}\n\t\tfor(int j=0;j<w;j++){\n\t\t\tprintf(\"%d \",tmp[j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t\t*/\n\t}\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%5>=2)return false;\n\t\tv/=5;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int j=0;j<h;j++){\n\t\tfor(int i=0;i<(1<<w);i++){\n\t\t\tcalc_row0(j,i);\n\t\t}\n\t}\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=1;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tif(dp[i][j][k]==-INF)continue;\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int j=1;j<=h;j++){\n\t\tfor(int i=0;i<vec.size();i++){\n\t\t\tif(is_ok(vec[i])){\n\t\t\t\tans=max(ans,dp[j][i][1]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 920, "memory_kb": 134592, "score_of_the_acc": -1.4739, "final_rank": 2 }, { "submission_id": "aoj_1560_3565277", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct uftree{\n\tint par[25];\n\tint rank[25];\n\tuftree(){\n\t}\n\tvoid init(int n){\n\t\tfor(int i=0;i<n;i++){\n\t\t\tpar[i]=i;\n\t\t\trank[i]=0;\n\t\t}\n\t}\n\tint find(int x){\n\t\tif(par[x]==x)return x;\n\t\treturn par[x]=find(par[x]);\n\t}\n\n\tvoid unite(int x,int y){\n\t\tx=find(x);\n\t\ty=find(y);\n\t\tif(x==y)return;\n\t\tif(rank[x]<rank[y]){\n\t\t\tpar[x]=y;\n\t\t}else{\n\t\t\tif(rank[x]==rank[y])rank[x]++;\n\t\t\tpar[y]=x;\n\t\t}\n\t}\n\n\tbool same(int x,int y){\n\t\treturn find(x)==find(y);\n\t}\n};\n\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t}\n\t\t\tfor(int i=0;i<=c;i++){\n\t\t\t\ttmp[x]=i;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*4;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tif(id[val]==-1){\n\t\tfor(int i=0;i<w;i++){\n\t\t\tprintf(\"%d \",tmp[i]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\t//assert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\nint colo[8];\nstack<int> st;\nbool po;\nuftree uf;\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tif(tmp2[y][x]>0){\n\t\t\tuf.unite(tmp2[y][x],sz);\n\t\t}\n\t\tst.push(tmp2[y][x]);\n\t}else{\n\t\tpo=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%4>=1)tmp2[0][x]=v%4;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=4;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tuf.init(6);\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\twhile(st.size())st.pop();\n\t\t\tpo=false;\n\t\t\tdfs_row(i,0,tmp2[0][i]);\n\t\t\tif(po){\n\t\t\t\twhile(st.size()){\n\t\t\t\t\tint v=st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\tfl2[v]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cn=0;\n\tint cn2=0;\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t\tif(fl2[i])cn2++;\n\t\t}\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\tif(cn2>0){\n\t\tint prev=-2;\n\t\tint kero=-100;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\tif(prev+1==i){\n\t\t\t\t\ttmp3[1][i]=kero;\n\t\t\t\t}else{\n\t\t\t\t\ttmp3[1][i]=++kero;\n\t\t\t\t}\n\t\t\t\tprev=i;\n\t\t\t}\n\t\t}\n\t}else{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]>0){\n\t\t\t\ttmp3[1][i]=uf.find(tmp3[1][i]);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tif(tmp[i]!=0)vt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(-105);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t\tmap<int,int> mp;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\tif(mp.find(tmp[i])==mp.end()){\n\t\t\t\tmp[tmp[i]]=mp.size();\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=mp[tmp[i]];\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\tif(dp[r][index_p][flag_p]+cost_i-cost_r>=0){\n\t\t/*\n\t\tprintf(\"r=%d %d %d-> index=%d flag=%d cost=%d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n\t\tfor(int i=0;i<2;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tprintf(\"%d \",tmp2[i][j]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\t\n\t\t}\n\t\tfor(int j=0;j<w;j++){\n\t\t\tprintf(\"%d \",tmp[j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t\t*/\n\t}\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%4>=2)return false;\n\t\tv/=4;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int j=0;j<h;j++){\n\t\tfor(int i=0;i<(1<<w);i++){\n\t\t\tcalc_row0(j,i);\n\t\t}\n\t}\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=1;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tif(dp[i][j][k]==-INF)continue;\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int j=1;j<=h;j++){\n\t\tfor(int i=0;i<vec.size();i++){\n\t\t\tif(is_ok(vec[i])){\n\t\t\t\tans=max(ans,dp[j][i][1]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.6153846153846154, "time_ms": 910, "memory_kb": 134576, "score_of_the_acc": -1.4686, "final_rank": 3 }, { "submission_id": "aoj_1560_3565194", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct uftree{\n\tint par[25];\n\tint rank[25];\n\tuftree(){\n\t}\n\tvoid init(int n){\n\t\tfor(int i=0;i<n;i++){\n\t\t\tpar[i]=i;\n\t\t\trank[i]=0;\n\t\t}\n\t}\n\tint find(int x){\n\t\tif(par[x]==x)return x;\n\t\treturn par[x]=find(par[x]);\n\t}\n\n\tvoid unite(int x,int y){\n\t\tx=find(x);\n\t\ty=find(y);\n\t\tif(x==y)return;\n\t\tif(rank[x]<rank[y]){\n\t\t\tpar[x]=y;\n\t\t}else{\n\t\t\tif(rank[x]==rank[y])rank[x]++;\n\t\t\tpar[y]=x;\n\t\t}\n\t}\n\n\tbool same(int x,int y){\n\t\treturn find(x)==find(y);\n\t}\n};\n\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}else{\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*4;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tif(id[val]==-1){\n\t\tfor(int i=0;i<w;i++){\n\t\t\tprintf(\"%d \",tmp[i]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\tassert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\nint colo[8];\nstack<int> st;\nbool po;\nuftree uf;\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tif(tmp2[y][x]>0){\n\t\t\tuf.unite(tmp2[y][x],sz);\n\t\t}\n\t\tst.push(tmp2[y][x]);\n\t}else{\n\t\tpo=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%4>=1)tmp2[0][x]=v%4;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=4;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tuf.init(6);\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\twhile(st.size())st.pop();\n\t\t\tpo=false;\n\t\t\tdfs_row(i,0,tmp2[0][i]);\n\t\t\tif(po){\n\t\t\t\twhile(st.size()){\n\t\t\t\t\tint v=st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\tfl2[v]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cn=0;\n\tint cn2=0;\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t\tif(fl2[i])cn2++;\n\t\t}\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\tif(cn2>0){\n\t\tint prev=-2;\n\t\tint kero=-100;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\tif(prev+1==i){\n\t\t\t\t\ttmp3[1][i]=kero;\n\t\t\t\t}else{\n\t\t\t\t\ttmp3[1][i]=++kero;\n\t\t\t\t}\n\t\t\t\tprev=i;\n\t\t\t}\n\t\t}\n\t}else{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]>0){\n\t\t\t\ttmp3[1][i]=uf.find(tmp3[1][i]);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tif(tmp[i]!=0)vt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(-105);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t\tmap<int,int> mp;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\tif(mp.find(tmp[i])==mp.end()){\n\t\t\t\tmp[tmp[i]]=mp.size();\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=mp[tmp[i]];\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\tif(dp[r][index_p][flag_p]+cost_i-cost_r>=0){\n\t\t/*\n\t\tprintf(\"r=%d %d %d-> index=%d flag=%d cost=%d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n\t\tfor(int i=0;i<2;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tprintf(\"%d \",tmp2[i][j]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\t\n\t\t}\n\t\tfor(int j=0;j<w;j++){\n\t\t\tprintf(\"%d \",tmp[j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t\t*/\n\t}\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%4>=2)return false;\n\t\tv/=4;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int j=0;j<h;j++){\n\t\tfor(int i=0;i<(1<<w);i++){\n\t\t\tcalc_row0(j,i);\n\t\t}\n\t}\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=1;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int j=1;j<=h;j++){\n\t\tfor(int i=0;i<vec.size();i++){\n\t\t\tif(is_ok(vec[i])){\n\t\t\t\tans=max(ans,dp[j][i][1]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.1282051282051282, "time_ms": 30, "memory_kb": 134420, "score_of_the_acc": -1.0091, "final_rank": 10 }, { "submission_id": "aoj_1560_3561683", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}else{\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*4;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tif(id[val]==-1){\n\t\tfor(int i=0;i<w;i++){\n\t\t\tprintf(\"%d \",tmp[i]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\tassert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\nstack<int> st;\nbool po;\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tst.push(tmp2[y][x]);\n\t}else{\n\t\tpo=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%4>=1)tmp2[0][x]=v%4;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=4;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\twhile(st.size())st.pop();\n\t\t\tpo=false;\n\t\t\tdfs_row(i,0,sz+1);\n\t\t\tif(po){\n\t\t\t\twhile(st.size()){\n\t\t\t\t\tint v=st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\tfl2[v]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsz++;\n\t\t}\n\t}\n\tint cn=0;\n\tint cn2=0;\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t\tif(fl2[i])cn2++;\n\t\t}\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\tif(cn2>0){\n\t\tint prev=-2;\n\t\tint kero=-100;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\tif(prev+1==i){\n\t\t\t\t\ttmp3[1][i]=kero;\n\t\t\t\t}else{\n\t\t\t\t\ttmp3[1][i]=++kero;\n\t\t\t\t}\n\t\t\t\tprev=i;\n\t\t\t}\n\t\t}\n\t}else{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tif(tmp[i]!=0)vt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(-105);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t\tmap<int,int> mp;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\tif(mp.find(tmp[i])==mp.end()){\n\t\t\t\tmp[tmp[i]]=mp.size();\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=mp[tmp[i]];\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\t/*\n\tif(dp[r][index_p][flag_p]+cost_i-cost_r>=0){\n\t\tprintf(\"r=%d %d %d-> index=%d flag=%d cost=%d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n\t\tfor(int i=0;i<2;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tprintf(\"%d \",tmp2[i][j]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\t\n\t\t}\n\t\tfor(int j=0;j<w;j++){\n\t\t\tprintf(\"%d \",tmp[j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\t*/\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%4>=2)return false;\n\t\tv/=4;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int j=0;j<h;j++){\n\t\tfor(int i=0;i<(1<<w);i++){\n\t\t\tcalc_row0(j,i);\n\t\t}\n\t}\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=1;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int j=1;j<=h;j++){\n\t\tfor(int i=0;i<vec.size();i++){\n\t\t\tif(is_ok(vec[i])){\n\t\t\t\tans=max(ans,dp[j][i][1]);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.4358974358974359, "time_ms": 1930, "memory_kb": 134528, "score_of_the_acc": -1.9995, "final_rank": 4 }, { "submission_id": "aoj_1560_3561682", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}else{\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*4;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tif(id[val]==-1){\n\t\tfor(int i=0;i<w;i++){\n\t\t\tprintf(\"%d \",tmp[i]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\tassert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\nstack<int> st;\nbool po;\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tst.push(tmp2[y][x]);\n\t}else{\n\t\tpo=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%4>=1)tmp2[0][x]=v%4;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=4;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\twhile(st.size())st.pop();\n\t\t\tpo=false;\n\t\t\tdfs_row(i,0,sz+1);\n\t\t\tif(po){\n\t\t\t\twhile(st.size()){\n\t\t\t\t\tint v=st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\tfl2[v]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsz++;\n\t\t}\n\t}\n\tint cn=0;\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t}\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\tif(cn>0){\n\t\tint prev=-2;\n\t\tint kero=-100;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp3[1][i]==0 && tmp2[1][i]==1){\n\t\t\t\tif(prev+1==i){\n\t\t\t\t\ttmp3[1][i]=kero;\n\t\t\t\t}else{\n\t\t\t\t\ttmp3[1][i]=++kero;\n\t\t\t\t}\n\t\t\t\tprev=i;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tif(tmp[i]!=0)vt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(-105);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t\tmap<int,int> mp;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\tif(mp.find(tmp[i])==mp.end()){\n\t\t\t\tmp[tmp[i]]=mp.size();\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp[i]==0)continue;\n\t\t\ttmp[i]=mp[tmp[i]];\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\t/*\n\tif(dp[r][index_p][flag_p]+cost_i-cost_r>=0){\n\t\tprintf(\"r=%d %d %d-> index=%d flag=%d cost=%d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n\t\tfor(int i=0;i<2;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tprintf(\"%d \",tmp2[i][j]);\n\t\t\t}\n\t\t\tprintf(\"\\n\");\t\n\t\t}\n\t\tfor(int j=0;j<w;j++){\n\t\t\tprintf(\"%d \",tmp[j]);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\t*/\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%4>=2)return false;\n\t\tv/=4;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int j=0;j<h;j++){\n\t\tfor(int i=0;i<(1<<w);i++){\n\t\t\tcalc_row0(j,i);\n\t\t}\n\t}\n\t\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int i=0;i<vec.size();i++){\n\t\tif(is_ok(vec[i])){\n\t\t\tans=max(ans,dp[h][i][1]);\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.1282051282051282, "time_ms": 30, "memory_kb": 134392, "score_of_the_acc": -1.0089, "final_rank": 9 }, { "submission_id": "aoj_1560_3561035", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}else{\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*4;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tassert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\nstack<int> st;\nbool po;\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tst.push(tmp2[y][x]);\n\t}else{\n\t\tpo=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%4>=1)tmp2[0][x]=v%4;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=4;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\twhile(st.size())st.pop();\n\t\t\tpo=false;\n\t\t\tdfs_row(i,0,sz+1);\n\t\t\tif(po){\n\t\t\t\twhile(st.size()){\n\t\t\t\t\tint v=st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\tfl2[v]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsz++;\n\t\t}\n\t}\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tint cn=0;\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t\t//printf(\"%c \",fl1[i]?'y':'n');\n\t\t}\n\t\t/*\n\t\tprintf(\"\\n\");\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tprintf(\"%c \",fl2[i]?'y':'n');\n\t\t}\n\t\tprintf(\"\\n\");\n\t\t*/\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tvt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(0);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\t/*\n\tprintf(\"r=%d %d %d-> %d %d %d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n\tfor(int i=0;i<2;i++){\n\t\tfor(int j=0;j<w;j++){\n\t\t\tprintf(\"%d \",tmp2[i][j]);\n\t\t}\n\t\tprintf(\"\\n\");\t\n\t}\n\t*/\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%4>=2)return false;\n\t\tv/=4;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int j=0;j<h;j++){\n\t\tfor(int i=0;i<(1<<w);i++){\n\t\t\tcalc_row0(j,i);\n\t\t}\n\t}\n\t\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int i=0;i<vec.size();i++){\n\t\tif(is_ok(vec[i])){\n\t\t\tans=max(ans,dp[h][i][1]);\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.3076923076923077, "time_ms": 20, "memory_kb": 134444, "score_of_the_acc": -1.0041, "final_rank": 6 }, { "submission_id": "aoj_1560_3561023", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}else{\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*4;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tassert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\nstack<int> st;\nbool po;\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tst.push(tmp2[y][x]);\n\t}else{\n\t\tpo=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%4>=1)tmp2[0][x]=v%4;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=4;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\twhile(st.size())st.pop();\n\t\t\tpo=false;\n\t\t\tdfs_row(i,0,sz+1);\n\t\t\tif(po){\n\t\t\t\twhile(st.size()){\n\t\t\t\t\tint v=st.top();\n\t\t\t\t\tst.pop();\n\t\t\t\t\tfl2[v]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tsz++;\n\t\t}\n\t}\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tint cn=0;\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t\t//printf(\"%c \",fl1[i]?'y':'n');\n\t\t}\n\t\t/*\n\t\tprintf(\"\\n\");\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tprintf(\"%c \",fl2[i]?'y':'n');\n\t\t}\n\t\tprintf(\"\\n\");\n\t\t*/\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tvt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(0);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\t/*\n\tprintf(\"r=%d %d %d-> %d %d %d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n\tfor(int i=0;i<2;i++){\n\t\tfor(int j=0;j<w;j++){\n\t\t\tprintf(\"%d \",tmp2[i][j]);\n\t\t}\n\t\tprintf(\"\\n\");\t\n\t}\n\t*/\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%4>=2)return false;\n\t\tv/=4;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<(1<<w);i++){\n\t\tcalc_row0(0,i);\n\t}\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int i=0;i<vec.size();i++){\n\t\tif(is_ok(vec[i])){\n\t\t\tans=max(ans,dp[h][i][1]);\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.1794871794871795, "time_ms": 30, "memory_kb": 134468, "score_of_the_acc": -1.0094, "final_rank": 8 }, { "submission_id": "aoj_1560_3560995", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}else{\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*4;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tassert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[1][index][flag]=cost_i-cost_r;\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nbool fl1[5];\nbool fl2[5];\nint tmp2[2][8];\nint tmp3[2][8];\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tif(y==0){\n\t\tfl2[tmp2[y][x]]=true;\n\t}\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%4>=1)tmp2[0][x]=v%4;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=4;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\t\n\tmemset(fl1,false,sizeof(fl1));\n\tmemset(fl2,false,sizeof(fl2));\n\tmemset(tmp3,0,sizeof(tmp3));\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\tdfs_row(i,0,sz+1);\n\t\t\tsz++;\n\t\t}\n\t}\n\t{\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t}\n\t\t}\n\t\tint cn=0;\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t}\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tvt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(0);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%4>=2)return false;\n\t\tv/=4;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<(1<<w);i++){\n\t\tcalc_row0(0,i);\n\t}\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int i=0;i<vec.size();i++){\n\t\tif(is_ok(vec[i])){\n\t\t\tans=max(ans,dp[h][i][1]);\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.02564102564102564, "time_ms": 30, "memory_kb": 134436, "score_of_the_acc": -1.0092, "final_rank": 11 }, { "submission_id": "aoj_1560_3560986", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint h,w,n,R;\nvector<int> vec;\n\nint tmp[8];\nint ki[8];\nint id[1<<25];\n\nvoid dfs(int x,int c){\n\tif(x==w){\n\t\tint val=0;\n\t\tfor(int i=0;i<w;i++){\n\t\t\tval+=tmp[i]*ki[i];\n\t\t}\n\t\tvec.push_back(val);\n\t}else{\n\t\tif(x==0){\n\t\t\ttmp[0]=0;\n\t\t\tdfs(x+1,0);\n\t\t\ttmp[0]=1;\n\t\t\tdfs(x+1,1);\n\t\t}else{\n\t\t\tif(tmp[x-1]==0){\n\t\t\t\ttmp[x]=c+1;\n\t\t\t\tdfs(x+1,c+1);\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}else{\n\t\t\t\ttmp[x]=c;\n\t\t\t\tdfs(x+1,c);\n\t\t\t\ttmp[x]=0;\n\t\t\t\tdfs(x+1,c);\n\t\t\t}\n\t\t}\n\t}\n}\n\nint dp[10][100000][2];\n\nvoid init(){\n\tki[0]=1;\n\tfor(int i=1;i<w;i++){\n\t\tki[i]=ki[i-1]*4;\n\t}\n\tdfs(0,0);\n\tsort(vec.begin(),vec.end());\n\tmemset(id,-1,sizeof(id));\n\tfor(int i=0;i<vec.size();i++){\n\t\tid[vec[i]]=i;\n\t}\n}\n\nint fie[8][8];\nint val[64];\nint sx,sy;\n\nint is_person(int r){\n\tif(sy!=r)return 0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && sx==i)return 1;\n\t}\n\treturn 0;\n}\n\nint calc_rock(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]==-1)return -1;\n\t\tif(tmp[i]>=1 && fie[r][i]==-2)sum+=R;\n\t}\n\treturn sum;\n}\n\nint calc_item(int r){\n\tint sum=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp[i]>=1 && fie[r][i]>=1){\n\t\t\tsum+=val[fie[r][i]];\n\t\t}\n\t}\n\treturn sum;\n}\n\nint calc_id(){\n\tint val=0;\n\tfor(int i=0;i<w;i++){\n\t\tval+=tmp[i]*ki[i];\n\t}\n\tassert(id[val]!=-1);\n\treturn id[val];\n}\n\nvoid calc_row0(int r,int bit){\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\tif(i==0){\n\t\t\t\tsz++;\n\t\t\t\ttmp[i]=sz;\n\t\t\t}else{\n\t\t\t\tif(tmp[i-1]!=0){\n\t\t\t\t\ttmp[i]=tmp[i-1];\n\t\t\t\t}else{\n\t\t\t\t\tsz++;\n\t\t\t\t\ttmp[i]=sz;\n\t\t\t\t}\n\t\t\t}\n\t\t}else{\n\t\t\ttmp[i]=0;\n\t\t}\n\t}\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r);\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\tdp[1][index][flag]=cost_i-cost_r;\n\t/*\n\tfor(int i=0;i<w;i++){\n\t\tprintf(\"%d \",tmp[i]);\n\t}\n\tprintf(\"\\n\");\n\tprintf(\"%d %d %d %d\\n\",1,index,flag,cost_i-cost_r);\n\t*/\n}\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\n\nint tmp2[2][8];\nint tmp3[2][8];\n\nvoid dfs_row(int x,int y,int sz){\n\ttmp3[y][x]=sz;\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i];\n\t\tint ny=y+dy[i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<2){\n\t\t\tif(tmp2[ny][nx]!=0 && tmp3[ny][nx]==0){\n\t\t\t\tdfs_row(nx,ny,sz);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid calc(int r,int index_p,int flag_p,int bit){\n\tmemset(tmp2,0,sizeof(tmp2));\n\t{\n\t\tint v=vec[index_p];\n\t\tint x=0;\n\t\twhile(v>0){\n\t\t\tif(v%4>=1)tmp2[0][x]=v%4;\n\t\t\telse{\n\t\t\t\ttmp2[0][x]=0;\n\t\t\t}\n\t\t\tv/=4;\n\t\t\tx++;\n\t\t}\n\t}\n\tfor(int i=0;i<w;i++){\n\t\tif((bit>>i)&1){\n\t\t\ttmp2[1][i]=1;\n\t\t}\n\t}\n\t{\n\t\tbool fl1[5];\n\t\tbool fl2[5];\n\t\tmemset(fl1,false,sizeof(fl1));\n\t\tmemset(fl2,false,sizeof(fl2));\n\t\tfor(int i=0;i<w;i++){\n\t\t\tif(tmp2[0][i]>0){\n\t\t\t\tfl1[tmp2[0][i]]=true;\n\t\t\t\tif(tmp2[1][i]>0){\n\t\t\t\t\tfl2[tmp2[0][i]]=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint cn=0;\n\t\tfor(int i=1;i<=4;i++){\n\t\t\tif(fl1[i])cn++;\n\t\t}\n\t\tif(cn>=2){\n\t\t\tfor(int i=1;i<=4;i++){\n\t\t\t\tif(fl1[i] && !fl2[i])return;\n\t\t\t}\n\t\t}\n\t}\n\tmemset(tmp3,0,sizeof(tmp3));\n\tint sz=0;\n\tfor(int i=0;i<w;i++){\n\t\tif(tmp2[0][i]!=0 && tmp3[0][i]==0){\n\t\t\tdfs_row(i,0,sz+1);\n\t\t\tsz++;\n\t\t}\n\t}\n\t/*\n\tfor(int i=0;i<w;i++){\n\t\tprintf(\"%d \",tmp3[1][i]);\n\t}\n\tprintf(\"\\n\");\n\t*/\n\t{\n\t\tvector<int> vt;\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=tmp3[1][i];\n\t\t\tvt.push_back(tmp[i]);\n\t\t}\n\t\tvt.push_back(0);\n\t\tsort(vt.begin(),vt.end());\n\t\tvt.erase(unique(vt.begin(),vt.end()),vt.end());\n\t\tfor(int i=0;i<w;i++){\n\t\t\ttmp[i]=lower_bound(vt.begin(),vt.end(),tmp[i])-vt.begin();\n\t\t}\n\t}\n\t\n\tint cost_r=calc_rock(r);\n\tif(cost_r==-1)return;\n\tint flag=is_person(r)|flag_p;\n\tint cost_i=calc_item(r);\n\tint index=calc_id();\n\t/*\n\tfor(int i=0;i<w;i++){\n\t\tprintf(\"%d \",tmp[i]);\n\t}\n\tprintf(\"\\n\");\n\t*/\n\tdp[r+1][index][flag]=max(dp[r+1][index][flag],dp[r][index_p][flag_p]+cost_i-cost_r);\n\t//printf(\"%d %d %d-> %d %d %d\\n\",r,index_p,flag_p,index,flag,dp[r][index_p][flag_p]+cost_i-cost_r);\n}\n\nbool is_ok(int v){\n\twhile(v>0){\n\t\tif(v%4>=2)return false;\n\t\tv/=4;\n\t}\n\treturn true;\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&h,&w,&n,&R);\n\tinit();\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(s[j]>='0' && s[j]<='9'){\n\t\t\t\tfie[i][j]=(s[j]-'0')+1;\n\t\t\t}\n\t\t\tif(s[j]>='a' && s[j]<='z'){\n\t\t\t\tfie[i][j]=(s[j]-'a')+11;\n\t\t\t}\n\t\t\tif(s[j]>='A' && s[j]<='Z'){\n\t\t\t\tfie[i][j]=(s[j]-'A')+37;\n\t\t\t}\n\t\t\tif(s[j]=='@'){\n\t\t\t\tsx=j;\n\t\t\t\tsy=i;\n\t\t\t}\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=-1;\n\t\t\t}\n\t\t\tif(s[j]=='*'){\n\t\t\t\tfie[i][j]=-2;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tchar c;\n\t\tint a;\n\t\tscanf(\" %c%d\",&c,&a);\n\t\tif(c>='0' && c<='9'){\n\t\t\tval[(c-'0')+1]=a;\n\t\t}\n\t\tif(c>='a' && c<='z'){\n\t\t\tval[(c-'a')+11]=a;\n\t\t}\n\t\tif(c>='A' && c<='Z'){\n\t\t\tval[(c-'A')+37]=a;\n\t\t}\n\t}\n\tfor(int i=0;i<=h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tdp[i][j][k]=-INF;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<(1<<w);i++){\n\t\tcalc_row0(0,i);\n\t}\n\tfor(int i=1;i<h;i++){\n\t\tfor(int j=0;j<vec.size();j++){\n\t\t\tfor(int k=0;k<2;k++){\n\t\t\t\tfor(int l=0;l<(1<<w);l++){\n\t\t\t\t\tcalc(i,j,k,l);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int i=0;i<vec.size();i++){\n\t\tif(is_ok(vec[i])){\n\t\t\tans=max(ans,dp[h][i][1]);\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.1794871794871795, "time_ms": 30, "memory_kb": 134456, "score_of_the_acc": -1.0094, "final_rank": 7 } ]
aoj_1558_cpp
Problem H: Puzzle and Hexagons Backgorund 超大人気ゲーム「パズル&ヘキサゴンズ」がついにリリースされた。このゲームは超面白すぎてドハマりする人が続出。あまりの熱中度に医師から中毒認定を受ける者も続出した。世界各国の有志達はこのゲームの中毒者達を助けるために「パズル&ヘキサゴンズ」のシミュレータを作り、危険な実機でのプレイを避けるよう促そうとした。あなたにはシミュレータ作りに協力して欲しい。 Problem 正六角形のマスを縦に H 個、横に W 個敷き詰めた盤面が与えられる。 Fig.1は H =4, W =7の時の盤面とそれに対応するマスの座標( x , y )を示す。 Fig.1 初期状態で各マスには色のついたブロックが存在する。 ブロックの色は以下のようにアルファベット一文字で表現される。 'R' ・・・赤 'G' ・・・緑 'B' ・・・青 'P' ・・・紫 'Y' ・・・黄 'E' ・・・水 次に操作の数 Q が与えられる。 各操作では回転の中心座標( x , y )が与えられ、そのマスの周囲にある6つのブロックを時計回りに一つ回転させることを示す。(Fig.2 参照)。 このとき、ブロックが存在しないマスも空のブロックが存在すると考えて時計回りに一つ回転させる。 ただし、指定された座標とその周辺の6つのマスの内いずれか一つでも H × W の盤面の中に存在していない場合は回転を行わない。 Fig.2 次に以下の処理ができなくなるまで繰り返す。 Fig.3において、ブロックAの位置からB, C, Dの位置のいずれのマスにもブロックが存在しないとき、ブロックAはCの位置に落下する。マスB, Dが存在しない場合はブロックも存在しないと考え、マスCが存在しない場合は落下の処理を行わない。 1の処理が可能なブロックが存在する場合は1に戻る。 同じ色のブロックが3つ以上繋がっている場合、そのブロックは全て消滅する。 2つのブロックが繋がるとはマスの一辺を共有することである。 注意:この一連の処理は、操作が一つも与えられていない状態(初期状態)でも行われる。 Fig.3 全ての操作を実行した後の最終的な盤面を出力せよ。 Input 入力は以下の形式で与えられる。 H W F 0, H−1 F 1, H−1 … F W−1, H−1 F 0, H−2 F 1, H−2 … F W−1, H−2 . . . F 0, 0 F 1, 0 … F W−1, 0 Q x 0 y 0 x 1 y 1 . . . x Q−1 y Q−1 1行目に、盤面の縦と横のサイズを表す2つの整数 H と W が与えられる。 2行目から H +1行目に、各添字に対応する盤面の色を表す文字列が与えられる。 H +2行目に、操作の数 Q が与えられる。 続く Q 行に回転の中心のマスの座標を表す x と y が与えられる。 Constraints 3 ≤ H ≤ 50 3 ≤ W ≤ 50 0 ≤ x < W 0 ≤ y < H 1 ≤ Q ≤ 100 F i, j ( 0 ≤ i < W , 0 ≤ j < H ) は'R','G','B','P','Y','E'のいずれかである。 Output 全ての操作を行った後の盤面を H 行で出力せよ。 ただし、ブロックが無いマスは'.'で表すこと。 Sample Input1 3 3 RGR RBP YEB 1 1 1 Sample Output1 … YBG EBP Fig.4はSample Input1における状態の遷移を表したものである。 Fig.4 Sample Input2 4 5 BYYGG RRRRR RRBRR YYGGB 2 3 1 3 1 Sample Output2 ..... ..... ..... B.BGB Sample Input3 4 4 BEEP ERYY BBRP RBYP 1 1 2 Sample Output3 .... .... .... .B.. 盤面の初期状態ですでに消えるブロックがあること注意。 両端にあるブロックの落下処理に注意。
[ { "submission_id": "aoj_1558_9166437", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 998244353\n//#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 55\n\nenum Type{\n\tGU,\n\tKI,\n};\n\nint H,W;\nint d_row[2][6] = {{-1,-1,0,1,0,-1},{-1,0,1,1,1,0}},d_col[2][6] = {{0,1,1,0,-1,-1},{0,1,1,0,-1,-1}};\n\nchar buf[SIZE][SIZE];\nbool check[SIZE][SIZE];\n\nbool rangeCheck(int row,int col){\n\n\treturn row >= 0 && row <= H-1 && col >= 0 && col <= W-1;\n}\n\n//落下できるか判定\nbool judgeFall(int row,int col){\n\n\tif(row == H-1){\n\n\t\treturn false;\n\t}\n\n\tType type = GU;\n\tif(col%2 == 1){\n\n\t\ttype = KI;\n\t}\n\n\tfor(int i = 2; i <= 4; i++){\n\n\t\tint adj_row = row+d_row[type][i];\n\t\tint adj_col = col+d_col[type][i];\n\t\tif(!rangeCheck(adj_row,adj_col))continue;\n\n\t\tif(buf[adj_row][adj_col] != '.'){\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\n//落下処理\nvoid funcFall(int row,int col){\n\n\tbuf[row+1][col] = buf[row][col];\n\tbuf[row][col] = '.';\n}\n\n//連結判定&消滅処理\nbool funcDelete(){\n\n\tbool ret = false;\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tcheck[row][col] = false;\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(buf[row][col] == '.' || check[row][col])continue;\n\n\t\t\tcheck[row][col] = true;\n\n\t\t\tvector<pair<int,int>> vec;\n\t\t\tvec.push_back(make_pair(row,col));\n\n\t\t\tqueue<pair<int,int>> Q;\n\t\t\tQ.push(make_pair(row,col));\n\n\t\t\twhile(!Q.empty()){\n\n\t\t\t\tpair<int,int> tmp = Q.front();\n\t\t\t\tQ.pop();\n\n\t\t\t\tint tmp_row = tmp.first,tmp_col = tmp.second;\n\n\t\t\t\tType type = GU;\n\t\t\t\tif(tmp_col%2 == 1){\n\n\t\t\t\t\ttype = KI;\n\t\t\t\t}\n\n\t\t\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\t\t\tint adj_row = tmp_row+d_row[type][i];\n\t\t\t\t\tint adj_col = tmp_col+d_col[type][i];\n\n\t\t\t\t\tif(!rangeCheck(adj_row,adj_col) || (buf[adj_row][adj_col] != buf[tmp_row][tmp_col])\n\t\t\t\t\t\t\t|| check[adj_row][adj_col])continue;\n\n\t\t\t\t\tcheck[adj_row][adj_col] = true;\n\t\t\t\t\tvec.push_back(make_pair(adj_row,adj_col));\n\t\t\t\t\tQ.push(make_pair(adj_row,adj_col));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(vec.size() <= 2)continue;\n\n\t\t\tret = true;\n\n\t\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\t\tint tmp_row = vec[i].first;\n\t\t\t\tint tmp_col = vec[i].second;\n\n\t\t\t\tbuf[tmp_row][tmp_col] = '.';\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nvoid role(int row,int col){\n\n\tType type = GU;\n\tif(col%2 == 1){\n\n\t\ttype = KI;\n\t}\n\n\tstring str = \"\";\n\n\tfor(int i = 0; i < 6; i++){\n\n\t\tint adj_row = row+d_row[type][i];\n\t\tint adj_col = col+d_col[type][i];\n\n\t\tif(!rangeCheck(adj_row,adj_col))return;\n\n\t\tstr += buf[adj_row][adj_col];\n\t}\n\n\tstr = str.back()+str;\n\tstr.pop_back();\n\n\tfor(int i = 0; i < 6; i++){\n\n\t\tint adj_row = row+d_row[type][i];\n\t\tint adj_col = col+d_col[type][i];\n\n\t\tbuf[adj_row][adj_col] = str[i];\n\t}\n}\n\n\nvoid calc(){\n\n\twhile(true){\n\n\t\t//落下できるブロックが存在する間は、落下処理を行う\n\t\twhile(true){\n\n\t\t\tvector<pair<int,int>> vec;\n\t\t\t//落下判定\n\t\t\tfor(int row = 0; row < H-1; row++){\n\t\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\t\tif(buf[row][col] != '.' && judgeFall(row,col)){\n\n\t\t\t\t\t\tvec.push_back(make_pair(row,col));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(vec.size() == 0)break;\n\n\t\t\t//落下処理\n\t\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\t\tfuncFall(vec[i].first,vec[i].second);\n\t\t\t}\n\t\t}\n\n\t\tif(!funcDelete()){ //消滅ブロックなし\n\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\n\nint main(){\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",buf[row]);\n\t}\n\n\t//初期状態で処理\n\tcalc();\n\n\tint numQ;\n\tscanf(\"%d\",&numQ);\n\n\tint col,row;\n\tfor(int i = 0; i < numQ; i++){\n\n\t\tscanf(\"%d %d\",&col,&row);\n\n\t\trow = (H-1)-row;\n\n\t\trole(row,col);\n\t\tcalc();\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tprintf(\"%s\\n\",buf[row]);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3552, "score_of_the_acc": -0.9968, "final_rank": 6 }, { "submission_id": "aoj_1558_9166435", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 998244353\n//#define MOD 1000000007\n#define EPS 0.000000001\n\n\n\n#define SIZE 55\n\nenum Type{\n\tGU,\n\tKI,\n};\n\nint H,W;\nint d_row[2][6] = {{-1,-1,0,1,0,-1},{-1,0,1,1,1,0}},d_col[2][6] = {{0,1,1,0,-1,-1},{0,1,1,0,-1,-1}};\n\nchar buf[SIZE][SIZE];\nbool check[SIZE][SIZE];\n\nbool rangeCheck(int row,int col){\n\n\treturn row >= 0 && row <= H-1 && col >= 0 && col <= W-1;\n}\n\n//落下できるか判定\nbool judgeFall(int row,int col){\n\n\tif(row == H-1){\n\n\t\treturn false;\n\t}\n\n\tType type = GU;\n\tif(col%2 == 1){\n\n\t\ttype = KI;\n\t}\n\n\tfor(int i = 2; i <= 4; i++){\n\n\t\tint adj_row = row+d_row[type][i];\n\t\tint adj_col = col+d_col[type][i];\n\t\tif(!rangeCheck(adj_row,adj_col))continue;\n\n\t\tif(buf[adj_row][adj_col] != '.'){\n\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\n//落下処理\nvoid funcFall(int row,int col){\n\n\tbuf[row+1][col] = buf[row][col];\n\tbuf[row][col] = '.';\n}\n\n//連結判定&消滅処理\nbool funcDelete(){\n\n\tbool ret = false;\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\n\t\t\tcheck[row][col] = false;\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(buf[row][col] == '.' || check[row][col])continue;\n\n\t\t\tcheck[row][col] = true;\n\n\t\t\tvector<pair<int,int>> vec;\n\t\t\tvec.push_back(make_pair(row,col));\n\n\t\t\tqueue<pair<int,int>> Q;\n\t\t\tQ.push(make_pair(row,col));\n\n\t\t\twhile(!Q.empty()){\n\n\t\t\t\tpair<int,int> tmp = Q.front();\n\t\t\t\tQ.pop();\n\n\t\t\t\tint tmp_row = tmp.first,tmp_col = tmp.second;\n\n\t\t\t\tType type = GU;\n\t\t\t\tif(tmp_col%2 == 1){\n\n\t\t\t\t\ttype = KI;\n\t\t\t\t}\n\n\t\t\t\tfor(int i = 0; i < 6; i++){\n\n\t\t\t\t\tint adj_row = tmp_row+d_row[type][i];\n\t\t\t\t\tint adj_col = tmp_col+d_col[type][i];\n\n\t\t\t\t\tif(!rangeCheck(adj_row,adj_col) || (buf[adj_row][adj_col] != buf[tmp_row][tmp_col])\n\t\t\t\t\t\t\t|| check[adj_row][adj_col])continue;\n\n\t\t\t\t\tcheck[adj_row][adj_col] = true;\n\t\t\t\t\tvec.push_back(make_pair(adj_row,adj_col));\n\t\t\t\t\tQ.push(make_pair(adj_row,adj_col));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(vec.size() <= 2)continue;\n\n\t\t\tret = true;\n\n\t\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\t\tint tmp_row = vec[i].first;\n\t\t\t\tint tmp_col = vec[i].second;\n\n\t\t\t\tbuf[tmp_row][tmp_col] = '.';\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nvoid role(int row,int col){\n\n\tType type = GU;\n\tif(col%2 == 1){\n\n\t\ttype = KI;\n\t}\n\n\tstring str = \"\";\n\n\tfor(int i = 0; i < 6; i++){\n\n\t\tint adj_row = row+d_row[type][i];\n\t\tint adj_col = col+d_col[type][i];\n\n\t\tif(!rangeCheck(adj_row,adj_col))return;\n\n\t\tstr += buf[adj_row][adj_col];\n\n\t\t//printf(\"(%d,%d)%c\\n\",adj_row,adj_col,buf[adj_row][adj_col]);\n\t}\n\n\tstr = str.back()+str;\n\tstr.pop_back();\n\n\t//printf(\"str:%s\\n\",str.c_str());\n\n\tfor(int i = 0; i < 6; i++){\n\n\t\tint adj_row = row+d_row[type][i];\n\t\tint adj_col = col+d_col[type][i];\n\n\t\tbuf[adj_row][adj_col] = str[i];\n\t}\n}\n\n\nvoid calc(){\n\n\twhile(true){\n\n\t\t//落下できるブロックが存在する間は、落下処理を行う\n\t\twhile(true){\n\n\t\t\tvector<pair<int,int>> vec;\n\t\t\t//落下判定\n\t\t\tfor(int row = 0; row < H-1; row++){\n\t\t\t\tfor(int col = 0; col < W; col++){\n\t\t\t\t\tif(buf[row][col] != '.' && judgeFall(row,col)){\n\n\t\t\t\t\t\tvec.push_back(make_pair(row,col));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(vec.size() == 0)break;\n\n\t\t\t//落下処理\n\t\t\tfor(int i = 0; i < vec.size(); i++){\n\n\t\t\t\tfuncFall(vec[i].first,vec[i].second);\n\t\t\t}\n\t\t}\n\n\t\tif(!funcDelete()){ //消滅ブロックなし\n\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\nvoid debug(){\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tprintf(\"%s\\n\",buf[row]);\n\t}\n}\n\n\nint main(){\n\n\tscanf(\"%d %d\",&H,&W);\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",buf[row]);\n\t}\n\n\t//初期状態で処理\n\tcalc();\n\n\t//debug();\n\n\tint numQ;\n\tscanf(\"%d\",&numQ);\n\n\tint col,row;\n\tfor(int i = 0; i < numQ; i++){\n\n\t\tscanf(\"%d %d\",&col,&row);\n\n\t\trow = (H-1)-row;\n\n\t\trole(row,col);\n\t\tcalc();\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tprintf(\"%s\\n\",buf[row]);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3560, "score_of_the_acc": -1.1429, "final_rank": 8 }, { "submission_id": "aoj_1558_3560854", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstring col=\"RGBPYE\";\n\nint rx[2][6]={\n\t{0,-1,-1,0,1,1},\n\t{0,-1,-1,0,1,1},\n};\n\nint ry[2][6]={\n\t{-1,0,1,1,1,0},\n\t{-1,-1,0,1,0,-1},\n};\n\nint fx[2][3]={\n\t{-1,0,1},\n\t{-1,0,1},\n};\n\nint fy[2][3]={\n\t{0,-1,0},\n\t{-1,-1,-1},\n};\n\nint h,w;\nint fie[51][51];\n\nvoid print(){\n\tfor(int i=0;i<h;i++){\n\t\tfor(int j=0;j<w;j++){\n\t\t\tif(fie[h-1-i][j]==-1){\n\t\t\t\tprintf(\".\");\n\t\t\t}else{\n\t\t\t\tprintf(\"%c\",col[fie[h-1-i][j]]);\n\t\t\t}\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n}\n\nvoid rotate(int cx,int cy){\n\tfor(int i=0;i<6;i++){\n\t\tint nx=cx+rx[cx%2][i];\n\t\tint ny=cy+ry[cx%2][i];\n\t\tif(nx<0 || nx>=w || ny<0 || ny>=h)return;\n\t}\n\tint n0x=cx+rx[cx%2][0];\n\tint n0y=cy+ry[cx%2][0];\n\tint tmp=fie[n0y][n0x];\n\tfor(int i=5;i>=0;i--){\n\t\tint n1x=cx+rx[cx%2][i];\n\t\tint n1y=cy+ry[cx%2][i];\n\t\tint n2x=cx+rx[cx%2][(i+1)%6];\n\t\tint n2y=cy+ry[cx%2][(i+1)%6];\n\t\tfie[n2y][n2x]=fie[n1y][n1x];\n\t}\n\tint n1x=cx+rx[cx%2][1];\n\tint n1y=cy+ry[cx%2][1];\n\tfie[n1y][n1x]=tmp;\n}\n\nbool fall(int cx,int cy){\n\tfor(int i=0;i<3;i++){\n\t\tint nx=cx+fx[cx%2][i];\n\t\tint ny=cy+fy[cx%2][i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<h){\n\t\t\tif(fie[ny][nx]!=-1)return false;\n\t\t}\n\t}\n\tswap(fie[cy][cx],fie[cy-1][cx]);\n\treturn true;\n}\n\nint cnt[2501];\nint type[51][51];\nbool used[51][51];\n\nvoid dfs(int cx,int cy,int k){\n\tcnt[k]++;\n\ttype[cy][cx]=k;\n\tused[cy][cx]=true;\n\tfor(int i=0;i<6;i++){\n\t\tint nx=cx+rx[cx%2][i];\n\t\tint ny=cy+ry[cx%2][i];\n\t\tif(nx>=0 && nx<w && ny>=0 && ny<h){\n\t\t\tif(fie[ny][nx]==fie[cy][cx] && !used[ny][nx]){\n\t\t\t\tdfs(nx,ny,k);\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid process(){\n\tbool flag=true;\n\twhile(flag){\n\t\tflag=false;\n\t\tbool flag2=true;\n\t\twhile(flag2){\n\t\t\tflag2=false;\n\t\t\tfor(int i=1;i<h;i++){\n\t\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\t\tif(fie[i][j]!=-1 && fall(j,i)){\n\t\t\t\t\t\tflag2=true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tmemset(cnt,0,sizeof(cnt));\n\t\tmemset(used,false,sizeof(used));\n\t\tint sz=0;\n\t\tfor(int i=0;i<h;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tif(fie[i][j]!=-1 && !used[i][j]){\n\t\t\t\t\tdfs(j,i,sz++);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<h;i++){\n\t\t\tfor(int j=0;j<w;j++){\n\t\t\t\tif(fie[i][j]!=-1 && cnt[type[i][j]]>=3){\n\t\t\t\t\tfie[i][j]=-1;\n\t\t\t\t\tflag=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(void){\n\tscanf(\"%d%d\",&h,&w);\n\tmemset(fie,-1,sizeof(fie));\n\tfor(int i=0;i<h;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<w;j++){\n\t\t\tfor(int k=0;k<6;k++){\n\t\t\t\tif(col[k]==s[j]){\n\t\t\t\t\tfie[h-1-i][j]=k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprocess();\n\tint q;\n\tscanf(\"%d\",&q);\n\tfor(int i=0;i<q;i++){\n\t\tint a,b;\n\t\tscanf(\"%d%d\",&a,&b);\n\t\trotate(a,b);\n\t\tprocess();\n\t}\n\tprint();\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3312, "score_of_the_acc": -0.9011, "final_rank": 5 }, { "submission_id": "aoj_1558_3560741", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<queue>\nusing namespace std;\n\n#define REP(i,N) for(int (i)=0;(i)<(N);(i)++)\nconstexpr int dh[6] = { 1,1,1,0,-1,0 };\nconstexpr int dw[6] = { -1,0,1,1,0,-1 };\n\nconstexpr int dh2[6] = { 0,1,0,-1,-1,-1 };\nconstexpr int dw2[6] = { -1,0,1,1,0,-1 };\n\nint H, W, Q;\nvector<string> F;\nvector<pair<int, int>> xy;\n\nbool isin(int h, int w) {\n\treturn 0 <= h && h < H && 0 <= w && w < W;\n}\n\nvoid rotate(int h, int w) {\n\tREP(dir, 6) {\n\t\tint nh = h + (w % 2 == 1 ? dh : dh2)[dir];\n\t\tint nw = w + (w % 2 == 1 ? dw : dw2)[dir];\n\t\tif (!isin(nh, nw))return;\n\t}\n\tREP(dir, 5) {\n\t\tint nh = h + (w % 2 == 1 ? dh : dh2)[dir];\n\t\tint nw = w + (w % 2 == 1 ? dw : dw2)[dir];\n\t\tint nnh = h + (w % 2 == 1 ? dh : dh2)[dir + 1];\n\t\tint nnw = w + (w % 2 == 1 ? dw : dw2)[dir + 1];\n\t\tswap(F[nh][nw], F[nnh][nnw]);\n\t}\n}\n\nbool can_fall(int h, int w) {\n\tif (h == H - 1)return false;//下端\n\tREP(dir, 3) {\n\t\tint nh = h + (w % 2 == 1 ? dh : dh2)[dir];\n\t\tint nw = w + (w % 2 == 1 ? dw : dw2)[dir];\n\t\tif (!isin(nh, nw))continue;\n\t\tif (F[nh][nw] != '.')return false;\n\t}\n\treturn true;\n}\n\nbool fall() {\n\tbool res = false;\n\tfor (int h = H - 1; h >= 0; h--)REP(t, 2)REP(w, W) {\n\t\tif (w % 2 == t)continue; // ずれによる段数を考慮\n\t\tint nh = h;\n\t\tint nw = w;\n\t\twhile (can_fall(nh, nw)) {\n\t\t\tswap(F[nh][nw], F[nh + 1][nw]);\n\t\t\tnh++;\n\t\t\tres = true;\n\t\t}\n\t}\n\treturn res;\n}\n\nvector<pair<int, int>> feed(\n\tconst int sh,\n\tconst int sw,\n\tconst char color,\n\tvector<vector<bool>>& see\n) {\n\tvector<pair<int, int>> cands;\n\tqueue<pair<int, int>> q;\n\tq.push({ sh,sw });\n\tsee[sh][sw] = true;\n\tcands.push_back({ sh,sw });\n\twhile (!q.empty()) {\n\t\tauto now = q.front(); q.pop();\n\t\tREP(dir, 6) {\n\t\t\tint w = now.second;\n\t\t\tint nh = now.first + (w % 2 == 1 ? dh : dh2)[dir];\n\t\t\tint nw = now.second + (w % 2 == 1 ? dw : dw2)[dir];\n\t\t\tif (!isin(nh, nw))continue;\n\t\t\tif (see[nh][nw])continue;\n\t\t\tif (F[nh][nw] == color) {\n\t\t\t\tsee[nh][nw] = true;\n\t\t\t\tcands.push_back({ nh,nw });\n\t\t\t\tq.push({ nh,nw });\n\t\t\t}\n\t\t}\n\t}\n\treturn cands;\n}\n\nbool erase() {\n\tbool res = false;\n\tvector<vector<bool>> see(H, vector<bool>(W, false));\n\tREP(h, H)REP(w, W)if (F[h][w] != '.' && !see[h][w]) {\n\t\tauto cands = feed(h, w, F[h][w], see);\n\t\tif (cands.size() < 3)continue;\n\t\tfor (auto cand : cands)F[cand.first][cand.second] = '.';\n\t\tres = true;\n\t}\n\treturn res;\n}\n\nvoid debug() {\n\tcout << \"-----------------\" << endl;\n\tfor (auto row : F)cout << row << endl;\n\tcout << \"-----------------\" << endl;\n}\n\nvoid combo() {\n\twhile (true) {\n\t\tif (!erase())break;\n\t\t//debug();\n\t\tif (!fall())break;\n\t\t//debug();\n\t}\n}\n\nint main() {\n\tcin >> H >> W;\n\tF.resize(H);\n\tREP(i, H)cin >> F[i];\n\tcin >> Q;\n\tREP(i, Q) {\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\txy.push_back({ x,y });\n\t}\n\t//debug();\n\tcombo();\n\t//debug();\n\tfor (auto p : xy) {\n\t\trotate(H - 1 - p.second, p.first);\n\t\t//debug();\n\t\tfall();\n\t\t//debug();\n\t\tcombo();\n\t\t//debug();\n\t}\n\tfor (auto row : F)cout << row << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3240, "score_of_the_acc": -1.301, "final_rank": 9 }, { "submission_id": "aoj_1558_3112862", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nint n,m,q,a[51][51],dx[2][6]={{-1,-1,-1,0,1,0},{0,-1,0,1,1,1}},dy[6]={1,0,-1,-1,0,1};\nbool u[51][51],u2[51][51];\nbool check(int x, int y) {return (x>=0&&y>=0&&x<n&&y<m);}\n \nint dfs(int x, int y, int k, bool o) {\n if(k!=a[x][y]) return 0;\n u[x][y]=u2[x][y]=true;\n if(o) a[x][y]=0;\n int sum=0;\n for(int i=0; i<6; i++) {\n if(check(x+dx[y%2][i],y+dy[i]) && !u2[x+dx[y%2][i]][y+dy[i]]) sum+=dfs(x+dx[y%2][i],y+dy[i],k,o);\n }\n return sum+1;\n}\n \nint main() {\n cin >> n >> m;\n for(int i=0; i<n; i++) {\n for(int j=0; j<m; j++) {\n char c;\n cin >> c;\n a[i][j]=(int)c;\n }\n }\n cin >> q;\n for(int t=0; t<=q; t++) {\n if(t) {\n int x,y;\n cin >> y >> x;\n x=n-x-1;\n int cnt=0;\n for(int i=0; i<6; i++) if(check(x+dx[y%2][i],y+dy[i])) cnt++;\n if(cnt==6) for(int i=0; i<5; i++) swap(a[x+dx[y%2][i]][y+dy[i]],a[x+dx[y%2][i+1]][y+dy[i+1]]);\n }\n while(1) {\n for(int i=n*2-1; i>=0; i--) {\n for(int j=i%2; j<m; j+=2) {\n int h=i/2;\n while(h<n-1 && a[h][j]) {\n for(int k=3; k<6; k++) if(check(h+dx[j%2][k],j+dy[k]) && a[h+dx[j%2][k]][j+dy[k]]) goto end;\n h++;\n swap(a[h][j],a[h-1][j]);\n }\n end:;\n }\n }\n bool ck=true;\n memset(u,0,sizeof(u));\n for(int i=0; i<n; i++) {\n for(int j=0; j<m; j++) {\n memset(u2,0,sizeof(u2));\n if(!u[i][j] && a[i][j] && dfs(i,j,a[i][j],0)>=3) {\n memset(u2,0,sizeof(u2));\n dfs(i,j,a[i][j],1);\n ck=false;\n }\n }\n }\n if(ck) break;\n }\n }\n for(int i=0; i<n; i++) {\n for(int j=0; j<m; j++) cout << (a[i][j]?(char)a[i][j]:'.');\n cout << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3124, "score_of_the_acc": -0.8262, "final_rank": 4 }, { "submission_id": "aoj_1558_1671474", "code_snippet": "/*\n * h.cc: \n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n\nusing namespace std;\n\n/* constant */\n\nconst int MAX_H = 50;\nconst int MAX_W = 50;\n\nconst int dxs[6] = {-1, 0, 1, 1, 0, -1};\nconst int dys[2][6] = {{0, -1, 0, 1, 1, 1}, {-1, -1, -1, 0, 1, 0}};\n\n/* typedef */\n\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\ntypedef queue<pii> qpii;\n\n/* global variables */\n\nint h, w;\nstring flds[MAX_H];\nbool used[MAX_H][MAX_W];\n\n/* subroutines */\n\ninline bool floating(int x, int y) {\n return\n (y > 0 && flds[y - 1][x] == '.' &&\n (x <= 0 || flds[y + dys[x & 1][0]][x - 1] == '.') &&\n (x >= w - 1 || flds[y + dys[x & 1][2]][x + 1] == '.'));\n}\n\nvoid fall_all() {\n for (int y = 1; y < h; y++) {\n for (int x = 1; x < w; x += 2)\n for (int y0 = y; flds[y0][x] != '.' && floating(x, y0); y0--)\n\tswap(flds[y0][x], flds[y0 - 1][x]);\n for (int x = 0; x < w; x += 2)\n for (int y0 = y; flds[y0][x] != '.' && floating(x, y0); y0--)\n\tswap(flds[y0][x], flds[y0 - 1][x]);\n }\n}\n\nbool vanish() {\n bool vnsh = false;\n memset(used, false, sizeof(used));\n\n for (int y0 = 0; y0 < h; y0++)\n for (int x0 = 0; x0 < w; x0++)\n if (flds[y0][x0] != '.' && ! used[y0][x0]) {\n\tvpii v;\n\tv.push_back(pii(x0, y0));\n\tqpii q;\n\tq.push(pii(x0, y0));\n\tchar ch = flds[y0][x0];\n\tused[y0][x0] = true;\n\n\twhile (! q.empty()) {\n\t pii u = q.front(); q.pop();\n\t int &ux = u.first, &uy = u.second;\n\t for (int di = 0; di < 6; di++) {\n\t int vx = ux + dxs[di], vy = uy + dys[ux & 1][di];\n\t if (vx >= 0 && vx < w && vy >= 0 && vy < h &&\n\t\t! used[vy][vx] && flds[vy][vx] == ch) {\n\t used[vy][vx] = true;\n\t v.push_back(pii(vx, vy));\n\t q.push(pii(vx, vy));\n\t }\n\t }\n\t}\n\n\tif (v.size() >= 3) {\n\t for (vpii::iterator vit = v.begin(); vit != v.end(); vit++)\n\t flds[vit->second][vit->first] = '.';\n\t vnsh = true;\n\t}\n }\n\n return vnsh;\n}\n\nvoid do_proc() {\n do {\n fall_all();\n } while (vanish());\n}\n\nvoid print_flds() {\n for (int y = h - 1; y >= 0; y--) cout << flds[y] << endl;\n}\n\n/* main */\n\nint main() {\n cin >> h >> w;\n for (int y = h - 1; y >= 0; y--) cin >> flds[y];\n\n do_proc();\n //print_flds();\n\n int q;\n cin >> q;\n while (q--) {\n int cx, cy;\n cin >> cx >> cy;\n if (cx <= 0 || cx >= w - 1 || cy <= 0 || cy >= h - 1) continue;\n\n char ctmp = flds[cy + dys[cx & 1][0]][cx + dxs[0]];\n for (int di = 1; di < 6; di++)\n flds[cy + dys[cx & 1][di - 1]][cx + dxs[di - 1]] =\n\tflds[cy + dys[cx & 1][di]][cx + dxs[di]];\n flds[cy + dys[cx & 1][5]][cx + dxs[5]] = ctmp;\n\n do_proc();\n }\n\n print_flds();\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1220, "score_of_the_acc": -0.6384, "final_rank": 2 }, { "submission_id": "aoj_1558_1249320", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nint dy[][6] = {{1, 1, 0, -1, 0, 1}, {1, 0, -1, -1, -1, 0}};\nint dx[] = {0, 1, 1, 0, -1, -1};\n\nvoid dropBlock(vector<string>& f)\n{\n int h = f.size();\n int w = f[0].size();\n\n for(int y=1; y<h; ++y){\n for(int i=0; i<w; ++i){\n int x;\n if(i < w / 2)\n x = i * 2 + 1;\n else\n x = (i - w / 2) * 2;\n if(f[y][x] == '.')\n continue;\n\n int y2 = y;\n while(y2 > 0 && f[y2-1][x] == '.' && (x == 0 || f[y2+dy[x%2][4]][x-1] == '.') && (x == w - 1 || f[y2+dy[x%2][2]][x+1] == '.')){\n swap(f[y2][x], f[y2-1][x]);\n -- y2;\n }\n }\n }\n}\n\nbool eraseBlock(vector<string>& f)\n{\n int h = f.size();\n int w = f[0].size();\n\n bool ans = false;\n vector<vector<bool> > check(h, vector<bool>(w, false));\n for(int y=0; y<h; ++y){\n for(int x=0; x<w; ++x){\n if(check[y][x] || f[y][x] == '.')\n continue;\n\n vector<pair<int, int> > v;\n queue<pair<int, int> > q;\n v.push_back(make_pair(y, x));\n q.push(make_pair(y, x));\n check[y][x] = true;\n while(!q.empty()){\n int y2 = q.front().first;\n int x2 = q.front().second;\n q.pop();\n\n for(int i=0; i<6; ++i){\n int y3 = y2 + dy[x2%2][i];\n int x3 = x2 + dx[i];\n if(0 <= y3 && y3 < h && 0 <= x3 && x3 < w && !check[y3][x3] && f[y2][x2] == f[y3][x3]){\n v.push_back(make_pair(y3, x3));\n q.push(make_pair(y3, x3));\n check[y3][x3] = true;\n }\n }\n }\n\n if(v.size() >= 3){\n ans = true;\n for(unsigned i=0; i<v.size(); ++i)\n f[v[i].first][v[i].second] = '.';\n }\n }\n }\n\n return ans;\n}\n\nvoid rotateBlock(vector<string>& f, int y, int x)\n{\n int h = f.size();\n int w = f[0].size();\n if(y == 0 || y == h-1 || x == 0 || x == w-1)\n return;\n\n for(int i=5; i>0; --i){\n int y2 = y + dy[x%2][i];\n int x2 = x + dx[i];\n int y3 = y + dy[x%2][i-1];\n int x3 = x + dx[i-1];\n swap(f[y2][x2], f[y3][x3]);\n }\n}\n\nint main()\n{\n int h, w;\n cin >> h >> w;\n\n vector<string> f(h);\n for(int i=h-1; i>=0; --i)\n cin >> f[i];\n\n int q;\n cin >> q;\n for(;;){\n do{\n dropBlock(f);\n }while(eraseBlock(f));\n\n if(--q < 0)\n break;\n\n int y, x;\n cin >> x >> y;\n rotateBlock(f, y, x);\n }\n \n for(int i=h-1; i>=0; --i)\n cout << f[i] << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 1220, "score_of_the_acc": -0.6384, "final_rank": 2 }, { "submission_id": "aoj_1558_1249045", "code_snippet": "#include <algorithm>\n#define IN(x,s,g) ((x) >= (s) && (x) < (g))\n#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))\n\n#include <queue>\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\n#include <cstdio>\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\n#include <set>\n\nusing namespace std;\n\nchar b[64][64];\nint w, h;\n\ninline int id(int x, int y){\n return y * w + x;\n}\n\nint dx[6] = { 0, 1, 1, 0, -1, -1 };\nint dy[2][6] = {\n { 1, 1, 0, -1, 0, 1 },\n { 1, 0, -1, -1, -1, 0 }\n};\n\n#define REPEX(x, y, xx, yy) \\\n for(int _ = 0, xx, yy; (_ < 6) && ((xx = x + dx[_]), (yy = y + dy[x % 2][_]), true); _++)\n\nint memo[64][64];\n\nint dfs(int y, int x, int id){\n int ret = 1;\n memo[y][x] = id;\n REPEX(x, y, xx, yy) if(ISIN(xx, yy, w, h) && b[yy][xx] == b[y][x] && memo[yy][xx] == -1){\n ret += dfs(yy, xx, id);\n }\n return ret;\n}\n\nvoid printBoard(){\n REP(i,h){\n puts(b[h - i - 1]);\n }\n}\n\nbool erase(){\n bool ret = false;\n REP(i,h) REP(j,w) memo[i][j] = -1;\n int id = 0;\n vector<int> e(h * w);\n REP(i,h) REP(j,w) if(memo[i][j] == -1 && b[i][j] != '.'){\n const int cnt = dfs(i, j, id);\n if(cnt >= 3){ e[id] = 1; ret = true; }\n id++;\n }\n REP(i,h) REP(j,w) if(memo[i][j] != -1 && e[memo[i][j]])\n b[i][j] = '.';\n // printBoard(); REP(i,h){ REP(j,w) printf(\"%d \", memo[i][j]); puts(\"\"); }\n return ret;\n}\n\nbool canDrop(int x, int y){\n for(int i = 2; i <= 4; i++){\n const int xx = x + dx[i];\n const int yy = y + dy[x % 2][i];\n if(ISIN(xx, yy, w, h) && b[yy][xx] != '.') return false;\n }\n return true;\n}\n\nbool drop(){\n bool ret = false;\n REP(i,h - 1){\n const int y = i + 1;\n for(int x = 1; x < w; x += 2){\n if(b[y][x] == '.') continue;\n int yy = y;\n while(yy > 0 && canDrop(x, yy)){ yy--; ret = true; }\n if(y != yy) swap(b[yy][x], b[y][x]);\n }\n for(int x = 0; x < w; x += 2){\n if(b[y][x] == '.') continue;\n int yy = y;\n while(yy > 0 && canDrop(x, yy)){ yy--; ret = true; }\n if(y != yy) swap(b[yy][x], b[y][x]);\n }\n }\n // printf(\"drop: %d\\n\", ret);\n return ret;\n}\n\nbool rotate(int x, int y){\n REPEX(x, y, xx, yy) if(!ISIN(xx, yy, w, h)) return false;\n\n for(int i = 0; i < 5; i++){\n const int p = (6 - i) % 6;\n const int q = (5 - i) % 6;\n swap(b[y + dy[x % 2][p]][x + dx[p]],\n b[y + dy[x % 2][q]][x + dx[q]]);\n }\n\n return true;\n}\n\nvoid process(){\n while(drop() || erase());\n}\n\nint main(){\n h = getInt(); w = getInt();\n REP(i,h) scanf(\"%s\", b[h - i - 1]);\n\n process();\n const int q = getInt();\n REP(_,q){\n const int x = getInt();\n const int y = getInt();\n if(rotate(x, y)) process();\n }\n\n printBoard();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1052, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1558_1248941", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=a;i<b;++i)\n#define for_rev(i,a,b) for(int i=a;i>=b;--i)\n#define rep(i,n) for(int i=0;i<(n);++i)\n#define allof(a) a.begin(),a.end()\n#define minit(a,b) memset(a,b,sizeof(a))\n#define size_of(a) (int)a.size()\n\ntypedef long long lint;\ntypedef double Double;\ntypedef pair< int, int > pii;\n\nint dx[6] = {0,1,1,0,-1,-1};\n\nint dy[2][6] = {\n\t{1,1,0,-1,0,1},\n\t{1,0,-1,-1,-1,0}\n};\n\nint H, W, Q;\nstring hanic[55];\n\nvoid rotate(int x, int y) {\n\tif (x == 0 || y == 0 || x == W - 1 || y == H - 1) return;\n\t\n\tint xp = x % 2;\n\tchar piv = hanic[y + dy[xp][5]][x + dx[5]];\n\t\n\tfor_rev(i,5,1) {\n\t\thanic[y + dy[xp][i]][x + dx[i]] = hanic[y + dy[xp][i - 1]][x + dx[i - 1]];\n\t}\n\t\n\thanic[y + dy[xp][0]][x + dx[0]] = piv;\n}\n\nvoid fall() {\n\tbool update = true;\n\t\n\twhile (update) {\n\t\tupdate = false;\n\t\t\n\t\tfor (int i = 1; i < H && !update; ++i)\n\t\tfor (int j = 0; j < W && !update; ++j) {\n\t\t\tif (hanic[i][j] == '.') continue;\n\t\t\t\n\t\t\tbool fal = true;\n\t\t\tfor_(d,2,5) {\n\t\t\t\tif (j == 0 && d == 4) continue;\n\t\t\t\tif (j == W - 1 && d == 2) continue;\n\t\t\t\tfal &= (hanic[i + dy[j % 2][d]][j + dx[d]] == '.');\n\t\t\t}\n\t\t\t\n\t\t\tif (fal) {\n\t\t\t\thanic[i + dy[j % 2][3]][j + dx[3]] = hanic[i][j];\n\t\t\t\thanic[i][j] = '.';\n\t\t\t\tupdate = true;\n\t\t\t}\n\t\t}\n\t}\n}\n\nbool vis[55][55];\n\nvoid rec(int x, int y, char col, vector< pii >& vp) {\n\tvp.push_back(pii(x, y));\n\tvis[y][x] = 1;\n\t\n\tfor_(d,0,6) {\n\t\tint nx = x + dx[d], ny = y + dy[x % 2][d];\n\t\tif (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;\n\t\tif (vis[ny][nx]) continue;\n\t\tif (hanic[ny][nx] == col) rec(nx, ny, col, vp);\n\t}\n}\n\nbool vanish() {\n\tminit(vis, 0);\n\tbool res = false;\n\t\n\tfor_(y,0,H) for_(x,0,W) {\n\t\tif (!vis[y][x] && hanic[y][x] != '.') {\n\t\t\tvector< pii > vp;\n\t\t\n\t\t\trec(x, y, hanic[y][x], vp);\n\t\t\t\n\t\t\tif (vp.size() >= 3) {\n\t\t\t\tfor (size_t i = 0; i < vp.size(); ++i) {\n\t\t\t\t\tpii p = vp[i];\n\t\t\t\t\thanic[p.second][p.first] = '.';\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tres = true;\n\t\t\t}\n\t\t}\n\t}\n\t\n\treturn res;\n}\n\nint main() {\n\tcin >> H >> W;\n\t\n\tfor_(i,0,H) cin >> hanic[H - i - 1];\n\t\n\tfall();\n\twhile (vanish()) fall();\n\t\n\tcin >> Q;\n\tfor_(i,0,Q) {\n\t\tint x, y;\n\t\tcin >> x >> y;\n\t\trotate(x, y);\n\t\tfall();\n\t\twhile (vanish()) fall();\n\t}\n\t\n\tfor_(i,0,H) cout << hanic[H - i - 1] << endl;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 1216, "score_of_the_acc": -1.0654, "final_rank": 7 } ]
aoj_1562_cpp
Divisor Problem 12以下の自然数 N が与えられるので、約数の個数がちょうど N 個であるような最小の自然数を求めよ。 Input 1つの自然数 N が 1 行で与えられる。 Constraints 1 ≤ N ≤ 12 Output 約数の個数がちょうど N 個であるような最小の自然数を1行に出力せよ。 Sample Input 1 1 Sample Output 1 1 Sample Input 2 2 Sample Output 2 2 Sample Input 3 3 Sample Output 3 4
[ { "submission_id": "aoj_1562_2884395", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n;\n\tcin>>n;\n\tint ans=0;\n\tint count[10000]={};\n\tfor(int i=1;i<=10000;i++){\n\t\tfor(int j=1;j<=10000;j++){\n\t\t\tif(i % j == 0) count[i]++;\n\t\t}\n\t\tif(count[i] == n){\n\t\t\tans = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tcout<<ans<<endl;\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3128, "score_of_the_acc": -1.0278, "final_rank": 4 }, { "submission_id": "aoj_1562_2884389", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n,m,i=0;\n\tcin >> n;\n\twhile(1){\n\t\tm=0;\n\t\ti++;\n\t\tfor(int j=1;j<100000;j++){\n\t\t\tif(i%j==0){\n\t\t\t\tm++;\n\t\t\t}\n\t\t}\n\t\tif(m==n){\n\t\t\tcout << i << endl;\n\t\t\tbreak;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 3080, "score_of_the_acc": -1.6423, "final_rank": 6 }, { "submission_id": "aoj_1562_2884378", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n\n int N;\n cin >> N;\n\n int count = 0;\n for(int n = 1; n < 10000; n++){\n for(int i = 1; i < 10000; i++){\n if(n%i == 0)count++;\n\n }\n if(N == count){\n cout << n << endl; return 0;\n }\n count = 0;\n}\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3104, "score_of_the_acc": -1.0156, "final_rank": 3 }, { "submission_id": "aoj_1562_2576352", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint a[1<<13],n;\nint main() {\n cin >> n;\n for(int i=1; i<=(1<<n); i++) {\n for(int j=1; j<=(1<<n); j++) a[j]+=!(j%i);\n }\n for(int i=1; i<=(1<<n); i++) {\n if(a[i]==n) {\n cout << i << endl;\n break;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3124, "score_of_the_acc": -1.0813, "final_rank": 5 }, { "submission_id": "aoj_1562_1521498", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = 1 << 30;\n\nint D(int x)\n{\n int ret = 0;\n for(int i = 1; i <= x; i++) {\n if(x % i == 0) ret++;\n }\n return(ret);\n}\n\nint main()\n{\n int data[14];\n fill_n(data, 14, INF);\n for(int i = 1; i < 10000; i++) {\n data[min(13, D(i))] = min(data[min(13, D(i))], i);\n }\n int N;\n cin >> N;\n cout << data[N] << endl;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 1160, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_1562_1520994", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <string>\n#include <cstring>\n#include <sstream>\n#include <algorithm>\n#include <functional>\n#include <queue>\n#include <stack>\n#include <cmath>\n#include <iomanip>\n#include <list>\n#include <tuple>\n#include <bitset>\n#include <ciso646>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair<ll, ll> P;\ntypedef tuple<ll, ll, ll> T;\ntypedef vector<ll> vec;\n\ninline bool cheak(ll x, ll y, ll xMax, ll yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; }\ninline ll toInt(string s) { ll v; istringstream sin(s); sin >> v; return v; }\ntemplate<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }\ntemplate<class T> inline T sqr(T x) { return x*x; }\n\n#define For(i,a,b)\tfor(ll (i) = (a);i < (b);(i)++)\n#define rep(i,n)\tFor(i,0,n)\n#define rFor(i,a,b)\tfor(ll (i) = (a-1);i >= (b);(i)--)\n#define rrep(i,n)\trFor(i,n,0)\n#define clr(a)\t\tmemset((a), 0 ,sizeof(a))\n#define mclr(a)\t\tmemset((a), -1 ,sizeof(a))\n#define all(a)\t\t(a).begin(),(a).end()\n\nconst ll mod = 1e9 + 7;\nconst ll INF = 1e9 + 9;\n\nint count(ll n){\n\tint ret = 0;\n\tfor (int j = 1; j <= n; j++){\n\t\tif (n % j == 0){\n\t\t\tret++;\n\t\t}\n\t}\n\treturn ret;\n}\n\nint main(){\n\n\tll n;\n\tcin >> n;\n\tfor (int i = 1;; i++){\n\t\tif (count(i) == n){\n\t\t\tcout << i << endl;\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1160, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1559_cpp
Problem I: Hopping Mind Problem チエノとカカオは同じ喫茶店で働く姉妹である。2人はとても仲が良く、ある日、とあるテーブルゲームで遊ぶことになった。 ゲームは R マス× C マスの盤面と、駒としてうさぎのTPを用いる。盤面の各マスは白か黒の色が塗られている。最初にTPを盤面の右下( R , C )におき、2人で次の行動を交互に行う。TPの現在の位置を( a , b )とすると、そこからジャンプ可能な位置( i , j )を1つ選び、TPをそこにジャンプさせる。TPがジャンプ可能な位置( i , j )は以下をすべて満たす。 1 ≤ i ≤ R かつ 1 ≤ j ≤ C かつ i ≤ a かつ j ≤ b かつ 1 ≤ ( a - i ) + ( b - j ) ≤ K ( i , j )は白いマスである 自分のターンにTPをジャンプさせることができなくなった場合、負けとなる。 チエノが先手、カカオが後手でこのゲームを行う。カカオは頭の中でゲームを最後まで先読みすることができ、常に最適な行動をとる。この時、チエノが勝つ方法が存在するかどうかを判定せよ。 Input 入力は以下の形式で与えられる。 R C K G 1,1 G 1,2 ... G 1,C G 2,1 G 2,2 ... G 2,C : G R,1 G R,2 ... G R,C 1行目に3つの整数 R , C , K が空白区切りで与えられる。次の R 行に盤面の情報として C 個の".”または"#”が与えられる。 G i,j は盤面の位置( i , j )の色を表し、”.”が白、"#”が黒を表す。 Constraints 1 ≤ R , C ≤ 1000 1 ≤ K ≤ 2000 G R,C は“.”である Output チエノが勝つ方法が存在する場合は”Chieno”を、存在しない場合は”Cacao”を1行に出力せよ。 Sample Input1 3 3 2 ... ... ... Sample Output1 Chieno Sample Input2 3 3 2 #.# .#. #.. Sample Output2 Cacao
[ { "submission_id": "aoj_1559_10760662", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 1005\n\nint H,W,K;\nint table[SIZE][SIZE];\nchar base_map[SIZE][SIZE];\n\n\nint main(){\n\n\tscanf(\"%d %d %d\",&H,&W,&K);\n\n\tfor(int row = 0; row < H; row++){\n\n\t\tscanf(\"%s\",base_map[row]);\n\t}\n\n\ttable[0][0] = -1;\n\tint North,West;\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tif(row == 0 && col == 0)continue;\n\n\t\t\tNorth = -1,West = -1;\n\n\t\t\tif(row > 0){\n\t\t\t\tif(base_map[row-1][col] == '.' && table[row-1][col] <= 0){\n\n\t\t\t\t\tNorth = K;\n\t\t\t\t}else{\n\n\t\t\t\t\tNorth = table[row-1][col]-1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(col > 0){\n\t\t\t\tif(base_map[row][col-1] == '.' && table[row][col-1] <= 0){\n\n\t\t\t\t\tWest = K;\n\n\t\t\t\t}else{\n\n\t\t\t\t\tWest = table[row][col-1]-1;\n\t\t\t\t}\n\n\t\t\t}\n\t\t\ttable[row][col] = max(North,West);\n\t\t}\n\t}\n\n\tif(table[H-1][W-1] > 0){\n\n\t\tprintf(\"Chieno\\n\");\n\n\t}else{\n\n\t\tprintf(\"Cacao\\n\");\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8388, "score_of_the_acc": -0.0277, "final_rank": 3 }, { "submission_id": "aoj_1559_10235134", "code_snippet": "// AOJ #1559 Hopping Mind\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\n \nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n int R, C, K;\n cin >> R >> C >> K;\n vector<string> board(R);\n for (int i = 0; i < R; i++) cin >> board[i];\n \n vector<vector<bool>> dp(R+1, vector<bool>(C+1, false));\n \n int sMax = R + C;\n vector<vector<int>> lose(sMax+1);\n \n for (int s = 2; s <= sMax; s++){\n int iLow = max(1, s - C);\n int iHigh = min(R, s - 1);\n for (int i = iLow; i <= iHigh; i++){\n int j = s - i;\n if(board[i-1][j-1] == '#') continue;\n \n bool win = false;\n int dLow = max(2, s - K);\n int dHigh = s - 1;\n for (int d = dLow; d <= dHigh; d++){\n int lowerRow = max(1, d - j);\n if(lowerRow > i) continue;\n auto it = std::lower_bound(lose[d].begin(), lose[d].end(), lowerRow);\n if(it != lose[d].end() && *it <= i){\n win = true;\n break;\n }\n }\n dp[i][j] = win;\n if(!win) lose[s].push_back(i);\n }\n }\n cout << ( dp[R][C] ? \"Chieno\" : \"Cacao\" ) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 6576, "score_of_the_acc": -0.7799, "final_rank": 8 }, { "submission_id": "aoj_1559_3580400", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct BIT{\n // [1,n]\n int n; vector<ll> bit;\n // 初期化\n BIT(){}\n BIT(int _n){\n n = _n;\n bit = vector<ll>(n+1,0);\n }\n // sum of [1,i]\n ll sum(int i){\n ll s = 0;\n while(i>0){\n s += bit[i];\n i -= i & -i;\n }\n return s;\n }\n // add x in i-th element\n void add(int i, ll x){\n while(i<=n){\n bit[i] += x;\n i += i & -i;\n }\n }\n // [l,r]\n ll sum(int l, int r){\n return sum(r)-sum(l-1);\n }\n};\n\nconst int M = 2020;\nconst int N = 3333;\n\nBIT row[N],col[N];\n\nint main(){\n int h,w,k;\n cin >>h >>w >>k;\n\n vector<string> s(h);\n rep(i,h) cin >>s[i];\n\n vector<vector<pair<int,int>>> v(h+w-1);\n rep(i,h)rep(j,w) v[i+j].pb({i, j});\n\n rep(i,N){\n row[i] = BIT(N);\n col[i] = BIT(N);\n }\n\n auto calc = [&](int y, int x, ll val){\n y -= M;\n x -= M;\n if(s[y][x] == '#') return 0;\n return (int)!val;\n };\n\n ll sum = 0;\n int pi = M, pj = M;\n vector<vector<int>> dp(N,vector<int>(N));\n rep(i,h+w-1){\n sort(all(v[i]));\n int V = v[i].size();\n\n int si = v[i][0].fi+M, sj = v[i][0].se+M;\n if(i>0){\n int ti = pi-k, tj = pj;\n while(ti<=pi){\n sum -= dp[ti][tj];\n ++ti;\n --tj;\n }\n sum += dp[pi][pj];\n\n if(pi == si) sum += col[sj].sum(si-k,si-1);\n else sum += row[si].sum(sj-k,sj-1);\n }\n\n dp[si][sj] = calc(si, sj, sum);\n ll tsum = sum;\n for(int j=1; j<V; ++j){\n int ti = v[i][j].fi+M, tj = v[i][j].se+M;\n tsum -= col[tj+1].sum(ti-k-1, ti-1-1);\n tsum += row[ti].sum(tj-k, tj-1);\n dp[ti][tj] = calc(ti, tj, tsum);\n }\n\n rep(j,V){\n int ti = v[i][j].fi+M, tj = v[i][j].se+M;\n row[ti].add(tj, dp[ti][tj]);\n col[tj].add(ti, dp[ti][tj]);\n }\n\n pi = si;\n pj = sj;\n }\n\n cout << (dp[M+h-1][M+w-1]?\"Cacao\":\"Chieno\") << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 231996, "score_of_the_acc": -2, "final_rank": 9 }, { "submission_id": "aoj_1559_3560788", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef pair<int,int> P;\ntypedef long long ll;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);(i)++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n\n#define INF INT_MAX/3\n\nint H,W,K;\nchar S[1001][1001];\nint d[1001][1001];\n\nint main(){\n scanf(\"%d%d%d\",&H,&W,&K);\n rep(i,H){\n scanf(\"%s\",S[i]);\n }\n\n rep(i,H)rep(j,W){\n if(i==0&&j==0){\n d[i][j]=-1;\n continue;\n }\n\n int a=-INF,b=-INF;\n if(i>0){\n if(S[i-1][j]=='.'&&d[i-1][j]<=0)a=K; \n else a=d[i-1][j]-1;\n }\n if(j>0){\n if(S[i][j-1]=='.'&&d[i][j-1]<=0)b=K; \n else b=d[i][j-1]-1;\n }\n\n d[i][j]=max(a,b);\n }\n\n if(d[H-1][W-1]<=0)cout<<\"Cacao\"<<endl;\n else cout<<\"Chieno\"<<endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 8064, "score_of_the_acc": -0.0263, "final_rank": 2 }, { "submission_id": "aoj_1559_3560784", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef pair<int,int> P;\ntypedef long long ll;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);(i)++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n\n#define INF INT_MAX/3\n\nint H,W,K;\nchar S[1001][1001];\nint d[1001][1001];\n\nint main(){\n scanf(\"%d%d%d\",&H,&W,&K);\n rep(i,H){\n scanf(\"%s\",S[i]);\n }\n\n rep(i,H)rep(j,W){\n if(i==0&&j==0){\n d[i][j]=-1;\n continue;\n }\n\n int a=INF,b=INF;\n if(i>0){\n if(S[i-1][j]=='.'&&d[i-1][j]<=0)a=K; \n else a=d[i-1][j]-1;\n }\n if(j>0){\n if(S[i][j-1]=='.'&&d[i][j-1]<=0)b=K; \n else b=d[i][j-1]-1;\n }\n\n d[i][j]=min(a,b);\n }\n\n if(d[H-1][W-1]<=0)cout<<\"Cacao\"<<endl;\n else cout<<\"Chieno\"<<endl;\n\n return 0;\n}", "accuracy": 0.34375, "time_ms": 10, "memory_kb": 8092, "score_of_the_acc": -0.0265, "final_rank": 11 }, { "submission_id": "aoj_1559_3560730", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\n\nint r,c,k;\nint fie[1005][1005];\nPP p[1000005];\nint dp[1005][1005];\nint maxi[1005][1005];\n\nint main(void){\n\tscanf(\"%d%d%d\",&r,&c,&k);\n\tfor(int i=0;i<r;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<c;j++){\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=1;\n\t\t\t}\n\t\t\tp[i*c+j]=PP(i+j,P(i,j));\n\t\t}\n\t}\n\tsort(p,p+r*c);\n\tqueue<P> que;\n\tfor(int i=0;i<r*c;i++){\n\t\tque.push(p[i].second);\n\t}\n\tmemset(maxi,-1,sizeof(maxi));\n\twhile(que.size()){\n\t\tP p=que.front();\n\t\tque.pop();\n\t\tint x=p.second,y=p.first;\n\t\tif(x>0){\n\t\t\tmaxi[y][x]=max(maxi[y][x],maxi[y][x-1]);\n\t\t}\n\t\tif(y>0){\n\t\t\tmaxi[y][x]=max(maxi[y][x],maxi[y-1][x]-1);\n\t\t}\n\t\tif(fie[y][x]==1)continue;\n\t\tif(x<=maxi[y][x]){\n\t\t\tdp[y][x]=1;\n\t\t}else{\n\t\t\tdp[y][x]=0;\n\t\t\tmaxi[y][x]=x+k;\n\t\t}\n\t}\n\tfor(int i=0;i<r;i++){\n\t\tfor(int j=0;j<c;j++){\n\t\t\t//printf(\"%d \",dp[i][j]);\n\t\t}\n\t\t//printf(\"\\n\");\n\t}\n\tprintf(\"%s\\n\",dp[r-1][c-1]==1?\"Chieno\":\"Cacao\");\n\treturn 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 34536, "score_of_the_acc": -0.5414, "final_rank": 7 }, { "submission_id": "aoj_1559_3560722", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\n\nint r,c,k;\nint fie[1005][1005];\nPP p[1000005];\nint dp[1005][1005];\nint maxi[1005][1005];\n\nint main(void){\n\tscanf(\"%d%d%d\",&r,&c,&k);\n\tfor(int i=0;i<r;i++){\n\t\tstring s;\n\t\tcin >> s;\n\t\tfor(int j=0;j<c;j++){\n\t\t\tif(s[j]=='#'){\n\t\t\t\tfie[i][j]=1;\n\t\t\t}\n\t\t\tp[i*r+j]=PP(i+j,P(i,j));\n\t\t}\n\t}\n\tsort(p,p+r*c);\n\tqueue<P> que;\n\tfor(int i=0;i<r*c;i++){\n\t\tque.push(p[i].second);\n\t}\n\tmemset(maxi,-1,sizeof(maxi));\n\twhile(que.size()){\n\t\tP p=que.front();\n\t\tque.pop();\n\t\tint x=p.second,y=p.first;\n\t\tif(x>0){\n\t\t\tmaxi[y][x]=max(maxi[y][x],maxi[y][x-1]);\n\t\t}\n\t\tif(y>0){\n\t\t\tmaxi[y][x]=max(maxi[y][x],maxi[y-1][x]-1);\n\t\t}\n\t\tif(fie[y][x]==1)continue;\n\t\tif(x<=maxi[y][x]){\n\t\t\tdp[y][x]=1;\n\t\t}else{\n\t\t\tdp[y][x]=0;\n\t\t\tmaxi[y][x]=x+k;\n\t\t}\n\t}\n\tfor(int i=0;i<r;i++){\n\t\tfor(int j=0;j<c;j++){\n\t\t\t//printf(\"%d \",dp[i][j]);\n\t\t}\n\t\t//printf(\"\\n\");\n\t}\n\tprintf(\"%s\\n\",dp[r-1][c-1]==1?\"Chieno\":\"Cacao\");\n\treturn 0;\n}", "accuracy": 0.375, "time_ms": 110, "memory_kb": 34672, "score_of_the_acc": -0.542, "final_rank": 10 }, { "submission_id": "aoj_1559_1256642", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nint main()\n{\n int r, c, k;\n cin >> r >> c >> k;\n vector<string> g(r, string(c, '#'));\n for(int y=0; y<r; ++y){\n for(int x=0; x<c; ++x){\n cin >> g[y][x];\n }\n }\n\n ++ k;\n vector<vector<bool> > dp(r, vector<bool>(c, false));\n vector<int> add(c);\n deque<int> sub(r+c);\n for(int y=0; y<r; ++y){\n int sum = 0;\n for(int x=0; x<c; ++x){\n if(0 <= y - k && g[y-k][x] == '.' && !dp[y-k][x]){\n -- add[x];\n -- sub[y+x-k];\n }\n\n sum += add[x];\n if(0 <= y + x - k)\n sum -= sub[y+x-k];\n\n if(g[y][x] == '.'){\n if(sum > 0){\n dp[y][x] = true;\n }\n else{\n ++ add[x];\n ++ sub[y+x];\n ++ sum;\n }\n }\n }\n }\n\n if(dp[r-1][c-1])\n cout << \"Chieno\" << endl;\n else\n cout << \"Cacao\" << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 2408, "score_of_the_acc": -0.2017, "final_rank": 6 }, { "submission_id": "aoj_1559_1249234", "code_snippet": "#include <string>\n#include <vector>\n#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<list>\n#include<deque>\n#include<bitset>\n#include<set>\n#include<map>\n#include<unordered_map>\n#include<cstring>\n#include<sstream>\n#include<complex>\n#include<iomanip>\n#include<numeric>\n#define X first\n#define Y second\n#define pb push_back\n#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))\n#define rrep(X,Y) for (int (X) = (Y-1);(X) >=0;--(X))\n#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))\n#define peat(X,Y) for (;(X) < (Y);++(X))\n#define all(X) (X).begin(),(X).end()\n#define rall(X) (X).rbegin(),(X).rend()\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n//template<class T> using vv=vector<vector<T>>;\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"{\"; rep(i,t.size()) {os<<t[i]<<\",\";} os<<\"}\"<<endl; return os;}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\n\nint sum[1123][1123],sumd[1123][1123],lose[1123][1123],n,m,t;\n\nint Sum(int y,int x){\n int re=sum[y][x];\n //cout<<pii(x,y)<<endl;\n if(x-t-2>=0)\n re+=sumd[y+1][x-t-2];\n //cout<<re<<\",\";\n if(y-t>=0){\n if(y-t)re-=sum[y-t-1][x];\n if(x)re-=sumd[y-t][x-1];\n }else{\n if(x-t+y)re-=sumd[0][x-t+y-1];\n }\n //cout<<re<<endl;\n return re;\n}\n\nint main(){\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(0);\n int i,j,k;\n cin>>n>>m>>t;\n vector<string> mp(n);\n rep(i,n)\n cin>>mp[i];\n rep(j,m)rep(i,n){\n if(Sum(i,j)==0&&mp[i][j]=='.'){\n lose[i][j]=1;\n ++sum[i][j];\n ++sumd[i][j];\n }\n sumd[i][j+1]+=sumd[i][j];\n if(i){\n sumd[i-1][j+1]+=sumd[i][j];\n sumd[i-1][j+2]-=sumd[i][j];\n }\n sum[i+1][j]+=sum[i][j];\n sum[i][j+1]+=sum[i][j];\n sum[i+1][j+1]-=sum[i][j];\n }\n //rep(i,n){rep(j,m)cout<<lose[i][j];cout<<endl;}\n //rep(i,n){rep(j,m)cout<<sum[i][j]<<\",\";cout<<endl;}\n //rep(i,n){rep(j,m)cout<<sumd[i][j]<<\",\";cout<<endl;}\n cout<<(lose[n-1][m-1]?\"Cacao\":\"Chieno\")<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 16428, "score_of_the_acc": -0.1027, "final_rank": 4 }, { "submission_id": "aoj_1559_1249025", "code_snippet": "#include <string>\n#include <vector>\n#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<list>\n#include<deque>\n#include<bitset>\n#include<set>\n#include<map>\n#include<unordered_map>\n#include<cstring>\n#include<sstream>\n#include<complex>\n#include<iomanip>\n#include<numeric>\n#define X first\n#define Y second\n#define pb push_back\n#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))\n#define rrep(X,Y) for (int (X) = (Y-1);(X) >=0;--(X))\n#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))\n#define peat(X,Y) for (;(X) < (Y);++(X))\n#define all(X) (X).begin(),(X).end()\n#define rall(X) (X).rbegin(),(X).rend()\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n//template<class T> using vv=vector<vector<T>>;\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"{\"; rep(i,t.size()) {os<<t[i]<<\",\";} os<<\"}\"<<endl; return os;}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\n\nint sum[1123][1123],sumd[1123][1123],lose[1123][1123],n,m,t;\n\nint Sum(int y,int x){\n int re=sum[y][x];\n //cout<<pii(x,y)<<endl;\n if(x-t-2>=0)\n re+=sumd[y+1][x-2];\n //cout<<re<<\",\";\n if(y-t>=0){\n if(y-t)re-=sum[y-t-1][x];\n if(x)re-=sumd[y-t][x-1];\n }else{\n if(x-t+y)re-=sumd[0][x-t+y-1];\n }\n //cout<<re<<endl;\n return re;\n}\n\nint main(){\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(0);\n int i,j,k;\n cin>>n>>m>>t;\n vector<string> mp(n);\n rep(i,n)\n cin>>mp[i];\n rep(j,m)rep(i,n){\n if(Sum(i,j)==0&&mp[i][j]=='.'){\n lose[i][j]=1;\n ++sum[i][j];\n ++sumd[i][j];\n }\n sumd[i][j+1]+=sumd[i][j];\n if(i){\n sumd[i-1][j+1]+=sumd[i][j];\n sumd[i-1][j+2]-=sumd[i][j];\n }\n sum[i+1][j]+=sum[i][j];\n sum[i][j+1]+=sum[i][j];\n sum[i+1][j+1]-=sum[i][j];\n }\n //rep(i,n){rep(j,m)cout<<lose[i][j];cout<<endl;}\n //rep(i,n){rep(j,m)cout<<sum[i][j]<<\",\";cout<<endl;}\n //rep(i,n){rep(j,m)cout<<sumd[i][j]<<\",\";cout<<endl;}\n cout<<(lose[n-1][m-1]?\"Cacao\":\"Chieno\")<<endl;\n return 0;\n}", "accuracy": 0.28125, "time_ms": 20, "memory_kb": 16428, "score_of_the_acc": -0.1027, "final_rank": 12 }, { "submission_id": "aoj_1559_1248872", "code_snippet": "#include<stdio.h>\n#include<algorithm>\nusing namespace std;\n#define SZ 1024\nchar p[1010][1010];\nint n, m, K, IT[SZ + SZ + 1];\nint Max(int b, int e){\n\tint r = -9999999;\n\tb += SZ, e += SZ;\n\twhile (b <= e){\n\t\tr = max(r, IT[b]);\n\t\tr = max(r, IT[e]);\n\t\tb = (b + 1) >> 1;\n\t\te = (e - 1) >> 1;\n\t}\n\treturn r;\n}\nvoid Add(int a, int b){\n\ta += SZ;\n\twhile (a){\n\t\tIT[a] = max(IT[a], b);\n\t\ta >>= 1;\n\t}\n}\nint main(){\n\tint i, j, ck;\n\tscanf(\"%d%d%d\", &n, &m, &K);\n\tfor (i = 1; i <= n; i++){\n\t\tscanf(\"%s\", p[i] + 1);\n\t}\n\tfor (i = 1; i < SZ * 2; i++)IT[i] = -9999999;\n\tfor (i = 1; i <= n; i++){\n\t\tfor (j = 1; j <= m; j++){\n\t\t\tck = 0;\n\t\t\tif (p[i][j] != '.')continue;\n\t\t\tif (i + j - Max(1, j) > K){\n\t\t\t\tck = 1;\n\t\t\t\tAdd(j, i + j);\n\t\t\t}\n\t\t}\n\t}\n\tif (ck == 0)printf(\"Chieno\\n\");\n\telse printf(\"Cacao\\n\");\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2008, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1559_1248836", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef pair<string,ut> ps;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\nconst int SIZE=1001;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nint isWin[SIZE][SIZE];\nint main(){\n\tint R,C,K;\n\tchar c;\n\tcin >>R >> C >> K;\n\tREP(i,R)\n\t\tREP(j,C){\n\t\t\tscanf(\" %c\",&c);\n\t\t\tif(c=='#')\n\t\t\t\tisWin[i][j]=1;\n\t\t}\n\tREP(i,R)\n\t\tREP(j,C){\n\t\t\tif(isWin[i][j]){\n\t\t\t\tisWin[i+1][j]=max(isWin[i+1][j],isWin[i][j]-1);\n\t\t\t\tisWin[i][j+1]=max(isWin[i][j+1],isWin[i][j]-1);\n\t\t\t}\n\t\t\telse{\n\t\t\t\tisWin[i+1][j]=max(isWin[i+1][j],K);\n\t\t\t\tisWin[i][j+1]=max(isWin[i][j+1],K);\n\t\t\t}\n\t\t}\n\tif(isWin[R-1][C-1])\n\t\tcout <<\"Chieno\" << endl;\n\telse\n\t\tcout <<\"Cacao\" << endl;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5100, "score_of_the_acc": -0.1334, "final_rank": 5 } ]
aoj_1555_cpp
Problem E: Distinct Dictionary Background 辞書をこよなく愛する者がいた。彼らは自分だけの辞書を作ることが大好きである。 そこであなたは彼らの辞書に、自由にカスタマイズする機能を付け加えてあげることにした。 Problem 初めに、何の単語も入っていない空の辞書が存在する。 N 個の文字列 S id と Q 個のクエリが与えられる。 各クエリはクエリの種類 k と文字列を指す id で与えられる。 クエリの種類は以下の3つである。 k =1のとき S id を辞書に追加する。 k =2のとき S id を辞書から削除する。 k =3のとき辞書に追加された文字列の中で S id を先頭からの部分文字列に含み且つ辞書順最小の文字列の id を出力する。無い場合は-1を出力する。 各クエリに答えるプログラムを書いてほしい。 注意: 入力のサイズが大きいので高速な入力に対応する形式を推奨する。 Input 入力は以下の形式で与えられる。 N S 1 S 2 . . . S N Q k 1 id 1 k 2 id 2 . . . k Q id Q 1行目に、1つの整数 N が与えられる。2行目から N +1行に、 N 個の文字列 S id が与えられる。 N +2行目にクエリの数 Q が与えられ、続く Q 行に各クエリの種類を表す k と文字列を指す id が与えれる。 Constraints 文字列は全て異なる。 N 個の文字列 S id の長さの合計は10 6 を超えない。 既に辞書に追加されている文字列が再び追加されるような入力は存在しない。 辞書に追加されていない文字列を削除するような入力は存在しない。 与えられる文字列に含まれる文字は英小文字のみである。 1 ≤ N ≤ 10 5 1 ≤ id ≤ N 1 ≤ Q ≤ 10 5 1 ≤ | S id | ≤ 10 5 (ただし、| s |は文字列 s の長さを表す。) Output クエリごとに解答を一行に出力せよ。 Sample Input1 3 apple app banana 9 1 1 3 1 3 2 3 3 2 1 1 2 3 1 3 2 3 3 Sample Output1 1 1 -1 -1 2 -1 Sample Input2 7 aaaab aaa aaaa aaaaaabc abbbbbc ab abbb 13 1 1 3 2 1 4 3 2 1 3 3 2 2 3 3 3 1 5 1 7 3 6 2 5 3 6 Sample Output2 1 4 3 4 7 7
[ { "submission_id": "aoj_1555_5550778", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param n `0 <= n`\n// @return minimum non-negative `x` s.t. `n <= 2**x`\nint ceil_pow2(int n) {\n int x = 0;\n while ((1U << x) < (unsigned int)(n)) x++;\n return x;\n}\n\n// @param n `1 <= n`\n// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`\nint bsf(unsigned int n) {\n#ifdef _MSC_VER\n unsigned long index;\n _BitScanForward(&index, n);\n return index;\n#else\n return __builtin_ctz(n);\n#endif\n}\n\n} // namespace internal\n\n} // namespace atcoder\n\nnamespace atcoder {\n\ntemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n public:\n segtree() : segtree(0) {}\n explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}\n explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {\n log = internal::ceil_pow2(_n);\n size = 1 << log;\n d = std::vector<S>(2 * size, e());\n for (int i = 0; i < _n; i++) d[size + i] = v[i];\n for (int i = size - 1; i >= 1; i--) {\n update(i);\n }\n }\n\n void set(int p, S x) {\n assert(0 <= p && p < _n);\n p += size;\n d[p] = x;\n for (int i = 1; i <= log; i++) update(p >> i);\n }\n\n S get(int p) const {\n assert(0 <= p && p < _n);\n return d[p + size];\n }\n\n S prod(int l, int r) const {\n assert(0 <= l && l <= r && r <= _n);\n S sml = e(), smr = e();\n l += size;\n r += size;\n\n while (l < r) {\n if (l & 1) sml = op(sml, d[l++]);\n if (r & 1) smr = op(d[--r], smr);\n l >>= 1;\n r >>= 1;\n }\n return op(sml, smr);\n }\n\n S all_prod() const { return d[1]; }\n\n template <bool (*f)(S)> int max_right(int l) const {\n return max_right(l, [](S x) { return f(x); });\n }\n template <class F> int max_right(int l, F f) const {\n assert(0 <= l && l <= _n);\n assert(f(e()));\n if (l == _n) return _n;\n l += size;\n S sm = e();\n do {\n while (l % 2 == 0) l >>= 1;\n if (!f(op(sm, d[l]))) {\n while (l < size) {\n l = (2 * l);\n if (f(op(sm, d[l]))) {\n sm = op(sm, d[l]);\n l++;\n }\n }\n return l - size;\n }\n sm = op(sm, d[l]);\n l++;\n } while ((l & -l) != l);\n return _n;\n }\n\n template <bool (*f)(S)> int min_left(int r) const {\n return min_left(r, [](S x) { return f(x); });\n }\n template <class F> int min_left(int r, F f) const {\n assert(0 <= r && r <= _n);\n assert(f(e()));\n if (r == 0) return 0;\n r += size;\n S sm = e();\n do {\n r--;\n while (r > 1 && (r % 2)) r >>= 1;\n if (!f(op(d[r], sm))) {\n while (r < size) {\n r = (2 * r + 1);\n if (f(op(d[r], sm))) {\n sm = op(d[r], sm);\n r--;\n }\n }\n return r + 1 - size;\n }\n sm = op(d[r], sm);\n } while ((r & -r) != r);\n return 0;\n }\n\n private:\n int _n, size, log;\n std::vector<S> d;\n\n void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\n\nconstexpr char newl = '\\n';\n\n// recommend { MOD:2^61-1, base:random }\nstruct RollingHash{\n using i128 = __int128_t;\n using ll = long long;\n ll base,mod;\n vector<ll> hash,cumulative_sum,inv;\n RollingHash(vector<ll> vals,ll B,ll MOD = (1LL<<61)-1){\n set_base(B);\n set_mod(MOD);\n build(vals);\n }\n RollingHash(string &s,ll B,ll MOD = (1LL<<61)-1){\n vector<ll> vals;\n for(char c:s) vals.emplace_back(c);\n set_base(B);\n set_mod(MOD);\n build(vals);\n }\n\tRollingHash(){}\n\n\tvoid init(string &s,ll B,ll MOD = (1LL<<61)-1){\n vector<ll> vals;\n for(char c:s) vals.emplace_back(c);\n set_base(B);\n set_mod(MOD);\n build(vals);\n\t}\n\n void set_base(ll B){ base=B; }\n void set_mod(ll MOD){ mod=MOD; }\n\n // mod multiprecation\n ll mod_mul(ll a,ll b){\n i128 res=a;\n res*=b;\n res=(res >> 61) + (res & mod);\n if(res >= mod) res-=mod;\n return (ll)res;\n }\n\n ll pow(ll a,i128 e){\n if(e==0) return 1;\n if(e%2==0){\n ll res=pow(a,e/2);\n return mod_mul(res,res);\n }\n return mod_mul(pow(a,e-1),a);\n }\n\n void build(vector<ll> vals){\n int n=vals.size();\n hash.assign(n,0);\n cumulative_sum.assign(n+1,0);\n inv.assign(n+1,0);\n i128 e=mod-2;\n inv[n]=pow(base,n*e);\n for(int i=n-1;i>=0;i--) inv[i]=mod_mul(inv[i+1],base);\n for(int i=0;i<n;i++) hash[i]=mod_mul(vals[i],pow(base,i));\n for(int i=0;i<n;i++) cumulative_sum[i+1]=(hash[i]+cumulative_sum[i])%mod;\n }\n\n // [l,r)\n long long find(int l,int r){\n ll res=cumulative_sum[r]-cumulative_sum[l];\n if(res<0) res+=mod;\n res=mod_mul(res,inv[l]);\n return (long long)res;\n }\n};\n\ntemplate<typename T>\nstruct RandomGenerator {\n mt19937 mt;\n RandomGenerator() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}\n\n // [a, b)\n T operator()(T a, T b) {\n uniform_int_distribution<T> dist(a, b - 1);\n return dist(mt);\n }\n\n // [0, b)\n T operator()(T b) { return (*this)(0,b); }\n};\n\n\n\nint op(int a,int b){\n\treturn min(a,b);\n}\n\nint e(){\n\treturn INT32_MAX/2;\n}\n\n\nint main(){\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tint n;\n\tcin>>n;\n\tvector<string> s(n);\n\tfor(int i=0;i<n;i++){\n\t\tcin>>s[i];\n\t}\n\n\tauto sorted=s;\n\tsort(sorted.begin(),sorted.end());\n\n\tvector<pair<string,int>> sorted_pair(n);\n\tfor(int i=0;i<n;i++){\n\t\tsorted_pair[i].first=s[i];\n\t\tsorted_pair[i].second=i;\n\t}\n\n\tsort(sorted_pair.begin(),sorted_pair.end());\n\t\n\n\n\tint q;\n\tcin>>q;\n\tvector<int> k(q),id(q);\n\tfor(int i=0;i<q;i++){\n\t\tcin>>k[i]>>id[i];\n\t}\n\n RandomGenerator<ll> rand;\n const ll base=rand(2LL,(1LL<<61)-1-2);\n\tvector<RollingHash> hashs(n);\n\tfor(int i=0;i<n;i++){\n\t\thashs[i].init(sorted[i],base);\n\t}\n\t// ここでソート済みの文字列のハッシュ構築済み\n\n\n\n\tsegtree<int,op,e> tree(vector<int>(n,e()));\n\n\n\t\n\tfor(int i=0;i<q;i++){\n\t\tif(k[i]==1){\n\t\t\tint sorted_idx=lower_bound(sorted.begin(),sorted.end(),s[id[i]-1])-sorted.begin();\n\t\t\ttree.set(sorted_idx,sorted_idx);\n\t\t\tcontinue;\n\t\t}\n\t\tif(k[i]==2){\n\t\t\tint sorted_idx=lower_bound(sorted.begin(),sorted.end(),s[id[i]-1])-sorted.begin();\n\t\t\ttree.set(sorted_idx,e());\n\t\t\tcontinue;\n\t\t}\n\n\t\t// ここで欲しい文字列のインデックス取得をする\n\t\tint sorted_idx=lower_bound(sorted.begin(),sorted.end(),s[id[i]-1])-sorted.begin();\n\t\tint idx=tree.prod(sorted_idx,n);\n\n\n\t\tif(idx>=n){\n\t\t\tcout<<-1<<newl;\n\t\t\tcontinue;\n\t\t}\n\n\t\t// s[sorted_idx]がs[idx]に含まれているかを判定する\n\t\tint sz1=sorted[sorted_idx].size();\n\t\tint sz2=sorted[idx].size();\n\t\tif(sz1>sz2){\n\t\t\tcout<<-1<<newl;\n\t\t\tcontinue;\n\t\t}\n\n\t\t// 検索する文字列の長さ分判定する\n\t\tif(hashs[sorted_idx].find(0,sz1)==hashs[idx].find(0,sz1)){\n\t\t\tcout<<sorted_pair[idx].second+1<<newl;\n\t\t}else{\n\t\t\tcout<<-1<<newl;\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 1000, "memory_kb": 52148, "score_of_the_acc": -1.2061, "final_rank": 14 }, { "submission_id": "aoj_1555_4542209", "code_snippet": "#include <iostream>\n#include <set>\n#include <map>\n#include <string>\nusing namespace std;\nusing llong = long long;\n\nllong n, q;\nstring s[100005];\nllong com, id;\n\nauto cmp = [&](int i, int j) {\n return s[i] < s[j];\n};\n\nset<int, decltype(cmp)> st(cmp);\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n cin >> n;\n for (int i = 1; i <= n; i++) {\n cin >> s[i];\n }\n\n cin >> q;\n for (int i = 0; i < q; i++) {\n cin >> com >> id;\n\n switch (com) {\n case 1:\n st.insert(id);\n break;\n case 2:\n st.erase(id);\n break;\n case 3:\n auto itr = st.lower_bound(id);\n if (itr == st.end()) cout << -1 << '\\n';\n else cout << *itr << '\\n';\n break;\n }\n }\n}", "accuracy": 0.047619047619047616, "time_ms": 20, "memory_kb": 8336, "score_of_the_acc": 0, "final_rank": 17 }, { "submission_id": "aoj_1555_4542188", "code_snippet": "#include <iostream>\n#include <set>\n#include <map>\n#include <string>\nusing namespace std;\nusing llong = long long;\n\nllong n, q;\nstring s[100005];\nllong com, id;\nset<string> st;\nmap<string, int> mp;\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> s[i];\n mp[s[i]] = i + 1;\n }\n\n cin >> q;\n for (int i = 0; i < q; i++) {\n cin >> com >> id;\n id--;\n\n switch (com) {\n case 1:\n st.insert(s[id]);\n break;\n case 2:\n st.erase(s[id]);\n break;\n case 3:\n auto itr = st.lower_bound(s[id]);\n if (itr == st.end()) cout << -1 << '\\n';\n else cout << mp[*itr] << '\\n';\n break;\n }\n }\n}", "accuracy": 0.047619047619047616, "time_ms": 50, "memory_kb": 13696, "score_of_the_acc": -0.0558, "final_rank": 18 }, { "submission_id": "aoj_1555_4542117", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\n#define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)\n#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)\n\n#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)\n#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)\n\n#define debug(x) cerr << \"(\" << __LINE__ << \")\" << #x << \": \" << (x) << endl\n\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n// constexpr i32 mod = 998244353;\nconstexpr i32 mod = 1e9 + 7;\nconstexpr i32 inf = 1001001001;\nconstexpr i64 infll = 1001001001001001001ll;\n\nconstexpr int dx[] = {0, -1, 1, 0, -1, 1, -1, 1};\nconstexpr int dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\nstruct IoSetup { IoSetup(int x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;\n\ntemplate <typename T = i64> T input() { T x; cin >> x; return x; }\n\ntemplate <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? \" \" : \"\"); } return os; }\ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }\n\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\ntemplate <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }\n\ntemplate <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }\ntemplate <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n// }}}\n\ntemplate< unsigned mod >\nstruct RollingHash {\n vector< unsigned > hashed, power;\n\n inline unsigned mul(unsigned a, unsigned b) const {\n unsigned long long x = (unsigned long long) a * b;\n unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;\n asm(\"divl %4; \\n\\t\" : \"=a\" (d), \"=d\" (m) : \"d\" (xh), \"a\" (xl), \"r\" (mod));\n return m;\n }\n\n RollingHash(const string &s, unsigned base = 10007) {\n int sz = (int) s.size();\n hashed.assign(sz + 1, 0);\n power.assign(sz + 1, 0);\n power[0] = 1;\n for(int i = 0; i < sz; i++) {\n power[i + 1] = mul(power[i], base);\n hashed[i + 1] = mul(hashed[i], base) + s[i];\n if(hashed[i + 1] >= mod) hashed[i + 1] -= mod;\n }\n }\n\n unsigned get(int l, int r) const {\n unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);\n if(ret >= mod) ret -= mod;\n return ret;\n }\n\n unsigned connect(unsigned h1, int h2, int h2len) const {\n unsigned ret = mul(h1, power[h2len]) + h2;\n if(ret >= mod) ret -= mod;\n return ret;\n }\n\n int LCP(const RollingHash< mod > &b, int l1, int r1, int l2, int r2) {\n int len = min(r1 - l1, r2 - l2);\n int low = -1, high = len + 1;\n while(high - low > 1) {\n int mid = (low + high) / 2;\n if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) low = mid;\n else high = mid;\n }\n return (low);\n }\n};\n\nusing RH = RollingHash< 1000000007 >;\n\nvoid solver() {\n int n;\n cin >> n;\n\n vector< string > ss(n);\n vector< pair< string, int > > ps(n);\n range(i, 0, n) {\n cin >> ss[i];\n ps[i] = make_pair(ss[i], i);\n }\n\n whole(sort, ps);\n\n vector< int > tos(n);\n range(i, 0, n) {\n tos[ps[i].second] = i;\n }\n\n vector< RH > rs;\n range(i, 0, n) {\n rs.emplace_back(RH(ps[i].first));\n }\n\n\n set< int > st;\n st.insert(n);\n int q = input();\n\n while (q--) {\n int k, id;\n cin >> k >> id;\n\n id = tos[id - 1];\n\n if (k == 1) {\n st.insert(id);\n }\n\n if (k == 2) {\n st.erase(id);\n }\n\n if (k == 3) {\n int idx = *st.lower_bound(id);\n \n if (idx == n) {\n cout << -1 << endl;\n continue;\n }\n\n int r = ps[id].first.size();\n if (rs[idx].hashed.size() >= rs[id].hashed.size() && \n rs[idx].get(0, r) == rs[id].get(0, r)) {\n cout << ps[idx].second + 1 << endl;\n } else {\n cout << -1 << endl;\n }\n }\n }\n}\n\nsigned main(int argc, char *argv[]) {\n solver();\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 29176, "score_of_the_acc": -0.2001, "final_rank": 4 }, { "submission_id": "aoj_1555_4542103", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nstruct segment_tree{\n int N;\n vector<int> ST;\n segment_tree(int n){\n N = 1;\n while (N < n){\n N *= 2;\n }\n ST = vector<int>(N * 2 - 1, 0);\n }\n void update(int i, int x){\n i += N - 1;\n ST[i] = x;\n while (i > 0){\n i = (i - 1) / 2;\n ST[i] = ST[i * 2 + 1] + ST[i * 2 + 2];\n }\n }\n int query(int L, int R, int i, int l, int r){\n if (r <= L || R <= l){\n return 0;\n } else if (L <= l && r <= R){\n return ST[i];\n } else {\n int m = (l + r) / 2;\n return query(L, R, i * 2 + 1, l, m) + query(L, R, i * 2 + 2, m, r);\n }\n }\n int query(int L, int R){\n return query(L, R, 0, 0, N);\n }\n};\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int N;\n cin >> N;\n vector<pair<string, int>> S(N);\n for (int i = 0; i < N; i++){\n cin >> S[i].first;\n S[i].second = i + 1;\n }\n sort(S.begin(), S.end());\n //id -> pos\n vector<int> pos(N + 1, -1);\n for (int i = 0; i < N; i++){\n pos[S[i].second] = i;\n }\n //string -> position\n map<string, int> mp;\n for (int i = 0; i < N; i++){\n mp[S[i].first] = i;\n }\n //Find range\n vector<int> R(N);\n for (int i = 0; i < N; i++){\n string T = S[i].first;\n T.back()++;\n auto itr = mp.lower_bound(T);\n itr--;\n R[i] = (*itr).second + 1;\n }\n //debug\n /*\n for (int i = 0; i < N; i++){\n cout << R[i] << endl;\n }\n */\n //point update range sum\n segment_tree ST(N);\n //queries\n int Q;\n cin >> Q;\n for (int i = 0; i < Q; i++){\n int k, id;\n cin >> k >> id;\n int p = pos[id];\n //cout << \"pos = \" << p << endl;\n if (k == 1){\n ST.update(p, 1);\n }\n if (k == 2){\n ST.update(p, 0);\n }\n if (k == 3){\n if (ST.query(p, R[p]) == 0){\n cout << -1 << endl;\n } else {\n //binary search\n int tv = R[p];\n int fv = p - 1;\n while (tv - fv > 1){\n int mv = (tv + fv) / 2;\n if (ST.query(p, mv) != 0){\n tv = mv;\n } else {\n fv = mv;\n }\n }\n cout << S[fv].second << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 17164, "score_of_the_acc": -0.1538, "final_rank": 3 }, { "submission_id": "aoj_1555_4244482", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 100005\n\nstruct Data{\n\n\tbool operator<(const struct Data &arg) const{\n\n\t\treturn line < arg.line;\n\t}\n\tint index;\n\tstring line;\n};\n\nstruct Node{\n\tint children[26];\n\tbool finish_FLG;\n};\n\n\nint N;\nint length[SIZE];\nint L[SIZE],R[SIZE];\nint num_node;\nmap<int,int> MAP,rev_MAP,TAIL;\nset<int> SET;\nData data[SIZE];\nNode nodes[1000005];\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tcin >> data[i].line;\n\t\tdata[i].index = i;\n\t\tlength[i] = data[i].line.length();\n\t}\n\n\tsort(data,data+N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tMAP[data[i].index] = i; //文字列がdataのどこにあるか\n\t\trev_MAP[i] = data[i].index; //dataのi番目の文字列は何か\n\n\t\tL[i] = BIG_NUM; //自分を含む最小のindex\n\t\tR[i] = -BIG_NUM; //自分を含む細大のindex\n\t}\n\n\tnum_node = 0;\n\n\tfor(int i = 0; i < 1000005; i++){\n\n\t\tfor(int k = 0; k < 26; k++){\n\n\t\t\tnodes[i].children[k] = -1;\n\t\t}\n\t\tnodes[i].finish_FLG = false;\n\t}\n\n\tint root = 0;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tint tmp_index = 0; //文字列のインデックス\n\t\tint tmp_ch = data[i].line[tmp_index]-'a';\n\n\t\tint tmp_loc = root; //トライ木上の位置\n\t\ttmp_index = 0;\n\n\t\twhile(true){\n\n\t\t\tif(nodes[tmp_loc].finish_FLG){ //終端する文字列あり\n\n\t\t\t\tint k = TAIL[tmp_loc];\n\t\t\t\tR[k] = i;\n\t\t\t}\n\n\t\t\tif(nodes[tmp_loc].children[tmp_ch] == -1){\n\n\t\t\t\tnodes[tmp_loc].children[tmp_ch] = ++num_node;\n\t\t\t}\n\t\t\t//子ノードに移動\n\t\t\ttmp_loc = nodes[tmp_loc].children[tmp_ch];\n\t\t\ttmp_index++;\n\n\t\t\tif(tmp_index == length[rev_MAP[i]]){ //今回の数字が最後の文字だった場合\n\t\t\t\tnodes[tmp_loc].finish_FLG = true;\n\t\t\t\tTAIL[tmp_loc] = rev_MAP[i];\n\t\t\t\tL[rev_MAP[i]] = i;\n\t\t\t\tR[rev_MAP[i]] = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\ttmp_ch = data[i].line[tmp_index]-'a';\n\t\t}\n\t}\n\n\tint num_query;\n\tscanf(\"%d\",&num_query);\n\n\tint command,num;\n\n\tset<int>::iterator it;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d %d\",&command,&num);\n\t\tnum--;\n\n\t\tswitch(command){\n\t\tcase 1:\n\n\t\t\tSET.insert(MAP[num]);\n\t\t\tbreak;\n\n\t\tcase 2:\n\n\t\t\tSET.erase(MAP[num]);\n\t\t\tbreak;\n\n\t\tcase 3:\n\n\t\t\tif(L[num] == BIG_NUM){ //元々自分を含む文字列なし\n\n\t\t\t\tprintf(\"-1\\n\");\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tit = SET.lower_bound(L[num]);\n\n\t\t\tif (it == SET.end() || *it > R[num]){\n\n\t\t\t\tprintf(\"-1\\n\");\n\n\t\t\t}else{\n\n\t\t\t\tprintf(\"%d\\n\", rev_MAP[*it]+1);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 133356, "score_of_the_acc": -0.8738, "final_rank": 13 }, { "submission_id": "aoj_1555_3560675", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<string,int> P;\n\nstruct rollinghash{\n\tstatic const int MD=3;\n\tconst ll base[MD]={1007LL,1009LL,1009LL};\n\tconst ll mods[MD]={999999937LL, 1000000007LL,912941443LL};\n\tvector<ll> hash[MD];\n\tvector<ll> pw[MD];\n\tvoid construct(const string &str){\n\t\tint n=str.size();\n\t\tfor(int i=0;i<MD;i++){\n\t\t\tpw[i].resize(n+1);\n\t\t\tpw[i][0]=1;\n\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\tpw[i][j+1]=pw[i][j]*base[i]%mods[i];\n\t\t\t}\n\t\t}\n\t\tfor(int i=0;i<MD;i++){\n\t\t\thash[i].resize(n+1);\n\t\t\thash[i][0]=0;\n\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\thash[i][j+1]=(hash[i][j]*base[i]%mods[i]+str[j])%mods[i];\n\t\t\t}\n\t\t}\n\t}\n\n\t// calc hash value [l,r)\n\tll hash_value(int l,int r,int i){\n\t\treturn ((hash[i][r]-hash[i][l]*pw[i][r-l])%mods[i]+mods[i])%mods[i];\n\t}\n};\n\nrollinghash rol[100005];\n\nint n;\nconst int B=300;\nP p[100005];\nint rid[100005];\nint cnt[100005];\nint Bcnt[500];\n\nbool isok(int s,int t){\n\t//printf(\"isok%d %d\\n\",s,t);\n\tint len=p[s].first.size();\n\tfor(int i=0;i<3;i++){\n\t\tif(rol[s].hash_value(0,len,i)!=rol[t].hash_value(0,len,i))return false;\n\t}\n\treturn true;\n}\n\nint query(int s){\n\tint v=s;\n\twhile(v%B!=0 && v<n){\n\t\tif(cnt[v]==1){\n\t\t\tif(p[s].first.size()>p[v].first.size())return -1;\n\t\t\tif(isok(s,v)){\n\t\t\t\treturn (p[v].second+1);\n\t\t\t}else{\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t}\n\t\tv++;\n\t}\n\twhile(v<n){\n\t\tif(Bcnt[v/B]>0)break;\n\t\tv+=B;\n\t}\n\twhile(v<n){\n\t\tif(cnt[v]==1){\n\t\t\tif(p[s].first.size()>p[v].first.size())return -1;\n\t\t\tif(isok(s,v)){\n\t\t\t\treturn (p[v].second+1);\n\t\t\t}else{\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t}\n\t\tv++;\n\t}\n\treturn -1;\n}\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tfor(int i=0;i<n;i++){\n\t\tcin >> p[i].first;\n\t\tp[i].second=i;\n\t}\n\tsort(p,p+n);\n\tfor(int i=0;i<n;i++){\n\t\trol[i].construct(p[i].first);\n\t\trid[p[i].second]=i;\n\t}\n\tint q;\n\tscanf(\"%d\",&q);\n\tfor(int i=0;i<q;i++){\n\t\tint a,b;\n\t\tscanf(\"%d%d\",&a,&b);\n\t\tb--;\n\t\tif(a==1){\n\t\t\tcnt[rid[b]]++;\n\t\t\tBcnt[rid[b]/B]++;\n\t\t}\n\t\tif(a==2){\n\t\t\tcnt[rid[b]]--;\n\t\t\tBcnt[rid[b]/B]--;\n\t\t}\n\t\tif(a==3){\n\t\t\tprintf(\"%d\\n\",query(rid[b]));\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 86908, "score_of_the_acc": -0.5941, "final_rank": 11 }, { "submission_id": "aoj_1555_3560650", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct RollingHash{\n static const int MD = 2;\n static const vector<ll> hash_base, hash_mod;\n\n int n;\n vector<ll> hs[MD], pw[MD];\n\n RollingHash(){}\n RollingHash(const string &s){\n n = s.size();\n rep(i,MD){\n hs[i].assign(n+1,0);\n pw[i].assign(n+1,0);\n hs[i][0] = 0;\n pw[i][0] = 1;\n rep(j,n){\n pw[i][j+1] = pw[i][j]*hash_base[i] % hash_mod[i];\n hs[i][j+1] = (hs[i][j]*hash_base[i]+s[j]) % hash_mod[i];\n }\n }\n }\n\n // 1-index\n ll hash_value(int l, int r, int i){\n return ((hs[i][r] - hs[i][l]*pw[i][r-l])%hash_mod[i]+hash_mod[i])%hash_mod[i];\n }\n\n bool match(int l1, int r1, int l2, int r2){\n bool ret = true;\n rep(i,MD) ret &= (hash_value(l1-1,r1,i) == hash_value(l2-1,r2,i));\n return ret;\n }\n\n ll calc(int l, int r){\n ll ret = 0;\n rep(i,MD) ret |= (hash_value(l-1,r,i)<<(i*32));\n return ret;\n }\n};\nconst vector<ll> RollingHash::hash_base{1009,1021};\nconst vector<ll> RollingHash::hash_mod{1000000009,1000000007};\n\n\n\nusing P = pair<string,int>;\n\nint main(){\n int n;\n scanf(\" %d\", &n);\n\n vector<P> s(n);\n rep(i,n){\n char tt[100010];\n scanf(\" %s\", tt);\n s[i] = {string(tt), i};\n }\n\n sort(all(s));\n\n vector<int> f(n),sz(n);\n rep(i,n){\n f[s[i].se] = i;\n sz[i] = s[i].fi.size();\n }\n\n vector<RollingHash> h(n);\n rep(i,n) h[i] = RollingHash(s[i].fi);\n\n auto check = [&](int a, int b){\n if(sz[a] > sz[b]) return false;\n\n ll va = h[a].calc(1,sz[a]);\n ll vb = h[b].calc(1,sz[a]);\n return va == vb;\n };\n\n set<int> active;\n int Q;\n scanf(\" %d\", &Q);\n while(Q--){\n int k,id;\n scanf(\" %d %d\", &k, &id);\n --id;\n\n if(k == 1) active.insert(f[id]);\n else if(k == 2) active.erase(f[id]);\n else{\n int ans = -1;\n auto itr = active.lower_bound(f[id]);\n if(itr != active.end()){\n int idx = *itr;\n if(check(f[id], idx)) ans = s[idx].se+1;\n }\n printf(\"%d\\n\", ans);\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 59236, "score_of_the_acc": -0.3517, "final_rank": 8 }, { "submission_id": "aoj_1555_3560604", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef pair<int,int> P;\ntypedef long long ll;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);(i)++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n\n#define INF INT_MAX/3\n\nstruct RangeMinQuery{\n int dat[(1<<19)-1];\n int size;\n\n void init(int n_){\n size=1;\n while(size<n_)size*=2;\n for(int i=0;i<2*size-1;i++)dat[i]=INF;\n }\n\n void update(int k,int a){\n k+=size-1;\n dat[k]=a;\n while(k>0){\n k=(k-1)/2;\n dat[k]=min(dat[k*2+1],dat[k*2+2]);\n }\n }\n\n int subquery(int a,int b,int k,int l,int r){\n if(r<=a||b<=l)return INF;\n if(a<=l&&r<=b)return dat[k];\n else{\n return min(subquery(a,b,k*2+1,l,(l+r)/2),subquery(a,b,k*2+2,(l+r)/2,r));\n }\n }\n\n int query(int a,int b){\n return subquery(a,b,0,0,size);\n }\n};\n\nstruct BIT{\n int n;\n vector<int> bit;\n BIT(int size):n(size),bit(size+1,0){}\n\n int sum(int i){\n int s=0;\n while(i>0){\n s+=bit[i];\n i-=i&-i;\n }\n return s;\n }\n\n void add(int i,int v){\n if(i==0)return ;\n while(i<=n){\n bit[i]+=v;\n i+=i&-i;\n }\n }\n\n int lower_bound(int w){\n if(w<=0)return 0;\n int x=0,r=1;\n while(r<n)r<<=1;\n for(int k=r;k>0;k>>=1){\n if(x+k<=n&&bit[x+k]<w){\n w-=bit[x+k];\n x+=k;\n }\n }\n return x+1;\n }\n};\n\nint N,Q;\nchar input[1000010];\nvector<string> S;\nvector<int> ord;\nint ridx[100010],lcp[100010];\nRangeMinQuery rmq;\n\nint main(){\n scanf(\"%d\",&N);\n S.resize(N);\n rep(i,N){\n scanf(\"%s\",input);\n S[i]=string(input);\n ord.push_back(i);\n }\n sort(ord.begin(),ord.end(),[=](const int& a,const int& b){\n return S[a] < S[b];\n });\n rep(i,N){\n ridx[ord[i]]=i;\n }\n rmq.init(N-1);\n rep(i,N-1){\n lcp[i]=0;\n rep(j,min(S[ord[i]].size(),S[ord[i+1]].size())){\n if(S[ord[i]][j]==S[ord[i+1]][j])lcp[i]++;\n else break;\n }\n // dbg(lcp[i]);\n rmq.update(i,lcp[i]);\n }\n\n BIT bit(N);\n\n scanf(\"%d\",&Q);\n rep(i,Q){\n int a,b;\n scanf(\"%d%d\",&a,&b);\n a--; b--;\n if(a==0){\n bit.add(ridx[b]+1,+1);\n }else if(a==1){\n bit.add(ridx[b]+1,-1);\n }else{\n int sum=bit.sum(ridx[b]);\n int idx=bit.lower_bound(sum+1);\n if(idx==N+1){\n cout<<-1<<endl;\n continue;\n }\n int len=rmq.query(ridx[b],idx-1);\n if(len>=(int)S[b].size())cout<<ord[idx-1]+1<<endl;\n else cout<<-1<<endl;\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 11112, "score_of_the_acc": -0.0947, "final_rank": 2 }, { "submission_id": "aoj_1555_3447913", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing int64 = long long;\nconst int mod = 1e9 + 7;\nconst int inf = (1 << 30) - 1;\nconst int64 infll = (1LL << 61) - 1;\n\nstruct IoSetup {\n IoSetup() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n cerr << fixed << setprecision(10);\n }\n} iosetup;\n\ntemplate< typename T >\nostream &operator<<(ostream &os, const vector< T > &v) {\n for(int i = 0; i < (int) v.size(); i++) {\n os << v[i] << (i + 1 != v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate< typename T >\nistream &operator>>(istream &is, vector< T > &v) {\n for(T &in : v) is >> in;\n return is;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate< typename T = int64 >\nvector< T > make_v(size_t a) {\n return vector< T >(a);\n}\n\ntemplate< typename T, typename... Ts >\nauto make_v(size_t a, Ts... ts) {\n return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));\n}\n\ntemplate< typename T, typename V >\ntypename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {\n t = v;\n}\n\ntemplate< typename T, typename V >\ntypename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {\n for(auto &e : t) fill_v(e, v);\n}\n\ntemplate< typename G >\nstruct HeavyLightDecomposition {\n G &g;\n vector< int > sz, in, out, head, rev, par;\n\n HeavyLightDecomposition(G &g) :\n g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {}\n\n void dfs_sz(int idx, int p) {\n par[idx] = p;\n sz[idx] = 1;\n if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back());\n for(auto &to : g[idx]) {\n if(to == p) continue;\n dfs_sz(to, idx);\n sz[idx] += sz[to];\n if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to);\n }\n }\n\n void dfs_hld(int idx, int par, int &times) {\n in[idx] = times++;\n rev[in[idx]] = idx;\n for(auto &to : g[idx]) {\n if(to == par) continue;\n head[to] = (g[idx][0] == to ? head[idx] : to);\n dfs_hld(to, idx, times);\n }\n out[idx] = times;\n }\n\n void build() {\n dfs_sz(0, -1);\n int t = 0;\n dfs_hld(0, -1, t);\n }\n\n int la(int v, int k) {\n while(1) {\n int u = head[v];\n if(in[v] - k >= in[u]) return rev[in[v] - k];\n k -= in[v] - in[u] + 1;\n v = par[u];\n }\n }\n\n int lca(int u, int v) {\n for(;; v = par[head[v]]) {\n if(in[u] > in[v]) swap(u, v);\n if(head[u] == head[v]) return u;\n }\n }\n\n template< typename T, typename Q, typename F >\n T query(int u, int v, const T &ti, const Q &q, const F &f, bool edge = false) {\n T l = ti, r = ti;\n for(;; v = par[head[v]]) {\n if(in[u] > in[v]) swap(u, v), swap(l, r);\n if(head[u] == head[v]) break;\n l = f(q(in[head[v]], in[v] + 1), l);\n }\n return f(f(q(in[u] + edge, in[v] + 1), l), r);\n// return {f(q(in[u] + edge, in[v] + 1), l), r};\n }\n\n template< typename Q >\n void add(int u, int v, const Q &q, bool edge = false) {\n for(;; v = par[head[v]]) {\n if(in[u] > in[v]) swap(u, v);\n if(head[u] == head[v]) break;\n q(in[head[v]], in[v] + 1);\n }\n q(in[u] + edge, in[v] + 1);\n }\n};\n\ntemplate< int char_size >\nstruct TrieNode {\n int nxt[char_size + 1];\n\n int exist; // 子ども以下に存在する文字列の数の合計\n vector< int > accept; // その文字列id\n\n TrieNode() : exist(0) {\n memset(nxt, -1, sizeof(nxt));\n }\n};\n\ntemplate< int char_size, int margin >\nstruct Trie {\n using Node = TrieNode< char_size >;\n\n vector< Node > nodes;\n int root;\n\n Trie() : root(0) {\n nodes.push_back(Node());\n }\n\n virtual void direct_action(int node, int id) {}\n\n virtual void child_action(int node, int child, int id) {}\n\n void update_direct(int node, int id) {\n nodes[node].accept.push_back(id);\n direct_action(node, id);\n }\n\n void update_child(int node, int child, int id) {\n ++nodes[node].exist;\n child_action(node, child, id);\n }\n\n void add(const string &str, int str_index, int node_index, int id) {\n if(str_index == str.size()) {\n update_direct(node_index, id);\n } else {\n const int c = str[str_index] - margin;\n if(nodes[node_index].nxt[c] == -1) {\n nodes[node_index].nxt[c] = (int) nodes.size();\n nodes.push_back(Node());\n }\n add(str, str_index + 1, nodes[node_index].nxt[c], id);\n update_child(node_index, nodes[node_index].nxt[c], id);\n }\n }\n\n void add(const string &str, int id) {\n add(str, 0, 0, id);\n }\n\n void add(const string &str) {\n add(str, nodes[0].exist);\n }\n\n void query(const string &str, const function< void(int) > &f, int str_index, int node_index) {\n for(auto &idx : nodes[node_index].accept) f(idx);\n if(str_index == str.size()) {\n return;\n } else {\n const int c = str[str_index] - margin;\n if(nodes[node_index].nxt[c] == -1) return;\n query(str, f, str_index + 1, nodes[node_index].nxt[c]);\n }\n }\n\n void query(const string &str, const function< void(int) > &f) {\n query(str, f, 0, 0);\n }\n\n int size() const {\n return (nodes[0].exist);\n }\n\n int nodesize() const {\n return ((int) nodes.size());\n }\n\n};\n\ntemplate< typename T >\nstruct edge {\n int src, to;\n T cost;\n\n edge(int to, T cost) : src(-1), to(to), cost(cost) {}\n\n edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}\n\n edge &operator=(const int &x) {\n to = x;\n return *this;\n }\n\n operator int() const { return to; }\n};\n\ntemplate< typename T >\nusing Edges = vector< edge< T > >;\ntemplate< typename T >\nusing WeightedGraph = vector< Edges< T > >;\nusing UnWeightedGraph = vector< vector< int > >;\ntemplate< typename T >\nusing Matrix = vector< vector< T > >;\n\ntemplate< typename Monoid >\nstruct SegmentTree {\n using F = function< Monoid(Monoid, Monoid) >;\n\n int sz;\n vector< Monoid > seg;\n\n const F f;\n const Monoid M1;\n\n SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {\n sz = 1;\n while(sz < n) sz <<= 1;\n seg.assign(2 * sz, M1);\n }\n\n void set(int k, const Monoid &x) {\n seg[k + sz] = x;\n }\n\n void build() {\n for(int k = sz - 1; k > 0; k--) {\n seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n }\n }\n\n void update(int k, const Monoid &x) {\n k += sz;\n seg[k] = x;\n while(k >>= 1) {\n seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n }\n }\n\n Monoid query(int a, int b) {\n Monoid L = M1, R = M1;\n for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n if(a & 1) L = f(L, seg[a++]);\n if(b & 1) R = f(seg[--b], R);\n }\n return f(L, R);\n }\n\n Monoid operator[](const int &k) const {\n return seg[k + sz];\n }\n};\n\n\nint main() {\n int N;\n cin >> N;\n vector< string > S(N);\n cin >> S;\n Trie< 26, 'a' > trie;\n vector< int > ord(N);\n iota(begin(ord), end(ord), 0);\n sort(begin(ord), end(ord), [&](int a, int b) {\n return S[a] < S[b];\n });\n vector< int > rev(N);\n for(int i = 0; i < N; i++) rev[ord[i]] = i;\n for(int i = 0; i < S.size(); i++) trie.add(S[ord[i]], i);\n UnWeightedGraph g(trie.nodesize());\n vector< int > id(N);\n function< void(int) > dfs = [&](int idx) {\n for(auto i : trie.nodes[idx].accept) id[i] = idx;\n for(int i = 0; i < 26; i++) {\n if(~trie.nodes[idx].nxt[i]) {\n g[idx].emplace_back(trie.nodes[idx].nxt[i]);\n dfs(trie.nodes[idx].nxt[i]);\n }\n }\n };\n dfs(0);\n HeavyLightDecomposition< UnWeightedGraph > hld(g);\n hld.build();\n int Q;\n cin >> Q;\n auto f = [](int a, int b) { return min(a, b); };\n SegmentTree< int > seg(g.size(), f, inf);\n auto q = [&](int a, int b) { return seg.query(a, b); };\n while(Q--) {\n int a, b;\n cin >> a >> b;\n --b;\n if(a == 1) {\n seg.update(hld.in[id[rev[b]]], rev[b]);\n } else if(a == 2) {\n seg.update(hld.in[id[rev[b]]], inf);\n } else {\n auto ret = seg.query(hld.in[id[rev[b]]], hld.out[id[rev[b]]]);\n if(ret == inf) cout << -1 << endl;\n else cout << ord[ret] + 1 << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 280, "memory_kb": 220940, "score_of_the_acc": -1.2653, "final_rank": 15 }, { "submission_id": "aoj_1555_1254485", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <vector>\n#include <set>\n#include <utility>\nusing namespace std;\n\ntypedef unsigned long long ull;\n\n// BASE: 29,53,131,257\n// MOD: 636094623231363827,348051774975651917,140814840257324803,71777214294589669\n\nint main() {\n ios::sync_with_stdio(false);\n int N; cin >> N;\n vector<pair<string, int>> S(N);\n for(int i = 0; i < N; ++i) {\n cin >> S[i].first;\n S[i].second = i;\n }\n sort(S.begin(), S.end());\n vector<int> M(N);\n for(int i = 0; i < N; ++i) {\n M[S[i].second] = i;\n }\n vector<vector<ull>> hash(N);\n for(int i = 0; i < N; ++i) {\n ull h = 0ULL;\n for(int j = 0; j < (int)S[i].first.length(); ++j) {\n h *= 29;\n h += (S[i].first[j] - 'a');\n h %= 636094623231363827;\n hash[i].push_back(h);\n }\n }\n set<int> D;\n int Q; cin >> Q;\n for(int q = 0; q < Q; ++q) {\n int k, id; cin >> k >> id;\n --id;\n if(k == 1) {\n D.insert(M[id]);\n }\n else if(k == 2) {\n D.erase(M[id]);\n }\n else {\n bool ok = false;\n auto succ = D.lower_bound(M[id]);\n if(succ != D.end() && S[M[id]].first.size() <= S[*succ].first.size() && hash[M[id]][S[M[id]].first.size() - 1] == hash[*succ][S[M[id]].first.size() - 1]) ok = true;\n if(ok) {\n cout << S[*succ].second + 1 << endl;\n }\n else {\n cout << -1 << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 25744, "score_of_the_acc": -0.2145, "final_rank": 5 }, { "submission_id": "aoj_1555_1249214", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nint main()\n{\n int n;\n cin >> n;\n vector<pair<string, int> > s(n);\n vector<int> longStr;\n for(int i=0; i<n; ++i){\n cin >> s[i].first;\n if(s[i].first.size() >= 1000)\n longStr.push_back(i);\n s[i].second = i + 1;\n }\n sort(s.begin(), s.end());\n\n vector<int> index(n+1);\n for(int i=0; i<n; ++i)\n index[s[i].second] = i;\n\n map<pair<int, int>, bool> compLongStr;\n for(int x : longStr){\n for(int y : longStr){\n compLongStr[make_pair(x, y)] = (s[y].first.substr(0, s[x].first.size()) == s[x].first);\n }\n }\n\n int q;\n cin >> q;\n set<int> dic;\n while(--q >= 0){\n int k, x;\n cin >> k >> x;\n x = index[x];\n\n if(k == 1){\n dic.insert(x);\n }\n else if(k == 2){\n dic.erase(x);\n }\n else{\n auto it = dic.lower_bound(x);\n if(it == dic.end()){\n cout << -1 << endl;\n continue;\n }\n\n int y = *it;\n if(compLongStr.find(make_pair(x, y)) != compLongStr.end()){\n if(compLongStr[make_pair(x, y)])\n cout << s[y].second << endl;\n else\n cout << -1 << endl;\n }\n else{\n if(s[y].first.substr(0, s[x].first.size()) == s[x].first)\n cout << s[y].second << endl;\n else\n cout << -1 << endl;\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 12912, "score_of_the_acc": -0.2154, "final_rank": 6 }, { "submission_id": "aoj_1555_1249190", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nint main()\n{\n int n;\n cin >> n;\n vector<pair<string, int> > s(n);\n vector<int> longStr;\n for(int i=0; i<n; ++i){\n cin >> s[i].first;\n if(s[i].first.size() >= 1000)\n longStr.push_back(i);\n s[i].second = i + 1;\n }\n sort(s.begin(), s.end());\n\n vector<int> index(n+1);\n for(int i=0; i<n; ++i)\n index[s[i].second] = i;\n\n map<pair<int, int>, bool> compLongStr;\n for(int i : longStr){\n for(int j : longStr){\n compLongStr[make_pair(i, j)] = s[i].first <= s[j].first;\n }\n }\n\n int q;\n cin >> q;\n set<int> dic;\n while(--q >= 0){\n int k, x;\n cin >> k >> x;\n x = index[x];\n\n if(k == 1){\n dic.insert(x);\n }\n else if(k == 2){\n dic.erase(x);\n }\n else{\n auto it = dic.lower_bound(x);\n if(it == dic.end()){\n cout << -1 << endl;\n continue;\n }\n\n int y = *it;\n if(compLongStr.find(make_pair(x, y)) != compLongStr.end()){\n if(compLongStr[make_pair(x, y)])\n cout << s[y].second << endl;\n else\n cout << -1 << endl;\n }\n else{\n if(s[y].first.substr(0, s[x].first.size()) == s[x].first)\n cout << s[y].second << endl;\n else\n cout << -1 << endl;\n }\n }\n }\n\n return 0;\n}", "accuracy": 0.2857142857142857, "time_ms": 200, "memory_kb": 10500, "score_of_the_acc": -0.1939, "final_rank": 16 }, { "submission_id": "aoj_1555_1249075", "code_snippet": "#include<cstdio>\n#include <iostream>\n#include <algorithm>\n#include<set>\nusing namespace std;\n\nstruct data{\n string s;\n int id, num;\n\n bool operator <(data d) const{\n if(d.s < s) return false;\n return true;\n }\n};\nint N;\n\ntypedef unsigned long long ull;\n\nconst ull B = 100000007;\n\nbool contain(string a, string b){\n int al = a.length(), bl = b.length();\n if(al > bl) return false;\n \n ull t = 1;\n for(int i = 0 ; i < al ; i++) t *= B;\n \n ull ah = 0, bh = 0;\n for(int i = 0 ; i < al ; i++) ah = ah * B + a[i];\n for(int i = 0 ; i < al ; i++) bh = bh * B + b[i];\n \n if(ah == bh) return true;\n return false;\n\n}\n\nint main() {\n //cin >> N;\n scanf(\"%d\", &N);\n data dict[N];\n int A[N];\n\n for(int i=0; i<N; i++){\n cin>>dict[i].s;\n dict[i].num=0;\n dict[i].id=i;\n }\n sort(dict, dict+N);\n //dictを辞書順sortする\n\n for(int i=0; i<N; i++){\n A[dict[i].id]=i;\n }\n \n set<int> S;\n \n int Q;\n cin >> Q;\n for(int i=0; i<Q; i++){\n int k, idx;\n //cin >> k >> idx;\n scanf(\"%d%d\", &k, &idx);\n //cout<< dict[A[idx-1]].s<<endl;\n if(k==1){\n dict[A[idx-1]].num=1;\n S.insert(A[idx-1]);\n }\n else if(k==2){\n dict[A[idx-1]].num=0;\n S.erase(A[idx-1]);\n }\n else{\n int flag=0;\n set<int>::iterator j = S.lower_bound(A[idx-1]);\n //cout << \"j = \" << *j << endl;\n \n //for(int j=A[idx-1]; j<N; j++){\n\tif(dict[*j].num==1){\n\t //部分文字列の判定\n\t if(contain(dict[A[idx-1]].s, dict[*j].s)){\n\t //cout<<dict[j].id+1<<endl;\n\t printf(\"%d\\n\", dict[*j].id+1);\n\t flag=1;\n\t }\n\t }\n\tif(j==S.end()){\n\t //cout<<-1<<endl;\n\t printf(\"-1\\n\");\n\t}\n\t}\n }\n return 0;\n \n}", "accuracy": 0.047619047619047616, "time_ms": 110, "memory_kb": 8772, "score_of_the_acc": -0.0939, "final_rank": 19 }, { "submission_id": "aoj_1555_1248909", "code_snippet": "#include <string>\n#include <vector>\n#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<list>\n#include<deque>\n#include<bitset>\n#include<set>\n#include<map>\n#include<unordered_map>\n#include<cstring>\n#include<sstream>\n#include<complex>\n#include<iomanip>\n#include<numeric>\n#define X first\n#define Y second\n#define pb push_back\n#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))\n#define rrep(X,Y) for (int (X) = (Y-1);(X) >=0;--(X))\n#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))\n#define peat(X,Y) for (;(X) < (Y);++(X))\n#define all(X) (X).begin(),(X).end()\n#define rall(X) (X).rbegin(),(X).rend()\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n//template<class T> using vv=vector<vector<T>>;\ntemplate<class T> ostream& operator<<(ostream &os, const vector<T> &t) {\nos<<\"{\"; rep(i,t.size()) {os<<t[i]<<\",\";} os<<\"}\"<<endl; return os;}\ntemplate<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<\"(\"<<t.first<<\",\"<<t.second<<\")\";}\n\nint main(){\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(0);\n int i,j,k,n,q;\n cin>>n;\n vector<string> str(n);\n rep(i,n)cin>>str[i];\n vector<pair<string,int>> srt;\n rep(i,n)srt.emplace_back(str[i],i);\n sort(all(srt));\n vector<int> s(n),rnk(n);\n rep(i,n)\n s[i]=srt[i].Y;\n rep(i,n)\n rnk[s[i]]=i;\n set<int> st;\n cin>>q;\n int t,id;\n rep(i,q){\n cin>>t>>id;\n --id;\n if(t==1){\n st.insert(rnk[id]);\n }else if(t==2){\n st.erase(rnk[id]);\n }else{\n auto re=st.lower_bound(rnk[id]);\n if(re==st.end()){\n\tcout<<-1<<endl;\n }else{\n\t//cout<<str[id]<<\",\"<<str[s[*re]]<<endl;\n\tif(str[id].size()<=str[s[*re]].size() && str[id]==str[s[*re]].substr(0,str[id].size()))\n\t cout<<s[*re]+1<<endl;\n\telse\n\t cout<<-1<<endl;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 10444, "score_of_the_acc": -0.4385, "final_rank": 10 }, { "submission_id": "aoj_1555_1248908", "code_snippet": "#include<stdio.h>\n#include<algorithm>\n#include<string>\n#include<set>\nusing namespace std;\nset<int>Set;\nset<int>::iterator it;\nint n, Num[101000], LCP[101000];\nint st[101000][2], ed[101000];\nchar p[101000];\nstruct A{\n\tint num;\n\tstring S;\n\tbool operator <(const A &p)const{\n\t\treturn S < p.S;\n\t}\n}w[101000];\nint main(){\n\tint i, j, s1, s2, top = 0, Q, typ, a, t;\n\tscanf(\"%d\", &n);\n\tfor (i = 1; i <= n; i++){\n\t\tscanf(\"%s\", p);\n\t\tw[i].S = p;\n\t\tw[i].num = i;\n\t}\n\tsort(w + 1, w + n + 1);\n\tfor (i = 1; i <= n; i++)Num[w[i].num] = i;\n\tfor (i = 1; i < n; i++){\n\t\ts1 = w[i].S.size(), s2 = w[i + 1].S.size();\n\t\tfor (j = 0; j < min(s1,s2); j++){\n\t\t\tif (w[i].S[j] != w[i + 1].S[j])break;\n\t\t}\n\t\tLCP[i] = j;\n\t}\n\tfor (i = 1; i <= n; i++){\n\t\twhile (top && st[top][0] > LCP[i - 1]){\n\t\t\ted[st[top][1]] = i - 1;\n\t\t\ttop--;\n\t\t}\n\t\ttop++;\n\t\tst[top][0] = w[i].S.size(); st[top][1] = i;\n\t}\n\twhile (top){\n\t\ted[st[top][1]] = n;\n\t\ttop--;\n\t}\n\tscanf(\"%d\", &Q);\n\twhile (Q--){\n\t\tscanf(\"%d%d\", &typ, &a);\n\t\tif (typ == 1){\n\t\t\tSet.insert(Num[a]);\n\t\t}\n\t\tif (typ == 2){\n\t\t\tSet.erase(Set.find(Num[a]));\n\t\t}\n\t\tif (typ == 3){\n\t\t\tit = Set.lower_bound(Num[a]);\n\t\t\tif (it == Set.end() || *it > ed[Num[a]]){\n\t\t\t\tprintf(\"-1\\n\");\n\t\t\t}\n\t\t\telse{\n\t\t\t\tprintf(\"%d\\n\", w[*it].num);\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 10032, "score_of_the_acc": -0.0692, "final_rank": 1 }, { "submission_id": "aoj_1555_1248884", "code_snippet": "#include <limits>\n#include <iterator>\n#include <utility>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\nnamespace lc {\ntemplate <typename Traits>\nclass SegmentTree {\npublic:\n\ttypedef typename Traits::value_type value_type;\nprivate:\n\tTraits m_traits;\n\tstd::vector<value_type> m_data;\n\tint m_size;\n\tvoid initialize(){\n\t\tfor(int i = m_size - 2; i >= 0; --i){\n\t\t\tm_data[i] = m_traits(m_data[i * 2 + 1], m_data[i * 2 + 2]);\n\t\t}\n\t}\n\tvalue_type query(int a, int b, int k, int l, int r) const {\n\t\tif(r <= a || b <= l){ return m_traits.default_value(); }\n\t\tif(a <= l && r <= b){ return m_data[k]; }\n\t\tconst value_type vl = query(a, b, k * 2 + 1, l, (l + r) / 2);\n\t\tconst value_type vr = query(a, b, k * 2 + 2, (l + r) / 2, r);\n\t\treturn m_traits(vl, vr);\n\t}\npublic:\n\ttemplate <typename Iterator>\n\tSegmentTree(\n\t\tIterator first, Iterator last, const Traits &traits = Traits()) :\n\t\tm_size(1), m_traits(traits)\n\t{\n\t\tconst int n = std::distance(first, last);\n\t\twhile(m_size < n){ m_size *= 2; }\n\t\tm_data.resize(m_size * 2 - 1, m_traits.default_value());\n\t\tstd::copy(first, last, m_data.begin() + m_size - 1);\n\t\tinitialize();\n\t}\n\tvoid update(int i, const value_type &val){\n\t\ti += m_size - 1;\n\t\tm_data[i] = val;\n\t\twhile(i > 0){\n\t\t\ti = (i - 1) / 2;\n\t\t\tm_data[i] = m_traits(m_data[i * 2 + 1], m_data[i * 2 + 2]);\n\t\t}\n\t}\n\tvalue_type query(int a, int b) const {\n\t\treturn query(a, b, 0, 0, m_size);\n\t}\n};\n}\nnamespace lc {\ntemplate <class T>\nstruct MinTraits {\n\ttypedef T value_type;\n\tvalue_type default_value() const {\n\t\treturn std::numeric_limits<value_type>::max();\n\t}\n\tvalue_type operator()(const value_type &a, const value_type &b) const {\n\t\treturn std::min(a, b);\n\t}\n};\n}\nusing namespace std;\ntypedef pair<string, int> psi;\ntypedef long long ll;\nint main(){\n\tios_base::sync_with_stdio(false);\n\tint n;\n\tcin >> n;\n\tvector<psi> table(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tcin >> table[i].first;\n\t\ttable[i].second = i;\n\t}\n\tsort(table.begin(), table.end());\n\tvector<int> inv(n);\n\tfor(int i = 0; i < n; ++i){ inv[table[i].second] = i; }\n\tvector<int> lcp(n);\n\tfor(int i = 0; i + 1 < n; ++i){\n\t\tconst int len = min(table[i].first.size(), table[i + 1].first.size());\n\t\tfor(lcp[i] = 0; lcp[i] < len; ++lcp[i]){\n\t\t\tif(table[i].first[lcp[i]] != table[i + 1].first[lcp[i]]){ break; }\n\t\t}\n\t}\n\tlc::SegmentTree<lc::MinTraits<int>> lcp_st(lcp.begin(), lcp.end());\n\tvector<int> range_table(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tconst int len = table[i].first.size();\n\t\tint l = i + 1, r = n;\n\t\twhile(l < r){\n\t\t\tconst int c = l + (r - l) / 2;\n\t\t\tif(lcp_st.query(i, c) >= len){\n\t\t\t\tl = c + 1;\n\t\t\t}else{\n\t\t\t\tr = c;\n\t\t\t}\n\t\t}\n\t\trange_table[i] = l;\n\t}\n\tconst ll llinf = numeric_limits<ll>::max();\n\tvector<ll> occur_st_init(n, llinf);\n\tlc::SegmentTree<lc::MinTraits<ll>> occur_st(\n\t\toccur_st_init.begin(), occur_st_init.end());\n\tint q;\n\tcin >> q;\n\twhile(q--){\n\t\tint t, s;\n\t\tcin >> t >> s;\n\t\t--s;\n\t\tif(t == 1){\n\t\t\toccur_st.update(inv[s], (static_cast<ll>(inv[s]) << 32) | s);\n\t\t}else if(t == 2){\n\t\t\toccur_st.update(inv[s], llinf);\n\t\t}else if(t == 3){\n\t\t\tconst ll k = occur_st.query(inv[s], range_table[inv[s]]);\n\t\t\tif(k >= llinf){\n\t\t\t\tcout << \"-1\\n\";\n\t\t\t}else{\n\t\t\t\tcout << (k & 0x7fffffff) + 1 << \"\\n\";\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 12348, "score_of_the_acc": -0.3556, "final_rank": 9 }, { "submission_id": "aoj_1555_1248854", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=a;i<b;++i)\n#define for_rev(i,a,b) for(int i=a;i>=b;--i)\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define allof(a) a.begin(),a.end()\n#define minit(a,b) memset(a,b,sizeof(a))\n#define size_of(a) (int)a.size()\n#define mp make_pair\n\ntypedef long long lint;\ntypedef double Double;\ntypedef pair< int, int > pii;\ntypedef unsigned long long ull;\ntypedef pair<string,int> psi;\n\nconst ull B = 1000000007;\n\nint n,q;\nstring s[100010];\npsi ns[100010];\nset<int> ss;\nvector<ull> v[100010];\nint id[100010];\n\nint main() {\n\tscanf(\"%d\",&n);\n\trep(i,n){\n\t\tcin>>s[i];\n\t\tns[i]=mp(s[i],i);\n\t\tv[i].resize((int)s[i].size()+1);\n\t\tull t=0;\n\t\trep(j,s[i].size()){\n\t\t\tt=t*B+s[i][j];\n\t\t\tv[i][j]=t;\n\t\t}\n\t}\n\tsort(ns,ns+n);\n\trep(i,n) id[ns[i].second]=i;\n\tscanf(\"%d\",&q);\n\twhile(q--){\n\t\tint k,p;\n\t\tscanf(\"%d %d\",&k,&p);\n\t\t--p;\n\t\tif(k==1)ss.insert(id[p]);\n\t\telse if(k==2)ss.erase(id[p]);\n\t\telse{\n\t\t\tauto it=ss.lower_bound(id[p]);\n\t\t\tint f=0,ret;\n\t\t\tif(it==ss.end())ret=-1;\n\t\t\telse{\n\t\t\t\tint t=ns[*it].second,l=s[p].size();\n\t\t\t\tif((int)s[t].size()>=l&&v[t][l-1]==v[p][l-1])f=1;\n\t\t\t\tif(!f)ret=-1;\n\t\t\t\telse ret=t+1;\n\t\t\t}\n\t\t\tprintf(\"%d\\n\",ret);\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 28116, "score_of_the_acc": -0.2461, "final_rank": 7 }, { "submission_id": "aoj_1555_1248761", "code_snippet": "#include <cstring>\n#include <algorithm>\n#define REP(i,n) for(int i=0; i<(int)(n); i++)\n\n#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)\n\n#include <cstdio>\n#include <iostream>\n#include <queue>\n#include <map>\n#include <set>\n\ninline int getInt(){ int s; scanf(\"%d\", &s); return s; }\n\n\nusing namespace std;\n\nstruct Node{\n int id;\n map<char, Node *> next;\n Node(){\n id = -1;\n }\n};\n\nvector<string> s;\nchar buff[100000 + 10];\n\nNode *getNext(Node *node, char c){\n if(node->next[c] == NULL)\n node->next[c] = new Node();\n return node->next[c];\n}\n\nvector<int> rng;\nvector<int> r;\n\nvoid dfs(Node *node){\n const int id = node->id;\n if(id != -1){\n r.push_back(id);\n FOR(it, node->next)\n dfs(it->second);\n rng[id] = r.size();\n }else{\n FOR(it, node->next)\n dfs(it->second);\n }\n}\n\nint main(){\n const int n = getInt();\n rng = vector<int>(n);\n\n Node *root = new Node();\n\n REP(i,n){\n scanf(\"%s\", buff);\n const int len = strlen(buff);\n\n Node *node = root;\n REP(i,len){\n const char c = buff[i];\n node = getNext(node, c);\n }\n node->id = i;\n }\n\n dfs(root);\n\n // REP(i,n) printf(\"%d \", r[i]); puts(\"\");\n // REP(i,n) printf(\"%d \", rng[i]); puts(\"\");\n\n vector<int> memo(n);\n REP(i,n) memo[r[i]] = i;\n\n const int q = getInt();\n set<int> in;\n REP(_,q){\n const int k = getInt();\n const int id = getInt() - 1;\n\n if(k == 1){\n in.insert(memo[id]);\n }else if(k == 2){\n in.erase(memo[id]);\n }else{\n set<int>::iterator it = in.lower_bound(memo[id]);\n if(it != in.end()){\n const int idx = *it;\n if(idx < rng[id]){\n printf(\"%d\\n\", r[idx] + 1);\n }else{\n puts(\"-1\");\n }\n }else{\n puts(\"-1\");\n }\n }\n\n }\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 125048, "score_of_the_acc": -0.7224, "final_rank": 12 } ]
aoj_1570_cpp
Lights of Apartment Problem エーちゃんとリカちゃんとハルトくんはマンションに遊びに来た。 3人は全ての部屋の電気を管理できる部屋に忍び込みいたずらをすることにした。 このマンションは n 個の立方体が1列に並んでいる形をしている。 各立方体は西から順に1辺の長さが1ずつ増えていて(1,2,3,..., n )、 i 番目の立方体は i 階あり、各階に縦 i ×横 i 個の部屋がある。 2番目以降の立方体の西側は1つ西の立方体の東側と接していて、全ての立方体の南側は真っ直ぐな道路に面している。 初め全ての部屋に電気がついている。 3人はそれぞれ次の行動をした。 エーちゃんは西から k 番目の全ての部屋の電気を消した。 リカちゃんは南から k 番目の全ての部屋の電気を消した。 ハルトくんは k 階の全ての部屋の電気を消した。 このようないたずらが m 回行われた後に電気がついている部屋の数を求めよ。 Input n m q 1 k 1 ... q m k m 入力は全て整数で与えられる。 1行目に立方体の数 n 、行動の数 m が与えられる。 2行目以降 m 行に行動した人の番号 q と k が与えられる。 q i が0の場合エーちゃん、 1の場合リカちゃん、2の場合ハルトくんが行動したことを表す。 3人は部屋がない場所の電気を消そうとすることもある。 Constraints 1 ≤ n , m ≤ 50000 0 ≤ q i ≤ 2 1 ≤ k i ≤ 2×10 9 同じ行動は一度しか与えられない Output 電気がついている部屋の数を1行に出力せよ。 Sample Input 1 3 1 0 4 Sample Output 1 27 Sample Input 2 3 2 2 2 2 3 Sample Output 2 14
[ { "submission_id": "aoj_1570_3089072", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<vector<int> > v(3, vector<int>(n+1, 0));\n while(--m >= 0){\n int q, k;\n cin >> q >> k;\n\n if(q == 0){\n long long a = 1;\n long long b = n + 1;\n while(a < b){\n long long c = (a + b) / 2;\n if(c * (c + 1) / 2 < k)\n a = c + 1;\n else\n b = c;\n }\n if(a <= n)\n ++ v[q][a];\n }\n else{\n if(k <= n)\n ++ v[q][k];\n }\n }\n\n long long ans = 0;\n for(int i=1; i<=n; ++i){\n for(int j=1; j<3; ++j)\n v[j][i] += v[j][i-1];\n\n long long cnt = 1;\n for(int j=0; j<3; ++j)\n cnt *= i - v[j][i];\n ans += cnt;\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3700, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_1570_3079431", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\n#define INF 1e16\n#define mod 1000000007\n\nll sum(ll a){\n return a*(a+1)/2;\n}\n\nll sqsum(ll a){\n return a*(a+1)*(2*a+1)/6;\n}\n\nll qsum(ll a){\n return sum(a)*sum(a);\n}\n\nll n,m;\nvector<P> qs;\nll cnt[3][50001],cntjust[3][50001];\n\nint main(){\n cin>>n>>m;\n rep(i,m){\n ll q,k;\n cin>>q>>k;\n if(q==0){\n ll lb=0,ub=n+1;\n while(ub-lb>1){\n ll mid=(ub+lb)/2;\n if(sum(mid)<k)lb=mid;\n else ub=mid;\n }\n k=ub;\n }\n if(k>n)continue;\n qs.push_back(P(q,k));\n cnt[q][k]++;\n cntjust[q][k]++;\n }\n rep(i,3)rep(j,n)cnt[i][j+1]+=cnt[i][j];\n m=qs.size();\n ll add=0,sub=0;\n rep(i,m){\n ll q=qs[i].fi,k=qs[i].se;\n if(q==0){\n sub+=k*k;\n }else{\n sub+=sqsum(n)-sqsum(k-1);\n }\n }\n\n rep(i,m){\n ll q=qs[i].fi,k=qs[i].se;\n if(q==0){\n add+=k*(cnt[1][k]+cnt[2][k]);\n }else if(q==1){\n add+=(sum(n)-sum(k-1))*cnt[2][k-1];\n add+=(sum(n)-sum(k-1))*cntjust[2][k];\n }else if(q==2){\n add+=(sum(n)-sum(k-1))*cnt[1][k-1];\n }\n }\n\n rep(i,m){\n ll q=qs[i].fi,k=qs[i].se;\n if(q==0){\n sub+=cnt[1][k]*cnt[2][k];\n }\n }\n\n cout<<qsum(n)+add-sub<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5588, "score_of_the_acc": -1.9613, "final_rank": 6 }, { "submission_id": "aoj_1570_3078644", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nusing vi = vector<int>;\n\nll f(ll x){\n return x*(x+1)/2;\n}\n\nint main(){\n int n,m;\n scanf(\" %d %d\", &n, &m);\n\n vector<vi> add(n+1,vi(3));\n rep(i,m){\n int q;\n ll k;\n scanf(\" %d %lld\", &q, &k);\n if(q==0){\n ll l=0, r=100000;\n while(r-l>1){\n ll mid = (l+r)/2;\n if(f(mid)<k) l=mid;\n else r=mid;\n }\n if(r<=n) ++add[r][q];\n }\n else{\n if(k<=n) ++add[k][q];\n }\n }\n\n ll ans = 0;\n ll a[3]={};\n for(ll i=1; i<=n; ++i){\n a[0] = add[i][0];\n for(int j=1; j<3; ++j) a[j] += add[i][j];\n\n ans += i*i*i;\n ans -= i*i*(a[0]+a[1]+a[2]);\n ans += i*(a[0]*a[1]+a[1]*a[2]+a[2]*a[0]);\n ans -= a[0]*a[1]*a[2];\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5508, "score_of_the_acc": -0.9206, "final_rank": 2 }, { "submission_id": "aoj_1570_3077932", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n,m;\nvector<ll> tate;\nvector<ll> yoko;\nbool flag[50005];\nll len[50005];\nll rect[50005];\n\nint main(void){\n\tfor(ll i=1;i<=50000;i++){\n\t\tlen[i]=len[i-1]+i;\n\t}\n\tfor(ll i=1;i<=50000;i++){\n\t\trect[i]=rect[i-1]+i*i;\n\t}\n\tscanf(\"%d%d\",&n,&m);\n\tfor(int i=0;i<m;i++){\n\t\tll q,k;\n\t\tscanf(\"%lld%lld\",&q,&k);\n\t\tk--;\n\t\tif(q==2){\n\t\t\tif(k<n)flag[k]=true;\n\t\t}\n\t\tif(q==0){\n\t\t\tif(k<len[n])tate.push_back(k);\n\t\t}\n\t\tif(q==1){\n\t\t\tif(k<n)yoko.push_back(k);\n\t\t}\n\t}\n\tsort(tate.begin(),tate.end());\n\tsort(yoko.begin(),yoko.end());\n\tll dec=0;\n\tll ans=0;\n\tll cnt_ty=(ll)0;\n\tfor(int i=0;i<tate.size();i++){\n\t\tint lv=upper_bound(len,len+n,tate[i])-len;\n\t\tint l2=lower_bound(yoko.begin(),yoko.end(),lv)-yoko.begin();\n\t\tdec+=lv;\n\t\tcnt_ty+=l2;\n\t}\n\tfor(int i=0;i<yoko.size();i++){\n\t\tdec+=len[n]-len[yoko[i]];\n\t}\n\tll po=0;\n\tint i_t=0,i_y=0;\n\tfor(int i=0;i<n;i++){\n\t\twhile(i_t<tate.size() && tate[i_t]<len[i]){\n\t\t\tint lv=upper_bound(len,len+n,tate[i_t])-len;\n\t\t\tint l2=lower_bound(yoko.begin(),yoko.end(),lv)-yoko.begin();\n\t\t\tdec-=lv;\n\t\t\tcnt_ty-=l2;\n\t\t\ti_t++;\n\t\t}\n\t\twhile(i_y<yoko.size() && yoko[i_y]<i){\n\t\t\ti_y++;\n\t\t}\n\t\tdec-=(ll)(i)*i_y;\n\t\tif(flag[i])continue;\n\t\tll val=rect[n]-rect[i];\n\t\tval-=dec;\n\t\tval+=cnt_ty;\n\t\tans+=val;\n\t\t//printf(\"%lld %lld %lld\\n\",rect[n]-rect[i],dec,(ll)cnt_ty);\n\t}\n\tprintf(\"%lld\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4332, "score_of_the_acc": -0.3218, "final_rank": 1 }, { "submission_id": "aoj_1570_2701032", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\n\n#define DUMP(x) cout<<#x<<\":\"<<(x)<<endl\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class T>\nistream& operator>>(istream& is, vector<T>& xs){\n for(auto& x: xs)\n\tis >> x;\n return is;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, const pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nostream& operator<<(ostream& os, const vector<T>& xs){\n for(unsigned int i=0;i<xs.size();++i)\n\tos << (i?\" \":\"\") << xs[i];\n return os;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n LL N, M;\n cin >> N >> M;\n\n LL ans = 0;\n VL idx = {0ll};\n for(LL i=1,j=1;i<=N;++i){\n\tans += i*i*i;\n\tidx.PB(j);\n\tj += i;\n }\n\n VVL qs(3);\n REP(i,M){\n\tLL Q, K;\n\tcin >> Q >> K;\n\tqs[Q].PB(K);\n }\n REP(q,3) SORT(qs[q]);\n\n VL blk_x;\n REP(i,N+1) blk_x.PB(i);\n\n for(LL x: qs[0]){\n\tif(x > N*(N+1)/2) continue;\n\tLL ix = (--upper_bound(ALL(idx), x)) - begin(idx);\n\tblk_x[ix]--;\n\tans -= ix*ix;\n }\n\n VL blk_y(N+1);\n for(LL i=1;i<=N;++i)\n\tblk_y[i] = blk_y[i-1] + i * blk_x[i];\n\n VL del_y(N+1);\n for(LL y:qs[1]){\n\tif(y > N) continue;\n\tans -= blk_y[N] - blk_y[y-1];\n\t++del_y[y];\n }\n\n LL acc = 0;\n VL blk_z(N+1);\n for(LL i=1;i<=N;++i){\n\tacc += del_y[i];\n\tblk_z[i] = blk_z[i-1] + (i - acc) * blk_x[i];\n }\n for(LL z: qs[2]){\n\tif(z > N) continue;\n\tans -= blk_z[N] - blk_z[z-1];\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5664, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_1570_2576347", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nll d[50010],x[50010],y[50010],z[50010],ans,n,m;\n \nint main() {\n cin >> n >> m;\n for(ll i=1; i<=n; i++) d[i]=d[i-1]+i;\n for(int i=0,a,b; i<m; i++) {\n cin >> a >> b;\n if(!a) b=lower_bound(d,d+n+1,b)-d;\n if(b>n) continue;\n if(!a) x[b]++;\n else if(a==1) y[b]++;\n else z[b]++;\n }\n for(ll i=1; i<=n; i++) {\n y[i]+=y[i-1];\n z[i]+=z[i-1];\n ans+=(i-x[i])*(i-y[i])*(i-z[i]);\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4572, "score_of_the_acc": -1.444, "final_rank": 5 } ]
aoj_1568_cpp
String Conversion Problem ジェニファーとマリアンはカーラに文字列 S をプレゼントしました。 しかし、カーラは文字列 S をもらっても嬉しくありません。 文字列 T が欲しかったのです。 3人は協力して文字列 S を文字列 T に変えることにしました。 最初にジェニファーが文字を任意の順番に並び替えます。 次にマリアンが2種類のアルファベットの小文字を交換することを任意の回数行います。 この操作では、例えば以下のように、文字列中の同じ文字が全て交換されます。 aab → a と b を交換 → bba aab → a と c を交換 → ccb 最後にカーラがある1文字を別の1文字に置換することを T になるまで繰り返します。 ジェニファーとマリアンはカーラの置換回数が少なくなるようにしてあげることにしました。 カーラが行う置換の回数の最小値を求めてください。 Input n S T 1行目に文字列の長さ n が与えられる。 2行目に文字列 S 、3行目に文字列 T が与えられる。 Constraints 1 ≤ n ≤ 10 5 S , T は 'a'~'z' のみを含む | S | = | T | = n Output カーラの最少の置換回数を1行に出力せよ。 Sample Input 1 3 abc xyz Sample Output 1 0 Sample Input 2 5 aaabb xyxyz Sample Output 2 1
[ { "submission_id": "aoj_1568_2576345", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint a[26],b[26],n,ans;\nstring s,t;\n\nint main() {\n cin >> n >> s >> t;\n for(int i=0; i<n; i++) {\n a[s[i]-'a']++;\n b[t[i]-'a']++;\n }\n sort(a,a+26);\n sort(b,b+26);\n for(int i=0; i<26; i++) ans+=max(0,b[i]-a[i]);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3332, "score_of_the_acc": -1, "final_rank": 13 }, { "submission_id": "aoj_1568_1526763", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <iostream>\n#include <string>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <functional>\n#include <cassert>\n\ntypedef long long ll;\nusing namespace std;\n\n#define mod 1000000007\n#define INF 1000000000\n#define LLINF 2000000000000000000LL\n\n#define SIZE 100000\n\nint main(){\n int n;\n int s_cc[26]={0},t_cc[26]={0};\n int ans = 0;\n string s,t;\n \n cin >> n >> s >> t;\n \n for(int i=0;i<n;i++){\n s_cc[s[i]-'a']++;\n t_cc[t[i]-'a']++;\n }\n \n sort(s_cc,s_cc+26);\n sort(t_cc,t_cc+26);\n \n for(int i=0;i<26;i++){\n ans += max(0,s_cc[i]-t_cc[i]);\n }\n \n cout << ans << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1524, "score_of_the_acc": -0.0794, "final_rank": 3 }, { "submission_id": "aoj_1568_1525935", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = 1 << 28;\n\nint main () {\n int n; cin >> n;\n string S, T;\n cin >> S >> T;\n\n vector<int> s(256), t(256);\n for (int i = 0; i < n; i++) {\n s[S[i]]++;\n t[T[i]]++;\n }\n\n sort(s.begin(), s.end(), greater<int>());\n sort(t.begin(), t.end(), greater<int>());\n\n int res = 0;\n for (int i = 0; i < t.size() && i < s.size(); i++) {\n res += abs(t[i] - s[i]);\n }\n\n cout << res / 2 << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1524, "score_of_the_acc": -0.0794, "final_rank": 3 }, { "submission_id": "aoj_1568_1521733", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#define loop(i,a,b) for(int i=a;i<b;i++)\n#define rep(i,a) loop(i,0,a)\nusing namespace std;\nint main(){\n int n;\n string a,b;\n cin>>n>>a>>b;\n\n int cnt1[256]={0};\n rep(i,n)cnt1[(char)a[i]]++;\n int cnt2[256]={0};\n rep(i,n)cnt2[(char)b[i]]++;\n\n vector<int>A,B;\n rep(i,256)if(cnt1[i])A.push_back(cnt1[i]);\n rep(i,256)if(cnt2[i])B.push_back(cnt2[i]);\n\n n=max(A.size(),B.size());\n while(A.size()!=n)A.push_back(0);\n while(B.size()!=n)B.push_back(0);\n sort(A.begin(),A.end());\n sort(B.begin(),B.end());\n ////////////////////////////\n /*rep(i,n){\n rep(j,n){\n if(B[i]==A[j]){\n\tA.erase(A.begin()+j);\n\tB.erase(B.begin()+i);\n\ti--;j--;\n\tbreak;\n }\n }\n }*/\n int out=0;\n rep(i,A.size()){\n if(B[i]>A[i]){\n out+=B[i]-A[i];\n }\n }\n cout<<out<<endl;\n /*\n rep(i,A.size())cout<<A[i]<<endl;\n cout<<endl;\n rep(i,B.size())cout<<B[i]<<endl;\n */\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1532, "score_of_the_acc": -0.0835, "final_rank": 11 }, { "submission_id": "aoj_1568_1521617", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <set>\n#include <vector>\n#include <queue>\n\n#define unless(c) if (!(c))\n#define each(i, c) for (auto& i: c)\n\nusing namespace std;\n\nconst int inf = 1 << 29;\nstruct edge { int to, cap, cost, rev; };\n\nconst int MAX_V = ('z' - 'a') * 2 + 10;\n\nint V = MAX_V;\nvector<edge> g[MAX_V];\nint h[MAX_V];\nint dist[MAX_V];\nint prevv[MAX_V], preve[MAX_V];\n\nvoid add_edge(int from, int to, int cap, int cost)\n{\n // cout << from << ' ' << to << endl;\n g[from].push_back((edge){to, cap, cost, g[to].size()});\n g[to].push_back((edge){from, 0, -cost, g[from].size() - 1});\n}\n\ntypedef pair<int, int> P;\n\nint min_cost_flow(int s, int t, int f)\n{\n // cout << \"st : \" << s << \" => \" << t << endl;\n int res = 0;\n while(f > 0){\n fill(dist, dist + V, inf);\n dist[s] = 0;\n while (true) {\n bool flg = false;\n for (int v = 0; v < V; v++) {\n if (dist[v] == inf) continue;\n for (int i = 0; i < g[v].size(); i++) {\n edge &e = g[v][i];\n if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {\n dist[e.to] = dist[v] + e.cost;\n\t prevv[e.to] = v;\n\t preve[e.to] = i;\n\t flg = true;\n }\n }\n }\n if (!flg) break;\n } \n // cout << __LINE__ << \": \" << dist[t] << endl;\n if(dist[t] == inf){\n return res;\n }\n int d = f;\n for (int v = t; v != s; v = prevv[v]) {\n // cout << v << endl;\n d = min(d, g[prevv[v]][preve[v]].cap);\n }\n // cout << __LINE__ << \": \" << endl;\n f -= d;\n res += d * dist[t];\n // cout << __LINE__ << \": \" << endl;\n for (int v = t; v != s; v = prevv[v]) {\n edge &e = g[prevv[v]][preve[v]];\n e.cap -= d;\n g[v][e.rev].cap += d;\n }\n // cout << __LINE__ << \": \" << endl;\n }\n return res;\n\n /*\n int res = 0;\n fill(h, h + V, inf);\n h[s] = 0;\n for (int loop = V; loop--; ) {\n for (int i = 0; i < V; ++i) {\n if (h[i] == inf) continue;\n for (int j = 0; j < g[i].size(); ++j) {\n edge& e = g[i][j];\n if (e.cap) {\n h[e.to] = min(h[e.to], h[i] + e.cost);\n }\n }\n }\n }\n for (int i = 0; i < V; ++i) {\n if (h[i] != inf) cout << i << \": \" << h[i] << endl;\n }\n while (f > 0) {\n priority_queue<P, vector<P>, greater<P> > q;\n fill(dist, dist + V, inf);\n dist[s] = 0;\n q.push(P(0, s));\n while (q.size()) {\n const P p = q.top();\n q.pop();\n const int v = p.second;\n if (dist[v] < p.first) continue;\n for (int i = 0; i < g[v].size(); ++i) {\n const edge& e = g[v][i];\n if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {\n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n q.push(P(dist[e.to], e.to));\n }\n }\n }\n if (dist[t] >= inf) {\n return res;\n }\n for (int v = 0; v < V; ++v) {\n h[v] += dist[v];\n }\n int d = f;\n for (int v = t; v != s; v = prevv[v]) {\n d = min(d, g[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * h[t];\n for (int v = t; v != s; v = prevv[v]) {\n edge& e = g[prevv[v]][preve[v]];\n e.cap -= d;\n g[v][e.rev].cap += d;\n }\n }\n return res;\n */\n}\n\nint main(void)\n{\n int n;\n while (cin >> n) {\n fill(g, g + V, vector<edge>());\n string s;\n string t;\n cin >> s >> t;\n int cnt_s[300];\n int cnt_t[300];\n set<char> cs(s.begin(), s.end());\n fill(cnt_s, cnt_s + 300, 0);\n fill(cnt_t, cnt_t + 300, 0);\n for (char c = 'a'; c <= 'z'; ++c) {\n cnt_s[c] = count(s.begin(), s.end(), c);\n cnt_t[c] = count(t.begin(), t.end(), c);\n }\n const int source = V - 1;\n const int sink = V - 2;\n const int base = V / 2;\n for (char c = 'a'; c <= 'z'; ++c) {\n for (char d = 'a'; d <= 'z'; ++d) {\n\tif (cnt_s[c] && cnt_t[d]) {\n\t int a = c - 'a';\n\t int b = d - 'a' + base;\n\t add_edge(a, b, 1, -min(cnt_s[c], cnt_t[d]));\n\t}\n }\n }\n for (char c = 'a'; c <= 'z'; ++c) {\n if (cnt_s[c]) {\n\tadd_edge(source, c - 'a', 1, 0);\n\tint n = c - 'a';\n\t// g[source].push_back((edge){n, 1, 0});\n }\n if (cnt_t[c]) {\n\tadd_edge(c - 'a' + base, sink, 1, 0);\n\tint n = c - 'a' + base;\n\t// g[n].push_back((edge){sink, 1, 0});\n }\n }\n int tmp = min_cost_flow(source, sink, cs.size());\n // cout << \"tmp: \" << tmp << endl;\n cout << n + tmp << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1532, "score_of_the_acc": -0.0835, "final_rank": 11 }, { "submission_id": "aoj_1568_1521549", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define loop(i,a,b) for(int i=a;i<(int)(b);i++)\n#define rep(i,b) loop(i,0,b)\n#define show(x) cout << #x << \" = \" << x << endl\ntemplate<class T>\nostream& operator << (ostream& os, const vector<T> v){\n rep(i,v.size()) os << v[i] << \" \";\n return os;\n}\n\ntypedef long long ll;\n\n\n\ntypedef int Capacity;\ntypedef int Cost;\nstruct PrimalDual {\n struct Edge {\n int dst;\n Capacity cap, cap_orig;\n Cost cost;\n int revEdge; bool isRev;\n Edge(int dst, Capacity cap, Cost cost, int revEdge, bool isRev)\n :dst(dst), cap(cap), cap_orig(cap), cost(cost), revEdge(revEdge), isRev(isRev) {}\n };\n int n;\n vector<vector<Edge> > g;\n enum : Cost { inf = 1<<29 };\n enum : int { MAX_V = 1000 };\n\n PrimalDual(int n_) : n(n_), g(vector<vector<Edge> >(n_)){}\n\n void add_edge(int src, int dst, Capacity cap, Cost cost) {\n g[src].emplace_back(dst, cap, cost, g[dst].size(), false);\n g[dst].emplace_back(src, 0, -cost, g[src].size() - 1, true);\n }\n\n Cost solve(int s, int t, int f) {\n Cost res = 0;\n static Cost h[MAX_V], dist[MAX_V];\n static int prevv[MAX_V], preve[MAX_V];\n rep(i,n)h[i] = 0;\n while (f > 0) {\n typedef pair<Cost,int> pcv;\n priority_queue<pcv, vector<pcv>, greater<pcv> > q;\n rep(i,n) dist[i] = inf;\n dist[s] = 0;\n q.emplace(pcv(0, s));\n while (q.size()) {\n pcv p = q.top(); q.pop();\n int v = p.second;\n if (dist[v] < p.first) continue;\n rep(i,g[v].size()){\n Edge &e = g[v][i];\n if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {\n dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];\n prevv[e.dst] = v;\n preve[e.dst] = i;\n q.emplace(pcv(dist[e.dst], e.dst));\n }\n }\n }\n if (dist[t] == inf) {\n return -1;\n }\n rep(v,n) h[v] += dist[v];\n // s-t 間最短路に沿って目一杯流す\n int d = f;\n for (int v = t; v != s; v = prevv[v]) {\n d = min(d, g[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * h[t];\n for (int v = t; v != s; v = prevv[v]) {\n Edge &e = g[prevv[v]][preve[v]];\n e.cap -= d;\n g[v][e.revEdge].cap += d;\n }\n }\n return res;\n }\n\n void view(){\n rep(i,g.size()){\n rep(j,g[i].size())if(!g[i][j].isRev){\n Edge& e = g[i][j];\n if(e.cap_orig - e.cap==0) continue;\n printf(\"%3d->%3d (flow:%d cost:%d)\\n\", i, e.dst, e.cap_orig - e.cap, e.cost);\n }\n }\n }\n\n};\n\nint main(){\n int n;\n while(cin >> n){\n string s,t;\n cin >> s >> t;\n vector<int> cs(26),ct(26);\n rep(i,n){\n cs[s[i]-'a']++;\n ct[t[i]-'a']++;\n }\n PrimalDual g(102);\n const int S = 100;\n const int T = 101;\n // from, to, cap, cost\n rep(i,26){\n g.add_edge(S, i, 1, 0);\n g.add_edge(50+i, T, 1, 0);\n }\n rep(i,26)rep(j,26){\n g.add_edge(i, j+50, 1, max(0, ct[j]-cs[i]));\n }\n int ans = g.solve(S,T,26);\n //g.view();\n //show(ans);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1524, "score_of_the_acc": -0.0794, "final_rank": 3 }, { "submission_id": "aoj_1568_1521368", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef complex<double> P;\n#define REP(i,n) for(int i=0;i<n;++i)\n#define REPR(i,n) for(int i=1;i<n;++i)\n\n#define DEBUG(x) cout<<#x<<\": \"<<x<<endl\n#define DEBUG_VEC(v) REP(i,v.size())cout<<#v<<\"[\"<<i<<\"]: \"<<v[i]<<endl\n#define ALL(a) (a).begin(),(a).end()\n\nint main(){\n int n;\n string S,T;\n cin >> n >> S >> T;\n int scnt[26],tcnt[26];\n REP(i,26)scnt[i]=tcnt[i]=0;\n REP(i,n){\n scnt[S[i]-'a']++;\n tcnt[T[i]-'a']++;\n }\n sort(scnt,scnt+26);\n sort(tcnt,tcnt+26);\n int rest = 0;\n int result = 0;\n REP(i,26){\n int sc = scnt[i], tc = tcnt[i];\n if(sc==tc)continue;\n if(sc>tc){\n result += (sc-tc);\n }\n }\n cout << result << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1404, "score_of_the_acc": -0.0183, "final_rank": 2 }, { "submission_id": "aoj_1568_1521255", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 2;\n\nint main()\n{\n int n;\n string s, t;\n cin >> n >> s >> t;\n\n vector<int> a(26, 0), b(26, 0);\n for(int i=0; i<n; ++i){\n ++ a[s[i]-'a'];\n ++ b[t[i]-'a'];\n }\n sort(a.begin(), a.end());\n sort(b.begin(), b.end());\n\n vector<vector<int> > dp(27, vector<int>(27, INF));\n dp[0][0] = 0;\n for(int i=0; i<27; ++i){\n for(int j=0; j<27; ++j){\n if(i < 26)\n dp[i+1][j] = min(dp[i+1][j], dp[i][j] + a[i]);\n if(j < 26)\n dp[i][j+1] = min(dp[i][j+1], dp[i][j] + b[j]);\n if(i < 26 && j < 26)\n dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + abs(a[i] - b[j]));\n }\n }\n cout << (dp[26][26] / 2) << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1524, "score_of_the_acc": -0.0794, "final_rank": 3 }, { "submission_id": "aoj_1568_1521244", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<set>\n#include<string>\n#include<stack>\n#include<cstdio>\n#include<cmath>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\n\n#define fr first\n#define sc second\n#define mp make_pair\n#define pb push_back\n#define rep(i,x) for(int i=0;i<x;i++)\n#define rep1(i,x) for(int i=1;i<=x;i++)\n#define rrep(i,x) for(int i=x-1;i>=0;i--)\n#define rrep1(i,x) for(int i=x;i>0;i--)\n#define sor(v) sort(v.begin(),v.end())\n#define rev(s) reverse(s.begin(),s.end())\n#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)\n#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)\n#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())\n#define mp1(a,b,c) P1(a,P(b,c))\n\nconst int INF=1000000000;\nconst int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};\nconst int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};\n\nint main(){\n\tint n;\n\tstring s,t;\n\tcin >> n >> s >> t;\n\tint a[30] = {} , b[30] = {};\n\trep(i,n){\n\t\ta[s[i]-'a'] ++;\n\t\tb[t[i]-'a'] ++;\n\t}\n\t\n\tvector<int> A,B;\n\trep(i,26){\n\t\tA.pb(a[i]);\n\t\tB.pb(b[i]);\n\t}\n\tsor(A);\n\tsor(B);\n\t\n\tint ret = 0;\n\trep(i,26){\n\t\tret += abs(A[i]-B[i]);\n\t}\n\t\n\tcout << ret/2 << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1528, "score_of_the_acc": -0.0815, "final_rank": 9 }, { "submission_id": "aoj_1568_1521188", "code_snippet": "#define _USE_MATH_DEFINES\n#define _CRT_SECURE_NO_DEPRECATE\n\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <limits>\n#include <ctime>\n#include <cassert>\n#include <map>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <stack>\n#include <queue>\n#include <numeric>\n#include <iterator>\n#include <bitset>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> Pii;\ntypedef pair<ll, ll> Pll;\n\n#define FOR(i,n) for(int i = 0; i < (n); i++)\n#define sz(c) ((int)(c).size())\n#define ten(x) ((int)1e##x)\n\ntemplate<typename ...> static inline int getchar_unlocked(void) { return getchar(); }\ntemplate<typename ...> static inline void putchar_unlocked(int c) { putchar(c); }\n#define mygc(c) (c)=getchar_unlocked()\n#define mypc(c) putchar_unlocked(c)\nvoid reader(int& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k&&k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; }\nvoid reader(ll& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k&&k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; }\nint reader(char c[]) { int i, s = 0; for (;;) { mygc(i); if (i != ' '&&i != '\\n'&&i != '\\r'&&i != '\\t'&&i != EOF) break; }c[s++] = i; for (;;) { mygc(i); if (i == ' ' || i == '\\n' || i == '\\r' || i == '\\t' || i == EOF) break; c[s++] = i; }c[s] = '\\0'; return s; }\ntemplate <class T, class S> void reader(T& x, S& y) { reader(x); reader(y); }\ntemplate <class T, class S, class U> void reader(T& x, S& y, U& z) { reader(x); reader(y); reader(z); }\ntemplate <class T, class S, class U, class V> void reader(T& x, S& y, U& z, V & w) { reader(x); reader(y); reader(z); reader(w); }\nvoid writer(int x, char c) { int s = 0, m = 0; char f[10]; if (x<0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); }\nvoid writer(ll x, char c) { int s = 0, m = 0; char f[20]; if (x<0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); }\nvoid writer(const char c[]) { int i; for (i = 0; c[i] != '\\0'; i++)mypc(c[i]); }\nvoid writer(const char x[], char c) { int i; for (i = 0; x[i] != '\\0'; i++)mypc(x[i]); mypc(c); }\ntemplate<class T> void writerLn(T x) { writer(x, '\\n'); }\ntemplate<class T, class S> void writerLn(T x, S y) { writer(x, ' '); writer(y, '\\n'); }\ntemplate<class T, class S, class U> void writerLn(T x, S y, U z) { writer(x, ' '); writer(y, ' '); writer(z, '\\n'); }\ntemplate<class T> void writerArr(T x[], int n) { if (!n) { mypc('\\n'); return; }FOR(i, n - 1)writer(x[i], ' '); writer(x[n - 1], '\\n'); }\n\nll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }\ntemplate<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; }\ntemplate<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; }\ntemplate<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }\n\nvector<int> f(string& s) {\n\tvector<int> ret(26);\n\tfor (auto c : s) ret[c - 'a']++;\n\tsort(ret.begin(), ret.end());\n\treturn ret;\n}\n\nint main() {\n\tint n;\n\tstring s, t;\n\tcin >> n >> s >> t;\n\tauto v1 = f(s), v2 = f(t);\n\tint ans = 0;\n\tFOR(i, 26) {\n\t\tint diff = v1[i] - v2[i];\n\t\tif (diff > 0) ans += diff;\n\t}\n\twriterLn(ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1368, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1568_1521125", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin(), (c).end()\n\nint inf = 10000000;\n\ntypedef int Weight;\nstruct Edge {\n int src, dst;\n Weight weight;\n Edge(int src, int dst, Weight weight) :\n src(src), dst(dst), weight(weight) { }\n Edge(int src, int dst) :\n src(src), dst(dst) { }\n};\nbool operator < (const Edge &e, const Edge &f) {\n return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!\n e.src != f.src ? e.src < f.src : e.dst < f.dst;\n}\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\n\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\n\nint hungarian(const Matrix &a) {\n int n = a.size(), p, q;\n Array fx(n, inf), fy(n, 0);\n vector<int> x(n, -1), y(n, -1);\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < n; ++j)\n fx[i] = max(fx[i], a[i][j]);\n for (int i = 0; i < n; ) {\n vector<int> t(n, -1), s(n+1, i);\n for (p = q = 0; p <= q && x[i] < 0; ++p)\n for (int k = s[p], j = 0; j < n && x[i] < 0; ++j)\n if (fx[k] + fy[j] == a[k][j] && t[j] < 0) {\n s[++q] = y[j], t[j] = k;\n if (s[q] < 0)\n for (p = j; p >= 0; j = p)\n y[j] = k = t[j], p = x[k], x[k] = j;\n }\n if (x[i] < 0) {\n int d = inf;\n for (int k = 0; k <= q; ++k)\n for (int j = 0; j < n; ++j)\n if (t[j] < 0) d = min(d, fx[s[k]] + fy[j] - a[s[k]][j]);\n for (int j = 0; j < n; ++j) fy[j] += (t[j] < 0 ? 0 : d);\n for (int k = 0; k <= q; ++k) fx[s[k]] -= d;\n } else ++i;\n }\n int ret = 0;\n for (int i = 0; i < n; ++i){\n \n\tret += a[i][x[i]];\n }\n return ret;\n}\n\n\nint main(){\n\tint n;\n\tcin >> n;\n\tstring s,t;\n\tcin >> s >> t;\n\tmap<char,int> dic1,dic2;\n\tfor( auto c : s ) dic1[c]++;\n\tfor( auto c : t ) dic2[c]++;\n\t\n\n\tMatrix mat(26,Array(26));\n\tfor(int i = 0 ; i < 26 ; i++){\n\t\tfor(int j = 0 ; j < 26 ; j++){\n\t\t\tmat[i][j] = min(dic1['a'+i],dic2['a'+j]);\n\t\t}\n\t}\n\tcout << n-hungarian(mat) << endl;\n\t\n\t\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1524, "score_of_the_acc": -0.0794, "final_rank": 3 }, { "submission_id": "aoj_1568_1521122", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<string>\n\nusing namespace std;\n\nvector<int> tov(string s){\n vector<int> v(128);\n for(auto e:s){\n v[e]++;\n }\n sort(begin(v),end(v));\n return v;\n}\n\nint main(){\n int n;\n cin>>n;\n string S;\n cin>>S;\n string T;\n cin>>T;\n auto sv=tov(S);\n auto tv=tov(T);\n int ans=0;\n for(int i=0;i<sv.size();i++){\n ans+=abs(sv[i]-tv[i]);\n }\n cout<<ans/2<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1528, "score_of_the_acc": -0.0815, "final_rank": 9 }, { "submission_id": "aoj_1568_1521115", "code_snippet": "#define _USE_MATH_DEFINES\n#include <algorithm>\n#include <cstdio>\n#include <functional>\n#include <iostream>\n#include <cfloat>\n#include <climits>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <map>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <time.h>\n#include <vector>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<int, int> i_i;\ntypedef pair<ll, int> ll_i;\ntypedef pair<double, int> d_i;\ntypedef pair<ll, ll> ll_ll;\ntypedef pair<double, double> d_d;\nstruct edge { int u, v; ll w; };\n\nll MOD = 1000000007;\nll _MOD = 1000000009;\nint INF = INT_MAX / 2;\ndouble EPS = 1e-10;\n\nint main() {\n\tint n; cin >> n;\n\tstring S, T; cin >> S >> T;\n\tvector<int> a(26), b(26);\n\tfor (int i = 0; i < n; i++) {\n\t\ta[S[i] - 'a']++;\n\t\tb[T[i] - 'a']++;\n\t}\n\tsort(a.begin(), a.end());\n\tsort(b.begin(), b.end());\n\tint sum = 0;\n\tfor (int k = 0; k < 26; k++)\n\t\tsum += abs(a[k] - b[k]);\n\tcout << sum / 2 << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1524, "score_of_the_acc": -0.0794, "final_rank": 3 } ]
aoj_1567_cpp
Lucky Number Problem 0 から N -1 までの数字があります。 M +1日間のラッキーナンバーを以下の方法で選びたいと思います。 最初の日にラッキーナンバーをランダムに決めます。 i 日後のラッキーナンバーを A 、( i +1)日後のラッキーナンバーを B とすると、 B = ( A + j ) % N (ただし j は 0 ≤ j < N かつ ( j / K ) が偶数となる全ての整数である ) で求められる B のうちのいずれかからランダムに決めます。ただし、 a / b の結果は小数点以下を切り捨てた整数とし、 a % b は a を b で割った余りとします。 また K は N / K が割り切れて、 N / K が偶数となる数字であることが保証されます。 例えば 0,1,2,3 と数字があり、 K = 1 のとき 0 の次のラッキーナンバーは 0 or 2 1 の次のラッキーナンバーは 1 or 3 2 の次のラッキーナンバーは 0 or 2 3 の次のラッキーナンバーは 1 or 3 となります。 次に Q 個の質問が与えられます。 各質問の内容は以下のとおりです。 最初の日のラッキーナンバーが a のとき b 日後のラッキーナンバーが c になるような b 日後までのラッキーナンバーの選び方は何通りでしょうか? 選び方は膨大な数になると思われるので 1,000,000,007 で割った余りを求めてください。 例えば、 N =4 K =1 で、最初のラッキーナンバーが 0 で 3 日後のラッキーナンバーが 2 になるような組み合わせは 0 → 0 → 0 → 2 0 → 0 → 2 → 2 0 → 2 → 0 → 2 0 → 2 → 2 → 2 の4通りです。 Input 入力は以下の形式で与えられる。 N M K Q a 1 b 1 c 1 a 2 b 2 c 2 . . . a Q b Q c Q Constraints 1 ≤ N ≤ 5000 1 ≤ M ≤ 5000 1 ≤ K ≤ 5000 N / K が割り切れる N / K は偶数 1 ≤ Q ≤ 100000 0 ≤ a i < N ( 1 ≤ i ≤ Q ) 0 ≤ b i ≤ M ( 1 ≤ i ≤ Q ) 0 ≤ c i < N ( 1 ≤ i ≤ Q ) Output 各質問に対し、答えを求め 1,000,000,007 で割った余りを一行ずつ順番に出力してください。 Sample Input1 6 3 1 10 0 1 0 0 2 0 0 3 0 1 3 3 0 2 1 2 2 2 2 3 2 2 2 0 1 1 1 2 2 1 Sample Output1 1 3 9 9 0 3 9 3 1 0
[ { "submission_id": "aoj_1567_3079308", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\n#define INF 1e16\n#define mod 1000000007\n\nll N,M,K,Q;\nll dp[5001][5001];\n\nint main(){\n cin>>N>>M>>K>>Q;\n dp[0][0]=1;\n rep(i,M){\n ll crtdp=0;\n rep(A,N){\n ll j=(N-A)%N;\n if((j/K)%2==0)(crtdp+=dp[i][A])%=mod;\n }\n rep(B,2*K){\n for(ll b=B;b<N;b+=2*K)dp[i+1][b]=crtdp;\n rep(j,N/K){\n ll a=(j*K+(B+1))%N;\n if(j%2==0) (crtdp+=dp[i][a])%=mod;\n else (crtdp+=mod-dp[i][a])%=mod;\n }\n }\n }\n\n while(Q--){\n ll a,b,c;\n cin>>a>>b>>c;\n c-=a;\n if(c<0)c+=N;\n cout<<dp[b][c]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1000, "memory_kb": 198452, "score_of_the_acc": -1.9982, "final_rank": 11 }, { "submission_id": "aoj_1567_3078629", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst int mod = 1e9+7;\nconst int N = 5005;\n\nint dp[N][N]={};\n\nint main(){\n int n,m,k,q;\n scanf(\" %d %d %d %d\", &n, &m, &k, &q);\n\n dp[0][0] = 1;\n rep(i,N-1){\n int val = 0;\n rep(j,n){\n if( (j/k)%2==0 ){\n int idx = (0-j+n)%n;\n (val += dp[i][idx]) %= mod;\n }\n }\n\n int B = 2*k;\n rep(j,B){\n for(int x=j; x<n; x+=B) dp[i+1][x] = val;\n\n rep(x,n/k){\n int idx = (j+1-x*k+n)%n;\n if(x%2==0) val = (val+dp[i][idx])%mod;\n else val = (val-dp[i][idx]+mod)%mod;\n }\n }\n }\n\n rep(i,q){\n int a,b,c;\n scanf(\" %d %d %d\", &a, &b, &c);\n int d = c-a;\n if(d<0) d+=n;\n printf(\"%d\\n\", dp[b][d]);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 101040, "score_of_the_acc": -0.8095, "final_rank": 2 }, { "submission_id": "aoj_1567_3077804", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n,m,K,q;\nll dp[5005][5005];\nll tp[5005];\n\nll get(int f,int t){\n\tif(f>=0){\n\t\treturn (tp[t]-tp[f]+MOD)%MOD;\n\t}else{\n\t\treturn (tp[K*2]-tp[K*2+f]+tp[t]+MOD)%MOD;\n\t}\n}\n\nint main(void){\n\tscanf(\"%d%d%d%d\",&n,&m,&K,&q);\n\tdp[0][0]=1;\n\tfor(int i=0;i<=m;i++){\n\t\tmemset(tp,0,sizeof(tp));\n\t\tfor(int j=0;j<2*K;j++){\n\t\t\tfor(int k=0;k<n/(2*K);k++){\n\t\t\t\ttp[j+1]+=dp[i][k*(2*K)+j];\n\t\t\t\ttp[j+1]%=MOD;\n\t\t\t}\n\t\t}\n\t\tfor(int j=0;j<2*K;j++){\n\t\t\ttp[j+1]+=tp[j];\n\t\t\ttp[j+1]%=MOD;\n\t\t}\n\t\tfor(int j=0;j<2*K;j++){\n\t\t\tfor(int k=0;k<n/(2*K);k++){\n\t\t\t\t//printf(\"%d %d %d %lld\\n\",i,j,k,get(j,j+K));\n\t\t\t\tdp[i+1][k*(2*K)+j]=get(j-K+1,j+1);\n\t\t\t\t//printf(\"%d %d %d %d %lld\\n\",i+1,k*(2*K)+j,j-K+1,j+1,dp[i+1][k*(2*K)+j]);\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=0;i<q;i++){\n\t\tint a,b,c;\n\t\tscanf(\"%d%d%d\",&a,&b,&c);\n\t\tint dif=(c-a+n)%n;\n\t\tprintf(\"%lld\\n\",dp[b][dif]);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 198808, "score_of_the_acc": -1.1667, "final_rank": 5 }, { "submission_id": "aoj_1567_2938729", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nconst int MOD = 1e9+7;\nint sm[10050];\nint dp[5050][5050];\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n int n,m,k,q;\n cin>>n>>m>>k>>q;\n memset(dp,0,sizeof(dp));\n dp[0][0]=1;\n for(int i=0;i<m;i++){\n memset(sm,0,sizeof(sm));\n for(int j=0;j<n;j++){\n int x=j%(2*k);\n sm[x]+=dp[i][j];\n sm[x]%=MOD;\n sm[2*k+x]+=dp[i][j];\n sm[2*k+x]%=MOD;\n }\n \n for(int j=1;j<4*k;j++) sm[j]=(sm[j]+sm[j-1])%MOD;\n \n for(int j=0;j<n;j++)\n dp[i+1][j]=(sm[2*k+(j%(2*k))]+MOD-sm[k+(j%(2*k))])%MOD;\n }\n \n for(int i=0;i<q;i++){\n int a,b,c;\n cin>>a>>b>>c;\n int d=(c-a+n)%n;\n cout<<dp[b][d]<<endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 102868, "score_of_the_acc": -0.9647, "final_rank": 3 }, { "submission_id": "aoj_1567_2936126", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nconst int MOD = 1e9+7;\nint sm[10050];\nint dp[5050][5050];\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n int n,m,k,q;\n cin>>n>>m>>k>>q;\n memset(dp,0,sizeof(dp));\n dp[0][0]=1;\n for(int i=0;i<m;i++){\n memset(sm,0,sizeof(sm));\n for(int j=0;j<n;j++){\n int x=j%(2*k);\n sm[x]+=dp[i][j];\n sm[x]%=MOD;\n sm[2*k+x]+=dp[i][j];\n sm[2*k+x]%=MOD;\n }\n \n for(int j=1;j<4*k;j++) sm[j]=(sm[j]+sm[j-1])%MOD;\n \n for(int j=0;j<n;j++)\n dp[i+1][j]=(sm[2*k+(j%(2*k))]+MOD-sm[k+(j%(2*k))])%MOD;\n }\n \n for(int i=0;i<q;i++){\n int a,b,c;\n cin>>a>>b>>c;\n int d=(c-a+n)%n;\n cout<<dp[b][d]<<endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 102868, "score_of_the_acc": -0.9647, "final_rank": 3 }, { "submission_id": "aoj_1567_2747237", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#define MOD 1000000007\n\nnamespace FFT{\n using dbl = long double;\n \n struct num{\n dbl x,y;\n num(){x=y=0;}\n num(dbl x,dbl y):x(x),y(y){}\n };\n \n inline num operator+(num a,num b){\n return num(a.x+b.x,a.y+b.y);\n }\n inline num operator-(num a,num b){\n return num(a.x-b.x,a.y-b.y);\n }\n inline num operator*(num a,num b){\n return num(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);\n }\n inline num conj(num a){\n return num(a.x,-a.y);\n }\n \n int base=1;\n vector<num> rts={{0,0},{1,0}};\n vector<int> rev={0,1};\n \n const dbl PI=acosl(-1.0);\n \n void ensure_base(int nbase){\n if(nbase<=base) return;\n \n rev.resize(1<<nbase);\n for(int i=0;i<(1<<nbase);i++)\n rev[i]=(rev[i>>1]>>1)+((i&1)<<(nbase-1));\n \n rts.resize(1<<nbase);\n while(base<nbase){\n dbl angle=2*PI/(1<<(base+1));\n for(int i=1<<(base-1);i<(1<<base);i++){\n\trts[i<<1]=rts[i];\n\tdbl angle_i=angle*(2*i+1-(1<<base));\n\trts[(i<<1)+1]=num(cos(angle_i),sin(angle_i));\n }\n base++;\n }\n }\n \n void fft(vector<num> &a,int n=-1){\n if(n==-1) n=a.size();\n assert((n&(n-1))==0);\n \n int zeros=__builtin_ctz(n);\n ensure_base(zeros);\n int shift=base-zeros;\n for(int i=0;i<n;i++)\n if(i<(rev[i]>>shift))\n\tswap(a[i],a[rev[i]>>shift]);\n \n for(int k=1;k<n;k<<=1){\n for(int i=0;i<n;i+=2*k){\n\tfor(int j=0;j<k;j++){\n\t num z=a[i+j+k]*rts[j+k];\n\t a[i+j+k]=a[i+j]-z;\n\t a[i+j]=a[i+j]+z;\n\t}\n }\n }\n }\n \n vector<num> fa;\n \n vector<Int> multiply(vector<int> &a,vector<int> &b){\n int need=a.size()+b.size()-1;\n int nbase=0;\n while((1<<nbase)<need) nbase++;\n ensure_base(nbase);\n \n int sz=1<<nbase;\n if(sz>(int)fa.size()) fa.resize(sz);\n for(int i=0;i<sz;i++){\n int x=(i<(int)a.size()?a[i]:0);\n int y=(i<(int)b.size()?b[i]:0);\n fa[i]=num(x,y);\n }\n fft(fa,sz);\n \n num r(0,-0.25/sz);\n for(int i=0;i<=(sz>>1);i++){\n int j=(sz-i)&(sz-1);\n num z=(fa[j]*fa[j]-conj(fa[i]*fa[i]))*r;\n if(i!=j)\n\tfa[j]=(fa[i]*fa[i]-conj(fa[j]*fa[j]))*r;\n fa[i]=z;\n }\n fft(fa,sz);\n \n vector<Int> res(need);\n for(int i=0;i<need;i++)\n res[i]=fa[i].x+0.5;\n \n return res;\n }\n \n};\n\nsigned main(){ \n cin.tie(0);\n ios::sync_with_stdio(0);\n \n Int n,m,k,q;\n cin>>n>>m>>k>>q;\n\n vector<int> v(n,0);\n for(int j=0;j<n;j++) v[j]=((j/k)%2==0); \n vector<int> dp(n,0);\n dp[0]=1;\n int p=0;\n auto calc=[&](){\n p++;\n auto nx=FFT::multiply(dp,v);\n for(int i=0;i<n;i++) dp[i]=0;\n for(int i=0;i<(int)nx.size();i++) dp[i%n]+=nx[i];\n for(int i=0;i<n;i++) dp[i]%=MOD;\n };\n \n using T = tuple<int, int, int>;\n vector<T> ans(q);\n for(int i=0;i<q;i++){\n int a,b,c;\n cin>>a>>b>>c;\n ans[i]=T(b,(c+n-a)%n,i);\n }\n sort(ans.begin(),ans.end());\n vector<int> pri(q);\n for(int i=0;i<q;i++){\n int b,l,idx;\n tie(b,l,idx)=ans[i];\n while(p<b) calc();\n pri[idx]=dp[l];\n }\n for(int i=0;i<q;i++) cout<<pri[i]<<endl;\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 160, "memory_kb": 4452, "score_of_the_acc": -0.125, "final_rank": 20 }, { "submission_id": "aoj_1567_2688989", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n Info(int arg_start,int arg_end){\n start = arg_start;\n end = arg_end;\n }\n int start,end;\n};\n\nll dp[5001][5001],work[16000];\n\n\nint main(){\n\n int N,M,K,Q;\n\n scanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n int LIMIT = 0;\n while(LIMIT < N)LIMIT += 2*K;\n\n vector<Info> V;\n\n for(int day = 0; day <= M; day++){\n for(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n }\n\n dp[0][0] = 1;\n\n for(int day = 0; day <= M-1; day++){\n\n for(int i = 0; i < 2*N; i++)work[i] = 0;\n\n for(int num = 0; num <= N-1; num++){\n\n if(dp[day][num] == 0)continue;\n\n work[num] += dp[day][num];\n work[num+K] -= dp[day][num];\n work[num+LIMIT] -= dp[day][num];\n work[num+LIMIT+K] += dp[day][num];\n }\n\n for(int i = 1; i < 2*N; i++){\n work[i] += work[i-1];\n }\n\n for(int i = 0; i < 2*N; i++){\n if(i+2*K < 2*N){\n work[i+2*K] += work[i];\n }else{\n break;\n }\n }\n\n dp[day+1][0] += work[0]%MOD;\n dp[day+1][0] %= MOD;\n\n for(int i = 1; i < 2*N; i++){\n dp[day+1][i%N] += work[i]%MOD;\n dp[day+1][i%N] %= MOD;\n }\n }\n\n int a,b,c;\n\n for(int loop = 0; loop < Q; loop++){\n scanf(\"%d %d %d\",&a,&b,&c);\n\n c = (c-a+N)%N;\n\n printf(\"%lld\\n\",dp[b][c]%MOD);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 198684, "score_of_the_acc": -1.416, "final_rank": 9 }, { "submission_id": "aoj_1567_2688984", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[16000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tint LIMIT = 0;\n\twhile(LIMIT < N)LIMIT += 2*K;\n\n\tvector<Info> V;\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\n\t\tfor(int i = 0; i < 2*N; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\twork[num] += dp[day][num];\n\t\t\twork[num+K] -= dp[day][num];\n\t\t\twork[num+LIMIT] -= dp[day][num];\n\t\t\twork[num+LIMIT+K] += dp[day][num];\n\t\t}\n\n\t\tfor(int i = 1; i < 2*N; i++){\n\t\t\twork[i] += work[i-1];\n\t\t}\n\n\t\tfor(int i = 0; i < 2*N; i++){\n\t\t\tif(i+2*K < 2*N){\n\t\t\t\twork[i+2*K] += work[i];\n\t\t\t}else{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = 0; i < 2*N; i++){\n\t\t\tdp[day+1][i%N] += work[i]%MOD;\n\t\t}\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 198692, "score_of_the_acc": -1.3744, "final_rank": 7 }, { "submission_id": "aoj_1567_2688978", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[16000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tint LIMIT = 0;\n\twhile(LIMIT < N)LIMIT += 2*K;\n\n\tvector<Info> V;\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\n\t\tfor(int i = 0; i < 2*N; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\twork[num] += dp[day][num];\n\t\t\twork[num+K] -= dp[day][num];\n\t\t\tif(num+LIMIT < 2*N)work[num+LIMIT] -= dp[day][num];\n\t\t\tif(num+LIMIT+K < 2*N)work[num+LIMIT+K] += dp[day][num];\n\t\t}\n\n\t\tfor(int i = 1; i < 2*N; i++){\n\t\t\twork[i] += work[i-1];\n\t\t}\n\n\t\tfor(int i = 0; i < 2*N; i++){\n\t\t\tif(i+2*K < 2*N){\n\t\t\t\twork[i+2*K] += work[i];\n\t\t\t}else{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tdp[day+1][i] = (work[i]+work[i+N])%MOD;\n\t\t}\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 198680, "score_of_the_acc": -1.2598, "final_rank": 6 }, { "submission_id": "aoj_1567_2688823", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[16000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tvector<Info> V;\n\n\tfor(int i = 0; i+K-1 < N; i += 2*K){\n\t\tV.push_back(Info(i,i+K-1));\n\t}\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\t\tfor(int i = 0; i < 3*N+2; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\t\t\t\twork[num+V[i].start] += dp[day][num];\n\t\t\t\twork[num+V[i].end+1] -= dp[day][num];\n\t\t\t}\n\t\t}\n\n\t\tdp[day+1][0] += work[0]%MOD;\n\t\tdp[day+1][0] %= MOD;\n\n\t\tfor(int i = 1; i < 3*N+2; i++){\n\t\t\twork[i] += work[i-1];\n\t\t\twork[i] %= MOD;\n\t\t\tdp[day+1][i%N] += work[i];\n\t\t\tdp[day+1][i%N] %= MOD;\n\t\t}\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 40, "memory_kb": 7100, "score_of_the_acc": -0.0136, "final_rank": 13 }, { "submission_id": "aoj_1567_2688819", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[16000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tvector<Info> V;\n\n\tfor(int i = 0; i+K-1 < N; i += 2*K){\n\t\tV.push_back(Info(i,i+K-1));\n\t}\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\t\t//for(int i = 0; i < 3*N+2; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\t\t\t\tfor(int k = V[i].start; k <= V[i].end; k++){\n\t\t\t\t\tdp[day+1][(num+k)%N] += dp[day][num];\n\t\t\t\t}\n\t\t\t\t/*work[num+V[i].start] += dp[day][num];\n\t\t\t\twork[num+V[i].end+1] -= dp[day][num];*/\n\t\t\t}\n\t\t}\n\n\t\t/*dp[day+1][0] += work[0]%MOD;\n\t\tdp[day+1][0] %= MOD;\n\n\t\tfor(int i = 1; i < 3*N+2; i++){\n\t\t\twork[i] += work[i-1];\n\t\t\twork[i] %= MOD;\n\t\t\tdp[day+1][i%N] += work[i];\n\t\t\tdp[day+1][i%N] %= MOD;\n\t\t}*/\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 50, "memory_kb": 7168, "score_of_the_acc": -0.0244, "final_rank": 18 }, { "submission_id": "aoj_1567_2688817", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[16000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tvector<Info> V;\n\n\tfor(int i = 0; i < N; i += 2*K){\n\t\tV.push_back(Info(i,min(i+K-1,N-K-1)));\n\t}\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\t\tfor(int i = 0; i < 3*N+2; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\t\t\t\tfor(int k = V[i].start; k <= V[i].end; k++){\n\t\t\t\t\tdp[day+1][(num+k)%N] += dp[day][num];\n\t\t\t\t}\n\t\t\t\t/*work[num+V[i].start] += dp[day][num];\n\t\t\t\twork[num+V[i].end+1] -= dp[day][num];*/\n\t\t\t}\n\t\t}\n\n\t\t/*dp[day+1][0] += work[0]%MOD;\n\t\tdp[day+1][0] %= MOD;\n\n\t\tfor(int i = 1; i < 3*N+2; i++){\n\t\t\twork[i] += work[i-1];\n\t\t\twork[i] %= MOD;\n\t\t\tdp[day+1][i%N] += work[i];\n\t\t\tdp[day+1][i%N] %= MOD;\n\t\t}*/\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 40, "memory_kb": 7160, "score_of_the_acc": -0.0139, "final_rank": 16 }, { "submission_id": "aoj_1567_2688813", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[16000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tvector<Info> V;\n\n\tfor(int i = 0; i < N; i += 2*K){\n\t\tV.push_back(Info(i,i+K-1));\n\t}\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\t\tfor(int i = 0; i < 3*N+2; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\t\t\t\tfor(int k = V[i].start; k <= V[i].end; k++){\n\t\t\t\t\tdp[day+1][(num+k)%N] += dp[day][num];\n\t\t\t\t}\n\t\t\t\t/*work[num+V[i].start] += dp[day][num];\n\t\t\t\twork[num+V[i].end+1] -= dp[day][num];*/\n\t\t\t}\n\t\t}\n\n\t\t/*dp[day+1][0] += work[0]%MOD;\n\t\tdp[day+1][0] %= MOD;\n\n\t\tfor(int i = 1; i < 3*N+2; i++){\n\t\t\twork[i] += work[i-1];\n\t\t\twork[i] %= MOD;\n\t\t\tdp[day+1][i%N] += work[i];\n\t\t\tdp[day+1][i%N] %= MOD;\n\t\t}*/\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 50, "memory_kb": 7160, "score_of_the_acc": -0.0243, "final_rank": 17 }, { "submission_id": "aoj_1567_2688067", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[16000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tvector<Info> V;\n\n\tfor(int i = 0; i < N; i += 2*K){\n\t\tV.push_back(Info(i,i+K-1));\n\t}\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\t\tfor(int i = 0; i < 3*N+2; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\t\t\t\twork[num+V[i].start] += dp[day][num];\n\t\t\t\twork[num+V[i].end+1] -= dp[day][num];\n\t\t\t}\n\t\t}\n\n\t\tdp[day+1][0] += work[0]%MOD;\n\t\tdp[day+1][0] %= MOD;\n\n\t\tfor(int i = 1; i < 3*N+2; i++){\n\t\t\twork[i] += work[i-1];\n\t\t\twork[i] %= MOD;\n\t\t\tdp[day+1][i%N] += work[i];\n\t\t\tdp[day+1][i%N] %= MOD;\n\t\t}\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 40, "memory_kb": 7132, "score_of_the_acc": -0.0138, "final_rank": 15 }, { "submission_id": "aoj_1567_2688060", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[12000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tvector<Info> V;\n\n\tfor(int i = 0; i < N; i += 2*K){\n\t\tV.push_back(Info(i,i+K-1));\n\t}\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\t\tfor(int i = 0; i < 2*N+2; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\t\t\t\twork[num+V[i].start] += dp[day][num];\n\t\t\t\twork[num+V[i].end+1] -= dp[day][num];\n\t\t\t}\n\t\t}\n\n\t\tdp[day+1][0] += work[0]%MOD;\n\t\tdp[day+1][0] %= MOD;\n\n\t\tfor(int i = 1; i < 2*N+2; i++){\n\t\t\twork[i] += work[i-1];\n\t\t\twork[i] %= MOD;\n\t\t\tdp[day+1][i%N] += work[i];\n\t\t\tdp[day+1][i%N] %= MOD;\n\t\t}\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 50, "memory_kb": 7180, "score_of_the_acc": -0.0245, "final_rank": 19 }, { "submission_id": "aoj_1567_2688055", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[12000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tvector<Info> V;\n\n\tfor(int i = 0; i < N; i += 2*K){\n\t\tV.push_back(Info(i,i+K-1));\n\t}\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\t\tfor(int i = 0; i < 2*N+2; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\t\t\t\twork[num+V[i].start] += dp[day][num];\n\t\t\t\twork[num+V[i].end+1] -= dp[day][num];\n\t\t\t}\n\t\t}\n\n\t\tdp[day+1][0] += work[0]%MOD;\n\n\t\tfor(int i = 1; i < 2*N+2; i++){\n\t\t\twork[i] += work[i-1];\n\t\t\twork[i] %= MOD;\n\t\t\tdp[day+1][i%N] += work[i];\n\t\t\tdp[day+1][i%N] %= MOD;\n\t\t}\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]%MOD);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 40, "memory_kb": 7116, "score_of_the_acc": -0.0137, "final_rank": 14 }, { "submission_id": "aoj_1567_2688051", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Info{\n\tInfo(int arg_start,int arg_end){\n\t\tstart = arg_start;\n\t\tend = arg_end;\n\t}\n\tint start,end;\n};\n\nll dp[5001][5001],work[12000];\n\n\nint main(){\n\n\tint N,M,K,Q;\n\n\tscanf(\"%d %d %d %d\",&N,&M,&K,&Q);\n\n\tvector<Info> V;\n\n\tfor(int i = 0; i < N; i += 2*K){\n\t\tV.push_back(Info(i,i+K-1));\n\t}\n\n\tfor(int day = 0; day <= M; day++){\n\t\tfor(int num = 0; num <= N-1; num++)dp[day][num] = 0;\n\t}\n\n\tdp[0][0] = 1;\n\n\tfor(int day = 0; day <= M-1; day++){\n\t\tfor(int i = 0; i < 2*N+2; i++)work[i] = 0;\n\n\t\tfor(int num = 0; num <= N-1; num++){\n\t\t\tif(dp[day][num] == 0)continue;\n\n\t\t\tfor(int i = 0; i < V.size(); i++){\n\t\t\t\twork[num+V[i].start] += dp[day][num];\n\t\t\t\twork[num+V[i].end+1] -= dp[day][num];\n\t\t\t}\n\t\t}\n\n\t\tdp[day+1][0] += work[0]%MOD;\n\n\t\tfor(int i = 1; i < 2*N+2; i++){\n\t\t\twork[i] += work[i-1];\n\t\t\twork[i] %= MOD;\n\t\t\tdp[day+1][i%N] += work[i];\n\t\t}\n\t}\n\n\tint a,b,c;\n\n\tfor(int loop = 0; loop < Q; loop++){\n\t\tscanf(\"%d %d %d\",&a,&b,&c);\n\n\t\tc = (c-a+N)%N;\n\n\t\tprintf(\"%lld\\n\",dp[b][c]);\n\t}\n\n\treturn 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 40, "memory_kb": 7084, "score_of_the_acc": -0.0135, "final_rank": 12 }, { "submission_id": "aoj_1567_2576367", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nll dp[5001][5001];\n \nint main() {\n ll n,m,k,q,mod=1000000007;\n cin >> n >> m >> k >> q;\n dp[0][0]=1;\n for(int i=0; i<n; i++) {\n if(i/k%2==0) dp[1][i]=1;\n }\n ll x=n/k/2,N=k*2;\n for(int i=1; i<m; i++) {\n ll sum=0;\n for(int j=N-k; j<N; j++) sum+=dp[i][j];\n for(int j=0; j<n; j++) {\n sum-=dp[i][(j-k+N)%N];\n sum+=dp[i][j];\n dp[i+1][j]=sum*x;\n dp[i+1][j]%=mod;\n }\n }\n while(q--) {\n int a,b,c;\n cin >> a >> b >> c;\n cout << dp[b][(c-a+n)%n] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 198456, "score_of_the_acc": -1.4044, "final_rank": 8 }, { "submission_id": "aoj_1567_1521987", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \n \n \nint mod = 1000000007;\nlong long dp[5010][5010];\n \nint main(){\n vector<int> mov;\n int N,K,M,Q;\n cin >> N >> M >> K >> Q;\n for(int i = 0 ; i < N ; i++){\n if( (i / K) % 2 == 0 ){\n\t\t\tmov.push_back(i);\n //cerr << i << endl;\n }\n }\n dp[0][0] = 1;\n for(int i = 0 ; i <= 5000 ; i++){\n\t\tint tot[5010] = {};\n\t\t\n\t\t// for(int k = 0 ; k < N ; k++){\n\t\t\t// printf(\"%3d\",dp[i][k]);\n\t\t\t// cout << \" \";\n\t\t// }\n\t\t// cout << endl;\n\t\t// for(int k = 0 ; k < N ; k++) cout << (k/K%2==0?\" ^\":\" \") << \" \";\n\t\t// cout << endl;\n\t\t\n\t\tfor(int k = 0 ; k < mov.size() ; k++){\n\t\t\ttot[0] += dp[i][mov[k]%N];\n\t\t\ttot[0] %= mod;\n\t\t}\n\t\t\n\t\tfor(int j = 1 ; j < 2*K ; j++){\n\t\t\ttot[j] = tot[j-1];\n\t\t\tint c = -1;\n\t\t\tfor(int k = 0 ; k < N ; k += K){\n\t\t\t\ttot[j] += c*dp[i][(k+j-1)%N];\n\t\t\t\ttot[j] %= mod;\n\t\t\t\tc *= -1;\n\t\t\t}\n\t\t}\n\t\t\n\t\t//reverse(tot,tot+2*K);\n\t\t// for(int j = 0 ; j < 2*K ; j++) cout << tot[j] << \" \" ;\n\t\t// cout << endl;\n\t\t\n\t\tfor(int j = 0 ; j < N ; j++){\n\t\t\tdp[i+1][j] += tot[(K+1+j)%(2*K)];\n\t\t\tdp[i+1][j] %= mod;\n\t\t}\n }\n for(int i = 0 ; i < Q ; i++){\n int a,b,c;\n cin >> a >> b >> c;\n int d = ((c-a)%N+N)%N;\n cout << (dp[b][d]%mod+mod)%mod << endl;\n }\n\t// for(int i = 0 ; i < 15 ; i++){\n\t\t// for(int j = 0 ; j < 15 ; j++){\n\t\t\t// printf(\"%10d \",(dp[i][j]+mod)%mod);\n\t\t// }\n\t\t// cout << endl;\n\t// }\n \n}", "accuracy": 1, "time_ms": 900, "memory_kb": 197012, "score_of_the_acc": -1.8866, "final_rank": 10 }, { "submission_id": "aoj_1567_1521613", "code_snippet": "#include <algorithm>\n#include <vector>\n#include <string>\n#include <iostream>\nnamespace lc {\ntemplate <int MOD>\nclass ModulusInteger {\npublic:\n\ttypedef ModulusInteger<MOD> self_type;\nprivate:\n\tint m_value;\n\tstatic self_type unsafe_construct(int x){\n\t\tself_type y;\n\t\ty.m_value = x;\n\t\treturn y;\n\t}\npublic:\n\tModulusInteger()\n\t\t: m_value(0)\n\t{ }\n\tModulusInteger(int x)\n\t\t: m_value(x % MOD)\n\t{\n\t\tif(m_value < 0){ m_value += MOD; }\n\t}\n\tint operator*() const { return m_value; }\n\tself_type &operator=(const self_type &x){\n\t\tm_value = x.m_value;\n\t\treturn *this;\n\t}\n\tself_type operator+(const self_type &x) const {\n\t\tconst int y = m_value + x.m_value;\n\t\treturn unsafe_construct(y >= MOD ? y - MOD : y);\n\t}\n\tself_type operator-(const self_type &x) const {\n\t\tconst int y = m_value - x.m_value;\n\t\treturn unsafe_construct(y < 0 ? y + MOD : y);\n\t}\n\tself_type operator*(const self_type &x) const {\n\t\treturn unsafe_construct(\n\t\t\tstatic_cast<long long>(m_value) * x.m_value % MOD);\n\t}\n\tself_type &operator+=(const self_type &x){ return (*this = *this + x); }\n\tself_type &operator-=(const self_type &x){ return (*this = *this - x); }\n\tself_type &operator*=(const self_type &x){ return (*this = *this * x); }\n};\ntemplate <int MOD>\nstd::ostream& operator<<(std::ostream &os, const ModulusInteger<MOD> &x){\n\tos << *x;\n return os;\n}\n}\nusing namespace std;\ntypedef lc::ModulusInteger<1000000007> mint;\nstatic mint dp[5001][5001];\nint main(){\n\tios_base::sync_with_stdio(false);\n\tint n, m, k, q;\n\tcin >> n >> m >> k >> q;\n\tdp[0][0] = 1;\n\tfor(int i = 0; i < m; ++i){\n\t\tfor(int j = 0; j < k; ++j){\n\t\t\tconst mint t = dp[i][j];\n\t\t\tdp[i + 1][j] += t;\n\t\t\tdp[i + 1][j + k] -= t;\n\t\t}\n\t\tfor(int j = k; j < k + k; ++j){\n\t\t\tconst mint t = dp[i][j];\n\t\t\tdp[i + 1][j] += t;\n\t\t\tdp[i + 1][k + k] -= t;\n\t\t\tdp[i + 1][0] += t;\n\t\t\tdp[i + 1][j - k] -= t;\n\t\t}\n\t\tfor(int j = 0; j < k + k; ++j){\n\t\t\tdp[i + 1][j + 1] += dp[i + 1][j];\n\t\t}\n\t}\n\tfor(int i = 0; i < q; ++i){\n\t\tint a, b, c;\n\t\tcin >> a >> b >> c;\n\t\tif(b == 0){\n\t\t\tcout << (a == c ? 1 : 0) << endl;\n\t\t}else{\n\t\t\ta %= (2 * k);\n\t\t\tc %= (2 * k);\n\t\t\tauto answer = dp[b][(c - a + 2 * k) % (2 * k)];\n\t\t\tif(b > 1){\n\t\t\t\tmint x = n / (k * 2), z = 1;\n\t\t\t\t--b;\n\t\t\t\twhile(b > 0){\n\t\t\t\t\tif(b & 1){ z *= x; }\n\t\t\t\t\tx *= x;\n\t\t\t\t\tb >>= 1;\n\t\t\t\t}\n\t\t\t\tanswer *= z;\n\t\t\t}\n\t\t\tcout << answer << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 98892, "score_of_the_acc": -0.6734, "final_rank": 1 } ]
aoj_1571_cpp
String Crossing Problem N 個の文字列{ S 1 , S 2 , ..., S N } が与えられる。 続いて Q 個のクエリが与えられる。 クエリの種類は以下の2つである。 a , b , c , d が入力され S a の b 文字目からの substring を S c の d -1文字までの substring の後に繋げる。これを新しい S c とする。 元の S c の d 文字目からの substring を S a の b -1文字までの substring の後に繋げる。これを新しい S a とする。 a , b , c が入力され S a の b 文字目を c に変更する。 例えば S 1 ="abcd", S 2 ="efgh"があり クエリの種類が1で a =1, b =2, c =2, d =3のとき a - -> bcd X ef - -> gh S 1 ="agh", S 2 ="efbcd"となる。 全てのクエリを処理した後の文字列 S 1 から S N を全て出力せよ。 Input 入力は以下の形式で与えられる。 N Q S 1 S 2 ... S N query 1 query 2 ... query Q 各 query は次のいずれかである。 1 a b c d or 2 a b c Constraints 1 ≤ N ≤ 10 5 1 ≤ Q ≤ 10 5 N 個の文字列の長さの合計は2×10 6 を越えない。 文字列 S i は全て英小文字であることが保証される。 クエリの種類が1のとき a ≠ c 1 ≤ b ≤ | S a | 1 ≤ d ≤ | S c | クエリの種類が2のとき 1 ≤ b ≤ | S a | c は英小文字であることが保証される。 高速な入出力を推奨する。 Output 各クエリを処理した後の文字列を S 1 から S N まで1行ずつ出力せよ。 S 1 S 2 ... S N Sample Input 1 2 1 abcd efgh 1 1 2 2 3 Sample Output 1 agh efbcd Sample Input 2 2 3 abcd efgh 1 1 2 2 3 2 1 3 x 2 2 4 x Sample Output 2 agx efbxd Sample Input 3 10 10 sjcvpauokb fmeaowomscy sepeqqfcosrjmonfsv zapc aromazjzqoeiqswvcaf clifpa dusudcz qeqdzdtdzlkhc gkpsjvdvadmf xrtyxnkolluagwxp 1 4 4 6 3 1 7 1 8 1 2 2 6 o 1 4 4 3 7 2 1 2 i 1 6 3 3 2 1 6 5 1 9 2 10 9 j 1 2 2 7 3 2 3 2 b Sample Output 3 sicvpauoeqqifpa fqdzdtdzlkhc sb zapfcosrjmonfsv aromazjzqoeiqswvcaf clepkb qemeaooomscy dusudcz gkpsjvdvadmf xrtyxnkojluagwxp
[ { "submission_id": "aoj_1571_10349818", "code_snippet": "// AOJ #1571 String Crossing\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\n#include <ext/rope>\nusing namespace __gnu_cxx;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nchar temp[2000005];\nstring Cins() { // 文字列の入力 スペース以下の文字で入力終了\n char *s = temp;\n\tdo *s = gc();\n\twhile (*s++ > ' ');\n\t*(s-1) = 0;\n string input(temp); // 文字配列からstringを作成\n return input;\n}\n\nint main(){\n int n = Cin(), q = Cin();\n rope<char>* A = new rope<char>[n];\n for(int i = 0; i < n; i++){\n string s = Cins();\n A[i] = rope<char>(s.c_str());\n }\n\n while(q--){\n int t = gc() & 0xf; gc();\n if(t == 1){\n int a = Cin()-1, b = Cin()-1, c = Cin()-1, d = Cin()-1;\n rope<char> L_a = A[a].substr(0, b);\n rope<char> R_a = A[a].substr(b, A[a].size() - b);\n rope<char> L_c = A[c].substr(0, d);\n rope<char> R_c = A[c].substr(d, A[c].size() - d);\n A[c] = L_c + R_a;\n A[a] = L_a + R_c;\n } else {\n int a = Cin()-1, b = Cin()-1;\n char ch = gc(); gc();\n A[a].replace(b, 1, &ch, 1);\n }\n }\n for (int i = 0; i < n; i++){\n for (int j = 0; j < A[i].size(); j++) pc(A[i][j]);\n pc('\\n');\n }\n// delete[] A;\n return 0;\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 47016, "score_of_the_acc": -0.6904, "final_rank": 3 }, { "submission_id": "aoj_1571_10349815", "code_snippet": "// AOJ #1571 String Crossing\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\n#include <ext/rope>\nusing namespace __gnu_cxx;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nchar temp[2000005];\nstring Cins() { // 文字列の入力 スペース以下の文字で入力終了\n char *s = temp;\n\tdo *s = gc();\n\twhile (*s++ > ' ');\n\t*(s-1) = 0;\n string input(temp); // 文字配列からstringを作成\n return input;\n}\n\nint main(){\n int n = Cin(), q = Cin();\n rope<char>* A = new rope<char>[n];\n for (int i = 0; i < n; i++){\n string s = Cins();\n A[i] = rope<char>(s.c_str());\n }\n\n while(q--){\n int t = gc() & 0xf; gc();\n if(t == 1){\n int a = Cin()-1, b = Cin()-1, c = Cin()-1, d = Cin()-1;\n rope<char> X = A[a].substr(b, A[a].size() - b);\n rope<char> Y = A[c].substr(d, A[c].size() - d);\n A[a].erase(b, A[a].size() - b);\n A[c].erase(d, A[c].size() - d);\n A[a].append(Y);\n A[c].append(X);\n } else {\n int a = Cin()-1, b = Cin()-1;\n char ch = gc(); gc();\n A[a].replace(b, 1, &ch, 1);\n }\n }\n\n for (int i = 0; i < n; i++){\n for (int j = 0; j < A[i].size(); j++) pc(A[i][j]);\n pc('\\n');\n }\n// delete[] A;\n return 0;\n}", "accuracy": 1, "time_ms": 680, "memory_kb": 46992, "score_of_the_acc": -0.7062, "final_rank": 4 }, { "submission_id": "aoj_1571_10349803", "code_snippet": "// AOJ #1571 String Crossing\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\n#include <ext/rope>\nusing namespace __gnu_cxx;\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int n, q;\n cin >> n >> q;\n rope<char>* A = new rope<char>[n];\n for(int i = 0; i < n; i++){\n string s;\n cin >> s;\n A[i] = rope<char>(s.c_str());\n }\n\n while(q--){\n int t;\n cin >> t;\n if(t == 1){\n int a, b, c, d;\n cin >> a >> b >> c >> d;\n a--; c--;\n rope<char> L_a = A[a].substr(0, b-1);\n rope<char> R_a = A[a].substr(b-1, A[a].size() - (b-1));\n rope<char> L_c = A[c].substr(0, d-1);\n rope<char> R_c = A[c].substr(d-1, A[c].size() - (d-1));\n A[c] = L_c + R_a;\n A[a] = L_a + R_c;\n } else {\n int a, b;\n char ch;\n cin >> a >> b >> ch;\n a--;\n A[a].replace(b-1, 1, &ch, 1);\n }\n }\n for (int i = 0; i < n; i++){\n for (int j = 0; j < A[i].size(); j++) cout << A[i][j];\n cout << endl;\n }\n// delete[] A;\n return 0;\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 46044, "score_of_the_acc": -0.7259, "final_rank": 5 }, { "submission_id": "aoj_1571_3111203", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nunsigned int y=11451419;\n\nint rand_int(int l=0,int r=11451419){\n\ty=y^(y<<13);\n\ty=y^(y>>17);\n\ty=y^(y<<5);\n\treturn y%(r-l+1)+l;\n}\n\n\ntemplate<class T>\nstruct treap{\n\tstruct node_t{\n\t\tT val; // 値\n\t\tnode_t *lch, *rch;\n\t\tint pri; // 優先度\n\t\tint cnt; // 部分木のサイズ\n\t\tT sum; // 部分木の値の和\n\t\tnode_t(T v, int p):val(v), pri(p), cnt(1), sum(v){\n\t\t\tlch = rch = NULL;\n\t\t}\n\t};\n\tnode_t *root;\n\ttreap():root(NULL){}\n\n\tint count(node_t *t){ return t ? t->cnt : 0; }\n\tT sum(node_t *t){ return t ? t->sum : 0; }\n\tnode_t *update(node_t *t){\n \tt->cnt = count(t->lch) + count(t->rch) + 1;\n \tt->sum = sum(t->lch) + sum(t->rch) + t->val;\n \treturn t;\n }\n\n //木rを木lの後に追加する\n node_t *merge(node_t *l, node_t *r){\n \tif(!l || !r) return l?l:r;\n if(l->pri > r->pri){ // 左の部分木の根のほうが優先度が高い\n \tl->rch = merge(l->rch, r);\n \treturn update(l);\n }else{\n \tr->lch = merge(l, r->lch);\n \treturn update(r);\n }\n }\n\n pair<node_t*, node_t*> split(node_t *t, int k){ // [0,k), [k,n)\n \tif(!t) return {NULL,NULL};\n \tif(k <= count(t->lch)){\n \t\tpair<node_t*,node_t*> s = split(t->lch,k);\n \t\tt->lch = s.second;\n \t\treturn {s.first, update(t)};\n \t}else{\n \t\tpair<node_t*,node_t*> s = split(t->rch, k-count(t->lch)-1);\n \t\tt->rch = s.first;\n \t\treturn {update(t), s.second};\n \t}\n }\n\n node_t *insert(node_t *t, int k, T val, int pri){\n \tpair<node_t*,node_t*> s = split(t,k);\n \tt = merge(s.first, new node_t(val,pri));\n \tt = merge(t, s.second);\n \treturn update(t);\n }\n\n \t// eraseによってsiz=0になるとバグって使えなくなるので注意\n node_t *erase(node_t *t, int k){\n \tpair<node_t*,node_t*> s2 = split(t,k+1);\n \tpair<node_t*,node_t*> s1 = split(s2.first, k);\n \tt = merge(s1.first, s2.second);\n \treturn update(t);\n }\n\n node_t *find(node_t *t, int k){\n \tint c = count(t->lch);\n \tif(k < c) return find(t->lch,k);\n \tif(k == c) return t;\n \treturn find(t->rch, k-c-1);\n }\n\n void insert(int k, T val){ root = insert(root, k, val, rand_int()); }\n void erase(int k){ root = erase(root, k); }\n node_t *find(int k){ return find(root,k); }\n\n};\n\ntreap<char> tr[100005];\nint n,q;\n\nint main(void){\n\tscanf(\"%d%d\",&n,&q);\n\tfor(int i=0;i<n;i++){\n\t\tstring str;\n\t\tcin >> str;\n\t\tfor(int j=0;j<str.size();j++){\n\t\t\ttr[i].insert(j,str[j]);\n\t\t}\n\t}\n\tfor(int i=0;i<q;i++){\n\t\tint type;\n\t\tscanf(\"%d\",&type);\n\t\tif(type==1){\n\t\t\tint a,b,c,d;\n\t\t\tscanf(\"%d%d%d%d\",&a,&b,&c,&d);\n\t\t\ta--;\n\t\t\tb--;\n\t\t\tc--;\n\t\t\td--;\n\t\t\tauto sa=tr[a].split(tr[a].root,b);\n\t\t\tauto sc=tr[c].split(tr[c].root,d);\n\t\t\ttr[a].root=tr[a].merge(sa.first,sc.second);\n\t\t\ttr[c].root=tr[c].merge(sc.first,sa.second);\n\t\t}else{\n\t\t\tint a,b;\n\t\t\tchar c;\n\t\t\tscanf(\"%d%d%*c%c%*c\",&a,&b,&c);\n\t\t\ta--;\n\t\t\tb--;\n\t\t\ttr[a].insert(b,c);\n\t\t\ttr[a].erase(b+1);\n\t\t}\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tint v=tr[i].count(tr[i].root);\n\t\tfor(int j=0;j<v;j++){\n\t\t\tprintf(\"%c\",tr[i].find(j)->val);\n\t\t}\n\t\tprintf(\"\\n\");\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 102460, "score_of_the_acc": -1, "final_rank": 9 }, { "submission_id": "aoj_1571_3086505", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\ntemplate<class T>\nstruct treap{\n struct node_t{\n T val; // 値\n node_t *lch, *rch;\n int pri; // 優先度\n int cnt; // 部分木のサイズ\n // T sum; // 部分木の値の和\n // T mn; // 部分木の最小値\n\n node_t(T v, int p):val(v), pri(p), cnt(1){\n lch = rch = NULL;\n }\n };\n\n const T INF = 19191919; // TODO:initialize\n mt19937 mt;\n node_t *root;\n treap():root(NULL){\n const int seed = 364364;\n mt = mt19937(seed);\n }\n\n int count(node_t *t){ return t ? t->cnt : 0; }\n // T sum(node_t *t){ return t ? t->sum : 0; }\n // T min(node_t *t){ return t ? t->mn : INF; }\n\n node_t *update(node_t *t){\n t->cnt = count(t->lch) + count(t->rch) + 1;\n // t->sum = sum(t->lch) + sum(t->rch) + t->val;\n // t->mn = std::min({min(t->lch), min(t->rch), t->val});\n return t;\n }\n\n node_t *merge(node_t *l, node_t *r){\n if(!l || !r) return l?l:r;\n\n if(l->pri > r->pri){ // 左の部分木の根のほうが優先度が高い\n l->rch = merge(l->rch, r);\n return update(l);\n }\n else{\n r->lch = merge(l, r->lch);\n return update(r);\n }\n }\n\n pair<node_t*, node_t*> split(node_t *t, int k){ // [0,k), [k,n)\n if(!t) return {NULL,NULL};\n if(k <= count(t->lch)){\n auto s = split(t->lch,k);\n t->lch = s.second;\n return {s.first, update(t)};\n }\n else{\n auto s = split(t->rch, k-count(t->lch)-1);\n t->rch = s.first;\n return {update(t), s.second};\n }\n }\n\n node_t *insert(node_t *t, int k, T val, int pri){\n auto s = split(t,k);\n t = merge(s.first, new node_t(val,pri));\n t = merge(t, s.second);\n return update(t);\n }\n\n node_t *erase(node_t *t, int k){\n auto s2 = split(t,k+1);\n auto s1 = split(s2.first, k);\n t = merge(s1.first, s2.second);\n return update(t);\n }\n\n node_t *find(node_t *t, int k){\n int c = count(t->lch);\n if(k < c) return find(t->lch,k);\n if(k == c) return t;\n return find(t->rch, k-c-1);\n }\n\n void insert(int k, T val){ root = insert(root, k, val, mt()); }\n void erase(int k){ root = erase(root, k); }\n node_t *find(int k){ return find(root,k); }\n\n // // circular shift (右端の値が左端に来て、それ以外が右に1つずつシフト)\n // void shift(int l, int r){\n // if(r-l==1) return;\n // assert(l<r);\n // auto sr = split(root, r);\n // auto sl = split(sr.first, l);\n // auto lr = split(sl.second, r-l-1);\n // root = merge(merge(sl.first, merge(lr.second, lr.first)), sr.second);\n // }\n\n // // [l,r)\n // T min(int l, int r){\n // assert(l<r);\n // auto sr = split(root, r);\n // auto sl = split(sr.first, l);\n // auto lr = sl.second;\n // T ret = min(lr);\n // // 元に戻す\n // root = merge(merge(sl.first, lr), sr.second);\n // return ret;\n // }\n};\n\nint N,Q;\nvector<treap<int> > ts;\n\nint main(){\n scanf(\"%d %d \",&N,&Q);\n ts.resize(N);\n rep(i,N){\n ts[i].insert(0,-1);\n int j=1;\n while(1){\n char c;\n scanf(\"%c\",&c);\n if(c=='\\n')break;\n ts[i].insert(j++,c-'a');\n }\n }\n\n while(Q--){\n int tp;\n scanf(\" %d\",&tp);\n if(tp==1){\n int a,b,c,d;\n scanf(\" %d %d %d %d\",&a,&b,&c,&d);\n a--;c--;\n\n auto pa=ts[0].split(ts[a].root,b);\n auto pc=ts[0].split(ts[c].root,d);\n\n ts[a].root=ts[0].merge(pa.fi,pc.se);\n ts[c].root=ts[0].merge(pc.fi,pa.se);\n }else{\n int a,b;\n char c;\n scanf(\" %d %d %c\",&a,&b,&c);\n a--;\n\n ts[a].erase(b);\n ts[a].insert(b,c-'a');\n }\n }\n\n rep(i,N){\n int sz=ts[i].count(ts[i].root);\n rep(j,sz-1)cout<<(char)(ts[i].find(j+1)->val+'a');\n cout<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 760, "memory_kb": 148288, "score_of_the_acc": -1.2048, "final_rank": 14 }, { "submission_id": "aoj_1571_3086486", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\ntemplate<class T>\nstruct treap{\n struct node_t{\n T val; // 値\n node_t *lch, *rch;\n int pri; // 優先度\n int cnt; // 部分木のサイズ\n // T sum; // 部分木の値の和\n // T mn; // 部分木の最小値\n\n node_t(T v, int p):val(v), pri(p), cnt(1){\n lch = rch = NULL;\n }\n };\n\n const T INF = 19191919; // TODO:initialize\n mt19937 mt;\n node_t *root;\n treap():root(NULL){\n const int seed = 364364;\n mt = mt19937(seed);\n }\n\n int count(node_t *t){ return t ? t->cnt : 0; }\n // T sum(node_t *t){ return t ? t->sum : 0; }\n // T min(node_t *t){ return t ? t->mn : INF; }\n\n node_t *update(node_t *t){\n t->cnt = count(t->lch) + count(t->rch) + 1;\n // t->sum = sum(t->lch) + sum(t->rch) + t->val;\n // t->mn = std::min({min(t->lch), min(t->rch), t->val});\n return t;\n }\n\n node_t *merge(node_t *l, node_t *r){\n if(!l || !r) return l?l:r;\n\n if(l->pri > r->pri){ // 左の部分木の根のほうが優先度が高い\n l->rch = merge(l->rch, r);\n return update(l);\n }\n else{\n r->lch = merge(l, r->lch);\n return update(r);\n }\n }\n\n pair<node_t*, node_t*> split(node_t *t, int k){ // [0,k), [k,n)\n if(!t) return {NULL,NULL};\n if(k <= count(t->lch)){\n auto s = split(t->lch,k);\n t->lch = s.second;\n return {s.first, update(t)};\n }\n else{\n auto s = split(t->rch, k-count(t->lch)-1);\n t->rch = s.first;\n return {update(t), s.second};\n }\n }\n\n node_t *insert(node_t *t, int k, T val, int pri){\n auto s = split(t,k);\n t = merge(s.first, new node_t(val,pri));\n t = merge(t, s.second);\n return update(t);\n }\n\n node_t *erase(node_t *t, int k){\n auto s2 = split(t,k+1);\n auto s1 = split(s2.first, k);\n t = merge(s1.first, s2.second);\n return update(t);\n }\n\n node_t *find(node_t *t, int k){\n int c = count(t->lch);\n if(k < c) return find(t->lch,k);\n if(k == c) return t;\n return find(t->rch, k-c-1);\n }\n\n void insert(int k, T val){ root = insert(root, k, val, mt()); }\n void erase(int k){ root = erase(root, k); }\n node_t *find(int k){ return find(root,k); }\n\n // // circular shift (右端の値が左端に来て、それ以外が右に1つずつシフト)\n // void shift(int l, int r){\n // if(r-l==1) return;\n // assert(l<r);\n // auto sr = split(root, r);\n // auto sl = split(sr.first, l);\n // auto lr = split(sl.second, r-l-1);\n // root = merge(merge(sl.first, merge(lr.second, lr.first)), sr.second);\n // }\n\n // // [l,r)\n // T min(int l, int r){\n // assert(l<r);\n // auto sr = split(root, r);\n // auto sl = split(sr.first, l);\n // auto lr = sl.second;\n // T ret = min(lr);\n // // 元に戻す\n // root = merge(merge(sl.first, lr), sr.second);\n // return ret;\n // }\n};\n\nint N,Q;\nvector<treap<int> > ts;\n\nint main(){\n scanf(\"%d %d \",&N,&Q);\n ts.resize(N);\n rep(i,N){\n ts[i].insert(0,-1);\n int j=1;\n while(1){\n char c;\n scanf(\"%c\",&c);\n if(c=='\\n')break;\n ts[i].insert(j++,c-'a');\n }\n }\n\n while(Q--){\n int tp;\n scanf(\" %d\",&tp);\n if(tp==1){\n int a,b,c,d;\n scanf(\" %d %d %d %d\",&a,&b,&c,&d);\n a--;c--;\n\n auto pa=ts[a].split(ts[a].root,b);\n auto pc=ts[c].split(ts[c].root,d);\n\n ts[a].root=ts[c].merge(pa.fi,pc.se);\n ts[c].root=ts[c].merge(pc.fi,pa.se);\n }else{\n int a,b;\n char c;\n scanf(\" %d %d %c\",&a,&b,&c);\n a--;\n\n ts[a].erase(b);\n ts[a].insert(b,c-'a');\n }\n }\n\n rep(i,N){\n int sz=ts[i].count(ts[i].root);\n rep(j,sz-1)cout<<(char)(ts[i].find(j+1)->val+'a');\n cout<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 760, "memory_kb": 148328, "score_of_the_acc": -1.2049, "final_rank": 15 }, { "submission_id": "aoj_1571_3086483", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\ntemplate<class T>\nstruct treap{\n struct node_t{\n T val; // 値\n node_t *lch, *rch;\n int pri; // 優先度\n int cnt; // 部分木のサイズ\n // T sum; // 部分木の値の和\n // T mn; // 部分木の最小値\n\n node_t(T v, int p):val(v), pri(p), cnt(1){\n lch = rch = NULL;\n }\n };\n\n const T INF = 19191919; // TODO:initialize\n mt19937 mt;\n node_t *root;\n treap():root(NULL){\n const int seed = 364364;\n mt = mt19937(seed);\n }\n\n int count(node_t *t){ return t ? t->cnt : 0; }\n // T sum(node_t *t){ return t ? t->sum : 0; }\n // T min(node_t *t){ return t ? t->mn : INF; }\n\n node_t *update(node_t *t){\n t->cnt = count(t->lch) + count(t->rch) + 1;\n // t->sum = sum(t->lch) + sum(t->rch) + t->val;\n // t->mn = std::min({min(t->lch), min(t->rch), t->val});\n return t;\n }\n\n node_t *merge(node_t *l, node_t *r){\n if(!l || !r) return l?l:r;\n\n if(l->pri > r->pri){ // 左の部分木の根のほうが優先度が高い\n l->rch = merge(l->rch, r);\n return update(l);\n }\n else{\n r->lch = merge(l, r->lch);\n return update(r);\n }\n }\n\n pair<node_t*, node_t*> split(node_t *t, int k){ // [0,k), [k,n)\n if(!t) return {NULL,NULL};\n if(k <= count(t->lch)){\n auto s = split(t->lch,k);\n t->lch = s.second;\n return {s.first, update(t)};\n }\n else{\n auto s = split(t->rch, k-count(t->lch)-1);\n t->rch = s.first;\n return {update(t), s.second};\n }\n }\n\n node_t *insert(node_t *t, int k, T val, int pri){\n auto s = split(t,k);\n t = merge(s.first, new node_t(val,pri));\n t = merge(t, s.second);\n return update(t);\n }\n\n node_t *erase(node_t *t, int k){\n auto s2 = split(t,k+1);\n auto s1 = split(s2.first, k);\n t = merge(s1.first, s2.second);\n return update(t);\n }\n\n node_t *find(node_t *t, int k){\n int c = count(t->lch);\n if(k < c) return find(t->lch,k);\n if(k == c) return t;\n return find(t->rch, k-c-1);\n }\n\n void insert(int k, T val){ root = insert(root, k, val, mt()); }\n void erase(int k){ root = erase(root, k); }\n node_t *find(int k){ return find(root,k); }\n\n // // circular shift (右端の値が左端に来て、それ以外が右に1つずつシフト)\n // void shift(int l, int r){\n // if(r-l==1) return;\n // assert(l<r);\n // auto sr = split(root, r);\n // auto sl = split(sr.first, l);\n // auto lr = split(sl.second, r-l-1);\n // root = merge(merge(sl.first, merge(lr.second, lr.first)), sr.second);\n // }\n\n // // [l,r)\n // T min(int l, int r){\n // assert(l<r);\n // auto sr = split(root, r);\n // auto sl = split(sr.first, l);\n // auto lr = sl.second;\n // T ret = min(lr);\n // // 元に戻す\n // root = merge(merge(sl.first, lr), sr.second);\n // return ret;\n // }\n};\n\nint N,Q;\nvector<treap<int> > ts;\n\nint main(){\n scanf(\"%d %d \",&N,&Q);\n ts.resize(N);\n rep(i,N){\n ts[i].insert(0,-1);\n int j=1;\n while(1){\n char c;\n scanf(\"%c\",&c);\n if(c=='\\n')break;\n ts[i].insert(j++,c-'a');\n }\n }\n\n while(Q--){\n int tp;\n scanf(\" %d\",&tp);\n if(tp==1){\n int a,b,c,d;\n scanf(\" %d %d %d %d\",&a,&b,&c,&d);\n a--;c--;\n\n auto pa=ts[a].split(ts[a].root,b);\n auto pc=ts[c].split(ts[c].root,d);\n\n ts[a].root=ts[a].merge(pa.fi,pc.se);\n ts[c].root=ts[c].merge(pc.fi,pa.se);\n }else{\n int a,b;\n char c;\n scanf(\" %d %d %c\",&a,&b,&c);\n a--;\n\n ts[a].erase(b);\n ts[a].insert(b,c-'a');\n }\n }\n\n rep(i,N){\n int sz=ts[i].count(ts[i].root);\n rep(j,sz-1)cout<<(char)(ts[i].find(j+1)->val+'a');\n cout<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 740, "memory_kb": 148312, "score_of_the_acc": -1.189, "final_rank": 13 }, { "submission_id": "aoj_1571_3078625", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\ntemplate<class T>\nstruct treap{\n struct node_t{\n T val; // 値\n node_t *lch, *rch;\n int pri; // 優先度\n int cnt; // 部分木のサイズ\n\n node_t(T v, int p):val(v), pri(p), cnt(1){\n lch = rch = NULL;\n }\n };\n\n const T INF = 19191919; // TODO:initialize\n mt19937 mt;\n node_t *root;\n treap():root(NULL){\n const int seed = 364364;\n mt = mt19937(seed);\n }\n\n int count(node_t *t){ return t ? t->cnt : 0; }\n\n node_t *update(node_t *t){\n t->cnt = count(t->lch) + count(t->rch) + 1;\n return t;\n }\n\n node_t *merge(node_t *l, node_t *r){\n if(!l || !r) return l?l:r;\n\n if(l->pri > r->pri){ // 左の部分木の根のほうが優先度が高い\n l->rch = merge(l->rch, r);\n return update(l);\n }\n else{\n r->lch = merge(l, r->lch);\n return update(r);\n }\n }\n\n pair<node_t*, node_t*> split(node_t *t, int k){ // [0,k), [k,n)\n if(!t) return {NULL,NULL};\n if(k <= count(t->lch)){\n auto s = split(t->lch,k);\n t->lch = s.second;\n return {s.first, update(t)};\n }\n else{\n auto s = split(t->rch, k-count(t->lch)-1);\n t->rch = s.first;\n return {update(t), s.second};\n }\n }\n\n node_t *insert(node_t *t, int k, T val, int pri){\n auto s = split(t,k);\n t = merge(s.first, new node_t(val,pri));\n t = merge(t, s.second);\n return update(t);\n }\n\n node_t *erase(node_t *t, int k){\n auto s2 = split(t,k+1);\n auto s1 = split(s2.first, k);\n t = merge(s1.first, s2.second);\n return update(t);\n }\n\n node_t *find(node_t *t, int k){\n int c = count(t->lch);\n if(k < c) return find(t->lch,k);\n if(k == c) return t;\n return find(t->rch, k-c-1);\n }\n\n void insert(int k, T val){ root = insert(root, k, val, mt()); }\n void erase(int k){ root = erase(root, k); }\n node_t *find(int k){ return find(root,k); }\n};\n\nvector<treap<int>> t;\n\ninline void read(int i){\n t[i].insert(0,'#');\n int j=1;\n while(1){\n char c;\n scanf(\"%c\", &c);\n if(c=='\\n') break;\n t[i].insert(j++, c-'a');\n }\n}\n\nvoid CHANGE(int a, int b, int c, int d){\n auto pa = t[a].split(t[a].root, b);\n auto pc = t[c].split(t[c].root, d);\n\n t[a].root = t[a].merge(pa.fi, pc.se);\n t[c].root = t[c].merge(pc.fi, pa.se);\n}\n\nvoid UPDATE(int a, int b, int c){\n t[a].erase(b);\n t[a].insert(b,c);\n}\n\nint main(){\n int n,q;\n scanf(\"%d %d \", &n, &q);\n\n t.resize(n);\n rep(i,n) read(i);\n\n // query\n rep(i,q){\n int tp;\n scanf(\" %d\", &tp);\n if(tp==1){\n int a,b,c,d;\n scanf(\" %d %d %d %d\", &a, &b, &c, &d);\n --a; --c;\n CHANGE(a,b,c,d);\n }\n else{\n int a,b;\n char c;\n scanf(\" %d %d %c\", &a, &b, &c);\n --a;\n UPDATE(a,b,c-'a');\n }\n }\n\n // 復元\n rep(i,n){\n int sz = t[i].count(t[i].root);\n rep(j,sz-1) printf(\"%c\", t[i].find(j+1)->val+'a');\n printf(\"\\n\");\n }\n return 0;\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 148308, "score_of_the_acc": -1.1572, "final_rank": 11 }, { "submission_id": "aoj_1571_3078152", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\ntemplate<class T>\nstruct treap{\n struct node_t{\n T val; // 値\n node_t *lch, *rch;\n int pri; // 優先度\n int cnt; // 部分木のサイズ\n // T sum; // 部分木の値の和\n // T mn; // 部分木の最小値\n\n node_t(T v, int p):val(v), pri(p), cnt(1){\n lch = rch = NULL;\n }\n };\n\n const T INF = 19191919; // TODO:initialize\n mt19937 mt;\n node_t *root;\n treap():root(NULL){\n const int seed = 364364;\n mt = mt19937(seed);\n }\n\n int count(node_t *t){ return t ? t->cnt : 0; }\n // T sum(node_t *t){ return t ? t->sum : 0; }\n // T min(node_t *t){ return t ? t->mn : INF; }\n\n node_t *update(node_t *t){\n t->cnt = count(t->lch) + count(t->rch) + 1;\n // t->sum = sum(t->lch) + sum(t->rch) + t->val;\n // t->mn = std::min({min(t->lch), min(t->rch), t->val});\n return t;\n }\n\n node_t *merge(node_t *l, node_t *r){\n if(!l || !r) return l?l:r;\n\n if(l->pri > r->pri){ // 左の部分木の根のほうが優先度が高い\n l->rch = merge(l->rch, r);\n return update(l);\n }\n else{\n r->lch = merge(l, r->lch);\n return update(r);\n }\n }\n\n pair<node_t*, node_t*> split(node_t *t, int k){ // [0,k), [k,n)\n if(!t) return {NULL,NULL};\n if(k <= count(t->lch)){\n auto s = split(t->lch,k);\n t->lch = s.second;\n return {s.first, update(t)};\n }\n else{\n auto s = split(t->rch, k-count(t->lch)-1);\n t->rch = s.first;\n return {update(t), s.second};\n }\n }\n\n node_t *insert(node_t *t, int k, T val, int pri){\n auto s = split(t,k);\n t = merge(s.first, new node_t(val,pri));\n t = merge(t, s.second);\n return update(t);\n }\n\n node_t *erase(node_t *t, int k){\n auto s2 = split(t,k+1);\n auto s1 = split(s2.first, k);\n t = merge(s1.first, s2.second);\n return update(t);\n }\n\n node_t *find(node_t *t, int k){\n int c = count(t->lch);\n if(k < c) return find(t->lch,k);\n if(k == c) return t;\n return find(t->rch, k-c-1);\n }\n\n void insert(int k, T val){ root = insert(root, k, val, mt()); }\n void erase(int k){ root = erase(root, k); }\n node_t *find(int k){ return find(root,k); }\n\n // // circular shift (右端の値が左端に来て、それ以外が右に1つずつシフト)\n // void shift(int l, int r){\n // if(r-l==1) return;\n // assert(l<r);\n // auto sr = split(root, r);\n // auto sl = split(sr.first, l);\n // auto lr = split(sl.second, r-l-1);\n // root = merge(merge(sl.first, merge(lr.second, lr.first)), sr.second);\n // }\n\n // // [l,r)\n // T min(int l, int r){\n // assert(l<r);\n // auto sr = split(root, r);\n // auto sl = split(sr.first, l);\n // auto lr = sl.second;\n // T ret = min(lr);\n // // 元に戻す\n // root = merge(merge(sl.first, lr), sr.second);\n // return ret;\n // }\n};\n\nvector<treap<int>> t;\n\ninline void read(int i){\n t[i].insert(0,'#');\n int j=1;\n while(1){\n char c;\n scanf(\"%c\", &c);\n if(c=='\\n') break;\n t[i].insert(j++, c-'a');\n }\n}\n\nvoid CHANGE(int a, int b, int c, int d){\n auto pa = t[a].split(t[a].root, b);\n auto pc = t[c].split(t[c].root, d);\n\n t[a].root = t[a].merge(pa.fi, pc.se);\n t[c].root = t[c].merge(pc.fi, pa.se);\n}\n\nvoid UPDATE(int a, int b, int c){\n t[a].erase(b);\n t[a].insert(b,c);\n}\n\nint main(){\n int n,q;\n scanf(\"%d %d \", &n, &q);\n\n t.resize(n);\n rep(i,n) read(i);\n\n // query\n rep(i,q){\n int tp;\n scanf(\" %d\", &tp);\n if(tp==1){\n int a,b,c,d;\n scanf(\" %d %d %d %d\", &a, &b, &c, &d);\n --a; --c;\n // --b; --d;\n CHANGE(a,b,c,d);\n }\n else{\n int a,b;\n char c;\n scanf(\" %d %d %c\", &a, &b, &c);\n --a;\n // --b;\n UPDATE(a,b,c-'a');\n }\n }\n\n // 復元\n rep(i,n){\n int sz = t[i].count(t[i].root);\n // dbg(sz);\n rep(j,sz-1) printf(\"%c\", t[i].find(j+1)->val+'a');\n printf(\"\\n\");\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 148344, "score_of_the_acc": -1.1733, "final_rank": 12 }, { "submission_id": "aoj_1571_3078034", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, a, n) for (ll i = ((ll) a); i < ((ll) n); i++)\nusing namespace std;\ntypedef long long ll;\n\nstruct seg {\n ll idx, l, r, next;\n};\n\nint main(void) {\n ll N, Q;\n cin >> N >> Q;\n vector<string> S(N);\n REP(i, 0, N) cin >> S[i];\n\n vector<seg> segments(5 * 100000);\n ll s = 0;\n\n vector<ll> head(N);\n REP(i, 0, N) {\n segments[s].idx = i;\n segments[s].l = 0;\n segments[s].r = S[i].length();\n segments[s].next = -1;\n head[i] = s++;\n }\n\n REP(i, 0, Q) {\n ll T;\n cin >> T;\n\n if (T == 1) {\n ll A, B, C, D;\n cin >> A >> B >> C >> D, A--, B--, C--, D--;\n\n ll a = head[A], b = B;\n while (b >= segments[a].r - segments[a].l) {\n b -= segments[a].r - segments[a].l;\n a = segments[a].next;\n }\n\n ll c = head[C], d = D;\n while (d >= segments[c].r - segments[c].l) {\n d -= segments[c].r - segments[c].l;\n c = segments[c].next;\n }\n\n ll segA = s++, segC = s++;\n segments[segA].idx = segments[a].idx;\n segments[segA].r = segments[a].r;\n segments[segA].l = segments[a].l + b;\n segments[segA].next = segments[a].next;\n segments[segC].idx = segments[c].idx;\n segments[segC].r = segments[c].r;\n segments[segC].l = segments[c].l + d;\n segments[segC].next = segments[c].next;\n\n segments[a].r = segments[segA].l;\n segments[a].next = segC;\n segments[c].r = segments[segC].l;\n segments[c].next = segA;\n }\n\n if (T == 2) {\n ll A, B;\n char C;\n cin >> A >> B >> C, A--, B--;\n\n ll a = head[A], b = B;\n while (b >= segments[a].r - segments[a].l) {\n b -= segments[a].r - segments[a].l;\n a = segments[a].next;\n }\n\n S[segments[a].idx][segments[a].l + b] = C;\n }\n\n if ((i + 1) % 310 == 0 || i + 1 == Q) {\n vector<string> T(N);\n REP(j, 0, N) {\n T[j] = \"\";\n ll a = head[j];\n while (a != -1) {\n T[j] += S[segments[a].idx].substr(segments[a].l, segments[a].r - segments[a].l);\n a = segments[a].next;\n }\n }\n S = T;\n\n s = 0;\n REP(j, 0, N) {\n segments[s].idx = j;\n segments[s].l = 0;\n segments[s].r = S[j].length();\n segments[s].next = -1;\n head[j] = s++;\n }\n }\n }\n\n REP(i, 0, N) cout << S[i] << endl;\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 30320, "score_of_the_acc": -0.6902, "final_rank": 2 }, { "submission_id": "aoj_1571_1526778", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef complex<double> P;\n#define REP(i,n) for(int i=0;i<n;++i)\n#define REPR(i,n) for(int i=1;i<n;++i)\n\n#define DEBUG(x) cout<<#x<<\": \"<<x<<endl\n#define DEBUG_VEC(v) REP(i,v.size())cout<<#v<<\"[\"<<i<<\"]: \"<<v[i]<<endl\n#define ALL(a) (a).begin(),(a).end()\n\n// ??????????????¨????????????\n\n// ????????°???????????°?????????????????§?????????????§???? 2????????????????????¢?´¢??¨??¨???\n// http://www.slideshare.net/iwiwi/2-12188757\n// ????????????\n\nstruct node_t{\n char val;\n node_t *ch[2];\n ll pri;\n int cnt;\n // string sum;\n node_t(char v,ll p):val(v),pri(p),cnt(1){\n // sum = \"\";\n // sum += v;\n ch[0]=ch[1]=NULL;\n }\n};\nint count(node_t *t){\n return t==NULL ? 0 : t->cnt;\n}\n// string sum(node_t *t){\n// return t==NULL ? \"\" : t->sum;\n// }\nnode_t* update(node_t *t){\n t->cnt = count(t->ch[0]) + 1 + count(t->ch[1]);\n // t->sum = sum(t->ch[0]) + t->val + sum(t->ch[1]);\n return t;\n}\nnode_t* merge(node_t *l, node_t *r){\n if(!l || !r) return !l?r:l;\n if(l->pri > r->pri){\n l->ch[1] = merge(l->ch[1],r);\n return update(l);\n }else{\n r->ch[0] = merge(l,r->ch[0]);\n return update(r);\n }\n}\npair<node_t*,node_t*> split(node_t *t, int k){\n if(!t) return make_pair((node_t*)NULL,(node_t*)NULL);\n if(k <= count(t->ch[0])){\n pair<node_t*,node_t*> s = split(t->ch[0],k);\n t->ch[0] = s.second;\n return make_pair(s.first,update(t));\n }else{\n pair<node_t*,node_t*> s = split(t->ch[1],k-count(t->ch[0])-1);\n t->ch[1] = s.first;\n return make_pair(update(t),s.second);\n }\n}\nnode_t* insert(node_t *t,int k,char val,ll pri){\n pair<node_t*,node_t*> lr = split(t,k);\n node_t* v = new node_t(val,pri);\n node_t* p = merge(lr.first,v);\n return merge(p,lr.second);\n}\nnode_t* erase(node_t *t,int k){\n pair<node_t*,node_t*> mr = split(t,k);\n pair<node_t*,node_t*> lm = split(mr.first,k-1);\n return merge(lm.first,mr.second);\n}\nvoid renew(node_t *t,int k,char val){\n node_t *target = t;\n vector<node_t*> l;\n int cnt = count(target);\n int left = 0;\n while(true){\n l.push_back(target);\n int lcnt = count(target->ch[0]);\n if(left+lcnt==k)break;\n if(left+lcnt>k)target = target->ch[0];\n if(left+lcnt<k){\n target = target->ch[1];\n left += lcnt+1;\n }\n }\n target->val = val;\n int sz = l.size();\n REP(i_,sz){\n int i = sz-1-i_;\n update(l[i]);\n }\n}\nvoid dfs(node_t *t,char *res){\n if(!t)return;\n int lcnt = count(t->ch[0]);\n res[lcnt] = t->val;\n dfs(t->ch[0],res);\n dfs(t->ch[1],res+lcnt+1);\n}\nvoid output(node_t *t){\n int len = count(t);\n char ret[len];\n dfs(t,ret);\n REP(i,len)cout<<ret[i];\n cout<<endl;\n}\n\nnode_t* str[100010];\n\nll rd(){\n // xorshift\n static ll x = 123456789, y = 362436069, z = 521288629, w = 88675123;\n ll t;\n t = x ^ (x<<11);\n x=y;y=z;z=w;\n return w=(w^(w>>19)^(t^(t>>8)));\n}\n\nint main(){\n int N,Q;\n cin >> N >> Q;\n // string S[N];\n // REP(i,N) cin >> S[i];\n REP(i,N){\n string s;\n cin>>s;\n int len = s.size();\n str[i] = new node_t(s[0],rd());\n REPR(j,len){\n str[i] = insert(str[i],count(str[i]),s[j],rd());\n }\n }\n REP(_,Q){\n int q_type;\n cin>>q_type;\n if(q_type == 1){\n int a,b,c,d;\n cin>>a>>b>>c>>d;\n --a;--b;--c;--d;\n pair<node_t*,node_t*> sa = split(str[a],b);\n pair<node_t*,node_t*> sc = split(str[c],d);\n str[a] = merge(sa.first,sc.second);\n str[c] = merge(sc.first,sa.second);\n }else{\n int a,b;\n char c;\n cin>>a>>b>>c;\n --a;--b;\n renew(str[a],b,c);\n // str[a] = erase(str[a],b+1);\n // str[a] = insert(str[a],b,c,rd());\n }\n }\n REP(i,N){\n // cout << output(str[i]) << endl;\n output(str[i]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 97616, "score_of_the_acc": -1.0665, "final_rank": 10 }, { "submission_id": "aoj_1571_1523656", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<set>\n#include<string>\n#include<stack>\n#include<cstdio>\n#include<cmath>\n#include<time.h>\n#include<cstdlib>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\n\n#define fr first\n#define sc second\n#define mp make_pair\n#define pb push_back\n#define rep(i,x) for(int i=0;i<x;i++)\n#define rep1(i,x) for(int i=1;i<=x;i++)\n#define rrep(i,x) for(int i=x-1;i>=0;i--)\n#define rrep1(i,x) for(int i=x;i>0;i--)\n#define sor(v) sort(v.begin(),v.end())\n#define rev(s) reverse(s.begin(),s.end())\n#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)\n#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)\n#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())\n#define mp1(a,b,c) P1(a,P(b,c))\n\nconst int INF=1000000000;\nconst int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};\nconst int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};\n\nstruct node{\n\tchar c;\n\tnode *next,*down;\n\tint siz;\n};\n\nconst double p = 0.30;\nconst int max_height = 10;\nnode null;\n\nstruct slis{\n\tint n;\n\tnode head;\n\tvoid show(){\n\t\tnode *h[max_height];\n\t\th[max_height-1] = &head;\n\t\trrep(i,max_height-1)h[i] = (*h[i+1]).down;\n\t\trrep(i,max_height){\n\t\t\tprintf(\"%d:\",i);\n\t\t\twhile(h[i] != &null){\n\t\t\t\tprintf(\"%c,%d \",h[i]->c,h[i]->siz);\n\t\t\t\th[i] = h[i]->next;\n\t\t\t}\n\t\t\tcout << endl;\n\t\t}\n\t}\n\tvoid show_(){\n\t\tnode* id = &head;\n\t\twhile(id->down != &null)id = id->down;\n\t\twhile(id->next != &null){\n\t\t\tid = id->next;\n\t\t\tprintf(\"%c\",id->c);\n\t\t}\n\t\tputs(\"\");\n\t}\n};\n\nnode* init(){//0???????????????\n\tnode* h = new node;\n\tnode* head[max_height];\n\thead[max_height-1] = h;\n\trrep1(i,max_height-1){\n\t\thead[i]->c = '$';\n\t\thead[i]->down = new node;\n\t\thead[i-1] = head[i]->down;\n\t}\n\thead[0]->c = '$';\n\thead[0]->down = &null;\n\treturn h;\n}\n\t\n\nslis* mkslis(int n,char *c,slis *ret){\n\t\n\t//???????´??????????????±??????????\n\tvector<int> height;\n\trep(i,n){\n\t\tint h = 1;\n\t\twhile(h < max_height && rand()%100 < p*100.0)h ++;\n\t\theight.pb(h);\n\t}\n\t\n\t//0???????????????\n\tnode* head[max_height];\n\thead[max_height-1] = init();\n\trrep1(i,max_height-1)head[i-1] = head[i]->down;\n\t/*node* h = new node;\n\tnode* head[max_height];\n\thead[max_height-1] = h;\n\trrep1(i,max_height-1){\n\t\thead[i]->c = '$';\n\t\thead[i]->down = new node;\n\t\thead[i-1] = head[i]->down;\n\t}\n\thead[0]->c = '$';\n\thead[0]->down = &null;*/\n\t\n\t//??¬????????????\n\tvector<node*> down;\n\trep(i,n)down.pb(&null);\n\trep(i,max_height){\n\t\tnode *id = head[i];\n\t\tint loc = 0;\n\t\trep(j,n){\n\t\t\tif(i < height[j]){\n\t\t\t\t(*id).next = new node;\n\t\t\t\t(*id).siz = j+1-loc;\n\t\t\t\tloc = j+1;\n\t\t\t\tid = (*id).next;\n\t\t\t\t(*id).c = *(c+j);\n\t\t\t\t(*id).down = down[j];\n\t\t\t\tdown[j] = id;\n\t\t\t}\n\t\t}\n\t\t(*id).next = &null;\n\t\t(*id).siz = n-loc;\n\t}\n\t\n\t(*ret).n = n;\n\t(*ret).head = *head[max_height-1];\n\treturn ret;\n}\n\npair<slis*,slis*> split(slis *a,slis *b,int k){\n\t//b???0???????????????\n\tnode* head[max_height];\n\thead[max_height-1] = init();\n\trrep1(i,max_height-1)head[i-1] = head[i]->down;\n\t\n\t//split\n\tnode* id = &a->head;\n\tint loc = 0;\n\tint h = max_height-1;\n\twhile(1){\n\t\twhile(loc + id->siz <= k){\n\t\t\tloc += id->siz;\n\t\t\tid = id->next;\n\t\t}\n\t\thead[h]->next = id->next;\n\t\thead[h]->siz = loc + id->siz - k;\n\t\tid->next = &null;\n\t\tid->siz = k - loc;\n\t\tif(id->down == &null)break;\n\t\tid = id->down;\n\t\th --;\n\t}\n\t\n\t//??????\n\tb->n = a->n - k;\n\ta->n = k;\n\tb->head = *head[max_height-1];\n\treturn pair<slis*,slis*>(a,b);\n}\n\nslis* merge(slis* a,slis* b){\n\tnode* id0 = &a->head;\n\tnode* id1 = &b->head;\n\twhile(id0 != &null){\n\t\twhile(id0->next != &null)id0 = id0->next;\n\t\tid0->next = id1->next;\n\t\tid0->siz += id1->siz;\n\t\tid0 = id0->down;\n\t\tid1 = id1->down;\n\t}\n\ta->n += b->n;\n\treturn a;\n}\n\nvoid updata(slis* lis,int k,char c){\n\tnode* id = &lis->head;\n\tint loc = 0;\n\twhile(id != &null){\n\t\twhile(id->next != &null && loc + id->siz <= k){\n\t\t\tloc += id->siz;\n\t\t\tid = id->next;\n\t\t}\n\t\tif(loc == k)id->c = c;\n\t\tid = id->down;\n\t}\n}\n\nint main(){\n\tstatic int n,q;\n\tstatic char c[2000010];\n\tstatic slis* s[100010];\n\tscanf(\"%d%d\",&n,&q);\n\trep(i,n){\n\t\tscanf(\"%s\",&c);\n\t\tint siz = 0;\n\t\twhile(c[siz] != '\\0')siz ++;\n\t\ts[i] = mkslis(siz,c,new slis);\n\t\trep(j,siz)c[j] = '\\0';\n\t}\n\trep(i,q){\n\t\tint t;\n\t\tscanf(\"%d\",&t);\n\t\tif(t == 1){\n\t\t\tint a,b,c,d;\n\t\t\tscanf(\"%d%d%d%d\",&a,&b,&c,&d);\n\t\t\tpair<slis*,slis*> p = split(s[a-1],new slis,b-1);\n\t\t\tpair<slis*,slis*> q = split(s[c-1],new slis,d-1);\n\t\t\ts[a-1] = merge(p.fr,q.sc);\n\t\t\ts[c-1] = merge(q.fr,p.sc);\n\t\t}\n\t\telse {\n\t\t\tint a,b;\n\t\t\tchar c_;\n\t\t\tscanf(\"%d%d %c\",&a,&b,&c_);\n\t\t\tupdata(s[a-1],b,c_);\n\t\t}\n\t}\n\t\n\trep(i,n){\n\t\ts[i]->show_();\n\t}\n\t\n\t/*int n;\n\tchar c[30];\n\tscanf(\"%d %s\",&n,&c);\n\tslis *test = new slis;\n\ttest = mkslis(n,c,test);\n\ttest->show();\n\tint k;\n\tscanf(\"%d\",&k);\n\tpair<slis*,slis*> p = split(test,new slis,k);\n\tp.fr->show();\n\tp.sc->show();\n\ttest = merge(p.fr,p.sc);\n\ttest->show();*/\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 239200, "score_of_the_acc": -1.4127, "final_rank": 17 }, { "submission_id": "aoj_1571_1523643", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<set>\n#include<string>\n#include<stack>\n#include<cstdio>\n#include<cmath>\n#include<time.h>\n#include<cstdlib>\nusing namespace std;\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\n\n#define fr first\n#define sc second\n#define mp make_pair\n#define pb push_back\n#define rep(i,x) for(int i=0;i<x;i++)\n#define rep1(i,x) for(int i=1;i<=x;i++)\n#define rrep(i,x) for(int i=x-1;i>=0;i--)\n#define rrep1(i,x) for(int i=x;i>0;i--)\n#define sor(v) sort(v.begin(),v.end())\n#define rev(s) reverse(s.begin(),s.end())\n#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)\n#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)\n#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())\n#define mp1(a,b,c) P1(a,P(b,c))\n\nconst int INF=1000000000;\nconst int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};\nconst int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};\n\nstruct node{\n\tchar c;\n\tnode *next,*down;\n\tint siz;\n};\n\nconst double p = 0.25;\nconst int max_height = 8;\nnode null;\n\nstruct slis{\n\tint n;\n\tnode head;\n\tvoid show(){\n\t\tnode *h[max_height];\n\t\th[max_height-1] = &head;\n\t\trrep(i,max_height-1)h[i] = (*h[i+1]).down;\n\t\trrep(i,max_height){\n\t\t\tprintf(\"%d:\",i);\n\t\t\twhile(h[i] != &null){\n\t\t\t\tprintf(\"%c,%d \",h[i]->c,h[i]->siz);\n\t\t\t\th[i] = h[i]->next;\n\t\t\t}\n\t\t\tcout << endl;\n\t\t}\n\t}\n\tvoid show_(){\n\t\tnode* id = &head;\n\t\twhile(id->down != &null)id = id->down;\n\t\twhile(id->next != &null){\n\t\t\tid = id->next;\n\t\t\tprintf(\"%c\",id->c);\n\t\t}\n\t\tputs(\"\");\n\t}\n};\n\nnode* init(){//0???????????????\n\tnode* h = new node;\n\tnode* head[max_height];\n\thead[max_height-1] = h;\n\trrep1(i,max_height-1){\n\t\thead[i]->c = '$';\n\t\thead[i]->down = new node;\n\t\thead[i-1] = head[i]->down;\n\t}\n\thead[0]->c = '$';\n\thead[0]->down = &null;\n\treturn h;\n}\n\t\n\nslis* mkslis(int n,char *c,slis *ret){\n\t\n\t//???????´??????????????±??????????\n\tvector<int> height;\n\trep(i,n){\n\t\tint h = 1;\n\t\twhile(h < max_height && rand()%100 < p*100.0)h ++;\n\t\theight.pb(h);\n\t}\n\t\n\t//0???????????????\n\tnode* head[max_height];\n\thead[max_height-1] = init();\n\trrep1(i,max_height-1)head[i-1] = head[i]->down;\n\t/*node* h = new node;\n\tnode* head[max_height];\n\thead[max_height-1] = h;\n\trrep1(i,max_height-1){\n\t\thead[i]->c = '$';\n\t\thead[i]->down = new node;\n\t\thead[i-1] = head[i]->down;\n\t}\n\thead[0]->c = '$';\n\thead[0]->down = &null;*/\n\t\n\t//??¬????????????\n\tvector<node*> down;\n\trep(i,n)down.pb(&null);\n\trep(i,max_height){\n\t\tnode *id = head[i];\n\t\tint loc = 0;\n\t\trep(j,n){\n\t\t\tif(i < height[j]){\n\t\t\t\t(*id).next = new node;\n\t\t\t\t(*id).siz = j+1-loc;\n\t\t\t\tloc = j+1;\n\t\t\t\tid = (*id).next;\n\t\t\t\t(*id).c = *(c+j);\n\t\t\t\t(*id).down = down[j];\n\t\t\t\tdown[j] = id;\n\t\t\t}\n\t\t}\n\t\t(*id).next = &null;\n\t\t(*id).siz = n-loc;\n\t}\n\t\n\t(*ret).n = n;\n\t(*ret).head = *head[max_height-1];\n\treturn ret;\n}\n\npair<slis*,slis*> split(slis *a,slis *b,int k){\n\t//b???0???????????????\n\tnode* head[max_height];\n\thead[max_height-1] = init();\n\trrep1(i,max_height-1)head[i-1] = head[i]->down;\n\t\n\t//split\n\tnode* id = &a->head;\n\tint loc = 0;\n\tint h = max_height-1;\n\twhile(1){\n\t\twhile(loc + id->siz <= k){\n\t\t\tloc += id->siz;\n\t\t\tid = id->next;\n\t\t}\n\t\thead[h]->next = id->next;\n\t\thead[h]->siz = loc + id->siz - k;\n\t\tid->next = &null;\n\t\tid->siz = k - loc;\n\t\tif(id->down == &null)break;\n\t\tid = id->down;\n\t\th --;\n\t}\n\t\n\t//??????\n\tb->n = a->n - k;\n\ta->n = k;\n\tb->head = *head[max_height-1];\n\treturn pair<slis*,slis*>(a,b);\n}\n\nslis* merge(slis* a,slis* b){\n\tnode* id0 = &a->head;\n\tnode* id1 = &b->head;\n\twhile(id0 != &null){\n\t\twhile(id0->next != &null)id0 = id0->next;\n\t\tid0->next = id1->next;\n\t\tid0->siz += id1->siz;\n\t\tid0 = id0->down;\n\t\tid1 = id1->down;\n\t}\n\ta->n += b->n;\n\treturn a;\n}\n\nvoid updata(slis* lis,int k,char c){\n\tnode* id = &lis->head;\n\tint loc = 0;\n\twhile(id != &null){\n\t\twhile(id->next != &null && loc + id->siz <= k){\n\t\t\tloc += id->siz;\n\t\t\tid = id->next;\n\t\t}\n\t\tif(loc == k)id->c = c;\n\t\tid = id->down;\n\t}\n}\n\nint main(){\n\tstatic int n,q;\n\tstatic char c[2000010];\n\tstatic slis* s[100010];\n\tscanf(\"%d%d\",&n,&q);\n\trep(i,n){\n\t\tscanf(\"%s\",&c);\n\t\tint siz = 0;\n\t\twhile(c[siz] != '\\0')siz ++;\n\t\ts[i] = mkslis(siz,c,new slis);\n\t\trep(j,siz)c[j] = '\\0';\n\t}\n\trep(i,q){\n\t\tint t;\n\t\tscanf(\"%d\",&t);\n\t\tif(t == 1){\n\t\t\tint a,b,c,d;\n\t\t\tscanf(\"%d%d%d%d\",&a,&b,&c,&d);\n\t\t\tpair<slis*,slis*> p = split(s[a-1],new slis,b-1);\n\t\t\tpair<slis*,slis*> q = split(s[c-1],new slis,d-1);\n\t\t\ts[a-1] = merge(p.fr,q.sc);\n\t\t\ts[c-1] = merge(q.fr,p.sc);\n\t\t}\n\t\telse {\n\t\t\tint a,b;\n\t\t\tchar c_;\n\t\t\tscanf(\"%d%d %c\",&a,&b,&c_);\n\t\t\tupdata(s[a-1],b,c_);\n\t\t}\n\t}\n\t\n\trep(i,n){\n\t\ts[i]->show_();\n\t}\n\t\n\t/*int n;\n\tchar c[30];\n\tscanf(\"%d %s\",&n,&c);\n\tslis *test = new slis;\n\ttest = mkslis(n,c,test);\n\ttest->show();\n\tint k;\n\tscanf(\"%d\",&k);\n\tpair<slis*,slis*> p = split(test,new slis,k);\n\tp.fr->show();\n\tp.sc->show();\n\ttest = merge(p.fr,p.sc);\n\ttest->show();*/\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 211500, "score_of_the_acc": -1.2779, "final_rank": 16 }, { "submission_id": "aoj_1571_1523297", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nstruct treap{\n static const int INF=INT_MAX;\n\n struct node{\n int val,mini,cnt,pri;\n node *lch,*rch;\n\n node(int val,int pri):val(val),pri(pri),cnt(1),mini(val){\n lch=rch=NULL;\n }\n };\n\n node *root;\n treap():root(NULL){srand(time(NULL));}\n\n int count(node *t){return !t?0:t->cnt;}\n int mini(node *t){return !t?INF:t->mini;}\n\n node *update(node *t){\n if(!t)return t;\n t->cnt=count(t->lch)+count(t->rch)+1;\n t->mini=min(min(mini(t->lch),mini(t->rch)),t->val);\n return t;\n }\n\n node *merge(node *l,node *r){\n if(!l||!r)return l?l:r;\n if(l->pri>r->pri){\n l->rch=merge(l->rch,r);\n return update(l);\n }\n r->lch=merge(l,r->lch);\n return update(r);\n }\n\n pair<node *,node *>split(node *t,int k){\n if(!t)return make_pair((node *)NULL,(node *)NULL);\n if(count(t->lch)>=k){\n pair<node *,node *>s=split(t->lch,k);\n t->lch=s.second;\n return make_pair(s.first,update(t));\n }\n pair<node *,node *>s=split(t->rch,k-count(t->lch)-1);\n t->rch=s.first;\n return make_pair(update(t),s.second);\n }\n\n node *insert(node *t,int k,int val,int pri){\n pair<node *,node *>s=split(t,k);\n t=merge(s.first,new node(val,pri));\n t=merge(t,s.second);\n return update(t);\n }\n\n node *erase(node *t,int k){\n pair<node *,node *>s1,s2;\n s2=split(t,k+1);\n s1=split(s2.first,k);\n return update((t=merge(s1.first,s2.second)));\n }\n\n\n void insert(int k,int val){\n root=insert(root,k,val,rand());\n }\n\n void erase(int k){\n root=erase(root,k);\n }\n\n int rangeMinimumQuery(node *t,int l,int r){\n if(!t)return INF;\n int sz=count(t);\n if(sz<=l||r<=0)return INF;\n if(l<=0&&sz<=r)return mini(t);\n\n int cnt=count(t->lch);\n\n int vl=rangeMinimumQuery(t->lch,l,r);\n int vr=rangeMinimumQuery(t->rch,l-cnt-1,r-cnt-1);\n\n int res=min(vl,vr);\n if(l<=cnt&&cnt<r)res=min(res,t->val);\n return res;\n }\n\n int rangeMinimumQuery(int l,int r){\n return rangeMinimumQuery(root,l,r);\n }\n\n\n\n void updateNode(int k,int val){\n erase(k);\n insert(k,val);\n\n }\n\n void Debug(node *t){\n if(!t)return;\n Debug(t->lch);\n cout<<t->val<<\" \";\n Debug(t->rch);\n }\n\n void Debug(){\n Debug(root);\n cout<<endl;\n }\n\n void query1(treap &t,int b,int d){\n pair<node *,node *>s1,s2;\n s1=split(root,b);\n s2=t.split(t.root,d);\n root=merge(s1.first,s2.second);\n t.root=merge(s2.first,s1.second);\n }\n\n void output(node *t){\n if(!t)return;\n output(t->lch);\n printf(\"%c\",t->val+'a');\n output(t->rch);\n }\n void output(){\n output(root);\n puts(\"\");\n }\n};\n\nvector<treap>v;\nchar latte[2000001];\nint main(){\n int n,q;\n scanf(\"%d%d\",&n,&q);\n\n v.resize(n);\n for(int i=0;i<n;i++){\n scanf(\"%s\",latte);\n for(int j=0;latte[j];j++){\n v[i].insert(j,latte[j]-'a');\n }\n }\n\n while(q--){\n int type;\n scanf(\"%d\",&type);\n if(type==1){\n int a,b,c,d;\n scanf(\"%d%d%d%d\",&a,&b,&c,&d);\n a--;b--;c--;d--;\n v[a].query1(v[c],b,d);\n }\n else{\n int a,b;\n char c;\n scanf(\"%d%d %c\",&a,&b,&c);\n a--;b--;\n v[a].updateNode(b,c-'a');\n }\n }\n\n for(int i=0;i<n;i++)v[i].output();\n return 0;\n}", "accuracy": 1, "time_ms": 720, "memory_kb": 100600, "score_of_the_acc": -0.9682, "final_rank": 7 }, { "submission_id": "aoj_1571_1522137", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n \nint main()\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n \n int n, q;\n cin >> n >> q;\n vector<list<string> > v(n+1);\n for(int i=1; i<=n; ++i){\n string s;\n cin >> s;\n for(unsigned j=0; j<s.size(); j+=1000)\n v[i].push_back(s.substr(j, 1000));\n }\n \n while(--q >= 0){\n int type;\n cin >> type;\n \n if(type == 1){\n int a, b, c, d;\n cin >> a >> b >> c >> d;\n \n auto it1 = v[a].begin();\n auto it2 = v[c].begin();\n while((int)it1->size() < b){\n b -= it1->size();\n ++ it1;\n }\n while((int)it2->size() < d){\n d -= it2->size();\n ++ it2;\n }\n \n string s = it2->substr(0, d-1) + it1->substr(b-1);\n string t = it1->substr(0, b-1) + it2->substr(d-1);\n *it1 = s;\n *it2 = t;\n \n list<string> x, y;\n x.splice(x.end(), v[a], v[a].begin(), it1);\n y.splice(y.end(), v[c], v[c].begin(), it2);\n x.splice(x.end(), v[c], v[c].begin(), v[c].end());\n y.splice(y.end(), v[a], v[a].begin(), v[a].end());\n v[a].swap(x);\n v[c].swap(y);\n }\n else{\n int a, b;\n char c;\n cin >> a >> b >> c;\n \n auto it = v[a].begin();\n while((int)it->size() < b){\n b -= it->size();\n ++ it;\n }\n (*it)[b-1] = c;\n }\n }\n \n for(int i=1; i<=n; ++i){\n for(const string& s : v[i])\n cout << s;\n cout << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 1270, "memory_kb": 6372, "score_of_the_acc": -1, "final_rank": 8 }, { "submission_id": "aoj_1571_1522048", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nclass Data\n{\npublic:\n string str;\n Data* next;\n Data(){\n next = NULL;\n }\n};\n\nint main()\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n\n int n, q;\n cin >> n >> q;\n vector<Data*> v(n+1, NULL);\n for(int i=1; i<=n; ++i){\n string s;\n cin >> s;\n Data **p = &v[i];\n for(unsigned j=0; j<s.size(); j+=1000){\n Data* d = new Data;\n d->str = s.substr(j, 1000);\n *p = d;\n p = &d->next;\n }\n }\n\n while(--q >= 0){\n int type;\n cin >> type;\n\n if(type == 1){\n int a, b, c, d;\n cin >> a >> b >> c >> d;\n\n Data* p1 = v[a];\n Data* p2 = v[c];\n while((int)p1->str.size() < b){\n b -= p1->str.size();\n p1 = p1->next;\n }\n while((int)p2->str.size() < d){\n d -= p2->str.size();\n p2 = p2->next;\n }\n\n string s = p1->str.substr(0, b-1) + p2->str.substr(d-1);\n string t = p2->str.substr(0, d-1) + p1->str.substr(b-1);\n p1->str = s;\n p2->str = t;\n\n swap(p1->next, p2->next);\n }\n else{\n int a, b;\n char c;\n cin >> a >> b >> c;\n\n Data* p = v[a];\n while((int)p->str.size() < b){\n b -= p->str.size();\n p = p->next;\n }\n p->str[b-1] = c;\n }\n }\n\n for(int i=1; i<=n; ++i){\n for(Data* p=v[i]; p!=NULL; p=p->next)\n cout << p->str;\n cout << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 6388, "score_of_the_acc": -0.9524, "final_rank": 6 }, { "submission_id": "aoj_1571_1521953", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int B=1000;\nchar latte[3000001];\nstring s[100000],_s[100000];\nint n;\nstruct data{\n int id,l,r;\n data(int a,int b,int c):id(a),l(b),r(c){}\n};\n\nvector<data>v[100000];\n\nvoid get(int a,int b,int &cur,int &sum){\n sum=cur=0;\n while(cur<(int)v[a].size()&&b-sum>=v[a][cur].r-v[a][cur].l){\n sum+=v[a][cur].r-v[a][cur].l;\n cur++;\n }\n}\n\n\nvoid div(int a,int b,vector<data>&X){\n int cur,sum;\n get(a,b,cur,sum);\n for(int i=(int)v[a].size()-1;i>cur;i--){\n //if(v[a].size()==0)continue;\n X.push_back(v[a].back());\n v[a].pop_back();\n }\n if(b==sum){\n //if(v[a].size()==0)return;\n X.push_back(v[a].back());\n v[a].pop_back();\n }\n else{\n //if(v[a].size()==0)return;\n data d=v[a].back();v[a].pop_back();\n v[a].push_back(data(d.id,d.l,d.l+b-sum));\n X.push_back(data(d.id,d.l+b-sum,d.r));\n }\n}\n\nvoid query1(){\n int a,b,c,d;\n scanf(\"%d%d%d%d\",&a,&b,&c,&d);\n a--;b--;c--;d--;\n\n vector<data>X,Y;\n\n div(a,b,X);\n div(c,d,Y);\n\n for(int i=(int)Y.size()-1;i>=0;i--)v[a].push_back(Y[i]);\n for(int i=(int)X.size()-1;i>=0;i--)v[c].push_back(X[i]);\n}\n\nvoid query2(){\n int a,b;char c;\n scanf(\"%d%d %c\",&a,&b,&c);\n a--;b--;\n\n int cur,sum;\n get(a,b,cur,sum);\n //if(cur>=v[a].size())return;\n int id=v[a][cur].id;\n int l=v[a][cur].l;\n int r=v[a][cur].r;\n //if(b-sum+l>=s[id].size())return;\n s[id][b-sum+l]=c;\n}\n\nvoid update(int query){\n for(int i=0;i<n;i++)v[i].clear();\n for(int i=0;i<n;i++)v[i].push_back(data(i,0,s[i].size()));\n\n for(int qry=0;qry<query;qry++){\n int type;scanf(\"%d\",&type);\n if(type==1)query1();\n else query2();\n\n\n }\n\n\n\n for(int i=0;i<n;i++){\n _s[i]=\"\";\n for(int j=0;j<v[i].size();j++){\n int id=v[i][j].id,l=v[i][j].l,r=v[i][j].r;\n if(r-l+l>s[id].size())continue;\n _s[i]+=s[id].substr(l,r-l);\n }\n }\n\n for(int i=0;i<n;i++)s[i]=_s[i];\n\n}\nint main(){\n int q;\n scanf(\"%d%d\",&n,&q);\n for(int i=0;i<n;i++){\n scanf(\"%s\",latte);\n s[i]=latte;\n }\n\n for(int i=0;i<(q+B-1)/B;i++){\n update(min(B,q-i*B));\n }\n for(int i=0;i<n;i++)printf(\"%s\\n\",s[i].c_str());\n\n return 0;\n}", "accuracy": 1, "time_ms": 620, "memory_kb": 18312, "score_of_the_acc": -0.5354, "final_rank": 1 }, { "submission_id": "aoj_1571_1521946", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int B=1000;\nchar latte[3000001];\nstring s[100000],_s[100000];\nint n;\nstruct data{\n int id,l,r;\n data(int a,int b,int c):id(a),l(b),r(c){}\n};\n\nvector<data>v[100000];\n\nvoid get(int a,int b,int &cur,int &sum){\n sum=cur=0;\n while(cur<(int)v[a].size()&&b-sum>=v[a][cur].r-v[a][cur].l){\n sum+=v[a][cur].r-v[a][cur].l;\n cur++;\n }\n}\n\n\nvoid div(int a,int b,vector<data>&X){\n int cur,sum;\n get(a,b,cur,sum);\n for(int i=(int)v[a].size()-1;i>cur;i--){\n if(v[a].size()==0)continue;\n X.push_back(v[a].back());\n v[a].pop_back();\n }\n if(b==sum){\n if(v[a].size()==0)return;\n X.push_back(v[a].back());\n v[a].pop_back();\n }\n else{\n if(v[a].size()==0)return;\n data d=v[a].back();v[a].pop_back();\n v[a].push_back(data(d.id,d.l,d.l+b-sum));\n X.push_back(data(d.id,d.l+b-sum,d.r));\n }\n}\n\nvoid query1(){\n int a,b,c,d;\n scanf(\"%d%d%d%d\",&a,&b,&c,&d);\n a--;b--;c--;d--;\n\n vector<data>X,Y;\n\n div(a,b,X);\n div(c,d,Y);\n\n for(int i=(int)Y.size()-1;i>=0;i--)v[a].push_back(Y[i]);\n for(int i=(int)X.size()-1;i>=0;i--)v[c].push_back(X[i]);\n}\n\nvoid query2(){\n int a,b;char c;\n scanf(\"%d%d %c\",&a,&b,&c);\n a--;b--;\n\n int cur,sum;\n get(a,b,cur,sum);\n //if(cur>=v[a].size())return;\n int id=v[a][cur].id;\n int l=v[a][cur].l;\n int r=v[a][cur].r;\n //if(b-sum+l>=s[id].size())return;\n s[id][b-sum+l]=c;\n}\n\nvoid update(int query){\n for(int i=0;i<n;i++)v[i].push_back(data(i,0,s[i].size()));\n\n for(int qry=0;qry<query;qry++){\n int type;scanf(\"%d\",&type);\n if(type==1)query1();\n else query2();\n\n\n\n }\n\n\n\n for(int i=0;i<n;i++){\n _s[i]=\"\";\n for(int j=0;j<v[i].size();j++){\n int id=v[i][j].id,l=v[i][j].l,r=v[i][j].r;\n _s[i]+=s[id].substr(l,r-l);\n }\n }\n\n for(int i=0;i<n;i++)s[i]=_s[i];\n}\nint main(){\n int q;\n scanf(\"%d%d\",&n,&q);\n for(int i=0;i<n;i++){\n scanf(\"%s\",latte);\n s[i]=latte;\n }\n\n for(int i=0;i<(q+B-1)/B;i++)update(min(B,q-i*B));\n\n for(int i=0;i<n;i++)printf(\"%s\\n\",s[i].c_str());\n\n return 0;\n}", "accuracy": 0.14285714285714285, "time_ms": 10, "memory_kb": 10420, "score_of_the_acc": -0.0174, "final_rank": 18 }, { "submission_id": "aoj_1571_1521822", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint B=1000;\nchar latte[2000001];\nstring s[100000],_s[100000];\nint n;\nstruct data{\n int id,l,r;\n data(int a,int b,int c):id(a),l(b),r(c){}\n};\n\nvector<data>v[100000];\n\nvoid get(int a,int b,int &cur,int &sum){\n sum=cur=0;\n while(cur<(int)v[a].size()&&sum+v[a][cur].r-v[a][cur].l<=b){\n sum+=v[a][cur].r-v[a][cur].l;\n cur++;\n }\n}\n\n\nvoid div(int a,int b,vector<data>&X){\n int cur,sum;\n get(a,b,cur,sum);\n for(int i=(int)v[a].size()-1;i>cur;i--){\n if(v[a].size()==0)continue;\n X.push_back(v[a].back());\n v[a].pop_back();\n }\n if(b==sum){\n if(v[a].size()==0)return;\n X.push_back(v[a].back());\n v[a].pop_back();\n }\n else{\n if(v[a].size()==0)return;\n data d=v[a].back();v[a].pop_back();\n v[a].push_back(data(d.id,d.l,d.l+b-sum));\n X.push_back(data(d.id,d.l+b-sum,d.r));\n }\n}\n\nvoid query1(){\n int a,b,c,d;\n scanf(\"%d%d%d%d\",&a,&b,&c,&d);\n a--;b--;c--;d--;\n\n vector<data>X,Y;\n\n div(a,b,X);\n div(c,d,Y);\n\n for(int i=(int)Y.size()-1;i>=0;i--)v[a].push_back(Y[i]);\n for(int i=(int)X.size()-1;i>=0;i--)v[c].push_back(X[i]);\n}\n\nvoid query2(){\n int a,b;char c;\n scanf(\"%d%d %c\",&a,&b,&c);\n a--;b--;\n\n int cur,sum;\n get(a,b,cur,sum);\n\n int id=v[a][cur].id;\n int l=v[a][cur].l;\n int r=v[a][cur].r;\n s[id][b-sum+l]=c;\n}\n\nvoid update(int query){\n for(int i=0;i<n;i++)v[i].push_back(data(i,0,s[i].size()));\n\n for(int qry=0;qry<query;qry++){\n int type;scanf(\"%d\",&type);\n if(type==1)query1();\n else query2();\n\n\n\n }\n\n\n\n for(int i=0;i<n;i++){\n _s[i]=\"\";\n for(int j=0;j<v[i].size();j++){\n int id=v[i][j].id,l=v[i][j].l,r=v[i][j].r;\n _s[i]+=s[id].substr(l,r-l);\n }\n }\n\n for(int i=0;i<n;i++)s[i]=_s[i];\n}\nint main(){\n int q;\n scanf(\"%d%d\",&n,&q);\n for(int i=0;i<n;i++){\n scanf(\"%s\",latte);\n s[i]=latte;\n }\n\n for(int i=0;i<(q+B-1)/B;i++)update(min(B,q-i*B));\n\n for(int i=0;i<n;i++)printf(\"%s\\n\",s[i].c_str());\n}", "accuracy": 0.14285714285714285, "time_ms": 10, "memory_kb": 10424, "score_of_the_acc": -0.0174, "final_rank": 19 }, { "submission_id": "aoj_1571_1521810", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint B=1000;\nchar latte[2000001];\nstring s[100000],_s[100000];\nint n;\nstruct data{\n int id,l,r;\n data(int a,int b,int c):id(a),l(b),r(c){}\n};\n\nvector<data>v[100000];\n\nvoid get(int a,int b,int &cur,int &sum){\n sum=cur=0;\n while(cur<(int)v[a].size()&&sum+v[a][cur].r-v[a][cur].l<=b){\n sum+=v[a][cur].r-v[a][cur].l;\n cur++;\n }\n}\n\n\nvoid div(int a,int b,vector<data>&X){\n int cur,sum;\n get(a,b,cur,sum);\n for(int i=(int)v[a].size()-1;i>cur;i--){\n X.push_back(v[a].back());\n v[a].pop_back();\n }\n if(b==sum){\n X.push_back(v[a].back());\n v[a].pop_back();\n }\n else{\n data d=v[a].back();v[a].pop_back();\n v[a].push_back(data(d.id,d.l,d.l+b-sum));\n X.push_back(data(d.id,d.l+b-sum,d.r));\n }\n}\n\nvoid query1(){\n int a,b,c,d;\n scanf(\"%d%d%d%d\",&a,&b,&c,&d);\n a--;b--;c--;d--;\n\n vector<data>X,Y;\n\n div(a,b,X);\n div(c,d,Y);\n\n for(int i=(int)Y.size()-1;i>=0;i--)v[a].push_back(Y[i]);\n for(int i=(int)X.size()-1;i>=0;i--)v[c].push_back(X[i]);\n}\n\nvoid query2(){\n int a,b;char c;\n scanf(\"%d%d %c\",&a,&b,&c);\n a--;b--;\n\n int cur,sum;\n get(a,b,cur,sum);\n\n int id=v[a][cur].id;\n int l=v[a][cur].l;\n int r=v[a][cur].r;\n s[id][b-sum+l]=c;\n}\n\nvoid update(int query){\n for(int i=0;i<n;i++)v[i].push_back(data(i,0,s[i].size()));\n\n for(int qry=0;qry<query;qry++){\n int type;scanf(\"%d\",&type);\n if(type==1)query1();\n else query2();\n\n\n\n }\n\n\n\n for(int i=0;i<n;i++){\n _s[i]=\"\";\n for(int j=0;j<v[i].size();j++){\n int id=v[i][j].id,l=v[i][j].l,r=v[i][j].r;\n _s[i]+=s[id].substr(l,r-l);\n }\n }\n\n for(int i=0;i<n;i++)s[i]=_s[i];\n}\nint main(){\n int q;\n scanf(\"%d%d\",&n,&q);\n for(int i=0;i<n;i++){\n scanf(\"%s\",latte);\n s[i]=latte;\n }\n\n for(int i=0;i<(q+B-1)/B;i++)update(min(B,q-i*B));\n\n for(int i=0;i<n;i++)printf(\"%s\\n\",s[i].c_str());\n}", "accuracy": 0.14285714285714285, "time_ms": 10, "memory_kb": 10428, "score_of_the_acc": -0.0174, "final_rank": 20 } ]
aoj_1578_cpp
Rubik Dungeon Problem あなたは n × n × n の立方体のルービックキューブ型のダンジョンにやってきた。 あなたは現在( x 1 , y 1 , z 1 )の部屋にいる。 目標の宝は( x 2 , y 2 , z 2 )の部屋にある。 あなたは隣接している前後左右上下の部屋に単位時間で移動することができる。 各部屋には6つのボタンがあり、それぞれのボタンを押すことにより、単位時間で、ルービックキューブのように以下の操作を行うことができる: 現在いる部屋と x 座標の値が同じ全ての部屋を時計または反時計周りに90度回転することができる 現在いる部屋と y 座標の値が同じ全ての部屋を時計または反時計周りに90度回転することができる 現在いる部屋と z 座標の値が同じ全ての部屋を時計または反時計周りに90度回転することができる 回転している途中で他の部屋に移動することはできない。 宝のある部屋まで最短で移動した場合の時間を求めよ。 Input n x 1 y 1 z 1 x 2 y 2 z 2 入力は全て整数で与えられる。 1行目に n が与えられる。 2行目に現在地の座標( x 1 , y 1 , z 1 )、3行目に宝のある部屋の座標( x 2 , y 2 , z 2 )が空白区切りで与えられる。 Constraints 2 ≤ n ≤ 10 5 0 ≤ x 1 , y 1 , z 1 , x 2 , y 2 , z 2 ≤ n −1 ( x 1 , y 1 , z 1 ) ≠ ( x 2 , y 2 , z 2 ) Output 最短の時間を出力せよ。 Sample Input 1 3 0 0 0 1 2 2 Sample Output 1 3 Sample Input 2 3 0 0 0 0 0 2 Sample Output 2 2 回転させると宝も移動してしまうので、回転させずに移動する。
[ { "submission_id": "aoj_1578_3090330", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nstruct Point{\n int x,y,z;\n};\n\nPoint READ(){\n int x,y,z;\n cin >>x >>y >>z;\n return {x,y,z};\n}\n\nint n;\nPoint s,g;\nint ans;\n\nPoint rot_x(Point p){\n return {p.x, n-1-p.z, p.y};\n}\n\nPoint rot_y(Point p){\n return {p.z, p.y, n-1-p.x};\n}\n\nPoint rot_z(Point p){\n return {n-1-p.y, p.x, p.z};\n}\n\nint dist(Point p, Point q){\n return abs(p.x-q.x) + abs(p.y-q.y) + abs(p.z-q.z);\n}\n\nbool IN(Point p){\n return 0<=p.x && p.x<n && 0<=p.y && p.y<n && 0<=p.z && p.z<n;\n}\n\nconst int dx[6]={1,-1,0,0,0,0};\nconst int dy[6]={0,0,1,-1,0,0};\nconst int dz[6]={0,0,0,0,1,-1};\nconst int add[3]={1,2,1};\n\nvoid dfs(int d, Point p, int cost){\n ans = min(ans, dist(p,g)+cost);\n if(d==6) return;\n\n Point np;\n rep(i,6){\n np = p;\n np.x += dx[i];\n np.y += dy[i];\n np.z += dz[i];\n if(IN(np)) dfs(d+1, np, cost+1);\n }\n\n if(p.x != g.x){\n np = p;\n rep(i,3){\n np = rot_x(np);\n dfs(d+1, np, cost+add[i]);\n }\n }\n\n if(p.y != g.y){\n np = p;\n rep(i,3){\n np = rot_y(np);\n dfs(d+1, np, cost+add[i]);\n }\n }\n\n if(p.z != g.z){\n np = p;\n rep(i,3){\n np = rot_z(np);\n dfs(d+1, np, cost+add[i]);\n }\n }\n}\n\nint main(){\n cin >>n;\n s = READ();\n g = READ();\n\n ans = dist(s,g);\n dfs(0,s,0);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3128, "score_of_the_acc": -0.8543, "final_rank": 1 }, { "submission_id": "aoj_1578_2747894", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n;\nint ans;\nint gx,gy,gz;\n\nvoid dfs(int c,int x,int y,int z){\n\tans=min(ans,abs(x-gx)+abs(y-gy)+abs(z-gz)+c);\n\t/*if(c<3){\n\t\tprintf(\"%d %d %d %d\\n\",c,x,y,z);\n\t}\n\t*/\n\tif(c==10)return;\n\tif(z!=gz){\n\t\tdfs(c+1,n+1-y,x,z);\n\t\tdfs(c+1,y,n+1-x,z);\n\t}else{\n\t\tif(z+1<=n)dfs(c+1,x,y,z+1);\n\t\tif(z-1>=1)dfs(c+1,x,y,z-1);\n\t}\n\tif(y!=gy){\n\t\tdfs(c+1,n+1-z,y,x);\n\t\tdfs(c+1,z,y,n+1-x);\n\t}else{\n\t\tif(y+1<=n)dfs(c+1,x,y+1,z);\n\t\tif(y-1>=1)dfs(c+1,x,y-1,z);\n\t}\n\tif(x!=gx){\n\t\tdfs(c+1,x,n+1-z,y);\n\t\tdfs(c+1,x,z,n+1-y);\n\t}else{\n\t\tif(x+1<=n)dfs(c+1,x+1,y,z);\n\t\tif(x-1>=1)dfs(c+1,x-1,y,z);\n\t}\n\n\n}\n\nint main(void){\n\tint sx,sy,sz;\n\tscanf(\"%d\",&n);\n\tscanf(\"%d%d%d\",&sx,&sy,&sz);\n\tsx++;\n\tsy++;\n\tsz++;\n\tscanf(\"%d%d%d\",&gx,&gy,&gz);\n\tgx++;\n\tgy++;\n\tgz++;\n\tans=abs(sx-gx)+abs(gy-sy)+abs(sz-gz);\n\tdfs(0,sx,sy,sz);\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3212, "score_of_the_acc": -1.3307, "final_rank": 3 }, { "submission_id": "aoj_1578_2578076", "code_snippet": "#include <iostream>\n#include <tuple>\n#include <queue>\n#include <map>\n\nusing Point = std::tuple<int, int, int>;\nusing roll_func = Point (*)(const Point &p, int N);\n\nconstexpr int INF = (1 << 29);\nconstexpr int LIMIT = 10;\n\nPoint roll_x(const Point &p, int N)\n{\n int x, y, z;\n std::tie(x, y, z) = p;\n return Point{N - y - 1, x, z};\n}\n\nPoint roll_y(const Point &p, int N)\n{\n int x, y, z;\n std::tie(x, y, z) = p;\n return Point{x, N - z - 1, y};\n}\n\nPoint roll_z(const Point &p, int N)\n{\n int x, y, z;\n std::tie(x, y, z) = p;\n return Point{N - z - 1, y, x};\n}\n\nPoint roll_n(const Point &p, int n, int N, roll_func func)\n{\n Point ret = p;\n for (int i = 0; i < n; i++) {\n ret = func(ret, N);\n }\n return ret;\n}\n\nPoint operator + (const Point &a, const Point &b)\n{\n int ax, ay, az, bx, by, bz;\n\n std::tie(ax, ay, az) = a;\n std::tie(bx, by, bz) = b;\n\n return Point{ax + bx, ay + by, az + bz};\n}\n\nint manhattan_distance(const Point &a, const Point &b)\n{\n int ax, ay, az, bx, by, bz;\n\n std::tie(ax, ay, az) = a;\n std::tie(bx, by, bz) = b;\n\n return abs(ax - bx) + abs(ay - by) + abs(az - bz);\n}\n\nint bfs(int N, const Point &s, const Point &t)\n{\n std::queue<Point> Q;\n Q.push(s);\n\n std::map<Point, int> min_times;\n min_times[s] = 0;\n\n int ret = INF;\n \n while (!Q.empty()) {\n Point curr = Q.front(); Q.pop();\n int curr_time = min_times[curr];\n\n ret = std::min(ret, curr_time + manhattan_distance(curr, t));\n \n if (curr_time == LIMIT) {\n continue;\n }\n\n constexpr Point dp[] = {Point{1, 0, 0}, Point{0, 1, 0}, Point{0, 0, 1}};\n for (int i = 0; i < 3; i++) {\n Point np = curr + dp[i];\n if (min_times.count(np) == 0) {\n min_times[np] = curr_time + 1;\n Q.push(np);\n }\n }\n \n for (int n : {1, 3}) {\n auto get = [](const Point &p, int k) -> int {\n switch (k) {\n case 0: return std::get<0>(p);\n case 1: return std::get<1>(p);\n case 2: return std::get<2>(p);\n }\n };\n\n int coord = 2;\n for (roll_func func : {roll_x, roll_y, roll_z}) { \n if (get(curr, coord) != get(t, coord)) {\n Point np = roll_n(curr, n, N, func);\n if (min_times.count(np) == 0) {\n min_times[np] = curr_time + 1;\n Q.push(np);\n }\n }\n\n coord = (coord + 1) % 3;\n }\n }\n }\n\n return ret;\n}\n\nint main()\n{\n int N;\n int sx, sy, sz, tx, ty, tz;\n\n std::cin >> N >> sx >> sy >> sz >> tx >> ty >> tz;\n\n Point s{sx, sy, sz}, t{tx, ty, tz}; \n std::cout << bfs(N, s, t) << std::endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4168, "score_of_the_acc": -1, "final_rank": 2 }, { "submission_id": "aoj_1578_1694207", "code_snippet": "#include <iostream>\n#include <algorithm> \n#include <vector>\n#include <cassert>\n#include <tuple>\nusing namespace std;\n\nusing vi = vector<int>;\nusing ll = long long;\n\nconst ll inf = 1e9;\n\ninline int dist(int x1, int y1, int z1, int x2, int y2, int z2) {\n return abs(x1 - y1) + abs(y1 - y2) + abs(z1 - z2);\n}\n\nint dx[] = { 0,0,0,0,-1,1 ,0};\nint dy[] = { 0,0,-1,1,0,0 ,0};\nint dz[] = { -1,1,0,0,0,0 ,0};\n\nint ans;\nint n;\n\nstruct P {\n int x, y, z;\n P(int x, int y, int z) :x(x), y(y), z(z) {}\n P() {}\n P rotX(int dir) const {\n return dir ? P(x, n - z - 1, y) : P(x, z, n - y - 1);\n }\n P rotY(int dir) const {\n return dir ? P(z, y, n - x - 1) : P(n - z - 1, y, x);\n }\n P rotZ(int dir) const {\n return dir ? P(n - y-1, x, z) : P(y, n - x-1, z);\n }\n int dist(P const &p) const {\n return abs(x - p.x) + abs(y - p.y) + abs(z - p.z);\n }\n bool inner() const {\n return\n 0 <= x && 0 <= y && 0 <= z &&\n x < n && y < n && z < n;\n }\n};\n\nvoid dfs(const P &a, const P &b, int role) {\n // cout << \" \" << a.x << \" \" << a.y << \" \" << a.z << \" \" << role << endl;\n ans = min(role + a.dist(b), ans);\n if (role == 9) return;\n if (a.x != b.x) {\n dfs(a.rotX(0), b, role + 1);\n dfs(a.rotX(1), b, role + 1);\n }\n if (a.y != b.y) {\n dfs(a.rotY(0), b, role + 1);\n dfs(a.rotY(1), b, role + 1);\n }\n if (a.z != b.z) {\n dfs(a.rotZ(0), b, role + 1);\n dfs(a.rotZ(1), b, role + 1);\n }\n}\n\nint main() {\n while (cin >> n) {\n ans = 1e9;\n P a, b;\n cin >> a.x >> a.y >> a.z;\n cin >> b.x >> b.y >> b.z;\n for (int i = 0; i < 6; ++i) {\n P aa = a;\n aa.x += dx[i];\n aa.y += dy[i];\n aa.z += dz[i];\n if (aa.inner()) {\n dfs(aa, b, 0);\n }\n }\n ++ans;\n dfs(a, b, 0);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 3116, "score_of_the_acc": -1.5997, "final_rank": 4 }, { "submission_id": "aoj_1578_1694189", "code_snippet": "#include <iostream>\n#include <algorithm> \n#include <vector>\n#include <cassert>\n#include <tuple>\nusing namespace std;\n\nusing vi = vector<int>;\nusing ll = long long;\n\nconst ll inf = 1e9;\n\ninline int dist(int x1, int y1, int z1, int x2, int y2, int z2) {\n return abs(x1 - y1) + abs(y1 - y2) + abs(z1 - z2);\n}\n\nint dx[] = { 0,0,0,0,-1,1 ,0};\nint dy[] = { 0,0,-1,1,0,0 ,0};\nint dz[] = { -1,1,0,0,0,0 ,0};\n\nint ans;\nint n;\n\nstruct P {\n int x, y, z;\n P(int x, int y, int z) :x(x), y(y), z(z) {}\n P() {}\n P rotX(int dir) const {\n return dir ? P(x, n - z - 1, y) : P(x, z, n - y - 1);\n }\n P rotY(int dir) const {\n return dir ? P(z, y, n - x - 1) : P(n - z - 1, y, x);\n }\n P rotZ(int dir) const {\n return dir ? P(n - y-1, x, z) : P(y, n - x-1, z);\n }\n int dist(P const &p) const {\n return abs(x - p.x) + abs(y - p.y) + abs(z - p.z);\n }\n bool inner() const {\n return\n 0 <= x && 0 <= y && 0 <= z &&\n x < n && y < n && z < n;\n }\n};\n\nvoid dfs(const P &a, const P &b, int role) {\n // cout << \" \" << a.x << \" \" << a.y << \" \" << a.z << \" \" << role << endl;\n ans = min(role + a.dist(b), ans);\n if (role == 10) return;\n if (a.x != b.x) {\n dfs(a.rotX(0), b, role + 1);\n dfs(a.rotX(1), b, role + 1);\n }\n if (a.y != b.y) {\n dfs(a.rotY(0), b, role + 1);\n dfs(a.rotY(1), b, role + 1);\n }\n if (a.z != b.z) {\n dfs(a.rotZ(0), b, role + 1);\n dfs(a.rotZ(1), b, role + 1);\n }\n}\n\nint main() {\n while (cin >> n) {\n ans = 1e9;\n P a, b;\n cin >> a.x >> a.y >> a.z;\n cin >> b.x >> b.y >> b.z;\n if (a.x == b.x || a.y == b.y || a.z == b.z) {\n for (int i = 0; i < 6; ++i) {\n P aa = a;\n aa.x += dx[i];\n aa.y += dy[i];\n aa.z += dz[i];\n if (aa.inner()) {\n dfs(aa, b, 0);\n }\n }\n ++ans;\n } \n dfs(a, b, 0);\n cout << ans << endl;\n }\n}", "accuracy": 0.2571428571428571, "time_ms": 320, "memory_kb": 3100, "score_of_the_acc": -1.4547, "final_rank": 5 }, { "submission_id": "aoj_1578_1694173", "code_snippet": "#include <iostream>\n#include <algorithm> \n#include <vector>\n#include <cassert>\n#include <tuple>\nusing namespace std;\n\nusing vi = vector<int>;\nusing ll = long long;\n\nconst ll inf = 1e9;\n\ninline int dist(int x1, int y1, int z1, int x2, int y2, int z2) {\n return abs(x1 - y1) + abs(y1 - y2) + abs(z1 - z2);\n}\n\nint dx[] = { 0,0,0,0,-1,1 ,0};\nint dy[] = { 0,0,-1,1,0,0 ,0};\nint dz[] = { -1,1,0,0,0,0 ,0};\n\nint ans;\nint n;\n\nstruct P {\n int x, y, z;\n P(int x, int y, int z) :x(x), y(y), z(z) {}\n P() {}\n P rotX(int dir) const {\n return dir ? P(x, n - z - 1, y) : P(x, z, n - y - 1);\n }\n P rotY(int dir) const {\n return dir ? P(z, y, n - x - 1) : P(n - z - 1, y, x);\n }\n P rotZ(int dir) const {\n return dir ? P(n - y-1, x, z) : P(y, n - x-1, z);\n }\n int dist(P const &p) const {\n return abs(x - p.x) + abs(y - p.y) + abs(z - p.z);\n }\n bool inner() const {\n return\n 0 <= x && 0 <= y && 0 <= z &&\n x < n && y < n && z < n;\n }\n};\n\nvoid dfs(const P &a, const P &b, int role) {\n // cout << \" \" << a.x << \" \" << a.y << \" \" << a.z << \" \" << role << endl;\n ans = min(role + a.dist(b), ans);\n if (role == 10) return;\n if (a.x != b.x) {\n dfs(a.rotX(0), b, role + 1);\n dfs(a.rotX(1), b, role + 1);\n }\n if (a.y != b.y) {\n dfs(a.rotY(0), b, role + 1);\n dfs(a.rotY(1), b, role + 1);\n }\n if (a.z != b.z) {\n dfs(a.rotZ(0), b, role + 1);\n dfs(a.rotZ(1), b, role + 1);\n }\n}\n\nint main() {\n while (cin >> n) {\n ans = 1e9;\n P a, b;\n cin >> a.x >> a.y >> a.z;\n cin >> b.x >> b.y >> b.z;\n if (a.x == b.x || a.y == b.y || a.z == b.z) {\n for (int i = 0; i < 6; ++i) {\n P aa = a;\n aa.x += dx[i];\n aa.y += dy[i];\n aa.z += dz[i];\n if (aa.inner()) {\n dfs(aa, b, 0);\n }\n }\n ++ans;\n } \n ++ans;\n dfs(a, b, 0);\n cout << ans << endl;\n }\n}", "accuracy": 0.2571428571428571, "time_ms": 320, "memory_kb": 3116, "score_of_the_acc": -1.4608, "final_rank": 6 }, { "submission_id": "aoj_1578_1694013", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\ntypedef long long ll;\ntypedef pair<int,int>pint;\ntypedef vector<int>vint;\ntypedef vector<pint>vpint;\n#define pb push_back\n#define mp make_pair\n#define fi first\n#define se second\n#define all(v) (v).begin(),(v).end()\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\ntemplate<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}\ntemplate<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}\n\nstruct data{\n int x,y,z,xx,yy,zz;\n data(int x,int y,int z,int xx,int yy,int zz):x(x),y(y),z(z),xx(xx),yy(yy),zz(zz){}\n bool operator<(const data &d)const{\n if(x!=d.x)return x<d.x;\n if(y!=d.y)return y<d.y;\n if(z!=d.z)return z<d.z;\n if(xx!=d.xx)return xx<d.xx;\n if(yy!=d.yy)return yy<d.yy;\n return zz<d.zz;\n }\n};\n\nint N;\nint x,y,z,xx,yy,zz;\n\nmap<data,int>dist;\ninline void add(data &d,data &nd,queue<data>&que){\n if(dist.find(nd)==dist.end())dist[nd]=1001001001;\n if(dist[nd]>dist[d]+1){\n dist[nd]=dist[d]+1;\n que.push(nd);\n }\n\n}\n\nsigned main(){\n cin>>N;\n cin>>x>>y>>z>>xx>>yy>>zz;\n\n data start(x,y,z,xx,yy,zz);\n dist[start]=0;\n queue<data>que;\n que.push(start);\n while(que.size()){\n data d=que.front();que.pop();\n data nd(0,0,0,0,0,0);\n if(d.x!=d.xx){\n nd=d;\n nd.y=d.z;\n nd.z=N-1-d.y;\n add(d,nd,que);\n\n nd=d;\n nd.yy=d.zz;\n nd.zz=N-1-d.yy;\n add(d,nd,que);\n }\n if(d.y!=d.yy){\n nd=d;\n nd.x=d.z;\n nd.z=N-1-d.x;\n add(d,nd,que);\n\n nd=d;\n nd.xx=d.zz;\n nd.zz=N-1-d.xx;\n add(d,nd,que);\n }\n if(d.z!=d.zz){\n nd=d;\n nd.x=d.y;\n nd.y=N-1-d.z;\n add(d,nd,que);\n\n nd=d;\n nd.xx=d.yy;\n nd.yy=N-1-d.zz;\n add(d,nd,que);\n }\n }\n\n int mi=1001001001;\n each(it,dist){\n data d=it->fi;\n int cost=abs(d.x-d.xx)+abs(d.y-d.yy)+abs(d.z-d.zz);\n chmin(mi,cost+it->se);\n }\n cout<<mi<<endl;\n return 0;\n}", "accuracy": 0.22857142857142856, "time_ms": 10, "memory_kb": 1540, "score_of_the_acc": 0, "final_rank": 7 } ]
aoj_1561_cpp
Problem K: Manhattan Warp Machine 2 Problem とある宇宙では、3次元の格子点上に星が存在し、宇宙人達はマンハッタンワープ装置という装置を使い、星間を移動している。 このワープ装置には、 N 個のボタンが付いており、ボタン i を押すと、現在いる星からのマンハッタン距離が d i である任意の星にワープすることができる。 ワープ装置は改良され、コストがかからずに移動出来る。 今、(0, 0, 0)の星にいるある宇宙人がある M 個の星を訪れたいと考えている。 M 個の星を全て訪れるのに最短で何回ワープをする必要があるか答えよ。 もし、1つでも訪れることが不可能な星がある場合は-1を出力せよ。 M 個の星は好きな順番に訪れることが出来、全て訪れた後に(0, 0, 0)に戻って来る必要は無い。 3次元上の全ての格子点には必ず星が存在する。 点( x 1 , y 1 , z 1 )と点( x 2 , y 2 , z 2 )間のマンハッタン距離は| x 1 - x 2 | + | y 1 - y 2 | + | z 1 - z 2 |で表される。 Input N M d 1 ... d N x 1 y 1 z 1 ... x M y M z M 入力は全て整数で与えられる。 1行目に N , M が与えられる。 2行目に移動可能なマンハッタン距離 d i が空白区切りで与えられる。 3行目以降の M 行に、 M 個の訪れたい星の格子点の座標( x i , y i , z i )が1行ずつ空白区切りで与えられる。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 15 1 ≤ M ≤ 15 1 ≤ d i ≤ 3×10 5 -10 5 ≤ x i , y i , z i ≤ 10 5 与えられるマンハッタン距離 d i は全て異なる。 与えられる格子点の座標は全て異なる。 Output M 個の星全てを訪れるのにかかる最短ワープ回数を1行に出力せよ。 訪れることが不可能な星がある場合は-1を出力せよ。 Sample Input 1 2 1 1 2 5 0 0 Sample Output 1 3 Sample Input 2 2 2 9 3 3 0 0 3 9 0 Sample Output 2 2 Sample Input 3 1 1 4 3 0 0 Sample Output 3 -1
[ { "submission_id": "aoj_1561_5550709", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)\n#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)\n\n#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)\n#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)\n\n#define debug(x) cerr << \"(\" << __LINE__ << \")\" << #x << \": \" << (x) << endl\n\nconstexpr i32 inf = 1001001001;\nconstexpr i64 infll = 1001001001001001001ll;\n\nconstexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; \nconstexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\nstruct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;\n\ntemplate <typename T = i64> T input() { T x; cin >> x; return x; }\n\ntemplate <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\"); } return os; } \ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }\n\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\ntemplate <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }\n\ntemplate <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }\ntemplate <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n// }}}\n\nclass BFS_Graph {\n const int INF = 1001001001;\n\n int n_;\n vector< vector< int > > G;\n vector< int > ds;\n\npublic:\n BFS_Graph(int n) : n_(n), G(n), ds(n, INF) {}\n\n int inf() const { return INF; }\n\n void add_edge(int u, int v) {\n G[u].emplace_back(v);\n }\n\n void build(int st) {\n queue<int> que;\n que.emplace(st);\n ds[st] = 0;\n\n while (not que.empty()) {\n int v = que.front();\n que.pop();\n for (auto &u : G[v]) {\n if (ds[u] != INF) continue;\n ds[u] = ds[v] + 1;\n que.emplace(u);\n }\n }\n }\n\n int operator[](size_t idx) const {\n return ds.at(idx);\n }\n};\n\ntemplate < typename CostType >\nclass shortestHamiltonPath {\n using u32 = unsigned int;\n struct edge {\n u32 to;\n CostType cost;\n\n edge(u32 to_, CostType cost_) : to(to_), cost(cost_) {}\n };\n\n const CostType INF = numeric_limits<CostType>::max() / 2;\n\n u32 V;\n vector< vector<edge> > G;\n vector< CostType > ds;\n\n vector< vector< CostType > > dp;\n\n CostType dfs(u32 v, u32 bit) {\n CostType &res = dp[v][bit];\n if (bit + 1 == 1u << V) res = 0;\n if (res != -1) return res;\n res = INF;\n for (auto [u, cost] : G[v]) {\n if (bit & (1 << u)) continue;\n res = min(res, cost + dfs(u, bit | (1 << u)));\n }\n return res;\n }\n\n public:\n shortestHamiltonPath(u32 V_) : V(V_), G(V_) {}\n\n void add_edge(int from, int to, CostType cost) {\n G[from].emplace_back(to, cost);\n }\n\n CostType inf() {\n return INF;\n }\n\n CostType shortest_hamilton_path() {\n dp.assign(V, vector< CostType >(1 << V, -1));\n return dfs(0, 1);\n }\n};\n\nvoid solve() {\n int n = input(), m = input();\n \n int V = 6e5;\n BFS_Graph G(V);\n\n int max_d = -1;\n range(i, 0, n) {\n int d = input();\n chmax(max_d, d);\n range(x, d, V) {\n G.add_edge(x - d, x);\n G.add_edge(x, x - d);\n }\n }\n\n G.build(0);\n\n vector< int > xs(m + 1), ys(m + 1), zs(m + 1);\n range(i, 1, m + 1) {\n cin >> xs[i] >> ys[i] >> zs[i];\n }\n \n shortestHamiltonPath<i64> T(m + 1);\n range(u, 0, m + 1) range(v, u + 1, m + 1) {\n int d = abs(xs[u] - xs[v]) + abs(ys[u] - ys[v]) + abs(zs[u] - zs[v]);\n int cost = G[d % max_d];\n if (cost == G.inf()) continue;\n T.add_edge(u, v, cost + d / max_d);\n T.add_edge(v, u, cost + d / max_d);\n }\n\n i64 ans = T.shortest_hamilton_path();\n if (ans == T.inf()) {\n cout << -1 << endl;\n } else {\n cout << ans << endl;\n }\n}\n\nsigned main() {\n solve();\n}", "accuracy": 0.125, "time_ms": 40, "memory_kb": 38312, "score_of_the_acc": -1.1765, "final_rank": 14 }, { "submission_id": "aoj_1561_5550528", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)\n#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)\n\n#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)\n#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)\n\n#define debug(x) cerr << \"(\" << __LINE__ << \")\" << #x << \": \" << (x) << endl\n\nconstexpr i32 inf = 1001001001;\nconstexpr i64 infll = 1001001001001001001ll;\n\nconstexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; \nconstexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};\n\nstruct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;\n\ntemplate <typename T = i64> T input() { T x; cin >> x; return x; }\n\ntemplate <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\"); } return os; } \ntemplate <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }\n\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << \"(\" << p.first << \", \" << p.second << \")\"; return os; }\ntemplate <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }\n\ntemplate <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }\ntemplate <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n// }}}\n\nclass BFS_Graph {\n const int INF = 1001001001;\n\n int n_;\n vector< vector< int > > G;\n vector< int > ds;\n\npublic:\n BFS_Graph(int n) : n_(n), G(n), ds(n, INF) {}\n\n int inf() const { return INF; }\n\n void add_edge(int u, int v) {\n G[u].emplace_back(v);\n }\n\n void build(int st) {\n queue<int> que;\n que.emplace(st);\n ds[st] = 0;\n\n while (not que.empty()) {\n int v = que.front();\n que.pop();\n for (auto &u : G[v]) {\n if (ds[u] != INF) continue;\n ds[u] = ds[v] + 1;\n que.emplace(u);\n }\n }\n }\n\n int operator[](size_t idx) const {\n return ds.at(idx);\n }\n};\n\ntemplate < typename CostType >\nclass shortestHamiltonPath {\n using u32 = unsigned int;\n struct edge {\n u32 to;\n CostType cost;\n\n edge(u32 to_, CostType cost_) : to(to_), cost(cost_) {}\n };\n\n const CostType INF = numeric_limits<CostType>::max() / 2;\n\n u32 V;\n vector< vector<edge> > G;\n vector< CostType > ds;\n\n vector< vector< CostType > > dp;\n\n CostType dfs(u32 v, u32 bit) {\n CostType &res = dp[v][bit];\n if (bit + 1 == 1u << V) res = 0;\n if (res != -1) return res;\n res = INF;\n for (auto [u, cost] : G[v]) {\n if (bit & (1 << u)) continue;\n res = min(res, cost + dfs(u, bit | (1 << u)));\n }\n return res;\n }\n\n public:\n shortestHamiltonPath(u32 V_) : V(V_), G(V_) {}\n\n void add_edge(int from, int to, CostType cost) {\n G[from].emplace_back(to, cost);\n }\n\n CostType inf() {\n return INF;\n }\n\n CostType shortest_hamilton_path() {\n dp.assign(V, vector< CostType >(1 << V, -1));\n return dfs(0, 1);\n }\n};\n\nvoid solve() {\n int n = input(), m = input();\n \n int V = 6e5;\n BFS_Graph G(V);\n\n int max_d;\n range(i, 0, n) {\n int d = input();\n chmax(max_d, d);\n range(x, d, V) {\n G.add_edge(x - d, x);\n G.add_edge(x, x - d);\n }\n }\n\n G.build(0);\n\n vector< int > xs(m + 1), ys(m + 1), zs(m + 1);\n range(i, 1, m + 1) {\n cin >> xs[i] >> ys[i] >> zs[i];\n }\n \n shortestHamiltonPath<i64> T(m + 1);\n range(u, 0, m + 1) range(v, u + 1, m + 1) {\n int d = abs(xs[u] - xs[v]) + abs(ys[u] - ys[v]) + abs(zs[u] - zs[v]);\n int cost = G[d % max_d];\n if (cost == G.inf()) continue;\n T.add_edge(u, v, cost + d / max_d);\n T.add_edge(v, u, cost + d / max_d);\n }\n\n i64 ans = T.shortest_hamilton_path();\n if (ans == T.inf()) {\n cout << -1 << endl;\n } else {\n cout << ans << endl;\n }\n}\n\nsigned main() {\n solve();\n}", "accuracy": 0.125, "time_ms": 40, "memory_kb": 38312, "score_of_the_acc": -1.1765, "final_rank": 14 }, { "submission_id": "aoj_1561_3112782", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF=1<<29,MAX=600001;\nstruct P {int x,y,z;}b[15];\n\nint D(P p, P q) {return abs(p.x-q.x)+abs(p.y-q.y)+abs(p.z-q.z);}\n\nint d[MAX];\nint main() {\n int n,m;\n cin >> n >> m;\n int a[n],e[2]={0,0};\n for(int i=0; i<n; i++) cin >> a[i];\n sort(a,a+n);\n for(int i=0; i<MAX; i++) d[i]=(i?INF:0);\n for(int i=0; i<n; i++) {\n e[(a[i]+1)%2]=a[i];\n d[a[i]]=1;\n for(int j=a[i]-e[a[i]%2]; j<=a[i]+e[a[i]%2]; j+=2) d[j]=min(d[j],2);\n }\n for(int i=0; i<=a[n-1]*2; i+=2) d[i]=min(d[i],2);\n for(int i=0; i<MAX; i++) d[i]=min(d[i],min(d[abs(i-e[0])],d[abs(i-e[1])])+1);\n for(int i=0; i<m; i++) cin >> b[i].x >> b[i].y >> b[i].z;\n int w[m][m],dp[1<<m][m],ans=INF;\n for(int i=0; i<m; i++) {\n for(int j=0; j<m; j++) w[i][j]=d[D(b[i],b[j])];\n }\n fill(dp[0],dp[1<<m],INF);\n for(int i=0; i<m; i++) dp[1<<i][i]=d[D((P){0,0,0},b[i])];\n for(int t=1; t<(1<<m); t++) {\n for(int i=0; i<m; i++) {\n for(int j=0; j<m; j++) dp[t|(1<<j)][j]=min(dp[t|(1<<j)][j],dp[t][i]+w[i][j]);\n }\n }\n for(int i=0; i<m; i++) ans=min(ans,dp[(1<<m)-1][i]);\n cout << (ans<INF?ans:-1) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7364, "score_of_the_acc": -0.1093, "final_rank": 1 }, { "submission_id": "aoj_1561_1256102", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 4;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<int> d(n);\n for(int i=0; i<n; ++i)\n cin >> d[i];\n sort(d.begin(), d.end());\n\n vector<int> x(m), y(m), z(m);\n for(int i=0; i<m; ++i)\n cin >> x[i] >> y[i] >> z[i];\n\n vector<int> cost(600001, INF);\n for(int i=0; i<n; ++i){\n for(int j=i; j<n; ++j){\n for(int k=d[j]-d[i]; k<=d[j]+d[i]; k+=2)\n cost[k] = 2;\n }\n }\n cost[0] = 0;\n for(int i=0; i<600001; ++i){\n for(int j=0; j<n; ++j){\n int k = i + d[j];\n if(k < 600001)\n cost[k] = min(cost[k], cost[i] + 1);\n\n k = abs(d[j] - i);\n if(k < 600001)\n cost[k] = min(cost[k], cost[i] + 1);\n }\n }\n\n vector<vector<int> > dp(1<<m, vector<int>(m, INF));\n for(int i=0; i<m; ++i){\n int dist = abs(x[i]) + abs(y[i]) + abs(z[i]);\n dp[1<<i][i] = cost[dist];\n }\n for(int i=0; i<(1<<m); ++i){\n for(int j=0; j<m; ++j){\n for(int k=0; k<m; ++k){\n int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);\n dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);\n }\n }\n }\n\n int ans = *min_element(dp.back().begin(), dp.back().end());\n if(ans < INF)\n cout << ans << endl;\n else\n cout << -1 << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 6804, "score_of_the_acc": -0.4461, "final_rank": 2 }, { "submission_id": "aoj_1561_1256087", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 4;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<int> d(n);\n for(int i=0; i<n; ++i)\n cin >> d[i];\n sort(d.begin(), d.end());\n\n vector<int> x(m), y(m), z(m);\n for(int i=0; i<m; ++i)\n cin >> x[i] >> y[i] >> z[i];\n\n vector<int> cost(600001, INF);\n for(int i=0; i<n; ++i){\n for(int j=i; j<n; ++j){\n for(int k=d[j]-d[i]; k<=d[j]+d[i]; k+=2)\n cost[k] = 2;\n }\n }\n cost[0] = 0;\n for(int i=0; i<600001; ++i){\n for(int j=0; j<n; ++j){\n int k = abs(i - d[j]);\n cost[i] = min(cost[i], cost[k] + 1);\n }\n }\n\n vector<vector<int> > dp(1<<m, vector<int>(m, INF));\n for(int i=0; i<m; ++i){\n int dist = abs(x[i]) + abs(y[i]) + abs(z[i]);\n dp[1<<i][i] = cost[dist];\n }\n for(int j=0; j<m; ++j){\n for(int i=0; i<(1<<m); ++i){\n for(int k=0; k<m; ++k){\n int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);\n dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);\n }\n }\n }\n\n int ans = *min_element(dp.back().begin(), dp.back().end());\n if(ans < INF)\n cout << ans << endl;\n else\n cout << -1 << endl;\n\n return 0;\n}", "accuracy": 0.6, "time_ms": 30, "memory_kb": 6804, "score_of_the_acc": -0.2108, "final_rank": 4 }, { "submission_id": "aoj_1561_1249531", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 4;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<int> d(n);\n for(int i=0; i<n; ++i)\n cin >> d[i];\n\n vector<int> x(m), y(m), z(m);\n for(int i=0; i<m; ++i)\n cin >> x[i] >> y[i] >> z[i];\n\n vector<int> cost(600001, INF);\n for(int i=0; i<n; ++i){\n for(int j=0; j<n; ++j){\n cost[d[i]+d[j]] = 2;\n }\n }\n for(int i=0; i<600001; ++i){\n for(int j=0; j<n; ++j){\n int k = i + d[j];\n if(k < 600001)\n cost[k] = min(cost[k], cost[i] + 1);\n }\n }\n for(int i=600000; i>2; --i)\n cost[i-2] = min(cost[i-2], cost[i]);\n for(int i=0; i<n; ++i)\n cost[d[i]] = 1;\n cost[0] = 0;\n\n vector<vector<int> > dp(1<<m, vector<int>(m, INF));\n for(int i=0; i<m; ++i){\n int dist = abs(x[i]) + abs(y[i]) + abs(z[i]);\n dp[1<<i][i] = cost[dist];\n }\n for(int i=0; i<(1<<m); ++i){\n for(int j=0; j<m; ++j){\n for(int k=0; k<m; ++k){\n int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);\n dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);\n }\n }\n }\n\n int ans = *min_element(dp.back().begin(), dp.back().end());\n if(ans < INF)\n cout << ans << endl;\n else\n cout << -1 << endl;\n\n return 0;\n}", "accuracy": 0.45, "time_ms": 10, "memory_kb": 3568, "score_of_the_acc": 0, "final_rank": 5 }, { "submission_id": "aoj_1561_1249504", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 4;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<int> d(n);\n for(int i=0; i<n; ++i)\n cin >> d[i];\n\n ++ m;\n vector<int> x(m), y(m), z(m);\n x[0] = y[0] = z[0] = 0;\n for(int i=1; i<m; ++i)\n cin >> x[i] >> y[i] >> z[i];\n\n vector<int> cost(1000000, INF);\n for(int i=0; i<n; ++i){\n for(int j=0; j<n; ++j){\n cost[d[i]+d[j]] = 2;\n }\n }\n for(int i=0; i<1000000; ++i){\n for(int j=0; j<n; ++j){\n int k = i + d[j];\n if(k < 1000000)\n cost[k] = min(cost[k], cost[i] + 1);\n }\n }\n for(int i=1000000-1; i>2; --i)\n cost[i-2] = min(cost[i-2], cost[i]);\n for(int i=0; i<n; ++i)\n cost[d[i]] = 1;\n cost[0] = 0;\n\n vector<vector<int> > dp(1<<m, vector<int>(m, INF));\n dp[1][0] = 0;\n for(int i=0; i<(1<<m); ++i){\n for(int j=0; j<m; ++j){\n for(int k=0; k<m; ++k){\n int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);\n dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);\n }\n }\n }\n\n int ans = *min_element(dp.back().begin(), dp.back().end());\n if(ans < INF)\n cout << ans << endl;\n else\n cout << -1 << endl;\n\n return 0;\n}", "accuracy": 0.45, "time_ms": 10, "memory_kb": 5152, "score_of_the_acc": -0.0456, "final_rank": 6 }, { "submission_id": "aoj_1561_1249500", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 4;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<int> d(n);\n for(int i=0; i<n; ++i)\n cin >> d[i];\n\n ++ m;\n vector<int> x(m), y(m), z(m);\n x[0] = y[0] = z[0] = 0;\n for(int i=1; i<m; ++i)\n cin >> x[i] >> y[i] >> z[i];\n\n vector<int> cost(1000000, INF);\n cost[0] = 0;\n for(int i=0; i<n; ++i){\n for(int j=0; j<n; ++j){\n cost[d[i]+d[j]] = 2;\n }\n }\n for(int i=0; i<1000000; ++i){\n for(int j=0; j<n; ++j){\n int k = i + d[j];\n if(k < 1000000)\n cost[k] = min(cost[k], cost[i] + 1);\n }\n }\n for(int i=1000000-1; i>2; --i)\n cost[i-2] = min(cost[i-2], cost[i]);\n for(int i=0; i<n; ++i)\n cost[d[i]] = 1;\n\n vector<vector<int> > dp(1<<m, vector<int>(m, INF));\n dp[1][0] = 0;\n for(int i=0; i<(1<<m); ++i){\n for(int j=0; j<m; ++j){\n for(int k=0; k<m; ++k){\n int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);\n dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);\n }\n }\n }\n\n int ans = *min_element(dp.back().begin(), dp.back().end());\n if(ans < INF)\n cout << ans << endl;\n else\n cout << -1 << endl;\n\n return 0;\n}", "accuracy": 0.075, "time_ms": 10, "memory_kb": 4952, "score_of_the_acc": -0.0398, "final_rank": 17 }, { "submission_id": "aoj_1561_1249455", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 2;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<int> d(n);\n for(int i=0; i<n; ++i)\n cin >> d[i];\n\n ++ m;\n vector<int> x(m), y(m), z(m);\n x[0] = y[0] = z[0] = 0;\n for(int i=1; i<m; ++i)\n cin >> x[i] >> y[i] >> z[i];\n\n vector<int> cost(1000000, INF);\n for(int i=0; i<n; ++i){\n for(int j=0; j<n; ++j){\n cost[d[i]+d[j]] = 2;\n }\n }\n for(int i=0; i<1000000; ++i){\n for(int j=0; j<n; ++j){\n int k = i + d[j];\n if(k < 1000000)\n cost[k] = min(cost[k], cost[i] + 1);\n }\n }\n for(int i=1000000-1; i>2; --i)\n cost[i-2] = min(cost[i-2], max(2, cost[i]));\n cost[0] = 0;\n for(int i=0; i<n; ++i)\n cost[d[i]] = 1;\n\n vector<vector<int> > dp(1<<m, vector<int>(m, INF));\n dp[1][0] = 0;\n for(int i=0; i<(1<<m); ++i){\n for(int j=0; j<m; ++j){\n for(int k=0; k<m; ++k){\n int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);\n dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);\n }\n }\n }\n\n int ans = *min_element(dp.back().begin(), dp.back().end());\n if(ans < INF)\n cout << ans << endl;\n else\n cout << -1 << endl;\n\n return 0;\n}", "accuracy": 0.45, "time_ms": 10, "memory_kb": 5152, "score_of_the_acc": -0.0456, "final_rank": 6 }, { "submission_id": "aoj_1561_1249440", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 2;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<int> d(n);\n for(int i=0; i<n; ++i)\n cin >> d[i];\n\n ++ m;\n vector<int> x(m), y(m), z(m);\n x[0] = y[0] = z[0] = 0;\n for(int i=1; i<m; ++i)\n cin >> x[i] >> y[i] >> z[i];\n\n vector<int> cost(1000000, INF);\n cost[0] = 0;\n for(int i=0; i<1000000; ++i){\n for(int j=0; j<n; ++j){\n int k = i + d[j];\n if(k < 1000000)\n cost[k] = min(cost[k], cost[i] + 1);\n }\n }\n for(int i=1000000-1; i>=2; --i)\n cost[i-2] = min(cost[i-2], cost[i]);\n\n vector<vector<int> > dp(1<<m, vector<int>(m, INF));\n dp[1][0] = 0;\n for(int i=0; i<(1<<m); ++i){\n for(int j=0; j<m; ++j){\n for(int k=0; k<m; ++k){\n int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);\n dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);\n }\n }\n }\n\n int ans = *min_element(dp.back().begin(), dp.back().end());\n if(ans < INF)\n cout << ans << endl;\n else\n cout << -1 << endl;\n\n return 0;\n}", "accuracy": 0.075, "time_ms": 10, "memory_kb": 4892, "score_of_the_acc": -0.0381, "final_rank": 16 }, { "submission_id": "aoj_1561_1249418", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nconst int INF = INT_MAX / 2;\n\nint main()\n{\n int n, m;\n cin >> n >> m;\n\n vector<int> d(n);\n int evenMax = -1;\n int oddMax = -1;\n for(int i=0; i<n; ++i){\n cin >> d[i];\n if(d[i] % 2 == 0)\n evenMax = max(evenMax, d[i]);\n else\n oddMax = max(oddMax, d[i]);\n }\n\n ++ m;\n vector<int> x(m), y(m), z(m);\n for(int i=1; i<m; ++i)\n cin >> x[i] >> y[i] >> z[i];\n\n vector<int> cost(1000000, INF);\n cost[0] = 0;\n for(int i=0; i<1000000; ++i){\n for(int j=0; j<n; ++j){\n if(i + d[j] < 1000000)\n cost[i+d[j]] = min(cost[i+d[j]], cost[i] + 1);\n }\n }\n for(int i=1000000-1; i>=2; --i)\n cost[i-2] = min(cost[i-2], max(2, cost[i]));\n\n vector<vector<int> > dp(1<<m, vector<int>(m, INF));\n dp[1][0] = 0;\n for(int i=0; i<(1<<m); ++i){\n for(int j=0; j<m; ++j){\n for(int k=0; k<m; ++k){\n int dist = abs(x[j] - x[k]) + abs(y[j] - y[k]) + abs(z[j] - z[k]);\n dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + cost[dist]);\n }\n }\n }\n\n int ans = *min_element(dp.back().begin(), dp.back().end());\n if(ans < INF)\n cout << ans << endl;\n else\n cout << -1 << endl;\n\n return 0;\n}", "accuracy": 0.225, "time_ms": 10, "memory_kb": 5152, "score_of_the_acc": -0.0456, "final_rank": 8 }, { "submission_id": "aoj_1561_1249213", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+9*1e+5;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d[16],c;\nint BIT[SIZE];\nvoid add(int x,int p){\n\twhile(x<=SIZE){\n\t\tBIT[x]+=p;\n\t\tx+=x&-x;\n\t}\n}\nint sum(int x){\n\tint sum=0;\n\twhile(x>0){\n\t\tsum+=BIT[x];\n\t\tx-=x&-x;\n\t}\n\treturn sum;\n}\nint calc(int x){\n if(x>=SIZE) return INF;\n if(isUselly[x] && x%2==0) return 2;\n if(DP[x]) return DP[x];\n return DP[x]=min(max(3,dist[x]),calc(x+2)); \n}\nvoid calc(){\n REP(i,SIZE){\n int k=SIZE-i-1;\n calc(k);\n }\n}\nint distances(int a,int b){\n int dif=0;\n REP(i,3)\n dif+=abs(point[b][i]-point[a][i]);\n int minium=min(dist[dif],calc(dif));\n if(sum(dif)>0 && dif%2)\n \tminium=min(minium,2);\n //cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n return minium;\n}\nLL sells(int now,int need){\n if(need==0) return 0;\n if(SDP[now][need]) return SDP[now][need];\n LL minium=INF;\n REP(i,M)\n if(need&(1<<i))\n minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n return SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n REP(i,N){\n cin >> d[i];\n edge.PB(pr(d[i],1));\n edge.PB(pr(-d[i],1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(next>0 && next<SIZE){\n if(nd==2)\n isUselly[next]=true;\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n\n REP(i,N)\n \tREP(j,N)\n \t\tif((d[i]^d[j])&1){\n \t\t\tadd(abs(d[i]-d[j]),1);\n \t\t\tadd(abs(d[i]+d[j]+1),-1);\n \t\t}\n REP(i,M){\n REP(j,3)\n cin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n REP(j,M+1)\n cost[i][j]=distances(i,j);\n \n LL ans=sells(M,(1<<M)-1);\n if(ans==INF)\n cout << -1 << endl;\n else\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 24008, "score_of_the_acc": -1.5883, "final_rank": 3 }, { "submission_id": "aoj_1561_1249194", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+2*1e+6;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d[16],c;\nint themin=INF,themax=0;\nint BIT[SIZE];\nvoid add(int x,int p){\n\twhile(x<=SIZE){\n\t\tBIT[x]+=p;\n\t\tx+=x&-x;\n\t}\n}\nint sum(int x){\n\tint sum=0;\n\twhile(x>0){\n\t\tsum+=BIT[x];\n\t\tx-=x&-x;\n\t}\n\treturn sum;\n}\nint calc(int x){\n\tif(x>SIZE) return INF;\n\tif(DP[x]) return DP[x];\n\tif(x%2)\n\treturn DP[x]=min(max(3,dist[x]),calc(x+2)); \n\treturn DP[x]=min(max(2,dist[x]),calc(x+2));\n}\nvoid calc(){\n\tREP(i,SIZE){\n\t\tint k=SIZE-i-1;\n\t\tcalc(k);\n\t}\n}\nint distances(int a,int b){\n\tint dif=0;\n\tREP(i,3)\n\t\tdif+=abs(point[b][i]-point[a][i]);\n\tint minium=min(dist[dif],calc(dif));\n\tif(sum(dif) && dif%2) minium=min(minium,2);\n\t//cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n\treturn minium;\n}\nLL sells(int now,int need){\n\tif(need==0)\treturn 0;\n\tif(SDP[now][need]) return SDP[now][need];\n\tLL minium=INF;\n\tREP(i,M)\n\t\tif(need&(1<<i))\n\t\t\tminium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n\treturn SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n if(M>15) return -1;\n if(N>15) return -1;\n REP(i,N){\n cin >> d[i];\n edge.PB(pr(d[i],1));\n edge.PB(pr(-d[i],1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(nd==2)\n \tisUselly[next]=true;\n if(next>=0 && next<SIZE){\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n REP(i,N)\n \tREP(j,N)\n \t\tif((d[i]^d[j])&1){\n \t\t\tadd(abs(d[i]-d[j]),1);\n \t\t\tadd(abs(d[i]+d[j]+1),-1);\n \t\t}\n REP(i,M){\n \tREP(j,3)\n \t\tcin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n \tREP(j,M+1)\n \t\tcost[i][j]=distances(i,j);\n\n LL ans=sells(M,(1<<M)-1);\n if(ans>=INF)\n \tcout << -1 << endl;\n else\n\t cout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.075, "time_ms": 60, "memory_kb": 26352, "score_of_the_acc": -0.9499, "final_rank": 18 }, { "submission_id": "aoj_1561_1249185", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+9*1e+5;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d[16],c;\nint BIT[SIZE];\nvoid add(int x,int p){\n\twhile(x<=SIZE){\n\t\tBIT[x]+=p;\n\t\tx+=x&-x;\n\t}\n}\nint sum(int x){\n\tint sum=0;\n\twhile(x>0){\n\t\tsum+=BIT[x];\n\t\tx-=x&-x;\n\t}\n\treturn sum;\n}\nint calc(int x){\n if(x>SIZE) return INF;\n if(isUselly[x] && x%2==0) return 2;\n if(DP[x]) return DP[x];\n return DP[x]=min(max(3,dist[x]),calc(x+2)); \n}\nvoid calc(){\n REP(i,SIZE){\n int k=SIZE-i-1;\n calc(k);\n }\n}\nint distances(int a,int b){\n int dif=0;\n REP(i,3)\n dif+=abs(point[b][i]-point[a][i]);\n int minium=min(dist[dif],calc(dif));\n if(sum(dif)>0 && dif%2)\n \tminium=min(minium,2);\n //cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n return minium;\n}\nLL sells(int now,int need){\n if(need==0) return 0;\n if(SDP[now][need]) return SDP[now][need];\n LL minium=INF;\n REP(i,M)\n if(need&(1<<i))\n minium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n return SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n REP(i,N){\n cin >> d[i];\n edge.PB(pr(d[i],1));\n edge.PB(pr(-d[i],1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(next>0 && next<SIZE){\n if(nd==2)\n isUselly[next]=true;\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n\n REP(i,N)\n \tREP(j,N)\n \t\tif((d[i]^d[j])&1){\n \t\t\tadd(abs(d[i]-d[j]),1);\n \t\t\tadd(abs(d[i]+d[j]+1),-1);\n \t\t}\n REP(i,M){\n REP(j,3)\n cin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n REP(j,M+1)\n cost[i][j]=distances(i,j);\n \n LL ans=sells(M,(1<<M)-1);\n if(ans==INF)\n cout << -1 << endl;\n else\n cout << ans << endl;\n}", "accuracy": 0.15, "time_ms": 30, "memory_kb": 15308, "score_of_the_acc": -0.4555, "final_rank": 9 }, { "submission_id": "aoj_1561_1249172", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+2*1e+6;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d[16],c;\nint themin=INF,themax=0;\nint BIT[SIZE];\nvoid add(int x,int p){\n\twhile(x<=SIZE){\n\t\tBIT[x]+=p;\n\t\tx+=x&-x;\n\t}\n}\nint sum(int x){\n\tint sum=0;\n\twhile(x>0){\n\t\tsum+=BIT[x];\n\t\tx-=x&-x;\n\t}\n\treturn sum;\n}\nint calc(int x){\n\tif(x>SIZE) return INF;\n\tif(DP[x]) return DP[x];\n\tif(x%2)\n\treturn DP[x]=min(max(3,dist[x]),calc(x+2)); \n\treturn DP[x]=min(max(2,dist[x]),calc(x+2));\n}\nvoid calc(){\n\tREP(i,SIZE){\n\t\tint k=SIZE-i-1;\n\t\tcalc(k);\n\t}\n}\nint distances(int a,int b){\n\tint dif=0;\n\tREP(i,3)\n\t\tdif+=abs(point[b][i]-point[a][i]);\n\tint minium=min(dist[dif],calc(dif));\n\tif(sum(dif) && dif%2) minium=min(minium,2);\n\t//cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n\treturn minium;\n}\nLL sells(int now,int need){\n\tif(need==0)\treturn 0;\n\tif(SDP[now][need]) return SDP[now][need];\n\tLL minium=INF;\n\tREP(i,M)\n\t\tif(need&(1<<i))\n\t\t\tminium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n\treturn SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n if(M>15) return -1;\n if(N>15) return -1;\n REP(i,N){\n cin >> d[i];\n edge.PB(pr(d[i],1));\n edge.PB(pr(-d[i],1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(nd==2)\n \tisUselly[next]=true;\n if(next>=0 && next<SIZE){\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n REP(i,N)\n \tREP(j,M)\n \t\tif((d[i]^d[j])&1){\n \t\t\tadd(abs(d[i]-d[j]),1);\n \t\t\tadd(abs(d[i]+d[j]+1),-1);\n \t\t}\n REP(i,M){\n \tREP(j,3)\n \t\tcin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n \tREP(j,M+1)\n \t\tcost[i][j]=distances(i,j);\n\n LL ans=sells(M,(1<<M)-1);\n if(ans>=INF)\n \tcout << -1 << endl;\n else\n\t cout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.075, "time_ms": 60, "memory_kb": 26352, "score_of_the_acc": -0.9499, "final_rank": 18 }, { "submission_id": "aoj_1561_1249159", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+2*1e+6;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d[16],c;\nint themin=INF,themax=0;\nint BIT[SIZE];\nvoid add(int x,int p){\n\twhile(x<=SIZE){\n\t\tBIT[x]+=p;\n\t\tx+=x&-x;\n\t}\n}\nint sum(int x){\n\tint sum=0;\n\twhile(x>0){\n\t\tsum+=BIT[x];\n\t\tx-=x&-x;\n\t}\n\treturn sum;\n}\nint calc(int x){\n\tif(x>SIZE) return INF;\n\tif(DP[x]) return DP[x];\n\tif(x%2)\n\treturn DP[x]=min(max(3,dist[x]),calc(x+2)); \n\treturn DP[x]=min(max(2,dist[x]),calc(x+2));\n}\nvoid calc(){\n\tREP(i,SIZE){\n\t\tint k=SIZE-i-1;\n\t\tcalc(k);\n\t}\n}\nint distances(int a,int b){\n\tint dif=0;\n\tREP(i,3)\n\t\tdif+=abs(point[b][i]-point[a][i]);\n\tint minium=min(dist[dif],calc(dif));\n\tif(sum(dif)) minium=min(minium,2);\n\t//cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n\treturn minium;\n}\nLL sells(int now,int need){\n\tif(need==0)\treturn 0;\n\tif(SDP[now][need]) return SDP[now][need];\n\tLL minium=INF;\n\tREP(i,M)\n\t\tif(need&(1<<i))\n\t\t\tminium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n\treturn SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n if(M>15) return -1;\n if(N>15) return -1;\n REP(i,N){\n cin >> d[i];\n edge.PB(pr(d[i],1));\n edge.PB(pr(-d[i],1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(nd==2)\n \tisUselly[next]=true;\n if(next>=0 && next<SIZE){\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n REP(i,N)\n \tREP(j,M)\n \t\tif((d[i]^d[j])&1){\n \t\t\tadd(abs(d[i]-d[j]),1);\n \t\t\tadd(abs(d[i]+d[j]+1),-1);\n \t\t}\n REP(i,M){\n \tREP(j,3)\n \t\tcin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n \tREP(j,M+1)\n \t\tcost[i][j]=distances(i,j);\n\n LL ans=sells(M,(1<<M)-1);\n if(ans>=INF)\n \tcout << -1 << endl;\n else\n\t cout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.075, "time_ms": 60, "memory_kb": 26352, "score_of_the_acc": -0.9499, "final_rank": 18 }, { "submission_id": "aoj_1561_1249116", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+2*1e+6;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d,c;\nint calc(int x){\n\tif(x>SIZE) return INF;\n\tif(isUselly[x]) return 2;\n\tif(DP[x]) return DP[x];\n\treturn DP[x]=min(max(3,dist[x]),calc(x+2)); \n}\nvoid calc(){\n\tREP(i,SIZE){\n\t\tint k=SIZE-i-1;\n\t\tcalc(k);\n\t}\n}\nint distances(int a,int b){\n\tint dif=0;\n\tREP(i,3)\n\t\tdif+=abs(point[b][i]-point[a][i]);\n\tint minium=min(dist[dif],calc(dif));\n\t//cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n\treturn minium;\n}\nLL sells(int now,int need){\n\tif(need==0)\treturn 0;\n\tif(SDP[now][need]) return SDP[now][need];\n\tLL minium=INF;\n\tREP(i,M)\n\t\tif(need&(1<<i))\n\t\t\tminium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n\treturn SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n if(M>15) return -1;\n if(N>15) return -1;\n REP(i,N){\n cin >> d;\n edge.PB(pr(d,1));\n edge.PB(pr(-d,1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(next>=0 && next<SIZE){\n \tif(nd==2)\n \t\tisUselly[next]=true;\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n REP(i,M){\n \tREP(j,3)\n \t\tcin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n \tREP(j,M+1)\n \t\tcost[i][j]=distances(i,j);\n\n LL ans=sells(M,(1<<M)-1);\n if(ans>=INF)\n \tcout << -1 << endl;\n else\n\t cout << ans << endl;\n\treturn 0;\n}", "accuracy": 0.15, "time_ms": 60, "memory_kb": 24652, "score_of_the_acc": -0.901, "final_rank": 11 }, { "submission_id": "aoj_1561_1249102", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+2*1e+6;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d,c;\nint calc(int x){\n\tif(x>SIZE) return INF;\n\tif(isUselly[x]) return 2;\n\tif(DP[x]) return DP[x];\n\treturn DP[x]=min(max(3,dist[x]),calc(x+2)); \n}\nvoid calc(){\n\tREP(i,SIZE){\n\t\tint k=SIZE-i-1;\n\t\tcalc(k);\n\t}\n}\nint distances(int a,int b){\n\tint dif=0;\n\tREP(i,3)\n\t\tdif+=abs(point[b][i]-point[a][i]);\n\tint minium=min(dist[dif],calc(dif));\n\t//cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n\treturn minium;\n}\nLL sells(int now,int need){\n\tif(need==0)\treturn 0;\n\tif(SDP[now][need]) return SDP[now][need];\n\tLL minium=INF;\n\tREP(i,M)\n\t\tif(need&(1<<i))\n\t\t\tminium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n\treturn SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n if(M>15) return -1;\n REP(i,N){\n cin >> d;\n edge.PB(pr(d,1));\n edge.PB(pr(-d,1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(next>=0 && next<SIZE){\n \tif(nd==2)\n \t\tisUselly[next]=true;\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n REP(i,M){\n \tREP(j,3)\n \t\tcin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n \tREP(j,M+1)\n \t\tcost[i][j]=distances(i,j);\n\n LL ans=sells(M,(1<<M)-1);\n if(ans>=INF)\n \tcout << -1 << endl;\n else\n\t cout << ans << endl;\n}", "accuracy": 0.15, "time_ms": 60, "memory_kb": 24652, "score_of_the_acc": -0.901, "final_rank": 11 }, { "submission_id": "aoj_1561_1249089", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+2*1e+6;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d,c;\nint calc(int x){\n\tif(x>SIZE) return INF;\n\tif(isUselly[x]) return 2;\n\tif(DP[x]) return DP[x];\n\treturn DP[x]=min(max(3,dist[x]),calc(x+2)); \n}\nvoid calc(){\n\tREP(i,SIZE){\n\t\tint k=SIZE-i-1;\n\t\tcalc(k);\n\t}\n}\nint distances(int a,int b){\n\tint dif=0;\n\tREP(i,3)\n\t\tdif+=abs(point[b][i]-point[a][i]);\n\tint minium=min(dist[dif],calc(dif));\n\t//cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n\treturn minium;\n}\nLL sells(int now,int need){\n\tif(need==0)\treturn 0;\n\tif(SDP[now][need]) return SDP[now][need];\n\tLL minium=INF;\n\tREP(i,M)\n\t\tif(need&(1<<i))\n\t\t\tminium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n\treturn SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n if(M>15) return -1;\n REP(i,N){\n cin >> d;\n edge.PB(pr(d,1));\n edge.PB(pr(-d,1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(next>0 && next<SIZE){\n \tif(nd==2)\n \t\tisUselly[next]=true;\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n REP(i,M){\n \tREP(j,3)\n \t\tcin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n \tREP(j,M+1)\n \t\tcost[i][j]=distances(i,j);\n\n LL ans=sells(M,(1<<M)-1);\n if(ans>=INF)\n \tcout << -1 << endl;\n else\n\t cout << ans << endl;\n}", "accuracy": 0.15, "time_ms": 60, "memory_kb": 24652, "score_of_the_acc": -0.901, "final_rank": 11 }, { "submission_id": "aoj_1561_1249083", "code_snippet": "#include <bits/stdc++.h>\n#define FOR(i,a,b) for(int i=(a);i<(b);i++)\n#define REP(i,b) FOR(i,0,b)\n#define F first\n#define S second\n#define X real()\n#define Y imag()\n#define PB push_back \n#define BE(c) c.begin(),c.end()\nusing namespace std;\ntypedef long long LL;\ntypedef complex<int> cld;\ntypedef LL ut;\ntypedef vector<ut> VI;\ntypedef pair<ut,ut> pr;\ntypedef vector<pr> Vpr;\ntypedef vector<ut,pr> ppr;\ntypedef priority_queue<pr,Vpr,greater<pr> > PQ;\ntypedef vector<ppr> Vppr;\nconst int SIZE=100+2*1e+6;\nconst int INF=1<<30;\nint dist[SIZE];\nVpr edge;\nPQ qu;\nint point[16][3];\nLL cost[16][16];\nint isUselly[SIZE];\nint DP[SIZE];\nint SDP[16][1<<16];\nint N,M,d,c;\nint calc(int x){\n\tif(x>SIZE) return INF;\n\tif(isUselly[x]) return 2;\n\tif(DP[x]) return DP[x];\n\treturn DP[x]=min(max(3,dist[x]),calc(x+2)); \n}\nvoid calc(){\n\tREP(i,SIZE){\n\t\tint k=SIZE-i-1;\n\t\tcalc(k);\n\t}\n}\nint distances(int a,int b){\n\tint dif=0;\n\tREP(i,3)\n\t\tdif+=abs(point[b][i]-point[a][i]);\n\tint minium=min(dist[dif],calc(dif));\n\t//cout << dif <<\" \" <<a<< \" \" <<b <<\" \" << minium << endl;\n\treturn minium;\n}\nLL sells(int now,int need){\n\tif(need==0)\treturn 0;\n\tif(SDP[now][need]) return SDP[now][need];\n\tLL minium=INF;\n\tREP(i,M)\n\t\tif(need&(1<<i))\n\t\t\tminium=min(minium,sells(i,need^(1<<i))+cost[now][i]);\n\treturn SDP[now][need]=minium;\n}\nint main(){\n FOR(i,1,SIZE)\n dist[i]=INF;\n cin >> N>> M;\n REP(i,N){\n cin >> d;\n edge.PB(pr(d,1));\n edge.PB(pr(-d,1));\n }\n qu.push(pr(0,0));\n while(!qu.empty()){\n int now=qu.top().S;\n int d=qu.top().F;\n qu.pop();\n if(d!=dist[now]) continue;\n REP(i,N*2){\n int next=now+edge[i].F;\n int nd=d+edge[i].S;\n if(next>0 && next<SIZE){\n \tif(nd==2)\n \t\tisUselly[next]=true;\n if(dist[next]>nd){\n dist[next]=nd;\n qu.push(pr(nd,next));\n }\n }\n }\n }\n REP(i,M){\n \tREP(j,3)\n \t\tcin >> point[i][j];\n }\n calc();\n REP(i,M+1)\n \tREP(j,M+1)\n \t\tcost[i][j]=distances(i,j);\n\n LL ans=sells(M,(1<<M)-1);\n if(ans==INF)\n \tcout << -1 << endl;\n else\n\t cout << ans << endl;\n}", "accuracy": 0.15, "time_ms": 60, "memory_kb": 24648, "score_of_the_acc": -0.9008, "final_rank": 10 } ]
aoj_1569_cpp
Bomb Removal Problem 縦 H ×横 W のグリッド上に N 個の点火された導火線とそれぞれに対する1つの爆弾が配置されている。それぞれの導火線と爆弾は、 L i 個のマスからなる path i 上に配置され、 path i の j 番目のマスを( px ij , py ij )とする。各爆弾は path i の最後のマス( px iL i , py iL i )に存在する。導火線の火は初期状態(0秒時点)でマス( px i1 , py i1 )にあり、1秒間に1マス進む(( px i1 , py i1 ),( px i2 , py i2 ),...,( px iL i , py iL i )の順に進む)。 最初、ロボットがマス( sx , sy ) にいる。このロボットは今いるマスの隣接する8方向のマスに1進む、または、そのマスに留まるのに1秒かかる。ただし、グリッドの外や爆弾のあるマスには移動することができない。ロボットは導火線の火があるマス上にいるとき消火することができる(同じマスに2つ以上の導火線の火がある場合はそれら全てを消すことができる)。 爆弾は導火線の火がマス( px iL i , py iL i )に到達したときに爆発する( path i の途中で導火線の火がマス( px iL i , py iL i )を通過したときは爆弾は爆発しない)。 全ての爆弾を爆発させずに全ての導火線の火を消火させるための最短の時間を出力せよ。ただし、どのように移動しても爆弾を爆発させてしまう場合は-1を出力せよ。なお、初期状態(0秒時点)でロボットが導火線の火の上にいる場合、消火することができる。 Input 入力は以下の形式で与えられる。 W H N sx sy L 1 path 1 L 2 path 2 ... L N path N 1行目に3つの整数 W , H , N が空白区切りで与えられる。2行目に2つの整数 sx , sy が空白区切りで与えられる。3行目から N +2行目に L i と path i が与えられる。 path i は以下の形式で与えられる。 px i1 py i1 px i2 py i2 ... px iL i py iL i Constraints 2 ≤ H , W ≤ 20 1 ≤ N ≤ 5 1 ≤ sx ≤ W 1 ≤ sy ≤ H 2 ≤ L i ≤ H + W 1 ≤ px ij ≤ W 1 ≤ py ij ≤ H path i の隣り合うマスは上下左右に隣接している。 導火線は互いに独立している(他の導火線に影響しない)。 爆弾はマス( sx , sy )には置かれない。 Output ロボットが全ての導火線の火を消火させるための最短時間または -1 を出力せよ。 Sample Input 1 2 2 1 2 1 3 1 1 1 2 2 2 Sample Output 1 1 サンプル1に対応する図は以下の通りである。 ロボットは1秒後に左下の(1, 2)のマスに移動することにより消火することができる。 Sample Input 2 2 2 1 2 1 2 1 1 1 2 Sample Output 2 -1 サンプル2に対応する図は以下の通りである。 ロボットは、(1, 1)または(2, 2)のマスに移動する、または今いるマス(2, 1)に留まることができるがいずれにせよ1秒後に爆弾が爆発してしまう。 Sample Input 3 3 3 2 1 3 3 2 3 2 2 2 1 5 2 1 1 1 1 2 2 2 3 2 Sample Output 3 2 サンプル3に対応する図は以下の通りである。 ロボットは、(2, 2)のマスに移動し、(1, 2)のマスに移動することにより2秒で全ての導火線の火を消火することができる。 Sample Input 4 3 3 1 2 2 2 2 2 2 1 Sample Output 4 0 ロボットは、初期状態(0秒時点)で(2, 2)のマスにいて、導火線の火も(2, 2)のマスにあるので0秒で消火することができる。
[ { "submission_id": "aoj_1569_10946142", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\n\n#define DUMP(x) cout<<#x<<\":\"<<(x)<<endl\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class T>\nistream& operator>>(istream& is, vector<T>& xs){\n for(auto& x: xs)\n\tis >> x;\n return is;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, const pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nostream& operator<<(ostream& os, const vector<T>& xs){\n for(unsigned int i=0;i<xs.size();++i)\n\tos << (i?\" \":\"\") << xs[i];\n return os;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\nconst int INF = 1e9;\n\nint dp[1<<5][41][21][21];\nint dir[] = {-1,0,1};\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int W, H, N;\n cin >> W >> H >> N;\n int BB = (1<<N) - 1;\n PII s;\n cin >> s;\n --s.FF;\n --s.SS;\n vector<vector<PII>> bm(N);\n int init = 0;\n REP(i,N){\n\tint L;\n\tcin >> L;\n\tREP(j,L){\n\t PII p;\n\t cin >> p;\n\t --p.FF;\n\t --p.SS;\n\t bm[i].EB(p);\n\t}\n\tif(bm[i][0] == s) init |= 1<<i;\n }\n\n auto isin = [W,H](const PII& p){ return 0 <= p.FF && p.FF < W && 0 <= p.SS && p.SS < H; };\n using D = pair<PII,PII>;\n fill((int*)dp, (int*)dp+(1<<5)*41*21*21, INF);\n queue<D> q;\n q.push(MP(MP(init,0),s));\n dp[init][0][s.FF][s.SS] = 0;\n int ans = -1;\n while(!q.empty()){\n\tauto dd = q.front();\n\tq.pop();\n\tif(dd.FF.FF == BB){\n\t ans = dd.FF.SS;\n\t break;\n\t}\n\n\tint ntime = dd.FF.SS+1;\n\tREP(dx,3) REP(dy,3){\n\t PII np(dd.SS.FF+dir[dx], dd.SS.SS+dir[dy]);\n\t if(!isin(np)) continue;\n\t bool ok = true;\n\t REP(i,N) if(bm[i].back() == np){ ok = false; break;}\n\t if(!ok) continue;\n\n\t bool bomb = false;\n\t PII ns(dd.FF.FF, ntime);\n\t REP(i,N){\n\t\tif(ns.FF>>i&1) continue;\n\t\tif(ntime >= SZ(bm[i])-1){\n\t\t bomb = true;\n\t\t break;\n\t\t}\n\t\telse if(bm[i][ntime] == np)\n\t\t ns.FF |= 1<<i;\n\t }\n\t if(bomb) continue;\n\n\t if(dp[ns.FF][ns.SS][np.FF][np.SS] == INF){\n\t\tdp[ns.FF][ns.SS][np.FF][np.SS] = ntime;\n\t\tq.push(MP(ns,np));\n\t }\n\t}\n }\n \n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5872, "score_of_the_acc": -0.2445, "final_rank": 10 }, { "submission_id": "aoj_1569_3367854", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i,n) REP(i,0,n)\n#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)\n#define repr(i, n) REPR(i, n, 0)\n#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)\n#define pb push_back\n#define all(r) r.begin(),r.end()\n#define rall(r) r.rbegin(),r.rend()\n#define fi first\n#define se second\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\nconst int INF = 1e9;\nconst ll MOD = 1e9 + 7;\ndouble EPS = 1e-8;\n\n// #define DEBUG_MODE\n#ifdef DEBUG_MODE\n#define dump(x) cout << #x << \" : \" << x << endl\n#define LINE cout << \"line : \" << __LINE__ << endl\n#define dumpV(v) cout << #v << \" : [\"; for(auto& t : v) cout << t << \", \"; cout<<\"]\" << endl\n#define STOP assert(false)\n#else\n#define dump(x) ;\n#define LINE ;\n#define dumpV(v);\n#define STOP ;\n#endif\n#define mp make_pair\n\nnamespace std{\n template<class S,class T>\n ostream &operator <<(ostream& out,const pair<S,T>& a){\n out<<'('<<a.fi<<\", \"<<a.se<<')';\n return out;\n }\n}\n\n\n\nconst int MAX_N = 5;\nconst int MAX_H = 20;\nconst int MAX_W = 20;\nconst int MAX_HW = MAX_H * MAX_W;\n\n\n// vertex => y, x, mask | 20 * 20 * 2^5\n// edge => (x, y, t)^2 | (400*40)^2\n\nint dist[MAX_HW][1 << MAX_N];\n\nint times[MAX_HW][MAX_HW];\n\nbool isBomb[MAX_HW];\n\nauto isOutOfRange = [](int h, int w, int H, int W) {\n return h < 0 || h >= H || w < 0 || w >= W;\n};\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int W, H, N;\n cin >> W >> H >> N;\n int sx, sy;\n cin >> sx >> sy;\n --sx; --sy;\n vector<vector<int>> paths(N);\n rep(i, N) {\n int sz;\n cin >> sz;\n paths[i].resize(sz-1);\n rep(j, sz) {\n int x, y;\n cin >> x >> y;\n --x; --y;\n if (j + 1 == sz) isBomb[y * W + x] = true;\n else paths[i][j] = (y * W + x);\n }\n }\n int HW = H * W;\n {\n rep(i, HW) rep(j, HW) times[i][j] = (i == j ? 0 : INF);\n rep(y, H) rep(x, W) if (!isBomb[y * W + x]) {\n REP(dy, -1, 2) REP(dx, -1, 2) if(dx|dy) {\n int nx = x + dx, ny = y + dy;\n if (isOutOfRange(ny, nx, H, W) || isBomb[ny * W + nx]) continue;\n times[y * W + x][ny * W + nx] = 1;\n }\n }\n rep(k, HW) rep(i, HW) rep(j, HW) times[i][j] = min(times[i][j], times[i][k] + times[k][j]);\n }\n rep(i, MAX_HW) rep(j, 1 << MAX_N) dist[i][j] = INF;\n using P = pair<int, pii>;\n priority_queue<P, vector<P>, greater<P> > q;\n {\n int mask = 0;\n int S = sy * W + sx;\n rep(i, N) {\n if (S == paths[i][0]) mask |= (1 << i);\n }\n dist[S][mask] = 0;\n q.push({0, {S, mask}});\n }\n\n {\n while (!q.empty()) {\n auto p = q.top(); q.pop();\n auto yx = p.se.fi, mask = p.se.se, t = p.fi;\n if (dist[yx][mask] < t) continue;\n dump(p);\n rep(i, N) if ((mask & (1 << i)) == 0) {\n int nmask = mask | (1<<i);\n REP(j, t, paths[i].size()) {\n auto nyx = paths[i][j];\n int dt = j - t;\n dump(mp(i, j));\n dump(times[yx][nyx]);\n if(times[yx][nyx] <= dt && j < dist[nyx][nmask]) {\n dist[nyx][nmask] = j;\n q.push({j, {nyx, nmask}});\n }\n }\n }\n }\n };\n int ans = INF;\n rep(i, HW) ans = min(ans, dist[i][(1<<N)-1]);\n cout << (ans == INF ? -1 : ans) << '\\n';\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3892, "score_of_the_acc": -0.5754, "final_rank": 12 }, { "submission_id": "aoj_1569_3367853", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i,n) REP(i,0,n)\n#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)\n#define repr(i, n) REPR(i, n, 0)\n#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)\n#define pb push_back\n#define all(r) r.begin(),r.end()\n#define rall(r) r.rbegin(),r.rend()\n#define fi first\n#define se second\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\nconst int INF = 1e9;\nconst ll MOD = 1e9 + 7;\ndouble EPS = 1e-8;\n\n// #define DEBUG_MODE\n#ifdef DEBUG_MODE\n#define dump(x) cout << #x << \" : \" << x << endl\n#define LINE cout << \"line : \" << __LINE__ << endl\n#define dumpV(v) cout << #v << \" : [\"; for(auto& t : v) cout << t << \", \"; cout<<\"]\" << endl\n#define STOP assert(false)\n#else\n#define dump(x) ;\n#define LINE ;\n#define dumpV(v);\n#define STOP ;\n#endif\n#define mp make_pair\n\nnamespace std{\n template<class S,class T>\n ostream &operator <<(ostream& out,const pair<S,T>& a){\n out<<'('<<a.fi<<\", \"<<a.se<<')';\n return out;\n }\n}\n\n\n\nconst int MAX_N = 5;\nconst int MAX_H = 20;\nconst int MAX_W = 20;\nconst int MAX_HW = MAX_H * MAX_W;\n\n\n// vertex => y, x, mask | 20 * 20 * 2^5\n// edge => (x, y, t)^2 | (400*40)^2\n\nint dist[MAX_HW][1 << MAX_N];\n\nint times[MAX_HW][MAX_HW];\n\nbool isBomb[MAX_HW];\n\nauto isOutOfRange = [](int h, int w, int H, int W) {\n return h < 0 || h >= H || w < 0 || w >= W;\n};\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int W, H, N;\n cin >> W >> H >> N;\n int sx, sy;\n cin >> sx >> sy;\n --sx; --sy;\n vector<vector<int>> paths(N);\n rep(i, N) {\n int sz;\n cin >> sz;\n paths[i].resize(sz-1);\n rep(j, sz) {\n int x, y;\n cin >> x >> y;\n --x; --y;\n if (j + 1 == sz) isBomb[y * W + x] = true;\n else paths[i][j] = (y * W + x);\n }\n }\n int HW = H * W;\n {\n rep(i, HW) rep(j, HW) times[i][j] = (i == j ? 0 : INF);\n rep(y, H) rep(x, W) if (!isBomb[y * W + x]) {\n REP(dy, -1, 2) REP(dx, -1, 2) if(dx|dy) {\n int nx = x + dx, ny = y + dy;\n if (isOutOfRange(ny, nx, H, W) || isBomb[ny * W + nx]) continue;\n times[y * W + x][ny * W + nx] = 1;\n }\n }\n rep(k, HW) rep(i, HW) rep(j, HW) times[i][j] = min(times[i][j], times[i][k] + times[k][j]);\n }\n rep(i, MAX_HW) rep(j, 1 << MAX_N) dist[i][j] = INF;\n using P = pair<int, pii>;\n priority_queue<P, vector<P>, greater<P> > q;\n {\n int mask = 0;\n int S = sy * W + sx;\n rep(i, N) {\n if (S == paths[i][0]) mask |= (1 << i);\n }\n dist[S][mask] = 0;\n q.push({0, {S, mask}});\n }\n\n {\n while (!q.empty()) {\n auto p = q.top(); q.pop();\n auto yx = p.se.fi, mask = p.se.se, t = p.fi;\n if (dist[yx][mask] < t) continue;\n dump(p);\n rep(i, N) if ((mask & (1 << i)) == 0) {\n int nmask = mask | (1<<i);\n REP(j, t + 1, paths[i].size()) {\n auto nyx = paths[i][j];\n int dt = j - t;\n dump(mp(i, j));\n dump(times[yx][nyx]);\n if(times[yx][nyx] <= dt && j < dist[nyx][nmask]) {\n dist[nyx][nmask] = j;\n q.push({j, {nyx, nmask}});\n }\n }\n }\n }\n };\n int ans = INF;\n rep(i, HW) ans = min(ans, dist[i][(1<<N)-1]);\n cout << (ans == INF ? -1 : ans) << '\\n';\n return 0;\n}", "accuracy": 0.45, "time_ms": 40, "memory_kb": 3792, "score_of_the_acc": -0.4586, "final_rank": 17 }, { "submission_id": "aoj_1569_3367851", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i,n) REP(i,0,n)\n#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)\n#define repr(i, n) REPR(i, n, 0)\n#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)\n#define pb push_back\n#define all(r) r.begin(),r.end()\n#define rall(r) r.rbegin(),r.rend()\n#define fi first\n#define se second\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\n\nconst int INF = 1e9;\nconst ll MOD = 1e9 + 7;\ndouble EPS = 1e-8;\n\n// #define DEBUG_MODE\n#ifdef DEBUG_MODE\n#define dump(x) cout << #x << \" : \" << x << endl\n#define LINE cout << \"line : \" << __LINE__ << endl\n#define dumpV(v) cout << #v << \" : [\"; for(auto& t : v) cout << t << \", \"; cout<<\"]\" << endl\n#define STOP assert(false)\n#else\n#define dump(x) ;\n#define LINE ;\n#define dumpV(v);\n#define STOP ;\n#endif\n#define mp make_pair\n\nnamespace std{\n template<class S,class T>\n ostream &operator <<(ostream& out,const pair<S,T>& a){\n out<<'('<<a.fi<<\", \"<<a.se<<')';\n return out;\n }\n}\n\n\n\nconst int MAX_N = 5;\nconst int MAX_H = 20;\nconst int MAX_W = 20;\nconst int MAX_HW = MAX_H * MAX_W;\n\n\n// vertex => y, x, mask | 20 * 20 * 2^5\n// edge => (x, y, t)^2 | (400*40)^2\n\nint dist[MAX_HW][1 << MAX_N];\n\nint times[MAX_HW][MAX_HW];\n\nbool isBomb[MAX_HW];\n\nauto isOutOfRange = [](int h, int w, int H, int W) {\n return h < 0 || h >= H || w < 0 || w >= W;\n};\n\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int W, H, N;\n cin >> W >> H >> N;\n int sx, sy;\n cin >> sx >> sy;\n --sx; --sy;\n vector<vector<int>> paths(N);\n rep(i, N) {\n int sz;\n cin >> sz;\n paths[i].resize(sz-1);\n rep(j, sz) {\n int x, y;\n cin >> x >> y;\n --x; --y;\n if (j + 1 == sz) isBomb[y * W + x] = true;\n else paths[i][j] = (y * W + x);\n }\n }\n int HW = H * W;\n {\n rep(i, HW) rep(j, HW) times[i][j] = (i == j ? 0 : INF);\n rep(y, H) rep(x, W) if (!isBomb[y * W + x]) {\n REP(dy, -1, 2) REP(dx, -1, 2) {\n int nx = x + dx, ny = y + dy;\n if (isOutOfRange(ny, nx, H, W) || isBomb[ny * W + nx]) continue;\n times[y * W + x][ny * W + nx] = 1;\n }\n }\n rep(k, HW) rep(i, HW) rep(j, HW) times[i][j] = min(times[i][j], times[i][k] + times[k][j]);\n }\n rep(i, MAX_HW) rep(j, 1 << MAX_N) dist[i][j] = INF;\n using P = pair<int, pii>;\n priority_queue<P, vector<P>, greater<P> > q;\n {\n int mask = 0;\n int S = sy * W + sx;\n rep(i, N) {\n if (S == paths[i][0]) mask |= (1 << i);\n }\n dist[S][mask] = 0;\n q.push({0, {S, mask}});\n }\n\n {\n while (!q.empty()) {\n auto p = q.top(); q.pop();\n auto yx = p.se.fi, mask = p.se.se, t = p.fi;\n if (dist[yx][mask] < t) continue;\n dump(p);\n rep(i, N) if ((mask & (1 << i)) == 0) {\n int nmask = mask | (1<<i);\n REP(j, t + 1, paths[i].size()) {\n auto nyx = paths[i][j];\n int dt = j - t;\n dump(mp(i, j));\n dump(times[yx][nyx]);\n if(times[yx][nyx] <= dt && j < dist[nyx][nmask]) {\n dist[nyx][nmask] = j;\n q.push({j, {nyx, nmask}});\n }\n }\n }\n }\n };\n int ans = INF;\n rep(i, HW) ans = min(ans, dist[i][(1<<N)-1]);\n cout << (ans == INF ? -1 : ans) << '\\n';\n return 0;\n}", "accuracy": 0.45, "time_ms": 50, "memory_kb": 3844, "score_of_the_acc": -0.5727, "final_rank": 18 }, { "submission_id": "aoj_1569_3082762", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nusing pi = pair<int,int>;\n\nconst int dx[9]={1,1,1,0,0,0,-1,-1,-1};\nconst int dy[9]={1,0,-1,1,0,-1,1,0,-1};\n\nconst int T = 44;\nbool vis[20][20][T][1<<5]={};\n\nstruct State{\n int y,x,t,mask;\n};\n\nint solve(){\n int w,h,n;\n cin >>w >>h >>n;\n\n pi s;\n cin >>s.se >>s.fi;\n --s.fi;\n --s.se;\n\n set<pi> bomb;\n vector<vector<pi>> l(n);\n rep(i,n){\n int L;\n cin >>L;\n rep(j,L){\n int x,y;\n cin >>x >>y;\n --x;\n --y;\n l[i].pb({y,x});\n }\n bomb.insert(l[i][L-1]);\n }\n\n auto IN = [&](int y, int x){\n return 0<=y && y<h && 0<=x && x<w;\n };\n\n queue<State> que;\n vis[s.fi][s.se][0][0] = true;\n que.push({s.fi,s.se,0,0});\n while(!que.empty()){\n State c = que.front();\n que.pop();\n if(c.t==T-1) break;\n\n int nmask = c.mask;\n rep(i,n){\n int L = l[i].size();\n if(c.t<L && l[i][c.t] == pi(c.y,c.x)) nmask |= (1<<i);\n }\n if(!vis[c.y][c.x][c.t][nmask]){\n vis[c.y][c.x][c.t][nmask] = true;\n que.push({c.y,c.x,c.t,nmask});\n }\n\n rep(d,9){\n int ny = c.y+dy[d], nx = c.x+dx[d];\n if(IN(ny,nx) && !bomb.count({ny,nx}) && !vis[ny][nx][c.t+1][c.mask]){\n vis[ny][nx][c.t+1][c.mask] = true;\n que.push({ny,nx,c.t+1,c.mask});\n }\n }\n }\n\n rep(i,T){\n rep(y,h)rep(x,w){\n if(vis[y][x][i][(1<<n)-1]) return i;\n }\n }\n return -1;\n}\n\nint main(){\n cout << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3836, "score_of_the_acc": -0.1278, "final_rank": 4 }, { "submission_id": "aoj_1569_3077981", "code_snippet": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint w,h,n;\nint l[5];\nP path[5][50];\nbool dp[405][25][25][1<<6];\nint fie[25][25];\n\nint solve(int sx,int sy){\n\tmemset(dp,false,sizeof(dp));\n\t\n\tint rect=0;\n\tfor(int a=0;a<n;a++){\n\t\tP p=path[a][0];\n\t\tif(p.first==sy && p.second==sx)rect+=(1<<a);\n\t}\n\tdp[0][sy][sx][rect]=true;\n\tfor(int i=0;i<=w+h+1;i++){\n\t\tfor(int bit=0;bit<(1<<n);bit++){\n\t\t\tfor(int j=0;j<h;j++){\n\t\t\t\tfor(int k=0;k<w;k++){\n\t\t\t\t\tif(!dp[i][j][k][bit])continue;\n\t\t\t\t\t//printf(\"%d %d %d %d\\n\",i,j,k,bit);\n\t\t\t\t\tif((bit+1)==(1<<n))return i;\n\t\t\t\t\tfor(int a=-1;a<=1;a++){\n\t\t\t\t\t\tfor(int b=-1;b<=1;b++){\n\t\t\t\t\t\t\tint nj=j+a;\n\t\t\t\t\t\t\tint nk=k+b;\n\t\t\t\t\t\t\tif(nj<0 || nj>=h || nk<0 || nk>=w)continue;\n\t\t\t\t\t\t\tif(fie[nj][nk]==-1)continue;\n\t\t\t\t\t\t\tint rec=0;\n\t\t\t\t\t\t\tfor(int g=0;g<n;g++){\n\t\t\t\t\t\t\t\tif(bit>>g & 1)continue;\n\t\t\t\t\t\t\t\tP p=path[g][i+1];\n\t\t\t\t\t\t\t\tif(p.first==nj && p.second==nk)rec+=(1<<g);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tdp[i+1][nj][nk][bit+rec]=true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn -1;\n}\n\nint main(void){\n\tscanf(\"%d%d%d\",&w,&h,&n);\n\tint sx,sy;\n\tscanf(\"%d%d\",&sx,&sy);\n\tsx--;\n\tsy--;\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%d\",&l[i]);\n\t\tfor(int j=0;j<l[i];j++){\n\t\t\tP p;\n\t\t\tscanf(\"%d%d\",&p.second,&p.first);\n\t\t\tp.second--;\n\t\t\tp.first--;\n\t\t\tpath[i][j]=p;\n\t\t\tif((j+1)==l[i]){\n\t\t\t\tfie[p.first][p.second]=-1;\n\t\t\t\tpath[i][j]=P(-1,-1);\n\t\t\t}\n\t\t}\n\t\tfor(int j=l[i];j<50;j++){\n\t\t\tpath[i][j]=P(-1,-1);\n\t\t}\n\t}\n\tprintf(\"%d\\n\",solve(sx,sy));\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 19048, "score_of_the_acc": -1, "final_rank": 14 }, { "submission_id": "aoj_1569_3077847", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\n#define INF 1e16\n#define mod 1000000007\n\nll W,H,N;\nll sx,sy;\nvector<P> ps[5];\nmap<P,bool> dame;\n\nbool vis[20][20][50][1<<5];\nll di[]={-1,-1,-1,0,0,1,1,1,0};\nll dj[]={-1,0,1,-1,1,-1,0,1,0};\n\nstruct state{\n ll i,j,t,S;\n};\n\nint main(){\n cin>>W>>H>>N;\n cin>>sx>>sy;\n sx--;sy--;\n rep(i,N){\n int tmp;\n cin>>tmp;\n rep(j,tmp){\n int a,b;\n cin>>b>>a;\n a--;b--;\n ps[i].push_back(P(a,b));\n }\n dame[ps[i][tmp-1]]=true;\n }\n\n queue<state> que;\n vis[sy][sx][0][0]=true;\n que.push((state){sy,sx,0,0});\n while(que.size()){\n state crt=que.front(); que.pop();\n\n ll nS=crt.S;\n rep(i,N){\n if(crt.t<(ll)ps[i].size()&&ps[i][crt.t]==P(crt.i,crt.j))nS|=(1<<i);\n }\n if(!vis[crt.i][crt.j][crt.t][nS]){\n vis[crt.i][crt.j][crt.t][nS]=true;\n que.push((state){crt.i,crt.j,crt.t,nS});\n }\n\n rep(dir,9){\n ll ni=crt.i+di[dir],nj=crt.j+dj[dir];\n if(ni>=0&&ni<H&&nj>=0&&nj<W&&crt.t+1<50&&!dame[P(ni,nj)]&&!vis[ni][nj][crt.t+1][crt.S]){\n vis[ni][nj][crt.t+1][crt.S]=true;\n que.push((state){ni,nj,crt.t+1,crt.S});\n }\n }\n }\n\n rep(t,50){\n rep(i,H)rep(j,W){\n if(vis[i][j][t][(1<<N)-1]){\n cout<<t<<endl;\n return 0;\n }\n }\n }\n cout<<-1<<endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4088, "score_of_the_acc": -1.1422, "final_rank": 15 }, { "submission_id": "aoj_1569_2839927", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\n\nint W, H, N;\nvector<vector<int>> get_dis(int nx, int ny, const vector<pair<int,int>>&bombs) {\n\tint dx[] = { -1,-1,0,1,1,1,0,-1 };\n\tint dy[] = { 0,1,1,1,0,-1,-1,-1 };\n\n\n\tvector<vector<int>>times(H,vector<int>(W,1e9));\n\n\ttimes[ny][nx]=0;\n\n\tqueue<pair<int,int>>que;\n\tque.emplace(nx,ny);\n\twhile (!que.empty()) {\n\t\tauto atop(que.front());\n\t\tque.pop();\n\n\t\tint now_x=atop.first;\n\t\tint now_y=atop.second;\n\t\tint now_time=times[now_y][now_x];\n\t\tfor (int way = 0; way < 8; ++way) {\n\t\t\tint next_x=now_x+dx[way];\n\t\t\tint next_y=now_y+dy[way];\n\n\t\t\tif(next_x<0||next_x>=W||next_y<0||next_y>=H)continue;\n\t\t\tif(find(bombs.begin(),bombs.end(),make_pair(next_x,next_y))!=bombs.end())continue;\n\n\t\t\tif (times[next_y][next_x] > now_time + 1) {\n\t\t\t\ttimes[next_y][next_x]=now_time+1;\n\t\t\t\tque.push(make_pair(next_x,next_y));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn times;\n}\n\nint get_atime(int nx, int ny, const vector<pair<int, int>>&path, const int ntime,const vector<pair<int,int>>bombs) {\n\n\tauto times=get_dis(nx,ny,bombs);\n\n\tfor (int i = ntime; i < path.size(); ++i) {\n\t\tint px=path[i].first;\n\t\tint py=path[i].second;\n\n\t\tint dis=times[py][px];\n\n\t\tif(dis<=i-ntime)return i;\n\t}\n\treturn -1;\n}\n\n\nint main() {cin>>W>>H>>N;\n\n\tint sx,sy;cin>>sx>>sy;\n\tsx--;sy--;\n\n\tvector<vector<pair<int,int>>>paths(N);\n\n\tvector<pair<int,int>>bombs(N);\n\tfor (int i = 0; i < N; ++i) {\n\t\tint p;cin>>p;\n\t\tfor (int j = 0; j < p; ++j) {\n\t\t\tint x,y;cin>>x>>y;x--;y--;\n\n\t\t\tif (j == p - 1) {\n\t\t\t\tbombs.emplace_back(x,y);\n\t\t\t}\n\t\t\telse {\n\n\t\t\t\tpaths[i].emplace_back(x, y);\n\n\t\t\t}\n\t\t}\n\t}\n\n\tvector<int>perms(N);\n\n\tiota(perms.begin(),perms.end(),0);\n\n\n\tint ans=1e8;\n\tdo {\n\n\t\tint now_x=sx;\n\t\tint now_y=sy;\n\t\tint now_time=0;\n\t\tfor (int i = 0; i < N; ++i) {\n\t\t\tconst int next_bomb_id=perms[i];\n\n\t\t\tint next_time=get_atime(now_x,now_y,paths[next_bomb_id],now_time,bombs);\n\t\t\tif (next_time == -1) {\n\t\t\t\tnow_time=1e8;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tnow_time=next_time;\n\t\t\t\tnow_x=paths[next_bomb_id][next_time].first;\n\t\t\t\tnow_y=paths[next_bomb_id][next_time].second;\n\t\t\t}\n\t\t}\n\t\tans=min(ans,now_time);\n\t}while(next_permutation(perms.begin(),perms.end()));\n\n\tif(ans>1e7)ans=-1;\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3172, "score_of_the_acc": -0.0897, "final_rank": 2 }, { "submission_id": "aoj_1569_2701042", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\n\n#define DUMP(x) cout<<#x<<\":\"<<(x)<<endl\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class T>\nistream& operator>>(istream& is, vector<T>& xs){\n for(auto& x: xs)\n\tis >> x;\n return is;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, const pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nostream& operator<<(ostream& os, const vector<T>& xs){\n for(unsigned int i=0;i<xs.size();++i)\n\tos << (i?\" \":\"\") << xs[i];\n return os;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\nconst int INF = 1e9;\n\nint dp[1<<5][41][21][21];\nint dir[] = {-1,0,1};\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int W, H, N;\n cin >> W >> H >> N;\n int BB = (1<<N) - 1;\n PII s;\n cin >> s;\n --s.FF;\n --s.SS;\n vector<vector<PII>> bm(N);\n int init = 0;\n REP(i,N){\n\tint L;\n\tcin >> L;\n\tREP(j,L){\n\t PII p;\n\t cin >> p;\n\t --p.FF;\n\t --p.SS;\n\t bm[i].EB(p);\n\t}\n\tif(bm[i][0] == s) init |= 1<<i;\n }\n\n auto isin = [W,H](const PII& p){ return 0 <= p.FF && p.FF < W && 0 <= p.SS && p.SS < H; };\n using D = pair<PII,PII>;\n fill((int*)dp, (int*)dp+(1<<5)*41*21*21, INF);\n queue<D> q;\n q.push(MP(MP(init,0),s));\n dp[init][0][s.FF][s.SS] = 0;\n int ans = -1;\n while(!q.empty()){\n\tauto dd = q.front();\n\tq.pop();\n\tif(dd.FF.FF == BB){\n\t ans = dd.FF.SS;\n\t break;\n\t}\n\n\tint ntime = dd.FF.SS+1;\n\tREP(dx,3) REP(dy,3){\n\t PII np(dd.SS.FF+dir[dx], dd.SS.SS+dir[dy]);\n\t if(!isin(np)) continue;\n\t bool ok = true;\n\t REP(i,N) if(bm[i].back() == np){ ok = false; break;}\n\t if(!ok) continue;\n\n\t bool bomb = false;\n\t PII ns(dd.FF.FF, ntime);\n\t REP(i,N){\n\t\tif(ns.FF>>i&1) continue;\n\t\tif(ntime >= SZ(bm[i])-1){\n\t\t bomb = true;\n\t\t break;\n\t\t}\n\t\telse if(bm[i][ntime] == np)\n\t\t ns.FF |= 1<<i;\n\t }\n\t if(bomb) continue;\n\n\t if(dp[ns.FF][ns.SS][np.FF][np.SS] == INF){\n\t\tdp[ns.FF][ns.SS][np.FF][np.SS] = ntime;\n\t\tq.push(MP(ns,np));\n\t }\n\t}\n }\n \n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5496, "score_of_the_acc": -0.2229, "final_rank": 7 }, { "submission_id": "aoj_1569_2690372", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Data{\n\tData(){\n\t\trow = col = 0;\n\t}\n\tData(int arg_row,int arg_col){\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t}\n\tint row,col;\n};\n\nstruct Info{\n\tInfo(int arg_state,int arg_row,int arg_col,int arg_time){\n\t\tstate = arg_state;\n\t\trow = arg_row;\n\t\tcol = arg_col;\n\t\ttime = arg_time;\n\t}\n\tint state,row,col,time;\n};\n\nint W,H,N;\nint num_path[5];\nbool state_check[20][20][32][41];\nint diff_row[9] = {-1,-1,-1,0,0,0,1,1,1},diff_col[9] = {-1,0,1,-1,0,1,-1,0,1};\nbool bomb_check[20][20];\nData path[5][40];\n\nbool rangeCheck(int row,int col){\n\tif(row >= 0 && row <= H-1 && col >= 0 && col <= W-1){\n\t\treturn true;\n\t}else{\n\t\treturn false;\n\t}\n}\n\nint main(){\n\n\tint POW[6];\n\tfor(int i = 0; i < 6; i++)POW[i] = pow(2,i);\n\n\n\tscanf(\"%d %d %d\",&W,&H,&N);\n\n\tint start_col,start_row;\n\tscanf(\"%d %d\",&start_col,&start_row);\n\tstart_col--;\n\tstart_row--;\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++)bomb_check[row][col] = false;\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%d\",&num_path[i]);\n\n\t\tfor(int k = 0; k < num_path[i]; k++){\n\t\t\tscanf(\"%d %d\",&path[i][k].col,&path[i][k].row);\n\t\t\tpath[i][k].col--;\n\t\t\tpath[i][k].row--;\n\t\t\tif(k == num_path[i]-1){\n\t\t\t\tbomb_check[path[i][k].row][path[i][k].col] = true;\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int row = 0; row < H; row++){\n\t\tfor(int col = 0; col < W; col++){\n\t\t\tfor(int state = 0; state < POW[N]; state++){\n\t\t\t\tfor(int time = 0; time <= 40; time++)state_check[row][col][state][time] = false;\n\t\t\t}\n\t\t}\n\t}\n\n\tint first_state = 0;\n\tfor(int i = 0; i < N; i++){\n\t\tif(path[i][0].row == start_row && path[i][0].col == start_col){\n\t\t\tfirst_state += POW[i];\n\t\t}\n\t}\n\n\tstate_check[start_row][start_col][first_state][0] = true;\n\n\tqueue<Info> Q;\n\tQ.push(Info(first_state,start_row,start_col,0));\n\n\tint ans = -1;\n\tint adj_row,adj_col,next_time,add_state;\n\tbool FLG;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.front().state == POW[N]-1){\n\t\t\tans = Q.front().time;\n\t\t\tbreak;\n\t\t}else{\n\n\t\t\tnext_time = Q.front().time+1;\n\n\t\t\tFLG = true;\n\t\t\tfor(int loop = 0; loop < N; loop++){\n\t\t\t\tif(Q.front().state & (1 << loop)){\n\t\t\t\t\t//Do nothing\n\t\t\t\t}else{\n\t\t\t\t\tif(next_time == num_path[loop]-1){\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif(!FLG){\n\t\t\t\tQ.pop();\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tfor(int i = 0; i < 9; i++){\n\t\t\t\tadj_row = Q.front().row+diff_row[i];\n\t\t\t\tadj_col = Q.front().col+diff_col[i];\n\n\t\t\t\tif(rangeCheck(adj_row,adj_col) == false || bomb_check[adj_row][adj_col] == true)continue;\n\n\t\t\t\tadd_state = 0;\n\n\t\t\t\tfor(int loop = 0; loop < N; loop++){\n\t\t\t\t\tif(Q.front().state & (1 << loop)){\n\t\t\t\t\t\t//Do nothing\n\t\t\t\t\t}else{\n\t\t\t\t\t\tif(path[loop][next_time].row == adj_row && path[loop][next_time].col == adj_col){\n\t\t\t\t\t\t\tadd_state += POW[loop];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(state_check[adj_row][adj_col][Q.front().state+add_state][next_time])continue;\n\n\t\t\t\tstate_check[adj_row][adj_col][Q.front().state+add_state][next_time] = true;\n\n\t\t\t\tQ.push(Info(Q.front().state+add_state,adj_row,adj_col,next_time));\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4076, "score_of_the_acc": -0.1415, "final_rank": 5 }, { "submission_id": "aoj_1569_2579423", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <tuple>\n\nusing pii = std::pair<int, int>;\nusing State = std::tuple<int, int, int, int>;\n\nconstexpr int MAXT = 40;\nconstexpr int INF = (1 << 29);\n\nint main()\n{\n int W, H, N, sx, sy;\n std::cin >> W >> H >> N >> sx >> sy;\n sx--; sy--;\n \n std::vector<std::vector<pii>> paths(N);\n std::vector<std::vector<bool>> pass(H, std::vector<bool>(W, true));\n \n int closed = 0;\n for (int i = 0; i < N; i++) {\n int L;\n std::cin >> L;\n \n std::vector<pii> path(L); \n for (int j = 0; j < L; j++) {\n int x, y;\n std::cin >> x >> y;\n x--; y--;\n path[j] = {x, y};\n \n if (j == 0 && x == sx && y == sy) {\n closed |= 1 << i;\n }\n\n if (j == L - 1) {\n pass[y][x] = false;\n }\n }\n\n paths[i] = path;\n }\n \n \n int d[H][W][1 << N][MAXT];\n std::fill(d[0][0][0], d[0][0][0] + H * W * (1 << N) * MAXT, INF);\n \n d[sy][sx][closed][0] = 0;\n \n std::queue<State> Q;\n Q.push(State(sx, sy, closed, 0));\n\n constexpr int dx[] = {-1, -1, -1, +0, +0, +0, +1, +1, +1};\n constexpr int dy[] = {-1, +0, +1, -1, +0, +1, -1, +0, +1};\n \n int ret = -1;\n while (!Q.empty()) {\n int x, y, S, t;\n std::tie(x, y, S, t) = Q.front(); Q.pop();\n\n auto is_timeup = [&]() -> bool {\n for (int i = 0; i < N; i++) {\n if (!(S >> i & 1) && (int)paths[i].size() <= t) {\n return true;\n } \n }\n return false;\n };\n \n if (is_timeup()) {\n continue;\n }\n \n if (S == (1 << N) - 1) {\n ret = t;\n break;\n }\n\n for (int i = 0; i < 9; i++) {\n int nx = x + dx[i], ny = y + dy[i]; // robot's next position\n if (nx < 0 || nx >= W || ny < 0 || ny >= H || !pass[ny][nx]) {\n continue;\n }\n \n int nS = S;\n for (int j = 0; j < N; j++) {\n if ((int)paths[j].size() <= t + 1) { continue; }\n \n int fx, fy; // fire's next position\n std::tie(fx, fy) = paths[j][t + 1];\n \n if (fx == nx && fy == ny) {\n nS |= 1 << j;\n }\n }\n\n if (t + 1 < d[ny][nx][nS][t + 1]) {\n d[ny][nx][nS][t + 1] = t + 1;\n Q.push(State(nx, ny, nS, t + 1));\n }\n }\n }\n\n std::cout << ret << std::endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5252, "score_of_the_acc": -0.2089, "final_rank": 6 }, { "submission_id": "aoj_1569_2576368", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> P;\nstruct PP {int k,t,x,y;};\nint dx[9]={-1,-1,-1,0,0,0,1,1,1},dy[9]={-1,0,1,-1,0,1,-1,0,1};\nbool check(int n,int m,int x,int y) {return x>=0&&x<n&&y>=0&&y<m;}\nint main() {\n int h,w,n,sx,sy,ans=-1,t=0;\n cin >> h >> w >> n >> sx >> sy;\n sx--,sy--;\n vector<P> v[n];\n queue<PP> que;\n bool d[41][1<<n][h][w];\n memset(d,0,sizeof(d));\n for(int i=0,m; i<n; i++) {\n cin >> m;\n for(int j=0,x,y; j<m; j++) {\n cin >> x >> y;\n x--,y--;\n if(!j && x==sx && y==sy) t|=1<<i;\n v[i].push_back(P(x,y));\n }\n }\n d[0][t][sx][sy]=1;\n que.push((PP){0,t,sx,sy});\n while(!que.empty()) {\n PP p=que.front();que.pop();\n int kk=p.k,tt=p.t,nx=p.x,ny=p.y;\n if(tt==(1<<n)-1) {\n if(ans==-1) ans=kk;\n else ans=min(ans,kk);\n continue;\n }\n for(int i=0; i<9; i++) {\n int x=nx+dx[i],y=ny+dy[i],k=kk+1,t=tt,f=1;\n if(!check(h,w,x,y)) continue;\n for(int j=0; j<n; j++) {\n if(v[j][v[j].size()-1].first==x&&v[j][v[j].size()-1].second==y) f=0;\n if(t&(1<<j)) continue;\n if(v[j].size()-1<=k) f=0;\n else if(v[j][k].first==x&&v[j][k].second==y) t|=1<<j;\n }\n if(f && !d[k][t][x][y]) {\n d[k][t][x][y]=1;\n que.push((PP){k,t,x,y});\n }\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3804, "score_of_the_acc": -0.237, "final_rank": 9 }, { "submission_id": "aoj_1569_1830100", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstring>\n#include <string>\n#include <algorithm>\n#include <iomanip>\nusing namespace std;\n\nstruct Point {\n\tint x, y;\n};\nvector<Point> path[5];\nstruct State {\n\tint x, y, t, bomb;\n};\n\nconst int INF = 1 << 25;\nint wf[300][300];\n\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tint H, W, N, sx, sy;\n\tcin >> H >> W >> N >> sx >> sy;\n\tfor(int i = 0; i < N; i++) {\n\t\tint l;\n\t\tcin >> l;\n\t\tpath[i] = vector<Point>(l);\n\t\tfor(int j = 0; j < l; j++) {\n\t\t\tcin >> path[i][j].x >> path[i][j].y;\n\t\t}\n\t}\n\n\tvector<int> order(N);\n\tfor(int i = 0; i < N; i++) {\n\t\torder[i] = i;\n\t}\n\n\tint ans = INF;\n\tdo {\n\t\tvector<State> states;\n\t\tstates.push_back(State{ sx, sy, 0 });\n\t\tfor(int i = 0; i < N; i++) {\n\t\t\tvector<Point>& p = path[order[i]];\n\t\t\tfor(int t = 0; t < p.size() - 1; t++) {\n\t\t\t\tstates.push_back(State{ p[t].x, p[t].y, t, i + 1 });\n\t\t\t}\n\t\t}\n\n\t\tfor(int i = 0; i < 300; i++) {\n\t\t\tfor(int j = 0; j < 300; j++) {\n\t\t\t\twf[i][j] = (i == j) ? 0 : INF;\n\t\t\t}\n\t\t}\n\n\t\tint sn = states.size();\n\t\tfor(int i = 0; i < sn; i++) {\n\t\t\tfor(int j = 0; j < sn; j++) {\n\t\t\t\tState s1 = states[i], s2 = states[j];\n\t\t\t\tif(s1.bomb + 1 != s2.bomb) continue;\n\t\t\t\tif(s2.t - s1.t < max(abs(s1.x - s2.x), abs(s1.y - s2.y))) continue;\n\t\t\t\twf[i][j] = s2.t - s1.t;\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 0; k < sn; k++) {\n\t\t\tfor(int i = 0; i < sn; i++) {\n\t\t\t\tfor(int j = 0; j < sn; j++) {\n\t\t\t\t\twf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\n\t\tfor(int i = 0; i < sn; i++) {\n\t\t\tif(states[i].bomb == N) {\n\t\t\t\tans = min(ans, wf[0][i]);\n\t\t\t}\n\t\t}\n\t} while(next_permutation(order.begin(), order.end()));\n\n\tcout << ((ans == INF) ? -1 : ans) << endl;\n}", "accuracy": 0.275, "time_ms": 10, "memory_kb": 3548, "score_of_the_acc": -0.1112, "final_rank": 20 }, { "submission_id": "aoj_1569_1527281", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <iostream>\n#include <string>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <functional>\n#include <cassert>\n\ntypedef long long ll;\nusing namespace std;\n\n#define mod 1000000007\n#define INF 1000000000\n#define LLINF 2000000000000000000LL\n\n#define SIZE 100000\n\nint x2[7] ={0,1,2,4,8,16,32};\nint mo[5] = {0,-1,0,1,0};\nint mo8[9] = {0,1,1,-1,1,0,-1,-1,0};\n\nint main(){\n int w,h,n;\n int sx,sy;\n int l,px,py;\n int mm[50][20][20]={0};\n int visit[50][20][20][32]={0};\n int bumb_x[5],bumb_y[5];\n \n scanf(\"%d%d%d%d%d\",&w,&h,&n,&sx,&sy);\n \n sx--;\n sy--;\n \n for(int i=0;i<n;i++){\n scanf(\"%d\",&l);\n \n for(int j=0;j<l;j++){\n scanf(\"%d%d\",&px,&py);\n \n px--;\n py--;\n \n if(j<l-1){\n mm[j][py][px] |= 1 << i;\n }else{\n bumb_x[i] = px;\n bumb_y[i] = py;\n }\n }\n }\n \n queue<pair<int,pair<int,int> > > q1;\n \n q1.push({mm[0][sy][sx],{sx,sy}});\n \n for(int i=0;i<=h+w+3;i++){\n queue<pair<int,pair<int,int> > > q2;\n \n //cerr << i << endl;\n \n while(q1.size()){\n pair<int,pair<int,int> > p = q1.front();\n q1.pop();\n \n int nowv = p.first;\n int nowx = p.second.first;\n int nowy = p.second.second;\n \n //cerr << nowv << \" \" << nowx << \" \" << nowy << endl;\n \n bool flag = false;\n \n for(int j=0;j<n;j++){\n if(bumb_x[j] == nowx && bumb_y[j] == nowy) flag = true;\n }\n \n if(flag) continue;\n \n if(visit[i][nowx][nowy][nowv]) continue;\n visit[i][nowx][nowy][nowv] = true;\n \n if(nowv == (1<<n)-1){\n printf(\"%d\\n\",i);\n return 0;\n }\n \n for(int j=0;j<8;j++){\n int tox = nowx+mo8[j];\n int toy = nowy+mo8[j+1];\n \n if(0<=tox && tox<w && 0<=toy && toy<h)\n q2.push({nowv|mm[i+1][toy][tox],{tox,toy}});\n }\n \n q2.push({nowv|mm[i+1][nowy][nowx],{nowx,nowy}});\n\n }\n \n q1 = q2;\n }\n \n puts(\"-1\");\n \n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 5800, "score_of_the_acc": -0.4626, "final_rank": 11 }, { "submission_id": "aoj_1569_1527279", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <iostream>\n#include <string>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <functional>\n#include <cassert>\n\ntypedef long long ll;\nusing namespace std;\n\n#define mod 1000000007\n#define INF 1000000000\n#define LLINF 2000000000000000000LL\n\n#define SIZE 100000\n\nint x2[7] ={0,1,2,4,8,16,32};\nint mo[5] = {0,-1,0,1,0};\nint mo8[9] = {0,1,1,-1,1,0,-1,-1,0};\n\nint main(){\n int w,h,n;\n int sx,sy;\n int l,px,py;\n int mm[50][20][20]={0};\n int visit[50][20][20][32]={0};\n int bumb_x[5],bumb_y[5];\n \n scanf(\"%d%d%d%d%d\",&w,&h,&n,&sx,&sy);\n \n sx--;\n sy--;\n \n for(int i=0;i<n;i++){\n scanf(\"%d\",&l);\n \n for(int j=0;j<l;j++){\n scanf(\"%d%d\",&px,&py);\n \n px--;\n py--;\n \n if(j<l-1){\n mm[j][py][px] |= 1 << i;\n }else{\n bumb_x[i] = px;\n bumb_y[i] = py;\n }\n }\n }\n \n queue<pair<int,pair<int,int> > > q1;\n \n q1.push({mm[0][sy][sx],{sx,sy}});\n \n for(int i=0;i<=h+w+3;i++){\n queue<pair<int,pair<int,int> > > q2;\n \n //cerr << i << endl;\n \n while(q1.size()){\n pair<int,pair<int,int> > p = q1.front();\n q1.pop();\n \n int nowv = p.first;\n int nowx = p.second.first;\n int nowy = p.second.second;\n \n //cerr << nowv << \" \" << nowx << \" \" << nowy << endl;\n \n bool flag = false;\n \n for(int j=0;j<n;j++){\n if(bumb_x[j] == nowx && bumb_y[j] == nowy) flag = true;\n }\n \n if(flag) continue;\n \n if(visit[i][nowx][nowy][nowv]) continue;\n visit[i][nowx][nowy][nowv] = true;\n \n if(nowv == (1<<n)-1){\n printf(\"%d\\n\",i);\n return 0;\n }\n \n for(int j=0;j<8;j++){\n int tox = nowx+mo8[j];\n int toy = nowy+mo8[j+1];\n \n if(0<=tox && tox<w && 0<=toy && toy<h)\n q2.push({nowv|mm[i+1][toy][tox],{tox,toy}});\n }\n }\n \n q1 = q2;\n }\n \n puts(\"-1\");\n \n return 0;\n}", "accuracy": 0.45, "time_ms": 10, "memory_kb": 4544, "score_of_the_acc": -0.1683, "final_rank": 16 }, { "submission_id": "aoj_1569_1525138", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX_H 20\n#define MAX_W 20\n#define MAX_N 5\n#define MAX_S 40\n#define INF 1e9\n\nstruct P{\n int x,y;\n P(){}\n P(int x,int y) : x(x),y(y) {}\n};\n\nstruct State{\n int x,y,S,t;\n State(int x,int y,int S,int t) : x(x),y(y),S(S),t(t) {}\n};\n\nint H,W,N,L[MAX_N],b;\nbool can[MAX_H][MAX_W];\nbool dp[MAX_H][MAX_W][1<<MAX_N][MAX_S];\nvector<P> path[MAX_N];\nconst int dx[9] = {-1,-1,-1,0,0,0,1,1,1};\nconst int dy[9] = {-1,0,1,-1,0,1,-1,0,1};\n\nbool inField(int x,int y){\n return (0 <= x && x < W && 0 <= y && y < H);\n}\n\nint bfs(int sx,int sy){\n if(b == (1<<N)-1) return 0;\n queue<State> Q; Q.push(State(sx,sy,b,0));\n memset(dp,false,sizeof(dp));\n dp[sy][sx][b][0] = true;\n while(!Q.empty()){\n State s = Q.front(); Q.pop();\n int x = s.x,y = s.y,S = s.S;\n int t = s.t+1;\n bool ok = true;\n for(int i = 0 ; i < N ; i++){\n if(!(S >> i & 1) && L[i] <= s.t){\n ok = false;\n break;\n }\n }\n if(!ok) continue;\n if(S == (1<<N)-1) continue;\n for(int i = 0 ; i < 9 ; i++){\n int nx = x + dx[i];\n int ny = y + dy[i];\n if(!inField(nx,ny)) continue;\n if(!can[ny][nx]) continue;\n int nS = S;\n for(int j = 0 ; j < N ; j++){\n if(L[j] <= t) continue;\n P p = path[j][t];\n if(p.x == nx && p.y == ny){\n nS |= 1<<j;\n }\n }\n if(!dp[ny][nx][nS][t]){\n dp[ny][nx][nS][t] = true;\n Q.push(State(nx,ny,nS,t));\n }\n }\n }\n int res = INF;\n for(int i = 0 ; i < H ; i++){\n for(int j = 0 ; j < W ; j++){\n for(int k = 1 ; k < H+W ; k++){\n if(dp[i][j][(1<<N)-1][k]){\n res = min(res,k);\n }\n }\n }\n }\n return (res == INF ? -1 : res);\n}\n\nint main(){\n int sx,sy,x,y;\n cin >> W >> H >> N >> sx >> sy; sx--; sy--;\n fill(can[0],can[0]+MAX_H*MAX_W,1); b = 0;\n for(int i = 0 ; i < N ; i++){\n cin >> L[i]; path[i].resize(L[i]);\n for(int j = 0 ; j < L[i] ; j++){\n cin >> x >> y; x--; y--;\n path[i][j] = P(x,y);\n if(j == L[i]-1) can[y][x] = 0;\n if(j == 0 && x == sx && y == sy){\n b |= 1<<i;\n }\n }\n }\n cout << bfs(sx,sy) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 1856, "score_of_the_acc": -0.2364, "final_rank": 8 }, { "submission_id": "aoj_1569_1525133", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX_H 20\n#define MAX_W 20\n#define MAX_N 5\n#define MAX_S 40\n#define INF 1e9\n\nstruct P{\n int x,y;\n P(){}\n P(int x,int y) : x(x),y(y) {}\n};\n\nstruct State{\n int x,y,S,t;\n State(int x,int y,int S,int t) : x(x),y(y),S(S),t(t) {}\n};\n\nint H,W,N,L[MAX_N],b;\nbool can[MAX_H][MAX_W];\nbool dp[MAX_H][MAX_W][1<<MAX_N][MAX_S];\nvector<P> path[MAX_N];\nconst int dx[9] = {-1,-1,-1,0,0,0,1,1,1};\nconst int dy[9] = {-1,0,1,-1,0,1,-1,0,1};\n\nbool inField(int x,int y){\n return (0 <= x && x < W && 0 <= y && y < H);\n}\n\nint bfs(int sx,int sy){\n if(b == (1<<N)-1) return 0;\n queue<State> Q; Q.push(State(sx,sy,b,0));\n memset(dp,false,sizeof(dp));\n dp[sy][sx][b][0] = true;\n while(!Q.empty()){\n State s = Q.front(); Q.pop();\n int x = s.x,y = s.y,S = s.S;\n int t = s.t;\n bool ok = true;\n for(int i = 0 ; i < N ; i++){\n if(!(S >> i & 1) && L[i] <= t){\n ok = false;\n break;\n }\n }\n if(!ok) continue;\n if(S == (1<<N)-1) continue;\n for(int i = 0 ; i < 9 ; i++){\n int nx = x + dx[i];\n int ny = y + dy[i];\n if(!inField(nx,ny)) continue;\n if(!can[ny][nx]) continue;\n int nS = S;\n for(int j = 0 ; j < N ; j++){\n if(L[j] <= t+1) continue;\n P p = path[j][t+1];\n if(p.x == nx && p.y == ny){\n nS |= 1<<j;\n }\n }\n if(!dp[ny][nx][nS][s.t+1]){\n dp[ny][nx][nS][s.t+1] = true;\n Q.push(State(nx,ny,nS,s.t+1));\n }\n }\n }\n int res = INF;\n for(int i = 0 ; i < H ; i++){\n for(int j = 0 ; j < W ; j++){\n for(int k = 1 ; k < H+W ; k++){\n if(dp[i][j][(1<<N)-1][k]){\n res = min(res,k);\n }\n }\n }\n }\n return (res == INF ? -1 : res);\n}\n\nint main(){\n int sx,sy,x,y;\n cin >> W >> H >> N >> sx >> sy; sx--; sy--;\n fill(can[0],can[0]+MAX_H*MAX_W,1); b = 0;\n for(int i = 0 ; i < N ; i++){\n cin >> L[i]; path[i].resize(L[i]);\n for(int j = 0 ; j < L[i] ; j++){\n cin >> x >> y; x--; y--;\n path[i][j] = P(x,y);\n if(j == L[i]-1) can[y][x] = 0;\n if(j == 0 && x == sx && y == sy){\n b |= 1<<i;\n }\n }\n }\n cout << bfs(sx,sy) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1860, "score_of_the_acc": -0.1256, "final_rank": 3 }, { "submission_id": "aoj_1569_1525120", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define MAX_H 20\n#define MAX_W 20\n#define MAX_N 5\n#define MAX_S 40\n#define INF 1e9\n\nstruct P{\n int x,y;\n P(){}\n P(int x,int y) : x(x),y(y) {}\n};\n\nstruct State{\n int x,y,S,t;\n State(int x,int y,int S,int t) : x(x),y(y),S(S),t(t) {}\n};\n\nint H,W,N,L[MAX_N],b;\nbool can[MAX_H][MAX_W];\nbool dp[MAX_H][MAX_W][1<<MAX_N][MAX_S];\nvector<P> path[MAX_N];\nconst int dx[9] = {-1,-1,-1,0,0,0,1,1,1};\nconst int dy[9] = {-1,0,1,-1,0,1,-1,0,1};\n\nbool inField(int x,int y){\n return (0 <= x && x < W && 0 <= y && y < H);\n}\n\nint bfs(int sx,int sy){\n queue<State> Q; Q.push(State(sx,sy,b,0));\n memset(dp,false,sizeof(dp));\n dp[sy][sx][b][0] = true;\n while(!Q.empty()){\n State s = Q.front(); Q.pop();\n int x = s.x,y = s.y,S = s.S;\n int t = s.t;\n bool ok = true;\n for(int i = 0 ; i < N ; i++){\n if(!(S >> i & 1) && L[i] <= t){\n ok = false;\n break;\n }\n }\n if(!ok) continue;\n if(S == (1<<N)-1) continue;\n for(int i = 0 ; i < 9 ; i++){\n int nx = x + dx[i];\n int ny = y + dy[i];\n if(!inField(nx,ny)) continue;\n if(!can[ny][nx]) continue;\n int nS = S;\n for(int j = 0 ; j < N ; j++){\n if(L[j] <= t+1) continue;\n P p = path[j][t+1];\n if(p.x == nx && p.y == ny){\n nS |= 1<<j;\n }\n }\n if(!dp[ny][nx][nS][s.t+1]){\n dp[ny][nx][nS][s.t+1] = true;\n Q.push(State(nx,ny,nS,s.t+1));\n }\n }\n }\n int res = INF;\n for(int i = 0 ; i < H ; i++){\n for(int j = 0 ; j < W ; j++){\n for(int k = 0 ; k < H+W ; k++){\n if(dp[i][j][(1<<N)-1][k]){\n res = min(res,k);\n }\n }\n }\n }\n return (res == INF ? -1 : res);\n}\n\nint main(){\n int sx,sy,x,y;\n cin >> W >> H >> N >> sx >> sy; sx--; sy--;\n fill(can[0],can[0]+MAX_H*MAX_W,1); b = 0;\n for(int i = 0 ; i < N ; i++){\n cin >> L[i]; path[i].resize(L[i]);\n for(int j = 0 ; j < L[i] ; j++){\n cin >> x >> y; x--; y--;\n path[i][j] = P(x,y);\n if(j == L[i]-1) can[y][x] = 0;\n if(j == 0 && x == sx && y == sy){\n b |= 1<<i;\n }\n }\n }\n cout << bfs(sx,sy) << endl;\n return 0;\n}", "accuracy": 0.375, "time_ms": 30, "memory_kb": 1856, "score_of_the_acc": -0.2364, "final_rank": 19 }, { "submission_id": "aoj_1569_1521994", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<vector>\n#include<queue>\n#include<algorithm>\n#include<cmath>\n#include<string>\n#include<cstring>\n#include<map>\n#include<functional>\n#include<stack>\n#include<list>\n#include<set>\n#include<deque>\n#include<climits>\nusing namespace std;\n \ntypedef long long ll;\n \nconst int dx[9] = {0, 1, 1, 1, 0, -1, -1, -1, 0};\nconst int dy[9] = {0, -1, 0, 1, 1, 1, 0, -1, -1};\n \nbool dp[40][1 << 5][20][20];\n \nint main(){\n int W, H, N, sx, sy, L[5];\n int px[5][40], py[5][40];\n cin >> W >> H >> N >> sx >> sy;\n sx--;\n sy--;\n for (int i = 0; i < N; i++)\n {\n cin >> L[i];\n for (int j = 0; j < L[i]; j++)\n {\n cin >> px[i][j] >> py[i][j];\n px[i][j]--;\n py[i][j]--;\n }\n }\n \n int ss = 0;\n for (int n = 0; n < N; n++)\n {\n if (px[n][0] == sx && py[n][0] == sy)\n ss = ss | (1 << n);\n }\n if (ss == (1 << N) - 1)\n {\n puts(\"0\");\n return 0;\n }\n dp[0][ss][sy][sx] = true;\n \n for (int i = 0; i <= H + W; i++)\n {\n for (int j = 0; j < (1 << N) - 1; j++)\n {\n bool ng = false;\n for (int k = 0; k < N; k++)\n {\n if (((j >> k)&1) == 1) continue;\n if (L[k] - 2 - i <= 0) ng = true;\n }\n if (ng) continue;\n for (int k = 0; k < H; k++)\n {\n for (int l = 0; l < W; l++)\n {\n if (!dp[i][j][k][l]) continue;\n for (int m = 0; m < 9; m++)\n {\n int nx = l + dx[m];\n int ny = k + dy[m];\n int nj = j;\n if(nx < 0 || W <= nx || ny < 0 || H <= ny) continue;\n bool onBomb = false;\n for (int n = 0; n < N; n++)\n {\n if (nx == px[n][L[n] - 1] && ny == py[n][L[n] - 1]) onBomb = true;\n if (i+1 != L[n] - 1 && px[n][i+1] == nx && py[n][i+1] == ny)\n nj = nj | (1 << n);\n }\n if (onBomb) continue;\n if (nj == (1 << N) - 1)\n {\n printf(\"%d\\n\", i+1);\n return 0;\n }\n dp[i+1][nj][ny][nx] = true;\n }\n }\n }\n }\n }\n \n puts(\"-1\");\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1608, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1569_1521734", "code_snippet": "#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <cstdlib>\n#include <cstring>\n\nusing namespace std;\ntypedef pair<int, int> pii;\nstatic const int dx[] = { 1, 1, 0, -1, -1, -1, 0, 1 };\nstatic const int dy[] = { 0, -1, -1, -1, 0, 1, 1, 1 };\nstatic const int INF = 1000000000;\n\nstatic int mat[400][400];\nstatic bool has_bomb[400];\nstatic bool dp[6][41];\n\ninline bool between(int a, int b, int c){\n\treturn a <= b && b < c;\n}\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tint w, h, n;\n\tcin >> w >> h >> n;\n\tfor(int i = 0; i < h * w; ++i){\n\t\tfor(int j = 0; j < h * w; ++j){ mat[i][j] = INF; }\n\t\tmat[i][i] = 0;\n\t\thas_bomb[i] = false;\n\t}\n\tint sx, sy;\n\tcin >> sx >> sy;\n\t--sx; --sy;\n\tvector<vector<pii>> paths(n);\n\tfor(int i = 0; i < n; ++i){\n\t\tint l;\n\t\tcin >> l;\n\t\tfor(int j = 0; j < l; ++j){\n\t\t\tint x, y;\n\t\t\tcin >> x >> y;\n\t\t\t--x; --y;\n\t\t\tpaths[i].emplace_back(x, y);\n\t\t}\n\t\tconst auto p = paths[i].back();\n\t\thas_bomb[p.second * w + p.first] = true;\n\t}\n\tfor(int i = 0; i < h; ++i){\n\t\tfor(int j = 0; j < w; ++j){\n\t\t\tif(has_bomb[i * w + j]){ continue; }\n\t\t\tfor(int d = 0; d < 8; ++d){\n\t\t\t\tconst int y = i + dy[d], x = j + dx[d];\n\t\t\t\tif(!between(0, y, h) || !between(0, x, w)){ continue; }\n\t\t\t\tif(has_bomb[y * w + x]){ continue; }\n\t\t\t\tmat[i * w + j][y * w + x] = 1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int k = 0; k < w * h; ++k){\n\t\tfor(int i = 0; i < w * h; ++i){\n\t\t\tfor(int j = 0; j < w * h; ++j){\n\t\t\t\tmat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);\n\t\t\t}\n\t\t}\n\t}\n\tsort(paths.begin(), paths.end());\n\tint answer = INF;\n\tdo {\n\t\tmemset(dp, 0, sizeof(dp));\n\t\tfor(int i = 0; i + 1 < paths[0].size(); ++i){\n\t\t\tconst auto &p = paths[0][i];\n\t\t\tconst int d = mat[sy * w + sx][p.second * w + p.first];\n\t\t\tif(d <= i){ dp[1][i] = true; }\n\t\t}\n\t\tfor(int i = 1; i < n; ++i){\n\t\t\tfor(int j = 0; j + 1 < paths[i - 1].size(); ++j){\n\t\t\t\tif(!dp[i][j]){ continue; }\n\t\t\t\tconst auto &s = paths[i - 1][j];\n\t\t\t\tfor(int k = j; k + 1 < paths[i].size(); ++k){\n\t\t\t\t\tconst auto &t = paths[i][k];\n\t\t\t\t\tconst int d =\n\t\t\t\t\t\tmat[s.second * w + s.first][t.second * w + t.first];\n\t\t\t\t\tif(d <= k - j){ dp[i + 1][k] = true; }\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int i = 0; i <= w + h; ++i){\n\t\t\tif(dp[n][i]){ answer = min(answer, i); }\n\t\t}\n\t} while(next_permutation(paths.begin(), paths.end()));\n\tif(answer >= INF){ answer = -1; }\n\tcout << answer << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 1828, "score_of_the_acc": -0.6793, "final_rank": 13 } ]
aoj_1574_cpp
Gossip Problem 1から n までの番号が順番に割り当てられている n 人のアイドルが横一列に並んでいる。 アイドル i は単位時間でアイドル i -1とアイドル i +1に情報を伝達することができる。ただし、アイドル1はアイドル2にしか情報を伝達できず、アイドル n はアイドル n -1にしか情報を伝達できない。 時刻0の時点で、番号が a 1 , a 2 , ..., a m である m 人のアイドルが秘密の情報を持っている。すべてのアイドルが秘密の情報を得ることのできる最小の時間を求めよ。 Input 入力は以下の形式で与えられる。 n m a 1 a 2 ... a m 1行目に2つの整数 n と m が空白区切りで与えられる。 2行目に m 個の整数 a 1 , a 2 , ..., a m が空白区切りで与えられる。 Constraints 2 ≤ n ≤ 10 5 1 ≤ m ≤ n 1 ≤ a i ≤ n a i の値は全て異なる a i は昇順で与えられる Output すべてのアイドルに情報が伝わる最小の時間を1行に出力する。 Sample Input 1 3 2 1 3 Sample Output 1 1 Sample Input 2 10 3 2 5 7 Sample Output 2 3 Sample Input 3 10 5 2 5 6 8 10 Sample Output 3 1 Sample Input 4 100000 1 1 Sample Output 4 99999
[ { "submission_id": "aoj_1574_2747836", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define mp make_pair\n#define pb push_back\n#define all(x) begin(x),end(x)\n#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))\n#define fi first\n#define se second\n#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\nvoid _dbg(string){cerr<<endl;}\ntemplate<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<\" = \"<<h<<\", \";_dbg(s.substr(l+1),t...);}\ntemplate<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nint main(){\n int n,m;\n cin>>n>>m;\n vector<int> a(m);\n rep(i,m) cin>>a[i];\n\n int ans = max(a[0]-1, n - a.back());\n rep(i,m-1){\n ans = max(ans, (a[i+1] - a[i])/2);\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3152, "score_of_the_acc": -0.5492, "final_rank": 14 }, { "submission_id": "aoj_1574_2717577", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n\tint n,m;cin>>n>>m;\n\tint ans=0;\n\tint b;\n\tfor(int i=0;i<m;i++){\n\t\tint a;cin>>a;\n\t\tif(i==0)ans=max(ans,a-1);\n\t\telse ans=max(ans,(a-b)/2);\n\t\tb=a;\n\t}\n\tans=max(ans,n-b);\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3112, "score_of_the_acc": -0.5381, "final_rank": 13 }, { "submission_id": "aoj_1574_2577071", "code_snippet": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nmain(){\n int n,m,sum=0,ans=0,x;\n cin>>n>>m;\n int a[n]={};\n r(i,m){\n cin>>x;\n x--;\n a[x]=1;\n }\n r(i,n){\n if(!a[i])a[i]=1,sum++;\n else{\n ans=max(ans,sum);\n break;\n }\n }\n sum=0;\n for(int i=n-1;i>=0;i--){\n if(!a[i])a[i]=1,sum++;\n else{\n ans=max(ans,sum);\n break;\n }\n }\n int p=0;\n r(i,n){\n if(!a[i])p++;\n else{\n ans=max(ans,(p+1)/2);\n p=0;\n }\n }\n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3488, "score_of_the_acc": -0.642, "final_rank": 16 }, { "submission_id": "aoj_1574_2576371", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n,m;\n cin >> n >> m;\n int d[n],d2[n],ans=0;\n fill(d,d+n,1<<29);\n fill(d2,d2+n,1<<29);\n for(int i=0,x; i<m; i++) {\n cin >> x;\n d[x-1]=d2[x-1]=0;\n }\n for(int i=1; i<n; i++) d[i]=min(d[i],d[i-1]+1);\n for(int i=n-2; i>=0; i--) d2[i]=min(d2[i],d2[i+1]+1);\n for(int i=0; i<n; i++) ans=max(ans,min(d[i],d2[i]));\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3884, "score_of_the_acc": -0.7514, "final_rank": 17 }, { "submission_id": "aoj_1574_2218514", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <string>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cstdio>\n#include <cmath>\n#define rep(i,a,b) for(int (i)=(a);i<(b);i++)\n#define INF 100000000\n#define MAX_N 1000000\nusing namespace std;\n\nint main(){\n int n,m,a=0,b=0;\n cin>>n>>m;\n int ans=0;\n cin>>a;\n int z=a;\n b=a;\n rep(i,1,m){\n cin>>a;\n ans=max(a-b,ans);\n //cout<<ans<<endl;\n b=a;\n }\n //cout<<ans<<endl;\n if(ans/2<n-a&&z-1<n-a){\n cout<<n-a<<endl;\n }else{\n cout<<max(ans/2,z-1)<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3080, "score_of_the_acc": -0.5293, "final_rank": 12 }, { "submission_id": "aoj_1574_1827040", "code_snippet": "#include<iostream>\n#include<iomanip>\n#include<string>\n#include<cstdlib>\n#include<cmath>\n#include<algorithm>\n#include<vector>\n#include<iomanip>\n\n\nusing namespace std;\n\n\nint main() {\n\tint n,m;\n\tcin >> n >> m;\n\n\tint count = 0;\n\tint input;\n\tint ex;\n\tfor(int i=0; i<m; i++) {\n\t\tcin >> input;\n\t\tif(i == 0 && input-1 > count) count = input-1;\n\t\tif(i == m-1 && n-input > count) count = n-input;\n\t\tif(i != 0 && (input-ex)/2 > count) count = (input-ex)/2;\n\t\tex=input;\n\t}\n\n\tcout << count << endl;\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1164, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1574_1801134", "code_snippet": "#include <fstream>\n#include <iostream>\n#include <vector>\n#include <iomanip>\n#include <algorithm>\n\nusing namespace std;\n#define ALL(c) (c).begin(), (c).end()\n#define REP(i,n) for(ll i=0; i < (n); ++i)\nusing ll = long long;\nusing vl = vector<ll>;\nint main(){\n#ifdef _WIN32\n\tifstream cin(\"sample.in\");\n\tofstream cout(\"sample.out\");\n#endif\n\tcout << fixed << setprecision(8);\n\tll n, m; cin >> n >> m;\n\tvl a(m); REP(i, m) cin >> a[i];\n\tREP(i, m) a[i]--;\n\n\tsort(ALL(a));\n\tll mi = 0;\n\tREP(i, n){\n\t\tauto it = lower_bound(ALL(a), i);\n\t\tll mii = 100000;\n\t\tif(it != a.end()) mii = min<ll>(mii, abs(*it - i));\n\t\tif (it != a.begin()) mii = min<ll>(mii, abs(*--it - i));\n\t\tmi = max(mi, mii);\n\t}\n\n\tcout << mi << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3176, "score_of_the_acc": -0.5558, "final_rank": 15 }, { "submission_id": "aoj_1574_1746986", "code_snippet": "/*\n * a.cc: \n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n\nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\n\n/* typedef */\n\n/* global variables */\n\nint as[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n, m;\n cin >> n >> m;\n for (int i = 0; i < m; i++) (cin >> as[i]), as[i]--;\n\n int ans = max(as[0], n - 1 - as[m - 1]);\n\n for (int i = 0; i < m - 1; i++) {\n int c = (as[i + 1] - as[i]) / 2;\n if (ans < c) ans = c;\n }\n\n printf(\"%d\\n\", ans);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1280, "score_of_the_acc": -0.032, "final_rank": 3 }, { "submission_id": "aoj_1574_1701941", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)\n#define pb push_back\n#define mp make_pair\n#define all(in) in.begin(),in.end()\nconst double PI=acos(-1);\nconst double EPS=1e-10;\nconst int inf=1e8;\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int,int> pii;\nint main(){\n\tint n,m;\n\tcin>>n>>m;\n\tvi in(m);\n\trep(i,m)cin>>in[i];\n\tint ma=in[0]-1;\n\trep(i,m-1){\n\t\tint q=in[i+1]-in[i];\n\t\tq/=2;\n\t\tma=max(ma,q);\n\t}\n\tma=max(ma,n-in[m-1]);\n\tcout<<ma<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1308, "score_of_the_acc": -0.0398, "final_rank": 6 }, { "submission_id": "aoj_1574_1696427", "code_snippet": "#include <iostream>\n#include <algorithm>\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nusing namespace std;\nint n,m;\nint a[200005];\nint b[200005];\n\nint main() {\n cin>>n>>m;\n rep(i,m) cin>>a[i];\n rep(i,m) a[i]--;\n rep(i,200005) b[i]=1e8;\n rep(i,m) b[a[i]]=0;\n rep(i,n) if(i) {\n b[i]=min(b[i],b[i-1]+1);\n }\n for(int i=n-1;i>=0;i--) if(i+1!=n) b[i]=min(b[i],b[i+1]+1);\n\n int ans=0;\n rep(i,n) ans=max(ans,b[i]);\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 2084, "score_of_the_acc": -0.2541, "final_rank": 11 }, { "submission_id": "aoj_1574_1695832", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint t[100000];\nint main(){\n\tint n,m;\tcin>>n>>m;\n\tvector<int> a(m);\n\tfor(int i=0;i<n;i++)\tt[i]=1e9;\n\tqueue<int> q;\n\tfor(int i=0;i<m;i++){\n\t\tcin>>a[i];\ta[i]--;\n\t\tt[a[i]]=0;\n\t\tq.push(a[i]);\n\t}\n\twhile(!q.empty()){\n\t\tint cur=q.front();\tq.pop();\n\t\tif(cur+1<n){\n\t\t\tif(t[cur+1]>t[cur]+1){\n\t\t\t\tt[cur+1]=t[cur]+1;\n\t\t\t\tq.push(cur+1);\n\t\t\t}\n\t\t}\n\t\tif(cur-1>=0){\n\t\t\tif(t[cur-1]>t[cur]+1){\n\t\t\t\tt[cur-1]=t[cur]+1;\n\t\t\t\tq.push(cur-1);\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tfor(int i=0;i<n;i++)\tans=max(ans,t[i]);\n\tcout<<ans<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1728, "score_of_the_acc": -0.1558, "final_rank": 9 }, { "submission_id": "aoj_1574_1694073", "code_snippet": "#include <bits/stdc++.h>\ntypedef long long LL;\n#define SORT(c) sort((c).begin(),(c).end())\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\nusing namespace std;\nint main(void)\n{\n int n,m;\n cin >> n >> m;\n vector<int> se;\n se.resize(m);\n REP(i,m) cin >> se[i];\n int answer=0;\n answer=max(answer,se[0]-1);\n REP(i,m-1) answer=max(answer,(se[i+1]-se[i])/2);\n answer=max(answer,n-se[m-1]);\n cout << answer << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1308, "score_of_the_acc": -0.0398, "final_rank": 6 }, { "submission_id": "aoj_1574_1693934", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\n\n\nint main()\n{\n\tint n ;\n\tint m;\n\tint a[100000];\n\tint b;\n\tint ans = 0;\n\tcin >> n >> m;\n\tfor (int i = 0; i < m; i++){\n\t\tcin >> a[i];\n\t}\n\n\tfor (int i = 0; i < m + 1; i++){\n\t\tif (i == 0){\n\t\t\tb = a[i] - 1;\n\t\t} else if (i == m){\n\t\t\tb = n - a[i - 1];\n\t\t}\n\t\telse{\n\t\t\tb = a[i] - a[i-1] - 1;\n\t\t\tif (b % 2 == 0){\n\t\t\t\tb /= 2;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tb++;\n\t\t\t\tb /= 2;\n\t\t\t}\n\t\t}\n\t\tans = max(b, ans);\n\t}\n\n\tcout << ans << endl;\n\t\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1304, "score_of_the_acc": -0.0387, "final_rank": 4 }, { "submission_id": "aoj_1574_1693915", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = 1 << 28;\n\nint N, M, B[100000];\nint dp[100000][4];\n\nint rec(int idx, int dir)\n{\n if(idx < 0 || idx >= N) return(INF);\n if(~dp[idx][dir + 1]) return(dp[idx][dir + 1]);\n if(B[idx]) return(0);\n return(dp[idx][dir + 1] = rec(idx + dir, dir) + 1);\n}\n\nint main()\n{\n fill_n(*dp, 4 * 100000, -1);\n\n cin >> N >> M;\n for(int i = 0; i < M; i++) {\n int A;\n cin >> A;\n B[--A] = true;\n }\n int ret = 0;\n for(int i = 0; i < N; i++) {\n ret = max(ret, min(rec(i, -1), rec(i, +1)));\n }\n cout << ret << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4284, "score_of_the_acc": -0.8619, "final_rank": 18 }, { "submission_id": "aoj_1574_1693888", "code_snippet": "#include<iostream>\nusing namespace std;\n\nint main(){\n\tint sum;\n\twhile(cin >> sum){\n\t\tint number, info[100005], ans;\n\t\tcin >> number;\n\t\tfor(int i = 0; i < number; i++){\n\t\t\tcin >> info[i];\n\t\t}\n\t\n\t\t\tfor(int i = 0; i < number + 1; i++){\n\t\t\t\tif(!i){\n\t\t\t\t\tans = info[i] - 1;\n\t\t\t\t}else if(i == number){\n\t\t\t\t\tans = max(ans, sum - info[i - 1]);\n\t\t\t\t}else{\n\t\t\t\t\tint a = (info[i] - info[i - 1]) / 2;\n\t\t\t\t\tans = max(ans, a);\n\t\t\t\t}\t\n\t\t\t}\t\t\n\t\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1304, "score_of_the_acc": -0.0387, "final_rank": 4 }, { "submission_id": "aoj_1574_1693835", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<n;++i)\n#define FOR(i,a,b) for(int i=a;i<=b;++i)\n#define LL long long\n#define Fi first\n#define Se second\nusing namespace std;\nstatic const LL INF = 1LL<<61LL;\ntypedef pair<int,int> PII;\n\nint N,M;\nint A[100010];\nint temp=1;\nint ans;\n\nint main(){\n cin>>N>>M;\n rep(i,M){\n int a;\n cin>>a;\n A[a]=1;\n }\n FOR(i,1,N){\n //cout<<temp<<endl;\n if(A[i]==1){\n if(temp==1&&A[1]!=1){\n int t=(i-temp);\n temp=i;\n ans=max(ans,t);\n continue;\n }\n int t=(i-temp-1)/2;\n if(i-temp-1<=0)t=0;\n else if((i-temp-1)%2!=0)t++;\n temp=i;\n ans=max(ans,t);\n }\n else if(i==N){\n ans=max(ans,N-temp);\n }\n }\n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1492, "score_of_the_acc": -0.0906, "final_rank": 8 }, { "submission_id": "aoj_1574_1693679", "code_snippet": "/*\n _ooOoo_\n o8888888o\n 88\" . \"88\n (| -_- |)\n O\\ = /O\n ____/`---'\\____\n .' \\\\| |// `.\n / \\\\||| : |||// \\\n / _||||| -:- |||||- \\\n | | \\\\\\ - /// | |\n | \\_| ''\\---/'' | |\n \\ .-\\__ `-` ___/-. /\n ___`. .' /--.--\\ `. . __\n .\"\" '< `.___\\_<|>_/___.' >'\"\".\n | | : `- \\`.;`\\ _ /`;.`/ - ` : | |\n \\ \\ `-. \\_ __\\ /__ _/ .-` / /\n======`-.____`-.___\\_____/___.-`____.-'======\n `=---='\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n pass System Test!\n*/\n#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& v) {\n if (!v.empty()) {\n out << '[';\n std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, \", \"));\n out << \"\\b\\b]\";\n }\n return out;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int N, M;\n cin >> N >> M;\n set<int> a;\n for (int i = 0; i < M; ++i) {\n int b;\n cin >> b;\n b--;\n a.insert(b);\n }\n\n int ma = 0;\n for (int i = 0; i < N; ++i) {\n if (i >= *(a.rbegin())) {\n ma = max(ma, i - *(a.rbegin()));\n continue;\n }\n\n auto it = a.lower_bound(i);\n int mi = N;\n mi = min(mi, *it - i);\n if (it != a.begin()) {\n it--;\n mi = min(mi, i - *it);\n }\n // cerr << mi << endl;\n ma = max(ma, mi);\n }\n cout << ma << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4784, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_1574_1693671", "code_snippet": "#include<iostream>\n#include<algorithm>\n\nusing namespace std;\n\nint main() {\n\n int n, m, i, in, def = 0, ans = 0, before = 0;\n\n cin >> n >> m;\n \n for(i = 0; i < m; i++) {\n cin >> in;\n \n if(i == 0 && in != 1) {\n def = in - 1;\n }\n\n else def = (in-before)/2 +0.5;\n\n ans = max(ans, def);\n\n before = in;\n }\n\n ans = max(ans, n - in);\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1164, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1574_1693624", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef complex<double> P;\ntypedef pair<int,int> pii;\n#define REP(i,n) for(ll i=0;i<n;++i)\n#define REPR(i,n) for(ll i=1;i<n;++i)\n#define FOR(i,a,b) for(ll i=a;i<b;++i)\n\n#define DEBUG(x) cout<<#x<<\": \"<<x<<endl\n#define DEBUG_VEC(v) cout<<#v<<\":\";REP(i,v.size())cout<<\" \"<<v[i];cout<<endl\n#define ALL(a) (a).begin(),(a).end()\n\n#define MOD (ll)(1e9+7)\n#define ADD(a,b) a=((a)+(b))%MOD\n#define FIX(a) ((a)%MOD+MOD)%MOD\n\nint main(){\n int n,m;\n cin>>n>>m;\n vi a(m);\n REP(i,m)cin>>a[i];\n REP(i,m)--a[i];\n vi d(n,1145141919);\n queue<int> Q;\n REP(i,m){\n d[a[i]]=0;\n Q.push(a[i]);\n }\n while(!Q.empty()){\n int p = Q.front(); Q.pop();\n if(p>0){\n if(d[p-1]>d[p]+1){\n d[p-1] = d[p]+1;\n Q.push(p-1);\n }\n }\n if(p<n-1){\n if(d[p+1]>d[p]+1){\n d[p+1] = d[p]+1;\n Q.push(p+1);\n }\n }\n }\n int res = 0;\n REP(i,n)res=max(res,d[i]);\n cout<<res<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 1732, "score_of_the_acc": -1.1569, "final_rank": 20 }, { "submission_id": "aoj_1574_1693620", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\ntypedef long long ll;\ntypedef pair<int,int>pint;\ntypedef vector<int>vint;\ntypedef vector<pint>vpint;\n#define pb push_back\n#define mp make_pair\n#define fi first\n#define se second\n#define all(v) (v).begin(),(v).end()\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\ntemplate<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}\ntemplate<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}\n\nint N,M;\nint dp[111111];\nsigned main(){\n cin>>N>>M;\n fill_n(dp,N,1<<25);\n rep(i,M){\n int a;\n cin>>a;\n dp[--a]=0;\n }\n\n reps(i,1,N)chmin(dp[i],dp[i-1]+1);\n for(int i=N-2;i>=0;i--)chmin(dp[i],dp[i+1]+1);\n\n cout<<*max_element(dp,dp+N)<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1940, "score_of_the_acc": -0.2144, "final_rank": 10 } ]
aoj_1577_cpp
Courage Test P「今日は肝試し企画の番組の収録だ。」 O型智絵里「き、きもだめし……」 四村かな子「智絵里ちゃん大丈夫?」 月野茜「さすがPさん!一行で状況が明晰判明にわかる素晴らしい解説です!さあ!火の玉に向かって走りましょう!」 P「ルールは簡単、みんなで協力してこのエリアのすべての神社にお参りしてくるだけだ。」 茜「オーソドックスですね!早速神社に向かって走りましょう!」 かな子「でもここ、日本有数の神社密度で行かなくちゃいけない神社がたくさんあるんじゃなかったっけ……」 かな子「でも茜ちゃんがいれば心強いよ!」 P「ちなみにスタートは全員違う神社からスタート、合流も禁止だ。それじゃあ智絵里とかな子は初期位置についてくれ」 智絵里「うぅ……」 かな子「お、お菓子を持っていけば大丈夫!」 智絵里「頑張る……」 茜「プロデューサー!私はどうすれば!」 P「お前は賑やかしだ」 茜「えっ」 P「座ってろ」 茜「」 Problem それぞれ1~ n の番号が付けられた n 個の神社と n -1本の道がある。各道は神社 a i と神社 b i を繋ぎ、双方向に移動することができる。それぞれの神社は、任意の神社から1本以上の道を経由して到達することができる。また、任意の2つの神社の間の(迂回しない)経路は一意に定まる。 O型智絵里ちゃんと四村かな子ちゃんがそれぞれ神社 u と神社 v にいる。 今、2人を任意に移動させることを考える。移動とは、ある神社から1本の道を経由して別の神社に進むことである。 その際、以下のルールを満たしたい。 2人合わせて全ての神社を訪問する。 2人の移動数を同じにする。 1つの神社, 道は1人しか通れず、さらに1度しか通ることができない。 神社 u , v は既に訪れているものとし、通ることはできない。 これらのルールを満たすことができるかどうかを判定し、できる場合は"Yes"を、できない場合は"No"を出力せよ。 Input 入力は以下の形式で与えられる。 n u v a 1 b 1 a 2 b 2 ... a n−1 b n−1 1行目に3つの整数 n , u , v が空白区切りで与えられる。 2行目から n 行目までに、2つの整数 a i , b i が空白区切りで与えられる。 Constraints 2 ≤ n ≤ 10 5 1 ≤ a i , b i , u , v ≤ n u ≠ v a i < b i ( a i , b i ) ≠ ( a j , b j ) ( i ≠ j ) Output ルールを満たすことができる場合は"Yes"を、できない場合は"No"を1行に出力する。 Sample Input 1 4 1 4 1 2 2 3 3 4 Sample Output 1 Yes Sample Input 2 8 3 6 1 2 2 4 3 4 4 5 5 6 6 7 7 8 Sample Output 2 No Sample Input 3 4 2 3 1 2 2 3 3 4 Sample Output 3 Yes Sample Input 4 6 1 5 1 2 1 4 2 3 4 5 3 6 Sample Output 4 No
[ { "submission_id": "aoj_1577_10239296", "code_snippet": "// AOJ #1577 Courage Test\n// 2025.2.22\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\t\treturn -n;\n\t}\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;\n}\n\nbool solve() {\n int n = Cin(), U = Cin(), V = Cin();\n\n if(n & 1) return 0;\n\n vector<vector<int>> G(n+1);\n for(int i = 1; i <= n-1; i++){\n int a = Cin(), b = Cin();\n G[a].push_back(b);\n G[b].push_back(a);\n }\n\n int leafCount = 0, deg3Count = 0, maxDeg = 0;\n for (int i = 1; i <= n; i++){\n int d = G[i].size();\n if(d == 1) leafCount++;\n if(d == 3) deg3Count++;\n maxDeg = max(maxDeg, d);\n }\n bool validPattern = false;\n if(leafCount == 2 && maxDeg <= 2) validPattern = true;\n else if(leafCount == 3 && maxDeg == 3 && deg3Count == 1) validPattern = true;\n else if(leafCount == 4 && maxDeg == 3 && deg3Count == 2) validPattern = true;\n\n if(!validPattern) return 0;\n\n vector<int> sub(n+1, 0);\n int candidate_u = -1, candidate_v = -1;\n function<void(int,int)> dfs = [&](int cur, int parent){\n sub[cur] = 1;\n for (int nb : G[cur]){\n if(nb == parent) continue;\n dfs(nb, cur);\n sub[cur] += sub[nb];\n if(sub[nb] == n/2){\n candidate_u = cur;\n candidate_v = nb;\n }\n }\n };\n dfs(1, 0);\n if(candidate_u == -1) return 0;\n\n vector<bool> compMark(n+1, false);\n auto get_component = [&](int start) -> vector<int> {\n vector<int> comp;\n queue<int> que;\n compMark.assign(n+1, false);\n compMark[start] = true;\n que.push(start);\n while(!que.empty()){\n int cur = que.front();\n que.pop();\n comp.push_back(cur);\n for (int nb : G[cur]){\n if((cur == candidate_u && nb == candidate_v) || (cur == candidate_v && nb == candidate_u))\n continue;\n if(!compMark[nb]){\n compMark[nb] = true;\n que.push(nb);\n }\n }\n }\n return comp;\n };\n vector<int> comp1 = get_component(candidate_v);\n vector<int> comp2 = get_component(candidate_u);\n if((int)comp1.size() != n/2 || (int)comp2.size() != n/2) return 0;\n\n bool uInComp1 = false, vInComp1 = false;\n for (int x : comp1){\n if(x == U) uInComp1 = true;\n if(x == V) vInComp1 = true;\n }\n if((uInComp1 && vInComp1) || (!uInComp1 && !vInComp1)) return 0;\n\n auto get_endpoints = [&](const vector<int>& comp) -> vector<int> {\n vector<bool> inComp(n+1, false);\n for (int x : comp) inComp[x] = true;\n vector<int> ends;\n for (int x : comp) {\n int cnt = 0;\n for (int nb : G[x]){\n if(!inComp[nb]) continue;\n cnt++;\n }\n if(cnt == 1) ends.push_back(x);\n }\n return ends;\n };\n vector<int> ends1 = get_endpoints(comp1);\n vector<int> ends2 = get_endpoints(comp2);\n if(ends1.size() != 2 || ends2.size() != 2) return 0;\n\n vector<int> endpoints;\n endpoints.insert(endpoints.end(), ends1.begin(), ends1.end());\n endpoints.insert(endpoints.end(), ends2.begin(), ends2.end());\n\n auto contains = [&](int x) -> bool {\n for (int y : endpoints)\n if(y == x) return true;\n return false;\n };\n if(!contains(U) || !contains(V)) return 0;\n\n int requiredDist = (n - 2) / 2;\n\n auto bfs_distance = [&](int start) -> vector<int> {\n vector<int> dist(n+1, INF);\n queue<int> que;\n dist[start] = 0;\n que.push(start);\n while(!que.empty()){\n int cur = que.front();\n que.pop();\n for (int nb : G[cur]){\n if(dist[nb] > dist[cur] + 1){\n dist[nb] = dist[cur] + 1;\n que.push(nb);\n }\n }\n }\n return dist;\n };\n vector<vector<int>> d(4);\n for (int i = 0; i < 4; i++){\n d[i] = bfs_distance(endpoints[i]);\n }\n\n vector<pair<pair<int,int>, pair<int,int>>> partitions = {\n {{0,1}, {2,3}},\n {{0,2}, {1,3}},\n {{0,3}, {1,2}}\n };\n bool ok = false;\n for(auto &part : partitions){\n auto p1 = part.first;\n auto p2 = part.second;\n if(d[p1.first][ endpoints[p1.second] ] == requiredDist &&\n d[p2.first][ endpoints[p2.second] ] == requiredDist)\n ok = true;\n }\n return ok;\n}\n\nint main(){\n printf(solve()? \"Yes\\n\": \"No\\n\");\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 20396, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_1577_10239294", "code_snippet": "// AOJ #1577 Courage Test\n// 2025.2.22\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\n\nbool solve() {\n int n, U, V;\n cin >> n >> U >> V;\n\n if(n & 1) return 0;\n\n vector<vector<int>> G(n+1);\n for(int i = 1; i <= n-1; i++){\n int a, b;\n cin >> a >> b;\n G[a].push_back(b);\n G[b].push_back(a);\n }\n\n int leafCount = 0, deg3Count = 0, maxDeg = 0;\n for (int i = 1; i <= n; i++){\n int d = G[i].size();\n if(d == 1) leafCount++;\n if(d == 3) deg3Count++;\n maxDeg = max(maxDeg, d);\n }\n bool validPattern = false;\n if(leafCount == 2 && maxDeg <= 2) validPattern = true;\n else if(leafCount == 3 && maxDeg == 3 && deg3Count == 1) validPattern = true;\n else if(leafCount == 4 && maxDeg == 3 && deg3Count == 2) validPattern = true;\n\n if(!validPattern) return 0;\n\n vector<int> sub(n+1, 0);\n int candidate_u = -1, candidate_v = -1;\n function<void(int,int)> dfs = [&](int cur, int parent){\n sub[cur] = 1;\n for (int nb : G[cur]){\n if(nb == parent) continue;\n dfs(nb, cur);\n sub[cur] += sub[nb];\n if(sub[nb] == n/2){\n candidate_u = cur;\n candidate_v = nb;\n }\n }\n };\n dfs(1, 0);\n if(candidate_u == -1) return 0;\n\n vector<bool> compMark(n+1, false);\n auto get_component = [&](int start) -> vector<int> {\n vector<int> comp;\n queue<int> que;\n compMark.assign(n+1, false);\n compMark[start] = true;\n que.push(start);\n while(!que.empty()){\n int cur = que.front();\n que.pop();\n comp.push_back(cur);\n for (int nb : G[cur]){\n if((cur == candidate_u && nb == candidate_v) || (cur == candidate_v && nb == candidate_u))\n continue;\n if(!compMark[nb]){\n compMark[nb] = true;\n que.push(nb);\n }\n }\n }\n return comp;\n };\n vector<int> comp1 = get_component(candidate_v);\n vector<int> comp2 = get_component(candidate_u);\n if((int)comp1.size() != n/2 || (int)comp2.size() != n/2) return 0;\n\n bool uInComp1 = false, vInComp1 = false;\n for (int x : comp1){\n if(x == U) uInComp1 = true;\n if(x == V) vInComp1 = true;\n }\n if((uInComp1 && vInComp1) || (!uInComp1 && !vInComp1)) return 0;\n\n auto get_endpoints = [&](const vector<int>& comp) -> vector<int> {\n vector<bool> inComp(n+1, false);\n for (int x : comp) inComp[x] = true;\n vector<int> ends;\n for (int x : comp) {\n int cnt = 0;\n for (int nb : G[x]){\n if(!inComp[nb]) continue;\n cnt++;\n }\n if(cnt == 1) ends.push_back(x);\n }\n return ends;\n };\n vector<int> ends1 = get_endpoints(comp1);\n vector<int> ends2 = get_endpoints(comp2);\n if(ends1.size() != 2 || ends2.size() != 2) return 0;\n\n vector<int> endpoints;\n endpoints.insert(endpoints.end(), ends1.begin(), ends1.end());\n endpoints.insert(endpoints.end(), ends2.begin(), ends2.end());\n\n auto contains = [&](int x) -> bool {\n for (int y : endpoints)\n if(y == x) return true;\n return false;\n };\n if(!contains(U) || !contains(V)) return 0;\n\n int requiredDist = (n - 2) / 2;\n\n auto bfs_distance = [&](int start) -> vector<int> {\n vector<int> dist(n+1, INF);\n queue<int> que;\n dist[start] = 0;\n que.push(start);\n while(!que.empty()){\n int cur = que.front();\n que.pop();\n for (int nb : G[cur]){\n if(dist[nb] > dist[cur] + 1){\n dist[nb] = dist[cur] + 1;\n que.push(nb);\n }\n }\n }\n return dist;\n };\n vector<vector<int>> d(4);\n for (int i = 0; i < 4; i++){\n d[i] = bfs_distance(endpoints[i]);\n }\n\n vector<pair<pair<int,int>, pair<int,int>>> partitions = {\n {{0,1}, {2,3}},\n {{0,2}, {1,3}},\n {{0,3}, {1,2}}\n };\n bool ok = false;\n for(auto &part : partitions){\n auto p1 = part.first;\n auto p2 = part.second;\n if(d[p1.first][ endpoints[p1.second] ] == requiredDist &&\n d[p2.first][ endpoints[p2.second] ] == requiredDist)\n ok = true;\n }\n return ok;\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << (solve()? \"Yes\": \"No\") << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 20368, "score_of_the_acc": -1.2476, "final_rank": 14 }, { "submission_id": "aoj_1577_5960328", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 998244353 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n x %= mod;\n if(x == 0) return 0;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n BIT(){}\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n void build(int n) {\n N = n;\n node.resize(N+10);\n }\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash{\n\n using ull = unsigned long long;\n const ull mod = (1ULL << 61) - 1;\n const ull MASK30 = (1ULL << 30) - 1;\n const ull MASK31 = (1ULL << 31) - 1;\n\n const ull MASK61 = mod;\n\n ull base;\n int n;\n vector<ull> hash, pow;\n\n RollingHash(const string &s)\n {\n random_device rnd;\n mt19937_64 mt(rnd());\n base = 1001;\n \n n = (int)s.size();\n hash.assign(n+1, 0);\n pow.assign(n+1, 1);\n \n for(int i=0; i<n; i++){\n hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);\n pow[i+1] = calc_mod(mul(pow[i], base));\n }\n }\n\n ull calc_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if(res >= mod) res -= mod;\n return res;\n }\n\n ull mul(ull a, ull b){\n ull au = a >> 31;\n ull ad = a & MASK31;\n ull bu = b >> 31;\n ull bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30;\n ull midd = mid & MASK30;\n return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n\n //[l,r)のハッシュ値\n inline ull get(int l, int r){\n ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));\n return res;\n }\n //string tのハッシュ値\n inline ull get(string t){\n ull res = 0;\n for(int i=0; i<t.size(); i++){\n res = calc_mod(mul(res, base)+t[i]);\n }\n return res;\n }\n //string s中のtの数をカウント\n inline int count(string t) {\n if(t.size() > n) return 0;\n auto hs = get(t);\n int res = 0;\n for(int i=0; i<n-t.size()+1; i++){\n if(get(i, i+t.size()) == hs) res++; \n }\n return res;\n }\n\n /* \n concat \n @verify https://codeforces.com/problemset/problem/514/C\n */\n inline ull concat(ull h1, ull h2, int h2len){\n return calc_mod(h2 + mul(h1, pow[h2len]));\n }\n\n // LCPを求める S[a:] T[b:]\n inline int LCP(int a, int b){\n int len = min((int)hash.size()-a, (int)hash.size()-b);\n \n int lb = -1, ub = len;\n while(ub-lb>1){\n int mid = (lb+ub)/2;\n\n if(get(a, a+mid) == get(b, b+mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n }\n};\nvector<vector<int>> g(101010);\nint sz[101010];\nint x,y;\nbool visited[101010];\nint depth[101010];\nvoid dfs(int v, int p) {\n sz[v] = 1;\n if(p != -1) depth[v] = depth[p] + 1;\n for(auto u: g[v]) {\n if(u == p) continue;\n dfs(u, v);\n sz[v] += sz[u];\n }\n}\nvoid dfs2(int v, int p) {\n visited[v] = true;\n for(auto u: g[v]) {\n if(u == p) continue;\n if(v == x && u == y) continue;\n if(v == y && u == x) continue;\n dfs2(u, v);\n }\n}\nvoid dfs3(int v, int p) {\n visited[v] = true;\n for(auto u: g[v]) {\n if(u == p) continue;\n if(v == x && u == y) continue;\n if(v == y && u == x) continue;\n dfs3(u, v);\n break;\n }\n}\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n \n int n, u, v; cin >> n >> u >> v;\n u--; v--;\n REP(i,n-1) {\n int a, b; cin >> a >> b;\n a--; b--;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n if(n%2 == 1) {\n cout << \"No\" << endl;\n return 0;\n }\n dfs(0, -1);\n REP(i,n) {\n for(auto j: g[i]) {\n if(depth[i] > depth[j]) continue;\n if(sz[j] == n/2) {\n x = i, y = j;\n \n }\n }\n }\n //cout << x << \" \" << y << endl;\n memset(visited, false, sizeof(visited));\n dfs2(0,-1);\n if(visited[u] == visited[v]) cout << \"No\" << endl;\n else{\n memset(visited, false, sizeof(visited));\n dfs3(u, -1);\n dfs3(v, -1);\n REP(i,n) {\n if(!visited[i]) {\n cout << \"No\" << endl;\n return 0; \n }\n }\n cout << \"Yes\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 15740, "score_of_the_acc": -0.5931, "final_rank": 3 }, { "submission_id": "aoj_1577_4036291", "code_snippet": "#include<algorithm>\n#include<bitset>\n#include<cassert>\n#include<cfloat>\n#include<climits>\n#include<cmath>\n#include<deque>\n#include<functional>\n#include<iomanip>\n#include<iostream>\n#include<map>\n#include<queue>\n#include<set>\n#include<stack>\n#include<string>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<vector>\n\nusing namespace std;\n\nusing lint = long long;\nusing P = pair<int, int>;\nusing LLP = pair<long long, long long>;\n\n#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)\n#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)\n#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)\n#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)\n#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)\n#define SORT(x) sort((x).begin(), (x).end())\n#define SORT_INV(x) sort((x).rbegin(), (x).rend())\n#define REVERSE(x) reverse((x).begin(), (x).end())\n#define TWINS(x) cout << ((x) ? \"Yay!\" : \":(\") << '\\n'\n#define endl '\\n'\n\nconstexpr int IINF = (1 << 30) - 1;\nconstexpr long long LLINF = 1LL << 61;\nconstexpr double EPS = 1e-10;\n\nconstexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};\nconstexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};\n\ntemplate<typename T>\nbool chmax(T& a, T b){\n if(a < b){\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<typename T>\nbool chmin(T& a, T b){\n if(b < a){\n a = b;\n return true;\n }\n return false;\n}\n\nbool solve2(int n, vector<int> s, vector< vector<int> > g){\n vector< vector<int> > ls(2);\n int tmp = 0;\n rep(i, n){\n if((int)g[i].size() == 1){\n int now = i;\n int pre = -1;\n while((int)ls[tmp].size() < (n >> 1)){\n ls[tmp].emplace_back(now);\n for(auto to : g[now]){\n if(to != pre){\n pre = now;\n now = to;\n break;\n }\n }\n }\n ++tmp;\n }\n }\n\n rep(i, 2){\n if((ls[i].front() == s[0] || ls[i].back() == s[0]) && (ls[i ^ 1].front() == s[1] || ls[i ^ 1].back() == s[1])){\n return true;\n }\n }\n\n return false;\n}\n\nbool solve3(int n, vector<int> s, vector< vector<int> > g){\n if((int)g[s[0]].size() != 1 && (int)g[s[1]].size() != 1){\n return false;\n }\n\n vector< vector<int> > ls(3);\n int tmp = 0;\n rep(i, n){\n if((int)g[i].size() == 1){\n int now = i;\n int pre = -1;\n bool flag = true;\n while(flag){\n flag = false;\n ls[tmp].emplace_back(now);\n for(auto to : g[now]){\n if(to != pre && (int)g[to].size() < 3){\n pre = now;\n now = to;\n flag = true;\n break;\n }\n }\n }\n ++tmp;\n }\n }\n\n tmp = -1;\n rep(i, 3){\n if((int)ls[i].size() == (n >> 1)){\n rep(j, 2){\n if(ls[i].front() == s[j] || ls[i].back() == s[j]){\n tmp = j;\n break;\n }\n }\n }\n }\n\n if(tmp == -1){\n return false;\n }\n\n return (int)g[s[tmp ^ 1]].size() == 1;\n}\n\nbool solve4(int n, vector<int> s, vector< vector<int> > g){\n if((int)g[s[0]].size() != 1 || (int)g[s[1]].size() != 1){\n return false;\n }\n\n vector< vector<int> > ls(4);\n int tmp = 0;\n rep(i, n){\n if((int)g[i].size() == 1){\n int now = i;\n int pre = -1;\n bool flag = true;\n while(flag){\n flag = false;\n ls[tmp].emplace_back(now);\n if((int)g[now].size() >= 3){\n break;\n }\n for(auto to : g[now]){\n if(to != pre){\n pre = now;\n now = to;\n flag = true;\n break;\n }\n }\n }\n ++tmp;\n }\n }\n\n bool flag = false;\n\n rep(i, 4){\n for(auto to : g[ls[i].back()]){\n rep(j, 4){\n if(to == ls[j].back()){\n flag = true;\n break;\n }\n }\n if(flag){\n break;\n }\n }\n if(flag){\n break;\n }\n }\n\n if(!flag){\n return false;\n }\n\n rep(i, 4){\n rep(j, i){\n if(ls[i].back() == ls[j].back() && (int)ls[i].size() + (int)ls[j].size() - 1 == (n >> 1)){\n rep(k, 4){\n rep(l, k){\n if(k == i || k == j || l == i || l == j){\n continue;\n }\n if((ls[i].front() == s[0] || ls[j].front() == s[0]) && (ls[k].front() == s[1] || ls[l].front() == s[1])){\n return true;\n }\n }\n }\n }\n }\n }\n\n return false;\n}\n\nbool solve(void){\n int n;\n cin >> n;\n\n if(n & 1){\n return false;\n }\n\n vector<int> s(2);\n rep(i, 2){\n cin >> s[i];\n --s[i];\n }\n\n vector< vector<int> > g(n);\n rep(i, n - 1){\n int a, b;\n cin >> a >> b;\n g[--a].emplace_back(--b);\n g[b].emplace_back(a);\n }\n\n int cnt = 0;\n rep(i, n){\n cnt += (int)g[i].size() == 1;\n }\n\n if(cnt == 2){\n return solve2(n, s, g);\n }else if(cnt == 3){\n return solve3(n, s, g);\n }else if(cnt == 4){\n return solve4(n, s, g);\n }\n\n return false;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n if(solve()){\n cout << \"Yes\" << endl;\n }else{\n cout << \"No\" << endl;\n }\n\n cout << flush;\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 14488, "score_of_the_acc": -0.7337, "final_rank": 5 }, { "submission_id": "aoj_1577_4031762", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long int;\nusing P = pair<ll,ll>;\nconstexpr int INF = 1<<30;\n\nint main() {\n int n, s[2];\n cin >> n >> s[0] >> s[1];\n s[0]--; s[1]--;\n vector<vector<int> > g(n), d(2,vector<int>(n, INF));\n for(int i=0;i<n-1;i++){\n int a, b;\n cin >> a >> b;\n a--; b--;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n if(n%2){\n cout << \"No\" << endl;\n return 0;\n }\n for(int i=0;i<2;i++){\n d[i][s[i]] = 0;\n queue<int> que;\n que.push(s[i]);\n while(!que.empty()){\n int u = que.front();\n que.pop();\n for(auto v : g[u]){\n if(d[i][v] > d[i][u]+1){\n d[i][v] = d[i][u]+1;\n que.push(v);\n }\n }\n }\n }\n\n vector<bool> visited(n);\n visited[s[0]] = true;\n visited[s[1]] = true;\n int p[2] = {s[0], s[1]};\n bool ok = true;\n for(int i=0;i<n/2-1;i++){\n for(int j=0;j<2;j++){\n int ma = 0, nxt = -1;\n for(auto v : g[p[j]]){\n if(!visited[v] && d[j^1][v] > ma){\n nxt = v;\n ma = d[j^1][v];\n }\n }\n if(nxt == -1){\n ok = false;\n break;\n }\n visited[nxt] = true;\n p[j] = nxt;\n }\n if(!ok) break;\n }\n cout << (ok ? \"Yes\" : \"No\") << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 8952, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_1577_4031753", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long int;\nusing P = pair<ll,ll>;\nconstexpr int INF = 1<<30;\n\nint main() {\n int n, s[2];\n cin >> n >> s[0] >> s[1];\n s[0]--; s[1]--;\n vector<vector<int> > g(n), d(2,vector<int>(n, INF));\n for(int i=0;i<n-1;i++){\n int a, b;\n cin >> a >> b;\n a--; b--;\n g[a].push_back(b);\n g[b].push_back(a);\n }\n if(n%2){\n cout << \"No\" << endl;\n return 0;\n }\n for(int i=0;i<2;i++){\n d[i][s[i]] = 0;\n queue<int> que;\n que.push(s[i]);\n while(!que.empty()){\n int u = que.front();\n que.pop();\n for(auto v : g[u]){\n if(d[i][v] < d[i][u]+1){\n d[i][v] = d[i][u]+1;\n que.push(v);\n }\n }\n }\n }\n\n vector<bool> visited(n);\n visited[s[0]] = true;\n visited[s[1]] = true;\n int p[2] = {s[0], s[1]};\n bool ok = true;\n for(int i=0;i<n/2-1;i++){\n for(int j=0;j<2;j++){\n int ma = 0, nxt = -1;\n for(auto v : g[p[j]]){\n if(!visited[v] && d[j^1][v] > ma){\n nxt = v;\n ma = d[j^1][v];\n }\n }\n if(nxt == -1){\n ok = false;\n break;\n }\n visited[nxt] = true;\n p[j] = nxt;\n }\n if(!ok) break;\n }\n cout << (ok ? \"Yes\" : \"No\") << endl;\n return 0;\n}", "accuracy": 0.43902439024390244, "time_ms": 50, "memory_kb": 9008, "score_of_the_acc": -1.0049, "final_rank": 20 }, { "submission_id": "aoj_1577_3800448", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1577.cc: Courage Test\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\n\n/* typedef */\n\ntypedef vector<int> vi;\n\n/* global variables */\n\nvi nbrs[MAX_N];\nint lvs[MAX_N], crs[MAX_N], ps[MAX_N];\n\n/* subroutines */\n\ninline int nextp(int u, int up) {\n vi &nbru = nbrs[u];\n int v0 = nbru[0];\n if (v0 != up) return v0;\n return (nbru.size() > 1) ? nbru[1] : -1;\n}\n\ninline int findbr(int u, int &l, int &up) {\n l = 0, up = -1;\n while (nbrs[u].size() <= 2) {\n int v = nextp(u, up);\n up = u, u = v;\n l++;\n }\n return u;\n}\n\ninline bool matchp(int p0, int p1, int p2, int p3, int u, int v) {\n return\n ((p0 == u || p1 == u) && (p2 == v || p3 == v)) ||\n ((p0 == v || p1 == v) && (p2 == u || p3 == u));\n}\n\n/* main */\n\nint main() {\n int n, u, v;\n scanf(\"%d%d%d\", &n, &u, &v);\n u--, v--;\n\n if (n & 1) { puts(\"No\"); return 0; }\n int hn = n / 2;\n \n for (int i = 1; i < n; i++) {\n int a, b;\n scanf(\"%d%d\", &a, &b);\n a--, b--;\n nbrs[a].push_back(b);\n nbrs[b].push_back(a);\n }\n\n int lvn = 0, crn = 0;\n for (int i = 0; i < n; i++) {\n int sz = nbrs[i].size();\n if (sz == 1) lvs[lvn++] = i;\n else if (sz == 3) crs[crn++] = i;\n\n if (sz > 3 || lvn > 4 || crn > 2) { puts(\"No\"); return 0; }\n }\n //printf(\"lvn=%d, crn=%d\\n\", lvn, crn);\n\n if (lvn == 2) {\n ps[0] = lvs[0];\n for (int i = 1, up = -1; i < n; i++) {\n ps[i] = nextp(ps[i - 1], up);\n up = ps[i - 1];\n }\n //for (int i = 0; i < n; i++) printf(\"%d \", ps[i]); putchar('\\n');\n\n if (matchp(ps[0], ps[hn - 1], ps[hn], ps[n - 1], u, v)) {\n puts(\"Yes\");\n return 0;\n }\n }\n else if (lvn == 3) {\n int ls[3], brs[3], bps[3];\n for (int i = 0; i < 3; i++) {\n brs[i] = findbr(lvs[i], ls[i], bps[i]);\n //printf(\"%d: lv=%d, l=%d, br=%d, bp=%d\\n\",\n //i, lvs[i], ls[i], brs[i], bps[i]);\n }\n\n for (int i = 0; i < 3; i++)\n if (ls[i] == hn) {\n\tint i1 = (i + 1) % 3, i2 = (i + 2) % 3;\n\tif (matchp(lvs[i], bps[i], lvs[i1], lvs[i2], u, v)) {\n\t puts(\"Yes\");\n\t return 0;\n\t}\n }\n }\n else { // lvn == 4\n int ls[4], brs[4], bps[4];\n for (int i = 0; i < 4; i++) {\n brs[i] = findbr(lvs[i], ls[i], bps[i]);\n //printf(\"%d: lv=%d, l=%d, br=%d, bp=%d\\n\",\n //i, lvs[i], ls[i], brs[i], bps[i]);\n }\n\n if (brs[0] != brs[1])\n for (int i = 2; i < 4; i++)\n\tif (brs[0] == brs[i]) {\n\t swap(lvs[1], lvs[i]);\n\t swap(ls[1], ls[i]);\n\t swap(brs[1], brs[i]);\n\t swap(bps[1], bps[i]);\n\t break;\n\t}\n if (ls[0] + ls[1] == hn - 1 && ls[2] + ls[3] == hn - 1 &&\n\tmatchp(lvs[0], lvs[1], lvs[2], lvs[3], u, v)) {\n puts(\"Yes\");\n return 0;\n }\n }\n\n puts(\"No\");\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 9068, "score_of_the_acc": -0.2601, "final_rank": 1 }, { "submission_id": "aoj_1577_2844688", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n\nvoid dfs(const vector<vector<int>>&edges, int now, int pre,int rest,vector<int>&ans,bool flag) {\n\n\tans[now]=true;\n\tif(rest==0)return;\n\tfor (auto e : edges[now]) {\n\t\tif(e==pre)continue;\n\t\telse {\n\t\t\tif (flag&&edges[now].size() == 3) {\n\t\t\t\tflag=false;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdfs(edges,e,now,rest-1,ans,flag);\n\t\t\treturn ;\n\t\t}\n\t}\n\treturn;\n}\n\nint main() {\n\tint N,S,T;cin>>N>>S>>T;\n\tS--;T--;\n\n\tvector<vector<int>>edges(N);\n\tfor (int i = 0; i < N-1; ++i) {\n\t\tint a,b;cin>>a>>b;\n\t\ta--;b--;\n\t\tedges[a].push_back(b);\n\t\tedges[b].push_back(a);\n\t}\n\tbool ok=false;\n\n\tif (N == 2) {\n\t\tok=true;\n\t}\n\telse if (N%2==1||edges[S].size() > 2 || edges[T].size() > 2) {\n\t\tok=false;\n\t}\n\telse {\n\t\tif (count_if(edges.begin(), edges.end(), [](const vector<int>&v) {return v.size() >= 4; }) >= 1) {\n\t\t\tok=false;\n\t\t}\n\t\telse if (count_if(edges.begin(), edges.end(), [](const vector<int>&v) {return v.size() >= 3; }) >= 3) {\n\t\t\tok=false;\n\t\t}\n\t\telse {\n\t\t\tfor (int i = 0; i < edges[S].size() * 2; ++i) {\n\t\t\t\tfor (int j = 0; j < edges[T].size() * 2; ++j) {\n\t\t\t\t\tvector<int>vs, vt;\n\t\t\t\t\tvs.resize(N); vt.resize(N);\n\t\t\t\t\tvs[S] = true;\n\t\t\t\t\tvt[T] = true;\n\t\t\t\t\tdfs(edges, edges[S][i / 2], S, N / 2 - 2, vs, i % 2 == 0);\n\t\t\t\t\tdfs(edges, edges[T][j / 2], T, N / 2 - 2, vt, j % 2 == 0);\n\n\t\t\t\t\tbool aok = true;\n\t\t\t\t\tfor (int k = 0; k < N; ++k) {\n\t\t\t\t\t\tif (!vs[k] && !vt[k])aok = false;\n\t\t\t\t\t}\n\t\t\t\t\tif (aok)ok = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t}\n\n\tif(ok)cout<<\"Yes\"<<endl;\n\telse cout<<\"No\"<<endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 9172, "score_of_the_acc": -1.0192, "final_rank": 8 }, { "submission_id": "aoj_1577_2750221", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst int N = 100010;\nvector<int> G[N];\n\nint par[N];\nvoid dfs(int x){\n for(int e:G[x])if(e!=par[x]){\n par[e] = x;\n dfs(e);\n }\n}\n\nbool b[N]={};\nint vis[N]={};\n\nbool solve(){\n int n,u,v;\n scanf(\" %d %d %d\", &n, &u, &v);\n --u;\n --v;\n rep(i,n-1){\n int x,y;\n scanf(\" %d %d\", &x, &y);\n --x;\n --y;\n G[x].pb(y);\n G[y].pb(x);\n }\n\n // if(n%2==1) return false;\n\n par[u] = -1;\n dfs(u);\n\n int tt = v;\n while(tt!=u){\n b[tt] = true;\n tt = par[tt];\n }\n\n int R=1, B=1;\n vis[u] = 1;\n vis[v] = 2;\n\n int now = u;\n while(R<n/2){\n int ch=0;\n for(int e:G[now])if(par[now]!=e && !vis[e]) ++ch;\n\n if(ch==0) break;\n else if(ch==1){\n for(int e:G[now])if(par[now]!=e && !vis[e]){\n ++R;\n vis[e] = 1;\n now = e;\n }\n }\n else if(ch==2){\n for(int e:G[now])if(par[now]!=e && !vis[e]){\n if(!b[e]){\n ++R;\n vis[e] = 1;\n now = e;\n break;\n }\n }\n }\n else return false;\n }\n\n while(B<n/2){\n int ct = 0;\n for(int e:G[v])if(vis[e]==0) ++ct;\n if(ct==0) break;\n if(ct>=2) return false;\n\n for(int e:G[v])if(vis[e]==0){\n ++B;\n vis[e] = 2;\n v = e;\n }\n }\n\n rep(i,n)if(vis[i]==0) return false;\n return R==B;\n}\n\nint main(){\n cout << (solve()?\"Yes\":\"No\") << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 14240, "score_of_the_acc": -0.7121, "final_rank": 4 }, { "submission_id": "aoj_1577_2748221", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\nconst int N = 100005;\nvector<int> g[N];\nint cnt[N];\n// P es[N];\nvector<P> es;\nint col[N];\nint deg[N];\n\nvoid dfs(ll v,ll pre){\n cnt[v]=1;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n dfs(nv,v);\n cnt[v]+=cnt[nv];\n }\n}\n\nvoid ddfs(ll v,ll pre,ll c){\n col[v]=c;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n ddfs(nv,v,c);\n }\n}\n\nbool solve(){\n int n,x,y;\n scanf(\"%d%d%d\", &n, &x, &y);\n x--; y--;\n\n // memset(deg,0,sizeof(deg));\n\n rep(i,n-1){\n int a,b;\n scanf(\"%d%d\", &a, &b);\n a--;b--;\n g[a].push_back(b);\n g[b].push_back(a);\n // es[i] = {a,b};\n es.push_back({a,b});\n deg[a]++;\n deg[b]++;\n }\n\n if(n==2) return true;\n if(n%2==1) return false;\n\n dfs(0,-1);\n memset(col,-1,sizeof(col));\n // rep(i,n-1){\n // ll a=es[i].fi, b=es[i].se;\n for(P p : es){\n ll a = p.first, b = p.second;\n if(cnt[a]<cnt[b]) swap(a,b);\n if(cnt[b]==n-cnt[b]){\n ddfs(a,b,0);\n ddfs(b,a,1);\n deg[a]--;\n deg[b]--;\n break;\n }\n }\n\n if(col[x]==col[y]) return false;\n\n int cnt1[2] = {0,0};\n rep(i,n){\n if(deg[i]>2) return false;\n if(deg[i]==1) cnt1[col[i]]++;\n }\n\n if(cnt1[0]!=2 || cnt1[1]!=2) return false;\n\n return (deg[x]==1 && deg[y]==1);\n}\n\nint main(){\n if(solve()) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 15824, "score_of_the_acc": -1.1005, "final_rank": 9 }, { "submission_id": "aoj_1577_2748220", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\nconst int N = 100005;\nvector<int> g[N];\nint cnt[N];\n// P es[N];\nvector<P> es;\nint col[N];\nint deg[N];\n\nvoid dfs(ll v,ll pre){\n cnt[v]=1;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n dfs(nv,v);\n cnt[v]+=cnt[nv];\n }\n}\n\nvoid ddfs(ll v,ll pre,ll c){\n col[v]=c;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n ddfs(nv,v,c);\n }\n}\n\nbool solve(){\n int n,x,y;\n scanf(\"%d%d%d\", &n, &x, &y);\n x--; y--;\n\n // memset(deg,0,sizeof(deg));\n\n rep(i,n-1){\n int a,b;\n scanf(\"%d%d\", &a, &b);\n a--;b--;\n g[a].push_back(b);\n g[b].push_back(a);\n // es[i] = {a,b};\n es.push_back({a,b});\n deg[a]++;\n deg[b]++;\n }\n\n if(n==2) return true;\n if(n%2==1) return false;\n\n dfs(0,-1);\n memset(col,-1,sizeof(col));\n rep(i,n-1){\n ll a=es[i].fi, b=es[i].se;\n if(cnt[a]<cnt[b]) swap(a,b);\n if(cnt[b]==n-cnt[b]){\n ddfs(a,b,0);\n ddfs(b,a,1);\n deg[a]--;\n deg[b]--;\n break;\n }\n }\n\n if(col[x]==col[y]) return false;\n\n int cnt1[2] = {0,0};\n rep(i,n){\n if(deg[i]>2) return false;\n if(deg[i]==1) cnt1[col[i]]++;\n }\n\n if(cnt1[0]!=2 || cnt1[1]!=2) return false;\n\n return (deg[x]==1 && deg[y]==1);\n}\n\nint main(){\n if(solve()) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 15828, "score_of_the_acc": -1.1008, "final_rank": 10 }, { "submission_id": "aoj_1577_2748218", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\nconst int N = 100005;\nvector<int> g[N];\nint cnt[N];\nP es[N];\nint col[N];\nint deg[N];\n\nvoid dfs(ll v,ll pre){\n cnt[v]=1;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n dfs(nv,v);\n cnt[v]+=cnt[nv];\n }\n}\n\nvoid ddfs(ll v,ll pre,ll c){\n col[v]=c;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n ddfs(nv,v,c);\n }\n}\n\nbool solve(){\n int n,x,y;\n scanf(\"%d%d%d\", &n, &x, &y);\n x--; y--;\n\n // memset(deg,0,sizeof(deg));\n\n rep(i,n-1){\n int a,b;\n scanf(\"%d%d\", &a, &b);\n a--;b--;\n g[a].push_back(b);\n g[b].push_back(a);\n es[i] = {a,b};\n deg[a]++;\n deg[b]++;\n }\n\n if(n==2) return true;\n if(n%2==1) return false;\n\n dfs(0,-1);\n memset(col,-1,sizeof(col));\n rep(i,n-1){\n ll a=es[i].fi, b=es[i].se;\n if(cnt[a]<cnt[b]) swap(a,b);\n if(cnt[b]==n-cnt[b]){\n ddfs(a,b,0);\n ddfs(b,a,1);\n deg[a]--;\n deg[b]--;\n break;\n }\n }\n\n if(col[x]==col[y]) return false;\n\n int cnt1[2] = {0,0};\n rep(i,n){\n if(deg[i]>2) return false;\n if(deg[i]==1) cnt1[col[i]]++;\n }\n\n if(cnt1[0]!=2 || cnt1[1]!=2) return false;\n\n return (deg[x]==1 && deg[y]==1);\n}\n\nint main(){\n if(solve()) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 16024, "score_of_the_acc": -1.118, "final_rank": 11 }, { "submission_id": "aoj_1577_2748216", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\nconst int N = 100005;\nvector<int> g[N];\nint cnt[N];\nP es[N];\nint col[N];\nint deg[N];\n\nvoid dfs(ll v,ll pre){\n cnt[v]=1;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n dfs(nv,v);\n cnt[v]+=cnt[nv];\n }\n}\n\nvoid ddfs(ll v,ll pre,ll c){\n col[v]=c;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n ddfs(nv,v,c);\n }\n}\n\nbool solve(){\n int n,x,y;\n scanf(\"%d%d%d\", &n, &x, &y);\n x--; y--;\n\n memset(deg,0,sizeof(deg));\n\n rep(i,n-1){\n int a,b;\n scanf(\"%d%d\", &a, &b);\n a--;b--;\n g[a].push_back(b);\n g[b].push_back(a);\n es[i] = {a,b};\n deg[a]++;\n deg[b]++;\n }\n\n if(n==2) return true;\n if(n%2==1) return false;\n\n dfs(0,-1);\n memset(col,-1,sizeof(col));\n rep(i,n-1){\n ll a=es[i].fi, b=es[i].se;\n if(cnt[a]<cnt[b]) swap(a,b);\n if(cnt[b]==n-cnt[b]){\n ddfs(a,b,0);\n ddfs(b,a,1);\n deg[a]--;\n deg[b]--;\n break;\n }\n }\n\n if(col[x]==col[y]) return false;\n\n int cnt1[2] = {0,0};\n rep(i,n){\n if(deg[i]>2) return false;\n if(deg[i]==1) cnt1[col[i]]++;\n }\n\n if(cnt1[0]!=2 || cnt1[1]!=2) return false;\n\n return (deg[x]==1 && deg[y]==1);\n}\n\nint main(){\n if(solve()) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 16088, "score_of_the_acc": -1.1236, "final_rank": 12 }, { "submission_id": "aoj_1577_2748056", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef int ll;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\nconst int N = 100005;\nvector<int> g[N];\nint cnt[N];\nP es[N];\nint col[N];\nint deg[N];\n\nvoid dfs(ll v,ll pre){\n cnt[v]=1;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n dfs(nv,v);\n cnt[v]+=cnt[nv];\n }\n}\n\nvoid ddfs(ll v,ll pre,ll c){\n col[v]=c;\n for(ll nv : g[v]){\n if(nv==pre)continue;\n ddfs(nv,v,c);\n }\n}\n\nbool solve(){\n int n,x,y;\n scanf(\"%d%d%d\", &n, &x, &y);\n x--; y--;\n\n memset(deg,0,sizeof(deg));\n\n rep(i,n-1){\n int a,b;\n scanf(\"%d%d\", &a, &b);\n a--;b--;\n g[a].push_back(b);\n g[b].push_back(a);\n es[i] = {a,b};\n deg[a]++;\n deg[b]++;\n }\n\n if(n==2) return true;\n if(n%2==1) return false;\n\n dfs(0,-1);\n memset(col,-1,sizeof(col));\n rep(i,n-1){\n ll a=es[i].fi, b=es[i].se;\n if(cnt[a]<cnt[b]) swap(a,b);\n if(cnt[b]==n-cnt[b]){\n ddfs(a,b,0);\n ddfs(b,a,1);\n deg[a]--;\n deg[b]--;\n break;\n }\n }\n\n if(col[x]==col[y]) return false;\n\n int cnt1[2] = {0,0};\n rep(i,n){\n if(deg[i]>2) return false;\n if(deg[i]==1) cnt1[col[i]]++;\n }\n\n if(cnt1[0]!=2 || cnt1[1]!=2) return false;\n\n return (deg[x]==1 && deg[y]==1);\n}\n\nint main(){\n if(solve()) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 16884, "score_of_the_acc": -1.1931, "final_rank": 13 }, { "submission_id": "aoj_1577_2698895", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define NUM 100000\n\nint N,A,B,num_A,num_B;\nbool check[NUM];\nvector<int> G[NUM];\n\nbool recursive(int side_A,int side_B){\n\n\tif(num_A == N/2 && num_B == N/2)return true;\n\n\tfor(int i = 0; i < G[side_A].size(); i++){\n\n\t\tif(check[G[side_A][i]])continue;\n\t\tcheck[G[side_A][i]] = true;\n\n\t\tnum_A++;\n\t\tfor(int k = 0; k < G[side_B].size(); k++){\n\n\t\t\tif(check[G[side_B][k]])continue;\n\t\t\tcheck[G[side_B][k]] = true;\n\n\t\t\tnum_B++;\n\t\t\tif(recursive(G[side_A][i],G[side_B][k]))return true;\n\n\t\t\tcheck[G[side_B][k]] = false;\n\t\t\tnum_B--;\n\t\t}\n\t\tcheck[G[side_A][i]] = false;\n\t\tnum_A--;\n\n\t}\n return false;\n}\n\n\nint main(){\n\n\tscanf(\"%d %d %d\",&N,&A,&B);\n\tA--;\n\tB--;\n\tnum_A = num_B = 1;\n\n for(int i = 0; i < N; i++)check[i] = false;\n\n check[A] = true;\n check[B] = true;\n\n int from,to;\n\n for(int loop = 0; loop < N-1; loop++){\n \tscanf(\"%d %d\",&from,&to);\n \tfrom--;\n \tto--;\n \tG[from].push_back(to);\n \tG[to].push_back(from);\n }\n\n if(N%2 != 0){\n printf(\"No\\n\");\n return 0;\n }\n\n int num_3 = 0;\n for(int i = 0; i < N; i++){\n \tif(G[i].size() >= 4){\n \t\tprintf(\"No\\n\");\n \t\treturn 0;\n \t}\n \tif(G[i].size() == 3){\n \t\tnum_3++;\n \t\tif(num_3 > 2){\n \t\t\tprintf(\"No\\n\");\n \t\t\treturn 0;\n \t\t}\n \t}\n }\n\n if(recursive(A,B)){\n \tprintf(\"Yes\\n\");\n }else{\n \tprintf(\"No\\n\");\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 12704, "score_of_the_acc": -0.5779, "final_rank": 2 }, { "submission_id": "aoj_1577_2592224", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstdio>\n \ntemplate<class T>\nusing Graph = std::vector<std::vector<T>>;\n \nbool dfs(Graph<int> &G, int step, int u, int v,\n std::vector<bool> &visited)\n{\n int n = G.size();\n int x = (step < n / 2 ? u : v);\n if (step == n - 1) {\n return true;\n }\n \n bool ret = false;\n for (int next : G[x]) {\n if (!visited[next]) {\n visited[next] = true;\n \n if (step < n / 2) {\n ret |= dfs(G, step + 1, next, v, visited);\n } else {\n ret |= dfs(G, step + 1, u, next, visited);\n } \n } \n }\n \n return ret;\n}\n \nbool solve(Graph<int> &G, int u, int v)\n{\n int n = G.size();\n if (n & 1) {\n return false;\n }\n \n std::vector<bool> visited(n, false);\n visited[u] = visited[v] = true;\n return dfs(G, 1, u, v, visited);\n}\n \nint main()\n{\n int n, u, v;\n scanf(\"%d %d %d\", &n, &u, &v);\n u--; v--;\n \n Graph<int> G(n);\n for (int i = 0; i < n - 1; i++) {\n int a, b;\n scanf(\"%d %d\", &a, &b);\n a--; b--;\n \n G[a].emplace_back(b);\n G[b].emplace_back(a);\n }\n \n printf(\"%s\\n\", (solve(G, u, v) ? \"Yes\" : \"No\"));\n \n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 17584, "score_of_the_acc": -1.2543, "final_rank": 15 }, { "submission_id": "aoj_1577_2592223", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstdio>\n \ntemplate<class T>\nusing Graph = std::vector<std::vector<T>>;\n \nbool dfs(Graph<int> &G, int step, int u, int v,\n std::vector<bool> &visited)\n{\n int n = G.size();\n int x = (step < n / 2 ? u : v);\n if (step == n - 1) {\n return true;\n }\n \n bool ret = false;\n for (int next : G[x]) {\n if (!visited[next]) {\n visited[next] = true;\n \n if (step < n / 2) {\n ret |= dfs(G, step + 1, next, v, visited);\n } else {\n ret |= dfs(G, step + 1, u, next, visited);\n } \n } \n }\n \n return ret;\n}\n \nbool solve(Graph<int> &G, int u, int v)\n{\n int n = G.size();\n if (n & 1) {\n return false;\n }\n \n std::vector<bool> visited(n, false);\n visited[u] = visited[v] = true;\n return dfs(G, 1, u, v, visited);\n}\n \nint main()\n{\n int n, u, v;\n scanf(\"%d %d %d\", &n, &u, &v);\n u--; v--;\n \n Graph<int> G(n);\n for (int i = 0; i < n - 1; i++) {\n int a, b;\n scanf(\"%d %d\", &a, &b);\n a--; b--;\n \n G[a].emplace_back(b);\n G[b].emplace_back(a);\n }\n \n printf(\"%s\\n\", (solve(G, u, v) ? \"Yes\" : \"No\"));\n \n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 17640, "score_of_the_acc": -1.2592, "final_rank": 16 }, { "submission_id": "aoj_1577_2592217", "code_snippet": "#include <iostream>\n#include <vector>\n \ntemplate<class T>\nusing Graph = std::vector<std::vector<T>>;\n \nbool dfs(Graph<int> &G, int step, int u, int v,\n std::vector<bool> &visited)\n{\n int n = G.size();\n int x = (step < n / 2 ? u : v);\n if (step == n - 1) {\n return true;\n }\n \n bool ret = false;\n for (int next : G[x]) {\n if (!visited[next]) {\n visited[next] = true;\n \n if (step < n / 2) {\n ret |= dfs(G, step + 1, next, v, visited);\n } else {\n ret |= dfs(G, step + 1, u, next, visited);\n } \n } \n }\n \n return ret;\n}\n \nbool solve(Graph<int> &G, int u, int v)\n{\n int n = G.size();\n if (n & 1) {\n return false;\n }\n \n std::vector<bool> visited(n, false);\n visited[u] = visited[v] = true;\n return dfs(G, 1, u, v, visited);\n}\n \nint main()\n{\n int n, u, v;\n std::cin >> n >> u >> v;\n u--; v--;\n \n Graph<int> G(n);\n for (int i = 0; i < n - 1; i++) {\n int a, b;\n std::cin >> a >> b;\n a--; b--;\n \n G[a].emplace_back(b);\n G[b].emplace_back(a);\n }\n \n std::cout << (solve(G, u, v) ? \"Yes\" : \"No\") << std::endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 17564, "score_of_the_acc": -1.7525, "final_rank": 18 }, { "submission_id": "aoj_1577_2577956", "code_snippet": "#include <iostream>\n#include <vector>\n\ntemplate<class T>\nusing Graph = std::vector<std::vector<int>>;\n\nbool dfs(Graph<int> &G, int step, int u, int v,\n std::vector<bool> &visited)\n{\n int n = G.size();\n int x = (step < n / 2 ? u : v);\n if (step == n - 1) {\n return true;\n }\n \n bool ret = false;\n for (int next : G[x]) {\n if (!visited[next]) {\n visited[next] = true;\n\n if (step < n / 2) {\n ret |= dfs(G, step + 1, next, v, visited);\n } else {\n ret |= dfs(G, step + 1, u, next, visited);\n } \n } \n }\n\n return ret;\n}\n\nbool solve(Graph<int> &G, int u, int v)\n{\n int n = G.size();\n if (n & 1) {\n return false;\n }\n\n std::vector<bool> visited(n, false);\n visited[u] = visited[v] = true;\n return dfs(G, 1, u, v, visited);\n}\n\nint main()\n{\n int n, u, v;\n std::cin >> n >> u >> v;\n u--; v--;\n \n Graph<int> G(n);\n for (int i = 0; i < n - 1; i++) {\n int a, b;\n std::cin >> a >> b;\n a--; b--;\n\n G[a].emplace_back(b);\n G[b].emplace_back(a);\n }\n\n std::cout << (solve(G, u, v) ? \"Yes\" : \"No\") << std::endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 17584, "score_of_the_acc": -1.7543, "final_rank": 19 }, { "submission_id": "aoj_1577_2576377", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> P;\nvector<P> v[100001];\nint n;\nint solve(int i,int p) {\n if(v[i].size()==1&&v[i][0].first==p) {\n v[i][0].second=n-1;\n return 1;\n }\n int sum=0,k=-1;\n for(int j=0; j<v[i].size(); j++) {\n if(v[i][j].first!=p) {\n if(v[i][j].second==0) {\n v[i][j].second=solve(v[i][j].first,i);\n sum+=v[i][j].second;\n } else sum+=v[i][j].second;\n } else k=j;\n }\n if(k!=-1) v[i][k].second=n-1-sum;\n return sum+1;\n}\nint d[111111],d2[111111],f2;\nvoid dfs(int x,int k,int y) {\n d[x]=k;\n for(int i=0; i<v[x].size(); i++) {\n if(d[v[x][i].first]==-1&&v[x][i].first!=y) dfs(v[x][i].first,k+1,y);\n }\n}\nvoid D(int s,int t) {\n memset(d,-1,sizeof(d));\n dfs(s,0,t);\n int M=-1,x,f=0;\n for(int i=1; i<=n; i++) {\n if(d[i]>M) M=d[i],x=i;\n }\n memset(d,-1,sizeof(d));\n dfs(x,0,t);\n for(int i=1; i<=n; i++) f|=d[i]==n/2-1;\n if(!f) f2=0;\n}\nbool check(int s, int t, int n) {return (!d2[s]||d2[s]==n/2-1)&&(!d2[t]||d2[t]==n/2-1);}\n\nint main() {\n int s,t;\n cin >> n >> s >> t;\n for(int i=0,x,y; i<n-1; i++) {\n cin >> x >> y;\n v[x].push_back(P(y,0));\n v[y].push_back(P(x,0));\n }\n for(int i=1; i<=n; i++) {\n if(v[i].size()==1) {\n solve(i,-1);\n break;\n }\n }\n int x=-1,y=-1;\n for(int i=1; i<=n; i++) {\n for(int j=0; j<v[i].size(); j++) {\n if(v[i][j].second==n/2) {x=i,y=v[i][j].first;break;}\n }\n }\n if(x!=-1) {\n memset(d2,-1,sizeof(d2));f2=1;\n D(x,y);\n for(int i=1; i<=n; i++) d2[i]=max(d2[i],d[i]);\n if(d2[s]!=-1&&d2[t]!=-1||d2[s]==-1&&d2[t]==-1) f2=0;\n D(y,x);\n for(int i=1; i<=n; i++) d2[i]=max(d2[i],d[i]);\n }\n if(n%2==0&&f2&&check(s,t,n)) cout << \"Yes\" << endl;\n else cout << \"No\" << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 17260, "score_of_the_acc": -1.726, "final_rank": 17 } ]
aoj_1583_cpp
Equivalent Vertices Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。 園児の一人であるゆう君は,プログラミングと同じくらいお絵描きが大好きだ。 これまでゆう君は丸と六角形と矢印で沢山絵を書いてきた。 ある日ゆう君はこれらの絵がグラフであることを知る。 丸と六角形は頂点と呼ばれ,矢印は辺と呼ばれているらしい。 ゆう君は2つの頂点間を結ぶようにして矢印を描き、更にその上に0か1を書く。 このように,辺に重み(0または1)があり有向な辺からなるグラフは重み付き有向グラフと呼ばれる。 また,頂点と数字 x (0または1)が与えられた時,その頂点から出ている矢印のうち, x と同じ重みを持つ矢印に従って別の頂点に移動することを x に従って遷移する と言う。 今日ゆう君は0と1からなるどのような数列に従って遷移しても最終的に到達する頂点の種類(丸または六角形)が一緒であるような頂点の対が存在することに気がついた。 これについてゆう君は一つの問題を思いついた。 Problem 重み付き有向グラフが与えられる。 このグラフの各頂点は0から順番に n -1まで番号が割り振られている。 また,各頂点はそこから重みが0の辺と1の辺の合計2本の辺が出て行く。 さらに,頂点には丸い頂点か六角形の頂点の2種類が存在し,各頂点はそれらのいずれかである。 以下にSample Input 2で与えられる重み付き有向グラフを示す。 図1. Sample Input 2 以下の形式で m 個の質問が与えられるので、それぞれについて答えを出力せよ。 頂点の番号 q が与えられるので,この頂点と 等価 な頂点の数を出力せよ 2つの頂点 a , b が以下の2つの条件を満たすとき, a と b は等価である。 a と b は同じ種類の頂点である 0と1からなる長さ1以上の任意の数列について,それに従って a と b のそれぞれから遷移を開始したとき最終的に到達する頂点の種類が同じである 例えば,図1の頂点3から数列0,0,1,0に従って遷移を開始すると, 3→2→1→1→1の順番で遷移し最終的に到達する頂点は1である。 Sample Input 2 において,頂点0と頂点2,頂点0と頂点4は等価である。 頂点0と頂点4について考える。 これらは両方とも丸い頂点であるため1つめの条件を満たす。 また,これらの頂点から0または1で遷移した結果到達する頂点は1か5である。 頂点1,5はそれぞれどのような数列に従って遷移してもその頂点に止まり続ける。 また,両方とも六角形の頂点である。 これより,頂点0と頂点4はどのような数列に従って遷移しても最終的に到達する頂点は六角形の頂点であるため2つめの条件も満たす。 条件を2つとも満たすので頂点0と頂点4は等価である。 最終的に到達する頂点は種類が同じであれば良いため,頂点の番号が同じである必要はないことに注意せよ。 また,頂点0と頂点1は等価でない。 頂点0は丸い頂点で頂点1は六角形頂点なので,1つめの条件を満たさない。 Input n m v 0 s 0 t 0 v 1 s 1 t 1 … v n−1 s n−1 t n−1 q 0 q 1 … q m−1 v i , s i , t i はそれぞれ i 番の頂点の種類,0による遷移先の頂点番号,1による遷移先の頂点番号 v i が0のとき, i 番の頂点は丸い頂点であり,1のとき六角形の頂点 q j は j 番目の質問で与えられる頂点の番号 Constraints 入力は全て整数として与えられる 1 ≤ n ≤ 3000 1 ≤ m ≤ n 0 ≤ s i , t i , q j ≤ n -1 0 ≤ v i ≤ 1 与えられるグラフの辺が双方向に移動できるものとしたとき,任意の2点を結ぶような辺の集合が存在する, つまり与えられるグラフは連結である Output 各質問で与えられる頂点の番号 q j に対して,それと等価な頂点の数をそれぞれ一行に出力せよ。 Sample Input 1 2 1 0 0 1 0 1 0 0 Sample Output 1 2 Sample Input 2 6 6 0 1 5 1 1 1 0 1 5 0 2 1 0 5 1 1 5 5 0 1 2 3 4 5 Sample Output 2 3 2 3 1 3 2
[ { "submission_id": "aoj_1583_10240168", "code_snippet": "// AOJ #1583 Equivalent Vertices\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int n, m;\n cin >> n >> m;\n\n vector<int> typ(n), nxt0(n), nxt1(n), grp(n);\n for (int i = 0; i < n; i++) {\n cin >> typ[i] >> nxt0[i] >> nxt1[i];\n grp[i] = typ[i];\n }\n\n vector<int> queries(m);\n for (int i = 0; i < m; i++) cin >> queries[i];\n\n bool f = true;\n while(f){\n f = false;\n vector<tuple<int,int,int>> sig(n);\n for (int i = 0; i < n; i++)\n sig[i] = make_tuple(grp[i], grp[nxt0[i]], grp[nxt1[i]]);\n\n vector<tuple<int,int,int>> uniq = sig;\n sort(uniq.begin(), uniq.end());\n uniq.erase(unique(uniq.begin(), uniq.end()), uniq.end());\n\n vector<int> newGroup(n);\n for (int i = 0; i < n; i++){\n int pos = int(lower_bound(uniq.begin(), uniq.end(), sig[i]) - uniq.begin());\n newGroup[i] = pos;\n }\n\n if(newGroup != grp){\n f = true;\n grp = newGroup;\n }\n }\n\n unordered_map<int, int> freq;\n for (int i = 0; i < n; i++) freq[grp[i]]++;\n\n for (int i = 0; i < m; i++) cout << freq[grp[queries[i]]] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 3644, "score_of_the_acc": -0.2523, "final_rank": 2 }, { "submission_id": "aoj_1583_6371577", "code_snippet": "#include <iostream>\n#include <unordered_map>\n#include <unordered_set>\n#include <set>\n#include <vector>\n#include <numeric>\n#include <algorithm>\n#include <queue>\n#include <string>\n#include <random>\n#include <array>\n#include <climits>\n#include <map>\n#include <cassert>\n#include <stack>\n#include <iomanip>\n#include <cfloat>\n#include <bitset>\n#include <fstream>\n#include <chrono>\n\nint main() {\n\tint n, m; std::cin >> n >> m;\n\tstd::vector<std::tuple<int, int, int>> vertices(n);\n\tfor (auto& [v, s, t] : vertices) {\n\t\tstd::cin >> v >> s >> t;\n\t}\n\tstd::vector<int> query(m);\n\tfor (auto& q : query) {\n\t\tstd::cin >> q;\n\t}\n\tstd::vector<int> group(n), temp(n), rank(2 * n * n), mode(2 * n * n, -1);\n\tfor (auto iter = 0; iter < n; ++iter) {\n\t\tfor (auto i = 0; i < n; ++i) {\n\t\t\tconst auto [v, s, t] = vertices[i];\n\t\t\ttemp[i] = (group[s] * n + group[t] << 1) + v;\n\t\t}\n\t\tauto r = 0;\n\t\tfor (auto i = 0; i < n; ++i) {\n\t\t\tif (mode[temp[i]] != iter) {\n\t\t\t\tmode[temp[i]] = iter;\n\t\t\t\trank[temp[i]] = r++;\n\t\t\t}\n\t\t}\n\t\tfor (auto i = 0; i < n; ++i) {\n\t\t\tgroup[i] = rank[temp[i]];\n\t\t}\n\t}\n\tstd::vector<int> count(n);\n\tfor (const auto g : group) {\n\t\t++count[g];\n\t}\n\tfor (const auto q : query) {\n\t\tstd::cout << count[group[q]] << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 143692, "score_of_the_acc": -0.4151, "final_rank": 5 }, { "submission_id": "aoj_1583_5998871", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1583.cc: Equivalent Vertices\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 3000;\nconst int MAX_GN = MAX_N * (MAX_N + 1) / 2;\nconst int INF = 1 << 30;\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef queue<int> qi;\n\n/* global variables */\n\nint vs[MAX_N], nbrs[MAX_N][2], cs[MAX_N];\nvi gnbrs[MAX_GN];\nint pids[MAX_N][MAX_N], pis[MAX_GN], pjs[MAX_GN];\nbool ds[MAX_GN];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n, m;\n scanf(\"%d%d\", &n, &m);\n\n for (int i = 0; i < n; i++)\n scanf(\"%d%d%d\", vs + i, nbrs[i], nbrs[i] + 1);\n\n memset(pids, -1, sizeof(pids));\n for (int i = 0, p = 0; i < n; i++)\n for (int j = i; j < n; j++, p++)\n pids[i][j] = p, pis[p] = i, pjs[p] = j;\n\n qi q;\n for (int i = 0, p = 0; i < n; i++)\n for (int j = i; j < n; j++, p++) {\n for (int di = 0; di < 2; di++) {\n\tint ri = nbrs[i][di], rj = nbrs[j][di];\n\tif (ri > rj) swap(ri, rj);\n\tgnbrs[pids[ri][rj]].push_back(p);\n }\n if (vs[i] != vs[j]) {\n\tds[p] = true;\n\tq.push(p);\n }\n }\n\n while (! q.empty()) {\n int u = q.front(); q.pop();\n int ui = pis[u], uj = pjs[u];\n vi &gnbru = gnbrs[u];\n\n for (vi::iterator vit = gnbru.begin(); vit != gnbru.end(); vit++) {\n int &v = *vit;\n if (! ds[v]) {\n\tds[v] = true;\n\tq.push(v);\n }\n }\n }\n\n for (int i = 0, p = 0; i < n; i++)\n for (int j = i; j < n; j++, p++)\n if (! ds[p]) cs[i]++, cs[j]++;\n\n while (m--) {\n int q;\n scanf(\"%d\", &q);\n printf(\"%d\\n\", cs[q] - 1);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 341500, "score_of_the_acc": -1.4607, "final_rank": 6 }, { "submission_id": "aoj_1583_3807393", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1583.cc: Equivalent Vertices\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 3000;\nconst int MAX_GN = MAX_N * (MAX_N + 1) / 2;\nconst int INF = 1 << 30;\n\n/* typedef */\n\ntypedef vector<int> vi;\ntypedef queue<int> qi;\n\n/* global variables */\n\nint vs[MAX_N], nbrs[MAX_N][2], cs[MAX_N];\nvi gnbrs[MAX_GN];\nint pids[MAX_N][MAX_N], pis[MAX_GN], pjs[MAX_GN];\nbool ds[MAX_GN];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n, m;\n scanf(\"%d%d\", &n, &m);\n\n for (int i = 0; i < n; i++)\n scanf(\"%d%d%d\", vs + i, nbrs[i], nbrs[i] + 1);\n\n memset(pids, -1, sizeof(pids));\n for (int i = 0, p = 0; i < n; i++)\n for (int j = i; j < n; j++, p++)\n pids[i][j] = p, pis[p] = i, pjs[p] = j;\n\n qi q;\n for (int i = 0, p = 0; i < n; i++)\n for (int j = i; j < n; j++, p++) {\n for (int di = 0; di < 2; di++) {\n\tint ri = nbrs[i][di], rj = nbrs[j][di];\n\tif (ri > rj) swap(ri, rj);\n\tgnbrs[pids[ri][rj]].push_back(p);\n }\n if (vs[i] != vs[j]) {\n\tds[p] = true;\n\tq.push(p);\n }\n }\n\n while (! q.empty()) {\n int u = q.front(); q.pop();\n int ui = pis[u], uj = pjs[u];\n vi &gnbru = gnbrs[u];\n\n for (vi::iterator vit = gnbru.begin(); vit != gnbru.end(); vit++) {\n int &v = *vit;\n if (! ds[v]) {\n\tds[v] = true;\n\tq.push(v);\n }\n }\n }\n\n for (int i = 0, p = 0; i < n; i++)\n for (int j = i; j < n; j++, p++)\n if (! ds[p]) cs[i]++, cs[j]++;\n\n while (m--) {\n int q;\n scanf(\"%d\", &q);\n printf(\"%d\\n\", cs[q] - 1);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1530, "memory_kb": 341132, "score_of_the_acc": -1.7685, "final_rank": 8 }, { "submission_id": "aoj_1583_1845377", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint n, m, v[3000], s[3000], t[3000];\n\nint main(){\n\tcin >> n >> m;\n\trep(i, n) cin >> v[i] >> s[i] >> t[i];\n\trep(it, n){\n\t\tvector<ll> x;\n\t\tvi next;\n\t\trep(i, n) x.pb(v[i] * (ll)1e9 + v[s[i]] * 10000 + v[t[i]]);\n\t\tsort(all(x)); x.erase(unique(all(x)), x.end());\n\t\trep(i, n) next.pb(lower_bound(all(x), v[i] * (ll)1e9 + v[s[i]] * 10000 + v[t[i]]) - x.begin());\n\t\trep(i, n) v[i] = next[i];\n\t}\n\tunordered_map<int, int> cnt;\n\trep(i, n) cnt[v[i]]++;\n\trep(i, m){\n\t\tint q; cin >> q;\n\t\tcout << cnt[v[q]] << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 790, "memory_kb": 3296, "score_of_the_acc": -0.3822, "final_rank": 4 }, { "submission_id": "aoj_1583_1694417", "code_snippet": "// * template\n\n#include <bits/stdc++.h>\n#ifdef LOCAL\n#include \"dump.hpp\"\n#else\n#define dump(...)\n#endif\n\nusing namespace std;\n\ntemplate<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }\ntemplate<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }\n\ntemplate<class T, class U> inline void fill_array(T &e, const U &v) { e = v; }\ntemplate<class T, class U, size_t s> inline void fill_array(T (&a)[s], const U &v) {for(auto&e:a)fill_array(e,v);}\ntemplate<class T, class U, size_t s> inline void fill_array(array<T, s> &a, const U &v) {for(auto&e:a)fill_array(e,v);}\ntemplate<class T, class U> inline void fill_array(vector<T> &a, const U &v) {for(auto&e:a)fill_array(e,v);}\n\n// * solve\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tusing Graph = vector<vector<int>>;\n\n\tint n, q;\n\tcin >> n >> q;\n\n\tset<int> circle, hexagon;\n\tarray<Graph, 2> rG;\n\trG.fill(Graph(n));\n\n\tfor(int v = 0; v < n; ++v) {\n\t\tint kind, zero, one;\n\t\tcin >> kind >> zero >> one;\n\n\t\trG[0][zero].emplace_back(v);\n\t\trG[1][one].emplace_back(v);\n\n\t\tif(kind == 0) {\n\t\t\tcircle.emplace(v);\n\t\t}\n\t\telse {\n\t\t\thexagon.emplace(v);\n\t\t}\n\t}\n\n\tvector<int> idx(n);\n\tvector<set<int>> S;\n\tqueue<int> que;\n\n\tconst auto update = [&idx, &S, &que](const set<int> &V) {\n\t\tfor(const auto &v : V) {\n\t\t\tidx[v] = S.size();\n\t\t}\n\t\tque.emplace(S.size());\n\t\tS.emplace_back(V);\n\t};\n\n\tupdate(circle);\n\tupdate(hexagon);\n\n\twhile(!que.empty()) {\n\t\tconst set<int> V = S[que.front()];\n\t\tque.pop();\n\n\t\tfor(const auto &G : rG) {\n\t\t\tmap<int, set<int>> M;\n\n\t\t\tfor(const auto &v : V) {\n\t\t\t\tfor(const auto &u : G[v]) {\n\t\t\t\t\tM[idx[u]].emplace(u);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor(const auto &e : M) {\n\t\t\t\tconst int i = e.first;\n\t\t\t\tset<int> U = e.second;\n\n\t\t\t\tassert(0 <= i && i < S.size());\n\t\t\t\tif(S[i].size() == U.size()) continue;\n\n\t\t\t\tset<int> W;\n\t\t\t\tfor(const auto &v : S[i]) {\n\t\t\t\t\tif(!U.count(v)) W.emplace(v);\n\t\t\t\t}\n\t\t\t\tupdate(U);\n\t\t\t\tupdate(W);\n\t\t\t}\n\t\t}\n\t}\n\n\twhile(q--) {\n\t\tint v;\n\t\tcin >> v;\n\t\tcout << S[idx[v]].size() << endl;\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 1970, "memory_kb": 214752, "score_of_the_acc": -1.6252, "final_rank": 7 }, { "submission_id": "aoj_1583_1693927", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <bitset>\n#define pb push_back\n#define rep(i,n) for (int i = 0; i < n; ++i)\n#define rrep(i,n) for (int i = 1; i <= n; ++i)\n#define drep(i,n) for (int i = (n)-1; i >= 0; --i)\n#define mins(x,y) x = min(x,y)\n#define maxs(x,y) x = max(x,y)\n#define rng(x) x.begin(),x.end()\n#define sz(x) int((x).size())\n#define fi first\n#define se second\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef vector<int> vi;\ntypedef vector<P> vp;\ntypedef vector<ll> vl;\n\nconst int MX = 3005;\nconst int INF = 1001001001;\n\nint n, q;\nint a[MX], b[MX];\nll c[MX];\n\nint main() {\n scanf(\"%d%d\",&n,&q);\n rep(i,n) {\n scanf(\"%d\",&c[i]);\n scanf(\"%d%d\",&a[i],&b[i]);\n // cout<<c[i]<<\" \"<<a[i]<<\" \"<<b[i]<<endl;\n // --a[i]; --b[i];\n }\n rep(ti,n) {\n vl x, nc;\n rep(i,n) nc.pb(c[i]*MX*MX+c[a[i]]*MX+c[b[i]]);\n x = nc;\n sort(rng(x));\n rep(i,n) c[i] = lower_bound(rng(x),nc[i])-x.begin();\n // rep(i,n) cout<<c[i]<<\" \"; cout<<\"\"<<endl;\n }\n rep(i,q) {\n int v;\n scanf(\"%d\",&v);\n // --v;\n int ans = 0;\n rep(j,n) if (c[v] == c[j]) ans++;\n printf(\"%d\\n\",ans);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 750, "memory_kb": 3352, "score_of_the_acc": -0.3614, "final_rank": 3 }, { "submission_id": "aoj_1583_1693866", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chmax(x,y) x=max(x,y)\nusing namespace std;\ntypedef pair<int,int> P;\nint N,M,v[3000],to[3000][2];\nbool done[3000][3000];\nvector<int> rG[3000][2];\nint main(){\n\tcin>>N>>M;\n\trep(i,N) cin>>v[i]>>to[i][0]>>to[i][1];\n\trep(i,N) rG[to[i][0]][0].pb(i),rG[to[i][1]][1].pb(i);\n\tqueue<P> que;\n\trep(i,N) rep(j,N) if(v[i]!=v[j]) que.push(P(i,j)),done[i][j]=1;\n\twhile(!que.empty()){\n\t\tP p=que.front();que.pop();\n\t\tint u=p.fs,v=p.sc;\n\t\trep(k,2){\n\t\t\tfor(int a:rG[u][k]) for(int b:rG[v][k]){\n\t\t\t\tif(!done[a][b]){\n\t\t\t\t\tque.push(P(a,b));\n\t\t\t\t\tdone[a][b]=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint c[3000]={};\n\trep(i,N){\n\t\trep(j,N) if(!done[i][j]) c[i]++;\n\t}\n\trep(i,M){\n\t\tint a;\n\t\tcin>>a;\n\t\tcout<<c[a]<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 48736, "score_of_the_acc": -0.2443, "final_rank": 1 } ]
aoj_1579_cpp
Array Update 2 Problem 項数 N 、初項 a 、公差 d の等差数列 A がある。 以下の形式で、数列を書き換える M 個の命令文が与えられるので、与えられた順序で M 回 数列 A を書き換えたときの数列 A の K 項目の値を求めなさい。 i 番目の命令文は3つの整数 x i , y i , z i で与えられる。(1 ≤ i ≤ M ) x i が0だった場合、 y i 項目から z i 項目までの区間において、値の順序を反転する。 x i が1だった場合、 y i 項目から z i 項目までの区間において、それぞれの値を1増加させる。 x i が2だった場合、 y i 項目から z i 項目までの区間において、それぞれの値を半分にする(小数点以下は切り捨てる)。 Input N a d M x 1 y 1 z 1 x 2 y 2 z 2 ... x M y M z M K 1行目に、1つの整数 N が与えられる。 2行目に、2つの整数 a と d が空白区切りで与えられる。 3行目に、1つの整数 M が与えられる。 4行目からの M 行のうち i 行目には i 番目の命令文を表す 3 つの整数 x i , y i , z i が空白区切りで与えられる。 最後の行に、1つの整数 K が与えられる。 Constraints 2 ≤ N ≤ 200000 1 ≤ a ≤ 5 1 ≤ d ≤ 5 1 ≤ M ≤ 200000 0 ≤ x i ≤ 2 (1 ≤ i ≤ M ) 1 ≤ y i ≤ N (1 ≤ i ≤ M ) 1 ≤ z i ≤ N (1 ≤ i ≤ M ) y i < z i (1 ≤ i ≤ M ) 1 ≤ K ≤ N Output 入力で与えられた順番で数列 A を M 回更新したときの K 項目を出力せよ。 Sample Input 1 4 2 1 3 0 1 2 1 1 4 2 2 4 3 Sample Output 1 2 { 2 , 3 , 4 , 5 } ↓ 0 1 2 ... 1項目から2項目までの区間の値の順序を反転する { 3 , 2 , 4 , 5 } ↓ 1 1 4 ... 1項目から4項目までの区間の値をそれぞれ1増やす { 4 , 3 , 5 , 6 } ↓ 2 2 4 ... 2項目から4項目までの区間の値をそれぞれ半分にする(小数点以下切り捨てる) { 3 , 1 , 2 , 3 } よって3項目の値は2である。 Sample Input 2 5 1 2 3 1 2 3 2 3 5 0 1 5 1 Sample Output 2 4 { 1 , 3 , 5 , 7 , 9 } ↓ 1 2 3 ... 2項目から3項目までの区間の値をそれぞれ1増やす { 1 , 4 , 6 , 7 , 9 } ↓ 2 3 5 ... 3項目から5項目までの区間の値をそれぞれ半分にする(小数点以下切り捨てる) { 1 , 4 , 3 , 3 , 4 } ↓ 0 1 5 ... 1項目から5項目までの区間の値の順序を反転する { 4 , 3 , 3 , 4 , 1 } よって1項目の値は4である。
[ { "submission_id": "aoj_1579_5995535", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\ntypedef long long ll;\nusing namespace std;\n#define inf 0x3f3f3f3f\nconst int N = 2E5+1000;\nint n;\nint a,d;\nint m;\nint k;\nint x[N],y[N],z[N];\n//一切从简单的地方出发\nint sp[N],p = 0;\n\nsigned main(void){\n cin>>n;\n cin>>a>>d;\n cin>>m;\n for(int i = 0;i<m;i++){\n cin>>x[i]>>y[i]>>z[i];\n if(!x[i]){\n sp[p++] = i;\n }\n }\n cin>>k;\n for(int i = p-1;i>=0;i--){\n int od = sp[i];\n int l = y[od], r = z[od];\n if(k>=l && k<=r){\n k = l+r-k;\n }\n }\n int val = a+(k-1)*d;\n for(int i = 0;i<m;i++){\n if( k>=y[i] && k<=z[i]){\n if(x[i] == 0){\n k = y[i]+z[i]-k;\n }else if(x[i] == 1){\n ++val;\n }else{\n val/=2;\n }\n }\n }\n cout<<val<<endl;\n\n\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 8344, "score_of_the_acc": -0.1702, "final_rank": 11 }, { "submission_id": "aoj_1579_5825735", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 1000000007 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash {\n static const int base1 = 1007, base2 = 2009;\n static const int mod1 = 1000000007, mod2 = 1000000009;\n vector<long long> hash1, hash2, power1, power2;\n int n;\n // construct\n RollingHash(const string &S) {\n n = (int)S.size();\n hash1.assign(n+1, 0);\n hash2.assign(n+1, 0);\n power1.assign(n+1, 1);\n power2.assign(n+1, 1);\n for (int i = 0; i < n; ++i) {\n hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;\n hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;\n power1[i+1] = (power1[i] * base1) % mod1;\n power2[i+1] = (power2[i] * base2) % mod2;\n }\n }\n \n // get hash of S[left:right]\n inline pair<long long, long long> get(int l, int r) const {\n long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;\n if (res1 < 0) res1 += mod1;\n long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;\n if (res2 < 0) res2 += mod2;\n return {res1, res2};\n }\n \n inline pair<long long, long long> c_shift(int l) {\n auto h1 = get(0, l);\n auto h2 = get(l, n);\n return {(h1.first + h2.first * power1[l]) % mod1, (h1.second + h2.second * power2[l]) % mod2};\n }\n \n // get lcp of S[a:] and T[b:]\n inline int getLCP(int a, int b) const {\n int len = min((int)hash1.size()-a, (int)hash1.size()-b);\n int low = 0, high = len;\n while (high - low > 1) {\n int mid = (low + high) >> 1;\n if (get(a, a+mid) != get(b, b+mid)) high = mid;\n else low = mid;\n }\n return low;\n }\n};\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n\n int N; cin >> N;\n int a, d; cin >> a >> d;\n\n int M; cin >> M;\n vector<tuple<int, int, int>> query(M);\n REP(i,M) {\n int x, y, z; cin >> x >> y >> z;\n y--; z--;\n query[i] = {x, y, z}; \n }\n int K; cin >> K;\n\n K--;\n for(int i=M-1; i>=0; i--) {\n auto[x, y, z] = query[i];\n if(x == 0) {\n if(y <= K && K <= z) {\n int len = z-y;\n K = y + (z-K);\n }\n }\n }\n int num = a + K*d;\n for(int i=0; i<M; i++) {\n auto[x, y, z] = query[i];\n if(x == 0) {\n if(y <= K && K <= z) {\n int len = z-y;\n K = y + (z - K);\n }\n }else if(x == 1) {\n if(y <= K && K <= z) num++;\n }else{\n if(y <= K && K <= z) num >>= 1;\n }\n }\n cout << num << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5164, "score_of_the_acc": -0.0245, "final_rank": 1 }, { "submission_id": "aoj_1579_5092952", "code_snippet": "#include \"bits/stdc++.h\"\n\n#define REP(i,num) for(ll i=0;i<(num);++i)\n#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))\n#define LOOP(i) while(i--)\n#define ALL(c) c.begin(),c.end()\n#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;\n#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}\n\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\n\nconstexpr ll atcoder_mod = 1e9+7;\n\ntemplate<typename T=ll>\nT in(){ T x; cin >> x; return (x); }\ntemplate<typename T=ll,typename C=vector<T>>\nC vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }\n\nvoid vout(){ cout << endl; }\ntemplate<typename Head,typename... Tail>\nvoid vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }\nvoid out(){ cout << endl; }\ntemplate<typename Head,typename... Tail>\nvoid out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }\n\ntemplate<typename T>\nbool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }\ntemplate<typename T>\nbool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }\n\nclass Query{\npublic:\n\tll x,y,z;\n};\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\tcout<<fixed<<setprecision(10);\n\n\tauto N=in(),A=in(),D=in(),M=in();\n\tvector<Query> Q(M);\n\tREP(i,M){\n\t\tQ[i].x=in();\n\t\tQ[i].y=in();\n\t\tQ[i].z=in();\n\t}\n\tll K=in()-1;\n\n\tREP(i,M){\n\t\tll index = M-1-i;\n\t\tif(Q[index].x==0 && Q[index].y-1<=K && K<=Q[index].z-1){\n\t\t\tll l=Q[index].y-1,r=Q[index].z-1;\n\t\t\tll x=K-l;\n\t\t\tK = r-x;\n\t\t}\n\t}\n\n\tll V=A+K*D;\n\tREP(i,M){\n\t\tif(Q[i].x==0){\n\t\t\tif(Q[i].y-1<=K && K<=Q[i].z-1){\n\t\t\t\tll l=Q[i].y-1,r=Q[i].z-1;\n\t\t\t\tll x=K-l;\n\t\t\t\tK = r-x;\n\t\t\t}\n\t\t}\n\t\telse if(Q[i].x==1){\n\t\t\tif(Q[i].y-1<=K && K<=Q[i].z-1){\n\t\t\t\tV++;\n\t\t\t}\n\t\t}\n\t\telse if(Q[i].x==2){\n\t\t\tif(Q[i].y-1<=K && K<=Q[i].z-1){\n\t\t\t\tV/=2;\n\t\t\t}\n\t\t}\n\t}\n\tout(V);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 7408, "score_of_the_acc": -0.0913, "final_rank": 6 }, { "submission_id": "aoj_1579_4968149", "code_snippet": "// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1579\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename Impl, typename Data, typename Node, size_t LIM>\nstruct RBST{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n alignas(Node) static inline char pool[sizeof(Node)*LIM];\n static inline Node* ptr = (Node*)pool;\n static inline size_t size;\n\n template<typename... Args>\n inline Node* create(Args&&... args){\n return new (ptr+size++) Node(std::forward<Args>(args)...);\n }\n\n inline size_t count(const Node *t){return Data::count(t);}\n\n inline Node* touch(Node *t){\n return static_cast<Impl*>(this)->touch(t);\n }\n\n inline void toggle(Node *t){\n return static_cast<Impl*>(this)->toggle(t);\n }\n\n inline Node* pushup(Node *t){\n return static_cast<Impl*>(this)->pushup(t);\n }\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=touch(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=touch(a);\n a->r=merge(a->r,b);\n a->r->p=a;\n return pushup(a);\n }\n b=touch(b);\n b->l=merge(a,b->l);\n b->l->p=b;\n return pushup(b);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=touch(a);\n if(k<=count(a->l)){\n if(a->l) a->l->p=nullptr;\n auto s=split(a->l,k);\n a->l=s.second;\n if(a->l) a->l->p=a;\n return make_pair(s.first,pushup(a));\n }\n if(a->r) a->r->p=nullptr;\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n if(a->r) a->r->p=a;\n return make_pair(pushup(a),s.second);\n }\n\n Node* insert(Node *a,size_t k,Node v){\n Node* b=create(v);\n auto s=split(a,k);\n return merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t k){\n assert(k<count(a));\n auto s=split(a,k);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* find_by_order(Node *a,size_t k){\n assert(k<count(a));\n a=touch(a);\n size_t num=count(a->l);\n if(k<num) return find_by_order(a->l,k);\n if(k>num) return find_by_order(a->r,k-(num+1));\n return a;\n }\n\n inline bool is_right(Node* a){\n return a->p and a->p->r==a;\n }\n\n size_t order_of_key(Node* a){\n size_t res=count(a->l);\n while(a){\n if(is_right(a)) res+=count(a->p->l)+1;\n a=a->p;\n }\n return res;\n }\n\n Node* build(size_t l,size_t r,const vector<Node> &vs){\n if(l+1==r) return create(vs[l]);\n size_t m=(l+r)>>1;\n return merge(build(l,m,vs),build(m,r,vs));\n }\n\n Node* build(const vector<Node> &vs){\n return build(0,vs.size(),vs);\n }\n\n template<typename T>\n Node* set_val(Node *a,size_t k,T val){\n assert(k<count(a));\n a=touch(a);\n size_t num=count(a->l);\n if(k<num) a->l=set_val(a->l,k,val);\n if(k>num) a->r=set_val(a->r,k-(num+1),val);\n if(k==num) a->val=val;\n return pushup(a);\n }\n\n Node* get_val(Node *a,size_t k){\n assert(k<count(a));\n a=touch(a);\n size_t num=count(a->l);\n if(k<num) return get_val(a->l,k);\n if(k>num) return get_val(a->r,k-(num+1));\n return a;\n }\n\n template<typename E>\n Node* update(Node *a,size_t l,size_t r,E v){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=touch(t.first);\n static_cast<Impl*>(this)->propagate(u,v);\n return merge(s.first,merge(u,t.second));\n }\n\n decltype(auto) query(Node *&a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=t.first;\n auto res=static_cast<Impl*>(this)->query(u);\n a=merge(s.first,merge(u,t.second));\n return res;\n }\n};\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename E, typename H>\nstruct Dual{\n struct Node{\n Node *l,*r,*p;\n size_t cnt;\n bool rev;\n E val,laz;\n Node(E val):\n cnt(1),rev(0),val(val),laz(val){l=r=p=nullptr;}\n };\n\n H h;\n E ei;\n Dual(H h,E ei):h(h),ei(ei){}\n\n static inline size_t count(const Node *t){\n return t?t->cnt:0;\n }\n\n inline void toggle(Node *t){\n swap(t->l,t->r);\n t->rev^=1;\n }\n\n inline void propagate(Node *t,E x){\n t->val=h(t->val,x);\n t->laz=h(t->laz,x);\n }\n\n inline bool dirty(Node *t){\n return t->rev or t->laz!=ei;\n }\n\n inline Node* eval(Node* t){\n if(t->rev){\n if(t->l) toggle(t->l);\n if(t->r) toggle(t->r);\n t->rev=false;\n }\n if(t->laz!=ei){\n if(t->l) propagate(t->l,t->laz);\n if(t->r) propagate(t->r,t->laz);\n t->laz=ei;\n }\n return t;\n }\n\n inline Node* pushup(Node *t){\n t->cnt=count(t->l)+1+count(t->r);\n return t;\n }\n\n inline E reflect(Node *t){\n assert(t);\n return h(t->val,t->laz);\n }\n};\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#include \"../rbst.cpp\"\n#undef call_from_test\n\n#endif\n//BEGIN CUT HERE\ntemplate<typename Data, size_t LIM>\nstruct Basic : RBST<Basic<Data, LIM>, Data, typename Data::Node, LIM>{\n using super = RBST<Basic, Data, typename Data::Node, LIM>;\n using Node = typename Data::Node;\n\n Data data;\n\n template<class... Args>\n Basic(Args... args):data(forward<Args>(args)...){}\n\n inline Node* touch(Node *t){return data.eval(t);}\n\n using super::toggle;\n inline void toggle(Node *t){return data.toggle(t);}\n template<typename E>\n inline void propagate(Node *t,E x){return data.propagate(t,x);}\n inline Node* pushup(Node *t){return data.pushup(t);}\n\n inline decltype(auto) get_val(Node *a,size_t k){\n return data.reflect(super::get_val(a,k));\n }\n\n using super::query;\n inline decltype(auto) query(Node *a){\n return data.reflect(a);\n }\n};\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n,a,d;\n cin>>n>>a>>d;\n\n using ll = long long;\n struct E{\n ll a,b,c;\n E(){}\n E(ll a,ll b,ll c):a(a),b(b),c(c){}\n bool operator!=(const E &o) const{\n return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);\n }\n };\n\n const ll MAX = 1e9;\n auto h=[](E a,E b){\n E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);\n c.c=c.a/c.b+b.c;\n c.a%=c.b;\n if(c.b>MAX){\n c.a=max(0LL,MAX-(c.b-c.a));\n c.b=MAX;\n }\n return c;\n };\n E ei(0,1,0);\n\n using Data = Dual<E, decltype(h)>;\n using Node = Data::Node;\n constexpr size_t LIM = 1e6;\n Basic<Data, LIM> G(h,ei);\n\n auto r=G.build(vector<Node>(n,ei));\n for(int i=0;i<n;i++){\n int v=a+d*i;\n r=G.update(r,i,i+1,E(0,1,v));\n }\n\n int m;\n cin>>m;\n for(int i=0;i<m;i++){\n int x,y,z;\n cin>>x>>y>>z;\n y--;\n if(x==0) r=G.toggle(r,y,z);\n if(x==1) r=G.update(r,y,z,E(0,1,1));\n if(x==2) r=G.update(r,y,z,E(0,2,0));\n }\n\n int k;\n cin>>k;\n k--;\n cout<<G.get_val(r,k).c<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 37932, "score_of_the_acc": -1.6429, "final_rank": 17 }, { "submission_id": "aoj_1579_4144952", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n#define inRange(x,a,b) (a <= x && x <= b)\n\nint main(){\n int n, a, d, m, k;\n cin >> n >> a >> d;\n cin >> m;\n vector<int> x(m), y(m), z(m);\n for(int i = 0; i < m; i++){\n cin >> x[i] >> y[i] >> z[i];\n y[i]--, z[i]--;\n }\n cin >> k;\n k--;\n for(int i = m-1; i >= 0; i--){\n if(x[i]==0&&inRange(k,y[i],z[i])){\n k = y[i] + z[i] - k;\n }\n }\n int ans = a+k*d;\n for(int i = 0; i < m; i++){\n if(!inRange(k, y[i], z[i])) continue;\n if(x[i] == 0){\n k = y[i] + z[i] - k;\n }else if(x[i] == 1){\n ans++;\n }else if(x[i] == 2){\n ans /= 2;\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4944, "score_of_the_acc": -0.0996, "final_rank": 9 }, { "submission_id": "aoj_1579_3949250", "code_snippet": "#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1579\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\nstruct FastIO{\n FastIO(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n}fastio_beet;\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename Node, size_t LIM>\nstruct BBSTBase{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n static array<Node, LIM> pool;\n size_t ptr;\n BBSTBase():ptr(0){}\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n inline Node* create(){\n return &pool[ptr++];\n }\n\n template<typename T>\n inline Node* create(T v){\n return &(pool[ptr++]=Node(v));\n }\n\n virtual void toggle(Node *a)=0;\n virtual Node* eval(Node* a)=0;\n virtual Node* recalc(Node* a)=0;\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n a->r->p=a;\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n b->l->p=b;\n return recalc(b);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n if(a->l) a->l->p=nullptr;\n auto s=split(a->l,k);\n a->l=s.second;\n if(a->l) a->l->p=a;\n return make_pair(s.first,recalc(a));\n }\n if(a->r) a->r->p=nullptr;\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n if(a->r) a->r->p=a;\n return make_pair(recalc(a),s.second);\n }\n\n template<typename T>\n Node* insert(Node *a,size_t pos,T v){\n Node* b=create(v);\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* find_by_order(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return find_by_order(a->l,k);\n if(k>num) return find_by_order(a->r,k-(num+1));\n return a;\n }\n\n inline bool is_right(Node* a){\n return a->p&&a->p->r==a;\n }\n\n size_t order_of_key(Node* a){\n size_t res=count(a->l);\n while(a){\n if(is_right(a)) res+=count(a->p->l)+1;\n a=a->p;\n }\n return res;\n }\n\n template<typename T>\n Node* build(size_t l,size_t r,const vector<T> &vs){\n if(l+1==r) return create(vs[l]);\n size_t m=(l+r)>>1;\n return merge(build(l,m,vs),build(m,r,vs));\n }\n\n template<typename T>\n Node* build(const vector<T> &vs){\n return build(0,vs.size(),vs);\n }\n};\ntemplate<typename Node, size_t LIM>\narray<Node, LIM> BBSTBase<Node, LIM>::pool;\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\ntemplate<typename Node, size_t LIM>\nstruct BBSTBase{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n static array<Node, LIM> pool;\n size_t ptr;\n BBSTBase():ptr(0){}\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n inline Node* create(){\n return &pool[ptr++];\n }\n\n template<typename T>\n inline Node* create(T v){\n return &(pool[ptr++]=Node(v));\n }\n\n virtual void toggle(Node *a)=0;\n virtual Node* eval(Node* a)=0;\n virtual Node* recalc(Node* a)=0;\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n a->r->p=a;\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n b->l->p=b;\n return recalc(b);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n if(a->l) a->l->p=nullptr;\n auto s=split(a->l,k);\n a->l=s.second;\n if(a->l) a->l->p=a;\n return make_pair(s.first,recalc(a));\n }\n if(a->r) a->r->p=nullptr;\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n if(a->r) a->r->p=a;\n return make_pair(recalc(a),s.second);\n }\n\n template<typename T>\n Node* insert(Node *a,size_t pos,T v){\n Node* b=create(v);\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* find_by_order(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return find_by_order(a->l,k);\n if(k>num) return find_by_order(a->r,k-(num+1));\n return a;\n }\n\n template<typename T>\n Node* build(size_t l,size_t r,const vector<T> &vs){\n if(l+1==r) return create(vs[l]);\n size_t m=(l+r)>>1;\n return merge(build(l,m,vs),build(m,r,vs));\n }\n\n template<typename T>\n Node* build(const vector<T> &vs){\n return build(0,vs.size(),vs);\n }\n};\ntemplate<typename Node, size_t LIM>\narray<Node, LIM> BBSTBase<Node, LIM>::pool;\n#endif\n//INSERT ABOVE HERE\n//BEGIN CUT HERE\n\ntemplate<typename Ep>\nstruct NodeBase{\n using E = Ep;\n NodeBase *l,*r,*p;\n size_t cnt;\n bool rev;\n E dat,laz;\n NodeBase(){}\n NodeBase(E dat,E laz):\n cnt(1),rev(0),dat(dat),laz(laz){l=r=p=nullptr;}\n};\n\ntemplate<typename Node, size_t LIM>\nstruct Dual : BBSTBase<Node, LIM>{\n using E = typename Node::E;\n using super = BBSTBase<Node, LIM>;\n using H = function<E(E, E)>;\n\n H h;\n E ei;\n\n Dual(H h,E ei):super(),h(h),ei(ei){}\n\n using super::create;\n using super::merge;\n using super::split;\n\n Node* build(size_t l,size_t r){\n if(l+1==r) return create(Node(ei,ei));\n size_t m=(l+r)>>1;\n return merge(build(l,m),build(m,r));\n }\n\n Node* init(int n){\n return build(0,n);\n }\n\n using super::count;\n Node* recalc(Node *a){\n a->cnt=count(a->l)+1+count(a->r);\n return a;\n }\n\n void propagate(Node *a,E x){\n a->dat=h(a->dat,x);\n a->laz=h(a->laz,x);\n }\n\n using super::toggle;\n void toggle(Node *a){\n swap(a->l,a->r);\n a->rev^=1;\n }\n\n // remove \"virtual\" for optimization\n virtual Node* eval(Node* a){\n if(a->laz!=ei){\n if(a->l) propagate(a->l,a->laz);\n if(a->r) propagate(a->r,a->laz);\n a->laz=ei;\n }\n if(a->rev){\n if(a->l) toggle(a->l);\n if(a->r) toggle(a->r);\n a->rev=false;\n }\n return recalc(a);\n }\n\n Node* update(Node *a,size_t l,size_t r,E x){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n propagate(u,x);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* set_val(Node *a,size_t k,E x){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) a->l=set_val(a->l,k,x);\n if(k>num) a->r=set_val(a->r,k-(num+1),x);\n if(k==num) a->dat=x;\n return recalc(a);\n }\n\n E get_val(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return get_val(a->l,k);\n if(k>num) return get_val(a->r,k-(num+1));\n return a->dat;\n }\n\n void dump(Node* a,typename vector<E>::iterator it){\n if(!count(a)) return;\n a=eval(a);\n dump(a->l,it);\n *(it+count(a->l))=a->dat;\n dump(a->r,it+count(a->l)+1);\n }\n\n vector<E> dump(Node* a){\n vector<E> vs(count(a));\n dump(a,vs.begin());\n return vs;\n }\n};\n//END CUT HERE\n//INSERT ABOVE HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nsigned main(){\n int n,a,d;\n cin>>n>>a>>d;\n\n using ll = long long;\n struct E{\n ll a,b,c;\n E(){}\n E(ll a,ll b,ll c):a(a),b(b),c(c){}\n bool operator!=(const E &o) const{\n return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);\n }\n };\n\n const ll MAX = 1e9;\n auto h=\n [](E a,E b){\n E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);\n c.c=c.a/c.b+b.c;\n c.a%=c.b;\n if(c.b>MAX){\n c.a=max(0LL,MAX-(c.b-c.a));\n c.b=MAX;\n }\n return c;\n };\n E ei(0,1,0);\n\n using Node = NodeBase<E>;\n constexpr size_t LIM = 2e5+100;\n Dual<Node, LIM> G(h,ei);\n\n auto r=G.init(n);\n for(int i=0;i<n;i++){\n int v=a+d*i;\n r=G.update(r,i,i+1,E(0,1,v));\n }\n\n int m;\n cin>>m;\n for(int i=0;i<m;i++){\n int x,y,z;\n cin>>x>>y>>z;\n y--;\n if(x==0) r=G.toggle(r,y,z);\n if(x==1) r=G.update(r,y,z,E(0,1,1));\n if(x==2) r=G.update(r,y,z,E(0,2,0));\n }\n\n int k;\n cin>>k;\n k--;\n cout<<G.get_val(r,k).c<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1000, "memory_kb": 19692, "score_of_the_acc": -1.457, "final_rank": 15 }, { "submission_id": "aoj_1579_3949248", "code_snippet": "#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1579\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\nstruct FastIO{\n FastIO(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n}fastio_beet;\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename Node, size_t LIM>\nstruct BBSTBase{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n static array<Node, LIM> pool;\n size_t ptr;\n BBSTBase():ptr(0){}\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n inline Node* create(){\n return &pool[ptr++];\n }\n\n template<typename T>\n inline Node* create(T v){\n return &(pool[ptr++]=Node(v));\n }\n\n virtual void toggle(Node *a)=0;\n virtual Node* eval(Node* a)=0;\n virtual Node* recalc(Node* a)=0;\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n a->r->p=a;\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n b->l->p=b;\n return recalc(b);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n if(a->l) a->l->p=nullptr;\n auto s=split(a->l,k);\n a->l=s.second;\n if(a->l) a->l->p=a;\n return make_pair(s.first,recalc(a));\n }\n if(a->r) a->r->p=nullptr;\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n if(a->r) a->r->p=a;\n return make_pair(recalc(a),s.second);\n }\n\n template<typename T>\n Node* insert(Node *a,size_t pos,T v){\n Node* b=create(v);\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* find_by_order(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return find_by_order(a->l,k);\n if(k>num) return find_by_order(a->r,k-(num+1));\n return a;\n }\n\n inline bool is_right(Node* a){\n return a->p&&a->p->r==a;\n }\n\n size_t order_of_key(Node* a){\n size_t res=count(a->l);\n while(a){\n if(is_right(a)) res+=count(a->p->l)+1;\n a=a->p;\n }\n return res;\n }\n\n template<typename T>\n Node* build(size_t l,size_t r,const vector<T> &vs){\n if(l+1==r) return create(vs[l]);\n size_t m=(l+r)>>1;\n return merge(build(l,m,vs),build(m,r,vs));\n }\n\n template<typename T>\n Node* build(const vector<T> &vs){\n return build(0,vs.size(),vs);\n }\n};\ntemplate<typename Node, size_t LIM>\narray<Node, LIM> BBSTBase<Node, LIM>::pool;\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\ntemplate<typename Node, size_t LIM>\nstruct BBSTBase{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n static array<Node, LIM> pool;\n size_t ptr;\n BBSTBase():ptr(0){}\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n inline Node* create(){\n return &pool[ptr++];\n }\n\n template<typename T>\n inline Node* create(T v){\n return &(pool[ptr++]=Node(v));\n }\n\n virtual void toggle(Node *a)=0;\n virtual Node* eval(Node* a)=0;\n virtual Node* recalc(Node* a)=0;\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n a->r->p=a;\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n b->l->p=b;\n return recalc(b);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n if(a->l) a->l->p=nullptr;\n auto s=split(a->l,k);\n a->l=s.second;\n if(a->l) a->l->p=a;\n return make_pair(s.first,recalc(a));\n }\n if(a->r) a->r->p=nullptr;\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n if(a->r) a->r->p=a;\n return make_pair(recalc(a),s.second);\n }\n\n template<typename T>\n Node* insert(Node *a,size_t pos,T v){\n Node* b=create(v);\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* find_by_order(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return find_by_order(a->l,k);\n if(k>num) return find_by_order(a->r,k-(num+1));\n return a;\n }\n\n template<typename T>\n Node* build(size_t l,size_t r,const vector<T> &vs){\n if(l+1==r) return create(vs[l]);\n size_t m=(l+r)>>1;\n return merge(build(l,m,vs),build(m,r,vs));\n }\n\n template<typename T>\n Node* build(const vector<T> &vs){\n return build(0,vs.size(),vs);\n }\n};\ntemplate<typename Node, size_t LIM>\narray<Node, LIM> BBSTBase<Node, LIM>::pool;\n#endif\n//INSERT ABOVE HERE\n//BEGIN CUT HERE\n\ntemplate<typename Ep>\nstruct NodeBase{\n using E = Ep;\n NodeBase *l,*r,*p;\n size_t cnt;\n bool rev;\n E dat,laz;\n NodeBase(){}\n NodeBase(E dat,E laz):\n cnt(1),rev(0),dat(dat),laz(laz){l=r=p=nullptr;}\n};\n\ntemplate<typename Node, size_t LIM>\nstruct Dual : BBSTBase<Node, LIM>{\n using E = typename Node::E;\n using super = BBSTBase<Node, LIM>;\n using H = function<E(E, E)>;\n\n H h;\n E ei;\n\n Dual(H h,E ei):super(),h(h),ei(ei){}\n\n using super::create;\n using super::merge;\n using super::split;\n\n Node* build(size_t l,size_t r){\n if(l+1==r) return create(Node(ei,ei));\n size_t m=(l+r)>>1;\n return merge(build(l,m),build(m,r));\n }\n\n Node* init(int n){\n return build(0,n);\n }\n\n using super::count;\n Node* recalc(Node *a){\n a->cnt=count(a->l)+1+count(a->r);\n return a;\n }\n\n void propagate(Node *a,E x){\n a->dat=h(a->dat,x);\n a->laz=h(a->laz,x);\n }\n\n using super::toggle;\n void toggle(Node *a){\n swap(a->l,a->r);\n a->rev^=1;\n }\n\n // remove \"virtual\" for optimization\n virtual Node* eval(Node* a){\n if(a->laz!=ei){\n if(a->l) propagate(a->l,a->laz);\n if(a->r) propagate(a->r,a->laz);\n a->laz=ei;\n }\n if(a->rev){\n if(a->l) toggle(a->l);\n if(a->r) toggle(a->r);\n a->rev=false;\n }\n return recalc(a);\n }\n\n Node* update(Node *a,size_t l,size_t r,E x){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n propagate(u,x);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* set_val(Node *a,size_t k,E x){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) a->l=set_val(a->l,k,x);\n if(k>num) a->r=set_val(a->r,k-(num+1),x);\n if(k==num) a->dat=x;\n return recalc(a);\n }\n\n E get_val(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return get_val(a->l,k);\n if(k>num) return get_val(a->r,k-(num+1));\n return a->dat;\n }\n\n void dump(Node* a,typename vector<E>::iterator it){\n if(!count(a)) return;\n a=eval(a);\n dump(a->l,it);\n *(it+count(a->l))=a->dat;\n dump(a->r,it+count(a->l)+1);\n }\n\n vector<E> dump(Node* a){\n vector<E> vs(count(a));\n dump(a,vs.begin());\n return vs;\n }\n};\n//END CUT HERE\n//INSERT ABOVE HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nsigned main(){\n int n,a,d;\n cin>>n>>a>>d;\n\n using ll = long long;\n struct E{\n ll a,b,c;\n E(){}\n E(ll a,ll b,ll c):a(a),b(b),c(c){}\n bool operator!=(const E &o) const{\n return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);\n }\n };\n\n const ll MAX = 1e9;\n auto h=\n [](E a,E b){\n E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);\n c.c=c.a/c.b+b.c;\n c.a%=c.b;\n if(c.b>MAX){\n c.a=max(0LL,MAX-(c.b-c.a));\n c.b=MAX;\n }\n return c;\n };\n E ei(0,1,0);\n\n using Node = NodeBase<E>;\n constexpr size_t LIM = 1e5+100;\n Dual<Node, LIM> G(h,ei);\n\n auto r=G.init(n);\n for(int i=0;i<n;i++){\n int v=a+d*i;\n r=G.update(r,i,i+1,E(0,1,v));\n }\n\n int m;\n cin>>m;\n for(int i=0;i<m;i++){\n int x,y,z;\n cin>>x>>y>>z;\n y--;\n if(x==0) r=G.toggle(r,y,z);\n if(x==1) r=G.update(r,y,z,E(0,1,1));\n if(x==2) r=G.update(r,y,z,E(0,2,0));\n }\n\n int k;\n cin>>k;\n k--;\n cout<<G.get_val(r,k).c<<endl;\n return 0;\n}", "accuracy": 0.29411764705882354, "time_ms": 270, "memory_kb": 7592, "score_of_the_acc": -0.3519, "final_rank": 18 }, { "submission_id": "aoj_1579_3949246", "code_snippet": "#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1579\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\nstruct FastIO{\n FastIO(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n}fastio_beet;\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\ntemplate<typename Node, size_t LIM>\nstruct BBSTBase{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n static array<Node, LIM> pool;\n size_t ptr;\n BBSTBase():ptr(0){}\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n inline Node* create(){\n return &pool[ptr++];\n }\n\n template<typename T>\n inline Node* create(T v){\n return &(pool[ptr++]=Node(v));\n }\n\n virtual void toggle(Node *a)=0;\n virtual Node* eval(Node* a)=0;\n virtual Node* recalc(Node* a)=0;\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n a->r->p=a;\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n b->l->p=b;\n return recalc(b);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n if(a->l) a->l->p=nullptr;\n auto s=split(a->l,k);\n a->l=s.second;\n if(a->l) a->l->p=a;\n return make_pair(s.first,recalc(a));\n }\n if(a->r) a->r->p=nullptr;\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n if(a->r) a->r->p=a;\n return make_pair(recalc(a),s.second);\n }\n\n template<typename T>\n Node* insert(Node *a,size_t pos,T v){\n Node* b=create(v);\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* find_by_order(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return find_by_order(a->l,k);\n if(k>num) return find_by_order(a->r,k-(num+1));\n return a;\n }\n\n inline bool is_right(Node* a){\n return a->p&&a->p->r==a;\n }\n\n size_t order_of_key(Node* a){\n size_t res=count(a->l);\n while(a){\n if(is_right(a)) res+=count(a->p->l)+1;\n a=a->p;\n }\n return res;\n }\n\n template<typename T>\n Node* build(size_t l,size_t r,const vector<T> &vs){\n if(l+1==r) return create(vs[l]);\n size_t m=(l+r)>>1;\n return merge(build(l,m,vs),build(m,r,vs));\n }\n\n template<typename T>\n Node* build(const vector<T> &vs){\n return build(0,vs.size(),vs);\n }\n};\ntemplate<typename Node, size_t LIM>\narray<Node, LIM> BBSTBase<Node, LIM>::pool;\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\ntemplate<typename Node, size_t LIM>\nstruct BBSTBase{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n static array<Node, LIM> pool;\n size_t ptr;\n BBSTBase():ptr(0){}\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n inline Node* create(){\n return &pool[ptr++];\n }\n\n template<typename T>\n inline Node* create(T v){\n return &(pool[ptr++]=Node(v));\n }\n\n virtual void toggle(Node *a)=0;\n virtual Node* eval(Node* a)=0;\n virtual Node* recalc(Node* a)=0;\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n a->r->p=a;\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n b->l->p=b;\n return recalc(b);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n if(a->l) a->l->p=nullptr;\n auto s=split(a->l,k);\n a->l=s.second;\n if(a->l) a->l->p=a;\n return make_pair(s.first,recalc(a));\n }\n if(a->r) a->r->p=nullptr;\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n if(a->r) a->r->p=a;\n return make_pair(recalc(a),s.second);\n }\n\n template<typename T>\n Node* insert(Node *a,size_t pos,T v){\n Node* b=create(v);\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* find_by_order(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return find_by_order(a->l,k);\n if(k>num) return find_by_order(a->r,k-(num+1));\n return a;\n }\n\n template<typename T>\n Node* build(size_t l,size_t r,const vector<T> &vs){\n if(l+1==r) return create(vs[l]);\n size_t m=(l+r)>>1;\n return merge(build(l,m,vs),build(m,r,vs));\n }\n\n template<typename T>\n Node* build(const vector<T> &vs){\n return build(0,vs.size(),vs);\n }\n};\ntemplate<typename Node, size_t LIM>\narray<Node, LIM> BBSTBase<Node, LIM>::pool;\n#endif\n//INSERT ABOVE HERE\n//BEGIN CUT HERE\n\ntemplate<typename Ep>\nstruct NodeBase{\n using E = Ep;\n NodeBase *l,*r,*p;\n size_t cnt;\n bool rev;\n E dat,laz;\n NodeBase(){}\n NodeBase(E dat,E laz):\n cnt(1),rev(0),dat(dat),laz(laz){l=r=p=nullptr;}\n};\n\ntemplate<typename Node, size_t LIM>\nstruct Dual : BBSTBase<Node, LIM>{\n using E = typename Node::E;\n using super = BBSTBase<Node, LIM>;\n using H = function<E(E, E)>;\n\n H h;\n E ei;\n\n Dual(H h,E ei):super(),h(h),ei(ei){}\n\n using super::create;\n using super::merge;\n using super::split;\n\n Node* build(size_t l,size_t r){\n if(l+1==r) return create(Node(ei,ei));\n size_t m=(l+r)>>1;\n return merge(build(l,m),build(m,r));\n }\n\n Node* init(int n){\n return build(0,n);\n }\n\n using super::count;\n Node* recalc(Node *a){\n a->cnt=count(a->l)+1+count(a->r);\n return a;\n }\n\n void propagate(Node *a,E x){\n a->dat=h(a->dat,x);\n a->laz=h(a->laz,x);\n }\n\n using super::toggle;\n void toggle(Node *a){\n swap(a->l,a->r);\n a->rev^=1;\n }\n\n // remove \"virtual\" for optimization\n virtual Node* eval(Node* a){\n if(a->laz!=ei){\n if(a->l) propagate(a->l,a->laz);\n if(a->r) propagate(a->r,a->laz);\n a->laz=ei;\n }\n if(a->rev){\n if(a->l) toggle(a->l);\n if(a->r) toggle(a->r);\n a->rev=false;\n }\n return recalc(a);\n }\n\n Node* update(Node *a,size_t l,size_t r,E x){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n propagate(u,x);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* set_val(Node *a,size_t k,E x){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) a->l=set_val(a->l,k,x);\n if(k>num) a->r=set_val(a->r,k-(num+1),x);\n if(k==num) a->dat=x;\n return recalc(a);\n }\n\n E get_val(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return get_val(a->l,k);\n if(k>num) return get_val(a->r,k-(num+1));\n return a->dat;\n }\n\n void dump(Node* a,typename vector<E>::iterator it){\n if(!count(a)) return;\n a=eval(a);\n dump(a->l,it);\n *(it+count(a->l))=a->dat;\n dump(a->r,it+count(a->l)+1);\n }\n\n vector<E> dump(Node* a){\n vector<E> vs(count(a));\n dump(a,vs.begin());\n return vs;\n }\n};\n//END CUT HERE\n//INSERT ABOVE HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nsigned main(){\n int n,a,d;\n cin>>n>>a>>d;\n\n using ll = long long;\n struct E{\n ll a,b,c;\n E(){}\n E(ll a,ll b,ll c):a(a),b(b),c(c){}\n bool operator!=(const E &o) const{\n return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);\n }\n };\n\n const ll MAX = 1e9;\n auto h=\n [](E a,E b){\n E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);\n c.c=c.a/c.b+b.c;\n c.a%=c.b;\n if(c.b>MAX){\n c.a=max(0LL,MAX-(c.b-c.a));\n c.b=MAX;\n }\n return c;\n };\n E ei(0,1,0);\n\n using Node = NodeBase<E>;\n constexpr size_t LIM = 1e6;\n Dual<Node, LIM> G(h,ei);\n\n auto r=G.init(n);\n for(int i=0;i<n;i++){\n int v=a+d*i;\n r=G.update(r,i,i+1,E(0,1,v));\n }\n\n int m;\n cin>>m;\n for(int i=0;i<m;i++){\n int x,y,z;\n cin>>x>>y>>z;\n y--;\n if(x==0) r=G.toggle(r,y,z);\n if(x==1) r=G.update(r,y,z,E(0,1,1));\n if(x==2) r=G.update(r,y,z,E(0,2,0));\n }\n\n int k;\n cin>>k;\n k--;\n cout<<G.get_val(r,k).c<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 1000, "memory_kb": 19692, "score_of_the_acc": -1.457, "final_rank": 15 }, { "submission_id": "aoj_1579_3939793", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\ntemplate<typename E>\nstruct RBST{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n struct Node{\n Node *l,*r;\n size_t cnt;\n bool rev;\n E dat,laz;\n Node(){}\n Node(E dat,E laz):\n cnt(1),rev(0),dat(dat),laz(laz){l=r=nullptr;}\n };\n\n using H = function<E(E,E)>;\n\n H h;\n E ei;\n\n const size_t LIM = 1e6;\n vector<Node> pool;\n size_t ptr;\n\n RBST(H h,E ei):h(h),ei(ei),pool(LIM),ptr(0){}\n\n inline Node* create(){\n return &(pool[ptr++]=Node(ei,ei));\n }\n\n Node* build(size_t l,size_t r){\n if(l+1==r) return create();\n size_t m=(l+r)>>1;\n return merge(build(l,m),build(m,r));\n }\n\n Node* init(int n){\n return build(0,n);\n }\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n Node* recalc(Node *a){\n a->cnt=count(a->l)+1+count(a->r);\n return a;\n }\n\n void propagate(Node *a,E x){\n a->dat=h(a->dat,x);\n a->laz=h(a->laz,x);\n }\n\n void toggle(Node *a){\n swap(a->l,a->r);\n a->rev^=1;\n }\n\n // remove \"virtual\" for optimization\n Node* eval(Node* a){\n if(a->laz!=ei){\n if(a->l) propagate(a->l,a->laz);\n if(a->r) propagate(a->r,a->laz);\n a->laz=ei;\n }\n if(a->rev){\n if(a->l) toggle(a->l);\n if(a->r) toggle(a->r);\n a->rev=false;\n }\n return recalc(a);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n auto s=split(a->l,k);\n a->l=s.second;\n return make_pair(s.first,recalc(a));\n }\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n return make_pair(recalc(a),s.second);\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n return recalc(b);\n }\n\n Node* insert(Node *a,size_t pos){\n Node* b=create();\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* update(Node *a,size_t l,size_t r,E x){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n propagate(u,x);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* set_val(Node *a,size_t k,E x){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) a->l=set_val(a->l,k,x);\n if(k>num) a->r=set_val(a->r,k-(num+1),x);\n if(k==num) a->dat=x;\n return recalc(a);\n }\n\n E get_val(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return get_val(a->l,k);\n if(k>num) return get_val(a->r,k-(num+1));\n return a->dat;\n }\n\n void dump(Node* a,typename vector<E>::iterator it){\n if(!count(a)) return;\n a=eval(a);\n dump(a->l,it);\n *(it+count(a->l))=a->dat;\n dump(a->r,it+count(a->l)+1);\n }\n\n vector<E> dump(Node* a){\n vector<E> v(count(a));\n dump(a,v.begin());\n return v;\n }\n};\n\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n,a,d;\n cin>>n>>a>>d;\n\n using ll = long long;\n struct E{\n ll a,b,c;\n E(){}\n E(ll a,ll b,ll c):a(a),b(b),c(c){}\n bool operator!=(const E &o) const{\n return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);\n }\n };\n\n const ll LIM = 1e9;\n auto h=\n [](E a,E b){\n E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);\n c.c=c.a/c.b+b.c;\n c.a%=c.b;\n if(c.b>LIM){\n c.a=max(0LL,LIM-(c.b-c.a));\n c.b=LIM;\n }\n return c;\n };\n E ei(0,1,0);\n RBST<E> G(h,ei);\n\n auto r=G.init(n);\n for(int i=0;i<n;i++){\n int v=a+d*i;\n r=G.update(r,i,i+1,E(0,1,v));\n }\n\n int m;\n cin>>m;\n for(int i=0;i<m;i++){\n int x,y,z;\n cin>>x>>y>>z;\n y--;\n if(x==0) r=G.toggle(r,y,z);\n if(x==1) r=G.update(r,y,z,E(0,1,1));\n if(x==2) r=G.update(r,y,z,E(0,2,0));\n }\n\n int k;\n cin>>k;\n k--;\n cout<<G.get_val(r,k).c<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 970, "memory_kb": 17808, "score_of_the_acc": -1.3703, "final_rank": 13 }, { "submission_id": "aoj_1579_3939528", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\ntemplate<typename E>\nstruct RBST{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n struct Node{\n Node *l,*r;\n size_t cnt;\n bool rev;\n E dat,laz;\n Node(){}\n Node(E dat,E laz):\n cnt(1),rev(0),dat(dat),laz(laz){l=r=nullptr;}\n };\n\n using H = function<E(E,E)>;\n\n H h;\n E ei;\n\n const size_t LIM = 1e6;\n vector<Node> pool;\n size_t ptr;\n\n RBST(H h,E ei):h(h),ei(ei),pool(LIM),ptr(0){}\n\n inline Node* create(){\n return &(pool[ptr++]=Node(ei,ei));\n }\n\n Node* build(size_t l,size_t r){\n if(l+1==r) return create();\n size_t m=(l+r)>>1;\n return merge(build(l,m),build(m,r));\n }\n\n Node* init(int n){\n return build(0,n);\n }\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n Node* recalc(Node *a){\n a->cnt=count(a->l)+1+count(a->r);\n return a;\n }\n\n void propagate(Node *a,E x){\n a->dat=h(a->dat,x);\n a->laz=h(a->laz,x);\n }\n\n void toggle(Node *a){\n swap(a->l,a->r);\n a->rev^=1;\n }\n\n // remove \"virtual\" for optimization\n virtual Node* eval(Node* a){\n if(a->laz!=ei){\n if(a->l) propagate(a->l,a->laz);\n if(a->r) propagate(a->r,a->laz);\n a->laz=ei;\n }\n if(a->rev){\n if(a->l) toggle(a->l);\n if(a->r) toggle(a->r);\n a->rev=false;\n }\n return recalc(a);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n auto s=split(a->l,k);\n a->l=s.second;\n return make_pair(s.first,recalc(a));\n }\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n return make_pair(recalc(a),s.second);\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n return recalc(b);\n }\n\n Node* insert(Node *a,size_t pos){\n Node* b=create();\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* update(Node *a,size_t l,size_t r,E x){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n propagate(u,x);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* set_val(Node *a,size_t k,E x){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) a->l=set_val(a->l,k,x);\n if(k>num) a->r=set_val(a->r,k-(num+1),x);\n if(k==num) a->dat=x;\n return recalc(a);\n }\n\n E get_val(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return get_val(a->l,k);\n if(k>num) return get_val(a->r,k-(num+1));\n return a->dat;\n }\n\n void dump(Node* a,typename vector<E>::iterator it){\n if(!count(a)) return;\n a=eval(a);\n dump(a->l,it);\n *(it+count(a->l))=a->dat;\n dump(a->r,it+count(a->l)+1);\n }\n\n vector<E> dump(Node* a){\n vector<E> v(count(a));\n dump(a,v.begin());\n return v;\n }\n};\n\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n,a,d;\n cin>>n>>a>>d;\n\n using ll = long long;\n struct E{\n ll a,b,c;\n E(){}\n E(ll a,ll b,ll c):a(a),b(b),c(c){}\n bool operator!=(const E &o) const{\n return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);\n }\n };\n\n const ll LIM = 1e9;\n auto h=\n [](E a,E b){\n E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);\n c.c=c.a/c.b+b.c;\n c.a%=c.b;\n if(c.b>LIM){\n c.a=max(0LL,LIM-(c.b-c.a));\n c.b=LIM;\n }\n return c;\n };\n E ei(0,1,0);\n RBST<E> G(h,ei);\n\n auto r=G.init(n);\n for(int i=0;i<n;i++){\n int v=a+d*i;\n r=G.update(r,i,i+1,E(0,1,v));\n }\n\n int m;\n cin>>m;\n for(int i=0;i<m;i++){\n int x,y,z;\n cin>>x>>y>>z;\n y--;\n if(x==0) r=G.toggle(r,y,z);\n if(x==1) r=G.update(r,y,z,E(0,1,1));\n if(x==2) r=G.update(r,y,z,E(0,2,0));\n }\n\n int k;\n cin>>k;\n k--;\n cout<<G.get_val(r,k).c<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 960, "memory_kb": 17768, "score_of_the_acc": -1.3589, "final_rank": 12 }, { "submission_id": "aoj_1579_3939517", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\ntemplate<typename E>\nstruct RBST{\n using u32 = uint32_t;\n u32 xor128(){\n static u32 x = 123456789;\n static u32 y = 362436069;\n static u32 z = 521288629;\n static u32 w = 88675123;\n\n u32 t = x ^ (x << 11);\n x = y; y = z; z = w;\n return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));\n }\n\n struct Node{\n Node *l,*r;\n size_t cnt;\n bool rev;\n E dat,laz;\n Node(){}\n Node(E dat,E laz):\n cnt(1),rev(0),dat(dat),laz(laz){l=r=nullptr;}\n };\n\n using H = function<E(E,E)>;\n\n H h;\n E ei;\n\n const size_t LIM = 1e6;\n vector<Node> pool;\n size_t ptr;\n\n RBST(H h,E ei):h(h),ei(ei),pool(LIM),ptr(0){}\n\n inline Node* create(){\n return &(pool[ptr++]=Node(ei,ei));\n }\n\n Node* build(size_t l,size_t r){\n if(l+1==r) return create();\n size_t m=(l+r)>>1;\n return merge(build(l,m),build(m,r));\n }\n\n Node* init(int n){\n return build(0,n);\n }\n\n size_t count(const Node *a){\n return a?a->cnt:0;\n }\n\n Node* recalc(Node *a){\n a->cnt=count(a->l)+1+count(a->r);\n return a;\n }\n\n void propagate(Node *a,E x){\n a->dat=h(a->dat,x);\n a->laz=h(a->laz,x);\n }\n\n void toggle(Node *a){\n swap(a->l,a->r);\n a->rev^=1;\n }\n\n // remove \"virtual\" for optimization\n virtual Node* eval(Node* a){\n if(a->laz!=ei){\n if(a->l) propagate(a->l,a->laz);\n if(a->r) propagate(a->r,a->laz);\n a->laz=ei;\n }\n if(a->rev){\n if(a->l) toggle(a->l);\n if(a->r) toggle(a->r);\n a->rev=false;\n }\n return recalc(a);\n }\n\n pair<Node*, Node*> split(Node* a,size_t k){\n if(a==nullptr) return make_pair(a,a);\n a=eval(a);\n if(k<=count(a->l)){\n auto s=split(a->l,k);\n a->l=s.second;\n return make_pair(s.first,recalc(a));\n }\n auto s=split(a->r,k-(count(a->l)+1));\n a->r=s.first;\n return make_pair(recalc(a),s.second);\n }\n\n Node* merge(Node* a,Node* b){\n if(a==nullptr) return b;\n if(b==nullptr) return a;\n if(xor128()%(count(a)+count(b))<count(a)){\n a=eval(a);\n a->r=merge(a->r,b);\n return recalc(a);\n }\n b=eval(b);\n b->l=merge(a,b->l);\n return recalc(b);\n }\n\n Node* insert(Node *a,size_t pos){\n Node* b=create();\n auto s=split(a,pos);\n return a=merge(merge(s.first,b),s.second);\n }\n\n Node* erase(Node *a,size_t pos){\n auto s=split(a,pos);\n auto t=split(s.second,1);\n return merge(s.first,t.second);\n }\n\n Node* toggle(Node *a,size_t l,size_t r){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n toggle(u);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* update(Node *a,size_t l,size_t r,E x){\n auto s=split(a,l);\n auto t=split(s.second,r-l);\n auto u=eval(t.first);\n propagate(u,x);\n return merge(s.first,merge(u,t.second));\n }\n\n Node* set_val(Node *a,size_t k,E x){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) a->l=set_val(a->l,k,x);\n if(k>num) a->r=set_val(a->r,k-(num+1),x);\n if(k==num) a->dat=x;\n return recalc(a);\n }\n\n E get_val(Node *a,size_t k){\n assert(k<count(a));\n a=eval(a);\n size_t num=count(a->l);\n if(k<num) return get_val(a->l,k);\n if(k>num) return get_val(a->r,k-(num+1));\n return a->dat;\n }\n\n void dump(Node* a,typename vector<E>::iterator it){\n if(!count(a)) return;\n a=eval(a);\n dump(a->l,it);\n *(it+count(a->l))=a->dat;\n dump(a->r,it+count(a->l)+1);\n }\n\n vector<E> dump(Node* a){\n vector<E> v(count(a));\n dump(a,v.begin());\n return v;\n }\n};\n\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n,a,d;\n cin>>n>>a>>d;\n\n using ll = long long;\n struct E{\n ll a,b,c;\n E(){}\n E(ll a,ll b,ll c):a(a),b(b),c(c){}\n bool operator==(const E &o) const{\n return a==o.a&&b==o.b&&c==o.c;\n }\n bool operator!=(const E &o) const{\n return !(*this==o);\n }\n };\n\n const ll LIM = 1e9;\n auto h=\n [](E a,E b){\n E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);\n c.c=c.a/c.b+b.c;\n c.a%=c.b;\n if(c.b>LIM){\n c.a=max(0LL,LIM-(c.b-c.a));\n c.b=LIM;\n }\n return c;\n };\n E ei(0,1,0);\n RBST<E> G(h,ei);\n\n auto r=G.init(n);\n for(int i=0;i<n;i++){\n int v=a+d*i;\n r=G.update(r,i,i+1,E(0,1,v));\n }\n\n int m;\n cin>>m;\n for(int i=0;i<m;i++){\n int x,y,z;\n cin>>x>>y>>z;\n y--;\n if(x==0) r=G.toggle(r,y,z);\n if(x==1) r=G.update(r,y,z,E(0,1,1));\n if(x==2) r=G.update(r,y,z,E(0,2,0));\n }\n\n int k;\n cin>>k;\n k--;\n cout<<G.get_val(r,k).c<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 970, "memory_kb": 17864, "score_of_the_acc": -1.372, "final_rank": 14 }, { "submission_id": "aoj_1579_3804697", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1579.cc: Array Update 2\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_M = 200000;\n\n/* typedef */\n\nstruct Op {\n int x, y, z;\n Op() {}\n Op(int _x, int _y, int _z): x(_x), y(_y), z(_z) {}\n};\n\n/* global variables */\n\nOp ops[MAX_M];\nint xs[MAX_M];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n, a, d, m;\n scanf(\"%d%d%d%d\", &n, &a, &d, &m);\n\n for (int i = 0; i < m; i++)\n scanf(\"%d%d%d\", &ops[i].x, &ops[i].y, &ops[i].z);\n\n int k;\n scanf(\"%d\", &k);\n\n int l = 0;\n for (int i = m - 1; i >= 0; i--) {\n Op &op = ops[i];\n if (op.y <= k && k <= op.z) {\n if (op.x != 0)\n\txs[l++] = op.x;\n else\n\tk = op.z - (k - op.y);\n }\n }\n //printf(\"l=%d, k=%d\\n\", l, k);\n\n int v = a + d * (k - 1);\n for (int i = l - 1; i >= 0; i--) {\n if (xs[i] == 1) v++;\n else v >>= 1;\n }\n\n printf(\"%d\\n\", v);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5412, "score_of_the_acc": -0.0319, "final_rank": 3 }, { "submission_id": "aoj_1579_2750222", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nint rev(int x, int l, int r){\n int n = x-l;\n return r-n;\n}\n\nint main(){\n int n,a,d,m;\n cin >>n >>a >>d >>m;\n\n vector<int> x(m),y(m),z(m);\n rep(i,m){\n cin >>x[i] >>y[i] >>z[i];\n --y[i];\n --z[i];\n }\n\n int k;\n cin >>k;\n --k;\n\n int idx = k;\n for(int i=m-1; i>=0; --i){\n if(x[i]==0){\n if(y[i]<=idx && idx<=z[i]) idx = rev(idx, y[i], z[i]);\n }\n }\n\n ll val = a+idx*d;\n rep(i,m){\n if(y[i]<=idx && idx<=z[i]){\n if(x[i]==0) idx = rev(idx, y[i], z[i]);\n if(x[i]==1) ++val;\n if(x[i]==2) val /= 2;\n }\n }\n cout << val << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4936, "score_of_the_acc": -0.0994, "final_rank": 8 }, { "submission_id": "aoj_1579_2747856", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define mp make_pair\n#define pb push_back\n#define all(x) begin(x),end(x)\n#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))\n#define fi first\n#define se second\n#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\nvoid _dbg(string){cerr<<endl;}\ntemplate<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<\" = \"<<h<<\", \";_dbg(s.substr(l+1),t...);}\ntemplate<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n#define long long long // for codeforces\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0); cout.tie(0);\n\n int n;\n long a,d;\n int m;\n cin>>n>>a>>d>>m;\n\n vector<int> x(m), y(m), z(m);\n rep(i,m) cin>>x[i]>>y[i]>>z[i];\n rep(i,m) y[i]--,z[i]--;\n int k;\n cin>>k;\n k--;\n\n for(int i=m-1; i>=0; i--){\n if(x[i]==0 && y[i]<=k && k<=z[i]){\n k = y[i] + z[i] - k;\n }\n }\n\n long ans = a + d*k;\n rep(i,m) if(y[i]<=k && k<=z[i]){\n if(x[i]==0){\n k = y[i] + z[i] - k;\n }\n else if(x[i]==1){\n ans++;\n }\n else if(x[i]==2){\n ans /= 2;\n }\n else assert(false);\n }\n\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4856, "score_of_the_acc": -0.0256, "final_rank": 2 }, { "submission_id": "aoj_1579_2691947", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nenum ORDER{\n\tPLUS,\n\tHALF,\n};\n\nint x[200000],y[200000],z[200000];\n\nint main(){\n\n\tint N;\n\tscanf(\"%d\",&N);\n\n\tint a,d;\n\tscanf(\"%d %d\",&a,&d);\n\n\tint M;\n\tscanf(\"%d\",&M);\n\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%d %d %d\",&x[i],&y[i],&z[i]);\n\t\ty[i]--;\n\t\tz[i]--;\n\t}\n\n\tint loc;\n\tscanf(\"%d\",&loc);\n\tloc--;\n\n\tstack<ORDER> S;\n\n\tfor(int i = M-1; i >= 0; i--){\n\n\t\tswitch(x[i]){\n\t\tcase 0:\n\t\t\tif(loc >= y[i] && loc <= z[i]){\n\t\t\t\tloc = z[i]-(loc-y[i]);\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\tif(loc >= y[i] && loc <= z[i]){\n\t\t\t\tS.push(PLUS);\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 2:\n\t\t\tif(loc >= y[i] && loc <= z[i]){\n\t\t\t\tS.push(HALF);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tint ans = a+loc*d;\n\n\twhile(!S.empty()){\n\n\t\tif(S.top() == PLUS){\n\t\t\tans++;\n\t\t}else{\n\t\t\tans /= 2;\n\t\t}\n\n\t\tS.pop();\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5520, "score_of_the_acc": -0.0555, "final_rank": 5 }, { "submission_id": "aoj_1579_2636764", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\nint main(){\n\n int n,a,d,m,k;\n cin >> n;\n cin >> a >> d;\n cin >> m;\n vector<int> x(m),y(m),z(m);\n for(int i=0; i<m; i++){\n cin >> x[i] >> y[i] >> z[i];\n y[i]--;\n z[i]--;\n }\n cin >> k;\n k--;\n vector<int> com;\n for(int i=m-1; i>=0; i--){\n if(x[i]==0 && (y[i]<=k && k<=z[i])){\n k = y[i]+z[i]-k;\n //cout << \"1 \" << i << endl;;\n }else{\n if(y[i]<=k && k<=z[i]){\n com.push_back(x[i]);\n }\n }\n }\n //cout << k << endl;\n int num = a+d*k;\n //cout << com.size() << endl;\n for(int i=com.size()-1;i>=0;i--){\n if(com[i]==1){\n num++;\n }else if(com[i]==2){\n num/=2;\n }\n }\n \n cout << num << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 5236, "score_of_the_acc": -0.1083, "final_rank": 10 }, { "submission_id": "aoj_1579_2636734", "code_snippet": "#include<iostream>\nusing namespace std;\nint main(){\n int n;\n cin>>n;\n int a,b;\n cin>>a>>b;\n int vec[n];\n for(int i=0;i<n;++i,a+=b) vec[i]=a;\n int m;\n cin>>m;\n int x[m], y[m], z[m];\n for(int i=0;i<m;++i) cin>>x[i]>>y[i]>>z[i];\n int k;\n cin>>k;\n --k;\n for(int i=m-1;i>=0;--i){\n if(x[i]==0){\n if(y[i]-1<=k && k<z[i]) k=z[i]-(k-y[i]+1)-1;\n }\n }\n for(int i=0;i<m;++i){\n if(y[i]-1<=k && k<z[i]){\n if(x[i]==1) vec[k]++;\n else if(x[i]==2) vec[k]/=2;\n }\n }\n cout<<vec[k]<<endl;\n}", "accuracy": 0.17647058823529413, "time_ms": 50, "memory_kb": 4636, "score_of_the_acc": -0.0394, "final_rank": 19 }, { "submission_id": "aoj_1579_2577969", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define MAX 200005\n\nint N,M,K,a,d;\nint x[MAX],y[MAX],z[MAX];\n\nint main(){\n scanf(\"%d %d %d %d\",&N,&a,&d,&M);\n for(int i=0;i<M;i++)scanf(\"%d %d %d\",&x[i],&y[i],&z[i]);\n scanf(\"%d\",&K);\n stack<int> st;\n for(int i=M-1;i>=0;i--){\n if(K<y[i]||z[i]<K)continue;\n if(x[i]==0)K=z[i]+y[i]-K; \n else st.push(x[i]);\n }\n int ans=a+d*(K-1);\n while(!st.empty()){\n if(st.top()==1)ans++;\n else ans/=2;\n st.pop();\n }\n printf(\"%d\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 5476, "score_of_the_acc": -0.044, "final_rank": 4 }, { "submission_id": "aoj_1579_2577860", "code_snippet": "#include <iostream>\n#include <vector>\n\nconstexpr int REVERSE = 0;\nconstexpr int INCREMENT = 1;\nconstexpr int HALF = 2;\n\nbool cover(int l, int r, int x)\n{\n return l <= x && x <= r;\n}\n\nint reverse(int l, int r, int x)\n{\n return r + l - x;\n}\n\nint main()\n{\n int N, a, d, M;\n std::cin >> N >> a >> d >> M;\n\n std::vector<int> x(M), y(M), z(M);\n for (int i = 0; i < M; i++) {\n std::cin >> x[i] >> y[i] >> z[i];\n }\n\n int K;\n std::cin >> K;\n\n for (int i = M - 1; i >= 0; i--) {\n if (x[i] == REVERSE && cover(y[i], z[i], K)) {\n K = reverse(y[i], z[i], K);\n }\n }\n\n int val = a + (K - 1) * d;\n for (int i = 0; i < M; i++) {\n if (!cover(y[i], z[i], K)) {\n continue;\n }\n \n switch (x[i]) {\n case REVERSE:\n K = reverse(y[i], z[i], K);\n break;\n \n case INCREMENT:\n val++;\n break;\n\n case HALF:\n val /= 2;\n break;\n }\n }\n\n std::cout << val << std::endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4928, "score_of_the_acc": -0.0991, "final_rank": 7 }, { "submission_id": "aoj_1579_2577852", "code_snippet": "#include <iostream>\n#include <vector>\n\nconstexpr int REVERSE = 0;\nconstexpr int INCREMENT = 1;\nconstexpr int HALF = 2;\n\nbool cover(int l, int r, int x)\n{\n return l <= x && x <= r;\n}\n\nint reverse(int l, int r, int x)\n{\n return r + l - x;\n}\n\nint main()\n{\n int N, a, d, M;\n std::cin >> N >> a >> d >> M;\n\n std::vector<int> x(M), y(M), z(M);\n for (int i = 0; i < M; i++) {\n std::cin >> x[i] >> y[i] >> z[i];\n }\n\n int K;\n std::cin >> K;\n\n for (int i = M - 1; i >= 0; i--) {\n if (x[i] == REVERSE && cover(y[i], z[i], K)) {\n K = reverse(y[i], z[i], K);\n }\n }\n\n int val = a + (K - 1) * d;\n for (int i = 0; i < M; i++) {\n if (!cover(y[i], z[i], K)) {\n continue;\n }\n \n switch (x[i]) {\n case INCREMENT:\n val++;\n break;\n\n case HALF:\n val /= 2;\n break;\n }\n }\n\n std::cout << val << std::endl;\n \n return 0;\n}", "accuracy": 0.17647058823529413, "time_ms": 60, "memory_kb": 4340, "score_of_the_acc": -0.0408, "final_rank": 20 } ]
aoj_1582_cpp
Circles and Ray Problem 2次元平面上にそれぞれ互いに共通部分をもたない N 個の円が与えられる。 また、円にはそれぞれ1から N までの番号が割り振られている。 あなたは、端点が1番目の円の円周上にあるような半直線を任意の数だけ設置することができる。 どの円も1本以上の半直線との共通部分を持っているようにするためには、最低何本の半直線を設置しなければならないかを求めなさい。 Input 入力は以下の形式で与えられる。 N x 1 y 1 r 1 x 2 y 2 r 2 ... x N y N r N 1行目に、1つの整数 N が与えられる。 2行目からの N 行のうち i 行目には i 番目の円のx座標、y座標、半径を表す3つの整数 x i , y i , r i が空白区切りで与えられる。 Constraints 2 ≤ N ≤ 16 -100 ≤ x i ≤ 100 (1 ≤ i ≤ N ) -100 ≤ y i ≤ 100 (1 ≤ i ≤ N ) 1 ≤ r i ≤ 100 (1 ≤ i ≤ N ) Output どの円も1本以上の半直線との共通部分を持っているようにするためには最低何本の半直線を設置しなければならないかを出力せよ。 Sample Input 1 3 0 0 2 3 3 1 6 1 1 Sample Output 1 1 Sample Input 2 4 1 2 3 12 2 2 7 6 2 1 9 3 Sample Output 2 2 Sample Input 3 5 0 0 5 0 10 1 10 0 1 -10 0 1 0 -10 1 Sample Output 3 4
[ { "submission_id": "aoj_1582_10240155", "code_snippet": "// AOJ #1582 Circles and Ray\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst double EPS = 1e-9;\nconst double PI = acos(-1.0);\n\nstruct Circle { int x, y, r; };\n\nstruct RayCandidate {\n double px, py;\n double dx, dy;\n int mask;\n};\n\ndouble dist(double ax, double ay, double bx, double by) {\n return hypot(ax - bx, ay - by);\n}\n\ndouble normAngle(double a) {\n while(a < 0) a += 2*PI;\n while(a >= 2*PI) a -= 2*PI;\n return a;\n}\n\nbool hitCircle(double Px, double Py, double dx, double dy, double cx, double cy, double r) {\n double vx = cx - Px, vy = cy - Py;\n double t = vx * dx + vy * dy;\n if(t < 0) return false;\n double projx = Px + t * dx, projy = Py + t * dy;\n return (dist(projx, projy, cx, cy) <= r + EPS);\n}\n\nint solveDP(const vector<RayCandidate>& rays, int N) {\n int full = (1 << N) - 1;\n vector<int> dp(1 << N, 1e9);\n dp[0] = 0;\n for (int mask = 0; mask < (1 << N); mask++) {\n if(dp[mask] == 1e9) continue;\n for(auto &ray : rays) {\n int nmask = mask | ray.mask;\n dp[nmask] = min(dp[nmask], dp[mask] + 1);\n }\n }\n return dp[full];\n}\n\nvoid addCandidateFromCircle1(const Circle &c1, const Circle &c2, vector<RayCandidate> &rays) {\n double cx1 = c1.x, cy1 = c1.y, r1 = c1.r;\n double cx2 = c2.x, cy2 = c2.y, r2 = c2.r;\n double d = dist(cx1, cy1, cx2, cy2);\n double base = atan2(cy2 - cy1, cx2 - cx1);\n if(d + EPS >= fabs(r1 - r2)) {\n double diff = (r1 - r2) / d;\n if(diff > 1) diff = 1; if(diff < -1) diff = -1;\n double ang = acos(diff);\n for (int sign = -1; sign <= 1; sign += 2) {\n double theta = normAngle(base + sign * ang);\n double Px = cx1 + r1 * cos(theta);\n double Py = cy1 + r1 * sin(theta);\n double dx = -sin(theta), dy = cos(theta);\n double dot = (cx2 - Px)*dx + (cy2 - Py)*dy;\n if(dot < 0) { dx = -dx; dy = -dy; }\n RayCandidate ray;\n ray.px = Px; ray.py = Py; ray.dx = dx; ray.dy = dy;\n rays.push_back(ray);\n }\n }\n if(d + EPS >= r1 + r2) {\n double diff = (r1 + r2) / d;\n if(diff > 1) diff = 1; if(diff < -1) diff = -1;\n double ang = acos(diff);\n for (int sign = -1; sign <= 1; sign += 2) {\n double theta = normAngle(base + sign * ang);\n double Px = cx1 + r1 * cos(theta);\n double Py = cy1 + r1 * sin(theta);\n double dx = -sin(theta), dy = cos(theta);\n double dot = (cx2 - Px)*dx + (cy2 - Py)*dy;\n if(dot < 0) { dx = -dx; dy = -dy; }\n RayCandidate ray;\n ray.px = Px; ray.py = Py; ray.dx = dx; ray.dy = dy;\n rays.push_back(ray);\n }\n }\n}\n\nvoid addCandidateFromPair(const Circle &c1, const Circle &ci, const Circle &cj, vector<RayCandidate> &rays) {\n double xi = ci.x, yi = ci.y, ri = ci.r;\n double xj = cj.x, yj = cj.y, rj = cj.r;\n double d = dist(xi, yi, xj, yj);\n for (int s_i = -1; s_i <= 1; s_i += 2) {\n for (int s_j = -1; s_j <= 1; s_j += 2) {\n double diff = ri * s_i - rj * s_j;\n if(d + EPS < fabs(diff)) continue;\n double cosVal = diff / d;\n if(cosVal > 1) cosVal = 1; if(cosVal < -1) cosVal = -1;\n double angOff = acos(cosVal);\n double base = atan2(yj - yi, xj - xi);\n for (int sign = -1; sign <= 1; sign += 2) {\n double theta = normAngle(base + sign * angOff);\n double Pi_x = xi + s_i * ri * cos(theta);\n double Pi_y = yi + s_i * ri * sin(theta);\n double ax = -s_i * sin(theta), ay = s_i * cos(theta);\n double cx1 = c1.x, cy1 = c1.y, r1 = c1.r;\n double Bx = Pi_x - cx1, By = Pi_y - cy1;\n double A_dot_B = ax * Bx + ay * By;\n double B2 = Bx*Bx + By*By;\n double disc = A_dot_B*A_dot_B - (B2 - r1*r1);\n if(disc < -EPS) continue;\n disc = max(disc, 0.0);\n double t1 = -A_dot_B + sqrt(disc);\n double t2 = -A_dot_B - sqrt(disc);\n for(double t : {t1, t2}) {\n double X = Pi_x + t * ax, Y = Pi_y + t * ay;\n if(fabs(dist(X, Y, cx1, cy1) - r1) > 1e-6) continue;\n double dot = (Pi_x - X)*ax + (Pi_y - Y)*ay;\n double useAx = ax, useAy = ay;\n if(dot < -EPS) {\n dot = (Pi_x - X)*(-ax) + (Pi_y - Y)*(-ay);\n useAx = -ax; useAy = -ay;\n }\n if(dot < -EPS) continue;\n RayCandidate ray;\n ray.px = X; ray.py = Y; ray.dx = useAx; ray.dy = useAy;\n rays.push_back(ray);\n }\n }\n }\n }\n}\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int N;\n cin >> N;\n vector<Circle> circ(N);\n for (int i = 0; i < N; i++){\n cin >> circ[i].x >> circ[i].y >> circ[i].r;\n }\n\n vector<RayCandidate> rays;\n for (int i = 1; i < N; i++){\n addCandidateFromCircle1(circ[0], circ[i], rays);\n }\n for (int i = 1; i < N; i++){\n for (int j = i + 1; j < N; j++){\n addCandidateFromPair(circ[0], circ[i], circ[j], rays);\n }\n }\n\n for(auto &ray : rays) {\n int m = 0;\n for (int i = 0; i < N; i++){\n if(hitCircle(ray.px, ray.py, ray.dx, ray.dy, circ[i].x, circ[i].y, circ[i].r))\n m |= (1 << i);\n }\n ray.mask = m;\n }\n int ans = solveDP(rays, N);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4068, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_1582_2750762", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n bool change = false;\n if(ar > br){\n swap(ar,br);\n swap(a,b);\n change = true;\n }\n double PI = acos(-1);\n vector<Line> ls;\n double d = abs(b-a);\n assert(!EQ(ar+br,d));\n double y = br-ar;\n double theta = asin(y/d);\n double phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta+PI/2)), \n b+polar(br,phi+sign*(theta+PI/2))));\n }\n y = br+ar;\n theta = asin(y/d);\n phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta-PI/2)), \n b+polar(br,phi+sign*(theta-PI/2)+PI)));\n }\n if(change)rep(i,ls.size()) {\n Point p,q;\n p = ls[i].first;\n q = ls[i].second;\n ls[i] = {q,p};\n }\n\n // rep (i,2) {\n // double sin = (ar - (1-i*2)*br) / d;\n // if (!LE(sin*sin, 1)) break;\n // double cos = sqrt(max(1 - sin*sin, 0.0));\n // rep (j,2) {\n // Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n // assert(EQ(abs(n),1.0));\n // ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n // if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n // }\n // }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[j],r[j],c[i],r[i]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3784, "score_of_the_acc": -0.8424, "final_rank": 16 }, { "submission_id": "aoj_1582_2750761", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n bool change = false;\n if(ar > br){\n swap(ar,br);\n swap(a,b);\n change = true;\n }\n double PI = acos(-1);\n vector<Line> ls;\n double d = abs(b-a);\n assert(!EQ(ar+br,d));\n double y = br-ar;\n double theta = asin(y/d);\n double phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta+PI/2)), \n b+polar(br,phi+sign*(theta+PI/2))));\n }\n y = br+ar;\n theta = asin(y/d);\n phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta-PI/2)), \n b+polar(br,phi+sign*(theta-PI/2)+PI)));\n }\n if(change)rep(i,ls.size()) {\n Point p,q;\n p = ls[i].first;\n q = ls[i].second;\n ls[i] = {q,p};\n }\n\n // rep (i,2) {\n // double sin = (ar - (1-i*2)*br) / d;\n // if (!LE(sin*sin, 1)) break;\n // double cos = sqrt(max(1 - sin*sin, 0.0));\n // rep (j,2) {\n // Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n // assert(EQ(abs(n),1.0));\n // ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n // if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n // }\n // }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3840, "score_of_the_acc": -1.5636, "final_rank": 8 }, { "submission_id": "aoj_1582_2750760", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n bool change = false;\n if(ar > br){\n swap(ar,br);\n swap(a,b);\n change = true;\n }\n double PI = acos(-1);\n vector<Line> ls;\n double d = abs(b-a);\n assert(!EQ(ar+br,d));\n double y = br-ar;\n double theta = asin(y/d);\n double phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta+PI/2)), \n b+polar(br,phi+sign*(theta+PI/2))));\n }\n y = br+ar;\n theta = asin(y/d);\n phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta-PI/2)), \n b+polar(br,phi+sign*(theta-PI/2)+PI)));\n }\n if(change)for(auto l : ls) {\n swap(l.first,l.second);\n }\n\n // rep (i,2) {\n // double sin = (ar - (1-i*2)*br) / d;\n // if (!LE(sin*sin, 1)) break;\n // double cos = sqrt(max(1 - sin*sin, 0.0));\n // rep (j,2) {\n // Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n // assert(EQ(abs(n),1.0));\n // ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n // if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n // }\n // }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3844, "score_of_the_acc": -0.9333, "final_rank": 19 }, { "submission_id": "aoj_1582_2750688", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n \nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n reverse(all(c));\n reverse(all(r));\n swap(c[0],c[n-1]);\n swap(r[0],r[n-1]);\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3428, "score_of_the_acc": -0.9394, "final_rank": 5 }, { "submission_id": "aoj_1582_2750685", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n if(ar > br){\n swap(ar,br);\n swap(a,b);\n }\n double PI = acos(-1);\n vector<Line> ls;\n double d = abs(b-a);\n assert(!EQ(ar+br,d));\n double y = br-ar;\n double theta = asin(y/d);\n double phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta+PI/2)), \n b+polar(br,phi+sign*(theta+PI/2))));\n }\n y = br+ar;\n theta = asin(y/d);\n phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta-PI/2)), \n b+polar(br,phi+sign*(theta-PI/2)+PI)));\n }\n\n // rep (i,2) {\n // double sin = (ar - (1-i*2)*br) / d;\n // if (!LE(sin*sin, 1)) break;\n // double cos = sqrt(max(1 - sin*sin, 0.0));\n // rep (j,2) {\n // Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n // assert(EQ(abs(n),1.0));\n // ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n // if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n // }\n // }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3840, "score_of_the_acc": -0.9273, "final_rank": 18 }, { "submission_id": "aoj_1582_2750666", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n if(ar > br){\n swap(ar,br);\n swap(a,b);\n }\n double PI = acos(-1);\n vector<Line> ls;\n double d = abs(b-a);\n double y = br-ar;\n double theta = asin(y/d);\n double phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta+PI/2)), \n b+polar(br,phi+sign*(theta+PI/2))));\n }\n y = br+ar;\n theta = asin(y/d);\n phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta-PI/2)), \n b+polar(br,phi+sign*(theta-PI/2)+PI)));\n }\n\n // rep (i,2) {\n // double sin = (ar - (1-i*2)*br) / d;\n // if (!LE(sin*sin, 1)) break;\n // double cos = sqrt(max(1 - sin*sin, 0.0));\n // rep (j,2) {\n // Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n // assert(EQ(abs(n),1.0));\n // ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n // if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n // }\n // }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3844, "score_of_the_acc": -0.9333, "final_rank": 19 }, { "submission_id": "aoj_1582_2750661", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\nconst double PI = acos(-1);\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n if(ar > br){\n swap(ar,br);\n swap(a,b);\n }\n\n vector<Line> ls;\n double d = abs(b-a);\n double y = br-ar;\n double theta = asin(y/d);\n double phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(\n Line(a+polar(ar,phi+sign*(theta+PI/2)), \n b+polar(br,phi+sign*(theta+PI/2))));\n }\n y = br+ar;\n theta = asin(y/d);\n phi = arg(b-a);\n for(int sign : {-1,1}){\n ls.push_back(Line(\n a+polar(ar,phi+sign*(theta-PI/2)), \n b+polar(br,phi+sign*(theta-PI/2)+PI)));\n }\n\n // rep (i,2) {\n // double sin = (ar - (1-i*2)*br) / d;\n // if (!LE(sin*sin, 1)) break;\n // double cos = sqrt(max(1 - sin*sin, 0.0));\n // rep (j,2) {\n // Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n // assert(EQ(abs(n),1.0));\n // ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n // if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n // }\n // }\n return ls;\n}\n \nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3832, "score_of_the_acc": -0.9152, "final_rank": 17 }, { "submission_id": "aoj_1582_2750364", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n if(ar < br){\n swap(ar,br);\n swap(a,b);\n }\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n assert(EQ(abs(n),1.0));\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n \nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[j],r[j],c[i],r[i]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3408, "score_of_the_acc": -0.2727, "final_rank": 11 }, { "submission_id": "aoj_1582_2750356", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n assert(EQ(abs(n),1.0));\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n \nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[j],r[j],c[i],r[i]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3424, "score_of_the_acc": -0.297, "final_rank": 14 }, { "submission_id": "aoj_1582_2750260", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n \n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n \n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n \n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n \n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n \n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n \nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n \n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (EQ(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n \nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n \nint main(){\n int n;\n cin >>n;\n \n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n \n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[j],r[j],c[i],r[i]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n \n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n \n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n \n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n \n m[i][j][k] = m[j][i][k] = val;\n }\n }\n \n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n \n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3420, "score_of_the_acc": -0.2909, "final_rank": 13 }, { "submission_id": "aoj_1582_2750183", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n\n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n\n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n\n m[i][j][k] = m[j][i][k] = val;\n }\n }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3420, "score_of_the_acc": -0.9273, "final_rank": 4 }, { "submission_id": "aoj_1582_2750180", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[j],r[j],c[i],r[i]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n\n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n\n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n\n m[i][j][k] = m[j][i][k] = val;\n }\n }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3408, "score_of_the_acc": -0.2727, "final_rank": 11 }, { "submission_id": "aoj_1582_2750178", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n\n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n\n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n\n m[i][j][k] = m[j][i][k] = val;\n }\n }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,i)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n cout << dp[(1<<n)-1] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3428, "score_of_the_acc": -0.303, "final_rank": 1 }, { "submission_id": "aoj_1582_2750166", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<long double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\ndouble distLP(Point a1, Point a2, Point p) {\n return abs(proj(a1, a2, p) - p);\n}\n\ndouble distLC(Point a1, Point a2, Point c, double r) {\n return max(distLP(a1, a2, c) - r, 0.0);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), (long double)0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, long double ar, Point b, long double br) {\n vector<Line> ls;\n long double d = abs(b-a);\n rep (i,2) {\n long double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n long double cos = sqrt(max(1 - sin*sin, (long double)0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(i,n)rep(j,i)if(i!=j){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n\n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n\n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n\n m[i][j][k] = val;\n }\n }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,i)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n int ans = dp[(1<<n)-1];\n assert(ans<n);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3428, "score_of_the_acc": -0.303, "final_rank": 1 }, { "submission_id": "aoj_1582_2750107", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\ndouble distLP(Point a1, Point a2, Point p) {\n return abs(proj(a1, a2, p) - p);\n}\n\ndouble distLC(Point a1, Point a2, Point c, double r) {\n return max(distLP(a1, a2, c) - r, 0.0);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(i,n)rep(j,n)if(i!=j){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n\n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n\n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n\n m[i][j][k] = val;\n }\n }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n int ans = dp[(1<<n)-1];\n assert(ans<n);\n cout << ans << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 60, "memory_kb": 3428, "score_of_the_acc": -0.303, "final_rank": 15 }, { "submission_id": "aoj_1582_2750100", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\ndouble distLP(Point a1, Point a2, Point p) {\n return abs(proj(a1, a2, p) - p);\n}\n\ndouble distLC(Point a1, Point a2, Point c, double r) {\n return max(distLP(a1, a2, c) - r, 0.0);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1.0)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(i,n)rep(j,i){\n // vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n vector<Line> seg = tangentLines(c[j],r[j],c[i],r[i]);\n\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n\n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n\n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n\n m[i][j][k] = m[j][i][k] = val;\n }\n }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,i)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n int ans = dp[(1<<n)-1];\n assert(ans<n);\n cout << ans << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 30, "memory_kb": 3428, "score_of_the_acc": -0.0303, "final_rank": 10 }, { "submission_id": "aoj_1582_2750096", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\ndouble distLP(Point a1, Point a2, Point p) {\n return abs(proj(a1, a2, p) - p);\n}\n\ndouble distLC(Point a1, Point a2, Point c, double r) {\n return max(distLP(a1, a2, c) - r, 0.0);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(i,n)rep(j,i){\n // vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n vector<Line> seg = tangentLines(c[j],r[j],c[i],r[i]);\n\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n if(tmp.size()==0) continue;\n\n Point p = tmp[0];\n if(ccw(p,l.fi,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j)|1;\n for(int x=1; x<n; ++x)if(x!=i && x!=j){\n VP ump = crosspointLC(p,l.fi,c[x],r[x]);\n if(ump.size()==0) continue;\n\n Point cc = ump[0];\n if(ccw(p,l.fi,cc)!=2) val |= (1<<x);\n }\n\n m[i][j][k] = m[j][i][k] = val;\n }\n }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,i)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n int ans = dp[(1<<n)-1];\n assert(ans<n);\n cout << ans << endl;\n return 0;\n}", "accuracy": 0.3103448275862069, "time_ms": 30, "memory_kb": 3408, "score_of_the_acc": 0, "final_rank": 9 }, { "submission_id": "aoj_1582_2748288", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\ndouble distLP(Point a1, Point a2, Point p) {\n return abs(proj(a1, a2, p) - p);\n}\n\ndouble distLC(Point a1, Point a2, Point c, double r) {\n return max(distLP(a1, a2, c) - r, 0.0);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(j,n)rep(i,j){\n vector<Line> seg = tangentLines(c[j],r[j],c[i],r[i]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n\n if(tmp.size()>0){\n Point p = tmp[0];\n Point q = l.fi;\n if(ccw(p,q,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j);\n rep(x,n)if(x!=i && x!=j){\n VP ump = crosspointLC(p,q,c[x],r[x]);\n if(ump.size()>0){\n // dbg(crosspointLC(p,q,c[x],r[x]).size());\n\n Point cc = ump[0];\n if(ccw(p,q,cc)!=2) val |= (1<<x);\n }\n }\n\n assert((val&1) == 1);\n m[i][j][k] = m[j][i][k] = val;\n }\n }\n }\n\n // rep(i,n)rep(j,n)rep(k,4){\n // printf(\" %d %d %d -> %d\\n\", i,j,k,m[i][j][k]);\n // }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,n)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n int ans = dp[(1<<n)-1];\n assert(ans<n);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3408, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_1582_2748285", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n/* 基本要素 */\ntypedef complex<double> Point;\ntypedef pair<Point, Point> Line;\ntypedef vector<Point> VP;\nconst double EPS = 1e-6; // 許容誤差^2\nconst double INF = 1e9;\n#define X real()\n#define Y imag()\n// #define LE(n,m) ((n) < (m) + EPS)\n#define LE(n,m) ((n) - (m) < EPS)\n// #define GE(n,m) ((n) + EPS > (m))\n#define GE(n,m) (EPS > (m) - (n))\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n\n// 内積 dot(a,b) = |a||b|cosθ\ndouble dot(Point a, Point b) {\n return a.X*b.X + a.Y*b.Y;\n}\n\n// 外積 cross(a,b) = |a||b|sinθ\ndouble cross(Point a, Point b) {\n return a.X*b.Y - a.Y*b.X;\n}\n\n// 点の進行方向\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b,c) > EPS) return +1; // counter clockwise\n if (cross(b,c) < -EPS) return -1; // clockwise\n if (dot(b,c) < -EPS) return +2; // c--a--b on line\n if (norm(b) < norm(c)) return -2; // a--b--c on line or a==b\n return 0; // a--c--b on line or a==c or b==c\n}\n\n// 点pの直線aへの射影点を返す\nPoint proj(Point a1, Point a2, Point p) {\n return a1 + dot(a2-a1, p-a1)/norm(a2-a1) * (a2-a1);\n}\n\ndouble distLP(Point a1, Point a2, Point p) {\n return abs(proj(a1, a2, p) - p);\n}\n\ndouble distLC(Point a1, Point a2, Point c, double r) {\n return max(distLP(a1, a2, c) - r, 0.0);\n}\n\nVP crosspointLC(Point a1, Point a2, Point c, double r) {\n VP ps;\n Point ft = proj(a1, a2, c);\n if(!GE(r*r,norm(ft-c))) return ps;\n Point dir = sqrt(max(r*r - norm(ft-c), 0.0)) / abs(a2-a1) * (a2-a1);\n ps.push_back(ft + dir);\n if (!EQ(r*r, norm(ft-c))) ps.push_back(ft - dir);\n return ps;\n}\n\n// 2円の共通接線。返される各直線に含まれる頂点は円との接点となる\nvector<Line> tangentLines(Point a, double ar, Point b, double br) {\n vector<Line> ls;\n double d = abs(b-a);\n rep (i,2) {\n double sin = (ar - (1-i*2)*br) / d;\n if (!LE(sin*sin, 1)) break;\n double cos = sqrt(max(1 - sin*sin, 0.0));\n rep (j,2) {\n Point n = (b-a) * Point(sin, (1-j*2)*cos) / d;\n ls.push_back(Line(a + ar*n, b + (1-i*2)*br*n));\n if (cos < EPS) break; // 重複する接線を無視(重複していいならこの行不要)\n }\n }\n return ls;\n}\n\nconst int N = 16;\nint dp[1<<N];\nint m[N][N][4];\n\nint main(){\n int n;\n cin >>n;\n\n vector<Point> c(n);\n vector<int> r(n);\n rep(i,n){\n int x,y;\n cin >>x >>y >>r[i];\n c[i] = Point(x,y);\n }\n\n rep(i,n)rep(j,i){\n vector<Line> seg = tangentLines(c[i],r[i],c[j],r[j]);\n int S = seg.size();\n assert(S==4);\n rep(k,S){\n Line l = seg[k];\n VP tmp = crosspointLC(l.fi,l.se,c[0],r[0]);\n\n if(tmp.size()>0){\n Point p = tmp[0];\n Point q = l.fi;\n if(ccw(p,q,l.se)==2) continue;\n\n int val = (1<<i)|(1<<j);\n rep(x,n)if(x!=i && x!=j){\n VP ump = crosspointLC(p,q,c[x],r[x]);\n if(ump.size()>0){\n // dbg(crosspointLC(p,q,c[x],r[x]).size());\n\n Point cc = ump[0];\n if(ccw(p,q,cc)!=2) val |= (1<<x);\n }\n }\n\n assert((val&1) == 1);\n m[i][j][k] = m[j][i][k] = val;\n }\n }\n }\n\n // rep(i,n)rep(j,n)rep(k,4){\n // printf(\" %d %d %d -> %d\\n\", i,j,k,m[i][j][k]);\n // }\n\n fill(dp,dp+(1<<N),10101);\n dp[1] = 0;\n rep(mask,1<<n){\n rep(i,n)rep(j,i)rep(k,4){\n int nmask = (mask|m[i][j][k]);\n dp[nmask] = min(dp[nmask], dp[mask]+1);\n }\n }\n\n int ans = dp[(1<<n)-1];\n assert(ans<n);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3428, "score_of_the_acc": -0.303, "final_rank": 1 } ]
aoj_1580_cpp
Barter Problem ナナツ君は、うまか棒を x 本、ふがしを y 本持っている。 n 人のお菓子交換人がいる。 それぞれの交換人 i は、 ナナツ君のうまか棒 a i 本と交換人のふがし b i 本 ナナツ君のふがし c i 本と交換人のブタメソ d i 個 のどちらか1つの方法で1回だけ交換してくれる。 この制約の下で、最終的に所持しているブタメソの個数を最大化せよ。 Input 入力は以下の形式で与えられる。 n x y a 1 b 1 c 1 d 1 a 2 b 2 c 2 d 2 ... a n b n c n d n 1行目に整数 n が与えられる。 2行目に整数 x , y が空白区切りで与えられる。 3行目から n +2行目に整数 a i , b i , c i , d i が空白区切りで与えられる。 Constraints 1 ≤ n ≤ 100 0 ≤ x , y ≤ 300 1 ≤ a i , b i , c i , d i ≤ 300 \(\sum_{i=1}^na_i\) ≤ 300, \(\sum_{i=1}^nb_i\) ≤ 300, \(\sum_{i=1}^nc_i\) ≤ 300, \(\sum_{i=1}^nd_i\) ≤ 300 Output 最終的にナナツ君が所持しているブタメソの個数の最大値を1行に出力する Sample Input 1 3 3 3 3 2 4 1 5 1 2 1 3 1 3 2 Sample Output 1 3 Sample Input 2 3 3 4 3 1 3 1 3 1 4 1 3 1 2 1 Sample Output 2 2 Sample Input 3 4 5 0 5 1 1 1 2 1 2 1 4 1 1 1 3 1 3 1 Sample Output 3 2 Sample Input 4 4 0 0 1 10 1 10 2 5 2 5 3 25 5 6 1 15 2 20 Sample Output 4 0
[ { "submission_id": "aoj_1580_5998864", "code_snippet": "#include <bits/stdc++.h>\n#define pt(x) cout << x << endl;\n#define Mid ((l + r) / 2)\n#define lson (rt << 1)\n#define rson (rt << 1 | 1)\nusing namespace std;\nconst int N = 609;\nint t[2][N * 2][N * 2], n, x, y;\nint ans = 0;\nsigned main()\n{\n\tmemset(t, 0xcf, sizeof(t));\n\tscanf(\"%d%d%d\", &n, &x, &y);\n\tt[0][x + N][y + N] = 0;\n\tfor(int i = 1; i <= n; i++) {\n\t\tint a, b, c, d;\n\t\tscanf(\"%d%d%d%d\", &a, &b, &c, &d);\n\t\tint (&f)[N * 2][N * 2] = t[i & 1];\n\t\tint (&g)[N * 2][N * 2] = t[(i - 1) & 1];\n//\t\tmemset(f, 0xcf, sizeof(f));\n\t\tmemcpy(f, g, sizeof(g));\n\t\tfor(int j = 0; j < 2 * N; j++) {\n\t\t\tfor(int k = 0; k < 2 * N; k++) if(g[j][k] != 0xcfcfcfcf){\n\t\t\t\tif(a <= j && k + b < 2 * N) {\n\t\t\t\t\tf[j - a][k + b] = max(f[j - a][k + b], g[j][k]);\n\t\t\t\t}\n\t\t\t\tif(c <= k) {\n\t\t\t\t\tf[j][k - c] = max(f[j][k - c], g[j][k] + d);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(i == n) {\n\t\t\tfor(int j = N; j < 2 * N; j++) \n\t\t\t\tfor(int k = N; k < 2 * N; k++)\n\t\t\t\t\tans = max(ans, f[j][k]);\n\t\t}\n\t\t\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}\n/*\n11\n7 50\n20 26 4 1\n21 15 15 17\n12 15 12 22\n26 25 11 9\n11 24 10 3\n25 9 14 18\n7 10 18 11\n5 21 11 20\n14 12 18 1\n9 26 11 8\n4 1 1 18\n\n*/", "accuracy": 1, "time_ms": 180, "memory_kb": 14876, "score_of_the_acc": -1.0342, "final_rank": 11 }, { "submission_id": "aoj_1580_5998856", "code_snippet": "#include <bits/stdc++.h>\n#define pt(x) cout << x << endl;\n#define Mid ((l + r) / 2)\n#define lson (rt << 1)\n#define rson (rt << 1 | 1)\nusing namespace std;\nconst int N = 609;\nint t[2][N * 2][N * 2], n, x, y;\nint ans = 0;\nsigned main()\n{\n\tmemset(t, 0xcf, sizeof(t));\n\tscanf(\"%d%d%d\", &n, &x, &y);\n\tt[0][x + N][y + N] = 0;\n\tfor(int i = 1; i <= n; i++) {\n\t\tint a, b, c, d;\n\t\tscanf(\"%d%d%d%d\", &a, &b, &c, &d);\n\t\tint (&f)[N * 2][N * 2] = t[i & 1];\n\t\tint (&g)[N * 2][N * 2] = t[(i - 1) & 1];\n\t\tmemset(f, 0xcf, sizeof(f));\n\t\tfor(int j = 0; j < 2 * N; j++) {\n\t\t\tfor(int k = 0; k < 2 * N; k++) if(g[j][k] != 0xcfcfcfcf){\n\t\t\t\tif(a <= j && k + b < 2 * N) {\n\t\t\t\t\tf[j - a][k + b] = max(f[j - a][k + b], g[j][k]);\n\t\t\t\t}\n\t\t\t\tif(c <= k) {\n\t\t\t\t\tf[j][k - c] = max(f[j][k - c], g[j][k] + d);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor(int j = N; j < 2 * N; j++) \n\t\t\tfor(int k = N; k < 2 * N; k++)\n\t\t\t\tans = max(ans, f[j][k]);\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "accuracy": 0.09523809523809523, "time_ms": 10, "memory_kb": 14808, "score_of_the_acc": -0.034, "final_rank": 14 }, { "submission_id": "aoj_1580_5998853", "code_snippet": "#include <bits/stdc++.h>\n#define pt(x) cout << x << endl;\n#define Mid ((l + r) / 2)\n#define lson (rt << 1)\n#define rson (rt << 1 | 1)\nusing namespace std;\nconst int N = 609;\nint t[2][N * 2][N * 2], n, x, y;\nint ans = 0;\nsigned main()\n{\n\tmemset(t, 0xcf, sizeof(t));\n\tscanf(\"%d%d%d\", &n, &x, &y);\n\tt[0][x + N][y + N] = 0;\n\tfor(int i = 1; i <= n; i++) {\n\t\tint a, b, c, d;\n\t\tscanf(\"%d%d%d%d\", &a, &b, &c, &d);\n\t\tint (&f)[N * 2][N * 2] = t[i & 1];\n\t\tint (&g)[N * 2][N * 2] = t[(i - 1) & 1];\n\t\tmemset(f, 0xcf, sizeof(f));\n\t\tfor(int j = 0; j < 2 * N; j++) {\n\t\t\tfor(int k = 0; k < 2 * N; k++) if(g[j][k] != 0xcfcfcfcf){\n\t\t\t\tif(a <= j && k + b < 2 * N) {\n\t\t\t\t\tf[j - a][k + b] = max(f[j - a][k + b], g[j][k]);\n\t\t\t\t\tif(j - a >= N && k + b >= N) ans = max(ans, f[j - a][k + b]);\n\t\t\t\t}\n\t\t\t\tif(c <= k) {\n\t\t\t\t\tf[j][k - c] = max(f[j][k - c], g[j][k] + d);\n\t\t\t\t\tif(k - c >= N && j >= N) ans = max(ans, f[j][k - c]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\", ans);\n\treturn 0;\n}", "accuracy": 0.09523809523809523, "time_ms": 10, "memory_kb": 14808, "score_of_the_acc": -0.034, "final_rank": 14 }, { "submission_id": "aoj_1580_4146608", "code_snippet": "// ふがしは初期300+交換+300で最大600本持つ可能性があり、\n// これを素直に実装すれば301(うまか[0,300])*901(ふがし[-300,600])\n// ところが、sum(c_i)が300以下なので、ふがしを301本以上持つのは無意味\n// よってふがしを301以上持つ状態は無いものとして\n// 301(うまか[0,300])*601(ふがし[-300,300])で実装してよい\n#include<iostream>\n#include<vector>\nusing namespace std;\n\nvector<vector<int>> dp(301, vector<int>(601, -10000));\n\nint main(){\n int n, x, y;\n scanf(\"%d\", &n);\n scanf(\"%d %d\", &x, &y);\n dp[x][300+y] = 0;\n for(int i = 0; i < n; i++){\n vector<vector<int>> ndp = dp;\n int a, b, c, d;\n scanf(\"%d %d %d %d\", &a, &b, &c, &d);\n for(int j = 0; j <= 300; j++){\n for(int k = 0; k <= 600; k++){\n if(dp[j][k] < 0) continue;\n if(j >= a && k+b <= 600) ndp[j-a][k+b] = max(ndp[j-a][k+b], dp[j][k]);\n if(k >= c) ndp[j][k-c] = max(ndp[j][k-c], dp[j][k]+d);\n }\n }\n dp = ndp;\n }\n int ans = 0;\n for(int i = 0; i <= 300; i++){\n for(int j = 300; j <= 600; j++){\n ans = max(ans, dp[i][j]);\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4388, "score_of_the_acc": -0.119, "final_rank": 1 }, { "submission_id": "aoj_1580_4146587", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n\nvector<vector<int>> dp(301, vector<int>(901, -10000));\n\nint main(){\n int n, x, y;\n scanf(\"%d\", &n);\n scanf(\"%d %d\", &x, &y);\n dp[x][300+y] = 0;\n for(int i = 0; i < n; i++){\n vector<vector<int>> ndp = dp;\n int a, b, c, d;\n scanf(\"%d %d %d %d\", &a, &b, &c, &d);\n for(int j = 0; j <= 300; j++){\n for(int k = 0; k <= 900; k++){\n if(dp[j][k] < 0) continue;\n if(j >= a) ndp[j-a][k+b] = max(ndp[j-a][k+b], dp[j][k]);\n if(k >= c) ndp[j][k-c] = max(ndp[j][k-c], dp[j][k]+d);\n }\n }\n dp = ndp;\n }\n int ans = 0;\n for(int i = 0; i <= 300; i++){\n for(int j = 300; j <= 900; j++){\n ans = max(ans, dp[i][j]);\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5008, "score_of_the_acc": -0.1798, "final_rank": 5 }, { "submission_id": "aoj_1580_4146583", "code_snippet": "// コピー含め8.1e7で間に合うはず\n#include<iostream>\n#include<vector>\nusing namespace std;\n\nvector<vector<int>> dp(901, vector<int>(901, -10000));\n\nint main(){\n int n, x, y;\n scanf(\"%d\", &n);\n scanf(\"%d %d\", &x, &y);\n dp[300+x][300+y] = 0;\n for(int i = 0; i < n; i++){\n vector<vector<int>> ndp = dp;\n int a, b, c, d;\n scanf(\"%d %d %d %d\", &a, &b, &c, &d);\n for(int j = 0; j <= 900; j++){\n for(int k = 0; k <= 900; k++){\n if(dp[j][k] < 0) continue;\n if(j >= a) ndp[j-a][k+b] = max(ndp[j-a][k+b], dp[j][k]);\n if(k >= c) ndp[j][k-c] = max(ndp[j][k-c], dp[j][k]+d);\n }\n }\n dp = ndp;\n }\n int ans = 0;\n for(int i = 300; i <= 900; i++){\n for(int j = 300; j <= 900; j++){\n ans = max(ans, dp[i][j]);\n }\n }\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 9252, "score_of_the_acc": -0.7813, "final_rank": 8 }, { "submission_id": "aoj_1580_3953579", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <numeric>\n\n#define REP(i, a, b) for (int i = int(a); i < int(b); i++)\nusing namespace std;\ntypedef long long int ll;\n\n// clang-format off\n#ifdef _DEBUG_\n#define dump(...) do{ cerr << __LINE__ << \":\\t\" << #__VA_ARGS__ << \" = \"; PPPPP(__VA_ARGS__); cerr << endl; } while(false)\ntemplate<typename T> void PPPPP(T t) { cerr << t; }\ntemplate<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << \", \"; PPPPP(s...); }\n#else\n#define dump(...)\n#endif\ntemplate<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }\ntemplate<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }\ntemplate<typename T>\nbool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false;}\ntemplate<typename T>\nbool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false;}\n// clang-format on\n\nconst int BS = 310;\nint dp[110][BS][BS * 3];\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n, x, y;\n cin >> n >> x >> y;\n vector<int> a(n), b(n), c(n), d(n);\n REP(i, 0, n) {\n cin >> a[i] >> b[i] >> c[i] >> d[i];\n }\n REP(i, 0, 110) {\n REP(j, 0, BS) {\n REP(k, 0, BS * 3) {\n dp[i][j][k] = -1;\n }\n }\n }\n\n dp[0][x][y + BS] = 0;\n\n REP(i, 0, n) {\n REP(j, 0, BS) {\n REP(k, 0, BS * 3) {\n if (dp[i][j][k] == -1) continue;\n if (j >= a[i] && k + b[i] < BS * 3) {\n chmax(dp[i + 1][j - a[i]][k + b[i]], dp[i][j][k]);\n }\n if (k >= c[i]) {\n chmax(dp[i + 1][j][k - c[i]], dp[i][j][k] + d[i]);\n }\n chmax(dp[i + 1][j][k], dp[i][j][k]);\n }\n }\n }\n int ans = 0;\n REP(i, 0, BS) {\n REP(j, BS, BS * 3) {\n chmax(ans, dp[n][i][j]);\n }\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 127092, "score_of_the_acc": -0.7383, "final_rank": 7 }, { "submission_id": "aoj_1580_3953575", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <numeric>\n\n#define REP(i, a, b) for (int i = int(a); i < int(b); i++)\nusing namespace std;\ntypedef long long int ll;\n\n// clang-format off\n#ifdef _DEBUG_\n#define dump(...) do{ cerr << __LINE__ << \":\\t\" << #__VA_ARGS__ << \" = \"; PPPPP(__VA_ARGS__); cerr << endl; } while(false)\ntemplate<typename T> void PPPPP(T t) { cerr << t; }\ntemplate<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << \", \"; PPPPP(s...); }\n#else\n#define dump(...)\n#endif\ntemplate<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }\ntemplate<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }\ntemplate<typename T>\nbool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false;}\ntemplate<typename T>\nbool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false;}\n// clang-format on\n\nconst int BS = 310;\nint dp[110][BS][BS * 3];\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int n, x, y;\n cin >> n >> x >> y;\n vector<int> a(n), b(n), c(n), d(n);\n REP(i, 0, n) {\n cin >> a[i] >> b[i] >> c[i] >> d[i];\n }\n REP(i, 0, 110) {\n REP(j, 0, BS) {\n REP(k, 0, BS * 3) {\n dp[i][j][k] = -1;\n }\n }\n }\n\n dp[0][x][y + BS] = 0;\n\n REP(i, 0, n) {\n REP(j, 0, BS) {\n REP(k, 0, BS * 3) {\n if (dp[i][j][k] == -1) continue;\n if (j >= a[i]) {\n chmax(dp[i + 1][j - a[i]][k + b[i]], dp[i][j][k]);\n }\n chmax(dp[i + 1][j][k - c[i]], dp[i][j][k] + d[i]);\n }\n }\n }\n int ans = 0;\n REP(i, 0, BS) {\n REP(j, BS, BS * 3) {\n chmax(ans, dp[n][i][j]);\n }\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 0.09523809523809523, "time_ms": 30, "memory_kb": 126956, "score_of_the_acc": -0.5026, "final_rank": 17 }, { "submission_id": "aoj_1580_3804760", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1580.cc: Barter\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100;\nconst int MAX_X = 300;\nconst int MAX_Y = 300;\n\n/* typedef */\n\n/* global variables */\n\nint as[MAX_N], bs[MAX_N], cs[MAX_N], ds[MAX_N];\nint dp[2][MAX_X + 1][MAX_X + MAX_Y + 1];\n\n/* subroutines */\n\ninline void setmax(int &a, int b) { if (a < b) a = b; }\n\n/* main */\n\nint main() {\n int n, x, y;\n scanf(\"%d%d%d\", &n, &x, &y);\n\n int bsum = 0, csum = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d%d%d%d\", as + i, bs + i, cs + i, ds + i);\n bsum += bs[i], csum += cs[i];\n }\n //printf(\"bsum=%d, csum=%d\\n\", bsum, csum);\n\n memset(dp, -1, sizeof(dp));\n int z = (bsum > y) ? bsum - y : 0;\n int maxy = z + y + bsum;\n\n dp[0][x][z + y] = 0;\n int cur = 0, nxt = 1;\n\n for (int i = 0; i < n; i++) {\n memcpy(dp[nxt], dp[cur], sizeof(dp[cur]));\n for (int j = 0; j <= x; j++)\n for (int k = 0; k <= maxy; k++)\n\tif (dp[cur][j][k] >= 0) {\n\t if (j >= as[i])\n\t setmax(dp[nxt][j - as[i]][k + bs[i]], dp[cur][j][k]);\n\t if (k >= cs[i])\n\t setmax(dp[nxt][j][k - cs[i]], dp[cur][j][k] + ds[i]);\n\t}\n\n cur ^= 1, nxt ^= 1;\n }\n\n int maxd = 0;\n for (int j = 0; j <= x; j++)\n for (int k = z; k <= maxy; k++)\n setmax(maxd, dp[cur][j][k]);\n\n printf(\"%d\\n\", maxd);\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4704, "score_of_the_acc": -0.1788, "final_rank": 4 }, { "submission_id": "aoj_1580_3090221", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst int INF = 1234567;\n\nconst int N = 303;\nconst int M = 634;\nint dp[N][M];\nint nx[N][M];\n\nint main(){\n int n,x,y;\n cin >>n >>x >>y;\n\n rep(j,N)rep(k,M) dp[j][k] = -INF;\n dp[x][N+y] = 0;\n\n rep(i,n){\n int a,b,c,d;\n cin >>a >>b >>c >>d;\n\n rep(j,N)rep(k,M) nx[j][k] = dp[j][k];\n rep(j,N)rep(k,M){\n if(j-a>=0 && k+b<M) nx[j-a][k+b] = max(nx[j-a][k+b], dp[j][k]);\n if(k-c>=0) nx[j][k-c] = max(nx[j][k-c], dp[j][k]+d);\n }\n\n rep(j,N)rep(k,M) dp[j][k] = nx[j][k];\n }\n\n int ans = 0;\n rep(i,N)for(int j=N; j<M; ++j) ans = max(ans, dp[i][j]);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 4624, "score_of_the_acc": -0.1786, "final_rank": 3 }, { "submission_id": "aoj_1580_3010793", "code_snippet": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nint dp[2][1200][1200];\nint main(){\n memset(dp,-1,sizeof(dp));\n int x,y,n;\n cin>>n>>x>>y;\n dp[0][x+600][y+600]=0;\n r(i,n){\n int a,b,c,d;\n cin>>a>>b>>c>>d;\n r(A,1200)r(B,1200){\n if(~dp[i%2][A][B]){\n dp[(i+1)%2][A][B]=max(dp[(i+1)%2][A][B],dp[i%2][A][B]);\n dp[(i+1)%2][A-a][B+b]=max(dp[(i+1)%2][A-a][B+b],dp[i%2][A][B]);\n dp[(i+1)%2][A][B-c]=max(dp[(i+1)%2][A][B-c],dp[i%2][A][B]+d);\n }\n }\n r(j,1200)r(k,1200)dp[i%2][j][k]=-1;\n }\n int ans=0;\n r(i,1200)r(j,1200){\n if(i>=600&&j>=600){\n ans=max(ans,dp[n%2][i][j]);\n }\n }\n cout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 14356, "score_of_the_acc": -0.7973, "final_rank": 10 }, { "submission_id": "aoj_1580_3010783", "code_snippet": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\nusing namespace std;\nint dp[2][1200][1200];\nint main(){\n memset(dp,-1,sizeof(dp));\n int x,y,n;\n cin>>n>>x>>y;\n dp[0][x+600][y+600]=0;\n r(i,n){\n int a,b,c,d;\n cin>>a>>b>>c>>d;\n r(A,1200)r(B,1200){\n if(~dp[i%2][A][B]){\n dp[(i+1)%2][A-a][B+b]=max(dp[(i+1)%2][A-a][B+b],dp[i%2][A][B]);\n dp[(i+1)%2][A][B-c]=max(dp[(i+1)%2][A][B-c],dp[i%2][A][B]+d);\n }\n }\n r(j,1200)r(k,1200)dp[i%2][j][k]=-1;\n }\n int ans=0;\n r(i,1200)r(j,1200){\n if(i>=600&&j>=600){\n ans=max(ans,dp[n%2][i][j]);\n }\n }\n cout<<ans<<endl;\n}", "accuracy": 0.09523809523809523, "time_ms": 30, "memory_kb": 14356, "score_of_the_acc": -0.1502, "final_rank": 16 }, { "submission_id": "aoj_1580_2747971", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n;\nint x,y;\nint dp[101][301][1201];\nint a[101],b[101],c[101],d[110];\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tscanf(\"%d%d\",&x,&y);\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%d%d%d%d\",&a[i],&b[i],&c[i],&d[i]);\n\t}\n\tmemset(dp,-1,sizeof(dp));\n\tdp[0][x][y+300]=0;\n\tint ans=0;\n\tfor(int i=0;i<n;i++){\n\t\tfor(int j=0;j<=300;j++){\n\t\t\tfor(int k=0;k<=900;k++){\n\t\t\t\tif(dp[i][j][k]>=0){\n\t\t\t\t\tdp[i+1][j][k]=max(dp[i][j][k],dp[i+1][j][k]);\n\t\t\t\t\tif(j>=a[i]){\n\t\t\t\t\t\tdp[i+1][j-a[i]][k+b[i]]=max(dp[i][j][k],dp[i+1][j-a[i]][k+b[i]]);\n\t\t\t\t\t\tif(k+b[i]>=300)ans=max(ans,dp[i+1][j-a[i]][k+b[i]]);\n\t\t\t\t\t}\n\t\t\t\t\tif(k>=c[i]){\n\t\t\t\t\t\tdp[i+1][j][k-c[i]]=max(dp[i][j][k]+d[i],dp[i+1][j][k-c[i]]);\n\t\t\t\t\t\tif(k-c[i]>=300)ans=max(ans,dp[i+1][j][k-c[i]]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 145840, "score_of_the_acc": -0.797, "final_rank": 9 }, { "submission_id": "aoj_1580_2747969", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n;\nint x,y;\nint dp[101][301][901];\nint a[101],b[101],c[101],d[110];\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tscanf(\"%d%d\",&x,&y);\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%d%d%d%d\",&a[i],&b[i],&c[i],&d[i]);\n\t}\n\tmemset(dp,-1,sizeof(dp));\n\tdp[0][x][y+300]=0;\n\tint ans=0;\n\tfor(int i=0;i<n;i++){\n\t\tfor(int j=0;j<=300;j++){\n\t\t\tfor(int k=0;k<=900;k++){\n\t\t\t\tif(dp[i][j][k]>=0){\n\t\t\t\t\tdp[i+1][j][k]=max(dp[i][j][k],dp[i+1][j][k]);\n\t\t\t\t\tif(j>=a[i]){\n\t\t\t\t\t\tdp[i+1][j-a[i]][k+b[i]]=max(dp[i][j][k],dp[i+1][j-a[i]][k+b[i]]);\n\t\t\t\t\t}\n\t\t\t\t\tif(k>=c[i]){\n\t\t\t\t\t\tdp[i+1][j][k-c[i]]=max(dp[i][j][k]+d[i],dp[i+1][j][k-c[i]]);\n\t\t\t\t\t\tif(k-c[i]>=300)ans=max(ans,dp[i+1][j][k-c[i]]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.047619047619047616, "time_ms": 30, "memory_kb": 110172, "score_of_the_acc": -0.4501, "final_rank": 19 }, { "submission_id": "aoj_1580_2747871", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define mp make_pair\n#define pb push_back\n#define all(x) begin(x),end(x)\n#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))\n#define fi first\n#define se second\n#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\nvoid _dbg(string){cerr<<endl;}\ntemplate<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<\" = \"<<h<<\", \";_dbg(s.substr(l+1),t...);}\ntemplate<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n#define chmax(a,b) a = max(a,b)\n\nint dp[2][305][1000];\nint a[105], b[105], c[105], d[105];\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0); cout.tie(0);\n\n int n,x,y;\n cin>>n>>x>>y;\n rep(i,n) cin>>a[i]>>b[i]>>c[i]>>d[i];\n\n auto cur = dp[0];\n auto nxt = dp[1];\n\n const int INF = 12345678;\n fill(cur[0], cur[303], -INF);\n\n cur[x][y+300] = 0;\n rep(i,n){\n fill(nxt[0], nxt[303], -INF);\n rep(j,301) for(int k=-300; k<=600; k++) if(cur[j][k+300]>=0){\n int tmp = cur[j][k+300];\n chmax(nxt[j][k+300], tmp);\n if(j>=a[i]) chmax(nxt[j-a[i]][k+300+b[i]], tmp);\n chmax(nxt[j][k+300-c[i]], tmp + d[i]);\n }\n swap(cur, nxt);\n }\n\n int ans = 0;\n rep(i,301) rep(j,601) ans = max(ans, cur[i][j+300]);\n\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 5596, "score_of_the_acc": -0.1816, "final_rank": 6 }, { "submission_id": "aoj_1580_2747862", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n;\nint x,y;\nint dp[101][301][601];\nint a[101],b[101],c[101],d[110];\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tscanf(\"%d%d\",&x,&y);\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%d%d%d%d\",&a[i],&b[i],&c[i],&d[i]);\n\t}\n\tmemset(dp,-1,sizeof(dp));\n\tdp[0][x][y]=0;\n\tint ans=0;\n\tfor(int i=0;i<n;i++){\n\t\tfor(int j=0;j<=300;j++){\n\t\t\tfor(int k=0;k<=600;k++){\n\t\t\t\tif(dp[i][j][k]>=0){\n\t\t\t\t\tdp[i+1][j][k]=max(dp[i][j][k],dp[i+1][j][k]);\n\t\t\t\t\tif(j>=a[i]){\n\t\t\t\t\t\tdp[i+1][j-a[i]][k+b[i]]=max(dp[i][j][k],dp[i+1][j-a[i]][k+b[i]]);\n\t\t\t\t\t}\n\t\t\t\t\tif(k>=c[i]){\n\t\t\t\t\t\tdp[i+1][j][k-c[i]]=max(dp[i][j][k]+d[i],dp[i+1][j][k-c[i]]);\n\t\t\t\t\t\tans=max(ans,dp[i+1][j][k-c[i]]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.047619047619047616, "time_ms": 20, "memory_kb": 74588, "score_of_the_acc": -0.2799, "final_rank": 18 }, { "submission_id": "aoj_1580_2747857", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n;\nint x,y;\nint dp[101][601][601];\nint a[101],b[101],c[101],d[110];\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tscanf(\"%d%d\",&x,&y);\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%d%d%d%d\",&a[i],&b[i],&c[i],&d[i]);\n\t}\n\tmemset(dp,-1,sizeof(dp));\n\tdp[0][x][y]=0;\n\tint ans=0;\n\tfor(int i=0;i<n;i++){\n\t\tfor(int j=0;j<=600;j++){\n\t\t\tfor(int k=0;k<=600;k++){\n\t\t\t\tif(dp[i][j][k]>=0){\n\t\t\t\t\tif(j>=a[i]){\n\t\t\t\t\t\tdp[i+1][j-a[i]][k+b[i]]=max(dp[i][j][k],dp[i+1][j-a[i]][k+b[i]]);\n\t\t\t\t\t}\n\t\t\t\t\tif(k>=c[i]){\n\t\t\t\t\t\tdp[i+1][j][k-c[i]]=max(dp[i][j][k]+d[i],dp[i+1][j][k-c[i]]);\n\t\t\t\t\t\tans=max(ans,dp[i+1][j][k-c[i]]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 0.047619047619047616, "time_ms": 40, "memory_kb": 145684, "score_of_the_acc": -0.6201, "final_rank": 20 }, { "submission_id": "aoj_1580_2692810", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nint a[100],b[100],c[100],d[100];\nint dp[101][901][901];\n\n\nint main(){\n\n\tint N;\n\tscanf(\"%d\",&N);\n\n\tint first_a,first_b;\n\tscanf(\"%d %d\",&first_a,&first_b);\n\n\tfor(int i = 0; i < N; i++){\n\t\tscanf(\"%d %d %d %d\",&a[i],&b[i],&c[i],&d[i]);\n\t}\n\n\tfor(int i = 0; i <= N; i++){\n\t\tfor(int k = 0; k <= 900; k++){\n\t\t\tfor(int p = 0; p <= 900; p++)dp[i][k][p] = -1;\n\t\t}\n\t}\n\n\tint BASE = 300;\n\n\tdp[0][BASE+first_a][BASE+first_b] = 0;\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k <= 900; k++){\n\t\t\tfor(int p = 0; p <= 900; p++){\n\t\t\t\tif(dp[i][k][p] == -1)continue;\n\n\t\t\t\tdp[i+1][k][p] = max(dp[i+1][k][p],dp[i][k][p]);\n\n\t\t\t\tif(k >= a[i]){\n\t\t\t\t\tdp[i+1][k-a[i]][p+b[i]] = max(dp[i+1][k-a[i]][p+b[i]],dp[i][k][p]);\n\t\t\t\t}\n\n\t\t\t\tif(p >= c[i]){\n\t\t\t\t\tdp[i+1][k][p-c[i]] = max(dp[i+1][k][p-c[i]],dp[i][k][p]+d[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tint ans = 0;\n\tfor(int k = BASE; k <= 900; k++){\n\t\tfor(int p = BASE; p <= 900; p++)ans = max(ans,dp[N][k][p]);\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 323468, "score_of_the_acc": -1.7647, "final_rank": 12 }, { "submission_id": "aoj_1580_2610339", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n\nint dp[301][301][901];\nstruct exc {\n\tint a;\n\tint b;\n\tint c;\n\tint d;\n};\nint main() { \n\tint N; cin >> N;\n\tint X, Y; cin >> X >> Y;\n\tfor (int i = 0; i < 301; ++i) {\n\t\tfor (int j = 0; j < 301; ++j) {\n\t\t\tfor (int k = 0; k < 901; ++k) {\n\t\t\t\tdp[i][j][k] = -1e9;\n\t\t\t}\n\t\t}\n\t}\n\tdp[0][X][Y + 300] = 0;\n\tvector<exc>es;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint a, b, c, d; cin >> a >> b >> c >> d;\n\t\tes.push_back(exc{ a,b,c,d });\n\t}\n\tfor (int i = 0; i < N; ++i) {\n\t\tfor (int j = 0; j < 301; ++j) {\n\t\t\tfor (int k = 0; k < 901; ++k) {\n\t\t\t\texc ex(es[i]);\n\t\t\t\tdp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);\n\t\t\t\t{\n\t\t\t\t\tint nj = j - ex.a;\n\t\t\t\t\tint nk = k+ex.b;\n\t\t\t\t\tif (nj < 0) {\n\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (nk > 900)nk = 900;\n\t\t\t\t\t\tdp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i][j][k]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t{\n\t\t\t\t\tint nk = k - ex.c;\n\t\t\t\t\tif (nk < 0) {\n\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tdp[i + 1][j][nk] = max(dp[i + 1][j][nk], dp[i][j][k] + ex.d);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint ans = 0;\n\tfor (int j = 0; j <= 300; ++j) {\n\t\tfor (int k = 300; k <= 900; ++k) {\n\t\t\tans = max(ans, dp[N][j][k]);\n\t\t}\n\t}\n\tcout << ans << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 322028, "score_of_the_acc": -1.9367, "final_rank": 13 }, { "submission_id": "aoj_1580_2579295", "code_snippet": "#include <iostream>\n#include <vector>\n\nconstexpr int MAXA = 350;\nconstexpr int MAXB = 700;\nconstexpr int OFFSET = 350;\n\nvoid chmax(int &a, int b)\n{\n a = (a < b ? b : a);\n}\n\nint main()\n{\n int n, x, y;\n std::cin >> n >> x >> y;\n \n std::vector<int> a(n), b(n), c(n), d(n);\n for (int i = 0; i < n; i++) {\n std::cin >> a[i] >> b[i] >> c[i] >> d[i];\n }\n \n std::vector<std::vector<int>> dp(MAXA, std::vector<int>(MAXB, -1));\n dp[x][y + OFFSET] = 0;\n \n for (int i = 0; i < n; i++) {\n for (int j = 0; j < MAXA; j++) {\n for (int k = 0; k < MAXB; k++) {\n if (dp[j][k] == -1) { continue; }\n\n if (j - a[i] >= 0 && k + b[i] < MAXB) {\n chmax(dp[j - a[i]][k + b[i]], dp[j][k]);\n }\n\n if (k - c[i] >= 0) {\n chmax(dp[j][k - c[i]], dp[j][k] + d[i]);\n }\n }\n }\n }\n\n int ret = 0;\n for (int i = 0; i < MAXA; i++) {\n for (int j = OFFSET; j < MAXB; j++) {\n chmax(ret, dp[i][j]);\n }\n }\n \n std::cout << ret << std::endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3948, "score_of_the_acc": -0.1765, "final_rank": 2 } ]
aoj_1581_cpp
Reflection Warp Machine Problem とある宇宙では、2次元の格子点上に n 個の星が存在し、宇宙人達はReflection Warp Machineを使い、星間を移動している。 この装置は、任意の位置、角度で直線を引くことができる。 この直線を対称軸として、現在いる座標から線対称の座標に移動することができる。 ただし、星が存在しない座標に移動することはできない。 一度引いた直線は何度でも利用できる。 現在、( x 0 , y 0 )の星にいるある宇宙人はすべての星を訪れたいと考えている。 星は好きな順番に訪れることができる。 すべての星を訪れるのに最小で何本の直線を引く必要があるか求めよ。 Input n x 0 y 0 ... x n−1 y n−1 入力は全て整数で与えられる。 1行目に n が与えられる。 2行目以降 n 行に i 番目の星の座標( x i , y i )が空白区切りで与えられる。 Constraints 2 ≤ n ≤ 8 −100 ≤ x i , y i ≤ 100 ( x i , y i ) ≠ ( x j , y j ) ( i ≠ j ) Output すべての星を訪れるために必要な最小の直線の数を1行に出力せよ。 Sample Input 1 3 0 0 0 1 1 0 Sample Output 1 2 Sample Input 2 4 0 0 0 1 0 2 0 3 Sample Output 2 2
[ { "submission_id": "aoj_1581_3806524", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1581.cc: Reflection Warp Machine\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 8;\nconst int MAX_M = MAX_N * (MAX_N - 1) / 2;\n\n/* typedef */\n\n// typedefs\n\ntypedef vector<int> vi;\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\n\ntemplate <typename T>\nstruct Pt {\n T x, y;\n\n Pt() {}\n Pt(T _x, T _y) : x(_x), y(_y) {}\n Pt(const Pt<T> &pt) : x(pt.x), y(pt.y) {}\n\n bool operator==(const Pt<T> pt) const { return x == pt.x && y == pt.y; }\n Pt<T> operator+(const Pt<T> pt) const { return Pt<T>(x + pt.x, y + pt.y); }\n Pt<T> operator-() const { return Pt<T>(-x, -y); }\n Pt<T> operator-(const Pt<T> pt) const { return Pt<T>(x - pt.x, y - pt.y); }\n Pt<T> operator*(T t) const { return Pt<T>(x * t, y * t); }\n Pt<T> operator/(T t) const { return Pt<T>(x / t, y / t); }\n T dot(Pt<T> v) const { return x * v.x + y * v.y; }\n T cross(Pt<T> v) const { return x * v.y - y * v.x; }\n T d2() { return x * x + y * y; }\n\n Pt<T> rot90() { return Pt<T>(-y, x); }\n\n bool operator<(const Pt& pt) const {\n return x < pt.x || (x == pt.x && y < pt.y);\n }\n\n void print() { printf(\"(%d,%d)\", x, y); }\n};\n\ntypedef Pt<int> pt;\n\nstruct UFT {\n int links[MAX_N], ranks[MAX_N], sizes[MAX_N];\n UFT() {}\n\n void init(int n) {\n for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;\n }\n\n int root(int i) {\n int i0 = i;\n while (links[i0] != i0) i0 = links[i0];\n return (links[i] = i0);\n }\n\n int rank(int i) { return ranks[root(i)]; }\n int size(int i) { return sizes[root(i)]; }\n bool same(int i, int j) { return root(i) == root(j); }\n\n int merge(int i0, int i1) {\n int r0 = root(i0), r1 = root(i1), mr;\n if (r0 == r1) return r0;\n if (ranks[r0] == ranks[r1]) {\n links[r1] = r0;\n sizes[r0] += sizes[r1];\n ranks[r0]++;\n mr = r0;\n }\n else if (ranks[r0] > ranks[r1]) {\n links[r1] = r0;\n sizes[r0] += sizes[r1];\n mr = r0;\n }\n else {\n links[r0] = r1;\n sizes[r1] += sizes[r0];\n mr = r1;\n }\n return mr;\n }\n};\n\n/* global variables */\n\npt ps[MAX_N], cs[MAX_M], vs[MAX_M];\nvpii ess[MAX_M];\nUFT uft;\n\n/* subroutines */\n\nvoid comb(int u, int k, int n, int bits, vi &cs) {\n if (k == 0) {\n cs.push_back(bits);\n return;\n }\n if (u >= n) return;\n\n for (int i = u, bi = 1 << i; i < n; i++, bi <<= 1)\n comb(i + 1, k - 1, n, bits | bi, cs);\n}\n\n/* main */\n\nint main() {\n int n;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++)\n scanf(\"%d%d\", &ps[i].x, &ps[i].y), ps[i] = ps[i] * 2;\n\n int m = 0;\n for (int i = 0; i < n; i++)\n for (int j = i + 1; j < n; j++) {\n pt c((ps[i] + ps[j]) / 2), v((ps[j] - ps[i]).rot90());\n int k = 0;\n for (; k < m; k++)\n\tif (vs[k].cross(v) == 0 && vs[k].cross(c - cs[k]) == 0)\n\t break;\n if (k < m)\n\tess[k].push_back(pii(i, j));\n else {\n\tcs[m] = c, vs[m] = v;\n\tess[m++].push_back(pii(i, j));\n }\n }\n //printf(\"m=%d\\n\", m);\n\n for (int l = 1; l <= n - 2; l++) {\n vi cs;\n comb(0, l, m, 0, cs);\n\n for (vi::iterator vit = cs.begin(); vit != cs.end(); vit++) {\n int &bits = *vit;\n uft.init(n);\n\n for (int i = 0, bi = 1; i < m; i++, bi <<= 1)\n\tif (bits & bi) {\n\t vpii &es = ess[i];\n\t for (vpii::iterator eit = es.begin(); eit != es.end(); eit++)\n\t uft.merge(eit->first, eit->second);\n\t}\n\n if (uft.size(0) == n) {\n\tprintf(\"%d\\n\", l);\n\treturn 0;\n }\n }\n }\n\n printf(\"%d\\n\", n - 1);\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4980, "score_of_the_acc": -0.1029, "final_rank": 4 }, { "submission_id": "aoj_1581_3804012", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 8\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\tPoint p[2];\n};\n\nint N;\nint boss[8],height[8],table[30];\nint REFLECT[8][30];\ndouble NUM = 10000;\nPoint info[8];\nvector<Line> LINE;\n\nint get_boss(int id){\n\tif(boss[id] == id)return id;\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]);\n\t}\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\tif(boss_x == boss_y)return;\n\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\theight[x]++;\n\t}\n}\n\nvoid init(){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tboss[i] = i;\n\t\theight[i] = 0;\n\t}\n}\n\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool equals(Point A,Point B){\n\n\treturn abs(A-B) < EPS;\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[0].y-A.p[1].y)/(A.p[0].x-A.p[1].x);\n\t}\n}\n\n//点Aと点Bの垂直二等分線を求める\nvoid calc_Vertical_Bisector(Point A,Point B){\n\n\tdouble first_slope = calc_slope(Line(A,B));\n\tPoint mid_Point = Point((A.x+B.x)/2,(A.y+B.y)/2);\n\n\tPoint another;\n\n\tif(fabs(first_slope) < EPS){ //AとBが水平に位置している場合\n\n\t\tanother.x = mid_Point.x;\n\t\tanother.y = mid_Point.y+NUM;\n\n\t}else if(fabs(first_slope-DBL_MAX) < EPS){ //AとBが垂直に位置している場合\n\n\t\tanother.x = mid_Point.x+NUM;\n\t\tanother.y = mid_Point.y;\n\n\t}else{ //その他\n\n\t\tdouble slope = -1.0/first_slope;\n\n\t\tanother.x = mid_Point.x+NUM;\n\t\tanother.y = mid_Point.y+NUM*slope;\n\t}\n\tLINE.push_back(Line(mid_Point,another));\n}\n\nbool dfs(vector<int> LINE_TO_USE,int index,int num_line){\n\n\tif(index == LINE.size()){\n\n\t\tif(LINE_TO_USE.size() != num_line)return false;\n\n\t\tinit();\n\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tfor(int k = 0; k < num_line; k++){\n\n\t\t\t\tif(REFLECT[i][LINE_TO_USE[k]] == -1)continue;\n\n\t\t\t\tunite(i,REFLECT[i][LINE_TO_USE[k]]);\n\t\t\t}\n\t\t}\n\n\t\tint num_group = 0;\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tif(i == get_boss(i)){\n\n\t\t\t\tnum_group++;\n\t\t\t}\n\t\t}\n\n\t\treturn num_group == 1; //全てが互いに行き来可能になったらOK\n\t}\n\n\tbool ret = dfs(LINE_TO_USE,index+1,num_line); ////LINE[index]を使わない\n\tif(ret){\n\n\t\treturn true;\n\t}\n\n\tif(LINE_TO_USE.size() == num_line)return false; //これ以上増やせないならfalseをreturn\n\n\t//LINE[index]を使う\n\tvector<int> next_LINE_TO_USE = LINE_TO_USE;\n\tnext_LINE_TO_USE.push_back(index);\n\n\treturn dfs(next_LINE_TO_USE,index+1,num_line);\n}\n\nbool is_OK(int num_line){\n\n\tvector<int> LINE_TO_USE; //使う直線の集合\n\n\treturn dfs(LINE_TO_USE,0,num_line);\n}\n\n//対称移動を事前計算しておく\nvoid calc_Reflect(){\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < LINE.size(); k++){\n\n\t\t\tREFLECT[i][k] = -1;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < LINE.size(); k++){\n\n\t\t\tPoint tmp = calc_Reflection_Point(LINE[k],info[i]);\n\n\t\t\tfor(int a = 0; a < N; a++){\n\t\t\t\tif(a == i)continue;\n\n\t\t\t\tif(equals(info[a],tmp)){\n\n\t\t\t\t\tREFLECT[i][k] = a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&info[i].x,&info[i].y);\n\t}\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tfor(int k = i+1; k < N; k++){\n\t\t\tcalc_Vertical_Bisector(info[i],info[k]); //垂直二等分線をあらかじめ全計算しておく\n\t\t}\n\t}\n\n\tcalc_Reflect();\n\n\tint minimum = N-1; //少なくともN-1本あればクリア可能\n\n\tfor(int num_line = 1; num_line < minimum; num_line++){\n\t\tif(is_OK(num_line)){\n\n\t\t\tminimum = num_line;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",minimum);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3288, "score_of_the_acc": -0.2531, "final_rank": 10 }, { "submission_id": "aoj_1581_3804010", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 8\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\tPoint p[2];\n};\n\nint N;\nint boss[8],height[8],table[30];\nint REFLECT[8][30];\ndouble NUM = 10000;\nPoint info[8];\nvector<Line> LINE;\n\nint get_boss(int id){\n\tif(boss[id] == id)return id;\n\telse{\n\t\treturn boss[id] = get_boss(boss[id]);\n\t}\n}\n\nvoid unite(int x,int y){\n\tint boss_x = get_boss(x);\n\tint boss_y = get_boss(y);\n\n\tif(boss_x == boss_y)return;\n\n\tif(height[x] > height[y]){\n\n\t\tboss[boss_y] = boss_x;\n\n\t}else if(height[x] < height[y]){\n\n\t\tboss[boss_x] = boss_y;\n\n\t}else{ //height[x] == height[y]\n\n\t\tboss[boss_y] = boss_x;\n\t\theight[x]++;\n\t}\n}\n\nvoid init(){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tboss[i] = i;\n\t\theight[i] = 0;\n\t}\n}\n\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool equals(Point A,Point B){\n\n\treturn abs(A-B) < EPS;\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[0].y-A.p[1].y)/(A.p[0].x-A.p[1].x);\n\t}\n}\n\n//点Aと点Bの垂直二等分線を求める\nvoid calc_Vertical_Bisector(Point A,Point B){\n\n\tdouble first_slope = calc_slope(Line(A,B));\n\tPoint mid_Point = Point((A.x+B.x)/2,(A.y+B.y)/2);\n\n\tPoint another;\n\n\tif(fabs(first_slope) < EPS){ //AとBが水平に位置している場合\n\n\t\tanother.x = mid_Point.x;\n\t\tanother.y = mid_Point.y+NUM;\n\n\t}else if(fabs(first_slope-DBL_MAX) < EPS){ //AとBが垂直に位置している場合\n\n\t\tanother.x = mid_Point.x+NUM;\n\t\tanother.y = mid_Point.y;\n\n\t}else{ //その他\n\n\t\tdouble slope = -1.0/first_slope;\n\n\t\tanother.x = mid_Point.x+NUM;\n\t\tanother.y = mid_Point.y+NUM*slope;\n\t}\n\tLINE.push_back(Line(mid_Point,another));\n}\n\nbool dfs(vector<int> LINE_TO_USE,int index,int num_line){\n\n\tif(index == LINE.size()){\n\n\t\tif(LINE_TO_USE.size() != num_line)return false;\n\n\t\tinit();\n\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tfor(int k = 0; k < num_line; k++){\n\n\t\t\t\tif(REFLECT[i][LINE_TO_USE[k]] == -1)continue;\n\n\t\t\t\tunite(i,REFLECT[i][LINE_TO_USE[k]]);\n\t\t\t}\n\t\t}\n\n\t\tint num_group = 0;\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tif(i == get_boss(i)){\n\n\t\t\t\tnum_group++;\n\t\t\t}\n\t\t}\n\n\t\treturn num_group == 1; //全てが互いに行き来可能になったらOK\n\t}\n\n\tbool ret = dfs(LINE_TO_USE,index+1,num_line); ////LINE[index]を使わない\n\tif(ret){\n\n\t\treturn true;\n\t}\n\n\tif(LINE_TO_USE.size() == num_line)return false; //これ以上増やせないならfalseをreturn\n\n\t//LINE[index]を使う\n\tvector<int> next_LINE_TO_USE = LINE_TO_USE;\n\tnext_LINE_TO_USE.push_back(index);\n\n\treturn dfs(next_LINE_TO_USE,index+1,num_line);\n}\n\nbool is_OK(int num_line){\n\n\tvector<int> LINE_TO_USE; //使う直線の集合\n\n\treturn dfs(LINE_TO_USE,0,num_line);\n}\n\n//対称移動を事前計算しておく\nvoid calc_Reflect(){\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < LINE.size(); k++){\n\n\t\t\tREFLECT[i][k] = -1;\n\t\t}\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\t\tfor(int k = 0; k < LINE.size(); k++){\n\n\t\t\tPoint tmp = calc_Reflection_Point(LINE[k],info[i]);\n\n\t\t\tfor(int a = 0; a < N; a++){\n\t\t\t\tif(a == i)continue;\n\n\t\t\t\tif(equals(info[a],tmp)){\n\n\t\t\t\t\tREFLECT[i][k] = a;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(){\n\n\tscanf(\"%d\",&N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&info[i].x,&info[i].y);\n\t}\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tfor(int k = i+1; k < N; k++){\n\t\t\tcalc_Vertical_Bisector(info[i],info[k]); //垂直二等分線をあらかじめ全計算しておく\n\t\t}\n\t}\n\n\tcalc_Reflect();\n\n\tint minimum = N-1; //少なくともN-1本あればクリア可能\n\n\tfor(int num_line = 0; num_line < minimum; num_line++){ //星が近い場合がある?\n\t\tif(is_OK(num_line)){\n\n\t\t\tminimum = num_line;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",minimum);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3276, "score_of_the_acc": -0.2528, "final_rank": 9 }, { "submission_id": "aoj_1581_2747990", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef P Vector;\n\ntypedef pair<P,Vector> Line;\n\nint gcd(int a,int b){\n\tif(b==0)return a;\n\treturn gcd(b,a%b);\n}\n\nint dot(Vector a,Vector b){\n\treturn a.first*a.second+b.first*b.second;\n}\n\nint cross(Vector a,Vector b){\n\treturn a.first*b.second-b.first*a.second;\n}\n\nint n;\nint x[9],y[9];\nint ans;\nvector<Line> li;\nbool po=false;\n\nbool same(Line a,Line b){\n\tif(a.second.first==0){\n\t\tif(a.first.first==b.first.first && b.second.first==0)return true;\n\t}else if(a.second.second==0){\n\t\tif(a.first.second==b.first.second && b.second.second==0)return true;\n\t}else{\n\t\tint diff=b.first.first-a.first.first;\n\t\tif(diff%a.second.first!=0)return false;\n\t\tint bi=diff/a.second.first;\n\t\tif(cross(a.second,b.second)==0 && a.first.second+bi*a.second.second==b.first.second)return true;\n\t}\n\treturn false;\n}\n\nvoid dfs(int bit){\n\t//printf(\"%d\\n\",bit);\n\tif(bit==(1<<n)-1){\n\t\tans=min(ans,(int)li.size());\n\t\treturn;\n\t}\n\tif(ans<=li.size())return;\n\tfor(int i=0;i<n;i++){\n\t\tif(bit>>i & 1){\n\t\t\tfor(int j=0;j<n;j++){\n\t\t\t\tif(!(bit>>j & 1)){\n\t\t\t\t\tP mid=P((x[i]+x[j])/2,(y[i]+y[j])/2);\n\t\t\t\t\tVector vec=Vector(y[j]-y[i],x[i]-x[j]);\n\t\t\t\t\tint g=gcd(abs(y[j]-y[i]),abs(x[i]-x[j]));\n\t\t\t\t\t//printf(\"%d %d %d\\n\",abs(y[j]-y[i]),abs(x[i]-y[j]),g);\n\t\t\t\t\tvec.first/=g;\n\t\t\t\t\tvec.second/=g;\n\t\t\t\t\tbool flag=false;\n\t\t\t\t\tfor(int k=0;k<li.size();k++){\n\t\t\t\t\t\tif(same(li[k],Line(mid,vec))){\n\t\t\t\t\t\t\tflag=true;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(!flag){\n\t\t\t\t\t\tli.push_back(Line(mid,vec));\n\t\t\t\t\t}\n\t\t\t\t\tdfs(bit+(1<<j));\n\t\t\t\t\tif(!flag){\n\t\t\t\t\t\tli.pop_back();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nint main(void){\n\tscanf(\"%d\",&n);\n\tfor(int i=0;i<n;i++){\n\t\tscanf(\"%d%d\",&x[i],&y[i]);\n\t\tx[i]*=2;\n\t\ty[i]*=2;\n\t}\n\tvector<Line> li;\n\tbool po=false;\n\tfor(int i=0;i<n;i++){\n\t\tfor(int j=i+1;j<n;j++){\n\t\t\tP mid=P((x[i]+x[j])/2,(y[i]+y[j])/2);\n\t\t\tVector vec=Vector(y[j]-y[i],x[i]-x[j]);\n\t\t\tint g=gcd(abs(y[j]-y[i]),abs(x[i]-x[j]));\n\t\t\tvec.first/=g;\n\t\t\tvec.second/=g;\n\t\t\tfor(int k=0;k<n;k++){\n\t\t\t\tif(k==i)continue;\n\t\t\t\tfor(int l=k+1;l<n;l++){\n\t\t\t\t\tif(l==j)continue;\n\t\t\t\t\tP mid2=P((x[k]+x[l])/2,(y[k]+y[l])/2);\n\t\t\t\t\tVector vec2=Vector(y[l]-y[k],x[k]-x[l]);\n\t\t\t\t\tg=gcd(abs(y[l]-y[k]),abs(x[l]-x[k]));\n\t\t\t\t\tvec2.first/=g;\n\t\t\t\t\tvec2.second/=g;\n\t\t\t\t\tif(same(Line(mid,vec),Line(mid2,vec2)))po=true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif(!po){\n\t\tprintf(\"%d\\n\",n-1);\n\t\treturn 0;\n\t}\n\tans=n-2;\n\tdfs(1);\n\tprintf(\"%d\\n\",ans);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 3208, "score_of_the_acc": -0.4374, "final_rank": 11 }, { "submission_id": "aoj_1581_2747965", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define fi first\n#define se second\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\n#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\nvoid _dbg(string){cerr<<endl;}\ntemplate<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<\" = \"<<h<<\", \";_dbg(s.substr(l+1),t...);}\ntemplate<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\n\nclass UnionFind {\npublic:\n int par[8], rank[8];\n UnionFind(){\n rep(i,8) par[i] = -1;\n rep(i,8) rank[i] = 0;\n }\n int find(int x){\n if(par[x]<0) return x;\n else return par[x] = find(par[x]);\n }\n void unite(int x, int y){\n x=find(x); y=find(y);\n if(x==y) return;\n if(rank[x] < rank[y]) swap(x,y);\n par[x] += par[y];\n par[y] = x;\n if(rank[x]==rank[y]) rank[x]++;\n }\n inline bool same(int x, int y){ return find(x) == find(y); }\n inline int size(int x){ return -par[find(x)]; }\n};\n\nint n;\nint x[101],y[101];\n\nint main(){\n scanf(\"%d\",&n);\n rep(i,n) scanf(\"%d%d\",x+i,y+i);\n\n using P = pair<int,int>;\n using PP = pair<P,int>;\n map<PP, vector<P>> lines;\n rep(i,n) repl(j,i+1,n){\n int dx=x[i]-x[j];\n int dy=y[i]-y[j];\n swap(dx,dy);\n dy = -dy;\n PP cp;\n if(dy==0){\n cp = PP({1,0}, y[i]+y[j]);\n }\n else if(dx==0){\n cp = PP({0,1}, x[i]+x[j]);\n }\n else{\n int g = __gcd(dx,dy);\n dx /= g; dy /= g;\n if(dx<0){ dx=-dx; dy = -dy; }\n cp = PP({dx,dy}, (y[i]+y[j])*dx - (x[i]+x[j])*dy);\n }\n lines[cp].push_back({i,j});\n }\n\n map<vector<int>, int> dp;\n vector<int> init(n);\n rep(i,n) init[i] = i;\n\n dp[init] = 0;\n int ref[8];\n for(auto &vp : lines){\n map<vector<int>, int> nxt(dp);\n for(auto &sv : dp){\n UnionFind uf;\n rep(i,n) uf.unite(i, sv.first[i]);\n for(auto &p : vp.second) uf.unite(p.first, p.second);\n\n rep(i,n) ref[i] = i;\n rep(i,n) ref[uf.find(i)] = min(i, ref[uf.find(i)]);\n vector<int> ns(n);\n rep(i,n) ns[i] = ref[uf.find(i)];\n\n auto itr = nxt.find(ns);\n if(itr == nxt.end()) nxt[ns] = sv.second + 1;\n else itr->second = min(itr->second, sv.second + 1);\n }\n swap(dp, nxt);\n }\n\n vector<int> fin(n,0);\n cout << dp[fin] << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4220, "score_of_the_acc": -0.0751, "final_rank": 1 }, { "submission_id": "aoj_1581_2747895", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define fi first\n#define se second\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\n#define INF 1e16\n#define mod 1000000007\n\nint n;\nint x[101],y[101];\n\nint main(){\n scanf(\"%d\",&n);\n rep(i,n) scanf(\"%d%d\",x+i,y+i);\n\n vector<int> idx;\n rep(i,n) idx.push_back(i);\n int res=1000000;\n\n using PP = pair<pair<int,int>,int>;\n do {\n set<PP> as;\n rep(i,n-1){\n int dx=x[idx[i+1]]-x[idx[i]];\n int dy=y[idx[i+1]]-y[idx[i]];\n swap(dx,dy);\n dy = -dy;\n if(dy==0){\n auto cp = PP({1,0}, y[idx[i]]+y[idx[i+1]]);\n as.insert(cp);\n }\n else if(dx==0){\n auto cp = PP({0,1}, x[idx[i]]+x[idx[i+1]]);\n as.insert(cp);\n }\n else{\n int g = __gcd(dx,dy);\n dx /= g; dy /= g;\n if(dx<0){ dx=-dx; dy = -dy; }\n auto cp = PP({dx,dy}, (y[idx[i]]+y[idx[i+1]])*dx - (x[idx[i]]+x[idx[i+1]])*dy);\n as.insert(cp);\n }\n }\n minch(res,(int)as.size());\n }while(next_permutation(all(idx)));\n cout<<res<<endl;\n return 0;\n}", "accuracy": 0.3939393939393939, "time_ms": 10, "memory_kb": 3228, "score_of_the_acc": -0.0425, "final_rank": 17 }, { "submission_id": "aoj_1581_2747888", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<double, double> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define mmin(x,y) (x<y?x:y)\n#define maxch(x,y) x=mmax(x,y)\n#define minch(x,y) x=mmin(x,y)\n#define uni(x) x.erase(unique(all(x)),x.end())\n#define exist(x,y) (find(all(x),y)!=x.end())\n#define bcnt __builtin_popcount\n\n#define INF 1e16\n#define mod 1000000007\n\nint n;\ndouble x[101],y[101];\n\nint main(){\n scanf(\"%d\",&n);\n rep(i,n){\n scanf(\"%lf%lf\",x+i,y+i);\n }\n vector<int> idx;\n rep(i,n)idx.push_back(i);\n int res=1000000;\n do{\n int sum=0;\n set<P> as;\n rep(i,n-1){\n double dx=x[idx[i+1]]-x[idx[i]];\n double dy=y[idx[i+1]]-y[idx[i]];\n if(dy==0){\n double midx=(double)(x[idx[i+1]]+x[idx[i]])/2.0;\n if(as.find(P(1e18,midx))!=as.end())continue;\n as.insert(P(1e18,midx));\n sum++;\n }else if(dx==0){\n double midy=(double)(y[idx[i+1]]+y[idx[i]])/2.0;\n if(as.find(P(0,midy))!=as.end())continue;\n as.insert(P(0,midy));\n sum++;\n }else{\n double a=-1.0/(dy/dx);\n double midx=(double)(x[idx[i+1]]+x[idx[i]])/2.0;\n double midy=(double)(y[idx[i+1]]+y[idx[i]])/2.0;\n double b=midy-midx*a;\n if(as.find(P(a,b))!=as.end())continue;\n as.insert(P(a,b));\n sum++;\n }\n }\n minch(res,sum);\n }while(next_permutation(all(idx)));\n cout<<res<<endl;\n return 0;\n}", "accuracy": 0.3939393939393939, "time_ms": 10, "memory_kb": 3280, "score_of_the_acc": -0.0436, "final_rank": 18 }, { "submission_id": "aoj_1581_2576382", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ndouble INF=1<<29;\ntypedef long long ll;\ntypedef complex<double> P;\ntypedef pair<int,int> PP;\nnamespace std {\n bool operator < (const P& a, const P& b) {\n return real(a)!=real(b)?real(a)<real(b):imag(a)<imag(b);\n }\n}\nint p[10],r[10];\nvoid init(){for(int i=0;i<10;i++)p[i]=i,r[i]=0;}\nint find(int x){return (p[x]==x)?x:(p[x]=find(p[x]));}\nvoid unite(int x,int y) {\n x=find(x),y=find(y);\n if(x==y)return;\n if(r[x]<r[y])p[x]=y;\n else{p[y]=x;if(r[x]==r[y])r[x]++;}\n}\nbool same(int x,int y){return find(x)==find(y);}\n\nP PerpendicularBisector(P a,P b) {\n double x,y;\n if(b.imag()-a.imag()) {\n x=-(b.real()-a.real())/(b.imag()-a.imag());\n y=-(a.real()+b.real())/2*x+(a.imag()+b.imag())/2;\n } else {\n x=INF;\n y=(a.real()+b.real())/2;\n }\n if(x==-0) x=0;\n return P(x,y);\n}\n\nP a[10];\nll n,k,ans;\nmap<P,vector<PP> > m;\nmap<int,P> ma;\nvoid dfs(int l, ll t) {\n bool f=1;\n for(int i=1; i<n; i++) {\n if(!same(0,i)) {\n f=0;\n break;\n }\n }\n if(f) ans=min(ans,t);\n if(t>=n-2) return;\n for(int i=l; i<k; i++) {\n int pp[n],rr[n];\n for(int j=0; j<n; j++) pp[j]=p[j],rr[j]=r[j];\n vector<PP> v=m[ma[i]];\n for(int j=0; j<v.size(); j++) unite(v[j].first,v[j].second);\n dfs(i+1,t+1);\n for(int j=0; j<n; j++) p[j]=pp[j],r[j]=rr[j];\n }\n}\n\nint main() {\n cin >> n;\n for(int i=0,x,y; i<n; i++) {\n cin >> x >> y;\n a[i]=P(x,y);\n }\n for(int i=0; i<n; i++) {\n for(int j=i+1; j<n; j++) m[PerpendicularBisector(a[i],a[j])].push_back(PP(i,j));\n }\n k=0;\n for(map<P,vector<PP> >::iterator it=m.begin();it!=m.end();it++) ma[k++]=it->first;\n init();\n ans=n-1;\n dfs(0,0);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3136, "score_of_the_acc": -0.0755, "final_rank": 2 }, { "submission_id": "aoj_1581_1907089", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n \n#define _overload(_1,_2,_3,name,...) name\n#define _rep(i,n) _range(i,0,n)\n#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)\n \n#define _rrep(i,n) _rrange(i,n,0)\n#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)\n#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)\n \n#define _all(arg) begin(arg),end(arg)\n#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))\n#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)\n#define clr(a,b) memset((a),(b),sizeof(a))\n#define bit(n) (1LL<<(n))\n \n// #define DEBUG\n \n#ifdef DEBUG\n #define dump(...) fprintf(stderr, __VA_ARGS__)\n#else\n #define dump(...)\n#endif\n \ntemplate<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}\ntemplate<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}\n \nusing namespace std;\nusing ll=long long;\nusing vi=vector<int>;\nusing vll=vector<ll>;\n \nconst double EPS = 1e-9;\nconst double PI = acos(-1.0);\nconst ll inf =1LL << 62;\nconst ll mod=1000000007LL;\nconst int dx[4]={1,0,-1,0};\nconst int dy[4]={0,1,0,-1};\n \n \nll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}\nll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}\nll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}\nll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}\nll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}\n \nrandom_device rd;\nmt19937 mt(rd());\nuniform_int_distribution<int> dice(1,6);\nuniform_real_distribution<double> score(0.0,10.0);\n \nusing E = tuple<int, int>;\nusing R = double;\nusing P = complex<R>;\nusing L = tuple<P, P>;\n \ninline R dt(P a, P b){\n\treturn imag(conj(a) * b);\n}\n\nbool match(L lhs, L rhs){\n P a1, a2, b1, b2;\n tie(a1, a2) = lhs;\n tie(b1, b2) = rhs;\n \n if(abs(dt(a2 - a1, b2 - b1)) > EPS) return false;\n if(abs(dt(a2 - a1, b2 - a1)) > EPS) return false;\n \n return true;\n}\n \nL make_line(P a, P b){\n\tP mid = (a + b) / P(2, 0);\n \n return L(mid, mid + (b - a) * P(0, 1));\n}\n\nint n, res;\nvector<P> p;\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) { }\n bool unionSet(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n\nbool check_tree(vector<E> & edges){\n\tUnionFind uf(n);\n\n\tfor(auto & e : edges){\n\t\tint a, b; tie(a, b) = e;\n\t\tuf.unionSet(a, b);\n\t}\n\n\trep(i, n){\n\t\tif(uf.root(0) != uf.root(i)) return false;\n\t}\n\treturn true;\n}\n\nsigned main(void){\n cin >> n;\n p = vector<P>(n);\n for(auto & e : p){\n R x, y; cin >> x >> y;\n e = P(x, y);\n }\n\n\tvector<E> tbl;\n\trep(a, n){\n\t\trep(b, a + 1, n){\n\t\t\ttbl.push_back(E(a, b));\n\t\t}\n\t}\n\n\tint res = n - 1;\n\n\tint k = n - 1;\n\tint comb = (1 << k) - 1;\n\tint len = tbl.size();\n\n\twhile(comb < 1 << len){\n\t\tvector<E> edges;\n\t\trep(i, len){\n\t\t\tif(((comb >> i) & 1) == 0) continue;\n\n\t\t\tedges.push_back(tbl[i]);\n\t\t}\n\n\t\tint x = comb & -comb, y = comb + x;\n\t\tcomb = (((comb & ~y) / x) >> 1) | y;\n\n\t\tif(not check_tree(edges)) continue;\n\n\t\tvector<L> s;\n\t\tfor(auto & e : edges){\n\t\t\tint a, b; tie(a, b) = e;\n\n\t\t\tL cline = make_line(p[a], p[b]);\n\t\t\t[&]{\n\t\t\t\tfor(auto & l : s){\n\t\t\t\t\tif(match(l, cline)) return;\n\t\t\t\t}\n\t\t\t\ts.push_back(cline);\n\t\t\t}();\n\t\t}\n\n//\t\tif(s.size() == 2){\n//\t\t\tfor(auto & e : edges){\n//\t\t\t\tint a, b; tie(a, b) = e;\n//\t\t\t\tcerr << a << \" \" << b << endl;\n//\n//\t\t\t\tL cline = make_line(p[a], p[b]);\n//\t\t\t\tP p1, p2; tie(p1, p2) = cline;\n//\t\t\t\tcerr << fixed << real(p1) << \" \" << imag(p1) << endl;\n//\t\t\t\tcerr << fixed << real(p2) << \" \" << imag(p2) << endl;\n//\t\t\t}\n//\t\t\tcerr << \"---\" << endl;\n//\t\t}\n\n\t\tchmin(res, (int)s.size());\n\t}\n\n\tcout << res << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 3244, "score_of_the_acc": -0.5545, "final_rank": 12 }, { "submission_id": "aoj_1581_1907075", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n \n#define _overload(_1,_2,_3,name,...) name\n#define _rep(i,n) _range(i,0,n)\n#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)\n \n#define _rrep(i,n) _rrange(i,n,0)\n#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)\n#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)\n \n#define _all(arg) begin(arg),end(arg)\n#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))\n#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)\n#define clr(a,b) memset((a),(b),sizeof(a))\n#define bit(n) (1LL<<(n))\n \n// #define DEBUG\n \n#ifdef DEBUG\n #define dump(...) fprintf(stderr, __VA_ARGS__)\n#else\n #define dump(...)\n#endif\n \ntemplate<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}\ntemplate<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}\n \nusing namespace std;\nusing ll=long long;\nusing vi=vector<int>;\nusing vll=vector<ll>;\n \nconst double EPS = 1e-9;\nconst double PI = acos(-1.0);\nconst ll inf =1LL << 62;\nconst ll mod=1000000007LL;\nconst int dx[4]={1,0,-1,0};\nconst int dy[4]={0,1,0,-1};\n \n \nll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}\nll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}\nll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}\nll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}\nll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}\n \nrandom_device rd;\nmt19937 mt(rd());\nuniform_int_distribution<int> dice(1,6);\nuniform_real_distribution<double> score(0.0,10.0);\n \nusing E = tuple<int, int>;\nusing R = double;\nusing P = complex<R>;\nusing L = tuple<P, P>;\n \ninline R dt(P a, P b){\n\treturn imag(conj(a) * b);\n}\n\nbool match(L lhs, L rhs){\n P a1, a2, b1, b2;\n tie(a1, b1) = lhs;\n tie(a2, b2) = rhs;\n \n if(abs(dt(a2 - a1, b2 - b1)) > EPS) return false;\n if(abs(dt(a2 - a1, b2 - a1)) > EPS) return false;\n \n return true;\n}\n \nL make_line(P a, P b){\n\tP mid = (a + b) / P(2, 0);\n \n return L(mid, mid + (b - a) * P(0, 1));\n}\n\nint n, res;\nvector<P> p;\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) { }\n bool unionSet(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n\nbool check_tree(vector<E> & edges){\n\tUnionFind uf(n);\n\n\tfor(auto & e : edges){\n\t\tint a, b; tie(a, b) = e;\n\t\tuf.unionSet(a, b);\n\t}\n\n\trep(i, n){\n\t\tif(uf.root(0) != uf.root(i)) return false;\n\t}\n\treturn true;\n}\n\nsigned main(void){\n cin >> n;\n p = vector<P>(n);\n for(auto & e : p){\n int x, y; cin >> x >> y;\n e = P(x, y);\n }\n\n\tvector<E> tbl;\n\trep(a, n){\n\t\trep(b, a + 1, n){\n\t\t\ttbl.push_back(E(a, b));\n\t\t}\n\t}\n\n\tint res = n - 1;\n\n\tint k = n - 1;\n\tint comb = (1 << k) - 1;\n\tint len = tbl.size();\n\n\twhile(comb < 1 << len){\n\t\tvector<E> edges;\n\t\trep(i, len){\n\t\t\tif(((comb >> i) & 1) == 0) continue;\n\n\t\t\tedges.push_back(tbl[i]);\n\t\t}\n\n\t\tint x = comb & -comb, y = comb + x;\n\t\tcomb = (((comb & ~y) / x) >> 1) | y;\n\n\t\tif(not check_tree(edges)) continue;\n\n\t\tvector<L> s;\n\t\tfor(auto & e : edges){\n\t\t\tint a, b; tie(a, b) = e;\n\n\t\t\tL cline = make_line(p[a], p[b]);\n\t\t\t[&]{\n\t\t\t\tfor(auto & l : s){\n\t\t\t\t\tif(match(l, cline)) return;\n\t\t\t\t}\n\t\t\t\ts.push_back(cline);\n\t\t\t}();\n\t\t}\n\n\t\tchmin(res, (int)s.size());\n\t}\n\n\tcout << res << endl;\n \n return 0;\n}", "accuracy": 0.18181818181818182, "time_ms": 20, "memory_kb": 3144, "score_of_the_acc": -0.0524, "final_rank": 19 }, { "submission_id": "aoj_1581_1907074", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n \n#define _overload(_1,_2,_3,name,...) name\n#define _rep(i,n) _range(i,0,n)\n#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)\n#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)\n \n#define _rrep(i,n) _rrange(i,n,0)\n#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)\n#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)\n \n#define _all(arg) begin(arg),end(arg)\n#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))\n#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)\n#define clr(a,b) memset((a),(b),sizeof(a))\n#define bit(n) (1LL<<(n))\n \n// #define DEBUG\n \n#ifdef DEBUG\n #define dump(...) fprintf(stderr, __VA_ARGS__)\n#else\n #define dump(...)\n#endif\n \ntemplate<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}\ntemplate<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}\n \nusing namespace std;\nusing ll=long long;\nusing vi=vector<int>;\nusing vll=vector<ll>;\n \nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst ll inf =1LL << 62;\nconst ll mod=1000000007LL;\nconst int dx[4]={1,0,-1,0};\nconst int dy[4]={0,1,0,-1};\n \n \nll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}\nll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}\nll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}\nll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}\nll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}\n \nrandom_device rd;\nmt19937 mt(rd());\nuniform_int_distribution<int> dice(1,6);\nuniform_real_distribution<double> score(0.0,10.0);\n \nusing E = tuple<int, int>;\nusing R = double;\nusing P = complex<R>;\nusing L = tuple<P, P>;\n \ninline R dt(P a, P b){\n\treturn imag(conj(a) * b);\n}\n\nbool match(L lhs, L rhs){\n P a1, a2, b1, b2;\n tie(a1, b1) = lhs;\n tie(a2, b2) = rhs;\n \n if(abs(dt(a2 - a1, b2 - b1)) > EPS) return false;\n if(abs(dt(a2 - a1, b2 - a1)) > EPS) return false;\n \n return true;\n}\n \nL make_line(P a, P b){\n\tP mid = (a + b) / P(2, 0);\n \n return L(mid, mid + (b - a) * P(0, 1));\n}\n\nint n, res;\nvector<P> p;\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) { }\n bool unionSet(int x, int y) {\n x = root(x); y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y]; data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) {\n return root(x) == root(y);\n }\n int root(int x) {\n return data[x] < 0 ? x : data[x] = root(data[x]);\n }\n int size(int x) {\n return -data[root(x)];\n }\n};\n\nbool check_tree(vector<E> & edges){\n\tUnionFind uf(n);\n\n\tfor(auto & e : edges){\n\t\tint a, b; tie(a, b) = e;\n\t\tuf.unionSet(a, b);\n\t}\n\n\trep(i, n){\n\t\tif(uf.root(0) != uf.root(i)) return false;\n\t}\n\treturn true;\n}\n\nsigned main(void){\n cin >> n;\n p = vector<P>(n);\n for(auto & e : p){\n int x, y; cin >> x >> y;\n e = P(x, y);\n }\n\n\tvector<E> tbl;\n\trep(a, n){\n\t\trep(b, a + 1, n){\n\t\t\ttbl.push_back(E(a, b));\n\t\t}\n\t}\n\n\tint res = n - 1;\n\n\tint k = n - 1;\n\tint comb = (1 << k) - 1;\n\tint len = tbl.size();\n\n\twhile(comb < 1 << len){\n\t\tvector<E> edges;\n\t\trep(i, len){\n\t\t\tif(((comb >> i) & 1) == 0) continue;\n\n\t\t\tedges.push_back(tbl[i]);\n\t\t}\n\n\t\tint x = comb & -comb, y = comb + x;\n\t\tcomb = (((comb & ~y) / x) >> 1) | y;\n\n\t\tif(not check_tree(edges)) continue;\n\n\t\tvector<L> s;\n\t\tfor(auto & e : edges){\n\t\t\tint a, b; tie(a, b) = e;\n\n\t\t\tL cline = make_line(p[a], p[b]);\n\t\t\t[&]{\n\t\t\t\tfor(auto & l : s){\n\t\t\t\t\tif(match(l, cline)) return;\n\t\t\t\t}\n\t\t\t\ts.push_back(cline);\n\t\t\t}();\n\t\t}\n\n\t\tchmin(res, (int)s.size());\n\t}\n\n\tcout << res << endl;\n \n return 0;\n}", "accuracy": 0.18181818181818182, "time_ms": 20, "memory_kb": 3184, "score_of_the_acc": -0.0532, "final_rank": 20 }, { "submission_id": "aoj_1581_1844882", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double EPS = 1e-8, INF = 1e12, PI = acos(-1.0);\ntypedef complex<double> P;\nnamespace std{\n\tbool operator<(const P& a, const P& b){\n\t\treturn real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);\n\t}\n}\ninline double cross(const P& a, const P& b){ return imag(conj(a)*b); }\ninline double dot(const P& a, const P& b){ return real(conj(a)*b); }\nstruct L : public vector<P>{\n\tL(const P &a, const P &b) {\n\t\tpush_back(a); push_back(b);\n\t}\n};\ntypedef vector<P> G;\n\ninline int ccw(P a, P b, P c) {\n\tb -= a; c -= a;\n\tif(cross(b, c) > 0) return +1;\t\t// counter clockwise\n\tif(cross(b, c) < 0) return -1;\t\t// clockwise\n\tif(dot(b, c) < 0) return +2;\t\t// c--a--b on line\n\tif(norm(b) < norm(c)) return -2;\t\t// a--b--c on line\n\treturn 0;\n}\n\nint n, x[8], y[8];\nvector<pi> t[8][8];\nmap<vi, int> dp;\n\nint root(vi &p, int x){\n\treturn x == p[x] ? x : p[x] = root(p, p[x]);\n}\n\nint rec(vi v){\n\tif(dp.count(v)) return dp[v];\n\t\n\tint &res = dp[v];\n\tint fin = 0;\n\trep(i, n) fin = max(fin, v[i]);\n\tif(fin == 0) return 0;\n\t\n\tres = inf;\n\trep(i, n) rep(j, i){\n\t\tvi uf, to(n, -1);\n\t\trep(k, fin + 1) uf.pb(k);\n\t\t\n\t\tfor(pi p : t[i][j]){\n\t\t\tint a = root(uf, v[p.first]), b = root(uf, v[p.second]);\n\t\t\tif(a != b) uf[b] = a;\n\t\t}\n\t\tint sz = 0;\n\t\trep(k, n) if(to[root(uf, v[k])] < 0) to[root(uf, v[k])] = sz++;\n\t\t\n\t\tvi nxt;\n\t\trep(k, n) nxt.pb(to[root(uf, v[k])]);\n\t\t\n\t\tif(v != nxt) res = min(res, rec(nxt) + 1);\n\t}\n\treturn res;\n}\nint main(){\n\tcin >> n;\n\trep(i, n) cin >> x[i] >> y[i];\n\trep(i, n) rep(j, i){\n\t\tP p((x[i] + x[j]) / 2.0, (y[i] + y[j]) / 2.0), q(x[j] - x[i], y[j] - y[i]);\n\t\tq *= P(0, 1);\n\t\tt[i][j].pb(mp(i, j));\n\t\trep(k, n) rep(l, k) if(k != i || l != j){\n\t\t\tP r((x[k] + x[l]) / 2.0, (y[k] + y[l]) / 2.0), s(x[l] - x[k], y[l] - y[k]);\n\t\t\ts *= P(0, 1);\n\t\t\tif(abs(cross(q, s)) < EPS && abs(cross(p - r, q)) < EPS){\n\t\t\t\t\n\t\t\t\tt[i][j].pb(mp(k, l));\n\t\t\t}\n\t\t}\n\t}\n\tvi init;\n\trep(i, n) init.pb(i);\n\tcout << rec(init) << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3660, "score_of_the_acc": -0.0982, "final_rank": 3 }, { "submission_id": "aoj_1581_1694941", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& v) {\n if (!v.empty()) {\n out << '[';\n std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, \", \"));\n out << \"\\b\\b]\";\n }\n return out;\n}\ntemplate <class T, class U>\nvoid chmin(T& t, U f) {\n if (t > f) t = f;\n}\ntemplate <class T, class U>\nvoid chmax(T& t, U f) {\n if (t < f) t = f;\n}\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) {}\n UnionFind(const UnionFind& u) { data = u.data; }\n bool unionSet(int x, int y) {\n x = root(x);\n y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y];\n data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) { return root(x) == root(y); }\n int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }\n int size(int x) { return -data[root(x)]; }\n};\n\nint N;\nvector<int> x, y;\nmap<pair<int, int>, vector<pair<int, int>>> m;\n\nvoid hoge(int i, int j) {\n //-2x(x2-x1)-2y(y2-y1)+()+()\n ll a = -2 * (x[j] - x[i]);\n ll b = -2 * (y[j] - y[i]);\n ll c = (x[j] * x[j] - x[i] * x[i]) + (y[j] * y[j] - y[i] * y[i]);\n for (int k = 0; k < N; ++k) {\n for (int l = k + 1; l < N; ++l) {\n if (abs(a * x[l] + b * y[l] + c) == abs(a * x[k] + b * y[k] + c) &&\n (x[k] - x[l]) * (y[i] - y[j]) == (y[k] - y[l]) * (x[i] - x[j])) {\n m[{i, j}].push_back({k, l});\n }\n }\n }\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cin >> N;\n x.resize(N), y.resize(N);\n for (int i = 0; i < N; ++i) cin >> x[i] >> y[i];\n\n for (int i = 0; i < N; ++i) {\n for (int j = i + 1; j < N; ++j) {\n hoge(i, j);\n }\n }\n\n UnionFind start(N);\n vector<pair<UnionFind, int>> dp;\n dp.push_back({start, 0});\n int mi = N;\n for (int i = 0; i < N; ++i) {\n for (int j = i + 1; j < N; ++j) {\n vector<pair<UnionFind, int>> v;\n for (auto& p : dp) {\n if (p.first.findSet(i, j)) continue;\n UnionFind next(p.first);\n for (auto& q : m[{i, j}]) {\n next.unionSet(q.first, q.second);\n }\n for (int k = 1; k < N; ++k) {\n if (!next.findSet(0, k)) {\n v.push_back({next, p.second + 1});\n break;\n }\n if (k == N - 1) chmin(mi, p.second + 1);\n }\n }\n for (auto& q : v) {\n dp.push_back(q);\n }\n }\n }\n cout << mi << endl;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 48364, "score_of_the_acc": -1.1145, "final_rank": 14 }, { "submission_id": "aoj_1581_1694359", "code_snippet": "/*\n _ooOoo_\n o8888888o\n 88\" . \"88\n (| -_- |)\n O\\ = /O\n ____/`---'\\____\n .' \\\\| |// `.\n / \\\\||| : |||// \\\n / _||||| -:- |||||- \\\n | | \\\\\\ - /// | |\n | \\_| ''\\---/'' | |\n \\ .-\\__ `-` ___/-. /\n ___`. .' /--.--\\ `. . __\n .\"\" '< `.___\\_<|>_/___.' >'\"\".\n | | : `- \\`.;`\\ _ /`;.`/ - ` : | |\n \\ \\ `-. \\_ __\\ /__ _/ .-` / /\n======`-.____`-.___\\_____/___.-`____.-'======\n `=---='\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n pass System Test!\n*/\n#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& out, const std::vector<T>& v) {\n if (!v.empty()) {\n out << '[';\n std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, \", \"));\n out << \"\\b\\b]\";\n }\n return out;\n}\ntemplate <class T, class U>\nvoid chmin(T& t, U f) {\n if (t > f) t = f;\n}\ntemplate <class T, class U>\nvoid chmax(T& t, U f) {\n if (t < f) t = f;\n}\n\nstruct UnionFind {\n vector<int> data;\n UnionFind(int size) : data(size, -1) {}\n UnionFind(const UnionFind& u) { data = u.data; }\n bool unionSet(int x, int y) {\n x = root(x);\n y = root(y);\n if (x != y) {\n if (data[y] < data[x]) swap(x, y);\n data[x] += data[y];\n data[y] = x;\n }\n return x != y;\n }\n bool findSet(int x, int y) { return root(x) == root(y); }\n int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }\n int size(int x) { return -data[root(x)]; }\n};\n\nint N;\nvector<int> x, y;\nmap<pair<int, int>, vector<pair<int, int>>> m;\n\nvoid hoge(int i, int j) {\n //-2x(x2-x1)-2y(y2-y1)+()+()\n ll a = -2 * (x[j] - x[i]);\n ll b = -2 * (y[j] - y[i]);\n ll c = (x[j] * x[j] - x[i] * x[i]) + (y[j] * y[j] - y[i] * y[i]);\n for (int k = 0; k < N; ++k) {\n for (int l = k + 1; l < N; ++l) {\n if (abs(a * x[l] + b * y[l] + c) == abs(a * x[k] + b * y[k] + c) &&\n (x[k] - x[l]) * (y[i] - y[j]) == (y[k] - y[l]) * (x[i] - x[j])) {\n m[{i, j}].push_back({k, l});\n }\n }\n }\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n cin >> N;\n x.resize(N), y.resize(N);\n for (int i = 0; i < N; ++i) cin >> x[i] >> y[i];\n\n for (int i = 0; i < N; ++i) {\n for (int j = i + 1; j < N; ++j) {\n hoge(i, j);\n }\n }\n\n UnionFind start(N);\n vector<pair<UnionFind, int>> dp;\n dp.push_back({start, 0});\n int mi = N;\n for (int i = 0; i < N; ++i) {\n for (int j = i + 1; j < N; ++j) {\n vector<pair<UnionFind, int>> v;\n for (auto& p : dp) {\n if (p.first.findSet(i, j)) continue;\n UnionFind next(p.first);\n for (auto& q : m[{i, j}]) {\n next.unionSet(q.first, q.second);\n }\n for (int k = 1; k < N; ++k) {\n if (!next.findSet(0, k)) {\n v.push_back({next, p.second + 1});\n break;\n }\n if (k == N - 1) chmin(mi, p.second + 1);\n }\n }\n for (auto& q : v) {\n dp.push_back(q);\n }\n }\n }\n cout << mi << endl;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 48448, "score_of_the_acc": -1.1163, "final_rank": 15 }, { "submission_id": "aoj_1581_1694276", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\ntypedef long long ll;\ntypedef pair<int,int>pint;\ntypedef vector<int>vint;\ntypedef vector<pint>vpint;\n#define pb push_back\n#define mp make_pair\n#define fi first\n#define se second\n#define all(v) (v).begin(),(v).end()\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\ntemplate<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}\ntemplate<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}\n\nint gcd(int a,int b){\n return b?gcd(b,a%b):a;\n}\n\nstruct data{\n int vx,vy,x0,y0;\n data(int a,int b,int c,int d){\n int g=gcd(abs(a),abs(b));\n a/=g;b/=g;\n if(a<0)a*=-1,b*=-1;\n else if(a==0&&b<0)b*=-1;\n vx=a;vy=b;x0=c;y0=d;\n }\n bool operator==(const data &d){\n if(vx!=d.vx||vy!=d.vy)return false;\n int xx=x0-d.x0,yy=y0-d.y0;\n if(vx==0)return xx==0;\n if(vy==0)return yy==0;\n return xx%vx==0&&yy%vy==0&&xx/vx==yy/vy;\n }\n};\n\nint N;\nint x[10],y[10];\n\nint table[10][10];\n\nint p[]={1,2,3,4,5,6,7,8};\nvint lis;\nbool f[100];\nint ans=1145141919;\n\nvoid dfs(int n,int c){\n if(n==N-1){\n chmin(ans,c);\n return;\n }\n\n int idx=p[n];\n rep(i,lis.size()){\n int tmp=table[lis[i]][idx];\n lis.pb(idx);\n if(f[tmp])dfs(n+1,c);\n else{\n f[tmp]=1;\n dfs(n+1,c+1);\n f[tmp]=0;\n }\n lis.pop_back();\n }\n}\n\nsigned main(){\n cin>>N;\n rep(i,N)cin>>x[i]>>y[i],x[i]*=2,y[i]*=2;\n\n vector<data>vec;\n rep(i,N)rep(j,N)if(i!=j){\n data d(y[j]-y[i],x[i]-x[j],(x[i]+x[j])/2,(y[i]+y[j])/2);\n int t=-1;\n rep(k,vec.size())if(vec[k]==d)t=k;\n if(t==-1){\n table[i][j]=vec.size();\n vec.pb(d);\n }\n else{\n table[i][j]=t;\n }\n }\n\n do{\n lis.clear();\n lis.pb(0);\n dfs(0,0);\n }while(next_permutation(p,p+N-1));\n\n cout<<ans<<endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 1220, "score_of_the_acc": -0.2326, "final_rank": 8 }, { "submission_id": "aoj_1581_1694213", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define FOR(i,n) for(int i = 0 ;i < (n); i++) \n#define sz(c) ((int)c.size())\n\nclass UF {\npublic:\n\tint n; vector<int> a;\n\tUF(int n) : n(n), a(n, -1) {}\n\tint find(int x) {\n\t\treturn a[x] < 0 ? x : (a[x] = find(a[x]));\n\t}\n\tbool unite(int x, int y) {\n\t\tx = find(x), y = find(y);\n\t\tif (x == y) return false;\n\t\tif (a[x] > a[y]) swap(x, y);\n\t\ta[x] += a[y];\n\t\ta[y] = x;\n\t\tn--;\n\t\treturn true;\n\t}\n\tint size(int x) {\n\t\treturn -a[find(x)];\n\t}\n};\n\nstruct P {\n\tP() {}\n\tint x, y;\n\tP(int x, int y) : x(x), y(y) {}\n\tP operator+(const P& r) const {\n\t\treturn P(x + r.x, y + r.y);\n\t}\n\tP operator-(const P& r) const {\n\t\treturn P(x - r.x, y - r.y);\n\t}\n};\n\nint cross(P& l, P& r) {\n\treturn l.x * r.y - l.y * r.x;\n}\nbool on(P a, P b, P c) {\n\tb = b - a;\n\tc = c - a;\n\treturn cross(b, c) == 0;\n}\n\nP center(P& l, P& r) {\n\treturn P((l.x + r.x) / 2, (l.y + r.y) / 2);\n}\n\nstruct L {\n\tP o; P dir;\n\tL(){};\n};\n\nL l[8][8];\nbool b[8][8][8][8];\n\nmap<vector<int>, int> mp;\nint dfs(vector<int> f) {\n\tif (mp.count(f)) return mp[f];\n\n\tbool end = true;\n\tFOR(i, sz(f)) {\n\t\tif (f[i] != 0) end = false;\n\t}\n\tif (end) {\n\t\treturn mp[f] = 0;\n\t}\n\n\tint ans = 100;\n\tFOR(i, sz(f)) {\n\t\tFOR(j, i) {\n\t\t\tif (f[i] == f[j]) continue;\n\t\t\tauto copyed = f;\n\t\t\tFOR(a, sz(f)) FOR(b, a) {\n\t\t\t\tif (f[b] == f[a]) continue;\n\t\t\t\tbool er = ::b[i][j][a][b];\n\t\t\t\tif (er) {\n\t\t\t\t\tint to = f[b];\n\t\t\t\t\tint from = f[a];\n\t\t\t\t\tFOR(c, sz(f)) if (f[c] == from) f[c] = to;\n\t\t\t\t}\n\t\t\t}\n\t\t\tans = min(ans,dfs(f));\n\t\t\tf = copyed;\n\t\t}\n\t}\n\n\treturn mp[f] = ans + 1;\n}\n\nint main() {\n\tint n; cin >> n;\n\tvector<P> vp;\n\tFOR(i, n) {\n\t\tint x, y; cin >> x >> y; vp.emplace_back(x * 2, y * 2);\n\t}\n\tFOR(i, n) FOR(j, n) {\n\t\tif (i == j) continue;\n\t\tl[i][j].o = center(vp[i], vp[j]);\n\t\tauto d = vp[i] - vp[j];\n\t\tl[i][j].dir = P(-d.y, d.x);\n\t}\n\tFOR(i, n) FOR(j, n) {\n\t\tif (i == j) continue;\n\t\tFOR(a, n) FOR(b, n) {\n\t\t\tif (a == b) continue;\n\t\t\tbool x = on(l[i][j].o, l[i][j].o + l[i][j].dir, l[a][b].o);\n\t\t\tbool y = on(l[i][j].o, l[i][j].o + l[i][j].dir, l[a][b].o + l[a][b].dir);\n\t\t\t::b[i][j][a][b] = x && y;\n\t\t}\n\t}\n\n\tvector<int> p(n);\n\tFOR(i, n) p[i] = i;\n\n\tint ans = dfs(p);\n\tcout << ans << endl;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 4560, "score_of_the_acc": -0.1521, "final_rank": 5 }, { "submission_id": "aoj_1581_1694169", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long double ld;\ntypedef complex<ld> Point;\ntypedef pair<int, int> P;\n\nconst ld eps = 1e-12, pi = acos(-1.0);\n\nconst int inf = 1e9;\n\nnamespace std {\n\tbool operator<(const Point& lhs, const Point& rhs) {\n\t\tif (lhs.real() < rhs.real() - eps) return true;\n\t\tif (lhs.real() > rhs.real() + eps) return false;\n\t\treturn lhs.imag() < rhs.imag();\n\t}\n}\n\nstruct Line {\n\tPoint a, b;\n\tLine() : a(Point(0,0)), b(Point(0,0)) {}\n\tLine(Point a, Point b) : a(a), b(b) {}\n};\n\nstruct Edge {\n\tint from, to, id;\n};\n\nint N;\nmap<P, int> tbl2;\nmap<int, int> tbl;\nvector< vector<Edge> > G;\nvector<Edge> edges;\n\nint to_hash(vector<int> v) {\n\tint res = 0;\n\tfor (int i = 0; i < N; ++i) {\n\t\tres = res * 5 + v[i];\n\t}\n\treturn res;\n}\nvector<int> from_hash(int hash) {\n\tvector<int> res;\n\tfor (int i = 0; i < N; ++i) {\n\t\tres.push_back(hash%5);\n\t\thash /= 5;\n\t}\n\treverse( res.begin(), res.end() );\n\treturn res;\n}\n\nclass UnionFind {\nprivate:\n\tint sz;\n\tvector<int> par, h;\n\npublic:\n\tUnionFind(int size) : sz(size) {\n\t\tpar.resize(sz);\n\t\tfor (int i = 0; i < sz; ++i) {\n\t\t\tpar[i] = i;\n\t\t}\n\t\th = vector<int>(sz, 0);\n\t}\n\tint root(int n) {\n\t\tif (par[n] == n) return n;\n\t\treturn par[n] = root(par[n]);\n\t}\n\tbool isUnited(int a, int b) {\n\t\treturn root(a) == root(b);\n\t}\n\tvoid unite(int a, int b) {\n\t\tint ra = root(a), rb = root(b);\n\t\tif (ra == rb) return;\n\t\tif (h[ra] < h[rb]) {\n\t\t\tpar[ra] = rb;\n\t\t}\n\t\telse if (h[ra] > h[rb]) {\n\t\t\tpar[rb] = ra;\n\t\t}\n\t\telse {\n\t\t\tpar[rb] = ra;\n\t\t\t++h[ra], ++h[rb];\n\t\t}\n\t}\n\tint hash() {\n\t\tvector<int> ids(sz);\n\t\tfor (int i = 0; i < sz; ++i) {\n\t\t\tint id = root(i);\n\t\t\tif ( h[id] == 0 ) {\n\t\t\t\tid = 0;\n\t\t\t}\n\t\t\telse {\n\t\t\t\t++id;\n\t\t\t}\n\t\t\tids[i] = id;\n\t\t}\n\t\tmap<int, int> m; m[0] = 0;\n\t\tfor (int i = 0, k = 1; i < ids.size(); ++i) {\n\t\t\tif (m.count(ids[i]) == 0) {\n\t\t\t\tm[ids[i]] = k++;\n\t\t\t}\n\t\t}\n\n\t\tfor (int i = 0; i < ids.size(); ++i) {\n\t\t\tids[i] = m[ids[i]];\n\t\t}\n\n\t\tassert( ids == from_hash(to_hash(ids)) );\n\t\treturn to_hash(ids);\n\t}\n};\n\nint get_id(P p) {\n\tif ( tbl2.count(p) == 0 ) {\n\t\tint id = tbl2.size();\n\t\ttbl2[p] = id;\n\t}\n\treturn tbl2[p];\n}\nint gcd(int a, int b) {\n\tif (b == 0) return a;\n\treturn gcd(b, a%b);\n}\nvoid add_edge(int a, int b, int id) {\n//\tcout << \"add_edge:\" << a << \" \" << b << \" \" << id << endl;\n\tG[a].push_back( (Edge){a, b, id} );\n\tG[b].push_back( (Edge){b, a, id} );\n\tedges.push_back( (Edge){a, b, id} );\n}\nstruct Data {\n\tLine l;\n\tint lid;\n\tint i, j;\n};\n\nld cross(Point a, Point b) {\n\treturn imag(conj(a) * b);\n}\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b-p, l.a-p)) < eps);\n}\nvoid input_and_make_graph() {\n\tcin >> N;\n\tG.resize(N);\n\tvector<Point> v;\n\tfor (int i = 0; i < N; ++i) {\n\t\tint x, y; cin >> x >> y;\n\t\tv.push_back( Point(x, y) );\n\t}\n\tvector<Data> dt;\n\tfor (int i = 0; i < N; ++i) {\n\t\tfor (int j = i+1; j < N; ++j) {\n\t\t\tPoint p1 = v[i], p2 = v[j];\n\t\t\tPoint m = (p1 + p2) / Point(2, 0);\n\t\t\tPoint a = (p2 - p1) * Point(0, 1);\n\t\t\tint vx = a.real(), vy = a.imag();\n\t\t\tint g = gcd(abs(vx), abs(vy)); vx /= g, vy /= g;\n\t\t\tif ((vx < 0) || (vx == 0 && vy < 0)) {\n\t\t\t\tvx *= -1, vy *= -1;\n\t\t\t}\n\t\t\tint id = get_id( P(vx, vy) );\n\t\t\tLine l(m, m+Point(vx, vy));\n\t\t\tdt.push_back( (Data){l, id, i, j} );\n\t\t}\n\t}\n\tUnionFind uf(dt.size());\n\tfor (int i = 0; i < dt.size(); ++i) {\n\t\tfor (int j = i+1; j < dt.size(); ++j) {\n\t\t\tif ( dt[i].lid == dt[j].lid && isis_lp(dt[j].l, dt[i].l.a) ) {\n\t\t\t\tuf.unite(i, j);\n\t\t\t}\n\t\t}\n\t}\n\tvector<int> ids(dt.size());\n\tfor (int i = 0; i < dt.size(); ++i) {\n\t\tids[i] = uf.root(i);\n\t}\n\t{\n\t\tvector<int> temp = ids;\n\t\tsort( temp.begin(), temp.end() );\n\t\ttemp.erase( unique(temp.begin(), temp.end()), temp.end() );\n\t\tfor (int i = 0; i < temp.size(); ++i) {\n\t\t\ttbl[temp[i]] = i;\n//\t\t\tcout << \"-\" << i << endl;\n\t\t}\n\t}\n\tfor (int i = 0; i < dt.size(); ++i) {\n\t\tadd_edge(dt[i].i, dt[i].j, tbl[ids[i]]);\n\t}\n//\t\t\tadd_edge(i, j, edge_id);\n}\n\nint dp[2][400000];\nvector< vector<int> > bucket(vector<int> v) {\n\tvector< vector<int> > res(N+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tres[v[i]].push_back(i);\n\t}\n\treturn res;\n}\nint merge(int h1, int h2) {\n//\tcout << \"merge\" << endl;\n/*\n\t{\n\t\tvector<int> v = from_hash(h1);\n\t\tcout << \"v1:\"; for (int i = 0; i < v.size(); ++i) cout << v[i] << \" \";\n\t\tcout << endl;\n\t}\n\t{\n\t\tvector<int> v = from_hash(h2);\n\t\tcout << \"v2:\"; for (int i = 0; i < v.size(); ++i) cout << v[i] << \" \";\n\t\tcout << endl;\n\t}\n*/\n\tvector< vector<int> > v1 = bucket(from_hash(h1));\n//\tcout << \"v1\" << endl;\n\tvector< vector<int> > v2 = bucket(from_hash(h2));\n\tUnionFind uf(N);\n//\tcout << \"bucket\" << endl;\n\tfor (int i = 1; i < v1.size(); ++i) {\n\t\tassert( v1[i].size() == 0 || v1[i].size() >= 2 );\n\t\tfor (int j = 1; j < v1[i].size(); ++j) {\n//\t\t\tcout << v1[i][j-1] << v1[i][j] << endl;\n\t\t\tuf.unite(v1[i][j], v1[i][j-1]);\n\t\t}\n\t}\n//\tcout << \"v2\" << endl;\n\tfor (int i = 1; i < v2.size(); ++i) {\n\t\tassert( v2[i].size() == 0 || v2[i].size() >= 2 );\n\t\tfor (int j = 1; j < v2[i].size(); ++j) {\n//\t\t\tcout << v2[i][j-1] << v2[i][j] << endl;\n\t\t\tuf.unite(v2[i][j], v2[i][j-1]);\n\t\t}\n\t}\n//\tcout << \"merge_r\" << endl;\n\treturn uf.hash();\n}\nint solve() {\n\tvector<int> h(tbl.size());\n\t{\n\t\tvector< vector<Edge> > v(tbl.size());\n\t\tfor (int i = 0; i < edges.size(); ++i) {\n\t\t\tEdge& e = edges[i];\n\t\t\tv[e.id].push_back(e);\n\t\t}\n\t\tfor (int i = 0; i < v.size(); ++i) {\n\t\t\tUnionFind uf(N);\n\t\t\tfor (int j = 0; j < v[i].size(); ++j) {\n\t\t\t\tEdge& e = v[i][j];\n\t\t\t\tuf.unite(e.from, e.to);\n\t\t\t}\n\t\t\th[i] = uf.hash();\n\t\t}\n\t}\n\n\tint maxJ = 1;\n\tfor (int i = 0; i < N; ++i) maxJ *= 5;\n\t--maxJ;\n\n\tfill(dp[0], dp[2], inf); dp[0][0] = 0;\n\tfor (int i = 0; i < h.size(); ++i) {\n\t\tfill(&dp[i+1&1][0], &dp[i+1&1][400000], inf);\n\t\tfor (int j = 0; j <= maxJ; ++j) {\n\t\t\tif (dp[i&1][j] == inf) continue;\n\t\t\tdp[i+1&1][j] = min(dp[i+1&1][j], dp[i&1][j]);\n\n//\t\t\tcout << \"!\" << i << \" \" << j << \" \" << dp[i&1][j] << endl;\n\n\t\t\tint nj = merge(j, h[i]);\n\t\t\tdp[i+1&1][nj] = min(dp[i+1&1][nj], dp[i&1][j]+1);\n//\t\t\tcout << \"h:\" << j << \" \" << h[i] << \" \" << nj << endl;\n\t\t}\n\t}\n\tvector<int> a;\n\tfor (int i = 0; i < N; ++i) a.push_back(1);\n\treturn dp[((int)h.size())&1][to_hash(a)];\n}\nint main() {\n\tinput_and_make_graph();\n\tcout << solve() << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 6304, "score_of_the_acc": -0.1542, "final_rank": 6 }, { "submission_id": "aoj_1581_1694166", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int,int> pii;\n#define REP(i,n) for(ll i=0;i<n;++i)\n#define REPR(i,n) for(ll i=1;i<n;++i)\n#define FOR(i,a,b) for(ll i=a;i<b;++i)\n\n#define DEBUG(x) cout<<#x<<\": \"<<x<<endl\n#define DEBUG_VEC(v) cout<<#v<<\":\";REP(i,v.size())cout<<\" \"<<v[i];cout<<endl\n#define ALL(a) (a).begin(),(a).end()\n\n#define MOD (ll)(1e9+7)\n#define ADD(a,b) a=((a)+(b))%MOD\n#define FIX(a) ((a)%MOD+MOD)%MOD\n\n#define N_MAX 8\n\n// int dat[N_MAX];\nvoid init(vi &dat){\n dat.assign(N_MAX,-1);\n}\nint root(vi &dat,int x){\n return dat[x]<0 ? x : (dat[x]=root(dat,dat[x]));\n}\nvoid unite(vi &dat,int x,int y){\n x=root(dat,x);\n y=root(dat,y);\n if(x!=y){\n if(dat[x]>dat[y])swap(x,y);\n dat[x] += dat[y];\n dat[y] = x;\n }\n}\nbool same(vi &dat, int x,int y){\n return root(dat,x)==root(dat,y);\n}\nint size(vi &dat, int x){\n return -dat[root(dat,x)];\n}\n\ntypedef double Real;\ntypedef complex<Real> P;\nReal dot(P a,P b){return real(conj(a)*b);}\nReal cross(P a,P b){return imag(conj(a)*b);}\n#define EPS 1e-14\n\nint n;\nP p[10];\nvector<pii> connect[10][10];\n\nint dfs(const int &cnt,vi &uf){\n if(size(uf,0) == n)return cnt;\n\n int ret = 100;\n REP(to,n){\n if(same(uf,0,to))continue;\n REP(from,n){\n if(!same(uf,0,from))continue;\n vector<pii> con = connect[from][to];\n vi nextuf = uf;\n REP(i,con.size()) unite(nextuf,con[i].first,con[i].second);\n int d = dfs(cnt+1,nextuf);\n if(d==cnt+1)return d;\n ret = min(ret,d);\n }\n }\n return ret;\n}\n\nint main(){\n cin>>n;\n REP(i,n){\n int x,y;\n cin>>x>>y;\n p[i] = P((Real)x,(Real)y);\n }\n REP(i,n)REP(j,n){\n if(i==j)continue;\n // make connect[i][j];\n P v = p[i]-p[j];\n P cv = p[j] + v/P(2,0);\n P mirror = v*P(0,1);\n REP(x,n){\n P a = p[x];\n // reflect\n a = conj((a-cv)/mirror)*mirror+cv;\n REP(y,x){\n if(abs(p[y]-a)<EPS)connect[i][j].push_back(make_pair<int,int>(x,y));\n }\n }\n }\n vi beg;\n init(beg);\n cout<<dfs(0,beg)<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 1240, "score_of_the_acc": -1.0004, "final_rank": 13 }, { "submission_id": "aoj_1581_1694121", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int,int> pii;\n#define REP(i,n) for(ll i=0;i<n;++i)\n#define REPR(i,n) for(ll i=1;i<n;++i)\n#define FOR(i,a,b) for(ll i=a;i<b;++i)\n\n#define DEBUG(x) cout<<#x<<\": \"<<x<<endl\n#define DEBUG_VEC(v) cout<<#v<<\":\";REP(i,v.size())cout<<\" \"<<v[i];cout<<endl\n#define ALL(a) (a).begin(),(a).end()\n\n#define MOD (ll)(1e9+7)\n#define ADD(a,b) a=((a)+(b))%MOD\n#define FIX(a) ((a)%MOD+MOD)%MOD\n\n#define N_MAX 8\nint g[N_MAX][N_MAX];\nvoid init(){\n REP(i,N_MAX)REP(j,N_MAX)g[i][j]=0;\n REP(i,N_MAX)g[i][i]=(rand()%2?252521:83025);\n}\nvoid unite(int x,int y){\n g[x][y] += 1;\n g[y][x] += 1;\n}\nvoid haki(int x,int y){\n g[x][y] -= 1;\n g[y][x] -= 1;\n}\n\ntypedef double Real;\ntypedef complex<Real> P;\nReal dot(P a,P b){return real(conj(a)*b);}\nReal cross(P a,P b){return imag(conj(a)*b);}\n#define EPS 1e-14\n\nint n;\nP p[10];\nvector<pii> connect[10][10];\n\nint dfs(const int &pos,const int &cnt){\n //////////\n bool h[N_MAX][N_MAX];\n REP(i,N_MAX)REP(j,N_MAX)h[i][j]=(g[i][j]>0);\n REP(k,N_MAX)REP(i,N_MAX)REP(j,N_MAX)h[i][j] |= h[i][k]&&h[k][j];\n //////////\n int sz = 0;\n REP(i,n)sz += h[pos][i];\n\n if(sz == n)return cnt;\n\n int ret = 100;\n REP(to,n){\n if(h[pos][to])continue;\n vector<pii> con = connect[pos][to];\n REP(i,con.size()) unite(con[i].first,con[i].second);\n ret = min(ret,dfs(to,cnt+1));\n REP(i,con.size()) haki(con[i].first,con[i].second);\n }\n return ret;\n}\n\nint main(){\n cin>>n;\n REP(i,n){\n int x,y;\n cin>>x>>y;\n p[i] = P((Real)x,(Real)y);\n }\n REP(i,n)REP(j,n){\n if(i==j)continue;\n // make connect[i][j];\n P v = p[i]-p[j];\n P cv = p[j] + v/P(2,0);\n P mirror = v*P(0,1);\n REP(x,n){\n P a = p[x];\n // reflect\n a = conj((a-cv)/mirror)*mirror+cv;\n REP(y,x){\n if(abs(p[y]-a)<EPS)connect[i][j].push_back(make_pair<int,int>(x,y));\n }\n }\n // REP(x,n)REP(y,x){\n // P w = p[x]-p[y];\n // if(abs(cross(v,w))<EPS){\n // P cw = p[y] + w/P(2,0);\n // if( (abs(cv-cw)<EPS)||(abs(dot(cw-cv,v))<EPS) ){\n // connect[i][j].push_back(make_pair<int,int>(x,y));\n // }\n // }\n // }\n }\n init();\n cout<<dfs(0,0)<<endl;\n return 0;\n}", "accuracy": 0.48484848484848486, "time_ms": 10, "memory_kb": 1228, "score_of_the_acc": -0.0002, "final_rank": 16 }, { "submission_id": "aoj_1581_1694037", "code_snippet": "#include<bits/stdc++.h>\n#define EQ(a,b) (abs((a)-(b)) < EPS)\nusing namespace std;\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<P,P> L;\ntypedef pair<int,int> pii;\ntypedef vector<pii> vp;\n\nconst D EPS = 1e-8;\nconst D PI = acos(-1);\n\nvoid normalize(vector<int> &v){\n map<int,int> cnt;\n int k = 0;\n for(int i=0;i<(int)v.size();i++){\n if(cnt.count(v[i]) == 0){\n cnt[v[i]] = k++;\n }\n }\n \n for(int i=0;i<(int)v.size();i++){\n v[i] = cnt[v[i]];\n }\n}\n\ninline D dot(P a, P b){\n return real(conj(a)*b);\n}\n\ninline D cross(P a, P b){\n return imag(conj(a)*b);\n}\n\ninline int ccw(P a, P b, P c){\n b -= a; c-= a;\n if(cross(b,c)>EPS) return 1;\n if(cross(b,c)<-EPS) return -1;\n return 0;\n}\n\ninline P rotate(P v, D s){\n return P(real(v)*cos(s) - imag(v)*sin(s), real(v)*sin(s) + imag(v)*cos(s) );\n}\n\ninline P norm(P p){return p*P(0,1);}\n\nmap< pair<int, vector<int> >, int> memo;\n\nint rec(int d, vector<int> use, vector<vp> &segs){\n pair< int, vector<int> > key(d,use);\n if(memo.count(key)) return memo[key];\n\n if(d == (int)segs.size()){\n /*\n for(int i=0;i<(int)use.size();i++){\n cout << use[i] << \" \";\n }\n cout << endl;\n */\n\n set<int> c;\n for(int x : use) c.insert(x);\n\n if(c.size() == 1) return 0;\n else return 100;\n }\n\n int res = rec(d+1,use,segs);\n\n for(auto p : segs[d]){\n int a = use[p.first], b = use[p.second];\n for(int i=0;i<(int)use.size();i++){\n if(use[i] == a) use[i] = b;\n }\n }\n normalize(use);\n res = min(res, rec(d+1,use,segs) + 1);\n\n return memo[key] = res;\n}\n\nint main(){\n int n;\n cin >> n;\n\n vector<P> p(n);\n for(int i=0;i<n;i++){\n int x,y;\n cin >> x >> y;\n p[i] = P(x,y);\n }\n\n vector< vp > segs;\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n P mid = (p[i]+p[j])/2.0;\n P nv = norm(p[i] - p[j]);\n L seg = make_pair(mid, mid + nv);\n\n vp ans;\n for(int k=0;k<n;k++){\n\tfor(int l=k+1;l<n;l++){\n\t P mid_c = (p[k]+p[l])/2.0;\n\t if( ccw(seg.first, seg.second, mid_c) == 0 ){\n\t if( abs( dot(seg.second - seg.first, p[k] - p[l])) < EPS){\n\t ans.push_back( make_pair(k,l) );\n\t }\n\t }\n\t}\n }\n /*\n cout << i << \" \" << j << \": \" << endl;\n for(pii a : ans){\n\tcout << a.first << \",\" << a.second << \" \";\n }\n cout << endl;\n */\n\n segs.push_back(ans);\n }\n }\n\n vector<int> use(n,0);\n for(int i=0;i<n;i++)use[i] = i;\n\n cout << rec(0,use,segs) << endl;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 7576, "score_of_the_acc": -0.1811, "final_rank": 7 } ]
aoj_1589_cpp
Problem C: Unhappy Class Problem 山之御船学園の1年G組は、不幸を背負った女子生徒たちが集まるクラスである。 彼女たちは、幸福になることを目標に、日々様々な試練に立ち向かう。 彼女たちのクラスでは、幸福になるための試練の一環として、幸福実技科目を受講することができる。 月曜から金曜のそれぞれ1限から N 限までに授業科目の枠があり、 M 個の受講可能な科目がある。 科目 i は、曜日 d i ( d i = 0, 1, 2, 3, 4がそれぞれ、月曜、火曜、水曜、木曜、金曜に対応する)の a i 限目から始まり、連続する k i コマで行われ、それを受講したときに得られる幸福度は t i である。 各生徒は、最大 L 個の科目を互いに重ならないように自由に選ぶことができる。どのように科目を選べば、最も高い幸福度を得られるだろうか。与えられた科目の情報から、得られる幸福度の最大値を求めてほしい。 Input 入力は以下の形式で与えられる。 N M L d 1 a 1 k 1 t 1 d 2 a 2 k 2 t 2 ... d M a M k M t M 1行目に3つの整数 N , M , L が空白区切りで与えられる。 2行目から M +1行目に4つの整数 d i , a i , k i , t i が空白区切りで与えられる。 Constraints 2 ≤ N ≤ 8 0 ≤ M ≤ 300 0 ≤ L ≤ min( N ×5, M ) 0 ≤ d i ≤ 4 1 ≤ a i ≤ N 1 ≤ k i a i + k i - 1 ≤ N 1 ≤ t i ≤ 100 Output 幸福度の和の最大値を1行に出力せよ。 Sample Input 1 3 7 3 0 1 1 1 0 1 1 2 1 1 3 4 1 1 1 1 1 2 1 2 2 1 1 3 2 2 2 1 Sample Output 1 9 Sample Input 2 5 10 5 0 1 1 2 0 2 1 2 0 1 2 3 1 2 1 2 1 4 2 3 2 1 1 1 2 1 1 2 3 3 2 3 4 1 1 2 4 2 1 2 Sample Output 2 13
[ { "submission_id": "aoj_1589_3865197", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <tuple>\n#include <bitset>\n#include <memory>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <iomanip>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nstruct Class {\n\tint length, satisfaction;\n};\nstd::vector<int> cal_max_schedule(const int sec, const std::vector<std::vector<Class>>& classes, std::vector<std::vector<int>>memo) {\n\tif (sec == classes.size()) return std::vector<int>(classes.size() + 1, 0);\n\tif (!memo[sec].empty()) return memo[sec];\n\tmemo[sec] = cal_max_schedule(sec + 1, classes, memo);\n\tfor (const auto& c : classes[sec]) {\n\t\tauto next = cal_max_schedule(sec + c.length, classes, memo);\n\t\tfor (auto i = 1; i < next.size(); ++i) {\n\t\t\tmemo[sec][i] = std::max(memo[sec][i], next[i - 1] + c.satisfaction);\n\t\t}\n\t}\n\treturn memo[sec];\n}\nstd::vector<int> cal_one_day_schedule(const std::vector<std::vector<Class>>& classes) {\n\treturn cal_max_schedule(0, classes, std::vector<std::vector<int>>(classes.size()));\n}\nint main() {\n\tint n, m, l; std::cin >> n >> m >> l;\n\tstd::vector<std::vector<std::vector<Class>>> classes(5, std::vector<std::vector<Class>>(n));\n\tfor (auto i = 0; i < m; ++i) {\n\t\tint d, a; std::cin >> d >> a;\n\t\tClass c; std::cin >> c.length >> c.satisfaction;\n\t\tclasses[d][a - 1].push_back(c);\n\t}\n\tstd::vector<int> max(l + 1, 0);\n\tfor (const auto& c : classes) {\n\t\tauto m = cal_one_day_schedule(c);\n\t\tfor (int i = l; i >= 0; --i) {\n\t\t\tfor (auto j = 0; j < m.size() && i + j < max.size(); ++j) {\n\t\t\t\tmax[i + j] = std::max(max[i + j], max[i] + m[j]);\n\t\t\t}\n\t\t}\n\t}\n\tstd::cout << *std::max_element(max.begin(), max.end()) << std::endl;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3180, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_1589_2642484", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n \n#define int long long\n#define pb push_back \n#define pf push_front \n#define mp make_pair\n#define fr first\n#define sc second\n#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )\n#define All(v) v.begin(), v.end()\n \ntypedef pair<int, int> Pii; typedef pair<int, Pii> Pip;\nconst int INF = 1107110711071107;\n \nint N, M, L;\nint dp[6][300][41][1 << 9];\n \nstruct S {\n int a, k, t;\n S(int a_, int k_, int t_) {\n a = a_; k = k_; t = t_;\n }\n};\n \nvector<S> D[5];\n \nint dfs(int d, int n, int l, int bit) {\n //cout << d << \" \" << n << \" \" << l << \" \" << bit << endl;;\n if ( d == 5 || l == 0 ) return 0;\n if ( D[d].size() == n ) return dfs(d+1, 0, l, 0);\n if ( dp[d][n][l][bit] ) return dp[d][n][l][bit];\n \n int ret = dfs(d, n+1, l, bit);\n int a = D[d][n].a, k = D[d][n].k, t = D[d][n].t;\n bool flag = true;\n for ( int i = a; i < a+k; i++ ) {\n if ( bit & (1 << i) ) {\n flag = false;\n break;\n }\n }\n \n if ( flag ) {\n int nbit = bit;\n for ( int i = a; i < a+k; i++ ) {\n nbit |= (1 << i); \n }\n ret = max(ret, dfs(d, n+1, l-1, nbit) + t);\n }\n \n return dp[d][n][l][bit] = ret;\n}\n \n \nsigned main() {\n cin >> N >> M >> L;\n \n Rep(i, M) {\n int d, a, k, t;\n cin >> d >> a >> k >> t;\n a--;\n D[d].pb(S(a, k, t));\n }\n //cout << endl;\n cout << dfs(0, 0, L, 0) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 28948, "score_of_the_acc": -1.1983, "final_rank": 4 }, { "submission_id": "aoj_1589_2636641", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n#define pb push_back \n#define pf push_front \n#define mp make_pair\n#define fr first\n#define sc second\n#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )\n#define All(v) v.begin(), v.end()\n\ntypedef pair<int, int> Pii; typedef pair<int, Pii> Pip;\nconst int INF = 1107110711071107;\n\nint N, M, L;\nint dp[6][300][41][1 << 9];\n\nstruct S {\n int a, k, t;\n S(int a_, int k_, int t_) {\n a = a_; k = k_; t = t_;\n }\n};\n\nvector<S> D[5];\n\nint dfs(int d, int n, int l, int bit) {\n //cout << d << \" \" << n << \" \" << l << \" \" << bit << endl;;\n if ( d == 5 || l == 0 ) return 0;\n if ( D[d].size() == n ) return dfs(d+1, 0, l, 0);\n if ( dp[d][n][l][bit] ) return dp[d][n][l][bit];\n\n int ret = dfs(d, n+1, l, bit);\n int a = D[d][n].a, k = D[d][n].k, t = D[d][n].t;\n bool flag = true;\n for ( int i = a; i < a+k; i++ ) {\n if ( bit & (1 << i) ) {\n flag = false;\n break;\n }\n }\n \n if ( flag ) {\n int nbit = bit;\n for ( int i = a; i < a+k; i++ ) {\n nbit |= (1 << i);\t\n }\n ret = max(ret, dfs(d, n+1, l-1, nbit) + t);\n }\n\n return dp[d][n][l][bit] = ret;\n}\n\n\nsigned main() {\n cin >> N >> M >> L;\n\n Rep(i, M) {\n int d, a, k, t;\n cin >> d >> a >> k >> t;\n a--;\n D[d].pb(S(a, k, t));\n }\n //cout << endl;\n cout << dfs(0, 0, L, 0) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 28992, "score_of_the_acc": -1.2, "final_rank": 5 }, { "submission_id": "aoj_1589_2558146", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define F first\n#define S second\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\n\nint dp[5][1<<8][301],dp2[6][301],d[5][301];\nint main() {\n int n,m,l;\n cin >> n >> m >> l;\n vector<PP> a[5];\n for(int i=0,x,y,z,w; i<m; i++) {\n cin >> x >> y >> z >> w;\n a[x].push_back(PP(y-1,P(z,w)));\n }\n for(int s=0; s<5; s++) {\n for(int t=0; t<(1<<n); t++) {\n for(int k=0; k<a[s].size(); k++) {\n for(int i=0; i<a[s].size(); i++) {\n int c=0;\n for(int j=a[s][i].F; j<a[s][i].F+a[s][i].S.F; j++) c|=t&(1<<j);\n if(c) continue;\n int tt=t;\n for(int j=a[s][i].F; j<a[s][i].F+a[s][i].S.F; j++) tt|=1<<j;\n dp[s][tt][k+1]=max(dp[s][tt][k+1],dp[s][t][k]+a[s][i].S.S);\n }\n }\n }\n }\n for(int i=0; i<5; i++) {\n for(int t=0; t<(1<<n); t++) {\n for(int j=0; j<=a[i].size(); j++) d[i][j]=max(d[i][j],dp[i][t][j]);\n }\n }\n for(int i=0; i<5; i++) {\n for(int k=0; k<=m; k++) {\n for(int j=0; j<=a[i].size(); j++) {\n if(k+j<301) dp2[i+1][k+j]=max(dp2[i+1][k+j],dp2[i][k]+d[i][j]);\n }\n }\n }\n int ans=0;\n for(int i=0; i<=l; i++) ans=max(ans,dp2[5][i]);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4592, "score_of_the_acc": -0.0547, "final_rank": 2 }, { "submission_id": "aoj_1589_2032071", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <sstream>\n#include <string>\n#include <complex>\n#include <cstdio>\nusing namespace std;\n\nint dp2[6][310] = {};\n\nint item[5][256];\nint main(){\n\tint N,M,L;\n\tcin >> N >> M >> L;\n\t\n\tfor(int i = 0 ; i < M ; i++){\n\t\tint d,a,k,t;\n\t\tcin >> d >> a >> k >> t;\n\t\t--a;\n\t\tint x = (1<<k) - 1;\n\t\tx = x << a;\n\t\titem[d][x] = max(item[d][x],t);\n\t}\n\tint ans = 0;\n\tfor(int i = 0 ; i < 5 ; i++){\n\t\tint dp[256][300] = {};\n\t\t\n\t\tfor(int j = 0 ; j < 256 ; j++){\n\t\t\tfor(int k = 0 ; k < 256 ; k++){\n\t\t\t\tif( j & k ) continue;\n\t\t\t\tfor(int l = 0 ; l <= 256 ; l++){\n\t\t\t\t \tdp[j|k][l+1] = max(dp[j|k][l+1],dp[k][l] + item[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint s[310] = {};\n\t\t\n\t\tfor(int l = 0 ; l <= 300 ; l++)\n\t\t\tfor(int j = 0 ; j < 256 ; j++)\n\t\t\t\ts[l] = max(s[l],dp[j][l]);\n\n\t\tfor(int l = 0 ; l <= 300 ; l++){\n\t\t\tfor(int j = 0 ; j < 300 ; j++){\n\t\t\t\tif( l + j <= 300 ){\n\t\t\t\t\tdp2[i+1][l+j] = max(dp2[i+1][l+j], dp2[i][j] + s[l]);\n\t\t\t\t} \n\t\t\t}\n\t\t}\n\n\t}\n\tcout << dp2[5][L] << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3388, "score_of_the_acc": -0.0081, "final_rank": 1 } ]
aoj_1588_cpp
Problem B: Potatoes Problem がっちょ君は N 面の畑と M 個の芋を所有している。各畑にはそれぞれ1から N までの番号が付けられている。がっちょ君は畑に芋を植え収穫することで、芋の数を増やしたいと考えている。 がっちょ君は一人暮らしであり、1人では K 面までの畑しか管理することができない。また、各畑の土の状態や面積は様々で、芋の収穫数や植えることのできる芋の数も様々である。芋を畑 i に植えた場合、1年後には畑 i に植えた芋1つにつき a i 個の芋を収穫することができる。ただし、畑 i には最大でも b i 個の芋しか植えることができない。 がっちょ君が K 面以内の畑に M 個以内の芋を植えた時、1年後に所有することができる芋の数の最大値を求めてほしい。 Input 入力は以下の形式で与えられる。 N M K a 1 a 2 ... a N b 1 b 2 ... b N 1行目に3個の整数 N , M , K が空白区切りで与えられる。 2行目に N 個の整数 a i が空白区切りで与えられる。 3行目に N 個の整数 b i が空白区切りで与えられる。 Constraints 1 ≤ N ≤ 15 1 ≤ M ≤ 10 4 1 ≤ K ≤ min( N ,3) 1 ≤ a i ≤ 10 3 1 ≤ b i ≤ 10 4 Output 芋の数の最大値を1行で出力せよ。 Sample Input 1 5 100 3 2 3 4 5 6 50 40 20 10 5 Sample Output 1 280 畑1に40個、畑2に40個、畑3に20個植えることで収穫後に280個の芋を所有することができる。 Sample Input 2 5 100 3 2 3 4 5 100 50 40 20 10 1 Sample Output 2 339 畑2に40個、畑3に20個、畑5に1個植えることで300個の芋を収穫することができ、植えなかった芋と合わせて答えは339になる。
[ { "submission_id": "aoj_1588_3807608", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1588.cc: Potatoes\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 15;\nconst int MAX_M = 10000;\nconst int MAX_K = 3;\n\n/* typedef */\n\n/* global variables */\n\nint as[MAX_N], bs[MAX_N];\nint dp[MAX_N + 1][MAX_K + 1][MAX_M + 1];\n\n/* subroutines */\n\ninline void setmax(int &a, int b) { if (a < b) a = b; }\n\n/* main */\n\nint main() {\n int n, m, k;\n scanf(\"%d%d%d\", &n, &m, &k);\n\n for (int i = 0; i < n; i++) scanf(\"%d\", as + i);\n for (int i = 0; i < n; i++) scanf(\"%d\", bs + i);\n\n memset(dp, -1, sizeof(dp));\n dp[0][0][0] = 0;\n\n for (int i = 0; i < n; i++) {\n int ai = as[i], bi = bs[i];\n memcpy(dp[i + 1], dp[i], sizeof(dp[i]));\n\n for (int j = 0; j < k; j++)\n for (int l = 0; l < m; l++)\n\tif (dp[i][j][l] >= 0) {\n\t for (int c = 1; c <= bi && l + c <= m; c++)\n\t setmax(dp[i + 1][j + 1][l + c], dp[i][j][l] + c * ai);\n\t}\n }\n\n int maxd = 0;\n for (int l = 0; l <= m; l++)\n setmax(maxd, dp[n][k][l] + (m - l));\n\n printf(\"%d\\n\", maxd);\n return 0;\n}", "accuracy": 0.13636363636363635, "time_ms": 1000, "memory_kb": 5792, "score_of_the_acc": -1.2108, "final_rank": 7 }, { "submission_id": "aoj_1588_2570404", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n#define pb push_back \n#define pf push_front \n#define mp make_pair\n#define fr first\n#define sc second\n#define Rep(i,n) for(int i=0;i<(n);i++)\n#define All(v) v.begin(),v.end()\n\ntypedef pair<int, int> Pii; typedef pair<int, Pii> Pip;\nconst int INF = 1107110711071107;\n\nint N, M, K;\nvector<Pii> ab;\nint a[16], b[16];\nint dp[16][10001][16];\n\nint dfs(int n, int m, int k) {\n if ( n == N || k == K || m == 0 ) return m;\n if ( dp[n][m][k] >= 0 ) return dp[n][m][k];\n\n int a = ab[n].fr, b = ab[n].sc;\n return dp[n][m][k] = max(dfs(n+1, max(0LL, m-b), k+1) + min(m, b)*a, dfs(n+1, m, k));\n}\n\nsigned main() {\n memset(dp, -1, sizeof(dp));\n cin >> N >> M >> K;\n Rep(i, N) cin >> a[i];\n Rep(i, N) cin >> b[i];\n \n Rep(i, N) {\n ab.pb(Pii(a[i], b[i]));\n }\n \n sort(All(ab), greater<Pii>());\n\n cout << dfs(0, M, 0) << endl;\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 23108, "score_of_the_acc": -1, "final_rank": 6 }, { "submission_id": "aoj_1588_2032028", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <sstream>\n#include <string>\n#include <complex>\n#include <cstdio>\nusing namespace std;\n\npair<int,int> a[15];\n\nint main(){\n\tint n,m,K;\n\tcin >> n >> m >> K;\n\tlong long sum = 0;\n\tfor(int i = 0 ; i < n ; i++) cin >> a[i].first;\n\tfor(int i = 0 ; i < n ; i++) cin >> a[i].second;\n\tlong long w = 0;\n\tfor(int k = 0 ; k < (1<<n) ; k++){\n\t\tif( __builtin_popcount(k) > K ) continue;\n\t\tvector<int> v;\n\n\t\tfor(int i = 0 ; i < n ; i++){\n\t\t\tif( k >> i & 1 )\n\t\t\t\tfor(int j = 0 ; j < a[i].second ; j++)\n\t\t\t\t\tv.push_back(a[i].first);\n\t\t}\n\t\tsort(v.rbegin(),v.rend());\n\n\t\tlong long s = m - min<int>(v.size(),m);\n\t\tfor(int i = 0 ; i < min<int>(v.size(),m) ; i++){\n\t\t\t//cout << v[i] << endl;\n\t\t\ts += v[i];\n\t\t}\n\t\tw = max(w,s);\n\n\t}\n\tcout << w << endl;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3264, "score_of_the_acc": -0.2167, "final_rank": 4 }, { "submission_id": "aoj_1588_2002639", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long int64;\n\nint main()\n{\n int N, M, K, A[15], B[15], C[15];\n cin >> N >> M >> K;\n for(int i = 0; i < N; i++) {\n cin >> A[i];\n }\n for(int i = 0; i < N; i++) {\n cin >> B[i];\n }\n int ret = 0;\n for(int k = 0; k < (1 << N); k++) {\n if(__builtin_popcount(k) > K) continue;\n int cost = 0;\n for(int i = 0; i < N; i++) {\n C[i] = B[i];\n }\n int foo = M;\n while(foo > 0) {\n int curr = 0;\n for(int i = 0; i < N; i++) {\n if((k >> i) & 1 && C[i] > 0) {\n curr = max(curr, A[i]);\n }\n }\n if(curr == 0) break;\n cost += curr;\n for(int i = 0; i < N; i++) {\n if((k >> i) & 1 && C[i] > 0 && A[i] == curr) {\n --C[i];\n --foo;\n break;\n }\n }\n }\n ret = max(ret, cost + foo);\n }\n cout << ret << endl;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 1168, "score_of_the_acc": -0.101, "final_rank": 3 }, { "submission_id": "aoj_1588_2002462", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <set>\n\nusing namespace std;\ntypedef pair<int,int> P;\n\nint a[15];\nint b[15];\nint dp[16][10001]; //[????????°][????????°]->??????\nset<P> S;\n\nint main(){\n int N,M,K;\n cin>>N>>M>>K;\n for(int i=0;i<N;i++){\n cin>>a[i];\n }\n for(int i=0;i<N;i++){\n cin>>b[i];\n }\n\n for(int i=1;i<=N;i++){\n int ai=a[i-1],bi=b[i-1];\n for(int k=i;k>0;k--){\n S.clear();\n for(int n=0;n<bi;n++) S.insert(P(dp[k-1][10000-n]+n*ai,10000-n));\n for(int m=10000;m>=0;m--){\n S.erase(P(dp[k-1][m+1]+(10000-m-1)*ai,m+1));\n if(m-bi>=0) S.insert(P(dp[k-1][m-bi]+(10000-m+bi)*ai,m-bi));\n int maximum=(*S.rbegin()).first-(10000-((*S.rbegin()).second))*ai+(m-(*S.rbegin()).second)*ai;\n dp[k][m]=max(dp[k][m],maximum);\n }\n }\n }\n int ans=0;\n for(int m=0;m<=M;m++) ans=max(ans,dp[K][m]+M-m);\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 2248, "score_of_the_acc": -0.2209, "final_rank": 5 }, { "submission_id": "aoj_1588_2002434", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <cmath>\n#include <climits>\n#include <iostream>\n#include <vector>\n#include <string>\n#include <sstream>\n#include <algorithm>\n#include <utility>\n#include <set>\n#include <map>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <functional>\n\nusing namespace std;\n\n#define int long long\n\n#define fst first\n#define scd second\n#define PB push_back\n#define MP make_pair\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n#define omajinai ios::sync_with_stdio(false);cin.tie(0)\n#define rep(i,x) for(int i=0;i<(int)(x);++i)\n#define rep1(i,x) for(int i=1;i<=(int)(x);++i)\n\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef pair<int, int> pii;\ntypedef vector<pii> vpii;\n\ntemplate<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}\ntemplate<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}\ntemplate<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}\ntemplate<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}\ntemplate<typename T>T get(){T a;cin>>a;return a;}\ntemplate<typename T>T rev(T a){reverse(all(a));return a;}\ntemplate<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}\n\nconst int inf = 1e9;\nconst ll linf = 3e18;\nconst double eps = 1e-9;\n\nint a[20], b[20];\n\nsigned main()\n{ // #define int long long ?????????????????§scanf???????????¨???????????????????????\\?????????????????????\n int N, M, K; cin >> N >> M >> K;\n\n rep(i, N) cin >> a[i];\n rep(i, N) cin >> b[i];\n\n int ma = 0;\n\n rep(bit, 1<<N) {\n int cnt = 0;\n vpii choice;\n rep(i, N) {\n if (bit >> i & 1) cnt += 1, choice.PB(pii(a[i], b[i]));\n }\n\n if (cnt != K) continue;\n\n sort(all(choice), greater<pii>());\n\n int sum = 0;\n\n int m = M;\n\n rep(i, K) {\n sum += choice[i].fst * min(m, choice[i].scd);\n m -= min(m, choice[i].scd);\n }\n\n sum += m;\n\n chmax(ma, sum);\n }\n\n cout << ma << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1200, "score_of_the_acc": -0.0015, "final_rank": 1 }, { "submission_id": "aoj_1588_2002387", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < n; i++)\nconst double PI = 3.1415926535897932384626433832795028841971;\nconst int INF = 100000000;\nconst double EPS = 1e-10;\nconst int MOD = 1000000007;\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nint n, m, k;\nint a[15], b[15];\nP c[15];\n\nint main(){\n\tcin >> n >> m >> k;\n rep(i,n) cin >> a[i];\n rep(i,n) cin >> b[i];\n rep(i,n) c[i] = P(a[i],b[i]);\n int ans = 0;\n sort(c,c+n);\n rep(i,(1<<n)){\n int fl = 0, ii = i;\n vector<P> tmp;\n rep(j,n){\n if(ii&1){\n fl++;\n tmp.push_back(c[j]);\n }\n ii >>= 1;\n }\n if(fl > k) continue;\n sort(tmp.begin(),tmp.end(),greater<P>());\n int mm = m, cnt = 0;\n rep(j,tmp.size()){\n cnt += tmp[j].first*min(mm,tmp[j].second);\n mm -= tmp[j].second;\n if(mm <= 0) break;\n }\n ans = max(ans,cnt+max(mm,0));\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 1204, "score_of_the_acc": -0.0016, "final_rank": 2 } ]
aoj_1593_cpp
Problem G: Star Problem 半径1の円に内接する正 N / K 角形の面積を求めよ。 ただし、正 N / K 角形を 「円周上に等間隔に N 個の点を取り、 K -1個おきにそれぞれの点を結んだ一番外側の図形」 と定義する。 例えば、5/2角形は次のように描くことができる。 まず、半径1の円周上に等間隔に5つの点を取る。 次に、それぞれの点を2-1=1つおきに結ぶ。 一番外側の図形が正5/2角形になる。 Input 入力は以下の形式で与えられる。 N K 2つの整数 N , K が1行に与えられる。 Constraints 入力は以下の制約を満たす。 5 ≤ N ≤ 10 6 1 < K < N /2 N , K は互いに素である整数 Output 半径1の円に内接する正 N / K 角形の面積を1行に出力せよ。10 -5 以下の誤差が許容される。 Sample Input 1 5 2 Sample Output 1 1.12256994 正5/2角形は上記の図形である。 Sample Input 2 20 3 Sample Output 2 2.93114293 正20/3角形は下のような図形である。 Sample Input 3 7 3 Sample Output 3 1.08395920 Sample Input 4 100000 3 Sample Output 4 3.14159265
[ { "submission_id": "aoj_1593_10848492", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << \"(\" << o.first << \",\" << o.second << \")\"; return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << \" \";} return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }\ntemplate<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << \"{ \"; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << \":\" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << \", \"; } out << \" }\"; return out; }\n\n/*\n <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1593>\n 問題文============================================================\n =================================================================\n 解説=============================================================\n ================================================================\n */\n\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld eps = 1e-9, pi = acos(-1.0);\nnamespace std {\n bool operator<(const Point &lhs, const Point &rhs) {\n if (lhs.real() < rhs.real() - eps) return true;\n if (lhs.real() > rhs.real() + eps) return false;\n return lhs.imag() < rhs.imag();\n }\n}\nPoint input_point() { ld x, y; cin >> x >> y; return Point(x, y); } // 点の入力\nbool eq(ld a, ld b) { return (abs(a - b) < eps); } // 誤差つき等号判定\nld dot(Point a, Point b) { return real(conj(a) * b); } // 内積\nld cross(Point a, Point b) { return imag(conj(a) * b); } // 外積\n\n// 直線の定義\nclass Line {\npublic:\n Point a, b;\n Line() : a(Point(0, 0)), b(Point(0, 0)) {}\n Line(Point a, Point b) : a(a), b(b) {}\n Point operator[](const int _num) {\n if (_num == 0)return a;\n else if (_num == 1)return b;\n else assert(false);\n }\n};\n\n// 円の定義\nclass Circle {\npublic:\n Point p;\n ld r;\n Circle() : p(Point(0, 0)), r(0) {}\n Circle(Point p, ld r) : p(p), r(r) {}\n};\n\n// 垂線の足\nPoint proj(Line l, Point p) {\n ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + t * (l.a - l.b);\n}\n\n// CCW\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b, c) > eps) return 1; // a,b,cが反時計周りの順に並ぶ\n if (cross(b, c) < -eps) return -1; // a,b,cが時計周りの順に並ぶ\n if (dot(b, c) < 0) return 2; // c,a,bの順に直線に並ぶ\n if (norm(b) < norm(c)) return -2; // a,b,cの順に直線に並ぶ\n return 0; // a,c,bの順に直線に並ぶ\n}\n\n/* 交差判定 */\n// 直線と直線の交差判定\nbool isis_ll(Line l, Line m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); }\n// 直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n return isis_ll(l, s) &&\n (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n// 線分と線分の交差判定\nbool isis_ss(Line s, Line t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n// 点の直線上判定\nbool isis_lp(Line l, Point p) { return (abs(cross(l.b - p, l.a - p)) < eps); }\n// 点の線分上判定\nbool isis_sp(Line s, Point p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); }\n\n/* 距離 */\n// 直線と直線の交点\nPoint is_ll(Line s, Line t) {\n Point sv = s.b - s.a, tv = t.b - t.a;\n assert(cross(sv, tv) != 0);\n return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\n// 直線と点の距離\nld dist_lp(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\n// 直線と直線の距離\nld dist_ll(Line l, Line m) {\n return isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// 直線と線分の距離\nld dist_ls(Line l, Line s) {\n return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// 線分と点の距離\nld dist_sp(Line s, Point p) {\n Point r = proj(s, p);\n return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nld dist_ss(Line s, Line t) {\n if (isis_ss(s, t)) return 0;\n return min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n/* 円 */\n// 円と円の交点\nvector<Point> is_cc(Circle c1, Circle c2) {\n vector<Point> res;\n ld d = abs(c1.p - c2.p);\n ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n ld dfr = c1.r * c1.r - rc * rc;\n if (abs(dfr) < eps) dfr = 0.0;\n else if (dfr < 0.0) return res; // no intersection\n ld rs = sqrt(dfr);\n Point diff = (c2.p - c1.p) / d;\n res.push_back(c1.p + diff * Point(rc, rs));\n if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n return res;\n}\n// 円と直線の交点\nvector<Point> is_lc(Circle c, Line l) {\n vector<Point> res;\n ld d = dist_lp(l, c.p);\n if (d < c.r + eps) {\n ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n Point nor = (l.a - l.b) / abs(l.a - l.b);\n res.push_back(proj(l, c.p) + len * nor);\n res.push_back(proj(l, c.p) - len * nor);\n }\n return res;\n}\n// 円と線分の距離\nvector<Point> is_sc(Circle c, Line l) {\n vector<Point> v = is_lc(c, l), res;\n for (Point p : v)\n if (isis_sp(l, p)) res.push_back(p);\n return res;\n}\n// 円と点の接線\nvector<Line> tangent_cp(Circle c, Point p) {\n vector<Line> ret;\n Point v = c.p - p;\n ld d = abs(v);\n ld l = sqrt(norm(v) - c.r * c.r);\n if (isnan(l)) { return ret; }\n Point v1 = v * Point(l / d, c.r / d);\n Point v2 = v * Point(l / d, -c.r / d);\n ret.push_back(Line(p, p + v1));\n if (l < eps) return ret;\n ret.push_back(Line(p, p + v2));\n return ret;\n}\n// 円と円の接線\nvector<Line> tangent_cc(Circle c1, Circle c2) {\n vector<Line> ret;\n if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n ret = tangent_cp(c1, center);\n }\n if (abs(c1.r - c2.r) > eps) {\n Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n vector<Line> nret = tangent_cp(c1, out);\n ret.insert(ret.end(), nret.begin(), nret.end());\n }\n else {\n Point v = c2.p - c1.p;\n v /= abs(v);\n Point q1 = c1.p + v * Point(0, 1) * c1.r;\n Point q2 = c1.p + v * Point(0, -1) * c1.r;\n ret.push_back(Line(q1, q1 + v));\n ret.push_back(Line(q2, q2 + v));\n }\n return ret;\n}\n\n/* 多角形 */\ntypedef vector<Point> Polygon;\n// 面積\nld area(const Polygon &p) {\n ld res = 0;\n int n = (int)p.size();\n for (int j = 0;j < n;j++) res += cross(p[j], p[(j + 1) % n]);\n return res / 2;\n}\n// 多角形の回転方向\nbool is_counter_clockwise(const Polygon &poly) {\n ld angle = 0;\n int n = (int)poly.size();\n for (int i = 0;i < n;i++) {\n Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n angle += arg((c - b) / (b - a));\n }\n return angle > eps;\n}\n// 凸性判定\nbool isConvex(const Polygon &poly){\n int n = (int)poly.size();\n if(n < 3) return false;\n int s = -3;\n for(int i = 0; i < n;i++){\n int r = ccw(poly[(i+n-1)%n],poly[i%n],poly[(i+1)%n]);\n if(r == 1 && s == -3) s = r;\n if(s*r == -1) return false;\n }\n return true;\n}\n// 点の内外判定\n// 0 => out : 1 => on : 2 => in\nint is_in_polygon(const Polygon &poly, Point p) {\n ld angle = 0;\n int n = (int)poly.size();\n for (int i = 0;i < n;i++) {\n Point a = poly[i], b = poly[(i + 1) % n];\n if (isis_sp(Line(a, b), p)) return 1;\n angle += arg((b - p) / (a - p));\n }\n return eq(angle, 0) ? 0 : 2;\n}\n\n// 凸包 : 凸多角形のある一辺上にある点を含まない\nPolygon convex_hull(vector<Point> ps) {\n int n = (int)ps.size();\n int k = 0;\n sort(ps.begin(), ps.end());\n Polygon ch(2 * n);\n for (int i = 0; i < n; ch[k++] = ps[i++])\n while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n ch.resize(k - 1);\n return ch;\n}\n// 凸包 : 凸多角形のある一辺上にある点も含む\nPolygon convex_hull2(vector<Point> ps) {\n int n = (int)ps.size();\n if (n < 3) return ps;\n sort(ps.begin(), ps.end());\n Polygon u = { ps[0], ps[1] }, l = { ps[n - 1],ps[n - 2] };\n for (int i = 2; i < n; i++) {\n for (int j = (int)u.size(); j >= 2 && ccw(u[j - 2], u[j - 1], ps[i]) >= 0;j--)u.pop_back();\n u.push_back(ps[i]);\n }\n for (int i = n - 3;i >= 0;i--) {\n for (int j = (int)l.size(); j >= 2 && ccw(l[j - 2], l[j - 1], ps[i]) >= 0;j--)l.pop_back();\n l.push_back(ps[i]);\n }\n reverse(l.begin(), l.end());\n for (int i = (int)u.size() - 2; i >= 1; i--)l.push_back(u[i]);\n return l;\n}\n\n// 凸多角形の直径\npair<pll,ld> convex_diameter(const Polygon& poly){\n int n = (int)poly.size();\n if(n == 2) return make_pair(pll(0,1),abs(poly[0]-poly[1]));\n int ii = 0, jj = 0;\n for(int i = 1;i < n;i++){\n if(poly[i].imag() > poly[ii].imag())ii = i;\n if(poly[i].imag() < poly[jj].imag())jj = i;\n }\n pll resp = make_pair(-1,-1);\n ld resd = 0;\n \n int i, maxi,j,maxj;\n i = maxi = ii; j = maxj = jj;\n while(i != maxj || j != maxi){\n ld cur = abs(poly[i] - poly[j]);\n if(resd + eps < cur){ resd = cur; resp = pll(i,j); }\n int di = (i+1)%n, dj = (j+1)%n;\n if(cross(poly[di]-poly[i],poly[dj]-poly[j]) < 0) i = di;\n else j = dj;\n }\n return make_pair(resp,resd);\n}\n// 凸カット\nPolygon convex_cut(const Polygon &ps, Line l) {\n int n = (int)ps.size();\n Polygon Q;\n for (int i = 0;i < n;i++) {\n Point A = ps[i], B = ps[(i + 1) % n];\n Line m = Line(A, B);\n if (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m))\n Q.push_back(is_ll(l, m));\n }\n return Q;\n}\n\n// 円と多角形の共通部分\ndouble area_of_polygon_and_circle(const Polygon& poly, const Circle c) {\n int n = (int)poly.size();\n ld r = 0;\n for (int i = n - 1, j = 0; j < n; i=j++) {\n Point v = abs(poly[j] - poly[i]) / (poly[j] - poly[i]);\n if (poly[j] == poly[i])continue;\n assert(poly[j] != poly[i]);\n Point a = (poly[i] - c.p)*v, b = (poly[j] - c.p)*v;\n ld d = norm(c.r) - norm(a.imag());\n if (abs(a.imag()) < eps) continue;\n if (d < 0)d = 0;\n d = sqrt(d);\n double l, m;\n r += norm(c.r)*((l = atan2(b.imag(), min(b.real(), -d)) - atan2(a.imag(), min(a.real(), -d)))\n + (m = atan2(b.imag(), max(b.real(), d)) - atan2(a.imag(), max(a.real(), d))))\n + a.imag()*(min(d, max(a.real(), -d)) - max(-d, min(b.real(), d)));\n assert(-pi < l && -pi < m && l < pi && m < pi);\n }\n return r / 2;\n}\nld solve(){\n ld res = pi;\n int N,K; cin >> N >> K;\n \n Polygon ps(N);\n ld theta = 0;\n ld diff = 2*pi/N;\n for(int i = 0; i < N;i++){\n ps[i] = Point(cos(theta),sin(theta));\n theta += diff;\n }\n \n Line l0(ps[0],ps[K]),l1(ps[1],ps[(1-K+N)%N]);\n Point p = is_ll(l0,l1);\n Polygon A = {ps[0],p,ps[1]};\n ld S1 = abs(area(A));\n ld S2 = pi/N - sin(diff)/2;\n res -= N*(S1+S2);\n return res;\n}\nint main(void) {\n cin.tie(0); ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(12) << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 34184, "score_of_the_acc": -1.3308, "final_rank": 4 }, { "submission_id": "aoj_1593_3689473", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ld = long double;\nusing Point = std::complex<ld>;\n\nconst ld eps = 1e-9, pi = acos(-1.0);\n\nnamespace std\n{\nbool operator<(const Point &lhs, const Point &rhs)\n{\n if (lhs.real() < rhs.real() - eps)\n return true;\n if (lhs.real() > rhs.real() + eps)\n return false;\n return lhs.imag() < rhs.imag();\n}\n} // namespace std\n\nPoint input_point()\n{\n ld x, y;\n std::cin >> x >> y;\n return Point(x, y);\n}\n\nbool eq(ld a, ld b)\n{\n return (abs(a - b) < eps);\n}\n\nld dot(Point a, Point b)\n{\n return real(conj(a) * b);\n}\n\nld cross(Point a, Point b)\n{\n return imag(conj(a) * b);\n}\n\n// CCW::counter clockwise\nint ccw(Point a, Point b, Point c)\n{\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // a,b,c : counter-clockwise\n if (cross(b, c) < -eps)\n return -1; // a,b,c : clockwise\n if (dot(b, c) < 0)\n return 2; // c,a,b : on a line\n if (norm(b) < norm(c))\n return -2; // a,b,c : on a line\n return 0; // a,c,b : on a line\n}\n\nclass Line\n{\npublic:\n Point a, b;\n Line() : a(Point(0, 0)), b(Point(0, 0)) {}\n Line(Point a, Point b) : a(a), b(b) {}\n};\n\nld dot(Line l, Line m)\n{\n return dot((l.a - l.b), (m.a - m.b));\n}\n\n// l:line, m:line が交点を持つか\nbool isis_ll(Line l, Line m)\n{\n return !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// l:line, s:segment\nbool isis_ls(Line l, Line s)\n{\n return isis_ll(l, s) &&\n (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// s:segment, t:segment\nbool isis_ss(Line s, Line t)\n{\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// p が l:line 上に存在するか\nbool isis_lp(Line l, Point p)\n{\n return (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\nbool isis_sp(Line s, Point p)\n{\n return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// p から l に下ろした足との交点\nPoint proj(Line l, Point p)\n{\n ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + t * (l.a - l.b);\n}\n\n// l:line, t:line の交点\nPoint is_ll(Line l, Line m)\n{\n Point lv = l.b - l.a, mv = m.b - m.a;\n assert(cross(lv, mv) != 0);\n return l.a + lv * cross(mv, m.a - l.a) / cross(mv, lv);\n}\n\n// p, l:line の距離\nld dist_lp(Line l, Point p)\n{\n return abs(p - proj(l, p));\n}\n\nld dist_ll(Line l, Line m)\n{\n return isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\nld dist_ls(Line l, Line s)\n{\n return isis_ls(l, s) ? 0 : std::min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\nld dist_sp(Line s, Point p)\n{\n Point r = proj(s, p);\n return isis_sp(s, r) ? abs(r - p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\nld dist_ss(Line s, Line t)\n{\n if (isis_ss(s, t))\n return 0;\n return std::min({dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});\n}\n\n// a, b の垂直二等分線. a -> b を90度反時計回り回転\nLine bisector(Point a, Point b)\n{\n Point mid = (a + b) * Point(0.5, 0);\n return Line(mid, mid + (b - a) * Point(0, pi / 2));\n}\n\n// 直線 l, m のなす角を求める\nld degree_ll(Line l, Line m)\n{\n ld cos_shita = dot(l, m) / (abs(l.b - l.a) * abs(m.b - m.a));\n if (cos_shita < -1.0)\n cos_shita = -1.0;\n if (cos_shita > 1.0)\n cos_shita = 1.0;\n ld shita = acos(cos_shita);\n // shita = sita * 180.0 / PI;\n return shita;\n}\n\nusing Polygon = std::vector<Point>;\n\nld area(const Polygon &p)\n{\n ld res = 0;\n int n = p.size();\n for (int i = 0; i < n; i++)\n {\n res += cross(p[i], p[(i + 1) % n]);\n }\n return res / 2;\n}\n\ntemplate <typename T>\nvoid printv(const vector<T> &v)\n{\n int sz = v.size();\n for (int i = 0; i < sz; i++)\n {\n cout << v[i] << \" \\n\"[i == sz - 1];\n }\n}\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int n, k;\n cin >> n >> k;\n vector<Point> ps;\n for (int i = 0; i < n; i++)\n {\n ld x = cos(2 * pi * i / n), y = sin(2 * pi * i / n);\n ps.push_back(Point(x, y));\n }\n Line l0 = Line(ps[0], ps[k]);\n Line l1 = Line(ps[1], ps[n - k + 1]);\n Point p = is_ll(l0, l1);\n //cout << p.real() << \" \" << p.imag() << endl;\n Polygon poly = {Point(0, 0), ps[0], p};\n cout << fixed << setprecision(10) << area(poly) * 2 * n << endl;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 36260, "score_of_the_acc": -1.5566, "final_rank": 6 }, { "submission_id": "aoj_1593_3061817", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << \"(\" << o.first << \",\" << o.second << \")\"; return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << \" \";} return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }\ntemplate<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << \"{ \"; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << \":\" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << \", \"; } out << \" }\"; return out; }\n\n/*\n <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1593>\n 問題文============================================================\n =================================================================\n 解説=============================================================\n ================================================================\n */\n\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld eps = 1e-9, pi = acos(-1.0);\nnamespace std {\n bool operator<(const Point &lhs, const Point &rhs) {\n if (lhs.real() < rhs.real() - eps) return true;\n if (lhs.real() > rhs.real() + eps) return false;\n return lhs.imag() < rhs.imag();\n }\n}\nPoint input_point() { ld x, y; cin >> x >> y; return Point(x, y); } // 点の入力\nbool eq(ld a, ld b) { return (abs(a - b) < eps); } // 誤差つき等号判定\nld dot(Point a, Point b) { return real(conj(a) * b); } // 内積\nld cross(Point a, Point b) { return imag(conj(a) * b); } // 外積\n\n// 直線の定義\nclass Line {\npublic:\n Point a, b;\n Line() : a(Point(0, 0)), b(Point(0, 0)) {}\n Line(Point a, Point b) : a(a), b(b) {}\n Point operator[](const int _num) {\n if (_num == 0)return a;\n else if (_num == 1)return b;\n else assert(false);\n }\n};\n\n// 円の定義\nclass Circle {\npublic:\n Point p;\n ld r;\n Circle() : p(Point(0, 0)), r(0) {}\n Circle(Point p, ld r) : p(p), r(r) {}\n};\n\n// 垂線の足\nPoint proj(Line l, Point p) {\n ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + t * (l.a - l.b);\n}\n\n// CCW\nint ccw(Point a, Point b, Point c) {\n b -= a; c -= a;\n if (cross(b, c) > eps) return 1; // a,b,cが反時計周りの順に並ぶ\n if (cross(b, c) < -eps) return -1; // a,b,cが時計周りの順に並ぶ\n if (dot(b, c) < 0) return 2; // c,a,bの順に直線に並ぶ\n if (norm(b) < norm(c)) return -2; // a,b,cの順に直線に並ぶ\n return 0; // a,c,bの順に直線に並ぶ\n}\n\n/* 交差判定 */\n// 直線と直線の交差判定\nbool isis_ll(Line l, Line m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); }\n// 直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n return isis_ll(l, s) &&\n (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n// 線分と線分の交差判定\nbool isis_ss(Line s, Line t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n// 点の直線上判定\nbool isis_lp(Line l, Point p) { return (abs(cross(l.b - p, l.a - p)) < eps); }\n// 点の線分上判定\nbool isis_sp(Line s, Point p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); }\n\n/* 距離 */\n// 直線と直線の交点\nPoint is_ll(Line s, Line t) {\n Point sv = s.b - s.a, tv = t.b - t.a;\n assert(cross(sv, tv) != 0);\n return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\n// 直線と点の距離\nld dist_lp(Line l, Point p) {\n return abs(p - proj(l, p));\n}\n\n// 直線と直線の距離\nld dist_ll(Line l, Line m) {\n return isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\n// 直線と線分の距離\nld dist_ls(Line l, Line s) {\n return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\n// 線分と点の距離\nld dist_sp(Line s, Point p) {\n Point r = proj(s, p);\n return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));\n}\n\n// 線分と線分の距離\nld dist_ss(Line s, Line t) {\n if (isis_ss(s, t)) return 0;\n return min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });\n}\n\n/* 円 */\n// 円と円の交点\nvector<Point> is_cc(Circle c1, Circle c2) {\n vector<Point> res;\n ld d = abs(c1.p - c2.p);\n ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n ld dfr = c1.r * c1.r - rc * rc;\n if (abs(dfr) < eps) dfr = 0.0;\n else if (dfr < 0.0) return res; // no intersection\n ld rs = sqrt(dfr);\n Point diff = (c2.p - c1.p) / d;\n res.push_back(c1.p + diff * Point(rc, rs));\n if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));\n return res;\n}\n// 円と直線の交点\nvector<Point> is_lc(Circle c, Line l) {\n vector<Point> res;\n ld d = dist_lp(l, c.p);\n if (d < c.r + eps) {\n ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n Point nor = (l.a - l.b) / abs(l.a - l.b);\n res.push_back(proj(l, c.p) + len * nor);\n res.push_back(proj(l, c.p) - len * nor);\n }\n return res;\n}\n// 円と線分の距離\nvector<Point> is_sc(Circle c, Line l) {\n vector<Point> v = is_lc(c, l), res;\n for (Point p : v)\n if (isis_sp(l, p)) res.push_back(p);\n return res;\n}\n// 円と点の接線\nvector<Line> tangent_cp(Circle c, Point p) {\n vector<Line> ret;\n Point v = c.p - p;\n ld d = abs(v);\n ld l = sqrt(norm(v) - c.r * c.r);\n if (isnan(l)) { return ret; }\n Point v1 = v * Point(l / d, c.r / d);\n Point v2 = v * Point(l / d, -c.r / d);\n ret.push_back(Line(p, p + v1));\n if (l < eps) return ret;\n ret.push_back(Line(p, p + v2));\n return ret;\n}\n// 円と円の接線\nvector<Line> tangent_cc(Circle c1, Circle c2) {\n vector<Line> ret;\n if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {\n Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);\n ret = tangent_cp(c1, center);\n }\n if (abs(c1.r - c2.r) > eps) {\n Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);\n vector<Line> nret = tangent_cp(c1, out);\n ret.insert(ret.end(), nret.begin(), nret.end());\n }\n else {\n Point v = c2.p - c1.p;\n v /= abs(v);\n Point q1 = c1.p + v * Point(0, 1) * c1.r;\n Point q2 = c1.p + v * Point(0, -1) * c1.r;\n ret.push_back(Line(q1, q1 + v));\n ret.push_back(Line(q2, q2 + v));\n }\n return ret;\n}\n\n/* 多角形 */\ntypedef vector<Point> Polygon;\n// 面積\nld area(const Polygon &p) {\n ld res = 0;\n int n = (int)p.size();\n for (int j = 0;j < n;j++) res += cross(p[j], p[(j + 1) % n]);\n return res / 2;\n}\n// 多角形の回転方向\nbool is_counter_clockwise(const Polygon &poly) {\n ld angle = 0;\n int n = (int)poly.size();\n for (int i = 0;i < n;i++) {\n Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];\n angle += arg((c - b) / (b - a));\n }\n return angle > eps;\n}\n// 凸性判定\nbool isConvex(const Polygon &poly){\n int n = (int)poly.size();\n if(n < 3) return false;\n int s = -3;\n for(int i = 0; i < n;i++){\n int r = ccw(poly[(i+n-1)%n],poly[i%n],poly[(i+1)%n]);\n if(r == 1 && s == -3) s = r;\n if(s*r == -1) return false;\n }\n return true;\n}\n// 点の内外判定\n// 0 => out : 1 => on : 2 => in\nint is_in_polygon(const Polygon &poly, Point p) {\n ld angle = 0;\n int n = (int)poly.size();\n for (int i = 0;i < n;i++) {\n Point a = poly[i], b = poly[(i + 1) % n];\n if (isis_sp(Line(a, b), p)) return 1;\n angle += arg((b - p) / (a - p));\n }\n return eq(angle, 0) ? 0 : 2;\n}\n\n// 凸包 : 凸多角形のある一辺上にある点を含まない\nPolygon convex_hull(vector<Point> ps) {\n int n = (int)ps.size();\n int k = 0;\n sort(ps.begin(), ps.end());\n Polygon ch(2 * n);\n for (int i = 0; i < n; ch[k++] = ps[i++])\n while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])\n while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;\n ch.resize(k - 1);\n return ch;\n}\n// 凸包 : 凸多角形のある一辺上にある点も含む\nPolygon convex_hull2(vector<Point> ps) {\n int n = (int)ps.size();\n if (n < 3) return ps;\n sort(ps.begin(), ps.end());\n Polygon u = { ps[0], ps[1] }, l = { ps[n - 1],ps[n - 2] };\n for (int i = 2; i < n; i++) {\n for (int j = (int)u.size(); j >= 2 && ccw(u[j - 2], u[j - 1], ps[i]) >= 0;j--)u.pop_back();\n u.push_back(ps[i]);\n }\n for (int i = n - 3;i >= 0;i--) {\n for (int j = (int)l.size(); j >= 2 && ccw(l[j - 2], l[j - 1], ps[i]) >= 0;j--)l.pop_back();\n l.push_back(ps[i]);\n }\n reverse(l.begin(), l.end());\n for (int i = (int)u.size() - 2; i >= 1; i--)l.push_back(u[i]);\n return l;\n}\n\n// 凸多角形の直径\npair<pll,ld> convex_diameter(const Polygon& poly){\n int n = (int)poly.size();\n if(n == 2) return make_pair(pll(0,1),abs(poly[0]-poly[1]));\n int ii = 0, jj = 0;\n for(int i = 1;i < n;i++){\n if(poly[i].imag() > poly[ii].imag())ii = i;\n if(poly[i].imag() < poly[jj].imag())jj = i;\n }\n pll resp = make_pair(-1,-1);\n ld resd = 0;\n \n int i, maxi,j,maxj;\n i = maxi = ii; j = maxj = jj;\n while(i != maxj || j != maxi){\n ld cur = abs(poly[i] - poly[j]);\n if(resd + eps < cur){ resd = cur; resp = pll(i,j); }\n int di = (i+1)%n, dj = (j+1)%n;\n if(cross(poly[di]-poly[i],poly[dj]-poly[j]) < 0) i = di;\n else j = dj;\n }\n return make_pair(resp,resd);\n}\n// 凸カット\nPolygon convex_cut(const Polygon &ps, Line l) {\n int n = (int)ps.size();\n Polygon Q;\n for (int i = 0;i < n;i++) {\n Point A = ps[i], B = ps[(i + 1) % n];\n Line m = Line(A, B);\n if (ccw(l.a, l.b, A) != -1) Q.push_back(A);\n if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m))\n Q.push_back(is_ll(l, m));\n }\n return Q;\n}\n\n// 円と多角形の共通部分\ndouble area_of_polygon_and_circle(const Polygon& poly, const Circle c) {\n int n = (int)poly.size();\n ld r = 0;\n for (int i = n - 1, j = 0; j < n; i=j++) {\n Point v = abs(poly[j] - poly[i]) / (poly[j] - poly[i]);\n if (poly[j] == poly[i])continue;\n assert(poly[j] != poly[i]);\n Point a = (poly[i] - c.p)*v, b = (poly[j] - c.p)*v;\n ld d = norm(c.r) - norm(a.imag());\n if (abs(a.imag()) < eps) continue;\n if (d < 0)d = 0;\n d = sqrt(d);\n double l, m;\n r += norm(c.r)*((l = atan2(b.imag(), min(b.real(), -d)) - atan2(a.imag(), min(a.real(), -d)))\n + (m = atan2(b.imag(), max(b.real(), d)) - atan2(a.imag(), max(a.real(), d))))\n + a.imag()*(min(d, max(a.real(), -d)) - max(-d, min(b.real(), d)));\n assert(-pi < l && -pi < m && l < pi && m < pi);\n }\n return r / 2;\n}\nld solve(){\n ld res = pi;\n int N,K; cin >> N >> K;\n \n Polygon ps(N);\n ld theta = 0;\n ld diff = 2*pi/N;\n for(int i = 0; i < N;i++){\n ps[i] = Point(cos(theta),sin(theta));\n theta += diff;\n }\n \n Line l0(ps[0],ps[K]),l1(ps[1],ps[(1-K+N)%N]);\n Point p = is_ll(l0,l1);\n Polygon A = {ps[0],p,ps[1]};\n ld S1 = abs(area(A));\n ld S2 = pi/N - sin(diff)/2;\n res -= N*(S1+S2);\n return res;\n}\nint main(void) {\n cin.tie(0); ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(12) << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 34452, "score_of_the_acc": -1.4218, "final_rank": 5 }, { "submission_id": "aoj_1593_3042463", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <iomanip>\n#include <complex>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\n\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nP crosspointLL(const L &l, const L &m) {\n double A = cross(l[1]-l[0], m[1]-m[0]);\n double B = cross(l[1]-l[0], l[1]-m[0]);\n return m[0] + B/A *(m[1]-m[0]);\n}\ndouble getarea(const VP &poly){\n double ret = 0;\n for (int i=0; i<(int)poly.size(); i++){ \n ret += cross(poly[i], poly[(i+1)%poly.size()]);\n }\n return ret*0.5;\n}\n\nint main(){\n int n,k;\n cin >> n >> k;\n VP v(n);\n for(int i=0; i<n; i++){\n v[i] = rotate(P(0, 1), 2*PI *i/n);\n }\n VP star;\n for(int i=0; i<n; i++){\n star.push_back(v[i]);\n if(k >= 2){\n star.push_back(crosspointLL(L(v[i], v[(i+k)%n]), L(v[(i+1)%n], v[(i+1-k+n)%n])));\n }\n }\n cout << fixed << setprecision(10);\n cout << getarea(star) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 51844, "score_of_the_acc": -1.6667, "final_rank": 7 }, { "submission_id": "aoj_1593_2635665", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <math.h>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nstruct Point{\n\tvoid set(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\tdouble x,y;\n};\n\nPoint point[1000000];\n\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\ndouble calc_S(Point a,Point b,Point c,Point d){\n\treturn fabs((b.x*a.y - b.y*a.x)+(c.x*b.y - c.y*b.x)+(d.x*c.y - d.y*c.x)+(a.x*d.y - a.y*d.x))/2;\n}\n\n\nint main(){\n\n\tint N,K;\n\tscanf(\"%d %d\",&N,&K);\n\n\tdouble base_degree = 360.0/(double)N;\n\n\tfor(int i = 0; i < N; i++){\n\t\tpoint[i].x = cos((base_degree*i*M_PI)/180.0);\n\t\tpoint[i].y = sin((base_degree*i*M_PI)/180.0);\n\t}\n\n\tPoint a,b,c,d,ret;\n\ta.set(point[0].x,point[0].y);\n\n\tret = calc_Cross_Point(point[0].x,point[K].x,point[1].x,point[N+1-K].x,point[0].y,point[K].y,point[1].y,point[N+1-K].y);\n\tb.set(ret.x,ret.y);\n\n\tc.set(0,0);\n\n\tret = calc_Cross_Point(point[0].x,point[N-K].x,point[N-1].x,point[(N-1+K)%N].x,point[0].y,point[N-K].y,point[N-1].y,point[(N-1+K)%N].y);\n\td.set(ret.x,ret.y);\n\n\tprintf(\"%.10lf\\n\",(double)N*calc_S(a,b,c,d));\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 19256, "score_of_the_acc": -0.3227, "final_rank": 1 }, { "submission_id": "aoj_1593_2002951", "code_snippet": "#include<iostream>\n#include<vector>\n#include<cmath>\n#include<cstdlib>\n#include<assert.h>\n#include <math.h>\n#include<iomanip>\nusing namespace std;\n#define OUT 0\n#define ON 1\n#define IN 2\n#define EPS (1e-10)\nclass P{ //???\npublic:\n double x,y;\n \n P(double _x=0,double _y=0):x(_x),y(_y){};\n P operator + (const P &p )const{ return P( x+p.x , y+p.y ); } //??????\n P operator - (const P &p )const{ return P( x-p.x , y-p.y ); } //??????\n P operator * (const double k )const{ return P( x*k , y*k ); } //??????\n P operator / (const double k )const{ return P( x/k , y/k ); } //??????\n \n bool operator == (const P &p){ return ( fabs(x-p.x)<EPS && fabs(y-p.y)<EPS ); }\n bool operator < (const P &p) const{ return ( x!=p.x ? x<p.x:y<p.y ); }\n \n double norm(){ return x*x+y*y; } //?????????\n double abs() { return sqrt(norm()); } //??§??????\n void normalize() {double d = sqrt(x*x+y*y); x /= d; y /= d;}\t//??£??????\n};\nstruct C{P p;double r;}; //???\nstruct S{P p1,p2;}; //??????\ntypedef vector<P> Polygon; //????§???¢\ntypedef P Vector; //????????????\ntypedef S L; //??´???\n\ndouble norm (P p) { return p.norm(); }\ndouble abs (P p) { return p.abs(); }\ndouble dot (Vector a,Vector b) { return a.x*b.x+a.y*b.y; }\ndouble cross(Vector a,Vector b) { return a.x*b.y-a.y*b.x; }\ndouble sqDist(P a, P b) {return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}\ndouble dist (P a, P b) {return sqrt(sqDist(a,b));}\nVector vec(S a) {return P(a.p2.x-a.p1.x,a.p2.y-a.p1.y);}\n\nint ccw(P p0,P p1,P p2){ //AOJ_BOOK_P386 verified\n Vector a = p1 - p0;\n Vector b = p2 - p0;\n \n if( cross(a,b) > EPS ) return 1 ; //COUNTER_CLOCKWISE\n if( cross(a,b) < -EPS ) return -1; //CLOCKWISE\n if( dot(a,b) < -EPS ) return 2; //ONLINE_BACK\n if( a.norm() < b.norm() ) return -2; //ONLINE_FRONT\n \n return 0; //ON_SEGMENT;\n}\n\n// ??´?????¨??´???????????? vefiried AOJ CGL_2\nP getCrossPointSS(S l1, S l2){\n double A = cross(vec(l1), vec(l2));\n double B = cross(vec(l1), l1.p2 - l2.p1);\n if(abs(A)<EPS && abs(B)<EPS) return l2.p1; // ?????´??????????????£?????????\n if(abs(A)<EPS) assert(false); // ??´?????????????????????\n return l2.p1 + vec(l2) * B / A;\n}\n\n//????????¨???????????¢ verified ARC042-B\ndouble dLP(S l, P p) { return abs(cross(l.p2-l.p1, p-l.p1)) /(l.p2-l.p1).abs(); }\n\nint main() {\n // source code\n int n, k;\n cin >> n >> k;\n double theta = (360.0/n)*(acos(-1)/180.0);\n P p = P(0, 1);\n vector<P> vp;\n for(int i = 0; i < n; i++){\n vp.push_back(P(p.x*cos(i*theta) - p.y*sin(i*theta),\n p.x*sin(i*theta) + p.y*cos(i*theta)));\n }\n double area = cross(vp[0], vp[1])/2.0;\n S a = S{vp[0], vp[k]};\n S b = S{vp[(1-k+n)%n], vp[1]};\n P cp = getCrossPointSS(a, b);\n //double dist = seg_to_point_dis(line(vp[0], vp[1]), cp);\n double d = dLP(S{vp[0], vp[1]}, cp);\n cout << setprecision(12) << (area - dist(vp[0], vp[1])*d/2)*n << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 19288, "score_of_the_acc": -0.3236, "final_rank": 2 }, { "submission_id": "aoj_1593_2002786", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <functional>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <sstream>\n#include <string>\n#include <complex>\n#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repd(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define mod 1000000007\n#define inf 2000000007\n#define mp make_pair\n#define pb push_back\ntypedef long long ll;\nusing namespace std;\ntemplate <typename T>\ninline void output(T a, int p) {\n if(p) cout << fixed << setprecision(p) << a << \"\\n\";\n else cout << a << \"\\n\";\n}\n// end of template\n\n\n#define eps 1e-9\nusing namespace std;\ntypedef complex<double> point;\ntypedef pair<point, point> line;\n\ndouble dis(point a, point b){\n return abs(a - b);\n}\n\ndouble dot(point a, point b){\n return a.real()*b.real() + a.imag()*b.imag();\n}\n\ndouble cross(point a, point b){\n return imag(conj(a)*b);\n}\n\nint ccw(point a, point b, point c){\n if(cross(b - a, c - a) > eps)return 1;\n if(cross(b - a, c - a) < -eps)return -1;\n if(dot(b - a, c - a) < -eps)return 2;\n if(abs(b - a) + eps < abs(c - a))return -2;\n return 0;\n}\n\ndouble line_to_point_dis(line l, point p){\n return abs(cross(p - l.first, l.second - l.first))/\n abs(l.second - l.first);\n}\n\ndouble seg_to_point_dis(line l, point p){\n point a = l.first, b = l.second, c = p;\n if(dot(b - a, c - a) < eps)return abs(c - a);\n if(dot(a - b, c - b) < eps)return abs(c - b);\n return line_to_point_dis(l, p);\n}\n\npoint cross_point(line a, line b){\n double d1 = cross(b.second - b.first, b.first - a.first);\n double d2 = cross(b.second - b.first, a.second - a.first);\n return a.first + (a.second - a.first)* d1/d2;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n // source code\n int n, k;\n cin >> n >> k;\n double theta = (360.0/n)*(acos(-1)/180.0);\n point p = point(0, 1);\n vector<point> vp;\n for(int i = 0; i < n; i++){\n vp.push_back(point(p.real()*cos(i*theta) - p.imag()*sin(i*theta),\n p.real()*sin(i*theta) + p.imag()*cos(i*theta)));\n }\n double area = cross(vp[0], vp[1])/2.0;\n \n line a = line(vp[0], vp[k]), b = line(vp[(1-k+n)%n], vp[1]);\n point cp = cross_point(a,b);\n double dist = seg_to_point_dis(line(vp[0], vp[1]), cp);\n output((area - dis(vp[0], vp[1])*dist/2)*n, 12);\n return 0;\n}", "accuracy": 0.11475409836065574, "time_ms": 50, "memory_kb": 19636, "score_of_the_acc": -0.3335, "final_rank": 10 }, { "submission_id": "aoj_1593_2002658", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <functional>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <sstream>\n#include <string>\n#include <complex>\n#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repd(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define mod 1000000007\n#define inf 2000000007\n#define mp make_pair\n#define pb push_back\ntypedef long long ll;\nusing namespace std;\ntemplate <typename T>\ninline void output(T a, int p) {\n if(p) cout << fixed << setprecision(p) << a << \"\\n\";\n else cout << a << \"\\n\";\n}\n// end of template\n\n\n#define eps 1e-9\nusing namespace std;\ntypedef complex<double> point;\ntypedef pair<point, point> line;\n\ndouble dis(point a, point b){\n return abs(a - b);\n}\n\ndouble dot(point a, point b){\n return a.real()*b.real() + a.imag()*b.imag();\n}\n\ndouble cross(point a, point b){\n return imag(conj(a)*b);\n}\n\nint ccw(point a, point b, point c){\n if(cross(b - a, c - a) > eps)return 1;\n if(cross(b - a, c - a) < -eps)return -1;\n if(dot(b - a, c - a) < -eps)return 2;\n if(abs(b - a) + eps < abs(c - a))return -2;\n return 0;\n}\n\ndouble line_to_point_dis(line l, point p){\n return abs(cross(p - l.first, l.second - l.first))/\n abs(l.second - l.first);\n}\n\ndouble seg_to_point_dis(line l, point p){\n point a = l.first, b = l.second, c = p;\n if(dot(b - a, c - a) < eps)return abs(c - a);\n if(dot(a - b, c - b) < eps)return abs(c - b);\n return line_to_point_dis(l, p);\n}\n\npoint cross_point(line a, line b){\n double d1 = cross(b.second - b.first, b.first - a.first);\n double d2 = cross(b.second - b.first, a.second - a.first);\n return a.first + (a.second - a.first)* d1/d2;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n // source code\n int n, k;\n cin >> n >> k;\n double theta = (360.0/n)*(acos(-1)/180.0);\n point p = point(0, 1);\n vector<point> vp;\n for(int i = 0; i < n; i++){\n vp.push_back(point(p));\n p = point(p.real()*cos(theta) - p.imag()*sin(theta),\n p.real()*sin(theta) + p.imag()*cos(theta));\n }\n double area = 0;\n for(int i = 0; i < n; i++){\n area += cross(vp[i], vp[(i + 1)%n]);\n }\n area /= 2;\n if(k == 1){\n output(area, 12);\n return 0;\n }\n\n line a = line(vp[0], vp[k]), b = line(vp[(1-k+n)%n], vp[1]);\n point cp = cross_point(a,b);\n double dist = seg_to_point_dis(line(vp[0], vp[1]), cp);\n output(area - dis(vp[0], vp[1])*dist*n/2, 12);\n return 0;\n}", "accuracy": 0.11475409836065574, "time_ms": 30, "memory_kb": 19504, "score_of_the_acc": -0.1631, "final_rank": 9 }, { "submission_id": "aoj_1593_2002623", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <functional>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <sstream>\n#include <string>\n#include <complex>\n#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repd(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define mod 1000000007\n#define inf 2000000007\n#define mp make_pair\n#define pb push_back\ntypedef long long ll;\nusing namespace std;\ntemplate <typename T>\ninline void output(T a, int p) {\n if(p) cout << fixed << setprecision(p) << a << \"\\n\";\n else cout << a << \"\\n\";\n}\n// end of template\n\n\n#define eps 1e-9\nusing namespace std;\ntypedef complex<double> point;\ntypedef pair<point, point> line;\n\ndouble dis(point a, point b){\n return abs(a - b);\n}\n\ndouble dot(point a, point b){\n return a.real()*b.real() + a.imag()*b.imag();\n}\n\ndouble cross(point a, point b){\n return imag(conj(a)*b);\n}\n\nint ccw(point a, point b, point c){\n if(cross(b - a, c - a) > eps)return 1;\n if(cross(b - a, c - a) < -eps)return -1;\n if(dot(b - a, c - a) < -eps)return 2;\n if(abs(b - a) + eps < abs(c - a))return -2;\n return 0;\n}\n\ndouble line_to_point_dis(line l, point p){\n return abs(cross(p - l.first, l.second - l.first))/\n abs(l.second - l.first);\n}\n\ndouble seg_to_point_dis(line l, point p){\n point a = l.first, b = l.second, c = p;\n if(dot(b - a, c - a) < eps)return abs(c - a);\n if(dot(a - b, c - b) < eps)return abs(c - b);\n return line_to_point_dis(l, p);\n}\n\npoint cross_point(line a, line b){\n double d1 = cross(b.second - b.first, b.first - a.first);\n double d2 = cross(b.second - b.first, a.second - a.first);\n return a.first + (a.second - a.first)* d1/d2;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n // source code\n int n, k;\n cin >> n >> k;\n double theta = (360.0/n) *(M_PI/180.0);\n point p = point(0, 1);\n vector<point> vp;\n for(int i = 0; i < n; i++){\n vp.push_back(point(p));\n p = point(p.real()*cos(theta) - p.imag()*sin(theta),\n p.real()*sin(theta) + p.imag()*cos(theta));\n }\n double area = 0;\n for(int i = 0; i < n; i++){\n area += cross(vp[i], vp[(i + 1)%n]);\n }\n area /= 2;\n line a = line(vp[0], vp[k]), b = line(vp[(1-k+n)%n], vp[1]);\n point cp = cross_point(a,b);\n double dist = seg_to_point_dis(line(vp[0], vp[1]), cp);\n output(area - dis(vp[0], vp[1])*dist*n/2, 12);\n return 0;\n}", "accuracy": 0.11475409836065574, "time_ms": 20, "memory_kb": 19600, "score_of_the_acc": -0.0825, "final_rank": 8 }, { "submission_id": "aoj_1593_2002585", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<complex>\n#include<vector>\nusing namespace std;\ntypedef complex<double> P;\ntypedef vector<P> G;\n#define rep(i,a) for(int i=0;i<a;i++)\nstruct L:public vector<P>{\n L(const P &a,const P &b){\n push_back(a);push_back(b);\n }\n};\nconst double EPS=1e-8;\nconst double PI=acos(-1);\ndouble cross(P a,P b){\n return imag(conj(a)*b);\n}\ndouble area(const G &g){\n double S=0;\n for(int i=0;i<g.size();i++){\n S+=(cross(g[i],g[(i+1)%g.size()]));\n }\n return abs(S/2.0);\n}\nP crosspointLL(const L &l,const L &m){\n double A=cross(l[1]-l[0],m[1]-m[0]);\n double B=cross(l[1]-l[0],l[1]-m[0]);\n if(abs(A)<EPS&&abs(B)<EPS)return m[0];\n return m[0]+B/A*(m[1]-m[0]);\n}\nP turn(P p,double t){\n return p*exp(P(.0,t*PI/180.0));\n}\nint main(){\n int n,k;\n cin>>n>>k;\n G g(n);\n g[0]=P(1,0);\n rep(i,n-1)g[i+1]=turn(g[i],360./n);\n double out=area(g);\n // cout<<out<<endl;\n P p=crosspointLL(L(g[0],g[k]),L(g[1],g[(1-k+n)%n]));\n G t(3);\n t[0]=g[0];\n t[1]=g[1];\n t[2]=p;\n out-=n*area(t);\n printf(\"%.9f\\n\",out);\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 16700, "score_of_the_acc": -0.4167, "final_rank": 3 } ]
aoj_1584_cpp
Sum of Sequences Problem n 個の要素を持つ数列 A 、 m 個の要素を持つ数列 B 、 それぞれが整数 c からなる q 個のクエリが与えられる。 各クエリについて、 数列 A の[ l a , r a ]を全て足した数と数列 B の[ l b , r b ]を全て足した数の差の絶対値が c になる l a , r a , l b , r b (0 ≤ l a ≤ r a ≤ n −1, 0 ≤ l b ≤ r b ≤ m −1, 配列の番号は0番から始まる) の組み合わせの数を求めよ。 Input n m q a 0 a 1 ... a n−1 b 0 b 1 ... b m−1 c 0 c 1 ... c q−1 入力は全て整数で与えられる。 1行目に数列の要素数 n と m 、クエリ数 q が与えられる。 2行目に数列 A の要素、3行目に数列 B の要素が空白区切りで与えられる。 4行目から q 行に各クエリの値 c i が与えられる。 Constraints 1 ≤ n , m ≤ 4×10 4 1 ≤ q ≤ 10 5 1 ≤ a i , b i ≤ 5 0 ≤ c i ≤ 2×10 5 Output 出力は q 行からなる。各クエリの組み合わせの数を順番に一行に出力せよ。 Sample Input 1 3 3 1 1 2 3 3 1 2 3 Sample Output 1 6 Sample Input 2 5 4 2 1 2 3 4 5 2 2 2 2 11 12 Sample Output 2 3 4
[ { "submission_id": "aoj_1584_5312473", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define SIZE 25\n#define MAX_N 40005\n#define MAX 200000\n\ntypedef complex<double> COMPLEX;\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nint LR_rev[1000005];\nvector<Info> info;\nvector<COMPLEX> V[SIZE];\n\nint A[MAX_N],B[MAX_N];\nint rui_A[MAX_N],rui_B[MAX_N];\n\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tLR_rev[0] = 0;\n\n\tfor(int i = 1; i < N; i++){\n\n\t\tif(i%2 == 1){\n\n\t\t\tLR_rev[i] = LR_rev[i-1]+POW[num_pow-1]; //1を足す代わりにPOW[num_pow-1]を足す\n\n\t\t}else{\n\n\t\t\tLR_rev[i] = LR_rev[i/2]/2; //半分のインデックスの値に、2を掛ける代わりに2で割る\n\t\t}\n\n\t\tif(i < LR_rev[i]){\n\t\t\tinfo.push_back(Info(i,LR_rev[i]));\n\t\t}\n\t}\n}\n\nvoid calc_COMPLEX(int N){\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tV[num_pow].resize(LEN/2);\n\n\t\tV[num_pow][0] = COMPLEX(cos(0),sin(0));\n\n\t\tCOMPLEX mult = COMPLEX(cos((2*M_PI)/LEN),sin((2*M_PI)/LEN));\n\n\t\tfor(int i = 1; i < LEN/2; i++){\n\n\t\t\tif(i%2 == 1){\n\t\t\t\tV[num_pow][i] = V[num_pow][i-1]*mult;\n\t\t\t}else{\n\t\t\t\tV[num_pow][i] = V[num_pow-1][i/2];\n\t\t\t}\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[num_pow][i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tinfo.clear();\n\tfor(int i = 0; i < SIZE; i++){\n\n\t\tV[i].clear();\n\t}\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tcalc_swap_pair(N,num_pow);\n\tcalc_COMPLEX(N);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_Q;\n\tscanf(\"%d %d %d\",&N,&M,&num_Q);\n\n\trui_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&A[i]);\n\t\trui_A[i] = A[i];\n\t\trui_A[i] += rui_A[i-1];\n\t}\n\n\tvector<COMPLEX> F1(MAX+1),Q1(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tF1[rui_A[i]] += 1;\n\t\tQ1[rui_A[i]] += 1;\n\t}\n\n\treverse(Q1.begin(),Q1.end());\n\n\tvector<COMPLEX> ret1 = convolution(F1,Q1);\n\n\trui_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&B[i]);\n\t\trui_B[i] = B[i];\n\t\trui_B[i] += rui_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> F2(MAX+1),Q2(MAX+1);\n\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tF2[rui_B[i]] += 1;\n\t\tQ2[rui_B[i]] += 1;\n\t}\n\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(F2,Q2);\n\n\tvector<COMPLEX> F3(MAX+1),Q3(MAX+1);\n\n\tfor(int i = 0; i <= MAX; i++){\n\n\t\tif(i == 0)continue; //区間和が0を除く\n\t\tF3[i] = ret1[MAX-i];\n\t}\n\tfor(int i = 0; i <= MAX; i++){\n\n\t\tif(i == 0)continue; //区間和が0を除く\n\t\tQ3[i] = ret2[MAX-i];\n\t}\n\treverse(Q3.begin(),Q3.end());\n\n\tvector<COMPLEX> ANS = convolution(F3,Q3);\n\n\tint C;\n\n\tfor(int loop = 0; loop < num_Q; loop++){\n\n\t\tscanf(\"%d\",&C);\n\n\t\tif(C == 0){\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ANS[MAX].real()));\n\t\t}else{\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ANS[MAX-C].real())+(ll)round(ANS[MAX+C].real()));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 104440, "score_of_the_acc": -0.9647, "final_rank": 2 }, { "submission_id": "aoj_1584_5305679", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nint LR_rev[2000005];\nvector<Info> info;\nvector<COMPLEX> V[SIZE];\n\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tLR_rev[0] = 0;\n\n\tfor(int i = 1; i < N; i++){\n\n\t\tif(i%2 == 1){\n\n\t\t\tLR_rev[i] = LR_rev[i-1]+POW[num_pow-1]; //1を足す代わりにPOW[num_pow-1]を足す\n\n\t\t}else{\n\n\t\t\tLR_rev[i] = LR_rev[i/2]/2; //半分のインデックスの値に、2を掛ける代わりに2で割る\n\t\t}\n\n\t\tif(i < LR_rev[i]){\n\t\t\tinfo.push_back(Info(i,LR_rev[i]));\n\t\t}\n\t}\n}\n\nvoid calc_COMPLEX(int N){\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tV[num_pow].resize(LEN/2);\n\n\t\tV[num_pow][0] = COMPLEX(cos(0),sin(0));\n\n\t\tCOMPLEX mult = COMPLEX(cos((2*M_PI)/LEN),sin((2*M_PI)/LEN));\n\n\t\tfor(int i = 1; i < LEN/2; i++){\n\n\t\t\tif(i%2 == 1){\n\t\t\t\tV[num_pow][i] = V[num_pow][i-1]*mult;\n\t\t\t}else{\n\t\t\t\tV[num_pow][i] = V[num_pow-1][i/2];\n\t\t\t}\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[num_pow][i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tinfo.clear();\n\tfor(int i = 0; i < SIZE; i++){\n\n\t\tV[i].clear();\n\t}\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tinfo.clear();\n\tcalc_swap_pair(N,num_pow);\n\tcalc_COMPLEX(N);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){ //★★累積和テーブル上の区間で、left==rightは除く(空になってしまうから)★★\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX].real()));\n\t\t}else{\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX-tmp].real())+(ll)round(ret3[MAX+tmp].real()));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 107164, "score_of_the_acc": -1, "final_rank": 3 }, { "submission_id": "aoj_1584_3903096", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nint LR_rev[2000005];\nvector<Info> info;\nvector<COMPLEX> V[SIZE];\n\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tLR_rev[0] = 0;\n\n\tfor(int i = 1; i < N; i++){\n\n\t\tif(i%2 == 1){\n\n\t\t\tLR_rev[i] = LR_rev[i-1]+POW[num_pow-1]; //1を足す代わりにPOW[num_pow-1]を足す\n\n\t\t}else{\n\n\t\t\tLR_rev[i] = LR_rev[i/2]/2; //半分のインデックスの値に、2を掛ける代わりに2で割る\n\t\t}\n\n\t\tif(i < LR_rev[i]){\n\t\t\tinfo.push_back(Info(i,LR_rev[i]));\n\t\t}\n\t}\n}\n\nvoid calc_COMPLEX(int N){\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tV[num_pow].resize(LEN/2);\n\n\t\tV[num_pow][0] = COMPLEX(cos(0),sin(0));\n\n\t\tCOMPLEX mult = COMPLEX(cos((2*M_PI)/LEN),sin((2*M_PI)/LEN));\n\n\t\tfor(int i = 1; i < LEN/2; i++){\n\n\t\t\tif(i%2 == 1){\n\t\t\t\tV[num_pow][i] = V[num_pow][i-1]*mult;\n\t\t\t}else{\n\t\t\t\tV[num_pow][i] = V[num_pow-1][i/2];\n\t\t\t}\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[num_pow][i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tinfo.clear();\n\tcalc_swap_pair(N,num_pow);\n\tcalc_COMPLEX(N);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX].real()));\n\t\t}else{\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX-tmp].real())+(ll)round(ret3[MAX+tmp].real()));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 107124, "score_of_the_acc": -1.0293, "final_rank": 5 }, { "submission_id": "aoj_1584_3903095", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nint LR_rev[2000005];\nvector<Info> info;\nvector<COMPLEX> V[SIZE];\n\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tLR_rev[0] = 0;\n\n\tfor(int i = 1; i < N; i++){\n\n\t\tif(i%2 == 1){\n\n\t\t\tLR_rev[i] = LR_rev[i-1]+POW[num_pow-1]; //1を足す代わりにPOW[num_pow-1]を足す\n\n\t\t}else{\n\n\t\t\tLR_rev[i] = LR_rev[i/2]/2; //半分のインデックスの値に、2を掛ける代わりに2で割る\n\t\t}\n\n\t\tif(i < LR_rev[i]){\n\t\t\tinfo.push_back(Info(i,LR_rev[i]));\n\t\t}\n\t}\n}\n\nvoid calc_COMPLEX(int N){\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tV[num_pow].resize(LEN/2);\n\n\t\tV[num_pow][0] = COMPLEX(cos(0),sin(0));\n\n\t\tCOMPLEX mult = COMPLEX(cos((2*M_PI)/LEN),sin((2*M_PI)/LEN));\n\n\t\tfor(int i = 1; i < LEN/2; i++){\n\n\t\t\tif(i%2 == 1){\n\t\t\t\tV[num_pow][i] = V[num_pow][i-1]*mult;\n\t\t\t}else{\n\t\t\t\tV[num_pow][i] = V[num_pow-1][i/2];\n\t\t\t}\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[num_pow][i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tif(info.size() == 0){\n\t\tcalc_swap_pair(N,num_pow);\n\t}\n\tcalc_COMPLEX(N);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX].real()));\n\t\t}else{\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX-tmp].real())+(ll)round(ret3[MAX+tmp].real()));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 107092, "score_of_the_acc": -1.0215, "final_rank": 4 }, { "submission_id": "aoj_1584_3902987", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nbool check[2000005];\nvector<Info> info;\nvector<COMPLEX> V[SIZE];\n\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tcheck[i] = false;\n\t}\n\n\tint tmp;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(check[i])continue;\n\n\t\ttmp = 0;\n\n\t\tfor(int loop = 0; loop < num_pow; loop++){\n\t\t\tif(i & (1 << loop)){\n\t\t\t\ttmp += POW[(num_pow-1)-loop];\n\t\t\t}\n\t\t}\n\n\t\tif(i < tmp){\n\t\t\tinfo.push_back(Info(i,tmp));\n\t\t\tcheck[tmp] = true;\n\t\t}\n\t}\n}\n\nvoid calc_COMPLEX(int N){\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tV[num_pow].resize(LEN/2);\n\n\t\tV[num_pow][0] = COMPLEX(cos(0),sin(0));\n\n\t\tCOMPLEX mult = COMPLEX(cos((2*M_PI)/LEN),sin((2*M_PI)/LEN));\n\n\t\tfor(int i = 1; i < LEN/2; i++){\n\n\t\t\tif(i%2 == 1){\n\t\t\t\tV[num_pow][i] = V[num_pow][i-1]*mult;\n\t\t\t}else{\n\t\t\t\tV[num_pow][i] = V[num_pow-1][i/2];\n\t\t\t}\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\n\tfor(int LEN = 2,num_pow = 0; LEN <= N; LEN *= 2,num_pow++){\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[num_pow][i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tinfo.clear();\n\tcalc_swap_pair(N,num_pow);\n\tcalc_COMPLEX(N);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX].real()));\n\t\t}else{\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX-tmp].real())+(ll)round(ret3[MAX+tmp].real()));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 430, "memory_kb": 105556, "score_of_the_acc": -1.0314, "final_rank": 6 }, { "submission_id": "aoj_1584_3902587", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nbool check[5000005];\nvector<Info> info;\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tcheck[i] = false;\n\t}\n\n\tint tmp;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(check[i])continue;\n\n\t\ttmp = 0;\n\n\t\tfor(int loop = 0; loop < num_pow; loop++){\n\t\t\tif(i & (1 << loop)){\n\t\t\t\ttmp += POW[(num_pow-1)-loop];\n\t\t\t}\n\t\t}\n\n\t\tif(i < tmp){\n\t\t\tinfo.push_back(Info(i,tmp));\n\t\t\tcheck[tmp] = true;\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\tvector<COMPLEX> V(N);\n\n\tfor(int LEN = 2; LEN <= N; LEN *= 2){\n\n\t\tfor(int i = 0; i < LEN/2; i++){\n\n\t\t\tV[i] = COMPLEX(cos((2*M_PI*i)/LEN),sin((2*M_PI*i)/LEN));\n\t\t}\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tinfo.clear();\n\tcalc_swap_pair(N,num_pow);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX].real()));\n\t\t}else{\n\n\t\t\tprintf(\"%lld\\n\",(ll)round(ret3[MAX-tmp].real())+(ll)round(ret3[MAX+tmp].real()));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 105504, "score_of_the_acc": -1.1576, "final_rank": 7 }, { "submission_id": "aoj_1584_3902584", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nbool check[5000005];\nvector<Info> info;\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tcheck[i] = false;\n\t}\n\n\tint tmp;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(check[i])continue;\n\n\t\ttmp = 0;\n\n\t\tfor(int loop = 0; loop < num_pow; loop++){\n\t\t\tif(i & (1 << loop)){\n\t\t\t\ttmp += POW[(num_pow-1)-loop];\n\t\t\t}\n\t\t}\n\n\t\tif(i < tmp){\n\t\t\tinfo.push_back(Info(i,tmp));\n\t\t\tcheck[tmp] = true;\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\tvector<COMPLEX> V(N);\n\n\tfor(int LEN = 2; LEN <= N; LEN *= 2){\n\n\t\tfor(int i = 0; i < LEN/2; i++){\n\n\t\t\tV[i] = COMPLEX(cos((2*M_PI*i)/LEN),sin((2*M_PI*i)/LEN));\n\t\t}\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tinfo.clear();\n\tcalc_swap_pair(N,num_pow);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%lld\\n\",(ll)(ret3[MAX].real()+0.5));\n\t\t}else{\n\n\t\t\tprintf(\"%lld\\n\",(ll)(ret3[MAX-tmp].real()+0.5)+(ll)(ret3[MAX+tmp].real()+0.5));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 600, "memory_kb": 105516, "score_of_the_acc": -1.1578, "final_rank": 8 }, { "submission_id": "aoj_1584_3902583", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nbool check[5000005];\nvector<Info> info;\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tcheck[i] = false;\n\t}\n\n\tint tmp;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(check[i])continue;\n\n\t\ttmp = 0;\n\n\t\tfor(int loop = 0; loop < num_pow; loop++){\n\t\t\tif(i & (1 << loop)){\n\t\t\t\ttmp += POW[(num_pow-1)-loop];\n\t\t\t}\n\t\t}\n\n\t\tif(i < tmp){\n\t\t\tinfo.push_back(Info(i,tmp));\n\t\t\tcheck[tmp] = true;\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\tvector<COMPLEX> V(N);\n\n\tfor(int LEN = 2; LEN <= N; LEN *= 2){\n\n\t\tfor(int i = 0; i < LEN/2; i++){\n\n\t\t\tV[i] = COMPLEX(cos((2*M_PI*i)/LEN),sin((2*M_PI*i)/LEN));\n\t\t}\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tinfo.clear();\n\tcalc_swap_pair(N,num_pow);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%lld\\n\",(ll)(ret3[MAX].real()+0.5));\n\t\t}else{\n\n\t\t\tprintf(\"%lld\\n\",(ll)(ret3[MAX-tmp].real()+0.5)+(int)(ret3[MAX+tmp].real()+0.5));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 590, "memory_kb": 105472, "score_of_the_acc": -1.1497, "final_rank": 18 }, { "submission_id": "aoj_1584_3902582", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nbool check[5000005];\nvector<Info> info;\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tcheck[i] = false;\n\t}\n\n\tint tmp;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(check[i])continue;\n\n\t\ttmp = 0;\n\n\t\tfor(int loop = 0; loop < num_pow; loop++){\n\t\t\tif(i & (1 << loop)){\n\t\t\t\ttmp += POW[(num_pow-1)-loop];\n\t\t\t}\n\t\t}\n\n\t\tif(i < tmp){\n\t\t\tinfo.push_back(Info(i,tmp));\n\t\t\tcheck[tmp] = true;\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\tvector<COMPLEX> V(N);\n\n\tfor(int LEN = 2; LEN <= N; LEN *= 2){\n\n\t\tfor(int i = 0; i < LEN/2; i++){\n\n\t\t\tV[i] = COMPLEX(cos((2*M_PI*i)/LEN),sin((2*M_PI*i)/LEN));\n\t\t}\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tinfo.clear();\n\tcalc_swap_pair(N,num_pow);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%d\\n\",(int)(ret3[MAX].real()+0.5));\n\t\t}else{\n\n\t\t\tprintf(\"%d\\n\",(int)(ret3[MAX-tmp].real()+0.5)+(int)(ret3[MAX+tmp].real()+0.5));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 570, "memory_kb": 105556, "score_of_the_acc": -1.1359, "final_rank": 17 }, { "submission_id": "aoj_1584_3902581", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\ntypedef complex<double> COMPLEX;\n#define MAX 200000\n#define SIZE 30\n\nint table_A[40005],table_B[40005];\n\n\nstruct Info{\n\tInfo(int arg_left,int arg_right){\n\t\tleft = arg_left;\n\t\tright = arg_right;\n\t}\n\tint left,right;\n};\n\nint POW[SIZE];\nbool check[5000005];\nvector<Info> info;\n\nvoid calc_swap_pair(int N,int num_pow){\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tcheck[i] = false;\n\t}\n\n\tint tmp;\n\n\tfor(int i = 0; i < N; i++){\n\t\tif(check[i])continue;\n\n\t\ttmp = 0;\n\n\t\tfor(int loop = 0; loop < num_pow; loop++){\n\t\t\tif(i & (1 << loop)){\n\t\t\t\ttmp += POW[(num_pow-1)-loop];\n\t\t\t}\n\t\t}\n\n\t\tif(i < tmp){\n\t\t\tinfo.push_back(Info(i,tmp));\n\t\t\tcheck[tmp] = true;\n\t\t}\n\t}\n}\n\n//離散フーリエ変換\nvector<COMPLEX> DFT(vector<COMPLEX> A) {\n\n\tint N = A.size();\n\n\t/*偶数項を左に、奇数項を右に仕分けるルールで要素数1まで仕分けした場合の、\n\t * 最終結果を先に作っておく(ビットが左右反転したものがその位置に来る)*/\n\tfor(int i = 0; i < info.size(); i++){\n\n\t\tswap(A[info[i].left],A[info[i].right]);\n\t}\n\n\tCOMPLEX a,add;\n\tvector<COMPLEX> V(N);\n\n\tfor(int LEN = 2; LEN <= N; LEN *= 2){\n\n\t\tfor(int i = 0; i < LEN/2; i++){\n\n\t\t\tV[i] = COMPLEX(cos((2*M_PI*i)/LEN),sin((2*M_PI*i)/LEN));\n\t\t}\n\n\t\tfor(int start = 0; start < N; start += LEN){\n\t\t\tfor(int i = 0; i < LEN/2; i++){\n\t\t\t\t//マージソートの容量で、配列を更新していく\n\t\t\t\ta = A[start+i];\n\t\t\t\tadd = V[i]*A[start+LEN/2+i];\n\n\t\t\t\tA[start+i] = a+add;\n\t\t\t\tA[start+LEN/2+i] = a-add;\n\t\t\t}\n\t\t}\n\t}\n\treturn A;\n}\n\n\n\n//離散逆フーリエ変換\nvector<COMPLEX> inverseDFT(vector<COMPLEX> A){\n\n\tint N = A.size();\n\n\tvector<COMPLEX> TMP = DFT(A);\n\n\t//係数を入れ替える&サイズNで各要素を割る\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = TMP[(N-i)%N];\n\t\tRET[i] = RET[i]/COMPLEX(N,0);\n\t}\n\n\treturn RET;\n}\n\nvector<COMPLEX> convolution(vector<COMPLEX> A,vector<COMPLEX> B){\n\n\tint degree = (A.size()-1)+(B.size()-1)+1;\n\n\tint N = 1,num_pow = 0;\n\twhile(N < degree){\n\t\tN *= 2;\n\t\tnum_pow++;\n\t}\n\n\tinfo.clear();\n\tcalc_swap_pair(N,num_pow);\n\n\tA.resize(N);\n\tB.resize(N);\n\n\tA = DFT(A);\n\tB = DFT(B);\n\n\tvector<COMPLEX> RET(N);\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tRET[i] = A[i]*B[i];\n\t}\n\n\treturn inverseDFT(RET);\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\tint N,M,num_query;\n\n\tscanf(\"%d %d %d\",&N,&M,&num_query);\n\n\ttable_A[0] = 0;\n\tfor(int i = 1; i <= N; i++){\n\n\t\tscanf(\"%d\",&table_A[i]);\n\t\ttable_A[i] += table_A[i-1];\n\t}\n\n\ttable_B[0] = 0;\n\tfor(int i = 1; i <= M; i++){\n\n\t\tscanf(\"%d\",&table_B[i]);\n\t\ttable_B[i] += table_B[i-1];\n\t}\n\n\n\tvector<COMPLEX> P(MAX+1),Q(MAX+1),R(MAX+1);\n\n\tfor(int i = 0; i <= N; i++){\n\n\t\tP[table_A[i]] += 1;\n\t\tQ[table_A[i]] += 1;\n\t}\n\treverse(Q.begin(),Q.end());\n\n\tvector<COMPLEX> ret = convolution(P,Q);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR[i] = ret[(MAX-1)-i];\n\t}\n\n\tvector<COMPLEX> P2(MAX+1),Q2(MAX+1),R2(MAX+1);\n\n\tfor(int i = 0; i <= M; i++){\n\n\t\tP2[table_B[i]] += 1;\n\t\tQ2[table_B[i]] += 1;\n\t}\n\treverse(Q2.begin(),Q2.end());\n\n\tvector<COMPLEX> ret2 = convolution(P2,Q2);\n\n\tfor(int i = 0; i <= MAX-1; i++){\n\n\t\tR2[i] = ret2[(MAX-1)-i];\n\t}\n\n\treverse(R2.begin(),R2.end());\n\n\tvector<COMPLEX> ret3 = convolution(R,R2);\n\n\tint tmp;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\n\t\tscanf(\"%d\",&tmp);\n\t\tif(tmp == 0){\n\n\t\t\tprintf(\"%d\\n\",(int)round(ret3[MAX].real()));\n\t\t}else{\n\n\t\t\tprintf(\"%d\\n\",(int)round(ret3[MAX-tmp].real())+(int)round(ret3[MAX+tmp].real()));\n\t\t}\n\t}\n\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 590, "memory_kb": 105592, "score_of_the_acc": -1.1513, "final_rank": 19 }, { "submission_id": "aoj_1584_2665299", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\nnamespace FastFourierTransform\n{\n\tusing C = complex< double >;\n\n\tvoid DiscreteFourierTransform(vector< C > &F, bool rev)\n\t{\n\t\tconst int N = (int)F.size();\n\t\tconst double PI = (rev ? -1 : 1) * acos(-1);\n\t\tfor (int i = 0, j = 1; j + 1 < N; j++) {\n\t\t\tfor (int k = N >> 1; k > (i ^= k); k >>= 1);\n\t\t\tif (i > j) swap(F[i], F[j]);\n\t\t}\n\t\tC w, s, t;\n\t\tfor (int i = 1; i < N; i <<= 1) {\n\t\t\tfor (int k = 0; k < i; k++) {\n\t\t\t\tw = polar(1.0, PI / i * k);\n\t\t\t\tfor (int j = 0; j < N; j += i * 2) {\n\t\t\t\t\ts = F[j + k];\n\t\t\t\t\tt = C(F[j + k + i].real() * w.real() - F[j + k + i].imag() * w.imag(),\n\t\t\t\t\t\tF[j + k + i].real() * w.imag() + F[j + k + i].imag() * w.real());\n\t\t\t\t\tF[j + k] = s + t, F[j + k + i] = s - t;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (rev) for (int i = 0; i < N; i++) F[i] /= N;\n\t}\n\n\tvector< long long > Multiply(const vector<long long int > &A, const vector<long long int > &B)\n\t{\n\t\tint sz = 1;\n\t\twhile (sz <= A.size() + B.size()) sz <<= 1;\n\t\tvector< C > F(sz), G(sz);\n\t\tfor (int i = 0; i < A.size(); i++) F[i] = A[i];\n\t\tfor (int i = 0; i < B.size(); i++) G[i] = B[i];\n\t\tDiscreteFourierTransform(F, false);\n\t\tDiscreteFourierTransform(G, false);\n\t\tfor (int i = 0; i < sz; i++) F[i] *= G[i];\n\t\tDiscreteFourierTransform(F, true);\n\t\tvector< long long > X(A.size() + B.size() - 1);\n\t\tfor (int i = 0; i < A.size() + B.size() - 1; i++) X[i] = F[i].real() + 0.5;\n\t\treturn (X);\n\t}\n};\nconst int asize = 2e5+100;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c= FastFourierTransform::Multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tvector<long long >c= FastFourierTransform::Multiply(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(abs(aaa*bbb-accumulate(ans.begin(),ans.end(),0ll))<1e9);\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 820, "memory_kb": 29908, "score_of_the_acc": -0.3433, "final_rank": 1 }, { "submission_id": "aoj_1584_2665272", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+f1;\n\t\tf[1]=f0-f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\n\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+100;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tvector<long long >c=multiply<long long int>(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(abs(aaa*bbb-accumulate(ans.begin(),ans.end(),0ll))<1e9);\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1520, "memory_kb": 50668, "score_of_the_acc": -1.1344, "final_rank": 15 }, { "submission_id": "aoj_1584_2665269", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+f1;\n\t\tf[1]=f0-f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\n\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+1;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tvector<long long >c=multiply<long long int>(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(abs(aaa*bbb-accumulate(ans.begin(),ans.end(),0ll))<1e9);\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1700, "memory_kb": 50756, "score_of_the_acc": -1.2699, "final_rank": 20 }, { "submission_id": "aoj_1584_2665268", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+f1;\n\t\tf[1]=f0-f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\n\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+1;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tvector<long long >c=multiply<long long int>(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(abs(aaa*bbb-accumulate(ans.begin(),ans.end(),0ll))<1e12);\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1520, "memory_kb": 50628, "score_of_the_acc": -1.1339, "final_rank": 14 }, { "submission_id": "aoj_1584_2665267", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+f1;\n\t\tf[1]=f0-f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\n\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+1;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tvector<long long >c=multiply<long long int>(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(abs(aaa*bbb-accumulate(ans.begin(),ans.end(),0ll))<1e6);\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1520, "memory_kb": 50464, "score_of_the_acc": -1.1317, "final_rank": 13 }, { "submission_id": "aoj_1584_2665266", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+f1;\n\t\tf[1]=f0-f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\n\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+1;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tvector<long long >c=multiply<long long int>(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(abs(aaa*bbb-accumulate(ans.begin(),ans.end(),0ll))<100);\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1510, "memory_kb": 50476, "score_of_the_acc": -1.1244, "final_rank": 12 }, { "submission_id": "aoj_1584_2665265", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+f1;\n\t\tf[1]=f0-f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\n\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+1;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tvector<long long >c=multiply<long long int>(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(aaa*bbb==accumulate(ans.begin(),ans.end(),0ll));\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1400, "memory_kb": 50436, "score_of_the_acc": -1.0418, "final_rank": 9 }, { "submission_id": "aoj_1584_2665257", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+1.0*f1;\n\t\tf[1]=f0-1.0*f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\n\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+1;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tvector<long long >c=multiply<long long int>(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(aaa*bbb==accumulate(ans.begin(),ans.end(),0ll));\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1410, "memory_kb": 50480, "score_of_the_acc": -1.0499, "final_rank": 10 }, { "submission_id": "aoj_1584_2665255", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+1.0*f1;\n\t\tf[1]=f0-1.0*f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\n\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+1;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tvector<long long int> c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tauto c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans; \n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0ll));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0ll));\n\tauto ans(solve(a_mp,b_mp));\n\tassert(aaa*bbb==accumulate(ans.begin(),ans.end(),0ll));\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1420, "memory_kb": 50464, "score_of_the_acc": -1.0571, "final_rank": 11 }, { "submission_id": "aoj_1584_2665238", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-9;\n/*\n?????????????????¨?????? FFT ???????????????\nn?¬??????????g(x) = \\sum_{i=0}^n a_i x^i , h(x) = \\sum_{i=0}^n b_i x^i ???????????????\nf(x) = (g*h)(x) = \\sum_{k=0}^{2n} \\sum_{i=0}^{k} a_i b_{k-i} x^k\n???O(n log n)??§?±??????????\n\n?????????:\ng(x)????????°??? vector<T> g\nh(x)????????°??? vector<T> h\n??¨????????¨???\nf = multiply(g, h);\n??§f(x)????????°vector<T> f????±??????????\n\nverified by: http://atc001.contest.atcoder.jp/tasks/fft_c\n(AtCoder Typical Contest 001 C)\n\ncomplex, vector, algorithm ?????????????????????????????????????????????\n*/\n#include<complex>\n#include<vector>\n#include<algorithm>\nusing namespace std;\nconst double PI = 3.141592653589793;\n/*\n*\n * n?¬??????????f????????????????????¨????????????\n * is_inv = false => ?????????\n * is_inv = true => ?????????\n * ?????????????????¨??????multiply????????§n??§?????£?????????\n */\nusing comp=complex<double>;\nvoid fft(vector<comp> &f, int n, bool is_inv) {\n\tif (n == 1) return;\n\tif (n == 2) {\n\t\tcomp zeta = comp(-1,0);\n\t\tcomp pow_zeta = 1.0;\n\n\t\tcomp f0(f[0]);\n\t\tcomp f1(f[1]);\n\n\t\tf[0]=f0+1.0*f1;\n\t\tf[1]=f0-1.0*f1;\n\t\treturn;\n\t}\n\tvector<comp> f0(n / 2), f1(n / 2);\n\tfor (int i = 0; i < n / 2; i++) {\n\t\tf0[i] = f[2 * i];\n\t\tf1[i] = f[2 * i + 1];\n\t\n\t}\n\tfft(f0, n / 2, is_inv);\n\tfft(f1, n / 2, is_inv);\n\tcomp zeta = comp(cos(2 * PI / n), sin(2 * PI / n));\n\tif (is_inv) zeta = 1.0 / zeta;\n\tcomp pow_zeta = 1.0;\n\tfor (int i = 0; i < n; i++) {\n\t\tf[i] = f0[i % (n / 2)] + pow_zeta * f1[i % (n / 2)];\n\t\tpow_zeta *= zeta;\n\t}\n}\ntemplate<typename T>\nvector<T> multiply(vector<T> &g, vector<T>& h) {\n\tint n = 1;\n\tint gs = g.size();\n\tint hs = h.size();\n\tint fs = gs + hs - 1;\n\twhile (n <= fs + 1) n *= 2;\n\tvector<comp> gg(n), hh(n), ff(n);\n\tfor (int i = 0; i < n; i++) {\n\t\tgg[i] = (i < gs) ? g[i] : 0;\n\t\thh[i] = (i < hs) ? h[i] : 0;\n\t}\n\tfft(gg, n, false);\n\tfft(hh, n, false);\n\tfor (int i = 0; i < n; i++) ff[i] = gg[i] * hh[i];\n\tfft(ff, n, true);\n\t\n\tvector<T> f(fs);\n\tfor (int i = 0; i < fs; i++) {\n\t\tf[i] = (T)round(ff[i].real() / n);\n\t}\n\t\n\treturn f;\n\t\n}\nconst int asize = 2e5+1;\nvector<long long int> get_mp(const vector<int>v) {\n\tvector<long long int>sums(v.size()+1);\n\tfor (int i = 0; i < v.size(); ++i) {\n\t\tsums[i+1]=sums[i]+v[i];\n\t}\n\n\tvector<long long int>a(asize);\n\tvector<long long int>b(asize);\n\tfor (auto sum : sums) {\n\t\ta[sum]++;\n\t\tb[asize-sum-1]++;\n\t}\n\tauto c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tfor (int i = asize; i < 2 * asize-1; ++i) {\n\t\tif (c[i]) {\n\t\t\tans[i - asize+1] += c[i];\n\t\t}\n\t}\n\treturn ans;\n}\nvector<long long int>solve(vector<long long int>a, vector<long long int>b) {\n\treverse(a.begin(),a.end());\n\n\tauto c=multiply(a,b);\n\tvector<long long int>ans(asize);\n\tans[0]=c[asize-1];\n\tfor (int i = 1; i < asize; ++i) {\n\t\tans[i]=c[asize-1-i]+c[asize-1+i];\n\t}\n\treturn ans;\n}\n\nint main() {\n\tint N,M,Q;cin>>N>>M>>Q;\n\tvector<int>as(N),bs(M);\n\tfor (int i = 0; i < N; ++i) {\n\t\tcin>>as[i];\n\t}\n\tfor (int i = 0; i < M; ++i) {\n\t\tcin>>bs[i];\n\t}\n\tlong long int aaa=static_cast<long long int>(N)*(N+1)/2;\n\tlong long int bbb=static_cast<long long int>(M)*(M+1)/2;\n\tauto a_mp(get_mp(as));\n\tassert(aaa=accumulate(a_mp.begin(),a_mp.end(),0l));\n\tauto b_mp(get_mp(bs));\n\tassert(bbb = accumulate(b_mp.begin(), b_mp.end(), 0l));\n\tauto ans(solve(a_mp,b_mp));\n\t//assert(num==accumulate(ans.begin(),ans.end(),0l));\n\twhile (Q--) {\n\t\tint n;cin>>n;\n\t\tcout<<ans[n]<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.2, "time_ms": 1520, "memory_kb": 50772, "score_of_the_acc": -1.1357, "final_rank": 16 } ]
aoj_1585_cpp
String in String Problem 文字列 S と Q 個のクエリが与えられる。 i 番目のクエリ(0 ≤ i ≤ Q -1)には閉区間[ l i , r i ]と文字列 M i が与えられる。 S の l i 文字目から r i 文字目までの部分文字列の中に文字列 M i はいくつ存在するか出力せよ。 Input 入力は以下の形式で与えられる。 S Q l 0 r 0 M 0 l 1 r 1 M 1 . . . l Q−1 r Q−1 M Q−1 1行目に文字列 S とクエリの数 Q が空白区切りで与えられる。 続く Q 行に整数 l i , r i , M i が空白区切りで与えられる。 Constraints 1 ≤| S | ≤ 100000 1 ≤ Q ≤ 100000 1 ≤ | M i | ≤ 100000 (0 ≤ i ≤ Q -1) 0 ≤ l i ≤ r i < | S | (0 ≤ i ≤ Q -1) 文字はすべて英小文字である 文字列 M の合計文字数は2000000を越えない Output 出力は Q 行からなる。各クエリに対する答えを順番に1行に出力せよ。 Sample Input 1 rupcrupc 5 0 3 rupc 0 7 rupc 2 7 ru 2 7 pc 1 5 u Sample Output 1 1 2 1 2 2 Sample Input 2 abatagaadbura 8 0 6 a 6 12 a 0 6 aa 0 3 a 3 5 a 5 9 a 1 8 b 1 12 b Sample Output 2 4 3 0 2 1 2 1 2 Sample Input 3 aaaaaaaaaa 5 0 9 aaa 0 9 aa 5 9 aaaa 2 8 aa 1 2 a Sample Output 3 8 9 2 6 2
[ { "submission_id": "aoj_1585_10072703", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Node {\n int next[26];\n int fail;\n vector<int> out;\n Node() : fail(0) {\n for(int i=0;i<26;i++) next[i]=-1;\n }\n};\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n string S; int Q;\n cin >> S >> Q;\n struct Query {int l, r; string m; int id;};\n vector<Query> qs(Q);\n vector<string> p;\n p.reserve(Q);\n for(auto &q: qs){cin>>q.l>>q.r>>q.m; p.push_back(q.m);}\n sort(p.begin(), p.end());\n p.erase(unique(p.begin(), p.end()), p.end());\n int P = p.size();\n vector<Node> trie(1);\n for(int i=0;i<P;i++){\n int node=0;\n for(char c:p[i]){\n if(trie[node].next[c-'a']==-1){\n trie[node].next[c-'a'] = trie.size();\n trie.emplace_back();\n }\n node = trie[node].next[c-'a'];\n }\n trie[node].out.push_back(i);\n }\n queue<int> q;\n for(int c=0;c<26;c++) {\n if(trie[0].next[c]!=-1){\n trie[trie[0].next[c]].fail=0;\n q.push(trie[0].next[c]);\n }\n else trie[0].next[c]=0;\n }\n while(!q.empty()){\n int current = q.front(); q.pop();\n for(int c=0;c<26;c++){\n if(trie[current].next[c]!=-1){\n int child = trie[current].next[c];\n int f = trie[current].fail;\n while(trie[f].next[c]==-1 && f!=0) f = trie[f].fail;\n if(trie[f].next[c]!=-1) trie[child].fail = trie[f].next[c];\n else trie[child].fail = 0;\n for(auto &x: trie[trie[child].fail].out) trie[child].out.push_back(x);\n q.push(child);\n }\n else trie[current].next[c] = trie[trie[current].fail].next[c];\n }\n }\n vector<vector<int>> o(P);\n int node=0;\n for(int i=0;i<S.size();i++){\n int c=S[i]-'a';\n node = trie[node].next[c];\n for(auto &x: trie[node].out){\n int pos = i - (int)p[x].size() +1;\n if(pos >=0) o[x].push_back(pos);\n }\n }\n for(auto &q: qs){\n q.id = lower_bound(p.begin(), p.end(), q.m) - p.begin();\n }\n for(auto &q: qs){\n int a=q.l, m=p[q.id].size(), b=q.r - m +1;\n if(b < a || m ==0){cout<< \"0\\n\"; continue;}\n cout<< upper_bound(o[q.id].begin(), o[q.id].end(), b) - lower_bound(o[q.id].begin(), o[q.id].end(), a) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 285384, "score_of_the_acc": -0.8385, "final_rank": 10 }, { "submission_id": "aoj_1585_10072702", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct N { \n int n[26], f; \n vector<int> o; \n N() : f(-1) { \n for(int i = 0; i < 26; i++) n[i] = -1; \n }\n};\nstruct Qr { int l, r; string m; int id; };\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0);\n \n string S; \n int Qc; \n cin >> S >> Qc;\n \n vector<Qr> qs(Qc); \n vector<string> p;\n for(auto &q: qs){\n cin >> q.l >> q.r >> q.m; \n p.push_back(q.m);\n }\n \n sort(p.begin(), p.end());\n p.erase(unique(p.begin(), p.end()), p.end());\n \n int P = p.size(); \n vector<N> t(1);\n \n for(int i = 0; i < P; i++){\n int node = 0; \n for(char c : p[i]){\n if(t[node].n[c - 'a'] == -1){\n t[node].n[c - 'a'] = t.size(); \n t.emplace_back();\n }\n node = t[node].n[c - 'a'];\n }\n t[node].o.push_back(i);\n }\n \n queue<int> q; \n for(int c = 0; c < 26; c++) {\n if(t[0].n[c] != -1){\n t[t[0].n[c]].f = 0; \n q.push(t[0].n[c]);\n }\n else {\n t[0].n[c] = 0;\n }\n }\n \n while(!q.empty()){\n int cur = q.front(); \n q.pop();\n for(int c = 0; c < 26; c++){\n if(t[cur].n[c] != -1){\n int child = t[cur].n[c];\n int f = t[cur].f;\n while(t[f].n[c] == -1 && f != 0) f = t[f].f;\n if(t[f].n[c] != -1) \n t[child].f = t[f].n[c];\n else \n t[child].f = 0;\n \n for(auto &x: t[t[child].f].o) \n t[child].o.push_back(x);\n \n q.push(child);\n }\n else {\n t[cur].n[c] = t[t[cur].f].n[c];\n }\n }\n }\n \n vector<vector<int>> o(P);\n int node = 0;\n for(int i = 0; i < S.size(); i++){\n int c = S[i] - 'a';\n node = t[node].n[c];\n for(auto &x: t[node].o){\n int pos = i - (int)p[x].size() + 1;\n if(pos >= 0) \n o[x].push_back(pos);\n }\n }\n \n for(auto &q: qs){\n q.id = lower_bound(p.begin(), p.end(), q.m) - p.begin();\n }\n \n for(auto &q: qs){\n int a = q.l; \n int m = p[q.id].size(); \n int b = q.r - m + 1;\n if(b < a || m == 0){\n cout << \"0\\n\"; \n continue;\n }\n cout << upper_bound(o[q.id].begin(), o[q.id].end(), b) - lower_bound(o[q.id].begin(), o[q.id].end(), a) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 285908, "score_of_the_acc": -0.8398, "final_rank": 11 }, { "submission_id": "aoj_1585_9734628", "code_snippet": "#include<bits/stdc++.h>\n#define pb push_back\n#define ll long long\nusing namespace std;\nstruct suffix_array {\n\tvector<int> sa, rk, lcp;\n\t//sa:后缀排序;rk:后缀名次;lcp:最长公共前缀\n\tint len; string s;\n\tvoid getsa() {\n\t\tsa.clear(); rk.clear(); sa.resize(len + 1, 0); rk.resize(len + 1, 0); sa[0] = len;\n\t\tfor(int i = 1; i <= len; i++) sa[i] = i - 1;\n\t\tsort(sa.begin() + 1, sa.end(), [&](int a, int b) {return s[a] < s[b];});\n\t\tfor(int i = 1; i <= len; i++) {\n\t\t\tint a = sa[i - 1], b = sa[i];\n\t\t\tif(i > 1 && s[a] == s[b]) rk[b] = rk[a];\n\t\t\telse rk[b] = i;\n\t\t}\n\t\tfor(int siz = 1; siz <= len; siz *= 2) {\n\t\t\tvector<int> lsa(sa), lrk(rk), cnt(len + 1);\n\t\t\tfor(int i = 0; i <= len; i++) cnt[i] = i;\n\t\t\tfor(auto t : lsa) {\n\t\t\t\tint T = t - siz;\n\t\t\t\tif(T >= 0) sa[cnt[lrk[T]]++] = T;\n\t\t\t}\n\t\t\tfor(int i = 1; i <= len; i++) {\n\t\t\t\tint a = sa[i - 1], b = sa[i];\n\t\t\t\tif(lrk[a] == lrk[b] && lrk[a + siz] == lrk[b + siz]) rk[b] = rk[a];\n\t\t\t\telse rk[b] = i;\n\t\t\t}\n\t\t}\n\t}\n\tvoid getlcp() {\n\t\tlcp.clear(); lcp.resize(len, 0); int h = 0;\n\t\tfor(int b = 0; b < len; b++) {\n\t\t\tint a = sa[rk[b] - 1];\n\t\t\twhile(a + h < len && s[a + h] == s[b + h]) ++h;\n\t\t\tlcp[rk[b] - 1] = h; if(h) --h; \n\t\t}\n\t}\n\tvoid init(string _s) {\n\t\ts = _s; len = (int)s.size();\n\t\tgetsa(); getlcp();\n\t}\n\tint bs_rit(string &t) {\n\t\tint l = 0, r = len + 1;\n\t\twhile(l + 1 <= r - 1) {\n\t\t\tint mid = (l + r) >> 1;\n\t\t\tbool fl = 1; int st = sa[mid];\n\t\t\tfor(int i = 0; i < (int)t.size(); i++) {\n\t\t\t\tif(st + i >= len || s[st + i] < t[i]) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif(s[st + i] > t[i]) {\n\t\t\t\t\tfl = 0; break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(fl) l = mid;\n\t\t\telse r = mid;\n\t\t}\n\t\treturn l;\n\t}\n\tint bs_lft(string &t) {\n\t\tint l = 0, r = len + 1;\n\t\twhile(l + 1 <= r - 1) {\n\t\t\tint mid = (l + r) >> 1;\n\t\t\tbool fl = 0; int st = sa[mid];\n\t\t\tfor(int i = 0; i < (int)t.size(); i++) {\n\t\t\t\tif(st + i >= len || s[st + i] < t[i]) {\n\t\t\t\t\tfl = 1; break;\n\t\t\t\t}\n\t\t\t\tif(s[st + i] > t[i]) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(fl) l = mid;\n\t\t\telse r = mid;\n\t\t}\n\t\treturn r;\n\t}\n}sa; // 不会写后缀自动机所以就放弃了(?)\n// 二维数点离线怎么做啊\nint ls[100005 << 5], rs[100005 << 5], sum[100005 << 5], rt[100005], tot;\nint build(int l, int r) {\n\tint root = ++tot;\n\tif (l == r) return root;\n\tint mid = (l + r) >> 1;\n\tls[root] = build(l, mid);\n\trs[root] = build(mid + 1, r);\n\treturn root;\n} // 甚至是从oi-wiki抄的板子\nint update(int k, int l, int r, int root) {\n\tint dir = ++tot;\n\tls[dir] = ls[root], rs[dir] = rs[root], sum[dir] = sum[root] + 1;\n\tif (l == r) return dir;\n\tint mid = (l + r) >> 1;\n\tif (k <= mid)\n\t\tls[dir] = update(k, l, mid, ls[dir]);\n\telse\n\t\trs[dir] = update(k, mid + 1, r, rs[dir]);\n return dir;\n}\nint query(int u, int v, int l, int r, int ql, int qr) {\n\tint mid = (l + r) >> 1;\n\tif(ql <= l && r <= qr) return sum[v] - sum[u];\n\tint ans = 0;\n\tif(ql <= mid)\n\t\tans += query(ls[u], ls[v], l, mid, ql, qr);\n\tif(mid + 1 <= qr)\n\t\tans += query(rs[u], rs[v], mid + 1, r, ql, qr);\n\treturn ans;\n}\nstring s, t;\nint n, q, l, r;\nint main() {\n\tcin >> s >> q;\n\tsa.init(s); n = (int)s.size();\n\t/*for(int i = 0; i < n; i++) cout << sa.rk[i] << \" \";\n\tcout << endl;\n\tfor(int i = 1; i <= n; i++) cout << sa.sa[i] << \" \";\n\tcout << endl;*/\n\trt[0] = build(1, n);\n\tfor(int i = 1; i <= n; i++)\n\t\trt[i] = update(sa.rk[i - 1], 1, n, rt[i - 1]);\n\twhile(q--) {\n\t\tcin >> l >> r >> t; l++; r++;\n\t\tint L = sa.bs_lft(t), R = sa.bs_rit(t);\n\t\tif(L > R || r - l + 1 < (int)t.size())\n\t\t\tcout << 0 << endl;\n\t\telse\n\t\t\tcout << query(rt[l - 1], rt[r - (int)t.size() + 1], 1, n, L, R) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 32524, "score_of_the_acc": -0.1809, "final_rank": 3 }, { "submission_id": "aoj_1585_9734627", "code_snippet": "#include<bits/stdc++.h>\n#define pb push_back\n#define ll long long\nusing namespace std;\nstruct suffix_array {\n\tvector<int> sa, rk, lcp;\n\t//sa:后缀排序;rk:后缀名次;lcp:最长公共前缀\n\tint len; string s;\n\tvoid getsa() {\n\t\tsa.clear(); rk.clear(); sa.resize(len + 1, 0); rk.resize(len + 1, 0); sa[0] = len;\n\t\tfor(int i = 1; i <= len; i++) sa[i] = i - 1;\n\t\tsort(sa.begin() + 1, sa.end(), [&](int a, int b) {return s[a] < s[b];});\n\t\tfor(int i = 1; i <= len; i++) {\n\t\t\tint a = sa[i - 1], b = sa[i];\n\t\t\tif(i > 1 && s[a] == s[b]) rk[b] = rk[a];\n\t\t\telse rk[b] = i;\n\t\t}\n\t\tfor(int siz = 1; siz <= len; siz *= 2) {\n\t\t\tvector<int> lsa(sa), lrk(rk), cnt(len + 1);\n\t\t\tfor(int i = 0; i <= len; i++) cnt[i] = i;\n\t\t\tfor(auto t : lsa) {\n\t\t\t\tint T = t - siz;\n\t\t\t\tif(T >= 0) sa[cnt[lrk[T]]++] = T;\n\t\t\t}\n\t\t\tfor(int i = 1; i <= len; i++) {\n\t\t\t\tint a = sa[i - 1], b = sa[i];\n\t\t\t\tif(lrk[a] == lrk[b] && lrk[a + siz] == lrk[b + siz]) rk[b] = rk[a];\n\t\t\t\telse rk[b] = i;\n\t\t\t}\n\t\t}\n\t}\n\tvoid getlcp() {\n\t\tlcp.clear(); lcp.resize(len, 0); int h = 0;\n\t\tfor(int b = 0; b < len; b++) {\n\t\t\tint a = sa[rk[b] - 1];\n\t\t\twhile(a + h < len && s[a + h] == s[b + h]) ++h;\n\t\t\tlcp[rk[b] - 1] = h; if(h) --h; \n\t\t}\n\t}\n\tvoid init(string _s) {\n\t\ts = _s; len = (int)s.size();\n\t\tgetsa(); getlcp();\n\t}\n\tint bs_rit(string &t) {\n\t\tint l = 0, r = len + 1;\n\t\twhile(l + 1 <= r - 1) {\n\t\t\tint mid = (l + r) >> 1;\n\t\t\tbool fl = 1; int st = sa[mid];\n\t\t\tfor(int i = 0; i < (int)t.size(); i++) {\n\t\t\t\tif(st + i >= len || s[st + i] < t[i]) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif(s[st + i] > t[i]) {\n\t\t\t\t\tfl = 0; break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(fl) l = mid;\n\t\t\telse r = mid;\n\t\t}\n\t\treturn l;\n\t}\n\tint bs_lft(string &t) {\n\t\tint l = 0, r = len + 1;\n\t\twhile(l + 1 <= r - 1) {\n\t\t\tint mid = (l + r) >> 1;\n\t\t\tbool fl = 0; int st = sa[mid];\n\t\t\tfor(int i = 0; i < (int)t.size(); i++) {\n\t\t\t\tif(st + i >= len || s[st + i] < t[i]) {\n\t\t\t\t\tfl = 1; break;\n\t\t\t\t}\n\t\t\t\tif(s[st + i] > t[i]) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(fl) l = mid;\n\t\t\telse r = mid;\n\t\t}\n\t\treturn r;\n\t}\n}sa; // 不会写后缀自动机所以就放弃了(?)\n// 二维数点离线怎么做啊\nint ls[100005 << 4], rs[100005 << 4], sum[100005 << 4], rt[100005], tot;\nint build(int l, int r) {\n\tint root = ++tot;\n\tif (l == r) return root;\n\tint mid = (l + r) >> 1;\n\tls[root] = build(l, mid);\n\trs[root] = build(mid + 1, r);\n\treturn root;\n} // 甚至是从oi-wiki抄的板子\nint update(int k, int l, int r, int root) {\n\tint dir = ++tot;\n\tls[dir] = ls[root], rs[dir] = rs[root], sum[dir] = sum[root] + 1;\n\tif (l == r) return dir;\n\tint mid = (l + r) >> 1;\n\tif (k <= mid)\n\t\tls[dir] = update(k, l, mid, ls[dir]);\n\telse\n\t\trs[dir] = update(k, mid + 1, r, rs[dir]);\n return dir;\n}\nint query(int u, int v, int l, int r, int ql, int qr) {\n\tint mid = (l + r) >> 1;\n\tif(ql <= l && r <= qr) return sum[v] - sum[u];\n\tint ans = 0;\n\tif(ql <= mid)\n\t\tans += query(ls[u], ls[v], l, mid, ql, qr);\n\tif(mid + 1 <= qr)\n\t\tans += query(rs[u], rs[v], mid + 1, r, ql, qr);\n\treturn ans;\n}\nstring s, t;\nint n, q, l, r;\nint main() {\n\tcin >> s >> q;\n\tsa.init(s); n = (int)s.size();\n\t/*for(int i = 0; i < n; i++) cout << sa.rk[i] << \" \";\n\tcout << endl;\n\tfor(int i = 1; i <= n; i++) cout << sa.sa[i] << \" \";\n\tcout << endl;*/\n\trt[0] = build(1, n);\n\tfor(int i = 1; i <= n; i++)\n\t\trt[i] = update(sa.rk[i - 1], 1, n, rt[i - 1]);\n\twhile(q--) {\n\t\tcin >> l >> r >> t; l++; r++;\n\t\tint L = sa.bs_lft(t), R = sa.bs_rit(t);\n\t\tif(L > R || r - l + 1 < (int)t.size())\n\t\t\tcout << 0 << endl;\n\t\telse\n\t\t\tcout << query(rt[l - 1], rt[r - (int)t.size() + 1], 1, n, L, R) << endl;\n\t}\n\treturn 0;\n}", "accuracy": 0.484375, "time_ms": 140, "memory_kb": 21912, "score_of_the_acc": -0.1141, "final_rank": 19 }, { "submission_id": "aoj_1585_5001329", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing i64 = int64_t;\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/rollinghashmonoid.cpp.html\nstruct RollingHashMonoid{\n using Hash = RollingHashMonoid;\n using u64 = uint64_t;\n static constexpr u64 mod = (1uLL << 61) - 1;\n static constexpr u64 m30 = (1uLL << 30) - 1;\n static constexpr u64 m31 = (1uLL << 31) - 1;\n static constexpr u64 m61 = mod;\n static constexpr u64 _base = 550390130435464343;\n static constexpr u64 _base_inv = 1803816245541264939;\n static vector<u64> base, base_inv;\n\n static inline u64 calc_mod(u64 x){\n u64 xu = x >> 61;\n u64 xd = x & m61;\n u64 res = xu + xd;\n if(res >= mod)\n res -= mod;\n return res;\n }\n\n static inline u64 raw_mul(u64 a, u64 b){\n u64 au = a >> 31;\n u64 ad = a & m31;\n u64 bu = b >> 31;\n u64 bd = b & m31;\n u64 mid = ad * bu + au * bd;\n u64 midu = mid >> 30;\n u64 midd = mid & m30;\n return au * bu * 2 + midu + (midd << 31) + ad * bd;\n }\n\n static void remake_table(int len){\n while(base.size() <= len){\n base.emplace_back(calc_mod(raw_mul(base.back(), _base)));\n base_inv.emplace_back(calc_mod(raw_mul(base_inv.back(), _base_inv)));\n }\n }\n\n u64 h, l;\n\n static inline u64 lshift(u64 a, int len){\n remake_table(len);\n return raw_mul(a, base[len]);\n }\n static inline u64 rshift(u64 a, int len){\n remake_table(len);\n return raw_mul(a, base_inv[len]);\n }\n\n Hash concat(const Hash& b) const{\n return RollingHashMonoid(\n calc_mod(h + lshift(b.h, l)),\n l + b.l\n );\n }\n Hash sub(const Hash& b) const{\n return RollingHashMonoid(\n calc_mod(rshift(h + mod - b.h, b.l)),\n l - b.l\n );\n }\n\n RollingHashMonoid() : h(0), l(0){}\n RollingHashMonoid(u64 x) : h(x), l(1){}\n RollingHashMonoid(u64 h, u64 l) : h(h), l(l){}\n\n friend bool operator==(const Hash& a, const Hash& b){return a.h == b.h && a.l == b.l;}\n\n template <typename T>\n static vector<Hash> make(vector<T>& x){\n vector<Hash> v(x.size() + 1);\n for(int i = 0; i < x.size(); ++i){\n v[i + 1] = v[i].concat(x[i]);\n }\n return v;\n }\n static vector<Hash> make(string& x){\n vector<Hash> v(x.size() + 1);\n for(int i = 0; i < x.size(); ++i){\n v[i + 1] = v[i].concat(x[i]);\n }\n return v;\n }\n};\n\nnamespace std{\ntemplate<> struct hash<RollingHashMonoid>{\n size_t operator()(const RollingHashMonoid x) const noexcept{\n return x.concat(x.l).h;\n }\n};\n}\n\n\nusing Hash = RollingHashMonoid;\nvector<uint64_t> Hash::base = {1};\nvector<uint64_t> Hash::base_inv = {1};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/hashmap.cpp.html\ntemplate <typename T, typename U, T del = numeric_limits<T>::max(), T null = numeric_limits<T>::max() - 1>\nstruct HashMap{\n static constexpr __int128_t z = 0xf332ac987401cba5;\n uint64_t n, q, d;\n\n vector<pair<T, U>> v;\n\n HashMap() : n(0), q(0), d(1), v(2, make_pair(null, U())){\n }\n\n inline uint64_t hash(T key){return uint64_t((z * __int128_t(key)) >> (64 - d)) & ((1LL << d) - 1);}\n\n pair<U, bool> find(T x){\n for(uint64_t i = hash(x); v[i].first != null; i = (i + 1) & ((1 << d) - 1))\n if(v[i].first == x)\n return make_pair(v[i].second, true);\n return make_pair(U(), false);\n }\n\n bool add(T x, U val){\n if(find(x).second)\n return false;\n if(((q + 1) << 1) > (1 << d) || (1 << d) < 3 * n)\n resize();\n uint64_t i = hash(x);\n for(; v[i].first != null && v[i].first != del; i = (i + 1) & ((1 << d) - 1));\n q += (v[i].first == null);\n ++n;\n v[i] = make_pair(x, val);\n return true;\n }\n\n bool erase(T x){\n uint64_t i = hash(x);\n for(; v[i].first != null && v[i].first != x; i = (i + 1) & ((1 << d) - 1));\n if(v[i].first == null)\n return false;\n --n;\n v[i] = make_pair(del, U());\n return true;\n }\n\n void resize(){\n ++d;\n vector<pair<T, U>> old_table;\n q = n;\n swap(old_table, v);\n v.assign(1 << d, make_pair(null, U()));\n n = 0;\n for(int i = 0; i < old_table.size(); ++i)\n if(old_table[i].first != null && old_table[i].first != del)\n add(old_table[i].first, old_table[i].second);\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/avl_map.cpp.html\ntemplate <typename T, typename U>\nstruct AVL_map{\n struct Node{\n int height;\n T key;\n U val;\n Node(T key, U val) : key(key), val(val), height(1){c[0] = 0; c[1] = 0;}\n int c[2];\n };\n vector<Node> nodes;\n stack<int> st;\n AVL_map(){\n nodes.emplace_back(T(), U());\n nodes[0].height = 0;\n }\n template <bool inv>\n int balance_factor(int x){return (nodes[nodes[x].c[0]].height - nodes[nodes[x].c[1]].height) * (inv ? -1 : 1);}\n void _update(int x){\n if(x == 0)\n return;\n nodes[x].height = max(nodes[nodes[x].c[0]].height, nodes[nodes[x].c[1]].height) + 1;\n }\n template <bool is_right>\n int rotate(int x){\n int y = nodes[x].c[1 ^ is_right];\n nodes[x].c[1 ^ is_right] = nodes[y].c[0 ^ is_right];\n nodes[y].c[0 ^ is_right] = x;\n _update(x);\n _update(y);\n return y;\n }\n template <bool inv>\n int _balance(int x){\n if(balance_factor<inv>(x) == 2){\n if(balance_factor<inv>(nodes[x].c[0 ^ inv]) < 0)\n nodes[x].c[0 ^ inv] = rotate<inv>(nodes[x].c[0 ^ inv]);\n x = rotate<1 ^ inv>(x);\n }\n return x;\n }\n int balance(int x){\n x = _balance<false>(x);\n x = _balance<true>(x);\n _update(x);\n return x;\n }\n int add(int x, T key, U val){\n if(x == 0){\n if(st.empty()){\n nodes.emplace_back(key, val);\n return nodes.size() - 1;\n }\n else{\n int y = st.top();\n st.pop();\n nodes[y] = Node(key, val);\n return y;\n }\n }\n else if(key == nodes[x].key)\n nodes[x].val = val;\n else if(key < nodes[x].key)\n nodes[x].c[0] = add(nodes[x].c[0], key, val);\n else\n nodes[x].c[1] = add(nodes[x].c[1], key, val);\n x = balance(x);\n return x;\n }\n // 子が片方しかない時にノードを削除する\n int _erase_top(int x, bool del){\n for(int i = 0; i < 2; ++i){\n if(nodes[x].c[i] == 0){\n int y = nodes[x].c[i ^ 1];\n if(del)\n st.push(x);\n return y;\n }\n }\n return 0;\n }\n // 最小の要素をdstにコピーしてから削除する\n int _copy_erase(int x, int dst, bool del){\n if(nodes[x].c[0] == 0){\n nodes[dst].val = nodes[x].val;\n return _erase_top(x, del);\n }\n nodes[x].c[0] = _copy_erase(nodes[x].c[0], dst, del);\n x = balance(x);\n return x;\n }\n int erase(int x, T key, bool del = true){\n if(key < nodes[x].key)\n nodes[x].c[0] = erase(nodes[x].c[0], key, del);\n else if(nodes[x].key < key)\n nodes[x].c[1] = erase(nodes[x].c[1], key, del);\n else{\n if(nodes[x].c[0] == 0 || nodes[x].c[1] == 0)\n return _erase_top(x, del);\n nodes[x].c[1] = _copy_erase(nodes[x].c[1], x, del);\n }\n x = balance(x);\n return x;\n }\n pair<U, bool> get(int x, T key){\n if(x == 0)\n return {U(), false};\n else if(key == nodes[x].key)\n return {nodes[x].val, true};\n else if(key < nodes[x].key)\n return get(nodes[x].c[0], key);\n else\n return get(nodes[x].c[1], key);\n }\n vector<pair<T, U>> list(int x){\n vector<pair<T, U>> v;\n stack<pair<int,bool>> sta;\n sta.emplace(x, false);\n bool add;\n while(!sta.empty()){\n tie(x, add) = sta.top();\n sta.pop();\n if(x == 0)\n continue;\n if(add)\n v.emplace_back(nodes[x].key, nodes[x].val);\n else{\n if(nodes[x].c[1])\n sta.emplace(nodes[x].c[1], false);\n sta.emplace(x, true);\n if(nodes[x].c[0])\n sta.emplace(nodes[x].c[0], false);\n }\n }\n return v;\n }\n};\n\n\nstruct HashPatriciaTree{\n struct Node{\n bool is_elm = false;\n int ch = 0;\n int len = 0;\n int par = -1;\n int data_idx = -1;\n Hash hash;\n };\n\n static int fat(int l, int r){\n return r & (-1 << (31 - __builtin_clz(l ^ r)));\n };\n\n AVL_map<int,int> avl;\n vector<function<Hash(int)>> hash_func;\n vector<Node> nodes;\n HashMap<uint64_t, int> hmap;\n vector<int> idxes;\n\n HashPatriciaTree() : nodes(1){\n }\n\n void make_fat(int x){\n int st = nodes[nodes[x].par].len;\n int en = nodes[x].len;\n int fa = fat(st, en);\n auto ha = hash_func[nodes[x].data_idx](fa);\n nodes[x].hash = ha;\n hmap.add(ha.concat(ha.l).h, x);\n }\n\n void make(int idx, int target, int len, int label){\n nodes.emplace_back();\n nodes.back().len = len;\n nodes.back().data_idx = idx;\n nodes.back().par = target;\n nodes[target].ch = avl.add(nodes[target].ch, label, nodes.size() - 1);\n make_fat(nodes.size() - 1);\n }\n\n void split(int idx, int target, int len, int match_len, int new_label, int root_label, int branch_label){\n int par = nodes[target].par;\n nodes.emplace_back();\n nodes.back().len = match_len;\n nodes.back().data_idx = idx;\n nodes.back().par = par;\n\n Hash ha = nodes[target].hash;\n hmap.erase(ha.concat(ha.l).h);\n\n nodes[par].ch = avl.add(nodes[par].ch, root_label, nodes.size() - 1);\n nodes.back().ch = avl.add(nodes.back().ch, branch_label, target);\n nodes[target].par = nodes.size() - 1;\n int target_siz = nodes.size() - 1;\n if(nodes.back().len != len)\n make(idx, nodes.size() - 1, len, new_label);\n make_fat(target);\n make_fat(target_siz);\n }\n\n int access(function<Hash(int)>& f, int i){\n return f(i + 1).sub(f(i)).h;\n }\n\n // f(x): 長さxのprefixのハッシュを返す\n // g(x): x文字目を返す\n void insert(int m, function<Hash(int)> f){\n hash_func.emplace_back(f);\n int len, x;\n tie(len, x) = match(m, f);\n assert(!(len || x) || (nodes[nodes[x].par].len < len && len <= nodes[x].len));\n if(len == nodes[x].len){\n if(len == m){\n idxes.emplace_back(x);\n nodes[x].data_idx = idxes.size() - 1;\n nodes[x].is_elm = true;\n return;\n }\n else\n make(idxes.size(), x, m, access(f, len));\n }\n else{\n split(idxes.size(), x, m, len, access(f, len), access(hash_func[nodes[x].data_idx], nodes[nodes[x].par].len), access(hash_func[nodes[x].data_idx], len));\n }\n idxes.emplace_back(nodes.size() - 1);\n nodes.back().is_elm = true;\n }\n\n int lcp(int ok, int ng, function<Hash(int)> f, function<Hash(int)> g){\n while(ng - ok > 1){\n int mid = (ok + ng) >> 1;\n (f(mid) == g(mid) ? ok : ng) = mid;\n }\n return ok;\n }\n\n // (マッチ長, ノード)\n pair<int,int> match(int m, function<Hash(int)> f){\n int ok = 0, ng = m + 1;\n int x = 0;\n int ok_len = 0;\n while(ng - ok > 1){\n int mid = fat(ok + 1, ng - 1);\n Hash h = f(mid);\n int idx;\n bool fl;\n tie(idx, fl) = hmap.find(h.concat(h.l).h);\n if(fl){\n x = idx;\n ok_len = ok;\n ok = nodes[x].len;\n }\n else{\n ng = mid;\n }\n }\n int match_len;\n if(ok == 0){\n match_len = 0;\n }\n else{\n match_len = lcp(ok_len, min(m, nodes[x].len) + 1, f, hash_func[nodes[x].data_idx]);\n if(match_len < nodes[x].len){\n return {match_len, x};\n }\n }\n int label = access(f, ok);\n int ch;\n bool fl;\n tie(ch, fl) = avl.get(nodes[x].ch, label);\n if(!fl){\n assert(match_len <= m);\n return {match_len, x};\n }\n match_len = lcp(match_len, min(m, nodes[ch].len) + 1, f, hash_func[nodes[ch].data_idx]);\n assert(match_len <= m);\n return {match_len, ch};\n }\n static function<Hash(int)> build(vector<Hash>& v){\n return [&v](int i){\n return v[i];\n };\n }\n static vector<function<Hash(int)>> build_suffix(vector<Hash>& v){\n vector<function<Hash(int)>> fv;\n vector<function<int(int)>> gv;\n for(int j = 0; j < v.size() - 1; ++j){\n fv.emplace_back([j, &v](int i){\n return v[i + j].sub(v[j]);\n });\n }\n return fv;\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/bitvector.cpp.html\nstruct BitVector{\n vector<uint64_t> v;\n vector<int> r;\n BitVector(){}\n void build(){\n r.assign(v.size() + 1, 0);\n for(int i = 0; i < v.size(); ++i)\n r[i + 1] = r[i] + __builtin_popcountll(v[i]);\n }\n bool access(int x){\n return (v[x >> 6] >> (x & 63)) & 1;\n }\n // [0, x)の1の出現回数\n int rank(int x){\n return r[x >> 6] + __builtin_popcountll(v[x >> 6] & ((1uLL << (x & 63)) - 1));\n }\n int rank(int x, bool fl){\n return fl ? rank(x) : x - rank(x);\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/waveletmatrix.cpp.html\ntemplate <typename T, int W>\nstruct WaveletMatrix{\n\n array<BitVector, W> bv;\n array<int, W> zero_cnt;\n\n WaveletMatrix(vector<T>& a){\n int n = a.size();\n vector<T> v(a);\n for(int i = W - 1; i >= 0; --i){\n vector<uint64_t> b((n >> 6) + 1, 0);\n vector<T> v1, v2;\n for(int j = 0; j < n; ++j){\n ((v[j] >> i) & 1 ? v2 : v1).push_back(v[j]);\n b[j >> 6] |= uint64_t((v[j] >> i) & 1) << (j & 63);\n }\n for(int j = 0; j < v.size(); ++j)\n v[j] = (j < v1.size() ? v1[j] : v2[j - v1.size()]);\n bv[i].v = move(b);\n bv[i].build();\n zero_cnt[i] = bv[i].rank(n, 0);\n }\n }\n\n // [l, r)内のxの数\n int count(int l, int r, T x){\n for(int i = W - 1; i >= 0; --i){\n bool fl = (x >> i) & 1;\n int st = bv[i].rank(l, fl);\n int en = bv[i].rank(r, fl);\n l = (fl ? zero_cnt[i] : 0) + st;\n r = (fl ? zero_cnt[i] : 0) + en;\n }\n return r - l;\n }\n\n // [l, r)内で[0, x)を満たす値の数\n int count_lower(int l, int r, T x){\n int cnt = 0;\n for(int i = W - 1; i >= 0; --i){\n bool fl = (x >> i) & 1;\n int st = bv[i].rank(l, fl);\n int en = bv[i].rank(r, fl);\n if(fl){\n st += zero_cnt[i];\n en += zero_cnt[i];\n cnt += (bv[i].rank(r, 0) - bv[i].rank(l, 0));\n }\n l = st, r = en;\n }\n return cnt;\n }\n\n // [l, r)内で[x, y)を満たす値の数\n int count_range(int l, int r, T x, T y){\n return count_lower(l, r, y) - count_lower(l, r, x);\n }\n\n // 0-indexedでk番目に小さいものを返す\n T kth_min(int l, int r, int k){\n T ans = 0;\n for(int i = W - 1; i >= 0; --i){\n int st = bv[i].rank(l, 0);\n int en = bv[i].rank(r, 0);\n if(en - st <= k){\n k -= en - st;\n l = zero_cnt[i] + bv[i].rank(l, 1);\n r = zero_cnt[i] + bv[i].rank(r, 1);\n ans |= (1uLL << i);\n }\n else{\n l = st, r = en;\n }\n }\n return ans;\n }\n\n // [l, r)でのx以上最小値\n pair<T, bool> predecessor(int l, int r, T x){\n int idx = count_lower(l, r, x);\n if(idx == r - l){\n return make_pair((1uLL << W) - 1, false);\n }\n return make_pair(kth_min(l, r, idx), true);\n }\n\n // [l, r)でのx以下最大値\n pair<T, bool> successor(int l, int r, T x){\n int idx = count_lower(l, r, x + 1);\n if(idx == 0)\n return make_pair(0, false);\n return make_pair(kth_min(l, r, idx - 1), true);\n }\n};\n\n\nsigned main(){\n HashPatriciaTree tree;\n string s;\n int q;\n cin >> s >> q;\n\n int n = s.size();\n vector<Hash> sh = Hash::make(s);\n auto s_res = HashPatriciaTree::build_suffix(sh);\n for(int i = 0; i < n; ++i){\n tree.insert(n - i, s_res[i]);\n }\n\n vector<int> v;\n int k = tree.nodes.size();\n vector<int> in(k), out(k);\n function<void(int)> f = [&](int x){\n in[x] = v.size();\n v.emplace_back(tree.nodes[x].is_elm ? tree.nodes[x].data_idx : n);\n for(auto p : tree.avl.list(tree.nodes[x].ch)){\n f(p.second);\n }\n out[x] = v.size();\n };\n f(0);\n\n WaveletMatrix<int, 20> wm(v);\n\n for(int i = 0; i < q; ++i){\n int l, r;\n string t;\n cin >> l >> r >> t;\n ++r;\n auto h = Hash::make(t);\n auto res = HashPatriciaTree::build(h);\n int len, node;\n tie(len, node) = tree.match(t.size(), res);\n if(len < t.size()){\n cout << 0 << endl;\n continue;\n }\n int in_x = in[node];\n int out_x = out[node];\n\n int left = l;\n int right = max(left, min(int(v.size()), r + 1 - int(t.size())));\n if(right <= left)\n cout << 0 << endl;\n else\n cout << wm.count_range(in_x, out_x, left, right) << endl;\n }\n}", "accuracy": 0.515625, "time_ms": 220, "memory_kb": 36160, "score_of_the_acc": -0.1955, "final_rank": 17 }, { "submission_id": "aoj_1585_5001247", "code_snippet": "#line 1 \"aoj_1585.test.cpp\"\n#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1585\"\n\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing i64 = int64_t;\n\n#line 1 \"/home/shibh308/workspace/compro/competitive-programming/library/lib/classes/rollinghashmonoid.cpp\"\nstruct RollingHashMonoid{\n using Hash = RollingHashMonoid;\n using u64 = uint64_t;\n static constexpr u64 mod = (1uLL << 61) - 1;\n static constexpr u64 m30 = (1uLL << 30) - 1;\n static constexpr u64 m31 = (1uLL << 31) - 1;\n static constexpr u64 m61 = mod;\n static constexpr u64 _base = 550390130435464343;\n static constexpr u64 _base_inv = 1803816245541264939;\n static vector<u64> base, base_inv;\n\n static inline u64 calc_mod(u64 x){\n u64 xu = x >> 61;\n u64 xd = x & m61;\n u64 res = xu + xd;\n if(res >= mod)\n res -= mod;\n return res;\n }\n\n static inline u64 raw_mul(u64 a, u64 b){\n u64 au = a >> 31;\n u64 ad = a & m31;\n u64 bu = b >> 31;\n u64 bd = b & m31;\n u64 mid = ad * bu + au * bd;\n u64 midu = mid >> 30;\n u64 midd = mid & m30;\n return au * bu * 2 + midu + (midd << 31) + ad * bd;\n }\n\n static void remake_table(int len){\n while(base.size() <= len){\n base.emplace_back(calc_mod(raw_mul(base.back(), _base)));\n base_inv.emplace_back(calc_mod(raw_mul(base_inv.back(), _base_inv)));\n }\n }\n\n u64 h, l;\n\n static inline u64 lshift(u64 a, int len){\n remake_table(len);\n return raw_mul(a, base[len]);\n }\n static inline u64 rshift(u64 a, int len){\n remake_table(len);\n return raw_mul(a, base_inv[len]);\n }\n\n Hash concat(const Hash& b) const{\n return RollingHashMonoid(\n calc_mod(h + lshift(b.h, l)),\n l + b.l\n );\n }\n Hash sub(const Hash& b) const{\n return RollingHashMonoid(\n calc_mod(rshift(h + mod - b.h, b.l)),\n l - b.l\n );\n }\n\n RollingHashMonoid() : h(0), l(0){}\n RollingHashMonoid(u64 x) : h(x), l(1){}\n RollingHashMonoid(u64 h, u64 l) : h(h), l(l){}\n\n friend bool operator==(const Hash& a, const Hash& b){return a.h == b.h && a.l == b.l;}\n\n template <typename T>\n static vector<Hash> make(vector<T>& x){\n vector<Hash> v(x.size() + 1);\n for(int i = 0; i < x.size(); ++i){\n v[i + 1] = v[i].concat(x[i]);\n }\n return v;\n }\n static vector<Hash> make(string& x){\n vector<Hash> v(x.size() + 1);\n for(int i = 0; i < x.size(); ++i){\n v[i + 1] = v[i].concat(x[i]);\n }\n return v;\n }\n};\n\nnamespace std{\ntemplate<> struct hash<RollingHashMonoid>{\n size_t operator()(const RollingHashMonoid x) const noexcept{\n return x.concat(x.l).h;\n }\n};\n}\n\n\nusing Hash = RollingHashMonoid;\nvector<uint64_t> Hash::base = {1};\nvector<uint64_t> Hash::base_inv = {1};\n#line 1 \"/home/shibh308/workspace/compro/competitive-programming/library/lib/classes/hashmap.cpp\"\ntemplate <typename T, typename U, T del = numeric_limits<T>::max(), T null = numeric_limits<T>::max() - 1>\nstruct HashMap{\n static constexpr __int128_t z = 0xf332ac987401cba5;\n uint64_t n, q, d;\n\n vector<pair<T, U>> v;\n\n HashMap() : n(0), q(0), d(1), v(2, make_pair(null, U())){\n }\n\n inline uint64_t hash(T key){return uint64_t((z * __int128_t(key)) >> (64 - d)) & ((1LL << d) - 1);}\n\n pair<U, bool> find(T x){\n for(uint64_t i = hash(x); v[i].first != null; i = (i + 1) & ((1 << d) - 1))\n if(v[i].first == x)\n return make_pair(v[i].second, true);\n return make_pair(U(), false);\n }\n\n bool add(T x, U val){\n if(find(x).second)\n return false;\n if(((q + 1) << 1) > (1 << d) || (1 << d) < 3 * n)\n resize();\n uint64_t i = hash(x);\n for(; v[i].first != null && v[i].first != del; i = (i + 1) & ((1 << d) - 1));\n q += (v[i].first == null);\n ++n;\n v[i] = make_pair(x, val);\n return true;\n }\n\n bool erase(T x){\n uint64_t i = hash(x);\n for(; v[i].first != null && v[i].first != x; i = (i + 1) & ((1 << d) - 1));\n if(v[i].first == null)\n return false;\n --n;\n v[i] = make_pair(del, U());\n return true;\n }\n\n void resize(){\n ++d;\n vector<pair<T, U>> old_table;\n q = n;\n swap(old_table, v);\n v.assign(1 << d, make_pair(null, U()));\n n = 0;\n for(int i = 0; i < old_table.size(); ++i)\n if(old_table[i].first != null && old_table[i].first != del)\n add(old_table[i].first, old_table[i].second);\n }\n};\n#line 1 \"/home/shibh308/workspace/compro/competitive-programming/library/lib/classes/avl_map.cpp\"\ntemplate <typename T, typename U>\nstruct AVL_map{\n struct Node{\n int height;\n T key;\n U val;\n Node(T key, U val) : key(key), val(val), height(1){c[0] = 0; c[1] = 0;}\n int c[2];\n };\n vector<Node> nodes;\n stack<int> st;\n AVL_map(){\n nodes.emplace_back(T(), U());\n nodes[0].height = 0;\n }\n template <bool inv>\n int balance_factor(int x){return (nodes[nodes[x].c[0]].height - nodes[nodes[x].c[1]].height) * (inv ? -1 : 1);}\n void _update(int x){\n if(x == 0)\n return;\n nodes[x].height = max(nodes[nodes[x].c[0]].height, nodes[nodes[x].c[1]].height) + 1;\n }\n template <bool is_right>\n int rotate(int x){\n int y = nodes[x].c[1 ^ is_right];\n nodes[x].c[1 ^ is_right] = nodes[y].c[0 ^ is_right];\n nodes[y].c[0 ^ is_right] = x;\n _update(x);\n _update(y);\n return y;\n }\n template <bool inv>\n int _balance(int x){\n if(balance_factor<inv>(x) == 2){\n if(balance_factor<inv>(nodes[x].c[0 ^ inv]) < 0)\n nodes[x].c[0 ^ inv] = rotate<inv>(nodes[x].c[0 ^ inv]);\n x = rotate<1 ^ inv>(x);\n }\n return x;\n }\n int balance(int x){\n x = _balance<false>(x);\n x = _balance<true>(x);\n _update(x);\n return x;\n }\n int add(int x, T key, U val){\n if(x == 0){\n if(st.empty()){\n nodes.emplace_back(key, val);\n return nodes.size() - 1;\n }\n else{\n int y = st.top();\n st.pop();\n nodes[y] = Node(key, val);\n return y;\n }\n }\n else if(key == nodes[x].key)\n nodes[x].val = val;\n else if(key < nodes[x].key)\n nodes[x].c[0] = add(nodes[x].c[0], key, val);\n else\n nodes[x].c[1] = add(nodes[x].c[1], key, val);\n x = balance(x);\n return x;\n }\n // 子が片方しかない時にノードを削除する\n int _erase_top(int x, bool del){\n for(int i = 0; i < 2; ++i){\n if(nodes[x].c[i] == 0){\n int y = nodes[x].c[i ^ 1];\n if(del)\n st.push(x);\n return y;\n }\n }\n return 0;\n }\n // 最小の要素をdstにコピーしてから削除する\n int _copy_erase(int x, int dst, bool del){\n if(nodes[x].c[0] == 0){\n nodes[dst].val = nodes[x].val;\n return _erase_top(x, del);\n }\n nodes[x].c[0] = _copy_erase(nodes[x].c[0], dst, del);\n x = balance(x);\n return x;\n }\n int erase(int x, T key, bool del = true){\n if(key < nodes[x].key)\n nodes[x].c[0] = erase(nodes[x].c[0], key, del);\n else if(nodes[x].key < key)\n nodes[x].c[1] = erase(nodes[x].c[1], key, del);\n else{\n if(nodes[x].c[0] == 0 || nodes[x].c[1] == 0)\n return _erase_top(x, del);\n nodes[x].c[1] = _copy_erase(nodes[x].c[1], x, del);\n }\n x = balance(x);\n return x;\n }\n pair<U, bool> get(int x, T key){\n if(x == 0)\n return {U(), false};\n else if(key == nodes[x].key)\n return {nodes[x].val, true};\n else if(key < nodes[x].key)\n return get(nodes[x].c[0], key);\n else\n return get(nodes[x].c[1], key);\n }\n vector<pair<T, U>> list(int x){\n vector<pair<T, U>> v;\n stack<pair<int,bool>> sta;\n sta.emplace(x, false);\n bool add;\n while(!sta.empty()){\n tie(x, add) = sta.top();\n sta.pop();\n if(x == 0)\n continue;\n if(add)\n v.emplace_back(nodes[x].key, nodes[x].val);\n else{\n if(nodes[x].c[1])\n sta.emplace(nodes[x].c[1], false);\n sta.emplace(x, true);\n if(nodes[x].c[0])\n sta.emplace(nodes[x].c[0], false);\n }\n }\n return v;\n }\n};\n#line 1 \"/home/shibh308/workspace/compro/competitive-programming/library/lib/classes/hashpatriciatree.cpp\"\nstruct HashPatriciaTree{\n struct Node{\n bool is_elm = false;\n int ch = 0;\n int len = 0;\n int par = -1;\n int data_idx = -1;\n Hash hash;\n };\n\n static int fat(int l, int r){\n return r & (-1 << (31 - __builtin_clz(l ^ r)));\n };\n\n AVL_map<int,int> avl;\n vector<function<Hash(int)>> hash_func;\n vector<function<int(int)>> access_func;\n vector<Node> nodes;\n HashMap<uint64_t, int> hmap;\n vector<int> idxes;\n\n HashPatriciaTree() : nodes(1){\n }\n\n void make_fat(int x){\n int st = nodes[nodes[x].par].len;\n int en = nodes[x].len;\n int fa = fat(st, en);\n auto ha = hash_func[nodes[x].data_idx](fa);\n nodes[x].hash = ha;\n hmap.add(ha.concat(ha.l).h, x);\n }\n\n void make(int idx, int target, int len, int label){\n nodes.emplace_back();\n nodes.back().len = len;\n nodes.back().data_idx = idx;\n nodes.back().par = target;\n nodes[target].ch = avl.add(nodes[target].ch, label, nodes.size() - 1);\n make_fat(nodes.size() - 1);\n }\n\n void split(int idx, int target, int len, int match_len, int new_label, int root_label, int branch_label){\n int par = nodes[target].par;\n nodes.emplace_back();\n nodes.back().len = match_len;\n nodes.back().data_idx = idx;\n nodes.back().par = par;\n\n Hash ha = nodes[target].hash;\n hmap.erase(ha.concat(ha.l).h);\n\n nodes[par].ch = avl.add(nodes[par].ch, root_label, nodes.size() - 1);\n nodes.back().ch = avl.add(nodes.back().ch, branch_label, target);\n nodes[target].par = nodes.size() - 1;\n int target_siz = nodes.size() - 1;\n if(nodes.back().len != len)\n make(idx, nodes.size() - 1, len, new_label);\n make_fat(target);\n make_fat(target_siz);\n }\n\n // f(x): 長さxのprefixのハッシュを返す\n // g(x): x文字目を返す\n void insert(int m, function<Hash(int)> f, function<int(int)> g){\n hash_func.emplace_back(f);\n access_func.emplace_back(g);\n int len, x;\n tie(len, x) = match(m, f, g);\n assert(!(len || x) || (nodes[nodes[x].par].len < len && len <= nodes[x].len));\n if(len == nodes[x].len){\n if(len == m){\n idxes.emplace_back(x);\n nodes[x].data_idx = idxes.size() - 1;\n nodes[x].is_elm = true;\n return;\n }\n else\n make(idxes.size(), x, m, g(len));\n }\n else{\n split(idxes.size(), x, m, len, g(len), access_func[nodes[x].data_idx](nodes[nodes[x].par].len), access_func[nodes[x].data_idx](len));\n }\n idxes.emplace_back(nodes.size() - 1);\n nodes.back().is_elm = true;\n }\n\n int lcp(int ok, int ng, function<Hash(int)> f, function<Hash(int)> g){\n while(ng - ok > 1){\n int mid = (ok + ng) >> 1;\n (f(mid) == g(mid) ? ok : ng) = mid;\n }\n return ok;\n }\n\n // (マッチ長, ノード)\n pair<int,int> match(int m, function<Hash(int)> f, function<int(int)> g){\n int ok = 0, ng = m + 1;\n int x = 0;\n int ok_len = 0;\n while(ng - ok > 1){\n int mid = fat(ok + 1, ng - 1);\n Hash h = f(mid);\n int idx;\n bool fl;\n tie(idx, fl) = hmap.find(h.concat(h.l).h);\n if(fl){\n x = idx;\n ok_len = ok;\n ok = nodes[x].len;\n }\n else{\n ng = mid;\n }\n }\n int match_len;\n if(ok == 0){\n match_len = 0;\n }\n else{\n match_len = lcp(ok_len, min(m, nodes[x].len) + 1, f, hash_func[nodes[x].data_idx]);\n if(match_len < nodes[x].len){\n return {match_len, x};\n }\n }\n int label = g(ok);\n int ch;\n bool fl;\n tie(ch, fl) = avl.get(nodes[x].ch, label);\n if(!fl){\n assert(match_len <= m);\n return {match_len, x};\n }\n match_len = lcp(match_len, min(m, nodes[ch].len) + 1, f, hash_func[nodes[ch].data_idx]);\n assert(match_len <= m);\n return {match_len, ch};\n }\n template<typename T>\n static pair<function<Hash(int)>, function<int(int)>> build(T& s, vector<Hash>& v){\n auto f = [&v](int i){\n return v[i];\n };\n auto g = [&s](int i){\n return s[i];\n };\n return {f, g};\n }\n template<typename T>\n static pair<vector<function<Hash(int)>>, vector<function<int(int)>>> build_suffix(T& s, vector<Hash>& v){\n vector<function<Hash(int)>> fv;\n vector<function<int(int)>> gv;\n for(int j = 0; j < s.size(); ++j){\n auto f = [j, &v](int i){\n return v[i + j].sub(v[j]);\n };\n auto g = [j, &s](int i){\n return s[i + j];\n };\n fv.emplace_back(f);\n gv.emplace_back(g);\n }\n return {fv, gv};\n }\n};\n#line 1 \"/home/shibh308/workspace/compro/competitive-programming/library/lib/classes/bitvector.cpp\"\nstruct BitVector{\n vector<uint64_t> v;\n vector<int> r;\n BitVector(){}\n void build(){\n r.assign(v.size() + 1, 0);\n for(int i = 0; i < v.size(); ++i)\n r[i + 1] = r[i] + __builtin_popcountll(v[i]);\n }\n bool access(int x){\n return (v[x >> 6] >> (x & 63)) & 1;\n }\n // [0, x)の1の出現回数\n int rank(int x){\n return r[x >> 6] + __builtin_popcountll(v[x >> 6] & ((1uLL << (x & 63)) - 1));\n }\n int rank(int x, bool fl){\n return fl ? rank(x) : x - rank(x);\n }\n};\n\n#line 1 \"/home/shibh308/workspace/compro/competitive-programming/library/lib/classes/waveletmatrix.cpp\"\ntemplate <typename T, int W>\nstruct WaveletMatrix{\n\n array<BitVector, W> bv;\n array<int, W> zero_cnt;\n\n WaveletMatrix(vector<T>& a){\n int n = a.size();\n vector<T> v(a);\n for(int i = W - 1; i >= 0; --i){\n vector<uint64_t> b((n >> 6) + 1, 0);\n vector<T> v1, v2;\n for(int j = 0; j < n; ++j){\n ((v[j] >> i) & 1 ? v2 : v1).push_back(v[j]);\n b[j >> 6] |= uint64_t((v[j] >> i) & 1) << (j & 63);\n }\n for(int j = 0; j < v.size(); ++j)\n v[j] = (j < v1.size() ? v1[j] : v2[j - v1.size()]);\n bv[i].v = move(b);\n bv[i].build();\n zero_cnt[i] = bv[i].rank(n, 0);\n }\n }\n\n // [l, r)内のxの数\n int count(int l, int r, T x){\n for(int i = W - 1; i >= 0; --i){\n bool fl = (x >> i) & 1;\n int st = bv[i].rank(l, fl);\n int en = bv[i].rank(r, fl);\n l = (fl ? zero_cnt[i] : 0) + st;\n r = (fl ? zero_cnt[i] : 0) + en;\n }\n return r - l;\n }\n\n // [l, r)内で[0, x)を満たす値の数\n int count_lower(int l, int r, T x){\n int cnt = 0;\n for(int i = W - 1; i >= 0; --i){\n bool fl = (x >> i) & 1;\n int st = bv[i].rank(l, fl);\n int en = bv[i].rank(r, fl);\n if(fl){\n st += zero_cnt[i];\n en += zero_cnt[i];\n cnt += (bv[i].rank(r, 0) - bv[i].rank(l, 0));\n }\n l = st, r = en;\n }\n return cnt;\n }\n\n // [l, r)内で[x, y)を満たす値の数\n int count_range(int l, int r, T x, T y){\n return count_lower(l, r, y) - count_lower(l, r, x);\n }\n\n // 0-indexedでk番目に小さいものを返す\n T kth_min(int l, int r, int k){\n T ans = 0;\n for(int i = W - 1; i >= 0; --i){\n int st = bv[i].rank(l, 0);\n int en = bv[i].rank(r, 0);\n if(en - st <= k){\n k -= en - st;\n l = zero_cnt[i] + bv[i].rank(l, 1);\n r = zero_cnt[i] + bv[i].rank(r, 1);\n ans |= (1uLL << i);\n }\n else{\n l = st, r = en;\n }\n }\n return ans;\n }\n\n // [l, r)でのx以上最小値\n pair<T, bool> predecessor(int l, int r, T x){\n int idx = count_lower(l, r, x);\n if(idx == r - l){\n return make_pair((1uLL << W) - 1, false);\n }\n return make_pair(kth_min(l, r, idx), true);\n }\n\n // [l, r)でのx以下最大値\n pair<T, bool> successor(int l, int r, T x){\n int idx = count_lower(l, r, x + 1);\n if(idx == 0)\n return make_pair(0, false);\n return make_pair(kth_min(l, r, idx - 1), true);\n }\n};\n\n#line 15 \"aoj_1585.test.cpp\"\n\n\nsigned main(){\n HashPatriciaTree tree;\n string s;\n int q;\n cin >> s >> q;\n\n int n = s.size();\n vector<Hash> sh = Hash::make(s);\n auto s_res = HashPatriciaTree::build_suffix(s, sh);\n for(int i = 0; i < n; ++i){\n tree.insert(n - i, s_res.first[i], s_res.second[i]);\n }\n\n vector<int> v;\n int k = tree.nodes.size();\n vector<int> in(k), out(k);\n function<void(int)> f = [&](int x){\n in[x] = v.size();\n v.emplace_back(tree.nodes[x].is_elm ? tree.nodes[x].data_idx : n);\n for(auto p : tree.avl.list(tree.nodes[x].ch)){\n f(p.second);\n }\n out[x] = v.size();\n };\n f(0);\n\n WaveletMatrix<int, 20> wm(v);\n\n for(int i = 0; i < q; ++i){\n int l, r;\n string t;\n cin >> l >> r >> t;\n ++r;\n auto h = Hash::make(t);\n auto res = HashPatriciaTree::build(t, h);\n int len, node;\n tie(len, node) = tree.match(t.size(), res.first, res.second);\n if(len < t.size()){\n cout << 0 << endl;\n continue;\n }\n int in_x = in[node];\n int out_x = out[node];\n\n int left = l;\n int right = max(left, min(int(v.size()), r + 1 - int(t.size())));\n if(right <= left)\n cout << 0 << endl;\n else\n cout << wm.count_range(in_x, out_x, left, right) << endl;\n }\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 65444, "score_of_the_acc": -0.3306, "final_rank": 6 }, { "submission_id": "aoj_1585_5001239", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing i64 = int64_t;\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/rollinghashmonoid.cpp.html\nstruct RollingHashMonoid{\n using Hash = RollingHashMonoid;\n using u64 = uint64_t;\n static constexpr u64 mod = (1uLL << 61) - 1;\n static constexpr u64 m30 = (1uLL << 30) - 1;\n static constexpr u64 m31 = (1uLL << 31) - 1;\n static constexpr u64 m61 = mod;\n static constexpr u64 _base = 550390130435464343;\n static constexpr u64 _base_inv = 1803816245541264939;\n static vector<u64> base, base_inv;\n\n static inline u64 calc_mod(u64 x){\n u64 xu = x >> 61;\n u64 xd = x & m61;\n u64 res = xu + xd;\n if(res >= mod)\n res -= mod;\n return res;\n }\n\n static inline u64 raw_mul(u64 a, u64 b){\n u64 au = a >> 31;\n u64 ad = a & m31;\n u64 bu = b >> 31;\n u64 bd = b & m31;\n u64 mid = ad * bu + au * bd;\n u64 midu = mid >> 30;\n u64 midd = mid & m30;\n return au * bu * 2 + midu + (midd << 31) + ad * bd;\n }\n\n static void remake_table(int len){\n while(base.size() <= len){\n base.emplace_back(calc_mod(raw_mul(base.back(), _base)));\n base_inv.emplace_back(calc_mod(raw_mul(base_inv.back(), _base_inv)));\n }\n }\n\n u64 h, l;\n\n static inline u64 lshift(u64 a, int len){\n remake_table(len);\n return raw_mul(a, base[len]);\n }\n static inline u64 rshift(u64 a, int len){\n remake_table(len);\n return raw_mul(a, base_inv[len]);\n }\n\n Hash concat(const Hash& b) const{\n return RollingHashMonoid(\n calc_mod(h + lshift(b.h, l)),\n l + b.l\n );\n }\n Hash sub(const Hash& b) const{\n return RollingHashMonoid(\n calc_mod(rshift(h + mod - b.h, b.l)),\n l - b.l\n );\n }\n\n RollingHashMonoid() : h(0), l(0){}\n RollingHashMonoid(u64 x) : h(x), l(1){}\n RollingHashMonoid(u64 h, u64 l) : h(h), l(l){}\n\n friend bool operator==(const Hash& a, const Hash& b){return a.h == b.h && a.l == b.l;}\n\n template <typename T>\n static vector<Hash> make(vector<T>& x){\n vector<Hash> v(x.size() + 1);\n for(int i = 0; i < x.size(); ++i){\n v[i + 1] = v[i].concat(x[i]);\n }\n return v;\n }\n static vector<Hash> make(string& x){\n vector<Hash> v(x.size() + 1);\n for(int i = 0; i < x.size(); ++i){\n v[i + 1] = v[i].concat(x[i]);\n }\n return v;\n }\n};\n\nnamespace std{\ntemplate<> struct hash<RollingHashMonoid>{\n size_t operator()(const RollingHashMonoid x) const noexcept{\n return x.concat(x.l).h;\n }\n};\n}\n\n\nusing Hash = RollingHashMonoid;\nvector<uint64_t> Hash::base = {1};\nvector<uint64_t> Hash::base_inv = {1};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/hashmap.cpp.html\ntemplate <typename T, typename U, T del = numeric_limits<T>::max(), T null = numeric_limits<T>::max() - 1>\nstruct HashMap{\n static constexpr __int128_t z = 0xf332ac987401cba5;\n uint64_t n, q, d;\n\n vector<pair<T, U>> v;\n\n HashMap() : n(0), q(0), d(1), v(2, make_pair(null, U())){\n }\n\n inline uint64_t hash(T key){return uint64_t((z * __int128_t(key)) >> (64 - d)) & ((1LL << d) - 1);}\n\n pair<U, bool> find(T x){\n for(uint64_t i = hash(x); v[i].first != null; i = (i + 1) & ((1 << d) - 1))\n if(v[i].first == x)\n return make_pair(v[i].second, true);\n return make_pair(U(), false);\n }\n\n bool add(T x, U val){\n if(find(x).second)\n return false;\n if(((q + 1) << 1) > (1 << d) || (1 << d) < 3 * n)\n resize();\n uint64_t i = hash(x);\n for(; v[i].first != null && v[i].first != del; i = (i + 1) & ((1 << d) - 1));\n q += (v[i].first == null);\n ++n;\n v[i] = make_pair(x, val);\n return true;\n }\n\n bool erase(T x){\n uint64_t i = hash(x);\n for(; v[i].first != null && v[i].first != x; i = (i + 1) & ((1 << d) - 1));\n if(v[i].first == null)\n return false;\n --n;\n v[i] = make_pair(del, U());\n return true;\n }\n\n void resize(){\n ++d;\n vector<pair<T, U>> old_table;\n q = n;\n swap(old_table, v);\n v.assign(1 << d, make_pair(null, U()));\n n = 0;\n for(int i = 0; i < old_table.size(); ++i)\n if(old_table[i].first != null && old_table[i].first != del)\n add(old_table[i].first, old_table[i].second);\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/avl_map.cpp.html\ntemplate <typename T, typename U>\nstruct AVL_map{\n struct Node{\n int height;\n T key;\n U val;\n Node(T key, U val) : key(key), val(val), height(1){c[0] = 0; c[1] = 0;}\n int c[2];\n };\n vector<Node> nodes;\n stack<int> st;\n AVL_map(){\n nodes.emplace_back(T(), U());\n nodes[0].height = 0;\n }\n template <bool inv>\n int balance_factor(int x){return (nodes[nodes[x].c[0]].height - nodes[nodes[x].c[1]].height) * (inv ? -1 : 1);}\n void _update(int x){\n if(x == 0)\n return;\n nodes[x].height = max(nodes[nodes[x].c[0]].height, nodes[nodes[x].c[1]].height) + 1;\n }\n template <bool is_right>\n int rotate(int x){\n int y = nodes[x].c[1 ^ is_right];\n nodes[x].c[1 ^ is_right] = nodes[y].c[0 ^ is_right];\n nodes[y].c[0 ^ is_right] = x;\n _update(x);\n _update(y);\n return y;\n }\n template <bool inv>\n int _balance(int x){\n if(balance_factor<inv>(x) == 2){\n if(balance_factor<inv>(nodes[x].c[0 ^ inv]) < 0)\n nodes[x].c[0 ^ inv] = rotate<inv>(nodes[x].c[0 ^ inv]);\n x = rotate<1 ^ inv>(x);\n }\n return x;\n }\n int balance(int x){\n x = _balance<false>(x);\n x = _balance<true>(x);\n _update(x);\n return x;\n }\n int add(int x, T key, U val){\n if(x == 0){\n if(st.empty()){\n nodes.emplace_back(key, val);\n return nodes.size() - 1;\n }\n else{\n int y = st.top();\n st.pop();\n nodes[y] = Node(key, val);\n return y;\n }\n }\n else if(key == nodes[x].key)\n nodes[x].val = val;\n else if(key < nodes[x].key)\n nodes[x].c[0] = add(nodes[x].c[0], key, val);\n else\n nodes[x].c[1] = add(nodes[x].c[1], key, val);\n x = balance(x);\n return x;\n }\n // 子が片方しかない時にノードを削除する\n int _erase_top(int x, bool del){\n for(int i = 0; i < 2; ++i){\n if(nodes[x].c[i] == 0){\n int y = nodes[x].c[i ^ 1];\n if(del)\n st.push(x);\n return y;\n }\n }\n return 0;\n }\n // 最小の要素をdstにコピーしてから削除する\n int _copy_erase(int x, int dst, bool del){\n if(nodes[x].c[0] == 0){\n nodes[dst].val = nodes[x].val;\n return _erase_top(x, del);\n }\n nodes[x].c[0] = _copy_erase(nodes[x].c[0], dst, del);\n x = balance(x);\n return x;\n }\n int erase(int x, T key, bool del = true){\n if(key < nodes[x].key)\n nodes[x].c[0] = erase(nodes[x].c[0], key, del);\n else if(nodes[x].key < key)\n nodes[x].c[1] = erase(nodes[x].c[1], key, del);\n else{\n if(nodes[x].c[0] == 0 || nodes[x].c[1] == 0)\n return _erase_top(x, del);\n nodes[x].c[1] = _copy_erase(nodes[x].c[1], x, del);\n }\n x = balance(x);\n return x;\n }\n pair<U, bool> get(int x, T key){\n if(x == 0)\n return {U(), false};\n else if(key == nodes[x].key)\n return {nodes[x].val, true};\n else if(key < nodes[x].key)\n return get(nodes[x].c[0], key);\n else\n return get(nodes[x].c[1], key);\n }\n vector<pair<T, U>> list(int x){\n vector<pair<T, U>> v;\n stack<pair<int,bool>> sta;\n sta.emplace(x, false);\n bool add;\n while(!sta.empty()){\n tie(x, add) = sta.top();\n sta.pop();\n if(x == 0)\n continue;\n if(add)\n v.emplace_back(nodes[x].key, nodes[x].val);\n else{\n if(nodes[x].c[1])\n sta.emplace(nodes[x].c[1], false);\n sta.emplace(x, true);\n if(nodes[x].c[0])\n sta.emplace(nodes[x].c[0], false);\n }\n }\n return v;\n }\n};\n\n\nstruct HashPatriciaTree{\n struct Node{\n bool is_elm = false;\n int ch = 0;\n int len = 0;\n int par = -1;\n int data_idx = -1;\n Hash hash;\n };\n\n static int fat(int l, int r){\n return r & (-1 << (31 - __builtin_clz(l ^ r)));\n };\n\n AVL_map<int,int> avl;\n vector<function<Hash(int)>> hash_func;\n vector<function<int(int)>> access_func;\n vector<Node> nodes;\n HashMap<uint64_t, int> hmap;\n vector<int> idxes;\n\n HashPatriciaTree() : nodes(1){\n }\n\n void make_fat(int x){\n int st = nodes[nodes[x].par].len;\n int en = nodes[x].len;\n int fa = fat(st, en);\n auto ha = hash_func[nodes[x].data_idx](fa);\n nodes[x].hash = ha;\n hmap.add(ha.concat(ha.l).h, x);\n }\n\n void make(int idx, int target, int len, int label){\n nodes.emplace_back();\n nodes.back().len = len;\n nodes.back().data_idx = idx;\n nodes.back().par = target;\n nodes[target].ch = avl.add(nodes[target].ch, label, nodes.size() - 1);\n make_fat(nodes.size() - 1);\n }\n\n void split(int idx, int target, int len, int match_len, int new_label, int root_label, int branch_label){\n int par = nodes[target].par;\n nodes.emplace_back();\n nodes.back().len = match_len;\n nodes.back().data_idx = idx;\n nodes.back().par = par;\n\n Hash ha = nodes[target].hash;\n hmap.erase(ha.concat(ha.l).h);\n\n nodes[par].ch = avl.add(nodes[par].ch, root_label, nodes.size() - 1);\n nodes.back().ch = avl.add(nodes.back().ch, branch_label, target);\n nodes[target].par = nodes.size() - 1;\n int target_siz = nodes.size() - 1;\n if(nodes.back().len != len)\n make(idx, nodes.size() - 1, len, new_label);\n make_fat(target);\n make_fat(target_siz);\n }\n\n // f(x): 長さxのprefixのハッシュを返す\n // g(x): x文字目を返す\n void insert(int m, function<Hash(int)> f, function<int(int)> g){\n hash_func.emplace_back(f);\n access_func.emplace_back(g);\n int len, x;\n tie(len, x) = match(m, f, g);\n assert(!(len || x) || (nodes[nodes[x].par].len < len && len <= nodes[x].len));\n if(len == nodes[x].len){\n if(len == m){\n idxes.emplace_back(x);\n nodes[x].data_idx = idxes.size() - 1;\n nodes[x].is_elm = true;\n return;\n }\n else\n make(idxes.size(), x, m, g(len));\n }\n else{\n split(idxes.size(), x, m, len, g(len), access_func[nodes[x].data_idx](nodes[nodes[x].par].len), access_func[nodes[x].data_idx](len));\n }\n idxes.emplace_back(nodes.size() - 1);\n nodes.back().is_elm = true;\n }\n\n int lcp(int ok, int ng, function<Hash(int)> f, function<Hash(int)> g){\n while(ng - ok > 1){\n int mid = (ok + ng) >> 1;\n (f(mid) == g(mid) ? ok : ng) = mid;\n }\n return ok;\n }\n\n // (マッチ長, ノード)\n pair<int,int> match(int m, function<Hash(int)> f, function<int(int)> g){\n int ok = 0, ng = m + 1;\n int x = 0;\n int ok_len = 0;\n while(ng - ok > 1){\n int mid = fat(ok + 1, ng - 1);\n Hash h = f(mid);\n int idx;\n bool fl;\n tie(idx, fl) = hmap.find(h.concat(h.l).h);\n if(fl){\n x = idx;\n ok_len = ok;\n ok = nodes[x].len;\n }\n else{\n ng = mid;\n }\n }\n int match_len;\n if(ok == 0){\n match_len = 0;\n }\n else{\n match_len = lcp(ok_len, min(m, nodes[x].len) + 1, f, hash_func[nodes[x].data_idx]);\n if(match_len < nodes[x].len){\n return {match_len, x};\n }\n }\n int label = g(ok);\n int ch;\n bool fl;\n tie(ch, fl) = avl.get(nodes[x].ch, label);\n if(!fl){\n assert(match_len <= m);\n return {match_len, x};\n }\n match_len = lcp(match_len, min(m, nodes[ch].len) + 1, f, hash_func[nodes[ch].data_idx]);\n assert(match_len <= m);\n return {match_len, ch};\n }\n template<typename T>\n static pair<function<Hash(int)>, function<int(int)>> build(T& s, vector<Hash>& v){\n auto f = [&v](int i){\n return v[i];\n };\n auto g = [&s](int i){\n return s[i];\n };\n return {f, g};\n }\n template<typename T>\n static pair<vector<function<Hash(int)>>, vector<function<int(int)>>> build_suffix(T& s, vector<Hash>& v){\n vector<function<Hash(int)>> fv;\n vector<function<int(int)>> gv;\n for(int j = 0; j < s.size(); ++j){\n auto f = [j, &v](int i){\n return v[i + j].sub(v[j]);\n };\n auto g = [j, &s](int i){\n return s[i + j];\n };\n fv.emplace_back(f);\n gv.emplace_back(g);\n }\n return {fv, gv};\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/bitvector.cpp.html\nstruct BitVector{\n vector<uint64_t> v;\n vector<int> r;\n BitVector(){}\n void build(){\n r.assign(v.size() + 1, 0);\n for(int i = 0; i < v.size(); ++i)\n r[i + 1] = r[i] + __builtin_popcountll(v[i]);\n }\n bool access(int x){\n return (v[x >> 6] >> (x & 63)) & 1;\n }\n // [0, x)の1の出現回数\n int rank(int x){\n return r[x >> 6] + __builtin_popcountll(v[x >> 6] & ((1uLL << (x & 63)) - 1));\n }\n int rank(int x, bool fl){\n return fl ? rank(x) : x - rank(x);\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/waveletmatrix.cpp.html\ntemplate <typename T, int W>\nstruct WaveletMatrix{\n\n array<BitVector, W> bv;\n array<int, W> zero_cnt;\n\n WaveletMatrix(vector<T>& a){\n int n = a.size();\n vector<T> v(a);\n for(int i = W - 1; i >= 0; --i){\n vector<uint64_t> b((n >> 6) + 1, 0);\n vector<T> v1, v2;\n for(int j = 0; j < n; ++j){\n ((v[j] >> i) & 1 ? v2 : v1).push_back(v[j]);\n b[j >> 6] |= uint64_t((v[j] >> i) & 1) << (j & 63);\n }\n for(int j = 0; j < v.size(); ++j)\n v[j] = (j < v1.size() ? v1[j] : v2[j - v1.size()]);\n bv[i].v = move(b);\n bv[i].build();\n zero_cnt[i] = bv[i].rank(n, 0);\n }\n }\n\n // [l, r)内のxの数\n int count(int l, int r, T x){\n for(int i = W - 1; i >= 0; --i){\n bool fl = (x >> i) & 1;\n int st = bv[i].rank(l, fl);\n int en = bv[i].rank(r, fl);\n l = (fl ? zero_cnt[i] : 0) + st;\n r = (fl ? zero_cnt[i] : 0) + en;\n }\n return r - l;\n }\n\n // [l, r)内で[0, x)を満たす値の数\n int count_lower(int l, int r, T x){\n int cnt = 0;\n for(int i = W - 1; i >= 0; --i){\n bool fl = (x >> i) & 1;\n int st = bv[i].rank(l, fl);\n int en = bv[i].rank(r, fl);\n if(fl){\n st += zero_cnt[i];\n en += zero_cnt[i];\n cnt += (bv[i].rank(r, 0) - bv[i].rank(l, 0));\n }\n l = st, r = en;\n }\n return cnt;\n }\n\n // [l, r)内で[x, y)を満たす値の数\n int count_range(int l, int r, T x, T y){\n return count_lower(l, r, y) - count_lower(l, r, x);\n }\n\n // 0-indexedでk番目に小さいものを返す\n T kth_min(int l, int r, int k){\n T ans = 0;\n for(int i = W - 1; i >= 0; --i){\n int st = bv[i].rank(l, 0);\n int en = bv[i].rank(r, 0);\n if(en - st <= k){\n k -= en - st;\n l = zero_cnt[i] + bv[i].rank(l, 1);\n r = zero_cnt[i] + bv[i].rank(r, 1);\n ans |= (1uLL << i);\n }\n else{\n l = st, r = en;\n }\n }\n return ans;\n }\n\n // [l, r)でのx以上最小値\n pair<T, bool> predecessor(int l, int r, T x){\n int idx = count_lower(l, r, x);\n if(idx == r - l){\n return make_pair((1uLL << W) - 1, false);\n }\n return make_pair(kth_min(l, r, idx), true);\n }\n\n // [l, r)でのx以下最大値\n pair<T, bool> successor(int l, int r, T x){\n int idx = count_lower(l, r, x + 1);\n if(idx == 0)\n return make_pair(0, false);\n return make_pair(kth_min(l, r, idx - 1), true);\n }\n};\n\n\nsigned main(){\n HashPatriciaTree tree;\n string s;\n int q;\n cin >> s >> q;\n\n int n = s.size();\n vector<Hash> sh = Hash::make(s);\n auto s_res = HashPatriciaTree::build_suffix(s, sh);\n for(int i = 0; i < n; ++i){\n tree.insert(n - i, s_res.first[i], s_res.second[i]);\n }\n\n vector<int> v;\n int k = tree.nodes.size();\n vector<int> in(k), out(k);\n function<void(int)> f = [&](int x){\n in[x] = v.size();\n v.emplace_back(tree.nodes[x].is_elm ? tree.nodes[x].data_idx : n);\n for(auto p : tree.avl.list(tree.nodes[x].ch)){\n f(p.second);\n }\n out[x] = v.size();\n };\n f(0);\n\n WaveletMatrix<int, 20> wm(v);\n\n for(int i = 0; i < q; ++i){\n int l, r;\n string t;\n cin >> l >> r >> t;\n ++r;\n auto h = Hash::make(t);\n auto res = HashPatriciaTree::build(t, h);\n int len, node;\n tie(len, node) = tree.match(t.size(), res.first, res.second);\n if(len < t.size()){\n cout << 0 << endl;\n continue;\n }\n int in_x = in[node];\n int out_x = out[node];\n\n int left = l;\n int right = max(left, min(int(v.size()), r + 1 - int(t.size())));\n if(right <= left)\n cout << 0 << endl;\n else\n cout << wm.count_range(in_x, out_x, left, right) << endl;\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 64384, "score_of_the_acc": -0.334, "final_rank": 8 }, { "submission_id": "aoj_1585_5001170", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing i64 = int64_t;\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/rollinghashmonoid.cpp.html\nstruct RollingHashMonoid{\n using Hash = RollingHashMonoid;\n using u64 = uint64_t;\n static constexpr u64 mod = (1uLL << 61) - 1;\n static constexpr u64 m30 = (1uLL << 30) - 1;\n static constexpr u64 m31 = (1uLL << 31) - 1;\n static constexpr u64 m61 = mod;\n static constexpr u64 _base = 550390130435464343;\n static constexpr u64 _base_inv = 1803816245541264939;\n static vector<u64> base, base_inv;\n\n static inline u64 calc_mod(u64 x){\n u64 xu = x >> 61;\n u64 xd = x & m61;\n u64 res = xu + xd;\n if(res >= mod)\n res -= mod;\n return res;\n }\n\n static inline u64 raw_mul(u64 a, u64 b){\n u64 au = a >> 31;\n u64 ad = a & m31;\n u64 bu = b >> 31;\n u64 bd = b & m31;\n u64 mid = ad * bu + au * bd;\n u64 midu = mid >> 30;\n u64 midd = mid & m30;\n return au * bu * 2 + midu + (midd << 31) + ad * bd;\n }\n\n static void remake_table(int len){\n while(base.size() <= len){\n base.emplace_back(calc_mod(raw_mul(base.back(), _base)));\n base_inv.emplace_back(calc_mod(raw_mul(base_inv.back(), _base_inv)));\n }\n }\n\n u64 h, l;\n\n static inline u64 lshift(u64 a, int len){\n remake_table(len);\n return raw_mul(a, base[len]);\n }\n static inline u64 rshift(u64 a, int len){\n remake_table(len);\n return raw_mul(a, base_inv[len]);\n }\n\n Hash concat(const Hash& b) const{\n return RollingHashMonoid(\n calc_mod(h + lshift(b.h, l)),\n l + b.l\n );\n }\n Hash sub(const Hash& b) const{\n return RollingHashMonoid(\n calc_mod(rshift(h + mod - b.h, b.l)),\n l - b.l\n );\n }\n\n RollingHashMonoid() : h(0), l(0){}\n RollingHashMonoid(u64 x) : h(x), l(1){}\n RollingHashMonoid(u64 h, u64 l) : h(h), l(l){}\n\n friend bool operator==(const Hash& a, const Hash& b){return a.h == b.h && a.l == b.l;}\n\n template <typename T>\n static vector<Hash> make(vector<T>& x){\n vector<Hash> v(x.size() + 1);\n for(int i = 0; i < x.size(); ++i){\n v[i + 1] = v[i].concat(x[i]);\n }\n return v;\n }\n static vector<Hash> make(string& x){\n vector<Hash> v(x.size() + 1);\n for(int i = 0; i < x.size(); ++i){\n v[i + 1] = v[i].concat(x[i]);\n }\n return v;\n }\n};\n\nnamespace std{\ntemplate<> struct hash<RollingHashMonoid>{\n size_t operator()(const RollingHashMonoid x) const noexcept{\n return x.concat(x.l).h;\n }\n};\n}\n\n\nusing Hash = RollingHashMonoid;\nvector<uint64_t> Hash::base = {1};\nvector<uint64_t> Hash::base_inv = {1};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/hashmap.cpp.html\ntemplate <typename T, typename U, T del = numeric_limits<T>::max(), T null = numeric_limits<T>::max() - 1>\nstruct HashMap{\n static constexpr __int128_t z = 0xf332ac987401cba5;\n uint64_t n, q, d;\n\n vector<pair<T, U>> v;\n\n HashMap() : n(0), q(0), d(1), v(2, make_pair(null, U())){\n }\n\n inline uint64_t hash(T key){return uint64_t((z * __int128_t(key)) >> (64 - d)) & ((1LL << d) - 1);}\n\n pair<U, bool> find(T x){\n for(uint64_t i = hash(x); v[i].first != null; i = (i + 1) & ((1 << d) - 1))\n if(v[i].first == x)\n return make_pair(v[i].second, true);\n return make_pair(U(), false);\n }\n\n bool add(T x, U val){\n if(find(x).second)\n return false;\n if(((q + 1) << 1) > (1 << d) || (1 << d) < 3 * n)\n resize();\n uint64_t i = hash(x);\n for(; v[i].first != null && v[i].first != del; i = (i + 1) & ((1 << d) - 1));\n q += (v[i].first == null);\n ++n;\n v[i] = make_pair(x, val);\n return true;\n }\n\n bool erase(T x){\n uint64_t i = hash(x);\n for(; v[i].first != null && v[i].first != x; i = (i + 1) & ((1 << d) - 1));\n if(v[i].first == null)\n return false;\n --n;\n v[i] = make_pair(del, U());\n return true;\n }\n\n void resize(){\n ++d;\n vector<pair<T, U>> old_table;\n q = n;\n swap(old_table, v);\n v.assign(1 << d, make_pair(null, U()));\n n = 0;\n for(int i = 0; i < old_table.size(); ++i)\n if(old_table[i].first != null && old_table[i].first != del)\n add(old_table[i].first, old_table[i].second);\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/avl_map.cpp.html\ntemplate <typename T, typename U>\nstruct AVL_map{\n struct Node{\n int height;\n T key;\n U val;\n Node(T key, U val) : key(key), val(val), height(1){c[0] = 0; c[1] = 0;}\n int c[2];\n };\n vector<Node> nodes;\n stack<int> st;\n AVL_map(){\n nodes.emplace_back(T(), U());\n nodes[0].height = 0;\n }\n template <bool inv>\n int balance_factor(int x){return (nodes[nodes[x].c[0]].height - nodes[nodes[x].c[1]].height) * (inv ? -1 : 1);}\n void _update(int x){\n if(x == 0)\n return;\n nodes[x].height = max(nodes[nodes[x].c[0]].height, nodes[nodes[x].c[1]].height) + 1;\n }\n template <bool is_right>\n int rotate(int x){\n int y = nodes[x].c[1 ^ is_right];\n nodes[x].c[1 ^ is_right] = nodes[y].c[0 ^ is_right];\n nodes[y].c[0 ^ is_right] = x;\n _update(x);\n _update(y);\n return y;\n }\n template <bool inv>\n int _balance(int x){\n if(balance_factor<inv>(x) == 2){\n if(balance_factor<inv>(nodes[x].c[0 ^ inv]) < 0)\n nodes[x].c[0 ^ inv] = rotate<inv>(nodes[x].c[0 ^ inv]);\n x = rotate<1 ^ inv>(x);\n }\n return x;\n }\n int balance(int x){\n x = _balance<false>(x);\n x = _balance<true>(x);\n _update(x);\n return x;\n }\n int add(int x, T key, U val){\n if(x == 0){\n if(st.empty()){\n nodes.emplace_back(key, val);\n return nodes.size() - 1;\n }\n else{\n int y = st.top();\n st.pop();\n nodes[y] = Node(key, val);\n return y;\n }\n }\n else if(key == nodes[x].key)\n nodes[x].val = val;\n else if(key < nodes[x].key)\n nodes[x].c[0] = add(nodes[x].c[0], key, val);\n else\n nodes[x].c[1] = add(nodes[x].c[1], key, val);\n x = balance(x);\n return x;\n }\n // 子が片方しかない時にノードを削除する\n int _erase_top(int x, bool del){\n for(int i = 0; i < 2; ++i){\n if(nodes[x].c[i] == 0){\n int y = nodes[x].c[i ^ 1];\n if(del)\n st.push(x);\n return y;\n }\n }\n return 0;\n }\n // 最小の要素をdstにコピーしてから削除する\n int _copy_erase(int x, int dst, bool del){\n if(nodes[x].c[0] == 0){\n nodes[dst].val = nodes[x].val;\n return _erase_top(x, del);\n }\n nodes[x].c[0] = _copy_erase(nodes[x].c[0], dst, del);\n x = balance(x);\n return x;\n }\n int erase(int x, T key, bool del = true){\n if(key < nodes[x].key)\n nodes[x].c[0] = erase(nodes[x].c[0], key, del);\n else if(nodes[x].key < key)\n nodes[x].c[1] = erase(nodes[x].c[1], key, del);\n else{\n if(nodes[x].c[0] == 0 || nodes[x].c[1] == 0)\n return _erase_top(x, del);\n nodes[x].c[1] = _copy_erase(nodes[x].c[1], x, del);\n }\n x = balance(x);\n return x;\n }\n pair<U, bool> get(int x, T key){\n if(x == 0)\n return {U(), false};\n else if(key == nodes[x].key)\n return {nodes[x].val, true};\n else if(key < nodes[x].key)\n return get(nodes[x].c[0], key);\n else\n return get(nodes[x].c[1], key);\n }\n vector<pair<T, U>> list(int x){\n vector<pair<T, U>> v;\n stack<pair<int,bool>> sta;\n sta.emplace(x, false);\n bool add;\n while(!sta.empty()){\n tie(x, add) = sta.top();\n sta.pop();\n if(x == 0)\n continue;\n if(add)\n v.emplace_back(nodes[x].key, nodes[x].val);\n else{\n if(nodes[x].c[1])\n sta.emplace(nodes[x].c[1], false);\n sta.emplace(x, true);\n if(nodes[x].c[0])\n sta.emplace(nodes[x].c[0], false);\n }\n }\n return v;\n }\n};\n\n\nstruct HashPatriciaTree{\n struct Node{\n bool is_elm = false;\n int ch = 0;\n int len = 0;\n int par = -1;\n int data_idx = -1;\n Hash hash;\n };\n\n static int fat(int l, int r){\n return r & (-1 << (31 - __builtin_clz(l ^ r)));\n };\n\n AVL_map<int,int> avl;\n vector<function<Hash(int)>> hash_func;\n vector<function<int(int)>> access_func;\n vector<Node> nodes;\n HashMap<uint64_t, int> hmap;\n vector<int> idxes;\n\n HashPatriciaTree() : nodes(1){\n }\n\n void make_fat(int x){\n int st = nodes[nodes[x].par].len;\n int en = nodes[x].len;\n int fa = fat(st, en);\n auto ha = hash_func[nodes[x].data_idx](fa);\n nodes[x].hash = ha;\n hmap.add(ha.concat(ha.l).h, x);\n }\n\n void make(int idx, int target, int len, int label){\n nodes.emplace_back();\n nodes.back().len = len;\n nodes.back().data_idx = idx;\n nodes.back().par = target;\n nodes[target].ch = avl.add(nodes[target].ch, label, nodes.size() - 1);\n make_fat(nodes.size() - 1);\n }\n\n void split(int idx, int target, int len, int match_len, int new_label, int root_label, int branch_label){\n int par = nodes[target].par;\n nodes.emplace_back();\n nodes.back().len = match_len;\n nodes.back().data_idx = idx;\n nodes.back().par = par;\n\n Hash ha = nodes[target].hash;\n hmap.erase(ha.concat(ha.l).h);\n\n nodes[par].ch = avl.add(nodes[par].ch, root_label, nodes.size() - 1);\n nodes.back().ch = avl.add(nodes.back().ch, branch_label, target);\n nodes[target].par = nodes.size() - 1;\n int target_siz = nodes.size() - 1;\n if(nodes.back().len != len)\n make(idx, nodes.size() - 1, len, new_label);\n make_fat(target);\n make_fat(target_siz);\n }\n\n // f(x): 長さxのprefixのハッシュを返す\n // g(x): x文字目を返す\n void insert(int m, function<Hash(int)> f, function<int(int)> g){\n hash_func.emplace_back(f);\n access_func.emplace_back(g);\n int len, x;\n tie(len, x) = match(m, f, g);\n assert(!(len || x) || (nodes[nodes[x].par].len < len && len <= nodes[x].len));\n if(len == nodes[x].len){\n if(len == m){\n idxes.emplace_back(x);\n nodes[x].data_idx = idxes.size() - 1;\n nodes[x].is_elm = true;\n return;\n }\n else\n make(idxes.size(), x, m, g(len));\n }\n else{\n split(idxes.size(), x, m, len, g(len), access_func[nodes[x].data_idx](nodes[nodes[x].par].len), access_func[nodes[x].data_idx](len));\n }\n idxes.emplace_back(nodes.size() - 1);\n nodes.back().is_elm = true;\n }\n\n int lcp(int ok, int ng, function<Hash(int)> f, function<Hash(int)> g){\n while(ng - ok > 1){\n int mid = (ok + ng) >> 1;\n (f(mid) == g(mid) ? ok : ng) = mid;\n }\n return ok;\n }\n\n // (マッチ長, ノード)\n pair<int,int> match(int m, function<Hash(int)> f, function<int(int)> g){\n int ok = 0, ng = m + 1;\n int x = 0;\n int ok_len = 0;\n while(ng - ok > 1){\n int mid = fat(ok + 1, ng - 1);\n Hash h = f(mid);\n int idx;\n bool fl;\n tie(idx, fl) = hmap.find(h.concat(h.l).h);\n if(fl){\n x = idx;\n ok_len = ok;\n ok = nodes[x].len;\n }\n else{\n ng = mid;\n }\n }\n int match_len;\n if(ok == 0){\n match_len = 0;\n }\n else{\n match_len = lcp(ok_len, min(m, nodes[x].len) + 1, f, hash_func[nodes[x].data_idx]);\n if(match_len < nodes[x].len){\n return {match_len, x};\n }\n }\n int label = g(ok);\n int ch;\n bool fl;\n tie(ch, fl) = avl.get(nodes[x].ch, label);\n if(!fl){\n assert(match_len <= m);\n return {match_len, x};\n }\n match_len = lcp(match_len, min(m, nodes[ch].len) + 1, f, hash_func[nodes[ch].data_idx]);\n assert(match_len <= m);\n return {match_len, ch};\n }\n template<typename T>\n static pair<function<Hash(int)>, function<int(int)>> build(T& s, vector<Hash>& v){\n auto f = [&v](int i){\n return v[i];\n };\n auto g = [&s](int i){\n return s[i];\n };\n return {f, g};\n }\n template<typename T>\n static pair<vector<function<Hash(int)>>, vector<function<int(int)>>> build_suffix(T& s, vector<Hash>& v){\n vector<function<Hash(int)>> fv;\n vector<function<int(int)>> gv;\n for(int j = 0; j < s.size(); ++j){\n auto f = [j, &v](int i){\n return v[i + j].sub(v[j]);\n };\n auto g = [j, &s](int i){\n return s[i + j];\n };\n fv.emplace_back(f);\n gv.emplace_back(g);\n }\n return {fv, gv};\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/bitvector.cpp.html\nstruct BitVector{\n vector<uint64_t> v;\n vector<int> r;\n BitVector(){}\n void build(){\n r.assign(v.size() + 1, 0);\n for(int i = 0; i < v.size(); ++i)\n r[i + 1] = r[i] + __builtin_popcountll(v[i]);\n }\n bool access(int x){\n return (v[x >> 6] >> (x & 63)) & 1;\n }\n // [0, x)の1の出現回数\n int rank(int x){\n return r[x >> 6] + __builtin_popcountll(v[x >> 6] & ((1uLL << (x & 63)) - 1));\n }\n int rank(int x, bool fl){\n return fl ? rank(x) : x - rank(x);\n }\n};\n\n\n// doc: https://shibh308.github.io/library/library/lib/classes/waveletmatrix.cpp.html\ntemplate <typename T, int W>\nstruct WaveletMatrix{\n\n array<BitVector, W> bv;\n array<int, W> zero_cnt;\n\n WaveletMatrix(vector<T>& a){\n int n = a.size();\n vector<T> v(a);\n for(int i = W - 1; i >= 0; --i){\n vector<uint64_t> b((n >> 6) + 1, 0);\n vector<T> v1, v2;\n for(int j = 0; j < n; ++j){\n ((v[j] >> i) & 1 ? v2 : v1).push_back(v[j]);\n b[j >> 6] |= uint64_t((v[j] >> i) & 1) << (j & 63);\n }\n for(int j = 0; j < v.size(); ++j)\n v[j] = (j < v1.size() ? v1[j] : v2[j - v1.size()]);\n bv[i].v = move(b);\n bv[i].build();\n zero_cnt[i] = bv[i].rank(n, 0);\n }\n }\n\n // [l, r)内のxの数\n int count(int l, int r, T x){\n for(int i = W - 1; i >= 0; --i){\n bool fl = (x >> i) & 1;\n int st = bv[i].rank(l, fl);\n int en = bv[i].rank(r, fl);\n l = (fl ? zero_cnt[i] : 0) + st;\n r = (fl ? zero_cnt[i] : 0) + en;\n }\n return r - l;\n }\n\n // [l, r)内で[0, x)を満たす値の数\n int count_lower(int l, int r, T x){\n int cnt = 0;\n for(int i = W - 1; i >= 0; --i){\n bool fl = (x >> i) & 1;\n int st = bv[i].rank(l, fl);\n int en = bv[i].rank(r, fl);\n if(fl){\n st += zero_cnt[i];\n en += zero_cnt[i];\n cnt += (bv[i].rank(r, 0) - bv[i].rank(l, 0));\n }\n l = st, r = en;\n }\n return cnt;\n }\n\n // [l, r)内で[x, y)を満たす値の数\n int count_range(int l, int r, T x, T y){\n return count_lower(l, r, y) - count_lower(l, r, x);\n }\n\n // 0-indexedでk番目に小さいものを返す\n T kth_min(int l, int r, int k){\n T ans = 0;\n for(int i = W - 1; i >= 0; --i){\n int st = bv[i].rank(l, 0);\n int en = bv[i].rank(r, 0);\n if(en - st <= k){\n k -= en - st;\n l = zero_cnt[i] + bv[i].rank(l, 1);\n r = zero_cnt[i] + bv[i].rank(r, 1);\n ans |= (1uLL << i);\n }\n else{\n l = st, r = en;\n }\n }\n return ans;\n }\n\n // [l, r)でのx以上最小値\n pair<T, bool> predecessor(int l, int r, T x){\n int idx = count_lower(l, r, x);\n if(idx == r - l){\n return make_pair((1uLL << W) - 1, false);\n }\n return make_pair(kth_min(l, r, idx), true);\n }\n\n // [l, r)でのx以下最大値\n pair<T, bool> successor(int l, int r, T x){\n int idx = count_lower(l, r, x + 1);\n if(idx == 0)\n return make_pair(0, false);\n return make_pair(kth_min(l, r, idx - 1), true);\n }\n};\n\n\nsigned main(){\n HashPatriciaTree tree;\n string s;\n int q;\n cin >> s >> q;\n\n int n = s.size();\n vector<Hash> sh = Hash::make(s);\n auto s_res = HashPatriciaTree::build_suffix(s, sh);\n for(int i = 0; i < n; ++i){\n tree.insert(n - i, s_res.first[i], s_res.second[i]);\n }\n\n vector<int> v;\n int k = tree.nodes.size();\n vector<int> in(k), out(k);\n function<void(int)> f = [&](int x){\n in[x] = v.size();\n v.emplace_back(tree.nodes[x].is_elm ? tree.nodes[x].data_idx : n);\n for(auto p : tree.avl.list(tree.nodes[x].ch)){\n f(p.second);\n }\n out[x] = v.size();\n };\n f(0);\n\n WaveletMatrix<int, 20> wm(v);\n\n for(int i = 0; i < q; ++i){\n int l, r;\n string t;\n cin >> l >> r >> t;\n ++r;\n auto h = Hash::make(t);\n auto res = HashPatriciaTree::build(t, h);\n int len, node;\n tie(len, node) = tree.match(t.size(), res.first, res.second);\n if(len < t.size()){\n cout << 0 << endl;\n continue;\n }\n int in_x = in[node];\n int out_x = out[node];\n\n int left = l;\n int right = max(left, min(int(v.size()), r + 1 - int(t.size())));\n if(right <= left)\n cout << 0 << endl;\n else\n cout << wm.count_range(in_x, out_x, left, right) << endl;\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 64168, "score_of_the_acc": -0.3334, "final_rank": 7 }, { "submission_id": "aoj_1585_3435015", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst int MD = 2;\nconst ll hash_base[MD] = {1007,1021};\nconst ll hash_mod[MD] = {1000000009,1000000007};\n\nint N;\n\nconst int SZ = 100010;\nll hs[MD][SZ], pw[MD][SZ];\nchar s[SZ];\nchar t[SZ];\n\nvoid init(){\n N = strlen(s);\n rep(i,MD){\n rep(j,N+1){\n hs[i][j] = 0;\n pw[i][j] = 0;\n }\n\n hs[i][0] = 0;\n pw[i][0] = 1;\n rep(j,N){\n pw[i][j+1] = pw[i][j]*hash_base[i] % hash_mod[i];\n hs[i][j+1] = (hs[i][j]*hash_base[i]+s[j]) % hash_mod[i];\n }\n }\n}\n\n// 1-index\nll hash_value(int l, int r, int i){\n return ((hs[i][r] - hs[i][l]*pw[i][r-l])%hash_mod[i]+hash_mod[i])%hash_mod[i];\n}\n\nll calc(int l, int r){\n return (hash_value(l-1,r,0)<<32)|hash_value(l-1,r,1);\n}\n\nvector<int> qidx[2000];\n\nint main(){\n int q;\n scanf(\" %s %d\", t, &q);\n\n int n = strlen(t);\n\n vector<int> l(q),r(q);\n vector<int> sz(q);\n vector<ll> qval(q);\n rep(i,q){\n scanf(\" %d %d %s\", &l[i], &r[i], s);\n\n sz[i] = strlen(s);\n init();\n qval[i] = calc(1, sz[i]);\n }\n\n vector<int> usz(sz);\n sort(all(usz));\n usz.erase(unique(all(usz)), usz.end());\n int U = usz.size();\n\n rep(i,q){\n int idx = lower_bound(all(usz), sz[i]) - usz.begin();\n qidx[idx].pb(i);\n }\n\n vector<int> ans(q,0);\n strcpy(s,t);\n init();\n\n assert(U<=500);\n rep(i,U){\n if(usz[i] > n) break;\n unordered_map<ll,vector<int>> pos;\n rep(j,n+1-usz[i]){\n ll hval = calc(j+1, j+usz[i]);\n pos[hval].pb(j);\n }\n\n for(int j:qidx[i]){\n int L = l[j], R = r[j] - (sz[j]-1);\n if(L>R) continue;\n if(pos.count(qval[j])){\n ans[j] = lower_bound(all(pos[qval[j]]), R+1) - lower_bound(all(pos[qval[j]]), L);\n }\n }\n }\n\n rep(i,q) printf(\"%d\\n\", ans[i]);\n return 0;\n}", "accuracy": 0.90625, "time_ms": 1710, "memory_kb": 18336, "score_of_the_acc": -1.0345, "final_rank": 14 }, { "submission_id": "aoj_1585_3435014", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nconst int MD = 2;\nconst ll hash_base[MD] = {1009,1021};\nconst ll hash_mod[MD] = {1000000009,1000000007};\n\nint N;\n\nconst int SZ = 100010;\nll hs[MD][SZ], pw[MD][SZ];\nchar s[SZ];\nchar t[SZ];\n\nvoid init(){\n N = strlen(s);\n rep(i,MD){\n rep(j,N+1){\n hs[i][j] = 0;\n pw[i][j] = 0;\n }\n\n hs[i][0] = 0;\n pw[i][0] = 1;\n rep(j,N){\n pw[i][j+1] = pw[i][j]*hash_base[i] % hash_mod[i];\n hs[i][j+1] = (hs[i][j]*hash_base[i]+s[j]) % hash_mod[i];\n }\n }\n}\n\n// 1-index\nll hash_value(int l, int r, int i){\n return ((hs[i][r] - hs[i][l]*pw[i][r-l])%hash_mod[i]+hash_mod[i])%hash_mod[i];\n}\n\nll calc(int l, int r){\n return (hash_value(l-1,r,0)<<32)|hash_value(l-1,r,1);\n}\n\nvector<int> qidx[2000];\n\nint main(){\n int q;\n scanf(\" %s %d\", t, &q);\n\n int n = strlen(t);\n\n vector<int> l(q),r(q);\n vector<int> sz(q);\n vector<ll> qval(q);\n rep(i,q){\n scanf(\" %d %d %s\", &l[i], &r[i], s);\n\n sz[i] = strlen(s);\n init();\n qval[i] = calc(1, sz[i]);\n }\n\n vector<int> usz(sz);\n sort(all(usz));\n usz.erase(unique(all(usz)), usz.end());\n int U = usz.size();\n\n rep(i,q){\n int idx = lower_bound(all(usz), sz[i]) - usz.begin();\n qidx[idx].pb(i);\n }\n\n vector<int> ans(q,0);\n strcpy(s,t);\n init();\n\n assert(U<=500);\n rep(i,U){\n if(usz[i] > n) break;\n unordered_map<ll,vector<int>> pos;\n rep(j,n+1-usz[i]){\n ll hval = calc(j+1, j+usz[i]);\n pos[hval].pb(j);\n }\n\n for(int j:qidx[i]){\n int L = l[j], R = r[j] - (sz[j]-1);\n if(L>R) continue;\n if(pos.count(qval[j])){\n ans[j] = lower_bound(all(pos[qval[j]]), R+1) - lower_bound(all(pos[qval[j]]), L);\n }\n }\n }\n\n rep(i,q) printf(\"%d\\n\", ans[i]);\n return 0;\n}", "accuracy": 0.90625, "time_ms": 1710, "memory_kb": 18332, "score_of_the_acc": -1.0345, "final_rank": 13 }, { "submission_id": "aoj_1585_3434624", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nusing pl = pair<int,int>;\n\nstruct RollingHash{\n static const int MD = 2;\n static const vector<ll> hash_base, hash_mod;\n\n int n;\n vector<ll> hs[MD], pw[MD];\n\n RollingHash(){}\n RollingHash(const string &s){\n n = s.size();\n rep(i,MD){\n hs[i].assign(n+1,0);\n pw[i].assign(n+1,0);\n hs[i][0] = 0;\n pw[i][0] = 1;\n rep(j,n){\n pw[i][j+1] = pw[i][j]*hash_base[i] % hash_mod[i];\n hs[i][j+1] = (hs[i][j]*hash_base[i]+s[j]) % hash_mod[i];\n }\n }\n }\n\n // 1-index\n ll hash_value(int l, int r, int i){\n return ((hs[i][r] - hs[i][l]*pw[i][r-l])%hash_mod[i]+hash_mod[i])%hash_mod[i];\n }\n\n bool match(int l1, int r1, int l2, int r2){\n bool ret = true;\n rep(i,MD) ret &= (hash_value(l1-1,r1,i) == hash_value(l2-1,r2,i));\n return ret;\n }\n\n pl calc(int l, int r){\n // vector<ll> ret(MD);\n // rep(i,MD) ret[i]=hash_value(l-1,r,i);\n // return ret;\n return {hash_value(l-1,r,0), hash_value(l-1,r,1)};\n }\n};\nconst vector<ll> RollingHash::hash_base{1009,1021};\nconst vector<ll> RollingHash::hash_mod{1000000009,1000000007};\n\nusing M = map<pl,int>;\nstruct SegTree{\n int n; vector<M> dat;\n //初期化\n SegTree(){}\n SegTree(int _n){\n n=1;\n while(n<_n) n*=2;\n dat=vector<M>(2*n-1);\n }\n\n void add(int k, pl v){\n k+=n-1;\n ++dat[k][v];\n //更新\n while(k>0){\n k=(k-1)/2;\n ++dat[k][v];\n }\n }\n //内部的に投げられるクエリ\n int _query(int a, int b, int k, int l, int r, pl v){\n if(r<=a || b<=l) return 0;\n if(a<=l && r<=b){\n if(dat[k].count(v)) return dat[k][v];\n return 0;\n }\n\n int vl=_query(a,b,2*k+1,l,(l+r)/2,v);\n int vr=_query(a,b,2*k+2,(l+r)/2,r,v);\n return vl+vr;\n }\n //[a,b)\n int query(int a, int b, pl v){\n return _query(a,b,0,0,n,v);\n }\n};\n\n\nint main(){\n cin.tie(0);ios::sync_with_stdio(false);\n\n string s;\n int q;\n cin >>s >>q;\n\n int n = s.size();\n\n vector<int> l(q),r(q);\n vector<string> m(q);\n vector<int> sz(q);\n rep(i,q){\n cin >>l[i] >>r[i] >>m[i];\n sz[i] = m[i].size();\n }\n\n vector<int> usz(sz);\n sort(all(usz));\n usz.erase(unique(all(usz)), usz.end());\n int U = usz.size();\n\n assert(U<=100);\n\n vector<vector<int>> qidx(U);\n rep(i,q){\n int idx = lower_bound(all(usz), sz[i]) - usz.begin();\n qidx[idx].pb(i);\n }\n\n vector<int> ans(q);\n RollingHash hs(s);\n rep(i,U){\n if(usz[i]<1000){\n SegTree st(n+1-usz[i]);\n rep(j,n+1-usz[i]) st.add(j, hs.calc(j+1, j+usz[i]));\n\n for(int j:qidx[i]){\n RollingHash qh(m[j]);\n int L = l[j], R = r[j] - (sz[j]-1);\n ans[j] = st.query(L,R+1,qh.calc(1,sz[j]));\n }\n }\n else{\n for(int j:qidx[i]){\n RollingHash qh(m[j]);\n pl val = qh.calc(1,sz[j]);\n\n int L = l[j], R = r[j] - (sz[j]-1);\n for(int k=L; k<=R; ++k){\n if(hs.calc(k+1, k+sz[j]) == val) ++ans[j];\n }\n }\n }\n }\n\n rep(i,q) cout << ans[i] << \"\\n\";\n return 0;\n}", "accuracy": 0.359375, "time_ms": 20, "memory_kb": 3884, "score_of_the_acc": 0, "final_rank": 20 }, { "submission_id": "aoj_1585_2891690", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\nint T_length,P_length,range;\nint *Rank; //接尾辞文字列の開始位置iの、辞書順にsortした際の順位表\nint *Suffix_Array; //接尾辞文字列の開始位置<インデックス>→毎回、辞書順に開始位置をソートする\nint *Work; //作業用配列\n\nchar T[100001],P[100001];\n\n\n//Tのstart文字目から、P_length文字分、Pと比較\n//0:等しい 1:Pの方が辞書順で早い 2:Tの方が辞書順で早い\nint strCmp_Suffix_Array(int start){\n\tint index,ret;\n\n\tret = 0;\n\n\tfor(index = 0; index < P_length && T[start+index] != '\\0'; index++){\n\t\tif(P[index] != T[start+index]){\n\t\t\tif(P[index] > T[start+index]){ //Tの方が辞書順で早い\n\t\t\t\tret = 2;\n\t\t\t}else{\n\t\t\t\tret = 1;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\tif(ret == 0 && index < P_length){ //不一致がないまま、Tが先に終わった場合\n\t\tret = 2; //Tの方が辞書順で早い\n\t}\n\treturn ret;\n}\n\n\n//まずrank[a]とrank[b]を比較、等しければrank[a+range],rank[b+range]を比較\nbool compare_Suffix_Array(int a,int b){\n\tif(Rank[a] != Rank[b])return Rank[a] < Rank[b];\n\telse{\n\t\tint rank_a = a + range <= T_length ? Rank[a+range]:-1; //rangeを足した部分が範囲外なら、最高にするため、rankを-1にする\n\t\tint rank_b = b + range <= T_length ? Rank[b+range]:-1;\n\t\treturn rank_a < rank_b;\n\t}\n}\n\n//文字列Tの接尾辞配列を構築\nvoid make_Suffix_Array(){\n\tfor(int i = 0; i <= T_length; i++){\n\t\tSuffix_Array[i] = i;\n\t\tRank[i] = i < T_length? T[i]:-1; //初期ランクは文字コードにする。T[T_length]は空文字なので、最高となるように-1とする\n\t}\n\n\t//range,2*range,4*range,とソート幅を次第に伸ばしていく\n\tfor(range = 1; range <= T_length; range*=2){\n\t\tsort(Suffix_Array,Suffix_Array+(T_length+1),compare_Suffix_Array); //range文字の幅でsuffix_Arrayをソート\n\n\t\tWork[Suffix_Array[0]] = 0;\n\t\tfor(int i = 1; i <= T_length; i++){\n\t\t\tWork[Suffix_Array[i]] = Work[Suffix_Array[i-1]] + (compare_Suffix_Array(Suffix_Array[i-1],Suffix_Array[i])?1:0);\n\t\t}\n\t\tfor(int i = 0; i <= T_length; i++){\n\t\t\tRank[i] = Work[i];\n\t\t}\n\t}\n}\n\n\n//パターン文字列の、Suffix_Array上での、最も左の一致★インデックス★を返却\nint find_match_left(){\n\n\tint ret = BIG_NUM;\n\n\tint left = 0,right = T_length,m;\n\tm = (left+right)/2; //suffix_arrayのインデックス\n\n\twhile(left <= right){\n\t\tswitch(strCmp_Suffix_Array(Suffix_Array[m])){\n\t\tcase 0: //T[Suffix_Array[m]~Suffix_Array[m]+P_length]の部分文字列がPと等しい\n\t\t\tret = m;\n\t\t\tright = m-1; //より左へ\n\t\t\tbreak;\n\t\tcase 1: //Pの方が辞書順で早い\n\t\t\tright = m-1;\n\t\t\tbreak;\n\t\tcase 2: //Tの方が辞書順で早い\n\t\t\tleft = m+1;\n\t\t\tbreak;\n\t\t}\n\t\tm = (left+right)/2;\n\t}\n\treturn ret;\n}\n\n//パターン文字列の、Suffix_Array上での、最も右の一致★インデックス★を返却\nint find_match_right(){\n\n\tint ret = -1;\n\n\tint left = 0,right = T_length,m;\n\tm = (left+right)/2; //suffix_arrayのインデックス\n\n\twhile(left <= right){\n\t\tswitch(strCmp_Suffix_Array(Suffix_Array[m])){\n\t\tcase 0: //T[Suffix_Array[m]~Suffix_Array[m]+P_length]の部分文字列がPと等しい\n\t\t\tret = max(ret,m);\n\t\t\tleft = m+1; //より右へ\n\t\t\tbreak;\n\t\tcase 1: //Pの方が辞書順で早い\n\t\t\tright = m-1;\n\t\t\tbreak;\n\t\tcase 2: //Tの方が辞書順で早い\n\t\t\tleft = m+1;\n\t\t\tbreak;\n\t\t}\n\t\tm = (left+right)/2;\n\t}\n\treturn ret;\n}\n\nint count_contain(int left,int right){\n\n\t//一番左の一致箇所\n\tint match_left = find_match_left();\n\tif(match_left == BIG_NUM)return 0;\n\n\t//一番右の一致箇所\n\tint match_right = find_match_right();\n\tif(match_right == BIG_NUM)return 0;\n\n\tint ret = 0;\n\tfor(int i = match_left; i <= match_right; i++){\n\t\tif(Suffix_Array[i] >= left && Suffix_Array[i]+P_length-1 <= right)ret++;\n\t}\n\treturn ret;\n}\n\n\nint main(){\n\n\tint num_query;\n\tscanf(\"%s %d\",T,&num_query);\n\n\tfor(T_length = 0; T[T_length] != '\\0'; T_length++);\n\n\tRank = new int[T_length+1];\n\tSuffix_Array = new int[T_length+1];\n\tWork = new int[T_length+1];\n\n\tmake_Suffix_Array();\n\n\tint left,right;\n\n\tfor(int loop = 0; loop < num_query; loop++){\n\t\tscanf(\"%d %d %s\",&left,&right,P);\n\n\t\tfor(P_length = 0; P[P_length] != '\\0'; P_length++);\n\n\t\tprintf(\"%d\\n\",count_contain(left,right));\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 540, "memory_kb": 4592, "score_of_the_acc": -0.3094, "final_rank": 5 }, { "submission_id": "aoj_1585_2748227", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define mp make_pair\n#define pb push_back\n#define all(x) begin(x),end(x)\n#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))\n#define fi first\n#define se second\n#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\nvoid _dbg(string){cerr<<endl;}\ntemplate<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<\" = \"<<h<<\", \";_dbg(s.substr(l+1),t...);}\ntemplate<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nclass SuffixArray {\npublic:\n string &s;\n vector<int> sa, rank;\n int n;\n void construct_sa(){\n sa.resize(n+1);\n rank.resize(n+1);\n rep(i,n+1){\n sa[i]=i;\n rank[i] = (i<n)?s[i]:(-1);\n }\n vector<int> tmp(n+1,0);\n for(int k=1; k<=n; k*=2){\n auto comp = [&](const int i, const int j){\n if(rank[i]!=rank[j]) return rank[i]<rank[j];\n int ri = (i+k<=n)?(rank[i+k]):-1;\n int rj = (j+k<=n)?(rank[j+k]):-1;\n return ri < rj;\n };\n sort(all(sa), comp);\n tmp[sa[0]]=0;\n rep(i,1,n+1){\n tmp[sa[i]] = tmp[sa[i-1]];\n if(comp(sa[i-1], sa[i])) tmp[sa[i]]++;\n }\n swap(tmp, rank);\n }\n }\n SuffixArray(string &str) : s(str){\n n = s.size();\n construct_sa();\n }\n int getRank(const string t) const {\n int m = t.size();\n int l = 0, r = n+1;\n while(r-l>1){\n int mid = (r+l)/2;\n if(s.compare(sa[mid], m, t) < 0) l = mid;\n else r = mid;\n }\n return r;\n }\n};\n\nint rank_v[100005];\n\nclass SegTree{\npublic:\n int n;\n vector<int> data[1<<18];\n SegTree(int n_){\n n=1;\n while(n<n_) n*=2;\n rep(i,n_){\n data[n-1+i].pb(rank_v[i]);\n }\n for(int i=n-2; i>=0; i--){\n int il = 2*i+1, ir = 2*i+2;\n data[i].resize(data[il].size()+data[ir].size());\n merge(all(data[il]), all(data[ir]), data[i].begin());\n }\n }\n // 区間[a,b)でx以下の個数\n int query(int a, int b, int x, int k, int l, int r) const {\n if(r<=a || b<=l) return 0;\n if(a<=l && r<=b){\n return upper_bound(all(data[k]), x) - data[k].begin();\n }\n int vl = query(a,b,x,k*2+1,l,(r+l)/2);\n int vr = query(a,b,x,k*2+2,(r+l)/2,r);\n return vl+vr;\n }\n int query(int a, int b, int x) const {\n return query(a,b,x,0,0,n);\n }\n};\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0); cout.tie(0);\n\n string s;\n int q;\n cin>>s>>q;\n SuffixArray sa(s);\n int n = s.size();\n\n rep(i,n) rank_v[i] = sa.rank[i];\n SegTree st(s.size());\n// dbg(sa.rank, sa.sa);\n rep(_,q){\n int l,r;\n string t;\n cin>>l>>r>>t;\n r = r+1 - (t.size()-1);\n int lb = sa.getRank(t);\n int ub = sa.getRank(t+'|');\n // dbg(lb,ub);\n\n int ans = st.query(l,r,ub-1) - st.query(l,r,lb-1);\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 22252, "score_of_the_acc": -0.3043, "final_rank": 4 }, { "submission_id": "aoj_1585_2748047", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define mp make_pair\n#define pb push_back\n#define all(x) begin(x),end(x)\n#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))\n#define fi first\n#define se second\n#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)\nvoid _dbg(string){cerr<<endl;}\ntemplate<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<\" = \"<<h<<\", \";_dbg(s.substr(l+1),t...);}\ntemplate<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nclass SuffixArray {\npublic:\n string &s;\n vector<int> sa, rank;\n int n;\n void construct_sa(){\n sa.resize(n+1);\n rank.resize(n+1);\n rep(i,n+1){\n sa[i]=i;\n rank[i] = (i<n)?s[i]:(-1);\n }\n vector<int> tmp(n+1,0);\n for(int k=1; k<=n; k*=2){\n auto comp = [&](const int i, const int j){\n if(rank[i]!=rank[j]) return rank[i]<rank[j];\n int ri = (i+k<=n)?(rank[i+k]):-1;\n int rj = (j+k<=n)?(rank[j+k]):-1;\n return ri < rj;\n };\n sort(all(sa), comp);\n tmp[sa[0]]=0;\n rep(i,1,n+1){\n tmp[sa[i]] = tmp[sa[i-1]];\n if(comp(sa[i-1], sa[i])) tmp[sa[i]]++;\n }\n swap(tmp, rank);\n }\n }\n SuffixArray(string &str) : s(str){\n n = s.size();\n construct_sa();\n }\n int getRank(const string t){\n int m = t.size();\n int l = 0, r = n+1;\n while(r-l>1){\n int mid = (r+l)/2;\n if(s.compare(sa[mid], m, t) < 0) l = mid;\n else r = mid;\n }\n return r;\n }\n};\n\nint rank_v[100005];\n\nclass SegTree{\npublic:\n int n;\n vector<int> data[1<<18];\n SegTree(int n_){\n n=1;\n while(n<n_) n*=2;\n rep(i,n_){\n data[n-1+i].pb(rank_v[i]);\n }\n for(int i=n-2; i>=0; i--){\n int il = 2*i+1, ir = 2*i+2;\n data[i].resize(data[il].size()+data[ir].size());\n merge(all(data[il]), all(data[ir]), data[i].begin());\n }\n }\n // 区間[a,b)でx以下の個数\n int query(int a, int b, int x, int k, int l, int r){\n if(r<=a || b<=l) return 0;\n if(a<=l && r<=b){\n return upper_bound(all(data[k]), x) - data[k].begin();\n }\n int vl = query(a,b,x,k*2+1,l,(r+l)/2);\n int vr = query(a,b,x,k*2+2,(r+l)/2,r);\n return vl+vr;\n }\n int query(int a, int b, int x){\n return query(a,b,x,0,0,n);\n }\n};\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(0); cout.tie(0);\n\n string s;\n int q;\n cin>>s>>q;\n SuffixArray sa(s);\n int n = s.size();\n\n rep(i,n) rank_v[i] = sa.rank[i];\n SegTree st(s.size());\n// dbg(sa.rank, sa.sa);\n rep(_,q){\n int l,r;\n string t;\n cin>>l>>r>>t;\n r = r+1 - (t.size()-1);\n int lb = sa.getRank(t);\n int ub = sa.getRank(t+'|');\n // dbg(lb,ub);\n\n int ans = st.query(l,r,ub-1) - st.query(l,r,lb-1);\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 22328, "score_of_the_acc": -0.334, "final_rank": 9 }, { "submission_id": "aoj_1585_2610007", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nstruct SuffixArray\n{\n vector< int > SA;\n string s;\n\n void Build_SA(const string &str)\n {\n s = str;\n SA.resize(s.size());\n iota(begin(SA), end(SA), 0);\n sort(begin(SA), end(SA), [&](const int &a, const int &b)\n {\n if(s[a] == s[b]) return (a > b);\n return (s[a] < s[b]);\n });\n vector< int > classes(s.size()), c(s.size()), cnt(s.size());\n for(int i = 0; i < s.size(); i++) {\n c[i] = s[i];\n }\n for(int len = 1; len < s.size(); len <<= 1) {\n for(int i = 0; i < s.size(); i++) {\n if(i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() && c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) {\n classes[SA[i]] = classes[SA[i - 1]];\n } else {\n classes[SA[i]] = i;\n }\n }\n iota(begin(cnt), end(cnt), 0);\n copy(begin(SA), end(SA), begin(c));\n for(int i = 0; i < s.size(); i++) {\n int s1 = c[i] - len;\n if(s1 >= 0) SA[cnt[classes[s1]]++] = s1;\n }\n classes.swap(c);\n }\n }\n\n int operator[](int k) const\n {\n return (SA[k]);\n }\n\n int size() const\n {\n return (s.size());\n }\n\n bool lt_substr(string &t, int si = 0, int ti = 0)\n {\n int sn = s.size(), tn = t.size();\n while(si < sn && ti < tn) {\n if(s[si] < t[ti]) return (true);\n if(s[si] > t[ti]) return (false);\n ++si, ++ti;\n }\n return (si >= sn && ti < tn);\n }\n\n int lower_bound(string &t)\n {\n int low = -1, high = SA.size();\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(lt_substr(t, SA[mid])) low = mid;\n else high = mid;\n }\n return (high);\n }\n\n pair< int, int > lower_upper_bound(string &t)\n {\n int idx = lower_bound(t);\n int low = idx - 1, high = SA.size();\n t.back()++;\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(lt_substr(t, SA[mid])) low = mid;\n else high = mid;\n }\n t.back()--;\n return (make_pair(idx, high));\n }\n\n void output()\n {\n for(int i = 0; i < size(); i++) {\n cout << i << \": \" << s.substr(SA[i]) << endl;\n }\n }\n};\n\nstruct SuccinctIndexableDictionary\n{\n size_t length;\n size_t blocks;\n vector< unsigned > bit, sum;\n\n SuccinctIndexableDictionary()\n {\n }\n\n SuccinctIndexableDictionary(size_t _length)\n {\n length = _length;\n blocks = (length + 31) >> 5;\n bit.assign(blocks, 0U);\n sum.assign(blocks, 0U);\n }\n\n void set(int k)\n {\n bit[k >> 5] |= 1U << (k & 31);\n }\n\n void build()\n {\n sum[0] = 0U;\n for(int i = 1; i < blocks; i++) {\n sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);\n }\n }\n\n bool operator[](int k) const\n {\n return (bool((bit[k >> 5] >> (k & 31)) & 1));\n }\n\n int rank(int k)\n {\n return (sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1)));\n }\n\n int rank(bool val, int k)\n {\n return (val ? rank(k) : k - rank(k));\n }\n\n int select(bool val, int k)\n {\n if(k < 0 || rank(val, length) <= k) return (-1);\n int low = 0, high = length;\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(rank(val, mid) >= k + 1) high = mid;\n else low = mid;\n }\n return (high - 1);\n }\n\n int select(bool val, int i, int l)\n {\n return select(val, i + rank(val, l));\n }\n};\n\ntemplate< class T, int MAXLOG >\nstruct WaveletMatrix\n{\n size_t length;\n SuccinctIndexableDictionary matrix[MAXLOG];\n int zs[MAXLOG];\n int buff1[MAXLOG], buff2[MAXLOG];\n\n void max_dfs(int d, int l, int r, int &k, T val, vector< T > &vs)\n {\n if(l >= r || !k) return;\n if(d == MAXLOG) {\n while(l++ < r && k > 0) vs.push_back(val), k--;\n return;\n }\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n max_dfs(d + 1, lc + zs[d], rc + zs[d], k, 1ULL << (MAXLOG - d - 1) | val, vs);\n max_dfs(d + 1, l - lc, r - rc, k, val, vs);\n }\n\n T max_dfs(int d, int l, int r, T val, T a, T b)\n {\n if(r - l <= 0 || val >= b) return -1;\n if(d == MAXLOG) return val >= a ? val : -1;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n T ret = max_dfs(d + 1, lc + zs[d], rc + zs[d], 1ULL << (MAXLOG - d - 1) | val, a, b);\n if(~ret) return ret;\n return max_dfs(d + 1, l - lc, r - rc, val, a, b);\n }\n\n int freq_dfs(int d, int l, int r, T val, T a, T b)\n {\n if(l == r) return 0;\n if(d == MAXLOG) return (a <= val && val < b) ? r - l : 0;\n T nv = 1ULL << (MAXLOG - d - 1) | val, nnv = ((1ULL << (MAXLOG - d - 1)) - 1) | nv;\n if(nnv < a || b <= val) return 0;\n if(a <= val && nnv < b) return r - l;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n return freq_dfs(d + 1, l - lc, r - rc, val, a, b) +\n freq_dfs(d + 1, lc + zs[d], rc + zs[d], nv, a, b);\n }\n\n void list_dfs(int d, int l, int r, T val, T a, T b, vector< pair< T, int>> &vs)\n {\n if(val >= b || r - l <= 0) return;\n if(d == MAXLOG) {\n if(a <= val) vs.push_back(make_pair(val, r - l));\n return;\n }\n T nv = val | (1LL << (MAXLOG - d - 1)), nnv = nv | (((1LL << (MAXLOG - d - 1)) - 1));\n if(nnv < a) return;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n list_dfs(d + 1, l - lc, r - rc, val, a, b, vs);\n list_dfs(d + 1, lc + zs[d], rc + zs[d], nv, a, b, vs);\n }\n\n\n WaveletMatrix(vector< T > data)\n {\n length = data.size();\n vector< T > l(length), r(length);\n for(int depth = 0; depth < MAXLOG; depth++) {\n matrix[depth] = SuccinctIndexableDictionary(length + 1);\n int left = 0, right = 0;\n for(int i = 0; i < length; i++) {\n bool k = (data[i] >> (MAXLOG - depth - 1)) & 1;\n if(k) r[right++] = data[i], matrix[depth].set(i);\n else l[left++] = data[i];\n }\n zs[depth] = left;\n matrix[depth].build();\n swap(l, data);\n for(int i = 0; i < right; i++) data[left + i] = r[i];\n }\n }\n\n T access(int k)\n {\n int ret = 0;\n bool bit;\n for(int depth = 0; depth < MAXLOG; depth++) {\n bit = matrix[depth][k];\n ret = (ret << 1) | bit;\n k = matrix[depth].rank(bit, k) + zs[depth] * bit;\n }\n return (ret);\n }\n\n int rank(T val, int k)\n {\n int l = 0, r = k;\n for(int depth = 0; depth < MAXLOG; depth++) {\n buff1[depth] = l, buff2[depth] = r;\n bool bit = (val >> (MAXLOG - depth - 1)) & 1;\n l = matrix[depth].rank(bit, l) + zs[depth] * bit;\n r = matrix[depth].rank(bit, r) + zs[depth] * bit;\n }\n return (r - l);\n }\n\n int select(T val, int kth)\n {\n rank(val, length);\n for(int depth = MAXLOG - 1; depth >= 0; depth--) {\n bool bit = (val >> (MAXLOG - depth - 1)) & 1;\n kth = matrix[depth].select(bit, kth, buff1[depth]);\n if(kth >= buff2[depth] || kth < 0) return (-1);\n kth -= buff1[depth];\n }\n return (kth);\n }\n\n int select(T val, int k, int l)\n {\n return (select(val, k + rank(val, l)));\n }\n\n\n int quantile(int left, int right, int kth)\n {\n if(right - left <= kth || kth < 0) return (-1);\n T ret = 0;\n for(int depth = 0; depth < MAXLOG; depth++) {\n int l = matrix[depth].rank(1, left);\n int r = matrix[depth].rank(1, right);\n if(r - l > kth) {\n left = l + zs[depth];\n right = r + zs[depth];\n ret |= 1ULL << (MAXLOG - depth - 1);\n } else {\n kth -= r - l;\n left -= l;\n right -= r;\n }\n }\n return (ret);\n }\n\n vector< T > topk(int l, int r, int k)\n {\n if(r - l < k) k = r - l;\n if(k < 0) return (vector< T >());\n vector< T > ret;\n max_dfs(0, l, r, k, 0, ret);\n return (ret);\n }\n\n vector< pair< T, int > > freq_list(int l, int r, T a, T b)\n {\n vector< pair< T, int > > ret;\n list_dfs(0, l, r, 0, a, b, ret);\n return (ret);\n }\n\n vector< pair< int, T > > get_rect(int l, int r, T a, T b)\n {\n vector< pair< T, int > > res = freq_list(l, r, a, b);\n vector< pair< int, T > > ret;\n for(auto &e : res) {\n for(int i = 0; i < e.second; i++) {\n ret.emplace_back(select(e.first, i, l), e.first);\n }\n }\n return (ret);\n }\n\n int rangefreq(int left, int right, T lower, T upper)\n {\n return (freq_dfs(0, left, right, 0, lower, upper));\n }\n};\n\nint main()\n{\n char S[100001], T[100001];\n int Q;\n\n scanf(\"%s %d\", S, &Q);\n SuffixArray sa;\n sa.Build_SA(S);\n vector< int > vs(sa.size());\n for(int i = 0; i < sa.size(); i++) vs[i] = sa[i];\n WaveletMatrix< int, 16 > mat(vs);\n\n for(int i = 0; i < Q; i++) {\n int l, r;\n scanf(\"%d %d %s\", &l, &r, T);\n string X = T;\n auto p = sa.lower_upper_bound(X);\n printf(\"%d\\n\", mat.rangefreq(p.first, p.second, l, r + 1 - (int) X.size() + 1));\n }\n\n}", "accuracy": 0.484375, "time_ms": 90, "memory_kb": 5064, "score_of_the_acc": -0.0442, "final_rank": 18 }, { "submission_id": "aoj_1585_2610006", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nstruct SuffixArray\n{\n vector< int > SA;\n string s;\n\n void Build_SA(const string &str)\n {\n s = str;\n SA.resize(s.size());\n iota(begin(SA), end(SA), 0);\n sort(begin(SA), end(SA), [&](const int &a, const int &b)\n {\n if(s[a] == s[b]) return (a > b);\n return (s[a] < s[b]);\n });\n vector< int > classes(s.size()), c(s.size()), cnt(s.size());\n for(int i = 0; i < s.size(); i++) {\n c[i] = s[i];\n }\n for(int len = 1; len < s.size(); len <<= 1) {\n for(int i = 0; i < s.size(); i++) {\n if(i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() && c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) {\n classes[SA[i]] = classes[SA[i - 1]];\n } else {\n classes[SA[i]] = i;\n }\n }\n iota(begin(cnt), end(cnt), 0);\n copy(begin(SA), end(SA), begin(c));\n for(int i = 0; i < s.size(); i++) {\n int s1 = c[i] - len;\n if(s1 >= 0) SA[cnt[classes[s1]]++] = s1;\n }\n classes.swap(c);\n }\n }\n\n int operator[](int k) const\n {\n return (SA[k]);\n }\n\n int size() const\n {\n return (s.size());\n }\n\n bool lt_substr(string &t, int si = 0, int ti = 0)\n {\n int sn = s.size(), tn = t.size();\n while(si < sn && ti < tn) {\n if(s[si] < t[ti]) return (true);\n if(s[si] > t[ti]) return (false);\n ++si, ++ti;\n }\n return (si >= sn && ti < tn);\n }\n\n int lower_bound(string &t)\n {\n int low = -1, high = SA.size();\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(lt_substr(t, SA[mid])) low = mid;\n else high = mid;\n }\n return (high);\n }\n\n pair< int, int > lower_upper_bound(string &t)\n {\n int idx = lower_bound(t);\n int low = idx - 1, high = SA.size();\n t.back()++;\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(lt_substr(t, SA[mid])) low = mid;\n else high = mid;\n }\n t.back()--;\n return (make_pair(idx, high));\n }\n\n void output()\n {\n for(int i = 0; i < size(); i++) {\n cout << i << \": \" << s.substr(SA[i]) << endl;\n }\n }\n};\n\nstruct SuccinctIndexableDictionary\n{\n size_t length;\n size_t blocks;\n vector< unsigned > bit, sum;\n\n SuccinctIndexableDictionary()\n {\n }\n\n SuccinctIndexableDictionary(size_t _length)\n {\n length = _length;\n blocks = (length + 31) >> 5;\n bit.assign(blocks, 0U);\n sum.assign(blocks, 0U);\n }\n\n void set(int k)\n {\n bit[k >> 5] |= 1U << (k & 31);\n }\n\n void build()\n {\n sum[0] = 0U;\n for(int i = 1; i < blocks; i++) {\n sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);\n }\n }\n\n bool operator[](int k) const\n {\n return (bool((bit[k >> 5] >> (k & 31)) & 1));\n }\n\n int rank(int k)\n {\n return (sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1)));\n }\n\n int rank(bool val, int k)\n {\n return (val ? rank(k) : k - rank(k));\n }\n\n int select(bool val, int k)\n {\n if(k < 0 || rank(val, length) <= k) return (-1);\n int low = 0, high = length;\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(rank(val, mid) >= k + 1) high = mid;\n else low = mid;\n }\n return (high - 1);\n }\n\n int select(bool val, int i, int l)\n {\n return select(val, i + rank(val, l));\n }\n};\n\ntemplate< class T, int MAXLOG >\nstruct WaveletMatrix\n{\n size_t length;\n SuccinctIndexableDictionary matrix[MAXLOG];\n int zs[MAXLOG];\n int buff1[MAXLOG], buff2[MAXLOG];\n\n void max_dfs(int d, int l, int r, int &k, T val, vector< T > &vs)\n {\n if(l >= r || !k) return;\n if(d == MAXLOG) {\n while(l++ < r && k > 0) vs.push_back(val), k--;\n return;\n }\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n max_dfs(d + 1, lc + zs[d], rc + zs[d], k, 1ULL << (MAXLOG - d - 1) | val, vs);\n max_dfs(d + 1, l - lc, r - rc, k, val, vs);\n }\n\n T max_dfs(int d, int l, int r, T val, T a, T b)\n {\n if(r - l <= 0 || val >= b) return -1;\n if(d == MAXLOG) return val >= a ? val : -1;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n T ret = max_dfs(d + 1, lc + zs[d], rc + zs[d], 1ULL << (MAXLOG - d - 1) | val, a, b);\n if(~ret) return ret;\n return max_dfs(d + 1, l - lc, r - rc, val, a, b);\n }\n\n int freq_dfs(int d, int l, int r, T val, T a, T b)\n {\n if(l == r) return 0;\n if(d == MAXLOG) return (a <= val && val < b) ? r - l : 0;\n T nv = 1ULL << (MAXLOG - d - 1) | val, nnv = ((1ULL << (MAXLOG - d - 1)) - 1) | nv;\n if(nnv < a || b <= val) return 0;\n if(a <= val && nnv < b) return r - l;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n return freq_dfs(d + 1, l - lc, r - rc, val, a, b) +\n freq_dfs(d + 1, lc + zs[d], rc + zs[d], nv, a, b);\n }\n\n void list_dfs(int d, int l, int r, T val, T a, T b, vector< pair< T, int>> &vs)\n {\n if(val >= b || r - l <= 0) return;\n if(d == MAXLOG) {\n if(a <= val) vs.push_back(make_pair(val, r - l));\n return;\n }\n T nv = val | (1LL << (MAXLOG - d - 1)), nnv = nv | (((1LL << (MAXLOG - d - 1)) - 1));\n if(nnv < a) return;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n list_dfs(d + 1, l - lc, r - rc, val, a, b, vs);\n list_dfs(d + 1, lc + zs[d], rc + zs[d], nv, a, b, vs);\n }\n\n\n WaveletMatrix(vector< T > data)\n {\n length = data.size();\n vector< T > l(length), r(length);\n for(int depth = 0; depth < MAXLOG; depth++) {\n matrix[depth] = SuccinctIndexableDictionary(length + 1);\n int left = 0, right = 0;\n for(int i = 0; i < length; i++) {\n bool k = (data[i] >> (MAXLOG - depth - 1)) & 1;\n if(k) r[right++] = data[i], matrix[depth].set(i);\n else l[left++] = data[i];\n }\n zs[depth] = left;\n matrix[depth].build();\n swap(l, data);\n for(int i = 0; i < right; i++) data[left + i] = r[i];\n }\n }\n\n T access(int k)\n {\n int ret = 0;\n bool bit;\n for(int depth = 0; depth < MAXLOG; depth++) {\n bit = matrix[depth][k];\n ret = (ret << 1) | bit;\n k = matrix[depth].rank(bit, k) + zs[depth] * bit;\n }\n return (ret);\n }\n\n int rank(T val, int k)\n {\n int l = 0, r = k;\n for(int depth = 0; depth < MAXLOG; depth++) {\n buff1[depth] = l, buff2[depth] = r;\n bool bit = (val >> (MAXLOG - depth - 1)) & 1;\n l = matrix[depth].rank(bit, l) + zs[depth] * bit;\n r = matrix[depth].rank(bit, r) + zs[depth] * bit;\n }\n return (r - l);\n }\n\n int select(T val, int kth)\n {\n rank(val, length);\n for(int depth = MAXLOG - 1; depth >= 0; depth--) {\n bool bit = (val >> (MAXLOG - depth - 1)) & 1;\n kth = matrix[depth].select(bit, kth, buff1[depth]);\n if(kth >= buff2[depth] || kth < 0) return (-1);\n kth -= buff1[depth];\n }\n return (kth);\n }\n\n int select(T val, int k, int l)\n {\n return (select(val, k + rank(val, l)));\n }\n\n\n int quantile(int left, int right, int kth)\n {\n if(right - left <= kth || kth < 0) return (-1);\n T ret = 0;\n for(int depth = 0; depth < MAXLOG; depth++) {\n int l = matrix[depth].rank(1, left);\n int r = matrix[depth].rank(1, right);\n if(r - l > kth) {\n left = l + zs[depth];\n right = r + zs[depth];\n ret |= 1ULL << (MAXLOG - depth - 1);\n } else {\n kth -= r - l;\n left -= l;\n right -= r;\n }\n }\n return (ret);\n }\n\n vector< T > topk(int l, int r, int k)\n {\n if(r - l < k) k = r - l;\n if(k < 0) return (vector< T >());\n vector< T > ret;\n max_dfs(0, l, r, k, 0, ret);\n return (ret);\n }\n\n vector< pair< T, int > > freq_list(int l, int r, T a, T b)\n {\n vector< pair< T, int > > ret;\n list_dfs(0, l, r, 0, a, b, ret);\n return (ret);\n }\n\n vector< pair< int, T > > get_rect(int l, int r, T a, T b)\n {\n vector< pair< T, int > > res = freq_list(l, r, a, b);\n vector< pair< int, T > > ret;\n for(auto &e : res) {\n for(int i = 0; i < e.second; i++) {\n ret.emplace_back(select(e.first, i, l), e.first);\n }\n }\n return (ret);\n }\n\n int rangefreq(int left, int right, T lower, T upper)\n {\n return (freq_dfs(0, left, right, 0, lower, upper));\n }\n};\n\nint main()\n{\n char S[100001], T[100001];\n int Q;\n\n scanf(\"%s %d\", S, &Q);\n SuffixArray sa;\n sa.Build_SA(S);\n vector< int > vs(sa.size());\n for(int i = 0; i < sa.size(); i++) vs[i] = sa[i];\n WaveletMatrix< int, 17 > mat(vs);\n\n for(int i = 0; i < Q; i++) {\n int l, r;\n scanf(\"%d %d %s\", &l, &r, T);\n string X = T;\n auto p = sa.lower_upper_bound(X);\n printf(\"%d\\n\", mat.rangefreq(p.first, p.second, l, r + 1 - (int) X.size() + 1));\n }\n\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 5604, "score_of_the_acc": -0.0929, "final_rank": 1 }, { "submission_id": "aoj_1585_2610003", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nstruct SuffixArray\n{\n vector< int > SA;\n string s;\n\n void Build_SA(const string &str)\n {\n s = str;\n SA.resize(s.size());\n iota(begin(SA), end(SA), 0);\n sort(begin(SA), end(SA), [&](const int &a, const int &b)\n {\n if(s[a] == s[b]) return (a > b);\n return (s[a] < s[b]);\n });\n vector< int > classes(s.size()), c(s.size()), cnt(s.size());\n for(int i = 0; i < s.size(); i++) {\n c[i] = s[i];\n }\n for(int len = 1; len < s.size(); len <<= 1) {\n for(int i = 0; i < s.size(); i++) {\n if(i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() && c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) {\n classes[SA[i]] = classes[SA[i - 1]];\n } else {\n classes[SA[i]] = i;\n }\n }\n iota(begin(cnt), end(cnt), 0);\n copy(begin(SA), end(SA), begin(c));\n for(int i = 0; i < s.size(); i++) {\n int s1 = c[i] - len;\n if(s1 >= 0) SA[cnt[classes[s1]]++] = s1;\n }\n classes.swap(c);\n }\n }\n\n int operator[](int k) const\n {\n return (SA[k]);\n }\n\n int size() const\n {\n return (s.size());\n }\n\n bool lt_substr(string &t, int si = 0, int ti = 0)\n {\n int sn = s.size(), tn = t.size();\n while(si < sn && ti < tn) {\n if(s[si] < t[ti]) return (true);\n if(s[si] > t[ti]) return (false);\n ++si, ++ti;\n }\n return (si >= sn && ti < tn);\n }\n\n int lower_bound(string &t)\n {\n int low = -1, high = SA.size();\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(lt_substr(t, SA[mid])) low = mid;\n else high = mid;\n }\n return (high);\n }\n\n pair< int, int > lower_upper_bound(string &t)\n {\n int idx = lower_bound(t);\n int low = idx - 1, high = SA.size();\n t.back()++;\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(lt_substr(t, SA[mid])) low = mid;\n else high = mid;\n }\n t.back()--;\n return (make_pair(idx, high));\n }\n\n void output()\n {\n for(int i = 0; i < size(); i++) {\n cout << i << \": \" << s.substr(SA[i]) << endl;\n }\n }\n};\n\nstruct SuccinctIndexableDictionary\n{\n size_t length;\n size_t blocks;\n vector< unsigned > bit, sum;\n\n SuccinctIndexableDictionary()\n {\n }\n\n SuccinctIndexableDictionary(size_t _length)\n {\n length = _length;\n blocks = (length + 31) >> 5;\n bit.assign(blocks, 0U);\n sum.assign(blocks, 0U);\n }\n\n void set(int k)\n {\n bit[k >> 5] |= 1U << (k & 31);\n }\n\n void build()\n {\n sum[0] = 0U;\n for(int i = 1; i < blocks; i++) {\n sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]);\n }\n }\n\n bool operator[](int k) const\n {\n return (bool((bit[k >> 5] >> (k & 31)) & 1));\n }\n\n int rank(int k)\n {\n return (sum[k >> 5] + __builtin_popcount(bit[k >> 5] & ((1U << (k & 31)) - 1)));\n }\n\n int rank(bool val, int k)\n {\n return (val ? rank(k) : k - rank(k));\n }\n\n int select(bool val, int k)\n {\n if(k < 0 || rank(val, length) <= k) return (-1);\n int low = 0, high = length;\n while(high - low > 1) {\n int mid = (low + high) >> 1;\n if(rank(val, mid) >= k + 1) high = mid;\n else low = mid;\n }\n return (high - 1);\n }\n\n int select(bool val, int i, int l)\n {\n return select(val, i + rank(val, l));\n }\n};\n\ntemplate< class T, int MAXLOG >\nstruct WaveletMatrix\n{\n size_t length;\n SuccinctIndexableDictionary matrix[MAXLOG];\n int zs[MAXLOG];\n int buff1[MAXLOG], buff2[MAXLOG];\n\n void max_dfs(int d, int l, int r, int &k, T val, vector< T > &vs)\n {\n if(l >= r || !k) return;\n if(d == MAXLOG) {\n while(l++ < r && k > 0) vs.push_back(val), k--;\n return;\n }\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n max_dfs(d + 1, lc + zs[d], rc + zs[d], k, 1ULL << (MAXLOG - d - 1) | val, vs);\n max_dfs(d + 1, l - lc, r - rc, k, val, vs);\n }\n\n T max_dfs(int d, int l, int r, T val, T a, T b)\n {\n if(r - l <= 0 || val >= b) return -1;\n if(d == MAXLOG) return val >= a ? val : -1;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n T ret = max_dfs(d + 1, lc + zs[d], rc + zs[d], 1ULL << (MAXLOG - d - 1) | val, a, b);\n if(~ret) return ret;\n return max_dfs(d + 1, l - lc, r - rc, val, a, b);\n }\n\n int freq_dfs(int d, int l, int r, T val, T a, T b)\n {\n if(l == r) return 0;\n if(d == MAXLOG) return (a <= val && val < b) ? r - l : 0;\n T nv = 1ULL << (MAXLOG - d - 1) | val, nnv = ((1ULL << (MAXLOG - d - 1)) - 1) | nv;\n if(nnv < a || b <= val) return 0;\n if(a <= val && nnv < b) return r - l;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n return freq_dfs(d + 1, l - lc, r - rc, val, a, b) +\n freq_dfs(d + 1, lc + zs[d], rc + zs[d], nv, a, b);\n }\n\n void list_dfs(int d, int l, int r, T val, T a, T b, vector< pair< T, int>> &vs)\n {\n if(val >= b || r - l <= 0) return;\n if(d == MAXLOG) {\n if(a <= val) vs.push_back(make_pair(val, r - l));\n return;\n }\n T nv = val | (1LL << (MAXLOG - d - 1)), nnv = nv | (((1LL << (MAXLOG - d - 1)) - 1));\n if(nnv < a) return;\n int lc = matrix[d].rank(1, l), rc = matrix[d].rank(1, r);\n list_dfs(d + 1, l - lc, r - rc, val, a, b, vs);\n list_dfs(d + 1, lc + zs[d], rc + zs[d], nv, a, b, vs);\n }\n\n\n WaveletMatrix(vector< T > data)\n {\n length = data.size();\n vector< T > l(length), r(length);\n for(int depth = 0; depth < MAXLOG; depth++) {\n matrix[depth] = SuccinctIndexableDictionary(length + 1);\n int left = 0, right = 0;\n for(int i = 0; i < length; i++) {\n bool k = (data[i] >> (MAXLOG - depth - 1)) & 1;\n if(k) r[right++] = data[i], matrix[depth].set(i);\n else l[left++] = data[i];\n }\n zs[depth] = left;\n matrix[depth].build();\n swap(l, data);\n for(int i = 0; i < right; i++) data[left + i] = r[i];\n }\n }\n\n T access(int k)\n {\n int ret = 0;\n bool bit;\n for(int depth = 0; depth < MAXLOG; depth++) {\n bit = matrix[depth][k];\n ret = (ret << 1) | bit;\n k = matrix[depth].rank(bit, k) + zs[depth] * bit;\n }\n return (ret);\n }\n\n int rank(T val, int k)\n {\n int l = 0, r = k;\n for(int depth = 0; depth < MAXLOG; depth++) {\n buff1[depth] = l, buff2[depth] = r;\n bool bit = (val >> (MAXLOG - depth - 1)) & 1;\n l = matrix[depth].rank(bit, l) + zs[depth] * bit;\n r = matrix[depth].rank(bit, r) + zs[depth] * bit;\n }\n return (r - l);\n }\n\n int select(T val, int kth)\n {\n rank(val, length);\n for(int depth = MAXLOG - 1; depth >= 0; depth--) {\n bool bit = (val >> (MAXLOG - depth - 1)) & 1;\n kth = matrix[depth].select(bit, kth, buff1[depth]);\n if(kth >= buff2[depth] || kth < 0) return (-1);\n kth -= buff1[depth];\n }\n return (kth);\n }\n\n int select(T val, int k, int l)\n {\n return (select(val, k + rank(val, l)));\n }\n\n\n int quantile(int left, int right, int kth)\n {\n if(right - left <= kth || kth < 0) return (-1);\n T ret = 0;\n for(int depth = 0; depth < MAXLOG; depth++) {\n int l = matrix[depth].rank(1, left);\n int r = matrix[depth].rank(1, right);\n if(r - l > kth) {\n left = l + zs[depth];\n right = r + zs[depth];\n ret |= 1ULL << (MAXLOG - depth - 1);\n } else {\n kth -= r - l;\n left -= l;\n right -= r;\n }\n }\n return (ret);\n }\n\n vector< T > topk(int l, int r, int k)\n {\n if(r - l < k) k = r - l;\n if(k < 0) return (vector< T >());\n vector< T > ret;\n max_dfs(0, l, r, k, 0, ret);\n return (ret);\n }\n\n vector< pair< T, int > > freq_list(int l, int r, T a, T b)\n {\n vector< pair< T, int > > ret;\n list_dfs(0, l, r, 0, a, b, ret);\n return (ret);\n }\n\n vector< pair< int, T > > get_rect(int l, int r, T a, T b)\n {\n vector< pair< T, int > > res = freq_list(l, r, a, b);\n vector< pair< int, T > > ret;\n for(auto &e : res) {\n for(int i = 0; i < e.second; i++) {\n ret.emplace_back(select(e.first, i, l), e.first);\n }\n }\n return (ret);\n }\n\n int rangefreq(int left, int right, T lower, T upper)\n {\n return (freq_dfs(0, left, right, 0, lower, upper));\n }\n};\n\nint main()\n{\n char S[100001], T[100001];\n int Q;\n\n scanf(\"%s %d\", S, &Q);\n SuffixArray sa;\n sa.Build_SA(S);\n vector< int > vs(sa.size());\n for(int i = 0; i < sa.size(); i++) vs[i] = sa[i];\n WaveletMatrix< int, 18 > mat(vs);\n\n for(int i = 0; i < Q; i++) {\n int l, r;\n scanf(\"%d %d %s\", &l, &r, T);\n string X = T;\n auto p = sa.lower_upper_bound(X);\n printf(\"%d\\n\", mat.rangefreq(p.first, p.second, l, r + 1 - (int) X.size() + 1));\n }\n\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 5588, "score_of_the_acc": -0.1047, "final_rank": 2 }, { "submission_id": "aoj_1585_1845791", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nconst int MAX_LEN = 2500000;\nint si[MAX_LEN], is[MAX_LEN], lcp[MAX_LEN];\n\nvoid buildSA(const char *t){\n\tstatic int a[MAX_LEN], b[MAX_LEN];\n\tstatic pair<int, int> p[MAX_LEN];\n\t\n\tint n = strlen(t);\n\t\n\trep(i, n + 1) is[i] = t[i], p[i] = make_pair(is[i], i);\n\tsort(p, p + n + 1);\n\trep(i, n + 1) si[i] = p[i].second;\n\t\n\tfor(int h = 0; ; ){\n\t\trep(i, n){\n\t\t\tint x = si[i + 1], y = si[i];\n\t\t\tb[i + 1] = b[i];\n\t\t\tif(is[x] > is[y] || is[x + h] > is[y + h]) b[i + 1]++;\n\t\t}\n\t\trep(i, n + 1) is[si[i]] = b[i];\n\t\tif(b[n] == n) break;\n\t\th = max(1, h << 1);\n\t\t\n\t\tfor(int k = h; k >= 0; k -= h){\n\t\t\trep(i, n + 1) b[i] = 0;\n\t\t\tb[0] = k;\n\t\t\t\n\t\t\tfor(int i = k; i <= n; i++) b[is[i]]++;\n\t\t\trep(i, n) b[i + 1] += b[i];\n\t\t\tfor(int i = n; i >= 0; i--)\n\t\t\t\ta[--b[si[i] + k > n ? 0 : is[si[i] + k]]] = si[i];\n\t\t\t\n\t\t\tswap(si, a);\n\t\t}\n\t}\n}\nvoid buildLCP(const char *t){\n\tint h = 0, n = strlen(t);\n\t\n\trep(i, n + 1){\n\t\tif(is[i]){\n\t\t\tfor(int j = si[is[i] - 1];\n\t\t\t\tj + h < n && i + h < n && t[j + h] == t[i + h]; h++);\n\t\t\tlcp[is[i]] = h;\n\t\t}\n\t\telse lcp[is[i]] = -1;\n\t\tif(h > 0) h--;\n\t}\n}\n\n//n?????????????????????+1\nint *buildRMQ(int *a, int n){\n\tint logn = 1;\n\tfor (int k = 1; k < n; k *= 2) ++logn;\n\tint *r = new int[n * logn];\n\tint *b = r;\n\tcopy(a, a+n, b);\n\tfor (int k = 1; k < n; k *= 2){\n\t\tcopy(b, b + n, b + n); b += n;\n\t\trep(i, n - k) b[i] = min(b[i], b[i+k]);\n\t}\n\treturn r;\n}\ninline int minimum(int x, int y, int *rmq, int n){\n\tif(y <= x) return inf;\n\tif(x + 1 == y) return rmq[x];\n\tint z = --y - x, k = 31 - __builtin_clz(z);\n\treturn min( rmq[x + n * k], rmq[y + n * k - (1 << k) + 1] );\n}\nint q, l[100000], r[100000];\n\nstruct RangeTree{\n\tvector<vi> dat;\n\tint n;\n\tRangeTree(int *a, int size){\n\t\tfor(n = 1; n < size; n *= 2);\n\t\tdat.resize(2 * n - 1);\n\t\tinit(a, size);\n\t}\n\tvoid init(int *a, int size){\n\t\trep(i, size) dat[i + n - 1].pb(a[i]);\n\t\tfor(int i = n - 2; i >= 0; i--){\n\t\t\tdat[i].resize(dat[i * 2 + 1].size() + dat[i * 2 + 2].size());\n\t\t\tmerge(all(dat[i * 2 + 1]), all(dat[i * 2 + 2]), dat[i].begin());\n\t\t}\n\t}\n\tint query(int a, int b, int k, int l, int r, int v, int V){\n\t\tif(r <= a || b <= l) return 0;\n\t\tif(a <= l && r <= b) return lower_bound(all(dat[k]), V) - lower_bound(all(dat[k]), v);\n\t\tint vl = query(a, b, k * 2 + 1, l, (l + r) / 2, v, V);\n\t\tint vr = query(a, b, k * 2 + 2, (l + r) / 2, r, v, V);\n\t\treturn vl + vr;\n\t}\n\t//[a, b)????????????[v, V)????????°\n\tint query(int a, int b, int v, int V){ return query(a, b, 0, 0, n, v, V); }\n};\n\nint main(){\n\tcin.tie(0); cin.sync_with_stdio(0);\n\tvi pos, len;\n\tstring s;\n\tcin >> s >> q;\n\trep(i, q){\n\t\tstring m; cin >> l[i] >> r[i] >> m; r[i]++;\n\t\ts += '$'; pos.pb(s.size());\n\t\ts += m; len.pb(m.size());\n\t}\n\t//dbg(s);\n\t//rep(i, q) dbg(s.substr(pos[i]));\n\t\n\tint n = s.size();\n\tbuildSA(s.c_str());\n\tbuildLCP(s.c_str());\n\tint *rmq = buildRMQ(lcp, n + 1);\n\t\n\tRangeTree S(si, n + 1);\n\t\n\trep(i, q){\n\t\tint idx = is[pos[i]]; assert(si[idx] == pos[i]);\n\t\tint lo = 0, hi = idx, mid, L, R;\n\t\twhile(lo + 1 < hi){\n\t\t\tmid = (lo + hi) / 2;\n\t\t\tif(minimum(mid + 1, idx + 1, rmq, n + 1) < len[i]) lo = mid;\n\t\t\telse hi = mid;\n\t\t}\n\t\tL = hi;\n\t\t\n\t\tlo = idx; hi = n + 1;\n\t\twhile(lo + 1 < hi){\n\t\t\tmid = (lo + hi) / 2;\n\t\t\tif(minimum(idx + 1, mid + 1, rmq, n + 1) < len[i]) hi = mid;\n\t\t\telse lo = mid;\n\t\t}\n\t\tR = hi;\n\t\t\n\t\t//cerr<<\"idx: \"<<idx<<\" L: \"<<L<<\" R: \"<<R<<\" min:\"<<l[i]<<\" max: \"<<r[i] - len[i]+1<<endl;\n\t\t\n\t\tprintf(\"%d\\n\", S.query(L, R, l[i], r[i] - len[i] + 1));\n\t}\n\t\n\t//rep(i, n+1)cerr<<i<<\" \"<<lcp[i]<<\" \"<<si[i]<<\" \"<<(i<=n?s.substr(si[i]):\"\")<<endl;\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1220, "memory_kb": 422256, "score_of_the_acc": -1.7101, "final_rank": 12 }, { "submission_id": "aoj_1585_1845752", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nconst int MAX_LEN = 2500000;\nint si[MAX_LEN], is[MAX_LEN], lcp[MAX_LEN];\n\nvoid buildSA(char *t){\n\tstatic int a[MAX_LEN], b[MAX_LEN];\n\tstatic pair<int, int> p[MAX_LEN];\n\t\n\tint n = strlen(t);\n\t\n\trep(i, n + 1) is[i] = t[i], p[i] = make_pair(is[i], i);\n\tsort(p, p + n + 1);\n\trep(i, n + 1) si[i] = p[i].second;\n\t\n\tfor(int h = 0; ; ){\n\t\trep(i, n){\n\t\t\tint x = si[i + 1], y = si[i];\n\t\t\tb[i + 1] = b[i];\n\t\t\tif(is[x] > is[y] || is[x + h] > is[y + h]) b[i + 1]++;\n\t\t}\n\t\trep(i, n + 1) is[si[i]] = b[i];\n\t\tif(b[n] == n) break;\n\t\th = max(1, h << 1);\n\t\t\n\t\tfor(int k = h; k >= 0; k -= h){\n\t\t\trep(i, n + 1) b[i] = 0;\n\t\t\tb[0] = k;\n\t\t\t\n\t\t\tfor(int i = k; i <= n; i++) b[is[i]]++;\n\t\t\trep(i, n) b[i + 1] += b[i];\n\t\t\tfor(int i = n; i >= 0; i--)\n\t\t\t\ta[--b[si[i] + k > n ? 0 : is[si[i] + k]]] = si[i];\n\t\t\t\n\t\t\tswap(si, a);\n\t\t}\n\t}\n}\nvoid buildLCP(char *t){\n\tint h = 0, n = strlen(t);\n\t\n\trep(i, n + 1){\n\t\tif(is[i]){\n\t\t\tfor(int j = si[is[i] - 1];\n\t\t\t\tj + h < n && i + h < n && t[j + h] == t[i + h]; h++);\n\t\t\tlcp[is[i]] = h;\n\t\t}\n\t\telse lcp[is[i]] = -1;\n\t\tif(h > 0) h--;\n\t}\n}\n\n//n?????????????????????+1\nint *buildRMQ(int *a, int n){\n\tint logn = 1;\n\tfor (int k = 1; k < n; k *= 2) ++logn;\n\tint *r = new int[n * logn];\n\tint *b = r;\n\tcopy(a, a+n, b);\n\tfor (int k = 1; k < n; k *= 2){\n\t\tcopy(b, b + n, b + n); b += n;\n\t\trep(i, n - k) b[i] = min(b[i], b[i+k]);\n\t}\n\treturn r;\n}\ninline int minimum(int x, int y, int *rmq, int n){\n\tif(--y < x) return inf;\n\tif(x == y) return rmq[x];\n\tint z = y - x, k = 31 - __builtin_clz(z);\n\treturn min( rmq[x + n * k], rmq[y + n * k - (1 << k) + 1] );\n}\n\nchar s[MAX_LEN];\nint q, l[100000], r[100000], pos[100000];\n\ntemplate<class T>struct SegTree{\n\tT *dat;\n\tint n;\n\tSegTree(int size, int *a){\n\t\tfor(n = 1; n < size; n *= 2);\n\t\tdat = new T[2 * n - 1];\n\t\tinit(size, a);\n\t}\n\t~SegTree(){ delete [] dat; }\n\t\n\tvoid init(int sz, int *a){\n\t\trep(i, sz) dat[i + n - 1].pb(a[i]);\n\t\tfor(int i = n - 2; i >= 0; i--){\n\t\t\tdat[i].resize(dat[i * 2 + 1].size() + dat[i * 2 + 2].size());\n\t\t\tmerge(all(dat[i * 2 + 1]), all(dat[i * 2 + 2]), dat[i].begin());\n\t\t}\n\t}\n\tint query(int a, int b, int k, int l, int r, int v, int V){\n\t\tif(r <= a || b <= l) return 0;\n\t\tif(a <= l && r <= b) return lower_bound(all(dat[k]), V) - lower_bound(all(dat[k]), v);\n\t\tint vl = query(a, b, k * 2 + 1, l, (l + r) / 2, v, V);\n\t\tint vr = query(a, b, k * 2 + 2, (l + r) / 2, r, v, V);\n\t\treturn vl + vr;\n\t}\n\tint query(int a, int b, int v, int V){ return query(a, b, 0, 0, n, v, V); }\n};\n\nint main(){\n\tscanf(\"%s%d\", s, &q);\n\tint p = strlen(s); s[p++] = '$';\n\trep(i, q){\n\t\tscanf(\"%d%d%s\", l + i, r + i, s + (pos[i] = p)); r[i]++;\n\t\tp += strlen(s + pos[i]);\n\t\ts[p++] = '$';\n\t}\n\tpos[q] = p;\n\tbuildSA(s);\n\tbuildLCP(s);\n\tint *rmq = buildRMQ(lcp, p + 1);\n\tSegTree<vi> S(p + 1, si);\n\t\n\trep(it, q){\n\t\tint idx = is[pos[it]], len = pos[it + 1] - pos[it] - 1;\n\t\tint lo = 0, hi = idx, mid, L, R;\n\t\t\n\t\twhile(lo + 1 < hi){\n\t\t\tmid = (lo + hi) / 2;\n\t\t\tif(minimum(mid + 1, idx + 1, rmq, p + 1) < len) lo = mid;\n\t\t\telse hi = mid;\n\t\t}\n\t\tL = hi; lo = idx; hi = p + 1;\n\t\twhile(lo + 1 < hi){\n\t\t\tmid = (lo + hi) / 2;\n\t\t\tif(minimum(idx + 1, mid + 1, rmq, p + 1) < len) hi = mid;\n\t\t\telse lo = mid;\n\t\t}\n\t\tR = hi;\n\t\t\n\t\t//cerr<<\"L: \"<<L<<\" R: \"<<R<<\" lit: \"<<l[it]<<\" rit: \"<<r[it]<<\" len: \"<<len<<\" idx: \"<<idx<<endl;\n\t\t\n\t\tprintf(\"%d\\n\", S.query(L, R, l[it], r[it] - len + 1));\n\t}\n\t//rep(i, p + 1) cerr<<i<<\" \"<<lcp[i]<<\" \"<<si[i]<<\" \"<<s + si[i]<<endl;\n\t\n\treturn 0;\n}", "accuracy": 0.671875, "time_ms": 1140, "memory_kb": 421284, "score_of_the_acc": -1.6604, "final_rank": 15 }, { "submission_id": "aoj_1585_1845333", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__LINE__<<\": \"<<#x<<\" = \"<<(x)<<endl\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef pair<int,int> pi;\nconst int inf = (int)1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nconst int MAX_LEN = 2500000;\nint si[MAX_LEN], is[MAX_LEN], lcp[MAX_LEN];\n\nvoid buildSA(char *t){\n\tstatic int a[MAX_LEN], b[MAX_LEN];\n\tstatic pair<int, int> p[MAX_LEN];\n\t\n\tint n = strlen(t);\n\t\n\trep(i, n + 1) is[i] = t[i], p[i] = make_pair(is[i], i);\n\tsort(p, p + n + 1);\n\trep(i, n + 1) si[i] = p[i].second;\n\t\n\tfor(int h = 0; ; ){\n\t\trep(i, n){\n\t\t\tint x = si[i + 1], y = si[i];\n\t\t\tb[i + 1] = b[i];\n\t\t\tif(is[x] > is[y] || is[x + h] > is[y + h]) b[i + 1]++;\n\t\t}\n\t\trep(i, n + 1) is[si[i]] = b[i];\n\t\tif(b[n] == n) break;\n\t\th = max(1, h << 1);\n\t\t\n\t\tfor(int k = h; k >= 0; k -= h){\n\t\t\trep(i, n + 1) b[i] = 0;\n\t\t\tb[0] = k;\n\t\t\t\n\t\t\tfor(int i = k; i <= n; i++) b[is[i]]++;\n\t\t\trep(i, n) b[i + 1] += b[i];\n\t\t\tfor(int i = n; i >= 0; i--)\n\t\t\t\ta[--b[si[i] + k > n ? 0 : is[si[i] + k]]] = si[i];\n\t\t\t\n\t\t\tswap(si, a);\n\t\t}\n\t}\n}\nvoid buildLCP(char *t){\n\tint h = 0, n = strlen(t);\n\t\n\trep(i, n + 1){\n\t\tif(is[i]){\n\t\t\tfor(int j = si[is[i] - 1];\n\t\t\t\tj + h < n && i + h < n && t[j + h] == t[i + h]; h++);\n\t\t\tlcp[is[i]] = h;\n\t\t}\n\t\telse lcp[is[i]] = -1;\n\t\tif(h > 0) h--;\n\t}\n}\n\n//n?????????????????????+1\nint *buildRMQ(int *a, int n){\n\tint logn = 1;\n\tfor (int k = 1; k < n; k *= 2) ++logn;\n\tint *r = new int[n * logn];\n\tint *b = r;\n\tcopy(a, a+n, b);\n\tfor (int k = 1; k < n; k *= 2){\n\t\tcopy(b, b + n, b + n); b += n;\n\t\trep(i, n - k) b[i] = min(b[i], b[i+k]);\n\t}\n\treturn r;\n}\ninline int minimum(int x, int y, int *rmq, int n){\n\tif(--y < x) return inf;\n\tif(x == y) return rmq[x];\n\tint z = y - x, k = 31 - __builtin_clz(z);\n\treturn min( rmq[x + n * k], rmq[y + n * k - (1 << k) + 1] );\n}\n\nchar s[MAX_LEN];\nint q, l[100000], r[100000], pos[100000];\n\ntemplate<class T>struct SegTree{\n\tT *dat;\n\tint n;\n\tSegTree(int size, int *a){\n\t\tfor(n = 1; n < size; n *= 2);\n\t\tdat = new T[2 * n - 1];\n\t\tinit(size, a);\n\t}\n\t~SegTree(){ delete [] dat; }\n\t\n\tvoid init(int sz, int *a){\n\t\trep(i, sz) dat[i + n - 1].pb(a[i]);\n\t\tfor(int i = n - 2; i >= 0; i--){\n\t\t\tdat[i].resize(dat[i * 2 + 1].size() + dat[i * 2 + 2].size());\n\t\t\tmerge(all(dat[i * 2 + 1]), all(dat[i * 2 + 2]), dat[i].begin());\n\t\t}\n\t}\n\tint query(int a, int b, int k, int l, int r, int v, int V){\n\t\tif(r <= a || b <= l) return 0;\n\t\tif(a <= l && r <= b) return lower_bound(all(dat[k]), V) - lower_bound(all(dat[k]), v);\n\t\tint vl = query(a, b, k * 2 + 1, l, (l + r) / 2, v, V);\n\t\tint vr = query(a, b, k * 2 + 2, (l + r) / 2, r, v, V);\n\t\treturn vl + vr;\n\t}\n\tint query(int a, int b, int v, int V){ return query(a, b, 0, 0, n, v, V); }\n};\n\nint main(){\n\tscanf(\"%s%d\", s, &q);\n\tint p = strlen(s); s[p++] = '$';\n\trep(i, q){\n\t\tscanf(\"%d%d%s\", l + i, r + i, s + (pos[i] = p)); r[i]++;\n\t\tp += strlen(s + pos[i]);\n\t\ts[p++] = '$';\n\t}\n\tpos[q] = p;\n\tbuildSA(s);\n\tbuildLCP(s);\n\tint *rmq = buildRMQ(lcp, p + 1);\n\tSegTree<vi> S(p + 1, si);\n\t\n\trep(it, q){\n\t\tint idx = is[pos[it]], len = pos[it + 1] - pos[it] - 1;\n\t\tint lo = -1, hi = idx, mid, L, R;\n\t\t\n\t\twhile(lo + 1 < hi){\n\t\t\tmid = (lo + hi) / 2;\n\t\t\tif(minimum(mid + 1, idx + 1, rmq, p + 1) < len) lo = mid;\n\t\t\telse hi = mid;\n\t\t}\n\t\tL = hi; lo = idx; hi = p + 1;\n\t\twhile(lo + 1 < hi){\n\t\t\tmid = (lo + hi) / 2;\n\t\t\tif(minimum(idx + 1, mid + 1, rmq, p + 1) < len) hi = mid;\n\t\t\telse lo = mid;\n\t\t}\n\t\tR = hi;\n\t\t\n\t\t//cerr<<\"L: \"<<L<<\" R: \"<<R<<\" lit: \"<<l[it]<<\" rit: \"<<r[it]<<\" len: \"<<len<<\" idx: \"<<idx<<endl;\n\t\t\n\t\tprintf(\"%d\\n\", S.query(L, R, l[it], r[it] - len + 1));\n\t}\n\t//rep(i, p + 1) cerr<<i<<\" \"<<lcp[i]<<\" \"<<si[i]<<\" \"<<s + si[i]<<endl;\n\t\n\treturn 0;\n}", "accuracy": 0.671875, "time_ms": 1160, "memory_kb": 421296, "score_of_the_acc": -1.6723, "final_rank": 16 } ]
aoj_1597_cpp
Problem K: Escape of Lappin the Phantom Thief Problem 怪盗ラッパンは宝石を盗みにやってきた。簡単に宝石を手にすることができたが、宝石にはセンサーが仕込まれており、警備ロボットに囲まれてしまった。 警備ロボットは宝石に向かって移動するように仕組まれている。センサーは簡単に外せそうになかったので、宝石を置いて逃走することにした。宝石をできるだけ警備ロボットから遠くに置いて、逃げるための時間稼ぎをすることにした。 敷地は n × m のマスからなる長方形の形をしていて、障害物はない。 k 体の警備ロボットは、それぞれマス( x i , y i )(0 ≤ x i ≤ n −1, 0 ≤ y i ≤ m −1)に配置されており、上下左右のマスに単位時間で移動できる。 警備ロボットは宝石があるマスに最短経路で移動する。 敷地内に自由に宝石を置けるとき、1台以上の警備ロボットが宝石のあるマスに到達するまでにかかる移動時間の最大値を求めよ。 Input n m k x 1 y 1 x 2 y 2 ... x k y k 入力はすべて整数で与えられる。 1行目に n , m , k が空白区切りで与えられる。 2行目以降 k 行に警備ロボットのいるマスの座標( x i , y i )が空白区切りで与えられる。 Constraints 1 ≤ n , m ≤ 5 × 10 4 1 ≤ k ≤ min(10 5 , n × m ) 0 ≤ x i ≤ n −1 0 ≤ y i ≤ m −1 与えられる座標はすべて異なる Output 警備ロボットが到達するまでに、最も時間がかかる場所への移動時間を1行に出力せよ。 Sample Input 1 20 10 1 0 0 Sample Output 1 28 Sample Input 2 20 10 2 0 0 17 5 Sample Output 2 15 Sample Input 3 20 10 3 0 0 17 5 6 9 Sample Output 3 11
[ { "submission_id": "aoj_1597_10240229", "code_snippet": "// AOJ #1597 Escape of Lappin the Phantom Thief\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct Robot { int x, y, u, v; };\n\nstruct Event { int v, L, R, delta; };\n\nstruct SegTree {\n int n;\n vector<int> tree, lazy;\n SegTree(int n) : n(n) {\n tree.assign(4 * n, 0);\n lazy.assign(4 * n, 0);\n }\n void push(int idx, int l, int r) {\n if(lazy[idx] != 0) {\n tree[idx] += lazy[idx];\n if(l != r) {\n lazy[idx*2] += lazy[idx];\n lazy[idx*2+1] += lazy[idx];\n }\n lazy[idx] = 0;\n }\n }\n void update(int idx, int l, int r, int ql, int qr, int val) {\n push(idx, l, r);\n if(qr < l || r < ql) return;\n if(ql <= l && r <= qr) {\n lazy[idx] += val;\n push(idx, l, r);\n return;\n }\n int mid = (l + r) / 2;\n update(idx*2, l, mid, ql, qr, val);\n update(idx*2+1, mid+1, r, ql, qr, val);\n tree[idx] = min(tree[idx*2], tree[idx*2+1]);\n }\n int query(int idx, int l, int r, int ql, int qr) {\n push(idx, l, r);\n if(qr < l || r < ql) return INT_MAX;\n if(ql <= l && r <= qr) return tree[idx];\n int mid = (l + r) / 2;\n return min(query(idx*2, l, mid, ql, qr), query(idx*2+1, mid+1, r, ql, qr));\n }\n};\n\nint n, m, k;\nvector<Robot> robots;\n\nvector<int> evenU, oddU;\n\nvoid getUInterval(int v, int n, int m, int U_max, int &L, int &R) {\n L = max({0, -v, v});\n R = min({U_max, 2*n - 2 - v, 2*m - 2 + v});\n}\n\nbool feasible(int d) {\n if(d == 0) return true;\n int r = d - 1;\n int U_max = n + m - 2;\n int v_min = -(m - 1), v_max = n - 1;\n\n vector<Event> events;\n for (int i = 0; i < k; i++) {\n int rv = robots[i].v;\n int ru = robots[i].u;\n int v_low = max(v_min, rv - r);\n int v_high = min(v_max, rv + r);\n if(v_low > v_high) continue;\n int L = max(0, ru - r);\n int R = min(U_max, ru + r);\n events.push_back({v_low, L, R, +1});\n events.push_back({v_high + 1, L, R, -1});\n }\n sort(events.begin(), events.end(), [](const Event &a, const Event &b){\n return a.v < b.v;\n });\n\n SegTree segEven(evenU.size());\n SegTree segOdd(oddU.size());\n\n auto updateParity = [&](int L, int R, int delta) {\n int li = (int)(lower_bound(evenU.begin(), evenU.end(), L) - evenU.begin());\n int ri = (int)(upper_bound(evenU.begin(), evenU.end(), R) - evenU.begin()) - 1;\n if(li <= ri) {\n segEven.update(1, 0, evenU.size()-1, li, ri, delta);\n }\n li = (int)(lower_bound(oddU.begin(), oddU.end(), L) - oddU.begin());\n ri = (int)(upper_bound(oddU.begin(), oddU.end(), R) - oddU.begin()) - 1;\n if(li <= ri) {\n segOdd.update(1, 0, oddU.size()-1, li, ri, delta);\n }\n };\n\n int evIdx = 0;\n for (int v = v_min; v <= v_max; v++) {\n while(evIdx < (int)events.size() && events[evIdx].v == v) {\n updateParity(events[evIdx].L, events[evIdx].R, events[evIdx].delta);\n evIdx++;\n }\n int L0, R0;\n getUInterval(v, n, m, U_max, L0, R0);\n if(L0 > R0) continue;\n int parity = v & 1;\n int effectiveL = (L0 % 2 == parity ? L0 : L0 + 1);\n int effectiveR = (R0 % 2 == parity ? R0 : R0 - 1);\n if(effectiveL > effectiveR) continue;\n\n if(v % 2 == 0) {\n int li = (int)(lower_bound(evenU.begin(), evenU.end(), effectiveL) - evenU.begin());\n int ri = (int)(upper_bound(evenU.begin(), evenU.end(), effectiveR) - evenU.begin()) - 1;\n if(li <= ri) {\n int mn = segEven.query(1, 0, evenU.size()-1, li, ri);\n if(mn == 0) return true;\n }\n } else {\n int li = (int)(lower_bound(oddU.begin(), oddU.end(), effectiveL) - oddU.begin());\n int ri = (int)(upper_bound(oddU.begin(), oddU.end(), effectiveR) - oddU.begin()) - 1;\n if(li <= ri) {\n int mn = segOdd.query(1, 0, oddU.size()-1, li, ri);\n if(mn == 0) return true;\n }\n }\n }\n return false;\n}\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n cin >> n >> m >> k;\n robots.resize(k);\n for (int i = 0; i < k; i++){\n cin >> robots[i].x >> robots[i].y;\n robots[i].u = robots[i].x + robots[i].y;\n robots[i].v = robots[i].x - robots[i].y;\n }\n int U_max = n + m - 2;\n for (int u = 0; u <= U_max; u++){\n if(u % 2 == 0) evenU.push_back(u);\n else oddU.push_back(u);\n }\n\n int low = 0, high = n + m;\n while(low < high) {\n int mid = (low + high) / 2;\n if(feasible(mid)) low = mid + 1;\n else high = mid;\n }\n cout << low-1 << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 2300, "memory_kb": 12588, "score_of_the_acc": -0.8982, "final_rank": 4 }, { "submission_id": "aoj_1597_3042363", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> P;\nstruct edge{\n int p,l,r,f;\n edge(int p,int l,int r,int f):p(p),l(l),r(r),f(f){}\n bool operator<(const edge e)const{\n return p<e.p;\n }\n};\nvector<P> v;\nvector<edge> e;\nint n,m,k;\nstatic const int MAX_SIZE = 1 << 17; \nint segMin[2][2 * MAX_SIZE - 1], segAdd[2][2 * MAX_SIZE - 1];\nvoid add(int a, int b, int x, int w, int k = 0, int l = 0, int r = MAX_SIZE){\n if(a<0) a=0;\n if(b>MAX_SIZE) b=MAX_SIZE;\n if (r <= a || b <= l) return; \n if (a <= l && r <= b){\n segAdd[w][k] += x;\n return;\n }\n add(a, b, x, w, k * 2 + 1, l, (l + r) / 2);\n add(a, b, x, w, k * 2 + 2, (l + r) / 2, r);\n segMin[w][k] = min(segMin[w][k * 2 + 1] + segAdd[w][k * 2 + 1],\n\t\t segMin[w][k * 2 + 2] + segAdd[w][k * 2 + 2]);\n}\n\nint getMin(int a, int b, int w, int k = 0, int l = 0, int r = MAX_SIZE){\n if(a<0) a=0;\n if(b>MAX_SIZE) b=MAX_SIZE;\n if (r <= a || b <= l) return (MAX_SIZE);\n if (a <= l && r <= b) return (segMin[w][k] + segAdd[w][k]); \n int left = getMin(a, b, w, k * 2 + 1, l, (l + r) / 2);\n int right = getMin(a, b, w, k * 2 + 2, (l + r) / 2, r);\n return (min(left, right) + segAdd[w][k]); \n}\n\nbool check(int d){\n memset(segAdd,0,sizeof(segAdd));\n memset(segMin,0,sizeof(segMin));\n int i,j,a,b=0,g=d-1,p,q;\n e.clear();\n for(i=0;i<v.size();i++){\n int x=v[i].first,y=v[i].second;\n e.push_back(edge(x+y-d,n+y-x-d,n+y-x+d,1));\n e.push_back(edge(x+y+d+2,n+y-x-d,n+y-x+d,-1));\n e.push_back(edge(x+y-g,n+y-x-g,n+y-x+g,1));\n e.push_back(edge(x+y+g+2,n+y-x-g,n+y-x+g,-1));\n }\n //for(i=0;i<e.size();i++) cout << i << \":\" << e[i].p <<\":\"<< e[i].l <<\":\"<< e[i].r <<\":\"<< e[i].f << endl;\n if(n%2){\n for(i=0;i<n+m;i++) add(i,i+1,MAX_SIZE,i%2);\n }else{\n for(i=0;i<n+m;i++) add(i,i+1,MAX_SIZE,(i+1)%2);\n }\n \n sort(e.begin(),e.end());\n for(i=0;i<e.size();i++){\n j=e[i].p;\n for(a=b;a<min(j,n+m-1);a++){\n if(a<n) p=a;\n else p=2*(n-1)-a;\n if(a<m) q=a;\n else q=2*(m-1)-a;\n int l=max(0,n-p),r=min(n+m-1,n+q);\n //cout << a << \":\" << l << \":\" << r << \":\" << getMin(l,r+1,a%2) << endl;\n if(getMin(l,r+1,a%2)==0) return true;\n }\n while(1){\n if(i==e.size()||j!=e[i].p) break;\n add(e[i].l,e[i].r+1,e[i].f,(j+MAX_SIZE)%2);\n i++;\n }\n b=max(0,j);\n i--;\n }\n for(a=b;a<=n+m-2;a++){\n if(a<n) p=a;\n else p=2*(n-1)-a;\n if(a<m) q=a;\n else q=2*(m-1)-a;\n int l=max(0,n-p),r=min(n+m-1,n+q);\n //cout << a << \":\" << l << \":\" << r << \":\" << getMin(l,r+1,a%2) << endl;\n if(getMin(l,r+1,a%2)==0) return true;\n }\n return false;\n}\nint main(){\n int i,j,x,y;\n cin>>n>>m>>k;\n for(i=0;i<k;i++){\n cin >> x >> y;\n v.push_back(P(x,y));\n }\n \n int l=0,r=n+m-2,mid;\n while(l+1<r){\n mid=(l+r)/2;\n //ccout << endl << l << \":\" << r << \":\" << mid << endl;\n if(check(mid)) l=mid;\n else r=mid;\n }\n if(k%n==0&&k/n==m) cout << 0 << endl;\n else cout << l+1 << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 2500, "memory_kb": 16096, "score_of_the_acc": -1.021, "final_rank": 5 }, { "submission_id": "aoj_1597_2558126", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define F first\n#define S second\ntypedef pair<int,int> P;\ntypedef pair<P,int> PP;\nint h,w,n,N=1<<17;\nP a[100000];\n\nclass StarrySkyTree{\npublic:\n int Mi[333333],A[333333];\n void init(){fill(Mi,Mi+333333,0);fill(A,A+333333,0);}\n void add(int a,int b,int x,int k=0,int l=0,int r=N) {\n if(r<=a||b<=l) return;\n if(a<=l&&r<=b){\n A[k]+=x;\n while(k){\n k=(k-1)/2;\n Mi[k]=min(Mi[k*2+1]+A[k*2+1],Mi[k*2+2]+A[k*2+2]);\n }\n return;\n }\n add(a,b,x,k*2+1,l,(l+r)/2);\n add(a,b,x,k*2+2,(l+r)/2,r);\n }\n int getMin(int a,int b,int k=0,int l=0,int r=N) {\n if(r<=a||b<=l)return 1<<29;\n if(a<=l&&r<=b)return Mi[k]+A[k];\n int left=getMin(a,b,k*2+1,l,(l+r)/2),right=getMin(a,b,k*2+2,(l+r)/2,r);\n return min(left,right)+A[k];\n }\n};\n\nStarrySkyTree t[2];\nvector<PP> v[2][111111];\nbool calc(int m) {\n t[0].init(),t[1].init();\n for(int i=0; i<h+w+2; i++) v[0][i].clear(),v[1][i].clear();\n for(int i=0; i<n; i++) {\n int o=(a[i].F+a[i].S)%2;\n int d1=a[i].F+a[i].S-m;\n int d2=a[i].F+a[i].S+m;\n int x=w-(a[i].S-a[i].F+o)/2-1,y=abs(d1)%2;\n d1=max(0,d1),d2=min(h+w,d2);\n x-=(m+y)/2;\n v[0][d1].push_back(PP(P(max(0,x+y),min(h+w,x+m+1)),1));\n v[0][d2+1].push_back(PP(P(max(0,x+y),min(h+w,x+m+1)),-1));\n v[1][d1].push_back(PP(P(max(0,x),min(h+w,x+m+y)),1));\n v[1][d2+1].push_back(PP(P(max(0,x),min(h+w,x+m+y)),-1));\n }\n int z=1,l=w-1,r=w;\n bool f=0,f2=0;\n for(int i=0; i<h+w; i++) {\n for(int k=0; k<2; k++) {\n for(int j=0; j<v[k][i].size(); j++) t[k].add(v[k][i][j].F.F,v[k][i][j].F.S,v[k][i][j].S);\n }\n z=min(z,t[i%2].getMin(l,r));\n if(i>=w-1) f=1;\n if(i>=h-1) f2=1;\n l+=f?(i%2?1:0):(i%2?0:-1);\n r+=f2?(i%2?0:-1):(i%2?1:0);\n }\n return z;\n}\n\nint main() {\n cin >> h >> w >> n;\n for(int i=0; i<n; i++) cin >> a[i].F >> a[i].S;\n int l=-1,r=h+w;\n while(l+1<r) {\n int m=(l+r)/2;\n if(calc(m)) r=m;\n else l=m;\n }\n cout << r << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 2820, "memory_kb": 32148, "score_of_the_acc": -1.371, "final_rank": 6 }, { "submission_id": "aoj_1597_2009069", "code_snippet": "#include <bits/stdc++.h>\n#define N (1<<18)\n#define INF (1e9)\nusing namespace std;\nstruct po{\n int l,r,x;\n po(){};\n po(int l,int r,int x):l(l),r(r),x(x){}\n};\nconst int base=1e5+2;\nint segMin[2][2*N-1], segAdd[2][2*N-1];\nint w,h,m,x[N],y[N];\nvector <po> mp[2][N];\npo LR[N];\n\n//starry sky tree\nvoid add(int a,int b,int x,int k,int l,int r,int idx){\n if(r<=a||b<=l) return;\n if(a<=l&&r<=b){segAdd[idx][k]+=x;return;} \n add(a,b,x,2*k+1,l,(l+r)/2,idx);\n add(a,b,x,2*k+2,(l+r)/2,r,idx);\n int L=segMin[idx][2*k+1]+segAdd[idx][2*k+1];\n int R=segMin[idx][2*k+2]+segAdd[idx][2*k+2];\n segMin[idx][k]=min(L,R);\n}\n\nint getMin(int a,int b,int k,int l,int r,int idx){\n if (r<=a||b<=l) return INF;\n if (a<=l&&r<=b) return segMin[idx][k]+segAdd[idx][k];\n int L=getMin(a,b,2*k+1,l,(l+r)/2,idx);\n int R=getMin(a,b,2*k+2,(l+r)/2,r,idx);\n return min(L,R)+segAdd[idx][k];\n}\n\nvoid mk_LR(){\n for(int i=0;i<h&&w==1;i++)LR[base+i]=po(base,base,i%2);\n if(w==1)return;\n\n LR[base]=po(base,base,0),LR[base+1]=po(base,base+1,1);\n int mn=min(w,h),S,cnt=0;\n for(int i=1;i<mn;i++) cnt+=2*i;\n S=(w*h-cnt)/mn;\n if(w==2)S--;\n for(int i=base+2;;i++){\n int l=LR[i-2].l-1,r=LR[i-2].r+1,x=LR[i-2].x;\n\n if(S==0&&!x)l=LR[i-1].l,r=l+mn-2,S--;\n else if(S==0&&x)l=LR[i-1].l+1,r=LR[i-1].r,S--;\n else if(S<0) l+=2,r-=2;\n if(l>r)break;\n if(r-l+1>mn) l=LR[i-2].l,r=l+mn-1;\n if(S>0&&LR[i-2].r-LR[i-2].l+1==mn)l=LR[i-2].l+1,r=l+mn-1;\n if(r-l+1==mn)S--;\n LR[i]=po(l,r,x);\n }\n}\n\nvoid mk_mp(int d){\n for(int i=0;i<N;i++) mp[0][i].clear(),mp[1][i].clear();\n \n for(int i=0;i<m;i++){\n int idx=y[i]+(w-x[i]-1)+base;\n int c=LR[idx].l+min(y[i],x[i]);\n int a=idx%2,l=c-d/2,r=c+d/2;\n if(d%2==0){\n mp[a][idx-d].push_back(po(l,r,1));\n mp[a][idx+d+1].push_back(po(l,r,-1));\n\n mp[!a][idx-d+1].push_back(po(l+!a,r-a,1));\n mp[!a][idx+d].push_back(po(l+!a,r-a,-1));\n }\n else {\n mp[!a][idx-d].push_back(po(l-a,r+!a,1));\n mp[!a][idx+d+1].push_back(po(l-a,r+!a,-1));\n \n mp[a][idx-d+1].push_back(po(l,r,1));\n mp[a][idx+d].push_back(po(l,r,-1));\n }\n }\n}\n\n\nbool check(int d){\n mk_mp(d);\n memset(segMin,0,sizeof(segMin));\n memset(segAdd,0,sizeof(segAdd));\n for(int i=0;i<N;i++){\n\n for(int k=0;k<2;k++)\n for(int j=0;j<mp[k][i].size();j++)\n\tadd(mp[k][i][j].l,mp[k][i][j].r+1,mp[k][i][j].x,0,0,N,k);\n \n if(LR[i].l&&!getMin(LR[i].l,LR[i].r+1,0,0,N,i%2))return 1;\n }\n return 0; \n}\n\nint search(){\n int L=0,M,R=100001;\n while(L<R){\n M=(L+R)/2;\n if(!check(M)) R=M;\n else L=M+1;\n }\n return L;\n}\n\nint main(){\n cin>>w>>h>>m;\n for(int i=0;i<m;i++)cin>>x[i]>>y[i];\n if(w>h){\n swap(w,h);\n for(int i=0;i<m;i++)swap(x[i],y[i]); \n }\n mk_LR();\n \n cout <<search()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 2830, "memory_kb": 72148, "score_of_the_acc": -1.963, "final_rank": 8 }, { "submission_id": "aoj_1597_2009063", "code_snippet": "#include <bits/stdc++.h>\n#define N (1<<18)\n#define INF (1e9)\nusing namespace std;\nstruct po{\n int l,r,x;\n po(){};\n po(int l,int r,int x):l(l),r(r),x(x){}\n};\nconst int base=1e5+2;\nint segMin[2][2*N-1], segAdd[2][2*N-1];\nint w,h,m,x[N],y[N];\nvector <po> mp[2][N];\npo LR[N];\n\n//starry sky tree\nvoid add(int a,int b,int x,int k,int l,int r,int idx){\n if(r<=a||b<=l) return;\n if(a<=l&&r<=b){segAdd[idx][k]+=x;return;} \n add(a,b,x,2*k+1,l,(l+r)/2,idx);\n add(a,b,x,2*k+2,(l+r)/2,r,idx);\n int L=segMin[idx][2*k+1]+segAdd[idx][2*k+1];\n int R=segMin[idx][2*k+2]+segAdd[idx][2*k+2];\n segMin[idx][k]=min(L,R);\n}\n\nint getMin(int a,int b,int k,int l,int r,int idx){\n if (r<=a||b<=l) return INF;\n if (a<=l&&r<=b) return segMin[idx][k]+segAdd[idx][k];\n int L=getMin(a,b,2*k+1,l,(l+r)/2,idx);\n int R=getMin(a,b,2*k+2,(l+r)/2,r,idx);\n return min(L,R)+segAdd[idx][k];\n}\n\nvoid mk_LR(){\n \n for(int i=0;i<h&&w==1;i++)LR[i+base]=po(base+i,base+i,i%2);\n if(w==1)return;\n LR[base]=po(base,base,0);\n LR[base+1]=po(base,base+1,1);\n int mn=min(w,h),S,cnt=0;\n for(int i=1;i<mn;i++) cnt+=2*i;\n S=(w*h-cnt)/mn;\n if(w==2)S--;\n for(int i=base+2;;i++){\n int l=LR[i-2].l-1,r=LR[i-2].r+1,x=LR[i-2].x;\n\n if(S==0&&!x)l=LR[i-1].l,r=l+mn-2,S--;\n else if(S==0&&x)l=LR[i-1].l+1,r=LR[i-1].r,S--;\n else if(S<0) l+=2,r-=2;\n if(l>r)break;\n if(r-l+1>mn) l=LR[i-2].l,r=l+mn-1;\n if(S>0&&LR[i-2].r-LR[i-2].l+1==mn)l=LR[i-2].l+1,r=l+mn-1;\n if(r-l+1==mn)S--;\n LR[i]=po(l,r,x);\n }\n}\n\nvoid mk_mp(int d){\n for(int i=0;i<N;i++) mp[0][i].clear();\n for(int i=0;i<N;i++) mp[1][i].clear();\n \n for(int i=0;i<m;i++){\n int idx=y[i]+(w-x[i]-1)+base;\n int c=LR[idx].l+min(y[i],x[i]);\n int a=idx%2,b=(idx+d)%2;\n int D=d/2;\n if(d%2==0){\n mp[b][idx-d].push_back(po(c-D,c+D,1));\n mp[b][idx+d+1].push_back(po(c-D,c+D,-1));\n\n mp[!b][idx-d+1].push_back(po(c-D+!a,c+D-a,1));\n mp[!b][idx+d].push_back(po(c-D+!a,c+D-a,-1));\n }\n else {\n mp[b][idx-d].push_back(po(c-D-a,c+D+!a,1));\n mp[b][idx+d+1].push_back(po(c-D-a,c+D+!a,-1));\n \n mp[!b][idx-d+1].push_back(po(c-D,c+D,1));\n mp[!b][idx+d].push_back(po(c-D,c+D,-1));\n }\n }\n}\n\n\nbool check(int d){\n mk_mp(d);\n memset(segMin,0,sizeof(segMin));\n memset(segAdd,0,sizeof(segAdd));\n for(int i=0;i<N;i++){\n int bit=i%2;\n for(int k=0;k<2;k++)\n for(int j=0;j<(int)mp[k][i].size();j++)add(mp[k][i][j].l,mp[k][i][j].r+1,mp[k][i][j].x,0,0,N,k);\n\n\n if(LR[i].l&&!getMin(LR[i].l,LR[i].r+1,0,0,N,bit))return 1;\n }\n return 0; \n}\n\nint search(){\n if(w==1){\n sort(y,y+m);\n int res=max(y[0],h-y[m-1]-1); \n for(int i=1;i<m;i++) res=max(res,(y[i]-y[i-1]-1)/2+(y[i]-y[i-1]-1)%2);\n return res;\n }\n \n int L=0,M,R=100001;\n while(L<R){\n M=(L+R)/2;\n if(!check(M)) R=M;\n else L=M+1;\n }\n return L;\n}\nint main(){\n cin>>w>>h>>m;\n for(int i=0;i<m;i++)cin>>x[i]>>y[i];\n if(w>h){\n swap(w,h);\n for(int i=0;i<m;i++)swap(x[i],y[i]); \n }\n mk_LR();\n cout <<search()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 2820, "memory_kb": 72152, "score_of_the_acc": -1.9595, "final_rank": 7 }, { "submission_id": "aoj_1597_2009062", "code_snippet": "#include <bits/stdc++.h>\n#define N (1<<18)\n#define INF (1e9)\nusing namespace std;\nstruct po{\n int l,r,x;\n po(){};\n po(int l,int r,int x):l(l),r(r),x(x){}\n};\nconst int base=1e5+2;\nint segMin[2][2*N-1], segAdd[2][2*N-1];\nint w,h,m,x[N],y[N];\nvector <po> mp[2][N];\npo LR[N];\n\n//starry sky tree\nvoid add(int a,int b,int x,int k,int l,int r,int idx){\n if(r<=a||b<=l) return;\n if(a<=l&&r<=b){segAdd[idx][k]+=x;return;} \n add(a,b,x,2*k+1,l,(l+r)/2,idx);\n add(a,b,x,2*k+2,(l+r)/2,r,idx);\n int L=segMin[idx][2*k+1]+segAdd[idx][2*k+1];\n int R=segMin[idx][2*k+2]+segAdd[idx][2*k+2];\n segMin[idx][k]=min(L,R);\n}\n\nint getMin(int a,int b,int k,int l,int r,int idx){\n if (r<=a||b<=l) return INF;\n if (a<=l&&r<=b) return segMin[idx][k]+segAdd[idx][k];\n int L=getMin(a,b,2*k+1,l,(l+r)/2,idx);\n int R=getMin(a,b,2*k+2,(l+r)/2,r,idx);\n return min(L,R)+segAdd[idx][k];\n}\n\nvoid mk_LR(){\n \n for(int i=0;i<h&&w==1;i++)LR[i+base]=po(base+i,base+i,i%2);\n if(w==1)return;\n LR[base]=po(base,base,0);\n LR[base+1]=po(base,base+1,1);\n int mn=min(w,h),S,cnt=0;\n for(int i=1;i<mn;i++) cnt+=2*i;\n S=(w*h-cnt)/mn;\n if(w==2)S--;\n for(int i=base+2;;i++){\n int l=LR[i-2].l-1,r=LR[i-2].r+1,x=LR[i-2].x;\n\n if(S==0&&!x)l=LR[i-1].l,r=l+mn-2,S--;\n else if(S==0&&x)l=LR[i-1].l+1,r=LR[i-1].r,S--;\n else if(S<0) l+=2,r-=2;\n if(l>r)break;\n if(r-l+1>mn) l=LR[i-2].l,r=l+mn-1;\n if(S>0&&LR[i-2].r-LR[i-2].l+1==mn)l=LR[i-2].l+1,r=l+mn-1;\n if(r-l+1==mn)S--;\n LR[i]=po(l,r,x);\n }\n}\n\nvoid mk_mp(int d){\n for(int i=0;i<N;i++) mp[0][i].clear();\n for(int i=0;i<N;i++) mp[1][i].clear();\n \n for(int i=0;i<m;i++){\n int idx=y[i]+(w-x[i]-1)+base;\n int c=LR[idx].l+min(y[i],x[i]);\n int a=idx%2,b=(idx+d)%2;\n int D=d/2;\n if(d%2==0){\n mp[b][idx-d].push_back(po(c-D,c+D,1));\n mp[b][idx+d+1].push_back(po(c-D,c+D,-1));\n\n mp[!b][idx-d+1].push_back(po(c-D+!a,c+D-a,1));\n mp[!b][idx+d].push_back(po(c-D+!a,c+D-a,-1));\n }\n else {\n mp[b][idx-d].push_back(po(c-D-a,c+D+!a,1));\n mp[b][idx+d+1].push_back(po(c-D-a,c+D+!a,-1));\n \n mp[!b][idx-d+1].push_back(po(c-D,c+D,1));\n mp[!b][idx+d].push_back(po(c-D,c+D,-1));\n }\n }\n}\n\n\nbool check(int d){\n mk_mp(d);\n for(int i=0;i<N;i++){\n int bit=i%2;\n for(int k=0;k<2;k++)\n for(int j=0;j<(int)mp[k][i].size();j++)add(mp[k][i][j].l,mp[k][i][j].r+1,mp[k][i][j].x,0,0,N,k);\n\n if(LR[i].l&&!getMin(LR[i].l,LR[i].r+1,0,0,N,bit))return 1;\n }\n return 0; \n}\n\nint search(){\n if(w==1){\n sort(y,y+m);\n int res=max(y[0],h-y[m-1]-1); \n for(int i=1;i<m;i++) res=max(res,(y[i]-y[i-1]-1)/2+(y[i]-y[i-1]-1)%2);\n return res;\n }\n \n int L=0,M,R=100001;\n while(L<R){\n M=(L+R)/2;\n if(!check(M)) R=M;\n else L=M+1;\n }\n return L;\n}\nint main(){\n cin>>w>>h>>m;\n for(int i=0;i<m;i++)cin>>x[i]>>y[i];\n if(w>h){\n swap(w,h);\n for(int i=0;i<m;i++)swap(x[i],y[i]); \n }\n mk_LR();\n cout <<search()<<endl;\n return 0;\n}", "accuracy": 0.04, "time_ms": 40, "memory_kb": 16016, "score_of_the_acc": -0.1444, "final_rank": 13 }, { "submission_id": "aoj_1597_2009060", "code_snippet": "#include <bits/stdc++.h>\n#define N (1<<18)\n#define INF (1e9)\nusing namespace std;\nstruct po{\n int l,r,x;\n po(){};\n po(int l,int r,int x):l(l),r(r),x(x){}\n};\nconst int base=1e5+2;\nint segMin[2][2*N-1], segAdd[2][2*N-1];\nint w,h,m,x[N],y[N];\nvector <po> mp[2][N];\npo LR[N];\n\n//starry sky tree\nvoid add(int a,int b,int x,int k,int l,int r,int idx){\n if(r<=a||b<=l) return;\n if(a<=l&&r<=b){segAdd[idx][k]+=x;return;} \n add(a,b,x,2*k+1,l,(l+r)/2,idx);\n add(a,b,x,2*k+2,(l+r)/2,r,idx);\n int L=segMin[idx][2*k+1]+segAdd[idx][2*k+1];\n int R=segMin[idx][2*k+2]+segAdd[idx][2*k+2];\n segMin[idx][k]=min(L,R);\n}\n\nint getMin(int a,int b,int k,int l,int r,int idx){\n if (r<=a||b<=l) return INF;\n if (a<=l&&r<=b) return segMin[idx][k]+segAdd[idx][k];\n int L=getMin(a,b,2*k+1,l,(l+r)/2,idx);\n int R=getMin(a,b,2*k+2,(l+r)/2,r,idx);\n return min(L,R)+segAdd[idx][k];\n}\n\nvoid mk_LR(){\n memset(LR,0,sizeof(LR));\n\n for(int i=0;i<h&&w==1;i++)LR[i+base]=po(base+i,base+i,i%2);\n if(w==1)return;\n LR[base]=po(base,base,0);\n LR[base+1]=po(base,base+1,1);\n int mn=min(w,h),S,cnt=0;\n for(int i=1;i<mn;i++) cnt+=2*i;\n S=(w*h-cnt)/mn;\n if(w==2)S--;\n for(int i=base+2;;i++){\n int l=LR[i-2].l-1,r=LR[i-2].r+1,x=LR[i-2].x;\n\n if(S==0&&!x)l=LR[i-1].l,r=l+mn-2,S--;\n else if(S==0&&x)l=LR[i-1].l+1,r=LR[i-1].r,S--;\n else if(S<0) l+=2,r-=2;\n if(l>r)break;\n if(r-l+1>mn) l=LR[i-2].l,r=l+mn-1;\n if(S>0&&LR[i-2].r-LR[i-2].l+1==mn)l=LR[i-2].l+1,r=l+mn-1;\n if(r-l+1==mn)S--;\n LR[i]=po(l,r,x);\n }\n \n /*\n for(int i=0;i<N;i++){\n if(LR[i].l==0)continue;\n cout <<i-base<<\" \"<<LR[i].l-base<<\" \"<<LR[i].r-base<<endl;\n }\n */\n}\n\nvoid mk_mp(int d){\n for(int i=0;i<N;i++) mp[0][i].clear();\n for(int i=0;i<N;i++) mp[1][i].clear();\n \n for(int i=0;i<m;i++){\n int idx=y[i]+(w-x[i]-1)+base;\n int c=LR[idx].l+min(y[i],x[i]);\n int a=idx%2,b=(idx+d)%2;\n int D=d/2;\n // cout <<idx-base<<\" \"<<c-base<<endl;\n //cout <<\"a=\"<<a<<endl;\n if(d%2==0){\n mp[b][idx-d].push_back(po(c-D,c+D,1));\n mp[b][idx+d+1].push_back(po(c-D,c+D,-1));\n\n mp[!b][idx-d+1].push_back(po(c-D+!a,c+D-a,1));\n mp[!b][idx+d].push_back(po(c-D+!a,c+D-a,-1));\n }\n else {\n mp[b][idx-d].push_back(po(c-D-a,c+D+!a,1));\n mp[b][idx+d+1].push_back(po(c-D-a,c+D+!a,-1));\n \n mp[!b][idx-d+1].push_back(po(c-D,c+D,1));\n mp[!b][idx+d].push_back(po(c-D,c+D,-1));\n }\n }\n /*\n for(int i=0;i<N;i++)\n for(int j=0;j<2;j++)\n if(mp[j][i].size())\n cout <<\"j=\"<<j<<\" \"<<i-base<<\" \"<<mp[j][i][0].l-base<<\" \"<<mp[j][i][0].r-base<<\" \"<<mp[j][i][0].x<<endl;\n */\n}\n\n\nbool check(int d){\n mk_mp(d);\n memset(segMin,0,sizeof(segMin));\n memset(segAdd,0,sizeof(segAdd));\n for(int i=0;i<N;i++){\n int bit=i%2;\n for(int k=0;k<2;k++)\n for(int j=0;j<(int)mp[k][i].size();j++)add(mp[k][i][j].l,mp[k][i][j].r+1,mp[k][i][j].x,0,0,N,k);\n if(LR[i].l&&!getMin(LR[i].l,LR[i].r+1,0,0,N,bit))return 1;\n }\n return 0; \n}\n\nint search(){\n if(w==1){\n sort(y,y+m);\n int res=max(y[0],h-y[m-1]-1); \n for(int i=1;i<m;i++) res=max(res,(y[i]-y[i-1]-1)/2+(y[i]-y[i-1]-1)%2);\n return res;\n }\n \n int L=0,M,R=100001;\n while(L<R){\n M=(L+R)/2;\n if(!check(M)) R=M;\n else L=M+1;\n }\n return L;\n}\n\nvoid debug(){\n int MP[100][100]={};\n cout <<\"P2\"<<endl;\n cout <<100<<\" \"<<100<<endl;\n cout <<15<<endl;\n for(int i=0;i<N;i++)\n if(LR[i].l)\n for(int j=LR[i].l;j<LR[i].r+1;j++) MP[i-base+50][j-base+50]=j%2? 10:15;\n\n /* mk_mp(11);\n for(int i=0;i<N;i++)\n for(int j=0;j<mp[i].size();j++)\n for(int k=mp[i][j].l;k<mp[i][j].r+1;k++)MP[i-base+50-(mp[i][j].x==-1)][k-base+50]=5;\n */ \n\n \n for(int i=99;i>=0;i--){\n for(int j=0;j<100;j++){\n if(j)cout <<\" \";\n cout <<MP[i][j];\n }\n cout <<endl;\n }\n}\n\nint main(){\n cin>>w>>h>>m;\n for(int i=0;i<m;i++)cin>>x[i]>>y[i];\n if(w>h){\n swap(w,h);\n for(int i=0;i<m;i++)swap(x[i],y[i]); \n }\n mk_LR();\n //cout<<check(2)<<endl;\n //debug();\n cout <<search()<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 2850, "memory_kb": 74176, "score_of_the_acc": -2, "final_rank": 9 }, { "submission_id": "aoj_1597_2003258", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef int _loop_int;\n#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)\n#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)\n#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)\n\n#define DEBUG(x) cout<<#x<<\": \"<<x<<endl\n#define DEBUG_VEC(v) cout<<#v<<\":\";REP(__i,v.size())cout<<\" \"<<v[__i];cout<<endl\n#define ALL(a) (a).begin(),(a).end()\n\n#define CHMIN(a,b) a=min((a),(b))\n#define CHMAX(a,b) a=max((a),(b))\n\n// mod\nconst ll MOD = 1000000007ll;\n#define FIX(a) ((a)%MOD+MOD)%MOD\n\n// floating\ntypedef double Real;\nconst Real EPS = 1e-11;\n#define EQ0(x) (abs(x)<EPS)\n#define EQ(a,b) (abs(a-b)<EPS)\ntypedef complex<Real> P;\n\nint n,m,k;\npii rob[125252];\nconst int INF = 830252521;\n\nconst int S_MAX = 1<<17;\n// seg tree\nstruct segtree{\n vi dat,sum;\n segtree(){\n init();\n }\n void init(){\n dat.assign(S_MAX*2-1,0);\n sum.assign(S_MAX*2-1,0);\n }\n void add(int l,int r,int v,int a,int b,int k){\n if(r<=a || b<=l)return;\n if(l<=a && b<=r){\n sum[k] += v;\n dat[k] += v;\n }else{\n int m = (a+b)/2;\n add(l,r,v,a,m,2*k+1);\n add(l,r,v,m,b,2*k+2);\n dat[k] = sum[k] + min(dat[2*k+1],dat[2*k+2]);\n }\n }\n void add(int l,int r,int v){\n add(l,r,v,0,S_MAX,0);\n }\n int segmin(int l,int r,int a,int b,int k){\n if(r<=a || b<=l)return INF;\n if(l<=a && b<=r){\n return dat[k];\n }else{\n int m = (a+b)/2;\n return sum[k] + min(segmin(l,r,a,m,2*k+1),segmin(l,r,m,b,2*k+2));\n }\n }\n int segmin(int l,int r){\n return segmin(l,r,0,S_MAX,0);\n }\n};\n\nconst int C = S_MAX>>1;\nbool check(int w){\n // init\n segtree X,Y;\n X.init();\n Y.init();\n\n int head = 0, tail = 0;\n REP(i,n+m+1){\n // seek and add\n while(head < k){\n if(rob[head].first-w <= i){\n // add\n int x = rob[head].first;\n int y = rob[head].second;\n if((x-w)%2==0){\n X.add((S_MAX+y-w)/2, (S_MAX+y+w)/2+1, 1);\n Y.add((S_MAX+y-w+1)/2,(S_MAX+y+w-1)/2+1,1);\n }else{\n Y.add((S_MAX+y-w)/2, (S_MAX+y+w)/2+1, 1);\n X.add((S_MAX+y-w+1)/2,(S_MAX+y+w-1)/2+1,1);\n }\n head++;\n }else{\n break;\n }\n }\n // check\n int up = i<=(n-1) ? i : 2*(n-1)-i;\n int ud = i<=(m-1) ? -i : i-2*(m-1);\n\n // debug\n // DEBUG(i);\n // printf(\"%*s 0\\n\",2*C,\"\");\n // REP(j,S_MAX)printf(\"%2d\",X.segmin(j,j+1));putchar('\\n');\n // REP(j,S_MAX)printf(\"%2d\",Y.segmin(j,j+1));putchar('\\n');\n // DEBUG(ud);\n // DEBUG(up);\n // if(i%2==0){\n // DEBUG(ud/2);\n // DEBUG(up/2);\n // }else{\n // DEBUG((ud-1)/2);\n // DEBUG((up-1)/2);\n // }\n if(i%2==0){\n if(X.segmin((S_MAX+ud)/2,(S_MAX+up)/2+1)==0){\n return false;\n }\n }else{\n if(Y.segmin((S_MAX+ud-1)/2,(S_MAX+up-1)/2+1)==0){\n return false;\n }\n }\n // seek and remove\n while(tail < k){\n if(rob[tail].first+w <= i){\n // add\n int x = rob[tail].first;\n int y = rob[tail].second;\n if((x-w)%2==0){\n X.add((S_MAX+y-w)/2, (S_MAX+y+w)/2+1, -1);\n Y.add((S_MAX+y-w+1)/2,(S_MAX+y+w-1)/2+1,-1);\n }else{\n Y.add((S_MAX+y-w)/2, (S_MAX+y+w)/2+1, -1);\n X.add((S_MAX+y-w+1)/2,(S_MAX+y+w-1)/2+1,-1);\n }\n tail++;\n }else{\n break;\n }\n }\n }\n return true;\n}\n\nint main(){\n scanf(\"%d%d%d\",&n,&m,&k);\n REP(i,k){\n int x,y;\n scanf(\"%d%d\",&x,&y);\n rob[i] = pii(x+y,x-y);\n }\n sort(rob,rob+k);\n int low = -1, high = n+m+2;\n // DEBUG(check(14));\n while(low+1<high){\n int mid = (low+high)/2;\n if(check(mid)){\n high = mid;\n }else{\n low = mid;\n }\n }\n printf(\"%d\\n\",high);\n return 0;\n}", "accuracy": 1, "time_ms": 1550, "memory_kb": 6200, "score_of_the_acc": -0.5374, "final_rank": 3 }, { "submission_id": "aoj_1597_2002933", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef vector<int>vint;\ntypedef pair<int,int>pint;\ntypedef vector<pint>vpint;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define all(v) (v).begin(),(v).end()\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\n#define pb push_back\n#define fi first\n#define se second\ntemplate<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}\ntemplate<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}\n\nstruct segtree{\n static const int SEG=1<<20;\n vint dat,put;\n segtree():dat(SEG*2),put(SEG*2){}\n inline void push(int k){\n dat[k]+=put[k];\n if(k<SEG-1){\n put[k*2+1]+=put[k];\n put[k*2+2]+=put[k];\n }\n put[k]=0;\n }\n void add(int a,int b,int x=0,int k=0,int l=0,int r=SEG){\n push(k);\n if(r<=a||b<=l)return;\n if(a<=l&&r<=b){\n put[k]+=x;\n push(k);\n return;\n }\n add(a,b,x,k*2+1,l,(l+r)/2);\n add(a,b,x,k*2+2,(l+r)/2,r);\n dat[k]=min(dat[k*2+1],dat[k*2+2]);\n }\n int query(int a,int b,int k=0,int l=0,int r=SEG){\n push(k);\n if(r<=a||b<=l)return 1001001001;\n if(a<=l&&r<=b)return dat[k];\n return min(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));\n }\n};\n\nint H,W,K;\nint X[100000],Y[100000];\n\nvector<tuple<int,int,int>>es[100000];\n\nint base=300000;\nbool C(int L){\n rep(i,100000)es[i].clear();\n rep(i,K){\n int lx=X[i]-L;\n int rx=X[i]+L+1;\n chmax(lx,0ll);\n chmin(rx,100000ll);\n es[lx].pb(make_tuple(Y[i]-L,Y[i]+L+1,1));\n es[rx].pb(make_tuple(Y[i]-L,Y[i]+L+1,-1));\n }\n\n segtree seg;\n\n int ly=1,ry=0;\n for(int x=0;x<H+W-1;x++){\n for(int i=0;i<es[x].size();i++){\n int a,b,k;\n tie(a,b,k)=es[x][i];\n seg.add(a+base,b+base,k);\n }\n if(x<H){\n ly--;ry++;\n }\n else if(x<W){\n ly--;ry--;\n }\n else{\n ly++;ry--;\n }\n if(seg.query(ly+base,ry+base)==0)return false;\n }\n return true;\n}\n\nsigned main(){\n cin>>W>>H>>K;\n rep(i,K)cin>>X[i]>>Y[i];\n\n if(W<H){\n swap(W,H);\n rep(i,K)swap(X[i],Y[i]);\n }\n rep(i,K){\n int x=X[i]+Y[i];\n int y=-X[i]+Y[i];\n X[i]=x;Y[i]=y;\n }\n\n int lb=-1,ub=100010;\n while(ub-lb>1){\n int mid=(ub+lb)/2;\n if(C(mid))ub=mid;\n else lb=mid;\n }\n cout<<ub<<endl;\n return 0;\n}", "accuracy": 0.16, "time_ms": 120, "memory_kb": 38052, "score_of_the_acc": -0.497, "final_rank": 10 }, { "submission_id": "aoj_1597_2002917", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef vector<int>vint;\ntypedef pair<int,int>pint;\ntypedef vector<pint>vpint;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define all(v) (v).begin(),(v).end()\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\n#define pb push_back\n#define fi first\n#define se second\ntemplate<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}\ntemplate<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}\n\nstruct segtree{\n static const int SEG=1<<20;\n vint dat,put;\n segtree():dat(SEG*2),put(SEG*2){}\n inline void push(int k){\n dat[k]+=put[k];\n if(k<SEG-1){\n put[k*2+1]+=put[k];\n put[k*2+2]+=put[k];\n }\n put[k]=0;\n }\n void add(int a,int b,int x=0,int k=0,int l=0,int r=SEG){\n push(k);\n if(r<=a||b<=l)return;\n if(a<=l&&r<=b){\n put[k]+=x;\n push(k);\n return;\n }\n add(a,b,x,k*2+1,l,(l+r)/2);\n add(a,b,x,k*2+2,(l+r)/2,r);\n dat[k]=min(dat[k*2+1],dat[k*2+2]);\n }\n int query(int a,int b,int k=0,int l=0,int r=SEG){\n push(k);\n if(r<=a||b<=l)return 1001001001;\n if(a<=l&&r<=b)return dat[k];\n return min(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));\n }\n};\n\nint H,W,K;\nint X[100000],Y[100000];\n\nvector<tuple<int,int,int>>es[100000];\n\nint base=300000;\nbool C(int L){\n rep(i,100000)es[i].clear();\n rep(i,K){\n int lx=X[i]-L;\n int rx=X[i]+L+1;\n chmax(lx,0ll);\n chmin(rx,100000ll);\n es[lx].pb(make_tuple(Y[i]-L,Y[i]+L+1,1));\n es[rx].pb(make_tuple(Y[i]-L,Y[i]+L+1,-1));\n }\n\n segtree seg;\n\n int ly=1,ry=0;\n for(int x=0;x<H+W-1;x++){\n for(int i=0;i<es[x].size();i++){\n int a,b,k;\n tie(a,b,k)=es[x][i];\n seg.add(a+base,b+base,k);\n }\n if(x<H){\n ly--;ry++;\n }\n else if(x<W){\n ly--;ry--;\n }\n else{\n ly++;ry--;\n }\n if(seg.query(ly+base,ry+base)==0)return false;\n }\n return true;\n}\n\nsigned main(){\n cin>>W>>H>>K;\n rep(i,K)cin>>X[i]>>Y[i];\n\n if(W<H){\n swap(W,H);\n rep(i,K)swap(X[i],Y[i]);\n }\n rep(i,K){\n int x=X[i]+Y[i];\n int y=-X[i]+Y[i];\n X[i]=x;Y[i]=y;\n }\n\n int lb=-1,ub=100000;\n while(ub-lb>1){\n int mid=(ub+lb)/2;\n if(C(mid))ub=mid;\n else lb=mid;\n }\n cout<<ub<<endl;\n return 0;\n}", "accuracy": 0.16, "time_ms": 120, "memory_kb": 38052, "score_of_the_acc": -0.497, "final_rank": 10 }, { "submission_id": "aoj_1597_2002905", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef vector<int>vint;\ntypedef pair<int,int>pint;\ntypedef vector<pint>vpint;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define all(v) (v).begin(),(v).end()\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\n#define pb push_back\n#define fi first\n#define se second\ntemplate<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}\ntemplate<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}\n\nstruct segtree{\n static const int SEG=1<<20;\n vint dat,put;\n segtree():dat(SEG*2),put(SEG*2){}\n inline void push(int k){\n dat[k]+=put[k];\n if(k<SEG-1){\n put[k*2+1]+=put[k];\n put[k*2+2]+=put[k];\n }\n put[k]=0;\n }\n void add(int a,int b,int x=0,int k=0,int l=0,int r=SEG){\n push(k);\n if(r<=a||b<=l)return;\n if(a<=l&&r<=b){\n put[k]+=x;\n push(k);\n return;\n }\n add(a,b,x,k*2+1,l,(l+r)/2);\n add(a,b,x,k*2+2,(l+r)/2,r);\n dat[k]=min(dat[k*2+1],dat[k*2+2]);\n }\n int query(int a,int b,int k=0,int l=0,int r=SEG){\n push(k);\n if(r<=a||b<=l)return 1001001001;\n if(a<=l&&r<=b)return dat[k];\n return min(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));\n }\n};\n\nint H,W,K;\nint X[50000],Y[50000];\n\nvector<tuple<int,int,int>>es[100000];\n\nint base=300000;\nbool C(int L){\n rep(i,100000)es[i].clear();\n rep(i,K){\n int lx=X[i]-L;\n int rx=X[i]+L+1;\n chmax(lx,0ll);\n chmin(rx,100000ll);\n es[lx].pb(make_tuple(Y[i]-L,Y[i]+L+1,1));\n es[rx].pb(make_tuple(Y[i]-L,Y[i]+L+1,-1));\n }\n\n segtree seg;\n\n int ly=1,ry=0;\n for(int x=0;x<H+W-1;x++){\n for(int i=0;i<es[x].size();i++){\n int a,b,k;\n tie(a,b,k)=es[x][i];\n seg.add(a+base,b+base,k);\n }\n if(x<H){\n ly--;ry++;\n }\n else if(x<W){\n ly--;ry--;\n }\n else{\n ly++;ry--;\n }\n if(seg.query(ly+base,ry+base)==0)return false;\n }\n return true;\n}\n\nsigned main(){\n cin>>W>>H>>K;\n rep(i,K)cin>>X[i]>>Y[i];\n\n if(W<H){\n swap(W,H);\n rep(i,K)swap(X[i],Y[i]);\n }\n rep(i,K){\n int x=X[i]+Y[i];\n int y=-X[i]+Y[i];\n X[i]=x;Y[i]=y;\n }\n\n int lb=-1,ub=100000;\n while(ub-lb>1){\n int mid=(ub+lb)/2;\n if(C(mid))ub=mid;\n else lb=mid;\n }\n cout<<ub<<endl;\n return 0;\n}", "accuracy": 0.16, "time_ms": 120, "memory_kb": 38052, "score_of_the_acc": -0.497, "final_rank": 10 }, { "submission_id": "aoj_1597_2002682", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\nstatic const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;\ntypedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;\ntemplate<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }\ntemplate<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }\n\nstruct AddMaxSegmentTree {\n\ttypedef int Val;\n\ttypedef int Add;\n\tvector<Val> nodes;\n\tvector<Val> add;\n\tint n;\n\tvoid init(int minN) {\n\t\tn = 1; while(n < minN) n *= 2;\n\t\tnodes.assign(n * 2, Val());\n\t\tadd.assign(n * 2, Val());\n\t}\n\tVal getRange(int i, int j) {\n\t\tif(i >= j) return -INF;\n\t\treturn getRangeRec(1, i, j, 0, n);\n\t}\n\tvoid addToRange(int i, int j, Add a) {\n\t\tif(i < j) addToRangeRec(1, i, j, a, 0, n);\n\t}\n\tVal getRangeRec(int i, int qL, int qR, int L, int R) {\n\t\tVal a = add[i];\n\t\tif(qL <= L && R <= qR)\n\t\t\treturn nodes[i] + a;\n\t\tif(a != 0) {\n\t\t\tadd[i * 2] += a;\n\t\t\tadd[i * 2 + 1] += a;\n\t\t\tnodes[i] += a;\n\t\t\tadd[i] = 0;\n\t\t}\n\t\tint mid = (L + R) >> 1;\n\t\tVal r = -INF;\n\t\tif(qL < mid) amax(r, getRangeRec(i * 2 + 0, qL, qR, L, mid));\n\t\tif(mid < qR) amax(r, getRangeRec(i * 2 + 1, qL, qR, mid, R));\n\t\tnodes[i] = max(nodes[i * 2] + add[i * 2], nodes[i * 2 + 1] + add[i * 2 + 1]);\n\t\treturn r;\n\t}\n\tvoid addToRangeRec(int i, int qL, int qR, Add a, int L, int R) {\n\t\tif(qL <= L && R <= qR) {\n\t\t\tadd[i] += a;\n\t\t\treturn;\n\t\t}\n\t\tint mid = (L + R) >> 1;\n\t\tif(qL < mid) addToRangeRec(i * 2 + 0, qL, qR, a, L, mid);\n\t\tif(mid < qR) addToRangeRec(i * 2 + 1, qL, qR, a, mid, R);\n\t\tnodes[i] = max(nodes[i * 2] + add[i * 2], nodes[i * 2 + 1] + add[i * 2 + 1]);\n\t}\n};\n\n\nstruct Event {\n\tint L, R;\n\tint sign;\n};\nint main() {\n\tint n; int m; int k;\n\twhile(~scanf(\"%d%d%d\", &n, &m, &k)) {\n\t\tvpii ab(k);\n\t\trep(i, k) {\n\t\t\tint y; int x;\n\t\t\tscanf(\"%d%d\", &y, &x);\n\t\t\tab[i] = { y + x, y - x };\n\t\t}\n\t\tvector<pair<int, int>> border(n + m - 1, make_pair(INF, -INF));\n\t\tauto setborder = [&border](int y, int x) {\n\t\t\tint a = y + x, b = y - x;\n\t\t\tamin(border[a].first, b);\n\t\t\tamax(border[a].second, b);\n\t\t};\n\t\trep(y, n) {\n\t\t\tsetborder(y, 0);\n\t\t\tsetborder(y, m - 1);\n\t\t}\n\t\trep(x, m) {\n\t\t\tsetborder(0, x);\n\t\t\tsetborder(n - 1, x);\n\t\t}\n\t\tint lo = -1, up = n + m - 1;\n\t\twhile(up - lo > 0) {\n\t\t\tint mid = (lo + up + 1) / 2;\n\t\t\tvector<vector<Event>> events(n + m);\n\t\t\tfor(auto p : ab) {\n\t\t\t\tint top = max(0, p.first - mid);\n\t\t\t\tint bot = min(n + m - 1, p.first + 1 + mid);\n\t\t\t\tint L = max(-m, p.second - mid);\n\t\t\t\tint R = min(+n, p.second + 1 + mid);\n\t\t\t\tevents[top].push_back(Event{ L, R, +1 });\n\t\t\t\tevents[bot].push_back(Event{ L, R, -1 });\n\t\t\t}\n\t\t\tAddMaxSegmentTree segt[2];\n\t\t\trep(p, 2) segt[p].init(m + n + 1);\n\t\t\tauto convert = [](int &L, int &R, int p) {\n\t\t\t\tif((L & 1) != p) ++ L;\n\t\t\t\tif((R & 1) != p) ++ R;\n\t\t\t\tL = (L - p) / 2;\n\t\t\t\tR = (R - p) / 2;\n\t\t\t};\n\t\t\tbool ok = true;\n\t\t\trep(a, n + m - 1) {\n\t\t\t\tfor(auto e : events[a]) {\n\t\t\t\t\trep(p, 2) {\n\t\t\t\t\t\tint L = e.L, R = e.R;\n\t\t\t\t\t\tconvert(L, R, p);\n\t\t\t\t\t\tsegt[p].addToRange(L + m, R + m, -e.sign);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tint L = border[a].first, R = border[a].second + 1;\n\t\t\t\tconvert(L, R, a % 2);\n\t\t\t\tif(segt[a % 2].getRange(L + m, R + m) == 0) {\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(ok)\n\t\t\t\tup = mid - 1;\n\t\t\telse\n\t\t\t\tlo = mid;\n\t\t}\n\t\tprintf(\"%d\\n\", lo + 1);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1210, "memory_kb": 14400, "score_of_the_acc": -0.537, "final_rank": 2 }, { "submission_id": "aoj_1597_2002476", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,a,b) for(int i=a;i<b;i++)\n#define REP(i,b) FOR(i,0,b)\n#define PB push_back\n\nusing ll=long long;\nusing pii=pair<int,int>;\nusing vi=vector<int>;\n\nint read(){\n\tint i;\n\tscanf(\"%d\",&i);\n\treturn i;\n}\n\nconst int inf=1000000007;\nstruct Seg{\n\tint mn[1<<18],lz[1<<18],s;\n\tvoid Init(int n){\n\t\ts=1;\n\t\twhile(s<n)s*=2;\n\t\tfill(mn,mn+s*2,0);\n\t\tfill(lz,lz+s*2,0);\n\t}\n\tint NodeMin(int i){\n\t\treturn mn[i]+lz[i];\n\t}\n\tvoid Propagate(int i){\n\t\tlz[i*2]+=lz[i];\n\t\tlz[i*2+1]+=lz[i];\n\t\tmn[i]+=lz[i];\n\t\tlz[i]=0;\n\t}\n\tvoid Add(int b,int e,int l,int r,int i,int v){\n\t\tif(e<=l||r<=b)\n\t\t\treturn;\n\t\tif(b<=l&&r<=e){\n\t\t\tlz[i]+=v;\n\t\t\treturn;\n\t\t}\n\t\tPropagate(i);\n\t\tAdd(b,e,l,(l+r)/2,i*2,v);\n\t\tAdd(b,e,(l+r)/2,r,i*2+1,v);\n\t\tmn[i]=min(NodeMin(i*2),NodeMin(i*2+1));\n\t}\n\tvoid Add(int b,int e,int v){\n\t\tAdd(b,e,0,s,1,v);\n\t}\n\tint Get(int b,int e,int l,int r,int i){\n\t\tif(e<=l||r<=b)\n\t\t\treturn inf;\n\t\tif(b<=l&&r<=e)\n\t\t\treturn NodeMin(i);\n\t\tPropagate(i);\n\t\treturn min(Get(b,e,l,(l+r)/2,i*2),Get(b,e,(l+r)/2,r,i*2+1));\n\t}\n\tint Get(int b,int e){\n\t\treturn Get(b,e,0,s,1);\n\t}\n} seg;\n\nconst int Nmax=334893;\nstruct Pos{\n\tint x,y;\n} ps[Nmax];\nint n,m,k;\n\nstruct Query{\n\tint y,b,e,v;\n\tbool operator<(const Query& rhs)const{\n\t\treturn y<rhs.y;\n\t}\n} qs[Nmax];\n\n\npii GetRange(int y){\n\tint b=abs(y);\n\tint e=(n-1)+(m-1)-abs(y-((m-1)-(n-1)))+1;\n\treturn pii(b,e);\n}\n\nbool Solve(int s){\n\tREP(i,k){\n\t\tqs[i*2]=Query{ps[i].y-s,max(0,ps[i].x-s),min(n+m,ps[i].x+s+1),1};\n\t\tqs[i*2+1]=Query{ps[i].y+s+1,max(0,ps[i].x-s),min(n+m,ps[i].x+s+1),-1};\n\t}\n\tqs[k*2]=Query{inf,0,0,0};\n\tsort(qs,qs+2*k+1);\n\t//for even\n\t{\n\t\tauto modify=[](int& v){\n\t\t\tif(v&1)v++;\n\t\t};\n\t\tseg.Init(n+m);\n\t\tint lastY=-(n-1);\n\t\tmodify(lastY);\n\t\tREP(i,2*k+1){\n\t\t\tQuery q=qs[i];\n\t\t\tmodify(q.y);\n\t\t\tmodify(q.b);\n\t\t\tmodify(q.e);\n\t\t\tif(lastY<q.y){\n\t\t\t\tpii u=GetRange(lastY),b=GetRange(q.y-1);\n\t\t\t\tif(lastY<=0&&0<=q.y-1)\n\t\t\t\t\tu.first=0;\n\t\t\t\tif(lastY<=(m-1)-(n-1)&&(m-1)-(n-1)<=q.y-1)\n\t\t\t\t\tu.second=(m-1)+(n-1)+1;\n\t\t\t\tmodify(u.first);\n\t\t\t\tmodify(u.second);\n\t\t\t\tmodify(b.first);\n\t\t\t\tmodify(b.second);\n\t\t\t\tif(seg.Get(min(u.first,b.first)/2,max(u.second,b.second)/2)==0)\n\t\t\t\t\treturn true;\n\t\t\t}\n\t\t\tseg.Add(q.b/2,q.e/2,q.v);\n\t\t\tlastY=q.y;\n\t\t}\n\t}\n\t//for odd\n\t{\n\t\tauto modify=[](int& v){\n\t\t\tif(!(v&1))v++;\n\t\t};\n\t\tseg.Init(n+m);\n\t\tint lastY=-(n-1);\n\t\tmodify(lastY);\n\t\tREP(i,2*k+1){\n\t\t\tQuery q=qs[i];\n\t\t\tmodify(q.y);\n\t\t\tmodify(q.b);\n\t\t\tmodify(q.e);\n\t\t\tif(lastY<q.y){\n\t\t\t\tpii u=GetRange(lastY),b=GetRange(q.y-1);\n\t\t\t\tif(lastY<=0&&0<=q.y-1)\n\t\t\t\t\tu.first=0;\n\t\t\t\tif(lastY<=(m-1)-(n-1)&&(m-1)-(n-1)<=q.y-1)\n\t\t\t\t\tu.second=(m-1)+(n-1)+1;\n\t\t\t\tmodify(u.first);\n\t\t\t\tmodify(u.second);\n\t\t\t\tmodify(b.first);\n\t\t\t\tmodify(b.second);\n\t\t\t\tif(seg.Get(min(u.first,b.first)/2,max(u.second,b.second)/2)==0)\n\t\t\t\t\treturn true;\n\t\t\t}\n\t\t\tseg.Add(q.b/2,q.e/2,q.v);\n\t\t\tlastY=q.y;\n\t\t}\n\t}\n\treturn false;\n}\n\nint main(){\n\tn=read(),m=read(),k=read();\n\tREP(i,k){\n\t\tint x=read(),y=read();\n\t\tps[i]=Pos{x+y,y-x};\n\t}\n\tint l=-1,r=n+m;\n\twhile(r-l>1){\n\t\tint mid=(r+l)/2;\n\t\tif(Solve(mid))\n\t\t\tl=mid;\n\t\telse\n\t\t\tr=mid;\n\t}\n\tcout<<r<<endl;\n}", "accuracy": 1, "time_ms": 1340, "memory_kb": 9196, "score_of_the_acc": -0.5067, "final_rank": 1 } ]
aoj_1594_cpp
Problem H: Hogemon Get Problem がっちょ君は人気のゲームHogemon Getに熱中している。がっちょ君が住んでいる会津国はそれぞれ1から N の番号がついている N 個の町からなる。また、会津国には M 本の道があり、すべての道は異なる2つの町を結んでいる。がっちょ君は道を双方向に移動することができるが、道以外を通って、ある町から別の町に行くことはできない。 Hogemon Getでは、町 i でボールを d i 個入手することができる。ただし、ある町で再びボールを入手するためには、最後にその町でボールを入手してから15分以上経過している必要がある。なお、がっちょ君は町1、町 N を含むすべての町を何度でも訪れることができる。 がっちょ君は最初、町1にいて、町 N に R 分以内で移動しなければならない。つまり、 R 分後に町 N にいる必要がある。がっちょ君は移動の際に、最大でいくつのボールを入手することができるだろうか。 Input 入力は以下の形式で与えられる。 N M R d 1 d 2 ... d N a 1 b 1 c 1 a 2 b 2 c 2 ... a M b M c M 入力はすべて整数である。 1行目に町の個数 N ,道の本数 M ,制限時間 R が空白区切りで与えられる。 2行目に町 i ( i =1,2,..., N )に訪れることで入手することができるボールの個数 d i が空白区切りで与えられる。 3行目から M +2行目に道 j ( j =1,2,..., M )の情報 a j , b j , c j が空白区切りで与えられる。 j 番目の道は町 a j と町 b j の間を c j 分で移動できることを表す。 Constraints 3 ≤ N ≤ 30 N -1 ≤ M ≤ min( N ×( N -1)/2, 300) 10 ≤ R ≤ 1000 0 ≤ d i ≤ 10 d 1 = d N = 0 1 ≤ a j < b j ≤ N 5 ≤ c j ≤ 100 町1から町 N へは R 分以内で移動できることが保証されている ある2つの町の組に対して2本以上の道があることはない Output 町1から町 N へ R 分以内に移動するまでに入手することができる最大のボールの個数を1行で出力せよ。 Sample Input 1 5 4 40 0 1 1 1 0 1 2 5 2 3 5 3 4 5 4 5 5 Sample Output 1 6 経過時間(分) 町の番号 ボールの数(個) 0 1 0 5 2 1 10 3 2 15 4 3 20 3 3 25 2 4 30 3 5 35 4 6 40 5 6 ※経過時間20分の町3では最後に町3でボールを入手してから15分経過していないのでボールを入手することができない。 Sample Input 2 4 3 100 0 3 1 0 1 2 5 2 3 30 3 4 5 Sample Output 2 16 経過時間(分) 町の番号 ボールの数(個) 0 1 0 5 2 3 20 2 6 35 2 9 50 2 12 65 2 15 95 3 16 100 4 16 Sample Input 3 5 4 50 0 1 1 10 0 1 2 10 2 3 10 2 4 10 4 5 10 Sample Output 3 22
[ { "submission_id": "aoj_1594_10501828", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int inf = 1e9;\n\nint main() {\n\n ios_base::sync_with_stdio(0);\n cin.tie(0), cout.tie(0); \n\n int n, m, R;\n cin >> n >> m >> R;\n\n vector<int> d(n);\n for (auto& x : d) cin >> x;\n\n vector<vector<int>> w(n, vector<int>(n, inf));\n for (int i = 0; i < n; ++i) w[i][i] = 0;\n\n for (int u, v, c, i = 0; i < m; ++i) {\n cin >> u >> v >> c;\n --u, --v;\n w[u][v] = min(w[u][v], c);\n w[v][u] = min(w[v][u], c);\n }\n\n for (int k = 0; k < n; ++k) {\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n w[i][j] = min(w[i][j], w[i][k] + w[k][j]);\n }\n }\n }\n\n vector<vector<vector<vector<int>>>> dp(R + 1, vector<vector<vector<int>>>(n, vector<vector<int>>(n + 1, vector<int>(2, -inf))));\n\n int res = 0;\n dp[0][0][n][0] = 0;\n for (int t = 0; t <= R; ++t) {\n for (int last = 0; last < n; ++last) {\n for (int pre_last = 0; pre_last <= n; ++pre_last) {\n for (int st : {0, 1}) {\n if (last == pre_last) continue;\n if (dp[t][last][pre_last][st] < 0) continue;\n\n {\n int nt = t + w[last][n - 1];\n if (nt <= R) res = max(res, dp[t][last][pre_last][st]);\n }\n\n for (int to = 0; to < n; ++to) {\n if (to == last) {\n int nt = t + 15;\n if (nt <= R) {\n dp[nt][last][pre_last][0] = max(dp[nt][last][pre_last][0], dp[t][last][pre_last][st] + d[last]);\n }\n } else if (to == pre_last) {\n int nt, n_st;\n if (st == 0) {\n if (2 * w[last][to] >= 15) nt = t + w[last][to], n_st = 0;\n else nt = t + 15 - w[last][to], n_st = 1;\n } else {\n nt = t + w[last][to];\n n_st = 0;\n }\n if (nt <= R) {\n dp[nt][pre_last][last][n_st] = max(dp[nt][pre_last][last][n_st], dp[t][last][pre_last][st] + d[pre_last]);\n }\n } else {\n int nt = t + w[last][to];\n if (nt <= R) {\n dp[nt][to][last][0] = max(dp[nt][to][last][0], dp[t][last][pre_last][st] + d[to]);\n }\n }\n }\n }\n }\n }\n }\n\n cout << res << \"\\n\";\n\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 55416, "score_of_the_acc": -0.4089, "final_rank": 5 }, { "submission_id": "aoj_1594_10501816", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int inf = 1e9;\n\nint main() {\n\n#ifdef LOCAL\n freopen(\"input.txt\", \"r\", stdin);\n#endif\n\n ios_base::sync_with_stdio(0);\n cin.tie(0), cout.tie(0); \n\n int n, m, R;\n cin >> n >> m >> R;\n\n vector<int> d(n);\n for (auto& x : d) cin >> x;\n\n vector<vector<int>> w(n, vector<int>(n, inf));\n for (int i = 0; i < n; ++i) w[i][i] = 0;\n\n for (int u, v, c, i = 0; i < m; ++i) {\n cin >> u >> v >> c;\n --u, --v;\n w[u][v] = min(w[u][v], c);\n w[v][u] = min(w[v][u], c);\n }\n\n for (int k = 0; k < n; ++k) {\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n w[i][j] = min(w[i][j], w[i][k] + w[k][j]);\n }\n }\n }\n\n vector<vector<vector<vector<int>>>> dp(R + 1, vector<vector<vector<int>>>(n, vector<vector<int>>(n + 1, vector<int>(2, -inf))));\n\n int res = 0;\n dp[0][0][n][0] = 0;\n for (int t = 0; t <= R; ++t) {\n for (int last = 0; last < n; ++last) {\n for (int pre_last = 0; pre_last <= n; ++pre_last) {\n for (int st : {0, 1}) {\n if (last == pre_last) continue;\n if (dp[t][last][pre_last][st] < 0) continue;\n\n {\n int nt = t + w[last][n - 1];\n if (nt <= R) res = max(res, dp[t][last][pre_last][st]);\n }\n\n for (int to = 0; to < n; ++to) {\n if (to == last) {\n int nt = t + 15;\n if (nt <= R) {\n dp[nt][last][pre_last][0] = max(dp[nt][last][pre_last][0], dp[t][last][pre_last][st] + d[last]);\n }\n } else if (to == pre_last) {\n int nt, n_st;\n if (st == 0) {\n if (2 * w[last][to] >= 15) nt = t + w[last][to], n_st = 0;\n else nt = t + 15 - w[last][to], n_st = 1;\n } else {\n nt = t + w[last][to];\n n_st = 0;\n }\n if (nt <= R) {\n dp[nt][pre_last][last][n_st] = max(dp[nt][pre_last][last][n_st], dp[t][last][pre_last][st] + d[pre_last]);\n }\n } else {\n int nt = t + w[last][to];\n if (nt <= R) {\n dp[nt][to][last][0] = max(dp[nt][to][last][0], dp[t][last][pre_last][st] + d[to]);\n }\n }\n }\n }\n }\n }\n }\n\n cout << res << \"\\n\";\n\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 55348, "score_of_the_acc": -0.4084, "final_rank": 4 }, { "submission_id": "aoj_1594_6063298", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int ull;\ntypedef pair<int, int> pil;\nconst ll ll_INF = 0x3f3f3f3f3f3f3f3f;\nconst ll ll_MAX = 0x7fffffffffffffff;\nconst int int_INF = 0x3f3f3f3f;\nconst int int_MAX = 0x7fffffff;\nconst double EPS = 1e-7;\n#define R register\n#define _max(a, b) ((a) > (b) ? (a) : (b))\n#define _min(a, b) ((a) < (b) ? (a) : (b))\n#define _abs(a) ((a) > 0 ? (a) : -(a))\n#define _swap(a, b) ((a) ^= (b) ^= (a) ^= (b))\n#define _eql(x, y) (_abs((x) - (y)) < EPS)\nconst int MX = 100;\nstruct Node {\n int v, val;\n int _next;\n} e[MX * MX * 2];\nint n, m, r;\nint head[MX], a[MX], cnt = 0;\nvoid addedge(int u, int v, int val) {\n e[++cnt].v = v;\n e[cnt].val = val;\n e[cnt]._next = head[u];\n head[u] = cnt;\n}\nint ready[MX] = {0}, have[MX], ans = 0;\nvoid DFS(int u, int now, int res) {\n if(now > r)\n return;\n if(u == n - 1) {\n ans = _max(ans, res);\n return;\n }\n if(ready[u] <= now) {\n int ori = ready[u];\n res += a[u], ready[u] = now + 15;\n for(int i = head[u]; i; i = e[i]._next) {\n DFS(e[i].v, now + e[i].val, res);\n }\n ready[u] = ori;\n }\n else {\n ready[u] += 15;\n DFS(res, ready[u], res + a[u]);\n ready[u] -= 15;\n for(int i = head[u]; i; i = e[i]._next) {\n DFS(e[i].v, now + e[i].val, res);\n }\n }\n}\nvoid Init() {\n cin >> n >> m >> r;\n int u, v, val;\n for(int i = 0; i < n; i++) {\n cin >> a[i];\n addedge(i, i, 15);\n // a[i + n] = a[i];\n // if(a[i]) {\n // addedge(i, i + n, 15);\n // addedge(i + n, i, 0);\n // }\n }\n for(int i = 0; i < m; i++) {\n cin >> u >> v >> val;\n u--, v--;\n addedge(u, v, val);\n addedge(v, u, val);\n }\n}\nvoid solve() {\n DFS(0, 0, 0);\n cout << ans<<endl;\n}\nint main() {\n std::ios::sync_with_stdio(false);\n#ifdef LOCAL\n freopen(\"data.in\", \"r\", stdin);\n freopen(\"data.out\", \"w\", stdout);\n#endif\n int t = 1;\n while(t--) {\n Init();\n solve();\n }\n return 0;\n}", "accuracy": 0.04838709677419355, "time_ms": 590, "memory_kb": 3180, "score_of_the_acc": -0.3204, "final_rank": 13 }, { "submission_id": "aoj_1594_3811776", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1594.cc: Hogemon Get\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 30;\nconst int MAX_R = 1000;\nconst int DC = 15;\nconst int INF = 1 << 29;\n\n/* typedef */\n\nstruct Stat {\n int d, lvs[MAX_N];\n Stat(): d(-1) { fill(lvs, lvs + MAX_N, -INF); }\n Stat(int _d): d(_d) { fill(lvs, lvs + MAX_N, -INF); }\n\n bool operator<(const Stat &s) const {\n if (d != s.d) return (d < s.d);\n for (int i = 0; i < MAX_N; i++)\n if (lvs[i] != s.lvs[i]) return (lvs[i] > s.lvs[i]);\n return false;\n }\n};\n\n/* global variables */\n\nint ds[MAX_N], dts[MAX_N][MAX_N];\nStat dp[MAX_R + 1][MAX_N][MAX_N];\n\n/* subroutines */\n\ninline void setmin(int &a, int b) { if (a > b) a = b; }\ninline void setmax(int &a, int b) { if (a < b) a = b; }\n\ninline void setdp(int vi, int vj, int vk, int ui, int uj, int uk,\n\t\t int d, int l = -1) {\n Stat udp = dp[ui][uj][uk];\n udp.d = d;\n if (l >= 0) udp.lvs[vj] = l;\n if (dp[vi][vj][vk] < udp) dp[vi][vj][vk] = udp;\n}\n\n/* main */\n\nint main() {\n int n, m, r;\n scanf(\"%d%d%d\", &n, &m, &r);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", ds + i);\n fill(dts[i], dts[i] + n, INF);\n dts[i][i] = 0;\n }\n\n for (int i = 0; i < m; i++) {\n int a, b, c;\n scanf(\"%d%d%d\", &a, &b, &c);\n a--, b--;\n dts[a][b] = dts[b][a] = c;\n }\n\n for (int k = 0; k < n; k++)\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n\tsetmin(dts[i][j], dts[i][k] + dts[k][j]);\n\n dp[0][0][0].d = ds[0];\n dp[0][0][0].lvs[0] = 0;\n\n for (int i = 0; i <= r; i++)\n for (int j = 0; j < n; j++)\n for (int k = 0; k < n; k++) {\n\tStat &udp = dp[i][j][k];\n\tif (udp.d >= 0) {\n\t //printf(\"dp[%d][%d][%d]=%d:\", i, j, k, dp[i][j][k]);\n\t //for (int l = 0; l < n; l++) printf(\" %d\", lvu[l]); putchar('\\n');\n\n\t if (i < r) {\n\t setdp(i + 1, j, k, i, j, k, udp.d);\n\t if (j == k && i + DC <= r)\n\t setdp(i + DC, j, k, i, j, k, udp.d + ds[j], i + DC);\n\t }\n\n\t for (int vj = 0; vj < n; vj++) {\n\t int vi = i + dts[j][vj];\n\t if (vi > i && vi <= r) {\n\t setdp(vi, vj, k, i, j, k, udp.d);\n\t if (udp.lvs[vj] + DC <= vi)\n\t\tsetdp(vi, vj, vj, i, j, k, udp.d + ds[vj], vi);\n\t }\n\t }\n\t}\n }\n\n int maxd = 0;\n for (int i = 0; i <= r; i++)\n for (int k = 0; k < n; k++)\n setmax(maxd, dp[i][n - 1][k].d);\n\n printf(\"%d\\n\", maxd);\n return 0;\n}", "accuracy": 0.14516129032258066, "time_ms": 110, "memory_kb": 112312, "score_of_the_acc": -0.7825, "final_rank": 11 }, { "submission_id": "aoj_1594_3811529", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1594.cc: Hogemon Get\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 30;\nconst int MAX_R = 1000;\nconst int DC = 15;\nconst int INF = 1 << 30;\n\n/* typedef */\n\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\n\nstruct Stat {\n int d, i, j;\n Stat() {}\n Stat(int _d, int _i, int _j): d(_d), i(_i), j(_j) {}\n bool operator<(const Stat &s) const { return d < s.d; }\n};\n\n/* global variables */\n\nint ds[MAX_N], dp[MAX_N][MAX_R + 1], lvs[MAX_N][MAX_R + 1][MAX_N];\nvpii nbrs[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n, m, r;\n scanf(\"%d%d%d\", &n, &m, &r);\n\n for (int i = 0; i < n; i++) scanf(\"%d\", ds + i);\n\n for (int i = 0; i < m; i++) {\n int a, b, c;\n scanf(\"%d%d%d\", &a, &b, &c);\n a--, b--;\n nbrs[a].push_back(pii(b, c));\n nbrs[b].push_back(pii(a, c));\n }\n\n memset(dp, -1, sizeof(dp));\n fill(lvs[0][0], lvs[0][0] + n, -INF);\n lvs[0][0][0] = 0;\n dp[0][0] = ds[0];\n\n priority_queue<Stat> q;\n q.push(Stat(ds[0], 0, 0));\n\n while (! q.empty()) {\n Stat u = q.top(); q.pop();\n if (u.d != dp[u.i][u.j]) continue;\n\n vpii &nbru = nbrs[u.i];\n int *lvu = lvs[u.i][u.j];\n for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {\n int &vi = vit->first, vj = u.j + vit->second;\n if (vj <= r) {\n\tif (lvu[vi] + DC <= vj) {\n\t int vd = u.d + ds[vi];\n\t if (dp[vi][vj] < vd) {\n\t dp[vi][vj] = vd;\n\t memcpy(lvs[vi][vj], lvu, sizeof(lvs[vi][vj]));\n\t lvs[vi][vj][vi] = vj;\n\t q.push(Stat(vd, vi, vj));\n\t }\n\t}\n\telse {\n\t if (dp[vi][vj] < u.d) {\n\t dp[vi][vj] = u.d;\n\t memcpy(lvs[vi][vj], lvu, sizeof(lvs[vi][vj]));\n\t q.push(Stat(u.d, vi, vj));\n\t }\n\n\t if (lvu[vi] + DC <= r) {\n\t vj = lvu[vi] + DC;\n\t int vd = u.d + ds[vi];\n\t if (dp[vi][vj] < vd) {\n\t dp[vi][vj] = vd;\n\t memcpy(lvs[vi][vj], lvu, sizeof(lvs[vi][vj]));\n\t lvs[vi][vj][vi] = vj;\n\t q.push(Stat(vd, vi, vj));\n\t }\n\t }\n\t}\n }\n }\n\n if (lvu[u.i] + DC <= r) {\n int vj = lvu[u.i] + DC;\n int vd = u.d + ds[u.i];\n if (dp[u.i][vj] < vd) {\n\tdp[u.i][vj] = vd;\n\tmemcpy(lvs[u.i][vj], lvu, sizeof(lvs[u.i][vj]));\n\tlvs[u.i][vj][u.i] = vj;\n\tq.push(Stat(vd, u.i, vj));\n }\n }\n }\n\n int maxd = 0;\n for (int j = 0; j <= r; j++)\n if (maxd < dp[n - 1][j]) maxd = dp[n - 1][j];\n\n printf(\"%d\\n\", maxd);\n return 0;\n}", "accuracy": 0.27419354838709675, "time_ms": 10, "memory_kb": 4928, "score_of_the_acc": -0.0116, "final_rank": 10 }, { "submission_id": "aoj_1594_2558315", "code_snippet": "#include <iostream>\n#include <vector>\n#include <cstring>\n\nconstexpr int MAX_N = 30;\nconstexpr int MAX_R = 1000;\nconstexpr int INF = (1 << 29);\n\ntemplate<class T>\nusing vec = std::vector<T>;\n\nvoid update(int &l, int r)\n{\n if (l < r) l = r;\n}\n\nint main()\n{\n int N, M, R;\n std::cin >> N >> M >> R;\n vec<int> ball(N);\n for (int i = 0; i < N; i++) {\n std::cin >> ball[i];\n }\n int d[MAX_N][MAX_N];\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n d[i][j] = (i != j) * INF;\n }\n }\n \n int a, b, c;\n for (int i = 0; i < M; i++) {\n std::cin >> a >> b >> c; a--; b--;\n d[a][b] = d[b][a] = c;\n }\n\n for (int k = 0; k < N; k++) {\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]);\n }\n }\n }\n \n int res = 0;\n vec<vec<vec<int>>> dp(MAX_R + 1, vec<vec<int>>(MAX_N, vec<int>(MAX_N, -1)));\n dp[0][0][0] = 0; \n \n for (int i = 0; i <= R; i++) {\n for (int prev = 0; prev < N; prev++) {\n for (int curr = 0; curr < N; curr++) { \n if (dp[i][curr][prev] == -1 || i + d[curr][N - 1] > R) {\n continue;\n }\n \n if (i + 15 <= R) {\n update(dp[i + 15][curr][curr], dp[i][curr][prev] + ball[curr]);\n }\n \n if (curr == N - 1) {\n update(res, dp[i][curr][prev]);\n }\n \n for (int next = 0; next < N; next++) {\n if (curr == next) continue;\n if (prev == next) {\n if (2 * d[prev][curr] >= 15) {\n int ni = i + d[prev][curr];\n if (ni <= R) {\n update(dp[ni][next][prev],\n dp[i][curr][prev] + ball[next]);\n } \n } else {\n int ni = i + (15 - d[prev][curr]);\n if (ni <= R) {\n update(dp[ni][next][prev],\n dp[i][curr][prev] + ball[next]);\n } \n }\n } else {\n int ni = i + d[curr][next]; \n if (ni <= R) {\n update(dp[ni][next][curr],\n dp[i][curr][prev] + ball[next]);\n }\n }\n }\n }\n }\n }\n \n std::cout << res << std::endl;\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 7640, "score_of_the_acc": -0.0573, "final_rank": 1 }, { "submission_id": "aoj_1594_2558134", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\nint dp[1111][31][31][9][2][2],a[31];\n\nint main() {\n int n,m,t;\n cin >> n >> m >> t;\n vector<PP> v[n];\n for(int i=0; i<n; i++) cin >> a[i];\n for(int i=0; i<m; i++) {\n int x,y,z;\n cin >> x >> y >> z;\n x--,y--;\n v[x].push_back(PP(y,P(z,a[y])));\n v[y].push_back(PP(x,P(z,a[x])));\n v[x].push_back(PP(x,P(15,a[x])));\n v[y].push_back(PP(y,P(15,a[y])));\n }\n memset(dp,-1,sizeof(dp));\n dp[0][0][0][8][0][0]=0;\n for(int i=0; i<t; i++)\n for(int x=0; x<n; x++)\n for(int j=0; j<n; j++)\n for(int k=5; k<9; k++)\n for(int f=0; f<2; f++)\n for(int h=0; h<2; h++) {\n if(dp[i][x][j][k][f][h]==-1) continue;\n for(int l=0; l<v[x].size(); l++) {\n PP p=v[x][l];\n int y=p.first,c=p.second.first,d=p.second.second;\n if(!h||y!=j) dp[i+c][y][x][min(8,c)][1][f]=max(dp[i+c][y][x][min(8,c)][1][f],dp[i][x][j][k][f][h]+d);\n else {\n dp[i+c][y][x][min(8,c)][0][f]=max(dp[i+c][y][x][min(8,c)][0][f],dp[i][x][j][k][f][h]);\n int z=max(0,15-(k<8?k:15)-c);\n dp[i+c+z][y][x][min(8,c+z)][1][f]=max(dp[i+c+z][y][x][min(8,c+z)][1][f],dp[i][x][j][k][f][h]+d);\n }\n }\n }\n int ans=0;\n for(int i=0; i<=t; i++)\n for(int j=0; j<n; j++)\n for(int k=0; k<9; k++)\n for(int l=0; l<2; l++)\n for(int f=0; f<2; f++) ans=max(ans,dp[i][n-1][j][k][l][f]);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 153244, "score_of_the_acc": -1.3591, "final_rank": 8 }, { "submission_id": "aoj_1594_2003440", "code_snippet": "#define _USE_MATH_DEFINES\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <complex>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <limits>\n#include <climits>\n#include <cfloat>\n#include <functional>\nusing namespace std;\n\nclass Edge\n{\npublic:\n int to, cost;\n Edge(int to, int cost){\n this->to = to;\n this->cost = cost;\n }\n};\n\nbool check(int pos, int t, const vector<pair<int, int> >& v)\n{\n for(const auto& p : v){\n if(pos == p.first && t - p.second < 15)\n return false;\n }\n return true;\n}\n\nvector<pair<int, int> > normalize(int pos, int t, const vector<pair<int, int> >& v)\n{\n vector<pair<int, int> > ans;\n for(const auto& p : v){\n if(pos == p.first){\n if(t - p.second < 15)\n ans.push_back(p);\n }\n else{\n if(t - p.second < 10)\n ans.push_back(p);\n }\n }\n sort(ans.begin(), ans.end());\n return ans;\n}\n\nint main()\n{\n int n, m, r;\n cin >> n >> m >> r;\n vector<int> d(n);\n for(int i=0; i<n; ++i)\n cin >> d[i];\n\n vector<vector<Edge> > edges(n);\n for(int i=0; i<m; ++i){\n int a, b, c;\n cin >> a >> b >> c;\n -- a;\n -- b;\n edges[a].push_back(Edge(b, c));\n edges[b].push_back(Edge(a, c));\n }\n\n vector<pair<int, int> > empty;\n vector<vector<map<vector<pair<int, int> >, int> > > dp(r+1, vector<map<vector<pair<int, int> >, int> >(n));\n dp[0][0][empty] = 0;\n for(int t=0; t<r; ++t){\n for(int curr=0; curr<n; ++curr){\n for(const auto& p : dp[t][curr]){\n vector<pair<int, int> > v = p.first;\n int ball = p.second;\n if(check(curr, t, v)){\n v.push_back(make_pair(curr, t));\n ball += d[curr];\n }\n\n for(const Edge& e : edges[curr]){\n int next = e.to;\n int t2 = t + e.cost;\n if(t2 <= r){\n auto v2 = normalize(next, t2, v);\n dp[t2][next][v2] = max(dp[t2][next][v2], ball);\n }\n }\n\n int t2;\n for(unsigned i=0; i<v.size(); ++i){\n if(v[i].first == curr)\n t2 = v[i].second + 15;\n }\n if(t2 <= r){\n auto v2 = normalize(curr, t2, v);\n dp[t2][curr][v2] = max(dp[t2][curr][v2], ball);\n }\n }\n }\n }\n \n int ans = 0;\n for(int t=0; t<=r; ++t){\n for(const auto& p : dp[t][n-1]){\n ans = max(ans, p.second);\n }\n }\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 1820, "memory_kb": 129960, "score_of_the_acc": -1.8448, "final_rank": 9 }, { "submission_id": "aoj_1594_2002962", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,a,b) for(int i=a;i<b;i++)\n#define REP(i,b) FOR(i,0,b)\n#define MP make_pair\n#define PB push_back\n\nusing ll=long long;\nusing pii=pair<int,int>;\nusing vi=vector<int>;\nusing ld=long double;\n\nint read(){\n\tint i;\n\tscanf(\"%d\",&i);\n\treturn i;\n}\n\nconst int inf=1000000007;\nvoid chmax(int& a,int b){\n\tif(a<b)\n\t\ta=b;\n}\nvoid chmin(int& a,int b){\n\tif(a>b)\n\t\ta=b;\n}\nint c[34][34],d[34];\nvi g[34];\nint dp[1001][34][34][16];\n\nint main(){\n\tint n=read(),m=read(),r=read();\n\tREP(i,n)\n\t\td[i]=read();\n\tREP(i,n)REP(j,n)c[i][j]=1145;\n\tREP(i,m){\n\t\tint a=read()-1,b=read()-1,_c=read();\n\t\tc[a][b]=c[b][a]=_c;\n\t}\n\tREP(k,n)REP(i,n)REP(j,n)\n\t\tchmin(c[i][j],c[i][k]+c[k][j]);\n\tREP(t,r+1)REP(i,n)REP(j,n)REP(k,16)\n\t\tdp[t][i][j][k]=-inf;\n\tdp[0][0][0][0]=0;\n\tREP(t,r){\n\t\tREP(i,n)REP(j,n)REP(k,16){\n\t\t\tREP(to,n){\n\t\t\t\tif(to==i){\n\t\t\t\t\tif(t+15<=r)\n\t\t\t\t\t\tchmax(dp[t+15][to][i][15],dp[t][i][j][k]+d[to]);\n\t\t\t\t}else if(to==j){\n\t\t\t\t\tint s=max(0,15-k-c[i][j]);\n\t\t\t\t\tif(t+s+c[i][j]<=r)\n\t\t\t\t\t\tchmax(dp[t+s+c[i][j]][to][i][min(15,s+c[i][j])],dp[t][i][j][k]+d[to]);\n\t\t\t\t}else if(t+c[i][to]<=r)\n\t\t\t\t\tchmax(dp[t+c[i][to]][to][i][min(15,c[i][to])],dp[t][i][j][k]+d[to]);\n\t\t\t}\n\t\t}\n\t}\n\tint ans=0;\n\tREP(t,r+1)REP(i,n)REP(j,16)\n\t\tans=max(ans,dp[t][n-1][i][j]);\n\tcout<<ans<<endl;\n}", "accuracy": 1, "time_ms": 1050, "memory_kb": 70732, "score_of_the_acc": -1.0247, "final_rank": 7 }, { "submission_id": "aoj_1594_2002888", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=(a);i<(b);++i)\ntypedef pair< int, int > pii;\ntemplate< typename T > void maxUpdate(T& a, T b) { a = max(a, b); }\n\nstruct Edge { int to, c; };\n\nint N, M, R, d[33];\nvector< vector< Edge > > edges;\n\nmap< vector< pii >, int > dp[1111][33];\n\nbool check(vector< pii >& vec, int u, int c) {\n\tfor (const pii& p : vec) if (p.first == u && p.second > c) return false;\n\treturn true;\n}\n\nvoid solve() {\n\tdp[0][0][vector< pii >()] = 0;\n\t\n\tfor_(r,0,R) for_(v,0,N) {\n\t\tfor (auto it : dp[r][v]) {\n\t\t\tvector< pii > vec = it.first;\n\t\t\tint ball = it.second;\n\t\t\t\n\t\t\tfor_(cr,1,16) {\n\t\t\t\tvector< pii > nxt_vec;\n\t\t\t\tbool flag = false;\n\t\t\t\tfor (const pii& p : vec) {\n\t\t\t\t\tif (p.second > cr) {\n\t\t\t\t\t\tnxt_vec.push_back(pii(p.first, p.second - cr));\n\t\t\t\t\t\tflag |= (p.first == v);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!flag) nxt_vec.push_back(pii(v, 15));\n\t\t\t\tmaxUpdate(dp[r + cr][v][nxt_vec], ball + (check(nxt_vec, v, 0) ? d[v] : 0));\n\t\t\t}\n\t\t\t\n\t\t\tfor (const Edge& e : edges[v]) {\n\t\t\t\tint nxt_ball = ball + (check(vec, e.to, e.c) ? d[e.to] : 0);\n\t\t\t\t\n\t\t\t\tvector< pii > nxt_vec;\n\t\t\t\tbool flag = false;\n\t\t\t\tfor (const pii& p : vec) {\n\t\t\t\t\tif (p.second > e.c) {\n\t\t\t\t\t\tnxt_vec.push_back(pii(p.first, p.second - e.c));\n\t\t\t\t\t\tflag |= (p.first == e.to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!flag) nxt_vec.push_back(pii(e.to, 15));\n\t\t\t\t\n\t\t\t\tmaxUpdate(dp[r + e.c][e.to][nxt_vec], nxt_ball);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tint ans = 0;\n\tfor_(r,0,R+1) for (auto it : dp[r][N-1]) maxUpdate(ans, it.second);\n\tcout << ans << endl;\n}\n\nint main() {\n\tcin >> N >> M >> R;\n\tfor_(i,0,N) cin >> d[i];\n\tedges.assign(N, vector< Edge >());\n\tfor_(i,0,M) {\n\t\tint a, b, c;\n\t\tcin >> a >> b >> c;\n\t\t--a; --b;\n\t\tedges[a].push_back(Edge{b, c});\n\t\tedges[b].push_back(Edge{a, c});\n\t}\n\tsolve();\n}", "accuracy": 0.06451612903225806, "time_ms": 40, "memory_kb": 9808, "score_of_the_acc": -0.0607, "final_rank": 12 }, { "submission_id": "aoj_1594_2002787", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\ntypedef vector<int>vint;\ntypedef pair<int,int>pint;\ntypedef vector<pint>vpint;\n#define rep(i,n) for(int i=0;i<(n);i++)\n#define reps(i,f,n) for(int i=(f);i<(n);i++)\n#define all(v) (v).begin(),(v).end()\n#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)\n#define pb push_back\n#define fi first\n#define se second\ntemplate<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}\ntemplate<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}\n\nint N,M,R;\nint D[30];\n\nvpint G[30];\nconst int INF=1001001001;\nint dist[30][30][2][2001];\n\ntypedef tuple<int,int,int,int,int>t5;\n\nsigned main(){\n cin>>N>>M>>R;\n rep(i,N)cin>>D[i];\n rep(i,M){\n int a,b,c;\n cin>>a>>b>>c;\n a--;b--;\n G[a].pb(pint(b,c));\n G[b].pb(pint(a,c));\n }\n\n fill_n(***dist,30*30*2*2001,INF);\n dist[0][0][0][0]=0;\n priority_queue<t5,vector<t5>,greater<t5>>que;\n que.push(make_tuple(0,0,0,0,0));\n\n while(que.size()){\n int t,v,p,f,c;\n tie(t,v,p,f,c)=que.top();\n que.pop();\n if(dist[v][p][f][c]<t)continue;\n if(t+15<=R&&dist[v][v][1][c+D[v]]>t+15){\n dist[v][v][1][c+D[v]]=t+15;\n que.push(make_tuple(t+15,v,v,1,c+D[v]));\n }\n\n for(auto &e:G[v]){\n int np;\n if(f)np=v;\n else np=e.fi;\n if(e.fi!=p||e.se*2>=15){\n if(t+e.se>R||dist[e.fi][np][1][c+D[e.fi]]<=t+e.se)continue;\n dist[e.fi][np][1][c+D[e.fi]]=t+e.se;\n que.push(make_tuple(t+e.se,e.fi,np,1,c+D[e.fi]));\n }\n else{\n if(t+e.se>R)continue;\n if(dist[e.fi][np][0][c]>t+e.se){\n dist[e.fi][np][0][c]=t+e.se;\n que.push(make_tuple(t+e.se,e.fi,np,0,c));\n }\n int nt=t+e.se+(15-e.se*2);\n if(nt>R)continue;\n if(dist[e.fi][e.fi][1][c+D[e.fi]]>nt){\n dist[e.fi][e.fi][1][c+D[e.fi]]=nt;\n que.push(make_tuple(nt,e.fi,e.fi,1,c+D[e.fi]));\n }\n }\n }\n }\n\n int ans=0;\n rep(i,N)rep(j,2001)rep(k,2)if(dist[N-1][i][k][j]<=R)chmax(ans,j);\n cout<<ans<<endl;\n return 0;\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 33604, "score_of_the_acc": -0.3906, "final_rank": 3 }, { "submission_id": "aoj_1594_2002572", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\nstatic const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;\ntypedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;\ntemplate<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }\ntemplate<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }\n\nint main() {\n\tint N; int M; int R;\n\twhile(~scanf(\"%d%d%d\", &N, &M, &R)) {\n\t\tvector<int> d(N);\n\t\tfor(int i = 0; i < N; ++ i)\n\t\t\tscanf(\"%d\", &d[i]);\n\t\tvector<vi> dist(N, vi(N, INF));\n\t\tvector<vpii> adj(N);\n\t\trep(i, M) {\n\t\t\tint a; int b; int c;\n\t\t\tscanf(\"%d%d%d\", &a, &b, &c), -- a, -- b;\n\t\t\tamin(dist[a][b], c);\n\t\t\tamin(dist[b][a], c);\n\t\t\tadj[a].emplace_back(b, c);\n\t\t\tadj[b].emplace_back(a, c);\n\t\t}\n\t\trep(i, N) amin(dist[i][i], 0);\n\t\trep(k, N) rep(i, N) rep(j, N)\n\t\t\tamin(dist[i][j], dist[i][k] + dist[k][j]);\n\t\tconst int T = 15;\n\t\tconst int X = N + 1;\n\t\tvector<vi> dpp(R + 1, vi(X * X * T, -1));\n\t\tauto dp = [&dpp, X, T](int r, int i, int j, int t) -> int& {\n\t\t\treturn dpp[r][(i * X + j) * T + t];\n\t\t};\n\t\tdp(0, 0, N, 0) = 0;\n\t\tint ans = -1;\n\t\trep(r, R + 1) rep(i, X) rep(j, X) rep(t, T) {\n\t\t\tint x = dp(r, i, j, t);\n\t\t\tif(x == -1) continue;\n\t\t\tif(r + dist[i][N - 1] <= R)\n\t\t\t\tamax(ans, x);\n\t\t\tif(j != N && t + dist[i][j] >= T) {\n\t\t\t\tamax(dp(r, i, N, 0), x);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\trep(k, N) if(k != i && k != j) {\n\t\t\t\tint c = dist[i][k];\n\t\t\t\tif(r + c <= R) {\n\t\t\t\t\tif(c >= T)\n\t\t\t\t\t\tamax(dp(r + c, k, N, 0), x + d[k]);\n\t\t\t\t\telse\n\t\t\t\t\t\tamax(dp(r + c, k, i, c), x + d[k]);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(r + T <= R)\n\t\t\t\tamax(dp(r + T, i, N, 0), x + d[i]);\n\t\t\tif(j != N) {\n\t\t\t\tint c = T - dist[i][j];\n\t\t\t\tif(r + c <= R)\n\t\t\t\t\tamax(dp(r + c, j, i, c), x + d[j]);\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\", ans);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 59384, "score_of_the_acc": -0.4187, "final_rank": 6 }, { "submission_id": "aoj_1594_2002541", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef int _loop_int;\n#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)\n#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)\n#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)\n\n#define DEBUG(x) cout<<#x<<\": \"<<x<<endl\n#define DEBUG_VEC(v) cout<<#v<<\":\";REP(i,v.size())cout<<\" \"<<v[i];cout<<endl\n#define ALL(a) (a).begin(),(a).end()\n\n#define CHMIN(a,b) a=min((a),(b))\n#define CHMAX(a,b) a=max((a),(b))\n\n// mod\nconst ll MOD = 1000000007ll;\n#define FIX(a) ((a)%MOD+MOD)%MOD\n\n// floating\ntypedef double Real;\nconst Real EPS = 1e-11;\n#define EQ0(x) (abs(x)<EPS)\n#define EQ(a,b) (abs(a-b)<EPS)\ntypedef complex<Real> P;\n\nint n,m,r;\nint d[35];\nint g[35][35];\nvi gg[35];\n// pos,time,prev -> max num\nint dp[35][1252][35][2];\nconst int INF = 830252521;\n\nint main(){\n scanf(\"%d%d%d\",&n,&m,&r);\n REP(i,n)scanf(\"%d\",d+i);\n REP(i,n)REP(j,n)g[i][j] = INF;\n REP(i,n)g[i][i] = 0;\n REP(i,m){\n int a,b,c;\n scanf(\"%d%d%d\",&a,&b,&c);\n --a;--b;\n g[a][b] = g[b][a] = c;\n gg[a].push_back(b);\n gg[b].push_back(a);\n }\n REP(i,n)REP(j,r+1)REP(k,n)REP(b,2)dp[i][j][k][b] = -INF;\n dp[0][0][0][0] = 0;\n REP(tm,r+1)REP(pos,n)REP(prv,n)REP(b,2){\n int now = dp[pos][tm][prv][b];\n if(now<0)continue;\n // printf(\"%d %d %d %d -> %d\\n\",tm,pos,prv,b,now);\n int prvlast = g[pos][prv];\n REP(i,gg[pos].size()){\n int to = gg[pos][i];\n int nxtprv = pos;\n if(b)nxtprv = to;\n if(to==prv){\n if(prvlast*2 >= 15){\n // go\n CHMAX(dp[to][tm+prvlast][nxtprv][0],now+d[to]);\n }else{\n // no wait\n CHMAX(dp[to][tm+prvlast][nxtprv][1],now);\n // wait and go\n CHMAX(dp[to][tm+15-prvlast][to][0],now+d[to]);\n }\n }else{\n // go\n CHMAX(dp[to][tm+g[pos][to]][nxtprv][0],now+d[to]);\n }\n }\n // all wait\n CHMAX(dp[pos][tm+15][pos][0],now+d[pos]);\n }\n int ans = 0;\n REP(tm,r+1)REP(prv,n)REP(b,2)CHMAX(ans,dp[n-1][tm][prv][b]);\n printf(\"%d\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 11240, "score_of_the_acc": -0.0924, "final_rank": 2 } ]
aoj_1595_cpp
Problem I: Traffic Tree Problem それぞれ1から N までの番号が付いた N 個の頂点が、 N -1本の無向辺によって繋がれたグラフが与えられる。各頂点について、その頂点からスタートしてすべての頂点を訪れるための最短のステップ数を出力せよ。 ただし、ある頂点から1本の辺をたどって別の頂点に移動することを1ステップとする。 Input 入力は以下の形式で与えられる。 N u 1 v 1 . . . u N−1 v N−1 1行目に、1つの整数 N が与えられる。 続く N -1行のうち i 行目には i 番目の辺の両端の頂点番号を表す整数 u i , v i が空白区切りで与えられる。 Constraints 2 ≤ N ≤ 10 5 1 ≤ u i , v i ≤ N (1 ≤ i ≤ N -1) u i ≠ v i 与えられるグラフは連結である Output 頂点1から頂点 N について i 行目に頂点 i からスタートしてすべての頂点を訪れるための最短のステップ数を出力せよ。 Sample Input 1 2 1 2 Sample Output 1 1 1 Sample Input 2 6 1 2 1 3 3 4 3 5 5 6 Sample Output 2 7 6 8 7 7 6 Sample Input2の図
[ { "submission_id": "aoj_1595_10965798", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <iomanip>\n#include <limits.h>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <string>\n#include <utility>\n#include <vector>\n#include <stack>\n#include <complex>\n#include <array>\n#include <cassert>\n#include <random>\n\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(A) A.begin(), A.end()\n#define debug(var) cout << #var << \" = \" << var << endl;\ntypedef long long ll;\n\ntemplate <typename T1, typename T2>\nostream &operator<<(ostream &os, const pair<T1, T2> &p)\n{\n os << \"(\" << p.first << \",\" << p.second << \")\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nistream &operator>>(istream &is, pair<T1, T2> &p)\n{\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v)\n{\n for (int i = 0; i < (int)v.size(); i++)\n {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<vector<T>> &v)\n{\n for (int i = 0; i < (int)v.size(); i++)\n {\n os << v[i] << endl;\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<vector<vector<T>>> &v)\n{\n int n = v.size();\n int m = v[0].size();\n int p = v[0][0].size();\n rep(k, p)\n {\n os << \"k = \" << k << endl;\n rep(i, n)\n {\n rep(j, m)\n {\n os << v[i][j][k];\n if (j < m - 1)\n {\n os << \" \";\n }\n else\n {\n os << endl;\n }\n }\n }\n }\n return os;\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v)\n{\n for (T &in : v)\n is >> in;\n return is;\n}\n\ntemplate <typename T, typename S>\nostream &operator<<(ostream &os, map<T, S> &mp)\n{\n for (auto &[key, val] : mp)\n {\n os << key << \":\" << val << \" \";\n }\n cout << endl;\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, set<T> st)\n{\n auto itr = st.begin();\n for (int i = 0; i < (int)st.size(); i++)\n {\n os << *itr << (i + 1 != (int)st.size() ? \" \" : \"\");\n itr++;\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, multiset<T> st)\n{\n auto itr = st.begin();\n for (int i = 0; i < (int)st.size(); i++)\n {\n os << *itr << (i + 1 != (int)st.size() ? \" \" : \"\");\n itr++;\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, queue<T> q)\n{\n while (q.size())\n {\n os << q.front() << \" \";\n q.pop();\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, deque<T> q)\n{\n while (q.size())\n {\n os << q.front() << \" \";\n q.pop_front();\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, stack<T> st)\n{\n while (st.size())\n {\n os << st.top() << \" \";\n st.pop();\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, priority_queue<T> pq)\n{\n while (pq.size())\n {\n os << pq.top() << \" \";\n pq.pop();\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, priority_queue<T, vector<T>, greater<T>> mpq)\n{\n while (mpq.size())\n {\n os << mpq.top() << \" \";\n mpq.pop();\n }\n return os;\n}\ntemplate <class E, class V, E (*merge)(E, E), E (*e)(), E (*put_edge)(V, int), V (*put_vertex)(E, int)>\nstruct RerootingDP {\n struct edge {\n int to, idx, xdi;\n };\n RerootingDP(int n_ = 0) : n(n_), inner_edge_id(0) {\n es.resize(2*n-2);\n start.resize(2*n-2);\n if (n == 1) es_build();\n }\n void add_edge(int u, int v, int idx, int xdi){\n start[inner_edge_id] = u;\n es[inner_edge_id] = {v,idx,xdi};\n inner_edge_id++;\n start[inner_edge_id] = v;\n es[inner_edge_id] = {u,xdi,idx};\n inner_edge_id++;\n if (inner_edge_id == 2*n-2){\n es_build();\n }\n }\n vector<V> build(int root_ = 0){\n root = root_;\n vector<V> subdp(n); subdp[0] = put_vertex(e(),0);\n outs.resize(n);\n vector<int> geta(n+1,0);\n for (int i = 0; i < n; i++) geta[i+1] = start[i+1] - start[i] - 1;\n geta[root+1]++;\n for (int i = 0; i < n; i++) geta[i+1] += geta[i];\n auto dfs = [&](auto sfs, int v, int f) -> void {\n E val = e();\n for (int i = start[v]; i < start[v+1]; i++){\n if (es[i].to == f){\n swap(es[start[v+1]-1],es[i]);\n }\n if (es[i].to == f) continue;\n sfs(sfs,es[i].to,v);\n E nval = put_edge(subdp[es[i].to],es[i].idx);\n outs[geta[v]++] = nval;\n val = merge(val,nval);\n }\n subdp[v] = put_vertex(val, v);\n };\n dfs(dfs,root,-1);\n return subdp;\n }\n vector<V> reroot(){\n vector<E> reverse_edge(n);\n reverse_edge[root] = e();\n vector<V> answers(n);\n auto dfs = [&](auto sfs, int v) -> void {\n int le = outs_start(v);\n int ri = outs_start(v+1);\n int siz = ri - le;\n vector<E> rui(siz+1);\n rui[siz] = e();\n for (int i = siz-1; i >= 0; i--){\n rui[i] = merge(outs[le+i],rui[i+1]);\n }\n answers[v] = put_vertex(merge(rui[0],reverse_edge[v]),v);\n E lui = e();\n for (int i = 0; i < siz; i++){\n V rdp = put_vertex(merge(merge(lui,rui[i+1]),reverse_edge[v]),v);\n reverse_edge[es[start[v]+i].to] = put_edge(rdp,es[start[v]+i].xdi);\n lui = merge(lui,outs[le+i]);\n sfs(sfs,es[start[v]+i].to);\n }\n };\n dfs(dfs,root);\n return answers;\n }\n private:\n int n, root, inner_edge_id;\n vector<E> outs;\n vector<edge> es;\n vector<int> start;\n int outs_start(int v){\n int res = start[v] - v;\n if (root < v) res++;\n return res;\n }\n void es_build(){\n vector<edge> nes(2*n-2);\n vector<int> nstart(n+2,0);\n for (int i = 0; i < 2*n-2; i++) nstart[start[i]+2]++;\n for (int i = 0; i < n; i++) nstart[i+1] += nstart[i];\n for (int i = 0; i < 2*n-2; i++) nes[nstart[start[i]+1]++] = es[i];\n swap(es,nes);\n swap(start,nstart);\n }\n};\nusing S = long long;\nS merge(S a,S b){\n return max(a,b);\n}\nS e(){\n return 0;\n}\nS put_edge(S x,int weight){\n return x+weight;\n}\nS put_vertex(S x,int i){\n return x;\n}\n\nint main(){\n int n;\n cin >> n;\n RerootingDP<S,S,merge,e,put_edge,put_vertex> dp(n);\n rep(i,n-1){\n int u,v;\n cin >> u >> v;\n u--;\n v--;\n dp.add_edge(u,v,1,1);\n }\n vector<S> ans = dp.build();\n // debug(ans);\n ans = dp.reroot();\n // debug(ans);\n rep(i,n){\n cout << 2*n-2-ans[i] << endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 24628, "score_of_the_acc": -0.9711, "final_rank": 10 }, { "submission_id": "aoj_1595_10959888", "code_snippet": "#define _GLGBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\nstruct DP\n{\n int max_dist;\n int max_dist_vertex;\n\n bool operator<(const DP &other) const\n {\n if (max_dist != other.max_dist)\n return max_dist < other.max_dist;\n\n return max_dist_vertex < other.max_dist_vertex;\n }\n};\n\nint N, ID = -1, invalid_vertex = -1;\nvector<vector<int>> G;\nvector<vector<DP>> dp;\nvector<DP> ans;\n\nDP dfs(int root, int parent)\n{\n int out_degree_of_root = static_cast<int>(G.at(root).size());\n DP max_dp_of_subtree{ID, root};\n\n dp.at(root) = vector<DP>(out_degree_of_root, DP{ID, invalid_vertex});\n\n for (int i = 0; i < out_degree_of_root; i++)\n {\n int adjacent = G.at(root).at(i);\n\n if (adjacent == parent)\n continue;\n\n dp.at(root).at(i) = dfs(adjacent, root);\n\n if (max_dp_of_subtree < dp.at(root).at(i))\n max_dp_of_subtree = dp.at(root).at(i);\n }\n\n return DP{\n max_dp_of_subtree.max_dist + 1,\n max_dp_of_subtree.max_dist_vertex};\n}\n\nvoid dfs_reroot(int root, DP parent_subtree_dp_info, int parent)\n{\n int out_degree_of_root = static_cast<int>(G.at(root).size());\n\n for (int i = 0; i < out_degree_of_root; i++)\n if (G.at(root).at(i) == parent)\n dp.at(root).at(i) = parent_subtree_dp_info;\n\n vector<DP> dp_prefix(out_degree_of_root + 1, DP{ID, invalid_vertex}), dp_suffix(out_degree_of_root + 1, DP{ID, invalid_vertex});\n\n for (int i = 0; i < out_degree_of_root; i++)\n {\n if (dp_prefix.at(i) < dp.at(root).at(i))\n dp_prefix.at(i + 1) = dp.at(root).at(i);\n else\n dp_prefix.at(i + 1) = dp_prefix.at(i);\n }\n\n for (int i = out_degree_of_root - 1; i >= 0; i--)\n {\n if (dp_suffix.at(i + 1) < dp.at(root).at(i))\n dp_suffix.at(i) = dp.at(root).at(i);\n else\n dp_suffix.at(i) = dp_suffix.at(i + 1);\n }\n\n ans.at(root) = DP{\n dp_prefix.at(out_degree_of_root).max_dist + 1,\n dp_prefix.at(out_degree_of_root).max_dist_vertex};\n\n for (int i = 0; i < out_degree_of_root; i++)\n {\n int adjacent = G.at(root).at(i);\n\n if (adjacent == parent)\n continue;\n\n DP max_dp_info;\n\n if (dp_prefix.at(i) < dp_suffix.at(i + 1))\n max_dp_info = dp_suffix.at(i + 1);\n else\n max_dp_info = dp_prefix.at(i);\n\n max_dp_info.max_dist++;\n\n if (out_degree_of_root == 1)\n max_dp_info.max_dist_vertex = root;\n\n dfs_reroot(adjacent, max_dp_info, root);\n }\n}\n\nint main()\n{\n cin >> N;\n\n G.resize(N);\n dp.resize(N);\n ans.resize(N, DP{ID, invalid_vertex});\n\n for (int i = 1; i <= N - 1; i++)\n {\n int A, B;\n cin >> A >> B;\n\n A--, B--;\n\n G.at(A).push_back(B);\n G.at(B).push_back(A);\n }\n\n dfs(0, -1);\n dfs_reroot(0, DP{ID, -1}, -1);\n\n for (int i = 0; i < N; i++)\n cout << 2 * (N - 1) - ans.at(i).max_dist << '\\n';\n\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 31944, "score_of_the_acc": -0.5921, "final_rank": 5 }, { "submission_id": "aoj_1595_10892327", "code_snippet": "// # pragma GCC target(\"avx2\")\n// # pragma GCC optimize(\"O3\")\n// # pragma GCC optimize(\"unroll-loops\")\n\n#include <bits/stdc++.h>\n// #include <atcoder/all>\nusing namespace std;\n// using namespace atcoder;\n\n// #include <boost/multiprecision/cpp_int.hpp>\n// using namespace boost::multiprecision;\n\n#define ll long long\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define vi vector<int>\n#define vl vector<ll>\n#define vd vector<double>\n#define vb vector<bool>\n#define vs vector<string>\n#define vc vector<char>\n#define ull unsigned long long\n#define chmax(a, b) a = max(a, b)\n#define chmin(a, b) a = min(a, b)\n\n// ll inf = (1ll << 61);\n// const double PI=3.1415926535897932384626433832795028841971;\n\n// ll rui(ll a,ll b){\n// if(b==0)return 1;\n// if(b%2==1) return a*rui(a*a,b/2);\n// return rui(a*a,b/2);\n// }\n\n// ll kai(ll n){\n// if(n==0)return 1;\n// return n*kai(n-1);\n// }\n\n// using mint = modint998244353;//static_modint<998244353>\n// using mint = modint1000000007;//static_modint<1000000007>\n// using mint = static_modint<1923865917>;\n// using mint = modint;//mint::set_mod(mod);\n\n// ll const mod=1000000007ll;\n// ll const mod=998244353ll;\n// ll modrui(ll a,ll b,ll mod){\n// a%=mod;\n// if(b==0)return 1;\n// if(b%2==1) return a*modrui(a*a%mod,b/2,mod)%mod;\n// return modrui(a*a%mod,b/2,mod)%mod;\n// }\n\n// ll inv(ll x){\n// x%=mod;\n// return modrui(x,mod-2);\n// }\n\n// ll modkai(ll n){\n// ll ret=1;\n// rep(i,n){\n// ret*=(i+1)%mod;\n// ret%=mod;\n// }\n// return ret;\n// }\n\n// void incr(vl &v,ll n){// n進法\n// ll k=v.size();\n// v[k-1]++;\n// ll now=k-1;\n// while (v[now]>=n)\n// {\n// v[now]=0;\n// if(now==0)break;\n// v[now-1]++;\n// now--;\n// }\n// return;\n// }\n\n\n\n\n\n\n\n//https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1595\nvl memo1(1<<17),memo2(1<<17),ans(1<<17,0);\n\nvoid dfs(vector<vl> const&g,vl const&par,ll now=0,ll d=0){\n vl v={d,memo1[now],memo2[now]};\n sort(v.begin(),v.end());\n chmax(ans[now],v[2]);\n for(auto &s:g[now]){\n if(memo1[s]+1==v[2]) dfs(g,par,s,v[1]+1);\n else dfs(g,par,s,v[2]+1);\n }\n}\n\nvoid solve(){\n ll n;\n cin >> n;\n vector<vl> g(n);\n vl par(n,-1);\n rep(i,n-1){\n ll u,v;\n cin >> u >> v;\n u--;\n v--;\n g[u].push_back(v);\n g[v].push_back(u);\n }\n deque<ll> dq={0,-1};\n while(!dq.empty()){\n ll x=dq[0],p=dq[1];\n par[x]=p;\n dq.pop_front();dq.pop_front();\n if(p!=-1){\n g[x].erase(find(g[x].begin(),g[x].end(),p));\n }\n for(auto &s:g[x]){\n dq.push_back(s);\n dq.push_back(x);\n }\n }\n vl deg(n);\n rep(i,n){\n deg[i]=g[i].size();\n if(deg[i]==0)dq.push_back(i);\n }\n while(!dq.empty()){\n ll x=dq[0];\n dq.pop_front();\n if(par[x]==-1)continue;\n ll p=par[x];\n if(memo1[p]<=memo1[x]+1){\n memo2[p]=memo1[p];\n memo1[p]=memo1[x]+1;\n }\n else if(memo2[p]<=memo1[x]+1){\n memo2[p]=memo1[x]+1;\n }\n deg[p]--;\n if(deg[p]==0)dq.push_back(p);\n }\n\n dfs(g,par);\n\n rep(i,n)cout << (n-1)*2-ans[i] << endl;\n}\n\nint main(){\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n ll t = 1;\n // cin >> t;\n while (t--) solve();\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 25532, "score_of_the_acc": -0.7986, "final_rank": 7 }, { "submission_id": "aoj_1595_10779535", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <set>\n#include <map>\n#include <cmath>\n#include <cstdint>\n#include <iomanip>\n#include <bit>\n#include <queue>\n#include <stack>\n//#include <atcoder/all>\n\nusing namespace std;\n\n//using mint = atcoder::modint998244353 ;\n\nvoid ansYesNo(bool ans, string yes = \"Yes\", string no = \"No\")\n{\n if (ans)\n {\n cout << yes << endl;\n }\n else\n {\n cout << no << endl;\n }\n}\n\nstruct Costs{\n int64_t child ;\n int64_t cost ;\n};\n\nvector<vector<pair<int64_t,int64_t>>> WeightedGraphInput(int Nodes, int Edges, bool Undirected, bool OneIndexed){\n vector<vector<pair<int64_t,int64_t>>> list(Nodes, vector<pair<int64_t,int64_t>>(0)) ;\n int StartIndex = 0 ;\n if (OneIndexed)\n {\n StartIndex = 1 ;\n }\n for (int i = 0; i < Edges; i++)\n {\n int64_t a,b,c ;\n cin >> a >> b ;\n c = 1 ;\n list.at(a-StartIndex).push_back(make_pair(b-StartIndex,c)) ;\n if (Undirected)\n {\n list.at(b-StartIndex).push_back(make_pair(a-StartIndex,c)) ;\n }\n }\n return list ;\n}\n\nint64_t calcSubScore(vector<vector<pair<int64_t,int64_t>>> &list, vector<int64_t> &d, vector<int64_t> &SubScore, int node, int p){\n int64_t SS = d.at(node) ;\n for (int i = 0; i < list.at(node).size(); i++)\n {\n int64_t next = list.at(node).at(i).first ;\n int64_t edgec = list.at(node).at(i).second ;\n if (next == p)\n {\n continue;\n }\n SS = max(SS,calcSubScore(list,d,SubScore,next,node) + edgec) ;\n }\n SubScore.at(node) = SS ;\n return SS ;\n}\n\nvoid updateCosts(pair<Costs,Costs> &C, Costs newc){\n if (C.first.cost < newc.cost)\n {\n C.second = C.first ;\n C.first = newc ;\n }else if (C.second.cost < newc.cost)\n {\n C.second = newc ;\n }\n \n return ;\n}\n\nint64_t solve(vector<vector<pair<int64_t,int64_t>>> &list, vector<int64_t> &d, vector<int64_t> &SubScore, vector<pair<Costs,Costs>> &dp, int node, int p){\n \n for (int i = 0; i < list.at(node).size(); i++)\n {\n int next = list.at(node).at(i).first ;\n int64_t edgec = list.at(node).at(i).second ;\n if (next == p)\n {\n updateCosts(dp.at(node),Costs(next,d.at(next)+edgec)) ;\n if (dp.at(next).first.child != node)\n {\n updateCosts(dp.at(node),Costs(next,dp.at(next).first.cost+edgec)) ;\n }else if (dp.at(next).second.child != node)\n {\n updateCosts(dp.at(node),Costs(next,dp.at(next).second.cost+edgec)) ;\n }\n \n }else\n {\n updateCosts(dp.at(node),Costs(next,SubScore.at(next)+edgec)) ;\n }\n }\n\n for (int i = 0; i < list.at(node).size(); i++)\n {\n int next = list.at(node).at(i).first ;\n if(next != p)solve(list,d,SubScore,dp,next,node) ;\n }\n return 0 ;\n}\n\nint main(){\n int n ;\n cin >> n ;\n vector<vector<pair<int64_t,int64_t>>> list = WeightedGraphInput(n,n-1,true,true) ;\n vector<int64_t> d(n,0) ;\n \n vector<int64_t> SubScore(n,0) ;\n calcSubScore(list,d,SubScore,0,-1) ;\n\n vector<pair<Costs,Costs>> dp(n,make_pair(Costs(-1,0),Costs(-1,0))) ;\n \n solve(list,d,SubScore,dp,0,-1) ;\n \n for (int i = 0; i < n; i++)\n {\n cout << 2*(n-1) - dp.at(i).first.cost << endl ;\n }\n \n}", "accuracy": 1, "time_ms": 70, "memory_kb": 27508, "score_of_the_acc": -0.6372, "final_rank": 6 }, { "submission_id": "aoj_1595_10378411", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(a) (a).begin(), (a).end()\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rrep(i, n) for (int i = (int)(n); i >= 0; i--)\n#define range(i, l, r) for (int i = (int)(l); i < (int)(r); i++)\nusing ll = long long;\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n for (int i = 0; i < v.size(); i++) {\n os << v[i] << (i == (v.size() - 1) ? \"\" : \", \");\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nostream& operator<<(ostream& os, const pair<T1, T2> p) {\n os << \"{\" << p.first << \", \" << p.second << \"}\";\n return os;\n}\n\nint n;\nvector<int> dist, ans;\nvector<vector<int>> edges;\nvoid dfs1(int i, int p) {\n dist[i] = 0;\n for (auto j : edges[i])\n if (j != p) {\n dfs1(j, i);\n dist[i] = max(dist[j] + 1, dist[i]);\n }\n}\n\nvoid dfs2(int i, int p, int pd) {\n vector<pair<int, int>> dp;\n for (auto j : edges[i]) {\n if (j == p) {\n dp.push_back({pd, j});\n } else {\n dp.push_back({dist[j], j});\n }\n }\n dp.push_back({-1, -1});\n sort(all(dp));\n reverse(all(dp));\n ans[i] = dp[0].first + 1;\n\n for (auto j : edges[i]) {\n if (j == p) continue;\n if (dp[0].second == j) {\n dfs2(j, i, dp[1].first + 1);\n } else {\n dfs2(j, i, dp[0].first + 1);\n }\n }\n}\n\nint main() {\n cin >> n;\n ans.resize(n);\n edges.resize(n);\n dist.resize(n);\n rep(i, n - 1) {\n int u, v;\n cin >> u >> v;\n u--;\n v--;\n edges[u].push_back(v);\n edges[v].push_back(u);\n }\n\n dfs1(0, -1);\n dfs2(0, -1, -1);\n rep(i, n) cout << 2 * (n - 1) - ans[i] << endl;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 28052, "score_of_the_acc": -1.0065, "final_rank": 11 }, { "submission_id": "aoj_1595_10377923", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(a) (a).begin(), (a).end()\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rrep(i, n) for (int i = (int)(n); i >= 0; i--)\n#define range(i, l, r) for (int i = (int)(l); i < (int)(r); i++)\nusing ll = long long;\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n os << \"[\";\n for (int i = 0; i < v.size(); i++) {\n os << v[i] << (i == (v.size() - 1) ? \"\" : \", \");\n }\n os << \"]\";\n return os;\n}\n\ntemplate <typename T1, typename T2>\nostream& operator<<(ostream& os, const pair<T1, T2> p) {\n os << \"{\" << p.first << \", \" << p.second << \"}\";\n return os;\n}\n\nint n;\nvector<int> ans;\nvoid dfs(int i, vector<vector<int>>& edges, vector<int>& par,\n vector<vector<pair<int, int>>>& dp1, vector<int>& dp2) {\n ans[i] = 2 * (n - 1) - dp2[i];\n for (auto j : edges[i]) {\n if (j != par[i]) {\n if (dp2[i] > 0 && dp1[i][0].second == j) {\n if (dp1[i].size() > 1) {\n dp2[i] = dp1[i][1].first + 1;\n } else {\n dp2[i] = 0;\n }\n }\n dp1[j].push_back({dp2[i], i});\n sort(all(dp1[j]));\n reverse(all(dp1[j]));\n dp2[j] = dp1[j][0].first + 1;\n dfs(j, edges, par, dp1, dp2);\n\n dp2[i] = dp1[i][0].first + 1;\n }\n }\n}\n\nint main() {\n cin >> n;\n ans.resize(n);\n vector<vector<int>> edges(n);\n rep(i, n - 1) {\n int u, v;\n cin >> u >> v;\n u--;\n v--;\n edges[u].push_back(v);\n edges[v].push_back(u);\n }\n\n vector<vector<pair<int, int>>> dp1(n);\n vector<int> dp2(n, -1);\n vector<int> q{~0, 0};\n vector<int> par(n, -1);\n while (!q.empty()) {\n int i = q.back();\n q.pop_back();\n if (i >= 0) {\n for (auto j : edges[i])\n if (j != par[i]) {\n par[j] = i;\n q.push_back(~j);\n q.push_back(j);\n }\n } else {\n i = ~i;\n for (auto j : edges[i])\n if (j != par[i]) {\n dp1[i].push_back({dp2[j], j});\n }\n sort(all(dp1[i]));\n reverse(all(dp1[i]));\n if (dp1[i].size() > 0) {\n dp2[i] = dp1[i][0].first + 1;\n } else {\n dp2[i] = 0;\n }\n }\n }\n\n dfs(0, edges, par, dp1, dp2);\n rep(i, n) cout << ans[i] << endl;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 29760, "score_of_the_acc": -1.115, "final_rank": 13 }, { "submission_id": "aoj_1595_10307144", "code_snippet": "#ifndef ONLINE_JUDGE\n#define _GLIBCXX_DEBUG //[]で配列外参照をするとエラーにしてくれる。上下のやつがないとTLEになるので注意 ABC311Eのサンプル4みたいなデバック中のTLEは防げないので注意\n#endif\n#include <bits/stdc++.h>\n\n// #include <atcoder/all>\n// using namespace atcoder;\n\nusing namespace std;\nusing ll = long long;\nll INF = 2e18;\nusing P = pair<ll, ll>;\n#define pb push_back\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define reprev(i, n) for (ll i = (n) - 1; i >= 0; i--)\n#define reps(i, n) for (ll i = 1; i <= (n); i++)\n#define for_(i, a, b) for (ll i = (a); i < (b); i++)\n#define all(v) v.begin(), v.end()\ntemplate <typename T>\ninline bool chmin(T &a, const T &b)\n{\n bool c = a > b;\n if (c)\n a = b;\n return c;\n}\ntemplate <typename T>\ninline bool chmax(T &a, const T &b)\n{\n bool c = a < b;\n if (c)\n a = b;\n return c;\n}\ntemplate <typename T>\ninline T ceil(T a, T b) { return (a + (b - 1)) / b; }\n// using mint = modint998244353;\n// using mint = modint1000000007;\n// using mint = static_modint<10>;//使うときはコメントアウトを外す\ntemplate <typename T>\nusing vc = vector<T>;\ntemplate <class T>\nistream &operator>>(istream &i, vc<T> &v)\n{\n rep(j, size(v)) i >> v[j];\n return i;\n}\ntemplate <class T>\nostream &operator<<(ostream &o, const vc<T> &v)\n{\n rep(j, size(v))\n {\n if (j)\n o << \" \";\n o << v[j];\n }\n o << endl;\n return o;\n}\n// ref:https://algo-logic.info/tree-dp/\n/* Rerooting: 全方位木 DP\n 問題ごとに以下を書き換える\n - 型DPと単位元\n - 型DPに対する二項演算 merge\n - まとめたDPを用いて新たな部分木のDPを計算する add_root\n 計算量: O(N)\n*/\nstruct Rerooting\n{\n /* start 問題ごとに書き換え */\n struct DP\n { // DP の型\n ll diff;\n ll cycle;\n ll edge_cnt;\n DP(long long diff_ = 0, ll cycle_ = 0, ll cnt = 0) : diff(diff_), cycle(cycle_), edge_cnt(cnt) {}\n };\n const DP identity = DP(); // 単位元(末端の値は add_root(identity) になるので注意)\n function<DP(DP, DP)> merge = [](DP dp_cum, DP d) -> DP\n {\n return DP(max(dp_cum.diff, d.diff), dp_cum.cycle + d.cycle, dp_cum.edge_cnt + d.edge_cnt);\n };\n function<DP(DP)> add_root = [](DP d) -> DP\n {\n if (d.cycle == 0 && d.edge_cnt == 0)\n {\n return DP(0, 0);\n }\n return DP(d.diff + 1, d.cycle + d.edge_cnt * 2, 0);\n };\n /* end 問題ごとに書き換え */\n // グラフの定義\n struct Edge\n {\n int to;\n };\n using Graph = vector<vector<Edge>>;\n vector<vector<DP>> dp; // dp[v][i]: vから出るi番目の有向辺に対応する部分木のDP\n vector<DP> ans; // ans[v]: 頂点vを根とする木の答え\n Graph G;\n Rerooting(int N) : G(N)\n {\n dp.resize(N);\n ans.assign(N, identity);\n }\n void add_edge(int a, int b)\n {\n G[a].push_back({b});\n }\n void build()\n {\n dfs(0); // 普通に木DP\n bfs(0, identity); // 残りの部分木に対応するDPを計算\n }\n DP dfs(int v, int p = -1)\n { // 頂点v, 親p\n DP dp_cum = identity;\n int deg = G[v].size();\n dp[v] = vector<DP>(deg, identity);\n ll cnt = 0;\n for (int i = 0; i < deg; i++)\n {\n int u = G[v][i].to;\n if (u == p)\n continue;\n cnt++;\n dp[v][i] = dfs(u, v);\n dp_cum = merge(dp_cum, dp[v][i]);\n // dp_cum = merge(dp_cum, DP(dp[v][i].dp + G[v][i].cost));\n }\n dp_cum.edge_cnt = cnt;\n return add_root(dp_cum);\n }\n void bfs(int v, const DP &dp_p, int p = -1)\n { // bfs だが、実装が楽なので中身は dfs になっている\n int deg = G[v].size();\n for (int i = 0; i < deg; i++)\n { // 前のbfsで計算した有向辺に対応する部分木のDPを保存\n if (G[v][i].to == p)\n dp[v][i] = dp_p;\n }\n vector<DP> dp_l(deg + 1, identity), dp_r(deg + 1, identity); // 累積merge\n for (int i = 0; i < deg; i++)\n {\n dp_l[i + 1] = merge(dp_l[i], dp[v][i]);\n dp_l[i + 1].edge_cnt++;\n // dp_l[i + 1] = merge(dp_l[i], DP(dp[v][i].dp + G[v][i].cost));\n }\n for (int i = deg - 1; i >= 0; i--)\n {\n dp_r[i] = merge(dp_r[i + 1], dp[v][i]);\n dp_r[i].edge_cnt++;\n // dp_r[i] = merge(dp_r[i + 1], DP(dp[v][i].dp + G[v][i].cost));\n }\n ans[v] = add_root(dp_l[deg]); // 頂点 v の答え\n for (int i = 0; i < deg; i++)\n { // 一つ隣の頂点に対しても同様に計算\n int u = G[v][i].to;\n if (u == p)\n continue;\n\n bfs(u, add_root(merge(dp_l[i], dp_r[i + 1])), v);\n }\n }\n};\nint main()\n{\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n // ref:https://rsk0315.hatenablog.com/entry/2020/05/09/170315\n ll N;\n cin >> N;\n Rerooting reroot(N);\n rep(i, N - 1)\n {\n ll a, b;\n cin >> a >> b;\n reroot.add_edge(a - 1, b - 1);\n reroot.add_edge(b - 1, a - 1);\n }\n reroot.build();\n ll ans = INF;\n rep(i, N)\n {\n cout << reroot.ans[i].cycle - reroot.ans[i].diff << endl;\n }\n\n // cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 106644, "score_of_the_acc": -2, "final_rank": 16 }, { "submission_id": "aoj_1595_10292497", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#ifdef LOCAL\n#include <debug.hpp>\n#else\n#define debug(...)\n#endif\n\n// g[from] contains outgoing edges (to, edgeid(from, to))\n// (E, op, e) is commutative monoid\n// ~edgeid(from, to) == edgeid(to, from)\n// return calculator of dp(r, v)\ntemplate <class V, class E>\nauto rerootingdp(auto op, E e, auto put_edge, auto put_vertex, const vector<vector<pair<int, int>>>& g, int root = 0) {\n struct dp {\n // dp(r, v) : root is r, dp of subtree v\n // ans[v] = dp(v, v)\n // from[v] = dp(v, par(v))\n // to[v] = dp(par(v), v)\n // from[root] and to[root] is undefined\n vector<V> ans, from, to;\n vector<int> down, up;\n vector<vector<int>> childs;\n bool is_in_subtree(int r, int v) { return down[r] < down[v] && up[v] <= up[r]; }\n V operator()(int r, int v) {\n if (r == v) return ans[v];\n if (!is_in_subtree(v, r)) return to[v];\n int left = 0, right = ssize(childs[v]);\n while (right - left > 1) {\n int mid = midpoint(left, right);\n (down[childs[v][mid]] <= down[r] ? left : right) = mid;\n }\n return from[childs[v][left]];\n }\n };\n // 根を root としたときの dp の値を計算\n int n = ssize(g);\n vector<E> from(n, e), to(n, e);\n vector<V> dp_to(n);\n vector<vector<int>> childs(n);\n vector<int> down(n), up(n);\n int t = 0;\n auto dfs1 = [&](auto&& self, int v, int p) -> void {\n down[v] = t++;\n childs[v].reserve(g[v].size());\n for (auto [c, eid] : g[v]) {\n if (c == p) continue;\n childs[v].emplace_back(c);\n self(self, c, v);\n // to[c] に頂点 c の情報を加えたもの\n dp_to[c] = put_vertex(to[c], c);\n to[c] = put_edge(dp_to[c], eid);\n to[v] = op(to[v], to[c]);\n }\n up[v] = t;\n };\n dfs1(dfs1, root, -1);\n\n vector<V> dp_ans(n), dp_from(n);\n // tail[i] := 頂点 v の子の辺に対し, 右側から蓄積した dp の値\n vector<E> tail(n);\n auto dfs2 = [&](auto&& self, int v, int p) -> void {\n tail[ssize(g[v])] = e;\n int cur = ssize(g[v]) - 1;\n for (auto [c, eid] : g[v] | views::reverse) {\n if (c == p) continue;\n tail[cur] = op(tail[cur + 1], to[c]);\n cur--;\n }\n dp_ans[v] = put_vertex(op(tail[++cur], from[v]), v);\n E acc = e;\n for (auto [c, eid] : g[v]) {\n if (c == p) continue;\n dp_from[c] = put_vertex(op(op(acc, tail[++cur]), from[v]), v);\n from[c] = put_edge(dp_from[c], ~eid);\n acc = op(acc, to[c]);\n }\n for (auto [c, eid] : g[v]) {\n if (c == p) continue;\n self(self, c, v);\n }\n };\n dfs2(dfs2, root, -1);\n return dp{dp_ans, dp_from, dp_to, down, up, childs};\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n int N;\n cin >> N;\n vector<vector<pair<int, int>>> G(N);\n for (int i = 0; i < N - 1; i++) {\n int u, v;\n cin >> u >> v, u--, v--;\n G[u].emplace_back(v, i);\n G[v].emplace_back(u, ~i);\n }\n\n // まずは普通の木 dp を考える\n // dp[i] := i をスタートして, その部分木に含まれる頂点を訪れて戻るまでの最短距離\n // vector<int> dp(N), height(N);\n // auto dfs = [&](auto&& self, int v, int p) -> void {\n // int _dp = 0, _height = 0;\n // for (auto [c, eid] : G[v]) {\n // if (c == p) continue;\n // self(self, c, v);\n // _dp += dp[c] + 2;\n // _height = max(_height, height[c] + 1);\n // }\n // dp[v] = _dp;\n // height[v] = _height;\n // };\n\n // for (int i = 0; i < N; i++) {\n // dp.assign(N, 0);\n // dfs(dfs, i, -1);\n // // dp[i] - height[i] が答え\n // cout << dp[i] - height[i] << '\\n';\n // }\n\n // 全方位木 dp\n using T = pair<int, int>;\n auto op = [](T a, T b) { return T{a.first + b.first, max(a.second, b.second)}; };\n T e = T{0, 0};\n auto put_edge = [](T v, int) { return T{v.first + 2, v.second + 1}; };\n auto put_vertex = [](T v, int) { return v; };\n\n auto dp = rerootingdp<T, T>(op, e, put_edge, put_vertex, G);\n for (int i = 0; i < N; i++) {\n debug(dp(i, i));\n cout << dp(i, i).first - dp(i, i).second << '\\n';\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 39380, "score_of_the_acc": -0.3962, "final_rank": 2 }, { "submission_id": "aoj_1595_10264445", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main() {\n int N;\n cin >> N;\n vector E(N, vector<int>());\n for (int i = 0; i < N - 1; i++) {\n int u, v;\n cin >> u >> v;\n u--;\n v--;\n E[u].push_back(v);\n E[v].push_back(u);\n }\n vector<int> dp1(N, 0);\n auto dfs1 = [&] (auto dfs1, int v, int p) -> void {\n for (int w : E[v]) {\n if (w != p) {\n dfs1(dfs1, w, v);\n dp1[v] = max(dp1[v], dp1[w] + 1);\n }\n }\n };\n dfs1(dfs1, 0, -1);\n vector<int> dp2(N, 0);\n vector<int> ans(N);\n auto dfs2 = [&] (auto dfs2, int v, int p) -> void {\n vector<pair<int, int>> A;//(葉までの距離, 頂点)\n A.emplace_back(0, -1);\n for (int w : E[v]) {\n if (w != p) {\n A.emplace_back(dp1[w] + 1, w);\n } else {\n A.emplace_back(dp2[v] + 1, w);\n }\n }\n sort(A.rbegin(), A.rend());\n ans[v] = A[0].first;\n for (int w : E[v]) {\n if (w != p) {\n if (A[0].second == w) {\n dp2[w] = A[1].first;\n } else {\n dp2[w] = A[0].first;\n }\n dfs2(dfs2, w, v);\n }\n }\n };\n dfs2(dfs2, 0, -1);\n for (int i = 0; i < N; i++) {\n cout << (N - 1) * 2 - ans[i] << '\\n';\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 31860, "score_of_the_acc": -0.4094, "final_rank": 3 }, { "submission_id": "aoj_1595_10238651", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n\nint N;\nvector<int> G[1<<17];\nvector<int> dp[1<<17];\nint ans[1<<17];\n\nvoid pG(){\n\tfor(int i=0;i<N;i++){\n\t\tcout<<i<<\":\";\n\t\tfor(int j=0;j<G[i].size();j++){\n\t\t\tcout<<G[i][j]<<\" \";\n\t\t}\n\t\tcout<<endl;\n\t}\n\tcout<<endl;\n}\n\nvoid pdp(){\n\tfor(int i=0;i<N;i++){\n\t\tcout<<i<<\":\";\n\t\tfor(int j=0;j<dp[i].size();j++){\n\t\t\tcout<<dp[i][j]<<\" \";\n\t\t}\n\t\tcout<<endl;\n\t}\n\tcout<<endl;\n}\n\nint rec(int v, int p=-1){\n\tint s=G[v].size();\n\tint res=0;\n\tdp[v].assign(s,-1);\n\tfor(int i=0;i<s;i++){\n\t\tint to=G[v][i];\n\t\tif(to!=p){\n\t\t\tdp[v][i]=rec(to,v);\n\t\t\tres=max(res,dp[v][i]+1);\n\t\t}\n\t}\n\treturn res;\n}\n\nvoid rerec(int v,int pval,int p){\n\tint s=G[v].size();\n\t//pdp();\n\tfor(int i=0;i<s;i++){\n\t\tint to=G[v][i];\n\t\tif(to==p){\n\t\t\tdp[v][i]=pval;\n\t\t}\n\t}\n\tvector<int> left(s+1,-1),right(s+1,-1);\n\tfor(int i=0;i<s;i++){\n\t\tleft[i+1]=max(left[i],dp[v][i]);\n\t\tright[i+1]=max(right[i],dp[v][s-i-1]);\n\t}\n\tfor(int i=0;i<s;i++){\n\t\tint to=G[v][i];\n\t\tif(to!=p){\n\t\t\trerec(to, max(left[i],right[s-i-1])+1,v);\n\t\t}\n\t}\n}\n\nvoid solve(){\n\trec(0,-1);\n\trerec(0,0,-1);\n\t//pdp();\n\tfor(int v=0;v<N;v++){\n\t\tint res=0;\n\t\tfor(int i=0;i<G[v].size();i++){\n\t\t\tres=max(res,dp[v][i]+1);\n\t\t}\n\t\tcout<<(N-1)*2-res<<endl;\n\t}\n}\n\nint main(){\n\tcin>>N;\n\tfor(int i=0;i<N-1;i++){\n\t\tint u,v;\n\t\tcin>>u>>v;\n\t\tu--,v--;\n\t\tG[u].push_back(v);\n\t\tG[v].push_back(u);\n\t}\n\tsolve();\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 31216, "score_of_the_acc": -1.1301, "final_rank": 14 }, { "submission_id": "aoj_1595_10238321", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\nusing namespace std;\n\nvector<int> G[1<<17];\nint dp[1<<17];\nint ans[1<<17];\n\nvoid dfs(int idx,int par){\n\tfor(auto &n:G[idx])if(n!=par){\n\t\tdfs(n,idx);\n\t\tdp[idx]=max(dp[idx],dp[n]+1);\n\t}\n}\n\nvoid dfs2(int idx,int d_par,int par){\n\tvector<pair<int,int>> d_child;\n\td_child.emplace_back(0,-1);\n\tfor(auto &n:G[idx]){\n\t\tif(n==par)d_child.emplace_back(d_par+1,n);\n\t\telse d_child.emplace_back(dp[n]+1,n);\n\t}\n\tsort(d_child.rbegin(),d_child.rend());\n\tans[idx]=d_child[0].first;\n\tfor(int &n:G[idx])if(n!=par){\n\t\tdfs2(n, d_child[d_child[0].second==n].first, idx);\n\t}\n}\n\nint main(){\n\tint N;\n\tcin>>N;\n\tfor(int i=0;i<N-1;i++){\n\t\tint u,v;\n\t\tcin>>u>>v;\n\t\tu--,v--;\n\t\tG[u].push_back(v);\n\t\tG[v].push_back(u);\n\t}\n\tdfs(0,-1);\n\tdfs2(0,0,-1);\n\tfor(int i=0;i<N;i++){\n\t\tcout<<(N-1)*2-ans[i]<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 28900, "score_of_the_acc": -1.0152, "final_rank": 12 }, { "submission_id": "aoj_1595_10059008", "code_snippet": "// #define _GLIBCXX_DEBUG\n#include <bits/stdc++.h>\nusing namespace std;\n\n/* alias*/\nusing ull = unsigned long long;\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<long long>;\nusing vs = vector<string>;\nusing vb = vector<bool>;\nusing vd = vector<double>;\nusing vvi = vector<vi>;\nusing vvl = vector<vl>;\nusing vvd = vector<vd>;\nusing pi = pair<int, int>;\nusing pl = pair<ll, ll>;\nusing vpi = vector<pi>;\nusing vpl = vector<pl>;\nusing vvpi = vector<vpi>;\nusing vvpl = vector<vpl>;\n\n/* define short */\n#define _overload5(a, b, c, d, e, name, ...) name\n#define _overload4(a, b, c, d, name, ...) name\n#define _overload3(a, b, c, name, ...) name\n#define _rep0(n) for(int i = 0; i < (int)(n); ++i)\n#define _rep1(i, n) for(int i = 0; i < (int)(n); ++i)\n#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)\n#define _rep3(i, a, b, c) for(int i = (int)(a); i < (int)(b); i += (int)(c))\n#define rep(...) _overload4(__VA_ARGS__, _rep3, _rep2, _rep1, _rep0)(__VA_ARGS__)\n#define rrep1(n) for(int i = (n) - 1;i >= 0;i--)\n#define rrep2(i,n) for(int i = (n) - 1;i >= 0;i--)\n#define rrep3(i,a,b) for(int i = (b) - 1;i >= (a);i--)\n#define rrep4(i,a,b,c) for(int i = (a) + ((b)-(a)-1) / (c) * (c);i >= (a);i -= c)\n#define rrep(...) _overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define sz(x) (int)(x).size()\n#define pcnt(x) __builtin_popcountll(x)\n#define eb emplace_back\n#define fi first\n#define se second\n\n/* debug */\n// 標準エラー出力を含む提出はrejectされる場合もあるので注意\n#define debug(x) cerr << \"\\033[33m(line:\" << __LINE__ << \") \" << #x << \": \" << x << \"\\033[m\" << endl;\n\n/* func */\ntemplate<class T> inline bool chmax(T& a, T b) { return ((a < b) ? (a = b, true) : (false)); }\ntemplate<class T> inline bool chmin(T& a, T b) { return ((a > b) ? (a = b, true) : (false)); }\n\n/* const */\nconst int INF = INT_MAX/2;\nconst ll LINF = 1LL<<60;\nconst int MOD = 1e9+7;\nconst int dx[] {1,0,-1,0};\nconst int dy[] {0,1,0,-1};\nconst double PI = 3.1415926535;\nconst string yes = \"Yes\";\nconst string no = \"No\";\n\n\nint main() { \n int n; cin >> n;\n vvi g(n);\n rep(i,n-1) {\n int u, v; cin >> u >> v; u--; v--;\n g[u].eb(v);\n g[v].eb(u);\n }\n\n vi dist(n);\n auto dfs = [&](auto self, int v, int pv=-1) -> void {\n for (auto nv: g[v]) {\n if (nv == pv) continue;\n self(self,nv,v);\n chmax(dist[v],dist[nv]+1);\n }\n };\n dfs(dfs,0);\n\n vi ans(n);\n auto rerooting = [&](auto self, int v, int pv=-1, int d_par=0) -> void {\n vpi d_dist; d_dist.eb(-1,-1);\n for (auto nv : g[v]) {\n if (nv == pv) d_dist.eb(d_par+1,nv);\n else d_dist.eb(dist[nv],nv);\n }\n sort(rall(d_dist));\n ans[v] = d_dist[0].fi;\n\n for (auto nv : g[v]) {\n if (nv == pv) continue;\n int nd_par = (d_dist[0].se == nv ? d_dist[1].fi : d_dist[0].fi);\n self(self,nv,v,nd_par);\n }\n };\n rerooting(rerooting,0);\n\n rep(v,n) {\n int now = 2*(n-1)-ans[v]-1;\n cout << now << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 31164, "score_of_the_acc": -0.4932, "final_rank": 4 }, { "submission_id": "aoj_1595_10046520", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nvector<int> edge[100010];\nint dist[100010]={};\nvoid dfs1(int now, int par){\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n dfs1(to, now);\n dist[now] = max(dist[now], 1+dist[to]);\n }\n}\nint dp[100010]={};\nvoid dfs2(int now, int par, int pval){\n int ma1=pval+1, ma2=0;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n int nv = dist[to]+1;\n if(ma2<nv){\n swap(nv, ma2);\n if(ma1<ma2) swap(ma1, ma2);\n }\n }\n dp[now] = ma1;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n if(dist[to]+1==ma1) dfs2(to, now, ma2);\n else dfs2(to, now, ma1);\n }\n}\nint main(){\n int n, u, v;\n cin >> n;\n for(int i=0;i<n-1;i++){\n cin >> u >> v;\n edge[--u].push_back(--v);\n edge[v].push_back(u);\n }\n dfs1(0, -1);\n dfs2(0, -1, -1);\n for(int i=0;i<n;i++) cout << n+n-2-dp[i] << endl;\n return(0);\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 15628, "score_of_the_acc": -0.8782, "final_rank": 8 }, { "submission_id": "aoj_1595_10046503", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nvector<int> edge[100010];\nint dist[100010]={};\nvoid dfs1(int now, int par){\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n dfs1(to, now);\n dist[now] = max(dist[now], 1+dist[to]);\n }\n}\nint dp[100010]={};\nvoid dfs2(int now, int par, int pval){\n int ma1=pval+1, ma2=0;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n int nv = dist[to]+1;\n if(ma2<nv){\n swap(nv, ma2);\n if(ma1<ma2) swap(ma1, ma2);\n }\n }\n dp[now] = ma1;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n if(dist[to]+1==ma1) swap(ma1, ma2);\n dfs2(to, now, ma1);\n }\n\n}\nint main(){\n int n, u, v;\n cin >> n;\n for(int i=0;i<n-1;i++){\n cin >> u >> v;\n u--;\n v--;\n edge[u].push_back(v);\n edge[v].push_back(u);\n }\n dfs1(0, -1);\n dfs2(0, -1, -1);\n for(int i=0;i<n;i++) cout << n+n-2-dp[i] << endl;\n return(0);\n}", "accuracy": 0.125, "time_ms": 110, "memory_kb": 9888, "score_of_the_acc": -0.8189, "final_rank": 19 }, { "submission_id": "aoj_1595_10046495", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nvector<int> edge[100010];\nint dist[100010]={};\nvoid dfs1(int now, int par){\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n dfs1(to, now);\n dist[now] = max(dist[now], 1+dist[to]);\n }\n}\nint dp[100010]={};\nvoid dfs2(int now, int par, int pval){\n int ma1=pval+1, ma2=0;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n int nv = dist[to]+1;\n if(ma2<nv){\n swap(nv, ma2);\n if(ma1<ma2) swap(ma1, ma2);\n }\n }\n dp[now] = ma1;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n if(dist[to]+1==ma1) swap(ma1, ma2);\n dfs2(to, now, ma1);\n }\n\n}\nint main(){\n int n, u, v;\n cin >> n;\n for(int i=0;i<n-1;i++){\n cin >> u >> v;\n edge[--u].push_back(--v);\n edge[v].push_back(u);\n }\n dfs1(0, -1);\n dfs2(0, -1, -1);\n for(int i=0;i<n;i++) cout << n+n-2-dp[i] << endl;\n return(0);\n}", "accuracy": 0.125, "time_ms": 110, "memory_kb": 9892, "score_of_the_acc": -0.8189, "final_rank": 20 }, { "submission_id": "aoj_1595_10046465", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nvector<int> edge[100010];\nint dp[100010]={};\nint dist[100010]={};\nvoid dfs1(int now, int par){\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n dfs1(to, now);\n dist[now] = max(dist[now], dist[to]+1);\n }\n}\nvoid dfs2(int now, int par, int pval){\n int ma1=pval+1, ma2=0;\n for(int i=0;i<edge[now].size();i++){\n int to=edge[now][i];\n if(to==par) continue;\n int nv = dist[to]+1;\n if(ma2<=nv){\n swap(nv, ma2);\n if(ma1<ma2) swap(ma1, ma2);\n }\n }\n dp[now] = ma1;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n if(dist[to]+1==ma1) swap(ma1, ma2);\n dfs2(to, now, ma1);\n }\n}\nint main(){\n int n, u, v;\n cin >> n;\n for(int i=0;i<n-1;i++){\n cin >> u >> v;\n edge[--u].push_back(--v);\n edge[v].push_back(u);\n }\n dfs1(0, -1);\n dfs2(0, -1, -1);\n for(int i=0;i<n;i++) cout << n+n-2-dp[i] << endl;\n return(0);\n}", "accuracy": 0.125, "time_ms": 110, "memory_kb": 9820, "score_of_the_acc": -0.8182, "final_rank": 18 }, { "submission_id": "aoj_1595_10046439", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nvector<int> edge[100010];\nint dp[100010], dist[100010]={};\nvoid dfs1(int now, int par){\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n dfs1(to, now);\n dist[now] = max(dist[now], dist[to]+1);\n }\n}\nvoid dfs2(int now, int par, int pval){\n int ma1=pval+1, ma2=0;\n for(int i=0;i<edge[now].size();i++){\n int to=edge[now][i], nv = dist[to]+1;\n if(to==par) continue;\n if(ma2<=dist[to]){\n swap(nv, ma2);\n if(ma1<ma2) swap(ma1, ma2);\n }\n }\n dp[now] = ma1;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n if(dist[to]+1==ma1) swap(ma1, ma2);\n dfs2(to, now, ma1);\n }\n}\nint main(){\n int n, u, v;\n cin >> n;\n for(int i=0;i<n-1;i++){\n cin >> u >> v;\n edge[--u].push_back(--v);\n edge[v].push_back(u);\n }\n dfs1(0, -1);\n dfs2(0, -1, -1);\n for(int i=0;i<n;i++) cout << n+n-2-dp[i] << endl;\n return(0);\n}", "accuracy": 0.125, "time_ms": 100, "memory_kb": 9892, "score_of_the_acc": -0.728, "final_rank": 17 }, { "submission_id": "aoj_1595_10046254", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nvector<int> edge[100010];\nll dist[100010]={};\nvoid dfs1(int now, int par){\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n dfs1(to, now);\n dist[now] = max(dist[now], 1+dist[to]);\n }\n}\nll dp[100010]={};\nvoid dfs2(int now, int par, int pval){\n int ma1=pval+1, ma2=0;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n int nv = dist[to]+1;\n if(ma2<nv){\n swap(nv, ma2);\n if(ma1<ma2) swap(ma1, ma2);\n }\n }\n\n dp[now] = ma1;\n for(int i=0;i<edge[now].size();i++){\n int to = edge[now][i];\n if(to==par) continue;\n if(dist[to]+1==ma1) dfs2(to, now, ma2);\n else dfs2(to, now, ma1);\n }\n\n}\n\nint main(){\n int n, u, v;\n cin >> n;\n for(int i=0;i<n-1;i++){\n cin >> u >> v;\n u--;\n v--;\n edge[u].push_back(v);\n edge[v].push_back(u);\n }\n dfs1(0, -1);\n dfs2(0, -1, -1);\n for(int i=0;i<n;i++) cout << n+n-2-dp[i] << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 18280, "score_of_the_acc": -0.9056, "final_rank": 9 }, { "submission_id": "aoj_1595_10036986", "code_snippet": "#line 1 \"/home/dispersion/competitive/icpc/ICPC-library/ICPC2024/merge_src_docs/template/24_template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\n#define rep2(i, l, r) for(ll i = l; i < r; i++)\n\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\n\n#define all(A) A.begin(), A.end()\n#define elif else if\nusing pii = pair<ll, ll>;\n\nbool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; }\n\nstruct IOSetup {\n IOSetup() {\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n} iosetup;\n\ntemplate<class T>\nvoid print(vector<T> a) {\n for(auto x : a) cout << x << ' ';\n cout << endl;\n}\n\nvoid print(auto x) { cout << x << endl; }\n\ntemplate<class Head, class... Tail>\nvoid print(Head &&head, Tail &&...tail) {\n cout << head << ' ';\n print(forward<Tail>(tail)...);\n}\n#line 2 \"Rerooting.cpp\"\n\nusing S = ll;\n\nint n;\nvector<vector<pair<int, int>>> edge;\n\nvector<S> dp1, dp2, ans;\nS e() {return 0LL;}\nS f(S res, int i) {return res + 1;}\nS g(S res, int v) {return res;}\nS merge(S a, S b) {return max(a, b);}\n\nvoid dfs1(int v, int p) {\n for(auto [u, id] : edge[v]) {\n if(u != p) {\n dfs1(u, v);\n dp1[v] = merge(dp1[v], f(dp1[u], id));\n }\n }\n dp1[v] = g(dp1[v], v);\n}\n\nvoid dfs2(int v, int p) {\n int s = edge[v].size();\n vector<S> R(s + 1, e());\n for(int i = s - 1; i >= 0; i--) {\n auto [u, id] = edge[v][i];\n if(u == p) R[i] = merge(R[i + 1], f(dp2[v], id));\n else R[i] = merge(R[i + 1], f(dp1[u], id));\n }\n\n S L = e();\n rep(i, s) {\n auto [u, id] = edge[v][i];\n if(u != p) {\n dp2[u] = g(merge(L, R[i + 1]), v);\n L = merge(L, f(dp1[u], id));\n dfs2(u, v);\n }\n else { L = merge(L, f(dp2[v], id)); }\n }\n ans[v] = g(L, v);\n}\n\n// https://onlinejudge.u-aizu.ac.jp/services/ice/?problemId=1595\nvoid solve1() {\n ll N; cin >> N;\n dp1.assign(N, e());\n dp2.assign(N, e());\n ans.resize(N);\n edge.assign(N, vector<pair<int, int>>());\n ll edge_id = 0;\n rep(i, N-1) {\n ll u, v; cin >> u >> v;\n u--, v--;\n edge[u].push_back({v, edge_id++});\n edge[v].push_back({u, edge_id++});\n }\n \n dfs1(0, -1);\n dfs2(0, -1);\n \n rep(i, N) {\n ll res = 2 * (N-1) - ans[i];\n cout << res << '\\n';\n }\n \n return;\n}\n\nint main() {\n solve1();\n \n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 23680, "score_of_the_acc": -0.1431, "final_rank": 1 }, { "submission_id": "aoj_1595_10035853", "code_snippet": "// 参考 https://algo-logic.info/tree-dp/\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n/* Rerooting: 全方位木 DP\n 問題ごとに以下を書き換える\n - 型DPと単位元\n - 型DPに対する二項演算 merge\n - まとめたDPを用いて新たな部分木のDPを計算する add_root\n 計算量: O(N)\n*/\n\n// グラフの定義\nstruct Edge {\n int to;\n long long w;\n};\nusing Graph = vector<vector<Edge>>;\n\nstruct Rerooting {\n /* start 問題ごとに書き換え */\n struct DP { // DP の型\n long long dp;\n };\n const DP identity = DP{0}; // 単位元(末端の値は add_root(identity) になるので注意)\n DP connect(DP d, Edge e){\n return DP{d.dp + e.w};\n }\n DP merge(DP dp_cum, DP d){\n return DP{max(dp_cum.dp, d.dp)};\n }\n DP add_root(DP d){\n return DP{d.dp};\n }\n /* end 問題ごとに書き換え */\n\n vector<vector<DP>> dp; // dp[v][i]: vから出るi番目の有向辺に対応する部分木のDP\n vector<DP> ans; // ans[v]: 頂点vを根とする木の答え\n Graph G;\n\n Rerooting(int N){\n G.resize(N);\n dp.resize(N);\n ans.assign(N, identity);\n }\n\n void add_edge(int a, Edge e) {\n G[a].push_back(e);\n }\n void build() {\n dfs(0); // 普通に木DP\n bfs(0, identity); // 残りの部分木に対応するDPを計算\n }\n\n DP dfs(int v, int p = -1) { // 頂点v, 親p\n DP dp_cum = identity;\n int deg = G[v].size();\n dp[v] = vector<DP>(deg, identity);\n for (int i = 0; i < deg; i++) {\n int u = G[v][i].to;\n if (u == p) continue;\n dp[v][i] = dfs(u, v);\n dp_cum = merge(dp_cum, connect(dp[v][i], G[v][i]));\n }\n return add_root(dp_cum);\n }\n void bfs(int v, const DP& dp_p, int p = -1) { // bfs だが、実装が楽なので中身は dfs になっている\n int deg = G[v].size();\n for (int i = 0; i < deg; i++) { // 前のbfsで計算した有向辺に対応する部分木のDPを保存\n if (G[v][i].to == p) dp[v][i] = dp_p;\n }\n vector<DP> dp_l(deg + 1, identity), dp_r(deg + 1, identity); // 累積merge\n for (int i = 0; i < deg; i++) {\n dp_l[i + 1] = merge(dp_l[i], connect(dp[v][i], G[v][i]));\n }\n for (int i = deg - 1; i >= 0; i--) {\n dp_r[i] = merge(dp_r[i + 1], connect(dp[v][i], G[v][i]));\n }\n\n ans[v] = add_root(dp_l[deg]); // 頂点 v の答え\n\n for (int i = 0; i < deg; i++) { // 一つ隣の頂点に対しても同様に計算\n int u = G[v][i].to;\n if (u == p) continue;\n bfs(u, add_root(merge(dp_l[i], dp_r[i + 1])), v);\n }\n }\n};\n\nint main(){\n int N; cin>>N;\n Rerooting reroot(N);\n for(int i=0;i<N-1;i++){\n int s,t; cin>>s>>t; s--; t--;\n reroot.add_edge(s,Edge{t,1});\n reroot.add_edge(t,Edge{s,1});\n }\n reroot.build();\n for(int i=0;i<N;i++){\n cout<<2*(N-1)-reroot.ans[i].dp<<endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 33396, "score_of_the_acc": -1.1526, "final_rank": 15 } ]
aoj_1596_cpp
Problem J: Char Swap Problem 長さが偶数の文字列 S が与えられる。 あなたは、文字列 S の隣り合った2つの文字を入れ替える操作を何回でも行うことができる。 文字列 S を回文にするためには、最小で何回の操作を行う必要があるだろうか? 回文にすることが不可能な場合は-1を出力せよ。 Input 入力は以下の形式で与えられる。 S 1行に文字列 S が与えられる。 Constraints 2 ≤ | S | ≤ 4 × 10 5 文字列はすべて英小文字で構成されている 文字列の長さは偶数である Output 文字列 S を回文にするための最小の操作回数を1行に出力する。回文にすることが不可能な場合は代わりに-1を1行に出力をする。 Sample Input 1 acca Sample Output 1 0 Sample Input 2 acpcacpc Sample Output 2 3 Sample Input 3 aizu Sample Output 3 -1
[ { "submission_id": "aoj_1596_10702848", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n\n// Ordered Set (PBDS)\ntemplate<typename T>\nusing ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n\n// Macros\n#define rep(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i++ : i--)\n#define deb(x) cout << #x << \"=\" << x << endl\n#define ll long long\n#define sz(x) (int)(x).size()\n\nvoid solve() {\n string s;\n cin >> s;\n\n int n = sz(s);\n map<int, int> cnt;\n map<int, set<int>> pos;\n bool ok = true;\n\n // Count character frequencies and store positions\n for (int i = 0; i < n; ++i) {\n int c = s[i] - 'a';\n cnt[c]++;\n pos[c].insert(i);\n }\n\n // Check if all character counts are even (palindrome check)\n for (int i = 0; i < 26; ++i) {\n if (cnt[i] % 2 != 0) {\n ok = false;\n break;\n }\n }\n\n if (!ok) {\n cout << \"-1\\n\";\n return;\n }\n\n ordered_set<int> ord;\n ll ans = 0;\n\n for (int i = 0; i < n / 2; ++i) {\n int c = s[i] - 'a';\n int idx = *prev(pos[c].end()); // Get last occurrence (same as rbegin() but usable)\n pos[c].erase(idx);\n\n int left_shift = ord.order_of_key(idx);\n int right_shift = sz(ord) - left_shift;\n int expected_pos = n - 1 - i;\n int new_idx = idx - left_shift + right_shift;\n if(expected_pos == new_idx) continue;\n ans += n - 1 - i - new_idx;\n ord.insert(idx);\n }\n\n cout << ans << \"\\n\";\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int t = 1;\n // cin >> t;\n while (t--) solve();\n\n return 0;\n}", "accuracy": 0.1875, "time_ms": 170, "memory_kb": 22096, "score_of_the_acc": -1.4081, "final_rank": 19 }, { "submission_id": "aoj_1596_10702844", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n#define int long long\n// Ordered Set (PBDS)\ntemplate<typename T>\nusing ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n\n// Macros\n#define rep(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i++ : i--)\n#define deb(x) cout << #x << \"=\" << x << endl\n#define ll long long\n#define sz(x) (int)(x).size()\n\nvoid solve() {\n string s;\n cin >> s;\n\n int n = sz(s);\n map<int, int> cnt;\n map<int, set<int>> pos;\n bool ok = true;\n\n // Count character frequencies and store positions\n for (int i = 0; i < n; ++i) {\n int c = s[i] - 'a';\n cnt[c]++;\n pos[c].insert(i);\n }\n\n // Check if all character counts are even (palindrome check)\n for (int i = 0; i < 26; ++i) {\n if (cnt[i] % 2 != 0) {\n ok = false;\n break;\n }\n }\n\n if (!ok) {\n cout << \"-1\\n\";\n return;\n }\n\n ordered_set<int> ord;\n ll ans = 0;\n\n for (int i = 0; i < n / 2; ++i) {\n int c = s[i] - 'a';\n int idx = *prev(pos[c].end()); // Get last occurrence (same as rbegin() but usable)\n pos[c].erase(idx);\n\n int left_shift = ord.order_of_key(idx);\n int right_shift = sz(ord) - left_shift;\n int expected_pos = n - 1 - i;\n int new_idx = idx - left_shift + right_shift;\n if(expected_pos == new_idx) continue;\n ans += n - 1 - i - new_idx;\n ord.insert(idx);\n }\n\n cout << ans << \"\\n\";\n}\nsigned main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int t = 1;\n // cin >> t;\n while (t--) solve();\n\n return 0;\n}", "accuracy": 0.1875, "time_ms": 120, "memory_kb": 25424, "score_of_the_acc": -1.3929, "final_rank": 18 }, { "submission_id": "aoj_1596_10702840", "code_snippet": "//████████╗██╗███╗░░██╗  ██╗░░░░░███████╗\n//╚══██╔══╝██║████╗░██║  ██║░░░░░██╔════╝\n//░░░██║░░░██║██╔██╗██║  ██║░░░░░█████╗░░\n//░░░██║░░░██║██║╚████║  ██║░░░░░██╔══╝░░\n//░░░██║░░░██║██║░╚███║  ███████╗███████╗\n//░░░╚═╝░░░╚═╝╚═╝░░╚══╝  ╚══════╝╚══════╝\n// __________________\n// | ________________ |\n// || ____ ||\n// || /\\ | ||\n// || /__\\ | ||\n// || / \\ |____ ||\n// ||________________||\n// |__________________|\n// \\###################\\\n// \\###################\\\n// \\ ____ \\\n// \\_______\\___\\_______\\\n// An AC a day keeps the doctor away.\n\n#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <ctime>\n#include <cassert>\n#include <complex>\n#include <string>\n#include <cstring>\n#include <chrono>\n#include <random>\n#include <bitset>\n#include <iomanip>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <array>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\ntemplate<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n#define vt vector\n#define all(x) begin(x), end(x)\n#define allr(x) rbegin(x), rend(x)\n#define ub upper_bound\n#define lb lower_bound\n#define db double\n#define ld long db\n#define ll long long\n#define ull unsigned long long\n#define vll vt<ll> \n#define vvll vt<vll>\n#define pll pair<ll, ll> \n#define vpll vt<pll>\n#define vvpll vt<vpll>\n#define vc vt<char> \n#define vvc vt<vc>\n#define vi vt<int>\n#define vvi vt<vi>\n#define vvvi vt<vvi>\n#define pii pair<int, int>\n#define vpii vt<pii>\n#define vs vt<string>\n#define vvs vt<vs>\n#define vb vt<bool>\n#define vvb vt<vb>\n#define vvpii vt<vpii>\n#define vd vt<db>\n#define ar(x) array<int, x>\n#define var(x) vt<ar(x)>\n#define vvar(x) vt<var(x)>\n#define al(x) array<ll, x>\n#define vall(x) vt<al(x)>\n#define vvall(x) vt<vall(x)>\n#define mset(m, v) memset(m, v, sizeof(m))\n#define pb push_back\n#define ff first\n#define ss second\n#define sv string_view\n#define MP make_pair\n#define MT make_tuple\n#define rsz resize\n#define sum(x) (ll)accumulate(all(x), 0LL)\n#define srt(x) sort(all(x))\n#define srtR(x) sort(allr(x))\n#define srtU(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())\n#define SORTED(x) is_sorted(all(x))\n#define rev(x) reverse(all(x))\n#define MAX(a) *max_element(all(a)) \n#define MIN(a) *min_element(all(a))\n#define ROTATE(a, p) rotate(begin(a), begin(a) + p, end(a))\n#define i128 __int128\n\n//SGT DEFINE\n#define lc i * 2 + 1\n#define rc i * 2 + 2\n#define lp lc, left, middle\n#define rp rc, middle + 1, right\n#define entireTree 0, 0, n - 1\n#define midPoint left + (right - left) / 2\n#define pushDown push(i, left, right)\n#define iter int i, int left, int right\n\n#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)\n\nstruct custom {\n static const uint64_t C = 0x9e3779b97f4a7c15; const uint32_t RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();\n size_t operator()(uint64_t x) const { return __builtin_bswap64((x ^ RANDOM) * C); }\n size_t operator()(const std::string& s) const { size_t hash = std::hash<std::string>{}(s); return hash ^ RANDOM; } };\ntemplate <class K, class V> using umap = std::unordered_map<K, V, custom>; template <class K> using uset = std::unordered_set<K, custom>;\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\n \ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, array<T, N>& arr) {\n for (size_t i = 0; i < N; i++) { is >> arr[i]; } return is;\n}\n\ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, vector<array<T, N>>& vec) {\n for (auto &arr : vec) { is >> arr; } return is;\n}\n\ninline std::ostream& operator<<(std::ostream& os, i128 x) {\n if(x == 0) { os << '0'; return os; } if(x < 0) { os << '-'; x = -x; }\n string s; while (x > 0) { int digit = int(x % 10); s.pb(char('0' + digit)); x /= 10; }\n rev(s); os << s; return os;\n}\n \ntemplate <typename T1, typename T2> istream &operator>>(istream& in, pair<T1, T2>& input) { return in >> input.ff >> input.ss; }\n \ntemplate <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &el : v) in >> el; return in; }\n\ntemplate<class T>\nvoid output_vector(vt<T>& a, int off_set = 0) {\n int n = a.size();\n for(int i = off_set; i < n; i++) {\n cout << a[i] << (i == n - 1 ? '\\n' : ' ');\n }\n}\n\ntemplate<typename T, typename Compare>\nvi closest_left(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = 0; i < n; i++) {\n auto& j = closest[i];\n while(j && cmp(a[i], a[j - 1])) j = closest[j - 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename Compare> // auto right = closest_right<int>(a, std::less<int>());\nvi closest_right(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = n - 1; i >= 0; i--) {\n auto& j = closest[i];\n while(j < n - 1 && cmp(a[i], a[j + 1])) j = closest[j + 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename V = string>\nvt<pair<T, int>> encode(const V& s) {\n vt<pair<T, int>> seg;\n for(auto& ch : s) {\n if(seg.empty() || ch != seg.back().ff) seg.pb({ch, 1});\n else seg.back().ss++;\n }\n return seg;\n}\n\n \ntemplate<typename K, typename V>\nauto operator<<(std::ostream &o, const std::map<K, V> &m) -> std::ostream& {\n o << \"{\"; int i = 0;\n for (const auto &[key, value] : m) { if (i++) o << \" , \"; o << key << \" : \" << value; }\n return o << \"}\";\n}\n\n#ifdef LOCAL\n#define debug(x...) debug_out(#x, x)\nvoid debug_out(const char* names) { std::cerr << std::endl; }\ntemplate <typename T, typename... Args>\nvoid debug_out(const char* names, T value, Args... args) {\n const char* comma = strchr(names, ',');\n std::cerr << \"[\" << (comma ? std::string(names, comma) : names) << \" = \" << value << \"]\";\n if (sizeof...(args)) { std::cerr << \", \"; debug_out(comma + 1, args...); } \n else { std::cerr << std::endl; }\n}\ntemplate<typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& o, const std::pair<T1, T2>& p) { return o << \"{\" << p.ff << \" , \" << p.ss << \"}\"; }\nauto operator<<(auto &o, const auto &x) -> decltype(end(x), o) {\n o << \"{\"; int i = 0; for (const auto &e : x) { if (i++) o << \" , \"; o << e; } return o << \"}\";\n} // remove for leetcode\n#include <sys/resource.h>\n#include <sys/time.h>\nvoid printMemoryUsage() {\n struct rusage usage;\n getrusage(RUSAGE_SELF, &usage);\n double memoryMB = usage.ru_maxrss / 1024.0;\n cerr << \"Memory usage: \" << memoryMB << \" MB\" << \"\\n\";\n}\n\n#define startClock clock_t tStart = clock();\n#define endClock std::cout << std::fixed << std::setprecision(10) << \"\\nTime Taken: \" << (double)(clock() - tStart) / CLOCKS_PER_SEC << \" seconds\" << std::endl;\n#else\n#define debug(...)\n#define startClock\n#define endClock\n\n#endif\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n#define eps 1e-9\n#define M_PI 3.14159265358979323846\nconst static string pi = \"3141592653589793238462643383279\";\nconst static ll INF = 1LL << 62;\nconst static int inf = 1e9 + 100;\nconst static int MK = 20;\nconst static int MX = 1e5 + 5;\nll gcd(ll a, ll b) { while (b != 0) { ll temp = b; b = a % b; a = temp; } return a; }\nll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }\nll floor(ll a, ll b) { if(b < 0) a = -a, b = -b; if (a >= 0) return a / b; return a / b - (a % b ? 1 : 0); }\nll ceil(ll a, ll b) { if (b < 0) a = -a, b = -b; if (a >= 0) return (a + b - 1) / b; return a / b; }\nint pct(ll x) { return __builtin_popcountll(x); }\nll have_bit(ll x, int b) { return x & (1LL << b); }\nint min_bit(ll x) { return __builtin_ctzll(x); }\nint max_bit(ll x) { return 63 - __builtin_clzll(x); } \nconst vvi dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; // UP, DOWN, LEFT, RIGHT\nconst vvi knight_dirs = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}}; // knight dirs\nconst vc dirChar = {'U', 'D', 'L', 'R'};\nint modExpo(ll base, ll exp, ll mod) { ll res = 1; base %= mod; while(exp) { if(exp & 1) res = (res * base) % mod; base = (base * base) % mod; exp >>= 1; } return res; }\nll extended_gcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extended_gcd(b, a % b, y, x); y -= (a / b) * x; return d; }\nint modExpo_on_string(ll a, string exp, int mod) { ll b = 0; for(auto& ch : exp) b = (b * 10 + (ch - '0')) % (mod - 1); return modExpo(a, b, mod); }\nll sum_even_series(ll n) { return (n / 2) * (n / 2 + 1);} \nll sum_odd_series(ll n) {return n - sum_even_series(n);} // sum of first n odd number is n ^ 2\nll sum_of_square(ll n) { return n * (n + 1) * (2 * n + 1) / 6; } // sum of 1 + 2 * 2 + 3 * 3 + 4 * 4 + ... + n * n\nstring make_lower(const string& t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return tolower(c); }); return s; }\nstring make_upper(const string&t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return toupper(c); }); return s; }\nll sqrt(ll n) { ll t = sqrtl(n); while(t * t < n) t++; while(t * t > n) t--; return t;}\ntemplate<typename T> T geometric_sum(ll n, ll k) { return (1 - T(n).pow(k + 1)) / (1 - n); } // return n^1 + n^2 + n^3 + n^4 + n^5 + ... + n^k\ntemplate<typename T> T geometric_power(ll p, ll k) { return (T(p).pow(k + 1) - 1) / T(p - 1); } // p^1 + p^2 + p^3 + ... + p^k\nbool is_perm(ll sm, ll square_sum, ll len) {return sm == len * (len + 1) / 2 && square_sum == len * (len + 1) * (2 * len + 1) / 6;} // determine if an array is a permutation base on sum and square_sum\nbool is_vowel(char c) {return c == 'a' || c == 'e' || c == 'u' || c == 'o' || c == 'i';}\n\ntemplate<class T, typename F = function<T(const T&, const T&)>>\nclass FW { \n public: \n int n, N;\n vt<T> root; \n T DEFAULT;\n F func;\n FW() {}\n FW(int n, T DEFAULT, F func = [](const T& a, const T& b) {return a + b;}) : func(func) { \n this->n = n; \n this->DEFAULT = DEFAULT;\n N = log2(n);\n root.rsz(n, DEFAULT);\n }\n \n inline void update_at(int id, T val) { \n assert(id >= 0);\n while(id < n) { \n root[id] = func(root[id], val);\n id |= (id + 1);\n }\n }\n \n inline T get(int id) { \n assert(id < n);\n T res = DEFAULT;\n while(id >= 0) { \n res = func(res, root[id]);\n id = (id & (id + 1)) - 1;\n }\n return res;\n }\n\n inline T queries_range(int left, int right) { \n return get(right) - get(left - 1);\n }\n\n inline T queries_at(int i) {\n return queries_range(i, i);\n }\n\n inline void update_range(int l, int r, T val) {\n\t\tif(l > r) return;\n update_at(l, val), update_at(r + 1, -val);\n }\n\t\n\tinline void reset() {\n\t\troot.assign(n, DEFAULT);\n\t}\n\n\tll select(ll k) {\n ll pos = -1;\n T acc = DEFAULT;\n for(ll bit = 1LL << N; bit > 0; bit >>= 1) {\n ll np = pos + bit;\n if(np < n) {\n T cand = acc + root[np];\n if(cand < k) {\n acc = cand;\n pos = np;\n }\n }\n }\n return pos + 1;\n }\n\n};\n\nvoid solve() {\n string s; cin >> s;\n const int K = 26;\n int n = s.size();\n if(n & 1) {\n cout << -1 << '\\n';\n return;\n }\n deque<int> q[K];\n for(int i = 0; i < n; i++) {\n q[s[i] - 'a'].pb(i);\n }\n for(int i = 0; i < K; i++) {\n if(q[i].size() & 1) {\n cout << -1 << '\\n';\n return;\n }\n }\n int i = 0, j = n - 1;\n ll res = 0;\n FW<int> root(n, 0);\n vi remove(n);\n auto erase = [&](int x) -> void {\n remove[q[x].front()] = remove[q[x].back()] = true;\n root.update_at(q[x].front(), 1);\n root.update_at(q[x].back(), 1);\n q[x].pop_back();\n q[x].pop_front();\n };\n int cnt = 0;\n while(i <= j && cnt < n) {\n while(remove[i]) i++;\n while(remove[j]) j--;\n cnt += 2;\n int l = s[i] - 'a';\n int r = s[j] - 'a';\n if(l == r) {\n erase(l);\n continue;\n }\n int L = q[l].back();\n int R = q[r].front();\n int left_cost = n - (L + root.queries_range(L, n - 1)) - 1;\n int right_cost = R - root.get(R);\n debug(l, r, i, j, left_cost, right_cost);\n res += min(left_cost, right_cost);\n if(left_cost < right_cost) erase(l);\n else erase(r);\n }\n cout << res << '\\n';\n}\n\nsigned main() {\n // careful for overflow, check for long long, use unsigned long long for random generator\n // when mle, look if problem require read in file, typically old problems\n IOS;\n startClock\n //generatePrime();\n\n int t = 1;\n //cin >> t;\n for(int i = 1; i <= t; i++) { \n //cout << \"Case #\" << i << \": \"; \n solve();\n }\n\n endClock\n #ifdef LOCAL\n printMemoryUsage();\n #endif\n\n return 0;\n}\n\n//███████████████████████████████████████████████████████████████████████████████████████████████████████\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░░░░░░░█\n//█░░▄▀░░█████████░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░░░░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░█████████░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░▄▀░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//███████████████████████████████████████████████████████████████████████████████████████████████████████", "accuracy": 1, "time_ms": 20, "memory_kb": 8564, "score_of_the_acc": -0.2081, "final_rank": 3 }, { "submission_id": "aoj_1596_10702824", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nclass FenwickTree{\n private:\n vector<int>bit;\n int n;\n public:\n FenwickTree(int n){\n this->n = n;\n bit.resize(n + 1,0);\n }\n void update(int i, int delta){\n while(i <= n){\n bit[i] += delta;\n i += (i & -i);\n }\n }\n int query(int i){\n int sum = 0;\n while(i > 0){\n sum += bit[i];\n i -= (i & -i);\n }\n return sum;\n }\n};\nlong long solve(string s){\n int n = s.length();\n vector<bool>alive(n, true);\n deque<int>pos[26];\n int left = 0, right = n - 1;\n FenwickTree bit(n);\n for(int i = 1; i <= n; i++){\n bit.update(i, 1);\n }\n auto remove = [&](int i){\n alive[i] = false;\n bit.update(i + 1, -1);\n };\n for(int i = 0; i < n; i++){\n s[i] -= 'a';\n pos[s[i]].push_back(i);\n }\n int odd_count = 0;\n for(int i = 0; i < n; i++){\n odd_count += pos[s[i]].size() & 1;\n }\n if(odd_count != (n & 1)){\n return -1;\n }\n int swaps = 0;\n while(left < right){\n if(!alive[left]){\n left++;\n }else if(!alive[right]){\n right--;\n }else{\n int char_left = s[left];\n int char_right = s[right];\n int cost_left = bit.query(n) - bit.query(pos[char_left].back() + 1);\n int cost_right = bit.query(pos[char_right].front() + 1) - 1;\n int ch;\n if(cost_left < cost_right){\n ch = char_left;\n swaps += cost_left;\n }else{\n ch = char_right;\n swaps += cost_right;\n }\n remove(pos[ch].front());\n pos[ch].pop_front();\n remove(pos[ch].back());\n pos[ch].pop_back();\n }\n \n }\n return swaps;\n}\nint32_t main(){\n string s;\n cin >> s;\n cout << solve(s) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 10488, "score_of_the_acc": -0.3026, "final_rank": 8 }, { "submission_id": "aoj_1596_10702808", "code_snippet": "//████████╗██╗███╗░░██╗  ██╗░░░░░███████╗\n//╚══██╔══╝██║████╗░██║  ██║░░░░░██╔════╝\n//░░░██║░░░██║██╔██╗██║  ██║░░░░░█████╗░░\n//░░░██║░░░██║██║╚████║  ██║░░░░░██╔══╝░░\n//░░░██║░░░██║██║░╚███║  ███████╗███████╗\n//░░░╚═╝░░░╚═╝╚═╝░░╚══╝  ╚══════╝╚══════╝\n// __________________\n// | ________________ |\n// || ____ ||\n// || /\\ | ||\n// || /__\\ | ||\n// || / \\ |____ ||\n// ||________________||\n// |__________________|\n// \\###################\\\n// \\###################\\\n// \\ ____ \\\n// \\_______\\___\\_______\\\n// An AC a day keeps the doctor away.\n\n#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <ctime>\n#include <cassert>\n#include <complex>\n#include <string>\n#include <cstring>\n#include <chrono>\n#include <random>\n#include <bitset>\n#include <iomanip>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <array>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\ntemplate<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n#define vt vector\n#define all(x) begin(x), end(x)\n#define allr(x) rbegin(x), rend(x)\n#define ub upper_bound\n#define lb lower_bound\n#define db double\n#define ld long db\n#define ll long long\n#define ull unsigned long long\n#define vll vt<ll> \n#define vvll vt<vll>\n#define pll pair<ll, ll> \n#define vpll vt<pll>\n#define vvpll vt<vpll>\n#define vc vt<char> \n#define vvc vt<vc>\n#define vi vt<int>\n#define vvi vt<vi>\n#define vvvi vt<vvi>\n#define pii pair<int, int>\n#define vpii vt<pii>\n#define vs vt<string>\n#define vvs vt<vs>\n#define vb vt<bool>\n#define vvb vt<vb>\n#define vvpii vt<vpii>\n#define vd vt<db>\n#define ar(x) array<int, x>\n#define var(x) vt<ar(x)>\n#define vvar(x) vt<var(x)>\n#define al(x) array<ll, x>\n#define vall(x) vt<al(x)>\n#define vvall(x) vt<vall(x)>\n#define mset(m, v) memset(m, v, sizeof(m))\n#define pb push_back\n#define ff first\n#define ss second\n#define sv string_view\n#define MP make_pair\n#define MT make_tuple\n#define rsz resize\n#define sum(x) (ll)accumulate(all(x), 0LL)\n#define srt(x) sort(all(x))\n#define srtR(x) sort(allr(x))\n#define srtU(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())\n#define SORTED(x) is_sorted(all(x))\n#define rev(x) reverse(all(x))\n#define MAX(a) *max_element(all(a)) \n#define MIN(a) *min_element(all(a))\n#define ROTATE(a, p) rotate(begin(a), begin(a) + p, end(a))\n#define i128 __int128\n\n//SGT DEFINE\n#define lc i * 2 + 1\n#define rc i * 2 + 2\n#define lp lc, left, middle\n#define rp rc, middle + 1, right\n#define entireTree 0, 0, n - 1\n#define midPoint left + (right - left) / 2\n#define pushDown push(i, left, right)\n#define iter int i, int left, int right\n\n#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)\n\nstruct custom {\n static const uint64_t C = 0x9e3779b97f4a7c15; const uint32_t RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();\n size_t operator()(uint64_t x) const { return __builtin_bswap64((x ^ RANDOM) * C); }\n size_t operator()(const std::string& s) const { size_t hash = std::hash<std::string>{}(s); return hash ^ RANDOM; } };\ntemplate <class K, class V> using umap = std::unordered_map<K, V, custom>; template <class K> using uset = std::unordered_set<K, custom>;\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\n \ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, array<T, N>& arr) {\n for (size_t i = 0; i < N; i++) { is >> arr[i]; } return is;\n}\n\ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, vector<array<T, N>>& vec) {\n for (auto &arr : vec) { is >> arr; } return is;\n}\n\ninline std::ostream& operator<<(std::ostream& os, i128 x) {\n if(x == 0) { os << '0'; return os; } if(x < 0) { os << '-'; x = -x; }\n string s; while (x > 0) { int digit = int(x % 10); s.pb(char('0' + digit)); x /= 10; }\n rev(s); os << s; return os;\n}\n \ntemplate <typename T1, typename T2> istream &operator>>(istream& in, pair<T1, T2>& input) { return in >> input.ff >> input.ss; }\n \ntemplate <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &el : v) in >> el; return in; }\n\ntemplate<class T>\nvoid output_vector(vt<T>& a, int off_set = 0) {\n int n = a.size();\n for(int i = off_set; i < n; i++) {\n cout << a[i] << (i == n - 1 ? '\\n' : ' ');\n }\n}\n\ntemplate<typename T, typename Compare>\nvi closest_left(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = 0; i < n; i++) {\n auto& j = closest[i];\n while(j && cmp(a[i], a[j - 1])) j = closest[j - 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename Compare> // auto right = closest_right<int>(a, std::less<int>());\nvi closest_right(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = n - 1; i >= 0; i--) {\n auto& j = closest[i];\n while(j < n - 1 && cmp(a[i], a[j + 1])) j = closest[j + 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename V = string>\nvt<pair<T, int>> encode(const V& s) {\n vt<pair<T, int>> seg;\n for(auto& ch : s) {\n if(seg.empty() || ch != seg.back().ff) seg.pb({ch, 1});\n else seg.back().ss++;\n }\n return seg;\n}\n\n \ntemplate<typename K, typename V>\nauto operator<<(std::ostream &o, const std::map<K, V> &m) -> std::ostream& {\n o << \"{\"; int i = 0;\n for (const auto &[key, value] : m) { if (i++) o << \" , \"; o << key << \" : \" << value; }\n return o << \"}\";\n}\n\n#ifdef LOCAL\n#define debug(x...) debug_out(#x, x)\nvoid debug_out(const char* names) { std::cerr << std::endl; }\ntemplate <typename T, typename... Args>\nvoid debug_out(const char* names, T value, Args... args) {\n const char* comma = strchr(names, ',');\n std::cerr << \"[\" << (comma ? std::string(names, comma) : names) << \" = \" << value << \"]\";\n if (sizeof...(args)) { std::cerr << \", \"; debug_out(comma + 1, args...); } \n else { std::cerr << std::endl; }\n}\ntemplate<typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& o, const std::pair<T1, T2>& p) { return o << \"{\" << p.ff << \" , \" << p.ss << \"}\"; }\nauto operator<<(auto &o, const auto &x) -> decltype(end(x), o) {\n o << \"{\"; int i = 0; for (const auto &e : x) { if (i++) o << \" , \"; o << e; } return o << \"}\";\n} // remove for leetcode\n#include <sys/resource.h>\n#include <sys/time.h>\nvoid printMemoryUsage() {\n struct rusage usage;\n getrusage(RUSAGE_SELF, &usage);\n double memoryMB = usage.ru_maxrss / 1024.0;\n cerr << \"Memory usage: \" << memoryMB << \" MB\" << \"\\n\";\n}\n\n#define startClock clock_t tStart = clock();\n#define endClock std::cout << std::fixed << std::setprecision(10) << \"\\nTime Taken: \" << (double)(clock() - tStart) / CLOCKS_PER_SEC << \" seconds\" << std::endl;\n#else\n#define debug(...)\n#define startClock\n#define endClock\n\n#endif\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n#define eps 1e-9\n#define M_PI 3.14159265358979323846\nconst static string pi = \"3141592653589793238462643383279\";\nconst static ll INF = 1LL << 62;\nconst static int inf = 1e9 + 100;\nconst static int MK = 20;\nconst static int MX = 1e5 + 5;\nll gcd(ll a, ll b) { while (b != 0) { ll temp = b; b = a % b; a = temp; } return a; }\nll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }\nll floor(ll a, ll b) { if(b < 0) a = -a, b = -b; if (a >= 0) return a / b; return a / b - (a % b ? 1 : 0); }\nll ceil(ll a, ll b) { if (b < 0) a = -a, b = -b; if (a >= 0) return (a + b - 1) / b; return a / b; }\nint pct(ll x) { return __builtin_popcountll(x); }\nll have_bit(ll x, int b) { return x & (1LL << b); }\nint min_bit(ll x) { return __builtin_ctzll(x); }\nint max_bit(ll x) { return 63 - __builtin_clzll(x); } \nconst vvi dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; // UP, DOWN, LEFT, RIGHT\nconst vvi knight_dirs = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}}; // knight dirs\nconst vc dirChar = {'U', 'D', 'L', 'R'};\nint modExpo(ll base, ll exp, ll mod) { ll res = 1; base %= mod; while(exp) { if(exp & 1) res = (res * base) % mod; base = (base * base) % mod; exp >>= 1; } return res; }\nll extended_gcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extended_gcd(b, a % b, y, x); y -= (a / b) * x; return d; }\nint modExpo_on_string(ll a, string exp, int mod) { ll b = 0; for(auto& ch : exp) b = (b * 10 + (ch - '0')) % (mod - 1); return modExpo(a, b, mod); }\nll sum_even_series(ll n) { return (n / 2) * (n / 2 + 1);} \nll sum_odd_series(ll n) {return n - sum_even_series(n);} // sum of first n odd number is n ^ 2\nll sum_of_square(ll n) { return n * (n + 1) * (2 * n + 1) / 6; } // sum of 1 + 2 * 2 + 3 * 3 + 4 * 4 + ... + n * n\nstring make_lower(const string& t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return tolower(c); }); return s; }\nstring make_upper(const string&t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return toupper(c); }); return s; }\nll sqrt(ll n) { ll t = sqrtl(n); while(t * t < n) t++; while(t * t > n) t--; return t;}\ntemplate<typename T> T geometric_sum(ll n, ll k) { return (1 - T(n).pow(k + 1)) / (1 - n); } // return n^1 + n^2 + n^3 + n^4 + n^5 + ... + n^k\ntemplate<typename T> T geometric_power(ll p, ll k) { return (T(p).pow(k + 1) - 1) / T(p - 1); } // p^1 + p^2 + p^3 + ... + p^k\nbool is_perm(ll sm, ll square_sum, ll len) {return sm == len * (len + 1) / 2 && square_sum == len * (len + 1) * (2 * len + 1) / 6;} // determine if an array is a permutation base on sum and square_sum\nbool is_vowel(char c) {return c == 'a' || c == 'e' || c == 'u' || c == 'o' || c == 'i';}\n\nvoid solve() {\n string s; cin >> s;\n const int K = 26;\n int n = s.size();\n if(n & 1) {\n cout << -1 << '\\n';\n return;\n }\n deque<int> q[K];\n for(int i = 0; i < n; i++) {\n q[s[i] - 'a'].pb(i);\n }\n for(int i = 0; i < K; i++) {\n if(q[i].size() & 1) {\n cout << -1 << '\\n';\n return;\n }\n }\n int i = 0, j = n - 1;\n ll res = 0;\n ordered_set<int> S;\n for(int i = 0; i < n; i++) {\n S.insert(i);\n }\n while(!S.empty()) {\n int l = s[*S.begin()] - 'a';\n int r = s[*S.rbegin()] - 'a';\n if(l == r) {\n S.erase(q[l].back());\n S.erase(q[l].front());\n q[l].pop_back();\n q[r].pop_front();\n continue;\n }\n int L = S.order_of_key(q[l].back()), R = S.order_of_key(q[r].front());\n int left_cost = S.size() - L - 1;\n int right_cost = R;\n if(left_cost < right_cost) {\n res += left_cost;\n S.erase(q[l].front());\n S.erase(q[l].back());\n q[l].pop_back();\n q[l].pop_front();\n } else {\n res += right_cost;\n S.erase(q[r].front());\n S.erase(q[r].back());\n q[r].pop_back();\n q[r].pop_front();\n }\n }\n cout << res << '\\n';\n}\n\nsigned main() {\n // careful for overflow, check for long long, use unsigned long long for random generator\n // when mle, look if problem require read in file, typically old problems\n IOS;\n startClock\n //generatePrime();\n\n int t = 1;\n //cin >> t;\n for(int i = 1; i <= t; i++) { \n //cout << \"Case #\" << i << \": \"; \n solve();\n }\n\n endClock\n #ifdef LOCAL\n printMemoryUsage();\n #endif\n\n return 0;\n}\n\n//███████████████████████████████████████████████████████████████████████████████████████████████████████\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░░░░░░░█\n//█░░▄▀░░█████████░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░░░░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░█████████░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░▄▀░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//███████████████████████████████████████████████████████████████████████████████████████████████████████", "accuracy": 1, "time_ms": 290, "memory_kb": 23940, "score_of_the_acc": -1.9272, "final_rank": 10 }, { "submission_id": "aoj_1596_10702801", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n\n// Ordered Set (PBDS)\ntemplate<typename T>\nusing ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n\n// Macros\n#define rep(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i++ : i--)\n#define deb(x) cout << #x << \"=\" << x << endl\n#define ll long long\n#define sz(x) (int)(x).size()\n\nvoid solve() {\n string s;\n cin >> s;\n\n int n = sz(s);\n map<int, int> cnt;\n map<int, set<int>> pos;\n bool ok = true;\n\n // Count character frequencies and store positions\n for (int i = 0; i < n; ++i) {\n int c = s[i] - 'a';\n cnt[c]++;\n pos[c].insert(i);\n }\n\n // Check if all character counts are even (palindrome check)\n for (int i = 0; i < 26; ++i) {\n if (cnt[i] % 2 != 0) {\n ok = false;\n break;\n }\n }\n\n if (!ok) {\n cout << \"-1\\n\";\n return;\n }\n\n ordered_set<int> ord;\n ll ans = 0;\n\n for (int i = 0; i < n / 2; ++i) {\n int c = s[i] - 'a';\n int idx = *prev(pos[c].end()); // Get last occurrence (same as rbegin() but usable)\n pos[c].erase(idx);\n\n int left_shift = ord.order_of_key(idx);\n int right_shift = sz(ord) - left_shift;\n int expected_pos = n - 1 - i;\n int new_idx = idx - left_shift + right_shift;\n if(expected_pos == new_idx) continue;\n ans += n - 1 - i - new_idx;\n ord.insert(idx);\n }\n\n cout << ans << \"\\n\";\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n int t = 1;\n // cin >> t;\n while (t--) solve();\n\n return 0;\n}", "accuracy": 0.1875, "time_ms": 170, "memory_kb": 22212, "score_of_the_acc": -1.4138, "final_rank": 20 }, { "submission_id": "aoj_1596_10702727", "code_snippet": "//████████╗██╗███╗░░██╗  ██╗░░░░░███████╗\n//╚══██╔══╝██║████╗░██║  ██║░░░░░██╔════╝\n//░░░██║░░░██║██╔██╗██║  ██║░░░░░█████╗░░\n//░░░██║░░░██║██║╚████║  ██║░░░░░██╔══╝░░\n//░░░██║░░░██║██║░╚███║  ███████╗███████╗\n//░░░╚═╝░░░╚═╝╚═╝░░╚══╝  ╚══════╝╚══════╝\n// __________________\n// | ________________ |\n// || ____ ||\n// || /\\ | ||\n// || /__\\ | ||\n// || / \\ |____ ||\n// ||________________||\n// |__________________|\n// \\###################\\\n// \\###################\\\n// \\ ____ \\\n// \\_______\\___\\_______\\\n// An AC a day keeps the doctor away.\n\n#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <ctime>\n#include <cassert>\n#include <complex>\n#include <string>\n#include <cstring>\n#include <chrono>\n#include <random>\n#include <bitset>\n#include <iomanip>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <array>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\ntemplate<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n#define vt vector\n#define all(x) begin(x), end(x)\n#define allr(x) rbegin(x), rend(x)\n#define ub upper_bound\n#define lb lower_bound\n#define db double\n#define ld long db\n#define ll long long\n#define ull unsigned long long\n#define vll vt<ll> \n#define vvll vt<vll>\n#define pll pair<ll, ll> \n#define vpll vt<pll>\n#define vvpll vt<vpll>\n#define vc vt<char> \n#define vvc vt<vc>\n#define vi vt<int>\n#define vvi vt<vi>\n#define vvvi vt<vvi>\n#define pii pair<int, int>\n#define vpii vt<pii>\n#define vs vt<string>\n#define vvs vt<vs>\n#define vb vt<bool>\n#define vvb vt<vb>\n#define vvpii vt<vpii>\n#define vd vt<db>\n#define ar(x) array<int, x>\n#define var(x) vt<ar(x)>\n#define vvar(x) vt<var(x)>\n#define al(x) array<ll, x>\n#define vall(x) vt<al(x)>\n#define vvall(x) vt<vall(x)>\n#define mset(m, v) memset(m, v, sizeof(m))\n#define pb push_back\n#define ff first\n#define ss second\n#define sv string_view\n#define MP make_pair\n#define MT make_tuple\n#define rsz resize\n#define sum(x) (ll)accumulate(all(x), 0LL)\n#define srt(x) sort(all(x))\n#define srtR(x) sort(allr(x))\n#define srtU(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())\n#define SORTED(x) is_sorted(all(x))\n#define rev(x) reverse(all(x))\n#define MAX(a) *max_element(all(a)) \n#define MIN(a) *min_element(all(a))\n#define ROTATE(a, p) rotate(begin(a), begin(a) + p, end(a))\n#define i128 __int128\n\n//SGT DEFINE\n#define lc i * 2 + 1\n#define rc i * 2 + 2\n#define lp lc, left, middle\n#define rp rc, middle + 1, right\n#define entireTree 0, 0, n - 1\n#define midPoint left + (right - left) / 2\n#define pushDown push(i, left, right)\n#define iter int i, int left, int right\n\n#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)\n\nstruct custom {\n static const uint64_t C = 0x9e3779b97f4a7c15; const uint32_t RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();\n size_t operator()(uint64_t x) const { return __builtin_bswap64((x ^ RANDOM) * C); }\n size_t operator()(const std::string& s) const { size_t hash = std::hash<std::string>{}(s); return hash ^ RANDOM; } };\ntemplate <class K, class V> using umap = std::unordered_map<K, V, custom>; template <class K> using uset = std::unordered_set<K, custom>;\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\n \ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, array<T, N>& arr) {\n for (size_t i = 0; i < N; i++) { is >> arr[i]; } return is;\n}\n\ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, vector<array<T, N>>& vec) {\n for (auto &arr : vec) { is >> arr; } return is;\n}\n\ninline std::ostream& operator<<(std::ostream& os, i128 x) {\n if(x == 0) { os << '0'; return os; } if(x < 0) { os << '-'; x = -x; }\n string s; while (x > 0) { int digit = int(x % 10); s.pb(char('0' + digit)); x /= 10; }\n rev(s); os << s; return os;\n}\n \ntemplate <typename T1, typename T2> istream &operator>>(istream& in, pair<T1, T2>& input) { return in >> input.ff >> input.ss; }\n \ntemplate <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &el : v) in >> el; return in; }\n\ntemplate<class T>\nvoid output_vector(vt<T>& a, int off_set = 0) {\n int n = a.size();\n for(int i = off_set; i < n; i++) {\n cout << a[i] << (i == n - 1 ? '\\n' : ' ');\n }\n}\n\ntemplate<typename T, typename Compare>\nvi closest_left(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = 0; i < n; i++) {\n auto& j = closest[i];\n while(j && cmp(a[i], a[j - 1])) j = closest[j - 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename Compare> // auto right = closest_right<int>(a, std::less<int>());\nvi closest_right(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = n - 1; i >= 0; i--) {\n auto& j = closest[i];\n while(j < n - 1 && cmp(a[i], a[j + 1])) j = closest[j + 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename V = string>\nvt<pair<T, int>> encode(const V& s) {\n vt<pair<T, int>> seg;\n for(auto& ch : s) {\n if(seg.empty() || ch != seg.back().ff) seg.pb({ch, 1});\n else seg.back().ss++;\n }\n return seg;\n}\n\n \ntemplate<typename K, typename V>\nauto operator<<(std::ostream &o, const std::map<K, V> &m) -> std::ostream& {\n o << \"{\"; int i = 0;\n for (const auto &[key, value] : m) { if (i++) o << \" , \"; o << key << \" : \" << value; }\n return o << \"}\";\n}\n\n#ifdef LOCAL\n#define debug(x...) debug_out(#x, x)\nvoid debug_out(const char* names) { std::cerr << std::endl; }\ntemplate <typename T, typename... Args>\nvoid debug_out(const char* names, T value, Args... args) {\n const char* comma = strchr(names, ',');\n std::cerr << \"[\" << (comma ? std::string(names, comma) : names) << \" = \" << value << \"]\";\n if (sizeof...(args)) { std::cerr << \", \"; debug_out(comma + 1, args...); } \n else { std::cerr << std::endl; }\n}\ntemplate<typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& o, const std::pair<T1, T2>& p) { return o << \"{\" << p.ff << \" , \" << p.ss << \"}\"; }\nauto operator<<(auto &o, const auto &x) -> decltype(end(x), o) {\n o << \"{\"; int i = 0; for (const auto &e : x) { if (i++) o << \" , \"; o << e; } return o << \"}\";\n} // remove for leetcode\n#include <sys/resource.h>\n#include <sys/time.h>\nvoid printMemoryUsage() {\n struct rusage usage;\n getrusage(RUSAGE_SELF, &usage);\n double memoryMB = usage.ru_maxrss / 1024.0;\n cerr << \"Memory usage: \" << memoryMB << \" MB\" << \"\\n\";\n}\n\n#define startClock clock_t tStart = clock();\n#define endClock std::cout << std::fixed << std::setprecision(10) << \"\\nTime Taken: \" << (double)(clock() - tStart) / CLOCKS_PER_SEC << \" seconds\" << std::endl;\n#else\n#define debug(...)\n#define startClock\n#define endClock\n\n#endif\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n#define eps 1e-9\n#define M_PI 3.14159265358979323846\nconst static string pi = \"3141592653589793238462643383279\";\nconst static ll INF = 1LL << 62;\nconst static int inf = 1e9 + 100;\nconst static int MK = 20;\nconst static int MX = 1e5 + 5;\nll gcd(ll a, ll b) { while (b != 0) { ll temp = b; b = a % b; a = temp; } return a; }\nll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }\nll floor(ll a, ll b) { if(b < 0) a = -a, b = -b; if (a >= 0) return a / b; return a / b - (a % b ? 1 : 0); }\nll ceil(ll a, ll b) { if (b < 0) a = -a, b = -b; if (a >= 0) return (a + b - 1) / b; return a / b; }\nint pct(ll x) { return __builtin_popcountll(x); }\nll have_bit(ll x, int b) { return x & (1LL << b); }\nint min_bit(ll x) { return __builtin_ctzll(x); }\nint max_bit(ll x) { return 63 - __builtin_clzll(x); } \nconst vvi dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; // UP, DOWN, LEFT, RIGHT\nconst vvi knight_dirs = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}}; // knight dirs\nconst vc dirChar = {'U', 'D', 'L', 'R'};\nint modExpo(ll base, ll exp, ll mod) { ll res = 1; base %= mod; while(exp) { if(exp & 1) res = (res * base) % mod; base = (base * base) % mod; exp >>= 1; } return res; }\nll extended_gcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extended_gcd(b, a % b, y, x); y -= (a / b) * x; return d; }\nint modExpo_on_string(ll a, string exp, int mod) { ll b = 0; for(auto& ch : exp) b = (b * 10 + (ch - '0')) % (mod - 1); return modExpo(a, b, mod); }\nll sum_even_series(ll n) { return (n / 2) * (n / 2 + 1);} \nll sum_odd_series(ll n) {return n - sum_even_series(n);} // sum of first n odd number is n ^ 2\nll sum_of_square(ll n) { return n * (n + 1) * (2 * n + 1) / 6; } // sum of 1 + 2 * 2 + 3 * 3 + 4 * 4 + ... + n * n\nstring make_lower(const string& t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return tolower(c); }); return s; }\nstring make_upper(const string&t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return toupper(c); }); return s; }\nll sqrt(ll n) { ll t = sqrtl(n); while(t * t < n) t++; while(t * t > n) t--; return t;}\ntemplate<typename T> T geometric_sum(ll n, ll k) { return (1 - T(n).pow(k + 1)) / (1 - n); } // return n^1 + n^2 + n^3 + n^4 + n^5 + ... + n^k\ntemplate<typename T> T geometric_power(ll p, ll k) { return (T(p).pow(k + 1) - 1) / T(p - 1); } // p^1 + p^2 + p^3 + ... + p^k\nbool is_perm(ll sm, ll square_sum, ll len) {return sm == len * (len + 1) / 2 && square_sum == len * (len + 1) * (2 * len + 1) / 6;} // determine if an array is a permutation base on sum and square_sum\nbool is_vowel(char c) {return c == 'a' || c == 'e' || c == 'u' || c == 'o' || c == 'i';}\n\ntemplate<class T, typename F = function<T(const T&, const T&)>>\nclass FW { \n public: \n int n, N;\n vt<T> root; \n T DEFAULT;\n F func;\n FW() {}\n FW(int n, T DEFAULT, F func = [](const T& a, const T& b) {return a + b;}) : func(func) { \n this->n = n; \n this->DEFAULT = DEFAULT;\n N = log2(n);\n root.rsz(n, DEFAULT);\n }\n \n inline void update_at(int id, T val) { \n assert(id >= 0);\n while(id < n) { \n root[id] = func(root[id], val);\n id |= (id + 1);\n }\n }\n \n inline T get(int id) { \n assert(id < n);\n T res = DEFAULT;\n while(id >= 0) { \n res = func(res, root[id]);\n id = (id & (id + 1)) - 1;\n }\n return res;\n }\n\n inline T queries_range(int left, int right) { \n return get(right) - get(left - 1);\n }\n\n inline T queries_at(int i) {\n return queries_range(i, i);\n }\n\n inline void update_range(int l, int r, T val) {\n\t\tif(l > r) return;\n update_at(l, val), update_at(r + 1, -val);\n }\n\t\n\tinline void reset() {\n\t\troot.assign(n, DEFAULT);\n\t}\n\n\tll select(ll k) {\n ll pos = -1;\n T acc = DEFAULT;\n for(ll bit = 1LL << N; bit > 0; bit >>= 1) {\n ll np = pos + bit;\n if(np < n) {\n T cand = acc + root[np];\n if(cand < k) {\n acc = cand;\n pos = np;\n }\n }\n }\n return pos + 1;\n }\n\n};\n\nvoid solve() {\n string s; cin >> s;\n const int K = 26;\n int n = s.size();\n if(n & 1) {\n cout << -1 << '\\n';\n return;\n }\n deque<int> q[K];\n for(int i = 0; i < n; i++) {\n q[s[i] - 'a'].pb(i);\n }\n for(int i = 0; i < K; i++) {\n if(q[i].size() & 1) {\n cout << -1 << '\\n';\n return;\n }\n }\n int i = 0, j = n - 1;\n vi remove(n);\n FW<int> root(n, 0);\n ll res = 0;\n while(i < j) {\n while(remove[i]) i++;\n while(remove[j]) j--;\n if(i >= j) break;\n int L = s[i] - 'a', R = s[j] - 'a';\n if(L == R) {\n q[L].pop_back();\n q[L].pop_front();\n i++, j--;\n continue;\n }\n int left_cost = (j - root.get(j)) - q[L].back();\n int right_cost = q[R].front() - (i + root.get(i));\n if(left_cost < right_cost) {\n res += left_cost;\n root.update_range(q[L].back(), j, 1);\n remove[q[L].back()] = remove[q[L].front()] = true;\n q[L].pop_back();\n q[L].pop_front();\n i++;\n } else {\n res += right_cost;\n root.update_range(i, q[R].front(), 1);\n remove[q[R].front()] = remove[q[R].back()] = true;\n q[R].pop_front();\n q[R].pop_back();\n j--;\n }\n }\n cout << res << '\\n';\n}\n\nsigned main() {\n // careful for overflow, check for long long, use unsigned long long for random generator\n // when mle, look if problem require read in file, typically old problems\n IOS;\n startClock\n //generatePrime();\n\n int t = 1;\n //cin >> t;\n for(int i = 1; i <= t; i++) { \n //cout << \"Case #\" << i << \": \"; \n solve();\n }\n\n endClock\n #ifdef LOCAL\n printMemoryUsage();\n #endif\n\n return 0;\n}\n\n//███████████████████████████████████████████████████████████████████████████████████████████████████████\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░░░░░░░█\n//█░░▄▀░░█████████░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░░░░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░█████████░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░▄▀░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//███████████████████████████████████████████████████████████████████████████████████████████████████████", "accuracy": 0.1875, "time_ms": 10, "memory_kb": 8464, "score_of_the_acc": -0.1675, "final_rank": 14 }, { "submission_id": "aoj_1596_10702721", "code_snippet": "//████████╗██╗███╗░░██╗  ██╗░░░░░███████╗\n//╚══██╔══╝██║████╗░██║  ██║░░░░░██╔════╝\n//░░░██║░░░██║██╔██╗██║  ██║░░░░░█████╗░░\n//░░░██║░░░██║██║╚████║  ██║░░░░░██╔══╝░░\n//░░░██║░░░██║██║░╚███║  ███████╗███████╗\n//░░░╚═╝░░░╚═╝╚═╝░░╚══╝  ╚══════╝╚══════╝\n// __________________\n// | ________________ |\n// || ____ ||\n// || /\\ | ||\n// || /__\\ | ||\n// || / \\ |____ ||\n// ||________________||\n// |__________________|\n// \\###################\\\n// \\###################\\\n// \\ ____ \\\n// \\_______\\___\\_______\\\n// An AC a day keeps the doctor away.\n\n#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <ctime>\n#include <cassert>\n#include <complex>\n#include <string>\n#include <cstring>\n#include <chrono>\n#include <random>\n#include <bitset>\n#include <iomanip>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <array>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\ntemplate<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n#define vt vector\n#define all(x) begin(x), end(x)\n#define allr(x) rbegin(x), rend(x)\n#define ub upper_bound\n#define lb lower_bound\n#define db double\n#define ld long db\n#define ll long long\n#define ull unsigned long long\n#define vll vt<ll> \n#define vvll vt<vll>\n#define pll pair<ll, ll> \n#define vpll vt<pll>\n#define vvpll vt<vpll>\n#define vc vt<char> \n#define vvc vt<vc>\n#define vi vt<int>\n#define vvi vt<vi>\n#define vvvi vt<vvi>\n#define pii pair<int, int>\n#define vpii vt<pii>\n#define vs vt<string>\n#define vvs vt<vs>\n#define vb vt<bool>\n#define vvb vt<vb>\n#define vvpii vt<vpii>\n#define vd vt<db>\n#define ar(x) array<int, x>\n#define var(x) vt<ar(x)>\n#define vvar(x) vt<var(x)>\n#define al(x) array<ll, x>\n#define vall(x) vt<al(x)>\n#define vvall(x) vt<vall(x)>\n#define mset(m, v) memset(m, v, sizeof(m))\n#define pb push_back\n#define ff first\n#define ss second\n#define sv string_view\n#define MP make_pair\n#define MT make_tuple\n#define rsz resize\n#define sum(x) (ll)accumulate(all(x), 0LL)\n#define srt(x) sort(all(x))\n#define srtR(x) sort(allr(x))\n#define srtU(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())\n#define SORTED(x) is_sorted(all(x))\n#define rev(x) reverse(all(x))\n#define MAX(a) *max_element(all(a)) \n#define MIN(a) *min_element(all(a))\n#define ROTATE(a, p) rotate(begin(a), begin(a) + p, end(a))\n#define i128 __int128\n\n//SGT DEFINE\n#define lc i * 2 + 1\n#define rc i * 2 + 2\n#define lp lc, left, middle\n#define rp rc, middle + 1, right\n#define entireTree 0, 0, n - 1\n#define midPoint left + (right - left) / 2\n#define pushDown push(i, left, right)\n#define iter int i, int left, int right\n\n#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)\n\nstruct custom {\n static const uint64_t C = 0x9e3779b97f4a7c15; const uint32_t RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();\n size_t operator()(uint64_t x) const { return __builtin_bswap64((x ^ RANDOM) * C); }\n size_t operator()(const std::string& s) const { size_t hash = std::hash<std::string>{}(s); return hash ^ RANDOM; } };\ntemplate <class K, class V> using umap = std::unordered_map<K, V, custom>; template <class K> using uset = std::unordered_set<K, custom>;\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\n \ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, array<T, N>& arr) {\n for (size_t i = 0; i < N; i++) { is >> arr[i]; } return is;\n}\n\ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, vector<array<T, N>>& vec) {\n for (auto &arr : vec) { is >> arr; } return is;\n}\n\ninline std::ostream& operator<<(std::ostream& os, i128 x) {\n if(x == 0) { os << '0'; return os; } if(x < 0) { os << '-'; x = -x; }\n string s; while (x > 0) { int digit = int(x % 10); s.pb(char('0' + digit)); x /= 10; }\n rev(s); os << s; return os;\n}\n \ntemplate <typename T1, typename T2> istream &operator>>(istream& in, pair<T1, T2>& input) { return in >> input.ff >> input.ss; }\n \ntemplate <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &el : v) in >> el; return in; }\n\ntemplate<class T>\nvoid output_vector(vt<T>& a, int off_set = 0) {\n int n = a.size();\n for(int i = off_set; i < n; i++) {\n cout << a[i] << (i == n - 1 ? '\\n' : ' ');\n }\n}\n\ntemplate<typename T, typename Compare>\nvi closest_left(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = 0; i < n; i++) {\n auto& j = closest[i];\n while(j && cmp(a[i], a[j - 1])) j = closest[j - 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename Compare> // auto right = closest_right<int>(a, std::less<int>());\nvi closest_right(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = n - 1; i >= 0; i--) {\n auto& j = closest[i];\n while(j < n - 1 && cmp(a[i], a[j + 1])) j = closest[j + 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename V = string>\nvt<pair<T, int>> encode(const V& s) {\n vt<pair<T, int>> seg;\n for(auto& ch : s) {\n if(seg.empty() || ch != seg.back().ff) seg.pb({ch, 1});\n else seg.back().ss++;\n }\n return seg;\n}\n\n \ntemplate<typename K, typename V>\nauto operator<<(std::ostream &o, const std::map<K, V> &m) -> std::ostream& {\n o << \"{\"; int i = 0;\n for (const auto &[key, value] : m) { if (i++) o << \" , \"; o << key << \" : \" << value; }\n return o << \"}\";\n}\n\n#ifdef LOCAL\n#define debug(x...) debug_out(#x, x)\nvoid debug_out(const char* names) { std::cerr << std::endl; }\ntemplate <typename T, typename... Args>\nvoid debug_out(const char* names, T value, Args... args) {\n const char* comma = strchr(names, ',');\n std::cerr << \"[\" << (comma ? std::string(names, comma) : names) << \" = \" << value << \"]\";\n if (sizeof...(args)) { std::cerr << \", \"; debug_out(comma + 1, args...); } \n else { std::cerr << std::endl; }\n}\ntemplate<typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& o, const std::pair<T1, T2>& p) { return o << \"{\" << p.ff << \" , \" << p.ss << \"}\"; }\nauto operator<<(auto &o, const auto &x) -> decltype(end(x), o) {\n o << \"{\"; int i = 0; for (const auto &e : x) { if (i++) o << \" , \"; o << e; } return o << \"}\";\n} // remove for leetcode\n#include <sys/resource.h>\n#include <sys/time.h>\nvoid printMemoryUsage() {\n struct rusage usage;\n getrusage(RUSAGE_SELF, &usage);\n double memoryMB = usage.ru_maxrss / 1024.0;\n cerr << \"Memory usage: \" << memoryMB << \" MB\" << \"\\n\";\n}\n\n#define startClock clock_t tStart = clock();\n#define endClock std::cout << std::fixed << std::setprecision(10) << \"\\nTime Taken: \" << (double)(clock() - tStart) / CLOCKS_PER_SEC << \" seconds\" << std::endl;\n#else\n#define debug(...)\n#define startClock\n#define endClock\n\n#endif\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n#define eps 1e-9\n#define M_PI 3.14159265358979323846\nconst static string pi = \"3141592653589793238462643383279\";\nconst static ll INF = 1LL << 62;\nconst static int inf = 1e9 + 100;\nconst static int MK = 20;\nconst static int MX = 1e5 + 5;\nll gcd(ll a, ll b) { while (b != 0) { ll temp = b; b = a % b; a = temp; } return a; }\nll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }\nll floor(ll a, ll b) { if(b < 0) a = -a, b = -b; if (a >= 0) return a / b; return a / b - (a % b ? 1 : 0); }\nll ceil(ll a, ll b) { if (b < 0) a = -a, b = -b; if (a >= 0) return (a + b - 1) / b; return a / b; }\nint pct(ll x) { return __builtin_popcountll(x); }\nll have_bit(ll x, int b) { return x & (1LL << b); }\nint min_bit(ll x) { return __builtin_ctzll(x); }\nint max_bit(ll x) { return 63 - __builtin_clzll(x); } \nconst vvi dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; // UP, DOWN, LEFT, RIGHT\nconst vvi knight_dirs = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}}; // knight dirs\nconst vc dirChar = {'U', 'D', 'L', 'R'};\nint modExpo(ll base, ll exp, ll mod) { ll res = 1; base %= mod; while(exp) { if(exp & 1) res = (res * base) % mod; base = (base * base) % mod; exp >>= 1; } return res; }\nll extended_gcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extended_gcd(b, a % b, y, x); y -= (a / b) * x; return d; }\nint modExpo_on_string(ll a, string exp, int mod) { ll b = 0; for(auto& ch : exp) b = (b * 10 + (ch - '0')) % (mod - 1); return modExpo(a, b, mod); }\nll sum_even_series(ll n) { return (n / 2) * (n / 2 + 1);} \nll sum_odd_series(ll n) {return n - sum_even_series(n);} // sum of first n odd number is n ^ 2\nll sum_of_square(ll n) { return n * (n + 1) * (2 * n + 1) / 6; } // sum of 1 + 2 * 2 + 3 * 3 + 4 * 4 + ... + n * n\nstring make_lower(const string& t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return tolower(c); }); return s; }\nstring make_upper(const string&t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return toupper(c); }); return s; }\nll sqrt(ll n) { ll t = sqrtl(n); while(t * t < n) t++; while(t * t > n) t--; return t;}\ntemplate<typename T> T geometric_sum(ll n, ll k) { return (1 - T(n).pow(k + 1)) / (1 - n); } // return n^1 + n^2 + n^3 + n^4 + n^5 + ... + n^k\ntemplate<typename T> T geometric_power(ll p, ll k) { return (T(p).pow(k + 1) - 1) / T(p - 1); } // p^1 + p^2 + p^3 + ... + p^k\nbool is_perm(ll sm, ll square_sum, ll len) {return sm == len * (len + 1) / 2 && square_sum == len * (len + 1) * (2 * len + 1) / 6;} // determine if an array is a permutation base on sum and square_sum\nbool is_vowel(char c) {return c == 'a' || c == 'e' || c == 'u' || c == 'o' || c == 'i';}\n\ntemplate<class T, typename F = function<T(const T&, const T&)>>\nclass FW { \n public: \n int n, N;\n vt<T> root; \n T DEFAULT;\n F func;\n FW() {}\n FW(int n, T DEFAULT, F func = [](const T& a, const T& b) {return a + b;}) : func(func) { \n this->n = n; \n this->DEFAULT = DEFAULT;\n N = log2(n);\n root.rsz(n, DEFAULT);\n }\n \n inline void update_at(int id, T val) { \n assert(id >= 0);\n while(id < n) { \n root[id] = func(root[id], val);\n id |= (id + 1);\n }\n }\n \n inline T get(int id) { \n assert(id < n);\n T res = DEFAULT;\n while(id >= 0) { \n res = func(res, root[id]);\n id = (id & (id + 1)) - 1;\n }\n return res;\n }\n\n inline T queries_range(int left, int right) { \n return get(right) - get(left - 1);\n }\n\n inline T queries_at(int i) {\n return queries_range(i, i);\n }\n\n inline void update_range(int l, int r, T val) {\n\t\tif(l > r) return;\n update_at(l, val), update_at(r + 1, -val);\n }\n\t\n\tinline void reset() {\n\t\troot.assign(n, DEFAULT);\n\t}\n\n\tll select(ll k) {\n ll pos = -1;\n T acc = DEFAULT;\n for(ll bit = 1LL << N; bit > 0; bit >>= 1) {\n ll np = pos + bit;\n if(np < n) {\n T cand = acc + root[np];\n if(cand < k) {\n acc = cand;\n pos = np;\n }\n }\n }\n return pos + 1;\n }\n\n};\n\nvoid solve() {\n string s; cin >> s;\n const int K = 26;\n int n = s.size();\n if(n & 1) {\n cout << -1 << '\\n';\n return;\n }\n deque<int> q[K];\n for(int i = 0; i < n; i++) {\n q[s[i] - 'a'].pb(i);\n }\n for(int i = 0; i < K; i++) {\n if(q[i].size() & 1) {\n cout << -1 << '\\n';\n return;\n }\n }\n int i = 0, j = n - 1;\n vi remove(n);\n FW<int> root(n, 0);\n ll res = 0;\n while(i < j) {\n while(remove[i]) i++;\n while(remove[j]) j--;\n if(i >= j) break;\n int L = s[i] - 'a', R = s[j] - 'a';\n if(L == R) {\n q[L].pop_back();\n q[L].pop_front();\n i++, j--;\n continue;\n }\n int left_cost = (j - root.get(j)) - q[L].back();\n int right_cost = q[R].front() - (i + root.get(i));\n if(left_cost < right_cost) {\n res += left_cost;\n root.update_range(q[L].back(), j, 1);\n remove[q[L].back()] = true;\n q[L].pop_back();\n q[L].pop_front();\n i++;\n } else {\n res += right_cost;\n root.update_range(i, q[R].front(), 1);\n remove[q[R].front()] = true;\n q[R].pop_front();\n q[R].pop_back();\n j--;\n }\n }\n cout << res << '\\n';\n}\n\nsigned main() {\n // careful for overflow, check for long long, use unsigned long long for random generator\n // when mle, look if problem require read in file, typically old problems\n IOS;\n startClock\n //generatePrime();\n\n int t = 1;\n //cin >> t;\n for(int i = 1; i <= t; i++) { \n //cout << \"Case #\" << i << \": \"; \n solve();\n }\n\n endClock\n #ifdef LOCAL\n printMemoryUsage();\n #endif\n\n return 0;\n}\n\n//███████████████████████████████████████████████████████████████████████████████████████████████████████\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░░░░░░░█\n//█░░▄▀░░█████████░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░░░░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░█████████░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░▄▀░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//███████████████████████████████████████████████████████████████████████████████████████████████████████", "accuracy": 0.1875, "time_ms": 10, "memory_kb": 8520, "score_of_the_acc": -0.1702, "final_rank": 15 }, { "submission_id": "aoj_1596_10702716", "code_snippet": "//████████╗██╗███╗░░██╗  ██╗░░░░░███████╗\n//╚══██╔══╝██║████╗░██║  ██║░░░░░██╔════╝\n//░░░██║░░░██║██╔██╗██║  ██║░░░░░█████╗░░\n//░░░██║░░░██║██║╚████║  ██║░░░░░██╔══╝░░\n//░░░██║░░░██║██║░╚███║  ███████╗███████╗\n//░░░╚═╝░░░╚═╝╚═╝░░╚══╝  ╚══════╝╚══════╝\n// __________________\n// | ________________ |\n// || ____ ||\n// || /\\ | ||\n// || /__\\ | ||\n// || / \\ |____ ||\n// ||________________||\n// |__________________|\n// \\###################\\\n// \\###################\\\n// \\ ____ \\\n// \\_______\\___\\_______\\\n// An AC a day keeps the doctor away.\n\n#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <ctime>\n#include <cassert>\n#include <complex>\n#include <string>\n#include <cstring>\n#include <chrono>\n#include <random>\n#include <bitset>\n#include <iomanip>\n#include <functional>\n#include <numeric>\n#include <stack>\n#include <array>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\ntemplate<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\n#define vt vector\n#define all(x) begin(x), end(x)\n#define allr(x) rbegin(x), rend(x)\n#define ub upper_bound\n#define lb lower_bound\n#define db double\n#define ld long db\n#define ll long long\n#define ull unsigned long long\n#define vll vt<ll> \n#define vvll vt<vll>\n#define pll pair<ll, ll> \n#define vpll vt<pll>\n#define vvpll vt<vpll>\n#define vc vt<char> \n#define vvc vt<vc>\n#define vi vt<int>\n#define vvi vt<vi>\n#define vvvi vt<vvi>\n#define pii pair<int, int>\n#define vpii vt<pii>\n#define vs vt<string>\n#define vvs vt<vs>\n#define vb vt<bool>\n#define vvb vt<vb>\n#define vvpii vt<vpii>\n#define vd vt<db>\n#define ar(x) array<int, x>\n#define var(x) vt<ar(x)>\n#define vvar(x) vt<var(x)>\n#define al(x) array<ll, x>\n#define vall(x) vt<al(x)>\n#define vvall(x) vt<vall(x)>\n#define mset(m, v) memset(m, v, sizeof(m))\n#define pb push_back\n#define ff first\n#define ss second\n#define sv string_view\n#define MP make_pair\n#define MT make_tuple\n#define rsz resize\n#define sum(x) (ll)accumulate(all(x), 0LL)\n#define srt(x) sort(all(x))\n#define srtR(x) sort(allr(x))\n#define srtU(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())\n#define SORTED(x) is_sorted(all(x))\n#define rev(x) reverse(all(x))\n#define MAX(a) *max_element(all(a)) \n#define MIN(a) *min_element(all(a))\n#define ROTATE(a, p) rotate(begin(a), begin(a) + p, end(a))\n#define i128 __int128\n\n//SGT DEFINE\n#define lc i * 2 + 1\n#define rc i * 2 + 2\n#define lp lc, left, middle\n#define rp rc, middle + 1, right\n#define entireTree 0, 0, n - 1\n#define midPoint left + (right - left) / 2\n#define pushDown push(i, left, right)\n#define iter int i, int left, int right\n\n#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)\n\nstruct custom {\n static const uint64_t C = 0x9e3779b97f4a7c15; const uint32_t RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();\n size_t operator()(uint64_t x) const { return __builtin_bswap64((x ^ RANDOM) * C); }\n size_t operator()(const std::string& s) const { size_t hash = std::hash<std::string>{}(s); return hash ^ RANDOM; } };\ntemplate <class K, class V> using umap = std::unordered_map<K, V, custom>; template <class K> using uset = std::unordered_set<K, custom>;\ntemplate<class T> using max_heap = priority_queue<T>;\ntemplate<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;\n \ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, array<T, N>& arr) {\n for (size_t i = 0; i < N; i++) { is >> arr[i]; } return is;\n}\n\ntemplate<typename T, size_t N>\nistream& operator>>(istream& is, vector<array<T, N>>& vec) {\n for (auto &arr : vec) { is >> arr; } return is;\n}\n\ninline std::ostream& operator<<(std::ostream& os, i128 x) {\n if(x == 0) { os << '0'; return os; } if(x < 0) { os << '-'; x = -x; }\n string s; while (x > 0) { int digit = int(x % 10); s.pb(char('0' + digit)); x /= 10; }\n rev(s); os << s; return os;\n}\n \ntemplate <typename T1, typename T2> istream &operator>>(istream& in, pair<T1, T2>& input) { return in >> input.ff >> input.ss; }\n \ntemplate <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &el : v) in >> el; return in; }\n\ntemplate<class T>\nvoid output_vector(vt<T>& a, int off_set = 0) {\n int n = a.size();\n for(int i = off_set; i < n; i++) {\n cout << a[i] << (i == n - 1 ? '\\n' : ' ');\n }\n}\n\ntemplate<typename T, typename Compare>\nvi closest_left(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = 0; i < n; i++) {\n auto& j = closest[i];\n while(j && cmp(a[i], a[j - 1])) j = closest[j - 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename Compare> // auto right = closest_right<int>(a, std::less<int>());\nvi closest_right(const vt<T>& a, Compare cmp) {\n int n = a.size(); vi closest(n); iota(all(closest), 0);\n for (int i = n - 1; i >= 0; i--) {\n auto& j = closest[i];\n while(j < n - 1 && cmp(a[i], a[j + 1])) j = closest[j + 1];\n }\n return closest;\n}\n\ntemplate<typename T, typename V = string>\nvt<pair<T, int>> encode(const V& s) {\n vt<pair<T, int>> seg;\n for(auto& ch : s) {\n if(seg.empty() || ch != seg.back().ff) seg.pb({ch, 1});\n else seg.back().ss++;\n }\n return seg;\n}\n\n \ntemplate<typename K, typename V>\nauto operator<<(std::ostream &o, const std::map<K, V> &m) -> std::ostream& {\n o << \"{\"; int i = 0;\n for (const auto &[key, value] : m) { if (i++) o << \" , \"; o << key << \" : \" << value; }\n return o << \"}\";\n}\n\n#ifdef LOCAL\n#define debug(x...) debug_out(#x, x)\nvoid debug_out(const char* names) { std::cerr << std::endl; }\ntemplate <typename T, typename... Args>\nvoid debug_out(const char* names, T value, Args... args) {\n const char* comma = strchr(names, ',');\n std::cerr << \"[\" << (comma ? std::string(names, comma) : names) << \" = \" << value << \"]\";\n if (sizeof...(args)) { std::cerr << \", \"; debug_out(comma + 1, args...); } \n else { std::cerr << std::endl; }\n}\ntemplate<typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& o, const std::pair<T1, T2>& p) { return o << \"{\" << p.ff << \" , \" << p.ss << \"}\"; }\nauto operator<<(auto &o, const auto &x) -> decltype(end(x), o) {\n o << \"{\"; int i = 0; for (const auto &e : x) { if (i++) o << \" , \"; o << e; } return o << \"}\";\n} // remove for leetcode\n#include <sys/resource.h>\n#include <sys/time.h>\nvoid printMemoryUsage() {\n struct rusage usage;\n getrusage(RUSAGE_SELF, &usage);\n double memoryMB = usage.ru_maxrss / 1024.0;\n cerr << \"Memory usage: \" << memoryMB << \" MB\" << \"\\n\";\n}\n\n#define startClock clock_t tStart = clock();\n#define endClock std::cout << std::fixed << std::setprecision(10) << \"\\nTime Taken: \" << (double)(clock() - tStart) / CLOCKS_PER_SEC << \" seconds\" << std::endl;\n#else\n#define debug(...)\n#define startClock\n#define endClock\n\n#endif\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n#define eps 1e-9\n#define M_PI 3.14159265358979323846\nconst static string pi = \"3141592653589793238462643383279\";\nconst static ll INF = 1LL << 62;\nconst static int inf = 1e9 + 100;\nconst static int MK = 20;\nconst static int MX = 1e5 + 5;\nll gcd(ll a, ll b) { while (b != 0) { ll temp = b; b = a % b; a = temp; } return a; }\nll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }\nll floor(ll a, ll b) { if(b < 0) a = -a, b = -b; if (a >= 0) return a / b; return a / b - (a % b ? 1 : 0); }\nll ceil(ll a, ll b) { if (b < 0) a = -a, b = -b; if (a >= 0) return (a + b - 1) / b; return a / b; }\nint pct(ll x) { return __builtin_popcountll(x); }\nll have_bit(ll x, int b) { return x & (1LL << b); }\nint min_bit(ll x) { return __builtin_ctzll(x); }\nint max_bit(ll x) { return 63 - __builtin_clzll(x); } \nconst vvi dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; // UP, DOWN, LEFT, RIGHT\nconst vvi knight_dirs = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}}; // knight dirs\nconst vc dirChar = {'U', 'D', 'L', 'R'};\nint modExpo(ll base, ll exp, ll mod) { ll res = 1; base %= mod; while(exp) { if(exp & 1) res = (res * base) % mod; base = (base * base) % mod; exp >>= 1; } return res; }\nll extended_gcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extended_gcd(b, a % b, y, x); y -= (a / b) * x; return d; }\nint modExpo_on_string(ll a, string exp, int mod) { ll b = 0; for(auto& ch : exp) b = (b * 10 + (ch - '0')) % (mod - 1); return modExpo(a, b, mod); }\nll sum_even_series(ll n) { return (n / 2) * (n / 2 + 1);} \nll sum_odd_series(ll n) {return n - sum_even_series(n);} // sum of first n odd number is n ^ 2\nll sum_of_square(ll n) { return n * (n + 1) * (2 * n + 1) / 6; } // sum of 1 + 2 * 2 + 3 * 3 + 4 * 4 + ... + n * n\nstring make_lower(const string& t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return tolower(c); }); return s; }\nstring make_upper(const string&t) { string s = t; transform(all(s), s.begin(), [](unsigned char c) { return toupper(c); }); return s; }\nll sqrt(ll n) { ll t = sqrtl(n); while(t * t < n) t++; while(t * t > n) t--; return t;}\ntemplate<typename T> T geometric_sum(ll n, ll k) { return (1 - T(n).pow(k + 1)) / (1 - n); } // return n^1 + n^2 + n^3 + n^4 + n^5 + ... + n^k\ntemplate<typename T> T geometric_power(ll p, ll k) { return (T(p).pow(k + 1) - 1) / T(p - 1); } // p^1 + p^2 + p^3 + ... + p^k\nbool is_perm(ll sm, ll square_sum, ll len) {return sm == len * (len + 1) / 2 && square_sum == len * (len + 1) * (2 * len + 1) / 6;} // determine if an array is a permutation base on sum and square_sum\nbool is_vowel(char c) {return c == 'a' || c == 'e' || c == 'u' || c == 'o' || c == 'i';}\n\ntemplate<class T, typename F = function<T(const T&, const T&)>>\nclass FW { \n public: \n int n, N;\n vt<T> root; \n T DEFAULT;\n F func;\n FW() {}\n FW(int n, T DEFAULT, F func = [](const T& a, const T& b) {return a + b;}) : func(func) { \n this->n = n; \n this->DEFAULT = DEFAULT;\n N = log2(n);\n root.rsz(n, DEFAULT);\n }\n \n inline void update_at(int id, T val) { \n assert(id >= 0);\n while(id < n) { \n root[id] = func(root[id], val);\n id |= (id + 1);\n }\n }\n \n inline T get(int id) { \n assert(id < n);\n T res = DEFAULT;\n while(id >= 0) { \n res = func(res, root[id]);\n id = (id & (id + 1)) - 1;\n }\n return res;\n }\n\n inline T queries_range(int left, int right) { \n return get(right) - get(left - 1);\n }\n\n inline T queries_at(int i) {\n return queries_range(i, i);\n }\n\n inline void update_range(int l, int r, T val) {\n\t\tif(l > r) return;\n update_at(l, val), update_at(r + 1, -val);\n }\n\t\n\tinline void reset() {\n\t\troot.assign(n, DEFAULT);\n\t}\n\n\tll select(ll k) {\n ll pos = -1;\n T acc = DEFAULT;\n for(ll bit = 1LL << N; bit > 0; bit >>= 1) {\n ll np = pos + bit;\n if(np < n) {\n T cand = acc + root[np];\n if(cand < k) {\n acc = cand;\n pos = np;\n }\n }\n }\n return pos + 1;\n }\n\n};\n\nvoid solve() {\n string s; cin >> s;\n const int K = 26;\n int n = s.size();\n if(n & 1) {\n cout << -1 << '\\n';\n return;\n }\n deque<int> q[K];\n for(int i = 0; i < n; i++) {\n q[s[i] - 'a'].pb(i);\n }\n for(int i = 0; i < K; i++) {\n if(q[i].size() & 1) {\n cout << -1 << '\\n';\n return;\n }\n }\n int i = 0, j = n - 1;\n vi remove(n);\n FW<int> root(n, 0);\n ll res = 0;\n while(i < j) {\n while(remove[i]) i++;\n while(remove[j]) j--;\n if(i >= j) break;\n int L = s[i] - 'a', R = s[j] - 'a';\n if(L == R) {\n q[L].pop_back();\n q[L].pop_front();\n i++, j--;\n continue;\n }\n int left_cost = (j - root.get(j)) - q[L].back();\n int right_cost = q[R].front() - (i + root.get(i));\n if(left_cost < right_cost) {\n res += left_cost;\n root.update_range(q[L].back(), j, 1);\n q[L].pop_back();\n i++;\n } else {\n res += right_cost;\n root.update_range(i, q[R].front(), 1);\n q[R].pop_front();\n j--;\n }\n }\n cout << res << '\\n';\n}\n\nsigned main() {\n // careful for overflow, check for long long, use unsigned long long for random generator\n // when mle, look if problem require read in file, typically old problems\n IOS;\n startClock\n //generatePrime();\n\n int t = 1;\n //cin >> t;\n for(int i = 1; i <= t; i++) { \n //cout << \"Case #\" << i << \": \"; \n solve();\n }\n\n endClock\n #ifdef LOCAL\n printMemoryUsage();\n #endif\n\n return 0;\n}\n\n//███████████████████████████████████████████████████████████████████████████████████████████████████████\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░░░░░░░░░██░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀▄▀▄▀▄▀▄▀░░██░░▄▀░░█░░▄▀░░░░░░░░░░█\n//█░░▄▀░░█████████░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░░░░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░█████████\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░░░░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░██░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░█████████░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░███░░▄▀░░███░░▄▀░░██░░▄▀░░░░░░▄▀░░█░░▄▀░░██░░▄▀░░█\n//█░░▄▀░░░░░░░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░▄▀▄▀░░█░░░░▄▀░░░░█░░▄▀░░██░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░░░░░▄▀░░█\n//█░░▄▀▄▀▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀░░░░█░░▄▀▄▀▄▀░░█░░▄▀░░██░░░░░░░░░░▄▀░░█░░▄▀▄▀▄▀▄▀▄▀░░█\n//█░░░░░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░███░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█\n//███████████████████████████████████████████████████████████████████████████████████████████████████████", "accuracy": 0.25, "time_ms": 30, "memory_kb": 8464, "score_of_the_acc": -0.2389, "final_rank": 12 }, { "submission_id": "aoj_1596_10702667", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nclass FenwickTree{\n private:\n vector<int>bit;\n int n;\n public:\n FenwickTree(int n){\n this->n = n;\n bit.resize(n + 1,0);\n }\n void update(int i, int delta){\n while(i <= n){\n bit[i] += delta;\n i += (i & -i);\n }\n }\n int query(int i){\n int sum = 0;\n while(i > 0){\n sum += bit[i];\n i -= (i & -i);\n }\n return sum;\n }\n};\nlong long solve(string s){\n int n = s.length();\n vector<bool>alive(n, true);\n deque<int>pos[26];\n int left = 0, right = n - 1;\n FenwickTree bit(n);\n for(int i = 1; i <= n; i++){\n bit.update(i, 1);\n }\n auto remove = [&](int i){\n alive[i] = false;\n bit.update(i + 1, -1);\n };\n for(int i = 0; i < n; i++){\n s[i] -= 'a';\n pos[s[i]].push_back(i);\n }\n int odd_count = 0;\n for(int i = 0; i < n; i++){\n odd_count += pos[s[i]].size() & 1;\n }\n if(odd_count != (n & 1)){\n return -1;\n }\n int swaps = 0;\n while(left < right){\n if(!alive[left]){\n left++;\n }else if(!alive[right]){\n right--;\n }else{\n int char_left = s[left];\n int char_right = s[right];\n int cost_left = bit.query(n) - bit.query(pos[char_left].back() + 1);\n int cost_right = bit.query(pos[char_right].front() + 1) - 1;\n int ch;\n if(cost_left < cost_right){\n ch = char_left;\n swaps += cost_left;\n }else{\n ch = char_right;\n swaps += cost_right;\n }\n remove(pos[ch].front());\n pos[ch].pop_front();\n remove(pos[ch].back());\n pos[ch].pop_back();\n }\n \n }\n return swaps;\n}\nint main(){\n string s;\n cin >> s;\n cout << solve(s) << endl;\n}", "accuracy": 0.1875, "time_ms": 20, "memory_kb": 6868, "score_of_the_acc": -0.1249, "final_rank": 13 }, { "submission_id": "aoj_1596_10702528", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// Fenwick Tree class at global scope\nclass FenwickTree {\nprivate:\n vector<int> bit;\n int n;\n\npublic:\n FenwickTree(int n) {\n this->n = n;\n bit.resize(n + 1, 0);\n }\n\n void update(int i, int delta) {\n while (i <= n) {\n bit[i] += delta;\n i += (i & -i);\n }\n }\n\n int query(int i) const {\n int sum = 0;\n while (i > 0) {\n sum += bit[i];\n i -= (i & -i);\n }\n return sum;\n }\n\n int query(int l, int r) const {\n return query(r) - query(l - 1);\n }\n};\n\n// Your function\nlong long minSwapsToPalindrome(string s) {\n int n = s.size();\n\n deque<int> pos[26];\n vector<bool> alive(n, true);\n\n for (int i = 0; i < n; ++i) {\n s[i] -= 'a';\n pos[s[i]].push_back(i);\n }\n\n int odd_count = 0;\n for (int c = 0; c < 26; ++c) {\n odd_count += pos[c].size() & 1;\n }\n if (odd_count != (n & 1)) {\n return -1; // impossible to form a palindrome\n }\n\n auto remove = [&](int idx, FenwickTree &bit) {\n alive[idx] = false;\n bit.update(idx + 1, -1);\n };\n\n FenwickTree bit(n);\n for (int i = 1; i <= n; ++i) {\n bit.update(i, 1);\n }\n\n long long total_swaps = 0;\n\n for (int left = 0, right = n - 1; left < right;) {\n if (!alive[left]) {\n ++left;\n } else if (!alive[right]) {\n --right;\n } else {\n int char_left = s[left];\n int char_right = s[right];\n\n int cost_left = bit.query(n) - bit.query(pos[char_left].back() + 1);\n int cost_right = bit.query(pos[char_right].front() + 1) - 1;\n\n int chosen_char;\n if (cost_left < cost_right) {\n chosen_char = char_left;\n total_swaps += cost_left;\n } else {\n chosen_char = char_right;\n total_swaps += cost_right;\n }\n\n remove(pos[chosen_char].front(), bit);\n remove(pos[chosen_char].back(), bit);\n pos[chosen_char].pop_front();\n pos[chosen_char].pop_back();\n }\n }\n\n return total_swaps;\n}\nint main() {\n string s;\n cin >> s;\n cout << minSwapsToPalindrome(s) << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 7216, "score_of_the_acc": -0.1419, "final_rank": 2 }, { "submission_id": "aoj_1596_10702512", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <deque>\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nconst int MAX_N = 1'000'000;\n\nint n;\nchar s[MAX_N + 1];\ndeque<int> pos[26]; // positions of each character\nbool alive[MAX_N]; // is this position still present?\n\n// Fenwick Tree class\nclass FenwickTree {\nprivate:\n int n;\n vector<int> bit;\n\npublic:\n FenwickTree(int n) : n(n), bit(n + 1, 0) {}\n\n void update(int i, int delta) {\n while (i <= n) {\n bit[i] += delta;\n i += (i & -i);\n }\n }\n\n int query(int i) const {\n int sum = 0;\n while (i > 0) {\n sum += bit[i];\n i -= (i & -i);\n }\n return sum;\n }\n\n int query(int l, int r) const {\n return query(r) - query(l - 1);\n }\n};\n\n// Mark position as removed\nvoid remove(int idx, FenwickTree &bit) {\n alive[idx] = false;\n bit.update(idx + 1, -1); // +1 for 1-based BIT\n}\n\nint main() {\n scanf(\"%s\", s);\n n = strlen(s);\n\n for (int i = 0; i < n; ++i) {\n s[i] -= 'a';\n pos[s[i]].push_back(i);\n }\n\n // Check if palindrome is possible\n int odd_count = 0;\n for (int c = 0; c < 26; ++c) {\n odd_count += pos[c].size() & 1;\n }\n if (odd_count != (n & 1)) {\n puts(\"-1\");\n return 0;\n }\n\n memset(alive, true, sizeof(alive));\n\n FenwickTree bit(n);\n for (int i = 1; i <= n; ++i) {\n bit.update(i, 1);\n }\n\n long long total_swaps = 0;\n\n for (int left = 0, right = n - 1; left < right;) {\n if (!alive[left]) {\n ++left;\n } else if (!alive[right]) {\n --right;\n } else {\n int char_left = s[left];\n int char_right = s[right];\n\n int cost_left = bit.query(n) - bit.query(pos[char_left].back() + 1);\n int cost_right = bit.query(pos[char_right].front() + 1) - 1;\n\n int chosen_char;\n if (cost_left < cost_right) {\n chosen_char = char_left;\n total_swaps += cost_left;\n } else {\n chosen_char = char_right;\n total_swaps += cost_right;\n }\n\n remove(pos[chosen_char].front(), bit);\n remove(pos[chosen_char].back(), bit);\n pos[chosen_char].pop_front();\n pos[chosen_char].pop_back();\n }\n }\n\n cout << total_swaps << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7860, "score_of_the_acc": -0.1378, "final_rank": 1 }, { "submission_id": "aoj_1596_10702504", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <deque>\n#include <iostream>\n\nconst int MAX_N = 1'000'000;\n\nint n;\nchar s[MAX_N + 1];\nstd::deque<int> pos[26]; // positions of each character\n\nint bit[MAX_N + 2]; // 1-based BIT\nbool alive[MAX_N]; // is this position still present?\n\n// BIT update: add `delta` at `idx`\nvoid update(int idx, int delta) {\n while (idx <= n) {\n bit[idx] += delta;\n idx += idx & -idx;\n }\n}\n\n// BIT query: sum[1..idx]\nint query(int idx) {\n int sum = 0;\n while (idx > 0) {\n sum += bit[idx];\n idx -= idx & -idx;\n }\n return sum;\n}\n\n// Mark position as removed\nvoid remove(int idx) {\n alive[idx] = false;\n update(idx + 1, -1); // +1 for 1-based BIT\n}\n\nint main() {\n scanf(\"%s\", s);\n n = strlen(s);\n\n for (int i = 0; i < n; ++i) {\n s[i] -= 'a';\n pos[s[i]].push_back(i);\n }\n\n // Check if palindrome is possible\n int odd_count = 0;\n for (int c = 0; c < 26; ++c) {\n odd_count += pos[c].size() & 1;\n }\n if (odd_count != (n & 1)) {\n puts(\"-1\");\n return 0;\n }\n\n memset(alive, true, sizeof(alive));\n for (int i = 1; i <= n; ++i) {\n update(i, 1);\n }\n\n long long total_swaps = 0;\n\n for (int left = 0, right = n - 1; left < right;) {\n if (!alive[left]) {\n ++left;\n } else if (!alive[right]) {\n --right;\n } else {\n int char_left = s[left];\n int char_right = s[right];\n\n int cost_left = query(n) - query(pos[char_left].back() + 1);\n int cost_right = query(pos[char_right].front() + 1) - 1;\n\n int chosen_char = (cost_left < cost_right) ? char_left : char_right;\n total_swaps += std::min(cost_left, cost_right);\n\n remove(pos[chosen_char].front());\n remove(pos[chosen_char].back());\n pos[chosen_char].pop_front();\n pos[chosen_char].pop_back();\n }\n }\n\n std::cout << total_swaps << std::endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 9692, "score_of_the_acc": -0.2278, "final_rank": 4 }, { "submission_id": "aoj_1596_10702455", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <deque>\n#include <iostream>\n\nconst int N = 1000000;\n\nint n;\nchar string[N + 1];\nstd::deque <int> letters[26];\n\nint count[N];\nbool present[N];\n\nvoid modify(int k, int v) {\n for(; k < n; k += ~k & k + 1) {\n count[k] += v;\n }\n}\n\nint query(int k) {\n int ret = 0;\n for (; k >= 0; k -= ~k & k + 1) {\n ret += count[k];\n }\n return ret;\n}\n\nvoid remove(int i) {\n present[i] = false;\n modify(i, -1);\n}\n\nint main() {\n scanf(\"%s\", string);\n n = strlen(string);\n for (int i = 0; i < n; ++ i) {\n letters[(int)(string[i] -= 'a')].push_back(i);\n }\n\n \n int odd_count = 0;\n for (int i = 0; i < 26; ++ i) {\n odd_count += letters[i].size() & 1;\n }\n if (odd_count != (n & 1)) {\n puts(\"-1\");\n return 0;\n }\n memset(present, true, sizeof(present));\n for (int i = 0; i < n; ++ i) {\n modify(i, 1);\n }\n long long answer = 0;\n for (int l = 0, r = n - 1; l < r; ) {\n if (!present[l]) {\n l ++;\n } else if (!present[r]) {\n r --;\n } else {\n int left_cost = query(n - 1) - query(letters[(int)string[l]].back());\n int right_cost = query(letters[(int)string[r]].front()) - 1;\n answer += std::min(left_cost, right_cost);\n int token = left_cost < right_cost ? string[l] : string[r];\n remove(letters[token].front());\n remove(letters[token].back());\n letters[token].pop_front();\n letters[token].pop_back();\n }\n }\n std::cout << answer << std::endl;\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 9692, "score_of_the_acc": -0.2635, "final_rank": 5 }, { "submission_id": "aoj_1596_5217008", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\n// 転倒数(マージソートを利用)\nll mergeCount(vector<ll> &a) {\n ll n = a.size();\n if (n <= 1) return 0;\n vector<ll> b(a.begin(), a.begin() + n / 2), c(a.begin() + n / 2, a.end());\n ll cnt = mergeCount(b) + mergeCount(c);\n // merge\n ll ai = 0, bi = 0, ci = 0;\n while (ai < n) {\n if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {\n a[ai++] = b[bi++];\n }\n else {\n cnt += n / 2 - bi;\n a[ai++] = c[ci++];\n }\n }\n return cnt;\n}\n\nll f(string &t, string &u) {\n vector<queue<ll>> x(26);\n for (ll i = 0; i < t.size(); i++) {\n x[t[i] - 'a'].push(i);\n }\n vector<ll> y(u.size());\n ll res = 0;\n for (ll i = 0; i < u.size(); i++) {\n y[i] = x[u[i] - 'a'].front();\n x[u[i] - 'a'].pop();\n }\n return mergeCount(y);\n}\n\nint main() {\n string s;\n cin >> s;\n \n map<char, ll> mp;\n for (int i = 0; i < s.size(); i++) {\n mp[s[i]]++;\n }\n \n int odd = 0;\n for (auto i : mp) {\n if (i.second % 2 == 1) odd++;\n }\n \n if ((s.size() % 2 == 1 && odd >= 2) || (s.size() % 2 == 0 && odd >= 1)) {\n cout << -1 << '\\n';\n return 0;\n }\n \n map<char, ll> mp2;\n ll ans = 0;\n string t = \"\", u = \"\";\n for (ll i = 0; i < s.size(); i++) {\n if (mp[s[i]] % 2 == 0) {\n if (mp2[s[i]] < mp[s[i]] / 2) {\n ans += i - t.size();\n mp2[s[i]]++;\n t += s[i];\n }\n else {\n u += s[i];\n }\n }\n else {\n if (mp2[s[i]] < mp[s[i]] / 2) {\n ans += i - t.size();\n mp2[s[i]]++;\n t += s[i];\n }\n else if (mp2[s[i]] == mp[s[i]] / 2) {\n ans += u.size();\n mp2[s[i]]++;\n }\n else {\n u += s[i];\n }\n }\n }\n \n reverse(t.begin(), t.end());\n \n ans += f(t, u);\n \n cout << ans << '\\n';\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 8984, "score_of_the_acc": -0.3002, "final_rank": 7 }, { "submission_id": "aoj_1596_4061357", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define REP(i, n) for (int (i) = 0; (i) < (n); ++(i))\n\ntemplate<typename T> struct BinaryIndexedTree {\n vector< T > data;\n BinaryIndexedTree(int sz) { data.assign(++sz, 0); }\n T sum(int k) {\n if (k < 0) return T(0);\n T ret = 0;\n for (++k; k > 0; k -= k & -k) ret += data[k];\n return (ret);\n }\n T sum(int l, int r) {\n assert(l <= r);\n return sum(r - 1) - sum(l - 1);\n }\n void add(int k, T x) {\n for (++k; k < data.size(); k += k & -k) data[k] += x;\n }\n};\n\ntemplate<typename T> int_fast64_t InversionNumber(vector< T > &v) {\n int_fast64_t sz = v.size(), ret = 0;\n vector< T > rev, v_cp = v; sort(v_cp.begin(), v_cp.end());\n for (auto &e : v) rev.emplace_back(lower_bound(v_cp.begin(), v_cp.end(), e) - v_cp.begin());\n BinaryIndexedTree< T > bit(sz);\n for (int i = 0; i < sz; ++i) {\n ret += i - bit.sum(rev[i]);\n bit.add(rev[i], 1);\n }\n return ret;\n}\n\nsigned main() {\n\n string S;\n while (cin >> S) {\n int N = S.size();\n\n int cnt[26], now[26];\n REP(i, 26) cnt[i] = now[i] = 0;\n REP(i, N) cnt[S[i] - 'a']++;\n\n bool valid = true;\n REP(i, 26) valid &= cnt[i] % 2 == 0;\n if (not valid) {\n cout << -1 << endl;\n continue;\n }\n\n // 塗り分ける\n bool color[N];\n REP(i, N) color[i] = false;\n REP(i, N) {\n int c = S[i] - 'a';\n if (now[c] < cnt[c] / 2) color[i] = true;\n now[c]++;\n }\n\n // 半分ずつに分けるコスト\n ll res = 0;\n REP(i, N / 2) res -= i;\n REP(i, N) if (color[i]) res += i;\n\n // 左半分と右半分の文字列\n string l = \"\", r = \"\";\n REP(i, N) {\n if (color[i]) l += S[i];\n else r += S[i];\n }\n reverse(l.begin(), l.end());\n\n map<int, queue<int>> ques;\n REP(i, N / 2) {\n int c = l[i] - 'a';\n ques[c].emplace(i);\n }\n\n // 転倒数を求める対象\n vector<int> r_vec;\n REP(i, N / 2) {\n int c = r[i] - 'a';\n r_vec.emplace_back(ques[c].front());\n ques[c].pop();\n }\n\n cout << res + InversionNumber(r_vec) << endl;\n }\n\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 8548, "score_of_the_acc": -0.2788, "final_rank": 6 }, { "submission_id": "aoj_1596_4061317", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\n#define rep(i, n) for(ll i = 0, i##_end = (n); i < i##_end; i++)\n#define repb(i, n) for(ll i = (n)-1; i >= 0; i--)\n\nstruct BIT {\n ll n;\n vll bit;\n //1-indexed\n BIT(ll n):n(n), bit(n+1, 0){}\n void add (ll a, ll w){\n for(ll x=a; x<=n; x+=x&-x) bit[x] += w;\n }\n ll sum(ll a){\n ll ret = 0;\n for(ll x = a; x>0; x-=x&-x){\n ret += bit[x];\n }\n return ret;\n }\n ll sum(ll a, ll b){\n return sum(b)-sum(a-1);\n }\n};\n\nconst ll MAX = 500010;\nll cnt[256];\nll cnt2[256];\nll cnt3[256];\nll idx[MAX];\nvll vv[256];\nll p[256];\nsigned main(){\n string s;\n cin >> s;\n ll n = s.size();\n rep(i, n){\n cnt[s[i]]++;\n }\n rep(i, 256){\n if(cnt[i]%2!=0){\n cout << -1 << endl;\n return 0; \n }\n }\n ll ic = 1;\n rep(i, n){\n cnt2[s[i]]++;\n if(cnt2[s[i]] <= cnt[s[i]]/2){\n idx[i] = ic;\n vv[s[i]].emplace_back(n+1-ic);\n ic++;\n }\n }\n rep(i, 256){\n reverse(vv[i].begin(), vv[i].end());\n }\n /*\n rep(i, n){\n cout << idx[i] << \" \";\n }\n cout << endl;\n */\n rep(i, n){\n cnt3[s[i]]++;\n if(cnt3[s[i]] > cnt[s[i]]/2){\n idx[i] = vv[s[i]][p[s[i]]];\n p[s[i]]++;\n }\n }\n /*\n rep(i, n){\n cout << idx[i] << \" \";\n }\n */\n BIT bi(n+2);\n ll ans = 0;\n rep(i, n){\n ans += bi.sum(idx[i]+1, n);\n bi.add(idx[i], 1);\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 11868, "score_of_the_acc": -0.3703, "final_rank": 9 }, { "submission_id": "aoj_1596_4061314", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(a) (a).begin(),(a).end()\n#define ALLR(a) (a).rbegin(),(a).rend()\n#define rep(i,n,m) for(ll i=(n);i<(m);i++)\n#define rrep(i,n,m) for(ll i=(m)-1;i>=(n);i--)\nusing P=pair<ll,ll>;\n#define se second\n#define fi first\nll INF=1e18;\ntemplate<typename T>\nvoid chmax(T &a,T b){if(a<b)a=b;};\ntemplate<typename T>\nvoid chmin(T &a,T b){if(a>b)a=b;};\ntemplate<typename T>\nstruct BIT{\n ll n,k=1;\n vector<T>data;\n BIT(ll size):n(size){\n data.assign(n,0);\n while(k*2<=n)k*=2;\n }\n void add(ll a, T w){\n for(ll i=a+1;i<=n;i+=(i&-i))data[i-1]+=w;\n }\n T sum(ll a){\n T ret=0;\n for(ll i=a+1;i>0;i-=(i&-i))ret+=data[i-1];\n return ret;\n }\n};\nint main(){\n vector<ll>cnt(26);\n string s;cin>>s;\n ll n=s.size();\n rep(i,0,n)cnt[s[i]-'a']++;\n vector<ll>idx(n);\n bool judge=true;\n rep(i,0,26)if(cnt[i]%2!=0)judge=false;\n if(!judge){\n cout<<-1<<endl;\n return 0;\n }\n vector<vector<ll>>num(26);\n ll k=0;\n rep(i,0,n){\n if(num[s[i]-'a'].size()<cnt[s[i]-'a']/2){\n idx[i]=i-k;\n }\n else{\n ll p=num[s[i]-'a'].size();\n ll p2=cnt[s[i]-'a'];\n p2/=2;\n idx[i]=n-1-num[s[i]-'a'][p2-1-(p-p2)];\n }\n num[s[i]-'a'].push_back(i);\n }\n BIT<ll>bit(n+1);\n ll res=0;\n rep(i,0,n){\n res+=bit.sum(n-idx[i]);\n bit.add(n-idx[i],1);\n }\n //rep(i,0,n)cout<<idx[i]<<\" \";\n //cout<<endl;\n cout<<res<<endl;\n return 0;\n}", "accuracy": 0.1875, "time_ms": 10, "memory_kb": 12624, "score_of_the_acc": -0.3717, "final_rank": 16 }, { "submission_id": "aoj_1596_4061096", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define REP(i, n) for (int (i) = 0; (i) < (n); ++(i))\n#define FOR(i, a, b) for (int (i) = (a); (i) < (b); ++(i))\n\nsigned main() {\n string s; //cin >> s;\n while (cin >> s) {\n int n = s.size();\n map<char, int> cnt;\n REP(i,n) {\n cnt[s[i]]++;\n }\n for(auto e : cnt) {\n if(e.second % 2 != 0) {\n cout << -1 << endl;\n return 0;\n }\n }\n map<char, int> cn;\n map<char, queue<int>> que;\n map<char, stack<int>> sta;\n int64_t ans = 0;\n int pre = -1;\n int l = 1, r = n / 2;\n REP(i,n) {\n cn[s[i]]++;\n if(cn[s[i]] <= cnt[s[i]] / 2) {\n if(pre == -1) {\n ans += i;\n } else {\n ans += max(0, i - l + 1);\n }\n // cerr << i << endl;\n // cerr << \"koko\" << ans << endl;\n pre = i;\n que[s[i]].push(l++);\n // cerr << \"que : \" << s[i] << \" \" << l<< endl;\n } else {\n sta[s[i]].push(r--);\n // cerr << \"sta : \" << s[i] << \" \" << r + 1 << endl;\n }\n }\n // cerr << ans << endl;\n int64_t anss = 0;\n for(auto &e : que) {\n char c = e.first;\n // cerr << c << endl;\n while(sta[c].size()) {\n int i = sta[c].top();\n sta[c].pop();\n int j = e.second.front();\n e.second.pop();\n // cerr << i << \" \" << j << endl;\n anss += abs(i - j);\n }\n }\n cout << ans + anss / 2 << endl;\n }\n}", "accuracy": 0.25, "time_ms": 10, "memory_kb": 5052, "score_of_the_acc": 0, "final_rank": 11 }, { "submission_id": "aoj_1596_4061093", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i, a) for(int64 i = 0; i < (a);i++)\n#define FOR(i, a, b) for(int64 i = (a); i < (b); i++)\n#define INF (int64)1e9\n#define INF_LL (int64)1e18\n#define all(x) x.begin(),x.end()\n#define fs first\n#define sc second\n\nusing int32 = int32_t;\nusing int64 = int64_t;\nusing PII = pair<int32, int32>;\nusing PLL = pair<int64, int64>;\n\ntemplate<typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b;}\ntemplate<typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b;}\n\ntemplate<typename CommutativeMonoid>\nclass FenwickTree {\npublic:\n using value_structure = CommutativeMonoid;\n using value_type = typename CommutativeMonoid::value_type;\n using size_type = ::std::size_t;\nprivate:\n vector<value_type> tree;\n size_type N;\n\n static value_type calc(const value_type& a, const value_type& b) {\n return value_structure::operation(a, b);\n }\npublic:\n FenwickTree() {}\n FenwickTree(const size_type& N): N(N+10), tree(N+11, value_structure::identity()) {}\n\n void update(size_type k, const value_type& diff) {\n for(;k <= N; k+= k & -k) {\n tree[k] = calc(tree[k], diff);\n }\n }\n\n value_type query(size_type k) const {\n value_type res = value_structure::identity();\n for(; k > 0; k -= k & -k) {\n res = calc(res, tree[k]);\n }\n return res;\n }\n\n void updatez(size_type k, value_type v) {\n update(k+1, v);\n }\n\n value_type queryz(size_type k) {\n return query(k+1);\n }\n};\n\nclass v_monoid {\npublic:\n using value_type = int64;\n static constexpr value_type identity() { return 0; }\n static value_type operation(const value_type& a, const value_type& b) {\n return a + b;\n }\n};\n\nint main(void) {\n string s;\n cin >> s;\n int64 cnt[26] = {};\n vector<deque<int64>> ptr(26);\n REP(i, s.size()) {\n cnt[s[i]-'a']++;\n ptr[s[i]-'a'].push_back(i);\n }\n FenwickTree<v_monoid> lft(s.size()), rgt(s.size());\n REP(i, 26) {\n if (cnt[i]%2) {\n cout << -1 << endl;\n return 0;\n }\n }\n int64 res = 0;\n REP(i, s.size()/2) {\n int64 now = INF, idx = -1;\n REP(j, 26) {\n if (ptr[j].size() < 2) continue;\n int64 l = ptr[j].front(), r = ptr[j].back();\n\n// cout << j << \": \" << l << \" \" << lft.queryz(l) << \", \" << r << \" \" << s.size()-r-1 << \" \" << rgt.query(s.size()-r-1) << endl;\n int64 cost = l-lft.queryz(l) + s.size()-r-1 - rgt.query(s.size()-r-1);\n if (cost < now) {\n idx = j;\n now = cost;\n }\n }\n// cout << idx << \" \" << now << endl;\n res += now;\n lft.updatez(ptr[idx].front(), 1);\n rgt.updatez(s.size()-ptr[idx].back()-1, 1);\n ptr[idx].pop_back();\n ptr[idx].pop_front();\n }\n cout << res << endl;\n}", "accuracy": 0.1875, "time_ms": 30, "memory_kb": 12740, "score_of_the_acc": -0.4488, "final_rank": 17 } ]
aoj_1590_cpp
Problem D: One-Time Path Problem N 個の島と M 本の橋がある。 N 個の島にはそれぞれ1から N までの番号が割り振られている。 M 本の橋にもそれぞれ1から M までの番号が割り振られている。 がっちょ君は現在(時刻0の時点で)、1番目の島にいる。 がっちょ君は i 番目の橋を利用することにより、 a i 番目の島から b i 番目の島へと単方向に移動することができる。 しかし、時刻0の時点では、すべての橋は潮が満ちていて海の中に沈んでしまっている。 i 番目の橋は、時刻 c i になると潮が引いて渡れるようになる。 そして、時刻 c i を過ぎると、すぐにまた潮が満ちて i 番目の橋は再び沈んでしまう。 再び沈んでしまうと、またいつ潮が引いて渡れるようになるかはわからない。 そこで、がっちょ君はそうなった橋は永遠に渡れなくなると考えるようにした。 がっちょ君は、できるだけ長い間、1から N -1番目の島の景色を眺めていたいのだが、 N 番目の島に船を泊めてあるので、最終的には N 番目の島に到着していなければならない。また、船の中で両親を待たせているため、がっちょ君は N 番目の島についたらすぐに船で出発して家へと帰らなければならない。 がっちょ君が橋を渡ったり島の中を移動する時間はとても短いので、0と仮定してよい。がっちょ君が1から N -1番目のいずれかの島にいることのできる時間の最大値を求めよ。ただし、どのように移動してもがっちょ君が N 番目の島へと移動できない場合は代わりに-1を出力せよ。 Input 入力は以下の形式で与えられる。 N M a 1 b 1 c 1 a 2 b 2 c 2 … a M b M c M 1行目には、2つの整数 N , M が空白区切りで与えられる。 2行目から M +1行目のそれぞれの行 i には、3つの整数 a i , b i , c i が空白区切りで与えられる。 Constraints 2 ≤ N ≤ 10 5 1 ≤ M ≤ 2 × 10 5 1 ≤ a i < N 1 ≤ b i ≤ N 1 ≤ c i ≤ 10 9 a i ≠ b i Output がっちょ君が N 番目の島へと移動できる場合は、がっちょ君が1から N -1番目のいずれかの島にいることのできる時間の最大値を出力する。 N 番目の島へと移動できない場合は、代わりに-1を出力する。 Sample Input 1 3 2 1 2 10 2 3 20 Sample Output 1 20 まず、1番目の島で時刻が10になるまで待ってから、2番目の島へと移動する。 次に、2番目の島で時刻が20になるまで待ってから、3番目の島へと移動する。 以上より、1から2番目のいずれかの島にいる時間は20となる。 Sample Input 2 4 4 1 2 27 1 3 37 2 3 47 3 1 57 Sample Output 2 -1 4番目の島に繋がる橋がないため、4番目の島へ移動することができない。 Sample Input 3 3 3 1 2 13 2 3 17 2 3 15 Sample Output 3 17 Sample Input 4 3 2 1 2 20 2 3 10 Sample Output 4 -1 Sample Input 5 3 2 1 2 10 2 3 10 Sample Output 5 10
[ { "submission_id": "aoj_1590_10853810", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << \"(\" << o.first << \",\" << o.second << \")\"; return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << \" \";} return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }\ntemplate<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << \"{ \"; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << \":\" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << \", \"; } out << \" }\"; return out; }\n\n/*\n <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1590>\n 問題文============================================================\n =================================================================\n 解説=============================================================\n ================================================================\n */\n\n\nll solve(){\n ll res = -1;\n ll N,M; cin >> N >> M;\n vector<vector<pll>> G(N);\n using Event = tuple<ll,ll,ll>;\n vector<Event> E;\n for(int i = 0; i < M;i++){\n ll a,b,c; cin >> a >> b >> c;\n a--; b--;\n E.push_back(Event(a,b,c));\n }\n sort(E.begin(),E.end(),[](const Event & e1,const Event& e2){\n return std::get<2>(e1) < std::get<2>(e2);\n });\n \n \n vector<int> flag(N); flag[0] = 1;\n vector<Event> event;\n int idx = 0;\n ll nowT = 0;\n while(idx < M){\n ll a,b,c; tie(a,b,c) = E[idx++];\n if(nowT == c) {\n event.push_back(Event(a,b,c));\n }else{\n nowT = c;\n bool update = false;\n while(true){\n update = false;\n for(int i = 0; i < event.size();i++){\n ll u,v,T; tie(u,v,T) = event[i];\n if(flag[u]){\n if(flag[v]){\n if(v == N-1) res = max(res,T);\n continue;\n //event.erase(event.begin()+i);\n }else{\n flag[v] = 1;\n update = true;\n }\n }\n }\n if(update) continue;\n break;\n }\n event.clear();\n event.push_back(Event(a,b,c));\n }\n }\n bool update = false;\n while(true){\n update = false;\n for(int i = 0; i < event.size();i++){\n ll u,v,T; tie(u,v,T) = event[i];\n if(flag[u]){\n if(flag[v]){\n if(v == N-1) res = max(res,T);\n continue;\n //event.erase(event.begin()+i);\n }else{\n flag[v] = 1;\n update = true;\n }\n }\n }\n if(update) continue;\n break;\n }\n return res;\n}\nint main(void) {\n cin.tie(0); ios_base::sync_with_stdio(false);\n cout << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 14860, "score_of_the_acc": -1.5306, "final_rank": 19 }, { "submission_id": "aoj_1590_6283721", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <queue>\nusing namespace std;\nusing P = pair<int, int>;\n\nint main(){\n int n, m;\n cin >> n >> m;\n vector<vector<P>> G(n);\n for(int i = 0; i < m; i++){\n int a, b, c; cin >> a >> b >> c;\n a--; b--;\n G[a].emplace_back(c, b);\n // G[b].emplace_back(c, a);\n }\n\n vector<int> t(n, 1 << 30);\n t[0] = 0;\n priority_queue<P, vector<P>, greater<P>> Q;\n Q.emplace(0, 0);\n while(!Q.empty()){\n auto c = Q.top(); Q.pop();\n if(t[c.second] < c.first) continue;\n for(auto &nex: G[c.second]){\n if(t[nex.second] > nex.first && nex.first >= c.first){\n t[nex.second] = nex.first;\n Q.emplace(nex.first, nex.second);\n }\n }\n }\n int ans = -(1 << 30);\n for(int i = 0; i < n-1; i++){\n for(auto &nex: G[i]){\n if(nex.second != n-1) continue;\n if(t[i] <= nex.first) ans = max(ans, nex.first);\n }\n }\n if(ans == -(1 << 30)) ans = -1;\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 9012, "score_of_the_acc": -0.4025, "final_rank": 11 }, { "submission_id": "aoj_1590_5514211", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <utility>\n#include <queue>\n\nusing namespace std;\n\nint N,M;\nconst int MAXN=1e5+7;\nusing edge_t=pair<int,int>;\nvector<edge_t> graph[MAXN];\nint dist[MAXN];\n\nconst int INF=1e9+7;\nint main(){\n scanf(\"%d%d\",&N,&M);\n for(int i=0;i<M;i++){\n int a,b,c;\n scanf(\"%d%d%d\",&a,&b,&c);\n graph[a].push_back({-c,b});\n }\n\n fill(dist+1,dist+N+1,INF);\n\n priority_queue<pair<int,int>> q;\n q.push({0,1});\n while(!q.empty()){\n int d=-q.top().first,v=q.top().second;\n q.pop();\n if (dist[v]<=d) continue;\n dist[v]=d;\n for(auto e:graph[v])\n if (e.first+d<=0) q.push({e.first,e.second});\n }\n\n int ans=-1;\n for(int v=1;v<N;v++){\n for(auto e:graph[v]){\n if (e.first+dist[v]>0) continue;\n if (e.second!=N) continue;\n ans=max(ans,-e.first);\n }\n }\n\n printf(\"%d\\n\",ans);\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 9696, "score_of_the_acc": -0.2967, "final_rank": 5 }, { "submission_id": "aoj_1590_4120194", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing i64 = int64_t;\n\nconstexpr int INF = 1e9 + 100;\n\nstruct Edge{\n int from, to, c;\n};\n\nstruct State{\n int num, tm;\n bool operator<(const State &rhs)const{\n return tm > rhs.tm;\n }\n};\n\nusing Graph = vector<vector<Edge>>;\n\nint main(){\n int n, m;\n cin >> n >> m;\n Graph graph(n+1, vector<Edge>());\n for(int i=0;i<m;++i){\n int a, b, c;\n cin >> a >> b >> c;\n graph[a].push_back((Edge){a, b, c});\n }\n\n vector<int> mini(n+1, INF);\n mini[1] = 0;\n priority_queue<State> que;\n que.push((State){1, 0});\n int ans = -1;\n while(!que.empty()){\n auto cur = que.top(); que.pop();\n if(mini[cur.num] < cur.tm)continue;\n for(auto e: graph[cur.num]){\n if(cur.tm > e.c)continue;\n if(e.to == n){\n ans = max(ans, e.c);\n }else{\n if(e.c >= mini[e.to])continue;\n mini[e.to] = e.c;\n que.push((State){e.to, e.c});\n }\n }\n }\n\n cout << ans << endl;\n\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 9004, "score_of_the_acc": -0.4769, "final_rank": 15 }, { "submission_id": "aoj_1590_4119967", "code_snippet": "#include<algorithm>\n#include<bitset>\n#include<cassert>\n#include<cfloat>\n#include<climits>\n#include<cmath>\n#include<deque>\n#include<functional>\n#include<iomanip>\n#include<iostream>\n#include<map>\n#include<queue>\n#include<set>\n#include<stack>\n#include<string>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<vector>\n\nusing namespace std;\n\nusing lint = long long;\nusing P = pair<int, int>;\nusing LLP = pair<long long, long long>;\n\n#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)\n#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)\n#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)\n#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)\n#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)\n#define SORT(x) sort((x).begin(), (x).end())\n#define SORT_INV(x) sort((x).rbegin(), (x).rend())\n#define REVERSE(x) reverse((x).begin(), (x).end())\n#define TWINS(x) cout << ((x) ? \"Yay!\" : \":(\") << '\\n'\n#define endl '\\n'\n\nconstexpr int IINF = (1 << 30) - 1;\nconstexpr long long LLINF = 1LL << 61;\nconstexpr double EPS = 1e-10;\n\nconstexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};\nconstexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};\n\nstruct edge{\n int to, cost;\n};\n\nstruct node{\n int id, cost;\n\n bool operator<(const node& rhs) const {\n return cost > rhs.cost;\n }\n};\n\ntemplate<typename T>\nbool chmax(T& a, T b){\n if(a < b){\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<typename T>\nbool chmin(T& a, T b){\n if(b < a){\n a = b;\n return true;\n }\n return false;\n}\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(10);\n\n int n, m;\n cin >> n >> m;\n\n vector< vector<edge> > g(n);\n rep(i, m){\n int a, b, c;\n cin >> a >> b >> c;\n g[--a].push_back({--b, c});\n }\n\n vector<int> cost(n, IINF);\n cost[0] = 0;\n priority_queue<node> que;\n que.push({0, 0});\n\n int ans = -1;\n\n while(!que.empty()){\n node now = que.top();\n que.pop();\n if(cost[now.id] < now.cost){\n continue;\n }\n for(auto e : g[now.id]){\n if(now.cost > e.cost){\n continue;\n }\n if(e.to == n - 1){\n chmax(ans, e.cost);\n }else if(chmin(cost[e.to], e.cost)){\n que.push({e.to, e.cost});\n }\n }\n }\n\n cout << ans << endl;\n\n cout << flush;\n\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 8624, "score_of_the_acc": -0.2524, "final_rank": 2 }, { "submission_id": "aoj_1590_3808506", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1590.cc: One-Time Path\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 100000;\nconst int INF = 1 << 30;\n\n/* typedef */\n\ntypedef pair<int,int> pii;\ntypedef vector<pii> vpii;\n\n/* global variables */\n\nvpii nbrs[MAX_N];\nint ds[MAX_N];\n\n/* subroutines */\n\n/* main */\n\nint main() {\n int n, m;\n scanf(\"%d%d\", &n, &m);\n\n for (int i = 0; i < m; i++) {\n int a, b, c;\n scanf(\"%d%d%d\", &a, &b, &c);\n a--, b--;\n //nbrs[a].push_back(pii(b, c));\n nbrs[b].push_back(pii(a, c));\n }\n\n int st = 0, gl = n - 1;\n int maxd = -1;\n vpii &nbrgl = nbrs[gl];\n\n for (vpii::iterator git = nbrgl.begin(); git != nbrgl.end(); git++) {\n int &gv = git->first, &gd = git->second;\n fill(ds, ds + n, -1);\n ds[gl] = ds[gv] = gd;\n\n priority_queue<pii> q;\n q.push(pii(gd, gv));\n\n while (! q.empty()) {\n pii u = q.top(); q.pop();\n int &ud = u.first, &ui = u.second;\n if (ud != ds[ui]) continue;\n if (ui == st) break;\n\n vpii &nbru = nbrs[ui];\n\n for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {\n\tint &vi = vit->first, &vd = vit->second;\n\tif (vd <= ud && ds[vi] < vd) {\n\t ds[vi] = vd;\n\t q.push(pii(vd, vi));\n\t}\n }\n }\n\n if (ds[st] >= 0 && maxd < gd) maxd = gd;\n }\n\n printf(\"%d\\n\", maxd);\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 9536, "score_of_the_acc": -0.2863, "final_rank": 4 }, { "submission_id": "aoj_1590_3689448", "code_snippet": "#include <iostream>\n#include <queue>\n#include <set>\n#include <vector>\n\nusing namespace std;\n\n#define REP(i, n) for (int i = 0; i < n; ++i)\n\nstruct E {\n int from, to, c;\n};\n\nusing G = vector<vector<E>>;\n\nint main() {\n int n, m;\n cin >> n >> m;\n G g(n);\n set<pair<int, int>> st;\n REP(i, m) {\n int a, b, c;\n cin >> a >> b >> c;\n a--;\n b--;\n if (b == n - 1)\n st.insert({a, c});\n else\n g[a].push_back({a, b, c});\n }\n using P = pair<int, int>;\n constexpr int INF = 1e9 + 1;\n vector<int> d(n, INF);\n d[0] = 0;\n priority_queue<P, vector<P>, greater<>> q;\n q.push({0, 0});\n while (!q.empty()) {\n P p = q.top();\n q.pop();\n if (p.first > d[p.second])\n continue;\n for (auto e : g[p.second]) {\n if (e.c >= d[p.second] && d[e.to] > e.c) {\n d[e.to] = e.c;\n q.push({e.c, e.to});\n }\n }\n }\n\n int ans = -1;\n for (auto a : st) {\n if (d[a.first] <= a.second)\n ans = max(ans, a.second);\n }\n cout << ans << endl;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 8996, "score_of_the_acc": -0.4764, "final_rank": 14 }, { "submission_id": "aoj_1590_3668675", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<int> ans;\n//scc\nstruct graph {\nprivate:\n\tint n; vector<vector<int>> G, rG;\n\tvector<bool> used;\n\tvector<int> vs;\n\n\tint mk;\n\tvector<vector<int>> fG;\n\tvector<vector<int>> ori;\n\tvector<int> trans;\npublic:\n\tgraph(int sz) {\n\t\tn = sz;\n\t\tG.resize(n);\n\t\trG.resize(n);\n\t\tused.resize(n);\n\n\t\tfG.resize(n);\n\t\ttrans.resize(n, -1);\n\t\tori.resize(n);\n\t}\n\tvoid add_edge(int a, int b) {\n\t\tG[a].push_back(b);\n\t\trG[b].push_back(a);\n\t}\n\tvoid dfs(int v) {\n\t\tused[v] = true;\n\t\trep(i, G[v].size()) {\n\t\t\tif (!used[G[v][i]])dfs(G[v][i]);\n\t\t}\n\t\tvs.push_back(v);\n\t}\n\tvoid rdfs(int v, int k) {\n\t\tused[v] = true;\n\t\tqueue<int> q;\n\t\tq.push(v);\n\t\tvector<int> c;\n\t\twhile (!q.empty()) {\n\t\t\tint id = q.front(); q.pop();\n\t\t\tori[k].push_back(id);\n\t\t\trep(j, rG[id].size()) {\n\t\t\t\tint to = rG[id][j];\n\t\t\t\tif (used[to]) {\n\t\t\t\t\tif (trans[to] >= 0)c.push_back(trans[to]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tused[to] = true; q.push(to);\n\t\t\t}\n\t\t}\n\t\tsort(c.begin(), c.end());\n\t\tint len = unique(c.begin(), c.end()) - c.begin();\n\t\trep(i, len) {\n\t\t\tfG[c[i]].push_back(k);\n\t\t}\n\t\trep(i, ori[k].size()) {\n\t\t\ttrans[ori[k][i]] = k;\n\t\t}\n\t}\n\tvoid scc() {\n\t\tfill(used.begin(), used.end(), false);\n\t\trep(i, n)if (!used[i])dfs(i);\n\t\tfill(used.begin(), used.end(), false);\n\t\tint k = 0;\n\t\tper(i, (int)vs.size()) {\n\t\t\tif (!used[vs[i]]) {\n\t\t\t\trdfs(vs[i], k); k++;\n\t\t\t}\n\t\t}\n\t\tmk = k;\n\t}\n\tvoid query(vector<int> &x,int &cur,int &sz) {\n\t\tvector<int> c(mk, 0);\n\t\tvector<int> val(mk, -1);\n\t\trep(i, mk) {\n\t\t\trep(j, ori[i].size()) {\n\t\t\t\tval[i] = max(val[i], ans[x[ori[i][j]]]);\n\t\t\t\tif (x[ori[i][j]] == sz - 1) {\n\t\t\t\t\tval[i] = max(val[i], cur);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep(i, mk) {\n\t\t\trep(j, fG[i].size()) {\n\t\t\t\tint to = fG[i][j];\n\t\t\t\tc[to]++;\n\t\t\t}\n\t\t}\n\t\tqueue<int> q;\n\t\trep(i, mk)if (c[i] == 0)q.push(i);\n\t\twhile (!q.empty()) {\n\t\t\tint id = q.front(); q.pop();\n\t\t\trep(j, fG[id].size()) {\n\t\t\t\tint to = fG[id][j]; c[to]--;\n\t\t\t\tif (c[to] == 0) {\n\t\t\t\t\tq.push(to);\n\t\t\t\t}\n\t\t\t\tval[to] = max(val[to], val[id]);\n\t\t\t}\n\t\t}\n\t\trep(i, mk) {\n\t\t\trep(j, ori[i].size()) {\n\t\t\t\tif (x[ori[i][j]] == sz - 1)continue;\n\t\t\t\tans[x[ori[i][j]]] = val[i];\n\t\t\t}\n\t\t}\n\t}\n};\n\n\nvoid to_unique(vector<int> &v) {\n\tsort(v.begin(), v.end());\n\tv.erase(unique(v.begin(), v.end()), v.end());\n}\nvoid solve() {\n\tint n, m; cin >> n >> m;\n\tans.resize(n, -1);\n\tvector<pair<int, P>> v;\n\trep(i, m) {\n\t\tint a, b, c; cin >> a >> b >> c;\n\t\ta--; b--;\n\t\tif (a == n - 1)continue;\n\t\tv.push_back({ c,{ b,a } });\n\t}\n\tsort(v.begin(), v.end(), greater<pair<int,P>>());\n\trep(i, v.size()) {\n\t\tint le = i;\n\t\twhile (i + 1 < (int)v.size() && v[i].first == v[i + 1].first) {\n\t\t\ti++;\n\t\t}\n\t\tif (i == le) {\n\t\t\tint id = v[i].second.first; int to = v[i].second.second;\n\t\t\tans[to] = max(ans[to], ans[id]);\n\t\t\tif (id == n - 1)ans[to] = max(ans[to], v[i].first);\n\t\t\tcontinue;\n\t\t}\n\t\tvector<int> x;\n\t\tvector<P> u;\n\t\tRep(j, le, i+1) {\n\t\t\tu.push_back(v[j].second);\n\t\t\tx.push_back(v[j].second.first);\n\t\t\tx.push_back(v[j].second.second);\n\t\t}\n\t\tto_unique(x);\n\t\t{\n\t\t\tmap<int, int> mp;\n\t\t\trep(j, x.size()) {\n\t\t\t\tmp[x[j]] = j;\n\t\t\t}\n\t\t\trep(j, u.size()) {\n\t\t\t\tu[j].first = mp[u[j].first];\n\t\t\t\tu[j].second = mp[u[j].second];\n\t\t\t}\n\t\t}\n\t\tgraph g(x.size());\n\t\trep(j, u.size()) {\n\t\t\t//cout << x[u[j].first] << \" \" << x[u[j].second] << endl;\n\t\t\tg.add_edge(u[j].first, u[j].second);\n\t\t}\n\t\tg.scc(); g.query(x,v[i].first,n);\n\t}\n\t//rep(i, n)cout << ans[i] << endl;\n\tcout << ans[0] << endl;\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 20572, "score_of_the_acc": -1.2, "final_rank": 18 }, { "submission_id": "aoj_1590_3417704", "code_snippet": "#include<iostream>\n#include<vector>\n#include<queue>\nusing namespace std;\n\nconst int INF = 1<<30;\n\nint main(){\n int n, m;\n cin >> n >> m;\n vector<pair<int,int>> v[n];\n int a, b, c;\n while(m-- > 0){\n cin >> a >> b >> c;\n a--, b--;\n v[a].push_back({b,c});\n }\n priority_queue<pair<int,int>> pq;\n pq.push({-0, 0});\n vector<int> d(n, 1<<30);\n int ans = 0;\n while(!pq.empty()){\n pair<int,int> p = pq.top(); pq.pop();\n int cost = -p.first, pos = p.second;\n if(d[pos] <= cost) continue;\n d[pos] = cost;\n for(pair<int,int> q : v[pos]){\n int to = q.first, c = q.second;\n if(cost > c) continue;\n if(to == n-1){\n ans = max(ans, c);\n }else if(d[to] > c){\n pq.push({-c, to});\n }\n }\n }\n cout << (ans == 0 ? -1 : ans) << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 8636, "score_of_the_acc": -0.4531, "final_rank": 12 }, { "submission_id": "aoj_1590_3409132", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> P;\ntypedef pair<int,P> P3;\ntypedef pair<double, P3> P4;\nconst ll MOD = ll(1e9+7);\nconst int IINF = INT_MAX;\nconst ll LLINF = LLONG_MAX;\nconst int MAX_N = int(1e5 + 5);\nconst double EPS = 1e-6;\nconst int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};\n#define REP(i, n) for (int i = 0; i < n; i++)\n#define REPR(i, n) for (int i = n; i >= 0; i--)\n#define SORT(v) sort((v).begin(), (v).end())\n#define ALL(v) (v).begin(), (v).end()\n\nint n, m, dmin[MAX_N],dmax[MAX_N];\nvector<vector<P> > g;\n\nint dijkstra(){\n fill(dmax, dmax+n, -1);\n fill(dmin, dmin+n, IINF);\n priority_queue<P, vector<P>, greater<P> > que;\n que.push({0,0});\n while(!que.empty()){\n P p = que.top();\n que.pop();\n int t = p.first, v = p.second;\n if(dmin[v] < t)continue;\n for(auto &e : g[v]){\n int u = e.first, c = e.second;\n if(c < t)continue;\n if(dmin[u] > c){\n dmin[u] = c;\n que.push({c,u});\n }\n dmax[u] = max(dmax[u],c);\n }\n }\n}\n\nint main() {\n cin >> n >> m;\n g.resize(n);\n REP(i,m){\n int a, b, c;\n cin >> a >> b >> c;\n a--; b--;\n g[a].push_back({b,c});\n }\n dijkstra();\n if(dmin[n-1]==IINF){\n cout << -1 << endl;\n }\n else{\n cout << dmax[n-1] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 8920, "score_of_the_acc": -0.4715, "final_rank": 13 }, { "submission_id": "aoj_1590_3132639", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int INF = (1 << 30) - 1;\n\nstruct Edge\n{\n int to, time;\n Edge () {}\n Edge ( int to, int time ) : to(to), time(time) {}\n};\n\nint N, M;\nusing T = pair < int , int >;\npriority_queue < T , vector < T > , greater < T > > pq;\nint min_cost[100005];\nvector < Edge > G[100005];\n\nint main()\n{\n int maxv = -1;\n\n scanf(\"%d %d\", &N, &M);\n\n while ( M-- ) {\n int a, b, c;\n scanf(\"%d %d %d\", &a, &b, &c); --a, --b;\n G[a].emplace_back(b, c);\n }\n\n fill_n(min_cost, 100005, INF);\n min_cost[0] = 0;\n pq.emplace(0, 0);\n\n while ( !pq.empty() ) {\n T d = pq.top(); pq.pop();\n if ( d.first > min_cost[d.second] ) continue;\n for ( auto x : G[d.second] ) {\n if ( x.to == N - 1 ) {\n if ( d.first <= x.time ) {\n maxv = max(maxv, x.time);\n }\n } else {\n if ( d.first <= x.time && x.time < min_cost[x.to] ) {\n min_cost[x.to] = x.time;\n pq.emplace(x.time, x.to);\n }\n }\n }\n }\n cout << maxv << endl;\n\n return ( 0 );\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 9532, "score_of_the_acc": -0.2861, "final_rank": 3 }, { "submission_id": "aoj_1590_3061737", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << \"(\" << o.first << \",\" << o.second << \")\"; return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << \" \";} return out; }\ntemplate<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }\ntemplate<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << \"{ \"; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << \":\" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << \", \"; } out << \" }\"; return out; }\n\n/*\n <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1590>\n 問題文============================================================\n =================================================================\n 解説=============================================================\n ================================================================\n */\n\n\nll solve(){\n ll res = -1;\n ll N,M; cin >> N >> M;\n vector<vector<pll>> G(N);\n using Event = tuple<ll,ll,ll>;\n vector<Event> E;\n for(int i = 0; i < M;i++){\n ll a,b,c; cin >> a >> b >> c;\n a--; b--;\n E.push_back(Event(a,b,c));\n }\n sort(E.begin(),E.end(),[](const Event & e1,const Event& e2){\n return std::get<2>(e1) < std::get<2>(e2);\n });\n \n \n vector<int> flag(N); flag[0] = 1;\n vector<Event> event;\n int idx = 0;\n ll nowT = 0;\n while(idx < M){\n ll a,b,c; tie(a,b,c) = E[idx++];\n if(nowT == c) {\n event.push_back(Event(a,b,c));\n }else{\n nowT = c;\n bool update = false;\n while(true){\n update = false;\n for(int i = 0; i < event.size();i++){\n ll u,v,T; tie(u,v,T) = event[i];\n if(flag[u]){\n if(flag[v]){\n if(v == N-1) res = max(res,T);\n continue;\n //event.erase(event.begin()+i);\n }else{\n flag[v] = 1;\n update = true;\n }\n }\n }\n if(update) continue;\n break;\n }\n event.clear();\n event.push_back(Event(a,b,c));\n }\n }\n bool update = false;\n while(true){\n update = false;\n for(int i = 0; i < event.size();i++){\n ll u,v,T; tie(u,v,T) = event[i];\n if(flag[u]){\n if(flag[v]){\n if(v == N-1) res = max(res,T);\n continue;\n //event.erase(event.begin()+i);\n }else{\n flag[v] = 1;\n update = true;\n }\n }\n }\n if(update) continue;\n break;\n }\n return res;\n}\nint main(void) {\n cin.tie(0); ios_base::sync_with_stdio(false);\n cout << solve() << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 13676, "score_of_the_acc": -1.5541, "final_rank": 20 }, { "submission_id": "aoj_1590_3042325", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nstruct E{\n ll to,cost;\n E(ll to,ll cost):to(to),cost(cost){}\n};\ntypedef pair<ll,ll> P;\nint main(){\n ll n,m,i,j,k,a,b,c;\n cin>>n>>m;\n vector<E> v[n];\n vector<E> can;\n for(i=0;i<m;i++){\n cin >> a >> b >> c;\n a--;b--;\n if(b==n-1) can.push_back(E(a,c));\n else v[a].push_back(E(b,c));\n }\n ll g[n];\n memset(g,-1,sizeof(g));\n priority_queue<P,vector<P>,greater<P> > q;\n q.push(P(1,0));\n while(!q.empty()){\n P p=q.top();q.pop();\n b=p.first;a=p.second;\n if(~g[a]&&g[a]<=b) continue;\n g[a]=b;\n for(i=0;i<v[a].size();i++){\n if(b<=v[a][i].cost)\n\tif(!~g[v[a][i].to]||g[v[a][i].to]>v[a][i].cost)\n\t q.push(P(v[a][i].cost,v[a][i].to));\n }\n }\n ll ans=-1;\n for(i=0;i<can.size();i++)\n if(~g[can[i].to]&&g[can[i].to]<=can[i].cost) ans=max(ans,can[i].cost);\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 12020, "score_of_the_acc": -0.672, "final_rank": 17 }, { "submission_id": "aoj_1590_2662295", "code_snippet": "#include \"bits/stdc++.h\"\n#include<unordered_map>\n#include<unordered_set>\n#pragma warning(disable:4996)\nusing namespace std;\n\n\nint main() {\n\tint N,M;cin>>N>>M;\n\tvector<vector<pair<int,int>>>edges(N);\n\tvector<int>memo(N-1,1e9+1e7);\n\tmemo[0]=0;\n\n\tfor (int i = 0; i < M; ++i) {\n\t\tint a,b,c;cin>>a>>b>>c;\n\t\ta--;b--;\n\t\tedges[a].push_back(make_pair(b,c));\n\t}\n\n\tpriority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>que;\n\tque.push(make_pair(0,0));\n\twhile (!que.empty()) {\n\t\tauto atop(que.top());\n\t\tque.pop();\n\t\tconst int now=atop.second;\n\t\tconst int now_time=atop.first;\n\t\tif(memo[now]<now_time)continue;\n\t\tfor (auto e : edges[now]) {\n\t\t\tif(e.first==N-1)continue;\n\t\t\tif (e.second >= now_time) {\n\t\t\t\tif (memo[e.first] > e.second) {\n\t\t\t\t\tque.push(make_pair(e.second,e.first));\n\t\t\t\t\tmemo[e.first]=e.second;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tint ans=-1;\n\tfor (int i = 0; i < N - 1; ++i) {\n\t\tfor (auto e : edges[i]) {\n\t\t\tif (e.first==N-1&&memo[i] <= e.second) {\n\t\t\t\tans=max(ans,e.second);\n\t\t\t}\n\t\t}\n\t}\n\t{\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 8704, "score_of_the_acc": -0.4825, "final_rank": 16 }, { "submission_id": "aoj_1590_2626655", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<\"(\"<<p.fi<<\",\"<<p.se<<\")\";return o;}\ntemplate<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<\"[\";for(T t:v){o<<t<<\",\";}o<<\"]\";return o;}\n\nusing pi = pair<int,int>;\nusing E = pair<int,pi>;\n\nint main()\n{\n int n,m;\n cin >>n >>m;\n\n vector<E> e(m);\n rep(i,m)\n {\n int a,b,c;\n cin >>a >>b >>c;\n --a;\n --b;\n e[i] = {c,{a,b}};\n }\n sort(all(e));\n\n int ans = -1;\n vector<bool> vis(n);\n vis[0] = true;\n rep(i,m)\n {\n pi p = e[i].se;\n\n if(vis[p.fi])\n {\n vis[p.se] = true;\n if(p.se == n-1) ans = e[i].fi;\n }\n }\n\n cout << ans << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 5108, "score_of_the_acc": -0.2, "final_rank": 1 }, { "submission_id": "aoj_1590_2585388", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Info{\n\tInfo(int arg_to,int arg_time){\n\t\tto = arg_to;\n\t\ttime = arg_time;\n\t}\n\tint to,time;\n};\n\nstruct Data{\n\tData(int arg_node_id,int arg_current_time){\n\t\tnode_id = arg_node_id;\n\t\tcurrent_time = arg_current_time;\n\t}\n\tbool operator<(const struct Data &arg) const{\n\t\treturn current_time > arg.current_time;\n\t}\n\tint node_id,current_time;\n};\n\nvector<Info> V[100000];\nint min_time[100000];\n\nint main(){\n\n\tint N,M;\n\tscanf(\"%d %d\",&N,&M);\n\n\tint from,to,time;\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%d %d %d\",&from,&to,&time);\n\t\tfrom--;\n\t\tto--;\n\t\tV[from].push_back(Info(to,time));\n\t}\n\n\tfor(int i = 0; i < N; i++)min_time[i] = BIG_NUM;\n\n\tmin_time[0] = 0;\n\n\tpriority_queue<Data> Q;\n\tQ.push(Data(0,0));\n\n\tint ans = -1;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().current_time > min_time[Q.top().node_id]){\n\t\t\tQ.pop();\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < V[Q.top().node_id].size(); i++){\n\t\t\t\tif(Q.top().current_time > V[Q.top().node_id][i].time)continue;\n\n\t\t\t\tif(V[Q.top().node_id][i].to == N-1){\n\t\t\t\t\tans = max(ans,V[Q.top().node_id][i].time);\n\t\t\t\t}else{\n\t\t\t\t\tif(min_time[V[Q.top().node_id][i].to] > V[Q.top().node_id][i].time){\n\t\t\t\t\t\tmin_time[V[Q.top().node_id][i].to] = V[Q.top().node_id][i].time;\n\t\t\t\t\t\tQ.push(Data(V[Q.top().node_id][i].to,V[Q.top().node_id][i].time));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 9504, "score_of_the_acc": -0.3093, "final_rank": 7 }, { "submission_id": "aoj_1590_2585383", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\nstruct Info{\n\tInfo(int arg_to,int arg_time){\n\t\tto = arg_to;\n\t\ttime = arg_time;\n\t}\n\tint to,time;\n};\n\nstruct Data{\n\tData(int arg_node_id,int arg_current_time){\n\t\tnode_id = arg_node_id;\n\t\tcurrent_time = arg_current_time;\n\t}\n\tbool operator<(const struct Data &arg) const{\n\t\treturn current_time > arg.current_time;\n\t}\n\tint node_id,current_time;\n};\n\nvector<Info> V[100000];\nint min_time[100000];\n\nint main(){\n\n\tint N,M;\n\tscanf(\"%d %d\",&N,&M);\n\n\tint from,to,time;\n\tfor(int i = 0; i < M; i++){\n\t\tscanf(\"%d %d %d\",&from,&to,&time);\n\t\tfrom--;\n\t\tto--;\n\t\tV[from].push_back(Info(to,time));\n\t}\n\n\tfor(int i = 0; i < N; i++)min_time[i] = BIG_NUM;\n\n\tmin_time[0] = 0;\n\n\tpriority_queue<Data> Q;\n\tQ.push(Data(0,0));\n\n\tint ans = -1;\n\n\twhile(!Q.empty()){\n\n\t\tif(Q.top().node_id == N-1){\n\t\t\tans = max(ans,Q.top().current_time);\n\t\t\tQ.pop();\n\t\t}else if(Q.top().current_time > min_time[Q.top().node_id]){\n\t\t\tQ.pop();\n\t\t}else{\n\n\t\t\tfor(int i = 0; i < V[Q.top().node_id].size(); i++){\n\t\t\t\tif(Q.top().current_time > V[Q.top().node_id][i].time)continue;\n\n\t\t\t\tif(min_time[V[Q.top().node_id][i].to] > V[Q.top().node_id][i].time){\n\t\t\t\t\tmin_time[V[Q.top().node_id][i].to] = V[Q.top().node_id][i].time;\n\t\t\t\t\tQ.push(Data(V[Q.top().node_id][i].to,V[Q.top().node_id][i].time));\n\t\t\t\t}else if(V[Q.top().node_id][i].to == N-1){\n\t\t\t\t\tQ.push(Data(V[Q.top().node_id][i].to,V[Q.top().node_id][i].time));\n\t\t\t\t}\n\t\t\t}\n\t\t\tQ.pop();\n\t\t}\n\t}\n\n\tprintf(\"%d\\n\",ans);\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 9504, "score_of_the_acc": -0.3343, "final_rank": 10 }, { "submission_id": "aoj_1590_2568353", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define MAX_N 100000\nstruct edge{ int to,cost; };\ntypedef pair<int,int> P;\nconst int INF=1000000001;\n \nint N,M;\nint d[MAX_N];\nvector<edge> G[MAX_N];\n \nint main(){\n cin>>N>>M;\n for(int i=0;i<M;i++){\n int a,b,c;\n scanf(\"%d %d %d\",&a,&b,&c);\n G[a].push_back((edge){b,c});\n }\n int ans=-1; \n priority_queue< P , vector<P> , greater<P> > Q;\n fill(d,d+MAX_N,INF);\n Q.push(P(1,1));\n d[1]=1;\n while(!Q.empty()){\n P p=Q.top();Q.pop();\n int pos=p.second;\n int cost=p.first;\n if(d[pos]<cost)continue;\n for(int i=0;i<(int)G[pos].size();i++){\n edge e=G[pos][i];\n if(e.cost<d[pos])continue;\n if(e.to==N)ans=max(ans,e.cost);\n if(d[e.to]>e.cost){\n d[e.to]=e.cost;\n Q.push(P(d[e.to],e.to));\n }\n }\n }\n printf(\"%d\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 9564, "score_of_the_acc": -0.3132, "final_rank": 8 }, { "submission_id": "aoj_1590_2558301", "code_snippet": "#include <cstdio>\n#include <climits>\n#include <vector>\n#include <queue>\n\nconstexpr int MAX_N = 100000;\nconstexpr int INF = INT_MAX;\n\ntemplate<class T>\nusing vec = std::vector<T>;\n\nstruct Edge {\n int to, age;\n Edge (int to, int age) : to{to}, age{age} {}\n};\n\nstruct State {\n int c, v;\n State (int c, int v) : c{c}, v{v} {}\n\n bool operator < (const State &s) const {\n return c > s.c;\n }\n};\n\nint main()\n{\n int N, M, a, b, c;\n scanf(\"%d %d\", &N, &M);\n \n vec<Edge> G[MAX_N];\n for (int i = 0; i < M; i++) {\n scanf(\"%d %d %d\", &a, &b, &c); a--; b--;\n G[a].push_back(Edge(b, c));\n }\n \n vec<int> d(MAX_N + 1, INF);\n d[0] = 1;\n\n std::priority_queue<State> pq;\n pq.push(State(1, 0));\n int res = -1;\n \n while (!pq.empty()) {\n State s = pq.top(); pq.pop();\n if (s.v == N - 1 || d[s.v] < s.c) continue;\n \n for (Edge &e : G[s.v]) {\n if (d[s.v] > e.age) continue;\n if (e.to == N - 1) {\n res = std::max(res, e.age);\n }\n \n if (e.age < d[e.to]) {\n d[e.to] = e.age;\n pq.push(State(d[e.to], e.to));\n }\n }\n }\n \n printf(\"%d\\n\", res);\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 9100, "score_of_the_acc": -0.3081, "final_rank": 6 }, { "submission_id": "aoj_1590_2558244", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define MAX_N 100000\nstruct edge{ int to,cost; };\ntypedef pair<int,int> P;\nconst int INF=1000000001;\n\nint N,M;\nint d[MAX_N];\nvector<edge> G[MAX_N];\n\nint main(){\n cin>>N>>M;\n for(int i=0;i<M;i++){\n int a,b,c;\n scanf(\"%d %d %d\",&a,&b,&c);\n G[a].push_back((edge){b,c});\n }\n int ans=-1; \n priority_queue< P , vector<P> , greater<P> > Q;\n fill(d,d+MAX_N,INF);\n Q.push(P(1,1));\n d[1]=1;\n while(!Q.empty()){\n P p=Q.top();Q.pop();\n int pos=p.second;\n int cost=p.first;\n if(d[pos]<cost)continue;\n for(int i=0;i<(int)G[pos].size();i++){\n edge e=G[pos][i];\n if(e.cost<d[pos])continue;\n if(e.to==N)ans=max(ans,e.cost);\n if(d[e.to]>e.cost){\n d[e.to]=e.cost;\n Q.push(P(d[e.to],e.to));\n }\n }\n }\n printf(\"%d\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 9660, "score_of_the_acc": -0.3194, "final_rank": 9 } ]
aoj_1599_cpp
Problem M: Dial Problem ダイヤル式の南京錠がある。この南京錠には、5つのダイヤルと、決定ボタンが用意されている。 5つあるダイヤルを回転させて16進数で5ケタの整数を作り、決定ボタンを押して入力すると、入力した値とパスワードが一致していれば開く仕組みになっている。5つのダイヤルそれぞれには 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2, ... の順で文字が現れるように書かれている。 持ち主はパスワードを忘れてしまった。しかし、パスワードの5ケタのうち、1つのケタだけが偶数で残り4つは奇数であったことは覚えている。 ただし、偶数とは0,2,4,6,8,a,c,eであり、奇数とは1,3,5,7,9,b,d,fである。 またこの鍵は不良品で、以下の条件でパスワードと異なる数字を入力しても開いてしまうことが分かっている。本来、入力した整数とパスワードが5つのケタすべてについて一致していなければ鍵は開かない。しかし、パスワードの偶数となっているケタが i ケタ目だった場合、 i ケタ目の一致判定を行うとき、その偶数の前後にある奇数が入力されていた場合でも i ケタ目は一致していると誤判定されてしまうのだ。 例えば、パスワードが"57677"の南京錠ならば、'6'の前後には'5'と'7'の奇数があるため"57677"のほかに、"57577"や"57777"を入力したときでも開いてしまう。 同様に、パスワードが"130bd"の南京錠ならば、'0'の前後には'1'と'f'の奇数があるため"130bd"のほかに、"13fbd"や"131bd"を入力したときでも開いてしまう。 今、パスワードの候補が N 個あり、それらのうちどれか1つが正解のパスワードであることが分かっている。しかたがないので持ち主は、鍵が開くまで何度も整数を入力して試し続けることにした。持ち主はできるだけ決定ボタンを押す回数を少なくしたい。 持ち主が最適な戦略をとったとき、最大で何回決定ボタンを押さなければならないだろうか? その回数を出力せよ。 また、実際に最大となるときに入力する整数の集合を昇順に出力せよ。 答えが一意に定まらない場合は、辞書順で最小となるものを出力せよ。 Input 入力は以下の形式で与えられる。 N password 1 password 2 ... password N 1行目に N が与えられる。 2行目から N +1行目の i 行目には、パスワードの候補を表す5ケタの16進数を表す文字列 password i が与えられる。 Constraints 1 ≤ N ≤ 6000 password i ≠ password j ( i < j ) 文字列は、0から9の数字または英小文字aからfの5文字で構成され、そのうち1文字は偶数であり、他の4文字は奇数である Output 鍵が開くまでに最適な戦略をとったときの決定ボタンを押す回数の最大値を1行に出力せよ。 続いて、最大となるときに入力する整数の集合を昇順に出力せよ。 もし、答えが一意に定まらない場合は、辞書順で最小となるものを出力せよ。 Sample Input 1 1 0111f Sample Output 1 1 0111f Sample Input 2 3 57567 57587 0d351 Sample Output 2 2 0d351 57577 Sample Input 3 6 1811b 1a11b f3b1e f3b1c b0df3 bedf3 Sample Output 3 3 1911b bfdf3 f3b1d
[ { "submission_id": "aoj_1599_3663954", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n//BEGIN CUT HERE\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}\n };\n\n vector<vector<edge> > G;\n vector<int> level,iter;\n\n Dinic(){}\n Dinic(int n):G(n),level(n),iter(n){}\n\n int add_edge(int from,int to,T cap){\n G[from].emplace_back(to,cap,G[to].size());\n G[to].emplace_back(from,directed?0:cap,G[from].size()-1);\n return G[to].back().rev;\n }\n\n void bfs(int s){\n fill(level.begin(),level.end(),-1);\n queue<int> que;\n level[s]=0;\n que.emplace(s);\n while(!que.empty()){\n int v=que.front();que.pop();\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n que.emplace(e.to);\n }\n }\n }\n }\n\n T dfs(int v,int t,T f){\n if(v==t) return f;\n for(int &i=iter[v];i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n T d=dfs(e.to,t,min(f,e.cap));\n if(d==0) continue;\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n return 0;\n }\n\n T flow(int s,int t,T lim){\n T fl=0;\n while(1){\n bfs(s);\n if(level[t]<0||lim==0) break;\n fill(iter.begin(),iter.end(),0);\n\n while(1){\n T f=dfs(s,t,lim);\n if(f==0) break;\n fl+=f;\n lim-=f;\n }\n }\n return fl;\n }\n\n T flow(int s,int t){\n return flow(s,t,numeric_limits<T>::max()/2);\n }\n\n T cut(int s,int t,int x,int a){\n static_assert(directed, \"must be directed\");\n auto &e=G[x][a];\n int y=e.to;\n T ce=e.cap,cr=G[y][e.rev].cap;\n if(cr==0) return 0;\n e.cap=G[y][e.rev].cap=0;\n T cap=cr-flow(x,y,cr);\n if(x!=s&&cap!=0) flow(x,s,cap);\n if(t!=y&&cap!=0) flow(t,y,cap);\n e.cap=ce+cr;\n return cap;\n }\n};\n//END CUT HERE\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n//INSERT ABOVE HERE\nsigned GRL_6_A(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n int V,E;\n cin>>V>>E;\n Dinic<int, true> dinic(V);\n for(int i=0;i<E;i++){\n int u,v,c;\n cin>>u>>v>>c;\n dinic.add_edge(u,v,c);\n }\n cout<<dinic.flow(0,V-1)<<endl;\n return 0;\n}\n\n/*\n verified on 2019/06/10\n http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A&lang=jp\n*/\n\n\nsigned SPOJ_FASTFLOW(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n Int n,m;\n cin>>n>>m;\n Dinic<Int, false> G(n);\n for(Int i=0;i<m;i++){\n Int a,b,c;\n cin>>a>>b>>c;\n if(a==b) continue;\n a--;b--;\n G.add_edge(a,b,c);\n }\n cout<<G.flow(0,n-1)<<endl;\n return 0;\n}\n/*\n verified on 2019/06/10\n https://www.spoj.com/problems/FASTFLOW/\n*/\n\nsigned SPOJ_BANKROB(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n,m,s,t;\n cin>>n>>m>>s>>t;\n s--;t--;\n const int INF=5050;\n Dinic<int, true> G(n*2);\n for(int i=0;i<m;i++){\n int x,y;\n cin>>x>>y;\n x--;y--;\n G.add_edge(n+x,y,INF);\n G.add_edge(n+y,x,INF);\n }\n\n for(int i=0;i<n;i++)\n G.add_edge(i,n+i,1);\n\n cout<<G.flow(n+s,t)<<endl;\n return 0;\n}\n/*\n verified on 2019/06/10\n https://www.spoj.com/problems/BANKROB/\n*/\n\nsigned AOJ_1599(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n;\n cin>>n;\n vector<string> es(n);\n for(int i=0;i<n;i++) cin>>es[i];\n\n const int L=5;\n vector<string> vs;\n for(string &s:es){\n for(char &c:s){\n if(isdigit(c)) c=c-'0';\n else c=c-'a'+10;\n }\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n s[i]=(s[i]+1)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+1)%16;\n }\n }\n es=compress(es);\n vs=compress(vs);\n auto vd=dict(vs);\n\n auto restore=\n [](string s){\n for(char &c:s){\n if(c>=10) c='a'+(c-10);\n else c='0'+c;\n }\n return s;\n };\n\n int m=vd.size();\n Dinic<int, true> G(m+2);\n int S=m,T=m+1;\n\n vector<int> used(m,0);\n using P = pair<int, int>;\n map<string, P> mp;\n\n for(string &s:es){\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n\n s[i]=(s[i]+1)%16;\n int x=vd[s];\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n int y=vd[s];\n s[i]=(s[i]+1)%16;\n\n int sum=0;\n for(char c:vs[x]) sum+=c/2;\n if(sum&1) swap(x,y);\n\n mp[s]=P(x,G.add_edge(x,y,1));\n\n if(!used[x]){\n mp[vs[x]]=P(S,G.add_edge(S,x,1));\n used[x]=1;\n }\n\n if(!used[y]){\n mp[vs[y]]=P(y,G.add_edge(y,T,1));\n used[y]=1;\n }\n }\n }\n\n vector<string> as;\n for(auto s:es) as.emplace_back(s);\n for(auto s:vs) as.emplace_back(s);\n sort(as.begin(),as.end());\n\n cout<<G.flow(S,T)<<\"\\n\";\n for(auto s:as){\n int x=mp[s].first;\n int a=mp[s].second;\n if(G.cut(S,T,x,a)==1){\n G.G[x][a].cap=0;\n cout<<restore(s)<<\"\\n\";\n }\n }\n cout<<flush;\n return 0;\n}\n/*\n verified on 2019/06/18\n http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1599\n*/\n\nsigned main(){\n //GRL_6_A();\n //SPOJ_FASTFLOW();\n //SPOJ_BANKROB();\n AOJ_1599();\n return 0;\n}", "accuracy": 1, "time_ms": 1780, "memory_kb": 8504, "score_of_the_acc": -1.9404, "final_rank": 3 }, { "submission_id": "aoj_1599_3663943", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}\n };\n\n vector<vector<edge> > G;\n vector<int> level,iter;\n\n Dinic(){}\n Dinic(int n):G(n),level(n),iter(n){}\n\n int add_edge(int from,int to,T cap){\n G[from].emplace_back(to,cap,G[to].size());\n G[to].emplace_back(from,directed?0:cap,G[from].size()-1);\n return G[to].back().rev;\n }\n\n void bfs(int s){\n fill(level.begin(),level.end(),-1);\n queue<int> que;\n level[s]=0;\n que.emplace(s);\n while(!que.empty()){\n int v=que.front();que.pop();\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n que.emplace(e.to);\n }\n }\n }\n }\n\n T dfs(int v,int t,T f){\n if(v==t) return f;\n for(int &i=iter[v];i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n T d=dfs(e.to,t,min(f,e.cap));\n if(d==0) continue;\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n return 0;\n }\n\n T flow(int s,int t,T lim){\n T fl=0;\n while(1){\n bfs(s);\n if(level[t]<0||lim==0) break;\n fill(iter.begin(),iter.end(),0);\n\n while(1){\n T f=dfs(s,t,lim);\n if(f==0) break;\n fl+=f;\n lim-=f;\n }\n }\n return fl;\n }\n\n T flow(int s,int t){\n return flow(s,t,numeric_limits<T>::max()/2);\n }\n\n T cut(int s,int t,int x,int a){\n static_assert(directed, \"must be directed\");\n auto &e=G[x][a];\n int y=e.to;\n T ce=e.cap,cr=G[y][e.rev].cap;\n if(cr==0) return 0;\n e.cap=G[y][e.rev].cap=0;\n T cap=cr-flow(x,y,cr);\n if(x!=s&&cap!=0) flow(x,s,cap);\n if(t!=y&&cap!=0) flow(t,y,cap);\n e.cap=ce+cr;\n return cap;\n }\n};\n\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n;\n cin>>n;\n vector<string> es(n);\n for(int i=0;i<n;i++) cin>>es[i];\n\n const int L=5;\n vector<string> vs;\n for(string &s:es){\n for(char &c:s){\n if(isdigit(c)) c=c-'0';\n else c=c-'a'+10;\n }\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n s[i]=(s[i]+1)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+1)%16;\n }\n }\n es=compress(es);\n vs=compress(vs);\n auto vd=dict(vs);\n\n auto restore=\n [](string s){\n for(char &c:s){\n if(c>=10) c='a'+(c-10);\n else c='0'+c;\n }\n return s;\n };\n\n int m=vd.size();\n Dinic<int, true> G(m+2);\n int S=m,T=m+1;\n\n vector<int> used(m,0);\n using P = pair<int, int>;\n map<string, P> mp;\n\n for(string &s:es){\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n\n s[i]=(s[i]+1)%16;\n int x=vd[s];\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n int y=vd[s];\n s[i]=(s[i]+1)%16;\n\n int sum=0;\n for(char c:vs[x]) sum+=c/2;\n if(sum&1) swap(x,y);\n\n mp[s]=P(x,G.add_edge(x,y,1));\n\n if(!used[x]){\n mp[vs[x]]=P(S,G.add_edge(S,x,1));\n used[x]=1;\n }\n\n if(!used[y]){\n mp[vs[y]]=P(y,G.add_edge(y,T,1));\n used[y]=1;\n }\n }\n }\n\n vector<string> as;\n for(auto s:es) as.emplace_back(s);\n for(auto s:vs) as.emplace_back(s);\n sort(as.begin(),as.end());\n\n cout<<G.flow(S,T)<<\"\\n\";\n for(auto s:as){\n int x=mp[s].first;\n int a=mp[s].second;\n if(G.cut(S,T,x,a)==1){\n G.G[x][a].cap=0;\n cout<<restore(s)<<\"\\n\";\n }\n }\n cout<<flush;\n return 0;\n}", "accuracy": 1, "time_ms": 1720, "memory_kb": 8492, "score_of_the_acc": -1.8758, "final_rank": 1 }, { "submission_id": "aoj_1599_3663939", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}\n };\n\n vector<vector<edge> > G;\n vector<int> level,iter;\n\n Dinic(){}\n Dinic(int n):G(n),level(n),iter(n){}\n\n int add_edge(int from,int to,T cap){\n G[from].emplace_back(to,cap,G[to].size());\n G[to].emplace_back(from,directed?0:cap,G[from].size()-1);\n return G[to].back().rev;\n }\n\n void bfs(int s){\n fill(level.begin(),level.end(),-1);\n queue<int> que;\n level[s]=0;\n que.emplace(s);\n while(!que.empty()){\n int v=que.front();que.pop();\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n que.emplace(e.to);\n }\n }\n }\n }\n\n T dfs(int v,int t,T f){\n if(v==t) return f;\n for(int &i=iter[v];i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n T d=dfs(e.to,t,min(f,e.cap));\n if(d==0) continue;\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n return 0;\n }\n\n T flow(int s,int t,T lim){\n T fl=0;\n while(1){\n bfs(s);\n if(level[t]<0||lim==0) break;\n fill(iter.begin(),iter.end(),0);\n\n while(1){\n T f=dfs(s,t,lim);\n if(f==0) break;\n fl+=f;\n lim-=f;\n }\n }\n return fl;\n }\n\n T flow(int s,int t){\n return flow(s,t,numeric_limits<T>::max()/2);\n }\n\n T cut(int s,int t,int x,int a){\n static_assert(directed, \"must be directed\");\n auto &e=G[x][a];\n int y=e.to;\n T ce=e.cap,cr=G[y][e.rev].cap;\n if(cr==0) return 0;\n e.cap=G[y][e.rev].cap=0;\n T cap=cr-flow(x,y,cr);\n if(x!=s) flow(x,s,cap);\n if(t!=y) flow(t,y,cap);\n e.cap=ce+cr;\n return cap;\n }\n};\n\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n;\n cin>>n;\n vector<string> es(n);\n for(int i=0;i<n;i++) cin>>es[i];\n\n const int L=5;\n vector<string> vs;\n for(string &s:es){\n for(char &c:s){\n if(isdigit(c)) c=c-'0';\n else c=c-'a'+10;\n }\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n s[i]=(s[i]+1)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+1)%16;\n }\n }\n es=compress(es);\n vs=compress(vs);\n auto vd=dict(vs);\n\n auto restore=\n [](string s){\n for(char &c:s){\n if(c>=10) c='a'+(c-10);\n else c='0'+c;\n }\n return s;\n };\n\n int m=vd.size();\n Dinic<int, true> G(m+2);\n int S=m,T=m+1;\n\n vector<int> used(m,0);\n using P = pair<int, int>;\n map<string, P> mp;\n\n for(string &s:es){\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n\n s[i]=(s[i]+1)%16;\n int x=vd[s];\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n int y=vd[s];\n s[i]=(s[i]+1)%16;\n\n int sum=0;\n for(char c:vs[x]) sum+=c/2;\n if(sum&1) swap(x,y);\n\n mp[s]=P(x,G.add_edge(x,y,1));\n\n if(!used[x]){\n mp[vs[x]]=P(S,G.add_edge(S,x,1));\n used[x]=1;\n }\n\n if(!used[y]){\n mp[vs[y]]=P(y,G.add_edge(y,T,1));\n used[y]=1;\n }\n }\n }\n\n vector<string> as;\n for(auto s:es) as.emplace_back(s);\n for(auto s:vs) as.emplace_back(s);\n sort(as.begin(),as.end());\n\n cout<<G.flow(S,T)<<\"\\n\";\n for(auto s:as){\n int x=mp[s].first;\n int a=mp[s].second;\n if(G.cut(S,T,x,a)==1){\n G.G[x][a].cap=0;\n cout<<restore(s)<<\"\\n\";\n }\n }\n cout<<flush;\n return 0;\n}", "accuracy": 1, "time_ms": 1830, "memory_kb": 8524, "score_of_the_acc": -2, "final_rank": 4 }, { "submission_id": "aoj_1599_3663867", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}\n };\n\n vector<vector<edge> > G;\n vector<int> level,iter;\n\n Dinic(){}\n Dinic(int n):G(n),level(n),iter(n){}\n\n void add_edge(int from,int to,T cap){\n G[from].emplace_back(to,cap,G[to].size());\n G[to].emplace_back(from,directed?0:cap,G[from].size()-1);\n }\n\n void bfs(int s){\n fill(level.begin(),level.end(),-1);\n queue<int> que;\n level[s]=0;\n que.emplace(s);\n while(!que.empty()){\n int v=que.front();que.pop();\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n que.emplace(e.to);\n }\n }\n }\n }\n\n T dfs(int v,int t,T f){\n if(v==t) return f;\n for(int &i=iter[v];i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n T d=dfs(e.to,t,min(f,e.cap));\n if(d==0) continue;\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n return 0;\n }\n\n T flow(int s,int t,T lim){\n T fl=0;\n while(1){\n bfs(s);\n if(level[t]<0||lim==0) break;\n fill(iter.begin(),iter.end(),0);\n\n while(1){\n T f=dfs(s,t,lim);\n if(f==0) break;\n fl+=f;\n lim-=f;\n }\n }\n return fl;\n }\n\n T flow(int s,int t){\n return flow(s,t,numeric_limits<T>::max()/2);\n }\n};\n\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n;\n cin>>n;\n vector<string> es(n);\n for(int i=0;i<n;i++) cin>>es[i];\n\n const int L=5;\n vector<string> vs;\n for(string &s:es){\n for(char &c:s){\n if(isdigit(c)) c=c-'0';\n else c=c-'a'+10;\n }\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n s[i]=(s[i]+1)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+1)%16;\n }\n }\n es=compress(es);\n vs=compress(vs);\n auto vd=dict(vs);\n\n auto restore=\n [](string s){\n for(char &c:s){\n if(c>=10) c='a'+(c-10);\n else c='0'+c;\n }\n return s;\n };\n\n int m=vd.size();\n Dinic<int, true> G(m+2);\n int S=m,T=m+1;\n\n vector<int> used(m,0);\n using P = pair<int, int>;\n map<string, P> mp;\n\n for(string &s:es){\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n\n s[i]=(s[i]+1)%16;\n int x=vd[s];\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n int y=vd[s];\n s[i]=(s[i]+1)%16;\n\n int sum=0;\n for(char c:vs[x]) sum+=c/2;\n if(sum&1) swap(x,y);\n\n mp[s]=P(x,G.G[x].size());\n G.add_edge(x,y,1);\n\n if(!used[x]){\n mp[vs[x]]=P(S,G.G[S].size());\n G.add_edge(S,x,1);\n used[x]=1;\n }\n\n if(!used[y]){\n mp[vs[y]]=P(y,G.G[y].size());\n G.add_edge(y,T,1);\n used[y]=1;\n }\n }\n }\n\n vector<string> as;\n for(auto s:es) as.emplace_back(s);\n for(auto s:vs) as.emplace_back(s);\n sort(as.begin(),as.end());\n\n cout<<G.flow(S,T)<<endl;\n for(auto s:as){\n int x=mp[s].first;\n auto &e=G.G[x][mp[s].second];\n if(e.cap==1) continue;\n\n int y=e.to;\n assert(G.G[y][e.rev].cap==1);\n G.G[y][e.rev].cap=0;\n\n int fwd=G.flow(x,y,1);\n if(fwd==1){\n e.cap=1;\n continue;\n }\n cout<<restore(s)<<endl;\n\n if(x!=S){\n int f=G.flow(x,S,1);\n assert(f);\n }\n if(T!=y){\n int f=G.flow(T,y,1);\n assert(f);\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1780, "memory_kb": 8456, "score_of_the_acc": -1.9128, "final_rank": 2 }, { "submission_id": "aoj_1599_3663826", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}\n };\n\n vector<vector<edge> > G;\n vector<int> level,iter;\n\n Dinic(){}\n Dinic(int n):G(n),level(n),iter(n){}\n\n void add_edge(int from,int to,T cap){\n G[from].emplace_back(to,cap,G[to].size());\n G[to].emplace_back(from,directed?0:cap,G[from].size()-1);\n }\n\n void bfs(int s){\n fill(level.begin(),level.end(),-1);\n queue<int> que;\n level[s]=0;\n que.emplace(s);\n while(!que.empty()){\n int v=que.front();que.pop();\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n que.emplace(e.to);\n }\n }\n }\n }\n\n T dfs(int v,int t,T f){\n if(v==t) return f;\n for(int &i=iter[v];i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n T d=dfs(e.to,t,min(f,e.cap));\n if(d==0) continue;\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n return 0;\n }\n\n T flow(int s,int t,T lim){\n T fl=0;\n while(1){\n bfs(s);\n if(level[t]<0||lim==0) break;\n fill(iter.begin(),iter.end(),0);\n\n while(1){\n T f=dfs(s,t,lim);\n if(f==0) break;\n fl+=f;\n lim-=f;\n }\n }\n return fl;\n }\n\n T flow(int s,int t){\n return flow(s,t,numeric_limits<T>::max()/2);\n }\n};\n\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n;\n cin>>n;\n vector<string> es(n);\n for(int i=0;i<n;i++) cin>>es[i];\n\n const int L=5;\n vector<string> vs;\n for(string &s:es){\n for(char &c:s){\n if(isdigit(c)) c=c-'0';\n else c=c-'a'+10;\n }\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n s[i]=(s[i]+1)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+1)%16;\n }\n }\n es=compress(es);\n vs=compress(vs);\n auto vd=dict(vs);\n\n auto restore=\n [](string s){\n for(char &c:s){\n if(c>=10) c='a'+(c-10);\n else c='0'+c;\n }\n return s;\n };\n\n int m=vd.size();\n Dinic<int, true> G(m+2);\n int S=m,T=m+1;\n\n vector<int> used(m,0);\n using P = pair<int, int>;\n map<string, P> mp;\n\n for(string &s:es){\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n\n s[i]=(s[i]+1)%16;\n int x=vd[s];\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n int y=vd[s];\n s[i]=(s[i]+1)%16;\n\n int sum=0;\n for(char c:vs[x]) sum+=c/2;\n if(sum&1) swap(x,y);\n\n mp[s]=P(x,G.G[x].size());\n G.add_edge(x,y,1);\n\n if(!used[x]){\n mp[vs[x]]=P(S,G.G[S].size());\n G.add_edge(S,x,1);\n used[x]=1;\n }\n\n if(!used[y]){\n mp[vs[y]]=P(y,G.G[y].size());\n G.add_edge(y,T,1);\n used[y]=1;\n }\n }\n }\n\n vector<string> as;\n for(auto s:es) as.emplace_back(s);\n for(auto s:vs) as.emplace_back(s);\n sort(as.begin(),as.end());\n\n G.flow(S,T);\n vector<string> ans;\n for(auto s:as){\n int x=mp[s].first;\n auto &e=G.G[x][mp[s].second];\n if(e.cap==1) continue;\n\n int y=e.to;\n assert(G.G[y][e.rev].cap==1);\n G.G[y][e.rev].cap=0;\n\n int fwd=G.flow(x,y);\n if(fwd==1){\n e.cap=1;\n continue;\n }\n\n ans.emplace_back(s);\n if(x!=S){\n int f=G.flow(x,S,1);\n assert(f);\n }\n if(T!=y){\n int f=G.flow(T,y,1);\n assert(f);\n }\n }\n\n cout<<ans.size()<<endl;\n for(auto s:ans) cout<<restore(s)<<\"\\n\";\n cout<<flush;\n return 0;\n}", "accuracy": 0.15625, "time_ms": 1280, "memory_kb": 8120, "score_of_the_acc": -1.2384, "final_rank": 5 }, { "submission_id": "aoj_1599_3663667", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}\n };\n\n vector<vector<edge> > G;\n vector<int> level,iter;\n\n Dinic(){}\n Dinic(int n):G(n),level(n),iter(n){}\n\n void add_edge(int from,int to,T cap){\n G[from].emplace_back(to,cap,G[to].size());\n G[to].emplace_back(from,directed?0:cap,G[from].size()-1);\n }\n\n void bfs(int s){\n fill(level.begin(),level.end(),-1);\n queue<int> que;\n level[s]=0;\n que.emplace(s);\n while(!que.empty()){\n int v=que.front();que.pop();\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n que.emplace(e.to);\n }\n }\n }\n }\n\n T dfs(int v,int t,T f){\n if(v==t) return f;\n for(int &i=iter[v];i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n T d=dfs(e.to,t,min(f,e.cap));\n if(d==0) continue;\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n return 0;\n }\n\n T flow(int s,int t,T lim){\n T fl=0;\n while(1){\n bfs(s);\n if(level[t]<0||lim==0) break;\n fill(iter.begin(),iter.end(),0);\n\n while(1){\n T f=dfs(s,t,lim);\n if(f==0) break;\n fl+=f;\n lim-=f;\n }\n }\n return fl;\n }\n\n T flow(int s,int t){\n return flow(s,t,numeric_limits<T>::max()/2);\n }\n};\n\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n;\n cin>>n;\n vector<string> es(n);\n for(int i=0;i<n;i++) cin>>es[i];\n\n const int L=5;\n vector<string> vs;\n for(string &s:es){\n for(char &c:s){\n if(isdigit(c)) c=c-'0';\n else c=c-'a'+10;\n }\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n s[i]=(s[i]+1)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+1)%16;\n }\n }\n es=compress(es);\n vs=compress(vs);\n auto vd=dict(vs);\n\n auto restore=\n [](string s){\n for(char &c:s){\n if(c>=10) c='a'+(c-10);\n else c='0'+c;\n }\n return s;\n };\n\n int m=vd.size();\n Dinic<int, true> G(m+2);\n int S=m,T=m+1;\n\n vector<int> used(m,0);\n using P = pair<int, int>;\n map<string, P> mp;\n\n for(string &s:es){\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n\n s[i]=(s[i]+1)%16;\n int x=vd[s];\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n int y=vd[s];\n s[i]=(s[i]+1)%16;\n\n if((s[i]/2)&1) swap(x,y);\n mp[s]=P(x,G.G[x].size());\n G.add_edge(x,y,1);\n\n if(!used[x]){\n mp[vs[x]]=P(S,G.G[S].size());\n G.add_edge(S,x,1);\n used[x]=1;\n }\n\n if(!used[y]){\n mp[vs[y]]=P(y,G.G[y].size());\n G.add_edge(y,T,1);\n used[y]=1;\n }\n }\n }\n\n vector<string> as;\n for(auto s:es) as.emplace_back(s);\n for(auto s:vs) as.emplace_back(s);\n sort(as.begin(),as.end());\n\n G.flow(S,T);\n vector<string> ans;\n for(auto s:as){\n int x=mp[s].first;\n auto &e=G.G[x][mp[s].second];\n if(e.cap==1) continue;\n int y=e.to;\n assert(G.G[y][e.rev].cap==1);\n G.G[y][e.rev].cap=0;\n\n if(x!=S){\n int f=G.flow(x,S,1);\n assert(f);\n }\n if(T!=y){\n int f=G.flow(T,y,1);\n assert(f);\n }\n\n int tmp=G.flow(S,T,1);\n if(tmp==1){\n assert(e.cap==0);\n assert(G.G[y][e.rev].cap==0);\n e.cap=1;\n continue;\n }\n ans.emplace_back(s);\n }\n\n cout<<ans.size()<<endl;\n for(auto s:ans) cout<<restore(s)<<\"\\n\";\n cout<<flush;\n return 0;\n}", "accuracy": 0.09375, "time_ms": 790, "memory_kb": 6788, "score_of_the_acc": 0, "final_rank": 6 }, { "submission_id": "aoj_1599_3663646", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}\n };\n\n vector<vector<edge> > G;\n vector<int> level,iter;\n\n Dinic(){}\n Dinic(int n):G(n),level(n),iter(n){}\n\n void add_edge(int from,int to,T cap){\n //cout<<from<<\" \"<<to<<\":\"<<cap<<endl;\n G[from].emplace_back(to,cap,G[to].size());\n G[to].emplace_back(from,directed?0:cap,G[from].size()-1);\n }\n\n void bfs(int s){\n fill(level.begin(),level.end(),-1);\n queue<int> que;\n level[s]=0;\n que.emplace(s);\n while(!que.empty()){\n int v=que.front();que.pop();\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n que.emplace(e.to);\n }\n }\n }\n }\n\n T dfs(int v,int t,T f){\n if(v==t) return f;\n for(int &i=iter[v];i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n T d=dfs(e.to,t,min(f,e.cap));\n if(d==0) continue;\n e.cap-=d;\n G[e.to][e.rev].cap+=d;\n return d;\n }\n }\n return 0;\n }\n\n T flow(int s,int t,T lim){\n T fl=0;\n while(1){\n bfs(s);\n if(level[t]<0||lim==0) break;\n fill(iter.begin(),iter.end(),0);\n\n while(1){\n T f=dfs(s,t,lim);\n if(f==0) break;\n fl+=f;\n lim-=f;\n }\n }\n return fl;\n }\n\n T flow(int s,int t){\n return flow(s,t,numeric_limits<T>::max()/2);\n }\n};\n\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\n//INSERT ABOVE HERE\nsigned main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n\n int n;\n cin>>n;\n vector<string> es(n);\n for(int i=0;i<n;i++) cin>>es[i];\n\n const int L=5;\n vector<string> vs;\n for(string &s:es){\n for(char &c:s){\n if(isdigit(c)) c=c-'0';\n else c=c-'a'+10;\n }\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n s[i]=(s[i]+1)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n vs.emplace_back(s);\n s[i]=(s[i]+1)%16;\n }\n }\n es=compress(es);\n vs=compress(vs);\n auto vd=dict(vs);\n\n auto restore=\n [](string s){\n for(char &c:s){\n if(c>=10) c='a'+(c-10);\n else c='0'+c;\n }\n return s;\n };\n\n int m=vd.size();\n Dinic<int, true> G(m+2);\n int S=m,T=m+1;\n\n vector<int> used(m,0);\n using P = pair<int, int>;\n map<string, P> mp;\n\n for(string &s:es){\n for(int i=0;i<L;i++){\n if(s[i]&1) continue;\n\n s[i]=(s[i]+1)%16;\n int x=vd[s];\n s[i]=(s[i]+15)%16;\n\n s[i]=(s[i]+15)%16;\n int y=vd[s];\n s[i]=(s[i]+1)%16;\n\n if((s[i]/2)&1) swap(x,y);\n mp[s]=P(x,G.G[x].size());\n G.add_edge(x,y,1);\n\n if(!used[x]){\n mp[vs[x]]=P(S,G.G[S].size());\n G.add_edge(S,x,1);\n used[x]=1;\n }\n\n if(!used[y]){\n mp[vs[y]]=P(y,G.G[y].size());\n G.add_edge(y,T,1);\n used[y]=1;\n }\n }\n }\n\n vector<string> as;\n for(auto s:es) as.emplace_back(s);\n for(auto s:vs) as.emplace_back(s);\n sort(as.begin(),as.end());\n\n /*\n cout<<m<<\":\"<<endl;\n for(auto s:as){\n int x=mp[s].first;\n auto &e=G.G[x][mp[s].second];\n int y=e.to;\n cout<<restore(s)<<\" \"<<x<<\" \"<<y<<endl;\n }\n */\n\n G.flow(S,T);\n vector<string> ans;\n for(auto s:as){\n int x=mp[s].first;\n auto &e=G.G[x][mp[s].second];\n if(e.cap==1) continue;\n int y=e.to;\n assert(G.G[y][e.rev].cap==1);\n G.G[y][e.rev].cap=0;\n\n if(x!=S) G.flow(x,S,1);\n if(T!=y) G.flow(T,y,1);\n\n int tmp=G.flow(S,T,1);\n if(tmp==1){\n assert(e.cap==0);\n assert(G.G[y][e.rev].cap==0);\n e.cap=1;\n continue;\n }\n ans.emplace_back(s);\n }\n\n cout<<ans.size()<<endl;\n for(auto s:ans) cout<<restore(s)<<\"\\n\";\n cout<<flush;\n return 0;\n}", "accuracy": 0.09375, "time_ms": 800, "memory_kb": 6824, "score_of_the_acc": -0.0304, "final_rank": 7 } ]
aoj_1598_cpp
Problem L: RedBlue Story 天空都市AIZUのUZIA高校では、競技プログラミングの部活動がとても盛んである。 この部活には、 n 人のRed Coderと n 人のBlue Coderが所属している。 ある日の部活の時間に、Red CoderとBlue Coderでペアを組み、この部活から n 組、KCPというコンテストに参加することとなった。この高校では、ペアを組む学生同士は握手するという習わしがあるので、部員たちは今すぐ自分のパートナーを見つけることにした。 部員たちは全速力で走るため、まっすぐにしか進めない。また、部員たちは、それぞれの部員が移動する距離の総和をできるだけ小さくしたいと思っている。 なお、部室には2つの円形のテーブルが置いてある。 Problem 二次元平面上に2つの円、 n 個の赤色の点、 n 個の青色の点がある。 2つの円の中心座標はそれぞれ( x 1 , y 1 ), ( x 2 , y 2 )であり、半径はそれぞれ r 1 , r 2 である。 赤い点 i は、座標( rx i , ry i )にあり、青い点 j は、座標( bx j , by j )にある。 あなたは、以下の操作を n 回繰り返す必要がある。 まだ選ばれていない点の中から、赤色の点と青色の点を1つずつ選んで、2点の共通の目的地を設定し、その目的地に向かって2点をそれぞれまっすぐ移動させる。 目的地は二次元平面上であれば、どこに設定しても構わない。ただし、選んだ2点は移動の際に円の内部を通過することはできないので、そのような移動が発生する目的地を設定することはできない。 n 回の操作をしたときの移動距離の総和を最小化せよ。 n 回の操作を行えない場合は、代わりに"Impossible" (""は除く) と出力せよ。 Input 入力は以下の形式で与えられる。 n x 1 y 1 r 1 x 2 y 2 r 2 rx 1 ry 1 rx 2 ry 2 ... rx n ry n bx 1 by 1 bx 2 by 2 ... bx n by n 入力は全て整数で与えられる。 1行目に n が与えられる。 2行目に x 1 , y 1 , r 1 が空白区切りで与えられる。 3行目に x 2 , y 2 , r 2 が空白区切りで与えられる。 4から3+ n 行に赤い点の座標( rx i , ry i )が空白区切りで与えられる。 4+ n から3+2× n 行に青い点の座標( bx j , by j )が空白区切りで与えられる。 Constraints 入力は以下の条件を満たす。 1 ≤ n ≤ 100 -1000 ≤ x i , y i ≤ 1000 1 ≤ r i ≤ 50 -1000 ≤ rx i , ry i , bx i , by i ≤ 1000 同じ座標に複数の点が存在していることはない 任意の円の半径を絶対値10 -9 以内で変化させても、高々絶対値10 -3 しか変化しない 任意の円の半径を絶対値10 -9 以内で変化させても、"Impossible"なケースは"Impossible"のままである 解は10000を超えない どの点も、円から10 -3 以上離れていて、点が円周上に存在したり、点が円に内包されることはない 2つの円は共通面積を持たず、10 -3 以上離れていることが保証される Output n 回の操作をしたときの移動距離の総和の最小値を1行に出力せよ。なお、出力はジャッジ解の出力との絶対誤差が10 -2 以内であれば許容される。 n 回の操作を行えない場合は、代わりに"Impossible" (""は除く) と出力せよ。 Sample Input 1 2 3 3 2 8 3 2 0 3 3 7 8 0 8 7 Sample Output 1 13.8190642862 Sample Input 2 2 3 3 2 8 3 2 3 0 3 7 8 0 8 7 Sample Output 2 10.0000000000 Sample Input 3 2 3 3 2 8 3 2 0 0 0 5 11 0 11 5 Sample Output 3 22.0000000000 Sample Input 4 1 10 10 10 31 10 10 15 19 26 1 Sample Output 4 Impossible 点同士をつなぐことはできません。
[ { "submission_id": "aoj_1598_8305781", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long LL;\ntypedef long double LD;\nconst int N = 205;\nconst int M = 100005;\nconst LD INF = 1e18;\nconst LD eps = 1e-10;\n\ninline int sgn(LD x) {\n return x > eps? 1 : x < -eps? -1 : 0;\n}\n\nstruct Point {\n LD x, y;\n Point(LD _x = 0, LD _y = 0): x(_x), y(_y) {}\n bool operator == (const Point &rhs) const {\n return sgn(x - rhs.x) == 0 && sgn(y - rhs.y) == 0;\n }\n Point operator + (const Point &rhs) const {\n return Point(x + rhs.x, y + rhs.y);\n }\n Point operator - (const Point &rhs) const {\n return Point(x - rhs.x, y - rhs.y);\n }\n Point operator * (const LD &rhs) const {\n return Point(x * rhs, y * rhs);\n }\n Point operator / (const LD &rhs) const {\n return Point(x / rhs, y / rhs);\n }\n LD operator * (const Point &rhs) const {\n return x * rhs.x + y * rhs.y;\n }\n LD operator ^ (const Point &rhs) const {\n return x * rhs.y - y * rhs.x;\n }\n LD len2() {\n return x * x + y * y;\n }\n LD len() {\n return sqrtl(len2());\n }\n Point unit() {\n return (*this) / len();\n }\n Point rotate(LD ang) {\n return Point(x * cos(ang) - y * sin(ang), x * sin(ang) + y * cos(ang));\n }\n} a[N], b[N];\n\nstruct Circle {\n Point c;\n LD r;\n Circle(Point _c = (0, 0), LD _r = 0): c(_c), r(_r) {}\n} C[2];\nvector<Point> ta[N], tb[N];\n\nPoint LineInter(Point u1, Point v1, Point u2, Point v2) {\n LD a1 = (v2 - u2) ^ (u1 - u2), a2 = (v2 - u2) ^ (v1 - u2);\n return (u1 * a2 - v1 * a1) / (a2 - a1);\n}\n\nPoint PointProjLine(Point p, Point u, Point v) {\n Point dir = (v - u).unit();\n return u + dir * (dir * (p - u));\n}\n\nLD PointLineDist(Point p, Point u, Point v) {\n Point dir = (v - u).unit();\n return fabsl(dir ^ (p - u));\n}\n\nbool Onseg(Point p, Point u, Point v) {\n if (sgn((p - u) ^ (v - u)) == 0 && sgn((p - u) * (p - v)) <= 0) return 1;\n return 0;\n}\n\nLD PointSegDist(Point p, Point u, Point v) {\n Point o = PointProjLine(p, u, v);\n if (Onseg(o, u, v)) return (p - o).len();\n return min((p - u).len(), (p - v).len());\n}\n\nbool SegCircleIntersect(Point u, Point v, Circle c) {\n return sgn(PointSegDist(c.c, u, v) - c.r) < 0;\n}\n\nvector<Point> Tangents(Point p) {\n vector<Point> ret;\n for (int i = 0; i < 2; ++i) {\n LD delta = acosl(C[i].r / (p - C[i].c).len());\n Point m = (p - C[i].c).unit() * C[i].r;\n ret.push_back(C[i].c + m.rotate(delta));\n ret.push_back(C[i].c + m.rotate(-delta));\n }\n return ret;\n}\n\nLD Meet(int i, int j) {\n if (!SegCircleIntersect(a[i], b[j], C[0]) && \n !SegCircleIntersect(a[i], b[j], C[1])) {\n return (a[i] - b[j]).len(); //go directly\n }\n LD ret = INF;\n for (auto u : ta[i]) {\n for (auto v : tb[j]) {\n if (sgn((u - a[i]) ^ (v - b[j])) == 0) continue; //parallel\n Point w = LineInter(a[i], u, b[j], v);\n if (SegCircleIntersect(a[i], w, C[0])) continue;\n if (SegCircleIntersect(a[i], w, C[1])) continue;\n if (SegCircleIntersect(b[j], w, C[0])) continue;\n if (SegCircleIntersect(b[j], w, C[1])) continue;\n ret = min(ret, (w - a[i]).len() + (w - b[j]).len());\n }\n }\n return ret;\n}\n\nstruct CostFlow {\n int n, S, T, maxflow, inq[N], pre[N];\n LD mincost, dis[N];\n \n struct Edge {\n int v, w, nxt;\n LD c;\n } e[M << 1];\n int first[N], eCnt;\n\n inline void AddEdge(int u, int v, int w, LD c) {\n e[++eCnt].v = v;\n e[eCnt].w = w;\n e[eCnt].c = c;\n e[eCnt].nxt = first[u];\n first[u] = eCnt;\n }\n\n inline void Add(int u, int v, int w, LD c) {\n AddEdge(u, v, w, c);\n AddEdge(v, u, 0, -c);\n }\n\n void Init(int _n) {\n n = _n;\n mincost = maxflow = 0;\n for (int i = 1; i <= n; ++i) {\n first[i] = 0;\n }\n eCnt = 1;\n }\n\n bool SPFA() {\n for (int i = 1; i <= n; ++i) {\n dis[i] = INF;\n pre[i] = 0;\n }\n dis[S] = 0;\n queue<int> q;\n q.push(S);\n while (!q.empty()) {\n int u = q.front();\n q.pop();\n inq[u] = 0;\n for (int i = first[u]; i; i = e[i].nxt) {\n int v = e[i].v;\n if (e[i].w > 0 && dis[v] > dis[u] + e[i].c + eps) {\n dis[v] = dis[u] + e[i].c;\n pre[v] = i;\n if (!inq[v]) {\n q.push(v);\n inq[v] = 1;\n }\n }\n }\n }\n return (dis[T] < INF / 2);\n }\n\n void Augment() {\n for (int i = pre[T]; i; i = pre[e[i ^ 1].v]) {\n e[i].w -= 1;\n e[i ^ 1].w += 1;\n }\n maxflow += 1;\n mincost += dis[T];\n }\n\n void MCMF(int s, int t) {\n S = s; T = t;\n while (SPFA()) {\n Augment();\n }\n }\n} Flow;\n\nvoid solve() {\n int n;\n scanf(\"%d\", &n);\n for (int i = 0; i < 2; ++i) {\n scanf(\"%Lf %Lf %Lf\", &C[i].c.x, &C[i].c.y, &C[i].r);\n }\n for (int i = 1; i <= n; ++i) {\n scanf(\"%Lf %Lf\", &a[i].x, &a[i].y);\n ta[i] = Tangents(a[i]);\n }\n for (int i = 1; i <= n; ++i) {\n scanf(\"%Lf %Lf\", &b[i].x, &b[i].y);\n tb[i] = Tangents(b[i]);\n }\n int S = n * 2 + 1, T = S + 1;\n Flow.Init(T);\n for (int i = 1; i <= n; ++i) {\n Flow.Add(S, i, 1, 0);\n Flow.Add(n + i, T, 1, 0);\n }\n for (int i = 1; i <= n; ++i) {\n for (int j = 1; j <= n; ++j) {\n LD dis = Meet(i, j);\n if (dis < INF / 2) {\n//printf(\"%d %d %.10Lf\\n\", i, j, dis);\n Flow.Add(i, n + j, 1, dis);\n }\n }\n }\n Flow.MCMF(S, T);\n if (Flow.maxflow != n) {\n puts(\"Impossible\");\n return;\n }\n printf(\"%.10Lf\\n\", Flow.mincost);\n}\n\nint main() {\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 4692, "score_of_the_acc": -0.9437, "final_rank": 6 }, { "submission_id": "aoj_1598_6017039", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double EPS = 1e-5,INF = 1e12;\ntypedef complex<double> P;\ntypedef pair<P,P> L;\nstruct C{P p;double r;};\nnamespace std {\n bool operator < (const P& a, const P& b) {\n return real(a)!=real(b)?real(a)<real(b):imag(a)<imag(b);\n }\n}\ndouble cross(const P& a, const P& b) {return imag(conj(a)*b);}\ndouble dot(const P& a, const P& b) {return real(conj(a)*b);}\nP proj(P p, L l){\n return l.first + dot(p - l.first, l.second - l.first) / norm(l.second - l.first) * (l.second - l.first);\n}\ndouble D(P a, P b) {\n return sqrt((a.real()-b.real())*(a.real()-b.real())+(a.imag()-b.imag())*(a.imag()-b.imag()));\n}\nbool intersectSP(const L &s, const P &p) {return abs(s.first-p)+abs(s.second-p)-abs(s.second-s.first) < EPS;}\nbool intersectLL(const L &l, const L &m) {\n return abs(cross(l.second-l.first, m.second-m.first)) > EPS ||\n abs(cross(l.second-l.first, m.first-l.first)) < EPS;\n}\nP crosspoint(const L &l, const L &m) {\n double A = cross(l.second - l.first, m.second - m.first);\n double B = cross(l.second - l.first, l.second - m.first);\n if (fabs(A) < EPS && fabs(B) < EPS) return m.first;\n if (fabs(A) < EPS) assert(false);\n return m.first + B / A * (m.second - m.first);\n}\nvector<P> getIntersectCS(C c, L s){\n vector<P> res;\n P h = proj(c.p, s);\n double d = abs(c.p - h);\n if(d > c.r + EPS);\n else if(d > c.r - EPS){\n if(intersectSP(s,h)) res.push_back(h);\n }\n else {\n P v = s.second - s.first;\n v = (sqrt(c.r*c.r - d*d) / abs(v)) * v;\n if(intersectSP(s,h+v)) res.push_back(h+v);\n if(intersectSP(s,h-v)) res.push_back(h-v);\n }\n return res;\n}\ndouble toRad(double agl) {return agl*M_PI/180.0;}\nP rotate(P a, double r){return P(a.real()*cos(r)-a.imag()*sin(r),a.real()*sin(r)+a.imag()*cos(r));}\nvector<P> tangent(C c, P q){\n vector<P> res;\n double a = abs(q - c.p);\n if(a < c.r - EPS);\n else if(a < c.r + EPS) res.push_back(rotate(c.p-q,toRad(90))+q);\n else {\n double b = sqrt(a*a-c.r*c.r);\n double psi = arg(q - c.p);\n double phi = M_PI - acos(b/a);\n res.push_back(q + b * P(cos(psi+phi), sin(psi+phi)));\n res.push_back(q + b * P(cos(psi-phi), sin(psi-phi)));\n }\n return res;\n}\nconst int MAX_V= 1002;\nstruct edge{\n int to,cap,rev;\n double cost;\n edge(int to,int cap,double cost,int rev) :to(to),cap(cap),cost(cost),rev(rev) {}\n};\nvector<edge> G[MAX_V];\ndouble dist[MAX_V];\nint prevv[MAX_V],preve[MAX_V];\nvoid add_edge(int from,int to,int cap,double cost){\n G[from].push_back(edge(to,cap,cost,G[to].size()));\n G[to].push_back(edge(from,0,-cost,G[from].size()-1));\n}\ndouble min_cost_flow(int s,int t,int f){\n double res=0;\n while(f>0){\n fill(dist,dist+MAX_V,INF);dist[s]=0;\n bool update=true;\n while(update){\n update=false;\n for(int v=0;v<MAX_V;v++){\n if(dist[v]==INF) continue;\n for(int i=0;i<(int)G[v].size();i++){\n edge &e=G[v][i];\n if(e.cap>0 && dist[e.to]>dist[v]+e.cost+EPS){\n dist[e.to]=dist[v]+e.cost;\n prevv[e.to]=v; preve[e.to]=i; update=true;\n }\n }\n }\n }\n if(dist[t]==INF)return -1;\n int d=f;\n for(int v=t;v!=s;v=prevv[v]){\n d=min(d,G[prevv[v]][preve[v]].cap);\n }\n f-=d;res+=dist[t]*d;\n for(int v=t;v!=s;v=prevv[v]){\n edge &e=G[prevv[v]][preve[v]];\n e.cap-=d;G[v][e.rev].cap+=d;\n }\n }\n return res;\n}\n\nint main() {\n int n;\n cin >> n;\n C c[2];\n for(int i=0,x,y,z; i<2; i++) {\n cin >> x >> y >> z;\n c[i]=(C){P(x,y),z};\n }\n P a[n],b[n];\n vector<L> v[n],v2[n];\n for(int i=0,x,y; i<n; i++) {\n cin >> x >> y;\n a[i]=P(x,y);\n for(int j=0; j<2; j++) {\n vector<P> g=tangent(c[j],a[i]);\n for(int k=0; k<g.size(); k++) v[i].push_back(L(a[i],g[k]));\n }\n }\n for(int i=0,x,y; i<n; i++) {\n cin >> x >> y;\n b[i]=P(x,y);\n for(int j=0; j<2; j++) {\n vector<P> g=tangent(c[j],b[i]);\n for(int k=0; k<g.size(); k++) v2[i].push_back(L(b[i],g[k]));\n }\n }\n double d[n][n];\n for(int i=0; i<n; i++) {\n for(int j=0; j<n; j++) {\n d[i][j]=INF;\n for(int k=0; k<v[i].size(); k++) {\n for(int l=0; l<v2[j].size(); l++) {\n if(!intersectLL(v[i][k],v2[j][l])) continue;\n P p=crosspoint(v[i][k],v2[j][l]);\n bool f=1;\n for(int x=0; x<2; x++) {\n if(D(p,c[x].p)<c[x].r) f=0;\n if(getIntersectCS(c[x],L(a[i],p)).size()>=2) f=0;\n if(getIntersectCS(c[x],L(b[j],p)).size()>=2) f=0;\n }\n if(f) d[i][j]=min(d[i][j],D(a[i],p)+D(b[j],p));\n }\n }\n bool f=1;\n for(int x=0; x<2; x++) {\n if(getIntersectCS(c[x],L(a[i],b[j])).size()>=2) f=0;\n }\n if(f) d[i][j]=min(d[i][j],D(a[i],b[j]));\n if(d[i][j]!=INF) add_edge(i,n+j,1,d[i][j]);\n }\n add_edge(1000,i,1,0);\n add_edge(i+n,1001,1,0);\n }\n double ans=min_cost_flow(1000,1001,n);\n if(ans<0) printf(\"Impossible\\n\");\n else printf(\"%.10f\\n\",ans);\n return 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 4428, "score_of_the_acc": -0.6995, "final_rank": 5 }, { "submission_id": "aoj_1598_3922498", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <tuple>\n#include <bitset>\n#include <memory>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <iomanip>\n#include <numeric>\n#include <climits>\n#include <cfloat>\n\nconstexpr double EPS = 0.000000001;\nstruct Coordinate;\nstruct Vector;\nstruct Segment;\nstruct Angle;\nstruct Line;\nstruct Circle;\nstruct Coordinate {\n\tdouble x, y;\n\tdouble distance(const Coordinate& that) const;\n\tVector operator-(const Coordinate& from) const;\n};\nstruct Vector {\n\tdouble x, y;\n\tVector rotate(const Angle& angle) const;\n\tdouble dot(const Vector& that) const;\n\tdouble cross(const Vector& that) const;\n\tdouble length() const;\n};\nVector operator*(const double& scala, const Vector& vec);\nVector operator*(const Vector& vec, const double scala);\nCoordinate operator+(const Coordinate& coord, const Vector& diff);\nCoordinate operator+(const Vector& diff, const Coordinate& coord);\nstruct Segment {\n\tCoordinate end_a, end_b;\n\tbool has_intersection(const Circle& that) const;\n\tdouble distance(const Coordinate& that) const;\n};\nstruct Angle {\n\tdouble sin, cos;\n\tstatic Angle from_sin(const double sin);\n\tAngle operator+(const Angle& that) const;\n\tAngle operator-() const;\n};\nstruct Line {\nprivate:\n\tconst Coordinate from, to;\n\tVector vec() const { return to - from; }\n\tdouble a() const { return to.y - from.y; }\n\tdouble b() const { return from.x - to.x; }\n\tdouble c() const { return - a() * from.x - from.y * b(); }\npublic:\n\tLine(const Coordinate& a, const Coordinate& b) : to{ a }, from{ b } {};\n\tbool is_cross(const Line& that) const;\n\tCoordinate cross_point(const Line& that) const;\n};\nstruct Circle {\n\tCoordinate center;\n\tdouble radius;\n\tstd::vector<Line> tangents(const Coordinate& that) const;\n};\n\ndouble Coordinate::distance(const Coordinate& that) const\n{\n\treturn (*this - that).length();\n}\n\nVector Coordinate::operator-(const Coordinate& from) const\n{\n\treturn Vector{ x - from.x, y - from.y };\n}\n\nVector Vector::rotate(const Angle& angle) const\n{\n\tauto len = length();\n\tauto a = Angle{ y / len, x / len } + angle;\n\treturn Vector{ a.cos * len, a.sin* len };\n}\n\ndouble Vector::dot(const Vector& that) const\n{\n\treturn x * that.x + y * that.y;\n}\n\ndouble Vector::cross(const Vector& that) const\n{\n\treturn x * that.y - y * that.x;\n}\n\ndouble Vector::length() const\n{\n\treturn std::sqrt(x * x + y * y);\n}\n\nVector operator*(const double& scala, const Vector& vec)\n{\n\treturn Vector{ vec.x * scala, vec.y * scala };\n}\n\nVector operator*(const Vector& vec, const double scala)\n{\n\treturn Vector{ vec.x * scala, vec.y * scala };\n}\n\nCoordinate operator+(const Coordinate& coord, const Vector& diff)\n{\n\treturn Coordinate{ coord.x + diff.x, coord.y + diff.y };\n}\n\nCoordinate operator+(const Vector& diff, const Coordinate& coord)\n{\n\treturn Coordinate{ diff.x + coord.x, diff.y + coord.y };\n}\n\nstd::vector<Line> Circle::tangents(const Coordinate& that) const\n{\n\tconst auto mid = center - that;\n\tconst auto angle = Angle::from_sin(radius / center.distance(that));\n\treturn { Line(that, mid.rotate(angle) + that), Line(that, mid.rotate(-angle) + that) };\n}\n\nbool Line::is_cross(const Line& that) const\n{\n\treturn std::abs(vec().cross(that.vec())) > 0;\n}\n\nCoordinate Line::cross_point(const Line& that) const\n{\n\tconst auto d = a() * that.b() - that.a() * b();\n\treturn Coordinate{ (b() * that.c() - that.b() * c()) / d, (that.a() * c() - a() * that.c()) / d };\n}\n\nAngle Angle::from_sin(const double sin)\n{\n\treturn Angle{ sin, std::sqrt(1 - sin * sin) };\n}\n\nAngle Angle::operator+(const Angle& that) const\n{\n\treturn Angle{ sin * that.cos + cos * that.sin, cos * that.cos - sin * that.sin };\n}\n\nAngle Angle::operator-() const\n{\n\treturn Angle{ -sin, cos };\n}\n\nbool Segment::has_intersection(const Circle& that) const\n{\n\treturn distance(that.center) < that.radius - EPS;\n}\n\ndouble Segment::distance(const Coordinate& that) const\n{\n\tconst auto vec = end_a - end_b;\n\tif (vec.dot(that - end_b) >= 0 && vec.dot(end_a - that) >= 0) {\n\t\treturn std::abs(vec.y * that.x - vec.x * that.y + end_a.x * end_b.y - end_a.y * end_b.x) / std::sqrt(vec.y * vec.y + vec.x * vec.x);\n\t}\n\telse {\n\t\treturn std::min(end_a.distance(that), end_b.distance(that));\n\t}\n}\n\nstruct Edge {\n\tint to;\n\tdouble cost;\n\tEdge* pair{ nullptr };\n\tbool has_flow;\n\tEdge(int _to, double _cost, bool _has_flow) :to{ _to }, cost{ _cost }, has_flow{ _has_flow }{};\n};\ntemplate<typename K, typename V>\nstruct KeyWithValue {\n\tK key;\n\tV value;\n\tKeyWithValue(const K& k, const V& v) :key{ k }, value{ v }{};\n};\ntemplate<typename K, typename V>\nbool operator>(const KeyWithValue<K, V>& a, const KeyWithValue<K, V>& b) {\n\treturn a.key > b.key;\n}\nvoid set_edge(std::vector<std::vector<Edge>>& node, const int from, const int to, double cost) {\n\tnode[from].emplace_back(to, cost, true);\n\tnode[to].emplace_back(from, -cost, false);\n\tnode[from].back().pair = &node[to].back();\n\tnode[to].back().pair = &node[from].back();\n}\ndouble min_cost_matching(std::vector<std::vector<Edge>> &nodes, const int flow, const int source, const int sink) {\n\tstd::vector<double> min_cost(nodes.size());\n\tstd::vector<double> potential(nodes.size(), 0);\n\tstd::vector<Edge*> prev(nodes.size(), nullptr);\n\tstd::priority_queue<KeyWithValue<double, int>, std::vector<KeyWithValue<double, int>>, std::greater<KeyWithValue<double, int>>> queue;\n\tdouble result = 0;\n\tfor (auto _i = 0; _i < flow; ++_i) {\n\t\tstd::fill(min_cost.begin(), min_cost.end(), DBL_MAX);\n\t\tmin_cost[source] = 0;\n\t\tqueue.emplace(0.0, source);\n\t\twhile (!queue.empty()) {\n\t\t\tauto top = queue.top(); queue.pop();\n\t\t\tif (top.key == min_cost[top.value]) {\n\t\t\t\tfor (auto& edge : nodes[top.value]) if (edge.has_flow) {\n\t\t\t\t\tif (min_cost[edge.to] > potential[top.value] + edge.cost - potential[edge.to] + top.key + EPS) {\n\t\t\t\t\t\tmin_cost[edge.to] = potential[top.value] + edge.cost - potential[edge.to] + top.key;\n\t\t\t\t\t\tprev[edge.to] = &edge;\n\t\t\t\t\t\tqueue.emplace(min_cost[edge.to], edge.to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (min_cost[sink] == DBL_MAX) return -1;\n\t\tfor (auto i = 0; i < nodes.size(); ++i) potential[i] += min_cost[i];\n\t\tresult += potential[sink];\n\t\tauto last = sink;\n\t\twhile (last != source) {\n\t\t\tauto prev_edge = prev[last];\n\t\t\tprev_edge->has_flow = false;\n\t\t\tprev_edge->pair->has_flow = true;\n\t\t\tlast = prev_edge->pair->to;\n\t\t}\n\t}\n\treturn result;\n}\ndouble cal_min_cost(const std::vector<Coordinate>& red, const std::vector<Coordinate>& blue, const std::vector<Circle>& circles) {\n\tstd::vector<std::vector<std::vector<Line>>> red_tangents(red.size()), blue_tangents(blue.size());\n\tfor (auto i = 0; i < red.size(); ++i) {\n\t\tfor (const auto &circle: circles) {\n\t\t\tred_tangents[i].push_back(circle.tangents(red[i]));\n\t\t\tblue_tangents[i].push_back(circle.tangents(blue[i]));\n\t\t}\n\t}\n\tconst int source = red.size() + blue.size();\n\tconst int sink = source + 1;\n\tstd::vector<std::vector<Edge>> node(sink + 1); for (auto& n : node) n.reserve(red.size() + 1);\n\tfor (auto r = 0; r < red.size(); ++r) for (auto b = 0; b < blue.size(); ++b){\n\t\tconst auto red_pos = red[r];\n\t\tconst auto blue_pos = blue[b];\n\t\tdouble min_cost = DBL_MAX;\n\t\tconst auto direct = Segment{ red_pos, blue_pos };\n\t\tbool is_blocked = false;\n\t\tfor (const auto& c : circles) if (!is_blocked) {\n\t\t\tis_blocked = direct.has_intersection(c);\n\t\t}\n\t\tif (is_blocked) {\n\t\t\tfor (auto i = 0; i < circles.size(); ++i) for (auto j = 0; j < circles.size(); ++j) {\n\t\t\t\tfor (const auto& red_tangent : red_tangents[r][i]) for (const auto& blue_tangent : blue_tangents[b][j]) if (red_tangent.is_cross(blue_tangent)) {\n\t\t\t\t\tconst auto cross_point = red_tangent.cross_point(blue_tangent);\n\t\t\t\t\tconst auto red_to_cp = Segment{ cross_point, red_pos };\n\t\t\t\t\tconst auto blue_to_cp = Segment{ cross_point, blue_pos };\n\t\t\t\t\tbool is_blocked = false;\n\t\t\t\t\tfor (auto c = 0; c < circles.size() && !is_blocked; ++c) {\n\t\t\t\t\t\t/*\n\t\t\t\t\t\tif (i == c && red_to_cp.has_intersection(circles[c])) {\n\t\t\t\t\t\t\tstd::cerr << \"i: \" << i << '\\n';\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (j == c && blue_to_cp.has_intersection(circles[c])) {\n\t\t\t\t\t\t\tstd::cerr << \"i: \" << i << '\\n';\n\t\t\t\t\t\t}\n\t\t\t\t\t\t*/\n\t\t\t\t\t\tis_blocked = ((i != c) && red_to_cp.has_intersection(circles[c])) || ((j != c) && blue_to_cp.has_intersection(circles[c]));\n\t\t\t\t\t}\n\t\t\t\t\tif (!is_blocked) {\n\t\t\t\t\t\tmin_cost = std::min(min_cost, red_pos.distance(cross_point) + blue_pos.distance(cross_point));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tmin_cost = red_pos.distance(blue_pos);\n\t\t}\n\t\tif (min_cost <= 10000) {\n\t\t\tset_edge(node, r, b + red.size(), min_cost);\n\t\t}\n\t}\n\tfor (auto r = 0; r < red.size(); ++r) {\n\t\tset_edge(node, source, r, 0);\n\t}\n\tfor (auto b = 0; b < blue.size(); ++b) {\n\t\tset_edge(node, b + red.size(), sink, 0);\n\t}\n\treturn min_cost_matching(node, red.size(), source, sink);\n}\nint main() {\n\tint n; std::cin >> n;\n\tstd::vector<Circle> circles(2); for (auto& c : circles) std::cin >> c.center.x >> c.center.y >> c.radius;\n\tstd::vector<Coordinate> red(n), blue(n); \n\tfor (auto& r : red) std::cin >> r.x >> r.y;\n\tfor (auto& b : blue) std::cin >> b.x >> b.y;\n\tauto min_cost = cal_min_cost(red, blue, circles);\n\tif (min_cost >= 0) {\n\t\tstd::cout << std::setprecision(10) << std::fixed << min_cost << '\\n';\n\t}\n\telse {\n\t\tstd::cout << \"Impossible\\n\";\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3852, "score_of_the_acc": -0.0305, "final_rank": 1 }, { "submission_id": "aoj_1598_3922496", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <unordered_map>\n#include <set>\n#include <unordered_set>\n#include <tuple>\n#include <bitset>\n#include <memory>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <iomanip>\n#include <numeric>\n#include <climits>\n#include <cfloat>\n\nconstexpr double EPS = 0.000000001;\nconstexpr double DBL_MAX_VALUE = 1e9;\nstruct Coordinate;\nstruct Vector;\nstruct Segment;\nstruct Angle;\nstruct Line;\nstruct Circle;\nstruct Coordinate {\n\tdouble x, y;\n\tdouble distance(const Coordinate& that) const;\n\tVector operator-(const Coordinate& from) const;\n};\nstruct Vector {\n\tdouble x, y;\n\tVector rotate(const Angle& angle) const;\n\tdouble dot(const Vector& that) const;\n\tdouble cross(const Vector& that) const;\n\tdouble length() const;\n};\nVector operator*(const double& scala, const Vector& vec);\nVector operator*(const Vector& vec, const double scala);\nCoordinate operator+(const Coordinate& coord, const Vector& diff);\nCoordinate operator+(const Vector& diff, const Coordinate& coord);\nstruct Segment {\n\tCoordinate end_a, end_b;\n\tbool has_intersection(const Circle& that) const;\n\tdouble distance(const Coordinate& that) const;\n};\nstruct Angle {\n\tdouble sin, cos;\n\tstatic Angle from_sin(const double sin);\n\tAngle operator+(const Angle& that) const;\n\tAngle operator-() const;\n};\nstruct Line {\nprivate:\n\tconst Coordinate from, to;\n\tVector vec() const { return to - from; }\n\tdouble a() const { return to.y - from.y; }\n\tdouble b() const { return from.x - to.x; }\n\tdouble c() const { return - a() * from.x - from.y * b(); }\npublic:\n\tLine(const Coordinate& a, const Coordinate& b) : to{ a }, from{ b } {};\n\tbool is_cross(const Line& that) const;\n\tCoordinate cross_point(const Line& that) const;\n};\nstruct Circle {\n\tCoordinate center;\n\tdouble radius;\n\tstd::vector<Line> tangents(const Coordinate& that) const;\n};\n\ndouble Coordinate::distance(const Coordinate& that) const\n{\n\treturn (*this - that).length();\n}\n\nVector Coordinate::operator-(const Coordinate& from) const\n{\n\treturn Vector{ x - from.x, y - from.y };\n}\n\nVector Vector::rotate(const Angle& angle) const\n{\n\tauto len = length();\n\tauto a = Angle{ y / len, x / len } + angle;\n\treturn Vector{ a.cos * len, a.sin* len };\n}\n\ndouble Vector::dot(const Vector& that) const\n{\n\treturn x * that.x + y * that.y;\n}\n\ndouble Vector::cross(const Vector& that) const\n{\n\treturn x * that.y - y * that.x;\n}\n\ndouble Vector::length() const\n{\n\treturn std::sqrt(x * x + y * y);\n}\n\nVector operator*(const double& scala, const Vector& vec)\n{\n\treturn Vector{ vec.x * scala, vec.y * scala };\n}\n\nVector operator*(const Vector& vec, const double scala)\n{\n\treturn Vector{ vec.x * scala, vec.y * scala };\n}\n\nCoordinate operator+(const Coordinate& coord, const Vector& diff)\n{\n\treturn Coordinate{ coord.x + diff.x, coord.y + diff.y };\n}\n\nCoordinate operator+(const Vector& diff, const Coordinate& coord)\n{\n\treturn Coordinate{ diff.x + coord.x, diff.y + coord.y };\n}\n\nstd::vector<Line> Circle::tangents(const Coordinate& that) const\n{\n\tconst auto mid = center - that;\n\tconst auto angle = Angle::from_sin(radius / center.distance(that));\n\treturn { Line(that, mid.rotate(angle) + that), Line(that, mid.rotate(-angle) + that) };\n}\n\nbool Line::is_cross(const Line& that) const\n{\n\treturn std::abs(vec().cross(that.vec())) > 0;\n}\n\nCoordinate Line::cross_point(const Line& that) const\n{\n\tconst auto d = a() * that.b() - that.a() * b();\n\treturn Coordinate{ (b() * that.c() - that.b() * c()) / d, (that.a() * c() - a() * that.c()) / d };\n}\n\nAngle Angle::from_sin(const double sin)\n{\n\treturn Angle{ sin, std::sqrt(1 - sin * sin) };\n}\n\nAngle Angle::operator+(const Angle& that) const\n{\n\treturn Angle{ sin * that.cos + cos * that.sin, cos * that.cos - sin * that.sin };\n}\n\nAngle Angle::operator-() const\n{\n\treturn Angle{ -sin, cos };\n}\n\nbool Segment::has_intersection(const Circle& that) const\n{\n\treturn distance(that.center) < that.radius - EPS;\n}\n\ndouble Segment::distance(const Coordinate& that) const\n{\n\tconst auto vec = end_a - end_b;\n\tif (vec.dot(that - end_b) >= 0 && vec.dot(end_a - that) >= 0) {\n\t\treturn std::abs(vec.y * that.x - vec.x * that.y + end_a.x * end_b.y - end_a.y * end_b.x) / std::sqrt(vec.y * vec.y + vec.x * vec.x);\n\t}\n\telse {\n\t\treturn std::min(end_a.distance(that), end_b.distance(that));\n\t}\n}\n\nstruct Edge {\n\tint to;\n\tdouble cost;\n\tEdge* pair{ nullptr };\n\tbool has_flow;\n\tEdge(int _to, double _cost, bool _has_flow) :to{ _to }, cost{ _cost }, has_flow{ _has_flow }{};\n};\ntemplate<typename K, typename V>\nstruct KeyWithValue {\n\tK key;\n\tV value;\n\tKeyWithValue(const K& k, const V& v) :key{ k }, value{ v }{};\n};\ntemplate<typename K, typename V>\nbool operator>(const KeyWithValue<K, V>& a, const KeyWithValue<K, V>& b) {\n\treturn a.key > b.key;\n}\nvoid set_edge(std::vector<std::vector<Edge>>& node, const int from, const int to, double cost) {\n\tnode[from].emplace_back(to, cost, true);\n\tnode[to].emplace_back(from, -cost, false);\n\tnode[from].back().pair = &node[to].back();\n\tnode[to].back().pair = &node[from].back();\n}\ndouble min_cost_matching(std::vector<std::vector<Edge>> &nodes, const int flow, const int source, const int sink) {\n\tstd::vector<double> min_cost(nodes.size());\n\tstd::vector<double> potential(nodes.size(), 0);\n\tstd::vector<Edge*> prev(nodes.size(), nullptr);\n\tstd::priority_queue<KeyWithValue<double, int>, std::vector<KeyWithValue<double, int>>, std::greater<KeyWithValue<double, int>>> queue;\n\tdouble result = 0;\n\tfor (auto _i = 0; _i < flow; ++_i) {\n\t\tstd::fill(min_cost.begin(), min_cost.end(), DBL_MAX_VALUE);\n\t\tmin_cost[source] = 0;\n\t\tqueue.emplace(0.0, source);\n\t\twhile (!queue.empty()) {\n\t\t\tauto top = queue.top(); queue.pop();\n\t\t\tif (top.key == min_cost[top.value]) {\n\t\t\t\tfor (auto& edge : nodes[top.value]) if (edge.has_flow) {\n\t\t\t\t\tif (min_cost[edge.to] > potential[top.value] + edge.cost - potential[edge.to] + top.key + EPS) {\n\t\t\t\t\t\tmin_cost[edge.to] = potential[top.value] + edge.cost - potential[edge.to] + top.key;\n\t\t\t\t\t\tprev[edge.to] = &edge;\n\t\t\t\t\t\tqueue.emplace(min_cost[edge.to], edge.to);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (min_cost[sink] == DBL_MAX_VALUE) return -1;\n\t\tfor (auto i = 0; i < nodes.size(); ++i) potential[i] += min_cost[i];\n\t\tresult += potential[sink];\n\t\tauto last = sink;\n\t\twhile (last != source) {\n\t\t\tauto prev_edge = prev[last];\n\t\t\tprev_edge->has_flow = false;\n\t\t\tprev_edge->pair->has_flow = true;\n\t\t\tlast = prev_edge->pair->to;\n\t\t}\n\t}\n\treturn result;\n}\ndouble cal_min_cost(const std::vector<Coordinate>& red, const std::vector<Coordinate>& blue, const std::vector<Circle>& circles) {\n\tstd::vector<std::vector<std::vector<Line>>> red_tangents(red.size()), blue_tangents(blue.size());\n\tfor (auto i = 0; i < red.size(); ++i) {\n\t\tfor (const auto &circle: circles) {\n\t\t\tred_tangents[i].push_back(circle.tangents(red[i]));\n\t\t\tblue_tangents[i].push_back(circle.tangents(blue[i]));\n\t\t}\n\t}\n\tconst int source = red.size() + blue.size();\n\tconst int sink = source + 1;\n\tstd::vector<std::vector<Edge>> node(sink + 1); for (auto& n : node) n.reserve(red.size() + 1);\n\tfor (auto r = 0; r < red.size(); ++r) for (auto b = 0; b < blue.size(); ++b){\n\t\tconst auto red_pos = red[r];\n\t\tconst auto blue_pos = blue[b];\n\t\tdouble min_cost = DBL_MAX;\n\t\tconst auto direct = Segment{ red_pos, blue_pos };\n\t\tbool is_blocked = false;\n\t\tfor (const auto& c : circles) if (!is_blocked) {\n\t\t\tis_blocked = direct.has_intersection(c);\n\t\t}\n\t\tif (is_blocked) {\n\t\t\tfor (auto i = 0; i < circles.size(); ++i) for (auto j = 0; j < circles.size(); ++j) {\n\t\t\t\tfor (const auto& red_tangent : red_tangents[r][i]) for (const auto& blue_tangent : blue_tangents[b][j]) if (red_tangent.is_cross(blue_tangent)) {\n\t\t\t\t\tconst auto cross_point = red_tangent.cross_point(blue_tangent);\n\t\t\t\t\tconst auto red_to_cp = Segment{ cross_point, red_pos };\n\t\t\t\t\tconst auto blue_to_cp = Segment{ cross_point, blue_pos };\n\t\t\t\t\tbool is_blocked = false;\n\t\t\t\t\tfor (auto c = 0; c < circles.size() && !is_blocked; ++c) {\n\t\t\t\t\t\tif (i == c && red_to_cp.has_intersection(circles[c])) {\n\t\t\t\t\t\t\tstd::cerr << \"i: \" << i << '\\n';\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (j == c && blue_to_cp.has_intersection(circles[c])) {\n\t\t\t\t\t\t\tstd::cerr << \"i: \" << i << '\\n';\n\t\t\t\t\t\t}\n\t\t\t\t\t\tis_blocked = ((i != c) && red_to_cp.has_intersection(circles[c])) || ((j != c) && blue_to_cp.has_intersection(circles[c]));\n\t\t\t\t\t}\n\t\t\t\t\tif (!is_blocked) {\n\t\t\t\t\t\tmin_cost = std::min(min_cost, red_pos.distance(cross_point) + blue_pos.distance(cross_point));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tmin_cost = red_pos.distance(blue_pos);\n\t\t}\n\t\tif (min_cost <= 10000) {\n\t\t\tset_edge(node, r, b + red.size(), min_cost);\n\t\t}\n\t}\n\tfor (auto r = 0; r < red.size(); ++r) {\n\t\tset_edge(node, source, r, 0);\n\t}\n\tfor (auto b = 0; b < blue.size(); ++b) {\n\t\tset_edge(node, b + red.size(), sink, 0);\n\t}\n\treturn min_cost_matching(node, red.size(), source, sink);\n}\nint main() {\n\tint n; std::cin >> n;\n\tstd::vector<Circle> circles(2); for (auto& c : circles) std::cin >> c.center.x >> c.center.y >> c.radius;\n\tstd::vector<Coordinate> red(n), blue(n); \n\tfor (auto& r : red) std::cin >> r.x >> r.y;\n\tfor (auto& b : blue) std::cin >> b.x >> b.y;\n\tauto min_cost = cal_min_cost(red, blue, circles);\n\tif (min_cost >= 0) {\n\t\tstd::cout << std::setprecision(10) << std::fixed << min_cost << '\\n';\n\t}\n\telse {\n\t\tstd::cout << \"Impossible\\n\";\n\t}\n\treturn 0;\n}", "accuracy": 0.59375, "time_ms": 10, "memory_kb": 3748, "score_of_the_acc": 0, "final_rank": 9 }, { "submission_id": "aoj_1598_3691330", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ld = double;\nusing Point = std::complex<ld>;\n\nconst ld eps = 1e-9, pi = acos(-1.0);\n\nnamespace std\n{\nbool operator<(const Point &lhs, const Point &rhs)\n{\n if (lhs.real() < rhs.real() - eps)\n return true;\n if (lhs.real() > rhs.real() + eps)\n return false;\n return lhs.imag() < rhs.imag();\n}\n} // namespace std\n\nPoint input_point()\n{\n ld x, y;\n std::cin >> x >> y;\n return Point(x, y);\n}\n\nbool eq(ld a, ld b)\n{\n return (abs(a - b) < eps);\n}\n\nld dot(Point a, Point b)\n{\n return real(conj(a) * b);\n}\n\nld cross(Point a, Point b)\n{\n return imag(conj(a) * b);\n}\n\n// CCW::counter clockwise\nint ccw(Point a, Point b, Point c)\n{\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // a,b,c : counter-clockwise\n if (cross(b, c) < -eps)\n return -1; // a,b,c : clockwise\n if (dot(b, c) < 0)\n return 2; // c,a,b : on a line\n if (norm(b) < norm(c))\n return -2; // a,b,c : on a line\n return 0; // a,c,b : on a line\n}\n\nclass Line\n{\npublic:\n Point a, b;\n Line() : a(Point(0, 0)), b(Point(0, 0)) {}\n Line(Point a, Point b) : a(a), b(b) {}\n};\n\nld dot(Line l, Line m)\n{\n return dot((l.a - l.b), (m.a - m.b));\n}\n\n// l:line, m:line が交点を持つか\nbool isis_ll(Line l, Line m)\n{\n return !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// l:line, s:segment\nbool isis_ls(Line l, Line s)\n{\n return isis_ll(l, s) &&\n (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// s:segment, t:segment\nbool isis_ss(Line s, Line t)\n{\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// p が l:line 上に存在するか\nbool isis_lp(Line l, Point p)\n{\n return (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\nbool isis_sp(Line s, Point p)\n{\n return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// p から l に下ろした足との交点\nPoint proj(Line l, Point p)\n{\n ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + t * (l.a - l.b);\n}\n\n// l:line, t:line の交点\nPoint is_ll(Line l, Line m)\n{\n Point lv = l.b - l.a, mv = m.b - m.a;\n assert(cross(lv, mv) != 0);\n return l.a + lv * cross(mv, m.a - l.a) / cross(mv, lv);\n}\n\n// p, l:line の距離\nld dist_lp(Line l, Point p)\n{\n return abs(p - proj(l, p));\n}\n\nld dist_ll(Line l, Line m)\n{\n return isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\nld dist_ls(Line l, Line s)\n{\n return isis_ls(l, s) ? 0 : std::min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\nld dist_sp(Line s, Point p)\n{\n Point r = proj(s, p);\n return isis_sp(s, r) ? abs(r - p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\nld dist_ss(Line s, Line t)\n{\n if (isis_ss(s, t))\n return 0;\n return std::min({dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});\n}\n\nclass Circle\n{\npublic:\n Point p;\n ld r;\n Circle() : p(Point(0, 0)), r(0) {}\n Circle(Point p, ld r) : p(p), r(r) {}\n};\n\n// c1, c2 の交点\nstd::vector<Point> is_cc(Circle c1, Circle c2)\n{\n std::vector<Point> res;\n ld d = abs(c1.p - c2.p);\n ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n ld dfr = c1.r * c1.r - rc * rc;\n if (abs(dfr) < eps)\n dfr = 0.0;\n else if (dfr < 0.0)\n return res; // no intersection\n ld rs = sqrt(dfr);\n Point diff = (c2.p - c1.p) / d;\n res.emplace_back(c1.p + diff * Point(rc, rs));\n if (dfr != 0.0)\n res.emplace_back(c1.p + diff * Point(rc, -rs));\n return res;\n}\n\nstd::vector<Point> is_lc(Circle c, Line l)\n{\n std::vector<Point> res;\n ld d = dist_lp(l, c.p);\n if (d < c.r + eps)\n {\n ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n Point nor = (l.a - l.b) / abs(l.a - l.b);\n res.emplace_back(proj(l, c.p) + len * nor);\n res.emplace_back(proj(l, c.p) - len * nor);\n }\n return res;\n}\n\nstd::vector<Point> is_sc(Circle c, Line l)\n{\n std::vector<Point> v = is_lc(c, l), res;\n for (Point p : v)\n if (isis_sp(l, p))\n res.emplace_back(p);\n return res;\n}\n\n// p から c への接線\nstd::vector<Line> tangent_cp(Circle c, Point p)\n{\n std::vector<Line> ret;\n Point v = c.p - p;\n ld d = abs(v);\n ld l = sqrt(norm(v) - c.r * c.r);\n if (isnan((long double)l))\n {\n return ret;\n }\n Point v1 = v * Point(l / d, c.r / d);\n Point v2 = v * Point(l / d, -c.r / d);\n ret.emplace_back(Line(p, p + v1));\n if (l < eps)\n return ret;\n ret.emplace_back(Line(p, p + v2));\n return ret;\n}\n\nbool isContain(Circle c, Line l)\n{\n return dist_sp(l, c.p) + eps < c.r;\n}\n\nclass minCostFlow\n{\n using capacity_type = int;\n using cost_type = double;\n using pii = std::pair<cost_type, int>;\n const int INF = 1e9;\n struct Edge\n {\n int to, rev;\n capacity_type cap;\n cost_type cost;\n Edge(int to_, int _rev, capacity_type cap_, cost_type cost_)\n : to(to_), rev(_rev), cap(cap_), cost(cost_) {}\n };\n int V;\n std::vector<std::vector<Edge>> G;\n // ポテンシャル\n std::vector<cost_type> h;\n // 最短距離\n std::vector<cost_type> dist;\n // 直前の頂点, 辺\n std::vector<int> prevv, preve;\n\npublic:\n minCostFlow(int _V) : V(_V), G(_V), h(_V), dist(_V), prevv(_V), preve(_V) {}\n void add(int from, int to, capacity_type cap, cost_type cost)\n {\n G[from].push_back(Edge(to, G[to].size(), cap, cost));\n G[to].push_back(Edge(from, G[from].size() - 1, 0, -cost));\n }\n cost_type calc(int s, int t, int f)\n {\n cost_type res = 0;\n fill(h.begin(), h.end(), 0);\n while (f > 0)\n {\n std::priority_queue<pii, std::vector<pii>, std::greater<pii>> que;\n fill(dist.begin(), dist.end(), INF);\n dist[s] = 0;\n que.push(pii(0, s));\n while (!que.empty())\n {\n pii p = que.top();\n que.pop();\n int v = p.second;\n if (dist[v] < p.first)\n continue;\n for (size_t i = 0; i < G[v].size(); i++)\n {\n Edge &e = G[v][i];\n if (e.cap > eps && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to] + eps)\n {\n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n que.push(pii(dist[e.to], e.to));\n }\n }\n }\n if (dist[t] == INF)\n return -1;\n for (int v = 0; v < V; v++)\n h[v] += dist[v];\n capacity_type d = f;\n for (int v = t; v != s; v = prevv[v])\n {\n d = std::min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * h[t];\n for (int v = t; v != s; v = prevv[v])\n {\n Edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n};\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int n;\n cin >> n;\n ld x1, y1, r1, x2, y2, r2;\n cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;\n Circle c1(Point(x1, y1), r1), c2(Point(x2, y2), r2);\n vector<Point> rs, bs;\n for (int i = 0; i < n; i++)\n {\n rs.push_back(input_point());\n }\n for (int i = 0; i < n; i++)\n {\n bs.push_back(input_point());\n }\n // dist[i][j] := rs[i], bs[j] の最短移動距離\n vector<vector<ld>> dist(n, vector<ld>(n, 1e15));\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n Line l = Line(rs[i], bs[j]);\n if (is_sc(c1, l).size() <= 1 && is_sc(c2, l).size() <= 1)\n {\n dist[i][j] = abs(rs[i] - bs[j]);\n continue;\n }\n // rs[i] から c1 への接線\n auto rl1 = tangent_cp(c1, rs[i]);\n // bs[j] から c1 への接線\n auto bl1 = tangent_cp(c1, bs[j]);\n // rs[i], c2 への接線\n auto rl2 = tangent_cp(c2, rs[i]);\n // bs[j], c2 への接線\n auto bl2 = tangent_cp(c2, bs[j]);\n for (auto l1 : rl1)\n {\n for (auto l2 : bl1)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c2, Line(rs[i], p)) || isContain(c2, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n for (auto l1 : rl2)\n {\n for (auto l2 : bl2)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c1, Line(rs[i], p)) || isContain(c1, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n // rs[i]-c1, bs[j]-c2\n for (auto l1 : rl1)\n {\n for (auto l2 : bl2)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c2, Line(rs[i], p)) || isContain(c1, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n // rs[i]-c2, bs[j]-c1\n for (auto l1 : rl2)\n {\n for (auto l2 : bl1)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c1, Line(rs[i], p)) || isContain(c2, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n }\n }\n minCostFlow mcf(2 * n + 2);\n for (int i = 0; i < n; i++)\n {\n mcf.add(0, i + 1, 1, 0);\n mcf.add(n + i + 1, 2 * n + 1, 1, 0);\n for (int j = 0; j < n; j++)\n {\n int st = i + 1, gt = n + 1 + j;\n if (dist[i][j] != 1e15)\n {\n mcf.add(st, gt, 1, dist[i][j]);\n }\n }\n }\n auto ret = mcf.calc(0, 2 * n + 1, n);\n if (ret == -1)\n cout << \"Impossible\" << endl;\n else\n cout << fixed << setprecision(10) << ret << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4016, "score_of_the_acc": -0.1897, "final_rank": 2 }, { "submission_id": "aoj_1598_3690545", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ld = double;\nusing Point = std::complex<ld>;\n\nconst ld eps = 1e-9, pi = acos(-1.0);\n\nnamespace std\n{\nbool operator<(const Point &lhs, const Point &rhs)\n{\n if (lhs.real() < rhs.real() - eps)\n return true;\n if (lhs.real() > rhs.real() + eps)\n return false;\n return lhs.imag() < rhs.imag();\n}\n} // namespace std\n\nPoint input_point()\n{\n ld x, y;\n std::cin >> x >> y;\n return Point(x, y);\n}\n\nbool eq(ld a, ld b)\n{\n return (abs(a - b) < eps);\n}\n\nld dot(Point a, Point b)\n{\n return real(conj(a) * b);\n}\n\nld cross(Point a, Point b)\n{\n return imag(conj(a) * b);\n}\n\n// CCW::counter clockwise\nint ccw(Point a, Point b, Point c)\n{\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // a,b,c : counter-clockwise\n if (cross(b, c) < -eps)\n return -1; // a,b,c : clockwise\n if (dot(b, c) < 0)\n return 2; // c,a,b : on a line\n if (norm(b) < norm(c))\n return -2; // a,b,c : on a line\n return 0; // a,c,b : on a line\n}\n\nclass Line\n{\npublic:\n Point a, b;\n Line() : a(Point(0, 0)), b(Point(0, 0)) {}\n Line(Point a, Point b) : a(a), b(b) {}\n};\n\nbool isis_sp(Line s, Point p)\n{\n return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// p から l に下ろした足との交点\nPoint proj(Line l, Point p)\n{\n ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + t * (l.a - l.b);\n}\n\n// l:line, t:line の交点\nPoint is_ll(Line l, Line m)\n{\n Point lv = l.b - l.a, mv = m.b - m.a;\n assert(cross(lv, mv) != 0);\n return l.a + lv * cross(mv, m.a - l.a) / cross(mv, lv);\n}\n\n// p, l:line の距離\nld dist_lp(Line l, Point p)\n{\n return abs(p - proj(l, p));\n}\n\nld dist_sp(Line s, Point p)\n{\n Point r = proj(s, p);\n return isis_sp(s, r) ? abs(r - p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\nclass Circle\n{\npublic:\n Point p;\n ld r;\n Circle() : p(Point(0, 0)), r(0) {}\n Circle(Point p, ld r) : p(p), r(r) {}\n};\n\nstd::vector<Point> is_lc(Circle c, Line l)\n{\n std::vector<Point> res;\n ld d = dist_lp(l, c.p);\n if (d < c.r + eps)\n {\n ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n Point nor = (l.a - l.b) / abs(l.a - l.b);\n res.emplace_back(proj(l, c.p) + len * nor);\n res.emplace_back(proj(l, c.p) - len * nor);\n }\n return res;\n}\n\nstd::vector<Point> is_sc(Circle c, Line l)\n{\n std::vector<Point> v = is_lc(c, l), res;\n for (Point p : v)\n if (isis_sp(l, p))\n res.emplace_back(p);\n return res;\n}\n\n// p から c への接線\nstd::vector<Line> tangent_cp(Circle c, Point p)\n{\n std::vector<Line> ret;\n Point v = c.p - p;\n ld d = abs(v);\n ld l = sqrt(norm(v) - c.r * c.r);\n if (isnan((long double)l))\n {\n return ret;\n }\n Point v1 = v * Point(l / d, c.r / d);\n Point v2 = v * Point(l / d, -c.r / d);\n ret.emplace_back(Line(p, p + v1));\n if (l < eps)\n return ret;\n ret.emplace_back(Line(p, p + v2));\n return ret;\n}\n\nbool isContain(Circle c, Line l)\n{\n return dist_sp(l, c.p) + eps < c.r;\n}\n\nclass minCostFlow\n{\n using capacity_type = int;\n using cost_type = double;\n using pii = std::pair<cost_type, int>;\n const int INF = 1e9;\n struct Edge\n {\n int to, rev;\n capacity_type cap;\n cost_type cost;\n Edge(int to_, int _rev, capacity_type cap_, cost_type cost_)\n : to(to_), rev(_rev), cap(cap_), cost(cost_) {}\n };\n int V;\n std::vector<std::vector<Edge>> G;\n // ポテンシャル\n std::vector<cost_type> h;\n // 最短距離\n std::vector<cost_type> dist;\n // 直前の頂点, 辺\n std::vector<int> prevv, preve;\n\npublic:\n minCostFlow(int _V) : V(_V), G(_V), h(_V), dist(_V), prevv(_V), preve(_V) {}\n void add(int from, int to, capacity_type cap, cost_type cost)\n {\n G[from].push_back(Edge(to, G[to].size(), cap, cost));\n G[to].push_back(Edge(from, G[from].size() - 1, 0, -cost));\n }\n cost_type calc(int s, int t, int f)\n {\n cost_type res = 0;\n fill(h.begin(), h.end(), 0);\n while (f > 0)\n {\n std::priority_queue<pii, std::vector<pii>, std::greater<pii>> que;\n fill(dist.begin(), dist.end(), INF);\n dist[s] = 0;\n que.push(pii(0, s));\n while (!que.empty())\n {\n pii p = que.top();\n que.pop();\n int v = p.second;\n if (dist[v] < p.first)\n continue;\n for (size_t i = 0; i < G[v].size(); i++)\n {\n Edge &e = G[v][i];\n if (e.cap > eps && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to] + eps)\n {\n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n que.push(pii(dist[e.to], e.to));\n }\n }\n }\n if (dist[t] == INF)\n return -1;\n for (int v = 0; v < V; v++)\n h[v] += dist[v];\n capacity_type d = f;\n for (int v = t; v != s; v = prevv[v])\n {\n d = std::min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * h[t];\n for (int v = t; v != s; v = prevv[v])\n {\n Edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n};\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int n;\n cin >> n;\n ld x1, y1, r1, x2, y2, r2;\n cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;\n Circle c1(Point(x1, y1), r1), c2(Point(x2, y2), r2);\n vector<Point> rs, bs;\n for (int i = 0; i < n; i++)\n {\n rs.push_back(input_point());\n }\n for (int i = 0; i < n; i++)\n {\n bs.push_back(input_point());\n }\n // dist[i][j] := rs[i], bs[j] の最短移動距離\n vector<vector<ld>> dist(n, vector<ld>(n, 1e15));\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n Line l = Line(rs[i], bs[j]);\n if (is_sc(c1, l).size() <= 1 && is_sc(c2, l).size() <= 1)\n {\n dist[i][j] = abs(rs[i] - bs[j]);\n continue;\n }\n // rs[i] から c1 への接線\n auto rl1 = tangent_cp(c1, rs[i]);\n // bs[j] から c1 への接線\n auto bl1 = tangent_cp(c1, bs[j]);\n // rs[i], c2 への接線\n auto rl2 = tangent_cp(c2, rs[i]);\n // bs[j], c2 への接線\n auto bl2 = tangent_cp(c2, bs[j]);\n for (auto l1 : rl1)\n {\n for (auto l2 : bl1)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c2, Line(rs[i], p)) || isContain(c2, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n for (auto l1 : rl2)\n {\n for (auto l2 : bl2)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c1, Line(rs[i], p)) || isContain(c1, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n // rs[i]-c1, bs[j]-c2\n for (auto l1 : rl1)\n {\n for (auto l2 : bl2)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c2, Line(rs[i], p)) || isContain(c1, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n // rs[i]-c2, bs[j]-c1\n for (auto l1 : rl2)\n {\n for (auto l2 : bl1)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c1, Line(rs[i], p)) || isContain(c2, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n }\n }\n minCostFlow mcf(2 * n + 2);\n for (int i = 0; i < n; i++)\n {\n mcf.add(0, i + 1, 1, 0);\n mcf.add(n + i + 1, 2 * n + 1, 1, 0);\n for (int j = 0; j < n; j++)\n {\n int st = i + 1, gt = n + 1 + j;\n if (dist[i][j] != 1e15)\n {\n mcf.add(st, gt, 1, dist[i][j]);\n }\n }\n }\n auto ret = mcf.calc(0, 2 * n + 1, n);\n if (ret == -1)\n cout << \"Impossible\" << endl;\n else\n cout << fixed << setprecision(10) << ret << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4096, "score_of_the_acc": -0.2132, "final_rank": 3 }, { "submission_id": "aoj_1598_3690544", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ld = double;\nusing Point = std::complex<ld>;\n\nconst ld eps = 1e-9, pi = acos(-1.0);\n\nnamespace std\n{\nbool operator<(const Point &lhs, const Point &rhs)\n{\n if (lhs.real() < rhs.real() - eps)\n return true;\n if (lhs.real() > rhs.real() + eps)\n return false;\n return lhs.imag() < rhs.imag();\n}\n} // namespace std\n\nPoint input_point()\n{\n ld x, y;\n std::cin >> x >> y;\n return Point(x, y);\n}\n\nbool eq(ld a, ld b)\n{\n return (abs(a - b) < eps);\n}\n\nld dot(Point a, Point b)\n{\n return real(conj(a) * b);\n}\n\nld cross(Point a, Point b)\n{\n return imag(conj(a) * b);\n}\n\n// CCW::counter clockwise\nint ccw(Point a, Point b, Point c)\n{\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // a,b,c : counter-clockwise\n if (cross(b, c) < -eps)\n return -1; // a,b,c : clockwise\n if (dot(b, c) < 0)\n return 2; // c,a,b : on a line\n if (norm(b) < norm(c))\n return -2; // a,b,c : on a line\n return 0; // a,c,b : on a line\n}\n\nclass Line\n{\npublic:\n Point a, b;\n Line() : a(Point(0, 0)), b(Point(0, 0)) {}\n Line(Point a, Point b) : a(a), b(b) {}\n};\n\nld dot(Line l, Line m)\n{\n return dot((l.a - l.b), (m.a - m.b));\n}\n\n// l:line, m:line が交点を持つか\nbool isis_ll(Line l, Line m)\n{\n return !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// l:line, s:segment\nbool isis_ls(Line l, Line s)\n{\n return isis_ll(l, s) &&\n (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// s:segment, t:segment\nbool isis_ss(Line s, Line t)\n{\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// p が l:line 上に存在するか\nbool isis_lp(Line l, Point p)\n{\n return (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\nbool isis_sp(Line s, Point p)\n{\n return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// p から l に下ろした足との交点\nPoint proj(Line l, Point p)\n{\n ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + t * (l.a - l.b);\n}\n\n// l:line, t:line の交点\nPoint is_ll(Line l, Line m)\n{\n Point lv = l.b - l.a, mv = m.b - m.a;\n assert(cross(lv, mv) != 0);\n return l.a + lv * cross(mv, m.a - l.a) / cross(mv, lv);\n}\n\n// p, l:line の距離\nld dist_lp(Line l, Point p)\n{\n return abs(p - proj(l, p));\n}\n\nld dist_sp(Line s, Point p)\n{\n Point r = proj(s, p);\n return isis_sp(s, r) ? abs(r - p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\nclass Circle\n{\npublic:\n Point p;\n ld r;\n Circle() : p(Point(0, 0)), r(0) {}\n Circle(Point p, ld r) : p(p), r(r) {}\n};\n\nstd::vector<Point> is_lc(Circle c, Line l)\n{\n std::vector<Point> res;\n ld d = dist_lp(l, c.p);\n if (d < c.r + eps)\n {\n ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n Point nor = (l.a - l.b) / abs(l.a - l.b);\n res.emplace_back(proj(l, c.p) + len * nor);\n res.emplace_back(proj(l, c.p) - len * nor);\n }\n return res;\n}\n\nstd::vector<Point> is_sc(Circle c, Line l)\n{\n std::vector<Point> v = is_lc(c, l), res;\n for (Point p : v)\n if (isis_sp(l, p))\n res.emplace_back(p);\n return res;\n}\n\n// p から c への接線\nstd::vector<Line> tangent_cp(Circle c, Point p)\n{\n std::vector<Line> ret;\n Point v = c.p - p;\n ld d = abs(v);\n ld l = sqrt(norm(v) - c.r * c.r);\n if (isnan((long double)l))\n {\n return ret;\n }\n Point v1 = v * Point(l / d, c.r / d);\n Point v2 = v * Point(l / d, -c.r / d);\n ret.emplace_back(Line(p, p + v1));\n if (l < eps)\n return ret;\n ret.emplace_back(Line(p, p + v2));\n return ret;\n}\n\nbool isContain(Circle c, Line l)\n{\n return dist_sp(l, c.p) + eps < c.r;\n}\n\nclass minCostFlow\n{\n using capacity_type = int;\n using cost_type = double;\n using pii = std::pair<cost_type, int>;\n const int INF = 1e9;\n struct Edge\n {\n int to, rev;\n capacity_type cap;\n cost_type cost;\n Edge(int to_, int _rev, capacity_type cap_, cost_type cost_)\n : to(to_), rev(_rev), cap(cap_), cost(cost_) {}\n };\n int V;\n std::vector<std::vector<Edge>> G;\n // ポテンシャル\n std::vector<cost_type> h;\n // 最短距離\n std::vector<cost_type> dist;\n // 直前の頂点, 辺\n std::vector<int> prevv, preve;\n\npublic:\n minCostFlow(int _V) : V(_V), G(_V), h(_V), dist(_V), prevv(_V), preve(_V) {}\n void add(int from, int to, capacity_type cap, cost_type cost)\n {\n G[from].push_back(Edge(to, G[to].size(), cap, cost));\n G[to].push_back(Edge(from, G[from].size() - 1, 0, -cost));\n }\n cost_type calc(int s, int t, int f)\n {\n cost_type res = 0;\n fill(h.begin(), h.end(), 0);\n while (f > 0)\n {\n std::priority_queue<pii, std::vector<pii>, std::greater<pii>> que;\n fill(dist.begin(), dist.end(), INF);\n dist[s] = 0;\n que.push(pii(0, s));\n while (!que.empty())\n {\n pii p = que.top();\n que.pop();\n int v = p.second;\n if (dist[v] < p.first)\n continue;\n for (size_t i = 0; i < G[v].size(); i++)\n {\n Edge &e = G[v][i];\n if (e.cap > eps && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to] + eps)\n {\n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n que.push(pii(dist[e.to], e.to));\n }\n }\n }\n if (dist[t] == INF)\n return -1;\n for (int v = 0; v < V; v++)\n h[v] += dist[v];\n capacity_type d = f;\n for (int v = t; v != s; v = prevv[v])\n {\n d = std::min(d, G[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * h[t];\n for (int v = t; v != s; v = prevv[v])\n {\n Edge &e = G[prevv[v]][preve[v]];\n e.cap -= d;\n G[v][e.rev].cap += d;\n }\n }\n return res;\n }\n};\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int n;\n cin >> n;\n ld x1, y1, r1, x2, y2, r2;\n cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;\n Circle c1(Point(x1, y1), r1), c2(Point(x2, y2), r2);\n vector<Point> rs, bs;\n for (int i = 0; i < n; i++)\n {\n rs.push_back(input_point());\n }\n for (int i = 0; i < n; i++)\n {\n bs.push_back(input_point());\n }\n // dist[i][j] := rs[i], bs[j] の最短移動距離\n vector<vector<ld>> dist(n, vector<ld>(n, 1e15));\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n Line l = Line(rs[i], bs[j]);\n if (is_sc(c1, l).size() <= 1 && is_sc(c2, l).size() <= 1)\n {\n dist[i][j] = abs(rs[i] - bs[j]);\n continue;\n }\n // rs[i] から c1 への接線\n auto rl1 = tangent_cp(c1, rs[i]);\n // bs[j] から c1 への接線\n auto bl1 = tangent_cp(c1, bs[j]);\n // rs[i], c2 への接線\n auto rl2 = tangent_cp(c2, rs[i]);\n // bs[j], c2 への接線\n auto bl2 = tangent_cp(c2, bs[j]);\n for (auto l1 : rl1)\n {\n for (auto l2 : bl1)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c2, Line(rs[i], p)) || isContain(c2, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n for (auto l1 : rl2)\n {\n for (auto l2 : bl2)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c1, Line(rs[i], p)) || isContain(c1, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n // rs[i]-c1, bs[j]-c2\n for (auto l1 : rl1)\n {\n for (auto l2 : bl2)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c2, Line(rs[i], p)) || isContain(c1, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n // rs[i]-c2, bs[j]-c1\n for (auto l1 : rl2)\n {\n for (auto l2 : bl1)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c1, Line(rs[i], p)) || isContain(c2, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n }\n }\n minCostFlow mcf(2 * n + 2);\n for (int i = 0; i < n; i++)\n {\n mcf.add(0, i + 1, 1, 0);\n mcf.add(n + i + 1, 2 * n + 1, 1, 0);\n for (int j = 0; j < n; j++)\n {\n int st = i + 1, gt = n + 1 + j;\n if (dist[i][j] != 1e15)\n {\n mcf.add(st, gt, 1, dist[i][j]);\n }\n }\n }\n auto ret = mcf.calc(0, 2 * n + 1, n);\n if (ret == -1)\n cout << \"Impossible\" << endl;\n else\n cout << fixed << setprecision(10) << ret << endl;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4096, "score_of_the_acc": -0.2132, "final_rank": 3 }, { "submission_id": "aoj_1598_3689841", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing ld = long double;\nusing Point = std::complex<ld>;\n\nconst ld eps = 1e-9, pi = acos(-1.0);\n\nnamespace std\n{\nbool operator<(const Point &lhs, const Point &rhs)\n{\n if (lhs.real() < rhs.real() - eps)\n return true;\n if (lhs.real() > rhs.real() + eps)\n return false;\n return lhs.imag() < rhs.imag();\n}\n} // namespace std\n\nPoint input_point()\n{\n ld x, y;\n std::cin >> x >> y;\n return Point(x, y);\n}\n\nbool eq(ld a, ld b)\n{\n return (abs(a - b) < eps);\n}\n\nld dot(Point a, Point b)\n{\n return real(conj(a) * b);\n}\n\nld cross(Point a, Point b)\n{\n return imag(conj(a) * b);\n}\n\n// CCW::counter clockwise\nint ccw(Point a, Point b, Point c)\n{\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // a,b,c : counter-clockwise\n if (cross(b, c) < -eps)\n return -1; // a,b,c : clockwise\n if (dot(b, c) < 0)\n return 2; // c,a,b : on a line\n if (norm(b) < norm(c))\n return -2; // a,b,c : on a line\n return 0; // a,c,b : on a line\n}\n\nclass Line\n{\npublic:\n Point a, b;\n Line() : a(Point(0, 0)), b(Point(0, 0)) {}\n Line(Point a, Point b) : a(a), b(b) {}\n};\n\nld dot(Line l, Line m)\n{\n return dot((l.a - l.b), (m.a - m.b));\n}\n\n// l:line, m:line が交点を持つか\nbool isis_ll(Line l, Line m)\n{\n return !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n\n// l:line, s:segment\nbool isis_ls(Line l, Line s)\n{\n return isis_ll(l, s) &&\n (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n\n// s:segment, t:segment\nbool isis_ss(Line s, Line t)\n{\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n\n// p が l:line 上に存在するか\nbool isis_lp(Line l, Point p)\n{\n return (abs(cross(l.b - p, l.a - p)) < eps);\n}\n\nbool isis_sp(Line s, Point p)\n{\n return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n\n// p から l に下ろした足との交点\nPoint proj(Line l, Point p)\n{\n ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + t * (l.a - l.b);\n}\n\n// l:line, t:line の交点\nPoint is_ll(Line l, Line m)\n{\n Point lv = l.b - l.a, mv = m.b - m.a;\n assert(cross(lv, mv) != 0);\n return l.a + lv * cross(mv, m.a - l.a) / cross(mv, lv);\n}\n\n// p, l:line の距離\nld dist_lp(Line l, Point p)\n{\n return abs(p - proj(l, p));\n}\n\nld dist_ll(Line l, Line m)\n{\n return isis_ll(l, m) ? 0 : dist_lp(l, m.a);\n}\n\nld dist_ls(Line l, Line s)\n{\n return isis_ls(l, s) ? 0 : std::min(dist_lp(l, s.a), dist_lp(l, s.b));\n}\n\nld dist_sp(Line s, Point p)\n{\n Point r = proj(s, p);\n return isis_sp(s, r) ? abs(r - p) : std::min(abs(s.a - p), abs(s.b - p));\n}\n\nld dist_ss(Line s, Line t)\n{\n if (isis_ss(s, t))\n return 0;\n return std::min({dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});\n}\n\nclass Circle\n{\npublic:\n Point p;\n ld r;\n Circle() : p(Point(0, 0)), r(0) {}\n Circle(Point p, ld r) : p(p), r(r) {}\n};\n\n// c1, c2 の交点\nstd::vector<Point> is_cc(Circle c1, Circle c2)\n{\n std::vector<Point> res;\n ld d = abs(c1.p - c2.p);\n ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n ld dfr = c1.r * c1.r - rc * rc;\n if (abs(dfr) < eps)\n dfr = 0.0;\n else if (dfr < 0.0)\n return res; // no intersection\n ld rs = sqrt(dfr);\n Point diff = (c2.p - c1.p) / d;\n res.emplace_back(c1.p + diff * Point(rc, rs));\n if (dfr != 0.0)\n res.emplace_back(c1.p + diff * Point(rc, -rs));\n return res;\n}\n\nstd::vector<Point> is_lc(Circle c, Line l)\n{\n std::vector<Point> res;\n ld d = dist_lp(l, c.p);\n if (d < c.r + eps)\n {\n ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;\n Point nor = (l.a - l.b) / abs(l.a - l.b);\n res.emplace_back(proj(l, c.p) + len * nor);\n res.emplace_back(proj(l, c.p) - len * nor);\n }\n return res;\n}\n\nstd::vector<Point> is_sc(Circle c, Line l)\n{\n std::vector<Point> v = is_lc(c, l), res;\n for (Point p : v)\n if (isis_sp(l, p))\n res.emplace_back(p);\n return res;\n}\n\n// p から c への接線\nstd::vector<Line> tangent_cp(Circle c, Point p)\n{\n std::vector<Line> ret;\n Point v = c.p - p;\n ld d = abs(v);\n ld l = sqrt(norm(v) - c.r * c.r);\n if (isnan(l))\n {\n return ret;\n }\n Point v1 = v * Point(l / d, c.r / d);\n Point v2 = v * Point(l / d, -c.r / d);\n ret.emplace_back(Line(p, p + v1));\n if (l < eps)\n return ret;\n ret.emplace_back(Line(p, p + v2));\n return ret;\n}\n\nbool isContain(Circle c, Line l)\n{\n // l が c と交差するか(交点が2個以上か)\n return dist_sp(l, c.p) + eps < c.r;\n}\n\nstruct Edge\n{\n using cost_type = ld;\n using capacity_type = int;\n int to, rev;\n capacity_type cap;\n cost_type cost;\n Edge(int _to, int _rev, capacity_type _cap, cost_type _cost) : to(_to), rev(_rev), cap(_cap), cost(_cost) {}\n};\n\nvoid add_edge(vector<vector<Edge>> &g, int from, int to, Edge::capacity_type cap, Edge::cost_type cost)\n{\n g[from].emplace_back(to, static_cast<int>(g[to].size()), cap, cost);\n g[to].emplace_back(from, static_cast<int>(g[from].size() - 1), Edge::capacity_type{0}, -cost);\n}\n\ntemplate <typename Edge>\ntypename Edge::cost_type min_cost_flow(vector<vector<Edge>> &g, int s, int t, typename Edge::capacity_type f)\n{\n using cost_type = typename Edge::cost_type;\n using P = std::pair<cost_type, int>;\n const auto inf = std::numeric_limits<cost_type>::max() / 2;\n cost_type res = 0;\n std::vector<cost_type> h(g.size()), dist(g.size());\n std::vector<int> prevv(g.size()), preve(g.size());\n while (f > 0)\n {\n std::priority_queue<P, std::vector<P>, std::greater<>> que;\n std::fill(std::begin(dist), std::end(dist), inf);\n dist[s] = 0;\n que.emplace(dist[s], s);\n while (!que.empty())\n {\n const auto cur_d = que.top().first;\n const int v = que.top().second;\n que.pop();\n if (dist[v] < cur_d)\n continue;\n for (int i = 0; i < (int)g[v].size(); ++i)\n {\n auto &e = g[v][i];\n if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to])\n {\n dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];\n prevv[e.to] = v;\n preve[e.to] = i;\n que.emplace(dist[e.to], e.to);\n }\n }\n }\n if (dist[t] == inf)\n return -1;\n for (int v = 0; v < (int)g.size(); ++v)\n {\n h[v] += dist[v];\n }\n\n auto d = f;\n for (int v = t; v != s; v = prevv[v])\n {\n d = std::min(d, g[prevv[v]][preve[v]].cap);\n }\n f -= d;\n res += d * h[t];\n for (int v = t; v != s; v = prevv[v])\n {\n auto &e = g[prevv[v]][preve[v]];\n e.cap -= d;\n g[v][e.rev].cap += d;\n }\n }\n return res;\n}\n\nint main()\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n\n int n;\n cin >> n;\n ld x1, y1, r1, x2, y2, r2;\n cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;\n Circle c1(Point(x1, y1), r1), c2(Point(x2, y2), r2);\n vector<Point> rs, bs;\n for (int i = 0; i < n; i++)\n {\n rs.push_back(input_point());\n }\n for (int i = 0; i < n; i++)\n {\n bs.push_back(input_point());\n }\n // dist[i][j] := rs[i], bs[j] の最短移動距離\n vector<vector<ld>> dist(n, vector<ld>(n, 1e15));\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n Line l = Line(rs[i], bs[j]);\n if (is_sc(c1, l).size() <= 1 && is_sc(c2, l).size() <= 1)\n {\n dist[i][j] = abs(rs[i] - bs[j]);\n continue;\n }\n // rs[i] から c1 への接線\n auto rl1 = tangent_cp(c1, rs[i]);\n // bs[j] から c1 への接線\n auto bl1 = tangent_cp(c1, bs[j]);\n // rs[i], c2 への接線\n auto rl2 = tangent_cp(c2, rs[i]);\n // bs[j], c2 への接線\n auto bl2 = tangent_cp(c2, bs[j]);\n for (auto l1 : rl1)\n {\n for (auto l2 : bl1)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c2, Line(rs[i], p)) || isContain(c2, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n for (auto l1 : rl2)\n {\n for (auto l2 : bl2)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c1, Line(rs[i], p)) || isContain(c1, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n // rs[i]-c1, bs[j]-c2\n for (auto l1 : rl1)\n {\n for (auto l2 : bl2)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c2, Line(rs[i], p)) || isContain(c1, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n // rs[i]-c2, bs[j]-c1\n for (auto l1 : rl2)\n {\n for (auto l2 : bl1)\n {\n Point lv = l1.b - l1.a, mv = l2.b - l2.a;\n if (cross(lv, mv) == 0)\n continue;\n Point p = is_ll(l1, l2);\n if (isContain(c1, Line(rs[i], p)) || isContain(c2, Line(bs[j], p)))\n continue;\n dist[i][j] = min(dist[i][j], abs(p - rs[i]) + abs(p - bs[j]));\n }\n }\n }\n }\n vector<vector<Edge>> g(2 * n + 2);\n for (int i = 0; i < n; i++)\n {\n add_edge(g, 0, i + 1, 1, 0);\n add_edge(g, n + i + 1, 2 * n + 1, 1, 0);\n for (int j = 0; j < n; j++)\n {\n int st = i + 1, gt = n + 1 + i;\n add_edge(g, st, gt, 1, dist[i][j]);\n // cout << i << \" \" << j << \" \" << dist[i][j] << endl;\n }\n }\n auto ret = min_cost_flow<Edge>(g, 0, 2 * n + 1, (Edge::capacity_type)n);\n if (ret > 100000)\n cout << \"Impossible\" << endl;\n else\n cout << fixed << setprecision(10) << ret << endl;\n}", "accuracy": 0.21875, "time_ms": 40, "memory_kb": 4108, "score_of_the_acc": -0.2723, "final_rank": 10 }, { "submission_id": "aoj_1598_3672445", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-9;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nint par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size()-1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, -1);\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = id;\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur];\n\t\tif (p < 0)return -1;\n\t\trep(j, G[p].size()) {\n\t\t\tif (G[p][j].cap > 0 && G[p][j].to == cur && dist[p] + G[p][j].cost == dist[cur]) {\n\t\t\t\tG[p][j].cap--;\n\t\t\t\tG[cur][G[p][j].rev].cap++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s,c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { {x,y},r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n,t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2,r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 7156, "score_of_the_acc": -1.7778, "final_rank": 7 }, { "submission_id": "aoj_1598_3672443", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-10;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nint par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size()-1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, -1);\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = id;\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur];\n\t\tif (p < 0)return -1;\n\t\trep(j, G[p].size()) {\n\t\t\tif (G[p][j].cap > 0 && G[p][j].to == cur && dist[p] + G[p][j].cost == dist[cur]) {\n\t\t\t\tG[p][j].cap--;\n\t\t\t\tG[cur][G[p][j].rev].cap++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s,c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { {x,y},r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n,t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2,r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1||ans>10000+eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\t\treturn 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 7048, "score_of_the_acc": -1.9683, "final_rank": 8 }, { "submission_id": "aoj_1598_3669097", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\nstatic const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;\ntypedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;\ntemplate<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }\ntemplate<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }\n \nconst double EPS = 1e-9;\n \nconst double InfCost = 1e99;\nstruct MinimumCostMaximumFlow {\n typedef int Index; typedef int Flow; typedef double Cost;\n static const Flow InfCapacity = INF;\n struct Edge {\n Index to; Index rev;\n Flow capacity; Cost cost;\n };\n vector<vector<Edge> > g;\n void init(Index n) { g.assign(n, vector<Edge>()); }\n void add(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) {\n Edge e, f; e.to = j, f.to = i; e.capacity = capacity, f.capacity = 0; e.cost = cost, f.cost = -cost;\n g[i].push_back(e); g[j].push_back(f);\n g[i].back().rev = (Index)g[j].size() - 1; g[j].back().rev = (Index)g[i].size() - 1;\n }\n void addB(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) {\n add(i, j, capacity, cost);\n add(j, i, capacity, cost);\n }\n pair<Cost, Flow> minimumCostMaximumFlow(Index s, Index t, Flow f = InfCapacity, bool bellmanFord = false) {\n int n = g.size();\n vector<Cost> dist(n); vector<Index> prev(n); vector<Index> prevEdge(n);\n pair<Cost, Flow> total = make_pair(0, 0);\n vector<Cost> potential(n);\n while(f > 0) {\n fill(dist.begin(), dist.end(), InfCost);\n if(bellmanFord || total.second == 0) {\n dist[s] = 0;\n rep(k, n) {\n bool update = false;\n rep(i, n) if(dist[i] != InfCost)\n for(Index ei = 0; ei < (Index)g[i].size(); ei ++) {\n const Edge &e = g[i][ei];\n if(e.capacity <= 0) continue;\n Index j = e.to; Cost d = dist[i] + e.cost;\n if(dist[j] > d + EPS) { //?????????\n dist[j] = d; prev[j] = i; prevEdge[j] = ei;\n update = true;\n }\n }\n if(!update) break;\n }\n } else {\n vector<bool> vis(n);\n priority_queue<pair<Cost, Index> > q;\n q.push(make_pair(-0, s)); dist[s] = 0;\n while(!q.empty()) {\n Index i = q.top().second; q.pop();\n if(vis[i]) continue;\n vis[i] = true;\n for(Index ei = 0; ei < (Index)g[i].size(); ei ++) {\n const Edge &e = g[i][ei];\n if(e.capacity <= 0) continue;\n Index j = e.to; Cost d = dist[i] + e.cost + potential[i] - potential[j];\n if(d < dist[i]) d = dist[i]; //?????????\n if(dist[j] > d) {\n dist[j] = d; prev[j] = i; prevEdge[j] = ei;\n q.push(make_pair(-d, j));\n }\n }\n }\n }\n if(dist[t] == InfCost) break;\n if(!bellmanFord) for(Index i = 0; i < n; i ++) potential[i] += dist[i];\n Flow d = f; Cost distt = 0;\n for(Index v = t; v != s; ) {\n Index u = prev[v]; const Edge &e = g[u][prevEdge[v]];\n d = min(d, e.capacity); distt += e.cost; v = u;\n }\n f -= d; total.first += d * distt; total.second += d;\n for(Index v = t; v != s; v = prev[v]) {\n Edge &e = g[prev[v]][prevEdge[v]];\n e.capacity -= d; g[e.to][e.rev].capacity += d;\n }\n }\n return total;\n }\n};\n \n \ntypedef complex<double> Point;\n \ndouble dot(Point a, Point b) {\n return (a * conj(b)).real();\n}\n \ndouble cross(Point a, Point b) {\n return (a * conj(b)).imag();\n}\n \nstruct Line {\n Point a, b;\n};\nstruct Circle {\n Point p;\n double r;\n};\n \nvoid tangentCP(const Circle &c, const Point &p, vector<Point> &res) {\n Point v = c.p - p;\n auto d = abs(v), l = sqrt(max(0., norm(v) - c.r * c.r));\n Point t = Point(l / d, c.r / d);\n res.push_back(v * t);\n if(l > 1e-9)\n res.push_back(v * conj(t));\n}\n \nPoint crosspoint(const Line &a, const Line &b) {\n auto A = cross(a.b - a.a, b.b - b.a), B = cross(a.b - a.a, a.b - b.a);\n return b.a + B / A * (b.b - b.a);\n}\n \nPoint projection(const Line &l, const Point &p) {\n double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + t * (l.a - l.b);\n}\n \nbool intersectSP(const Line &s, const Point &p) {\n return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) <= EPS;\n}\n \ndouble distanceSP(const Line &s, const Point &p) {\n const Point r = projection(s, p);\n if(intersectSP(s, r)) { return abs(r - p); }\n return min(abs(s.a - p), abs(s.b - p));\n}\n \nint main() {\n int n;\n while(~scanf(\"%d\", &n)) {\n Circle circles[2];\n rep(k, 2) {\n double x, y, r;\n scanf(\"%lf%lf%lf\", &x, &y, &r);\n circles[k] = { {x, y}, r };\n }\n vector<Point> points(n * 2);\n rep(i, n * 2) {\n double x, y;\n scanf(\"%lf%lf\", &x, &y);\n points[i] = { x,y };\n }\n vector<vector<Point>> tangents(n * 2);\n rep(i, n * 2) {\n rep(k, 2)\n tangentCP(circles[k], points[i], tangents[i]);\n }\n vector<Point> vsA, vsB;\n int src = n * 2, dst = src + 1;\n MinimumCostMaximumFlow mcmf;\n mcmf.init(dst + 1);\n rep(i, n) {\n mcmf.add(src, i, 1);\n mcmf.add(n + i, dst, 1);\n }\n rep(i, n) reu(j, n, n * 2) {\n vsA = tangents[i];\n vsB = tangents[j];\n vsA.push_back(points[j] - points[i]);\n vsB.push_back(points[i] - points[j]);\n double dist = 1e99;\n for(auto v : vsA) for(auto w : vsB) {\n Point x =\n abs(cross(v, w)) <= EPS ?\n (points[i] + points[j]) * .5 :\n crosspoint(Line{ points[i], points[i] + v }, Line{ points[j], points[j] + w });\n Line s{ points[i], x }, t{ points[j], x };\n bool ok = true;\n for(const Circle &c : circles) {\n ok &= distanceSP(s, c.p) >= c.r - EPS;\n ok &= distanceSP(t, c.p) >= c.r - EPS;\n }\n if(ok)\n amin(dist, abs(s.b - s.a) + abs(t.b - t.a));\n }\n if(dist < 1e99)\n mcmf.add(i, j, 1, dist);\n }\n auto fp = mcmf.minimumCostMaximumFlow(src, dst, n);\n if(fp.second != n) {\n puts(\"Impossible\");\n } else {\n printf(\"%.10f\\n\", fp.first);\n }\n }\n return 0;\n}", "accuracy": 0.21875, "time_ms": 80, "memory_kb": 3804, "score_of_the_acc": -0.4053, "final_rank": 12 }, { "submission_id": "aoj_1598_3668985", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))\n#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))\nstatic const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;\ntypedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;\ntemplate<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }\ntemplate<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }\n\nconst double EPS = 1e-9;\n\nconst double InfCost = 1e99;\nstruct MinimumCostMaximumFlow {\n\ttypedef int Index; typedef int Flow; typedef double Cost;\n\tstatic const Flow InfCapacity = INF;\n\tstruct Edge {\n\t\tIndex to; Index rev;\n\t\tFlow capacity; Cost cost;\n\t};\n\tvector<vector<Edge> > g;\n\tvoid init(Index n) { g.assign(n, vector<Edge>()); }\n\tvoid add(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) {\n\t\tEdge e, f; e.to = j, f.to = i; e.capacity = capacity, f.capacity = 0; e.cost = cost, f.cost = -cost;\n\t\tg[i].push_back(e); g[j].push_back(f);\n\t\tg[i].back().rev = (Index)g[j].size() - 1; g[j].back().rev = (Index)g[i].size() - 1;\n\t}\n\tvoid addB(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) {\n\t\tadd(i, j, capacity, cost);\n\t\tadd(j, i, capacity, cost);\n\t}\n\tpair<Cost, Flow> minimumCostMaximumFlow(Index s, Index t, Flow f = InfCapacity, bool bellmanFord = false) {\n\t\tint n = g.size();\n\t\tvector<Cost> dist(n); vector<Index> prev(n); vector<Index> prevEdge(n);\n\t\tpair<Cost, Flow> total = make_pair(0, 0);\n\t\tvector<Cost> potential(n);\n\t\twhile(f > 0) {\n\t\t\tfill(dist.begin(), dist.end(), InfCost);\n\t\t\tif(bellmanFord || total.second == 0) {\n\t\t\t\tdist[s] = 0;\n\t\t\t\trep(k, n) {\n\t\t\t\t\tbool update = false;\n\t\t\t\t\trep(i, n) if(dist[i] != InfCost)\n\t\t\t\t\t\tfor(Index ei = 0; ei < (Index)g[i].size(); ei ++) {\n\t\t\t\t\t\t\tconst Edge &e = g[i][ei];\n\t\t\t\t\t\t\tif(e.capacity <= 0) continue;\n\t\t\t\t\t\t\tIndex j = e.to; Cost d = dist[i] + e.cost;\n\t\t\t\t\t\t\tif(dist[j] > d + EPS) {\t//?????????\n\t\t\t\t\t\t\t\tdist[j] = d; prev[j] = i; prevEdge[j] = ei;\n\t\t\t\t\t\t\t\tupdate = true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\tif(!update) break;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tvector<bool> vis(n);\n\t\t\t\tpriority_queue<pair<Cost, Index> > q;\n\t\t\t\tq.push(make_pair(-0, s)); dist[s] = 0;\n\t\t\t\twhile(!q.empty()) {\n\t\t\t\t\tIndex i = q.top().second; q.pop();\n\t\t\t\t\tif(vis[i]) continue;\n\t\t\t\t\tvis[i] = true;\n\t\t\t\t\tfor(Index ei = 0; ei < (Index)g[i].size(); ei ++) {\n\t\t\t\t\t\tconst Edge &e = g[i][ei];\n\t\t\t\t\t\tif(e.capacity <= 0) continue;\n\t\t\t\t\t\tIndex j = e.to; Cost d = dist[i] + e.cost + potential[i] - potential[j];\n\t\t\t\t\t\tif(d < dist[i])\td = dist[i];\t//?????????\n\t\t\t\t\t\tif(dist[j] > d) {\n\t\t\t\t\t\t\tdist[j] = d; prev[j] = i; prevEdge[j] = ei;\n\t\t\t\t\t\t\tq.push(make_pair(-d, j));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(dist[t] == InfCost) break;\n\t\t\tif(!bellmanFord) for(Index i = 0; i < n; i ++) potential[i] += dist[i];\n\t\t\tFlow d = f; Cost distt = 0;\n\t\t\tfor(Index v = t; v != s; ) {\n\t\t\t\tIndex u = prev[v]; const Edge &e = g[u][prevEdge[v]];\n\t\t\t\td = min(d, e.capacity); distt += e.cost; v = u;\n\t\t\t}\n\t\t\tf -= d; total.first += d * distt; total.second += d;\n\t\t\tfor(Index v = t; v != s; v = prev[v]) {\n\t\t\t\tEdge &e = g[prev[v]][prevEdge[v]];\n\t\t\t\te.capacity -= d; g[e.to][e.rev].capacity += d;\n\t\t\t}\n\t\t}\n\t\treturn total;\n\t}\n};\n\n\ntypedef complex<double> Point;\n\ndouble dot(Point a, Point b) {\n\treturn (a * conj(b)).real();\n}\n\ndouble cross(Point a, Point b) {\n\treturn (a * conj(b)).imag();\n}\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p;\n\tdouble r;\n};\n\nvoid tangentCP(const Circle &c, const Point &p, vector<Point> &res) {\n\tPoint v = c.p - p;\n\tauto d = abs(v), l = sqrt(max(0., norm(v) - c.r * c.r));\n\tPoint t = Point(l / d, c.r / d);\n\tres.push_back(v * t);\n\tif(l > 1e-9)\n\t\tres.push_back(v * conj(t));\n}\n\nPoint crosspoint(const Line &a, const Line &b) {\n\tauto A = cross(a.b - a.a, b.b - b.a), B = cross(a.b - a.a, a.b - b.a);\n\treturn b.a + B / A * (b.b - b.a);\n}\n\nPoint projection(const Line &l, const Point &p) {\n\tdouble t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n\nbool intersectSP(const Line &s, const Point &p) {\n\treturn abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) <= EPS;\n}\n\ndouble distanceSP(const Line &s, const Point &p) {\n\tconst Point r = projection(s, p);\n\tif(intersectSP(s, r)) { return abs(r - p); }\n\treturn min(abs(s.a - p), abs(s.b - p));\n}\n\nint main() {\n\tint n;\n\twhile(~scanf(\"%d\", &n)) {\n\t\tCircle circles[2];\n\t\trep(k, 2) {\n\t\t\tdouble x, y, r;\n\t\t\tscanf(\"%lf%lf%lf\", &x, &y, &r);\n\t\t\tcircles[k] = { {x, y}, r };\n\t\t}\n\t\tvector<Point> points(n * 2);\n\t\trep(i, n * 2) {\n\t\t\tdouble x, y;\n\t\t\tscanf(\"%lf%lf\", &x, &y);\n\t\t\tpoints[i] = { x,y };\n\t\t}\n\t\tvector<vector<Point>> tangents(n * 2);\n\t\trep(i, n * 2) {\n\t\t\trep(k, 2)\n\t\t\t\ttangentCP(circles[k], points[i], tangents[i]);\n\t\t}\n\t\tvector<Point> vsA, vsB;\n\t\tint src = n * 2, dst = src + 1;\n\t\tMinimumCostMaximumFlow mcmf;\n\t\tmcmf.init(dst + 1);\n\t\trep(i, n) {\n\t\t\tmcmf.add(src, i, 1);\n\t\t\tmcmf.add(n + i, dst, 1);\n\t\t}\n\t\trep(i, n) reu(j, n, n * 2) {\n\t\t\tvsA = tangents[i];\n\t\t\tvsB = tangents[j];\n\t\t\tvsA.push_back(points[j] - points[i]);\n\t\t\tvsB.push_back(points[i] - points[j]);\n\t\t\tdouble dist = 1e99;\n\t\t\tfor(auto v : vsA) for(auto w : vsB) {\n\t\t\t\tPoint x =\n\t\t\t\t\tabs(cross(v, w)) <= EPS ?\n\t\t\t\t\t(points[i] + points[j]) * .5 :\n\t\t\t\t\tcrosspoint(Line{ points[i], points[i] + v }, Line{ points[j], points[j] + w });\n\t\t\t\tLine s{ points[i], x }, t{ points[j], x };\n\t\t\t\tbool ok = true;\n\t\t\t\tfor(const Circle &c : circles) {\n\t\t\t\t\tok &= distanceSP(s, c.p) >= c.r - EPS;\n\t\t\t\t\tok &= distanceSP(t, c.p) >= c.r - EPS;\n\t\t\t\t}\n\t\t\t\tif(ok)\n\t\t\t\t\tamin(dist, abs(s.b - s.a) + abs(t.b - t.a));\n\t\t\t}\n\t\t\tif(dist < 1e99)\n\t\t\t\tmcmf.add(i, j, 1, dist);\n\t\t}\n\t\tauto fp = mcmf.minimumCostMaximumFlow(src, dst, n);\n\t\tif(fp.second != n) {\n\t\t\tputs(\"Impossible\");\n\t\t} else {\n\t\t\tprintf(\"%.10f\\n\", fp.first);\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 80, "memory_kb": 3752, "score_of_the_acc": -0.3901, "final_rank": 11 }, { "submission_id": "aoj_1598_3668973", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-10;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nint par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size()-1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, -1);\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = id;\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur];\n\t\tif (p < 0)return -1;\n\t\trep(j, G[p].size()) {\n\t\t\tif (G[p][j].cap > 0 && G[p][j].to == cur && dist[p] + G[p][j].cost == dist[cur]) {\n\t\t\t\tG[p][j].cap--;\n\t\t\t\tG[cur][G[p][j].rev].cap++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s,c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { {x,y},r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n,t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2,r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1||ans>10000+eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\t\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 120, "memory_kb": 6804, "score_of_the_acc": -1.5078, "final_rank": 13 }, { "submission_id": "aoj_1598_3668970", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-10;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nint par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size()-1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, -1);\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = id;\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur];\n\t\tif (p < 0)return -1;\n\t\trep(j, G[p].size()) {\n\t\t\tif (G[p][j].cap > 0 && G[p][j].to == cur && dist[p] + G[p][j].cost == dist[cur]) {\n\t\t\t\tG[p][j].cap--;\n\t\t\t\tG[cur][G[p][j].rev].cap++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s,c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { {x,y},r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n,t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2,r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1||ans>10000+eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\t\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 120, "memory_kb": 6908, "score_of_the_acc": -1.5383, "final_rank": 18 }, { "submission_id": "aoj_1598_3668947", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-8;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nP par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, P{ -1,-1 });\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = { id,j };\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur].first,j=par[cur].second;\n\t\tif (p < 0)return -1;\n\t\tedge &e = G[p][j];\n\t\te.cap--;\n\t\tG[e.to][e.rev].cap++;\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s, c.p);\n\treturn dist < c.r - eps;\n}\n\n\nvoid solve() {\n\tint n; cin >> n;\n\tvector<Circle> c(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { { x,y },r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n, t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2, r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod||cost>10000+eps)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (10000 < ans&&ans < 10000 + eps)ans = 10000;\n\tif (ans == -1 || ans>10000 + eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 120, "memory_kb": 6872, "score_of_the_acc": -1.5278, "final_rank": 16 }, { "submission_id": "aoj_1598_3668935", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-8;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nP par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, P{ -1,-1 });\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = { id,j };\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur].first,j=par[cur].second;\n\t\tif (p < 0)return -1;\n\t\tedge &e = G[p][j];\n\t\te.cap--;\n\t\tG[e.to][e.rev].cap++;\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s, c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { { x,y },r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n, t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2, r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1 || ans>10000 + eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 120, "memory_kb": 6928, "score_of_the_acc": -1.5442, "final_rank": 19 }, { "submission_id": "aoj_1598_3668892", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-8;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nP par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, P{ -1,-1 });\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = { id,j };\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur].first,j=par[cur].second;\n\t\tif (p < 0)return -1;\n\t\tedge &e = G[p][j];\n\t\te.cap--;\n\t\tG[e.to][e.rev].cap++;\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s, c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { { x,y },r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n, t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2, r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1 || ans>10000 + eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 120, "memory_kb": 6840, "score_of_the_acc": -1.5184, "final_rank": 14 }, { "submission_id": "aoj_1598_3668847", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-6;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nint par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, -1);\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = id;\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur];\n\t\tif (p < 0)return -1;\n\t\trep(j, G[p].size()) {\n\t\t\tif (G[p][j].cap > 0 && G[p][j].to == cur && eq(dist[p] + G[p][j].cost,dist[cur])) {\n\t\t\t\tG[p][j].cap--;\n\t\t\t\tG[cur][G[p][j].rev].cap++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s, c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { { x,y },r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n, t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2, r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1 || ans>10000 + eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 120, "memory_kb": 6892, "score_of_the_acc": -1.5336, "final_rank": 17 }, { "submission_id": "aoj_1598_3668824", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-5;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nint par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size() - 1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, -1);\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = id;\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur];\n\t\tif (p < 0)return -1;\n\t\trep(j, G[p].size()) {\n\t\t\tif (G[p][j].cap > 0 && G[p][j].to == cur && dist[p] + G[p][j].cost == dist[cur]) {\n\t\t\t\tG[p][j].cap--;\n\t\t\t\tG[cur][G[p][j].rev].cap++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s, c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { { x,y },r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n, t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2, r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1 || ans>10000 + eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 120, "memory_kb": 6852, "score_of_the_acc": -1.5219, "final_rank": 15 }, { "submission_id": "aoj_1598_3668774", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 1000000007;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\ntypedef pair<ll, ll> LP;\ntypedef vector<int> vec;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define stop char nyaa;cin>>nyaa;\n#define per(i,n) for(int i=n-1;i>=0;i--)\n\n//geometry\ntypedef long double ld;\ntypedef complex<ld> Point;\nconst ld pi = acos(-1.0);\nconst ld eps = 1e-10;\n\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\nld dot(Point a, Point b) { return real(conj(a)*b); }\nld cross(Point a, Point b) { return imag(conj(a)*b); }\n\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\nbool isis_sp(Line s, Point p) {\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\nld dist_lp(Line l, Point p) {\n\treturn abs(p - proj(l, p));\n}\nld dist_sp(Line s, Point p) {\n\tPoint r = proj(s, p);\n\treturn isis_sp(s, r) ? abs(p - r) : min(abs(p - s.a), abs(p - s.b));\n}\n\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a;\n\tPoint tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\n\nvector<Line> tangent_cp(Circle c, Point p) {\n\tvector<Line> res;\n\tPoint v = c.p - p;\n\tld d = abs(v);\n\tld l = sqrt(norm(v) - c.r*c.r);\n\tif (isnan(l))return res;\n\tPoint v1 = v * Point(l / d, c.r / d);\n\tPoint v2 = v * Point(l / d, -c.r / d);\n\tres.push_back(Line{ p,p + v1 });\n\tif (l < eps)return res;\n\tres.push_back(Line{ p,p + v2 });\n\treturn res;\n}\n\nint max_n;\nconst int mn = 100000;\nstruct edge {\n\tint to, cap; ld cost; int rev;\n};\ntypedef pair<ld, int> speP;\nvector<edge> G[mn];\nint par[mn];\nld dist[mn];\nvoid add_edge(int from, int to, int cap, ld cost) {\n\tG[from].push_back({ to,cap,cost,(int)G[to].size() });\n\tG[to].push_back({ from,0,-cost,(int)G[from].size()-1 });\n\tmax_n = max({ max_n,from + 1,to + 1 });\n}\nld minimum_road(int s, int t) {\n\tfill(par, par + max_n, -1);\n\tfill(dist, dist + max_n, mod);\n\tdist[s] = 0;\n\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\tq.push({ 0,s });\n\twhile (!q.empty()) {\n\t\tspeP p = q.top(); q.pop();\n\t\tint id = p.second;\n\t\tif (id == t)continue;\n\t\tif (p.first > dist[id])continue;\n\t\trep(j, G[id].size()) {\n\t\t\tif (G[id][j].cap > 0) {\n\t\t\t\tint to = G[id][j].to;\n\t\t\t\tld nd = p.first + G[id][j].cost;\n\t\t\t\tif (nd < dist[to]) {\n\t\t\t\t\tdist[to] = nd;\n\t\t\t\t\tpar[to] = id;\n\t\t\t\t\tq.push({ dist[to],to });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tint cur = t;\n\twhile (cur != s) {\n\t\tint p = par[cur];\n\t\tif (p < 0)return -1;\n\t\trep(j, G[p].size()) {\n\t\t\tif (G[p][j].cap > 0 && G[p][j].to == cur && dist[p] + G[p][j].cost == dist[cur]) {\n\t\t\t\tG[p][j].cap--;\n\t\t\t\tG[cur][G[p][j].rev].cap++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcur = p;\n\t}\n\treturn dist[t];\n}\nld minimum_cost_flow(int s, int t, int k) {\n\tld ret = 0;\n\trep(i, k) {\n\t\tld z = minimum_road(s, t);\n\t\tif (z < 0)return -1;\n\t\tret += z;\n\t}\n\treturn ret;\n}\n\nbool isis_cs(Circle c, Line s) {\n\tld dist = dist_sp(s,c.p);\n\treturn dist < c.r - eps;\n}\n\nvector<Circle> c;\n\nvoid solve() {\n\tint n; cin >> n;\n\tc.resize(2);\n\trep(i, 2) {\n\t\tld x, y, r; cin >> x >> y >> r;\n\t\tc[i] = { {x,y},r };\n\t}\n\tvector<Point> a(n), b(n);\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\ta[i] = { x,y };\n\t}\n\trep(i, n) {\n\t\tld x, y; cin >> x >> y;\n\t\tb[i] = { x,y };\n\t}\n\tint s = 2 * n, t = 2 * n + 1;\n\trep(i, n) {\n\t\tadd_edge(s, i, 1, 0);\n\t\tadd_edge(i + n,t, 1, 0);\n\t}\n\tvector<vector<Line>> l(n), r(n);\n\trep(i, n) {\n\t\trep(j, 2) {\n\t\t\tvector<Line> u = tangent_cp(c[j], a[i]);\n\t\t\trep(k, u.size())l[i].push_back(u[k]);\n\t\t\tu = tangent_cp(c[j], b[i]);\n\t\t\trep(k, u.size())r[i].push_back(u[k]);\n\t\t}\n\t}\n\trep(i, n) {\n\t\trep(j, n) {\n\t\t\tPoint le = a[i], ri = b[j];\n\t\t\tld cost = mod;\n\t\t\t{\n\t\t\t\tLine l = { le,ri };\n\t\t\t\tbool f = true;\n\t\t\t\trep(k, 2) {\n\t\t\t\t\tif (isis_cs(c[k], l))f = false;\n\t\t\t\t}\n\t\t\t\tif (f) {\n\t\t\t\t\tcost = abs(ri - le);\n\t\t\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\trep(k1, l[i].size()) {\n\t\t\t\trep(k2,r[j].size()) {\n\t\t\t\t\tLine lle = l[i][k1];\n\t\t\t\t\tLine rri = r[j][k2];\n\t\t\t\t\tif (!isis_ll(lle, rri))continue;\n\t\t\t\t\tPoint cn = is_ll(lle, rri);\n\t\t\t\t\tLine sl = { le,cn };\n\t\t\t\t\tLine sr = { ri,cn };\n\t\t\t\t\tbool valid = true;\n\t\t\t\t\trep(k, 2) {\n\t\t\t\t\t\tif (isis_cs(c[k], sl) || isis_cs(c[k], sr)) {\n\t\t\t\t\t\t\tvalid = false; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (valid) {\n\t\t\t\t\t\tcost = min(cost, abs(cn - le) + abs(cn - ri));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cost == mod)continue;\n\t\t\tadd_edge(i, n + j, 1, cost);\n\t\t}\n\t}\n\tld ans = minimum_cost_flow(s, t, n);\n\tif (ans == -1||ans>10000+eps) {\n\t\tcout << \"Impossible\" << endl;\n\t}\n\telse {\n\t\tcout << ans << endl;\n\t}\n}\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\tsolve();\n\t//stop\n\t\treturn 0;\n}", "accuracy": 0.21875, "time_ms": 120, "memory_kb": 6928, "score_of_the_acc": -1.5442, "final_rank": 19 } ]
aoj_1604_cpp
Deadlock Detection In concurrent processing environments, a deadlock is an undesirable situation where two or more threads are mutually waiting for others to finish using some resources and cannot proceed further. Your task is to detect whether there is any possibility of deadlocks when multiple threads try to execute a given instruction sequence concurrently. The instruction sequence consists of characters ' u ' or digits from ' 0 ' to ' 9 ', and each of them represents one instruction. 10 threads are trying to execute the same single instruction sequence. Each thread starts its execution from the beginning of the sequence and continues in the given order, until all the instructions are executed. There are 10 shared resources called locks from L 0 to L 9 . A digit k is the instruction for acquiring the lock L k . After one of the threads acquires a lock L k , it is kept by the thread until it is released by the instruction ' u '. While a lock is kept, none of the threads, including one already acquired it, cannot newly acquire the same lock L k . Precisely speaking, the following steps are repeated until all threads finish. One thread that has not finished yet is chosen arbitrarily. The chosen thread tries to execute the next instruction that is not executed yet. If the next instruction is a digit k and the lock L k is not kept by any thread, the thread executes the instruction k and acquires L k . If the next instruction is a digit k and the lock L k is already kept by some thread, the instruction k is not executed. If the next instruction is ' u ', the instruction is executed and all the locks currently kept by the thread are released. After executing several steps, sometimes, it falls into the situation that the next instructions of all unfinished threads are for acquiring already kept locks. Once such a situation happens, no instruction will ever be executed no matter which thread is chosen. This situation is called a deadlock . There are instruction sequences for which threads can never reach a deadlock regardless of the execution order. Such instruction sequences are called safe . Otherwise, in other words, if there exists one or more execution orders that lead to a deadlock, the execution sequence is called unsafe . Your task is to write a program that tells whether the given instruction sequence is safe or unsafe. Input The input consists of at most 50 datasets, each in the following format. n s n is the length of the instruction sequence and s is a string representing the sequence. n is a positive integer not exceeding 10,000. Each character of s is either a digit (' 0 ' to ' 9 ') or ' u ', and s always ends with ' u '. The end of the input is indicated by a line with a zero. Output For each dataset, if the given instruction sequence is safe, then print "SAFE" in a line. If it is unsafe, then print "UNSAFE" in a line. Sample Input 11 01u12u0123u 6 01u10u 8 201u210u 9 01u12u20u 3 77u 12 9u8u845u954u 0 Output for the Sampl ...(truncated)
[ { "submission_id": "aoj_1604_10851564", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstring>\nusing namespace std;\n#define rep(i,n) for(int i = 0; i < n; i++)\nbool dp[1024][10];\nvoid solve(int n){\n string s;\n cin >> s;\n int state = 0;\n memset(dp,0,sizeof(dp));\n rep(i,n-2){\n if(s[i] == 'u'){\n state = 0;\n continue;\n }\n state |= 1<<(s[i]-'0');\n if(s[i+1] == 'u')continue;\n int nx = s[i+1] - '0';\n dp[state][nx] = true;\n if(state & (1<<nx)){\n cout << \"UNSAFE\" << endl;\n return;\n }\n }\n for(int i = 0; i < 1024; i++){\n for(int j = 0; j < 10; j++){\n if(!dp[i][j])continue;\n for(int k = 0; k < 1024; k++){\n if(i&k)continue;\n for(int l = 0; l < 10; l++){\n if(!dp[k][l])continue;\n bool f = i & (1<<l);\n bool g = k & (1<<j);\n if(f and g){\n cout << \"UNSAFE\" << endl;\n return;\n }\n else if(f){\n dp[i|k][j] = true;\n }\n else if(g){\n dp[i|k][l] = true;\n }\n }\n }\n }\n }\n cout << \"SAFE\" << endl;\n return;\n}\nint main(){\n int n;\n while(cin >> n and n > 0){\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3400, "score_of_the_acc": -0.0066, "final_rank": 1 }, { "submission_id": "aoj_1604_10673681", "code_snippet": "#include <bits/stdc++.h>\n#define fi first\n#define se second\n#define rep(i,s,n) for (int i = (s); i < (n); ++i)\n#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n#define len(x) (int)(x).size()\n#define dup(x,y) (((x)+(y)-1)/(y))\n#define pb push_back\n#define eb emplace_back\n#define Field(T) vector<vector<T>>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\ntemplate<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;\nusing P = pair<int,int>;\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) {\n return 1;\n }\n string s;\n cin >> s;\n int m = 10;\n vector<int> f(1<<m);\n int idx = 0;\n rep(i,0,n) {\n if (s[i] == 'u') {\n int b = 0;\n rep(j,idx,i) {\n if ((b >> (s[j]-'0'))&1) {\n cout << \"UNSAFE\" << endl;\n return 0;\n }\n f[b] |= (1<<(s[j]-'0'));\n b |= (1<<(s[j]-'0'));\n }\n idx = i+1;\n }\n }\n rep(t,0,1<<m) {\n if (__builtin_popcount(t) <= 1) continue; \n vector<int> dp(1<<m, -1);\n function<bool(int)> rec = [&](int bit) {\n if (dp[bit] != -1) return dp[bit];\n if (bit != 0 && bit != t && (f[bit]&t)) {\n return dp[bit] = 1;\n } else if (__builtin_popcount(bit) == 1) {\n return dp[bit] = 0;\n }\n for (int nbit = bit&(bit-1); nbit > 0; nbit = bit&(nbit-1)) {\n if (rec(nbit) && rec(bit-nbit)) {\n return dp[bit] = 1;\n }\n }\n return dp[bit] = 0;\n };\n if (rec(t)) {\n cout << \"UNSAFE\" << endl;\n return 0;\n }\n }\n cout << \"SAFE\" << endl;\n return 0;\n}\n\nint main() {\n while(!solve());\n return 0;\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3380, "score_of_the_acc": -0.0277, "final_rank": 2 }, { "submission_id": "aoj_1604_10670288", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 30;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n \nvoid solve(ll n){\n Str(s);\n vector<vector<ll>> gru;\n gru.push_back({});\n rep(i,n){\n if(isdigit(s[i])){\n gru.back().push_back(s[i]-'0');\n }else{\n gru.push_back({});\n }\n }\n vector<vector<ll>> tmpgru;\n rep(i,sz(gru)){\n if(sz(gru[i]) > 0)tmpgru.push_back(gru[i]);\n }\n swap(gru,tmpgru);\n ll siz= sz(gru);\n if(siz == 0){\n cout << \"SAFE\" << endl;\n return;\n } \n vector<unordered_set<ll>> dp(vector<unordered_set<ll>>(1<<10));\n dp[0].insert(0);\n rep(i,siz){\n vector<unordered_set<ll>> ndp(vector<unordered_set<ll>>(1<<10));\n rep(j,1 << 10){\n each(k,dp[j]){//使われてないやつ\n //選ばない\n if(j >> gru[i][0] & 1){\n ndp[j].insert(k);\n }else{\n ndp[j].insert(k|1<<gru[i][0]);\n }\n ll nj = j;\n ll nk = k;\n rep(l,sz(gru[i])-1){\n if(nj >> gru[i][l] & 1){\n break;//すでに取られてるから取れない\n }\n //置く\n nj |= 1 << gru[i][l];\n if(nk >> gru[i][l]&1){\n nk ^= 1 << gru[i][l];\n }\n if(nj >> gru[i][l+1] & 1){\n ndp[nj].insert(nk);\n }else{\n ndp[nj].insert(nk|1<< gru[i][l+1]);\n }\n }\n }\n }\n swap(dp,ndp);\n }\n rep(j,1 << 10){\n if(dp[j].contains(0)){\n cout << \"UNSAFE\" << endl;\n return ;\n }\n }\n \n cout << \"SAFE\" << endl;\n}\n\nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n);\n if(n == 0)break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 3320, "memory_kb": 4420, "score_of_the_acc": -0.9869, "final_rank": 16 }, { "submission_id": "aoj_1604_10648718", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\n#include <stdlib.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define all(x) (x).begin(), (x).end()\n//constexpr ll MOD = 1000000007;\nconstexpr ll MOD = 998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 1LL<<60;\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\nll gcd(ll a, ll b){\n if(a%b == 0){\n return b;\n }else{\n return gcd(b, a%b);\n }\n}\n\nll lcm(ll a, ll b){\n return a*b / gcd(a, b);\n}\n\nll powMod(ll x, ll n) {\n if (n == 0) return 1 % MOD;\n ll val = powMod(x, n / 2);\n val *= val;\n val %= MOD;\n if (n % 2 == 1) val *= x;\n return val % MOD;\n}\n\nint main() {\n while(true){\n ll n; cin >> n;\n if(n == 0) break;\n string s; cin >> s;\n vector<pll> pairs;\n {\n ll l = 0;\n ll f = 1;\n while(l < n){\n ll r = l+1;\n if(s[l] == 'u'){\n l++;\n continue;\n }\n while(r < n && s[r] != 'u') r++;\n // cout << l << \" \" << r << endl;\n ll num = 0;\n rep(i,l,r-1){\n num ^= (1<<(s[i]-'0'));\n pairs.push_back({num, s[i+1]-'0'});\n if(num >> (s[i+1]-'0') & 1){\n f = 0;\n break;\n }\n }\n l = r+1;\n } \n if(f == 0){\n cout << \"UNSAFE\" << endl;\n continue;\n }\n }\n vector<vector<ll>> dp(1<<10, vector<ll>(10,0));\n for(auto [x, y] : pairs) dp[x][y] = 1;\n ll f = 1;\n rep(i,0,1<<10)rep(j,0,10){\n if(dp[i][j] == 0) continue;\n for(auto [x,y]: pairs){\n if((x&i) == 0 && (i>>y&1) == 1) dp[i|x][j] = 1;\n if((x&i) == 0 && (x>>j&1) == 1) dp[i|x][y] = 1;\n } \n }\n rep(i,0,1<<10)rep(j,0,10){\n if(dp[i][j] == 0) continue;\n rep(k,i+1,1<<10)rep(l,0,10){\n if(dp[k][l] == 1 && (i&k) == 0 && (k>>j&1) && (i>>l&1)) f = 0;\n }\n }\n if(f == 0) cout << \"UNSAFE\" << endl;\n else cout << \"SAFE\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 3840, "score_of_the_acc": -0.2775, "final_rank": 10 }, { "submission_id": "aoj_1604_9446776", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n string S;\n cin >> S;\n bool memo[1024][10] = {};\n int Cur = 0;\n bool ANS = false;\n rep(i,0,N) {\n if (S[i] == 'u') Cur = 0;\n else {\n int Next = S[i] - '0';\n if ((Cur >> Next) & 1) {\n ANS = true;\n }\n memo[Cur][Next] = true;\n Cur |= (1<<Next);\n }\n }\n if (ANS) {\n cout << \"UNSAFE\" << endl;\n continue;\n }\n queue<pair<int,int>> Q;\n rep(i,0,1<<10) {\n rep(j,0,10) {\n if (memo[i][j]) Q.push({i,j});\n }\n }\n while(!Q.empty()) {\n pair<int,int> P = Q.front();\n Q.pop();\n int Comp = 1023-P.first;\n for (int NX = Comp; NX >= 0; NX--) {\n NX &= Comp;\n for (int NY = 0; NY < 10; NY++) {\n if (memo[NX][NY]) {\n if ((P.first >> NY) & 1 && !memo[P.first | NX][P.second]) {\n memo[P.first | NX][P.second] = true;\n Q.push({P.first | NX, P.second});\n }\n if ((NX >> P.second) & 1 && !memo[P.first | NX][NY]) {\n memo[P.first | NX][NY] = true;\n Q.push({P.first | NX, NY});\n }\n }\n }\n }\n }\n rep(LX,0,1024) {\n int Comp = 1023 - LX;\n for (int RX = Comp; RX >= 0; RX--) {\n RX &= Comp;\n rep(LY,0,10) {\n rep(RY,0,10) {\n if (memo[LX][LY] && memo[RX][RY] && (LX >> RY) & 1 && (RX >> LY) & 1) ANS = true;\n }\n }\n }\n }\n cout << (ANS ? \"UNSAFE\" : \"SAFE\") << endl;\n}\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3556, "score_of_the_acc": -0.0902, "final_rank": 6 }, { "submission_id": "aoj_1604_9343641", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\n\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n;cin >> n;\n if(!n)break;\n string s;cin >> s;\n int msk = 0;\n vector<vector<bool>> dp(1 << 10,vector<bool>(10,false));\n bool res = true;\n rep(i,n-1){\n if(s[i] == 'u')msk = 0;\n else{\n if(msk & (1 << (s[i]-'0'))){\n res = false;\n }else{\n if(msk){\n dp[msk][s[i]-'0'] = true;\n }\n msk |= (1 << (s[i]-'0'));\n }\n }\n }\n for(int bit = 1;bit < 1 << 10;bit++){\n rep(i,10)rep(j,10){\n int S = bit;\n while(S){\n int T = bit - S;\n if((T & (1 << i)) && dp[S][i] && dp[T][j]){\n if((S & (1 << j)))res = false;\n dp[bit][j] = true;\n }\n S = (S-1) & bit;\n }\n }\n }\n cout << (res ? \"SAFE\\n\" : \"UNSAFE\\n\");\n }\n\n\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3472, "score_of_the_acc": -0.0647, "final_rank": 3 }, { "submission_id": "aoj_1604_9316007", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q; return 1;\n }\n return 0;\n};\n\n\nvoid solve(ll N) {\n string S;\n cin >> S;\n\n ll p = 0;\n vector<set<ll>> DP(1024);\n rep(i, S.size()) {\n if (S[i] == 'u') {\n p = 0;\n }\n else {\n ll d = S[i] - '0';\n DP[p].insert(d);\n if (p & (1 << d)) {\n cout << \"UNSAFE\" << endl;\n return;\n }\n p += (1 << d);\n }\n }\n rep(bit, 1024) {\n for (ll b = bit; b >= 0; b = (b - 1) & (bit)) {\n ll a = bit - b;\n for (auto d : DP[a]) {\n if (b & (1 << d)) {\n for (auto e : DP[b]) {\n if (bit == 1 && e == 0) {\n cout << \"\";\n }\n DP[bit].insert(e);\n }\n }\n }\n if (b == 0)break;\n }\n\n }\n rep(bit, 1024) {\n for (ll b = bit; b >= 0; b = (b - 1) & (bit)) {\n ll a = bit - b;\n for (auto d : DP[a])for (auto e : DP[b]) {\n if (b & (1 << d))\n {\n if (a & (1 << e)) {\n cout << \"UNSAFE\" << endl;\n return;\n }\n }\n \n }\n if (b == 0)break;\n }\n }\n cout << \"SAFE\" << endl;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n ll N;\n while (cin >> N) {\n if (N == 0)return 0;\n solve(N);\n }\n\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3712, "score_of_the_acc": -0.0827, "final_rank": 5 }, { "submission_id": "aoj_1604_9249610", "code_snippet": "#include <bits/stdc++.h>\n\nbool solve() {\n int N;\n std::cin >> N;\n if (N == 0) return false;\n std::string S;\n std::cin >> S;\n std::vector<std::pair<int, int>> state(S.size());\n bool ans{};\n {\n int cur{};\n for (int i{} ; i + 1 < (int)S.size() ; i++) {\n if (S[i] == 'u') cur = 0; \n else {\n if (cur & (1 << (S[i] - '0'))) {\n ans = true;\n }\n cur |= (1 << (S[i] - '0'));\n }\n\n if (S[i + 1] == 'u') state[i] = { -1, -1 };\n else if (cur == 0) state[i] = { -1, -1 };\n else state[i] = { cur, S[i + 1] - '0' };\n }\n }\n std::vector<std::vector<int>> T(1 << 10);\n for (int i{} ; i < (int)S.size() ; i++) {\n if (state[i].first != -1) {\n T[state[i].first].push_back(i);\n }\n }\n std::vector vis(1 << 10, std::vector<bool>(S.size()));\n auto dfs{[&](auto dfs, int cur, int v) -> void {\n assert(state[v].first != -1);\n if (ans) return;\n vis[cur][v] = true;\n if (cur & (1 << state[v].second)) {\n ans = true;\n return;\n }\n int mask{((1 << 10) - 1) ^ cur};\n for (int bit{mask} ; ; bit = (bit - 1) & mask) {\n for (auto x : T[bit]) {\n if (state[x].first & (1 << state[v].second)) {\n assert((cur & state[x].first) == 0);\n int nextcur{cur | state[x].first};\n if (vis[nextcur][x]) continue;\n dfs(dfs, nextcur, x);\n }\n }\n if (bit == 0) break;\n }\n }};\n for (int i{} ; i < (int)S.size() ; i++) {\n if (state[i].first != -1) dfs(dfs, state[i].first, i);\n }\n std::cout << (ans ? \"UNSAFE\" : \"SAFE\") << '\\n';\n return true;\n}\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 370, "memory_kb": 4520, "score_of_the_acc": -0.2305, "final_rank": 9 }, { "submission_id": "aoj_1604_9196679", "code_snippet": "#include <bits/stdc++.h>\n\nint solve() {\n int n;\n std::cin >> n;\n if (n == 0) return 1;\n std::string s;\n std::cin >> s;\n\n std::vector<std::pair<unsigned, int>> ts;\n for (int l = 0, r = 0; l < n; l = r) {\n while (l < n && s[l] == 'u') l++;\n if (l == n) break;\n r = l;\n while (r < n && s[r] != 'u') r++;\n unsigned state = 0;\n for (int i = l; i < r; i++) {\n int last = s[i] - '0';\n ts.emplace_back(state, last);\n state |= 1 << (int)(s[i] - '0');\n }\n }\n\n std::set<std::pair<unsigned, int>> memo;\n auto dfs = [&](auto self, unsigned state, int i) -> bool {\n std::pair<unsigned, int> key = {state, i};\n if (memo.find(key) != memo.end()) return false;\n memo.insert(key);\n if (state & (1 << i)) return true;\n for (auto [mask, j]: ts) {\n if ((state & mask) != 0) continue;\n if (mask & (1 << i)) {\n if (self(self, state | mask, j)) return true;\n }\n }\n return false;\n };\n for (auto [state, last]: ts) {\n if (dfs(dfs, state, last)) {\n std::cout << \"UNSAFE\" << '\\n';\n return 0;\n }\n }\n std::cout << \"SAFE\" << '\\n';\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 3616, "score_of_the_acc": -0.1286, "final_rank": 7 }, { "submission_id": "aoj_1604_9129510", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(v) v.begin(),v.end()\n#define dbg(x) cerr << #x << \": \" << (x) << endl;\ntemplate<class F, class S>\nostream& operator<<(ostream& os, pair<F,S>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate<class Iter>\nvoid print(Iter beg, Iter end) {\n for (Iter itr = beg; itr != end; ++itr) {\n cerr << *itr << ' ';\n }\n cerr << '\\n';\n}\n\nbool solve() {\n int n;\n string s;\n cin >> n;\n if (n == 0) exit(0);\n cin >> s;\n for (int i = 0; i < n; ++i) if (s[i] != 'u') s[i] -= '0';\n\n int K = 10;\n int have = 0;\n vector dp(1<<K, vector(K, false));\n for (int i = 0; i < n; ++i) {\n if (have >> s[i] & 1) {\n return false;\n }\n dp[have][s[i]] = 1;\n if (s[i] == 'u') {\n have = 0;\n } else {\n have |= 1 << s[i];\n }\n }\n for (int a = 0; a < 1<<K; ++a) {\n for (int b = 0; b < K; ++b) {\n if (!dp[a][b]) continue;\n for (int c = 0; c < 1<<K; ++c) {\n for (int d = 0; d < K; ++d) {\n if (!dp[c][d]) continue;\n if (a & c) continue;\n\n if ((a >> d & 1) && (c >> b & 1)) {\n return false;\n }\n\n if (a >> d & 1) dp[a | c][b] = 1;\n if (c >> b & 1) dp[a | c][d] = 1;\n }\n }\n }\n }\n return true;\n}\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n while (true) {\n cout << (solve() ? \"SAFE\" : \"UNSAFE\") << '\\n';\n }\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 3356, "score_of_the_acc": -0.0769, "final_rank": 4 }, { "submission_id": "aoj_1604_9093873", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nbool is_end = false;\n\nbitset<(1<<10)> dp;\n\nvoid calc()\n{\n ll N; cin >> N;\n \n if (N == 0)\n {\n is_end = true;\n return;\n }\n \n string S; cin >> S;\n \n const uint siz = (1<<10);\n vector< bitset<siz> > locked(10);\n \n uint hoge = 0;\n for (int i = 0; i < N; ++i)\n {\n char c = S[i];\n \n if (c == 'u')\n {\n hoge = 0;\n }\n else\n {\n int n = c - '0';\n locked[n].set(hoge);\n hoge |= (1u<<n);\n }\n }\n \n bitset<siz> dp(0);\n bool safe = true;\n for (uint bit = 0; bit < siz; ++bit)\n {\n dp = 0;\n for (int i = 0; i < 10; ++i)\n {\n if (bit & (1<<i)) dp |= locked[i];\n }\n \n for (uint inbit = 0; inbit < siz; ++inbit)\n {\n if (dp[inbit]) continue;\n for (uint subbit = inbit; subbit > inbit / 2; subbit = inbit & (subbit - 1))\n {\n if (dp[subbit] && dp[inbit ^ subbit])\n {\n dp[inbit] = 1;\n break;\n }\n }\n }\n \n for (uint inbit = 0; inbit < siz; ++inbit)\n {\n if ((bit & inbit) == bit)\n {\n if (dp[inbit])\n {\n safe = false;\n break;\n }\n }\n }\n \n if (!safe) break;\n }\n \n \n cout << (safe ? \"SAFE\" : \"UNSAFE\") << endl;\n \n return;\n}\n\nint main()\n{\n while (!is_end)\n {\n calc();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 1150, "memory_kb": 3372, "score_of_the_acc": -0.2976, "final_rank": 12 }, { "submission_id": "aoj_1604_9093842", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nbool is_end = false;\n\nbitset<(1<<10)> dp;\n\nvoid calc()\n{\n ll N; cin >> N;\n \n if (N == 0)\n {\n is_end = true;\n return;\n }\n \n string S; cin >> S;\n \n const ll siz = (1<<10);\n vector< bitset<siz> > locked(10);\n \n ll hoge = 0;\n for (int i = 0; i < N; ++i)\n {\n char c = S[i];\n \n if (c == 'u')\n {\n hoge = 0;\n }\n else\n {\n int n = c - '0';\n locked[n].set(hoge);\n hoge |= (1LL<<n);\n }\n }\n \n bitset<siz> dp(0);\n bool safe = true;\n for (int bit = 0; bit < siz; ++bit)\n {\n dp = 0;\n for (int i = 0; i < 10; ++i)\n {\n if (bit & (1<<i)) dp |= locked[i];\n }\n \n for (int inbit = 0; inbit < siz; ++inbit)\n {\n if (dp[inbit]) continue;\n for (int subbit = inbit; subbit > 0; subbit = inbit & (subbit - 1))\n {\n if (dp[subbit] && dp[inbit ^ subbit]) dp[inbit] = 1;\n }\n }\n \n for (int inbit = 0; inbit < siz; ++inbit)\n {\n if ((bit & inbit) == bit)\n {\n if (dp[inbit]) safe = false;\n }\n }\n \n if (!safe) break;\n }\n \n \n cout << (safe ? \"SAFE\" : \"UNSAFE\") << endl;\n \n return;\n}\n\nint main()\n{\n while (!is_end)\n {\n calc();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 3160, "memory_kb": 3344, "score_of_the_acc": -0.8177, "final_rank": 15 }, { "submission_id": "aoj_1604_9075262", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(v) v.begin(),v.end()\n#define dbg(x) cerr << #x << \": \" << (x) << endl;\ntemplate<class F, class S>\nostream& operator<<(ostream& os, pair<F,S>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate<class Iter>\nvoid print(Iter beg, Iter end) {\n for (Iter itr = beg; itr != end; ++itr) {\n cerr << *itr << ' ';\n }\n cerr << '\\n';\n}\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n while (true) {\n int n;\n string s;\n cin >> n;\n if (n == 0) break;\n cin >> s;\n\n bool safe = true;\n int K = 10;\n vector dp(1<<K, vector(K, false));\n int mask = 0;\n for (int i = 0; i < n; ++i) {\n if (s[i] == 'u') {\n mask = 0;\n } else {\n s[i] -= '0';\n dp[mask][s[i]] = true;\n if (mask & 1<<s[i]) {\n safe = false;\n break;\n } else {\n mask |= 1 << s[i];\n }\n }\n }\n for (int a = 0; a < 1<<K; ++a) {\n for (int b = 0; b < K; ++b) {\n for (int c = 0; c < 1<<K; ++c) {\n for (int d = 0; d < K; ++d) {\n if (dp[a][b] && dp[c][d] && (a & c)==0 && (c & (1 << b))) {\n dp[a | c][d] = true;\n }\n if (dp[a][b] && dp[c][d] && (a & c)==0 && (a & (1 << d))) {\n dp[a | c][b] = true;\n }\n }\n }\n }\n }\n if (safe) {\n vector<pair<int,int>> state;\n for (int i = 0; i < 1<<K; ++i) {\n for (int j = 0; j < K; ++j) {\n if (dp[i][j]) state.emplace_back(i, j);\n }\n }\n for (auto [a,b] : state) {\n for (auto [c,d] : state) {\n if (a & c) continue;\n if ((a & (1 << d)) && (c & (1 << b))) {\n safe = false;\n break;\n }\n }\n }\n }\n cout << (safe ? \"SAFE\" : \"UNSAFE\") << '\\n';\n }\n}", "accuracy": 1, "time_ms": 3860, "memory_kb": 3596, "score_of_the_acc": -1.0299, "final_rank": 17 }, { "submission_id": "aoj_1604_8023530", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,s,t) for (int i = (int)(s); i < (int)(t); ++i)\n#define all(x) (x).begin(), (x).end()\n\ntemplate<typename T>\nvoid print(vector<T>& vs){\n for(auto x:vs)cout<<x<<\" \";\n cout<<endl;\n}\n\nbool dp[1<<10][1<<10];\n\nint main() {\n while(true){\n int n;\n cin>>n;\n if(n==0)break;\n string s;\n cin>>s;\n\n // S -> k\n // take S disjointly. if all k are in S then deadlock\n\n // dp[S][T]: locked S, next T\n\n vector<vector<int>> G(1<<10);\n int S=0;\n rep(i,0,n){\n if(s[i]!='u'){\n G[S].push_back(s[i] - '0');\n S|=(1<<(s[i]-'0'));\n }else{\n S=0;\n }\n }\n rep(S,0,1<<10){\n sort(all(G[S]));\n G[S].erase(unique(all(G[S])), G[S].end());\n }\n\n memset(dp, 0, sizeof(dp));\n\n dp[0][0]=1;\n rep(S,0,1<<10){\n int X=S;\n do{\n for(int k:G[X]){\n rep(T,0,1<<10){\n dp[S][T|(1<<k)] = dp[S][T|(1<<k)] || dp[S^X][T];\n }\n }\n X=(X-1)&S;\n }while(X!=S);\n }\n bool deadlock =false;\n rep(S,0,1<<10){\n rep(T,1,1<<10){\n if(dp[S][T] && ((S&T)==T)){\n deadlock=true;\n break;\n }\n }\n if(deadlock)break;\n }\n cout<<(deadlock?\"UNSAFE\":\"SAFE\")<<endl;\n }\n}", "accuracy": 1, "time_ms": 2370, "memory_kb": 4596, "score_of_the_acc": -0.7604, "final_rank": 14 }, { "submission_id": "aoj_1604_8023480", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\n#define rep(i,n) for(ll i=0;i<n;i++)\n#define all(A) A.begin(),A.end()\n\nint main() {\n\n ll N;\n while (cin >> N) {\n if (N == 0)return 0; string S;\n cin >> S;\n vvll OK(1024, vll(1024, 0));\n rep(bit, 1024) {\n ll dit = 0;\n bool say=0;\n rep(i, N) {\n if (S[i] == 'u') {\n dit = 0;\n say=0;\n }\n else if(!say){\n ll d = S[i] - '0';\n OK[bit][dit] |= (1 << d);\n if ((bit | dit) & (1 << d)){\n say=1;\n }\n dit += (1 << d);\n }\n }\n }\n vvb DP(1024, vb(1024, 0));\n rep(i, 1024)rep(k, 10)if (OK[0][i] & (1 << k))DP[i][1 << k] = 1;\n string AN = \"SAFE\";\n rep(bit, 1024) {\n rep(kit, 1024) {\n if (DP[bit][kit]) {\n if ((bit & kit) == kit) {\n AN = \"UN\" + AN;\n break;\n }\n else {\n ll zit = 1023 - bit;\n for (ll b = zit; b >= 0; b = (b - 1) & zit) {\n if (b == 1) {\n cout << \"\";\n }\n rep(k, 10) {\n if (OK[bit][b] & (1 << k)) {\n DP[bit|b][kit | (1 << k)] = 1;\n }\n }\n if (b == 0)break;\n }\n }\n }\n }\n if (AN[0] == 'U')break;\n }\n cout << AN << endl;\n }\n}", "accuracy": 1, "time_ms": 2990, "memory_kb": 11780, "score_of_the_acc": -1.7734, "final_rank": 20 }, { "submission_id": "aoj_1604_8023346", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\nvector<int>G[1<<10];\nbool dp[1<<10][1<<10];\nstring S;\nint main()\n{\n\tint N;\n\twhile(cin>>N,N)\n\t{\n\t\tcin>>S;\n\t\tfor(int i=0;i<1<<10;i++)G[i].clear();\n\t\tbool out=false;\n\t\tfor(int i=0;i<N;)\n\t\t{\n\t\t\tint j=i;\n\t\t\twhile(S[j]!='u')j++;\n\t\t\tint ex=0;\n\t\t\tfor(int k=i;k<j;k++)\n\t\t\t{\n\t\t\t\tint u=S[k]-'0';\n\t\t\t\tif(ex>>u&1)\n\t\t\t\t{\n\t\t\t\t\tout=true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tG[ex].push_back(u);\n\t\t\t\tex|=1<<u;\n\t\t\t}\n\t\t\ti=j+1;\n\t\t}\n\t\tif(out)\n\t\t{\n\t\t\tcout<<\"UNSAFE\"<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int i=0;i<1<<10;i++)for(int j=0;j<1<<10;j++)dp[i][j]=false;\n\t\tdp[0][0]=true;\n\t\tfor(int i=0;!out&&i<1<<10;i++)\n\t\t{\n\t\t\tint r=(1<<10)-1^i;\n\t\t\tfor(int j=r;!out&&j>=0;j--)\n\t\t\t{\n\t\t\t\tj&=r;\n\t\t\t\tint nj=j^r;\n\t\t\t\tif(!dp[i][nj])continue;\n\t\t\t\tfor(int k=r;!out&&k>=0;k--)\n\t\t\t\t{\n\t\t\t\t\tk&=r;\n\t\t\t\t\tfor(int w:G[k])\n\t\t\t\t\t{\n\t\t\t\t\t\tint ni=i|k;\n\t\t\t\t\t\tint nnj=(nj|1<<w)&~ni;\n\t\t\t\t\t\tif(nnj==0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tout=true;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdp[ni][nnj]=true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<(!out?\"SAFE\":\"UNSAFE\")<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 460, "memory_kb": 4752, "score_of_the_acc": -0.2815, "final_rank": 11 }, { "submission_id": "aoj_1604_8017829", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <climits>\n#include <iostream>\n#include <numeric>\n#include <utility>\n#include <vector>\n\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int, int>;\n#define For(i, a, b) for (int i = int(a); i < int(b); ++i)\n#define rep(i, n) For(i, 0, n)\n#define sz(v) int(v.size())\n#define fora(s, v) for (auto &s : v)\n#define all(v) v.begin(), v.end()\n\ntemplate <class T>\nusing V = vector<T>;\ntemplate <class T>\nusing VV = V<V<T>>;\n\ntemplate <class T>\nbool chmax(T &a, const T b) {\n return a < b ? a = b, 1 : 0;\n}\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n string s;\n cin >> s;\n\n bool ans = true;\n\n VV<bool> f(1 << 10, V<bool>(10, false));\n {\n int S = 0;\n rep(i, n) {\n if (s[i] == 'u') {\n S = 0;\n } else {\n int k = s[i] - '0';\n\n if (S >> k & 1) ans = false;\n\n f[S][k] = true;\n S |= 1 << k;\n }\n }\n }\n\n rep(S, 1 << 10) rep(i, 10) {\n if (!f[S][i]) continue;\n rep(T, 1 << 10) rep(j, 10) {\n if (!f[T][j]) continue;\n\n auto c1 = ((S & T) == 0) && (T >> i & 1);\n auto c2 = ((S & T) == 0) && (S >> j & 1);\n if (c1 && c2) {\n ans = false;\n }\n if (c1) {\n f[S | T][j] = true;\n }\n if (c2) {\n f[S | T][i] = true;\n }\n }\n }\n\n puts(ans ? \"SAFE\" : \"UNSAFE\");\n }\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 3464, "score_of_the_acc": -0.2278, "final_rank": 8 }, { "submission_id": "aoj_1604_8012288", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cassert>\nusing namespace std;\nusing ll = long long;\n#define chmin(a,b) a = min(a,b)\nint n;\nvoid solve(){\n string s;\n cin>>s;\n int ni = 0;\n int now = 0;\n vector<vector<int>> can(10,vector<int>(1<<10,0));\n while(ni<n){\n if(s[ni]=='u'){\n now = 0;\n ni++;\n continue;\n }\n int nj = s[ni] - '0';\n now |= 1<<nj;\n if(s[ni+1]=='u') {\n ni++;\n continue;\n }\n int nk = s[ni+1] - '0';\n can[nk][now]=1;\n ni++;\n }\n vector<vector<int>> dp(1<<10,vector<int>(1<<10,1e9));\n dp[0][0] = 0;\n int mask = (1<<10) - 1;\n for(int i = 0;i<1<<10;i++){\n for(int j = 0;j<1<<10;j++){\n if(dp[i][j]>10) continue;\n int now = mask ^ j;\n for(int k = mask;k>=0;k--){\n k &= now;\n for(int l = 0;l<10;l++){\n if(can[l][k]==0) continue;\n chmin(dp[i|1<<l][j|k],dp[i][j]+1);\n }\n }\n }\n }\n for(int i = 1;i<1<<10;i++){\n for(int j = 0;j<1<<10;j++){\n if((i&j)!=i) continue;\n if(dp[i][j]<=10){\n cout<<\"UNSAFE\\n\";\n return ;\n }\n }\n }\n cout<<\"SAFE\\n\";\n}\n \nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n while(true){\n cin>>n;\n if(n==0) break;\n solve();\n\n }\n}", "accuracy": 1, "time_ms": 2790, "memory_kb": 7284, "score_of_the_acc": -1.1884, "final_rank": 19 }, { "submission_id": "aoj_1604_8012231", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cassert>\nusing namespace std;\nusing ll = long long;\n#define chmin(a,b) a = min(a,b)\nint n;\nvoid solve(){\n string s;\n cin>>s;\n int ni = 0;\n int now = 0;\n vector<vector<int>> can(10,vector<int>(1<<10,0));\n while(ni<n){\n if(s[ni]=='u'){\n now = 0;\n ni++;\n continue;\n }\n int nj = s[ni] - '0';\n now |= 1<<nj;\n if(s[ni+1]=='u') {\n ni++;\n continue;\n }\n int nk = s[ni+1] - '0';\n can[nk][now]=1;\n ni++;\n }\n vector<vector<int>> dp(1<<10,vector<int>(1<<10,1e9));\n dp[0][0] = 0;\n int mask = (1<<10) - 1;\n for(int i = 0;i<1<<10;i++){\n for(int j = 0;j<1<<10;j++){\n if(dp[i][j]>10) continue;\n int now = mask ^ j;\n for(int k = mask;k>=0;k--){\n k &= now;\n for(int l = 0;l<10;l++){\n if(can[l][k]==0) continue;\n chmin(dp[i|1<<l][j|k],dp[i][j]+1);\n }\n }\n }\n }\n for(int i = 1;i<1<<10;i++){\n for(int j = 0;j<1<<10;j++){\n if((i&j)!=i) continue;\n if(dp[i][j]<=10){\n cout<<\"UNSAFE\\n\";\n return ;\n }\n }\n }\n cout<<\"SAFE\\n\";\n}\n \nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n while(true){\n cin>>n;\n if(n==0) break;\n solve();\n\n }\n}", "accuracy": 1, "time_ms": 2780, "memory_kb": 7248, "score_of_the_acc": -1.1815, "final_rank": 18 }, { "submission_id": "aoj_1604_7971608", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n#include <bits/stdc++.h>\n// #include <atcoder/all>\n// using namespace atcoder;\n// using mint = modint1000000007;\n// using mint = modint998244353;\n// using vmint = vc<mint>;using vvmint = vc<vmint>;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define srep(i, s, n) for (int i = s; i < n; i++)\n#define drep(i, n) for (int i = n; i >= 0; i--)\n#define dsrep(i, s, n) for (int i = s; i >= n; i--)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\" ) << endl\n#define Yes(n) cout << ((n) ? \"Yes\" : \"No\" ) << endl\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst double pi = 3.141592653589793;\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const int mod = 1000000007;\nconst int mod = 998244353;\ninline bool inside(long long y, long long x, long long H, long long W) {return 0 <= y and y < H and 0 <= x and x < W; }\n\nstruct phash{\n inline size_t operator()(const pair<int,int> & p) const{\n const auto h1 = hash<int>()(p.first);\n const auto h2 = hash<int>()(p.second);\n return h1 ^ (h2 << 1);\n }\n};\n\nvoid solve(int &N){\n string S; cin >> S;\n vv<bool> possible((1 << 10), vc<bool>(10, false));\n ll now = 0;\n rep(i, N){\n if (S[i] == 'u') now = 0;\n else{\n if (now & (1 << (S[i] - '0'))){\n cout << \"UNSAFE\" << endl;\n return;\n }\n if (now != 0){\n possible[now][S[i] - '0'] = true;\n }\n now |= (1 << (S[i] - '0'));\n }\n }\n\n rep(a1, 1 << 10) rep(b1, 10) rep(a2, 1 << 10) rep(b2, 10){\n if (possible[a1][b1] && possible[a2][b2]){\n if ((a1 & a2) == 0 && (a1 >> b2) & 1 && (a2 >> b1) & 1){\n cout << \"UNSAFE\" << endl;\n return;\n }\n if ((a1 & a2) == 0 && (a1 >> b2) & 1){\n possible[a1 | a2][b1] = true;\n }\n if ((a1 & a2) == 0 && (a2 >> b1) & 1){\n possible[a1 | a2][b2] = true;\n }\n }\n }\n cout << \"SAFE\" << endl;\n}\n\nint main(){\n while (true){\n int N; cin >> N;\n if (N == 0) return 0;\n solve(N);\n }\n}", "accuracy": 1, "time_ms": 1940, "memory_kb": 3460, "score_of_the_acc": -0.5138, "final_rank": 13 } ]
aoj_1607_cpp
Development of Small Flying Robots You are developing small flying robots in your laboratory. The laboratory is a box-shaped building with K levels, each numbered 1 through K from bottom to top. The floors of all levels are square-shaped with their edges precisely aligned east-west and north-south. Each floor is divided into R × R cells. We denote the cell on the z -th level in the x -th column from the west and the y -th row from the south as ( x , y , z ). (Here, x and y are one-based.) For each x , y , and z ( z > 1), the cell ( x , y , z ) is located immediately above the cell ( x , y , z − 1). There are N robots flying in the laboratory, each numbered from 1 through N . Initially, the i -th robot is located at the cell ( x i , y i , z i ). By your effort so far, you successfully implemented the feature to move each flying robot to any place that you planned. As the next step, you want to implement a new feature that gathers all the robots in some single cell with the lowest energy consumption, based on their current locations and the surrounding environment. Floors of the level two and above have several holes. Holes are rectangular and their edges align with edges of the cells on the floors. There are M holes in the laboratory building, each numbered 1 through M . The j -th hole can be described by five integers u 1 j , v 1 j , u 2 j , v 2 j , and w j . The j -th hole extends over the cells ( x , y , w j ) where u 1 j ≤ x ≤ u 2 j and v 1 j ≤ y ≤ v 2 j . Possible movements of robots and energy consumption involved are as follows. You can move a robot from one cell to an adjacent cell, toward one of north, south, east, or west. The robot consumes its energy by 1 for this move. If there is a hole to go through immediately above, you can move the robot upward by a single level. The robot consumes its energy by 100 for this move. The robots never fall down even if there is a hole below. Note that you can move two or more robots to the same cell. Now, you want to gather all the flying robots at a single cell in the K -th level where there is no hole on the floor, with the least energy consumption. Compute and output the minimum total energy required by the robots. Input The input consists of at most 32 datasets, each in the following format. Every value in the input is an integer. N M K R x 1 y 1 z 1 ... x N y N z N u 11 v 11 u 21 v 21 w 1 ... u 1 M v 1 M u 2 M v 2 M w M N is the number of robots in the laboratory (1 ≤ N ≤ 100). M is the number of holes (1 ≤ M ≤ 50), K is the number of levels (2 ≤ K ≤ 10), and R is the number of cells in one row and also one column on a single floor (3 ≤ R ≤ 1,000,000). For each i , integers x i , y i , and z i represent the cell that the i -th robot initially located at (1 ≤ x i ≤ R , 1 ≤ y i ≤ R , 1 ≤ z i ≤ K ). Further, for each j , integers u 1 j , v 1 j , u 2 j , v 2 j , and w j describe the position and the extent of the j -th hole (1 ≤ u 1 j ≤ u 2 j ≤ R , 1 ≤ v 1 j ≤ v 2 j ≤ R , 2 ≤ w j ≤ K ). Th ...(truncated)
[ { "submission_id": "aoj_1607_9405300", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nint solve(int N) {\n int M = in(), K = in(), R = in();\n vector<tuple<int,int,int>> A(N);\n for(auto &[x, y, z] : A) x = in(), y = in(), z = in();\n\n vector<vector<tuple<int,int,int,int>>> B(K + 1);\n for(int _ : rep(M)) {\n int u1 = in(), v1 = in(), u2 = in(), v2 = in(), w = in();\n B[w].push_back({u1, v1, u2, v2});\n }\n\n vector<map<pair<int,int>, int>> C(N);\n for(int i : rep(N)) {\n map<pair<int,int>, int> &mp = C[i];\n auto [sx, sy, sz] = A[i];\n mp[{sx, sy}] = 0;\n\n for(int z = sz + 1; z <= K; z++) {\n map<pair<int,int>, int> nt;\n for(auto [p, d] : mp) {\n auto [x, y] = p;\n for(auto [u1, v1, u2, v2] : B[z]) {\n int nx = [&] {\n if(u1 <= x and x <= u2) return x;\n return abs(u1 - x) <= abs(u2 - x) ? u1 : u2;\n }();\n int ny = [&] {\n if(v1 <= y and y <= v2) return y;\n return abs(v1 - y) <= abs(v2 - y) ? v1 : v2;\n }();\n int nd = d + abs(x - nx) + abs(y - ny) + 100;\n if(nt.count({nx, ny})) chmin(nt[{nx, ny}], nd);\n else nt[{nx, ny}] = nd;\n }\n }\n mp = move(nt);\n }\n }\n\n vector<int> xs, ys;\n for(auto &mp : C) for(auto [p, d] : mp) {\n auto [x, y] = p;\n xs.push_back(x);\n ys.push_back(y);\n }\n for(auto [u1, v1, u2, v2] : B[K]) {\n xs.push_back(u1 - 1);\n xs.push_back(u2 + 1);\n ys.push_back(v1 - 1);\n ys.push_back(v2 + 1);\n }\n unique(xs);\n unique(ys);\n\n int ans = 2e9;\n for(int x : xs) for(int y : ys) {\n if(1 <= x and x <= R and 1 <= y and y <= R) {\n bool in_hall = [&] {\n for(auto [u1, v1, u2, v2] : B[K]) {\n if(u1 <= x and x <= u2 and v1 <= y and y <= v2) {\n return true;\n }\n }\n return false;\n }();\n if(not in_hall) {\n int score = 0;\n for(auto &mp : C) {\n int Min = 2e9;\n for(auto [p, d] : mp) {\n auto [cx, cy] = p;\n chmin(Min, d + abs(cx - x) + abs(cy - y));\n }\n score += Min;\n }\n chmin(ans, score);\n }\n }\n }\n return ans;\n}\n\nint main() {\n while(true) {\n int N = in();\n if(N == 0) return 0;\n print(solve(N));\n }\n}", "accuracy": 1, "time_ms": 1220, "memory_kb": 3944, "score_of_the_acc": -0.1075, "final_rank": 4 }, { "submission_id": "aoj_1607_9316264", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q; return 1;\n }\n return 0;\n};\ntemplate<class T>\nbool chmin(T& p, T q) {\n if (p > q) {\n p = q; return 1;\n }\n return 0;\n};\n\nbool is_contain(pair<pair<ll, ll>, pair<ll, ll>>& h, ll x, ll y) {\n if (h.first.first <= x && x<=h.second.first && h.second.second >= y && h.first.second <= y)return 1;\n return 0;\n}\n\n\nvoid solve(ll N) {\n ll M, K, R;\n cin >> M >> K >> R;\n vector<vector<pair<pair<ll, ll>, pair<ll, ll>>>> HALL(K);\n vll X(N), Y(N), Z(N);\n rep(i, N) {\n cin >> X[i] >> Y[i] >> Z[i];\n Z[i]--;\n }\n rep(i, M) {\n ll x, y, u, v, w;\n cin >> x >> y >> u >> v >> w;\n HALL[w - 2].push_back({ {x,y},{u,v} });\n }\n //最上階のhogehoge\n vector<map<pair<ll, ll>, ll>> D(N);\n rep(i, N) {\n map<pair<ll, ll>, ll> dist;\n dist[{X[i], Y[i]}] = 0;\n for (ll z = Z[i]; z < K - 1; z++) {\n map<pair<ll, ll>, ll> ndist;\n for (auto xyd : dist) {\n ll x = xyd.first.first;\n ll y = xyd.first.second;\n ll d = xyd.second;\n\n for (auto h : HALL[z]) {\n ll nx = x, ny = y;\n ll nd = d+100;\n if (is_contain(h, x, y)) {\n\n }\n else if (h.first.first <= x && x <= h.second.first) {\n if (h.first.second > y) {\n ny = h.first.second;\n }\n else {\n ny = h.second.second;\n }\n }\n else if (h.first.second <= y && y <= h.second.second) {\n if (h.first.first > x) {\n nx = h.first.first;\n }\n else {\n nx = h.second.first;\n }\n }\n else {\n if (h.first.first > x) {\n nx = h.first.first;\n }\n else {\n nx = h.second.first;\n }\n if (h.first.second > y) {\n ny = h.first.second;\n }\n else {\n ny = h.second.second;\n }\n }\n nd += abs(nx - x) + abs(ny - y);\n\n if (!ndist.count({ nx,ny }))ndist[{nx, ny}] = nd+1;\n chmin(ndist[{nx, ny}], nd);\n }\n }\n swap(dist, ndist);\n }\n // cout<<i<<endl;\n // for(auto dd:dist){\n // cout<<dd.first.first<<\" \"<<dd.first.second<<\" \"<<dd.second<<endl;\n // }\n // cout<<\"===================\\n\";\n D[i] = dist;\n }\n //ここまでで話は2次元に落ちた\n\n set<ll> BX,BY;\n for(auto H:HALL[K-2]){\n BX.insert(H.first.first-1);\n BX.insert(H.second.first+1);\n BY.insert(H.first.second-1);\n BY.insert(H.second.second+1);\n }\n rep(i,N)for(auto xy:D[i]){\n BX.insert(xy.first.first);\n BY.insert(xy.first.second);\n }\n\n ll an=1e18;\n for(auto x:BX)for(auto y:BY){\n if(x<1||y<1||x>R||y>R)continue;\n bool OK=1;\n for(auto h:HALL[K-2])if(is_contain(h,x,y))OK=0;\n if(!OK)continue;\n\n ll res=0;\n rep(i,N){\n ll fes=1e18;\n for(auto uvw:D[i]){\n ll u=uvw.first.first;\n ll v=uvw.first.second;\n ll w=uvw.second;\n chmin(fes,abs(u-x)+abs(v-y)+w);\n }\n res+=fes;\n }\n chmin(an,res);\n }\n \n cout<<an<<endl;\n \n\n \n\n // cout << \"MADA\" << endl;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n ll N;\n while (cin >> N) {\n if (N == 0)return 0;\n solve(N);\n }\n\n}", "accuracy": 1, "time_ms": 1270, "memory_kb": 3864, "score_of_the_acc": -0.1135, "final_rank": 5 }, { "submission_id": "aoj_1607_6021710", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\nint sum[305][305];\nint dist[305][305];\nbool isemp[305][305];\n\nint n;\nvoid solve() {\n\tint m, k, r; cin >> m >> k >> r;\n\tvector<int> x(n), y(n), z(n);\n\trep(i, n)cin >> x[i] >> y[i] >> z[i];\n\tvector<int> lx(m), ly(m), rx(m), ry(m);\n\tvector<int> w(m);\n\trep(i, m) {\n\t\tcin >> lx[i] >> ly[i] >> rx[i] >> ry[i] >> w[i];\n\t}\n\tvector<int> vx, vy;\n\trep(i, n) {\n\t\tvx.push_back(x[i]);\n\t\tvy.push_back(y[i]);\n\t}\n\trep(i, m) {\n\t\tvx.push_back(lx[i]);\n\t\tvy.push_back(ly[i]);\n\t\tvx.push_back(rx[i]);\n\t\tvy.push_back(ry[i]);\n\t\tif (w[i] == k) {\n\t\t\tif (lx[i] > 1)vx.push_back(lx[i] - 1);\n\t\t\tif (rx[i] + 1 <= r)vx.push_back(rx[i] + 1);\n\t\t\tif (ly[i] > 1)vy.push_back(ly[i] - 1);\n\t\t\tif (ry[i] + 1 <= r)vy.push_back(ry[i] + 1);\n\t\t}\n\t}\n\tsort(all(vx));\n\tvx.erase(unique(all(vx)), vx.end());\n\tsort(all(vy));\n\tvy.erase(unique(all(vy)), vy.end());\n\trep(i, vx.size())rep(j, vy.size()) {\n\t\tisemp[i][j] = false;\n\t\tsum[i][j] = 0;\n\t}\n\trep(i, m)if(w[i]==k) {\n\t\tint idxl = lower_bound(all(vx), lx[i]) - vx.begin();\n\t\tint idxr = lower_bound(all(vx), rx[i]) - vx.begin();\n\t\tint idyl = lower_bound(all(vy), ly[i]) - vy.begin();\n\t\tint idyr = lower_bound(all(vy), ry[i]) - vy.begin();\n\t\tRep1(x, idxl, idxr)Rep1(y, idyl, idyr) {\n\t\t\tisemp[x][y] = true;\n\t\t}\n\t}\n\tauto isin = [&](int x, int y) {\n\t\tif (x < 0 || x >= vx.size() || y<0 || y >= vy.size())return false;\n\t\treturn true;\n\t};\n\tvector<vector<int>> holes(k + 1);\n\trep(i, m) {\n\t\tholes[w[i]].push_back(i);\n\t}\n\tauto inseg = [&](int x, int l, int r) {\n\t\treturn min(r, max(l, x));\n\t};\n\tint ad = 0;\n\trep(id, n) {\n\t\tad += 100 * (k - z[id]);\n\t\tmap<P, int> mp;\n\t\tmp[{x[id], y[id]}] = 0;\n\t\tint cur = z[id];\n\t\twhile (cur < k) {\n\t\t\tmap<P, int> nex;\n\t\t\tfor (auto p : mp) {\n\t\t\t\tint cx = p.first.first;\n\t\t\t\tint cy = p.first.second;\n\t\t\t\tint cval = p.second;\n\t\t\t\tfor (int h : holes[cur + 1]) {\n\t\t\t\t\tint nx, ny;\n\t\t\t\t\tbool inx = false, iny = false;\n\t\t\t\t\tif (lx[h] <= cx && cx <= rx[h])inx = true;\n\t\t\t\t\tif (ly[h] <= cy && cy <= ry[h])iny = true;\n\t\t\t\t\tif (inx && iny) {\n\t\t\t\t\t\tnx = cx, ny = cy;\n\t\t\t\t\t}\n\t\t\t\t\telse if (inx) {\n\t\t\t\t\t\tnx = cx, ny = inseg(cy, ly[h], ry[h]);\n\t\t\t\t\t}\n\t\t\t\t\telse if (iny) {\n\t\t\t\t\t\tnx = inseg(cx, lx[h], rx[h]), ny = cy;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tnx = inseg(cx, lx[h], rx[h]), ny = inseg(cy, ly[h], ry[h]);\n\t\t\t\t\t}\n\t\t\t\t\tint nval = cval + abs(nx - cx) + abs(ny - cy);\n\t\t\t\t\tP np = { nx,ny };\n\t\t\t\t\tif (nex.find(np) == nex.end()) {\n\t\t\t\t\t\tnex[np] = nval;\n\t\t\t\t\t}\n\t\t\t\t\telse if (nex[np] > nval)nex[np] = nval;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcur++;\n\t\t\tswap(mp, nex);\n\t\t}\n\t\trep(i, vx.size())rep(j, vy.size()) {\n\t\t\tdist[i][j] = mod;\n\t\t}\n\t\tusing speP = pair<int, P>;\n\t\tpriority_queue<speP, vector<speP>, greater<speP>> q;\n\t\tfor (auto p : mp) {\n\t\t\tint cx = p.first.first;\n\t\t\tint cy = p.first.second;\n\t\t\tint cval = p.second;\n\t\t\tcx = lower_bound(all(vx), cx) - vx.begin();\n\t\t\tcy = lower_bound(all(vy), cy) - vy.begin();\n\t\t\tdist[cx][cy] = cval;\n\t\t\tq.push({ cval,{cx,cy} });\n\t\t}\n\t\twhile (!q.empty()) {\n\t\t\tspeP p = q.top(); q.pop();\n\t\t\tint cx = p.second.first;\n\t\t\tint cy = p.second.second;\n\t\t\tint cval = p.first;\n\t\t\trep(d, 4) {\n\t\t\t\tint nx = cx + dx[d];\n\t\t\t\tint ny = cy + dy[d];\n\t\t\t\tif (isin(nx, ny)) {\n\t\t\t\t\tint nval = cval + abs(vx[nx] - vx[cx]) + abs(vy[ny] - vy[cy]);\n\t\t\t\t\tif (nval < dist[nx][ny]) {\n\t\t\t\t\t\tdist[nx][ny] = nval;\n\t\t\t\t\t\tq.push({ nval,{nx,ny} });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\trep(i, vx.size())rep(j, vy.size()) {\n\t\t\tsum[i][j] += dist[i][j];\n\t\t}\n\t}\n\tint ans = -1;\n\trep(i, vx.size())rep(j, vy.size())if (!isemp[i][j]) {\n\t\tif (ans < 0 || sum[i][j] < ans)ans = sum[i][j];\n\t}\n\tans += ad;\n\t//cout << \"ans is \";\n\tcout << ans << \"\\n\";\n}\n\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\t//cout << fixed << setprecision(8);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\twhile (cin >> n, n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3530, "memory_kb": 4036, "score_of_the_acc": -0.4136, "final_rank": 7 }, { "submission_id": "aoj_1607_5050351", "code_snippet": "//copied from http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=4036948#1\n// my submission is http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=5050345#1\n\n\n#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1607\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\nstruct FastIO{\n FastIO(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n}fastio_beet;\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\ntemplate<typename V>\nV compress(V v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\nmap<char, int> dict(const string &v){\n return dict(vector<char>(v.begin(),v.end()));\n}\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned ABC036_C(){\n int n;\n cin>>n;\n vector<int> a(n);\n for(int i=0;i<n;i++) cin>>a[i];\n auto v=compress(a);\n auto m=dict(v);\n for(int i=0;i<n;i++) cout<<m[a[i]]<<endl;\n return 0;\n}\n/*\n verified on 2018/08/26\n https://abc036.contest.atcoder.jp/tasks/abc036_c\n*/\n\nsigned main(){\n ABC036_C();\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n#include<ext/pb_ds/tag_and_trait.hpp>\nusing namespace __gnu_pbds;\ntemplate <typename T>\nusing gtree = tree<T,null_type,less<T>,rb_tree_tag,\n tree_order_statistics_node_update>;\n// usage:\n// find_by_order(int k):\n// return the iterator of k-th smallest element (0-indexed)\n// order_of_key(T key): return the index of key in tree\n//END CUT HERE\n#ifndef call_from_test\nsigned ARC028_B(){\n using ll = long long;\n int n,k;\n cin>>n>>k;\n vector<int> xs(n);\n for(int i=0;i<n;i++) cin>>xs[i];\n map<int, int> mp;\n for(int i=0;i<n;i++) mp[xs[i]]=i+1;\n gtree<ll> G;\n for(int i=0;i<k-1;i++) G.insert(xs[i]);\n for(int i=k-1;i<n;i++){\n G.insert(xs[i]);\n auto key=*G.find_by_order(k-1);\n cout<<mp[key]<<endl;\n }\n return 0;\n}\n\n/*\n verified on 2019/12/09\n https://atcoder.jp/contests/arc028/tasks/arc028_2\n*/\n\nsigned ARC033_C(){\n int q;\n cin>>q;\n gtree<int> G;\n while(q--){\n int t,x;\n cin>>t>>x;\n if(t==1) G.insert(x);\n if(t==2){\n int k=*G.find_by_order(x-1);\n G.erase(k);\n cout<<k<<endl;\n }\n }\n return 0;\n}\n\n/*\n verified on 2019/12/09\n https://atcoder.jp/contests/arc033/tasks/arc033_3\n*/\n\nsigned main(){\n //ARC028_B();\n //ARC033_C();\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n#endif\n//BEGIN CUT HERE\n// prohibited to push an element less than popped one\n// Key: int or long long\ntemplate<typename K,typename V>\nstruct RadixHeap{\n static constexpr int bit = sizeof(K) * 8;\n array<vector< pair<K, V> >, bit> vs;\n\n int size;\n K last;\n RadixHeap():size(0),last(0){}\n\n bool empty() const{return size==0;}\n\n inline int getbit(int a){\n return a?bit-__builtin_clz(a):0;\n }\n\n inline int getbit(long long a){\n return a?bit-__builtin_clzll(a):0;\n }\n\n void emplace(K key,V val){\n size++;\n vs[getbit(key^last)].emplace_back(key,val);\n }\n\n pair<K, V> pop(){\n if(vs[0].empty()){\n int idx=1;\n while(vs[idx].empty()) idx++;\n last=min_element(vs[idx].begin(),vs[idx].end())->first;\n for(auto &p:vs[idx]) vs[getbit(p.first^last)].emplace_back(p);\n vs[idx].clear();\n }\n --size;\n auto res=vs[0].back();\n vs[0].pop_back();\n return res;\n }\n};\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nconst int BS = 8, BS2 = BS * 2;\nconst int msk = (1<<BS)-1;\nconst int MAX = 12 * (1<<BS) * (1<<BS);\nint dist[MAX];\nbool hole[MAX];\nlong long add[(1<<BS) * (1<<BS)];\nlong long dp[(1<<BS)][(1<<BS)];\n\nsigned main(){\n int n;\n while(cin>>n,n){\n int m,k,r;\n cin>>m>>k>>r;\n vector<int> x(n),y(n),z(n);\n for(int i=0;i<n;i++) cin>>x[i]>>y[i]>>z[i];\n vector<int> u1(m),v1(m),u2(m),v2(m),w(m);\n for(int i=0;i<m;i++) cin>>u1[i]>>v1[i]>>u2[i]>>v2[i]>>w[i];\n\n vector<int> vx(x),vy(y);\n for(int p=0;p<m;p++){\n vx.emplace_back(u1[p]);\n vx.emplace_back(u2[p]);\n vy.emplace_back(v1[p]);\n vy.emplace_back(v2[p]);\n if(w[p]!=k) continue;\n if(u1[p]-1>=1) vx.emplace_back(u1[p]-1);\n if(u2[p]+1<=r) vx.emplace_back(u2[p]+1);\n if(v1[p]-1>=1) vy.emplace_back(v1[p]-1);\n if(v2[p]+1<=r) vy.emplace_back(v2[p]+1);\n }\n\n vx=compress(vx);\n vy=compress(vy);\n auto mx=dict(vx);\n auto my=dict(vy);\n\n // kokokara y yuusen\n auto idx=[&](int cy,int cx,int f){return (f<<BS2)|(cy<<BS)|cx;};\n\n memset(hole,0,sizeof(hole));\n for(int p=0;p<m;p++)\n for(int i=my[v1[p]];i<=my[v2[p]];i++)\n for(int j=mx[u1[p]];j<=mx[u2[p]];j++)\n hole[idx(i,j,w[p])]=1;\n\n int sy=vy.size(),sx=vx.size();\n\n auto dijkstra=\n [&](int a)->void{\n vector<int> wy,wx;\n for(int p=0;p<m;p++){\n if(w[p]<=z[a]) continue;\n wy.emplace_back(v1[p]);\n wy.emplace_back(v2[p]);\n wx.emplace_back(u1[p]);\n wx.emplace_back(u2[p]);\n }\n wy.emplace_back(y[a]);\n wx.emplace_back(x[a]);\n wy=compress(wy);\n wx=compress(wx);\n auto zy=dict(wy);\n auto zx=dict(wx);\n int ty=zy.size(),tx=zx.size();\n\n vector<pair<int, int> > vs;\n {\n for(int i=0;i<MAX;i++) dist[i]=INT_MAX;\n RadixHeap<int, int> q;\n {\n int v=idx(zy[y[a]],zx[x[a]],z[a]);\n dist[v]=0;\n q.emplace(dist[v],v);\n }\n\n while(!q.empty()){\n auto p=q.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n\n int f=v>>BS2,i=(v>>BS)&msk,j=v&msk;\n int ai=my[wy[i]],aj=mx[wx[j]];\n if(f==k){\n vs.emplace_back((ai<<BS)|aj,dist[v]);\n continue;\n }\n\n auto push=\n [&](int ni,int nj,int nf){\n int u=idx(ni,nj,nf);\n int c=abs(wy[ni]-wy[i])+abs(wx[nj]-wx[j]);\n\n if(dist[u]>dist[v]+c){\n dist[u]=dist[v]+c;\n q.emplace(dist[u],u);\n }\n };\n\n if(hole[idx(ai,aj,f+1)]){\n push(i,j,f+1);\n continue;\n }\n\n if(i+1<ty) push(i+1,j,f);\n if(i-1>=0) push(i-1,j,f);\n if(j+1<tx) push(i,j+1,f);\n if(j-1>=0) push(i,j-1,f);\n }\n }\n {\n for(int i=0;i<(sy<<BS);i++) add[i]=INT_MAX;\n RadixHeap<int, int> q;\n for(auto p:vs){\n int v=p.first,d=p.second;\n add[v]=d;\n q.emplace(add[v],v);\n }\n\n while(!q.empty()){\n auto p=q.pop();\n int v=p.second;\n if(add[v]<p.first) continue;\n\n int i=v>>BS,j=v&msk;\n auto push=\n [&](int ni,int nj){\n int u=(ni<<BS)|nj;\n int c=abs(vy[ni]-vy[i])+abs(vx[nj]-vx[j]);\n if(add[u]>add[v]+c){\n add[u]=add[v]+c;\n q.emplace(add[u],u);\n }\n };\n\n if(i+1<sy) push(i+1,j);\n if(i-1>=0) push(i-1,j);\n if(j+1<sx) push(i,j+1);\n if(j-1>=0) push(i,j-1);\n }\n }\n };\n\n memset(dp,0,sizeof(dp));\n for(int p=0;p<n;p++){\n dijkstra(p);\n for(int i=0;i<sy;i++)\n for(int j=0;j<sx;j++)\n dp[i][j]+=add[(i<<BS)|j];\n }\n\n long long ans=1e18;\n for(int i=0;i<sy;i++)\n for(int j=0;j<sx;j++)\n if(!hole[idx(i,j,k)]) chmin(ans,dp[i][j]);\n\n for(int p=0;p<n;p++) ans+=(k-z[p])*100;\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6900, "memory_kb": 8424, "score_of_the_acc": -0.8878, "final_rank": 12 }, { "submission_id": "aoj_1607_4973359", "code_snippet": "//Let's join Kaede Takagaki Fan Club !!\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\ntypedef pair<P,P> P2;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define eps 1e-7\n//#define INF 1000000000\n#define fi first\n#define sc second\n#define rep(i,x) for(int i=0;i<x;i++)\n#define repn(i,x) for(int i=1;i<=x;i++)\n#define SORT(x) sort(x.begin(),x.end())\n#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())\n#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())\n#define all(x) x.begin(),x.end()\ntemplate<class T>\nvoid dmp(T a){\n\trep(i,a.size()) cout << a[i] << \" \";\n\tcout << endl;\n}\ntemplate<class T>\nbool chmax(T&a, T b){\n\tif(a < b){\n\t\ta = b;\n\t\treturn 1;\n\t}\n\treturn 0;\n}\ntemplate<class T>\nbool chmin(T&a, T b){\n\tif(a > b){\n\t\ta = b;\n\t\treturn 1;\n\t}\n\treturn 0;\n}\ntemplate<class T>\nvoid g(T &a){\n\tcin >> a;\n}\ntemplate<class T>\nvoid o(const T &a,bool space=false){\n\tcout << a << (space?' ':'\\n');\n}\n//ios::sync_with_stdio(false);\nconst ll mod = 1000000007;//998244353\ntemplate<class T>\nvoid add(T&a,T b){\n\ta+=b;\n\tif(a >= mod) a-=mod;\n}\n\nint n;\nint m, k, r;\nint x[105], y[105], z[105];\nvector<P2>hole[16];\nvector<P1>dp[105][16];\nvoid solve(){\n\tll ans = 0;\n\tcin >> m >> k >> r;\n\trepn(i, n){\n\t\tcin >> x[i] >> y[i] >> z[i];\n\t\tans += 100LL * (k-z[i]);\n\t}\n\trepn(i, k){\n\t\thole[i].clear();\n\t\trepn(j, n) dp[j][i].clear();\n\t}\n\tset<int>S;\n\trepn(i, m){\n\t\tint a1, b1, a2, b2, w; cin >> a1 >> b1 >> a2 >> b2 >> w;\n\t\thole[w].pb(mp( mp(a1, a2), mp(b1, b2) ));\n\t\tS.insert(a1); S.insert(a2);\n\t}\n\trepn(i, n){\n\t\tdp[i][z[i]].pb(mp(0, mp(x[i], y[i])));\n\t\tfor(int j=z[i]+1;j<=k;j++){\n\t\t\tmap<P, int>M;\n\t\t\tfor(const auto at:dp[i][j-1]){\n\t\t\t\tfor(const auto zz:hole[j]){\n\t\t\t\t\tint val = at.fi;\n\t\t\t\t\tP za = at.sc;\n\t\t\t\t\tif(zz.fi.fi > za.fi){\n\t\t\t\t\t\tval += zz.fi.fi-za.fi; za.fi = zz.fi.fi;\n\t\t\t\t\t}\n\t\t\t\t\tif(zz.fi.sc < za.fi){\n\t\t\t\t\t\tval += za.fi-zz.fi.sc; za.fi = zz.fi.sc;\n\t\t\t\t\t}\n\t\t\t\t\tif(zz.sc.fi > za.sc){\n\t\t\t\t\t\tval += zz.sc.fi-za.sc; za.sc = zz.sc.fi;\n\t\t\t\t\t}\n\t\t\t\t\tif(zz.sc.sc < za.sc){\n\t\t\t\t\t\tval += za.sc-zz.sc.sc; za.sc = zz.sc.sc;\n\t\t\t\t\t}\n\t\t\t\t\tif(M.find(za) == M.end()) M[za] = val;\n\t\t\t\t\telse chmin(M[za], val);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(const auto at:M){\n\t\t\t\tdp[i][j].pb(mp(at.sc, at.fi));\n\t\t\t}\n\t\t}\n\t}\n\tvector<int>zx, zy;\n\trepn(i, n){\n\t\tfor(const auto at:dp[i][k]){\n\t\t\tzx.pb(at.sc.fi);\n\t\t\tzy.pb(at.sc.sc);\n\t\t}\n\t}\n\tfor(const auto at:hole[k]){\n\t\tzx.pb(at.fi.fi-1); zx.pb(at.fi.fi);\n\t\tzx.pb(at.fi.sc); zx.pb(at.fi.sc+1);\n\t\t\n\t\tzy.pb(at.sc.fi-1); zy.pb(at.sc.fi);\n\t\tzy.pb(at.sc.sc); zy.pb(at.sc.sc+1);\n\t}\n\tSORT(zx); SORT(zy);\n\tERASE(zx); ERASE(zy);\n\tll mn = 1e18;\n\trep(i, zx.size())rep(j, zy.size()){\n\t\tint X = zx[i], Y = zy[j], bad = 0;\n\t\tfor(const auto at:hole[k]){\n\t\t\tif(at.fi.fi <= X && X <= at.fi.sc && at.sc.fi <= Y && Y <= at.sc.sc){\n\t\t\t\tbad = 1; break;\n\t\t\t}\n\t\t}\n\t\tif(bad) continue;\n\t\tll sum = 0;\n\t\trepn(i, n){\n\t\t\tll add = 1e18;\n\t\t\tfor(const auto at:dp[i][k]){\n\t\t\t\tchmin(add, 1LL*abs(X-at.sc.fi) + abs(Y-at.sc.sc) + at.fi);\n\t\t\t}\n\t\t\tsum += add;\n\t\t\tif(sum >= mn) break;\n\t\t}\n\t\tchmin(mn, sum);\n\t}\n\tcout << mn + ans << '\\n';\n}\nint main(){\n\twhile(1){\n\t\tcin >> n;\n\t\tif(n == 0) return 0;\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 440, "memory_kb": 4092, "score_of_the_acc": -0.0052, "final_rank": 2 }, { "submission_id": "aoj_1607_4970375", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=int64_t;\ntemplate<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}\ntemplate<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}\n\nint n;\nvoid solve(){\n int m,k,r;\n cin>>m>>k>>r;\n vector<int> xs, ys;\n vector<int> x(n),y(n),z(n);\n struct hole{\n int u,v,U,V;\n };\n vector<vector<hole>> H(k);\n ll ans{};\n for(int i{};i<(n);++i){\n cin>>x[i]>>y[i]>>z[i];\n ans+=(k-z[i])*100;\n --x[i];--y[i];--z[i];\n xs.push_back(x[i]);\n ys.push_back(y[i]);\n }\n for(int i{},u,v,U,V,w;i<m;++i){\n cin>>u>>v>>U>>V>>w;\n H[--w].push_back(hole{--u,--v,--U,--V});\n xs.push_back(u);\n xs.push_back(U);\n ys.push_back(v);\n ys.push_back(V);\n if(w==k-1){\n if(u) xs.push_back(u-1);\n if(U+1<r) xs.push_back(U+1);\n if(v) ys.push_back(v-1);\n if(V+1<r) ys.push_back(V+1);\n }\n }\n struct kou{\n int x,y;\n ll d;\n };\n vector<vector<kou>> e(n);\n for(int i{};i<k;++i){\n for(int j{};j<n;++j){\n if(!e[j].empty()){\n vector<kou> ne;\n for(auto[p,q,d]:e[j]){\n for(auto[u,v,U,V]:H[i]){\n int X=clamp(p, u, U);\n int Y=clamp(q, v, V);\n //cerr<<p<<\" \"<<q<<\" \"<<d<<\" \"<<X<<\" \"<<Y<<'\\n';\n ne.push_back(kou{X, Y, d+abs(X-p)+abs(Y-q)});\n }\n }\n e[j].clear();\n int N=ne.size();\n vector<bool> need(N,true);\n for(int I{};I<N;++I) if(need[I]){\n for(int J{};J<(I);++J) if(need[J]){\n if(ne[I].d+abs(ne[I].x-ne[J].x)+abs(ne[I].y-ne[J].y)<=ne[J].d){\n need[J]=false;\n }\n else if(ne[J].d+abs(ne[I].x-ne[J].x)+abs(ne[I].y-ne[J].y)<=ne[I].d){\n need[I]=false;\n }\n }\n }\n for(int I{};I<N;++I) if(need[I]){\n e[j].push_back(ne[I]);\n }\n }\n if(i==z[j]){\n e[j].push_back(kou{x[j],y[j],0});\n }\n }\n /*for(auto&E:e){\n for(auto[x,y,d]:E){\n cerr<<x<<\" \"<<y<<\" \"<<d<<'\\n';\n }\n cerr<<'\\n';\n }*/\n }\n sort(xs.begin(),xs.end());\n sort(ys.begin(),ys.end());\n auto it=unique(xs.begin(),xs.end());\n xs.erase(it,xs.end());\n auto it2=unique(ys.begin(),ys.end());\n ys.erase(it2,ys.end());\n unordered_map<int,int> ix,iy;\n int XS=xs.size(), YS=ys.size();\n for(int i{};i<XS;++i){\n ix[xs[i]]=i;\n }\n for(int i{};i<YS;++i){\n iy[ys[i]]=i;\n }\n /*for(auto&i:xs) cerr<<i+1<<\" \";\n cerr<<'\\n';\n for(auto&i:ys) cerr<<i+1<<\" \";\n cerr<<'\\n'<<'\\n';////////////////////*/\n auto D=vector(n, vector(XS,vector(YS, LLONG_MAX/2)));\n for(int p{};p<(n);++p){\n for(auto[X,Y,d]:e[p]){\n D[p][ix[X]][iy[Y]]=d;\n }\n for(int i{};i<(XS);++i){\n for(int j{};j<(YS);++j){\n if(i) chmin(D[p][i][j], D[p][i-1][j]+xs[i]-xs[i-1]);\n if(j) chmin(D[p][i][j], D[p][i][j-1]+ys[j]-ys[j-1]);\n }\n }\n for(int i{XS};i--;){\n for(int j{};j<(YS);++j){\n if(i+1<XS) chmin(D[p][i][j], D[p][i+1][j]+xs[i+1]-xs[i]);\n if(j) chmin(D[p][i][j], D[p][i][j-1]+ys[j]-ys[j-1]);\n }\n }\n for(int i{};i<(XS);++i){\n for(int j{YS};j--;){\n if(i) chmin(D[p][i][j], D[p][i-1][j]+xs[i]-xs[i-1]);\n if(j+1<YS) chmin(D[p][i][j], D[p][i][j+1]+ys[j+1]-ys[j]);\n }\n }\n for(int i{XS};i--;){\n for(int j{YS};j--;){\n if(i+1<XS) chmin(D[p][i][j], D[p][i+1][j]+xs[i+1]-xs[i]);\n if(j+1<YS) chmin(D[p][i][j], D[p][i][j+1]+ys[j+1]-ys[j]);\n }\n }\n /*for(auto&i:D[p]){\n for(auto&j:i){\n cerr<<j<<\" \";\n }\n cerr<<'\\n';\n }\n cerr<<'\\n';///////////*/\n }\n ll ANS=LLONG_MAX;\n for(int i{};i<(XS);++i){\n for(int j{};j<(YS);++j){\n bool f=false;\n for(auto[u,v,U,V]:H[k-1]){\n if(u<=xs[i] and xs[i]<=U and v<=ys[j] and ys[j]<=V){\n f=true;\n break;\n }\n }\n if(f) continue;\n ll tmp{};\n for(int p{};p<n;++p){\n tmp+=D[p][i][j];\n }\n if(chmin(ANS, tmp)){\n //cerr<<xs[i]+1<<\" \"<<ys[j]+1<<\" \"<<ANS<<'\\n';\n };\n }\n }\n cout<<ans+ANS<<endl;\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n while(cin>>n, n) solve();\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 42284, "score_of_the_acc": -0.2647, "final_rank": 6 }, { "submission_id": "aoj_1607_4969291", "code_snippet": "#pragma GCC optimize(\"O3\")\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define N 200100\n#define MOD 1000000007 //998244353\n#define ll long long\n#define rep(i, n) for(int i = 0; i < n; ++i)\n#define rep2(i, a, b) for(int i = a; i <= b; ++i)\n#define rep3(i, a, b) for(int i = a; i >= b; --i)\n#define eb emplace_back\n#define pb push_back\n#define all(c) (c).begin(), (c).end()\n#define vi vector<int>\n#define pii pair<int,int>\n#define pll pair<ll,ll>\nint d[310][310];\nint d2[310][310];\nbool w[310][310];\nint ans[310][310];\nint dx[310];\nint dy[310];\n\n\nint main() {\n\tint n;\n\tint m, k, r;\n\tint x, y, z;\n\tint ax[100];\n\tint ay[100];\n\tint az[100];\n\tint x1[50];\n\tint x2[50];\n\tint y1[50];\n\tint y2[50];\n\tint zz[50];\n\tvector<int>fx;\n\tvector<int>fy;\n\tvector<int>xx;\n\tvector<int>yy;\n\tvector<int>ox;\n\tvector<int>oy;\n\tint fians;\n\tmap<int, int>rx;\n\tmap<int, int>ry;\n\tint sx, sy, osx, osy, itrx, itry;\n\tint cx1, cx2, cy1, cy2;\n\tpriority_queue<pair<int, pii> > pq;\n\tpair<int, pii> paa;\n\n\twhile (true) {\n\t\tcin >> n;\n\t\tif (n == 0)break;\n\t\trep(i, 310) {\n\t\t\trep(j, 310)ans[i][j] = 0;\n\t\t}\n\t\tcin >> m >> k >> r;\n\t\tfx.clear();\n\t\tfy.clear();\n\t\trep(i, n) {\n\t\t\tcin >> ax[i] >> ay[i] >> az[i];\n\t\t\tfx.pb(ax[i]);\n\t\t\tfy.pb(ay[i]);\n\t\t}\n\t\trep(i, m) {\n\t\t\tcin >> x1[i] >> y1[i] >> x2[i] >> y2[i] >> zz[i];\n\t\t\tfx.pb(x1[i]);\n\t\t\tfx.pb(x2[i]);\n\t\t\tif (x1[i] > 1)fx.pb(x1[i] - 1);\n\t\t\tif (x2[i] < r)fx.pb(x2[i] + 1);\n\t\t\tfy.pb(y1[i]);\n\t\t\tfy.pb(y2[i]);\n\t\t\tif (y1[i] > 1)fy.pb(y1[i] - 1);\n\t\t\tif (y2[i] < r)fy.pb(y2[i] + 1);\n\t\t}\n\t\tsort(fx.begin(), fx.end());\n\t\tfx.erase(unique(fx.begin(), fx.end()), fx.end());\n\t\tsort(fy.begin(), fy.end());\n\t\tfy.erase(unique(fy.begin(), fy.end()), fy.end());\n\t\trep(nn, n) {\n\t\t\txx.clear();\n\t\t\tyy.clear();\n\t\t\txx.pb(ax[nn]);\n\t\t\tyy.pb(ay[nn]);\n\t\t\tsx = 1;\n\t\t\tsy = 1;\n\t\t\td2[0][0] = 0;\n\t\t\trep2(i, az[nn], k) {\n\t\t\t\tox = xx;\n\t\t\t\toy = yy;\n\t\t\t\trep(j, m) {\n\t\t\t\t\tif (zz[j] == i+1) {\n\t\t\t\t\t\txx.pb(x1[j]);\n\t\t\t\t\t\txx.pb(x2[j]);\n\t\t\t\t\t\tyy.pb(y1[j]);\n\t\t\t\t\t\tyy.pb(y2[j]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tsort(xx.begin(), xx.end());\n\t\t\t\txx.erase(unique(xx.begin(), xx.end()), xx.end());\n\t\t\t\tsort(yy.begin(), yy.end());\n\t\t\t\tyy.erase(unique(yy.begin(), yy.end()), yy.end());\n\t\t\t\tif (i == k) {\n\t\t\t\t\txx = fx;\n\t\t\t\t\tyy = fy;\n\t\t\t\t}\n\t\t\t\tosx = sx;\n\t\t\t\tosy = sy;\n\t\t\t\tsx = xx.size();\n\t\t\t\tsy = yy.size();\n\t\t\t\trep(j, sx)rx[xx[j]] = j;\n\t\t\t\trep(j, sy)ry[yy[j]] = j;\n\t\t\t\trep(j, sx-1) {\n\t\t\t\t\tdx[j] = xx[j + 1] - xx[j];\n\t\t\t\t}\n\t\t\t\trep(j, sy - 1) {\n\t\t\t\t\tdy[j] = yy[j + 1] - yy[j];\n\t\t\t\t}\n\t\t\t\trep(ii, sx) {\n\t\t\t\t\trep(jj, sy) {\n\t\t\t\t\t\tw[ii][jj] = false;\n\t\t\t\t\t\td[ii][jj] = MOD;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\trep(j, m) {\n\t\t\t\t\tif (zz[j] == i) {\n\t\t\t\t\t\tcx1 = rx[x1[j]];\n\t\t\t\t\t\tcx2 = rx[x2[j]];\n\t\t\t\t\t\tcy1 = ry[y1[j]];\n\t\t\t\t\t\tcy2 = ry[y2[j]];\n\t\t\t\t\t\trep2(ii, cx1, cx2) {\n\t\t\t\t\t\t\trep2(jj, cy1, cy2)w[ii][jj] = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\twhile (!pq.empty())pq.pop();\n\t\t\t\titrx = 0;\n\t\t\t\trep(ii, sx) {\n\t\t\t\t\tif (ox[itrx] == xx[ii]) {\n\t\t\t\t\t\titry = 0;\n\t\t\t\t\t\trep(jj, sy) {\n\t\t\t\t\t\t\tif (oy[itry] == yy[jj]) {\n\t\t\t\t\t\t\t\tif (i == az[nn]) {\n\t\t\t\t\t\t\t\t\td[ii][jj] = 0;\n\t\t\t\t\t\t\t\t\tpq.push({ -d[ii][jj],{ii,jj} });\n\t\t\t\t\t\t\t\t}\n else if (w[ii][jj]) {\n\t\t\t\t\t\t\t\t\td[ii][jj] = d2[itrx][itry] + 100;\n\t\t\t\t\t\t\t\t\tpq.push({ -d[ii][jj],{ii,jj} });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\titry++;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (itry >= osy)break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\titrx++;\n\t\t\t\t\t}\n\t\t\t\t\tif (itrx >= osx)break;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\twhile (!pq.empty()) {\n\t\t\t\t\twhile (!pq.empty()) {\n\t\t\t\t\t\tpaa = pq.top();\n\t\t\t\t\t\tz = -(paa.first);\n\t\t\t\t\t\tx = paa.second.first;\n\t\t\t\t\t\ty = paa.second.second;\n\t\t\t\t\t\tif (d[x][y] == z)break;\n\t\t\t\t\t\tpq.pop();\n\t\t\t\t\t}\n\t\t\t\t\tif (pq.empty())break;\n\t\t\t\t\tif (x > 0) {\n\t\t\t\t\t\tif (d[x - 1][y] > z + dx[x - 1]) {\n\t\t\t\t\t\t\td[x - 1][y] = z + dx[x - 1];\n\t\t\t\t\t\t\tpq.push({ -d[x - 1][y],{x - 1,y} });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (x < (sx - 1)) {\n\t\t\t\t\t\tif (d[x + 1][y] > z + dx[x]) {\n\t\t\t\t\t\t\td[x + 1][y] = z + dx[x];\n\t\t\t\t\t\t\tpq.push({ -d[x + 1][y],{x + 1,y} });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (y > 0) {\n\t\t\t\t\t\tif (d[x][y-1] > z + dy[y - 1]) {\n\t\t\t\t\t\t\td[x][y - 1] = z + dy[y - 1];\n\t\t\t\t\t\t\tpq.push({ -d[x][y - 1],{x,y - 1} });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (y < (sy - 1)) {\n\t\t\t\t\t\tif (d[x][y + 1] > z + dy[y]) {\n\t\t\t\t\t\t\td[x][y + 1] = z + dy[y];\n\t\t\t\t\t\t\tpq.push({ -d[x][y + 1],{x,y + 1} });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tpq.pop();\n\t\t\t\t}\n\t\t\t\trep(ii, sx) {\n\t\t\t\t\trep(jj, sy){\nd2[ii][jj] = d[ii][jj];\n //cout<<d[ii][jj]<<\" \";\n }\n //cout<<endl;\n\t\t\t\t}\n\n\t\t\t}//階\n\t\t\trep(ii, sx) {\n\t\t\t\trep(jj, sy) {\n\t\t\t\t\tans[ii][jj] += d2[ii][jj];\n\t\t\t\t}\n\t\t\t}\n\n\t\t}//ロボット\n\t\tfians = MOD * 2;\n\t\trep(ii, sx) {\n\t\t\trep(jj, sy) {\n\t\t\t\tif(!w[ii][jj])fians = min(fians, ans[ii][jj]);\n\t\t\t}\n\t\t}\n\t\tcout << fians << endl;\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 7270, "memory_kb": 4864, "score_of_the_acc": -0.9137, "final_rank": 13 }, { "submission_id": "aoj_1607_4036948", "code_snippet": "#define PROBLEM \"http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1607\"\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#define call_from_test\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\nstruct FastIO{\n FastIO(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n }\n}fastio_beet;\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n//END CUT HERE\n#ifndef call_from_test\nsigned main(){\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n#endif\n//BEGIN CUT HERE\ntemplate<typename V>\nV compress(V v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\ntemplate<typename T>\nmap<T, int> dict(const vector<T> &v){\n map<T, int> res;\n for(int i=0;i<(int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\nmap<char, int> dict(const string &v){\n return dict(vector<char>(v.begin(),v.end()));\n}\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned ABC036_C(){\n int n;\n cin>>n;\n vector<int> a(n);\n for(int i=0;i<n;i++) cin>>a[i];\n auto v=compress(a);\n auto m=dict(v);\n for(int i=0;i<n;i++) cout<<m[a[i]]<<endl;\n return 0;\n}\n/*\n verified on 2018/08/26\n https://abc036.contest.atcoder.jp/tasks/abc036_c\n*/\n\nsigned main(){\n ABC036_C();\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\n#endif\n//BEGIN CUT HERE\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\n#include<ext/pb_ds/tag_and_trait.hpp>\nusing namespace __gnu_pbds;\ntemplate <typename T>\nusing gtree = tree<T,null_type,less<T>,rb_tree_tag,\n tree_order_statistics_node_update>;\n// usage:\n// find_by_order(int k):\n// return the iterator of k-th smallest element (0-indexed)\n// order_of_key(T key): return the index of key in tree\n//END CUT HERE\n#ifndef call_from_test\nsigned ARC028_B(){\n using ll = long long;\n int n,k;\n cin>>n>>k;\n vector<int> xs(n);\n for(int i=0;i<n;i++) cin>>xs[i];\n map<int, int> mp;\n for(int i=0;i<n;i++) mp[xs[i]]=i+1;\n gtree<ll> G;\n for(int i=0;i<k-1;i++) G.insert(xs[i]);\n for(int i=k-1;i<n;i++){\n G.insert(xs[i]);\n auto key=*G.find_by_order(k-1);\n cout<<mp[key]<<endl;\n }\n return 0;\n}\n\n/*\n verified on 2019/12/09\n https://atcoder.jp/contests/arc028/tasks/arc028_2\n*/\n\nsigned ARC033_C(){\n int q;\n cin>>q;\n gtree<int> G;\n while(q--){\n int t,x;\n cin>>t>>x;\n if(t==1) G.insert(x);\n if(t==2){\n int k=*G.find_by_order(x-1);\n G.erase(k);\n cout<<k<<endl;\n }\n }\n return 0;\n}\n\n/*\n verified on 2019/12/09\n https://atcoder.jp/contests/arc033/tasks/arc033_3\n*/\n\nsigned main(){\n //ARC028_B();\n //ARC033_C();\n return 0;\n}\n#endif\n\n#ifndef call_from_test\n#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n#endif\n//BEGIN CUT HERE\n// prohibited to push an element less than popped one\n// Key: int or long long\ntemplate<typename K,typename V>\nstruct RadixHeap{\n static constexpr int bit = sizeof(K) * 8;\n array<vector< pair<K, V> >, bit> vs;\n\n int size;\n K last;\n RadixHeap():size(0),last(0){}\n\n bool empty() const{return size==0;}\n\n inline int getbit(int a){\n return a?bit-__builtin_clz(a):0;\n }\n\n inline int getbit(long long a){\n return a?bit-__builtin_clzll(a):0;\n }\n\n void emplace(K key,V val){\n size++;\n vs[getbit(key^last)].emplace_back(key,val);\n }\n\n pair<K, V> pop(){\n if(vs[0].empty()){\n int idx=1;\n while(vs[idx].empty()) idx++;\n last=min_element(vs[idx].begin(),vs[idx].end())->first;\n for(auto &p:vs[idx]) vs[getbit(p.first^last)].emplace_back(p);\n vs[idx].clear();\n }\n --size;\n auto res=vs[0].back();\n vs[0].pop_back();\n return res;\n }\n};\n//END CUT HERE\n#ifndef call_from_test\n//INSERT ABOVE HERE\nsigned main(){\n return 0;\n}\n#endif\n\n#undef call_from_test\n\nconst int BS = 8, BS2 = BS * 2;\nconst int msk = (1<<BS)-1;\nconst int MAX = 12 * (1<<BS) * (1<<BS);\nint dist[MAX];\nbool hole[MAX];\nlong long add[(1<<BS) * (1<<BS)];\nlong long dp[(1<<BS)][(1<<BS)];\n\nsigned main(){\n int n;\n while(cin>>n,n){\n int m,k,r;\n cin>>m>>k>>r;\n vector<int> x(n),y(n),z(n);\n for(int i=0;i<n;i++) cin>>x[i]>>y[i]>>z[i];\n vector<int> u1(m),v1(m),u2(m),v2(m),w(m);\n for(int i=0;i<m;i++) cin>>u1[i]>>v1[i]>>u2[i]>>v2[i]>>w[i];\n\n vector<int> vx(x),vy(y);\n for(int p=0;p<m;p++){\n vx.emplace_back(u1[p]);\n vx.emplace_back(u2[p]);\n vy.emplace_back(v1[p]);\n vy.emplace_back(v2[p]);\n if(w[p]!=k) continue;\n if(u1[p]-1>=1) vx.emplace_back(u1[p]-1);\n if(u2[p]+1<=r) vx.emplace_back(u2[p]+1);\n if(v1[p]-1>=1) vy.emplace_back(v1[p]-1);\n if(v2[p]+1<=r) vy.emplace_back(v2[p]+1);\n }\n\n vx=compress(vx);\n vy=compress(vy);\n auto mx=dict(vx);\n auto my=dict(vy);\n\n // kokokara y yuusen\n auto idx=[&](int cy,int cx,int f){return (f<<BS2)|(cy<<BS)|cx;};\n\n memset(hole,0,sizeof(hole));\n for(int p=0;p<m;p++)\n for(int i=my[v1[p]];i<=my[v2[p]];i++)\n for(int j=mx[u1[p]];j<=mx[u2[p]];j++)\n hole[idx(i,j,w[p])]=1;\n\n int sy=vy.size(),sx=vx.size();\n\n auto dijkstra=\n [&](int a)->void{\n vector<int> wy,wx;\n for(int p=0;p<m;p++){\n if(w[p]<=z[a]) continue;\n wy.emplace_back(v1[p]);\n wy.emplace_back(v2[p]);\n wx.emplace_back(u1[p]);\n wx.emplace_back(u2[p]);\n }\n wy.emplace_back(y[a]);\n wx.emplace_back(x[a]);\n wy=compress(wy);\n wx=compress(wx);\n auto zy=dict(wy);\n auto zx=dict(wx);\n int ty=zy.size(),tx=zx.size();\n\n vector<pair<int, int> > vs;\n {\n for(int i=0;i<MAX;i++) dist[i]=INT_MAX;\n RadixHeap<int, int> q;\n {\n int v=idx(zy[y[a]],zx[x[a]],z[a]);\n dist[v]=0;\n q.emplace(dist[v],v);\n }\n\n while(!q.empty()){\n auto p=q.pop();\n int v=p.second;\n if(dist[v]<p.first) continue;\n\n int f=v>>BS2,i=(v>>BS)&msk,j=v&msk;\n int ai=my[wy[i]],aj=mx[wx[j]];\n if(f==k){\n vs.emplace_back((ai<<BS)|aj,dist[v]);\n continue;\n }\n\n auto push=\n [&](int ni,int nj,int nf){\n int u=idx(ni,nj,nf);\n int c=abs(wy[ni]-wy[i])+abs(wx[nj]-wx[j]);\n\n if(dist[u]>dist[v]+c){\n dist[u]=dist[v]+c;\n q.emplace(dist[u],u);\n }\n };\n\n if(hole[idx(ai,aj,f+1)]){\n push(i,j,f+1);\n continue;\n }\n\n if(i+1<ty) push(i+1,j,f);\n if(i-1>=0) push(i-1,j,f);\n if(j+1<tx) push(i,j+1,f);\n if(j-1>=0) push(i,j-1,f);\n }\n }\n {\n for(int i=0;i<(sy<<BS);i++) add[i]=INT_MAX;\n RadixHeap<int, int> q;\n for(auto p:vs){\n int v=p.first,d=p.second;\n add[v]=d;\n q.emplace(add[v],v);\n }\n\n while(!q.empty()){\n auto p=q.pop();\n int v=p.second;\n if(add[v]<p.first) continue;\n\n int i=v>>BS,j=v&msk;\n auto push=\n [&](int ni,int nj){\n int u=(ni<<BS)|nj;\n int c=abs(vy[ni]-vy[i])+abs(vx[nj]-vx[j]);\n if(add[u]>add[v]+c){\n add[u]=add[v]+c;\n q.emplace(add[u],u);\n }\n };\n\n if(i+1<sy) push(i+1,j);\n if(i-1>=0) push(i-1,j);\n if(j+1<sx) push(i,j+1);\n if(j-1>=0) push(i,j-1);\n }\n }\n };\n\n memset(dp,0,sizeof(dp));\n for(int p=0;p<n;p++){\n dijkstra(p);\n for(int i=0;i<sy;i++)\n for(int j=0;j<sx;j++)\n dp[i][j]+=add[(i<<BS)|j];\n }\n\n long long ans=1e18;\n for(int i=0;i<sy;i++)\n for(int j=0;j<sx;j++)\n if(!hole[idx(i,j,k)]) chmin(ans,dp[i][j]);\n\n for(int p=0;p<n;p++) ans+=(k-z[p])*100;\n cout<<ans<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 7280, "memory_kb": 7976, "score_of_the_acc": -0.9352, "final_rank": 14 }, { "submission_id": "aoj_1607_3703054", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define ADD 100\n\nstruct LOC{\n\tLOC(){\n\t\tx = y = 0;\n\t}\n\tLOC(int arg_x,int arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\tvoid set(int arg_x,int arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\tbool operator<(const struct LOC &arg) const{\n\t\tif(x != arg.x){\n\n\t\t\treturn x < arg.x;\n\t\t}else{\n\n\t\t\treturn y < arg.y;\n\t\t}\n\t}\n\tbool operator==(const struct LOC &arg) const{\n\t\treturn x == arg.x && y == arg.y;\n\t}\n\n\tint x,y;\n};\n\n\nstruct Info{\n\n\tLOC loc;\n\tint floor;\n};\n\nstruct Data{\n\tint x_1,y_1,x_2,y_2;\n};\n\nstruct State{\n\tState(LOC arg_loc,int arg_sum_cost,int arg_hole_index){\n\t\tloc.set(arg_loc.x,arg_loc.y);\n\t\tsum_cost = arg_sum_cost;\n\t\thole_index = arg_hole_index;\n\t}\n\tLOC loc;\n\tint sum_cost,hole_index;\n};\n\nint num_robot,num_hole,K,R;\nInfo info[105];\nData data[55];\nvector<int> HOLE[10];\nvector<State> V[105],dp[2];\n\n\nint calc_dist(LOC a, LOC b){\n\n\treturn abs(a.x-b.x)+abs(a.y-b.y);\n}\n\nLOC calc_near_loc(LOC loc,Data data){\n\n\tLOC ret;\n\n\tif(loc.y < data.y_1){\n\n\t\tif(loc.x < data.x_1){\n\n\t\t\tret.set(data.x_1,data.y_1);\n\n\t\t}else if(loc.x > data.x_2){\n\n\t\t\tret.set(data.x_2,data.y_1);\n\n\t\t}else{\n\n\t\t\tret.set(loc.x,data.y_1);\n\t\t}\n\n\n\t}else if(loc.y > data.y_2){\n\n\t\tif(loc.x < data.x_1){\n\n\t\t\tret.set(data.x_1,data.y_2);\n\n\t\t}else if(loc.x > data.x_2){\n\n\t\t\tret.set(data.x_2,data.y_2);\n\n\t\t}else{\n\n\t\t\tret.set(loc.x,data.y_2);\n\t\t}\n\n\t}else{\n\n\t\tif(loc.x < data.x_1){\n\n\t\t\tret.set(data.x_1,loc.y);\n\n\t\t}else if(loc.x > data.x_2){\n\n\t\t\tret.set(data.x_2,loc.y);\n\n\t\t}else{\n\n\t\t\tret.set(loc.x,loc.y);\n\t\t}\n\t}\n\n\treturn ret;\n}\n\n\nvoid func(){\n\n\tfor(int i = 0; i < num_robot; i++){\n\n\t\tV[i].clear();\n\t}\n\n\tscanf(\"%d %d %d\",&num_hole,&K,&R);\n\n\tfor(int i = 0; i < K; i++){\n\n\t\tHOLE[i].clear();\n\t}\n\n\tfor(int i = 0; i < num_robot; i++){\n\n\t\tscanf(\"%d %d %d\",&info[i].loc.x,&info[i].loc.y,&info[i].floor);\n\t\tinfo[i].floor--;\n\t}\n\n\tint floor;\n\n\tfor(int i = 0; i < num_hole; i++){\n\n\t\tscanf(\"%d %d %d %d %d\",&data[i].x_1,&data[i].y_1,&data[i].x_2,&data[i].y_2,&floor);\n\t\tfloor--;\n\t\tHOLE[floor].push_back(i);\n\t}\n\n\tLOC tmp,next_loc;\n\tint index,next_cost,data_index;\n\tint CURRENT = 0,NEXT = 1;\n\tbool FLG;\n\n\tfor(int loop = 0; loop < num_robot; loop++){\n\n\t\tdp[CURRENT].clear();\n\t\tdp[NEXT].clear();\n\n\t\ttmp.set(info[loop].loc.x,info[loop].loc.y);\n\n\t\tindex = -1;\n\n\t\tfor(int k = 0; k < HOLE[info[loop].floor].size(); k++){\n\t\t\tdata_index = HOLE[info[loop].floor][k];\n\n\t\t\tif(data[data_index].x_1 <= tmp.x && data[data_index].x_2 >= tmp.x &&\n\t\t\t\t\tdata[data_index].y_1 <= tmp.y && data[data_index].y_2 >= tmp.y){\n\t\t\t\tindex = data_index;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tdp[CURRENT].push_back(State(tmp,0,index));\n\n\t\tfor(int next_floor = info[loop].floor+1; next_floor <= K-1; next_floor++){\n\t\t\tfor(int i = 0; i < dp[CURRENT].size(); i++){\n\n\t\t\t\ttmp = dp[CURRENT][i].loc;\n\n\t\t\t\tfor(int k = 0; k < HOLE[next_floor].size(); k++){\n\t\t\t\t\tnext_loc = calc_near_loc(tmp,data[HOLE[next_floor][k]]);\n\n\t\t\t\t\tFLG = false;\n\t\t\t\t\tfor(int a = 0; a < dp[NEXT].size(); a++){\n\t\t\t\t\t\tif(dp[NEXT][a].loc.x == next_loc.x && dp[NEXT][a].loc.y == next_loc.y){\n\t\t\t\t\t\t\tFLG = true;\n\t\t\t\t\t\t\tindex = a;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tnext_cost = dp[CURRENT][i].sum_cost+calc_dist(tmp,next_loc)+ADD;\n\n\t\t\t\t\tif(!FLG){ //初登場\n\n\t\t\t\t\t\tdp[NEXT].push_back(State(next_loc,next_cost,HOLE[next_floor][k]));\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tif(dp[NEXT][index].sum_cost > next_cost){\n\t\t\t\t\t\t\tdp[NEXT][index].sum_cost = next_cost;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdp[CURRENT].clear();\n\n\t\t\tfor(int a = 0; a < dp[NEXT].size(); a++){\n\t\t\t\tFLG = true;\n\t\t\t\tfor(int b = 0; b < dp[NEXT].size(); b++){\n\t\t\t\t\tif(a == b)continue;\n\t\t\t\t\t if(dp[NEXT][b].sum_cost+calc_dist(dp[NEXT][a].loc,dp[NEXT][b].loc) <= dp[NEXT][a].sum_cost){\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(FLG){\n\t\t\t\t\tdp[CURRENT].push_back(dp[NEXT][a]);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdp[NEXT].clear();\n\t\t}\n\n\t\tfor(int k = 0; k < dp[CURRENT].size(); k++){\n\n\t\t\tV[loop].push_back(dp[CURRENT][k]);\n\t\t}\n\t}\n\n\tvector<int> X,Y;\n\tfor(int i = 0; i < num_robot; i++){\n\n\t\tX.push_back(info[i].loc.x);\n\t\tY.push_back(info[i].loc.y);\n\t}\n\n\tfor(int i = 0; i < num_hole; i++){\n\n\t\tX.push_back(data[i].x_1);\n\t\tX.push_back(data[i].x_1-1);\n\t\tX.push_back(data[i].x_2);\n\t\tX.push_back(data[i].x_2+1);\n\t\tY.push_back(data[i].y_1);\n\t\tY.push_back(data[i].y_1-1);\n\t\tY.push_back(data[i].y_2);\n\t\tY.push_back(data[i].y_2+1);\n\t}\n\n\tsort(X.begin(),X.end());\n\tX.erase(unique(X.begin(),X.end()),X.end());\n\n\tsort(Y.begin(),Y.end());\n\tY.erase(unique(Y.begin(),Y.end()),Y.end());\n\n\tvector<LOC> CANDIDATE;\n\n\tfor(int i = 0; i < X.size(); i++){\n\t\tfor(int k = 0; k < Y.size(); k++){\n\t\t\tCANDIDATE.push_back(LOC(X[i],Y[k]));\n\t\t}\n\t}\n\n\tvector<LOC> WORK = CANDIDATE;\n\tCANDIDATE.clear();\n\n\tfor(int i = 0; i < WORK.size(); i++){\n\t\tFLG = true;\n\t\tfor(int a = 0; a < HOLE[K-1].size(); a++){\n\t\t\tindex = HOLE[K-1][a];\n\t\t\tif(data[index].x_1 <= WORK[i].x && data[index].x_2 >= WORK[i].x &&\n\t\t\t\t\tdata[index].y_1 <= WORK[i].y && data[index].y_2 >= WORK[i].y){\n\t\t\t\tFLG = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(FLG){\n\t\t\tCANDIDATE.push_back(WORK[i]);\n\t\t}\n\t}\n\n\tint ans = BIG_NUM;\n\tint minimum,tmp_sum,tmp_cost;\n\n\tfor(int i = 0; i < CANDIDATE.size(); i++){\n\t\ttmp_sum = 0;\n\t\tfor(int a = 0; a < num_robot; a++){\n\t\t\tminimum = BIG_NUM;\n\t\t\tfor(int k = 0; k < V[a].size(); k++){\n\t\t\t\ttmp_cost = V[a][k].sum_cost+calc_dist(V[a][k].loc,CANDIDATE[i]);\n\t\t\t\tminimum = min(minimum,tmp_cost);\n\t\t\t}\n\t\t\ttmp_sum += minimum;\n\t\t}\n\t\tans = min(ans,tmp_sum);\n\t}\n\n\tprintf(\"%d\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&num_robot);\n\t\tif(num_robot == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 790, "memory_kb": 4808, "score_of_the_acc": -0.0562, "final_rank": 3 }, { "submission_id": "aoj_1607_3352800", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = signed;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\ntemplate<typename T,typename U> \nusing gmap = cc_hash_table<T, U, hash<T> >;\n\n//INSERT ABOVE HERE\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\ngmap<T, Int> dict(const vector<T> &v){\n gmap<T, Int> res;\n for(Int i=0;i<(Int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\ntemplate< typename T >\nstruct RadixHeap\n{\n vector< pair< Int, T > > v[33];\n Int size, last;\n\n RadixHeap() : size(0), last(0) {}\n\n bool empty() const { return size == 0; }\n\n inline Int getbit(Int a)\n {\n return a ? 32 - __builtin_clz(a) : 0;\n }\n\n void push(Int key, const T &value)\n {\n ++size;\n v[getbit(key ^ last)].emplace_back(key, value);\n }\n\n pair<Int, T > pop()\n {\n if(v[0].empty()) {\n Int idx = 1;\n while(v[idx].empty()) ++idx;\n last = min_element(begin(v[idx]), end(v[idx]))->first;\n for(auto &p : v[idx]) v[getbit(p.first ^ last)].emplace_back(p);\n v[idx].clear();\n }\n --size;\n auto ret = v[0].back();\n v[0].pop_back();\n return ret;\n }\n};\n\nconst Int BS = 8, BS2 = BS * 2;\nconst Int msk = (1<<BS)-1;\nconst Int MAX = 12 * (1<<BS) * (1<<BS);\nInt dist[MAX];\nbool hole[MAX];\nlong long add[(1<<BS) * (1<<BS)];\nlong long dp[(1<<BS)][(1<<BS)];\n\nsigned main(){\n Int n;\n while(cin>>n,n){\n Int m,k,r;\n cin>>m>>k>>r;\n vector<Int> x(n),y(n),z(n);\n for(Int i=0;i<n;i++) cin>>x[i]>>y[i]>>z[i];\n vector<Int> u1(m),v1(m),u2(m),v2(m),w(m);\n for(Int i=0;i<m;i++) cin>>u1[i]>>v1[i]>>u2[i]>>v2[i]>>w[i];\n \n vector<Int> vx(x),vy(y);\n for(Int p=0;p<m;p++){\n vx.emplace_back(u1[p]);\n vx.emplace_back(u2[p]);\n vy.emplace_back(v1[p]);\n vy.emplace_back(v2[p]);\n if(w[p]!=k) continue;\n if(u1[p]-1>=1) vx.emplace_back(u1[p]-1);\n if(u2[p]+1<=r) vx.emplace_back(u2[p]+1);\n if(v1[p]-1>=1) vy.emplace_back(v1[p]-1);\n if(v2[p]+1<=r) vy.emplace_back(v2[p]+1);\n }\n \n vx=compress(vx);\n vy=compress(vy);\n auto mx=dict(vx);\n auto my=dict(vy);\n\n // kokokara y yuusen\n auto idx=[&](Int cy,Int cx,Int f){return (f<<BS2)|(cy<<BS)|cx;};\n \n memset(hole,0,sizeof(hole)); \n for(Int p=0;p<m;p++)\n for(Int i=my[v1[p]];i<=my[v2[p]];i++)\n for(Int j=mx[u1[p]];j<=mx[u2[p]];j++)\n hole[idx(i,j,w[p])]=1;\n \n Int sy=vy.size(),sx=vx.size();\n \n auto dijkstra=\n [&](Int a)->void{ \n vector<Int> wy,wx;\n for(Int p=0;p<m;p++){\n if(w[p]<=z[a]) continue; \n wy.emplace_back(v1[p]);\n wy.emplace_back(v2[p]); \n wx.emplace_back(u1[p]);\n wx.emplace_back(u2[p]);\n }\n wy.emplace_back(y[a]);\n wx.emplace_back(x[a]);\n wy=compress(wy);\n wx=compress(wx);\n auto zy=dict(wy);\n auto zx=dict(wx);\n Int ty=zy.size(),tx=zx.size();\n \n vector<pair<Int, Int> > vs;\n {\n for(Int i=0;i<MAX;i++) dist[i]=INT_MAX;\n RadixHeap<Int> q;\n {\n Int v=idx(zy[y[a]],zx[x[a]],z[a]);\n dist[v]=0;\n q.push(dist[v],v);\n }\n \n while(!q.empty()){\n auto p=q.pop();\n Int v=p.second;\n if(dist[v]<p.first) continue;\n \n Int f=v>>BS2,i=(v>>BS)&msk,j=v&msk;\n Int ai=my[wy[i]],aj=mx[wx[j]];\n if(f==k){\n vs.emplace_back((ai<<BS)|aj,dist[v]);\n continue;\n }\n \n auto push=\n [&](Int ni,Int nj,Int nf){\n Int u=idx(ni,nj,nf);\n Int c=abs(wy[ni]-wy[i])+abs(wx[nj]-wx[j]);\n \n if(dist[u]>dist[v]+c){\n dist[u]=dist[v]+c; \n q.push(dist[u],u);\n }\n };\n \n if(hole[idx(ai,aj,f+1)]){\n push(i,j,f+1);\n continue;\n }\n \n if(i+1<ty) push(i+1,j,f);\n if(i-1>=0) push(i-1,j,f);\n if(j+1<tx) push(i,j+1,f);\n if(j-1>=0) push(i,j-1,f);\n }\n }\n { \n for(Int i=0;i<(sy<<BS);i++) add[i]=INT_MAX;\n RadixHeap<Int> q; \n for(auto p:vs){\n Int v=p.first,d=p.second;\n add[v]=d;\n q.push(add[v],v);\n }\n \n while(!q.empty()){\n auto p=q.pop();\n Int v=p.second;\n if(add[v]<p.first) continue;\n \n Int i=v>>BS,j=v&msk; \n auto push=\n [&](Int ni,Int nj){\n Int u=(ni<<BS)|nj;\n Int c=abs(vy[ni]-vy[i])+abs(vx[nj]-vx[j]); \n if(add[u]>add[v]+c){\n add[u]=add[v]+c; \n q.push(add[u],u);\n }\n };\n \n if(i+1<sy) push(i+1,j);\n if(i-1>=0) push(i-1,j);\n if(j+1<sx) push(i,j+1);\n if(j-1>=0) push(i,j-1);\n }\n }\n };\n \n memset(dp,0,sizeof(dp)); \n for(Int p=0;p<n;p++){\n dijkstra(p); \n for(Int i=0;i<sy;i++)\n for(Int j=0;j<sx;j++)\n dp[i][j]+=add[(i<<BS)|j];\n }\n \n long long ans=1e18;\n for(Int i=0;i<sy;i++)\n for(Int j=0;j<sx;j++)\n if(!hole[idx(i,j,k)]) chmin(ans,dp[i][j]);\n\n for(Int p=0;p<n;p++) ans+=(k-z[p])*100;\n cout<<ans<<endl; \n }\n return 0;\n}", "accuracy": 1, "time_ms": 5810, "memory_kb": 8008, "score_of_the_acc": -0.7409, "final_rank": 9 }, { "submission_id": "aoj_1607_3352792", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = signed;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\n\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\ntemplate<typename T,typename U> \nusing gmap = cc_hash_table<T, U, hash<T> >;\n\n//INSERT ABOVE HERE\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\ngmap<T, Int> dict(const vector<T> &v){\n gmap<T, Int> res;\n for(Int i=0;i<(Int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\npair< Int, Int > v[33][1<<18];\nInt cur[33];\n\nstruct RadixHeap\n{\n Int size, last;\n RadixHeap() : size(0), last(0) {\n for(Int i=0;i<33;i++) cur[i]=0;\n }\n\n bool empty() const { return size == 0; }\n\n inline Int getbit(Int a) {\n return a ? 32 - __builtin_clz(a) : 0;\n }\n\n void push(Int key, const Int &value) {\n ++size;\n Int uku=getbit(key^last);\n v[uku][cur[uku]++]=make_pair(key,value);\n }\n\n pair<Int, Int> pop() {\n if(!cur[0]) { \n Int idx = 1;\n while(!cur[idx]) ++idx;\n last=min_element(v[idx],v[idx]+cur[idx])->first;\n for(Int i=0;i<cur[idx];i++){\n auto &p=v[idx][i];\n Int uku=getbit(p.first^last);\n v[uku][cur[uku]++]=p;\n }\n cur[idx]=0;\n }\n --size;\n auto ret = v[0][--cur[0]];\n return ret;\n }\n};\n\nconst Int BS = 8, BS2 = BS * 2;\nconst Int msk = (1<<BS)-1;\nconst Int MAX = 12 * (1<<BS) * (1<<BS);\nInt dist[MAX];\nbool hole[MAX];\nlong long add[(1<<BS) * (1<<BS)];\nlong long dp[(1<<BS)][(1<<BS)];\n\nsigned main(){\n Int n;\n while(cin>>n,n){\n Int m,k,r;\n cin>>m>>k>>r;\n vector<Int> x(n),y(n),z(n);\n for(Int i=0;i<n;i++) cin>>x[i]>>y[i]>>z[i];\n vector<Int> u1(m),v1(m),u2(m),v2(m),w(m);\n for(Int i=0;i<m;i++) cin>>u1[i]>>v1[i]>>u2[i]>>v2[i]>>w[i];\n \n vector<Int> vx(x),vy(y);\n for(Int p=0;p<m;p++){\n vx.emplace_back(u1[p]);\n vx.emplace_back(u2[p]);\n vy.emplace_back(v1[p]);\n vy.emplace_back(v2[p]);\n if(w[p]!=k) continue;\n if(u1[p]-1>=1) vx.emplace_back(u1[p]-1);\n if(u2[p]+1<=r) vx.emplace_back(u2[p]+1);\n if(v1[p]-1>=1) vy.emplace_back(v1[p]-1);\n if(v2[p]+1<=r) vy.emplace_back(v2[p]+1);\n }\n \n vx=compress(vx);\n vy=compress(vy);\n auto mx=dict(vx);\n auto my=dict(vy);\n\n // kokokara y yuusen\n auto idx=[&](Int cy,Int cx,Int f){return (f<<BS2)|(cy<<BS)|cx;};\n \n memset(hole,0,sizeof(hole)); \n for(Int p=0;p<m;p++)\n for(Int i=my[v1[p]];i<=my[v2[p]];i++)\n for(Int j=mx[u1[p]];j<=mx[u2[p]];j++)\n hole[idx(i,j,w[p])]=1;\n \n Int sy=vy.size(),sx=vx.size();\n \n auto dijkstra=\n [&](Int a)->void{ \n vector<Int> wy,wx;\n for(Int p=0;p<m;p++){\n if(w[p]<=z[a]) continue; \n wy.emplace_back(v1[p]);\n wy.emplace_back(v2[p]); \n wx.emplace_back(u1[p]);\n wx.emplace_back(u2[p]);\n }\n wy.emplace_back(y[a]);\n wx.emplace_back(x[a]);\n wy=compress(wy);\n wx=compress(wx);\n auto zy=dict(wy);\n auto zx=dict(wx);\n Int ty=zy.size(),tx=zx.size();\n \n vector<pair<Int, Int> > vs;\n {\n for(Int i=0;i<MAX;i++) dist[i]=INT_MAX;\n RadixHeap q;\n {\n Int v=idx(zy[y[a]],zx[x[a]],z[a]);\n dist[v]=0;\n q.push(dist[v],v);\n }\n \n while(!q.empty()){\n auto p=q.pop();\n Int v=p.second;\n if(dist[v]<p.first) continue;\n \n Int f=v>>BS2,i=(v>>BS)&msk,j=v&msk;\n Int ai=my[wy[i]],aj=mx[wx[j]];\n if(f==k){\n vs.emplace_back((ai<<BS)|aj,dist[v]);\n continue;\n }\n \n auto push=\n [&](Int ni,Int nj,Int nf){\n Int u=idx(ni,nj,nf);\n Int c=abs(wy[ni]-wy[i])+abs(wx[nj]-wx[j]);\n \n if(dist[u]>dist[v]+c){\n dist[u]=dist[v]+c; \n q.push(dist[u],u);\n }\n };\n \n if(hole[idx(ai,aj,f+1)]){\n push(i,j,f+1);\n continue;\n }\n \n if(i+1<ty) push(i+1,j,f);\n if(i-1>=0) push(i-1,j,f);\n if(j+1<tx) push(i,j+1,f);\n if(j-1>=0) push(i,j-1,f);\n }\n }\n { \n for(Int i=0;i<(sy<<BS);i++) add[i]=INT_MAX;\n RadixHeap q; \n for(auto p:vs){\n Int v=p.first,d=p.second;\n add[v]=d;\n q.push(add[v],v);\n }\n \n while(!q.empty()){\n auto p=q.pop();\n Int v=p.second;\n if(add[v]<p.first) continue;\n \n Int i=v>>BS,j=v&msk; \n auto push=\n [&](Int ni,Int nj){\n Int u=(ni<<BS)|nj;\n Int c=abs(vy[ni]-vy[i])+abs(vx[nj]-vx[j]); \n if(add[u]>add[v]+c){\n add[u]=add[v]+c; \n q.push(add[u],u);\n }\n };\n \n if(i+1<sy) push(i+1,j);\n if(i-1>=0) push(i-1,j);\n if(j+1<sx) push(i,j+1);\n if(j-1>=0) push(i,j-1);\n }\n }\n };\n \n memset(dp,0,sizeof(dp)); \n for(Int p=0;p<n;p++){\n dijkstra(p); \n for(Int i=0;i<sy;i++)\n for(Int j=0;j<sx;j++)\n dp[i][j]+=add[(i<<BS)|j];\n }\n \n long long ans=1e18;\n for(Int i=0;i<sy;i++)\n for(Int j=0;j<sx;j++)\n if(!hole[idx(i,j,k)]) chmin(ans,dp[i][j]);\n\n for(Int p=0;p<n;p++) ans+=(k-z[p])*100;\n cout<<ans<<endl; \n }\n return 0;\n}", "accuracy": 1, "time_ms": 5950, "memory_kb": 8208, "score_of_the_acc": -0.7608, "final_rank": 10 }, { "submission_id": "aoj_1607_3352789", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = signed;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nunordered_map<T, Int> dict(const vector<T> &v){\n unordered_map<T, Int> res;\n for(Int i=0;i<(Int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\npair< Int, Int > v[33][1<<18];\nInt cur[33];\n\nstruct RadixHeap\n{\n Int size, last;\n RadixHeap() : size(0), last(0) {\n for(Int i=0;i<33;i++) cur[i]=0;\n }\n\n bool empty() const { return size == 0; }\n\n inline Int getbit(Int a) {\n return a ? 32 - __builtin_clz(a) : 0;\n }\n\n void push(Int key, const Int &value) {\n ++size;\n Int uku=getbit(key^last);\n v[uku][cur[uku]++]=make_pair(key,value);\n }\n\n pair<Int, Int> pop() {\n if(!cur[0]) { \n Int idx = 1;\n while(!cur[idx]) ++idx;\n last=min_element(v[idx],v[idx]+cur[idx])->first;\n for(Int i=0;i<cur[idx];i++){\n auto &p=v[idx][i];\n Int uku=getbit(p.first^last);\n v[uku][cur[uku]++]=p;\n }\n cur[idx]=0;\n }\n --size;\n auto ret = v[0][--cur[0]];\n return ret;\n }\n};\n\nconst Int BS = 8, BS2 = BS * 2;\nconst Int msk = (1<<BS)-1;\nconst Int MAX = 12 * (1<<BS) * (1<<BS);\nInt dist[MAX];\nbool hole[MAX];\nlong long add[(1<<BS) * (1<<BS)];\nlong long dp[(1<<BS)][(1<<BS)];\n\n//INSERT ABOVE HERE\nsigned main(){\n Int n;\n while(cin>>n,n){\n Int m,k,r;\n cin>>m>>k>>r;\n vector<Int> x(n),y(n),z(n);\n for(Int i=0;i<n;i++) cin>>x[i]>>y[i]>>z[i];\n vector<Int> u1(m),v1(m),u2(m),v2(m),w(m);\n for(Int i=0;i<m;i++) cin>>u1[i]>>v1[i]>>u2[i]>>v2[i]>>w[i];\n \n vector<Int> vx(x),vy(y);\n for(Int p=0;p<m;p++){\n vx.emplace_back(u1[p]);\n vx.emplace_back(u2[p]);\n vy.emplace_back(v1[p]);\n vy.emplace_back(v2[p]);\n if(w[p]!=k) continue;\n if(u1[p]-1>=1) vx.emplace_back(u1[p]-1);\n if(u2[p]+1<=r) vx.emplace_back(u2[p]+1);\n if(v1[p]-1>=1) vy.emplace_back(v1[p]-1);\n if(v2[p]+1<=r) vy.emplace_back(v2[p]+1);\n }\n \n vx=compress(vx);\n vy=compress(vy);\n auto mx=dict(vx);\n auto my=dict(vy);\n\n // kokokara y yuusen\n auto idx=[&](Int cy,Int cx,Int f){return (f<<BS2)|(cy<<BS)|cx;};\n \n memset(hole,0,sizeof(hole)); \n for(Int p=0;p<m;p++)\n for(Int i=my[v1[p]];i<=my[v2[p]];i++)\n for(Int j=mx[u1[p]];j<=mx[u2[p]];j++)\n hole[idx(i,j,w[p])]=1;\n \n Int sy=vy.size(),sx=vx.size();\n \n auto dijkstra=\n [&](Int a)->void{ \n vector<Int> wy,wx;\n for(Int p=0;p<m;p++){\n if(w[p]<=z[a]) continue; \n wy.emplace_back(v1[p]);\n wy.emplace_back(v2[p]); \n wx.emplace_back(u1[p]);\n wx.emplace_back(u2[p]);\n }\n wy.emplace_back(y[a]);\n wx.emplace_back(x[a]);\n wy=compress(wy);\n wx=compress(wx);\n auto zy=dict(wy);\n auto zx=dict(wx);\n Int ty=zy.size(),tx=zx.size();\n \n vector<pair<Int, Int> > vs;\n {\n for(Int i=0;i<MAX;i++) dist[i]=INT_MAX;\n RadixHeap q;\n {\n Int v=idx(zy[y[a]],zx[x[a]],z[a]);\n dist[v]=0;\n q.push(dist[v],v);\n }\n \n while(!q.empty()){\n auto p=q.pop();\n Int v=p.second;\n if(dist[v]<p.first) continue;\n \n Int f=v>>BS2,i=(v>>BS)&msk,j=v&msk;\n Int ai=my[wy[i]],aj=mx[wx[j]];\n if(f==k){\n vs.emplace_back((ai<<BS)|aj,dist[v]);\n continue;\n }\n \n auto push=\n [&](Int ni,Int nj,Int nf){\n Int u=idx(ni,nj,nf);\n Int c=abs(wy[ni]-wy[i])+abs(wx[nj]-wx[j]);\n \n if(dist[u]>dist[v]+c){\n dist[u]=dist[v]+c; \n q.push(dist[u],u);\n }\n };\n \n if(hole[idx(ai,aj,f+1)]){\n push(i,j,f+1);\n continue;\n }\n \n if(i+1<ty) push(i+1,j,f);\n if(i-1>=0) push(i-1,j,f);\n if(j+1<tx) push(i,j+1,f);\n if(j-1>=0) push(i,j-1,f);\n }\n }\n { \n for(Int i=0;i<(sy<<BS);i++) add[i]=INT_MAX;\n RadixHeap q; \n for(auto p:vs){\n Int v=p.first,d=p.second;\n add[v]=d;\n q.push(add[v],v);\n }\n \n while(!q.empty()){\n auto p=q.pop();\n Int v=p.second;\n if(add[v]<p.first) continue;\n \n Int i=v>>BS,j=v&msk; \n auto push=\n [&](Int ni,Int nj){\n Int u=(ni<<BS)|nj;\n Int c=abs(vy[ni]-vy[i])+abs(vx[nj]-vx[j]); \n if(add[u]>add[v]+c){\n add[u]=add[v]+c; \n q.push(add[u],u);\n }\n };\n \n if(i+1<sy) push(i+1,j);\n if(i-1>=0) push(i-1,j);\n if(j+1<sx) push(i,j+1);\n if(j-1>=0) push(i,j-1);\n }\n }\n };\n \n memset(dp,0,sizeof(dp)); \n for(Int p=0;p<n;p++){\n dijkstra(p); \n for(Int i=0;i<sy;i++)\n for(Int j=0;j<sx;j++)\n dp[i][j]+=add[(i<<BS)|j];\n }\n \n long long ans=1e18;\n for(Int i=0;i<sy;i++)\n for(Int j=0;j<sx;j++)\n if(!hole[idx(i,j,k)]) chmin(ans,dp[i][j]);\n\n for(Int p=0;p<n;p++) ans+=(k-z[p])*100;\n cout<<ans<<endl; \n }\n return 0;\n}", "accuracy": 1, "time_ms": 6610, "memory_kb": 8224, "score_of_the_acc": -0.8482, "final_rank": 11 }, { "submission_id": "aoj_1607_3352783", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = signed;\ntemplate<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}\ntemplate<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}\n\ntemplate<typename T>\nvector<T> compress(vector<T> v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n return v;\n}\n\ntemplate<typename T>\nmap<T, Int> dict(const vector<T> &v){\n map<T, Int> res;\n for(Int i=0;i<(Int)v.size();i++)\n res[v[i]]=i;\n return res;\n}\n\npair< Int, Int > v[33][1<<18];\nInt cur[33];\n\nstruct RadixHeap\n{\n Int size, last;\n RadixHeap() : size(0), last(0) {\n for(Int i=0;i<33;i++) cur[i]=0;\n }\n\n bool empty() const { return size == 0; }\n\n inline Int getbit(Int a) {\n return a ? 32 - __builtin_clz(a) : 0;\n }\n\n void push(Int key, const Int &value) {\n ++size;\n Int uku=getbit(key^last);\n v[uku][cur[uku]++]=make_pair(key,value);\n }\n\n pair<Int, Int> pop() {\n if(!cur[0]) { \n Int idx = 1;\n while(!cur[idx]) ++idx;\n last=min_element(v[idx],v[idx]+cur[idx])->first;\n for(Int i=0;i<cur[idx];i++){\n auto &p=v[idx][i];\n Int uku=getbit(p.first^last);\n v[uku][cur[uku]++]=p;\n }\n cur[idx]=0;\n }\n --size;\n auto ret = v[0][--cur[0]];\n return ret;\n }\n};\n\nconst Int BS = 8, BS2 = BS * 2;\nconst Int msk = (1<<BS)-1;\nconst Int MAX = 12 * (1<<BS) * (1<<BS);\nInt dist[MAX];\nbool hole[MAX];\nlong long add[(1<<BS) * (1<<BS)];\nlong long dp[(1<<BS)][(1<<BS)];\n\n//INSERT ABOVE HERE\nsigned main(){\n Int n;\n while(cin>>n,n){\n Int m,k,r;\n cin>>m>>k>>r;\n vector<Int> x(n),y(n),z(n);\n for(Int i=0;i<n;i++) cin>>x[i]>>y[i]>>z[i];\n vector<Int> u1(m),v1(m),u2(m),v2(m),w(m);\n for(Int i=0;i<m;i++) cin>>u1[i]>>v1[i]>>u2[i]>>v2[i]>>w[i];\n \n vector<Int> vx(x),vy(y);\n for(Int p=0;p<m;p++){\n vx.emplace_back(u1[p]);\n vx.emplace_back(u2[p]);\n vy.emplace_back(v1[p]);\n vy.emplace_back(v2[p]);\n if(w[p]!=k) continue;\n if(u1[p]-1>=1) vx.emplace_back(u1[p]-1);\n if(u2[p]+1<=r) vx.emplace_back(u2[p]+1);\n if(v1[p]-1>=1) vy.emplace_back(v1[p]-1);\n if(v2[p]+1<=r) vy.emplace_back(v2[p]+1);\n }\n \n vx=compress(vx);\n vy=compress(vy);\n auto mx=dict(vx);\n auto my=dict(vy);\n\n // kokokara y yuusen\n auto idx=[&](Int cy,Int cx,Int f){return (f<<BS2)|(cy<<BS)|cx;};\n \n memset(hole,0,sizeof(hole)); \n for(Int p=0;p<m;p++)\n for(Int i=my[v1[p]];i<=my[v2[p]];i++)\n for(Int j=mx[u1[p]];j<=mx[u2[p]];j++)\n hole[idx(i,j,w[p])]=1;\n \n Int sy=vy.size(),sx=vx.size();\n \n auto dijkstra=\n [&](Int a)->void{ \n vector<Int> wy,wx;\n for(Int p=0;p<m;p++){\n if(w[p]<=z[a]) continue; \n wy.emplace_back(v1[p]);\n wy.emplace_back(v2[p]); \n wx.emplace_back(u1[p]);\n wx.emplace_back(u2[p]);\n }\n wy.emplace_back(y[a]);\n wx.emplace_back(x[a]);\n wy=compress(wy);\n wx=compress(wx);\n auto zy=dict(wy);\n auto zx=dict(wx);\n Int ty=zy.size(),tx=zx.size();\n \n vector<pair<Int, Int> > vs;\n {\n for(Int i=0;i<MAX;i++) dist[i]=INT_MAX;\n RadixHeap q;\n {\n Int v=idx(zy[y[a]],zx[x[a]],z[a]);\n dist[v]=0;\n q.push(dist[v],v);\n }\n \n while(!q.empty()){\n auto p=q.pop();\n Int v=p.second;\n if(dist[v]<p.first) continue;\n \n Int f=v>>BS2,i=(v>>BS)&msk,j=v&msk;\n Int ai=my[wy[i]],aj=mx[wx[j]];\n if(f==k){\n vs.emplace_back((ai<<BS)|aj,dist[v]);\n continue;\n }\n \n auto push=\n [&](Int ni,Int nj,Int nf){\n Int u=idx(ni,nj,nf);\n Int c=abs(wy[ni]-wy[i])+abs(wx[nj]-wx[j]);\n \n if(dist[u]>dist[v]+c){\n dist[u]=dist[v]+c; \n q.push(dist[u],u);\n }\n };\n \n if(hole[idx(ai,aj,f+1)]){\n push(i,j,f+1);\n continue;\n }\n \n if(i+1<ty) push(i+1,j,f);\n if(i-1>=0) push(i-1,j,f);\n if(j+1<tx) push(i,j+1,f);\n if(j-1>=0) push(i,j-1,f);\n }\n }\n { \n for(Int i=0;i<(sy<<BS);i++) add[i]=INT_MAX;\n RadixHeap q; \n for(auto p:vs){\n Int v=p.first,d=p.second;\n add[v]=d;\n q.push(add[v],v);\n }\n \n while(!q.empty()){\n auto p=q.pop();\n Int v=p.second;\n if(add[v]<p.first) continue;\n \n Int i=v>>BS,j=v&msk; \n auto push=\n [&](Int ni,Int nj){\n Int u=(ni<<BS)|nj;\n Int c=abs(vy[ni]-vy[i])+abs(vx[nj]-vx[j]); \n if(add[u]>add[v]+c){\n add[u]=add[v]+c; \n q.push(add[u],u);\n }\n };\n \n if(i+1<sy) push(i+1,j);\n if(i-1>=0) push(i-1,j);\n if(j+1<sx) push(i,j+1);\n if(j-1>=0) push(i,j-1);\n }\n }\n };\n \n memset(dp,0,sizeof(dp)); \n for(Int p=0;p<n;p++){\n dijkstra(p); \n for(Int i=0;i<sy;i++)\n for(Int j=0;j<sx;j++)\n dp[i][j]+=add[(i<<BS)|j];\n }\n \n long long ans=1e18;\n for(Int i=0;i<sy;i++)\n for(Int j=0;j<sx;j++)\n if(!hole[idx(i,j,k)]) chmin(ans,dp[i][j]);\n\n for(Int p=0;p<n;p++) ans+=(k-z[p])*100;\n cout<<ans<<endl; \n }\n return 0;\n}", "accuracy": 1, "time_ms": 7880, "memory_kb": 8192, "score_of_the_acc": -1.0159, "final_rank": 15 }, { "submission_id": "aoj_1607_2360055", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = INT_MAX;\n\ntemplate <class T>\npair<vector<T>, map<T, int> > compress(vector<T>& a) {\n\tvector<T> v;\n\tfor(int i = 0; i < a.size(); i++) v.push_back(a[i]);\n\tsort(v.begin(), v.end());\n\tv.erase(unique(v.begin(), v.end()), v.end());\n\tmap<T, int> m;\n\tfor(int i = 0; i < v.size(); i++) m[v[i]] = i;\n\treturn { v, m };\n}\n\nstruct edge {\n\tint x, y, z;\n\tint cost;\n};\n\nstruct S {\n\tint x, y, z;\n\tint cost;\n\n\tbool operator <(const S& x) const{\n\t\treturn cost > x.cost;\n\t}\n};\n\nusing P = pair<int, int>;\nvector<edge> G[300][300][11];\nint dist[300][300][11];\nint sum[300][300];\n\nint dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tint N, M, K, R;\n\tvector<int> X(100), Y(100), Z(100), xl(50), xr(50), yt(50), yb(50), h(50);\n\twhile(cin >> N, N) {\n\t\tcin >> M >> K >> R;\n\n\t\tvector<int> xs, ys;\n\t\tfor(int i = 0; i < N; i++) {\n\t\t\tcin >> X[i] >> Y[i] >> Z[i];\n\t\t\txs.push_back(X[i]);\n\t\t\tys.push_back(Y[i]);\n\t\t}\n\t\tfor(int i = 0; i < M; i++) {\n\t\t\tcin >> xl[i] >> yt[i] >> xr[i] >> yb[i] >> h[i];\n\t\t\txs.push_back(xl[i]);\n\t\t\txs.push_back(xr[i]);\n\t\t\tys.push_back(yt[i]);\n\t\t\tys.push_back(yb[i]);\n\t\t\tif(h[i] == K) {\n\t\t\t\tif(xl[i] - 1 >= 1) xs.push_back(xl[i] - 1);\n\t\t\t\tif(xr[i] + 1 <= R) xs.push_back(xr[i] + 1);\n\t\t\t\tif(yt[i] - 1 >= 1) ys.push_back(yt[i] - 1);\n\t\t\t\tif(yb[i] + 1 <= R) ys.push_back(yb[i] + 1);\n\t\t\t}\n\t\t}\n\n\t\tauto zipX = compress(xs);\n\t\tauto zipY = compress(ys);\n\n\t\tint XN = zipX.first.size();\n\t\tint YN = zipY.first.size();\n\n\t\tfor(int k = 1; k <= K; k++) {\n\t\t\tfor(int i = 0; i < XN; i++) {\n\t\t\t\tfor(int j = 0; j < YN; j++) {\n\t\t\t\t\tG[i][j][k].clear();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 1; k <= K; k++) {\n\t\t\tfor(int i = 0; i < XN; i++) {\n\t\t\t\tfor(int j = 0; j < YN; j++) {\n\t\t\t\t\tint cx = zipX.first[i];\n\t\t\t\t\tint cy = zipY.first[j];\n\t\t\t\t\tif(k == K) {\n\t\t\t\t\t\tfor(int l = 0; l < 4; l++) {\n\t\t\t\t\t\t\tif(i + dx[l] < 0 || XN <= i + dx[l]) continue;\n\t\t\t\t\t\t\tif(j + dy[l] < 0 || YN <= j + dy[l]) continue;\n\t\t\t\t\t\t\tint nx = zipX.first[i + dx[l]];\n\t\t\t\t\t\t\tint ny = zipY.first[j + dy[l]];\n\t\t\t\t\t\t\tint cost = abs(cx - nx) + abs(cy - ny);\n\t\t\t\t\t\t\tG[i][j][k].push_back({ i + dx[l], j + dy[l], k, cost });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tfor(int m = 0; m < M; m++) {\n\t\t\t\t\t\t\tif(k + 1 != h[m]) continue;\n\t\t\t\t\t\t\tint cost = INF;\n\t\t\t\t\t\t\tint ii = 0, jj = 0;\n\t\t\t\t\t\t\tint li = zipX.second[xl[m]];\n\t\t\t\t\t\t\tint ri = zipX.second[xr[m]];\n\t\t\t\t\t\t\tint tj = zipY.second[yt[m]];\n\t\t\t\t\t\t\tint bj = zipY.second[yb[m]];\n\t\t\t\t\t\t\tif(li <= i && i <= ri && tj <= j && j <= bj) {\n\t\t\t\t\t\t\t\tif(cost > 100) {\n\t\t\t\t\t\t\t\t\tcost = 100;\n\t\t\t\t\t\t\t\t\tii = i, jj = j;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse if(li <= i && i <= ri) {\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cy - yt[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cy - yt[m]);\n\t\t\t\t\t\t\t\t\tii = i, jj = tj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cy - yb[m])) {\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cy - yb[m]);\n\t\t\t\t\t\t\t\t\tii = i, jj = bj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse if(tj <= j && j <= bj) {\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xl[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xl[m]);\n\t\t\t\t\t\t\t\t\tii = li, jj = j;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xr[m])) {\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xr[m]);\n\t\t\t\t\t\t\t\t\tii = ri, jj = j;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xl[m]) + abs(cy - yt[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xl[m]) + abs(cy - yt[m]);\n\t\t\t\t\t\t\t\t\tii = li, jj = tj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xr[m]) + abs(cy - yt[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xr[m]) + abs(cy - yt[m]);\n\t\t\t\t\t\t\t\t\tii = ri, jj = tj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xl[m]) + abs(cy - yb[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xl[m]) + abs(cy - yb[m]);\n\t\t\t\t\t\t\t\t\tii = li, jj = bj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xr[m]) + abs(cy - yb[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xr[m]) + abs(cy - yb[m]);\n\t\t\t\t\t\t\t\t\tii = ri, jj = bj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tG[i][j][k].push_back({ ii, jj, k + 1, cost });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tvector<P> goal;\n\t\tfor(int i = 0; i < XN; i++) {\n\t\t\tfor(int j = 0; j < YN; j++) {\n\t\t\t\tint cx = zipX.first[i];\n\t\t\t\tint cy = zipY.first[j];\n\t\t\t\tbool ok = true;\n\t\t\t\tfor(int m = 0; m < M; m++) {\n\t\t\t\t\tif(K == h[m] && xl[m] <= cx && cx <= xr[m] && yt[m] <= cy && cy <= yb[m]) {\n\t\t\t\t\t\tok = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(ok) goal.push_back({ i, j });\n\t\t\t}\n\t\t}\n\n\t\tmemset(sum, 0, sizeof sum);\n\t\tfor(int n = 0; n < N; n++) {\n\t\t\tfill((int*)begin(dist), (int*)end(dist), INF);\n\t\t\tdist[zipX.second[X[n]]][zipY.second[Y[n]]][Z[n]] = 0;\n\t\t\tpriority_queue<S> q;\n\t\t\tq.push({ zipX.second[X[n]],zipY.second[Y[n]], Z[n], 0 });\n\t\t\twhile(q.size()) {\n\t\t\t\tS s = q.top();\n\t\t\t\tq.pop();\n\t\t\t\tif(dist[s.x][s.y][s.z] < s.cost) continue;\n\t\t\t\tfor(auto& e : G[s.x][s.y][s.z]) {\n\t\t\t\t\tint ncost = s.cost + e.cost;\n\t\t\t\t\tif(dist[e.x][e.y][e.z] > ncost){\n\t\t\t\t\t\tdist[e.x][e.y][e.z] = ncost;\n\t\t\t\t\t\tq.push({ e.x, e.y, e.z, ncost });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(auto& g : goal) {\n\t\t\t\tsum[g.first][g.second] += dist[g.first][g.second][K];\n\t\t\t}\n\t\t}\n\t\tint ans = INF;\n\t\tfor(auto& g : goal) {\n\t\t\tans = min(ans, sum[g.first][g.second]);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 7980, "memory_kb": 157944, "score_of_the_acc": -2, "final_rank": 17 }, { "submission_id": "aoj_1607_2360041", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n//typedef long long int;\nconst int INF = INT_MAX;\n\ntemplate <class T>\npair<vector<T>, map<T, int> > compress(vector<T>& a) {\n\tvector<T> v;\n\tfor(int i = 0; i < a.size(); i++) v.push_back(a[i]);\n\tsort(v.begin(), v.end());\n\tv.erase(unique(v.begin(), v.end()), v.end());\n\tmap<T, int> m;\n\tfor(int i = 0; i < v.size(); i++) m[v[i]] = i;\n\treturn { v, m };\n}\n\nstruct edge {\n\tint x, y, z;\n\tint cost;\n};\n\nstruct S {\n\tint x, y, z;\n\tint cost;\n\n\tbool operator <(const S& x) const{\n\t\treturn cost > x.cost;\n\t}\n};\n\nusing P = pair<int, int>;\nvector<edge> G[300][300][11];\nint dist[300][300][11];\nint sum[300][300];\n\nint dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };\n\nint main() {\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\n\tint N, M, K, R;\n\tvector<int> X(100), Y(100), Z(100), xl(50), xr(50), yt(50), yb(50), h(50);\n\twhile(cin >> N, N) {\n\t\tcin >> M >> K >> R;\n\n\t\tvector<int> xs, ys;\n\t\tfor(int i = 0; i < N; i++) {\n\t\t\tcin >> X[i] >> Y[i] >> Z[i];\n\t\t\txs.push_back(X[i]);\n\t\t\tys.push_back(Y[i]);\n\t\t}\n\t\tfor(int i = 0; i < M; i++) {\n\t\t\tcin >> xl[i] >> yt[i] >> xr[i] >> yb[i] >> h[i];\n\t\t\txs.push_back(xl[i]);\n\t\t\txs.push_back(xr[i]);\n\t\t\tys.push_back(yt[i]);\n\t\t\tys.push_back(yb[i]);\n\t\t\tif(h[i] == K) {\n\t\t\t\tif(xl[i] - 1 >= 1) xs.push_back(xl[i] - 1);\n\t\t\t\tif(xr[i] + 1 <= R) xs.push_back(xr[i] + 1);\n\t\t\t\tif(yt[i] - 1 >= 1) ys.push_back(yt[i] - 1);\n\t\t\t\tif(yb[i] + 1 <= R) ys.push_back(yb[i] + 1);\n\t\t\t}\n\t\t}\n\n\t\tauto zipX = compress(xs);\n\t\tauto zipY = compress(ys);\n\n\t\tint XN = zipX.first.size();\n\t\tint YN = zipY.first.size();\n\n\t\tfor(int k = 1; k <= K; k++) {\n\t\t\tfor(int i = 0; i < XN; i++) {\n\t\t\t\tfor(int j = 0; j < YN; j++) {\n\t\t\t\t\tG[i][j][k].clear();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int k = 1; k <= K; k++) {\n\t\t\tfor(int i = 0; i < XN; i++) {\n\t\t\t\tfor(int j = 0; j < YN; j++) {\n\t\t\t\t\tint cx = zipX.first[i];\n\t\t\t\t\tint cy = zipY.first[j];\n\t\t\t\t\tif(k == K) {\n\t\t\t\t\t\tfor(int l = 0; l < 4; l++) {\n\t\t\t\t\t\t\tif(i + dx[l] < 0 || XN <= i + dx[l]) continue;\n\t\t\t\t\t\t\tif(j + dy[l] < 0 || YN <= j + dy[l]) continue;\n\t\t\t\t\t\t\tint nx = zipX.first[i + dx[l]];\n\t\t\t\t\t\t\tint ny = zipY.first[j + dy[l]];\n\t\t\t\t\t\t\tint cost = abs(cx - nx) + abs(cy - ny);\n\t\t\t\t\t\t\tG[i][j][k].push_back({ i + dx[l], j + dy[l], k, cost });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\n\t\t\t\t\t\tfor(int m = 0; m < M; m++) {\n\t\t\t\t\t\t\tif(k + 1 != h[m]) continue;\n\t\t\t\t\t\t\tint cost = INF;\n\t\t\t\t\t\t\tint ii = 0, jj = 0;\n\t\t\t\t\t\t\tint li = zipX.second[xl[m]];\n\t\t\t\t\t\t\tint ri = zipX.second[xr[m]];\n\t\t\t\t\t\t\tint tj = zipY.second[yt[m]];\n\t\t\t\t\t\t\tint bj = zipY.second[yb[m]];\n\t\t\t\t\t\t\tif(li <= i && i <= ri && tj <= j && j <= bj) {\n\t\t\t\t\t\t\t\tif(cost > 100) {\n\t\t\t\t\t\t\t\t\tcost = 100;\n\t\t\t\t\t\t\t\t\tii = i, jj = j;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse if(li <= i && i <= ri) {\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cy - yt[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cy - yt[m]);\n\t\t\t\t\t\t\t\t\tii = i, jj = tj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cy - yb[m])) {\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cy - yb[m]);\n\t\t\t\t\t\t\t\t\tii = i, jj = bj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse if(tj <= j && j <= bj) {\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xl[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xl[m]);\n\t\t\t\t\t\t\t\t\tii = li, jj = j;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xr[m])) {\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xr[m]);\n\t\t\t\t\t\t\t\t\tii = ri, jj = j;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xl[m]) + abs(cy - yt[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xl[m]) + abs(cy - yt[m]);\n\t\t\t\t\t\t\t\t\tii = li, jj = tj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xr[m]) + abs(cy - yt[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xr[m]) + abs(cy - yt[m]);\n\t\t\t\t\t\t\t\t\tii = ri, jj = tj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xl[m]) + abs(cy - yb[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xl[m]) + abs(cy - yb[m]);\n\t\t\t\t\t\t\t\t\tii = li, jj = bj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif(cost > 100 + abs(cx - xr[m]) + abs(cy - yb[m])){\n\t\t\t\t\t\t\t\t\tcost = 100 + abs(cx - xr[m]) + abs(cy - yb[m]);\n\t\t\t\t\t\t\t\t\tii = ri, jj = bj;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tG[i][j][k].push_back({ ii, jj, k + 1, cost });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tvector<P> goal;\n\t\tfor(int i = 0; i < XN; i++) {\n\t\t\tfor(int j = 0; j < YN; j++) {\n\t\t\t\tint cx = zipX.first[i];\n\t\t\t\tint cy = zipY.first[j];\n\t\t\t\tbool ok = true;\n\t\t\t\tfor(int m = 0; m < M; m++) {\n\t\t\t\t\tif(K == h[m] && xl[m] <= cx && cx <= xr[m] && yt[m] <= cy && cy <= yb[m]) {\n\t\t\t\t\t\tok = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(ok) goal.push_back({ i, j });\n\t\t\t}\n\t\t}\n\n\t\tmemset(sum, 0, sizeof sum);\n\t\tfor(int n = 0; n < N; n++) {\n\t\t\tfor(int k = 1; k <= K; k++) {\n\t\t\t\tfor(int i = 0; i < XN; i++) {\n\t\t\t\t\tfor(int j = 0; j < YN; j++) {\n\t\t\t\t\t\tdist[i][j][k] = INF;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdist[zipX.second[X[n]]][zipY.second[Y[n]]][Z[n]] = 0;\n\t\t\tpriority_queue<S> q;\n\t\t\tq.push({ zipX.second[X[n]],zipY.second[Y[n]], Z[n], 0 });\n\t\t\twhile(q.size()) {\n\t\t\t\tS s = q.top();\n\t\t\t\tq.pop();\n\t\t\t\tif(dist[s.x][s.y][s.z] < s.cost) continue;\n\t\t\t\t//cout << s.cost << endl;\n\t\t\t\tfor(auto& e : G[s.x][s.y][s.z]) {\n\t\t\t\t\tint ncost = s.cost + e.cost;\n\t\t\t\t\tif(dist[e.x][e.y][e.z] > ncost){\n\t\t\t\t\t\tdist[e.x][e.y][e.z] = ncost;\n\t\t\t\t\t\tq.push({ e.x, e.y, e.z, ncost });\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(auto& g : goal) {\n\t\t\t\tsum[g.first][g.second] += dist[g.first][g.second][K];\n\t\t\t}\n\t\t}\n\t\tint ans = INF;\n\t\tfor(auto& g : goal) {\n\t\t\tans = min(ans, sum[g.first][g.second]);\n\t\t}\n\t\tcout << ans << endl;\n\t}\n\n}", "accuracy": 1, "time_ms": 7230, "memory_kb": 156840, "score_of_the_acc": -1.8936, "final_rank": 16 }, { "submission_id": "aoj_1607_2127943", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }\n\ntypedef pair<int, int> point;\n#define X first\n#define Y second\n\nstruct state {\n\tpoint pos;\n\tint cost;\n\n\tstate(int x, int y, int cost_):pos(x, y), cost(cost_) {}\n\n\tbool operator<(const state &s) const {\n\t\treturn cost < s.cost;\n\t}\n};\n\ninline bool in_range(int l, int r, int x) {\n\treturn l <= x && x <= r;\n}\n\ninline int calc_best(int l, int r, int x) {\n\treturn x < l ? l : r < x ? r : x;\n}\n\ninline int dist(const point &a, const point &b) {\n\treturn abs(a.X - b.X) + abs(a.Y - b.Y);\n}\n\nvoid reduce(vector<state> &states) {\n\tsort(begin(states), end(states));\n\n\tconst int n = states.size();\n\tvector<bool> ok(n, true);\n\tint last = 0;\n\n\tfor(int i = 0; i < n; ++i) {\n\t\tif(ok[i]) {\n\t\t\tfor(int j = i + 1; j < n; ++j) {\n\t\t\t\tif(ok[j] && states[j].cost - states[i].cost >= dist(states[i].pos, states[j].pos)) {\n\t\t\t\t\tok[j] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tstates[last++] = states[i];\n\t\t}\n\t}\n\n\tstates.erase(begin(states) + last, end(states));\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\tfor(int n; cin >> n && n;) {\n\t\tint m, k, r;\n\t\tcin >> m >> k >> r;\n\n\t\tvector<int> cx, cy;\n\t\tvector<int> rx(n), ry(n), rz(n);\n\n\t\tfor(int i = 0; i < n; ++i) {\n\t\t\tcin >> rx[i] >> ry[i] >> rz[i];\n\t\t\tcx.emplace_back(rx[i]);\n\t\t\tcy.emplace_back(ry[i]);\n\t\t\t--rz[i];\n\t\t}\n\n\t\tvector<vector<pair<point, point>>> holes(k);\n\t\tfor(int i = 0; i < m; ++i) {\n\t\t\tint x1, y1, x2, y2, w;\n\t\t\tcin >> x1 >> y1 >> x2 >> y2 >> w;\n\t\t\tholes[w - 1].emplace_back(point(x1, y1), point(x2, y2));\n\n\t\t\tif(w == k) {\n\t\t\t\tif(x1 > 1) cx.emplace_back(x1 - 1);\n\t\t\t\tif(x2 < r) cx.emplace_back(x2 + 1);\n\t\t\t\tif(y1 > 1) cy.emplace_back(y1 - 1);\n\t\t\t\tif(y2 < r) cy.emplace_back(y2 + 1);\n\t\t\t}\n\n\t\t\tcx.emplace_back(x1);\n\t\t\tcx.emplace_back(x2);\n\t\t\tcy.emplace_back(y1);\n\t\t\tcy.emplace_back(y2);\n\t\t}\n\n\t\tvector<state> candidate;\n\t\tcandidate.reserve(cx.size() * cy.size());\n\t\tsort(begin(cx), end(cx));\n\t\tsort(begin(cy), end(cy));\n\t\tcx.erase(unique(begin(cx), end(cx)), end(cx));\n\t\tcy.erase(unique(begin(cy), end(cy)), end(cy));\n\n\t\tfor(const auto &x : cx) {\n\t\t\tfor(const auto &y : cy) {\n\t\t\t\tfor(const auto &hole : holes.back()) {\n\t\t\t\t\tif(in_range(hole.first.X, hole.second.X, x) && in_range(hole.first.Y, hole.second.Y, y)) {\n\t\t\t\t\t\tgoto next_candidate;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcandidate.emplace_back(x, y, 0);\n\t\t\tnext_candidate:;\n\t\t\t}\n\t\t}\n\n\t\tint ans = 0;\n\t\tfor(int r_idx = 0; r_idx < n; ++r_idx) {\n\t\t\tans += (k - (rz[r_idx] + 1)) * 100;\n\n\t\t\tvector<state> dp(1, state(rx[r_idx], ry[r_idx], 0));\n\n\t\t\tfor(int i = rz[r_idx] + 1; i < k; ++i) {\n\t\t\t\tvector<state> next_dp;\n\n\t\t\t\tfor(const auto &s : dp) {\n\t\t\t\t\tfor(const auto &hole : holes[i]) {\n\t\t\t\t\t\tconst int x = calc_best(hole.first.X, hole.second.X, s.pos.X);\n\t\t\t\t\t\tconst int y = calc_best(hole.first.Y, hole.second.Y, s.pos.Y);\n\t\t\t\t\t\tnext_dp.emplace_back(x, y, s.cost + dist(s.pos, make_pair(x, y)));\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tdp = move(next_dp);\n\t\t\t\treduce(dp);\n\t\t\t}\n\n\t\t\tfor(auto &c : candidate) {\n\t\t\t\tint min_cost = INT_MAX;\n\n\t\t\t\tfor(const auto &e : dp) {\n\t\t\t\t\tchmin(min_cost, e.cost + dist(c.pos, e.pos));\n\t\t\t\t}\n\t\t\t\tc.cost += min_cost;\n\t\t\t}\n\t\t}\n\n\t\tans += min_element(begin(candidate), end(candidate))->cost;\n\t\tcout << ans << endl;\n\t}\n\n\treturn EXIT_SUCCESS;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 3692, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1607_1902970", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <unordered_map>\n#include <cstdlib>\n#include <queue>\n#include <utility>\nusing namespace std;\n\ntypedef long long LL;\n\nconst LL INF = 1LL << 50;\n\nstruct hole;\n\nvector<int> xs, ys;\nvector<vector<hole>> vhls;\nint szx, szy;\n\ninline int encode(int x, int y){\n\treturn x * szy + y;\n}\ninline void decode(int e, int &x, int &y){\n\tx = e / szy;\n\ty = e % szy;\n}\n\nstruct robot{\n\tint x, y, z;\n\trobot(int x_, int y_, int z_)\n\t: x(x_), y(y_), z(z_) {\n\t\txs.push_back(x);\n\t\tys.push_back(y);\n\t}\n};\n\nstruct hole{\n\tint xl, yl, xr, yr;\n\n\thole(int u1, int v1, int u2, int v2, int s)\n\t: xl(u1), yl(v1), xr(u2), yr(v2)\n\t{\n\t\txs.push_back(u1);\n\t\txs.push_back(u2);\n\t\tys.push_back(v1);\n\t\tys.push_back(v2);\n\n\t\tif(s >= 0){\n\t\t\tif(u1 > 1){ xs.push_back(u1 - 1); }\n\t\t\tif(u2 < s){ xs.push_back(u2 + 1); }\n\n\t\t\tif(v1 > 1){ ys.push_back(v1 - 1); }\n\t\t\tif(v2 < s){ ys.push_back(v2 + 1); }\n\t\t}\n\t}\n\n\tbool in(int x, int y) const{\n\t\treturn xl <= x && x <= xr && yl <= y && y <= yr;\n\t}\n\n\tvoid nearest(int x, int y, int &nx, int &ny) const{\n\t\tif(x < xl){ nx = xl; }\n\t\telse if(x > xr){ nx = xr; }\n\t\telse{ nx = x; }\n\n\t\tif(y < yl){ ny = yl; }\n\t\telse if(y > yr){ ny = yr; }\n\t\telse{ ny = y; }\n\t}\n};\n\nstruct cmpfst{\n\ttemplate <class T, class U>\n\tbool operator() (const pair<T,U> &p, const pair<T,U> &q) const{\n\t\treturn p.first < q.first;\n\t}\n};\n\nvoid compress(const vector<int> &v, int &t){\n\tt = lower_bound(v.begin(), v.end(), t) - v.begin();\n}\n\nvector<LL> calc(const robot &rb){\n\tunordered_map<int,LL> mp1, mp2;\n\tmp1.emplace(encode(rb.x, rb.y), 0);\n\n\tint hg = vhls.size() - 1;\n\tfor(int z = rb.z + 1; z <= hg; ++z){\n\t\tfor(auto p : mp1){\n\t\t\tint x, y, nx, ny;\n\t\t\tdecode(p.first, x, y);\n\t\t\tfor(const auto &h : vhls[z]){\n\t\t\t\th.nearest(x, y, nx, ny);\n\t\t\t\tLL &t = mp2.emplace(encode(nx, ny), INF).first->second;\n\t\t\t\tt = min(t, p.second + abs(xs[x] - xs[nx]) + abs(ys[y] - ys[ny]) + 100);\n\t\t\t}\n\t\t}\n\t\tmp1.swap(mp2);\n\t\tmp2.clear();\n\t}\n\n\ttypedef pair<LL,int> pli;\n\n\tvector<LL> ret(szx * szy, INF);\n\tpriority_queue<pli,vector<pli>,cmpfst> pq;\n\tfor(auto p : mp1){\n\t\tret[p.first] = p.second;\n\t\tpq.emplace(-p.second, p.first);\n\t}\n\n\tint dd[5] = {0, -1, 0, 1, 0};\n\n\twhile(!pq.empty()){\n\t\tauto p = pq.top();\n\t\tpq.pop();\n\t\tif(ret[p.second] != -p.first){ continue; }\n\t\tint x, y;\n\t\tdecode(p.second, x, y);\n\t\tfor(int i = 0; i < 4; ++i){\n\t\t\tint nx = x + dd[i];\n\t\t\tint ny = y + dd[i + 1];\n\t\t\tif(nx < 0 || nx >= szx || ny < 0 || ny >= szy){ continue; }\n\t\t\tint e = encode(nx, ny);\n\t\t\tLL c = -p.first + abs(xs[x] - xs[nx]) + abs(ys[y] - ys[ny]);\n\t\t\tif(ret[e] > c){\n\t\t\t\tret[e] = c;\n\t\t\t\tpq.emplace(-c, e);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nint main(){\n\tios::sync_with_stdio(false);\n\n\tint n, m, k, s;\n\n\tvector<robot> rbs;\n\trbs.reserve(100);\n\n\twhile(cin >> n >> m >> k >> s){\n\t\txs = ys = {1, s};\n\n\t\trbs.clear();\n\t\tvhls.clear();\n\t\tvhls.resize(k + 1);\n\n\t\tfor(int i = 0; i < n; ++i){\n\t\t\tint x, y, z;\n\t\t\tcin >> x >> y >> z;\n\t\t\trbs.emplace_back(x, y, z);\n\t\t}\n\t\tfor(int i = 0; i < m; ++i){\n\t\t\tint u1, v1, u2, v2, w;\n\t\t\tcin >> u1 >> v1 >> u2 >> v2 >> w;\n\t\t\tvhls[w].emplace_back(u1, v1, u2, v2, w == k ? s : -1);\n\t\t}\n\n\t\tsort(xs.begin(), xs.end());\n\t\txs.erase(unique(xs.begin(), xs.end()), xs.end());\n\t\tsort(ys.begin(), ys.end());\n\t\tys.erase(unique(ys.begin(), ys.end()), ys.end());\n\t\tszx = xs.size();\n\t\tszy = ys.size();\n\n\t\tfor(auto &r : rbs){\n\t\t\tcompress(xs, r.x);\n\t\t\tcompress(ys, r.y);\n\t\t}\n\t\tfor(auto &hls : vhls){\n\t\t\tfor(auto &h : hls){\n\t\t\t\tcompress(xs, h.xl);\n\t\t\t\tcompress(xs, h.xr);\n\t\t\t\tcompress(ys, h.yl);\n\t\t\t\tcompress(ys, h.yr);\n\t\t\t}\n\t\t}\n\n\t\tvector<vector<LL>> tms;\n\t\tfor(int i = 0; i < n; ++i){\n\t\t\ttms.emplace_back(calc(rbs[i]));\n\t\t}\n\n\t\tLL ans = INF;\n\t\tfor(int x = 0; x < szx; ++x){\n\t\t\tfor(int y = 0; y < szy; ++y){\n\t\t\t\tbool ok = true;\n\t\t\t\tfor(const auto &h : vhls.back()){\n\t\t\t\t\tif(h.in(x, y)){\n\t\t\t\t\t\tok = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(ok){\n\t\t\t\t\tint e = encode(x, y);\n\t\t\t\t\tLL sum = 0;\n\t\t\t\t\tfor(int i = 0; i < n; ++i){\n\t\t\t\t\t\tsum += tms[i][e];\n\t\t\t\t\t}\n\t\t\t\t\tans = min(ans, sum);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(ans == INF){ ans = -1; }\n\t\tcout << ans << '\\n';\n\t}\n}", "accuracy": 1, "time_ms": 3850, "memory_kb": 41736, "score_of_the_acc": -0.7003, "final_rank": 8 } ]
aoj_1606_cpp
Complex Paper Folding Dr. G, an authority in paper folding and one of the directors of Intercultural Consortium on Paper Crafts, believes complexity gives figures beauty. As one of his assistants, your job is to find the way to fold a paper to obtain the most complex figure. Dr. G defines the complexity of a figure by the number of vertices. With the same number of vertices, one with longer perimeter is more complex. To simplify the problem, we consider papers with convex polygon in their shapes and folding them only once. Each paper should be folded so that one of its vertices exactly lies on top of another vertex. Write a program that, for each given convex polygon, outputs the perimeter of the most complex polygon obtained by folding it once. Figure 1 (left) illustrates the polygon given in the first dataset of Sample Input. This happens to be a rectangle. Folding this rectangle can yield three different polygons illustrated in the Figures 1-a to c by solid lines. For this dataset, you should answer the perimeter of the pentagon shown in Figure 1-a. Though the rectangle in Figure 1-b has longer perimeter, the number of vertices takes priority. (a) (b) (c) Figure 1: The case of the first sample input Figure 2 (left) illustrates the polygon given in the second dataset of Sample Input, which is a triangle. Whichever pair of its vertices are chosen, quadrangles are obtained as shown in Figures 2-a to c. Among these, you should answer the longest perimeter, which is that of the quadrangle shown in Figure 2-a. (a) (b) (c) Figure 2: The case of the second sample input Although we start with a convex polygon, folding it may result a concave polygon. Figure 3 (left) illustrates the polygon given in the third dataset in Sample Input. A concave hexagon shown in Figure 3 (right) can be obtained by folding it, which has the largest number of vertices. Figure 3: The case of the third sample input Figure 4 (left) illustrates the polygon given in the fifth dataset in Sample Input. Although the perimeter of the polygon in Figure 4-b is longer than that of the polygon in Figure 4-a, because the polygon in Figure 4-b is a quadrangle, the answer should be the perimeter of the pentagon in Figure 4-a. (a) (b) Figure 4: The case of the fifth sample input Input The input consists of multiple datasets, each in the following format. n x 1 y 1 ... x n y n n is the number of vertices of the given polygon. n is a positive integer satisfying 3 ≤ n ≤ 20. ( x i , y i ) gives the coordinates of the i -th vertex. x i and y i are integers satisfying 0 ≤ x i , y i < 1000. The vertices ( x 1 , y 1 ), ..., ( x n , y n ) are given in the counterclockwise order on the xy -plane with y axis oriented upwards. You can assume that all the given polygons are convex. All the possible polygons obtained by folding the given polygon satisfy the following conditions. Distance between any two vertices is greater than or equal to 0.00001. For any three consecutive vertices P, Q, and R, ...(truncated)
[ { "submission_id": "aoj_1606_10853765", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <deque>\ntypedef long long ll;\ntypedef long double ld;\n//typedef double ld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-6;\nconst ld PI = acos(-1);\nconst int LEN = 2e3 + 5;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\nstruct II { int i1, i2; };\n\nint Q, N;\nld ANS[LEN];\nint ans = 0;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return { x * p.x + y * p.y }; }\n\tld operator / (const Pos& p) const { return { x * p.y - y * p.x }; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos unit() const { return *this / mag(); }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tld rad() const { return atan2(y, x); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = { 0, 0 };\ntypedef std::vector<Pos> Polygon;\nstruct Linear {//ps[0] -> ps[1] ::\n\tPos ps[2];\n\tPos dir_;\n\tconst Pos& operator [] (const int& i) const { return ps[i]; }\n\tconst Pos& dir() const { return dir_; }\n\tLinear(Pos a = Pos(0, 0), Pos b = Pos(0, 0)) {\n\t\tps[0] = a;\n\t\tps[1] = b;\n\t\tdir_ = (ps[1] - ps[0]).unit();\n\t}\n\tbool include(const Pos& p) const { return sign(dir_ / (p - ps[0])) > 0; }\n\tfriend bool parallel(const Linear& l0, const Linear& l1) { return zero(l0.dir() / l1.dir()); }\n\tfriend bool same_dir(const Linear& l0, const Linear& l1) { return parallel(l0, l1) && l0.dir() * l1.dir() > 0; }\n\tbool operator < (const Linear& l0) const {\n\t\tif (same_dir(*this, l0)) return l0.include(ps[0]);\n\t\telse return cmpq(this->dir(), l0.dir());\n\t}\n};\ntypedef std::vector<Linear> Planes;\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { ld ret = cross(d1, d2, d3); return sign(ret); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) >= 0; }\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) > 0; }\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) { ld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2); return (p1 * a2 + p2 * a1) / (a1 + a2); }\nPos intersection(const Linear& l1, const Linear& l2) { return intersection(l1[0], l1[1], l2[0], l2[1]); }\nPolygon half_plane_intersection(std::vector<Linear>& HP) {\n\tauto check = [&](Linear& u, Linear& v, Linear& w) -> bool {\n\t\treturn w.include(intersection(u, v));\n\t\t};\n\tstd::sort(HP.begin(), HP.end());\n\tstd::deque<Linear> dq;\n\tint sz = HP.size();\n\tfor (int i = 0; i < sz; ++i) {\n\t\tif (i && same_dir(HP[i], HP[(i - 1) % sz])) continue;\n\t\twhile (dq.size() > 1 && !check(dq[dq.size() - 2], dq[dq.size() - 1], HP[i])) dq.pop_back();\n\t\twhile (dq.size() > 1 && !check(dq[1], dq[0], HP[i])) dq.pop_front();\n\t\tdq.push_back(HP[i]);\n\t}\n\twhile (dq.size() > 2 && !check(dq[dq.size() - 2], dq[dq.size() - 1], dq[0])) dq.pop_back();\n\twhile (dq.size() > 2 && !check(dq[1], dq[0], dq[dq.size() - 1])) dq.pop_front();\n\tsz = dq.size();\n\tif (sz < 3) return {};\n\tPolygon HPI;\n\tfor (int i = 0; i < sz; ++i) HPI.push_back(intersection(dq[i], dq[(i + 1) % sz]));\n\treturn HPI;\n}\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret * .5;\n}\nld round(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += (H[i] - H[(i + 1) % sz]).mag();\n\treturn ret;\n}\nPos mirror(const Linear& l, const Pos& p) {\n\tPos s = l[0], e = l[1];\n\tif (!ccw(s, e, p)) return p;\n\tPos v = ~(e - s);\n\tPos m = intersection(s, e, p, p + v);\n\tv = m - p;\n\treturn p + v + v;\n}\nint inner_check_bi_search(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return -1;\n\tif (on_seg_strong(H[0], H[1], p) || on_seg_strong(H[0], H[sz - 1], p)) return 0;\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return 1;\n\telse if (on_seg_strong(H[s], H[e], p)) return 0;\n\telse return -1;\n}\nint inner_check_bi_search_2(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return -1;\n\tif (p == H[0] || p == H[1] || p == H[sz - 1] ) return 1;\n\tif (on_seg_weak(H[0], H[1], p) || on_seg_weak(H[0], H[sz - 1], p)) return 0;\n\t//if (on_seg_strong(H[0], H[1], p) || on_seg_strong(H[0], H[sz - 1], p)) return 0;\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return 2;\n\telse if (H[s] == p || H[e] == p) return 1;\n\telse if (on_seg_weak(H[s], H[e], p)) return 0;\n\t//else if (on_seg_strong(H[s], H[e], p)) return 0;\n\telse return -1;\n}\nint inner_check(const Polygon& H, const Pos& p) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i1 = (i + 1) % sz;\n\t\tif (H[i] == p) return 1;\n\t\tif (H[i1] == p) return 1;\n\t\tif (on_seg_weak(H[i], H[i1], p)) return 0;\n\t\tif (ccw(H[i], H[i1], p) < 0) return -1;\n\t}\n\treturn 2;\n}\nII find_(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return { -1, -1 };\n\tif (p == H[0]) return { 0, -1 };\n\tif (p == H[1]) return { 1, -1 };\n\tif (p == H[sz - 1]) return { sz - 1, -1 };\n\tif (on_seg_strong(H[0], H[1], p)) return { 0, 1 };\n\tif (on_seg_strong(H[0], H[sz - 1], p)) return { 0, sz - 1 };\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return { -1, -1 };\n\telse if (H[s] == p) return { s, -1 };\n\telse if (H[e] == p) return { e, -1 };\n\telse if (on_seg_strong(H[s], H[e], p)) return { s, e };\n\telse return { -1, -1 };\n}\nII find(const Polygon& H, const Pos& p) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i1 = (i + 1) % sz;\n\t\tif (H[i] == p) return { i, -1 };\n\t\tif (H[i1] == p) return { i1, -2 };\n\t\tif (on_seg_weak(H[i], H[i1], p)) return { i, i1 };\n\t\tif (ccw(H[i], H[i1], p) < 0) return { -1, -1 };\n\t}\n\treturn { -1, -1 };\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tld ret = 0;\n\tint V = 0;\n\tPolygon H(N);\n\tfor (Pos& p : H) std::cin >> p;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = i + 1; j < N; j++) {\n\t\t\tPos m = (H[i] + H[j]) * .5;\n\t\t\tPos v = ~(H[i] - H[j]);\n\t\t\tLinear I = Linear(m, m + v);\n\t\t\tLinear J = Linear(m, m - v);\n\t\t\tPlanes P0, Pi, Pj;\n\t\t\tfor (int k = 0; k < N; k++) P0.push_back(Linear(H[k], H[(k + 1) % N]));\n\t\t\tPi = P0;\n\t\t\tPi.push_back(I);\n\t\t\tPolygon HI = half_plane_intersection(Pi);\n\t\t\tPj = P0;\n\t\t\tPj.push_back(J);\n\t\t\tPolygon HJ = half_plane_intersection(Pj);\n\t\t\tfor (Pos& p : HJ) p = mirror(J, p);\n\t\t\tstd::reverse(HJ.begin(), HJ.end());\n\t\t\tP0.clear();\n\t\t\tint sz = HI.size();\n\t\t\tfor (int k = 0; k < sz; k++) P0.push_back(Linear(HI[k], HI[(k + 1) % sz]));\n\t\t\tsz = HJ.size();\n\t\t\tfor (int k = 0; k < sz; k++) P0.push_back(Linear(HJ[k], HJ[(k + 1) % sz]));\n\t\t\tPolygon hpi = half_plane_intersection(P0);\n\t\t\tld R = round(HI) + round(HJ) - round(hpi);\n\t\t\tint ni = HI.size(), nj = HJ.size();\n\t\t\tint ij = 0;\n\t\t\tfor (const Pos& p : HI)\n\t\t\t\tif (inner_check_bi_search(hpi, p) >= 0 ||\n\t\t\t\t\tinner_check_bi_search(HJ, p) >= 0) ni--;\n\t\t\tfor (const Pos& p : HJ)\n\t\t\t\tif (inner_check_bi_search(hpi, p) >= 0 ||\n\t\t\t\t\tinner_check_bi_search(HI, p) >= 0) nj--;\n\t\t\tfor (const Pos& p : hpi) {\n\t\t\t\t//int ii = inner_check_bi_search_2(HI, p);\n\t\t\t\t//int jj = inner_check_bi_search_2(HJ, p);\n\t\t\t\tint ii = inner_check(HI, p);\n\t\t\t\tint jj = inner_check(HJ, p);\n\t\t\t\t//assert(ii != -1 && jj != -1);\n\t\t\t\t//if (ii == -1 || jj == -1) {\n\t\t\t\t//\tstd::cout << \"DEBUG:: FUCK::\\n\";\n\t\t\t\t//\tstd::cout << \"H = [\\n\";\n\t\t\t\t//\tfor (Pos& p : H) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"hpi = [\\n\";\n\t\t\t\t//\tfor (Pos& p : hpi) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"HI = [\\n\";\n\t\t\t\t//\tfor (Pos& p : HI) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"HJ = [\\n\";\n\t\t\t\t//\tfor (Pos& p : HJ) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"DEBUG:: FUCK::\\n\";\n\t\t\t\t//}\n\t\t\t\tif (ii == 1 && jj == 1) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint i1 = iii.i1, i2 = (i1 + 1) % HI.size(), i0 = (i1 - 1 + HI.size()) % HI.size();\n\t\t\t\t\tint j1 = jjj.i1, j2 = (j1 + 1) % HJ.size(), j0 = (j1 - 1 + HJ.size()) % HJ.size();\n\t\t\t\t\t//std::cout << i0 << \" \" << i1 << \" \" << i2 << \"\\n\";\n\t\t\t\t\t//std::cout << j0 << \" \" << j1 << \" \" << j2 << \"\\n\";\n\t\t\t\t\tif (!ccw(HI[i0], HI[i1], HJ[j2]) || !ccw(HJ[j0], HJ[j1], HI[i2])) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 0 && jj == 0) continue;\n\t\t\t\telse if (ii == 0 && jj == 1) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint i1 = iii.i1, i2 = iii.i2;\n\t\t\t\t\tint j1 = jjj.i1, j2 = (j1 + 1) % HJ.size(), j0 = (j1 - 1 + HJ.size()) % HJ.size();\n\t\t\t\t\tint c1 = ccw(HI[i1], HI[i2], HJ[j0]);\n\t\t\t\t\tint c2 = ccw(HI[i1], HI[i2], HJ[j2]);\n\t\t\t\t\tif (!c1 || !c2 || c1 * c2 == 1) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 1 && jj == 0) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint j1 = jjj.i1, j2 = jjj.i2;\n\t\t\t\t\tint i1 = iii.i1, i2 = (i1 + 1) % HI.size(), i0 = (i1 - 1 + HI.size()) % HI.size();\n\t\t\t\t\tint c1 = ccw(HJ[j1], HJ[j2], HI[i0]);\n\t\t\t\t\tint c2 = ccw(HJ[j1], HJ[j2], HI[i2]);\n\t\t\t\t\tif (!c1 || !c2 || c1 * c2 == 1) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 2 || jj == 2) ij++;\n\t\t\t}\n\t\t\tint n = hpi.size() + ni + nj - ij;\n\t\t\tif (V < n) {\n\t\t\t\tV = n;\n\t\t\t\tret = R;\n\t\t\t}\n\t\t\telse if (V == n) {\n\t\t\t\tV = n;\n\t\t\t\tret = std::max(ret, R);\n\t\t\t}//TOTAL O(T * 20 * 20 * 20 * 20) ~= O(T * 16,0000)\n\t\t}\n\t}\n\t//std::cout << ret << \"\\n\";\n\tANS[ans++] = ret;\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (int i = 0; i < ans; i++) std::cout << ANS[i] << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj11044", "accuracy": 1, "time_ms": 10, "memory_kb": 3488, "score_of_the_acc": -0.6822, "final_rank": 3 }, { "submission_id": "aoj_1606_9805561", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <deque>\ntypedef long long ll;\ntypedef long double ld;\n//typedef double ld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-6;\nconst ld PI = acos(-1);\nconst int LEN = 2e3 + 5;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\nstruct II { int i1, i2; };\n\nint Q, N;\nld ANS[LEN];\nint ans = 0;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return { x * p.x + y * p.y }; }\n\tld operator / (const Pos& p) const { return { x * p.y - y * p.x }; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos unit() const { return *this / mag(); }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tld rad() const { return atan2(y, x); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = { 0, 0 };\ntypedef std::vector<Pos> Polygon;\nstruct Linear {//ps[0] -> ps[1] ::\n\tPos ps[2];\n\tPos dir_;\n\tconst Pos& operator [] (const int& i) const { return ps[i]; }\n\tconst Pos& dir() const { return dir_; }\n\tLinear(Pos a = Pos(0, 0), Pos b = Pos(0, 0)) {\n\t\tps[0] = a;\n\t\tps[1] = b;\n\t\tdir_ = (ps[1] - ps[0]).unit();\n\t}\n\tbool include(const Pos& p) const { return sign(dir_ / (p - ps[0])) > 0; }\n\tfriend bool parallel(const Linear& l0, const Linear& l1) { return zero(l0.dir() / l1.dir()); }\n\tfriend bool same_dir(const Linear& l0, const Linear& l1) { return parallel(l0, l1) && l0.dir() * l1.dir() > 0; }\n\tbool operator < (const Linear& l0) const {\n\t\tif (same_dir(*this, l0)) return l0.include(ps[0]);\n\t\telse return cmpq(this->dir(), l0.dir());\n\t}\n};\ntypedef std::vector<Linear> Planes;\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { ld ret = cross(d1, d2, d3); return sign(ret); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) >= 0; }\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) > 0; }\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) { ld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2); return (p1 * a2 + p2 * a1) / (a1 + a2); }\nPos intersection(const Linear& l1, const Linear& l2) { return intersection(l1[0], l1[1], l2[0], l2[1]); }\nPolygon half_plane_intersection(std::vector<Linear>& HP) {\n\tauto check = [&](Linear& u, Linear& v, Linear& w) -> bool {\n\t\treturn w.include(intersection(u, v));\n\t\t};\n\tstd::sort(HP.begin(), HP.end());\n\tstd::deque<Linear> dq;\n\tint sz = HP.size();\n\tfor (int i = 0; i < sz; ++i) {\n\t\tif (i && same_dir(HP[i], HP[(i - 1) % sz])) continue;\n\t\twhile (dq.size() > 1 && !check(dq[dq.size() - 2], dq[dq.size() - 1], HP[i])) dq.pop_back();\n\t\twhile (dq.size() > 1 && !check(dq[1], dq[0], HP[i])) dq.pop_front();\n\t\tdq.push_back(HP[i]);\n\t}\n\twhile (dq.size() > 2 && !check(dq[dq.size() - 2], dq[dq.size() - 1], dq[0])) dq.pop_back();\n\twhile (dq.size() > 2 && !check(dq[1], dq[0], dq[dq.size() - 1])) dq.pop_front();\n\tsz = dq.size();\n\tif (sz < 3) return {};\n\tPolygon HPI;\n\tfor (int i = 0; i < sz; ++i) HPI.push_back(intersection(dq[i], dq[(i + 1) % sz]));\n\treturn HPI;\n}\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret * .5;\n}\nld round(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += (H[i] - H[(i + 1) % sz]).mag();\n\treturn ret;\n}\nPos mirror(const Linear& l, const Pos& p) {\n\tPos s = l[0], e = l[1];\n\tif (!ccw(s, e, p)) return p;\n\tPos v = ~(e - s);\n\tPos m = intersection(s, e, p, p + v);\n\tv = m - p;\n\treturn p + v + v;\n}\nint inner_check_bi_search(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return -1;\n\tif (on_seg_strong(H[0], H[1], p) || on_seg_strong(H[0], H[sz - 1], p)) return 0;\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return 1;\n\telse if (on_seg_strong(H[s], H[e], p)) return 0;\n\telse return -1;\n}\nint inner_check_bi_search_2(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return -1;\n\tif (p == H[0] || p == H[1] || p == H[sz - 1] ) return 1;\n\tif (on_seg_weak(H[0], H[1], p) || on_seg_weak(H[0], H[sz - 1], p)) return 0;\n\t//if (on_seg_strong(H[0], H[1], p) || on_seg_strong(H[0], H[sz - 1], p)) return 0;\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return 2;\n\telse if (H[s] == p || H[e] == p) return 1;\n\telse if (on_seg_weak(H[s], H[e], p)) return 0;\n\t//else if (on_seg_strong(H[s], H[e], p)) return 0;\n\telse return -1;\n}\nint inner_check(const Polygon& H, const Pos& p) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i1 = (i + 1) % sz;\n\t\tif (H[i] == p) return 1;\n\t\tif (H[i1] == p) return 1;\n\t\tif (on_seg_weak(H[i], H[i1], p)) return 0;\n\t\tif (ccw(H[i], H[i1], p) < 0) return -1;\n\t}\n\treturn 2;\n}\nII find_(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return { -1, -1 };\n\tif (p == H[0]) return { 0, -1 };\n\tif (p == H[1]) return { 1, -1 };\n\tif (p == H[sz - 1]) return { sz - 1, -1 };\n\tif (on_seg_strong(H[0], H[1], p)) return { 0, 1 };\n\tif (on_seg_strong(H[0], H[sz - 1], p)) return { 0, sz - 1 };\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return { -1, -1 };\n\telse if (H[s] == p) return { s, -1 };\n\telse if (H[e] == p) return { e, -1 };\n\telse if (on_seg_strong(H[s], H[e], p)) return { s, e };\n\telse return { -1, -1 };\n}\nII find(const Polygon& H, const Pos& p) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i1 = (i + 1) % sz;\n\t\tif (H[i] == p) return { i, -1 };\n\t\tif (H[i1] == p) return { i1, -2 };\n\t\tif (on_seg_weak(H[i], H[i1], p)) return { i, i1 };\n\t\tif (ccw(H[i], H[i1], p) < 0) return { -1, -1 };\n\t}\n\treturn { -1, -1 };\n}\nbool query() {\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tld ret = 0;\n\tint V = 0;\n\tPolygon H(N);\n\tfor (Pos& p : H) std::cin >> p;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = i + 1; j < N; j++) {\n\t\t\tPos m = (H[i] + H[j]) * .5;\n\t\t\tPos v = ~(H[i] - H[j]);\n\t\t\tLinear I = Linear(m, m + v);\n\t\t\tLinear J = Linear(m, m - v);\n\t\t\tPlanes P0, Pi, Pj;\n\t\t\tfor (int k = 0; k < N; k++) P0.push_back(Linear(H[k], H[(k + 1) % N]));\n\t\t\tPi = P0;\n\t\t\tPi.push_back(I);\n\t\t\tPolygon HI = half_plane_intersection(Pi);\n\t\t\tPj = P0;\n\t\t\tPj.push_back(J);\n\t\t\tPolygon HJ = half_plane_intersection(Pj);\n\t\t\tfor (Pos& p : HJ) p = mirror(J, p);\n\t\t\tstd::reverse(HJ.begin(), HJ.end());\n\t\t\tP0.clear();\n\t\t\tint sz = HI.size();\n\t\t\tfor (int k = 0; k < sz; k++) P0.push_back(Linear(HI[k], HI[(k + 1) % sz]));\n\t\t\tsz = HJ.size();\n\t\t\tfor (int k = 0; k < sz; k++) P0.push_back(Linear(HJ[k], HJ[(k + 1) % sz]));\n\t\t\tPolygon hpi = half_plane_intersection(P0);\n\t\t\tld R = round(HI) + round(HJ) - round(hpi);\n\t\t\tint ni = HI.size(), nj = HJ.size();\n\t\t\tint ij = 0;\n\t\t\tfor (const Pos& p : HI)\n\t\t\t\tif (inner_check_bi_search(hpi, p) >= 0 ||\n\t\t\t\t\tinner_check_bi_search(HJ, p) >= 0) ni--;\n\t\t\tfor (const Pos& p : HJ)\n\t\t\t\tif (inner_check_bi_search(hpi, p) >= 0 ||\n\t\t\t\t\tinner_check_bi_search(HI, p) >= 0) nj--;\n\t\t\tfor (const Pos& p : hpi) {\n\t\t\t\t//int ii = inner_check_bi_search_2(HI, p);\n\t\t\t\t//int jj = inner_check_bi_search_2(HJ, p);\n\t\t\t\tint ii = inner_check(HI, p);\n\t\t\t\tint jj = inner_check(HJ, p);\n\t\t\t\t//assert(ii != -1 && jj != -1);\n\t\t\t\t//if (ii == -1 || jj == -1) {\n\t\t\t\t//\tstd::cout << \"DEBUG:: FUCK::\\n\";\n\t\t\t\t//\tstd::cout << \"H = [\\n\";\n\t\t\t\t//\tfor (Pos& p : H) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"hpi = [\\n\";\n\t\t\t\t//\tfor (Pos& p : hpi) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"HI = [\\n\";\n\t\t\t\t//\tfor (Pos& p : HI) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"HJ = [\\n\";\n\t\t\t\t//\tfor (Pos& p : HJ) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"DEBUG:: FUCK::\\n\";\n\t\t\t\t//}\n\t\t\t\tif (ii == 1 && jj == 1) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint i1 = iii.i1, i2 = (i1 + 1) % HI.size(), i0 = (i1 - 1 + HI.size()) % HI.size();\n\t\t\t\t\tint j1 = jjj.i1, j2 = (j1 + 1) % HJ.size(), j0 = (j1 - 1 + HJ.size()) % HJ.size();\n\t\t\t\t\t//std::cout << i0 << \" \" << i1 << \" \" << i2 << \"\\n\";\n\t\t\t\t\t//std::cout << j0 << \" \" << j1 << \" \" << j2 << \"\\n\";\n\t\t\t\t\tif (!ccw(HI[i0], HI[i1], HJ[j2]) || !ccw(HJ[j0], HJ[j1], HI[i2])) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 0 && jj == 0) continue;\n\t\t\t\telse if (ii == 0 && jj == 1) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint i1 = iii.i1, i2 = iii.i2;\n\t\t\t\t\tint j1 = jjj.i1, j2 = (j1 + 1) % HJ.size(), j0 = (j1 - 1 + HJ.size()) % HJ.size();\n\t\t\t\t\tint c1 = ccw(HI[i1], HI[i2], HJ[j0]);\n\t\t\t\t\tint c2 = ccw(HI[i1], HI[i2], HJ[j2]);\n\t\t\t\t\tif (!c1 || !c2 || c1 * c2 == 1) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 1 && jj == 0) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint j1 = jjj.i1, j2 = jjj.i2;\n\t\t\t\t\tint i1 = iii.i1, i2 = (i1 + 1) % HI.size(), i0 = (i1 - 1 + HI.size()) % HI.size();\n\t\t\t\t\tint c1 = ccw(HJ[j1], HJ[j2], HI[i0]);\n\t\t\t\t\tint c2 = ccw(HJ[j1], HJ[j2], HI[i2]);\n\t\t\t\t\tif (!c1 || !c2 || c1 * c2 == 1) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 2 || jj == 2) ij++;\n\t\t\t}\n\t\t\tint n = hpi.size() + ni + nj - ij;\n\t\t\tif (V < n) {\n\t\t\t\tV = n;\n\t\t\t\tret = R;\n\t\t\t}\n\t\t\telse if (V == n) {\n\t\t\t\tV = n;\n\t\t\t\tret = std::max(ret, R);\n\t\t\t}//TOTAL O(T * 20 * 20 * 20 * 20) ~= O(T * 16,0000)\n\t\t}\n\t}\n\t//std::cout << ret << \"\\n\";\n\tANS[ans++] = ret;\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (int i = 0; i < ans; i++) std::cout << ANS[i] << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj11044", "accuracy": 1, "time_ms": 10, "memory_kb": 3652, "score_of_the_acc": -1, "final_rank": 5 }, { "submission_id": "aoj_1606_9805559", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <deque>\ntypedef long long ll;\ntypedef long double ld;\n//typedef double ld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-6;\nconst ld PI = acos(-1);\nconst int LEN = 2e3 + 5;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\nstruct II { int i1, i2; };\n\nint Q, N;\nld ANS[LEN];\nint ans = 0;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return { x * p.x + y * p.y }; }\n\tld operator / (const Pos& p) const { return { x * p.y - y * p.x }; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos unit() const { return *this / mag(); }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tld rad() const { return atan2(y, x); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = { 0, 0 };\ntypedef std::vector<Pos> Polygon;\nstruct Linear {//ps[0] -> ps[1] ::\n\tPos ps[2];\n\tPos dir_;\n\tconst Pos& operator [] (const int& i) const { return ps[i]; }\n\tconst Pos& dir() const { return dir_; }\n\tLinear(Pos a = Pos(0, 0), Pos b = Pos(0, 0)) {\n\t\tps[0] = a;\n\t\tps[1] = b;\n\t\tdir_ = (ps[1] - ps[0]).unit();\n\t}\n\tbool include(const Pos& p) const { return sign(dir_ / (p - ps[0])) > 0; }\n\tfriend bool parallel(const Linear& l0, const Linear& l1) { return zero(l0.dir() / l1.dir()); }\n\tfriend bool same_dir(const Linear& l0, const Linear& l1) { return parallel(l0, l1) && l0.dir() * l1.dir() > 0; }\n\tbool operator < (const Linear& l0) const {\n\t\tif (same_dir(*this, l0)) return l0.include(ps[0]);\n\t\telse return cmpq(this->dir(), l0.dir());\n\t}\n};\ntypedef std::vector<Linear> Planes;\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { ld ret = cross(d1, d2, d3); return sign(ret); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) >= 0; }\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) > 0; }\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) { ld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2); return (p1 * a2 + p2 * a1) / (a1 + a2); }\nPos intersection(const Linear& l1, const Linear& l2) { return intersection(l1[0], l1[1], l2[0], l2[1]); }\nPolygon half_plane_intersection(std::vector<Linear>& HP) {\n\tauto check = [&](Linear& u, Linear& v, Linear& w) -> bool {\n\t\treturn w.include(intersection(u, v));\n\t\t};\n\tstd::sort(HP.begin(), HP.end());\n\tstd::deque<Linear> dq;\n\tint sz = HP.size();\n\tfor (int i = 0; i < sz; ++i) {\n\t\tif (i && same_dir(HP[i], HP[(i - 1) % sz])) continue;\n\t\twhile (dq.size() > 1 && !check(dq[dq.size() - 2], dq[dq.size() - 1], HP[i])) dq.pop_back();\n\t\twhile (dq.size() > 1 && !check(dq[1], dq[0], HP[i])) dq.pop_front();\n\t\tdq.push_back(HP[i]);\n\t}\n\twhile (dq.size() > 2 && !check(dq[dq.size() - 2], dq[dq.size() - 1], dq[0])) dq.pop_back();\n\twhile (dq.size() > 2 && !check(dq[1], dq[0], dq[dq.size() - 1])) dq.pop_front();\n\tsz = dq.size();\n\tif (sz < 3) return {};\n\tPolygon HPI;\n\tfor (int i = 0; i < sz; ++i) HPI.push_back(intersection(dq[i], dq[(i + 1) % sz]));\n\treturn HPI;\n}\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret * .5;\n}\nld round(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += (H[i] - H[(i + 1) % sz]).mag();\n\treturn ret;\n}\nPos mirror(const Linear& l, const Pos& p) {\n\tPos s = l[0], e = l[1];\n\tif (!ccw(s, e, p)) return p;\n\tPos v = ~(e - s);\n\tPos m = intersection(s, e, p, p + v);\n\tv = m - p;\n\treturn p + v + v;\n}\nint inner_check_bi_search(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return -1;\n\tif (on_seg_strong(H[0], H[1], p) || on_seg_strong(H[0], H[sz - 1], p)) return 0;\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return 1;\n\telse if (on_seg_strong(H[s], H[e], p)) return 0;\n\telse return -1;\n}\nint inner_check_bi_search_2(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return -1;\n\tif (p == H[0] || p == H[1] || p == H[sz - 1] ) return 1;\n\tif (on_seg_weak(H[0], H[1], p) || on_seg_weak(H[0], H[sz - 1], p)) return 0;\n\t//if (on_seg_strong(H[0], H[1], p) || on_seg_strong(H[0], H[sz - 1], p)) return 0;\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return 2;\n\telse if (H[s] == p || H[e] == p) return 1;\n\telse if (on_seg_weak(H[s], H[e], p)) return 0;\n\t//else if (on_seg_strong(H[s], H[e], p)) return 0;\n\telse return -1;\n}\nint inner_check(const Polygon& H, const Pos& p) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i1 = (i + 1) % sz;\n\t\tif (H[i] == p) return 1;\n\t\tif (H[i1] == p) return 1;\n\t\tif (on_seg_weak(H[i], H[i1], p)) return 0;\n\t\tif (ccw(H[i], H[i1], p) < 0) return -1;\n\t}\n\treturn 2;\n}\nII find_(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return { -1, -1 };\n\tif (p == H[0]) return { 0, -1 };\n\tif (p == H[1]) return { 1, -1 };\n\tif (p == H[sz - 1]) return { sz - 1, -1 };\n\tif (on_seg_strong(H[0], H[1], p)) return { 0, 1 };\n\tif (on_seg_strong(H[0], H[sz - 1], p)) return { 0, sz - 1 };\n\tint s = 1, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return { -1, -1 };\n\telse if (H[s] == p) return { s, -1 };\n\telse if (H[e] == p) return { e, -1 };\n\telse if (on_seg_strong(H[s], H[e], p)) return { s, e };\n\telse return { -1, -1 };\n}\nII find(const Polygon& H, const Pos& p) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i1 = (i + 1) % sz;\n\t\tif (H[i] == p) return { i, -1 };\n\t\tif (H[i1] == p) return { i1, -2 };\n\t\tif (on_seg_weak(H[i], H[i1], p)) return { i, i1 };\n\t\tif (ccw(H[i], H[i1], p) < 0) return { -1, -1 };\n\t}\n\treturn { -1, -1 };\n}\nbool query() {//O(2000)\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tld ret = 0;\n\tint V = 0;\n\tPolygon H(N);\n\tfor (Pos& p : H) std::cin >> p;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = i + 1; j < N; j++) {//(O(2000 * 20 * 20) == O(800000)\n\t\t\tPos m = (H[i] + H[j]) * .5;\n\t\t\tPos v = ~(H[i] - H[j]);\n\t\t\tLinear I = Linear(m, m + v);\n\t\t\tLinear J = Linear(m, m - v);\n\t\t\tPlanes P0, Pi, Pj;\n\t\t\tfor (int k = 0; k < N; k++) P0.push_back(Linear(H[k], H[(k + 1) % N]));\n\t\t\tPi = P0;\n\t\t\tPi.push_back(I);\n\t\t\tPolygon HI = half_plane_intersection(Pi);\n\t\t\tPj = P0;\n\t\t\tPj.push_back(J);\n\t\t\tPolygon HJ = half_plane_intersection(Pj);\n\t\t\tfor (Pos& p : HJ) p = mirror(J, p);\n\t\t\tstd::reverse(HJ.begin(), HJ.end());\n\t\t\tP0.clear();\n\t\t\tint sz = HI.size();\n\t\t\tfor (int k = 0; k < sz; k++) P0.push_back(Linear(HI[k], HI[(k + 1) % sz]));\n\t\t\tsz = HJ.size();\n\t\t\tfor (int k = 0; k < sz; k++) P0.push_back(Linear(HJ[k], HJ[(k + 1) % sz]));\n\t\t\tPolygon hpi = half_plane_intersection(P0);\n\t\t\tld R = round(HI) + round(HJ) - round(hpi);\n\t\t\tint ni = HI.size(), nj = HJ.size();\n\t\t\tint ij = 0;\n\t\t\tfor (const Pos& p : HI)\n\t\t\t\tif (inner_check_bi_search(hpi, p) >= 0 ||\n\t\t\t\t\tinner_check_bi_search(HJ, p) >= 0) ni--;\n\t\t\tfor (const Pos& p : HJ)\n\t\t\t\tif (inner_check_bi_search(hpi, p) >= 0 ||\n\t\t\t\t\tinner_check_bi_search(HI, p) >= 0) nj--;\n\t\t\tfor (const Pos& p : hpi) {\n\t\t\t\t//int ii = inner_check_bi_search_2(HI, p);\n\t\t\t\t//int jj = inner_check_bi_search_2(HJ, p);\n\t\t\t\tint ii = inner_check(HI, p);\n\t\t\t\tint jj = inner_check(HJ, p);\n\t\t\t\t//assert(ii != -1 && jj != -1);\n\t\t\t\t//if (ii == -1 || jj == -1) {\n\t\t\t\t//\tstd::cout << \"DEBUG:: FUCK::\\n\";\n\t\t\t\t//\tstd::cout << \"H = [\\n\";\n\t\t\t\t//\tfor (Pos& p : H) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"hpi = [\\n\";\n\t\t\t\t//\tfor (Pos& p : hpi) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"HI = [\\n\";\n\t\t\t\t//\tfor (Pos& p : HI) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"HJ = [\\n\";\n\t\t\t\t//\tfor (Pos& p : HJ) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"DEBUG:: FUCK::\\n\";\n\t\t\t\t//}\n\t\t\t\tif (ii == 1 && jj == 1) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint i1 = iii.i1, i2 = (i1 + 1) % HI.size(), i0 = (i1 - 1 + HI.size()) % HI.size();\n\t\t\t\t\tint j1 = jjj.i1, j2 = (j1 + 1) % HJ.size(), j0 = (j1 - 1 + HJ.size()) % HJ.size();\n\t\t\t\t\t//std::cout << i0 << \" \" << i1 << \" \" << i2 << \"\\n\";\n\t\t\t\t\t//std::cout << j0 << \" \" << j1 << \" \" << j2 << \"\\n\";\n\t\t\t\t\tif (!ccw(HI[i0], HI[i1], HJ[j2]) || !ccw(HJ[j0], HJ[j1], HI[i2])) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 0 && jj == 0) continue;\n\t\t\t\telse if (ii == 0 && jj == 1) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint i1 = iii.i1, i2 = iii.i2;\n\t\t\t\t\tint j1 = jjj.i1, j2 = (j1 + 1) % HJ.size(), j0 = (j1 - 1 + HJ.size()) % HJ.size();\n\t\t\t\t\tint c1 = ccw(HI[i1], HI[i2], HJ[j0]);\n\t\t\t\t\tint c2 = ccw(HI[i1], HI[i2], HJ[j2]);\n\t\t\t\t\tif (!c1 || !c2 || c1 * c2 == 1) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 1 && jj == 0) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint j1 = jjj.i1, j2 = jjj.i2;\n\t\t\t\t\tint i1 = iii.i1, i2 = (i1 + 1) % HI.size(), i0 = (i1 - 1 + HI.size()) % HI.size();\n\t\t\t\t\tint c1 = ccw(HJ[j1], HJ[j2], HI[i0]);\n\t\t\t\t\tint c2 = ccw(HJ[j1], HJ[j2], HI[i2]);\n\t\t\t\t\tif (!c1 || !c2 || c1 * c2 == 1) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 2 || jj == 2) ij++;\n\t\t\t}\n\t\t\tint n = hpi.size() + ni + nj - ij;\n\t\t\tif (V < n) {\n\t\t\t\tV = n;\n\t\t\t\tret = R;\n\t\t\t}\n\t\t\telse if (V == n) {\n\t\t\t\tV = n;\n\t\t\t\tret = std::max(ret, R);\n\t\t\t}//TOTAL O(2000 * 20 * 20 * 20 * 20) ~= O(3,2000,0000)\n\t\t}\n\t}\n\t//std::cout << ret << \"\\n\";\n\tANS[ans++] = ret;\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (int i = 0; i < ans; i++) std::cout << ANS[i] << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj11044", "accuracy": 1, "time_ms": 10, "memory_kb": 3580, "score_of_the_acc": -0.8605, "final_rank": 4 }, { "submission_id": "aoj_1606_9805556", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <deque>\ntypedef long long ll;\ntypedef long double ld;\n//typedef double ld;\nconst ld INF = 1e17;\nconst ld TOL = 1e-6;\nconst ld PI = acos(-1);\nconst int LEN = 2e3 + 5;\ninline int sign(const ld& x) { return x < -TOL ? -1 : x > TOL; }\ninline bool zero(const ld& x) { return !sign(x); }\nstruct II { int i1, i2; };\n\nint Q, N;\nld ANS[LEN];\nint ans = 0;\nstruct Pos {\n\tld x, y;\n\tPos(ld X = 0, ld Y = 0) : x(X), y(Y) {}\n\tbool operator == (const Pos& p) const { return zero(x - p.x) && zero(y - p.y); }\n\tbool operator != (const Pos& p) const { return !zero(x - p.x) || !zero(y - p.y); }\n\tbool operator < (const Pos& p) const { return zero(x - p.x) ? y < p.y : x < p.x; }\n\tPos operator + (const Pos& p) const { return { x + p.x, y + p.y }; }\n\tPos operator - (const Pos& p) const { return { x - p.x, y - p.y }; }\n\tPos operator * (const ld& scalar) const { return { x * scalar, y * scalar }; }\n\tPos operator / (const ld& scalar) const { return { x / scalar, y / scalar }; }\n\tld operator * (const Pos& p) const { return { x * p.x + y * p.y }; }\n\tld operator / (const Pos& p) const { return { x * p.y - y * p.x }; }\n\tPos operator - () const { return { -x, -y }; }\n\tPos operator ~ () const { return { -y, x }; }\n\tPos unit() const { return *this / mag(); }\n\tld Euc() const { return x * x + y * y; }\n\tld mag() const { return sqrt(Euc()); }\n\tld rad() const { return atan2(y, x); }\n\tint quad() const { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }\n\tfriend bool cmpq(const Pos& a, const Pos& b) { return (a.quad() != b.quad()) ? a.quad() < b.quad() : a / b > 0; }\n\tfriend std::istream& operator >> (std::istream& is, Pos& p) { is >> p.x >> p.y; return is; }\n\tfriend std::ostream& operator << (std::ostream& os, const Pos& p) { os << p.x << \" \" << p.y; return os; }\n}; const Pos O = { 0, 0 };\ntypedef std::vector<Pos> Polygon;\nstruct Linear {//ps[0] -> ps[1] ::\n\tPos ps[2];\n\tPos dir_;\n\tconst Pos& operator [] (const int& i) const { return ps[i]; }\n\tconst Pos& dir() const { return dir_; }\n\tLinear(Pos a = Pos(0, 0), Pos b = Pos(0, 0)) {\n\t\tps[0] = a;\n\t\tps[1] = b;\n\t\tdir_ = (ps[1] - ps[0]).unit();\n\t}\n\tbool include(const Pos& p) const { return sign(dir_ / (p - ps[0])) > 0; }\n\tfriend bool parallel(const Linear& l0, const Linear& l1) { return zero(l0.dir() / l1.dir()); }\n\tfriend bool same_dir(const Linear& l0, const Linear& l1) { return parallel(l0, l1) && l0.dir() * l1.dir() > 0; }\n\tbool operator < (const Linear& l0) const {\n\t\tif (same_dir(*this, l0)) return l0.include(ps[0]);\n\t\telse return cmpq(this->dir(), l0.dir());\n\t}\n};\ntypedef std::vector<Linear> Planes;\nld cross(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) / (d3 - d2); }\nint ccw(const Pos& d1, const Pos& d2, const Pos& d3) { ld ret = cross(d1, d2, d3); return sign(ret); }\nld dot(const Pos& d1, const Pos& d2, const Pos& d3) { return (d2 - d1) * (d3 - d2); }\nbool on_seg_strong(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) >= 0; }\nbool on_seg_weak(const Pos& d1, const Pos& d2, const Pos& d3) { return !ccw(d1, d2, d3) && sign(dot(d1, d3, d2)) > 0; }\nPos intersection(const Pos& p1, const Pos& p2, const Pos& q1, const Pos& q2) { ld a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2); return (p1 * a2 + p2 * a1) / (a1 + a2); }\nPos intersection(const Linear& l1, const Linear& l2) { return intersection(l1[0], l1[1], l2[0], l2[1]); }\nPolygon half_plane_intersection(std::vector<Linear>& HP) {\n\tauto check = [&](Linear& u, Linear& v, Linear& w) -> bool {\n\t\treturn w.include(intersection(u, v));\n\t\t};\n\tstd::sort(HP.begin(), HP.end());\n\tstd::deque<Linear> dq;\n\tint sz = HP.size();\n\tfor (int i = 0; i < sz; ++i) {\n\t\tif (i && same_dir(HP[i], HP[(i - 1) % sz])) continue;\n\t\twhile (dq.size() > 1 && !check(dq[dq.size() - 2], dq[dq.size() - 1], HP[i])) dq.pop_back();\n\t\twhile (dq.size() > 1 && !check(dq[1], dq[0], HP[i])) dq.pop_front();\n\t\tdq.push_back(HP[i]);\n\t}\n\twhile (dq.size() > 2 && !check(dq[dq.size() - 2], dq[dq.size() - 1], dq[0])) dq.pop_back();\n\twhile (dq.size() > 2 && !check(dq[1], dq[0], dq[dq.size() - 1])) dq.pop_front();\n\tsz = dq.size();\n\tif (sz < 3) return {};\n\tPolygon HPI;\n\tfor (int i = 0; i < sz; ++i) HPI.push_back(intersection(dq[i], dq[(i + 1) % sz]));\n\treturn HPI;\n}\nld area(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += H[i] / H[(i + 1) % sz];\n\treturn ret * .5;\n}\nld round(const Polygon& H) {\n\tld ret = 0;\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) ret += (H[i] - H[(i + 1) % sz]).mag();\n\treturn ret;\n}\nPos mirror(const Linear& l, const Pos& p) {\n\tPos s = l[0], e = l[1];\n\tif (!ccw(s, e, p)) return p;\n\tPos v = ~(e - s);\n\tPos m = intersection(s, e, p, p + v);\n\tv = m - p;\n\treturn p + v + v;\n}\nint inner_check_bi_search(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return -1;\n\tif (on_seg_strong(H[0], H[1], p) || on_seg_strong(H[0], H[sz - 1], p)) return 0;\n\tint s = 0, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return 1;\n\telse if (on_seg_strong(H[s], H[e], p)) return 0;\n\telse return -1;\n}\nint inner_check_bi_search_2(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return -1;\n\tif (p == H[0] || p == H[1] || p == H[sz - 1] ) return 1;\n\tif (on_seg_weak(H[0], H[1], p) || on_seg_weak(H[0], H[sz - 1], p)) return 0;\n\t//if (on_seg_strong(H[0], H[1], p) || on_seg_strong(H[0], H[sz - 1], p)) return 0;\n\tint s = 0, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return 2;\n\telse if (H[s] == p || H[e] == p) return 1;\n\telse if (on_seg_weak(H[s], H[e], p)) return 0;\n\t//else if (on_seg_strong(H[s], H[e], p)) return 0;\n\telse return -1;\n}\nint inner_check(const Polygon& H, const Pos& p) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i1 = (i + 1) % sz;\n\t\tif (H[i] == p) return 1;\n\t\tif (H[i1] == p) return 1;\n\t\tif (on_seg_weak(H[i], H[i1], p)) return 0;\n\t\tif (ccw(H[i], H[i1], p) < 0) return -1;\n\t}\n\treturn 2;\n}\nII find_(const std::vector<Pos>& H, const Pos& p) {//convex\n\tint sz = H.size();\n\t//if (!sz) return -1;\n\t//if (sz == 1) return p == H[0] ? 0 : -1;\n\t//if (sz == 2) return on_seg_strong(H[0], H[1], p) ? 0 : -1;\n\tassert(sz >= 3);\n\tif (cross(H[0], H[1], p) < 0 || cross(H[0], H[sz - 1], p) > 0) return { -1, -1 };\n\tif (p == H[0]) return { 0, -1 };\n\tif (p == H[1]) return { 1, -1 };\n\tif (p == H[sz - 1]) return { sz - 1, -1 };\n\tif (on_seg_strong(H[0], H[1], p)) return { 0, 1 };\n\tif (on_seg_strong(H[0], H[sz - 1], p)) return { 0, sz - 1 };\n\tint s = 0, e = sz - 1, m;\n\twhile (s + 1 < e) {\n\t\tm = s + e >> 1;\n\t\tif (cross(H[0], H[m], p) > 0) s = m;\n\t\telse e = m;\n\t}\n\tif (cross(H[s], H[e], p) > 0) return { -1, -1 };\n\telse if (H[s] == p) return { s, -1 };\n\telse if (H[e] == p) return { e, -1 };\n\telse if (on_seg_strong(H[s], H[e], p)) return { s, e };\n\telse return { -1, -1 };\n}\nII find(const Polygon& H, const Pos& p) {\n\tint sz = H.size();\n\tfor (int i = 0; i < sz; i++) {\n\t\tint i1 = (i + 1) % sz;\n\t\tif (H[i] == p) return { i, -1 };\n\t\tif (H[i1] == p) return { i1, -2 };\n\t\tif (on_seg_weak(H[i], H[i1], p)) return { i, i1 };\n\t\tif (ccw(H[i], H[i1], p) < 0) return { -1, -1 };\n\t}\n\treturn { -1, -1 };\n}\nbool query() {//O(2000)\n\tstd::cin >> N;\n\tif (!N) return 0;\n\tld ret = 0;\n\tint V = 0;\n\tPolygon H(N);\n\tfor (Pos& p : H) std::cin >> p;\n\tfor (int i = 0; i < N; i++) {\n\t\tfor (int j = i + 1; j < N; j++) {//(O(2000 * 20 * 20) == O(800000)\n\t\t\tPos m = (H[i] + H[j]) * .5;\n\t\t\tPos v = ~(H[i] - H[j]);\n\t\t\tLinear I = Linear(m, m + v);\n\t\t\tLinear J = Linear(m, m - v);\n\t\t\tPlanes P0, Pi, Pj;\n\t\t\tfor (int k = 0; k < N; k++) P0.push_back(Linear(H[k], H[(k + 1) % N]));\n\t\t\tPi = P0;\n\t\t\tPi.push_back(I);\n\t\t\tPolygon HI = half_plane_intersection(Pi);\n\t\t\tPj = P0;\n\t\t\tPj.push_back(J);\n\t\t\tPolygon HJ = half_plane_intersection(Pj);\n\t\t\tfor (Pos& p : HJ) p = mirror(J, p);\n\t\t\tstd::reverse(HJ.begin(), HJ.end());\n\t\t\tP0.clear();\n\t\t\tint sz = HI.size();\n\t\t\tfor (int k = 0; k < sz; k++) P0.push_back(Linear(HI[k], HI[(k + 1) % sz]));\n\t\t\tsz = HJ.size();\n\t\t\tfor (int k = 0; k < sz; k++) P0.push_back(Linear(HJ[k], HJ[(k + 1) % sz]));\n\t\t\tPolygon hpi = half_plane_intersection(P0);\n\t\t\tld R = round(HI) + round(HJ) - round(hpi);\n\t\t\tint ni = HI.size(), nj = HJ.size();\n\t\t\tint ij = 0;\n\t\t\tfor (const Pos& p : HI)\n\t\t\t\tif (inner_check_bi_search(hpi, p) >= 0 ||\n\t\t\t\t\tinner_check_bi_search(HJ, p) >= 0) ni--;\n\t\t\tfor (const Pos& p : HJ)\n\t\t\t\tif (inner_check_bi_search(hpi, p) >= 0 ||\n\t\t\t\t\tinner_check_bi_search(HI, p) >= 0) nj--;\n\t\t\tfor (const Pos& p : hpi) {\n\t\t\t\t//int ii = inner_check_bi_search_2(HI, p);\n\t\t\t\t//int jj = inner_check_bi_search_2(HJ, p);\n\t\t\t\tint ii = inner_check(HI, p);\n\t\t\t\tint jj = inner_check(HJ, p);\n\t\t\t\t//assert(ii != -1 && jj != -1);\n\t\t\t\t//if (ii == -1 || jj == -1) {\n\t\t\t\t//\tstd::cout << \"DEBUG:: FUCK::\\n\";\n\t\t\t\t//\tstd::cout << \"H = [\\n\";\n\t\t\t\t//\tfor (Pos& p : H) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"hpi = [\\n\";\n\t\t\t\t//\tfor (Pos& p : hpi) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"HI = [\\n\";\n\t\t\t\t//\tfor (Pos& p : HI) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"HJ = [\\n\";\n\t\t\t\t//\tfor (Pos& p : HJ) std::cout << \"(\" << p.x << \", \" << p.y << \"),\\n\";\n\t\t\t\t//\tstd::cout << \"]\\n\";\n\t\t\t\t//\tstd::cout << \"DEBUG:: FUCK::\\n\";\n\t\t\t\t//}\n\t\t\t\tif (ii == 1 && jj == 1) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint i1 = iii.i1, i2 = (i1 + 1) % HI.size(), i0 = (i1 - 1 + HI.size()) % HI.size();\n\t\t\t\t\tint j1 = jjj.i1, j2 = (j1 + 1) % HJ.size(), j0 = (j1 - 1 + HJ.size()) % HJ.size();\n\t\t\t\t\t//std::cout << i0 << \" \" << i1 << \" \" << i2 << \"\\n\";\n\t\t\t\t\t//std::cout << j0 << \" \" << j1 << \" \" << j2 << \"\\n\";\n\t\t\t\t\tif (!ccw(HI[i0], HI[i1], HJ[j2]) || !ccw(HJ[j0], HJ[j1], HI[i2])) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 0 && jj == 0) continue;\n\t\t\t\telse if (ii == 0 && jj == 1) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint i1 = iii.i1, i2 = iii.i2;\n\t\t\t\t\tint j1 = jjj.i1, j2 = (j1 + 1) % HJ.size(), j0 = (j1 - 1 + HJ.size()) % HJ.size();\n\t\t\t\t\tint c1 = ccw(HI[i1], HI[i2], HJ[j0]);\n\t\t\t\t\tint c2 = ccw(HI[i1], HI[i2], HJ[j2]);\n\t\t\t\t\tif (!c1 || !c2 || c1 * c2 == 1) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 1 && jj == 0) {\n\t\t\t\t\tII iii = find(HI, p);\n\t\t\t\t\tII jjj = find(HJ, p);\n\t\t\t\t\tint j1 = jjj.i1, j2 = jjj.i2;\n\t\t\t\t\tint i1 = iii.i1, i2 = (i1 + 1) % HI.size(), i0 = (i1 - 1 + HI.size()) % HI.size();\n\t\t\t\t\tint c1 = ccw(HJ[j1], HJ[j2], HI[i0]);\n\t\t\t\t\tint c2 = ccw(HJ[j1], HJ[j2], HI[i2]);\n\t\t\t\t\tif (!c1 || !c2 || c1 * c2 == 1) ij++;\n\t\t\t\t}\n\t\t\t\telse if (ii == 2 || jj == 2) ij++;\n\t\t\t}\n\t\t\tint n = hpi.size() + ni + nj - ij;\n\t\t\tif (V < n) {\n\t\t\t\tV = n;\n\t\t\t\tret = R;\n\t\t\t}\n\t\t\telse if (V == n) {\n\t\t\t\tV = n;\n\t\t\t\tret = std::max(ret, R);\n\t\t\t}//TOTAL O(2000 * 20 * 20 * 20 * log(20)) ~= O(80000000)\n\t\t}\n\t}\n\t//std::cout << ret << \"\\n\";\n\tANS[ans++] = ret;\n\treturn 1;\n}\nvoid solve() {\n\tstd::cin.tie(0)->sync_with_stdio(0);\n\tstd::cout.tie(0);\n\tstd::cout << std::fixed;\n\tstd::cout.precision(15);\n\twhile (query());\n\tfor (int i = 0; i < ans; i++) std::cout << ANS[i] << \"\\n\";\n\treturn;\n}\nint main() { solve(); return 0; }//boj11044", "accuracy": 1, "time_ms": 10, "memory_kb": 3652, "score_of_the_acc": -1, "final_rank": 5 }, { "submission_id": "aoj_1606_6023149", "code_snippet": "//#pragma GCC optimize(\"O3\")\n//#pragma GCC optimize(\"unroll-loops\")\n#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<unordered_set>\n#include<utility>\n#include<cassert>\n#include<complex>\n#include<numeric>\n#include<array>\nusing namespace std;\n\n//#define int long long\ntypedef long long ll;\n\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 998244353;\nconst ll INF = mod * mod;\ntypedef pair<int, int>P;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\n#define all(v) (v).begin(),(v).end()\ntypedef pair<ll, ll> LP;\ntypedef long double ld;\ntypedef pair<ld, ld> LDP;\nconst ld eps = 1e-8;\nconst ld pi = acosl(-1.0);\n\nll mod_pow(ll x, ll n, ll m = mod) {\n\tif (n < 0) {\n\t\tll res = mod_pow(x, -n, m);\n\t\treturn mod_pow(res, m - 2, m);\n\t}\n\tif (abs(x) >= m)x %= m;\n\tif (x < 0)x += m;\n\tll res = 1;\n\twhile (n) {\n\t\tif (n & 1)res = res * x % m;\n\t\tx = x * x % m; n >>= 1;\n\t}\n\treturn res;\n}\nstruct modint {\n\tll n;\n\tmodint() :n(0) { ; }\n\tmodint(ll m) :n(m) {\n\t\tif (n >= mod)n %= mod;\n\t\telse if (n < 0)n = (n % mod + mod) % mod;\n\t}\n\toperator int() { return n; }\n};\nbool operator==(modint a, modint b) { return a.n == b.n; }\nmodint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }\nmodint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }\nmodint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }\nmodint operator+(modint a, modint b) { return a += b; }\nmodint operator-(modint a, modint b) { return a -= b; }\nmodint operator*(modint a, modint b) { return a *= b; }\nmodint operator^(modint a, ll n) {\n\tif (n == 0)return modint(1);\n\tmodint res = (a * a) ^ (n / 2);\n\tif (n % 2)res = res * a;\n\treturn res;\n}\n\nll inv(ll a, ll p) {\n\treturn (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);\n}\nmodint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }\nmodint operator/=(modint& a, modint b) { a = a / b; return a; }\nconst int max_n = 1 << 2;\nmodint fact[max_n], factinv[max_n];\nvoid init_f() {\n\tfact[0] = modint(1);\n\tfor (int i = 0; i < max_n - 1; i++) {\n\t\tfact[i + 1] = fact[i] * modint(i + 1);\n\t}\n\tfactinv[max_n - 1] = modint(1) / fact[max_n - 1];\n\tfor (int i = max_n - 2; i >= 0; i--) {\n\t\tfactinv[i] = factinv[i + 1] * modint(i + 1);\n\t}\n}\nmodint comb(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[b] * factinv[a - b];\n}\nmodint combP(int a, int b) {\n\tif (a < 0 || b < 0 || a < b)return 0;\n\treturn fact[a] * factinv[a - b];\n}\n\n\n\ntypedef complex<ld> Point;\nld dot(Point a, Point b) { return real(conj(a) * b); }\nld cross(Point a, Point b) { return imag(conj(a) * b); }\nnamespace std {\n\tbool operator<(const Point& lhs, const Point& rhs) {\n\t\treturn lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();\n\t}\n}\nstruct Line {\n\tPoint a, b;\n};\nstruct Circle {\n\tPoint p; ld r;\n};\nint ccw(Point a, Point b, Point c) {\n\tb -= a; c -= a;\n\tif (cross(b, c) > eps)return 1;//counter clockwise\n\tif (cross(b, c) < -eps)return -1;//clock wise\n\tif (dot(b, c) < 0)return 2;//c--a--b on line\n\tif (norm(b) < norm(c))return -2;//a--b--c on line\n\treturn 0; //a--c--b on line\n}\nbool eq(ld a, ld b) {\n\treturn abs(a - b) < eps;\n}\n//2直線の交差判定\nbool isis_ll(Line l, Line m) {\n\treturn !eq(cross(l.b - l.a, m.b - m.a), 0);\n}\n//直線と線分の交差判定\nbool isis_ls(Line l, Line s) {\n\treturn (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);\n}\n//点が直線上に存在するか\nbool isis_lp(Line l, Point p) {\n\treturn (abs(cross(l.b - p, l.a - p)) < eps);\n}\n//点が線分上に存在するか\nbool isis_sp(Line s, Point p) {\n\t//誤差がisis_lpに比べて大きいので、できるだけisis_lpを使う\n\treturn (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);\n}\n//線分と線分の交差判定\n//bool isis_ss(Line s, Line t) {\n//\treturn(cross(s.b - s.a, t.a - s.a)*cross(s.b - s.a, t.b - s.a) < -eps && cross(t.b - t.a, s.a - t.a)*cross(t.b - t.a, s.b - t.a) < -eps);\n//}\n//線分と線分の交差判定2\nbool isis_ss(Line s, Line t) {\n\treturn ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\n//点から直線への垂線の足\nPoint proj(Line l, Point p) {\n\tld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n\treturn l.a + t * (l.a - l.b);\n}\n//直線と直線の交点\n//平行な2直線に対しては使うな!!!!\nPoint is_ll(Line s, Line t) {\n\tPoint sv = s.b - s.a; Point tv = t.b - t.a;\n\treturn s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);\n}\ntypedef vector<Point> polygon;\npolygon ConvexHull(polygon p) {\n\tint n = p.size();\n\tint k = 0;\n\tsort(p.begin(), p.end());\n\tpolygon ch(2 * n);\n\tfor (int i = 0; i < n; ch[k++] = p[i++]) {\n\t\twhile (k >= 2 && ccw(ch[k - 2], ch[k - 1], p[i]) <= 0)--k;\n\t}\n\tfor (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {\n\t\twhile (k >= t && ccw(ch[k - 2], ch[k - 1], p[i]) <= 0)--k;\n\t}\n\tch.resize(k - 1);\n\treturn ch;\n}\nld area(const polygon& p) {\n\tld ret = 0;\n\tint n = p.size();\n\trep(j, n)ret += cross(p[j], p[(j + 1) % n]);\n\treturn ret / 2.0;\n}\npolygon convex_cut(const polygon& p, Line l) {\n\tvector<Point> ret;\n\trep(i, p.size()) {\n\t\tPoint a = p[i], b = p[(i + 1) % p.size()];\n\t\tif (ccw(l.a, l.b, a) == 1)ret.push_back(a);\n\t\tif (ccw(l.a, l.b, a) * ccw(l.a, l.b, b) < 0) {\n\t\t\tret.push_back(is_ll({ a,b }, l));\n\t\t}\n\t}\n\treturn ret;\n}\n//2点の垂直二等分線\nLine mid_line(Point a, Point b) {\n\tld mx = (real(a) + real(b)) / 2.0, my = (imag(a) + imag(b)) / 2.0;\n\tld dx = real(b) - real(a), dy = imag(b) - imag(a);\n\tswap(dx, dy); dx = -dx;\n\tPoint le = { mx - dx,my - dy }, ri = { mx + dx,my + dy };\n\t//a,le,ri is counter clockwise\n\treturn { le,ri };\n}\nvector<Point> is_ss(Line s1, Line s2) {\n\tif (!isis_ss(s1, s2))return {};\n\tvector<Point> res;\n\tif (abs(cross(s1.b - s1.a, s2.b - s2.a)) < eps) {\n\t\tif (isis_sp(s1, s2.a)) res.push_back(s2.a);\n\t\tif (isis_sp(s1, s2.b)) res.push_back(s2.b);\n\t\tif (isis_sp(s2, s1.a)) res.push_back(s1.a);\n\t\tif (isis_sp(s2, s1.b)) res.push_back(s1.b);\n\t}\n\telse {\n\t\tres.push_back(is_ll(s1, s2));\n\t}\n\treturn res;\n}\nint is_in_polygon(polygon& poly, Point p) {\n\tld sum = 0;\n\tint n = poly.size();\n\trep(i, n) {\n\t\tint ni = i + 1 == n ? 0 : i + 1;\n\t\tPoint pl = poly[i];\n\t\tPoint pr = poly[ni];\n\t\tLine e = { pl,pr };\n\t\tif (isis_sp(e, p))return 0;\n\t\tsum += arg((pr - p) / (pl - p));\n\t}\n\tif (abs(sum) < pi / 2)return 2;\n\treturn 1;\n}\nPoint rev(Line l, Point p) {\n\tPoint h = proj(l, p);\n\treturn h + (h - p);\n}\n\nint dx[4] = { 1,0,-1,0 };\nint dy[4] = { 0,1,0,-1 };\n\nvector<Line> outside(polygon& pl, polygon& pr) {\n\tvector<Line> res;\n\trep(x, pl.size()) {\n\t\tvector<ld> ts;\n\t\tts.push_back(0);\n\t\tts.push_back(1);\n\t\tint nx = x + 1 == pl.size() ? 0 : x + 1;\n\t\tLine l1 = { pl[x],pl[nx] };\n\t\trep(y, pr.size()) {\n\t\t\tint ny = y + 1 == pr.size() ? 0 : y + 1;\n\t\t\tLine l2 = { pr[y],pr[ny] };\n\t\t\tif (isis_ss(l1, l2)) {\n\t\t\t\tauto vp = is_ss(l1, l2);\n\t\t\t\tfor (Point p : vp) {\n\t\t\t\t\tld t = abs(p - l1.a) / (abs(l1.b - l1.a));\n\t\t\t\t\tts.push_back(t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsort(all(ts));\n\t\trep(k, ts.size() - 1) {\n\t\t\tif (abs(ts[k] - ts[k + 1]) < eps)continue;\n\t\t\tPoint lp = l1.a + (l1.b - l1.a) * ts[k];\n\t\t\tPoint rp = l1.a + (l1.b - l1.a) * ts[k + 1];\n\t\t\tPoint mp = (lp + rp) / (ld)2.0;\n\t\t\tif (is_in_polygon(pr, mp) != 1) {\n\t\t\t\tres.push_back({ lp,rp });\n\t\t\t}\n\t\t}\n\t}\n\t//cout << res.size() << \"\\n\";\n\treturn res;\n}\nint n;\nvoid solve() {\n\tvector<Point> p(n);\n\trep(i, n) {\n\t\tint x, y; cin >> x >> y;\n\t\tp[i] = { (ld)x,(ld)y };\n\t}\n\tint macnt = -1;\n\tld malen = -1;\n\trep(i, n)Rep(j, i + 1, n) {\n\t\t//p[i] and p[j]\n\t\tLine l = mid_line(p[i], p[j]);\n\t\tpolygon pl = p, pr = p;\n\t\tpl = convex_cut(pl, l);\n\t\tswap(l.a, l.b);\n\t\tpr = convex_cut(pr, l);\n\t\trep(i, pr.size()) {\n\t\t\tpr[i] = rev(l, pr[i]);\n\t\t}\n\t\treverse(all(pr));\n\t\t//cout << i << \" \" << j << \"\\n\";\n\t\tvector<Line> lv;\n\t\tauto x = outside(pl, pr);\n\t\tfor (Line l : x)lv.push_back(l);\n\t\tx = outside(pr, pl);\n\t\tfor (Line l : x)lv.push_back(l);\n\t\tvector<Point> nw;\n\t\tauto trans = [&](Point& p) {\n\t\t\trep(i, nw.size()) {\n\t\t\t\tif (abs(nw[i] - p) < eps) {\n\t\t\t\t\tp = nw[i];\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t};\n\t\tld anslen = 0;\n\t\tvector<bool> e(lv.size());\n\t\trep(i, lv.size()) {\n\t\t\tLine& z = lv[i];\n\t\t\tif (!trans(z.a))nw.push_back(z.a);\n\t\t\tif (!trans(z.b))nw.push_back(z.b);\n\t\t\tif (z.b < z.a)swap(z.a, z.b);\n\t\t\tbool exi = false;\n\t\t\trep(j, i)if (abs(lv[i].a - lv[j].a) < eps && abs(lv[i].b - lv[j].b) < eps)exi = true;\n\t\t\tif (!exi) {\n\t\t\t\tanslen += abs(lv[i].b - lv[i].a);\n\t\t\t}\n\t\t\te[i] = exi;\n\t\t}\n\t\tint anscnt = 0;\n\t\trep(i, nw.size()) {\n\t\t\tvector<Point> vp;\n\t\t\trep(j, lv.size())if (!e[j]) {\n\t\t\t\tif (abs(lv[j].a - nw[i]) < eps) {\n\t\t\t\t\tvp.push_back(lv[j].b);\n\t\t\t\t}\n\t\t\t\tif (abs(lv[j].b - nw[i]) < eps) {\n\t\t\t\t\tvp.push_back(lv[j].a);\n\t\t\t\t}\n\t\t\t}\n\t\t\t//assert(vp.size()==2)\n\t\t\tif (isis_sp({ vp[0],vp[1] }, nw[i])) {\n\n\t\t\t}\n\t\t\telse {\n\t\t\t\tanscnt++;\n\t\t\t}\n\t\t}\n\t\t//cout << i << \" \" << j << \" \" << anscnt << \" \" << anslen << \"\\n\";\n\t\tif (macnt < anscnt) {\n\t\t\tmacnt = anscnt;\n\t\t\tmalen = anslen;\n\t\t}\n\t\telse if (macnt == anscnt) {\n\t\t\tmalen = max(malen, anslen);\n\t\t}\n\t}\n\t//cout << macnt << \" \";\n\tcout << malen << \"\\n\";\n}\n\n\n\nsigned main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tcout << fixed << setprecision(10);\n\t//init_f();\n\t//init();\n\t//int t; cin >> t; rep(i, t)\n\twhile (cin >> n, n)\n\t\tsolve();\n\treturn 0;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3588, "score_of_the_acc": -1.876, "final_rank": 7 }, { "submission_id": "aoj_1606_4774445", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS2 0.000000001\nusing namespace std;\n\n\n#define EPS 0.00001\n#define EPS2 0.0000000001\n#define EPS3 0.000001\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS2 && fabs(y-p.y) < EPS2;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3Lf,%.3Lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS2){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS2){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS2){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS2){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS2){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS2){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS2){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS2){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS2){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS2)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS2)return CLOCKWISE;\n\tif(dot(a,b) < -EPS2)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS2){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS2){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\t//from側がnear_from\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //★fromから見て反時計回りで近い方なら(from+i)%N★\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){ //★★nan対策★★\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){ //★★nan対策★★\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\t\t\t\t//時計回りの位置関係なら別途計算\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS3){ //同一直線上\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS3){ //同一直線上\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(isSame(cross_point[0],cross_point[1]))tmp_num--; //★★★★★★コーナーケースあり★★★★★★\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3256, "score_of_the_acc": -0.4548, "final_rank": 1 }, { "submission_id": "aoj_1606_4774424", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS2 0.000000001\nusing namespace std;\n\n\n#define EPS 0.00001\n#define EPS2 0.0000000001\n#define EPS3 0.000001\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS2 && fabs(y-p.y) < EPS2;\n\t}\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3Lf,%.3Lf)\\n\",x,y);\n\t}\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS2){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS2){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS2){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS2){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS2){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS2){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS2){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS2){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS2){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS2)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS2)return CLOCKWISE;\n\tif(dot(a,b) < -EPS2)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS2){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS2){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\t//printf(\"match_A.size():%lld\\n\",match_A.size());\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS3){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS3){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(isSame(cross_point[0],cross_point[1]))tmp_num--;\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t/*\tprintf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"from:%d to:%d tmp_num:%d tmp_dist:%.3Lf\\n\",from,to,tmp_num,tmp_dist);\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3308, "score_of_the_acc": -0.5556, "final_rank": 2 }, { "submission_id": "aoj_1606_4774312", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS2 0.000000001\nusing namespace std;\n\n\n#define EPS 0.0001\n#define EPS2 0.0000000001\n\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS2 && fabs(y-p.y) < EPS2;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS2){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS2){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS2){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS2){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS2){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS2){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS2){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS2){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS2){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS2)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS2)return CLOCKWISE;\n\tif(dot(a,b) < -EPS2)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS2){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS2){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3232, "score_of_the_acc": -0.4083, "final_rank": 11 }, { "submission_id": "aoj_1606_4774311", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS2 0.000000001\nusing namespace std;\n\n\n#define EPS 0.0000001\n#define EPS2 0.0000000001\n\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS2 && fabs(y-p.y) < EPS2;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS2){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS2){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS2){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS2){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS2){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS2){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS2){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS2){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS2){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS2)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS2)return CLOCKWISE;\n\tif(dot(a,b) < -EPS2)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS2){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS2){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3180, "score_of_the_acc": -0.3075, "final_rank": 9 }, { "submission_id": "aoj_1606_4774310", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS2 0.000000001\nusing namespace std;\n\n\n#define EPS 0.000001\n#define EPS2 0.0000000001\n\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS2 && fabs(y-p.y) < EPS2;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS2){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS2){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS2){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS2){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS2){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS2){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS2){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS2){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS2){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS2)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS2)return CLOCKWISE;\n\tif(dot(a,b) < -EPS2)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS2){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS2){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3224, "score_of_the_acc": -0.3928, "final_rank": 10 }, { "submission_id": "aoj_1606_4774308", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS2 0.000000001\nusing namespace std;\n\n\n#define EPS 0.00001\n#define EPS2 0.0000000001\n\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS2 && fabs(y-p.y) < EPS2;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS2){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS2){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS2){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS2){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS2){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS2){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS2){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS2){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS2){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS2)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS2)return CLOCKWISE;\n\tif(dot(a,b) < -EPS2)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS2){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS2){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3236, "score_of_the_acc": -0.416, "final_rank": 12 }, { "submission_id": "aoj_1606_4774301", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS2 0.000000001\nusing namespace std;\n\n\n#define EPS 0.000000001\n#define EPS2 0.000000001\n\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS2 && fabs(y-p.y) < EPS2;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS2){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS2){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS2){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS2){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS2){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS2){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS2){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS2){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS2){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS2)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS2)return CLOCKWISE;\n\tif(dot(a,b) < -EPS2)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS2){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS2){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3348, "score_of_the_acc": -0.6331, "final_rank": 18 }, { "submission_id": "aoj_1606_4774296", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS2 0.000000001\nusing namespace std;\n\n\n#define EPS 0.00001\n#define EPS2 0.000000001\n\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS2 && fabs(y-p.y) < EPS2;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS2){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS2){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS2){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS2){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS2){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS2){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS2){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS2){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS2){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS2)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS2)return CLOCKWISE;\n\tif(dot(a,b) < -EPS2)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS2){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS2){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3136, "score_of_the_acc": -0.2222, "final_rank": 8 }, { "submission_id": "aoj_1606_4774203", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define EPS 0.00001\n#define EPS2 0.000000001\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3316, "score_of_the_acc": -0.5711, "final_rank": 16 }, { "submission_id": "aoj_1606_4774199", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define EPS 0.00001\n#define EPS2 0.000000001\n\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tdouble x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tdouble dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\ndouble NUM = BIG_NUM;\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(double x1,double y1,double x2,double y2,double xp,double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tdouble slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tdouble x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tdouble ans = 0;\n\n\tdouble slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tdouble tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS){ //水平\n\n\t\t\t\tdouble tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tdouble tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tdouble deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tdouble cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tdouble cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 10, "memory_kb": 3476, "score_of_the_acc": -0.6589, "final_rank": 20 }, { "submission_id": "aoj_1606_4774194", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define EPS 0.00000001\n#define EPS2 0.000000001\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3312, "score_of_the_acc": -0.5633, "final_rank": 15 }, { "submission_id": "aoj_1606_4774189", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define EPS 0.00001\n#define EPS2 0.000000001\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/acos(-1));\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/acos(-1));\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3264, "score_of_the_acc": -0.4703, "final_rank": 14 }, { "submission_id": "aoj_1606_4774172", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define EPS 0.00001\n#define EPS2 0.000000001\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\t//scanf(\"%Lf %Lf\",&x,&y);\n\t\tcin >> x >> y;\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3336, "score_of_the_acc": -0.6098, "final_rank": 17 }, { "submission_id": "aoj_1606_4774162", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define EPS 0.000001\n#define EPS2 0.000001\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3260, "score_of_the_acc": -0.4625, "final_rank": 13 }, { "submission_id": "aoj_1606_4774153", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 1000000000000000000\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n\n#define EPS 0.000001\n#define EPS2 0.00000000001\n\n\nstruct Point{\n\tPoint(long double arg_x,long double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (long double a){ return Point(a*x,a*y); }\n\tPoint operator / (long double a){ return Point(x/a,y/a); }\n\n\tlong double abs(){ return sqrt(norm()); }\n\tlong double norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\t\treturn x != p.x? x < p.x: y < p.y;\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\t/*void debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}*/\n\tlong double x,y;\n};\n\nstruct Info{\n\n\tbool operator<(const struct Info &arg) const{\n\n\t\treturn dist < arg.dist;\n\t}\n\tlong double dist;\n\tPoint point;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\n\nint N;\nlong double NUM = BIG_NUM;\n\n\nlong double calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\nint func(long double x1,long double y1,long double x2, long double y2, long double xp, long double yp){\n\tlong double naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nlong double calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\n\n//交点を求める関数\nPoint calc_Cross_Point(long double x1,long double x2,long double x3,long double x4,long double y1,long double y2,long double y3,long double y4){\n\tPoint ret;\n\tret.x = ((x2-x1)*(y3*(x4-x3)+x3*(y3-y4))-(x4-x3)*(y1*(x2-x1)+x1*(y1-y2)))/((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1));\n\tif(x1 != x2){\n\t\tret.y = ((y2-y1)*ret.x+y1*(x2-x1)+x1*(y1-y2))/(x2-x1);\n\t}else{\n\t\tret.y = ((y4-y3)*ret.x+y3*(x4-x3)+x3*(y3-y4))/(x4-x3);\n\t}\n\treturn ret;\n}\n\n//インタフェース関数\nPoint calc_Cross_Point(Point a,Point b,Point c,Point d){\n\treturn calc_Cross_Point(a.x,b.x,c.x,d.x,a.y,b.y,c.y,d.y);\n}\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\nlong double norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\nlong double abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\nlong double cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\nlong double dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nlong double calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n//線分ではなく直線と点の距離\nlong double getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\nlong double getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\nlong double getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\nPoint calc_Cross_Point(Line A,Line B){\n\n\tif(getDistanceSP(B,A.p[0]) < EPS){\n\n\t\treturn A.p[0];\n\n\t}else if(getDistanceSP(B,A.p[1]) < EPS){\n\n\t\treturn A.p[1];\n\n\t}else if(getDistanceSP(A,B.p[0]) < EPS){\n\n\t\treturn B.p[0];\n\n\t}else if(getDistanceSP(A,B.p[1]) < EPS){\n\n\t\treturn B.p[1];\n\t}\n\n\treturn calc_Cross_Point(A.p[0],A.p[1],B.p[0],B.p[1]);\n}\n\nPoint calc_Reflection_Point(long double x1,long double y1,long double x2,long double y2,long double xp,long double yp){\n\n\tPoint ret;\n\n\tbool X_FLG = false,Y_FLG = false;\n\tlong double slope;\n\n\tif(y1 == y2){\n\t\tX_FLG = true;\n\t}else if(x1 == x2){\n\t\tY_FLG = true;\n\t}else{\n\t\tslope = (y2-y1)/(x2-x1);\n\t}\n\n\tif(X_FLG){\n\t\tret.x = xp,ret.y=y1;\n\t}else if(Y_FLG){\n\t\tret.x = x1,ret.y = yp;\n\t}else{\n\t\tret.x = (yp*(x2-x1)*(y2-y1)+xp*(x2-x1)*(x2-x1)-y1*(y2-y1)*(x2-x1)+x1*(y2-y1)*(y2-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));\n\t\tret.y = ((x1-x2)*ret.x+yp*(y2-y1)+xp*(x2-x1))/(y2-y1);\n\t}\n\tret.x = 2*ret.x-xp;\n\tret.y = 2*ret.y-yp;\n\n\treturn ret;\n}\n\nPoint calc_Reflection_Point(Line line,Point point){\n\n\treturn calc_Reflection_Point(line.p[0].x,line.p[0].y,line.p[1].x,line.p[1].y,point.x,point.y);\n}\n\nbool isSame(Point a, Point b){\n\n\n\treturn abs(a-b) < EPS;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -1;\nstatic const int ON_SEGMENT = 0;\n\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nvoid func(){\n\n\tPolygon P;\n\n\tlong double x,y;\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%Lf %Lf\",&x,&y);\n\t\tP.push_back(Point(x,y));\n\t}\n\n\tint num_max = 0;\n\tlong double ans = 0;\n\n\tlong double slope,slope2;\n\tLine base_line; //垂直二等分線\n\n\tint num_cross;\n\tPoint cross_point[2],tmp_cross;\n\tint near_from[2],far_from[2];\n\n\tPolygon A,B;\n\tPolygon final_A,final_B;\n\tvector<Info> info;\n\n\tVector pre_A,pre_B,now_A,now_B;\n\tvector<int> match_A,match_B;\n\n\t//fromをtoに重ねる\n\tfor(int from = 0; from < N; from++){\n\t\tfor(int to = 0; to < N; to++){\n\n\t\t\tif(to == from)continue;\n\n\t\t\tslope = calc_slope(Line(P[from],P[to]));\n\n\t\t\tif(fabs(slope-DBL_MAX) < EPS){ //垂直\n\n\t\t\t\tlong double tmp_y = (P[from].y+P[to].y)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(-NUM,tmp_y); //左\n\t\t\t\tbase_line.p[1] = Point(NUM,tmp_y); //右\n\n\t\t\t}else if(fabs(slope) < EPS){ //水平\n\n\t\t\t\tlong double tmp_x = (P[from].x+P[to].x)/2.0;\n\n\t\t\t\tbase_line.p[0] = Point(tmp_x,-NUM); //下\n\t\t\t\tbase_line.p[1] = Point(tmp_x,NUM); //上\n\n\t\t\t}else{\n\n\t\t\t\tslope2 = -1.0/slope;\n\t\t\t\tPoint tmp_mid = Point((P[from].x+P[to].x)/2.0,(P[from].y+P[to].y)/2.0);\n\n\t\t\t\tbase_line.p[0] = Point(tmp_mid.x-NUM,tmp_mid.y-NUM*slope2);\n\t\t\t\tbase_line.p[1] = Point(tmp_mid.x+NUM,tmp_mid.y+NUM*slope2);\n\t\t\t}\n\n\n\t\t\t//垂直二等分線との交点および隣接点を求める\n\t\t\tnum_cross = 0;\n\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tLine tmp_line = Line(P[(from+i)%N],P[(from+(i+1))%N]);\n\t\t\t\tif(!is_Cross(base_line,tmp_line))continue;\n\n\t\t\t\tcross_point[num_cross] = calc_Cross_Point(base_line,tmp_line);\n\n\t\t\t\tif(num_cross == 0){ //反時計回りで近い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+i)%N;\n\t\t\t\t\tfar_from[num_cross] = (from+(i+1))%N;\n\n\t\t\t\t}else{ //反時計回りで遠い方\n\n\t\t\t\t\tnear_from[num_cross] = (from+(i+1))%N;\n\t\t\t\t\tfar_from[num_cross] = (from+i)%N;\n\t\t\t\t}\n\t\t\t\tnum_cross++;\n\t\t\t}\n\n\t\t\tbool FLG = true;\n\n\t\t\t//near[0]→near[1]へ行くまでにfarを通るか\n\t\t\tif(near_from[0] != near_from[1]){\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((near_from[0]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}else if((near_from[0]+i)%N == far_from[0] || (near_from[0]+i)%N == far_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}else{\n\n\t\t\t\t//far1→far0が半時計周りか\n\n\t\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\t\tif((far_from[1]+i)%N == far_from[0]){\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}else if((far_from[1]+i)%N == near_from[0] || (far_from[1]+i)%N == near_from[1]){\n\n\t\t\t\t\t\tFLG = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//near[0]→near[1]を反時計回りで行けるようにする\n\t\t\tif(!FLG){\n\n\t\t\t\tswap(cross_point[0],cross_point[1]);\n\t\t\t\tswap(near_from[0],near_from[1]);\n\t\t\t\tswap(far_from[0],far_from[1]);\n\t\t\t}\n\n\n\n\t\t\tA.clear();\n\t\t\tB.clear();\n\n\t\t\t//printf(\"far1: %d far0:%d\\n\",far_from[1],far_from[0]);\n\n\t\t\t//とどまる方のポリゴン\n\t\t\tA.push_back(cross_point[1]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(far_from[1]+i)%N],cross_point[0])||isSame(P[(far_from[1]+i)%N],cross_point[1]))continue;\n\n\t\t\t\tA.push_back(P[(far_from[1]+i)%N]);\n\t\t\t\t//printf(\"A[%d]\",i+1);\n\t\t\t\t//A[i+1].debug();\n\t\t\t\tif((far_from[1]+i)%N == far_from[0])break;\n\t\t\t}\n\t\t\tA.push_back(cross_point[0]);\n\t\t\treverse(A.begin(),A.end()); //cross_point[0]→cross_point[1]の順にする(時計回り)\n\n\t\t\t//printf(\"\\n\");\n\n\t\t\t//動く方のポリゴン\n\t\t\tB.push_back(cross_point[0]);\n\t\t\tfor(int i = 0; i < N; i++){\n\n\t\t\t\tif(isSame(P[(near_from[0]+i)%N],cross_point[0])||isSame(P[(near_from[0]+i)%N],cross_point[1]))continue;\n\t\t\t\tPoint ref_p = calc_Reflection_Point(base_line,P[(near_from[0]+i)%N]);\n\t\t\t\tB.push_back(ref_p);\n\t\t\t\t//printf(\"B[%d]\",i);\n\t\t\t\t//ref_p.debug();\n\t\t\t\tif((near_from[0]+i)%N == near_from[1])break;\n\t\t\t}\n\t\t\tB.push_back(cross_point[1]);\n\n\n\t\t\tfinal_A.clear();\n\t\t\tfinal_B.clear();\n\n\t\t\tfinal_A.push_back(A[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int a = 1; a < A.size(); a++){\n\n\t\t\t\tLine work_A = Line(A[a-1],A[a]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int b = 0; b < B.size(); b++){\n\n\t\t\t\t\tLine work_B = Line(B[b],B[(b+1)%B.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\ttmp_info.dist = calc_dist(A[a-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tsort(info.begin(),info.end());\n\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_A.push_back(info[i].point);\n\t\t\t\t}\n\n\t\t\t\tfinal_A.push_back(A[a]);\n\t\t\t}\n\n\t\t\tfinal_B.push_back(B[0]);\n\n\t\t\t//非端点の交点があるなら、そこで線分を分割する\n\t\t\tfor(int b = 1; b < B.size(); b++){\n\n\t\t\t\tLine work_B = Line(B[b-1],B[b]);\n\n\t\t\t\tinfo.clear();\n\n\t\t\t\tfor(int a = 0; a < A.size(); a++){\n\n\t\t\t\t\tLine work_A = Line(A[a],A[(a+1)%A.size()]);\n\t\t\t\t\tif(!is_Cross(work_A,work_B))continue;\n\n\t\t\t\t\tPoint tmp_cross = calc_Cross_Point(work_A,work_B);\n\t\t\t\t\tif(!isSame(tmp_cross,work_B.p[0]) && !isSame(tmp_cross,work_B.p[1]) &&\n\t\t\t\t\t\t\t!isSame(tmp_cross,work_A.p[0]) && !isSame(tmp_cross,work_A.p[1])){\n\n\t\t\t\t\t\tInfo tmp_info;\n\t\t\t\t\t\ttmp_info.point = tmp_cross;\n\t\t\t\t\t\t//printf(\"Bの追加点:\");\n\t\t\t\t\t\t//tmp_cross.debug();\n\t\t\t\t\t\ttmp_info.dist = calc_dist(B[b-1],tmp_cross);\n\t\t\t\t\t\tinfo.push_back(tmp_info);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(info.size() == 0){\n\n\t\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfor(int i = 0; i < info.size(); i++){ //A[a-1]から見て近い順\n\n\t\t\t\t\tfinal_B.push_back(info[i].point);\n\t\t\t\t}\n\t\t\t\tfinal_B.push_back(B[b]);\n\t\t\t}\n\n\t\t\tmatch_A.clear();\n\t\t\tmatch_B.clear();\n\n\t\t\t//Bと一致する点のインデックスを昇順に列挙する\n\t\t\tfor(int a = 1; a < final_A.size()-1; a++){\n\t\t\t\tfor(int b = 0; b < final_B.size(); b++){\n\t\t\t\t\tif(isSame(final_A[a],final_B[b])){\n\n\t\t\t\t\t\t//printf(\"★A重なり:\");\n\t\t\t\t\t\t//final_A[a].debug();\n\t\t\t\t\t\tmatch_A.push_back(a);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor(int b = 1; b < final_B.size()-1; b++){\n\t\t\t\tfor(int a = 0; a < final_A.size(); a++){\n\t\t\t\t\tif(isSame(final_B[b],final_A[a])){\n\n\t\t\t\t\t\t//printf(\"★B重なり:\");\n\t\t\t\t\t\t//final_B[b].debug();\n\t\t\t\t\t\tmatch_B.push_back(b);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//match_A.size() == match_B.size()であるはず\n\n\t\t\t//cross_point[1]を最後に加える\n\t\t\tmatch_A.push_back(final_A.size()-1);\n\t\t\tmatch_B.push_back(final_B.size()-1);\n\n\t\t\t//角度計算のためのvector\n\t\t\tpre_A = cross_point[1]-cross_point[0];\n\t\t\tpre_B = pre_A;\n\n\t\t\tlong double tmp_dist = calc_dist(cross_point[0],cross_point[1]);\n\n\t\t\tint index_A = 0,index_B = 0;\n\t\t\tint tmp_num = 1;\n\n\t\t\tPoint pre_P = cross_point[1];\n\n\t\t\t//printf(\"\\n\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tlong double deg_A,deg_B;\n\n\t\t\tfor(int i = 0; i < match_A.size(); i++){\n\n\t\t\t\tif(i > 0){\n\t\t\t\t\tpre_A = pre_P-final_A[index_A];\n\t\t\t\t\tpre_B = pre_P-final_B[index_B];\n\t\t\t\t}\n\n\t\t\t\tnow_A = final_A[(index_A+1)]-final_A[index_A];\n\t\t\t\tnow_B = final_B[(index_B+1)]-final_B[index_B];\n\n\t\t\t\tlong double cos_A = dot(pre_A,now_A)/(abs(now_A)*abs(pre_A));\n\t\t\t\tlong double cos_B = dot(pre_B,now_B)/(abs(now_B)*abs(pre_B));\n\n\t\t\t\tif(fabs(cos_A+1.0) < EPS2){\n\n\t\t\t\t\tdeg_A = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_A = acos(cos_A)*(180/M_PI);\n\t\t\t\t}\n\t\t\t\tif(fabs(cos_B+1.0) < EPS2){\n\n\t\t\t\t\tdeg_B = 180.0;\n\t\t\t\t}else{\n\n\t\t\t\t\tdeg_B = acos(cos_B)*(180/M_PI);\n\t\t\t\t}\n\n\n\t\t\t\t//printf(\"i:%d\\n\",i);\n\n\t\t\t\tif(ccw(final_A[index_A],pre_P,final_A[(index_A+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Aは時計回り\\n\");\n\t\t\t\t\tdeg_A = 360.0-deg_A;\n\t\t\t\t}\n\t\t\t\tif(ccw(final_B[index_B],pre_P,final_B[(index_B+1)]) == CLOCKWISE){\n\n\t\t\t\t\t//printf(\"Bは時計回り\\n\");\n\t\t\t\t\tdeg_B = 360.0-deg_B;\n\t\t\t\t}\n\n\t\t\t\t//printf(\"deg_A:%.3lf deg_B:%.3lf\\n\",deg_A,deg_B);\n\n\t\t\t\tif(deg_A > deg_B){\n\n\t\t\t\t\t//printf(\"Aの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_A-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\t//より外側に位置する方の辺の長さを足す\n\t\t\t\t\tfor(int k = index_A+1; k <= match_A[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_A[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_A[k],final_A[k-1]);\n\t\t\t\t\t\tpre_P = final_A[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\n\t\t\t\t}else{\n\n\t\t\t\t\t//printf(\"Bの方が大きい\\n\");\n\n\t\t\t\t\tif(fabs(deg_B-180.0) < EPS2){ //同一直線上\n\t\t\t\t\t\t//printf(\"頂点へらし\\n\");\n\t\t\t\t\t\ttmp_num--;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor(int k = index_B+1; k <= match_B[i]; k++){\n\n\t\t\t\t\t\t//printf(\"追加点:\");\n\t\t\t\t\t\t//final_B[k].debug();\n\t\t\t\t\t\ttmp_dist += calc_dist(final_B[k],final_B[k-1]);\n\t\t\t\t\t\tpre_P = final_B[k-1];\n\t\t\t\t\t\ttmp_num++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tindex_A = match_A[i];\n\t\t\t\tindex_B = match_B[i];\n\n\t\t\t}\n\n\t\t\tif(tmp_num < num_max)continue;\n\n\t\t\t/*printf(\"\\nfrom:%d to:%d\\n\",from,to);\n\n\t\t\tprintf(\"交点0:\");\n\t\t\tcross_point[0].debug();\n\t\t\tprintf(\"交点1:\");\n\t\t\tcross_point[1].debug();\n\n\t\t\tfor(int i = 0; i < 2; i++){\n\n\t\t\t\tprintf(\"near[%d]:%d far:%d\\n\",i,near_from[i],far_from[i]);\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴン とどまる\\n\");\n\t\t\tfor(int i = 0; i < A.size(); i++){\n\n\t\t\t\tA[i].debug();\n\t\t\t}\n\n\t\t\tprintf(\"\\nポリゴん 動く\\n\");\n\t\t\tfor(int i= 0; i < B.size(); i++){\n\n\t\t\t\tB[i].debug();\n\t\t\t}*/\n\n\t\t\t//printf(\"tmp_num:%d tmp_dist:%.3lf\\n\",tmp_num,tmp_dist);\n\n\n\t\t\tif(tmp_num > num_max){\n\n\t\t\t\tnum_max = tmp_num;\n\t\t\t\tans = tmp_dist;\n\n\t\t\t}else{ //tmp_num == num_max\n\n\t\t\t\tans = max(ans,tmp_dist);\n\t\t\t}\n\t\t}\n\t}\n\n\t//printf(\"num_max:%d\\n\",num_max);\n\n\tprintf(\"%.15Lf\\n\",ans);\n}\n\nint main(){\n\n\twhile(true){\n\n\t\tscanf(\"%d\",&N);\n\t\tif(N == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 0.5, "time_ms": 30, "memory_kb": 3356, "score_of_the_acc": -0.6486, "final_rank": 19 } ]
aoj_1603_cpp
500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, eve ...(truncated)
[ { "submission_id": "aoj_1603_11044339", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1 << 30;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n \nvoid solve(int n) {\n vll x(n);cin >> x;\n\n // dp[i][j][k] := iまでみてj枚500円玉があり、k円小銭があるときの支払う総額の最小値\n // dp[i][k] := (500enndama no maisuu, tukattakingaku)\n ll lim = 500 * 100 + 10;\n vector<vpll>dp(n+1,vector<pll>(lim,{0,INF}));\n dp[0][0] = {0,0};\n auto cng = [&](pll &a, pll b){\n if(a.first < b.first){\n a = b;\n }else if(a.first == b.first && a.second > b.second)a = b;\n }; \n rep(i,n){\n // vector<unordered_map<ll,ll>>ndp(n+1);\n rrep(j,lim-1,0)if(dp[i][j] != pll{0,INF}){\n // if(j+n-i-1 <jmax)continue;\n auto [num,val] = dp[i][j];\n cng(dp[i+1][j],dp[i][j]);\n //500円ちょうどを作り出す\n ll rem = (x[i] + 500) % 1000;\n if(rem <= j){\n ll nk = j -rem;\n cng(dp[i+1][nk],pll{num+1,val + x[i]});\n }\n //1000の倍数で払う\n if(x[i] % 1000 != 0){\n ll payval = lceil(x[i],1000) * 1000;\n if(x[i] % 1000 <= 500){\n //500円もらえる\n ll nk = j + payval - x[i] -500;\n // if(500 * (n-i+1) < nk)continue;\n cng(dp[i+1][nk],pll{num+1,val+x[i]});\n }else{\n //500円もらえない\n ll nk = j + payval -x[i];\n // if(500 * (n-i+1) < nk)continue;\n cng(dp[i+1][nk],pll{num,val+x[i]});\n }\n }\n }\n }\n /*\n debug\n \n */\n // cout <<\"i :\" << i << endl; \n // repn(j,n){\n // cout <<\"j :\" << j << endl;\n // if(!dp[j].empty()){\n // for(auto &[k,cost]:dp[j]){\n // cout << k << \" \" << cost << endl;\n // }\n // }\n // }\n // cout << endl;\n ll num = 0;\n ll best = INF;\n rep(i,lim){\n if(chmax(num,dp[n][i].first)){\n best = dp[n][i].second;\n }else if(num == dp[n][i].first){\n chmin(best,dp[n][i].second);\n }\n }\n cout << num << \" \" << best << endl;\n \n\n}\n\n\nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while (true) {\n int n; cin >> n;\n if (n == 0) break;\n solve(n);\n }\n \n}", "accuracy": 1, "time_ms": 360, "memory_kb": 43392, "score_of_the_acc": -0.3008, "final_rank": 9 }, { "submission_id": "aoj_1603_10852951", "code_snippet": "#include <bits/stdc++.h>\n \n#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )\n#define REP(i,n) FOR(i,0,n)\n#define YYS(x,arr) for(auto& x:arr)\n#define ALL(x) (x).begin(),(x).end()\n#define SORT(x) sort( (x).begin(),(x).end() )\n#define REVERSE(x) reverse( (x).begin(),(x).end() )\n#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )\n#define PW(x) (1LL<<(x))\n#define SZ(x) ((ll)(x).size())\n#define SHOW(x) cout << #x << \" = \" << x << endl\n#define SHOWA(x,n) for( int yui = 0; yui < n; yui++ ){ cout << x[yui] << \" \"; } cout << endl\n\n#define pb emplace_back\n#define fi first\n#define se second\n\nusing namespace std;\n\ntypedef long double ld;\ntypedef long long int ll;\ntypedef pair<int,int> pi;\ntypedef pair<ll,ll> pl;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<bool> vb;\ntypedef vector<ld> vd;\ntypedef vector<pi> vpi;\ntypedef vector<pl> vpl;\ntypedef vector<vpl> gr;\ntypedef vector<vl> ml;\ntypedef vector<vd> md;\ntypedef vector<vi> mi;\n \nconst ll INF = (ll)1e9 + 10;\nconst ll INFLL = (ll)1e18 + 10;\nconst ld EPS = 1e-12;\nconst ll MOD = 1e9+7;\n \ntemplate<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }\ntemplate<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }\ntemplate<class T> inline T sq( T a ){ return a * a; }\n\nll in(){ long long int x; scanf( \"%lld\" , &x ); return x; }\nchar yuyushiki[1000010]; string stin(){ scanf( \"%s\" , yuyushiki ); return yuyushiki; }\n\n// head\n\nconst int N = 50010;\n\npi dp[128][N];\n\nint n;\nint p[128];\n\nint main(){\n\n while( 1 ){\n n = in();\n if( n == 0 ){\n break;\n }\n \n REP( i , n ){\n p[i] = in();\n }\n\n REP( i , 128 ){\n REP( j , N ){\n dp[i][j] = pi( -INF, INF );\n }\n }\n \n dp[0][0] = pi( 0 , 0 );\n\n REP( i , n ){\n REP( k , N ){\n if( dp[i][k] == pi( -INF, INF ) ){\n continue;\n }\n\n // get\n int nec = 0;\n int get = 500 - ( p[i] % 1000 );\n if( p[i] % 1000 == 0 ){\n nec = 500;\n get = 0;\n } else if( p[i] % 1000 > 500 ){\n nec = p[i] % 1000 - 500;\n get = 0;\n }\n if( nec <= k ){\n int nk = k-nec+get;\n if( 0 <= nk && nk < N ){\n chmax( dp[i+1][nk], pi( dp[i][k].fi + 1, dp[i][k].se - p[i] ) );\n }\n }\n \n // not get\n get = 1000 - ( p[i] % 1000 );\n if( p[i] % 1000 == 0 ){\n get = 0;\n }\n if( k+get < N ){\n chmax( dp[i+1][k+get], pi( dp[i][k].fi, dp[i][k].se - p[i] ) );\n }\n chmax( dp[i+1][k], dp[i][k] );\n }\n }\n\n pi res = pi( -INF, INF );\n REP( j , N ){\n chmax( res , dp[n][j] );\n }\n\n printf( \"%d %d\\n\" , res.fi, -res.se );\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 410, "memory_kb": 53456, "score_of_the_acc": -0.3746, "final_rank": 14 }, { "submission_id": "aoj_1603_10683416", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n\ntemplate<typename T>\ninline void chmax(T& a, T b) { a = max(a, b); }\n\ninline void solve() {\n int n;\n cin >> n;\n if (n == 0) exit(0);\n vector<int> a(n);\n rep(i, n) cin >> a[i];\n constexpr int m = 50000;\n vector dp(n + 1, vector<pair<int, int>>(m, make_pair(-1e9, -1e9)));\n dp[0][0] = { 0, 0 };\n rep(i, n) {\n for (int j = 0; j < m; ++j) {\n if (dp[i][j].first < 0) continue;\n // つかわない\n chmax(dp[i + 1][j], dp[i][j]);\n // つかう\n if (0 < a[i] % 1000 && a[i] % 1000 <= 500) {\n int rem = 500 - a[i] % 1000;\n if (j + rem < m) chmax(dp[i + 1][j + rem], make_pair(dp[i][j].first + 1, dp[i][j].second - a[i]));\n }\n else {\n int use = ((a[i] % 1000 - 500) + 1000) % 1000;\n // ふつうにはらう\n {\n int rem = (1000 - a[i] % 1000) % 1000;\n if (j + rem < m) chmax(dp[i + 1][j + rem], make_pair(dp[i][j].first, dp[i][j].second - a[i]));\n }\n // おつりつかう\n if (j >= use) {\n if (j - use < m) chmax(dp[i + 1][j - use], make_pair(dp[i][j].first + 1, dp[i][j].second - a[i]));\n }\n }\n }\n }\n pair<int, int> ans = { -1e9, -1e9 };\n rep(j, m) chmax(ans, dp[n][j]);\n cout << ans.first << ' ' << -ans.second << '\\n';\n}\n\nint main() {\n cin.tie(nullptr)->sync_with_stdio(false);\n while (true) solve();\n // solve();\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 43392, "score_of_the_acc": -0.2987, "final_rank": 8 }, { "submission_id": "aoj_1603_10673328", "code_snippet": "#include <bits/stdc++.h>\n#define fi first\n#define se second\n#define rep(i,s,n) for (int i = (s); i < (n); ++i)\n#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n#define len(x) (int)(x).size()\n#define dup(x,y) (((x)+(y)-1)/(y))\n#define pb push_back\n#define eb emplace_back\n#define Field(T) vector<vector<T>>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\ntemplate<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;\nusing P = pair<int,int>;\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\n\nstatic constexpr int inf = 1000000000;\n\nbool solve() {\n int n;\n cin >> n;\n if (n == 0) return 1;\n vector<int> p(n);\n rep(i,0,n) cin >> p[i];\n vector<vector<P>> dp(n+1, vector<P>(50001, {0, inf}));\n dp[0][0].se = 0;\n rep(i,0,n) rep(j,0,50001) {\n if (dp[i][j].se == inf) continue;\n chmin(dp[i+1][j], dp[i][j]);\n int x = 1000*dup(p[i], 1000) - p[i];\n if (x >= 500) {\n P np = dp[i][j];\n np.fi -= 1, np.se += p[i];\n chmin(dp[i+1][j+x-500], np);\n } else {\n if (500-x > j) {\n P np = dp[i][j];\n np.se += p[i];\n chmin(dp[i+1][j+x], np);\n } else {\n P np = dp[i][j];\n np.fi -= 1, np.se += p[i];\n chmin(dp[i+1][j+x-500], np);\n }\n }\n }\n P ans = {0, inf};\n rep(j,0,50001) chmin(ans, dp[n][j]);\n cout << -ans.fi << \" \" << ans.se << endl;\n return 0;\n}\n\nint main() {\n while(!solve());\n return 0;\n}", "accuracy": 1, "time_ms": 490, "memory_kb": 42976, "score_of_the_acc": -0.3254, "final_rank": 13 }, { "submission_id": "aoj_1603_10645470", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\n#include <stdlib.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define ll int\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define all(x) (x).begin(), (x).end()\n//constexpr ll MOD = 1000000007;\nconstexpr ll MOD = 998244353;\nconstexpr int IINF = 1001001001;\n// constexpr ll INF = 1LL<<60;\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\nll gcd(ll a, ll b){\n if(a%b == 0){\n return b;\n }else{\n return gcd(b, a%b);\n }\n}\n\nll lcm(ll a, ll b){\n return a*b / gcd(a, b);\n}\n\nll powMod(ll x, ll n) {\n if (n == 0) return 1 % MOD;\n ll val = powMod(x, n / 2);\n val *= val;\n val %= MOD;\n if (n % 2 == 1) val *= x;\n return val % MOD;\n}\n\nint main() {\n while(true){\n ll n; cin >> n;\n if(n == 0) break;\n vector<ll> p(n), pm(n);\n rep(i,0,n) cin >> p[i], pm[i] = p[i]%1000;\n vector<pll> dp(50000, {0,-IINF});\n dp[0] = {0,0};\n rep(i,0,n){\n vector<pll> ndp(50000, {0,-IINF});\n rep(j,0,50000){\n if(dp[j] == make_pair(0,-IINF)) continue;\n auto [x, y] = dp[j];\n // cout << \"ok \" << i << \" \" << j << \" \" << x << \" \" << y << endl;\n chmax(ndp[j], dp[j]);\n if(pm[i] > 500){\n chmax(ndp[j+1000-pm[i]], make_pair(x, y-p[i]));\n if(j >= -500+pm[i]) chmax(ndp[j+500-pm[i]], make_pair(x+1, y-p[i]));\n }else if(pm[i] > 0){\n chmax(ndp[j+500-pm[i]], make_pair(x+1, y-p[i]));\n }else{\n if(j >= 500) chmax(ndp[j-500], make_pair(x+1, y-p[i]));\n }\n }\n swap(dp, ndp);\n pll ans = {0,0};\n rep(j,0,50000){\n chmax(ans, dp[j]);\n }\n // cout << i << \" \" << ans.first << \" \" << -ans.second << endl;\n }\n pll ans = {0, 0};\n rep(i,0,50000){\n chmax(ans, dp[i]);\n }\n cout << ans.first << \" \" << -ans.second << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 330, "memory_kb": 4564, "score_of_the_acc": -0.0502, "final_rank": 4 }, { "submission_id": "aoj_1603_10627917", "code_snippet": "//#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (ll i=0;i<(ll)n;i++)\n#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)\n#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)\n#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)\n#define vl vector<ll>\n#define vvl vector<vector<ll>>\n#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<\" \";}cout<<endl;\n#define vpdbg(a) rep(ii,a.size()){cout<<\"{\"<<a[ii].first<<\",\"<<a[ii].second<<\"} \";}cout<<endl;\n#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<\" \";}cout<<endl;}\n#define setdbg(a) for(const auto & ii:a){cout<<ii<<\" \";}cout<<endl;\n#define inf 1e9\n#define mod 998244353LL\n//#define mod 1000000007LL\n#define eps 0.000000001\nrandom_device rnd;// 非決定的な乱数生成器\nmt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード\n\n//#include<boost/multiprecision/cpp_int.hpp>\n//#define bbi boost::multiprecision::cpp_int\n//#include<atcoder/lazysegtree>\n\n\n//整数同士の累乗の計算をする。\nll power(ll A, ll B) {\n\tll result = 1;\n\tfor (ll i=0;i<B;i++){\n\t\tresult *= A;\n\t}\n\treturn result;\n}\n\n// nのk乗をmodで割った余りを計算\nll power_mod(ll n, ll k){\n\tlong long result = 1;\n\twhile (k > 0){\n\t\tif ((k&1) ==1)result=(result*n)%mod;\n\t\tn=n*n%mod;\n\t\tk >>= 1;\n\t}\n\treturn result;\n}\n\n\n//受け取った2次元文字の外側に、文字pをコーティングする。\nvector<string> pad(vector<string> &s,char p){\n\tll h=s.size();\n\tll w=s[0].size();\n\tvector<string> res(h+2,string(w+2,p));\n\trep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];\n\treturn res;\n}\n\n// Union-Find\nstruct UnionFind {\n\tvector<int> par, siz;\n\tUnionFind(int n) : par(n, -1) , siz(n, 1) { }\n\t// 根を求める\n\tint root(int x) {\n\t\tif (par[x] == -1) return x;\n\t\telse return par[x] = root(par[x]);\n\t}\n\t// x と y が同じグループに属するかどうか (根が一致するかどうか)\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\t// x を含むグループと y を含むグループとを併合する\n\tbool unite(int x, int y) {\n\t\tx = root(x), y = root(y);\n\t\tif (x == y) return false; \n\t\tif (siz[x] < siz[y]) swap(x, y);\n\t\tpar[y] = x;\n\t\tsiz[x] += siz[y];\n\t\treturn true;\n\t}\n\t// x を含むグループのサイズ\n\tint size(int x) {\n\t\treturn siz[root(x)];\n\t}\n};\n\n\n//グリッド問題等用\nvl dx={1,0,-1,0};\nvl dy={0,1,0,-1};\n\n\nll solve(){\n\tll n;\n\tcin>>n;\n\tif(n==0){return 1;}\n\n\t//dp[i][j] = i番目までの商品を買って、j円の小銭を所持している時の{個数,使用金額}\n\tvector<vector<pair<ll,ll>>> dp(n+1,vector<pair<ll,ll>>(n*500+1,{inf,inf}));\n\t\n\tdp[0][0]={0,0};\n\n\trep(i,n){\n\t\tll a;\n\t\tcin>>a;\n\n\t\t//そもそも買わない場合\n\t\tdp[i+1]=dp[i];\n\t\t\n\t\t//必ず買う場合\n\t\tif(1<=a%1000&&a%1000<=500){\n\t\t\tll tmp = 1000-500-(a%1000);\n\t\t\trep(j,i*500+1){\n\t\t\t\tdp[i+1][j+tmp] = min(make_pair(dp[i][j].first-1,dp[i][j].second+a),dp[i+1][j+tmp]);\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\t\n\t\t//買う場合\n\t\trep(j,i*500+1){\n\t\t\tll p=a%1000;\n\t\t\tif(p==0)p+=1000;\n\t\t\tll tmp = 1000-500-p;\n\t\t\tif(0<=tmp+j){\n\t\t\t\tpair<ll,ll> pr={dp[i][j].first-1,dp[i][j].second+a};\n\t\t\t\tdp[i+1][tmp+j]=min(pr,dp[i+1][tmp+j]);\n\t\t\t}else{\n\t\t\t\tpair<ll,ll> pr={dp[i][j].first,dp[i][j].second+a};\n\t\t\t\tdp[i+1][1000-p+j]=min(pr,dp[i+1][1000-p+j]);\n\t\t\t}\n\t\t}\n\t}\n\n\tpair<ll,ll> ans={inf,inf};\n\t\n\tloop(i,0,n*500){\n\t\tans=min(ans,dp[n][i]);\n\t}\n\n\tcout<<-ans.first<<\" \"<<ans.second<<endl;\n\treturn 0;\n}\n\n//メイン\nint main(){\n\twhile(solve()==0);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 900, "memory_kb": 83024, "score_of_the_acc": -0.6632, "final_rank": 17 }, { "submission_id": "aoj_1603_10572305", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1 << 30;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n \nvoid solve(int n) {\n vll x(n);cin >> x;\n\n // dp[i][j][k] := iまでみてj枚500円玉があり、k円小銭があるときの支払う総額の最小値\n // dp[i][k] := (500enndama no maisuu, tukattakingaku)\n ll lim = 500 * 100 + 10;\n vector<vpll>dp(n+1,vector<pll>(lim,{0,INF}));\n dp[0][0] = {0,0};\n auto cng = [&](pll &a, pll b){\n if(a.first < b.first){\n a = b;\n }else if(a.first == b.first && a.second > b.second)a = b;\n }; \n rep(i,n){\n // vector<unordered_map<ll,ll>>ndp(n+1);\n rrep(j,lim-1,0)if(dp[i][j] != pll{0,INF}){\n // if(j+n-i-1 <jmax)continue;\n auto [num,val] = dp[i][j];\n cng(dp[i+1][j],dp[i][j]);\n //500円ちょうどを作り出す\n ll rem = (x[i] + 500) % 1000;\n if(rem <= j){\n ll nk = j -rem;\n cng(dp[i+1][nk],pll{num+1,val + x[i]});\n }\n //1000の倍数で払う\n if(x[i] % 1000 != 0){\n ll payval = lceil(x[i],1000) * 1000;\n if(x[i] % 1000 <= 500){\n //500円もらえる\n ll nk = j + payval - x[i] -500;\n // if(500 * (n-i+1) < nk)continue;\n cng(dp[i+1][nk],pll{num+1,val+x[i]});\n }else{\n //500円もらえない\n ll nk = j + payval -x[i];\n // if(500 * (n-i+1) < nk)continue;\n cng(dp[i+1][nk],pll{num,val+x[i]});\n }\n }\n }\n }\n /*\n debug\n \n */\n // cout <<\"i :\" << i << endl; \n // repn(j,n){\n // cout <<\"j :\" << j << endl;\n // if(!dp[j].empty()){\n // for(auto &[k,cost]:dp[j]){\n // cout << k << \" \" << cost << endl;\n // }\n // }\n // }\n // cout << endl;\n ll num = 0;\n ll best = INF;\n rep(i,lim){\n if(chmax(num,dp[n][i].first)){\n best = dp[n][i].second;\n }else if(num == dp[n][i].first){\n chmin(best,dp[n][i].second);\n }\n }\n cout << num << \" \" << best << endl;\n \n\n}\n\n\nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while (true) {\n int n; cin >> n;\n if (n == 0) break;\n solve(n);\n }\n \n}", "accuracy": 1, "time_ms": 360, "memory_kb": 43392, "score_of_the_acc": -0.3008, "final_rank": 9 }, { "submission_id": "aoj_1603_10571657", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int M = 5000;\nint main(){\n while (true){\n int n;\n cin >> n;\n if (n == 0){\n return 0;\n }\n vector<vector<int>> dp(n + 1, vector<int>(M, 1e9));\n dp[0][0] = 0;\n for (int i = 0; i < n; i++){\n int p;\n cin >> p;\n for (int j = n - 1; j >= 0; j--){\n for (int k = M - 1; k >= 0; k--){\n int c = p % 1000;\n if (c <= 500){\n if (0 < c and k + (500 - c) < M){\n dp[j + 1][k + (500 - c)] = min(dp[j + 1][k + (500 - c)], dp[j][k] + p);\n }\n if (k >= c + 500){\n dp[j + 1][k - (c + 500)] = min(dp[j + 1][k - (c + 500)], dp[j][k] + p);\n }\n } else {\n if (k + (1000 - c) < M){\n dp[j][k + (1000 - c)] = min(dp[j][k + (1000 - c)], dp[j][k] + p);\n }\n if (k >= (c - 500)){\n dp[j + 1][k - (c - 500)] = min(dp[j + 1][k - (c - 500)], dp[j][k] + p);\n }\n }\n }\n }\n }\n for (int i = n; i >= 0; i--){\n int m = *min_element(dp[i].begin(), dp[i].end());\n if (m != 1e9){\n cout << i << \" \" << m << endl;\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 3050, "memory_kb": 5204, "score_of_the_acc": -0.6233, "final_rank": 16 }, { "submission_id": "aoj_1603_10541264", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\n\nusing namespace std;\nusing ll = long long;\nusing pll = pair<ll, ll>;\nusing bint = __int128_t;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define TT template <typename T>\nTT using vec = vector<T>;\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\ntemplate <class T> bool chmin(T &x, T y) {\n return x > y ? (x = y, true) : false;\n}\ntemplate <class T> bool chmax(T &x, T y) {\n return x < y ? (x = y, true) : false;\n}\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nstd::ostream &operator<<(std::ostream &dest, __int128_t value) {\n std::ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = std::end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = std::end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(std::ios_base::badbit);\n }\n }\n return dest;\n}\n\n__int128 parse(string &s) {\n __int128 ret = 0;\n for (size_t i = 0; i < s.length(); i++)\n if ('0' <= s[i] && s[i] <= '9')\n ret = 10 * ret + s[i] - '0';\n return ret;\n}\n\nstd::istream &operator>>(std::istream &dest, __int128_t &value) {\n string s;\n dest >> s;\n value = parse(s);\n return dest;\n}\n\nconst ll inf = LLONG_MAX / 4;\nint main() {\n ll n;\n while (1) {\n cin >> n;\n if (n == 0)\n return 0;\n vector<ll> P(n);\n rep(i, 0, n) cin >> P[i];\n\n const ll M = 550 * n;\n\n auto change = [&](pll &a, pll b) {\n if (a.first < b.first) {\n a = b;\n return true;\n } else if (a.first == b.first) {\n return chmin(a.second, b.second);\n }\n return false;\n };\n\n auto oturi = [&](ll x) {\n x %= 1000;\n if (x == 0)\n return 0LL;\n return 1000LL - x;\n };\n\n vector<pll> dp(M, {-inf, inf});\n dp[0] = {0, 0};\n\n rep(i, 0, n) {\n auto pre = dp;\n\n // 買う場合\n rep(one, 0, M) if (pre[one] != pll{-inf, inf}) {\n // 1000円pay\n if (oturi(P[i]) + one < M)\n change(dp[oturi(P[i]) + one],\n {pre[one].first, pre[one].second + P[i]});\n // 500円玉get\n if (P[i] % 1000 == 0) {\n if (one >= 500) {\n change(dp[one - 500],\n {pre[one].first + 1, pre[one].second + P[i]});\n }\n } else {\n if (P[i] % 1000 <= 500) {\n if (oturi(P[i]) + one - 500 < M)\n change(\n dp[oturi(P[i]) + one - 500],\n {pre[one].first + 1, pre[one].second + P[i]});\n } else {\n ll use = P[i] % 1000 - 500;\n if (one >= use)\n change(dp[one - use], {pre[one].first + 1,\n pre[one].second + P[i]});\n }\n }\n }\n }\n\n pll ans(-inf, inf);\n rep(one, 0, M) {\n change(ans, dp[one]);\n }\n cout << ans.first << \" \" << ans.second << endl;\n }\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 5120, "score_of_the_acc": -0.0307, "final_rank": 3 }, { "submission_id": "aoj_1603_10474876", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr int MAX_MONEY = 50000;\nconstexpr int INF = 1e9;\n\nint main() {\n while (true) {\n int N;\n cin >> N;\n if (N == 0) {\n break;\n }\n\n vector<int> prices(N);\n for (int i = 0; i < N; ++i) {\n cin >> prices[i];\n }\n\n vector<pair<int, int>> dp(MAX_MONEY + 1, {-1, INF});\n dp[0] = {0, 0};\n\n for (int idx = 0; idx < N; ++idx) {\n int price = prices[idx];\n vector<pair<int, int>> next_dp = dp;\n\n for (int m = 0; m <= MAX_MONEY; ++m) {\n if (dp[m].first == -1) {\n continue;\n }\n\n auto [c, s] = dp[m];\n if (next_dp[m].first < c || (next_dp[m].first == c && next_dp[m].second > s)) {\n next_dp[m] = {c, s};\n }\n\n int r = price % 1000;\n int need = min(r, m);\n int pay = price - need;\n int change = (1000 - (pay % 1000)) % 1000;\n int new_m = m - need + change;\n\n if (change < 500 && new_m <= MAX_MONEY) {\n if (next_dp[new_m].first < c || (next_dp[new_m].first == c && next_dp[new_m].second > s + price)) {\n next_dp[new_m] = {c, s + price};\n }\n }\n\n if (r == 0 && m >= 500) {\n int nm = m - 500;\n if (nm >= 0 && nm <= MAX_MONEY) {\n if (next_dp[nm].first < c + 1 || (next_dp[nm].first == c + 1 && next_dp[nm].second > s + price)) {\n next_dp[nm] = {c + 1, s + price};\n }\n }\n } else if (r > 0 && r <= 500) {\n int nm = m + 500 - r;\n if (nm <= MAX_MONEY) {\n if (next_dp[nm].first < c + 1 || (next_dp[nm].first == c + 1 && next_dp[nm].second > s + price)) {\n next_dp[nm] = {c + 1, s + price};\n }\n }\n } else if (r > 500 && m >= r - 500) {\n int nm = m - (r - 500);\n if (nm >= 0 && nm <= MAX_MONEY) {\n if (next_dp[nm].first < c + 1 || (next_dp[nm].first == c + 1 && next_dp[nm].second > s + price)) {\n next_dp[nm] = {c + 1, s + price};\n }\n }\n }\n }\n dp.swap(next_dp);\n }\n\n int max_c = -1, min_s = INF;\n for (int m = 0; m <= MAX_MONEY; ++m) {\n if (dp[m].first > max_c || (dp[m].first == max_c && dp[m].second < min_s)) {\n max_c = dp[m].first;\n min_s = dp[m].second;\n }\n }\n cout << max_c << \" \" << min_s << endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 4568, "score_of_the_acc": -0.0042, "final_rank": 1 }, { "submission_id": "aoj_1603_10396289", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<n;i++)\n#define REP2(i, n) for (ll i=0;i<n;i++)\n#define REP3(i, a, n) for (ll i=a;i<n;i++)\n#define REP4(i, a, b, n) for(ll i=a;i<n;i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=n;i>=0;i--)\n#define RREP2(i, n) for(ll i=n;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=n;i>=a;i--)\n#define RREP4(i, a, b, n) for(ll i=n;i>=a;i-=b)\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define foa(a,v) (auto& a : (v))\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\n#define pb push_back\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(pll x){cout << x.first << ' ' << x.second << '\\n';}\nvoid solve(ll n,vll p){\n vll q;\n rep(i,n){\n q.push_back(p[i]);\n q[i] %= 1000;\n }\n vector<vector<pll>> dp(n+1,vpll(100000+10,{1LL<<60,1LL<<60}));\n dp[0][0] = {0,0};\n ll m = 100000+10;\n rep(i,n){\n rep(j,m){\n dp[i+1][j] = min(dp[i+1][j],dp[i][j]);\n if(1 <= q[i] && q[i] <= 500){\n dp[i+1][j + (1000 - q[i] - 500)] = min(dp[i+1][j + (1000 - q[i] - 500)],{dp[i][j].first - 1,dp[i][j].second + p[i]});\n }\n if(1 <= q[i] && j + 1000 - q[i] < m){\n dp[i+1][j + 1000 - q[i]] = min(dp[i+1][j + 1000 - q[i]],{dp[i][j].first,dp[i][j].second + p[i]});\n }\n if(0 <= j - (q[i] + 500) && q[i] + 500 < 1000){\n dp[i+1][j - (q[i] + 500)] = min(dp[i+1][j - (q[i] + 500)],{dp[i][j].first-1,dp[i][j].second + p[i]});\n }\n if(0 <= j - (q[i] + 500 - 1000) && q[i] + 500 >= 1000){\n dp[i+1][j - (q[i] + 500 - 1000)] = min(dp[i+1][j - (q[i] + 500 - 1000)],{dp[i][j].first - 1,dp[i][j].second + p[i]});\n }\n }\n }\n //print(dp[3][200]);\n pll ans = {0,1LL<<60};\n rep(i,m){\n ans = min(ans,dp[n][i]);\n }\n print(-ans.first,ans.second);\n}\n\nint main(){\n while(1){\n LL(n);\n if(n == 0){break;}\n VLL(p,n);\n solve(n,p);\n\n }\n}", "accuracy": 1, "time_ms": 1850, "memory_kb": 162816, "score_of_the_acc": -1.364, "final_rank": 19 }, { "submission_id": "aoj_1603_9431279", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nint MAXS = 50001;\nstatic pair<int,int> DP[101][50001] = {};\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n vector<int> C(N);\n rep(i,0,N) cin >> C[i];\n rep(i,0,101) {\n rep(j,0,50001) DP[i][j] = {-inf,-inf};\n }\n DP[0][0] = {0,0};\n rep(i,0,N) {\n rep(j,0,50001) {\n if (DP[i][j].first == -inf) continue;\n chmax(DP[i+1][j], DP[i][j]);\n if (1 <= (C[i] % 1000) && (C[i] % 1000) <= 500) {\n chmax(DP[i+1][j+((100000-C[i])%500)], {DP[i][j].first + 1, DP[i][j].second - C[i]});\n }\n else {\n int X;\n if (C[i] % 1000 == 0) X = 500;\n else X = C[i] % 500;\n if (j >= X) {\n chmax(DP[i+1][j-X], {DP[i][j].first + 1, DP[i][j].second - C[i]});\n }\n chmax(DP[i+1][j+((100000-C[i])%500)], {DP[i][j].first, DP[i][j].second - C[i]});\n }\n }\n }\n pair<int,int> ANS = {-inf,-inf};\n rep(i,0,50001) chmax(ANS,DP[N][i]);\n cout << ANS.first << ' ' << -ANS.second << endl;\n}\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 42836, "score_of_the_acc": -0.2869, "final_rank": 7 }, { "submission_id": "aoj_1603_9417786", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define rep(i,n) for(int i = 0; i < n; i++)\n#define MMAX 1000000000000000000LL\n\n#define int ll\n\n#define NO make_pair(-1, -1)\n\nvoid main_()\n{\n const int MAX = 1000 * 100;\n int n;\n cin >> n;\n if(!n) exit(0);\n\n vector<int> a(n);\n rep(i, n) cin >> a[i];\n// vector<vector<pair<int,int>>> dp(MAX, vector<pair<int,int>>(n + 1, NO));\n vector<pair<int,int>> dp(MAX, NO);\n\n dp[0] = make_pair(0, 0);\n\n for(int i = 0; i < n; i++) {\n vector<pair<int,int>> nxt = dp;\n for(int j = 0; j < MAX; j++) {\n if(dp.at(j).first == -1) continue;\n nxt.at(j) = max(nxt.at(j), dp.at(j));\n int change = j + (1000 - a[i] % 1000) % 1000;\n auto p = dp[j];\n nxt.at(change) = max(nxt.at(change), make_pair(p.first, p.second - a[i]));\n if(change < 500) continue;\n change -= 500;\n nxt.at(change) = max(nxt.at(change), make_pair(p.first + 1, p.second - a[i]));\n }\n dp = nxt;\n }\n\n pair<int,int> p = make_pair(0, 0);\n for(int i = 0; i < MAX; i++) {\n p = max(p, dp[i]);\n }\n cout << p.first << \" \" << -p.second << endl;\n}\n\nsigned main()\n{\n while(1) main_();\n}", "accuracy": 1, "time_ms": 640, "memory_kb": 6200, "score_of_the_acc": -0.1254, "final_rank": 6 }, { "submission_id": "aoj_1603_9408635", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long double ld;\ntypedef pair<int, int> pint;\nconst int MOD = 1000000007;\n#define rep(i, N) for(int i = 0; i < (int)N; i++)\n#define ALL(V) (V).begin(), (V).end()\nint dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};\nint dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};\n\nconst int INF = 1e8;\n\ntemplate<class T> inline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T> inline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\nint dp[101][50001] = {INF};\nint ndp[101][50001] = {INF};\n\nint main() {\n while (1) {\n int N; cin >> N;\n if (N == 0) break;\n vector<int> P(N);\n rep (i, N) cin >> P[i];\n rep (i, 101) rep (j, 50001) dp[i][j] = INF, ndp[i][j] = INF;\n\n dp[0][0] = 0;\n int oturi = 0;\n rep (i, N) {\n if (i % 2 == 0) { // ndpを更新\n rep (k, N) {\n rep (j, oturi + 1) ndp[k][j] = INF;\n }\n rep (k, N) {\n rep (j, oturi + 1) {\n // not buy\n chmin(ndp[k][j], dp[k][j]);\n int np = P[i] % 1000;\n // buy\n if (np == 0) {\n // 小銭の500円があるならそれを一緒に払って500円玉を得る\n if (j - 500 >= 0) chmin(ndp[k + 1][j - 500], dp[k][j] + P[i]);\n } else if (np <= 500) {\n // 1000円払って500円玉を得る\n if (j + (1000 - np) % 500 <= 50000) chmin(ndp[k + 1][j + (1000 - np) % 500], dp[k][j] + P[i]);\n } else if (np > 500) {\n // 1000円払って小銭をもらう\n if (j + (1000 - np) <= 50000) chmin(ndp[k][j + (1000 - np)], dp[k][j] + P[i]);\n // 小銭を払って500円玉を得る\n if (j - np % 500 >= 0) chmin(ndp[k + 1][j - np % 500], dp[k][j] + P[i]);\n }\n }\n }\n } else { // dpを更新\n rep (k, N) {\n rep (j, oturi + 1) dp[k][j] = INF;\n }\n rep (k, N) {\n rep (j, oturi + 1) {\n // not buy\n chmin(dp[k][j], ndp[k][j]);\n int np = P[i] % 1000;\n // buy\n if (np == 0) {\n // 小銭の500円があるならそれを一緒に払って500円玉を得る\n if (j - 500 >= 0) chmin(dp[k + 1][j - 500], ndp[k][j] + P[i]);\n } else if (np <= 500) {\n // 1000円払って500円玉を得る\n if (j + (1000 - np) % 500 <= 50000) chmin(dp[k + 1][j + (1000 - np) % 500], ndp[k][j] + P[i]);\n } else if (np > 500) {\n // 1000円払って小銭をもらう\n if (j + (1000 - np) <= 50000) chmin(dp[k][j + (1000 - np)], ndp[k][j] + P[i]);\n // 小銭を払って500円玉を得る\n if (j - np % 500 >= 0) chmin(dp[k + 1][j - np % 500], ndp[k][j] + P[i]);\n }\n }\n }\n }\n oturi += (1000 - P[i] % 1000) % 500;\n }\n \n int ans_idx = 0;\n int ans_value = 0;\n if (N % 2 == 0) {\n rep (i, N + 1) {\n rep (j, 50001) {\n if (dp[i][j] == INF) continue;\n if (ans_idx < i) {\n ans_idx = i;\n ans_value = dp[i][j];\n // cout << ans_idx << ' ' << ans_value << ' ' << j << endl;\n } else if (ans_idx == i) {\n chmin(ans_value, dp[i][j]);\n // cout << ans_idx << ' ' << ans_value << ' ' << j << endl;\n }\n }\n }\n } else {\n rep (i, N + 1) {\n rep (j, 50001) {\n if (ndp[i][j] == INF) continue;\n if (ans_idx < i) {\n ans_idx = i;\n ans_value = ndp[i][j];\n // cout << ans_idx << ' ' << ans_value << ' ' << j << endl;\n } else if (ans_idx == i) {\n chmin(ans_value, ndp[i][j]);\n // cout << ans_idx << ' ' << ans_value << ' ' << j << endl;\n }\n }\n }\n }\n \n cout << ans_idx << ' ' << ans_value << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4890, "memory_kb": 42864, "score_of_the_acc": -1.2452, "final_rank": 18 }, { "submission_id": "aoj_1603_9408567", "code_snippet": "#include <bits/stdc++.h>\n\n//使用したライブラリ\n//https://github.com/atcoder/ac-library/tree/fe9b6fca9ab4e1be946ea23a4e6a2a751cf4aaa2\n//https://github.com/ei1333/library/tree/7da7fca8cebc1d17a048c9483f07e39c8e465cdf\n//https://github.com/zawa-tin/cp-documentation/tree/3842eb973aace809be0bcf1649e2fe85e99098e3\n//https://github.com/luckylat/library/tree/8e2a2a6a58bef4644c9442998a1c9c1890672cd3\n\nconst int M{500 * 100 + 1};\n\nint Ceil(int A, int B) {\n assert(B != 0);\n return (A + B - 1) / B;\n}\n\nstd::pair<int, int> operator+(std::pair<int, int> A, std::pair<int, int> B) {\n return {A.first + B.first, A.second + B.second};\n}\n\nstd::pair<int, int> chopt(std::pair<int, int> A, std::pair<int, int> B) {\n if (A.first < B.first) A = B;\n else if (A.first == B.first and A.second > B.second) A = B;\n return A;\n}\n\nbool solve() {\n int N;\n std::cin >> N;\n if (N == 0) return false;\n const std::pair<int, int> INVALID{ -1, -1 };\n std::vector<std::pair<int, int>> dp(M, INVALID);\n dp[0] = { 0, 0 };\n while (N--) {\n int P;\n std::cin >> P;\n std::vector<std::pair<int, int>> next{dp};\n for (int i{} ; i < M ; i++) {\n if (dp[i] == INVALID) continue;\n int turi{1000 * Ceil(P, 1000) - P};\n assert(0 <= turi and turi <= 1000);\n if (turi >= 500) {\n turi -= 500;\n next[i + turi] = chopt(next[i + turi], dp[i] + std::make_pair(1, P));\n }\n else {\n next[i + turi] = chopt(next[i + turi], dp[i] + std::make_pair(0, P));\n int need{500 - turi};\n if (need <= i) {\n next[i - need] = chopt(next[i - need], dp[i] + std::make_pair(1, P));\n }\n }\n }\n dp = std::move(next);\n }\n int max{-1}, min{};\n for (int i{} ; i < M ; i++) {\n if (max < dp[i].first) {\n std::tie(max, min) = dp[i];\n }\n else if (max == dp[i].first and min > dp[i].second) {\n std::tie(max, min) = dp[i];\n }\n }\n std::cout << max << ' ' << min << '\\n';\n return true;\n}\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3896, "score_of_the_acc": -0.0209, "final_rank": 2 }, { "submission_id": "aoj_1603_9377882", "code_snippet": "#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include <iomanip>\n#include <climits>\n#include <cmath>\n#include <functional>\n#include <numeric>\n#include <regex>\n#include <array>\n#include <fstream>\n#include <sstream>\n\n//#include<atcoder/modint>\n//using namespace atcoder;\n\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)\n#define repl(i,l,r) for (int i = l; i < (int)(r); i++)\n#define all(a) a.begin(),a.end()\n#define Pii pair<int,int>\n#define Pll pair<long,long>\n#define INFi 1000000001\n#define INFl 1000000000000000001\n#define ll long long\nusing namespace std;\n\n\n\ntemplate<class T> inline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class T> inline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate<class T> void printArray(vector<T>&A){\n for(T&a:A){\n cout<<a<<\" \";\n }\n cout<<endl;\n}\ntemplate<class T> void printArrayln(vector<T>&A){\n for(T&a:A){\n cout<<a<<endl;\n }\n}\n\n\ntemplate<class T1,class T2> std::ostream &operator<<(std::ostream &out, const pair<T1,T2> &A){\n cout<<\"{\"<<A.first<<\",\"<<A.second<<\"}\";\n return out;\n}\n\ntemplate<class T1,class T2> std::ostream &operator<<(std::ostream &out, const map<T1,T2> &M){\n for(const auto&A:M){\n cout<<\"{\"<<A.first<<\",\"<<A.second<<\"}\";\n }\n return out;\n}\n\ntemplate<class T1> std::ostream &operator<<(std::ostream &out, const set<T1> &M){\n cout<<\"{\";\n for(const auto&A:M){\n cout<<A<<\", \";\n }\n cout<<\"}\"<<endl;\n return out;\n}\n\n\ntemplate<class T1> std::ostream &operator<<(std::ostream &out, const multiset<T1> &M){\n cout<<\"{\";\n for(const auto&A:M){\n cout<<A<<\", \";\n }\n cout<<\"}\"<<endl;\n return out;\n}\n\ntemplate<class T> std::ostream &operator<<(std::ostream &out, const vector<T> &A){\n for(const T &a:A){\n cout<<a<<\" \";\n }\n return out;\n}\n\nvoid print() { cout << endl; }\n \ntemplate <typename Head, typename... Tail>\nvoid print(Head H, Tail... T) {\n cout << H << \" \";\n print(T...);\n}\n\n\ntemplate<class T> std::istream &operator>>(std::istream &in,vector<T>&A){\n for(T&a:A){\n std::cin>>a;\n }\n return in;\n}\n\nstruct dpn{\n int coins;\n int cost;\n // 比較関数\n bool operator<(const dpn &right) const {\n if(coins!=right.coins)return coins<right.coins;\n return cost>right.cost;\n }\n};\n\n\nvoid solve(int N){\n vector<int> P(N);\n rep(i,N)cin>>P[i];\n const int M = 499*100;\n vector<vector<dpn>> dp(N+1,vector<dpn>(M+1,{-10000,0}));\n dp[0][0] = {0,0};\n\n for(int i=0;i<N;i++){\n for(int j=0;j<M;j++){\n chmax(dp[i+1][j],dp[i][j]);\n // iを買う 1000円だけで\n int otsuri = (1000-P[i]%1000)%1000;\n bool coin = false;\n if(otsuri >= 500){\n coin = true;\n otsuri -= 500;\n }\n if(j+otsuri<=M){\n chmax(dp[i+1][j+otsuri],{dp[i][j].coins+coin,dp[i][j].cost+P[i]});\n }\n // 買う 小銭で\n int border = (P[i]+500)%1000;\n if(j>=border){\n chmax(dp[i+1][j-border],{dp[i][j].coins+1,dp[i][j].cost+P[i]});\n }\n }\n }\n // dpテーブルを表示\n for(int i=0;i<=N;i++){\n for(int j=0;j<=600;j++){\n if(dp[i][j].coins<0)continue;\n //cout << i << \" \" << j << \" \" << dp[i][j].coins << \" \" << dp[i][j].cost ;\n //cout << endl;\n\n }\n }\n\n auto ans = dp[N][0];\n for(int i=0;i<=M;i++){\n chmax(ans,dp[N][i]);\n }\n cout<<ans.coins<<\" \"<<ans.cost<<endl;\n\n\n}\n\nint main(void){\n std::cin.tie(0)->sync_with_stdio(0);\n while(1){\n int n;cin>>n;\n if(n==0)break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 930, "memory_kb": 42936, "score_of_the_acc": -0.4172, "final_rank": 15 }, { "submission_id": "aoj_1603_9374569", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n\nusing ll = long long;\nusing i64 = long long;\nusing pll = pair<ll,ll>;\nusing plll = pair<pll,ll>;\nusing pii =pair<int,int>;\n\nconstexpr ll mod = 1000000007;\n\nint dx[4]={1,0,-1,0};\nint dy[4]={0,1,0,-1};\nint dX[4]={1,0,-1,0};\nint dY[4]={0,-1,0,1};\n\n\n\n//library\n\n//end\n\n\n\n\nusing Graph =vector<vector<int>>;\n\nint main(){\n while (true){\n int n;\n cin>>n;\n if (n==0)break;\n int m=50010;\n vector<vector<pii>> dp(n+1,vector<pii>(m,pair{-1,0}));\n dp[0][0].first=0;\n for (int i=0; i<n; i++){\n int p;\n cin>>p;\n for (int j=0; j<m; j++){\n if (dp[i][j].first==-1)continue;\n //cout<<' '<<i<<' '<<j<<' '<<dp[i][j].first<<endl;\n\n //p+500円払う\n if (j>=(p+500)%1000){\n if (dp[i+1][j-(p+500)%1000].first<dp[i][j].first+1){\n dp[i+1][j-(p+500)%1000].first=dp[i][j].first+1;\n dp[i+1][j-(p+500)%1000].second=dp[i][j].second+p;\n\n }\n else if (dp[i+1][j-(p+500)%1000].second>dp[i][j].second+p && dp[i+1][j-(p+500)%1000].first==dp[i][j].first+1){\n dp[i+1][j-(p+500)%1000].first=dp[i][j].first+1;\n dp[i+1][j-(p+500)%1000].second=dp[i][j].second+p;\n }\n }\n\n //1000円札だけで払う\n int s=(p+999)/1000;\n if (1000*s-p>=500){\n if (dp[i+1][j+1000*s-p-500].first<dp[i][j].first+1){\n dp[i+1][j+1000*s-p-500].first=dp[i][j].first+1;\n dp[i+1][j+1000*s-p-500].second=dp[i][j].second+p;\n\n }\n else if (dp[i+1][j+1000*s-p-500].second>dp[i][j].second+p && dp[i+1][j+1000*s-p-500].first==dp[i][j].first+1){\n dp[i+1][j+1000*s-p-500].first=dp[i][j].first+1;\n dp[i+1][j+1000*s-p-500].second=dp[i][j].second+p;\n }\n }\n else{\n if (dp[i+1][j+1000*s-p].first<dp[i][j].first){\n //cout<<'!'<<endl;\n dp[i+1][j+1000*s-p].first=dp[i][j].first;\n dp[i+1][j+1000*s-p].second=dp[i][j].second+p;\n //cout<<dp[i+1][j+1000*s-p].first<<' '<<dp[i+1][j+1000*s-p].second<<endl;\n\n }\n else if (dp[i+1][j+1000*s-p].second>dp[i][j].second+p && dp[i+1][j+1000*s-p].first==dp[i][j].first){\n\n dp[i+1][j+1000*s-p].first=dp[i][j].first;\n dp[i+1][j+1000*s-p].second=dp[i][j].second+p;\n }\n }\n\n //i番目の品物を買わない\n if (dp[i+1][j].first<dp[i][j].first){\n dp[i+1][j].first=dp[i][j].first;\n dp[i+1][j].second=dp[i][j].second;\n\n }\n else if (dp[i+1][j].first==dp[i][j].first && dp[i][j].second<dp[i+1][j].second){\n dp[i+1][j].first=dp[i][j].first;\n dp[i+1][j].second=dp[i][j].second;\n }\n\n }\n\n }\n int a1=0,a2=0;\n for (int j=0; j<m; j++){\n auto[a,b]=dp[n][j];\n //cout<<a<<b<<endl;\n if (a>a1){\n a1=a;\n a2=b;\n }\n if (a==a1 && a2>b){\n a1=a;\n a2=b;\n }\n }\n\n cout<<a1<<' '<<a2<<endl;\n\n\n }\n\n\n\n\n}", "accuracy": 1, "time_ms": 480, "memory_kb": 43080, "score_of_the_acc": -0.324, "final_rank": 12 }, { "submission_id": "aoj_1603_9340739", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\n\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n;cin >> n;\n if(!n)break;\n vi p(n);cin >> p;\n int MX = 1000*n+10;\n vector<P> dp(MX,{-1,1ll << 50});\n dp[0] = {0,0};\n auto f = [](P &a,P b)->void{\n if(a.first > b.first)return;\n if(a.first == b.first && a.second <= b.second)return;\n swap(a,b);\n };\n rep(i,n){\n vector<P> DP(MX,{-1,1ll << 50});\n rep(j,MX){\n if(dp[j].first == -1)continue;\n int g = p[i]%1000;\n if(0 < g && g <= 500){\n f(DP[j+500-g],{dp[j].first+1,dp[j].second+p[i]});\n f(DP[j],dp[j]);\n }else{\n int k = g%500;\n if(k == 0)k += 500;\n if(k <= j)f(DP[j-k],{dp[j].first+1,dp[j].second+p[i]});\n f(DP[j],dp[j]);\n if(g != 0)f(DP[j+1000-g],{dp[j].first,dp[j].second+p[i]});\n }\n }\n swap(dp,DP);\n }\n P res = {-1,1ll << 50};\n rep(i,MX)f(res,dp[i]);\n cout << res << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 390, "memory_kb": 6220, "score_of_the_acc": -0.0732, "final_rank": 5 }, { "submission_id": "aoj_1603_9327501", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <limits.h>\nusing namespace std;\n\nconst long long inf = 1000000000000000000LL;\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n == 0) break;\n\n vector<int> P(n);\n for (int i = 0; i < n; ++i) {\n cin >> P[i];\n }\n\n int k_max = 5 * n + 1;\n int j_max = n + 1;\n vector<vector<vector<long long>>> dp(n + 1, vector<vector<long long>>(j_max, vector<long long>(k_max, -inf)));\n \n // dp[i個目の店][j枚500円玉もらう][k枚1000円札払う] = 小銭合計\n dp[0][0][0] = 0;\n \n for (int i = 0; i < n; ++i) {\n int d = P[i] / 1000;\n int m = P[i] % 1000;\n for (int j = 0; j < j_max; ++j) {\n for (int k = 0; k < k_max; ++k) {\n if (dp[i][j][k] == -inf) continue;\n\n if (m == 0) {\n dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k]);\n if (dp[i][j][k] >= 500 && j + 1 < j_max && k + d < k_max) {\n dp[i+1][j+1][k+d] = max(dp[i+1][j+1][k+d], dp[i][j][k] - 500);\n }\n } else if (m <= 500) {\n if (j + 1 < j_max && k + d + 1 < k_max) {\n dp[i+1][j+1][k+d+1] = max(dp[i+1][j+1][k+d+1], dp[i][j][k] + 500 - m);\n }\n } else {\n dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k]);\n if (k + d + 1 < k_max) {\n dp[i+1][j][k+d+1] = max(dp[i+1][j][k+d+1], dp[i][j][k] + 1000 - m);\n }\n if (dp[i][j][k] >= m - 500 && j + 1 < j_max && k + d + 1 < k_max) {\n dp[i+1][j+1][k+d+1] = max(dp[i+1][j+1][k+d+1], dp[i][j][k] - (m - 500));\n }\n }\n }\n }\n }\n\n int c = 0;\n for (int i = 0; i < j_max; ++i) {\n for (int k = 0; k < k_max; ++k) {\n if (dp[n][i][k] >= 0) {\n c = max(c, i);\n }\n }\n }\n\n int mn = INT_MAX;\n for (int i = 0; i < k_max; ++i) {\n if (dp[n][c][i] >= 0) {\n mn = min(mn, i);\n }\n }\n\n long long s = 1000LL * mn - dp[n][c][mn] - c * 500LL;\n cout << c << \" \" << s << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 400, "memory_kb": 43708, "score_of_the_acc": -0.3112, "final_rank": 11 }, { "submission_id": "aoj_1603_9317387", "code_snippet": "#include<iostream>\n#include<algorithm>\nusing namespace std;\nvoid chmin(int &a, int b){if(a>b)a=b;}\nint N,P,Q;\nconst int INF=1<<25;\nconst int M=4000;\nint dp[101][M+500],ep[101][M+500];\nbool solve()\n{\n cin>>N;\n if(N==0)return 0;\n for(int i=0;i<=N;i++)for(int j=0;j<M;j++)dp[i][j]=INF;\n dp[0][0]=0;\n for(int i=0;i<N;i++)\n {\n for(int j=0;j<=N;j++)for(int k=0;k<M;k++)ep[j][k]=INF;\n cin>>P;\n Q=P%1000;\n for(int j=0;j<=N;j++)for(int k=0;k<M;k++)\n {\n chmin(ep[j][k],dp[j][k]);\n if(Q==0)\n {\n if(k>=500)chmin(ep[j+1][k-500],dp[j][k]+P);\n }\n else if(Q==500)\n {\n chmin(ep[j+1][k],dp[j][k]+P);\n }\n else if(Q<500)\n {\n chmin(ep[j+1][k+500-Q],dp[j][k]+P);\n if(k-500>=Q)chmin(ep[j+1][k-500-Q],dp[j][k]+P);\n }\n else\n {\n chmin(ep[j][k+1000-Q],dp[j][k]+P);\n if(k+500>=Q)chmin(ep[j+1][k+500-Q],dp[j][k]+P);\n if(k-500>=Q)chmin(ep[j+1][k-500-Q],dp[j][k]+P);\n }\n }\n swap(dp,ep);\n }\n for(int j=N;j>=0;j--)\n {\n int ans=*min_element(dp[j],dp[j]+M);\n if(ans<INF)\n {\n cout<<j<<' '<<ans<<'\\n';\n return 1;\n }\n }\n return 1;\n}\nint main(){while(solve()){}}", "accuracy": 0.5, "time_ms": 3080, "memory_kb": 6984, "score_of_the_acc": -0.6408, "final_rank": 20 } ]
aoj_1609_cpp
Look for the Winner! The citizens of TKB City are famous for their deep love in elections and vote counting. Today they hold an election for the next chairperson of the electoral commission. Now the voting has just been closed and the counting is going to start. The TKB citizens have strong desire to know the winner as early as possible during vote counting. The election candidate receiving the most votes shall be the next chairperson. Suppose for instance that we have three candidates A , B , and C and ten votes. Suppose also that we have already counted six of the ten votes and the vote counts of A , B , and C are four, one, and one, respectively. At this moment, every candidate has a chance to receive four more votes and so everyone can still be the winner. However, if the next vote counted is cast for A , A is ensured to be the winner since A already has five votes and B or C can have at most four votes at the end. In this example, therefore, the TKB citizens can know the winner just when the seventh vote is counted. Your mission is to write a program that receives every vote counted, one by one, identifies the winner, and determines when the winner gets ensured. Input The input consists of at most 1500 datasets, each consisting of two lines in the following format. n c 1 c 2 … c n n in the first line represents the number of votes, and is a positive integer no greater than 100. The second line represents the n votes, separated by a space. Each c i (1 ≤ i ≤ n ) is a single uppercase letter, i.e. one of 'A' through 'Z'. This represents the election candidate for which the i -th vote was cast. Counting shall be done in the given order from c 1 to c n . You should assume that at least two stand as candidates even when all the votes are cast for one candidate. The end of the input is indicated by a line containing a zero. Output For each dataset, unless the election ends in a tie, output a single line containing an uppercase letter c and an integer d separated by a space: c should represent the election winner and d should represent after counting how many votes the winner is identified. Otherwise, that is, if the election ends in a tie, output a single line containing `TIE'. Sample Input 1 A 4 A A B B 5 L M N L N 6 K K K K K K 6 X X X Y Z X 10 A A A B A C A C C B 10 U U U U U V V W W W 0 Output for the Sample Input A 1 TIE TIE K 4 X 5 A 7 U 8
[ { "submission_id": "aoj_1609_10810381", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint N;\nchar S[100009];\n\nvoid Testcase() {\n pair<int, char> votes[26], votes2[256];\n for (int i = 0; i < 26; i++) votes[i] = make_pair(0, 'A' + i);\n\n // Simulation\n for (int i = 0; i < N; i++) {\n votes[S[i] - 'A'].first += 1;\n\n // Sorting\n for (int j = 0; j < 26; j++) votes2[j] = votes[j];\n sort(votes2, votes2 + 26);\n reverse(votes2, votes2 + 26);\n\n // Deciding\n if (votes2[0].first > votes2[1].first + (N - i - 1)) {\n cout << votes2[0].second << \" \" << i + 1 << endl;\n return;\n }\n }\n\n // If Draw\n cout << \"TIE\" << endl;\n}\n\nint main() {\n while (true) {\n cin >> N; if (N == 0) break;\n for (int i = 0; i < N; i++) cin >> S[i];\n Testcase();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3380, "score_of_the_acc": -0.6385, "final_rank": 11 }, { "submission_id": "aoj_1609_9494815", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\n... 2nd output should repmax_votesent after counting how many votes the winner is identified ...\n\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 1000000000;\n\nconst int MAX_N = 100;\nstatic int vect[MAX_N];\n\nconst int LETTERS = 26;\nstatic int counts[LETTERS];\n\n//------------------------------------------------------------------------------\nvoid reset()\n{\n for (int x=0; x<LETTERS; ++x) counts[x] = 0;\n}\n\nint max_vote(int N, int banned)\n{\n int res = 0;\n for (int i=0; i<N; ++i)\n for (int x=0; x<LETTERS; ++x)\n if (x != banned && counts[x] > res)\n res = counts[x];\n return res;\n}\nvoid query(int N)\n{\n int num = 0;\n int y1 = -1;\n int max_votes = 0;\n for (int x=0; x<LETTERS; ++x)\n {\n if (counts[x] > max_votes) { y1 = x; max_votes = counts[x]; num = 1; }\n else if (counts[x] == max_votes) num++;\n }\n if (num > 1) { printf(\"TIE\\n\"); return; }\n\n reset();\n for (int i=0; i<N; ++i)\n {\n counts[vect[i]]++;\n if (counts[y1] > max_vote(N,y1) + N-i-1) { printf(\"%c %d\\n\", y1+'A', i+1); return; }\n }\n}\n\n\n//------------------------------------------------------------------------------\nvoid test()\n{\n\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //test(); return 0;\n //DEBUG = true;\n //--------------------------------------------------------------------------\n int N, num;\n char c;\n while (true)\n {\n num = scanf(\"%d \", &N); if (N == 0) break;\n reset();\n for (int i=0; i<N; ++i)\n {\n num = scanf(\"%c \", &c);\n vect[i] = c-'A';\n counts[c-'A']++;\n }\n if (DEBUG)\n {\n for (int x=0; x<LETTERS; ++x)\n if (counts[x])\n printf(\"%c => %d\\n\", x+'A', counts[x]);\n }\n query(N);\n }\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 20, "memory_kb": 3568, "score_of_the_acc": -1.0357, "final_rank": 19 }, { "submission_id": "aoj_1609_9458138", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint main() {\nwhile(1) {\n int N;\n cin >> N;\n if (N == 0) return 0;\n int NOW[26] = {};\n vector<int> V(N);\n rep(i,0,N) {\n char C;\n cin >> C;\n V[i] = C - 'A';\n }\n bool check = false;\n rep(i,0,N) {\n NOW[V[i]]++;\n vector<pair<int,int>> X(26);\n rep(j,0,26) X[j] = {NOW[j],j};\n sort(ALL(X));\n reverse(ALL(X));\n if (X[0].first > X[1].first + N - i - 1) {\n cout << (char)(X[0].second + 'A') << ' ' << i + 1 << endl;\n check = true;\n break;\n }\n }\n if (!check) {\n cout << \"TIE\" << endl;\n }\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3436, "score_of_the_acc": -0.7462, "final_rank": 14 }, { "submission_id": "aoj_1609_9424408", "code_snippet": "#include <bits/stdc++.h>\n//using\nusing intl = long long;\nusing ll = long long;\nusing itnl = long long;//typo用\nusing uintl = unsigned long long;\nusing itn = int;//typo用\nusing ld = long double;\nusing namespace std;\n\n//関数マクロ\n#define rep(i, n) for(intl i = 0; i < (intl)(n); i++)\n#define rrep(i, n) for(intl i = (intl)(n) - 1; i >= 0; i--)\n#define repi(i, a, b) for(intl i = (intl)(a); i < (intl)(b); i++)\n#define rrepi(i, a, b) for(intl i = (intl)(a) - 1; i >= (intl)(b); i--)\n#define all(x) (x).begin(),(x).end()\n#define rall(x) (x).rbegin(),(x).rend()\n#define m0(x) memset(x,0,sizeof(x))\n#define m1(x) memset(x,1,sizeof(x))\n#define fill(x,y) memset(x,y,sizeof(x))\n#define alength(a) (sizeof(a) / sizeof(a[0]))\n#define debug(x) cout << #x << \":\" << x << endl\n\n//定数マクロ\n#define pb push_back\n#define mp make_pair\n#define pii pair<intl,intl>\n\n//定数\nconst intl INF = 1e18;\nconst intl MOD = 1e9+7; //1e9より大きい最小の素数, 1e9より小さい最大の素数は998244353\nconst ld EPS = 1.0e-14;\nconst ld PI = acos(-1);\n\n//テンプレート関数\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) {a = b; return true;} return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) {a = b; return true;} return false; }\nintl modpow(intl a, intl n, intl mod) {intl res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}\nintl modpow(intl a, intl n) {intl res = 1; while (n > 0) {if (n & 1) res = res * a % MOD;a = a * a % MOD;n >>= 1;}return res;}\n//intl gcd(intl a, intl b){ if(!b)return a; return gcd(b, a % b); } //c++17からある\n//intl lcm(intl a,intl b){ return a / gcd(a, b) * b; } //c++17からある\n\n//自作テンプレート関数\nvoid fp(bool f){cout << (f ? \"Yes\" : \"No\") << endl;}//Yes,Noの出力を楽にするよう\nvoid fp(bool f, string s, string t){cout << (f ? s : t) << endl;}//上の関数のYes,Noを任意の文字列でできるようにしたもの\nintl fact(intl k){ intl a = 1; for(int i = 2; i <= k; i++){ a *= i; } return a; }//k!を求める関数,O(N)\nintl digit10(intl a){ intl b = 0; do{ a /= 10; b++; }while(a); return b; }//aを10進数で表したときの桁数を求める関数,O(logN)\nintl mceil(intl a, intl b){ return (a + b - 1) / b; }//aをbで割って切り上げる関数,O(1)\n\n\n//--------------入力受け取る場所----------------------------------------------------\nintl n;\nvector<char> c;\n\n\nvoid input_var(){\n cin >> n;\n if (n == 0) exit(0);\n c.resize(n);\n rep(i, n) cin >> c[i];\n}\n//-------------------------------------------------------------------------------\n\nvoid solve(){\n map<char, intl> m;\n for (char i = 'A'; i <= 'Z'; i++) {\n m[i] = 0;\n }\n rep(i, n) {\n m[c[i]]++;\n intl num1 = 0;\n char ch1 = '0';\n intl num2 = 0;\n char ch2 = '0';\n for (char i = 'A'; i <= 'Z'; i++) {\n if (m[i] > num1) {\n num2 = num1;\n ch2 = ch1;\n num1 = m[i];\n ch1 = i;\n }\n else if (m[i] > num2) {\n num2 = m[i];\n ch2 = i;\n }\n }\n if (num1 - num2 > n - i - 1) {\n cout << ch1 << \" \" << i + 1 << endl;\n return;\n }\n }\n cout << \"TIE\" << endl;\n}\n\nsigned main(){\n cout << fixed << setprecision(10);\n\n while (true) {\n input_var();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.5846, "final_rank": 6 }, { "submission_id": "aoj_1609_9372185", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nbool solve()\n{\n int N;\n cin>>N;\n if(N==0)return 0;\n vector<char>C(N);\n for(int i=0;i<N;i++)cin>>C[i];\n int rem=N;\n vector<int>cnt(26,0);\n for(int i=0;i<N;i++)\n {\n rem--;\n cnt[C[i]-'A']++;\n for(int c=0;c<26;c++)\n {\n int MAX=0;\n for(int d=0;d<26;d++)if(c!=d)MAX=max(MAX,cnt[d]);\n if(cnt[c]>MAX+rem)\n {\n cout<<char('A'+c)<<' '<<i+1<<'\\n';\n return 1;\n }\n }\n }\n cout<<\"TIE\"<<'\\n';\n return 1;\n}\nint main(){while(solve()){}}", "accuracy": 1, "time_ms": 10, "memory_kb": 3440, "score_of_the_acc": -0.7538, "final_rank": 15 }, { "submission_id": "aoj_1609_9361364", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define all(x) (x).begin(),(x).end()\n#define eb emplace_back\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing vl = vector<long long>;\nusing vvl = vector<vector<long long>>; using vs = vector<string>;\nusing pl = pair<long long, long long>;\ntemplate <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}\ntemplate <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}\ntemplate <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}\nconst ll INF = 1LL << 60;\nconst ld PI = acos(-1);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);\n \ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }\ntemplate <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\ntemplate <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }\ntemplate <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << \"=>\" << v.second << ','; os << '}'; return os; }\ntemplate <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << \"=>\" << v.second << ','; os << '}'; return os; }\n#ifdef LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl\n#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << std::endl : std::cerr)\n#else\n#define dbg(x) ((void)0)\n#define dbgif(cond, x) ((void)0)\n#endif\nvoid solve();\nint main(){\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout<<fixed<<setprecision(15);\n int num_tc = 1;\n //cin >> num_tc;\n rep(tc,num_tc){\n //cout << \"Case #\" << tc+1 << \": \" ;// << endl;\n solve();\n }\n}\n//見切り発車で実装しては行けない.頭の中でコードが完全に出来上がるまで書き始めるな.\nvoid solve(){\n while(true){\n ll N;cin>>N;\n if(N==0)return;\n vector<ll> cnt(26),res(26);\n vector<char> A(N);\n rep(i,N){\n cin>>A[i];\n res[A[i]-'A']++;\n }\n ll ans = -1;\n char who = 'A';\n rep(i,N){\n char a = A[i];\n cnt[a-'A']++;\n //本当に勝ったか?\n //自分以外の人が残りを全て得ても超えられない\n rep(k,26){\n bool ok = true;\n rep(j,26){\n if(j==k)continue;\n ok&=cnt[k]>cnt[j]+N-i-1;\n }\n if(ok){\n who = 'A'+k;\n ans = i+1;\n break;\n }\n }\n if(ans!=-1)break;\n }\n if(ans==-1)cout<<\"TIE\"<<endl;\n else cout<<who<<\" \"<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3364, "score_of_the_acc": -0.6077, "final_rank": 8 }, { "submission_id": "aoj_1609_9335792", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\nint main(void) {\n while(true) {\n int n;\n cin >> n;\n if(n == 0) break;\n vector<pair<int, int>> cnt(26);\n rep(i, 0, 26) cnt[i].second = i;\n char ans = 'a';\n int d = 1e9;\n rep(i, 0, n) {\n char c;\n cin >> c;\n cnt[c - 'A'].first++;\n vector<pair<int, int>> tmp = cnt;\n sort(tmp.rbegin(), tmp.rend());\n int ma1 = tmp[0].first, ma2 = tmp[1].first;\n if(ma2 + (n - 1 - i) < ma1) {\n ans = 'A' + tmp[0].second;\n d = min(d, (int)i + 1);\n }\n }\n if(d == 1e9) {\n cout << \"TIE\" << '\\n';\n } else {\n cout << ans << ' ' << d << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3356, "score_of_the_acc": -0.5923, "final_rank": 7 }, { "submission_id": "aoj_1609_9121595", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(v) v.begin(),v.end()\n\nint main() {\n while (true) {\n int n;\n cin >> n;\n if (n==0) return 0;\n vector<int> c(n);\n for (int i = 0; i < n; ++i) {\n char x;\n cin >> x;\n c[i] = x - 'A';\n }\n\n int K = 26;\n vector<int> cnt(K);\n bool fin = false;\n for (int i = 0; i < n && !fin; ++i) {\n cnt[c[i]]++;\n\n for (int j = 0; j < K && !fin; ++j) {\n int mx = 0;\n for (int k = 0; k < K; ++k) {\n if (j==k) continue;\n mx = max(mx,cnt[k]);\n }\n\n if (cnt[j] > mx+n-i-1) {\n cout << (char)('A'+j) << ' ' << i+1 << endl;\n fin = true;\n }\n }\n }\n\n if (!fin) cout << \"TIE\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3320, "score_of_the_acc": -0.5231, "final_rank": 4 }, { "submission_id": "aoj_1609_8528329", "code_snippet": "#include <bits/stdc++.h>\n\nint solve() {\n std::array<int, 256> ans;\n ans.fill(0);\n\n int n;\n std::cin >> n;\n if (n == 0) return 1;\n std::vector<char> cs(n);\n for (int i = 0; i < n; i++) std::cin >> cs[i];\n\n auto check = [&](int rem) -> int {\n int top_i = std::max_element(ans.begin(), ans.end()) - ans.begin();\n int top_v = ans[top_i];\n ans[top_i] = 0;\n int second_i = std::max_element(ans.begin(), ans.end()) - ans.begin();\n int second_v = ans[second_i];\n\n if (top_v > second_v + rem) return top_i;\n ans[top_i] = top_v;\n return -1;\n };\n\n for (int i = 0; i < n; i++) {\n int rem = n - i - 1;\n ans[cs[i]]++;\n auto res = check(rem);\n if (res != -1) {\n std::cout << (char)res << ' ' << i + 1 << std::endl;\n return 0;\n }\n }\n std::cout << \"TIE\" << std::endl;\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3356, "score_of_the_acc": -0.628, "final_rank": 10 }, { "submission_id": "aoj_1609_7818273", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nstring func(int n){\n\tmap<string,int> m;\n\tstring res = \"TIE\";\n\tfor(int i=0;i<n;++i){\n\t\tstring p;\n\t\tcin >> p;\n\t\t++m[p];\n\t\tint r = n - i - 1;\n\t\tvector<pair<int,string>> line;\n\t\tline.emplace_back(0,\"A\");\n\t\tfor(auto i:m)line.emplace_back(i.second,i.first);\n\t\tsort(line.begin(),line.end());\n\t\tif(line[line.size()-2].first + r < line[line.size()-1].first){\n\t\t\tif(res == \"TIE\")res = line.back().second + \" \" + to_string(i+1);\n\t\t}\n\t}\n\treturn res;\n}\n\nint main(){\n\tint n;\n\twhile(cin >> n && n){\n\t\tcout << func(n) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3552, "score_of_the_acc": -0.9692, "final_rank": 18 }, { "submission_id": "aoj_1609_7158845", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n while (cin >> n, n) {\n int cnt[128] = {};\n auto f = [&](int k) -> int {\n auto p = max_element(begin(cnt), end(cnt));\n for (auto q = cnt; q < end(cnt); q++) {\n if (q != p) {\n if (*p <= *q + k) {\n return -1;\n }\n }\n }\n return p - cnt;\n };\n bool flag = false;\n for (auto i = 0; i != n; ++i) {\n char c;\n cin >> c;\n cnt[c]++;\n auto r = f(n - 1 - i);\n if (~r && !flag) {\n cout << char(r) << \" \" << i + 1 << \"\\n\";\n flag = 1;\n }\n }\n if (!flag) cout << \"TIE\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3364, "score_of_the_acc": -0.6077, "final_rank": 8 }, { "submission_id": "aoj_1609_6770374", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n #include <atcoder/all>\nusing namespace atcoder;\n#endif\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef tuple<ll, ll, ll> T;\n#define rep(i, n) for(ll i = 0; i < n; i++)\n\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n \n while(true){\n int n; cin >> n;\n if(n == 0)break;\n vector<char> c(n);\n rep(i,n)cin >> c[i];\n map<char,int> num;\n int m1 = 0,m2 = 0,i1 = -1;\n bool flag = false;\n rep(i,n){\n num[c[i]]++;\n m1 = 0,m2 = 0,i1 = -1;\n rep(j,26){\n if(num['A'+j] > m1 && i1 != j){\n m2 = m1;\n m1 = num['A'+j];\n i1 = j;\n }\n else if(num['A'+j] > m2){\n m2 = num['A'+j];\n }\n }\n // cout << m1 << \" \" << m2 << endl;\n if(m1 - m2 > n-i-1){\n cout << (char)('A'+i1) << \" \" << i+1 << endl;\n flag = true;\n break;\n }\n }\n if(!flag) cout << \"TIE\" << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3444, "score_of_the_acc": -0.7615, "final_rank": 16 }, { "submission_id": "aoj_1609_6745947", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n while(true){\n bool go_nxt = false;\n int64_t N; cin >> N;\n if(N == 0){\n return 0;\n }\n vector<int64_t> vote(27);\n for(int64_t i = 0; i < N; i++){\n char c; cin >> c;\n if(go_nxt) continue;\n vote[c - 'A']++;\n for(int64_t k = 0; k < 26; k++){\n bool flag = true;\n for(int64_t j = 0; j < 26; j++){\n if(k == j) continue;\n if(vote[k] <= vote[j] + N - i - 1) flag = false;\n }\n if(flag){\n cout << char('A' + k) << \" \" << i+1 << '\\n';\n go_nxt = true;\n }\n }\n }\n if(not go_nxt) cout << \"TIE\" << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3196, "score_of_the_acc": -0.2846, "final_rank": 2 }, { "submission_id": "aoj_1609_6611416", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\n#define rep(i, n) for(int i = 0; i < (int)n; i++)\n#define repp(i, m, n) for(int i = (int)m; i < (int)n; i++)\n\n#define ALL(x) x.begin(),x.end()\n\n#define YESNO(bool) if(bool){cout<<\"YES\"<<endl;}else{cout<<\"NO\"<<endl;}\n#define yesno(bool) if(bool){cout<<\"yes\"<<endl;}else{cout<<\"no\"<<endl;}\n#define YesNo(bool) if(bool){cout<<\"Yes\"<<endl;}else{cout<<\"No\"<<endl;}\n#define keta(x) cout << std::fixed << std::setprecision(15) << (double)x << endl;\n\nvoid solve(int n){\n vector<char> c(n);\n rep(i, n) cin >> c[i];\n int max_num = 0; //票数の最大値\n int next_max_num = 0;\n\n map<char, int> cnt;\n for(char s = 'A'; s <= 'Z'; s++){\n cnt[s] = 0;\n }\n\n rep(i, n){\n cnt[c[i]]++;\n //最大値の番号と数を求める\n char maxID; int maxNum = 0;\n //最大値の次の数\n int nextNum = 0;\n\n for(char s = 'A'; s <= 'Z'; s++){\n if(maxNum <= cnt[s]){\n maxNum = cnt[s];\n maxID = s;\n }\n }\n\n //次の票\n for(char s = 'A'; s <= 'Z'; s++){\n if(maxID == s) continue;\n if(nextNum <= cnt[s]){\n nextNum = cnt[s];\n }\n }\n\n if(maxNum - nextNum > n-i-1){\n cout << maxID << \" \" << i+1 << endl;\n return;\n }\n }\n cout << \"TIE\" << endl;\n \n}\n\nint main(){\n while(1){\n int n;\n cin >> n;\n if(n == 0) break;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3340, "score_of_the_acc": -0.5615, "final_rank": 5 }, { "submission_id": "aoj_1609_6585450", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n//#include<atcoder/all>\n//using namespace atcoder;\nusing ll = long long;\n\nint n;\nvector<char> vc;\n\nvoid solve(){\n\tvector<int> cnt(26);\n\tfor(int i = 0;i<n;i++){\n\t\tcnt[vc[i]-'A']++;\n\t\tint amari = n-i-1;\n\t\tfor(int j = 0;j<26;j++){\n\t\t\tbool is_c = true;\n\t\t\tfor(int k = 0;k<26;k++){\n\t\t\t\tif(k==j)continue;\n\t\t\t\tif(cnt[j]<=cnt[k]+amari)is_c = false;\n\t\t\t}\n\t\t\tif(is_c){\n\t\t\t\tcout<<(char)(j+'A')<<' '<<i+1<<endl;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\tcout<<\"TIE\"<<endl;\n}\n\nsigned main(){\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\twhile(1){\n\t\tcin >> n;\n\t\tif(n==0)break;\n\t\tvc = vector<char>(n);\n\t\tfor(auto &i:vc)cin >> i;\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3452, "score_of_the_acc": -0.7769, "final_rank": 17 }, { "submission_id": "aoj_1609_6562367", "code_snippet": "#line 2 \"library/KowerKoint/base.hpp\"\n\n#ifndef ONLINE_JUDGE\n#define _GLIBCXX_DEBUG\n#endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define REP(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)\n#define ALL(a) (a).begin(),(a).end()\n#define END(...) { print(__VA_ARGS__); return; }\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VVVI = vector<VVI>;\nusing ll = long long;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing VVVL = vector<VVL>;\nusing VD = vector<double>;\nusing VVD = vector<VD>;\nusing VVVD = vector<VVD>;\nusing VS = vector<string>;\nusing VVS = vector<VS>;\nusing VVVS = vector<VVS>;\nusing VC = vector<char>;\nusing VVC = vector<VC>;\nusing VVVC = vector<VVC>;\nusing P = pair<int, int>;\nusing VP = vector<P>;\nusing VVP = vector<VP>;\nusing VVVP = vector<VVP>;\nusing LP = pair<ll, ll>;\nusing VLP = vector<LP>;\nusing VVLP = vector<VLP>;\nusing VVVLP = vector<VVLP>;\n\ntemplate <typename T>\nusing PQ = priority_queue<T>;\ntemplate <typename T>\nusing GPQ = priority_queue<T, vector<T>, greater<T>>;\n\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\n\nvoid print() { cout << '\\n'; }\ntemplate<typename T>\nvoid print(const T &t) { cout << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid print(const Head &head, const Tail &... tail) {\n cout << head << ' ';\n print(tail...);\n}\n\n#ifdef ONLINE_JUDGE\ntemplate<typename... Args>\nvoid dbg(const Args &... args) {}\n#else\nvoid dbg() { cerr << '\\n'; }\ntemplate<typename T>\nvoid dbg(const T &t) { cerr << t << '\\n'; }\ntemplate<typename Head, typename... Tail>\nvoid dbg(const Head &head, const Tail &... tail) {\n cerr << head << ' ';\n dbg(tail...);\n}\n#endif\n\ntemplate< typename T1, typename T2 >\nostream &operator<<(ostream &os, const pair< T1, T2 >& p) {\n os << p.first << \" \" << p.second;\n return os;\n}\n\ntemplate< typename T1, typename T2 >\nistream &operator>>(istream &is, pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate< typename T >\nostream &operator<<(ostream &os, const vector< T > &v) {\n for(int i = 0; i < (int) v.size(); i++) {\n os << v[i] << (i + 1 != (int) v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate< typename T >\nistream &operator>>(istream &is, vector< T > &v) {\n for(T &in : v) is >> in;\n return is;\n}\n\ntemplate<typename T>\nvector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {\n vector<vector<T>> res;\n vector<T> cur;\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(val);\n }\n res.push_back(cur);\n return res;\n}\n\nvector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {\n vector<string> res;\n string cur = \"\";\n for(auto it = begin; it != end; it++) {\n if(*it == val) {\n res.push_back(cur);\n cur.clear();\n } else cur.push_back(val);\n }\n res.push_back(cur);\n return res;\n}\n\ntemplate< typename T1, typename T2 >\ninline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\n\ntemplate< typename T1, typename T2 >\ninline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\ntemplate <typename T>\npair<VI, vector<T>> compress(const vector<T> &a) {\n int n = a.size();\n vector<T> x;\n REP(i, n) x.push_back(a[i]);\n sort(ALL(x)); x.erase(unique(ALL(x)), x.end());\n VI res(n);\n REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();\n return make_pair(res, x);\n}\n\ntemplate <typename T>\npair<vector<T>, vector<T>> factorial(int n) {\n vector<T> res(n+1), rev(n+1);\n res[0] = 1;\n REP(i, n) res[i+1] = res[i] * (i+1);\n rev[n] = 1 / res[n];\n for(int i = n; i > 0; i--) {\n rev[i-1] = rev[i] * i;\n }\n return make_pair(res, rev);\n}\n#line 3 \"library/KowerKoint/internal_operator.hpp\"\n\nnamespace internal_operator {\n template <typename T>\n T default_add(T a, T b) { return a + b; }\n template <typename T>\n T default_sub(T a, T b) { return a - b; }\n template <typename T>\n T zero() { return T(0); }\n template <typename T>\n T default_div(T a, T b) { return a / b; }\n template <typename T>\n T default_mult(T a, T b) { return a * b; }\n template <typename T>\n T one() { return T(1); }\n template <typename T>\n T default_xor(T a, T b) { return a ^ b; }\n template <typename T>\n T default_and(T a, T b) { return a & b; }\n template <typename T>\n T default_or(T a, T b) { return a | b; }\n ll mod3() { return 998244353LL; }\n ll mod7() { return 1000000007LL; }\n ll mod9() { return 1000000009LL; }\n}\n\n#line 3 \"library/KowerKoint/integer.hpp\"\n\nVL divisor(ll n) {\n assert(n > 0);\n VL fow, bck;\n for(ll i = 1; i * i <= n; i++) {\n if(n % i == 0) {\n fow.push_back(i);\n if(i * i != n) bck.push_back(n / i);\n }\n }\n reverse(ALL(bck));\n fow.insert(fow.end(), ALL(bck));\n return fow;\n}\n\nbool is_prime(ll n) {\n assert(n > 0);\n for(ll d = 2; d*d <= n; d++) {\n if(n % d == 0) return false;\n }\n return true;\n}\n\nVL least_prime_factors(ll n) {\n assert(n > 0);\n VL lpfs(n+1, -1), primes;\n FOR(d, 2, n+1) {\n if(lpfs[d] == -1) {\n lpfs[d] = d;\n primes.push_back(d);\n }\n for(ll p : primes) {\n if(p * d > n || p > lpfs[d]) break;\n lpfs[p*d] = p;\n }\n }\n return lpfs;\n}\n\nVL prime_list(ll n) {\n assert(n > 0);\n VL primes;\n vector<bool> sieved(n+1);\n FOR(d, 2, n+1) {\n if(!sieved[d]) {\n primes.push_back(d);\n for(ll i = d*d; i <= n; i += d) sieved[i] = 1;\n }\n }\n return primes;\n}\n\nmap<ll, int> prime_factor(ll n) {\n assert(n > 0);\n map<ll, int> factor;\n for(ll d = 2; d*d <= n; d++) {\n while(n%d == 0) {\n n /= d;\n factor[d]++;\n }\n }\n if(n > 1) factor[n]++;\n return factor;\n}\n\nll extgcd(ll a, ll b, ll& x, ll& y) {\n x = 1, y = 0;\n ll nx = 0, ny = 1;\n while(b) {\n ll q = a / b;\n tie(a, b) = LP(b, a % b);\n tie(x, nx) = LP(nx, x - nx*q);\n tie(y, ny) = LP(ny, y - ny*q);\n }\n return a;\n}\n\nll inv_mod(ll n, ll m) {\n ll x, y;\n assert(extgcd(n, m, x, y) == 1);\n x %= m;\n if(x < 0) x += m;\n return x;\n}\n\nll pow_mod(ll a, ll n, ll m) {\n if(n == 0) return 1LL;\n if(n < 0) return inv_mod(pow_mod(a, -n, m), m);\n ll res = 1;\n while(n) {\n if(n & 1) {\n res *= a;\n res %= m;\n }\n n >>= 1;\n a *= a;\n a %= m;\n }\n return res;\n}\n\n#line 5 \"library/KowerKoint/modint.hpp\"\n\ntemplate <ll (*mod)()>\nstruct Modint {\n ll val;\n \n Modint(): val(0) {}\n\n Modint(ll x): val(x) {\n val %= mod();\n if(val < 0) val += mod();\n }\n\n Modint& operator+=(const Modint& r) {\n val += r.val;\n if(val >= mod()) val -= mod();\n return *this;\n }\n friend Modint operator+(const Modint& l, const Modint& r) {\n return Modint(l) += r;\n }\n\n Modint& operator-=(const Modint& r) {\n val -= r.val;\n if(val < mod()) val += mod();\n return *this;\n }\n friend Modint operator-(const Modint& l, const Modint& r) {\n return Modint(l) -= r;\n }\n\n Modint& operator*=(const Modint& r) {\n val *= r.val;\n val %= mod();\n return *this;\n }\n Modint operator*(const Modint& r) {\n return (Modint(*this) *= r);\n }\n friend Modint operator*(const Modint& l, const Modint& r) {\n return Modint(l) *= r;\n }\n\n Modint pow(ll n) const {\n return Modint(pow_mod(val, n, mod()));\n }\n\n Modint inv() const {\n return Modint(inv_mod(val, mod()));\n }\n\n Modint& operator/=(const Modint& r) {\n return (*this *= r.inv());\n }\n friend Modint operator/(const Modint& l, const Modint& r) {\n return Modint(l) /= r;\n }\n\n Modint& operator^=(const ll n) {\n val = pow_mod(val, n, mod());\n return *this;\n }\n Modint operator^(const ll n) {\n return this->pow(n);\n }\n\n Modint operator+() const { return *this; }\n Modint operator-() const { return Modint() - *this; }\n\n Modint& operator++() {\n val++;\n if(val == mod()) val = 0LL;\n return *this;\n }\n Modint& operator++(int) {\n Modint res(*this);\n ++*this;\n return res;\n }\n\n Modint& operator--() {\n if(val == 0LL) val = mod();\n val--;\n return *this;\n }\n Modint& operator--(int) {\n Modint res(*this);\n --*this;\n return res;\n }\n\n friend bool operator==(const Modint& l, const Modint& r) {\n return l.val == r.val;\n }\n friend bool operator!=(const Modint& l, const Modint& r) {\n return l.val != r.val;\n }\n\n static pair<vector<Modint>, vector<Modint>> factorial(int n) {\n vector<Modint> fact(n+1), rfact(n+1);\n fact[0] = 1;\n REP(i, n) fact[i+1] = fact[i] * (i+1);\n rfact[n] = 1 / fact[n];\n for(int i = n-1; i >= 0; i--) rfact[i] = rfact[i+1] * (i+1);\n return {fact, rfact};\n }\n\n friend istream& operator>>(istream& is, Modint& mi) {\n is >> mi.val;\n return is;\n }\n\n friend ostream& operator<<(ostream& os, const Modint& mi) {\n os << mi.val;\n return os;\n }\n};\n\nusing MI3 = Modint<internal_operator::mod3>;\nusing V3 = vector<MI3>;\nusing VV3 = vector<V3>;\nusing VVV3 = vector<VV3>;\nusing MI7 = Modint<internal_operator::mod7>;\nusing V7 = vector<MI7>;\nusing VV7 = vector<V7>;\nusing VVV7 = vector<VV7>;\nusing MI9 = Modint<internal_operator::mod9>;\nusing V9 = vector<MI9>;\nusing VV9 = vector<V9>;\nusing VVV9 = vector<VV9>;\n#line 3 \"library/KowerKoint/counting.hpp\"\n\ntemplate <typename T>\nstruct Counting {\n vector<T> fact, ifact;\n\n Counting() {}\n Counting(ll n) {\n expand(n);\n }\n\n void expand(ll n) {\n ll sz = (ll)fact.size();\n if(sz > n) return;\n fact.resize(n+1);\n ifact.resize(n+1);\n fact[0] = 1;\n FOR(i, max(1LL, sz), n+1) fact[i] = fact[i-1] * i;\n ifact[n] = 1 / fact[n];\n for(ll i = n-1; i >= sz; i--) ifact[i] = ifact[i+1] * (i+1);\n }\n\n T permutation(ll n, ll r) {\n assert(n >= r);\n assert(r >= 0);\n expand(n);\n return fact[n] * ifact[n-r];\n }\n\n T combination(ll n, ll r) {\n assert(n >= r);\n assert(r >= 0);\n expand(n);\n return fact[n] * ifact[r] * ifact[n-r];\n }\n\n T stirling(ll n, ll k) {\n assert(n >= k);\n assert(k >= 0);\n if(n == 0) return 1;\n T res = 0;\n int sign = k%2? -1 : 1;\n expand(k);\n REP(i, k+1) {\n res += sign * ifact[i] * ifact[k-i] * T(i).pow(n);\n sign *= -1;\n }\n return res;\n }\n\n vector<vector<T>> stirling_table(ll n, ll k) {\n assert(n >= 0 && k >= 0);\n vector<vector<T>> res(n+1, vector<T>(k+1));\n res[0][0] = 1;\n FOR(i, 1, n+1) FOR(j, 1, k+1) {\n res[i][j] = res[i-1][j-1] + j * res[i-1][j];\n }\n return res;\n }\n\n T bell(ll n, ll k) {\n assert(n >= 0 && k >= 0);\n expand(k);\n vector<T> tmp(k+1);\n int sign = 1;\n tmp[0] = 1;\n FOR(i, 1, k+1) {\n sign *= -1;\n tmp[i] = tmp[i-1] + sign * ifact[i];\n }\n T res = 0;\n REP(i, k+1) {\n res += T(i).pow(n) * ifact[i] * tmp[k-i];\n }\n return res;\n }\n\n vector<vector<T>> partition_table(ll n) {\n assert(n >= 0);\n vector<vector<T>> res(n+1, vector<T>(n+1));\n REP(i, n+1) res[0][i] = 1;\n FOR(i, 1, n+1) FOR(j, 1, n+1) {\n res[i][j] = res[i][j-1] + (i<j? 0 : res[i-j][j]);\n }\n return res;\n }\n};\n#line 2 \"Contests/Dummy/main.cpp\"\n\n/* #include \"atcoder/all\" */\n/* using namespace atcoder; */\n/* #include \"KowerKoint/expansion/ac-library/all.hpp\" */\n\nvoid solve(){\n while(1) {\n int n; cin >> n;\n if(!n) break;\n VC v(n); cin >> v;\n VP cnt(26);\n REP(i, 26) cnt[i].second = i;\n bool tie = true;\n REP(i, n) {\n cnt[v[i]-'A'].first++;\n VP cnt_s = cnt;\n sort(ALL(cnt_s));\n if(cnt_s[25].first > cnt_s[24].first+(n-i-1)) {\n tie = false;\n print((char)('A' + cnt_s[25].second), i+1);\n break;\n }\n }\n if(tie) print(\"TIE\");\n }\n}\n\n// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)\nint main() {\n // Fasterize input/output script\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(100);\n // scanf/printf user should delete this fasterize input/output script\n\n int t = 1;\n //cin >> t; // comment out if solving multi testcase\n for(int testCase = 1;testCase <= t;++testCase){\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 290, "memory_kb": 3452, "score_of_the_acc": -1.7769, "final_rank": 20 }, { "submission_id": "aoj_1609_6503072", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define INF ((1LL<<62)-(1LL<<31))\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\ntypedef long long ll;\ntypedef pair<ll,ll> pl;\n\nint main() {\n while(true) {\n int n;\n cin >> n;\n if(n==0) break;\n vector<char> c(n);\n map<char,int> mp;\n bool flag=false;\n rep(i,n) {\n cin >> c[i];\n mp[c[i]]++;\n if(!flag) {\n vector<pair<int,char>> vec;\n for(auto u:mp) vec.push_back({u.second,u.first});\n sort(rall(vec));\n if((int)vec.size()==1) {\n if(vec[0].first>n/2) {\n cout << vec[0].second << \" \" << i+1 << endl;\n flag=true;\n }\n } else {\n if(vec[0].first-vec[1].first>n-i-1) {\n cout << vec[0].second << \" \" << i+1 << endl;\n flag=true;\n }\n }\n } \n }\n if(!flag) cout << \"TIE\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3300, "score_of_the_acc": -0.4846, "final_rank": 3 }, { "submission_id": "aoj_1609_6471486", "code_snippet": "#include <iostream>\n#include <queue>\nusing namespace std;\n\nconst int MAX_N = 100;\nconst int MAX_CANDIDATE = 26;\n\nint Diff( int *count )\n{\n priority_queue<int> pq;\n for ( int i = 0; i < MAX_CANDIDATE; i++ )\n {\n pq.push( count[i] );\n }\n int first = pq.top(); pq.pop();\n int second = pq.top(); pq.pop();\n \n return first - second;\n}\n\nchar Max( int *count )\n{\n int max_count = 0;\n int index = 0;\n for ( int i = 0; i < MAX_CANDIDATE; i++ )\n {\n if ( count[i] > max_count )\n {\n max_count = count[i];\n index = i;\n }\n }\n \n return (char)index + 'A';\n}\n\nint main()\n{\n int n;\n char c[MAX_N];\n \n while ( 1 )\n {\n cin >> n;\n if ( !n ) break;\n \n for ( int i = 0; i < n; i++ )\n {\n cin >> c[i];\n }\n \n int count[MAX_CANDIDATE] = { 0 };\n int i;\n for ( i = 0; i < n; i++ )\n {\n count[c[i] - 'A']++;\n int diff = Diff( count );\n \n if ( n - i - 1 < diff )\n {\n cout << Max( count ) << ' ' << i + 1 << endl;\n break;\n }\n }\n if ( i >= n ) cout << \"TIE\" << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3048, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1609_6024663", "code_snippet": "#pragma GCC optimize(\"Ofast,no-stack-protector,unroll-loops,fast-math\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define pb(...) emplace_back(__VA_ARGS__)\n#define mp(a, b) make_pair(a, b)\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define lscan(x) scanf(\"%I64d\", &x)\n#define lprint(x) printf(\"%I64d\", x)\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define rep2(i, n) for (ll i = (ll)n - 1; i >= 0; i--)\n#define REP(i, l, r) for (ll i = l; i < (r); i++)\n#define REP2(i, l, r) for (ll i = (ll)r - 1; i >= (l); i--)\n#define siz(x) (ll) x.size()\ntemplate <class T>\nusing rque = priority_queue<T, vector<T>, greater<T>>;\n\ntemplate <class T>\nbool chmin(T &a, const T &b) {\n if (b < a) {\n a = b;\n return 1;\n }\n return 0;\n}\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (b > a) {\n a = b;\n return 1;\n }\n return 0;\n}\n\n__int128_t gcd(__int128_t a, __int128_t b) {\n if (a == 0)\n return b;\n if (b == 0)\n return a;\n __int128_t cnt = a % b;\n while (cnt != 0) {\n a = b;\n b = cnt;\n cnt = a % b;\n }\n return b;\n}\n\nlong long extGCD(long long a, long long b, long long &x, long long &y) {\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n long long d = extGCD(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\n\nstruct UnionFind {\n vector<ll> data;\n int num;\n\n UnionFind(int sz) {\n data.assign(sz, -1);\n num = sz;\n }\n\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y)\n return (false);\n if (data[x] > data[y])\n swap(x, y);\n data[x] += data[y];\n data[y] = x;\n num--;\n return (true);\n }\n\n int find(int k) {\n if (data[k] < 0)\n return (k);\n return (data[k] = find(data[k]));\n }\n\n ll size(int k) {\n return (-data[find(k)]);\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n};\n\ntemplate <int mod>\nstruct ModInt {\n int x;\n\n ModInt() : x(0) {\n }\n\n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {\n }\n\n ModInt &operator+=(const ModInt &p) {\n if ((x += p.x) >= mod)\n x -= mod;\n return *this;\n }\n\n ModInt &operator-=(const ModInt &p) {\n if ((x += mod - p.x) >= mod)\n x -= mod;\n return *this;\n }\n\n ModInt &operator*=(const ModInt &p) {\n x = (int)(1LL * x * p.x % mod);\n return *this;\n }\n\n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n\n ModInt operator-() const {\n return ModInt(-x);\n }\n\n ModInt operator+(const ModInt &p) const {\n return ModInt(*this) += p;\n }\n\n ModInt operator-(const ModInt &p) const {\n return ModInt(*this) -= p;\n }\n\n ModInt &operator++() {\n return *this += ModInt(1);\n }\n\n ModInt operator++(int) {\n ModInt tmp = *this;\n ++*this;\n return tmp;\n }\n\n ModInt &operator--() {\n return *this -= ModInt(1);\n }\n\n ModInt operator--(int) {\n ModInt tmp = *this;\n --*this;\n return tmp;\n }\n\n ModInt operator*(const ModInt &p) const {\n return ModInt(*this) *= p;\n }\n\n ModInt operator/(const ModInt &p) const {\n return ModInt(*this) /= p;\n }\n\n bool operator==(const ModInt &p) const {\n return x == p.x;\n }\n\n bool operator!=(const ModInt &p) const {\n return x != p.x;\n }\n\n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n\n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while (n > 0) {\n if (n & 1)\n ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n\n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n\n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt<mod>(t);\n return (is);\n }\n\n static int get_mod() {\n return mod;\n }\n};\n\nll mpow2(ll x, ll n, ll mod) {\n ll ans = 1;\n while (n != 0) {\n if (n & 1)\n ans = ans * x % mod;\n x = x * x % mod;\n n = n >> 1;\n }\n return ans;\n}\nll modinv2(ll a, ll mod) {\n ll b = mod, u = 1, v = 0;\n while (b) {\n ll t = a / b;\n a -= t * b;\n swap(a, b);\n u -= t * v;\n swap(u, v);\n }\n u %= mod;\n if (u < 0)\n u += mod;\n return u;\n}\n\n// constexpr int mod = 1000000007;\nconstexpr int mod = 998244353;\n// constexpr int mod = 31607;\nusing mint = ModInt<mod>;\n\nmint mpow(mint x, ll n) {\n mint ans = 1;\n while (n != 0) {\n if (n & 1)\n ans *= x;\n x *= x;\n n = n >> 1;\n }\n return ans;\n}\n\n// ----- library -------\n// ----- library -------\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (1) {\n int n;\n cin >> n;\n if (n == 0)\n break;\n char c;\n int ans = -1;\n char ansc;\n vector<int> cnt(26, 0);\n rep(i, n) {\n cin >> c;\n cnt[c - 'A']++;\n if (ans == -1) {\n vector<pair<int, int>> v;\n rep(i, 26) v.pb(cnt[i], i);\n sort(rall(v));\n if (v[0].first > v[1].first + n - 1 - i) {\n ans = i + 1;\n ansc = (char)('A' + v[0].second);\n }\n }\n }\n if (ans == -1)\n cout << \"TIE\\n\";\n else\n cout << ansc << ' ' << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3416, "score_of_the_acc": -0.7077, "final_rank": 13 }, { "submission_id": "aoj_1609_6007480", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define fs first\n#define sc second\n#define pb push_back\n#define mp make_pair\n#define eb emplace_back\n#define ALL(A) A.begin(), A.end()\n#define RALL(A) A.rbegin(), A.rend()\ntypedef long long ll;\ntypedef pair<ll, ll> P;\nconst ll mod = 1000000007;\nconst ll LINF = 1LL << 60;\nconst ll INF = 1 << 30;\n\nint main() {\n int m;\n\n while (1) {\n cin >> m;\n if (m == 0) break;\n map<char, int> v;\n vector<char> vv(m);\n for (int i = 0; i < m; i++) {\n cin >> vv[i];\n // v[vv[i]] = 0;\n }\n bool flag = true;\n for (int i = 1; i <= m; i++) {\n char c;\n c = vv[i - 1];\n v[c]++;\n for (auto e : v) {\n bool flag2 = true;\n for (auto ee : v) {\n if (e != ee) {\n if (flag && flag2 && e.sc <= ee.sc + (m - i)) {\n flag2 = false;\n }\n }\n }\n if (v.size() != 1 && flag2 && flag) {\n cout << e.fs << \" \" << i << endl;\n flag = false;\n } else if (v.size() == 1 && flag2 && flag) {\n if (e.sc * 2 > m) {\n cout << e.fs << \" \" << i << endl;\n flag = false;\n }\n }\n }\n }\n if (flag) {\n cout << \"TIE\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3380, "score_of_the_acc": -0.6385, "final_rank": 11 } ]
aoj_1610_cpp
Bamboo Blossoms The bamboos live for decades, and at the end of their lives, they flower to make their seeds. Dr. ACM, a biologist, was fascinated by the bamboos in blossom in his travel to Tsukuba. He liked the flower so much that he was tempted to make a garden where the bamboos bloom annually. Dr. ACM started research of improving breed of the bamboos, and finally, he established a method to develop bamboo breeds with controlled lifetimes. With this method, he can develop bamboo breeds that flower after arbitrarily specified years. Let us call bamboos that flower k years after sowing " k -year-bamboos." k years after being sowed, k -year-bamboos make their seeds and then die, hence their next generation flowers after another k years. In this way, if he sows seeds of k -year-bamboos, he can see bamboo blossoms every k years. For example, assuming that he sows seeds of 15-year-bamboos, he can see bamboo blossoms every 15 years; 15 years, 30 years, 45 years, and so on, after sowing. Dr. ACM asked you for designing his garden. His garden is partitioned into blocks, in each of which only a single breed of bamboo can grow. Dr. ACM requested you to decide which breeds of bamboos should he sow in the blocks in order to see bamboo blossoms in at least one block for as many years as possible. You immediately suggested to sow seeds of one-year-bamboos in all blocks. Dr. ACM, however, said that it was difficult to develop a bamboo breed with short lifetime, and would like a plan using only those breeds with long lifetimes. He also said that, although he could wait for some years until he would see the first bloom, he would like to see it in every following year. Then, you suggested a plan to sow seeds of 10-year-bamboos, for example, in different blocks each year, that is, to sow in a block this year and in another block next year, and so on, for 10 years. Following this plan, he could see bamboo blossoms in one block every year except for the first 10 years. Dr. ACM objected again saying he had determined to sow in all blocks this year. After all, you made up your mind to make a sowing plan where the bamboos bloom in at least one block for as many consecutive years as possible after the first m years (including this year) under the following conditions: the plan should use only those bamboo breeds whose lifetimes are m years or longer, and Dr. ACM should sow the seeds in all the blocks only this year. Input The input consists of at most 50 datasets, each in the following format. m n An integer m (2 ≤ m ≤ 100) represents the lifetime (in years) of the bamboos with the shortest lifetime that Dr. ACM can use for gardening. An integer n (1 ≤ n ≤ 500,000) represents the number of blocks. The end of the input is indicated by a line containing two zeros. Output No matter how good your plan is, a "dull-year" would eventually come, in which the bamboos do not flower in an ...(truncated)
[ { "submission_id": "aoj_1610_10848308", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\nusing ll = long long;\nconst double pi = acos(-1);\n#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)\n#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)\n#define REP(i,n) FOR(i,0,n)\n#define RREP(i,n) RFOR(i,0,n)\n#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME\n#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)\n#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)\ntemplate<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n\tREP(i, v.size()) { if (i)os << \" \"; os << v[i]; }return os;\n}\ntemplate<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {\n\tREP(i, v.size()) { if (i)os << endl; os << v[i]; }return os;\n}\n\nint main() {\n\tll m, n;\n\n\twhile (true) {\n\t\tcin >> m >> n;\n\n\t\tif (m == 0) {\n\t\t\tbreak;\n\t\t}\n\n\t\tll x = min((ll)7400000, m * n * 10);\n\n\t\t//vector<ll> prime;\n\t\tvector<bool> is_prime(x + 1, true);\n\t\tll count = 0;\n\t\tll ans = 0;\n\n\t\tis_prime[0] = is_prime[1] = false;\n\n\t\tFOR(i, m, x + 1) {\n\t\t\tif (is_prime[i]) {\n\t\t\t\t//prime.push_back(i);\n\n\t\t\t\tcount++;\n\n\t\t\t\tif (count == n + 1) {\n\t\t\t\t\tans = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tfor (ll j = 2 * i; j <= x; j += i) {\n\t\t\t\t\tis_prime[j] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 640, "memory_kb": 4232, "score_of_the_acc": -0.0005, "final_rank": 1 }, { "submission_id": "aoj_1610_10828076", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n const int L = 7368800;\n\n while(true) {\n ll M, N;\n cin >> M >> N;\n\n if(!M) { break; }\n\n bitset<L> dp;\n\n ll p = M;\n for(ll i = 0; i < N; i++) {\n while(dp[p]) { p++; }\n for(ll j = p; j < L; j += p) { dp[j] = true; }\n }\n\n while(dp[p]) { p++; }\n\n cout << p << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 860, "memory_kb": 4192, "score_of_the_acc": -0.0346, "final_rank": 2 }, { "submission_id": "aoj_1610_10698781", "code_snippet": "#include<bits/stdc++.h>\n#define FOR(i,s,t) for(int i = s; i<t ; i++)\n#define SZ(x) (int)x.size()\n#define ALL(x) x.begin(), x.end()\n\nusing namespace std;\n\nusing VI = vector<int>;\nusing LL = long long;\nusing VL = vector<LL>;\n\nint era[7400000];\nint main() {\n\tcin.tie(0);\n\tios_base::sync_with_stdio(false);\n\n\tint N, M;\n\n\twhile (cin >> M >> N, N || M) {\n\t\tfill(era, era + 7400000, 0);\n\t\tint ans = 0;\n\t\tfor (int i = M; i < 7400000; i++) {\n\t\t\tif (era[i])continue;\n\t\t\telse {\n\t\t\t\tif (N > 0) {\n\t\t\t\t\tN--;\n\t\t\t\t\tera[i] = 1;\n\t\t\t\t\tfor (int j = 2 * i; j < 7400000; j += i) {\n\t\t\t\t\t\tera[j] = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tans = i;\n\t\t\t\t\tgoto X;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tX:;\n\t\tcout << ans << endl;\n\t}\n\t\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 3770, "memory_kb": 32280, "score_of_the_acc": -0.8563, "final_rank": 14 }, { "submission_id": "aoj_1610_10679951", "code_snippet": "#define _USE_MATH_DEFINES\n#include <bits/stdc++.h>\nusing ll = long long;\nusing ull = unsigned long long;\nint main()\n{\n ll m, n;\n while (std::cin >> m >> n,m)\n {\n std::vector<ll>vec(7400000, 0);\n ll x = m;\n for (ll i = 0; i <n ; i++)\n {\n while (vec[x])\n {\n x++;\n }\n for (ll j = x; j < vec.size(); j+=x)vec[j] = 1;\n }\n for (ll i = m; i < vec.size(); i++)\n {\n if (!vec[i])\n {\n std::cout << i << \"\\n\";\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 4930, "memory_kb": 60880, "score_of_the_acc": -1.4091, "final_rank": 17 }, { "submission_id": "aoj_1610_10676361", "code_snippet": "#line 1 \"2016C.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2016C.cpp\"\n\nusing namespace std;\n\n// using namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(x)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst int inf=1ll<<30;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\nbitset<7368792> v;\nconst int k = 7368792;\nint solve(){\n ll m, n; cin >> m >> n;\n if(m==0) return 1;\n rep(i,k) {\n v[i] = false;\n }\n ll left = n;\n rep(i,k) {\n if(v[i]) continue;\n ll cur = i+m;\n while(cur<k+m) {\n v[cur-m] = true;\n cur += i+m;\n }\n left--;\n if(left==0) break;\n }\n ll ans = 0;\n rep(i,k) {\n if(v[i]) ans++;\n else break;\n }\n cout << ans+m << '\\n';\n return 0;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(!solve());\n}", "accuracy": 1, "time_ms": 1150, "memory_kb": 4372, "score_of_the_acc": -0.0826, "final_rank": 10 }, { "submission_id": "aoj_1610_10672835", "code_snippet": "#include <bits/stdc++.h>\n#define FAST_IO \n\n#define OVERRIDE(a,b,c,d,...) d\n#define REP2(i,n) for (i32 i = 0; i < (i32)(n); ++i)\n#define REP3(i,m,n) for (i32 i = (i32)(m); i < (i32)(n); ++i)\n#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)\n#define PER2(i,n) for (i32 i = (i32)(n)-1; i >= 0; --i)\n#define PER3(i,m,n) for (i32 i = (i32)(n)-1; i >= (i32)(m); --i)\n#define PER(...) OVERRIDE(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)\n#define ALL(x) begin(x), end(x)\n#define LEN(x) (i32)(x.size())\nusing namespace std;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing i32 = signed int;\nusing i64 = signed long long;\nusing f64 = double;\nusing f80 = long double;\nusing pi = pair<i32, i32>;\nusing pl = pair<i64, i64>;\ntemplate <typename T>\nusing V = vector<T>;\ntemplate <typename T>\nusing VV = V<V<T>>;\ntemplate <typename T>\nusing VVV = V<V<V<T>>>;\ntemplate <typename T>\nusing VVVV = V<V<V<V<T>>>>;\ntemplate <typename T>\nusing PQR = priority_queue<T, V<T>, greater<T>>;\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n if (x > y) {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n if (x < y) {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ni32 lob(const V<T> &arr, const T &v) {\n return (i32)(lower_bound(ALL(arr), v) - arr.begin());\n}\ntemplate <typename T>\ni32 upb(const V<T> &arr, const T &v) {\n return (i32)(upper_bound(ALL(arr), v) - arr.begin());\n}\ntemplate <typename T>\nV<i32> argsort(const V<T> &arr) {\n V<i32> ret(arr.size());\n iota(ALL(ret), 0);\n sort(ALL(ret), [&](i32 i, i32 j) -> bool {\n if (arr[i] == arr[j]) {\n return i < j;\n } else {\n return arr[i] < arr[j];\n }\n });\n return ret;\n}\n[[maybe_unused]] constexpr i32 INF = 1000000100;\n[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;\nstruct SetUpIO {\n SetUpIO() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} set_up_io;\nvoid scan(char &x) { cin >> x; }\nvoid scan(u32 &x) { cin >> x; }\nvoid scan(u64 &x) { cin >> x; }\nvoid scan(i32 &x) { cin >> x; }\nvoid scan(i64 &x) { cin >> x; }\nvoid scan(string &x) { cin >> x; }\ntemplate <typename T>\nvoid scan(V<T> &x) {\n for (T &ele : x) {\n scan(ele);\n }\n}\nvoid read() {}\ntemplate <typename Head, typename... Tail>\nvoid read(Head &head, Tail &...tail) {\n scan(head);\n read(tail...);\n}\n#define CHAR(...) char __VA_ARGS__; read(__VA_ARGS__);\n#define U32(...) u32 __VA_ARGS__; read(__VA_ARGS__);\n#define U64(...) u64 __VA_ARGS__; read(__VA_ARGS__);\n#define I32(...) i32 __VA_ARGS__; read(__VA_ARGS__);\n#define I64(...) i64 __VA_ARGS__; read(__VA_ARGS__);\n#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);\n#define VEC(type,name,size) V<type> name(size); read(name);\n#define VVEC(type,name,size1,size2) VV<type> name(size1, V<type>(size2)); read(name);\n#define DBG(...) (void)0\nconstexpr i32 K = 7368800;\ni32 arr[K];\ni32 id = 0;\nvoid solve(i32 m, i32 n) {\n ++id;\n i32 cur = m;\n REP(i, n) {\n while (arr[cur] == id) {\n ++cur;\n }\n i32 use = cur;\n for (i32 i = use; i < K; i += use) {\n arr[i] = id;\n }\n }\n while (arr[cur] == id) {\n ++cur;\n }\n cout << cur << endl;\n}\nint main() {\n while (true) {\n I32(m, n);\n if (m == 0) {\n break;\n }\n solve(m, n);\n }\n}", "accuracy": 1, "time_ms": 4730, "memory_kb": 32460, "score_of_the_acc": -1.0098, "final_rank": 15 }, { "submission_id": "aoj_1610_10670132", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#pragma GCC optimize(\"O0\")\n#else\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\n\n#include <bits/stdc++.h>\n// #include <bits/extc++.h>\nusing namespace std;\n\n// #include <atcoder/modint>\n// using mint = atcoder::modint998244353;\n// istream& operator>>(istream& in, mint& x) {\n// long long a;\n// in >> a;\n// x = a;\n// return in;\n// }\n// ostream& operator<<(ostream& out, const mint& x) {\n// return out << x.val();\n// }\n\nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing i128 = __int128;\nusing u128 = unsigned __int128;\nusing f128 = __float128;\n\ntemplate <class T>\nconstexpr T infty = 0;\ntemplate <>\nconstexpr int infty<int> = 1'000'000'000;\ntemplate <>\nconstexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;\ntemplate <>\nconstexpr u32 infty<u32> = infty<int>;\ntemplate <>\nconstexpr u64 infty<u64> = infty<ll>;\ntemplate <>\nconstexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;\ntemplate <>\nconstexpr double infty<double> = infty<ll>;\ntemplate <>\nconstexpr long double infty<long double> = infty<ll>;\n\nusing pi = pair<int, int>;\nusing pl = pair<ll, ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\ntemplate <class T>\nusing vc = vector<T>;\ntemplate <class T>\nusing vvc = vector<vc<T>>;\nusing vvi = vvc<int>;\nusing vvl = vvc<ll>;\ntemplate <class T>\nusing vvvc = vector<vvc<T>>;\ntemplate <class T>\nusing vvvvc = vector<vvvc<T>>;\ntemplate <class T>\nusing vvvvvc = vector<vvvvc<T>>;\ntemplate <class T>\nusing pqg = std::priority_queue<T, vector<T>, greater<T>>;\ntemplate <class T, class U>\nusing umap = unordered_map<T, U>;\n\n// template <typename K>\n// using tree = __gnu_pbds::tree<K, __gnu_pbds::null_type, std::less<>,\n// __gnu_pbds::rb_tree_tag,\n// __gnu_pbds::tree_order_statistics_node_update>;\n\n#define vv(type, name, h, ...) \\\n vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) \\\n vector<vector<vector<type>>> name( \\\n h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name( \\\n a, vector<vector<vector<type>>>( \\\n b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n\n// FOR(a) := for (ll _ = 0; _ < (ll)a; ++_)\n// FOR(i, a) := for (ll i = 0; i < (ll)a; ++i)\n// FOR(i, a, b) := for (ll i = a; i < (ll)b; ++i)\n// FOR(i, a, b, c) := for (ll i = a; i < (ll)b; i += (c))\n// FOR_R(a) := for (ll i = (a) - 1; i >= 0; --i)\n// FOR_R(i, a) := for (ll i = (a) - 1; i >= 0; --i)\n// FOR_R(i, a, b) := for (ll i = (b) - 1; i >= (ll)a; --i)\n#define FOR1(a) for (ll _ = 0; _ < (ll)a; ++_)\n#define FOR2(i, a) for (ll i = 0; i < (ll)a; ++i)\n#define FOR3(i, a, b) for (ll i = a; i < (ll)b; ++i)\n#define FOR4(i, a, b, c) for (ll i = a; i < (ll)b; i += (c))\n#define FOR1_R(a) for (ll i = (a) - 1; i >= 0; --i)\n#define FOR2_R(i, a) for (ll i = (a) - 1; i >= 0; --i)\n#define FOR3_R(i, a, b) for (ll i = (b) - 1; i >= (ll)a; --i)\n#define overload4(a, b, c, d, e, ...) e\n#define overload3(a, b, c, d, ...) d\n#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)\n#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)\n\n#define FOR_subset(t, s) \\\n for (int t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\nint popcnt_mod_2(int x) { return __builtin_parity(x); }\nint popcnt_mod_2(u32 x) { return __builtin_parity(x); }\nint popcnt_mod_2(ll x) { return __builtin_parityll(x); }\nint popcnt_mod_2(u64 x) { return __builtin_parityll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\ntemplate <typename T>\nT floor(T a, T b) {\n return a / b - (a % b && (a ^ b) < 0);\n}\ntemplate <typename T>\nT ceil(T x, T y) {\n return floor(x + y - 1, y);\n}\ntemplate <typename T>\nT bmod(T x, T y) {\n return x - y * floor(x, y);\n}\ntemplate <typename T>\npair<T, T> divmod(T x, T y) {\n T q = floor(x, y);\n return {q, x - q * y};\n}\n\ntemplate <typename T, typename U>\nT POW(U x_, int n) {\n T x = x_;\n T ret = 1;\n while (n > 0) {\n if (n & 1) ret *= x;\n x *= x;\n n >>= 1;\n }\n return ret;\n}\n\ntemplate <typename T, typename U>\nT SUM(const vector<U> &A) {\n T sm = 0;\n for (auto &&a : A) sm += a;\n return sm;\n}\n\n#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define UNIQUE(x) \\\n sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()\n\ntemplate <class T, class S>\ninline bool chmax(T &a, const S &b) {\n return (a < b ? a = b, 1 : 0);\n}\ntemplate <class T, class S>\ninline bool chmin(T &a, const S &b) {\n return (a > b ? a = b, 1 : 0);\n}\n\n// ? は -1\nvc<int> s_to_vi(const string &S, char first_char) {\n vc<int> A(S.size());\n FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }\n return A;\n}\n\ntemplate <typename T, typename U>\nvector<T> cumsum(vector<U> &A, int off = 1) {\n int N = A.size();\n vector<T> B(N + 1);\n FOR(i, N) { B[i + 1] = B[i] + A[i]; }\n if (off == 0) B.erase(B.begin());\n return B;\n}\n\ntemplate <typename T>\nvector<int> argsort(const vector<T> &A) {\n vector<int> ids(A.size());\n iota(all(ids), 0);\n sort(all(ids),\n [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });\n return ids;\n}\n\n// A[I[0]], A[I[1]], ...\ntemplate <typename T>\nvc<T> rearrange(const vc<T> &A, const vc<int> &I) {\n vc<T> B(I.size());\n FOR(i, I.size()) B[i] = A[I[i]];\n return B;\n}\n\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\n\nvoid print(){\n cout << '\\n';\n}\ntemplate<class T>\nvoid print(const T& a){\n cout << a << '\\n';\n}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\ntemplate<class T>\nvoid print(vector<T> &a){\n for (int i = 0; i < (int)a.size(); ++i) {\n cout << a[i] << \" \\n\"[i == (int)a.size() - 1];\n }\n}\ntemplate<class T>\nvoid print(vector<T> &&a){\n for (int i = 0; i < (int)a.size(); ++i) {\n cout << a[i] << \" \\n\"[i == (int)a.size() - 1];\n }\n}\ntemplate<class T>\nvoid print(vector<vector<T>> &a){\n for (int i = 0; i < (int)a.size(); ++i) {\n for (int j = 0; j < (int)a[i].size(); ++j) {\n cout << a[i][j] << \" \\n\"[j == (int)a[i].size() - 1];\n }\n }\n}\ntemplate<class T>\nvoid print(vector<vector<T>> &&a){\n for (int i = 0; i < (int)a.size(); ++i) {\n for (int j = 0; j < (int)a[i].size(); ++j) {\n cout << a[i][j] << \" \\n\"[j == (int)a[i].size() - 1];\n }\n }\n}\nvoid YESNO(bool b) { cout << (b ? \"YES\" : \"NO\") << endl; }\nvoid YesNo(bool b) { cout << (b ? \"Yes\" : \"No\") << endl; }\n\n#ifdef LOCAL\n// https://zenn.dev/sassan/articles/19db660e4da0a4\n#include \"/Library/cpp-dump/dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\n// CPP_DUMP_DEFINE_EXPORT_OBJECT(mint, val());\n#else\n#define dump(...)\n#define CPP_DUMP_SET_OPTION(...)\n#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)\n#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)\n#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)\n#endif\n\n\n//----------------------------------------------------------------\nconstexpr ll MAXA = 8000000;\n\nvoid solve() {\n ll m, n;\n cin >> m >> n;\n dump(m, n);\n if (m == 0 && n == 0) {\n exit(0);\n }\n vc<bool> ans(MAXA, false);\n while (n > 0) {\n ll k = m;\n while (k < MAXA) {\n ans[k] = true;\n k += m;\n }\n while (ans[m]) ++m;\n --n;\n }\n print(m);\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(20);\n CPP_DUMP_SET_OPTION(max_line_width, 80);\n CPP_DUMP_SET_OPTION(log_label_func, cpp_dump::log_label::filename());\n CPP_DUMP_SET_OPTION(enable_asterisk, true);\n while (true) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1080, "memory_kb": 4580, "score_of_the_acc": -0.0743, "final_rank": 7 }, { "submission_id": "aoj_1610_10668088", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool solve(){\n int m,n;cin>>m>>n;\n if(m==0)return 0;\n const int sz =1e7;\n vector<bool> isok(sz);\n int i=m;\n for(int t=0;t<n;t++){\n for(int j=i;j<sz;j+=i)isok[j]=true;\n while(i<sz&&isok[i])i++;\n }\n cout<<i<<endl;\n return 1;\n}\nint main(){\n //m,m+1,m+2...が最適?\n //No\n //配列を用意して貪欲\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 1460, "memory_kb": 4816, "score_of_the_acc": -0.1372, "final_rank": 12 }, { "submission_id": "aoj_1610_10637182", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll m, n;\nvoid input() { cin >> m >> n; }\nvoid solve() {\n vector<char> ok(8000000, 0);\n int i;\n for(i = m; 1; i++) {\n if(ok[i] == 0) {\n if(n) {\n n--;\n for(int j = i; j < ok.size(); j += i) {\n ok[j] = true;\n }\n } else {\n break;\n }\n }\n }\n cout << i << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(m == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1240, "memory_kb": 11204, "score_of_the_acc": -0.1852, "final_rank": 13 }, { "submission_id": "aoj_1610_10631573", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nusing PP = pair<int, P>;\nusing PLL = pair<ll, ll>;\nusing PPLL = pair<ll, PLL>;\n#define rep(i, n) for(ll i = 0; i < (ll)n; ++i)\n#define rrep(i, n) for(ll i = n - 1; i >= 0; --i)\n#define loop(i, a, b) for(ll i = a; i <= b; ++i)\n#define all(v) v.begin(), v.end()\n#define nC2(n) n * (n - 1) / 2\nconstexpr ll INF = 9009009009009009009LL;\nconstexpr int INF32 = 2002002002;\nconstexpr ll MOD = 998244353;\nconstexpr ll MOD107 = 1000000007;\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\ntemplate <class T>\ninline bool chmax(T &a, const T &b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmin(T &a, const T &b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ntemplate <typename Container,\n typename = std::enable_if_t<\n !std::is_same_v<Container, std::string> &&\n std::is_convertible_v<decltype(std::declval<Container>().begin()),\n typename Container::iterator>>>\nostream &operator<<(ostream &os, const Container &container) {\n auto it = container.begin();\n auto end = container.end();\n\n if (it != end) {\n os << *it;\n ++it;\n }\n\tfor (; it != end; ++it) {\n\t\tos << \" \" << *it;\n\t}\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i != v.size() - 1) os << \" \";\n }\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<vector<T>>& vv) {\n\tfor (size_t i = 0; i < vv.size(); ++i) {\n\t\tos << vv[i];\n\t\tif (i != vv.size() - 1) os << \"\\n\";\n }\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n\tassert(v.size() > 0);\n\tfor (size_t i = 0; i < v.size(); ++i) is >> v[i];\n\treturn is;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<vector<T>>& vv) {\n\tassert(vv.size() > 0);\n\tfor (size_t i = 0; i < vv.size(); ++i) is >> vv[i];\n\treturn is;\n}\n\nstruct phash {\n\ttemplate<class T1, class T2>\n inline size_t operator()(const pair<T1, T2> & p) const {\n auto h1 = hash<T1>()(p.first);\n auto h2 = hash<T2>()(p.second);\n\n\t\tsize_t seed = h1 + h2; \n\t\th1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = (h1 >> 16) ^ h1;\n seed ^= h1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n\t\th2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = (h2 >> 16) ^ h2;\n seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n return seed;\n }\n};\n\n\n\n\n\nint solve() {\n\tll m, n; cin >> m >> n;\n\tif (m == 0 && n == 0) return 1;\n\n\tvector<bool> a(1e7, false);\n\tloop(i, m, 7368791) {\n\t\tif (n == 0) break;\n\t\tif (a[i]) continue;\n\t\tn--;\n\t\tfor (int j = i; j < a.size(); j += i) {\n\t\t\ta[j] = true;\n\t\t}\n\t}\n\n\tll ans = 0;\n\tloop(i, m, 7368791) {\n\t\tif (!a[i]) break;\n\t\tans++;\n\t}\n\n\tcout << ans + m << \"\\n\";\n\n\treturn 0;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile (!solve()) { }\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1100, "memory_kb": 4696, "score_of_the_acc": -0.079, "final_rank": 8 }, { "submission_id": "aoj_1610_10605987", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\n#include <stdlib.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define all(x) (x).begin(), (x).end()\n//constexpr ll MOD = 1000000007;\nconstexpr ll MOD = 998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 1LL<<60;\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\nll gcd(ll a, ll b){\n if(a%b == 0){\n return b;\n }else{\n return gcd(b, a%b);\n }\n}\n\nll lcm(ll a, ll b){\n return a*b / gcd(a, b);\n}\n\nll powMod(ll x, ll n) {\n if (n == 0) return 1 % MOD;\n ll val = powMod(x, n / 2);\n val *= val;\n val %= MOD;\n if (n % 2 == 1) val *= x;\n return val % MOD;\n}\nint main() {\n while(true){\n ll m, n; cin >> m >> n;\n if(m*n == 0) break;\n vector<ll> ok(7368792,0);\n rep(i,m,7368792){\n if(ok[i] == 0){\n if(n == 0){\n cout << i << endl;\n break;\n }\n for(ll j = i; j < 7368792; j += i) ok[j] = 1;\n n--;\n }\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6990, "memory_kb": 60980, "score_of_the_acc": -1.7348, "final_rank": 20 }, { "submission_id": "aoj_1610_10577879", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T>\nostream &operator<<(ostream &os, const vector<T> &a){\n\tif (a.empty()) return os;\n\tos << a.front();\n\tfor (auto e : a | views::drop(1)){\n\t\tos << ' ' << e;\n\t}\n\treturn os;\n}\nvoid dump(auto... vs) {\n\t((cout << vs << ' '), ...) << endl;\n}\n\n\nvoid solve(ll N,ll M) {\n\tll L=8e6;\n\tvector<bool> B(L,false);\n\tll now=M;\n\trep(_,N){\n\t\twhile (B[now])now++;\n\t\tfor (ll j=now;j<L;j+=now){\n\t\t\tB[j]=true;\n\t\t}\n\t}\n\twhile (B[now])now++;\n\tcout<<now<<endl;\n return;\n}\n\n\nint main() {\n while (true){\n\t\tll N,M;\n\t\tcin>>M>>N;\n\t\tif (N==0)break;\n solve(N,M);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1120, "memory_kb": 4556, "score_of_the_acc": -0.0803, "final_rank": 9 }, { "submission_id": "aoj_1610_10565562", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n#define int long long\n\nvoid solve(int m, int n) {\n int mx = 8e6;\n vector<bool> ok(mx, false);\n\n for (int i=m; i<=mx; ++i) {\n if (n == 0) break;\n if (ok[i]) continue;\n --n;\n for (int j=1; j<=mx; ++j) {\n if (i*j >= mx) break;\n ok[i*j] = true;\n }\n }\n\n for (int i=m; i<=mx; ++i) {\n if (!ok[i]) {\n cout << i << endl;\n return ;\n }\n }\n}\n\nsigned main() {\n while(true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n solve(n, m);\n }\n}\n\n/*\nmemo\n\n1\nA\n4\nA A B B\n5\nL M N L N\n6\nK K K K K K\n6\nX X X Y Z X\n10\nA A A B A C A C C B\n10\nU U U U U V V W W W\n0\n\n\n*/", "accuracy": 1, "time_ms": 1180, "memory_kb": 4560, "score_of_the_acc": -0.0898, "final_rank": 11 }, { "submission_id": "aoj_1610_10565488", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cassert>\nusing namespace std;\nusing ll = long long;\ntemplate<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}\ntemplate<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}\nconstexpr int INTINF = 1000001000;\nconstexpr int INTMAX = 2147483647;\nconstexpr ll LLMAX = 9223372036854775807;\nconstexpr ll LLINF = 4450000000011100000;\n\nconstexpr int MX = 7368791; \n\nvoid solve(int M, int N) {\n vector<bool> can_make_tokyo(MX+1, false);\n\n int idx = M;\n\n for (int h=0; h<N; h++) {\n for (int i=idx; i<=MX; i++) {\n if (!can_make_tokyo[i]) {\n for (int j=idx; j<=MX; j+= i) {\n can_make_tokyo[j] = true;\n }\n while (can_make_tokyo[i]) i++;\n idx = i;\n break;\n }\n }\n }\n cout << idx << endl;\n}\n\nint main() {\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n while(true) {\n int m, n;\n cin >> m >> n;\n if (m == 0 && n == 0) break;\n solve(m,n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1040, "memory_kb": 4448, "score_of_the_acc": -0.0663, "final_rank": 5 }, { "submission_id": "aoj_1610_10495245", "code_snippet": "#include<bits/stdc++.h>\n//#include <atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\n#define all(v) v.begin(),v.end()\nusing ll = long long;\nusing ull = unsigned long long;\nusing lll = __int128;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\n//using mint=modint1000000007;\n//using mint=modint998244353;\n\nconst ll INF=1ll<<60;\nll mod10=1e9+7;\nll mod99=998244353;\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<n;++i)\n#define per(i,n) for(ll i=n-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=a;i<n;++i)\n#define per2(i,a,n) for (ll i=a;i>=n;--i)\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nbool solve(){\n ll M,N;cin>>M>>N;\n if(M==0&&N==0) return 1;\n ll K=7500000;\n vll A(K);\n ll now=M;\n rep(_,N){\n for(int i=now;i<K;i+=now){\n A[i]=1;\n }\n while(A[now]==1) now++;\n }\n cout<<now<<endl;\n return 0;\n}\n\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(false);\n \n while(!solve());\n}", "accuracy": 1, "time_ms": 4960, "memory_kb": 61812, "score_of_the_acc": -1.4258, "final_rank": 18 }, { "submission_id": "aoj_1610_10494247", "code_snippet": "# include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nconst double pi = acos(-1);\ntemplate<class T>constexpr T inf() { return ::std::numeric_limits<T>::max(); }\ntemplate<class T>constexpr T hinf() { return inf<T>() / 2; }\ntemplate <typename T_char>T_char TL(T_char cX) { return tolower(cX); }\ntemplate <typename T_char>T_char TU(T_char cX) { return toupper(cX); }\ntemplate<class T> bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; }\ntemplate<class T> bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; }\nint popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }\nint d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }\nint d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }\nll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); };\nll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; };\nll MOD(ll x, ll m){return (x%m+m)%m; }\nll FLOOR(ll x, ll m) {ll r = (x%m+m)%m; return (x-r)/m; }\ntemplate<class T> using dijk = priority_queue<T, vector<T>, greater<T>>;\n# define all(qpqpq) (qpqpq).begin(),(qpqpq).end()\n# define UNIQUE(wpwpw) (wpwpw).erase(unique(all((wpwpw))),(wpwpw).end())\n# define LOWER(epepe) transform(all((epepe)),(epepe).begin(),TL<char>)\n# define UPPER(rprpr) transform(all((rprpr)),(rprpr).begin(),TU<char>)\n# define rep(i,upupu) for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++)\n# define reps(i,opopo) for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++)\n# define len(x) ((ll)(x).size())\n# define bit(n) (1LL << (n))\n# define pb push_back\n# define eb emplace_back\n# define exists(c, e) ((c).find(e) != (c).end())\n\nstruct INIT{\n\tINIT(){\n\t\tstd::ios::sync_with_stdio(false);\n\t\tstd::cin.tie(0);\n\t\tcout << fixed << setprecision(20);\n\t}\n}INIT;\n\nnamespace mmrz {\n\tvoid solve();\n}\n\nint main(){\n\tmmrz::solve();\n}\n#define debug(...) (static_cast<void>(0))\n\nusing namespace mmrz;\n\nusing ll = long long;\n\n\nbool SOLVE(){\n\tll M,N;cin>>M>>N;\n\tif(N==0&&M==0) return 0;\n\tll K=7568791;\n\tvector<bool> vis(K+1);\n\tint fir = M;\n\tfor(;N > 0;N--){\n\t\twhile(vis[fir])fir++;\n\t\tfor(int i = fir;i <= K;i += fir){\n\t\t\tvis[i] = true;\n\t\t}\n\t}\n\twhile(vis[fir])fir++;\n\tcout << fir << '\\n';\n\t\n\treturn 1;\n\n}\n\nvoid mmrz::solve(){\n\twhile(SOLVE());\n}", "accuracy": 1, "time_ms": 1070, "memory_kb": 4448, "score_of_the_acc": -0.071, "final_rank": 6 }, { "submission_id": "aoj_1610_10475463", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\nconstexpr bool DEBUG = false;\nconstexpr ll INF = 2e18;\n\nconstexpr int MAX_SIZ = 7368791;\n\nint solve(int N, int M) {\n int cnt = 0;\n vector<bool> ok(MAX_SIZ + 1, false);\n for (int y = M; y <= MAX_SIZ; y++) {\n if (ok[y]) {\n continue;\n }\n\n // すでにN区画に花を植えているなら、その年が答え\n if (cnt >= N) {\n return y;\n }\n\n // y年竹を植える\n cnt++;\n for (int ny = y; ny <= MAX_SIZ; ny += y) {\n ok[ny] = true;\n }\n }\n return -1;\n}\n\nint main() {\n while (true) {\n int M, N;\n cin >> M >> N;\n if (N == 0) {\n break;\n }\n cout << solve(N, M) << endl;\n }\n}", "accuracy": 1, "time_ms": 1030, "memory_kb": 4428, "score_of_the_acc": -0.0645, "final_rank": 4 }, { "submission_id": "aoj_1610_10467211", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\n\nint main(){\n int m,n;\n while(true){\n cin>>m>>n;\n if(m==0&&n==0)break;\n int sz=7700000;\n \n vector<bool> check(sz,false);\n \n int cnt=0;\n \n for(int i=m;i<sz;i++){\n if(!check[i]){\n for(int j=i;j<sz;j+=i){\n check[j]=true;\n }\n cnt++;\n }\n if(cnt==n)break;\n }\n for(int i=m;i<sz;i++){\n if(!check[i]){\n cout<<i<<\"\\n\";\n break;\n }\n }\n \n \n }\n}", "accuracy": 1, "time_ms": 940, "memory_kb": 4308, "score_of_the_acc": -0.0487, "final_rank": 3 }, { "submission_id": "aoj_1610_10425670", "code_snippet": "#include <iostream>\n#include <string.h>\nusing namespace std;\n\nint memo[20000000];\nconst int max1=8000000;\nvoid f(int m,int n){\n\tmemset(memo,0,sizeof(memo));\n\tint ans=m+1;\n\tfor(int i=m;i<max1;i++){\n\t\tif(memo[i]==0){\n\t\t\tif(n==0){\n\t\t\t\tans=i;\n\t\t\t\tbreak;\n\t\t\t}else{\n\t\t\t\tn--;\n\t\t\t\tfor(int j=i*2;j<max1;j+=i){\n\t\t\t\t\tmemo[j]=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tcout<<ans<<endl;\n}\n\nint main() {\n\tint m,n;\n\twhile(true){\n\t\tcin>>m>>n;\n\t\tif(m+n==0)break;\n\t\tf(m,n);\n\t}\n\t\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4260, "memory_kb": 81480, "score_of_the_acc": -1.5701, "final_rank": 19 }, { "submission_id": "aoj_1610_10264278", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst int MAX = 7500000;\nvector<int> seen(MAX,-1);\nint t = 0;\n\nint solve(){\n\tint M; cin >> M;\n\tint N; cin >> N;\n\tif(M == 0){\n\t\treturn 0;\n\t}\n\tint cnt = 0;\n\tint now = M;\n\twhile(cnt < N){\n\t\tif(seen[now] == t){\n\t\t\tnow++;\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int i = now; i < MAX; i += now){\n\t\t\tseen[i] = t;\n\t\t}\n\t\tcnt++;\n\t\tnow++;\n\t}\n\twhile(seen[now] == t)now++;\n\tcout << now << endl;\n\tt++;\n\treturn 1;\n}\n\nint main(){\n\twhile(1){\n\t\tif(!solve())break;\n\t}\n}", "accuracy": 1, "time_ms": 4860, "memory_kb": 32700, "score_of_the_acc": -1.0334, "final_rank": 16 } ]
aoj_1614_cpp
Warp Drive The warp drive technology is reforming air travel, making the travel times drastically shorter. Aircraft reaching above the warp fields built on the ground surface can be transferred to any desired destination in a twinkling. With the current immature technology, however, building warp fields is quite expensive. Our budget allows building only two of them. Fortunately, the cost does not depend on the locations of the warp fields, and we can build them anywhere on the ground surface, even at an airport. Your task is, given locations of airports and a list of one way flights among them, find the best locations to build the two warp fields that give the minimal average cost . The average cost is the root mean square of travel times of all the flights, defined as follows. Here, m is the number of flights and t j is the shortest possible travel time of the j -th flight. Note that the value of t j depends on the locations of the warp fields. For simplicity, we approximate the surface of the ground by a flat two dimensional plane, and approximate airports, aircraft, and warp fields by points on the plane. Different flights use different aircraft with possibly different cruising speeds. Times required for climb, acceleration, deceleration and descent are negligible. Further, when an aircraft reaches above a warp field, time required after that to its destination is zero. As is explained below, the airports have integral coordinates. Note, however, that the warp fields can have non-integer coordinates. Input The input consists of at most 35 datasets, each in the following format. n m x 1 y 1 ... x n y n a 1 b 1 v 1 ... a m b m v m n is the number of airports, and m is the number of flights (2 ≤ n ≤ 20, 2 ≤ m ≤ 40). For each i , x i and y i are the coordinates of the i -th airport. x i and y i are integers with absolute values at most 1000. For each j , a j and b j are integers between 1 and n inclusive, and are the indices of the departure and arrival airports for the j -th flight, respectively. v j is the cruising speed for the j -th flight, that is, the distance that the aircraft of the flight moves in one time unit. v j is a decimal fraction with two digits after the decimal point (1 ≤ v j ≤ 10). The following are guaranteed. Two different airports have different coordinates. The departure and arrival airports of any of the flights are different. Two different flights have different departure airport and/or arrival airport. The end of the input is indicated by a line containing two zeros. Output For each dataset, output the average cost when you place the two warp fields optimally. The output should not contain an absolute error greater than 10 -6 . Sample Input 3 4 100 4 100 0 0 0 1 2 1.00 2 1 1.00 3 1 9.99 3 2 9.99 7 6 0 0 1 0 2 0 0 10 1 10 2 10 20 5 1 7 1.00 2 7 1.00 3 7 1.00 4 7 1.00 5 7 1.00 6 7 1.00 4 4 -1 -1 1 -1 -1 1 1 1 1 4 1.10 4 2 2.10 2 3 3.10 3 1 4.10 8 12 -91 0 -62 0 -18 0 2 0 23 0 55 0 63 0 80 0 2 7 3.96 3 2 2.25 2 4 5.48 8 7 ...(truncated)
[ { "submission_id": "aoj_1614_10579461", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T>\nostream &operator<<(ostream &os, const vector<T> &a){\n\tif (a.empty()) return os;\n\tos << a.front();\n\tfor (auto e : a | views::drop(1)){\n\t\tos << ' ' << e;\n\t}\n\treturn os;\n}\nvoid dump(auto... vs) {\n\t((cout << vs << ' '), ...) << endl;\n}\n\n\n// #pragma once\n\n#include <type_traits>\n#include <concepts>\n#include <compare>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <iostream>\n#include <numeric>\n#include <cassert>\n#include <ranges>\n\n#if __has_include(<boost/multiprecision/cpp_bin_float.hpp>)\n\n#include<boost/multiprecision/cpp_bin_float.hpp>\n\n#endif\n\nnamespace float_geometry {\n\ntemplate<class T>\nstruct is_floating_point_number : std::bool_constant<false> {};\n\ntemplate<std::floating_point F>\nstruct is_floating_point_number<F> : std::bool_constant<true> {};\n\n#if __has_include(<boost/multiprecision/cpp_bin_float.hpp>)\n\ntemplate<unsigned D>\nstruct is_floating_point_number<boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<D>>> : std::bool_constant<true> {};\n\ntemplate<unsigned D>\nstruct is_floating_point_number<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<D>>> : std::bool_constant<true> {};\n\n#endif\n\ntemplate<class F>\nconcept FloatingPoint = is_floating_point_number<F>::value;\n\ntemplate<FloatingPoint Float>\nconstexpr Float EPS = Float{1e-8};\n\ntemplate<FloatingPoint Float>\nconstexpr int sign(const Float &x, Float eps = EPS<Float>){\n return (x < -eps ? -1 : x > eps ? 1 : 0);\n}\n\ntemplate<FloatingPoint Float>\nstruct point {\n Float x, y;\n point () {}\n point (Float _x, Float _y) : x(_x), y(_y) {}\n point &operator+=(const point &r){\n x += r.x;\n y += r.y;\n return *this;\n }\n point &operator-=(const point &r){\n x -= r.x;\n y -= r.y;\n return *this;\n }\n point operator+() const {\n return *this;\n }\n point operator-() const {\n x = -x;\n y = -y;\n return *this;\n }\n point &operator*=(const Float &a){\n x *= a;\n y *= a;\n return *this;\n }\n\tpoint &operator/=(const Float &a){\n x /= a;\n y /= a;\n return *this;\n }\n friend point operator+(const point& lhs, const point& rhs){\n return point(lhs) += rhs;\n }\n friend point operator-(const point& lhs, const point& rhs){\n return point(lhs) -= rhs;\n }\n friend point operator*(const point& lhs, const Float& a){\n return point(lhs) *= a;\n }\n friend point operator*(const Float& a, const point& rhs){\n return point(rhs) *= a;\n }\n\tfriend point operator/(const point& lhs, const Float& a){\n return point(lhs) /= a;\n }\n auto operator<=>(const point&) const = default;\n // friend bool operator<(const point &lhs, const point &rhs){\n // if (lhs.x == rhs.x) return lhs.y < rhs.y;\n // return lhs.x < rhs.x;\n // }\n // friend bool operator==(const point &lhs, const point &rhs){\n // return (lhs.x == rhs.x && lhs.y == rhs.y);\n // }\n friend std::ostream &operator<<(std::ostream &os, const point& p){\n return os << p.x << ' ' << p.y;\n }\n friend std::istream &operator>>(std::istream &is, point &a){\n Float _x, _y; is >> _x >> _y;\n a = point(_x, _y);\n return (is);\n }\n friend Float norm(const point &a) {\n return a.x*a.x + a.y*a.y;\n }\n friend Float dot(const point &a, const point &b){\n return a.x*b.x + a.y*b.y;\n }\n friend Float cross(const point &a, const point &b){\n return a.x*b.y - a.y*b.x;\n }\n\tfriend point rot90(const point &a){\n\t\treturn point(-a.y, a.x);\n\t}\n friend point rot(const point &a, const Float rad){\n using std::cos, std::sin;\n Float cosrad = cos(rad);\n Float sinrad = sin(rad);\n return point(a.x * cosrad - a.y * sinrad, a.x * sinrad + a.y * cosrad);\n }\n friend Float abs(const point &a){\n using std::sqrt;\n return sqrt(norm(a));\n }\n\n friend int quadrant_atan2(const point &a){\n // not origin point\n // ceil ( atan2(y,x) / (pi/2) )\n\t\tint signx = sign(a.x);\n\t\tint signy = sign(a.y);\n\t\tif (signx <= 0 && signy < 0) return -1;\n\t\tif (signx > 0 && signy <= 0) return 0;\n\t\tif (signx >= 0 && signy > 0) return 1;\n\t\tif (signx < 0 && signy >= 0) return 2;\n // origin point x == 0 && y == 0\n return 0;\n }\n\n friend int sign_cross(const point &a, const point &b){\n using std::abs;\n return sign(cross(a, b), EPS<Float> * (abs(a.x) + abs(a.y) + abs(b.x) + abs(b.y)));\n // return sign(cross(a, b), EPS<Float> * (abs<Float>(a.x) + abs<Float>(a.y) + abs<Float>(b.x) + abs<Float>(b.y)));\n }\n\n friend int sign_dot(const point &a, const point &b){\n using std::abs;\n return sign(dot(a, b), EPS<Float> * (abs(a.x) + abs(a.y) + abs(b.x) + abs(b.y)));\n // return sign(dot(a, b), EPS<Float> * (abs<Float>(a.x) + abs<Float>(a.y) + abs<Float>(b.x) + abs<Float>(b.y)));\n }\n\n\tfriend int ccw(const point &a, point b, point c){\n\t\tb -= a;\n\t\tc -= a;\n\t\tint signcr = sign_cross(b, c);\n\t\tif (signcr > 0){\n\t\t\t// ccw \n\t\t\t// c\n\t\t\t// a --> b \n\t\t\treturn 1;\n\t\t}\n\t\tif (signcr < 0){\n\t\t\t// cw\n\t\t\t// a --> b\n\t\t\t// c\n\t\t\treturn -1;\n\t\t}\n\t\tif (sign_dot(b, c) < 0){\n\t\t\t// c a --> b\n\t\t\treturn 2;\n\t\t}\n\t\tif (norm(b) < norm(c)){\n\t\t\t// a --> b c\n\t\t\treturn -2;\n\t\t}\n\t\t// a - c -> b\n\t\treturn 0;\n\t}\n friend bool has_common_point(const point &a, const point &b){\n return sign(a.x - b.x) == 0 && sign(a.y - b.y) == 0;\n }\n\n ld arg() const {\n std::complex<ld> a(x, y);\n return std::arg(a);\n }\n};\n\ntemplate<FloatingPoint Float>\nstruct arg_less {\n constexpr bool operator()(const point<Float> &l, const point<Float> &r){\n using std::atan2;\n return atan2(l.y, l.x) < atan2(r.y, r.x);\n }\n};\n\ntemplate<FloatingPoint Float>\nvoid arg_sort(std::vector<point<Float>> &a){\n\tstd::sort(a.begin(), a.end(), arg_less<Float>{});\n}\n\n\ntemplate<FloatingPoint Float>\nstd::vector<int> upper_convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tstd::vector<int> ids(a.size());\n std::iota(ids.begin(), ids.end(), 0);\n\tstd::sort(ids.begin(), ids.end(), [&](int l, int r){\n\t\treturn a[l] < a[r];\n\t});\n\tstd::vector<int> st(a.size());\n\tint ptr = 0;\n\tfor (int i : ids){\n\t\tif (ptr >= 1 && a[st[ptr-1]].x == a[i].x) ptr--;\n\t\twhile (ptr >= 2){\n\t\t\tint c = st[ptr-1];\n\t\t\tint p = st[ptr-2];\n\t\t\tif (sign_cross(a[i] - a[c], a[c] - a[p]) > 0){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tptr--;\n\t\t}\n\t\tst[ptr++] = i;\n\t}\n\tst.resize(ptr);\n\treturn st;\n}\n\ntemplate<FloatingPoint Float>\nstd::vector<int> lower_convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tstd::vector<int> ids(a.size());\n std::iota(ids.begin(), ids.end(), 0);\n\tstd::sort(ids.begin(), ids.end(), [&](int l, int r){\n\t\treturn a[l] < a[r];\n\t});\n\tstd::vector<int> st(a.size());\n\tint ptr = 0;\n\tfor (int i : ids){\n\t\tif (ptr >= 1 && a[st[ptr-1]].x == a[i].x) continue;\n\t\twhile (ptr >= 2){\n\t\t\tint c = st[ptr-1];\n\t\t\tint p = st[ptr-2];\n\t\t\tif (sign_cross(a[c] - a[p], a[i] - a[c]) > 0){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tptr--;\n\t\t}\n\t\tst[ptr++] = i;\n\t}\n\tst.resize(ptr);\n\treturn st;\n}\n\ntemplate<FloatingPoint Float>\nstd::vector<int> convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tauto upper = upper_convex_hull_index(a);\n\tauto lower = lower_convex_hull_index(a);\n\tif (upper.size() == 1u){\n\t\t// lower.size() == 1u\n\t\tif (a[upper.front()] == a[lower.front()]){\n\t\t\treturn {upper.front()};\n\t\t}\n\t\treturn {lower.front(), upper.front()};\n\t}\n\tif (a[upper.back()] == a[lower.back()]){\n\t\tlower.pop_back();\n\t}\n\tlower.insert(lower.end(), upper.rbegin(), upper.rend());\n\tif (a[upper.front()] == a[lower.front()]) lower.pop_back();\n\treturn lower;\n}\n\n\ntemplate<FloatingPoint Float>\nstruct line {\n\tpoint<Float> end0, end1;\n line () {}\n\tline (const point<Float> &_end0, const point<Float> &_end1) : end0(_end0), end1(_end1) {\n\t\tassert(end0 != end1);\n\t}\n\t// auto operator<=>(const line &) const = default;\n\tpoint<Float> direction() const {\n\t\treturn end1 - end0;\n\t}\n friend bool is_parallel(const line &a, const line &b){\n return sign_cross(a.direction(), b.direction()) == 0;\n }\n friend bool is_orthgonal(const line &a, const line &b){\n return sign_dot(a.direction(), b.direction()) == 0;\n }\n\tfriend bool has_common_point(const line &a, const point<Float> &b){\n\t\treturn sign(cross(a.direction(), b - a.end0)) == 0;\n\t}\n\tfriend bool has_common_point(const line &a, const line &b){\n\t\treturn !is_parallel(a, b) || has_common_point(a, b.end0);\n\t}\n\tfriend point<Float> common_point(const line &a, const line &b){\n\t\t// assert(has_common_point(a, b));\n\t\tif (is_parallel(a, b)){\n\t\t\treturn a.end0;\n\t\t}\n\t\treturn a.end0 + a.direction() * cross(b.end0 - a.end0, b.end1 - b.end0) / cross(a.direction(), b.direction());\n\t}\n\tfriend point<Float> projection(const line &a, const point<Float> &b){\n\t\tauto dir = a.direction();\n\t\treturn a.end0 + dir * (dot(dir, b - a.end0) / norm(dir));\n\t}\n\tfriend point<Float> reflection(const line &a, const point<Float> &b){\n\t\tauto prj = projection(a, b);\n\t\treturn prj + prj - b;\n\t}\n friend line<Float> perpendicular_bisector(const point<Float> &p1, const point<Float> &p2){\n auto ctr = (p1 + p2) / Float{2};\n auto dir = rot90(p2 - p1);\n return line<Float>(ctr, ctr + dir);\n }\n};\n\ntemplate<FloatingPoint Float>\nline<Float> perpendicular_bisector(const point<Float> &p1, const point<Float> &p2){\n auto ctr = (p1 + p2) / Float{2};\n auto dir = rot90(p2 - p1);\n return line<Float>(ctr, ctr + dir);\n}\n\n\ntemplate<FloatingPoint Float>\nstruct segment {\n\tpoint<Float> end0, end1;\n segment () {}\n\tsegment (const point<Float> &_end0, const point<Float> &_end1) : end0(_end0), end1(_end1) {\n\t\tassert(end0 != end1);\n\t}\n\t// auto operator<=>(const segment &) const = default;\n\tpoint<Float> direction() const {\n\t\treturn end1 - end0;\n\t}\n friend bool is_parallel(const segment &a, const segment &b){\n return sign_cross(a.direction(), b.direction()) == 0;\n }\n friend bool is_orthgonal(const segment &a, const segment &b){\n return sign_dot(a.direction(), b.direction()) == 0;\n }\n\tfriend bool has_common_point(const segment &a, const segment &b){\n\t\treturn ccw(a.end0, a.end1, b.end0) * ccw(a.end0, a.end1, b.end1) <= 0\n\t\t\t&& ccw(b.end0, b.end1, a.end0) * ccw(b.end0, b.end1, a.end1) <= 0;\n\t}\n\tfriend bool has_common_point(const segment &a, const point<Float> &b){\n\t\treturn ccw(a.end0, a.end1, b) == 0;\n\t}\n\tfriend point<Float> common_point(const segment &a, const segment &b){\n\t\t// assert(has_common_point(a, b));\n\t\tif (is_parallel(a, b)){\n\t\t\tif (has_common_point(a, b.end0)){\n\t\t\t\treturn b.end0;\n\t\t\t}\n\t\t\tif (has_common_point(a, b.end1)){\n\t\t\t\treturn b.end1;\n\t\t\t}\n\t\t\treturn a.end0;\n\t\t}\n\t\treturn a.end0 + a.direction() * cross(b.end0 - a.end0, b.end1 - b.end0) / cross(a.direction(), b.direction());\n\t}\n\tline<Float> as_line() const {\n\t\treturn line<Float>(end0, end1);\n\t}\n};\n\ntemplate<FloatingPoint Float>\nstruct circle {\n point<Float> center;\n Float radius;\n circle () {}\n circle (const point<Float> &_center, const Float &_radius) : center(_center), radius(_radius) {\n assert(sign(radius) >= 0);\n if (radius < Float{}){\n radius = Float{};\n }\n }\n friend int num_of_common_tangents(const circle &a, const circle &b){\n if (has_common_point(a.center, b.center) && sign(a.radius - b.radius) == 0){\n return 5; // infty\n }\n Float d = abs(a.center - b.center);\n Float r1 = a.radius, r2 = b.radius;\n int sumsign = sign(d - (r1 + r2));\n if (sumsign >= 0) return sumsign + 3;\n using std::abs;\n int diffsign = sign(d - abs(r1 - r2));\n return diffsign + 1;\n }\n friend bool has_common_point(const circle &a, const circle &b){\n int cnt = num_of_common_tangents(a, b);\n return cnt == 1 || cnt == 2 || cnt == 3 || cnt == 5;\n }\n friend bool has_common_point(const circle &c, const line<Float> &l){\n point<Float> p = reflection(l, c.center);\n Float d = abs(c.center, p);\n return sign(c.radius - d) >= 0;\n }\n friend bool has_common_point(const circle &c, const segment<Float> &s){\n return std::ranges::any_of(common_points(c, s.as_line()), [&](auto p){\n return has_common_point(s, p);\n });\n }\n friend bool has_common_point(const circle &c, const point<Float> &p){\n return sign(abs(p - c.center) - c.radius) == 0;\n }\n friend std::vector<point<Float>> common_points(const circle &a, const circle &b){\n int cnt = num_of_common_tangents(a, b);\n assert(cnt != 5); // cannot be same circle\n if (cnt == 0 || cnt == 4){\n return {};\n }\n Float d = abs(a.center - b.center);\n Float x = (d*d + a.radius*a.radius - b.radius*b.radius) / (d + d);\n point<Float> p = a.center + (b.center - a.center) * x / d;\n if (cnt == 1 || cnt == 3 || sign(a.radius - x) <= 0){\n return {p};\n }\n point<Float> v = rot90(b.center - a.center);\n using std::sqrt;\n v *= sqrt(a.radius*a.radius - x*x) / abs(v);\n return {p + v, p - v};\n }\n friend std::vector<point<Float>> common_points(const circle &c, const line<Float> &l) {\n point<Float> p = projection(l, c.center);\n Float d = abs(c.center - p);\n int cnt = sign(c.radius - d) + 1;\n if (cnt == 0){\n return {};\n }\n if (cnt == 1){\n return {p};\n }\n point<Float> v = l.direction();\n using std::sqrt;\n v *= sqrt(c.radius*c.radius - d*d) / abs(v);\n return {p + v, p - v};\n }\n};\n\n} // float_geometry\n\nusing namespace float_geometry;\nusing vec=point<ld>;\n\nvoid solve(ll N,ll M) {\n\tvector<vec> AP(N);\n\trep(i,N){\n\t\tcin>>AP[i];\n\t}\n\tvector<tuple<ll,ll,ld>> edge(M);\n\trep(i,M){\n\t\tll a,b;\n\t\tld v;\n\t\tcin>>a>>b>>v;\n\t\ta--;\n\t\tb--;\n\t\tedge[i]={a,b,v};\n\t}\n\tauto wp_cost=[&](ll ES)->ld {\n\t\tif (ES==0)return 0;\n\t\tld sum=0;\n\t\tvec sum_v(0,0);\n\t\trep(j,M){\n\t\t\tif (~ES >> j & 1) continue;\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tsum+=1/(v*v);\n\t\t\tsum_v+=AP[a]/(v*v);\n\t\t}\n\t\tsum_v/=sum;\n\t\tld cost=0;\n\t\trep(j,M){\n\t\t\tif (~ES>>j&1)continue;\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tld d=(abs(sum_v-AP[a])/v);\n\t\t\tcost+=d*d;\n\t\t}\n\t\treturn cost;\n\t};\n\tauto cost_set=[&](ll S)->ld {\n\t\tset<ll> ES_set;\n\t\trep(j,M){\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tif (~S>>a&1)continue;\n\t\t\tcircle<ld> cent(AP[a],abs(AP[a]-AP[b]));\n\t\t\tvector<ld> cpt;\n\t\t\tset<ll> same;\n\t\t\tsame.insert(j);\n\t\t\trep(nj,M){\n\t\t\t\tif (j==nj)continue;\n\t\t\t\tauto [na,nb,nv]=edge[nj];\n\t\t\t\tif (~S>>na&1)continue;\n\t\t\t\tcircle<ld> cr(AP[na],abs(AP[na]-AP[nb]));\n\t\t\t\tif (num_of_common_tangents(cent,cr)==5){\n\t\t\t\t\tsame.insert(nj);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tauto ps=common_points(cent,cr);\n\t\t\t\tfor (auto pp:ps){\n\t\t\t\t\tcpt.push_back((pp-AP[a]).arg());\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(all(cpt));\n\t\t\tvector<vec> CP;\n\t\t\tif (cpt.size()==0){\n\t\t\t\tCP.push_back(AP[a]+vec(cent.radius,0));\n\t\t\t}\n\t\t\telse if (cpt.size()==1){\n\t\t\t\tld ar=cpt[0]+1.57;\n\t\t\t\tCP.push_back(AP[a]+rot(vec(cent.radius,0),ar));\n\t\t\t}\n\t\t\telse{\n\t\t\t\tll sz=cpt.size();\n\t\t\t\trep(k,sz){\n\t\t\t\t\tld ar=cpt[k]+cpt[(k+1)%sz];\n if (k==sz-1)ar+=2*acos(-1);\n\t\t\t\t\tar/=2;\n\t\t\t\t\tCP.push_back(AP[a]+rot(vec(cent.radius,0),ar));\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (vec p:CP){\n\t\t\t\tll ps=0;\n\t\t\t\trep(nj,M){\n\t\t\t\t\tif (same.contains(nj))continue;\n\t\t\t\t\tauto [na,nb,nv]=edge[nj];\n\t\t\t\t\tif (~S>>na&1)continue;\n\t\t\t\t\tif (abs(AP[na]-p)<abs(AP[na]-AP[nb])){\n\t\t\t\t\t\tps^=1LL<<nj;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tES_set.insert(ps);\n\t\t\t\tfor (ll nj:same){\n\t\t\t\t\tps^=1LL<<nj;\n\t\t\t\t}\n\t\t\t\tES_set.insert(ps);\n\t\t\t}\n\t\t}\n\t\tld mc=1e18;\n\t\tfor (ll es:ES_set){\n\t\t\tld c=0;\n\t\t\trep(j,M){\n\t\t\t\tauto [a,b,v]=edge[j];\n\t\t\t\tif (~S>>a&1)continue;\n\t\t\t\tif (es>>j&1)continue;\n\t\t\t\tld d=(abs(AP[a]-AP[b])/v);\n\t\t\t\tc+=d*d;\n\t\t\t}\n\t\t\tc+=wp_cost(es);\n\t\t\tchmin(mc,c);\n\t\t}\n\t\treturn mc;\n\t};\n\tauto pt_div=[&]()->ld {\n\t\tset<ll> div_set;\n\t\trep(i,N){\n\t\t\trep(ni,N){\n\t\t\t\tll S=0;\n\t\t\t\tvector<ll> ol;\n\t\t\t\trep(j,N){\n\t\t\t\t\tll cw=ccw(AP[i],AP[ni],AP[j]);\n\t\t\t\t\tif (cw==1){\n\t\t\t\t\t\tS^=1LL<<j;\n\t\t\t\t\t}\n\t\t\t\t\telse if (abs(cw)!=1){\n\t\t\t\t\t\tol.push_back(j);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif ((AP[i]-AP[ni]).x==0){\n\t\t\t\t\tsort(all(ol),[&](ll ii,ll jj){\n\t\t\t\t\t\treturn (AP[ii].y<AP[jj].y);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tsort(all(ol),[&](ll ii,ll jj){\n\t\t\t\t\t\treturn (AP[ii].x<AP[jj].x);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tfor (ll k:ol){\n\t\t\t\t\tdiv_set.insert(S);\n\t\t\t\t\tS^=1LL<<k;\n\t\t\t\t}\n\t\t\t\tfor (ll k:ol){\n\t\t\t\t\tdiv_set.insert(S);\n\t\t\t\t\tS^=1LL<<k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tld cost=1e18;\n\t\tll mask=1LL<<N;\n\t\tmask--;\n\t\tfor (ll s:div_set){\n\t\t\tld c=cost_set(s)+cost_set(s^mask);\n\t\t\tchmin(cost,c);\n\t\t}\n\t\treturn cost;\n\t};\n\tld ans=pt_div();\n\tans/=M;\n\tans=sqrt(ans);\n\tcout<<setprecision(15)<<fixed;\n\tcout<<ans<<endl;\n return;\n}\n\n\nint main() {\n while (true){\n\t\tll N,M;\n\t\tcin>>N>>M;\n\t\tif (N==0)break;\n solve(N,M);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3700, "memory_kb": 4096, "score_of_the_acc": -0.6167, "final_rank": 5 }, { "submission_id": "aoj_1614_10579459", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T>\nostream &operator<<(ostream &os, const vector<T> &a){\n\tif (a.empty()) return os;\n\tos << a.front();\n\tfor (auto e : a | views::drop(1)){\n\t\tos << ' ' << e;\n\t}\n\treturn os;\n}\nvoid dump(auto... vs) {\n\t((cout << vs << ' '), ...) << endl;\n}\n\n\n// #pragma once\n\n#include <type_traits>\n#include <concepts>\n#include <compare>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <iostream>\n#include <numeric>\n#include <cassert>\n#include <ranges>\n\n#if __has_include(<boost/multiprecision/cpp_bin_float.hpp>)\n\n#include<boost/multiprecision/cpp_bin_float.hpp>\n\n#endif\n\nnamespace float_geometry {\n\ntemplate<class T>\nstruct is_floating_point_number : std::bool_constant<false> {};\n\ntemplate<std::floating_point F>\nstruct is_floating_point_number<F> : std::bool_constant<true> {};\n\n#if __has_include(<boost/multiprecision/cpp_bin_float.hpp>)\n\ntemplate<unsigned D>\nstruct is_floating_point_number<boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<D>>> : std::bool_constant<true> {};\n\ntemplate<unsigned D>\nstruct is_floating_point_number<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<D>>> : std::bool_constant<true> {};\n\n#endif\n\ntemplate<class F>\nconcept FloatingPoint = is_floating_point_number<F>::value;\n\ntemplate<FloatingPoint Float>\nconstexpr Float EPS = Float{1e-8};\n\ntemplate<FloatingPoint Float>\nconstexpr int sign(const Float &x, Float eps = EPS<Float>){\n return (x < -eps ? -1 : x > eps ? 1 : 0);\n}\n\ntemplate<FloatingPoint Float>\nstruct point {\n Float x, y;\n point () {}\n point (Float _x, Float _y) : x(_x), y(_y) {}\n point &operator+=(const point &r){\n x += r.x;\n y += r.y;\n return *this;\n }\n point &operator-=(const point &r){\n x -= r.x;\n y -= r.y;\n return *this;\n }\n point operator+() const {\n return *this;\n }\n point operator-() const {\n x = -x;\n y = -y;\n return *this;\n }\n point &operator*=(const Float &a){\n x *= a;\n y *= a;\n return *this;\n }\n\tpoint &operator/=(const Float &a){\n x /= a;\n y /= a;\n return *this;\n }\n friend point operator+(const point& lhs, const point& rhs){\n return point(lhs) += rhs;\n }\n friend point operator-(const point& lhs, const point& rhs){\n return point(lhs) -= rhs;\n }\n friend point operator*(const point& lhs, const Float& a){\n return point(lhs) *= a;\n }\n friend point operator*(const Float& a, const point& rhs){\n return point(rhs) *= a;\n }\n\tfriend point operator/(const point& lhs, const Float& a){\n return point(lhs) /= a;\n }\n auto operator<=>(const point&) const = default;\n // friend bool operator<(const point &lhs, const point &rhs){\n // if (lhs.x == rhs.x) return lhs.y < rhs.y;\n // return lhs.x < rhs.x;\n // }\n // friend bool operator==(const point &lhs, const point &rhs){\n // return (lhs.x == rhs.x && lhs.y == rhs.y);\n // }\n friend std::ostream &operator<<(std::ostream &os, const point& p){\n return os << p.x << ' ' << p.y;\n }\n friend std::istream &operator>>(std::istream &is, point &a){\n Float _x, _y; is >> _x >> _y;\n a = point(_x, _y);\n return (is);\n }\n friend Float norm(const point &a) {\n return a.x*a.x + a.y*a.y;\n }\n friend Float dot(const point &a, const point &b){\n return a.x*b.x + a.y*b.y;\n }\n friend Float cross(const point &a, const point &b){\n return a.x*b.y - a.y*b.x;\n }\n\tfriend point rot90(const point &a){\n\t\treturn point(-a.y, a.x);\n\t}\n friend point rot(const point &a, const Float rad){\n using std::cos, std::sin;\n Float cosrad = cos(rad);\n Float sinrad = sin(rad);\n return point(a.x * cosrad - a.y * sinrad, a.x * sinrad + a.y * cosrad);\n }\n friend Float abs(const point &a){\n using std::sqrt;\n return sqrt(norm(a));\n }\n\n friend int quadrant_atan2(const point &a){\n // not origin point\n // ceil ( atan2(y,x) / (pi/2) )\n\t\tint signx = sign(a.x);\n\t\tint signy = sign(a.y);\n\t\tif (signx <= 0 && signy < 0) return -1;\n\t\tif (signx > 0 && signy <= 0) return 0;\n\t\tif (signx >= 0 && signy > 0) return 1;\n\t\tif (signx < 0 && signy >= 0) return 2;\n // origin point x == 0 && y == 0\n return 0;\n }\n\n friend int sign_cross(const point &a, const point &b){\n using std::abs;\n return sign(cross(a, b), EPS<Float> * (abs(a.x) + abs(a.y) + abs(b.x) + abs(b.y)));\n // return sign(cross(a, b), EPS<Float> * (abs<Float>(a.x) + abs<Float>(a.y) + abs<Float>(b.x) + abs<Float>(b.y)));\n }\n\n friend int sign_dot(const point &a, const point &b){\n using std::abs;\n return sign(dot(a, b), EPS<Float> * (abs(a.x) + abs(a.y) + abs(b.x) + abs(b.y)));\n // return sign(dot(a, b), EPS<Float> * (abs<Float>(a.x) + abs<Float>(a.y) + abs<Float>(b.x) + abs<Float>(b.y)));\n }\n\n\tfriend int ccw(const point &a, point b, point c){\n\t\tb -= a;\n\t\tc -= a;\n\t\tint signcr = sign_cross(b, c);\n\t\tif (signcr > 0){\n\t\t\t// ccw \n\t\t\t// c\n\t\t\t// a --> b \n\t\t\treturn 1;\n\t\t}\n\t\tif (signcr < 0){\n\t\t\t// cw\n\t\t\t// a --> b\n\t\t\t// c\n\t\t\treturn -1;\n\t\t}\n\t\tif (sign_dot(b, c) < 0){\n\t\t\t// c a --> b\n\t\t\treturn 2;\n\t\t}\n\t\tif (norm(b) < norm(c)){\n\t\t\t// a --> b c\n\t\t\treturn -2;\n\t\t}\n\t\t// a - c -> b\n\t\treturn 0;\n\t}\n friend bool has_common_point(const point &a, const point &b){\n return sign(a.x - b.x) == 0 && sign(a.y - b.y) == 0;\n }\n\n ld arg() const {\n std::complex<ld> a(x, y);\n return std::arg(a);\n }\n};\n\ntemplate<FloatingPoint Float>\nstruct arg_less {\n constexpr bool operator()(const point<Float> &l, const point<Float> &r){\n using std::atan2;\n return atan2(l.y, l.x) < atan2(r.y, r.x);\n }\n};\n\ntemplate<FloatingPoint Float>\nvoid arg_sort(std::vector<point<Float>> &a){\n\tstd::sort(a.begin(), a.end(), arg_less<Float>{});\n}\n\n\ntemplate<FloatingPoint Float>\nstd::vector<int> upper_convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tstd::vector<int> ids(a.size());\n std::iota(ids.begin(), ids.end(), 0);\n\tstd::sort(ids.begin(), ids.end(), [&](int l, int r){\n\t\treturn a[l] < a[r];\n\t});\n\tstd::vector<int> st(a.size());\n\tint ptr = 0;\n\tfor (int i : ids){\n\t\tif (ptr >= 1 && a[st[ptr-1]].x == a[i].x) ptr--;\n\t\twhile (ptr >= 2){\n\t\t\tint c = st[ptr-1];\n\t\t\tint p = st[ptr-2];\n\t\t\tif (sign_cross(a[i] - a[c], a[c] - a[p]) > 0){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tptr--;\n\t\t}\n\t\tst[ptr++] = i;\n\t}\n\tst.resize(ptr);\n\treturn st;\n}\n\ntemplate<FloatingPoint Float>\nstd::vector<int> lower_convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tstd::vector<int> ids(a.size());\n std::iota(ids.begin(), ids.end(), 0);\n\tstd::sort(ids.begin(), ids.end(), [&](int l, int r){\n\t\treturn a[l] < a[r];\n\t});\n\tstd::vector<int> st(a.size());\n\tint ptr = 0;\n\tfor (int i : ids){\n\t\tif (ptr >= 1 && a[st[ptr-1]].x == a[i].x) continue;\n\t\twhile (ptr >= 2){\n\t\t\tint c = st[ptr-1];\n\t\t\tint p = st[ptr-2];\n\t\t\tif (sign_cross(a[c] - a[p], a[i] - a[c]) > 0){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tptr--;\n\t\t}\n\t\tst[ptr++] = i;\n\t}\n\tst.resize(ptr);\n\treturn st;\n}\n\ntemplate<FloatingPoint Float>\nstd::vector<int> convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tauto upper = upper_convex_hull_index(a);\n\tauto lower = lower_convex_hull_index(a);\n\tif (upper.size() == 1u){\n\t\t// lower.size() == 1u\n\t\tif (a[upper.front()] == a[lower.front()]){\n\t\t\treturn {upper.front()};\n\t\t}\n\t\treturn {lower.front(), upper.front()};\n\t}\n\tif (a[upper.back()] == a[lower.back()]){\n\t\tlower.pop_back();\n\t}\n\tlower.insert(lower.end(), upper.rbegin(), upper.rend());\n\tif (a[upper.front()] == a[lower.front()]) lower.pop_back();\n\treturn lower;\n}\n\n\ntemplate<FloatingPoint Float>\nstruct line {\n\tpoint<Float> end0, end1;\n line () {}\n\tline (const point<Float> &_end0, const point<Float> &_end1) : end0(_end0), end1(_end1) {\n\t\tassert(end0 != end1);\n\t}\n\t// auto operator<=>(const line &) const = default;\n\tpoint<Float> direction() const {\n\t\treturn end1 - end0;\n\t}\n friend bool is_parallel(const line &a, const line &b){\n return sign_cross(a.direction(), b.direction()) == 0;\n }\n friend bool is_orthgonal(const line &a, const line &b){\n return sign_dot(a.direction(), b.direction()) == 0;\n }\n\tfriend bool has_common_point(const line &a, const point<Float> &b){\n\t\treturn sign(cross(a.direction(), b - a.end0)) == 0;\n\t}\n\tfriend bool has_common_point(const line &a, const line &b){\n\t\treturn !is_parallel(a, b) || has_common_point(a, b.end0);\n\t}\n\tfriend point<Float> common_point(const line &a, const line &b){\n\t\t// assert(has_common_point(a, b));\n\t\tif (is_parallel(a, b)){\n\t\t\treturn a.end0;\n\t\t}\n\t\treturn a.end0 + a.direction() * cross(b.end0 - a.end0, b.end1 - b.end0) / cross(a.direction(), b.direction());\n\t}\n\tfriend point<Float> projection(const line &a, const point<Float> &b){\n\t\tauto dir = a.direction();\n\t\treturn a.end0 + dir * (dot(dir, b - a.end0) / norm(dir));\n\t}\n\tfriend point<Float> reflection(const line &a, const point<Float> &b){\n\t\tauto prj = projection(a, b);\n\t\treturn prj + prj - b;\n\t}\n friend line<Float> perpendicular_bisector(const point<Float> &p1, const point<Float> &p2){\n auto ctr = (p1 + p2) / Float{2};\n auto dir = rot90(p2 - p1);\n return line<Float>(ctr, ctr + dir);\n }\n};\n\ntemplate<FloatingPoint Float>\nline<Float> perpendicular_bisector(const point<Float> &p1, const point<Float> &p2){\n auto ctr = (p1 + p2) / Float{2};\n auto dir = rot90(p2 - p1);\n return line<Float>(ctr, ctr + dir);\n}\n\n\ntemplate<FloatingPoint Float>\nstruct segment {\n\tpoint<Float> end0, end1;\n segment () {}\n\tsegment (const point<Float> &_end0, const point<Float> &_end1) : end0(_end0), end1(_end1) {\n\t\tassert(end0 != end1);\n\t}\n\t// auto operator<=>(const segment &) const = default;\n\tpoint<Float> direction() const {\n\t\treturn end1 - end0;\n\t}\n friend bool is_parallel(const segment &a, const segment &b){\n return sign_cross(a.direction(), b.direction()) == 0;\n }\n friend bool is_orthgonal(const segment &a, const segment &b){\n return sign_dot(a.direction(), b.direction()) == 0;\n }\n\tfriend bool has_common_point(const segment &a, const segment &b){\n\t\treturn ccw(a.end0, a.end1, b.end0) * ccw(a.end0, a.end1, b.end1) <= 0\n\t\t\t&& ccw(b.end0, b.end1, a.end0) * ccw(b.end0, b.end1, a.end1) <= 0;\n\t}\n\tfriend bool has_common_point(const segment &a, const point<Float> &b){\n\t\treturn ccw(a.end0, a.end1, b) == 0;\n\t}\n\tfriend point<Float> common_point(const segment &a, const segment &b){\n\t\t// assert(has_common_point(a, b));\n\t\tif (is_parallel(a, b)){\n\t\t\tif (has_common_point(a, b.end0)){\n\t\t\t\treturn b.end0;\n\t\t\t}\n\t\t\tif (has_common_point(a, b.end1)){\n\t\t\t\treturn b.end1;\n\t\t\t}\n\t\t\treturn a.end0;\n\t\t}\n\t\treturn a.end0 + a.direction() * cross(b.end0 - a.end0, b.end1 - b.end0) / cross(a.direction(), b.direction());\n\t}\n\tline<Float> as_line() const {\n\t\treturn line<Float>(end0, end1);\n\t}\n};\n\ntemplate<FloatingPoint Float>\nstruct circle {\n point<Float> center;\n Float radius;\n circle () {}\n circle (const point<Float> &_center, const Float &_radius) : center(_center), radius(_radius) {\n assert(sign(radius) >= 0);\n if (radius < Float{}){\n radius = Float{};\n }\n }\n friend int num_of_common_tangents(const circle &a, const circle &b){\n if (has_common_point(a.center, b.center) && sign(a.radius - b.radius) == 0){\n return 5; // infty\n }\n Float d = abs(a.center - b.center);\n Float r1 = a.radius, r2 = b.radius;\n int sumsign = sign(d - (r1 + r2));\n if (sumsign >= 0) return sumsign + 3;\n using std::abs;\n int diffsign = sign(d - abs(r1 - r2));\n return diffsign + 1;\n }\n friend bool has_common_point(const circle &a, const circle &b){\n int cnt = num_of_common_tangents(a, b);\n return cnt == 1 || cnt == 2 || cnt == 3 || cnt == 5;\n }\n friend bool has_common_point(const circle &c, const line<Float> &l){\n point<Float> p = reflection(l, c.center);\n Float d = abs(c.center, p);\n return sign(c.radius - d) >= 0;\n }\n friend bool has_common_point(const circle &c, const segment<Float> &s){\n return std::ranges::any_of(common_points(c, s.as_line()), [&](auto p){\n return has_common_point(s, p);\n });\n }\n friend bool has_common_point(const circle &c, const point<Float> &p){\n return sign(abs(p - c.center) - c.radius) == 0;\n }\n friend std::vector<point<Float>> common_points(const circle &a, const circle &b){\n int cnt = num_of_common_tangents(a, b);\n assert(cnt != 5); // cannot be same circle\n if (cnt == 0 || cnt == 4){\n return {};\n }\n Float d = abs(a.center - b.center);\n Float x = (d*d + a.radius*a.radius - b.radius*b.radius) / (d + d);\n point<Float> p = a.center + (b.center - a.center) * x / d;\n if (cnt == 1 || cnt == 3 || sign(a.radius - x) <= 0){\n return {p};\n }\n point<Float> v = rot90(b.center - a.center);\n using std::sqrt;\n v *= sqrt(a.radius*a.radius - x*x) / abs(v);\n return {p + v, p - v};\n }\n friend std::vector<point<Float>> common_points(const circle &c, const line<Float> &l) {\n point<Float> p = projection(l, c.center);\n Float d = abs(c.center - p);\n int cnt = sign(c.radius - d) + 1;\n if (cnt == 0){\n return {};\n }\n if (cnt == 1){\n return {p};\n }\n point<Float> v = l.direction();\n using std::sqrt;\n v *= sqrt(c.radius*c.radius - d*d) / abs(v);\n return {p + v, p - v};\n }\n};\n\n} // float_geometry\n\nusing namespace float_geometry;\nusing vec=point<ld>;\n\nvoid solve(ll N,ll M) {\n\tvector<vec> AP(N);\n\trep(i,N){\n\t\tcin>>AP[i];\n\t}\n\tvector<tuple<ll,ll,ld>> edge(M);\n\trep(i,M){\n\t\tll a,b;\n\t\tld v;\n\t\tcin>>a>>b>>v;\n\t\ta--;\n\t\tb--;\n\t\tedge[i]={a,b,v};\n\t}\n\tauto wp_cost=[&](ll ES)->ld {\n\t\tif (ES==0)return 0;\n\t\tld sum=0;\n\t\tvec sum_v(0,0);\n\t\trep(j,M){\n\t\t\tif (~ES >> j & 1) continue;\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tsum+=1/(v*v);\n\t\t\tsum_v+=AP[a]/(v*v);\n\t\t}\n\t\tsum_v/=sum;\n\t\tld cost=0;\n\t\trep(j,M){\n\t\t\tif (~ES>>j&1)continue;\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tld d=(abs(sum_v-AP[a])/v);\n\t\t\tcost+=d*d;\n\t\t}\n\t\treturn cost;\n\t};\n\tauto cost_set=[&](ll S)->ld {\n\t\tset<ll> ES_set;\n\t\trep(j,M){\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tif (~S>>a&1)continue;\n\t\t\tcircle<ld> cent(AP[a],abs(AP[a]-AP[b]));\n\t\t\tvector<ld> cpt;\n\t\t\tset<ll> same;\n\t\t\tsame.insert(j);\n\t\t\trep(nj,M){\n\t\t\t\tif (j==nj)continue;\n\t\t\t\tauto [na,nb,nv]=edge[nj];\n\t\t\t\tif (~S>>na&1)continue;\n\t\t\t\tcircle<ld> cr(AP[na],abs(AP[na]-AP[nb]));\n\t\t\t\tif (num_of_common_tangents(cent,cr)==5){\n\t\t\t\t\tsame.insert(nj);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tauto ps=common_points(cent,cr);\n\t\t\t\tfor (auto pp:ps){\n\t\t\t\t\tcpt.push_back((pp-AP[a]).arg());\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(all(cpt));\n\t\t\tvector<vec> CP;\n\t\t\tif (cpt.size()==0){\n\t\t\t\tCP.push_back(AP[a]+vec(cent.radius,0));\n\t\t\t}\n\t\t\telse if (cpt.size()==1){\n\t\t\t\tld ar=cpt[0]+1.57;\n\t\t\t\tCP.push_back(AP[a]+rot(vec(cent.radius,0),ar));\n\t\t\t}\n\t\t\telse{\n\t\t\t\tll sz=cpt.size();\n\t\t\t\trep(k,sz){\n\t\t\t\t\tld ar=cpt[k]+cpt[(k+1)%sz];\n\t\t\t\t\tar/=2;\n\t\t\t\t\tCP.push_back(AP[a]+rot(vec(cent.radius,0),ar));\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (vec p:CP){\n\t\t\t\tll ps=0;\n\t\t\t\trep(nj,M){\n\t\t\t\t\tif (same.contains(nj))continue;\n\t\t\t\t\tauto [na,nb,nv]=edge[nj];\n\t\t\t\t\tif (~S>>na&1)continue;\n\t\t\t\t\tif (abs(AP[na]-p)<abs(AP[na]-AP[nb])){\n\t\t\t\t\t\tps^=1LL<<nj;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tES_set.insert(ps);\n\t\t\t\tfor (ll nj:same){\n\t\t\t\t\tps^=1LL<<nj;\n\t\t\t\t}\n\t\t\t\tES_set.insert(ps);\n\t\t\t}\n\t\t}\n\t\tld mc=1e18;\n\t\tfor (ll es:ES_set){\n\t\t\tld c=0;\n\t\t\trep(j,M){\n\t\t\t\tauto [a,b,v]=edge[j];\n\t\t\t\tif (~S>>a&1)continue;\n\t\t\t\tif (es>>j&1)continue;\n\t\t\t\tld d=(abs(AP[a]-AP[b])/v);\n\t\t\t\tc+=d*d;\n\t\t\t}\n\t\t\tc+=wp_cost(es);\n\t\t\tchmin(mc,c);\n\t\t}\n\t\treturn mc;\n\t};\n\tauto pt_div=[&]()->ld {\n\t\tset<ll> div_set;\n\t\trep(i,N){\n\t\t\trep(ni,N){\n\t\t\t\tll S=0;\n\t\t\t\tvector<ll> ol;\n\t\t\t\trep(j,N){\n\t\t\t\t\tll cw=ccw(AP[i],AP[ni],AP[j]);\n\t\t\t\t\tif (cw==1){\n\t\t\t\t\t\tS^=1LL<<j;\n\t\t\t\t\t}\n\t\t\t\t\telse if (abs(cw)!=1){\n\t\t\t\t\t\tol.push_back(j);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif ((AP[i]-AP[ni]).x==0){\n\t\t\t\t\tsort(all(ol),[&](ll ii,ll jj){\n\t\t\t\t\t\treturn (AP[ii].y<AP[jj].y);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tsort(all(ol),[&](ll ii,ll jj){\n\t\t\t\t\t\treturn (AP[ii].x<AP[jj].x);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tfor (ll k:ol){\n\t\t\t\t\tdiv_set.insert(S);\n\t\t\t\t\tS^=1LL<<k;\n\t\t\t\t}\n\t\t\t\tfor (ll k:ol){\n\t\t\t\t\tdiv_set.insert(S);\n\t\t\t\t\tS^=1LL<<k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tld cost=1e18;\n\t\tll mask=1LL<<N;\n\t\tmask--;\n\t\tfor (ll s:div_set){\n\t\t\tld c=cost_set(s)+cost_set(s^mask);\n\t\t\tchmin(cost,c);\n\t\t}\n\t\treturn cost;\n\t};\n\tld ans=pt_div();\n\tans/=M;\n\tans=sqrt(ans);\n\tcout<<setprecision(15)<<fixed;\n\tcout<<ans<<endl;\n return;\n}\n\n\nint main() {\n while (true){\n\t\tll N,M;\n\t\tcin>>N>>M;\n\t\tif (N==0)break;\n solve(N,M);\n }\n return 0;\n}", "accuracy": 0.5, "time_ms": 3640, "memory_kb": 4096, "score_of_the_acc": -0.6044, "final_rank": 13 }, { "submission_id": "aoj_1614_10579455", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }\nll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }\ntemplate<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T>\nostream &operator<<(ostream &os, const vector<T> &a){\n\tif (a.empty()) return os;\n\tos << a.front();\n\tfor (auto e : a | views::drop(1)){\n\t\tos << ' ' << e;\n\t}\n\treturn os;\n}\nvoid dump(auto... vs) {\n\t((cout << vs << ' '), ...) << endl;\n}\n\n\n// #pragma once\n\n#include <type_traits>\n#include <concepts>\n#include <compare>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <iostream>\n#include <numeric>\n#include <cassert>\n#include <ranges>\n\n#if __has_include(<boost/multiprecision/cpp_bin_float.hpp>)\n\n#include<boost/multiprecision/cpp_bin_float.hpp>\n\n#endif\n\nnamespace float_geometry {\n\ntemplate<class T>\nstruct is_floating_point_number : std::bool_constant<false> {};\n\ntemplate<std::floating_point F>\nstruct is_floating_point_number<F> : std::bool_constant<true> {};\n\n#if __has_include(<boost/multiprecision/cpp_bin_float.hpp>)\n\ntemplate<unsigned D>\nstruct is_floating_point_number<boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<D>>> : std::bool_constant<true> {};\n\ntemplate<unsigned D>\nstruct is_floating_point_number<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<D>>> : std::bool_constant<true> {};\n\n#endif\n\ntemplate<class F>\nconcept FloatingPoint = is_floating_point_number<F>::value;\n\ntemplate<FloatingPoint Float>\nconstexpr Float EPS = Float{1e-9};\n\ntemplate<FloatingPoint Float>\nconstexpr int sign(const Float &x, Float eps = EPS<Float>){\n return (x < -eps ? -1 : x > eps ? 1 : 0);\n}\n\ntemplate<FloatingPoint Float>\nstruct point {\n Float x, y;\n point () {}\n point (Float _x, Float _y) : x(_x), y(_y) {}\n point &operator+=(const point &r){\n x += r.x;\n y += r.y;\n return *this;\n }\n point &operator-=(const point &r){\n x -= r.x;\n y -= r.y;\n return *this;\n }\n point operator+() const {\n return *this;\n }\n point operator-() const {\n x = -x;\n y = -y;\n return *this;\n }\n point &operator*=(const Float &a){\n x *= a;\n y *= a;\n return *this;\n }\n\tpoint &operator/=(const Float &a){\n x /= a;\n y /= a;\n return *this;\n }\n friend point operator+(const point& lhs, const point& rhs){\n return point(lhs) += rhs;\n }\n friend point operator-(const point& lhs, const point& rhs){\n return point(lhs) -= rhs;\n }\n friend point operator*(const point& lhs, const Float& a){\n return point(lhs) *= a;\n }\n friend point operator*(const Float& a, const point& rhs){\n return point(rhs) *= a;\n }\n\tfriend point operator/(const point& lhs, const Float& a){\n return point(lhs) /= a;\n }\n auto operator<=>(const point&) const = default;\n // friend bool operator<(const point &lhs, const point &rhs){\n // if (lhs.x == rhs.x) return lhs.y < rhs.y;\n // return lhs.x < rhs.x;\n // }\n // friend bool operator==(const point &lhs, const point &rhs){\n // return (lhs.x == rhs.x && lhs.y == rhs.y);\n // }\n friend std::ostream &operator<<(std::ostream &os, const point& p){\n return os << p.x << ' ' << p.y;\n }\n friend std::istream &operator>>(std::istream &is, point &a){\n Float _x, _y; is >> _x >> _y;\n a = point(_x, _y);\n return (is);\n }\n friend Float norm(const point &a) {\n return a.x*a.x + a.y*a.y;\n }\n friend Float dot(const point &a, const point &b){\n return a.x*b.x + a.y*b.y;\n }\n friend Float cross(const point &a, const point &b){\n return a.x*b.y - a.y*b.x;\n }\n\tfriend point rot90(const point &a){\n\t\treturn point(-a.y, a.x);\n\t}\n friend point rot(const point &a, const Float rad){\n using std::cos, std::sin;\n Float cosrad = cos(rad);\n Float sinrad = sin(rad);\n return point(a.x * cosrad - a.y * sinrad, a.x * sinrad + a.y * cosrad);\n }\n friend Float abs(const point &a){\n using std::sqrt;\n return sqrt(norm(a));\n }\n\n friend int quadrant_atan2(const point &a){\n // not origin point\n // ceil ( atan2(y,x) / (pi/2) )\n\t\tint signx = sign(a.x);\n\t\tint signy = sign(a.y);\n\t\tif (signx <= 0 && signy < 0) return -1;\n\t\tif (signx > 0 && signy <= 0) return 0;\n\t\tif (signx >= 0 && signy > 0) return 1;\n\t\tif (signx < 0 && signy >= 0) return 2;\n // origin point x == 0 && y == 0\n return 0;\n }\n\n friend int sign_cross(const point &a, const point &b){\n using std::abs;\n return sign(cross(a, b), EPS<Float> * (abs(a.x) + abs(a.y) + abs(b.x) + abs(b.y)));\n // return sign(cross(a, b), EPS<Float> * (abs<Float>(a.x) + abs<Float>(a.y) + abs<Float>(b.x) + abs<Float>(b.y)));\n }\n\n friend int sign_dot(const point &a, const point &b){\n using std::abs;\n return sign(dot(a, b), EPS<Float> * (abs(a.x) + abs(a.y) + abs(b.x) + abs(b.y)));\n // return sign(dot(a, b), EPS<Float> * (abs<Float>(a.x) + abs<Float>(a.y) + abs<Float>(b.x) + abs<Float>(b.y)));\n }\n\n\tfriend int ccw(const point &a, point b, point c){\n\t\tb -= a;\n\t\tc -= a;\n\t\tint signcr = sign_cross(b, c);\n\t\tif (signcr > 0){\n\t\t\t// ccw \n\t\t\t// c\n\t\t\t// a --> b \n\t\t\treturn 1;\n\t\t}\n\t\tif (signcr < 0){\n\t\t\t// cw\n\t\t\t// a --> b\n\t\t\t// c\n\t\t\treturn -1;\n\t\t}\n\t\tif (sign_dot(b, c) < 0){\n\t\t\t// c a --> b\n\t\t\treturn 2;\n\t\t}\n\t\tif (norm(b) < norm(c)){\n\t\t\t// a --> b c\n\t\t\treturn -2;\n\t\t}\n\t\t// a - c -> b\n\t\treturn 0;\n\t}\n friend bool has_common_point(const point &a, const point &b){\n return sign(a.x - b.x) == 0 && sign(a.y - b.y) == 0;\n }\n\n ld arg() const {\n std::complex<ld> a(x, y);\n return std::arg(a);\n }\n};\n\ntemplate<FloatingPoint Float>\nstruct arg_less {\n constexpr bool operator()(const point<Float> &l, const point<Float> &r){\n using std::atan2;\n return atan2(l.y, l.x) < atan2(r.y, r.x);\n }\n};\n\ntemplate<FloatingPoint Float>\nvoid arg_sort(std::vector<point<Float>> &a){\n\tstd::sort(a.begin(), a.end(), arg_less<Float>{});\n}\n\n\ntemplate<FloatingPoint Float>\nstd::vector<int> upper_convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tstd::vector<int> ids(a.size());\n std::iota(ids.begin(), ids.end(), 0);\n\tstd::sort(ids.begin(), ids.end(), [&](int l, int r){\n\t\treturn a[l] < a[r];\n\t});\n\tstd::vector<int> st(a.size());\n\tint ptr = 0;\n\tfor (int i : ids){\n\t\tif (ptr >= 1 && a[st[ptr-1]].x == a[i].x) ptr--;\n\t\twhile (ptr >= 2){\n\t\t\tint c = st[ptr-1];\n\t\t\tint p = st[ptr-2];\n\t\t\tif (sign_cross(a[i] - a[c], a[c] - a[p]) > 0){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tptr--;\n\t\t}\n\t\tst[ptr++] = i;\n\t}\n\tst.resize(ptr);\n\treturn st;\n}\n\ntemplate<FloatingPoint Float>\nstd::vector<int> lower_convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tstd::vector<int> ids(a.size());\n std::iota(ids.begin(), ids.end(), 0);\n\tstd::sort(ids.begin(), ids.end(), [&](int l, int r){\n\t\treturn a[l] < a[r];\n\t});\n\tstd::vector<int> st(a.size());\n\tint ptr = 0;\n\tfor (int i : ids){\n\t\tif (ptr >= 1 && a[st[ptr-1]].x == a[i].x) continue;\n\t\twhile (ptr >= 2){\n\t\t\tint c = st[ptr-1];\n\t\t\tint p = st[ptr-2];\n\t\t\tif (sign_cross(a[c] - a[p], a[i] - a[c]) > 0){\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tptr--;\n\t\t}\n\t\tst[ptr++] = i;\n\t}\n\tst.resize(ptr);\n\treturn st;\n}\n\ntemplate<FloatingPoint Float>\nstd::vector<int> convex_hull_index(const std::vector<point<Float>> &a){\n\tif (a.empty()) return {};\n\tauto upper = upper_convex_hull_index(a);\n\tauto lower = lower_convex_hull_index(a);\n\tif (upper.size() == 1u){\n\t\t// lower.size() == 1u\n\t\tif (a[upper.front()] == a[lower.front()]){\n\t\t\treturn {upper.front()};\n\t\t}\n\t\treturn {lower.front(), upper.front()};\n\t}\n\tif (a[upper.back()] == a[lower.back()]){\n\t\tlower.pop_back();\n\t}\n\tlower.insert(lower.end(), upper.rbegin(), upper.rend());\n\tif (a[upper.front()] == a[lower.front()]) lower.pop_back();\n\treturn lower;\n}\n\n\ntemplate<FloatingPoint Float>\nstruct line {\n\tpoint<Float> end0, end1;\n line () {}\n\tline (const point<Float> &_end0, const point<Float> &_end1) : end0(_end0), end1(_end1) {\n\t\tassert(end0 != end1);\n\t}\n\t// auto operator<=>(const line &) const = default;\n\tpoint<Float> direction() const {\n\t\treturn end1 - end0;\n\t}\n friend bool is_parallel(const line &a, const line &b){\n return sign_cross(a.direction(), b.direction()) == 0;\n }\n friend bool is_orthgonal(const line &a, const line &b){\n return sign_dot(a.direction(), b.direction()) == 0;\n }\n\tfriend bool has_common_point(const line &a, const point<Float> &b){\n\t\treturn sign(cross(a.direction(), b - a.end0)) == 0;\n\t}\n\tfriend bool has_common_point(const line &a, const line &b){\n\t\treturn !is_parallel(a, b) || has_common_point(a, b.end0);\n\t}\n\tfriend point<Float> common_point(const line &a, const line &b){\n\t\t// assert(has_common_point(a, b));\n\t\tif (is_parallel(a, b)){\n\t\t\treturn a.end0;\n\t\t}\n\t\treturn a.end0 + a.direction() * cross(b.end0 - a.end0, b.end1 - b.end0) / cross(a.direction(), b.direction());\n\t}\n\tfriend point<Float> projection(const line &a, const point<Float> &b){\n\t\tauto dir = a.direction();\n\t\treturn a.end0 + dir * (dot(dir, b - a.end0) / norm(dir));\n\t}\n\tfriend point<Float> reflection(const line &a, const point<Float> &b){\n\t\tauto prj = projection(a, b);\n\t\treturn prj + prj - b;\n\t}\n friend line<Float> perpendicular_bisector(const point<Float> &p1, const point<Float> &p2){\n auto ctr = (p1 + p2) / Float{2};\n auto dir = rot90(p2 - p1);\n return line<Float>(ctr, ctr + dir);\n }\n};\n\ntemplate<FloatingPoint Float>\nline<Float> perpendicular_bisector(const point<Float> &p1, const point<Float> &p2){\n auto ctr = (p1 + p2) / Float{2};\n auto dir = rot90(p2 - p1);\n return line<Float>(ctr, ctr + dir);\n}\n\n\ntemplate<FloatingPoint Float>\nstruct segment {\n\tpoint<Float> end0, end1;\n segment () {}\n\tsegment (const point<Float> &_end0, const point<Float> &_end1) : end0(_end0), end1(_end1) {\n\t\tassert(end0 != end1);\n\t}\n\t// auto operator<=>(const segment &) const = default;\n\tpoint<Float> direction() const {\n\t\treturn end1 - end0;\n\t}\n friend bool is_parallel(const segment &a, const segment &b){\n return sign_cross(a.direction(), b.direction()) == 0;\n }\n friend bool is_orthgonal(const segment &a, const segment &b){\n return sign_dot(a.direction(), b.direction()) == 0;\n }\n\tfriend bool has_common_point(const segment &a, const segment &b){\n\t\treturn ccw(a.end0, a.end1, b.end0) * ccw(a.end0, a.end1, b.end1) <= 0\n\t\t\t&& ccw(b.end0, b.end1, a.end0) * ccw(b.end0, b.end1, a.end1) <= 0;\n\t}\n\tfriend bool has_common_point(const segment &a, const point<Float> &b){\n\t\treturn ccw(a.end0, a.end1, b) == 0;\n\t}\n\tfriend point<Float> common_point(const segment &a, const segment &b){\n\t\t// assert(has_common_point(a, b));\n\t\tif (is_parallel(a, b)){\n\t\t\tif (has_common_point(a, b.end0)){\n\t\t\t\treturn b.end0;\n\t\t\t}\n\t\t\tif (has_common_point(a, b.end1)){\n\t\t\t\treturn b.end1;\n\t\t\t}\n\t\t\treturn a.end0;\n\t\t}\n\t\treturn a.end0 + a.direction() * cross(b.end0 - a.end0, b.end1 - b.end0) / cross(a.direction(), b.direction());\n\t}\n\tline<Float> as_line() const {\n\t\treturn line<Float>(end0, end1);\n\t}\n};\n\ntemplate<FloatingPoint Float>\nstruct circle {\n point<Float> center;\n Float radius;\n circle () {}\n circle (const point<Float> &_center, const Float &_radius) : center(_center), radius(_radius) {\n assert(sign(radius) >= 0);\n if (radius < Float{}){\n radius = Float{};\n }\n }\n friend int num_of_common_tangents(const circle &a, const circle &b){\n if (has_common_point(a.center, b.center) && sign(a.radius - b.radius) == 0){\n return 5; // infty\n }\n Float d = abs(a.center - b.center);\n Float r1 = a.radius, r2 = b.radius;\n int sumsign = sign(d - (r1 + r2));\n if (sumsign >= 0) return sumsign + 3;\n using std::abs;\n int diffsign = sign(d - abs(r1 - r2));\n return diffsign + 1;\n }\n friend bool has_common_point(const circle &a, const circle &b){\n int cnt = num_of_common_tangents(a, b);\n return cnt == 1 || cnt == 2 || cnt == 3 || cnt == 5;\n }\n friend bool has_common_point(const circle &c, const line<Float> &l){\n point<Float> p = reflection(l, c.center);\n Float d = abs(c.center, p);\n return sign(c.radius - d) >= 0;\n }\n friend bool has_common_point(const circle &c, const segment<Float> &s){\n return std::ranges::any_of(common_points(c, s.as_line()), [&](auto p){\n return has_common_point(s, p);\n });\n }\n friend bool has_common_point(const circle &c, const point<Float> &p){\n return sign(abs(p - c.center) - c.radius) == 0;\n }\n friend std::vector<point<Float>> common_points(const circle &a, const circle &b){\n int cnt = num_of_common_tangents(a, b);\n assert(cnt != 5); // cannot be same circle\n if (cnt == 0 || cnt == 4){\n return {};\n }\n Float d = abs(a.center - b.center);\n Float x = (d*d + a.radius*a.radius - b.radius*b.radius) / (d + d);\n point<Float> p = a.center + (b.center - a.center) * x / d;\n if (cnt == 1 || cnt == 3 || sign(a.radius - x) <= 0){\n return {p};\n }\n point<Float> v = rot90(b.center - a.center);\n using std::sqrt;\n v *= sqrt(a.radius*a.radius - x*x) / abs(v);\n return {p + v, p - v};\n }\n friend std::vector<point<Float>> common_points(const circle &c, const line<Float> &l) {\n point<Float> p = projection(l, c.center);\n Float d = abs(c.center - p);\n int cnt = sign(c.radius - d) + 1;\n if (cnt == 0){\n return {};\n }\n if (cnt == 1){\n return {p};\n }\n point<Float> v = l.direction();\n using std::sqrt;\n v *= sqrt(c.radius*c.radius - d*d) / abs(v);\n return {p + v, p - v};\n }\n};\n\n} // float_geometry\n\nusing namespace float_geometry;\nusing vec=point<ld>;\n\nvoid solve(ll N,ll M) {\n\tvector<vec> AP(N);\n\trep(i,N){\n\t\tcin>>AP[i];\n\t}\n\tvector<tuple<ll,ll,ld>> edge(M);\n\trep(i,M){\n\t\tll a,b;\n\t\tld v;\n\t\tcin>>a>>b>>v;\n\t\ta--;\n\t\tb--;\n\t\tedge[i]={a,b,v};\n\t}\n\tauto wp_cost=[&](ll ES)->ld {\n\t\tif (ES==0)return 0;\n\t\tld sum=0;\n\t\tvec sum_v(0,0);\n\t\trep(j,M){\n\t\t\tif (~ES >> j & 1) continue;\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tsum+=1/(v*v);\n\t\t\tsum_v+=AP[a]/(v*v);\n\t\t}\n\t\tsum_v/=sum;\n\t\tld cost=0;\n\t\trep(j,M){\n\t\t\tif (~ES>>j&1)continue;\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tld d=(abs(sum_v-AP[a])/v);\n\t\t\tcost+=d*d;\n\t\t}\n\t\treturn cost;\n\t};\n\tauto cost_set=[&](ll S)->ld {\n\t\tif (S == 0) return 0;\n\t\tset<ll> ES_set;\n\t\trep(j,M){\n\t\t\tauto [a,b,v]=edge[j];\n\t\t\tif (~S>>a&1)continue;\n\t\t\tcircle<ld> cent(AP[a],abs(AP[a]-AP[b]));\n\t\t\tvector<ld> cpt;\n\t\t\tset<ll> same;\n\t\t\tsame.insert(j);\n\t\t\trep(nj,M){\n\t\t\t\tif (j==nj)continue;\n\t\t\t\tauto [na,nb,nv]=edge[nj];\n\t\t\t\tif (~S>>na&1)continue;\n\t\t\t\tcircle<ld> cr(AP[na],abs(AP[na]-AP[nb]));\n\t\t\t\tif (num_of_common_tangents(cent,cr)==5){\n\t\t\t\t\tsame.insert(nj);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tauto ps=common_points(cent,cr);\n\t\t\t\tfor (auto pp:ps){\n\t\t\t\t\tcpt.push_back((pp-AP[a]).arg());\n\t\t\t\t}\n\t\t\t}\n\t\t\tsort(all(cpt));\n\t\t\tvector<vec> CP;\n\t\t\tif (cpt.size()==0){\n\t\t\t\tCP.push_back(AP[a]+vec(cent.radius,0));\n\t\t\t}\n\t\t\telse if (cpt.size()==1){\n\t\t\t\tld ar=cpt[0]+acos(-1);\n\t\t\t\tCP.push_back(AP[a]+rot(vec(cent.radius,0),ar));\n\t\t\t}\n\t\t\telse{\n\t\t\t\tll sz=cpt.size();\n\t\t\t\trep(k,sz){\n\t\t\t\t\tld ar=cpt[k]+(k == sz-1 ? cpt[0]+acos(-1)*2 : cpt[k+1]);\n\t\t\t\t\tar/=2;\n\t\t\t\t\tCP.push_back(AP[a]+rot(vec(cent.radius,0),ar));\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (vec p:CP){\n\t\t\t\tll ps=0;\n\t\t\t\trep(nj,M){\n\t\t\t\t\tif (same.contains(nj))continue;\n\t\t\t\t\tauto [na,nb,nv]=edge[nj];\n\t\t\t\t\tif (~S>>na&1)continue;\n\t\t\t\t\tif (abs(AP[na]-p)<abs(AP[na]-AP[nb])){\n\t\t\t\t\t\tps^=1LL<<nj;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tES_set.insert(ps);\n\t\t\t\tfor (ll nj:same){\n\t\t\t\t\tps^=1LL<<nj;\n\t\t\t\t}\n\t\t\t\tES_set.insert(ps);\n\t\t\t}\n\t\t}\n\t\tld mc=1e18;\n\t\tfor (ll es:ES_set){\n\t\t\tld c=0;\n\t\t\trep(j,M){\n\t\t\t\tauto [a,b,v]=edge[j];\n\t\t\t\tif (~S>>a&1)continue;\n\t\t\t\tif (es>>j&1)continue;\n\t\t\t\tld d=(abs(AP[a]-AP[b])/v);\n\t\t\t\tc+=d*d;\n\t\t\t}\n\t\t\tc+=wp_cost(es);\n\t\t\tchmin(mc,c);\n\t\t}\n\t\treturn mc;\n\t};\n\tauto pt_div=[&]()->ld {\n\t\tset<ll> div_set;\n\t\trep(i,N){\n\t\t\trep(ni,N){\n\t\t\t\tll S=0;\n\t\t\t\tvector<ll> ol;\n\t\t\t\trep(j,N){\n\t\t\t\t\tll cw=ccw(AP[i],AP[ni],AP[j]);\n\t\t\t\t\tif (cw==1){\n\t\t\t\t\t\tS^=1LL<<j;\n\t\t\t\t\t}\n\t\t\t\t\telse if (abs(cw)!=1){\n\t\t\t\t\t\tol.push_back(j);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif ((AP[i]-AP[ni]).x==0){\n\t\t\t\t\tsort(all(ol),[&](ll ii,ll jj){\n\t\t\t\t\t\treturn (AP[ii].y<AP[jj].y);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\telse{\n\t\t\t\t\tsort(all(ol),[&](ll ii,ll jj){\n\t\t\t\t\t\treturn (AP[ii].x<AP[jj].x);\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tfor (ll k:ol){\n\t\t\t\t\tdiv_set.insert(S);\n\t\t\t\t\tS^=1LL<<k;\n\t\t\t\t}\n\t\t\t\tfor (ll k:ol){\n\t\t\t\t\tdiv_set.insert(S);\n\t\t\t\t\tS^=1LL<<k;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tld cost=1e18;\n\t\tll mask=(1LL<<N)-1;\n\t\tfor (ll s:div_set){\n\t\t\tld c=cost_set(s)+cost_set(s^mask);\n\t\t\tchmin(cost,c);\n\t\t}\n\t\treturn cost;\n\t};\n\tld ans=pt_div();\n\tans/=M;\n\tans=sqrt(ans);\n\tcout<<setprecision(15)<<fixed;\n\tcout<<ans<<endl;\n return;\n}\n\n\nint main() {\n while (true){\n\t\tll N,M;\n\t\tcin>>N>>M;\n\t\tif (N==0)break;\n solve(N,M);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3700, "memory_kb": 4096, "score_of_the_acc": -0.6167, "final_rank": 5 }, { "submission_id": "aoj_1614_7743984", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,s,n) for (int i = s; i < (int)(n); i++)\n#define rrep(i,s,n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(),v.end()\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\ntemplate<typename T> bool chmin(T &a, const T &b){\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate<typename T> bool chmax(T &a, const T &b){\n if (a >= b) return false;\n a = b;\n return true;\n}\n\nnamespace lib{\n\nusing namespace std;\n\n} // namespace lib;\n\nusing namespace lib;\n\nnamespace lib {\n\nusing vec = complex<ld>;\nconst ld eps = 1e-7;\n\nvoid ldout(int len = 15){\n cout << fixed << setprecision(len);\n}\n\nint sgn(ld a) {\n return (a < -eps) ? -1 : (a > eps) ? 1 : 0;\n}\n\nld dot(const vec &a, const vec &b){\n return (conj(a) * b).real();\n}\n\nld cross(const vec &a, const vec &b){\n return (conj(a) * b).imag();\n}\n\nint isp(const vec &a, const vec &b, const vec &c) {\n int cross_sgn = sgn(cross(b - a, c - a));\n if(cross_sgn == 0) {\n if(sgn(dot(b - a, c - a)) < 0) return -2;\n if(sgn(dot(a - b, c - b)) < 0) return 2;\n }\n return cross_sgn;\n}\n\nvec rot90(const vec &a) {\n return {-a.imag(), a.real()};\n}\n\nvec rot(const vec &a, ld rad){\n return a * vec(cosl(rad),sinl(rad));\n}\n\nbool comp_for_argument_sort(const vec &lhs, const vec &rhs){\n //if (abs(arg(lhs)-arg(rhs)) < eps) return false; // need ?\n return arg(lhs) < arg(rhs);\n}\n\n} // namespace lib\n\n\nnamespace lib {\n\nstruct line {\n vec a, b;\n};\n\nvec proj(const line &l, const vec &p) {\n vec ab = l.b - l.a;\n return l.a + ab * (dot(ab, p - l.a) / norm(ab));\n}\n\nvec refl(const line &l, const vec &p) {\n return proj(l, p) * ld(2) - p;\n}\n\nint intersection(const line &a, const line &b) {\n if(sgn(cross(a.b - a.a, b.a - b.b)) != 0) {\n if(sgn(dot(a.b - a.a, b.a - b.b)) == 0) {\n return 1;\n }\n return 0;\n }\n else if(sgn(cross(a.b - a.a, b.a - a.a)) != 0) {\n return 2;\n }\n else {\n return 3;\n }\n}\n\nld dist(const line &a, const vec &p) {\n return abs(cross(p - a.a, a.b - a.a) / abs(a.b - a.a));\n}\n\nvec cross_point(const line &a, const line &b) {\n assert(intersection(a, b) < 2);\n return a.a + (a.b - a.a) * cross(b.a - a.a, b.b - b.a) / cross(a.b - a.a, b.b - b.a);\n}\n\n}\n\nnamespace lib {\n\nstruct circle {\n vec c;\n ld r;\n};\n\nint intersection(const circle &c1, const circle &c2) {\n ld d = abs(c1.c - c2.c);\n ld r1 = c1.r;\n ld r2 = c2.r;\n if(r1 < r2) std::swap(r1, r2);\n if(sgn(d - (r1 + r2)) > 0) {\n return 4;\n }\n else if(sgn(d - (r1 + r2) == 0)) {\n return 3;\n }\n else if(sgn(d - r1 + r2) > 0) {\n return 2;\n }\n else if(sgn(d - r1 + r2) == 0) {\n return 1;\n }\n else return 0;\n}\n\ncircle incircle_of_triangle(const vec &a, const vec &b, const vec &c) {\n ld A = abs(b - c), B = abs(c - a), C = abs(a - b);\n vec in = A * a + B * b + C * c;\n in /= A + B + C;\n ld r = abs(cross(in - a, b - a) / abs(b - a));\n return {in, r};\n}\n\ncircle circumscribed_circle_of_triangle(const vec &a, const vec &b, const vec &c) {\n line p = {(a + b)/ld(2.0), (a + b)/ld(2.0)+rot90(b - a)};\n line q = {(b + c)/ld(2.0), (b + c)/ld(2.0)+rot90(c - b)};\n vec cross = cross_point(p, q);\n return {cross, abs(a-cross)};\n}\n\nvector<vec> cross_point(const circle &c, const line &l) {\n vector<vec> ps;\n ld d = dist(l, c.c);\n if(sgn(d - c.r) == 0) ps.emplace_back(proj(l, c.c));\n else if(sgn(d - c.r) < 0) {\n vec p = proj(l, c.c);\n vec v = l.b - l.a;\n v *= sqrt(max(c.r*c.r - d * d, ld(0))) / abs(v);\n ps.emplace_back(p + v);\n ps.emplace_back(p - v);\n }\n return ps;\n}\n\nvector<vec> cross_point(const circle &c1, const circle &c2) {\n vector<vec> ps;\n int cnt_tangent = intersection(c1, c2);\n if(cnt_tangent == 0 || cnt_tangent == 4) return {};\n ld d = abs(c2.c - c1.c);\n ld x = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);\n vec p = c1.c + (c2.c - c1.c) * x / d;\n vec v = rot90(c2.c - c1.c);\n if(cnt_tangent == 1 || cnt_tangent == 3) ps.emplace_back(p);\n else {\n v *= sqrt(max(c1.r * c1.r - x * x, ld(0))) / abs(v);\n ps.emplace_back(p + v);\n ps.emplace_back(p - v);\n }\n return ps;\n}\n\n}\n\n\ntemplate<typename T>\nvoid vout(const vector<T> &a){\n rep(i,0,a.size()){\n cout << a[i] << \" \\n\"[i == a.size()-1];\n } cout << endl;\n}\n\nbool on_line(const line &a, const vec &v){\n return sgn(dist(a,v)) == 0;\n}\n\nbool same_line(const line &a, const line &b){\n return on_line(a,b.a) && on_line(a,b.b);\n}\n\nbool same_circle(const circle &a, const circle &b){\n return sgn(abs(a.c-b.c)) == 0 && sgn(abs(a.r-b.r)) == 0;\n}\n\nll gcd_safe(ll a, ll b){\n return gcd(abs(a),abs(b));\n}\n\nstruct Line{\n ll a, b, c; // ax + by = c\n ll xp, yp, xq, yq;\n void norm(){ // a >= 0, if (a == 0) -> b >= 0, gcd(a,b,c) = 1\n if (a < 0) a = -a, b = -b, c = -c;\n if (a == 0) if (b < 0) b = -b, c = -c;\n ll g = gcd_safe(gcd_safe(a,b),c);\n a /= g, b /= g, c /= g;\n }\n Line (ll _a = 1, ll _b = 0, ll _c = 0) : a(_a), b(_b), c(_c) {\n assert(_a != 0 || _b != 0);\n norm();\n }\n Line (ll x1, ll y1, ll x2, ll y2){ // --- (x1,y1) --- (x2,y2) ---\n assert(x1 != x2 || y1 != y2);\n ll wx = x2 - x1, wy = y2 - y1;\n ll wg = gcd_safe(wx,wy);\n a = wy / wg, b = -wx / wg;\n c = a * x1 + b * y1;\n norm();\n xp = x1, yp = y1, xq = x2, yq = y2;\n }\n bool operator<(const Line &rhs) const { // to use set or map for Line\n if (a == rhs.a){\n if (b == rhs.b) return c < rhs.c;\n return b < rhs.b;\n }\n return a < rhs.a;\n }\n};\n\nusing P = pair<int,int>;\n\nvoid input_point(int n, int m, vector<vec> &a, vector<line> &lines, vector<circle> &cirs,\n vector<P> &es, vector<ld> &vrs, vector<ll> &nmsk){\n vector<ll> xs(n), ys(n);\n rep(i,0,n){\n cin >> xs[i] >> ys[i];\n a.emplace_back(rot(vec(xs[i],ys[i]),1));\n }\n set<Line> prelines;\n rep(i,0,n-1) rep(j,i+1,n) prelines.insert(Line(xs[i],ys[i],xs[j],ys[j]));\n for (auto cl : prelines){\n vec xy1(cl.xp,cl.yp);\n vec xy2(cl.xq,cl.yq);\n lines.push_back({rot(xy1,1),rot(xy2,1)});\n }\n set<P> cirst;\n rep(i,0,m){\n int u, v; cin >> u >> v; u--, v--;\n ld vr; cin >> vr;\n es[i] = P(u,v);\n vrs[i] = 1/vr/vr;\n nmsk[u] |= (1LL << i);\n int dx = xs[u] - xs[v];\n int dy = ys[u] - ys[v];\n cirst.insert(make_pair(u,dx*dx+dy*dy));\n }\n for (auto p : cirst){\n cirs.push_back({a[p.first],sqrtl(p.second)});\n }\n}\n\nvoid solve1(int n, int m){\n ldout();\n vector<vec> a;\n vector<line> lines;\n vector<circle> cirs;\n vector<P> es(m);\n vector<ld> vrs(m);\n vector<ll> nmsk(n,0);\n input_point(n,m,a,lines,cirs,es,vrs,nmsk);\n cirs.push_back({vec(0,0),5000}); // add large circle\n int ns = cirs.size();\n using pvt = pair<vec,int>;\n // enumerate x, leftmost point of circle, rightmost point of circle, cross point of two circles\n vector<pvt> vs;\n rep(i,0,ns){\n vs.emplace_back(pvt(cirs[i].c - vec(cirs[i].r,0), 0));\n vs.emplace_back(pvt(cirs[i].c + vec(cirs[i].r,0), 1));\n rep(j,i+1,ns){\n for (auto p : cross_point(cirs[i],cirs[j])){\n vs.emplace_back(pvt(p,2));\n }\n }\n }\n sort(all(vs),[&](pvt l, pvt r){\n if (sgn(abs(l.first-r.first)) == 0) return false;\n return l.first.real() < r.first.real();\n });\n // unique x\n vector<pvt> nvs;\n vec mae(-100000,0);\n for (pvt vt : vs){\n if (sgn(abs(mae-vt.first)) == 0) continue;\n nvs.emplace_back(vt);\n mae = vt.first;\n }\n swap(nvs,vs);\n int ms = vs.size();\n vector<vec> pnt;\n rep(i,0,ms){\n if (vs[i].second < 2) continue;\n int cnt = 0;\n rep(j,0,ns) if (sgn(cirs[j].r - abs(cirs[j].c - vs[i].first)) == 0) cnt++;\n vs[i].second = cnt;\n pnt.emplace_back(vs[i].first);\n }\n // enumerate y corresponding each x\n vector<vector<pvt>> ver(ms);\n rep(i,0,ms){\n line l({vec(vs[i].first.real(),0),vec(vs[i].first.real(),1)});\n ver[i].emplace_back(vs[i]);\n rep(j,0,ns){\n for (auto p : cross_point(cirs[j],l)){\n if (sgn(abs(vs[i].first-p)) == 0) continue;\n ver[i].emplace_back(pvt(p,-1));\n }\n }\n sort(all(ver[i]),[&](pvt l, pvt r){\n return l.first.imag() < r.first.imag();\n });\n }\n // enumerate y corresponding middle x = (x1 + x2) / 2, each consecutive x1, x2\n vector<vector<vec>> mid(ms-1);\n rep(i,0,ms-1){\n ld mx = (vs[i].first.real() + vs[i+1].first.real()) / 2;\n line l({vec(mx,0),vec(mx,1)});\n rep(j,0,ns){\n for (auto p : cross_point(cirs[j],l)){\n mid[i].emplace_back(p);\n }\n }\n sort(all(mid[i]),[&](vec l, vec r){\n return l.imag() < r.imag();\n });\n }\n // enumerate point which is representing each area\n rep(itr,0,ms-1){\n int id = 0;\n rep(i,0,ver[itr].size()){\n if (ver[itr][i].second == 1) continue;\n if (ver[itr][i].second == -1){\n id++;\n continue;\n }\n rep(j,0,abs(ver[itr][i].second-1)){\n pnt.emplace_back((mid[itr][id]+mid[itr][id+1])/ld(2));\n id++;\n }\n id++;\n }\n }\n int psiz = pnt.size();\n vector<ll> pmsk(psiz,0);\n rep(j,0,psiz){\n rep(i,0,m){\n ld d1 = abs(a[es[i].first]-a[es[i].second]);\n ld d2 = abs(a[es[i].first]-pnt[j]);\n if (d2 < d1) pmsk[j] |= (1LL<<i);\n } //cout << pmsk[j] << endl; cout << pnt[j] << endl;\n }\n sort(all(pmsk)); pmsk.erase(unique(all(pmsk)),pmsk.end()); psiz = pmsk.size();\n map<ll,ld> indist, outdist;\n auto get_indist = [&](ll msk){\n if (msk == 0) return ld(0);\n if (indist.find(msk) != indist.end()) return indist[msk];\n vec rui(0,0);\n ld wgt = 0;\n rep(i,0,m){\n if (msk >> i & 1){\n rui += a[es[i].first] * vrs[i];\n wgt += vrs[i];\n }\n }\n rui /= wgt;\n ld res = 0;\n rep(i,0,m){\n if (msk >> i & 1){\n res += norm(rui-a[es[i].first]) * vrs[i];\n }\n }\n return indist[msk] = res;\n };\n auto get_outdist = [&](ll msk){\n if (msk == 0) return ld(0);\n if (outdist.find(msk) != outdist.end()) return outdist[msk];\n ld res = 0;\n rep(i,0,m){\n if (msk >> i & 1){\n res += norm(a[es[i].first]-a[es[i].second]) * vrs[i];\n }\n }\n return outdist[msk] = res;\n };\n auto calc = [&](ll msk){\n ld res = 2e18;\n rep(j,0,psiz){\n ll inmsk = pmsk[j]&msk;\n ll outmsk = inmsk^msk;\n chmin(res,get_indist(inmsk)+get_outdist(outmsk));\n }\n return res;\n };\n ld ans = 2e18;\n for (auto l : lines){\n vector<int> idc;\n ll lmsk = 0, rmsk = 0;\n rep(i,0,n){\n if (on_line(l,a[i])){\n idc.emplace_back(i);\n continue;\n }\n (sgn(cross(l.b-l.a,a[i]-l.a)) == 1 ? lmsk : rmsk) |= nmsk[i];\n }\n sort(all(idc),[&](int i, int j){\n return a[i].real() < a[j].real();\n });\n int siz = idc.size();\n vector<ll> lui(siz+1,0), rui(siz+1,0);\n rep(i,0,siz){\n lui[i+1] = (lui[i] | nmsk[idc[i]]);\n }\n rrep(i,0,siz){\n rui[i] = (rui[i+1] | nmsk[idc[i]]);\n }\n for (int cl = 0; cl <= siz; cl++){\n chmin(ans,calc(lmsk|lui[cl])+calc(rmsk|rui[cl]));\n chmin(ans,calc(rmsk|lui[cl])+calc(lmsk|rui[cl]));\n }\n }\n cout << sqrt(ans/m) << endl;\n}\n\nint main(){\n while (true){\n int n, m; cin >> n >> m;\n if (n == 0) break;\n solve1(n,m);\n }\n}", "accuracy": 1, "time_ms": 3070, "memory_kb": 18792, "score_of_the_acc": -1.4694, "final_rank": 8 }, { "submission_id": "aoj_1614_5965164", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define rep1(i,n) for(int i=1;i<=int(n);i++)\n#define per(i,n) for(int i=int(n)-1;i>=0;i--)\n#define per1(i,n) for(int i=int(n);i>0;i--)\n#define all(c) c.begin(),c.end()\n#define si(x) int(x.size())\n#define pb push_back\n#define eb emplace_back\n#define fs first\n#define sc second\ntemplate<class T> using V = vector<T>;\ntemplate<class T> using VV = vector<vector<T>>;\ntemplate<class T,class U> bool chmax(T& x, U y){\n\tif(x<y){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T,class U> bool chmin(T& x, U y){\n\tif(y<x){ x=y; return true; }\n\treturn false;\n}\ntemplate<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}\ntemplate<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}\ntemplate<class T>\nV<T> Vec(size_t a) {\n return V<T>(a);\n}\ntemplate<class T, class... Ts>\nauto Vec(size_t a, Ts... ts) {\n return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));\n}\ntemplate<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){\n\treturn o<<\"(\"<<p.fs<<\",\"<<p.sc<<\")\";\n}\ntemplate<class T> ostream& operator<<(ostream& o,const vector<T> &vc){\n\to<<\"{\";\n\tfor(const T& v:vc) o<<v<<\",\";\n\to<<\"}\";\n\treturn o;\n}\nconstexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }\n\n#ifdef LOCAL\n#define show(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = \" << (x) << endl\nvoid dmpr(ostream& os){os<<endl;}\ntemplate<class T,class... Args>\nvoid dmpr(ostream&os,const T&t,const Args&... args){\n\tos<<t<<\" ~ \";\n\tdmpr(os,args...);\n}\n#define shows(...) cerr << \"LINE\" << __LINE__ << \" : \";dmpr(cerr,##__VA_ARGS__)\n#define dump(x) cerr << \"LINE\" << __LINE__ << \" : \" << #x << \" = {\"; \\\n\tfor(auto v: x) cerr << v << \",\"; cerr << \"}\" << endl;\n#else\n#define show(x) void(0)\n#define dump(x) void(0)\n#define shows(...) void(0)\n#endif\n\ntemplate<class D> D divFloor(D a, D b){\n\treturn a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);\n}\ntemplate<class D> D divCeil(D a, D b) {\n\treturn a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);\n}\n\nusing D = double;\nusing P = complex<D>;\nusing L = pair<P,P>;\nusing Pol = vector<P>;\nstruct C{P p;D r;};\nD inf=1e50,eps=1e-10,PI=acos(0.0)*2;\nbool eq(D a, D b) { return abs(a-b)<eps;}\nbool eq(P a, P b) { return abs(a-b)<eps;}\nint sig(D a) { return eq(a,0) ? 0 : (a>0 ? 1 : -1);}\nint large(D a,D b){return eq(a,b)?0:(a>b?1:-1);}\nbool compxy (const P& l, const P& r){\n\treturn eq(l.real(),r.real()) ? l.imag()<r.imag() : l.real() < r.real();\n}\nbool compyx (const P& l, const P& r){\n\treturn eq(l.imag(),r.imag()) ? l.real()<r.real() : l.imag() < r.imag();\n}\ninline D dot(P a, P b) { return real(conj(a)*b);}\ninline D cro(P a, P b) { return imag(conj(a)*b);}\n//enum ENCCW{CCW(left)=1, CW(right)=-1, FRONT=-2, BACK=2, ON=0};\n//ON優先(including endpoint)\ninline int ccw (P a, P b, P c){\n\tif(sig(cro(b-a,c-a))==1) return 1;\n\tif(sig(cro(b-a,c-a))==-1) return -1;\n\tif(eq(abs(a-c)+abs(c-b),abs(a-b))) return 0;\n\tif(eq(abs(a-b)+abs(b-c),abs(a-c))) return -2;\n\tif(eq(abs(c-a)+abs(a-b),abs(c-b))) return 2;\n\tassert(false);\n}\ninline int ccw(L a,P p){return ccw(a.fs,a.sc,p);}\ninline P proj(P a, P b){\t\t//ベクトルaのbへの射影\n\treturn (dot(a,b)/norm(b))*b;\n}\ninline D verlen(L l,P p){\t//垂線の長さ(符号付き ccw(左)が正)\n\treturn cro(l.sc-l.fs,p-l.fs)/abs(l.sc-l.fs);\n}\ninline P perp(L l, P p){\t\t//垂線の足\n\tD t=dot(p-l.fs,l.fs-l.sc)/norm(l.fs-l.sc);\n\treturn l.fs+t*(l.fs-l.sc);\n}\ninline P refl(L l, P p){\n\treturn p+2.0*(perp(l,p)-p);\n}\ninline bool ispal(L a, L b){\n\treturn sig(cro(a.fs-a.sc,b.fs-b.sc))==0;\n}\ninline bool ovLL(L a, L b){\n\treturn ispal(a,b) && sig(cro(a.fs-a.sc,b.fs-a.sc))==0;\n}\ninline bool iLL(L a, L b){\t\t//intersect or overload\n\treturn !ispal(a,b) || ovLL(a,b);\n}\ninline bool iLS(L l, L s){\t\t//intersect(including endpoint) or overload\n\treturn cro(l.sc-l.fs,s.fs-l.fs)*cro(l.sc-l.fs,s.sc-l.fs)<eps;\n}\ninline bool iLSex(L l, L s){\t\t//intersect(excluding endpoint)\n\treturn cro(l.sc-l.fs,s.fs-l.fs)*cro(l.sc-l.fs,s.sc-l.fs)<-eps;\n}\ninline bool iLP(L l, P p){\t\t//on line\n\treturn abs(sig(cro(l.sc-p,l.fs-p)))!=1;\n}\ninline bool iSS(L a, L b){\t\t//intersect(including endpoint) or overload\n\treturn ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0 && ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0;\n}\ninline bool iSSex(L a, L b){\t\t//intersect(excluding endpoint)\n\treturn ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<0 && ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<0;\n}\ninline bool iSP(L s, P p){\t\t//intersect(including endpoint) or overload\n\treturn ccw(s.fs,s.sc,p)==0;\n}\ninline D dLP(L l, P p) { return abs(perp(l,p)-p);}\ninline D dLL(L a, L b) { return iLL(a,b) ? 0 : dLP(a,b.fs);}\ninline D dLS(L l, L s) { return iLS(l,s) ? 0 : min(dLP(l,s.fs),dLP(l,s.sc));}\ninline D dSP(L s, P p) {\n\tP q=perp(s,p);\n\treturn iSP(s,q) ? abs(p-q) : min(abs(p-s.fs),abs(p-s.sc));\n}\ninline D dSS(L a, L b) {\n\tif(iSS(a,b)) return 0;\n\treturn min(min(dSP(a,b.fs),dSP(a,b.sc)),min(dSP(b,a.fs),dSP(b,a.sc)));\n}\ninline P intLL(L a, L b) {\t//intersection\n\tassert(!ispal(a,b));\n\tD t=cro(a.sc-a.fs,a.sc-b.fs)/cro(a.sc-a.fs,b.sc-b.fs);\n\treturn b.fs+t*(b.sc-b.fs);\n}\n//\ninline int iCL(C c, L l){\t\t//num of intersection(s)\n\tD d=dLP(l,c.p);\n\treturn eq(d,c.r) ? 1 : (d<c.r ? 2 : 0);\n}\nbool containCC(C a,C b){\t//a contain b? (edge case is true)\n\treturn abs(a.p-b.p)+b.r<a.r+eps;\n}\nbool containCCex(C a,C b){\t//a contain b? (edge case is false)\n\treturn abs(a.p-b.p)+b.r<a.r-eps;\n}\n// 交点が2個のとき、aをbが削ると思うと、削れた部分はret[0] -> res[1] (ccw) の部分\nvector<P> intCC(C a,C b){\n\tif(abs(a.p-b.p) > a.r+b.r + eps) return {};\n\tif(containCCex(a,b) || containCCex(b,a)) return {};\n\tD d = abs(a.p-b.p);\n\tD tmp = (a.r*a.r+d*d-b.r*b.r)/(2.0*a.r*d);\n\tif(tmp < -1) tmp = -1;\n\tif(tmp > 1) tmp = 1;\n\tD theta = acos(tmp);\n\tP p1 = a.p+(b.p-a.p)/d*polar(a.r,-theta);\n\tP p2 = a.p+(b.p-a.p)/d*polar(a.r,theta);\n\tif(eq(p1,p2)) return {p1};\n\treturn {p1,p2};\n}\ninline vector<L> tanCP(C c,P p){\n\tint x=large(c.r,abs(p-c.p));\n\tvector<L> ret;\n\tif(x==1) return ret;\n\tif(x==0){\n\t\tret.pb(L(p,p+(c.p-p)*P(0,1)));\n\t\treturn ret;\n\t}\n\tD theta=acos(c.r/abs(p-c.p));\n\tret.pb(L(p,c.p+(p-c.p)/abs(p-c.p)*polar(c.r,theta)));\n\tret.pb(L(p,c.p+(p-c.p)/abs(p-c.p)*polar(c.r,-theta)));\n\treturn ret;\n}\ninline Pol intCL(C c,L l){\n\tint x=large(dLP(l,c.p),c.r);\n\tPol ret;\n\tif(x==1) return ret;\n\tif(x==0){\n\t\tret.pb(perp(l,c.p));\n\t\treturn ret;\n\t}\n\tD d=dLP(l,c.p);\n\tif(d<eps){\n\t\tret.pb(c.p+(l.fs-l.sc)/abs(l.fs-l.sc)*c.r);\n\t\tret.pb(c.p-(l.fs-l.sc)/abs(l.fs-l.sc)*c.r);\n\t\treturn ret;\n\t}\n\tD theta=acos(d/c.r);\n\tP p=perp(l,c.p);\n\tret.pb(c.p+(p-c.p)/d*polar(c.r,theta));\n\tret.pb(c.p+(p-c.p)/d*polar(c.r,-theta));\n\treturn ret;\n}\ninline vector<L> intan(C a,C b){\n\tP p=(a.r*b.p+b.r*a.p)/(a.r+b.r);\n\treturn tanCP(a,p);\n}\ninline vector<L> outtan(C a,C b){\n\tif(a.r==b.r){\n\t\tvector<L> ret;\n\t\tP p=(a.p-b.p)/abs(a.p-b.p)*polar(a.r,PI/2);\n\t\tret.pb(L(a.p+p,b.p+p));\n\t\tret.pb(L(a.p-p,b.p-p));\n\t\treturn ret;\n\t}\n\tP p=(a.r*b.p-b.r*a.p)/(a.r-b.r);\n\treturn tanCP(a,p);\n}\nD aTri(P a, P b, P c){ return cro(b-a,c-a)/2;}\nD aPol(Pol p){\t\t\t//点集合はCCWに与える\n\tint n=p.size();\n\tD ret=0;\n\trep(i,n) ret+=cro(p[i],p[(i+1)%n])/2;\n\treturn ret;\n}\nP gPol(Pol p){\t\t\t//多角形内部が一様な重さを持つときの重心\n\tint n=p.size();\n\tP g;\n\tD s=aPol(p);\n\tassert(s>eps);\n\trep(i,n){\n\t\tD ds=cro(p[i],p[(i+1)%n])/2;\n\t\tg+=ds/3*(p[i]+p[(i+1)%n]);\n\t}\n\treturn g/s;\n}\n//enum ENCONT{INP=1,ONP=0,OUTP=-1};\nint contain(Pol pol, P p){\n\tbool in=false;\n\trep(i,pol.size()){\n\t\tP a=pol[i]-p,b=pol[(i+1)%pol.size()]-p;\n\t\tif(ccw(a,b,P(0,0))==0) return 0;\n\t\tif(imag(a)>imag(b)) swap(a,b);\n\t\tif(sig(imag(a))<=0 && 0<sig(imag(b)) && ccw(P(0,0),a,b)==1) in=!in;\n\t}\n\treturn in ? 1 : -1;\n}\nL BL;\nbool compBL(const P& a,const P& b){\t\t\t//BL上の点をソート(BL.fs側が小さい)\n\treturn dot(BL.sc-BL.fs,a-BL.fs)<dot(BL.sc-BL.fs,b-BL.fs);\n}\nbool containE(Pol pol,L l){\t\t\t//polは閉集合だと思って.\n\tvector<P> ps;\n\tps.pb(l.fs);\n\tps.pb(l.sc);\n\tint N=pol.size();\n\trep(i,N){\n\t\tL a(pol[i],pol[(i+1)%N]);\n\t\tif(ispal(a,l)) continue;\n\t\tif(!ispal(a,l)&&iSS(a,l)) ps.pb(intLL(a,l));\n\t}\n\tBL=l;\n\tsort(all(ps),compBL);\n\tint K=ps.size();\n\trep(i,K-1) ps.pb((ps[i]+ps[i+1])/2.0);\n\tfor(P p:ps) if(!contain(pol,p)) return 0;\n\treturn 1;\n}\ninline D heron(D a, D b, D c){\n\tdouble s=(a+b+c)/2;\n\tif(s-a<eps || s-b<eps || s-c<eps) return 0;\t\t//S=0 || 三角形できない\n\treturn sqrt(s*(s-a)*(s-b)*(s-c));\n}\ninline Pol conv(Pol p){\t\t//convex\n\tint n=p.size(),k=0;\n\tsort(all(p),compxy);\n\tPol ret(2*n);\n\trep(i,n){\n\t\twhile(k>=2 && ccw(ret[k-2],ret[k-1],p[i])<=0) --k;\n\t\tret[k++]=p[i];\n\t}\n\tfor(int i=n-2,t=k+1;i>=0;i--){\n\t\twhile(k>=t && ccw(ret[k-2],ret[k-1],p[i])<=0) --k;\n\t\tret[k++]=p[i];\n\t}\n\tret.resize(k-1);\n\treturn ret;\n}\ninline Pol convall(Pol p){\t\t//conv上の点全部\t\t//点が2回以上出てくる\n\tint n=p.size(),k=0;\n\tsort(all(p),compxy);\n\tPol ret(2*n);\n\trep(i,n){\n\t\twhile(k>=2 && ccw(ret[k-2],ret[k-1],p[i])==-1) --k;\n\t\tret[k++]=p[i];\n\t}\n\tfor(int i=n-2,t=k+1;i>=0;i--){\n\t\twhile(k>=t && ccw(ret[k-2],ret[k-1],p[i])==-1) --k;\n\t\tret[k++]=p[i];\n\t}\n\tret.resize(k-1);\n\tret.erase(unique(all(ret)),ret.end());\n\treturn ret;\n}\ninline Pol convexcut(Pol p,L l){\t//ccw,leftが残る\n\tPol ret;\n\trep(i,p.size()){\n\t\tif(ccw(l.fs,l.sc,p[i])!=-1) ret.pb(p[i]);\n\t\tL s=L(p[i],p[(i+1)%p.size()]);\n\t\tif(iLSex(l,s)) ret.pb(intLL(l,s));\n\t}\n\treturn ret;\n}\ninline Pol interpol(Pol a,Pol b){\t//ccw,ccw\n\trep(j,b.size()){\n\t\ta=convexcut(a,L(b[j],b[(j+1)%b.size()]));\n\t}\n\treturn a;\n}\ninline vector<Pol> makevoronoi(Pol vp,Pol pol){\t//left\n\tvector<Pol> ret;\n\tPol polc=pol;\n\trep(i,vp.size()){\n\t\tpol=polc;\n\t\trep(j,vp.size()){\n\t\t\tif(i==j) continue;\n\t\t\tP p=(vp[i]+vp[j])/2.0,q=p+(vp[j]-vp[i])*P(0,1);\n\t\t\tpol=convexcut(pol,L(p,q));\n\t\t}\n\t\tret.pb(pol);\n\t}\n\treturn ret;\n}\ninline D pol_diameter(Pol p){\n\tp=conv(p);\n\tint n=p.size();\n\tassert(n>=2);\n\tif(n==2) return abs(p[0]-p[1]);\n\tint i=0,j=0;\n\trep(k,n){\n\t\tif(!compxy(p[i],p[k])) i=k;\n\t\tif(compxy(p[j],p[k])) j=k;\n\t}\n\tD ret=0;\n\tint si=i,sj=j;\n\twhile(i!=sj || j!=si){\n\t\tret=max(ret,abs(p[i]-p[j]));\n\t\tif(cro(p[(i+1)%n]-p[i],p[(j+1)%n]-p[j])<0) i=(i+1)%n;\n\t\telse j=(j+1)%n;\n\t}\n\treturn ret;\n}\ninline D closest_pair(P *a, int n){\n\tif(n<=1) return inf;\n\tint m=n/2;\n\tD x=a[m].real();\n\tD d=min(closest_pair(a,m),closest_pair(a+m,n-m));\n\tinplace_merge(a,a+m,a+n,compyx);\n\tvector<P> b;\n\trep(i,n){\n\t\tif(abs(x-a[i].real())>=d) continue;\n\t\trep(j,b.size()){\n\t\t\tP dp=a[i]-b[b.size()-1-j];\n\t\t\tif(dp.imag()>=d) break;\n\t\t\td=min(d,abs(dp));\n\t\t}\n\t\tb.pb(a[i]);\n\t}\n\treturn d;\n}\ninline D compute_shortest(P *a,int n){\n\tsort(a,a+n,compxy);\n\treturn closest_pair(a,n);\n}\n\ndouble rnd(double l,double r){\n\tstatic random_device rd;\n\tstatic mt19937 gen(rd());\n\treturn uniform_real_distribution<double>(l,r)(gen);\n}\n\n// return {p \\cap halfplane}\n// size = O(N^2)\n// time complexity: O(N^3) slow\n// bit表現で持ってるので大きい時はvectorに変えようね!\n// 無駄なのが含まれてるかもしれねえ 自信なし\n// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1614\nV<ll> enumeratePlaneDivision(V<P> p){\n\tint N = si(p);\n\tV<ll> divs;\n//\trep(i,1<<N) divs.pb(i);\n\tdivs.pb(0); divs.pb((1LL<<N)-1);\n\trep(i,N) rep(j,N) if(i != j){\n\t\tP mid = (p[j]+p[i])/2.0;\n\t\tP vec = (p[j]-p[i]) * polar(1.0,1e-8);\n\t\tll s=0, t=0;\n\t\trep(k,N){\n\t\t\tif(cro(vec,p[k]-mid) < 0) s |= 1LL<<k;\n\t\t\telse t |= 1LL<<k;\n\t\t}\n\t\tdivs.pb(s); divs.pb(t);\n\t}\n\tmkuni(divs);\n\treturn divs;\n}\n\n// return { {i s.t. p \\in c[i]} | p: point }\n// 円の集合が与えられるので、どのような領域に分けられるか というのを、円に含まれるか含まれないかの集合で特徴づけて計算する\n// max: N^2-N+2\n// とか言ったけどやや嘘で、正の面積をもつ領域しか列挙してない気もするし、あと無駄なのも含まれてる気もする\n// ロバストにやりたくね~ これで許してくれ\n// 各領域に対して、境界に交点を含まない場合→境界は円なので内と外をためす\n// 含む場合、交点と2つの円弧に挟まれた部分を列挙すればよい\n// と思うじゃん、bがaに内接してたら、a-bの部分は、交点の近傍からは得られないんだよね\n// よく考えると、このように内接してるやつを避けるには、(他の交点からも得られない i.e. どこでも内接 ということを考慮すると、)\n// ↑で言った円の内側を試すときにランダムな回転をして一点取れば良いことが示せる\n// かしこいね~~~ もう一切このライブラリの事信用していません\n\n\nV<ll> enumerateCircleInclusion(V<C> c){\n\tint N = si(c);\n\tV<ll> inclusions;\n//\trep(i,1<<N) inclusions.pb(i);\n\tV<P> cands;\n\tD eps_here = 1e-8;\n\trep(i,N){\n\t\tcands.pb(c[i].p + polar(c[i].r-eps_here, 1.0));\n\t\tcands.pb(c[i].p + polar(c[i].r+eps_here, 1.0));\n\t}\n\trep(i,N) rep(j,N) if(i!=j){\n\t\tV<P> f = intCC(c[i],c[j]);\n\t\tfor(P p: f){\n\t\t\tP d1 = (p-c[i].p); d1 /= abs(d1);\n\t\t\tP d2 = (p-c[j].p); d2 /= abs(d2);\n\t\t\tP dir = (d1+d2); dir /= abs(dir);\n\t\t\trep(_,4){\n\t\t\t\tcands.pb(p + dir * eps_here);\n\t\t\t\tdir *= P(0,1);\n\t\t\t}\n\t\t}\n\t}\n\tfor(P p: cands){\n\t\tll s = 0;\n\t\trep(i,N) if(abs(p-c[i].p) < c[i].r) s |= 1LL<<i;\n\t\tinclusions.pb(s);\n\t}\n\tmkuni(inclusions);\n\treturn inclusions;\n}\n\nint main(){\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\t\t//DON'T USE scanf/printf/puts !!\n\tcout << fixed << setprecision(20);\n\n\twhile(true){\n\t\tint N,M; cin >> N >> M;\n\t\tif(N == 0) break;\n\t\tV<P> p(N);\n\t\trep(i,N){\n\t\t\tD x,y; cin >> x >> y;\n\t\t\tp[i] = P(x,y);\n\t\t}\n\t\tV<int> from(M),to(M);\n\t\tV<D> weight(M);\n\t\trep(i,M){\n\t\t\tD v;\n\t\t\tcin >> from[i] >> to[i] >> v;\n\t\t\tfrom[i]--,to[i]--;\n\t\t\tweight[i] = 1.0/v/v;\n\t\t}\n\t\tV<C> c(M);\n\t\trep(i,M) c[i] = {p[from[i]], abs(p[from[i]]-p[to[i]])};\n\t\tauto divs = enumeratePlaneDivision(p);\t\t// N^2\n\t\tauto inc = enumerateCircleInclusion(c);\t\t// M^2\n\t\tshow(divs);show(inc);\n\t\tD ans = inf;\n\t\tfor(ll sl: divs){\n\t\t\tauto f = [&](ll s){\n\t\t\t\tD res = inf;\n\t\t\t\tfor(ll usecolor: inc){\n\t\t\t\t\tP red;\n\t\t\t\t\tD rr=0;\n\t\t\t\t\trep(i,M) if(s&1LL<<from[i]){\n\t\t\t\t\t\tif(usecolor&1LL<<i){\n\t\t\t\t\t\t\trr += weight[i];\n\t\t\t\t\t\t\tred += p[from[i]] * weight[i];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif(rr) red /= rr;\n\t\t\t\t\tD tmp = 0;\n\t\t\t\t\trep(i,M) if(s&1LL<<from[i]){\n\t\t\t\t\t\tif(usecolor&1LL<<i){\n\t\t\t\t\t\t\ttmp += weight[i] * norm(red - p[from[i]]);\n\t\t\t\t\t\t}else{\t// black\n\t\t\t\t\t\t\ttmp += weight[i] * norm(p[to[i]] - p[from[i]]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tchmin(res,tmp);\n\t\t\t\t}\n\t\t\t\treturn res;\n\t\t\t};\n\t\t\tll sr = ((1LL<<N)-1) ^ sl;\n\t\t\tchmin(ans,f(sl) + f(sr));\n\t\t}\n\t\tcout << sqrt(ans/M) << endl;\n\t}\n}", "accuracy": 1, "time_ms": 1640, "memory_kb": 4472, "score_of_the_acc": -0.2214, "final_rank": 4 }, { "submission_id": "aoj_1614_5442144", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n#define EPS 0.00000001\n#define SIZE 41\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Info{\n\tint from,to;\n\tdouble V,D,rev_V2;\n};\n\nstruct DATA{\n\tDATA(Point arg_p,double arg_rad){\n\t\tp = arg_p;\n\t\trad = arg_rad;\n\t}\n\tDATA(){\n\t\trad = 0;\n\t}\n\tbool operator<(const struct DATA &arg) const{\n\n\t\treturn rad < arg.rad;\n\t}\n\tPoint p;\n\tdouble rad;\n};\n\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nll POW[SIZE];\nint N,M,table[SIZE];\nvector<int> STATE;\nvector<int> INFO[SIZE];\ndouble dp[1024*1024];\ndouble DIST[SIZE],NUM = 100000;\nPoint point[SIZE];\nInfo info[SIZE];\nCircle C[SIZE];\nbool check[SIZE];\nvector<DATA> ON_P[SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\ndouble arg(Vector p){\n\treturn atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n\treturn Point(cos(r)*a,sin(r)*a);\n}\n\n//円と円の交点\nvector<Point> getCrossPoints(Circle c1,Circle c2){\n\n\tvector<Point> res;\n\n\tdouble d = abs(c1.center-c2.center);\n\tdouble a = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n\tdouble t = arg(c2.center-c1.center);\n\n\tres.push_back(Point(c1.center+polar(c1.r,t+a)));\n\tres.push_back(Point(c1.center+polar(c1.r,t-a)));\n\n\treturn res;\n}\n\n//t^2を求める\ndouble getT(Point a,Point b,double V){\n\n\tdouble A = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);\n\treturn A/(V*V);\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nvoid func(){\n\n\tSTATE.clear();\n\n\tfor(int i = 0; i < SIZE; i++){\n\n\t\tINFO[i].clear();\n\t\tDIST[i] = 0;\n\t\tON_P[i].clear();\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&point[i].x,&point[i].y);\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%d %d %lf\",&info[i].from,&info[i].to,&info[i].V);\n\t\tinfo[i].from--;\n\t\tinfo[i].to--;\n\n\t\tinfo[i].rev_V2 = 1.0/(info[i].V*info[i].V);\n\t\tdouble tmp_dist = calc_dist(point[info[i].from],point[info[i].to]);\n\t\tinfo[i].D = tmp_dist*tmp_dist;\n\n\t\tC[i].center = point[info[i].from];\n\t\tC[i].r = tmp_dist;\n\n\t\tINFO[info[i].from].push_back(i);\n\n\t\tDIST[i] = getT(point[info[i].from],point[info[i].to],info[i].V);\n\t}\n\n\tfor(int i = 0; i < N-1; i++){\n\t\tfor(int k = i+1; k < N; k++){\n\n\t\t\tPoint mid_p = (point[i]+point[k])/2;\n\n\t\t\tLine base_line;\n\n\t\t\tif(fabs(point[i].x-point[k].x) < EPS){\n\n\t\t\t\tbase_line = Line(Point(-NUM,mid_p.y),Point(NUM,mid_p.y));\n\n\t\t\t}else if(fabs(point[i].y-point[k].y) < EPS){\n\n\t\t\t\tbase_line = Line(Point(mid_p.x,-NUM),Point(mid_p.x,NUM));\n\n\t\t\t}else{\n\n\t\t\t\tdouble tmp_slope = calc_slope(Line(point[i],point[k]));\n\t\t\t\ttmp_slope = (-1.0)/tmp_slope;\n\n\t\t\t\tbase_line = Line(Point(mid_p.x-NUM,mid_p.y-NUM*tmp_slope),Point(mid_p.x+NUM,mid_p.y+NUM*tmp_slope));\n\t\t\t}\n\n\t\t\tvector<int> I,K,MID;\n\t\t\tI.push_back(i);\n\t\t\tK.push_back(k);\n\n\t\t\tfor(int a = 0; a < N; a++){\n\t\t\t\tif(a == i || a == k)continue;\n\n\t\t\t\tif(getDistanceSP(base_line,point[a]) < EPS){\n\n\t\t\t\t\tMID.push_back(a);\n\t\t\t\t}else{\n\n\t\t\t\t\tLine work_line = Line(point[a],point[i]);\n\t\t\t\t\tif(is_Cross(base_line,work_line)){\n\n\t\t\t\t\t\tK.push_back(a);\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tI.push_back(a);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint state_I = 0,state_K = 0;\n\t\t\tfor(int a = 0; a < I.size(); a++){\n\n\t\t\t\tstate_I += POW[I[a]];\n\t\t\t}\n\t\t\tfor(int a = 0; a < K.size(); a++){\n\n\t\t\t\tstate_K += POW[K[a]];\n\t\t\t}\n\n\t\t\tif(MID.size() == 0){\n\n\t\t\t\tSTATE.push_back(state_I);\n\t\t\t\tSTATE.push_back(state_K);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tfor(int state = 0; state < POW[MID.size()]; state++){\n\t\t\t\tint work_I = state_I;\n\t\t\t\tint work_K = state_K;\n\t\t\t\tfor(int loop = 0; loop < MID.size(); loop++){\n\t\t\t\t\tif(state&POW[loop]){\n\n\t\t\t\t\t\twork_I += POW[MID[loop]];\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\twork_K += POW[MID[loop]];\n\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tSTATE.push_back(work_I);\n\t\t\t\tSTATE.push_back(work_K);\n\t\t\t}\n\t\t}\n\t}\n\n\tsort(STATE.begin(),STATE.end());\n\tSTATE.erase(unique(STATE.begin(),STATE.end()),STATE.end());\n\n\n\tfor(int i = 0; i < M-1; i++){\n\t\tfor(int k = i+1; k < M; k++){\n\n\t\t\tdouble tmp_dist = calc_dist(C[i].center,C[k].center);\n\n\t\t\tif(tmp_dist > C[i].r+C[k].r+EPS)continue;\n\t\t\tif(min(C[i].r,C[k].r)+tmp_dist+EPS < max(C[i].r,C[k].r)){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tvector<Point> ret = getCrossPoints(C[i],C[k]);\n\t\t\tfor(int a = 0; a < ret.size(); a++){\n\n\t\t\t\tON_P[i].push_back(DATA(ret[a],calc_rad(ret[a]-C[i].center)));\n\t\t\t\tON_P[k].push_back(DATA(ret[a],calc_rad(ret[a]-C[k].center)));\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tON_P[i].push_back(DATA(Point(C[i].center.x+C[i].r,C[i].center.y),0));\n\t\tON_P[i].push_back(DATA(Point(C[i].center.x,C[i].center.y+C[i].r),M_PI/2));\n\t\tON_P[i].push_back(DATA(Point(C[i].center.x-C[i].r,C[i].center.y),M_PI));\n\t\tON_P[i].push_back(DATA(Point(C[i].center.x,C[i].center.y-C[i].r),(M_PI*3)/2.0));\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\t\tif(ON_P[i].size() == 0)continue;\n\n\t\tsort(ON_P[i].begin(),ON_P[i].end());\n\t}\n\n\n\tvector<ll> cross_STATE;\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tvector<Point> ret;\n\t\tret.push_back(C[i].center);\n\n\t\tfor(int k = 0; k < ON_P[i].size(); k++){\n\n\t\t\tPoint tmp_p = ON_P[i][k].p;\n\n\t\t\tint size = ON_P[i].size();\n\t\t\tdouble tmp_rad = ON_P[i][(k+1)%size].rad-ON_P[i][k].rad;\n\t\t\tif((k+1) == size){\n\n\t\t\t\ttmp_rad += 2*M_PI;\n\t\t\t}\n\t\t\ttmp_rad /= 2;\n\n\t\t\tPoint calc_P = rotate(C[i].center,tmp_p,tmp_rad);\n\n\t\t\tVector tmp_vec = calc_P-C[i].center;\n\t\t\ttmp_vec = tmp_vec/abs(tmp_vec);\n\n\t\t\tret.push_back(calc_P+tmp_vec*10*EPS);\n\t\t\tret.push_back(calc_P-tmp_vec*10*EPS);\n\t\t}\n\n\n\t\tfor(int a = 0; a < ret.size(); a++){\n\n\t\t\tll tmp_S = 0;\n\n\t\t\tfor(int b = 0; b < M; b++){\n\t\t\t\tdouble tmp_dist = calc_dist(C[b].center,ret[a]);\n\t\t\t\tif(tmp_dist > C[b].r+EPS)continue;\n\n\t\t\t\ttmp_S += POW[b];\n\t\t\t}\n\t\t\tcross_STATE.push_back(tmp_S);\n\t\t}\n\t}\n\n\tsort(cross_STATE.begin(),cross_STATE.end());\n\tcross_STATE.erase(unique(cross_STATE.begin(),cross_STATE.end()),cross_STATE.end());\n\n\n\tdp[0] = 0;\n\tfor(int state = 1; state < POW[N]; state++){\n\n\t\tdp[state] = HUGE_NUM;\n\t}\n\n\tfor(int i = 0; i < STATE.size(); i++){\n\n\t\tint base_state = STATE[i];\n\n\t\tvector<int> BIN;\n\t\tint count = 0;\n\n\t\tll MASK = 0;\n\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(base_state&POW[k]){\n\t\t\t\tfor(int a = 0; a < INFO[k].size(); a++){\n\n\t\t\t\t\tBIN.push_back(INFO[k][a]);\n\t\t\t\t\tMASK += POW[INFO[k][a]];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tset<ll> SET;\n\n\t\tfor(int a = 0; a < BIN.size(); a++){\n\n\t\t\tSET.insert(POW[BIN[a]]);\n\t\t}\n\t\tfor(int a = 0; a < cross_STATE.size(); a++){\n\n\t\t\tSET.insert(MASK&cross_STATE[a]);\n\t\t}\n\n\t\tfor(auto tmp_S: SET){\n\n\t\t\tdouble sum = 0;\n\n\t\t\tvector<int> warp_vec;\n\n\t\t\tfor(int k = 0; k < BIN.size(); k++){\n\t\t\t\tif(tmp_S&POW[BIN[k]]){\n\n\t\t\t\t\twarp_vec.push_back(BIN[k]);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tsum += DIST[BIN[k]];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdouble X = 0,Y = 0;\n\n\t\t\tfor(int E = 0; E < 2; E++){\n\n\t\t\t\tdouble bunshi = 0;\n\t\t\t\tdouble bunbo = 0;\n\n\t\t\t\tfor(int b = 0; b < warp_vec.size(); b++){\n\n\t\t\t\t\tint tmp_from = info[warp_vec[b]].from;\n\n\t\t\t\t\tif(E == 0){\n\n\t\t\t\t\t\tbunshi += point[tmp_from].x*info[warp_vec[b]].rev_V2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tbunshi += point[tmp_from].y*info[warp_vec[b]].rev_V2;\n\t\t\t\t\t}\n\t\t\t\t\tbunbo += info[warp_vec[b]].rev_V2;\n\t\t\t\t}\n\n\t\t\t\tif(E == 0){\n\n\t\t\t\t\tX = bunshi/bunbo;\n\t\t\t\t}else{\n\n\t\t\t\t\tY = bunshi/bunbo;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tPoint WARP_P = Point(X,Y);\n\t\t\tfor(int b = 0; b < warp_vec.size(); b++){\n\t\t\t\tif(tmp_S&POW[warp_vec[b]]){\n\t\t\t\t\tsum += getT(point[info[warp_vec[b]].from],WARP_P,info[warp_vec[b]].V);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdp[base_state] = min(dp[base_state],sum);\n\t\t}\n\n\t}\n\n\tdouble ans = HUGE_NUM;\n\n\tfor(int state = 0; state < POW[N]; state++){\n\n\t\tans = min(ans,dp[state]+dp[(POW[N]-1)-state]);\n\t}\n\n\tprintf(\"%.12lf\\n\",sqrt(ans/M));\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\twhile(true){\n\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4300, "memory_kb": 16056, "score_of_the_acc": -1.5377, "final_rank": 9 }, { "submission_id": "aoj_1614_5442133", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n//#define EPS 0.000000001\nusing namespace std;\n\n\n#define EPS 0.00000001\n#define SIZE 41\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = arg_y;\n\t}\n\n\tPoint(){\n\t\tx = y = 0.0;\n\t}\n\n\tPoint operator + (Point p){ return Point(x+p.x,y+p.y); }\n\tPoint operator - (Point p){ return Point(x-p.x,y-p.y);}\n\tPoint operator * (double a){ return Point(a*x,a*y); }\n\tPoint operator / (double a){ return Point(x/a,y/a); }\n\n\tdouble abs(){ return sqrt(norm()); }\n\tdouble norm(){ return x*x + y*y; }\n\n\tbool operator<(const Point &p) const{\n\n\t\tif(fabs(x-p.x) > EPS){\n\n\t\t\treturn x < p.x;\n\t\t}else{\n\n\t\t\treturn y < p.y;\n\t\t}\n\t}\n\n\tbool operator == (const Point &p) const{\n\t\treturn fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;\n\t}\n\n\n\tvoid debug(){\n\n\t\tprintf(\"(%.3lf,%.3lf)\\n\",x,y);\n\t}\n\n\tdouble x,y;\n};\n\ntypedef Point Vector;\ntypedef vector<Point> Polygon;\n\nstruct Line{\n\tLine(){\n\n\t}\n\tLine(Point a,Point b){\n\t\tp[0] = a;\n\t\tp[1] = b;\n\t}\n\t/*void outPut(){\n\n\t\tprintf(\"(%.3lf,%.3lf)-(%.3lf,%.3lf)\\n\",p[0].x,p[0].y,p[1].x,p[1].y);\n\t}*/\n\tPoint p[2];\n};\n\nstruct Info{\n\tint from,to;\n\tdouble V,D,rev_V2;\n};\n\nstruct DATA{\n\tDATA(Point arg_p,double arg_rad){\n\t\tp = arg_p;\n\t\trad = arg_rad;\n\t}\n\tDATA(){\n\t\trad = 0;\n\t}\n\tbool operator<(const struct DATA &arg) const{\n\n\t\treturn rad < arg.rad;\n\t}\n\tPoint p;\n\tdouble rad;\n};\n\n\nstruct Circle{\n\tPoint center;\n\tdouble r;\n};\n\nll POW[SIZE];\nint N,M,table[SIZE];\nvector<int> STATE;\nvector<int> INFO[SIZE];\ndouble MULT = 10;\ndouble dp[1024*1024],diff_x[3] = {-MULT*EPS,0,MULT*EPS},diff_y[3] = {-MULT*EPS,0,MULT*EPS};\ndouble DIST[SIZE],NUM = 100000;\nPoint point[SIZE];\nInfo info[SIZE];\nCircle C[SIZE];\nbool check[SIZE];\nvector<DATA> ON_P[SIZE];\n\n\ndouble calc_dist(Point A,Point B){\n\n\treturn sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));\n}\n\ndouble norm(Vector a){\n\treturn a.x*a.x+a.y*a.y;\n}\n\ndouble abs(Vector a){\n\treturn sqrt(norm(a));\n}\n\ndouble cross(Vector a,Vector b){\n return a.x*b.y-a.y*b.x;\n}\n\ndouble dot(Vector a,Vector b){\n return a.x*b.x + a.y*b.y;\n}\n\nstatic const int COUNTER_CLOCKWISE = 1;\nstatic const int CLOCKWISE = -1;\nstatic const int ONLINE_BACK = 2;\nstatic const int ONLINE_FRONT = -2;\nstatic const int ON_SEGMENT = 0;\n\n/*\n * p0を基準点として、p1から見てp2が\n * 反時計回り側にあれば\n * COUNTER_CLOCKWISE\n * */\nint ccw(Point p0,Point p1,Point p2){\n\n\tVector a = p1 - p0;\n\tVector b = p2 - p0;\n\n\tif(cross(a,b) > EPS)return COUNTER_CLOCKWISE;\n\tif(cross(a,b) < -EPS)return CLOCKWISE;\n\tif(dot(a,b) < -EPS)return ONLINE_BACK;\n\tif(a.norm() < b.norm())return ONLINE_FRONT;\n\n\treturn ON_SEGMENT;\n}\n\nint func(double x1,double y1,double x2, double y2, double xp, double yp){\n\tdouble naiseki,norm1,norm2,gaiseki;\n\tnorm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));\n\tnorm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1));\n\tnaiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1);\n\tgaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1);\n\tif(gaiseki > EPS){\n\t\treturn 1;\n\t}else if(gaiseki < -EPS){\n\t\treturn -1;\n\t}\n\tif(naiseki < -EPS){\n\t\treturn 2;\n\t}\n\n\tif(norm1 < norm2){\n\t\treturn -2;\n\t}\n\treturn 0;\n}\n\n\n//★★直線ではなく、線分の交差判定★★\nbool is_Cross(Line a,Line b){\n\n\tif(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)*\n\t\t\tfunc(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 &&\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)*\n\t\t\tfunc(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ndouble calc_len(Vector a){\n return sqrt(a.x*a.x+a.y*a.y);\n}\n\n\nPoint calc_minus(Point a,Point b){\n Point ret;\n\n ret.x = a.x-b.x;\n ret.y = a.y-b.y;\n\n return ret;\n}\n\n//線分ではなく直線と点の距離\ndouble getDistanceLP(Line l,Point p){\n return fabs(cross(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0]))/calc_len(calc_minus(l.p[1],l.p[0])));\n}\n\n//点と線分の距離\ndouble getDistanceSP(Line l,Point p){\n\tif(dot(calc_minus(l.p[1],l.p[0]),calc_minus(p,l.p[0])) < 0.0)return calc_len(calc_minus(p,l.p[0]));\n\tif(dot(calc_minus(l.p[0],l.p[1]),calc_minus(p,l.p[1])) < 0.0)return calc_len(calc_minus(p,l.p[1]));\n\treturn getDistanceLP(l,p);\n}\n\n//線分と線分の距離\ndouble getDistance(Line A,Line B){\n\tif(is_Cross(A,B))return 0.0;\n\treturn min(min(getDistanceSP(A,B.p[0]),getDistanceSP(A,B.p[1])),\n\t\t\tmin(getDistanceSP(B,A.p[0]),getDistanceSP(B,A.p[1])));\n}\n\n\ndouble calc_slope(Line A){\n\n\tif(fabs(A.p[0].x-A.p[1].x) < EPS){\n\n\t\treturn DBL_MAX;\n\n\t}else if(fabs(A.p[0].y-A.p[1].y) < EPS){\n\n\t\treturn 0;\n\n\t}else{\n\n\t\treturn (A.p[1].y-A.p[0].y)/(A.p[1].x-A.p[0].x);\n\t}\n}\n\ndouble arg(Vector p){\n\treturn atan2(p.y,p.x);\n}\n\nVector polar(double a,double r){\n\treturn Point(cos(r)*a,sin(r)*a);\n}\n\n//円と円の交点\nvector<Point> getCrossPoints(Circle c1,Circle c2){\n\n\tvector<Point> res;\n\n\tdouble d = abs(c1.center-c2.center);\n\tdouble a = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));\n\tdouble t = arg(c2.center-c1.center);\n\n\tres.push_back(Point(c1.center+polar(c1.r,t+a)));\n\tres.push_back(Point(c1.center+polar(c1.r,t-a)));\n\n\treturn res;\n}\n\n//t^2を求める\ndouble getT(Point a,Point b,double V){\n\n\tdouble A = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);\n\treturn A/(V*V);\n}\n\n/*点moveをbaseを中心にradラジアン回転させる\nrad > 0なら反時計回り、rad < 0なら時計周り\n*/\nPoint rotate(Point base,Point move,double rad){\n\n\tPoint ret;\n\tmove = move-base;\n\n\tret.x = move.x*cos(rad)-move.y*sin(rad);\n\tret.y = move.x*sin(rad)+move.y*cos(rad);\n\n\tret = ret+base;\n\n\treturn ret;\n}\n\n//点のradを求める関数\ndouble calc_rad(Point p){\n\n\tif(p.y == 0){\n\n\t\tif(p.x > 0){\n\n\t\t\treturn 0;\n\n\t\t}else{ //p.x < 0\n\n\t\t\treturn M_PI;\n\t\t}\n\n\t}else if(p.x == 0){\n\n\t\tif(p.y > 0){\n\n\t\t\treturn M_PI/2.0;\n\t\t}else{\n\n\n\t\t\treturn 3*M_PI/2.0;\n\t\t}\n\n\t}else{\n\n\t\tdouble base = atan(fabs(p.y)/fabs(p.x));\n\n\t\tif(p.x > 0){\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn base;\n\n\t\t\t}else{ //p.y < 0\n\n\t\t\t\treturn 2*M_PI-base;\n\t\t\t}\n\n\t\t}else{ //p.x < 0\n\n\t\t\tif(p.y > 0){\n\n\t\t\t\treturn M_PI-base;\n\n\t\t\t}else{\n\n\t\t\t\treturn base+M_PI;\n\t\t\t}\n\t\t}\n\t}\n}\n\n\nvoid func(){\n\n\tSTATE.clear();\n\n\tfor(int i = 0; i < SIZE; i++){\n\n\t\tINFO[i].clear();\n\t\tDIST[i] = 0;\n\t\tON_P[i].clear();\n\t}\n\n\tfor(int i = 0; i < N; i++){\n\n\t\tscanf(\"%lf %lf\",&point[i].x,&point[i].y);\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tscanf(\"%d %d %lf\",&info[i].from,&info[i].to,&info[i].V);\n\t\tinfo[i].from--;\n\t\tinfo[i].to--;\n\n\t\tinfo[i].rev_V2 = 1.0/(info[i].V*info[i].V);\n\t\tdouble tmp_dist = calc_dist(point[info[i].from],point[info[i].to]);\n\t\tinfo[i].D = tmp_dist*tmp_dist;\n\n\t\tC[i].center = point[info[i].from];\n\t\tC[i].r = tmp_dist;\n\n\t\tINFO[info[i].from].push_back(i);\n\n\t\tDIST[i] = getT(point[info[i].from],point[info[i].to],info[i].V);\n\t}\n\n\t/*2空港間の垂直二等分線を使い、空港の集合を二分割して、\n\t 同じワープポイントを使う可能性のある集合を全列挙する\n\t (垂直二等分線上に空港が並ぶ場合あり)\n\t 同じ空港に属する別の便が、異なるワープポイントを使うことはない\n\t 使うなら近いほうがいいから。ただし、目的地の方が近ければ、ワープポイントを使わない\n\t */\n\tfor(int i = 0; i < N-1; i++){\n\t\tfor(int k = i+1; k < N; k++){\n\n\t\t\tPoint mid_p = (point[i]+point[k])/2;\n\n\t\t\tLine base_line; //垂直二等分線\n\n\t\t\tif(fabs(point[i].x-point[k].x) < EPS){ //垂直\n\n\t\t\t\tbase_line = Line(Point(-NUM,mid_p.y),Point(NUM,mid_p.y));\n\n\t\t\t}else if(fabs(point[i].y-point[k].y) < EPS){ //水平\n\n\t\t\t\tbase_line = Line(Point(mid_p.x,-NUM),Point(mid_p.x,NUM));\n\n\t\t\t}else{\n\n\t\t\t\tdouble tmp_slope = calc_slope(Line(point[i],point[k]));\n\t\t\t\ttmp_slope = (-1.0)/tmp_slope;\n\n\t\t\t\tbase_line = Line(Point(mid_p.x-NUM,mid_p.y-NUM*tmp_slope),Point(mid_p.x+NUM,mid_p.y+NUM*tmp_slope));\n\t\t\t}\n\n\t\t\tvector<int> I,K,MID;\n\t\t\tI.push_back(i);\n\t\t\tK.push_back(k);\n\n\t\t\tfor(int a = 0; a < N; a++){\n\t\t\t\tif(a == i || a == k)continue;\n\n\t\t\t\tif(getDistanceSP(base_line,point[a]) < EPS){ //垂直二等分線上にある空港\n\n\t\t\t\t\tMID.push_back(a);\n\t\t\t\t}else{\n\n\t\t\t\t\tLine work_line = Line(point[a],point[i]); //iへの直線\n\t\t\t\t\tif(is_Cross(base_line,work_line)){\n\n\t\t\t\t\t\tK.push_back(a);\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tI.push_back(a);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint state_I = 0,state_K = 0;\n\t\t\tfor(int a = 0; a < I.size(); a++){\n\n\t\t\t\tstate_I += POW[I[a]];\n\t\t\t}\n\t\t\tfor(int a = 0; a < K.size(); a++){\n\n\t\t\t\tstate_K += POW[K[a]];\n\t\t\t}\n\n\t\t\tif(MID.size() == 0){\n\n\t\t\t\tSTATE.push_back(state_I);\n\t\t\t\tSTATE.push_back(state_K);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t//垂直二等分線上の空港を、IまたはKに振り分ける\n\t\t\tfor(int state = 0; state < POW[MID.size()]; state++){\n\t\t\t\tint work_I = state_I;\n\t\t\t\tint work_K = state_K;\n\t\t\t\tfor(int loop = 0; loop < MID.size(); loop++){\n\t\t\t\t\tif(state&POW[loop]){\n\n\t\t\t\t\t\twork_I += POW[MID[loop]];\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\twork_K += POW[MID[loop]];\n\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tSTATE.push_back(work_I);\n\t\t\t\tSTATE.push_back(work_K);\n\t\t\t}\n\t\t}\n\t}\n\n\tsort(STATE.begin(),STATE.end());\n\tSTATE.erase(unique(STATE.begin(),STATE.end()),STATE.end());\n\n\n\t//円弧上の点の回転角度を計算するため、ある円上にある交点を計算\n\tfor(int i = 0; i < M-1; i++){\n\t\tfor(int k = i+1; k < M; k++){\n\n\t\t\t//交差しない\n\t\t\tdouble tmp_dist = calc_dist(C[i].center,C[k].center);\n\n\t\t\t//片方がもう片方を含む\n\t\t\tif(tmp_dist > C[i].r+C[k].r+EPS)continue;\n\t\t\tif(min(C[i].r,C[k].r)+tmp_dist+EPS < max(C[i].r,C[k].r)){\n\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tvector<Point> ret = getCrossPoints(C[i],C[k]);\n\t\t\tfor(int a = 0; a < ret.size(); a++){\n\n\t\t\t\tON_P[i].push_back(DATA(ret[a],calc_rad(ret[a]-C[i].center)));\n\t\t\t\tON_P[k].push_back(DATA(ret[a],calc_rad(ret[a]-C[k].center)));\n\t\t\t}\n\t\t}\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\n\t\tON_P[i].push_back(DATA(Point(C[i].center.x+C[i].r,C[i].center.y),0));\n\t\tON_P[i].push_back(DATA(Point(C[i].center.x,C[i].center.y+C[i].r),M_PI/2));\n\t\tON_P[i].push_back(DATA(Point(C[i].center.x-C[i].r,C[i].center.y),M_PI));\n\t\tON_P[i].push_back(DATA(Point(C[i].center.x,C[i].center.y-C[i].r),(M_PI*3)/2.0));\n\t}\n\n\tfor(int i = 0; i < M; i++){\n\t\tif(ON_P[i].size() == 0)continue;\n\n\t\tsort(ON_P[i].begin(),ON_P[i].end());\n\t}\n\n\n\tvector<ll> cross_STATE;\n\n\t//ある2円を交差させた際に、その共通領域を同じ共通領域とする便の集合を前計算\n\tfor(int i = 0; i < M; i++){\n\n\t\tvector<Point> ret;\n\t\tret.push_back(C[i].center);\n\n\t\tfor(int k = 0; k < ON_P[i].size(); k++){\n\n\t\t\tPoint tmp_p = ON_P[i][k].p;\n\n\t\t\tint size = ON_P[i].size();\n\t\t\tdouble tmp_rad = ON_P[i][(k+1)%size].rad-ON_P[i][k].rad;\n\t\t\tif((k+1) == size){\n\n\t\t\t\ttmp_rad += 2*M_PI;\n\t\t\t}\n\t\t\ttmp_rad /= 2;\n\n\t\t\tPoint calc_P = rotate(C[i].center,tmp_p,tmp_rad);\n\n\t\t\tVector tmp_vec = calc_P-C[i].center;\n\t\t\ttmp_vec = tmp_vec/abs(tmp_vec);\n\n\t\t\tret.push_back(calc_P+tmp_vec*10*EPS);\n\t\t\tret.push_back(calc_P-tmp_vec*10*EPS);\n\n\t\t\t/*//誤差が出るので、適当に近傍の点を入れる\n\t\t\tfor(int e = 0; e < 3; e++){\n\t\t\t\tfor(int f = 0; f < 3; f++){\n\n\t\t\t\t\tPoint adj_p = Point(calc_P.x+diff_x[e],calc_P.y+diff_y[f]);\n\n\t\t\t\t\tif(calc_dist(C[i].center,adj_p) > C[i].r+EPS || calc_dist(C[k].center,adj_p) > C[k].r+EPS)continue;\n\n\t\t\t\t\tret.push_back(adj_p);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tcalc_P = rotate(C[i].center,tmp_p,-tmp_rad);\n\n\n\t\t\t//誤差が出るので、適当に近傍の点を入れる\n\t\t\tfor(int e = 0; e < 3; e++){\n\t\t\t\tfor(int f = 0; f < 3; f++){\n\n\t\t\t\t\tPoint adj_p = Point(calc_P.x+diff_x[e],calc_P.y+diff_y[f]);\n\n\t\t\t\t\tif(calc_dist(C[i].center,adj_p) > C[i].r+EPS || calc_dist(C[k].center,adj_p) > C[k].r+EPS)continue;\n\n\t\t\t\t\tret.push_back(adj_p);\n\t\t\t\t}\n\t\t\t}\n\n*/\n\t\t}\n\n\n\t\tfor(int a = 0; a < ret.size(); a++){\n\n\t\t\tll tmp_S = 0;\n\n\t\t\tfor(int b = 0; b < M; b++){\n\t\t\t\tdouble tmp_dist = calc_dist(C[b].center,ret[a]);\n\t\t\t\tif(tmp_dist > C[b].r+EPS)continue;\n\n\t\t\t\ttmp_S += POW[b];\n\t\t\t}\n\t\t\tcross_STATE.push_back(tmp_S);\n\t\t}\n\t}\n\n\tsort(cross_STATE.begin(),cross_STATE.end());\n\tcross_STATE.erase(unique(cross_STATE.begin(),cross_STATE.end()),cross_STATE.end());\n\n\t/*for(int a = 0; a < cross_STATE.size(); a++){\n\n\t\tprintf(\"%lld\\n\",cross_STATE[a]);\n\t}\n\n\treturn;*/\n\n\n\tdp[0] = 0; //空集合\n\tfor(int state = 1; state < POW[N]; state++){\n\n\t\tdp[state] = HUGE_NUM;\n\t}\n\n\n\t//ある状態について、ワープを使う空港と使わない空港の組み合わせを全探索する\n\tfor(int i = 0; i < STATE.size(); i++){\n\n\t\tint base_state = STATE[i]; //空港の集合\n\t\t//printf(\"\\nbase_state:%d\\n\",base_state);\n\n\t\tfor(int k = 0; k < M; k++){\n\n\t\t\tcheck[k] = false;\n\t\t\ttable[k] = -1;\n\t\t}\n\n\t\tvector<int> BIN; //便のindex\n\t\tint count = 0;\n\n\t\tll MASK = 0;\n\n\t\tfor(int k = 0; k < N; k++){\n\t\t\tif(base_state&POW[k]){\n\t\t\t\tfor(int a = 0; a < INFO[k].size(); a++){\n\n\t\t\t\t\tBIN.push_back(INFO[k][a]);\n\t\t\t\t\tcheck[INFO[k][a]] = true;\n\t\t\t\t\ttable[INFO[k][a]] = count++; //この集合での、便のindex\n\t\t\t\t\tMASK += POW[INFO[k][a]];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tset<ll> SET; //ワープ地点を使う便の集合\n\n\t\tfor(int a = 0; a < BIN.size(); a++){\n\n\t\t\tSET.insert(POW[BIN[a]]);\n\t\t}\n\t\tfor(int a = 0; a < cross_STATE.size(); a++){\n\n\t\t\tSET.insert(MASK&cross_STATE[a]);\n\t\t}\n\n\t\t/*for(int a = 0; a < BIN.size()-1; a++){\n\t\t\tfor(int b = a+1; b < BIN.size(); b++){\n\n\t\t\t\tSET.insert(MASK&ADJ[BIN[a]][BIN[b]]);\n\t\t\t}\n\t\t}*/\n\n\t\tfor(auto tmp_S: SET){\n\n\t\t\tdouble sum = 0;\n\n\t\t\tvector<int> warp_vec;\n\n\t\t\tfor(int k = 0; k < BIN.size(); k++){\n\t\t\t\tif(tmp_S&POW[BIN[k]]){ //ワープを使う\n\n\t\t\t\t\twarp_vec.push_back(BIN[k]);\n\n\t\t\t\t}else{\n\n\t\t\t\t\tsum += DIST[BIN[k]];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdouble X = 0,Y = 0;\n\n\t\t\tfor(int E = 0; E < 2; E++){\n\n\t\t\t\tdouble bunshi = 0;\n\t\t\t\tdouble bunbo = 0;\n\n\t\t\t\tfor(int b = 0; b < warp_vec.size(); b++){\n\n\t\t\t\t\tint tmp_from = info[warp_vec[b]].from;\n\n\t\t\t\t\tif(E == 0){\n\n\t\t\t\t\t\tbunshi += point[tmp_from].x*info[warp_vec[b]].rev_V2;\n\n\t\t\t\t\t}else{\n\n\t\t\t\t\t\tbunshi += point[tmp_from].y*info[warp_vec[b]].rev_V2;\n\t\t\t\t\t}\n\t\t\t\t\tbunbo += info[warp_vec[b]].rev_V2;\n\t\t\t\t}\n\n\t\t\t\tif(E == 0){\n\n\t\t\t\t\tX = bunshi/bunbo;\n\t\t\t\t}else{\n\n\t\t\t\t\tY = bunshi/bunbo;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tPoint WARP_P = Point(X,Y); //ワープ地点\n\t\t\tfor(int b = 0; b < warp_vec.size(); b++){\n\t\t\t\tif(tmp_S&POW[warp_vec[b]]){\n\t\t\t\t\tsum += getT(point[info[warp_vec[b]].from],WARP_P,info[warp_vec[b]].V);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tdp[base_state] = min(dp[base_state],sum);\n\t\t}\n\n\t}\n\n\tdouble ans = HUGE_NUM;\n\n\tfor(int state = 0; state < POW[N]; state++){\n\n\t\tans = min(ans,dp[state]+dp[(POW[N]-1)-state]);\n\t}\n\n\tprintf(\"%.12lf\\n\",sqrt(ans/M));\n}\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i < SIZE; i++){\n\n\t\tPOW[i] = POW[i-1]*2;\n\t}\n\n\twhile(true){\n\n\t\tscanf(\"%d %d\",&N,&M);\n\t\tif(N == 0 && M == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4280, "memory_kb": 16128, "score_of_the_acc": -1.5384, "final_rank": 10 }, { "submission_id": "aoj_1614_3117995", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n C(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP unit(const P &p){\n return p/abs(p);\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nVP crosspointCC(C a, C b){\n VP ret;\n if(a.p == b.p && a.r == b.r){\n return ret;\n }\n if(a.r < b.r) swap(a,b);\n double dist = abs(b.p-a.p);\n P dir = a.r*unit(b.p-a.p);\n if(EQ(dist, a.r+b.r) || EQ(dist, a.r-b.r)){\n ret.push_back(a.p +dir);\n }else if(a.r-b.r < dist && dist < a.r+b.r){\n double cos = (a.r*a.r +dist*dist -b.r*b.r)/(2*a.r*dist);\n double sin = sqrt(1 -cos*cos);\n ret.push_back(a.p +dir*P(cos, sin));\n ret.push_back(a.p +dir*P(cos, -sin));\n }\n return ret;\n}\n\nstruct edge{\n int from,to;\n double v;\n edge(int f, int t, double v):from(f),to(t),v(v){}\n edge(){};\n};\n\nvector<int> linear_division(const VP &p){\n int n = p.size();\n vector<int> ret;\n for(int i=0; i<n; i++){\n for(int j=i+1; j<n; j++){\n int div = 0;\n vector<pair<P, int> > online;\n for(int k=0; k<n; k++){\n if(ccw(p[i], p[j], p[k]) == 1){\n div |= 1<<k;\n }else if(ccw(p[i], p[j], p[k]) != -1){\n online.emplace_back(p[k], k);\n }\n }\n sort(online.begin(), online.end());\n for(int d=0; d<2; d++){\n for(int k=0; k<(int)online.size(); k++){\n div ^= 1<<online[k].second;\n if((div & 1) == 0){\n ret.push_back(div ^ ((1<<n) -1));\n }else{\n ret.push_back(div);\n }\n }\n }\n }\n }\n sort(ret.begin(), ret.end());\n ret.erase(unique(ret.begin(), ret.end()), ret.end());\n return ret;\n}\n\nvector<long long int> circle_division(const VP &p, const vector<edge> &e){\n int m = e.size();\n vector<C> c(m);\n for(int i=0; i<m; i++){\n c[i] = C(p[e[i].from], abs(p[e[i].from] -p[e[i].to]));\n }\n vector<vector<double> > cpa(m);\n for(int i=0; i<m; i++){\n for(int j=0; j<m; j++){\n VP cp = crosspointCC(c[i], c[j]);\n for(const P &point: cp){\n cpa[i].push_back(arg(point -c[i].p));\n }\n }\n cpa[i].push_back(-PI);\n cpa[i].push_back(PI);\n sort(cpa[i].begin(), cpa[i].end());\n }\n\n VP cand;\n for(int i=0; i<m; i++){\n for(int j=0; j<(int)cpa[i].size()-1; j++){\n double angle = (cpa[i][j] +cpa[i][j+1]) /2;\n P rot = rotate(P(c[i].r, 0), angle);\n cand.push_back(c[i].p +rot +unit(rot)*EPS*10.0);\n cand.push_back(c[i].p +rot -unit(rot)*EPS*10.0);\n }\n }\n\n vector<long long int> ret;\n for(const P &point: cand){\n long long int div = 0;\n for(int i=0; i<m; i++){\n if(abs(point -c[i].p) < c[i].r +EPS){\n div |= 1LL<<i;\n }\n }\n ret.push_back(div);\n }\n sort(ret.begin(), ret.end());\n ret.erase(unique(ret.begin(), ret.end()), ret.end());\n return ret;\n}\n\ndouble solve_onewarp(const VP &p, const vector<edge> &e, long long int ldiv, const vector<long long int> &cdiv){\n int m = e.size();\n double ret = INF;\n for(long long int inout: cdiv){\n double weight = 0;\n P g(0, 0);\n for(int i=0; i<m; i++){\n if((1<<e[i].from & ldiv) == 0) continue;\n if((1LL<<i & inout) != 0){\n double w = 1/e[i].v /e[i].v;\n g += w *p[e[i].from];\n weight += w;\n }\n }\n g /= weight;\n double sub = 0;\n for(int i=0; i<m; i++){\n if((1<<e[i].from & ldiv) == 0) continue;\n if((1LL<<i & inout) != 0){\n sub += norm((p[e[i].from] -g) /e[i].v);\n }else{\n sub += norm((p[e[i].from] -p[e[i].to]) /e[i].v);\n }\n }\n ret = min(ret, sub);\n }\n return ret;\n}\n\ndouble solve_twowarp(const VP &p, const vector<edge> &e){\n vector<int> ldiv = linear_division(p);\n vector<long long int> cdiv = circle_division(p, e);\n double ret = INF;\n for(int bit: ldiv){\n int revbit = bit ^((1<<p.size()) -1);\n ret = min(ret, solve_onewarp(p, e, bit, cdiv) +solve_onewarp(p, e, revbit, cdiv));\n }\n return sqrt(ret /e.size());\n}\n\nint main(){\n while(1){\n int n,m;\n cin >> n >> m;\n if(n==0) break;\n\n VP p(n);\n for(int i=0; i<n; i++){\n double x,y;\n cin >> x >> y;\n p[i] = P(x, y);\n }\n vector<edge> e(m);\n for(int i=0; i<m; i++){\n cin >> e[i].from >> e[i].to >> e[i].v;\n e[i].from--; e[i].to--;\n }\n cout << fixed << setprecision(10);\n cout << solve_twowarp(p, e) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 1490, "memory_kb": 3964, "score_of_the_acc": -0.1568, "final_rank": 3 }, { "submission_id": "aoj_1614_3117977", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <complex>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <array>\nusing namespace std;\nconst double EPS = 1e-8;\nconst double INF = 1e12;\nconst double PI = acos(-1);\n#define EQ(n,m) (abs((n)-(m)) < EPS)\n#define X real()\n#define Y imag()\n\ntypedef complex<double> P;\ntypedef vector<P> VP;\nstruct L : array<P, 2>{\n L(const P& a, const P& b){ at(0)=a; at(1)=b; }\n L(){}\n};\nstruct C{\n P p;\n double r;\n C(const P& p, const double& r) : p(p), r(r) {}\n C(){}\n};\n\nnamespace std{\n bool operator < (const P& a, const P& b){\n return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y;\n }\n bool operator == (const P& a, const P& b){\n return abs(a-b) < EPS;\n }\n}\n\ndouble dot(P a, P b){\n return (conj(a)*b).X;\n}\ndouble cross(P a, P b){\n return (conj(a)*b).Y;\n}\nint ccw(P a, P b, P c){\n b -= a;\n c -= a;\n if(cross(b,c) > EPS) return +1; //ccw\n if(cross(b,c) < -EPS) return -1; //cw\n if(dot(b,c) < -EPS) return +2; //c-a-b\n if(abs(c)-abs(b) > EPS) return -2; //a-b-c\n return 0; //a-c-b\n}\nP unit(const P &p){\n return p/abs(p);\n}\nP rotate(const P &p, double rad){\n return p *P(cos(rad), sin(rad));\n}\n\nVP crosspointCC(C a, C b){\n VP ret;\n if(a.p == b.p && a.r == b.r){\n return ret;\n }\n if(a.r < b.r) swap(a,b);\n double dist = abs(b.p-a.p);\n P dir = a.r*unit(b.p-a.p);\n if(EQ(dist, a.r+b.r) || EQ(dist, a.r-b.r)){\n ret.push_back(a.p +dir);\n }else if(a.r-b.r < dist && dist < a.r+b.r){\n double cos = (a.r*a.r +dist*dist -b.r*b.r)/(2*a.r*dist);\n double sin = sqrt(1 -cos*cos);\n ret.push_back(a.p +dir*P(cos, sin));\n ret.push_back(a.p +dir*P(cos, -sin));\n }\n return ret;\n}\n\nstruct edge{\n int from,to;\n double v;\n edge(int f, int t, double v):from(f),to(t),v(v){}\n edge(){};\n};\n\nvector<int> linear_division(const VP &p){\n int n = p.size();\n vector<int> ret;\n for(int i=0; i<n; i++){\n for(int j=i+1; j<n; j++){\n int ccwbit = 0;\n vector<pair<P, int> > online;\n for(int k=0; k<n; k++){\n if(ccw(p[i], p[j], p[k]) == 1){\n ccwbit |= 1<<k;\n }else if(ccw(p[i], p[j], p[k]) != -1){\n online.emplace_back(p[k], k);\n }\n }\n sort(online.begin(), online.end());\n for(int d=0; d<2; d++){\n for(int k=0; k<(int)online.size(); k++){\n ccwbit ^= 1<<online[k].second;\n if((ccwbit & 1) == 0){\n ret.push_back(ccwbit ^ ((1<<n) -1));\n }else{\n ret.push_back(ccwbit);\n }\n }\n }\n }\n }\n sort(ret.begin(), ret.end());\n ret.erase(unique(ret.begin(), ret.end()), ret.end());\n return ret;\n}\n\ndouble solve_onewarp(const VP &p, const vector<edge> &e){\n int m = e.size();\n if(m == 0) return 0.0;\n\n vector<C> c(m);\n for(int i=0; i<m; i++){\n c[i] = C(p[e[i].from], abs(p[e[i].from] -p[e[i].to]));\n }\n vector<vector<double> > cpa(m);\n for(int i=0; i<m; i++){\n for(int j=0; j<m; j++){\n VP cp = crosspointCC(c[i], c[j]);\n for(const P &point: cp){\n cpa[i].push_back(arg(point -c[i].p));\n }\n }\n cpa[i].push_back(-PI);\n cpa[i].push_back(PI);\n sort(cpa[i].begin(), cpa[i].end());\n }\n\n VP cand;\n for(int i=0; i<m; i++){\n for(int j=0; j<(int)cpa[i].size()-1; j++){\n double angle = (cpa[i][j] +cpa[i][j+1]) /2;\n P rot = rotate(P(c[i].r, 0), angle);\n cand.push_back(c[i].p +rot +unit(rot)*EPS*10.0);\n cand.push_back(c[i].p +rot -unit(rot)*EPS*10.0);\n }\n }\n\n double ret = INF;\n for(const P &point: cand){\n long long int inout = 0;\n double weight = 0;\n P g(0, 0);\n for(int i=0; i<m; i++){\n if(abs(point -c[i].p) < c[i].r +EPS){\n double w = 1/e[i].v /e[i].v;\n g += w *c[i].p;\n inout |= 1LL<<i;\n weight += w;\n }\n }\n g /= weight;\n double sub = 0;\n for(int i=0; i<m; i++){\n if((1LL<<i & inout) != 0){\n sub += norm((p[e[i].from] -g) /e[i].v);\n }else{\n sub += norm((p[e[i].from] -p[e[i].to]) /e[i].v);\n }\n }\n ret = min(ret, sub);\n }\n return ret;\n}\n\ndouble solve_twowarp(const VP &p, const vector<edge> &e){\n vector<int> division = linear_division(p);\n double ret = INF;\n for(int bit: division){\n vector<vector<edge> > newe(2);\n for(const edge &ed: e){\n newe[(1<<ed.from & bit) != 0].push_back(ed);\n }\n ret = min(ret, solve_onewarp(p, newe[0]) +solve_onewarp(p, newe[1]));\n }\n return sqrt(ret /e.size());\n}\n\nint main(){\n while(1){\n int n,m;\n cin >> n >> m;\n if(n==0) break;\n\n VP p(n);\n for(int i=0; i<n; i++){\n double x,y;\n cin >> x >> y;\n p[i] = P(x, y);\n }\n vector<edge> e(m);\n for(int i=0; i<m; i++){\n cin >> e[i].from >> e[i].to >> e[i].v;\n e[i].from--; e[i].to--;\n }\n cout << fixed << setprecision(10);\n cout << solve_twowarp(p, e) << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5670, "memory_kb": 3816, "score_of_the_acc": -1, "final_rank": 7 }, { "submission_id": "aoj_1614_2160066", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\n\nclass Point{\npublic:\n double x, y;\n\n Point(double x = 0.0, double y = 0.0) : x(x), y(y){}\n\n Point operator + (const Point& p) const{ return Point(x + p.x, y + p.y); }\n Point& operator += (const Point& p){ x += p.x; y += p.y; return *this;}\n Point operator - (const Point& p) const{ return Point(x - p.x, y - p.y); }\n Point& operator -= (const Point& p){ x -= p.x; y -= p.y; return *this;}\n Point operator * (double a) const{ return Point(a*x, a*y); }\n Point& operator *= (double a){ x *= a; y *= a; return *this;}\n Point operator / (double a) const{ return Point(x / a, y / a); }\n Point& operator /= (double a){ x /= a; y /= a; return *this;}\n\n double abs(){ return sqrt(norm()); }\n double norm(){ return x*x + y*y; }\n};\n\nclass Circle{\npublic:\n\tPoint c;\n\tdouble r;\n};\ntypedef Point Vector;\n\ndouble dot(Vector a, Vector b){\n\treturn a.x*b.x + a.y*b.y;\n}\ndouble cross(Vector a, Vector b){\n\treturn a.x*b.y - a.y*b.x;\n}\nPoint rotate(Point p,double angle){\n return Point(p.x*cos(angle) - p.y*sin(angle),\n\t\t\t p.x*sin(angle) + p.y*cos(angle));\n}\n\npair<Point, Point> getCrossPoints(const Circle& c1, const Circle& c2){\n double d = (c2.c - c1.c).abs();\n double a = (c1.r*c1.r + d*d - c2.r*c2.r) / (2*d);\n Vector e = (c2.c - c1.c) / d;\n Vector h = rotate(e, PI/2) * sqrt(max(c1.r*c1.r - a*a, 0.));\n if(d + min(c1.r, c2.r) <= max(c1.r, c2.r))\n\treturn MP(c1.c + e*c1.r, c1.c + e*c1.r);\n\n return MP(c1.c + e*a + h, c1.c + e*a - h);\n}\n\n// ??????????????§?????¢???????????????\n// ?????????????????°???O(N^2)\nvoid partCircles(const vector<Circle>& cs, set<LL>& res){\n int N = SZ(cs);\n vector<Point> ps;\n REP(i,N) REP(j,N){\n\tif(i == j) continue;\n\tauto cc = getCrossPoints(cs[i], cs[j]);\n\tPoint crossPoints[2] = {cc.FF, cc.SS};\n\tREP(k,2){\n\t Vector delta = crossPoints[1-k] - crossPoints[k];\n\t if(delta.abs() == 0)\n\t\tdelta = crossPoints[k] - cs[i].c;\n\t delta /= delta.abs();\n\t REP(d,4){\n\t\tps.PB(crossPoints[k] + delta*EPS);\n\t\tdelta = rotate(delta, PI/2);\n\t }\n\t}\n }\n\n for(auto&& p: ps){\n\tLL bit = 0;\n\tREP(i,N)\n\t if((cs[i].c - p).abs() < cs[i].r)\n\t\tbit |= 1LL<<i;\n\tres.insert(bit);\n }\n}\n\n// ?????¢?????´?????§??????????????¨????????????????????????????????????\n// ????????????O(N^2)\nvoid partPoints(vector<Point> ps, set<PLL>& res){\n int N = SZ(ps);\n REP(i,N){\n\tVector e(0,1);\n\tREP(d,4){\n\t ps.PB(ps[i]+e*EPS);\n\t e = rotate(e, PI/2);\n\t}\n }\n \n REP(i,SZ(ps)) REP(j,SZ(ps)){\n\tif(i == j) continue;\n\tPoint p1 = ps[i];\n\tPoint p2 = ps[j];\n\tVector e = p2 - p1;\n\te /= e.abs();\n\te = rotate(e, PI/2);\n\tLL b1 = 0, b2 = 0;\n\tREP(k,N)\n\t if(dot(e, ps[k]-p1) >= 0)\n\t\tb1 |= 1LL<<k;\n\t else\n\t\tb2 |= 1LL<<k;\n\tif(b1 > b2) swap(b1, b2);\n\tres.insert(MP(b1,b2));\n }\n}\n\nint N, M;\ndouble calcBest(LL bps, LL bcs, VI& as, vector<double>& vs, vector<Circle>& cs){\n Point q(0, 0);\n double inv = 0.;\n REP(i,M){\n\tif((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n\tdouble tmp = 1. / (vs[i] * vs[i]);\n\tq.x += cs[i].c.x * tmp;\n\tq.y += cs[i].c.y * tmp;\n\tinv += tmp;\n }\n if(inv <= 0) return 0;\n q.x /= inv;\n q.y /= inv;\n\n double res = 0.;\n REP(i,M){\n\tif((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n\tres += ((cs[i].c - q).norm() - (cs[i].r * cs[i].r)) / (vs[i] * vs[i]);\n }\n return res;\n}\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n while(cin>>N>>M,N){\n\tvector<Point> ps(N);\n\tREP(i,N) cin >> ps[i].x >> ps[i].y;\n\tVI as(M), bs(M);\n\tvector<double> vs(M);\n\tvector<Circle> cs(M);\n\tREP(i,M){\n\t cin >> as[i] >> bs[i] >> vs[i];\n\t --as[i];\n\t --bs[i];\n\t cs[i].c = ps[as[i]];\n\t cs[i].r = (ps[bs[i]] - ps[as[i]]).abs();\n\t}\n\tif(M <= 2){\n\t cout << fixed << setprecision(9) << 0. << endl;\n\t continue;\n\t}\n\n\tset<LL> planes;\n\tset<PLL> points;\n\tpartCircles(cs, planes);\n\tpartPoints(ps, points);\n\n\tdouble best = 0.;\n\tfor(auto&& bpos: points){\n\t double d1 = 0, d2 = 0;\n\t for(auto&& bpls: planes){\n\t\tmini(d1, calcBest(bpos.FF, bpls, as, vs, cs));\n\t\tmini(d2, calcBest(bpos.SS, bpls, as, vs, cs));\n\t }\n\t mini(best, d1+d2);\n\t}\n\n\tdouble ans = 0;\n\tREP(i,M) ans += pow(cs[i].r / vs[i], 2);\n\tcout << fixed << setprecision(9) << sqrt((ans+best)/M) << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 3928, "score_of_the_acc": -0.0095, "final_rank": 1 }, { "submission_id": "aoj_1614_2160062", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n \nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n \n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n \n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n \n#define FF first\n#define SS second\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n \nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\n \nclass Point{\npublic:\n double x, y;\n \n Point(double x = 0.0, double y = 0.0) : x(x), y(y){}\n \n Point operator + (const Point& p) const{ return Point(x + p.x, y + p.y); }\n Point& operator += (const Point& p){ x += p.x; y += p.y; return *this;}\n Point operator - (const Point& p) const{ return Point(x - p.x, y - p.y); }\n Point& operator -= (const Point& p){ x -= p.x; y -= p.y; return *this;}\n Point operator * (double a) const{ return Point(a*x, a*y); }\n Point& operator *= (double a){ x *= a; y *= a; return *this;}\n Point operator / (double a) const{ return Point(x / a, y / a); }\n Point& operator /= (double a){ x /= a; y /= a; return *this;}\n \n double abs(){ return sqrt(norm()); }\n double norm(){ return x*x + y*y; }\n};\n \nclass Circle{\npublic:\n Point c;\n double r;\n};\ntypedef Point Vector;\n \ndouble dot(Vector a, Vector b){\n return a.x*b.x + a.y*b.y;\n}\ndouble cross(Vector a, Vector b){\n return a.x*b.y - a.y*b.x;\n}\nPoint rotate(Point p,double angle){\n return Point(p.x*cos(angle) - p.y*sin(angle),\n p.x*sin(angle) + p.y*cos(angle));\n}\n \npair<Point, Point> getCrossPoints(const Circle& c1, const Circle& c2){\n double d = (c2.c - c1.c).abs();\n double a = (c1.r*c1.r + d*d - c2.r*c2.r) / (2*d);\n Vector e = (c2.c - c1.c) / d;\n Vector h = rotate(e, PI/2) * sqrt(max(c1.r*c1.r - a*a, 0.));\n\n return MP(c1.c + e*a + h, c1.c + e*a - h);\n}\n\nvoid partCircles(const vector<Circle>& cs, set<LL>& res){\n int N = SZ(cs);\n vector<Point> ps;\n REP(i,N) REP(j,N){\n if(i == j) continue;\n auto cc = getCrossPoints(cs[i], cs[j]);\n Point crossPoints[2] = {cc.FF, cc.SS};\n REP(k,2){\n Vector delta = crossPoints[1-k] - crossPoints[k];\n if(delta.abs() == 0)\n delta = crossPoints[k] - cs[i].c;\n delta /= delta.abs();\n REP(d,4){\n ps.PB(crossPoints[k] + delta*EPS);\n delta = rotate(delta, PI/2);\n }\n }\n }\n \n for(auto&& p: ps){\n LL bit = 0;\n REP(i,N)\n if((cs[i].c - p).abs() < cs[i].r)\n bit |= 1LL<<i;\n res.insert(bit);\n }\n}\n \n\nvoid partPoints(vector<Point> ps, set<PLL>& res){\n int N = SZ(ps);\n REP(i,N){\n Vector e(0,1);\n REP(d,4){\n ps.PB(ps[i]+e*EPS);\n e = rotate(e, PI/2);\n }\n }\n \n REP(i,SZ(ps)) REP(j,SZ(ps)){\n if(i == j) continue;\n Point p1 = ps[i];\n Point p2 = ps[j];\n Vector e = p2 - p1;\n e /= e.abs();\n e = rotate(e, PI/2);\n LL b1 = 0, b2 = 0;\n REP(k,N)\n if(dot(e, ps[k]-p1) >= 0)\n b1 |= 1LL<<k;\n else\n b2 |= 1LL<<k;\n if(b1 > b2) swap(b1, b2);\n res.insert(MP(b1,b2));\n }\n}\n \nint N, M;\ndouble calcBest(LL bps, LL bcs, VI& as, vector<double>& vs, vector<Circle>& cs){\n Point q(0, 0);\n double inv = 0.;\n REP(i,M){\n if((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n double tmp = 1. / (vs[i] * vs[i]);\n q.x += cs[i].c.x * tmp;\n q.y += cs[i].c.y * tmp;\n inv += tmp;\n }\n if(inv <= 0) return 0;\n q.x /= inv;\n q.y /= inv;\n \n double res = 0.;\n REP(i,M){\n if((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n res += ((cs[i].c - q).norm() - (cs[i].r * cs[i].r)) / (vs[i] * vs[i]);\n }\n return res;\n}\n \nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n \n while(cin>>N>>M,N){\n vector<Point> ps(N);\n REP(i,N) cin >> ps[i].x >> ps[i].y;\n VI as(M), bs(M);\n vector<double> vs(M);\n vector<Circle> cs(M);\n REP(i,M){\n cin >> as[i] >> bs[i] >> vs[i];\n --as[i];\n --bs[i];\n cs[i].c = ps[as[i]];\n cs[i].r = (ps[bs[i]] - ps[as[i]]).abs();\n }\n if(M <= 2){\n cout << fixed << setprecision(9) << 0. << endl;\n continue;\n }\n \n set<LL> planes;\n set<PLL> points;\n partCircles(cs, planes);\n partPoints(ps, points);\n \n double best = 0.;\n for(auto&& bpos: points){\n double d1 = 0, d2 = 0;\n for(auto&& bpls: planes){\n mini(d1, calcBest(bpos.FF, bpls, as, vs, cs));\n mini(d2, calcBest(bpos.SS, bpls, as, vs, cs));\n }\n mini(best, d1+d2);\n }\n \n double ans = 0;\n REP(i,M) ans += pow(cs[i].r / vs[i], 2);\n cout << fixed << setprecision(9) << sqrt((ans+best)/M) << endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 770, "memory_kb": 3968, "score_of_the_acc": -0.0101, "final_rank": 2 }, { "submission_id": "aoj_1614_2160045", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\n\nclass Point{\npublic:\n double x, y;\n\n Point(double x = 0.0, double y = 0.0) : x(x), y(y){}\n\n Point operator + (const Point& p) const{ return Point(x + p.x, y + p.y); }\n Point& operator += (const Point& p){ x += p.x; y += p.y; return *this;}\n Point operator - (const Point& p) const{ return Point(x - p.x, y - p.y); }\n Point& operator -= (const Point& p){ x -= p.x; y -= p.y; return *this;}\n Point operator * (double a) const{ return Point(a*x, a*y); }\n Point& operator *= (double a){ x *= a; y *= a; return *this;}\n Point operator / (double a) const{ return Point(x / a, y / a); }\n Point& operator /= (double a){ x /= a; y /= a; return *this;}\n\n double abs(){ return sqrt(norm()); }\n double norm(){ return x*x + y*y; }\n};\n\nclass Circle{\npublic:\n\tPoint c;\n\tdouble r;\n};\ntypedef Point Vector;\n\ndouble dot(Vector a, Vector b){\n\treturn a.x*b.x + a.y*b.y;\n}\ndouble cross(Vector a, Vector b){\n\treturn a.x*b.y - a.y*b.x;\n}\nPoint rotate(Point p,double angle){\n return Point(p.x*cos(angle) - p.y*sin(angle),\n\t\t\t p.x*sin(angle) + p.y*cos(angle));\n}\n\npair<Point, Point> getCrossPoints(const Circle& c1, const Circle& c2){\n double d = (c2.c - c1.c).abs();\n double a = (c1.r*c1.r + d*d - c2.r*c2.r) / (2*d);\n Vector e = (c2.c - c1.c) / d;\n Vector h = rotate(e, PI/2) * sqrt(max(c1.r*c1.r - a*a, 0.));\n if(d + min(c1.r, c2.r) <= max(c1.r, c2.r))\n\treturn MP(c1.c + e*c1.r, c1.c + e*c1.r);\n\n return MP(c1.c + e*a + h, c1.c + e*a - h);\n}\n\n// ??????????????§?????¢???????????????\n// ?????????????????°???O(N^2)\nvoid partCircles(const vector<Circle>& cs, set<LL>& res){\n int N = SZ(cs);\n vector<Point> ps;\n REP(i,N) REP(j,N){\n\tif(i == j) continue;\n\tauto cc = getCrossPoints(cs[i], cs[j]);\n\tPoint crossPoints[2] = {cc.FF, cc.SS};\n\tREP(k,2){\n\t Vector delta = crossPoints[1-k] - crossPoints[k];\n\t if(delta.abs() == 0)\n\t\tdelta = crossPoints[k] - cs[i].c;\n\t delta /= delta.abs();\n\t REP(d,4){\n\t\tps.PB(crossPoints[k] + delta*EPS);\n\t\tdelta = rotate(delta, PI/2);\n\t }\n\t}\n }\n\n for(auto&& p: ps){\n\tLL bit = 0;\n\tREP(i,N)\n\t if((cs[i].c - p).abs() < cs[i].r)\n\t\tbit |= 1LL<<i;\n\tres.insert(bit);\n }\n}\n\n// ?????¢?????´?????§??????????????¨????????????????????????????????????\n// ????????????O(N^2)\nvoid partPoints(vector<Point> ps, set<PLL>& res){\n int N = SZ(ps);\n REP(i,N){\n\tVector e(0,1);\n\tREP(d,4){\n\t ps.PB(ps[i]+e*EPS);\n\t e = rotate(e, PI/2);\n\t}\n }\n \n REP(i,SZ(ps)) REP(j,SZ(ps)){\n\tif(i == j) continue;\n\tPoint p1 = ps[i];\n\tPoint p2 = ps[j];\n\tVector e = p2 - p1;\n\te /= e.abs();\n\te = rotate(e, PI/2);\n\tLL b1 = 0, b2 = 0;\n\tREP(k,N)\n\t if(dot(e, ps[k]-p1) >= 0)\n\t\tb1 |= 1LL<<k;\n\t else\n\t\tb2 |= 1LL<<k;\n\tif(b1 > b2) swap(b1, b2);\n\tres.insert(MP(b1,b2));\n }\n}\n\nint N, M;\ndouble calcBest(LL bps, LL bcs, VI& as, vector<double>& vs, vector<Circle>& cs){\n Point q(0, 0);\n double inv = 0.;\n REP(i,M){\n\tif((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n\tdouble tmp = 1. / (vs[i] * vs[i]);\n\tq.x += cs[i].c.x * tmp;\n\tq.y += cs[i].c.y * tmp;\n\tinv += tmp;\n }\n if(inv <= 0) return 0;\n q.x /= inv;\n q.y /= inv;\n\n double res = 0.;\n REP(i,M){\n\tif((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n\tres += ((cs[i].c - q).norm() - (cs[i].r * cs[i].r)) / (vs[i] * vs[i]);\n }\n return res;\n}\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n while(cin>>N>>M,N){\n\tvector<Point> ps(N);\n\tREP(i,N) cin >> ps[i].x >> ps[i].y;\n\tVI as(M), bs(M);\n\tvector<double> vs(M);\n\tvector<Circle> cs(M);\n\tREP(i,M){\n\t cin >> as[i] >> bs[i] >> vs[i];\n\t --as[i];\n\t --bs[i];\n\t cs[i].c = ps[as[i]];\n\t cs[i].r = (ps[bs[i]] - ps[as[i]]).abs();\n\t}\n\n\tset<LL> planes;\n\tset<PLL> points;\n\tpartCircles(cs, planes);\n\tpartPoints(ps, points);\n\n\tdouble best = 0.;\n\tfor(auto&& bpos: points){\n\t double d1 = 0, d2 = 0;\n\t for(auto&& bpls: planes){\n\t\tmini(d1, calcBest(bpos.FF, bpls, as, vs, cs));\n\t\tmini(d2, calcBest(bpos.SS, bpls, as, vs, cs));\n\t }\n\t mini(best, d1+d2);\n\t}\n\n\tdouble ans = 0;\n\tREP(i,M) ans += pow(cs[i].r / vs[i], 2);\n\tcout << fixed << setprecision(9) << sqrt((ans+best)/M) << endl;\n }\n\n return 0;\n}", "accuracy": 0.5, "time_ms": 840, "memory_kb": 4024, "score_of_the_acc": -0.0282, "final_rank": 12 }, { "submission_id": "aoj_1614_2160034", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<string>;\n\n#define ALL(a) begin((a)),end((a))\n#define RALL(a) (a).rbegin(), (a).rend()\n#define PB push_back\n#define EB emplace_back\n#define MP make_pair\n#define SZ(a) int((a).size())\n#define SORT(c) sort(ALL((c)))\n#define RSORT(c) sort(RALL((c)))\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n)\n\n#define FF first\n#define SS second\ntemplate<class S, class T>\nistream& operator>>(istream& is, pair<S,T>& p){\n return is >> p.FF >> p.SS;\n}\ntemplate<class S, class T>\nostream& operator<<(ostream& os, pair<S,T>& p){\n return os << p.FF << \" \" << p.SS;\n}\ntemplate<class T>\nvoid maxi(T& x, T y){\n if(x < y) x = y;\n}\ntemplate<class T>\nvoid mini(T& x, T y){\n if(x > y) x = y;\n}\n\nconst double EPS = 1e-10;\nconst double PI = acos(-1.0);\nconst LL MOD = 1e9+7;\n\nclass Point{\npublic:\n double x, y;\n\n Point(double x = 0.0, double y = 0.0) : x(x), y(y){}\n\n Point operator + (const Point& p) const{ return Point(x + p.x, y + p.y); }\n Point& operator += (const Point& p){ x += p.x; y += p.y; return *this;}\n Point operator - (const Point& p) const{ return Point(x - p.x, y - p.y); }\n Point& operator -= (const Point& p){ x -= p.x; y -= p.y; return *this;}\n Point operator * (double a) const{ return Point(a*x, a*y); }\n Point& operator *= (double a){ x *= a; y *= a; return *this;}\n Point operator / (double a) const{ return Point(x / a, y / a); }\n Point& operator /= (double a){ x /= a; y /= a; return *this;}\n\n double abs(){ return sqrt(norm()); }\n double norm(){ return x*x + y*y; }\n};\n\nclass Circle{\npublic:\n\tPoint c;\n\tdouble r;\n};\ntypedef Point Vector;\n\ndouble dot(Vector a, Vector b){\n\treturn a.x*b.x + a.y*b.y;\n}\ndouble cross(Vector a, Vector b){\n\treturn a.x*b.y - a.y*b.x;\n}\nPoint rotate(Point p,double angle){\n return Point(p.x*cos(angle) - p.y*sin(angle),\n\t\t\t p.x*sin(angle) + p.y*cos(angle));\n}\n\npair<Point, Point> getCrossPoints(const Circle& c1, const Circle& c2){\n double d = (c2.c - c1.c).abs();\n double a = (c1.r*c1.r + d*d - c2.r*c2.r) / (2*d);\n Vector e = (c2.c - c1.c) / d;\n Vector h = rotate(e, PI/2) * sqrt(max(c1.r*c1.r - a*a, 0.));\n return make_pair(c1.c + e*a + h, c1.c + e*a - h);\n}\n\n// ??????????????§?????¢???????????????\n// ?????????????????°???O(N^2)\nvoid partCircles(const vector<Circle>& cs, set<LL>& res){\n int N = SZ(cs);\n vector<Point> ps;\n REP(i,N) REP(j,N){\n\tif(i == j) continue;\n\tauto cc = getCrossPoints(cs[i], cs[j]);\n\tPoint crossPoints[2] = {cc.FF, cc.SS};\n\tREP(k,2){\n\t Vector delta = crossPoints[1-k] - crossPoints[k];\n\t if(delta.abs() == 0) delta.x = 1;\n\t delta /= delta.abs();\n\t REP(d,4){\n\t\tps.PB(crossPoints[k] + delta*EPS);\n\t\tdelta = rotate(delta, PI/2);\n\t }\n\t}\n }\n\n for(auto&& p: ps){\n\tLL bit = 0;\n\tREP(i,N)\n\t if((cs[i].c - p).abs() < cs[i].r)\n\t\tbit |= 1LL<<i;\n\tres.insert(bit);\n }\n}\n\n// ?????¢?????´?????§??????????????¨????????????????????????????????????\n// ????????????O(N^2)\nvoid partPoints(vector<Point> ps, set<PLL>& res){\n int N = SZ(ps);\n REP(i,N){\n\tVector e(0,1);\n\tREP(d,4){\n\t ps.PB(ps[i]+e*EPS);\n\t e = rotate(e, PI/2);\n\t}\n }\n \n REP(i,SZ(ps)) REP(j,SZ(ps)){\n\tif(i == j) continue;\n\tPoint p1 = ps[i];\n\tPoint p2 = ps[j];\n\tVector e = p2 - p1;\n\te /= e.abs();\n\te = rotate(e, PI/2);\n\tLL b1 = 0, b2 = 0;\n\tREP(k,N)\n\t if(dot(e, ps[k]-p1) >= 0)\n\t\tb1 |= 1LL<<k;\n\t else\n\t\tb2 |= 1LL<<k;\n\tif(b1 > b2) swap(b1, b2);\n\tres.insert(MP(b1,b2));\n }\n}\n\nint N, M;\ndouble calcBest(LL bps, LL bcs, VI& as, vector<double>& vs, vector<Circle>& cs){\n Point q(0, 0);\n double inv = 0.;\n REP(i,M){\n\tif((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n\tdouble tmp = 1. / (vs[i] * vs[i]);\n\tq.x += cs[i].c.x * tmp;\n\tq.y += cs[i].c.y * tmp;\n\tinv += tmp;\n }\n if(inv <= 0) return 0;\n q.x /= inv;\n q.y /= inv;\n\n double res = 0.;\n REP(i,M){\n\tif((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n\tres += ((cs[i].c - q).norm() - (cs[i].r * cs[i].r)) / (vs[i] * vs[i]);\n }\n return res;\n}\nPoint calcBestQ(LL bps, LL bcs, VI& as, vector<double>& vs, vector<Circle>& cs){\n Point q(0, 0);\n double inv = 0.;\n REP(i,M){\n\tif((bcs>>i&1) == 0 || (bps>>as[i]&1) == 0) continue;\n\tdouble tmp = 1. / (vs[i] * vs[i]);\n\tq.x += cs[i].c.x * tmp;\n\tq.y += cs[i].c.y * tmp;\n\tinv += tmp;\n }\n if(inv <= 0) return 0;\n q.x /= inv;\n q.y /= inv;\n\n return q;\n}\n\nint main(){\n cin.tie(0);\n ios_base::sync_with_stdio(false);\n\n int ccc=0;\n while(cin>>N>>M,N){\n\t++ccc;\n\tvector<Point> ps(N);\n\tREP(i,N) cin >> ps[i].x >> ps[i].y;\n\tVI as(M), bs(M);\n\tvector<double> vs(M);\n\tvector<Circle> cs(M);\n\tREP(i,M){\n\t cin >> as[i] >> bs[i] >> vs[i];\n\t --as[i];\n\t --bs[i];\n\t cs[i].c = ps[as[i]];\n\t cs[i].r = (ps[bs[i]] - ps[as[i]]).abs();\n\t}\n\n\tset<LL> planes;\n\tset<PLL> points;\n\tpartCircles(cs, planes);\n\tpartPoints(ps, points);\n\n\tdouble best = 0.;\n\tfor(auto&& bpos: points){\n\t double d1 = 0, d2 = 0;\n\t for(auto&& bpls: planes){\n\t\tmini(d1, calcBest(bpos.FF, bpls, as, vs, cs));\n\t\tmini(d2, calcBest(bpos.SS, bpls, as, vs, cs));\n\t }\n\t mini(best, d1+d2);\n\t}\n\n\tdouble ans = 0;\n\tREP(i,M) ans += pow(cs[i].r / vs[i], 2);\n\tcout << fixed << setprecision(9) << sqrt((ans+best)/M) << endl;\n }\n\n return 0;\n}", "accuracy": 0.5, "time_ms": 830, "memory_kb": 4024, "score_of_the_acc": -0.0261, "final_rank": 11 } ]
aoj_1612_cpp
3D Printing We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction. First, using a CAD system, we prepare n ( n ≥ k ) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied. Each cube may overlap zero, one or two other cubes, but not three or more. When a cube overlap two other cubes, those two cubes do not overlap. Two non-overlapping cubes do not touch at their surfaces, edges or corners. Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area. Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones. Figure E1. A polyhedron formed with connected identical cubes. Input The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format. n k s x 1 y 1 z 1 ... x n y n z n In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i -th line, there are three integers x i , y i and z i that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied. The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×10 7 ≤ x i , y i , z i ≤ 4×10 7 . The end of the input is indicated by a line containing three zeros separated by a space. Output For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1. Sample Input 1 1 100 100 100 100 6 4 10 100 100 100 106 102 102 112 110 104 104 116 102 100 114 104 92 107 100 10 4 10 -100 101 100 -108 102 120 -116 103 100 -124 100 100 -132 99 100 -92 98 100 -84 100 140 -76 103 100 -68 102 100 -60 101 100 10 4 10 100 100 100 108 101 100 116 102 100 124 100 100 132 102 100 200 100 103 192 100 102 184 100 101 176 100 100 168 100 103 4 4 10 100 100 100 108 94 100 1 ...(truncated)
[ { "submission_id": "aoj_1612_10578406", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n/* BOOST MULTIPRECISION\n#include<boost/multiprecision/cpp_int.hpp>\nusing namespace boost::multiprecision;\n//*/\n\ntypedef long long ll;\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) begin(v),end(v)\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n\tif (a <= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> bool chmax(T &a, const T &b) {\n\tif (a >= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> T max(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T min(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T sum(vector<T> &a){\n\tT ret = 0;\n\tfor (int i=0; i<(int)a.size(); i++) ret += a[i];\n\treturn ret;\n}\n\n// importbisect\ntemplate <typename T>\nint bisect_left(vector<T> &X, T v){\n\treturn lower_bound(X.begin(), X.end(), v) - X.begin();\n}\n\ntemplate <typename T>\nint bisect_right(vector<T> &X, T v){\n\treturn upper_bound(X.begin(), X.end(), v) - X.begin();\n}\n// ----\n\n// defcomp\ntemplate <typename T>\nvector<T> compress(vector<T> &X) {\n\tvector<T> vals = X;\n\tsort(vals.begin(), vals.end());\n\tvals.erase(unique(vals.begin(), vals.end()), vals.end());\n\treturn vals;\n}\n// -----\n\n// makediv\nvector<ll> makediv(ll n){\n\tvector<ll> ld, ud;\n\tfor (ll i=1; i*i<=n; i++){\n\t\tif (n%i == 0){\n\t\t\tld.push_back(i);\n\t\t\tif (i != n/i){\n\t\t\t\tud.push_back(n/i);\n\t\t\t}\n\t\t}\n\t}\n\treverse(ud.begin(), ud.end());\n\tld.insert(ld.end(), ud.begin(), ud.end());\n\treturn ld;\n}\n// -----\n\n// Fast Factorization\n// https://judge.yosupo.jp/submission/38126\n// !!! CHANGED THE PRIMARY TEST !!!\ntypedef unsigned int uint;\n\nstruct Mint {\n\tuint64_t n;\n\tstatic uint64_t mod, inv, r2;\n\tMint() : n(0) { }\n\tMint(const uint64_t &x) : n(init(x)) { }\n\tstatic uint64_t init(const uint64_t &w) { return reduce(__uint128_t(w) * r2); }\n\tstatic void set_mod(const uint64_t &m) {\n\t\tmod = inv = m;\n\t\tfor(int i = 0; i < 5; i++)\tinv *= 2 - inv * m;\n\t\tr2 = -__uint128_t(m) % m;\n\t}\n\tstatic uint64_t reduce(const __uint128_t &x) {\n\t\tuint64_t y = uint64_t(x >> 64) - uint64_t((__uint128_t(uint64_t(x) * inv) * mod) >> 64);\n\t\treturn int64_t(y) < 0 ? y + mod : y;\n\t}\n\tMint& operator+= (const Mint &x) { n += x.n - mod; if(int64_t(n) < 0) n += mod; return *this; }\n\tMint& operator+ (const Mint &x) const { return Mint(*this) += x; }\n\tMint& operator*= (const Mint &x) { n = reduce(__uint128_t(n) * x.n); return *this; }\n\tMint& operator* (const Mint &x) const { return Mint(*this) *= x; }\n\tuint64_t val() const { return reduce(n); }\n};\n\nuint64_t Mint::mod, Mint::inv, Mint::r2;\n\nbool suspect(const uint64_t &a, const uint64_t &s, uint64_t d, const uint64_t &n) {\n\tif(Mint::mod != n)\tMint::set_mod(n);\n\tMint x(1), xx(a), o(x), m(n - 1);\n\twhile(d > 0) {\n\t\tif(d & 1)\tx *= xx;\n\t\txx *= xx;\n\t\td >>= 1;\n\t}\n\tif(x.n == o.n)\treturn true;\n\tfor(uint r = 0; r < s; r++) {\n\t\tif(x.n == m.n)\treturn true;\n\t\tx *= x;\n\t}\n\treturn false;\n}\n\nbool is_prime(const uint64_t &n) {\n\tif(n <= 1 || (n > 2 && (~n & 1)))\treturn false;\n\tuint64_t d = n - 1, s = 0;\n\twhile(~d & 1)\ts++, d >>= 1;\n\tstatic const uint64_t a_big[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};\n\tstatic const uint64_t a_smo[] = {2, 7, 61};\n\tif(n < 4759123141LL) {\n\t\tfor(auto &&p : a_smo) {\n\t\t\tif(p >= n)\tbreak;\n\t\t\tif(!suspect(p, s, d, n))\treturn false;\n\t\t}\n\t} else {\n\t\tfor(auto &&p : a_big) {\n\t\t\tif(p >= n)\tbreak;\n\t\t\tif(!suspect(p, s, d, n))\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nuint64_t rho_pollard(const uint64_t &n) {\n\tif(~n & 1)\treturn 2u;\n\tstatic random_device rng;\n\tuniform_int_distribution<uint64_t> rr(1, n - 1);\n\tif(Mint::mod != n)\tMint::set_mod(n);\n\tfor(;;) {\n\t\tuint64_t c_ = rr(rng), g = 1, r = 1, m = 500;\n\t\tMint y(rr(rng)), xx(0), c(c_), ys(0), q(1);\n\t\twhile(g == 1) {\n\t\t\txx.n = y.n;\n\t\t\tfor(uint i = 1; i <= r; i++)\ty *= y, y += c;\n\t\t\tuint64_t k = 0; g = 1;\n\t\t\twhile(k < r && g == 1) {\n\t\t\t\tfor(uint i = 1; i <= (m > (r - k) ? (r - k) : m); i++) {\n\t\t\t\t\tys.n = y.n;\n\t\t\t\t\ty *= y; y += c;\n\t\t\t\t\tuint64_t xxx = xx.val(), yyy = y.val();\n\t\t\t\t\tq *= Mint(xxx > yyy ? xxx - yyy : yyy - xxx);\n\t\t\t\t}\n\t\t\t\tg = __gcd<uint64_t>(q.val(), n);\n\t\t\t\tk += m;\n\t\t\t}\n\t\t\tr *= 2;\n\t\t}\n\t\tif(g == n)\tg = 1;\n\t\twhile(g == 1) {\n\t\t\tys *= ys; ys += c;\n\t\t\tuint64_t xxx = xx.val(), yyy = ys.val();\n\t\t\tg = __gcd<uint64_t>(xxx > yyy ? xxx - yyy : yyy - xxx, n);\n\t\t}\n\t\tif(g != n && is_prime(g))\treturn g;\n\t}\n\tassert(69 == 420);\n}\n\ntemplate <typename T>\nvector<T> inter_factor(const T &n) {\n\tif(n < 2)\treturn { };\n\tif(is_prime(n))\treturn {n};\n\tT d = rho_pollard(static_cast<uint64_t>(n));\n\tvector<T> l = inter_factor(d), r = inter_factor(n / d);\n\tl.insert(l.end(), r.begin(), r.end());\n\treturn l;\n}\n\ntemplate <typename T>\nvector<T> factor(T n) {\n\tvector<T> f1;\n\tfor(uint i = 2; i < 100; i += (i & 1) + 1)\n\t\twhile(n % i == 0)\tf1.push_back(i), n /= i;\n\tvector<T> f2 = inter_factor(n);\n\tf1.insert(f1.end(), f2.begin(), f2.end());\n\tsort(f1.begin(), f1.end());\n\treturn f1;\n}\n\n\nvoid solve(int n, int k, ll s) {\n\tvector<ll> x(n), y(n), z(n);\n\trep(i,0,n) {\n\t\tcin >> x[i] >> y[i] >> z[i];\n\t}\n\n\tauto hyomen_sub = [&](ll ax, ll ay, ll bx, ll by) {\n\t\tll xl = max(ax, bx);\n\t\tll xr = min(ax + s, bx + s);\n\t\tll yl = max(ay, by);\n\t\tll yr = min(ay + s, by + s);\n\t\tll xlen = max(0LL, xr - xl);\n\t\tll ylen = max(0LL, yr - yl);\n\t\treturn ylen * xlen * 2;\n\t};\n\n\t// common hyomen of (i, j)\n\tauto hyomen = [&](int i, int j) -> ll {\n\t\t// x[i], y[i], z[i];\n\t\t// x[j], y[j], z[j];\n\t\tll ans = 0;\n\t\tans += hyomen_sub(x[i], y[i], x[j], y[j]);\n\t\tans += hyomen_sub(y[i], z[i], y[j], z[j]);\n\t\tans += hyomen_sub(z[i], x[i], z[j], x[j]);\n\t\treturn ans;\n\t};\n\n\tauto is_linked = [&](int i, int j) -> bool {\n\t\tif (hyomen_sub(x[i], y[i], x[j], y[j]) == 0) return false;\n\t\tif (hyomen_sub(y[i], z[i], y[j], z[j]) == 0) return false;\n\t\tif (hyomen_sub(z[i], x[i], z[j], x[j]) == 0) return false;\n\t\treturn true;\n\t};\n\n\tvector ikeru(n, vector<int>(0));\n\trep(i,0,n) {\n\t\trep(j,i+1,n) {\n\t\t\tif (i == j) continue;\n\t\t\tif (is_linked(i, j)) {\n\t\t\t\tikeru[i].push_back(j);\n\t\t\t\tikeru[j].push_back(i);\n\t\t\t}\n\t\t}\n\t}\n\n\tatcoder::dsu uf(n);\n\trep(i,0,n){\n\t\tfor (int j: ikeru[i]) {\n\t\t\tuf.merge(i, j);\n\t\t}\n\t}\n\t\n\tvector<int> deg_sum(n);\n\trep(i,0,n){\n\t\tdeg_sum[uf.leader(i)] += (int)ikeru[i].size();\n\t}\n\n\t// minimum answer\n\tll ans = 1e18;\n\n\trep(st,0,n) {\n\t\tif (uf.leader(st) != st) continue;\n\t\t//cout << deg_sum[st] << ' ' << uf.size(st) << endl;\n\t\tif (deg_sum[st] == uf.size(st) * 2) {\n\t\t\t// circle;\n\t\t\tint genten = st;\n\t\t\tvector<int> renketsu_list;\n\t\t\tvector<bool> seen(n);\n\t\t\tseen[genten] = 1;\n\t\t\t\n\t\t\tvector<int> mada(0);\n\t\t\tmada.push_back(genten);\n\t\t\twhile(!mada.empty()){\n\t\t\t\tint i = mada.back();\n\t\t\t\tmada.pop_back();\n\t\t\t\trenketsu_list.push_back(i);\n\t\t\t\tfor (int j: ikeru[i]) {\n\t\t\t\t\tif (seen[j]) continue;\n\t\t\t\t\tmada.push_back(j);\n\t\t\t\t\tseen[j] = 1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tint m = (int)renketsu_list.size();\n\t\t\trep(i,0,m-1) {\n\t\t\t\tassert(is_linked(renketsu_list[i],renketsu_list[i+1]));\n\t\t\t}\n\t\t\tassert(is_linked(renketsu_list[m-1],renketsu_list[0]));\n\t\t\t//cout << m << endl;\n\n\t\t\t// unused with originally genten -> 0\n\t\t\tvector dp_sub0(2*m, vector<ll>(m+1, (ll)1e18));\n\t\t\t// used with originally genten -> 0\n\t\t\tvector dp_sub1(2*m, vector<ll>(m+1, (ll)1e18));\n\t\t\tvector dp_sub2(2*m, vector<ll>(m+1, (ll)1e18));\n\t\t\tdp_sub0[0][0] = 0;\n\t\t\tdp_sub1[0][1] = 6*s*s;\n\t\t\trep(i,1,2*m) {\n\t\t\t\trep(j,0,m+1) {\n\t\t\t\t\tchmin(dp_sub0[i][j], dp_sub0[i-1][j]);\n\t\t\t\t\tchmin(dp_sub2[i][j], dp_sub1[i-1][j]);\n\t\t\t\t\tchmin(dp_sub2[i][j], dp_sub2[i-1][j]);\n\t\t\t\t}\n\t\t\t\trep(j,0,m) {\n\t\t\t\t\tchmin(dp_sub1[i][j+1], dp_sub0[i-1][j] + 6*s*s);\n\t\t\t\t\tchmin(dp_sub1[i][j+1], dp_sub1[i-1][j] + 6*s*s - hyomen(renketsu_list[(i-1)%m], renketsu_list[i%m]));\n\t\t\t\t}\n\t\t\t}\n\t\t\tll val = 0;\n\t\t\trep(i,0,m) {\n\t\t\t\tval += 6 * s * s;\n\t\t\t\tval -= hyomen(renketsu_list[i], renketsu_list[(i+1)%m]);\n\t\t\t}\n\t\t\tif (m==k) chmin(ans, val);\n\t\t\tif (m>k) {\n\t\t\t\tchmin(ans,dp_sub1[2*m-1][k]);\n\t\t\t\tchmin(ans,dp_sub2[2*m-1][k]);\n\t\t\t\t\n\t\t\t}\n\t\t}else if (deg_sum[st] == uf.size(st) * 2 - 2){\n\t\t\tif (deg_sum[st] == 0) {\n\t\t\t\tif (k == 1) {\n\t\t\t\t\tchmin(ans,6*s*s);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// path;\n\t\t\tint genten = -1;\n\t\t\trep(i,0,n) {\n\t\t\t\tif (!uf.same(i,st)) continue;\n\t\t\t\tif ((int)ikeru[i].size() == 1) {\n\t\t\t\t\tgenten = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(genten != -1);\n\t\t\t\n\t\t\tvector<int> renketsu_list;\n\t\t\tvector<bool> seen(n);\n\t\t\tseen[genten] = 1;\n\t\t\t\n\t\t\tvector<int> mada(0);\n\t\t\tmada.push_back(genten);\n\t\t\twhile(!mada.empty()){\n\t\t\t\tint i = mada.back();\n\t\t\t\tmada.pop_back();\n\t\t\t\trenketsu_list.push_back(i);\n\t\t\t\tfor (int j: ikeru[i]) {\n\t\t\t\t\tif (seen[j]) continue;\n\t\t\t\t\tmada.push_back(j);\n\t\t\t\t\tseen[j] = 1;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tint m = (int)renketsu_list.size();\n\t\t\t// unused\n\t\t\tvector dp_sub0(m, vector<ll>(m+1, (ll)1e18));\n\t\t\t// used\n\t\t\tvector dp_sub1(m, vector<ll>(m+1, (ll)1e18));\n\t\t\t// end\n\t\t\tvector dp_sub2(m, vector<ll>(m+1, (ll)1e18));\n\t\t\tdp_sub0[0][0] = 0;\n\t\t\tdp_sub1[0][1] = 6 * s * s;\n\t\t\trep(i,1,m) {\n\t\t\t\trep(j,0,m+1) {\n\t\t\t\t\tchmin(dp_sub0[i][j], dp_sub0[i-1][j]);\n\t\t\t\t\tchmin(dp_sub2[i][j], dp_sub1[i-1][j]);\n\t\t\t\t\tchmin(dp_sub2[i][j], dp_sub2[i-1][j]);\n\t\t\t\t}\n\t\t\t\trep(j,0,m) {\n\t\t\t\t\tchmin(dp_sub1[i][j+1], dp_sub0[i-1][j] + 6*s*s);\n\t\t\t\t\tchmin(dp_sub1[i][j+1], dp_sub1[i-1][j] + 6*s*s - hyomen(renketsu_list[i-1], renketsu_list[i]));\n\t\t\t\t}\n\t\t\t}\n//\t\t\tvector<ll> ret(m+1,(ll)1e18);\n\t\t\tif (m>=k) {\n\t\t\t\tchmin(ans,dp_sub1[m-1][k]);\n\t\t\t\tchmin(ans,dp_sub2[m-1][k]);\n\t\t\t}\n\t\t\t\n\n\t\t}else{\n\t\t\tassert(false);\n\t\t}\n\t}\n\n\t/*\n\n\tvector<ll> ans(1,0LL);\n\n\tfor (auto a: gogo_list) {\n\t\tvector<ll> nans((int)a.size()+(int)ans.size()-1,(ll)1e18);\n\t\trep(i,0,(int)a.size()) {\n\t\t\trep(j,0,(int)ans.size()) {\n\t\t\t\tchmin(nans[i+j],a[i]+ans[j]);\n\t\t\t}\n\t\t}\n\t\tswap(ans,nans);\n\t}\n\t\t*/\n\tif (ans<1e17){\n\t\tcout<<ans<<endl;\n\t}else{\n\t\tcout<<-1<<endl;\n\t}\n}\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\twhile(true) {\n\t\tint n; cin >> n;\n\t\tint k; cin >> k;\n\t\tll s; cin >> s;\n\t\tif (n == 0) break;\n\t\tsolve(n, k, s);\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 5376, "score_of_the_acc": -0.5676, "final_rank": 15 }, { "submission_id": "aoj_1612_10494495", "code_snippet": "# include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nconst double pi = acos(-1);\ntemplate<class T>constexpr T inf() { return ::std::numeric_limits<T>::max(); }\ntemplate<class T>constexpr T hinf() { return inf<T>() / 2; }\ntemplate <typename T_char>T_char TL(T_char cX) { return tolower(cX); }\ntemplate <typename T_char>T_char TU(T_char cX) { return toupper(cX); }\ntemplate<class T> bool chmin(T& a,T b) { if(a > b){a = b; return true;} return false; }\ntemplate<class T> bool chmax(T& a,T b) { if(a < b){a = b; return true;} return false; }\nint popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }\nint d_sum(ll n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }\nint d_cnt(ll n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }\nll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); };\nll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g*b; };\nll MOD(ll x, ll m){return (x%m+m)%m; }\nll FLOOR(ll x, ll m) {ll r = (x%m+m)%m; return (x-r)/m; }\ntemplate<class T> using dijk = priority_queue<T, vector<T>, greater<T>>;\n# define all(qpqpq) (qpqpq).begin(),(qpqpq).end()\n# define UNIQUE(wpwpw) (wpwpw).erase(unique(all((wpwpw))),(wpwpw).end())\n# define LOWER(epepe) transform(all((epepe)),(epepe).begin(),TL<char>)\n# define UPPER(rprpr) transform(all((rprpr)),(rprpr).begin(),TU<char>)\n# define rep(i,upupu) for(ll i = 0, i##_len = (upupu);(i) < (i##_len);(i)++)\n# define reps(i,opopo) for(ll i = 1, i##_len = (opopo);(i) <= (i##_len);(i)++)\n# define len(x) ((ll)(x).size())\n# define bit(n) (1LL << (n))\n# define pb push_back\n# define eb emplace_back\n# define exists(c, e) ((c).find(e) != (c).end())\n\nstruct INIT{\n\tINIT(){\n\t\tstd::ios::sync_with_stdio(false);\n\t\tstd::cin.tie(0);\n\t\tcout << fixed << setprecision(20);\n\t}\n}INIT;\n\nnamespace mmrz {\n\tvoid solve();\n}\n\nint main(){\n\tmmrz::solve();\n}\n#define debug(...) (static_cast<void>(0))\n\nusing namespace mmrz;\n#define rep2(i,a,n) for(int i=a;i<n;i++)\nbool SOLVE(){\n\tll n, k, s; cin >> n >> k >> s;\n\tif(n==0) return true;\n\t\n\tvector<tuple<ll,ll,ll>> v(n);\n\trep(i,n) {\n\t\tll x, y, z; cin >> x >> y >> z;\n\t\tv[i] = {x, y, z};\n\t}\n\n\t// auto calc_d = [&](ll l, ll r) -> ll {\n\t// \tll ret = -1;\n\t// \tif(l > r)swap(l, r);\n\t// \tif(r <= l + s){\n\t// \t\treturn l+s-r;\n\t// \t}\n\t// \treturn ret;\n\t// };\n\n\tvector<vector<pair<ll,ll>>> g(n);\n\trep(i,n) {\n\t\tauto [x1, y1, z1] = v[i];\n\t\tll xs = x1 + s;\n\t\tll ys = y1 + s;\n\t\tll zs = z1 + s;\n\t\trep2(j,i+1,n) {\n\t\t\tauto [x2,y2,z2] = v[j];\n\t\t\tbool isAlreadyChecked = false;\n\t\t\trep(x_mul, 2) rep(y_mul, 2) rep(z_mul, 2) {\n\t\t\t\tif(isAlreadyChecked) break;\n\t\t\t\tll px = x2 + s*x_mul;\n\t\t\t\tll py = y2 + s*y_mul;\n\t\t\t\tll pz = z2 + s*z_mul;\n\t\t\t\tif(x1 <= px && px <= xs && y1 <= py && py <= ys && z1 <= pz && pz <= zs) {\n\t\t\t\t\tisAlreadyChecked = true;\n\t\t\t\t\tll dx = x_mul == 0 ? xs-px: px-x1;\n\t\t\t\t\tll dy = y_mul == 0 ? ys-py: py-y1;\n\t\t\t\t\tll dz = z_mul == 0 ? zs-pz: pz-z1;\n\t\t\t\t\tll weight = 2ll*dx*dy + 2ll*dx*dz + 2ll*dy*dz;\n\t\t\t\t\tg[i].push_back({j, weight});\n\t\t\t\t\tg[j].push_back({i, weight});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// rep(j, n) {\n\t\t// // for(int j = i+1;j < n;j++) {\n\t\t// \tif(i == j)continue;\n\t\t// \tauto [x2, y2, z2] = v[j];\n\n\t\t// \tll xs = x1 + s;\n\t\t// \tll ys = y1 + s;\n\t\t// \tll zs = z1 + s;\n\n\t\t// \t// ll dx = calc_d(x1, x2);\n\t\t// \t// ll dy = calc_d(y1, y2);\n\t\t// \t// ll dz = calc_d(z1, z2);\n\n\t\t// \t// if(dx == -1 || dy == -1 || dz == -1)continue;\n\n\t\t// \t// ll weight = 2ll*dx*dy + 2ll*dx*dz + 2ll*dy*dz;\n\n\t\t// \t// g[i].push_back({j,weight});\n\t\t// \t// g[j].push_back({i,weight});\n\t\t\t\n\t\t// \tif(x1 <= x2 && x2 <= xs && y1 <= y2 && y2 <= ys && z1 <= z2 && z2 <= zs) {\n\n\t\t// \t\tll dx = xs - x2;\n\t\t// \t\tll dy = ys - y2;\n\t\t// \t\tll dz = zs - z2;\n\n\t\t// \t\tll weight = 2ll*dx*dy + 2ll*dx*dz + 2ll*dy*dz;\n\n\t\t// \t\tg[i].push_back({j,weight});\n\t\t// \t\tg[j].push_back({i,weight});\n\t\t\t\n\t\t// \t}\n\t\t// }\n\t}\n\n\tdebug(g);\n\n\tif(k == 1){\n\t\tcout << 6ll*s*s << '\\n';\n\t\treturn 0;\n\t}\n\t\n\tif(k == 2){\n\t\tll ans = hinf<ll>();\n\t\trep(i, n){\n\t\t\tfor(auto [to, e] : g[i]){\n\t\t\t\tchmin(ans, 6ll*s*s*2 - e);\n\t\t\t}\n\t\t}\n\t\tcout << (ans == hinf<ll>() ? -1 : ans) << '\\n';\n\t\treturn 0;\n\t}\n\n\tdebug(g);\n\tll ans = hinf<ll>();\n\t\n\trep(i, n){\n\t\tfor(auto [TO, E] : g[i]){\n\t\t\tll sm = E;\n\t\t\tunordered_set<int> st;\n\t\t\tst.emplace(i);\n\t\t\tst.emplace(TO);\n\t\t\tint cur = TO, pre = i;\n\t\t\tbool flg = true;\n\t\t\tfor(int rem = k-2;rem >= 0;rem--){\n\t\t\t\t\n\t\t\t\tbool update = false;\n\t\t\t\tfor(auto [to, e] : g[cur]){\n\t\t\t\t\tif(to == pre)continue;\n\t\t\t\t\tif(rem != 0 && st.contains(to))continue;\n\t\t\t\t\tif(rem != 0 || st.contains(to))sm += e;\n\t\t\t\t\tst.emplace(to);\n\t\t\t\t\tpre = cur;\n\t\t\t\t\tcur = to;\n\t\t\t\t\tupdate = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tdebug(pre, cur, sm, rem, st, update);\n\t\t\t\tif(rem && not update){\n\t\t\t\t\tflg = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(flg){\n\t\t\t\tchmin(ans, 6ll*s*s*k - sm);\n\t\t\t\tdebug(st, sm);\n\t\t\t}\n\t\t}\n\t}\n\tcout << (ans == hinf<ll>() ? -1 : ans) << '\\n';\n\treturn 0;\n}\n\nvoid mmrz::solve(){\n\t// int t = 1;\n\t//cin >> t;\n\twhile(!SOLVE());\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3584, "score_of_the_acc": -0.1814, "final_rank": 13 }, { "submission_id": "aoj_1612_9412332", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nbool connect(vector<int> v1, vector<int> v2, int S){\n if(abs(v1[0]-v2[0])<S&&abs(v1[1]-v2[1])<S&&abs(v1[2]-v2[2])<S) return true;\n else return false;\n}\n\nint add(vector<int> v1, vector<int> v2, int S){\n vector<int> dr(3);\n for(int i=0;i<3;i++) dr[i]=S-abs(v1[i]-v2[i]);\n int ret=6*S*S-2*(dr[0]*dr[1]+dr[1]*dr[2]+dr[2]*dr[0]);\n return ret;\n}\n\nint epoint(int s,vector<vector<int>> &side){\n set<int> itta;\n queue<vector<int>> que;\n int last=s;\n que.push({s,-1});//{今の位置,前の位置}\n itta.insert(s);\n while(!que.empty()){\n auto v=que.front();\n que.pop();\n int now=v[0],pre=v[1];\n for(int next:side[now]){\n if(next==pre) continue;\n if(itta.count(next)){\n last=-1;\n break;\n }\n else{\n itta.insert(next);\n last=next;\n que.push({next,now});\n }\n }\n if(last==-1) break;\n }\n return last;\n}\n\n\nvoid solve(int N, int K, int S,vector<int> &ANS){\n \n vector<vector<int>> pos(N,vector<int>(3));\n for(int i=0;i<N;i++){\n for(int j=0;j<3;j++){\n cin >> pos[i][j];\n }\n }\n if(K==1){\n ANS.push_back(6*S*S);\n return;\n }\n vector<vector<int>> side(N,vector<int>(0));\n for(int i=0;i<N;i++){\n for(int j=i+1;j<N;j++){\n if(connect(pos[i],pos[j],S)){\n side[i].push_back(j);\n side[j].push_back(i);\n //cout << i<<\"と\"<<j<<\"は連結\"<<endl;//\n }\n }\n }\n vector<bool> used(N,false);\n int ans=INT_MAX;\n for(int i=0;i<N;i++){\n if(!used[i]){\n int start=epoint(i,side);\n if(start==-1){\n vector<int> con;\n con.push_back(i);\n used[i]=true;\n int sec=side[i][0];\n con.push_back(sec);\n used[sec]=true;\n\n queue<int> hai;\n hai.push(sec);\n while(!hai.empty()){\n int now=hai.front();\n hai.pop();\n for(int next:side[now]){\n if(!used[next]){\n used[next]=true;\n con.push_back(next);\n hai.push(next);\n }\n } \n }\n int sz=con.size();\n if(sz==K){\n int area=0;\n for(int j=0;j<K;j++){\n area+=add(pos[con[j]],pos[con[(j+1)%K]],S);\n }\n ans=min(ans,area);\n }\n if(sz>K){\n int area=6*S*S;\n for(int j=1;j<K;j++){\n area+=add(pos[con[j-1]],pos[con[j]],S);\n }\n ans=min(ans,area);\n //cout << \"ループの場合\"<<area<<endl;//\n for(int j=1;j<sz;j++){\n area+=add(pos[con[(K-2+j)%sz]],pos[con[(K-1+j)%sz]],S);\n area-=add(pos[con[j-1]],pos[con[j]],S);\n ans=min(ans,area);\n //cout << \"ループの場合\"<<area<<endl;//\n }\n }\n }\n else{\n queue<int> hai;\n hai.push(start);\n used[start]=true;\n vector<int> con;\n con.push_back(start);\n while(!hai.empty()){\n int now=hai.front();\n hai.pop();\n for(int next:side[now]){\n if(!used[next]){\n used[next]=true;\n con.push_back(next);\n hai.push(next);\n }\n } \n }\n int sz=con.size();\n //for(int x:con) cout << x <<' ';//\n //cout <<\"これらは連結\"<<endl;//\n if(sz>=K){\n int area=6*S*S;\n for(int j=1;j<K;j++){\n area+=add(pos[con[j-1]],pos[con[j]],S);\n }\n ans=min(ans,area);\n //cout << \"鎖状の場合\"<<area<<endl;//\n for(int j=K;j<sz;j++){\n area+=add(pos[con[j-1]],pos[con[j]],S);\n area-=add(pos[con[j-K]],pos[con[j-K+1]],S);\n ans=min(ans,area);\n //cout << \"鎖状の場合\"<<area<<endl;//\n }\n }\n }\n }\n }\n if(ans==INT_MAX) ans=-1;\n ANS.push_back(ans);\n\n}\n\n\n\nint main() {\n vector<int> ANS;\n while(true){\n int N,K,S;\n cin>>N>>K>>S;\n if(N==0) break;\n solve(N,K,S,ANS);\n }\n for(int a:ANS) cout << a << endl;\n \n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3432, "score_of_the_acc": -0.141, "final_rank": 10 }, { "submission_id": "aoj_1612_9239661", "code_snippet": "#include <bits/stdc++.h>\n\nbool solve() {\n long long N, K, S;\n std::cin >> N >> K >> S;\n if (N == 0) return false;\n std::array<std::vector<long long>, 3> P;\n for (int i{} ; i < 3 ; i++) P[i] = std::vector<long long>(N);\n for (int i{} ; i < N ; i++) {\n for (int j{} ; j < 3 ; j++) std::cin >> P[j][i];\n }\n std::vector<std::vector<std::pair<int, long long>>> g(N);\n for (int i{} ; i < N ; i++) {\n for (int j{} ; j < N ; j++) {\n if (i == j) continue;\n bool ok{true};\n long long len[3];\n for (int k{} ; k < 3 ; k++) {\n long long l{std::max(P[k][i], P[k][j])}, r{std::min(P[k][i], P[k][j]) + S};\n ok &= (l < r);\n len[k] = r - l;\n }\n if (!ok) continue;\n long long cost{}; \n for (int i{} ; i < 3 ; i++) {\n long long area{len[i + 1 == 3 ? 0 : i + 1] * len[i]};\n cost += 2 * area;\n }\n g[i].emplace_back(j, cost);\n }\n }\n std::vector<bool> used(N);\n int start{-1};\n auto rec{[&](auto rec, int v, int cnt, long long sum) -> long long {\n long long res{(cnt == K ? sum : -1)};\n for (auto [x, w] : g[v]) {\n if (!used[x] and cnt < K) {\n used[x] = true;\n res = std::max(res, rec(rec, x, cnt + 1, sum + w));\n used[x] = false;\n }\n else if (K != 2 and cnt == K and x == start) {\n assert(used[x]);\n res += w;\n }\n }\n return res;\n }};\n long long ans{-1};\n for (int v{} ; v < N ; v++) {\n used[v] = true;\n start = v;\n ans = std::max(ans, rec(rec, v, 1, 0LL)); \n used[v] = false;\n }\n if (ans == -1) {\n std::cout << ans << '\\n';\n }\n else {\n ans = (long long)K * 6 * S * S - ans;\n std::cout << ans << '\\n';\n }\n return true;\n}\n\nint main() {\n std::cin.tie(nullptr)->sync_with_stdio(false);\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3456, "score_of_the_acc": -0.1474, "final_rank": 11 }, { "submission_id": "aoj_1612_9122008", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(v) v.begin(),v.end()\ntemplate<class F, class S>\nostream& operator<<(ostream& os, const pair<F,S>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate<class Iter>\nvoid print(Iter begin, Iter end) {\n for (auto itr = begin; itr != end; ++itr) {\n cerr << (*itr) << ' ';\n }\n cerr << '\\n';\n}\n#define dbg(v) cerr << #v << \": \" << v << '\\n';\nint N,K,S;\nvector<array<int,3>> P;\nusing Graph = vector<vector<int>>;\nGraph G;\nvector<vector<int>> W;\n\nint cycle(vector<int>& vert) {\n if (vert.size() == K) {\n int sum = 0;\n for (int i : vert) {\n for (int j : vert) {\n sum += W[i][j];\n }\n }\n assert(sum % 2 == 0);\n return sum / 2;\n }\n int n = vert.size();\n map<int, bool> used;\n int start = vert[0];\n vert.clear();\n vert.push_back(start);\n used[start] = true;\n for (int i = 0; i < n-1; ++i) {\n int pos = vert.back();\n for (int to : G[pos]) {\n if (!used[to]) {\n vert.push_back(to);\n used[to] = true;\n break;\n }\n }\n }\n for (int i = 0; i < n; ++i) {\n vert.push_back(vert[i]);\n }\n\n vector<int> rui(2*n);\n for (int i = 1; i < 2*n; ++i) {\n rui[i] = rui[i-1] + W[vert[i]][vert[i-1]];\n }\n int res = 0;\n for (int i = K-1; i < 2*n; ++i) {\n res = max(res, rui[i] - rui[i-K+1]);\n }\n return res;\n}\n\nint path(vector<int>& vert) {\n int start = -1;\n for (int v : vert) {\n if (G[v].size() <= 1) {\n start = v;\n break;\n }\n }\n int n = vert.size();\n map<int, bool> used;\n vert.clear();\n vert.push_back(start);\n used[start] = true;\n for (int i = 0; i < n-1; ++i) {\n int pos = vert.back();\n for (int to : G[pos]) {\n if (!used[to]) {\n vert.push_back(to);\n used[to] = true;\n break;\n }\n }\n }\n\n vector<int> rui(n);\n for (int i = 1; i < n; ++i) {\n rui[i] = rui[i-1] + W[vert[i]][vert[i-1]];\n }\n int res = 0;\n for (int i = K-1; i < n; ++i) {\n res = max(res, rui[i] - rui[i-K+1]);\n }\n return res;\n}\n\nint calc(int x1, int y1, int z1, int x2, int y2, int z2)\n{\n if (x1 > x2) swap(x1, x2);\n int dx = max(0, x1 + S - x2);\n if (y1 > y2) swap(y1, y2);\n int dy = max(0, y1 + S - y2);\n if (z1 > z2) swap(z1, z2);\n int dz = max(0, z1 + S - z2);\n if (dx==0 || dy==0 || dz==0) return 0;\n return (dx * dy + dy * dz + dz * dx) * 2;\n}\n\nint solve() {\n cin >> N >> K >> S;\n if (N+K+S == 0) exit(0);\n P.resize(N);\n for (int i = 0; i < N; ++i) {\n for (int j = 0; j < 3; ++j) {\n cin >> P[i][j];\n }\n }\n\n G.clear();\n G.resize(N);\n W.clear();\n W.resize(N, vector<int>(N,0));\n for (int i = 0; i < N; ++i) {\n for (int j = i+1; j < N; ++j) {\n // calc weight of edge i-j\n auto [x1, y1, z1] = P[i];\n auto [x2, y2, z2] = P[j];\n int diff = calc(x1,y1,z1, x2,y2,z2);\n W[i][j] = W[j][i] = diff;\n if (diff > 0) {\n G[i].emplace_back(j);\n G[j].emplace_back(i);\n }\n }\n }\n // for (int i = 0; i < N; ++i) {\n // cerr << i << \":\";\n // print(ALL(G[i]));\n // }\n vector<bool> used(N);\n int ans = -1;\n for (int i = 0; i < N; ++i) {\n if (used[i]) continue;\n vector<int> vert;\n queue<int> q;\n used[i] = true;\n q.push(i);\n vert.push_back(i);\n while (q.size()) {\n int v = q.front();\n q.pop();\n for (auto to : G[v]) {\n if (!used[to]) {\n used[to] = true;\n q.push(to);\n vert.push_back(to);\n }\n }\n }\n\n bool isCycle = true;\n for (int v : vert) {\n if (G[v].size() != 2) {\n isCycle = false;\n }\n }\n if (vert.size() < K) {continue;}\n\n if (isCycle) {\n ans = max(ans, cycle(vert));\n } else {\n ans = max(ans, path(vert));\n }\n }\n if (ans == -1) return -1;\n return K * S*S*6 - ans;\n}\n\nint main() {\n while (true) {\n cout << solve() << '\\n';\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7000, "score_of_the_acc": -1, "final_rank": 19 }, { "submission_id": "aoj_1612_8031034", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\n\nusing i64 = long long;\nusing ll = long long;\n\nbool solve(){\n int n, k, s;\n cin >> n >> k >> s;\n if (n == 0) {\n return false;\n }\n vector<ll> x(n);\n vector<ll> y(n);\n vector<ll> z(n);\n for (int i = 0; i < n; i++) {\n cin >> x[i] >> y[i] >> z[i];\n }\n ll base = s * s * 6 * k;\n if (k == 1) {\n cout << base << endl;\n return true;\n }\n vector<int> e1(n, -1);\n vector<int> e2(n, -1);\n vector<ll> s1(n, -1);\n vector<ll> s2(n, -1);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (i == j) {\n continue;\n }\n ll ix = s + min(x[i], x[j]) - max(x[i], x[j]);\n ll iy = s + min(y[i], y[j]) - max(y[i], y[j]);\n ll iz = s + min(z[i], z[j]) - max(z[i], z[j]);\n if (ix > 0 && iy > 0 && iz > 0) {\n ll siz = (ix * iy + ix * iz + iy * iz) * 2;\n if (e1[i] == -1) {\n e1[i] = j;\n s1[i] = siz;\n } else {\n e2[i] = j;\n s2[i] = siz;\n }\n }\n }\n }\n ll ans = 0;\n bool any = false;\n for (int i = 0; i < n; i++) {\n if (e1[i] != -1) {\n int p = i;\n int c = e1[i];\n ll su = 0;\n ll ns = s1[i];\n bool ok = true;\n //cout << i << \"_2\" << endl;\n for (int j = 1; j < k; j++) {\n //cout << c << \", \" << p << endl;\n if (c == -1) {\n ok = false;\n break;\n }\n su += ns;\n if (e1[c] == p && e2[c] != -1) {\n ns = s2[c];\n p = c;\n c = e2[c];\n } else if (e2[c] == p && e1[c] != -1) {\n ns = s1[c];\n p = c;\n c = e1[c];\n } else {\n p = c;\n c = -1;\n }\n if (p == i) {\n ok = false;\n break;\n }\n }\n if (c == i) {\n su += ns;\n }\n if (ok) {\n any = true;\n ans = max(ans, su);\n }\n }\n if (e2[i] != -1) {\n int p = i;\n int c = e2[i];\n ll su = 0;\n ll ns = s2[i];\n bool ok = true;\n //cout << i << \"_2\" << endl;\n for (int j = 1; j < k; j++) {\n //cout << c << \", \" << p << endl;\n if (c == -1) {\n ok = false;\n break;\n }\n su += ns;\n if (e1[c] == p && e2[c] != -1) {\n ns = s2[c];\n p = c;\n c = e2[c];\n } else if (e2[c] == p && e1[c] != -1) {\n ns = s1[c];\n p = c;\n c = e1[c];\n } else {\n p = c;\n c = -1;\n }\n if (p == i) {\n ok = false;\n break;\n }\n }\n if (c == i) {\n su += ns;\n }\n if (ok) {\n any = true;\n ans = max(ans, su);\n }\n }\n }\n if (any) {\n cout << base - ans << endl;\n } else {\n cout << -1 << endl;\n }\n return true;\n}\n\nint main() {\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3432, "score_of_the_acc": -0.0501, "final_rank": 2 }, { "submission_id": "aoj_1612_8026061", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n\nstruct Unionfind{\nprivate:\n int n;\n std::vector<int> parents;\n\npublic:\n Unionfind(int n):n(n), parents(n,-1) {}\n\n int find(int x) {\n if(parents[x] < 0) return x;\n return parents[x] = find(parents[x]);\n }\n\n void unite(int x,int y) {\n x = find(x);\n y = find(y);\n\n if(x == y) return;\n if(parents[x] > parents[y]) std::swap(x, y);\n parents[x] += parents[y];\n parents[y] = x;\n return;\n } \n\n int size(int x) {\n return -parents[find(x)];\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n\n std::vector<int> members(int x) {\n int root = find(x);\n std::vector<int> member;\n for(int i = 0;i < n; i++){\n if(find(i) == root ){\n member.push_back(i);\n }\n }\n return member;\n }\n\n int group_cnt(void) {\n int c = 0;\n for(int i = 0;i < n; i++){\n if(parents[i] < 0) ++c;\n }\n return c;\n }\n};\n\nlong long calc(vector<vector<pair<int,long long>>>& G, Unionfind& uf, vector<bool> in_circle, int n, int k, int s){\n long long ans = -1;\n vector<bool> done(G.size());\n for(int i = 0; i < G.size(); i++){\n if(done[i]) continue;\n auto member = uf.members(i);\n if(member.size() < k) continue;\n\n\n bool circle = false;\n int start = i;\n for(int j = 0; j < member.size(); j++){\n int m = member[j];\n done[m] = 1, circle |= in_circle[m];\n if(G[m].size() == 1) start = m;\n }\n\n // cout << circle << endl;\n // for(int i = 0; i < member.size(); i++) cout << member[i] << \" \";\n //cout << endl;\n\n vector<long long> a;\n vector<bool> visited(member.size());\n auto dfs = [&](auto&& self, int v, int prev = -1)->void{\n visited[v] = true;\n for(auto[nv, rew] : G[v]){\n if(visited[nv]){\n if(prev != start && nv == start) a.push_back(rew);\n continue;\n }\n a.push_back(rew);\n self(self, nv, v);\n }\n };\n dfs(dfs, start);\n\n //for(int i = 0; i < a.size(); i++){\n // cout << a[i] << \" \";\n //}\n //cout << endl;\n\n if(member.size() == k){\n ans = max(ans, accumulate(a.begin(), a.end(), 0ll));\n }\n else{\n if(circle){\n vector<long long> sum(3*a.size() + 1);\n for(int i = 0; i < sum.size() - 1; i++){\n sum[i + 1] = sum[i] + a[i % a.size()];\n }\n for(int i = k - 1; i <= a.size() + k; i++){\n ans = max(ans, sum[i] - sum[i - (k - 1)]);\n }\n }\n else{\n vector<long long> sum(a.size() + 1);\n for(int i = 0; i < a.size(); i++){\n sum[i + 1] = sum[i] + a[i % a.size()];\n }\n for(int i = k - 1; i < sum.size(); i++){\n ans = max(ans, sum[i] - sum[i - (k - 1)]);\n }\n }\n }\n }\n return ans;\n} \n\n\nvoid solve(int n, int k, long long s){\n vector<int> x(n), y(n), z(n);\n Unionfind uf(n);\n vector<bool> in_circle(n);\n vector<vector<pair<int, long long>>> G(n);\n for(int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i];\n auto process = [&](int i, int j)->void{\n long long area = 0;\n for(int mask = 0; mask < 1<<3; mask++){\n vector<int> co(3);\n co[0] = x[i], co[1] = y[i], co[2] = z[i];\n for(int p = 0; p < 3; p++){\n if(mask >> p & 1) co[p] += s;\n }\n if(x[j] <= co[0] && co[0] <= x[j] + s && y[j] <= co[1] && co[1] <= y[j] + s && z[j] <= co[2] && co[2] <= z[j] + s){\n long long tate = ((mask >> 0 & 1) ? co[0] - x[j] : x[j] + s - co[0]);\n long long yoko = ((mask >> 1 & 1) ? co[1] - y[j] : y[j] + s - co[1]);\n long long takasa = ((mask >> 2 & 1) ? co[2] - z[j] : z[j] + s - co[2]);\n area = 2 * (tate * yoko + tate * takasa + yoko * takasa);\n } \n }\n if(area != 0){\n G[i].push_back({j, area}), G[j].push_back({i, area});\n if(uf.same(i, j)) in_circle[i] = in_circle[j] = 1;\n uf.unite(i, j);\n }\n };\n for(int i = 0; i < n; i++) for(int j = 0; j < i; j++) process(i, j);\n //for(int i = 0; i < n; i++){\n // for(auto[j, cost]:G[i]) cout << i << \" \" << j << \" \" << cost << endl;\n //}\n long long tmp = calc(G, uf, in_circle, n, k, s);\n if(tmp == -1) cout << tmp << endl;\n else cout << 6*s*s*k - tmp << endl;\n}\n\nint main() {\n while(1){\n int n, k, s; cin >> n >> k >> s;\n if(n == 0 && k == 0 && s == 0) break;\n solve(n, k, s);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3544, "score_of_the_acc": -0.7162, "final_rank": 17 }, { "submission_id": "aoj_1612_7720545", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,s,n) for (int i = s; i < (int)(n); i++)\n#define rrep(i,s,n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(),v.end()\n\nusing ll = long long;\nusing ull = unsigned long long;\n\ntemplate<typename T> bool chmin(T &a, const T &b){\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate<typename T> bool chmax(T &a, const T &b){\n if (a >= b) return false;\n a = b;\n return true;\n}\n\nint main() {\n const int INF = std::numeric_limits<int>::max();\n int n,k,s;\n while(std::cin >> n >> k >> s, n != 0) {\n using P = std::array<int, 3>;\n std::vector<P> ps(n);\n for(auto &p: ps) {\n rep(i,0,3) {\n std::cin >> p[i];\n }\n }\n auto is_connect = [&](P a, P b) -> bool {\n rep(bit,0,1<<3) {\n bool is_ok = true;\n rep(i,0,3) {\n int ret = a[i];\n if((bit >> i) & 1) {\n ret += s;\n }\n is_ok &= b[i] <= ret && ret <= b[i] + s;\n }\n if(is_ok) return true;\n }\n return false;\n };\n auto f = [&](P a, P b) -> int {\n P diff;\n rep(i,0,3) diff[i] = s - std::abs(a[i] - b[i]);\n int ret = 0;\n rep(i,0,3) ret += 2 * diff[i] * diff[(i + 1) % 3];\n return ret;\n };\n if(k == 1) {\n std::cout << 6 * s * s << '\\n';\n continue;\n }\n if(k == 2) {\n int ans = INF;\n rep(i,0,n) {\n rep(j,i+1,n) {\n if(is_connect(ps[i], ps[j])) chmin(ans, 6*s*s*2 - f(ps[i], ps[j]));\n }\n }\n if(ans == INF) ans = -1;\n std::cout << ans << '\\n';\n continue;\n }\n\n std::vector g(n, std::vector<int>());\n rep(i,0,n) rep(j,i+1,n) {\n if(is_connect(ps[i], ps[j])) {\n g[i].emplace_back(j);\n g[j].emplace_back(i);\n }\n }\n std::vector<std::vector<int>> groups;\n {\n std::vector<int> deg1;\n std::vector<int> seen(n, -1);\n std::vector<int> group;\n auto dfs = [&](auto &&self, int v) -> void {\n seen[v] = 1;\n group.emplace_back(v);\n for(auto nv: g[v]) if(seen[nv] < 0) {\n self(self, nv);\n }\n };\n rep(i,0,n) if(g[i].size() == 1 && seen[i] < 0) {\n dfs(dfs, i);\n groups.emplace_back(group);\n group.clear();\n }\n rep(i,0,n) if(seen[i] < 0) {\n dfs(dfs, i);\n groups.emplace_back(group);\n group.clear();\n }\n }\n int ans = INF;\n for(auto group: groups) {\n int sz = group.size();\n if(sz < k) continue;\n std::vector<int> diffs;\n rep(i,0,sz-1) {\n int idx = group[i];\n int jdx = group[i+1];\n diffs.emplace_back(f(ps[idx], ps[jdx]));\n }\n bool is_loop = is_connect(ps[group[0]], ps[group.back()]);\n int w = k-1;\n if(is_loop) {\n diffs.emplace_back(f(ps[group[0]], ps[group.back()]));\n if(sz == k) w = k;\n }\n sz = diffs.size();\n int ret = 0;\n rep(i,0,w) {\n ret += diffs[i];\n }\n chmin(ans, 6 * s * s * k - ret);\n rep(i,0,sz) {\n if(!is_loop && i + w >= sz) break;\n ret -= diffs[i];\n ret += diffs[(i + w) % sz];\n chmin(ans, 6 * s * s * k - ret);\n }\n }\n if(ans == INF) ans = -1;\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3572, "score_of_the_acc": -0.1782, "final_rank": 12 }, { "submission_id": "aoj_1612_7720139", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool is_connect(int i, int j, int s, vector<int>&x, vector<int>&y, vector<int>&z){\n\tint px,py,pz,qx,qy,qz;\n\tfor (int k=0; k<8; k++){\n\t\tif (k&1){\n\t\t\tpx=i;\n\t\t\tqx=j;\n\t\t}else{\n\t\t\tpx=j;\n\t\t\tqx=i;\n\t\t}\n\t\tif (k&2){\n\t\t\tpy=i;\n\t\t\tqy=j;\n\t\t}else{\n\t\t\tpy=j;\n\t\t\tqy=i;\n\t\t}\n\t\tif (k&4){\n\t\t\tpz=i;\n\t\t\tqz=j;\n\t\t}else{\n\t\t\tpz=j;\n\t\t\tqz=i;\n\t\t}\n\t\tif (!(x[px] <= x[qx] && x[qx] <= x[px]+s)) continue;\n\t\tif (!(y[py] <= y[qy] && y[qy] <= y[py]+s)) continue;\n\t\tif (!(z[pz] <= z[qz] && z[qz] <= z[pz]+s)) continue;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nint kasane(int i, int j, int s, vector<int>&x, vector<int>&y, vector<int>&z){\n\tint px,py,pz;\n\tif (x[i] <= x[j]){\n\t\tpx = x[i]+s-x[j];\n\t}else{\n\t\tpx = x[j]+s-x[i];\n\t}\n\tif (y[i] <= y[j]){\n\t\tpy = y[i]+s-y[j];\n\t}else{\n\t\tpy = y[j]+s-y[i];\n\t}\n\tif (z[i] <= z[j]){\n\t\tpz = z[i]+s-z[j];\n\t}else{\n\t\tpz = z[j]+s-z[i];\n\t}\n\treturn 2*(px*py+py*pz+pz*px);\n}\n\nvoid solve(int n, int k, int s){\n\tvector<int> x(n), y(n), z(n);\n\tfor (int i=0; i<n; i++){\n\t\tcin >> x[i] >> y[i] >> z[i];\n\t}\n\n\tvector<vector<int>> ikeru(n, vector<int>(0));\n\tvector<int> deg(n);\n\tfor (int i=0; i<n; i++){\n\t\tfor (int j=i+1; j<n; j++){\n\t\t\tif (is_connect(i, j, s, x, y, z)){\n\t\t\t\tikeru[i].push_back(j);\n\t\t\t\tikeru[j].push_back(i);\n\t\t\t\tdeg[i] += 1;\n\t\t\t\tdeg[j] += 1;\n\t\t\t}\n\t\t}\n\t}\n\t\n\tvector<int> deg1(0);\n\tvector<bool> tansaku(n);\n\tfor (int i=0; i<n; i++){\n\t\tif (deg[i] <= 1) deg1.push_back(i);\n\t}\n\tvector<vector<int>> groups(0,vector<int>(0));\n\tint cnt = 0;\n\tvector<bool> isen(0);\n\tfor (int st: deg1){\n\t\tvector<int> mada = {st};\n\t\ttansaku[st] = true;\n\t\tgroups.push_back(vector<int>(0));\n\t\tgroups[cnt].push_back(st);\n\t\tisen.push_back(false);\n\t\twhile(!mada.empty()){\n\t\t\tint i = mada.back(); mada.pop_back();\n\t\t\tfor (int j: ikeru[i]){\n\t\t\t\tif(!tansaku[j]){\n\t\t\t\t\ttansaku[j] = true;\n\t\t\t\t\tgroups[cnt].push_back(j);\n\t\t\t\t\tmada.push_back(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcnt += 1;\n\t}\n\tfor (int st=0; st<n; st++){\n\t\tif (tansaku[st]) continue;\n\t\tvector<int> mada = {st};\n\t\tgroups.push_back(vector<int>(0));\n\t\tisen.push_back(true);\n\t\twhile(!mada.empty()){\n\t\t\tint i = mada.back(); mada.pop_back();\n\t\t\tif(tansaku[i]) continue;\n\t\t\tgroups[cnt].push_back(i);\n\t\t\ttansaku[i] = true;\n\t\t\tfor (int j: ikeru[i]){\n\t\t\t\tif(!tansaku[j]){\n\t\t\t\t\tmada.push_back(j);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcnt += 1;\n\t}\n\n\tint ans = 1e9;\n\tfor (int xx=0; xx<cnt; xx++){\n\t\tint m = groups[xx].size();\n\t\tif (m < k) continue;\t\n\t\tint w = k-1;\n\t\tif (isen[xx]){\n\t\t\tif (m==k) w=k;\n\t\t\tvector<int> dif(2*m);\n\t\t\tfor (int i=0; i<2*m; i++){\n\t\t\t\tdif[i] = kasane(groups[xx][i%m], groups[xx][(i+1)%m], s, x, y, z);\n\t\t\t}\n\t\t\tvector<int> rui(2*m+1);\n\t\t\tfor (int i=0; i<2*m; i++){\n\t\t\t\trui[i+1] = rui[i] + dif[i];\n\t\t\t}\n\t\t\tfor (int i=0; i<=2*m-w; i++){\n\t\t\t\tans = min(ans, 6*s*s*k - (rui[i+w] - rui[i]));\n\t\t\t}\n\t\t}else{\n\t\t\tvector<int> dif(m-1);\n\t\t\tfor (int i=0; i<m-1; i++){\n\t\t\t\tdif[i] = kasane(groups[xx][i%m], groups[xx][(i+1)%m], s, x, y, z);\n\t\t\t}\n\t\t\tvector<int> rui(m);\n\t\t\tfor (int i=0; i<m-1; i++){\n\t\t\t\trui[i+1] = rui[i] + dif[i];\n\t\t\t}\n\t\t\tfor (int i=0; i<m-w; i++){\n\t\t\t\tans = min(ans, 6*s*s*k - (rui[i+w] - rui[i]));\n\t\t\t}\n\t\t}\n\t}\n\tif (ans == 1e9) ans = -1;\n\tcout << ans << \"\\n\";\n}\n\nint main(){\n\twhile(true){\n\t\tint n,k,s; cin >> n >> k >> s;\n\t\tif (n == 0) break;\n\t\tsolve(n,k,s);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3572, "score_of_the_acc": -0.0873, "final_rank": 9 }, { "submission_id": "aoj_1612_7720114", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i,x,n) for(int i = (int)x; i < (int)n; i++)\n#define rrep(i,x,n) for(int i = int(n) - 1; i >= x; i--)\n#define all(a) a.begin(), a.end()\n\nusing ll = long long;\nusing ull = unsigned long long;\n\ntemplate<class T>\nbool chmin(T &a, T b) {\n if(a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate<class T>\nbool chmax(T &a, T b) {\n if(a >= b) return false;\n a = b;\n return true;\n}\n\nint main() {\n using pv = array<ll,3>;\n auto cnct = [](pv a, pv b, ll w){\n rep(s,0,8){\n pv c = {0,0,0};\n rep(i,0,3) c[i] = b[i] + (s >> i & 1) * w;\n bool ok = true;\n rep(i,0,3){\n if (c[i] < a[i] || a[i]+w < c[i]) ok = false;\n }\n if (ok) return true;\n }\n return false;\n };\n auto cov = [&](pv a, pv b, ll w){\n pv c = {0,0,0};\n rep(i,0,3) c[i] = w - abs(a[i]-b[i]);\n ll res = 0;\n rep(i,0,3){\n res += 2 * c[i] * c[(i+1)%3];\n }\n return res;\n };\n while (true){\n int n, k; cin >> n >> k;\n ll s; cin >> s;\n if (n == 0) break;\n vector<pv> a(n);\n rep(i,0,n){\n ll x, y, z; cin >> x >> y >> z;\n a[i] = {x,y,z};\n }\n if (k == 1){\n cout << 6*s*s << endl;\n continue;\n }\n if (k == 2){\n set<pv> st;\n bool ok = false;\n rep(i,0,n){\n if (st.find(a[i]) == st.end()){\n st.insert(a[i]);\n }\n else {\n ok = true; \n }\n }\n if (ok){\n cout << 6*s*s << endl;\n continue;\n }\n }\n vector<vector<int>> es(n);\n vector<int> deg(n,0);\n rep(i,0,n) rep(j,0,n){\n if (i == j) continue;\n if (cnct(a[i],a[j],s)) es[i].emplace_back(j), deg[i]++;\n }\n vector<int> vis(n,0);\n const ll inf = 1'000'000'000'000'000'000;\n ll ans = inf;\n rep(i,0,n){\n if (vis[i] == 1) continue;\n vector<int> ids;\n ids.emplace_back(i);\n vis[i] = 1;\n queue<int> que;\n que.push(i);\n while (!que.empty()){\n int p = que.front(); que.pop(); //cout << p << endl;\n for (int q : es[p]){\n if (vis[q] == 1) continue;\n vis[q] = 1;\n ids.emplace_back(q);\n que.push(q);\n }\n }\n int nsiz = ids.size(); //cout << nsiz << endl; cout << endl;\n if (nsiz < k) continue;\n bool ng = true;\n int id = ids[0];\n for (int j : ids){\n if (deg[j] == 1) id = j, ng = false;\n }\n for (int j : ids) vis[j] = 0;\n ids.clear();\n ids.emplace_back(id);\n vis[id] = 1;\n que.push(id);\n while (!que.empty()){\n int p = que.front(); que.pop();\n for (int q : es[p]){\n if (vis[q] == 1) continue;\n vis[q] = 1;\n ids.emplace_back(q);\n que.push(q);\n break;\n }\n }\n int siz = ids.size(); //cout << nsiz << endl;\n if (siz < k) continue;\n int cs = k-1;\n if (ng){\n if (siz == k){\n ll cur = 6*s*s*k;\n rep(j,0,siz) cur -= cov(a[ids[j]],a[ids[(j+1)%siz]],s);\n if (k == 2) cur += cov(a[ids[0]],a[ids[1]],s);\n chmin(ans,cur);\n continue;\n }\n rep(j,0,siz){\n ids.emplace_back(ids[j]);\n }\n }\n siz = ids.size();\n vector<ll> cum(siz, 0);\n rep(j,0,siz-1) {\n cum[j+1] = cum[j] + cov(a[ids[j]],a[ids[j+1]],s);\n }\n rep(j,0,siz) {\n if(j + cs >= siz) break;\n chmin(ans, 6*s*s*k - (cum[j+cs] - cum[j]));\n }\n }\n if (ans == inf) ans = -1;\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3432, "score_of_the_acc": -0.5046, "final_rank": 14 }, { "submission_id": "aoj_1612_7719767", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i,x,n) for(int i = (int)x; i < (int)n; i++)\n#define rrep(i,x,n) for(int i = int(n) - 1; i >= x; i--)\n#define all(a) a.begin(), a.end()\n\nusing ll = long long;\nusing ull = unsigned long long;\n\ntemplate<class T>\nbool chmin(T &a, T b) {\n if(a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate<class T>\nbool chmax(T &a, T b) {\n if(a >= b) return false;\n a = b;\n return true;\n}\n\nconst ll inf = ll(1) << ll(60);\n\nint main() {\n ll n,k,s;\n using P = std::vector<ll>;\n while(std::cin >> n >> k >> s, !(n == 0)) {\n auto is_connect = [&](P a, P b){\n ll w = s;\n rep(ss,0,8){\n P c = {0,0,0};\n rep(i,0,3) c[i] = b[i] + (ss >> i & 1) * w;\n bool ok = true;\n rep(i,0,3){\n if (c[i] < a[i] || a[i]+w < c[i]) ok = false;\n }\n if (ok) return true;\n }\n return false;\n };\n auto cover_sum = [&](P a, P b) -> ll {\n std::vector<ll> diff(3);\n rep(i,0,3) {\n ll x1 = a[i];\n ll x2 = b[i];\n if(x1 > x2) std::swap(x1, x2);\n diff[i] = x1 + s - x2;\n }\n ll sum = 0;\n rep(i,0,3) sum += 2 * diff[i] * diff[(i + 1) % 3];\n return sum;\n };\n std::vector<P> ps(n, P(3));\n for(auto &p: ps) {\n rep(i,0,3) std::cin >> p[i];\n }\n if(k == 1) {\n std::cout << 6 * s * s << '\\n';\n continue;\n }\n if (k == 2){\n set<P> st;\n bool ok = false;\n rep(i,0,n){\n if (st.find(ps[i]) == st.end()){\n st.insert(ps[i]);\n }\n else {\n ok = true; \n }\n }\n if (ok){\n cout << 6*s*s << endl;\n continue;\n }\n ll ans = inf;\n rep(i,0,n) {\n rep(j,i+1,n) {\n if(is_connect(ps[i], ps[j])) {\n chmin(ans, 6*s*s*2 - cover_sum(ps[i], ps[j]));\n }\n }\n }\n if(ans == inf) ans = -1;\n std::cout << ans << '\\n';\n continue;\n }\n std::vector g(n, std::vector<int>());\n rep(i,0,n) {\n rep(j,i+1,n) {\n if(is_connect(ps[i], ps[j])) {\n g[i].emplace_back(j);\n g[j].emplace_back(i);\n }\n }\n }\n std::vector<std::vector<int>> groups;\n {\n std::vector<int> deg1;\n rep(i,0,n) if(g[i].size() == 1) deg1.emplace_back(i);\n std::vector<int> group, seen(n, -1);\n auto dfs = [&](auto &&self, int idx) -> void {\n for(auto v: g[idx]) {\n if(seen[v] < 0) {\n seen[v] = 1;\n group.emplace_back(v);\n self(self, v);\n }\n }\n };\n for(auto i: deg1) {\n if(seen[i] > 0) continue;\n group.clear();\n group.emplace_back(i);\n seen[i] = 1;\n dfs(dfs, i);\n groups.emplace_back(group);\n }\n rep(i,0,n) {\n if(seen[i] > 0) continue;\n group.clear();\n seen[i] = 1;\n group.emplace_back(i);\n dfs(dfs, i);\n groups.emplace_back(group);\n }\n }\n ll ans = inf;\n for(auto group: groups) {\n int sz = group.size();\n if(sz < k) continue;\n std::vector<ll> diffs;\n rep(i,0,sz-1) {\n int idx = group[i];\n int jdx = group[i+1];\n diffs.emplace_back(cover_sum(ps[idx], ps[jdx]));\n }\n int w = k-1;\n {\n int idx = group[0];\n int jdx = group.back();\n if(is_connect(ps[idx], ps[jdx])) {\n if(group.size() == k) w = k;\n int c = diffs.size();\n diffs.emplace_back(cover_sum(ps[idx], ps[jdx]));\n rep(i,0,c) diffs.emplace_back(diffs[i]);\n }\n }\n sz = diffs.size();\n std::vector<ll> cum(sz+1, 0);\n rep(i,0,sz) {\n cum[i+1] = cum[i] + diffs[i];\n }\n rep(i,0,sz) {\n if(i + w > sz) break;\n chmin(ans, 6 * s * s * k - (cum[i+w] - cum[i]));\n }\n }\n if (ans == inf) ans = -1;\n std::cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3536, "score_of_the_acc": -0.9868, "final_rank": 18 }, { "submission_id": "aoj_1612_6775373", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <cstddef>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n/* macro */\n\n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)\n#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)\n#define RRep(i, a, n) for (i64 i = ((i64)(n - i64(1))); i >= (i64)(a); i--)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define Bit(n) (1LL << (n))\n\n/* macro end */\n\n/* template */\n\nnamespace ebi {\n\n#ifdef LOCAL\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\nvoid debug_out() { std::cerr << std::endl; }\n\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cout << \" :\";\n debug_out(t...);\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {\n return os << pa.first << \" \" << pa.second;\n}\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {\n return os >> pa.first >> pa.second;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {\n for (std::size_t i = 0; i < vec.size(); i++)\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n\ntemplate <typename T>\nstd::istream &operator>>(std::istream &os, std::vector<T> &vec) {\n for (T &e : vec) std::cin >> e;\n return os;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {\n if (opt) {\n os << opt.value();\n } else {\n os << \"invalid value\";\n }\n return os;\n}\n\nusing std::size_t;\nusing i32 = std::int32_t;\nusing u32 = std::uint32_t;\nusing i64 = std::int64_t;\nusing u64 = std::uint64_t;\n\ntemplate <class T, T init>\nauto make_vector(int n) {\n return std::vector<T>(n, init);\n}\n\ntemplate <class T, T init, typename Head, typename... Tail>\nauto make_vector(Head n, Tail... ts) {\n return std::vector(n, make_vector<T, init>(ts...));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\nT pow(T x, i64 n) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = res * x;\n x = x * x;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT mod_pow(T x, i64 n, i64 mod) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT scan() {\n T val;\n std::cin >> val;\n return val;\n}\n\ntemplate <class T>\nstruct Edge {\n int to;\n T cost;\n Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}\n};\n\ntemplate <class T>\nstruct Graph : std::vector<std::vector<Edge<T>>> {\n using std::vector<std::vector<Edge<T>>>::vector;\n void add_edge(int u, int v, T w, bool directed = false) {\n (*this)[u].emplace_back(v, w);\n if (directed) return;\n (*this)[v].emplace_back(u, w);\n }\n};\n\nstruct graph : std::vector<std::vector<int>> {\n using std::vector<std::vector<int>>::vector;\n void add_edge(int u, int v, bool directed = false) {\n (*this)[u].emplace_back(v);\n if (directed) return;\n (*this)[v].emplace_back(u);\n }\n};\n\nconstexpr i64 LNF = std::numeric_limits<i64>::max() / 4;\n\nconstexpr int INF = std::numeric_limits<int>::max() / 2;\n\nconst std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};\nconst std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};\n\n} // namespace ebi\n\nnamespace ebi {\n\nvoid main_() {\n int n, k, s;\n while (std::cin >> n >> k >> s, !(n == 0 && k == 0 && s == 0)) {\n std::vector<std::tuple<i64, i64, i64>> ps(n);\n for (auto &[x, y, z] : ps) {\n std::cin >> x >> y >> z;\n }\n if(k == 1) {\n std::cout << i64(s) * i64(s) * 6 << '\\n';\n continue;\n }\n Graph<i64> g(n);\n auto f = [&](int i, int j) -> std::vector<std::tuple<i64, i64, i64>> {\n std::vector<std::tuple<i64, i64, i64>> ret;\n auto [x, y, z] = ps[i];\n auto [nx, ny, nz] = ps[j];\n rep(a, 0, 2) {\n rep(b, 0, 2) {\n rep(c, 0, 2) {\n int px = nx + s * a;\n int py = ny + s * b;\n int pz = nz + s * c;\n if (x <= px && px <= x + s && y <= py && py <= y + s &&\n z <= pz && pz <= z + s) {\n ret.emplace_back(px, py, pz);\n }\n }\n }\n }\n return ret;\n };\n rep(i, 0, n) {\n for (int j = i + 1; j < n; j++) {\n auto ret = f(i, j);\n if (ret.size() == 0) continue;\n auto rhs = f(j, i);\n assert(rhs.size() == ret.size());\n ret.insert(ret.end(), all(rhs));\n\n i64 res = 0;\n\n\n if (ret.size() == 2) {\n auto [x, y, z] = ret[0];\n auto [nx, ny, nz] = ret[1];\n res = 2 * (std::abs(nx - x) * std::abs(ny - y) +\n std::abs(ny - y) * std::abs(nz - z) +\n std::abs(nz - z) * std::abs(nx - x));\n } else if (ret.size() == 4 || ret.size() == 8) {\n auto [x, y, z] = ret[0];\n int idx = ret.size() / 2;\n while (idx < (int)ret.size()) {\n auto [nx, ny, nz] = ret[idx];\n if (x == nx || y == ny || z == nz) {\n idx++;\n }\n else {\n break;\n }\n }\n auto [nx, ny, nz] = ret[idx];\n res = 2 * (std::abs(nx - x) * std::abs(ny - y) +\n std::abs(ny - y) * std::abs(nz - z) +\n std::abs(nz - z) * std::abs(nx - x));\n } else {\n assert(0);\n }\n g.add_edge(i, j, res);\n }\n }\n std::vector<int> seen(n, -1);\n i64 ans = -1;\n std::vector<i64> a;\n int root = -1;\n auto dfs = [&](auto &&self, int v, int par = -1) -> void {\n seen[v] = 1;\n for (auto [nv, cost] : g[v]) {\n if (nv == par) continue;\n if(nv == root) {\n a.emplace_back(cost);\n continue;\n }\n if (seen[nv] < 0) {\n a.emplace_back(cost);\n self(self, nv, v);\n }\n }\n return;\n };\n rep(i, 0, n) {\n if (g[i].size() == 1 && seen[i] < 0) {\n a.clear();\n dfs(dfs, i);\n if ((int)a.size() < k - 1) continue;\n i64 sum = 0;\n rep(j,0,k-1) {\n sum += a[j];\n }\n chmax(ans, sum);\n int l = 0, r = k-1;\n while(r < (int)a.size()) {\n sum -= a[l++];\n sum += a[r++];\n chmax(ans, sum);\n }\n }\n }\n rep(i,0,n) {\n if(seen[i] < 0) {\n a.clear();\n root = i;\n dfs(dfs, i);\n if((int)a.size() < k-1) continue;\n if((int)a.size() == k) {\n chmax(ans, std::accumulate(all(a), i64(0)));\n continue;\n }\n {\n auto b = a;\n a.insert(a.end(), all(b));\n }\n i64 sum = 0;\n rep(j,0,k-1) {\n sum += a[j];\n }\n int l = 0, r = k-1;\n while(r < (int)a.size()) {\n sum -= a[l++];\n sum += a[r++];\n chmax(ans, sum);\n }\n }\n }\n if(ans > 0) {\n ans = i64(s) * i64(s) * 6 * k - ans;\n }\n std::cout << ans << '\\n';\n }\n}\n\n} // namespace ebi\n\nint main() {\n std::cout << std::fixed << std::setprecision(15);\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n int t = 1;\n // std::cin >> t;\n while (t--) {\n ebi::main_();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3480, "score_of_the_acc": -0.0628, "final_rank": 6 }, { "submission_id": "aoj_1612_6717176", "code_snippet": "using namespace std;\n#include <bits/stdc++.h>\n\n#define ll long long\n#define ul unsigned long\n#define dd long double\n#define MAX 9223372036854775807\n#define REP(i,st,N) for(ll i=st; i<N; i++)\n\nll sp;\n\nll solve(vector<ll>& sq1, vector<ll>& sq2, ll s) {\n\tvector<ll> e(3);\n\tREP(i, 0, 3) {\n\t\tll mi = min(sq1[i], sq2[i]);\n\t\tll ma = max(sq1[i], sq2[i]);\n\t\te[i] = max(mi + s - ma, 0LL);\n\t\tif (e[i] == 0) return 0;\n\t}\n\tll sum = 0;\n\tREP(i, 0, 3) sum += 2 * e[i] * e[(i + 1) % 3];\n\treturn sum;\n}\n\nll dfs(vector<vector<pair<ll, ll>>>& g, ll st, ll pre, ll k, ll initK, vector<bool>& done) {\n\tll ans = -1;\n\tif (k == 0) {\n\t\tfor (pair<ll, ll> p : g[st]) if (p.first == sp && initK != 2) return p.second;\n\t\treturn 0;\n\t}\n\n\tfor (pair<ll, ll> p : g[st]) if (p.first != pre && !done[p.first]) {\n\t\tdone[p.first] = true;\n\t\tll t = dfs(g, p.first, st, k - 1, initK, done);\n\t\tif (t >= 0) t += p.second;\n\t\tans = max(ans, t);\n\t\tdone[p.first] = false;\n\t}\n\treturn ans;\n}\n\nint main() {\n\twhile (1) {\n\t\tll n, k, s; cin >> n >> k >> s;\n\t\tif (!(n || k || s)) break;\n\t\tvector<vector<ll>> sq;\n\t\tll ma = 6 * s * s * k;\n\t\tREP(i, 0, n) {\n\t\t\tvector<ll> c(3);\n\t\t\tcin >> c[0] >> c[1] >> c[2];\n\t\t\tsq.push_back(c);\n\t\t}\n\t\tif (n == 1 || k == 1) {\n\t\t\tcout << ma << endl;\n\t\t\tcontinue;\n\t\t}\n\t\tvector<vector<pair<ll, ll>>> g(n);\n\t\tREP(i, 0, n) REP(j, i + 1, n) {\n\t\t\tll x = solve(sq[i], sq[j], s);\n\t\t\tif (x > 0) {\n\t\t\t\tg[i].push_back({ j,x });\n\t\t\t\tg[j].push_back({ i,x });\n\t\t\t}\n\t\t}\n\t\tll x = 0;\n\t\tvector<bool> done(n, false);\n\t\tREP(i, 0, n) {\n\t\t\tsp = i;\n\t\t\tx = max(x, dfs(g, i, -1, k - 1,k, done));\n\t\t}\n\t\tif (x <= 0) cout << -1 << endl;\n\t\telse cout << ma - x << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3456, "score_of_the_acc": -0.0564, "final_rank": 5 }, { "submission_id": "aoj_1612_6637783", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing vl = vector<ll>;\ntemplate<class T> using vc = vector<T>;\ntemplate<class T> using vvc = vector<vector<T>>;\n\n#define eb emplace_back\n#define all(x) (x).begin(), (x).end()\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)\n#define repe(i, l, r) for (ll i = (l); i < (r); i++)\n#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)\n#define repa(i,n) for (auto& i: n)\n\ntemplate<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}\n\nstruct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;\n\ntemplate <typename T, typename U> ostream& operator<<(ostream&out,const pair<T, U>&a){return out<<a.first<<' '<<a.second;}\ntemplate <typename T> ostream& operator<<(ostream&out, const vector<T>&a){for(auto it=a.begin();it!= a.end();){out<<*it;if(++it!=a.end())out<<' ';}return out;}\ntemplate <typename T, size_t N> ostream& operator<<(ostream&out, const array<T, N>&a){for(auto it=a.begin();it!= a.end();){out<<*it;if(++it!=a.end())out<<' ';}return out;}\ntemplate <typename T> ostream& operator<<(ostream&out, const set<T>&a){for(auto it=a.begin();it!=a.end();){out<<*it;if(++it!=a.end())out<<' ';}return out;}\ntemplate <typename T, typename U> ostream& operator<<(ostream&out, const map<T, U>&a){for(auto it=a.begin();it!=a.end();){out<<*it;if(++it!=a.end())out<<'\\n';}return out;}\n\n#ifdef DEBUG\ntemplate <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << \" \"; cerr << endl; }\ntemplate <class T, class N, size_t AN> void verr(const array<T, AN>& a, const N& n) { rep(i, n) cerr << a[i] << \" \"; cerr << endl; }\nll dbgt = 1; void err() { cerr << \"passed \" << dbgt++ << endl; }\ntemplate<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?\" \":\"\\n\") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }\n#else\nvoid err(){}\ntemplate <class H, class... T>\nvoid err(H &&h, T &&...t) {}\ntemplate <class H, class... T>\nvoid verr(H &&h, T &&...t) {}\n#endif\n\nconst ll INF = 4e18;\nconst ld EPS = 1e-11;\nconst ld PI = acos(-1.0L);\n// const ll MOD = 1e9 + 7;\nconst ll MOD = 998244353;\n//--------------------------------------------------------------------------------//\nstruct point {\n int x, y, z;\n point(){}\n point(int x, int y, int z): x(x), y(y), z(z){}\n};\n\nstruct UnionFind {\nprivate:\n vector<int> par;\n vector<int> count;\n vector<int> rank;\npublic:\n UnionFind(int N) {\n count.assign(N, 1);\n rank.assign(N, 0);\n par.assign(N, 0);\n rep(i, N) par[i] = i;\n }\n\n int root(int x) {\n if (par[x] == x)\n return x;\n else {\n par[x] = root(par[x]);\n return par[x];\n }\n }\n\n void unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return;\n if (rank[x] < rank[y])\n swap(x, y);\n else if (rank[x] == rank[y])\n rank[y]++;\n par[y] = x;\n count[x] += count[y];\n return;\n }\n\n int size(int x) {\n return count[root(x)];\n }\n\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n};\nusing UF = struct UnionFind;\n\n\nvoid solve(int N, int K, int S){\n // err(N, K, S);\n vc<point> ps(N);\n rep(i, N) cin >> ps[i].x >> ps[i].y >> ps[i].z;\n if(K == 1) {\n cout << 6 * S * S << endl;\n return;\n }\n\n auto dup = [&](point p1, point p2) -> int {\n ll s = 1, ssum = 0;\n if (p1.x >= p2.x + S or p2.x >= p1.x + S) return 0;\n int dx = min(p1.x + S - p2.x, p2.x + S - p1.x);\n if(p1.y >= p2.y + S or p2.y >= p1.y + S) return 0;\n int dy = min(p1.y + S - p2.y, p2.y + S - p1.y);\n if (p1.z >= p2.z + S or p2.z >= p1.z + S) return 0;\n int dz = min(p1.z + S - p2.z, p2.z + S - p1.z);\n ssum = 2 * (dx * dy + dy * dz + dz * dx);\n return ssum;\n };\n\n UF ufall(N);\n vvc<pair<int, int>> G(N);\n rep(i, N) repe(j, i + 1, N) {\n auto s = dup(ps[i], ps[j]);\n if(s > 0) {\n G[i].eb(j, s);\n G[j].eb(i, s);\n ufall.unite(i, j);\n }\n }\n\n ll ans = INF;\n rep(rt, N) {\n if (ufall.size(rt) < K) continue;\n\n bool iscycle = true;\n int v1;\n rep(i, N) {\n if (!ufall.issame(i, rt)) continue;\n if (G[i].size() < 2) {\n iscycle = false;\n v1 = i;\n }\n }\n\n if (iscycle) {\n vl es;\n int cur = rt, par = G[cur][0].first;\n int ncur, npar;\n do {\n for(auto[to, ts]: G[cur]) {\n if (to == par) continue;\n es.eb(ts);\n tie(ncur, npar) = {to, cur};\n }\n // err(\"cp\", cur, par);\n tie(cur, par) = {ncur, npar};\n } while (cur != rt);\n int en = es.size();\n if(ufall.size(rt) == K) {\n chmin(ans, K * 6 * S * S - accumulate(all(es), 0ll));\n // err(\"pass\", K * 6 * S * S - accumulate(all(es), 0ll), en);\n continue;\n }\n vl acc(en * 2 + 1);\n rep(i, 2 * en) acc[i + 1] = acc[i] + es[i % en];\n repe(i, K - 1, en * 2) chmin(ans, K * 6 * S * S - (acc[i] - acc[i - K + 1]));\n } else {\n vl es;\n int cur = v1, par = -1;\n int ncur, npar;\n while (true) {\n bool update = false;\n for(auto[to, ts]: G[cur]) {\n if (to == par) continue;\n es.eb(ts);\n update = true;\n tie(ncur, npar) = {to, cur};\n }\n if (!update) break;\n tie(cur, par) = {ncur, npar};\n }\n int en = es.size();\n vl acc(en + 1);\n rep(i, en) acc[i + 1] = acc[i] + es[i];\n repe(i, K - 1, en + 1) chmin(ans, K * 6 * S * S - (acc[i] - acc[i - K + 1]));\n }\n }\n cout << (ans == INF ? -1 : ans) << endl;\n}\n\nint main() {\n int counter = 0;\n while (true) {\n int N, K, S;\n cin >> N >> K >> S;\n counter++;\n if (N == 0) break;\n solve(N, K, S);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3440, "score_of_the_acc": -0.0522, "final_rank": 4 }, { "submission_id": "aoj_1612_6026797", "code_snippet": "//Let's join Kaede Takagaki Fan Club !!\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cassert>\n#include <string>\n#include <algorithm>\n#include <vector>\n#include <queue>\n#include <stack>\n#include <functional>\n#include <iostream>\n#include <map>\n#include <set>\n#include <unordered_map>\n#include <unordered_set>\n#include <cassert>\n#include <iomanip>\n#include <chrono>\n#include <random>\n#include <bitset>\n#include <complex>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\n#define int long long\n//#define L __int128\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\ntypedef pair<P,P> P2;\n#define pu push\n#define pb push_back\n#define eb emplace_back\n#define mp make_pair\n#define eps 1e-7\n#define INF 1000000000\n#define a first\n#define b second\n#define fi first\n#define sc second\n#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,x) for(int i=0;i<x;i++)\n#define repn(i,x) for(int i=1;i<=x;i++)\n#define SORT(x) sort(x.begin(),x.end())\n#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())\n#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())\n#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())\n#define all(x) x.begin(),x.end()\n#define si(x) int(x.size())\n \n#ifdef LOCAL\n#define dmp(x) cerr<<__LINE__<<\" \"<<#x<<\" \"<<x<<endl\n#else\n#define dmp(x) void(0)\n#endif\n \ntemplate<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}\ntemplate<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}\n \ntemplate<class t> using vc=vector<t>;\n \ntemplate<class t,class u>\nostream& operator<<(ostream& os,const pair<t,u>& p){\n\treturn os<<\"{\"<<p.fi<<\",\"<<p.sc<<\"}\";\n}\n \ntemplate<class t> ostream& operator<<(ostream& os,const vc<t>& v){\n\tos<<\"{\";\n\tfor(auto e:v)os<<e<<\",\";\n\treturn os<<\"}\";\n}\n \n\ntemplate<class T>\nvoid o(const T &a,bool space=false){\n\tcout << a << (space?' ':'\\n');\n}\n//ios::sync_with_stdio(false);\n//const ll mod = 998244353;\nconst ll mod = 1000000007;\nmt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());\ntemplate<class T>\nvoid add(T&a,T b){\n\ta+=b;\n\tif(a >= mod) a-=mod;\n}\nll modpow(ll x,ll n){\n\tll res=1;\n\twhile(n>0){\n\t\tif(n&1) res=res*x%mod;\n\t\tx=x*x%mod;\n\t\tn>>=1;\n\t}\n\treturn res;\n}\n#define _sz 1\nll F[_sz],R[_sz];\nvoid make(){\n\tF[0] = 1;\n\tfor(int i=1;i<_sz;i++) F[i] = F[i-1]*i%mod;\n\tR[_sz-1] = modpow(F[_sz-1], mod-2);\n\tfor(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1) % mod;\n}\nll C(int a,int b){\n\tif(b < 0 || a < b) return 0;\n\treturn F[a]*R[b]%mod*R[a-b]%mod;\n}\n//複数テストケースなので積み上げる変数はローカルに!\n//置けないなら必ず初期化!\nint n, k, s, x[2005], y[2005], z[2005];\nvoid solve(){\n\t//最初の入力は済んでいる\n\trep(i, n) cin >> x[i] >> y[i] >> z[i];\n\tauto area = [&](P p, P q){\n\t\tint x = min(p.a + s, q.a + s) - max(p.a, q.a);\n\t\tint y = min(p.b + s, q.b + s) - max(p.b, q.b);\n\t\tchmax(x, 0LL);\n\t\tchmax(y, 0LL);\n\t\treturn x*y;\n\t};\n\tvc<vc<P>>edge(n);\n\trep(i, n){\n\t\trep(j, i){\n\t\t\tint f = area(mp(x[j], y[j]), mp(x[i], y[i]));\n\t\t\tint g = area(mp(x[j], z[j]), mp(x[i], z[i]));\n\t\t\tint h = area(mp(z[j], y[j]), mp(z[i], y[i]));\n\t\t\tif(!f or !g or !h) continue;\n\t\t\t\n\t\t\tedge[i].eb(j, f + g + h);\n\t\t\tedge[j].eb(i, f + g + h);\n\t\t}\n\t}\n\tint ans = -1e18;\n\trep(i, n){\n\t\tif(k == 1){\n\t\t\tchmax(ans, 0LL);\n\t\t\tcontinue;\n\t\t}\n\t\tfor(auto a:edge[i]){\n\t\t\tvc<int>vis(n, 0);\n\t\t\tvis[i] = 1;\n\t\t\t\n\t\t\tint cur = a.a;\n\t\t\tint tmp = a.b;\n\t\t\tint zan = k-1;\n\t\t\twhile(1){\n\t\t\t\tif(vis[cur]) break;\n\t\t\t\tvis[cur] = 1;\n\t\t\t\tzan --;\n\t\t\t\tif(zan == 0) {\n\t\t\t\t\tif(k > 2){\n\t\t\t\t\t\tfor(auto a:edge[cur]){\n\t\t\t\t\t\t\tif(a.a == i){\n\t\t\t\t\t\t\t\ttmp += a.b;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tfor(auto a:edge[cur]){\n\t\t\t\t\tif(!vis[a.a]) {\n\t\t\t\t\t\tcur = a.a;\n\t\t\t\t\t\ttmp += a.b;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(!zan) chmax(ans, tmp);\n\t\t}\n\t}\n\tif(ans < 0) o(-1);\n\telse o(2LL * (3LL*s*s*k - ans));\n}\nsigned main(){\n\t//cin.tie(0);\n\tios::sync_with_stdio(0);\n\tcout<<fixed<<setprecision(20);\n\twhile(true){\n\t\tcin>>n>>k>>s;\n\t\t//終了条件\n\t\tif(!n) return 0;\n\t\tsolve();\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3244, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1612_6022554", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <climits>\n#include <string>\n#include <cmath>\n#include <bitset>\n#include <complex>\n#include <functional>\n#include <ctime>\n#include <cassert>\n#include <fstream>\n#include <stack>\n#include <random>\n#include <iomanip>\n#include <time.h>\n#include <list>\n#include <unordered_map>\n\n#include <algorithm>\n#include <array>\n\nusing namespace std;\ntypedef long long ll;\ntypedef long double dd;\ntypedef vector<ll> vl;\ntypedef vector<dd> vd;\ntypedef vector<vector<ll>> vvl;\ntypedef vector<vector<dd>> vvd;\n//#define i_7 (ll)(1E9+7)\n#define i_7 998244353\n#define i_5 i_7-2\nll mod(ll a){\n ll c=a%i_7;\n if(c>=0)return c;\n return c+i_7;\n}\ntypedef pair<ll,ll> l_l;\ntypedef pair<dd,dd> d_d;\nll inf=(ll)1E18;\n#define rep(i,l,r) for(ll i=l;i<=r;i++)\n#define pb push_back\nll max(ll a,ll b){if(a<b)return b;else return a;}\nll min(ll a,ll b){if(a>b)return b;else return a;}\ndd EPS=1E-6;\ndd PI=acos(-1);\n#define endl \"\\n\"\n#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n\n\n\nstruct Edge{\n ll to,cost;\n Edge(ll to, ll cost){\n this->to = to;\n this->cost = cost;\n }\n};\n\nll hoge;\n\nvoid dfs(ll idx, vector<bool> &seen, const vector<vector<Edge>> &v, vector<ll> &tmp){\n seen[idx] = true;\n hoge=idx;\n for(Edge e : v[idx]){\n if(seen[e.to])continue;\n tmp.pb(e.cost);\n dfs(e.to, seen, v, tmp);\n }\n}\n\n\nsigned main(){fastio\n while(true){\n ll n,K,s;cin>>n>>K>>s;\n if(n==0)break;\n vvl x(n);\n rep(i,0,n-1){\n x[i].resize(3);\n cin>>x[i][0]>>x[i][1]>>x[i][2];\n }\n vector<vector<Edge>> v(n);\n rep(i,0,n-1){\n rep(j,0,n-1){\n if(i>=j)continue;\n bool ok = true;\n vl u;\n rep(k,0,2){\n if(x[j][k]<=x[i][k] && x[i][k]<=x[j][k]+s){\n u.pb(x[j][k]+s-x[i][k]);\n }else if(x[i][k]<=x[j][k] && x[j][k]<=x[i][k]+s){\n u.pb(x[i][k]+s-x[j][k]);\n }else{\n ok = false;\n }\n }\n if(ok){\n ll pro=0;\n rep(k,0,2){\n rep(l,0,2){\n if(k<l){\n pro += 2*u[k]*u[l];\n }\n }\n }\n ll cost = pro;\n v[i].pb(Edge(j,cost));\n v[j].pb(Edge(i,cost));\n }\n }\n }\n vector<bool> seen(n, false);\n ll ans=inf;\n rep(i,0,n-1){\n if(seen[i])continue;\n if(v[i].size() == 0){\n if(K==1){\n ans = min(ans, 6*s*s);\n }\n continue;\n }\n vector<ll> tm;\n bool loop = false;\n if(v[i].size() == 1){\n dfs(i, seen, v, tm);\n }else{\n seen[i] = true;\n vector<ll> tmp;\n tmp.pb(v[i][0].cost);\n dfs(v[i][0].to, seen, v, tmp);\n if(hoge == v[i][1].to)loop = true;\n vector<ll> tmp2;\n tmp2.pb(v[i][1].cost);\n if(!seen[v[i][1].to]){\n dfs(v[i][1].to, seen, v, tmp2);\n }\n reverse(tmp2.begin(), tmp2.end());\n for(ll co : tmp2){\n tm.pb(co);\n }\n for(ll co : tmp){\n tm.pb(co);\n }\n }\n ll num;\n if(loop){\n num = tm.size();\n }else{\n num = tm.size() + 1;\n }\n if(num < K){\n continue;\n }\n ll m = tm.size();\n vl sum(m+1,0);\n rep(i,1,m){\n sum[i] = sum[i-1] + tm[i-1];\n }\n ll maxi = 0;\n if(loop){\n if(K==num){\n maxi = sum[m];\n }else{\n rep(j,K-1,m+(K-1)-1){\n if(j<=m){\n maxi = max(maxi, sum[j]-sum[j-(K-1)]);\n }else{\n maxi = max(maxi, sum[m]-sum[j-(K-1)]+sum[j-m]);\n }\n }\n }\n }else{\n rep(j,K-1,m){\n maxi = max(maxi, sum[j]-sum[j-(K-1)]);\n }\n }\n ans = min(ans, 6*s*s*K - maxi);\n }\n if(ans == inf){\n cout<<-1<<endl;\n }else{\n cout<<ans<<endl;\n }\n }\n \n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3508, "score_of_the_acc": -0.0703, "final_rank": 7 }, { "submission_id": "aoj_1612_6015570", "code_snippet": "#include<bits/stdc++.h>\nusing ll = long long;\n#define var auto\nconst char newl = '\\n';\n\nusing namespace std;\n\ntemplate<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }\ntemplate<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }\n\nconst ll inf = INT32_MAX;\n\nstruct Box;\nbool ow(Box a, Box b);\n\nll w;\n\nstruct Box {\n ll x, y, z;\n ll s() {\n return w * w * 6;\n }\n ll eats(Box& a) {\n if (not ow(*this, a)) return 0;\n var dx = w - abs(x - a.x);\n var dy = w - abs(y - a.y);\n var dz = w - abs(z - a.z);\n assert(dx <= w && dy <= w && dz <= w);\n ll res = 0;\n return (dx * dy + dy * dz + dz * dx) * 2;\n }\n};\n\nbool ow(Box a, Box b) {\n ll maxdist = 0;\n chmax(maxdist, abs(a.x - b.x));\n chmax(maxdist, abs(a.y - b.y));\n chmax(maxdist, abs(a.z - b.z));\n assert(maxdist != w);\n return maxdist < w;\n}\n\nstruct Boxes {\n ll s = 0;\n deque<Box> q;\n Box prev2;\n Box prev1;\n\n void check() {\n ll res = 0;\n for (int i = 0; i < q.size(); i++) {\n res += q[i].s();\n if (i == 0) continue;\n res -= q[i].eats(q[i - 1]);\n }\n if (res != s) {\n cout << \"\";\n }\n assert(res == s);\n }\n\n void push(Box cur) {\n check();\n if (q.size() != 0) {\n s -= cur.eats(q.back());\n }\n s += cur.s();\n q.emplace_back(cur);\n }\n void pop() {\n check();\n assert(!q.empty());\n var popped = q.front(); q.pop_front();\n s -= popped.s();\n if (q.size() == 0) return;\n s += popped.eats(q.front());\n }\n};\n\nstruct UnionFind {\n int num;\n vector<int> rs, ps;\n UnionFind(int n) :num(n), rs(n, 1), ps(n, 0) {\n iota(ps.begin(), ps.end(), 0);\n }\n int find(int x) {\n return (x == ps[x] ? x : ps[x] = find(ps[x]));\n }\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n void unite(int x, int y) {\n x = find(x); y = find(y);\n if (x == y) return;\n if (rs[x] < rs[y]) swap(x, y);\n rs[x] += rs[y];\n ps[y] = x;\n num--;\n }\n int size(int x) {\n return rs[find(x)];\n }\n int count() const {\n return num;\n }\n};\n\nbool solve() {\n int n, k;\n cin >> n >> k >> w;\n if (n == 0) return false;\n vector<Box> v(n);\n for (var&& item : v) cin >> item.x >> item.y >> item.z;\n\n vector<vector<int>> graph(n);\n UnionFind uf(n);\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n if (ow(v[i], v[j])) {\n uf.unite(i, j);\n graph[i].emplace_back(j);\n graph[j].emplace_back(i);\n }\n }\n }\n\n\n const ll inf = LLONG_MAX;\n ll res = inf;\n\n for (int id = 0; id < n; id++) {\n bool has_point = false;\n int first = -1;\n for (int i = 0; i < n; i++) {\n if (uf.find(i) != id) continue;\n if (graph[i].size() == 1) {\n if (not has_point) {\n first = i;\n }\n has_point = true;\n continue;\n }\n if (not has_point) {\n first = i;\n }\n }\n\n if (first == -1) continue;\n if (uf.size(first) < k) continue;\n\n int last = -1;\n int cur = first;\n Boxes b; b.push(v[cur]);\n\n while (b.q.size() < k) {\n for (var&& adj : graph[cur]) {\n if (adj == last) continue;\n b.push(v[adj]);\n last = cur;\n cur = adj;\n break;\n }\n }\n\n assert(b.q.size() == k);\n\n if (k != 1 && uf.size(first) == k && not has_point) {\n b.s -= b.q.front().eats(b.q.back());\n chmin(res, b.s);\n continue;\n }\n\n ll curres = b.s;\n\n int cnt = has_point ? (uf.size(first) - k) : (uf.size(first) - 1);\n for (int _ = 0; _ < cnt; _++) {\n b.pop();\n for (var&& adj : graph[cur]) {\n if (adj == last) continue;\n b.push(v[adj]);\n last = cur;\n cur = adj;\n break;\n }\n assert(b.q.size() == k);\n chmin(curres, b.s);\n }\n\n chmin(res, curres);\n }\n if (res == inf) res = -1;\n cout << res << endl;\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n while (solve());\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3432, "score_of_the_acc": -0.0501, "final_rank": 2 }, { "submission_id": "aoj_1612_6011336", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n//using namespace atcoder;\n#pragma GCC target (\"avx2\")\n#pragma GCC optimization (\"O3\")\n#pragma GCC optimization (\"unroll-loops\")\nusing namespace std;\n \ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<string> VS;\ntypedef pair<int, int> PII;\ntypedef pair<int, int> pii;\ntypedef pair<long long, long long> PLL;\ntypedef pair<int, PII> TIII;\n \ntypedef long long ll;\ntypedef long double ld;\ntypedef unsigned long long ull;\n \n \n#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define trav(a, x) for (auto &a : x)\n#define all(x) x.begin(), x.end()\n \n#define MOD 1000000007\n \ntemplate<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}\nconst double EPS = 1e-8, PI = acos(-1);\nconst double pi = 3.141592653589793238462643383279;\n//ここから編集 \ntypedef string::const_iterator State;\nll GCD(ll a, ll b){\n return (b==0)?a:GCD(b, a%b);\n}\nll LCM(ll a, ll b){\n return a/GCD(a, b) * b;\n}\ntemplate< int mod >\nstruct ModInt {\n int x;\n \n ModInt() : x(0) {}\n \n ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n \n ModInt &operator+=(const ModInt &p) {\n if((x += p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator-=(const ModInt &p) {\n if((x += mod - p.x) >= mod) x -= mod;\n return *this;\n }\n \n ModInt &operator*=(const ModInt &p) {\n x = (int) (1LL * x * p.x % mod);\n return *this;\n }\n \n ModInt &operator/=(const ModInt &p) {\n *this *= p.inverse();\n return *this;\n }\n \n ModInt operator-() const { return ModInt(-x); }\n \n ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }\n \n ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }\n \n ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }\n \n ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }\n \n bool operator==(const ModInt &p) const { return x == p.x; }\n \n bool operator!=(const ModInt &p) const { return x != p.x; }\n \n ModInt inverse() const {\n int a = x, b = mod, u = 1, v = 0, t;\n while(b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return ModInt(u);\n }\n \n ModInt pow(int64_t n) const {\n ModInt ret(1), mul(x);\n while(n > 0) {\n if(n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n \n friend ostream &operator<<(ostream &os, const ModInt &p) {\n return os << p.x;\n }\n \n friend istream &operator>>(istream &is, ModInt &a) {\n int64_t t;\n is >> t;\n a = ModInt< mod >(t);\n return (is);\n }\n \n static int get_mod() { return mod; }\n};\n \nusing modint = ModInt< 998244353 >;\ntemplate< typename T >\nstruct Combination {\n vector< T > _fact, _rfact, _inv;\n \n Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {\n _fact[0] = _rfact[sz] = _inv[0] = 1;\n for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;\n _rfact[sz] /= _fact[sz];\n for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);\n for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];\n }\n \n inline T fact(int k) const { return _fact[k]; }\n \n inline T rfact(int k) const { return _rfact[k]; }\n \n inline T inv(int k) const { return _inv[k]; }\n \n T P(int n, int r) const {\n if(r < 0 || n < r) return 0;\n return fact(n) * rfact(n - r);\n }\n \n T C(int p, int q) const {\n if(q < 0 || p < q) return 0;\n return fact(p) * rfact(q) * rfact(p - q);\n }\n \n T H(int n, int r) const {\n if(n < 0 || r < 0) return (0);\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\n \nll modpow(ll x, ll n, ll mod) {\n ll res = 1;\n x %= mod;\n if(x == 0) return 0;\n while(n) {\n if(n&1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n} \ninline long long mod(long long a, long long m) {\n return (a % m + m) % m;\n}\ntemplate<typename T> \nstruct BIT{\n int N;\n std::vector<T> node;\n BIT(){}\n BIT(int n){\n N = n;\n node.resize(N+10);\n }\n void build(int n) {\n N = n;\n node.resize(N+10);\n }\n /* a: 1-indexed */\n void add(int a, T x){\n for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;\n }\n\n /* [1, a] */\n T sum(int a){\n T res = 0;\n for(int x=a; x>0; x -= x & -x) res += node[x];\n return res;\n }\n\n /* [l, r] */\n T rangesum(int l, int r){\n return sum(r) - sum(l-1);\n }\n\n /* \n a1+a2+...+aw >= valとなるような最小のwを返す\n @verify https://codeforces.com/contest/992/problem/E\n */\n int lower_bound(T val) {\n if(val < 0) return 0;\n\n int res = 0;\n int n = 1; \n while (n < N) n *= 2;\n\n T tv=0;\n for (int k = n; k > 0; k /= 2) {\n if(res + k < N && node[res + k] < val){\n val -= node[res+k];\n res += k;\n }\n }\n return res+1; \n }\n};\nstruct UnionFind{\n std::vector<int> par;\n std::vector<int> siz;\n\n UnionFind(int sz_): par(sz_), siz(sz_) {\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n void init(int sz_){\n par.resize(sz_);\n siz.resize(sz_);\n for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;\n }\n\n int root(int x){\n if(x == par[x]) return x;\n return par[x] = root(par[x]);\n }\n\n bool merge(int x, int y){\n x = root(x), y = root(y);\n if(x == y) return false;\n if(siz[x] < siz[y]) std::swap(x, y);\n siz[x] += siz[y];\n par[y] = x;\n return true;\n }\n\n bool issame(int x, int y){\n return root(x) == root(y);\n }\n\n int size(int x){\n return siz[root(x)];\n }\n};\nstruct RollingHash{\n\n using ull = unsigned long long;\n const ull mod = (1ULL << 61) - 1;\n const ull MASK30 = (1ULL << 30) - 1;\n const ull MASK31 = (1ULL << 31) - 1;\n\n const ull MASK61 = mod;\n\n ull base;\n int n;\n vector<ull> hash, pow;\n\n RollingHash(const string &s)\n {\n random_device rnd;\n mt19937_64 mt(rnd());\n base = 1001;\n \n n = (int)s.size();\n hash.assign(n+1, 0);\n pow.assign(n+1, 1);\n \n for(int i=0; i<n; i++){\n hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);\n pow[i+1] = calc_mod(mul(pow[i], base));\n }\n }\n\n ull calc_mod(ull x){\n ull xu = x >> 61;\n ull xd = x & MASK61;\n ull res = xu + xd;\n if(res >= mod) res -= mod;\n return res;\n }\n\n ull mul(ull a, ull b){\n ull au = a >> 31;\n ull ad = a & MASK31;\n ull bu = b >> 31;\n ull bd = b & MASK31;\n ull mid = ad * bu + au * bd;\n ull midu = mid >> 30;\n ull midd = mid & MASK30;\n return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);\n }\n\n //[l,r)のハッシュ値\n inline ull get(int l, int r){\n ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));\n return res;\n }\n //string tのハッシュ値\n inline ull get(string t){\n ull res = 0;\n for(int i=0; i<t.size(); i++){\n res = calc_mod(mul(res, base)+t[i]);\n }\n return res;\n }\n //string s中のtの数をカウント\n inline int count(string t) {\n if(t.size() > n) return 0;\n auto hs = get(t);\n int res = 0;\n for(int i=0; i<n-t.size()+1; i++){\n if(get(i, i+t.size()) == hs) res++; \n }\n return res;\n }\n\n /* \n concat \n @verify https://codeforces.com/problemset/problem/514/C\n */\n inline ull concat(ull h1, ull h2, int h2len){\n return calc_mod(h2 + mul(h1, pow[h2len]));\n }\n\n // LCPを求める S[a:] T[b:]\n inline int LCP(int a, int b){\n int len = min((int)hash.size()-a, (int)hash.size()-b);\n \n int lb = -1, ub = len;\n while(ub-lb>1){\n int mid = (lb+ub)/2;\n\n if(get(a, a+mid) == get(b, b+mid)) lb = mid;\n else ub = mid;\n }\n return lb;\n }\n};\nint h;\nll ans = (1LL<<60);\nll x[2010], y[2010], z[2010];\nint n, k, s;\nset<int> st;\nvoid dfs(int v, int p, int nokori, vector<vector<pair<ll,ll>>> &g, ll sum) {\n ll t = sum;\n for(auto[u, cost]: g[v]) {\n if(u != p && st.find(u) != st.end()) t -= cost;\n }\n\n if(nokori == 0) {\n ans = min(ans, t);\n return ;\n }\n \n for(auto[u, cost]: g[v]) {\n if(p == u) continue;\n if(st.find(u) != st.end()) continue;\n st.insert(u);\n dfs(u, v, nokori-1, g, t + 6 * s * s - cost);\n st.erase(u);\n }\n\n}\nint main() { \n cin.tie(0);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(12);\n\n while(cin >> n >> k >> s) {\n if(n == 0 && k == 0 && s == 0) break;\n REP(i,n) cin >> x[i] >> y[i] >> z[i];\n vector<vector<pair<ll,ll>>> g(n);\n st.clear();\n ans = (1LL<<60);\n for(int i=0; i<n; i++) {\n for(int j=i+1; j<n; j++) {\n\n int ax = x[i], bx = x[i] + s, cx = x[j], dx = x[j] + s;\n if(bx < cx) continue;\n if(dx < ax) continue;\n int ay = y[i], by = y[i] + s, cy = y[j], dy = y[j] + s;\n if(by < cy) continue;\n if(dy < ay) continue;\n int az = z[i], bz = z[i] + s, cz = z[j], dz = z[j] + s;\n if(bz < cz) continue;\n if(dz < az) continue;\n\n \n vector<ll> a;\n if(ax <= cx && cx <= bx) a.push_back(bx-cx);\n else a.push_back(dx-ax);\n if(ay <= cy && cy <= by) a.push_back(by-cy);\n else a.push_back(dy-ay);\n if(az <= cz && cz <= bz) a.push_back(bz-cz);\n else a.push_back(dz-az);\n ll t = 2*a[0]*a[1]+2*a[0]*a[2]+2*a[1]*a[2];\n g[i].push_back({j,t});\n g[j].push_back({i,t});\n }\n }\n for(int i=0; i<n; i++) {\n st.insert(i);\n dfs(i, -1, k-1, g, 6 * s * s);\n st.erase(i);\n }\n if(ans == (1LL<<60)) ans = -1;\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3480, "score_of_the_acc": -0.6083, "final_rank": 16 }, { "submission_id": "aoj_1612_5990703", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i = 0; i < (n); i++)\nusing namespace std;\ntypedef long long ll;\n\nint main(){\n cin.tie(0);\n ios::sync_with_stdio(0);\n \n while(true) {\n int n,k,s; cin >> n >> k >> s; if(n == 0) break;\n vector<vector<pair<int,int>>> G(n);\n vector<int> x(n), y(n), z(n);\n rep(i,n) cin >> x[i] >> y[i] >> z[i];\n\n if(k == 1) { cout << 6 * s * s << endl; continue; }\n\n auto calc = [&](int i, int j) {\n int dx = s - abs(x[i] - x[j]);\n int dy = s - abs(y[i] - y[j]);\n int dz = s - abs(z[i] - z[j]);\n if(dx <= 0 || dy <= 0 || dz <= 0) return 0;\n return 2 * (dx * dy + dy * dz + dz * dx);\n };\n\n rep(i,n)rep(j,n)if(i < j) {\n int cost = calc(i, j);\n if(cost != 0) {\n G[i].push_back({j, cost});\n G[j].push_back({i, cost});\n }\n }\n\n int ans = 0;\n rep(S,n) {\n for(auto to : G[S]) {\n int T = to.first;\n vector<int> can;\n vector<bool> used(n, false);\n can.push_back(T); used[T] = true;\n can.push_back(S); used[S] = true;\n\n bool f = (k == 2);\n rep(_,k-2) {\n f = false;\n for(auto nt : G[T]) {\n if(!used[nt.first]) {\n f = true;\n used[nt.first] = true;\n T = nt.first;\n can.push_back(T);\n break;\n }\n }\n }\n\n if(!f) continue;\n int sum = 0;\n for(int i : can) for(int j : can) \n if(i < j) sum += calc(i, j);\n ans = max(ans, sum);\n }\n }\n \n cout << (ans == 0 ? -1 : 6 * s * s * k - ans) << endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3520, "score_of_the_acc": -1.0735, "final_rank": 20 }, { "submission_id": "aoj_1612_5943625", "code_snippet": "//#include<atcoder/all>\n\n#ifndef ATCODER_MODINT_HPP\n#define ATCODER_MODINT_HPP 1\n\n//#include <atcoder/internal_math>\n#ifndef ATCODER_INTERNAL_MATH_HPP\n#define ATCODER_INTERNAL_MATH_HPP 1\n\n#include <utility>\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param m `1 <= m`\n// @return x mod m\nconstexpr long long safe_mod(long long x, long long m) {\n x %= m;\n if (x < 0) x += m;\n return x;\n}\n\n// Fast moduler by barrett reduction\n// Reference: https://en.wikipedia.org/wiki/Barrett_reduction\n// NOTE: reconsider after Ice Lake\nstruct barrett {\n unsigned int _m;\n unsigned long long im;\n\n // @param m `1 <= m`\n barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}\n\n // @return m\n unsigned int umod() const { return _m; }\n\n // @param a `0 <= a < m`\n // @param b `0 <= b < m`\n // @return `a * b % m`\n unsigned int mul(unsigned int a, unsigned int b) const {\n // [1] m = 1\n // a = b = im = 0, so okay\n\n // [2] m >= 2\n // im = ceil(2^64 / m)\n // -> im * m = 2^64 + r (0 <= r < m)\n // let z = a*b = c*m + d (0 <= c, d < m)\n // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im\n // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2\n // ((ab * im) >> 64) == c or c + 1\n unsigned long long z = a;\n z *= b;\n#ifdef _MSC_VER\n unsigned long long x;\n _umul128(z, im, &x);\n#else\n unsigned long long x =\n (unsigned long long)(((unsigned __int128)(z)*im) >> 64);\n#endif\n unsigned int v = (unsigned int)(z - x * _m);\n if (_m <= v) v += _m;\n return v;\n }\n};\n\n// @param n `0 <= n`\n// @param m `1 <= m`\n// @return `(x ** n) % m`\nconstexpr long long pow_mod_constexpr(long long x, long long n, int m) {\n if (m == 1) return 0;\n unsigned int _m = (unsigned int)(m);\n unsigned long long r = 1;\n unsigned long long y = safe_mod(x, m);\n while (n) {\n if (n & 1) r = (r * y) % _m;\n y = (y * y) % _m;\n n >>= 1;\n }\n return r;\n}\n\n// Reference:\n// M. Forisek and J. Jancina,\n// Fast Primality Testing for Integers That Fit into a Machine Word\n// @param n `0 <= n`\nconstexpr bool is_prime_constexpr(int n) {\n if (n <= 1) return false;\n if (n == 2 || n == 7 || n == 61) return true;\n if (n % 2 == 0) return false;\n long long d = n - 1;\n while (d % 2 == 0) d /= 2;\n for (long long a : {2, 7, 61}) {\n long long t = d;\n long long y = pow_mod_constexpr(a, t, n);\n while (t != n - 1 && y != 1 && y != n - 1) {\n y = y * y % n;\n t <<= 1;\n }\n if (y != n - 1 && t % 2 == 0) {\n return false;\n }\n }\n return true;\n}\ntemplate <int n> constexpr bool is_prime = is_prime_constexpr(n);\n\n// @param b `1 <= b`\n// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g\nconstexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {\n a = safe_mod(a, b);\n if (a == 0) return {b, 0};\n\n // Contracts:\n // [1] s - m0 * a = 0 (mod b)\n // [2] t - m1 * a = 0 (mod b)\n // [3] s * |m1| + t * |m0| <= b\n long long s = b, t = a;\n long long m0 = 0, m1 = 1;\n\n while (t) {\n long long u = s / t;\n s -= t * u;\n m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b\n\n // [3]:\n // (s - t * u) * |m1| + t * |m0 - m1 * u|\n // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)\n // = s * |m1| + t * |m0| <= b\n\n auto tmp = s;\n s = t;\n t = tmp;\n tmp = m0;\n m0 = m1;\n m1 = tmp;\n }\n // by [3]: |m0| <= b/g\n // by g != b: |m0| < b/g\n if (m0 < 0) m0 += b / s;\n return {s, m0};\n}\n\n// Compile time primitive root\n// @param m must be prime\n// @return primitive root (and minimum in now)\nconstexpr int primitive_root_constexpr(int m) {\n if (m == 2) return 1;\n if (m == 167772161) return 3;\n if (m == 469762049) return 3;\n if (m == 754974721) return 11;\n if (m == 998244353) return 3;\n int divs[20] = {};\n divs[0] = 2;\n int cnt = 1;\n int x = (m - 1) / 2;\n while (x % 2 == 0) x /= 2;\n for (int i = 3; (long long)(i)*i <= x; i += 2) {\n if (x % i == 0) {\n divs[cnt++] = i;\n while (x % i == 0) {\n x /= i;\n }\n }\n }\n if (x > 1) {\n divs[cnt++] = x;\n }\n for (int g = 2;; g++) {\n bool ok = true;\n for (int i = 0; i < cnt; i++) {\n if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {\n ok = false;\n break;\n }\n }\n if (ok) return g;\n }\n}\ntemplate <int m> constexpr int primitive_root = primitive_root_constexpr(m);\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_MATH_HPP\n\n//#include <atcoder/internal_type_traits>\n#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP\n#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\nnamespace atcoder {\n\nnamespace internal {\n\n#ifndef _MSC_VER\ntemplate <class T>\nusing is_signed_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value ||\n std::is_same<T, __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int128 =\n typename std::conditional<std::is_same<T, __uint128_t>::value ||\n std::is_same<T, unsigned __int128>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing make_unsigned_int128 =\n typename std::conditional<std::is_same<T, __int128_t>::value,\n __uint128_t,\n unsigned __int128>;\n\ntemplate <class T>\nusing is_integral = typename std::conditional<std::is_integral<T>::value ||\n is_signed_int128<T>::value ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_signed_int = typename std::conditional<(is_integral<T>::value &&\n std::is_signed<T>::value) ||\n is_signed_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<(is_integral<T>::value &&\n std::is_unsigned<T>::value) ||\n is_unsigned_int128<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<\n is_signed_int128<T>::value,\n make_unsigned_int128<T>,\n typename std::conditional<std::is_signed<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type>::type;\n\n#else\n\ntemplate <class T> using is_integral = typename std::is_integral<T>;\n\ntemplate <class T>\nusing is_signed_int =\n typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing is_unsigned_int =\n typename std::conditional<is_integral<T>::value &&\n std::is_unsigned<T>::value,\n std::true_type,\n std::false_type>::type;\n\ntemplate <class T>\nusing to_unsigned = typename std::conditional<is_signed_int<T>::value,\n std::make_unsigned<T>,\n std::common_type<T>>::type;\n\n#endif\n\ntemplate <class T>\nusing is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;\n\ntemplate <class T>\nusing is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;\n\ntemplate <class T> using to_unsigned_t = typename to_unsigned<T>::type;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_INTERNAL_TYPE_TRAITS_HPP\n\n#include <cassert>\n#include <numeric>\n#include <type_traits>\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\nnamespace atcoder {\n\nnamespace internal {\n\nstruct modint_base {};\nstruct static_modint_base : modint_base {};\n\ntemplate <class T> using is_modint = std::is_base_of<modint_base, T>;\ntemplate <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;\n\n} // namespace internal\n\ntemplate <int m, std::enable_if_t<(1 <= m)>* = nullptr>\nstruct static_modint : internal::static_modint_base {\n using mint = static_modint;\n\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n static_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n static_modint(T v) {\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n static_modint(T v) {\n _v = (unsigned int)(v % umod());\n }\n static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n if (prime) {\n assert(_v);\n return pow(umod() - 2);\n } else {\n auto eg = internal::inv_gcd(_v, m);\n assert(eg.first == 1);\n return eg.second;\n }\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n static constexpr bool prime = internal::is_prime<m>;\n};\n\ntemplate <int id> struct dynamic_modint : internal::modint_base {\n using mint = dynamic_modint;\n\n public:\n static int mod() { return (int)(bt.umod()); }\n static void set_mod(int m) {\n assert(1 <= m);\n bt = internal::barrett(m);\n }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n\n dynamic_modint() : _v(0) {}\n template <class T, internal::is_signed_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n long long x = (long long)(v % (long long)(mod()));\n if (x < 0) x += mod();\n _v = (unsigned int)(x);\n }\n template <class T, internal::is_unsigned_int_t<T>* = nullptr>\n dynamic_modint(T v) {\n _v = (unsigned int)(v % mod());\n }\n dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }\n\n unsigned int val() const { return _v; }\n\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n\n mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator-=(const mint& rhs) {\n _v += mod() - rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n mint& operator*=(const mint& rhs) {\n _v = bt.mul(_v, rhs._v);\n return *this;\n }\n mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n\n mint operator+() const { return *this; }\n mint operator-() const { return mint() - *this; }\n\n mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n mint inv() const {\n auto eg = internal::inv_gcd(_v, mod());\n assert(eg.first == 1);\n return eg.second;\n }\n\n friend mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n\n private:\n unsigned int _v;\n static internal::barrett bt;\n static unsigned int umod() { return bt.umod(); }\n};\ntemplate <int id> internal::barrett dynamic_modint<id>::bt = 998244353;\n\nusing modint998244353 = static_modint<998244353>;\nusing modint1000000007 = static_modint<1000000007>;\nusing modint = dynamic_modint<-1>;\n\nnamespace internal {\n\ntemplate <class T>\nusing is_static_modint = std::is_base_of<internal::static_modint_base, T>;\n\ntemplate <class T>\nusing is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;\n\ntemplate <class> struct is_dynamic_modint : public std::false_type {};\ntemplate <int id>\nstruct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};\n\ntemplate <class T>\nusing is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;\n\n} // namespace internal\n\n} // namespace atcoder\n\n#endif // ATCODER_MODINT_HPP\n\n\n////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n#ifndef ATCODER_DSU_HPP\n#define ATCODER_DSU_HPP 1\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\n#endif // ATCODER_DSU_HPP\n\n\n\n\n\n\n\n\n\nusing namespace atcoder;\n\n#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n//typedef int ll;\n\nvoid mod_print(ll k){\n // const ll i_7=998244353;\n const ll i_7 =(ll)(1E9+7);\n auto mod=[&](ll a){\n ll c=a%i_7;\n if(c>=0)return c;\n return c+i_7;\n };\n ll P=50000;\n for(ll y=1;y<=P;y++){\n ll x=mod(y*k);\n if(abs(x)<=P||x+P>=i_7){\n if(x+P>=i_7){\n x-=i_7;\n }\n cerr<<x<<\"/\"<<y<<\" \";\n //cerr<<setprecision(5)<<(dd)x/(dd)y<<\" \";\n return;\n }\n }\n cerr<<\"nun\"<<endl;\n}\n/*\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/multiprecision/cpp_dec_float.hpp>\nnamespace multi = boost::multiprecision;\ntypedef multi::cpp_int LL;\ntypedef multi::number<multi::cpp_dec_float<1024>> DD;// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)\n//*/\ntypedef long double dd;\ntypedef pair<ll,ll> l_l;\ntypedef pair<dd,dd> d_d;\nll inf=(ll)1E18;\n#define rep(i,l,r) for(ll i=l;i<=r;i++)\n#define rrep(i,r,l) for(ll i=r;i>=l;i--)\n#define pb push_back\nll max(ll a,ll b){if(a<b)return b;else return a;}\nll min(ll a,ll b){if(a>b)return b;else return a;}\ndd EPS=1E-12;\n#define fi first\n#define se second\n\n#define SORT(v) sort(v.begin(),v.end())\n#define ERASE(v) sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end())\n#define POSL(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())\n#define POSU(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())\ntemplate<class T,class S>\ninline bool chmax(T &a, S b) {\n if(a < b) {\n a = (T)b;\n return true;\n }\n return false;\n}\ntemplate<class T,class S>\ninline bool chmin(T &a, S b) {\n if(a > b) {\n a = (T)b;\n return true;\n }\n return false;\n}\n#define all(c) c.begin(),c.end()\n\n//using mint = modint998244353;\nusing mint = modint1000000007;\n//using mint=modint;\n//using mint=static_modint<100>;\n//using mint=dd;\n//using mint=ll;\n\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<vvl>vvvl;\ntypedef vector<mint>vi;\ntypedef vector<vi>vvi;\ntypedef vector<vvi>vvvi;\ntypedef vector<vvvi>vvvvi;\ntypedef vector<l_l>vl_l;\ntypedef vector<vl_l>vvl_l;\ntypedef vector<string>vs;\ntypedef vector<vs>vvs;\ntypedef vector<dd> vd;\ntypedef vector<vd> vvd;\ntypedef vector<vvd> vvvd;\ntypedef vector<d_d>vd_d;\ndd PI=acos((dd)-1);\n\n#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout<<setprecision(20);\ntemplate <class T> using pq = priority_queue<T>;\ntemplate <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;\n\n// g++ -std=gnu++17 -Wall -Wextra -O2 -DONLINE_JUDGE main.cpp && ./a.out\n#define endl \"\\n\" //インタラクティブで消す!!!!!!!!!!!!!!!!!!!!!\n#define popcount __builtin_popcountll\n#define SHUFFLE(v) shuffle(all(v),default_random_engine(chrono::system_clock::now().time_since_epoch().count()))\n//////////////////////////////////////////////////////////////////////////////\ntemplate<class S>\nvoid DEBUG_PRINT(S x){\n cerr<<x<<endl;\n}\ntemplate<class S,class T>\nvoid DEBUG_PRINT(pair<S,T>x){\n cerr<<\"(\"<<x.fi<<\",\"<<x.se<<\")\"<<endl;\n}\ntemplate<class S>\nvoid DEBUG_PRINT(vector<S> x){\n for(auto y:x)cerr<<setw(2)<<y<<\" \";\n cerr<<endl;\n}\ntemplate<class S,class T>\nvoid DEBUG_PRINT(vector<pair<S,T>>x){\n for(auto y:x)cerr<<\"(\"<<y.fi<<\",\"<<y.se<<\") \";\n cerr<<endl;\n}\ntemplate<class S>\nvoid DEBUG_PRINT(vector<vector<S>> x){\n cerr<<endl;\n for(auto y:x){\n for(auto z:y){\n cerr<<setw(2)<<z<<\" \";\n }cerr<<endl;\n }cerr<<endl;\n}\n\n#define TO_STRING_FOR_DEBUG(VariableName) # VariableName\n#ifdef LOCAL\n#define DEB(V) cerr<< TO_STRING_FOR_DEBUG(V) << \": \"; DEBUG_PRINT(V);\n#else\n#define DEB(V) void(0)\n#endif\n/////////////////////////////////////////////////////////////////////////////////////////\n\nvoid solve(){\n ll n,k,s;cin>>n>>k>>s;\n if(n==0)exit(0);\n vvl a(n,vl(3));\n rep(i,0,n-1)rep(j,0,2)cin>>a[i][j];\n \n if(k==1){\n cout<<6*s*s<<endl;return ;\n }\n \n vvl_l v(n);\n rep(i,0,n-1){\n rep(j,i+1,n-1){\n auto calc=[](vl a,vl b,ll s){\n vl v;\n rep(i,0,2){\n ll dis=abs(a[i]-b[i]);\n if(dis>=s){\n return 0ll;\n }else{\n v.pb(s-dis);\n }\n }\n ll ans=0;\n rep(i,0,2){\n rep(j,i+1,2){\n ans += v[i]*v[j];\n }\n }\n return ans*2;\n };\n ll E=calc(a[i],a[j],s);\n if(E==0)continue;\n v[i].pb({j,E});\n v[j].pb({i,E});\n }\n }\n rep(i,0,n-1){\n cerr<<i<<\":\";\n DEB(v[i]);\n }\n dsu d(n);\n rep(i,0,n-1){\n for(auto x:v[i]){\n d.merge(i,x.fi);\n }\n }\n map<l_l,ll>mp;\n rep(i,0,n-1){\n for(auto x:v[i]){\n mp[{i,x.fi}] = x.se;\n }\n }\n auto G=d.groups();\n vl used(n);\n ll MAX =0;\n for(auto g:G){\n ll sz=g.size();\n ll edge_cnt=0;\n for(auto x:g){\n for(auto y:v[x]){\n if(x>y.fi)continue;\n edge_cnt ++;\n }\n }\n assert(edge_cnt == sz || edge_cnt == sz-1);\n \n if(sz<k)continue;\n \n ll pos=g[0];\n if(edge_cnt == sz-1){\n for(auto x:g){\n if((int)v[x].size()==1){\n pos=x;\n break;\n }\n }\n assert((int)v[pos].size()==1);\n }\n \n vl vec={pos};\n used[pos]=true;\n while(1){\n bool bre=true;\n for(auto x:v[pos]){\n if(used[x.fi])continue;\n used[x.fi]=true;\n pos=x.fi;\n vec.pb(x.fi);\n bre=false;\n break;\n }\n if(bre)break;\n }\n assert((int)vec.size() == sz);\n \n auto callast=[&](vl v){\n rep(i,0,(int)v.size()-1){\n ll sum=0;\n rep(j,i,i+k-2){//k-1個\n if(j>=(int)v.size()){\n return;\n }\n sum += v[j];\n }\n chmax(MAX,sum);\n }\n };\n if(edge_cnt == sz-1){\n vl v;\n rep(i,0,sz-2){\n v.pb(mp[{vec[i],vec[i+1]}]);\n }\n callast(v);\n }else{\n if(sz==k){\n ll sum=0;\n rep(i,0,sz-1){//k個\n sum += mp[{vec[i%sz],vec[(i+1)%sz]}];\n }\n chmax(MAX,sum);\n }else{\n vl v;\n rep(i,0,2*sz){\n v.pb(mp[{vec[i%sz],vec[(i+1)%sz]}]);\n }\n callast(v);\n }\n }\n }\n if(MAX==0){\n cout<<-1<<endl;\n }else{\n cout<<k*6*s*s - MAX<<endl;\n }\n}\n\nsigned main(){fastio\n \n while(1){\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3564, "score_of_the_acc": -0.0852, "final_rank": 8 } ]
aoj_1611_cpp
Daruma Otoshi You are playing a variant of a game called "Daruma Otoshi (Dharma Block Striking)". At the start of a game, several wooden blocks of the same size but with varying weights are stacked on top of each other, forming a tower. Another block symbolizing Dharma is placed atop. You have a wooden hammer with its head thicker than the height of a block, but not twice that. You can choose any two adjacent blocks, except Dharma on the top, differing at most 1 in their weight, and push both of them out of the stack with a single blow of your hammer. The blocks above the removed ones then fall straight down, without collapsing the tower. You cannot hit a block pair with weight difference of 2 or more, for that makes too hard to push out blocks while keeping the balance of the tower. There is no chance in hitting three blocks out at a time, for that would require superhuman accuracy. The goal of the game is to remove as many blocks as you can. Your task is to decide the number of blocks that can be removed by repeating the blows in an optimal order. Figure D1. Striking out two blocks at a time In the above figure, with a stack of four blocks weighing 1, 2, 3, and 1, in this order from the bottom, you can hit middle two blocks, weighing 2 and 3, out from the stack. The blocks above will then fall down, and two blocks weighing 1 and the Dharma block will remain. You can then push out the remaining pair of weight-1 blocks after that. Input The input consists of multiple datasets. The number of datasets is at most 50. Each dataset is in the following format. n w 1 w 2 … w n n is the number of blocks, except Dharma on the top. n is a positive integer not exceeding 300. w i gives the weight of the i -th block counted from the bottom. w i is an integer between 1 and 1000, inclusive. The end of the input is indicated by a line containing a zero. Output For each dataset, output in a line the maximum number of blocks you can remove. Sample Input 4 1 2 3 4 4 1 2 3 1 5 5 1 2 3 6 14 8 7 1 4 3 5 4 1 6 8 10 4 6 5 5 1 3 5 1 3 0 Output for the Sample Input 4 4 2 12 0
[ { "submission_id": "aoj_1611_11035458", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n\nint main(){\n\n while(1){\n int n;\n cin >> n;\n if (n == 0) break;\n vector<int> w(n);\n for (int i=0; i<n; i++) cin >> w[i];\n\n vector<vector<int>> dp(n+1, vector<int>(n+1, 0));\n for(int width=2; width<=n; width++){\n for (int l=0; l+width<=n; l++){\n int r = l + width;\n // if (r >= n) continue;\n // dp[l+1][r-1] == r - l - 2 : 間の全てのブロックを落とせる。\n // cout << \"r=\" << r << \", l=\" << l << endl;\n if (dp[l+1][r-1] == r - l - 2 && abs(w[r-1]-w[l]) <= 1){\n dp[l][r] = width;\n // cout << \"all\" << endl;\n }\n else{\n for (int mid=l; mid<=r; mid++){\n // cout << \"mid=\" << mid << \", dp[l][mid]=\" << dp[l][mid] << \", dp[mid][r]=\" << dp[mid][r] << endl;\n dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r]);\n }\n }\n // cout << \"dp[\" << l << \"][\" << r << \"] = \" << dp[l][r] << endl;\n }\n }\n cout << dp[0][n] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3768, "score_of_the_acc": -0.449, "final_rank": 4 }, { "submission_id": "aoj_1611_11035150", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(a,b,c) for(int a=b;a<c;a++)\n#define rep1(a,b,c) for(int a=b;a>=c;a--)\n#define vc vector\n#define pbk push_back\n#define ebk emplace_back\n#define fst first\n#define snd second\n#define sz(a) (int)a.size()\n#define out(a,b,c,d,e) cout << a << \" \" << b << \" \" << c << \" \" << d << \" \" << e << endl\nusing pii=pair<int,int>;\n\nvoid chmax(int &a,int b){a=max(a,b);return;}\nvoid chmin(int &a,int b){a=min(a,b);return;}\n\nsigned main() {\n\tint n;\n\tvc<int> ans;\n\twhile(true){\n\t\tcin >> n;\n\t\tif(n==0) break;\n\t\tvc<int> a(n);\n\t\trep(i,0,n) cin >> a[i];\n\n\n\t\tvc dp(n+1,vc<int>(n+2,-1));\n\n\n\t\tauto dfs=[&](auto dfs,int s,int t)->int{\n\t\t\tif(dp[s][t]!=-1) return dp[s][t];\n\t\t\tif(t-s<=1) return 0;\n\t\t\tif(t-s==2){\n\t\t\t\tif(abs(a[s]-a[s+1])<=1) return dp[s][t]=2;\n\t\t\t\telse return dp[s][t]=0;\n\t\t\t}\n\t\t\tint tmp=0;\n\t\t\tif(abs(a[s]-a[t-1])<=1 && dfs(dfs,s+1,t-1)==t-s-2){\n\t\t\t\tchmax(tmp,t-s);\n\t\t\t}\n\t\t\trep(i,s+1,t){\n\t\t\t\tint r=dfs(dfs,s,i)+dfs(dfs,i,t);\n\t\t\t\tchmax(tmp,r);\n\t\t\t}\n\t\t\treturn dp[s][t]=tmp;\n\t\t};\n\n\t\tcout << (dfs(dfs,0,n)) << endl;\n\n\t}\n\t//for(int x:ans) cout << x << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 3968, "score_of_the_acc": -1.454, "final_rank": 18 }, { "submission_id": "aoj_1611_11035144", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\n#define rep(a,b,c) for(int a=b;a<c;a++)\n#define rep1(a,b,c) for(int a=b;a>=c;a--)\n#define vc vector\n#define pbk push_back\n#define ebk emplace_back\n#define fst first\n#define snd second\n#define sz(a) (int)a.size()\n#define out(a,b,c,d,e) cout << a << \" \" << b << \" \" << c << \" \" << d << \" \" << e << endl\nusing pii=pair<int,int>;\n\nvoid chmax(int &a,int b){a=max(a,b);return;}\nvoid chmin(int &a,int b){a=min(a,b);return;}\n\nsigned main() {\n\tint n;\n\tvc<int> ans;\n\twhile(true){\n\t\tcin >> n;\n\t\tif(n==0) break;\n\t\tvc<int> a(n);\n\t\trep(i,0,n) cin >> a[i];\n\n\n\t\tvc dp(n+1,vc<int>(n+2,-1));\n\n\n\t\tauto dfs=[&](auto dfs,int s,int t)->int{\n\t\t\tif(dp[s][t]!=-1) return dp[s][t];\n\t\t\tif(t-s<=1) return 0;\n\t\t\tif(t-s==2){\n\t\t\t\tif(abs(a[s]-a[s+1])<=1) return 2;\n\t\t\t\telse return 0;\n\t\t\t}\n\t\t\tint tmp=0;\n\t\t\tif(abs(a[s]-a[t-1])<=1 && dfs(dfs,s+1,t-1)==t-s-2){\n\t\t\t\tchmax(tmp,t-s);\n\t\t\t}\n\t\t\trep(i,s+1,t){\n\t\t\t\tint r=dfs(dfs,s,i)+dfs(dfs,i,t);\n\t\t\t\tchmax(tmp,r);\n\t\t\t}\n\t\t\tdp[s][t]=tmp;\n\t\t\treturn tmp;\n\t\t};\n\n\tcout << (dfs(dfs,0,n)) << endl;\n\n\t}\n\t//for(int x:ans) cout << x << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 870, "memory_kb": 3968, "score_of_the_acc": -1.4843, "final_rank": 20 }, { "submission_id": "aoj_1611_11035119", "code_snippet": "#include <bits/stdc++.h>\n#define ALL(obj) begin(obj), end(obj)\nusing namespace std;\n\ntemplate <class T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\n\nint N;\nvector<int> w;\nvector<vector<int>> dp;\n\nint rec(int l = 0, int r = N) { // メモ化再帰\n if ((r - l) <= 1) return 0;\n if ((r - l) == 2) {\n if (abs(w[l] - w[l + 1]) <= 1) { // 隣り合う2つの差が1以下\n return 2;\n } else {\n return 0;\n }\n }\n\n int &ret = dp[l][r]; // 更新の対象についての参照を宣言\n if (ret != -1) return ret; // 既に計算済みならその値を使う\n\n // 1. 全部取り除けるとき\n if (abs(w[l] - w[r-1]) <= 1 && rec(l + 1, r - 1) == r - l - 2) chmax(ret, r - l);\n // 2. そうでない時\n for (int i = l + 1; i <= r - 1; i++) {\n chmax(ret, rec(l, i) + rec(i, r));\n }\n\n return ret;\n}\n\nint main() {\n vector<int> ans;\n while (true) {\n cin >> N;\n if (N == 0) break;\n w.resize(N);\n for (int i = 0; i < N; i++) {\n cin >> w.at(i);\n }\n\n dp.assign(N + 2, vector<int>(N + 2, -1)); // 初期化\n\n ans.push_back(rec());\n }\n\n for (auto i : ans) {\n cout << i << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 3816, "score_of_the_acc": -1.1015, "final_rank": 14 }, { "submission_id": "aoj_1611_10937835", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pii = pair < int, int > ;\nusing pll = pair < ll, ll > ;\n#define all(x) x.begin(), x.end()\nconst double pi = 3.141592653589793238;\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\n//n以下の約数列挙。O(√n)\nvector<ll> enumdiv(ll n){\n vector<ll> res;\n for(ll i = 1; i*i <= n; i++){\n if(n%i==0){\n res.push_back(i);\n if(i*i!=n) res.push_back(n/i); \n }\n }\n sort(all(res));\n return res;\n}\n//nを素因数分解する。{素数、指数}\nvector<pair<ll,ll>> prime_factorize(ll n){\n vector<pair<ll,ll>> res;\n for(ll a = 2; a*a<=n; a++){\n if(n%a==0){\n ll ex = 0;//指数部分\n while(n%a==0){\n n/=a;\n ex++;\n }\n res.push_back({a,ex});\n }\n }\n if(n!=1) res.push_back({n,1});\n return res;\n}\nstruct UnionFind{\n vector<int> par;//各要素の親 root=-1\n UnionFind(int sz){\n par.assign(sz,-1);\n }\n bool unite(int x,int y){\n x = find(x);\n y = find(y);\n if(x==y) return false;\n if(par[x]>par[y]) swap(x,y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n int find(int k){//kが属する根ノードを返す\n if(par[k]<0) return k;\n return par[k] = find(par[k]);\n }\n int size(int k){//kが属する木のサイズ\n return -par[find(k)];\n }\n vector<vector<int>> groups(){//全ての要素を連結成分ごとにグループ分けして返す。\n vector<vector<int>> result;\n map<int,vector<int>> mp;\n for(int i = 0; i < (int)par.size(); i++){\n mp[find(i)].push_back(i);\n }\n for(auto [key,val] : mp){\n result.push_back(val);\n }\n return result;\n }\n};\n// a^n mod を計算する\nlong long modpow(long long a, long long n, long long mod) {\n\tlong long res = 1;\n\twhile (n > 0) {\n\t\tif (n & 1) res = res * a % mod;\n\t\ta = a * a % mod;\n\t\tn >>= 1;\n\t}\n\treturn res;\n}\nint main() {\n int n;\n while(cin >> n){\n if(n==0) break;\n vector<int> w(n);\n for(int i = 0; i < n; i++){\n cin >> w[i];\n }\n vector<vector<int>> dp(n+1,vector<int>(n+1,-1));\n auto rec = [&](auto self,int l,int r) -> int{\n if(r-l<=1) return 0;\n if(r-l==2){\n if(abs(w[l+1]-w[l])<=1){\n return 2;\n }else return 0;\n }\n if(dp[l][r]!=-1) return dp[l][r];\n if(abs(w[l]-w[r-1])<=1&&self(self,l+1,r-1)==r-l-2){//全部取り除ける時\n dp[l][r] = max(dp[l][r],r-l);\n }\n for(int i = l+1; i <= r-1; i++){\n dp[l][r] = max(dp[l][r],self(self,l,i)+self(self,i,r));\n }\n return dp[l][r];\n };\n cout << rec(rec,0,n) << endl;\n }\n}", "accuracy": 1, "time_ms": 1100, "memory_kb": 3748, "score_of_the_acc": -1.4111, "final_rank": 17 }, { "submission_id": "aoj_1611_10891129", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/math>\nusing namespace atcoder;\n#endif\n\nusing ll = long long;\nusing ld = long double;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vll = vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing vvvll = vector<vector<vector<ll>>>;\nusing vd = vector<double>;\nusing vvd = vector<vector<double>>;\nusing vstr = vector<string>;\nusing vchar = vector<char>;\nusing vvchar = vector<vector<char>>;\nusing vb = vector<bool>;\nusing vvb = vector<vector<bool>>;\nusing pii = pair<int,int>;\nusing pll = pair<long long, long long>;\nusing Graph = vector<vector<int>>;\nconst int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};\nconst int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};\n\n#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)\n#define REP(i, a, b) for (int i = (int)a; i <= (int)b; i++)\n#define all(v) v.begin(), v.end()\n#define rall(v) v.rbegin(), v.rend()\n#define YESNO(flag) cout << (flag ? \"Yes\" : \"No\") << \"\\n\"\n#define spa \" \"\n\nconst int inf = 1070000000;\nconst long long INF = 4500000000000000000;\nconst long long MOD = 998244353;\n//const long long MOD = 1000000007;\nconst double pi = 3.141592653589793238;\nconst double eps = (1e-10);\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\n\ntemplate<class T> inline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class T> inline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\n//xのn乗\nll llpow(ll x, ll n) {\n ll ans = 1;\n while(n > 0) {\n if(n & 1) ans *= x;\n x *= x;\n n >>= 1; \n }\n return ans;\n}\nll modpow(ll x, ll n, ll mod) {\n ll ans = 1;\n while(n > 0) {\n if(n & 1) ans = ans * x % mod;\n x = x * x % mod;\n n >>= 1;\n }\n return ans;\n}\n//mod pでのaの逆元\nll inverse(ll a, ll p) {\n return modpow(a, p-2, p);\n}\n//nの階乗\nll fact(ll n) {\n if(n == 0) return 1;\n else return n * fact(n-1);\n}\nll modfact(ll n, ll mod) {\n if(n == 0) return 1;\n else return (n * modfact(n-1, mod)) % mod;\n}\n//nCr=n!/r!(n-r)! mod p\nll comb(ll n, ll r, ll mod) {\n ll a = modfact(n, mod), b = modfact(r, mod), c = modfact(n-r, mod);\n b = inverse(b, mod); c = inverse(c, mod);\n return (((a*b)%mod)*c)%mod;\n}\n\n//1+2+...+n = n(n+1)/2\nll triangle(ll n) {\n ll x = n*(n+1);\n return x/2;\n}\n//素数判定\nbool is_prime(long long x) {\n if(x == 1) return false;\n \n for(long long i = 2; i*i <= x; i++) {\n if(x % i == 0) return false;\n }\n return true;\n}\n\n//座標圧縮\nvector<int> compress(vector<int> A) {\n vector<int> B = A;\n\n sort(B.begin(), B.end());\n B.erase(unique(B.begin(), B.end()), B.end());\n\n vector<int> res(A.size());\n for(int i = 0; i < (int)A.size(); i++) {\n res[i] = lower_bound(B.begin(), B.end(), A[i]) - B.begin();\n }\n return res;\n}\n\n//正方形グリッドの右回転\nvoid rotate_right(vector<vector<char>> &S) {\n int N = S.size();\n auto T = S;\n rep(i,0,N) rep(j,0,N) {\n T[i][j] = S[N-1-j][i];\n }\n S = T;\n}\n//左回転\nvoid rotate_left(vector<vector<char>> &S) {\n int N = S.size();\n auto T = S;\n rep(i,0,N) rep(j,0,N) {\n T[i][j] = S[j][N-1-i];\n }\n S = T;\n}\n\n//2点(a,b)と(x,y)の距離\ndouble dist2(ll a, ll b, ll x, ll y) {\n return sqrt((a-x)*(a-x) + (b-y)*(b-y));\n}\nint man(int a, int b, int x, int y) {\n return abs(a-x) + abs(b-y);\n}\n\n//桁数\nint digit(ll N) {\n if(N <= 9) return 1;\n\n return 1 + digit(N / 10);\n}\n//A進数文字列NをB進数に変換. A,Bは10以下\nstring base_change(string N, ll A, ll B) {\n ll X = stoll(N), Y = 0;\n\n ll i = 1;\n while(X != 0) {\n Y += (X % 10) * i;\n i *= A;\n X /= 10;\n }\n\n X = Y, Y = 0;\n i = 1;\n while(X != 0) {\n Y += (X % B) * i;\n i *= 10;\n X /= B;\n }\n\n return to_string(Y);\n}\n//回文判定\nbool palin(string S) {\n rep(i,0,S.size()) {\n if(S[i] != S[S.size()-1-i]) return false;\n }\n return true;\n}\n\n//hh:mm:ss to second\nint convert_to_time(string S) {\n ll h = 10*(S[0]-'0') + (S[1]-'0');\n ll m = 10*(S[3]-'0') + (S[4]-'0');\n ll s = 10*(S[6]-'0') + (S[7]-'0');\n \n return h*3600+m*60+s;\n}\n\n//DFS\nvoid dfs(const Graph &G, int v, vector<bool> &seen) { \n seen[v] = true;\n \n for (auto next_v : G[v]) { \n if (!seen[next_v]) {\n dfs(G, next_v, seen);\n }\n }\n}\nvoid griddfs(const vector<vector<char>> &G, int x, int y, vector<vector<bool>> &seen) {\n int H = G.size(), W = G[0].size();\n seen[x][y] = true;\n \n for (int i = 0; i < 8; i++) { \n int nx = x + dx[i], ny = y + dy[i];\n if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;\n if (!seen[nx][ny] and G[nx][ny] != '#') griddfs(G, nx, ny, seen);\n }\n}\n//BFS\nvoid bfs(const Graph &G, int v, vector<int> &dist) {\n rep(i,0,dist.size()) dist[i] = inf;\n dist[v] = 0;\n queue<int> q;\n q.push(v);\n while(!q.empty()) {\n int u = q.front();\n q.pop();\n for(auto next_u : G[u]) {\n if(dist[next_u] == inf) {\n dist[next_u] = dist[u] + 1;\n q.push(next_u);\n }\n }\n }\n}\nvoid gridbfs(const vector<vector<char>> &G, int x, int y, vector<vector<int>> &dist) {\n int H = G.size(), W = G[0].size();\n rep(i,0,H) rep(j,0,W) dist[i][j] = inf;\n dist[x][y] = 0;\n queue<pair<int, int>> q;\n q.push(make_pair(x, y));\n while(!q.empty()) {\n int x = q.front().first, y = q.front().second;\n q.pop();\n for(int i = 0; i < 4; i++) {\n int nx = x + dx[i], ny = y + dy[i];\n if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;\n if(dist[nx][ny] == inf and G[nx][ny] != '#') {\n dist[nx][ny] = dist[x][y] + 1;\n q.push(make_pair(nx, ny));\n }\n }\n }\n}\n//ワーシャル・フロイド\nvector<vector<ll>> floyd(vector<vector<ll>> G) {\n int N = G.size();\n auto dp = G;\n for (int k = 0; k < N; k++){\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if(dp[i][j] > dp[i][k] + dp[k][j] and dp[i][k] != INF and dp[k][j] != INF) {\n dp[i][j] = dp[i][k] + dp[k][j];\n }\n }\n }\n }\n return dp;\n}\n\n//Union-Find\nstruct UnionFind {\n vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2\n \n UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化\n for(int i = 0; i < N; i++) par[i] = i;\n }\n \n int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}\n if (par[x] == x) return x;\n return par[x] = root(par[x]);\n }\n \n void unite(int x, int y) { // xとyの木を併合\n int rx = root(x); //xの根をrx\n int ry = root(y); //yの根をry\n if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま\n par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける\n }\n \n bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す\n int rx = root(x);\n int ry = root(y);\n return rx == ry;\n }\n};\n\nvoid solve() {\n}\n\nint main() {\n while(1) {\n int N; cin >> N;\n if(N == 0) break;\n vi A(N); rep(i,0,N) cin >> A[i];\n vvi dp(N+1, vi(N+1, 0));\n REP(k,2,N) {\n REP(l,0,N-k) {\n int r = l+k;\n rep(i,l+1,r) chmax(dp[l][r], dp[l][i]+dp[i][r]);\n if(abs(A[l]-A[r-1]) <= 1 and dp[l+1][r-1] == r-l-2) dp[l][r] = r-l;\n }\n }\n cout << dp[0][N] << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3768, "score_of_the_acc": -0.4995, "final_rank": 7 }, { "submission_id": "aoj_1611_10875922", "code_snippet": "// #pragma GCC optimize(\"Ofast\")\n// #pragma GCC optimize (\"unroll-loops\")\n// #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n\n#include<bits/stdc++.h>\nusing namespace std;\n\n#include<complex>\n//#include <ext/pb_ds/tree_policy.hpp>\n//#include <ext/pb_ds/assoc_container.hpp>\n\n#define x() real()\n#define y() imag()\n#define endl '\\n'\n#define ll long long\ntypedef long double T;\ntypedef complex<T> point;\n#define pii pair<int,int>\n#define all(x) x.begin(),x.end()\n#define fastIO cin.tie(0)->sync_with_stdio(0);\n\n//using namespace __gnu_pbds;\n//template <typename T> using o_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;\n\n//If you can't solve a problem then there's an easier\n//sub-problem that you can not solve.Find it!!\n\nconst int N = 310;\nint a[N] , can[N][N] , dp[N][N];\n\nint main()\n{\n fastIO;\n\n //int t = 1; cin >> t;\n\n while (1)\n {\n int n; cin >> n;\n if (n == 0) break;\n\n memset(can , 0 , sizeof can);\n memset(dp , 0 , sizeof dp);\n\n for (int i = 1 ; i <= n ; i++) cin >> a[i];\n\n for (int len = 2 ; len <= n ; len += 2)\n {\n for (int i = 1 ; i + len - 1 <= n ; i++)\n {\n int lim = i + len - 1;\n\n for (int j = i + 1 ; j <= lim ; j++)\n {\n if (abs(a[i] - a[j]) > 1) continue;\n\n bool left = (j == i + 1) || can[i + 1][j - 1];\n bool right = (j + 1 > lim) || can[j + 1][lim];\n\n if (left && right)\n can[i][lim] |= 1;\n }\n }\n }\n\n int ans = 0;\n\n for (int len = 2 ; len <= n ; len++)\n {\n for (int i = 1 ; i + len - 1 <= n ; i++)\n {\n int j = i + len - 1;\n\n if (can[i][j])\n {\n dp[i][j] = j - i + 1;\n ans = max(ans , dp[i][j]);\n continue;\n }\n\n for (int k = i ; k < j ; k++)\n dp[i][j] = max(dp[i][j] , dp[i][k] + dp[k + 1][j]);\n\n ans = max(ans , dp[i][j]);\n }\n }\n\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 4120, "score_of_the_acc": -1.0389, "final_rank": 13 }, { "submission_id": "aoj_1611_10875921", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n int n;\n if (!(cin >> n)) break;\n if (n == 0) break;\n\n vector<int> w(n);\n for (int i = 0; i < n; i++) cin >> w[i];\n\n vector<vector<bool>> can(n, vector<bool>(n, false));\n vector<vector<int>> dp(n, vector<int>(n, 0));\n\n for (int len = 2; len <= n; len++) {\n for (int l = 0; l + len - 1 < n; l++) {\n int r = l + len - 1;\n\n if (len % 2 == 0) {\n for (int m = l + 1; m <= r; m++) {\n if (abs(w[l] - w[m]) <= 1) {\n bool left_ok = (m == l + 1) || can[l + 1][m - 1];\n bool right_ok = (m == r) || can[m + 1][r];\n if (left_ok && right_ok) {\n can[l][r] = true;\n break;\n }\n }\n }\n }\n\n int best = can[l][r] ? len : 0;\n for (int k = l; k < r; k++) {\n best = max(best, dp[l][k] + dp[k + 1][r]);\n }\n dp[l][r] = best;\n }\n }\n\n cout << dp[0][n - 1] << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3532, "score_of_the_acc": -0.1919, "final_rank": 3 }, { "submission_id": "aoj_1611_10869963", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n while(cin >> n){\n if(n==0){\n return 0;\n }\n\n vector<int> w(n);\n for(int i=0; i<n; i++){\n cin >> w[i];\n }\n\n vector<vector<int>> dp(n, vector<int>(n,0));\n for(int leng=2; leng<=n; leng++){\n for(int i=0; i<n-leng+1; i++){\n int j=i+leng-1;\n \n if(dp[i+1][j-1]==j-i-1 && abs(w[i]-w[j])<=1){\n dp[i][j]=j-i+1;\n continue;\n }\n \n for(int k=i; k<j; k++){\n dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);\n }\n }\n }\n\n cout << dp[0][n-1] << '\\n';\n\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3792, "score_of_the_acc": -0.4722, "final_rank": 5 }, { "submission_id": "aoj_1611_10866801", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n vector<int> res_list;\n while (true) {\n int n; cin >> n;\n if (n == 0) break;\n vector<int> W(n);\n for (int i = 0; i < n; ++i) cin >> W[i];\n\n vector<vector<int>> dp(n, vector<int>(n,INT_MAX));\n for (int i = 0; i < n; ++i) dp[i][i] = 1;\n for (int l = 2; l <= n; ++l) {\n for (int i = 0; i < n - l + 1; ++i) {\n int j = i + l - 1;\n if (l == 2) {\n if (abs(W[i] - W[j]) <= 1) dp[i][j] = 0;\n else dp[i][j] = 2;\n continue;\n }\n if (abs(W[i] - W[j]) <= 1 && dp[i + 1][j - 1] == 0) {\n dp[i][j] = 0;\n continue;\n }\n for (int k = i; k < j; ++k) {\n dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);\n } \n }\n }\n\n res_list.push_back(n - dp[0][n-1]);\n }\n for (int res: res_list) cout << res << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3468, "score_of_the_acc": -0.0424, "final_rank": 2 }, { "submission_id": "aoj_1611_10864647", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nusing pii=pair<int,int>;\n#define rep(a,b,c) for(int a=b;a<c;a++)\n#define rep1(a,b,c) for(int a=b;a>=c;a--)\n#define vc vector\n#define pbk push_back\n#define ebk emplace_back\n#define fst first\n#define snd second\n#define sz(a) (int)a.size()\n#define out(a,b,c,d,e) cout << a << \" \" << b << \" \" << c << \" \" << d << \" \" << e << endl\n\nvoid chmax(int &a,int b){a=max(a,b);return;}\n\nsigned main() {\n\tint n;\n\twhile(true){\n\t\tcin >> n;\n\t\tif(n==0) return 0;\n\t\tvc<int> w(n);\n\t\trep(i,0,n) cin >> w[i];\n\n\t\tvc dp(n,vc<int>(n+1,-1));\n\n\t\tauto dfs=[&](auto dfs,int s,int t)->int{\n\t\t\tif(dp[s][t]!=-1) return dp[s][t];\n\t\t\tif(t-s==1) return dp[s][t]=0;\n\t\t\tif(t-s==2){\n\t\t\t\tif(abs(w[s]-w[t-1])<=1) return dp[s][t]=2;\n\t\t\t\telse return dp[s][t]=0;\n\t\t\t}\n\t\t\tint tmp=0;\n\t\t\tif(abs(w[s]-w[t-1])<=1 && dfs(dfs,s+1,t-1)==t-s-2){\n\t\t\t\treturn dp[s][t]=t-s;\n\t\t\t}\n\t\t\trep(i,s+1,t){\n\t\t\t\tchmax(tmp,dfs(dfs,s,i)+dfs(dfs,i,t));\n\t\t\t}\n\t\t\treturn dp[s][t]=tmp;\n\t\t};\n\n\t\tint ans=dfs(dfs,0,n);\n\t\tcout << ans << endl;\n\n\t}\n\n\n}", "accuracy": 1, "time_ms": 790, "memory_kb": 3840, "score_of_the_acc": -1.2258, "final_rank": 16 }, { "submission_id": "aoj_1611_10856891", "code_snippet": "#include<bits/stdc++.h>\n#define FOR(i,s,t) for(int i = s; i<t ; i++)\n#define SZ(x) (int)x.size()\n#define ALL(x) x.begin(), x.end()\n\nusing namespace std;\n\nusing VI = vector<int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVI = vector<VI>;\n\n\nint main() {\n\tint N;\n\twhile (cin >> N, N) {\n\t\tVI a(N);\n\t\tFOR(i, 0, N) {\n\t\t\tscanf(\"%d\", &a[i]);\n\t\t}\n\t\tVVI checkdp(N + 1, VI(N + 1, 0));\n\t\tVVI dp(N + 1, VI(N + 1, 0));\n\t\tint ans = 0;\n\n\t\tFOR(alen, 2, N + 1) {\n\t\t\tFOR(i, 0, N) {\n\t\t\t\tint j = alen + i - 1;\n\t\t\t\tif (j >= N) continue;\n\t\t\t\tif (dp[i + 1][j - 1] == alen - 2 && abs(a[i] - a[j]) <= 1) dp[i][j] = alen;\n\t\t\t\tFOR(k, i, j) {\n\t\t\t\t\tdp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout << dp[0][N - 1] << endl;\n\t\t//\n\n\n\n\t\t//FOR(i, 0, N - 1) {\n\t\t//\tif (abs(a[i] - a[i + 1]) <= 1) {\n\t\t//\t\tcheckdp[i][i + 1] = 1;\n\t\t//\t\tdp[i][i + 1] = 2;\n\t\t//\t\tans = 2;\n\t\t//\t}\n\t\t//}\n\n\t\t//for (int alen = 4; alen <= N; alen += 2) {// 長さalenを合成\n\t\t//\tfor (int s = 0; s < N; s++) {\n\t\t//\t\tfor (int spl = s + 1; spl <= N; spl++) {\n\t\t//\t\t\tint end = alen + s - 1;\n\t\t//\t\t\t// checkdp[s][spl], checkdp[spl + 1][end] merge;\n\t\t//\t\t\t// あとで境界処理をすること\n\t\t//\t\t\tif (s < end &&spl < end&& end < N) {// cond\n\t\t//\t\t\t\tif (checkdp[s][spl] && checkdp[spl + 1][end]) {\n\t\t//\t\t\t\t\tcheckdp[s][end] = 1;\n\t\t//\t\t\t\t\tdp[s][end] = alen;\n\t\t//\t\t\t\t\tans = max(ans, alen);\n\t\t//\t\t\t\t}\n\t\t//\t\t\t\tif (abs(a[s] - a[end]) <= 1 && checkdp[s + 1][spl] && checkdp[spl + 1][end - 1]) {\n\t\t//\t\t\t\t\tcheckdp[s][end] = 1;\n\t\t//\t\t\t\t\tdp[s][end] = alen;\n\t\t//\t\t\t\t\tans = max(ans, alen);\n\t\t//\t\t\t\t}\n\n\t\t//\t\t\t\tif (abs(a[s] - a[end]) <= 1 && checkdp[s + 1][end - 1]) {\n\n\t\t//\t\t\t\t\tcheckdp[s][end] = 1;\n\t\t//\t\t\t\t\tdp[s][end] = alen;\n\t\t//\t\t\t\t\tans = max(ans, alen);\n\t\t//\t\t\t\t}\n\t\t//\t\t\t}\n\t\t//\t\t}\n\t\t//\t}\n\t\t//}\n\t\t////cout << \"PREANS\" << ans << endl;\n\t\t////int ss, tt;\n\t\t////while (cin >> ss >> tt) {\n\t\t////\tcout << \"(s:\" << ss << \",t\" << tt << \"):\" << checkdp[ss][tt] << endl;\n\t\t////}\n\t\t//// くくりはmergeできたので、O XM O みたいなのを合成する\n\n\t\t//// max / + \n\n\t\t//for (int alen = 4; alen <= N; alen++) {// 長さalenを合成\n\t\t//\tfor (int s = 0; s < N; s++) {\n\t\t//\t\tfor (int spl = s + 1; spl <= N; spl++) {\n\t\t//\t\t\tint end = alen + s - 1;\n\t\t//\t\t\t// checkdp[s][spl], checkdp[spl + 1][end] merge;\n\t\t//\t\t\t// あとで境界処理をすること\n\t\t//\t\t\tif (s < end &&spl < end&& end < N) {// cond\n\n\n\t\t//\t\t\t\tdp[s][end] = max({ dp[s][end],dp[s][spl] + dp[spl + 1][end] });\n\t\t//\t\t\t\tans = max(ans, dp[s][end]);\n\t\t//\t\t\t\t/*if (checkdp[s][spl] && checkdp[spl + 1][end]) {\n\t\t//\t\t\t\t\tcheckdp[s][end] = 1;\n\t\t//\t\t\t\t\tdp[s][end] = alen;\n\t\t//\t\t\t\t\tans = max(ans, alen);\n\t\t//\t\t\t\t}\n\t\t//\t\t\t\tif (abs(a[s] - a[end]) <= 1 && checkdp[s + 1][spl] && checkdp[spl + 1][end - 1]) {\n\t\t//\t\t\t\t\tcheckdp[s][end] = 1;\n\t\t//\t\t\t\t\tdp[s][end] = alen;\n\t\t//\t\t\t\t\tans = max(ans, alen);\n\t\t//\t\t\t\t}\n\n\t\t//\t\t\t\tif (abs(a[s] - a[end]) <= 1 && checkdp[s + 1][end - 1]) {\n\n\t\t//\t\t\t\t\tcheckdp[s][end] = 1;\n\t\t//\t\t\t\t\tdp[s][end] = alen;\n\t\t//\t\t\t\t\tans = max(ans, alen);\n\t\t//\t\t\t\t}*/\n\t\t//\t\t\t}\n\t\t//\t\t}\n\t\t//\t}\n\t\t//}\n\n\t\t//cout << ans << endl;\n\n\t}\n\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 4172, "score_of_the_acc": -1.0101, "final_rank": 12 }, { "submission_id": "aoj_1611_10854239", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n vector<int> res_list;\n while (true) {\n int n; cin >> n;\n if (n == 0) break;\n vector<int> W(n);\n for (int i = 0; i < n; ++i) cin >> W[i];\n\n vector<vector<int>> dp(n, vector<int>(n, 0));\n for (int l = 2; l <= n; ++l) {\n for (int i = 0; i < n - l + 1; ++i) {\n int j = i + l - 1;\n if (abs(W[i] - W[j]) < 2 && dp[i + 1][j - 1] == l - 2) dp[i][j] = l; \n for (int k = i; k < j; ++k) {\n dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j]);\n }\n }\n }\n\n res_list.push_back(dp[0][n-1]);\n }\n\n for (int res: res_list) cout << res << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3452, "score_of_the_acc": -0.0202, "final_rank": 1 }, { "submission_id": "aoj_1611_10853208", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define FOR(i,a,b) for(int (i)=(a);(i)<(b);(i)++)\n#define REP(i,n) FOR(i,0,(n))\n\ntypedef long long ll;\n\nconst int M=10000000;\nint dp[400][400];\n\nvector<int>wass;\nbool ok(int a, int b){\n return abs(wass[a]-wass[b])<=1;\n}\nint solve(int l,int r){\n if(dp[l][r]!=-1)return dp[l][r];\n if(l==r)return dp[l][r]=0;\n else if(l==r+1)return dp[l][r]=0;\n else{\n int ans=0;\n for(int p=l+1;p<r;++p){\n ans=max(ans,solve(l,p)+solve(p,r));\n }\n if(ok(l,r-1)){\n if(solve(l+1,r-1)==r-l-2){\n ans=r-l;\n }\n }\n return dp[l][r]=ans;\n }\n}\nint main()\n{\n while(true){\n for(int i=0;i<400;++i){\n for(int j=0;j<400;++j){\n dp[i][j]=-1;\n }\n }\n int N;cin>>N;\n if(!N)break;\n wass.assign(N,0);\n\n for(int i=0;i<N;++i){\n cin>>wass[i];\n }\n int ans=solve(0,N);\n cout<<ans<<endl;\n }\n \n}", "accuracy": 1, "time_ms": 800, "memory_kb": 4000, "score_of_the_acc": -1.4581, "final_rank": 19 }, { "submission_id": "aoj_1611_10829050", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint dp[309][309];\n\nint main() {\n while(true){\n int N;\n cin >> N;\n if(N == 0){\n return 0;\n }\n vector<int> weight(N);\n for(int i = 0; i < N; i++){\n cin >> weight[i];\n }\n for(int i = 0; i < N+2; i++){\n for(int j = 0; j < N+2; j++){\n dp[i][j] = 0;\n }\n }\n for(int i = 0; i < N; i++){\n dp[i][i+1] = 0;\n }\n\n for(int W = 2; W <= N; W++){\n for(int L = 0; L + W <= N; L++){\n int R = L + W;\n\n if(dp[L+1][R-1] == W-2 && abs(weight[L] - weight[R-1]) <= 1){\n dp[L][R] = W;\n }\n\n for(int i = L; i <= R; i++){\n dp[L][R] = max(dp[L][R], dp[L][i] + dp[i][R]);\n }\n }\n }\n cout << dp[0][N] << endl;\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3808, "score_of_the_acc": -0.4944, "final_rank": 6 }, { "submission_id": "aoj_1611_10781360", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nusing Graph = vector<vector<int>>;\nconst int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};\nconst int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};\n\n#define rep(i, a, b) for(int i = a; i < b; i++)\n#define all(v) v.begin(), v.end()\n#define YESNO(flag) cout << (flag ? \"Yes\" : \"No\") << \"\\n\"\n#define inf 1070000000\n#define INF 4500000000000000000\n#define MOD 998244353\n//#define MOD 1000000007\n#define pi 3.14159265358979\n\ntemplate<class T> inline bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate<class T> inline bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\n//xのn乗\nll llpow(ll x, ll n) {\n ll ans = 1;\n while(n > 0) {\n if(n & 1) ans *= x;\n x *= x;\n n >>= 1; \n }\n return ans;\n}\nll modpow(ll x, ll n, ll mod) {\n ll ans = 1;\n while(n > 0) {\n if(n & 1) ans = ans * x % mod;\n x = x * x % mod;\n n >>= 1;\n }\n return ans;\n}\n//nの階乗\nll fact(ll n) {\n if(n == 0) return 1;\n else return n * fact(n-1);\n}\n//素数判定\nbool is_prime(long long x) {\n if(x == 1) return false;\n \n for(long long i = 2; i*i <= x; i++) {\n if(x % i == 0) return false;\n }\n return true;\n}\n\n//座標圧縮\nvector<int> compress(vector<int> A) {\n vector<int> B = A;\n\n sort(B.begin(), B.end());\n B.erase(unique(B.begin(), B.end()), B.end());\n\n vector<int> res(A.size());\n for(int i = 0; i < (int)A.size(); i++) {\n res[i] = lower_bound(B.begin(), B.end(), A[i]) - B.begin();\n }\n return res;\n}\n\n//正方形グリッドの右回転\nvoid rotate_right(vector<vector<char>> &S) {\n int N = S.size();\n auto T = S;\n rep(i,0,N) rep(j,0,N) {\n T[i][j] = S[N-1-j][i];\n }\n S = T;\n}\n//左回転\nvoid rotate_left(vector<vector<char>> &S) {\n int N = S.size();\n auto T = S;\n rep(i,0,N) rep(j,0,N) {\n T[i][j] = S[j][N-1-i];\n }\n S = T;\n}\n\n//2点(a,b)と(x,y)の距離\ndouble dist(ll a, ll b, ll x, ll y) {\n return sqrt((a-x)*(a-x) + (b-y)*(b-y));\n}\nint man(int a, int b, int x, int y) {\n return abs(a-x) + abs(b-y);\n}\n\n//桁数\nint digit(ll N) {\n if(N <= 9) return 1;\n\n return 1 + digit(N / 10);\n}\n//A進数文字列NをB進数に変換. A,Bは10以下\nstring base_change(string N, ll A, ll B) {\n ll X = stoll(N), Y = 0;\n\n ll i = 1;\n while(X != 0) {\n Y += (X % 10) * i;\n i *= A;\n X /= 10;\n }\n\n X = Y, Y = 0;\n i = 1;\n while(X != 0) {\n Y += (X % B) * i;\n i *= 10;\n X /= B;\n }\n\n return to_string(Y);\n}\n//回文判定\nbool palin(string S) {\n rep(i,0,S.size()) {\n if(S[i] != S[S.size()-1-i]) return false;\n }\n return true;\n}\n\n\n//DFS\nvoid dfs(const Graph &G, int v, vector<bool> &seen) { \n seen[v] = true;\n \n for (auto next_v : G[v]) { \n if (!seen[next_v]) {\n dfs(G, next_v, seen);\n }\n }\n}\nvoid griddfs(const vector<vector<char>> &G, int x, int y, vector<vector<bool>> &seen) {\n int H = G.size(), W = G[0].size();\n seen[x][y] = true;\n \n for (int i = 0; i < 8; i++) { \n int nx = x + dx[i], ny = y + dy[i];\n if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;\n if (!seen[nx][ny] and G[nx][ny] != '#') griddfs(G, nx, ny, seen);\n }\n}\n//BFS\nvoid bfs(const Graph &G, int v, vector<int> &dist) {\n rep(i,0,dist.size()) dist[i] = inf;\n dist[v] = 0;\n queue<int> q;\n q.push(v);\n while(!q.empty()) {\n int u = q.front();\n q.pop();\n for(auto next_u : G[u]) {\n if(dist[next_u] == inf) {\n dist[next_u] = dist[u] + 1;\n q.push(next_u);\n }\n }\n }\n}\nvoid gridbfs(const vector<vector<char>> &G, int x, int y, vector<vector<int>> &dist) {\n int H = G.size(), W = G[0].size();\n rep(i,0,H) rep(j,0,W) dist[i][j] = inf;\n dist[x][y] = 0;\n queue<pair<int, int>> q;\n q.push(make_pair(x, y));\n while(!q.empty()) {\n int x = q.front().first, y = q.front().second;\n q.pop();\n for(int i = 0; i < 4; i++) {\n int nx = x + dx[i], ny = y + dy[i];\n if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;\n if(dist[nx][ny] == inf and G[nx][ny] != '#') {\n dist[nx][ny] = dist[x][y] + 1;\n q.push(make_pair(nx, ny));\n }\n }\n }\n}\n//ワーシャル・フロイド\nvoid floyd(vector<vector<ll>> &G) {\n int N = G.size();\n for (int k = 0; k < N; k++){\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if(G[i][j] > G[i][k] + G[k][j]) {\n G[i][j] = G[i][k] + G[k][j];\n }\n }\n }\n }\n}\n\nvoid solve() {\n}\n\nint main() {\n while(1) {\n int N; cin >> N;\n if(N == 0) break;\n vector<int> A(N); rep(i,0,N) cin >> A[i];\n vector<vector<int>> dp(N+1, vector<int>(N+1, 0));\n\n rep(i,0,N) dp[i][i+1] = 0;\n rep(W, 2, N+1) {\n rep(l,0,N) {\n int r = l + W;\n if(r > N) continue;\n\n if(abs(A[l] - A[r-1]) <= 1 and dp[l+1][r-1] == r-l-2) dp[l][r] = r-l;\n\n rep(mid,l+1,r) {\n chmax(dp[l][r], dp[l][mid] + dp[mid][r]);\n }\n }\n }\n\n cout << dp[0][N] << \"\\n\";\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3872, "score_of_the_acc": -0.654, "final_rank": 10 }, { "submission_id": "aoj_1611_10769044", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n#include <cmath>\n#include <numeric>\n#include <iomanip>\n#include <map>\n#include <queue>\n#include <set>\n#include <bitset>\n#include <stack>\n#include <list>\n#include <unordered_set>\n#include <unordered_map>\n#define rep(i, a, b) for (int i = a; i < b; ++i)\nusing namespace std;\n\nconst int MOD = 998244353;\nconst int mod = 1000000007;\n\nconst long long INF = 1LL << 60;\nconstexpr double pi = 3.141592653589793238462643383279502884L;\n\nstruct Edge {\n int to;\n long long w;\n Edge(int to, long long w) : to(to), w(w) {}\n};\n\nusing Graph = vector<vector<Edge>>;\nusing P = pair<int, int>;\nusing Pll = pair<long long, int>;\nusing ll = long long;\n\ntemplate<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } else return false; }\ntemplate<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } else return false; }\n\nvector<vector<int>> dp;\n\nint rec(int l, int r, const vector<int> &w) {\n // 計算済みの時はその値を返す\n if (dp[l][r] != -1) return dp[l][r];\n\n // 一個のみの時は取り除けないので取り除かない,つまり0を返す\n if (abs(l - r) <= 1) return 0;\n\n int res = 0;\n // 区間[l, r)を計算する\n // パターン1: [l, r)までの区間が全部取り除けるとき\n // その中身がすべて1以下で構成されているとき取り除けることになる\n if (abs(w[l] - w[r-1]) <= 1 && rec(l+1, r-1, w) == r - l - 2) {\n // [l, r)を取り除く\n res = r - l;\n }\n\n // パターン2: 全部取り除かず,区間内でぼちぼち取り除く\n for (int i = l+1; i <= r-1; ++i) {\n // 左側部分での最適な取り除き方をしたときの取り除き個数\n // と右側部分での最適な取り除き方をしたときの取り除き個数\n // を合わせれば今回の[l, r)の最適な取り出し方が得られる\n res = max(res, rec(l, i, w) + rec(i, r, w));\n }\n\n // メモ化再帰\n return dp[l][r] = res;\n}\n\nint main() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n\n while (true) {\n int n; cin >> n;\n if (n == 0) break;\n\n vector<int> w(n);\n for (int i = 0; i < n; ++i) {\n cin >> w[i];\n }\n\n dp.assign(n+1, vector<int>(n+1, -1));\n cout << rec(0, n, w) << endl;\n }\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 3840, "score_of_the_acc": -1.1348, "final_rank": 15 }, { "submission_id": "aoj_1611_10750034", "code_snippet": "#include <iostream>\n#include <vector>\n#include <iomanip>\n#include <algorithm>\n#include <tuple>\n#include <set>\n#include <map>\n#include <bitset>\n#include <cmath>\n#include <deque>\n#include <queue>\n#include <stack>\n#include <cassert>\n#include <unordered_set>\n#include <unordered_map>\n#include <cmath>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define all(v) v.begin(), v.end()\n\nll llpow(ll a, ll t)\n{\n ll res = 1;\n rep(i, t) res *= a;\n return res;\n}\n\nll inf = std::numeric_limits<ll>::max();\n\nint main()\n{\n vector<ll> ans;\n while (true)\n {\n ll n;\n cin >> n;\n if (!n)\n {\n break;\n }\n vector<ll> vec(n);\n rep(i, n)\n {\n cin >> vec[i];\n }\n // lrで取り除ける最大\n vector<vector<ll>> dp(n, vector<ll>(n, 0));\n rep(i, n) // diff\n {\n rep(j, n) // left\n {\n if (!i)\n {\n continue;\n }\n if (i + j > n - 1)\n {\n continue;\n }\n if (i == 1)\n {\n if (abs(vec[j] - vec[j + i]) <= 1)\n {\n dp[j][j + i] = 2;\n }\n continue;\n }\n if (dp[j + 1][j + i - 1] == i - 1 && abs(vec[j] - vec[j + i]) <= 1)\n {\n dp[j][j + i] = i + 1;\n continue;\n }\n rep(k, i)\n {\n dp[j][j + i] = max(dp[j][j + i], dp[j][j + k] + dp[j + k + 1][j + i]);\n }\n }\n }\n // cout << dp[0][3] << endl;\n ans.push_back(dp[0][n - 1]);\n }\n for (auto num : ans)\n {\n cout << num << endl;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3896, "score_of_the_acc": -0.6571, "final_rank": 11 }, { "submission_id": "aoj_1611_10743637", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int, int>;\n#define chmin(x, y) x = min(x, y)\n#define chmax(x, y) x = max(x, y)\n\nint inf = 10010010;\n\nint main()\n{\n while (true)\n {\n int n;\n cin >> n;\n if (n == 0)\n break;\n vector<int> w;\n for (int i = 0; i < n; ++i)\n {\n int a;\n cin >> a;\n w.push_back(a);\n }\n vector<vector<int>> dp(n + 5, vector<int>(n + 5, 0));\n for (int i = 0; i < n - 1; ++i)\n {\n if (abs(w[i] - w[i + 1]) <= 1)\n {\n dp[i][i + 1] = 2;\n }\n }\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n int start = j;\n int end = i + j;\n if (end >= n)\n break;\n for (int k = 1; k < i; ++k)\n {\n if (dp[start][start + k] != 0 && dp[start + k + 1][end])\n {\n dp[start][end] = max(\n dp[start][end], dp[start][start + k] + dp[start + k + 1][end]);\n }\n }\n if (dp\n [start + 1][end - 1] != 0 &&\n abs(w[start] - w[end]) <= 1)\n {\n dp[start][end] = max(dp[start][end], dp[start + 1][end - 1] + 2);\n }\n }\n }\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n int start = j;\n int end = i + j;\n if (end >= n)\n break;\n for (int k = 0; k < i; ++k)\n {\n dp[start][end] = max(\n dp[start][end], dp[start][start + k] + dp[start + k + 1][end]);\n }\n }\n }\n cout << dp[0][n - 1] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3708, "score_of_the_acc": -0.5071, "final_rank": 8 }, { "submission_id": "aoj_1611_10743627", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int, int>;\n#define chmin(x, y) x = min(x, y)\n#define chmax(x, y) x = max(x, y)\n\nint inf = 10010010;\n\nint main()\n{\n while (true)\n {\n int n;\n cin >> n;\n if (n == 0)\n break;\n vector<int> w;\n for (int i = 0; i < n; ++i)\n {\n int a;\n cin >> a;\n w.push_back(a);\n }\n vector<vector<int>> dp(n + 5, vector<int>(n + 5, 0));\n for (int i = 0; i < n - 1; ++i)\n {\n if (abs(w[i] - w[i + 1]) <= 1)\n {\n dp[i][i + 1] = 2;\n }\n }\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n int start = j;\n int end = i + j;\n if (end >= n)\n break;\n for (int k = 0; k < i; ++k)\n {\n if (dp[start][start + k] != 0 && dp[start + k + 1][end])\n {\n dp[start][end] = max(\n dp[start][end], dp[start][start + k] + dp[start + k + 1][end]);\n }\n }\n if (dp\n [start + 1][end - 1] != 0 &&\n abs(w[start] - w[end]) <= 1)\n {\n dp[start][end] = max(dp[start][end], dp[start + 1][end - 1] + 2);\n }\n }\n }\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n int start = j;\n int end = i + j;\n if (end >= n)\n break;\n for (int k = 0; k < i; ++k)\n {\n dp[start][end] = max(\n dp[start][end], dp[start][start + k] + dp[start + k + 1][end]);\n }\n }\n }\n cout << dp[0][n - 1] << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3736, "score_of_the_acc": -0.546, "final_rank": 9 } ]
aoj_1605_cpp
Bridge Construction Planning There is a city consisting of many small islands, and the citizens live in these islands. Citizens feel inconvenience in requiring ferry rides between these islands. The city mayor decided to build bridges connecting all the islands. The city has two construction companies, A and B. The mayor requested these companies for proposals, and obtained proposals in the form: "Company A (or B) can build a bridge between islands u and v in w hundred million yen." The mayor wants to accept some of these proposals to make a plan with the lowest budget. However, if the mayor accepts too many proposals of one company, the other may go bankrupt, which is not desirable for the city with only two construction companies. However, on the other hand, to avoid criticism on wasteful construction, the mayor can only accept the minimum number (i.e., n − 1) of bridges for connecting all the islands. Thus, the mayor made a decision that exactly k proposals by the company A and exactly n − 1 − k proposals by the company B should be accepted. Your task is to write a program that computes the cost of the plan with the lowest budget that satisfies the constraints. Here, the cost of a plan means the sum of all the costs mentioned in the accepted proposals. Input The input consists of multiple datasets. The number of datasets is at most 30. Each dataset is in the following format. n m k u 1 v 1 w 1 l 1 ... u m v m w m l m The first line contains three integers n , m , and k , where n is the number of islands, m is the total number of proposals, and k is the number of proposals that are to be ordered to company A (2 ≤ n ≤ 200, 1 ≤ m ≤ 600, and 0 ≤ k ≤ n −1). Islands are identified by integers, 1 through n . The following m lines denote the proposals each of which is described with three integers u i , v i , w i and one character l i , where u i and v i denote two bridged islands, w i is the cost of the bridge (in hundred million yen), and l i is the name of the company that submits this proposal (1 ≤ u i ≤ n , 1 ≤ v i ≤ n , 1 ≤ w i ≤ 100, and l i = 'A' or 'B'). You can assume that each bridge connects distinct islands, i.e., u i ≠ v i , and each company gives at most one proposal for each pair of islands, i.e., { u i , v i } ≠ { u j , v j } if i ≠ j and l i = l j . The end of the input is indicated by a line with three zeros separated by single spaces. Output For each dataset, output a single line containing a single integer that denotes the cost (in hundred million yen) of the plan with the lowest budget. If there are no plans that satisfy the constraints, output −1. Sample Input 4 5 2 1 2 2 A 1 3 2 A 1 4 2 A 2 3 1 B 3 4 1 B 5 8 2 1 2 1 A 2 3 1 A 3 4 3 A 4 5 3 A 1 2 5 B 2 3 5 B 3 4 8 B 4 5 8 B 5 5 1 1 2 1 A 2 3 1 A 3 4 1 A 4 5 1 B 3 5 1 B 4 5 3 1 2 2 A 2 4 3 B 3 4 4 B 2 3 5 A 3 1 6 A 0 0 0 Output for the Sample Input 5 16 -1 -1
[ { "submission_id": "aoj_1605_10719736", "code_snippet": "/*\n\nhttps://hitonanode.github.io/cplib-cpp/combinatorial_opt/test/matroid_intersection_dijkstra.aoj1605.test.cpp\n\nedit by noya2\n\n*/\n\n#line 1 \"combinatorial_opt/test/matroid_intersection_dijkstra.aoj1605.test.cpp\"\n#define PROBLEM \"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1605\"\n#line 2 \"combinatorial_opt/matroid_intersection_dijkstra.hpp\"\n\n#include <cassert>\n#include <queue>\n#include <vector>\n\n// Find maximum weight size k + 1 intersection of two matroids using Dijkstra's algorithm\n// Return `true` iff larger intersection is found.\n// Complexity: O(Cn + nk log n) (C: circuit query)\ntemplate <class Matroid1, class Matroid2, class T = int>\nbool augment_matroid_intersection_dijkstra(\n Matroid1 &m1, // Matroid, size n, updated\n Matroid2 &m2, // Matroid, size n, updated\n std::vector<bool> &I, // Size k maximum weight intersection, size n, updated\n const std::vector<T> &weight, // Weights of elements, size n\n std::vector<T> &potential // Potential, size n + 2, updated\n) {\n const int n = I.size();\n\n assert((int)m1.size() == n);\n assert((int)m2.size() == n);\n assert((int)weight.size() == n);\n assert(potential.empty() or ((int)potential.size() == n) or ((int)potential.size() == n + 2));\n\n m1.set(I);\n m2.set(I);\n\n potential.resize(n + 2);\n\n auto l = [&](int e) -> T { return e < n ? (I.at(e) ? weight.at(e) : -weight.at(e)) : T(); };\n auto edge_len = [&](int s, int t) -> T { return l(t) - potential.at(t) + potential.at(s); };\n\n if (false) { // 自明な追加が可能かチェック(省略してもアルゴリズムは正当)\n int max_elem = -1;\n for (int e = 0; e < n; ++e) {\n if (!I.at(e) and (max_elem < 0 or weight.at(max_elem) < weight.at(e))) max_elem = e;\n }\n if (max_elem < 0) return false;\n for (int e = 0; e < n; ++e) {\n if (!I.at(e) and weight.at(e) == weight.at(max_elem) and m1.circuit(e).empty() and\n m2.circuit(e).empty()) {\n potential.at(e) -= l(e);\n I.at(e) = true;\n return true;\n }\n }\n }\n\n // Find minimum length (& minimum num. of vertices) gs-gt path\n const int gs = n, gt = n + 1;\n std::vector<std::vector<int>> to(gt + 1);\n\n bool has_gs_edge = false, has_gt_edge = false;\n\n for (int e = 0; e < n; ++e) {\n if (I.at(e)) continue;\n\n const auto c1 = m1.circuit(e), c2 = m2.circuit(e);\n\n if (c1.empty()) {\n to.at(e).push_back(gt);\n if (!has_gt_edge) {\n has_gt_edge = true;\n potential.at(gt) = potential.at(e);\n }\n if (T el = edge_len(e, gt); el < T()) potential.at(gt) += el;\n }\n for (int f : c1) {\n if (f != e) to.at(e).push_back(f);\n }\n\n if (c2.empty()) {\n to.at(gs).push_back(e);\n if (!has_gs_edge) {\n has_gs_edge = true;\n potential.at(gs) = potential.at(e) - l(e);\n }\n if (T el = edge_len(gs, e); el < T()) potential.at(gs) -= el;\n }\n for (int f : c2) {\n if (f != e) to.at(f).push_back(e);\n }\n }\n\n if (const T e0 = potential.at(gs); e0 != T()) {\n for (auto &p : potential) p -= e0;\n }\n\n if (!has_gs_edge or !has_gt_edge) return false;\n\n std::vector<bool> potential_fixed(gt + 1);\n\n T potential_add_unfixed_es = T();\n\n auto fix_potential = [&](int e) -> void {\n assert(!potential_fixed.at(e));\n potential_fixed.at(e) = true;\n potential.at(e) += potential_add_unfixed_es;\n };\n\n std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<>> pq;\n std::vector<T> dijkstra(gt + 1);\n std::vector<int> prv(gt + 1, -1);\n\n pq.emplace(T(), gs);\n\n // noya2 begin\n int many = 1001001;\n while (!pq.empty()){\n many--;\n assert(many > 0);\n auto [d, e] = pq.top(); pq.pop();\n if (potential_fixed[e]) continue;\n potential_fixed[e] = true;\n for (int ne : to[e]){\n if (potential_fixed[ne]) continue;\n auto len = edge_len(e, ne);\n assert(len >= T());\n auto nd = d + len;\n if (prv[ne] == -1 || nd < dijkstra[ne]){\n dijkstra[ne] = nd;\n prv[ne] = e;\n pq.emplace(dijkstra[ne], ne);\n }\n }\n }\n for (int i = 0; i < gt+1; i++){\n if (prv[i] == -1) continue;\n potential[i] = dijkstra[i] + potential[i] - potential[gs];\n }\n\n /*\n\n while (!pq.empty()) {\n const int e = pq.top().second;\n pq.pop();\n if (potential_fixed.at(e)) continue;\n if (e != gs) potential_add_unfixed_es = edge_len(prv.at(e), e);\n\n std::vector<std::pair<int, int>> push_cands;\n\n auto rec = [&](auto &&self, int cur) -> bool {\n if (cur == gt) return true;\n fix_potential(cur);\n\n for (int nxt : to.at(cur)) {\n if (potential_fixed.at(nxt)) continue;\n\n const T len = edge_len(cur, nxt) - potential_add_unfixed_es;\n // if (len < T()) std::cerr << cur << ' ' << nxt << ' ' << len << std::endl;\n assert(len >= T());\n\n if (len == T()) {\n prv.at(nxt) = cur;\n if (self(self, nxt)) return true;\n } else {\n if (prv.at(nxt) == -1 or potential_add_unfixed_es + len < dijkstra.at(nxt)) {\n dijkstra.at(nxt) = potential_add_unfixed_es + len;\n prv.at(nxt) = cur;\n push_cands.emplace_back(nxt, cur);\n }\n }\n }\n return false;\n };\n if (rec(rec, e)) break;\n\n for (auto [nxt, now] : push_cands) {\n if (prv.at(nxt) == now) pq.emplace(dijkstra.at(nxt), nxt);\n }\n }\n\n for (int e = 0; e < gt + 1; ++e) {\n if (!potential_fixed.at(e)) fix_potential(e);\n }\n */\n\n // noya2 end\n\n if (prv.at(gt) < 0) return false;\n\n prv.assign(gt + 1, -1);\n std::queue<int> q;\n q.push(gs);\n\n for (int now = q.front(); now != gt; now = q.front()) {\n q.pop();\n for (int nxt : to.at(now)) {\n if (prv[nxt] == -1 && potential[now] + l(nxt) == potential[nxt]){ // noya2\n // if (prv.at(nxt) == -1 and edge_len(now, nxt) == T()) { // noya2\n prv.at(nxt) = now;\n q.push(nxt);\n }\n }\n }\n\n for (int e = prv.at(gt); e != gs; e = prv.at(e)) {\n potential.at(e) -= l(e);\n I.at(e) = !I.at(e);\n }\n\n return true;\n}\n#line 3 \"combinatorial_opt/matroids/graphic_matroid.hpp\"\n#include <utility>\n#line 5 \"combinatorial_opt/matroids/graphic_matroid.hpp\"\n\n// GraphicMatroid: subgraph of undirected graphs, without loops\nclass GraphicMatroid {\n using Vertex = int;\n using Element = int;\n int M;\n int V; // # of vertices of graph\n std::vector<std::vector<std::pair<Vertex, Element>>> to;\n std::vector<std::pair<Vertex, Vertex>> edges;\n std::vector<Element> backtrack;\n std::vector<Vertex> vprev;\n std::vector<int> depth, root;\n\npublic:\n GraphicMatroid(int V, const std::vector<std::pair<Vertex, Vertex>> &edges_)\n : M(edges_.size()), V(V), to(V), edges(edges_) {\n for (int e = 0; e < int(edges_.size()); e++) {\n int u = edges_[e].first, v = edges_[e].second;\n assert(0 <= u and u < V);\n assert(0 <= v and v < V);\n if (u != v) {\n to[u].emplace_back(v, e);\n to[v].emplace_back(u, e);\n }\n }\n }\n int size() const { return M; }\n\n std::vector<Vertex> que;\n template <class State> void set(State I) {\n assert(int(I.size()) == M);\n backtrack.assign(V, -1);\n vprev.assign(V, -1);\n depth.assign(V, -1);\n root.assign(V, -1);\n que.resize(V);\n int qb = 0, qe = 0;\n for (Vertex i = 0; i < V; i++) {\n if (backtrack[i] >= 0) continue;\n que[qb = 0] = i, qe = 1, depth[i] = 0;\n while (qb < qe) {\n Vertex now = que[qb++];\n root[now] = i;\n for (auto nxt : to[now]) {\n if (depth[nxt.first] < 0 and I[nxt.second]) {\n backtrack[nxt.first] = nxt.second;\n vprev[nxt.first] = now;\n depth[nxt.first] = depth[now] + 1;\n que[qe++] = nxt.first;\n }\n }\n }\n }\n }\n\n std::vector<Element> circuit(const Element e) const {\n assert(0 <= e and e < M);\n Vertex s = edges[e].first, t = edges[e].second;\n if (root[s] != root[t]) return {};\n std::vector<Element> ret{e};\n auto step = [&](Vertex &i) { ret.push_back(backtrack[i]), i = vprev[i]; };\n int ddepth = depth[s] - depth[t];\n for (; ddepth > 0; --ddepth) step(s);\n for (; ddepth < 0; ++ddepth) step(t);\n while (s != t) step(s), step(t);\n return ret;\n }\n};\n#line 4 \"combinatorial_opt/matroids/partition_matroid.hpp\"\n\n// Partition matroid (partitional matroid) : direct sum of uniform matroids\nclass PartitionMatroid {\n using Element = int;\n int M;\n std::vector<std::vector<Element>> parts;\n std::vector<int> belong;\n std::vector<int> R;\n std::vector<int> cnt;\n std::vector<std::vector<Element>> circuits;\n\npublic:\n // parts: partition of [0, 1, ..., M - 1]\n // R: only R[i] elements from parts[i] can be chosen for each i.\n PartitionMatroid(int M, const std::vector<std::vector<int>> &parts_, const std::vector<int> &R_)\n : M(M), parts(parts_), belong(M, -1), R(R_) {\n assert(parts.size() == R.size());\n for (int i = 0; i < int(parts.size()); i++) {\n for (Element e : parts[i]) belong[e] = i;\n }\n for (Element e = 0; e < M; e++) {\n // assert(belong[e] != -1);\n if (belong[e] == -1) {\n belong[e] = parts.size();\n parts.push_back({e});\n R.push_back(1);\n }\n }\n }\n int size() const { return M; }\n\n template <class State> void set(const State &I) {\n cnt = R;\n for (int e = 0; e < M; e++) {\n if (I[e]) cnt[belong[e]]--;\n }\n circuits.assign(cnt.size(), {});\n for (int e = 0; e < M; e++) {\n if (I[e] and cnt[belong[e]] == 0) circuits[belong[e]].push_back(e);\n }\n }\n\n std::vector<Element> circuit(const Element e) const {\n assert(0 <= e and e < M);\n int p = belong[e];\n if (cnt[p] == 0) {\n auto ret = circuits[p];\n ret.push_back(e);\n return ret;\n }\n return {};\n }\n};\n#line 5 \"combinatorial_opt/test/matroid_intersection_dijkstra.aoj1605.test.cpp\"\n#include <iostream>\n#include <numeric>\n#line 9 \"combinatorial_opt/test/matroid_intersection_dijkstra.aoj1605.test.cpp\"\nusing namespace std;\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n while (true) {\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0) break;\n vector<vector<int>> partition(2);\n vector<int> R{K, N - 1 - K};\n vector<pair<int, int>> edges;\n vector<int> weight;\n for (int e = 0; e < M; ++e) {\n int u, v, w;\n char l;\n cin >> u >> v >> w >> l;\n --u, --v;\n partition[l == 'B'].push_back(e);\n edges.emplace_back(u, v);\n weight.push_back(-w);\n }\n PartitionMatroid M1(edges.size(), partition, R);\n GraphicMatroid M2(N, edges);\n\n vector<int> potential(weight.size());\n vector<bool> ret(weight.size());\n while (augment_matroid_intersection_dijkstra(M1, M2, ret, weight, potential)) continue;\n int ne = accumulate(ret.begin(), ret.end(), 0);\n if (ne < N - 1) {\n cout << \"-1\\n\";\n } else {\n int sum = 0;\n for (int e = 0; e < M; ++e) sum -= ret.at(e) * weight.at(e);\n cout << sum << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 3744, "score_of_the_acc": -0.5122, "final_rank": 15 }, { "submission_id": "aoj_1605_10220901", "code_snippet": "// AOJ #1605 Bridge Construction Planning\n// 2025.2.15\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct DSU {\n vector<int> parent, rank;\n DSU(int n) : parent(n), rank(n,0) {\n for(int i = 0; i < n; i++){\n parent[i] = i;\n }\n }\n int findp(int a) {\n return parent[a] == a ? a : parent[a] = findp(parent[a]);\n }\n bool unite(int a, int b) {\n a = findp(a), b = findp(b);\n if(a == b) return false;\n if(rank[a] < rank[b]) swap(a, b);\n parent[b] = a;\n if(rank[a] == rank[b]) rank[a]++;\n return true;\n }\n};\n \nstruct Edge {\n int u, v;\n int cost;\n bool isA;\n int idx;\n};\n \nstruct MSTResult {\n bool valid;\n int redCount;\n ll origCost;\n};\n \nMSTResult computeMST(int n, const vector<Edge>& edges, int lambda) {\n int m = edges.size();\n vector<Edge> sortedEdges = edges;\n auto cmp = [lambda](const Edge &a, const Edge &b) {\n int wa = a.cost + (a.isA ? lambda : 0);\n int wb = b.cost + (b.isA ? lambda : 0);\n if(wa != wb) return wa < wb;\n return a.idx < b.idx;\n };\n sort(sortedEdges.begin(), sortedEdges.end(), cmp);\n \n DSU dsu(n);\n int edgeCount = 0;\n int redCount = 0;\n ll sumOrig = 0;\n for (int i = 0; i < m && edgeCount < n - 1; i++){\n int u = sortedEdges[i].u - 1;\n int v = sortedEdges[i].v - 1;\n if(dsu.unite(u, v)){\n edgeCount++;\n sumOrig += sortedEdges[i].cost;\n if(sortedEdges[i].isA) redCount++;\n }\n }\n MSTResult res;\n res.valid = (edgeCount == n - 1);\n res.redCount = res.valid ? redCount : -1;\n res.origCost = res.valid ? sumOrig : -1;\n return res;\n}\n \nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n \n while(true){\n int n, m, k;\n cin >> n >> m >> k;\n if(n == 0) break;\n \n vector<Edge> edges;\n edges.reserve(m);\n for (int i = 0; i < m; i++){\n int u, v, w;\n char comp;\n cin >> u >> v >> w >> comp;\n Edge e;\n e.u = u; e.v = v; e.cost = w;\n e.isA = (comp == 'A');\n e.idx = i;\n edges.push_back(e);\n }\n \n int lambdaLow = -1000, lambdaHigh = 1000;\n MSTResult resLow = computeMST(n, edges, lambdaLow);\n MSTResult resHigh = computeMST(n, edges, lambdaHigh);\n if(!resLow.valid || !resHigh.valid){\n cout << -1 << \"\\n\";\n continue;\n }\n int maxRed = resLow.redCount;\n int minRed = resHigh.redCount;\n \n if(k < minRed || k > maxRed){\n cout << -1 << endl;\n continue;\n }\n \n ll best = -LLONG_MAX;\n for (int lambda = lambdaLow; lambda <= lambdaHigh; lambda++){\n MSTResult cur = computeMST(n, edges, lambda);\n if(!cur.valid) continue;\n ll candidate = cur.origCost + (ll)lambda * (cur.redCount - k);\n best = max(best, candidate);\n }\n \n cout << best << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 380, "memory_kb": 3424, "score_of_the_acc": -0.5056, "final_rank": 13 }, { "submission_id": "aoj_1605_10137704", "code_snippet": "#include<iostream>\n#include<algorithm>\ntemplate<typename T=int>\nstruct Edge{\n int from,to;\n T weight;\n int index;\n Edge(int from_,int to_,T weight_=T(),int index_=-1):from(from_),to(to_),weight(weight_),index(index_){}\n Edge():from(-1),to(-1),weight(),index(-1){}\n friend std::ostream &operator<<(std::ostream &os,const Edge&e){\n os<<'[';\n os<<\"from:\"<<e.from;\n os<<\"to:\"<<e.to;\n os<<\"weight:\"<<e.weight;\n os<<\"index:\"<<e.index;\n os<<']';\n return os;\n }\n};\n#include<vector>\ntemplate<typename T=int>\nstruct Graph{\nprivate:\n int n;\n std::vector<Edge<T>>edge;\n std::vector<Edge<T>>g;\n std::vector<int>ptr;\n bool directed;\n struct graph_range{\n using iterator=typename std::vector<Edge<T>>::iterator;\n iterator l,r;\n iterator begin()const{return l;}\n iterator end()const{return r;}\n int size()const{return r-l;}\n Edge<T> &operator[](int i)const{return l[i];}\n };\n struct const_graph_range{\n using iterator=typename std::vector<Edge<T>>::const_iterator;\n iterator l,r;\n iterator begin()const{return l;}\n iterator end()const{return r;}\n int size()const{return r-l;}\n const Edge<T> &operator[](int i)const{return l[i];}\n };\npublic:\n Graph(int n_,bool dir_):n(n_),directed(dir_){}\n Graph():n(0){}\n Graph(int n_,bool dir_,const std::vector<Edge<T>>&e):n(n_),directed(dir_),edge(e){build();}\n template<bool weighted=false,bool index=1>\n void read(int m){\n edge.reserve(m);\n for(int i=0;i<m;i++){\n int u,v;\n std::cin>>u>>v;\n T w;\n if constexpr(index)u--,v--;\n if constexpr(weighted)std::cin>>w;\n else w=1;\n edge.emplace_back(u,v,w,i);\n }\n build();\n }\n void add_edge(int u,int v){\n int id=edge.size();\n edge.emplace_back(u,v,1,id);\n }\n void add_edge(int u,int v,T w){\n int id=edge.size();\n edge.emplace_back(u,v,w,id);\n }\n void add_edge(int u,int v,T w,int index){\n edge.emplace_back(u,v,w,index);\n }\n void build(){\n std::vector<int>cnt(n+1,0);\n for(const Edge<T>&e:edge){\n cnt[e.from+1]++;\n if(!directed)cnt[e.to+1]++;\n }\n for(int i=1;i<=n;i++)cnt[i]+=cnt[i-1];\n ptr=cnt;\n g.resize(cnt[n]);\n for(const Edge<T>&e:edge){\n g[cnt[e.from]++]=e;\n if(!directed)g[cnt[e.to]++]=Edge<T>(e.to,e.from,e.weight,e.index);\n }\n }\n void reverse(){\n if(directed){\n for(Edge<T>&e:edge)swap(e.from,e.to);\n build();\n }\n }\n inline void to_directed(){\n directed=true;\n build();\n }\n inline void to_undirected(){\n directed=false;\n build();\n }\n void reserve(int m){edge.reserve(m);}\n graph_range operator[](int i){return graph_range{g.begin()+ptr[i],g.begin()+ptr[i+1]};}\n const_graph_range operator[](int i)const{return const_graph_range{g.begin()+ptr[i],g.begin()+ptr[i+1]};}\n const Edge<T>& get_edge(int i)const{return edge[i];}\n inline bool is_directed()const{return directed;}\n inline int size()const{return n;}\n inline int edge_size()const{return edge.size();}\n typename std::vector<Edge<T>>::iterator begin(){return edge.begin();}\n typename std::vector<Edge<T>>::iterator end(){return edge.end();}\n typename std::vector<Edge<T>>::const_iterator begin()const{return edge.begin();}\n typename std::vector<Edge<T>>::const_iterator end()const{return edge.end();}\n};\nstruct GraphicMatroid{\nprivate:\n Graph<>g;\n std::vector<std::pair<int,int>>pre;\n std::vector<int>que,dst,root;\n int p,q;\npublic:\n GraphicMatroid(const Graph<>&g_):g(g_),pre(g_.edge_size()),que(g_.edge_size()),dst(g_.size()),root(g_.size()){}\n int size()const{return g.edge_size();}\n void set(const std::vector<bool>&I){\n std::fill(pre.begin(),pre.end(),std::make_pair(-1,-1));\n for(int i=0;i<g.size();i++){\n if(pre[i].first==-1){\n for(p=q=0,que[q++]=i,dst[i]=0;p<q;){\n int x=que[p++];\n root[x]=i;\n for(const Edge<>&e:g[x])if(I[e.index]&&pre[e.to].first==-1){\n pre[e.to]=std::make_pair(e.from,e.index);\n dst[e.to]=dst[e.from]+1;\n que[q++]=e.to;\n }\n }\n }\n }\n }\n std::vector<int>circuit(int x)const{\n const Edge<>&e=g.get_edge(x);\n if(root[e.from]!=root[e.to])return {};\n std::vector<int>res;\n int u=e.from,v=e.to;\n if(dst[u]>dst[v])std::swap(u,v);\n while(dst[u]<dst[v])res.push_back(pre[v].second),v=pre[v].first;\n while(u!=v){\n res.push_back(pre[u].second),u=pre[u].first;\n res.push_back(pre[v].second),v=pre[v].first;\n }\n return res;\n }\n};\nstruct PartitionMatroid{\nprivate:\n int n;\n std::vector<std::vector<int>>s;\n std::vector<int>belong;\n std::vector<int>c,c2;\n std::vector<std::vector<int>>circuits;\npublic:\n PartitionMatroid(int n_,const std::vector<std::vector<int>>&s_,const std::vector<int>&c_):n(n_),s(s_),belong(n,-1),c2(c_){\n for(int i=0;i<(int)s.size();i++){\n for(int j:s[i])belong[j]=i;\n }\n for(int i=0;i<n;i++)if(belong[i]==-1){\n belong[i]=s.size();\n s.push_back({i});\n c2.push_back(1);\n }\n circuits.resize(s.size());\n }\n int size()const{return n;}\n void set(const std::vector<bool>&I){\n c=c2;\n for(int i=0;i<n;i++)if(I[i])c[belong[i]]--;\n for(int i=0;i<(int)circuits.size();i++)circuits[i].clear();\n for(int i=0;i<n;i++)if(I[i]&&c[belong[i]]==0)circuits[belong[i]].push_back(i);\n }\n std::vector<int>circuit(int x)const{\n if(c[belong[x]]==0){\n std::vector<int>res=circuits[belong[x]];\n res.push_back(x);\n return res;\n }\n return {};\n }\n};\n#include<cassert>\n#include<limits>\n#include<numeric>\ntemplate<typename T>\nstd::pair<std::vector<T>,std::vector<int>>shortest_path_faster_algorithm(const Graph<T>&g,int s=0){\n static constexpr T inf=std::numeric_limits<T>::max();\n static constexpr T minf=std::numeric_limits<T>::min();\n int n=g.size();\n std::vector<T>dst(n,inf);\n std::vector<int>pre(n,-1);\n dst[s]=0;\n std::vector<int>que(n+1);\n int p=0,q=0;\n std::vector<int>time(n,0);\n std::vector<bool>contain(n,false);\n que[q++]=s;\n contain[s]=true;\n while(p!=q){\n int x=que[p++];\n if(p==n+1)p=0;\n contain[x]=false;\n for(const Edge<T>&e:g[x]){\n T cost=dst[x]+e.weight;\n if(dst[e.to]>cost){\n dst[e.to]=cost;\n pre[e.to]=e.from;\n if(!contain[e.to]){\n if(++time[e.to]>=n)continue;\n contain[e.to]=true;\n que[q++]=e.to;\n if(q==n+1)q=0;\n }\n }\n }\n }\n std::fill(contain.begin(),contain.end(),false);\n p=q=0;\n for(int i=0;i<n;i++)for(const Edge<T>&e:g[i])if(dst[i]!=inf&&e.from==e.to&&e.weight<0)time[i]=n;\n for(int i=0;i<n;i++)if(time[i]>=n){\n que[q++]=i;\n contain[i]=true;\n }\n while(p!=q){\n int x=que[p++];\n dst[x]=minf;\n if(p==n+1)p=0;\n for(const Edge<T>&e:g[x])if(!contain[e.to]){\n contain[e.to]=true;\n que[q++]=e.to;\n if(q==n+1)q=0;\n }\n }\n return std::make_pair(dst,pre);\n}\ntemplate<typename M1,typename M2,typename W>\nstd::vector<bool>matroid_intersection(M1 m1,M2 m2,std::vector<W>weight={}){\n static constexpr W inf=std::numeric_limits<W>::max();\n assert(m1.size()==m2.size());\n assert(weight.empty()||(int)weight.size()==m1.size());\n int n=m1.size();\n if(weight.empty())weight=std::vector<W>(n,0);\n std::vector<bool>res(n,false);\n while(true){\n Graph<W>g(n+2,true);\n m1.set(res),m2.set(res);\n for(int i=0;i<n;i++)if(!res[i]){\n std::vector<int>c1=m1.circuit(i),c2=m2.circuit(i);\n if(c1.empty())g.add_edge(i,n+1,0);\n for(int j:c1)if(i!=j)g.add_edge(i,j,-weight[j]*(n+1)+1);\n if(c2.empty())g.add_edge(n,i,weight[i]*(n+1)+1);\n for(int j:c2)if(i!=j)g.add_edge(j,i,weight[i]*(n+1)+1);\n }\n g.build();\n auto [dst,pre]=shortest_path_faster_algorithm(g,n);\n if(dst[n+1]==inf)break;\n for(int i=pre[n+1];i!=n;i=pre[i])res[i]=!res[i];\n }\n return res;\n}\nusing namespace std;\nint main(){\n int n,m,k;\n while(true){\n cin>>n>>m>>k;\n if(n==0)break;\n Graph<>g(n,false);\n vector<int>weight(m);\n vector<vector<int>>b(2);\n for(int i=0;i<m;i++){\n int u,v,w;\n char c;\n cin>>u>>v>>w>>c;\n u--,v--;\n g.add_edge(u,v);\n weight[i]=w;\n b[c=='B'].push_back(i);\n }\n g.build();\n GraphicMatroid gm(g);\n PartitionMatroid pm(m,b,{k,n-1-k});\n auto ans=matroid_intersection(gm,pm,weight);\n int sum=0;\n for(int i=0;i<m;i++)if(ans[i])sum+=weight[i],n--;\n if(n==1)cout<<sum<<'\\n';\n else cout<<\"-1\\n\";\n }\n}", "accuracy": 1, "time_ms": 340, "memory_kb": 4804, "score_of_the_acc": -0.5919, "final_rank": 18 }, { "submission_id": "aoj_1605_10021837", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroids/matroid_example.hpp\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroids/graphic_matroid.hpp\n// GraphicMatroid: subgraph of undirected graphs, without loops\nclass GraphicMatroid{\n using Vertex = int;\n using Element = int;\n int M;\n int n; // # of vertices of graph\n vector<vector<pair<Vertex,Element>>> to; //{行先、辺番号}\n vector<pair<Vertex,Vertex>> edges; \n vector<Element> backtrack;\n vector<Vertex> vprev;\n vector<int> dep,root;\n\n public:\n GraphicMatroid(int n,const vector<pair<Vertex,Vertex>> &edges_)\n : M(edges_.size()),n(n),to(n),edges(edges_){\n for(int e = 0;e < int(edges_.size());e++){\n int u = edges_[e].first,v = edges_[e].second;\n assert(0 <= u && u < n);\n assert(0 <= v && v < n);\n if(u != v){\n to[u].emplace_back(v,e);\n to[v].emplace_back(u,e);\n }\n }\n }\n int size()const{return M;}\n\n vector<Vertex> que;\n // O(size of matroid)\n template<class State = vector<bool>> void set(State I){\n assert(int(I.size()) == M);\n backtrack.assign(n,-1);\n vprev.assign(n,-1);\n dep.assign(n,-1);\n root.assign(n,-1);\n que.resize(n);\n int qb = 0,qe = 0;\n for(Vertex i = 0;i < n;i++){\n if(backtrack[i] >= 0)continue;\n que[qb = 0] = i;qe = 1,dep[i] = 0;\n while(qb < qe){\n Vertex ov = que[qb++];\n root[ov] = i;\n for(auto [nv,nidx] : to[ov]){\n if(dep[nv] < 0 && I[nidx]){\n backtrack[nv] = nidx;\n vprev[nv] = ov;\n dep[nv] = dep[ov] + 1;\n que[qe++] = nv;\n }\n }\n }\n }\n }\n\n // 閉路が順番に出力されるわけではない。O(size of independent set)\n vector<Element> circuit(const Element e)const{\n assert(0 <= e && e < M);\n Vertex s = edges[e].first,t = edges[e].second;\n if(root[s] != root[t])return {};\n vector<Element> res{e};\n auto step = [&](Vertex &i){res.emplace_back(backtrack[i]),i = vprev[i];};\n int dep_diff = dep[s] - dep[t];\n for(;dep_diff > 0;--dep_diff)step(s);\n for(;dep_diff < 0;++dep_diff)step(t);\n while(s != t)step(s),step(t);\n return res;\n }\n};\n\n\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroids/matroid_example.hpp\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroids/partition_matroid.hpp\n// Partition matroid (partitional matroid) : direct sum of uniform matroids\nclass PartitionMatroid{\n using Element = int;\n int M;\n vector<vector<Element>> parts;\n vector<int> belong;\n vector<int> R;\n vector<int> cnt;\n vector<vector<Element>> circuits;\n\n public:\n // parts: partition of [0, 1, ..., M - 1]\n // R: only R[i] elements from parts[i] can be chosen for each i.\n PartitionMatroid(int M,const vector<vector<Element>> &parts_,const vector<int> &R_)\n : M(M),parts(parts_),belong(M,-1),R(R_) {\n assert(parts.size() == R.size());\n for(int i = 0;i < int(parts.size());i++){\n for(Element e : parts[i])belong[e] = i;\n }\n for(Element e = 0;e < M;e++){\n if(belong[e] == -1){\n belong[e] = parts.size();\n parts.push_back({e});\n R.emplace_back(1);\n }\n }\n }\n int size() const {return M;}\n\n // O(size of matroid)\n template<class State = vector<bool>>\n void set(const State &I){\n cnt = R;\n for(int e = 0;e < M;e++){\n if(I[e])cnt[belong[e]]--;\n }\n circuits.assign(cnt.size(),{});\n for(int e = 0;e < M;e++){\n if(I[e] && cnt[belong[e]] == 0)circuits[belong[e]].emplace_back(e);\n }\n }\n // O(size of independent set)\n vector<Element> circuit(const Element e)const{\n assert(0 <= e && e < M);\n int p = belong[e];\n if(cnt[p] == 0){\n auto res = circuits[p];\n res.emplace_back(e);\n return res;\n }\n return {};\n }\n};\n\n\n\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroid_intersection.hpp.html\n// http://dopal.cs.uec.ac.jp/okamotoy/lect/2015/matroid/\n// ``重み付き!!!``重み付き!!!``重み付き!!!``重み付き!!!``重み付き!!!``重み付き!!!\n// m1, m2: matroids\n// I: independent set (will be updated if augmenting path is found)\n// Return `true` iff augmenting path is found.\n// Complexity: O(Cn + n^2) (C: circuit query,n:matroid台集合の要素数)\ntemplate<class Matroid1,class Matroid2,class T = int>\nbool matroid_intersection_augment(Matroid1 &m1,Matroid2 &m2,vector<bool> &I,const vector<T> &weights){\n const int n = m1.size();\n assert(m2.size() == n);\n assert((int)I.size() == n);\n\n const int gs = n,gt = n+1;\n const T INVALID = numeric_limits<T>::max();\n vector<tuple<int,int,T>> v;\n m1.set(I);\n m2.set(I);\n // 補助グラフの作成 O(nC + n^2)\n for(int e = 0;e < n;e++){\n if(I[e])continue;\n auto c1 = m1.circuit(e),c2 = m2.circuit(e);\n if(c1.empty())v.emplace_back(e,gt,T());\n for(auto f : c1)if(f != e)v.emplace_back(e,f,-weights[f]);\n if(c2.empty())v.emplace_back(gs,e,weights[e]);\n for(auto f : c2)if(f != e)v.emplace_back(f,e,weights[e]);\n }\n // 最短経路 Bellman-ford O(n^3)\n vector<T> dist(n+2,INVALID);\n vector<T> prev(n+2,INVALID);\n dist[gs] = 0;\n for(int _ = 0;_ < n+2;_++){ // 今、頂点数は n+2\n bool changed = false;\n for(auto &[ov,nv,wei] : v){\n if(dist[ov] == INVALID)continue;\n if(dist[nv] > dist[ov] + wei){\n dist[nv] = dist[ov] + wei;\n prev[nv] = ov;\n changed = true;\n }\n }\n if(!changed)break;\n }// 負閉路はそんざいしない(はず)\n \n // 経路復元。増加路実行\n if(dist[gt] == INVALID)return false;\n int now_vertex = gt;\n while(prev[now_vertex] != INVALID){\n now_vertex = prev[now_vertex];\n if(now_vertex != gs && now_vertex != gt)I[now_vertex] = !I[now_vertex];\n }\n return true;\n}\n\n// Minimum weight matroid intersection solver\n// Complexity: O(Cn^2 + n^4) (C : circuit query)\ntemplate <class Matroid1, class Matroid2, class T = int>\nvector<bool> MatroidIntersection(Matroid1 matroid1,Matroid2 matroid2,vector<T> &weights){\n const int n = matroid1.size();\n assert(matroid2.size() == n);\n assert(weights.size() == n);\n \n vector<bool> I(n);\n while(matroid_intersection_augment(matroid1,matroid2,I,weights));\n return I;\n}\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,m,K;cin >> n >> m >> K;\n if(!n)break;\n ve parts(2);\n vector<int> R{K,n-K-1};\n vector<pair<int,int>> edge;\n vi weight;\n rep(i,m){\n int u,v,w;char l;cin >> u >> v >> w >> l;\n u--;v--;\n edge.emplace_back(u,v);\n parts[l-'A'].emplace_back(i);\n weight.emplace_back(w);\n }\n GraphicMatroid gm(n,edge);\n PartitionMatroid pm(m,parts,R);\n vb I = MatroidIntersection(gm,pm,weight);\n int cnt = accumulate(ALL(I),0);\n if(cnt != n-1)cout << \"-1\\n\";\n else{\n int res = 0;\n rep(e,m)res += I[e]*weight[e];\n cout << res << \"\\n\";\n }\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 4988, "score_of_the_acc": -0.5101, "final_rank": 14 }, { "submission_id": "aoj_1605_10020829", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroids/matroid_example.hpp\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroids/graphic_matroid.hpp\n// GraphicMatroid: subgraph of undirected graphs, without loops\nclass GraphicMatroid{\n using Vertex = int;\n using Element = int;\n int M;\n int n; // # of vertices of graph\n vector<vector<pair<Vertex,Element>>> to; //{行先、辺番号}\n vector<pair<Vertex,Vertex>> edges; \n vector<Element> backtrack;\n vector<Vertex> vprev;\n vector<int> dep,root;\n\n public:\n GraphicMatroid(int n,const vector<pair<Vertex,Vertex>> &edges_)\n : M(edges_.size()),n(n),to(n),edges(edges_){\n for(int e = 0;e < int(edges_.size());e++){\n int u = edges_[e].first,v = edges_[e].second;\n assert(0 <= u && u < n);\n assert(0 <= v && v < n);\n if(u != v){\n to[u].emplace_back(v,e);\n to[v].emplace_back(u,e);\n }\n }\n }\n int size()const{return M;}\n\n vector<Vertex> que;\n // O(size of matroid)\n template<class State = vector<bool>> void set(State I){\n assert(int(I.size()) == M);\n backtrack.assign(n,-1);\n vprev.assign(n,-1);\n dep.assign(n,-1);\n root.assign(n,-1);\n que.resize(n);\n int qb = 0,qe = 0;\n for(Vertex i = 0;i < n;i++){\n if(backtrack[i] >= 0)continue;\n que[qb = 0] = i;qe = 1,dep[i] = 0;\n while(qb < qe){\n Vertex ov = que[qb++];\n root[ov] = i;\n for(auto [nv,nidx] : to[ov]){\n if(dep[nv] < 0 && I[nidx]){\n backtrack[nv] = nidx;\n vprev[nv] = ov;\n dep[nv] = dep[ov] + 1;\n que[qe++] = nv;\n }\n }\n }\n }\n }\n\n // 閉路が順番に出力されるわけではない。O(size of independent set)\n vector<Element> circuit(const Element e)const{\n assert(0 <= e && e < M);\n Vertex s = edges[e].first,t = edges[e].second;\n if(root[s] != root[t])return {};\n vector<Element> res{e};\n auto step = [&](Vertex &i){res.emplace_back(backtrack[i]),i = vprev[i];};\n int dep_diff = dep[s] - dep[t];\n for(;dep_diff > 0;--dep_diff)step(s);\n for(;dep_diff < 0;++dep_diff)step(t);\n while(s != t)step(s),step(t);\n return res;\n }\n};\n\n\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroids/matroid_example.hpp\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroids/partition_matroid.hpp\n// Partition matroid (partitional matroid) : direct sum of uniform matroids\nclass PartitionMatroid{\n using Element = int;\n int M;\n vector<vector<Element>> parts;\n vector<int> belong;\n vector<int> R;\n vector<int> cnt;\n vector<vector<Element>> circuits;\n\n public:\n // parts: partition of [0, 1, ..., M - 1]\n // R: only R[i] elements from parts[i] can be chosen for each i.\n PartitionMatroid(int M,const vector<vector<Element>> &parts_,const vector<int> &R_)\n : M(M),parts(parts_),belong(M,-1),R(R_) {\n assert(parts.size() == R.size());\n for(int i = 0;i < int(parts.size());i++){\n for(Element e : parts[i])belong[e] = i;\n }\n for(Element e = 0;e < M;e++){\n if(belong[e] == -1){\n belong[e] = parts.size();\n parts.push_back({e});\n R.emplace_back(1);\n }\n }\n }\n int size() const {return M;}\n\n // O(size of matroid)\n template<class State = vector<bool>>\n void set(const State &I){\n cnt = R;\n for(int e = 0;e < M;e++){\n if(I[e])cnt[belong[e]]--;\n }\n circuits.assign(cnt.size(),{});\n for(int e = 0;e < M;e++){\n if(I[e] && cnt[belong[e]] == 0)circuits[belong[e]].emplace_back(e);\n }\n }\n // O(size of independent set)\n vector<Element> circuit(const Element e)const{\n assert(0 <= e && e < M);\n int p = belong[e];\n if(cnt[p] == 0){\n auto res = circuits[p];\n res.emplace_back(e);\n return res;\n }\n return {};\n }\n};\n\n\n// https://hitonanode.github.io/cplib-cpp/combinatorial_opt/matroid_intersection.hpp.html\n// http://dopal.cs.uec.ac.jp/okamotoy/lect/2015/matroid/\n// ``重み付き!!!``重み付き!!!``重み付き!!!``重み付き!!!``重み付き!!!``重み付き!!!\n// m1, m2: matroids\n// I: independent set (will be updated if augmenting path is found)\n// Return `true` iff augmenting path is found.\n// Complexity: O(Cn + n^2) (C: circuit query,n:matroid台集合の要素数)\ntemplate<class Matroid1,class Matroid2,class T = int>\nbool matroid_intersection_augment(Matroid1 &m1,Matroid2 &m2,vector<bool> &I,const vector<T> &weights){\n const int n = m1.size();\n assert(m2.size() == n);\n assert((int)I.size() == n);\n\n // auto weight = [&](int e){return weights.empty() ? T() : weights[e]*(n+1);};\n\n const int gs = n,gt = n+1;\n const T INVALID = numeric_limits<T>::max();\n vector<tuple<int,int,T>> v(n+2);\n m1.set(I);\n m2.set(I);\n // 補助グラフの作成 O(nC + n^2)\n for(int e = 0;e < n;e++){\n if(I[e])continue;\n auto c1 = m1.circuit(e),c2 = m2.circuit(e);\n if(c1.empty())v.emplace_back(e,gt,T());\n for(auto f : c1)if(f != e)v.emplace_back(e,f,-weights[f]);\n if(c2.empty())v.emplace_back(gs,e,weights[e]);\n for(auto f : c2)if(f != e)v.emplace_back(f,e,weights[e]);\n }\n // 最短経路 Bellman-ford O(n^3)\n vector<T> dist(n+2,INVALID);\n vector<T> prev(n+2,INVALID);\n dist[gs] = 0;\n for(int _ = 0;_ < n+2;_++){ // 今、頂点数は n+2\n bool changed = false;\n for(auto &[ov,nv,wei] : v){\n if(dist[ov] == INVALID)continue;\n if(dist[nv] > dist[ov] + wei){\n dist[nv] = dist[ov] + wei;\n prev[nv] = ov;\n changed = true;\n }\n }\n if(!changed)break;\n }// 負閉路はそんざいしない(はず)\n \n // 経路復元。増加路実行\n if(dist[gt] == INVALID)return false;\n int now_vertex = gt;\n while(prev[now_vertex] != INVALID){\n now_vertex = prev[now_vertex];\n if(now_vertex != gs && now_vertex != gt)I[now_vertex] = !I[now_vertex];\n }\n return true;\n}\n\n// Minimum weight matroid intersection solver\n// Complexity: O(Cn^2 + n^4) (C : circuit query)\ntemplate <class Matroid1, class Matroid2, class T = int>\nvector<bool> MatroidIntersection(Matroid1 matroid1,Matroid2 matroid2,vector<T> &weights){\n const int n = matroid1.size();\n assert(matroid2.size() == n);\n assert(weights.size() == n);\n \n vector<bool> I(n);\n while(matroid_intersection_augment(matroid1,matroid2,I,weights));\n return I;\n}\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,m,K;cin >> n >> m >> K;\n if(!n)break;\n ve parts(2);\n vector<int> R{K,n-K-1};\n vector<pair<int,int>> edge;\n vi weight;\n rep(i,m){\n int u,v,w;char l;cin >> u >> v >> w >> l;\n u--;v--;\n edge.emplace_back(u,v);\n parts[l-'A'].emplace_back(i);\n weight.emplace_back(w);\n }\n GraphicMatroid gm(n,edge);\n PartitionMatroid pm(m,parts,R);\n vb I = MatroidIntersection(gm,pm,weight);\n int cnt = accumulate(ALL(I),0);\n if(cnt != n-1)cout << \"-1\\n\";\n else{\n int res = 0;\n rep(e,m)res += I[e]*weight[e];\n cout << res << \"\\n\";\n }\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 5000, "score_of_the_acc": -0.4488, "final_rank": 12 }, { "submission_id": "aoj_1605_9315926", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct dsu {\n public:\n dsu() : _n(0) {}\n dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\nusing namespace std;\n\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\ntemplate<class T>\nbool chmax(T& p, T q) {\n if (p < q) {\n p = q; return 1;\n }\n return 0;\n};\n\n\nvoid solve(ll N,ll M,ll K) {\n vector<vector<pair<ll,ll>>> GA(N),GB(N);\n rep(i,M){\n ll U,V,W;\n char C;\n cin>>U>>V>>W>>C;\n U--;V--;\n (C=='A'?GA:GB)[U].push_back({V,W});\n (C=='A'?GA:GB)[V].push_back({U,W});\n }\n {\n dsu dA(N),dB(N),d(N);\n ll nA=0,nB=0;\n rep(i,N){\n for(auto vw:GA[i]){\n if(!dA.same(i,vw.first))nA++;\n d.merge(i,vw.first);\n dA.merge(i,vw.first);\n }\n for(auto vw:GB[i]){\n if(!dB.same(i,vw.first))nB++;\n d.merge(i,vw.first);\n dB.merge(i,vw.first);\n }\n }\n if(d.size(0)!=N){\n cout<<-1<<endl;\n return;\n }\n if(nA<K){\n cout<<-1<<endl;\n return;\n }\n if(nB<N-K-1){\n cout<<-1<<endl;\n return;\n }\n }\n\n ll L=0,R=210;\n while(R-L>1){\n ll mid=(R+L)/2;\n\n\n vector<pair<ll,pair<ll,pair<ll,ll>>>> E;\n \n rep(i,N){\n for(auto &vw:GA[i]){\n vw.second+=(mid-105);\n E.push_back({vw.second,{0,{i,vw.first}}});\n }\n for(auto vw:GB[i]){\n E.push_back({vw.second,{1,{i,vw.first}}});\n }\n }\n sort(all(E));\n dsu d(N);\n ll cnt=0;\n rep(i,E.size()){\n ll v=E[i].second.second.first;\n ll u=E[i].second.second.second;\n if(d.same(u,v))continue;\n d.merge(u,v);\n cnt+=(E[i].second.first==0?1:0);\n }\n\n if(cnt>=K){\n L=mid;\n }\n else R=mid;\n \n rep(i,N){\n for(auto &vw:GA[i]){\n vw.second-=(mid-105);\n }\n }\n\n }\n\n ll an=0;\n // cout<<L<<endl;\n vector<pair<ll,pair<ll,pair<ll,ll>>>> E;\n \n rep(i,N){\n for(auto &vw:GA[i]){\n vw.second+=(L-105);\n E.push_back({vw.second,{0,{i,vw.first}}});\n }\n for(auto vw:GB[i]){\n E.push_back({vw.second,{1,{i,vw.first}}});\n }\n }\n sort(all(E));\n dsu d(N);\n ll cnt=0;\n rep(i,E.size()){\n ll v=E[i].second.second.first;\n ll u=E[i].second.second.second;\n if(d.same(u,v))continue;\n d.merge(u,v);\n cnt+=E[i].first;\n }\n cout<<cnt-(L-105)*K<<endl;\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n ll N,M,K;\n while (cin >> N>>M>>K) {\n if (N == 0)return 0;\n solve(N,M,K);\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3492, "score_of_the_acc": -0.0498, "final_rank": 3 }, { "submission_id": "aoj_1605_9278152", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nbool is_end = false;\n\nstruct UnionFind\n{\n int n;\n vector<int> par;\n \n UnionFind() = default;\n UnionFind(int _n) : n(_n), par(_n)\n {\n for (int i = 0; i < n; ++i) par[i] = i;\n }\n \n int root(int x)\n {\n if (par[x] == x) return x;\n else return par[x] = root(par[x]);\n }\n \n bool same(int x, int y)\n {\n return root(x) == root(y);\n }\n \n void merge(int x, int y)\n {\n par[root(y)] = root(x);\n return;\n }\n \n};\n\nvoid solve()\n{\n int N, M, K; cin >> N >> M >> K;\n if (N == 0)\n {\n is_end = true;\n return;\n }\n \n vector<array<int, 3>> edgeA, edgeB;\n for (int i = 0; i < M; ++i)\n {\n int u, v, w; cin >> u >> v >> w;\n u--, v--;\n char c; cin >> c;\n \n if (c == 'A') edgeA.push_back({w, u, v});\n else edgeB.push_back({w, u, v});\n }\n sort(edgeA.begin(), edgeA.end());\n sort(edgeB.begin(), edgeB.end());\n \n int A = edgeA.size(), B = edgeB.size();\n vector<vector<tuple<int, int, int, UnionFind>>> connect(N+1, vector<tuple<int, int, int, UnionFind>>(N+1));\n \n const int INF = 1e9;\n connect[0][0] = {0, 0, 0, UnionFind(N)};\n for (int i = 0; i <= K; ++i)\n {\n for (int j = 0; j <= N - 1 - K; ++j)\n {\n if (i == 0 && j == 0) continue;\n \n int s = INF, a = A, b = B;\n UnionFind uf(N);\n \n // A\n if (i > 0)\n {\n auto [sa, ida, idb, ufa] = connect[i-1][j];\n bool done = false;\n for (int k = ida; k < A; ++k)\n {\n auto [w, u, v] = edgeA[k];\n if (ufa.same(u, v)) continue;\n \n ufa.merge(u, v);\n sa += w;\n ida = k + 1;\n done = true;\n break;\n }\n \n if (done)\n {\n if (sa < s)\n {\n s = sa, a = ida, b = idb;\n uf = ufa;\n }\n }\n }\n \n // B\n if (j > 0)\n {\n auto [sb, ida, idb, ufb] = connect[i][j-1];\n bool done = false;\n for (int k = idb; k < B; ++k)\n {\n auto [w, u, v] = edgeB[k];\n if (ufb.same(u, v)) continue;\n \n ufb.merge(u, v);\n sb += w;\n idb = k + 1;\n done = true;\n break;\n }\n \n if (done)\n {\n if (sb < s)\n {\n s = sb, a = ida, b = idb;\n uf = ufb;\n }\n }\n }\n \n connect[i][j] = {s, a, b, uf};\n }\n }\n \n {\n auto [s, a, b, uf] = connect[K][N - 1 - K];\n if (s >= INF) cout << -1 << endl;\n else cout << s << endl;\n }\n \n return;\n}\n\n\nint main()\n{\n while (!is_end)\n {\n solve();\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 13108, "score_of_the_acc": -1.0375, "final_rank": 19 }, { "submission_id": "aoj_1605_8887366", "code_snippet": "// #line 1 \"library/Template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define ALL(v) (v).begin(),(v).end()\n#define UNIQUE(v) sort(ALL(v)),(v).erase(unique(ALL(v)),(v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v,x) int(lower_bound(ALL(v),(x))-(v).begin())\n#define UB(v,x) int(upper_bound(ALL(v),(x))-(v).begin())\n\nusing ll=long long int;\nusing ull=unsigned long long;\nusing i128=__int128_t;\nusing u128=__uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}\ntemplate<typename T,typename U>T ceil(T x,U y){assert(y!=0); if(y<0)x=-x,y=-y; return (x>0?(x+y-1)/y:x/y);}\ntemplate<typename T,typename U>T floor(T x,U y){assert(y!=0); if(y<0)x=-x,y=-y; return (x>0?x/y:(x-y+1)/y);}\ntemplate<typename T>int popcnt(T x){return __builtin_popcountll(x);}\ntemplate<typename T>int topbit(T x){return (x==0?-1:63-__builtin_clzll(x));}\ntemplate<typename T>int lowbit(T x){return (x==0?-1:__builtin_ctzll(x));}\n// #line 2 \"library/Utility/fastio.hpp\"\n#include <unistd.h>\n\nclass FastIO {\n static constexpr int L = 1 << 16;\n char rdbuf[L];\n int rdLeft = 0, rdRight = 0;\n inline void reload() {\n int len = rdRight - rdLeft;\n memmove(rdbuf, rdbuf + rdLeft, len);\n rdLeft = 0, rdRight = len;\n rdRight += fread(rdbuf + len, 1, L - len, stdin);\n }\n inline bool skip() {\n for (;;) {\n while (rdLeft != rdRight and rdbuf[rdLeft] <= ' ')\n rdLeft++;\n if (rdLeft == rdRight) {\n reload();\n if (rdLeft == rdRight)\n return false;\n } else\n break;\n }\n return true;\n }\n template <typename T, enable_if_t<is_integral<T>::value, int> = 0>\n inline bool _read(T &x) {\n if (!skip())\n return false;\n if (rdLeft + 20 >= rdRight)\n reload();\n bool neg = false;\n if (rdbuf[rdLeft] == '-') {\n neg = true;\n rdLeft++;\n }\n x = 0;\n while (rdbuf[rdLeft] >= '0' and rdLeft < rdRight) {\n x = x * 10 +\n (neg ? -(rdbuf[rdLeft++] ^ 48) : (rdbuf[rdLeft++] ^ 48));\n }\n return true;\n }\n inline bool _read(__int128_t &x) {\n if (!skip())\n return false;\n if (rdLeft + 40 >= rdRight)\n reload();\n bool neg = false;\n if (rdbuf[rdLeft] == '-') {\n neg = true;\n rdLeft++;\n }\n x = 0;\n while (rdbuf[rdLeft] >= '0' and rdLeft < rdRight) {\n x = x * 10 +\n (neg ? -(rdbuf[rdLeft++] ^ 48) : (rdbuf[rdLeft++] ^ 48));\n }\n return true;\n }\n inline bool _read(__uint128_t &x) {\n if (!skip())\n return false;\n if (rdLeft + 40 >= rdRight)\n reload();\n x = 0;\n while (rdbuf[rdLeft] >= '0' and rdLeft < rdRight) {\n x = x * 10 + (rdbuf[rdLeft++] ^ 48);\n }\n return true;\n }\n template <typename T, enable_if_t<is_floating_point<T>::value, int> = 0>\n inline bool _read(T &x) {\n if (!skip())\n return false;\n if (rdLeft + 20 >= rdRight)\n reload();\n bool neg = false;\n if (rdbuf[rdLeft] == '-') {\n neg = true;\n rdLeft++;\n }\n x = 0;\n while (rdbuf[rdLeft] >= '0' and rdbuf[rdLeft] <= '9' and\n rdLeft < rdRight) {\n x = x * 10 + (rdbuf[rdLeft++] ^ 48);\n }\n if (rdbuf[rdLeft] != '.')\n return true;\n rdLeft++;\n T base = .1;\n while (rdbuf[rdLeft] >= '0' and rdbuf[rdLeft] <= '9' and\n rdLeft < rdRight) {\n x += base * (rdbuf[rdLeft++] ^ 48);\n base *= .1;\n }\n if (neg)\n x = -x;\n return true;\n }\n inline bool _read(char &x) {\n if (!skip())\n return false;\n if (rdLeft + 1 >= rdRight)\n reload();\n x = rdbuf[rdLeft++];\n return true;\n }\n inline bool _read(string &x) {\n if (!skip())\n return false;\n for (;;) {\n int pos = rdLeft;\n while (pos < rdRight and rdbuf[pos] > ' ')\n pos++;\n x.append(rdbuf + rdLeft, pos - rdLeft);\n if (rdLeft == pos)\n break;\n rdLeft = pos;\n if (rdLeft == rdRight)\n reload();\n else\n break;\n }\n return true;\n }\n template <typename T> inline bool _read(vector<T> &v) {\n for (auto &x : v) {\n if (!_read(x))\n return false;\n }\n return true;\n }\n\n char wtbuf[L], tmp[50];\n int wtRight = 0;\n inline void _write(const char &x) {\n if (wtRight > L - 32)\n flush();\n wtbuf[wtRight++] = x;\n }\n inline void _write(const string &x) {\n for (auto &c : x)\n _write(c);\n }\n template <typename T, enable_if_t<is_integral<T>::value, int> = 0>\n inline void _write(T x) {\n if (wtRight > L - 32)\n flush();\n if (x == 0) {\n _write('0');\n return;\n } else if (x < 0) {\n _write('-');\n if (__builtin_expect(x == std::numeric_limits<T>::min(), 0)) {\n switch (sizeof(x)) {\n case 2:\n _write(\"32768\");\n return;\n case 4:\n _write(\"2147483648\");\n return;\n case 8:\n _write(\"9223372036854775808\");\n return;\n }\n }\n x = -x;\n }\n int pos = 0;\n while (x != 0) {\n tmp[pos++] = char((x % 10) | 48);\n x /= 10;\n }\n rep(i, 0, pos) wtbuf[wtRight + i] = tmp[pos - 1 - i];\n wtRight += pos;\n }\n inline void _write(__int128_t x) {\n if (wtRight > L - 40)\n flush();\n if (x == 0) {\n _write('0');\n return;\n } else if (x < 0) {\n _write('-');\n x = -x;\n }\n int pos = 0;\n while (x != 0) {\n tmp[pos++] = char((x % 10) | 48);\n x /= 10;\n }\n rep(i, 0, pos) wtbuf[wtRight + i] = tmp[pos - 1 - i];\n wtRight += pos;\n }\n inline void _write(__uint128_t x) {\n if (wtRight > L - 40)\n flush();\n if (x == 0) {\n _write('0');\n return;\n }\n int pos = 0;\n while (x != 0) {\n tmp[pos++] = char((x % 10) | 48);\n x /= 10;\n }\n rep(i, 0, pos) wtbuf[wtRight + i] = tmp[pos - 1 - i];\n wtRight += pos;\n }\n inline void _write(double x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n _write(s);\n }\n template <typename T> inline void _write(const vector<T> &v) {\n rep(i, 0, v.size()) {\n if (i)\n _write(' ');\n _write(v[i]);\n }\n }\n\n public:\n FastIO() {}\n ~FastIO() { flush(); }\n inline void read() {}\n template <typename Head, typename... Tail>\n inline void read(Head &head, Tail &...tail) {\n assert(_read(head));\n read(tail...);\n }\n template <bool ln = true, bool space = false> inline void write() {\n if (ln)\n _write('\\n');\n }\n template <bool ln = true, bool space = false, typename Head,\n typename... Tail>\n inline void write(const Head &head, const Tail &...tail) {\n if (space)\n _write(' ');\n _write(head);\n write<ln, true>(tail...);\n }\n inline void flush() {\n fwrite(wtbuf, 1, wtRight, stdout);\n wtRight = 0;\n }\n};\n\n#define debug(var) do{std::cerr << #var << \" : \";view(var); cerr << '\\n';}while(0)\ntemplate<typename T> void view(T e){std::cerr << e;}\ntemplate<typename T,typename U> void view(pair<T,U> e){\n std::cerr << '{';\n view(e.first);\n std::cerr << ',';\n view(e.second);\n std::cerr << '}';\n}\ntemplate <typename T, template<class> class C> void view(const C<T>& v){\n std::cerr << \"[\";\n for(const auto& e : v){ view(e); cerr << ','; }\n std::cerr << \"]\";\n}\n\n\n/**\n * @brief Fast IO\n */\n// #line 3 \"sol.cpp\"\n\nstruct GraphicMatroid{\n using P=pair<int,int>;\n int n;\n vector<P> E;\n vector<vector<P>> G;\n vector<int> allow;\n GraphicMatroid(){}\n GraphicMatroid(int n,vector<P>& E):n(n),E(E),G(n){\n rep(i,0,SZ(E)){\n auto [u,v]=E[i];\n G[u].push_back({v,i});\n G[v].push_back({u,i});\n }\n }\n void set(vector<int>& I){\n allow=I;\n }\n vector<int> circuit(int e){\n auto [x,y]=E[e];\n vector<int> ret;\n ret.push_back(e);\n auto dfs=[&](auto& dfs,int v,int p)->bool{\n if(v==y)return true;\n for(auto& [to,i]:G[v])if(allow[i] and to!=p){\n ret.push_back(i);\n if(dfs(dfs,to,v))return true;\n ret.pop_back();\n }\n return false;\n };\n if(dfs(dfs,x,-1))return ret;\n else return {};\n }\n};\n\nstruct PartitionMatroid{\n vector<int> grp; // -1:not assign\n vector<int> lim;\n vector<vector<int>> cnt;\n PartitionMatroid(){}\n PartitionMatroid(vector<int>& grp,vector<int> lim):grp(grp),lim(lim){}\n void set(vector<int>& I){\n cnt.assign(SZ(lim),{});\n rep(i,0,SZ(grp))if(I[i]!=0 and grp[i]!=-1){\n cnt[grp[i]].push_back(i);\n }\n }\n vector<int> circuit(int e){\n if(grp[e]==-1)return {};\n if(SZ(cnt[grp[e]])+1>lim[grp[e]]){\n vector<int> ret=cnt[grp[e]];\n ret.push_back(e);\n return ret;\n }\n else return {};\n }\n};\n\ntemplate<typename M1,typename M2,typename Val>\npair<vector<Val>,vector<vector<int>>> MinimumMatroidIntersection(M1& m1,M2& m2,vector<Val>& ws){\n using P=pair<Val,int>;\n int n=SZ(ws);\n \n vector<Val> ret1;\n vector<vector<int>> ret2;\n \n Val profit=0;\n vector<int> I(n);\n ret1.push_back(profit);\n ret2.push_back(I);\n \n for(;;){\n vector G(n+2,vector<P>());\n int S=n,T=n+1;\n m1.set(I);\n m2.set(I);\n \n rep(e,0,n){\n if(I[e])continue;\n auto c1=m1.circuit(e);\n if(c1.empty())G[S].push_back({ws[e]*(n+1)+1,e});\n else for(auto& to:c1)if(e!=to){\n G[to].push_back({ws[e]*(n+1)+1,e});\n }\n auto c2=m2.circuit(e);\n if(c2.empty())G[e].push_back({Val(0),T});\n else for(auto& to:c2)if(e!=to){\n G[e].push_back({-ws[to]*(n+1)+1,to});\n }\n }\n \n vector<ll> dist(n+2,INF);\n dist[S]=0;\n vector<int> pre(n+2,-1);\n for(;;){\n bool upd=0;\n rep(v,0,n+2)for(auto& [cost,to]:G[v]){\n if(chmin(dist[to],dist[v]+cost)){\n pre[to]=v;\n upd=1;\n }\n }\n if(!upd)break;\n }\n \n if(dist[T]==INF)break;\n int cur=T;\n while(cur!=S){\n cur=pre[cur];\n if(cur!=S){\n I[cur]^=1;\n if(I[cur])profit+=ws[cur];\n else profit-=ws[cur];\n }\n }\n ret1.push_back(profit);\n ret2.push_back(I);\n \n // debug(profit);\n // debug(I);\n }\n return {ret1,ret2};\n}\n\n\nFastIO io;\nint main(){\n for(;;){\n int n,m,k;\n io.read(n,m,k);\n if(n==0)break;\n using P=pair<int,int>;\n vector<P> E;\n vector<ll> ws(m);\n vector<int> grp(m),lim={k,n-1-k};\n rep(i,0,m){\n int u,v,w;\n char c;\n io.read(u,v,w,c);\n u--; v--;\n E.push_back({u,v});\n ws[i]=w;\n if(c=='A')grp[i]=0;\n else grp[i]=1;\n }\n \n GraphicMatroid m1(n,E);\n PartitionMatroid m2(grp,lim);\n \n auto [ret,tmp]=MinimumMatroidIntersection(m1,m2,ws);\n if(SZ(ret)==n)io.write(ret.back());\n else io.write(-1);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 810, "memory_kb": 4904, "score_of_the_acc": -1.1893, "final_rank": 20 }, { "submission_id": "aoj_1605_8304590", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define per(i, n) for (int i = (n)-1; i >= 0; i--)\n#define rep2(i, l, r) for (int i = (l); i < (r); i++)\n#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)\n#define each(e, x) for (auto &e : x)\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define pb push_back\n#define eb emplace_back\n#define MM << ' ' <<\n#define TT template <typename T>\nusing ll = long long;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nconst int inf = (1 << 30) - 1;\n\nTT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }\nTT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }\n\nint main() {\n while (true) {\n int N, M, K;\n cin >> N >> M >> K;\n\n if (N == 0) break;\n\n vector<int> r(N, -1);\n\n auto root = [&](auto &&root, int v) -> int {\n if (r[v] == -1) return v;\n return r[v] = root(root, r[v]);\n };\n\n auto unite = [&](int u, int v) {\n u = root(root, u), v = root(root, v);\n if (u == v) return false;\n r[v] = u;\n return true;\n };\n\n vector<tuple<int, int, int, char>> es;\n\n rep(i, M) {\n int u, v, w;\n char c;\n cin >> u >> v >> w >> c;\n u--, v--;\n es.eb(w, u, v, c);\n }\n\n sort(all(es));\n\n int l1 = 0, l2 = 0;\n int ans = 0;\n\n vector<bool> used(M, false);\n\n rep(i, M) {\n auto [w, u, v, c] = es[i];\n if (unite(u, v)) {\n ans += w;\n if (c == 'A') l1++;\n if (c == 'B') l2++;\n used[i] = true;\n }\n }\n\n // cout << l1 MM l2 << endl;\n\n if (l1 + l2 != N - 1) {\n cout << \"-1\\n\";\n continue;\n }\n\n // cout << N MM M MM K << endl;\n\n while (l1 != K) {\n vector<vector<tuple<int, int, int, int>>> li(N);\n rep(i, M) {\n if (!used[i]) continue;\n auto [w, u, v, c] = es[i];\n li[u].eb(v, c, w, i);\n li[v].eb(u, c, w, i);\n }\n\n vector<int> d(N, inf);\n vector<tuple<int, int, int, int>> pre(N);\n queue<int> que;\n que.emplace(0);\n d[0] = 0;\n while (!empty(que)) {\n int i = que.front();\n que.pop();\n for (auto [j, c, w, id] : li[i]) {\n if (chmin(d[j], d[i] + 1)) {\n pre[j] = make_tuple(i, c, w, id);\n que.emplace(j);\n }\n }\n }\n\n char x = (l1 < K ? 'A' : 'B');\n int mi = inf;\n pii P{-1, -1};\n\n rep(i, M) {\n auto [w, u, v, c] = es[i];\n if (used[i] || c != x) continue;\n int ma = -inf, ma_id = -1;\n while (u != v) {\n if (d[u] < d[v]) swap(u, v);\n auto [p, c, w, id] = pre[u];\n if (c != x && chmax(ma, w)) {\n ma_id = id; //\n }\n u = p;\n }\n if (chmin(mi, w - ma)) {\n P = {i, ma_id}; //\n }\n }\n\n if (mi == inf) {\n ans = -1;\n break;\n }\n\n ans += mi;\n used[P.first] = true;\n used[P.second] = false;\n if (l1 < K) l1++;\n if (l1 > K) l1--;\n }\n\n cout << ans << '\\n';\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3396, "score_of_the_acc": -0.0653, "final_rank": 4 }, { "submission_id": "aoj_1605_8023450", "code_snippet": "#include<iostream>\n#include<vector>\n#include<algorithm>\n#include<cassert>\nusing namespace std;\nstruct UF{\n\tvector<int>pr;\n\tUF(int n):pr(n,-1){}\n\tint find(int u)\n\t{\n\t\tif(pr[u]>=0)return pr[u]=find(pr[u]);\n\t\treturn u;\n\t}\n\tbool merge(int u,int v)\n\t{\n\t\tu=find(u);\n\t\tv=find(v);\n\t\tif(u==v)return false;\n\t\tif(-pr[u]<-pr[v])swap(u,v);\n\t\tpr[u]+=pr[v];\n\t\tpr[v]=u;\n\t\treturn true;\n\t}\n};\nstruct edge{\n\tint u,v,w;\n\tbool operator<(const edge&rhs)const{return w<rhs.w;}\n};\nostream&operator<<(ostream&os,const edge&e)\n{\n\treturn os<<\"(\"<<e.u+1<<\", \"<<e.v+1<<\", \"<<e.w<<\")\";\n}\nint N,M,K;\nvector<edge>A,B;\nvector<edge>f(vector<edge>E)\n{\n\tUF uf(N);\n\tvector<edge>ret;\n\tsort(E.begin(),E.end());\n\tfor(edge e:E)\n\t{\n\t\tif(uf.merge(e.u,e.v))ret.push_back(e);\n\t}\n\treturn ret;\n}\nint main()\n{\n\twhile(cin>>N>>M>>K,N)\n\t{\n\t\tA.clear();\n\t\tB.clear();\n\t\tfor(int i=0;i<M;i++)\n\t\t{\n\t\t\tedge E;\n\t\t\tcin>>E.u>>E.v>>E.w;\n\t\t\tE.u--,E.v--;\n\t\t\tchar l;\n\t\t\tcin>>l;\n\t\t\tif(l=='A')A.push_back(E);\n\t\t\telse B.push_back(E);\n\t\t}\n\t\tA=f(A);\n\t\tB=f(B);\n\t\tvector<edge>useA,restA;\n\t\t{\n\t\t\tUF uf(N);\n\t\t\tfor(edge e:B)uf.merge(e.u,e.v);\n\t\t\tfor(edge e:A)\n\t\t\t{\n\t\t\t\tif(uf.merge(e.u,e.v))\n\t\t\t\t{\n\t\t\t\t\tuseA.push_back(e);\n\t\t\t\t}\n\t\t\t\telse restA.push_back(e);\n\t\t\t}\n\t\t}\n\t\tint cnt=K-(int)useA.size();\n\t\tif(cnt<0||cnt>restA.size())\n\t\t{\n\t\t\tcout<<-1<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int t=0;t<cnt;t++)\n\t\t{\n\t\t\tUF uf(N);\n\t\t\tfor(edge e:useA)assert(uf.merge(e.u,e.v));\n\t\t\t/*\n\t\t\tint ALL=0;\n\t\t\tUF ALLuf=uf;\n\t\t\tfor(edge e:B)if(ALLuf.merge(e.u,e.v))ALL+=e.w;\n\t\t\t*/\n\t\t\tint MN=1e9,MNi;\n\t\t\tfor(int i=0;i<restA.size();i++)\n\t\t\t{\n\t\t\t\tUF NOWuf=uf;\n\t\t\t\tassert(NOWuf.merge(restA[i].u,restA[i].v));\n\t\t\t\tint now=restA[i].w;\n\t\t\t\tfor(edge e:B)if(NOWuf.merge(e.u,e.v))now+=e.w;\n\t\t\t\tif(MN>now)\n\t\t\t\t{\n\t\t\t\t\tMN=now;\n\t\t\t\t\tMNi=i;\n\t\t\t\t}\n\t\t\t}\n\t\t\tuseA.push_back(restA[MNi]);\n\t\t\trestA.erase(restA.begin()+MNi);\n\t\t}\n\t\tassert(useA.size()==K);\n\t\tUF uf(N);\n\t\tfor(edge e:useA)assert(uf.merge(e.u,e.v));\n\t\tfor(edge e:B)if(uf.merge(e.u,e.v))\n\t\t{\n\t\t\tuseA.push_back(e);\n\t\t\t//cout<<e<<\" from B\\n\";\n\t\t}\n\t\tif(useA.size()<N-1)\n\t\t{\n\t\t\tcout<<-1<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\tassert(useA.size()==N-1);\n\t\tint ans=0;\n\t\tfor(edge e:useA)ans+=e.w;\n\t\tcout<<ans<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3484, "score_of_the_acc": -0.1115, "final_rank": 7 }, { "submission_id": "aoj_1605_7967277", "code_snippet": "#include <bits/stdc++.h>\n\nnamespace luz {\n\n using usize = std::size_t;\n using isize = std::ptrdiff_t;\n\n struct rep {\n struct itr {\n usize i;\n constexpr itr(const usize i) noexcept: i(i) {}\n void operator++() noexcept {\n ++i;\n }\n constexpr usize operator*() const noexcept {\n return i;\n }\n constexpr bool operator!=(\n const itr x) const noexcept {\n return i != x.i;\n }\n };\n const itr f, l;\n constexpr rep(const usize f, const usize l) noexcept\n : f(std::min(f, l)),\n l(l) {}\n constexpr auto begin() const noexcept {\n return f;\n }\n constexpr auto end() const noexcept {\n return l;\n }\n };\n\n}\n\nnamespace luz {\n class DisjointSetUnion {\n usize n_;\n\n std::vector< isize > vals_;\n\n void bound_check(usize v) const {\n assert(v < n_);\n }\n\n usize impl_leader(usize v) {\n if (vals_[v] < 0) return v;\n return vals_[v] = leader(vals_[v]);\n }\n\n public:\n DisjointSetUnion() = default;\n explicit DisjointSetUnion(usize n) : n_(n), vals_(n, -1) {}\n\n usize size() const {\n return n_;\n }\n\n usize leader(usize v) {\n bound_check(v);\n return impl_leader(v);\n }\n\n bool same(usize u, usize v) {\n bound_check(u), bound_check(v);\n return impl_leader(u) == impl_leader(v);\n }\n\n usize merge(usize u, usize v) {\n bound_check(u);\n bound_check(v);\n\n isize x = impl_leader(u);\n isize y = impl_leader(v);\n if (x == y) return x;\n if (-vals_[x] < -vals_[y]) std::swap(x, y);\n vals_[x] += vals_[y];\n vals_[y] = x;\n return x;\n }\n\n usize group_size(usize v) {\n bound_check(v);\n return -vals_[impl_leader(v)];\n }\n };\n\n}\n\nvoid solve(int n, int m, int k) {\n struct plan {\n int u, v, w, is_a;\n };\n\n luz::DisjointSetUnion dsu(n);\n std::vector< plan > plans(m);\n for (auto &p: plans) {\n char c;\n std::cin >> p.u >> p.v >> p.w >> c;\n p.is_a = (c == 'A');\n p.u--;\n p.v--;\n\n dsu.merge(p.u, p.v);\n }\n\n if (dsu.group_size(0) != n) {\n std::cout << -1 << std::endl;\n return;\n }\n\n // dw \\in [-100, 100]\n auto calc = [&](int dw) {\n for (auto &p: plans) {\n if (not p.is_a) continue;\n p.w += dw;\n }\n\n auto aplans = plans;\n auto bplans = plans;\n\n for (auto &p: plans) {\n if (not p.is_a) continue;\n p.w -= dw;\n }\n\n auto cmp_a = [&](const plan &a, const plan &b) {\n if (a.w == b.w) {\n return a.is_a < b.is_a;\n }\n return a.w < b.w;\n };\n auto cmp_b = [&](const plan &a, const plan &b) {\n if (a.w == b.w) {\n return a.is_a > b.is_a;\n }\n return a.w < b.w;\n };\n\n std::sort(aplans.begin(), aplans.end(), cmp_a);\n std::sort(bplans.begin(), bplans.end(), cmp_b);\n\n luz::DisjointSetUnion af(n), bf(n);\n int cnt_a = 0, cnt_b = 0;\n int mst_a = 0, mst_b = 0;\n \n for (auto &p: aplans) {\n int u = p.u;\n int v = p.v;\n if (af.same(u, v)) continue;\n if (p.is_a) cnt_a++;\n af.merge(u, v);\n mst_a += p.w;\n }\n for (auto &p: bplans) {\n int u = p.u;\n int v = p.v;\n if (bf.same(u, v)) continue;\n if (p.is_a) cnt_b++;\n bf.merge(u, v);\n mst_b += p.w;\n }\n\n assert(mst_a == mst_b);\n\n return std::make_tuple(cnt_a, cnt_b, mst_a);\n };\n\n int ok = 100, ng = -101;\n while (ok - ng > 1) {\n // std::cerr << \"(\" << ng << \", \" << ok << \"]\" << std::endl;\n int dw = (ok + ng) / 2;\n\n // A 側に下駄をはかせて A を使える下界、上界\n auto [lb, up, mst] = calc(dw);\n // std::cerr << lb << \" \" << up << \" \" << mst << std::endl;\n\n if (k < lb) {\n ng = dw;\n } else {\n ok = dw;\n }\n }\n\n auto [lb, up, mst] = calc(ok);\n // std::cerr << ok << std::endl;\n // std::cerr << lb << \" \" << up << \" \" << mst << std::endl;\n\n if (lb <= k and k <= up) {\n std::cout << mst - k * ok << std::endl;\n } else {\n std::cout << -1 << std::endl;\n }\n}\n\nint main() {\n int n, m, k;\n while (std::cin >> n >> m >> k, n) {\n solve(n, m, k);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3432, "score_of_the_acc": -0.0439, "final_rank": 2 }, { "submission_id": "aoj_1605_7965410", "code_snippet": "#include <bits/stdc++.h>\n\nnamespace luz {\n\n using usize = std::size_t;\n using isize = std::ptrdiff_t;\n\n struct rep {\n struct itr {\n usize i;\n constexpr itr(const usize i) noexcept: i(i) {}\n void operator++() noexcept {\n ++i;\n }\n constexpr usize operator*() const noexcept {\n return i;\n }\n constexpr bool operator!=(\n const itr x) const noexcept {\n return i != x.i;\n }\n };\n const itr f, l;\n constexpr rep(const usize f, const usize l) noexcept\n : f(std::min(f, l)),\n l(l) {}\n constexpr auto begin() const noexcept {\n return f;\n }\n constexpr auto end() const noexcept {\n return l;\n }\n };\n\n}\n\nnamespace luz {\n class DisjointSetUnion {\n usize n_;\n\n std::vector< isize > vals_;\n\n void bound_check(usize v) const {\n assert(v < n_);\n }\n\n usize impl_leader(usize v) {\n if (vals_[v] < 0) return v;\n return vals_[v] = leader(vals_[v]);\n }\n\n public:\n DisjointSetUnion() = default;\n explicit DisjointSetUnion(usize n) : n_(n), vals_(n, -1) {}\n\n usize size() const {\n return n_;\n }\n\n usize leader(usize v) {\n bound_check(v);\n return impl_leader(v);\n }\n\n bool same(usize u, usize v) {\n bound_check(u), bound_check(v);\n return impl_leader(u) == impl_leader(v);\n }\n\n usize merge(usize u, usize v) {\n bound_check(u);\n bound_check(v);\n\n isize x = impl_leader(u);\n isize y = impl_leader(v);\n if (x == y) return x;\n if (-vals_[x] < -vals_[y]) std::swap(x, y);\n vals_[x] += vals_[y];\n vals_[y] = x;\n return x;\n }\n\n usize group_size(usize v) {\n bound_check(v);\n return -vals_[impl_leader(v)];\n }\n };\n\n}\n\nvoid solve(int n, int m, int k) {\n struct plan {\n int u, v, w, is_a;\n };\n\n luz::DisjointSetUnion dsu(n);\n std::vector< plan > plans(m);\n for (auto &p: plans) {\n char c;\n std::cin >> p.u >> p.v >> p.w >> c;\n p.is_a = (c == 'A');\n p.u--;\n p.v--;\n\n dsu.merge(p.u, p.v);\n }\n\n if (dsu.group_size(0) != n) {\n std::cout << -1 << std::endl;\n return;\n }\n\n // dw \\in [-100, 100]\n auto calc = [&](int dw) {\n for (auto &p: plans) {\n if (not p.is_a) continue;\n p.w += dw;\n }\n\n auto aplans = plans;\n auto bplans = plans;\n\n for (auto &p: plans) {\n if (not p.is_a) continue;\n p.w -= dw;\n }\n\n auto cmp_a = [&](const plan &a, const plan &b) {\n if (a.w == b.w) {\n return a.is_a < b.is_a;\n }\n return a.w < b.w;\n };\n auto cmp_b = [&](const plan &a, const plan &b) {\n if (a.w == b.w) {\n return a.is_a > b.is_a;\n }\n return a.w < b.w;\n };\n\n std::sort(aplans.begin(), aplans.end(), cmp_a);\n std::sort(bplans.begin(), bplans.end(), cmp_b);\n\n luz::DisjointSetUnion af(n), bf(n);\n int cnt_a = 0, cnt_b = 0;\n int mst_a = 0, mst_b = 0;\n \n for (auto &p: aplans) {\n int u = p.u;\n int v = p.v;\n if (af.same(u, v)) continue;\n if (p.is_a) cnt_a++;\n af.merge(u, v);\n mst_a += p.w;\n }\n for (auto &p: bplans) {\n int u = p.u;\n int v = p.v;\n if (bf.same(u, v)) continue;\n if (p.is_a) cnt_b++;\n bf.merge(u, v);\n mst_b += p.w;\n }\n\n assert(mst_a == mst_b);\n\n return std::make_tuple(cnt_a, cnt_b, mst_a);\n };\n\n int ok = 100, ng = -101;\n while (ok - ng > 1) {\n // std::cerr << \"(\" << ng << \", \" << ok << \"]\" << std::endl;\n int dw = (ok + ng) / 2;\n\n // A 側に下駄をはかせて A を使える下界、上界\n auto [lb, up, mst] = calc(dw);\n // std::cerr << lb << \" \" << up << \" \" << mst << std::endl;\n\n if (k < lb) {\n ng = dw;\n } else {\n ok = dw;\n }\n }\n\n auto [lb, up, mst] = calc(ok);\n // std::cerr << ok << std::endl;\n // std::cerr << lb << \" \" << up << \" \" << mst << std::endl;\n\n if (lb <= k and k <= up) {\n std::cout << mst - k * ok << std::endl;\n } else {\n std::cout << -1 << std::endl;\n }\n}\n\nint main() {\n int n, m, k;\n while (std::cin >> n >> m >> k, n) {\n solve(n, m, k);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3420, "score_of_the_acc": -0.0427, "final_rank": 1 }, { "submission_id": "aoj_1605_7698513", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = (int)s; i < (int)n; i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= s; i--)\n#define all(v) v.begin(), v.end()\n\nusing ll = long long;\n\nstruct dsu {\n std::vector<int> par;\n\n public:\n dsu(int n = 0) : par(n, -1) {}\n\n int leader(int x) {\n if (par[x] < 0)\n return x;\n else\n return par[x] = leader(par[x]);\n }\n\n bool same(int x, int y) { return leader(x) == leader(y); }\n\n bool merge(int x, int y) {\n x = leader(x);\n y = leader(y);\n if(x == y) return false;\n if (par[x] > par[y]) std::swap(x, y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n\n int size(int x) {\n x = leader(x);\n return -par[x];\n }\n};\n\nint main() {\n int n, m, k;\n while (std::cin >> n >> m >> k, !(n == 0 && m == 0 && k == 0)) {\n std::vector edges(m, std::vector<int>(4));\n rep(i, 0, m) {\n std::cin >> edges[i][0] >> edges[i][1];\n edges[i][0]--;\n edges[i][1]--;\n std::cin >> edges[i][2];\n char c;\n std::cin >> c;\n edges[i][3] = c - 'A';\n }\n auto check = [&](std::vector<std::vector<int>> a, std::vector<std::vector<int>> b, int cnt) -> bool {\n dsu uf(n);\n for(auto edge: b) {\n uf.merge(edge[0], edge[1]);\n }\n int t = 0;\n for(auto edge: a) {\n int u = edge[0];\n int v = edge[1];\n if(!uf.same(u, v)) {\n t++;\n uf.merge(u, v);\n }\n }\n return t <= cnt && uf.size(0) == n;\n };\n {\n std::vector<std::vector<int>> edges_a, edges_b;\n for(auto edge: edges) {\n if(edge[3] == 0) edges_a.emplace_back(edge);\n else edges_b.emplace_back(edge);\n }\n if(!(check(edges_a, edges_b, k) && check(edges_b, edges_a, n-1-k))) {\n std::cout << \"-1\\n\";\n continue;\n }\n }\n ll ok = -200, ng = 200;\n auto f = [&](ll c) -> std::pair<ll, int> {\n std::vector ps(m, std::vector<int>(4));\n rep(i, 0, m) {\n ps[i] = edges[i];\n if (ps[i][3] == 0) ps[i][2] += c;\n }\n std::sort(all(ps), [](auto lhs, auto rhs) {\n return (lhs[2] != rhs[2]) ? lhs[2] < rhs[2] : lhs[3] < rhs[3];\n });\n dsu uf(n);\n int cnt = 0;\n ll sum = 0;\n for (auto edge : ps) {\n int u = edge[0];\n int v = edge[1];\n int w = edge[2];\n if (uf.same(u, v)) continue;\n uf.merge(u, v);\n sum += w;\n if (edge[3] == 0) cnt++;\n }\n return {sum, cnt};\n };\n while (std::abs(ok - ng) > 1) {\n ll mid = (ok + ng) / 2;\n auto [sum, cnt] = f(mid);\n if (cnt >= k) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n auto [sum, cnt] = f(ok);\n sum -= k * ok;\n std::cout << sum << '\\n';\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3544, "score_of_the_acc": -0.0674, "final_rank": 5 }, { "submission_id": "aoj_1605_7210924", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,s,t) for (int i = (int)(s); i < (int)(t); ++i)\n#define all(x) (x).begin(), (x).end()\n\nstruct UnionFind {\n vector<int> data;\n\n UnionFind(int n) : data(n, -1) {}\n\n int find(int x) {\n if (data[x] < 0) return x;\n return data[x] = find(data[x]);\n }\n\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (data[x] > data[y]) swap(x, y);\n data[x] += data[y];\n data[y] = x;\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n};\n\ntemplate <typename T>\nvoid print(set<T>& st) {\n for (auto& i : st) cout << i << \" \";\n cout << endl;\n}\n\ntemplate <typename T, typename U>\nvoid print(pair<T, U>& p) {\n cout << \"(\" << p.first << \",\" << p.second << endl;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while (true) {\n int n, m, k;\n cin >> n >> m >> k;\n if (n == 0) break;\n vector<tuple<int, int, int, char>> edges(m);\n set<int> unused, used;\n vector<pair<int, int>> init(m);\n rep(i,0,m) {\n int u, v, w;\n char l;\n cin >> u >> v >> w >> l;\n --u, --v;\n edges[i] = {u, v, w, l};\n init[i] = {w, i};\n }\n sort(all(init));\n\n UnionFind uf(n);\n int cnt = 0;\n for (auto [_, i] : init) {\n auto [u, v, w, l] = edges[i];\n if (l == 'A' && cnt < k && !uf.same(u, v)) {\n uf.unite(u, v);\n used.insert(i);\n ++cnt;\n } else {\n unused.insert(i);\n }\n }\n\n if (cnt < k) {\n cout << -1 << \"\\n\";\n continue;\n }\n\n auto flip = [&](int i) {\n if (used.count(i)) {\n used.erase(i);\n unused.insert(i);\n } else {\n unused.erase(i);\n used.insert(i);\n }\n };\n\n // print(used);\n\n bool ok = true;\n rep(_,0,n-k-1) {\n // print(used);\n UnionFind uf(n);\n vector<vector<pair<int, int>>> G(n);\n for (int i : used) {\n auto [u, v, w, l] = edges[i];\n uf.unite(u, v);\n G[u].push_back({v, i});\n G[v].push_back({u, i});\n }\n\n // find min source\n pair<int, int> minA(1e9, -1), minB(1e9, -1);\n for (int i : unused) {\n auto [u, v, w, l] = edges[i];\n if (!uf.same(u, v)) {\n if (l == 'A') minA = min(minA, {w, i});\n else minB = min(minB, {w, i});\n }\n }\n\n // find min AB pair\n vector<int> par(n, -1), pare(n, -1), dep(n, -1);\n\n auto dfs = [&](auto& dfs, int v) -> void {\n for (auto [c, i] : G[v]) {\n if (c == par[v]) continue;\n dep[c] = dep[v] + 1;\n par[c] = v;\n pare[c] = i;\n dfs(dfs, c);\n }\n };\n\n rep(i,0,n) {\n if (dep[i] != -1) continue;\n dep[i] = 0;\n dfs(dfs, i);\n }\n\n tuple<int, int, int> minAB(1e9, -1, -1);\n for (int i : unused) {\n auto [u, v, w, l] = edges[i];\n if (l == 'A' || !uf.same(u, v)) continue;\n pair<int, int> maxA(-1e9, -1);\n while (u != v) {\n // cout << u << \" \" << v << endl;\n if (dep[u] < dep[v]) swap(u, v);\n int e = pare[u];\n if (get<3>(edges[e]) == 'A') maxA = max(maxA, {get<2>(edges[e]), e});\n u = par[u];\n }\n auto [x, j] = maxA;\n if (j == -1) continue;\n minAB = min(minAB, {w-x, i, j});\n }\n\n // print(minA);\n // print(minB);\n\n if (minB.first <= get<0>(minAB)+minA.first) {\n auto [w, i] = minB;\n if (i == -1) {\n ok = false;\n break;\n }\n flip(i);\n } else {\n auto [w, i, j] = minAB;\n auto [x, k] = minA;\n if (i == -1 || j == -1 || k == -1) {\n ok = false;\n break;\n }\n flip(i);\n flip(j);\n flip(k);\n }\n }\n // print(used);\n if (ok) {\n ll ans = 0;\n for (int i : used) {\n ans += get<2>(edges[i]);\n }\n cout << ans << \"\\n\";\n } else {\n cout << -1 << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3536, "score_of_the_acc": -0.0917, "final_rank": 6 }, { "submission_id": "aoj_1605_6775879", "code_snippet": "#include <algorithm>\n#include <cassert>\n#include <cstddef>\n#include <cstdint>\n#include <iomanip>\n#include <iostream>\n#include <utility>\n#include <vector>\n\nnamespace luz {\n\n using isize = std::ptrdiff_t;\n using usize = std::size_t;\n\n using i32 = std::int32_t;\n using i64 = std::int64_t;\n using u32 = std::uint32_t;\n using u64 = std::uint64_t;\n \n} // namespace luz\n\nnamespace luz {\n\n struct rep {\n struct itr {\n usize i;\n constexpr itr(const usize i) noexcept : i(i) {}\n void operator++() noexcept { ++i; }\n constexpr usize operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }\n };\n const itr f, l;\n constexpr rep(const usize f, const usize l) noexcept\n : f(std::min(f, l)), l(l) {}\n constexpr auto begin() const noexcept { return f; }\n constexpr auto end() const noexcept { return l; }\n };\n\n struct rrep {\n struct itr {\n usize i;\n constexpr itr(const usize i) noexcept : i(i) {}\n void operator++() noexcept { --i; }\n constexpr usize operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }\n };\n const itr f, l;\n constexpr rrep(const usize f, const usize l) noexcept\n : f(l - 1), l(std::min(f, l) - 1) {}\n constexpr auto begin() const noexcept { return f; }\n constexpr auto end() const noexcept { return l; }\n };\n\n} // namespace luz\n\nnamespace luz {\n\n void set_fast_ios() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n} // namespace luz\n\nnamespace luz {\n\n void io_set(usize precision) {\n std::cout << std::fixed << std::setprecision(precision);\n std::cerr << std::fixed << std::setprecision(precision);\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template< typename T = i64 > T input() {\n T tmp;\n std::cin >> tmp;\n return tmp;\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template< typename T >\n std::ostream &operator<<(std::ostream &os, std::vector< T > &vs) {\n for (usize i: rep(0, vs.size())) {\n os << vs[i] << (i + 1 != vs.size() ? \" \" : \"\");\n }\n return os;\n }\n\n template< typename T >\n std::istream &operator>>(std::istream &is, std::vector< T > &vs) {\n for (T &v: vs) {\n is >> v;\n }\n return is;\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template< typename T1, typename T2 >\n std::ostream &operator<<(std::ostream &os, std::pair < T1, T2 > &p) {\n os << \"(\" << p.first << \", \" << p.second << \")\";\n return os;\n }\n\n template< typename T1, typename T2 >\n std::istream &operator>>(std::istream &is, std::pair< T1, T2 > &p) {\n is >> p.first >> p.second;\n return is;\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template <typename T>\n std::vector<T> make_vector(usize a, T b) {\n return std::vector<T>(a, b);\n }\n\n template <typename... Ts>\n auto make_vector(usize a, Ts... ts) {\n return std::vector<decltype(make_vector(ts...))>(a, make_vector(ts...));\n }\n\n} // namespace luz\n\nnamespace luz {\n\n template <typename T1, typename T2>\n inline bool chmax(T1 &a, T2 b) {\n return a < b and (a = b, true);\n }\n\n template <typename T1, typename T2>\n inline bool chmin(T1 &a, T2 b) {\n return a > b and (a = b, true);\n }\n\n} // namespace luz\n\n#include <map>\n\nstruct UnionFind {\n std::vector< int > data;\n\n UnionFind() = default;\n\n explicit UnionFind(size_t sz) : data(sz, -1) {}\n\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if(x == y) return false;\n if(data[x] > data[y]) std::swap(x, y);\n data[x] += data[y];\n data[y] = x;\n return true;\n }\n\n int find(int k) {\n if(data[k] < 0) return (k);\n return data[k] = find(data[k]);\n }\n\n int size(int k) {\n return -data[find(k)];\n }\n\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n};\n\nnamespace luz {\n\n void solve(usize n, usize m, usize k) {\n std::vector< usize > us(m), vs(m);\n std::vector< int > ws(m);\n std::vector< bool > ls(m);\n\n for (usize i: rep(0, m)) {\n us[i] = input() - 1;\n vs[i] = input() - 1;\n ws[i] = input();\n ls[i] = (input<char>() == 'A');\n }\n\n { // reachablility\n UnionFind uf(n);\n for (usize i: rep(0, m)) {\n uf.unite(us[i], vs[i]);\n }\n for (usize v: rep(0, n)) {\n if (uf.same(0, v)) continue;\n std::cout << -1 << std::endl;\n return;\n }\n }\n\n auto check = [&](int lambda) {\n // < <cost, is_b >, idx>\n std::vector< std::pair< std::pair<int, bool>, usize > > es;\n\n // < <cost, is_a >, idx>\n std::vector< std::pair< std::pair<int, bool>, usize > > res;\n\n for (usize i: rep(0, m)) {\n int cost = ws[i] + (ls[i] ? lambda : 0);\n es.emplace_back(std::make_pair(cost, not ls[i]), i);\n res.emplace_back(std::make_pair(cost, ls[i]), i);\n }\n\n std::sort( es.begin(), es.end());\n std::sort(res.begin(), res.end());\n\n usize cnt_a0 = 0, cnt_a1 = 0;\n int mst = 0;\n\n {\n UnionFind dsu(n);\n for (auto [_, i]: es) {\n if (dsu.same(us[i], vs[i])) continue;\n dsu.unite(us[i], vs[i]);\n cnt_a0 += ls[i];\n mst += ws[i] + (ls[i] ? lambda : 0);\n }\n }\n {\n UnionFind dsu(n);\n for (auto [_, i]: res) {\n if (dsu.same(us[i], vs[i])) continue;\n dsu.unite(us[i], vs[i]);\n cnt_a1 += ls[i];\n }\n }\n\n return std::make_pair(std::make_pair(cnt_a0, cnt_a1), mst);\n };\n\n constexpr int inf = 1001001001;\n int ans = inf;\n for (int lambda = -105; lambda <= 105; lambda++) {\n auto [cnt, mst] = check(lambda);\n if (cnt.second <= k and k <= cnt.first) {\n chmin(ans, mst - k * lambda);\n }\n }\n\n if (ans == inf) ans = -1;\n std::cout << ans << std::endl;\n }\n\n void main_() {\n usize n, m, k;\n\n while (std::cin >> n >> m >> k, n) {\n solve(n, m, k);\n }\n }\n\n} // namespace luz\n\nint main() {\n luz::set_fast_ios();\n luz::io_set(15);\n\n luz::main_();\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3344, "score_of_the_acc": -0.1977, "final_rank": 9 }, { "submission_id": "aoj_1605_6607688", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\n//constexpr long long int MOD = 1000000007;\nconstexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-8;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nclass UnionFind {\n\tvector<int>parent;\n\tvector<int>rank;\npublic:\n\tUnionFind(int num) {\n\t\tnum++;\n\t\tparent.resize(num);\n\t\trank.resize(num);\n\t\tfor (int i = 0; i < num; i++) {\n\t\t\tparent[i] = i;\n\t\t\trank[i] = 0;\n\t\t}\n\t}\n\tint Find(int node) {\n\t\tif (parent[node] == node)return node;\n\t\telse return parent[node] = Find(parent[node]);\n\t}\n\tvoid Unite(int u, int v) {\n\t\tu = Find(u);\n\t\tv = Find(v);\n\t\tif (u == v)return;\n\t\tif (rank[u] < rank[v])parent[u] = v;\n\t\telse {\n\t\t\tparent[v] = u;\n\t\t\tif (rank[u] == rank[v])rank[u]++;\n\t\t}\n\t}\n\tbool Check_Same(int u, int v) {\n\t\treturn Find(u) == Find(v);\n\t}\n};\n\nstruct Edge {\n\tint cost;\n\tint u, v;\n\tEdge(int cost, int u, int v) :cost(cost), u(u), v(v) {\n\n\t}\n\tbool operator<(const Edge& edge)const {\n\t\treturn cost < edge.cost;\n\t}\n};\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N >> M >> K, N) {\n\t\tvector<int>au;\n\t\tvector<int>av;\n\t\tvector<int>aw;\n\t\tvector<int>bu;\n\t\tvector<int>bv;\n\t\tvector<int>bw;\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tint a, b, c;\n\t\t\tcin >> a >> b >> c;\n\t\t\ta--, b--;\n\t\t\tchar ab;\n\t\t\tcin >> ab;\n\t\t\tif (ab == 'A') {\n\t\t\t\tau.push_back(a);\n\t\t\t\tav.push_back(b);\n\t\t\t\taw.push_back(c);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tbu.push_back(a);\n\t\t\t\tbv.push_back(b);\n\t\t\t\tbw.push_back(c);\n\t\t\t}\n\t\t}\n\t\tlong long ans = LLONG_MAX;\n\t\tfor (int i = -200; i <= 200; i++) {\n\t\t\tint score = 0;\n\t\t\tint comp = N;\n\t\t\t{\n\t\t\t\tUnionFind uf(N);\n\t\t\t\tvector<Edge>edge;\n\t\t\t\tfor (int j = 0; j < au.size(); j++) {\n\t\t\t\t\tedge.push_back(Edge(aw[j] + i, au[j], av[j]));\n\t\t\t\t}\n\t\t\t\tfor (int j = 0; j < bu.size(); j++) {\n\t\t\t\t\tedge.push_back(Edge(bw[j], -bu[j], -bv[j]));\n\t\t\t\t}\n\t\t\t\treverse(edge.begin(), edge.end());\n\t\t\t\tstable_sort(edge.begin(), edge.end());\n\t\t\t\tint num = 0;\n\t\t\t\tfor (auto j : edge) {\n\t\t\t\t\tif (uf.Check_Same(abs(j.u), abs(j.v)))continue;\n\t\t\t\t\tcomp--;\n\t\t\t\t\tuf.Unite(abs(j.u), abs(j.v));\n\t\t\t\t\tscore += j.cost;\n\t\t\t\t\tnum += j.u + j.v > 0;\n\t\t\t\t}\n\t\t\t\tL = num;\n\t\t\t}\n\t\t\t{\n\t\t\t\tUnionFind uf(N);\n\t\t\t\tvector<Edge>edge;\n\t\t\t\tfor (int j = 0; j < au.size(); j++) {\n\t\t\t\t\tedge.push_back(Edge(aw[j] + i, au[j], av[j]));\n\t\t\t\t}\n\t\t\t\tfor (int j = 0; j < bu.size(); j++) {\n\t\t\t\t\tedge.push_back(Edge(bw[j], -bu[j], -bv[j]));\n\t\t\t\t}\n\t\t\t\tstable_sort(edge.begin(), edge.end());\n\t\t\t\tint num = 0;\n\t\t\t\tfor (auto j : edge) {\n\t\t\t\t\tif (uf.Check_Same(abs(j.u), abs(j.v)))continue;\n\t\t\t\t\tuf.Unite(abs(j.u), abs(j.v));\n\t\t\t\t\tnum += j.u + j.v > 0;\n\t\t\t\t}\n\t\t\t\tR = num;\n\t\t\t}\n\t\t\tif (L > R) {\n\t\t\t\tswap(L, R);\n\t\t\t}\n\t\t\tif (comp==1&&L <= K && K <= R) {\n\t\t\t\tans = min(ans, score - i * K);\n\t\t\t}\n\t\t}\n\t\tif (ans == LLONG_MAX)ans = -1;\n\t\tcout << ans << endl;\n\t}\n}", "accuracy": 1, "time_ms": 200, "memory_kb": 3448, "score_of_the_acc": -0.283, "final_rank": 11 }, { "submission_id": "aoj_1605_6555258", "code_snippet": "#line 1 \"combinatorial_opt/test/matroid_intersection.aoj1605.test.cpp\"\n#define PROBLEM \"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1605\"\n#line 2 \"graph/shortest_path.hpp\"\n#include <algorithm>\n#include <cassert>\n#include <deque>\n#include <fstream>\n#include <functional>\n#include <limits>\n#include <queue>\n#include <string>\n#include <utility>\n#include <vector>\n\n// CUT begin\ntemplate <typename T, T INF = std::numeric_limits<T>::max() / 2, int INVALID = -1>\nstruct ShortestPath {\n int V, E;\n bool single_positive_weight;\n T wmin, wmax;\n std::vector<std::vector<std::pair<int, T>>> to;\n\n ShortestPath(int V = 0) : V(V), E(0), single_positive_weight(true), wmin(0), wmax(0), to(V) {}\n void add_edge(int s, int t, T w) {\n assert(0 <= s and s < V);\n assert(0 <= t and t < V);\n to[s].emplace_back(t, w);\n E++;\n if (w > 0 and wmax > 0 and wmax != w) single_positive_weight = false;\n wmin = std::min(wmin, w);\n wmax = std::max(wmax, w);\n }\n\n std::vector<T> dist;\n std::vector<int> prev;\n\n // Dijkstra algorithm\n // Complexity: O(E log E)\n using Pque = std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>,\n std::greater<std::pair<T, int>>>;\n template <class Heap = Pque> void Dijkstra(int s) {\n assert(0 <= s and s < V);\n dist.assign(V, INF);\n dist[s] = 0;\n prev.assign(V, INVALID);\n Heap pq;\n pq.emplace(0, s);\n while (!pq.empty()) {\n T d;\n int v;\n std::tie(d, v) = pq.top();\n pq.pop();\n if (dist[v] < d) continue;\n for (auto nx : to[v]) {\n T dnx = d + nx.second;\n if (dist[nx.first] > dnx) {\n dist[nx.first] = dnx, prev[nx.first] = v;\n pq.emplace(dnx, nx.first);\n }\n }\n }\n }\n\n // Dijkstra algorithm, O(V^2 + E)\n void DijkstraVquad(int s) {\n assert(0 <= s and s < V);\n dist.assign(V, INF);\n dist[s] = 0;\n prev.assign(V, INVALID);\n std::vector<char> fixed(V, false);\n while (true) {\n int r = INVALID;\n T dr = INF;\n for (int i = 0; i < V; i++) {\n if (!fixed[i] and dist[i] < dr) r = i, dr = dist[i];\n }\n if (r == INVALID) break;\n fixed[r] = true;\n int nxt;\n T dx;\n for (auto p : to[r]) {\n std::tie(nxt, dx) = p;\n if (dist[nxt] > dist[r] + dx) dist[nxt] = dist[r] + dx, prev[nxt] = r;\n }\n }\n }\n\n // Bellman-Ford algorithm\n // Complexity: O(VE)\n bool BellmanFord(int s, int nb_loop) {\n assert(0 <= s and s < V);\n dist.assign(V, INF), prev.assign(V, INVALID);\n dist[s] = 0;\n for (int l = 0; l < nb_loop; l++) {\n bool upd = false;\n for (int v = 0; v < V; v++) {\n if (dist[v] == INF) continue;\n for (auto nx : to[v]) {\n T dnx = dist[v] + nx.second;\n if (dist[nx.first] > dnx) dist[nx.first] = dnx, prev[nx.first] = v, upd = true;\n }\n }\n if (!upd) return true;\n }\n return false;\n }\n\n // Bellman-ford algorithm using queue (deque)\n // Complexity: O(VE)\n // Requirement: no negative loop\n void SPFA(int s) {\n assert(0 <= s and s < V);\n dist.assign(V, INF);\n prev.assign(V, INVALID);\n std::deque<int> q;\n std::vector<char> in_queue(V);\n dist[s] = 0;\n q.push_back(s), in_queue[s] = 1;\n while (!q.empty()) {\n int now = q.front();\n q.pop_front(), in_queue[now] = 0;\n for (auto nx : to[now]) {\n T dnx = dist[now] + nx.second;\n int nxt = nx.first;\n if (dist[nxt] > dnx) {\n dist[nxt] = dnx;\n if (!in_queue[nxt]) {\n if (q.size() and dnx < dist[q.front()]) { // Small label first optimization\n q.push_front(nxt);\n } else {\n q.push_back(nxt);\n }\n prev[nxt] = now, in_queue[nxt] = 1;\n }\n }\n }\n }\n }\n\n void ZeroOneBFS(int s) {\n assert(0 <= s and s < V);\n dist.assign(V, INF), prev.assign(V, INVALID);\n dist[s] = 0;\n std::deque<int> que;\n que.push_back(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop_front();\n for (auto nx : to[v]) {\n T dnx = dist[v] + nx.second;\n if (dist[nx.first] > dnx) {\n dist[nx.first] = dnx, prev[nx.first] = v;\n if (nx.second) {\n que.push_back(nx.first);\n } else {\n que.push_front(nx.first);\n }\n }\n }\n }\n }\n\n bool dag_solver(int s) {\n assert(0 <= s and s < V);\n std::vector<int> indeg(V, 0);\n std::queue<int> que;\n que.push(s);\n while (que.size()) {\n int now = que.front();\n que.pop();\n for (auto nx : to[now]) {\n indeg[nx.first]++;\n if (indeg[nx.first] == 1) que.push(nx.first);\n }\n }\n dist.assign(V, INF), prev.assign(V, INVALID);\n dist[s] = 0;\n que.push(s);\n while (que.size()) {\n int now = que.front();\n que.pop();\n for (auto nx : to[now]) {\n indeg[nx.first]--;\n if (dist[nx.first] > dist[now] + nx.second)\n dist[nx.first] = dist[now] + nx.second, prev[nx.first] = now;\n if (indeg[nx.first] == 0) que.push(nx.first);\n }\n }\n return *max_element(indeg.begin(), indeg.end()) == 0;\n }\n\n // Retrieve a sequence of vertex ids that represents shortest path [s, ..., goal]\n // If not reachable to goal, return {}\n std::vector<int> retrieve_path(int goal) const {\n assert(int(prev.size()) == V);\n assert(0 <= goal and goal < V);\n if (dist[goal] == INF) return {};\n std::vector<int> ret{goal};\n while (prev[goal] != INVALID) {\n goal = prev[goal];\n ret.push_back(goal);\n }\n std::reverse(ret.begin(), ret.end());\n return ret;\n }\n\n void solve(int s) {\n if (wmin >= 0) {\n if (single_positive_weight) {\n ZeroOneBFS(s);\n } else {\n if ((long long)V * V < (E << 4)) {\n DijkstraVquad(s);\n } else {\n Dijkstra(s);\n }\n }\n } else {\n BellmanFord(s, V);\n }\n }\n\n // Warshall-Floyd algorithm\n // Complexity: O(E + V^3)\n std::vector<std::vector<T>> dist2d;\n void WarshallFloyd() {\n dist2d.assign(V, std::vector<T>(V, INF));\n for (int i = 0; i < V; i++) {\n dist2d[i][i] = 0;\n for (auto p : to[i]) dist2d[i][p.first] = std::min(dist2d[i][p.first], p.second);\n }\n for (int k = 0; k < V; k++) {\n for (int i = 0; i < V; i++) {\n if (dist2d[i][k] == INF) continue;\n for (int j = 0; j < V; j++) {\n if (dist2d[k][j] == INF) continue;\n dist2d[i][j] = std::min(dist2d[i][j], dist2d[i][k] + dist2d[k][j]);\n }\n }\n }\n }\n\n void dump_graphviz(std::string filename = \"shortest_path\") const {\n std::ofstream ss(filename + \".DOT\");\n ss << \"digraph{\\n\";\n for (int i = 0; i < V; i++) {\n for (const auto &e : to[i])\n ss << i << \"->\" << e.first << \"[label=\" << e.second << \"];\\n\";\n }\n ss << \"}\\n\";\n ss.close();\n return;\n }\n};\n#line 5 \"combinatorial_opt/matroid_intersection.hpp\"\n\n// CUT begin\n// (Min weight) matroid intersection solver\n// Algorithm based on http://dopal.cs.uec.ac.jp/okamotoy/lect/2015/matroid/\n// Complexity: O(CE^2 + E^3) (C : circuit query, non-weighted)\ntemplate <class M1, class M2, class T = int>\nstd::vector<bool> MatroidIntersection(M1 matroid1, M2 matroid2, std::vector<T> weights = {}) {\n using State = std::vector<bool>;\n using Element = int;\n assert(matroid1.size() == matroid2.size());\n const int M = matroid1.size();\n\n for (auto &x : weights) x *= M + 1;\n if (weights.empty()) weights.assign(M, 0);\n\n const Element gs = M, gt = M + 1;\n State I(M);\n\n while (true) {\n ShortestPath<T> sssp(M + 2);\n matroid1.set(I);\n matroid2.set(I);\n for (int e = 0; e < M; e++) {\n if (I[e]) continue;\n auto c1 = matroid1.circuit(e), c2 = matroid2.circuit(e);\n if (c1.empty()) sssp.add_edge(e, gt, 0);\n for (Element f : c1) {\n if (f != e) sssp.add_edge(e, f, -weights[f] + 1);\n }\n if (c2.empty()) sssp.add_edge(gs, e, weights[e] + 1);\n for (Element f : c2) {\n if (f != e) sssp.add_edge(f, e, weights[e] + 1);\n }\n }\n sssp.solve(gs);\n auto aug_path = sssp.retrieve_path(gt);\n if (aug_path.empty()) break;\n for (auto e : aug_path) {\n if (e != gs and e != gt) I[e] = !I[e];\n }\n }\n return I;\n}\n#line 5 \"combinatorial_opt/matroids/graphic_matroid.hpp\"\n\n// GraphicMatroid: subgraph of undirected graphs, without loops\nclass GraphicMatroid {\n using Vertex = int;\n using Element = int;\n int M;\n int V; // # of vertices of graph\n std::vector<std::vector<std::pair<Vertex, Element>>> to;\n std::vector<std::pair<Vertex, Vertex>> edges;\n std::vector<Element> backtrack;\n std::vector<Vertex> vprev;\n std::vector<int> depth, root;\n\npublic:\n GraphicMatroid(int V, const std::vector<std::pair<Vertex, Vertex>> &edges_)\n : M(edges_.size()), V(V), to(V), edges(edges_) {\n for (int e = 0; e < int(edges_.size()); e++) {\n int u = edges_[e].first, v = edges_[e].second;\n assert(0 <= u and u < V);\n assert(0 <= v and v < V);\n if (u != v) {\n to[u].emplace_back(v, e);\n to[v].emplace_back(u, e);\n }\n }\n }\n int size() const { return M; }\n\n std::vector<Vertex> que;\n template <class State> void set(State I) {\n assert(int(I.size()) == M);\n backtrack.assign(V, -1);\n vprev.assign(V, -1);\n depth.assign(V, -1);\n root.assign(V, -1);\n que.resize(V);\n int qb = 0, qe = 0;\n for (Vertex i = 0; i < V; i++) {\n if (backtrack[i] >= 0) continue;\n que[qb = 0] = i, qe = 1, depth[i] = 0;\n while (qb < qe) {\n Vertex now = que[qb++];\n root[now] = i;\n for (auto nxt : to[now]) {\n if (depth[nxt.first] < 0 and I[nxt.second]) {\n backtrack[nxt.first] = nxt.second;\n vprev[nxt.first] = now;\n depth[nxt.first] = depth[now] + 1;\n que[qe++] = nxt.first;\n }\n }\n }\n }\n }\n\n std::vector<Element> circuit(const Element e) const {\n assert(0 <= e and e < M);\n Vertex s = edges[e].first, t = edges[e].second;\n if (root[s] != root[t]) return {};\n std::vector<Element> ret{e};\n auto step = [&](Vertex &i) { ret.push_back(backtrack[i]), i = vprev[i]; };\n int ddepth = depth[s] - depth[t];\n for (; ddepth > 0; --ddepth) step(s);\n for (; ddepth < 0; ++ddepth) step(t);\n while (s != t) step(s), step(t);\n return ret;\n }\n};\n#line 4 \"combinatorial_opt/matroids/partition_matroid.hpp\"\n\n// Partition matroid (partitional matroid) : direct sum of uniform matroids\nclass PartitionMatroid {\n using Element = int;\n int M;\n std::vector<std::vector<Element>> parts;\n std::vector<int> belong;\n std::vector<int> R;\n std::vector<int> cnt;\n std::vector<std::vector<Element>> circuits;\n\npublic:\n // parts: partition of [0, 1, ..., M - 1]\n // R: only R[i] elements from parts[i] can be chosen for each i.\n PartitionMatroid(int M, const std::vector<std::vector<int>> &parts_, const std::vector<int> &R_)\n : M(M), parts(parts_), belong(M, -1), R(R_) {\n assert(parts.size() == R.size());\n for (int i = 0; i < int(parts.size()); i++) {\n for (Element e : parts[i]) belong[e] = i;\n }\n for (Element e = 0; e < M; e++) {\n // assert(belong[e] != -1);\n if (belong[e] == -1) {\n belong[e] = parts.size();\n parts.push_back({e});\n R.push_back(1);\n }\n }\n }\n int size() const { return M; }\n\n template <class State> void set(const State &I) {\n cnt = R;\n for (int e = 0; e < M; e++) {\n if (I[e]) cnt[belong[e]]--;\n }\n circuits.assign(cnt.size(), {});\n for (int e = 0; e < M; e++) {\n if (I[e] and cnt[belong[e]] == 0) circuits[belong[e]].push_back(e);\n }\n }\n\n std::vector<Element> circuit(const Element e) const {\n assert(0 <= e and e < M);\n int p = belong[e];\n if (cnt[p] == 0) {\n auto ret = circuits[p];\n ret.push_back(e);\n return ret;\n }\n return {};\n }\n};\n#line 5 \"combinatorial_opt/test/matroid_intersection.aoj1605.test.cpp\"\n#include <iostream>\n#include <numeric>\n#line 9 \"combinatorial_opt/test/matroid_intersection.aoj1605.test.cpp\"\nusing namespace std;\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n while (true) {\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0) break;\n vector<vector<int>> partition(2);\n vector<int> R{K, N - 1 - K};\n vector<pair<int, int>> edges;\n vector<int> weights;\n for (int e = 0; e < M; ++e) {\n int u, v, w;\n char l;\n cin >> u >> v >> w >> l;\n --u, --v;\n partition[l == 'B'].push_back(e);\n edges.emplace_back(u, v);\n weights.push_back(w);\n }\n PartitionMatroid M1(edges.size(), partition, R);\n GraphicMatroid M2(N, edges);\n vector<bool> ret = MatroidIntersection(M1, M2, weights);\n int ne = accumulate(ret.begin(), ret.end(), 0);\n if (ne < N - 1) {\n cout << \"-1\\n\";\n } else {\n int sum = 0;\n for (int e = 0; e < M; ++e) sum += ret[e] * weights[e];\n cout << sum << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 4072, "score_of_the_acc": -0.5446, "final_rank": 16 }, { "submission_id": "aoj_1605_6340217", "code_snippet": "#line 2 \"library/bits/stdc++.h\"\n\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <typeindex>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#endif\n#line 2 \"Source.cpp\"\nusing namespace std;\n\n#define REP(a, b) for (long long a = 0; a < b; ++a)\n#define ll long long\n#define ld long double\n#define ALL(x) begin(x), end(x)\n\n#line 1 \"library/atcoder/dsu.hpp\"\n\n\n\n#line 5 \"library/atcoder/dsu.hpp\"\n#include <cassert>\n#line 7 \"library/atcoder/dsu.hpp\"\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n\n\n#line 10 \"Source.cpp\"\n\n#define int long long\nvoid solve()\n{\n int n, m, k;\n cin >> n >> m >> k;\n if (n == 0)\n exit(0);\n vector<tuple<int, int, int, int>> edges;\n REP(i, m)\n {\n int a, b, c;\n string d;\n cin >> a >> b >> c >> d;\n a--;\n b--;\n edges.push_back({c, a, b, d[0] - 'A'});\n }\n sort(ALL(edges));\n atcoder::dsu graph(n);\n vector<tuple<int, int, int, int>> current_edges;\n for (int i = 0; i < m; ++i)\n {\n if (graph.same(get<1>(edges[i]), get<2>(edges[i])))\n continue;\n current_edges.push_back(edges[i]);\n graph.merge(get<1>(edges[i]), get<2>(edges[i]));\n k -= 1 - get<3>(edges[i]);\n edges.erase(edges.begin() + i);\n i--;\n }\n\n REP(i, n)\n {\n if (!graph.same(i, 0))\n {\n cout << -1 << endl;\n return;\n }\n }\n while (k != 0)\n {\n tuple<int, int, int> target = make_tuple(1e18, 0, 0);\n REP(i, current_edges.size())\n {\n if (k < 0)\n {\n if (get<3>(current_edges[i]) == 1)\n continue;\n }\n else\n {\n if (get<3>(current_edges[i]) == 0)\n continue;\n }\n\n atcoder::dsu graph(n);\n REP(q, current_edges.size())\n {\n if (i == q)\n continue;\n graph.merge(get<1>(current_edges[q]), get<2>(current_edges[q]));\n }\n\n REP(q, edges.size())\n {\n if (get<3>(edges[q]) == get<3>(current_edges[i]))\n continue;\n if (graph.same(get<1>(edges[q]), get<2>(edges[q])))\n continue;\n tuple<int, int, int> now;\n get<0>(now) = get<0>(edges[q]) - get<0>(current_edges[i]);\n get<1>(now) = i;\n get<2>(now) = q;\n if (get<0>(now) < get<0>(target))\n {\n target = now;\n }\n break;\n }\n }\n if (get<0>(target) == 1e18)\n {\n cout << -1 << endl;\n return;\n }\n swap(current_edges[get<1>(target)], edges[get<2>(target)]);\n if (k > 0)\n k--;\n else\n k++;\n sort(ALL(current_edges));\n sort(ALL(edges));\n }\n int ans = 0;\n for (auto x : current_edges)\n {\n ans += get<0>(x);\n }\n cout << ans << endl;\n}\n\n#undef int\nint main()\n{\n iostream::sync_with_stdio(false);\n cout << fixed << setprecision(100);\n while (true)\n solve();\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3412, "score_of_the_acc": -0.2044, "final_rank": 10 }, { "submission_id": "aoj_1605_6049288", "code_snippet": "#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <cmath>\n#include <complex>\n#include <deque>\n#include <forward_list>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\nusing namespace std;\nusing lint = long long;\nusing pint = pair<int, int>;\nusing plint = pair<lint, lint>;\nstruct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;\n#define ALL(x) (x).begin(), (x).end()\n#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)\n#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)\n#define REP(i, n) FOR(i,0,n)\n#define IREP(i, n) IFOR(i,0,n)\ntemplate <typename T, typename V>\nvoid ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }\ntemplate <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }\ntemplate <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }\ntemplate <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }\nint floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }\ntemplate <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }\ntemplate <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }\ntemplate <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }\ntemplate <typename T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }\ntemplate <typename T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }\ntemplate <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }\n#if __cplusplus >= 201703L\ntemplate <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }\ntemplate <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }\n#endif\ntemplate <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << \"deq[\"; for (auto v : vec) os << v << ','; os << ']'; return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }\ntemplate <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }\ntemplate <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << \"=>\" << v.second << ','; os << '}'; return os; }\ntemplate <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << \"=>\" << v.second << ','; os << '}'; return os; }\n#ifdef HITONANODE_LOCAL\nconst string COLOR_RESET = \"\\033[0m\", BRIGHT_GREEN = \"\\033[1;32m\", BRIGHT_RED = \"\\033[1;31m\", BRIGHT_CYAN = \"\\033[1;36m\", NORMAL_CROSSED = \"\\033[0;9;37m\", RED_BACKGROUND = \"\\033[1;41m\", NORMAL_FAINT = \"\\033[0;2m\";\n#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << endl\n#define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << \" = \" << (x) << NORMAL_FAINT << \" (L\" << __LINE__ << \") \" << __FILE__ << COLOR_RESET << endl : cerr)\n#else\n#define dbg(x) (x)\n#define dbgif(cond, x) 0\n#endif\n\ntemplate <typename T, T INF = std::numeric_limits<T>::max() / 2, int INVALID = -1> struct ShortestPath {\n int V, E;\n bool single_positive_weight;\n T wmin, wmax;\n std::vector<std::vector<std::pair<int, T>>> to;\n\n ShortestPath(int V = 0) : V(V), E(0), single_positive_weight(true), wmin(0), wmax(0), to(V) {}\n void add_edge(int s, int t, T w) {\n assert(0 <= s and s < V);\n assert(0 <= t and t < V);\n to[s].emplace_back(t, w);\n E++;\n if (w > 0 and wmax > 0 and wmax != w) single_positive_weight = false;\n wmin = std::min(wmin, w);\n wmax = std::max(wmax, w);\n }\n\n std::vector<T> dist;\n std::vector<int> prev;\n\n // Dijkstra algorithm\n // Complexity: O(E log E)\n using Pque = std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>>;\n template <class Heap = Pque> void Dijkstra(int s) {\n assert(0 <= s and s < V);\n dist.assign(V, INF);\n dist[s] = 0;\n prev.assign(V, INVALID);\n Heap pq;\n pq.emplace(0, s);\n while (!pq.empty()) {\n T d;\n int v;\n std::tie(d, v) = pq.top();\n pq.pop();\n if (dist[v] < d) continue;\n for (auto nx : to[v]) {\n T dnx = d + nx.second;\n if (dist[nx.first] > dnx) {\n dist[nx.first] = dnx, prev[nx.first] = v;\n pq.emplace(dnx, nx.first);\n }\n }\n }\n }\n\n // Dijkstra algorithm, O(V^2 + E)\n void DijkstraVquad(int s) {\n assert(0 <= s and s < V);\n dist.assign(V, INF);\n dist[s] = 0;\n prev.assign(V, INVALID);\n std::vector<char> fixed(V, false);\n while (true) {\n int r = INVALID;\n T dr = INF;\n for (int i = 0; i < V; i++) {\n if (!fixed[i] and dist[i] < dr) r = i, dr = dist[i];\n }\n if (r == INVALID) break;\n fixed[r] = true;\n int nxt;\n T dx;\n for (auto p : to[r]) {\n std::tie(nxt, dx) = p;\n if (dist[nxt] > dist[r] + dx) dist[nxt] = dist[r] + dx, prev[nxt] = r;\n }\n }\n }\n\n // Bellman-Ford algorithm\n // Complexity: O(VE)\n bool BellmanFord(int s, int nb_loop) {\n assert(0 <= s and s < V);\n dist.assign(V, INF), prev.assign(V, INVALID);\n dist[s] = 0;\n for (int l = 0; l < nb_loop; l++) {\n bool upd = false;\n for (int v = 0; v < V; v++) {\n if (dist[v] == INF) continue;\n for (auto nx : to[v]) {\n T dnx = dist[v] + nx.second;\n if (dist[nx.first] > dnx) dist[nx.first] = dnx, prev[nx.first] = v, upd = true;\n }\n }\n if (!upd) return true;\n }\n return false;\n }\n\n // Bellman-ford algorithm using queue (deque)\n // Complexity: O(VE)\n // Requirement: no negative loop\n void SPFA(int s) {\n assert(0 <= s and s < V);\n dist.assign(V, INF);\n prev.assign(V, INVALID);\n std::deque<int> q;\n std::vector<char> in_queue(V);\n dist[s] = 0;\n q.push_back(s), in_queue[s] = 1;\n while (!q.empty()) {\n int now = q.front();\n q.pop_front(), in_queue[now] = 0;\n for (auto nx : to[now]) {\n T dnx = dist[now] + nx.second;\n int nxt = nx.first;\n if (dist[nxt] > dnx) {\n dist[nxt] = dnx;\n if (!in_queue[nxt]) {\n if (q.size() and dnx < dist[q.front()]) { // Small label first optimization\n q.push_front(nxt);\n } else {\n q.push_back(nxt);\n }\n prev[nxt] = now, in_queue[nxt] = 1;\n }\n }\n }\n }\n }\n\n void ZeroOneBFS(int s) {\n assert(0 <= s and s < V);\n dist.assign(V, INF), prev.assign(V, INVALID);\n dist[s] = 0;\n std::deque<int> que;\n que.push_back(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop_front();\n for (auto nx : to[v]) {\n T dnx = dist[v] + nx.second;\n if (dist[nx.first] > dnx) {\n dist[nx.first] = dnx, prev[nx.first] = v;\n if (nx.second) {\n que.push_back(nx.first);\n } else {\n que.push_front(nx.first);\n }\n }\n }\n }\n }\n\n bool dag_solver(int s) {\n assert(0 <= s and s < V);\n std::vector<int> indeg(V, 0);\n std::queue<int> que;\n que.push(s);\n while (que.size()) {\n int now = que.front();\n que.pop();\n for (auto nx : to[now]) {\n indeg[nx.first]++;\n if (indeg[nx.first] == 1) que.push(nx.first);\n }\n }\n dist.assign(V, INF), prev.assign(V, INVALID);\n dist[s] = 0;\n que.push(s);\n while (que.size()) {\n int now = que.front();\n que.pop();\n for (auto nx : to[now]) {\n indeg[nx.first]--;\n if (dist[nx.first] > dist[now] + nx.second)\n dist[nx.first] = dist[now] + nx.second, prev[nx.first] = now;\n if (indeg[nx.first] == 0) que.push(nx.first);\n }\n }\n return *max_element(indeg.begin(), indeg.end()) == 0;\n }\n\n // Retrieve a sequence of vertex ids that represents shortest path [s, ..., goal]\n // If not reachable to goal, return {}\n std::vector<int> retrieve_path(int goal) const {\n assert(int(prev.size()) == V);\n assert(0 <= goal and goal < V);\n if (dist[goal] == INF) return {};\n std::vector<int> ret{goal};\n while (prev[goal] != INVALID) {\n goal = prev[goal];\n ret.push_back(goal);\n }\n std::reverse(ret.begin(), ret.end());\n return ret;\n }\n\n void solve(int s) {\n if (wmin >= 0) {\n if (single_positive_weight) {\n ZeroOneBFS(s);\n } else {\n if ((long long)V * V < (E << 4)) {\n DijkstraVquad(s);\n } else {\n Dijkstra(s);\n }\n }\n } else {\n BellmanFord(s, V);\n }\n }\n\n // Warshall-Floyd algorithm\n // Complexity: O(E + V^3)\n std::vector<std::vector<T>> dist2d;\n void WarshallFloyd() {\n dist2d.assign(V, std::vector<T>(V, INF));\n for (int i = 0; i < V; i++) {\n dist2d[i][i] = 0;\n for (auto p : to[i]) dist2d[i][p.first] = std::min(dist2d[i][p.first], p.second);\n }\n for (int k = 0; k < V; k++) {\n for (int i = 0; i < V; i++) {\n if (dist2d[i][k] == INF) continue;\n for (int j = 0; j < V; j++) {\n if (dist2d[k][j] == INF) continue;\n dist2d[i][j] = std::min(dist2d[i][j], dist2d[i][k] + dist2d[k][j]);\n }\n }\n }\n }\n\n void dump_graphviz(std::string filename = \"shortest_path\") const {\n std::ofstream ss(filename + \".DOT\");\n ss << \"digraph{\\n\";\n for (int i = 0; i < V; i++) {\n for (const auto &e : to[i]) ss << i << \"->\" << e.first << \"[label=\" << e.second << \"];\\n\";\n }\n ss << \"}\\n\";\n ss.close();\n return;\n }\n};\n\n\n// Partition matroid (partitional matroid) : direct sum of uniform matroids\nclass PartitionMatroid {\n using Element = int;\n int M;\n std::vector<std::vector<Element>> parts;\n std::vector<int> belong;\n std::vector<int> R;\n std::vector<int> cnt;\n std::vector<std::vector<Element>> circuits;\n\npublic:\n // parts: partition of [0, 1, ..., M - 1]\n // R: only R[i] elements from parts[i] can be chosen for each i.\n PartitionMatroid(int M, const std::vector<std::vector<int>> &parts_, const std::vector<int> &R_)\n : M(M), parts(parts_), belong(M, -1), R(R_) {\n assert(parts.size() == R.size());\n for (int i = 0; i < int(parts.size()); i++) {\n for (Element e : parts[i]) belong[e] = i;\n }\n for (Element e = 0; e < M; e++) {\n // assert(belong[e] != -1);\n if (belong[e] == -1) {\n belong[e] = parts.size();\n parts.push_back({e});\n R.push_back(1);\n }\n }\n }\n int size() const { return M; }\n\n template <class State> void set(const State &I) {\n cnt = R;\n for (int e = 0; e < M; e++) {\n if (I[e]) cnt[belong[e]]--;\n }\n circuits.assign(cnt.size(), {});\n for (int e = 0; e < M; e++) {\n if (I[e] and cnt[belong[e]] == 0) circuits[belong[e]].push_back(e);\n }\n }\n\n std::vector<Element> circuit(const Element e) const {\n assert(0 <= e and e < M);\n int p = belong[e];\n if (cnt[p] == 0) {\n auto ret = circuits[p];\n ret.push_back(e);\n return ret;\n }\n return {};\n }\n};\n\n// GraphicMatroid: subgraph of undirected graphs, without loops\nclass GraphicMatroid {\n using Vertex = int;\n using Element = int;\n int M;\n int V; // # of vertices of graph\n std::vector<std::vector<std::pair<Vertex, Element>>> to;\n std::vector<std::pair<Vertex, Vertex>> edges;\n std::vector<Element> backtrack;\n std::vector<Vertex> vprev;\n std::vector<int> depth, root;\n\npublic:\n GraphicMatroid(int V, const std::vector<std::pair<Vertex, Vertex>> &edges_)\n : M(edges_.size()), V(V), to(V), edges(edges_) {\n for (int e = 0; e < int(edges_.size()); e++) {\n int u = edges_[e].first, v = edges_[e].second;\n assert(0 <= u and u < V);\n assert(0 <= v and v < V);\n if (u != v) {\n to[u].emplace_back(v, e);\n to[v].emplace_back(u, e);\n }\n }\n }\n int size() const { return M; }\n\n std::vector<Vertex> que;\n template <class State> void set(State I) {\n assert(int(I.size()) == M);\n backtrack.assign(V, -1);\n vprev.assign(V, -1);\n depth.assign(V, -1);\n root.assign(V, -1);\n que.resize(V);\n int qb = 0, qe = 0;\n for (Vertex i = 0; i < V; i++) {\n if (backtrack[i] >= 0) continue;\n que[qb = 0] = i, qe = 1, depth[i] = 0;\n while (qb < qe) {\n Vertex now = que[qb++];\n root[now] = i;\n for (auto nxt : to[now]) {\n if (depth[nxt.first] < 0 and I[nxt.second]) {\n backtrack[nxt.first] = nxt.second;\n vprev[nxt.first] = now;\n depth[nxt.first] = depth[now] + 1;\n que[qe++] = nxt.first;\n }\n }\n }\n }\n }\n\n std::vector<Element> circuit(const Element e) const {\n assert(0 <= e and e < M);\n Vertex s = edges[e].first, t = edges[e].second;\n if (root[s] != root[t]) return {};\n std::vector<Element> ret{e};\n auto step = [&](Vertex &i) { ret.push_back(backtrack[i]), i = vprev[i]; };\n int ddepth = depth[s] - depth[t];\n for (; ddepth > 0; --ddepth) step(s);\n for (; ddepth < 0; ++ddepth) step(t);\n while (s != t) step(s), step(t);\n return ret;\n }\n};\n\n// (Min weight) matroid intersection solver\n// Algorithm based on http://dopal.cs.uec.ac.jp/okamotoy/lect/2015/matroid/\n// Complexity: O(CE^2 + E^3) (C : circuit query, non-weighted)\ntemplate <class M1, class M2, class T = int>\nstd::vector<bool> MatroidIntersection(M1 matroid1, M2 matroid2, std::vector<T> weights = {}) {\n using State = std::vector<bool>;\n using Element = int;\n assert(matroid1.size() == matroid2.size());\n const int M = matroid1.size();\n\n for (auto &x : weights) x *= M + 1;\n if (weights.empty()) weights.assign(M, 0);\n\n const Element gs = M, gt = M + 1;\n State I(M);\n\n while (true) {\n ShortestPath<T> sssp(M + 2);\n matroid1.set(I);\n matroid2.set(I);\n for (int e = 0; e < M; e++) {\n if (I[e]) continue;\n auto c1 = matroid1.circuit(e), c2 = matroid2.circuit(e);\n if (c1.empty()) sssp.add_edge(e, gt, 0);\n for (Element f : c1) {\n if (f != e) sssp.add_edge(e, f, -weights[f] + 1);\n }\n if (c2.empty()) sssp.add_edge(gs, e, weights[e] + 1);\n for (Element f : c2) {\n if (f != e) sssp.add_edge(f, e, weights[e] + 1);\n }\n }\n sssp.solve(gs);\n auto aug_path = sssp.retrieve_path(gt);\n if (aug_path.empty()) break;\n for (auto e : aug_path) {\n if (e != gs and e != gt) I[e] = !I[e];\n }\n }\n return I;\n}\n\n\n\nint main() {\n while (true) {\n int N, M, K;\n cin >> N >> M >> K;\n if (N == 0) break;\n vector<vector<int>> partition(2);\n vector<int> R{K, N - 1 - K};\n vector<pint> edges;\n vector<int> weights;\n REP(e, M) {\n int u, v, w;\n char l;\n cin >> u >> v >> w >> l;\n --u, --v;\n partition[l == 'B'].push_back(e);\n edges.emplace_back(u, v);\n weights.push_back(w);\n }\n PartitionMatroid M1(M, partition, R);\n GraphicMatroid M2(N, edges);\n vector<bool> ret = MatroidIntersection(M1, M2, weights);\n int ne = accumulate(ret.begin(), ret.end(), 0);\n if (ne < N - 1) {\n cout << \"-1\\n\";\n } else {\n int sum = 0;\n for (int e = 0; e < M; ++e) sum += ret[e] * weights[e];\n cout << sum << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 360, "memory_kb": 4144, "score_of_the_acc": -0.5517, "final_rank": 17 }, { "submission_id": "aoj_1605_6030082", "code_snippet": "#include<cstdio>\n#include<queue>\n#include<set>\n#include<algorithm>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef pair<ll, P> Q;\nconstexpr ll INF = 1000000000000000LL;\nconstexpr Q QINF = Q(INF+10, P(INF, INF));\nconstexpr Q QINF2 = Q(-INF, P(INF, INF));\n\nclass UnionFind {\n vector<ll> par, rank; // par > 0: number, par < 0: -par\npublic:\n UnionFind(ll n) : par(n, 1), rank(n, 0) {}\n ll getSize(ll x) {\n return par[find(x)];\n }\n ll find(ll x) {\n if (par[x] > 0) return x;\n return -(par[x] = -find(-par[x]));\n }\n void merge(ll x, ll y) {\n x = find(x);\n y = find(y);\n if (x == y) return;\n if (rank[x] < rank[y]) {\n par[y] += par[x];\n par[x] = -y;\n } else {\n par[x] += par[y];\n par[y] = -x;\n if (rank[x] == rank[y]) rank[x]++;\n }\n }\n bool isSame(ll x, ll y) {\n return find(x) == find(y);\n }\n};\n\n\nll n, m, k, u, v, w;\nchar l[2];\nset<P> e[200];\n\nQ dfs(ll now, ll to, ll par) {\n if (now == to) return QINF2;\n for (P p : e[now]) if (p.first != par) {\n Q q = dfs(p.first, to, now);\n if (q.first != INF + 10) {\n return max(q, Q(p.second, P(now, p.first)));\n }\n }\n return QINF;\n}\n\nint main() {\n while (scanf(\"%lld%lld%lld\", &n, &m, &k), n) {\n ll ans = 0;\n vector<Q> ea, eb;\n set<Q> rest;\n for (ll i = 0; i < n; i++) e[i].clear();\n for (ll i = 0; i < m; i++) {\n scanf(\"%lld%lld%lld%s\", &u, &v, &w, l);\n if (l[0] == 'A') ea.push_back(Q(w, P(u-1, v-1)));\n else eb.push_back(Q(w, P(u-1, v-1)));\n }\n sort(ea.begin(), ea.end());\n sort(eb.begin(), eb.end());\n UnionFind uf(n);\n for (Q q : eb) {\n u = q.second.first, v = q.second.second, w = q.first;\n if (uf.isSame(u, v)) continue;\n ans += w;\n uf.merge(u, v);\n e[u].insert(P(v, w));\n e[v].insert(P(u, w));\n }\n for (Q q : ea) {\n u = q.second.first, v = q.second.second, w = q.first;\n if (uf.isSame(u, v)) {\n rest.insert(Q(w, P(u, v)));\n continue;\n }\n ans += w;\n uf.merge(u, v);\n e[u].insert(P(v, -INF));\n e[v].insert(P(u, -INF));\n k--;\n }\n if (uf.getSize(0) != n or k < 0 or k > (ll)rest.size()) {\n printf(\"-1\\n\");\n continue;\n }\n for (ll i = 0; i < k; i++) {\n ll mn = INF;\n Q result, result2;\n for (Q q : rest) {\n u = q.second.first, v = q.second.second, w = q.first;\n Q t = dfs(u, v, -1);\n if (t.first == -INF) continue;\n ll tmp = w - t.first;\n if (mn > tmp) {\n mn = tmp;\n result = q;\n result2 = t;\n }\n }\n if (mn == INF) {\n ans = -1;\n break;\n }\n ans += mn;\n u = result.second.first, v = result.second.second, w = result.first;\n e[u].insert(P(v, -INF));\n e[v].insert(P(u, -INF));\n u = result2.second.first, v = result2.second.second, w = result2.first;\n e[u].erase(P(v, w));\n e[v].erase(P(u, w));\n rest.erase(result);\n }\n printf(\"%lld\\n\", ans);\n }\n}", "accuracy": 1, "time_ms": 160, "memory_kb": 2988, "score_of_the_acc": -0.1875, "final_rank": 8 } ]
aoj_1615_cpp
Gift Exchange Party A gift exchange party will be held at a school in TKB City. For every pair of students who are close friends, one gift must be given from one to the other at this party, but not the other way around. It is decided in advance the gift directions, that is, which student of each pair receives a gift. No other gift exchanges are made. If each pair randomly decided the gift direction, some might receive countless gifts, while some might receive only few or even none. You'd like to decide the gift directions for all the friend pairs that minimize the difference between the smallest and the largest numbers of gifts received by a student. Find the smallest and the largest numbers of gifts received when the difference between them is minimized. When there is more than one way to realize that, find the way that maximizes the smallest number of received gifts. Input The input consists of at most 10 datasets, each in the following format. n m u 1 v 1 ... u m v m n is the number of students, and m is the number of friendship relations (2 ≤ n ≤ 100, 1 ≤ m ≤ n ( n -1)/2). Students are denoted by integers between 1 and n , inclusive. The following m lines describe the friendship relations: for each i , student u i and v i are close friends ( u i < v i ). The same friendship relations do not appear more than once. The end of the input is indicated by a line containing two zeros. Output For each dataset, output a single line containing two integers l and h separated by a single space. Here, l and h are the smallest and the largest numbers, respectively, of gifts received by a student. Sample Input 3 3 1 2 2 3 1 3 4 3 1 2 1 3 1 4 4 6 1 2 1 3 1 4 2 3 3 4 2 4 0 0 Output for the Sample Input 1 1 0 1 1 2
[ { "submission_id": "aoj_1615_10945981", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) a.begin(), a.end()\n#define MS(m,v) memset(m,v,sizeof(m))\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<int> vi;\ntypedef vector<string> vs;\ntypedef pair<int, int> pii;\nconst int MOD = 1e9 + 7;\ntemplate<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }\ntemplate<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }\ntemplate<class T>\nistream& operator >> (istream& is, vector<T>& v)\n{\n\tfor (auto &i : v) is >> i;\n\treturn is;\n}\ntemplate<class T>\nostream& operator<<(ostream& os, vector<T>& v)\n{\n\tconst string delimiter = \"\\n\";\n\tREP(i, v.size())\n\t{\n\t\tos << v[i];\n\t\tif (i != v.size() - 1) os << delimiter;\n\t}\n\treturn os;\n}\n/*--------------------template--------------------*/\n\nconst int INF = 1e6;\n\ntypedef int Weight;\ntypedef int Flow;\nstruct Edge {\n\tint src, dest, rev;\n\tFlow cap;\n\tWeight cost;\n\tbool operator < (const Edge &rhs) const\n\t{\n\t\treturn cost > rhs.cost;\n\t}\n\tEdge(int s, int d) : src(s), dest(d) { ; }\n\tEdge(int s, int d, int c) : src(s), dest(d), cost(c) { ; }\n\tEdge(int s, int d, int r, Flow cp, Weight cst) : src(s), dest(d), rev(r), cap(cp), cost(cst) { ; }\n};\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> Graph;\ntypedef vector<Weight> Array;\ntypedef vector<Array> Matrix;\nvoid add_edge(Graph &g, int src, int dest, Flow cap)\n{\n\tg[src].push_back(Edge{ src, dest, (int)g[dest].size(), cap, 0 });\n\tg[dest].push_back(Edge{ dest, src, (int)g[src].size() - 1, 0, 0 });\n}\n\nvoid add_edge(Graph &g, int from, int to, Flow cap, Weight cost)\n{\n\tg[from].push_back(Edge(from, to, (int)g[to].size(), cap, cost));\n\tg[to].push_back(Edge(to, from, (int)g[from].size() - 1, 0, -cost));\n}\n\nFlow dfs(Graph &g, vector<bool> &used, int v, int t, Flow f)\n{\n\tif (v == t) return f;\n\tused[v] = true;\n\tfor (Edge& e : g[v])\n\t{\n\t\tif (!used[e.dest] && e.cap > 0)\n\t\t{\n\t\t\tFlow d = dfs(g, used, e.dest, t, min(f, e.cap));\n\t\t\tif (d > 0)\n\t\t\t{\n\t\t\t\te.cap -= d;\n\t\t\t\tg[e.dest][e.rev].cap += d;\n\t\t\t\treturn d;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}\n\nFlow ford_fulkerson(Graph &g, int s, int t)\n{\n\tFlow flow = 0;\n\tfor (;;)\n\t{\n\t\tvector<bool> used(g.size(), false);\n\t\tFlow f = dfs(g, used, s, t, INF);\n\t\tif (f == 0) return flow;\n\t\tflow += f;\n\t}\n}\n\n\nint main()\n{\n\tcin.sync_with_stdio(false); cout << fixed << setprecision(10);\n\tint n, m;\n\twhile (cin >> n >> m, n)\n\t{\n\t\tGraph g(n + m + 2);\n\t\tint src = n + m, sink = n + m + 1;\n\t\tREP(i, m) add_edge(g, src, i, 1);\n\t\tREP(i, m)\n\t\t{\n\t\t\tint a, b; cin >> a >> b;\n\t\t\ta--; b--;\n\t\t\tadd_edge(g, i, m + a, 1);\n\t\t\tadd_edge(g, i, m + b, 1);\n\t\t}\n\t\tint tmpcap = -1;\n\t\tint mn = -1, mx = -1;\n\t\twhile(1)\n\t\t{\n\t\t\ttmpcap++;\n\t\t\tGraph h = g;\n\t\t\tREP(i, n) add_edge(h, m + i, sink, tmpcap);\n\t\t\tint f = ford_fulkerson(h, src, sink);\n\t\t\tif (tmpcap * n == f)\n\t\t\t{\n\t\t\t\tmn = tmpcap;\n\t\t\t}\n\t\t\tif (m == f)\n\t\t\t{\n\t\t\t\tmx = tmpcap;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tcout << mn << \" \" << mx << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 4832, "score_of_the_acc": -0.8085, "final_rank": 16 }, { "submission_id": "aoj_1615_10684468", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 数値型\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int,int>;\nusing Pll = pair<ll, ll>;\nusing Pli = pair<ll, int>;\nusing Pil = pair<int, ll>;\n\n// vector関連\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\ntemplate<typename T>\nusing vc = vector<T>;\ntemplate<typename T>\nusing vvc = vector<vc<T>>;\ntemplate<typename T>\nusing vvvc = vector<vvc<T>>;\ntemplate<typename T>\nusing vvvvc = vector<vvvc<T>>;\n\n// priority_queue\ntemplate<typename T>\nusing pq = priority_queue<T>;\ntemplate<typename T>\nusing pqg = priority_queue<T, vc<T>, greater<T>>;\n\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(int i = a; i < (int)(b); i++)\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\n#define MIN(vec) *min_element(vec)\n#define MAX(vec) *max_element(vec)\n#define next_perm(vec) (vec).begin(), (vec).end()\n#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())\n#define el \"\\n\"\n#define Yes cout << \"Yes\" << el\n#define No cout << \"No\" << el\n#define YES cout << \"YES\" << el\n#define NO cout << \"NO\" << el\n#define EPS 1e-8\n#define Equal(a, b) (fabs((a)-(b)) < EPS) \n#define dbg(x) cerr << #x << \"=\" << x << el \n\n// 定数\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\nconstexpr int DX8[] = {1, 0, -1, 0, 1, 1, -1, -1};\nconstexpr int DY8[] = {0, 1, 0, -1, 1, -1, 1, -1};\n\ntemplate<typename T1, typename T2>\nostream &operator<< (ostream &os, pair<T1, T2> p) {\n os << \"{\" << p.first << \",\" << p.second << \"}\";\n return os;\n}\ntemplate<typename T>\nostream &operator<< (ostream &os, vc<T> &vec) {\n int sz = vec.size();\n rep(i, sz){\n os << vec[i] << (i==sz-1?\"\":\" \");\n }\n return os;\n}\n\ntemplate<typename T1, typename T2>\nistream &operator>> (istream &is, pair<T1, T2> &p) {\n is >> p.first >> p.second;\n return is;\n}\ntemplate<typename T>\nistream &operator>> (istream &is, vc<T> &vec) {\n int sz = vec.size();\n rep(i, sz) { is >> vec[i]; }\n return is;\n}\n/// @brief aとbの最大値をaに格納。更新があったかbool値を返す\n/// @tparam T1 \n/// @tparam T2 \n/// @param a \n/// @param b \n/// @return bool\ntemplate<typename T1, typename T2>\ninline bool chmax(T1 &a, T2 b){\n bool ret = a<b;\n if(ret) a = b;\n return ret;\n}\n\n/// @brief aとbの最小値をaに格納。更新があったかbool値を返す\n/// @tparam T1 \n/// @tparam T2 \n/// @param a \n/// @param b \n/// @return bool\ntemplate<typename T1, typename T2>\ninline bool chmin(T1 &a, T2 b){\n bool ret = a>b;\n if(ret) {a = b;}\n return ret;\n}\n\ninline void YesNo(bool flag){\n if(flag) {Yes;}\n else {No;}\n return;\n}\n\ninline void YESNO(bool flag){\n if(flag) {YES;}\n else {NO;}\n return;\n}\n\ninline bool outof(ll x, ll xlim){\n return (x<0 || x>=xlim);\n}\n\ntemplate<typename T>\ninline T sqnorm(T x, T y){\n return x*x+y*y;\n}\n\n/// @brief char->int\n/// @param c \n/// @return int\ninline int ctoi(char c){\n return c-'0';\n}\n\n/// @brief xを素因数分解\n/// @param x \n/// @return vector<Pli>, 素因数の昇順に {p, cnt}\nvector<Pli> prime_fact(ll x){\n vector<Pli> ret;\n for(ll i=2; i*i<=x; i++){\n if(x%i == 0){\n ret.emplace_back(i, 0);\n while(x%i == 0){\n ret.back().second++;\n x /= i;\n }\n }\n }\n if(x != 1) ret.emplace_back(x, 1);\n return ret;\n}\n\n/// @brief xの約数列挙\n/// @param x \n/// @return vll, 約数の昇順\nvll divisor_enum(ll x){\n vector<ll> ret;\n for(ll i=1; i*i<=x; i++){\n if(x%i == 0){\n ret.push_back(x/i);\n ret.push_back(i);\n }\n }\n sort(all(ret));\n UNIQUE(ret);\n return ret;\n}\n\n/// @brief 繰り返し二乗法。\n/// @tparam T \n/// @param x \n/// @param k \n/// @param op \n/// @param e \n/// @return \ntemplate<typename T>\nT pow_t(T x, ll k, T (*op)(T, T), T (*e)()){\n T ret = e();\n while(k){\n if(k&1) ret *= x;\n x *= x;\n k >>= 1;\n }\n return ret;\n}\n\nll powll(ll x, ll k){\n return pow_t<ll>(x, k, [](ll a, ll b) -> ll{return a*b;}, []() -> ll{return 1;});\n}\n\ninline int pop_cnt(ll x) { return __builtin_popcountll(x); }\ninline int top_bit(ll x) { return (x==0?-1:63-__builtin_clzll(x));}\n\ntemplate< typename flow_t >\nstruct Dinic {\n const flow_t INF;\n\n struct edge {\n int to;\n flow_t cap;\n int rev;\n bool isrev;\n int idx;\n };\n\n vector< vector< edge > > graph;\n vector< int > min_cost, iter;\n\n Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {}\n\n void add_edge(int from, int to, flow_t cap, int idx = -1) {\n graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false, idx});\n graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true, idx});\n }\n\n bool bfs(int s, int t) {\n min_cost.assign(graph.size(), -1);\n queue< int > que;\n min_cost[s] = 0;\n que.push(s);\n while(!que.empty() && min_cost[t] == -1) {\n int p = que.front();\n que.pop();\n for(auto &e : graph[p]) {\n if(e.cap > 0 && min_cost[e.to] == -1) {\n min_cost[e.to] = min_cost[p] + 1;\n que.push(e.to);\n }\n }\n }\n return min_cost[t] != -1;\n }\n\n flow_t dfs(int idx, const int t, flow_t flow) {\n if(idx == t) return flow;\n for(int &i = iter[idx]; i < graph[idx].size(); i++) {\n edge &e = graph[idx][i];\n if(e.cap > 0 && min_cost[idx] < min_cost[e.to]) {\n flow_t d = dfs(e.to, t, min(flow, e.cap));\n if(d > 0) {\n e.cap -= d;\n graph[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n\n flow_t max_flow(int s, int t) {\n flow_t flow = 0;\n while(bfs(s, t)) {\n iter.assign(graph.size(), 0);\n flow_t f = 0;\n while((f = dfs(s, t, INF)) > 0) flow += f;\n }\n return flow;\n }\n\n void output() {\n for(int i = 0; i < graph.size(); i++) {\n for(auto &e : graph[i]) {\n if(e.isrev) continue;\n auto &rev_e = graph[e.to][e.rev];\n cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << e.cap + rev_e.cap << \")\" << endl;\n }\n }\n }\n};\n\n\ntemplate< typename flow_t, template< typename > class F >\nstruct MaxFlowLowerBound {\n F< flow_t > flow;\n vector< flow_t > in, up;\n typename F< flow_t >::edge *latte, *malta;\n int X, Y, V;\n flow_t sum;\n\n MaxFlowLowerBound(int V) : V(V), flow(V + 2), X(V), Y(V + 1), sum(0), in(V) {}\n\n void add_edge(int from, int to, flow_t low, flow_t high) {\n assert(from != to);\n flow.add_edge(from, to, high - low, up.size());\n in[from] -= low;\n in[to] += low;\n up.emplace_back(high);\n }\n\n void build() {\n for(int i = 0; i < V; i++) {\n if(in[i] > 0) {\n flow.add_edge(X, i, in[i]);\n sum += in[i];\n } else if(in[i] < 0) {\n flow.add_edge(i, Y, -in[i]);\n }\n }\n }\n\n bool can_flow(int s, int t) {\n assert(s != t);\n flow.add_edge(t, s, flow.INF);\n latte = &flow.graph[t].back();\n malta = &flow.graph[s].back();\n return can_flow();\n }\n\n bool can_flow() {\n build();\n auto ret = flow.max_flow(X, Y);\n return ret >= sum;\n }\n\n flow_t max_flow(int s, int t) {\n if(can_flow(s, t)) {\n return flow.max_flow(s, t);\n } else {\n return -1;\n }\n }\n\n flow_t min_flow(int s, int t) {\n if(can_flow(s, t)) {\n auto ret = flow.INF - latte->cap;\n latte->cap = malta->cap = 0;\n return ret - flow.max_flow(t, s);\n } else {\n return -1;\n }\n }\n\n void output(int M) {\n vector< flow_t > ans(M);\n for(int i = 0; i < flow.graph.size(); i++) {\n for(auto &e : flow.graph[i]) {\n if(!e.isrev && ~e.idx) ans[e.idx] = up[e.idx] - e.cap;\n }\n }\n for(auto &p : ans) cout << p << endl;\n }\n};\n\n\n\n\nvoid main2();\n\nint main(){\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n main2();\n}\n\nbool solve(){\n int n, m;\n cin >> n >> m;\n if(n == 0) return false;\n vc<P> es(m);\n rep(i, m){\n int u, v;\n cin >> u >> v;\n u--;v--;\n es[i] = {u, v};\n }\n int ma = 0;\n P ans = {INF, 2*INF};\n rep(mi, n+1){\n bool ng = false;\n chmax(ma, mi);\n while(true){\n vi vvv(1, 2+n+m);\n MaxFlowLowerBound<int, Dinic> mf(n+m+2);\n rep(i, m){\n auto [u, v] = es[i];\n mf.add_edge(n+m, i, 1, 1);\n mf.add_edge(i, m+u, 0, 1);\n mf.add_edge(i, m+v, 0, 1);\n }\n rep(i, n){\n mf.add_edge(m+i, n+m+1, mi, ma);\n }\n auto flow = mf.max_flow(n+m, n+m+1);\n if(flow >= m) break;\n if(ma == n){\n ng = true;\n break;\n }\n ma++;\n }\n if(ng) break;\n if(ans.second - ans.first > ma - mi){\n ans.first = mi;\n ans.second = ma;\n }\n }\n cout << ans.first << \" \" << ans.second << el;\n \n \n return true;\n}\n\nvoid main2(){\n while(solve()){\n ;\n }\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 4608, "score_of_the_acc": -0.615, "final_rank": 8 }, { "submission_id": "aoj_1615_10682955", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\nstruct HLD {\n vector<vector<int>> g;\n vector<int> sz, in, out, par, head, dep, ord;\n HLD(vector<vector<int>> &g_, int root = 0)\n : g(g_), sz((int)g_.size()), in((int)g_.size()), out((int)g_.size()), par((int)g_.size()), head((int)g_.size(), root), dep((int)g_.size()) {\n dfs_sz(root, -1);\n dfs_hld(root, -1);\n }\n void dfs_sz(int v, int p) {\n par[v] = p;\n sz[v] = 1;\n if (g[v].size() && g[v][0] == p) swap(g[v][0], g[v].back());\n for (auto &i : g[v]) {\n if (i != p) {\n dep[i] = dep[v] + 1;\n dfs_sz(i, v);\n sz[v] += sz[i];\n if (sz[g[v][0]] < sz[i]) swap(g[v][0], i);\n }\n }\n }\n void dfs_hld(int v, int p) {\n in[v] = ord.size();\n ord.push_back(v);\n for (auto i : g[v]) {\n if (i != p) {\n if (i == g[v][0]) {\n // Heavy\n head[i] = head[v];\n } else {\n // Light\n head[i] = i;\n }\n dfs_hld(i, v);\n }\n }\n out[v] = ord.size();\n }\n int lca(int u, int v) {\n while (1) {\n if (in[u] > in[v]) swap(u, v);\n if (head[u] == head[v]) return u;\n v = par[head[v]];\n }\n }\n int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }\n int la(int v, int d) {\n while (v != -1) {\n int u = head[v];\n if (in[v] - d >= in[u]) return ord[in[v] - d];\n d -= in[v] - in[u] + 1, v = par[u];\n }\n return -1;\n }\n int jump(int from, int to, int d) {\n int l = lca(from, to);\n if (d <= dep[from] - dep[l]) return la(from, d);\n d -= dep[from] - dep[l];\n if (d <= dep[to] - dep[l]) return la(to, dep[to] - dep[l] - d);\n return -1;\n }\n};\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nstruct FunctionMo {\n int n;\n vector<pair<int, int>> xy;\n\n explicit FunctionMo(int n) : n(n) {}\n\n void add(int x, int y) { xy.emplace_back(x, y); }\n\n template <typename AX, typename SX, typename AY, typename SY, typename O>\n void build(const AX &add_x, const SX &sub_x, const AY &add_y, const SY &sub_y, const O &out) {\n int q = (int)xy.size();\n int bs = max<int>(1, sqrt(n));\n vector<int> ord(q);\n iota(begin(ord), end(ord), 0);\n sort(begin(ord), end(ord), [&](int a, int b) {\n int ablock = xy[a].first / bs, bblock = xy[b].first / bs;\n if (ablock != bblock) return ablock < bblock;\n return (ablock & 1) ? xy[a].second > xy[b].second : xy[a].second < xy[b].second;\n });\n int x = 0, y = 0;\n for (auto idx : ord) {\n while (x > xy[idx].first) add_x(x++, y);\n while (y < xy[idx].second) add_y(x, y++);\n while (x < xy[idx].first) sub_x(x--, y);\n while (y > xy[idx].second) sub_y(x, y--);\n out(idx);\n }\n }\n\n template <typename A, typename E, typename O>\n void build(const A &add, const E &erase, const O &out) {\n build(add, add, erase, erase, out);\n }\n};\nstruct UnionFind {\n vector<int> par, siz;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n};\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\ntemplate <typename T, typename Compare = less<T>>\nvector<pair<int, T>> monotone_minima(int H, int W, const function<T(int, int)> &f, const Compare &comp = Compare()) {\n vector<pair<int, T>> dp(H);\n function<void(int, int, int, int)> dfs = [&](int top, int bottom, int left, int right) {\n if (top > bottom) return;\n int line = (top + bottom) / 2;\n T ma;\n int mi = -1;\n for (int i = left; i <= right; i++) {\n T cst = f(line, i);\n if (mi == -1 || comp(cst, ma)) {\n ma = cst;\n mi = i;\n }\n }\n dp[line] = make_pair(mi, ma);\n dfs(top, line - 1, left, mi);\n dfs(line + 1, bottom, mi, right);\n };\n dfs(0, H - 1, 0, W - 1);\n return dp;\n}\n#include <algorithm>\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap> struct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d =\n self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\nvoid solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0) exit(0);\n vector<int> u(m), v(m);\n rep(i, m) {\n cin >> u[i] >> v[i];\n u[i]--, v[i]--;\n }\n atcoder::mf_graph<int> g_tmp(n + m + 4);\n int S = n + m, T = S + 1, e1 = T + 1, e2 = e1 + 1;\n rep(i, m) g_tmp.add_edge(S, n + i, 1);\n rep(i, m) {\n g_tmp.add_edge(n + i, u[i], 1);\n g_tmp.add_edge(n + i, v[i], 1);\n }\n // g_tmp.add_edge(e1, S, INF);\n // g_tmp.add_edge(T, e2, INF);\n int d = INF, ansl = -1, ansu = -1;\n for (int lb = m / n; lb >= 0; lb--) {\n int ok = n+1 , ng = lb - 1;\n while (ok - ng > 1) {\n int ub = (ok + ng) / 2;\n auto g = g_tmp;\n int cp=m;\n rep(i, n) {\n // i->T,[lb,ub]\n g.add_edge(i, T, lb);\n g.add_edge(i, e2, ub-lb);\n cp-=lb;\n }\n g.add_edge(e2,T,cp);\n if (g.flow(S, T) == m)\n ok = ub;\n else\n ng = ub;\n }\n if (ok == n + 1) continue;\n // cout << lb << \" \" << ok << endl;\n if (chmin(d, ok - lb)) {\n ansl = lb;\n ansu = ok;\n }\n }\n cout << ansl << \" \" << ansu << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (t) solve();\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 4760, "score_of_the_acc": -0.675, "final_rank": 14 }, { "submission_id": "aoj_1615_10673158", "code_snippet": "#include <bits/stdc++.h>\n#define FAST_IO \n\n#define OVERRIDE(a,b,c,d,...) d\n#define REP2(i,n) for (i32 i = 0; i < (i32)(n); ++i)\n#define REP3(i,m,n) for (i32 i = (i32)(m); i < (i32)(n); ++i)\n#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)\n#define PER2(i,n) for (i32 i = (i32)(n)-1; i >= 0; --i)\n#define PER3(i,m,n) for (i32 i = (i32)(n)-1; i >= (i32)(m); --i)\n#define PER(...) OVERRIDE(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)\n#define ALL(x) begin(x), end(x)\n#define LEN(x) (i32)(x.size())\nusing namespace std;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing i32 = signed int;\nusing i64 = signed long long;\nusing f64 = double;\nusing f80 = long double;\nusing pi = pair<i32, i32>;\nusing pl = pair<i64, i64>;\ntemplate <typename T>\nusing V = vector<T>;\ntemplate <typename T>\nusing VV = V<V<T>>;\ntemplate <typename T>\nusing VVV = V<V<V<T>>>;\ntemplate <typename T>\nusing VVVV = V<V<V<V<T>>>>;\ntemplate <typename T>\nusing PQR = priority_queue<T, V<T>, greater<T>>;\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n if (x > y) {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n if (x < y) {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ni32 lob(const V<T> &arr, const T &v) {\n return (i32)(lower_bound(ALL(arr), v) - arr.begin());\n}\ntemplate <typename T>\ni32 upb(const V<T> &arr, const T &v) {\n return (i32)(upper_bound(ALL(arr), v) - arr.begin());\n}\ntemplate <typename T>\nV<i32> argsort(const V<T> &arr) {\n V<i32> ret(arr.size());\n iota(ALL(ret), 0);\n sort(ALL(ret), [&](i32 i, i32 j) -> bool {\n if (arr[i] == arr[j]) {\n return i < j;\n } else {\n return arr[i] < arr[j];\n }\n });\n return ret;\n}\n[[maybe_unused]] constexpr i32 INF = 1000000100;\n[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;\nstruct SetUpIO {\n SetUpIO() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} set_up_io;\nvoid scan(char &x) { cin >> x; }\nvoid scan(u32 &x) { cin >> x; }\nvoid scan(u64 &x) { cin >> x; }\nvoid scan(i32 &x) { cin >> x; }\nvoid scan(i64 &x) { cin >> x; }\nvoid scan(string &x) { cin >> x; }\ntemplate <typename T>\nvoid scan(V<T> &x) {\n for (T &ele : x) {\n scan(ele);\n }\n}\nvoid read() {}\ntemplate <typename Head, typename... Tail>\nvoid read(Head &head, Tail &...tail) {\n scan(head);\n read(tail...);\n}\n#define CHAR(...) char __VA_ARGS__; read(__VA_ARGS__);\n#define U32(...) u32 __VA_ARGS__; read(__VA_ARGS__);\n#define U64(...) u64 __VA_ARGS__; read(__VA_ARGS__);\n#define I32(...) i32 __VA_ARGS__; read(__VA_ARGS__);\n#define I64(...) i64 __VA_ARGS__; read(__VA_ARGS__);\n#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);\n#define VEC(type,name,size) V<type> name(size); read(name);\n#define VVEC(type,name,size1,size2) VV<type> name(size1, V<type>(size2)); read(name);\n#define DBG(...) (void)0\n\n#define ATCODER_MAXFLOW_HPP 1\n\n#define ATCODER_INTERNAL_QUEUE_HPP 1\nnamespace atcoder {\nnamespace internal {\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n}\n}\n\nnamespace atcoder {\ntemplate <class Cap> struct mf_graph {\n public:\n mf_graph() : _n(0) {}\n explicit mf_graph(int n) : _n(n), g(n) {}\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n Cap flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d =\n self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n level[v] = _n;\n return res;\n };\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n}\nvoid solve(i32 n, i32 m) {\n V<i32> u(m), v(m);\n REP(i, m) {\n read(u[i], v[i]);\n --u[i];\n --v[i];\n }\n pi ans(0, n - 1);\n auto judge = [&](i32 l, i32 r) -> bool {\n atcoder::mf_graph<i32> g(n + m + 3);\n i32 s = n + m, t = n + m + 1, p = n + m + 2;\n REP(i, m) {\n g.add_edge(u[i], n + i, 1);\n g.add_edge(v[i], n + i, 1);\n }\n REP(i, n) { g.add_edge(p, i, r - l); }\n g.add_edge(s, p, m - n * l);\n REP(i, n) { g.add_edge(s, i, l); }\n REP(i, m) { g.add_edge(n + i, t, 1); }\n return g.flow(s, t) == m;\n };\n REP(l, n) {\n if (m < n * l) {\n break;\n }\n i32 ok = n, ng = l - 1;\n while (ok - ng > 1) {\n i32 mid = (ok + ng) / 2;\n if (judge(l, mid)) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n if (ok != n) {\n if (ok - l < ans.second - ans.first) {\n ans.first = l;\n ans.second = ok;\n } else if (ok - l == ans.second - ans.first && l >= ans.first) {\n ans.first = l;\n ans.second = ok;\n }\n }\n }\n cout << ans.first << ' ' << ans.second << endl;\n}\nint main() {\n while (1) {\n I32(n, m);\n if (n == 0) {\n break;\n }\n solve(n, m);\n }\n}", "accuracy": 1, "time_ms": 250, "memory_kb": 4200, "score_of_the_acc": -0.4146, "final_rank": 3 }, { "submission_id": "aoj_1615_10578667", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = leader(parent_or_size[a]);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n // root node: -1 * component size\n // otherwise: parent\n std::vector<int> parent_or_size;\n};\n\n} // namespace atcoder\n/* BOOST MULTIPRECISION\n#include<boost/multiprecision/cpp_int.hpp>\nusing namespace boost::multiprecision;\n//*/\n\ntypedef long long ll;\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) begin(v),end(v)\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n\tif (a <= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> bool chmax(T &a, const T &b) {\n\tif (a >= b) return false;\n\ta = b;\n\treturn true;\n}\n\ntemplate <typename T> T max(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T min(vector<T> &a){\n\tassert(!a.empty());\n\tT ret = a[0];\n\tfor (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);\n\treturn ret;\n}\n\ntemplate <typename T> T sum(vector<T> &a){\n\tT ret = 0;\n\tfor (int i=0; i<(int)a.size(); i++) ret += a[i];\n\treturn ret;\n}\n\n// importbisect\ntemplate <typename T>\nint bisect_left(vector<T> &X, T v){\n\treturn lower_bound(X.begin(), X.end(), v) - X.begin();\n}\n\ntemplate <typename T>\nint bisect_right(vector<T> &X, T v){\n\treturn upper_bound(X.begin(), X.end(), v) - X.begin();\n}\n// ----\n\n// defcomp\ntemplate <typename T>\nvector<T> compress(vector<T> &X) {\n\tvector<T> vals = X;\n\tsort(vals.begin(), vals.end());\n\tvals.erase(unique(vals.begin(), vals.end()), vals.end());\n\treturn vals;\n}\n// -----\nstruct edge {\n\tint to, cap, rev;\n};\n\n\nvoid solve(int n, int m) {\n\tvector<int> u(m), v(m);\n\trep(i,0,m) {\n\t\tcin >> u[i] >> v[i];\n\t\tu[i]--; v[i]--;\n\t}\n\n\tvector<vector<edge>> mf(n+m+2, vector<edge>(0));\n\tauto add_edge = [&](int i, int j, int cap) -> void {\n\t\tmf[i].push_back(\n\t\t\tedge{j, cap, (int)mf[j].size()}\n\t\t);\n\t\tmf[j].push_back(\n\t\t\tedge{i, 0, (int)mf[i].size()-1}\n\t\t);\n\t};\n\n\tvector<bool> seen(n+m+2);\n\tvector<pair<int,int>> road;\n\tauto dfs = [&](auto self, int i, int j, int dat) -> int {\n\t\tif (i == j) return dat;\n\t\tseen[i] = 1;\n\n\t\trep(x,0,(int)mf[i].size()) {\n\t\t\tauto e = mf[i][x];\n\t\t\tif (e.cap == 0) continue;\n\t\t\tif (seen[e.to]) continue;\n\t\t\tint r = self(self, e.to, j, min(e.cap, dat));\n\t\t\tif (r > 0) {\n\t\t\t\troad.push_back(pair(i, x));\n\t\t\t\treturn r;\n\t\t\t}\n\t\t}\n\n\t\treturn 0;\n\t};\n\n\tauto flow = [&](int i, int j) -> int {\n\t\tint f = 0;\n\t\twhile (true) {\n\t\t\tfill(all(seen), false);\n\t\t\tvector<pair<int,int>>().swap(road);\n\t\t\tint g = dfs(dfs, i, j, (int)1e9);\n\t\t\tf += g;\n\t\t\tif (g == 0) break;\n\t\t\tfor (auto [r,x]: road) {\n\t\t\t\tmf[r][x].cap -= g;\n\t\t\t\tmf[mf[r][x].to][mf[r][x].rev].cap += g;\n\t\t\t\t//assert(mf[mf[r][x].to][mf[r][x].rev].rev==x);\n\t\t\t}\n\t\t};\n\t\treturn f;\n\t};\n\n\tint ans = (int)1e9;\n\tint ansmin = -1;\n\tint ansmax = -1;\n\n\n\trep(mins,0,n+1) {\n\t\trep(i,0,n+m+2) {\n\t\t\tvector<edge>().swap(mf[i]);\n\t\t}\n\n\t\tvector<int> jun(n);\n\t\tvector<int> rev(n);\n\n\t\trep(i,0,m){\n\t\t\tadd_edge(i,m+u[i],1);\n\t\t\tadd_edge(i,m+v[i],1);\n\t\t\tadd_edge(n+m,i,1);\n\t\t}\n\n\t\trep(i,0,n){\n\t\t\tadd_edge(i+m,n+m+1,mins);\n\t\t\tjun[i] = (int)mf[i+m].size()-1;\n\t\t\trev[i] = (int)mf[n+m+1].size()-1;\n\t\t}\n\n\t\tint f = flow(n+m,n+m+1);\n\n\t\tbool mode = 1;\n\t\trep(i,0,n) {\n\t\t\tif (mf[i+m][jun[i]].cap != 0) {\n\t\t\t\tmode = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tassert(mf[n+m+1][rev[i]].cap == mins);\n\t\t\tmf[n+m+1][rev[i]].cap = 0;\n\t\t}\n\t\tif (!mode) break;\n\n\t\tint maxs = mins;\n\t\twhile (f != m) {\n\t\t\trep(i,0,n){\n\t\t\t\tmf[i+m][jun[i]].cap += 1;\n\t\t\t\t//cout << mf[i+m][jun[i]].cap << endl;\n\t\t\t}\n\t\t\tmaxs++;\n\t\t\tf += flow(n+m,n+m+1);\n\t\t\t\n\t\t\t//cout << mins << ' ' << maxs << ' ' << f << endl;\n\t\t}\n\t\t\n\t\tif (ans >= maxs-mins){\n\t\t\tansmin = mins;\n\t\t\tansmax = maxs;\n\t\t\tans = maxs-mins;\n\t\t}\n\t}\n\n\tcout << ansmin << ' ' << ansmax << endl;\n}\n\nint main(){\n\tios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n\twhile(true) {\n\t\tint n, m;\n\t\tcin >> n >> m;\n\t\tif (n == 0) break;\n\t\tsolve(n, m);\n\t}\n}", "accuracy": 1, "time_ms": 1990, "memory_kb": 4224, "score_of_the_acc": -0.7519, "final_rank": 15 }, { "submission_id": "aoj_1615_10312283", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n\nstruct Flow{\n\tstruct edge {long long to, cap, rev; };\n\tvector<vector<edge>> G;\n vector<int> level;\n vector<int> iter;\n\tint MAX_V;\n \n\tFlow(int v){\n\t\tG.resize(v);\n\t\titer.resize(v);\n level.resize(v , -1);\n\t\tMAX_V = v;\n\t}\n \n\tvoid add_edge(int from, int to, long long cap){\n\t\tG[from].push_back(edge{to, cap, (long long)G[to].size()});\n\t\tG[to].push_back(edge{from, 0, (long long)G[from].size()-1});\n\t};\n \n void bfs(int s){\n for(int i = 0; i < MAX_V; i++)level[i] = -1;\n queue<int> que;\n que.push(s);\n level[s] = 0;\n while(!que.empty()){\n int v = que.front(); que.pop();\n for(int i = 0; i < (int)(G[v].size()); i++){\n edge &e = G[v][i];\n if(e.cap > 0 && level[e.to] < 0){\n level[e.to] = level[v] + 1;\n que.push(e.to);\n }\n }\n }\n }\n \n\tlong long dfs(int v, int t, long long f){\n\t\tif(v == t)return f;\n\t\tfor(int &i = iter[v]; i < (int)(G[v].size()); i++){\n\t\t\tedge &e = G[v][i];\n\t\t\tif(e.cap > 0 && level[v] < level[e.to]){\n\t\t\t\tlong long d = dfs(e.to, t, min(f, e.cap));\n\t\t\t\tif(d > 0){\n\t\t\t\t\te.cap -= d;\n\t\t\t\t\tG[e.to][e.rev].cap += d;\n\t\t\t\t\treturn d;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n \n\tlong long max_flow(int s, int t){\n\t\tlong long flow = 0;\n\t\twhile(1){\n\t\t\tbfs(s);\n\t\t\tif(level[t] < 0)return flow;\n for(int i = 0; i < MAX_V; i++)iter[i] = 0;\n\t\t\tlong long f;\n while((f = dfs(s , t , 1LL<<60)) > 0)flow += f;\n\t\t}\n\t\treturn 0;\n\t}\n};\n\n\n\nint main(){\n\twhile(1){\n\t\tint N,M; cin >> N >> M;\n\t\tif(N == 0)break;\n\t\tvector<int> v(M) , u(M);\n\t\tfor(int i = 0; i < M; i++){\n\t\t\tcin >> v[i] >> u[i];\n\t\t\t--v[i];\n\t\t\t--u[i];\n\t\t}\n\t\tauto ok = [&](int low , int high)->bool{\n\t\t\tFlow f(N+M+4);\n\t\t\tint S = N+M+3;\n\t\t\tint T = S-1;\n\t\t\tint S2 = T-1;\n\t\t\tint T2 = S2-1;\n\t\t\tfor(int i = 0; i < M; i++){\n\t\t\t\tf.add_edge(S , i , 1);\n\t\t\t\tf.add_edge(i , M+v[i] , 1);\n\t\t\t\tf.add_edge(i , M+u[i] , 1);\n\t\t\t}\n\t\t\tfor(int i = 0; i < N; i++){\n\t\t\t\tf.add_edge(M+i , T , high - low);\n\t\t\t\tf.add_edge(S2 , T , low);\n\t\t\t\tf.add_edge(M+i , T2 , low);\n\t\t\t}\n\t\t\tll FLOW = 0;\n\t\t\tFLOW += f.max_flow(S2 , T2);\n\t\t\tFLOW += f.max_flow(S2 , T);\n\t\t\tFLOW += f.max_flow(S , T2);\n\t\t\tFLOW += f.max_flow(S , T);\n\t\t\tif(FLOW < M)return 0;\n\t\t\tfor(auto e:f.G[S2]){\n\t\t\t\tif(e.cap > 0)return 0;\n\t\t\t}\n\t\t\tfor(auto e:f.G[T2]){\n\t\t\t\tif(e.cap < low)return 0;\n\t\t\t}\n\t\t\treturn 1;\n\t\t};\n\t\tint LO = -1 , HI = M;\n\t\tint hi = 1;\n\t\tfor(int lo = 0; lo < N; lo++){\n\t\t\thi = max(hi , lo);\n\t\t\twhile(hi <= N && !ok(lo, hi))hi++;\n\t\t\tif(hi == N+1)break;\n\t\t\tif(HI - LO >= hi - lo){\n\t\t\t\tHI = hi;\n\t\t\t\tLO = lo;\n\t\t\t}\n\t\t}\n\t\t/*\n\t\tfor(int i = 0; i < N; i++){\n\t\t\tfor(int j = i; j < N; j++){\n\t\t\t\tcout << i << ' ' << j << ' ' << ok(i,j) << endl;\n\t\t\t}\n\t\t}\n\t\t*/\n\t\tcout << LO << ' ' << HI << endl;\n\t}\n}", "accuracy": 1, "time_ms": 230, "memory_kb": 4608, "score_of_the_acc": -0.6169, "final_rank": 9 }, { "submission_id": "aoj_1615_9632804", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define vi vector<int>\n#define vl vector<ll>\n#define ov4(a, b, c, d, name, ...) name\n#define rep3(i, a, b, c) for(ll i = (a); i < (b); i += (c))\n#define rep2(i, a, b) rep3(i, a, b, 1)\n#define rep1(i, n) rep2(i, 0, n)\n#define rep0(n) rep1(aaaaa, n)\n#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)\n#define per(i, a, b) for(ll i = (a) - 1; i >= (b); i--)\n#define fore(e, v) for(auto &&e : v)\n#define all(a) begin(a), end(a)\n#define si(a) (int)(size(a))\n#define lb(v, x) (lower_bound(all(v), x) - begin(v))\n#define eb emplace_back\n\ntemplate <typename T, typename S> bool chmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; }\ntemplate <typename T, typename S> bool chmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; }\n\nconst int INF = 1e9 + 100;\nconst ll INFL = 3e18 + 100;\n\n#define i128 __int128_t\n\nstruct _ {\n _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }\n} __;\ntemplate <typename T> struct Dinic {\n const T INF;\n\n struct edge {\n int to;\n T cap;\n int rev;\n bool isrev;\n int idx;\n };\n\n vector<vector<edge>> g;\n vector<int> c, iter;\n Dinic(int V) : INF(numeric_limits<T>::max()), g(V) {}\n void add_edge(int from, int to, T cap, int idx = -1) {\n g[from].emplace_back((edge){to, cap, si(g[to]), false, idx});\n g[to].emplace_back((edge){from, 0, si(g[from]) - 1, true, idx});\n }\n\n bool bfs(int s, int t) {\n c.assign(si(g), -1);\n queue<int> q;\n c[s] = 0;\n q.push(s);\n while(!q.empty() && c[t] == -1) {\n int x = q.front();\n q.pop();\n fore(e, g[x]) {\n if(e.cap > 0 && c[e.to] == -1) {\n c[e.to] = c[x] + 1;\n q.push(e.to);\n }\n }\n }\n return c[t] != -1;\n }\n\n T dfs(int x, int t, T flow) {\n if(x == t) return flow;\n for(int &i = iter[x]; i < si(g[x]); i++) {\n edge &e = g[x][i];\n if(e.cap > 0 && c[x] < c[e.to]) {\n T d = dfs(e.to, t, min(flow, e.cap));\n if(d > 0) {\n e.cap -= d;\n g[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n\n T max_flow(int s, int t) {\n T flow = 0;\n while(bfs(s, t)) {\n iter.assign(si(g), 0);\n T f = 0;\n while((f = dfs(s, t, INF)) > 0) flow += f;\n }\n return flow;\n }\n\n // void output() {\n // for(int i = 0; i < g.size(); i++) {\n // for(auto &e : g[i]) {\n // if(e.isrev) continue;\n // auto &rev_e = g[e.to][e.rev];\n // cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << e.cap + rev_e.cap << \")\" << endl;\n // }\n // }\n // }\n};\n\ntemplate <typename T> struct lrFlow {\n Dinic<T> flow;\n vector<T> in, up;\n int X, Y, n;\n T sum;\n typename Dinic<T>::edge *p, *q;\n\n lrFlow(int n) : n(n), X(n), Y(n + 1), sum(0), in(n), flow(n + 2) {}\n\n void add_edge(int from, int to, T low, T high) {\n flow.add_edge(from, to, high - low, si(up));\n in[from] -= low, in[to] += low;\n up.eb(high);\n }\n\n void build() {\n rep(i, n) {\n if(in[i] > 0) {\n flow.add_edge(X, i, in[i]);\n sum += in[i];\n } else if(in[i] < 0) {\n flow.add_edge(i, Y, -in[i]);\n }\n }\n }\n\n bool can_flow(int s, int t) {\n flow.add_edge(t, s, flow.INF);\n p = &flow.g[t].back();\n q = &flow.g[s].back();\n return can_flow();\n }\n\n bool can_flow() {\n build();\n auto ret = flow.max_flow(X, Y);\n return ret >= sum;\n }\n\n T max_flow(int s, int t) {\n if(can_flow(s, t)) {\n return flow.max_flow(s, t);\n } else {\n return -1;\n }\n }\n\n T min_flow(int s, int t) {\n if(can_flow(s, t)) {\n auto ret = flow.INF - p->cap;\n p->cap = q->cap = 0;\n return ret - flow.max_flow(t, s);\n } else {\n return -1;\n }\n }\n\n // void output(int M) {\n // vector<flow_t> ans(M);\n // for(int i = 0; i < flow.graph.size(); i++) {\n // for(auto &e : flow.graph[i]) {\n // if(!e.isrev && ~e.idx) ans[e.idx] = up[e.idx] - e.cap;\n // }\n // }\n // for(auto &p : ans) cout << p << endl;\n // }\n};\n\nint main() {\n int n, m;\n while(cin >> n >> m, n) {\n vi u(m), v(m);\n rep(i, m) {\n cin >> u[i] >> v[i];\n --u[i], --v[i];\n }\n auto check = [&](int l, int r) {\n lrFlow<int> flow(n + m + 2);\n int s = n + m, t = n + m + 1;\n rep(i, m) {\n flow.add_edge(s, i, 1, 1);\n flow.add_edge(i, m + u[i], 0, 1);\n flow.add_edge(i, m + v[i], 0, 1);\n }\n rep(i, n) { flow.add_edge(m + i, t, l, r); }\n auto res = flow.max_flow(s, t) == m;\n return res;\n };\n int p = 0, q = n;\n int l = 0;\n rep(r, n + 1) {\n while(l <= r and check(l, r)) {\n p = l, q = r;\n ++l;\n }\n }\n cout << p << \" \" << q << endl;\n }\n}", "accuracy": 1, "time_ms": 350, "memory_kb": 4412, "score_of_the_acc": -0.5403, "final_rank": 5 }, { "submission_id": "aoj_1615_9566917", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<ll,ll>;\nusing vi = vector<ll>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing ve = vector<vector<int>>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\n#define rep(i,n) for(ll i = 0;i < (ll)n;i++)\n#define ALL(x) (x).begin(),(x).end()\n#define sz(c) ((ll)(c).size())\n#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())\n#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())\n// #define MOD 1000000007\n#define MOD 998244353\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<\" \"<<p.second;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}\ntemplate<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}\nld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}\n\n\n// https://ei1333.github.io/luzhiled/snippets/graph/dinic.html\ntemplate<typename flow_t>\nstruct Dinic{\n /*\n 復元をするときはfromの頂点から出る辺からのcapを出力.\n is_e == trueの時その辺は元のグラフにあった\"本当の\"辺.\n \"本当の\"辺eについてfr -> toにv[e.to][e.ref].capだけフローが流れている.\n 二部マッチングの復元の場合,仮想の始点(終点)から(へ)の辺に注意.\n */\n struct edge{\n int to;\n flow_t cap;\n int ref;//逆辺の番号\n bool is_e;//本物の辺かどうか\n int idx;//入力で与えられる辺としてのindex\n };\n vector<vector<edge>> v;\n vector<int> mn_cost,iter;\n const flow_t inf;\n Dinic(int n):inf(numeric_limits<flow_t>::max()),v(n){}\n void add_edge(int fr,int to,flow_t cap,int idx = -1){\n v[fr].emplace_back((edge){to,cap,(int)v[to].size(),true,idx});\n v[to].emplace_back((edge){fr,0,(int)v[fr].size()-1,false,idx});\n }\n bool bfs(int s,int t){\n mn_cost.assign(v.size(),-1);\n queue<int> que;\n mn_cost[s] = 0;\n que.push(s);\n while(!que.empty() && mn_cost[t] == -1){\n int ov = que.front();que.pop();\n for(auto &e : v[ov]){\n if(e.cap > 0 && mn_cost[e.to] == -1){\n mn_cost[e.to] = mn_cost[ov] + 1;\n que.push(e.to);\n }\n }\n }\n return mn_cost[t] != -1;\n }\n flow_t dfs(int ov,const int t,flow_t flow){\n if(ov == t)return flow;\n for(int &i = iter[ov];i < v[ov].size();i++){\n edge &e = v[ov][i];\n if(e.cap > 0 && mn_cost[ov] < mn_cost[e.to]){\n flow_t d = dfs(e.to,t,min(flow,e.cap));\n if(d > 0){\n e.cap -= d;\n v[e.to][e.ref].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n // O(m n^2)(n:頂点数 m:辺数)\n flow_t max_flow(int s,int t){\n flow_t flow = 0;\n while(bfs(s,t)){\n iter.assign(v.size(),0);\n flow_t f = 0;\n while((f = dfs(s,t,inf)) > 0)flow += f;\n }\n return flow;\n }\n void output(){\n for(int i = 0;i < v.size();i++){\n for(auto &e : v[i]) {\n if(!e.is_e) continue;\n auto &rev_e = v[e.to][e.ref];\n cout << i << \"->\" << e.to << \" (flow: \" << rev_e.cap << \"/\" << e.cap + rev_e.cap << \")\" << \"\\n\";\n }\n }\n }\n};\n\nint main(){\n \n ios_base::sync_with_stdio(0), cin.tie(0);\n while(1){\n int n,m;cin >> n >> m;\n if(!n)break;\n P res = {0,n+100};\n vector<P> v(m);cin >> v;\n for(auto &[a,b] : v)a--,b--;\n int r = 1;\n rep(l,n){\n if(r < l)r = l;\n while(1){\n if(r >= n)break;\n Dinic<ll> dn(4 + n + m);\n ll sm = 0;\n const int s = n+m,t = n+m+1,S = n+m+2,T = n+m+3;\n rep(i,m){\n auto [a,b] = v[i];\n dn.add_edge(a,n+i,1);\n dn.add_edge(b,n+i,1);\n dn.add_edge(n+i,t,1);\n }\n rep(i,n){\n if(l != r)dn.add_edge(s,i,r-l);\n if(l){\n dn.add_edge(S,i,l);\n dn.add_edge(s,T,l);\n sm += l;\n }\n }\n ll a = dn.max_flow(S,T);\n ll b = dn.max_flow(s,T);\n ll c = dn.max_flow(S,t);\n ll d = dn.max_flow(s,t);\n // cout << b + d << \" a\\n\";\n // cout << a << \" \" << b << \" \" << c << \" \" << d << \" : \" << sm << \" a\\n\";\n if(sm == a + c && sm == a + b && m == b + d){\n auto [L,R] = res;\n if(r-l < R-L)res = {l,r};\n break;\n }\n r++;\n }\n }\n cout << res << \"\\n\";\n }\n \n\n return 0;\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 4656, "score_of_the_acc": -0.6561, "final_rank": 11 }, { "submission_id": "aoj_1615_9480955", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\n\nusing namespace std;\nusing ll = long long;\nusing pll = pair<ll, ll>;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n\n#define TT template <typename T>\nTT using vec = vector<T>;\nTT using vvec = vec<vec<T>>;\nTT using vvvec = vec<vvec<T>>;\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\ntemplate <class T1, class T2> bool chmin(T1 &x, T2 y) {\n return x > y ? (x = y, true) : false;\n}\ntemplate <class T1, class T2> bool chmax(T1 &x, T2 y) {\n return x < y ? (x = y, true) : false;\n}\nTT int pre_id(vec<T> &A, T x) { // must sorted\n return lower_bound(all(A), x) - A.begin() - 1;\n}\nTT int nex_id(vec<T> &A, T x) { // must sorted\n return upper_bound(all(A), x) - A.begin();\n}\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\ntemplate <class T> struct mf_graph {\n struct edge {\n int st, to;\n T cap, flow;\n };\n\n struct nedge {\n int to, rev;\n T cap;\n };\n\n int n;\n vec<unordered_map<int, int>> rev;\n vec<pair<int, int>> pos;\n vec<vec<nedge>> g;\n\n mf_graph(int _n) : n(_n), g(n), rev(n) {}\n\n int add_edge(int s, int t, T cap) {\n int m = pos.size();\n pos.push_back({s, g[s].size()});\n int fi = g[s].size();\n int ti = g[t].size();\n if (s == t) ti++;\n g[s].push_back(nedge{t, ti, cap});\n g[t].push_back(nedge{s, fi, 0});\n rev[s][t] = m;\n return m;\n }\n\n T flow(int s, int t, T flow_limit = numeric_limits<T>::max()) {\n vec<int> lv(n), it(n, 0);\n\n auto bfs = [&]() {\n queue<int> que;\n fill(lv.begin(), lv.end(), -1);\n lv[s] = 0;\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n\n for (auto e : g[v]) {\n if (e.cap == 0 || lv[e.to] >= 0) continue;\n lv[e.to] = lv[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n\n auto dfs = [&](auto f, int v, T up) {\n if (v == s) return up;\n T res = 0;\n int LV = lv[v];\n for (int &i = it[v]; i < int(g[v].size()); i++) {\n nedge &e = g[v][i];\n if (LV <= lv[e.to] || g[e.to][e.rev].cap == 0) continue;\n T d = f(f, e.to, min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n lv[v] = n;\n return res;\n };\n\n T flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (lv[t] == -1) break;\n fill(it.begin(), it.end(), 0);\n T f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n\n // 以下、不要なら省略\n\n int get_id(int s, int t) { return rev[s][t]; }\n\n edge get_edge(int i) {\n int m = pos.size();\n auto e = g[pos[i].first][pos[i].second];\n auto re = g[e.to][e.rev];\n return edge{pos[i].first, e.to, e.cap + re.cap, re.cap};\n }\n\n void change_edge(int i, T nc, T nf) {\n int m = pos.size();\n auto &e = g[pos[i].first][pos[i].second];\n auto &re = g[e.to][e.rev];\n e.cap = nc - nf;\n re.cap = nf;\n }\n\n vec<bool> min_cut(int s) {\n vec<bool> seen(n);\n queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n seen[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !seen[e.to]) {\n seen[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return seen;\n }\n};\n\n/*\n@brief Maxflow\n@docs doc/maxflow.md\n*/\n\nstruct mf_graph_with_lowerbound {\n int n;\n mf_graph<ll> mf;\n int S, T;\n ll sum;\n vector<ll> lows, d;\n ll total_flow;\n\n mf_graph_with_lowerbound(int n)\n : n(n), mf(n + 2), S(n), T(n + 1), sum(0), d(n, 0), total_flow(0) {}\n\n int add_edge(int from, int to, ll low, ll high) {\n lows.push_back(low);\n d[from] -= low;\n d[to] += low;\n return mf.add_edge(from, to, high - low);\n }\n\n void build() {\n rep(i, 0, n) {\n if (d[i] > 0) {\n mf.add_edge(S, i, d[i]);\n sum += d[i];\n } else if(d[i] < 0) {\n mf.add_edge(i, T, -d[i]);\n }\n }\n }\n\n bool is_feasable(int s, int t) {\n build();\n mf.add_edge(t, s, LLONG_MAX / 4); // INF = LLONG_MAX / 4\n total_flow += mf.flow(S, T);\n return total_flow == sum;\n }\n\n ll flow(int s, int t) {\n if (is_feasable(s, t) == false) return -1;\n mf.add_edge(S, s, LLONG_MAX / 4);\n mf.add_edge(t, T, LLONG_MAX / 4);\n return (total_flow += mf.flow(S, T)) - sum;\n }\n\n vector<ll> output() {\n vector<ll> res;\n rep(i, 0, lows.size()) { res.push_back(lows[i] + mf.get_edge(i).flow); }\n return res;\n }\n};\n\nint main() {\n int n, m;\n while (1) {\n cin >> n >> m;\n\n if (n == 0) break;\n\n vector<pll> es;\n rep(i, 0, m) {\n int u, v;\n cin >> u >> v;\n u--, v--;\n es.emplace_back(u, v);\n }\n\n int ans_m = -1;\n int ans_M = -1;\n\n auto che = [&](int mi, int Mi) {\n int s = n + m;\n int t = s + 1;\n\n mf_graph_with_lowerbound mf(t + 1);\n\n rep(i, 0, m) {\n mf.add_edge(s, i, 1, 1);\n mf.add_edge(i, m + es[i].first, 0, 1);\n mf.add_edge(i, m + es[i].second, 0, 1);\n }\n\n rep(i, 0, n) { mf.add_edge(m + i, t, mi, Mi); }\n\n if (mf.is_feasable(s, t)) {\n return true;\n } else\n return false;\n };\n\n ll w = 1e7;\n rrep(mi, 0, n + 1) {\n ll li = 0;\n ll ri = li + min<ll>(w, m);\n while (li < ri) { /// xxxxoooo\n ll mid = (li + ri) / 2;\n if (che(mi, mi + mid)) {\n ri = mid;\n } else {\n li = mid + 1;\n }\n }\n if (che(mi, mi + li) && chmin(w, li - mi)) {\n ans_m = mi;\n ans_M = mi + li;\n }\n }\n\n cout << ans_m << \" \" << ans_M << endl;\n }\n}", "accuracy": 1, "time_ms": 4540, "memory_kb": 5392, "score_of_the_acc": -1.8184, "final_rank": 19 }, { "submission_id": "aoj_1615_9480949", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\n\nusing namespace std;\nusing ll = long long;\nusing pll = pair<ll, ll>;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n\n#define TT template <typename T>\nTT using vec = vector<T>;\nTT using vvec = vec<vec<T>>;\nTT using vvvec = vec<vvec<T>>;\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\ntemplate <class T1, class T2> bool chmin(T1 &x, T2 y) {\n return x > y ? (x = y, true) : false;\n}\ntemplate <class T1, class T2> bool chmax(T1 &x, T2 y) {\n return x < y ? (x = y, true) : false;\n}\nTT int pre_id(vec<T> &A, T x) { // must sorted\n return lower_bound(all(A), x) - A.begin() - 1;\n}\nTT int nex_id(vec<T> &A, T x) { // must sorted\n return upper_bound(all(A), x) - A.begin();\n}\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\ntemplate <class T> struct mf_graph {\n struct edge {\n int st, to;\n T cap, flow;\n };\n\n struct nedge {\n int to, rev;\n T cap;\n };\n\n int n;\n vec<unordered_map<int, int>> rev;\n vec<pair<int, int>> pos;\n vec<vec<nedge>> g;\n\n mf_graph(int _n) : n(_n), g(n), rev(n) {}\n\n int add_edge(int s, int t, T cap) {\n int m = pos.size();\n pos.push_back({s, g[s].size()});\n int fi = g[s].size();\n int ti = g[t].size();\n if (s == t) ti++;\n g[s].push_back(nedge{t, ti, cap});\n g[t].push_back(nedge{s, fi, 0});\n rev[s][t] = m;\n return m;\n }\n\n T flow(int s, int t, T flow_limit = numeric_limits<T>::max()) {\n vec<int> lv(n), it(n, 0);\n\n auto bfs = [&]() {\n queue<int> que;\n fill(lv.begin(), lv.end(), -1);\n lv[s] = 0;\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n\n for (auto e : g[v]) {\n if (e.cap == 0 || lv[e.to] >= 0) continue;\n lv[e.to] = lv[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n\n auto dfs = [&](auto f, int v, T up) {\n if (v == s) return up;\n T res = 0;\n int LV = lv[v];\n for (int &i = it[v]; i < int(g[v].size()); i++) {\n nedge &e = g[v][i];\n if (LV <= lv[e.to] || g[e.to][e.rev].cap == 0) continue;\n T d = f(f, e.to, min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n lv[v] = n;\n return res;\n };\n\n T flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (lv[t] == -1) break;\n fill(it.begin(), it.end(), 0);\n T f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n\n // 以下、不要なら省略\n\n int get_id(int s, int t) { return rev[s][t]; }\n\n edge get_edge(int i) {\n int m = pos.size();\n auto e = g[pos[i].first][pos[i].second];\n auto re = g[e.to][e.rev];\n return edge{pos[i].first, e.to, e.cap + re.cap, re.cap};\n }\n\n void change_edge(int i, T nc, T nf) {\n int m = pos.size();\n auto &e = g[pos[i].first][pos[i].second];\n auto &re = g[e.to][e.rev];\n e.cap = nc - nf;\n re.cap = nf;\n }\n\n vec<bool> min_cut(int s) {\n vec<bool> seen(n);\n queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n seen[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !seen[e.to]) {\n seen[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return seen;\n }\n};\n\n/*\n@brief Maxflow\n@docs doc/maxflow.md\n*/\n\nstruct mf_graph_with_lowerbound {\n int n;\n mf_graph<ll> mf;\n int S, T;\n ll sum;\n vector<ll> lows, d;\n ll total_flow;\n\n mf_graph_with_lowerbound(int n)\n : n(n), mf(n + 2), S(n), T(n + 1), sum(0), d(n, 0), total_flow(0) {}\n\n int add_edge(int from, int to, ll low, ll high) {\n lows.push_back(low);\n d[from] -= low;\n d[to] += low;\n return mf.add_edge(from, to, high - low);\n }\n\n void build() {\n rep(i, 0, n) {\n if (d[i] > 0) {\n mf.add_edge(S, i, d[i]);\n sum += d[i];\n } else\n mf.add_edge(i, T, -d[i]);\n }\n }\n\n bool is_feasable(int s, int t) {\n build();\n mf.add_edge(t, s, LLONG_MAX / 4); // INF = LLONG_MAX / 4\n total_flow += mf.flow(S, T);\n return total_flow == sum;\n }\n\n ll flow(int s, int t) {\n if (is_feasable(s, t) == false) return -1;\n mf.add_edge(S, s, LLONG_MAX / 4);\n mf.add_edge(t, T, LLONG_MAX / 4);\n return (total_flow += mf.flow(S, T)) - sum;\n }\n\n vector<ll> output() {\n vector<ll> res;\n rep(i, 0, lows.size()) { res.push_back(lows[i] + mf.get_edge(i).flow); }\n return res;\n }\n};\n\nint main() {\n int n, m;\n while (1) {\n cin >> n >> m;\n\n if (n == 0) break;\n\n vector<pll> es;\n rep(i, 0, m) {\n int u, v;\n cin >> u >> v;\n u--, v--;\n es.emplace_back(u, v);\n }\n\n int ans_m = -1;\n int ans_M = -1;\n\n auto che = [&](int mi, int Mi) {\n int s = n + m;\n int t = s + 1;\n\n mf_graph_with_lowerbound mf(t + 1);\n\n rep(i, 0, m) {\n mf.add_edge(s, i, 1, 1);\n mf.add_edge(i, m + es[i].first, 0, 1);\n mf.add_edge(i, m + es[i].second, 0, 1);\n }\n\n rep(i, 0, n) { mf.add_edge(m + i, t, mi, Mi); }\n\n if (mf.is_feasable(s, t)) {\n return true;\n } else\n return false;\n };\n\n ll w = 1e7;\n rrep(mi, 0, n + 1) {\n ll li = 0;\n ll ri = li + min<ll>(w, m);\n while (li < ri) { /// xxxxoooo\n ll mid = (li + ri) / 2;\n if (che(mi, mi + mid)) {\n ri = mid;\n } else {\n li = mid + 1;\n }\n }\n if (che(mi, mi + li) && chmin(w, li - mi)) {\n ans_m = mi;\n ans_M = mi + li;\n }\n }\n\n cout << ans_m << \" \" << ans_M << endl;\n }\n}", "accuracy": 1, "time_ms": 4540, "memory_kb": 5316, "score_of_the_acc": -1.7801, "final_rank": 18 }, { "submission_id": "aoj_1615_9480943", "code_snippet": "#include <bits/stdc++.h>\n//#include <atcoder/all>\n\nusing namespace std;\nusing ll = long long;\nusing pll = pair<ll, ll>;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n\n#define TT template <typename T>\nTT using vec = vector<T>;\nTT using vvec = vec<vec<T>>;\nTT using vvvec = vec<vvec<T>>;\nTT using minheap = priority_queue<T, vector<T>, greater<T>>;\nTT using maxheap = priority_queue<T>;\ntemplate <class T1, class T2> bool chmin(T1 &x, T2 y) {\n return x > y ? (x = y, true) : false;\n}\ntemplate <class T1, class T2> bool chmax(T1 &x, T2 y) {\n return x < y ? (x = y, true) : false;\n}\nTT int pre_id(vec<T> &A, T x) { // must sorted\n return lower_bound(all(A), x) - A.begin() - 1;\n}\nTT int nex_id(vec<T> &A, T x) { // must sorted\n return upper_bound(all(A), x) - A.begin();\n}\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\n\ntemplate<class T> \nstruct mf_graph {\n struct edge {\n int st, to;\n T cap, flow;\n };\n\n struct nedge {\n int to, rev;\n T cap;\n };\n\n int n;\n vec<unordered_map<int, int>> rev;\n vec<pair<int, int>> pos;\n vec<vec<nedge>> g;\n\n mf_graph(int _n) : n(_n), g(n), rev(n) {}\n\n int add_edge(int s, int t, T cap) {\n int m = pos.size();\n pos.push_back({s, g[s].size()});\n int fi = g[s].size(); int ti = g[t].size();\n if(s==t)ti++;\n g[s].push_back(nedge{t, ti, cap});\n g[t].push_back(nedge{s, fi, 0});\n rev[s][t] = m;\n return m;\n }\n\n T flow(int s, int t, T flow_limit = numeric_limits<T>::max()) {\n vec<int> lv(n), it(n, 0);\n\n auto bfs = [&]() {\n queue<int> que;\n fill(lv.begin(), lv.end(), -1);\n lv[s] = 0;\n que.push(s);\n while(!que.empty()) {\n int v = que.front();\n que.pop();\n\n for(auto e : g[v]) {\n if(e.cap == 0 || lv[e.to] >= 0) continue;\n lv[e.to] = lv[v] + 1;\n if(e.to == t) return;\n que.push(e.to);\n }\n }\n };\n\n auto dfs = [&](auto f, int v, T up) {\n if(v==s) return up;\n T res = 0;\n int LV = lv[v];\n for(int& i = it[v]; i < int(g[v].size()); i++) {\n nedge& e = g[v][i];\n if(LV <= lv[e.to] || g[e.to][e.rev].cap == 0) continue;\n T d = f(f, e.to, min(up - res, g[e.to][e.rev].cap));\n if(d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if(res == up) return res;\n }\n lv[v] = n;\n return res;\n };\n\n T flow = 0;\n while(flow < flow_limit) {\n bfs();\n if(lv[t] == -1) break;\n fill(it.begin(), it.end(), 0);\n T f = dfs(dfs, t, flow_limit - flow);\n if(!f) break;\n flow += f;\n }\n return flow;\n }\n\n //以下、不要なら省略\n\n int get_id(int s, int t) {\n return rev[s][t];\n }\n\n edge get_edge(int i) {\n int m = pos.size();\n auto e = g[pos[i].first][pos[i].second];\n auto re = g[e.to][e.rev];\n return edge{pos[i].first, e.to, e.cap + re.cap, re.cap};\n }\n\n void change_edge(int i, T nc, T nf) {\n int m = pos.size();\n auto& e = g[pos[i].first][pos[i].second];\n auto& re = g[e.to][e.rev];\n e.cap = nc - nf;\n re.cap = nf;\n }\n\n vec<bool> min_cut(int s) {\n vec<bool> seen(n);\n queue<int> que;\n que.push(s);\n while(!que.empty()) {\n int p = que.front();\n que.pop();\n seen[p]=true;\n for(auto e : g[p]) {\n if(e.cap && !seen[e.to]) {\n seen[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return seen;\n }\n\n};\n\n\n\n/*\n@brief Maxflow\n@docs doc/maxflow.md\n*/\n\nstruct mf_graph_with_lowerbound {\n int n;\n mf_graph<ll> mf;\n int S, T;\n ll sum;\n vector<ll> lows, d;\n ll total_flow;\n\n\n mf_graph_with_lowerbound(int n) : n(n), mf(n+ 2), S(n), T(n + 1), sum(0), d(n, 0), total_flow(0) {\n }\n\n\n int add_edge(int from, int to, ll low, ll high) {\n lows.push_back(low);\n d[from] -= low;\n d[to] += low;\n return mf.add_edge(from, to, high - low);\n }\n\n void build() {\n rep(i, 0, n) {\n if(d[i] > 0) {\n mf.add_edge(S, i, d[i]);\n sum += d[i];\n }\n else mf.add_edge(i, T, -d[i]);\n }\n }\n\n bool is_feasable(int s, int t) {\n build();\n mf.add_edge(t, s, LLONG_MAX / 4); // INF = LLONG_MAX / 4\n total_flow += mf.flow(S, T);\n return total_flow == sum;\n }\n\n ll flow(int s, int t) {\n if(is_feasable(s, t) == false) return -1;\n mf.add_edge(S, s, LLONG_MAX / 4);\n mf.add_edge(t, T, LLONG_MAX / 4);\n return (total_flow += mf.flow(S, T)) - sum;\n }\n\n vector<ll> output() {\n vector<ll> res;\n rep(i, 0, lows.size()) {\n res.push_back(lows[i] + mf.get_edge(i).flow);\n }\n return res;\n\n }\n};\n\n\nint main() {\n int n, m;\n while(1) {\n cin >> n >> m;\n\n if(n == 0) break;\n\n vector<pll> es;\n rep(i, 0, m) {\n int u, v;\n cin >> u >> v;\n u--, v--;\n es.emplace_back(u, v);\n }\n\n\n int ans_m = -1;\n int ans_M = -1;\n\n auto che = [&](int mi, int Mi) {\n int s = n + m;\n int t = s + 1;\n\n mf_graph_with_lowerbound mf(t + 1);\n \n\n rep(i, 0, m) {\n mf.add_edge(s, i, 1, 1);\n mf.add_edge(i, m + es[i].first, 0, 1);\n mf.add_edge(i, m + es[i].second, 0, 1);\n }\n\n rep(i, 0, n) {\n mf.add_edge(m + i, t, mi, Mi);\n }\n\n if(mf.is_feasable(s, t)) {\n return true;\n }\n else return false;\n\n };\n\n \n ll w = 1e7;\n rrep(mi, 0, n + 1) {\n ll li = 0;\n ll ri = m;\n while(li < ri) {///xxxxoooo\n ll mid = (li + ri) / 2;\n if(che(mi, mi + mid)) {\n ri = mid;\n }\n else {\n li = mid + 1;\n }\n }\n if(che(mi, mi + li) && chmin(w, li - mi)) {\n ans_m = mi;\n ans_M = mi + li;\n\n }\n }\n\n cout << ans_m << \" \" << ans_M << endl;\n }\n \n\n}", "accuracy": 1, "time_ms": 5360, "memory_kb": 5344, "score_of_the_acc": -1.9475, "final_rank": 20 }, { "submission_id": "aoj_1615_9474816", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nstruct MaxFlow {\n struct Edge {\n int to, rev;\n ll cap;\n };\n int V;\n vector<vector<Edge>> G;\n vector<int> itr, level;\n using P = pair<int, int>;\n vector<P> es;\n\n public:\n MaxFlow() {}\n MaxFlow(int V) : V(V) {\n G.assign(V, vector<Edge>());\n }\n int add_vertex() {\n G.push_back(vector<Edge>());\n return V++;\n }\n void add_edge(int from, int to, ll cap) {\n int fid = SZ(G[from]), tid = SZ(G[to]);\n if (from == to)\n tid++;\n es.push_back({from, fid});\n G[from].push_back({to, tid, cap});\n G[to].push_back({from, fid, 0});\n }\n struct Type {\n int from, to;\n ll cap, recap;\n };\n Type get_edge(int i) {\n auto [from, pos] = es[i];\n auto e = G[from][pos];\n auto re = G[e.to][e.rev];\n return Type{from, e.to, e.cap, re.cap};\n }\n void bfs(int s) {\n level.assign(V, -1);\n queue<int> q;\n level[s] = 0;\n q.push(s);\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto &e : G[v]) {\n if (e.cap > 0 && level[e.to] < 0) {\n level[e.to] = level[v] + 1;\n q.push(e.to);\n }\n }\n }\n }\n ll dfs(int v, int t, ll f) {\n if (v == t)\n return f;\n for (int &i = itr[v]; i < (int)G[v].size(); i++) {\n Edge &e = G[v][i];\n if (e.cap > 0 && level[v] < level[e.to]) {\n ll d = dfs(e.to, t, min(f, e.cap));\n if (d > 0) {\n e.cap -= d, G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n ll run(int s, int t) {\n ll ret = 0, f;\n while (bfs(s), level[t] >= 0) {\n itr.assign(V, 0);\n while ((f = dfs(s, t, INF)) > 0)\n ret += f;\n }\n return ret;\n }\n vector<int> cut() {\n vector<int> ret(V);\n rep(v, 0, V) if (level[v] < 0) ret[v] = 1;\n return ret;\n }\n};\n\n/**\n * @brief Maximum Flow\n */\n\ntemplate <class Cap, class Cost> struct MinCostFlow {\n struct X {\n int from, to;\n Cap lb, ub, flow;\n Cost cost;\n };\n struct Edge {\n int to, rev;\n Cap cap;\n Cost cost;\n };\n using P = pair<int, int>;\n int n, m;\n vector<X> es;\n vector<Cap> exc;\n vector<Cost> dual;\n vector<vector<Edge>> g;\n Cost MX;\n MinCostFlow(int _n) : n(_n), m(0), exc(n), dual(n), g(n), MX(0) {}\n void add_edge(int from, int to, Cap lb, Cap ub, Cost cost) {\n m++;\n chmax(MX, cost);\n chmax(MX, -cost);\n es.push_back({from, to, lb, ub, 0, cost});\n }\n void add_excess(int v, Cap c) { exc[v] += c; }\n pair<bool, Cost> run() {\n MaxFlow mf(n + 2);\n int S = n, T = n + 1;\n Cap psum = 0, nsum = 0;\n for (auto &e : es) {\n exc[e.to] += e.lb;\n exc[e.from] -= e.lb;\n mf.add_edge(e.from, e.to, e.ub - e.lb);\n }\n rep(i, 0, n) {\n if (exc[i] > 0) {\n psum += exc[i];\n mf.add_edge(S, i, exc[i]);\n }\n if (exc[i] < 0) {\n nsum += -exc[i];\n mf.add_edge(i, T, -exc[i]);\n }\n }\n\n if (psum != nsum or mf.run(S, T) != psum)\n return {false, 0};\n\n using P = pair<int, int>;\n vector<P> pos;\n rep(i, 0, m) {\n auto e = mf.get_edge(i);\n Cost cost = es[i].cost * n;\n int fid = SZ(g[e.from]), tid = SZ(g[e.to]);\n if (e.from == e.to)\n tid++;\n pos.push_back({e.from, fid});\n g[e.from].push_back({e.to, tid, (int)e.cap, cost});\n g[e.to].push_back({e.from, fid, (int)e.recap, -cost});\n }\n\n // solve\n\n Cost eps = MX * n + 1;\n while (eps > 1) {\n eps = max<Cost>(eps >> 2, 1);\n refine(eps);\n }\n\n Cost ret = 0;\n rep(i, 0, m) {\n auto [from, fid] = pos[i];\n es[i].flow = es[i].ub - g[from][fid].cap;\n ret += es[i].flow * es[i].cost;\n }\n dual.assign(n, 0);\n for (;;) {\n bool upd = 0;\n rep(i, 0, n) {\n for (auto &e : g[i])\n if (e.cap) {\n auto cost = dual[i] + e.cost / n;\n if (chmin(dual[e.to], cost)) {\n upd = 1;\n }\n }\n }\n if (!upd)\n break;\n }\n return {true, ret};\n }\n Cap get_flow(int i) const { return es[i].flow; }\n\n private:\n void refine(Cost &eps) {\n exc.assign(n, 0);\n vector<int> used(n);\n queue<int> que;\n vector<int> iter(n);\n\n auto cost = [&](int from, const Edge &e) {\n return e.cost + dual[from] - dual[e.to];\n };\n auto push = [&](int from, Edge &e, Cap cap) {\n exc[from] -= cap;\n exc[e.to] += cap;\n g[e.to][e.rev].cap += cap;\n e.cap -= cap;\n };\n auto relabel = [&](int v) {\n iter[v] = 0;\n Cost down = MX * (n + 1);\n for (auto &e : g[v])\n if (e.cap) {\n chmin(down, eps + cost(v, e));\n }\n dual[v] -= down;\n que.push(v);\n used[v] = 1;\n };\n\n rep(i, 0, n) {\n for (auto &e : g[i])\n if (e.cap and cost(i, e) < 0) {\n push(i, e, e.cap);\n }\n }\n rep(i, 0, n) if (exc[i] > 0) {\n used[i] = 1;\n que.push(i);\n }\n while (!que.empty()) {\n auto v = que.front();\n que.pop();\n used[v] = 0;\n for (int &i = iter[v]; i < SZ(g[v]); i++) {\n auto &e = g[v][i];\n if (e.cap and cost(v, e) < 0) {\n push(v, e, min(exc[v], e.cap));\n if (!used[e.to] and exc[e.to] > 0) {\n used[e.to] = 1;\n que.push(e.to);\n }\n if (exc[v] == 0)\n break;\n }\n }\n if (exc[v] > 0) {\n relabel(v);\n }\n }\n eps = 0;\n rep(i, 0, n) {\n for (auto &e : g[i])\n if (e.cap) {\n chmax(eps, -cost(i, e));\n }\n }\n }\n};\n\n/**\n * @brief Minimum Cost b-flow\n */\n\nbool f(const pair<int,int>& P1, const pair<int,int>& P2) {\n if (P1.second-P1.first != P2.second-P2.first) return P1.second-P1.first<P2.second-P2.first;\n return P1.first > P2.first;\n}\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<int> U(M), V(M);\n rep(i,0,M) cin >> U[i] >> V[i], U[i]--, V[i]--;\n vector<pair<int,int>> Cand;\n for (int L = 0, R = 0; L < N-1; L++) {\n if (L > R) R = L;\n for (; R < N; R++) {\n MinCostFlow<int,int> G(N+M+2);\n rep(i,0,N) {\n G.add_edge(N+M,i,L,R,0);\n }\n rep(i,0,M) {\n G.add_edge(U[i],i+N,0,1,0);\n G.add_edge(V[i],i+N,0,1,0);\n G.add_edge(i+N,N+M+1,0,1,0);\n }\n G.add_edge(N+M+1,N+M,0,M,-1);\n auto [ok, ret] = G.run();\n if (ok) {\n if (ret == -M) {\n break;\n }\n }\n }\n if (R == N) break;\n Cand.push_back({L,R});\n }\n sort(ALL(Cand),f);\n cout << Cand[0].first << ' ' << Cand[0].second << endl;\n}\n}", "accuracy": 1, "time_ms": 1110, "memory_kb": 5448, "score_of_the_acc": -1.2056, "final_rank": 17 }, { "submission_id": "aoj_1615_9372689", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\n#define rep(i,n) for(ll i=0;i<n;i++)\n\nvoid solve(ll N,ll M){\n vector<vll> G(N,vll(N,0));\n vll E(N,0);\n rep(i,M){\n ll U,V;\n cin>>U>>V;\n U--;V--;\n G[U][V]=1;\n E[V]++; \n }\n while(1){\n ll m=1e18,M=-1e18;\n ll mid=-1,Mid=-1;\n rep(i,N){\n if(E[i]<m){\n m=E[i];\n mid=i;\n }\n if(E[i]>M){\n M=E[i];\n Mid=i;\n }\n }\n if(m==M||m+1==M)break;\n \n bool EX=0;\n queue<ll> Q;\n Q.push(mid);\n vll dep(N,-1);\n dep[mid]=0;\n while(!Q.empty()){\n ll p=Q.front();\n Q.pop();\n rep(i,N){\n if(G[p][i]==1){\n if(dep[i]!=-1)continue;\n dep[i]=dep[p]+1;\n Q.push(i);\n }\n }\n }\n rep(i,N){\n if(dep[i]!=-1&&E[i]>=m+2){\n ll x=i;\n E[i]--;\n E[mid]++;\n while(x!=mid){\n rep(j,N){\n if(G[j][x]==1&&dep[j]==dep[x]-1){\n G[x][j]=1;\n G[j][x]=0;\n x=j;\n break;\n }\n }\n }\n EX=1;\n break;\n }\n }\n if(EX)continue;\n \n Q.push(Mid);\n dep.assign(N,-1);\n dep[Mid]=0;\n while(!Q.empty()){\n ll p=Q.front();\n Q.pop();\n rep(i,N){\n if(G[i][p]==1){\n if(dep[i]!=-1)continue;\n dep[i]=dep[p]+1;\n Q.push(i);\n }\n }\n }\n rep(i,N){\n if(dep[i]!=-1&&E[i]<=M-2){\n ll x=i;\n E[i]++;\n E[Mid]--;\n while(x!=Mid){\n rep(j,N){\n if(G[x][j]==1&&dep[j]==dep[x]-1){\n G[x][j]=0;\n G[j][x]=1;\n x=j;\n break;\n }\n }\n }\n EX=1;\n break;\n }\n }\n if(EX)continue;\n\n break;\n }\n\n sort(E.begin(),E.end());\n cout<<E[0]<<\" \"<<E.back()<<endl;\n\n}\n\nint main() {\n\n ll H,W;\n while (cin >> H>>W) {\n if (H+W == 0)return 0;\n solve(H,W);\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3468, "score_of_the_acc": -0.015, "final_rank": 1 }, { "submission_id": "aoj_1615_9365911", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\ntemplate < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }\ntemplate < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }\ntemplate < class T > vector< T > operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }\ntemplate < class T > vector< T > operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\n#line 1 \"cp-library/src/graph/max_flow.hpp\"\ntemplate < class Cap > struct mf_graph {\n public:\n explicit mf_graph(int n) : n(n), g(n) {}\n \n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from and from < n);\n assert(0 <= to and to < n);\n assert(0 <= cap);\n int m = pos.size();\n pos.push_back({from, g[from].size()});\n int from_id = g[from].size();\n int to_id = g[to].size() + (from == to);\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to; Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = pos.size();\n assert(0 <= i and i < m);\n _edge e = g[pos[i].first][pos[i].second];\n _edge r = g[e.to][e.rev];\n return edge{pos[i].first, e.to, e.cap + r.cap, r.cap};\n }\n\n vector<edge> edges() {\n int m = pos.size();\n vector<edge> res(m);\n for(int i : rep(m)) res[i] = get_edge(i);\n return res;\n }\n\n void change_edge(int i, Cap cap, Cap flow) {\n int m = pos.size();\n assert(0 <= i and i < m);\n assert(0 <= flow and flow <= cap);\n _edge& e = g[pos[i].first][pos[i].second];\n _edge& r = g[e.to][e.rev];\n e.cap = cap - flow;\n r.cap = flow;\n }\n\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s and s < n);\n assert(0 <= t and t < n);\n assert(s != t);\n\n vector<int> level(n), iter(n);\n auto bfs = [&]() {\n fill(level.begin(), level.end(), -1);\n level[s] = 0;\n queue<int> q;\n q.push(s);\n while(not q.empty()) {\n int v = q.front(); q.pop();\n for(_edge e : g[v]) {\n if(e.cap == 0 or level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if(e.to == t) return;\n q.push(e.to);\n }\n }\n };\n\n auto dfs = [&](auto self, int v, Cap up) {\n if(v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for(int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if(level_v <= level[e.to] or g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, min(up - res, g[e.to][e.rev].cap));\n if(d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if(res == up) return res;\n }\n level[v] = n;\n return res;\n };\n\n Cap flow = 0;\n while(flow < flow_limit) {\n bfs();\n if(level[t] == -1) break;\n fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if(f == 0) break;\n flow += f;\n }\n return flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, numeric_limits<Cap>::max());\n }\n\n vector<int> min_cut(int s) {\n vector<int> visited(n, 0);\n queue<int> q;\n q.push(s);\n while(not q.empty()) {\n int p = q.front(); q.pop();\n visited[p] = 1;\n for(_edge e : g[p]) {\n if(e.cap && not visited[e.to]) {\n visited[e.to] = 1;\n q.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int n;\n struct _edge {\n int to, rev; Cap cap;\n };\n vector<pair<int,int>> pos;\n vector<vector<_edge>> g;\n};\n#line 5 \"A.cpp\"\n\npair<int,int> solve(int n, int m) {\n vector<pair<int,int>> e(m);\n for(auto &[u, v] : e) u = in(), v = in(), u--, v--;\n\n auto f = [&](int L, int R) -> bool {\n mf_graph<int> g(n + m + 4);\n const int S = n + m + 0, T = n + m + 1;\n const int SS = n + m + 2, TT = n + m + 3;\n auto V = [&](int i) { return i; };\n auto E = [&](int i) { return n + i; };\n for(int i : rep(m)) {\n auto [u, v] = e[i];\n g.add_edge(V(u), E(i), 1);\n g.add_edge(V(v), E(i), 1);\n g.add_edge(E(i), T, 1);\n }\n for(int v : rep(n)) {\n g.add_edge(S, V(v), R - L);\n g.add_edge(SS, V(v), L);\n g.add_edge(S, TT, L);\n }\n const int SSTT = g.flow(SS, TT);\n const int STT = g.flow(S , TT);\n const int SST = g.flow(SS, T );\n const int ST = g.flow(S , T );\n return STT + ST == m;\n };\n\n int ansL = 0, ansR = n;\n for(int L = 0, R = 0; R <= n; R++) {\n while(L <= R and f(L, R)) {\n tie(ansL, ansR) = make_pair(L, R);\n L++;\n }\n }\n return {ansL, ansR};\n}\n\nint main() {\n while(true) {\n int n = in(), m = in();\n if(make_pair(n, m) == make_pair(0, 0)) return 0;\n auto [l, h] = solve(n, m);\n print(l, h);\n }\n}", "accuracy": 1, "time_ms": 300, "memory_kb": 4268, "score_of_the_acc": -0.4582, "final_rank": 4 }, { "submission_id": "aoj_1615_9340556", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\nstruct Dinic {\nprivate:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\npublic:\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, 0 });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, inf)) res += flow;\n };\n return res;\n }\n};\n\nint main() \n{\n vi<pair<int, int>> ans;\n int n, m;\n while (cin >> n >> m, n) {\n vi<int> u(m), v(m);\n rep(i, m) cin >> u[i] >> v[i];\n rep(i, m) u[i]--;\n rep(i, m) v[i]--;\n\n int high = n, low = 0;\n int S = n + m, T = S + 1, T2 = S + 2;\n\n auto f = [&](int h, int l) {\n Dinic dn(n + m + 3);\n rep(i, m) dn.addedge(S, i, 1);\n rep(i, m) {\n dn.addedge(i, m + u[i], 1);\n dn.addedge(i, m + v[i], 1);\n }\n rep(i, n) {\n dn.addedge(m + i, T, h);\n //dn.addedge(T, m + i, l);\n dn.addedge(m + i, T2, l);\n }\n return dn;\n };\n\n rep(l, n + 1) {\n for(int h = l; h <= min(n, l + high - low); h++) {\n Dinic dn = f(h, l);\n \n int flow = dn.maxflow(S, T);\n if (flow < m) continue;\n\n int flow_l = dn.maxflow(T, T2);\n if (flow_l < l * n) continue;\n\n if (h - l <= high - low) {\n high = h;\n low = l;\n break;\n }\n }\n }\n ans.push_back({ low, high });\n }\n for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1810, "memory_kb": 4000, "score_of_the_acc": -0.6051, "final_rank": 7 }, { "submission_id": "aoj_1615_9340542", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\nstruct Dinic {\nprivate:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\npublic:\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, 0 });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, inf)) res += flow;\n };\n return res;\n }\n};\n\nint main() \n{\n vi<pair<int, int>> ans;\n int n, m;\n while (cin >> n >> m, n) {\n vi<int> u(m), v(m);\n rep(i, m) cin >> u[i] >> v[i];\n rep(i, m) u[i]--;\n rep(i, m) v[i]--;\n\n int high = n, low = 0;\n int S = n + m, T = S + 1, T2 = S + 2;\n\n auto f = [&](int h, int l) {\n Dinic dn(n + m + 3);\n rep(i, m) dn.addedge(S, i, 1);\n rep(i, m) {\n dn.addedge(i, m + u[i], 1);\n dn.addedge(i, m + v[i], 1);\n }\n rep(i, n) {\n dn.addedge(m + i, T, h);\n //dn.addedge(T, m + i, l);\n dn.addedge(m + i, T2, l);\n }\n return dn;\n };\n\n rep(l, n + 1) {\n for(int h = l; h <= min(n, l + high - low); h++) {\n Dinic dn = f(h, l);\n \n int flow = dn.maxflow(S, T);\n if (flow < m) continue;\n\n int flow_l = dn.maxflow(T, T2);\n if (flow_l < l * n) continue;\n\n if (h - l <= high - low) {\n high = h;\n low = l;\n break;\n }\n }\n }\n ans.push_back({ low, high });\n }\n for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n\treturn 0;\n}\n\n\n//\n//struct edge {\n// int to, cap, rev;\n//};\n//\n//int main()\n//{\n// vi<pair<int, int>> ans;\n// int n, m;\n// while (cin >> n >> m, n) {\n// vii<edge> to(n);\n// vi<int> deg(n);\n// rep(i, m) {\n// int u, v;\n// cin >> u >> v;\n// u--, v--;\n// int su = (int)to[u].size();\n// int sv = (int)to[v].size();\n// to[u].push_back({ v, 0, sv });\n// to[v].push_back({ u, 1, su });\n// deg[u]++;\n// }\n//\n// map<int, set<int>> mp;\n// rep(i, n) mp[deg[i]].insert(i);\n//\n// vi<bool> visited(n);\n// auto dfs = [&](auto dfs, int s, int v, int mn = 0)->bool {\n// visited[v] = true;\n// if ((!mn && deg[s] + 2 <= deg[v]) \n// || (mn && deg[v] + 2 <= deg[s])) {\n// mp[deg[v]].erase(v);\n// mp[deg[s]].erase(s);\n// if ((int)mp[deg[v]].size() == 0) mp.erase(deg[v]);\n// if ((int)mp[deg[s]].size() == 0) mp.erase(deg[s]);\n//\n// if (!mn) {\n// deg[v]--;\n// deg[s]++;\n// }\n// else {\n// deg[v]++;\n// deg[s]--;\n// }\n//\n// mp[deg[v]].insert(v);\n// mp[deg[s]].insert(s);\n// return true;\n// }\n// for (auto& elem : to[v]) {\n// if (elem.cap == mn || visited[elem.to]) continue;\n// if (dfs(dfs, s, elem.to, mn)) {\n// elem.cap = mn;\n// to[elem.to][elem.rev].cap = 1 - mn;\n// return true;\n// }\n// }\n// return false;\n// };\n//\n// bool ok = true;\n// while (ok) {\n// ok = false;\n// for (int v : mp.begin()->second) {\n// visited.assign(n, false);\n// ok = dfs(dfs, v, v);\n// if (ok) break;\n// }\n//\n// if (ok) continue;\n//\n// for (int v : mp.rbegin()->second) {\n// visited.assign(n, false);\n// ok = dfs(dfs, v, v, 1);\n// if (ok) break;\n// }\n// }\n// ans.push_back({ mp.begin()->first, mp.rbegin()->first });\n// }\n//\n// for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n// return 0;\n//}", "accuracy": 1, "time_ms": 1800, "memory_kb": 4112, "score_of_the_acc": -0.6598, "final_rank": 12 }, { "submission_id": "aoj_1615_9340540", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\nstruct Dinic {\nprivate:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\npublic:\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, 0 });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, inf)) res += flow;\n };\n return res;\n }\n};\n\nint main() \n{\n vi<pair<int, int>> ans;\n int n, m;\n while (cin >> n >> m, n) {\n vi<int> u(m), v(m);\n rep(i, m) cin >> u[i] >> v[i];\n rep(i, m) u[i]--;\n rep(i, m) v[i]--;\n\n int high = m + 1, low = 0;\n int S = n + m, T = S + 1, T2 = S + 2;\n\n auto f = [&](int h, int l) {\n Dinic dn(n + m + 3);\n rep(i, m) dn.addedge(S, i, 1);\n rep(i, m) {\n dn.addedge(i, m + u[i], 1);\n dn.addedge(i, m + v[i], 1);\n }\n rep(i, n) {\n dn.addedge(m + i, T, h);\n //dn.addedge(T, m + i, l);\n dn.addedge(m + i, T2, l);\n }\n return dn;\n };\n\n rep(l, n + 1) {\n for(int h = l; h <= min(n, l + high - low); h++) {\n Dinic dn = f(h, l);\n \n int flow = dn.maxflow(S, T);\n if (flow < m) continue;\n\n int flow_l = dn.maxflow(T, T2);\n if (flow_l < l * n) continue;\n\n if (h - l <= high - low) {\n high = h;\n low = l;\n break;\n }\n }\n }\n ans.push_back({ low, high });\n }\n for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n\treturn 0;\n}\n\n\n//\n//struct edge {\n// int to, cap, rev;\n//};\n//\n//int main()\n//{\n// vi<pair<int, int>> ans;\n// int n, m;\n// while (cin >> n >> m, n) {\n// vii<edge> to(n);\n// vi<int> deg(n);\n// rep(i, m) {\n// int u, v;\n// cin >> u >> v;\n// u--, v--;\n// int su = (int)to[u].size();\n// int sv = (int)to[v].size();\n// to[u].push_back({ v, 0, sv });\n// to[v].push_back({ u, 1, su });\n// deg[u]++;\n// }\n//\n// map<int, set<int>> mp;\n// rep(i, n) mp[deg[i]].insert(i);\n//\n// vi<bool> visited(n);\n// auto dfs = [&](auto dfs, int s, int v, int mn = 0)->bool {\n// visited[v] = true;\n// if ((!mn && deg[s] + 2 <= deg[v]) \n// || (mn && deg[v] + 2 <= deg[s])) {\n// mp[deg[v]].erase(v);\n// mp[deg[s]].erase(s);\n// if ((int)mp[deg[v]].size() == 0) mp.erase(deg[v]);\n// if ((int)mp[deg[s]].size() == 0) mp.erase(deg[s]);\n//\n// if (!mn) {\n// deg[v]--;\n// deg[s]++;\n// }\n// else {\n// deg[v]++;\n// deg[s]--;\n// }\n//\n// mp[deg[v]].insert(v);\n// mp[deg[s]].insert(s);\n// return true;\n// }\n// for (auto& elem : to[v]) {\n// if (elem.cap == mn || visited[elem.to]) continue;\n// if (dfs(dfs, s, elem.to, mn)) {\n// elem.cap = mn;\n// to[elem.to][elem.rev].cap = 1 - mn;\n// return true;\n// }\n// }\n// return false;\n// };\n//\n// bool ok = true;\n// while (ok) {\n// ok = false;\n// for (int v : mp.begin()->second) {\n// visited.assign(n, false);\n// ok = dfs(dfs, v, v);\n// if (ok) break;\n// }\n//\n// if (ok) continue;\n//\n// for (int v : mp.rbegin()->second) {\n// visited.assign(n, false);\n// ok = dfs(dfs, v, v, 1);\n// if (ok) break;\n// }\n// }\n// ans.push_back({ mp.begin()->first, mp.rbegin()->first });\n// }\n//\n// for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n// return 0;\n//}", "accuracy": 1, "time_ms": 1810, "memory_kb": 4112, "score_of_the_acc": -0.6617, "final_rank": 13 }, { "submission_id": "aoj_1615_9340539", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\nstruct Dinic {\nprivate:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\npublic:\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, 0 });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, inf)) res += flow;\n };\n return res;\n }\n};\n\nint main() \n{\n vi<pair<int, int>> ans;\n int n, m;\n while (cin >> n >> m, n) {\n vi<int> u(m), v(m);\n rep(i, m) cin >> u[i] >> v[i];\n rep(i, m) u[i]--;\n rep(i, m) v[i]--;\n\n int high = m + 1, low = 0;\n int S = n + m, T = S + 1, T2 = S + 2;\n\n auto f = [&](int h, int l) {\n Dinic dn(n + m + 3);\n rep(i, m) dn.addedge(S, i, 1);\n rep(i, m) {\n dn.addedge(i, m + u[i], 1);\n dn.addedge(i, m + v[i], 1);\n }\n rep(i, n) {\n dn.addedge(m + i, T, h);\n //dn.addedge(T, m + i, l);\n dn.addedge(m + i, T2, l);\n }\n return dn;\n };\n\n rep(l, n + 1) {\n for(int h = (high == m + 1 ? l : max(high, l)); h <= min(n, l + high - low); h++) {\n Dinic dn = f(h, l);\n \n int flow = dn.maxflow(S, T);\n if (flow < m) continue;\n\n int flow_l = dn.maxflow(T, T2);\n if (flow_l < l * n) continue;\n\n if (h - l <= high - low) {\n high = h;\n low = l;\n break;\n }\n }\n }\n ans.push_back({ low, high });\n }\n for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n\treturn 0;\n}\n\n\n//\n//struct edge {\n// int to, cap, rev;\n//};\n//\n//int main()\n//{\n// vi<pair<int, int>> ans;\n// int n, m;\n// while (cin >> n >> m, n) {\n// vii<edge> to(n);\n// vi<int> deg(n);\n// rep(i, m) {\n// int u, v;\n// cin >> u >> v;\n// u--, v--;\n// int su = (int)to[u].size();\n// int sv = (int)to[v].size();\n// to[u].push_back({ v, 0, sv });\n// to[v].push_back({ u, 1, su });\n// deg[u]++;\n// }\n//\n// map<int, set<int>> mp;\n// rep(i, n) mp[deg[i]].insert(i);\n//\n// vi<bool> visited(n);\n// auto dfs = [&](auto dfs, int s, int v, int mn = 0)->bool {\n// visited[v] = true;\n// if ((!mn && deg[s] + 2 <= deg[v]) \n// || (mn && deg[v] + 2 <= deg[s])) {\n// mp[deg[v]].erase(v);\n// mp[deg[s]].erase(s);\n// if ((int)mp[deg[v]].size() == 0) mp.erase(deg[v]);\n// if ((int)mp[deg[s]].size() == 0) mp.erase(deg[s]);\n//\n// if (!mn) {\n// deg[v]--;\n// deg[s]++;\n// }\n// else {\n// deg[v]++;\n// deg[s]--;\n// }\n//\n// mp[deg[v]].insert(v);\n// mp[deg[s]].insert(s);\n// return true;\n// }\n// for (auto& elem : to[v]) {\n// if (elem.cap == mn || visited[elem.to]) continue;\n// if (dfs(dfs, s, elem.to, mn)) {\n// elem.cap = mn;\n// to[elem.to][elem.rev].cap = 1 - mn;\n// return true;\n// }\n// }\n// return false;\n// };\n//\n// bool ok = true;\n// while (ok) {\n// ok = false;\n// for (int v : mp.begin()->second) {\n// visited.assign(n, false);\n// ok = dfs(dfs, v, v);\n// if (ok) break;\n// }\n//\n// if (ok) continue;\n//\n// for (int v : mp.rbegin()->second) {\n// visited.assign(n, false);\n// ok = dfs(dfs, v, v, 1);\n// if (ok) break;\n// }\n// }\n// ans.push_back({ mp.begin()->first, mp.rbegin()->first });\n// }\n//\n// for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n// return 0;\n//}", "accuracy": 1, "time_ms": 1260, "memory_kb": 4108, "score_of_the_acc": -0.5569, "final_rank": 6 }, { "submission_id": "aoj_1615_9340285", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\nstruct Dinic {\nprivate:\n const long long inf = 1e18;\n vector<int> dist; //sからの距離\n vector<int> iter; //どこまで調べたか\npublic:\n struct edge {\n int to, rev; long long cap;\n edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}\n };\n\n int n;\n vector<vector<edge>> to;\n\n Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}\n\n void addedge(int u, int v, long long cap) {//u->vにcapの容量\n int su = (int)to[u].size(), sv = (int)to[v].size();\n to[u].push_back({ v, sv, cap });\n to[v].push_back({ u, su, 0 });\n }\n\n long long dfs(int v, int t, long long f) { //t: 終点, f: flow\n if (v == t) return f;\n int sv = (int)to[v].size();\n for (int& i = iter[v]; i < sv; i++) {\n edge& nv = to[v][i];\n if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here\n long long nf = dfs(nv.to, t, min(f, nv.cap));\n if (nf > 0) {\n nv.cap -= nf;\n to[nv.to][nv.rev].cap += nf;\n return nf;\n }\n }\n return 0;\n }\n\n void bfs(int s) {\n dist.assign(n, -1);\n queue<int> q;\n q.push(s);\n dist[s] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.pop();\n for (auto nv : to[v]) {\n if (nv.cap > 0 && dist[nv.to] < 0) {\n dist[nv.to] = dist[v] + 1;\n q.push(nv.to);\n }\n }\n }\n }\n\n long long maxflow(int s, int t) {//s:始点, t:終点\n long long res = 0, flow = 0;\n while (true) {\n bfs(s);\n if (dist[t] < 0) break;\n\n iter.assign(n, 0);\n while (flow = dfs(s, t, inf)) res += flow;\n };\n return res;\n }\n};\n\nint main() \n{\n vi<pair<int, int>> ans;\n int n, m;\n while (cin >> n >> m, n) {\n vi<int> u(m), v(m);\n rep(i, m) cin >> u[i] >> v[i];\n rep(i, m) u[i]--;\n rep(i, m) v[i]--;\n\n int high = m, low = 0;\n int S = n + m, T = S + 1, T2 = S + 2;\n\n auto f = [&](int h, int l) {\n Dinic dn(n + m + 3);\n rep(i, m) dn.addedge(S, i, 1);\n rep(i, m) {\n dn.addedge(i, m + u[i], 1);\n dn.addedge(i, m + v[i], 1);\n }\n rep(i, n) {\n dn.addedge(m + i, T, h);\n //dn.addedge(T, m + i, l);\n dn.addedge(m + i, T2, l);\n }\n return dn;\n };\n\n rep(l, n + 1) {\n for(int h = l; h <= min(n, l + high - low); h++) {\n Dinic dn = f(h, l);\n \n int flow = dn.maxflow(S, T);\n if (flow < m) continue;\n\n int flow_l = dn.maxflow(T, T2);\n if (flow_l < l * n) continue;\n\n if (h - l <= high - low) {\n high = h;\n low = l;\n break;\n }\n }\n }\n ans.push_back({ low, high });\n }\n for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1790, "memory_kb": 4096, "score_of_the_acc": -0.6499, "final_rank": 10 }, { "submission_id": "aoj_1615_9309817", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\ntemplate<class T>\nusing vii = vector<vi<T>>;\n\ntemplate<class T>\nusing viii = vector<vii<T>>;\n\nstruct edge {\n int to, cap, rev;\n};\n\nint main()\n{\n vi<pair<int, int>> ans;\n int n, m;\n while (cin >> n >> m, n) {\n vii<edge> to(n);\n vi<int> deg(n);\n rep(i, m) {\n int u, v;\n cin >> u >> v;\n u--, v--;\n int su = (int)to[u].size();\n int sv = (int)to[v].size();\n to[u].push_back({ v, 0, sv });\n to[v].push_back({ u, 1, su });\n deg[u]++;\n }\n\n map<int, set<int>> mp;\n rep(i, n) mp[deg[i]].insert(i);\n\n vi<bool> visited(n);\n auto dfs = [&](auto dfs, int s, int v, int mn = 0)->bool {\n visited[v] = true;\n if ((!mn && deg[s] + 2 <= deg[v]) \n || (mn && deg[v] + 2 <= deg[s])) {\n mp[deg[v]].erase(v);\n mp[deg[s]].erase(s);\n if ((int)mp[deg[v]].size() == 0) mp.erase(deg[v]);\n if ((int)mp[deg[s]].size() == 0) mp.erase(deg[s]);\n\n if (!mn) {\n deg[v]--;\n deg[s]++;\n }\n else {\n deg[v]++;\n deg[s]--;\n }\n\n mp[deg[v]].insert(v);\n mp[deg[s]].insert(s);\n return true;\n }\n for (auto& elem : to[v]) {\n if (elem.cap == mn || visited[elem.to]) continue;\n if (dfs(dfs, s, elem.to, mn)) {\n elem.cap = mn;\n to[elem.to][elem.rev].cap = 1 - mn;\n return true;\n }\n }\n return false;\n };\n\n bool ok = true;\n while (ok) {\n ok = false;\n for (int v : mp.begin()->second) {\n visited.assign(n, false);\n ok = dfs(dfs, v, v);\n if (ok) break;\n }\n\n if (ok) continue;\n\n for (int v : mp.rbegin()->second) {\n visited.assign(n, false);\n ok = dfs(dfs, v, v, 1);\n if (ok) break;\n }\n }\n ans.push_back({ mp.begin()->first, mp.rbegin()->first });\n }\n\n for (auto elem : ans) cout << elem.first << \" \" << elem.second << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3580, "score_of_the_acc": -0.0566, "final_rank": 2 } ]
aoj_1613_cpp
Deciphering Characters Image data which are left by a mysterious syndicate were discovered. You are requested to analyze the data. The syndicate members used characters invented independently. A binary image corresponds to one character written in black ink on white paper. Although you found many variant images that represent the same character, you discovered that you can judge whether or not two images represent the same character with the surrounding relation of connected components. We present some definitions as follows. Here, we assume that white pixels fill outside of the given image. White connected component : A set of white pixels connected to each other horizontally or vertically (see below). Black connected component : A set of black pixels connected to each other horizontally, vertically, or diagonally (see below). Connected component : A white or a black connected component. Background component : The connected component including pixels outside of the image. Any white pixels on the periphery of the image are thus included in the background component. connected disconnected connected connected Connectedness of white pixels Connectedness of black pixels Let C 1 be a connected component in an image and C 2 be another connected component in the same image with the opposite color. Let's think of a modified image in which colors of all pixels not included in C 1 nor C 2 are changed to that of C 2 . If neither C 1 nor C 2 is the background component, the color of the background component is changed to that of C 2 . We say that C 1 surrounds C 2 in the original image when pixels in C 2 are not included in the background component in the modified image. (see below) Two images represent the same character if both of the following conditions are satisfied. The two images have the same number of connected components. Let S and S' be the sets of connected components of the two images. A bijective function f : S → S' satisfying the following conditions exists. For each connected component C that belongs to S , f ( C ) has the same color as C . For each of C 1 and C 2 belonging to S , f ( C 1 ) surrounds f ( C 2 ) if and only if C 1 surrounds C 2 . Let's see an example. Connected components in the images of the figure below has the following surrounding relations. C 1 surrounds C 2 . C 2 surrounds C 3 . C 2 surrounds C 4 . C' 1 surrounds C' 2 . C' 2 surrounds C' 3 . C' 2 surrounds C' 4 . A bijective function defined as f ( C i ) = C' i for each connected component satisfies the conditions stated above. Therefore, we can conclude that the two images represent the same character. Make a program judging whether given two images represent the same character. Input The input consists of at most 200 datasets. The end of the input is represented by a line containing two zeros. Each dataset is formatted as follows. image 1 image 2 Each image has the following format. h w p (1,1) ... p (1, w ) ... p ( h ,1) ... p ( h , w ) h and w are the height an ...(truncated)
[ { "submission_id": "aoj_1613_10867681", "code_snippet": "#include <bits/stdc++.h>\n \n#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )\n#define REP(i,n) FOR(i,0,n)\n#define YYS(x,arr) for(auto& x:arr)\n#define ALL(x) (x).begin(),(x).end()\n#define SORT(x) sort( (x).begin(),(x).end() )\n#define REVERSE(x) reverse( (x).begin(),(x).end() )\n#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )\n#define PW(x) (1LL<<(x))\n#define SZ(x) ((ll)(x).size())\n#define SHOW(x) cout << #x << \" = \" << x << endl\n#define SHOWA(x,n) for( int yui = 0; yui < n; yui++ ){ cout << x[yui] << \" \"; } cout << endl\n\n#define pb emplace_back\n#define fi first\n#define se second\n\nusing namespace std;\n\ntypedef long double ld;\ntypedef long long int ll;\ntypedef pair<int,int> pi;\ntypedef pair<ll,ll> pl;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<bool> vb;\ntypedef vector<ld> vd;\ntypedef vector<pi> vpi;\ntypedef vector<pl> vpl;\ntypedef vector<vpl> gr;\ntypedef vector<vl> ml;\ntypedef vector<vd> md;\ntypedef vector<vi> mi;\n \nconst ll INF = (ll)1e9 + 10;\nconst ll INFLL = (ll)1e18 + 10;\nconst ld EPS = 1e-12;\nconst ll MOD = 1e9+7;\n \ntemplate<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }\ntemplate<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }\ntemplate<class T> inline T sq( T a ){ return a * a; }\n\nll in(){ long long int x; scanf( \"%lld\" , &x ); return x; }\nchar yuyushiki[1000010]; string stin(){ scanf( \"%s\" , yuyushiki ); return yuyushiki; }\n\n// head\n\nconst int dx[] = { 1 , 0 , -1 , 0 , 1 , -1 , -1 , 1 };\nconst int dy[] = { 0 , -1 , 0 , 1 , -1 , -1 , 1 , 1 };\nconst int type[] = { 4 , 8 };\nconst char opp[] = { '#' , '.' };\n\nconst int N = 10;\n\nll A[N];\nll B[N];\n\nvi G[100010];\n\nint it = 0;\n\nint n, m;\n\nbool used[128][128];\n\nstring s[128];\n\nbool isin( int y , int x ){\n return 0 <= y && y < n && 0 <= x && x < m;\n}\n\nvoid dfs2( int y , int x , int t , vpi &v ){\n // cout << y << \" \" << x << endl;\n used[y][x] = true;\n v.pb( y , x );\n REP( i , type[t] ){\n int ny = y + dy[i];\n int nx = x + dx[i];\n if( isin( ny , nx ) && !used[ny][nx] ){\n if( s[ny][nx] == opp[t] ){\n v.pb( ny , nx );\n } else {\n dfs2( ny , nx , t , v );\n }\n }\n }\n}\n\nint dfs( int y, int x , int t ){\n int cur = it++;\n vpi v(0);\n // cout << \"---\" << endl;\n dfs2( y , x , t , v );\n YYS( w , v ){\n if( !used[w.fi][w.se] ){\n G[cur].pb( dfs( w.fi , w.se , 1-t ) );\n }\n }\n return cur;\n}\n\nint get(){\n n = in();\n m = in();\n if( n == 0 && m == 0 ){\n return -1;\n }\n s[0] = string( m+2 , '.' );\n REP( i , n ){\n s[i+1] = \".\" + stin() + \".\";\n }\n s[n+1] = string( m+2 , '.' );\n n += 2;\n m += 2;\n /*\n REP( i , n ){\n cout << s[i] << endl;\n }\n */\n REP( i , n ){\n REP( j , m ){\n used[i][j] = false;\n }\n }\n return dfs( 0 , 0 , 0 );\n}\n\nll ha( int x , ll a , ll b ){\n ll res = a;\n vl v(0);\n YYS( w , G[x] ){\n v.pb( ha( w , a , b ) );\n }\n SORT( v );\n YYS( w , v ){\n res = ( res * a + w ) % MOD;\n }\n return ( res + b ) % MOD;\n}\n\nint main(){\n\n mt19937 mt(727272);\n \n REP( i , N ){\n A[i] = mt();\n B[i] = mt();\n }\n \n while( 1 ){\n REP( i , 100010 ){\n G[i].clear();\n }\n it = 0;\n int ra = get();\n if( ra == -1 ){\n break;\n }\n int rb = get();\n /*\n REP( i , it ){\n SHOWA( G[i] , SZ(G[i]) );\n }\n */\n bool ok = true;\n REP( i , N ){\n if( ha( ra , A[i] , B[i] ) != ha( rb , A[i] , B[i] ) ){\n ok = false;\n break;\n }\n }\n if( ok ){\n puts( \"yes\" );\n } else {\n puts( \"no\" );\n }\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 6104, "score_of_the_acc": -0.7343, "final_rank": 15 }, { "submission_id": "aoj_1613_10675182", "code_snippet": "// C++ includes used for precompiling -*- C++ -*-\n// Copyright (C) 2003-2013 Free Software Foundation, Inc.\n//\n// This file is part of the GNU ISO C++ Library. This library is free\n// software; you can redistribute it and/or modify it under the\n// terms of the GNU General Public License as published by the\n// Free Software Foundation; either version 3, or (at your option)\n// any later version.\n// This library is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n// Under Section 7 of GPL version 3, you are granted additional\n// permissions described in the GCC Runtime Library Exception, version\n// 3.1, as published by the Free Software Foundation.\n// You should have received a copy of the GNU General Public License and\n// a copy of the GCC Runtime Library Exception along with this program;\n// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see\n// <http://w...content-available-to-author-only...u.org/licenses/>.\n/** @file stdc++.h\n * This is an implementation file for a precompiled header.\n */\n// 17.4.1.2 Headers\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <type_traits>\n#include <typeindex>\n#include <unordered_map>\n#include <unordered_set>\n\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nvector<T> dijkstra(vector<vector<pair<int, T>>> &g, vector<int> start) {\n using P = pair<T, int>;\n vector<T> dp(g.size(), numeric_limits<T>::max());\n priority_queue<P, vector<P>, greater<P>> que;\n for (int s : start) {\n dp[s] = 0;\n que.push({0, s});\n }\n while (que.size()) {\n auto [d, v] = que.top();\n que.pop();\n if (dp[v] != d) continue;\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], d + c)) que.push({dp[u], u});\n }\n }\n return dp;\n}\n\nvector<int> BFS01(vector<vector<pair<int, int>>> &g, vector<int> start) {\n vector<int> dp(g.size(), numeric_limits<int>::max());\n deque<int> que;\n for (int s : start) {\n dp[s] = 0;\n que.push_front(s);\n }\n while (que.size()) {\n auto v = que.front();\n que.pop_front();\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], dp[v] + c)) {\n if (c == 0)\n que.push_front(u);\n else\n que.push_back(u);\n }\n }\n }\n return dp;\n}\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nstruct UnionFind {\n vector<int> par, siz;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n};\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\nvector<vector<int>> read_tree() {\n int h, w;\n cin >> h >> w;\n if (h == 0 && w == 0) exit(0);\n vector<string> s(h + 2);\n s[0] = s[h + 1] = string(w + 2, '.');\n for (int i = 1; i <= h; i++) {\n cin >> s[i];\n s[i] = '.' + s[i] + '.';\n }\n h += 2, w += 2;\n UnionFind uf(h * w);\n rep(i, h) {\n rep(j, w) {\n if (s[i][j] == '.') {\n if (i && s[i - 1][j] == '.') uf.unite(i * w + j, (i - 1) * w + j);\n if (j && s[i][j - 1] == '.') uf.unite(i * w + j, i * w + (j - 1));\n } else {\n if (i && s[i - 1][j] == '#') uf.unite(i * w + j, (i - 1) * w + j);\n if (j && s[i][j - 1] == '#') uf.unite(i * w + j, i * w + (j - 1));\n if (i && j && s[i - 1][j - 1] == '#') uf.unite(i * w + j, (i - 1) * w + (j - 1));\n if (i && j + 1 < w && s[i - 1][j + 1] == '#') uf.unite(i * w + j, (i - 1) * w + (j + 1));\n }\n }\n }\n int C = 0;\n int n = h * w;\n vector<int> cmp(n);\n cmp[uf.find(0)] = C++;\n rep(i, n) {\n if (uf.same(0, i)) continue;\n if (uf.find(i) == i) cmp[i] = C++;\n }\n rep(i, n) cmp[i] = cmp[uf.find(i)];\n vector<vector<int>> g(C);\n rep(i, h) {\n rep(j, w) {\n if (s[i][j] == '.') {\n if (i && s[i - 1][j] == '#') {\n g[cmp[(i - 1) * w + j]].push_back(cmp[i * w + j]);\n g[cmp[i * w + j]].push_back(cmp[(i - 1) * w + j]);\n }\n\n if (j && s[i][j - 1] == '#') {\n g[cmp[i * w + j - 1]].push_back(cmp[i * w + j]);\n g[cmp[i * w + j]].push_back(cmp[i * w + j - 1]);\n }\n\n if (i + 1 < h && s[i + 1][j] == '#') {\n g[cmp[(i + 1) * w + j]].push_back(cmp[i * w + j]);\n g[cmp[i * w + j]].push_back(cmp[(i + 1) * w + j]);\n }\n\n if (j + 1 < w && s[i][j + 1] == '#') {\n g[cmp[i * w + j + 1]].push_back(cmp[i * w + j]);\n g[cmp[i * w + j]].push_back(cmp[i * w + j + 1]);\n }\n }\n }\n }\n rep(i, C) { compress(g[i]); }\n // rep(i, C) { cout << g[i] << endl; }\n return g;\n}\nusing mint = modint<998244353>;\nmint C = 97;\nmint rooted_tree_hash(vector<vector<int>> g) {\n int n = g.size();\n vector<mint> h(n, 1);\n function<void(int, int)> dfs = [&](int v, int p) {\n for (int u : g[v]) {\n if (u == p) continue;\n dfs(u, v);\n h[v] *= (h[u]*h[u] + C);\n }\n };\n dfs(0, -1);\n return h[0];\n}\nvoid solve() {\n auto g1 = read_tree();\n auto g2 = read_tree();\n if (g1.size() != g2.size()) {\n cout << \"no\" << endl;\n return;\n }\n mint h1 = rooted_tree_hash(g1);\n mint h2 = rooted_tree_hash(g2);\n if (h1 == h2)\n cout << \"yes\" << endl;\n else\n cout << \"no\" << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4096, "score_of_the_acc": -0.1355, "final_rank": 1 }, { "submission_id": "aoj_1613_10672975", "code_snippet": "// C++ includes used for precompiling -*- C++ -*-\n// Copyright (C) 2003-2013 Free Software Foundation, Inc.\n//\n// This file is part of the GNU ISO C++ Library. This library is free\n// software; you can redistribute it and/or modify it under the\n// terms of the GNU General Public License as published by the\n// Free Software Foundation; either version 3, or (at your option)\n// any later version.\n// This library is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n// Under Section 7 of GPL version 3, you are granted additional\n// permissions described in the GCC Runtime Library Exception, version\n// 3.1, as published by the Free Software Foundation.\n// You should have received a copy of the GNU General Public License and\n// a copy of the GCC Runtime Library Exception along with this program;\n// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see\n// <http://w...content-available-to-author-only...u.org/licenses/>.\n/** @file stdc++.h\n * This is an implementation file for a precompiled header.\n */\n// 17.4.1.2 Headers\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <type_traits>\n#include <typeindex>\n#include <unordered_map>\n#include <unordered_set>\n\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nvector<T> dijkstra(vector<vector<pair<int, T>>> &g, vector<int> start) {\n using P = pair<T, int>;\n vector<T> dp(g.size(), numeric_limits<T>::max());\n priority_queue<P, vector<P>, greater<P>> que;\n for (int s : start) {\n dp[s] = 0;\n que.push({0, s});\n }\n while (que.size()) {\n auto [d, v] = que.top();\n que.pop();\n if (dp[v] != d) continue;\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], d + c)) que.push({dp[u], u});\n }\n }\n return dp;\n}\n\nvector<int> BFS01(vector<vector<pair<int, int>>> &g, vector<int> start) {\n vector<int> dp(g.size(), numeric_limits<int>::max());\n deque<int> que;\n for (int s : start) {\n dp[s] = 0;\n que.push_front(s);\n }\n while (que.size()) {\n auto v = que.front();\n que.pop_front();\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], dp[v] + c)) {\n if (c == 0)\n que.push_front(u);\n else\n que.push_back(u);\n }\n }\n }\n return dp;\n}\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nstruct UnionFind {\n vector<int> par, siz;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n};\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\nvector<vector<int>> read_tree() {\n int h, w;\n cin >> h >> w;\n if (h == 0 && w == 0) exit(0);\n vector<string> s(h + 2);\n s[0] = s[h + 1] = string(w + 2, '.');\n for (int i = 1; i <= h; i++) {\n cin >> s[i];\n s[i] = '.' + s[i] + '.';\n }\n h += 2, w += 2;\n UnionFind uf(h * w);\n rep(i, h) {\n rep(j, w) {\n if (s[i][j] == '.') {\n if (i && s[i - 1][j] == '.') uf.unite(i * w + j, (i - 1) * w + j);\n if (j && s[i][j - 1] == '.') uf.unite(i * w + j, i * w + (j - 1));\n } else {\n if (i && s[i - 1][j] == '#') uf.unite(i * w + j, (i - 1) * w + j);\n if (j && s[i][j - 1] == '#') uf.unite(i * w + j, i * w + (j - 1));\n if (i && j && s[i - 1][j - 1] == '#') uf.unite(i * w + j, (i - 1) * w + (j - 1));\n if (i && j + 1 < w && s[i - 1][j + 1] == '#') uf.unite(i * w + j, (i - 1) * w + (j + 1));\n }\n }\n }\n int C = 0;\n int n = h * w;\n vector<int> cmp(n);\n cmp[uf.find(0)] = C++;\n rep(i, n) {\n if (uf.same(0, i)) continue;\n if (uf.find(i) == i) cmp[i] = C++;\n }\n rep(i, n) cmp[i] = cmp[uf.find(i)];\n vector<vector<int>> g(C);\n rep(i, h) {\n rep(j, w) {\n if (s[i][j] == '.') {\n if (i && s[i - 1][j] == '#') {\n g[cmp[(i - 1) * w + j]].push_back(cmp[i * w + j]);\n g[cmp[i * w + j]].push_back(cmp[(i - 1) * w + j]);\n }\n\n if (j && s[i][j - 1] == '#') {\n g[cmp[i * w + j - 1]].push_back(cmp[i * w + j]);\n g[cmp[i * w + j]].push_back(cmp[i * w + j - 1]);\n }\n\n if (i + 1 < h && s[i + 1][j] == '#') {\n g[cmp[(i + 1) * w + j]].push_back(cmp[i * w + j]);\n g[cmp[i * w + j]].push_back(cmp[(i + 1) * w + j]);\n }\n\n if (j + 1 < w && s[i][j + 1] == '#') {\n g[cmp[i * w + j + 1]].push_back(cmp[i * w + j]);\n g[cmp[i * w + j]].push_back(cmp[i * w + j + 1]);\n }\n }\n }\n }\n rep(i, C) { compress(g[i]); }\n // rep(i, C) { cout << g[i] << endl; }\n return g;\n}\nusing mint = modint<998244353>;\nmint C = 97;\nmint rooted_tree_hash(vector<vector<int>> g) {\n int n = g.size();\n vector<mint> h(n, 1);\n function<void(int, int)> dfs = [&](int v, int p) {\n for (int u : g[v]) {\n if (u == p) continue;\n dfs(u, v);\n h[v] *= (h[u] + C);\n }\n };\n dfs(0, -1);\n return h[0];\n}\nvoid solve() {\n auto g1 = read_tree();\n auto g2 = read_tree();\n if (g1.size() != g2.size()) {\n cout << \"no\" << endl;\n return;\n }\n mint h1 = rooted_tree_hash(g1);\n mint h2 = rooted_tree_hash(g2);\n if (h1 == h2)\n cout << \"yes\" << endl;\n else\n cout << \"no\" << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 4096, "score_of_the_acc": -0.1355, "final_rank": 1 }, { "submission_id": "aoj_1613_10670417", "code_snippet": "#ifdef LOCAL\n#define _GLIBCXX_DEBUG\n#pragma GCC optimize(\"O0\")\n#else\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#endif\n\n#include <bits/stdc++.h>\n// #include <bits/extc++.h>\nusing namespace std;\n\n#include <atcoder/all>\n\n// #include <atcoder/modint>\n// using mint = atcoder::modint998244353;\n// istream& operator>>(istream& in, mint& x) {\n// long long a;\n// in >> a;\n// x = a;\n// return in;\n// }\n// ostream& operator<<(ostream& out, const mint& x) {\n// return out << x.val();\n// }\n\nusing ll = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing i128 = __int128;\nusing u128 = unsigned __int128;\nusing f128 = __float128;\n\ntemplate <class T>\nconstexpr T infty = 0;\ntemplate <>\nconstexpr int infty<int> = 1'000'000'000;\ntemplate <>\nconstexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;\ntemplate <>\nconstexpr u32 infty<u32> = infty<int>;\ntemplate <>\nconstexpr u64 infty<u64> = infty<ll>;\ntemplate <>\nconstexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;\ntemplate <>\nconstexpr double infty<double> = infty<ll>;\ntemplate <>\nconstexpr long double infty<long double> = infty<ll>;\n\nusing pi = pair<int, int>;\nusing pl = pair<ll, ll>;\nusing vi = vector<int>;\nusing vl = vector<ll>;\ntemplate <class T>\nusing vc = vector<T>;\ntemplate <class T>\nusing vvc = vector<vc<T>>;\nusing vvi = vvc<int>;\nusing vvl = vvc<ll>;\ntemplate <class T>\nusing vvvc = vector<vvc<T>>;\ntemplate <class T>\nusing vvvvc = vector<vvvc<T>>;\ntemplate <class T>\nusing vvvvvc = vector<vvvvc<T>>;\ntemplate <class T>\nusing pqg = std::priority_queue<T, vector<T>, greater<T>>;\ntemplate <class T, class U>\nusing umap = unordered_map<T, U>;\n\n// template <typename K>\n// using tree = __gnu_pbds::tree<K, __gnu_pbds::null_type, std::less<>,\n// __gnu_pbds::rb_tree_tag,\n// __gnu_pbds::tree_order_statistics_node_update>;\n\n#define vv(type, name, h, ...) \\\n vector<vector<type>> name(h, vector<type>(__VA_ARGS__))\n#define vvv(type, name, h, w, ...) \\\n vector<vector<vector<type>>> name( \\\n h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))\n#define vvvv(type, name, a, b, c, ...) \\\n vector<vector<vector<vector<type>>>> name( \\\n a, vector<vector<vector<type>>>( \\\n b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))\n\n// FOR(a) := for (ll _ = 0; _ < (ll)a; ++_)\n// FOR(i, a) := for (ll i = 0; i < (ll)a; ++i)\n// FOR(i, a, b) := for (ll i = a; i < (ll)b; ++i)\n// FOR(i, a, b, c) := for (ll i = a; i < (ll)b; i += (c))\n// FOR_R(a) := for (ll i = (a) - 1; i >= 0; --i)\n// FOR_R(i, a) := for (ll i = (a) - 1; i >= 0; --i)\n// FOR_R(i, a, b) := for (ll i = (b) - 1; i >= (ll)a; --i)\n#define FOR1(a) for (ll _ = 0; _ < (ll)a; ++_)\n#define FOR2(i, a) for (ll i = 0; i < (ll)a; ++i)\n#define FOR3(i, a, b) for (ll i = a; i < (ll)b; ++i)\n#define FOR4(i, a, b, c) for (ll i = a; i < (ll)b; i += (c))\n#define FOR1_R(a) for (ll i = (a) - 1; i >= 0; --i)\n#define FOR2_R(i, a) for (ll i = (a) - 1; i >= 0; --i)\n#define FOR3_R(i, a, b) for (ll i = (b) - 1; i >= (ll)a; --i)\n#define overload4(a, b, c, d, e, ...) e\n#define overload3(a, b, c, d, ...) d\n#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)\n#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)\n\n#define FOR_subset(t, s) \\\n for (int t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\nint popcnt_mod_2(int x) { return __builtin_parity(x); }\nint popcnt_mod_2(u32 x) { return __builtin_parity(x); }\nint popcnt_mod_2(ll x) { return __builtin_parityll(x); }\nint popcnt_mod_2(u64 x) { return __builtin_parityll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\ntemplate <typename T>\nT floor(T a, T b) {\n return a / b - (a % b && (a ^ b) < 0);\n}\ntemplate <typename T>\nT ceil(T x, T y) {\n return floor(x + y - 1, y);\n}\ntemplate <typename T>\nT bmod(T x, T y) {\n return x - y * floor(x, y);\n}\ntemplate <typename T>\npair<T, T> divmod(T x, T y) {\n T q = floor(x, y);\n return {q, x - q * y};\n}\n\ntemplate <typename T, typename U>\nT POW(U x_, int n) {\n T x = x_;\n T ret = 1;\n while (n > 0) {\n if (n & 1) ret *= x;\n x *= x;\n n >>= 1;\n }\n return ret;\n}\n\ntemplate <typename T, typename U>\nT SUM(const vector<U> &A) {\n T sm = 0;\n for (auto &&a : A) sm += a;\n return sm;\n}\n\n#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define UNIQUE(x) \\\n sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()\n\ntemplate <class T, class S>\ninline bool chmax(T &a, const S &b) {\n return (a < b ? a = b, 1 : 0);\n}\ntemplate <class T, class S>\ninline bool chmin(T &a, const S &b) {\n return (a > b ? a = b, 1 : 0);\n}\n\n// ? は -1\nvc<int> s_to_vi(const string &S, char first_char) {\n vc<int> A(S.size());\n FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }\n return A;\n}\n\ntemplate <typename T, typename U>\nvector<T> cumsum(vector<U> &A, int off = 1) {\n int N = A.size();\n vector<T> B(N + 1);\n FOR(i, N) { B[i + 1] = B[i] + A[i]; }\n if (off == 0) B.erase(B.begin());\n return B;\n}\n\ntemplate <typename T>\nvector<int> argsort(const vector<T> &A) {\n vector<int> ids(A.size());\n iota(all(ids), 0);\n sort(all(ids),\n [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });\n return ids;\n}\n\n// A[I[0]], A[I[1]], ...\ntemplate <typename T>\nvc<T> rearrange(const vc<T> &A, const vc<int> &I) {\n vc<T> B(I.size());\n FOR(i, I.size()) B[i] = A[I[i]];\n return B;\n}\n\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\n\nvoid print(){\n cout << '\\n';\n}\ntemplate<class T>\nvoid print(const T& a){\n cout << a << '\\n';\n}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\ntemplate<class T>\nvoid print(vector<T> &a){\n for (int i = 0; i < (int)a.size(); ++i) {\n cout << a[i] << \" \\n\"[i == (int)a.size() - 1];\n }\n}\ntemplate<class T>\nvoid print(vector<T> &&a){\n for (int i = 0; i < (int)a.size(); ++i) {\n cout << a[i] << \" \\n\"[i == (int)a.size() - 1];\n }\n}\ntemplate<class T>\nvoid print(vector<vector<T>> &a){\n for (int i = 0; i < (int)a.size(); ++i) {\n for (int j = 0; j < (int)a[i].size(); ++j) {\n cout << a[i][j] << \" \\n\"[j == (int)a[i].size() - 1];\n }\n }\n}\ntemplate<class T>\nvoid print(vector<vector<T>> &&a){\n for (int i = 0; i < (int)a.size(); ++i) {\n for (int j = 0; j < (int)a[i].size(); ++j) {\n cout << a[i][j] << \" \\n\"[j == (int)a[i].size() - 1];\n }\n }\n}\nvoid YESNO(bool b) { cout << (b ? \"YES\" : \"NO\") << endl; }\nvoid YesNo(bool b) { cout << (b ? \"Yes\" : \"No\") << endl; }\n\n#ifdef LOCAL\n// https://zenn.dev/sassan/articles/19db660e4da0a4\n#include \"/Library/cpp-dump/dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\n// CPP_DUMP_DEFINE_EXPORT_OBJECT(mint, val());\n#else\n#define dump(...)\n#define CPP_DUMP_SET_OPTION(...)\n#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)\n#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)\n#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)\n#endif\n\n\n//----------------------------------------------------------------\n\nstring stroftree() {\n ll h, w;\n cin >> h >> w;\n if (h == 0 && w == 0) exit(0);\n h += 2;\n w += 2;\n vc<string> p(h);\n p[0] = string(w, '.');\n FOR(i, 1, h - 1) {\n string s;\n cin >> s;\n p[i] = '.' + s + '.';\n }\n p[h-1] = string(w, '.');\n atcoder::dsu uf(h * w);\n FOR(i, h) FOR(j, w - 1) {\n ll idx = i * w + j;\n if (p[i][j] == p[i][j+1]) {\n uf.merge(idx, idx + 1);\n }\n }\n FOR(i, h - 1) FOR(j, w) {\n ll idx = i * w + j;\n if (p[i][j] == p[i+1][j]) {\n uf.merge(idx, idx + w);\n }\n }\n FOR(i, h - 1) FOR(j, 1, w) {\n ll a = i * w + j;\n ll b = (i + 1) * w + j - 1;\n if (p[i][j] == '#' && p[i+1][j-1] == '#') {\n uf.merge(a, b);\n }\n }\n FOR(i, h - 1) FOR(j, w - 1) {\n ll a = i * w + j;\n ll b = (i + 1) * w + j + 1;\n if (p[i][j] == '#' && p[i+1][j+1] == '#') {\n uf.merge(a, b);\n }\n }\n vv(ll, lbl, h, w);\n auto groups = uf.groups();\n FOR(gpi, groups.size()) {\n FOR(pti, groups[gpi].size()) {\n ll idx = groups[gpi][pti];\n ll i = idx / w;\n ll j = idx % w;\n lbl[i][j] = gpi;\n }\n }\n // dump(lbl);\n ll n = groups.size();\n ll di[] = {0, 0, 1, -1};\n ll dj[] = {1, -1, 0, 0};\n vc<set<ll>> vs(n);\n FOR(i, h) FOR(j, w) {\n FOR(k, 4) {\n ll ni = i + di[k];\n ll nj = j + dj[k];\n if (ni < 0 || ni >= h || nj < 0 || nj >= w) {\n continue;\n }\n if (lbl[i][j] == lbl[ni][nj]) continue;\n vs[lbl[i][j]].emplace(lbl[ni][nj]);\n }\n }\n ll slbl = lbl[0][0];\n vc<bool> used(n);\n vvl G(n);\n queue<ll> que;\n que.emplace(slbl);\n while (!que.empty()) {\n ll frm = que.front();\n que.pop();\n used[frm] = true;\n for (auto x : vs[frm]) {\n if (used[x]) continue;\n G[frm].emplace_back(x);\n que.emplace(x);\n }\n }\n auto dfs = [&](auto&& self, ll v) -> string {\n vc<string> ss(G[v].size());\n FOR(i, G[v].size()) {\n ss[i] = '(' + self(self, G[v][i]) + ')';\n }\n sort(all(ss));\n string ret = \"\";\n FOR(i, ss.size()) {\n ret += ss[i];\n }\n return ret;\n };\n return dfs(dfs, slbl);\n}\n\nvoid solve() {\n string s = stroftree();\n string t = stroftree();\n dump(s, t);\n if (s == t) {\n print(\"yes\");\n } else {\n print(\"no\");\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(20);\n CPP_DUMP_SET_OPTION(max_line_width, 80);\n CPP_DUMP_SET_OPTION(log_label_func, cpp_dump::log_label::filename());\n CPP_DUMP_SET_OPTION(enable_asterisk, true);\n while (true) {\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4196, "score_of_the_acc": -0.3279, "final_rank": 7 }, { "submission_id": "aoj_1613_10606879", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\n#include <stdlib.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define all(x) (x).begin(), (x).end()\n//constexpr ll MOD = 1000000007;\nconstexpr ll MOD = 998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 1LL<<60;\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\nll gcd(ll a, ll b){\n if(a%b == 0){\n return b;\n }else{\n return gcd(b, a%b);\n }\n}\n\nll lcm(ll a, ll b){\n return a*b / gcd(a, b);\n}\n\nll powMod(ll x, ll n) {\n if (n == 0) return 1 % MOD;\n ll val = powMod(x, n / 2);\n val *= val;\n val %= MOD;\n if (n % 2 == 1) val *= x;\n return val % MOD;\n}\n\nclass UnionFind {\n vector<ll> parent, maxi, mini;\n inline ll root(ll n){\n return (parent[n] < 0? n:parent[n] = root(parent[n]));\n }\npublic:\n UnionFind(ll n_ = 1): parent(n_, -1), maxi(n_), mini(n_){\n iota(maxi.begin(), maxi.end(), 0);\n iota(mini.begin(), mini.end(), 0);\n }\n\n inline bool same(ll x, ll y){\n return root(x) == root(y);\n }\n\n inline void unite(ll x, ll y){\n ll rx = root(x);\n ll ry = root(y);\n if(rx == ry) return;\n if(parent[rx] > parent[ry]) swap(rx, ry);\n parent[rx] += parent[ry];\n parent[ry] = rx;\n maxi[rx] = std::max(maxi[rx],maxi[ry]);\n mini[rx] = std::min(mini[rx],mini[ry]);\n }\n\n inline ll min(ll x){\n return mini[root(x)];\n }\n\n inline ll max(ll x){\n return maxi[root(x)];\n }\n\n inline ll size(ll x){\n return (-parent[root(x)]);\n }\n\n inline ll operator[](ll x){\n return root(x);\n }\n\n map<ll,vector<ll>> groups(){\n map<ll,vector<ll>> mp;\n rep(i,0,parent.size())mp[root(i)].push_back(i);\n return mp;\n }\n};\n\nstring solve(vector<string> p){\n ll h = p.size();\n ll w = p[0].size();\n UnionFind uf(h*w);\n rep(i,0,h)rep(j,0,w){\n if(i < h-1 && p[i][j] == p[i+1][j]) uf.unite(i*w+j, (i+1)*w+j);\n if(j < w-1 && p[i][j] == p[i][j+1]) uf.unite(i*w+j, i*w+j+1);\n if(i < h-1 && j < w-1 && p[i][j] == p[i+1][j+1] && p[i][j] == '#') uf.unite(i*w+j, (i+1)*w+j+1);\n if(i > 0 && j < w-1 && p[i][j] == p[i-1][j+1] && p[i][j] == '#') uf.unite(i*w+j, (i-1)*w+j+1);\n }\n auto tmp = uf.groups();\n map<ll,set<ll>> g;\n rep(i,0,h)rep(j,0,w){\n if(i < h-1 && p[i][j] != p[i+1][j]){\n g[uf[i*w+j]].insert(uf[(i+1)*w+j]);\n g[uf[(i+1)*w+j]].insert(uf[i*w+j]);\n }\n if(j < w-1 && p[i][j] != p[i][j+1]){\n g[uf[i*w+j]].insert(uf[i*w+j+1]);\n g[uf[i*w+j+1]].insert(uf[i*w+j]);\n }\n }\n vector<ll> check(h*w,0);\n auto dfs = [&](auto dfs, ll v, ll par) -> string {\n // cout << v << \" \" << par << endl;\n check[v] = 1;\n string res = \"(\";\n vector<string> vec;\n for(auto nv: g[v]){\n if(check[nv]) continue;\n string x = dfs(dfs, nv, v);\n vec.push_back(x);\n }\n sort(all(vec));\n for(auto x: vec) res += x;\n res.push_back(')');\n return res;\n };\n return dfs(dfs, uf[0], -1); \n}\n\nstring solve_(vector<string> p){\n ll h = p.size(), w = p[0].size();\n vector<string> np(h+2, string(w+2, '.'));\n rep(i,0,h)rep(j,0,w) np[i+1][j+1] = p[i][j];\n return solve(np);\n}\n\nint main() {\n while(true){\n ll h1, w1; cin >> h1 >> w1;\n if(h1*w1 == 0) break;\n vector<string> p1(h1);\n rep(i,0,h1) cin >> p1[i];\n string s1 = solve_(p1);\n ll h2, w2; cin >> h2 >> w2;\n vector<string> p2(h2);\n rep(i,0,h2) cin >> p2[i];\n string s2 = solve_(p2);\n if(s1 == s2) cout << \"yes\" << endl;\n else cout << \"no\" << endl;\n // cout << s1 << endl << s2 << endl << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 4296, "score_of_the_acc": -0.4369, "final_rank": 10 }, { "submission_id": "aoj_1613_10578410", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &p){\n os << p.first << \" \" << p.second;\n return os;\n}\ntemplate <typename T, typename U>\nistream &operator>>(istream &is, pair<T, U> &p){\n is >> p.first >> p.second;\n return is;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v){\n int s = (int)v.size();\n for (int i = 0; i < s; i++) os << (i ? \" \" : \"\") << v[i];\n return os;\n}\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v){\n for (auto &x : v) is >> x;\n return is;\n}\n\nvoid in() {}\ntemplate <typename T, class... U>\nvoid in(T &t, U &...u){\n cin >> t;\n in(u...);\n}\n\nvoid out() { cout << \"\\n\"; }\ntemplate <typename T, class... U, char sep = ' '>\nvoid out(const T &t, const U &...u){\n cout << t;\n if (sizeof...(u)) cout << sep;\n out(u...);\n}\n\ntemplate<typename T>\nvoid out(const vector<vector<T>> &vv){\n int s = (int)vv.size();\n for (int i = 0; i < s; i++) out(vv[i]);\n}\n\nstruct IoSetup {\n IoSetup(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(7);\n }\n} iosetup_noya2;\n\nconst int iinf = 1'000'000'007;\nconst long long linf = 2'000'000'000'000'000'000LL;\nconst long long mod998 = 998244353;\nconst long long mod107 = 1000000007;\nconst long double pi = 3.14159265358979323;\nconst vector<int> dx = {0,1,0,-1,1,1,-1,-1};\nconst vector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\n\nvoid yes(){ cout << \"Yes\\n\"; }\nvoid no(){ cout << \"No\\n\"; }\nvoid YES(){ cout << \"YES\\n\"; }\nvoid NO(){ cout << \"NO\\n\"; }\nvoid yn(bool t){ t ? yes() : no(); }\nvoid YN(bool t){ t ? YES() : NO(); }\n\n\nunsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){\n if (a == 0 || b == 0) return a + b;\n int n = __builtin_ctzll(a); a >>= n;\n int m = __builtin_ctzll(b); b >>= m;\n while (a != b) {\n int mm = __builtin_ctzll(a - b);\n bool f = a > b;\n unsigned long long c = f ? a : b;\n b = f ? b : a;\n a = (c - b) >> mm;\n }\n return a << min(n, m);\n}\n\ntemplate<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(abs(a),abs(b))); }\n\nlong long sqrt_fast(long long n) {\n if (n <= 0) return 0;\n long long x = sqrt(n);\n while ((x + 1) * (x + 1) <= n) x++;\n while (x * x > n) x--;\n return x;\n}\n\ntemplate<typename T> T floor_div(const T n, const T d) {\n assert(d != 0);\n return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);\n}\n\ntemplate<typename T> T ceil_div(const T n, const T d) {\n assert(d != 0);\n return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);\n}\n\ntemplate<typename T> void uniq(vector<T> &v){\n sort(v.begin(),v.end());\n v.erase(unique(v.begin(),v.end()),v.end());\n}\n\ntemplate <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }\n\ntemplate <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }\n\ntemplate<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }\n\n} // namespace noya2\n\n#define rep(i,n) for (int i = 0; i < (int)(n); i++)\n#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)\n#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)\n#define all(v) (v).begin(),(v).end()\n\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing pil = pair<int,ll>;\nusing pli = pair<ll,int>;\n\nnamespace noya2{\n\n/* ~ (. _________ . /) */\n\n}\n\nusing namespace noya2;\n\n\n#include <cassert>\n#include <concepts>\n\ntemplate <int m>\nstruct static_modint {\n using mint = static_modint;\n public:\n static constexpr int mod() { return m; }\n static mint raw(int v) {\n mint x;\n x._v = v;\n return x;\n }\n constexpr static_modint() : _v(0) {}\n template<std::signed_integral T>\n constexpr static_modint(T v){\n long long x = (long long)(v % (long long)(umod()));\n if (x < 0) x += umod();\n _v = (unsigned int)(x);\n }\n template<std::unsigned_integral T>\n constexpr static_modint(T v){\n _v = (unsigned int)(v % umod());\n }\n constexpr unsigned int val() const { return _v; }\n mint& operator++() {\n _v++;\n if (_v == umod()) _v = 0;\n return *this;\n }\n mint& operator--() {\n if (_v == 0) _v = umod();\n _v--;\n return *this;\n }\n mint operator++(int) {\n mint result = *this;\n ++*this;\n return result;\n }\n mint operator--(int) {\n mint result = *this;\n --*this;\n return result;\n }\n constexpr mint& operator+=(const mint& rhs) {\n _v += rhs._v;\n if (_v >= umod()) _v -= umod();\n return *this;\n }\n constexpr mint& operator-=(const mint& rhs) {\n _v -= rhs._v;\n if (_v >= umod()) _v += umod();\n return *this;\n }\n constexpr mint& operator*=(const mint& rhs) {\n unsigned long long z = _v;\n z *= rhs._v;\n _v = (unsigned int)(z % umod());\n return *this;\n }\n constexpr mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }\n constexpr mint operator+() const { return *this; }\n constexpr mint operator-() const { return mint() - *this; }\n constexpr mint pow(long long n) const {\n assert(0 <= n);\n mint x = *this, r = 1;\n while (n) {\n if (n & 1) r *= x;\n x *= x;\n n >>= 1;\n }\n return r;\n }\n constexpr mint inv() const {\n assert(_v);\n return pow(umod() - 2);\n }\n friend constexpr mint operator+(const mint& lhs, const mint& rhs) {\n return mint(lhs) += rhs;\n }\n friend constexpr mint operator-(const mint& lhs, const mint& rhs) {\n return mint(lhs) -= rhs;\n }\n friend constexpr mint operator*(const mint& lhs, const mint& rhs) {\n return mint(lhs) *= rhs;\n }\n friend constexpr mint operator/(const mint& lhs, const mint& rhs) {\n return mint(lhs) /= rhs;\n }\n friend constexpr bool operator==(const mint& lhs, const mint& rhs) {\n return lhs._v == rhs._v;\n }\n friend constexpr bool operator!=(const mint& lhs, const mint& rhs) {\n return lhs._v != rhs._v;\n }\n friend std::ostream &operator<<(std::ostream &os, const mint& p) {\n return os << p.val();\n }\n friend std::istream &operator>>(std::istream &is, mint &a) {\n long long t; is >> t;\n a = mint(t);\n return (is);\n }\n\n private:\n unsigned int _v;\n static constexpr unsigned int umod() { return m; }\n};\n\nusing mint = static_modint<998244353>;\n\nvector<mint> wdep;\n\nvoid init(){\n wdep.resize(1001001);\n mt19937_64 mt(-1);\n rep(i,wdep.size()){\n wdep[i] = mt();\n }\n}\n\null calc_hash(int h, int w){\n vector<string> a(h+4,string(w+4,'?'));\n rep(i,h){\n string s; in(s);\n rep(j,w){\n a[i+2][j+2] = s[j];\n }\n }\n vector done(h+4,vector<bool>(w+4,false));\n auto findcc = [&](int sx, int sy){\n int kk = (a[sx][sy] == '.' ? 4 : 8);\n vector<pii> ret;\n queue<pii> que;\n que.emplace(sx,sy);\n done[sx][sy] = true;\n while (!que.empty()){\n auto [x, y] = que.front(); que.pop();\n ret.emplace_back(x,y);\n rep(k,kk){\n int nx = x + dx[k];\n int ny = y + dy[k];\n if (!done[nx][ny] && a[sx][sy] == a[nx][ny]){\n done[nx][ny] = true;\n que.emplace(nx,ny);\n }\n }\n }\n return ret;\n };\n auto dfs = [&](auto sfs, vector<pii> cc) -> pair<mint,int> {\n // out(cc);\n int hei = 0;\n mint prd = 1;\n for (auto [ix, iy] : cc){\n rep(k,4){\n int nx = ix + dx[k];\n int ny = iy + dy[k];\n if (!(range(0,nx,h+4) && range(0,ny,w+4))) continue;\n if (done[nx][ny]) continue;\n auto ncc = findcc(nx,ny);\n auto [dp, he] = sfs(sfs,ncc);\n chmax(hei,he+1);\n prd *= dp;\n }\n }\n return {prd+wdep[hei],hei};\n };\n vector<pii> cc;\n rep(i,h+4) rep(j,w+4){\n if (i == 0 || i == h+3 || j == 0 || j == w+3){\n a[i][j] = '#';\n done[i][j] = true;\n cc.emplace_back(i,j);\n }\n else if (i == 1 || i == h+2 || j == 1 || j == w+2){\n a[i][j] = '.';\n }\n }\n // rep(i,h+4) out(a[i]);\n return dfs(dfs,cc).first.val();\n}\n\nvoid solve(int h1, int w1){\n ull x1 = calc_hash(h1,w1);\n int h2, w2; in(h2,w2);\n ull x2 = calc_hash(h2,w2);\n out(x1 == x2 ? \"yes\" : \"no\");\n}\n\nint main(){\n init();\n while (true){\n int h, w; in(h,w);\n if (h == 0) break;\n solve(h,w);\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 7464, "score_of_the_acc": -1.0833, "final_rank": 18 }, { "submission_id": "aoj_1613_10275787", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n\nclass UnionFind\n {\n private:\n vector<ll> p;\n vector<ll> rank;\n public:\n UnionFind(ll n){\n p.resize(n,-1);\n rank.resize(n,1);\n }\n ll find(ll x){\n if(p[x]<0)return x;\n return p[x]=find(p[x]);\n }\n void unite(ll x,ll y){\n x = find(x);\n y = find(y);\n if(x == y)return;\n if(rank[x] > rank[y])swap(x,y);\n if(rank[x] == rank[y])rank[y]++;\n p[y] += p[x];\n p[x] = y;\n }\n ll size(ll x){\n return -p[find(x)];\n }\n bool same(ll x, ll y){\n return find(x) == find(y);\n }\n };\n \nvector<int> dx = {1,0,-1,0,1,1,-1,-1};\nvector<int> dy = {0,1,0,-1,1,-1,1,-1};\n\nconst ll mod = 998244353;\nconst ll X = 19960515;\n\nint main(){\n\tauto f = [&](int H , int W , vector<vector<int>> G)->ll{\n\t\tUnionFind uf(H*W);\n\t\tfor(int i = 0; i < H; i++){\n\t\t\tfor(int j = 0; j < W; j++){\n\t\t\t\tfor(int k = 0; k < (G[i][j] == 1 ? 8:4); k++){\n\t\t\t\t\tint ni = i + dx[k];\n\t\t\t\t\tint nj = j + dy[k];\n\t\t\t\t\tif(ni < 0 || ni >= H)continue;\n\t\t\t\t\tif(nj < 0 || nj >= W)continue;\n\t\t\t\t\tif(G[i][j] != G[ni][nj])continue;\n\t\t\t\t\tuf.unite(i*W+j , ni*W+nj);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint N = 0;\n\t\tvector<int> idx(H*W,-1);\n\t\tfor(int i = 0; i < H*W; i++)if(uf.find(i) == i){\n\t\t\tidx[i] = N;\n\t\t\tN++;\n\t\t}\n\t\tvector<set<int>> graph(N);\n\t\tfor(int i = 0; i < H; i++){\n\t\t\tfor(int j = 0; j < W; j++){\n\t\t\t\tfor(int k = 0; k < 4; k++){\n\t\t\t\t\tint ni = i + dx[k];\n\t\t\t\t\tint nj = j + dy[k];\n\t\t\t\t\tif(ni < 0 || ni >= H)continue;\n\t\t\t\t\tif(nj < 0 || nj >= W)continue;\n\t\t\t\t\tint x = uf.find(i*W+j);\n\t\t\t\t\tx = idx[x];\n\t\t\t\t\tint y = uf.find(ni*W+nj);\n\t\t\t\t\ty = idx[y];\n\t\t\t\t\tif(x == y)continue;\n\t\t\t\t\tgraph[x].insert(y);\n\t\t\t\t\tgraph[y].insert(x);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tint root = idx[uf.find(0)];\n\t\tauto dfs = [&](auto dfs , int v , int p = -1)->ll{\n\t\t\tll ret = 1;\n\t\t\tfor(auto c:graph[v]){\n\t\t\t\tif(c == p)continue;\n\t\t\t\tret *= (X + dfs(dfs , c , v));\n\t\t\t\tret %= mod;\n\t\t\t}\n\t\t\treturn ret;\n\t\t};\n\t\treturn dfs(dfs, root);\n\t};\n\twhile(1){\n\t\tint H1,W1; cin >> H1 >> W1;\n\t\tif(H1 == 0){\n\t\t\treturn 0;\n\t\t}\n\t\tvector<vector<int>> G1(H1+2 , vector<int>(W1+2));\n\t\tfor(int i = 1; i <= H1; i++){\n\t\t\tfor(int j = 1; j <= W1; j++){\n\t\t\t\tchar c; cin >> c;\n\t\t\t\tif(c == '#')G1[i][j] = 1;\n\t\t\t}\n\t\t}\n\t\tint H2,W2; cin >> H2 >> W2;\n\t\tvector<vector<int>> G2(H2+2 , vector<int>(W2+2));\n\t\tfor(int i = 1; i <= H2; i++){\n\t\t\tfor(int j = 1; j <= W2; j++){\n\t\t\t\tchar c; cin >> c;\n\t\t\t\tif(c == '#')G2[i][j] = 1;\n\t\t\t}\n\t\t}\n\t\tH1 += 2;\n\t\tW1 += 2;\n\t\tH2 += 2;\n\t\tW2 += 2;\n\t\tif(f(H1,W1,G1) == f(H2,W2,G2))cout << \"yes\" << endl;\n\t\telse cout << \"no\" << endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4096, "score_of_the_acc": -0.4689, "final_rank": 14 }, { "submission_id": "aoj_1613_9582796", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint dx1[4] = {1,0,-1,0}, dy1[4] = {0,1,0,-1};\nint dx2[8] = {1,1,1,0,-1,-1,-1,0}, dy2[8] = {-1,0,1,1,1,0,-1,-1};\n\nmap<vector<int>,int> mp;\n\nvector<vector<int>> MakeGraph(int N, int M) {\n N += 2, M += 2;\n vector<vector<char>> C(N,vector<char>(M,'.'));\n rep(i,1,N-1) rep(j,1,M-1) cin >> C[i][j];\n int Cur = 0;\n vector<vector<int>> B(N, vector<int>(M,-1));\n rep(i,0,N) {\n rep(j,0,M) {\n if (B[i][j] == -1 && C[i][j] == '.') {\n queue<pair<int,int>> Q;\n Q.push({i,j});\n B[i][j] = Cur;\n while(!Q.empty()) {\n auto [X,Y] = Q.front();\n Q.pop();\n rep(k,0,4) {\n int NX = X + dx1[k], NY = Y + dy1[k];\n if (0 <= NX && NX < N && 0 <= NY && NY < M) {\n if (C[NX][NY] == '.' && B[NX][NY] == -1) {\n B[NX][NY] = Cur;\n Q.push({NX,NY});\n }\n }\n }\n }\n Cur++;\n }\n }\n }\n rep(i,0,N) {\n rep(j,0,M) {\n if (B[i][j] == -1 && C[i][j] == '#') {\n queue<pair<int,int>> Q;\n Q.push({i,j});\n B[i][j] = Cur;\n while(!Q.empty()) {\n auto [X,Y] = Q.front();\n Q.pop();\n rep(k,0,8) {\n int NX = X + dx2[k], NY = Y + dy2[k];\n if (0 <= NX && NX < N && 0 <= NY && NY < M) {\n if (C[NX][NY] == '#' && B[NX][NY] == -1) {\n B[NX][NY] = Cur;\n Q.push({NX,NY});\n }\n }\n }\n }\n Cur++;\n }\n }\n }\n vector<vector<pair<int,int>>> P(Cur);\n rep(i,0,N) {\n rep(j,0,M) {\n P[B[i][j]].push_back({i,j});\n }\n }\n vector<vector<int>> Ret(Cur);\n vector<bool> used(N,false);\n queue<int> Q2;\n Q2.push(0);\n used[0] = true;\n while(!Q2.empty()) {\n int Z = Q2.front();\n Q2.pop();\n for (auto [X,Y] : P[Z]) {\n rep(i,0,4) {\n int NX = X + dx1[i], NY = Y + dy1[i];\n if (0 <= NX && NX < N && 0 <= NY && NY < M) {\n if (!used[B[NX][NY]]) {\n Ret[Z].push_back(B[NX][NY]);\n used[B[NX][NY]] = true;\n Q2.push(B[NX][NY]);\n }\n }\n }\n }\n }\n return Ret;\n}\n\nint ID = 0;\n\nint DFS(vector<vector<int>>& V, int X) {\n vector<int> A;\n for (int NX : V[X]) {\n A.push_back(DFS(V,NX));\n }\n sort(ALL(A));\n if (mp.count(A)) return mp[A];\n else {\n mp[A] = ID;\n return ID++;\n }\n}\n\nbool Compare(vector<vector<int>>& V1, vector<vector<int>>& V2) {\n if (V1.size() != V2.size()) return false;\n return DFS(V1,0) == DFS(V2,0);\n}\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<vector<int>> V1 = MakeGraph(N,M);\n cin >> N >> M;\n vector<vector<int>> V2 = MakeGraph(N,M);\n cout << (Compare(V1,V2) ? \"yes\" : \"no\") << endl;\n}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3792, "score_of_the_acc": -0.2242, "final_rank": 5 }, { "submission_id": "aoj_1613_9582794", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\nint dx1[4] = {1,0,-1,0}, dy1[4] = {0,1,0,-1};\nint dx2[8] = {1,1,1,0,-1,-1,-1,0}, dy2[8] = {-1,0,1,1,1,0,-1,-1};\n\nmap<vector<int>,int> mp;\n\nvector<vector<int>> MakeGraph(int N, int M) {\n N += 2, M += 2;\n vector<vector<char>> C(N,vector<char>(M,'.'));\n rep(i,1,N-1) rep(j,1,M-1) cin >> C[i][j];\n int Cur = 0;\n vector<vector<int>> B(N, vector<int>(M,-1));\n rep(i,0,N) {\n rep(j,0,M) {\n if (B[i][j] == -1 && C[i][j] == '.') {\n queue<pair<int,int>> Q;\n Q.push({i,j});\n B[i][j] = Cur;\n while(!Q.empty()) {\n auto [X,Y] = Q.front();\n Q.pop();\n rep(k,0,4) {\n int NX = X + dx1[k], NY = Y + dy1[k];\n if (0 <= NX && NX < N && 0 <= NY && NY < M) {\n if (C[NX][NY] == '.' && B[NX][NY] == -1) {\n B[NX][NY] = Cur;\n Q.push({NX,NY});\n }\n }\n }\n }\n Cur++;\n }\n }\n }\n rep(i,0,N) {\n rep(j,0,M) {\n if (B[i][j] == -1 && C[i][j] == '#') {\n queue<pair<int,int>> Q;\n Q.push({i,j});\n B[i][j] = Cur;\n while(!Q.empty()) {\n auto [X,Y] = Q.front();\n Q.pop();\n rep(k,0,8) {\n int NX = X + dx2[k], NY = Y + dy2[k];\n if (0 <= NX && NX < N && 0 <= NY && NY < M) {\n if (C[NX][NY] == '#' && B[NX][NY] == -1) {\n B[NX][NY] = Cur;\n Q.push({NX,NY});\n }\n }\n }\n }\n Cur++;\n }\n }\n }\n vector<vector<pair<int,int>>> P(Cur);\n rep(i,0,N) {\n rep(j,0,M) {\n P[B[i][j]].push_back({i,j});\n }\n }\n vector<vector<int>> Ret(Cur);\n vector<bool> used(N,false);\n queue<int> Q2;\n Q2.push(0);\n used[0] = true;\n while(!Q2.empty()) {\n int Z = Q2.front();\n Q2.pop();\n for (auto [X,Y] : P[Z]) {\n rep(i,0,4) {\n int NX = X + dx1[i], NY = Y + dy1[i];\n if (0 <= NX && NX < N && 0 <= NY && NY < M) {\n if (!used[B[NX][NY]]) {\n Ret[Z].push_back(B[NX][NY]);\n used[B[NX][NY]] = true;\n Q2.push(B[NX][NY]);\n }\n }\n }\n }\n }\n return Ret;\n}\n\nint ID = 0;\n\nint DFS(vector<vector<int>> V, int X) {\n vector<int> A;\n for (int NX : V[X]) {\n A.push_back(DFS(V,NX));\n }\n sort(ALL(A));\n if (mp.count(A)) return mp[A];\n else {\n mp[A] = ID;\n return ID++;\n }\n}\n\nint main() {\nwhile(1) {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return 0;\n vector<vector<int>> V1 = MakeGraph(N,M);\n cin >> N >> M;\n vector<vector<int>> V2 = MakeGraph(N,M);\n cout << (DFS(V1,0) == DFS(V2,0) ? \"yes\" : \"no\") << endl;\n}\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3956, "score_of_the_acc": -0.4329, "final_rank": 9 }, { "submission_id": "aoj_1613_9379813", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst vector<pair<int,int>>white={{1,0},{-1,0},{0,1},{0,-1}};\nconst vector<pair<int,int>>black={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};\nvector<vector<int>>make_tree(const vector<vector<char>>&g)\n{\n int h=g.size(),w=g[0].size();\n int cnt=0;\n vector<vector<bool>>vis(h,vector<bool>(w,false));\n vector<vector<int>>idx(h,vector<int>(w));\n vector<vector<pair<int,int>>>inv;\n for(int i=0;i<h;i++)for(int j=0;j<w;j++)if(!vis[i][j])\n {\n inv.push_back({});\n vis[i][j]=true;\n queue<pair<int,int>>q;\n q.push(make_pair(i,j));\n while(!q.empty())\n {\n auto[r,c]=q.front();\n q.pop();\n idx[r][c]=cnt;\n inv[cnt].push_back(make_pair(r,c));\n for(auto[dr,dc]:(g[r][c]=='.'?white:black))\n {\n int tr=r+dr,tc=c+dc;\n if(0<=tr&&tr<h&&0<=tc&&tc<w&&!vis[tr][tc]&&g[tr][tc]==g[r][c])\n {\n vis[tr][tc]=true;\n q.push(make_pair(tr,tc));\n }\n }\n }\n cnt++;\n }\n vector<bool>checked(cnt,false);\n checked[0]=true;\n vector<vector<int>>res(cnt);\n for(int i=0;i<cnt;i++)\n {\n // for(int j=0;j<cnt;j++)cout<<checked[j];cout<<endl;\n assert(checked[i]);\n for(auto[r,c]:inv[i])for(auto[dr,dc]:white)\n {\n int tr=r+dr,tc=c+dc;\n if(0<=tr&&tr<h&&0<=tc&&tc<w&&!checked[idx[tr][tc]])\n {\n checked[idx[tr][tc]]=true;\n res[i].push_back(idx[tr][tc]);\n }\n }\n sort(res[i].begin(),res[i].end());\n res[i].erase(unique(res[i].begin(),res[i].end()),res[i].end());\n }\n return res;\n}\nstring tree_hash(const vector<vector<int>>&t)\n{\n auto dfs=[&](auto dfs, int u, int p)->string\n {\n vector<string>cld;\n for(int v:t[u])if(v!=p)cld.push_back(dfs(dfs,v,u));\n sort(cld.begin(),cld.end());\n string res=\"(\";\n for(auto s:cld)res+=s;\n return res+\")\";\n };\n return dfs(dfs,0,-1);\n}\nbool solve()\n{\n int H1,W1;\n cin>>H1>>W1;\n if(H1==0&&W1==0)return false;\n vector<vector<char>>grid1(H1+2,vector<char>(W1+2,'.'));\n for(int i=1;i<=H1;i++)for(int j=1;j<=W1;j++)cin>>grid1[i][j];\n int H2,W2;\n cin>>H2>>W2;\n vector<vector<char>>grid2(H2+2,vector<char>(W2+2,'.'));\n for(int i=1;i<=H2;i++)for(int j=1;j<=W2;j++)cin>>grid2[i][j];\n auto T1=make_tree(grid1);\n auto T2=make_tree(grid2);\n cout<<(tree_hash(T1)==tree_hash(T2)?\"yes\":\"no\")<<endl;\n return true;\n}\nint main(){while(solve()){}}", "accuracy": 1, "time_ms": 60, "memory_kb": 3760, "score_of_the_acc": -0.2993, "final_rank": 6 }, { "submission_id": "aoj_1613_9372890", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nbool is_end = false;\n\nconst vector<pair<int, int>> move_white = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\nconst vector<pair<int, int>> move_black = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};\n\nstruct grid {\n // grid() : grid(0, 0, 0) {}\n // grid(int h, int w, int c) : H(h), W(w), cnt_component(c) {}\n int H, W;\n int cnt_component = 0;\n vector<vector<char>> p;\n vector<vector<int>> component_id;\n vector<vector<pair<int, int>>> component;\n // グリッドの内部のマスか判定する\n bool _within_area(int r, int c) {\n return (0 <= r && r < H && 0 <= c && c < W);\n }\n // 始点と id を指定 同じ連結成分に id を伝播する\n void _bfs_allocate_id(int r_s, int c_s, int id_s) {\n queue<pair<int, int>> que;\n const vector<pair<int, int>> dir = (p[r_s][c_s] == '.' ? move_white : move_black);\n component_id[r_s][c_s] = id_s;\n component.push_back(vector<pair<int, int>>());\n que.push(make_pair(r_s, c_s));\n while(!que.empty()) {\n auto [r, c] = que.front();\n que.pop();\n for(const auto &[dr, dc] : dir) {\n const int nr = r + dr;\n const int nc = c + dc;\n if(_within_area(nr, nc) && p[nr][nc] == p[r][c] && component_id[nr][nc] == -1) {\n component_id[nr][nc] = component_id[r][c];\n que.push(make_pair(nr, nc));\n }\n }\n }\n }\n // 全体に連結成分の id を割り振る\n void allocate_id() {\n for(int i = 0; i < H; i++) {\n for(int j = 0; j < W; j++) {\n if(component_id[i][j] != -1) continue;\n _bfs_allocate_id(i, j, cnt_component++);\n }\n }\n for(int i = 0; i < H; i++) {\n for(int j = 0; j < W; j++) {\n component[component_id[i][j]].push_back(make_pair(i, j));\n }\n }\n }\n // 木に変形する 直属の子のリストを返す\n vector<vector<int>> to_tree() {\n vector<bool> used(cnt_component);\n vector<vector<int>> child(cnt_component);\n queue<int> que;\n que.push(0);\n used[0] = true;\n while(!que.empty()) {\n const int i = que.front();\n que.pop();\n for(const auto &[r, c] : component[i]) {\n const vector<pair<int, int>> dir = (p[r][c] == '.' ? move_white : move_black);\n for(const auto &[dr, dc] : dir) {\n const int nr = r + dr;\n const int nc = c + dc;\n if(_within_area(nr, nc) && !used[component_id[nr][nc]]) {\n const int j = component_id[nr][nc];\n child[i].push_back(j);\n que.push(j);\n used[j] = true;\n }\n }\n }\n }\n return child;\n }\n // 入力を読む\n grid(int c) : cnt_component(c) {\n cin >> H >> W;\n if(H == 0 && W == 0) {\n is_end = true;\n }\n H += 2, W += 2;\n p = vector(H, vector<char>(W, '.'));\n component_id = vector(H, vector<int>(W, -1));\n for(int i = 1; i < H - 1; i++) {\n string str_in;\n cin >> str_in;\n for(int j = 1; j < W - 1; j++) {\n p[i][j] = str_in[j - 1];\n }\n }\n allocate_id();\n }\n};\n\nstring hash_tree(vector<vector<int>> &child, int cur = 0) {\n if(child[cur].empty()) return \"\";\n vector<string> hash_child;\n for(const int &nxt : child[cur]) {\n hash_child.push_back('(' + hash_tree(child, nxt) + ')');\n }\n sort(hash_child.begin(), hash_child.end());\n string ret = \"\";\n for(const string &str : hash_child) {\n ret += str;\n }\n return ret;\n}\n\nbool solve() {\n grid G1(0);\n if(is_end) { return false; }\n grid G2(0);\n if(G1.cnt_component != G2.cnt_component) { cout << \"no\" << endl; return true; }\n vector<vector<int>> T1 = G1.to_tree();\n vector<vector<int>> T2 = G2.to_tree();\n cout << (hash_tree(T1) == hash_tree(T2) ? \"yes\" : \"no\") << endl;\n return true;\n}\n\nint main() {\n while(solve()) {}\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3928, "score_of_the_acc": -0.3424, "final_rank": 8 }, { "submission_id": "aoj_1613_9372649", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\n#define rep(i,n) for(ll i=0;i<n;i++)\n\nvll dx={1,0,-1,0,1,-1,1,-1};\nvll dy={0,1,0,-1,1,1,-1,-1};\nvector<vll> to_tree(vector<string> S){\n ll H=S.size();\n ll W=S[0].size();\n\n vector<vector<ll>> val(H,vll(W,-1));\n ll cnt=0;\n rep(h,H)rep(w,W){\n if(val[h][w]!=-1)continue;\n queue<pair<ll,ll>> Q;\n Q.push({h,w});\n val[h][w]=cnt;\n while(!Q.empty()){\n ll y=Q.front().first;\n ll x=Q.front().second;\n Q.pop();\n ll ds=4;\n if(S[y][x]=='#')ds=8;\n rep(d,ds){\n ll ny=y+dy[d];\n ll nx=x+dx[d];\n if(ny<0||nx<0||ny>=H||nx>=W)continue;\n if(S[ny][nx]!=S[y][x])continue;\n if(val[ny][nx]!=-1)continue;\n val[ny][nx]=cnt;\n Q.push({ny,nx});\n }\n }\n cnt++;\n }\n\n // rep(h,H){\n // rep(w,W){\n // cout<<val[h][w];\n // }\n // cout<<endl;\n // }\n\n vector<vll> G(cnt);\n vector<set<ll>> seen(cnt);\n rep(i,cnt)seen[i].insert(i);\n rep(h,H)rep(w,W){\n if(w!=W-1){\n if(!seen[val[h][w]].count(val[h][w+1])){\n G[val[h][w]].push_back(val[h][w+1]);\n seen[val[h][w]].insert(val[h][w+1]);\n }\n }\n if(h!=H-1){\n if(!seen[val[h][w]].count(val[h+1][w])){\n G[val[h][w]].push_back(val[h+1][w]);\n seen[val[h][w]].insert(val[h+1][w]);\n }\n }\n }\n return G;\n\n\n}\n\nusing ull=unsigned long long;\nstruct TreeHasher{\n using uint128=__uint128_t;\n static const ull Mod=(1ull<<61ull)-1;\n static inline ull add(ull a,ull b){if((a+=b)>=Mod)a-=Mod;return a;}\n static inline ull mul(ull a,ull b){uint128 c=(uint128)a*b;return add(c>>61,c&Mod);}\n vector<ull> R;\n // 木のサイズ\n TreeHasher(int n){\n R.push_back(1);\n mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());\n uniform_int_distribution<ull> rand(1,Mod-1);\n for(int i=1;i<=n;i++) R.push_back(rand(mt));\n // R.resize(n+1);\n // iota(ALL(R),1);\n }\n inline vector<int> BFS(vector<vector<int>> &g,int st,vector<int> &dis,bool memo_parent=false){\n vector<int> par;\n if(memo_parent) par.assign(g.size(),-1);\n dis.assign(g.size(),-1);\n dis[st]=0;\n queue<int> que;\n que.push(st);\n while(!que.empty()){\n int cur=que.front();que.pop();\n for(auto &to:g[cur]){\n if(dis[to]>=0) continue;\n dis[to]=dis[cur]+1;\n if(memo_parent) par[to]=cur;\n que.push(to);\n }\n }\n return par;\n }\n\n ull RootedTreeHash(vector<vector<ll>> &g,int root){\n vector<int> dis(g.size(),-1);\n vector<ull> h(g.size());\n function<void(int,int)> dfs=[&](int par,int cur){\n int d=-1;\n for(auto &to:g[cur])if(par!=to){\n dfs(cur,to);\n d=max(d,dis[to]);\n }\n d++;\n dis[cur]=d;\n ull ret=1;\n // rng hash\n for(auto &to:g[cur])if(par!=to) ret=mul(ret,add(h[to],R[d]));\n h[cur]=ret;\n };\n dfs(-1,root);\n return h[root];\n }\n bool RootedTreeIsomorphic(vector<vector<ll>> &g,int g_root,vector<vector<ll>> &h,int h_root){\n if(g.size()!=h.size()) return false;\n return (RootedTreeHash(g,g_root)==RootedTreeHash(h,h_root));\n }\n};\n\n\nvoid solve(ll H,ll W) {\n vector<vector<vll>> G(2); \n rep(i,2){\n vector<string> S(H+2);\n rep(w,W+2){\n S[0].push_back('.');\n S[H+1].push_back('.');\n }\n rep(h,H){\n cin>>S[h+1];\n S[h+1]=\".\"+S[h+1]+\".\";\n }\n\n G[i]=to_tree(S);\n\n if(i==0)cin>>H>>W;\n }\n if(G[0].size()!=G[1].size()){\n cout<<\"no\"<<endl;\n return;\n }\n TreeHasher T(G[0].size());\n cout<<(T.RootedTreeIsomorphic(G[0],0,G[1],0)?\"yes\":\"no\")<<endl;\n}\n\nint main() {\n\n ll H,W;\n\n while (cin >> H>>W) {\n if (H+W == 0)return 0;\n solve(H,W);\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3736, "score_of_the_acc": -0.2098, "final_rank": 4 }, { "submission_id": "aoj_1613_9347359", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\n\n#ifdef DEFINED_ONLY_IN_LOCAL\n#include \"../cpp-dump/dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\nnamespace cp = cpp_dump;\n#else\n#define dump(...)\n#define CPP_DUMP_SET_OPTION(...)\n#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)\n#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)\n#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)\n#endif\n\nconst long long INF = 1e9;\nconst long long MOD1 = 1e9 + 7;\nconst long long MOD2 = 998244353;\nconst long long LINF = 1e18;\nusing namespace std;\n// using namespace atcoder;\n\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << endl\n#define Yes(n) cout << ((n) ? \"yes\" : \"no\") << endl\n#define POSSIBLE(n) cout << ((n) ? \"POSSIBLE\" : \"IMPOSSIBLE\") << endl\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << endl\n#define Takahashi(n) cout << ((n) ? \"Takahashi\" : \"Aoki\") << endl\n#define TAKAHASHI(n) cout << ((n) ? \"TAKAHASHI\" : \"AOKI\") << endl\n#define First(n) cout << ((n) ? \"First\" : \"Second\") << endl\n#define FIRST(n) cout << ((n) ? \"FIRST\" : \"SECOND\") << endl\n#define COUT(x) cout << (x) << endl\n#define SCOUT(x) cout << (x) << \" \"\n#define VECCOUT(x) \\\n for (auto &youso_ : (x)) \\\n cout << right << setw(10) << youso_ << \" \"; \\\n cout << endl\n#define ENDL cout << endl\n#define CIN(...) \\\n int __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define LCIN(...) \\\n long long __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define SCIN(...) \\\n string __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define VECCIN(x) \\\n for (auto &youso_ : (x)) \\\n cin >> youso_\n#define mp make_pair\n\ntypedef long long ll;\ntypedef vector<long long> vl;\ntypedef vector<vl> vvl;\ntypedef pair<long long, long long> pll;\n\n#define COUT(x) cout << (x) << endl\nvoid CINT()\n{\n}\ntemplate <class Head, class... Tail>\nvoid CINT(Head &&head, Tail &&...tail)\n{\n cin >> head;\n CINT(move(tail)...);\n}\n\n// using mint = modint998244353;\n// ostream& operator<< (ostream& os, const mint &x) {os << x.val(); return os;}\n\nvvl solve(int h, int w, vvl &children)\n{\n\n vector<string> S(h);\n\n VECCIN(S);\n ll count = -LINF;\n for (ll i = 0; i < h; i++)\n for (ll j = 0; j < w; j++)\n if (i * j == 0 || i == h - 1 || j == w - 1)\n if (S.at(i).at(j) == '#')\n count++;\n vvl m(h + 2, vl(w + 2, (count == h * w - (h - 2) * (w - 2)) ? -2 : (-1)));\n for (ll i = 0; i < h; i++)\n {\n for (ll j = 0; j < w; j++)\n {\n m.at(i + 1).at(j + 1) = S.at(i).at(j) == '#' ? -2 : -1;\n }\n }\n int u_id = 0;\n // vvl children;\n set<pair<int, int>> s;\n for (ll i = 0; i < h + 2; i++)\n for (ll j = 0; j < w + 2; j++)\n {\n queue<pair<int, int>> q_outer;\n if (m.at(i).at(j) < 0)\n {\n q_outer.push(mp(i, j));\n }\n while (q_outer.size())\n {\n int q_outer_i, q_outer_j;\n tie(q_outer_i, q_outer_j) = q_outer.front();\n q_outer.pop();\n if (m.at(q_outer_i).at(q_outer_j) >= 0)\n continue;\n children.push_back(vl());\n bool is_black = m.at(q_outer_i).at(q_outer_j) == -2;\n int now = m.at(q_outer_i).at(q_outer_j);\n m.at(q_outer_i).at(q_outer_j) = u_id;\n // dump(now);\n queue<pair<int, int>> q;\n q.push(mp(q_outer_i, q_outer_j));\n while (q.size())\n {\n int ni, nj;\n tie(ni, nj) = q.front();\n q.pop();\n for (int next_i = ni - 1; next_i <= ni + 1; next_i++)\n for (int next_j = nj - 1; next_j <= nj + 1; next_j++)\n if (0 <= next_i && next_i < h + 2 && 0 <= next_j && next_j < w + 2)\n {\n int mdist = abs(next_i - ni) + abs(next_j - nj);\n if ((mdist == 1 && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id) ||\n (mdist == 2 && is_black && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id))\n {\n int child = u_id;\n int parent = m.at(next_i).at(next_j);\n if (s.count(mp(parent, child)) == false)\n children.at(parent).push_back(child);\n s.insert(mp(parent, child));\n }\n if (mdist == 1 && m.at(next_i).at(next_j) == now)\n {\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n if (mdist == 2 && is_black && m.at(next_i).at(next_j) == now)\n {\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n }\n }\n u_id++;\n }\n }\n return m;\n}\n\nll hs(vvl &m, int now)\n{\n ll res = 1;\n for (size_t i = 0; i < m.at(now).size(); i++)\n {\n res *= hs(m, m.at(now).at(i));\n res %= MOD1;\n }\n\n res += 37;\n res *= MOD2;\n res %= MOD1;\n return res;\n}\n\nvoid Main()\n{\n while (1)\n {\n LCIN(h_1, w_1);\n if (h_1 == 0)\n break;\n vvl children_1;\n vvl res1 = solve(h_1, w_1, children_1);\n LCIN(h_2, w_2);\n vvl children_2;\n vvl res2 = solve(h_2, w_2, children_2);\n // dump(res1, res2)\n // dump(children_1, children_2);\n // dump(res1, res2);\n ll h1 = hs(children_1, 0);\n ll h2 = hs(children_2, 0);\n // dump(h1, h2);\n Yes(h1 == h2);\n }\n}\nint main()\n{\n cout << fixed << setprecision(15);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3744, "score_of_the_acc": -0.4618, "final_rank": 13 }, { "submission_id": "aoj_1613_9347352", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\n\n#ifdef DEFINED_ONLY_IN_LOCAL\n#include \"../cpp-dump/dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\nnamespace cp = cpp_dump;\n#else\n#define dump(...)\n#define CPP_DUMP_SET_OPTION(...)\n#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)\n#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)\n#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)\n#endif\n\nconst long long INF = 1e9;\nconst long long MOD1 = 1e9 + 7;\nconst long long MOD2 = 998244353;\nconst long long LINF = 1e18;\nusing namespace std;\n// using namespace atcoder;\n\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << endl\n#define Yes(n) cout << ((n) ? \"yes\" : \"no\") << endl\n#define POSSIBLE(n) cout << ((n) ? \"POSSIBLE\" : \"IMPOSSIBLE\") << endl\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << endl\n#define Takahashi(n) cout << ((n) ? \"Takahashi\" : \"Aoki\") << endl\n#define TAKAHASHI(n) cout << ((n) ? \"TAKAHASHI\" : \"AOKI\") << endl\n#define First(n) cout << ((n) ? \"First\" : \"Second\") << endl\n#define FIRST(n) cout << ((n) ? \"FIRST\" : \"SECOND\") << endl\n#define COUT(x) cout << (x) << endl\n#define SCOUT(x) cout << (x) << \" \"\n#define VECCOUT(x) \\\n for (auto &youso_ : (x)) \\\n cout << right << setw(10) << youso_ << \" \"; \\\n cout << endl\n#define ENDL cout << endl\n#define CIN(...) \\\n int __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define LCIN(...) \\\n long long __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define SCIN(...) \\\n string __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define VECCIN(x) \\\n for (auto &youso_ : (x)) \\\n cin >> youso_\n#define mp make_pair\n\ntypedef long long ll;\ntypedef vector<long long> vl;\ntypedef vector<vl> vvl;\ntypedef pair<long long, long long> pll;\n\n#define COUT(x) cout << (x) << endl\nvoid CINT()\n{\n}\ntemplate <class Head, class... Tail>\nvoid CINT(Head &&head, Tail &&...tail)\n{\n cin >> head;\n CINT(move(tail)...);\n}\n\n// using mint = modint998244353;\n// ostream& operator<< (ostream& os, const mint &x) {os << x.val(); return os;}\n\nvvl solve(int h, int w, vvl &children)\n{\n\n vector<string> S(h);\n\n VECCIN(S);\n ll count = -LINF;\n for (ll i = 0; i < h; i++)\n for (ll j = 0; j < w; j++)\n if (i * j == 0 || i == h - 1 || j == w - 1)\n if (S.at(i).at(j) == '#')\n count++;\n vvl m(h + 2, vl(w + 2, (count == h * w - (h - 2) * (w - 2)) ? -2 : (-1)));\n for (ll i = 0; i < h; i++)\n {\n for (ll j = 0; j < w; j++)\n {\n dump(i, j, S);\n m.at(i + 1).at(j + 1) = S.at(i).at(j) == '#' ? -2 : -1;\n }\n }\n int u_id = 0;\n // vvl children;\n set<pair<int, int>> s;\n for (ll i = 0; i < h + 2; i++)\n for (ll j = 0; j < w + 2; j++)\n {\n queue<pair<int, int>> q_outer;\n if (m.at(i).at(j) < 0)\n {\n q_outer.push(mp(i, j));\n }\n while (q_outer.size())\n {\n int q_outer_i, q_outer_j;\n tie(q_outer_i, q_outer_j) = q_outer.front();\n q_outer.pop();\n if (m.at(q_outer_i).at(q_outer_j) >= 0)\n continue;\n children.push_back(vl());\n bool is_black = m.at(q_outer_i).at(q_outer_j) == -2;\n int now = m.at(q_outer_i).at(q_outer_j);\n m.at(q_outer_i).at(q_outer_j) = u_id;\n dump(m);\n queue<pair<int, int>> q;\n q.push(mp(q_outer_i, q_outer_j));\n while (q.size())\n {\n int ni, nj;\n tie(ni, nj) = q.front();\n q.pop();\n for (int next_i = ni - 1; next_i <= ni + 1; next_i++)\n for (int next_j = nj - 1; next_j <= nj + 1; next_j++)\n if (0 <= next_i && next_i < h + 2 && 0 <= next_j && next_j < w + 2)\n {\n int mdist = abs(next_i - ni) + abs(next_j - nj);\n if ((mdist == 1 && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id) ||\n (mdist == 2 && is_black && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id))\n {\n int child = u_id;\n int parent = m.at(next_i).at(next_j);\n if (s.count(mp(parent, child)) == false)\n children.at(parent).push_back(child);\n s.insert(mp(parent, child));\n }\n // if (m.at(next_i).at(next_j) < 0)\n // {\n // q_outer.push(mp(next_i, next_j));\n // }\n // dump(next_i, next_j);\n if (mdist == 1 && m.at(next_i).at(next_j) == now)\n {\n // dump(\" if (mdist == 1 && m.at(next_i).at(next_j) == now)\");\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n if (mdist == 2 && is_black && m.at(next_i).at(next_j) == now)\n {\n // dump(\" if (mdist == 2 && is_black && m.at(next_i).at(next_j) == now)\");\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n }\n }\n u_id++;\n }\n }\n return m;\n}\n\nll hs(vvl &m, int now)\n{\n ll res = 1;\n for (size_t i = 0; i < m.at(now).size(); i++)\n {\n res *= hs(m, m.at(now).at(i));\n res %= MOD1;\n }\n\n res += 37;\n res *= MOD2;\n res %= MOD1;\n return res;\n}\n\nvoid Main()\n{\n while (1)\n {\n LCIN(h_1, w_1);\n if (h_1 == 0)\n break;\n vvl children_1;\n vvl res1 = solve(h_1, w_1, children_1);\n LCIN(h_2, w_2);\n vvl children_2;\n vvl res2 = solve(h_2, w_2, children_2);\n dump(res1, res2);\n dump(children_1, children_2);\n ll h1 = hs(children_1, 0);\n ll h2 = hs(children_2, 0);\n dump(h1, h2);\n Yes(h1 == h2);\n }\n}\nint main()\n{\n cout << fixed << setprecision(15);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3696, "score_of_the_acc": -0.4495, "final_rank": 12 }, { "submission_id": "aoj_1613_9347332", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\n\n#ifdef DEFINED_ONLY_IN_LOCAL\n#include \"../cpp-dump/dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\nnamespace cp = cpp_dump;\n#else\n#define dump(...)\n#define CPP_DUMP_SET_OPTION(...)\n#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)\n#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)\n#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)\n#endif\n\nconst long long INF = 1e9;\nconst long long MOD1 = 1e9 + 7;\nconst long long MOD2 = 998244353;\nconst long long LINF = 1e18;\nusing namespace std;\n// using namespace atcoder;\n\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << endl\n#define Yes(n) cout << ((n) ? \"yes\" : \"no\") << endl\n#define POSSIBLE(n) cout << ((n) ? \"POSSIBLE\" : \"IMPOSSIBLE\") << endl\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << endl\n#define Takahashi(n) cout << ((n) ? \"Takahashi\" : \"Aoki\") << endl\n#define TAKAHASHI(n) cout << ((n) ? \"TAKAHASHI\" : \"AOKI\") << endl\n#define First(n) cout << ((n) ? \"First\" : \"Second\") << endl\n#define FIRST(n) cout << ((n) ? \"FIRST\" : \"SECOND\") << endl\n#define COUT(x) cout << (x) << endl\n#define SCOUT(x) cout << (x) << \" \"\n#define VECCOUT(x) \\\n for (auto &youso_ : (x)) \\\n cout << right << setw(10) << youso_ << \" \"; \\\n cout << endl\n#define ENDL cout << endl\n#define CIN(...) \\\n int __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define LCIN(...) \\\n long long __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define SCIN(...) \\\n string __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define VECCIN(x) \\\n for (auto &youso_ : (x)) \\\n cin >> youso_\n#define mp make_pair\n\ntypedef long long ll;\ntypedef vector<long long> vl;\ntypedef vector<vl> vvl;\ntypedef pair<long long, long long> pll;\n\n#define COUT(x) cout << (x) << endl\nvoid CINT()\n{\n}\ntemplate <class Head, class... Tail>\nvoid CINT(Head &&head, Tail &&...tail)\n{\n cin >> head;\n CINT(move(tail)...);\n}\n\n// using mint = modint998244353;\n// ostream& operator<< (ostream& os, const mint &x) {os << x.val(); return os;}\n\nvvl solve(int h, int w, vvl &children)\n{\n\n vector<string> S(h);\n\n VECCIN(S);\n ll count = -LINF;\n for (ll i = 0; i < h; i++)\n for (ll j = 0; j < w; j++)\n if (i * j == 0 || i == h - 1 || j == w - 1)\n if (S.at(i).at(j) == '#')\n count++;\n vvl m(h + 2, vl(w + 2, (count == h * w - (h - 2) * (w - 2)) ? -2 : (-1)));\n for (ll i = 0; i < h; i++)\n {\n for (ll j = 0; j < w; j++)\n {\n dump(i, j, S);\n m.at(i + 1).at(j + 1) = S.at(i).at(j) == '#' ? -2 : -1;\n }\n }\n int u_id = 0;\n // vvl children;\n set<pair<int, int>> s;\n for (ll i = 0; i < h + 2; i++)\n for (ll j = 0; j < w + 2; j++)\n {\n queue<pair<int, int>> q_outer;\n if (m.at(i).at(j) < 0)\n {\n q_outer.push(mp(i, j));\n }\n while (q_outer.size())\n {\n int q_outer_i, q_outer_j;\n tie(q_outer_i, q_outer_j) = q_outer.front();\n q_outer.pop();\n if (m.at(q_outer_i).at(q_outer_j) >= 0)\n continue;\n children.push_back(vl());\n bool is_black = m.at(q_outer_i).at(q_outer_j) == -2;\n int now = m.at(q_outer_i).at(q_outer_j);\n m.at(q_outer_i).at(q_outer_j) = u_id;\n dump(m);\n queue<pair<int, int>> q;\n q.push(mp(q_outer_i, q_outer_j));\n while (q.size())\n {\n int ni, nj;\n tie(ni, nj) = q.front();\n q.pop();\n for (int next_i = ni - 1; next_i <= ni + 1; next_i++)\n for (int next_j = nj - 1; next_j <= nj + 1; next_j++)\n if (0 <= next_i && next_i < h + 2 && 0 <= next_j && next_j < w + 2)\n {\n int mdist = abs(next_i - ni) + abs(next_j - nj);\n if ((mdist == 1 && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id) ||\n (mdist == 2 && is_black && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id))\n {\n int child = u_id;\n int parent = m.at(next_i).at(next_j);\n if (s.count(mp(parent, child)) == false)\n children.at(parent).push_back(child);\n s.insert(mp(parent, child));\n }\n // if (m.at(next_i).at(next_j) < 0)\n // {\n // q_outer.push(mp(next_i, next_j));\n // }\n // dump(next_i, next_j);\n if (mdist == 1 && m.at(next_i).at(next_j) == now)\n {\n // dump(\" if (mdist == 1 && m.at(next_i).at(next_j) == now)\");\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n if (mdist == 2 && is_black && m.at(next_i).at(next_j) == now)\n {\n // dump(\" if (mdist == 2 && is_black && m.at(next_i).at(next_j) == now)\");\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n }\n }\n u_id++;\n }\n }\n return m;\n}\n\nstring hs(vvl &m, int now)\n{\n string res = \"\";\n vector<string> S;\n for (size_t i = 0; i < m.at(now).size(); i++)\n {\n S.push_back(hs(m, m.at(now).at(i)));\n }\n sort(S.begin(), S.end());\n for (size_t i = 0; i < m.at(now).size(); i++)\n res += S.at(i);\n\n return '(' + res + ')';\n}\n\nvoid Main()\n{\n while (1)\n {\n LCIN(h_1, w_1);\n if (h_1 == 0)\n break;\n vvl children_1;\n vvl res1 = solve(h_1, w_1, children_1);\n LCIN(h_2, w_2);\n vvl children_2;\n vvl res2 = solve(h_2, w_2, children_2);\n dump(res1, res2);\n dump(children_1, children_2);\n string h1 = hs(children_1, 0);\n string h2 = hs(children_2, 0);\n dump(h1, h2);\n Yes(h1 == h2);\n }\n}\nint main()\n{\n cout << fixed << setprecision(15);\n Main();\n return 0;\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3656, "score_of_the_acc": -0.4393, "final_rank": 11 }, { "submission_id": "aoj_1613_9347305", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\n\n#ifdef DEFINED_ONLY_IN_LOCAL\n#include \"../cpp-dump/dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\nnamespace cp = cpp_dump;\n#else\n#define dump(...)\n#define CPP_DUMP_SET_OPTION(...)\n#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)\n#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)\n#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)\n#endif\n\nconst long long INF = 1e9;\nconst long long MOD1 = 1e9 + 7;\nconst long long MOD2 = 998244353;\nconst long long LINF = 1e18;\nusing namespace std;\n// using namespace atcoder;\n\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << endl\n#define Yes(n) cout << ((n) ? \"yes\" : \"no\") << endl\n#define POSSIBLE(n) cout << ((n) ? \"POSSIBLE\" : \"IMPOSSIBLE\") << endl\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << endl\n#define Takahashi(n) cout << ((n) ? \"Takahashi\" : \"Aoki\") << endl\n#define TAKAHASHI(n) cout << ((n) ? \"TAKAHASHI\" : \"AOKI\") << endl\n#define First(n) cout << ((n) ? \"First\" : \"Second\") << endl\n#define FIRST(n) cout << ((n) ? \"FIRST\" : \"SECOND\") << endl\n#define COUT(x) cout << (x) << endl\n#define SCOUT(x) cout << (x) << \" \"\n#define VECCOUT(x) \\\n for (auto &youso_ : (x)) \\\n cout << right << setw(10) << youso_ << \" \"; \\\n cout << endl\n#define ENDL cout << endl\n#define CIN(...) \\\n int __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define LCIN(...) \\\n long long __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define SCIN(...) \\\n string __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define VECCIN(x) \\\n for (auto &youso_ : (x)) \\\n cin >> youso_\n#define mp make_pair\n\ntypedef long long ll;\ntypedef vector<long long> vl;\ntypedef vector<vl> vvl;\ntypedef pair<long long, long long> pll;\n\n#define COUT(x) cout << (x) << endl\nvoid CINT()\n{\n}\ntemplate <class Head, class... Tail>\nvoid CINT(Head &&head, Tail &&...tail)\n{\n cin >> head;\n CINT(move(tail)...);\n}\n\n// using mint = modint998244353;\n// ostream& operator<< (ostream& os, const mint &x) {os << x.val(); return os;}\n\nvvl solve(int h, int w, vvl &children)\n{\n\n vector<string> S(h);\n\n VECCIN(S);\n ll count = 0;\n for (ll i = 0; i < h; i++)\n for (ll j = 0; j < w; j++)\n if (i * j == 0 || i == h - 1 || j == w - 1)\n if (S.at(i).at(j) == '#')\n count++;\n vvl m(h + 2, vl(w + 2, (count == h * w - (h - 2) * (w - 2)) ? -2 : (-1)));\n for (ll i = 0; i < h; i++)\n {\n for (ll j = 0; j < w; j++)\n {\n m.at(i + 1).at(j + 1) = S.at(i).at(j) == '#' ? -2 : -1;\n }\n }\n int u_id = 0;\n // vvl children;\n set<pair<int, int>> s;\n for (ll i = 0; i < h + 2; i++)\n for (ll j = 0; j < w + 2; j++)\n {\n queue<pair<int, int>> q_outer;\n if (m.at(i).at(j) < 0)\n {\n q_outer.push(mp(i, j));\n }\n while (q_outer.size())\n {\n int q_outer_i, q_outer_j;\n tie(q_outer_i, q_outer_j) = q_outer.front();\n q_outer.pop();\n if (m.at(q_outer_i).at(q_outer_j) >= 0)\n continue;\n children.push_back(vl());\n bool is_black = m.at(q_outer_i).at(q_outer_j) == -2;\n int now = m.at(q_outer_i).at(q_outer_j);\n m.at(q_outer_i).at(q_outer_j) = u_id;\n // dump(now);\n queue<pair<int, int>> q;\n q.push(mp(q_outer_i, q_outer_j));\n while (q.size())\n {\n int ni, nj;\n tie(ni, nj) = q.front();\n q.pop();\n for (int next_i = ni - 1; next_i <= ni + 1; next_i++)\n for (int next_j = nj - 1; next_j <= nj + 1; next_j++)\n if (0 <= next_i && next_i < h + 2 && 0 <= next_j && next_j < w + 2)\n {\n int mdist = abs(next_i - ni) + abs(next_j - nj);\n if ((mdist == 1 && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id) ||\n (mdist == 2 && is_black && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id))\n {\n int child = u_id;\n int parent = m.at(next_i).at(next_j);\n if (s.count(mp(parent, child)) == false)\n children.at(parent).push_back(child);\n s.insert(mp(parent, child));\n }\n if (mdist == 1 && m.at(next_i).at(next_j) == now)\n {\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n if (mdist == 2 && is_black && m.at(next_i).at(next_j) == now)\n {\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n }\n }\n u_id++;\n }\n }\n return m;\n}\n\nstring hs(vvl &m, int now)\n{\n string res = \"\";\n vector<string> S;\n for (size_t i = 0; i < m.at(now).size(); i++)\n {\n S.push_back(hs(m, m.at(now).at(i)));\n }\n sort(S.begin(), S.end());\n for (size_t i = 0; i < m.at(now).size(); i++)\n res += S.at(i);\n\n return '(' + res + ')';\n}\n\nvoid Main()\n{\n while (1)\n {\n LCIN(h_1, w_1);\n if (h_1 == 0)\n break;\n vvl children_1;\n vvl res1 = solve(h_1, w_1, children_1);\n LCIN(h_2, w_2);\n vvl children_2;\n vvl res2 = solve(h_2, w_2, children_2);\n // dump(res1, res2)\n // dump(children_1, children_2);\n // dump(res1, res2);\n string h1 = hs(children_1, 0);\n string h2 = hs(children_2, 0);\n // dump(h1, h2);\n Yes(h1 == h2);\n }\n}\nint main()\n{\n cout << fixed << setprecision(15);\n Main();\n return 0;\n}", "accuracy": 0.5, "time_ms": 80, "memory_kb": 3672, "score_of_the_acc": -0.4434, "final_rank": 20 }, { "submission_id": "aoj_1613_9347299", "code_snippet": "#include <bits/stdc++.h>\n// #include <atcoder/all>\n\n#ifdef DEFINED_ONLY_IN_LOCAL\n#include \"../cpp-dump/dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\nnamespace cp = cpp_dump;\n#else\n#define dump(...)\n#define CPP_DUMP_SET_OPTION(...)\n#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)\n#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)\n#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)\n#endif\n\nconst long long INF = 1e9;\nconst long long MOD1 = 1e9 + 7;\nconst long long MOD2 = 998244353;\nconst long long LINF = 1e18;\nusing namespace std;\n// using namespace atcoder;\n\n#define YES(n) cout << ((n) ? \"YES\" : \"NO\") << endl\n#define Yes(n) cout << ((n) ? \"yes\" : \"no\") << endl\n#define POSSIBLE(n) cout << ((n) ? \"POSSIBLE\" : \"IMPOSSIBLE\") << endl\n#define Possible(n) cout << ((n) ? \"Possible\" : \"Impossible\") << endl\n#define Takahashi(n) cout << ((n) ? \"Takahashi\" : \"Aoki\") << endl\n#define TAKAHASHI(n) cout << ((n) ? \"TAKAHASHI\" : \"AOKI\") << endl\n#define First(n) cout << ((n) ? \"First\" : \"Second\") << endl\n#define FIRST(n) cout << ((n) ? \"FIRST\" : \"SECOND\") << endl\n#define COUT(x) cout << (x) << endl\n#define SCOUT(x) cout << (x) << \" \"\n#define VECCOUT(x) \\\n for (auto &youso_ : (x)) \\\n cout << right << setw(10) << youso_ << \" \"; \\\n cout << endl\n#define ENDL cout << endl\n#define CIN(...) \\\n int __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define LCIN(...) \\\n long long __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define SCIN(...) \\\n string __VA_ARGS__; \\\n CINT(__VA_ARGS__)\n#define VECCIN(x) \\\n for (auto &youso_ : (x)) \\\n cin >> youso_\n#define mp make_pair\n\ntypedef long long ll;\ntypedef vector<long long> vl;\ntypedef vector<vl> vvl;\ntypedef pair<long long, long long> pll;\n\n#define COUT(x) cout << (x) << endl\nvoid CINT()\n{\n}\ntemplate <class Head, class... Tail>\nvoid CINT(Head &&head, Tail &&...tail)\n{\n cin >> head;\n CINT(move(tail)...);\n}\n\n// using mint = modint998244353;\n// ostream& operator<< (ostream& os, const mint &x) {os << x.val(); return os;}\n\nvvl solve(int h, int w, vvl &children)\n{\n\n vector<string> S(h);\n\n VECCIN(S);\n ll count = 0;\n for (ll i = 0; i < h; i++)\n for (ll j = 0; j < w; j++)\n if (i * j == 0 || i == h - 1 || j == w - 1)\n if (S.at(i).at(j) == '#')\n count++;\n vvl m(h + 2, vl(w + 2, (count == h * w - (h - 2) * (w - 2)) ? -2 : (-1)));\n for (ll i = 0; i < h; i++)\n {\n for (ll j = 0; j < w; j++)\n {\n m.at(i + 1).at(j + 1) = S.at(i).at(j) == '#' ? -2 : -1;\n }\n }\n int u_id = 0;\n // vvl children;\n set<pair<int, int>> s;\n for (ll i = 0; i < h + 2; i++)\n for (ll j = 0; j < w + 2; j++)\n {\n queue<pair<int, int>> q_outer;\n if (m.at(i).at(j) < 0)\n {\n q_outer.push(mp(i, j));\n }\n while (q_outer.size())\n {\n int q_outer_i, q_outer_j;\n tie(q_outer_i, q_outer_j) = q_outer.front();\n q_outer.pop();\n if (m.at(q_outer_i).at(q_outer_j) >= 0)\n continue;\n children.push_back(vl());\n bool is_black = m.at(q_outer_i).at(q_outer_j) == -2;\n int now = m.at(q_outer_i).at(q_outer_j);\n m.at(q_outer_i).at(q_outer_j) = u_id;\n // dump(now);\n queue<pair<int, int>> q;\n q.push(mp(q_outer_i, q_outer_j));\n while (q.size())\n {\n int ni, nj;\n tie(ni, nj) = q.front();\n q.pop();\n for (int next_i = ni - 1; next_i <= ni + 1; next_i++)\n for (int next_j = nj - 1; next_j <= nj + 1; next_j++)\n if (0 <= next_i && next_i < h + 2 && 0 <= next_j && next_j < w + 2)\n {\n int mdist = abs(next_i - ni) + abs(next_j - nj);\n if ((mdist == 1 && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id) ||\n (mdist == 2 && is_black && m.at(next_i).at(next_j) >= 0 && m.at(next_i).at(next_j) < u_id))\n {\n int child = u_id;\n int parent = m.at(next_i).at(next_j);\n if (s.count(mp(parent, child)) == false)\n children.at(parent).push_back(child);\n s.insert(mp(parent, child));\n }\n if (mdist == 1 && m.at(next_i).at(next_j) == now)\n {\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n if (mdist == 2 && is_black && m.at(next_i).at(next_j) == now)\n {\n m.at(next_i).at(next_j) = u_id;\n q.push(mp(next_i, next_j));\n }\n }\n }\n u_id++;\n }\n }\n return m;\n}\n\nll hs(vvl &m, int now)\n{\n ll res = 1;\n for (size_t i = 0; i < m.at(now).size(); i++)\n {\n res *= hs(m, m.at(now).at(i));\n res %= MOD1;\n }\n\n res += 37;\n res *= MOD2;\n res %= MOD1;\n return res;\n}\n\nvoid Main()\n{\n while (1)\n {\n LCIN(h_1, w_1);\n if (h_1 == 0)\n break;\n vvl children_1;\n vvl res1 = solve(h_1, w_1, children_1);\n LCIN(h_2, w_2);\n vvl children_2;\n vvl res2 = solve(h_2, w_2, children_2);\n // dump(res1, res2)\n // dump(children_1, children_2);\n // dump(res1, res2);\n ll h1 = hs(children_1, 0);\n ll h2 = hs(children_2, 0);\n // dump(h1, h2);\n Yes(h1 == h2);\n }\n}\nint main()\n{\n cout << fixed << setprecision(15);\n Main();\n return 0;\n}", "accuracy": 0.5, "time_ms": 80, "memory_kb": 3652, "score_of_the_acc": -0.4382, "final_rank": 19 }, { "submission_id": "aoj_1613_9128026", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<set>\n#include<queue>\n#include<algorithm>\nusing namespace std;\nint dy4[4] = {1,0,-1,0};\nint dx4[4] = {0,1,0,-1};\nint dy8[8] = {1,1,1,0,-1,-1,-1,0};\nint dx8[8] = {-1,0,1,1,1,0,-1,-1};\nbool inr(int l, int x, int r){\n return (l <= x) && (x < r);\n}\nvoid dfs(int now, vector<set<int>> &G, vector<string> &str, vector<bool> &seen){\n seen[now] = true;\n vector<string> tmp;\n for(int next:G[now]){\n if(seen[next])continue;\n dfs(next, G, str, seen);\n tmp.push_back(str[next]);\n }\n sort(tmp.begin(), tmp.end());\n str[now] = \"(\";\n for(int i = 0; i < (int)tmp.size(); i++){\n str[now] += tmp[i];\n }\n str[now] += \")\";\n return;\n}\nstring graph_to_string(vector<set<int>> G){\n vector<string> str(G.size());\n vector<bool> seen(G.size(), false);\n // 木を括弧からなる文字列に変換する\n dfs(0, G, str, seen);\n return str[0];\n}\nvector<set<int>> pixel_to_graph(int h, int w, vector<string> P){\n // 画像の周りに白のピクセルを追加する\n for(int i = 0; i < h; i++){\n P[i] = \".\" + P[i] + \".\";\n }\n {\n string tmp(w+2, '.');\n P.insert(P.begin(), tmp);\n P.push_back(tmp);\n }\n // ピクセルを幅優先探索で連結成分分解\n int cnt = 0;\n vector<vector<int>> comp(h+2, vector<int>(w+2, -1));\n for(int i = 0; i < h+2; i++){\n for(int j = 0; j < w+2; j++){\n if(comp[i][j] == -1){\n char color = P[i][j];\n queue<pair<int,int>> qu;\n qu.push(make_pair(i,j));\n comp[i][j] = cnt;\n while(!qu.empty()){\n pair<int,int> now = qu.front();\n qu.pop();\n for(int d = 0; d < 8; d++){\n int ny, nx;\n if(color == '.'){ // 白の場合\n if(4 <= d)break;\n ny = now.first + dy4[d];\n nx = now.second + dx4[d];\n }else{ // 黒の場合\n ny = now.first + dy8[d];\n nx = now.second + dx8[d];\n }\n if(!inr(0, ny ,h+2) || !inr(0, nx ,w+2))continue;\n if(P[ny][nx] != color || comp[ny][nx] != -1)continue;\n qu.push(make_pair(ny,nx));\n comp[ny][nx] = cnt;\n }\n }\n cnt++;\n }\n }\n }\n // 連結成分同士の位置関係をグラフに変換する\n vector<set<int>> G(cnt);\n for(int i = 0; i < h+2; i++){\n for(int j = 0; j < w+2; j++){\n if(i+1 < h+2){\n if(comp[i][j] != comp[i+1][j]){\n G[comp[i][j]].insert(comp[i+1][j]);\n G[comp[i+1][j]].insert(comp[i][j]);\n }\n }\n if(j+1 < w+2){\n if(comp[i][j] != comp[i][j+1]){\n G[comp[i][j]].insert(comp[i][j+1]);\n G[comp[i][j+1]].insert(comp[i][j]);\n }\n }\n }\n }\n return G;\n}\nint main(){\n while(1){\n int h[2],w[2];\n vector<string> P[2];\n // 画像1 入力\n cin >> h[0] >> w[0];\n if(h[0] == 0 && w[0] == 0)break; // データセットの終わり\n P[0].resize(h[0]);\n for(int i = 0; i < h[0]; i++){\n cin >> P[0][i];\n }\n\n // 画像2 入力\n cin >> h[1] >> w[1];\n P[1].resize(h[1]);\n for(int i = 0; i < h[1]; i++){\n cin >> P[1][i];\n }\n\n // 出力\n if(graph_to_string(pixel_to_graph(h[0],w[0],P[0])) == graph_to_string(pixel_to_graph(h[1],w[1],P[1]))){\n cout << \"yes\" << endl;\n }else{\n cout << \"no\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3568, "score_of_the_acc": -0.1667, "final_rank": 3 }, { "submission_id": "aoj_1613_9125259", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(v) v.begin(),v.end()\n#define dbg(x) cerr << #x << \": \" << (x) << endl;\ntemplate<class F, class S>\nostream& operator<<(ostream& os, pair<F,S>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate<class Iter>\nvoid print(Iter beg, Iter end) {\n for (Iter itr = beg; itr != end; ++itr) {\n cerr << *itr << ' ';\n }\n cerr << '\\n';\n}\nusing Pic = vector<vector<char>>;\nusing Graph = vector<vector<int>>;\nPic S[2];\nint dx[] = {1,0,-1,0,1};\n\nvector<pair<int,int>> paintConnected(const Pic& p, vector<vector<int>>& col, int sx, int sy, int idx) {\n vector<pair<int,int>> inside;\n int h = p.size();\n int w = p[0].size();\n queue<pair<int,int>> q;\n q.emplace(sx, sy);\n col[sx][sy] = idx;\n while (q.size()) {\n auto [x,y] = q.front();\n q.pop();\n \n if (p[sx][sy] == '.') {\n for (int i = 0; i < 4; ++i) {\n int nx = x + dx[i];\n int ny = y + dx[i+1];\n if (!(0 <= nx && nx < h && 0 <= ny && ny < w)) continue;\n if (col[nx][ny] != -1) continue;\n if (p[nx][ny] != p[sx][sy]) {\n if (col[nx][ny] == -1) inside.emplace_back(nx, ny);\n continue;\n }\n q.emplace(nx, ny);\n col[nx][ny] = idx;\n }\n } else {\n for (int dx = -1; dx < 2; ++dx) {\n for (int dy = -1; dy < 2; ++dy) {\n if (dx==0 && dy==0) continue;\n int nx = x + dx;\n int ny = y + dy;\n if (!(0 <= nx && nx < h && 0 <= ny && ny < w)) continue;\n if (col[nx][ny] != -1) continue;\n if (p[nx][ny] != p[sx][sy]) {\n if (col[nx][ny] == -1) inside.emplace_back(nx, ny);\n continue;\n }\n q.emplace(nx, ny);\n col[nx][ny] = idx;\n }\n }\n }\n }\n return inside;\n}\n\npair<vector<vector<int>>, int> paint(const Pic& p) {\n int h = p.size();\n int w = p[0].size();\n queue<pair<int,int>> q;\n vector<vector<int>> col(h, vector<int>(w,-1));\n int idx = 0;\n q.emplace(0,0);\n\n while (q.size()) {\n auto [x,y] = q.front();\n q.pop();\n if (col[x][y] != -1) continue;\n\n auto inside = paintConnected(p, col, x, y, idx);\n for (auto x : inside) {\n q.push(x);\n }\n ++idx;\n }\n return {col, idx};\n}\n\nGraph gen(const Pic& p, vector<vector<int>>& col, int kind) {\n int h = col.size();\n int w = col[0].size();\n\n set<pair<int,int>> st;\n\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n for (int dx = -1; dx < 2; ++dx) {\n for (int dy = -1; dy < 2; ++dy) {\n if (dx==0 && dy==0) continue;\n int nx = i + dx;\n int ny = j + dy;\n if (!(0 <= nx && nx < h && 0 <= ny && ny < w)) continue;\n int a = col[nx][ny];\n int b = col[i][j];\n if (p[i][j] == p[nx][ny]) continue;\n if (a == b) continue;\n if (a > b) swap(a,b);\n st.emplace(a, b);\n }\n }\n }\n }\n Graph g(kind);\n for (auto [u,v] : st) {\n g[u].push_back(v);\n g[v].push_back(u);\n }\n return g;\n}\n// O(N + M)\npair<vector<int>, int> bfs(const Graph& g, int start) {\n int n = g.size();\n vector<int> dis(n, 1e9), pre(n, -1);\n queue<int> q;\n dis[start] = 0;\n q.push(start);\n int ma = 0;\n int farthest = start;\n while (q.size()) {\n int pos = q.front();\n q.pop();\n for (int to : g[pos]) {\n if (dis[pos] + 1 < dis[to]) {\n dis[to] = dis[pos] + 1;\n pre[to] = pos;\n q.push(to);\n if (dis[to] > ma) {\n ma = dis[to];\n farthest = to;\n }\n }\n }\n }\n return {pre, farthest};\n}\n\n// 木の中心を求める(木の直径の中央の頂点)\n// 1or2頂点\nvector<int> center(const Graph& tree) {\n auto [pre1, v1] = bfs(tree, 0);\n auto [pre2, v2] = bfs(tree, v1);\n\n vector<int> path;\n int now = v2;\n while (now != -1) {\n path.push_back(now);\n now = pre2[now];\n }\n\n vector<int> centers;\n centers.push_back(path[path.size() / 2]);\n if (path.size() % 2 == 0) centers.push_back(path[path.size() / 2 - 1]);\n return centers;\n}\n// 木の名前を求める\nint name(int pos, int pre, const Graph& tree, map<vector<int>,int>& cmp, int& id) {\n vector<int> seq;\n for (int c : tree[pos]) {\n if (c == pre) continue;\n seq.push_back(name(c, pos, tree, cmp, id));\n }\n if (seq.empty()) {\n return 0;\n }\n sort(ALL(seq));\n if (cmp.count(seq)) {\n return cmp.at(seq);\n } else {\n cmp[seq] = ++id;\n return id;\n }\n}\n// 根付き木の同型性判定\nbool isomorphism_rooted(const Graph& t1, const Graph& t2, int r1, int r2) {\n int id = 0;\n map<vector<int>, int> cmp;\n int name1 = name(r1, -1, t1, cmp, id);\n int name2 = name(r2, -1, t2, cmp, id);\n return (name1 == name2);\n}\n\nbool solve() {\n for (int t = 0; t < 2; ++t) {\n int h,w;\n cin >> h >> w;\n if (h+w == 0) exit(0);\n S[t].assign(h+2, vector<char>(w+2, '.'));\n for (int i = 1; i <= h; ++i) {\n for (int j = 1; j <= w; ++j) {\n cin >> S[t][i][j];\n }\n }\n }\n\n vector<vector<int>> col[2];\n int kind[2];\n for (int i = 0; i < 2; ++i) {\n tie(col[i], kind[i]) = paint(S[i]);\n }\n Graph g[2];\n for (int i = 0; i < 2; ++i) {\n g[i] = gen(S[i], col[i], kind[i]);\n }\n\n return isomorphism_rooted(g[0], g[1], 0, 0);\n}\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n while (true) {\n cout << (solve() ? \"yes\" : \"no\")<< '\\n';\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3716, "score_of_the_acc": -0.9547, "final_rank": 16 }, { "submission_id": "aoj_1613_9125247", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define ALL(v) v.begin(),v.end()\n#define dbg(x) cerr << #x << \": \" << (x) << endl;\ntemplate<class F, class S>\nostream& operator<<(ostream& os, pair<F,S>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate<class Iter>\nvoid print(Iter beg, Iter end) {\n for (Iter itr = beg; itr != end; ++itr) {\n cerr << *itr << ' ';\n }\n cerr << '\\n';\n}\nusing Pic = vector<vector<char>>;\nusing Graph = vector<vector<int>>;\nPic S[2];\nint dx[] = {1,0,-1,0,1};\n\nvector<pair<int,int>> paintConnected(const Pic& p, vector<vector<int>>& col, int sx, int sy, int idx) {\n vector<pair<int,int>> inside;\n int h = p.size();\n int w = p[0].size();\n queue<pair<int,int>> q;\n q.emplace(sx, sy);\n col[sx][sy] = idx;\n while (q.size()) {\n auto [x,y] = q.front();\n q.pop();\n \n if (p[sx][sy] == '.') {\n for (int i = 0; i < 4; ++i) {\n int nx = x + dx[i];\n int ny = y + dx[i+1];\n if (!(0 <= nx && nx < h && 0 <= ny && ny < w)) continue;\n if (col[nx][ny] != -1) continue;\n if (p[nx][ny] != p[sx][sy]) {\n if (col[nx][ny] == -1) inside.emplace_back(nx, ny);\n continue;\n }\n q.emplace(nx, ny);\n col[nx][ny] = idx;\n }\n } else {\n for (int dx = -1; dx < 2; ++dx) {\n for (int dy = -1; dy < 2; ++dy) {\n if (dx==0 && dy==0) continue;\n int nx = x + dx;\n int ny = y + dy;\n if (!(0 <= nx && nx < h && 0 <= ny && ny < w)) continue;\n if (col[nx][ny] != -1) continue;\n if (p[nx][ny] != p[sx][sy]) {\n if (col[nx][ny] == -1) inside.emplace_back(nx, ny);\n continue;\n }\n q.emplace(nx, ny);\n col[nx][ny] = idx;\n }\n }\n }\n }\n return inside;\n}\n\npair<vector<vector<int>>, int> paint(const Pic& p) {\n int h = p.size();\n int w = p[0].size();\n queue<pair<int,int>> q;\n vector<vector<int>> col(h, vector<int>(w,-1));\n int idx = 0;\n q.emplace(0,0);\n\n while (q.size()) {\n auto [x,y] = q.front();\n q.pop();\n if (col[x][y] != -1) continue;\n\n auto inside = paintConnected(p, col, x, y, idx);\n for (auto x : inside) {\n q.push(x);\n }\n ++idx;\n }\n return {col, idx};\n}\n\nGraph gen(const Pic& p, vector<vector<int>>& col, int kind) {\n int h = col.size();\n int w = col[0].size();\n\n set<pair<int,int>> st;\n\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n for (int dx = -1; dx < 2; ++dx) {\n for (int dy = -1; dy < 2; ++dy) {\n if (dx==0 && dy==0) continue;\n int nx = i + dx;\n int ny = j + dy;\n if (!(0 <= nx && nx < h && 0 <= ny && ny < w)) continue;\n int a = col[nx][ny];\n int b = col[i][j];\n if (p[i][j] == p[nx][ny]) continue;\n if (a == b) continue;\n if (a > b) swap(a,b);\n st.emplace(a, b);\n }\n }\n }\n }\n Graph g(kind);\n for (auto [u,v] : st) {\n g[u].push_back(v);\n g[v].push_back(u);\n }\n return g;\n}\n// O(N + M)\ntuple<vector<int>, vector<int>, int> bfs(const Graph& g, int start) {\n int n = g.size();\n vector<int> dis(n, 1e9), pre(n, -1);\n queue<int> q;\n dis[start] = 0;\n q.push(start);\n int ma = 0;\n int farthest = start;\n while (q.size()) {\n int pos = q.front();\n q.pop();\n for (int to : g[pos]) {\n if (dis[pos] + 1 < dis[to]) {\n dis[to] = dis[pos] + 1;\n pre[to] = pos;\n q.push(to);\n if (dis[to] > ma) {\n ma = dis[to];\n farthest = to;\n }\n }\n }\n }\n return {dis, pre, farthest};\n}\n\n// 木の中心を求める(木の直径の中央の頂点)\n// 1or2頂点\nvector<int> center(const Graph& tree) {\n auto [dis1, pre1, v1] = bfs(tree, 0);\n auto [dis2, pre2, v2] = bfs(tree, v1);\n\n vector<int> path;\n int now = v2;\n while (now != -1) {\n path.push_back(now);\n now = pre2[now];\n }\n\n vector<int> centers;\n centers.push_back(path[path.size() / 2]);\n if (path.size() % 2 == 0) centers.push_back(path[path.size() / 2 - 1]);\n return centers;\n}\n// 木の名前を求める\nint name(int pos, int pre, const Graph& tree, map<vector<int>,int>& cmp, int& id) {\n vector<int> seq;\n for (int c : tree[pos]) {\n if (c == pre) continue;\n seq.push_back(name(c, pos, tree, cmp, id));\n }\n if (seq.empty()) {\n return 0;\n }\n sort(ALL(seq));\n if (cmp.count(seq)) {\n return cmp.at(seq);\n } else {\n cmp[seq] = ++id;\n return id;\n }\n}\n// 根付き木の同型性判定\nbool isomorphism_rooted(const Graph& t1, const Graph& t2, int r1, int r2) {\n int id = 0;\n map<vector<int>, int> cmp;\n int name1 = name(r1, -1, t1, cmp, id);\n int name2 = name(r2, -1, t2, cmp, id);\n return (name1 == name2);\n}\n\nbool solve() {\n for (int t = 0; t < 2; ++t) {\n int h,w;\n cin >> h >> w;\n if (h+w == 0) exit(0);\n S[t].assign(h+2, vector<char>(w+2, '.'));\n for (int i = 1; i <= h; ++i) {\n for (int j = 1; j <= w; ++j) {\n cin >> S[t][i][j];\n }\n }\n }\n\n vector<vector<int>> col[2];\n int kind[2];\n for (int i = 0; i < 2; ++i) {\n tie(col[i], kind[i]) = paint(S[i]);\n }\n Graph g[2];\n for (int i = 0; i < 2; ++i) {\n g[i] = gen(S[i], col[i], kind[i]);\n }\n\n return isomorphism_rooted(g[0], g[1], 0, 0);\n}\n\nint main() {\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n while (true) {\n cout << (solve() ? \"yes\" : \"no\")<< '\\n';\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 3588, "score_of_the_acc": -1.0051, "final_rank": 17 } ]
aoj_1618_cpp
A Garden with Ponds Mr. Gardiner is a modern garden designer who is excellent at utilizing the terrain features. His design method is unique: he first decides the location of ponds and design them with the terrain features intact. According to his unique design procedure, all of his ponds are rectangular with simple aspect ratios. First, Mr. Gardiner draws a regular grid on the map of the garden site so that the land is divided into cells of unit square, and annotates every cell with its elevation. In his design method, a pond occupies a rectangular area consisting of a number of cells. Each of its outermost cells has to be higher than all of its inner cells. For instance, in the following grid map, in which numbers are elevations of cells, a pond can occupy the shaded area, where the outermost cells are shaded darker and the inner cells are shaded lighter. You can easily see that the elevations of the outermost cells are at least three and those of the inner ones are at most two. A rectangular area on which a pond is built must have at least one inner cell. Therefore, both its width and depth are at least three. When you pour water at an inner cell of a pond, the water can be kept in the pond until its level reaches that of the lowest outermost cells. If you continue pouring, the water inevitably spills over. Mr. Gardiner considers the larger capacity the pond has, the better it is. Here, the capacity of a pond is the maximum amount of water it can keep. For instance, when a pond is built on the shaded area in the above map, its capacity is (3 − 1) + (3 − 0) + (3 − 2) = 6, where 3 is the lowest elevation of the outermost cells and 1, 0, 2 are the elevations of the inner cells. Your mission is to write a computer program that, given a grid map describing the elevation of each unit square cell, calculates the largest possible capacity of a pond built in the site. Note that neither of the following rectangular areas can be a pond. In the left one, the cell at the bottom right corner is not higher than the inner cell. In the right one, the central cell is as high as the outermost cells. Input The input consists of at most 100 datasets, each in the following format. d w e 1, 1 ... e 1, w ... e d , 1 ... e d, w The first line contains d and w , representing the depth and the width, respectively, of the garden site described in the map. They are positive integers between 3 and 10, inclusive. Each of the following d lines contains w integers between 0 and 9, inclusive, separated by a space. The x -th integer in the y -th line of the d lines is the elevation of the unit square cell with coordinates ( x, y ). The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a single line containing the largest possible capacity of a pond that can be built in the garden site described in the dataset. If no ponds can be built, output a single line containing a zero. Sample ...(truncated)
[ { "submission_id": "aoj_1618_10542183", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<string>\n#include<vector>\n#include<algorithm>\n#include<tuple>\n#include<map>\n#include<queue>\n#include<set>\n#include<unordered_set>\n#include<stack>\n#include<deque>\n#include<bitset>\n#include<cassert>\n#include<numeric>\n#include<regex>\n#define ll long long\n#define int_max ((int)2147483647)\n#define ll_max ((ll)9223372036854775807)\n#define all(a) (a).begin(), (a).end()\nusing namespace std;\n\nvoid solve(const int d, const int w){\n\n vector<vector<int>> grid(d,vector<int>(w));\n\n for(int i=0;i<d;i++){\n for(int j=0;j<w;j++){\n cin>>grid[i][j];\n }\n }\n\n int ans=0;\n\n for(int h = 1; h < 10; h++){\n vector<vector<int>> lake(d,vector<int>(w));\n for(int i = 0; i < d; i++){\n for(int j = 0; j < w; j++){\n lake[i][j] = max(0, h - grid[i][j]);\n }\n }\n\n for(int i = 1; i < d-1; i++){\n for(int j = 1; j < w-1; j++){\n for(int x = i; x < d-1; x++){\n for(int y = j; y < w-1; y++){\n \n int v = 0;\n bool isLake = true;\n\n for(int k = i-1; k <= x+1; k++){\n for(int l = j-1; l <= y+1; l++){\n if((k == i-1 || k == x+1) || (l == j-1 || l == y+1)){\n if(lake[k][l] != 0){\n isLake = false;\n }\n }\n else{\n if(lake[k][l] == 0){\n isLake = false;\n }\n else{\n v += lake[k][l];\n }\n }\n }\n }\n\n if(isLake){\n ans = max(ans, v);\n }\n }\n }\n }\n }\n }\n\n cout<<ans<<endl;\n\n}\n\nint main(){\n\n int d,w;\n cin>>d>>w;\n\n while(0<d+w){\n solve(d,w);\n cin>>d>>w;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3380, "score_of_the_acc": -1.3476, "final_rank": 17 }, { "submission_id": "aoj_1618_9405426", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\nusing vi = vector<int>;\nusing vl = vector<long long>;\nusing vs = vector<string>;\nusing vc = vector<char>;\nusing vb = vector<bool>;\nusing vpii = vector<pair<int, int>>;\nusing vpll = vector<pair<long long, long long>>;\nusing vvi = vector<vector<int>>;\nusing vvl = vector<vector<long long>>;\nusing vvc = vector<vector<char>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing pii = pair<int, int>;\nusing vvb = vector<vector<bool>>;\nusing Graph = vector<vector<ll>>;\nconst ll LINF = 1001002003004005006ll;\nconst int INF = 1001001001;\nconst int MOD = 998244353;\n\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rep3(i, m, n) for (int i = (m); i < (int)(n); i++)\n#define rrep(i, m, n) for (int i = (m); i >= (int)(n); i--)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntemplate <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }\ntemplate <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }\n\nvi dx4 = {1, 0, -1, 0}, dy4 = {0, 1, 0, -1};\nvi dx8 = {1, 1, 1, 0, 0, -1, -1, -1}, dy8 = {1, 0, -1, 1, -1, 1, 0, -1};\n\nint solve(vvi e, int t, int b, int l, int r)\n{\n int min = INF;\n rep3(i, t, b + 1) { chmin(min, e[i][l]); }\n rep3(i, t, b + 1) { chmin(min, e[i][r]); }\n rep3(i, l, r + 1) { chmin(min, e[t][i]); }\n rep3(i, l, r + 1) { chmin(min, e[b][i]); }\n int max = 0;\n rep3(i, t + 1, b)\n {\n rep3(j, l + 1, r) { chmax(max, e[i][j]); }\n }\n if (min <= max)\n {\n return 0;\n }\n int res = 0;\n rep3(i, t + 1, b)\n {\n rep3(j, l + 1, r) { res += min - e[i][j]; }\n }\n return res;\n}\n\nint main()\n{\n while (1)\n {\n // 宣言・入力の受け取り\n int d, w;\n cin >> d >> w;\n // 終了条件\n if (d == 0 && w == 0)\n {\n break;\n }\n // 宣言・入力の受け取り\n vvi e(d, vi(w));\n rep(i, d)\n {\n rep(j, w) { cin >> e[i][j]; }\n }\n // solve\n int ans = 0;\n rep(t, d)\n {\n rep(l, w)\n {\n rep(b, d)\n {\n rep(r, w)\n {\n if (t + 2 <= b && l + 2 <= r)\n {\n chmax(ans, solve(e, t, b, l, r));\n }\n }\n }\n }\n }\n // 出力\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3344, "score_of_the_acc": -0.7619, "final_rank": 11 }, { "submission_id": "aoj_1618_9377643", "code_snippet": "#include <bits/stdc++.h>\n\n// #include <atcoder/all>\n\nusing namespace std;\n// using namespace atcoder;\ntypedef long long ll;\ntemplate <class T, class... Ts>\nvoid println(const T& a, const Ts&... b) {\n cout << a;\n (void)(cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\ntemplate <class T>\nvoid printv(const T& a, string sep = \" \", string end = \"\\n\") {\n for (auto x : a) {\n (void)(cout << x << sep);\n }\n cout << end;\n}\nvoid println() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid eprintln(const T& a, const Ts&... b) {\n cerr << a;\n (void)(cerr << ... << (cerr << ' ', b));\n cerr << '\\n';\n}\ntemplate <class T>\nvoid eprintv(const T& a, string sep = \" \", string end = \"\\n\") {\n for (auto x : a) {\n (void)(cerr << x << sep);\n }\n cerr << end;\n}\nvoid eprintln() { cerr << '\\n'; }\ntemplate <class... T>\nvoid input(T&... a) { (cin >> ... >> a); }\n#define rep(i, n) for (ll i = 0; i < n; i++)\n#define rep1(i, n) for (ll i = 1; i <= n; i++)\n#define yesno(a) cout << (a ? \"Yes\" : \"No\") << '\\n';\n#define YESNO(a) cout << (a ? \"YES\" : \"NO\") << '\\n';\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n cout << fixed << setprecision(15);\n\n while (1) {\n ll n, m;\n input(n, m);\n if (n == 0 && m == 0) break;\n\n vector<vector<ll>> board(n, vector<ll>(m));\n rep(i, n) rep(j, m) input(board[i][j]);\n\n // rep(sx, n - 2) {\n ll best_ans = 0;\n for (ll sx = 0; sx < n - 2; sx++) {\n for (ll ex = sx + 2; ex < n; ex++) {\n for (ll sy = 0; sy < m - 2; sy++) {\n for (ll ey = sy + 2; ey < m; ey++) {\n // println(\"RANGE\", sx, ex, sy, ey);\n ll maxv = 0;\n ll minv = LLONG_MAX;\n set<ll> outer, inner;\n vector<ll> inner_all;\n for (ll x = sx; x <= ex; x++) {\n for (ll y = sy; y <= ey; y++) {\n // println(x, y);\n if ((x == sx || x == ex) || (y == sy || y == ey)) {\n // println(\"outer\", x, y);\n outer.insert(board[x][y]);\n } else {\n // println(\"inner\", x, y);\n inner.insert(board[x][y]);\n inner_all.push_back(board[x][y]);\n }\n }\n }\n // printv(outer);\n // printv(inner);\n ll lower_outer = *outer.begin();\n ll upper_inner = *inner.rbegin();\n // println(lower_outer, upper_inner);\n if (lower_outer > upper_inner) {\n ll tot = 0;\n for (auto i : inner_all) {\n tot += (lower_outer - i);\n }\n best_ans = max(best_ans, tot);\n }\n }\n }\n }\n }\n println(best_ans);\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3356, "score_of_the_acc": -1.7905, "final_rank": 18 }, { "submission_id": "aoj_1618_8037499", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\nusing pint = pair<int,int>;\nconst int INF = 1e9;\n\nint main()\n{\n\n\tauto calc=[](vector<vector<int>> g) {\n\t\tint h=g.size();\n\t\tint w=g[0].size();\n\t\tint soto=g[0][0];\n\t\tfor(int i=0;i<h;i++)soto=min({soto,g[i][0],g[i][w-1]});\n\t\tfor(int i=0;i<w;i++)soto=min({soto,g[0][i],g[h-1][i]});\n\t\t\n\t\tint sum=0;\n\t\tfor(int i=1;i<h-1;i++){\n\t\t\tfor(int j=1; j<w-1;j++){\n\t\t\t\tif(g[i][j]>=soto)sum=-INF;\n\t\t\t\telse sum+=soto-g[i][j];\n\t\t\t}\n\t\t}\n\t\treturn sum;\n\t};\n\n\twhile(true) {\n\t\tint d,w;\n\t\tcin>>d>>w;\n\t\tif(d+w==0)break;\n\t\tvector<vector<int>> g(d,vector<int>(w));\n\t\tfor(auto &i:g)for(auto &j:i)cin>>j;\n\n\t\tauto get_grid = [&](int y1,int x1, int y2, int x2) {\n\t\t\tvector<vector<int>> grid(y2-y1,vector<int>(x2-x1));\n\t\t\tfor(int i=y1;i<y2;i++){\n\t\t\t\tfor(int j=x1;j<x2;j++){\n\t\t\t\t\tgrid[i-y1][j-x1]=g[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn grid;\n\t\t};\n\n\t\tint ans = -INF;\n\t\tfor(int y1=0;y1<d;y1++){\n\t\t\tfor(int x1=0;x1<w;x1++){\n\t\t\t\tfor(int y2=y1+3;y2<=d;y2++){\n\t\t\t\t\tfor(int x2=x1+3;x2<=w;x2++){\n\n\t\t\t\t\t\tvector<vector<int>> grid=get_grid(y1,x1,y2,x2);\n\t\t\t\t\t\tint ret=calc(grid);\n\t\t\t\t\t\tans=max(ans,ret);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcout<<(ans<0?0:ans)<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.781, "final_rank": 12 }, { "submission_id": "aoj_1618_6981089", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int d, w, e[22][22], cnt[22][22], sum, ans;\n bool ike;\n while(cin >> d >> w, d!=0){\n for(int i=0;i<d;i++){\n for(int j=0;j<w;j++){\n cin >> e[i][j];\n cnt[i][j] = 0;\n }\n }\n ans = 0;\n for(int k=1;k<=9;k++){\n for(int i=0;i<d;i++){\n for(int j=0;j<w;j++){\n if(e[i][j] < k){\n cnt[i][j]++;\n }\n }\n }\n for(int y1=0;y1<d;y1++){\n for(int y2=y1+2;y2<d;y2++){\n for(int x1=0;x1<w;x1++){\n for(int x2=x1+2;x2<w;x2++){\n ike = true;\n sum = 0;\n if(count(cnt[y1]+x1, cnt[y1]+x2+1, 0) < x2-x1+1) ike = false;\n if(count(cnt[y2]+x1, cnt[y2]+x2+1, 0) < x2-x1+1) ike = false;\n for(int i=y1+1;i<y2;i++){\n if(cnt[i][x1]!=0 || cnt[i][x2]!=0 || count(cnt[i]+x1+1, cnt[i]+x2, 0) > 0) ike = false;\n sum += accumulate(cnt[i]+x1, cnt[i]+x2+1, 0);\n }\n if(ike){\n ans = max(ans, sum);\n }\n }\n }\n }\n }\n \n }\n cout << ans << endl;\n }\n return(0);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3092, "score_of_the_acc": -0.1619, "final_rank": 4 }, { "submission_id": "aoj_1618_6760796", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n\n#define rep(i, a, b) for (auto i = (a); i < (b); ++i)\n#define repc(i, a, b) for (auto i = (a); i <= (b); ++i)\n#define repr(i, a, b) for (int i = (a); i >= (b); --i)\n#define each_rev(itr, a) for (auto itr = rbegin(a); itr != rend(a); ++itr)\n#define fn1(x, expr) [&](const auto& x) { return (expr); }\n#define fn2(x, y, expr) [&](const auto& x, const auto& y) { return (expr); }\n#define pb push_back\n#define eb emplace_back\n#define LEN(a) int(a.size())\n#define all(a) begin(a), end(a)\n#define rall(a) rbegin(a), rend(a)\n#define lb(a, elem) distance(std::begin(a), lower_bound(all(a), (elem)))\n#define ub(a, elem) distance(std::begin(a), upper_bound(all(a), (elem)))\n#define MIN(a) (*min_element(all(a)))\n#define MAX(a) (*max_element(all(a)))\n#define UNIQ(a) a.erase(unique(all(a)), end(a))\n#define IN(x, begin, end) ((begin) <= (x) && (x) < (end))\n// type alias\n#define let const auto\nusing i64 = int64_t;\nusing u64 = uint64_t;\nusing i32 = int32_t;\nusing u32 = uint32_t;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nusing pii = pair<int, int>;\nusing pll = pair<i64, i64>;\nusing ldouble = long double;\ntemplate <class T> using vec = std::vector<T>;\ntemplate <class T> using MaxHeap = std::priority_queue<T>;\ntemplate <class T> using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;\n// constants\nconstexpr int INF = 0x3f3f3f3f;\nconstexpr i64 LINF = 0x3f3f3f3f3f3f3f3f;\nconstexpr double PI = 3.14159265358979323846;\nconstexpr std::pair<int, int> DIR4[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\nconstexpr std::pair<int, int> DIR8[] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}};\n// utils\ntemplate <class T, class U> inline bool chmin(T& a, const U& b) { return b < a && (a = b, true); }\ntemplate <class T, class U> inline bool chmax(T& a, const U& b) { return b > a && (a = b, true); }\ntemplate <class T> inline int sgn(T x) noexcept { return (T(0) < x) - (x < T(0)); }\ntemplate <class T, class U> inline auto divup(T a, U b) noexcept { return (a + b - 1) / b; }\ntemplate <class T, class U> inline auto sigma(const T& l, const U& r) noexcept { return (l + r) * (r - l + 1) / 2; }\ninline i64 sigma(int l, int r) noexcept { return sigma((i64)l, (i64)r); }\ninline i64 choose2(i64 n) noexcept { return n * (n - 1) >> 1; }\ninline void yes(bool x = true) { cout << (x ? \"yes\\n\" : \"no\\n\"); }\ninline void Yes(bool x = true) { cout << (x ? \"Yes\\n\" : \"No\\n\"); }\ninline void YES(bool x = true) { cout << (x ? \"YES\\n\" : \"NO\\n\"); }\n#define WHEN_BYTESIZE(T, bytes) template <typename T, std::enable_if_t<sizeof(T) == (bytes), std::nullptr_t> = nullptr>\nWHEN_BYTESIZE(T, 4) inline int popcnt(T n) { return __builtin_popcount((unsigned)n); }\nWHEN_BYTESIZE(T, 8) inline int popcnt(T n) { return __builtin_popcountll((unsigned long long)n); }\nWHEN_BYTESIZE(T, 4) inline int topbit(T n) { return n == 0 ? -1 : 31 - __builtin_clz((unsigned)n); }\nWHEN_BYTESIZE(T, 8) inline int topbit(T n) { return n == 0 ? -1 : 63 - __builtin_clzll((unsigned long long)n); }\nWHEN_BYTESIZE(T, 4) inline int lowbit(T n) { return n == 0 ? 32 : __builtin_ctz((unsigned)n); }\nWHEN_BYTESIZE(T, 8) inline int lowbit(T n) { return n == 0 ? 64 : __builtin_ctzll((unsigned long long)n); }\ntemplate <typename T> inline bool ispow2(T i) { return i && (i & -i) == i; }\n\ntemplate <class Container, enable_if_t<not is_same_v<typename Container::value_type, char>, nullptr_t> = nullptr>\nistream& operator>>(istream& is, Container& a) {\n for (auto&& e : a) is >> e;\n return is;\n}\nistream& operator>>(istream& is, vector<char>& a) {\n for (auto& c: a) is >> c;\n return is;\n}\ntemplate <class Container, enable_if_t<not is_same_v<typename Container::value_type, char>, nullptr_t> = nullptr>\nostream& operator<<(ostream& os, const Container& a) {\n if (&os != &cout) {\n os << '[';\n unsigned cnt = 0;\n for (auto it = begin(a); it != end(a) && cnt < 30; ++it, ++cnt) os << *it << (cnt + 1 < size(a) ? \", \" : \"\");\n if (cnt < size(a)) os << \"...\";\n return os << ']';\n }\n if (a.size() == 0) return os;\n os << *begin(a);\n for (auto it = next(begin(a)); it != end(a); ++it) os << ' ' << *it;\n return os;\n}\nostream& operator<<(ostream& os, const vector<char>& a) {\n os << '[';\n for (auto it = a.begin(); it != a.end(); ++it) {\n if (isprint(*it)) os << '\\'' << *it << '\\'';\n else os << int(*it);\n os << \" \"[it + 1 == a.end()];\n }\n return os << ']';\n}\ntemplate <class T, class U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }\ntemplate <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << '(' << p.first << \", \" << p.second << ')'; }\n\nostream& operator<<(ostream& os, __uint128_t n) {\n if (n == 0) return os << '0';\n char buf[40]{};\n char* p = buf + 38;\n while (n) {\n *(p--) = char(n % 10) + '0';\n n /= 10;\n }\n return os << (p + 1);\n}\n\nostream& operator<<(ostream& os, __int128_t n) {\n if (n < 0) return os << '-' << __uint128_t(-n);\n return os << __uint128_t(n);\n}\n\nistream& operator>>(istream& is, __uint128_t& n) {\n int c;\n while (isspace(c = is.get()));\n n = unsigned(c - '0');\n while (isdigit(c = is.get())) n = n * 10 + unsigned(c - '0');\n return is;\n}\n\nistream& operator>>(istream& is, __int128_t& n) {\n int c;\n bool neg;\n while (isspace(c = is.get()));\n if (c == '-') {\n n = 0;\n neg = true;\n } else {\n n = c - '0';\n neg = false;\n }\n while (isdigit(c = is.get())) n = n * 10 + (c - '0');\n if (neg) n = -n;\n return is;\n}\n\ninline void echo() { std::cout << '\\n'; }\n\ntemplate <class Head, class... Tail>\ninline void echo(Head&& head, Tail&&... tail) {\n std::cout << head;\n if constexpr (sizeof...(tail)) std::cout << ' ';\n echo(std::forward<Tail>(tail)...);\n}\n\ntemplate <class... Args>\ninline void read(Args&... args) {\n (std::cin >> ... >> args);\n}\n\nstatic inline void __attribute__((constructor)) io_setup() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout << std::fixed << std::setprecision(10);\n}\n\n#ifdef LOCAL_DEBUG\n#define dump(...) (std::clog << \"\\e[34;1mL\" << __LINE__ << \": \\e[m\"), dbg_impl::dump_(dbg_impl::split_(#__VA_ARGS__).begin(), __VA_ARGS__)\nnamespace dbg_impl {\nvector<string> split_(const char* s) {\n vector<string> res;\n char buf[64], *end = buf;\n unsigned paren = 0, brace = 0;\n for (const char* p = s; *p != '\\0'; ++p) {\n if (*p == ' ') continue;\n if (*p == ',' && paren == 0 && brace == 0) {\n res.emplace_back(buf, end), end = buf;\n continue;\n }\n if (*p == '(') ++paren;\n if (*p == ')') --paren;\n if (*p == '{') ++brace;\n if (*p == '}') --brace;\n *end++ = *p;\n }\n res.emplace_back(buf, end);\n return res;\n}\nvoid dump_(vector<string>::iterator) { std::clog << std::endl; }\n\ntemplate <class Head, class... Tail>\nvoid dump_(vector<string>::iterator itr, Head&& head, Tail&&... tail) {\n std::clog << *itr << \"\\e[90;1m=\\e[33;1m\" << head << \"\\e[m, \";\n dump_(++itr, std::forward<Tail>(tail)...);\n}\n} // namespace dbg_impl\n#else\n#define dump(...) ((void)0)\n#endif\n\n#define INT(...) int __VA_ARGS__; read(__VA_ARGS__)\n#define LL(...) i64 __VA_ARGS__; read(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; read(__VA_ARGS__)\n#define CHR(...) char __VA_ARGS__; read(__VA_ARGS__)\n#define DBL(...) double __VA_ARGS__; read(__VA_ARGS__)\n#define VEC(type, name, len) vector<type> name(len); cin >> name\n\ntemplate <class T>\nstruct Cumsum {\n using value_type = T;\n std::valarray<T> data;\n Cumsum() = default;\n explicit Cumsum(const std::vector<T>& a) : data(a.size() + 1) {\n for (size_t i = 0; i < a.size(); ++i) data[i + 1] = data[i] + a[i];\n }\n //! range sum [l, r)\n inline const T sum(size_t l, size_t r) const {\n assert(l <= r);\n return data[r] - data[l];\n }\n inline size_t size() const { return data.size(); }\n inline auto begin() const { return std::begin(data); }\n inline auto end() const { return std::end(data); }\n};\n\ntemplate <class T, class Func>\ninline T bin_search(T ok, T ng, const Func& isOk) {\n while (std::abs(ok - ng) > 1) {\n const T mid = (ok + ng) >> 1;\n (isOk(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate <class ForwardItr>\nauto runlength(const ForwardItr begin, const ForwardItr end) {\n vector<pair<ForwardItr, size_t>> res;\n res.reserve(distance(begin, end));\n for (auto itr = begin; itr != end;) {\n size_t cnt = 1;\n const auto head = itr++;\n while (itr != end && *itr == *head) ++itr, ++cnt;\n res.emplace_back(head, cnt);\n }\n res.shrink_to_fit();\n return res;\n}\n\ntemplate <typename T>\nconstexpr T power(T x, int64_t exp) {\n T res(1);\n if (exp < 0) exp = -exp, x = 1 / x;\n for (; exp; exp >>= 1) {\n if (exp & 1) res *= x;\n x *= x;\n }\n return res;\n}\n\ntemplate <typename CostT>\nstruct WeightedEdge {\n using Self = WeightedEdge<CostT>;\n uint32_t to;\n CostT cost;\n\n WeightedEdge() = default;\n WeightedEdge(unsigned to_, CostT cost_) : to(to_), cost(cost_) {}\n operator int() const { return (int)to; }\n operator uint32_t() const { return (uint32_t)to; }\n bool operator<(const Self& other) const { return cost < other.cost; }\n bool operator>(const Self& other) const { return cost > other.cost; }\n bool operator<=(const Self& other) const { return cost <= other.cost; }\n bool operator>=(const Self& other) const { return cost >= other.cost; }\n friend ostream& operator<<(ostream& os, const Self& e) { return os << \"(to=\" << e.to << \", cost=\" << e.cost << \")\"; }\n};\ntemplate <class CostT>\nusing WGraph = vector<vector<WeightedEdge<CostT>>>;\n\nusing UWGraph = vector<vector<uint32_t>>;\n\nenum class Directed : bool { No, Yes };\n\ntemplate <Directed directed>\nUWGraph read_unweighted_graph(size_t vertex_num, size_t edge_num, bool decrement_node_id = true) {\n UWGraph g(vertex_num);\n for (size_t i = 0; i < edge_num; ++i) {\n uint32_t a, b;\n cin >> a >> b;\n if (decrement_node_id) --a, --b;\n g[a].push_back(b);\n if constexpr (directed == Directed::No) g[b].push_back(a);\n }\n return g;\n}\n\ntemplate <Directed directed, class CostT>\nWGraph<CostT> read_weighted_graph(size_t vertex_num, size_t edge_num, bool decrement_node_id = true) {\n WGraph<CostT> g(vertex_num);\n for (size_t i = 0; i < edge_num; ++i) {\n uint32_t a, b;\n CostT cost;\n cin >> a >> b >> cost;\n if (decrement_node_id) --a, --b;\n g[a].emplace_back(b, cost);\n if constexpr (directed == Directed::No) g[b].emplace_back(a, cost);\n }\n return g;\n}\n\n// -------------------- main --------------------\n\nint main() {\n int H, W;\n while (cin >> H >> W, H && W) {\n auto S = vector(H, vector(W, 0));\n rep(i, 0, H) {\n cin >> S[i];\n }\n\n int ans = 0;\n rep(sy, 0, H) rep(sx, 0, W) {\n rep(gy, sy + 2, H) rep(gx, sx + 2, W) {\n\n vec<int> inner;\n vec<int> outer;\n repc(i, sy, gy) repc(j, sx, gx) {\n if (i == sy || i == gy || j == sx || j == gx) {\n outer.push_back(S[i][j]);\n } else {\n inner.push_back(S[i][j]);\n }\n }\n\n let outer_min = MIN(outer);\n let inner_max = MAX(inner);\n if (outer_min <= inner_max) continue;\n\n int sum = 0;\n for (let x : inner) {\n sum += outer_min - x;\n }\n\n chmax(ans, sum);\n }\n }\n\n echo(ans);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3380, "score_of_the_acc": -0.8476, "final_rank": 14 }, { "submission_id": "aoj_1618_5970485", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define FOR(i, a, b) for (int i = (a); i < (b); i++)\n#define REP(i, n) for (int i = 0; i < (n); i++)\n#define ALL(obj) (obj).begin(), (obj).end()\n#define vi vector<int>\n#define vvi vector<vector<int>>\n#define vs vector<string>\n#define vc vector<char>\n#define pii pair<int, int>\n#define us unordered_set\n#define ud unordered_dict\n#define um unordered_map\n#define Lower_bound(vec, n) distance((vec).begin(), lower_bound((vec).begin(), (vec).end(), (n)))\n#define Upper_bound(vec, n) distance((vec).begin(), upper_bound((vec).begin(), (vec).end(), (n)))\n#define Erase(vec) (vec).erase(unique((vec).begin(), (vec).end()), (vec).end())\n#define Find(vec, n) find((vec).begin(), (vec).end(), n)\n#define rev_sort(vec) sort((vec).begin(), (vec).end(), greater<int>())\nusing Graph = vector<vector<int>>;\nconst int INF = 1LL << 60;\nconst int MOD = (int)1e9 + 7;\nconst double EPS = 1e-9;\n//const string alphabet = abcdefghijklmnopqrstuvwxyz;\n//const string Alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ;\n\nsigned main() {\n int d, w;\n while (true) {\n cin >> d >> w;\n if (d == 0) break;\n\n vvi field(d, vector<int>(w));\n int intmp;\n REP(i, d) {\n REP(j, w) {\n cin >> intmp;\n field[i][j] = intmp;\n }\n }\n int ans = 0;\n\n FOR(height, 3, d+1) {\n FOR(width, 3, w+1) {\n REP(r_head, d+1-height) {\n REP(c_head, w+1-width) {\n vi out, in;\n FOR(r, r_head, r_head+height) {\n FOR(c, c_head, c_head+width) {\n if ((r == r_head || c == c_head) || (r == r_head+height-1 || c == c_head+width-1)) {\n out.push_back(field[r][c]);\n }\n else {\n in.push_back(field[r][c]);\n }\n }\n }\n int out_min = *min_element(ALL(out));\n int in_max = *max_element(ALL(in));\n if (out_min <= in_max) continue;\n int volume = out_min * in.size() - accumulate(ALL(in), 0);\n ans = max(ans, volume);\n }\n }\n }\n }\n\n cout << ans << endl;\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3444, "score_of_the_acc": -1, "final_rank": 16 }, { "submission_id": "aoj_1618_5849240", "code_snippet": "#include <algorithm>\n#include <iomanip>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <deque>\n#include <functional>\n#include <iostream>\n#include <iterator>\n#include <map>\n#include <queue>\n#include <set>\n#include <string>\n#include <sstream>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <numeric>\n#include <vector>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n#define GET_MACRO(_1, _2, _3, NAME, ...) NAME\n#define _rep(i, n) _rep2(i, 0, n)\n#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)\n#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nusing i64 = long long;\ntemplate<class T, class U>\nbool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }\ntemplate<class T, class U>\nbool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }\n\ntemplate<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}\ntemplate<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}\ntemplate<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}\ntemplate<typename T>string join(vector<vector<T>>&vv){string s=\"\\n\";rep(i,vv.size())s+=join(vv[i])+\"\\n\";return s;}\ntemplate<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}\ntemplate<class T> using pq = priority_queue<T, vector<T>, greater<T>>;\n\nint main() {\n while(1) {\n int d, w;\n cin >> d >> w;\n if(d == 0 && w == 0) break;\n vector table(d, vector<int>(w));\n rep(i, d) rep(j, w) cin >> table[i][j];\n int ans = 0;\n rep(i1, d) {\n rep(j1, w){\n rep(i2, i1 + 1, d) {\n rep(j2, j1 + 1, w) {\n vector<int> out, in;\n //上\n rep(j3, j1, j2 + 1) out.push_back(table[i1][j3]);\n //下\n rep(j3, j1, j2 + 1) out.push_back(table[i2][j3]);\n //左\n rep(i3, i1, i2 + 1) out.push_back(table[i3][j1]);\n //右\n rep(i3, i1, i2 + 1) out.push_back(table[i3][j2]);\n rep(i3, i1 + 1, i2) {\n rep(j3, j1 + 1, j2) {\n in.push_back(table[i3][j3]);\n }\n }\n if(int(in.size()) == 0) continue;\n int out_min = *min_element(all(out));\n int in_max = *max_element(all(in));\n if(out_min <= in_max) continue;\n int now_ans = 0;\n for(auto now : in) now_ans += out_min - now;\n chmax(ans, now_ans);\n }\n }\n }\n }\n cout << ans << endl;\n } \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3364, "score_of_the_acc": -0.8095, "final_rank": 13 }, { "submission_id": "aoj_1618_5849233", "code_snippet": "#include <algorithm>\n#include <iomanip>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cmath>\n#include <cstdio>\n#include <deque>\n#include <functional>\n#include <iostream>\n#include <iterator>\n#include <map>\n#include <queue>\n#include <set>\n#include <string>\n#include <sstream>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <numeric>\n#include <vector>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n#define GET_MACRO(_1, _2, _3, NAME, ...) NAME\n#define _rep(i, n) _rep2(i, 0, n)\n#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)\n#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\nusing i64 = long long;\ntemplate<class T, class U>\nbool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }\ntemplate<class T, class U>\nbool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }\n\ntemplate<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}\ntemplate<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}\ntemplate<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}\ntemplate<typename T>string join(vector<vector<T>>&vv){string s=\"\\n\";rep(i,vv.size())s+=join(vv[i])+\"\\n\";return s;}\ntemplate<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}\ntemplate<class T> using pq = priority_queue<T, vector<T>, greater<T>>;\n\nint main() {\n while(1) {\n int d, w;\n cin >> d >> w;\n if(d == 0 && w == 0) break;\n vector table(d, vector<int>(w));\n rep(i, d) rep(j, w) cin >> table[i][j];\n int ans = 0;\n rep(i1, d) {\n rep(j1, w){\n rep(i2, i1 + 1, d) {\n rep(j2, j1 + 1, w) {\n vector<int> out, in;\n //上\n rep(j3, j1, j2 + 1) out.push_back(table[i1][j3]);\n // if(i1 == 0 && j1 == 0 && i2 == 2 && j2 == 3) {\n // cout << out << endl;\n // }\n //下\n rep(j3, j1, j2 + 1) out.push_back(table[i2][j3]);\n // if(i1 == 0 && j1 == 0 && i2 == 2 && j2 == 3) {\n // cout << out << endl;\n // }\n //左\n rep(i3, i1, i2 + 1) out.push_back(table[i3][j1]);\n // if(i1 == 0 && j1 == 0 && i2 == 2 && j2 == 3) {\n // cout << out << endl;\n // }\n //右\n rep(i3, i1, i2 + 1) out.push_back(table[i3][j2]);\n rep(i3, i1 + 1, i2) {\n rep(j3, j1 + 1, j2) {\n in.push_back(table[i3][j3]);\n }\n }\n if(int(in.size()) == 0) continue;\n int out_min = *min_element(all(out));\n int in_max = *max_element(all(in));\n if(out_min <= in_max) continue;\n int now_ans = 0;\n // cout << \"来てる\" << endl;\n for(auto now : in) now_ans += out_min - now;\n // cout << \"now_ans : \" << now_ans << endl;\n if(now_ans == 5) {\n // cout << i1 << \" \" << j1 << \" \" << i2 << \" \" << j2 << endl;\n // cout << \"out : \" << out << endl;\n // cout << \"in : \" << in << endl;\n }\n chmax(ans, now_ans);\n }\n }\n }\n }\n cout << ans << endl;\n } \n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3336, "score_of_the_acc": -0.7429, "final_rank": 10 }, { "submission_id": "aoj_1618_4939903", "code_snippet": "#include <bits/stdc++.h>\n\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n//#include <boost/multiprecision/cpp_int.hpp>\nusing namespace std;\n\n//using namespace boost::multiprecision;\n//#include<atcoder/all>\n//using namespace atcoder;\n\nusing dou =long double;\nstring yes=\"yes\";\nstring Yes=\"Yes\";\nstring YES=\"YES\";\nstring no=\"no\";\nstring No=\"No\";\nstring NO=\"NO\";\n\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<ll,ll> PL;\n\nconst ll mod = 998244353ll;\n//const ll mod = 1000000007ll;\n//const ll mod = 100000;\n\n\nstruct mint {\n ll x; // typedef long long ll;\n mint(ll x=0):x((x%mod+mod)%mod){}\n mint operator-() const { return mint(-x);}\n mint& operator+=(const mint a) {\n if ((x += a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator-=(const mint a) {\n if ((x += mod-a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}\n mint operator+(const mint a) const { return mint(*this) += a;}\n mint operator-(const mint a) const { return mint(*this) -= a;}\n mint operator*(const mint a) const { return mint(*this) *= a;}\n mint pow(ll t) const {\n if (!t) return 1;\n mint a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n\n // for prime modhttps://atcoder.jp/contests/abc166/submit?taskScreenName=abc166_f\n mint inv() const { return pow(mod-2);}\n mint& operator/=(const mint a) { return *this *= a.inv();}\n mint operator/(const mint a) const { return mint(*this) /= a;}\n};\n\nistream& operator>>(istream& is, const mint& a) { return is >> a.x;}\nostream& operator<<(ostream& os, const mint& a) { return os << a.x;}\n\n#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)\n#define brep(n) for(int bit=0;bit<(1<<n);bit++)\n#define bbrep(n) for(int bbit=0;bbit<(1<<n);bbit++)\n#define erep(i,container) for (auto &i : container)\n#define itrep(i,container) for (auto i : container)\n#define irep(i, n) for(ll i = n-1; i >= (ll)0ll; i--)\n#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)\n#define reprep(i,j,h,w) rep(i,h)rep(j,w)\n#define repreprep(i,j,k,h,w,n) rep(i,h)rep(j,w)rep(k,n)\n#define all(x) (x).begin(),(x).end()\n#define rall(x) (x).rbegin(),(x).rend()\n#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];\n#define pb push_back\n#define pf push_front\n#define query int qq;std::cin >> qq;rep(qqq,qq)\n#define lb lower_bound\n#define ub upper_bound\n#define fi first\n#define se second\n#define itn int\n#define mp make_pair\n//#define sum(a) accumulate(all(a),0ll)\n#define keta fixed<<setprecision\n#define vout(a) erep(qxqxqx,a)std::cout << qxqxqx << ' ';std::cout << std::endl;\n#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))\n//#define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))\n#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));\n#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));\n#define case std::cout <<\"Case #\" <<qqq+1<<\":\"\n#define RES(a,i,j) a.resize(i);rep(ii,i)a[ii].resize(j);\n#define RESRES(a,i,j,k) a.resize(i);rep(ii,i)a[ii].resize(j);reprep(ii,jj,i,j){dp[ii][jj].resize(k)};\n#define res resize\n#define as assign\n#define ffor for(;;)\n#define ppri(a,b) std::cout << a<<\" \"<<b << std::endl\n#define pppri(a,b,c) std::cout << a<<\" \"<<b <<\" \"<< c<<std::endl\n#define ppppri(a,b,c,d) std::cout << a<<\" \"<<b <<\" \"<< c<<' '<<d<<std::endl\n#define aall(x,n) (x).begin(),(x).begin()+(n)\n#define SUM(a) accumulate(all(a),0ll) \n#define stirng string\n#define gin(a,b) int a,b;std::cin >> a>>b;a--;b--;\n#define popcount __builtin_popcount\n#define permu(a) next_permutation(all(a))\n#define aru(a,d) a.find(d)!=a.end()\n\n//#define grid_input(a,type) int h,w;std::cin >> h>>w;vvector(a,type,h,w,0);reprep(i,j,h,w)std::cin >> a[i][j];\n\n//typedef long long T;\nll ceili(ll a,ll b){\n return ((a+b-1)/b);\n}\nconst int INF = 2'000'000'000;\nconst ll INF64 =32233720854775807ll;\n//const ll INF64 = 9223372036854775807ll;\n\n//const ll INF64 = 243'000'000'000'000'000'0;\n\n\n//const ll MOD = 100'000'000'7ll;\nconst ll MOD = 998244353ll;\n//const ll MOD = 1000003ll;\nconst ll OD = 1000000000000007ll;\nconst dou pi=3.141592653589793;\n\nlong long modpow(long long a, long long n) { //累乗の剰余\n long long res = 1;\n while (n > 0) {\n if (n & 1) res = res * a % MOD;\n a = a * a % MOD;\n n >>= 1;\n }\n return res;\n}\n\n//メモ\n//ゲーム(Grundy数とか)の復習をする\n//リスニング力をどうにかする\n//モノイドをやる!!!!\n//特に理由がなければllを使おうキャンペーン\n//個数制限付きナップサックの復習\n//戻すDP\n//周期性の解析結果の考察をする\n//いい加減英語スライドを作り始める\n//改良モデルを深堀りすると良さそう\n//被服調査票を提出する\n//PGバトル 10/24 13:00~\n//学科棟まで書類を受け取りに行く\n//全方位木DPとスライド最小値\n//やること困ったらSRM 500番台やるといいかも\n\nint main(){\n ffor{\n int h,w;\n std::cin >> h>>w;\n if(h==0)break;\n vvector(a,int,h,w,0);\n reprep(i,j,h,w)std::cin >> a[i][j];\n int ans=0;\n reprep(i,j,h,w){\n rrep(ii,i+2,h){\n rrep(jj,j+2,w){\n bool ok=1;\n std::vector<int> d;\n int ma=0,mi=INF;\n rrep(y,i,ii+1){\n rrep(x,j,jj+1){\n if(y==i||y==ii||x==j||x==jj){\n chmin(mi,a[y][x]);\n }\n else{\n chmax(ma,a[y][x]);\n d.pb(a[y][x]);\n }\n }\n }\n if(mi<=ma)continue;\n int dum=0;\n erep(p,d)dum+=mi-p;\n chmax(ans,dum);\n }\n }\n }\n std::cout << ans << std::endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3188, "score_of_the_acc": -0.3905, "final_rank": 9 }, { "submission_id": "aoj_1618_4892512", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define N (1000000000+7)\n//#define N (998244353)\n#define INF 1e16\ntypedef long long ll;\n\nint board[20][20];\n\nint calc(int x,int y,int z, int w){\n int M1= 1000;\n vector<int>s;\n for(int i=x;i<=y;i++){\n for(int j=z;j<=w;j++){\n if(i==x||i==y){\n M1 = min(M1,board[i][j]);\n }\n else{\n if(j==z||j==w){\n M1 = min(M1,board[i][j]);\n }\n else{\n s.push_back(board[i][j]);\n }\n }\n }\n }\n sort(s.begin(),s.end(),greater<int>());\n if(M1<=s[0])return 0;\n else{\n int ans = 0;\n for(int i=0;i<s.size();i++){\n ans+=abs(M1-s[i]);\n }\n return ans;\n }\n\n}\n\nint main(void){\n while(true){\n int d,w;\n cin>>d>>w;\n if(d==0 && w==0)break;\n for(int i=0;i<d;i++){\n for(int j=0;j<w;j++){\n cin>>board[i][j];\n }\n }\n int ans = 0;\n for(int i=0;i<d;i++){\n for(int j=i+2;j<d;j++){\n for(int k=0;k<w;k++){\n for(int l=k+2;l<w;l++){\n ans = max(ans,calc(i,j,k,l));\n }\n }\n }\n }\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3436, "score_of_the_acc": -0.981, "final_rank": 15 }, { "submission_id": "aoj_1618_4887439", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\n// debug methods\n// usage: debug(x,y);\n#define CHOOSE(a) CHOOSE2 a\n#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x\n#define debug_1(x1) cout<<#x1<<\": \"<<x1<<endl\n#define debug_2(x1,x2) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<endl\n#define debug_3(x1,x2,x3) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<endl\n#define debug_4(x1,x2,x3,x4) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<endl\n#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<\": \"<<x1<<\", \"#x2<<\": \"<<x2<<\", \"#x3<<\": \"<<x3<<\", \"#x4<<\": \"<<x4<<\", \"#x5<<\": \"<<x5<<endl\n#ifdef _DEBUG\n#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\n#define MAX_N (1000006)\n#define INF (1LL << 60)\nconst int MOD = (int)1e9 + 7;\n\n\nsigned main(){\n while(1){\n int d,w;\n cin >> d >> w;\n if(d==0)break;\n vector<vector<int>> e(d, vector<int>(w));\n for(int i=0; i<d; i++)\n for(int j=0; j<w; j++)\n cin >> e[i][j];\n\n int ans = 0;\n for(int i=0; i<d; i++){\n for(int j=0; j<w; j++){\n // (i,j) (k,l) が池\n for(int k=i; k<d; k++){\n for(int l=j; l<w; l++){\n int mn = INF;\n vector<int> inner;\n for(int p=i; p<=k; p++){\n for(int q=j; q<=l; q++){\n if(p==i || p==k || q==j || q==l) {\n mn = min(mn, e[p][q]);\n } else {\n inner.push_back(e[p][q]);\n }\n }\n }\n if(inner.empty()) continue;\n int sum = 0;\n bool f=true;\n for(auto x: inner) {\n if(mn-x<=0){\n f=false;\n break;\n }\n sum += mn-x;\n }\n if(f){\n ans = max(ans, sum);\n }\n }\n }\n }\n }\n cout << ans << endl;\n }\n}\n\n/*\n\n*/", "accuracy": 1, "time_ms": 10, "memory_kb": 3152, "score_of_the_acc": -0.3048, "final_rank": 7 }, { "submission_id": "aoj_1618_3957502", "code_snippet": "#include <bits/stdc++.h>\n#define REP(i, n) for (int i = 0; i < (int)(n); i++)\nusing namespace std;\n\nint main() {\n for (int d, w; cin >> d >> w, d;) {\n vector<vector<int>> e(d, vector<int>(w));\n REP(i, d) REP(j, w) cin >> e[i][j];\n int ans = 0;\n REP(sx, d) REP(sy, w) {\n REP(tx, d) REP(ty, w) {\n if (sx >= tx || sy >= ty) continue;\n int mn = 1e9;\n REP(i, d) {\n if (i < sx || i > tx) continue;\n mn = min(mn, e[i][sy]);\n mn = min(mn, e[i][ty]);\n }\n REP(j, w) {\n if (j < sy || j > ty) continue;\n mn = min(mn, e[sx][j]);\n mn = min(mn, e[tx][j]);\n }\n bool f = false;\n int c = 0;\n REP(i, d) REP(j, w) {\n if (i <= sx || i >= tx) continue;\n if (j <= sy || j >= ty) continue;\n if (mn <= e[i][j]) f = true;\n c += mn - e[i][j];\n }\n if (f) continue;\n ans = max(ans, c);\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3036, "score_of_the_acc": -0.0286, "final_rank": 2 }, { "submission_id": "aoj_1618_3727368", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(long long int i=0;i<n;++i)\ntypedef long long int ll;\n\nint func(vector<vector<int>> m,int a,int b,int c,int d){\n int ret=10;\n for(int i=b;i<=d;i++){\n ret=min({ret,m[a][i],m[c][i]});\n }\n for(int i=a;i<=c;i++){\n ret=min({ret,m[i][b],m[i][d]});\n }\n int ff=-1;\n a++;b++;c--;d--;\n for(int i=b;i<=d;i++){\n ff=max({ff,m[a][i],m[c][i]});\n }\n for(int i=a;i<=c;i++){\n ff=max({ff,m[i][b],m[i][d]});\n }\n if(ff>=ret){\n ret=-1;\n }\n return ret;\n}\n\nint main(){\n\n while(1){\n int d,w;\n cin >> d >> w;\n if(d==0)break;\n vector<vector<int>> m(d,vector<int>(w));\n for(int i=0;i<d;i++){\n for(int j=0;j<w;j++){\n cin >> m[i][j];\n }\n }\n int ans=0;\n for(int i=0;i<=d-3;i++){\n for(int j=0;j<=w-3;j++){\n for(int k=i+2;k<d;k++){\n for(int l=j+2;l<w;l++){\n int mi=func(m,i,j,k,l);\n int su=0;\n if(mi==-1)continue;\n for(int s=i+1;s<k;s++){\n for(int t=j+1;t<l;t++){\n su+=max(0,mi-m[s][t]);\n }\n }\n ans=max(ans,su);\n }\n }\n }\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3060, "score_of_the_acc": -0.0857, "final_rank": 3 }, { "submission_id": "aoj_1618_3680133", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n\n\nint main(){\n\nwhile(1){\n int d,w;\n cin >> d >>w;\n if(d==0 && w==0)break;\n int ground[d][w];\n for(int i=0;i<d;i++){\n for(int j=0;j<w;j++){\n cin >>ground[i][j];\n }\n }\n int ans=0;\n vector<int> lake(0);\n vector<int> wall(0);\n for(int dstart=0;dstart<d-2;dstart++){\n for(int dend=dstart+2;dend<d;dend++){\n for(int wstart=0;wstart<w-2;wstart++){\n for(int wend=wstart+2;wend<w;wend++){\n lake.clear();\n wall.clear();\n for(int dd=dstart;dd<=dend;dd++){\n for(int ww=wstart;ww<=wend;ww++){\n if(dd==dstart || dd==dend || ww==wstart|| ww==wend){\n wall.push_back(ground[dd][ww]);\n }\n else lake.push_back(ground[dd][ww]);\n }\n }\n if(wall.size()!=0)sort(wall.begin(),wall.end());\n if(lake.size()!=0){\n sort(lake.begin(),lake.end());\n reverse(lake.begin(),lake.end());\n }\n int temp=0;\n for(int pl=0;pl<lake.size();pl++)temp+=(wall[0]-lake[pl]);\n if(wall[0]<=lake[0])temp=0;\n ans=max(ans,temp);\n }\n }\n }\n }\n cout << ans <<endl;\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3100, "score_of_the_acc": -0.181, "final_rank": 5 }, { "submission_id": "aoj_1618_3677912", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint solve(int d, int w) {\n int e[d][w];\n for (int i = 0; i < d; i++) {\n for (int j = 0; j < w; j++) {\n cin >> e[i][j];\n }\n }\n int ans = 0;\n for (int x1 = 0; x1 < d; x1++) {\n for (int x2 = 0; x2 < d; x2++) {\n if (x2 - x1 < 2) continue;\n for (int y1 = 0; y1 < w; y1++) {\n for (int y2 = 0; y2 < w; y2++) {\n if (y2 - y1 < 2) continue;\n int inner_height = 0;\n int surround_height = 100;\n int inner_sum = 0;\n int counter = 0;\n for (int i = 0; i < d; i++) {\n for (int j = 0; j < w; j++) {\n if (x1 < i && i < x2 && y1 < j && j < y2) {\n inner_height = max(inner_height, e[i][j]);\n inner_sum += e[i][j];\n counter++;\n }\n if (x1 <= i && i <= x2 && (j == y1 || j == y2)) {\n surround_height = min(surround_height, e[i][j]);\n }\n if (y1 <= j && j <= y2 && (i == x1 || i == x2)) {\n surround_height = min(surround_height, e[i][j]);\n }\n }\n }\n if (surround_height > inner_height) {\n int tmp = counter * surround_height - inner_sum;\n ans = max(ans, tmp);\n }\n }\n }\n }\n }\n return ans;\n}\n\nint main() {\n int d, w;\n while (true) {\n cin >> d >> w;\n if (!d && !w) return 0;\n cout << solve(d, w) << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3024, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1618_3342793", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = int64_t;\nusing P = pair<ll, ll>;\n\nconst ll INF = 5e15;\n\nint main(){\n while(1){\n ll H, W;\n ll ans = 0;\n cin >> H >> W;\n if(!H && !W) break;\n vector<vector<ll>> V(H, vector<ll>(W));\n for(auto &v : V) for(auto &e : v) cin >> e;\n for(ll up = 0; up < H; up++){\n for(ll down = up + 2; down < H; down++){\n for(ll left = 0; left < W; left++){\n for(ll right = left + 2; right < W; right++){\n ll mine = INF, maxl = 0;\n ll suml = 0;\n for(ll h = 0; h < H; h++){\n for(ll w = 0; w < W; w++){\n if(!(left <= w && w <= right &&\n up <= h && h <= down)) continue;\n if(h == up || \n h == down ||\n w == left || \n w == right){\n mine = min(mine, V[h][w]);\n }else{\n maxl = max(maxl, V[h][w]);\n suml += V[h][w];\n }\n }\n }\n if(mine <= maxl) continue;\n ll lenh = down - up + 1;\n ll lenw = right - left + 1;\n ll area = lenh * lenw - 2 * lenh - 2 * lenw + 4;\n ans = max(ans, area * mine - suml);\n }\n }\n }\n }\n cout << ans << '\\n';\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3156, "score_of_the_acc": -0.3143, "final_rank": 8 }, { "submission_id": "aoj_1618_2883107", "code_snippet": "#include<iostream>\n#include<algorithm>\n#include<vector>\n\nusing namespace std;\n\nstruct Pond\n{\n int up,down,left,right;\n vector<int> canal;\n vector<int> moat;\n int min,max;\n void make(int up,int down,int left,int right,vector<int> canal,vector<int>moat)\n {\n this->up=up; this->down=down; this->left=left; this->right=right;\n this->canal=canal; this->moat=moat;\n }\n void setVals(){\n max= *max_element(canal.begin(),canal.end());\n min= *min_element(moat.begin(),moat.end());\n }\n bool isMake()\n {\n return (max<min);\n }\n};\n\nint calc(Pond pond)\n{\n int sum=0;\n for(int i=0;i<pond.canal.size();i++){\n sum+=pond.min-pond.canal[i];\n }\n return sum;\n}\nvoid print(vector<int> list){\n for(int x:list) cout<<x<<\" \";\n cout<<endl;\n}\nint main()\n{\n while(1){\n int depth,width;\n vector<int> canal(0),moat(0);\n cin>>depth>>width;\n int stage[depth][width];\n if((depth==0)and(width==0)) return 0;\n \n for(int i=0;i<depth;i++){\n for(int j=0;j<width;j++){\n\tcin>>stage[i][j];\n }\n }\n int mx=0;\n for(int up=0;up<depth-1;up++){\n for(int left=0;left<width-1;left++){\n\tfor(int down=up+2;down<depth;down++){\n\t for(int right=left+2;right<width;right++){\n\t //cout<<\"up=\"<<up<<\" down=\"<<down<<endl;\n\t //cout<<\"left=\"<<left<<\" right=\"<<right<<endl;\n\t canal.resize(0);\n\t moat.resize(0);\n\t for(int s=up;s<=down;s++){\n\t for(int t=left;t<=right;t++){\n\t\t//cout<<\"s=\"<<s<<\" t=\"<<t<<endl;\n\t\tif((s!=up and s!=down)and(t!=left and t!=right)){\n\t\t //cout<<\"canal.push(\"<<stage[s][t]<<\")\"<<endl;\n\t\t canal.push_back(stage[s][t]);\n\t\t}else{\n\t\t //cout<<\"moat.push(\"<<stage[s][t]<<\")\"<<endl;\n\t\t moat.push_back(stage[s][t]);\n\t\t}\n\t }\n\t }\n\t //print(canal);\n\t //print(moat);\n\t Pond pond;\n\t pond.make(up,down,left,right,canal,moat);\n\t pond.setVals();\n\t //cout<<\"make pond:\"<<endl;\n\t if(pond.isMake()){\n\t //cout<<\"judge=YES\"<<endl;\n\t mx=max(mx,calc(pond));\n\t }else{\n\t //cout<<\"judge=NO\"<<endl;\n\t }\n\t }\n\t}\n }\n }\n //cout<<\"count=\"<<count<<endl;\n cout<<mx<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3140, "score_of_the_acc": -0.2762, "final_rank": 6 } ]
aoj_1624_cpp
Income Inequality We often compute the average as the first step in processing statistical data. Yes, the average is a good tendency measure of data, but it is not always the best. In some cases, the average may hinder the understanding of the data. For example, consider the national income of a country. As the term income inequality suggests, a small number of people earn a good portion of the gross national income in many countries. In such cases, the average income computes much higher than the income of the vast majority. It is not appropriate to regard the average as the income of typical people. Let us observe the above-mentioned phenomenon in some concrete data. Incomes of n people, a 1 , ... , a n , are given. You are asked to write a program that reports the number of people whose incomes are less than or equal to the average ( a 1 + ... + a n ) / n . Input The input consists of multiple datasets, each in the following format. n a 1 a 2 ... a n A dataset consists of two lines. In the first line, the number of people n is given. n is an integer satisfying 2 ≤ n ≤ 10 000. In the second line, incomes of n people are given. a i (1 ≤ i ≤ n ) is the income of the i -th person. This value is an integer greater than or equal to 1 and less than or equal to 100 000. The end of the input is indicated by a line containing a zero. The sum of n 's of all the datasets does not exceed 50 000. Output For each dataset, output the number of people whose incomes are less than or equal to the average. Sample Input 7 15 15 15 15 15 15 15 4 10 20 30 60 10 1 1 1 1 1 1 1 1 1 100 7 90 90 90 90 90 90 10 7 2 7 1 8 2 8 4 0 Output for the Sample Input 7 3 9 1 4
[ { "submission_id": "aoj_1624_9210748", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n vector<int>ans(0);\n while(true){\n double n;\n cin >> n;\n if(n==0){\n break;\n }\n vector<double>A(n);\n for(int i=0;i<n;i++){\n cin >> A[i];\n }\n double sum = accumulate(A.begin(),A.end(),0);\n // cout << sum << endl;\n double ave = sum/n;\n // cout << ave << endl;\n\n int cnt=0;\n for(int i=0;i<n;i++){\n if(A[i]<=ave)cnt++;\n }\n ans.push_back(cnt); \n }\n for(int i=0;i<ans.size();i++){\n cout << ans[i] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3284, "score_of_the_acc": -0.0036, "final_rank": 2 }, { "submission_id": "aoj_1624_6731269", "code_snippet": "#include <bits/stdc++.h> \nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef queue<int> qi;\ntypedef priority_queue<int> pqi;\ntypedef priority_queue<int, vector<int>, greater<int>> mpqi;\ntypedef pair<int, int> pi;\ntypedef vector<ll> vl;\n#define mp make_pair\n#define pb push_back\n#define MAX_N 200005\nll INF = 1e18;\n#define md107 1000000007\n#define rep(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)\ntypedef vector<vector<int>> vvi;\nint n;\nvoid solve(){\n while (true)\n {\n int n; cin >> n;\n if (n == 0) {\n break;\n }\n vi vc(MAX_N, 0);\n ll sum = 0;\n int cnt = 0;\n rep(i, 1, n) {\n cin >> vc[i];\n sum += vc[i];\n }\n float aver = sum / n;\n rep(i, 1, n) {\n if (vc[i] <= aver) cnt++;\n }\n cout << cnt << endl;\n }\n \n}\nint main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n solve();\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4144, "score_of_the_acc": -0.0583, "final_rank": 3 }, { "submission_id": "aoj_1624_6207359", "code_snippet": "#include <iostream> // cout, endl, cin\n#include <string> // string, to_string, stoi\n#include <vector> // vector\n#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound\n#include <utility> // pair, make_pair\n#include <tuple> // tuple, make_tuple\n#include <cstdint> // int64_t, int*_t\n#include <cstdio> // printf\n#include <map> // map\n#include <queue> // queue, priority_queue\n#include <set> // set\n#include <stack> // stack\n#include <deque> // deque\n#include <unordered_map> // unordered_map\n#include <unordered_set> // unordered_set\n#include <bitset> // bitset\n#include <cctype> // isupper, islower, isdigit, toupper, tolower\n#include<math.h>\n#include <iomanip>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\nusing namespace std;\ntypedef long long ll;\n\n\n\n\n//const long long mod=1000000007;\n//const long long mod2=998244353;\nusing ll=long long;\n\nconst ll inf=1e18;\nusing P= pair<ll, ll>;\n\nconst int MAX = 200005;\n// 今回採用する大きい素数\nconst int MOD = 1e9+7;\n\n// メモを保管する場所\nll fact[MAX], inv_fact[MAX], inv[MAX];\n\n// メモを計算する\nvoid init() {\n // 初期値設定と1はじまりインデックスに直す\n fact[0] = 1;\n fact[1] = 1;\n inv[0] = 1;\n inv[1] = 1;\n inv_fact[0] = 1;\n inv_fact[1] = 1;\n // メモの計算\n repi(i, 2, MAX){\n // 階乗\n fact[i] = fact[i - 1] * i % MOD;\n // 逆元\n inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;\n // 逆元の階乗\n inv_fact[i] = inv_fact[i - 1] * inv[i] % MOD;\n }\n}\n\n// 二項係数の実体\nll nCk(int n, int k) {\n ll x = fact[n]; // n!の計算\n ll y = inv_fact[n-k]; // (n-k)!の計算\n ll z = inv_fact[k]; // k!の計算\n if (n < k) return 0; // 例外処理\n if (n < 0 || k < 0) return 0; // 例外処理\n return x * ((y * z) % MOD) % MOD; //二項係数の計算\n}\n\n\nll pow_pow(ll x,ll n,ll mod){\n if(n==0) return 1;\n x%=mod;\n \n ll res=pow_pow(x*x%mod,n/2,mod);\n if(n&1)res=res*x%mod;\n return res;\n}\n\n\n\nint extgcd(int a,int b,int &x,int &y){\n int d=a;\n if(b!=0){\n d=extgcd(b,a%b,y,x);\n y-=(a/b)*x;\n }else{\n x=1;y=0;\n }\n return d;\n}\nint mod_inverse(int a,int m){\n int x,y;\n extgcd(a,m,x,y);\n return (m+x%m)%m;\n}\n\nstruct UnionFind {\n vector<int> par, siz;\n\n UnionFind(int n) : par(n, -1) , siz(n, 1) { }\n\n // 根を求める\n int root(int x) {\n if (par[x] == -1) return x;\n else return par[x] = root(par[x]);\n }\n\n // x と y が同じグループに属するかどうか (根が一致するかどうか)\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n\n // x を含むグループと y を含むグループとを併合する\n bool unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return false; \n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n return true;\n }\n\n // x を含むグループのサイズ\n int size(int x) {\n return siz[root(x)];\n }\n};\n \n //min(x,y)が0以下の場合はmax(x,y)が返される\n//ユークリッドの互除法を元に実装\nll gcd(ll x,ll y){\n if(y==0)return x;\n return gcd(y,x%y);\n}\n\n//オーバフローしないようにかける順番を気を付ける\nll lcm(ll x,ll y){\n return ll(x/gcd(x,y))*y;\n}\n\n\n\n\ntemplate<class T> bool chmin(T& a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n else return false;\n}\ntemplate<class T> bool chmax(T& a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n else return false;\n}\n\n//using Graph1=vector<vector<Edge> >;\n//using Graph=vector<vector<int> >;\n\n// auto mod int\n// https://youtu.be/L8grWxBlIZ4?t=9858\n// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize\n// https://youtu.be/8uowVvQ_-Mo?t=1329 : division\nconst int mod = 1e9+7;\nstruct mint {\n ll x; // typedef long long ll;\n mint(ll x=0):x((x%mod+mod)%mod){}\n mint operator-() const { return mint(-x);}\n mint& operator+=(const mint a) {\n if ((x += a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator-=(const mint a) {\n if ((x += mod-a.x) >= mod) x -= mod;\n return *this;\n }\n mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}\n mint operator+(const mint a) const { return mint(*this) += a;}\n mint operator-(const mint a) const { return mint(*this) -= a;}\n mint operator*(const mint a) const { return mint(*this) *= a;}\n mint pow(ll t) const {\n if (!t) return 1;\n mint a = pow(t>>1);\n a *= a;\n if (t&1) a *= *this;\n return a;\n }\n \n // for prime mod\n mint inv() const { return pow(mod-2);}\n mint& operator/=(const mint a) { return *this *= a.inv();}\n mint operator/(const mint a) const { return mint(*this) /= a;}\n};\nistream& operator>>(istream& is, const mint& a) { return is >> a.x;}\nostream& operator<<(ostream& os, const mint& a) { return os << a.x;}\n// combination mod prime\n// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619\nstruct combination {\n vector<mint> fact, ifact;\n combination(int n):fact(n+1),ifact(n+1) {\n //assert(n < mod);\n fact[0] = 1;\n for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;\n ifact[n] = fact[n].inv();\n for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;\n }\n mint operator()(int n, int k) {\n if (k < 0 || k > n) return 0;\n return fact[n]*ifact[k]*ifact[n-k];\n }\n mint p(int n, int k) {\n return fact[n]*ifact[n-k];\n }\n} c(1000005);\n \n\n\n\n//const ll INF=1e18;\n\n\nclass segment_tree {\nprivate:\n\tint sz;\n\tstd::vector<int> seg;\n\tstd::vector<int> lazy;\n\tvoid push(int k) {\n\t\tif (k < sz) {\n\t\t\tlazy[k * 2] = max(lazy[k * 2], lazy[k]);\n\t\t\tlazy[k * 2 + 1] = max(lazy[k * 2 + 1], lazy[k]);\n\t\t}\n\t\tseg[k] = max(seg[k], lazy[k]);\n\t\tlazy[k] = 0;\n\t}\n\tvoid update(int a, int b, int x, int k, int l, int r) {\n\t\tpush(k);\n\t\tif (r <= a || b <= l) return;\n\t\tif (a <= l && r <= b) {\n\t\t\tlazy[k] = x;\n\t\t\tpush(k);\n\t\t\treturn;\n\t\t}\n\t\tupdate(a, b, x, k * 2, l, (l + r) >> 1);\n\t\tupdate(a, b, x, k * 2 + 1, (l + r) >> 1, r);\n\t\tseg[k] = max(seg[k * 2], seg[k * 2 + 1]);\n\t}\n\tint range_max(int a, int b, int k, int l, int r) {\n\t\tpush(k);\n\t\tif (r <= a || b <= l) return 0;\n\t\tif (a <= l && r <= b) return seg[k];\n\t\tint lc = range_max(a, b, k * 2, l, (l + r) >> 1);\n\t\tint rc = range_max(a, b, k * 2 + 1, (l + r) >> 1, r);\n\t\treturn max(lc, rc);\n\t}\npublic:\n\tsegment_tree() : sz(0), seg(), lazy() {};\n\tsegment_tree(int N) {\n\t\tsz = 1;\n\t\twhile (sz < N) {\n\t\t\tsz *= 2;\n\t\t}\n\t\tseg = std::vector<int>(sz * 2, 0);\n\t\tlazy = std::vector<int>(sz * 2, 0);\n\t}\n\tvoid update(int l, int r, int x) {\n\t\tupdate(l, r, x, 1, 0, sz);\n\t}\n\tint range_max(int l, int r) {\n\t\treturn range_max(l, r, 1, 0, sz);\n\t}\n //cin.tie(0);\n //ios_base::sync_with_stdio(false); これらをmain関数の先頭に\n};\n\nconst ll seg_size=100005;\nll seg[seg_size*2];\n\nvoid seg_set(int ind,ll v){\n ind+=seg_size;\n seg[ind]=max(seg[ind],v);\n while(true){\n if(ind==0)break;\n ind/=2;\n seg[ind]=max(seg[ind*2],seg[ind*2+1]);\n }\n}\n\nll seg_take(int l,int r){\n l+=seg_size;\n r+=seg_size;\n ll ans=0;\n while(l<r){\n if(l%2==1){\n ans=max(ans,seg[l]);\n l++;\n }\n l/=2;\n if(r%2==1){\n ans=max(ans,seg[r-1]);\n r--;\n }\n r/=2;\n\n }\n return ans;\n}\nstruct Edge{\n int to;ll w;\n Edge(int to,ll w):to(to),w(w){}\n};\n\nvoid comp(vector<int>&a){\n set<int>s(a.begin(),a.end());\n map<int,int>d;\n int cnt=0;\n for(auto x:s)d[x]=cnt++;\n for(auto&x:a)x=d[x];\n}\n\nusing graph = vector<vector<ll> > ;\n\nint main(){\n vector<ll> ans;\n while(1){\n int n; cin >> n;\n if(n==0)break;\n vector<ll> a(n);\n ll tot=0;\n rep(i,n){\n cin >> a[i];\n tot+=a[i];\n }\n int cnt=0;\n rep(i,n)if(n*a[i]<=tot)cnt++;\n ans.push_back(cnt);\n }\n for(auto u: ans)cout << u << endl;\n \n}", "accuracy": 1, "time_ms": 10, "memory_kb": 18932, "score_of_the_acc": -1, "final_rank": 4 }, { "submission_id": "aoj_1624_6029003", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cctype>\n#include <climits>\n#include <cmath>\n#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <list>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <utility>\n#include <vector>\nusing namespace std;\ntypedef long double ld;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\ntypedef vector<int> vi;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<long long> vll;\ntypedef vector<double> vd;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define repll(i, n) for (long long int i = 0; i < n; i++)\n#define fin(ans) cout << (ans) << endl\n#define P 1000000007\n#define STI(s) atoi(s.c_str()) // string to int\n#define mp(p, q) make_pair(p, q)\n#define All(a) a.begin(), a.end()\n#define Sort(a) sort(a.begin(), a.end())\n\n\n/////////////////////////////////////////\n\nint main(void) {\n /////////////////////////////////////////////////////\n\n double n;\n while (cin >> n) {\n if (!n) break;\n\n vd a(n);\n double sum = 0;\n rep(i, n) {\n cin >> a[i];\n sum += a[i];\n }\n\n int count = 0;\n rep(i, n) {\n if (a[i] <= sum / n) count++;\n }\n cout << count << endl;\n }\n\n //////////////////////////////////////////////////////\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3228, "score_of_the_acc": 0, "final_rank": 1 } ]
aoj_1616_cpp
Taro's Shopping Mammy decided to give Taro his first shopping experience. Mammy tells him to choose any two items he wants from those listed in the shopping catalogue, but Taro cannot decide which two, as all the items look attractive. Thus he plans to buy the pair of two items with the highest price sum, not exceeding the amount Mammy allows. As getting two of the same item is boring, he wants two different items. You are asked to help Taro select the two items. The price list for all of the items is given. Among pairs of two items in the list, find the pair with the highest price sum not exceeding the allowed amount, and report the sum. Taro is buying two items, not one, nor three, nor more. Note that, two or more items in the list may be priced equally. Input The input consists of multiple datasets, each in the following format. n m a 1 a 2 ... a n A dataset consists of two lines. In the first line, the number of items n and the maximum payment allowed m are given. n is an integer satisfying 2 ≤ n ≤ 1000. m is an integer satisfying 2 ≤ m ≤ 2,000,000. In the second line, prices of n items are given. a i (1 ≤ i ≤ n ) is the price of the i -th item. This value is an integer greater than or equal to 1 and less than or equal to 1,000,000. The end of the input is indicated by a line containing two zeros. The sum of n 's of all the datasets does not exceed 50,000. Output For each dataset, find the pair with the highest price sum not exceeding the allowed amount m and output the sum in a line. If the price sum of every pair of items exceeds m , output NONE instead. Sample Input 3 45 10 20 30 6 10 1 2 5 8 9 11 7 100 11 34 83 47 59 29 70 4 100 80 70 60 50 4 20 10 5 10 16 0 0 Output for the Sample Input 40 10 99 NONE 20
[ { "submission_id": "aoj_1616_10840723", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main(){\n while(true){\n int n, m;\n cin >> n >> m;\n if(!n) break;\n\n vector<int> price(n);\n for(auto& i:price) cin >> i;\n\n int ans = -1;\n\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n if(price[i] + price[j] > m) continue;\n if(price[i] + price[j] > ans) ans = price[i] + price[j];\n }\n }\n \n if(ans == -1) cout << \"NONE\" << endl;\n else cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3368, "score_of_the_acc": -0.0801, "final_rank": 12 }, { "submission_id": "aoj_1616_10600934", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A>\nvoid chmin(A &l, const A &r) {\n if (r < l)\n l = r;\n}\ntemplate <typename A>\nvoid chmax(A &l, const A &r) {\n if (l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nll n, m;\nvector<ll> v;\nvoid input() {\n cin >> n >> m;\n v.resize(n);\n for (auto &x : v) cin >> x;\n}\n\nvoid solve() {\n ll ans = 0;\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n if (v[i] + v[j] <= m) {\n chmax(ans, v[i] + v[j]);\n }\n }\n }\n\n if (ans == 0)\n cout << \"NONE\" << endl;\n else\n cout << ans << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n while (1) {\n input();\n if (n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3408, "score_of_the_acc": -0.0903, "final_rank": 16 }, { "submission_id": "aoj_1616_9614596", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n\nint main()\n{\n\twhile (true)\n\t{\n\t\tint n, m;\n\t\tcin >> n >> m;\n\n\t\tif (n == 0 && m == 0)\n\t\t\tbreak;\n\n\t\tvector<int> a(n);\n\t\trep(i, n) cin >> a[i];\n\n\t\tint ans = 0;\n\t\tfor (int i = 0; i < n - 1; i++)\n\t\t{\n\t\t\tfor (int j = i+1; j < n; j++)\n\t\t\t{\n\t\t\t\tif (a[i] + a[j] <= m)\n\t\t\t\t{\n\t\t\t\t\tans = max(ans, a[i] + a[j]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (ans == 0)\n\t\t\tcout << \"NONE\" << endl;\n\t\telse\n\t\t\tcout << ans << endl;\n\n\t}\n\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3056, "score_of_the_acc": -0.001, "final_rank": 2 }, { "submission_id": "aoj_1616_9494418", "code_snippet": "#include <iostream>\n#include <cmath>\nusing namespace std;\n\nconst int MAX_N = 1000;\nint n, m, ans;\nint a[MAX_N];\n\nint main() {\n while (cin >> n >> m) {\n if (n == 0 && m == 0)\n break;\n ans = 0;\n for (int i = 0; i < n; i++)\n cin >> a[i];\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n if (a[i] + a[j] <= m)\n ans = max(ans, a[i] + a[j]);\n }\n }\n if (ans)\n cout << ans << endl;\n else\n cout << \"NONE\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3100, "score_of_the_acc": -0.0122, "final_rank": 6 }, { "submission_id": "aoj_1616_9419021", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n using ll=long long;\n using vl=vector<ll>;\n using vll=vector<vl>;\n int n=1,m;\n while(n!=0){\n cin>>n>>m;\n if(n==0 && m==0){\n break;\n }\n vl s(n);\n for(int i=0;i<n;i++){\n cin>>s.at(i);\n }\n int r=0;\n for(int i=0;i<n-1;i++){\n for(int j=i+1;j<n;j++){\n if(s.at(i)+s.at(j)<=m){\n if(s.at(i)+s.at(j)>r){\n r=s.at(i)+s.at(j);\n }\n }\n }\n }\n if(r==0){\n cout << \"NONE\" <<endl;\n }\n else{\n cout<<r<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3312, "score_of_the_acc": -0.0659, "final_rank": 10 }, { "submission_id": "aoj_1616_9416414", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,N) for(i=0;i<N;i++)\n#define ll long long\n\nint main(void){\n vector<ll>ans;\n ll i,j,k;\n\n for(;;){\n ll N,M;\n ll A[1009];\n cin>>N>>M;\n if(N==0&&M==0)break;\n rep(i,N)cin>>A[i];\n ll a=-1;\n rep(i,N){\n for(j=i+1;j<N;j++){\n ll sum=A[i]+A[j];\n if(M<sum)continue;\n a=max(a,sum);\n }\n }\n ans.push_back(a);\n }\n\n rep(i,ans.size()){\n if(ans[i]==-1){\n cout<<\"NONE\"<<endl;\n continue;\n }\n cout<<ans[i]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3164, "score_of_the_acc": -0.0284, "final_rank": 8 }, { "submission_id": "aoj_1616_9411118", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing ull = unsigned long long;\nconst int INF = 2001001001;\nconst ll LINF = 9001001001001001001;\n\n#define rep(i, n) for (int i = 0; (i) < (n); (i)++)\n#define rep1(i, n) for (int i = 1; (i) < (n + 1); (i)++)\n#define all(a) (a).begin(), (a).end()\n\nint main(){\n while(true){\n int n, m;\n cin >> n >> m;\n\n if(!n && !m) break; \n\n int a[n];\n rep(i,n) cin >> a[i];\n\n int ans = 0;\n rep(i,n){\n for(int j = i+1; j < n; j++){\n if(a[i] + a[j] <= m) ans = max(ans, a[i] + a[j]);\n }\n }\n if(ans) cout << ans << endl;\n else cout << \"NONE\" << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3436, "score_of_the_acc": -0.0974, "final_rank": 17 }, { "submission_id": "aoj_1616_9394615", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\nint main() {\n\n int n,m,l;\n bool end = false;\n int q = 0;\n vector<string> result(1000);\n while(end == false){\n l = 0;\n cin >> n >> m;\n if(n==0 && m== 0){\n /*rep(i,q){\n cout << result[i] << endl;\n }*/\n end = true;\n }else{\n vector<int> a(n);\n for(ll i = 0; i < n ;i++){\n //cout<< i << endl;\n cin >> a.at(i);\n //cout<< i << endl;\n }\n //cout<< \"終わり\" << endl;\n for (ll i = 0; i < n; i++)\n {\n //cout<< i << endl;\n for(int j = 0; j < n; j++)\n {\n if(l < a[i] + a[j] ){\n if(a[i] + a[j] <= m){\n if(i!=j){\n l = a[i] + a[j];\n }\n\n\n }\n }\n\n }\n\n }\n if(l != 0 && l <= m){\n //result[q]=to_string(l);\n cout << l << endl;\n }\n if(l == 0){\n // result[q]=\"NONE\";\n cout << \"NONE\" << endl;\n }\n q++;\n \n\n }\n\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3376, "score_of_the_acc": -0.0822, "final_rank": 14 }, { "submission_id": "aoj_1616_9394605", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\nint main() {\n\n int n,m,l;\n bool end = false;\n int q = 0;\n vector<string> result(1000);\n while(end == false){\n l = 0;\n cin >> n >> m;\n if(n==0 && m== 0){\n rep(i,q){\n cout << result[i] << endl;\n }\n end = true;\n }else{\n vector<int> a(n);\n for(ll i = 0; i < n ;i++){\n //cout<< i << endl;\n cin >> a.at(i);\n //cout<< i << endl;\n }\n //cout<< \"終わり\" << endl;\n for (ll i = 0; i < n; i++)\n {\n //cout<< i << endl;\n for(int j = 0; j < n; j++)\n {\n if(l < a[i] + a[j] ){\n if(a[i] + a[j] <= m){\n if(i!=j){\n l = a[i] + a[j];\n }\n\n\n }\n }\n\n }\n\n }\n if(l != 0 && l <= m){\n result[q]=to_string(l);\n\n }\n if(l == 0){\n result[q]=\"NONE\";\n }\n q++;\n \n\n }\n\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3236, "score_of_the_acc": -0.0467, "final_rank": 9 }, { "submission_id": "aoj_1616_9381277", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n,m;\n cin>>n>>m;\n while(n||m){\n vector<int> a(n);\n for(int i=0;i<n;i++)cin>>a[i];\n int ans=0;\n for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++){\n if(a[i]+a[j]<=m) ans=max(ans,a[i]+a[j]);\n }\n if(ans==0) cout<<\"NONE\"<<endl;\n else cout<<ans<<endl;\n cin>>n>>m;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3052, "score_of_the_acc": 0, "final_rank": 1 }, { "submission_id": "aoj_1616_9377498", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)\n\nint main() {\n\twhile (1)\n\t{\n\t\tint\tn, m;\n\t\tcin >> n >> m;\n\t\tif (!n && !m)\n\t\t\tbreak ;\n\t\tvector<int> a(n);\n\t\trep(i, 0, n) cin >> a[i];\n\n\t\tint\tmax = 0;\n\t\trep(i, 0, n - 1)\n\t\t{\n\t\t\trep(j, i + 1, n)\n\t\t\t{\n\t\t\t\tif (a[i] + a[j] <= m && a[i] + a[j] > max)\n\t\t\t\t\tmax = a[i] + a[j];\n\t\t\t}\n\t\t}\n\t\tif (!max)\n\t\t\tcout << \"NONE\" << endl;\n\t\telse\n\t\t\tcout << max << endl;\n\t}\n\treturn (0);\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3352, "score_of_the_acc": -0.0761, "final_rank": 11 }, { "submission_id": "aoj_1616_9377403", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 1; i <= n; i++)\n#define rep2(i,n) for (int i = 0; i < n; i++)\n\nint main(){\n vector<int> vec;\nwhile(true){\n int n, m;\n if(n == 0 && m == 0){\n break;\n }\n cin >> n >> m;\n int a[n];\n rep(i,n) cin >> a[i];\n int ans = -1;\n rep(i,n){\n rep(j,n){\n if(i != j && a[i] + a[j] <= m){\n ans = max(a[i] + a[j] , ans);\n }\n }\n } \n vec.push_back(ans);\n}\n int k = vec.size();\n rep2(i,k-1){\n if(vec[i] == -1) cout << \"NONE\" << endl;\n else cout << vec[i] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3060, "score_of_the_acc": -0.002, "final_rank": 3 }, { "submission_id": "aoj_1616_9377306", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rep2(i, s, n) for (int i = (int)s; i < (int)(n); i++)\nusing namespace std;\nusing ll = long long;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }\n\nint main() {\n while (true) {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n \n vector<int> a(n);\n rep(i, n) {\n cin >> a[i];\n }\n\n int ans = 0;\n rep(i, n) {\n rep(j, n) {\n if (i == j) continue;\n int sum = a[i] + a[j];\n if (sum <= m) chmax(ans, sum);\n }\n }\n\n if (ans == 0) {\n cout << \"NONE\" << endl;\n } else {\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3448, "score_of_the_acc": -0.1004, "final_rank": 18 }, { "submission_id": "aoj_1616_9377280", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(int i=0; i<(int)(n); i++)\n#define ll long long\n#define Graph vector<vector<int>>\n#define pii pair<int, int>\n\nint main(){\n\n while(true){\n int n, m;\n cin >> n >> m;\n if(n == 0 && m == 0) break;\n vector<int> vec(n);\n rep(i, n){\n cin >> vec[i];\n }\n int ans=0;\n rep(i, n){\n rep(j, n){\n if(i == j) continue;\n if(vec[i]+vec[j] <= m){\n ans = max(ans, vec[i]+vec[j]);\n }\n }\n }\n if(ans == 0){\n cout << \"NONE\" << endl;\n }else{\n cout << ans << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3096, "score_of_the_acc": -0.0112, "final_rank": 5 }, { "submission_id": "aoj_1616_9371103", "code_snippet": "#include <bits/stdc++.h>\n//#include \"atcoder/all\"\nusing namespace std;\n//using namespace atcoder;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\n#define all(x) (x).begin(), (x).end()\n#define rep1(a) for(ll i = 0; i < a; i++)\n#define rep2(i, a) for(ll i = 0; i < a; i++)\n#define rep3(i, a, b) for(ll i = a; i < b; i++)\n#define rep4(i, a, b, c) for(ll i = a; i < b; i += c)\n#define overload4(a, b, c, d, e, ...) e\n#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n#define rrep(i, a, b) for (ll i = a; i >= b; i--)\n#define fore(i, a) for (auto &i : a)\n#define vvl(a,b,c) vector<vector<ll>>(a,vector<ll>(b,c))\n#define vvvl(a,b,c,d) vector<vector<vector<ll>>>(a,vvl(b,c,d))\n#define enld endl\n#define fix(n) cout << fixed << setprecision(n);\n#define yesno(b) cout << (b ? \"Yes\":\"No\") << endl\n#define debug(var) do{std::cout << #var << \" : \";view(var);}while(0)\ntemplate<typename T> void view(T e){std::cout << e << std::endl;}\ntemplate<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << \" \"; } std::cout << std::endl;}\ntemplate<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }\ntemplate<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;\nvoid fast_io(){cin.tie(nullptr);ios_base::sync_with_stdio(false);}\nconst int inf = INT_MAX / 2;\nconst ll infl = 1LL<<60;\nconst ll mod = 1000000007;\nconst ll mod2 = 998244353;\nconst int dx[8] = {0, 1, 0,-1, 1, 1,-1,-1};\nconst int dy[8] = {1, 0,-1, 0, 1,-1, 1,-1};\n\nint main() {\n fast_io();\n for(;;) {\n int N,M; cin >> N >> M;\n if(N==0) break;\n vector<ll>A(N); rep(N) cin >> A[i];\n ll ans = 0;\n rep(i,N) rep(j,N) if(i!=j && A[i]+A[j]<=M) chmax(ans,A[i]+A[j]);\n if(ans==0) cout << \"NONE\" << enld;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3476, "score_of_the_acc": -0.1075, "final_rank": 19 }, { "submission_id": "aoj_1616_9367152", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n while(true){\n int n,m;\n cin >> n >> m;\n if(n==0&&m==0)break;\n vector<int>a(n);\n for(int i=0;i<n;i++){\n cin >> a[i];\n }\n\n vector<int>sum(0);\n for(int i=0;i<n-1;i++){\n for(int j=i+1;j<n;j++){\n sum.push_back(a[i]+a[j]);\n }\n }\n sort(sum.begin(),sum.end());\n\n // for(int i=0;i<sum.size();i++){\n // cout << sum[i] << \" \";\n // }\n // cout << endl;\n auto it = upper_bound(sum.begin(),sum.end(),m);\n // cout << it-sum.begin() << endl;\n if(it-sum.begin()==0){\n cout << \"NONE\" << endl;\n }else{\n cout << sum[it-sum.begin()-1] << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 6996, "score_of_the_acc": -2, "final_rank": 20 }, { "submission_id": "aoj_1616_9347900", "code_snippet": "#include <bits/extc++.h>\nusing namespace std;\n\nbool solve();\n\nint main() {\n while(solve());\n return 0;\n}\n\nbool solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) return false;\n vector<int> a(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n\n int ans = -1;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (i == j) continue;\n if (a[i] + a[j] <= m) ans = max(ans, a[i] + a[j]);\n }\n }\n\n if (ans == -1) puts(\"NONE\");\n else cout << ans << endl;\n return true;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3092, "score_of_the_acc": -0.0101, "final_rank": 4 }, { "submission_id": "aoj_1616_9238122", "code_snippet": "#include<iostream>\nusing namespace std;\n\nint main(){\n int n,m,sum,x;\n int ans;\n while(true){\n cin >> n >> m;\n ans=0;\n if(n==0 && m==0){\n break;\n }\n int array[n];\n for(int i=0;i<n;i++) cin >> array[i];\n for(int i=0;i<n;i++){\n for(int j=0;j<n; j++){\n sum=array[i] + array[j];\n if(\n sum<=m && ans<sum && i!=j)ans=sum;\n }\n }\n if(ans!=0){\n cout << ans << endl;\n }else{\n cout << \"NONE\" << endl;\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3120, "score_of_the_acc": -0.0172, "final_rank": 7 }, { "submission_id": "aoj_1616_9237712", "code_snippet": "#include <iostream>\n#include <algorithm>\n\nusing namespace std;\nint main() {\n while (true){\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) break;\n int a[n];\n for (int i = 0; i < n; i++) cin >> a[i];\n int ans = -1;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n int current = a[i] + a[j];\n if (i == j) continue;\n if (ans < current && current <= m) {\n ans = current;\n }\n }\n }\n if (ans == -1) cout << \"NONE\" << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3368, "score_of_the_acc": -0.0801, "final_rank": 12 }, { "submission_id": "aoj_1616_9221818", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\nint ctoi(char c){return c-'0';}\nll ctoll(char c){return c-'0';}\n\nvoid solve(int n,int m){\n vector<int>a(n);\n int ans = -1;\n rep(i,n)cin >> a[i];\n rep(i,n)rep(j,n){\n if(i==j)continue;\n if(ans < a[i]+a[j]){\n if(a[i]+a[j]<=m)ans = a[i]+a[j];\n }\n }\n if(ans == -1)cout << \"NONE\" << endl;\n else cout << ans << endl;\n}\n\nint main(){\n while(true){\n int n,m;\n cin >> n >> m;\n if(n==0&&m==0)break;\n solve(n,m);\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3380, "score_of_the_acc": -0.0832, "final_rank": 15 } ]
aoj_1622_cpp
Go around the Labyrinth Explorer Taro got a floor plan of a labyrinth. The floor of this labyrinth is in the form of a two-dimensional grid. Each of the cells on the floor plan corresponds to a room and is indicated whether it can be entered or not. The labyrinth has only one entrance located at the northwest corner, which is upper left on the floor plan. There is a treasure chest in each of the rooms at other three corners, southwest, southeast, and northeast. To get a treasure chest, Taro must move onto the room where the treasure chest is placed. Taro starts from the entrance room and repeats moving to one of the enterable adjacent rooms in the four directions, north, south, east, or west. He wants to collect all the three treasure chests and come back to the entrance room. A bad news for Taro is that, the labyrinth is quite dilapidated and even for its enterable rooms except for the entrance room, floors are so fragile that, once passed over, it will collapse and the room becomes not enterable. Determine whether it is possible to collect all the treasure chests and return to the entrance. Input The input consists of at most 100 datasets, each in the following format. N M c 1,1 ... c 1, M ... c N ,1 ... c N , M The first line contains N , which is the number of rooms in the north-south direction, and M , which is the number of rooms in the east-west direction. N and M are integers satisfying 2 ≤ N ≤ 50 and 2 ≤ M ≤ 50. Each of the following N lines contains a string of length M . The j -th character c i,j of the i -th string represents the state of the room ( i,j ), i -th from the northernmost and j -th from the westernmost; the character is the period (' . ') if the room is enterable and the number sign (' # ') if the room is not enterable. The entrance is located at (1,1), and the treasure chests are placed at ( N, 1), ( N,M ) and (1, M ). All of these four rooms are enterable. Taro cannot go outside the given N × M rooms. The end of the input is indicated by a line containing two zeros. Output For each dataset, output YES if it is possible to collect all the treasure chests and return to the entrance room, and otherwise, output NO in a single line. Sample Input 3 3 ... .#. ... 5 5 ..#.. ..... #.... ..... ..... 3 8 ..#..... ........ .....#.. 3 5 ..#.. ..... ..#.. 4 4 .... .... ..## ..#. 0 0 Output for the Sample Input YES NO YES NO NO
[ { "submission_id": "aoj_1622_10703013", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\n\ntemplate<typename flow>\nstruct max_flow {\n struct edge {\n int to;\n flow cap;\n int rev;\n };\n int V;\n vector<vector<edge> > G;\n vector<int> itr, level;\n max_flow(int V) : V(V) {G.assign(V, vector<edge>()); }\n\n void add_edge(int from, int to, flow cap) {\n G[from].push_back((edge){to, cap, (int)G[to].size()});\n G[to].push_back((edge) {from, 0, (int)G[from].size()-1});\n }\n\n void bfs(int s) {\n level.assign(V, -1);\n queue<int> q;\n level[s] = 0;\n q.push(s);\n while(!q.empty()) {\n int v = q.front();\n q.pop();\n for(auto &e : G[v]) {\n if(e.cap > 0 && level[e.to] < 0) {\n level[e.to] = level[v] + 1;\n q.push(e.to);\n }\n }\n }\n }\n\n flow dfs(int v, int t, flow f) {\n if(v == t) return f;\n for(int& i = itr[v]; i < (int)G[v].size(); ++i) {\n edge &e = G[v][i];\n if(e.cap > 0 && level[v] < level[e.to]) {\n flow d = dfs(e.to, t, min(f, e.cap));\n if(d > 0) {\n e.cap -= d;\n G[e.to][e.rev].cap += d;\n return d;\n }\n }\n }\n return 0;\n }\n\n flow run(int s, int t) {\n flow ret = 0, f;\n while(bfs(s), level[t] >= 0) {\n itr.assign(V, 0);\n while((f = dfs(s, t, INF)) > 0) ret += f;\n }\n return ret;\n }\n};\n\nint H, W;\nstring field[55];\nint dh[4] = {1, -1, 0, 0};\nint dw[4] = {0, 0, 1, -1};\nmax_flow<int> init() {\n max_flow<int> graph(10001);\n graph.add_edge(1*W+1, 1*W+1+5000, 1);\n graph.add_edge(H*W+1, H*W+1+5000, 1);\n graph.add_edge(H*W+W-1, H*W+W-1+5000, 1);\n graph.add_edge(1*W+W-1, 1*W+W-1+5000, 1);\n for(int h = 1; h <= H; h++) {\n for(int w = 1; w <= W; w++) {\n if(field[h][w] == '#') continue;\n graph.add_edge(h*W + w, h*W + w + 5000, 1);\n for(int k = 0; k <= 3; k++) {\n int newh = h + dh[k];\n int neww = w + dw[k];\n if(newh <= 0 || newh > H || neww <= 0 || neww > W) continue;\n if(field[newh][neww] == '#') continue;\n graph.add_edge(h*W+w + 5000, newh*W+neww, 1);\n }\n }\n }\n return graph;\n}\n\nint TARGET = 10000;\n\nint main() {\n while(true) {\n cin >> H >> W;\n if(H == 0 && W == 0) break;\n for(int i = 1; i <= H; i++) {\n cin >> field[i];\n field[i] = \"&\" + field[i];\n }\n bool clear = true;\n max_flow<int> graph = init();\n graph.add_edge(0, 1*W+1, 1);\n graph.add_edge(0, H*W+1, 1);\n graph.add_edge(1*W+W-1 + 5000, TARGET, 1);\n graph.add_edge(H*W+W-1 + 5000, TARGET, 1);\n if(graph.run(0, TARGET) < 2) clear = false;\n graph = init();\n graph.add_edge(0, 1*W+1, 1);\n graph.add_edge(0, 1*W+W-1, 1);\n graph.add_edge(H*W+1 + 5000, TARGET, 1);\n graph.add_edge(H*W+W-1 + 5000, TARGET, 1);\n if(graph.run(0, TARGET) < 2) clear = false;\n graph = init();\n graph.add_edge(0, 1*W+1, 2);\n graph.add_edge(0, H*W+W-1, 2);\n graph.add_edge(1*W+W-1 + 5000, TARGET, 2);\n graph.add_edge(H*W+1 + 5000, TARGET, 2);\n if(graph.run(0, TARGET) < 4) clear = false;\n if(clear) cout << \"YES\" << endl;\n else cout << \"NO\" << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4748, "score_of_the_acc": -0.3387, "final_rank": 7 }, { "submission_id": "aoj_1622_10636878", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\n#include <cassert>\n#include <vector>\n\nnamespace atcoder {\n\nstruct dsu {\n public:\n dsu() : _n(0) {}\n explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}\n\n int merge(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n int x = leader(a), y = leader(b);\n if (x == y) return x;\n if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);\n parent_or_size[x] += parent_or_size[y];\n parent_or_size[y] = x;\n return x;\n }\n\n bool same(int a, int b) {\n assert(0 <= a && a < _n);\n assert(0 <= b && b < _n);\n return leader(a) == leader(b);\n }\n\n int leader(int a) {\n assert(0 <= a && a < _n);\n return _leader(a);\n }\n\n int size(int a) {\n assert(0 <= a && a < _n);\n return -parent_or_size[leader(a)];\n }\n\n std::vector<std::vector<int>> groups() {\n std::vector<int> leader_buf(_n), group_size(_n);\n for (int i = 0; i < _n; i++) {\n leader_buf[i] = leader(i);\n group_size[leader_buf[i]]++;\n }\n std::vector<std::vector<int>> result(_n);\n for (int i = 0; i < _n; i++) {\n result[i].reserve(group_size[i]);\n }\n for (int i = 0; i < _n; i++) {\n result[leader_buf[i]].push_back(i);\n }\n result.erase(\n std::remove_if(result.begin(), result.end(),\n [&](const std::vector<int>& v) { return v.empty(); }),\n result.end());\n return result;\n }\n\n private:\n int _n;\n std::vector<int> parent_or_size;\n\n int _leader(int a) {\n if (parent_or_size[a] < 0) return a;\n return parent_or_size[a] = _leader(parent_or_size[a]);\n }\n};\n\n} // namespace atcoder\n\nusing namespace std;\nusing namespace atcoder; \ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 60;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\nstruct Lowlink{\n ll n;\n vector<ll> ord;\n vector<ll>low;\n vector<ll>articulation,bridge;//atriculation 関節点,bridgeは橋になる辺の番号を格納 \n vector<vector<pll>> g;\n vector<ll>found;\n vector<ll> c;//二重辺連結分解で使用\n ll m;//辺の数\n \n Lowlink(ll _n):n(_n),m(0){\n ord.resize(n,INF);\n low.resize(n);\n g.resize(n);\n found.resize(n);\n }\n \n ll add_edge(ll u, ll v){\n g[u].push_back({v,m});\n g[v].push_back({u,m});\n return m++;\n }\n \n ll dfs(ll v,ll par, ll num){\n ord[v] = num;\n low[v] = num;\n found[v] =1;\n bool isa = false; //関節点かどうか\n ll cnt = 0;//vが根のときに関節点かどうか判定に使用\n for(auto &p:g[v]){\n auto[to,id] = p;\n if(to == par){continue;}\n if(found[to] != 0){\n //発見済みだったから今見ている辺は後退辺\n chmin(low[v],ord[to]);\n }else{\n //未発見\n cnt++;\n num = dfs(to,v,num+1);\n if(par != -1 && low[to] >= ord[v]){\n isa = true;\n }\n chmin(low[v],low[to]);\n if(ord[v] < low[to]){\n //子のlowが子で終わってたらbridge\n bridge.push_back(id);\n }\n }\n }\n if(par == -1 && cnt >= 2)isa = true;\n if(isa)articulation.push_back(v);//関節点だった\n return num + 1;\n }\n //構築して連結成分数を返す \n ll build(){\n ll k = 0;\n ll cnt = 0;//シンプルに連結成分数\n rep(i,n){\n if(found[i] == 0){\n k = dfs(i,-1,k);\n cnt++;\n }\n }\n return cnt; //シンプルに連結成分数を返しておく\n }\n \n vector<vector<ll>> bc;\n vector<ll> bc_id;\n //二重頂点連結分解\n void bcc(){\n bc_id = vector<ll>(m,-1);\n vector<ll> used(n);\n auto add = [&](ll id,ll num){\n bc[num].push_back(id);\n bc_id[id] = num;\n };\n auto dfs = [&](auto dfs,ll v,ll par,ll num)->void{\n used[v] = 1;\n for(auto &[to,id]:g[v]){\n if(to == par){continue;}\n if(used[to] == 0 ){\n ll nownum = num;\n if(low[to] >= ord[v]){\n nownum = bc.size();\n bc.push_back({});//新しくグループ作る\n }\n add(id,nownum);\n dfs(dfs,to,v,nownum);\n }else if(ord[to] < ord[v]){\n add(id,num);//後退辺は同じグループ\n }\n }\n };\n rep(i,n){\n if(used[i] == 0){\n dfs(dfs,i,-1,-1);\n }\n }\n }\n\n \n\n //二重辺連結分解 build()した後\n //cに各頂点がどのグループに属するか格納される\n //グラフから橋を全て削除したときの連結成分\n ll two_edge_connect_component(){\n //~~~~~~二重辺連結分解~~~~~~~~~~~\n // 計算量 O(n+m)\n c.resize(n,-1);\n ll ccnt = 0;\n rep(i,n){\n if(c[i] == -1){\n auto dfs2 = [&](auto dfs2, ll v,ll par,ll num)->ll{\n if(par != -1 && ord[par] >= low[v]){\n c[v] = c[par];\n }else{\n c[v] = num++;\n }\n for(auto &[to,id]:g[v]){\n if(c[to] == -1){\n num = dfs2(dfs2,to,v,num);\n }\n }\n return num;\n };\n ccnt = dfs2(dfs2,i,-1,ccnt);\n }\n }\n return ccnt;\n }\n};\n\n\nvoid solve(ll n,ll m){\n vector<string > s(n);\n rep(i,n){\n cin >> s[i];\n }\n // vvll g(n *m);\n auto clc = [&](ll i,ll j){\n return i * m + j;\n };\n\n set<pll> kado ={{0,0},{n-1,m-1},{n-1,0},{0,m-1}};\n \n rep(I,n)rep(J,m)if(!kado.contains({I,J})){\n dsu uf(n*m);\n rep(i,n){\n rep(j,m)if(s[i][j] != '#' && (i != I or j != J)){\n rep(k,4){\n ll ny = i + _ta[k];\n ll nx = j + _yo[k];\n if(isin(ny,nx,n,m)&& s[ny][nx] != '#'&& (ny !=I or nx != J)){\n uf.merge(clc(i,j),clc(ny,nx));\n }\n }\n }\n }\n // bool ok = true;\n if(!uf.same(clc(0,0),clc(0,m-1))){\n cout <<\"NO\" << endl;\n return ;\n }\n if(!uf.same(clc(0,0),clc(n-1,m-1))){\n cout << \"NO\" << endl;\n return ;\n }\n if(!uf.same(clc(0,0),clc(n-1,0))){\n cout << \"NO\" << endl;\n return ;\n }\n }\n \n cout << \"YES\" << endl;\n\n}\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n,m);\n if(n == 0 && m == 0)break;\n solve(n,m);\n }\n}", "accuracy": 1, "time_ms": 520, "memory_kb": 3712, "score_of_the_acc": -0.6887, "final_rank": 14 }, { "submission_id": "aoj_1622_10630779", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#include <atcoder/all>\nusing namespace atcoder;\n\nusing ll = long long;\nusing mint = modint998244353;\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if(N == 0) return false;\n vector<string> S(N);\n for(int i = 0; i < N; i++) {\n cin >> S[i];\n }\n if(S[0][1] == '#' || S[1][0] == '#') {\n cout << \"NO\" << endl;\n return true;\n }\n if(S[0][M - 2] == '#' || S[1][M - 1] == '#') {\n cout << \"NO\" << endl;\n return true;\n }\n if(S[N - 1][1] == '#' || S[N - 2][0] == '#') {\n cout << \"NO\" << endl;\n return true;\n }\n if(S[N - 1][M - 2] == '#' || S[N - 2][M - 1] == '#') {\n cout << \"NO\" << endl;\n return true;\n }\n if(N <= 3 && M <= 3) {\n cout << \"YES\" << endl;\n return true;\n }\n auto ID = [&](int i, int j) -> int {\n return i * M + j;\n };\n // cout << \"non-trivial\" << endl;\n vector<pair<int, int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n mf_graph<int> g(2 * N * M + 2);\n for(int i = 0; i < N; i++) {\n for(int j = 0; j < M; j++) {\n if(S[i][j] == '#') continue;\n int x = ID(i, j);\n g.add_edge(2 * x, 2 * x + 1, 1);\n for(auto [di, dj] : dirs) {\n int ii = i + di, jj = j + dj;\n if(!(0 <= ii && ii < N && 0 <= jj && jj < M && S[ii][jj] == '.')) continue;\n // cout << i << \" \" << j << \" \" << ii << \" \" << jj << endl;\n int y = ID(ii, jj);\n g.add_edge(2 * x + 1, 2 * y, 1);\n g.add_edge(2 * y + 1, 2 * x, 1);\n }\n }\n }\n mf_graph<int> ga = g;\n mf_graph<int> gb = g;\n mf_graph<int> gc = g;\n const int SRC = 2 * N * M;\n const int DST = 2 * N * M + 1;\n {\n ga.add_edge(SRC, 2 * ID(0, 1), 1);\n ga.add_edge(SRC, 2 * ID(1, 0), 1);\n ga.add_edge(2 * ID(N - 1, M - 2) + 1, DST, 1);\n ga.add_edge(2 * ID(N - 2, M - 1) + 1, DST, 1);\n int f = ga.flow(SRC, DST);\n // vector ma(N, vector<int>(M));\n // for(auto e : ga.edges()) {\n // int p = e.from;\n // int q = e.to;\n // if(p != SRC && q != DST && p % 2 == 0 && q % 2 == 1 && p + 1 == q) {\n // int i = (p / 2) / M;\n // int j = (p / 2) % M;\n // ma[i][j] = e.flow;\n // }\n // }\n // for(int i = 0; i < N; i++) {\n // for(int j = 0; j < M; j++) {\n // cout << ma[i][j] << \" \\n\"[j == M - 1];\n // }\n // }\n if(f < 2) {\n // cout << \"type1\" << \" \" << f << endl;\n cout << \"NO\" << endl;\n return true;\n }\n }\n {\n gb.add_edge(SRC, 2 * ID(N - 2, 0), 1);\n gb.add_edge(SRC, 2 * ID(N - 1, 1), 1);\n gb.add_edge(2 * ID(1, 0) + 1, DST, 1);\n gb.add_edge(2 * ID(N - 1, M - 2) + 1, DST, 1);\n int f = gb.flow(SRC, DST);\n if(f < 2) {\n // cout << \"type2\" << \" \" << f << endl;\n cout << \"NO\" << endl;\n return true;\n }\n }\n {\n gc.add_edge(SRC, 2 * ID(0, M - 2), 1);\n gc.add_edge(SRC, 2 * ID(1, M - 1), 1);\n gc.add_edge(2 * ID(0, 1) + 1, DST, 1);\n gc.add_edge(2 * ID(N - 2, M - 1) + 1, DST, 1);\n int f = gc.flow(SRC, DST);\n if(f < 2) {\n // cout << \"type3\" << \" \" << f << endl;\n cout << \"NO\" << endl;\n return true;\n }\n }\n cout << \"YES\" << endl;\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 8148, "score_of_the_acc": -1.012, "final_rank": 18 }, { "submission_id": "aoj_1622_9933475", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\nad hoc => WA\nmax flow WA:\nvertex cannot be transversed more than once\nedge <-> vertex OR multiple layers of grid (2 layer !)\n\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 2147483647;\n\nconst int MAX_N = 50;\nstatic bool G[MAX_N][MAX_N];\n\ntypedef pair<int,int> PA;\ntypedef vector<PA> PAS;\nstatic PAS action = {{-1,0}, {0,1}, {1,0}, {0,-1}};\n\ntypedef vector<int> States;\n//------------------------------------------------------------------------------\nconst int MAX_NN = 2*MAX_N*MAX_N + 2;\nstatic int level[MAX_NN];\nstatic int start[MAX_NN];\n\n//------------------------------------------------------------------------------\nstruct Edge\n{\n int w, f, c, j;\n\n bool operator<(const Edge& other) const\n {\n return f < other.f;\n }\n\n Edge(): w(-1), f(0), c(0), j(-1) {}\n Edge(int v2, int flow, int capacity, int reverse_edge): w(v2), f(flow), c(capacity), j(reverse_edge) {}\n void debug() const { printf(\"(%d, %d, %d, %d)\\n\", w, f, c, j); }\n};\n\ntypedef vector<Edge> Edges;\ntypedef vector<Edges> GRAPH;\n\n//------------------------------------------------------------------------------\nvoid add_edge(int N, GRAPH& G, int v, int w, int c)\n{\n // forward edge:\n Edge e1(w, 0, c, G[w].size());\n\n // backward edge:\n Edge e2(v, 0, 0, G[v].size());\n\n G[v].push_back( e1 );\n G[w].push_back( e2 );\n}\n\n//------------------------------------------------------------------------------\nbool bfs(int NN, GRAPH& GG, int src, int dest)\n{\n //--------------------------------------------------------------------------\n // setup params:\n for (int v=0; v<NN; ++v) level[v] = -1;\n\n queue<int> Q;\n Q.push(src);\n level[src] = 0;\n\n //--------------------------------------------------------------------------\n // search:\n while (!Q.empty())\n {\n int v = Q.front(); Q.pop();\n for (Edge e: GG[v])\n {\n if (level[e.w] < 0 && e.f < e.c)\n {\n level[e.w] = level[v] + 1;\n Q.push(e.w);\n }\n }\n }\n\n //--------------------------------------------------------------------------\n // is dest reachable from src?\n return level[dest] >= 0;\n}\n\nint send_flow(int NN, GRAPH& GG, int v, int dest, int flow)\n{\n if (v == dest) return flow;\n for (; start[v]<GG[v].size(); start[v]++)\n { \n Edge& e = GG[v][start[v]];\n if (level[e.w] == level[v] + 1 && e.f < e.c)\n {\n int min_flow = min(flow, e.c-e.f);\n int new_flow = send_flow(NN, GG, e.w, dest, min_flow);\n \n if (new_flow)\n { \n e.f += new_flow;\n GG[e.w][e.j].f -= new_flow;\n return new_flow; \n }\n }\n }\n return 0;\n}\n\n\n//------------------------------------------------------------------------------\nint dinic(int NN, GRAPH& GG, int src, int dest)\n{\n //if (DEBUG) {for (int v=0; v<NN; ++v) for (Edge e: GG[v]) { printf(\"%d -> \", v); e.debug(); }}\n\n //--------------------------------------------------------------------------\n // base cases:\n if (dest == src) return 0;\n\n //--------------------------------------------------------------------------\n // init params:\n int res = 0;\n\n //--------------------------------------------------------------------------\n // compute:\n while (bfs(NN, GG, src, dest))\n {\n for (int v=0; v<NN; ++v) start[v] = 0;\n while (int flow = send_flow(NN, GG, src, dest, INF)) res += flow;\n }\n\n //--------------------------------------------------------------------------\n // report:\n return res;\n}\n\n//------------------------------------------------------------------------------\nvoid debug_grid(int M, int N)\n{\n printf(\"grid:\\n\");\n for (int m=0; m<M; ++m) { for (int n=0; n<N; ++n) printf(\"%d \",G[m][n]); printf(\"\\n\"); }\n printf(\"\\n\");\n}\n\n\nStates neighbors(int M, int N, int v)\n{\n States SS;\n int m1 = v/N;\n int n1 = v%N;\n if (!G[m1][n1]) return SS;\n\n int m2, n2, w;\n //--------------------------------------------------------------------------\n for (PA p: action)\n {\n m2 = m1 + p.first;\n n2 = n1 + p.second;\n if (m2 < 0 || m2 >= M || n2 < 0 || n2 >= N || !G[m2][n2]) continue;\n w = m2*N + n2;\n SS.push_back( w );\n }\n\n //--------------------------------------------------------------------------\n return SS;\n}\n\n//------------------------------------------------------------------------------\nbool ok(int M, int N)\n{\n for (int m=0; m<M; ++m)\n for (int n=0; n<N; ++n)\n if (!G[m][n])\n return false;\n return true;\n}\n\nbool feasible(int M, int N)\n{\n int v;\n v = 0;\n if (neighbors(M, N, v).size() < 2) return false;\n\n v = N-1;\n if (neighbors(M, N, v).size() < 2) return false;\n\n v = M*N-1;\n if (neighbors(M, N, v).size() < 2) return false;\n\n v = M*N-N;\n if (neighbors(M, N, v).size() < 2) return false;\n\n return true;\n}\n\n\nGRAPH setup(int M, int N)\n{\n int NN = 2*M*N+2;\n int src = NN-2;\n\n GRAPH GG(NN);\n add_edge(NN, GG, src, 0, 2);\n\n for (int v=0; v<M*N; ++v)\n {\n if (!G[v/N][v%N]) continue;\n\n add_edge(NN, GG, M*N+v, v, 1); //<----------\n\n for (int w: neighbors(M, N, v))\n add_edge(NN, GG, v, w+M*N, 1);\n }\n\n return GG;\n}\n\n//------------------------------------------------------------------------------\nbool solve(int M, int N)\n{\n if (DEBUG) debug_grid(M, N);\n //--------------------------------------------------------------------------\n // base cases:\n if (M == 2 || N == 2) return ok(M, N);\n if (!feasible(M, N)) return false;\n //--------------------------------------------------------------------------\n // init:\n //--------------------------------------------------------------------------\n // compute:\n int NN = 2*M*N+2;\n int src = NN-2;\n\n vector<int> dests = {N-1 + M*N, M*N-1 + M*N, M*N-N + M*N};\n for (int dest: dests)\n {\n GRAPH GG = setup(M, N);\n int flow = dinic(NN, GG, src, dest);\n if (flow != 2) return false;\n }\n //--------------------------------------------------------------------------\n // report:\n return true;\n}\n\n//------------------------------------------------------------------------------\nvoid test()\n{\n\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //test(); return 0;\n //DEBUG = true;\n //--------------------------------------------------------------------------\n int M, N, num;\n char c;\n while (true)\n {\n num = scanf(\"%d %d \", &M, &N);\n if (M == 0 && N == 0) break;\n for (int m=0; m<M; ++m)\n for (int n=0; n<N; ++n)\n { num = scanf(\"%c \", &c); if (c == '.') G[m][n] = true; else G[m][n] = false; }\n if (solve(M, N)) printf(\"YES\\n\");\n else printf(\"NO\\n\");\n }\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 30, "memory_kb": 3784, "score_of_the_acc": -0.1134, "final_rank": 1 }, { "submission_id": "aoj_1622_9409072", "code_snippet": "#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <type_traits>\n#include <typeindex>\n#include <unordered_map>\n#include <unordered_set>\n\n#endif\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T& a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T& a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\nconstexpr int INF = 1001001001;\nconstexpr int64_t llINF = 3000000000000000000;\n\n#include <algorithm>\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T>\nstruct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap>\nstruct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\nvoid solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) exit(0);\n vector<string> s(n);\n rep(i, n) cin >> s[i];\n atcoder::mf_graph<int> mf(n * m * 2 + 2);\n vector<vector<int>> id(n, vector<int>(m));\n {\n int t = 0;\n rep(i, n) rep(j, m) id[i][j] = t++;\n }\n rep(i, n) {\n rep(j, m) {\n if (s[i][j] == '#') continue;\n mf.add_edge(id[i][j], id[i][j] + n * m, 1);\n\n if (i + 1 < n && s[i + 1][j] == '.') mf.add_edge(id[i][j] + n * m, id[i + 1][j], 1);\n if (j + 1 < m && s[i][j + 1] == '.') mf.add_edge(id[i][j] + n * m, id[i][j + 1], 1);\n if (i && s[i - 1][j] == '.') mf.add_edge(id[i][j] + n * m, id[i - 1][j], 1);\n if (j && s[i][j - 1] == '.') mf.add_edge(id[i][j] + n * m, id[i][j - 1], 1);\n }\n }\n auto mf2 = mf;\n cout << (mf.flow(n * m, id[n - 1][m - 1], 2) == 2 && mf2.flow(id[n - 1][0] + n * m, id[0][m - 1], 2) == 2 ? \"YES\" : \"NO\") << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n /*int t;\n cin >> t;\n while (t--) */\n while (1) solve();\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4704, "score_of_the_acc": -0.2813, "final_rank": 6 }, { "submission_id": "aoj_1622_9381828", "code_snippet": "#include<iostream>\n#include<vector>\n\n#include <algorithm>\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n\nnamespace atcoder {\n\ntemplate <class Cap> struct mf_graph {\n public:\n mf_graph() : _n(0) {}\n explicit mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d =\n self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) return res;\n }\n level[v] = _n;\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\nusing namespace std;\nconst vector<pair<int,int>>D={{1,0},{-1,0},{0,1},{0,-1}};\nbool solve()\n{\n int H,W;\n cin>>H>>W;\n if(H==0&&W==0)return false;\n vector<string>S(H);\n for(int i=0;i<H;i++)cin>>S[i];\n const int N=H*W;\n atcoder::mf_graph<long long>G(2*N);\n G.add_edge(0,N,1);\n for(int r=0;r<H;r++)for(int c=0;c<W;c++)G.add_edge(r*W+c,r*W+c+N,1);\n for(int r=0;r<H;r++)for(int c=0;c<W;c++)if(S[r][c]=='.')\n {\n for(auto[dr,dc]:D)\n {\n int tr=r+dr,tc=c+dc;\n if(0<=tr&&tr<H&&0<=tc&&tc<W&&S[tr][tc]=='.')G.add_edge(r*W+c+N,tr*W+tc,1);\n }\n }\n auto GG=G,GGG=G;\n cout<<(G.flow(0,W-1)==2&&GG.flow(0,N-W)==2&&GGG.flow(0,N-1)==2?\"YES\":\"NO\")<<'\\n';\n return true;\n}\nint main(){while(solve()){}}", "accuracy": 1, "time_ms": 20, "memory_kb": 5584, "score_of_the_acc": -0.477, "final_rank": 8 }, { "submission_id": "aoj_1622_9351399", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct dsu {\n vector<int> p, sz;\n dsu(int n) : p(n), sz(n) {\n iota(p.begin(), p.end(), 0);\n }\n int find(int x) {\n return (p[x] == x ? x : p[x] = find(p[x]));\n }\n bool same(int x, int y) {\n return find(x) == find(y);\n }\n void merge(int x, int y) {\n x = find(x), y = find(y);\n if (sz[x] < sz[y]) swap(x, y);\n p[y] = x;\n sz[x] += sz[y];\n }\n};\n\nbool solve() {\n int N, M; cin >> N >> M;\n if (!N) return false;\n vector S(N, string());\n for (auto& s : S) cin >> s;\n vector<vector<bool>> G(N, vector(M, false));\n for (int i = 0 ; i < N ; i++) {\n for (int j = 0 ; j < M ; j++) {\n G[i][j] = S[i][j] == '.';\n }\n }\n auto valid = [&](int y, int x) -> bool {\n return 0 <= y and y < N and 0 <= x and x < M and G[y][x];\n };\n auto f = [&](int y, int x) -> int {\n return y * M + x;\n };\n array<int, 4> dy = { 1, 0, -1, 0 };\n array<int, 4> dx = { 0, 1, 0, -1 };\n for (int i = 0 ; i < N ; i++) {\n for (int j = 0 ; j < M ; j++) {\n if (not G[i][j]) continue;\n if (i == 0 and j == 0) continue;\n if (i == N - 1 and j == 0) continue;\n if (i == 0 and j == M - 1) continue;\n if (i == N - 1 and j == M - 1) continue;\n dsu uf(N * M);\n for (int y = 0 ; y < N ; y++) {\n for (int x = 0 ; x < M ; x++) {\n if (y == i and x == j) continue;\n if (not G[y][x]) continue;\n for (int d = 0 ; d < 4 ; d++) {\n int ny = y + dy[d], nx = x + dx[d];\n if (not valid(ny, nx)) continue;\n if (ny == i and nx == j) continue;\n uf.merge(f(y, x), f(ny, nx));\n }\n }\n }\n bool ok = true;\n ok &= uf.same(f(0, 0), f(N - 1, 0));\n ok &= uf.same(f(N - 1, 0), f(N - 1, M - 1));\n ok &= uf.same(f(N - 1, M - 1), f(0, M - 1));\n if (not ok) {\n // cout << i << ' ' << j << endl;\n cout << \"NO\" << endl;\n return true;\n }\n }\n }\n dsu uf(N * M);\n for (int y = 0 ; y < N ; y++) {\n for (int x = 0 ; x < M ; x++) {\n if (not G[y][x]) continue;\n for (int d = 0 ; d < 4 ; d++) {\n int ny = y + dy[d], nx = x + dx[d];\n if (not valid(ny, nx)) continue;\n uf.merge(f(y, x), f(ny, nx));\n }\n }\n }\n bool ok = true;\n ok &= uf.same(f(0, 0), f(N - 1, 0));\n ok &= uf.same(f(N - 1, 0), f(N - 1, M - 1));\n ok &= uf.same(f(N - 1, M - 1), f(0, M - 1));\n if (not ok) {\n // cout << i << ' ' << j << endl;\n cout << \"NO\" << endl;\n return true;\n }\n cout << \"YES\" << endl;\n return true;\n}\n\nint main() {\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 780, "memory_kb": 3516, "score_of_the_acc": -0.9611, "final_rank": 17 }, { "submission_id": "aoj_1622_9293575", "code_snippet": "#include <bits/stdc++.h>\n\n#include <algorithm>\n\n#include <vector>\n\nnamespace atcoder {\n\nnamespace internal {\n\ntemplate <class T> struct simple_queue {\n std::vector<T> payload;\n int pos = 0;\n void reserve(int n) { payload.reserve(n); }\n int size() const { return int(payload.size()) - pos; }\n bool empty() const { return pos == int(payload.size()); }\n void push(const T& t) { payload.push_back(t); }\n T& front() { return payload[pos]; }\n void clear() {\n payload.clear();\n pos = 0;\n }\n void pop() { pos++; }\n};\n\n} // namespace internal\n\n} // namespace atcoder\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\nnamespace atcoder {\n\ntemplate <class Cap> struct mf_graph {\n public:\n mf_graph() : _n(0) {}\n mf_graph(int n) : _n(n), g(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < _n);\n assert(0 <= to && to < _n);\n assert(0 <= cap);\n int m = int(pos.size());\n pos.push_back({from, int(g[from].size())});\n int from_id = int(g[from].size());\n int to_id = int(g[to].size());\n if (from == to) to_id++;\n g[from].push_back(_edge{to, to_id, cap});\n g[to].push_back(_edge{from, from_id, 0});\n return m;\n }\n\n struct edge {\n int from, to;\n Cap cap, flow;\n };\n\n edge get_edge(int i) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n auto _e = g[pos[i].first][pos[i].second];\n auto _re = g[_e.to][_e.rev];\n return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};\n }\n std::vector<edge> edges() {\n int m = int(pos.size());\n std::vector<edge> result;\n for (int i = 0; i < m; i++) {\n result.push_back(get_edge(i));\n }\n return result;\n }\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n int m = int(pos.size());\n assert(0 <= i && i < m);\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& _e = g[pos[i].first][pos[i].second];\n auto& _re = g[_e.to][_e.rev];\n _e.cap = new_cap - new_flow;\n _re.cap = new_flow;\n }\n\n Cap flow(int s, int t) {\n return flow(s, t, std::numeric_limits<Cap>::max());\n }\n Cap flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < _n);\n assert(0 <= t && t < _n);\n assert(s != t);\n\n std::vector<int> level(_n), iter(_n);\n internal::simple_queue<int> que;\n\n auto bfs = [&]() {\n std::fill(level.begin(), level.end(), -1);\n level[s] = 0;\n que.clear();\n que.push(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (auto e : g[v]) {\n if (e.cap == 0 || level[e.to] >= 0) continue;\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.push(e.to);\n }\n }\n };\n auto dfs = [&](auto self, int v, Cap up) {\n if (v == s) return up;\n Cap res = 0;\n int level_v = level[v];\n for (int& i = iter[v]; i < int(g[v].size()); i++) {\n _edge& e = g[v][i];\n if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;\n Cap d =\n self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));\n if (d <= 0) continue;\n g[v][i].cap += d;\n g[e.to][e.rev].cap -= d;\n res += d;\n if (res == up) break;\n }\n return res;\n };\n\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs();\n if (level[t] == -1) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(dfs, t, flow_limit - flow);\n if (!f) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n std::vector<bool> visited(_n);\n internal::simple_queue<int> que;\n que.push(s);\n while (!que.empty()) {\n int p = que.front();\n que.pop();\n visited[p] = true;\n for (auto e : g[p]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.push(e.to);\n }\n }\n }\n return visited;\n }\n\n private:\n int _n;\n struct _edge {\n int to, rev;\n Cap cap;\n };\n std::vector<std::pair<int, int>> pos;\n std::vector<std::vector<_edge>> g;\n};\n\n} // namespace atcoder\n\nusing namespace atcoder;\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\n\nvoid solve(ll H,ll W) {\n vector<string> S(H);\n rep(h,H)cin>>S[h];\n mf_graph<ll> G(H*W*2);\n vll dx={1,-1,0,0};\n vll dy={0,0,1,-1};\n rep(h,H)rep(w,W){\n if(S[h][w]=='#')continue;\n G.add_edge(h*W+w,h*W+w+H*W,1);\n rep(d,4){\n ll nh=h+dy[d];\n ll nw=w+dx[d];\n if(nh<0||nw<0||nh>=H||nw>=W)continue;\n if(S[nh][nw]=='#')continue;\n G.add_edge(h*W+w+H*W,nh*W+nw,1);\n }\n }\n G.add_edge(0,H*W,1);\n auto G2=G,G3=G;\n ll an=min({G.flow(0,(H-1)*W),G2.flow(0,W-1),G3.flow(0,(H-1)*W+W-1)});\n cout<<(an==2?\"YES\":\"NO\")<<endl;\n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll H,W;\n while (cin >> H>>W) {\n if (H == 0)return 0;\n solve(H,W);\n }\n\n\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5812, "score_of_the_acc": -0.5246, "final_rank": 10 }, { "submission_id": "aoj_1622_9126959", "code_snippet": "#include <bits/stdc++.h>\n\nstruct Edge {\n int from;\n int to;\n int cap;\n int rev;\n Edge(int from, int to, int cap, int rev): from(from), to(to), cap(cap), rev(rev) {}\n};\n\nstruct Flow {\n std::vector<std::vector<Edge>> graph;\n Flow(int n): graph(n) {}\n void add_edge(int from, int to, int cap) {\n int id = graph[from].size();\n int rev = graph[to].size();\n graph[from].emplace_back(from, to, cap, rev);\n graph[to].emplace_back(to, from, 0, id);\n }\n\n std::vector<bool> used;\n int dfs(int u, const int sink, int cur_flow) {\n if (u == sink) return cur_flow;\n used[u] = true;\n for (auto& e: graph[u]) {\n if (e.cap == 0) continue;\n if (used[e.to]) continue;\n int res = dfs(e.to, sink, std::min(cur_flow, e.cap));\n if (res != 0) {\n e.cap -= res;\n graph[e.to][e.rev].cap += res;\n return res;\n }\n }\n return 0;\n }\n\n int max_flow(int source, int sink) {\n int ans = 0;\n while (true) {\n used.assign(graph.size(), false);\n int res = dfs(source, sink, 1e9);\n if (res == 0) {\n break;\n }\n ans += res;\n }\n return ans;\n }\n};\n\nint solve() {\n int n, m;\n std::cin >> n >> m;\n if (n == 0) return 1;\n\n std::vector<std::string> board(n);\n for (int i = 0; i < n; i++) std::cin >> board[i];\n\n int offset = 0;\n std::vector in(n, std::vector<int>(m));\n std::vector mid(n, std::vector<int>(m));\n std::vector out(n, std::vector<int>(m));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n in[i][j] = offset++;\n mid[i][j] = offset++;\n out[i][j] = offset++;\n }\n }\n const int source = out[0][0];\n Flow flow(offset);\n\n // in -> out\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n flow.add_edge(in[i][j], out[i][j], 1);\n }\n }\n\n const int DI[] = {0, 1, 0, -1};\n const int DJ[] = {1, 0, -1, 0};\n // out -> in\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (board[i][j] == '#') continue;\n for (int d = 0; d < 4; d++) {\n int ni = i + DI[d];\n int nj = j + DJ[d];\n if (ni < 0 || nj < 0 || ni >= n || nj >= m) continue;\n if (board[ni][nj] == '#') continue;\n flow.add_edge(out[i][j], in[ni][nj], 1);\n }\n }\n }\n\n Flow f1 = flow;\n Flow f2 = flow;\n Flow f3 = flow;\n int ff1 = f1.max_flow(source, in[0][m - 1]);\n int ff2 = f2.max_flow(source, in[n - 1][0]);\n int ff3 = f3.max_flow(source, in[n - 1][m - 1]);\n // std::cerr << ff1 << ' ' << ff2 << ' ' << ff3 << std::endl;\n if (ff1 == ff2 && ff2 == ff3 && ff3 == 2) {\n std::cout << \"YES\" << '\\n';\n } else {\n std::cout << \"NO\" << '\\n';\n }\n\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 6368, "score_of_the_acc": -0.6526, "final_rank": 11 }, { "submission_id": "aoj_1622_7785140", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool connected(int n, int m, vector<vector<char>> &c){\n\tvector<vector<bool>> dp(n, vector<bool>(m));\n\tdp[0][0] = true;\n\tvector<pair<int,int>> mada = {pair(0, 0)};\n\tvector<pair<int,int>> go = {pair(-1, 0), pair(1, 0), pair(0, 1), pair(0, -1)};\n\twhile (!mada.empty()){\n\t\tint i = mada.back().first;\n\t\tint j = mada.back().second;\n\t\tmada.pop_back();\n\t\tfor (auto [xx, yy]: go){\n\t\t\tint x = i + xx;\n\t\t\tint y = j + yy;\n\t\t\tif (0 <= x && x < n && 0 <= y && y < m){\n\t\t\t\tif (c[x][y] == '.' && !dp[x][y]){\n\t\t\t\t\tmada.push_back(pair(x, y));\n\t\t\t\t\tdp[x][y] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!dp[n-1][m-1]) return false;\n\tif (!dp[0][m-1]) return false;\n\tif (!dp[n-1][0]) return false;\n\treturn true;\n}\n\nint main(){\n\twhile(true){\n\t\tint n, m; cin >> n >> m;\n\t\tif (n == 0) break;\n\n\t\tvector<vector<char>> c(n, vector<char>(m));\n\t\tfor (int i=0; i<n; i++){\n\t\t\tfor (int j=0; j<m; j++){\n\t\t\t\tcin >> c[i][j];\n\t\t\t}\n\t\t}\n\n\t\tif (!connected(n, m, c)){\n\t\t\tcout << \"NO\" << endl;\n\t\t\tcontinue;\n\t\t}\n\n\t\tbool mode = true;\n\t\tfor (int i=0; i<n; i++){\n\t\t\tfor (int j=0; j<m; j++){\n\t\t\t\tif (i == 0 && j == 0) continue;\n\t\t\t\tif (i == n-1 && j == 0) continue;\n\t\t\t\tif (i == 0 && j == m-1) continue;\n\t\t\t\tif (i == n-1 && j == m-1) continue;\n\t\t\t\tif (c[i][j] == '.'){\n\t\t\t\t\tc[i][j] = '#';\n\t\t\t\t\tif (!connected(n, m, c)) mode = false;\n\t\t\t\t\tc[i][j] = '.';\n\t\t\t\t}\n\t\t\t\tif (!mode) break;\n\t\t\t}\n\t\t\tif (!mode) break;\n\t\t}\n\n\n\t\tif (mode) cout << \"YES\" << endl;\n\t\telse cout << \"NO\" << endl;\n\t}\n}", "accuracy": 1, "time_ms": 530, "memory_kb": 3504, "score_of_the_acc": -0.6574, "final_rank": 12 }, { "submission_id": "aoj_1622_7111607", "code_snippet": "#include <bits/stdc++.h>\n\nint ri() {\n\tint n;\n\tscanf(\"%d\", &n);\n\treturn n;\n}\nconst std::vector<std::pair<int, int> > dd = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\nbool solve() {\n\tint n = ri();\n\tint m = ri();\n\t\n\tif (!n && !m) return false;\n\t\n\tauto in = [&] (int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; };\n\t\n\tstd::string a[n];\n\tfor (auto &i : a) std::cin >> i;\n\t\n\tstd::vector<std::vector<bool> > used(n, std::vector<bool>(m));\n\t\n\tauto bfs = [&] (int sx, int sy) {\n\t\tstd::vector<std::vector<bool> > reachable(n, std::vector<bool>(m));\n\t\tstd::queue<std::pair<int, int> > que;\n\t\treachable[sx][sy] = true;\n\t\tque.push({sx, sy});\n\t\twhile (que.size()) {\n\t\t\tauto i = que.front();\n\t\t\tque.pop();\n\t\t\t\n\t\t\tauto go = [&] (int nx, int ny) {\n\t\t\t\tif (in(nx, ny) && a[nx][ny] == '.' && !used[nx][ny] && !reachable[nx][ny]) {\n\t\t\t\t\treachable[nx][ny] = true;\n\t\t\t\t\tque.push({nx, ny});\n\t\t\t\t}\n\t\t\t};\n\t\t\tfor (auto d : dd) go(i.first + d.first, i.second + d.second);\n\t\t}\n\t\treturn reachable;\n\t};\n\tint x = 1;\n\tint y = 0;\n\tint last_dx = 1;\n\tint last_dy = 0;\n\t\n\tstd::vector<std::pair<int, int> > route;\n\tauto go = [&] (int gx, int gy) {\n\t\twhile (x != gx || y != gy) {\n\t\t\tused[x][y] = true;\n\t\t\t\n\t\t\tauto reachable = bfs(gx, gy);\n\t\t\t\n\t\t\tint dx = last_dy;\n\t\t\tint dy = -last_dx;\n\t\t\tbool ok = false;\n\t\t\tfor (int i = 0; i < 4; i++) {\n\t\t\t\tif (in(x + dx, y + dy) && reachable[x + dx][y + dy]) { ok = true; break; }\n\t\t\t\tstd::swap(dx, dy);\n\t\t\t\tdx *= -1;\n\t\t\t}\n\t\t\tif (!ok) return false;\n\t\t\tx += dx;\n\t\t\ty += dy;\n\t\t\t// std::cerr << x << \" \" << y << std::endl;\n\t\t\tlast_dx = dx;\n\t\t\tlast_dy = dy;\n\t\t\troute.push_back({x, y});\n\t\t}\n\t\treturn true;\n\t};\n\tbool ok = a[1][0] == '.';\n\tok = ok && go(n - 1, 0);\n\tok = ok && go(n - 1, m - 1);\n\tok = ok && go(0, m - 1);\n\tok = ok && go(0, 0);\n\tputs(ok ? \"YES\" : \"NO\");\n\t\n\treturn true;\n}\n\nint main() {\n\twhile (solve());\n\treturn 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3448, "score_of_the_acc": -0.1156, "final_rank": 2 }, { "submission_id": "aoj_1622_6710847", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing vl = vector<ll>;\ntemplate<class T> using vc = vector<T>;\ntemplate<class T> using vvc = vector<vector<T>>;\n\n#define eb emplace_back\n#define all(x) (x).begin(), (x).end()\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)\n#define repe(i, l, r) for (ll i = (l); i < (r); i++)\n#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)\n#define repa(i,n) for (auto& i: n)\n\ntemplate<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}\ntemplate<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}\n\nstruct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;\n\ntemplate <typename T, typename U> ostream& operator<<(ostream&out,const pair<T, U>&a){return out<<a.first<<' '<<a.second;}\ntemplate <typename T> ostream& operator<<(ostream&out, const vector<T>&a){for(auto it=a.begin();it!= a.end();){out<<*it;if(++it!=a.end())out<<' ';}return out;}\ntemplate <typename T, size_t N> ostream& operator<<(ostream&out, const array<T, N>&a){for(auto it=a.begin();it!= a.end();){out<<*it;if(++it!=a.end())out<<' ';}return out;}\ntemplate <typename T> ostream& operator<<(ostream&out, const set<T>&a){for(auto it=a.begin();it!=a.end();){out<<*it;if(++it!=a.end())out<<' ';}return out;}\ntemplate <typename T, typename U> ostream& operator<<(ostream&out, const map<T, U>&a){for(auto it=a.begin();it!=a.end();){out<<*it;if(++it!=a.end())out<<'\\n';}return out;}\n\n#ifdef DEBUG\ntemplate <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << \" \"; cerr << endl; }\ntemplate <class T, class N, size_t AN> void verr(const array<T, AN>& a, const N& n) { rep(i, n) cerr << a[i] << \" \"; cerr << endl; }\nll dbgt = 1; void err() { cerr << \"passed \" << dbgt++ << endl; }\ntemplate<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?\" \":\"\\n\") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }\n#else\nvoid err(){}\ntemplate <class H, class... T>\nvoid err(H &&h, T &&...t) {}\ntemplate <class H, class... T>\nvoid verr(H &&h, T &&...t) {}\n#endif\n\nconst ll INF = 4e18;\nconst ld EPS = 1e-11;\nconst ld PI = acos(-1.0L);\n// const ll MOD = 1e9 + 7;\nconst ll MOD = 998244353;\n//--------------------------------------------------------------------------------//\narray<int, 4> di = {0, 1, 0, -1}, dj = {1, 0, -1, 0};\n\nstruct UnionFind {\nprivate:\n vector<int> par;\n vector<int> count;\n vector<int> rank;\npublic:\n UnionFind(int N) {\n count.assign(N, 1);\n rank.assign(N, 0);\n par.assign(N, 0);\n rep(i, N) par[i] = i;\n }\n\n int root(int x) {\n if (par[x] == x)\n return x;\n else {\n par[x] = root(par[x]);\n return par[x];\n }\n }\n\n void unite(int x, int y) {\n x = root(x), y = root(y);\n if (x == y) return;\n if (rank[x] < rank[y])\n swap(x, y);\n else if (rank[x] == rank[y])\n rank[y]++;\n par[y] = x;\n count[x] += count[y];\n return;\n }\n\n int size(int x) {\n return count[root(x)];\n }\n\n bool issame(int x, int y) {\n return root(x) == root(y);\n }\n};\nusing UF = struct UnionFind;\n\n\nvoid solve(int H, int W){\n vc<string> C(H);\n rep(i, H) cin >> C[i];\n vvc<int> used(H, vc<int>(W));\n rep(i, H) rep(j, W) {\n if (C[i][j] == '#') used[i][j] = true;\n }\n\n queue<pair<int, int>> q;\n auto padding1 = [&]() -> void {\n queue<pair<int, int>> tmpq;\n rep(i, H) rep(j, W) tmpq.emplace(i, j);\n while (!tmpq.empty()) {\n auto [i, j] = tmpq.front();\n tmpq.pop();\n int can = 0, li, lj;\n rep(d, 4) {\n int ti = i + di[d], tj = j + dj[d];\n if (ti < 0 or ti >= H or tj < 0 or tj >= W) continue;\n if (used[ti][tj]) continue;\n can++;\n li = ti, lj = tj;\n }\n if (can == 0) used[i][j] = true;\n if (can == 1) {\n used[i][j] = true;\n tmpq.emplace(li, lj);\n }\n }\n };\n\n padding1();\n\n auto cango = [&](int i, int j) -> bool {\n if (i < 0 or i >= H or j < 0 or j >= W) return false;\n if (used[i][j]) return false;\n return true;\n };\n rep(i, H) rep(j, W) {\n int can = 0;\n if (used[i][j]) continue;\n rep(d, 4) {\n int ti = i + di[d], tj = j + dj[d];\n if (!cango(ti, tj)) continue;\n can++;\n }\n if (can == 2) q.emplace(i, j);\n }\n rep(i, H) rep(j, W) {\n int can = 0;\n if (used[i][j]) continue;\n rep(d, 4) {\n int ti = i + di[d], tj = j + dj[d];\n if (!cango(ti, tj)) continue;\n can++;\n }\n if (can == 3) q.emplace(i, j);\n }\n rep(i, H) rep(j, W) {\n int can = 0;\n if (used[i][j]) continue;\n rep(d, 4) {\n int ti = i + di[d], tj = j + dj[d];\n if (!cango(ti, tj)) continue;\n can++;\n }\n if (can == 4) q.emplace(i, j);\n }\n while(!q.empty()) {\n auto [ci, cj] = q.front();\n q.pop();\n if (used[ci][cj]) continue;\n UF tuf(H * W);\n rep(i, H) rep(j, W) {\n if (i == ci and j == cj) continue;\n if (used[i][j]) continue;\n rep(d, 4) {\n int ti = i + di[d], tj = j + dj[d];\n if (ti == ci and tj == cj) continue;\n if (!cango(ti, tj)) continue;\n tuf.unite(i * W + j, ti * W + tj);\n }\n }\n bool closed = false;\n vc<pair<int, int>> ads;\n rep(d, 4) {\n int ti = ci + di[d], tj = cj + dj[d];\n if (!cango(ti, tj)) continue;\n ads.eb(ti, tj);\n }\n if (ads.size() <= 1) continue;\n set<int> st;\n for (auto [i, j] : ads) {\n st.emplace(tuf.root(i * W + j));\n }\n if (st.size() != 2) continue;\n int cli, clj;\n for (auto [i, j] : ads) {\n int id = i * W + j;\n if (tuf.issame(id, 0) or tuf.issame(id, H * W - 1) or tuf.issame(id, (H - 1) * W) or tuf.issame(id, W - 1))\n continue;\n closed = true;\n cli = i, clj = j;\n }\n if (closed) {\n // err(\"closed\", ci, cj);\n // used[ci][cj] = true;\n rep(i, H) rep(j, W) {\n if (tuf.issame(cli * W + clj, i * W + j)) used[i][j] = true;\n }\n padding1();\n // rep(i, H) {\n // cerr << setfill('0') << right << setw(2) << i << setfill(' ') << \" \";\n // cerr << C[i] << \" \";\n // rep(j, W) cerr << (used[i][j] ? '#' : '.');\n // cerr << endl;\n // }\n }\n }\n\n padding1();\n\n UF uf(H * W);\n rep(i, H) rep(j, W) {\n if (used[i][j]) continue;\n rep(d, 2) {\n int ti = i + di[d], tj = j + dj[d];\n if (ti >= H or tj >= W or used[ti][tj]) continue;\n uf.unite(i * W + j, ti * W + tj);\n }\n }\n\n if (used[0][0] or used[0][W - 1] or used[H - 1][0] or used[H - 1][W - 1]) {\n cout << \"NO\" << endl;\n return;\n }\n\n if(!(uf.issame(0, W - 1) and uf.issame(0, (H - 1) * W) and uf.issame(0, H * W - 1))) {\n cout << \"NO\" << endl;\n return;\n }\n\n // rep(i, H) {\n // cerr << setfill('0') << right << setw(2) << i << setfill(' ') << \" \";\n // cerr << C[i] << \" \";\n // rep(j, W) cerr << (used[i][j] ? '#' : '.');\n // cerr << endl;\n // }\n\n vvc<int> arrived(H, vc<int>(W));\n int ci = 0, cj = 0, cd = 0;\n do {\n // err(ci, cj);\n int ld = (cd + 3) % 4;\n int li = ci + di[ld], lj = cj + dj[ld];\n if(cango(li, lj)) {\n ci = li, cj = lj, cd = ld;\n arrived[ci][cj] = true, used[ci][cj] = true;\n continue;\n }\n int fd = cd;\n int fi = ci + di[fd], fj = cj + dj[fd];\n if(cango(fi, fj)) {\n ci = fi, cj = fj, cd = fd;\n arrived[ci][cj] = true, used[ci][cj] = true;\n continue;\n }\n\n int rd = (cd + 1) % 4;\n int ri = ci + di[rd], rj = cj + dj[rd];\n if(cango(ri, rj)) {\n ci = ri, cj = rj, cd = rd;\n arrived[ci][cj] = true, used[ci][cj] = true;\n continue;\n }\n\n cout << \"NO\" << endl;\n return;\n } while (!(ci == 0 and cj == 0));\n\n if(arrived[0][0] and arrived[0][W - 1] and arrived[H - 1][0] and arrived[H - 1][W - 1]) {\n cout << \"YES\" << endl;\n }else{\n cout << \"NO\" << endl;\n }\n}\n\nint main() {\n while(true){\n int H, W;\n cin >> H >> W;\n if (H == 0) break;\n solve(H, W);\n }\n}", "accuracy": 1, "time_ms": 760, "memory_kb": 3512, "score_of_the_acc": -0.9362, "final_rank": 16 }, { "submission_id": "aoj_1622_6027761", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\n//const int mod = 1e9 + 7;\nconst int mod = 998244353;\n\nstruct Dinic{\n\tstruct edge{\n\t\tll to, cap, rev;\n\t};\n\tvector<vector<edge>> G;\n\tvector<int> level, itr;\n\tll n;\n\tconst ll INF = 1<<30;\n \n\tDinic(ll n) : n(n){\n\t\tG.resize(n);\n\t\tlevel.resize(n);\n\t\titr.resize(n);\n\t}\n \n\tvoid add_edge(ll from, ll to, ll cap){\n\t\tG[from].push_back({to,cap,(ll)G[to].size()});\n\t\tG[to].push_back({from,0,(ll)G[from].size()-1});\n\t}\n \n\tvoid bfs(int s){\n\t\tfill(level.begin(), level.end(), -1);\n\t\tqueue<int> q;\n\t\tlevel[s] = 0;\n\t\tq.push(s);\n\t\twhile(!q.empty()){\n\t\t\tint u = q.front(); q.pop();\n\t\t\tfor(auto &e : G[u]){\n\t\t\t\tif(e.cap > 0 && level[e.to] < 0){\n\t\t\t\t\tlevel[e.to] = level[u] + 1;\n\t\t\t\t\tq.push(e.to);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tll dfs(int u, int t, ll f){\n\t\tif(u == t) return f;\n\t\tfor(int &i = itr[u]; i < G[u].size(); i++){\n\t\t\tedge &e = G[u][i];\n\t\t\tif(e.cap > 0 && level[u] < level[e.to]){\n\t\t\t\tll d = dfs(e.to, t, min(f, e.cap));\n\t\t\t\tif(d > 0){\n\t\t\t\t\te.cap -= d;\n\t\t\t\t\tG[e.to][e.rev].cap += d;\n\t\t\t\t\treturn d;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n \n\tll max_flow(ll s, ll t){\n\t\tll flow = 0;\n\t\twhile(1){\n\t\t\tbfs(s);\n\t\t\tif(level[t] < 0) return flow;\n\t\t\tfill(itr.begin(), itr.end(), 0);\n\t\t\tll f;\n\t\t\twhile((f = dfs(s, t, INF)) > 0){\n\t\t\t\tflow += f;\n\t\t\t}\n\t\t}\n\t}\n};\n\nbool in(ll y, ll x, ll h, ll w){\n\treturn 0 <= y && y < h && 0 <= x && x < w;\n}\n\nint main(){\n\twhile(1){\n\t\tint n,m; cin >> n >> m;\n\t\tif(!n) break; \n\t\tvs s(n); rep(i,n) cin >> s[i];\n\t\tDinic a(n*m*2),b(n*m*2);\n\t\tint sa = 0, sb = (n-1)*m, ta = n*m*2-1, tb = m-1+n*m;\n\t\trep(i,n) rep(j,m){\n\t\t\tif(s[i][j] == '#') continue;\n\t\t\trep(k,4){\n\t\t\t\tint ny = i + dy[k];\n\t\t\t\tint nx = j + dx[k];\n\t\t\t\tif(!in(ny,nx,n,m) || s[ny][nx] == '#') continue;\n\t\t\t\ta.add_edge(i*m+j+n*m,ny*m+nx,1);\n\t\t\t\tb.add_edge(i*m+j+n*m,ny*m+nx,1);\n\t\t\t}\n\t\t\tint u = i*m+j, v = i*m+j+n*m;\n\t\t\tif(i==0&&j==0||i==0&&j==m-1||i==n-1&&j==0||i==n-1&&j==m-1){\n\t\t\t\ta.add_edge(u,v,2);\n\t\t\t\tb.add_edge(u,v,2);\n\t\t\t}else{\n\t\t\t\ta.add_edge(u,v,1);\n\t\t\t\tb.add_edge(u,v,1);\n\t\t\t}\n\t\t}\n\t\tif(a.max_flow(sa,ta) >= 2 && b.max_flow(sb,tb) >= 2){\n\t\t\tcout << \"YES\\n\";\n\t\t}else{\n\t\t\tcout << \"NO\\n\";\n\t\t}\n\t}\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 5588, "score_of_the_acc": -0.4778, "final_rank": 9 }, { "submission_id": "aoj_1622_6025942", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <utility>\n#include <string>\n#include <vector>\n#include <set>\n#include <stack>\n#include <queue>\n#include <map>\n#include <math.h>\n#include <string.h>\n#include <iomanip>\n#include <numeric>\n#include <cstdlib>\n#include <cstdint>\n#include <cmath>\n#include <functional>\n#include <limits>\n#include <cassert>\n#include <bitset>\n#include <chrono>\n#include <random>\n#include <memory>\n \n/* macro */\n \n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)\n#define Rep(i, a, n) for (i64 i = (i64)(a); i< (i64)(n); i++)\n#define RRep(i, a, n) for (i64 i = ((i64)(n-i64(1))); i>=(i64)(a); i--)\n#define all(v) (v).begin(),(v).end()\n#define rall(v) (v).rbegin(),(v).rend()\n#define Bit(n) (1LL<<(n))\n \n/* macro end */\n \n/* template */\n \nnamespace ebi {\n \n#ifdef LOCAL\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n \nvoid debug_out() { std::cerr << std::endl; }\n \ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cout << \" :\";\n debug_out(t...);\n}\n \ntemplate <typename T1, typename T2>\nstd::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {\n return os << pa.first << \" \" << pa.second;\n}\n \ntemplate <typename T>\nstd::ostream& operator<<(std::ostream& os, std::vector<T> vec) {\n for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n \nusing size_t = std::size_t;\nusing i64 = std::int64_t;\nusing u64 = std::uint64_t;\nusing ld = long double;\n \ntemplate<class T> void fill(std::vector<T> &v) { for(T &e: v) std::cin >> e; }\n \ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b){\n a = b;\n return true;\n }\n return false;\n}\n \ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b){\n a = b;\n return true;\n }\n return false;\n}\n \ntemplate <class T>\nT gcd(T a, T b){\n if( b==0 ) return a;\n else return gcd(b, a%b);\n}\n \ntemplate <class T>\nT lcm(T a, T b) {\n a /= gcd(a, b);\n return a*b;\n}\n \ntemplate<class T>\nT Pow(T x, i64 n) {\n T res = 1;\n while(n>0){\n if(n&1) res = res *x;\n x = x*x;\n n>>=1;\n }\n return res;\n}\n \ntemplate<class T>\nT scan() {\n T val;\n std::cin >> val;\n return val;\n}\n \nconstexpr i64 LNF = std::numeric_limits<i64>::max()/4;\n \nconstexpr int INF = std::numeric_limits<int>::max();\n \nconst std::vector<int> dy = {1,0,-1,0,1,1,-1,-1};\nconst std::vector<int> dx = {0,1,0,-1,1,-1,1,-1};\n \n}\n \n/* template end */\n\nnamespace ebi {\n\nvoid main_() {\n int n,m;\n while(std::cin >> n >> m, !(n == 0 && m == 0)) {\n std::vector<std::string> c(n);\n rep(i,0,n) {\n std::cin >> c[i];\n }\n std::vector seen(n, std::vector<int>(m, -1));\n int by = -1, bx = -1;\n auto dfs = [&](auto &&self, int y, int x) -> void {\n seen[y][x] = 1;\n rep(i,0,4) {\n int ny = y + dy[i];\n int nx = x + dx[i];\n if(0 <= ny && ny < n && 0 <= nx && nx < m && (!(ny == by && nx == bx)) && seen[ny][nx] < 0 && c[ny][nx] != '#') {\n self(self, ny, nx);\n }\n }\n };\n int flag = true;\n rep(i,0,n) {\n rep(j,0,m) {\n by = i;\n bx = j;\n if((by == 0 && bx == 0) || (by == 0 && bx == m-1) || (by == n-1 && bx == m-1) || (by == n-1 && bx == 0)) {\n continue;\n }\n seen = std::vector(n, std::vector<int>(m, -1));\n dfs(dfs, 0, 0);\n if(seen[0][0] > 0 && seen[0][m-1] > 0 && seen[n-1][0] > 0 && seen[n-1][m-1] > 0) {\n flag &= true;\n }\n else {\n flag = false;\n }\n }\n }\n if(flag) {\n std::cout << \"YES\\n\";\n } \n else {\n std::cout << \"NO\\n\";\n }\n }\n}\n\n}\n \nint main() {\n std::cout << std::fixed << std::setprecision(15);\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n int t = 1;\n // std::cin >> t;\n while(t--) {\n ebi::main_();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 830, "memory_kb": 3564, "score_of_the_acc": -1.0314, "final_rank": 19 }, { "submission_id": "aoj_1622_6021393", "code_snippet": "//#pragma once\n#define _CRT_SECURE_NO_WARNINGS\n\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string>\n#include <iostream>\n#include <queue>\n#include <algorithm>\n#include <sstream>\n#include <vector>\n#include <math.h>\n#include <set>\n#include <map>\n#include <unordered_map>\n#include <numeric>\n#include <bitset>\n#include <iomanip>\n#include <cctype>\n#include <cstdlib> // srand,rand\n#include <random>\n#include <functional>\n#include <list>\n#include <stack>\n\n\n\n\nusing namespace std;\n#define ll long long\n#define ll128 long long\n#define lp(i,n) for(ll i=0;i<n;i++)\n\n#define modd 1000000007\n#define mod2 998244353\nconst double EPS = 1e-34;\nconst double PI = acos(-1);\n\n#define INF 8223372036854775807ll\n#define ALL(a) (a).begin(),(a).end()\n\n\ntypedef pair<long long, long long> pl;\ntypedef pair<long long, pl> lpl;\ntypedef pair<long double, long double> pd;\ntypedef pair<ll, string> pls;\ntypedef pair<string, ll> psl;\n\n\n\ntypedef string::const_iterator State;\nclass ParseError {};\n\n\n\n/*\nclass SegmentTree {\n\n\n\nprivate:\n\n\tll cont_num = 2;\n\tpd initial_v;\n\tvector<pd> dat;\n\n\npublic:\n\n\n\tSegmentTree() {};\n\n\n\tvoid init(ll size, double initial_value_first, double initial_value_second) {\n\n\t\tcont_num = 2;\n\t\tinitial_v.first = initial_value_first;\n\t\tinitial_v.second = initial_value_second;\n\n\t\twhile (cont_num < size) {\n\t\t\tcont_num *= 2;\n\t\t}\n\n\t\tdat.resize(2 * cont_num);//サイズ設定\n\n\t\tfor (int i = 0; i < 2 * cont_num; i++)dat[i] = initial_v;//初期化\n\n\n\n\t}\n\n\n\tvoid Update(ll position, double value_f, double value_s) {\n\n\n\n\t\tll k = cont_num + position;\n\n\t\tdat[k].first = value_f;\n\t\tdat[k].second = value_s;\n\n\t\twhile (k > 1) {\n\t\t\tk /= 2;\n\t\t\tdat[k].first = dat[k * 2 + 1].first * dat[k * 2].first;\n\t\t\tdat[k].second = dat[k * 2 + 1].first * dat[k * 2].second + dat[k * 2 + 1].second;\n\n\n\t\t}\n\n\n\t}\n\n\t/*\n\tll query_proces(ll a, ll b, ll k, ll l, ll r) {\n\n\t\tif (r <= a || b <= l)return initial_v;\n\n\t\tif (a <= l && r <= b)return dat[k];\n\t\telse {\n\t\t\tll vl = query_proces(a, b, k * 2, l, (l + r) / 2);\n\t\t\tll vr = query_proces(a, b, k * 2 + 1, (l + r) / 2, r);\n\t\t\treturn min(vl, vr);\n\t\t}\n\n\t}\n\n\n\tll query(ll left, ll right) {\n\t\treturn query_proces(left, right, 1, 0, cont_num);\n\t}\n\t*/\n\t/*\n\t\tdouble query() {\n\t\t\treturn dat[1].first + dat[1].second;\n\t\t}\n\n\t};*/\n\n\ntemplate <typename T>\nclass Zip {\n\tvector<T> d;\n\tbool flag;\n\tvoid init() {\n\t\tsort(d.begin(), d.end());\n\t\td.erase(unique(d.begin(), d.end()), d.end());\n\t\tflag = false;\n\t}\npublic:\n\tZip() {\n\t\tflag = false;\n\t}\n\tvoid add(T x) {\n\t\td.push_back(x);\n\t\tflag = true;\n\t}\n\tll getNum(T x) {\n\t\tif (flag) init();\n\t\treturn lower_bound(d.begin(), d.end(), x) - d.begin();\n\t}\n\n\tT getVal(ll num) {\n\t\treturn d[num];\n\t}\n\n\tll size() {\n\t\tif (flag) init();\n\t\treturn (ll)d.size();\n\t}\n};\n\n\n\n\ntypedef struct ST\n{\n\tll first;\n\tll second;\n\tll cost;\n\n\tbool operator<(const ST& another) const\n\t{\n\t\treturn cost < another.cost;//比較\n\t};\n\n\tbool operator>(const ST& another) const\n\t{\n\t\treturn cost > another.cost;//比較\n\t};\n\n}ST;\n//queue<ST> qst;\npriority_queue<ST, vector<ST>, greater<ST> > qst;\n\n\n\n/*vector <ST> vst;\nST st[200005];\nST bufst;\nbitset<5000> bits;*/\n\n\nlong long modinv(long long aa, long long mm) {\n\tlong long bb = mm, uu = 1, vv = 0;\n\twhile (bb) {\n\t\tlong long tt = aa / bb;\n\t\taa -= tt * bb; swap(aa, bb);\n\t\tuu -= tt * vv; swap(uu, vv);\n\t}\n\tuu %= mm;\n\tif (uu < 0) uu += mm;\n\treturn uu;\n}\n\n\n\n\n\nll zettai(ll aa) {\n\n\tif (aa < 0) {\n\t\taa *= -1;\n\t}\n\treturn aa;\n\n}\n\nfloat zettai(float aa) {\n\n\tif (aa < 0) {\n\t\taa *= -1;\n\t}\n\treturn aa;\n\n}\n\ndouble zettai(double aa) {\n\n\tif (aa < 0) {\n\t\taa *= -1;\n\t}\n\treturn aa;\n\n}\n\n\n/*\nclass UnionFind\n{\n\npublic:\n\n\n\tvector <ll>pairent;\n\tvector <ll>depth;\n\tvector <ll>size;\n\n\n\tUnionFind(ll Amount) : pairent(Amount, 1), depth(Amount, 1), size(Amount, 1) {\n\n\t\tfor (ll i = 0; i < Amount; i++) {\n\t\t\tpairent[i] = i;\n\t\t}\n\n\n\t}\n\n\n\tll FindPairent(ll x) {\n\t\tif (pairent[x] == x)return x;\n\t\telse return pairent[x] = FindPairent(pairent[x]);\n\n\t}\n\n\n\tll Merge(ll x, ll y) {\n\t\tx = FindPairent(x);\n\t\ty = FindPairent(y);\n\n\t\tif (x != y) {\n\t\t\tif (depth[x] > depth[y]) {\n\t\t\t\tpairent[y] = pairent[x];\n\t\t\t\treturn size[x] += size[y];\n\t\t\t}\n\t\t\telse {\n\t\t\t\tpairent[x] = pairent[y];\n\t\t\t\tif (depth[x] == depth[y]) {\n\t\t\t\t\tdepth[y] ++;\n\t\t\t\t}\n\n\t\t\t\treturn size[y] += size[x];\n\t\t\t}\n\n\t\t}\n\t\telse {\n\t\t\treturn -1;\n\t\t}\n\n\n\n\t}\n\n\n\n\tbool IsSame(ll x, ll y) {\n\t\tif (FindPairent(x) == FindPairent(y))return true;\n\t\telse return false;\n\t}\n\n\n\tll GetSize(ll x) {\n\t\tx = FindPairent(x);\n\t\treturn size[x];\n\t}\n\n\n};\n*/\n\n\n\ntemplate <typename T>\nclass RollingHash {\n\nprivate:\n\tvector<T> all_array;\n\tvector<T> sub_array;\n\tvector<bool> match_flag;\n\tvector<ll128> ruijou;\n\tll match_count = 0;\n\tvector<ll>match_place;\n\npublic:\n\n\tvoid Clear() {\n\t\tall_array.clear();\n\t\tsub_array.clear();\n\t\tmatch_flag.clear();\n\t\tmatch_place.clear();\n\t}\n\n\tRollingHash() {\n\t\tClear();\n\t}\n\n\tvoid AddAllArray(T factor) {\n\t\tall_array.push_back(factor);\n\t}\n\n\tvoid AddSubArray(T factor) {\n\t\tsub_array.push_back(factor);\n\t}\n\n\tvoid Calculate_Ruijou(ll128 tei, ll max_num, ll128 mod) {\n\t\truijou.clear();\n\t\truijou.push_back(1);\n\t\tif (max_num > 0) {\n\t\t\twhile (ruijou.size() < max_num + 1) {\n\t\t\t\truijou.push_back(ruijou.back() * tei % mod);\n\t\t\t}\n\t\t}\n\n\t}\n\n\tll128 GetSubHash(ll128 b, ll128 mod) {\n\t\tll128 ans = 0;\n\t\tCalculate_Ruijou(b, sub_array.size(), mod);\n\t\tfor (ll i = 0; i < sub_array.size(); i++) {\n\t\t\tans += (sub_array[i] % mod) * ruijou[sub_array.size() - 1 - i] % mod;\n\t\t\tans %= mod;\n\t\t}\n\n\t\treturn ans;\n\t}\n\n\tll128 GetAllHashInit(ll128 b, ll128 mod) {\n\t\tll128 ans = 0;\n\t\tfor (ll i = 0; i < sub_array.size(); i++) {\n\t\t\tans += (all_array[i] % mod) * ruijou[sub_array.size() - 1 - i] % mod;\n\t\t\tans %= mod;\n\t\t}\n\n\t\treturn ans;\n\t}\n\n\tvoid CheckMatchFlag() {\n\t\tmatch_place.clear();\n\t\tmatch_count = 0;\n\t\tfor (ll i = 0; i < match_flag.size(); i++) {\n\t\t\tif (match_flag[i] == true) {\n\t\t\t\tmatch_count++;\n\t\t\t\tmatch_place.push_back(i);\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid Rolling(ll128 b, ll128 mod) {\n\t\tll128 sub_hash = GetSubHash(b, mod);\n\t\tll128 all_hash = GetAllHashInit(b, mod);\n\n\t\tif (sub_hash != all_hash) {\n\t\t\tmatch_flag[0] = false;\n\t\t}\n\n\t\tfor (ll i = 1; i < match_flag.size(); i++) {\n\t\t\tall_hash = (all_hash * b % mod - all_array[i - 1] % mod * ruijou[sub_array.size()] % mod + all_array[i + sub_array.size() - 1] % mod) % mod;\n\t\t\twhile (all_hash < 0)all_hash += mod;\n\t\t\tall_hash %= mod;\n\t\t\tif (all_hash != sub_hash)match_flag[i] = false;\n\t\t}\n\t}\n\n\tvoid Match() {\n\t\tll128 b = 3;\n\t\t//ll128 mod = 999999999999999989;\n\t\tll128 mod = 1000000007;\n\t\tll128 sub_hash = GetSubHash(b, mod);\n\n\t\tif (sub_array.size() <= all_array.size()) {\n\n\t\t\twhile (match_flag.size() < all_array.size()) {\n\t\t\t\tmatch_flag.push_back(true);\n\t\t\t}\n\n\t\t\twhile (match_flag.size() > all_array.size() - sub_array.size() + 1) {\n\t\t\t\tmatch_flag.pop_back();\n\t\t\t}\n\n\n\t\t\tRolling(b, mod);\n\n\t\t\tb = 5;\n\t\t\tmod = mod2;\n\n\t\t\tRolling(b, mod);\n\n\t\t\tb = 7;\n\t\t\tmod = modd;\n\n\t\t\tRolling(b, mod);\n\n\t\t\tb = mod2;\n\t\t\tmod = modd;\n\n\t\t\tRolling(b, mod);\n\n\t\t\tCheckMatchFlag();\n\n\t\t}\n\n\n\n\t}\n\n\tll GetMatchCount() {\n\t\treturn match_count;\n\t}\n\n\tll GetMatchPlace(ll num) {\n\t\tif (num >= match_place.size())return -1;\n\t\telse return match_place[num];\n\t}\n\n};\n\n\n\nclass UnionFind\n{\n\npublic:\n\n\tvector <ll>pairent;\n\tvector <ll>depth;\n\tvector <ll>size;\n\n\tUnionFind(ll Amount) : pairent(Amount, 1), depth(Amount, 1), size(Amount, 1) {\n\n\t\tfor (ll i = 0; i < Amount; i++) {\n\t\t\tpairent[i] = i;\n\t\t}\n\t}\n\n\n\tll FindPairent(ll x) {\n\t\tif (pairent[x] == x)return x;\n\t\telse return pairent[x] = FindPairent(pairent[x]);\n\t}\n\n\n\tll Merge(ll x, ll y) {\n\t\tx = FindPairent(x);\n\t\ty = FindPairent(y);\n\n\t\tif (x != y) {\n\t\t\tif (depth[x] > depth[y]) {\n\t\t\t\tpairent[y] = pairent[x];\n\t\t\t\treturn size[x] += size[y];\n\t\t\t}\n\t\t\telse {\n\t\t\t\tpairent[x] = pairent[y];\n\t\t\t\tif (depth[x] == depth[y]) {\n\t\t\t\t\tdepth[y] ++;\n\t\t\t\t}\n\t\t\t\treturn size[y] += size[x];\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\treturn -1;\n\t\t}\n\t}\n\n\n\n\tbool IsSame(ll x, ll y) {\n\t\tif (FindPairent(x) == FindPairent(y))return true;\n\t\telse return false;\n\t}\n\n\n\tll GetSize(ll x) {\n\t\tx = FindPairent(x);\n\t\treturn size[x];\n\t}\n\n\n};\n\n\n\n\nstruct Edge\n{\n\tll a, b, cost;\n\n\n\tbool operator<(const Edge& other) const {\n\t\treturn cost < other.cost;\n\t}\n};\n\nstruct Graph\n{\n\tll n; // 頂点数\n\tvector<Edge> es; // 辺集合\n};\n\nclass Kruskal {\n\n\tGraph origin_G;\n\tGraph MST;\n\tll total_cost = 0;\n\npublic:\n\n\tvoid Solve() {\n\t\tUnionFind uf = UnionFind(MST.n);\n\t\tfor (ll i = 0; i < origin_G.es.size(); i++) {\n\t\t\tll a = origin_G.es[i].a;\n\t\t\tll b = origin_G.es[i].b;\n\t\t\tll cost = origin_G.es[i].cost;\n\n\t\t\tif (!uf.IsSame(a, b)) {\n\t\t\t\tuf.Merge(a, b);\n\t\t\t\tMST.es.push_back(origin_G.es[i]);\n\t\t\t\ttotal_cost += cost;\n\t\t\t}\n\t\t}\n\t}\n\n\tKruskal(Graph graph) {\n\t\torigin_G = graph;\n\t\tMST = graph;\n\t\tMST.es.clear();\n\t\tsort(origin_G.es.begin(), origin_G.es.end());\n\t}\n\n\tll GetMinCost() {\n\t\treturn total_cost;\n\t}\n\n};\n\n\nll RepeatSquaring(ll N, ll P, ll M) {\n\tif (P == 0) return 1;\n\tif (P % 2 == 0) {\n\t\tll t = RepeatSquaring(N, P / 2, M) % M;\n\t\treturn t * t % M;\n\t}\n\treturn N * RepeatSquaring(N, P - 1, M) % M;\n}\n\n/*\nll KurikaesiNijou(ll a, ll b, ll P) {\n\n\tif (b == 0)return 1;\n\tif (b % 2 == 0) {\n\t\tll c=KurikaesiNijou(a,b/2,P)%P;\n\t\treturn c * c %P;\n\t}\n\telse {\n\t\tll c = KurikaesiNijou(a, b/2, P) % P;\n\t\treturn a * c * c % P;\n\t}\n\n}*/\n\n\n\n\nll GCD(ll a, ll b) {\n\tif (a % b == 0)return b;\n\telse return GCD(b, a % b);\n}\n\nll Min(ll a, ll b) {\n\tif (a < b)return a;\n\telse return b;\n}\n\nll Max(ll a, ll b) {\n\tif (a > b)return a;\n\telse return b;\n}\n\nll Sum(ll a, ll b) {\n\treturn a + b;\n}\n\nll Add(ll a, ll b) {\n\treturn a + b;\n}\n\ntemplate <typename T>\nclass SegmentTree {\n\tll n;\n\tvector<T> node;\n\tfunction<T(T, T)> fun, fun2;\n\tbool customChange;\n\tT outValue, initValue;\npublic:\n\tvoid init(ll num, function<T(T, T)> resultFunction, T init, T out, function<T(T, T)> changeFunction = NULL) {\n\t\t// changeFunction: (input, beforevalue) => newvalue\n\t\tfun = resultFunction;\n\t\tfun2 = changeFunction;\n\t\tcustomChange = changeFunction != NULL;\n\t\tn = 1;\n\t\twhile (n < num) n *= 2;\n\t\tnode.resize(2 * n - 1);\n\t\tfill(node.begin(), node.end(), init);\n\t\toutValue = out;\n\t\tinitValue = init;\n\t}\n\tvoid valueChange(ll num, T value) {\n\t\tnum += n - 1;\n\t\tif (customChange) node[num] = fun2(value, node[num]);\n\t\telse node[num] = value;\n\t\twhile (num > 0) num = (num - 1) / 2, node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);\n\t}\n\tT rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b)\n\t\tif (r == -1) r = n;\n\t\tif (a <= l && r <= b) return node[k];\n\t\tif (b <= l || r <= a) return outValue;\n\t\tll mid = (l + r) / 2;\n\t\treturn fun(rangeQuery(a, b, l, mid, 2 * k + 1), rangeQuery(a, b, mid, r, 2 * k + 2));\n\t}\n};\n\n\nll extgcd(ll a, ll b, ll& x, ll& y) {\n\tif (b == 0) {\n\t\tx = 1;\n\t\ty = 0;\n\t\treturn a;\n\t}\n\tll gcd = extgcd(b, a % b, x, y);\n\tll oldX = x;\n\tx = y;\n\ty = oldX - a / b * y;\n\treturn gcd;\n}\n\n\nclass Combination {\n\n\tvector<ll> factorial;\n\tvector<ll> factorial_inv;\n\tll mod;\n\tll max_n;\n\n\npublic:\n\tvoid Init(ll init_max_n, ll init_mod) {\n\t\tmax_n = init_max_n;\n\t\tmod = init_mod;\n\t\tfactorial.resize(max_n + 1);\n\t\tfactorial[0] = 1;\n\t\tfor (ll i = 1; i < factorial.size(); i++) {\n\t\t\tfactorial[i] = factorial[i - 1] * i;\n\t\t\tfactorial[i] %= mod;\n\t\t}\n\n\t\tfactorial_inv.resize(max_n + 1);\n\t\tfactorial_inv[0] = 1;\n\t\tfor (ll i = 1; i < factorial_inv.size(); i++) {\n\t\t\tfactorial_inv[i] = factorial_inv[i - 1] * modinv(i, mod);\n\t\t\tfactorial_inv[i] %= mod;\n\t\t}\n\t}\n\n\n\n\tll GetComb(ll n, ll k) {\n\t\tif (n < k)return 0;\n\t\tif (k == 0)return 1;\n\n\t\tll comb = factorial[n];\n\t\tcomb *= factorial_inv[k];\n\t\tcomb %= mod;\n\t\tcomb *= factorial_inv[n - k];\n\t\tcomb %= mod;\n\t\treturn comb;\n\t}\n\n\tll GetH(ll n, ll k) {//n+k-1<=max_N\n\t\tll comb = factorial[n + k - 1];\n\t\tcomb *= factorial_inv[n];\n\t\tcomb %= mod;\n\t\tcomb *= factorial_inv[k - 1];\n\t\tcomb %= mod;\n\t\treturn comb;\n\t}\n\n};\n\n\n\nclass Tree {\n\n\tll node_N;\n\tvector<ll> node;\n\tvector<vector<pl>> pass;\n\tll diameter = -1;\n\tvector<ll> dist_Diamieter[2];\n\n\tpl maxDist_Num;\n\npublic:\n\n\n\tvoid Init(ll node_Num) {\n\t\tnode_N = node_Num;\n\t\tnode.resize(node_N + 1);\n\t\tpass.resize(node_N + 1);\n\n\t\tdist_Diamieter[0].resize(node_N + 1);\n\t\tlp(i, node_N + 1)dist_Diamieter[0][i] = -1;\n\t\tdist_Diamieter[1].resize(node_N + 1);\n\t\tlp(i, node_N + 1)dist_Diamieter[1][i] = -1;\n\t}\n\n\tvoid AddEdge(ll a, ll b, ll dist) {\n\t\tpl bufpl;\n\t\tbufpl.first = b;\n\t\tbufpl.second = dist;\n\t\tpass[a].push_back(bufpl);\n\t\tbufpl.first = a;\n\t\tpass[b].push_back(bufpl);\n\t}\n\n\n\tvoid DFS(ll step, ll now, ll dist) {\n\n\t\tdist_Diamieter[step][now] = dist;\n\t\tif (dist_Diamieter[step][now] > maxDist_Num.first) {\n\t\t\tmaxDist_Num.first = dist_Diamieter[step][now];\n\t\t\tmaxDist_Num.second = now;\n\t\t}\n\n\t\tfor (ll i = 0; i < pass[now].size(); i++) {\n\t\t\tll next_node = pass[now][i].first;\n\t\t\tif (dist_Diamieter[step][next_node] == -1) {\n\t\t\t\tDFS(step, next_node, dist + pass[now][i].second);\n\t\t\t}\n\t\t}\n\t}\n\n\tll GetDiameter(ll min_node_num) {\n\t\tif (diameter >= 0)return diameter;\n\t\telse {\n\t\t\tmaxDist_Num.first = -1;\n\t\t\tmaxDist_Num.second = -1;\n\t\t\tDFS(0, min_node_num, 0ll);\n\t\t\tll step2_start = maxDist_Num.second;\n\n\t\t\tmaxDist_Num.first = -1;\n\t\t\tmaxDist_Num.second = -1;\n\t\t\tDFS(1, step2_start, 0ll);\n\n\t\t\tdiameter = maxDist_Num.first;\n\t\t\treturn diameter;\n\t\t}\n\t}\n\n\tll GetDistFromMinNode(ll num) {\n\t\treturn dist_Diamieter[0][num];\n\t}\n\n};\n\n\n\n\nstruct LDE {\n\tll a, b, c, x, y;\n\tll m = 0;\n\tbool check = true;//解が存在するか\n\n\t//初期化\n\tLDE(ll a_, ll b_, ll c_) : a(a_), b(b_), c(c_) {\n\t\tll g = GCD(a, b);\n\t\tif (c % g != 0) {\n\t\t\tcheck = false;\n\t\t}\n\t\telse {\n\t\t\t//ax+by=gの特殊解を求める\n\t\t\textgcd(abs(a), abs(b), x, y);\n\t\t\tif (a < 0)x = -x;\n\t\t\tif (b < 0)y = -y;\n\t\t\t//ax+by=cの特殊解を求める(オーバフローに注意!)\n\t\t\tx *= c / g; y *= c / g;\n\t\t\t//一般解を求めるために割る\n\t\t\ta /= g; b /= g;\n\t\t}\n\t}\n\n\t//拡張ユークリッドの互除法\n\t//返り値:aとbの最大公約数\n\tll extgcd(ll a, ll b, ll& x0, ll& y0) {\n\t\tif (b == 0) {\n\t\t\tx0 = 1;\n\t\t\ty0 = 0;\n\t\t\treturn a;\n\t\t}\n\t\tll d = extgcd(b, a % b, y0, x0);\n\t\ty0 -= a / b * x0;\n\t\treturn d;\n\t}\n\n\t//パラメータmの更新(書き換え)\n\tvoid m_update(ll m_) {\n\t\tx += (m_ - m) * b;\n\t\ty -= (m_ - m) * a;\n\t\tm = m_;\n\t}\n};\n\n\n/*\ntemplate< typename Monoid >\nstruct SegmentTreee {\n\tusing F = function< Monoid(Monoid, Monoid) >;\n\n\tint sz;\n\tvector< Monoid > seg;\n\n\tconst F f;\n\tconst Monoid M1;\n\n\tSegmentTreee(int n, const F f, const Monoid& M1) : f(f), M1(M1) {\n\t\tsz = 1;\n\t\twhile (sz < n) sz <<= 1;\n\t\tseg.assign(2 * sz, M1);\n\t}\n\n\tvoid set(int k, const Monoid& x) {\n\t\tseg[k + sz] = x;\n\t}\n\n\tvoid build() {\n\t\tfor (int k = sz - 1; k > 0; k--) {\n\t\t\tseg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n\t\t}\n\t}\n\n\tvoid update(int k, const Monoid& x) {\n\t\tk += sz;\n\t\tseg[k] = x;\n\t\twhile (k >>= 1) {\n\t\t\tseg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);\n\t\t}\n\t}\n\n\tMonoid query(int a, int b) {\n\t\tMonoid L = M1, R = M1;\n\t\tfor (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n\t\t\tif (a & 1) L = f(L, seg[a++]);\n\t\t\tif (b & 1) R = f(seg[--b], R);\n\t\t}\n\t\treturn f(L, R);\n\t}\n\n\tMonoid operator[](const int& k) const {\n\t\treturn seg[k + sz];\n\t}\n\n\ttemplate< typename C >\n\tint find_subtree(int a, const C& check, Monoid& M, bool type) {\n\t\twhile (a < sz) {\n\t\t\tMonoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);\n\t\t\tif (check(nxt)) a = 2 * a + type;\n\t\t\telse M = nxt, a = 2 * a + 1 - type;\n\t\t}\n\t\treturn a - sz;\n\t}\n\n\n\ttemplate< typename C >\n\tint find_first(int a, const C& check) {\n\t\tMonoid L = M1;\n\t\tif (a <= 0) {\n\t\t\tif (check(f(L, seg[1]))) return find_subtree(1, check, L, false);\n\t\t\treturn -1;\n\t\t}\n\t\tint b = sz;\n\t\tfor (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {\n\t\t\tif (a & 1) {\n\t\t\t\tMonoid nxt = f(L, seg[a]);\n\t\t\t\tif (check(nxt)) return find_subtree(a, check, L, false);\n\t\t\t\tL = nxt;\n\t\t\t\t++a;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n\n\ttemplate< typename C >\n\tint find_last(int b, const C& check) {\n\t\tMonoid R = M1;\n\t\tif (b >= sz) {\n\t\t\tif (check(f(seg[1], R))) return find_subtree(1, check, R, true);\n\t\t\treturn -1;\n\t\t}\n\t\tint a = sz;\n\t\tfor (b += sz; a < b; a >>= 1, b >>= 1) {\n\t\t\tif (b & 1) {\n\t\t\t\tMonoid nxt = f(seg[--b], R);\n\t\t\t\tif (check(nxt)) return find_subtree(b, check, R, true);\n\t\t\t\tR = nxt;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n};\n\n*/\n\n\nvector<pair<long long, long long> > prime_factorize(long long N) {\n\tvector<pair<long long, long long> > res;\n\tfor (long long a = 2; a * a <= N; ++a) {\n\t\tif (N % a != 0) continue;\n\t\tlong long ex = 0;\n\t\twhile (N % a == 0) {\n\t\t\t++ex;\n\t\t\tN /= a;\n\t\t}\n\t\tres.push_back({ a, ex });\n\t}\n\tif (N != 1) res.push_back({ N, 1 });\n\treturn res;\n}\n\n\nCombination co;\ntypedef pair<int, int> pi;\npriority_queue<pl, vector<pl>, greater<pl> > pqpl;\n\n\npriority_queue<ll, vector<ll>, greater<ll> > pqll;\n\n\nll nck(ll n, ll k, ll m) {\n\tll ans = 1;\n\tll pre_ans = 1;\n\tif (k == 0)return 1;\n\tfor (ll i = 1; i <= k; i++) {\n\t\tpre_ans = ans;\n\t\tans = pre_ans * (n - i + 1);\n\t\tans %= m;\n\t\tans *= modinv(i, m);\n\t\tans %= modd;\n\t}\n\treturn ans;\n}\n\n\nll H, W;\nstring S[100] = {};\nvoid Search(bool gone[51][51], bool gone2[51][51], ll h, ll w) {\n\n\tgone[h][w] = true;\n\tll offset[4][2] = { {0,-1}, {1,0},{0,1},{-1,0} };\n\tlp(i, 4) {\n\t\tll next_h = h + offset[i][0];\n\t\tll next_w = w + offset[i][1];\n\t\tif (next_h < 0 || H <= next_h)continue;\n\t\tif (next_w < 0 || W <= next_w)continue;\n\t\tif (gone[next_h][next_w])continue;\n\t\tif (S[next_h][next_w] == '#')continue;\n\t\tSearch(gone, gone2, next_h, next_w);\n\t}\n\n}\n\n\nint main() {\n\twhile (true) {\n\t\tcin >> H>> W;\n\t\tif (H == 0)break;\n\n\t\tlp(i, 100)S[i].clear();\n\t\tlp(i, H)cin >> S[i];\n\t\tstring ans = \"YES\";\n\t\tlp(i, H) {\n\t\t\tlp(j, W) {\n\t\t\t\tbool gone[51][51] = {};\n\t\t\t\tbool gone2[51][51] = {};\n\t\t\t\tif (i == 0 && j == 0)continue;\n\t\t\t\tif (i == 0 && j == W-1)continue;\n\t\t\t\tif (i == H-1 && j == 0)continue;\n\t\t\t\tif (i == H-1 && j == W-1)continue;\n\t\t\t\tif (S[i][j] == '#')continue;\n\t\t\t\tS[i][j] = '#';\n\t\t\t\tSearch(gone, gone2, 0, 0);\n\t\t\t\tif ((!gone[0][0]) || (!gone[H - 1][0]) || (!gone[0][W - 1]) || (!gone[H - 1][W - 1]))ans = \"NO\";\n\t\t\t\tS[i][j] = '.';\n\t\t\t}\n\t\t}\n\t\tbool gone[51][51] = {};\n\t\tbool gone2[51][51] = {};\n\t\tSearch(gone, gone2, 0, 0);\n\t\tif ((!gone[0][0]) || (!gone[H - 1][0]) || (!gone[0][W - 1]) || (!gone[H - 1][W - 1]))ans = \"NO\";\n\t\tcout << ans << endl;\n\t}\n\n\t//cout << fixed << setprecision(12) << bb << endl;\n\treturn 0;\n}\n\n/*\n10\n10 9 8 7 6 5 4 3 2 1\n\n*/", "accuracy": 1, "time_ms": 500, "memory_kb": 3684, "score_of_the_acc": -0.6588, "final_rank": 13 }, { "submission_id": "aoj_1622_6012765", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n#define fi first\n#define se second\n#define N 214514\n#define all(x) (x).begin(), (x).end()\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\nconst ll inf = 1000000000000000000;\nll n, m;\nchar c[55][55];\nll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nbool func() {\n ll cnt = 0;\n vector<vector<bool>> used(n, vector<bool>(m));\n queue<P> que;\n que.push({0, 0});\n used[0][0] = 1;\n while (!que.empty()) {\n P p = que.front();\n que.pop();\n if ((p.fi == 0 || p.fi == n - 1) && (p.se == 0 || p.se == m - 1)) cnt++;\n for (int k = 0; k < 4; k++) {\n ll ny = p.fi + dy[k], nx = p.se + dx[k];\n if (!(0 <= ny && ny < n && 0 <= nx && nx < m)) continue;\n if (used[ny][nx]) continue;\n if (c[ny][nx] == '#') continue;\n used[ny][nx] = 1;\n que.push({ny, nx});\n }\n }\n return (cnt == 4);\n}\nvoid solve() {\n cin >> n >> m;\n if (!n) exit(0);\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++) cin >> c[i][j];\n bool ok = func();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if ((i == 0 || i == n - 1) && (j == 0 || j == m - 1)) continue;\n if (c[i][j] == '.') {\n c[i][j] = '#';\n if (!func()) ok = 0;\n c[i][j] = '.';\n }\n }\n }\n cout << (ok ? \"YES\\n\" : \"NO\\n\");\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n ll t = 1;\n // cin >> t;\n while (1) solve();\n return 0;\n}", "accuracy": 1, "time_ms": 650, "memory_kb": 3356, "score_of_the_acc": -0.7711, "final_rank": 15 }, { "submission_id": "aoj_1622_5975059", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];\n#define ft first\n#define sc second\n#define pb push_back\n#define lb lower_bound\n#define ub upper_bound\n#define all(v) (v).begin(),(v).end()\n#define LB(a,x) lb(all(a),x)-a.begin()\n#define UB(a,x) ub(all(a),x)-a.begin()\n#define mod 1000000007\n//#define mod 998244353\n#define FS fixed<<setprecision(15)\nusing namespace std;\ntypedef long long ll;\nconst double pi=3.141592653589793;\ntemplate<class T> using V=vector<T>;\nusing P=pair<ll,ll>;\ntypedef unsigned long long ull;\ntypedef long double ldouble;\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }\ntemplate<class T> inline void out(T a){ cout << a << '\\n'; }\nvoid YN(bool ok){if(ok) cout << \"Yes\" << endl; else cout << \"No\" << endl;}\n//void YN(bool ok){if(ok) cout << \"YES\" << endl; else cout << \"NO\" << endl;}\n\n\nconst ll INF=1e18;\nconst int mx=200005;\n\nint seen[55][55];\n\nint check(int i,int j,int n,int m){\n return 0<=i&&i<n&&0<=j&&j<m;\n}\n\nint main(){\n //オーバーフローは大丈夫ですか??\n cin.tie(0);ios::sync_with_stdio(false);\n while(true){\n int n,m;\n cin>>n>>m;\n if(n==0&&m==0) break;\n V<string> s(n);\n cinf(n,s);\n int ok=1;\n rep(I,n){\n rep(J,m){\n if(I==0&&J==0) continue;\n if(I==0&&J==m-1) continue;\n if(I==n-1&&J==0) continue;\n if(I==n-1&&J==m-1) continue;\n rep(i,n)rep(j,m)seen[i][j]=0;\n //seen[0][0]=1;\n V<int> dx={-1,1,0,0};\n V<int> dy={0,0,1,-1};\n function<void(int,int)> dfs=[&](int i,int j){\n seen[i][j]=1;\n rep(k,4){\n int x=i+dx[k];\n int y=j+dy[k];\n if(!check(x,y,n,m)||(x==I&&y==J)) continue;\n if(s[x][y]!='#'&&!seen[x][y])dfs(x,y);\n }\n };\n dfs(0,0);\n int f=0;\n if(!seen[n-1][0]) f=1;\n if(!seen[n-1][m-1]) f=1;\n if(!seen[0][m-1]) f=1;\n if(f) ok=0;\n }\n }\n if(ok) out(\"YES\");\n else out(\"NO\");\n }\n}", "accuracy": 1, "time_ms": 840, "memory_kb": 3636, "score_of_the_acc": -1.0584, "final_rank": 20 }, { "submission_id": "aoj_1622_5974338", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntemplate <typename Cap, bool directed> struct Dinic {\n Dinic(int n) : n(n), G(n), level(n), iter(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < n);\n assert(0 <= to && to < n);\n assert(0 <= cap);\n assert(from != to);\n int m = pos.size(), from_id = G[from].size(), to_id = G[to].size();\n pos.emplace_back(from, G[from].size());\n G[from].emplace_back(to, cap, to_id);\n G[to].emplace_back(from, directed ? 0 : cap, from_id);\n return m;\n }\n\n int add_vertex() {\n G.emplace_back();\n level.emplace_back();\n iter.emplace_back();\n return n++;\n }\n\n std::tuple<int, int, Cap, Cap> get_edge(int i) {\n assert(0 <= i && i < (int)pos.size());\n auto e = G[pos[i].first][pos[i].second];\n auto re = G[e.to][e.rev];\n return {pos[i].first, e.to, e.cap + re.cap, re.cap};\n }\n\n std::vector<std::tuple<int, int, Cap, Cap>> edges() {\n std::vector<std::tuple<int, int, Cap, Cap>> res;\n for (size_t i = 0; i < pos.size(); i++) res.emplace_back(get_edge(i));\n return res;\n }\n\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n assert(0 <= i && i < (int)pos.size());\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& e = G[pos[i].first][pos[i].second];\n auto& re = G[e.to][e.rev];\n e.cap = new_cap - new_flow;\n re.cap = (directed ? new_flow : new_cap + new_flow);\n }\n\n Cap max_flow(int s, int t) { return max_flow(s, t, std::numeric_limits<Cap>::max()); }\n\n Cap max_flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < n);\n assert(0 <= t && t < n);\n if (s == t) return 0;\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs(s, t);\n if (level[t] < 0) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(s, t, flow_limit - flow);\n if (f == 0) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n assert(0 <= s && s < n);\n std::vector<bool> visited(n);\n std::queue<int> que;\n visited[s] = true;\n que.emplace(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (const auto& e : G[v]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.emplace(e.to);\n }\n }\n }\n return visited;\n }\n\nprivate:\n struct edge {\n int to;\n Cap cap;\n int rev;\n edge(int to, Cap cap, int rev) : to(to), cap(cap), rev(rev) {}\n };\n\n int n;\n std::vector<std::vector<edge>> G;\n std::vector<std::pair<int, int>> pos;\n std::vector<int> level, iter;\n\n void bfs(int s, int t) {\n std::fill(level.begin(), level.end(), -1);\n std::queue<int> que;\n level[s] = 0;\n que.emplace(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (const auto& e : G[v]) {\n if (e.cap > 0 && level[e.to] < 0) {\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.emplace(e.to);\n }\n }\n }\n }\n\n Cap dfs(int v, int t, Cap f) {\n if (v == t) return f;\n for (int& i = iter[v]; i < (int)G[v].size(); i++) {\n auto& e = G[v][i];\n if (e.cap <= 0 || level[v] >= level[e.to]) continue;\n Cap d = dfs(e.to, t, min(f, e.cap));\n if (d <= 0) continue;\n e.cap -= d;\n G[e.to][e.rev].cap += d;\n return d;\n }\n return 0;\n }\n};\n\n/**\n * @brief Dinic (Maximum flow)\n * @docs docs/flow/Dinic.md\n */\n\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\n\nvoid solve(int N, int M) {\n vector<string> S(N);\n for (auto& s : S) cin >> s;\n\n Dinic<int, true> D(N * M * 2);\n auto IN = [&](int x, int y) { return x * M + y; };\n auto OUT = [&](int x, int y) { return IN(x, y) + N * M; };\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < M; j++) {\n if (S[i][j] == '#') continue;\n D.add_edge(IN(i, j), OUT(i, j), 1);\n for (int k = 0; k < 4; k++) {\n int ni = i + dx[k], nj = j + dy[k];\n if (ni < 0 or N <= ni or nj < 0 or M <= nj) continue;\n if (S[ni][nj] == '#') continue;\n D.add_edge(OUT(i, j), IN(ni, nj), 1);\n }\n }\n }\n\n auto D2 = D;\n int a = D.max_flow(OUT(0, 0), IN(N - 1, M - 1), 2), b = D2.max_flow(OUT(N - 1, 0), IN(0, M - 1), 2);\n cout << (a == 2 and b == 2 ? \"YES\" : \"NO\") << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int N, M;\n while (cin >> N >> M, N) solve(N, M);\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4536, "score_of_the_acc": -0.2462, "final_rank": 4 }, { "submission_id": "aoj_1622_5974336", "code_snippet": "#define LOCAL\n#include <bits/stdc++.h>\nusing namespace std;\n#pragma region Macros\ntypedef long long ll;\ntypedef __int128_t i128;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\n#define ALL(x) (x).begin(), (x).end()\n\ntemplate <typename T> istream& operator>>(istream& is, vector<T>& v) {\n for (T& x : v) is >> x;\n return is;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {\n os << '{';\n for (auto itr = m.begin(); itr != m.end();) {\n os << '(' << itr->first << ',' << itr->second << ')';\n if (++itr != m.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {\n os << '{';\n for (auto itr = s.begin(); itr != s.end();) {\n os << *itr;\n if (++itr != s.end()) os << ',';\n }\n os << '}';\n return os;\n}\ntemplate <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 == (int)v.size() ? \"\" : \" \");\n }\n return os;\n}\n\ntemplate <int i, typename T> void print_tuple(ostream&, const T&) {}\ntemplate <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {\n if (i) os << ',';\n os << get<i>(t);\n print_tuple<i + 1, T, Args...>(os, t);\n}\ntemplate <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {\n os << '{';\n print_tuple<0, tuple<Args...>, Args...>(os, t);\n return os << '}';\n}\n\nvoid debug_out() { cerr << '\\n'; }\ntemplate <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {\n cerr << head;\n if (sizeof...(Tail) > 0) cerr << \", \";\n debug_out(move(tail)...);\n}\n#ifdef LOCAL\n#define debug(...) \\\n cerr << \" \"; \\\n cerr << #__VA_ARGS__ << \" :[\" << __LINE__ << \":\" << __FUNCTION__ << \"]\" << '\\n'; \\\n cerr << \" \"; \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...) 42\n#endif\n\ntemplate <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }\ntemplate <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }\n\nint topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }\nint topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }\nint botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }\nint botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }\nint popcount(signed t) { return __builtin_popcount(t); }\nint popcount(long long t) { return __builtin_popcountll(t); }\nbool ispow2(int i) { return i && (i & -i) == i; }\n\ntemplate <class T> T ceil(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <class T> T floor(T x, T y) {\n assert(y >= 1);\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\n\ntemplate <class T1, class T2> inline bool chmin(T1& a, T2 b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T1, class T2> inline bool chmax(T1& a, T2 b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n#pragma endregion\n\n#include <cassert>\n#include <limits>\n#include <queue>\n#include <vector>\n\ntemplate <typename Cap, bool directed> struct Dinic {\n Dinic(int n) : n(n), G(n), level(n), iter(n) {}\n\n int add_edge(int from, int to, Cap cap) {\n assert(0 <= from && from < n);\n assert(0 <= to && to < n);\n assert(0 <= cap);\n assert(from != to);\n int m = pos.size(), from_id = G[from].size(), to_id = G[to].size();\n pos.emplace_back(from, G[from].size());\n G[from].emplace_back(to, cap, to_id);\n G[to].emplace_back(from, directed ? 0 : cap, from_id);\n return m;\n }\n\n int add_vertex() {\n G.emplace_back();\n level.emplace_back();\n iter.emplace_back();\n return n++;\n }\n\n std::tuple<int, int, Cap, Cap> get_edge(int i) {\n assert(0 <= i && i < (int)pos.size());\n auto e = G[pos[i].first][pos[i].second];\n auto re = G[e.to][e.rev];\n return {pos[i].first, e.to, e.cap + re.cap, re.cap};\n }\n\n std::vector<std::tuple<int, int, Cap, Cap>> edges() {\n std::vector<std::tuple<int, int, Cap, Cap>> res;\n for (size_t i = 0; i < pos.size(); i++) res.emplace_back(get_edge(i));\n return res;\n }\n\n void change_edge(int i, Cap new_cap, Cap new_flow) {\n assert(0 <= i && i < (int)pos.size());\n assert(0 <= new_flow && new_flow <= new_cap);\n auto& e = G[pos[i].first][pos[i].second];\n auto& re = G[e.to][e.rev];\n e.cap = new_cap - new_flow;\n re.cap = (directed ? new_flow : new_cap + new_flow);\n }\n\n Cap max_flow(int s, int t) { return max_flow(s, t, std::numeric_limits<Cap>::max()); }\n\n Cap max_flow(int s, int t, Cap flow_limit) {\n assert(0 <= s && s < n);\n assert(0 <= t && t < n);\n if (s == t) return 0;\n Cap flow = 0;\n while (flow < flow_limit) {\n bfs(s, t);\n if (level[t] < 0) break;\n std::fill(iter.begin(), iter.end(), 0);\n while (flow < flow_limit) {\n Cap f = dfs(s, t, flow_limit - flow);\n if (f == 0) break;\n flow += f;\n }\n }\n return flow;\n }\n\n std::vector<bool> min_cut(int s) {\n assert(0 <= s && s < n);\n std::vector<bool> visited(n);\n std::queue<int> que;\n visited[s] = true;\n que.emplace(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (const auto& e : G[v]) {\n if (e.cap && !visited[e.to]) {\n visited[e.to] = true;\n que.emplace(e.to);\n }\n }\n }\n return visited;\n }\n\nprivate:\n struct edge {\n int to;\n Cap cap;\n int rev;\n edge(int to, Cap cap, int rev) : to(to), cap(cap), rev(rev) {}\n };\n\n int n;\n std::vector<std::vector<edge>> G;\n std::vector<std::pair<int, int>> pos;\n std::vector<int> level, iter;\n\n void bfs(int s, int t) {\n std::fill(level.begin(), level.end(), -1);\n std::queue<int> que;\n level[s] = 0;\n que.emplace(s);\n while (!que.empty()) {\n int v = que.front();\n que.pop();\n for (const auto& e : G[v]) {\n if (e.cap > 0 && level[e.to] < 0) {\n level[e.to] = level[v] + 1;\n if (e.to == t) return;\n que.emplace(e.to);\n }\n }\n }\n }\n\n Cap dfs(int v, int t, Cap f) {\n if (v == t) return f;\n for (int& i = iter[v]; i < (int)G[v].size(); i++) {\n auto& e = G[v][i];\n if (e.cap <= 0 || level[v] >= level[e.to]) continue;\n Cap d = dfs(e.to, t, min(f, e.cap));\n if (d <= 0) continue;\n e.cap -= d;\n G[e.to][e.rev].cap += d;\n return d;\n }\n return 0;\n }\n};\n\n/**\n * @brief Dinic (Maximum flow)\n * @docs docs/flow/Dinic.md\n */\n\nconst int INF = 1e9;\nconst long long IINF = 1e18;\nconst int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};\nconst char dir[4] = {'D', 'R', 'U', 'L'};\nconst long long MOD = 1000000007;\n// const long long MOD = 998244353;\n\nvoid solve(int N, int M) {\n vector<string> S(N);\n for (auto& s : S) cin >> s;\n\n Dinic<int, true> D(N * M * 2);\n auto IN = [&](int x, int y) { return x * M + y; };\n auto OUT = [&](int x, int y) { return IN(x, y) + N * M; };\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < M; j++) {\n if (S[i][j] == '#') continue;\n D.add_edge(IN(i, j), OUT(i, j), 1);\n for (int k = 0; k < 4; k++) {\n int ni = i + dx[k], nj = j + dy[k];\n if (ni < 0 or N <= ni or nj < 0 or M <= nj) continue;\n if (S[ni][nj] == '#') continue;\n D.add_edge(OUT(i, j), IN(ni, nj), 1);\n }\n }\n }\n\n auto D2 = D;\n int a = D.max_flow(OUT(0, 0), IN(N - 1, M - 1), 2), b = D2.max_flow(OUT(N - 1, 0), IN(0, M - 1), 2);\n cout << (a == 2 and b == 2 ? \"YES\" : \"NO\") << '\\n';\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int N, M;\n while (cin >> N >> M, N) solve(N, M);\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4632, "score_of_the_acc": -0.2783, "final_rank": 5 }, { "submission_id": "aoj_1622_5968721", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\n\nstruct Edge{\n int to,rev; ll cap;\n Edge(int to,ll cap,int rev):to(to),cap(cap),rev(rev){}\n};\n// ここの値に注意!!\nconst int N=5200;\nconst ll INF=1e9;\nvector<Edge> g[N];\nint level[N]; \nint iter[N];\nvoid add_edge(int s,int t,ll c){\n g[s].push_back(Edge(t,c,g[t].size()));\n g[t].push_back(Edge(s,0,g[s].size()-1));\n}\nvoid bfs(int s){\n memset(level,-1,sizeof(level));\n queue<int> q;\n level[s]=0;\n q.push(s);\n while(q.size()){\n int v=q.front(); q.pop();\n for(auto &e:g[v]){\n if(e.cap>0&&level[e.to]<0){\n level[e.to]=level[v]+1;\n q.push(e.to);\n }\n }\n }\n}\nll dfs(int v,int t,ll f){\n if(v==t)return f;\n for(int &i=iter[v];i<g[v].size();i++){\n Edge &e=g[v][i];\n if(e.cap>0&&level[v]<level[e.to]){\n int d=dfs(e.to,t,min(f,e.cap));\n if(d>0){\n e.cap-=d;\n g[e.to][e.rev].cap+=d;\n return d;\n }\n }\n }\n return 0;\n}\nll max_flow(int s,int t){\n ll flow=0;\n while(1){\n bfs(s);\n if(level[t]<0)return flow;\n memset(iter,0,sizeof(iter));\n int f;\n while((f=dfs(s,t,INF))>0){\n flow+=f;\n }\n }\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n int h,w;\n while(cin >> h >> w,h){\n vector<string> s(h);\n for(int i=0;i<h;i++){\n cin >> s[i];\n }\n int S = 2*h*w;\n int t[3]={w-1,(h-1)*w,h*w-1};\n int T = 2*h*w+1;\n bool ok = true;\n for(int _=0;_<3;_++){\n for(int i=0;i<=T;i++){\n g[i].clear();\n }\n add_edge(S,0,2);\n for(int i=0;i<h;i++){\n for(int j=0;j<w;j++){\n if(s[i][j] == '.'){\n if(i+1<h and s[i+1][j] == '.'){\n add_edge(i*w+j+h*w,(i+1)*w+j,1);\n add_edge((i+1)*w+j+h*w, i*w+j, 1);\n }\n if(j+1<w and s[i][j+1] == '.'){\n add_edge(i*w+j+h*w,i*w+j+1,1);\n add_edge(i*w+j+1+h*w, i*w+j, 1);\n }\n }\n if(i+j == 0 or i*w+j == t[_]){\n add_edge(i*w+j, i*w+j+w*h, 2);\n }\n else{\n add_edge(i*w+j, i*w+j+w*h, 1);\n }\n }\n }\n add_edge(t[_]+h*w, T, 2);\n if(max_flow(S, T)<2){\n ok = false;\n }\n }\n if(ok){\n cout << \"YES\" << \"\\n\";\n }\n else{\n cout << \"NO\" << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 4252, "score_of_the_acc": -0.187, "final_rank": 3 } ]
aoj_1619_cpp
Making Lunch Boxes Taro has been hooked on making lunch boxes recently. Taro has obtained a new lunch box recipe book today, and wants to try as many of the recipes listed in the book as possible. Enough of the ingredients for all the recipes are at hand, but they all are in vacuum packs of two. If only one of them is used leaving the other, the leftover will be rotten easily, but making two of the same recipe is not interesting. Thus he decided to make a set of lunch boxes, each with different recipe, that wouldn't leave any unused ingredients. Note that the book may include recipes for different lunch boxes made of the same set of ingredients. How many lunch box recipes can Taro try today at most following his dogma? Input The input consists of at most 50 datasets, each in the following format. n m b 1,1 ... b 1, m ... b n, 1 ... b n,m The first line contains n , which is the number of recipes listed in the book, and m , which is the number of ingredients. Both n and m are positive integers and satisfy 1 ≤ n ≤ 500, 1 ≤ m ≤ 500 and 1 ≤ n × m ≤ 500 . The following n lines contain the information for each recipe with the string of length m consisting of 0 or 1. b i,j implies whether the i -th recipe needs the j -th ingredient. 1 means the ingredient is needed for the recipe and 0 means not. Each line contains at least one 1. The end of the input is indicated by a line containing two zeros. Output For each dataset, output the maximum number of recipes Taro can try. Sample Input 4 3 110 101 011 110 7 1 1 1 1 1 1 1 1 4 5 10000 01000 00100 00010 6 6 111111 011000 100000 000010 100001 100100 0 0 Output for the Sample Input 3 6 0 6
[ { "submission_id": "aoj_1619_10849110", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<map>\n#include<vector>\n#include<string>\n#include<algorithm>\n\nusing namespace std;\n\nstring xx(string a, string b)\n{\n for(int i = 0; i < a.size(); i++)\n a[i] = (a[i]-'0')^(b[i]-'0')+'0';\n return a;\n}\n\nint main()\n{\n int n, m;\n while(cin >> n >> m && n != 0)\n {\n map<string, int> _m;\n vector<string> v(n);\n for(int i = 0; i < n; i++)\n cin >> v[i];\n for(int i = 0; i < v.size(); i++)\n {\n map<string, int> ma = _m;\n map<string, int>::iterator it = _m.begin();\n while(it != _m.end())\n {\n string tmp = xx(v[i], it->first);\n if(ma.count(tmp))\n {\n ma[tmp] = max(it->second + 1, ma[tmp]);\n }\n else\n ma[tmp] = it->second + 1;\n it++;\n }\n _m = ma;\n if(!_m.count(v[i]))\n _m[v[i]] = 1;\n }\n //for(map<string, int>::iterator it = ma.begin(); it != ma.end(); it++)\n string zero;\n for(int i = 0; i < m; i++)\n zero.push_back('0');\n if(_m.count(zero))\n {\n cout << _m[zero] << endl;\n }\n else\n cout << 0 << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 3790, "memory_kb": 232696, "score_of_the_acc": -1.4561, "final_rank": 16 }, { "submission_id": "aoj_1619_10717133", "code_snippet": "#include<bits/stdc++.h>\n//#include<atcoder/all>\nusing namespace std;\n//using namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\n\n//using mint = modint998244353;\n//using mint = modint1000000007;\n\ntemplate<class T> using pq = priority_queue<T>; //大きい順\ntemplate<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順\n\n#define vec_unique(v) v.erase(unique(v.begin(), v.end()), v.end()) //重複削除\n#define vec_iota(v) iota(v.begin(), v.end(), 0) //0, 1, 2, 3, ..., n - 1にセット\n\nint dx[4] = {1, -1, 0, 0};\nint dy[4] = {0, 0, 1, -1};\n//int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};\n//int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};\n\n#define INF 2e18\n#define INF2 2e9\n\nint main() {\n while(true) {\n int n, m;\n cin >> n >> m;\n if(n == 0 && m == 0) {\n break;\n }\n\n vector<vector<char>> b(n, vector<char>(m));\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n cin >> b[i][j];\n }\n }\n\n if(n <= m) {\n int ans = 0;\n for(int i = 0; i < (1 << n); i++) {\n vector<int> ing(m);\n int cnt = 0;\n for(int j = 0; j < n; j++) {\n if(i & (1 << j)) {\n cnt++;\n for(int k = 0; k < m; k++) {\n if(b[j][k] == '1') {\n ing[k]++;\n }\n }\n }\n }\n bool flag = true;\n for(int k = 0; k < m; k++) {\n if(ing[k] % 2 == 1) {\n flag = false;\n }\n }\n\n if(flag) {\n ans = max(ans, cnt);\n }\n }\n\n cout << ans << endl;\n } else {\n vector<int> w(n);\n for(int i = 0; i < n; i++) {\n int d = 1;\n for(int j = m - 1; j >= 0; j--) {\n if(b[i][j] == '1') w[i] += d;\n d *= 2;\n }\n }\n\n vector<vector<int>> dp(n + 1, vector<int>((1 << m), -1));\n dp[0][0] = 0;\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < (1 << m); j++) {\n dp[i + 1][j] = dp[i][j];\n }\n for(int j = 0; j < (1 << m); j++) {\n if(dp[i][j] != -1) dp[i + 1][j ^ w[i]] = max(dp[i + 1][j ^ w[i]], dp[i][j] + 1);\n }\n }\n\n cout << dp[n][0] << endl;\n }\n }\n //cout << fixed << setprecision(15) << << endl;\n return 0;\n}", "accuracy": 1, "time_ms": 7190, "memory_kb": 208672, "score_of_the_acc": -1.8288, "final_rank": 20 }, { "submission_id": "aoj_1619_10713609", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint n,m;\nmap<string,int>Table;\n\nchar now[505];\n\nstruct node\n{\n string s;\n int x;\n\n //node(string s,int x):s(s),x(x){}\n};\n\nqueue<node>q;\n\nvoid dp()\n{\n map<string,int>Temp;\n map<string,int>::iterator it;\n\n for (it=Table.begin();it!=Table.end();it++)\n {\n string old=it->first;\n string New=\"\";\n for (int i=0;i<m;i++)\n if (now[i]==old[i]) New+=\"0\";else New+=\"1\";\n\n if (!Table.count(New))\n {\n node t;\n t.s=New;t.x=it->second+1;\n q.push(t);\n }\n else if (Table[New]<it->second+1)\n {\n Temp[New]=max(Temp[New],it->second+1);\n //cout<<old<<\" \"<<New<<\" \"<<Table[New]<<endl;\n }\n }\n\n while (!q.empty())\n {\n node temp=q.front();q.pop();\n Table[temp.s]=max(Table[temp.s],temp.x);\n //cout<<temp.s<<\" \"<<temp.x<<endl;\n }\n\n for (it=Temp.begin();it!=Temp.end();it++)\n {\n string s=it->first;\n Table[s]=Temp[s];\n }\n}\n\nint main()\n{\n //ios_base::sync_with_stdio(false);\n //cin.tie(NULL); cout.tie(NULL);\n while (cin>>n>>m)\n {\n if (n==0 && m==0) break;\n\n string init=\"\";\n for (int i=0;i<m;i++) init+=\"0\";\n Table.clear();\n Table[init]=0;\n\n for (int i=0;i<n;i++)\n {\n //cin>>now;\n scanf(\"%s\",now);\n dp();\n }\n\n printf(\"%d\\n\",Table[init]);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 5030, "memory_kb": 120196, "score_of_the_acc": -1.1537, "final_rank": 14 }, { "submission_id": "aoj_1619_10713590", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint rp[505][505],n,m;\nmap<string,int>Table;\nstring now;\n\nstruct node\n{\n string s;\n int x;\n\n //node(string s,int x):s(s),x(x){}\n};\n\nvoid dp()\n{\n queue<node>q;\n map<string,int>Temp;\n\n map<string,int>::iterator it;\n for (it=Table.begin();it!=Table.end();it++)\n {\n string old=it->first;\n string New=\"\";\n for (int i=0;i<m;i++)\n if (now[i]==old[i]) New+=\"0\";else New+=\"1\";\n\n if (!Table.count(New))\n {\n node t;\n t.s=New;t.x=it->second+1;\n q.push(t);\n }\n else if (Table[New]<it->second+1)\n {\n Temp[New]=max(Temp[New],it->second+1);\n //cout<<old<<\" \"<<New<<\" \"<<Table[New]<<endl;\n }\n }\n\n while (!q.empty())\n {\n node temp=q.front();q.pop();\n Table[temp.s]=max(Table[temp.s],temp.x);\n //cout<<temp.s<<\" \"<<temp.x<<endl;\n }\n\n for (it=Temp.begin();it!=Temp.end();it++)\n {\n string s=it->first;\n Table[s]=Temp[s];\n }\n}\n\nint main()\n{\n //std::ios::sync_with_stdio(false);\n while (cin>>n>>m)\n {\n if (n==0 && m==0) break;\n\n string init=\"\";\n for (int i=0;i<m;i++) init+=\"0\";\n Table.clear();\n Table[init]=0;\n\n for (int i=0;i<n;i++)\n {\n cin>>now;\n dp();\n }\n\n cout<<Table[init]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4960, "memory_kb": 120120, "score_of_the_acc": -1.1436, "final_rank": 13 }, { "submission_id": "aoj_1619_10713579", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint rp[505][505],n,m;\nmap<string,int>Table;\n\nstruct node\n{\n string s;\n int x;\n\n node(string s,int x):s(s),x(x){}\n};\n\nvoid dp(string now)\n{\n queue<node>q;\n map<string,int>Temp;\n for (map<string,int>::iterator it=Table.begin();it!=Table.end();it++)\n {\n string old=it->first;\n string New=\"\";\n for (int i=0;i<m;i++)\n if (now[i]==old[i]) New+=\"0\";else New+=\"1\";\n\n if (!Table.count(New))\n {\n q.push(node(New,Table[old]+1));\n }\n else if (Table[New]<Table[old]+1)\n {\n Temp[New]=max(Temp[New],Table[old]+1);\n //cout<<old<<\" \"<<New<<\" \"<<Table[New]<<endl;\n }\n }\n\n while (!q.empty())\n {\n node temp=q.front();q.pop();\n Table[temp.s]=max(Table[temp.s],temp.x);\n //cout<<temp.s<<\" \"<<temp.x<<endl;\n }\n\n for (map<string,int>::iterator it=Temp.begin();it!=Temp.end();it++)\n {\n string s=it->first;\n Table[s]=Temp[s];\n }\n}\n\nint main()\n{\n std::ios::sync_with_stdio(false);\n while (cin>>n>>m)\n {\n if (n==0 && m==0) break;\n\n string init=\"\";\n for (int i=0;i<m;i++) init+=\"0\";\n Table.clear();\n Table[init]=0;\n\n for (int i=0;i<n;i++)\n {\n string s;\n cin>>s;\n dp(s);\n }\n\n cout<<Table[init]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 6180, "memory_kb": 120204, "score_of_the_acc": -1.3141, "final_rank": 15 }, { "submission_id": "aoj_1619_10713574", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint rp[505][505],n,m;\nmap<string,int>Table;\n\nstruct node\n{\n string s;\n int x;\n\n node(string s,int x):s(s),x(x){}\n};\n\nvoid dp(string now)\n{\n queue<node>q;\n for (map<string,int>::iterator it=Table.begin();it!=Table.end();it++)\n {\n string old=it->first;\n string New=\"\";\n for (int i=0;i<m;i++)\n if (now[i]==old[i]) New+=\"0\";else New+=\"1\";\n\n if (!Table.count(New))\n {\n q.push(node(New,Table[old]+1));\n }\n else if (Table[New]<Table[old]+1)\n {\n q.push(node(New,Table[old]+1));\n //cout<<old<<\" \"<<New<<\" \"<<Table[New]<<endl;\n }\n }\n\n while (!q.empty())\n {\n node temp=q.front();q.pop();\n Table[temp.s]=temp.x;\n //cout<<temp.s<<\" \"<<temp.x<<endl;\n\n }\n}\n\nint main()\n{\n std::ios::sync_with_stdio(false);\n while (cin>>n>>m)\n {\n if (n==0 && m==0) break;\n\n string init=\"\";\n for (int i=0;i<m;i++) init+=\"0\";\n Table.clear();\n Table[init]=0;\n\n for (int i=0;i<n;i++)\n {\n string s;\n cin>>s;\n dp(s);\n }\n\n cout<<Table[init]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 4530, "memory_kb": 119508, "score_of_the_acc": -1.0811, "final_rank": 12 }, { "submission_id": "aoj_1619_10713567", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint n, m;\nchar b[505][505];\nint dp[1<<24], tmp[1<<24];\nint tb[505];\n\nbool input() {\n cin >> n >> m;\n if (!n && !m) return 0;\n for (int i = 0 ; i < n ; i++ ) {\n cin >> b[i];\n }\n return 1;\n}\n\nvoid solve() {\n int res = 0;\n memset(dp, -1, sizeof(dp));\n if ( n > m ) {\n memset(dp,-1,sizeof(dp));\n dp[0]=0;\n \n for (int i=0; i<n; i++){\n char *pEnd;\n int bb=strtol(b[i],&pEnd,2);\n for (int S=(1<<m)-1; S>=0; S--){\n if (dp[S^bb]!=-1)tmp[S] = dp[S^bb]+1;\n else tmp[S]=-1;\n }\n for (int S=(1<<m)-1; S>=0; S--){\n dp[S] = max(dp[S],tmp[S]);\n }\n res = dp[0];\n }\n } else { // n <= m\n for (int i = 1 ; i < (1<<n) ; i++ ) {\n int val = 0, cnt = 0;\n memset(tb, 0, sizeof(tb));\n for (int j = 0 ; j < n ; j++ ) {\n if ( i & (1 << j) ) {\n for ( int k = 0 ; k < m ; k++ ) {\n tb[k] ^= (b[j][k] - '0');\n }\n cnt++;\n }\n }\n bool flag = true;\n for ( int k = 0 ; k < m ;k++ ) {\n if ( tb[k] ) {\n flag = false;\n break;\n }\n }\n if ( flag ) {\n res = max(res, cnt);\n }\n }\n }\n cout << res << endl;\n}\n\nint main(){\n while(input()) {\n solve();\n }\n}", "accuracy": 1, "time_ms": 5220, "memory_kb": 79388, "score_of_the_acc": -1.0078, "final_rank": 10 }, { "submission_id": "aoj_1619_10713520", "code_snippet": "#include<cstdio>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<cstring>\nusing namespace std;\n\nconst int N = 500;\n\nint a[N + 2][N + 2], n, m, st[(1 << 22) + 2], st2[(1 << 22) + 2], sum[N + 2], val[N + 2];\n\nint main()\n{\n\tfor (; scanf(\"%d%d\", &n, &m) && (n || m); )\n\t{\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tfor (int j = 0; j < m; j++)\n\t\t\t{\n\t\t\t\tscanf(\"%1d\", &a[i][j]);\n\t\t\t}\n\t\tint ans = 0;\n\t\tif (n <= 22)\n\t\t{\n\t\t\tfor (int k = 1; k < 1 << n; k++)\n\t\t\t{\n\t\t\t\tfor (int i = 0; i < m; i++)\n\t\t\t\t\tsum[i] = 0;\n\t\t\t\tint cnt = 0;\n\t\t\t\tfor (int l = 0; l < n; l++)\n\t\t\t\t\tif (k >> l & 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tcnt++;\n\t\t\t\t\t\tfor (int i = 0; i < m; i++)\n\t\t\t\t\t\t\tsum[i] ^= a[l][i];\n\t\t\t\t\t}\n\t\t\t\tint ok = 1;\n\t\t\t\tfor (int i = 0; i < m; i++)\n\t\t\t\t\tif (sum[i])\n\t\t\t\t\t\tok = 0;\n\t\t\t\tif (ok)\n\t\t\t\t\tans = max(ans, cnt);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = 0; i < 1 << m; i++)\n\t\t\t\tst[i] = st2[i] = 0;\n\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t{\n\t\t\t\tval[i] = 0;\n\t\t\t\tfor (int j = 0; j < m; j++)\n\t\t\t\t{\n\t\t\t\t\tval[i] = 2 * val[i] + a[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t{\n\t\t\t\tif (st[val[i]] > 0)\n\t\t\t\t\tans = max(ans, st[val[i]] + 1);\n\t\t\t\tfor (int j = 0; j < 1 << m; j++)\n\t\t\t\t\tst2[j^val[i]] = st[j] > 0 ? st[j] + 1 : 0;\n\t\t\t\tst2[val[i]] = max(st2[val[i]], 1);\n\t\t\t\tfor (int j = 0; j < 1 << m; j++)\n\t\t\t\t\tst[j] = max(st[j], st2[j]);\n\t\t\t}\n\t\t}\n\t\tprintf(\"%d\\n\", ans);\n\t}\n}\n\n/*\n\n\n*/", "accuracy": 1, "time_ms": 5310, "memory_kb": 24384, "score_of_the_acc": -0.7879, "final_rank": 8 }, { "submission_id": "aoj_1619_10702739", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nmap < unsigned long long , int > dp[505];\nbitset < 505 > a[505];\nint n,m;\nchar s[505];\nhash < bitset < 505 > > H;\n\nint dfs( int pos, bitset < 505 > s ){\n\tif ( pos == n ){\n\t\tif ( s.count() == 0 ) return 0;\n\t\telse return -1e9;\n\t}\n\t\n\thash < bitset < 505 > > H;\n\tif ( dp[pos].find(H(s)) != dp[pos].end() ) return dp[pos][H(s)];\n\tint ans = dfs(pos+1,s);\n\tans = max( ans, dfs(pos+1, s ^ a[pos]) + 1);\n\treturn dp[pos][H(s)] = ans;\n}\n\nint main(){\n\t\n\twhile ( scanf(\"%d%d\",&n,&m) == 2 && n + m > 0 ){\n\t\tfor ( int i = 0; i < n; i++ ){\n\t\t\ta[i].reset();\n\t\t\tscanf(\"%s\",&s);\n\t\t\tint len = strlen(s);\n\t\t\tfor ( int j = 0; j < m; j++ ){\n\t\t\t\tif ( s[j] == '0' ) continue;\n\t\t\t\ta[i].set(j);\n\t\t\t}\n\t\t\tdp[i].clear();\n\t\t}\n\t\t\n\t\tbitset < 505 > bs;\n\t\tprintf(\"%d\\n\",dfs(0,bs));\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 6480, "memory_kb": 153228, "score_of_the_acc": -1.4955, "final_rank": 17 }, { "submission_id": "aoj_1619_10685335", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n\nusing ll = long long;\nconst int MOD = 1000000007;\nconst int Mod = 998244353;\nconst int MAX = 1000000005;\nconst long long INF = 1000000000000000005LL;\n#define rep(i, n) for (int i = 0; i < (n); i++)\n#define rep2(i, a, b) for (int i = (a); i < (b); i++)\n#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)\n#define all(x) (x).begin(),(x).end()\ntemplate<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }\n\nbool solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0 && m == 0) return false;\n vector<string> b(n);\n for (int i = 0; i < n; i++) cin >> b[i];\n int ans = 0;\n if (n <= 22) {\n vector<int> s(m);\n for (int bit = 0; bit < (1 << n); bit++) {\n for (int i = 0; i < n; i++) {\n if (bit & (1 << i)) {\n for (int j = 0; j < m; j++) s[j] ^= (int)(b[i][j] - '0');\n }\n }\n bool ok = true;\n for (int i = 0; i < m; i++) {\n if (s[i]) {\n ok = false;\n s[i] = 0;\n }\n }\n if (ok) ans = max(ans, __builtin_popcount(bit));\n }\n } else {\n vector<vector<int>> dp(n+1, vector<int>(1 << m, -1));\n dp[0][0] = 0;\n for (int i = 0; i < n; i++) {\n int binary = 0;\n for (int j = 0; j < m; j++) binary += (int)(b[i][j] - '0') * (1 << j);\n for (int bit = 0; bit < (1 << m); bit++) {\n dp[i+1][bit] = max(dp[i+1][bit], dp[i][bit]);\n if (dp[i][bit] != -1) {\n int nxtbit = bit ^ binary;\n dp[i+1][nxtbit] = max(dp[i+1][nxtbit], dp[i][bit] + 1);\n }\n }\n }\n ans = dp[n][0];\n }\n cout << ans << endl;\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 6350, "memory_kb": 208976, "score_of_the_acc": -1.7129, "final_rank": 19 }, { "submission_id": "aoj_1619_10668271", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool solve(){\n int n,m;cin>>n>>m;\n if(n==0)return 0;\n vector<string>b(n);\n for(auto &x:b)cin>>x;\n\n int ans=0;\n if(n<m){\n for(int s=0;s<(1<<n);s++){\n vector<int>c(m);\n int pc=0;\n for(int i=0;i<n;i++){\n if(s>>i&1){\n pc++;\n for(int j=0;j<m;j++)c[j]^=(b[i][j]-'0');\n }\n }\n bool ng=false;\n for(int j=0;j<m;j++)if(c[j])ng=true;\n if(!ng)ans=max(ans,pc);\n }\n }else{\n vector<int>dp(1<<m,-1e6);\n dp[0]=0;\n //使用材料xorがsのときのmax\n vector<int>c(n);\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++)c[i]=c[i]*2+int(b[i][j]-'0');\n }\n for(int i=0;i<n;i++){\n vector<int>old(1<<m,-1e6);\n swap(old,dp);\n for(int s=0;s<(1<<m);s++){\n dp[s]=max(dp[s],old[s]);\n dp[s^c[i]]=max(dp[s^c[i]],old[s]+1);\n }\n } \n ans=dp[0];\n }\n cout<<ans<<endl;\n return 1;\n}\nint main(){\n while(solve());\n return 0;\n}", "accuracy": 1, "time_ms": 2960, "memory_kb": 36356, "score_of_the_acc": -0.5108, "final_rank": 3 }, { "submission_id": "aoj_1619_10668267", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nbool solve(){\n int n,m;cin>>n>>m;\n if(n==0)return 0;\n vector<string>b(n);\n for(auto &x:b)cin>>x;\n\n int ans=0;\n if(n<m){\n for(int s=0;s<(1<<n);s++){\n vector<int>c(m);\n int pc=0;\n for(int i=0;i<n;i++){\n if(s>>i&1){\n pc++;\n for(int j=0;j<m;j++)c[j]^=(b[i][j]-'0');\n }\n }\n bool ng=false;\n for(int j=0;j<m;j++)if(c[j])ng=true;\n if(!ng)ans=max(ans,pc);\n }\n }else{\n vector<int>dp(1<<m,-1e6);\n dp[0]=0;\n //使用材料xorがsのときのmax\n vector<int>c(n);\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++)c[i]=c[i]*2+int(b[i][j]-'0');\n }\n for(int i=0;i<n;i++){\n vector<int>old(1<<m,-1e6);\n swap(old,dp);\n for(int s=0;s<(1<<m);s++){\n dp[s]=max(dp[s],old[s]);\n dp[s^c[i]]=max(dp[s^c[i]],old[s]+1);\n }\n } \n ans=dp[0];\n }\n cout<<ans<<endl;\n return 1;\n}\nint main(){\n while(solve());\n\n return 0;\n}", "accuracy": 1, "time_ms": 2960, "memory_kb": 36412, "score_of_the_acc": -0.511, "final_rank": 4 }, { "submission_id": "aoj_1619_10658718", "code_snippet": "#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 数値型\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\nusing P = pair<int,int>;\nusing Pll = pair<ll, ll>;\nusing Pli = pair<ll, int>;\nusing Pil = pair<int, ll>;\n\n// vector関連\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\ntemplate<typename T>\nusing vc = vector<T>;\ntemplate<typename T>\nusing vvc = vector<vc<T>>;\ntemplate<typename T>\nusing vvvc = vector<vvc<T>>;\ntemplate<typename T>\nusing vvvvc = vector<vvvc<T>>;\n\n// priority_queue\ntemplate<typename T>\nusing pq = priority_queue<T>;\ntemplate<typename T>\nusing pqg = priority_queue<T, vc<T>, greater<T>>;\n\n#define rep(i, n) for(int i = 0; i < (int)(n); i++)\n#define FOR(i, a, b) for(int i = a; i < (int)(b); i++)\n#define all(a) (a).begin(),(a).end()\n#define rall(a) (a).rbegin(),(a).rend()\n#define MIN(vec) *min_element(vec)\n#define MAX(vec) *max_element(vec)\n#define next_perm(vec) (vec).begin(), (vec).end()\n#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())\n#define el \"\\n\"\n#define Yes cout << \"Yes\" << el\n#define No cout << \"No\" << el\n#define YES cout << \"YES\" << el\n#define NO cout << \"NO\" << el\n#define EPS 1e-8\n#define Equal(a, b) (fabs((a)-(b)) < EPS) \n#define dbg(x) cerr << #x << \"=\" << x << el \n\n// 定数\nconst string abc = \"abcdefghijklmnopqrstuvwxyz\";\nconst string ABC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconstexpr int INF = 1001001001;\nconstexpr ll LINF = 1001001001001001001ll;\nconstexpr int DX[] = {1, 0, -1, 0};\nconstexpr int DY[] = {0, 1, 0, -1};\nconstexpr int DX8[] = {1, 0, -1, 0, 1, 1, -1, -1};\nconstexpr int DY8[] = {0, 1, 0, -1, 1, -1, 1, -1};\n\ntemplate<typename T1, typename T2>\nostream &operator<< (ostream &os, pair<T1, T2> p) {\n os << \"{\" << p.first << \",\" << p.second << \"}\";\n return os;\n}\ntemplate<typename T>\nostream &operator<< (ostream &os, vc<T> &vec) {\n int sz = vec.size();\n rep(i, sz){\n os << vec[i] << (i==sz-1?\"\":\" \");\n }\n return os;\n}\n\ntemplate<typename T1, typename T2>\nistream &operator>> (istream &is, pair<T1, T2> &p) {\n is >> p.first >> p.second;\n return is;\n}\ntemplate<typename T>\nistream &operator>> (istream &is, vc<T> &vec) {\n int sz = vec.size();\n rep(i, sz) { is >> vec[i]; }\n return is;\n}\n/// @brief aとbの最大値をaに格納。更新があったかbool値を返す\n/// @tparam T1 \n/// @tparam T2 \n/// @param a \n/// @param b \n/// @return bool\ntemplate<typename T1, typename T2>\ninline bool chmax(T1 &a, T2 b){\n bool ret = a<b;\n if(ret) a = b;\n return ret;\n}\n\n/// @brief aとbの最小値をaに格納。更新があったかbool値を返す\n/// @tparam T1 \n/// @tparam T2 \n/// @param a \n/// @param b \n/// @return bool\ntemplate<typename T1, typename T2>\ninline bool chmin(T1 &a, T2 b){\n bool ret = a>b;\n if(ret) {a = b;}\n return ret;\n}\n\ninline void YesNo(bool flag){\n if(flag) {Yes;}\n else {No;}\n return;\n}\n\ninline void YESNO(bool flag){\n if(flag) {YES;}\n else {NO;}\n return;\n}\n\ninline bool outof(ll x, ll xlim){\n return (x<0 || x>=xlim);\n}\n\ntemplate<typename T>\ninline T sqnorm(T x, T y){\n return x*x+y*y;\n}\n\n/// @brief char->int\n/// @param c \n/// @return int\ninline int ctoi(char c){\n return c-'0';\n}\n\n/// @brief xを素因数分解\n/// @param x \n/// @return vector<Pli>, 素因数の昇順に {p, cnt}\nvector<Pli> prime_fact(ll x){\n vector<Pli> ret;\n for(ll i=2; i*i<=x; i++){\n if(x%i == 0){\n ret.emplace_back(i, 0);\n while(x%i == 0){\n ret.back().second++;\n x /= i;\n }\n }\n }\n if(x != 1) ret.emplace_back(x, 1);\n return ret;\n}\n\n/// @brief xの約数列挙\n/// @param x \n/// @return vll, 約数の昇順\nvll divisor_enum(ll x){\n vector<ll> ret;\n for(ll i=1; i*i<=x; i++){\n if(x%i == 0){\n ret.push_back(x/i);\n ret.push_back(i);\n }\n }\n sort(all(ret));\n UNIQUE(ret);\n return ret;\n}\n\n/// @brief 繰り返し二乗法。\n/// @tparam T \n/// @param x \n/// @param k \n/// @param op \n/// @param e \n/// @return \ntemplate<typename T>\nT pow_t(T x, ll k, T (*op)(T, T), T (*e)()){\n T ret = e();\n while(k){\n if(k&1) ret *= x;\n x *= x;\n k >>= 1;\n }\n return ret;\n}\n\nll powll(ll x, ll k){\n return pow_t<ll>(x, k, [](ll a, ll b) -> ll{return a*b;}, []() -> ll{return 1;});\n}\n\ninline int pop_cnt(ll x) { return __builtin_popcountll(x); }\ninline int top_bit(ll x) { return (x==0?-1:63-__builtin_clzll(x));}\n\nvoid main2();\n\nint main(){\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n main2();\n}\n\nbool solve2(int n, int m){\n if(n == 0) return false;\n vc<string> b(n);\n rep(i, n) cin >> b[i];\n vc<bitset<500>> b2(n, bitset<500>(0));\n rep(i, n){\n rep(j, m){\n if(b[i][j] == '1') b2[i].set(j, 1);\n } \n }\n int ans = 0;\n rep(S, 1<<n){\n bitset<500> bs(0);\n int cnt = pop_cnt(S);\n if(cnt <= ans) continue; \n rep(j, n){\n if(S>>j&1) bs ^= b2[j];\n }\n if(bs.count() == 0) ans = cnt;\n }\n cout << ans << el;\n return true;\n}\nbool solve3(int n, int m){\n vc<string> b(n);\n rep(i, n) cin >> b[i];\n vi dp(1<<m, -INF);\n vi dp2(1<<m, -INF);\n dp[0] = 0;\n rep(i, n){\n int mask = 0;\n rep(j, m) if(b[i][j] == '1') mask |= 1<<j;\n rep(S, 1<<m){\n dp2[S] = max(dp[S], dp[S^mask]+1); \n }\n swap(dp, dp2);\n }\n cout << dp[0] << el;\n return true;\n}\nbool solve(){\n int n, m;\n cin >> n >> m;\n if(n == 0) return false;\n if(n < m){\n return solve2(n, m);\n }\n else{\n return solve3(n, m);\n }\n}\n\nvoid main2(){\n while(solve()){\n ;\n }\n}", "accuracy": 1, "time_ms": 710, "memory_kb": 36212, "score_of_the_acc": -0.1964, "final_rank": 2 }, { "submission_id": "aoj_1619_10632212", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n// #ifndef ONLINE_JUDGE\n// #define _GLIBCXX_DEBUG // 配列外参照のエラー\n// #endif\n\n// スタンダードライブラリ\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型関連\nusing ll = long long;\nusing lint = long long;\nusing pll = pair<ll, ll>;\nusing plll = tuple<ll, ll, ll>;\nusing pii = pair<int, int>;\nusing piii = tuple<ll, ll, ll>;\ntemplate <class T> using prique = priority_queue<T>;\n\n// 型定数\nconstexpr ll mod = 998244353;\nconstexpr ll MOD = 1000000007;\nint INF32 = 2e9;\nll INF64 = 9e18;\n#define endl '\\n';\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\n/////////////////////////// print関連\ntemplate <typename T> void print(vector<T> A);\nvoid print();\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail);\nvoid print(string S);\ntemplate <typename T> void print(vector<vector<T>> &F);\n\ninline void Yes(bool f);\n\n///////////////////ここから//////////////////////\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0) {\n return false;\n }\n vector<string> SS;\n vector<int> B(N);\n for (int i = 0; i < N; i++) {\n string S;\n cin >> S;\n SS.push_back(S);\n int val = 0;\n for (int j = 0; j < M; j++) {\n if (S[j] == '1') {\n val += (1 << j);\n }\n }\n B[i] = val;\n }\n if (N <= 22) {\n int ans = 0;\n for (int S = 0; S < (1 << N); S++) {\n int cnt = 0;\n vector<int> V(M);\n for (int i = 0; i < N; i++) {\n if (S & (1 << i)) {\n cnt++;\n for (int j = 0; j < M; j++) {\n if (SS[i][j] == '1') {\n V[j]=1-V[j];\n }\n }\n }\n }\n bool ok = true;\n for (int j = 0; j < M; j++) {\n if (V[j]) {\n ok = false;\n break;\n }\n }\n if (ok) {\n ans=max(ans,cnt);\n }\n }\n print(ans);\n } else {\n vector<int> dp(1 << M, -1);\n dp[0] = 0;\n for (int i = 0; i < N; i++) {\n vector<int> ndp(1 << M, -1);\n for (int S = 0; S < (1 << M); S++) {\n if (dp[S] == -1) {\n continue;\n }\n ndp[S ^ B[i]] = max(ndp[S ^ B[i]], dp[S] + 1);\n ndp[S] = max(ndp[S], dp[S]);\n }\n swap(dp,ndp);\n }\n print(dp[0]);\n }\n\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n while (solve())\n ;\n}\n\n////////////////////print/////////////////////////\n// 1次元ベクトルを出力する\ntemplate <typename T> void print(vector<T> A) {\n if (A.size() == 0) {\n return;\n }\n for (size_t i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}\n\n// 2次元ベクトルを出力する\ntemplate <typename T> void print(vector<vector<T>> &F) {\n for (size_t i = 0; i < F.size(); i++) {\n for (size_t j = 0; j < F[i].size(); j++) {\n cout << F[i][j];\n if (j < F[i].size() - 1) {\n cout << ' ';\n }\n }\n cout << endl;\n }\n}\n\n// 空白行を出力するprint\nvoid print() { cout << endl; }\n\n// 数値・文字列を空白区切りで出力する. print(a,b)など\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n cout << head;\n if (sizeof...(tail) != 0)\n cout << \" \";\n print(forward<Tail>(tail)...);\n}\n\nvoid print(string S) { cout << S << endl; }\n\n//////////////出力関連\n\ninline void Yes(bool f) {\n if (f) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 4000, "memory_kb": 19832, "score_of_the_acc": -0.586, "final_rank": 5 }, { "submission_id": "aoj_1619_10630838", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n\nvector<int> myxor(vector<int> &a,vector<int> &b){\n assert(a.size()==b.size());\n vector<int> ans(a.size(),0);\n for(int i=0;i<(ll)a.size();i++){\n ans.at(i)=a.at(i)^b.at(i);\n }\n return ans;\n}\n\nbool solve(){\n int n,m; cin>>n>>m;\n if(n==0 && m == 0) return 0;\n\n vector<vector<int>> b(n,vector<int>(m));\n vector<ll> recipeint(n,0);\n\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n char c; cin>>c;\n b[i][j] = c-'0';\n }\n }\n ll ans=0;\n if(n <= 20){\n for(int i=0;i<(1<<n);i++){\n bitset<501> bt(0);\n ll siz=0;\n for(int k=0;k<n;k++){\n if(i&(1<<k)){\n siz++;\n for(int j=0;j<m;j++){\n if(b[k][j]==1)bt.flip(j);\n }\n }\n }\n if((int)bt.count()!=0)continue;\n ans=max(ans,siz);\n }\n }\n else{\n map<vector<int>,ll> mp;\n vector<pair<vector<int>,ll>> t(0);\n for(int i=0;i<n;i++){\n t.clear();\n if(mp.count(b.at(i))==0){\n t.push_back(make_pair(b.at(i),1));\n }\n for(pair<vector<int>,ll> pp:mp){\n t.push_back(make_pair(myxor(pp.first,b.at(i)),pp.second+1));\n }\n for(int i=0;i<(ll)t.size();i++){\n if(mp.count(t.at(i).first)==0){\n mp[t.at(i).first]=t.at(i).second;\n }\n else{\n mp.at(t.at(i).first)=max(mp.at(t.at(i).first),t.at(i).second);\n }\n }\n }\n if(mp.count(vector<int>(m,0))==0){\n ans=0;\n }\n else{\n ans=mp.at(vector<int>(m,0));\n }\n }\n cout<<ans<<endl;\n return 1;\n}\n\nint main(){\n while(solve());\n}", "accuracy": 1, "time_ms": 4070, "memory_kb": 249200, "score_of_the_acc": -1.5649, "final_rank": 18 }, { "submission_id": "aoj_1619_10630613", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma GCC optimize(\"Ofast\")\n\n#define rep(i, n) for(ll i = 0; i < (n); ++i)\n#define all(A) A.begin(), A.end()\n\nbool chmin(auto &a, auto b) {return a > b ? a = b, 1 : 0;}\nbool chmax(auto &a, auto b) {return a < b ? a = b, 1 : 0;}\n\nusing ll = long long;\n\nvoid print(auto... vs) {\n ((cout << vs << ' '), ...) << endl;\n}\n\nstruct dsu {\n int n;\n vector<int> par;\n\n dsu(int n) : n(n), par(n, -1){};\n\n int find(int v) {\n if(par[v] < 0) return v;\n return par[v] = find(par[v]);\n }\n\n bool merge(int x, int y) {\n x = find(x);\n y = find(y);\n if(x == y) return false;\n if(par[x] > par[y]) swap(x, y);\n par[x] += par[y];\n par[y] = x;\n return true;\n }\n\n int size(int v) { return -par[find(v)]; }\n\n bool same(int x, int y) { return find(x) == find(y); }\n};\n\nint main() {\n while(1) {\n ll N, M; cin >> N >> M;\n if(N + M == 0) break;\n vector<string> B(N);\n rep(i, N) {\n cin >> B[i];\n }\n \n ll res = 0;\n if(N <= 26) {\n // N < 25\n using bs = bitset<500>;\n vector<bs> b(N);\n rep(i, N) {\n b[i] = 0;\n rep(j, M) {\n if(B[i][j] == '1') b[i][j] = 1;\n }\n }\n // rep(i, N) {\n // print(b[i]);\n // }\n \n for(ll bit = 0; bit < (1LL<<N); ++bit) {\n bs tmp = 0;\n rep(i, N) {\n if(bit & (1LL<<i)) {\n tmp ^= b[i];\n }\n }\n // print(bit, tmp);\n if(tmp == 0) chmax(res, __builtin_popcountll(bit));\n }\n \n \n }\n else {\n // M < 25\n vector<int> b(N);\n rep(i, N) {\n ll now = 0;\n rep(j, M) {\n now *= 2;\n now += (B[i][j] - '0');\n }\n b[i] = now;\n }\n \n vector dp(N+1, vector<int>(1LL<<M, -1));\n dp[0][0] = 0;\n rep(i, N) {\n rep(bit, 1LL<<M) {\n if(dp[i][bit] >= 0) {\n chmax(dp[i+1][bit], dp[i][bit]);\n chmax(dp[i+1][bit ^ b[i]], dp[i][bit] + 1);\n }\n }\n }\n \n // rep(i, N) {\n // rep(bit, 1LL<<M) {\n // print(i, bit, dp[i][bit]);\n // }\n // }\n res = max(dp[N][0], 0);\n }\n \n cout << res << endl;\n \n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 5800, "memory_kb": 33256, "score_of_the_acc": -0.8938, "final_rank": 9 }, { "submission_id": "aoj_1619_10630551", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve1(int N, int M, vector<string> B) {\n vector<bitset<500>> bit(N);\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < M; j++) {\n if (B[i][j] == '1') bit[i].set(j);\n }\n }\n int ans = 0;\n for (int i = 0; i < 1 << N; i++) {\n bitset<500> b;\n for (int j = 0; j < N; j++) {\n if (i >> j & 1) b ^= bit[j];\n }\n if (b.count() == 0) {\n ans = max(ans, __builtin_popcount(i));\n }\n }\n cout << ans << '\\n';\n}\n\nvoid solve2(int N, int M, vector<string> B) {\n vector<int> dp(1 << M, -1 << 30);\n dp[0] = 0;\n for (int i = 0; i < N; i++) {\n auto ep = dp;\n int bit = 0;\n for (int j = 0; j < M; j++) {\n if (B[i][j] == '1') bit ^= 1 << j;\n }\n for (int j = 0; j < 1 << M; j++) {\n ep[j ^ bit] = max(ep[j ^ bit], dp[j] + 1);\n }\n dp = ep;\n }\n cout << dp[0] << '\\n';\n}\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if (N == 0 && M == 0) return false;\n vector<string> B(N);\n for (int i = 0; i < N; i++) cin >> B[i];\n if (N <= 23) solve1(N, M, B);\n else solve2(N, M, B);\n return true;\n}\n\nint main() {\n while (solve()) {}\n}", "accuracy": 1, "time_ms": 4710, "memory_kb": 12516, "score_of_the_acc": -0.6541, "final_rank": 7 }, { "submission_id": "aoj_1619_10630521", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#include <atcoder/all>\nusing namespace atcoder;\n\nusing ll = long long;\nusing mint = modint998244353;\n\nunordered_map<string, int> enumerate(vector<string> S, int M) {\n int N = S.size();\n unordered_map<string, int> ret;\n for(int s = 0; s < 1 << N; s++) {\n int cnt = 0;\n string x(M, '0');\n for(int i = 0; i < N; i++) {\n if((~s >> i) & 1) continue;\n cnt++;\n for(int j = 0; j < M; j++) {\n x[j] ^= S[i][j] - '0';\n }\n }\n ret[x] = max(ret[x], cnt);\n }\n return ret;\n}\n\nbool solve() {\n int N, M;\n cin >> N >> M;\n if(N == 0) return false;\n vector<string> S(N);\n for(int i = 0; i < N; i++) {\n cin >> S[i];\n }\n if(N <= 30) {\n vector<string> L, R;\n for(int i = 0; i < N; i++) {\n (i & 1 ? L : R).push_back(S[i]);\n }\n auto mpl = enumerate(L, M);\n auto mpr = enumerate(R, M);\n int ans = 0;\n for(auto [s, c] : mpl) {\n if(mpr.count(s)) ans = max(ans, c + mpr[s]);\n // cout << s << \" \" << c << \" \" << (mpr.count(s) ? mpr[s] : 0) << endl;\n }\n cout << ans << endl;\n }else {\n vector<int> A;\n for(auto s : S) {\n int ret = 0;\n for(char c : s) ret = 2 * ret + (c - '0');\n A.push_back(ret);\n }\n const int INF = 1 << 20;\n vector dp(N + 1, vector<int>(1 << M, -INF));\n dp[0][0] = 0;\n for(int i = 0; i < N; i++) {\n for(int j = 0; j < 1 << M; j++) {\n dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);\n dp[i + 1][j ^ A[i]] = max(dp[i + 1][j ^ A[i]], dp[i][j] + 1);\n }\n }\n cout << dp[N][0] << endl;\n }\n return true;\n}\n\nint main() {\n while(solve());\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 12740, "score_of_the_acc": -0.0009, "final_rank": 1 }, { "submission_id": "aoj_1619_10630473", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll =long long;\n\nusing BS=bitset<500>;\nvoid solve(ll H,ll W){\n vector<BS> B(H);\n for(int i=0;i<H;i++){\n string S;\n cin>>S;\n B[i]=BS(S);\n }\n unordered_map<BS,ll> M;\n M[0]=0;\n for(int i=0;i<H;i++){\n unordered_map<BS,ll> NM;\n for(auto [m,n]:M){\n NM[m]=max(NM[m],n);\n NM[m^B[i]]=max(NM[m^B[i]],n+1);\n }\n swap(NM,M);\n }\n cout<<M[0]<<endl;\n\n \n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n ll H,W;\n while(cin>>H>>W,H+W!=0)\n solve(H,W);\n}", "accuracy": 1, "time_ms": 2990, "memory_kb": 167704, "score_of_the_acc": -1.0699, "final_rank": 11 }, { "submission_id": "aoj_1619_10604073", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\n#include <stdlib.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define all(x) (x).begin(), (x).end()\n//constexpr ll MOD = 1000000007;\nconstexpr ll MOD = 998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 1LL<<60;\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\n\nll gcd(ll a, ll b){\n if(a%b == 0){\n return b;\n }else{\n return gcd(b, a%b);\n }\n}\n\nll lcm(ll a, ll b){\n return a*b / gcd(a, b);\n}\n\nll powMod(ll x, ll n) {\n if (n == 0) return 1 % MOD;\n ll val = powMod(x, n / 2);\n val *= val;\n val %= MOD;\n if (n % 2 == 1) val *= x;\n return val % MOD;\n}\n\nint main() {\n while(true){\n ll n, m; cin >> n >> m;\n if(n*m == 0) break;\n if(n < m){\n vector<vector<ll>> b(n,vector<ll>(m));\n rep(i,0,n){\n string s; cin >> s;\n rep(j,0,m) b[i][j] = s[j]-'0';\n }\n ll ans = 0;\n // rep(i,0,n){\n // rep(j,0,m) cout << b[i][j];\n // cout << endl;\n // }\n rep(bit,0,1LL<<n){\n vector<ll> num(m,0);\n rep(i,0,n){\n if(bit>>i&1){\n rep(j,0,m) num[j] ^= b[i][j];\n }\n }\n ll sum = 0;\n rep(j,0,m) sum += num[j];\n if(sum == 0){\n chmax(ans, __popcount(bit));\n // cout << \"bit:\" << bit << endl;\n }\n }\n cout << ans << endl;\n }else{\n vector<ll> b(n);\n rep(i,0,n){\n string s; cin >> s;\n ll num = 0;\n for(auto c: s){\n num *= 2;\n num += c-'0';\n }\n b[i] = num;\n }\n vector<ll> dp(1LL<<m, -INF);\n dp[0] = 0;\n rep(i,0,n){\n vector<ll> ndp(1LL << m, -INF);\n rep(j,0,1LL<<m){\n if(dp[j] == -INF) continue;\n chmax(ndp[j], dp[j]);\n chmax(ndp[j^b[i]], dp[j]+1);\n }\n swap(dp, ndp);\n }\n cout << dp[0] << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 2740, "memory_kb": 73064, "score_of_the_acc": -0.6352, "final_rank": 6 } ]
aoj_1620_cpp
Boolean Expression Compressor You are asked to build a compressor for Boolean expressions that transforms expressions to the shortest form keeping their meaning. The grammar of the Boolean expressions has terminals 0 1 a b c d - ^ * ( ) , start symbol <E> and the following production rule: <E>  ::= 0 | 1 | a | b | c | d | - <E>  | ( <E> ^ <E> ) | ( <E> * <E> ) Letters a , b , c and d represent Boolean variables that have values of either 0 or 1 . Operators are evaluated as shown in the Table below. In other words, - means negation (NOT), ^ means exclusive disjunction (XOR), and * means logical conjunction (AND). Table: Evaluations of operators Write a program that calculates the length of the shortest expression that evaluates equal to the given expression with whatever values of the four variables. For example, 0 , that is the first expression in the sample input, cannot be shortened further. Therefore the shortest length for this expression is 1. For another example, (a*(1*b)) , the second in the sample input, always evaluates equal to (a*b) and (b*a) , which are the shortest. The output for this expression, thus, should be 5 . Input The input consists of multiple datasets. A dataset consists of one line, containing an expression conforming to the grammar described above. The length of the expression is less than or equal to 16 characters. The end of the input is indicated by a line containing one ‘ . ’ (period). The number of datasets in the input is at most 200. Output For each dataset, output a single line containing an integer which is the length of the shortest expression that has the same value as the given expression for all combinations of values in the variables. Sample Input 0 (a*(1*b)) (1^a) (-(-a*-b)*a) (a^(b^(c^d))) . Output for the Sample Input 1 5 2 1 13
[ { "submission_id": "aoj_1620_10853829", "code_snippet": "#include<cstdio>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<cstring>\nusing namespace std;\n\nconst int N = 16;\n\nchar S[N + 2];\nint T[N + 2], p;\nmap<char, int> M;\nconst char *al = \"abcd\";\nconst char *nu = \"01\";\nconst char *st = \"^*\";\n\nvector<vector<char>> V[N + 2];\n\nint get(vector<char> &s)\n{\n\tif (strchr(nu, s[p]))\n\t\treturn s[p++] - '0';\n\tif (strchr(al, s[p]))\n\t\treturn M[s[p++]];\n\tif (s[p] == '-')\n\t{\n\t\tp++;\n\t\treturn 1 ^ get(s);\n\t}\n\tp++;\n\tint l = get(s);\n\tint t = p++;\n\tint r = get(s);\n\tp++;\n\tif (s[t] == '^')\n\t\treturn l^r;\n\telse\n\t\treturn l&r;\n}\n\nint get(char *s)\n{\n\tif (strchr(nu, s[p]))\n\t\treturn s[p++] - '0';\n\tif (strchr(al, s[p]))\n\t\treturn M[s[p++]];\n\tif (s[p] == '-')\n\t{\n\t\tp++;\n\t\treturn 1 ^ get(s);\n\t}\n\tp++;\n\tint l = get(s);\n\tint t = p++;\n\tint r = get(s);\n\tp++;\n\tif (s[t] == '^')\n\t\treturn l^r;\n\telse\n\t\treturn l&r;\n}\n\nvoid init(int k)\n{\n\tif (k == 1)\n\t{\n\t\tfor (int i = 0; i < 4; i++)\n\t\t\tV[k].push_back({ al[i] });\n\t\tfor (int i = 0; i < 2; i++)\n\t\t\tV[k].push_back({ nu[i] });\n\t}\n\tif (k >= 2)\n\t{\n\t\tfor (auto &t : V[k - 1])\n\t\t{\n\t\t\tV[k].push_back({ '-' });\n\t\t\tvector<char> &v = V[k].back();\n\t\t\tv.insert(v.end(), t.begin(), t.end());\n\t\t}\n\t}\n\tif (k >= 3)\n\t{\n\t\tfor (int i = 1, j = k - 3 - 1; i < k - 3; i++, j--)\n\t\t{\n\t\t\tfor (auto &t : V[i])\n\t\t\t\tfor (auto &t2 : V[j])\n\t\t\t\t{\n\t\t\t\t\tfor (int l = 0; l < 2; l++)\n\t\t\t\t\t{\n\t\t\t\t\t\tV[k].push_back({ '(' });\n\t\t\t\t\t\tvector<char> &v = V[k].back();\n\t\t\t\t\t\tv.insert(v.end(), t.begin(), t.end());\n\t\t\t\t\t\tv.push_back(st[l]);\n\t\t\t\t\t\tv.insert(v.end(), t2.begin(), t2.end());\n\t\t\t\t\t\tv.push_back(')');\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t}\n\t}\n\t//for (auto &v : V[k])\n\t//{\n\t//\tfor (auto &c : v)\n\t//\t\tprintf(\"%c\", c);\n\t//\tprintf(\"\\n\");\n\t//\tprintf(\"k=%d\\n\", k);\n\t//}\n}\n\nint main()\n{\n\tfor (int i = 1; i < N; i++)\n\t{\n\t\tinit(i);\n\t\t//printf(\"i=%d\\n\", i);\n\t}\n\tfor (; scanf(\"%s\", S) && S[0] != '.'; )\n\t{\n\t\tfor (int t = 0; t < 1 << 4; t++)\n\t\t{\n\t\t\tfor (int l = 0; l < 4; l++)\n\t\t\t\tM[al[l]] = t >> l & 1;\n\t\t\tp = 0;\n\t\t\tT[t] = get(S);\n\t\t\t//printf(\"t=%d\\n\", t);\n\t\t}\n\t\t//printf(\"A\\n\");\n\t\tint ans = 1;\n\t\tfor (; ans < 16; ans++)\n\t\t{\n\t\t\tbool ok = false;\n\t\t\tfor (auto &v : V[ans])\n\t\t\t{\n\t\t\t\tbool all = true;\n\t\t\t\tfor (int t = 0; t < 1 << 4; t++)\n\t\t\t\t{\n\t\t\t\t\tfor (int l = 0; l < 4; l++)\n\t\t\t\t\t\tM[al[l]] = t >> l & 1;\n\t\t\t\t\tp = 0;\n\t\t\t\t\tall &= T[t] == get(v);\n\t\t\t\t\tif (!all)\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (all)\n\t\t\t\t{\n\t\t\t\t\tok = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ok)\n\t\t\t\tbreak;\n\t\t\t//printf(\"nowans=%d\\n\", ans);\n\t\t}\n\t\tprintf(\"%d\\n\", ans);\n\t}\n}\n\n/*\n\n\n*/", "accuracy": 1, "time_ms": 2900, "memory_kb": 166396, "score_of_the_acc": -1.7769, "final_rank": 20 }, { "submission_id": "aoj_1620_10668668", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n// #ifndef ONLINE_JUDGE\n// #define _GLIBCXX_DEBUG // 配列外参照のエラー\n// #endif\n\n// スタンダードライブラリ\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型関連\nusing ll = long long;\nusing lint = long long;\nusing pll = pair<ll, ll>;\nusing plll = tuple<ll, ll, ll>;\nusing pii = pair<int, int>;\nusing piii = tuple<ll, ll, ll>;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vvvi = vector<vector<vector<int>>>;\nusing vvvvi = vector<vector<vector<vector<int>>>>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\nusing vvvl = vector<vector<vector<ll>>>;\nusing vvvvl = vector<vector<vector<vector<ll>>>>;\n\ntemplate <class T> using prique = priority_queue<T>;\n\n// 型定数\nconstexpr ll mod = 998244353;\nconstexpr ll MOD = 1000000007;\nint INF32 = 2e9;\nll INF64 = 9e18;\n#define endl '\\n';\n\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\n\n/////////////////////////// print関連\ntemplate <typename T> void print(vector<T> A);\nvoid print();\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail);\nvoid print(string S);\ntemplate <typename T> void print(vector<vector<T>> &F);\n\ninline void Yes(bool f);\n\n\nint get_val(string &S, vi &V) {\n int idx=0;\n auto E = [&](auto E) -> int {\n if (S[idx] == '0' || S[idx] == '1') {\n idx++;\n return S[idx-1] - '0';\n }\n if (S[idx] == 'a' || S[idx] == 'b' || S[idx] == 'c' || S[idx] == 'd') {\n idx++;\n return V[S[idx-1]-'a'];\n }\n if (S[idx] == '-') {\n idx++;\n int nval = E(E);\n return (1^nval);\n }\n idx++;\n int left = E(E);\n char op = S[idx];\n idx++;\n int right = E(E);\n idx++;\n if (op == '*') {\n return (left & right);\n }\n return (left ^ right);\n };\n\n return E(E);\n}\n\nbool is_sameST(string S, string T) {\n\n \n\n for (int B = 0; B < (1 << 4); B++) {\n vi V(4);\n for (int i = 0; i < 4; i++) {\n if (B & (1 << i)) {\n V[i]=1;\n }\n }\n if (get_val(S, V) != get_val(T, V)) {\n return false;\n }\n }\n return true;\n}\n\n///////////////////ここから//////////////////////\nbool solve(vector<vector<string>> &Logical) {\n string S;\n cin >> S;\n if (S == \".\")\n return false;\n int N = S.size();\n\n \n for (int i = 1; i < N; i++) {\n for (auto l : Logical[i]) {\n if (is_sameST(S, l)) {\n print(i);\n return true;\n }\n }\n }\n print(N);\n\n return true;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n\n int N=15;\n vector<vector<string>> Logical(N + 1);\n Logical[1] = {\"a\", \"b\", \"c\", \"d\", \"0\", \"1\"};\n\n for (int i = 2; i < N + 1; i++) {\n for (auto l : Logical[i - 1]) {\n Logical[i].push_back(\"-\" + l);\n }\n for (int j = 1; j < i - 3; j++) {\n for (auto l1 : Logical[j]) {\n for (auto l2 : Logical[i - 3 - j]) {\n Logical[i].push_back(\"(\" + l1 + \"*\" + l2 + \")\");\n Logical[i].push_back(\"(\" + l1 + \"^\" + l2 + \")\");\n }\n }\n }\n }\n while (solve(Logical))\n ;\n}\n\n////////////////////print/////////////////////////\n// 1次元ベクトルを出力する\ntemplate <typename T> void print(vector<T> A) {\n if (A.size() == 0) {\n return;\n }\n for (size_t i = 0; i < A.size() - 1; i++) {\n cout << A[i] << ' ';\n }\n cout << A[A.size() - 1] << endl;\n return;\n}\n\n// 2次元ベクトルを出力する\ntemplate <typename T> void print(vector<vector<T>> &F) {\n for (size_t i = 0; i < F.size(); i++) {\n for (size_t j = 0; j < F[i].size(); j++) {\n cout << F[i][j];\n if (j < F[i].size() - 1) {\n cout << ' ';\n }\n }\n cout << endl;\n }\n}\n\n// 空白行を出力するprint\nvoid print() { cout << endl; }\n\n// 数値・文字列を空白区切りで出力する. print(a,b)など\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n cout << head;\n if (sizeof...(tail) != 0)\n cout << \" \";\n print(forward<Tail>(tail)...);\n}\n\nvoid print(string S) { cout << S << endl; }\n\n//////////////出力関連\n\ninline void Yes(bool f) {\n if (f) {\n cout << \"Yes\" << endl;\n } else {\n cout << \"No\" << endl;\n }\n}", "accuracy": 1, "time_ms": 1370, "memory_kb": 98536, "score_of_the_acc": -0.949, "final_rank": 17 }, { "submission_id": "aoj_1620_10630583", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll =long long;\n\n\nll a=0,b=0,c=0,d=0;\nll eval(string S){\n if(S==\"a\"){\n return a;\n }\n else if(S==\"b\"){\n return b;\n }\n else if(S==\"c\"){\n return c;\n }\n else if(S==\"d\"){\n return d;\n }\n else if(S==\"0\"){\n return 0;\n }\n else if(S==\"1\"){\n return (1<<16)-1;\n }\n else if(S[0]=='-'){\n return (1<<16)-1-eval(S.substr(1,S.size()-1));\n }\n else{\n ll k=0;\n for(ll i=1;i<S.size();i++){\n if(S[i]=='(')k++;\n else if(S[i]==')')k--;\n else if(S[i]=='*'&&k==0){\n return eval(S.substr(1,i-1))&eval(S.substr(i+1,S.size()-i-2));\n }\n else if(S[i]=='^'&&k==0){\n return eval(S.substr(1,i-1))^eval(S.substr(i+1,S.size()-i-2));\n }\n }\n }\n return 0;\n}\n\nmap<ll,ll> AN;\n\n\nvoid init(){\n for(int i=0;i<16;i++){\n a*=2;\n b*=2;\n c*=2;\n d*=2;\n if(i%16<8)a++;\n if(i%8<4)b++;\n if(i%4<2)c++;\n if(i%2<1)d++;\n }\n\n priority_queue<pair<ll,string>,vector<pair<ll,string>>,greater<pair<ll,string>>> Q;\n Q.push({1,\"0\"});\n Q.push({1,\"1\"});\n Q.push({1,\"a\"});\n Q.push({1,\"b\"});\n Q.push({1,\"c\"});\n Q.push({1,\"d\"});\n // AN[eval(\"0\")]=1;\n // AN[eval(\"1\")]=1;\n // AN[eval(\"a\")]=1;\n // AN[eval(\"b\")]=1;\n // AN[eval(\"c\")]=1;\n // AN[eval(\"d\")]=1;\n set<string> seen;\n ll MX=15;\n while(!Q.empty()){\n auto [n,s]=Q.top();\n Q.pop();\n // cout<<AN.size()<<endl;\n if(seen.count(s))continue;\n seen.insert(s);\n if(AN.count(eval(s)))continue;\n AN[eval(s)]=n;\n string ns=\"-\"+s;\n if(!AN.count(eval(ns))&&ns.size()<15){\n Q.push({ns.size(),ns});\n }\n for(auto ss:seen){\n ns=\"(\"+ss+\"^\"+s+\")\";\n if(!AN.count(eval(ns))&&ns.size()<15){\n Q.push({ns.size(),ns});\n }\n ns=\"(\"+ss+\"*\"+s+\")\";\n if(!AN.count(eval(ns))&&ns.size()<15){\n Q.push({ns.size(),ns});\n }\n ns=\"(\"+s+\"^\"+ss+\")\";\n if(!AN.count(eval(ns))&&ns.size()<15){\n Q.push({ns.size(),ns});\n }\n ns=\"(\"+s+\"*\"+ss+\")\";\n if(!AN.count(eval(ns))&&ns.size()<15){\n Q.push({ns.size(),ns});\n }\n }\n }\n}\n\n\nvoid solve(string S){\n if(!AN.count(eval(S))){\n AN[eval(S)]=S.size();\n }\n cout<<AN[eval(S)]<<\"\\n\";\n}\n\n\nint main(){\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n init();\n\n string S;\n while(cin>>S,S!=\".\")solve(S);\n}", "accuracy": 1, "time_ms": 3730, "memory_kb": 4264, "score_of_the_acc": -1.0048, "final_rank": 19 }, { "submission_id": "aoj_1620_10582668", "code_snippet": "#pragma region Macros\n#include <bits/stdc++.h>\n\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\nusing lint = long long;\nusing mint = modint998244353;\nusing ull = unsigned long long;\nusing ld = long double;\nusing int128 = __int128_t;\n#define all(x) (x).begin(), (x).end()\n#define EPS 1e-8\n#define uniqv(v) v.erase(unique(all(v)), v.end())\n#define OVERLOAD_REP(_1, _2, _3, name, ...) name\n#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))\n#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)\n#define log(x) cout << x << endl\n#define logfixed(x) cout << fixed << setprecision(10) << x << endl;\n#define logy(bool) \\\n if (bool) { \\\n cout << \"Yes\" << endl; \\\n } else { \\\n cout << \"No\" << endl; \\\n }\n\nostream &operator<<(ostream &dest, __int128_t value) {\n ostream::sentry s(dest);\n if (s) {\n __uint128_t tmp = value < 0 ? -value : value;\n char buffer[128];\n char *d = end(buffer);\n do {\n --d;\n *d = \"0123456789\"[tmp % 10];\n tmp /= 10;\n } while (tmp != 0);\n if (value < 0) {\n --d;\n *d = '-';\n }\n int len = end(buffer) - d;\n if (dest.rdbuf()->sputn(d, len) != len) {\n dest.setstate(ios_base::badbit);\n }\n }\n return dest;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const vector<T> &v) {\n for (int i = 0; i < (int)v.size(); i++) {\n os << v[i] << (i + 1 != (int)v.size() ? \" \" : \"\");\n }\n return os;\n}\n\ntemplate <typename T>\nostream &operator<<(ostream &os, const set<T> &set_var) {\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end()) os << \" \";\n itr--;\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << itr->first << \" -> \" << itr->second << \"\\n\";\n }\n return os;\n}\n\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const pair<T, U> &pair_var) {\n os << \"(\" << pair_var.first << \", \" << pair_var.second << \")\";\n return os;\n}\n\nvoid out() { cout << '\\n'; }\ntemplate <class T, class... Ts>\nvoid out(const T &a, const Ts &...b) {\n cout << a;\n (cout << ... << (cout << ' ', b));\n cout << '\\n';\n}\n\ntemplate <typename T>\nistream &operator>>(istream &is, vector<T> &v) {\n for (T &in : v) is >> in;\n return is;\n}\n\ninline void in(void) { return; }\ntemplate <typename First, typename... Rest>\nvoid in(First &first, Rest &...rest) {\n cin >> first;\n in(rest...);\n return;\n}\n\ntemplate <typename T>\nbool chmax(T &a, const T &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmin(T &a, const T &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\nvector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};\nvector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};\nvector<lint> dx4 = {1, 0, -1, 0};\nvector<lint> dy4 = {0, 1, 0, -1};\n\n#pragma endregion\n\nbool operate(bool A, char c, bool B) {\n if (c == '^') return A ^ B;\n if (c == '*') return A & B;\n return 1;\n}\n\nbool eval(string s, bool a, bool b, bool c, bool d) {\n // cout << s << endl;\n int l = s.size();\n if (s == \"0\") return 0;\n if (s == \"1\") return 1;\n if (s == \"a\") return a;\n if (s == \"b\") return b;\n if (s == \"c\") return c;\n if (s == \"d\") return d;\n\n // is (E*F) or 1 or 0\n if (s[0] == '(' && s[l - 1] == ')') {\n int x = 0;\n int op;\n bool L, R;\n for (int p = 1; p < l; ++p) {\n if (s[p] == '(') x++;\n if (s[p] == ')') x--;\n if (x == 0 && s[p] != '-') {\n L = eval(s.substr(1, p), a, b, c, d);\n op = s[p + 1];\n R = eval(s.substr(p + 2, l - p - 3), a, b, c, d);\n return operate(L, op, R);\n }\n }\n assert(false);\n\n } else if (s[0] == '-') {\n return !eval(s.substr(1, l - 1), a, b, c, d);\n } else {\n assert(false);\n }\n}\n\nint main() {\n cin.tie(0)->sync_with_stdio(0);\n string s;\n\n vector<vector<string>> create(17, vector<string>());\n queue<string> que;\n\n create[1] = {\"a\", \"b\", \"c\", \"d\", \"1\", \"0\"};\n for (auto v : create[1]) {\n que.push(v);\n }\n\n auto hash = [&](string s) -> int {\n int h = 0;\n for (int bit = 0; bit < (1 << 4); ++bit) {\n int a = bit & 1, b = (bit >> 1) & 1;\n int c = (bit >> 2) & 1, d = (bit >> 3) & 1;\n h += (1 << bit) * (int)eval(s, a, b, c, d);\n }\n return h;\n };\n\n map<int, int> smallest;\n map<int, string> ans;\n for (auto v : create[1]) {\n smallest[hash(v)] = 1;\n ans[hash(v)] = v;\n }\n\n for (int size = 1; size <= 16; ++size)\n for (int pos = 0; pos < create[size].size(); ++pos) {\n string E = create[size][pos];\n // cout << E << endl;\n // -E\n if (1 + size <= 16) {\n string onew = \"-\" + E;\n if (!smallest.contains(hash(onew)) || smallest[hash(onew)] > 1 + size) {\n create[1 + size].push_back(onew);\n smallest[hash(onew)] = 1 + size;\n ans[hash(onew)] = onew;\n }\n }\n // (E op F)\n for (int i = 1; i <= size; ++i) {\n if (3 + size + i > 16) break;\n int new_size = 3 + size + i;\n for (auto F : create[i]) {\n string new1 = '(' + E + '^' + F + ')';\n string new2 = '(' + E + '*' + F + ')';\n if (!smallest.contains(hash(new1)) || smallest[hash(new1)] > new_size) {\n create[new_size].push_back(new1);\n smallest[hash(new1)] = new_size;\n ans[hash(new1)] = new1;\n }\n if (!smallest.contains(hash(new2)) || smallest[hash(new2)] > new_size) {\n create[new_size].push_back(new2);\n smallest[hash(new2)] = new_size;\n ans[hash(new2)] = new2;\n }\n }\n }\n }\n\n while (1) {\n in(s);\n if (s == \".\") break;\n\n cout << smallest[hash(s)] << endl;\n // cout << ans[hash(s)] << endl;\n }\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 4096, "score_of_the_acc": -0.0333, "final_rank": 7 }, { "submission_id": "aoj_1620_10545893", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 30;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\ntypedef string::const_iterator State;\n\nclass ParseError {};\n//プロトタイプ宣言\nll expression(State &begin);\n\nstruct node{\n\tchar ch; // charがいいときもある\n\tvector<ll> to;\n};\nvector<node>nodes;\n\nset<char> variable = {'0','1','a','b','c','d'};\n//四則演算の式を構文解析し評価結果を返す。\nll expression(State &begin){\n if(variable.count(*begin)){\n ll ret = nodes.size();\n nodes.push_back({*begin,{}});\n begin++;\n return ret;\n }\n if(*begin== '-'){\n ll ret = nodes.size();\n nodes.push_back({'-',{}});\n begin++;\n ll nxt = expression(begin);\n nodes[ret].to.push_back(nxt);\n nodes[nxt].to.push_back(ret);\n return ret;\n }else if(*begin == '('){\n begin++;\n ll before = expression(begin);\n ll ret = nodes.size();\n assert(*begin == '^' or *begin == '*');\n nodes.push_back({*begin,{}});\n begin++;\n ll after = expression(begin);\n nodes[ret].to.push_back(before);\n nodes[ret].to.push_back(after);\n nodes[before].to.push_back(ret);\n nodes[after].to.push_back(ret);\n assert(*begin == ')');\n begin++;\n return ret;\n }\n return -1;\n}\n\n// map<string,pair<ll,vector<node>>>mp;\n\nvector<ll>states(1LL<<16,INF);\n\n\nll dfs(vll&val,ll v,ll par){\n char c = nodes[v].ch;\n if(variable.count(c)){\n if(c== '0' or c == '1'){\n return c-'0';\n }else{\n return val[c-'a'];\n }\n }else if(c == '-'){\n vll tmp;\n each(p,nodes[v].to)if(p != par){\n tmp.push_back(dfs(val,p,v));\n }\n assert(sz(tmp) ==1);\n return !tmp[0];\n }else {\n assert(c == '*' or c == '^');\n vll tmp;\n each(p,nodes[v].to)if(p != par){\n tmp.push_back(dfs(val,p,v));\n }\n assert(sz(tmp) ==2);\n if(c == '*'){\n return tmp[0]&tmp[1];\n }else{\n return tmp[0]^tmp[1];\n }\n }\n}\n\nvoid solve(string s){\n nodes.clear();\n State begin = s.begin();\n ll root = expression(begin);\n assert(begin== s.end());\n vll val(4);\n // vector<ll> found(n,0);//n頂点\n\n ll nowstate = 0;\n rep(i,1<<4){\n rep(j,4){\n if(i >> j &1){\n val[j] = 1;\n }else{\n val[j] = 0;\n }\n }\n nowstate+=dfs(val,root,-1) << i;\n }\n\n // assert(states.count(nowstate));\n cout << min(states[nowstate],sz(s)) << endl;\n\n}\n \nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n vector<vector<string>> sable(15);\n sable[0].push_back(\"a\");\n sable[0].push_back(\"b\");\n sable[0].push_back(\"c\");\n sable[0].push_back(\"d\");\n sable[0].push_back(\"0\");\n sable[0].push_back(\"1\");\n\n ll sablesiz = sz(sable[0]);\n ll rem = 1LL << 16;\n rep(i,15){\n // cerr << i << endl;\n if(rem == 0)break;\n if(i != 0){\n // - をつけるだけ\n each(p,sable[i-1])if(p[0] != '-' && p != \"0\" && p != \"1\"){\n sable[i].push_back(string('-'+p));\n }\n //演算\n // (u * v)\n // (u ^ v)\n \n if(i >=3){\n for(ll j = 1;i+1-j-3 >=1;j++){\n ll k = i+1 -3-j;\n // assert(k >= 1);\n each(p,sable[j-1]){\n each(q,sable[k-1]){\n if((p == \"0\" or p == \"1\") && (q== \"0\" or q == \"1\"))continue;\n sable[i].push_back('('+p+'*'+q+')');\n sable[i].push_back('('+p+'^'+q+')');\n }\n }\n }\n }\n sablesiz += sz(sable[i]);\n }\n\n vll val(4);\n each(s,sable[i]){\n // assert(sz(s) == i+1);\n State begin = s.begin();\n ll root = expression(begin);\n // mp[s] = {root,nodes};\n // assert(begin== s.end());\n\n ll state = 0;\n\n rep(i,1<<4){\n rep(j,4){\n if(i >> j &1){\n val[j] = 1;\n }else{\n val[j] = 0;\n }\n }\n state += dfs(val,root,-1) << i;\n }\n if(states[state] == INF){\n rem--;\n }\n chmin(states[state],i+1);\n nodes.clear();\n }\n \n }\n\n // rep(i,6){\n // each(p,sable[i]){\n // cout << p << endl;\n // }\n // }\n\n\n // cerr << sablesiz << endl;\n\n\n\n\n while(true){\n Str(s);\n if(s ==\".\")break;\n solve(s);\n }\n}", "accuracy": 1, "time_ms": 2470, "memory_kb": 50368, "score_of_the_acc": -0.9491, "final_rank": 18 }, { "submission_id": "aoj_1620_10544739", "code_snippet": "#line 1 \"AOJ/ICPC2017/e.cpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#line 2 \"/opt/ei1333_s_library/math/combinatorics/montgomery-mod-int.hpp\"\n\ntemplate <uint32_t mod_, bool fast = false>\nstruct MontgomeryModInt {\n private:\n using mint = MontgomeryModInt;\n using i32 = int32_t;\n using i64 = int64_t;\n using u32 = uint32_t;\n using u64 = uint64_t;\n\n static constexpr u32 get_r() {\n u32 ret = mod_;\n for (i32 i = 0; i < 4; i++) ret *= 2 - mod_ * ret;\n return ret;\n }\n\n static constexpr u32 r = get_r();\n\n static constexpr u32 n2 = -u64(mod_) % mod_;\n\n static_assert(r * mod_ == 1, \"invalid, r * mod != 1\");\n static_assert(mod_ < (1 << 30), \"invalid, mod >= 2 ^ 30\");\n static_assert((mod_ & 1) == 1, \"invalid, mod % 2 == 0\");\n\n u32 x;\n\n public:\n MontgomeryModInt() : x{} {}\n\n MontgomeryModInt(const i64 &a)\n : x(reduce(u64(fast ? a : (a % mod() + mod())) * n2)) {}\n\n static constexpr u32 reduce(const u64 &b) {\n return u32(b >> 32) + mod() - u32((u64(u32(b) * r) * mod()) >> 32);\n }\n\n mint &operator+=(const mint &p) {\n if (i32(x += p.x - 2 * mod()) < 0) x += 2 * mod();\n return *this;\n }\n\n mint &operator-=(const mint &p) {\n if (i32(x -= p.x) < 0) x += 2 * mod();\n return *this;\n }\n\n mint &operator*=(const mint &p) {\n x = reduce(u64(x) * p.x);\n return *this;\n }\n\n mint &operator/=(const mint &p) {\n *this *= p.inv();\n return *this;\n }\n\n mint operator-() const { return mint() - *this; }\n\n mint operator+(const mint &p) const { return mint(*this) += p; }\n\n mint operator-(const mint &p) const { return mint(*this) -= p; }\n\n mint operator*(const mint &p) const { return mint(*this) *= p; }\n\n mint operator/(const mint &p) const { return mint(*this) /= p; }\n\n bool operator==(const mint &p) const {\n return (x >= mod() ? x - mod() : x) == (p.x >= mod() ? p.x - mod() : p.x);\n }\n\n bool operator!=(const mint &p) const {\n return (x >= mod() ? x - mod() : x) != (p.x >= mod() ? p.x - mod() : p.x);\n }\n\n u32 val() const {\n u32 ret = reduce(x);\n return ret >= mod() ? ret - mod() : ret;\n }\n\n mint pow(u64 n) const {\n mint ret(1), mul(*this);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n\n mint inv() const { return pow(mod() - 2); }\n\n friend ostream &operator<<(ostream &os, const mint &p) {\n return os << p.val();\n }\n\n friend istream &operator>>(istream &is, mint &a) {\n i64 t;\n is >> t;\n a = mint(t);\n return is;\n }\n\n static constexpr u32 mod() { return mod_; }\n};\n\ntemplate <uint32_t mod>\nusing modint = MontgomeryModInt<mod>;\nusing modint998244353 = modint<998244353>;\nusing modint1000000007 = modint<1000000007>;\n#line 1 \"/opt/ei1333_s_library/math/fft/superset-zeta-moebius-transform.hpp\"\n/**\n * @brief Superset Zeta/Moebius Transform (上位集合のゼータ/メビウス変換)\n */\ntemplate <typename T>\nvoid superset_zeta_transform(vector<T> &f) {\n const int n = (int)f.size();\n assert((n & (n - 1)) == 0);\n for (int i = 1; i < n; i <<= 1) {\n for (int j = 0; j < n; j += i << 1) {\n for (int k = 0; k < i; k++) {\n f[j + k] += f[j + k + i];\n }\n }\n }\n}\n\ntemplate <typename T>\nvoid superset_moebius_transform(vector<T> &f) {\n const int n = (int)f.size();\n assert((n & (n - 1)) == 0);\n for (int i = 1; i < n; i <<= 1) {\n for (int j = 0; j < n; j += i << 1) {\n for (int k = 0; k < i; k++) {\n f[j + k] -= f[j + k + i];\n }\n }\n }\n}\n#line 2 \"/opt/ei1333_s_library/math/fft/bitwise-and-convolution.hpp\"\n\n/**\n * @brief Bitwise And Convolution (Bitwise-AND畳み込み)\n */\ntemplate <typename T>\nvector<T> bitwise_and_convolution(vector<T> f, vector<T> g) {\n const int n = (int)f.size();\n assert(f.size() == g.size());\n assert((n & (n - 1)) == 0);\n superset_zeta_transform(f);\n superset_zeta_transform(g);\n for (int i = 0; i < n; i++) f[i] *= g[i];\n superset_moebius_transform(f);\n return f;\n}\n#line 1 \"/opt/ei1333_s_library/math/fft/fast-walsh-hadamard-transform.hpp\"\n/**\n * @brief Fast Walsh Hadamard Transform (高速ウォルシュアダマール変換)\n */\ntemplate <typename T>\nvoid fast_walsh_hadamard_transform(vector<T> &f, bool inv = false) {\n const int n = (int)f.size();\n assert((n & (n - 1)) == 0);\n for (int i = 1; i < n; i <<= 1) {\n for (int j = 0; j < n; j += i << 1) {\n for (int k = 0; k < i; k++) {\n T s = f[j + k], t = f[j + k + i];\n f[j + k] = s + t;\n f[j + k + i] = s - t;\n }\n }\n }\n if (inv) {\n T inv_n = T(1) / n;\n for (auto &x : f) x *= inv_n;\n }\n}\n#line 2 \"/opt/ei1333_s_library/math/fft/bitwise-xor-convolution.hpp\"\n\n/**\n * @brief Bitwise Xor Convolution (Bitwise-XOR畳み込み)\n */\ntemplate <typename T>\nvector<T> bitwise_xor_convolution(vector<T> f, vector<T> g) {\n const int n = (int)f.size();\n assert(f.size() == g.size());\n assert((n & (n - 1)) == 0);\n fast_walsh_hadamard_transform(f, false);\n fast_walsh_hadamard_transform(g, false);\n for (int i = 0; i < n; i++) f[i] *= g[i];\n fast_walsh_hadamard_transform(f, true);\n return f;\n}\n#line 7 \"AOJ/ICPC2017/e.cpp\"\nusing mint = modint998244353;\n\nint parse(string s) {\n if (s == \"0\") {\n return 0;\n }\n if (s == \"1\") {\n return (1 << 16) - 1;\n }\n if (s == \"a\") {\n int res = 0;\n for (int d = 0; d < 16; ++d) {\n if (d >> 3 & 1) {\n res += 1 << d;\n }\n }\n return res;\n }\n if (s == \"b\") {\n int res = 0;\n for (int d = 0; d < 16; ++d) {\n if (d >> 2 & 1) {\n res += 1 << d;\n }\n }\n return res;\n }\n if (s == \"c\") {\n int res = 0;\n for (int d = 0; d < 16; ++d) {\n if (d >> 1 & 1) {\n res += 1 << d;\n }\n }\n return res;\n }\n if (s == \"d\") {\n int res = 0;\n for (int d = 0; d < 16; ++d) {\n if (d >> 0 & 1) {\n res += 1 << d;\n }\n }\n return res;\n }\n if (s[0] == '-') {\n return (1 << 16) - 1 - parse(string(s.begin() + 1, s.end()));\n }\n int stack = 0;\n for (int i = 0; i < s.size(); ++i) {\n if (s[i] == '(') {\n ++stack;\n }\n if (s[i] == ')') {\n --stack;\n }\n if (stack == 1 && s[i] == '^') {\n return parse(string(s.begin() + 1, s.begin() + i)) ^\n parse(string(s.begin() + i + 1, s.end() - 1));\n }\n if (stack == 1 && s[i] == '*') {\n return parse(string(s.begin() + 1, s.begin() + i)) &\n parse(string(s.begin() + i + 1, s.end() - 1));\n }\n }\n exit(1);\n}\n\nbool solve(vector<vector<mint>> &table) {\n string s;\n cin >> s;\n if (s == \".\") {\n return false;\n }\n int n = parse(s);\n int ans = 1;\n while (table[ans][n] == 0) {\n ++ans;\n }\n cout << ans << endl;\n return true;\n}\n\nvector<vector<mint>> precalc() {\n vector<vector<mint>> res(17, vector<mint>(1 << 16));\n res[1][parse(string(\"0\"))] = 1;\n res[1][parse(string(\"1\"))] = 1;\n res[1][parse(string(\"a\"))] = 1;\n res[1][parse(string(\"b\"))] = 1;\n res[1][parse(string(\"c\"))] = 1;\n res[1][parse(string(\"d\"))] = 1;\n for (int i = 2; i < 17; ++i) {\n for (int n = 0; n < (1 << 16); ++n) {\n if (res[i - 1][n] != 0) {\n res[i][n] = 1;\n res[i][(1 << 16) - 1 - n] = 1;\n }\n }\n if (i < 5) {\n continue;\n }\n for (int j = 1; j < i - 3; ++j) {\n auto tmp = bitwise_xor_convolution(res[j], res[i - 3 - j]);\n for (int n = 0; n < (1 << 16); ++n) {\n if (tmp[n] != 0) {\n res[i][n] = 1;\n }\n }\n }\n for (int j = 1; j < i - 3; ++j) {\n auto tmp = bitwise_and_convolution(res[j], res[i - 3 - j]);\n for (int n = 0; n < (1 << 16); ++n) {\n if (tmp[n] != 0) {\n res[i][n] = 1;\n }\n }\n }\n }\n return res;\n}\n\nint main(void) {\n auto table = precalc();\n while (solve(table))\n ;\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 8204, "score_of_the_acc": -0.072, "final_rank": 10 }, { "submission_id": "aoj_1620_9625720", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef pair<ll,ll> pi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (lower_bound(ALL(A),x)-A.begin())\ntemplate<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}\ntemplate<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}\nint main(){\n vi ans(65536,1e18);\n ans[0]=ans[43690]=ans[52428]=ans[61680]=ans[65280]=ans[65535]=1;\n priority_queue<pi>Q;\n Q.emplace(pi(-1,0));\n Q.emplace(pi(-1,43690));\n Q.emplace(pi(-1,52428));\n Q.emplace(pi(-1,61680));\n Q.emplace(pi(-1,65280));\n Q.emplace(pi(-1,65535));\n set<ll>S;\n while(Q.size()){\n ll d=-Q.top().first;\n ll v=Q.top().second;\n Q.pop();\n if(d!=ans[v]||d>16)continue;\n S.insert(v);\n if(chmin(ans[65535-v],d+1))Q.emplace(pi(-d-1,65535-v));\n for(auto u:S){\n if(chmin(ans[u^v],d+ans[u]+3))Q.emplace(pi(-ans[u^v],u^v));\n if(chmin(ans[u&v],d+ans[u]+3))Q.emplace(pi(-ans[u&v],u&v));\n }\n }\n while(1){\n string S;cin>>S;\n if(S==\".\")return 0;\n ll N=S.size();\n vi par(N);\n {\n stack<ll>st;\n REP(i,N){\n if(S[i]=='(')st.emplace(i);\n if(S[i]==')')par[st.top()]=i,st.pop();\n }\n }\n ll a=0;\n function<ll(ll,ll,ll)>f=[&](ll l,ll r,ll i){\n if(r-l==1){\n if(S[l]=='1')return 1LL;\n if(S[l]=='0')return 0LL;\n return (i>>(S[l]-'a'))%2;\n }\n if(S[l]=='-')return 1-f(l+1,r,i);\n assert(S[l]=='('&&par[l]==r-1);\n ll lev=0,t=l+1;\n while(lev||(S[t]!='*'&&S[t]!='^')){\n if(S[t]=='(')lev++;\n if(S[t]==')')lev--;\n t++;\n }\n ll x=f(l+1,t,i),y=f(t+1,r-1,i);\n if(S[t]=='^')return x^y;\n return x&y;\n };\n REP(i,16)if(f(0,N,i))a+=1<<i;\n cout<<ans[a]<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 6872, "score_of_the_acc": -0.0235, "final_rank": 5 }, { "submission_id": "aoj_1620_9408035", "code_snippet": "#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <type_traits>\n#include <typeindex>\n#include <unordered_map>\n#include <unordered_set>\n\n#endif\nusing namespace std;\nusing ll = long long;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\nconstexpr int INF = 1001001001;\nconstexpr int64_t llINF = 3000000000000000000;\nconst double pi = acos(-1);\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\n\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\nll modpow(ll a, ll b, ll m) {\n a %= m;\n ll res = 1 % m;\n while (b) {\n if (b & 1) {\n res *= a;\n res %= m;\n }\n a *= a;\n a %= m;\n b >>= 1;\n }\n return res;\n}\nll modinv(ll x, ll modulo) {\n ll a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return (u >= 0 ? u % modulo : (modulo - (-u) % modulo) % modulo);\n}\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nstruct Matrix {\n int n;\n vector<vector<T>> m;\n Matrix() : Matrix(0) {}\n Matrix(int x) : Matrix(vector<vector<T>>(x, vector<T>(x, 0))) {}\n Matrix(const vector<vector<T>> &a) {\n n = a.size();\n m = a;\n }\n vector<T> &operator[](int i) { return m[i]; }\n const vector<T> &operator[](int i) const { return m[i]; }\n static Matrix identity(int x) {\n Matrix res(x);\n for (int i = 0; i < x; i++) res[i][i] = 1;\n return res;\n }\n Matrix operator+(const Matrix &a) const {\n Matrix x = (*this);\n return x += a;\n }\n Matrix operator*(const Matrix &a) const {\n Matrix x = (*this);\n return x *= a;\n }\n Matrix &operator+=(const Matrix &a) {\n Matrix res(n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n res[i][j] = m[i][j] + a[i][j];\n }\n }\n m = res.m;\n return *this;\n }\n Matrix &operator*=(const Matrix &a) {\n Matrix res(n);\n for (int i = 0; i < n; i++) {\n for (int k = 0; k < n; k++) {\n for (int j = 0; j < n; j++) {\n res[i][j] += m[i][k] * a[k][j];\n }\n }\n }\n m = res.m;\n return *this;\n }\n Matrix pow(ll b) const {\n Matrix x = *this, res = identity(n);\n while (b) {\n if (b & 1) {\n res *= x;\n }\n x *= x;\n b >>= 1;\n }\n return res;\n }\n};\nstruct UnionFind {\n vector<int> par, siz, es;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n es.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n es[i] = 0;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) {\n es[x]++;\n return false;\n }\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n es[x] += es[y] + 1;\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n int edges(int x) { return es[find(x)]; }\n};\ntemplate <class S, S (*op)(S, S), S (*e)()>\nstruct segtree {\n int siz = 1;\n vector<S> dat;\n segtree(int n) : segtree(vector<S>(n, e())) {}\n segtree(const vector<S> &a) {\n while (siz < a.size()) siz <<= 1;\n dat = vector<S>(siz << 1, e());\n for (int i = 0; i < a.size(); i++) dat[siz + i] = a[i];\n for (int i = siz - 1; i >= 1; i--) dat[i] = op(dat[2 * i], dat[2 * i + 1]);\n }\n void set(int p, S x) {\n p += siz;\n dat[p] = x;\n while (p > 0) {\n p >>= 1;\n dat[p] = op(dat[2 * p], dat[2 * p + 1]);\n }\n }\n void add(int p, S x) { set(p, get(p) + x); }\n S get(int p) { return dat[p + siz]; }\n S prod(int l, int r) {\n S vl = e(), vr = e();\n l += siz, r += siz;\n while (l < r) {\n if (l & 1) vl = op(vl, dat[l++]);\n if (r & 1) vr = op(dat[--r], vr);\n l >>= 1, r >>= 1;\n }\n return op(vl, vr);\n }\n S all_prod() { return dat[1]; }\n};\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\ntemplate <class T, T (*op)(T, T), T (*e)()>\nstruct sparsetable {\n vector<vector<T>> table;\n vector<int> logtable;\n sparsetable() = default;\n sparsetable(vector<T> v) {\n int len = 0;\n while ((1 << len) <= v.size()) len++;\n table.assign(len, vector<T>(1 << len));\n for (int i = 0; i < (int)v.size(); i++) table[0][i] = v[i];\n for (int i = 1; i < len; i++) {\n for (int j = 0; j + (1 << i) <= (1 << len); j++) {\n table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);\n }\n }\n logtable.resize(v.size() + 1);\n for (int i = 2; i < logtable.size(); i++) {\n logtable[i] = logtable[(i >> 1)] + 1;\n }\n }\n T query(int l, int r) {\n assert(l <= r);\n if (l == r) return e();\n int len = logtable[r - l];\n return op(table[len][l], table[len][r - (1 << len)]);\n }\n};\ntemplate <class T, T (*op)(T, T), T (*e)()>\nstruct disjointsparsetable {\n vector<vector<T>> table;\n vector<int> logtable;\n disjointsparsetable() = default;\n disjointsparsetable(vector<T> v) {\n int len = 0;\n while ((1 << len) <= v.size()) len++;\n table.assign(len, vector<T>(1 << len, e()));\n for (int i = 0; i < (int)v.size(); i++) table[0][i] = v[i];\n for (int i = 1; i < len; i++) {\n int shift = 1 << i;\n for (int j = 0; j < (int)v.size(); j += shift << 1) {\n int t = min(j + shift, (int)v.size());\n table[i][t - 1] = v[t - 1];\n for (int k = t - 2; k >= j; k--) table[i][k] = op(v[k], table[i][k + 1]);\n if (v.size() <= t) break;\n table[i][t] = v[t];\n int r = min(t + shift, (int)v.size());\n for (int k = t + 1; k < r; k++) table[i][k] = op(table[i][k - 1], v[k]);\n }\n }\n logtable.resize(1 << len);\n for (int i = 2; i < logtable.size(); i++) {\n logtable[i] = logtable[(i >> 1)] + 1;\n }\n }\n T query(int l, int r) {\n if (l == r) return e();\n if (l >= --r) return table[0][l];\n int len = logtable[l ^ r];\n return op(table[len][l], table[len][r]);\n };\n};\npair<int, int> lcatree_op(pair<int, int> a, pair<int, int> b) { return min(a, b); }\npair<int, int> lcatree_e() { return {1000000000, -1}; }\nstruct lca_tree {\n int n, size;\n vector<int> in, ord, depth;\n sparsetable<pair<int, int>, lcatree_op, lcatree_e> st;\n lca_tree(vector<vector<int>> g, int root = 0) : n((int)g.size()), size(log2(n) + 2), in(n), depth(n, n) {\n depth[root] = 0;\n function<void(int, int)> dfs = [&](int v, int p) {\n in[v] = (int)ord.size();\n ord.push_back(v);\n for (int u : g[v]) {\n if (u == p) continue;\n if (depth[u] > depth[v] + 1) {\n depth[u] = depth[v] + 1;\n dfs(u, v);\n ord.push_back(v);\n }\n }\n };\n dfs(root, -1);\n vector<pair<int, int>> vec((int)ord.size());\n for (int i = 0; i < (int)ord.size(); i++) {\n vec[i] = make_pair(depth[ord[i]], ord[i]);\n }\n st = vec;\n }\n int lca(int u, int v) {\n if (in[u] > in[v]) swap(u, v);\n if (u == v) return u;\n return st.query(in[u], in[v]).second;\n }\n int dist(int u, int v) {\n int l = lca(u, v);\n return depth[u] + depth[v] - 2 * depth[l];\n }\n};\nstruct auxiliary_tree : lca_tree {\n vector<vector<int>> G;\n auxiliary_tree(vector<vector<int>> &g) : lca_tree(g), G(n) {}\n pair<int, vector<int>> query(vector<int> vs, bool decending = false) {\n // decending:親から子の方向のみ辺を貼る\n assert(!vs.empty());\n sort(vs.begin(), vs.end(), [&](int a, int b) { return in[a] < in[b]; });\n int m = vs.size();\n stack<int> st;\n st.push(vs[0]);\n for (int i = 0; i < m - 1; i++) {\n int w = lca(vs[i], vs[i + 1]);\n if (w != vs[i]) {\n int l = st.top();\n st.pop();\n while (!st.empty() && depth[w] < depth[st.top()]) {\n if (!decending) G[l].push_back(st.top());\n G[st.top()].push_back(l);\n l = st.top();\n st.pop();\n }\n if (st.empty() || st.top() != w) {\n st.push(w);\n vs.push_back(w);\n }\n if (!decending) G[l].push_back(w);\n G[w].push_back(l);\n }\n st.push(vs[i + 1]);\n }\n while (st.size() > 1) {\n int x = st.top();\n st.pop();\n if (!decending) G[x].push_back(st.top());\n G[st.top()].push_back(x);\n }\n // {root,vertex_list}\n return make_pair(st.top(), vs);\n }\n void clear(vector<int> vs) {\n for (int v : vs) G[v].clear();\n }\n};\nstruct Mo {\n int n;\n vector<pair<int, int>> lr;\n\n explicit Mo(int n) : n(n) {}\n\n void add(int l, int r) { /* [l, r) */\n lr.emplace_back(l, r);\n }\n\n template <typename AL, typename AR, typename EL, typename ER, typename O>\n void build(const AL &add_left, const AR &add_right, const EL &erase_left, const ER &erase_right, const O &out) {\n int q = (int)lr.size();\n int bs = n / min<int>(n, sqrt(q));\n vector<int> ord(q);\n iota(begin(ord), end(ord), 0);\n sort(begin(ord), end(ord), [&](int a, int b) {\n int ablock = lr[a].first / bs, bblock = lr[b].first / bs;\n if (ablock != bblock) return ablock < bblock;\n return (ablock & 1) ? lr[a].second > lr[b].second : lr[a].second < lr[b].second;\n });\n int l = 0, r = 0;\n for (auto idx : ord) {\n while (l > lr[idx].first) add_left(--l);\n while (r < lr[idx].second) add_right(r++);\n while (l < lr[idx].first) erase_left(l++);\n while (r > lr[idx].second) erase_right(--r);\n out(idx);\n }\n }\n\n template <typename A, typename E, typename O>\n void build(const A &add, const E &erase, const O &out) {\n build(add, add, erase, erase, out);\n }\n};\ntemplate <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()>\nstruct lazy_segtree {\n int _n, sz = 1;\n vector<S> dat;\n vector<F> lazy;\n lazy_segtree(vector<S> a) : _n(int(a.size())) {\n while (sz < _n) sz <<= 1;\n dat.resize(sz * 2, e());\n lazy.resize(sz * 2, id());\n rep(i, _n) dat[sz + i] = a[i];\n for (int i = sz - 1; i >= 1; i--) dat[i] = op(dat[2 * i], dat[2 * i + 1]);\n }\n void eval(int k) {\n dat[k] = mapping(lazy[k], dat[k]);\n if (k < sz) {\n lazy[k * 2] = composition(lazy[k], lazy[k * 2]);\n lazy[k * 2 + 1] = composition(lazy[k], lazy[k * 2 + 1]);\n }\n lazy[k] = id();\n }\n void apply(int a, int b, F f, int k = 1, int l = 0, int r = -1) {\n eval(k);\n if (r == -1) r = sz;\n if (r <= a || b <= l) return;\n if (a <= l && r <= b) {\n lazy[k] = composition(f, lazy[k]);\n eval(k);\n return;\n }\n int m = (l + r) >> 1;\n apply(a, b, f, 2 * k, l, m);\n apply(a, b, f, 2 * k + 1, m, r);\n dat[k] = op(dat[2 * k], dat[2 * k + 1]);\n }\n S prod(int a, int b, int k = 1, int l = 0, int r = -1) {\n eval(k);\n if (r == -1) r = sz;\n if (r <= a || b <= l) return e();\n if (a <= l && r <= b) return dat[k];\n int m = (l + r) >> 1;\n S vl = prod(a, b, 2 * k, l, m);\n S vr = prod(a, b, 2 * k + 1, m, r);\n return op(vl, vr);\n }\n};\n\ntemplate <class S, S (*op)(S, S), S (*e)()>\nstruct dual_segtree {\n int sz = 1, log = 0;\n vector<S> lz;\n dual_segtree(vector<S> a) {\n int n = a.size();\n while (sz < n) {\n sz <<= 1;\n log++;\n }\n lz.assign(sz << 1, e());\n for (int i = 0; i < n; i++) lz[i + sz] = a[i];\n }\n void push(int k) {\n int b = __builtin_ctz(k);\n for (int d = log; d > b; d--) {\n lz[k >> d << 1] = op(lz[k >> d << 1], lz[k >> d]);\n lz[k >> d << 1 | 1] = op(lz[k >> d << 1 | 1], lz[k >> d]);\n lz[k >> d] = e();\n }\n }\n void apply(int l, int r, S x) {\n l += sz, r += sz;\n push(l);\n push(r);\n while (l < r) {\n if (l & 1) {\n lz[l] = op(lz[l], x);\n l++;\n }\n if (r & 1) {\n r--;\n lz[r] = op(lz[r], x);\n }\n l >>= 1, r >>= 1;\n }\n }\n S get(int k) {\n k += sz;\n S res = e();\n while (k) {\n res = op(res, lz[k]);\n k >>= 1;\n }\n return res;\n }\n};\n\n/*\n#include <atcoder/string>\nint string_period(string s) {\n int n = s.size();\n auto z = atcoder::z_algorithm(s);\n for (int i = 1; i < n; i++) {\n if (n % i == 0 && z[i] == n - i) return i;\n }\n return n;\n}\n*/\nstruct LowLink {\n vector<vector<int>> g;\n vector<int> ord, low, out;\n vector<bool> used;\n vector<pair<int, int>> bridge;\n vector<pair<int, int>> articulation;\n int unions;\n LowLink(vector<vector<int>> g) : g(g) {\n int n = (int)g.size();\n ord.resize(n);\n low.resize(n);\n out.resize(n);\n used.resize(n);\n unions = 0;\n int t = 0;\n for (int i = 0; i < n; i++) {\n if (!used[i]) {\n dfs(i, t, -1);\n unions++;\n }\n }\n }\n void dfs(int v, int &t, int par) {\n used[v] = true;\n ord[v] = t++, low[v] = ord[v];\n int cnt = 0;\n bool par_back = false;\n for (int to : g[v]) {\n if (!used[to]) {\n dfs(to, t, v);\n low[v] = min(low[v], low[to]);\n if (ord[v] < low[to]) bridge.push_back(minmax(v, to));\n if (ord[v] <= low[to]) cnt++;\n } else if (to != par || par_back) {\n low[v] = min(low[v], ord[to]);\n } else\n par_back = true;\n }\n if (par != -1) cnt++;\n if (cnt >= 2) articulation.push_back({v, cnt});\n out[v] = t;\n }\n};\ntemplate <typename T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n if constexpr (is_integral_v<T>) {\n uniform_int_distribution<T> dist(l, r);\n return dist(mt);\n } else if constexpr (is_floating_point_v<T>) {\n uniform_real_distribution<T> dist(l, r);\n return dist(mt);\n }\n}\nint res[1 << 16];\nchar Minus(char c) {\n if (c == '0') return '1';\n return '0';\n}\nchar Xor(char a, char b) {\n if (a == b) return '0';\n return '1';\n}\nchar And(char a, char b) {\n if (a == '1' && b == '1') return '1';\n return '0';\n}\nint calc_formula(string s) {\n string t;\n for (char c : s) {\n t += c;\n while (1) {\n if (t.size() >= 2 && t.end()[-2] == '-' && isdigit(t.back())) {\n char d = Minus(t.back());\n t.pop_back();\n t.pop_back();\n t += d;\n continue;\n }\n if (t.back() == ')') {\n char d = '.';\n if (t.end()[-3] == '^')\n d = Xor(t.end()[-4], t.end()[-2]);\n else\n d = And(t.end()[-4], t.end()[-2]);\n t.pop_back();\n t.pop_back();\n t.pop_back();\n t.pop_back();\n t.pop_back();\n t.push_back(d);\n continue;\n }\n break;\n }\n }\n return (t[0] - '0');\n}\nint calc_charFormula(string &s) {\n int ans = 0;\n rep(i, 16) {\n string t = s;\n for (char &c : t) {\n rep(j, 4) {\n if (c == 'a' + j) c = '0' + ((i >> j) & 1);\n }\n }\n ans |= calc_formula(t) << i;\n }\n return ans;\n}\nvoid precalc() {\n fill(res, res + 65536, INF);\n vector<vector<string>> E(17);\n E[1] = {\"0\", \"1\", \"a\", \"b\", \"c\", \"d\"};\n for (auto s : E[1]) {\n res[calc_charFormula(s)] = 1;\n }\n for (int i = 2; i <= 16; i++) {\n vector<string> tmp;\n for (auto s : E[i - 1]) {\n tmp.push_back('-' + s);\n }\n for (int j = 1; j + 3 < i; j++) {\n int k = i - 3 - j;\n for (auto s : E[j]) {\n for (auto t : E[k]) {\n tmp.push_back('(' + s + '^' + t + ')');\n tmp.push_back('(' + s + '*' + t + ')');\n }\n }\n }\n for (auto s : tmp) {\n int ans = calc_charFormula(s);\n if (chmin(res[ans], s.size())) E[i].push_back(s);\n }\n }\n}\nvoid solve() {\n string s;\n cin >> s;\n if (s == \".\") exit(0);\n cout << res[calc_charFormula(s)] << endl;\n // calc for 2^4 patterns\n // generate all possible string and calc for 2^4 patterns\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n /*int t;\n cin >> t;\n while (t--) */\n precalc();\n while (1) solve();\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 4372, "score_of_the_acc": -0.0162, "final_rank": 4 }, { "submission_id": "aoj_1620_9369749", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\ntemplate <class T>\nusing vc = vector<T>;\ntemplate <class T>\nusing vvc = vc<vc<T>>;\nvoid solve();\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n ll t = 1;\n // cin >> t;\n for (int i = 1; i <= t; i++) solve();\n return 0;\n}\n#define rep(i, a, b) for (ll i = (a); i < (b); i++)\nll dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};\n// ll dy[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};\nstring s, t;\nll pos;\nvoid solve() {\n ll ABCD[4];\n auto get = [&](char c) -> ll {\n if (c == 'a') return ABCD[0];\n if (c == 'b') return ABCD[1];\n if (c == 'c') return ABCD[2];\n if (c == 'd') return ABCD[3];\n return 0;\n };\n vc<ll> min_op(1 << 16, 1e9);\n auto E = [&](auto &&E, string &S) -> ll {\n if (S[pos] == '(') {\n pos++;\n ll v1 = E(E, S);\n char op = S[pos++];\n ll v2 = E(E, S);\n pos++;\n if (op == '^') return v1 ^ v2;\n return v1 & v2;\n }\n if (S[pos] == '0' || S[pos] == '1') {\n pos++;\n return S[pos - 1] - '0';\n }\n if ('a' <= S[pos] && S[pos] <= 'd') {\n ll v = get(S[pos]);\n pos++;\n return v;\n }\n pos++;\n ll v1 = E(E, S);\n return 1 - v1;\n };\n auto check2 = [&]() -> void {\n ll num = 0;\n rep(a, 0, 2) {\n rep(b, 0, 2) {\n rep(c, 0, 2) {\n rep(d, 0, 2) {\n ABCD[0] = a, ABCD[1] = b, ABCD[2] = c, ABCD[3] = d;\n pos = 0;\n ll val = a * 8 + b * 4 + c * 2 + d;\n num |= (1 << val) * E(E, t);\n }\n }\n }\n }\n min_op[num] = min(min_op[num], (ll)t.size());\n };\n auto check = [&](vc<char> &C) -> void {\n ll nC = C.size();\n if (nC >= 5) return;\n if (nC == 0) return;\n auto add1 = [&](bool f) -> void {\n if (f) t += \"-\";\n t += \"(\";\n };\n auto add2 = [&](bool f, int idx) -> void {\n if (f) t += \"-\";\n t += C[idx];\n };\n auto add_op = [&](bool f) -> void {\n if (f)\n t += \"^\";\n else\n t += \"*\";\n };\n if (nC == 1) {\n rep(m2, 0, 2) {\n t = \"\";\n add2(m2, 0);\n check2();\n }\n return;\n }\n if (nC == 2) {\n rep(m1, 0, 2) {\n rep(m2, 0, 4) {\n rep(op, 0, 2) {\n t = \"\";\n add1(m1);\n add2(m2 & 1, 0);\n add_op(op);\n add2(m2 & 2, 1);\n t += \")\";\n check2();\n }\n }\n }\n return;\n }\n if (nC == 3) {\n rep(m1, 0, 4) {\n rep(m2, 0, 8) {\n rep(op, 0, 4) {\n t = \"\";\n add1(m1 & 1);\n add1(m1 & 2);\n add2(m2 & 1, 0);\n add_op(op & 1);\n add2(m2 & 2, 1);\n t += \")\";\n add_op(op & 2);\n add2(m2 & 4, 2);\n t += \")\";\n check2();\n }\n }\n }\n return;\n }\n rep(m1, 0, 8) {\n rep(m2, 0, 16) {\n rep(op, 0, 8) {\n t = \"\";\n add1(m1 & 1);\n add1(m1 & 2);\n add1(m1 & 4);\n add2(m2 & 1, 0);\n add_op(op & 1);\n add2(m2 & 2, 1);\n t += \")\";\n add_op(op & 2);\n add2(m2 & 4, 2);\n t += \")\";\n add_op(op & 4);\n add2(m2 & 8, 3);\n t += \")\";\n check2();\n }\n }\n }\n rep(m1, 0, 8) {\n rep(m2, 0, 16) {\n rep(op, 0, 8) {\n t = \"\";\n add1(m1 & 1);\n add1(m1 & 2);\n add2(m2 & 1, 0);\n add_op(op & 1);\n add2(m2 & 2, 1);\n t += \")\";\n add_op(op & 2);\n add1(m1 & 4);\n add2(m2 & 4, 2);\n add_op(op & 4);\n add2(m2 & 8, 3);\n t += \")\";\n t += \")\";\n check2();\n }\n }\n }\n return;\n };\n auto dfs = [&](auto &&dfs, vc<char> &C) -> void {\n if (C.size() >= 5) return;\n check(C);\n for (char c = 'a'; c <= 'd'; c++) {\n C.push_back(c);\n dfs(dfs, C);\n C.pop_back();\n }\n };\n vc<char> C;\n dfs(dfs, C);\n min_op[0] = 1, min_op[65535] = 1;\n while (true) {\n string s;\n cin >> s;\n if (s == \".\") break;\n ll num = 0;\n rep(a, 0, 2) rep(b, 0, 2) rep(c, 0, 2) rep(d, 0, 2) {\n ABCD[0] = a, ABCD[1] = b, ABCD[2] = c, ABCD[3] = d;\n pos = 0;\n ll val = a * 8 + b * 4 + c * 2 + d;\n num |= (1 << val) * E(E, s);\n }\n cout << min_op[num] << endl;\n }\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 3484, "score_of_the_acc": -0.0538, "final_rank": 9 }, { "submission_id": "aoj_1620_9232294", "code_snippet": "#include <bits/stdc++.h>\n\nconst int N{1 << 16};\nint dist[N];\n\nvoid init() {\n const int INF{(int)2e9};\n std::fill(dist, dist + N, INF);\n using qt = std::pair<int, int>;\n std::priority_queue<qt, std::vector<qt>, std::greater<qt>> que;\n dist[0] = 1; // 0\n que.emplace(dist[0], 0);\n dist[N - 1] = 1; // 1\n que.emplace(dist[1], N - 1);\n for (int i{} ; i < 4 ; i++) { // a, b, c, d\n int val{};\n for (int bit{} ; bit < (1 << 4) ; bit++) {\n if (bit & (1 << i)) val |= (1 << bit);\n }\n dist[val] = 1;\n que.emplace(dist[val], val);\n }\n while (que.size()) {\n auto [d, v]{que.top()};\n que.pop();\n if (dist[v] < d) continue;\n {// -\n int x{(N - 1) ^ v};\n if (dist[x] > dist[v] + 1) {\n dist[x] = dist[v] + 1;\n que.emplace(dist[x], x);\n }\n }\n for (int i{} ; i < N ; i++) {\n if (dist[i] + dist[v] + 3 > 16) continue;\n {\n int x{i ^ v};\n if (dist[x] > dist[i] + dist[v] + 3) {\n dist[x] = dist[i] + dist[v] + 3;\n que.emplace(dist[x], x);\n }\n }\n {\n int x{i & v};\n if (dist[x] > dist[i] + dist[v] + 3) {\n dist[x] = dist[i] + dist[v] + 3;\n que.emplace(dist[x], x);\n }\n }\n }\n }\n}\n\nint parse(const std::string& S) {\n int res{};\n for (int bit{} ; bit < (1 << 4) ; bit++) {\n int it{};\n auto f{[&](auto f) -> int {\n if (S[it] == '(') {\n it++;\n int front{f(f)};\n assert(S[it] == '*' or S[it] == '^');\n char op{S[it]};\n it++;\n int back{f(f)};\n assert(S[it] == ')');\n it++;\n return op == '*' ? (front & back) : (front ^ back);\n }\n else if (S[it] == '-') {\n it++;\n int val{f(f)};\n return 1 - val;\n }\n else {\n char c{S[it]};\n it++;\n if (c == '0') return 0;\n if (c == '1') return 1;\n if (c == 'a') return (bool)(bit & (1 << 0));\n if (c == 'b') return (bool)(bit & (1 << 1));\n if (c == 'c') return (bool)(bit & (1 << 2));\n if (c == 'd') return (bool)(bit & (1 << 3));\n assert(false);\n return -1;\n }\n }};\n if (f(f)) res |= (1 << bit);\n }\n return res;\n}\n\nbool solve() {\n std::string S;\n std::cin >> S;\n if (S == \".\") return false;\n std::cout << dist[parse(S)] << '\\n';\n return true;\n}\n\nint main() {\n init();\n std::cin.tie(nullptr)->sync_with_stdio(false);\n while (solve()) ;\n}", "accuracy": 1, "time_ms": 100, "memory_kb": 3584, "score_of_the_acc": -0.0248, "final_rank": 6 }, { "submission_id": "aoj_1620_9004236", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\nbool solve(const string& s, int& i, int x) {\n if(s[i] == '0') { i++; return 0; }\n if(s[i] == '1') { i++; return 1; }\n if(s[i] == 'a') { i++; return x & (1 << 0); }\n if(s[i] == 'b') { i++; return x & (1 << 1); }\n if(s[i] == 'c') { i++; return x & (1 << 2); }\n if(s[i] == 'd') { i++; return x & (1 << 3); }\n if(s[i] == '-') { i++; return !solve(s, i, x); }\n if(s[i] == '(') {\n i++;\n bool L = solve(s, i, x);\n assert(s[i] == '^' or s[i] == '*');\n char op = s[i];\n i++;\n bool R = solve(s, i, x);\n assert(s[i] == ')');\n i++;\n if(op == '^') {\n return L ^ R;\n } else {\n return L & R;\n }\n }\n assert(0);\n}\n\nset<string> st;\nmap<vector<int>, int> mp;\nvoid dfs(string s) {\n if(s.size() <= 16 and !st.count(s)) {\n\n vector<int> f(1 << 4, 0);\n for(int x : rep(1 << 4)) {\n int i = 0;\n f[x] = solve(s, i, x);\n }\n if(!mp.count(f) or mp[f] > s.size()) {\n mp[f] = s.size();\n st.insert(s);\n dfs(\"-\" + s);\n for(string t : st) {\n if(1 + s.size() + 1 + t.size() + 1 <= 16) {\n dfs(\"(\" + s + \"^\" + t + \")\");\n dfs(\"(\" + t + \"^\" + s + \")\");\n dfs(\"(\" + s + \"*\" + t + \")\");\n dfs(\"(\" + t + \"*\" + s + \")\");\n }\n }\n }\n }\n}\n\nint main() {\n dfs(\"0\");\n dfs(\"1\");\n dfs(\"a\");\n dfs(\"b\");\n dfs(\"c\");\n dfs(\"d\");\n\n for(string s : st) {\n vector<int> f(1 << 4, 0);\n for(int x : rep(1 << 4)) {\n int i = 0;\n f[x] = solve(s, i, x);\n }\n if(!mp.count(f)) {\n mp[f] = s.size();\n } else {\n chmin(mp[f], (int)s.size());\n }\n }\n\n while(true) {\n string s = in();\n if(s == \".\") return 0;\n\n vector<int> f(1 << 4, 0);\n for(int x : rep(1 << 4)) {\n int i = 0;\n f[x] = solve(s, i, x);\n }\n print(mp[f]);\n }\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 4120, "score_of_the_acc": -0.0389, "final_rank": 8 }, { "submission_id": "aoj_1620_8393693", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst vector<int> base = { 0x0000, 0xffff, 0xaaaa, 0xcccc, 0xf0f0, 0xff00 };\n\nint parse(const string& s, int &pos) {\n\tif (s[pos] == '0' || s[pos] == '1' || s[pos] == 'a' || s[pos] == 'b' || s[pos] == 'c' || s[pos] == 'd') {\n\t\tint res = base[s[pos] == '0' ? 0 : s[pos] == '1' ? 1 : s[pos] == 'a' ? 2 : s[pos] == 'b' ? 3 : s[pos] == 'c' ? 4 : 5];\n\t\tpos += 1;\n\t\treturn res;\n\t}\n\tif (s[pos] == '-') {\n\t\tpos += 1;\n\t\treturn 65535 - parse(s, pos);\n\t}\n\tpos += 1;\n\tint lc = parse(s, pos);\n\tchar ch = s[pos];\n\tpos += 1;\n\tint rc = parse(s, pos);\n\tpos += 1;\n\treturn (ch == '^' ? lc ^ rc : lc & rc);\n}\n\nint main() {\n\t// step #1. precompute distances\n\tvector<int> dist(65536, -1);\n\tvector<vector<int> > group(2);\n\tint cnt = base.size();\n\tgroup[1] = base;\n\tfor (int i : base) {\n\t\tdist[i] = 1;\n\t}\n\tint d = 1, x = 0;\n\twhile (cnt < 65536) {\n\t\td += 1;\n\t\tgroup.push_back(vector<int>());\n\t\tfor (int i : group[d - 1]) {\n\t\t\tif (dist[65535 - i] == -1) {\n\t\t\t\tdist[65535 - i] = d;\n\t\t\t\tgroup[d].push_back(65535 - i);\n\t\t\t\tcnt += 1;\n\t\t\t}\n\t\t}\n\t\tif (d >= 5) {\n\t\t\tfor (int i = 1; i <= d - 4; i++) {\n\t\t\t\tfor (int j : group[i]) {\n\t\t\t\t\tfor (int k : group[(d - 3) - i]) {\n\t\t\t\t\t\tx += 1;\n\t\t\t\t\t\tif (dist[j ^ k] == -1) {\n\t\t\t\t\t\t\tdist[j ^ k] = d;\n\t\t\t\t\t\t\tgroup[d].push_back(j ^ k);\n\t\t\t\t\t\t\tcnt += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (dist[j & k] == -1) {\n\t\t\t\t\t\t\tdist[j & k] = d;\n\t\t\t\t\t\t\tgroup[d].push_back(j & k);\n\t\t\t\t\t\t\tcnt += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// step #2. solve\n\tcin.tie(0);\n\tios::sync_with_stdio(false);\n\twhile (true) {\n\t\tstring s;\n\t\tcin >> s;\n\t\tif (s == \".\") {\n\t\t\tbreak;\n\t\t}\n\t\tint pos = 0;\n\t\tint bit = parse(s, pos);\n\t\tcout << dist[bit] << '\\n';\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3756, "score_of_the_acc": -0.0044, "final_rank": 2 }, { "submission_id": "aoj_1620_8027079", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n\nstruct Parser {\n string S;\n int idx;\n int val;\n Parser(string s) : S(s), idx(0){};\n\n char peek() { return S[idx]; }\n char read() { return S[idx++]; }\n bool consume(char c) {\n if (peek() == c) {\n read();\n return true;\n }\n return false;\n }\n bool eos() { return idx == (int)S.size(); }\n\n int solve(int v) {\n val = v;\n idx = 0;\n return E();\n }\n\n int E() {\n if (consume('-')) {\n return 1 - E();\n }\n if (consume('(')) {\n int a = E();\n char op = read();\n int b = E();\n consume(')');\n if (op == '^') {\n if (a != b) return 1;\n return 0;\n } else {\n if (a == 1 && b == 1) return 1;\n return 0;\n }\n }\n char a = read();\n if (a == '0') return 0;\n if (a == '1') return 1;\n if (a == 'a') return ((val >> 0) & 1);\n if (a == 'b') return ((val >> 1) & 1);\n if (a == 'c') return ((val >> 2) & 1);\n if (a == 'd') return ((val >> 3) & 1);\n exit(1);\n }\n};\n\nint res(string s) {\n int re = 0;\n Parser ps(s);\n rep(v, 1 << 4) {\n re *= 2;\n ll a = ps.solve(v);\n re += a;\n if (!(a == 0 || a == 1)) {\n cout << \"WA\" << endl;\n }\n }\n return re;\n}\n\nstruct Pre {\n vector<int> dp;\n\n Pre() {\n int inf = 10000;\n dp.resize(1 << 16, inf);\n vector<int> update(1 << 16, inf);\n set<int> ok;\n auto add = [&](int key, int val) {\n if (dp[key] != inf) return;\n update[key] = min(update[key], val);\n };\n // 更新するとき気を付ける\n\n auto nxtmns = [&]() {\n for (auto key : ok) {\n // add(((1 << 16) - 1) ^ key, dp[key] + 1);\n int u = 0;\n rep(i, 16) {\n if (!((key >> i) & 1)) {\n u += (1 << i);\n }\n }\n add(u, dp[key] + 1);\n }\n };\n\n auto nxt = [&]() {\n for (auto k1 : ok) {\n for (auto k2 : ok) {\n if (3 + dp[k1] + dp[k2] > 18) continue;\n add(k1 ^ k2, 3 + dp[k1] + dp[k2]);\n add(k1 & k2, 3 + dp[k1] + dp[k2]);\n }\n }\n };\n\n auto ud = [&](int v) {\n rep(i, 1 << 16) if (update[i] == v) {\n dp[i] = update[i];\n ok.insert(i);\n update[i] = inf;\n }\n };\n\n add(res(\"0\"), 1);\n add(res(\"1\"), 1);\n add(res(\"a\"), 1);\n add(res(\"b\"), 1);\n add(res(\"c\"), 1);\n add(res(\"d\"), 1);\n\n for (int v = 1; v <= 16; v++) {\n ud(v);\n nxtmns();\n ud(v);\n nxt();\n }\n ud(17);\n\n // cout << \"=====\" << endl;\n // rep(i, 1 << 16) cout << i << \" \" << dp[i] << endl;\n // cout << \"=====\" << endl;\n }\n} pre;\n\nbool solve() {\n string S;\n cin >> S;\n if (S == \".\") return false;\n\n int ans = min((int)S.size(), pre.dp[res(S)]);\n if ((int)S.size() < pre.dp[res(S)]) {\n cout << \"WA!! \" << S << \" \" << pre.dp[res(S)] << endl;\n exit(1);\n }\n cout << ans << endl;\n\n return true;\n}\n\nint main() {\n auto st = [&](int s) {\n string res;\n while (s > 0) {\n res += '0' + s % 2;\n s /= 2;\n }\n return res;\n };\n auto nxtmns = [&](int key) {\n int u = 0;\n rep(i, 16) {\n if (!((key >> i) & 1)) {\n u += (1 << i);\n }\n }\n return u;\n };\n\n // cout << \"a : \" << pre.dp[res(\"a\")] << endl;\n // cout << \"-a : \" << pre.dp[res(\"-a\")] << endl;\n // cout << \"nxtmnsval \" << st(nxtmns(res(\"a\"))) << endl;\n // cout << \"res( a) : \" << st(res(\"a\")) << endl;\n // cout << \"res(-a) : \" << st(res(\"-a\")) << endl;\n auto out = [&](string s) { cout << s << \" \" << pre.dp[res(s)] << endl; };\n // out(\"(c*d)\");\n // out(\"((c*d)*a)\");\n // out(\"-((c*d)*a)\");\n // out(\"--a\");\n // out(\"(-((c*d)*a)^a)\");\n // out(\"(a^b)\");\n while (solve())\n ;\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3496, "score_of_the_acc": -0.0108, "final_rank": 3 }, { "submission_id": "aoj_1620_8016523", "code_snippet": "#include<bits/stdc++.h>\n#define rng(i,a,b) for(int i=(a);i<(b);i++)\n#define rep(i,n) rng(i,0,(n))\n#define vec vector\n#define pb emplace_back\n#define all(a) (a).begin(),(a).end()\nusing ll = long long;\nusing ld = long double;\nusing namespace std;\n\nsigned main() {\n\n struct st {\n string s;\n int val;\n };\n\n //1\n vec<st>e;\n int one = 0, two = 0, thr = 0;\n rep(z, 4) {\n st tmp;\n tmp.s = 'a' + z;\n tmp.val = 0;\n rep(i, 2)rep(j, 2)rep(x, 2)rep(y, 2) {\n if (z == 0 && i == 1 || z == 1 && j == 1 || z == 2 && x == 1 || z == 3 && y == 1) {\n int keta = (i << 3) | (j << 2) | (x << 1) | y;\n tmp.val |= (1 << keta);\n }\n }\n e.pb(tmp);\n tmp.s = \"-\";\n tmp.s += 'a' + z;\n tmp.val = 0;\n rep(i, 2)rep(j, 2)rep(x, 2)rep(y, 2) {\n if (z == 0 && i == 0 || z == 1 && j == 0 || z == 2 && x == 0 || z == 3 && y == 0) {\n int keta = (i << 3) | (j << 2) | (x << 1) | y;\n tmp.val |= (1 << keta);\n }\n }\n e.pb(tmp);\n one += 2;\n }\n\n //2 = 1*1\n rep(i, one)rng(j, i + 1, one) {\n rep(k, 2)rep(r, 2) {\n st tmp;\n tmp.s = \"(\";\n tmp.s += ((k == 0) ? \"\" : \"-\") + e[i].s + \"^\" + ((r == 0) ? \"\" : \"-\") + e[j].s + \")\";\n tmp.val = (k == 0 ? e[i].val : (((1 << 16) - 1) ^ e[i].val)) ^ (r == 0 ? e[j].val : (((1 << 16) - 1) ^ e[j].val));\n e.pb(tmp);\n tmp;\n tmp.s = \"(\";\n tmp.s += ((k == 0) ? \"\" : \"-\") + e[i].s + \"*\" + ((r == 0) ? \"\" : \"-\") + e[j].s + \")\";\n tmp.val = (k == 0 ? e[i].val : (((1 << 16) - 1) ^ e[i].val)) & (r == 0 ? e[j].val : (((1 << 16) - 1) ^ e[j].val));\n e.pb(tmp);\n two += 2;\n }\n }\n\n //3=1*2\n rep(i, one)rng(j, one, one + two) {\n rep(k, 2)rep(r, 2) {\n st tmp;\n tmp.s = \"(\";\n tmp.s += ((k == 0) ? \"\" : \"-\") + e[i].s + \"^\" + ((r == 0) ? \"\" : \"-\") + e[j].s + \")\";\n tmp.val = (k == 0 ? e[i].val : (((1 << 16) - 1) ^ e[i].val)) ^ (r == 0 ? e[j].val : (((1 << 16) - 1) ^ e[j].val));\n e.pb(tmp);\n tmp.s = \"(\";\n tmp.s += ((k == 0) ? \"\" : \"-\") + e[i].s + \"*\" + ((r == 0) ? \"\" : \"-\") + e[j].s + \")\";\n tmp.val = (k == 0 ? e[i].val : (((1 << 16) - 1) ^ e[i].val)) & (r == 0 ? e[j].val : (((1 << 16) - 1) ^ e[j].val));\n e.pb(tmp);\n thr += 2;\n }\n }\n\n //3=2*2\n rng(i, one, one + two)rng(j, i + 1, one + two) {\n rep(k, 2)rep(r, 2) {\n st tmp;\n tmp.s = \"(\";\n tmp.s += ((k == 0) ? \"\" : \"-\") + e[i].s + \"^\" + ((r == 0) ? \"\" : \"-\") + e[j].s + \")\";\n tmp.val = (k == 0 ? e[i].val : (((1 << 16) - 1) ^ e[i].val)) ^ (r == 0 ? e[j].val : (((1 << 16) - 1) ^ e[j].val));\n if (tmp.s.size() <= 16)e.pb(tmp);\n tmp.s = \"(\";\n tmp.s += ((k == 0) ? \"\" : \"-\") + e[i].s + \"*\" + ((r == 0) ? \"\" : \"-\") + e[j].s + \")\";\n tmp.val = (k == 0 ? e[i].val : (((1 << 16) - 1) ^ e[i].val)) & (r == 0 ? e[j].val : (((1 << 16) - 1) ^ e[j].val));\n if (tmp.s.size() <= 16)e.pb(tmp);\n }\n }\n\n //4=(1,2)*3\n rep(i, one + two)rng(j, one + two, one + two + thr) {\n rep(k, 2)rep(r, 2) {\n st tmp;\n tmp.s = \"(\";\n tmp.s += ((k == 0) ? \"\" : \"-\") + e[i].s + \"^\" + ((r == 0) ? \"\" : \"-\") + e[j].s + \")\";\n tmp.val = (k == 0 ? e[i].val : (((1 << 16) - 1) ^ e[i].val)) ^ (r == 0 ? e[j].val : (((1 << 16) - 1) ^ e[j].val));\n if (tmp.s.size() <= 16)e.pb(tmp);\n tmp.s = \"(\";\n tmp.s += ((k == 0) ? \"\" : \"-\") + e[i].s + \"*\" + ((r == 0) ? \"\" : \"-\") + e[j].s + \")\";\n tmp.val = (k == 0 ? e[i].val : (((1 << 16) - 1) ^ e[i].val)) & (r == 0 ? e[j].val : (((1 << 16) - 1) ^ e[j].val));\n if (tmp.s.size() <= 16)e.pb(tmp);\n }\n }\n string s;\n while (cin >> s && s != \".\") {\n int val = 0;\n vec<int>kakko(s.size(), -1);\n stack<int>stc;\n rep(i, s.size()) {\n if (s[i] == '(') {\n stc.push(i);\n }\n else if (s[i] == ')') {\n int t = stc.top();stc.pop();\n kakko[t] = i;\n }\n }\n rep(i, 2)rep(j, 2)rep(x, 2)rep(y, 2) {\n auto dfs = [&](int l, int r, auto& dfs) ->bool {\n int cnt = 0;\n vec<bool>v;\n int op = -1;\n for (int z = l;z < r;z++) {\n if (s[z] == '(') {\n bool ret = dfs(z + 1, kakko[z], dfs);\n if (cnt % 2 == 1) ret = !ret;\n cnt = 0;\n v.pb(ret);\n z = kakko[z];\n }\n else if ('a' <= s[z] && s[z] <= 'd' || s[z] == '0' || s[z] == '1') {\n if (s[z] == 'a') v.pb((cnt % 2 == 0) ? i : 1 - i);\n if (s[z] == 'b') v.pb((cnt % 2 == 0) ? j : 1 - j);\n if (s[z] == 'c') v.pb((cnt % 2 == 0) ? x : 1 - x);\n if (s[z] == 'd') v.pb((cnt % 2 == 0) ? y : 1 - y);\n if (s[z] == '0') v.pb((cnt % 2 == 0) ? 0 : 1);\n if (s[z] == '1') v.pb((cnt % 2 == 0) ? 1 : 0);\n cnt = 0;\n }\n else if (s[z] == '*') op = 0;\n else if (s[z] == '^') op = 1;\n else cnt++;\n }\n if (op < 0) return v[0];\n if (op == 0) return v[0] & v[1];\n return v[0] ^ v[1];\n };\n int keta = (i << 3) | (j << 2) | (x << 1) | y;\n val |= (dfs(0, s.size(), dfs) << keta);\n }\n\n //0か1は例外処理\n bool flag = true;\n rep(i, 2)rep(j, 2)rep(x, 2)rep(y, 2) {\n int keta = (i << 3) | (j << 2) | (x << 1) | y;\n if ((val >> keta) & 1) flag = false;\n }\n if (flag) {\n cout << \"1\\n\";\n continue;\n }\n flag = true;\n rep(i, 2)rep(j, 2)rep(x, 2)rep(y, 2) {\n int keta = (i << 3) | (j << 2) | (x << 1) | y;\n if (!((val >> keta) & 1)) flag = false;\n }\n if (flag) {\n cout << \"1\\n\";\n continue;\n }\n\n int ans = s.size();\n for (const auto& g : e) {\n if (g.s.size() > ans) continue;\n if (val == g.val) ans = min(ans, int(g.s.size()));\n if (val == (((1 << 16) - 1) ^ g.val)) ans = min(ans, int(g.s.size()) + 1);\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 2180, "memory_kb": 16604, "score_of_the_acc": -0.6639, "final_rank": 16 }, { "submission_id": "aoj_1620_7974903", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint NOT(int x) {\n return x^1;\n}\nint XOR(int x, int y) {\n return x^y;\n}\nint AND(int x, int y) {\n return x&y;\n}\nint expression(string &s, int &l, int &r, vector<int> &f) {\n if( '0' <= s[l] && s[l] <= '1' ) {\n return s[l]-'0';\n }else if( 'a' <= s[l] && s[l] <= 'd' ) {\n return f[s[l]-'a'];\n }else if( s[l] == '-' ) {\n l++;\n return NOT(expression(s, l, r, f));\n }else {\n int p, q;\n char c;\n for( int i = l+1, d = 0; i <= r-1; i++ ) {\n if( s[i] == '(' ) d++;\n if( s[i] == ')' ) d--;\n if( ( s[i] == '*' || s[i] == '^' ) && d == 0 ) c = s[i], p = i-1, q = i+1;\n }\n if( c == '*' ) {\n l++, r--;\n return AND(expression(s, l, p, f), expression(s, q, r, f));\n }\n if( c == '^' ) {\n l++, r--;\n return XOR(expression(s, l, p, f), expression(s, q, r, f));\n }\n }\n return -1;\n}\nint main() {\n int L = 16, rem = 1<<L;\n vector<int> ans(1<<L, -1);\n vector<vector<string>> E(L+1, vector<string>());\n vector<vector<int>> f_all(1<<4, vector<int>(4));\n for( int i = 0; i < 1<<4; i++ ) {\n for( int j = 0; j < 4; j++ ) {\n f_all[i][j] = (i>>j)&1;\n }\n }\n auto get_key = [&](string s) -> int {\n int ret = 0;\n for( int i = 0; i < 1<<4; i++ ) {\n int _l = 0, _r = (int)s.size()-1;\n ret |= expression(s, _l, _r, f_all[i])<<i;\n }\n return ret;\n };\n auto operate = [&](string s) -> void {\n int ret = get_key(s);\n if( ans[ret] == -1 ) {\n rem--;\n ans[ret] = (int)s.size();\n }\n };\n E[1] = {\"0\", \"1\", \"a\", \"b\", \"c\", \"d\"};\n for( string &s : E[1] ) {\n operate(s);\n }\n for( int l = 2; l <= L; l++ ) {\n for( string &s : E[l-1] ) {\n if( s.front() != '-' ) E[l].push_back(\"-\"+s);\n }\n if( l >= 5 ) {\n for( int x = 1; x <= (l-3)/2; x++ ) {\n for( string &s : E[x] ) {\n for( string &t : E[l-3-x] ) {\n E[l].push_back(\"(\"+s+\"*\"+t+\")\");\n E[l].push_back(\"(\"+s+\"^\"+t+\")\");\n }\n }\n }\n }\n for( string &s : E[l] ) {\n if( rem == 0 ) break;\n operate(s);\n }\n }\n string e;\n while( true ) {\n cin >> e;\n if( e == \".\" ) break;\n cout << ans[get_key(e)] << endl;\n }\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 45816, "score_of_the_acc": -0.4211, "final_rank": 14 }, { "submission_id": "aoj_1620_7776375", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i,s,n) for(int i = int(s); i < int(n); i++)\n#define rrep(i,s,n) for(int i = int(n) - 1; i >= s; i--)\n#define all(v) v.begin(), v.end()\n\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\n\ntemplate<class T>\nbool chmin(T &a, T b) {\n if(a <= b) return false;\n a = b;\n return true;\n}\n\ntemplate<class T>\nbool chmax(T &a, T b) {\n if(a >= b) return false;\n a = b;\n return true;\n}\n\nint main() {\n const int INF = std::numeric_limits<int>::max() / 4;\n const int sz = 4;\n const int m = 1<<sz;\n const int mask = (1<<m) - 1;\n const int max = 16;\n std::vector<int> table(4, 0);\n rep(i,0,4) {\n rep(bit,0,1<<sz) {\n if((bit >> i) & 1) {\n table[i] |= (1<<bit);\n }\n }\n }\n std::vector<int> dp(1<<m, INF);\n {\n dp[0] = 1;\n dp[mask] = 1;\n std::vector<std::pair<int,int>> p;\n p.emplace_back(1, 0);\n p.emplace_back(1, mask);\n rep(i,0,4) {\n p.emplace_back(1, table[i]);\n dp[table[i]] = 1;\n }\n int idx = 0;\n while(idx < (int)p.size()) {\n auto [lenS, S] = p[idx];\n if(dp[S] < lenS || dp[S] > max) {\n idx++;\n continue;\n }\n if(dp[mask ^ S] > lenS + 1) {\n dp[mask ^ S] = lenS + 1;\n p.emplace_back(lenS + 1, mask ^ S);\n }\n rep(i,0,idx) {\n auto [lenT, T] = p[i];\n if(dp[T] < lenT) continue;\n if(dp[S^T] > lenS + lenT + 3) {\n dp[S^T] = lenS + lenT + 3;\n p.emplace_back(lenS + lenT + 3, S^T);\n }\n if(dp[S&T] > lenS + lenT + 3) {\n dp[S&T] = lenS + lenT + 3;\n p.emplace_back(lenS + lenT + 3, S & T);\n }\n }\n idx++;\n }\n }\n std::string s;\n while(std::cin >> s, s != \".\") {\n int idx = 0;\n auto f = [&]() -> int {\n char c = s[idx++];\n if(c == '0') return 0;\n else if(c == '1') return (1<<m) - 1;\n else if('a' <= c && c <= 'd') {\n return table[c-'a'];\n }\n else {\n assert(0);\n }\n };\n auto expect = [&](char c) -> bool {\n return s[idx] == c;\n };\n auto consume = [&](char c) -> void {\n if(s[idx] == c) {\n idx++;\n }\n else {\n assert(0);\n }\n };\n auto E = [&](auto &&self) -> int {\n if(expect('-')) {\n consume('-');\n int e = self(self);\n return mask ^ e;\n }\n else if(expect('(')) {\n consume('(');\n int lhs = self(self);\n char c = s[idx];\n consume(c);\n int rhs = self(self);\n consume(')');\n if(c == '*') {\n return lhs & rhs;\n }\n else if(c == '^') {\n return lhs ^ rhs;\n }\n else {\n assert(0);\n }\n }\n else {\n return f();\n }\n };\n int res = E(E);\n assert(idx == (int)s.size());\n std::cout << dp[res] << '\\n';\n }\n}", "accuracy": 1, "time_ms": 310, "memory_kb": 5224, "score_of_the_acc": -0.0913, "final_rank": 11 }, { "submission_id": "aoj_1620_6787084", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vvvvll = vector<vvvll>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nusing vvvb = vector<vvb>;\nusing vd = vector<double>;\nusing vvd = vector<vd>;\nusing vvvd = vector<vvd>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\nusing pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;\n\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\nll gcd(ll(a), ll(b)) {\n if (b == 0)return a;\n ll c = a;\n while (a % b != 0) {\n c = a % b;\n a = b;\n b = c;\n }\n return b;\n}\n\nvll M((1 << 16), 1e18);\n\nbool dev(string S, bool a, bool b, bool c, bool d) {\n bool res = 0;\n ll N = S.size();\n if (S[0] == '(') {\n ll kakko = 0;\n for (ll i = 1; i < N; i++) {\n if (S[i] == '(')kakko++;\n else if (S[i] == ')')kakko--;\n else if (S[i] == '^' && kakko == 0) {\n string S1 = S.substr(1, i - 1);\n string S2 = S.substr(i + 1, N - i - 2);\n return dev(S1, a, b, c, d) ^ dev(S2, a, b, c, d);\n break;\n }\n else if (S[i] == '*' && kakko == 0) {\n string S1 = S.substr(1, i - 1);\n string S2 = S.substr(i + 1, N - i - 2);\n return dev(S1, a, b, c, d) & dev(S2, a, b, c, d);\n break;\n }\n }\n }\n else if (S[0] == '-') {\n return !dev(S.substr(1, N - 1), a, b, c, d);\n }\n else if (S[0] == 'a')return a;\n else if (S[0] == 'b')return b;\n else if (S[0] == 'c')return c;\n else if (S[0] == 'd')return d;\n else if (S[0] == '0')return 0;\n else if (S[0] == '1')return 1;\n return res;\n}\n\nll solve(string S) {\n ll res = 0;\n rep(bit, (1 << 4)) {\n bool a = (bit & 1);\n bool b = (bit & 2);\n bool c = (bit & 4);\n bool d = (bit & 8);\n if (dev(S, a, b, c, d))res += (1 << bit);\n }\n return res;\n}\nset<string> maked;\nvector<string> V;\n\nvoid make(string S) {\n if (maked.count(S))return;\n maked.insert(S);\n if(chmin(M[solve(S)], ll(S.size()))){\n \n V.push_back(S);\n make(\"-\" + S);\n rep(i,V.size()){\n string p = V[i];\n if (p != \"\") {\n if (p.size() + S.size() > 10)continue;\n make(\"(\" + p + \"*\" + S + \")\");\n make(\"(\" + p + \"^\" + S + \")\");\n make(\"(\" + S + \"*\" + p + \")\");\n make(\"(\" + S + \"^\" + p + \")\");\n }\n }\n }\n\n}\n\n\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n maked.insert(\"\");\n make(\"0\");\n make(\"1\");\n make(\"a\");\n make(\"b\");\n make(\"c\");\n make(\"d\");\n string S;\n while (cin >> S) {\n if (S == \".\")return 0;\n cout << min(ll(S.size()),M[solve(S)]) << endl;\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3972, "score_of_the_acc": -0.003, "final_rank": 1 }, { "submission_id": "aoj_1620_6710763", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\nusing namespace std;\n// #include <atcoder/lazysegtree>\n// using namespace atcoder;\n\ntypedef long long int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\ntypedef pair<ll, ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<vll> vvll;\ntypedef vector<vvll> vvvll;\ntypedef vector<pll> vpll;\ntypedef vector<vpll> vvpll;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<double> vd;\ntypedef vector<vd> vvd;\ntypedef priority_queue<pll, vpll, function<bool(pll, pll)> > pqpll;\ntypedef priority_queue<ll, vll, function<bool(pll, pll)> > pqll;\n\n//struct edge{ ll from,to,cost; edge(ll e_from,ll e_to,ll e_cost): from(e_from), to(e_to), cost(e_cost){} };\n//typedef vector<vector<edge>> Graph;\n\n#define rep(i,a,n) for(ll i = a;i < n;i++)\n#define rrep(i,a,n) for(ll i = n-1; i >= a;i--)\n#define LINF (1LL << 60)\n#define INF (1 << 30)\n#define fs first\n#define sc second\n#define EPS 1e-10\n#define ALL(a) a.begin(), a.end()\n#define tcheck(a) if((clock() - start)/(ld)CLOCKS_PER_SEC >= a) break\n#define debug(s) cout << #s << endl\n#define debugval(x) cout << #x\" = \" << x << endl\n\ntemplate<typename T> ll sz(vector<T>& pos) { return (ll)pos.size(); }\ntemplate<typename T> ll sz(priority_queue<T, vector<T> >& pq) { return (ll)pq.size(); }\ntemplate<typename T> ll sz(priority_queue<T, vector<T>, greater<T> >& pq) { return (ll)pq.size(); }\nll sz(string& s) { return (ll)s.size(); }\n\ntemplate<typename T> void chmin(T& a, T b) { if (a > b) a = b; }\ntemplate<typename T> void chmax(T& a, T b) { if (a < b) a = b; }\n\nll gcd(ll a, ll b) { return ((!b) ? a : gcd(b, a % b)); }\nll lcm(ll a, ll b) { return a / gcd(a, b) * b; }\nll dx[4] = { 0,-1,0,1 }, dy[4] = { -1,0,1,0 };\ninline bool isinside(ll i, ll n) { return (i < n&& i >= 0); }\n\ntypedef string::const_iterator State;\nclass ParseError {};\n\nconst ll A = 0xFF00, B = 0xF0F0, C = 0xCCCC, D = 0xAAAA;\n\nll expr(State& begin) {\n if (*begin == '1') {\n begin++;\n return 0xFFFF;\n }\n else if (*begin == '0') {\n begin++;\n return 0;\n }\n else if ('a' <= *begin && *begin <= 'd') {\n ll res = 0;\n if (*begin == 'a') res = A;\n else if (*begin == 'b') res = B;\n else if (*begin == 'c') res = C;\n else if (*begin == 'd') res = D;\n begin++;\n return res;\n }\n else if (*begin == '-') {\n begin++;\n ll res = expr(begin);\n res ^= 0xFFFF;\n return res;\n }\n else if (*begin == '(') {\n begin++;\n ll res = expr(begin);\n char op = *begin; begin++;\n ll res2 = expr(begin);\n if (op == '^') res ^= res2;\n else if (op == '*') res &= res2;\n else {\n cout << \"parseerror0 : \" << *begin << endl;\n throw ParseError();\n }\n begin++;\n return res;\n }\n else {\n cout << \"parseerror1 : \" << *begin << endl;\n throw ParseError();\n }\n return 0;\n}\n\n\nstring t = \"01abcd\";\nvector<string> tvec = vector<string>({ \"-E\",\"(E^E)\",\"(E*E)\" });\nint main() {\n string s;\n while (1) {\n cin >> s;\n if (s == \".\") break;\n\n State begin = s.begin();\n ll res = expr(begin);\n\n ll ans = 16;\n queue<string> que;\n que.emplace(\"E\");\n while (que.size()) {\n string str = que.front();\n que.pop();\n if (ans <= str.size())continue;\n ll ecnt = 0;\n rep(i, 0, sz(str)) if (str[i] == 'E') ecnt++;\n rep(i, 0, sz(str)) {\n if (str[i] == 'E') {\n rep(j, 0, sz(t)) {\n str[i] = t[j];\n begin = str.begin();\n if (ecnt == 1 && expr(begin) == res) chmin(ans, sz(str));\n else if (ecnt >= 2) que.push(str);\n }\n\n rep(j, 0, sz(tvec)) {\n string newstr = str.substr(0, i) + tvec[j] + str.substr(i + 1, sz(str) - i - 1);\n if (sz(newstr) <= 15) que.push(newstr);\n }\n }\n }\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1390, "memory_kb": 19432, "score_of_the_acc": -0.4689, "final_rank": 15 }, { "submission_id": "aoj_1620_6690512", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <ctime>\n#include <cassert>\n#include <complex>\n#include <string>\n#include <cstring>\n#include <chrono>\n#include <random>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <functional>\n\n// AtCoder\n// #include <atcoder/all>\n// using namespace atcoder;\n\n#ifdef LOCAL\n #define eprintf(...) fprintf(stderr, __VA_ARGS__)\n#else\n #define eprintf(...)\n#endif\n\n#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)\n#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)\n#define reprev_(i, a_, b_, a, b, ...) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)\n#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)\n#define all(x) (x).begin(), (x).end()\ntemplate <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }\ntemplate <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }\n#define fls(x) (64 - __builtin_clzll(x))\n#define pcnt(x) __builtin_popcountll(x)\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair <int,int> P;\ntypedef long double ld;\n\nint var(char c) {\n if (c == '0') return 0b0000'0000'0000'0000;\n if (c == '1') return 0b1111'1111'1111'1111;\n if (c == 'a') return 0b1111'1111'0000'0000;\n if (c == 'b') return 0b1111'0000'1111'0000;\n if (c == 'c') return 0b1100'1100'1100'1100;\n if (c == 'd') return 0b1010'1010'1010'1010;\n // cerr << \"[debug] c=\" << c << endl;\n assert(false);\n}\nint parse(const string &s, int &cur) {\n // cerr << \"[debug] cur=\" << cur << \" s[cur]=\" << s[cur] << endl;\n\n int res = 0;\n if (s[cur] == '(') {\n int l = parse(s, ++cur);\n char op = s[cur++];\n int r = parse(s, cur);\n ++cur;\n if (op == '*') return l & r;\n else return l ^ r;\n } else if (s[cur] == '-') {\n int tmp = parse(s, ++cur);\n return ~tmp & 0b1111'1111'1111'1111;\n } else {\n return var(s[cur++]);\n }\n // cerr << \"[debug] cur=\" << cur << endl;\n assert(false);\n}\nint parse(const string &s) {\n int cur = 0;\n return parse(s, cur);\n}\nvoid debug_out(int res) {\n cerr << \"[debug] \";\n reprev (i, 16) cerr << (res >> i & 1);\n cerr << endl;\n}\n\nvector<int> make_table(void) {\n vector<int> res(1 << 16, 1e9);\n map<int, int> mp, mp_;\n mp[var('0')] = 1;\n mp[var('1')] = 1;\n mp[var('a')] = 1;\n mp[var('b')] = 1;\n mp[var('c')] = 1;\n mp[var('d')] = 1;\n rep (_, 4) {\n for (auto [k, v] : mp) {\n // -\n int minus = ~k & 0b1111'1111'1111'1111;\n if (mp.find(minus) == mp.end()) mp[minus] = v + 1;\n else chmin(mp[minus], v + 1);\n }\n\n if (_ == 3) break;\n\n mp_ = mp;\n for (auto [k, v] : mp) {\n for (auto [k_, v_] : mp) {\n int res = k & k_;\n if (mp_.find(res) == mp_.end()) mp_[res] = v + v_ + 3;\n else chmin(mp_[res], v + v_ + 3);\n }\n\n // ^\n for (auto [k_, v_] : mp) {\n int res = k ^ k_;\n if (mp_.find(res) == mp_.end()) mp_[res] = v + v_ + 3;\n else chmin(mp_[res], v + v_ + 3);\n }\n }\n swap(mp, mp_);\n }\n for (auto [k, v] : mp) res[k] = v;\n return res;\n}\n\nint main (void)\n{\n cin.tie(0);\n ios::sync_with_stdio(false);\n auto table = make_table();\n string s;\n while (true) {\n cin >> s;\n if (s == \".\") break;\n int parsed = parse(s);\n // debug_out(parsed);\n cout << table[parsed] << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 510, "memory_kb": 6288, "score_of_the_acc": -0.1516, "final_rank": 12 }, { "submission_id": "aoj_1620_5963071", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <climits>\n#include <string>\n#include <cmath>\n#include <bitset>\n#include <complex>\n#include <functional>\n#include <ctime>\n#include <cassert>\n#include <fstream>\n#include <stack>\n#include <random>\n#include <iomanip>\n#include <time.h>\n#include <list>\n#include <unordered_map>\n\n#include <algorithm>\n#include <array>\n\nusing namespace std;\ntypedef long long ll;\ntypedef long double dd;\ntypedef vector<ll> vl;\ntypedef vector<dd> vd;\ntypedef vector<vector<ll>> vvl;\n#define i_7 (ll)(1E9+7)\n//#define i_7 998244353\n#define i_5 i_7-2\nll mod(ll a){\n ll c=a%i_7;\n if(c>=0)return c;\n return c+i_7;\n}\ntypedef pair<ll,ll> l_l;\ntypedef pair<dd,dd> d_d;\nll inf=(ll)1E18;\n#define rep(i,l,r) for(ll i=l;i<=r;i++)\n#define pb push_back\nll max(ll a,ll b){if(a<b)return b;else return a;}\nll min(ll a,ll b){if(a>b)return b;else return a;}\ndd EPS=1E-6;\ndd PI=acos(-1);\n#define endl \"\\n\"\n#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n\n\n\n\nll exp(const string &s, ll &cur, const bitset<4> cha){\n if(s[cur] == '-'){\n cur++;\n ll tmp = exp(s, cur, cha);\n return 1-tmp;\n }else if(s[cur] == '('){\n cur++;\n ll t1 = exp(s, cur, cha);\n char op = s[cur];\n cur++;\n ll t2 = exp(s, cur, cha);\n cur++;\n if(op == '*'){\n return t1&t2;\n }else if(op == '^'){\n return t1^t2;\n }\n assert(0);\n return 0;\n }else if(s[cur] == '0' || s[cur] == '1'){\n ll tmp = s[cur] - '0';\n cur++;\n return tmp;\n }else{\n ll index = s[cur] - 'a';\n ll tmp = cha[index];\n cur++;\n return tmp;\n }\n}\n\nbitset<16> exp_all(const string &s){\n bitset<16> res;\n rep(i,0,15){\n bitset<4> cha(i);\n ll cur=0;\n ll exp_res = exp(s, cur, cha);\n res[i] = exp_res;\n }\n return res;\n}\n\nstruct Hoge{\n ll time;\n bitset<16> value;\n Hoge(ll time, bitset<16> value){\n this->time = time;\n this->value = value;\n }\n bool operator < ( const Hoge &b ) const{\n if(this->time == b.time){\n return this->value.to_ulong() < b.value.to_ulong();\n }else{\n return this->time < b.time;\n }\n }\n \n};\n\nsigned main(){fastio\n ll N=(1<<16);\n ll dp[N];\n rep(i,0,N-1)dp[i] = inf;\n \n bitset<16> bit_0(\"0000000000000000\");\n bitset<16> bit_1(\"1111111111111111\");\n bitset<16> bit_a(\"1111111100000000\");\n bitset<16> bit_b(\"1111000011110000\");\n bitset<16> bit_c(\"1100110011001100\");\n bitset<16> bit_d(\"1010101010101010\");\n dp[bit_0.to_ulong()] = 1;\n dp[bit_1.to_ulong()] = 1;\n dp[bit_a.to_ulong()] = 1;\n dp[bit_b.to_ulong()] = 1;\n dp[bit_c.to_ulong()] = 1;\n dp[bit_d.to_ulong()] = 1;\n set<Hoge> que;\n que.insert(Hoge(1, bit_0));\n que.insert(Hoge(1, bit_1));\n que.insert(Hoge(1, bit_a));\n que.insert(Hoge(1, bit_b));\n que.insert(Hoge(1, bit_c));\n que.insert(Hoge(1, bit_d));\n \n while(!que.empty()){\n Hoge que_tmp=*que.begin(); que.erase(que.begin());\n ll d=que_tmp.time;\n bitset<16> value = que_tmp.value;\n if(dp[value.to_ulong()] < d)continue;\n //'-'\n //cout<<value<<' '<<dp[value.to_ulong()]<<' '<<que.size()<<endl;\n if(d > 16)continue;\n \n bitset<16> not_ = ~value;\n if(dp[not_.to_ulong()] > dp[value.to_ulong()] + 1){\n dp[not_.to_ulong()] = dp[value.to_ulong()] + 1;\n que.insert(Hoge(dp[not_.to_ulong()], not_));\n }\n rep(i,0,(1<<16)-1){\n if(dp[i]==inf)continue;\n bitset<16> bi(i);\n bitset<16> and_ = value&bi;\n bitset<16> xor_ = value^bi;\n if(dp[and_.to_ulong()] > dp[value.to_ulong()] + dp[i] +3){\n dp[and_.to_ulong()] = dp[value.to_ulong()] + dp[i] +3;\n que.insert(Hoge(dp[and_.to_ulong()], and_));\n }\n if(dp[xor_.to_ulong()] > dp[value.to_ulong()] + dp[i] +3){\n dp[xor_.to_ulong()] = dp[value.to_ulong()] + dp[i] +3;\n que.insert(Hoge(dp[xor_.to_ulong()], xor_));\n }\n }\n }\n \n //cout<<\"Hoge\"<<endl;\n \n while(true){\n string s;cin>>s;\n if(s==\".\"){\n return 0;\n }\n bitset<16> res = exp_all(s);\n cout<<dp[res.to_ulong()]<<endl;\n }\n \n return 0;\n}", "accuracy": 1, "time_ms": 210, "memory_kb": 25784, "score_of_the_acc": -0.1906, "final_rank": 13 } ]
aoj_1623_cpp
Equivalent Deformation Two triangles T 1 and T 2 with the same area are on a plane. Your task is to perform the following operation to T 1 several times so that it is exactly superposed on T 2 . Here, vertices of T 1 can be on any of the vertices of T 2 . Compute the minimum number of operations required to superpose T 1 on T 2 . (Operation): Choose one vertex of the triangle and move it to an arbitrary point on a line passing through the vertex and parallel to the opposite side. An operation example The following figure shows a possible sequence of the operations for the first dataset of the sample input. Input The input consists of at most 2000 datasets, each in the following format. x 11 y 11 x 12 y 12 x 13 y 13 x 21 y 21 x 22 y 22 x 23 y 23 x ij and y ij are the x- and y- coordinate of the j -th vertex of T i . The following holds for the dataset. All the coordinate values are integers with at most 1000 of absolute values. T 1 and T 2 have the same positive area size. The given six vertices are all distinct points. An empty line is placed between datasets. The end of the input is indicated by EOF. Output For each dataset, output the minimum number of operations required in one line. If five or more operations are required, output Many instead. Note that, vertices may have non-integral values after they are moved. You can prove that, for any input satisfying the above constraints, the number of operations required is bounded by some constant. Sample Input 0 1 2 2 1 0 1 3 5 2 4 3 0 0 0 1 1 0 0 3 0 2 1 -1 -5 -4 0 1 0 15 -10 14 -5 10 0 -8 -110 221 -731 525 -555 -258 511 -83 -1000 -737 66 -562 533 -45 -525 -450 -282 -667 -439 823 -196 606 -768 -233 0 0 0 1 1 0 99 1 100 1 55 2 354 -289 89 -79 256 -166 131 -196 -774 -809 -519 -623 -990 688 -38 601 -360 712 384 759 -241 140 -59 196 629 -591 360 -847 936 -265 109 -990 -456 -913 -787 -884 -1000 -1000 -999 -999 -1000 -998 1000 1000 999 999 1000 998 -386 -83 404 -83 -408 -117 -162 -348 128 -88 296 -30 -521 -245 -613 -250 797 451 -642 239 646 309 -907 180 -909 -544 -394 10 -296 260 -833 268 -875 882 -907 -423 Output for the Sample Input 4 3 3 3 3 4 4 4 4 Many Many Many Many
[ { "submission_id": "aoj_1623_9769018", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n/**\n * @brief template\n */\n\nusing T = double;\nconst T eps = 1e-6;\nusing Point = complex<T>;\nusing Poly = vector<Point>;\n#define X real()\n#define Y imag()\ntemplate <typename T> inline bool eq(const T &a, const T &b) {\n return fabs(a - b) < eps;\n}\nbool cmp(const Point &a, const Point &b) {\n auto sub = [&](Point a) {\n return (a.Y < 0 ? -1 : (a.Y == 0 && a.X >= 0 ? 0 : 1));\n };\n if (sub(a) != sub(b))\n return sub(a) < sub(b);\n return a.Y * b.X < a.X * b.Y;\n}\nstruct Line {\n Point a, b, dir;\n Line() {}\n Line(Point _a, Point _b) : a(_a), b(_b), dir(b - a) {}\n Line(T A, T B, T C) {\n if (eq(A, .0)) {\n a = Point(0, C / B), b = Point(1 / C / B);\n } else if (eq(B, .0)) {\n a = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n};\nstruct Segment : Line {\n Segment() {}\n Segment(Point _a, Point _b) : Line(_a, _b) {}\n};\nstruct Circle {\n Point p;\n T r;\n Circle() {}\n Circle(Point _p, T _r) : p(_p), r(_r) {}\n};\n\nistream &operator>>(istream &is, Point &p) {\n T x, y;\n is >> x >> y;\n p = Point(x, y);\n return is;\n}\nostream &operator<<(ostream &os, Point &p) {\n os << fixed << setprecision(12) << p.X << ' ' << p.Y;\n return os;\n}\nPoint unit(const Point &a) {\n return a / abs(a);\n}\nT dot(const Point &a, const Point &b) {\n return a.X * b.X + a.Y * b.Y;\n}\nT cross(const Point &a, const Point &b) {\n return a.X * b.Y - a.Y * b.X;\n}\nPoint rot(const Point &a, const T &theta) {\n return Point(cos(theta) * a.X - sin(theta) * a.Y,\n sin(theta) * a.X + cos(theta) * a.Y);\n}\nPoint rot90(const Point &a) {\n return Point(-a.Y, a.X);\n}\nT arg(const Point &a, const Point &b, const Point &c) {\n double ret = acos(dot(a - b, c - b) / abs(a - b) / abs(c - b));\n if (cross(a - b, c - b) < 0)\n ret = -ret;\n return ret;\n}\n\nPoint Projection(const Line &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Projection(const Segment &l, const Point &p) {\n T t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n}\nPoint Reflection(const Line &l, const Point &p) {\n return p + (Projection(l, p) - p) * 2.;\n}\nint ccw(const Point &a, Point b, Point c) {\n b -= a;\n c -= a;\n if (cross(b, c) > eps)\n return 1; // ccw\n\n if (cross(b, c) < -eps)\n return -1; // cw\n\n if (dot(b, c) < 0)\n return 2; // c,a,b\n\n if (norm(b) < norm(c))\n return -2; // a,b,c\n\n return 0; // a,c,b\n\n}\nbool isOrthogonal(const Line &a, const Line &b) {\n return eq(dot(a.b - a.a, b.b - b.a), .0);\n}\nbool isParallel(const Line &a, const Line &b) {\n return eq(cross(a.b - a.a, b.b - b.a), .0);\n}\nbool isIntersect(const Segment &a, const Segment &b) {\n return ccw(a.a, a.b, b.a) * ccw(a.a, a.b, b.b) <= 0 and\n ccw(b.a, b.b, a.a) * ccw(b.a, b.b, a.b) <= 0;\n}\nint isIntersect(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d > a.r + b.r + eps)\n return 4;\n if (eq(d, a.r + b.r))\n return 3;\n if (eq(d, abs(a.r - b.r)))\n return 1;\n if (d < abs(a.r - b.r) - eps)\n return 0;\n return 2;\n}\nT Dist(const Line &a, const Point &b) {\n Point c = Projection(a, b);\n return abs(c - b);\n}\nT Dist(const Segment &a, const Point &b) {\n if (dot(a.b - a.a, b - a.a) < eps)\n return abs(b - a.a);\n if (dot(a.a - a.b, b - a.b) < eps)\n return abs(b - a.b);\n return abs(cross(a.b - a.a, b - a.a)) / abs(a.b - a.a);\n}\nT Dist(const Segment &a, const Segment &b) {\n if (isIntersect(a, b))\n return .0;\n T res = min({Dist(a, b.a), Dist(a, b.b), Dist(b, a.a), Dist(b, a.b)});\n return res;\n}\nPoint Intersection(const Line &a, const Line &b) {\n T d1 = cross(a.b - a.a, b.b - b.a);\n T d2 = cross(a.b - a.a, a.b - b.a);\n if (eq(d1, 0.) and eq(d2, 0.))\n return b.a;\n return b.a + (b.b - b.a) * (d2 / d1);\n}\nPoly Intersection(const Circle &a, const Line &b) {\n Poly res;\n T d = Dist(b, a.p);\n if (d > a.r + eps)\n return res;\n Point h = Projection(b, a.p);\n if (eq(d, a.r)) {\n res.push_back(h);\n return res;\n }\n Point e = unit(b.b - b.a);\n T ph = sqrt(a.r * a.r - d * d);\n res.push_back(h - e * ph);\n res.push_back(h + e * ph);\n return res;\n}\nPoly Intersection(const Circle &a, const Segment &b) {\n Line c(b.a, b.b);\n Poly sub = Intersection(a, c);\n double xmi = min(b.a.X, b.b.X), xma = max(b.a.X, b.b.X);\n double ymi = min(b.a.Y, b.b.Y), yma = max(b.a.Y, b.b.Y);\n Poly res;\n rep(i, 0, sub.size()) {\n if (xmi <= sub[i].X + eps and sub[i].X - eps <= xma and\n ymi <= sub[i].Y + eps and sub[i].Y - eps <= yma) {\n res.push_back(sub[i]);\n }\n }\n return res;\n}\nPoly Intersection(const Circle &a, const Circle &b) {\n Poly res;\n int mode = isIntersect(a, b);\n T d = abs(a.p - b.p);\n if (mode == 4 or mode == 0)\n return res;\n if (mode == 3) {\n T t = a.r / (a.r + b.r);\n res.push_back(a.p + (b.p - a.p) * t);\n return res;\n }\n if (mode == 1) {\n if (b.r < a.r - eps) {\n res.push_back(a.p + (b.p - a.p) * (a.r / d));\n } else {\n res.push_back(b.p + (a.p - b.p) * (b.r / d));\n }\n return res;\n }\n T rc = (a.r * a.r + d * d - b.r * b.r) / d / 2.;\n T rs = sqrt(a.r * a.r - rc * rc);\n if (a.r - abs(rc) < eps)\n rs = 0;\n Point e = unit(b.p - a.p);\n res.push_back(a.p + rc * e + rs * e * Point(0, 1));\n res.push_back(a.p + rc * e + rs * e * Point(0, -1));\n return res;\n}\nPoly HalfplaneIntersection(vector<Line> &H) {\n sort(ALL(H), [&](Line &l1, Line &l2) { return cmp(l1.dir, l2.dir); });\n auto outside = [&](Line &L, Point p) -> bool {\n return cross(L.dir, p - L.a) < -eps;\n };\n deque<Line> deq;\n int sz = 0;\n rep(i, 0, SZ(H)) {\n while (sz > 1 and\n outside(H[i], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 1 and outside(H[i], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz > 0 and fabs(cross(H[i].dir, deq[sz - 1].dir)) < eps) {\n if (dot(H[i].dir, deq[sz - 1].dir) < 0) {\n return {};\n }\n if (outside(H[i], deq[sz - 1].a)) {\n deq.pop_back();\n sz--;\n } else\n continue;\n }\n deq.push_back(H[i]);\n sz++;\n }\n\n while (sz > 2 and outside(deq[0], Intersection(deq[sz - 1], deq[sz - 2]))) {\n deq.pop_back();\n sz--;\n }\n while (sz > 2 and outside(deq[sz - 1], Intersection(deq[0], deq[1]))) {\n deq.pop_front();\n sz--;\n }\n if (sz < 3)\n return {};\n deq.push_back(deq.front());\n Poly ret;\n rep(i, 0, sz) ret.push_back(Intersection(deq[i], deq[i + 1]));\n return ret;\n}\n\nT Area(const Poly &a) {\n T res = 0;\n int n = a.size();\n rep(i, 0, n) res += cross(a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Poly &a, const Circle &b) {\n int n = a.size();\n if (n < 3)\n return .0;\n auto rec = [&](auto self, const Circle &c, const Point &p1,\n const Point &p2) {\n Point va = c.p - p1, vb = c.p - p2;\n T f = cross(va, vb), res = .0;\n if (eq(f, .0))\n return res;\n if (max(abs(va), abs(vb)) < c.r + eps)\n return f;\n if (Dist(Segment(p1, p2), c.p) > c.r - eps)\n return c.r * c.r * arg(vb * conj(va));\n auto u = Intersection(c, Segment(p1, p2));\n Poly sub;\n sub.push_back(p1);\n for (auto &x : u)\n sub.push_back(x);\n sub.push_back(p2);\n rep(i, 0, sub.size() - 1) res += self(self, c, sub[i], sub[i + 1]);\n return res;\n };\n T res = .0;\n rep(i, 0, n) res += rec(rec, b, a[i], a[(i + 1) % n]);\n return fabs(res / 2.);\n}\nT Area(const Circle &a, const Circle &b) {\n T d = abs(a.p - b.p);\n if (d >= a.r + b.r - eps)\n return .0;\n if (d <= abs(a.r - b.r) + eps) {\n T r = min(a.r, b.r);\n return M_PI * r * r;\n }\n T ath = acos((a.r * a.r + d * d - b.r * b.r) / d / a.r / 2.);\n T res = a.r * a.r * (ath - sin(ath * 2) / 2.);\n T bth = acos((b.r * b.r + d * d - a.r * a.r) / d / b.r / 2.);\n res += b.r * b.r * (bth - sin(bth * 2) / 2.);\n return fabs(res);\n}\nbool isConvex(const Poly &a) {\n int n = a.size();\n int cur, pre, nxt;\n rep(i, 0, n) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n cur = i;\n if (ccw(a[pre], a[cur], a[nxt]) == -1)\n return 0;\n }\n return 1;\n}\nint isContained(const Poly &a,\n const Point &b) { // 0:not contain,1:on edge,2:contain\n\n bool res = 0;\n int n = a.size();\n rep(i, 0, n) {\n Point p = a[i] - b, q = a[(i + 1) % n] - b;\n if (p.Y > q.Y)\n swap(p, q);\n if (p.Y < eps and eps < q.Y and cross(p, q) > eps)\n res ^= 1;\n if (eq(cross(p, q), .0) and dot(p, q) < eps)\n return 1;\n }\n return (res ? 2 : 0);\n}\nPoly ConvexHull(Poly &a) {\n sort(ALL(a), [](const Point &p, const Point &q) {\n return (eq(p.Y, q.Y) ? p.X < q.X : p.Y < q.Y);\n });\n a.erase(unique(ALL(a)), a.end());\n int n = a.size(), k = 0;\n Poly res(n * 2);\n for (int i = 0; i < n; res[k++] = a[i++]) {\n while (k >= 2 and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n for (int i = n - 2, t = k + 1; i >= 0; res[k++] = a[i--]) {\n while (k >= t and\n cross(res[k - 1] - res[k - 2], a[i] - res[k - 1]) < eps)\n k--;\n }\n res.resize(k - 1);\n return res;\n}\nT Diam(const Poly &a) {\n int n = a.size();\n int x = 0, y = 0;\n rep(i, 1, n) {\n if (a[i].Y > a[x].Y)\n x = i;\n if (a[i].Y < a[y].Y)\n y = i;\n }\n T res = abs(a[x] - a[y]);\n int i = x, j = y;\n do {\n if (cross(a[(i + 1) % n] - a[i], a[(j + 1) % n] - a[j]) < 0)\n i = (i + 1) % n;\n else\n j = (j + 1) % n;\n chmax(res, abs(a[i] - a[j]));\n } while (i != x or j != y);\n return res;\n}\nPoly Cut(const Poly &a, const Line &l) {\n int n = a.size();\n Poly res;\n rep(i, 0, n) {\n Point p = a[i], q = a[(i + 1) % n];\n if (ccw(l.a, l.b, p) != -1)\n res.push_back(p);\n if (ccw(l.a, l.b, p) * ccw(l.a, l.b, q) < 0)\n res.push_back(Intersection(Line(p, q), l));\n }\n return res;\n}\n\nT Closest(Poly &a) {\n int n = a.size();\n if (n <= 1)\n return 0;\n sort(ALL(a), [&](Point a, Point b) {\n return (eq(a.X, b.X) ? a.Y < b.Y : a.X < b.X);\n });\n Poly buf(n);\n auto rec = [&](auto self, int lb, int rb) -> T {\n if (rb - lb <= 1)\n return (T)INF;\n int mid = (lb + rb) >> 1;\n auto x = a[mid].X;\n T res = min(self(self, lb, mid), self(self, mid, rb));\n inplace_merge(a.begin() + lb, a.begin() + mid, a.begin() + rb,\n [&](auto p, auto q) { return p.Y < q.Y; });\n int ptr = 0;\n rep(i, lb, rb) {\n if (abs(a[i].X - x) >= res)\n continue;\n rep(j, 0, ptr) {\n auto sub = a[i] - buf[ptr - 1 - j];\n if (sub.Y >= res)\n break;\n chmin(res, abs(sub));\n }\n buf[ptr++] = a[i];\n }\n return res;\n };\n return rec(rec, 0, n);\n}\n\nCircle Incircle(const Point &a, const Point &b, const Point &c) {\n T A = abs(b - c), B = abs(c - a), C = abs(a - b);\n Point p(A * a.X + B * b.X + C * c.X, A * a.Y + B * b.Y + C * c.Y);\n p /= (A + B + C);\n T r = Dist(Line(a, b), p);\n return Circle(p, r);\n}\nCircle Circumcircle(const Point &a, const Point &b, const Point &c) {\n Line l1((a + b) / 2., (a + b) / 2. + (b - a) * Point(0, 1));\n Line l2((b + c) / 2., (b + c) / 2. + (c - b) * Point(0, 1));\n Point p = Intersection(l1, l2);\n return Circle(p, abs(p - a));\n}\nPoly tangent(const Point &a, const Circle &b) {\n return Intersection(b, Circle(a, sqrt(norm(b.p - a) - b.r * b.r)));\n}\nvector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> res;\n T d = abs(a.p - b.p);\n if (eq(d, 0.))\n return res;\n Point u = unit(b.p - a.p);\n Point v = u * Point(0, 1);\n for (int t : {-1, 1}) {\n T h = (a.r + b.r * t) / d;\n if (eq(h * h, 1.)) {\n res.push_back(Line(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v));\n } else if (1 > h * h) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n res.push_back(Line(a.p + (U + V) * a.r, b.p - (U + V) * (b.r * t)));\n res.push_back(Line(a.p + (U - V) * a.r, b.p - (U - V) * (b.r * t)));\n }\n }\n return res;\n}\n\n/**\n * @brief Geometry\n */\n\nint main() {\n Point P[2][3];\n double x,y;\nwhile(cin >> x >> y) {\n P[0][0] = Point(x,y);\n rep(i,0,2) {\n rep(j,0,3) {\n if (i == 0 && j == 0) continue;\n cin >> x >> y;\n P[i][j] = Point(x,y);\n }\n }\n auto DFS = [&](auto self, vector<Point> match, vector<Point> tmp, int id, int Dep) -> int {\n if (Dep >= 5) return 5;\n if (id == 3) {\n rep(i,0,3) {\n if (!eq(tmp[i],match[i])) return 5;\n }\n return Dep;\n }\n int Ret = 5;\n Point P1 = tmp[id], P2 = tmp[(id+1)%3], P3 = tmp[(id+2)%3];\n Point Q = match[id];\n if (isParallel(Line(P1,Q),Line(P2,P3))) {\n vector<Point> next = tmp;\n next[id] = Q;\n chmin(Ret,self(self,match,next,id+1,Dep+1));\n }\n else {\n {\n vector<Point> next = tmp;\n next[id] = Q;\n next[(id+1)%3] = Intersection(Line(P2,P2+P3-P1),Line(P3,P3+P1-Q));\n chmin(Ret,self(self,match,next,id+1,Dep+2));\n }\n {\n vector<Point> next = tmp;\n next[id] = Q;\n next[(id+2)%3] = Intersection(Line(P3,P3+P2-P1),Line(P2,P2+P1-Q));\n chmin(Ret,self(self,match,next,id+1,Dep+2));\n }\n }\n return Ret;\n };\n int ANS = 5;\n vector<int> ord1(3);\n iota(ALL(ord1),0);\n do {\n vector<int> ord2(3);\n iota(ALL(ord2),0);\n do {\n vector<Point> match(3);\n rep(i,0,3) match[i] = P[0][ord1[i]];\n vector<Point> tmp(3);\n rep(i,0,3) tmp[i] = P[1][ord2[i]];\n chmin(ANS, DFS(DFS,match,tmp,0,0));\n } while(next_permutation(ALL(ord2)));\n } while(next_permutation(ALL(ord1)));\n if (ANS == 5) cout << \"Many\" << endl;\n else cout << ANS << endl;\n}\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3488, "score_of_the_acc": -0.8721, "final_rank": 12 }, { "submission_id": "aoj_1623_9283785", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nusing Real = long double;\nconst Real EPS = 1e-8, PI = acos(Real(-1.0));\nint sign(const Real& r) {\n if(r <= -EPS) return -1;\n if(r >= +EPS) return +1;\n return 0;\n}\nbool eq(const Real& a, const Real& b) {\n return sign(a - b) == 0;\n}\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, const Point& p) {\n return os << p.real() << ' ' << p.imag();\n}\nReal dot(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).real();\n}\nReal cross(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).imag();\n}\nint ccw(const Point& a, Point b, Point c) {\n b -= a;\n c -= a;\n if(sign(cross(b, c)) == 1) return 1;\n if(sign(cross(b, c)) == -1) return -1;\n if(sign(dot(b, c)) == -1) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\nstruct Line {\n Point a, b;\n Line() = default;\n Line(const Point& a, const Point& b)\n : a(a), b(b) {}\n};\nusing Segment = Line;\nbool is_parallel(const Line& a, const Line& b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\nbool is_intersect_ll(const Line& l1, const Line& l2) {\n if(!eq(cross(l1.b - l1.a, l2.b - l2.a), 0.0)) return true;\n return eq(cross(l1.b - l1.a, l2.b - l1.a), 0.0);\n}\nvector<Point> intersection_ll(const Line& l1, const Line& l2) {\n vector<Point> res;\n if(!is_intersect_ll(l1, l2)) return res;\n Real a = cross(l1.b - l1.a, l2.b - l2.a);\n Real b = cross(l1.b - l1.a, l1.b - l2.a);\n if(eq(a, 0.0) and eq(b, 0.0)) {\n res.push_back(l2.a);\n } else {\n res.push_back(l2.a + (l2.b - l2.a) * b / a);\n }\n return res;\n}\nint main(void) {\n cin.tie(0);\n ios::sync_with_stdio(0);\n vector<Point> p1(3), p2(3);\n while(cin >> p1[0]) {\n rep(i, 1, 3) cin >> p1[i];\n rep(i, 0, 3) cin >> p2[i];\n auto is_three = [&](vector<Point> t1, vector<Point> t2, vector<int> ord) -> bool {\n rep(i, 0, 3) {\n Line l1 = Line(t1[ord[i]], t2[ord[i]]);\n Line l2 = Line(t1[ord[(i + 1) % 3]], t1[ord[(i + 2) % 3]]);\n if(!is_parallel(l1, l2)) return false;\n t1[ord[i]] = t2[ord[i]];\n }\n return true;\n };\n auto is_four = [&](vector<Point> t1, vector<Point> t2, vector<int> ord) -> bool {\n Line l1 = Line(t1[ord[0]], t2[ord[0]]);\n Line l2 = Line(t1[ord[1]], t1[ord[2]]);\n if(is_parallel(l1, l2)) {\n vector<Point> t3 = t1;\n t3[ord[0]] = t2[ord[0]];\n\n Point dir1 = t3[ord[1]] - t2[ord[1]];\n Line l3 = Line(t3[ord[0]], t3[ord[0]] + dir1);\n Point dir2 = t3[ord[0]] - t3[ord[1]];\n Line l4 = Line(t3[ord[2]], t3[ord[2]] + dir2);\n if(is_intersect_ll(l3, l4)) return true;\n\n return false;\n }\n\n Point dir1 = t1[ord[0]] - t1[ord[2]];\n Line l3 = Line(t1[ord[1]], t1[ord[1]] + dir1);\n Point dir2 = t1[ord[0]] - t2[ord[0]];\n Line l4 = Line(t1[ord[2]], t1[ord[2]] + dir2);\n vector<Point> cand1 = intersection_ll(l3, l4);\n if(!cand1.empty()) {\n vector<Point> t3 = t1;\n t3[ord[1]] = cand1[0];\n vector<int> ord2(3);\n rep(i, 0, 3) ord2[i] = i;\n do {\n if(is_three(t3, t2, ord2)) return true;\n } while(next_permutation(ord2.begin(), ord2.end()));\n }\n\n Point dir3 = t1[ord[0]] - t1[ord[1]];\n Line l5 = Line(t1[ord[2]], t1[ord[2]] + dir3);\n Point dir4 = t1[ord[0]] - t2[ord[0]];\n Line l6 = Line(t1[ord[1]], t1[ord[1]] + dir4);\n vector<Point> cand2 = intersection_ll(l5, l6);\n if(!cand2.empty()) {\n vector<Point> t3 = t1;\n t3[ord[2]] = cand2[0];\n vector<int> ord2(3);\n rep(i, 0, 3) ord2[i] = i;\n do {\n if(is_three(t3, t2, ord2)) return true;\n } while(next_permutation(ord2.begin(), ord2.end()));\n }\n\n return false;\n };\n vector<int> perm(3);\n rep(i, 0, 3) perm[i] = i;\n int ans = 5;\n do {\n vector<Point> t2 = p2;\n vector<Point> t1(3);\n rep(i, 0, 3) {\n t1[i] = p1[perm[i]];\n }\n if(ccw(t1[0], t1[1], t1[2]) != ccw(t2[0], t2[1], t2[2])) continue;\n vector<int> ord(3);\n rep(i, 0, 3) ord[i] = i;\n do {\n if(is_three(t1, t2, ord)) ans = min(ans, 3);\n if(is_four(t1, t2, ord)) ans = min(ans, 4);\n } while(next_permutation(ord.begin(), ord.end()));\n } while(next_permutation(perm.begin(), perm.end()));\n if(ans == 5) {\n cout << \"Many\" << '\\n';\n } else {\n cout << ans << '\\n';\n }\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3544, "score_of_the_acc": -1.0041, "final_rank": 17 }, { "submission_id": "aoj_1623_9283770", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nusing Real = long double;\nconst Real EPS = 1e-8, PI = acos(Real(-1.0));\nint sign(const Real& r) {\n if(r <= -EPS) return -1;\n if(r >= +EPS) return +1;\n return 0;\n}\nbool eq(const Real& a, const Real& b) {\n return sign(a - b) == 0;\n}\nusing Point = complex<Real>;\nistream& operator>>(istream& is, Point& p) {\n Real a, b;\n is >> a >> b;\n p = Point(a, b);\n return is;\n}\nostream& operator<<(ostream& os, const Point& p) {\n return os << p.real() << ' ' << p.imag();\n}\nReal dot(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).real();\n}\nReal cross(const Point& p1, const Point& p2) {\n return (conj(p1) * p2).imag();\n}\nint ccw(const Point& a, Point b, Point c) {\n b -= a;\n c -= a;\n if(sign(cross(b, c)) == 1) return 1;\n if(sign(cross(b, c)) == -1) return -1;\n if(sign(dot(b, c)) == -1) return +2;\n if(norm(b) < norm(c)) return -2;\n return 0;\n}\nstruct Line {\n Point a, b;\n Line() = default;\n Line(const Point& a, const Point& b)\n : a(a), b(b) {}\n};\nusing Segment = Line;\nbool is_parallel(const Line& a, const Line& b) {\n return eq(cross(a.b - a.a, b.b - b.a), 0.0);\n}\nbool is_intersect_ll(const Line& l1, const Line& l2) {\n if(!eq(cross(l1.b - l1.a, l2.b - l2.a), 0.0)) return true;\n return eq(cross(l1.b - l1.a, l2.b - l1.a), 0.0);\n}\nvector<Point> intersection_ll(const Line& l1, const Line& l2) {\n vector<Point> res;\n if(!is_intersect_ll(l1, l2)) return res;\n Real a = cross(l1.b - l1.a, l2.b - l2.a);\n Real b = cross(l1.b - l1.a, l1.b - l2.a);\n if(eq(a, 0.0) and eq(b, 0.0)) {\n res.push_back(l2.a);\n } else {\n res.push_back(l2.a + (l2.b - l2.a) * b / a);\n }\n return res;\n}\nint main(void) {\n cin.tie(0);\n ios::sync_with_stdio(0);\n while(1) {\n vector<Point> p1(3), p2(3);\n rep(i, 0, 3) cin >> p1[i];\n if(cin.eof()) break;\n rep(i, 0, 3) cin >> p2[i];\n if(cin.eof()) break;\n auto is_three = [&](vector<Point> t1, vector<Point> t2, vector<int> ord) -> bool {\n rep(i, 0, 3) {\n Line l1 = Line(t1[ord[i]], t2[ord[i]]);\n Line l2 = Line(t1[ord[(i + 1) % 3]], t1[ord[(i + 2) % 3]]);\n if(!is_parallel(l1, l2)) return false;\n t1[ord[i]] = t2[ord[i]];\n }\n return true;\n };\n auto is_four = [&](vector<Point> t1, vector<Point> t2, vector<int> ord) -> bool {\n Line l1 = Line(t1[ord[0]], t2[ord[0]]);\n Line l2 = Line(t1[ord[1]], t1[ord[2]]);\n if(is_parallel(l1, l2)) {\n vector<Point> t3 = t1;\n t3[ord[0]] = t2[ord[0]];\n\n Point dir1 = t3[ord[1]] - t2[ord[1]];\n Line l3 = Line(t3[ord[0]], t3[ord[0]] + dir1);\n Point dir2 = t3[ord[0]] - t3[ord[1]];\n Line l4 = Line(t3[ord[2]], t3[ord[2]] + dir2);\n if(is_intersect_ll(l3, l4)) return true;\n\n return false;\n }\n\n Point dir1 = t1[ord[0]] - t1[ord[2]];\n Line l3 = Line(t1[ord[1]], t1[ord[1]] + dir1);\n Point dir2 = t1[ord[0]] - t2[ord[0]];\n Line l4 = Line(t1[ord[2]], t1[ord[2]] + dir2);\n vector<Point> cand1 = intersection_ll(l3, l4);\n if(!cand1.empty()) {\n vector<Point> t3 = t1;\n t3[ord[1]] = cand1[0];\n vector<int> ord2(3);\n rep(i, 0, 3) ord2[i] = i;\n do {\n if(is_three(t3, t2, ord2)) return true;\n } while(next_permutation(ord2.begin(), ord2.end()));\n }\n\n Point dir3 = t1[ord[0]] - t1[ord[1]];\n Line l5 = Line(t1[ord[2]], t1[ord[2]] + dir3);\n Point dir4 = t1[ord[0]] - t2[ord[0]];\n Line l6 = Line(t1[ord[1]], t1[ord[1]] + dir4);\n vector<Point> cand2 = intersection_ll(l5, l6);\n if(!cand2.empty()) {\n vector<Point> t3 = t1;\n t3[ord[2]] = cand2[0];\n vector<int> ord2(3);\n rep(i, 0, 3) ord2[i] = i;\n do {\n if(is_three(t3, t2, ord2)) return true;\n } while(next_permutation(ord2.begin(), ord2.end()));\n }\n\n return false;\n };\n vector<int> perm(3);\n rep(i, 0, 3) perm[i] = i;\n int ans = 5;\n do {\n vector<Point> t2 = p2;\n vector<Point> t1(3);\n rep(i, 0, 3) {\n t1[i] = p1[perm[i]];\n }\n if(ccw(t1[0], t1[1], t1[2]) != ccw(t2[0], t2[1], t2[2])) continue;\n vector<int> ord(3);\n rep(i, 0, 3) ord[i] = i;\n do {\n if(is_three(t1, t2, ord)) ans = min(ans, 3);\n if(is_four(t1, t2, ord)) ans = min(ans, 4);\n } while(next_permutation(ord.begin(), ord.end()));\n } while(next_permutation(perm.begin(), perm.end()));\n if(ans == 5) {\n cout << \"Many\";\n } else {\n cout << ans;\n }\n if(cin.eof()) break;\n cout << '\\n';\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3424, "score_of_the_acc": -0.7184, "final_rank": 4 }, { "submission_id": "aoj_1623_9282361", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\n#define all(A) A.begin(),A.end()\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\n\nbool OK = 1;\nbool is_diff(vll& PX, vll& PY, vll& QX, vll& QY, ll a, ll b, bool DIFF) {\n ll dx = PX[(a + 1) % 3] - PX[(a + 2) % 3];\n ll dy = PY[(a + 1) % 3] - PY[(a + 2) % 3];\n\n ll DX = PX[a] - QX[b];\n ll DY = PY[a] - QY[b];\n\n ll g = gcd(dx, dy);\n dx /= g;\n dy /= g;\n if (dx < 0) {\n dx *= -1;\n dy *= -1;\n }\n if (dx == 0 && dy <= 0)dy *= -1;\n\n ll G = gcd(DX, DY);\n DX /= G;\n DY /= G;\n if (DX < 0) {\n DX *= -1;\n DY *= -1;\n }\n if (DX == 0 && DY < 0)DY *= -1;\n if (dx == DX && dy == DY) {\n PX[a] = QX[b];\n PY[a] = QY[b];\n return 0;\n }\n\n\n\n\n if (DIFF)return 1;\n DX = QX[(b + 1) % 3] - QX[(b + 2) % 3];\n DY = QY[(b + 1) % 3] - QY[(b + 2) % 3];\n\n G = gcd(DX, DY);\n DX /= G;\n DY /= G;\n if (DX < 0) {\n DX *= -1;\n DY *= -1;\n }\n if (DX == 0 && DY < 0)DY *= -1;\n if (dx == DX && dy == DY) {\n OK = 0;\n return 1;\n }\n\n\n\n\n ll RHS = dy * DX - dx * DY;\n ll LHS = dy * DX * PX[a] - dx * DX * PY[a] - DY * dx * QX[b] + dx * DX * QY[b];\n ll GHS = gcd(RHS, LHS);\n RHS /= GHS;\n LHS /= GHS;\n // if (RHS < 0) {\n // RHS *= -1;\n // LHS *= -1;\n // }\n //nx=LHS/RHS\n //ny=dy/dx*(nx-PX[a])+PY[a]\n //ny=DY/DX*(nx-QX[b])+QY[b]\n if (dx != 0) {\n rep(i, 3) {\n if (i != a) {\n PX[i] *= (RHS * dx);\n PY[i] *= (RHS * dx);\n }\n if (i != b) {\n QX[i] *= (RHS * dx);\n QY[i] *= (RHS * dx);\n }\n }\n ll u = PX[a];\n ll v = PY[a];\n PX[a] = QX[b] = LHS * dx;\n //DY*(LHS-u*RHS)+v*DX*RHS;\n PY[a] = QY[b] = dy * (LHS - u * RHS) + v * dx * RHS;\n }\n else {\n rep(i, 3) {\n if (i != a) {\n PX[i] *= (RHS * DX);\n PY[i] *= (RHS * DX);\n }\n if (i != b) {\n QX[i] *= (RHS * DX);\n QY[i] *= (RHS * DX);\n }\n }\n //ny=DY/DX*(nx-QX[b])+QY[b]\n ll u = QX[b];\n ll v = QY[b];\n PX[a] = QX[b] = LHS * DX;\n PY[a] = QY[b] = DY * (LHS - u * RHS) + v * DX * RHS;\n }\n return 1;\n}\n\nbool eq(vll& PX, vll& PY, vll& QX, vll& QY) {\n vector<pair<ll, ll>> P(3), Q(3);\n rep(i, 3) {\n P[i] = { PX[i],PY[i] };\n Q[i] = { QX[i],QY[i] };\n }\n sort(all(P));\n sort(all(Q));\n return P == Q;\n}\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n\n\n vector<ll> X(6), Y(6);\n while (cin >> X[0] >> Y[0]) {\n rep(i, 5)cin >> X[i + 1] >> Y[i + 1];\n\n ll an = 5;\n\n rep(uu,2){\n rep(aa, 9)rep(bb, 9)rep(cc, 9)rep(dd, 9) {\n if (aa / 3 == bb / 3 || bb / 3 == cc / 3 || dd / 3 == cc / 3)continue;\n vector<ll> PX, PY, QX, QY;\n rep(i, 3) {\n PX.push_back(X[i]);\n PY.push_back(Y[i]);\n QX.push_back(X[i + 3]);\n QY.push_back(Y[i + 3]);\n }\n bool DIFF = 0;\n vll MV = { aa,bb,cc,dd };\n rep(p, 4) {\n if (aa == 0 && bb == 3 && cc <= 16) {\n cout << \"\";\n }\n if (!eq(PX, PY, QX, QY)) {\n ll a = MV[p] / 3;\n ll b = MV[p] % 3;\n OK = 1;\n if (PX[a] != QX[b] || PY[a] != QY[b]) {\n bool dif = is_diff(PX, PY, QX, QY, a, b, DIFF);\n if (DIFF && dif)OK = 0;\n if (dif)DIFF = 1;\n if (!OK) {\n continue;\n }\n }\n }\n }\n\n if (eq(PX, PY, QX, QY)) {\n // if (!DIFF)cout << aa << \" \" << bb << \" \" << cc << \" \" << dd << endl;\n if (!DIFF)an = min(an, 3ll);\n else an = min(an, 4ll);\n }\n\n }\n rep(i,3){\n swap(X[i],X[i+3]);\n swap(Y[i],Y[i+3]);\n }\n\n }\n if (an > 4)cout << \"Many\" << endl;\n else cout << an << endl;\n\n }\n\n\n\n}", "accuracy": 1, "time_ms": 7370, "memory_kb": 3460, "score_of_the_acc": -1.8, "final_rank": 20 }, { "submission_id": "aoj_1623_9072252", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\nusing i128 = __int128_t;\ntemplate < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }\ntemplate < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }\ntemplate < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); }\ntemplate < class T, class U > T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); }\nint popcnt(i32 x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n#line 2 \"cp-library/src/utility/rep_itr.hpp\"\ntemplate < class T > struct itr_rep {\n T i, d;\n constexpr itr_rep(const T i) noexcept : i(i), d(1) {}\n constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}\n void operator++() noexcept { i += d; }\n constexpr int operator*() const noexcept { return i; }\n constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }\n};\n\ntemplate < class T > struct rep {\n const itr_rep< T > s, t;\n constexpr rep(const T t) noexcept : s(0), t(t) {}\n constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}\n constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n\ntemplate < class T > struct revrep {\n const itr_rep < T > s, t;\n constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}\n constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}\n constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}\n constexpr auto begin() const noexcept { return s; }\n constexpr auto end () const noexcept { return t; }\n};\n#line 3 \"cp-library/src/utility/io.hpp\"\n\n/* 128bit integer */\nistream& operator>>(istream& is, i128& x) {\n std::string s; is >> s;\n int pm = (s[0] == '-');\n x = 0;\n for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');\n if(pm) x *= -1;\n return is;\n}\nostream& operator<<(ostream& os, const i128& x) {\n if(x == 0) return os << '0';\n i128 y = x;\n if(y < 0) { os << '-'; y *= -1; }\n std::vector<int> ny;\n while(y > 0) { ny.push_back(y % 10); y /= 10; }\n for(int i : revrep(ny.size())) os << ny[i];\n return os;\n}\n\ntemplate < class S, class T > istream& operator>>(istream& is, std::pair< S, T >& x) { is >> x.first >> x.second; return is; }\ntemplate < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << \" \" << x.second; return os; }\n\nnamespace scanner {\n struct sca {\n template < class T > operator T() {\n T s; std::cin >> s; return s;\n }\n };\n struct vec {\n int n;\n vec(int n) : n(n) {}\n template < class T > operator std::vector< T >() {\n std::vector< T > v(n);\n for(T& x : v) std::cin >> x;\n return v;\n }\n };\n struct mat {\n int h, w;\n mat(int h, int w) : h(h), w(w) {}\n template < class T > operator std::vector< std::vector< T > >() {\n std::vector m(h, std::vector< T >(w));\n for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;\n return m;\n }\n };\n struct speedup {\n speedup() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(0);\n }\n } speedup_instance;\n}\nscanner::sca in() { return scanner::sca(); }\nscanner::vec in(int n) { return scanner::vec(n); }\nscanner::mat in(int h, int w) { return scanner::mat(h, w); }\n\nnamespace printer {\n void precision(int d) { std::cout << std::fixed << std::setprecision(d); }\n void flush() { std::cout.flush(); }\n}\n\ntemplate < class T >\nostream& operator<<(ostream& os, const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }\n return os;\n}\n\nint print() { std::cout << '\\n'; return 0; }\ntemplate < class head, class... tail > int print(head&& h, tail&&... t) {\n std::cout << h; if(sizeof...(tail)) std::cout << ' ';\n return print(std::forward<tail>(t)...);\n}\ntemplate < class T > int print_n(const std::vector< T > a) {\n int n = a.size();\n for(int i : rep(n)) std::cout << a[i] << \"\\n\";\n return 0;\n}\n\n\n#line 2 \"cp-library/src/utility/key_val.hpp\"\n\ntemplate < class K, class V >\nstruct key_val {\n K key; V val;\n key_val() {}\n key_val(K key, V val) : key(key), val(val) {}\n template < std::size_t Index >\n std::tuple_element_t< Index, key_val >& get() {\n if constexpr (Index == 0) return key;\n if constexpr (Index == 1) return val;\n }\n};\n\nnamespace std {\n\ntemplate < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};\ntemplate < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };\ntemplate < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };\n\n}\n#line 2 \"cp-library/src/utility/vec_op.hpp\"\ntemplate < class T > key_val< int, T > max_of(const vector< T >& a) {\n int i = std::max_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class T > key_val< int, T > min_of(const vector< T >& a) {\n int i = std::min_element(a.begin(), a.end()) - a.begin();\n return {i, a[i]};\n}\ntemplate < class S, class T > S sum_of(const vector< T >& a) {\n S sum = 0;\n for(const T x : a) sum += x;\n return sum;\n}\ntemplate < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {\n vector< S > res(R - L, S(0));\n for(const T x : a) res[x - L] += 1;\n return res;\n}\ntemplate < class S, class T > struct prefix_sum {\n vector< S > s;\n prefix_sum(const vector< T >& a) : s(a) {\n s.insert(s.begin(), S(0));\n for(int i : rep(a.size())) s[i + 1] += s[i];\n }\n // [L, R)\n S sum(int L, int R) { return s[R] - s[L]; }\n};\n#line 3 \"cp-library/src/utility/heap.hpp\"\n\ntemplate < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;\ntemplate < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;\n\n#line 27 \"cp-library/src/cp-template.hpp\"\n\n#line 1 \"cp-library/src/algorithm/bin_search.hpp\"\ntemplate < class T, class F >\nT bin_search(T ok, T ng, F f) {\n while(abs(ng - ok) > 1) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n\ntemplate < class T, class F >\nT bin_search_real(T ok, T ng, F f, int step = 80) {\n while(step--) {\n T mid = (ok + ng) / 2;\n (f(mid) ? ok : ng) = mid;\n }\n return ok;\n}\n#line 2 \"cp-library/src/algorithm/argsort.hpp\"\n\ntemplate < class T > std::vector< int > argsort(const std::vector< T > &a) {\n std::vector< int > ids((int)a.size());\n std::iota(ids.begin(), ids.end(), 0);\n std::sort(ids.begin(), ids.end(), [&](int i, int j) {\n return a[i] < a[j] || (a[i] == a[j] && i < j);\n });\n return ids;\n}\n#line 1 \"macro.hpp\"\nnamespace macro {\n\nusing size_type = int;\ntemplate < class container > void sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }\ntemplate < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }\ntemplate < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }\ntemplate < class container > void unique(container& a) {\n std::sort(std::begin(a), std::end(a));\n a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));\n}\ntemplate < class container > container sorted(const container& a) { container b = a; sort(b); return std::move(b); }\ntemplate < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }\ntemplate < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }\ntemplate < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }\ntemplate < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }\ntemplate < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }\n\nconst std::vector<std::pair<size_type, size_type>> dir4 = { {+1, 0}, {-1, 0}, { 0, +1}, { 0, -1} };\nconst std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1, 0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1, 0}, {+1, +1} };\n\n#ifdef _DEBUG\n#define debug(x) std::cout << \"[\" << __LINE__ << \"] \" << #x << \": \" << x << std::endl\n#else\n#define debug(x)\n#endif\n\ntemplate < class container > void concat(container& a, const container& b) {\n a.insert(std::end(a), std::begin(b), std::end(b));\n}\nstd::vector<size_type> iota(const size_type n) {\n std::vector<size_type> I(n);\n std::iota(std::begin(I), std::end(I), 0);\n return I;\n}\ntemplate < class container > std::vector<size_type> sort_idx(const container& a) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });\n return I;\n}\ntemplate < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {\n const size_type n = a.size();\n std::vector<size_type> I = iota(n);\n std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });\n return std::move(I);\n}\n\nstruct grid {\n using size_type = int;\n size_type H, W;\n grid(const size_type H, const size_type W) : H(H), W(W) {}\n bool contains(const size_type i, const size_type j) {\n return 0 <= i and i < H and 0 <= j and j < W;\n }\n};\n\nusing f64 = long double;\n\n} // namespace macro\n\nusing namespace macro;\n#line 3 \"A.cpp\"\n\ntemplate < class T > int sign(const T x) {\n T e = (is_integral_v< T > ? 1 : 1e-8);\n if(x <= -e) return -1;\n if(x >= +e) return +1;\n return 0;\n}\ntemplate < class T > bool equals(const T& a, const T& b) { return sign(a - b) == 0; }\ntemplate < class T > struct point {\n T x, y;\n point() : x(0), y(0) {}\n point(T x, T y) : x(x), y(y) {}\n point(std::pair< T, T > p) : x(p.first), y(p.second) {}\n point& operator+=(const point& p) { x += p.x, y += p.y; return *this; }\n point& operator-=(const point& p) { x -= p.x, y -= p.y; return *this; }\n point& operator*=(const T r) { x *= r, y *= r; return *this; }\n point& operator/=(const T r) { x /= r, y /= r; return *this; }\n point operator+(const point& p) const { return point(*this) += p; }\n point operator-(const point& p) const { return point(*this) -= p; }\n point operator*(const T r) const { return point(*this) *= r; }\n point operator/(const T r) const { return point(*this) /= r; }\n point operator-() const { return {-x, -y}; }\n bool operator==(const point& p) const { return equals(x, p.x) and equals(y, p.y); }\n bool operator!=(const point& p) const { return !equals(x, p.x) or !equals(y, p.y); }\n bool operator<(const point& p) const { return x == p.x ? y < p.y : x < p.x; }\n point< T > rot(double theta) {\n static_assert(is_floating_point_v< T >);\n double cos_ = std::cos(theta), sin_ = std::sin(theta);\n return {cos_ * x - sin_ * y, sin_ * x + cos_ * y};\n }\n};\ntemplate < class T > istream& operator>>(istream& is, point< T >& p) { return is >> p.x >> p.y; }\ntemplate < class T > ostream& operator<<(ostream& os, point< T >& p) { return os << p.x << \" \" << p.y; }\ntemplate < class T > T dot(const point< T >& a, const point< T >& b) { return a.x * b.x + a.y * b.y; }\ntemplate < class T > T det(const point< T >& a, const point< T >& b) { return a.x * b.y - a.y * b.x; }\ntemplate < class T > T norm(const point< T >& p) { return p.x * p.x + p.y * p.y; }\ntemplate < class T > double abs(const point< T >& p) { return std::sqrt(norm(p)); }\ntemplate < class T > double angle(const point< T >& p) { return std::atan2(p.y, p.x); }\n\ntemplate < class T > int ccw(const point< T >& a, point< T > b, point< T > c) {\n b -= a, c -= a;\n if(sign(det(b, c)) == +1) return +1; // counter clockwise\n if(sign(det(b, c)) == -1) return -1; // clockwise\n if(sign(dot(b, c)) == -1) return +2; // c-a-b\n if(norm(b) < norm(c)) return -2; // a-b-c\n return 0; // a-c-b\n}\n\ntemplate < class T > struct line {\n point< T > a, b;\n line() {}\n line(point< T > a, point< T > b) : a(a), b(b) {}\n};\ntemplate < class T > point< T > projection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return l.a + (l.a - l.b) * dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n}\ntemplate < class T > point< T > reflection(const line< T >& l, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n return p + (projection(l, p) - p) * T(2);\n}\ntemplate < class T > bool orthogonal(const line< T >& a, const line< T >& b) { return equals(dot(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > bool parallel (const line< T >& a, const line< T >& b) { return equals(det(a.b - a.a, b.b - b.a), T(0)); }\ntemplate < class T > point< T > cross_point_ll(const line< T >& l, const line< T >& m) {\n static_assert(is_floating_point_v< T >);\n T A = det(l.b - l.a, m.b - m.a);\n T B = det(l.b - l.a, l.b - m.a);\n if(equals(abs(A), T(0)) and equals(abs(B), T(0))) return m.a;\n return m.a + (m.b - m.a) * B / A;\n}\n\ntemplate < class T > using segment = line< T >;\ntemplate < class T > bool intersect_ss(const segment< T >& s, const segment< T >& t) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 and ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;\n}\ntemplate < class T > double distance_sp(const segment< T >& s, const point< T >& p) {\n static_assert(is_floating_point_v< T >);\n point r = projection(s, p);\n if(ccw(s.a, s.b, r) == 0) return abs(r - p);\n return std::min(abs(s.a - p), abs(s.b - p));\n}\ntemplate < class T > double distance_ss(const segment< T >& a, const segment< T >& b) {\n if(intersect_ss(a, b)) return 0;\n return std::min({ distance_sp(a, b.a), distance_sp(a, b.b), distance_sp(b, a.a), distance_sp(b, a.b) });\n}\n\ntemplate < class T > using polygon = std::vector< point< T > >;\ntemplate < class T > T area2(const polygon< T >& p) {\n T s = 0;\n int n = p.size();\n for(int i = 0; i < n; i++) s += det(p[i], p[(i + 1) % n]);\n return s;\n}\ntemplate < class T > T area(const polygon< T >& p) { return area2(p) / T(2); }\n\ntemplate < class T > bool is_convex(const polygon< T >& p) {\n int n = p.size();\n for(int i = 0; i < n; i++) if(ccw(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]) == -1) return false;\n return true;\n}\ntemplate < class T > int contains(const polygon< T >& g, const point< T >& p) {\n int n = g.size();\n bool in = false;\n for(int i = 0; i < n; i++) {\n point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(sign(a.y - b.y) == +1) std::swap(a, b);\n if(sign(a.y) <= 0 and sign(b.y) ==+1 and sign(det(a, b)) == -1) in = !in;\n if(sign(det(a, b)) == 0 and sign(dot(a, b)) <= 0) return 1; // ON\n }\n return in ? 2 : 0;\n}\ntemplate < class T > polygon< T > convex_cut(const polygon< T >& p, const line< T >& l) {\n int n = p.size();\n polygon< T > res;\n for(int i = 0; i < n; i++) {\n point now = p[i], nxt = p[(i + 1) % n];\n if(ccw(l.a, l.b, now) != -1) res.push_back(now);\n if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) res.push_back(cross_point_ll(line(now, nxt), l));\n }\n return res;\n}\ntemplate < class T > polygon< T > convex_hull(polygon< T >& p) {\n int n = p.size(), k = 0;\n if(n <= 2) return p;\n std::sort(p.begin(), p.end());\n polygon< T > ch(n + n);\n for(int i = 0; i < n; ch[k++] = p[i++])\n while(k >= 2 and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--])\n while(k >= t and sign(det(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1])) == -1) k--;\n ch.resize(k - 1);\n return ch;\n}\ntemplate < class T > T diameter2(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n int n = p.size(), is = 0, js = 0;\n for(int i = 1; i < n; i++) {\n if(sign(p[i].y - p[is].y) == +1) is = i;\n if(sign(p[i].y - p[js].y) == -1) js = i;\n }\n T dist_max = norm(p[is] - p[js]);\n int maxi = is, i = is, maxj = js, j = js;\n do {\n if(sign(det(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j])) >= 0) j = (j + 1) % n; else i = (i + 1) % n;\n if(norm(p[i] - p[j]) > dist_max) {\n dist_max = norm(p[i] - p[j]);\n maxi = i, maxj = j;\n }\n } while(i != is or j != js);\n return dist_max;\n}\ntemplate < class T > double diameter(const polygon< T >& p) {\n static_assert(is_floating_point_v< T >);\n return std::sqrt(diameter2(p));\n}\n\ntemplate < class T > struct circle {\n point< T > p;\n T r;\n circle() = default;\n circle(point< T > p, T r) : p(p), r(r) {}\n};\ntemplate < class T > istream& operator>>(istream& is, circle< T >& c) { return is >> c.p >> c.r; }\ntemplate < class T > int intersect_cc(circle< T > c1, circle< T > c2) {\n if(c1.r < c2.r) std::swap(c1, c2);\n T d = abs(c1.p - c2.p);\n if(sign(c1.r + c2.r - d) == -1) return 4;\n if(equals(c1.r + c2.r, d)) return 3;\n if(sign(c1.r - c2.r - d) == -1) return 2;\n if(equals(c1.r - c2.r, d)) return 1;\n return 0;\n}\ntemplate < class T > std::pair<point< T >, point< T >> cross_point_cc(const circle< T >& c1, const circle< T >& c2) {\n T d = abs(c1.p - c2.p);\n T a = std::acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));\n T t = angle(c2.p - c1.p);\n point< T > p1 = c1.p + point< T >(std::cos(t + a), std::sin(t + a)) * c1.r;\n point< T > p2 = c1.p + point< T >(std::cos(t - a), std::sin(t - a)) * c1.r;\n return {p1, p2};\n}\n\nint solve(vector<point<f64>> P_, vector<point<f64>> Q_) {\n int ans = 5;\n vector<int> I = {0, 1, 2}, J = {0, 1, 2};\n do { do {\n auto dfs = [&](auto self, vector<point<f64>> P, vector<point<f64>> Q, int i) -> int {\n if(i == 0) {\n if(P[0] == Q[0]) return 0 + self(self, P, Q, i + 1);\n if(parallel(line<f64>(P[0], Q[0]), line<f64>(P[1], P[2]))) {\n P[0] = Q[0];\n return 1 + self(self, P, Q, i + 1);\n } else {\n int ans = 100;\n for(int j : {1, 2}) {\n const int k = 3 - j;\n line<f64> l0(P[j], P[j] + (P[k] - P[0]));\n line<f64> l1(P[k], P[k] + (P[0] - Q[0]));\n if(parallel(l0, l1)) continue;\n point<f64> p = cross_point_ll<f64>(l0, l1);\n vector<point<f64>> nP = P;\n nP[j] = p;\n nP[0] = Q[0];\n chmin(ans, 2 + self(self, nP, Q, i + 1));\n }\n return ans;\n }\n }\n\n if(i == 1) {\n if(P[1] == Q[1]) return 0 + self(self, P, Q, i + 1);\n if(parallel(line<f64>(P[1], Q[1]), line<f64>(P[0], P[2]))) {\n P[1] = Q[1];\n return 1 + self(self, P, Q, i + 1);\n } else {\n line<f64> l0(P[2], P[2] + (P[0] - P[1]));\n line<f64> l1(P[0], P[0] + (P[1] - Q[1]));\n if(parallel(l0, l1)) return 100;\n point<f64> p = cross_point_ll<f64>(l0, l1);\n P[2] = p;\n P[1] = Q[1];\n return 2 + self(self, P, Q, i + 1);\n }\n }\n\n if(i == 2) {\n if(P[2] == Q[2]) return 0;\n if(parallel(line<f64>(P[2], Q[2]), line<f64>(P[0], P[1]))) {\n return 1;\n } else {\n return 100;\n }\n }\n\n assert(0);\n };\n\n vector<point<f64>> P(3), Q(3);\n for(int i : rep(3)) {\n P[i] = P_[I[i]];\n Q[i] = Q_[J[i]];\n }\n chmin(ans, dfs(dfs, P, Q, 0));\n } while(next_permutation(I.begin(), I.end())); } while(next_permutation(J.begin(), J.end()));\n return ans;\n}\n\nint main() {\n vector<point<f64>> P(3), Q(3);\n while(cin >> P[0].x) {\n cin >> P[0].y;\n for(int i : rep(1, 3)) cin >> P[i].x >> P[i].y;\n for(int i : rep(0, 3)) cin >> Q[i].x >> Q[i].y;\n int ans = solve(P, Q);\n print(ans == 5 ? \"Many\" : to_string(ans));\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3452, "score_of_the_acc": -0.7837, "final_rank": 8 }, { "submission_id": "aoj_1623_8016820", "code_snippet": "#include <bits/stdc++.h>\n#define rng(i, a, b) for (int i = (a); i < (b); i++)\n#define rep(i, n) rng(i, 0, (n))\n#define vec vector\nusing namespace std;\ntypedef long double D;\ntypedef complex<D> P;\ntypedef pair<P, P> L;\ntypedef vector<P> VP;\nconst D EPS = 1e-9;\n#define X real()\n#define Y imag()\n#define EQ(n, m) (abs((n) - (m)) < EPS)\nD dot(P a, P b)\n{\n return (conj(a) * b).X;\n}\nD cross(P a, P b)\n{\n return (conj(a) * b).Y;\n}\nP crosspointLL(P a1, P a2, P b1, P b2)\n{\n D d1 = cross(b2 - b1, b1 - a1);\n D d2 = cross(b2 - b1, a2 - a1);\n if (EQ(d1, 0) && EQ(d2, 0))\n return a1;\n if (EQ(d2, 0))\n return P(5000, 5000);\n return a1 + d1 / d2 * (a2 - a1);\n}\nVP inx, y;\nint solve(VP x, int cnt)\n{\n\n vector<bool> okx(3, false), oky(3, false);\n rep(i, 3) rep(j, 3)\n {\n if (oky[j])\n continue;\n if (EQ(x[i], y[j]))\n {\n okx[i] = true;\n oky[j] = true;\n }\n }\n bool ok = true;\n rep(i, 3)\n {\n if (okx[i] == false)\n ok = false;\n }\n if (ok)\n {\n return 0;\n }\n int res = 5;\n rep(i, 3) rep(j, 3)\n {\n if (okx[i] || oky[j])\n continue;\n // x[i]をy[j]に移動\n if (EQ(cross(x[(i + 1) % 3] - x[(i + 2) % 3], y[j] - x[i]), 0))\n {\n VP v = x;\n v[i] = y[j];\n res = min(res, 1 + solve(v, cnt));\n }\n else\n {\n if (okx[(i + 1) % 3] == false)\n {\n VP v = x;\n P z = crosspointLL(x[(i + 2) % 3], x[(i + 2) % 3] + y[j] - x[i], x[(i + 1) % 3], x[(i + 1) % 3] + x[i] - x[(i + 2) % 3]);\n v[(i + 1) % 3] = z;\n if (cnt == 0)\n res = min(res, 1 + solve(v, cnt + 1));\n }\n if (okx[(i + 2) % 3] == false)\n {\n VP v = x;\n P z = crosspointLL(x[(i + 1) % 3], x[(i + 1) % 3] + y[j] - x[i], x[(i + 2) % 3], x[(i + 2) % 3] + x[i] - x[(i + 1) % 3]);\n v[(i + 2) % 3] = z;\n if (cnt == 0)\n res = min(res, 1 + solve(v, cnt + 1));\n }\n }\n }\n // rep(i, 3)\n // {\n // cout << x[i].X << \" \" << x[i].Y << endl;\n // }\n // cout << endl;\n // cout << res << endl;\n return res;\n}\n\nsigned main()\n{\n int x0;\n while (cin >> x0)\n {\n inx.clear();\n y.clear();\n for (int i = 0; i < 3; i++)\n {\n if (i == 0)\n {\n int v;\n cin >> v;\n inx.emplace_back(x0, v);\n }\n else\n {\n int u, v;\n cin >> u >> v;\n inx.emplace_back(u, v);\n }\n }\n for (int i = 0; i < 3; i++)\n {\n int u, v;\n cin >> u >> v;\n y.emplace_back(u, v);\n }\n int ans = solve(inx, 0);\n if (ans >= 5)\n cout << \"Many\" << endl;\n else\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3436, "score_of_the_acc": -0.7537, "final_rank": 7 }, { "submission_id": "aoj_1623_8013089", "code_snippet": "#include <algorithm>\n#include <climits>\n#include <iostream>\n#include <numeric>\n#include <utility>\n#include <vector>\n\nusing namespace std;\nusing uint = unsigned int;\nusing ll = long long;\nusing ull = unsigned long long;\nusing lint = ll;\nusing ulint = ull;\nusing pii = pair<int, int>;\nusing pll = pair<lint, lint>;\ntemplate <class T>\nusing V = vector<T>;\ntemplate <class T>\nusing VV = V<V<T>>;\n#define For(i, a, b) for (int i = int(a); i < int(b); ++i)\n#define rep(i, n) For(i, 0, n)\ntemplate <class T, class U>\nbool chmin(T &a, const U &b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\nbool chmax(T &a, const U &b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\nT div_floor(T a, T b) {\n if (b < 0) a = -a, b = -b;\n return a >= 0 ? a / b : (a + 1) / b - 1;\n}\ntemplate <class T>\nT div_ceil(T a, T b) {\n if (b < 0) a = -a, b = -b;\n return a <= 0 ? a / b : (a - 1) / b + 1;\n}\n\ntemplate <class T>\nostream &operator<<(ostream &os, const V<T> &v) {\n os << \"[\";\n for (auto d : v) os << d << \", \";\n return os << \"]\";\n}\n\n#include <cmath>\nusing re = long double;\nre EPS = 1e-9;\n\nint sgn(re x) {\n if (x < -EPS) return -1;\n if (x > EPS) return 1;\n return 0;\n}\nbool eq(re x, re y) { return sgn(x - y) == 0; }\nbool ge(re x, re y) { return sgn(x - y) == 1; }\nbool le(re x, re y) { return sgn(x - y) == -1; }\nbool geq(re x, re y) { return sgn(x - y) >= 0; }\nbool leq(re x, re y) { return sgn(x - y) <= 0; }\n\nstruct pt {\n re x, y;\n\n pt(re x = 0, re y = 0) : x(x), y(y) {}\n\n bool operator==(const pt &p) const { return eq(x, p.x) && eq(y, p.y); }\n bool operator<(const pt &p) const {\n if (eq(x, p.x)) {\n return le(y, p.y);\n }\n return le(x, p.x);\n }\n\n pt &operator+=(const pt &r) {\n this->x += r.x;\n this->y += r.y;\n return *this;\n }\n pt &operator-=(const pt &r) {\n this->x -= r.x;\n this->y -= r.y;\n return *this;\n }\n pt &operator*=(const re &r) {\n this->x *= r;\n this->y *= r;\n return *this;\n }\n pt &operator/=(const re &r) {\n this->x /= r;\n this->y /= r;\n return *this;\n }\n\n friend pt operator+(const pt &l, const pt &r) { return pt(l) += r; }\n friend pt operator-(const pt &l, const pt &r) { return pt(l) -= r; }\n friend pt operator*(const pt &l, const re &r) { return pt(l) *= r; }\n friend pt operator/(const pt &l, const re &r) { return pt(l) /= r; }\n\n friend re operator*(const pt &l, const pt &r) {\n return l.x * r.x + l.y * r.y;\n }\n friend re operator^(const pt &l, const pt &r) {\n return l.x * r.y - l.y * r.x;\n }\n};\nostream &operator<<(ostream &os, const pt &p) {\n os << \"(\";\n os << p.x << \", \" << p.y;\n return os << \")\";\n}\n\nusing Vec = pt;\nusing Pts = V<pt>;\n\nre norm(pt p) { return p * p; }\nre abs(pt p) { return sqrt(norm(p)); }\nre arg(pt p) { return atan2(p.y, p.x); }\nre angle(pt a, pt b) { return arg({a * b, a ^ b}); }\n\nstruct Line {\n pt a;\n Vec v;\n\n Line() {}\n Line(pt a, Vec v) : a(a), v(v) {}\n};\n\npt intersection(Line l1, Line l2) {\n auto [a, v] = l1;\n auto [b, w] = l2;\n auto t = ((b - a) ^ w) / (v ^ w);\n return a + v * t;\n}\n\nint main() {\n auto dfs = [&](auto self, int cur, Pts ps, Pts qs, int num, int &ans) {\n if (cur == 3) {\n chmin(ans, num);\n return;\n }\n auto a = ps[cur], b = ps[(cur + 1) % 3], c = ps[(cur + 2) % 3];\n if (a == qs[cur]) {\n self(self, cur + 1, ps, qs, num, ans);\n return;\n }\n\n auto v = qs[cur] - a;\n if (eq(v ^ (b - c), 0)) {\n ps[cur] = qs[cur];\n self(self, cur + 1, ps, qs, num + 1, ans);\n return;\n }\n\n {\n auto vs = ps;\n auto p = intersection(Line(c, v), Line(b, c - a));\n ps[cur] = qs[cur];\n ps[(cur + 1) % 3] = p;\n self(self, cur + 1, vs, qs, num + 2, ans);\n }\n {\n auto vs = ps;\n auto p = intersection(Line(b, v), Line(c, b - a));\n ps[cur] = qs[cur];\n ps[(cur + 2) % 3] = p;\n self(self, cur + 1, vs, qs, num + 2, ans);\n }\n };\n\n Pts ps(3), qs(3);\n while (cin >> ps[0].x) {\n cin >> ps[0].y;\n For(i, 1, 3) cin >> ps[i].x >> ps[i].y;\n rep(i, 3) cin >> qs[i].x >> qs[i].y;\n\n int ans = 100;\n sort(ps.begin(), ps.end());\n do {\n sort(qs.begin(), qs.end());\n do {\n dfs(dfs, 0, ps, qs, 0, ans);\n\n } while (next_permutation(qs.begin(), qs.end()));\n } while (next_permutation(ps.begin(), ps.end()));\n\n if (ans >= 5) {\n puts(\"Many\");\n } else {\n printf(\"%d\\n\", ans);\n }\n }\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3536, "score_of_the_acc": -0.9891, "final_rank": 15 }, { "submission_id": "aoj_1623_7783144", "code_snippet": "#line 1 \"test/geometry/base_rational.test.cpp\"\n#define PROBLEM \"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1623&lang=jp\"\n\n#line 2 \"template/template.hpp\"\n\n#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = s; i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <typename T> bool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\nnamespace lib {\n\nusing namespace std;\n\n} // namespace lib\n\n// using namespace lib;\n#line 2 \"utility/rational.hpp\"\n\n#line 4 \"utility/rational.hpp\"\n\nnamespace lib{\n\nstruct rational{\n rational() : p(0), q(1) {}\n rational(ll n) : p(n), q(1) {}\n rational(ll n, ll m){\n assert(m != 0);\n if (m < 0) n = -n, m = -m;\n ll g = gcd(n,m);\n p = n / g; q = m / g;\n }\n ld val(){\n return ld(p) / ld(q);\n }\n rational& operator+=(const rational& rhs){\n ll g = gcd(q,rhs.q);\n ll np = rhs.q / g * p + q / g * rhs.p;\n ll nq = q / g * rhs.q;\n ll ng = gcd(np,nq);\n p = np/ng, q = nq/ng;\n return *this; \n }\n rational& operator-=(const rational& rhs){\n (*this) += rational(-rhs.p,rhs.q);\n return *this;\n }\n rational& operator*=(const rational& rhs){\n ll g1 = gcd(q,rhs.p), g2 = gcd(p,rhs.q);\n ll np = p / g2 * rhs.p / g1;\n ll nq = q / g1 * rhs.q / g2;\n p = np, q = nq;\n return *this;\n }\n rational& operator/=(const rational& rhs){\n (*this) *= rational(rhs.q,rhs.p);\n return *this;\n }\n rational operator+() const {return *this;}\n rational operator-() const {return rational() - *this;}\n friend rational operator+(const rational& lhs, const rational& rhs) {\n return rational(lhs) += rhs;\n }\n friend rational operator-(const rational& lhs, const rational& rhs) {\n return rational(lhs) -= rhs;\n }\n friend rational operator*(const rational& lhs, const rational& rhs) {\n return rational(lhs) *= rhs;\n }\n friend rational operator/(const rational& lhs, const rational& rhs) {\n return rational(lhs) /= rhs;\n }\n friend bool operator==(const rational& lhs, const rational& rhs) {\n return lhs.p == rhs.p && lhs.q == rhs.q;\n }\n friend bool operator!=(const rational& lhs, const rational& rhs) {\n return lhs.p != rhs.p || lhs.q != rhs.q;\n }\n friend bool operator< (const rational lhs, const rational rhs) {\n return less_than(lhs,rhs);\n }\n friend bool operator> (const rational lhs, const rational rhs) {\n return less_than(rhs,lhs);\n }\n friend bool operator<= (const rational lhs, const rational rhs) {\n return lhs == rhs || lhs < rhs;\n }\n friend bool operator>= (const rational lhs, const rational rhs) {\n return lhs == rhs || lhs > rhs;\n }\n friend std::ostream &operator<<(std::ostream &os,const rational&r) {\n return os << r.p << \" / \" << r.q;\n }\n private:\n ll p, q;\n static bool less_than(rational lhs, rational rhs){\n __int128_t lv = __int128_t(lhs.p) * __int128_t(rhs.q);\n __int128_t rv = __int128_t(lhs.q) * __int128_t(rhs.p);\n return lv < rv;\n }\n};\n\n\n} // namespace lib\n#line 2 \"geometry/base_arbitary.hpp\"\n\n#line 4 \"geometry/base_arbitary.hpp\"\n\nnamespace lib {\n\ntemplate<typename T>\nstruct Vec {\n T x, y;\n Vec (T _x = T(0), T _y = T(0)) : x(_x), y(_y) {}\n Vec& operator*=(const T& a){\n x *= a;\n y *= a;\n return *this;\n }\n Vec& operator/=(const T& a){\n x /= a;\n y /= a;\n return *this;\n }\n Vec& operator+=(const Vec& rhs) {\n x += rhs.x;\n y += rhs.y;\n return *this;\n }\n Vec& operator-=(const Vec& rhs) {\n x -= rhs.x;\n y -= rhs.y;\n return *this;\n }\n friend bool operator==(const Vec& lhs, const Vec& rhs) {\n return lhs.x == rhs.x && lhs.y == rhs.y;\n }\n friend bool operator!=(const Vec& lhs, const Vec& rhs) {\n return lhs.x != rhs.x || lhs.y != rhs.y;\n }\n friend Vec operator+(const Vec& lhs, const Vec& rhs) {\n return Vec(lhs) += rhs;\n }\n friend Vec operator-(const Vec& lhs, const Vec& rhs) {\n return Vec(lhs) -= rhs;\n }\n friend Vec operator*(const Vec& lhs, const T& rhs) {\n return Vec(lhs) *= rhs;\n }\n friend Vec operator*(const T& rhs, const Vec& lhs) {\n return Vec(lhs) *= rhs;\n }\n friend Vec operator/(const Vec& lhs, const T& rhs) {\n return Vec(lhs) /= rhs;\n }\n friend Vec operator/(const T& rhs, const Vec& lhs) {\n return Vec(lhs) /= rhs;\n }\n};\n\ntemplate<typename T>\nT dot(const Vec<T> &a, const Vec<T> &b){\n return a.x * b.x + a.y * b.y;\n}\n\n// cross > 0 : counter clockwise a -> b\ntemplate<typename T>\nT cross(const Vec<T> &a, const Vec<T> &b){\n return a.x * b.y - a.y * b.x;\n}\n\ntemplate<typename T>\nld abs(const Vec<T> &a){\n return sqrtl(a.x*a.x+a.y*a.y);\n}\n\ntemplate<typename T>\nT norm(const Vec<T> &a){\n return a.x*a.x+a.y*a.y;\n}\n\ntemplate<typename T>\nstruct Line {\n Vec<T> p, q;\n};\n\ntemplate<typename T>\nint intersection(const Line<T> &a, const Line<T> &b){\n if (cross(a.p-a.q,b.p-b.q) == 0){\n if (cross(a.p-b.p,a.q-b.p) == 0) return 2;\n return 0;\n }\n return 1;\n}\n\n// intersection == 1 (cross(a.p-a.q,b.p-b.q) != 0)\ntemplate<typename T>\nVec<T> cross_point(const Line<T> &a, const Line<T> &b){\n Vec<T> va = a.p-a.q, vb = b.p-b.q;\n Vec<T> ba = b.p-a.q;\n T alpha = cross(ba,vb) / cross(va,vb);\n return alpha * a.p + (1 - alpha) * a.q;\n}\n\n\n} // namespace lib\n#line 6 \"test/geometry/base_rational.test.cpp\"\n\nusing namespace lib;\nusing vec = Vec<rational>;\nusing line = Line<rational>;\n\nconst vector<vector<int>> order = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};\n\nint main(){\n while (true){\n vector<vec> ia(3), b(3);\n rep(i,0,6){\n ll x, y; cin >> x >> y;\n if (!cin) return 0;\n (i < 3 ? ia[i] : b[i-3]) = vec(x,y);\n }\n int ans = 5;\n for (auto fid : order) for (auto tid : order) rep(j,0,2){\n auto a = ia;\n int cur = 0;\n rep(i,0,3){\n int f = fid[i], t = tid[i];\n if (a[f] == b[t]) continue;\n cur++;\n int p = (f+1)%3, q = (f+2)%3;\n if (cross(a[p]-a[q],b[t]-a[f]) == 0){\n a[f] = b[t];\n continue;\n }\n cur++;\n if (j == 1) swap(p,q);\n line l1({a[p],a[p]+a[f]-b[t]}), l2({a[q],a[q]+a[f]-a[p]});\n if (intersection(l1,l2) == 1){\n a[q] = cross_point(l1,l2);\n a[f] = b[t];\n }\n else {\n cur = 5;\n break;\n }\n }\n chmin(ans,cur);\n }\n if (ans == 5) cout << \"Many\" << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1530, "memory_kb": 3460, "score_of_the_acc": -1.0065, "final_rank": 18 }, { "submission_id": "aoj_1623_7783133", "code_snippet": "#line 1 \"test/geometry/base_rational.test.cpp\"\n#define PROBLEM \"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1623&lang=jp\"\n\n#line 2 \"template/template.hpp\"\n\n#include <bits/stdc++.h>\n\n#define rep(i, s, n) for (int i = s; i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n#define all(v) v.begin(), v.end()\n\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\ntemplate <typename T> bool chmin(T &a, const T &b) {\n if (a <= b) return false;\n a = b;\n return true;\n}\ntemplate <typename T> bool chmax(T &a, const T &b) {\n if (a >= b) return false;\n a = b;\n return true;\n}\n\nnamespace lib {\n\nusing namespace std;\n\n} // namespace lib\n\n// using namespace lib;\n#line 2 \"utility/rational.hpp\"\n\n#line 4 \"utility/rational.hpp\"\n\nnamespace lib{\n\nstruct rational{\n rational() : p(0), q(1) {}\n rational(ll n) : p(n), q(1) {}\n rational(ll n, ll m){\n assert(m != 0);\n if (m < 0) n = -n, m = -m;\n ll g = gcd(n,m);\n p = n / g; q = m / g;\n }\n ld val(){\n return ld(p) / ld(q);\n }\n rational& operator+=(const rational& rhs){\n ll g = gcd(q,rhs.q);\n ll np = rhs.q / g * p + q / g * rhs.p;\n ll nq = q / g * rhs.q;\n ll ng = gcd(np,nq);\n p = np/ng, q = nq/ng;\n return *this; \n }\n rational& operator-=(const rational& rhs){\n (*this) += rational(-rhs.p,rhs.q);\n return *this;\n }\n rational& operator*=(const rational& rhs){\n ll g1 = gcd(q,rhs.p), g2 = gcd(p,rhs.q);\n ll np = p / g2 * rhs.p / g1;\n ll nq = q / g1 * rhs.q / g2;\n p = np, q = nq;\n return *this;\n }\n rational& operator/=(const rational& rhs){\n (*this) *= rational(rhs.q,rhs.p);\n return *this;\n }\n rational operator+() const {return *this;}\n rational operator-() const {return rational() - *this;}\n friend rational operator+(const rational& lhs, const rational& rhs) {\n return rational(lhs) += rhs;\n }\n friend rational operator-(const rational& lhs, const rational& rhs) {\n return rational(lhs) -= rhs;\n }\n friend rational operator*(const rational& lhs, const rational& rhs) {\n return rational(lhs) *= rhs;\n }\n friend rational operator/(const rational& lhs, const rational& rhs) {\n return rational(lhs) /= rhs;\n }\n friend bool operator==(const rational& lhs, const rational& rhs) {\n return lhs.p == rhs.p && lhs.q == rhs.q;\n }\n friend bool operator!=(const rational& lhs, const rational& rhs) {\n return lhs.p != rhs.p || lhs.q != rhs.q;\n }\n friend bool operator< (const rational lhs, const rational rhs) {\n return less_than(lhs,rhs);\n }\n friend bool operator> (const rational lhs, const rational rhs) {\n return less_than(rhs,lhs);\n }\n friend bool operator<= (const rational lhs, const rational rhs) {\n return lhs == rhs || lhs < rhs;\n }\n friend bool operator>= (const rational lhs, const rational rhs) {\n return lhs == rhs || lhs > rhs;\n }\n friend std::ostream &operator<<(std::ostream &os,const rational&r) {\n return os << r.p << \" / \" << r.q;\n }\n private:\n ll p, q;\n static bool less_than(rational lhs, rational rhs){\n __int128_t lv = __int128_t(lhs.p) * __int128_t(rhs.q);\n __int128_t rv = __int128_t(lhs.q) * __int128_t(rhs.p);\n return lv < rv;\n }\n};\n\n\n} // namespace lib\n#line 2 \"geometry/base_arbitary.hpp\"\n\n#line 4 \"geometry/base_arbitary.hpp\"\n\nnamespace lib {\n\ntemplate<typename T>\nstruct Vec {\n T x, y;\n Vec (T _x = T(0), T _y = T(0)) : x(_x), y(_y) {}\n Vec& operator*=(const T& a){\n x *= a;\n y *= a;\n return *this;\n }\n Vec& operator/=(const T& a){\n x /= a;\n y /= a;\n return *this;\n }\n Vec& operator+=(const Vec& rhs) {\n x += rhs.x;\n y += rhs.y;\n return *this;\n }\n Vec& operator-=(const Vec& rhs) {\n x -= rhs.x;\n y -= rhs.y;\n return *this;\n }\n friend bool operator==(const Vec& lhs, const Vec& rhs) {\n return lhs.x == rhs.x && lhs.y == rhs.y;\n }\n friend bool operator!=(const Vec& lhs, const Vec& rhs) {\n return lhs.x != rhs.x || lhs.y != rhs.y;\n }\n friend Vec operator+(const Vec& lhs, const Vec& rhs) {\n return Vec(lhs) += rhs;\n }\n friend Vec operator-(const Vec& lhs, const Vec& rhs) {\n return Vec(lhs) -= rhs;\n }\n friend Vec operator*(const Vec& lhs, const T& rhs) {\n return Vec(lhs) *= rhs;\n }\n friend Vec operator*(const T& rhs, const Vec& lhs) {\n return Vec(lhs) *= rhs;\n }\n friend Vec operator/(const Vec& lhs, const T& rhs) {\n return Vec(lhs) /= rhs;\n }\n friend Vec operator/(const T& rhs, const Vec& lhs) {\n return Vec(lhs) /= rhs;\n }\n};\n\ntemplate<typename T>\nT dot(const Vec<T> &a, const Vec<T> &b){\n return a.x * b.x + a.y * b.y;\n}\n\n// cross > 0 : counter clockwise a -> b\ntemplate<typename T>\nT cross(const Vec<T> &a, const Vec<T> &b){\n return a.x * b.y - a.y * b.x;\n}\n\ntemplate<typename T>\nld abs(const Vec<T> &a){\n return sqrtl(a.x*a.x+a.y*a.y);\n}\n\ntemplate<typename T>\nT norm(const Vec<T> &a){\n return a.x*a.x+a.y*a.y;\n}\n\ntemplate<typename T>\nstruct Line {\n Vec<T> p, q;\n};\n\ntemplate<typename T>\nint intersection(const Line<T> &a, const Line<T> &b){\n if (cross(a.p-a.q,b.p-b.q) == 0){\n if (cross(a.p-b.p,a.q-b.p) == 0) return 2;\n return 0;\n }\n return 1;\n}\n\n// intersection == 1\ntemplate<typename T>\nVec<T> cross_point(const Line<T> &a, const Line<T> &b){\n Vec<T> va = a.p-a.q, vb = b.p-b.q;\n Vec<T> ba = b.p-a.q;\n T alpha = cross(ba,vb) / cross(va,vb);\n return alpha * a.p + (1 - alpha) * a.q;\n}\n\n\n} // namespace lib\n#line 6 \"test/geometry/base_rational.test.cpp\"\n\nusing namespace lib;\nusing vec = Vec<rational>;\nusing line = Line<rational>;\n\nconst vector<vector<int>> order = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};\n\nint main(){\n while (true){\n vector<vec> ia(3), b(3);\n rep(i,0,6){\n ll x, y; cin >> x >> y;\n if (!cin) return 0;\n (i < 3 ? ia[i] : b[i-3]) = vec(x,y);\n }\n int ans = 5;\n for (auto fid : order) for (auto tid : order) rep(j,0,2){\n auto a = ia;\n int cur = 0;\n rep(i,0,3){\n int f = fid[i], t = tid[i];\n if (a[f] == b[t]) continue;\n cur++;\n int p = (f+1)%3, q = (f+2)%3;\n if (cross(a[p]-a[q],b[t]-a[f]) == 0){\n a[f] = b[t];\n continue;\n }\n cur++;\n if (j == 1) swap(p,q);\n line l1({a[p],a[p]+a[f]-b[t]}), l2({a[q],a[q]+a[f]-a[p]});\n if (intersection(l1,l2) == 1){\n a[q] = cross_point(l1,l2);\n a[f] = b[t];\n }\n else {\n cur = 5;\n break;\n }\n }\n chmin(ans,cur);\n }\n if (ans == 5) cout << \"Many\" << endl;\n else cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 1530, "memory_kb": 3316, "score_of_the_acc": -0.6637, "final_rank": 3 }, { "submission_id": "aoj_1623_7782179", "code_snippet": "#line 1 \"f.cpp\"\n#include<bits/stdc++.h>\n#define rep(i,n) for (int i = 0; i < int(n); ++i)\n#define repp(i,n,m) for (int i = m; i < int(n); ++i)\n#define reb(i,n) for (int i = int(n)-1; i >= 0; --i)\n#define all(v) v.begin(),v.end()\n\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int,int>;\nusing PL = pair<ll,ll>;\n\ntemplate<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}\ntemplate<typename T1, typename T2>void print(pair<T1,T2> a);\ntemplate<typename T>void print(vector<T> v);\ntemplate<typename T>void print(vector<vector<T>> v);\nvoid print(){ putchar(' '); }\nvoid print(bool a){ printf(\"%d\", a); }\nvoid print(int a){ printf(\"%d\", a); }\nvoid print(long a){ printf(\"%ld\", a); }\nvoid print(long long a){ printf(\"%lld\", a); }\nvoid print(char a){ printf(\"%c\", a); }\nvoid print(char a[]){ printf(\"%s\", a); }\nvoid print(const char a[]){ printf(\"%s\", a); }\nvoid print(long double a){ printf(\"%.15Lf\", a); }\nvoid print(const string& a){ for(auto&& i : a) print(i); }\nvoid print(unsigned int a){ printf(\"%u\", a); }\nvoid print(__int128_t a){print((long long)(a));}\ntemplate<class T> void print(const T& a){ cout << a;}\nint out(){ putchar('\\n'); return 0; }\ntemplate<class T> int out(const T& t){ print(t); putchar('\\n'); return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }\ntemplate<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}\ntemplate<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}\ntemplate<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}\n\ntemplate<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}\ntemplate<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\n\nvoid fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);}\n\nnamespace noya2{\n\nconst int INF = 1001001007;\nconst long long mod1 = 998244353;\nconst long long mod2 = 1000000007;\nconst long long inf = 2e18;\nconst long double pi = 3.14159265358979323;\nconst long double eps = 1e-7;\nvector<int> dx = {0,1,0,-1,1,1,-1,-1};\nvector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\nvoid o(){out(\"!?\");}\nvoid yn(bool f){out(f ? \"Yes\" : \"No\");}\nvoid YN(bool F){out(F ? \"YES\" : \"NO\");}\ntemplate<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}\ntemplate<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}\n\n} // namespace noya2\nusing namespace noya2;\n\n#line 2 \"rational.hpp\"\n\nnamespace noya2{\n\ntemplate<typename T>\nstruct rational{\n rational() : p(T(0)), q(T(1)) {}\n rational(T n) : p(n), q(T(1)) {}\n rational(T n, T m){\n assert(m != T(0));\n if (m < T(0)) n = -n, m = -m;\n T g = gcd(n,m);\n p = n / g; q = m / g;\n }\n T num(){return p;}\n T den(){return q;}\n void set_num(T n){\n p = n;\n T g = gcd(p,q);\n p /= g, q /= g;\n }\n void set_den(T n){\n assert(n != T(0));\n q = n;\n T g = gcd(p,q);\n p /= g, q /= g;\n }\n long double val(){\n return (long double)(p) / (long double)(q);\n }\n rational& operator+=(const rational& rhs){\n T g = gcd(q,rhs.q);\n T np = rhs.q / g * p + q / g * rhs.p;\n T nq = q / g * rhs.q;\n T ng = gcd(np,nq);\n p = np/ng, q = nq/ng;\n return *this; \n }\n rational& operator-=(const rational& rhs){\n T g = gcd(q,rhs.q);\n T np = rhs.q / g * p - q / g * rhs.p;\n T nq = q / g * rhs.q;\n T ng = gcd(np,nq);\n p = np/ng, q = nq/ng;\n return *this;\n }\n rational& operator*=(const rational& rhs){\n T g1 = gcd(q,rhs.p), g2 = gcd(p,rhs.q);\n T np = p / g2 * rhs.p / g1;\n T nq = q / g1 * rhs.q / g2;\n p = np, q = nq;\n return *this;\n }\n rational& operator/=(const rational& rhs){\n assert(rhs.p != T(0));\n T g1 = gcd(q,rhs.q), g2 = gcd(p,rhs.p);\n T np = p / g2 * rhs.q / g1;\n T nq = q / g1 * rhs.p / g2;\n p = np, q = nq;\n return *this;\n }\n rational operator+() const {return *this;}\n rational operator-() const {return rational() - *this;}\n friend rational operator+(const rational& lhs, const rational& rhs) {\n return rational(lhs) += rhs;\n }\n friend rational operator-(const rational& lhs, const rational& rhs) {\n return rational(lhs) -= rhs;\n }\n friend rational operator*(const rational& lhs, const rational& rhs) {\n return rational(lhs) *= rhs;\n }\n friend rational operator/(const rational& lhs, const rational& rhs) {\n return rational(lhs) /= rhs;\n }\n friend bool operator==(const rational& lhs, const rational& rhs) {\n return lhs.p == rhs.p && lhs.q == rhs.q;\n }\n friend bool operator!=(const rational& lhs, const rational& rhs) {\n return lhs.p != rhs.p || lhs.q != rhs.q;\n }\n friend bool operator< (const rational lhs, const rational rhs) {\n return less_than(lhs,rhs);\n }\n friend bool operator> (const rational lhs, const rational rhs) {\n return less_than(rhs,lhs);\n }\n friend bool operator<= (const rational lhs, const rational rhs) {\n return lhs == rhs || lhs < rhs;\n }\n friend bool operator>= (const rational lhs, const rational rhs) {\n return lhs == rhs || lhs > rhs;\n }\n friend std::ostream &operator<<(std::ostream &os,const rational&r) {\n return os << r.p << \" / \" << r.q;\n }\n private:\n T p, q;\n T gcd(T a,T b){if(a<T(0))return gcd(-a,b);if(b<T(0))return gcd(a,-b);return _gcd(a,b);}\n T _gcd(T a,T b){return b?_gcd(b,a%b):a;}\n static bool less_than(rational lhs, rational rhs){\n __int128_t lv = __int128_t(lhs.p) * __int128_t(rhs.q);\n __int128_t rv = __int128_t(lhs.q) * __int128_t(rhs.p);\n return lv < rv;\n }\n};\n\nusing rat = rational<long long>;\n\n} // namespace noya2\n#line 2 \"geometry2D.hpp\"\n\n#line 6 \"geometry2D.hpp\"\n\n#line 2 \"math.hpp\"\n\n#line 6 \"math.hpp\"\n\nnamespace noya2{\n\nusing namespace std;\nusing ll = long long;\n\ntemplate<typename T> T sqrt_safe(T x){ // floor(sqrt(x))\n assert(x >= T(0));\n if (x <= T(1)) return x;\n T tmp = (T)(sqrtl((long double)(x))) + T(2);\n while (tmp--){\n if (tmp * tmp <= x) break;\n }\n return tmp;\n}\n\nll mod_safe(ll a, ll m){ // m >= 1, 0 <= mod_safe(a,m) < m, mod_safe(a,m) = a (mod m)\n ll res = a % m;\n if (res < 0) res += m;\n return res;\n}\nll modpow(ll x, ll n, ll mod){\n x = mod_safe(x,mod);\n if (n == 0) return 1;\n ll res = modpow(x,n/2,mod);\n res = (res * res) % mod;\n if (n % 2 == 1) res = (res * x) % mod;\n return res;\n}\n\nll naive_gcd(ll a, ll b){ return b ? naive_gcd(b, a % b) : a; }\n\n// gcd(N >= 0, 0) = N, especialy gcd(0, 0) = 0\nll gcd_safe(ll a, ll b){ return naive_gcd(abs(a),abs(b)); }\nll lcm_safe(ll a, ll b){ return a / gcd_safe(a,b) * b; }\n\nvoid ext_gcd1_plus(ll a, ll b, ll &x, ll &y){ // gcd(a,b) = 1, a >= 0, b >= 0, ax + by = 1\n if (b == 0){ // a = 1\n x = 1, y = 0;\n return ;\n }\n ext_gcd1_plus(b, a%b, y, x);\n x = mod_safe(x,b);\n y = (1 - a * x) / b;\n}\nvoid ext_gcd1_minus(ll a, ll b, ll &x, ll &y){ // gcd(a,b) = 1, a >= 0, b >= 0, ax - by = 1\n if (b == 0){ // a = 1\n x = 1, y = 0;\n return ;\n }\n ext_gcd1_plus(b, a%b, y, x);\n x = mod_safe(x,b);\n y = (a * x - 1) / b;\n}\n\npair<ll,ll> ext_gcd(ll a, ll b, ll c){ // ax + by = c, |a|,|b|,|c| < 1e9, x <= max(|B|,|C|), y <= max(|A|,|C|)\n if (a < 0) return ext_gcd(-a,-b,-c);\n if (c < 0){\n pair<ll,ll> res = ext_gcd(a,b,-c);\n res.first = -res.first, res.second = -res.second;\n return res;\n }\n if (c == 0) return pair<ll,ll>(0,0);\n if (a == 0 && b == 0) return pair<ll,ll>(0,0); // answer not exist\n ll g = gcd_safe(a,b);\n if (c % g != 0) return pair<ll,ll>(0,0); // answer not exist\n a /= g, b /= g, c /= g;\n ll x, y;\n if (b == 0) return pair<ll,ll>(c,0);\n if (b > 0) ext_gcd1_plus(a,b,x,y);\n else ext_gcd1_minus(a,-b,x,y);\n x = mod_safe(x*c,abs(b));\n y = (c - a * x) / b;\n return pair<ll,ll>(x,y);\n}\n\ntemplate<typename T>\nT ceil_safe(T p, T q);\n\ntemplate<typename T>\nT floor_safe(T p, T q){\n if (q < T(0)) return floor_safe(-p,-q);\n if (p >= T(0)) return p / q;\n return -ceil_safe(-p,q);\n}\n\ntemplate<typename T>\nT ceil_safe(T p, T q){\n if (q < T(0)) return ceil_safe(-p,-q);\n if (p >= T(0)) return (p + q - 1) / q;\n return -floor_safe(-p,q);\n}\n\nstruct Eratosthenes{\n static vector<int> table;\n Eratosthenes (int Nmax = -1) {init(Nmax);}\n static void init(int Nmax){\n if (!table.empty()) return ;\n table.resize(Nmax+1,0);\n table[0] = 1, table[1] = 1;\n for (int p = 2; p <= Nmax; p++){\n if (table[p] != 0) continue;\n for (int j = p; j <= Nmax; j += p){\n table[j] = p;\n }\n }\n }\n};\nvector<int>Eratosthenes::table = vector<int>(0);\n\nvoid build_eratosthenes(const int Nmax){Eratosthenes::init(Nmax);}\n\nvector<pair<int,int>> fast_prime_factorization(int N){\n int pre = -1, cnt = 0;\n vector<pair<int,int>> res;\n while(true) {\n if (N == 1){\n if (cnt > 0) res.emplace_back(pre,cnt);\n break;\n }\n int div = Eratosthenes::table[N];\n if (pre != div){\n if (cnt > 0) res.emplace_back(pre,cnt);\n pre = div, cnt = 1;\n }\n else cnt++;\n N /= div;\n }\n return res;\n}\n\nvector<int> fast_divisor_enumeration(int N){\n auto pes = fast_prime_factorization(N);\n vector<int> res = {1};\n for (auto pe : pes){\n vector<int> nres;\n for (auto x : res){\n for (int _t = 0; _t <= pe.second; _t++){\n nres.emplace_back(x);\n x *= pe.first;\n }\n }\n swap(res,nres);\n }\n return res;\n}\n\nbool fast_is_prime(int N){\n if (N <= 1) return false;\n return Eratosthenes::table[N] == N;\n}\n\nvector<int> mobius(int N){\n vector<int> res(N+1,0);\n res[1] = 1;\n for (int p = 2; p <= N; p++){\n if (fast_is_prime(p)){\n for (int i = N/p; i > 0; i--){\n res[i*p] = -res[i];\n }\n }\n }\n return res;\n};\n\nvector<pair<ll,int>> prime_factorization(ll N){\n vector<pair<ll,int>> res;\n ll iN = N;\n for (ll d = 2; d * d <= N; d++){\n if (iN % d != 0) continue;\n if (iN == 1) break;\n int ie = 0;\n while (iN % d == 0) iN /= d, ie++;\n res.emplace_back(d,ie);\n }\n if (iN != 1) res.emplace_back(iN,1);\n return res;\n}\n\nvector<ll> divisor_enumeration(ll N){\n vector<ll> res;\n for (ll d = 1; d * d <= N; d++){\n if (N % d != 0) continue;\n res.emplace_back(d);\n if (d * d != N) res.emplace_back(N/d);\n }\n return res;\n}\n\nbool is_prime(ll N){\n if (N <= 1) return false;\n if (N <= 3) return true;\n if (N % 2 == 0) return false;\n for (ll d = 3; d * d <= N; d += 2){\n if (N % d == 0) return false;\n }\n return true;\n}\n\n}// namespace noya2\n#line 8 \"geometry2D.hpp\"\n\nnamespace noya2{\n\nusing namespace std;\nusing ld = long double;\n\ntemplate<typename T>\nstruct Vec {\n T x, y;\n Vec (T _x = T(0), T _y = T(0)) : x(_x), y(_y) {}\n Vec& operator*=(const T& a){\n x *= a;\n y *= a;\n return *this;\n }\n Vec& operator/=(const T& a){\n x /= a;\n y /= a;\n return *this;\n }\n Vec& operator+=(const Vec& rhs) {\n x += rhs.x;\n y += rhs.y;\n return *this;\n }\n Vec& operator-=(const Vec& rhs) {\n x -= rhs.x;\n y -= rhs.y;\n return *this;\n }\n friend Vec operator+(const Vec& lhs, const Vec& rhs) {\n return Vec(lhs) += rhs;\n }\n friend Vec operator-(const Vec& lhs, const Vec& rhs) {\n return Vec(lhs) -= rhs;\n }\n friend Vec operator*(const Vec& lhs, const T& rhs) {\n return Vec(lhs) *= rhs;\n }\n friend Vec operator*(const T& rhs, const Vec& lhs) {\n return Vec(lhs) *= rhs;\n }\n friend Vec operator/(const Vec& lhs, const T& rhs) {\n return Vec(lhs) /= rhs;\n }\n friend Vec operator/(const T& rhs, const Vec& lhs) {\n return Vec(lhs) /= rhs;\n }\n ld norm(){\n return sqrtl(ld(x*x+y*y));\n }\n T norm2(){\n return x*x+y*y;\n }\n};\n\ntemplate<typename T>\nT dot(const Vec<T> &a, const Vec<T> &b){\n return a.x * b.x + a.y * b.y;\n}\n\n// cross > 0 : counter clockwise a -> b\ntemplate<typename T>\nT cross(const Vec<T> &a, const Vec<T> &b){\n return a.x * b.y - a.y * b.x;\n}\n\n// T = int, ll\n// number of lattice points ( point(x,y) s.t. x in Z and y in Z ) on Vec a\ntemplate<typename T>\nT lattice_points(const Vec<T> &a){\n return gcd_safe(a.x,a.y) + 1;\n}\n\n// e : vec(OX)\n// orthant k : [ pi/2 * k, pi/2 * (k+1) ) rad\n// orthant 4 is NOT expected\ntemplate<typename T>\nint orthant(const Vec<T> &e, const Vec<T> &p){\n T _dot = dot(e,p), _cross = cross(e,p);\n if (_dot > 0 && _cross >= 0) return 0;\n if (_dot <= 0 && _cross > 0) return 1;\n if (_dot < 0 && _cross <= 0) return 2;\n if (_dot >= 0 && _cross < 0) return 3;\n return 4;\n}\n\n// usage : \n// vector<Vec<ll>> vec;\n// sort(vec.begin(),vec.end(),comp_for_argument_sort<ll>);\ntemplate<typename T>\nbool comp_for_argument_sort(const Vec<T> &lhs, const Vec<T> &rhs){\n int pl = orthant(Vec<T>(1,0),lhs), pr = orthant(Vec<T>(1,0),rhs);\n if (pl == pr){\n return cross(lhs,rhs) > 0;\n }\n return pl < pr;\n}\n\n// DISTINCT points are given\n// ans[0] --- ans[1] --- ... --- ans[k-1] --- ans[k] --- ans[0]\n// upper_hull --- lower_hull\n// upper_hull : x is from small to large\n// lower hull : x is from large to small\n// points rearranged counter clockwise\ntemplate<typename T>\nvector<Vec<T>> convex_hull(vector<Vec<T>> a){\n int n = a.size();\n if (n <= 2) return a;\n using vec = Vec<T>;\n auto comp = [&](vec lhs, vec rhs){\n if (lhs.x == rhs.x) return lhs.y < rhs.y;\n return lhs.x < rhs.x;\n };\n sort(a.begin(),a.end(),comp);\n stack<int> uid, did;\n uid.push(0);\n vec ri = a[n-1];\n for (int i = 1; i < n-1; i++){\n vec le = a[uid.top()];\n if (cross(ri-le,a[i]-le) > 0){\n while (uid.size() >= 2){\n int test = uid.top(); uid.pop();\n vec from = a[uid.top()];\n if (cross(a[i]-from,a[test]-from) > 0){\n uid.push(test);\n break;\n }\n }\n uid.push(i);\n }\n }\n did.push(0);\n for (int i = 1; i < n-1; i++){\n vec le = a[did.top()];\n if (cross(ri-le,a[i]-le) < 0){\n while (did.size() >= 2){\n int test = did.top(); did.pop();\n vec from = a[did.top()];\n if (cross(a[i]-from,a[test]-from) < 0){\n did.push(test);\n break;\n }\n }\n did.push(i);\n }\n }\n vector<int> ids(1,n-1);\n while (!did.empty()) ids.emplace_back(did.top()), did.pop();\n reverse(ids.begin(),ids.end());\n while (!uid.empty()) ids.emplace_back(uid.top()), uid.pop();\n ids.pop_back();\n vector<vec> ans(ids.size());\n for (int i = 0; i < (int)(ids.size()); i++){\n ans[i] = a[ids[i]];\n }\n return ans;\n}\n\nusing ll = long long;\nstruct Line{\n ll a, b, c; // ax + by = c\n void norm(){ // a >= 0, if (a == 0) -> b >= 0, gcd(a,b,c) = 1\n if (a < 0) a = -a, b = -b, c = -c;\n if (a == 0) if (b < 0) b = -b, c = -c;\n ll g = gcd_safe(gcd_safe(a,b),c);\n a /= g, b /= g, c /= g;\n }\n Line (ll _a = 1, ll _b = 0, ll _c = 0) : a(_a), b(_b), c(_c) {\n assert(_a != 0 || _b != 0);\n norm();\n }\n Line (ll x1, ll y1, ll x2, ll y2){ // --- (x1,y1) --- (x2,y2) ---\n assert(x1 != x2 || y1 != y2);\n ll wx = x2 - x1, wy = y2 - y1;\n ll wg = gcd_safe(wx,wy);\n a = wy / wg, b = -wx / wg;\n c = a * x1 + b * y1;\n norm();\n }\n static Line get_Line(ll x1, ll y1, ll ex, ll ey){ // y = ey/ex (x - x1) + y1 \n return Line(ey,-ex,ey*x1-ex*y1);\n }\n static Line vertical_Line(ll x1, ll y1, ll x2, ll y2){ // perpendicular bisector\n ll ex = y2 - y1, ey = x1 - x2;\n return Line(ey*2,-ex*2,ey*(x1+x2)-ex*(y1+y2));\n }\n void slide(const Vec<ll> &v){ // ax + by = c -> a(x - vx) + b(y - vy) = c\n c += a * v.x + b * v.y;\n }\n //Vec<ll> normal_Vec(const Vec<ll> &from){}\n ld dist_from_point(const Vec<ll> &from){\n return abs(ld(a) * ld(from.x) + ld(b) * ld(from.y) - ld(c)) / sqrtl(ld(a*a+b*b));\n }\n bool operator<(const Line &rhs) const { // to use set or map for Line\n if (a == rhs.a){\n if (b == rhs.b) return c < rhs.c;\n return b < rhs.b;\n }\n return a < rhs.a;\n }\n};\n\nstruct Directed_Line{\n Vec<ll> from, direct; // Line : from + k * direct\n void normalize(){\n ll g = gcd_safe(direct.x,direct.y);\n direct /= g;\n }\n Directed_Line (ll x_from = 0, ll y_from = 0, ll x_to = 1, ll y_to = 0){\n from.x = x_from, from.y = y_from;\n direct.x = x_to - x_from;\n direct.y = y_to - y_from;\n normalize();\n }\n Directed_Line (Vec<ll> _from, Vec<ll> _to){\n from = _from;\n direct = _to - _from;\n normalize();\n }\n bool in(const Vec<ll> &r, bool on){\n ll z = cross(direct,r-from);\n if (z == 0) return on;\n return z > 0;\n }\n bool slide_if_smaller(const Vec<ll> &v){\n if (in(from+v,false)){\n from += v;\n return true;\n }\n return false;\n }\n Directed_Line slide(const Vec<ll> &v){\n auto res = *this;\n res.from += v;\n return res;\n }\n};\n\n\n// two line segments AB and CD shared at least one point\nbool check_intersection(ll ax, ll ay, ll bx, ll by, ll cx, ll cy, ll dx, ll dy){\n // both AB and CD are parallel to y-axis \n if (ax == bx && cx == dx){\n if (ax != cx) return false;\n return (min(ay,by) <= max(cy,dy)) && (min(cy,dy) <= max(ay,by));\n }\n // change to cx != dx \n if (cx == dx){\n swap(ax,cx), swap(ay,cy);\n swap(bx,dx), swap(by,dy);\n }\n // change to ax >= bx\n if (ax < bx){\n swap(ax,bx), swap(ay,by);\n }\n // change to cx >= dx\n if (cx < dx){\n swap(cx,dx), swap(cy,dy);\n }\n // AB is parallel to y-axis\n if (ax == bx){\n __int128_t lv = __int128_t(cx - dx) * __int128_t(min(ay,by) - dy);\n __int128_t mv = __int128_t(cy - dy) * __int128_t(ax - dx);\n __int128_t rv = __int128_t(cx - dx) * __int128_t(max(ay,by) - dy);\n return lv <= mv && mv <= rv;\n }\n // both AB and CD are NOT parallel to y-axis\n ll gAB = gcd_safe(ay-by,ax-bx);\n ll pAB = (ax-bx) / gAB, qAB = (ay-by) / gAB;\n ll gCD = gcd_safe(cy-dy,cx-dx);\n ll pCD = (cx-dx) / gCD, qCD = (cy-dy) / gCD;\n // AB and CD are parallel\n if (pAB == pCD && qAB == qCD){\n if (bx == dx){\n return by == dy;\n }\n ll gDB = gcd_safe(dy-by,dx-bx);\n ll pDB = (dx-bx) / gDB, qDB = (dy-by) / gDB;\n if (bx > dx){\n pDB = -pDB;\n qDB = -qDB;\n }\n // Line AB = Line CD\n if (pDB == pAB && qDB == qAB){\n return bx <= cx && dx <= ax;\n }\n return false;\n }\n // AB and CD are NOT parallel\n __int128_t tal = __int128_t(cx - dx) * __int128_t(ay - cy); // ta = tal - tar\n __int128_t tar = __int128_t(cy - dy) * __int128_t(ax - cx); // ta <=> 0 == tal <=> tar\n __int128_t tbl = __int128_t(cx - dx) * __int128_t(by - cy); // tb = tbl - tbr\n __int128_t tbr = __int128_t(cy - dy) * __int128_t(bx - cx); // tb <=> 0 == tbl <=> tbr\n __int128_t tcl = __int128_t(ax - bx) * __int128_t(cy - ay); // tc = tcl - tcr\n __int128_t tcr = __int128_t(ay - by) * __int128_t(cx - ax); // tc <=> 0 == tcl <=> tcr\n __int128_t tdl = __int128_t(ax - bx) * __int128_t(dy - ay); // td = tdl - tdr\n __int128_t tdr = __int128_t(ay - by) * __int128_t(dx - ax); // td <=> 0 == tdl <=> tdr\n // ta * tb <= 0 && tc * td <= 0 -> true\n // ta > 0 && tb > 0 -> false\n if ((tal > tar && tbl > tbr) || (tal < tar && tbl < tbr)){\n return false;\n }\n // tc > 0 && td > 0 -> false\n if ((tcl > tcr && tdl > tdr) || (tcl < tcr && tdl < tdr)){\n return false;\n }\n return true;\n}\n\n} // namespace noya2\n#line 66 \"f.cpp\"\n\nusing vec = Vec<rat>;\n\nconst vector<vector<int>> order = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};\n\nbool mat2(vec a0, vec a1, vec b, vec &res){\n res = vec(b.x * a1.y - b.y * a1.x, b.y * a0.x - b.x * a0.y);\n rat d = cross(a0,a1);\n if (d == 0) return false;\n res /= d;\n return true;\n}\n\nvoid solve(){\n while (true){\n vector<vec> ia(3), b(3);\n rep(i,6){\n ll x, y; cin >> x >> y;\n if (!cin) return ;\n (i < 3 ? ia[i] : b[i-3]) = vec(x,y);\n }\n \n int ans = 5;\n for (auto fid : order) for (auto tid : order) rep(j,2){\n auto a = ia;\n int cur = 3;\n rep(i,3){\n int f = fid[i], t = tid[i];\n int p = (f+1)%3, q = (f+2)%3;\n if (cross(a[p]-a[q],b[t]-a[f]) == 0){\n a[f] = b[t];\n continue;\n }\n cur++;\n if (cur == 5) break;\n if (j == 1) swap(p,q);\n vec ab;\n if (mat2(a[f]-b[t],a[p]-a[f],a[q]-a[p],ab)){\n a[q] += ab.y * (a[f]-a[p]);\n a[f] = b[t];\n }\n else {\n cur = 5;\n break;\n }\n }\n chmin(ans,cur);\n }\n if (ans == 5) cout << \"Many\" << endl;\n else cout << ans << endl;\n if (cin.eof()) break;\n }\n}\n\nint main(){\n fast_io();\n int t = 1; //cin >> t;\n while(t--) solve();\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3416, "score_of_the_acc": -0.7292, "final_rank": 5 }, { "submission_id": "aoj_1623_7780703", "code_snippet": "#line 1 \"f.cpp\"\n#include<bits/stdc++.h>\n#define rep(i,n) for (int i = 0; i < int(n); ++i)\n#define repp(i,n,m) for (int i = m; i < int(n); ++i)\n#define reb(i,n) for (int i = int(n)-1; i >= 0; --i)\n#define all(v) v.begin(),v.end()\n\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int,int>;\nusing PL = pair<ll,ll>;\n\ntemplate<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}\ntemplate<typename T1, typename T2>void print(pair<T1,T2> a);\ntemplate<typename T>void print(vector<T> v);\ntemplate<typename T>void print(vector<vector<T>> v);\nvoid print(){ putchar(' '); }\nvoid print(bool a){ printf(\"%d\", a); }\nvoid print(int a){ printf(\"%d\", a); }\nvoid print(long a){ printf(\"%ld\", a); }\nvoid print(long long a){ printf(\"%lld\", a); }\nvoid print(char a){ printf(\"%c\", a); }\nvoid print(char a[]){ printf(\"%s\", a); }\nvoid print(const char a[]){ printf(\"%s\", a); }\nvoid print(long double a){ printf(\"%.15Lf\", a); }\nvoid print(const string& a){ for(auto&& i : a) print(i); }\nvoid print(unsigned int a){ printf(\"%u\", a); }\nvoid print(__int128_t a){print((long long)(a));}\ntemplate<class T> void print(const T& a){ cout << a;}\nint out(){ putchar('\\n'); return 0; }\ntemplate<class T> int out(const T& t){ print(t); putchar('\\n'); return 0; }\ntemplate<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }\ntemplate<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}\ntemplate<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}\ntemplate<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}\n\ntemplate<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}\ntemplate<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}\n\nvoid fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);}\n\nnamespace noya2{\n\nconst int INF = 1001001007;\nconst long long mod1 = 998244353;\nconst long long mod2 = 1000000007;\nconst long long inf = 2e18;\nconst long double pi = 3.14159265358979323;\nconst long double eps = 1e-7;\nvector<int> dx = {0,1,0,-1,1,1,-1,-1};\nvector<int> dy = {1,0,-1,0,1,-1,-1,1};\nconst string ALP = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nconst string alp = \"abcdefghijklmnopqrstuvwxyz\";\nconst string NUM = \"0123456789\";\nvoid o(){out(\"!?\");}\nvoid yn(bool f){out(f ? \"Yes\" : \"No\");}\nvoid YN(bool F){out(F ? \"YES\" : \"NO\");}\ntemplate<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}\ntemplate<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}\n\n} // namespace noya2\nusing namespace noya2;\n\n#line 2 \"rational.hpp\"\n\nnamespace noya2{\n\ntemplate<typename T>\nstruct rational{\n rational() : p(T(0)), q(T(1)) {}\n rational(T n) : p(n), q(T(1)) {}\n rational(T n, T m){\n assert(m != T(0));\n if (m < T(0)) n = -n, m = -m;\n T g = gcd(n,m);\n p = n / g; q = m / g;\n }\n T num(){return p;}\n T den(){return q;}\n void set_num(T n){\n p = n;\n T g = gcd(p,q);\n p /= g, q /= g;\n }\n void set_den(T n){\n assert(n != T(0));\n q = n;\n T g = gcd(p,q);\n p /= g, q /= g;\n }\n long double val(){\n return (long double)(p) / (long double)(q);\n }\n rational& operator+=(const rational& rhs){\n T g = gcd(q,rhs.q);\n T np = rhs.q / g * p + q / g * rhs.p;\n T nq = q / g * rhs.q;\n T ng = gcd(np,nq);\n p = np/ng, q = nq/ng;\n return *this; \n }\n rational& operator-=(const rational& rhs){\n T g = gcd(q,rhs.q);\n T np = rhs.q / g * p - q / g * rhs.p;\n T nq = q / g * rhs.q;\n T ng = gcd(np,nq);\n p = np/ng, q = nq/ng;\n return *this;\n }\n rational& operator*=(const rational& rhs){\n T g1 = gcd(q,rhs.p), g2 = gcd(p,rhs.q);\n T np = p / g2 * rhs.p / g1;\n T nq = q / g1 * rhs.q / g2;\n p = np, q = nq;\n return *this;\n }\n rational& operator/=(const rational& rhs){\n assert(rhs.p != T(0));\n T g1 = gcd(q,rhs.q), g2 = gcd(p,rhs.p);\n T np = p / g2 * rhs.q / g1;\n T nq = q / g1 * rhs.p / g2;\n p = np, q = nq;\n return *this;\n }\n rational operator+() const {return *this;}\n rational operator-() const {return rational() - *this;}\n friend rational operator+(const rational& lhs, const rational& rhs) {\n return rational(lhs) += rhs;\n }\n friend rational operator-(const rational& lhs, const rational& rhs) {\n return rational(lhs) -= rhs;\n }\n friend rational operator*(const rational& lhs, const rational& rhs) {\n return rational(lhs) *= rhs;\n }\n friend rational operator/(const rational& lhs, const rational& rhs) {\n return rational(lhs) /= rhs;\n }\n friend bool operator==(const rational& lhs, const rational& rhs) {\n return lhs.p == rhs.p && lhs.q == rhs.q;\n }\n friend bool operator!=(const rational& lhs, const rational& rhs) {\n return lhs.p != rhs.p || lhs.q != rhs.q;\n }\n friend bool operator< (const rational lhs, const rational rhs) {\n return less_than(lhs,rhs);\n }\n friend bool operator> (const rational lhs, const rational rhs) {\n return less_than(rhs,lhs);\n }\n friend bool operator<= (const rational lhs, const rational rhs) {\n return lhs == rhs || lhs < rhs;\n }\n friend bool operator>= (const rational lhs, const rational rhs) {\n return lhs == rhs || lhs > rhs;\n }\n friend std::ostream &operator<<(std::ostream &os,const rational&r) {\n return os << r.p << \" / \" << r.q;\n }\n private:\n T p, q;\n T gcd(T a,T b){if(a<T(0))return gcd(-a,b);if(b<T(0))return gcd(a,-b);return _gcd(a,b);}\n T _gcd(T a,T b){return b?_gcd(b,a%b):a;}\n static bool less_than(rational lhs, rational rhs){\n __int128_t lv = __int128_t(lhs.p) * __int128_t(rhs.q);\n __int128_t rv = __int128_t(lhs.q) * __int128_t(rhs.p);\n return lv < rv;\n }\n};\n\nusing rat = rational<long long>;\n\n} // namespace noya2\n#line 2 \"geometry2D.hpp\"\n\n#line 6 \"geometry2D.hpp\"\n\n#line 2 \"math.hpp\"\n\n#line 6 \"math.hpp\"\n\nnamespace noya2{\n\nusing namespace std;\nusing ll = long long;\n\ntemplate<typename T> T sqrt_safe(T x){ // floor(sqrt(x))\n assert(x >= T(0));\n if (x <= T(1)) return x;\n T tmp = (T)(sqrtl((long double)(x))) + T(2);\n while (tmp--){\n if (tmp * tmp <= x) break;\n }\n return tmp;\n}\n\nll mod_safe(ll a, ll m){ // m >= 1, 0 <= mod_safe(a,m) < m, mod_safe(a,m) = a (mod m)\n ll res = a % m;\n if (res < 0) res += m;\n return res;\n}\nll modpow(ll x, ll n, ll mod){\n x = mod_safe(x,mod);\n if (n == 0) return 1;\n ll res = modpow(x,n/2,mod);\n res = (res * res) % mod;\n if (n % 2 == 1) res = (res * x) % mod;\n return res;\n}\n\nll naive_gcd(ll a, ll b){ return b ? naive_gcd(b, a % b) : a; }\n\n// gcd(N >= 0, 0) = N, especialy gcd(0, 0) = 0\nll gcd_safe(ll a, ll b){ return naive_gcd(abs(a),abs(b)); }\nll lcm_safe(ll a, ll b){ return a / gcd_safe(a,b) * b; }\n\nvoid ext_gcd1_plus(ll a, ll b, ll &x, ll &y){ // gcd(a,b) = 1, a >= 0, b >= 0, ax + by = 1\n if (b == 0){ // a = 1\n x = 1, y = 0;\n return ;\n }\n ext_gcd1_plus(b, a%b, y, x);\n x = mod_safe(x,b);\n y = (1 - a * x) / b;\n}\nvoid ext_gcd1_minus(ll a, ll b, ll &x, ll &y){ // gcd(a,b) = 1, a >= 0, b >= 0, ax - by = 1\n if (b == 0){ // a = 1\n x = 1, y = 0;\n return ;\n }\n ext_gcd1_plus(b, a%b, y, x);\n x = mod_safe(x,b);\n y = (a * x - 1) / b;\n}\n\npair<ll,ll> ext_gcd(ll a, ll b, ll c){ // ax + by = c, |a|,|b|,|c| < 1e9, x <= max(|B|,|C|), y <= max(|A|,|C|)\n if (a < 0) return ext_gcd(-a,-b,-c);\n if (c < 0){\n pair<ll,ll> res = ext_gcd(a,b,-c);\n res.first = -res.first, res.second = -res.second;\n return res;\n }\n if (c == 0) return pair<ll,ll>(0,0);\n if (a == 0 && b == 0) return pair<ll,ll>(0,0); // answer not exist\n ll g = gcd_safe(a,b);\n if (c % g != 0) return pair<ll,ll>(0,0); // answer not exist\n a /= g, b /= g, c /= g;\n ll x, y;\n if (b == 0) return pair<ll,ll>(c,0);\n if (b > 0) ext_gcd1_plus(a,b,x,y);\n else ext_gcd1_minus(a,-b,x,y);\n x = mod_safe(x*c,abs(b));\n y = (c - a * x) / b;\n return pair<ll,ll>(x,y);\n}\n\ntemplate<typename T>\nT ceil_safe(T p, T q);\n\ntemplate<typename T>\nT floor_safe(T p, T q){\n if (q < T(0)) return floor_safe(-p,-q);\n if (p >= T(0)) return p / q;\n return -ceil_safe(-p,q);\n}\n\ntemplate<typename T>\nT ceil_safe(T p, T q){\n if (q < T(0)) return ceil_safe(-p,-q);\n if (p >= T(0)) return (p + q - 1) / q;\n return -floor_safe(-p,q);\n}\n\nstruct Eratosthenes{\n static vector<int> table;\n Eratosthenes (int Nmax = -1) {init(Nmax);}\n static void init(int Nmax){\n if (!table.empty()) return ;\n table.resize(Nmax+1,0);\n table[0] = 1, table[1] = 1;\n for (int p = 2; p <= Nmax; p++){\n if (table[p] != 0) continue;\n for (int j = p; j <= Nmax; j += p){\n table[j] = p;\n }\n }\n }\n};\nvector<int>Eratosthenes::table = vector<int>(0);\n\nvoid build_eratosthenes(const int Nmax){Eratosthenes::init(Nmax);}\n\nvector<pair<int,int>> fast_prime_factorization(int N){\n int pre = -1, cnt = 0;\n vector<pair<int,int>> res;\n while(true) {\n if (N == 1){\n if (cnt > 0) res.emplace_back(pre,cnt);\n break;\n }\n int div = Eratosthenes::table[N];\n if (pre != div){\n if (cnt > 0) res.emplace_back(pre,cnt);\n pre = div, cnt = 1;\n }\n else cnt++;\n N /= div;\n }\n return res;\n}\n\nvector<int> fast_divisor_enumeration(int N){\n auto pes = fast_prime_factorization(N);\n vector<int> res = {1};\n for (auto pe : pes){\n vector<int> nres;\n for (auto x : res){\n for (int _t = 0; _t <= pe.second; _t++){\n nres.emplace_back(x);\n x *= pe.first;\n }\n }\n swap(res,nres);\n }\n return res;\n}\n\nbool fast_is_prime(int N){\n if (N <= 1) return false;\n return Eratosthenes::table[N] == N;\n}\n\nvector<int> mobius(int N){\n vector<int> res(N+1,0);\n res[1] = 1;\n for (int p = 2; p <= N; p++){\n if (fast_is_prime(p)){\n for (int i = N/p; i > 0; i--){\n res[i*p] = -res[i];\n }\n }\n }\n return res;\n};\n\nvector<pair<ll,int>> prime_factorization(ll N){\n vector<pair<ll,int>> res;\n ll iN = N;\n for (ll d = 2; d * d <= N; d++){\n if (iN % d != 0) continue;\n if (iN == 1) break;\n int ie = 0;\n while (iN % d == 0) iN /= d, ie++;\n res.emplace_back(d,ie);\n }\n if (iN != 1) res.emplace_back(iN,1);\n return res;\n}\n\nvector<ll> divisor_enumeration(ll N){\n vector<ll> res;\n for (ll d = 1; d * d <= N; d++){\n if (N % d != 0) continue;\n res.emplace_back(d);\n if (d * d != N) res.emplace_back(N/d);\n }\n return res;\n}\n\nbool is_prime(ll N){\n if (N <= 1) return false;\n if (N <= 3) return true;\n if (N % 2 == 0) return false;\n for (ll d = 3; d * d <= N; d += 2){\n if (N % d == 0) return false;\n }\n return true;\n}\n\n}// namespace noya2\n#line 8 \"geometry2D.hpp\"\n\nnamespace noya2{\n\nusing namespace std;\nusing ld = long double;\n\ntemplate<typename T>\nstruct Vec {\n T x, y;\n Vec (T _x = T(0), T _y = T(0)) : x(_x), y(_y) {}\n Vec& operator*=(const T& a){\n x *= a;\n y *= a;\n return *this;\n }\n Vec& operator/=(const T& a){\n x /= a;\n y /= a;\n return *this;\n }\n Vec& operator+=(const Vec& rhs) {\n x += rhs.x;\n y += rhs.y;\n return *this;\n }\n Vec& operator-=(const Vec& rhs) {\n x -= rhs.x;\n y -= rhs.y;\n return *this;\n }\n friend bool operator==(const Vec& lhs, const Vec& rhs) {\n return lhs.x == rhs.x && lhs.y == rhs.y;\n }\n friend bool operator!=(const Vec& lhs, const Vec& rhs) {\n return lhs.x != rhs.x || lhs.y != rhs.y;\n }\n friend Vec operator+(const Vec& lhs, const Vec& rhs) {\n return Vec(lhs) += rhs;\n }\n friend Vec operator-(const Vec& lhs, const Vec& rhs) {\n return Vec(lhs) -= rhs;\n }\n friend Vec operator*(const Vec& lhs, const T& rhs) {\n return Vec(lhs) *= rhs;\n }\n friend Vec operator*(const T& rhs, const Vec& lhs) {\n return Vec(lhs) *= rhs;\n }\n friend Vec operator/(const Vec& lhs, const T& rhs) {\n return Vec(lhs) /= rhs;\n }\n friend Vec operator/(const T& rhs, const Vec& lhs) {\n return Vec(lhs) /= rhs;\n }\n ld norm(){\n return sqrtl(ld(x*x+y*y));\n }\n T norm2(){\n return x*x+y*y;\n }\n};\n\ntemplate<typename T>\nT dot(const Vec<T> &a, const Vec<T> &b){\n return a.x * b.x + a.y * b.y;\n}\n\n// cross > 0 : counter clockwise a -> b\ntemplate<typename T>\nT cross(const Vec<T> &a, const Vec<T> &b){\n return a.x * b.y - a.y * b.x;\n}\n\n// T = int, ll\n// number of lattice points ( point(x,y) s.t. x in Z and y in Z ) on Vec a\ntemplate<typename T>\nT lattice_points(const Vec<T> &a){\n return gcd_safe(a.x,a.y) + 1;\n}\n\n// e : vec(OX)\n// orthant k : [ pi/2 * k, pi/2 * (k+1) ) rad\n// orthant 4 is NOT expected\ntemplate<typename T>\nint orthant(const Vec<T> &e, const Vec<T> &p){\n T _dot = dot(e,p), _cross = cross(e,p);\n if (_dot > 0 && _cross >= 0) return 0;\n if (_dot <= 0 && _cross > 0) return 1;\n if (_dot < 0 && _cross <= 0) return 2;\n if (_dot >= 0 && _cross < 0) return 3;\n return 4;\n}\n\n// usage : \n// vector<Vec<ll>> vec;\n// sort(vec.begin(),vec.end(),comp_for_argument_sort<ll>);\ntemplate<typename T>\nbool comp_for_argument_sort(const Vec<T> &lhs, const Vec<T> &rhs){\n int pl = orthant(Vec<T>(1,0),lhs), pr = orthant(Vec<T>(1,0),rhs);\n if (pl == pr){\n return cross(lhs,rhs) > 0;\n }\n return pl < pr;\n}\n\n// DISTINCT points are given\n// ans[0] --- ans[1] --- ... --- ans[k-1] --- ans[k] --- ans[0]\n// upper_hull --- lower_hull\n// upper_hull : x is from small to large\n// lower hull : x is from large to small\n// points rearranged counter clockwise\ntemplate<typename T>\nvector<Vec<T>> convex_hull(vector<Vec<T>> a){\n int n = a.size();\n if (n <= 2) return a;\n using vec = Vec<T>;\n auto comp = [&](vec lhs, vec rhs){\n if (lhs.x == rhs.x) return lhs.y < rhs.y;\n return lhs.x < rhs.x;\n };\n sort(a.begin(),a.end(),comp);\n stack<int> uid, did;\n uid.push(0);\n vec ri = a[n-1];\n for (int i = 1; i < n-1; i++){\n vec le = a[uid.top()];\n if (cross(ri-le,a[i]-le) > 0){\n while (uid.size() >= 2){\n int test = uid.top(); uid.pop();\n vec from = a[uid.top()];\n if (cross(a[i]-from,a[test]-from) > 0){\n uid.push(test);\n break;\n }\n }\n uid.push(i);\n }\n }\n did.push(0);\n for (int i = 1; i < n-1; i++){\n vec le = a[did.top()];\n if (cross(ri-le,a[i]-le) < 0){\n while (did.size() >= 2){\n int test = did.top(); did.pop();\n vec from = a[did.top()];\n if (cross(a[i]-from,a[test]-from) < 0){\n did.push(test);\n break;\n }\n }\n did.push(i);\n }\n }\n vector<int> ids(1,n-1);\n while (!did.empty()) ids.emplace_back(did.top()), did.pop();\n reverse(ids.begin(),ids.end());\n while (!uid.empty()) ids.emplace_back(uid.top()), uid.pop();\n ids.pop_back();\n vector<vec> ans(ids.size());\n for (int i = 0; i < (int)(ids.size()); i++){\n ans[i] = a[ids[i]];\n }\n return ans;\n}\n\nusing ll = long long;\nstruct Line{\n ll a, b, c; // ax + by = c\n void norm(){ // a >= 0, if (a == 0) -> b >= 0, gcd(a,b,c) = 1\n if (a < 0) a = -a, b = -b, c = -c;\n if (a == 0) if (b < 0) b = -b, c = -c;\n ll g = gcd_safe(gcd_safe(a,b),c);\n a /= g, b /= g, c /= g;\n }\n Line (ll _a = 1, ll _b = 0, ll _c = 0) : a(_a), b(_b), c(_c) {\n assert(_a != 0 || _b != 0);\n norm();\n }\n Line (ll x1, ll y1, ll x2, ll y2){ // --- (x1,y1) --- (x2,y2) ---\n assert(x1 != x2 || y1 != y2);\n ll wx = x2 - x1, wy = y2 - y1;\n ll wg = gcd_safe(wx,wy);\n a = wy / wg, b = -wx / wg;\n c = a * x1 + b * y1;\n norm();\n }\n static Line get_Line(ll x1, ll y1, ll ex, ll ey){ // y = ey/ex (x - x1) + y1 \n return Line(ey,-ex,ey*x1-ex*y1);\n }\n static Line vertical_Line(ll x1, ll y1, ll x2, ll y2){ // perpendicular bisector\n ll ex = y2 - y1, ey = x1 - x2;\n return Line(ey*2,-ex*2,ey*(x1+x2)-ex*(y1+y2));\n }\n void slide(const Vec<ll> &v){ // ax + by = c -> a(x - vx) + b(y - vy) = c\n c += a * v.x + b * v.y;\n }\n //Vec<ll> normal_Vec(const Vec<ll> &from){}\n ld dist_from_point(const Vec<ll> &from){\n return abs(ld(a) * ld(from.x) + ld(b) * ld(from.y) - ld(c)) / sqrtl(ld(a*a+b*b));\n }\n bool operator<(const Line &rhs) const { // to use set or map for Line\n if (a == rhs.a){\n if (b == rhs.b) return c < rhs.c;\n return b < rhs.b;\n }\n return a < rhs.a;\n }\n};\n\nstruct Directed_Line{\n Vec<ll> from, direct; // Line : from + k * direct\n void normalize(){\n ll g = gcd_safe(direct.x,direct.y);\n direct /= g;\n }\n Directed_Line (ll x_from = 0, ll y_from = 0, ll x_to = 1, ll y_to = 0){\n from.x = x_from, from.y = y_from;\n direct.x = x_to - x_from;\n direct.y = y_to - y_from;\n normalize();\n }\n Directed_Line (Vec<ll> _from, Vec<ll> _to){\n from = _from;\n direct = _to - _from;\n normalize();\n }\n bool in(const Vec<ll> &r, bool on){\n ll z = cross(direct,r-from);\n if (z == 0) return on;\n return z > 0;\n }\n bool slide_if_smaller(const Vec<ll> &v){\n if (in(from+v,false)){\n from += v;\n return true;\n }\n return false;\n }\n Directed_Line slide(const Vec<ll> &v){\n auto res = *this;\n res.from += v;\n return res;\n }\n};\n\n\n// two line segments AB and CD shared at least one point\nbool check_intersection(ll ax, ll ay, ll bx, ll by, ll cx, ll cy, ll dx, ll dy){\n // both AB and CD are parallel to y-axis \n if (ax == bx && cx == dx){\n if (ax != cx) return false;\n return (min(ay,by) <= max(cy,dy)) && (min(cy,dy) <= max(ay,by));\n }\n // change to cx != dx \n if (cx == dx){\n swap(ax,cx), swap(ay,cy);\n swap(bx,dx), swap(by,dy);\n }\n // change to ax >= bx\n if (ax < bx){\n swap(ax,bx), swap(ay,by);\n }\n // change to cx >= dx\n if (cx < dx){\n swap(cx,dx), swap(cy,dy);\n }\n // AB is parallel to y-axis\n if (ax == bx){\n __int128_t lv = __int128_t(cx - dx) * __int128_t(min(ay,by) - dy);\n __int128_t mv = __int128_t(cy - dy) * __int128_t(ax - dx);\n __int128_t rv = __int128_t(cx - dx) * __int128_t(max(ay,by) - dy);\n return lv <= mv && mv <= rv;\n }\n // both AB and CD are NOT parallel to y-axis\n ll gAB = gcd_safe(ay-by,ax-bx);\n ll pAB = (ax-bx) / gAB, qAB = (ay-by) / gAB;\n ll gCD = gcd_safe(cy-dy,cx-dx);\n ll pCD = (cx-dx) / gCD, qCD = (cy-dy) / gCD;\n // AB and CD are parallel\n if (pAB == pCD && qAB == qCD){\n if (bx == dx){\n return by == dy;\n }\n ll gDB = gcd_safe(dy-by,dx-bx);\n ll pDB = (dx-bx) / gDB, qDB = (dy-by) / gDB;\n if (bx > dx){\n pDB = -pDB;\n qDB = -qDB;\n }\n // Line AB = Line CD\n if (pDB == pAB && qDB == qAB){\n return bx <= cx && dx <= ax;\n }\n return false;\n }\n // AB and CD are NOT parallel\n __int128_t tal = __int128_t(cx - dx) * __int128_t(ay - cy); // ta = tal - tar\n __int128_t tar = __int128_t(cy - dy) * __int128_t(ax - cx); // ta <=> 0 == tal <=> tar\n __int128_t tbl = __int128_t(cx - dx) * __int128_t(by - cy); // tb = tbl - tbr\n __int128_t tbr = __int128_t(cy - dy) * __int128_t(bx - cx); // tb <=> 0 == tbl <=> tbr\n __int128_t tcl = __int128_t(ax - bx) * __int128_t(cy - ay); // tc = tcl - tcr\n __int128_t tcr = __int128_t(ay - by) * __int128_t(cx - ax); // tc <=> 0 == tcl <=> tcr\n __int128_t tdl = __int128_t(ax - bx) * __int128_t(dy - ay); // td = tdl - tdr\n __int128_t tdr = __int128_t(ay - by) * __int128_t(dx - ax); // td <=> 0 == tdl <=> tdr\n // ta * tb <= 0 && tc * td <= 0 -> true\n // ta > 0 && tb > 0 -> false\n if ((tal > tar && tbl > tbr) || (tal < tar && tbl < tbr)){\n return false;\n }\n // tc > 0 && td > 0 -> false\n if ((tcl > tcr && tdl > tdr) || (tcl < tcr && tdl < tdr)){\n return false;\n }\n return true;\n}\n\n} // namespace noya2\n#line 66 \"f.cpp\"\n\nusing vec = Vec<rat>;\n\nconst vector<vector<int>> order = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};\n\nbool mat2(vec a0, vec a1, vec b, vec &res){\n res = vec(b.x * a1.y - b.y * a1.x, b.y * a0.x - b.x * a0.y);\n rat d = cross(a0,a1);\n if (d == 0) return false;\n res /= d;\n return true;\n}\n\nvoid solve(){\n ll xx;\n while (std::cin >> xx){\n vector<vec> ia(3), b(3);\n {\n ll y;\n std::cin >> y;\n ia[0] = {xx, y};\n }\n rep(i,6){\n if(i == 0) continue;\n ll x, y; cin >> x >> y;\n (i < 3 ? ia[i] : b[i-3]) = vec(x,y);\n }\n int ans = 5;\n for (auto fid : order) for (auto tid : order) rep(j,2){\n auto a = ia;\n int cur = 0;\n rep(i,3){\n int f = fid[i], t = tid[i];\n if (a[f] == b[t]) continue;\n cur++;\n int p = (f+1)%3, q = (f+2)%3;\n if (cross(a[p]-a[q],b[t]-a[f]) == 0){\n a[f] = b[t];\n continue;\n }\n cur++;\n if (j == 1) swap(p,q);\n vec ab;\n if (mat2(a[f]-b[t],a[p]-a[f],a[q]-a[p],ab)){\n a[q] += ab.y * (a[f]-a[p]);\n a[f] = b[t];\n }\n else {\n cur = 1000;\n break;\n }\n }\n chmin(ans,cur);\n }\n if (ans == 5) out(\"Many\");\n else out(ans);\n }\n}\n\nint main(){\n fast_io();\n int t = 1; //cin >> t;\n while(t--) solve();\n}", "accuracy": 1, "time_ms": 850, "memory_kb": 3520, "score_of_the_acc": -1.057, "final_rank": 19 }, { "submission_id": "aoj_1623_7780702", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstddef>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n/* macro */\n\n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)\n#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)\n#define RRep(i, a, n) for (i64 i = ((i64)(n - i64(1))); i >= (i64)(a); i--)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define Bit(n) (1LL << (n))\n\n/* macro end */\n\n/* template */\n\nnamespace ebi {\n\n#ifdef LOCAL\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\nvoid debug_out() { std::cerr << std::endl; }\n\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cout << \" :\";\n debug_out(t...);\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {\n return os << pa.first << \" \" << pa.second;\n}\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {\n return os >> pa.first >> pa.second;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {\n for (std::size_t i = 0; i < vec.size(); i++)\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n\ntemplate <typename T>\nstd::istream &operator>>(std::istream &os, std::vector<T> &vec) {\n for (T &e : vec) std::cin >> e;\n return os;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {\n if (opt) {\n os << opt.value();\n } else {\n os << \"invalid value\";\n }\n return os;\n}\n\nusing std::size_t;\nusing i32 = std::int32_t;\nusing u32 = std::uint32_t;\nusing i64 = std::int64_t;\nusing u64 = std::uint64_t;\n\ntemplate <class T, T init>\nauto make_vector(int n) {\n return std::vector<T>(n, init);\n}\n\ntemplate <class T, T init, typename Head, typename... Tail>\nauto make_vector(Head n, Tail... ts) {\n return std::vector(n, make_vector<T, init>(ts...));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\nT pow(T x, i64 n) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = res * x;\n x = x * x;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT mod_pow(T x, i64 n, i64 mod) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT scan() {\n T val;\n std::cin >> val;\n return val;\n}\n\ntemplate <class T>\nstruct Edge {\n int to;\n T cost;\n Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}\n};\n\ntemplate <class T>\nstruct Graph : std::vector<std::vector<Edge<T>>> {\n using std::vector<std::vector<Edge<T>>>::vector;\n void add_edge(int u, int v, T w, bool directed = false) {\n (*this)[u].emplace_back(v, w);\n if (directed) return;\n (*this)[v].emplace_back(u, w);\n }\n};\n\nstruct graph : std::vector<std::vector<int>> {\n using std::vector<std::vector<int>>::vector;\n void add_edge(int u, int v, bool directed = false) {\n (*this)[u].emplace_back(v);\n if (directed) return;\n (*this)[v].emplace_back(u);\n }\n};\n\nconstexpr i64 LNF = std::numeric_limits<i64>::max() / 4;\n\nconstexpr int INF = std::numeric_limits<int>::max() / 2;\n\nconst std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};\nconst std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};\n\n} // namespace ebi\n\nnamespace ebi {\n\nconstexpr long double EPS = 1e-7;\n\nconst long double PI = std::acos(-1);\n\nnamespace internal {\n\nint sgn(long double a) {\n return (a<-EPS) ? -1 : (a>EPS) ? 1 : 0;\n}\n\nlong double add(long double a, long double b) {\n if(std::abs(a+b) < EPS*(std::abs(a) + std::abs(b))) return 0;\n return a+b;\n}\n\n} // namespace internal\n\n\nlong double arg_to_radian(long double arg) {\n return PI * arg / (long double)(180);\n}\n\nstruct point {\n long double x,y;\n\n point() = default;\n\n point(long double x, long double y) : x(x), y(y) { }\n\n point &operator+=(const point rhs) noexcept {\n x = internal::add(x, rhs.x);\n y = internal::add(y, rhs.y);\n return *this;\n }\n\n point &operator-=(const point rhs) noexcept {\n x = internal::add(x, -rhs.x);\n y = internal::add(y, -rhs.y);\n return *this;\n }\n\n point &operator*=(const point rhs) noexcept {\n long double _x = internal::add(x*rhs.x, -y*rhs.y);\n long double _y = internal::add(x*rhs.y, y*rhs.x);\n x = _x;\n y = _y;\n return *this;\n }\n\n point &operator*=(const long double k) noexcept {\n x *= k;\n y *= k;\n return *this;\n }\n\n point &operator/=(const long double k) {\n assert(internal::sgn(k)!=0);\n x /= k;\n y /= k;\n return *this;\n }\n\n point operator+(const point &rhs) const noexcept {\n return point(*this) += rhs;\n }\n\n point operator-(const point &rhs) const noexcept {\n return point(*this) -= rhs;\n }\n\n point operator*(const point &rhs) const noexcept {\n return point(*this) *= rhs;\n }\n\n point operator*(const long double rhs) const noexcept {\n return point(*this) *= rhs;\n }\n\n point operator/(const long double rhs) const {\n return point(*this) /= rhs;\n }\n\n point operator-() const noexcept {\n return point(0, 0) - *this;\n }\n\n long double abs() const noexcept {\n return std::sqrt(internal::add(x*x, y*y));\n }\n\n long double dot(const point rhs) const noexcept {\n return internal::add(x*rhs.x, y*rhs.y);\n }\n\n long double det(const point rhs) const noexcept {\n return internal::add(x*rhs.y, -y*rhs.x);\n }\n\n // arctan(y/x) (単位はラジアン)\n\n long double arg() const {\n return std::atan2(y, x);\n }\n\n // x昇順, その後y昇順\n\n bool operator<(const point &rhs) const noexcept {\n if(internal::sgn(x-rhs.x)) return internal::sgn(x-rhs.x)<0;\n return internal::sgn(y-rhs.y)<0;\n }\n\n bool operator==(const point &rhs) const noexcept {\n if(internal::sgn(x - rhs.x) == 0 && internal::sgn(y - rhs.y) == 0) return true;\n else return false;\n }\n\n bool operator!=(const point &rhs) const noexcept {\n return !(*this == rhs);\n }\n};\n\nstd::ostream& operator<<(std::ostream& os, const point &a) {\n return os << a.x << \" \" << a.y;\n}\n\nstd::istream& operator>>(std::istream& os, point &a) {\n return os >> a.x >> a.y;\n}\n\npoint conj(const point &a) {\n return point(a.x, -a.y);\n}\n\n// 点a をang(ラジアン)回転する\n\npoint rot(const point &a, long double ang) {\n return point(std::cos(ang) * a.x - std::sin(ang) * a.y, std::sin(ang) * a.x + std::cos(ang) * a.y);\n} \n\npoint rot90(const point &a) {\n return point(-a.y, a.x);\n}\n\nlong double dot(const point &a, const point &b) {\n return a.dot(b);\n}\n\nlong double det(const point &a, const point &b) {\n return a.det(b);\n}\n\nlong double abs(const point &a) {\n return a.abs();\n}\n\nlong double norm(const point &a) {\n return internal::add(a.x*a.x, a.y*a.y);\n}\n\nint isp(const point &a, const point &b, const point &c) {\n int flag = internal::sgn(det(b-a,c-a));\n if(flag == 0) {\n if(internal::sgn(dot(b-a, c-a))<0) return -2;\n if(internal::sgn(dot(a-b, c-b))<0) return +2;\n }\n return flag;\n}\n\n// 分割統治で最近点対を求める O(N log N)\n\nlong double closest_pair(std::vector<point> p) {\n std::sort(p.begin(), p.end());\n int n = p.size();\n auto f = [&](auto &&self, int l, int r) -> long double {\n if(r-l == 1) {\n return 1e9;\n }\n int mid = (l+r)/2;\n long double x = p[mid].x;\n long double d = std::min(self(self, l, mid), self(self, mid, r));\n std::vector<point> b;\n b.reserve(r-l);\n int j = mid;\n for(int i = l; i < mid; i++) {\n while(j < r && p[j].y <= p[i].y) {\n b.emplace_back(p[j++]);\n }\n b.emplace_back(p[i]);\n }\n while(j < r) {\n b.emplace_back(p[j++]);\n }\n for(int i = 0; i < r-l; i++) {\n p[l+i] = b[i];\n }\n b.clear();\n for(int i = l; i < r; i++) {\n if(std::abs(p[i].x - x) >= d) continue;\n for(int j = int(b.size())-1; j >= 0; j--) {\n if(p[i].y - b[j].y >= d) break;\n d = std::min(d, abs(p[i]-b[j]));\n }\n b.emplace_back(p[i]);\n }\n return d;\n };\n return f(f, 0, n);\n}\n\n// ∠ABCを求める(ラジアン)\n\nlong double angle(const point &A, const point &B, const point &C) {\n long double a = (B - C).abs(), b = (C - A).abs(), c = (A - B).abs();\n long double cos = internal::add(internal::add(a*a, c*c), -b*b)/(2.0*c*a);\n return std::acos(cos);\n}\n\nvoid arg_sort(std::vector<point> &a) {\n int n = a.size();\n std::vector ps(4, std::vector<point>());\n auto idx = [](point v) -> int {\n if(v.y >= 0) return (v.x >= 0) ? 0 : 1;\n else return (v.x >= 0) ? 3 : 2;\n };\n for(auto p: a) {\n assert(!(p.x == 0 && p.y == 0));\n ps[idx(p)].emplace_back(p);\n }\n a.clear();\n a.reserve(n);\n for(int i = 0; i < 4; i++) {\n std::sort(ps[i].begin(), ps[i].end(), \n [](point &p1, point &p2) -> bool {\n int flag = internal::sgn(internal::add(p1.x * p2.y, - p2.x * p1.y));\n return flag == 0 ? (norm(p1) < norm(p2)) : flag > 0;\n });\n for(auto &p: ps[i]) a.emplace_back(p);\n }\n return;\n}\n\ntemplate<class T>\nvoid arg_sort_ll(std::vector<std::pair<T , T>> &a) {\n using Point = std::pair<T, T>;\n int n = a.size();\n std::vector ps(4, std::vector<Point>());\n auto idx = [](Point v) -> int {\n if(v.second >= 0) return (v.first >= 0) ? 0 : 1;\n else return (v.first >= 0) ? 3 : 2;\n };\n for(auto p: a) {\n assert(!(p.first == 0 && p.second == 0));\n ps[idx(p)].emplace_back(p);\n }\n a.clear();\n a.reserve(n);\n for(int i = 0; i < 4; i++) {\n std::sort(ps[i].begin(), ps[i].end(), \n [](Point &p1, Point &p2) -> bool { \n T flag = p1.first * p2.second - p2.first * p1.second;\n return flag == 0 ? (p1.first * p1.first + p1.second * p1.second < p2.first * p2.first + p2.second * p2.second) : flag > 0;\n });\n for(auto &p: ps[i]) a.emplace_back(p);\n }\n return;\n}\n\n}\n\nnamespace ebi {\n\nstruct line {\n point a,b;\n\n line(long double x1, long double y1, long double x2, long double y2) : a(x1, y1), b(x2, y2) { }\n\n line(const point &a, const point &b) : a(a), b(b) { }\n\n point proj(const point &p) const {\n return a + (b-a)*(dot(b-a,p-a)/norm(b-a));\n }\n\n point relf(const point &p) const {\n return proj(p)*double(2) - p;\n }\n\n long double distance(const point &c) const {\n return std::abs(det(c - a, b - a)/abs(b-a));\n }\n};\n\nint intersection(const line &a, const line &b) {\n if(internal::sgn(det(a.b-a.a, b.a-b.b)) != 0) {\n if(internal::sgn(dot(a.b-a.a, b.b-b.a)) == 0) { // 垂直\n return 1;\n }\n return 0; // 交差\n }\n else if(internal::sgn(det(a.b-a.a, b.a-a.a)) != 0) { // 平行\n return 2;\n }\n else { // 同一直線\n return 3;\n }\n}\n\npoint cross_point(const point &a, const point &b, const point &c, const point &d) {\n return a + (b-a) * det(c - a, d - c) / det(b - a, d - c);\n}\n\n// 交点があるか確認する!\npoint cross_point(const line &s, const line &t) {\n assert(intersection(s, t) < 2);\n return s.a + (s.b - s.a) * det(t.a - s.a, t.b - t.a) / det(s.b - s.a, t.b - t.a);\n}\n\n// 直線aと点cの距離\nlong double distance(const line &a, const point &c) {\n return std::abs(det(c-a.a, a.b - a.a)/abs(a.b-a.a));\n}\n\nlong double distance(const line &a, const line &b) {\n if(intersection(a, b) < 2) {\n return 0;\n }\n else {\n return distance(a, b.a);\n }\n}\n\n}\n\nnamespace ebi {\n\nstruct line_segment {\n point a, b;\n\n line_segment() = default;\n\n line_segment(long double x1, long double y1, long double x2, long double y2) : a(x1, y1), b(x2, y2) { }\n\n line_segment(const point &a, const point &b) : a(a), b(b) { }\n};\n\n// 線分ab, cd が交わるか判定\nbool intersection_line_segment(const point &a, const point &b, const point &c, const point &d) {\n if(internal::sgn(isp(a,b,c)*isp(a,b,d)) <= 0 && internal::sgn(isp(c,d,a)*isp(c,d,b)) <= 0) {\n return true;\n }\n return false;\n}\n\n// 線分ab, cd が交わるか判定\nbool intersection(const line_segment &a, const line_segment &b) {\n return intersection_line_segment(a.a, a.b, b.a, b.b);\n}\n\nbool intersection(const line &a, const line_segment &b) {\n if(internal::sgn(det(a.b - a.a, b.a - a.a)) * internal::sgn(det(a.b - a.a, b.b - a.a)) < 0) {\n return true;\n }\n else {\n return false;\n }\n}\n\npoint cross_point(const line_segment &s, const line_segment &t) {\n assert(intersection(s, t));\n return s.a + (s.b - s.a) * det(t.a - s.a, t.b - t.a) / det(s.b - s.a, t.b - t.a);\n}\n\nlong double distance(const line_segment &a, const point &c) {\n if(internal::sgn(dot(a.b - a.a, c - a.a)) < 0) {\n return abs(c-a.a);\n }\n else if(internal::sgn(dot(a.a - a.b, c - a.b)) < 0) {\n return abs(c-a.b);\n }\n else {\n return std::abs(det(c - a.a, a.b - a.a)/abs(a.b-a.a));\n }\n}\n\nlong double distance(const line_segment &a, const line_segment &b) {\n if(intersection(a, b)) {\n return 0;\n }\n else {\n return std::min(std::min(distance(a, b.a), distance(a, b.b)), std::min(distance(b, a.a), distance(b, a.b)));\n }\n}\n\nlong double distance(const line &a, const line_segment &b) {\n if(intersection(a, b)) {\n return 0;\n }\n else {\n return std::min(distance(a, b.a), distance(a, b.b));\n }\n}\n\n}\n\nnamespace ebi {\n\nvoid main_() {\n long double x;\n while (std::cin >> x) {\n long double y;\n std::vector<point> t1(3), t2(3);\n std::cin >> y;\n t1[0] = {x, y};\n std::cin >> t1[1] >> t1[2] >> t2;\n std::vector<int> idx(3), jdx(3);\n std::iota(all(idx), 0);\n std::iota(all(jdx), 0);\n int ans = INF;\n\n auto move = [&](int i, int j, int k, std::vector<point> &t, point target) -> int {\n if(t[i] == target) return 0;\n int cnt = 0;\n line l1(t[j], t[j] + target - t[i]);\n line l2(t[k], t[k] + t[i] - t[j]);\n if(intersection(l1, l2) >= 2) return 1000;\n point p = cross_point(l1, l2);\n if(p != t[k]) cnt++;\n t[k] = p;\n t[i] = target;\n cnt++;\n return cnt;\n };\n do {\n do {\n {\n int cnt = 0;\n auto t = t1;\n cnt += move(idx[0], idx[1], idx[2], t, t2[jdx[0]]);\n cnt += move(idx[1], idx[0], idx[2], t, t2[jdx[1]]);\n line_segment lseg(t[idx[2]], t2[jdx[2]]);\n if(intersection(line(t[idx[0]], t[idx[1]]), lseg)) cnt = 1000;\n if(t[idx[2]] != t2[jdx[2]]) cnt++;\n chmin(ans, cnt);\n }\n {\n int cnt = 0;\n auto t = t1;\n cnt += move(idx[0], idx[2], idx[1], t, t2[jdx[0]]);\n cnt += move(idx[1], idx[0], idx[2], t, t2[jdx[1]]);\n line_segment lseg(t[idx[2]], t2[jdx[2]]);\n if(intersection(line(t[idx[0]], t[idx[1]]), lseg)) cnt = 1000;\n if(t[idx[2]] != t2[jdx[2]]) cnt++;\n chmin(ans, cnt);\n }\n } while(std::next_permutation(all(jdx)));\n } while(std::next_permutation(all(idx)));\n if(ans >= 5) {\n std::cout << \"Many\\n\";\n continue;\n }\n std::cout << ans << '\\n';\n }\n\n}\n\n} // namespace ebi\n\nint main() {\n std::cout << std::fixed << std::setprecision(15);\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n int t = 1;\n // std::cin >> t;\n while (t--) {\n ebi::main_();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3432, "score_of_the_acc": -0.7374, "final_rank": 6 }, { "submission_id": "aoj_1623_7111722", "code_snippet": "#include <cmath>\n#include <string>\n#include <vector>\n#include <cassert>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nint gcd(int x, int y) {\n\tif (y == 0) return x;\n\treturn gcd(y, x % y);\n}\n\nclass fraction {\npublic:\n\tint a, b;\n\tfraction() : a(0), b(1) {};\n\tfraction(int a_, int b_) {\n\t\tassert(b_ != 0);\n\t\tint g = gcd(abs(a_), abs(b_));\n\t\ta = a_ / g;\n\t\tb = b_ / g;\n\t\tif (b < 0) {\n\t\t\ta *= -1;\n\t\t\tb *= -1;\n\t\t}\n\t};\n\tbool operator==(const fraction& f) const {\n\t\treturn a == f.a && b == f.b;\n\t}\n\tbool operator!=(const fraction& f) const {\n\t\treturn a != f.a || b != f.b;\n\t}\n\tfraction operator+=(const fraction& f) {\n\t\t(*this) = fraction(a * f.b + f.a * b, b * f.b);\n\t\treturn (*this);\n\t}\n\tfraction operator-=(const fraction& f) {\n\t\t(*this) = fraction(a * f.b - f.a * b, b * f.b);\n\t\treturn (*this);\n\t}\n\tfraction operator*=(const fraction& f) {\n\t\t(*this) = fraction(a * f.a, b * f.b);\n\t\treturn (*this);\n\t}\n\tfraction operator/=(const fraction& f) {\n\t\tassert(f.a != 0);\n\t\t(*this) = fraction(a * f.b, b * f.a);\n\t\treturn (*this);\n\t}\n\tfraction operator+(const fraction& f) const {\n\t\treturn fraction(*this) += f;\n\t}\n\tfraction operator-(const fraction& f) const {\n\t\treturn fraction(*this) -= f;\n\t}\n\tfraction operator*(const fraction& f) const {\n\t\treturn fraction(*this) *= f;\n\t}\n\tfraction operator/(const fraction& f) const {\n\t\treturn fraction(*this) /= f;\n\t}\n\tstring to_string() const {\n\t\treturn std::to_string(a) + \"/\" + std::to_string(b);\n\t}\n};\n\nclass point2d {\npublic:\n\tfraction x, y;\n\tpoint2d() : x(fraction()), y(fraction()) {};\n\tpoint2d(const fraction& x_, const fraction& y_) : x(x_), y(y_) {};\n\tbool operator==(const point2d& p) const {\n\t\treturn x == p.x && y == p.y;\n\t}\n\tbool operator!=(const point2d& p) const {\n\t\treturn x != p.x || y != p.y;\n\t}\n\tpoint2d& operator+=(const point2d& p) {\n\t\tx += p.x;\n\t\ty += p.y;\n\t\treturn (*this);\n\t}\n\tpoint2d& operator-=(const point2d& p) {\n\t\tx -= p.x;\n\t\ty -= p.y;\n\t\treturn (*this);\n\t}\n\tpoint2d& operator*=(const fraction& f) {\n\t\tx *= f;\n\t\ty *= f;\n\t\treturn (*this);\n\t}\n\tpoint2d& operator/=(const fraction& f) {\n\t\tx /= f;\n\t\ty /= f;\n\t\treturn (*this);\n\t}\n\tpoint2d operator+(const point2d& p) const {\n\t\treturn point2d(*this) += p;\n\t}\n\tpoint2d operator-(const point2d& p) const {\n\t\treturn point2d(*this) -= p;\n\t}\n\tpoint2d operator*(const fraction& f) const {\n\t\treturn point2d(*this) *= f;\n\t}\n\tpoint2d operator/(const fraction& f) const {\n\t\treturn point2d(*this) /= f;\n\t}\n\tfraction dot(const point2d& p) const {\n\t\treturn x * p.x + y * p.y;\n\t}\n\tfraction cross(const point2d& p) const {\n\t\treturn x * p.y - y * p.x;\n\t}\n};\n\nconst int INF = 1012345678;\n\n// find status of crossing of two lines [p1 + v1 * t and p2 + v2 * t]\nint cross_status(const point2d& p1, const point2d& v1, const point2d& p2, const point2d& v2) {\n\tassert(!(v1.x == fraction(0, 1) && v1.y == fraction(0, 1)) && !(v2.x == fraction(0, 1) && v2.y == fraction(0, 1)));\n\tif (v1.cross(v2) != fraction(0, 1)) {\n\t\treturn 100; // one cross point\n\t}\n\tif ((p2 - p1).cross(v1) != fraction(0, 1)) {\n\t\treturn 101; // no cross point\n\t}\n\treturn 102; // cross line\n}\n\n// find cross point of two lines [p1 + v1 * t and p2 + v2 * t]\npoint2d cross_point(const point2d& p1, const point2d& v1, const point2d& p2, const point2d& v2) {\n\tassert(cross_status(p1, v1, p2, v2) == 100);\n\t// p1 + v1 * t1 = p2 + v2 * t2\n\t// ==> (-v1) * t1 + v2 * t2 = p1 - p2\n\t// ==> [ -v1, v2 ] * [ [ t1 ], [ t2 ] ] = p1 - p2\n\t// ==> [ [ t1 ], [ t2 ] ] = [ -v1, v2 ]^-1 * (p1 - p2) = 1/(-v1.x*v2.y+v1.y*v2.x) * [ [ v2.y, -v2.x ], [ v1.y, -v1.x ] ] * (p1 - p2)\n\tfraction detinv = fraction(1, 1) / (v1.y * v2.x - v1.x * v2.y);\n\tfraction t1 = detinv * (v2.y * (p1 - p2).x - v2.x * (p1 - p2).y);\n\treturn p1 + v1 * t1;\n}\n\n// solve for 0->1->0 case\nbool solve_010(const vector<point2d>& TA, const vector<point2d>& TB) {\n\tassert(TA[2] == TB[2]);\n\tint status = cross_status(TA[0], TA[2] - TA[1], TB[0], TB[2] - TB[1]);\n\tif (status == 100) {\n\t\tpoint2d p = cross_point(TA[0], TA[2] - TA[1], TB[0], TB[2] - TB[1]);\n\t\treturn ((TB[1] - TA[1]).cross(TA[2] - p) == fraction(0, 1));\n\t}\n\tif (status == 101) {\n\t\treturn false;\n\t}\n\tif (status == 102) {\n\t\treturn false;\n\t}\n\treturn false;\n}\n\n// solve for specific case\nint solve_target(const vector<point2d>& TA, const vector<point2d>& TB) {\n\t// step #1. check for 0->1->2\n\tbool flag1 = true;\n\tif ((TB[0] - TA[0]).cross(TA[2] - TA[1]) != fraction(0, 1)) flag1 = false;\n\tif ((TB[1] - TA[1]).cross(TA[2] - TB[0]) != fraction(0, 1)) flag1 = false;\n\tif ((TB[2] - TA[2]).cross(TB[1] - TB[0]) != fraction(0, 1)) flag1 = false;\n\tif (flag1 == true) {\n\t\treturn 3;\n\t}\n\n\t// step #2. check for 0->1->0->2\n\tif ((TB[2] - TA[2]).cross(TB[1] - TB[0]) == fraction(0, 1)) {\n\t\tbool flag2 = solve_010(TA, vector<point2d>({ TB[0], TB[1], TA[2] }));\n\t\tif (flag2 == true) {\n\t\t\treturn 4;\n\t\t}\n\t}\n\n\t// step #3. check for 2->0->1->0\n\tif ((TB[2] - TA[2]).cross(TA[1] - TA[0]) == fraction(0, 1)) {\n\t\tbool flag3 = solve_010(vector<point2d>({ TA[0], TA[1], TB[2] }), TB);\n\t\tif (flag3 == true) {\n\t\t\treturn 4;\n\t\t}\n\t}\n\n\t// step #4. check for 0->1->2->0\n\tint status = cross_status(TA[0], TA[2] - TA[1], TB[0], TB[2] - TB[1]);\n\tif (status == 100) {\n\t\tpoint2d p = cross_point(TA[0], TA[2] - TA[1], TB[0], TB[2] - TB[1]);\n\t\tbool flag4 = true;\n\t\tif ((TB[1] - TA[1]).cross(TA[2] - p) != fraction(0, 1)) flag4 = false;\n\t\tif ((TB[2] - TA[2]).cross(TB[1] - p) != fraction(0, 1)) flag4 = false;\n\t\tif (flag4 == true) {\n\t\t\treturn 4;\n\t\t}\n\t}\n\tif (status == 101) {\n\t\t// NO CHANCE!\n\t}\n\tif (status == 102) {\n\t\tfraction c1 = (TB[0] - TA[0]).cross(TA[1] - TA[0]);\n\t\tfraction c2 = (TB[0] - TA[0]).cross(TB[1] - TB[0]);\n\t\tfraction c3 = (TA[2] - TA[1]).cross(TB[1] - TA[1]);\n\t\tif ((c1.a > 0) == (c2.a > 0) && c3.a != 0) {\n\t\t\treturn 4;\n\t\t}\n\t}\n\n\t// no chance\n\treturn INF;\n}\n\n// solve for general case\nint solve(const vector<point2d>& TA, const vector<point2d>& TB) {\n\tconst vector<vector<int> > perms = { { 0, 1, 2 }, { 0, 2, 1 }, { 1, 0, 2 }, { 1, 2, 0 }, { 2, 0, 1 }, { 2, 1, 0 } };\n\tint res = INF;\n\tfor (int i = 0; i < 6; i++) {\n\t\tfor (int j = 0; j < 6; j++) {\n\t\t\tint subres = solve_target(vector<point2d>({ TA[perms[i][0]], TA[perms[i][1]], TA[perms[i][2]] }), vector<point2d>({ TB[perms[j][0]], TB[perms[j][1]], TB[perms[j][2]] }));\n\t\t\t// cout << i << ' ' << j << ' ' << subres << endl;\n\t\t\tres = min(res, subres);\n\t\t}\n\t}\n\treturn res;\n}\n\nint main() {\n\tint x, y;\n\twhile (cin >> x >> y) {\n\t\tvector<point2d> TA(3), TB(3);\n\t\tTA[0].x = fraction(x, 1);\n\t\tTA[0].y = fraction(y, 1);\n\t\tfor (int i = 1; i < 3; i++) {\n\t\t\tcin >> x >> y;\n\t\t\tTA[i].x = fraction(x, 1);\n\t\t\tTA[i].y = fraction(y, 1);\n\t\t}\n\t\tfor (int i = 0; i < 3; i++) {\n\t\t\tcin >> x >> y;\n\t\t\tTB[i].x = fraction(x, 1);\n\t\t\tTB[i].y = fraction(y, 1);\n\t\t}\n\t\tint res = solve(TA, TB);\n\t\tif (res != INF) {\n\t\t\tcout << res << endl;\n\t\t}\n\t\telse {\n\t\t\tcout << \"Many\" << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3348, "score_of_the_acc": -0.5401, "final_rank": 2 }, { "submission_id": "aoj_1623_6788750", "code_snippet": "#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstddef>\n#include <cstdint>\n#include <cstdlib>\n#include <cstring>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n/* macro */\n\n#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)\n#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)\n#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)\n#define RRep(i, a, n) for (i64 i = ((i64)(n - i64(1))); i >= (i64)(a); i--)\n#define all(v) (v).begin(), (v).end()\n#define rall(v) (v).rbegin(), (v).rend()\n#define Bit(n) (1LL << (n))\n\n/* macro end */\n\n/* template */\n\nnamespace ebi {\n\n#ifdef LOCAL\n#define debug(...) \\\n std::cerr << \"LINE: \" << __LINE__ << \" [\" << #__VA_ARGS__ << \"]:\", \\\n debug_out(__VA_ARGS__)\n#else\n#define debug(...)\n#endif\n\nvoid debug_out() { std::cerr << std::endl; }\n\ntemplate <typename Head, typename... Tail>\nvoid debug_out(Head h, Tail... t) {\n std::cerr << \" \" << h;\n if (sizeof...(t) > 0) std::cout << \" :\";\n debug_out(t...);\n}\n\ntemplate <typename T1, typename T2>\nstd::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {\n return os << pa.first << \" \" << pa.second;\n}\n\ntemplate <typename T1, typename T2>\nstd::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {\n return os >> pa.first >> pa.second;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {\n for (std::size_t i = 0; i < vec.size(); i++)\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \" \");\n return os;\n}\n\ntemplate <typename T>\nstd::istream &operator>>(std::istream &os, std::vector<T> &vec) {\n for (T &e : vec) std::cin >> e;\n return os;\n}\n\ntemplate <typename T>\nstd::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {\n if (opt) {\n os << opt.value();\n } else {\n os << \"invalid value\";\n }\n return os;\n}\n\nusing std::size_t;\nusing i32 = std::int32_t;\nusing u32 = std::uint32_t;\nusing i64 = std::int64_t;\nusing u64 = std::uint64_t;\n\ntemplate <class T, T init>\nauto make_vector(int n) {\n return std::vector<T>(n, init);\n}\n\ntemplate <class T, T init, typename Head, typename... Tail>\nauto make_vector(Head n, Tail... ts) {\n return std::vector(n, make_vector<T, init>(ts...));\n}\n\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\n\ntemplate <class T>\nT pow(T x, i64 n) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = res * x;\n x = x * x;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT mod_pow(T x, i64 n, i64 mod) {\n T res = 1;\n while (n > 0) {\n if (n & 1) res = (res * x) % mod;\n x = (x * x) % mod;\n n >>= 1;\n }\n return res;\n}\n\ntemplate <class T>\nT scan() {\n T val;\n std::cin >> val;\n return val;\n}\n\ntemplate <class T>\nstruct Edge {\n int to;\n T cost;\n Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}\n};\n\ntemplate <class T>\nstruct Graph : std::vector<std::vector<Edge<T>>> {\n using std::vector<std::vector<Edge<T>>>::vector;\n void add_edge(int u, int v, T w, bool directed = false) {\n (*this)[u].emplace_back(v, w);\n if (directed) return;\n (*this)[v].emplace_back(u, w);\n }\n};\n\nstruct graph : std::vector<std::vector<int>> {\n using std::vector<std::vector<int>>::vector;\n void add_edge(int u, int v, bool directed = false) {\n (*this)[u].emplace_back(v);\n if (directed) return;\n (*this)[v].emplace_back(u);\n }\n};\n\nconstexpr i64 LNF = std::numeric_limits<i64>::max() / 4;\n\nconstexpr int INF = std::numeric_limits<int>::max() / 2;\n\nconst std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};\nconst std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};\n\n} // namespace ebi\n\nnamespace ebi {\n\nconstexpr long double EPS = 1e-7;\n\nconst long double PI = std::acos(-1);\n\nnamespace internal {\n\nint sgn(long double a) {\n return (a<-EPS) ? -1 : (a>EPS) ? 1 : 0;\n}\n\nlong double add(long double a, long double b) {\n if(std::abs(a+b) < EPS*(std::abs(a) + std::abs(b))) return 0;\n return a+b;\n}\n\n} // namespace internal\n\n\nlong double arg_to_radian(long double arg) {\n return PI * arg / (long double)(180);\n}\n\nstruct point {\n long double x,y;\n\n point() = default;\n\n point(long double x, long double y) : x(x), y(y) { }\n\n point &operator+=(const point rhs) noexcept {\n x = internal::add(x, rhs.x);\n y = internal::add(y, rhs.y);\n return *this;\n }\n\n point &operator-=(const point rhs) noexcept {\n x = internal::add(x, -rhs.x);\n y = internal::add(y, -rhs.y);\n return *this;\n }\n\n point &operator*=(const point rhs) noexcept {\n long double _x = internal::add(x*rhs.x, -y*rhs.y);\n long double _y = internal::add(x*rhs.y, y*rhs.x);\n x = _x;\n y = _y;\n return *this;\n }\n\n point &operator*=(const long double k) noexcept {\n x *= k;\n y *= k;\n return *this;\n }\n\n point &operator/=(const long double k) {\n assert(internal::sgn(k)!=0);\n x /= k;\n y /= k;\n return *this;\n }\n\n point operator+(const point &rhs) const noexcept {\n return point(*this) += rhs;\n }\n\n point operator-(const point &rhs) const noexcept {\n return point(*this) -= rhs;\n }\n\n point operator*(const point &rhs) const noexcept {\n return point(*this) *= rhs;\n }\n\n point operator*(const long double rhs) const noexcept {\n return point(*this) *= rhs;\n }\n\n point operator/(const long double rhs) const {\n return point(*this) /= rhs;\n }\n\n point operator-() const noexcept {\n return point(0, 0) - *this;\n }\n\n long double abs() const noexcept {\n return std::sqrt(internal::add(x*x, y*y));\n }\n\n long double dot(const point rhs) const noexcept {\n return internal::add(x*rhs.x, y*rhs.y);\n }\n\n long double det(const point rhs) const noexcept {\n return internal::add(x*rhs.y, -y*rhs.x);\n }\n\n // arctan(y/x) (単位はラジアン)\n\n long double arg() const {\n return std::atan2(y, x);\n }\n\n // x昇順, その後y昇順\n\n bool operator<(const point &rhs) const noexcept {\n if(internal::sgn(x-rhs.x)) return internal::sgn(x-rhs.x)<0;\n return internal::sgn(y-rhs.y)<0;\n }\n\n bool operator==(const point &rhs) const noexcept {\n if(internal::sgn(x - rhs.x) == 0 && internal::sgn(y - rhs.y) == 0) return true;\n else return false;\n }\n\n bool operator!=(const point &rhs) const noexcept {\n return !(*this == rhs);\n }\n};\n\nstd::ostream& operator<<(std::ostream& os, const point &a) {\n return os << a.x << \" \" << a.y;\n}\n\nstd::istream& operator>>(std::istream& os, point &a) {\n return os >> a.x >> a.y;\n}\n\npoint conj(const point &a) {\n return point(a.x, -a.y);\n}\n\n// 点a をang(ラジアン)回転する\n\npoint rot(const point &a, long double ang) {\n return point(std::cos(ang) * a.x - std::sin(ang) * a.y, std::sin(ang) * a.x + std::cos(ang) * a.y);\n} \n\npoint rot90(const point &a) {\n return point(-a.y, a.x);\n}\n\nlong double dot(const point &a, const point &b) {\n return a.dot(b);\n}\n\nlong double det(const point &a, const point &b) {\n return a.det(b);\n}\n\nlong double abs(const point &a) {\n return a.abs();\n}\n\nlong double norm(const point &a) {\n return internal::add(a.x*a.x, a.y*a.y);\n}\n\nint isp(const point &a, const point &b, const point &c) {\n int flag = internal::sgn(det(b-a,c-a));\n if(flag == 0) {\n if(internal::sgn(dot(b-a, c-a))<0) return -2;\n if(internal::sgn(dot(a-b, c-b))<0) return +2;\n }\n return flag;\n}\n\n// 分割統治で最近点対を求める O(N log N)\n\nlong double closest_pair(std::vector<point> p) {\n std::sort(p.begin(), p.end());\n int n = p.size();\n auto f = [&](auto &&self, int l, int r) -> long double {\n if(r-l == 1) {\n return 1e9;\n }\n int mid = (l+r)/2;\n long double x = p[mid].x;\n long double d = std::min(self(self, l, mid), self(self, mid, r));\n std::vector<point> b;\n b.reserve(r-l);\n int j = mid;\n for(int i = l; i < mid; i++) {\n while(j < r && p[j].y <= p[i].y) {\n b.emplace_back(p[j++]);\n }\n b.emplace_back(p[i]);\n }\n while(j < r) {\n b.emplace_back(p[j++]);\n }\n for(int i = 0; i < r-l; i++) {\n p[l+i] = b[i];\n }\n b.clear();\n for(int i = l; i < r; i++) {\n if(std::abs(p[i].x - x) >= d) continue;\n for(int j = int(b.size())-1; j >= 0; j--) {\n if(p[i].y - b[j].y >= d) break;\n d = std::min(d, abs(p[i]-b[j]));\n }\n b.emplace_back(p[i]);\n }\n return d;\n };\n return f(f, 0, n);\n}\n\n// ∠ABCを求める(ラジアン)\n\nlong double angle(const point &A, const point &B, const point &C) {\n long double a = (B - C).abs(), b = (C - A).abs(), c = (A - B).abs();\n long double cos = internal::add(internal::add(a*a, c*c), -b*b)/(2.0*c*a);\n return std::acos(cos);\n}\n\nvoid arg_sort(std::vector<point> &a) {\n int n = a.size();\n std::vector ps(4, std::vector<point>());\n auto idx = [](point v) -> int {\n if(v.y >= 0) return (v.x >= 0) ? 0 : 1;\n else return (v.x >= 0) ? 3 : 2;\n };\n for(auto p: a) {\n assert(!(p.x == 0 && p.y == 0));\n ps[idx(p)].emplace_back(p);\n }\n a.clear();\n a.reserve(n);\n for(int i = 0; i < 4; i++) {\n std::sort(ps[i].begin(), ps[i].end(), \n [](point &p1, point &p2) -> bool {\n int flag = internal::sgn(internal::add(p1.x * p2.y, - p2.x * p1.y));\n return flag == 0 ? (norm(p1) < norm(p2)) : flag > 0;\n });\n for(auto &p: ps[i]) a.emplace_back(p);\n }\n return;\n}\n\ntemplate<class T>\nvoid arg_sort_ll(std::vector<std::pair<T , T>> &a) {\n using Point = std::pair<T, T>;\n int n = a.size();\n std::vector ps(4, std::vector<Point>());\n auto idx = [](Point v) -> int {\n if(v.second >= 0) return (v.first >= 0) ? 0 : 1;\n else return (v.first >= 0) ? 3 : 2;\n };\n for(auto p: a) {\n assert(!(p.first == 0 && p.second == 0));\n ps[idx(p)].emplace_back(p);\n }\n a.clear();\n a.reserve(n);\n for(int i = 0; i < 4; i++) {\n std::sort(ps[i].begin(), ps[i].end(), \n [](Point &p1, Point &p2) -> bool { \n T flag = p1.first * p2.second - p2.first * p1.second;\n return flag == 0 ? (p1.first * p1.first + p1.second * p1.second < p2.first * p2.first + p2.second * p2.second) : flag > 0;\n });\n for(auto &p: ps[i]) a.emplace_back(p);\n }\n return;\n}\n\n}\n\nnamespace ebi {\n\nstruct line {\n point a,b;\n\n line(long double x1, long double y1, long double x2, long double y2) : a(x1, y1), b(x2, y2) { }\n\n line(const point &a, const point &b) : a(a), b(b) { }\n\n point proj(const point &p) const {\n return a + (b-a)*(dot(b-a,p-a)/norm(b-a));\n }\n\n point relf(const point &p) const {\n return proj(p)*double(2) - p;\n }\n\n long double distance(const point &c) const {\n return std::abs(det(c - a, b - a)/abs(b-a));\n }\n};\n\nint intersection(const line &a, const line &b) {\n if(internal::sgn(det(a.b-a.a, b.a-b.b)) != 0) {\n if(internal::sgn(dot(a.b-a.a, b.b-b.a)) == 0) { // 垂直\n return 1;\n }\n return 0; // 交差\n }\n else if(internal::sgn(det(a.b-a.a, b.a-a.a)) != 0) { // 平行\n return 2;\n }\n else { // 同一直線\n return 3;\n }\n}\n\npoint cross_point(const point &a, const point &b, const point &c, const point &d) {\n return a + (b-a) * det(c - a, d - c) / det(b - a, d - c);\n}\n\n// 交点があるか確認する!\npoint cross_point(const line &s, const line &t) {\n assert(intersection(s, t) < 2);\n return s.a + (s.b - s.a) * det(t.a - s.a, t.b - t.a) / det(s.b - s.a, t.b - t.a);\n}\n\n// 直線aと点cの距離\nlong double distance(const line &a, const point &c) {\n return std::abs(det(c-a.a, a.b - a.a)/abs(a.b-a.a));\n}\n\nlong double distance(const line &a, const line &b) {\n if(intersection(a, b) < 2) {\n return 0;\n }\n else {\n return distance(a, b.a);\n }\n}\n\n}\n\nnamespace ebi {\n\nstruct line_segment {\n point a, b;\n\n line_segment() = default;\n\n line_segment(long double x1, long double y1, long double x2, long double y2) : a(x1, y1), b(x2, y2) { }\n\n line_segment(const point &a, const point &b) : a(a), b(b) { }\n};\n\n// 線分ab, cd が交わるか判定\nbool intersection_line_segment(const point &a, const point &b, const point &c, const point &d) {\n if(internal::sgn(isp(a,b,c)*isp(a,b,d)) <= 0 && internal::sgn(isp(c,d,a)*isp(c,d,b)) <= 0) {\n return true;\n }\n return false;\n}\n\n// 線分ab, cd が交わるか判定\nbool intersection(const line_segment &a, const line_segment &b) {\n return intersection_line_segment(a.a, a.b, b.a, b.b);\n}\n\nbool intersection(const line &a, const line_segment &b) {\n if(internal::sgn(det(a.b - a.a, b.a - a.a)) * internal::sgn(det(a.b - a.a, b.b - a.a)) < 0) {\n return true;\n }\n else {\n return false;\n }\n}\n\npoint cross_point(const line_segment &s, const line_segment &t) {\n assert(intersection(s, t));\n return s.a + (s.b - s.a) * det(t.a - s.a, t.b - t.a) / det(s.b - s.a, t.b - t.a);\n}\n\nlong double distance(const line_segment &a, const point &c) {\n if(internal::sgn(dot(a.b - a.a, c - a.a)) < 0) {\n return abs(c-a.a);\n }\n else if(internal::sgn(dot(a.a - a.b, c - a.b)) < 0) {\n return abs(c-a.b);\n }\n else {\n return std::abs(det(c - a.a, a.b - a.a)/abs(a.b-a.a));\n }\n}\n\nlong double distance(const line_segment &a, const line_segment &b) {\n if(intersection(a, b)) {\n return 0;\n }\n else {\n return std::min(std::min(distance(a, b.a), distance(a, b.b)), std::min(distance(b, a.a), distance(b, a.b)));\n }\n}\n\nlong double distance(const line &a, const line_segment &b) {\n if(intersection(a, b)) {\n return 0;\n }\n else {\n return std::min(distance(a, b.a), distance(a, b.b));\n }\n}\n\n}\n\nnamespace ebi {\n\nvoid main_() {\n long double x;\n while (std::cin >> x) {\n long double y;\n std::vector<point> t1(3), t2(3);\n std::cin >> y;\n t1[0] = {x, y};\n std::cin >> t1[1] >> t1[2] >> t2;\n std::vector<int> idx(3), jdx(3);\n std::iota(all(idx), 0);\n std::iota(all(jdx), 0);\n int ans = INF;\n\n auto move = [&](int i, int j, int k, std::vector<point> &t, point target) -> int {\n if(t[i] == target) return 0;\n int cnt = 0;\n line l1(t[j], t[j] + target - t[i]);\n line l2(t[k], t[k] + t[i] - t[j]);\n if(intersection(l1, l2) >= 2) return 1000;\n point p = cross_point(l1, l2);\n if(p != t[k]) cnt++;\n t[k] = p;\n t[i] = target;\n cnt++;\n return cnt;\n };\n do {\n do {\n {\n int cnt = 0;\n auto t = t1;\n cnt += move(idx[0], idx[1], idx[2], t, t2[jdx[0]]);\n cnt += move(idx[1], idx[0], idx[2], t, t2[jdx[1]]);\n line_segment lseg(t[idx[2]], t2[jdx[2]]);\n if(intersection(line(t[idx[0]], t[idx[1]]), lseg)) cnt = 1000;\n if(t[idx[2]] != t2[jdx[2]]) cnt++;\n chmin(ans, cnt);\n }\n {\n int cnt = 0;\n auto t = t1;\n cnt += move(idx[0], idx[2], idx[1], t, t2[jdx[0]]);\n cnt += move(idx[1], idx[0], idx[2], t, t2[jdx[1]]);\n line_segment lseg(t[idx[2]], t2[jdx[2]]);\n if(intersection(line(t[idx[0]], t[idx[1]]), lseg)) cnt = 1000;\n if(t[idx[2]] != t2[jdx[2]]) cnt++;\n chmin(ans, cnt);\n }\n } while(std::next_permutation(all(jdx)));\n } while(std::next_permutation(all(idx)));\n if(ans >= 5) {\n std::cout << \"Many\\n\";\n continue;\n }\n std::cout << ans << '\\n';\n }\n\n}\n\n} // namespace ebi\n\nint main() {\n std::cout << std::fixed << std::setprecision(15);\n std::cin.tie(nullptr);\n std::ios::sync_with_stdio(false);\n int t = 1;\n // std::cin >> t;\n while (t--) {\n ebi::main_();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3492, "score_of_the_acc": -0.8803, "final_rank": 13 }, { "submission_id": "aoj_1623_6681940", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <cstdio>\n#include <ctime>\n#include <assert.h>\n#include <chrono>\n#include <random>\n#include <numeric>\n#include <set>\n#include <deque>\n#include <stack>\n#include <sstream>\n#include <utility>\n#include <cstring>\n#include <unordered_map>\n#include <unordered_set>\n#include <tuple>\n#include <array>\n#include <bitset>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) {\n return (ull)rng() % B;\n}\ninline double time() {\n return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;\n}\n\nconst double eps=1e-8;\n\nstruct point{\n double x,y;\n point() {}\n point(double x,double y): x(x), y(y) {}\n point& operator-=(const point &p){\n x -= p.x; y -= p.y;\n return *this;\n }\n point& operator+=(const point &p){\n x += p.x; y += p.y;\n return *this;\n }\n point& operator*=(double d){\n x *= d; y *= d;\n return *this;\n }\n point& operator/=(double d){\n x /= d; y /= d;\n return *this;\n }\n const point operator+ (const point& p) const;\n const point operator- (const point& p) const;\n const point operator* (double d) const;\n const point operator/ (double d) const;\n const bool operator == (const point& p) const;\n};\nconst point point::operator+ (const point& p) const{\n point res(*this); return res += p;\n}\nconst point point::operator- (const point& p) const{\n point res(*this); return res -= p;\n}\nconst point point::operator* (double d) const{\n point res(*this); return res *= d;\n}\nconst point point::operator/ (double d) const{\n point res(*this); return res /= d;\n}\nconst bool point::operator == (const point& p) const{\n point pp(*this);\n return (p.x == pp.x and p.y == pp.y);\n}\nstruct line{\n point A,B;\n line() {}\n line(point A, point B): A(A), B(B) {}\n};\n\n// BOJ-6487\n// 平行判定\nbool is_parallel(line l1, line l2){\n double dx1 = l1.B.x - l1.A.x;\n double dy1 = l1.B.y - l1.A.y;\n double dx2 = l2.B.x - l2.A.x;\n double dy2 = l2.B.y - l2.A.y;\n return abs(dy1*dx2 - dy2*dx1) < eps;\n}\n\npoint vec(line L){\n return (L.B - L.A);\n}\ndouble cross(point a,point b){\n return a.x*b.y-a.y*b.x;\n}\n// AOJ 2596\n// 直線の交点\npoint line_intersection(line l1, line l2){\n return l1.A + vec(l1) * cross(l2.A - l1.A, vec(l2)) / cross(vec(l1), vec(l2));\n}\n\nint main(){\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n array<point, 3> P,Q;\n while(cin >> P[0].x >> P[0].y >> P[1].x >> P[1].y >> P[2].x >> P[2].y){\n cin >> Q[0].x >> Q[0].y >> Q[1].x >> Q[1].y >> Q[2].x >> Q[2].y;\n int res = 5;\n auto dfs=[&](auto dfs,array<point,3> s,int cnt)->void{\n\n auto check=[&]()->int{\n vector<int> p={0,1,2};\n int mx = 0;\n do{\n int cnt = 0;\n for(int i=0;i<3;i++){\n if(s[p[i]] == Q[i])cnt++;\n }\n mx = max(mx, cnt);\n }while(next_permutation(p.begin(), p.end()));\n return mx;\n };\n\n int u = check();\n // 揃った\n if(u == 3){\n res = min(res, cnt); return;\n }\n // 枝刈り\n if(cnt+3-u >= res) return;\n\n // s[i]をQ[j]に揃えたい\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n if(s[i] == Q[j])continue;\n\n // 直接揃えられる\n {\n line L1 = {s[i], Q[j]};\n line L2;\n if(i == 0) L2 = {s[1], s[2]};\n else if(i == 1) L2 = {s[0], s[2]};\n else L2 = {s[0], s[1]};\n\n if(is_parallel(L1, L2)){\n auto p = s[i];\n s[i] = Q[j];\n dfs(dfs,s,cnt+1);\n s[i] = p;\n }\n }\n // 1点経由\n if(3-u+1 < res){\n int k1 = -1, k2 = -1;\n for(int k=0;k<3;k++){\n if(i == k)continue;\n if(k1 == -1) k1 = k;\n else k2 = k;\n }\n // k2をずらす\n {\n line L1 = {s[k1], Q[j]-s[i]+s[k1]};\n line L2 = {s[k2], s[i]-s[k1]+s[k2]};\n auto p = line_intersection(L1, L2);\n auto pp = s[k2];\n s[k2] = p;\n dfs(dfs,s,cnt+1);\n s[k2] = pp;\n }\n swap(k1,k2);\n // k1をずらす\n {\n line L1 = {s[k1], Q[j]-s[i]+s[k1]};\n line L2 = {s[k2], s[i]-s[k1]+s[k2]};\n auto p = line_intersection(L1, L2);\n auto pp = s[k2];\n s[k2] = p;\n dfs(dfs,s,cnt+1);\n s[k2] = pp;\n } \n }\n }\n }\n\n };\n dfs(dfs,P,0);\n if(res >= 5){\n cout << \"Many\" << \"\\n\";\n }\n else{\n cout << res << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3484, "score_of_the_acc": -0.8599, "final_rank": 11 }, { "submission_id": "aoj_1623_6598121", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)\n#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)\n#define all(x) begin(x), end(x)\ntemplate <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }\ntemplate <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }\n\nusing T = double;\nusing Vec = std::complex<T>;\n\nconst T PI = std::acos(-1);\n\nconstexpr T eps = 1e-8;\ninline bool eq(T a, T b) { return std::abs(a - b) < eps; }\ninline bool eq(Vec a, Vec b) { return std::abs(a - b) < eps; }\ninline bool lt(T a, T b) { return a < b - eps; }\ninline bool leq(T a, T b) { return a < b + eps; }\n\nstd::istream& operator>>(std::istream& is, Vec& p) {\n T x, y;\n is >> x >> y;\n p = {x, y};\n return is;\n}\n\nstruct Line {\n Vec p1, p2;\n Line() = default;\n Line(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Segment {\n Vec p1, p2;\n Segment() = default;\n Segment(const Vec& p1, const Vec& p2) : p1(p1), p2(p2) {}\n Vec dir() const { return p2 - p1; }\n};\n\nstruct Circle {\n Vec c;\n T r;\n Circle() = default;\n Circle(const Vec& c, T r) : c(c), r(r) {}\n};\n\nusing Polygon = std::vector<Vec>;\n\nT dot(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).real();\n}\n\nT cross(const Vec& a, const Vec& b) {\n return (std::conj(a) * b).imag();\n}\n\nVec rot(const Vec& a, T ang) {\n return a * Vec(std::cos(ang), std::sin(ang));\n}\n\nVec perp(const Vec& a) {\n return Vec(-a.imag(), a.real());\n}\n\nVec projection(const Line& l, const Vec& p) {\n return l.p1 + dot(p - l.p1, l.dir()) * l.dir() / std::norm(l.dir());\n}\n\nVec reflection(const Line& l, const Vec& p) {\n return T(2) * projection(l, p) - p;\n}\n\n// 0: collinear\n// 1: counter-clockwise\n// 2: clockwise\nint ccw(const Vec& a, const Vec& b, const Vec& c) {\n if (eq(cross(b - a, c - a), 0)) return 0;\n if (lt(cross(b - a, c - a), 0)) return -1;\n return 1;\n}\n\nvoid sort_by_arg(std::vector<Vec>& pts) {\n std::sort(pts.begin(), pts.end(), [&](auto& p, auto& q) {\n if ((p.imag() < 0) != (q.imag() < 0)) return (p.imag() < 0);\n if (cross(p, q) == 0) {\n if (p == Vec(0, 0)) return !(q.imag() < 0 || (q.imag() == 0 && q.real() > 0));\n if (q == Vec(0, 0)) return (p.imag() < 0 || (p.imag() == 0 && p.real() > 0));\n return (p.real() > q.real());\n }\n return (cross(p, q) > 0);\n });\n}\n\nVec intersection(const Line& l, const Line& m) {\n Vec r = m.p1 - l.p1;\n assert(!eq(cross(l.dir(), m.dir()), 0)); // not parallel\n return l.p1 + cross(m.dir(), r) / cross(m.dir(), l.dir()) * l.dir();\n}\n\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (int i = 0; i < (int) v.size(); ++i) os << v[i] << \" \";\n return os;\n}\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout << fixed << setprecision(15);\n\n auto check2 = [&](vector<Vec> a, vector<Vec> b) {\n if (!eq(a[0], b[0])) {\n return false;\n }\n Line l(b[1], b[1]+b[2]-b[0]);\n if (!eq(cross(l.p1 - a[1], l.p2 - a[1]), 0)) return false;\n b[1] = a[1];\n return ccw(a[0], a[1], a[2]) == ccw(b[0], b[1], b[2]);\n };\n\n auto check3 = [&](vector<Vec> a, vector<Vec> b) {\n if (eq(a[0], b[0])) {\n Line m(b[0], b[0]+b[1]-a[1]);\n Line n(b[2], b[2]+b[1]-b[0]);\n if (!eq(cross(m.dir(), n.dir()), 0)) {\n auto p = intersection(m, n);\n b[2] = p;\n return check2(a, b);\n }\n return false;\n } else {\n Line l(b[0], b[0]+b[2]-b[1]);\n if (!eq(cross(l.p1 - a[0], l.p2 - a[0]), 0)) return false;\n b[0] = a[0];\n return check2(a, b);\n }\n };\n\n auto check4 = [&](vector<Vec> a, vector<Vec> b) {\n Line l(b[0], b[0]+b[2]-b[1]);\n if (eq(cross(l.p1 - a[0], l.p2 - a[0]), 0)) {\n auto tmp = b[0];\n b[0] = a[0];\n if (check3(a, b)) return true;\n b[0] = tmp;\n }\n // move b[2]\n {\n Line m(b[1], b[1]+b[0]-a[0]);\n Line n(b[2], b[2]+b[1]-b[0]);\n if (!eq(cross(m.dir(), n.dir()), 0)) {\n auto p = intersection(m, n);\n auto tmp = b[2];\n b[2] = p;\n if (check3(a, b)) return true;\n b[2] = tmp;\n }\n }\n // move b[1]\n {\n Line m(b[2], b[2]+b[0]-a[0]);\n Line n(b[1], b[1]+b[2]-b[0]);\n if (!eq(cross(m.dir(), n.dir()), 0)) {\n auto p = intersection(m, n);\n auto tmp = b[1];\n b[1] = p;\n if (check3(a, b)) return true;\n b[1] = tmp;\n }\n }\n return false;\n };\n\n while (true) {\n vector<Vec> a(3), b(3);\n for (auto& x : a) cin >> x;\n for (auto& x : b) cin >> x;\n swap(a, b);\n vector<int> perm1 = {0, 1, 2};\n int ans = 5;\n do {\n vector<Vec> aa(3);\n rep(i,0,3) aa[i] = a[perm1[i]];\n vector<int> perm2 = {0, 1, 2};\n do {\n vector<Vec> bb(3);\n rep(i,0,3) bb[i] = b[perm2[i]];\n\n if (check3(aa, bb)) {\n chmin(ans, 3);\n } else if (check4(aa, bb)) {\n chmin(ans, 4);\n }\n } while (next_permutation(all(perm2)));\n } while (next_permutation(all(perm1)));\n if (ans < 5) cout << ans;\n else cout << \"Many\";\n cout << \"\\n\";\n string s;\n getline(cin, s);\n if (!getline(cin, s)) break;\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3544, "score_of_the_acc": -1.0014, "final_rank": 16 }, { "submission_id": "aoj_1623_6030341", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\n//constexpr long long int MOD = 1000000007;\nconstexpr long long int MOD = 998244353;\nconstexpr long double EPS = 1e-7;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nvector<long double>Sx;\nvector<long double>Sy;\n\nstruct Point {\n\tlong double x;\n\tlong double y;\n\tPoint(long double xx, long double yy) {\n\t\tx = xx, y = yy;\n\t}\n\tPoint() {\n\n\t}\n\tvoid Input() {\n\t\tcin >> x >> y;\n\t}\n\tPoint operator + (const Point& c)const {\n\t\tPoint box;\n\t\tbox.x = x + c.x;\n\t\tbox.y = y + c.y;\n\t\treturn box;\n\t}\n\tPoint operator - (const Point& c)const {\n\t\tPoint box;\n\t\tbox.x = x - c.x;\n\t\tbox.y = y - c.y;\n\t\treturn box;\n\t}\n\tPoint operator * (const long double& b)const {\n\t\tPoint box;\n\t\tbox.x = x * b;\n\t\tbox.y = y * b;\n\t\treturn box;\n\t}\n\tbool operator <(const Point& c)const {\n\t\tif (y < c.y)return true;\n\t\tif (y > c.y)return false;\n\t\tif (x < c.x)return true;\n\t\treturn false;\n\t}\n\tbool operator >(const Point& c)const {\n\t\tif (y > c.y)return true;\n\t\tif (y < c.y)return false;\n\t\tif (x > c.x)return true;\n\t\treturn false;\n\t}\n\tbool operator ==(const Point& c)const {\n\t\treturn abs(x - c.x) <= EPS && abs(y - c.y) <= EPS;\n\t}\n\tlong double det(Point a) {\n\t\treturn x * a.y - y * a.x;\n\t}\n};\n\nstruct Line {\n\tlong double a, b, c;\n\tLine(const int aa, const int bb, const int cc) {\n\t\ta = aa, b = bb, c = cc;\n\t}\n\tLine(const Point s, const Point t) {\n\t\tif (abs(s.x - t.x) < EPS) {\n\t\t\ta = 1;\n\t\t\tb = 0;\n\t\t}\n\t\telse {\n\t\t\tb = 1;\n\t\t\ta = (t.y - s.y) / (s.x - t.x);\n\t\t}\n\t\tc = s.x * a + s.y * b;\n\t}\n};\n\nlong double dot(Point a, Point b) {\n\treturn a.x * b.x + a.y * b.y;\n}\n\nlong double cross(Point a, Point b) {\n\treturn a.x * b.y - b.x * a.y;\n}\n\nlong double Distance(Point a, Point b) {\n\treturn sqrtl((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));\n}\n\nlong double Distance(Point a, pair<Point, Point>b) {\n\tLine l = Line(b.first, b.second);\n\tif (abs(l.a * a.x + l.b * a.y - l.c) < EPS) {\n\t\tif (abs(Distance(a, b.first) + Distance(a, b.second) - Distance(b.second, b.first)) <= sqrtl(EPS))return 0;\n\t\treturn min(Distance(a, b.first), Distance(a, b.second));\n\t}\n\tlong double A = powl((b.first.x - b.second.x), 2) + powl((b.first.y - b.second.y), 2);\n\tlong double B = 2 * (a.x - b.first.x) * (b.first.x - b.second.x) + 2 * (a.y - b.first.y) * (b.first.y - b.second.y);\n\tlong double r = B / 2 / A;\n\tr = -r;\n\tif (r >= 0 && r <= 1) {\n\t\treturn Distance(a, Point(b.first.x + r * (b.second.x - b.first.x), b.first.y + r * (b.second.y - b.first.y)));\n\t}\n\telse {\n\t\treturn min(Distance(a, b.first), Distance(a, b.second));\n\t}\n}\n\nbool LineCross(Point a1, Point a2, Point b1, Point b2) {\n\treturn cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS && cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS && !(abs((a1 - a2).x * (a1 - b1).y - (a1 - a2).y * (a1 - b1).x) < EPS && abs((a1 - a2).x * (a1 - b2).y - (a1 - a2).y * (a1 - b2).x) < EPS && abs(Distance(a1, a2) - Distance(a1, b1) - Distance(a2, b1)) > EPS && abs(Distance(a1, a2) - Distance(a2, b2) - Distance(a1, b2)) > EPS && abs(Distance(b1, b2) - Distance(a1, b1) - Distance(a1, b2)) > EPS && abs(Distance(b1, b2) - Distance(a2, b1) - Distance(a2, b2)) > EPS);\n}\n\nlong double Distance(pair<Point, Point>a, pair<Point, Point>b) {\n\tif (LineCross(a.first, a.second, b.first, b.second))return 0;\n\treturn min({ Distance(a.first,b),Distance(a.second,b),Distance(b.first,a),Distance(b.second,a) });\n}\n\nPoint LineCross(Line a, Line b) {\n\tPoint ret;\n\tret.x = (a.c * b.b - a.b * b.c) / (a.a * b.b - a.b * b.a);\n\tret.y = -(a.c * b.a - a.a * b.c) / (a.a * b.b - a.b * b.a);\n\treturn ret;\n}\n\nlong double TriangleArea(Point a, Point b, Point c) {\n\tlong double A, B, C;\n\tA = Distance(a, b);\n\tB = Distance(a, c);\n\tC = Distance(b, c);\n\tlong double S = (A + B + C) / 2;\n\treturn sqrtl(S * (S - A) * (S - B) * (S - C));\n}\n\nvoid func(vector<Point>s, vector<Point>g, vector<int>oks, vector<int>okg, int& ans, int step = 0) {\n\tif (step >= 5)return;\n\t//cout << endl;\n\t//for (auto i : s)cout << i.x << \" \" << i.y << endl;\n\t//for (auto i : g)cout << i.x << \" \" << i.y << endl;\n\t//cout << step << \" \" << oks[0] << \" \" << oks[1] << \" \" << oks[2] << endl;\n\tif (oks[0] && oks[1] && oks[2]) {\n\t\tans = min(ans, step);\n\t\treturn;\n\t}\n\tauto ns = s;\n\tfor (int i = 0; i < 3; i++) {\n\t\tif (oks[i])continue;\n\t\tfor (int j = 0; j < 3; j++) {\n\t\t\tif (okg[j])continue;\n\t\t\tfor (int k = 0; k < 3; k++) {\n\t\t\t\ts = ns;\n\t\t\t\tif (i == k)continue;\n\t\t\t\tLine a(s[k], s[k] + s[i] - s[3 - i - k]);\n\t\t\t\tLine b(s[3 - i - k], g[j] - s[i] + s[3 - i - k]);\n\t\t\t\t//\tcout << a.a << \" \" << a.b << \" \" << b.a << \" \" << b.b << endl;\n\t\t\t\tauto p = LineCross(a, b);\n\t\t\t\tif (isinf(p.x) || isinf(p.y))continue;\n\t\t\t\tif (isnan(p.x) || isnan(p.y))continue;\n\t\t\t\toks[i] = 1;\n\t\t\t\tokg[j] = 1;\n\t\t\t\tint add = 1;\n\t\t\t\tif (Distance(s[k], p) <= EPS) {\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\ts[k] = p;\n\t\t\t\t\tadd++;\n\t\t\t\t}\n\t\t\t\ts[i] = g[j];\n\t\t\t\tif (add == 1 || !oks[k])func(s, g, oks, okg, ans, step + add);\n\t\t\t\toks[i] = 0;\n\t\t\t\tokg[j] = 0;\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid Solve() {\n\tvector<long double>Gx(3);\n\tvector<long double>Gy(3);\n\tfor (int i = 0; i < 3; i++) {\n\t\tcin >> Gx[i] >> Gy[i];\n\t}\n\tint ans = 100;\n\tvector<int>oks(3);\n\tvector<int>okg(3);\n\tvector<Point>S;\n\tvector<Point>G;\n\tfor(int i=0;i<3;i++){\n\t\tS.push_back(Point(Sx[i], Sy[i]));\n\t\tG.push_back(Point(Gx[i], Gy[i]));\n\t}\n\tfunc(S, G, oks, okg, ans);\n\tif (ans >= 5)cout << \"Many\\n\";\n\telse cout << ans << endl;\n}\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\tSx.resize(3);\n\tSy.resize(3);\n\twhile (cin >> Sx[0] >> Sy[0]) {\n\t\tfor (int i = 1; i < 3; i++) {\n\t\t\tcin >> Sx[i] >> Sy[i];\n\t\t}\n\t\tSolve();\n\t}\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3476, "score_of_the_acc": -0.8422, "final_rank": 10 }, { "submission_id": "aoj_1623_6014736", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// arg(x) : argment,[-PI,PI]\nusing CP = complex<long double>;\n#define X real()\n#define Y imag()\nconst long double PI = acos(-1.0L);\nconst long double EPS = 1e-7;\nbool operator==(const CP &l, const CP &r) { return norm(l - r) <= EPS; }\nstruct Circle {\n CP o;\n long double r;\n Circle(long double _x = 0.0L, long double _y = 0.0L, long double _r = 0.0L)\n : o(CP(_x, _y)), r(_r) {}\n Circle(CP _o, long double _r = 0.0) : o(_o), r(_r) {}\n bool operator<(const Circle &cr) const { return r < cr.r; }\n};\n\nstruct Line {\n CP s, t;\n Line(long double sx = 0.0L, long double sy = 0.0L, long double tx = 0.0L,\n long double ty = 0.0L)\n : s(CP(sx, sy)), t(CP(tx, ty)) {}\n Line(CP _s, CP _t) : s(_s), t(_t) {}\n};\n\n// cos a\nlong double costh(const long double &a, const long double &b,\n const long double &c) {\n return (b * b - a * a + c * c) / (2.0L * b * c);\n}\n\n// dot(a,b) = |a||b|cos x\nlong double dot(const CP &a, const CP &b) { return (conj(a) * b).X; }\n// cross(a,b) : area of parallelogram\n// sign : a-> b ,counter clockwise? + : -\nlong double cross(const CP &a, const CP &b) { return (conj(a) * b).Y; }\nlong double corner(const CP &a, const CP &b) {\n //[0,PI]\n return acos(dot(a, b) / (abs(a) * abs(b)));\n}\n\nCP projectionLP(const CP &s, const CP &t, const CP &p) {\n if (s == t) return s;\n CP base = t - s;\n long double r = dot(p - s, base) / norm(base);\n return s + base * r;\n}\nCP projectionLP(const Line &l, const CP &p) {\n return projectionLP(l.s, l.t, p);\n}\n\nCP reflectionLP(const CP &s, const CP &t, const CP &p) {\n CP tmp = (projectionLP(s, t, p) - p);\n tmp *= 2;\n return p + tmp;\n}\nCP reflectionLP(const Line &l, const CP &p) {\n return reflectionLP(l.s, l.t, p);\n}\n\nint calc_clockwiseSP(const CP &s, CP t, CP p) {\n t -= s, p -= s;\n if (cross(t, p) > EPS) return 1; // \"COUNTER_CLOCKWISE\"\n if (cross(t, p) < -EPS) return -1; //\"CLOCK_WISE\"\n if (dot(t, p) < 0) return 2; // \"ONLINE_BACK\"\n if (norm(t) < norm(p)) return -2; // \"ONLINE_FRONT\"\n return 0; // \"ON_SEGMENT\"\n}\nint calc_clockwiseSP(const Line &l, const CP &p) {\n return calc_clockwiseSP(l.s, l.t, p);\n}\n\nint parallel_orthogonalLL(const CP &s, CP t, const CP &a, CP b) {\n t -= s;\n b -= a;\n if (abs(cross(t, b)) <= EPS) return 2; // \"parallel\"\n if (abs(dot(t, b)) <= EPS) return 1; // \"orthogonal\"\n return 0;\n}\nint parallel_orthogonalLL(const Line &ll, const Line &lr) {\n return parallel_orthogonalLL(ll.s, ll.t, lr.s, lr.t);\n}\n\nCP intersectionLL(const CP &a, const CP &b, const CP &c, const CP &d) {\n return a + (b - a) * (cross(d - c, d - a) / cross(d - c, b - a));\n}\nCP intersectionLL(const Line &ll, const Line &lr) {\n return intersectionLL(ll.s, ll.t, lr.s, lr.t);\n}\n\nbool on_segSP(const CP &s, const CP &t, const CP &p) {\n // if not use end point, dot(s - p, t - p) < 0\n return abs(cross(s - p, t - p)) <= EPS && dot(s - p, t - p) <= 0;\n}\nbool on_segSP(const Line &l, const CP &p) { return on_segSP(l.s, l.t, p); }\n\n// crossing segments? (a,b) and (c,d)\nbool iscrossSS(const CP &a, const CP &b, const CP &c, const CP &d) {\n // parallel\n return calc_clockwiseSP(a, b, c) * calc_clockwiseSP(a, b, d) <= 0 &&\n calc_clockwiseSP(c, d, a) * calc_clockwiseSP(c, d, b) <= 0;\n // if (abs(cross(a - b, c - d)) <= EPS) {\n // return on_segSP(a, b, c) || on_segSP(a, b, d) || on_segSP(c, d, a) ||\n // on_segSP(c, d, b);\n // }\n // CP isp = intersectionLL(a, b, c, d);\n // return on_segSP(a, b, isp) && on_segSP(c, d, isp);\n}\n\nbool iscrossSS(const Line &ll, const Line &lr) {\n return iscrossSS(ll.s, ll.t, lr.s, lr.t);\n}\n\nlong double distLP(const CP &s, const CP &t, const CP &p) {\n return abs(cross(t - s, p - s) / abs(t - s));\n}\nlong double distLP(const Line &l, const CP &p) { return distLP(l.s, l.t, p); }\n\nlong double distSP(const CP &s, const CP &t, const CP &p) {\n if (dot(t - s, p - s) < 0) return abs(p - s);\n if (dot(s - t, p - t) < 0) return abs(p - t);\n return distLP(s, t, p);\n}\nlong double distSP(const Line &l, const CP &p) { return distSP(l.s, l.t, p); }\n\nlong double distSS(const CP &a, const CP &b, const CP &c, const CP &d) {\n long double res = 1e18;\n if (iscrossSS(a, b, c, d)) return 0.0L;\n res = min(res, distSP(a, b, c));\n res = min(res, distSP(a, b, d));\n res = min(res, distSP(c, d, a));\n res = min(res, distSP(c, d, b));\n return res;\n}\nlong double distSS(const Line &ll, const Line &lr) {\n return distSS(ll.s, ll.t, lr.s, lr.t);\n}\n\n// counter clockwise\nbool is_convex(const vector<CP> &pol) {\n int n = pol.size();\n for (int i = 0; i < n; ++i)\n if (cross(pol[(i + 1) % n] - pol[i], pol[(i + 2) % n] - pol[(i + 1) % n]) <\n -EPS)\n return 0;\n return 1;\n}\n\nvector<CP> convex_hull(vector<CP> &ps) {\n auto lmd = [&](const CP &l, const CP &r) {\n if (l.X != r.X) return l.X < r.X;\n return l.Y < r.Y;\n };\n vector<CP> res;\n int psize = ps.size();\n sort(ps.begin(), ps.end(), lmd);\n int k = 0;\n res.resize(psize * 2);\n for (int i = 0; i < psize; ++i) {\n while (k > 1 && cross(res[k - 1] - res[k - 2], ps[i] - res[k - 1]) <= 0)\n --k;\n res[k++] = ps[i];\n }\n for (int i = psize - 2, t = k; i >= 0; --i) {\n while (k > t && cross(res[k - 1] - res[k - 2], ps[i] - res[k - 1]) <= 0)\n --k;\n res[k++] = ps[i];\n }\n res.resize(k - 1);\n return res;\n}\n\nlong double convex_diameter(const vector<CP> &pol) {\n vector<CP> ps = pol;\n ps = convex_hull(ps);\n int n = ps.size(), i = 0, j = 0;\n if (n < 2) return 0.0L;\n if (n == 2) return abs(ps[0] - ps[1]);\n for (int k = 0; k < n; ++k) {\n if (ps[k].X < ps[i].X) i = k;\n if (ps[k].X > ps[j].X) j = k;\n }\n long double res = 0;\n int si = i, sj = j;\n while (i != sj || j != si) {\n res = max(res, abs(ps[i] - ps[j]));\n if (cross(ps[(i + 1) % n] - ps[i], ps[(j + 1) % n] - ps[j]) < 0)\n (++i) %= n;\n else\n (++j) %= n;\n }\n return res;\n}\n\nvector<CP> convex_cut(const vector<CP> &pol, const CP &s, const CP &t) {\n vector<CP> res;\n int n = pol.size();\n for (int i = 0; i < n; ++i) {\n CP nows = pol[i], nowt = pol[(i + 1) % n];\n if (cross(t - s, nows - s) >= -EPS) res.push_back(nows);\n if (cross(t - s, nows - s) * cross(t - s, nowt - s) < 0)\n res.push_back(intersectionLL(s, t, nows, nowt));\n }\n return res;\n}\nvector<CP> convex_cut(const vector<CP> &pol, const Line &l) {\n return convex_cut(pol, l.s, l.t);\n}\n\n// number of tangents\nint iscrossCC(Circle l, Circle r) {\n if (l.r < r.r) swap(l, r);\n long double distlr = abs(l.o - r.o);\n if (l.r + r.r < distlr)\n return 4; // not touch\n else if (abs(distlr - l.r - r.r) <= EPS)\n return 3; // circumscription\n else if (l.r - r.r < distlr)\n return 2; // cross\n else if (abs(distlr - l.r + r.r) <= EPS)\n return 1; // inscribed\n else // contain\n return 0;\n}\n\nvector<CP> intersectionCC(const Circle &c1, const Circle &c2) {\n vector<CP> res;\n if (iscrossCC(c1, c2) == 4) return res;\n\n long double d = abs(c1.o - c2.o);\n long double a = acos(costh(c2.r, c1.r, d));\n long double t = atan2(c2.o.imag() - c1.o.imag(), c2.o.real() - c1.o.real());\n res.push_back(c1.o + CP(cos(t + a) * c1.r, sin(t + a) * c1.r));\n res.push_back(c1.o + CP(cos(t - a) * c1.r, sin(t - a) * c1.r));\n if (res[0].X > res[1].X || (res[0].X == res[1].X && res[0].Y > res[1].Y))\n swap(res[0], res[1]);\n return res;\n}\n\nvector<CP> intersectionCL(const Circle &ci, const CP &s, CP t) {\n vector<CP> res(2, projectionLP(s, t, ci.o));\n long double r = sqrt(ci.r * ci.r - norm(res[0] - ci.o));\n if (r <= EPS || t == s) return res;\n t -= s;\n t *= r / abs(t);\n res[0] += t;\n res[1] -= t;\n if (res[0].X > res[1].X || (res[0].X == res[1].X && res[0].Y > res[1].Y))\n swap(res[0], res[1]);\n return res;\n}\nvector<CP> intersectionCL(const Circle &ci, const Line &l) {\n return intersectionCL(ci, l.s, l.t);\n}\n\nvector<CP> contactCP(const Circle &ci, const CP &p) {\n vector<CP> res;\n long double d = abs(ci.o - p);\n if (abs(d - ci.r) <= EPS) {\n res.push_back(p);\n return res;\n } else if (d < ci.r)\n return res;\n long double arg = asin(ci.r / d);\n res.push_back((ci.o - p) * CP(cos(arg), sin(arg)));\n res[0] *= (d * cos(arg)) / abs(res[0]);\n res[0] += p;\n res.push_back(reflectionLP(p, ci.o, res[0]));\n if (res[0].X > res[1].X || (res[0].X == res[1].X && res[0].Y > res[1].Y))\n swap(res[0], res[1]);\n return res;\n}\n\nvector<Line> tangentCC(Circle cl, Circle cr) {\n vector<Line> res;\n if (cl.r < cr.r) swap(cl, cr);\n long double g = abs(cl.o - cr.o);\n if (abs(g - 0.0L) <= EPS) return res;\n CP hor = (cr.o - cl.o) / g, ver;\n ver = hor * (CP(cos(PI * 0.5L), sin(PI * 0.5L)));\n for (int s : {-1, 1}) {\n long double h = (cl.r + s * cr.r) / g;\n if (abs(1 - h * h) <= EPS) {\n res.emplace_back(cl.o + hor * cl.r, cl.o + (hor + ver) * cl.r);\n } else if (1 - h * h > 0) {\n CP nhor = hor * h, nver = ver * sqrtl(1 - h * h);\n res.emplace_back(cl.o + (nhor + nver) * cl.r,\n cr.o - (nhor + nver) * (cr.r * s));\n res.emplace_back(cl.o + (nhor - nver) * cl.r,\n cr.o - (nhor - nver) * (cr.r * s));\n }\n }\n return res;\n}\n\nlong double areaPol(const vector<CP> &pol) {\n int n = pol.size();\n long double res = 0;\n for (int i = 0; i < n; ++i)\n res += (pol[(i - 1 + n) % n].X - pol[(i + 1) % n].X) * pol[i].Y;\n return res / 2.0L;\n}\n\nint containPolP(const vector<CP> &pol, CP p) {\n bool con = 0, onseg = 0;\n int n = pol.size();\n for (int i = 0; i < n; ++i) {\n onseg |= on_segSP(pol[i], pol[(i + 1) % n], p);\n CP s = pol[i] - p, t = pol[(i + 1) % n] - p;\n if (s.Y > t.Y) swap(s, t);\n if (s.Y * t.Y <= 0 && t.Y > 0 && cross(s, t) < 0) con = !con;\n }\n if (onseg) return 1;\n if (con) return 2;\n return 0;\n}\n\nlong double closest_pair(vector<CP> &ps, int l = -1, int r = -1,\n bool reqsqrt = 0) {\n if (l == r && l == -1) {\n l = 0;\n r = ps.size();\n reqsqrt = 1;\n auto lmd = [&](const CP &l, const CP &r) {\n if (l.X != r.X) return l.X < r.X;\n return l.Y < r.Y;\n };\n sort(ps.begin(), ps.end(), lmd);\n }\n if (r - l < 2) return 1e18;\n if (r - l == 2) {\n if (ps[l].Y > ps[l + 1].Y) swap(ps[l], ps[l + 1]);\n if (reqsqrt) return abs(ps[l] - ps[l + 1]);\n return norm(ps[l] - ps[l + 1]);\n }\n int mid = (l + r) / 2;\n long double x = ps[mid].X,\n res = min(closest_pair(ps, l, mid), closest_pair(ps, mid, r));\n auto f = [](CP pl, CP pr) { return pl.Y < pr.Y; };\n inplace_merge(ps.begin() + l, ps.begin() + mid, ps.begin() + r, f);\n vector<CP> tmp;\n for (int i = l; i < r; ++i) {\n long double dx = abs(ps[i].X - x);\n int tsize = tmp.size();\n if (dx * dx >= res) continue;\n for (int j = 0; j < tsize; ++j) {\n CP delta = ps[i] - tmp[tsize - 1 - j];\n if (delta.Y * delta.Y >= res) break;\n res = min(res, norm(delta));\n }\n tmp.push_back(ps[i]);\n }\n if (reqsqrt) res = sqrtl(res);\n return res;\n}\n\nCircle min_ball(vector<CP> ps) {\n int n = ps.size();\n if (n == 1) return Circle(ps[0], 0.0L);\n mt19937 mt(int(time(0)));\n shuffle(ps.begin(), ps.end(), mt);\n auto make3 = [](const CP &a, const CP &b, const CP &c) {\n long double A = norm(b - c), B = norm(c - a), C = norm(a - b),\n S = cross(b - a, c - a);\n CP p = (A * (B + C - A) * a + B * (C + A - B) * b + C * (A + B - C) * c) /\n (4 * S * S);\n long double nowr = norm(p - a);\n return Circle(p, nowr);\n };\n auto make2 = [](const CP &a, const CP &b) {\n CP c = (a + b) / 2.0L;\n long double nowr = norm(a - c);\n return Circle(c, nowr);\n };\n auto in_circle = [](const CP &a, const Circle &c) {\n return norm(a - c.o) <= c.r + EPS;\n };\n Circle res = make2(ps[0], ps[1]);\n for (int i = 2; i < n; ++i)\n if (!in_circle(ps[i], res)) {\n res = make2(ps[0], ps[i]);\n for (int j = 1; j < i; ++j)\n if (!in_circle(ps[j], res)) {\n res = make2(ps[i], ps[j]);\n for (int k = 0; k < j; ++k)\n if (!in_circle(ps[k], res)) res = make3(ps[i], ps[j], ps[k]);\n }\n }\n res.r = sqrtl(res.r);\n return res;\n}\n\nbool arg_comp(CP a, CP b) {\n int up_down_a = a.Y > 0 || (abs(a.Y) <= EPS && a.X >= 0);\n int up_down_b = b.Y > 0 || (abs(b.Y) <= EPS && b.X >= 0);\n if (up_down_a != up_down_b) return up_down_a < up_down_b;\n if (a.X * b.Y == a.Y * b.X) return norm(a) < norm(b);\n return calc_clockwiseSP(CP(0, 0), a, b) == 1;\n}\nbool operator<(const Line &l, const Line &r) {\n CP lp = l.t - l.s, rp = r.t - r.s;\n return arg_comp(lp, rp);\n}\n\nvector<CP> s, t;\nvector<int> ps(3), pt(3), pf = {1, 2};\n\nint solve();\nint calc();\n\nint main() {\n int x, y;\n while (cin >> x) {\n s.clear(), t.clear();\n pf = {1, 2};\n cin >> y;\n s.emplace_back(x, y);\n for (int i = 0; i < 2; ++i) {\n cin >> x >> y;\n s.emplace_back(x, y);\n }\n for (int i = 0; i < 3; ++i) {\n cin >> x >> y;\n t.emplace_back(x, y);\n }\n int res = solve();\n if (res >= 5)\n cout << \"Many\" << endl;\n else\n cout << res << endl;\n }\n return 0;\n}\n\nint solve() {\n int res = 5;\n vector<CP> vs = s, vt = t;\n do {\n iota(ps.begin(), ps.end(), 0);\n do {\n iota(pt.begin(), pt.end(), 0);\n do {\n for (int i = 0; i < 3; ++i) s[i] = vs[ps[i]], t[i] = vt[pt[i]];\n res = min(res, calc());\n } while (next_permutation(pt.begin(), pt.end()));\n } while (next_permutation(ps.begin(), ps.end()));\n } while (next_permutation(pf.begin(), pf.end()));\n return res;\n}\n\nint calc() {\n int res = 0;\n if (abs(t[0] - s[0]) > EPS) {\n CP veca = t[0] - s[0], tar = s[pf[0]], vecb = s[0] - tar;\n veca /= abs(veca), vecb /= abs(vecb);\n CP x = tar + veca, y = s[pf[1]], z = y + vecb;\n if (abs(calc_clockwiseSP(s[0], t[0], tar)) != 1) return 5;\n assert(parallel_orthogonalLL(s[0], tar, z, y) == 2);\n CP is = intersectionLL(tar, x, y, z);\n if (abs(is - y) > EPS) s[pf[1]] = is, ++res;\n ++res, s[0] = t[0];\n }\n if (abs(t[1] - s[1]) > EPS) {\n CP veca = t[1] - s[1], vecb = s[0] - s[1];\n veca /= abs(veca), vecb /= abs(vecb);\n CP x = s[0] + veca, y = s[2] + vecb;\n CP is = intersectionLL(s[0], x, s[2], y);\n if (abs(is - s[2]) > EPS) s[2] = is, ++res;\n ++res, s[1] = t[1];\n }\n if (abs(t[2] - s[2]) > EPS) {\n if (parallel_orthogonalLL(s[2], t[2], s[0], s[1]) != 2) return 5;\n ++res;\n }\n return res;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3504, "score_of_the_acc": -0.9116, "final_rank": 14 }, { "submission_id": "aoj_1623_6006303", "code_snippet": "#pragma GCC ta g(\"avx2\")\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define DEBUG\n#ifdef DEBUG\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << '(' << p.first << ',' << p.second << ')';\n return os;\n}\ntemplate <class T> ostream &operator<<(ostream &os, const vector<T> &v) {\n os << '{';\n for(int i = 0; i < (int)v.size(); i++) {\n if(i) { os << ','; }\n os << v[i];\n }\n os << '}';\n return os;\n}\nvoid debugg() { cerr << endl; }\ntemplate <class T, class... Args>\nvoid debugg(const T &x, const Args &... args) {\n cerr << \" \" << x;\n debugg(args...);\n}\n#define debug(...) \\\n cerr << __LINE__ << \" [\" << #__VA_ARGS__ << \"]: \", debugg(__VA_ARGS__)\n#define dump(x) cerr << __LINE__ << \" \" << #x << \" = \" << (x) << endl\n#else\n#define debug(...) (void(0))\n#define dump(x) (void(0))\n#endif\nusing namespace std;\ntypedef long long ll;\ntypedef vector<ll> vl;\ntypedef vector<vl> vvl;\ntypedef vector<char> vc;\ntypedef vector<string> vs;\ntypedef vector<bool> vb;\ntypedef vector<double> vd;\ntypedef pair<ll,ll> P;\ntypedef pair<int,int> pii;\ntypedef vector<P> vpl;\ntypedef tuple<ll,ll,ll> tapu;\n#define rep(i,n) for(int i=0; i<(n); i++)\n#define REP(i,a,b) for(int i=(a); i<(b); i++)\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\ntemplate<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){\n\tif(a>b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){\n\tif(a<b){\n\t\ta = b; return true;\n\t}\n\telse return false;\n}\ntemplate<typename T> inline void print(T &a){\n int sz = a.size();\n for(auto itr = a.begin(); itr != a.end(); itr++){\n\t\tcout << *itr;\n sz--;\n if(sz) cout << \" \";\n\t}\n cout << \"\\n\";\n}\ntemplate<typename T1,typename T2> inline void print2(T1 a, T2 b){\n\tcout << a << \" \" << b << \"\\n\";\n}\ntemplate<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){\n\tcout << a << \" \" << b << \" \" << c << \"\\n\";\n}\nvoid mark() {cout << \"#\" << \"\\n\";}\nll pcount(ll x) {return __builtin_popcountll(x);}\n//#include <atcoder/maxflow>\n//using namespace atcoder;\nconst int inf = (1<<30)-1;\nconst ll linf = 1LL<<61;\nconst int MAX = 510000;\nint dy[8] = {0,1,0,-1,1,-1,-1,1};\nint dx[8] = {-1,0,1,0,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double eps = 1e-9;\nconst int mod = 1e9 + 7;\n//const int mod = 998244353;\n\nnamespace geometry {\n // Point : 複素数型を位置ベクトルとして扱う\n // 実軸(real)をx軸、挙軸(imag)をy軸として見る\n using D = long double;\n using Point = complex<D>;\n const D EPS = 1e-7;\n const D PI = acosl(-1);\n\n inline bool equal(const D &a, const D &b) { return fabs(a - b) < EPS; }\n\n // 単位ベクトル(unit vector)を求める\n Point unitVector(const Point &a) { return a / abs(a); }\n\n // 法線ベクトル(normal vector)を求める\n // 90度回転した単位ベクトルをかける\n // -90度がよければPoint(0, -1)をかける\n Point normalVector(const Point &a) { return a * Point(0, 1); }\n\n // 内積(dot product) : a・b = |a||b|cosΘ\n D dot(const Point &a, const Point &b) {\n return (a.real() * b.real() + a.imag() * b.imag());\n }\n\n // 外積(cross product) : a×b = |a||b|sinΘ\n D cross(const Point &a, const Point &b) {\n return (a.real() * b.imag() - a.imag() * b.real());\n }\n\n // 点pを反時計回りにtheta度回転\n // thetaはラジアン!!!\n Point rotate(const Point &p, const D &theta) {\n return Point(cos(theta) * p.real() - sin(theta) * p.imag(),\n sin(theta) * p.real() + cos(theta) * p.imag());\n }\n\n // ラジアン->度\n D radianToDegree(const D &radian) { return radian * 180.0 / PI; }\n\n // 度->ラジアン\n D degreeToRadian(const D &degree) { return degree * PI / 180.0; }\n\n // 点の回転方向\n // 点a, b, cの位置関係について(aが基準点)\n int ccw(const Point &a, Point b, Point c) {\n b -= a, c -= a;\n // 点a, b, c が\n // 反時計回りの時、\n if(cross(b, c) > EPS) return 1;\n // 時計回りの時、\n if(cross(b, c) < -EPS) return -1;\n // c, a, bがこの順番で同一直線上にある時、\n if(dot(b, c) < 0) return 2;\n // a, b, cがこの順番で同一直線上にある場合、\n if(norm(b) < norm(c)) return -2;\n // cが線分ab上にある場合、\n return 0;\n }\n\n // Line : 直線を表す構造体\n // b - a で直線・線分を表せる\n struct Line {\n Point a, b;\n Line() = default;\n Line(Point a, Point b) : a(a), b(b) {}\n // Ax+By=C\n Line(D A, D B, D C) {\n if(equal(A, 0)) {\n a = Point(0, C / B), b = Point(1, C / B);\n } else if(equal(B, 0)) {\n b = Point(C / A, 0), b = Point(C / A, 1);\n } else {\n a = Point(0, C / B), b = Point(C / A, 0);\n }\n }\n };\n\n // Segment : 線分を表す構造体\n // Lineと同じ\n struct Segment : Line {\n Segment() = default;\n\n Segment(Point a, Point b) : Line(a, b) {}\n };\n\n // Circle : 円を表す構造体\n // pが中心の位置ベクトル、rは半径\n struct Circle {\n Point p;\n D r;\n\n Circle() = default;\n\n Circle(Point p, D r) : p(p), r(r) {}\n };\n\n // 2直線の直交判定 : a⊥b <=> dot(a, b) = 0\n bool isOrthogonal(const Line &a, const Line &b) {\n return equal(dot(a.b - a.a, b.b - b.a), 0);\n }\n // 2直線の平行判定 : a//b <=> cross(a, b) = 0\n bool isParallel(const Line &a, const Line &b) {\n return equal(cross(a.b - a.a, b.b - b.a), 0);\n }\n\n // 点cが直線ab上にあるか\n bool isPointOnLine(const Point &a, const Point &b, const Point &c) {\n return isParallel(Line(a, b), Line(a, c));\n }\n\n // 点cが\"線分\"ab上にあるか\n bool isPointOnSegment(const Point &a, const Point &b, const Point &c) {\n // |a-c| + |c-b| <= |a-b| なら線分上\n return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);\n }\n\n // 直線lと点pの距離を求める\n D distanceBetweenLineAndPoint(const Line &l, const Point &p) {\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n\n // 線分lと点pの距離を求める\n // 定義:点pから「線分lのどこか」への最短距離\n D distanceBetweenSegmentAndPoint(const Segment &l, const Point &p) {\n if(dot(l.b - l.a, p - l.a) < EPS) return abs(p - l.a);\n if(dot(l.a - l.b, p - l.b) < EPS) return abs(p - l.b);\n return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);\n }\n\n // 直線s, tの交点の計算\n Point crossPoint(const Line &s, const Line &t) {\n D d1 = cross(s.b - s.a, t.b - t.a);\n D d2 = cross(s.b - s.a, s.b - t.a);\n if(equal(abs(d1), 0) && equal(abs(d2), 0)) return t.a;\n return t.a + (t.b - t.a) * (d2 / d1);\n }\n\n // 線分s, tの交点の計算\n Point crossPoint(const Segment &s, const Segment &t) {\n return crossPoint(Line(s), Line(t));\n }\n\n // 線分sと線分tが交差しているかどうか\n // bound:線分の端点を含むか\n bool isIntersect(const Segment &s, const Segment &t, bool bound) {\n return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) < bound &&\n ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) < bound;\n }\n\n // 線分sとtの距離\n D distanceBetweenSegments(const Segment &s, const Segment &t) {\n if(isIntersect(s, t, 1)) return (D)(0);\n D ans = distanceBetweenSegmentAndPoint(s, t.a);\n chmin(ans, distanceBetweenSegmentAndPoint(s, t.b));\n chmin(ans, distanceBetweenSegmentAndPoint(t, s.a));\n chmin(ans, distanceBetweenSegmentAndPoint(t, s.b));\n return ans;\n }\n\n // 射影(projection)\n // 直線(線分)lに点pから引いた垂線の足を求める\n Point projection(const Line &l, const Point &p) {\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n Point projection(const Segment &l, const Point &p) {\n D t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);\n return l.a + (l.a - l.b) * t;\n }\n\n // 反射(reflection)\n // 直線lを対称軸として点pと線対称の位置にある点を求める\n Point reflection(const Line &l, const Point &p) {\n return p + (projection(l, p) - p) * (D)2.0;\n }\n\n // 2つの円の交差判定\n // 返り値は共通接線の数\n int isIntersect(const Circle &c1, const Circle &c2) {\n D d = abs(c1.p - c2.p);\n // 2つの円が離れている場合\n if(d > c1.r + c2.r + EPS) return 4;\n // 外接している場合\n if(equal(d, c1.r + c2.r)) return 3;\n // 内接している場合\n if(equal(d, abs(c1.r - c2.r))) return 1;\n // 内包している場合\n if(d < abs(c1.r - c2.r) - EPS) return 0;\n return 2;\n }\n\n // 2つの円の交点\n vector<Point> crossPoint(const Circle &c1, const Circle &c2) {\n vector<Point> res;\n int mode = isIntersect(c1, c2);\n // 2つの中心の距離\n D d = abs(c1.p - c2.p);\n // 2円が離れている場合\n if(mode == 4) return res;\n // 1つの円がもう1つの円に内包されている場合\n if(mode == 0) return res;\n // 2円が外接する場合\n if(mode == 3) {\n D t = c1.r / (c1.r + c2.r);\n res.emplace_back(c1.p + (c2.p - c1.p) * t);\n return res;\n }\n // 内接している場合\n if(mode == 1) {\n if(c2.r < c1.r - EPS) {\n res.emplace_back(c1.p + (c2.p - c1.p) * (c1.r / d));\n } else {\n res.emplace_back(c2.p + (c1.p - c2.p) * (c2.r / d));\n }\n return res;\n }\n // 2円が重なる場合\n D rc1 = (c1.r * c1.r + d * d - c2.r * c2.r) / (2 * d);\n D rs1 = sqrt(c1.r * c1.r - rc1 * rc1);\n if(c1.r - abs(rc1) < EPS) rs1 = 0;\n Point e12 = (c2.p - c1.p) / abs(c2.p - c1.p);\n res.emplace_back(c1.p + rc1 * e12 + rs1 * e12 * Point(0, 1));\n res.emplace_back(c1.p + rc1 * e12 + rs1 * e12 * Point(0, -1));\n return res;\n }\n\n // 点pが円cの内部(円周上も含む)に入っているかどうか\n bool isInCircle(const Circle &c, const Point &p) {\n D d = abs(c.p - p);\n return (equal(d, c.r) || d < c.r - EPS);\n }\n\n // 円cと直線lの交点\n vector<Point> crossPoint(const Circle &c, const Line &l) {\n vector<Point> res;\n D d = distanceBetweenLineAndPoint(l, c.p);\n // 交点を持たない\n if(d > c.r + EPS) return res;\n // 接する\n Point h = projection(l, c.p);\n if(equal(d, c.r)) {\n res.emplace_back(h);\n return res;\n }\n Point e = unitVector(l.b - l.a);\n D ph = sqrt(c.r * c.r - d * d);\n res.emplace_back(h - e * ph);\n res.emplace_back(h + e * ph);\n return res;\n }\n\n // 点pを通る円cの接線\n // 2本あるので、接点のみを返す\n vector<Point> tangentToCircle(const Point &p, const Circle &c) {\n return crossPoint(c, Circle(p, sqrt(norm(c.p - p) - c.r * c.r)));\n }\n\n // 円の共通接線\n vector<Line> tangent(const Circle &a, const Circle &b) {\n vector<Line> ret;\n // 2円の中心間の距離\n D g = abs(a.p - b.p);\n // 円が内包されている場合\n if(equal(g, 0)) return ret;\n Point u = unitVector(b.p - a.p);\n Point v = rotate(u, PI / 2);\n for(int s : {-1, 1}) {\n D h = (a.r + b.r * s) / g;\n if(equal(h * h, 1)) {\n ret.emplace_back(a.p + (h > 0 ? u : -u) * a.r,\n a.p + (h > 0 ? u : -u) * a.r + v);\n\n } else if(1 - h * h > 0) {\n Point U = u * h, V = v * sqrt(1 - h * h);\n ret.emplace_back(a.p + (U + V) * a.r,\n b.p - (U + V) * (b.r * s));\n ret.emplace_back(a.p + (U - V) * a.r,\n b.p - (U - V) * (b.r * s));\n }\n }\n return ret;\n }\n\n // 多角形の面積を求める\n D PolygonArea(const vector<Point> &p) {\n D res = 0;\n int n = p.size();\n for(int i = 0; i < n - 1; i++) res += cross(p[i], p[i + 1]);\n res += cross(p[n - 1], p[0]);\n return res * 0.5;\n }\n\n // 凸多角形かどうか\n bool isConvex(const vector<Point> &p) {\n int n = p.size();\n int now, pre, nxt;\n for(int i = 0; i < n; i++) {\n pre = (i - 1 + n) % n;\n nxt = (i + 1) % n;\n now = i;\n if(ccw(p[pre], p[now], p[nxt]) == -1) return false;\n }\n return true;\n }\n\n // 凸包 O(NlogN)\n vector<Point> ConvexHull(vector<Point> &p) {\n int n = (int)p.size(), k = 0;\n sort(all(p), [](const Point &a, const Point &b) {\n return (real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b));\n });\n vector<Point> ch(2 * n);\n // 一直線上の3点を含める -> (< -EPS)\n // 含め無い -> (< EPS)\n for(int i = 0; i < n; ch[k++] = p[i++]) { // lower\n while(k >= 2 &&\n cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS)\n --k;\n }\n for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { // upper\n while(k >= t &&\n cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS)\n --k;\n }\n ch.resize(k - 1);\n return ch;\n }\n\n // 多角形gに点pが含まれているか?\n // 含まれる:2, 辺上にある:1, 含まれない:0\n int isContained(const vector<Point> &g, const Point &p) {\n bool in = false;\n int n = (int)g.size();\n for(int i = 0; i < n; i++) {\n Point a = g[i] - p, b = g[(i + 1) % n] - p;\n if(imag(a) > imag(b)) swap(a, b);\n if(imag(a) <= EPS && EPS < imag(b) && cross(a, b) < -EPS) in = !in;\n if(cross(a, b) == 0 && dot(a, b) <= 0) return 1;\n }\n return (in ? 2 : 0);\n }\n\n vector<Point> convex_cut(const vector<Point> &g, Line l){\n vector<Point> ret;\n for(int i=0; i<g.size(); i++){\n Point now = g[i], nxt = g[(i+1)%g.size()];\n if(ccw(l.a,l.b,now) != -1) ret.push_back(now);\n if(ccw(l.a,l.b,now) * ccw(l.a,l.b,nxt) < 0){\n ret.push_back(crossPoint(Line(now,nxt),l));\n }\n }\n return ret;\n }\n\n bool equal (const Point &a, const Point &b) {\n return equal(a.real(),b.real()) && equal(a.imag(),b.imag());\n }\n\n D circlesIntersectArea(Circle &a, Circle &b){\n double d = abs(a.p - b.p);\n if(d >= a.r + b.r - EPS) return 0; //2円が離れている\n auto R = minmax(a.r,b.r);\n if(d <= R.second - R.first + EPS) return R.first*R.first*pi; //円が円を内包する\n D theta1 = acos((d*d + a.r*a.r - b.r*b.r)/(2*a.r*d));\n D theta2 = acos((d*d + b.r*b.r - a.r*a.r)/(2*b.r*d));\n D ret = a.r*a.r*(theta1-cos(theta1)*sin(theta1)) + b.r*b.r*(theta2-cos(theta2)*sin(theta2));\n return ret;\n }\n} // namespace geometry\n\nusing namespace geometry;\n\n\nint main(){\n int x,y;\n while(cin >> x){\n cin >> y;\n vector<Point> a(3),b(3);\n a[0] = Point(x,y);\n rep(i,2){\n int x,y; cin >> x >> y;\n a[i+1] = Point(x,y);\n }\n rep(i,3){\n int x,y; cin >> x >> y;\n b[i] = Point(x,y);\n }\n vl nxt(3); rep(i,3) nxt[i] = i;\n vl nxt2(3); rep(i,3) nxt2[i] = i;\n\n //3回\n bool f = false;\n bool h = false;\n do{\n do{\n auto c = a;\n bool g = true;\n rep(i,3){\n int id = nxt[i];\n int u = (id+1) % 3;\n int v = (id+2) % 3;\n if(isParallel(Line(c[u],c[v]), Line(c[id],b[nxt2[i]]))){\n c[id] = b[nxt2[i]];\n if(i == 0) h = true;\n }else{\n g = false; break;\n }\n }\n if(g) f = true;\n }while(next_permutation(all(nxt2)));\n }while(next_permutation(all(nxt)));\n if(f){\n cout << \"3\\n\";\n continue;\n }\n\n //4回\n if(h){\n cout << \"4\\n\";\n continue;\n }\n rep(j,3){ //最初に動かすもの\n do{\n if(j == nxt[0]) continue;\n do{\n auto c = a;\n Point vec = Point(c[(j+1)%3]-c[(j+2)%3]);\n Line l1(c[j],c[j]+vec);\n Point vec2(c[nxt[0]]-b[nxt2[0]]);\n Line l2(c[3-j-nxt[0]]+vec2,c[3-j-nxt[0]]);\n if(isParallel(l1,l2)) continue;\n c[j] = crossPoint(l1,l2);\n\n //3回のときと同じ\n bool g = true;\n rep(i,3){\n int id = nxt[i];\n int u = (id+1) % 3;\n int v = (id+2) % 3;\n if(isParallel(Line(c[u],c[v]), Line(c[id],b[nxt2[i]]))){\n c[id] = b[nxt2[i]];\n }else{\n if(i == 0) assert(false);\n g = false; break;\n }\n }\n if(g) f = true;\n }while(next_permutation(all(nxt2)));\n }while(next_permutation(all(nxt)));\n }\n if(f){\n cout << \"4\\n\";\n }else{\n cout << \"Many\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3468, "score_of_the_acc": -0.819, "final_rank": 9 }, { "submission_id": "aoj_1623_5347461", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst double EPS = 0.00001;\nstruct point{\n double x, y;\n point(){\n }\n point(double x, double y): x(x), y(y){\n }\n point operator +(point P){\n return point(x + P.x, y + P.y);\n }\n point operator -(point P){\n return point(x - P.x, y - P.y);\n }\n point operator *(double k){\n return point(x * k, y * k);\n }\n point operator /(double k){\n return point(x / k, y / k);\n }\n};\ndouble abs(point P){\n return sqrt(P.x * P.x + P.y * P.y);\n}\ndouble cross(point P, point Q){\n return P.x * Q.y - P.y * Q.x;\n}\nstruct line{\n point A, B;\n line(point A, point B): A(A), B(B){\n }\n};\npoint vec(line L){\n return L.B - L.A;\n}\nbool is_parallel(line L1, line L2){\n return abs(cross(vec(L1), vec(L2))) <= EPS;\n}\nline parallel(point P, line L){\n return line(P, P + vec(L));\n}\npoint line_intersection(line L1, line L2){\n return L1.A + vec(L1) * cross(L2.A - L1.A, vec(L2)) / cross(vec(L1), vec(L2));\n}\nbool ok(vector<point> A, vector<point> B){\n vector<int> p = {0, 1, 2}, q = {0, 1, 2};\n while (true){\n while (true){\n if (is_parallel(line(A[p[0]], B[q[0]]), line(A[p[1]], A[p[2]]))){\n if (is_parallel(line(A[p[1]], B[q[1]]), line(B[q[0]], A[p[2]]))){\n if (is_parallel(line(A[p[2]], B[q[2]]), line(B[q[0]], B[q[1]]))){\n return true;\n }\n }\n }\n if (!next_permutation(q.begin(), q.end())){\n break;\n }\n }\n if (!next_permutation(p.begin(), p.end())){\n break;\n }\n }\n return false;\n}\nint main(){\n while (true){\n vector<point> A(3);\n cin >> A[0].x;\n if (!cin){\n break;\n }\n cin >> A[0].y;\n for (int i = 1; i < 3; i++){\n cin >> A[i].x >> A[i].y;\n }\n vector<point> B(3);\n for (int i = 0; i < 3; i++){\n cin >> B[i].x >> B[i].y;\n }\n vector<point> C = {A[0], A[1], A[2], B[0], B[1], B[2]};\n if (ok(A, B)){\n cout << 3 << endl;\n } else {\n bool P = false;\n for (int i = 0; i < 2; i++){\n for (int j = 0; j < 3; j++){\n line L1 = parallel(A[j], line(A[(j + 1) % 3], A[(j + 2) % 3]));\n for (int k = 0; k < 6; k++){\n for (int l = 0; l < 6; l++){\n for (int m = l + 1; m < 6; m++){\n line L2 = parallel(C[k], line(C[l], C[m]));\n if (!is_parallel(L1, L2)){\n vector<point> A2 = A;\n A2[j] = line_intersection(L1, L2);\n if (ok(A2, B) || ok(B, A2)){\n P = true;\n }\n }\n }\n }\n }\n }\n swap(A, B);\n }\n if (P){\n cout << 4 << endl;\n } else {\n cout << \"Many\" << endl;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 450, "memory_kb": 3124, "score_of_the_acc": -0.0598, "final_rank": 1 } ]
aoj_1625_cpp
Origami, or the art of folding paper Master Grus is a famous origami (paper folding) artist, who is enthusiastic about exploring the possibility of origami art. For future creation, he is now planning fundamental experiments to establish the general theory of origami. One rectangular piece of paper is used in each of his experiments. He folds it horizontally and/or vertically several times and then punches through holes in the folded paper. The following figure illustrates the folding process of a simple experiment, which corresponds to the third dataset of the Sample Input below. Folding the 10 × 8 rectangular piece of paper shown at top left three times results in the 6 × 6 square shape shown at bottom left. In the figure, dashed lines indicate the locations to fold the paper and round arrows indicate the directions of folding. Grid lines are shown as solid lines to tell the sizes of rectangular shapes and the exact locations of folding. Color densities represent the numbers of overlapping layers. Punching through holes at A and B in the folded paper makes nine holes in the paper, eight at A and another at B. Your mission in this problem is to write a computer program to count the number of holes in the paper, given the information on a rectangular piece of paper and folding and punching instructions. Input The input consists of at most 1000 datasets, each in the following format. n m t p d 1 c 1 ... d t c t x 1 y 1 ... x p y p n and m are the width and the height, respectively, of a rectangular piece of paper. They are positive integers at most 32. t and p are the numbers of folding and punching instructions, respectively. They are positive integers at most 20. The pair of d i and c i gives the i -th folding instruction as follows: d i is either 1 or 2. c i is a positive integer. If d i is 1, the left-hand side of the vertical folding line that passes at c i right to the left boundary is folded onto the right-hand side. If d i is 2, the lower side of the horizontal folding line that passes at c i above the lower boundary is folded onto the upper side. After performing the first i −1 folding instructions, if d i is 1, the width of the shape is greater than c i . Otherwise the height is greater than c i . ( x i + 1/2, y i + 1/2) gives the coordinates of the point where the i -th punching instruction is performed. The origin (0, 0) is at the bottom left of the finally obtained shape. x i and y i are both non-negative integers and they are less than the width and the height, respectively, of the shape. You can assume that no two punching instructions punch holes at the same location. The end of the input is indicated by a line containing four zeros. Output For each dataset, output p lines, the i -th of which contains the number of holes punched in the paper by the i -th punching instruction. Sample Input 2 1 1 1 1 1 0 0 1 3 2 1 2 1 2 1 0 0 10 8 3 2 2 2 1 3 1 1 0 1 3 4 3 3 3 2 1 2 2 1 1 1 0 1 0 0 0 0 0 0 Output for the Sample Input 2 3 8 1 3 6
[ { "submission_id": "aoj_1625_10854025", "code_snippet": "#include <iostream>\n#include <cstring>\n#define ll long long\n#define N 100000\n\nusing namespace std;\n\nint g[600][600], dx, dy;\n\nint main(void) {\n#ifdef _DEBUG\n\tfreopen(\"1.in\", \"r\", stdin);\n#else \n\tios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#endif\n\tint n, m, t, p;\n\twhile (cin >> n >> m >> t >> p, n) {\n\t\tdx = dy = 0;\n\t\tmemset(g, 0, sizeof(g));\n\t\tfor (int i = 0; i != n; ++i) for (int j = 0; j != m; ++j) g[i][j] = 1;\n\t\twhile (t--) { \n\t\t\tint d, c; cin >> d >> c;\n\t\t\tif (d == 1) {\n\t\t\t\tfor (int i = 0; i != c; ++i)\n\t\t\t\t\tfor (int j = 0; j != m - dy; ++j)\n\t\t\t\t\t\tg[dx + c + i][dy + j] += g[dx + c - i - 1][dy + j];\n\t\t\t\tdx += c;\n\t\t\t\tif (n < dx + 2 * c) n = dx + 2 * c;\n\t\t\t}\n\t\t\telse if (d == 2) {\n\t\t\t\tfor (int j = 0; j != c; ++j)\n\t\t\t\t\tfor (int i = 0; i != n - dx; ++i)\n\t\t\t\t\t\tg[dx + i][dy + c + j] += g[dx + i][dy + c - j - 1];\n\t\t\t\tdy += c;\n\t\t\t\tif (m < dy + 2 * c) m = dy + 2 * c;\n\t\t\t}\n\t\t}\n\t\twhile (p--) {\n\t\t\tint x, y; cin >> x >> y;\n\t\t\tcout << g[dx + x][dy + y] << endl;\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 4784, "score_of_the_acc": -0.022, "final_rank": 5 }, { "submission_id": "aoj_1625_10625671", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,b) for(int i=a;i<b;i++)\n#define rrep(i,a,b) for(int i=a;i>=b;i--)\n#define fore(i,a) for(auto &i:a)\n#define all(x) (x).begin(),(x).end()\nusing namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }\ntypedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }\n//---------------------------------------------------------------------------------------------------\n/*---------------------------------------------------------------------------------------------------\n            ∧_∧ \n      ∧_∧  (´<_` )  Welcome to My Coding Space!\n     ( ´_ゝ`) /  ⌒i \n    /   \    | | \n    /   / ̄ ̄ ̄ ̄/  | \n  __(__ニつ/  _/ .| .|____ \n     \/____/ (u ⊃ \n---------------------------------------------------------------------------------------------------*/\n\n\n\n\nint W, H, T, P;\nint A[2][101][101];\n//---------------------------------------------------------------------------------------------------\nvoid _main() {\n while (cin >> W >> H >> T >> P) {\n if (W == 0) return;\n \n rep(t, 0, 2) rep(y, 0, 101) rep(x, 0, 101) A[t][y][x] = 0;\n rep(y, 0, H) rep(x, 0, W) A[0][y][x] = 1;\n\n rep(t, 0, T) {\n int d, c; cin >> d >> c;\n rep(y, 0, 101) rep(x, 0, 101) A[(t + 1) % 2][y][x] = 0;\n\n if (d == 1) {\n rep(y, 0, H) rep(x, 0, W) A[(t + 1) % 2][y][x] = A[t % 2][y][x + c];\n rep(x, 0, c) rep(y, 0, H) A[(t + 1) % 2][y][x] += A[t % 2][y][c - 1 - x];\n } else {\n rep(y, 0, H) rep(x, 0, W) A[(t + 1) % 2][y][x] = A[t % 2][y + c][x];\n rep(x, 0, W) rep(y, 0, c) A[(t + 1) % 2][y][x] += A[t % 2][c - 1 - y][x];\n }\n }\n\n rep(p, 0, P) {\n int x, y; cin >> x >> y;\n printf(\"%d\\n\", A[T % 2][y][x]);\n }\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3576, "score_of_the_acc": -0.0039, "final_rank": 3 }, { "submission_id": "aoj_1625_10617810", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvoid init() {}\n\nll w, h, t, p;\nvoid input() { cin >> w >> h >> t >> p; }\nvoid solve() {\n vector<vector<ll>> v(1000, vector<ll>(1000, 0));\n ll th = 0, tw = 0, bh = h, bw = w;\n\n for(int i = 0; i < h; i++)\n for(int j = 0; j < w; j++)\n v[i][j] = 1;\n\n for(int i = 0; i < t; i++) {\n ll a, b;\n cin >> a >> b;\n if(a == 1) {\n for(int j = th; j < bh; j++) {\n for(int k = tw; k < tw + b; k++) {\n ll diff = 2 * (tw + b - k) - 1;\n v[j][k + diff] += v[j][k];\n chmax(bw, (ll)(k+diff+1));\n }\n }\n tw += b;\n } else {\n for(int j = th; j < th + b; j++) {\n for(int k = tw; k < bw; k++) {\n ll diff = 2 * (th + b - j) - 1;\n v[j+diff][k] += v[j][k];\n chmax(bh, (ll)(j+diff+1));\n }\n }\n th += b;\n }\n }\n\n for(int i = 0; i < p; i++) {\n ll a, b;\n cin >> a >> b;\n cout << v[th + b][tw + a] << endl;\n }\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(w == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 1570, "memory_kb": 11052, "score_of_the_acc": -0.5758, "final_rank": 16 }, { "submission_id": "aoj_1625_10542589", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n#define int long long\n\nvoid solve(int n, int m, int t, int p) {\n int k = 30;\n vector<vector<int>> paper(m*k, vector<int>(n*k, 0));\n for (int i=0; i<m; ++i) {\n for (int j=0; j<n; ++j) {\n paper[m*k-i-1][j] = 1;\n }\n }\n\n int left = 0, low = m*k-1;\n for (;t--;) {\n int d, c;\n cin >> d >> c;\n if (d == 1) {\n //縦軸で折り返す\n for (int i=0; i<=low; ++i) {\n for (int j=0; j<c; ++j) {\n //left left+1 left+2 | left+6-2-1\n //left | left+2-0-1\n //paper[i][j]をpaper[i][left+2c-j-1]に加算\n paper[i][left+2*c-j-1] += paper[i][left+j];\n paper[i][left+j] = 0;\n }\n }\n left += c;\n } else {\n //横軸で折り返す\n for (int i=0; i<c; ++i) {\n for (int j=left; j<n*k; ++j) {\n //low low-1 low-2 | low-6+2+1 low-6+1+1 low-6+0+1\n paper[low-2*c+i+1][j] += paper[low-i][j];\n paper[low-i][j] = 0;\n }\n }\n low -= c;\n }\n // for (int _=0; _<10; ++_) cout << \"=\";\n // cout << \"here~~\" << \"\\n\";\n // cout << \"left low \" << left << ' ' << low << endl;\n // for (int i=0; i<m; ++i) {\n // for (int j=0; j<n; ++j) {\n // cout << paper[i][j] << ' ';\n // }\n // cout << endl;\n // }\n // for (auto _x: paper) {\n // for (auto __x: _x) {\n // cout << __x << ' ';\n // }\n // cout << endl;\n // }\n }\n\n for (;p--;) {\n int x, y;\n cin >> x >> y;\n cout << paper[low-y][left+x] << endl;\n }\n}\n\nsigned main() {\n while(true) {\n int n, m, t, p;\n cin >> n >> m >> t >> p;\n if (n+m+t+p == 0) break;\n solve(n, m, t, p);\n }\n}\n\n/*\nmemo\n\n\n2 1 1 1\n1 1\n0 0\n\n1 3 2 1\n2 1\n2 1\n0 0\n10 8 3 2\n2 2\n1 3\n1 1\n0 1\n3 4\n\n\n3 3 3 2\n1 2\n2 1\n1 1\n0 1\n0 0\n0 0 0 0\n\n\n*/", "accuracy": 1, "time_ms": 20, "memory_kb": 10668, "score_of_the_acc": -0.0955, "final_rank": 10 }, { "submission_id": "aoj_1625_10542460", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)\n#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)\n#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)\n#define each(p,a) for(auto &p:a)\ntypedef vector<ll> vll;\ntypedef vector<pair<ll ,ll>> vpll;\ntypedef vector<vector<ll>> vvll;\ntypedef set<ll> sll;\ntypedef map<ll , ll> mpll;\ntypedef pair<ll ,ll> pll;\ntypedef tuple<ll , ll , ll> tpl3;\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define all(a) (a).begin(),(a).end()\n#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );\n#define sz(x) (ll)x.size()\n// << std::fixed << std::setprecision(10)\nconst ll INF = 1LL << 30;\nconst ld EPS = 1e-9;\n \nll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}\nll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}\ninline ll positive_mod(ll a,ll m){return (a % m + m)%m;}\ninline ll popcnt(ull a){ return __builtin_popcountll(a);}\n//0indexed\ninline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}\ninline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}\ntemplate<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}\ntemplate<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}\ntemplate<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}\ntemplate<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?\" \":\"\");}return os;}\ntemplate<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << \"{\" << p.first << \",\" << p.second << \"}\";return os;}\ntemplate<class... T>void input(T&... a){(cin >> ... >> a);}\nvoid print(){cout << endl;}\ntemplate<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}\ntemplate<class T> void pspace(const T& a){ cout << a << ' ';}\nvoid perr(){cerr << endl;}\ntemplate<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}\nvoid yes(bool i = true){ return print(i?\"yes\":\"no\"); }\nvoid Yes(bool i = true){ return print(i?\"Yes\":\"No\"); }\nvoid YES(bool i = true){ return print(i?\"YES\":\"NO\"); }\ntemplate <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}\ntemplate <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}\ntemplate <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}\ntemplate <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}\n//grid探索用\nvector<ll> _ta = {0,0,1,-1,1,1,-1,-1};\nvector<ll> _yo = {1,-1,0,0,1,-1,1,-1};\nbool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}\n \nll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}\nll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} \nconst ll MOD9 = 998244353LL;\nconst ll MOD10 = 1000000007LL;\n\n\n // for AOJ or ICPC or etc..\ntemplate<class Tp>\nbool zero (const Tp &x) {\n return x == 0;\n}\n\ntemplate<class Tp, class... Args>\nbool zero (const Tp &x, const Args& ...args) {\n return zero(x) and zero(args...);\n}\n\nvoid solve(ll n,ll m,ll t,ll p){\n swap(n,m);\n vvll paper(700,vll(700));\n ll tp = 0,dn = n;\n ll l = 0,r = m; \n reps(i,tp,dn)reps(j,l,r){\n paper[i][j] = 1;\n }\n while(t--){\n LL(d,c);\n if(d == 1){\n for(ll i = tp;i < dn;i++){\n rep(j,c){\n paper[i][l+c+j] += paper[i][l+c-1-j];\n paper[i][l+c-1-j] = 0;\n }\n }\n l += c;\n chmax(r,l+c);\n }else{\n rep(i,c){\n for(ll j =l;j<r;j++){\n paper[tp+c+i][j] += paper[tp+c-1-i][j];\n paper[tp+c-1-i][j] = 0;\n }\n }\n tp += c;\n chmax(dn,tp+c);\n }\n }\n // ll ans = 0;\n while(p--){\n LL(x,y);\n cout << paper[tp+y][l+x] << endl;\n }\n}\n\nint main(){\n ios::sync_with_stdio(false);cin.tie(nullptr);\n while(true){\n LL(n,m,t,p);\n // perr(n,m,t,p);\n if(zero(n,m,t,p))break;\n solve(n,m,t,p);\n }\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 5376, "score_of_the_acc": -0.0969, "final_rank": 12 }, { "submission_id": "aoj_1625_10356120", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main () {\n while(1){\n int n,m,t,p;\n cin>>m>>n>>t>>p;\n if(n == 0 && m == 0 && t == 0 && p == 0){\n break;\n }\n vector<vector<int>> g(1000,vector<int>(1000,0));\n for(int i = 0 ; i < n;i++){\n for(int j = 0 ; j < m;j++){\n g[i][j] = 1;\n }\n }\n int d[t],c[t];\n for(int i = 0 ; i < t;i++){\n cin>>d[i]>>c[i];\n }\n int x[p],y[p];\n for(int i = 0 ; i < p;i++){\n cin>>x[i]>>y[i];\n }\n int L = 0,R = m,U = n ,D = 0;\n for(int i = 0 ; i < t;i++){\n if(d[i] == 1){\n for(int j = 0; j < c[i];j++){\n for(int k = D ; k <U;k++){\n g[k][L + c[i] + j] += g[k][L + c[i] - j - 1];\n }\n }\n L += c[i];\n if(R-L < c[i]){\n R = L + 2 * c[i];\n // R += (c[i] - R + 1);\n }\n }else{\n for(int j = 0 ; j < c[i];j++){\n for(int k = L ; k < R; k++){\n g[D + c[i] + j][k] += g[D + c[i] - j - 1][k];\n }\n }\n D += c[i];\n if(U-D < c[i]) {\n U = D + 2 * c[i];\n // U += (c[i]-U+1);\n }\n }\n }\n vector<vector<bool>> che(1000,vector<bool>(1000,false));\n for(int i = 0 ; i < p;i++){\n cout<<g[D + y[i]][L + x[i]]<<endl;\n che[D+y[i]][L+x[i]] = true;\n }\n }\n return(0);\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 7480, "score_of_the_acc": -0.0925, "final_rank": 9 }, { "submission_id": "aoj_1625_9424520", "code_snippet": "#include <bits/stdc++.h>\n//using\nusing intl = long long;\nusing ll = long long;\nusing itnl = long long;//typo用\nusing uintl = unsigned long long;\nusing itn = int;//typo用\nusing ld = long double;\nusing namespace std;\n\n//関数マクロ\n#define rep(i, n) for(intl i = 0; i < (intl)(n); i++)\n#define rrep(i, n) for(intl i = (intl)(n) - 1; i >= 0; i--)\n#define repi(i, a, b) for(intl i = (intl)(a); i < (intl)(b); i++)\n#define rrepi(i, a, b) for(intl i = (intl)(a) - 1; i >= (intl)(b); i--)\n#define all(x) (x).begin(),(x).end()\n#define rall(x) (x).rbegin(),(x).rend()\n#define m0(x) memset(x,0,sizeof(x))\n#define m1(x) memset(x,1,sizeof(x))\n#define fill(x,y) memset(x,y,sizeof(x))\n#define alength(a) (sizeof(a) / sizeof(a[0]))\n#define debug(x) cout << #x << \":\" << x << endl\n\n//定数マクロ\n#define pb push_back\n#define mp make_pair\n#define pii pair<intl,intl>\n\n//定数\nconst intl INF = 1e18;\nconst intl MOD = 1e9+7; //1e9より大きい最小の素数, 1e9より小さい最大の素数は998244353\nconst ld EPS = 1.0e-14;\nconst ld PI = acos(-1);\n\n//テンプレート関数\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) {a = b; return true;} return false; }\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) {a = b; return true;} return false; }\nintl modpow(intl a, intl n, intl mod) {intl res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}\nintl modpow(intl a, intl n) {intl res = 1; while (n > 0) {if (n & 1) res = res * a % MOD;a = a * a % MOD;n >>= 1;}return res;}\n//intl gcd(intl a, intl b){ if(!b)return a; return gcd(b, a % b); } //c++17からある\n//intl lcm(intl a,intl b){ return a / gcd(a, b) * b; } //c++17からある\n\n//自作テンプレート関数\nvoid fp(bool f){cout << (f ? \"Yes\" : \"No\") << endl;}//Yes,Noの出力を楽にするよう\nvoid fp(bool f, string s, string t){cout << (f ? s : t) << endl;}//上の関数のYes,Noを任意の文字列でできるようにしたもの\nintl fact(intl k){ intl a = 1; for(int i = 2; i <= k; i++){ a *= i; } return a; }//k!を求める関数,O(N)\nintl digit10(intl a){ intl b = 0; do{ a /= 10; b++; }while(a); return b; }//aを10進数で表したときの桁数を求める関数,O(logN)\nintl mceil(intl a, intl b){ return (a + b - 1) / b; }//aをbで割って切り上げる関数,O(1)\n\n\n//--------------入力受け取る場所----------------------------------------------------\nintl n,m,t,p;\nvector<intl> d,c,x,y;\n\n\nvoid input_var(){\n cin >> n >> m >> t >> p;\n if (n == 0 && m == 0 && t == 0 && p == 0) exit(0);\n rep(i, t) {\n cin >> d[i] >> c[i];\n }\n rep(i, p) {\n cin >> x[i] >> y[i];\n }\n}\n//-------------------------------------------------------------------------------\n\nvoid solve(){\n intl left = 0;\n intl right = n;\n intl top = 0;\n intl bottom = m;\n vector<vector<intl>> kasanari(100 * m + 1, vector<intl>(100 * n + 1, 0));\n rep(i,m)rep(j,n)kasanari[i][j] = 1;\n /*rep(j,m) {\n rep(k,n)cout << kasanari[j][k] << \" \";\n cout << endl;\n }*/\n rep(i, t) {\n //cout << d[i] << c[i] << \" \" << top << \" \" << left << endl;\n if (d[i] == 1) {\n for (intl j = 0; j < c[i]; j++) {\n for (intl k = top; k < bottom; k++) {\n kasanari[k][left + 2 * c[i] - j - 1] += kasanari[k][left + j];\n kasanari[k][left + j] = 0;\n }\n }\n right = max(left + 2 * c[i], right);\n left += c[i];\n }\n else {\n for (intl j = 0; j < c[i]; j++) {\n for (intl k = left; k < right; k++) {\n kasanari[top + 2 * c[i] - j - 1][k] += kasanari[top + j][k];\n kasanari[top + j][k] = 0;\n }\n }\n bottom += max(top + 2 * c[i], bottom);\n top += c[i];\n }\n /*rep(j,2*m+1) {\n rep(k,2*n+1)cout << kasanari[j][k] << \" \";\n cout << endl;\n }*/\n }\n rep(i, p) {\n //cout << bottom << \" \" << x[i] << \" \" << left << \" \" << y[i] << endl; \n cout << kasanari[y[i]+top][x[i]+left] << endl;\n }\n}\n\nsigned main(){\n cout << fixed << setprecision(10);\n\n d.resize(21);\n c.resize(21);\n x.resize(21);\n y.resize(21);\n while (true) {\n input_var();\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 83340, "score_of_the_acc": -1.1258, "final_rank": 19 }, { "submission_id": "aoj_1625_9408012", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int n,m,t,p;\n int d,c,x,y;\n int cum,cun;\n int ans;\n while(1){\n cin>>n>>m>>t>>p;\n ans=0;\n if(n+m+t+p==0){\n break;\n }\n vector<vector<int>> paper(1500,vector<int>(1500,0)); //横、高さの順\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n paper.at(j).at(i)=1;\n }\n }\n cun=0;\n cum=0;\n for(int i=0;i<t;i++){\n cin>>d>>c;\n if(d==1){\n for(int j=cum;j<m;j++){\n for(int k=cun+c,l=1;k<cun+2*c;k++,l+=2){\n paper.at(j).at(k)+=paper.at(j).at(k-l);\n }\n }\n cun+=c;\n n+=max(0,cun-n+c);\n }\n else if(d==2){\n for(int j=cum+c,l=1;j<cum+2*c;j++,l+=2){\n for(int k=cun;k<n;k++){\n paper.at(j).at(k)+=paper.at(j-l).at(k);\n }\n }\n cum+=c;\n m+=max(0,cum-m+c);\n }\n }\n ans=0;\n for(int i=0;i<p;i++){\n cin>>x>>y;\n cout<<paper.at(cum+y).at(cun+x)<<endl;\n }\n }\n}", "accuracy": 1, "time_ms": 1760, "memory_kb": 11968, "score_of_the_acc": -0.6455, "final_rank": 17 }, { "submission_id": "aoj_1625_9407864", "code_snippet": "//#include <atcoder/all>\n#include <bits/stdc++.h>\n#define rep(i, a, b) for (int i = a; i < b; i++)\n#define rrep(i, a, b) for (int i = a - 1; i >= b; i--)\n#define ll long long\n#define ull unsigned long long\n#define graph vector<vector<int>>\nusing namespace std;\n//using namespace atcoder;\n\nint mod=998244353;\nvector<int> dx={1,0,-1,0},dy={0,1,0,-1};\n\nint main(){\n while(1){\n int n,m,t,p; cin>>n>>m>>t>>p;\n if(n==0) break;\n\n vector<vector<ll>> grid(650,vector<ll>(650));\n rep(i,0,n){\n rep(j,0,m){\n grid[i][j]=1;\n }\n }\n\n int l=0,u=0;\n\n rep(i,0,t){\n int d,c; cin>>d>>c;\n if(d==1){\n l += c;\n rep(ii,u,u+m){\n rep(j,0,c){\n grid[l+j][ii] += grid[l-j-1][ii];\n grid[l-j-1][ii]=0;\n }\n }\n }\n\n else{\n u += c;\n rep(ii,l,l+n){\n rep(j,0,c){\n grid[ii][u+j] += grid[ii][u-j-1];\n grid[ii][u-j-1]=0;\n }\n }\n }\n }\n\n rep(i,0,p){\n int x,y; cin>>x>>y;\n cout << grid[x+l][y+u] << endl;\n }\n }\n \n}", "accuracy": 1, "time_ms": 630, "memory_kb": 6556, "score_of_the_acc": -0.2313, "final_rank": 15 }, { "submission_id": "aoj_1625_9407583", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define all(A) A.begin(),A.end()\n#define ALL(A) A.begin(),A.end()\n#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)\ntemplate<class T>\nbool chmax(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p < q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\ntemplate<class T>\nbool chmin(T& p, T q, bool C = 1) {\n if (C == 0 && p == q) {\n return 1;\n }\n if (p > q) {\n p = q;\n return 1;\n }\n else {\n return 0;\n }\n}\n\n\n\nvoid solve(ll N, ll M, ll T, ll P) {\n swap(N, M);\n vector<vll> D(N * 100, vll(M * 100, 0));\n rep(i, N)rep(j, M)D[i][j] = 1;\n rep(t, T) {\n ll d, c;\n cin >> d >> c;\n\n if (d == 1) {\n ll L = 1e18;\n rep(j, M * 100)rep(i, N * 100) {\n if (D[i][j] > 0) {\n L = min(L, j);\n break;\n }\n }\n for (ll j = L; j <= L + c - 1; j++) {\n rep(i, N * 100) {\n ll ds = L + c - j;\n D[i][L + c - 1 + ds] += D[i][j];\n D[i][j] = 0;\n }\n }\n }\n else {\n ll W = 1e18;\n rep(i, N * 100)rep(j, M * 100) {\n //if (W >= 0)break;\n if (D[i][j] > 0) {\n W = min(W, i);\n break;\n }\n }\n for (ll i = W; i <= W + c - 1; i++) {\n rep(j, M * 100) {\n ll ds = W + c - i;\n D[W + c - 1 + ds][j] += D[i][j];\n D[i][j] = 0;\n }\n }\n }\n }\n ll L = 1e18, W = 1e18;\n rep(i, N * 100)rep(j, M * 100) {\n if (D[i][j] > 0) {\n W = min(W, i);\n L = min(L, j);\n }\n }\n ll an = 0;\n rep(p, P) {\n ll X, Y;\n cin >> X >> Y;\n cout << D[Y + W][X + L] << endl;\n }\n}\n\n\nint main() {\n\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n\n ll N, M, T, P;\n while (cin >> N >> M >> T >> P, N != 0)solve(N, M, T, P);\n\n}", "accuracy": 1, "time_ms": 3270, "memory_kb": 83300, "score_of_the_acc": -1.9995, "final_rank": 20 }, { "submission_id": "aoj_1625_9375390", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int n, int m, int t, int p){\n vector<int> d(t), c(t);\n for(int i = 0; i < t; i++){\n cin >> d[i] >> c[i];\n }\n vector<int> x(p), y(p);\n for(int i = 0; i < p; i++){\n cin >> x[i] >> y[i];\n }\n \n // 動的にメモリを確保\n vector<vector<int>> paper(n * n, vector<int>(m * m, 0));\n for(int i = 0; i < n; i++){\n for(int j = 0; j < m; j++){\n paper[i][j] = 1;\n }\n }\n \n int delx = 0, dely = 0;\n int widx = n, widy = m;\n\n for(int i = 0; i < t; i++){\n if(d[i] == 1){\n for(int j = dely; j < dely + widy; j++){\n for(int k = delx; k < delx + c[i]; k++){\n paper[2 * (delx + c[i]) - 1 - k][j] += paper[k][j];\n }\n }\n delx += c[i];\n if(c[i] * 2 < widx) widx -= c[i];\n else widx = c[i];\n }\n else if(d[i] == 2){\n for(int j = delx; j < delx + widx; j++){\n for(int k = dely; k < dely + c[i]; k++){\n paper[j][2 * (dely + c[i]) - 1 - k] += paper[j][k];\n }\n }\n dely += c[i];\n if(c[i] * 2 < widy) widy -= c[i];\n else widy = c[i];\n }\n }\n\n for(int i = 0; i < p; i++){\n // 範囲外アクセスを防ぐためのチェック\n if (delx + x[i] < n * n && dely + y[i] < m * m) {\n cout << paper[delx + x[i]][dely + y[i]] << endl;\n } \n //else {\n // cout << \"Out of bounds\" << endl;\n //}\n }\n}\n\nint main() {\n while(true){\n int n, m, t, p;\n cin >> n >> m >> t >> p;\n if(n == 0) break;\n solve(n, m, t, p);\n }\n //return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 7344, "score_of_the_acc": -0.051, "final_rank": 6 }, { "submission_id": "aoj_1625_9375327", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int n, int m, int t, int p){\n vector<int> d(t), c(t);\n for(int i = 0; i < t; i++){\n cin >> d[i] >> c[i];\n }\n vector<int> x(p), y(p);\n for(int i = 0; i < p; i++){\n cin >> x[i] >> y[i];\n }\n \n // 動的にメモリを確保\n vector<vector<int>> paper(n * n, vector<int>(m * m, 0));\n for(int i = 0; i < n; i++){\n for(int j = 0; j < m; j++){\n paper[i][j] = 1;\n }\n }\n \n int delx = 0, dely = 0;\n int widx = n, widy = m;\n\n for(int i = 0; i < t; i++){\n if(d[i] == 1){\n for(int j = dely; j < dely + widy; j++){\n for(int k = delx; k < delx + c[i]; k++){\n paper[2 * (delx + c[i]) - 1 - k][j] += paper[k][j];\n }\n }\n delx += c[i];\n if(c[i] * 2 < widx) widx -= c[i];\n else widx = c[i];\n }\n else if(d[i] == 2){\n for(int j = delx; j < delx + widx; j++){\n for(int k = dely; k < dely + c[i]; k++){\n paper[j][2 * (dely + c[i]) - 1 - k] += paper[j][k];\n }\n }\n dely += c[i];\n if(c[i] * 2 < widy) widy -= c[i];\n else widy = c[i];\n }\n }\n\n for(int i = 0; i < p; i++){\n // 範囲外アクセスを防ぐためのチェック\n if (delx + x[i] < n * n && dely + y[i] < m * m) {\n cout << paper[delx + x[i]][dely + y[i]] << endl;\n } else {\n cout << \"Out of bounds\" << endl;\n }\n }\n}\n\nint main() {\n while(true){\n int n, m, t, p;\n cin >> n >> m >> t >> p;\n if(n == 0) break;\n solve(n, m, t, p);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 7128, "score_of_the_acc": -0.0513, "final_rank": 7 }, { "submission_id": "aoj_1625_9346986", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#ifdef LOCAL\n #include \"settings/debug.cpp\"\n #define _GLIBCXX_DEBUG\n#else\n #define Debug(...) void(0)\n#endif\n#define rep(i, n) for (int i = 0; i < (n); ++i)\nusing ll = long long;\nusing ull = unsigned long long;\n\nint main() {\n while (true) {\n ll n, m, t, p;\n cin >> m >> n >> t >> p;\n if (n == 0 && m == 0 && t == 0 && p == 0) {\n break;\n }\n ll left = 0, bottom = 30 * n - 1;\n vector<vector<ll>> cnt(30 * n, vector<ll>(30 * m, 0));\n rep(i, n) rep(j, m) cnt[30 * n - 1 - i][j] = 1;\n rep(i, t) {\n ll d, c;\n cin >> d >> c;\n if (d == 1) {\n rep(i, c) {\n ll now_x = bottom, now_y = left + i;\n while (true) {\n if (cnt[now_x][now_y] == 0) {\n break;\n }\n cnt[now_x][left + 2 * c - 1 - i] += cnt[now_x][now_y];\n now_x--;\n // cerr << now_x << \" \" << now_y << endl;\n }\n }\n left += c;\n }\n else {\n rep(i, c) {\n ll now_x = bottom - i, now_y = left;\n while (true) {\n if (cnt[now_x][now_y] == 0) {\n break;\n }\n cnt[bottom - (2 * c - 1 - i)][now_y] += cnt[now_x][now_y];\n now_y++;\n // cerr << \"now: \" << now_x << \" \" << now_y << endl;\n }\n }\n bottom -= c;\n }\n // rep(i, n) {\n // rep(j, m) {\n // cerr << cnt[bottom - i][left + j] << \" \";\n // }\n // cerr << endl;\n // }\n // cerr << endl;\n }\n rep(i, p) {\n ll x, y;\n cin >> y >> x;\n cout << cnt[bottom - x][left + y] << endl;\n }\n }\n return 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 10476, "score_of_the_acc": -0.0962, "final_rank": 11 }, { "submission_id": "aoj_1625_9335785", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<ll, ll>;\n#define rep(i, a, b) for(ll i = a; i < b; ++i)\n#define rrep(i, a, b) for(ll i = a; i >= b; --i)\nconstexpr ll inf = 4e18;\nstruct SetupIO {\n SetupIO() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout << fixed << setprecision(30);\n }\n} setup_io;\n\nll D = 100;\nvector<vector<ll>> rot(vector<vector<ll>> tab) {\n vector<vector<ll>> rot_tab(D * 3);\n rep(i,0,D * 3){\n rot_tab[i].resize(D * 3, 0);\n }\n rep(i,0,D * 3){\n rep(j,0,D * 3){\n rot_tab[i][j] = tab[j][i];\n }\n }\n return rot_tab;\n}\nbool valid(ll i, ll j){\n return (0 <= i) && (i < D * 3) && (0 <= j) && (j < D * 3);\n}\nvector<vector<ll>> oru(vector<vector<ll>> tab, ll c) {\n vector<vector<ll>> new_tab(D * 3);\n rep(i,0,D * 3){\n new_tab[i].resize(D * 3, 0);\n }\n\n rep(i,0,D * 3){\n rep(j,0,D * 3){\n if(j < D + c) {\n continue;\n }\n ll j2 = (D+c) + (D+c-1) - j;\n if (!valid(i, j2)) {\n continue;\n }\n new_tab[i][j] = tab[i][j] + tab[i][(D+c) + (D+c-1) - j];\n }\n }\n return new_tab;\n}\nvector<vector<ll>> hidariue(vector<vector<ll>> tab) {\n // 左上が(D, D)に来るように\n vector<vector<ll>> new_tab(D * 3);\n rep(i,0,D * 3){\n new_tab[i].resize(D * 3, 0);\n }\n\n ll min_i = inf;\n ll min_j = inf;\n rep(i,0,D * 3){\n rep(j,0,D * 3){\n if(tab[i][j] > 0) {\n min_i = min(min_i, i);\n min_j = min(min_j, j);\n }\n }\n }\n\n rep(i,0,D * 3){\n rep(j,0,D * 3){\n if (valid(i + (min_i - D), j + (min_j - D))) {\n new_tab[i][j] = tab[i + (min_i - D)][j + (min_j - D)];\n }\n }\n }\n\n return new_tab;\n}\n// void output(vector<vector<ll>> tab) {\n// cerr << \"\\n\";\n// rep(i,0,D * 3) {\n// rep(j,0,D * 3) {\n// cerr << tab[i][j];\n// }\n// cerr << \"\\n\";\n// }\n// cerr << \"\\n\";\n// }\n\nint main(void) {\n while(true){\n ll n,m,t,p;cin>>n>>m>>t>>p;\n if (n == 0) break;\n vector<vector<ll>> tab(D * 3);\n rep(i,0,D * 3){\n tab[i].resize(D * 3, 0);\n }\n rep(i,D,D+m){\n rep(j,D,D+n){\n tab[i][j] = 1;\n }\n }\n rep(i,0,t){\n ll d,c;cin>>d>>c;\n if (d == 2) {\n tab = rot(tab);\n }\n tab = oru(tab, c);\n if (d == 2) {\n tab = rot(tab);\n }\n tab = hidariue(tab);\n }\n rep(i,0,p){\n ll x, y;cin>>x>>y;\n cout << tab[D + y][D + x] << \"\\n\";\n }\n }\n}", "accuracy": 1, "time_ms": 2340, "memory_kb": 5280, "score_of_the_acc": -0.7399, "final_rank": 18 }, { "submission_id": "aoj_1625_9322405", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <deque>\n#include <algorithm>\n#include <assert.h>\n\nusing std::cin, std::cout, std::vector, std::string, std::pair;\n\n#define rep(i, n) for (int i = 0; i < (n); ++i)\n\ntemplate <typename T>\nvoid print_vector(const vector<T> &vec, const string &splitter = \" \") {\n for (int i = 0; i < vec.size(); ++i) {\n cout << vec[i];\n if (i == vec.size() - 1) cout << \"\\n\";\n else cout << splitter;\n }\n}\n\n\n// s = (2b - n^2 + 1)/2n\n\nint main() {\n int w, h, t, p;\n\n while (cin >> w >> h >> t >> p, w) {\n vector pos(h, vector<pair<int, int>>(w));\n std::multimap<pair<int, int>, pair<int, int>> map;\n for (int i = 0; i < h; ++i) {\n for (int j = 0; j < w; ++j) {\n pos[i][j] = {i, j};\n map.emplace(pair {i, j}, pair {i, j});\n }\n }\n\n int left_x = 0, right_x = w, bottom_y = 0, top_y = h;\n rep(q, t) {\n int d, c; cin >> d >> c;\n\n if (d == 1) {\n for (int y = bottom_y; y < top_y; ++y) {\n for (int xd = 0; xd < c; ++xd) {\n int x = left_x + c - xd - 1;\n auto rng = map.equal_range({y, x});\n int nxt_x = left_x + c + xd;\n for (auto iter = rng.first; iter != rng.second; ++iter) {\n auto [i, j] = iter->second;\n pos[i][j] = { y, nxt_x };\n map.emplace(pair { y, nxt_x }, pair { i, j });\n }\n map.erase({ y, x });\n }\n }\n\n left_x += c;\n if (left_x + c >= right_x) {\n right_x = left_x + c;\n }\n } else {\n for (int yd = 0; yd < c; ++yd) {\n int y = bottom_y + c - yd - 1;\n int nxt_y = bottom_y + c + yd;\n for (int x = left_x; x < right_x; ++x) {\n auto rng = map.equal_range({ y, x });\n for (auto iter = rng.first; iter != rng.second; ++iter) {\n auto [i, j] = iter->second;\n pos[i][j] = { nxt_y, x };\n map.emplace(pair { nxt_y, x }, pair { i, j });\n }\n map.erase({ y, x });\n }\n }\n\n bottom_y += c;\n if (bottom_y + c >= top_y) {\n top_y = bottom_y + c;\n }\n }\n }\n\n// int ans = 0;\n rep(q, p) {\n int x, y; cin >> x >> y;\n int tmp = map.count({ y + bottom_y, x + left_x });\n// ans += tmp;\n cout << tmp << std::endl;\n// std::cerr << tmp << \"\\n\";\n }\n// cout << ans << std::endl;\n// std::cerr << std::endl;\n }\n\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3308, "score_of_the_acc": -0.0005, "final_rank": 1 }, { "submission_id": "aoj_1625_9309965", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\n#define rep(i, n) for(ll i = 0; i < (n); i++)\n#define reps(i, l, r) for(ll i = (l); i < (r); i++)\n#define all(a) (a).begin(), (a).end()\n#define endl \"\\n\";\nconst ll INF = 2e18;\nconst ll mod1 = 1000000007;\nconst ll mod2 = 998244353;\nll dx[4] = {-1, 0, 1, 0};\nll dy[4] = {0, -1, 0, 1};\n\nvoid solve() {\n ll N, M, T, P; cin >> N >> M >> T >> P;\n while (N != 0 || M != 0 || T != 0 || P != 0) {\n vector<vector<ll>>A(N*N, vector<ll>(M*M, 0));\n rep(i, N) rep(j, M) A[i][j] = 1;\n ll u = 0, l = 0, up = N, lp = M;\n while (T--) {\n ll d, c; cin >> d >> c;\n if (d == 1) {\n ll nu = u + 2 * c - 1;\n up = max(up, nu + 1);\n while (1) {\n reps(i, l, lp) {A[nu][i] += A[u][i];}\n if (u + 1 == nu) break;\n u++; nu--;\n }\n u = nu;\n }\n else {\n ll nl = l + 2 * c - 1;\n lp = max(lp, nl + 1);\n while (1) {\n reps(i, u, up) {A[i][nl] += A[i][l];}\n if (l + 1 == nl) break;\n l++; nl--;\n }\n l = nl;\n }\n }\n while (P--) {\n ll x, y; cin >> x >> y;\n x += u; y += l;\n cout << A[x][y] << endl;\n }\n cin >> N >> M >> T >> P;\n }\n}\nsigned main() {\n ll T = 1; //cin >> T;\n while (T--) solve();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 11172, "score_of_the_acc": -0.1049, "final_rank": 13 }, { "submission_id": "aoj_1625_9223352", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint A[200][200];\n\nint main(){\n int n,m,t,p;\n while(1){\n cin>>n>>m>>t>>p;\n if(n==0&&m==0&&t==0&&p==0)break;\n\n for(int i=0;i<200;i++)\n for(int j=0;j<200;j++)\n A[i][j]=0;\n \n for(int i=0;i<m;i++)\n for(int j=0;j<n;j++)\n A[i][j]=1;\n \n for(int u=0;u<t;u++){\n int d,c;\n cin>>d>>c;\n if(d==1){\n \n for(int i=0;i<m;i++){\n for(int j=0;j<c;j++){\n int dist=c-j;\n A[i][c+dist-1]+=A[i][j];\n A[i][j]=0;\n }\n }\n\n for(int i=0;i<100;i++){\n for(int j=0;j<100;j++){\n A[i][j]=A[i][j+c];\n }\n }\n \n }else{\n \n for(int i=0;i<c;i++){\n for(int j=0;j<n;j++){\n int dist=c-i;\n A[c+dist-1][j]+=A[i][j];\n A[i][j]=0;\n }\n }\n\n for(int i=0;i<100;i++){\n for(int j=0;j<100;j++){\n A[i][j]=A[i+c][j];\n }\n }\n \n }\n \n }\n \n for(int i=0;i<p;i++){\n int x,y;\n cin>>x>>y;\n cout<<A[y][x]<<endl;\n }\n \n }\n return 0;\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3264, "score_of_the_acc": -0.0031, "final_rank": 2 }, { "submission_id": "aoj_1625_8856207", "code_snippet": "#include <bits/stdc++.h>\n\nint solve() {\n int m, n, t, p;\n std::cin >> m >> n >> t >> p;\n if (n == 0) return 1;\n const int N = 1000;\n std::vector count(N, std::vector<int>(N));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n count[i][j] = 1;\n }\n }\n int left = 0;\n int low = 0;\n while (t--) {\n int d, c;\n std::cin >> d >> c;\n if (d == 1) {\n int right = left + 2 * c;\n for (int i = low; i < N; i++) {\n for (int j = 0; j < c; j++) {\n count[i][right - 1 - j] += count[i][left + j];\n }\n }\n left += c;\n } else {\n int up = low + 2 * c;\n for (int i = 0; i < c; i++) {\n for (int j = left; j < N; j++) {\n count[up - 1 - i][j] += count[low + i][j];\n }\n }\n low += c;\n }\n // for (int i = 0; i < 2 * n; i++) {\n // for (int j = 0; j < 2 * m; j++) {\n // std::cerr << count[i][j] << ' ';\n // }\n // std::cerr << std::endl;\n // }\n // std::cerr << \"-----------\" << std::endl;\n }\n\n while (p--) {\n int x, y;\n std::cin >> x >> y;\n std::cout << count[low + y][left + x] << '\\n';\n }\n return 0;\n}\n\nint main() {\n while (!solve());\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 7076, "score_of_the_acc": -0.0721, "final_rank": 8 }, { "submission_id": "aoj_1625_8011346", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint N, M, T, P;\nlong long A[1050][1050];\nint D[30], C[30];\nint X[30], Y[30];\nvector<long long> Ans;\n\nint main() {\n while (true) {\n cin >> M >> N >> T >> P;\n if (N == 0 && M == 0 && T == 0 && P == 0) {\n break;\n }\n int lef = 0, dow = 0;\n for (int i = 0; i < 1050; i++) {\n for (int j = 0; j < 1050; j++) {\n A[i][j] = 0;\n }\n }\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < M; j++) {\n A[i][j] = 1;\n }\n }\n for (int i = 0; i < T; i++) {\n cin >> D[i] >> C[i];\n }\n for (int i = 0; i < P; i++) {\n cin >> Y[i] >> X[i];\n }\n for (int i = 0; i < T; i++) {\n if (D[i] == 1) {\n for (int j = 0; j < C[i]; j++) {\n for (int k = dow; k < 1050; k++) {\n int ll = C[i] + lef - 1 - j;\n int rr = j + C[i] + lef;\n A[k][rr] += A[k][ll];\n }\n }\n lef += C[i];\n } else {\n for (int j = 0; j < C[i]; j++) {\n for (int k = lef; k < 1050; k++) {\n int dd = dow + C[i] - 1 - j;\n int uu = j + C[i] + dow;\n A[uu][k] += A[dd][k];\n }\n }\n dow += C[i];\n }\n }\n for (int i = 0; i < P; i++) {\n Ans.push_back(A[X[i] + dow][Y[i] + lef]);\n }\n }\n for (auto x : Ans) {\n cout << x << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 220, "memory_kb": 11988, "score_of_the_acc": -0.1734, "final_rank": 14 }, { "submission_id": "aoj_1625_8001380", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for(long long i = 0;i < (long long)(n);i++)\n\nmap<pair<ll,ll>,ll> f(map<pair<ll,ll>,ll> mp){\n //0,0の調整\n ll mi_n = 1LL << 60;\n ll mi_m = 1LL << 60;\n for(auto &q:mp){\n if(q.second == 0)continue;\n auto [x,y] = q.first;\n if(x < mi_n){\n mi_n = x;\n }\n if(y < mi_m){\n mi_m = y;\n }\n }\n\n map<pair<ll,ll>,ll> ret;\n for(auto &q:mp){\n if(q.second == 0)continue;\n auto [x,y] = q.first;\n ret[{x- mi_n,y - mi_m}] = q.second;\n }\n return ret;\n\n \n}\n\nint main(){\n while(true){\n ll n,m,t,p;cin >> n >> m >> t >> p;\n if(n == 0 && m == 0 && t == 0 && p == 0){\n break;\n }\n map<pair<ll,ll>,ll> mp;\n // vector<vector<pair<ll,ll>>> g(n,vector<pair<ll,ll>>(m));\n rep(i,n){\n rep(j,m){\n mp[{i,j}] = 1;\n }\n }\n\n //おる\n rep(z,t){\n ll d,c;cin >> d >> c;\n map<pair<ll,ll>,ll> tmp_mp;\n if(d == 1){\n for(auto &q:mp){\n auto[x,y] = q.first;\n if(x <c){\n tmp_mp[{c+(c-x-1),y}] += q.second;\n q.second = 0;\n }\n }\n }else{\n for(auto &q:mp){\n auto[x,y] = q.first;\n if(y <c){\n tmp_mp[{x,c+(c-y- 1)}] += q.second;\n q.second = 0;\n }\n }\n }\n for(auto &q:tmp_mp){\n mp[q.first] += q.second;\n }\n mp = f(mp);\n\n }\n\n rep(z,p){\n ll x,y;cin >> x >> y;\n if(mp.count({x,y})){\n cout << mp[{x,y}] << endl;\n }else{\n //ないけど\n cout << 0 << endl;\n }\n }\n\n \n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3712, "score_of_the_acc": -0.0056, "final_rank": 4 } ]
aoj_1628_cpp
Floating-Point Numbers In this problem, we consider floating-point number formats, data representation formats to approximate real numbers on computers. Scientific notation is a method to express a number, frequently used for numbers too large or too small to be written tersely in usual decimal form. In scientific notation, all numbers are written in the form m × 10 e . Here, m (called significand ) is a number greater than or equal to 1 and less than 10, and e (called exponent ) is an integer. For example, a number 13.5 is equal to 1.35 × 10 1 , so we can express it in scientific notation with significand 1.35 and exponent 1. As binary number representation is convenient on computers, let's consider binary scientific notation with base two, instead of ten. In binary scientific notation, all numbers are written in the form m × 2 e . Since the base is two, m is limited to be less than 2. For example, 13.5 is equal to 1.6875 × 2 3 , so we can express it in binary scientific notation with significand 1.6875 and exponent 3. The significand 1.6875 is equal to 1 + 1/2 + 1/8 + 1/16, which is 1.1011 2 in binary notation. Similarly, the exponent 3 can be expressed as 11 2 in binary notation. A floating-point number expresses a number in binary scientific notation in finite number of bits. Although the accuracy of the significand and the range of the exponent are limited by the number of bits, we can express numbers in a wide range with reasonably high accuracy. In this problem, we consider a 64-bit floating-point number format, simplified from one actually used widely, in which only those numbers greater than or equal to 1 can be expressed. Here, the first 12 bits are used for the exponent and the remaining 52 bits for the significand. Let's denote the 64 bits of a floating-point number by b 64 ... b 1 . With e an unsigned binary integer ( b 64 ... b 53 ) 2 , and with m a binary fraction represented by the remaining 52 bits plus one (1. b 52 ... b 1 ) 2 , the floating-point number represents the number m × 2 e . We show below the bit string of the representation of 13.5 in the format described above. In floating-point addition operations, the results have to be approximated by numbers representable in floating-point format. Here, we assume that the approximation is by truncation. When the sum of two floating-point numbers a and b is expressed in binary scientific notation as a + b = m × 2 e (1 ≤ m < 2, 0 ≤ e < 2 12 ), the result of addition operation on them will be a floating-point number with its first 12 bits representing e as an unsigned integer and the remaining 52 bits representing the first 52 bits of the binary fraction of m . A disadvantage of this approximation method is that the approximation error accumulates easily. To verify this, let's make an experiment of adding a floating-point number many times, as in the pseudocode shown below. Here, s and a are floating-point numbers, and the results of individual addition are approximated as describ ...(truncated)
[ { "submission_id": "aoj_1628_10670479", "code_snippet": "//#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (ll i=0;i<(ll)n;i++)\n#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)\n#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)\n#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)\n#define vl vector<ll>\n#define vvl vector<vector<ll>>\n#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<\" \";}cout<<endl;\n#define vpdbg(a) rep(ii,a.size()){cout<<\"{\"<<a[ii].first<<\",\"<<a[ii].second<<\"} \";}cout<<endl;\n#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<\" \";}cout<<endl;}\n#define setdbg(a) for(const auto & ii:a){cout<<ii<<\" \";}cout<<endl;\n#define inf 4000000000000000000LL\n#define mod 998244353LL\n//#define mod 1000000007LL\n#define eps 0.000000001\nrandom_device rnd;// 非決定的な乱数生成器\nmt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード\n\n//#include<boost/multiprecision/cpp_int.hpp>\n//#define bbi boost::multiprecision::cpp_int\n//#include<atcoder/lazysegtree>\n\n\n//整数同士の累乗の計算をする。\nll power(ll A, ll B) {\n\tll result = 1;\n\tfor (ll i=0;i<B;i++){\n\t\tresult *= A;\n\t}\n\treturn result;\n}\n\n// nのk乗をmodで割った余りを計算\nll power_mod(ll n, ll k){\n\tlong long result = 1;\n\twhile (k > 0){\n\t\tif ((k&1) ==1)result=(result*n)%mod;\n\t\tn=n*n%mod;\n\t\tk >>= 1;\n\t}\n\treturn result;\n}\n\n\n//受け取った2次元文字の外側に、文字pをコーティングする。\nvector<string> pad(vector<string> &s,char p){\n\tll h=s.size();\n\tll w=s[0].size();\n\tvector<string> res(h+2,string(w+2,p));\n\trep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];\n\treturn res;\n}\n\n// Union-Find\nstruct UnionFind {\n\tvector<int> par, siz;\n\tUnionFind(int n) : par(n, -1) , siz(n, 1) { }\n\t// 根を求める\n\tint root(int x) {\n\t\tif (par[x] == -1) return x;\n\t\telse return par[x] = root(par[x]);\n\t}\n\t// x と y が同じグループに属するかどうか (根が一致するかどうか)\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\t// x を含むグループと y を含むグループとを併合する\n\tbool unite(int x, int y) {\n\t\tx = root(x), y = root(y);\n\t\tif (x == y) return false; \n\t\tif (siz[x] < siz[y]) swap(x, y);\n\t\tpar[y] = x;\n\t\tsiz[x] += siz[y];\n\t\treturn true;\n\t}\n\t// x を含むグループのサイズ\n\tint size(int x) {\n\t\treturn siz[root(x)];\n\t}\n};\n\n\n//グリッド問題等用\nvl dx={1,0,-1,0};\nvl dy={0,1,0,-1};\n\n//受け取った文字列を、第2引数が0なら全て小文字に、1なら大文字に変換する関数\nstring cnv_string(const string &str, int mode) {\n\tstring result = str;\n\tif (mode == 0) {\n\t\t// 小文字に変換\n\t\tfor (char &c : result) {\n\t\t\tc = tolower(c);\n\t\t}\n\t} else if (mode == 1) {\n\t\t// 大文字に変換\n\t\tfor (char &c : result) {\n\t\t\tc = toupper(c);\n\t\t}\n\t}\n\treturn result;\n}\n\n//第一引数で受け取った数を、第二引数で受け取った数の進数と見做して、第三引数の進数へ変換する。\nstring cnv_base(const string &str, ll from_base, ll to_base) {\n\tll num = 0;\n\t//小文字があったら大文字に変換\n\tstring num_str=cnv_string(str,1);\n\t// 数値を10進数に変換\n\tfor (char digit : num_str) {\n\t\tnum = num * from_base + (isdigit(digit) ? digit - '0' : digit - 'A' + 10);\n\t}\n\tstring result;\n\t// 数値を目的の基数に変換\n\twhile (num > 0) {\n\t\tll remainder = num % to_base;\n\t\tresult.push_back(remainder < 10 ? remainder + '0' : remainder - 10 + 'A');\n\t\tnum /= to_base;\n\t}\n\t// 結果を逆順にして返す\n\treverse(result.begin(), result.end());\n\treturn result.empty() ? \"0\" : result;\n}\n\nstring b;\n\n//b×nを計算\nstring muls(ll n){\n\tll bint=stoll(cnv_base(b,2,10));\n\tll ans=n*bint;\n\treturn cnv_base(to_string(ans),10,2);\n}\nstring pluss(string x,string y){\n\tll ans=stoll(cnv_base(x,2,10))+stoll(cnv_base(y,2,10));\n\treturn cnv_base(to_string(ans),10,2);\n}\n\nll solve(){\n\tll n;\n\tcin>>n;\n\tif(n==0){return 1;}\n\n\tcin>>b;\n\tb.insert(b.begin(),'1');\n\n\tll kasuu=0;\n\tstring ans=b;\n\t\n\twhile(n!=0){\n\t\tll mn=1,mx=min(n,1LL<<(54-b.size()));\n\n\t\twhile(mn!=mx){\n\t\t\tll mid=mn+mx;\n\t\t\tmid/=2;\n\t\t\tstring s=pluss(ans,muls(mid));\n\t\t\tif(s.size()==54)mx=mid;\n\t\t\telse mn=mid+1;\n\t\t}\n\n\t\tans=pluss(ans,muls(mx));\n\t\tn-=mx;\n\t\tif(ans.size()==53){\n\t\t\tbreak;\n\t\t}\n\n\t\tans.pop_back();\n\t\tb.pop_back();\n\t\tif(b.size()==0)b=\"0\";\n\t\tkasuu++;\n\t}\n\tstring ans1 = cnv_base(to_string(kasuu),10,2);\n\twhile(ans1.size()!=12)ans1.insert(ans1.begin(),'0');\n\n\tcout<<ans1;\n\tloop(i,1,52)cout<<ans[i];\n\tcout<<endl;\n\treturn 0;\n}\n\n//メイン\nint main(){\n\twhile(solve()==0);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 660, "memory_kb": 3456, "score_of_the_acc": -1.1617, "final_rank": 14 }, { "submission_id": "aoj_1628_10662397", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\nusing pbds_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_mset = tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pbds_umap = gp_hash_table<int, int>;\nusing pbds_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nstruct Matrix {\n vector<vector<T>> m;\n Matrix() = default;\n Matrix(int x) : m(vector(x, vector<T>(x, 0))) {}\n Matrix(const vector<vector<T>> &a) : m(a) {}\n vector<T> &operator[](int i) { return m[i]; }\n const vector<T> &operator[](int i) const { return m[i]; }\n static Matrix identity(int x) {\n Matrix res(x);\n for (int i = 0; i < x; i++) res[i][i] = 1;\n return res;\n }\n static Matrix zero(int x) { return Matrix(x); }\n\n Matrix operator+() const { return (*this); }\n Matrix operator-() const { return Matrix(this->m.size()) - (*this); }\n Matrix operator+(const Matrix &a) const {\n Matrix x = (*this);\n return x += a;\n }\n Matrix operator-(const Matrix &a) const {\n Matrix x = (*this);\n return x -= a;\n }\n Matrix operator*(const Matrix &a) const {\n Matrix x = (*this);\n return x *= a;\n }\n Matrix operator*(const T &a) const {\n Matrix x = (*this);\n return x *= a;\n }\n Matrix &operator+=(const Matrix &a) {\n int n = m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n m[i][j] += a[i][j];\n }\n }\n return *this;\n }\n Matrix &operator-=(const Matrix &a) {\n int n = m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n m[i][j] -= a[i][j];\n }\n }\n return *this;\n }\n Matrix &operator*=(const Matrix &a) {\n int n = m.size();\n Matrix res(n);\n for (int i = 0; i < n; i++) {\n for (int k = 0; k < n; k++) {\n for (int j = 0; j < n; j++) {\n res[i][j] += m[i][k] * a[k][j];\n }\n }\n }\n m = res.m;\n return *this;\n }\n Matrix &operator*=(const T &a) {\n int n = m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n m[i][j] *= a;\n }\n }\n return *this;\n }\n Matrix pow(ll b) const {\n Matrix x = *this, res = identity(x.m.size());\n while (b) {\n if (b & 1) {\n res *= x;\n }\n x *= x;\n b >>= 1;\n }\n return res;\n }\n T determinant() {\n int n = m.size();\n Matrix A(*this);\n T ret = 1;\n for (int i = 0; i < n; i++) {\n int pivot = -1;\n for (int j = i; j < n; j++) {\n if (A[j][i] != 0) pivot = j;\n }\n if (pivot == -1) return T(0);\n if (i != pivot) {\n ret *= -1;\n swap(A[i], A[pivot]);\n }\n ret *= A[i][i];\n T tmp = T(1) / A[i][i];\n for (int j = 0; j < n; j++) {\n A[i][j] *= tmp;\n }\n for (int j = i + 1; j < n; j++) {\n T a = A[j][i];\n for (int k = 0; k < n; k++) {\n A[j][k] -= A[i][k] * a;\n }\n }\n }\n return ret;\n }\n Matrix inverse() {\n assert(determinant() != 0);\n int n = m.size();\n Matrix ret = identity(n);\n Matrix A(*this);\n for (int i = 0; i < n; i++) {\n int pivot = -1;\n for (int j = i; j < n; j++) {\n if (A[j][i] != 0) pivot = j;\n }\n if (i != pivot) {\n swap(ret[i], ret[pivot]);\n swap(A[i], A[pivot]);\n }\n T tmp = T(1) / A[i][i];\n for (int j = 0; j < n; j++) {\n A[i][j] *= tmp;\n ret[i][j] *= tmp;\n }\n for (int j = 0; j < n; j++) {\n if (j == i) continue;\n T a = A[j][i];\n for (int k = 0; k < n; k++) {\n A[j][k] -= A[i][k] * a;\n ret[j][k] -= ret[i][k] * a;\n }\n }\n }\n return ret;\n }\n vector<T> characteristic_polynomial() {\n int n = m.size();\n if (n == 0) return {1};\n Matrix A(*this);\n for (int i = 1; i < n; i++) {\n int pivot = -1;\n for (int j = i; j < n; j++) {\n if (A[j][i - 1] != 0) pivot = j;\n }\n if (pivot == -1) continue;\n if (i != pivot) {\n swap(A[i], A[pivot]);\n for (int j = 0; j < n; j++) swap(A[j][i], A[j][pivot]);\n }\n T tmp = T(1) / A[i][i - 1];\n vector<T> c(n);\n for (int j = i + 1; j < n; j++) c[j] = tmp * A[j][i - 1];\n for (int j = i + 1; j < n; j++) {\n for (int k = i - 1; k < n; k++) {\n A[j][k] -= A[i][k] * c[j];\n }\n }\n for (int j = 0; j < n; j++) {\n for (int k = i + 1; k < n; k++) {\n A[j][i] += A[j][k] * c[k];\n }\n }\n }\n vector dp(n + 1, vector<T>(n + 1));\n dp[0][0] = 1;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j <= i; j++) {\n dp[i + 1][j] -= dp[i][j] * A[i][i];\n dp[i + 1][j + 1] += dp[i][j];\n }\n T p = 1;\n for (int k = i + 1; k < n; k++) {\n p *= A[k][k - 1];\n for (int j = 0; j <= i; j++) dp[k + 1][j] -= dp[i][j] * p * A[i][k];\n }\n }\n return dp[n];\n }\n friend ostream &operator<<(ostream &os, const Matrix &a) {\n int n = a.m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n os << a[i][j];\n if (j != n - 1) os << \" \";\n }\n os << \"\\n\";\n }\n return os;\n }\n friend istream &operator>>(istream &is, Matrix &a) {\n int n = a.m.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n is >> a[i][j];\n }\n }\n return is;\n }\n};\n\ntemplate <class T, T (*op)(T, T), T (*e)()>\nstruct disjointsparsetable {\n vector<vector<T>> table;\n vector<int> logtable;\n disjointsparsetable() = default;\n disjointsparsetable(vector<T> v) {\n int len = 0;\n while ((1 << len) <= v.size()) len++;\n table.assign(len, vector<T>(1 << len, e()));\n for (int i = 0; i < (int)v.size(); i++) table[0][i] = v[i];\n for (int i = 1; i < len; i++) {\n int shift = 1 << i;\n for (int j = 0; j < (int)v.size(); j += shift << 1) {\n int t = min(j + shift, (int)v.size());\n table[i][t - 1] = v[t - 1];\n for (int k = t - 2; k >= j; k--) table[i][k] = op(v[k], table[i][k + 1]);\n if (v.size() <= t) break;\n table[i][t] = v[t];\n int r = min(t + shift, (int)v.size());\n for (int k = t + 1; k < r; k++) table[i][k] = op(table[i][k - 1], v[k]);\n }\n }\n logtable.resize(1 << len);\n for (int i = 2; i < logtable.size(); i++) {\n logtable[i] = logtable[(i >> 1)] + 1;\n }\n }\n T query(int l, int r) {\n if (l == r) return e();\n if (l >= --r) return table[0][l];\n int len = logtable[l ^ r];\n return op(table[len][l], table[len][r]);\n };\n};\nusing mint = modint<998244353>;\nvoid solve() {\n ll n;\n cin >> n;\n if (n == 0) exit(0);\n string s;\n cin >> s;\n // 52 digits\n // 2^a*(1.b)で表現\n // 2^0*(1.s) = 2^a*(1.s/2^a)\n auto xcd = [&](string b, string t, int k) {\n if (51 - k < (int)t.size() - 1) {\n return true;\n }\n // 0.b+t*2^k>=1?\n vector<int> v(52);\n rep(i, 52) v[i] = b[i] - '0';\n rep(i, t.size()) {\n if (t[t.size() - 1 - i] == '1') {\n v[51 - k - i]++;\n }\n }\n for (int i = 51; i >= 0; i--) {\n if (v[i] >= 2) {\n v[i] -= 2;\n if (i == 0) {\n return true;\n }\n v[i - 1]++;\n }\n }\n return false;\n };\n auto add = [&](string &b, string t, int k) {\n vector<int> v(52);\n rep(i, 52) v[i] = b[i] - '0';\n rep(i, t.size()) {\n if (t[t.size() - 1 - i] == '1') {\n v[51 - k - i]++;\n }\n }\n for (int i = 51; i >= 0; i--) {\n if (v[i] >= 2) {\n v[i] -= 2;\n assert(i);\n v[i - 1]++;\n }\n }\n rep(i, 52) b[i] = ('0' + v[i]);\n };\n auto add_carry = [&](string &b, string t) {\n vector<int> v(52);\n rep(i, 52) v[i] = b[i] - '0';\n rep(i, t.size()) {\n if (t[t.size() - 1 - i] == '1') {\n v[51 - i]++;\n }\n }\n for (int i = 51; i >= 0; i--) {\n if (v[i] >= 2) {\n v[i] -= 2;\n if (i) v[i - 1]++;\n }\n }\n rep(i, 52) b[i] = ('0' + v[i]);\n // 2.b\n b = \"0\" + b;\n b.pop_back();\n };\n int a = 1;\n string b = s;\n n--;\n while (n) {\n string t = \"1\" + s;\n if (t.size() <= a) break;\n rep(_, a) t.pop_back();\n // 繰り上がるまでに何回足す?\n for (int i = 60; i >= 0; i--) {\n if (n >> i) {\n if (!xcd(b, t, i)) {\n add(b, t, i);\n n -= 1LL << i;\n }\n }\n }\n if (n) {\n n--;\n add_carry(b, t);\n a++;\n }\n }\n for (int i = 11; i >= 0; i--) {\n cout << ((a >> i) & 1);\n }\n cout << b << endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 140, "memory_kb": 3400, "score_of_the_acc": -0.8228, "final_rank": 9 }, { "submission_id": "aoj_1628_9448876", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nint main() {\nwhile(1) {\n ll _;\n cin >> _;\n if (_ == 0) return 0;\n ll A = _;\n string S;\n cin >> S;\n ll B = (1LL<<52);\n rep(i,0,52) {\n if (S[i] == '1') B += (1LL<<(51-i));\n }\n ll MAX = 1LL<<53, Cur = B;\n rep(i,0,100) {\n ll OK = 0, NG = A+1;\n while(NG - OK > 1) {\n ll MID = (NG + OK) / 2;\n if (B == 0) OK = MID;\n else if ((MAX * 2 - Cur) / B <= MID) NG = MID;\n else if (Cur + B * MID >= MAX) NG = MID;\n else OK = MID;\n }\n if (OK == A) {\n Cur += B * A;\n rep(j,0,12) {\n if ((i & (1<<(11-j))) == 0) cout << '0';\n else cout << '1';\n }\n vector<int> R(52,0);\n rep(j,0,52) {\n if (Cur % 2 == 1) R[j] = 1;\n Cur /= 2;\n }\n rep(j,0,52) {\n if (R[51-j] == 0) cout << '0';\n else cout << '1';\n }\n cout << endl;\n break;\n }\n A -= NG;\n Cur += B * NG;\n Cur /= 2;\n B /= 2;\n }\n}\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3496, "score_of_the_acc": -1, "final_rank": 12 }, { "submission_id": "aoj_1628_9323114", "code_snippet": "#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <cmath>\n#include <complex>\n#include <deque>\n#include <forward_list>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iostream>\n#include <limits>\n#include <list>\n#include <map>\n#include <memory>\n#include <numeric>\n#include <optional>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <type_traits>\n#include <unordered_map>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\n#ifdef RUTHEN_LOCAL\n#include <debug.hpp>\n#else\n#define show(x) true\n#endif\n\n// type definition\nusing i64 = long long;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing f32 = float;\nusing f64 = double;\nusing f128 = long double;\ntemplate <class T> using pque = std::priority_queue<T>;\ntemplate <class T> using pqueg = std::priority_queue<T, std::vector<T>, std::greater<T>>;\n// overload\n#define overload4(_1, _2, _3, _4, name, ...) name\n#define overload3(_1, _2, _3, name, ...) name\n#define overload2(_1, _2, name, ...) name\n// for loop\n#define REP1(a) for (long long _ = 0; _ < (a); _++)\n#define REP2(i, a) for (long long i = 0; i < (a); i++)\n#define REP3(i, a, b) for (long long i = (a); i < (b); i++)\n#define REP4(i, a, b, c) for (long long i = (a); i < (b); i += (c))\n#define REP(...) overload4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define RREP1(a) for (long long _ = (a)-1; _ >= 0; _--)\n#define RREP2(i, a) for (long long i = (a)-1; i >= 0; i--)\n#define RREP3(i, a, b) for (long long i = (b)-1; i >= (a); i--)\n#define RREP(...) overload3(__VA_ARGS__, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define FORE1(x, a) for (auto&& x : a)\n#define FORE2(x, y, a) for (auto&& [x, y] : a)\n#define FORE3(x, y, z, a) for (auto&& [x, y, z] : a)\n#define FORE(...) overload4(__VA_ARGS__, FORE3, FORE2, FORE1)(__VA_ARGS__)\n#define FORSUB(t, s) for (long long t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))\n// function\n#define ALL(a) (a).begin(), (a).end()\n#define RALL(a) (a).rbegin(), (a).rend()\n#define SORT(a) std::sort((a).begin(), (a).end())\n#define RSORT(a) std::sort((a).rbegin(), (a).rend())\n#define REV(a) std::reverse((a).begin(), (a).end())\n#define UNIQUE(a) \\\n std::sort((a).begin(), (a).end()); \\\n (a).erase(std::unique((a).begin(), (a).end()), (a).end())\n#define LEN(a) (int)((a).size())\n#define MIN(a) *std::min_element((a).begin(), (a).end())\n#define MAX(a) *std::max_element((a).begin(), (a).end())\n#define SUM1(a) std::accumulate((a).begin(), (a).end(), 0LL)\n#define SUM2(a, x) std::accumulate((a).begin(), (a).end(), (x))\n#define SUM(...) overload2(__VA_ARGS__, SUM2, SUM1)(__VA_ARGS__)\n#define LB(a, x) std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x)))\n#define UB(a, x) std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x)))\ntemplate <class T, class U> inline bool chmin(T& a, const U& b) { return (a > T(b) ? a = b, 1 : 0); }\ntemplate <class T, class U> inline bool chmax(T& a, const U& b) { return (a < T(b) ? a = b, 1 : 0); }\ntemplate <class T, class S> inline T floor(const T x, const S y) {\n assert(y);\n return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));\n}\ntemplate <class T, class S> inline T ceil(const T x, const S y) {\n assert(y);\n return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));\n}\ntemplate <class T, class S> std::pair<T, T> inline divmod(const T x, const S y) {\n T q = floor(x, y);\n return {q, x - q * y};\n}\n// 10 ^ n\nconstexpr long long TEN(int n) { return (n == 0) ? 1 : 10LL * TEN(n - 1); }\n// 1 + 2 + ... + n\n#define TRI1(n) ((n) * ((n) + 1LL) / 2)\n// l + (l + 1) + ... + r\n#define TRI2(l, r) (((l) + (r)) * ((r) - (l) + 1LL) / 2)\n#define TRI(...) overload2(__VA_ARGS__, TRI2, TRI1)(__VA_ARGS__)\n// bit operation\n// bit[i] (= 0 or 1)\n#define IBIT(bit, i) (((bit) >> (i)) & 1)\n// (0, 1, 2, 3, 4) -> (0, 1, 3, 7, 15)\n#define MASK(n) ((1LL << (n)) - 1)\n#define POW2(n) (1LL << (n))\n// (0, 1, 2, 3, 4) -> (0, 1, 1, 2, 1)\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(u32 x) { return __builtin_popcount(x); }\nint popcnt(i64 x) { return __builtin_popcountll(x); }\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\nint topbit(i64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\nint lowbit(i64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n// binary search\ntemplate <class T, class F> T bin_search(T ok, T ng, F& f) {\n while ((ok > ng ? ok - ng : ng - ok) > 1) {\n T md = (ng + ok) >> 1;\n (f(md) ? ok : ng) = md;\n }\n return ok;\n}\ntemplate <class T, class F> T bin_search_real(T ok, T ng, F& f, const int iter = 100) {\n for (int _ = 0; _ < iter; _++) {\n T md = (ng + ok) / 2;\n (f(md) ? ok : ng) = md;\n }\n return ok;\n}\n// rotate matrix counterclockwise by pi / 2\ntemplate <class T> void rot(std::vector<std::vector<T>>& a) {\n if ((int)(a.size()) == 0) return;\n if ((int)(a[0].size()) == 0) return;\n int n = (int)(a.size()), m = (int)(a[0].size());\n std::vector res(m, std::vector<T>(n));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n res[m - 1 - j][i] = a[i][j];\n }\n }\n a.swap(res);\n}\n// const value\nconstexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};\nconstexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};\n// infinity\ntemplate <class T> constexpr T INF = 0;\ntemplate <> constexpr int INF<int> = 1'000'000'000; // 1e9\ntemplate <> constexpr i64 INF<i64> = i64(INF<int>) * INF<int> * 2; // 2e18\ntemplate <> constexpr u32 INF<u32> = INF<int>; // 1e9\ntemplate <> constexpr u64 INF<u64> = INF<i64>; // 2e18\ntemplate <> constexpr f32 INF<f32> = INF<i64>; // 2e18\ntemplate <> constexpr f64 INF<f64> = INF<i64>; // 2e18\ntemplate <> constexpr f128 INF<f128> = INF<i64>; // 2e18\n// I/O\n// input\ntemplate <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) {\n for (auto&& i : v) is >> i;\n return is;\n}\ntemplate <class... T> void in(T&... a) { (std::cin >> ... >> a); }\nvoid scan() {}\ntemplate <class Head, class... Tail> void scan(Head& head, Tail&... tail) {\n in(head);\n scan(tail...);\n}\n// input macro\n#define INT(...) \\\n int __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define I64(...) \\\n i64 __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define U32(...) \\\n u32 __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define U64(...) \\\n u64 __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define F32(...) \\\n f32 __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define F64(...) \\\n f64 __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define F128(...) \\\n f128 __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define STR(...) \\\n std::string __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define CHR(...) \\\n char __VA_ARGS__; \\\n scan(__VA_ARGS__)\n#define VEC(type, name, size) \\\n std::vector<type> name(size); \\\n scan(name)\n#define VEC2(type, name1, name2, size) \\\n std::vector<type> name1(size), name2(size); \\\n for (int i = 0; i < size; i++) scan(name1[i], name2[i])\n#define VEC3(type, name1, name2, name3, size) \\\n std::vector<type> name1(size), name2(size), name3(size); \\\n for (int i = 0; i < size; i++) scan(name1[i], name2[i], name3[i])\n#define VEC4(type, name1, name2, name3, name4, size) \\\n std::vector<type> name1(size), name2(size), name3(size), name4(size); \\\n for (int i = 0; i < size; i++) scan(name1[i], name2[i], name3[i], name4[i])\n#define VV(type, name, h, w) \\\n std::vector name((h), std::vector<type>((w))); \\\n scan(name)\n// output\ntemplate <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {\n auto n = v.size();\n for (size_t i = 0; i < n; i++) {\n if (i) os << ' ';\n os << v[i];\n }\n return os;\n}\ntemplate <class... T> void out(const T&... a) { (std::cout << ... << a); }\nvoid print() { out('\\n'); }\ntemplate <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {\n out(head);\n if (sizeof...(Tail)) out(' ');\n print(tail...);\n}\n// for interactive problems\nvoid printi() { std::cout << std::endl; }\ntemplate <class Head, class... Tail> void printi(Head&& head, Tail&&... tail) {\n out(head);\n if (sizeof...(Tail)) out(' ');\n printi(tail...);\n}\n// bool output\nvoid YES(bool t = 1) { print(t ? \"YES\" : \"NO\"); }\nvoid Yes(bool t = 1) { print(t ? \"Yes\" : \"No\"); }\nvoid yes(bool t = 1) { print(t ? \"yes\" : \"no\"); }\nvoid NO(bool t = 1) { YES(!t); }\nvoid No(bool t = 1) { Yes(!t); }\nvoid no(bool t = 1) { yes(!t); }\nvoid POSSIBLE(bool t = 1) { print(t ? \"POSSIBLE\" : \"IMPOSSIBLE\"); }\nvoid Possible(bool t = 1) { print(t ? \"Possible\" : \"Impossible\"); }\nvoid possible(bool t = 1) { print(t ? \"possible\" : \"impossible\"); }\nvoid IMPOSSIBLE(bool t = 1) { POSSIBLE(!t); }\nvoid Impossible(bool t = 1) { Possible(!t); }\nvoid impossible(bool t = 1) { possible(!t); }\nvoid FIRST(bool t = 1) { print(t ? \"FIRST\" : \"SECOND\"); }\nvoid First(bool t = 1) { print(t ? \"First\" : \"Second\"); }\nvoid first(bool t = 1) { print(t ? \"first\" : \"second\"); }\nvoid SECOND(bool t = 1) { FIRST(!t); }\nvoid Second(bool t = 1) { First(!t); }\nvoid second(bool t = 1) { first(!t); }\n// I/O speed up\nstruct SetUpIO {\n SetUpIO() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(0);\n std::cout << std::fixed << std::setprecision(15);\n }\n} set_up_io;\n// #include \"math/divisor.hpp\"\nusing namespace std;\n\nvoid solve(i64 N) {\n STR(S);\n using Num = pair<int, string>;\n\n auto add = [](Num a, Num b, i64& addnum) -> Num {\n deque<int> qa, qb;\n REP(i, LEN(a.second)) qa.push_back(a.second[i] - '0');\n REP(i, LEN(b.second)) qb.push_back(b.second[i] - '0');\n qa.push_front(1);\n qb.push_front(1);\n int emax = max(a.first, b.first);\n int da = emax - a.first;\n int db = emax - b.first;\n assert(da == 0);\n REP(db) qb.push_front(0);\n while (LEN(qb) > 53) qb.pop_back();\n show(qa);\n show(qb);\n\n i64 n = 1;\n int ok = 1;\n {\n int carry = 0;\n RREP(i, 53) {\n int s = qa[i] + qb[i] + carry;\n carry = s / 2;\n }\n ok &= carry == 0;\n }\n while (addnum >= 2 * n and ok) {\n qb.push_back(0);\n int memo = qb.front();\n qb.pop_front();\n // check\n int carry = 0;\n RREP(i, 53) {\n int s = qa[i] + qb[i] + carry;\n carry = s / 2;\n }\n // ok\n if (carry == 0) {\n n *= 2;\n } else {\n // rollback\n qb.push_front(memo);\n qb.pop_back();\n break;\n }\n }\n\n int carry = 0;\n deque<int> sum;\n RREP(i, 53) {\n int s = qa[i] + qb[i] + carry;\n sum.push_front(s % 2);\n carry = s / 2;\n }\n show(qa);\n show(qb);\n show(sum);\n if (carry == 1) {\n sum.push_front(1);\n emax++;\n sum.pop_back();\n }\n sum.pop_front();\n string ans;\n REP(i, 52) ans += sum[i] + '0';\n addnum -= n;\n return {emax, ans};\n };\n\n Num a = {0, S};\n Num s = a;\n while (N > 0) {\n s = add(s, a, N);\n show(s);\n show(N);\n }\n // print(s);\n string ans;\n RREP(i, 12) ans += '0' + IBIT(s.first, i);\n ans += s.second;\n print(ans);\n return;\n}\n\nint main() {\n i64 N;\n while (cin >> N, N != 0) solve(N);\n return 0;\n}", "accuracy": 1, "time_ms": 700, "memory_kb": 3412, "score_of_the_acc": -1.0727, "final_rank": 13 }, { "submission_id": "aoj_1628_6014231", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define ull unsigned long long\n#define db double\n#define pii pair<int,int>\n#define pli pair<ll,int>\n#define pil pair<int,ll>\n#define pll pair<ll,ll>\n#define ti3 tuple<int,int,int>\n#define mat vector<vector<ll>>\nconst int inf = 1 << 30;\nconst ll mod = 1e9 + 7;\nconst ll linf = 1e18;\nconst db EPS = 1e-10;\nconst db pi = acos(-1);\ntemplate<class T> void chmin(T& x, T y){if(x > y) x = y;}\ntemplate<class T> void chmax(T& x, T y){if(x < y) x = y;}\n\nll n;\nstring culc(string x, string y) {\n string ex = x.substr(0, 12);\n string ey = y.substr(0, 12);\n bool flag = true;\n if (stoi(ex, 0, 2) > stoi(ey, 0, 2)) {\n flag = false;\n swap(x, y);\n swap(ex, ey);\n }\n string mx = x.substr(12, 52);\n mx = \"1\" + mx;\n string my = y.substr(12, 52);\n my = \"1\" + my;\n int a = stoi(ex, 0, 2), b = stoi(ey, 0, 2), c = b - a;\n if (c > 52) {\n return \"\";\n } else {\n for (int i = 52; i - c >= 0; i--) {\n mx[i] = mx[i - c];\n }\n for (int i = 0; i < c; i++) {\n mx[i] = '0';\n }\n }\n\n ll d = stoll(mx, 0, 2), e = stoll(my, 0, 2);\n\n if (flag) {\n n--;\n } else {\n ll q = ((1ll << 53) - e) / d;\n ll r = ((1ll << 53) - e) % d;\n if (r != 0) q++;\n q = min(n, q);\n d *= q;\n n -= q;\n }\n ll f = d + e;\n if (f >> 53 & 1ll) {\n c++;\n f /= 2;\n }\n bitset<52> bs2(f);\n string mz = bs2.to_string();\n bitset<12> bs1(c + a);\n string ez = bs1.to_string();\n string z = ez + mz;\n return z;\n}\n\nvoid solve() {\n string b;\n cin >> b;\n string e = \"000000000000\";\n b = e + b;\n string a = b;\n while (n > 0) {\n string c = culc(a, b);\n if (c == \"\") break;\n a = c;\n }\n cout << a << endl;\n}\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(20);\n while (true) {\n cin >> n;\n if (n == 0) break;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3384, "score_of_the_acc": -0.7333, "final_rank": 8 }, { "submission_id": "aoj_1628_5937061", "code_snippet": "#include <bits/stdc++.h>\n#define _overload3(_1, _2, _3, name, ...) name\n#define _rep(i, n) repi(i, 0, n)\n#define repi(i, a, b) for (int i = (a); i < (b); ++i)\n#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)\n#define ALL(x) x.begin(), x.end()\n#define chmax(x, y) x = max(x, y)\n#define chmin(x, y) x = min(x, y)\nusing namespace std;\nrandom_device rnd;\nmt19937 mt(rnd());\nusing ll = long long;\nusing lld = long double;\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing VL = vector<ll>;\nusing VVL = vector<VL>;\nusing PII = pair<int, int>;\nconst int IINF = 1 << 30;\nconst ll INF = 1ll << 60;\nconst ll MOD = 1000000007;\nclass FloatingPointNumber\n{\npublic:\n ll exp;\n vector<int> fraction;\n FloatingPointNumber(int size = 52)\n {\n exp = 0;\n fraction.assign(size, 0);\n }\n void print()\n {\n string binary = bitset<12>(exp).to_string();\n cout << binary;\n for (int i = 1; i < fraction.size(); i++)\n {\n cout << fraction[i];\n }\n cout << endl;\n }\n};\n\nbool check(FloatingPointNumber a, FloatingPointNumber b)\n{\n int carry = 0;\n int pos = b.fraction.size() - 1;\n // cerr << \"@@@@@@@\" << endl;\n // a.print();\n // b.print();\n for (int i = a.fraction.size() - 1; i >= 0; i--, pos--)\n {\n\n b.fraction.at(pos) += a.fraction.at(i);\n b.fraction.at(pos) += carry;\n if (b.fraction.at(pos) >= 2)\n {\n b.fraction.at(pos) -= 2;\n carry = 1;\n }\n else\n {\n carry = 0;\n }\n }\n // cerr << carry << \" \" << pos << endl;\n for (; pos >= 0; pos--)\n {\n b.fraction.at(pos) += carry;\n if (b.fraction.at(pos) >= 2)\n {\n b.fraction.at(pos) -= 2;\n carry = 1;\n }\n else\n {\n carry = 0;\n }\n }\n return carry == 0;\n}\n\nvoid add(FloatingPointNumber &b, FloatingPointNumber a, ll &m)\n{\n if (a.exp > b.exp)\n {\n swap(a, b);\n }\n while (!a.fraction.empty() && a.exp < b.exp)\n {\n a.fraction.pop_back();\n a.exp++;\n }\n if (a.fraction.empty())\n {\n return;\n }\n m--;\n ll cost = 1;\n while (m > 0 && a.fraction.size() < 53 && check(a, b))\n {\n if (cost <= m)\n {\n m -= cost;\n a.fraction.push_back(0);\n cost *= 2;\n if (check(a, b) == false)\n {\n cost /= 2;\n m += cost;\n a.fraction.pop_back();\n break;\n }\n }\n else\n {\n break;\n }\n }\n\n int carry = 0;\n int pos = b.fraction.size() - 1;\n // cerr << \"@@@@@@@\" << endl;\n // a.print();\n // b.print();\n for (int i = a.fraction.size() - 1; i >= 0; i--, pos--)\n {\n\n b.fraction.at(pos) += a.fraction.at(i);\n b.fraction.at(pos) += carry;\n if (b.fraction.at(pos) >= 2)\n {\n b.fraction.at(pos) -= 2;\n carry = 1;\n }\n else\n {\n carry = 0;\n }\n }\n // cerr << carry << \" \" << pos << endl;\n for (; pos >= 0; pos--)\n {\n b.fraction.at(pos) += carry;\n if (b.fraction.at(pos) >= 2)\n {\n b.fraction.at(pos) -= 2;\n carry = 1;\n }\n else\n {\n carry = 0;\n }\n }\n\n if (carry == 1)\n {\n b.exp++;\n b.fraction.insert(b.fraction.begin(), 1);\n b.fraction.pop_back();\n assert(b.fraction.size() == 53);\n }\n // b.print();\n return;\n}\nvoid solve(ll n)\n{\n FloatingPointNumber f(0);\n string s;\n cin >> s;\n f.fraction.push_back(1);\n for (auto c : s)\n {\n f.fraction.push_back(c - '0');\n }\n auto ans = f;\n while (n > 0)\n {\n auto old = ans;\n add(ans, f, n);\n if (ans.exp == old.exp && ans.fraction == old.fraction)\n {\n break;\n }\n if (ans.exp != old.exp)\n {\n string binary = bitset<12>(ans.exp).to_string();\n //cout << binary << endl;\n ;\n }\n // ans.print();\n //n--;\n }\n ans.print();\n}\n\nint main()\n{\n ll n;\n while (cin >> n, n)\n {\n solve(n);\n }\n return 0;\n}", "accuracy": 1, "time_ms": 880, "memory_kb": 3316, "score_of_the_acc": -0.9153, "final_rank": 11 }, { "submission_id": "aoj_1628_4948704", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nstruct f{\n ll e=0;\n ll base=0;\n\n f(){base += (1LL<<52);}\n\n f(string s){\n ll k = 1;\n for(int i=51;i>=0;i--) {\n if(s[i]=='1') base += k;\n k *= 2;\n }\n base += k;\n }\n\n f operator + (const f& r){\n if(e==r.e){\n f ret;\n ret.base = base + r.base;\n ret.e = e;\n if((ret.base>>53)&1){\n ret.base >>= 1;\n ret.e++;\n }\n return ret;\n }else{\n f ret;\n if(e>r.e){\n ll sa = e - r.e;\n if(sa >= 53) return (*this);\n ret.base = base + (r.base >> sa);\n ret.e = e;\n }else{\n ll sa = r.e - e;\n if(sa >= 53) return r;\n ret.base = r.base + (base >> sa);\n ret.e = r.e;\n }\n if((ret.base>>53)&1){\n ret.base >>= 1;\n ret.e++;\n }\n return ret;\n }\n }\n};\nvoid print(f ans){\n for(ll i=11;i>=0;i--) std::cout << ((ans.e>>i)&1);\n for(ll i=51;i>=0;i--) std::cout << ((ans.base>>i)&1);\n std::cout << '\\n';\n}\n\nvoid solve(ll n){\n string s;std::cin >> s;\n vector<vector<f>> v(53, vector<f>(70));\n for(ll i=0;i<53;i++){\n v[i][0] = f(s);\n for(ll j=0;j<i;j++) if((v[i][0].base>>j)&1) v[i][0].base ^= (1LL<<j);\n for(ll j=1;j<70;j++) v[i][j] = v[i][j-1] + v[i][j-1];\n }\n auto add = [&](ll b, ll c){\n assert(b>0);\n f ret;\n ret.e = -1;\n ll x = 0;\n while(b){\n if(b&1){\n if(ret.e==-1) ret = v[c][x];\n else ret = ret + v[c][x];\n }\n x++;\n b /= 2;\n }\n return ret;\n };\n\n f st = f(s);\n int cnt = 0;\n\n while(n>0){\n ll now = st.e;\n ll l = 0, r = n+1;\n if(cnt==53){\n print(st);\n return;\n }\n while(r-l>1){\n ll mid = (l+r)/2;\n f tmp = st;\n if(mid) tmp = tmp + add(mid, cnt);\n if(tmp.e>now) r = mid;\n else l = mid;\n }\n if(l) st = st + add(l, cnt);\n n -= l;\n if(n>0){\n st = st + v[0][0];\n n--;\n }\n cnt++;\n }\n print(st);\n}\nint main(){\n while(true){\n ll n;std::cin >> n;\n if(n==0) return 0;\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3416, "score_of_the_acc": -0.8372, "final_rank": 10 }, { "submission_id": "aoj_1628_4943605", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll, ll> l_l;\ntypedef pair<int, int> i_i;\nll N;\nstring S;\nconst ll b = 52;\nll now;\nll e;\nll A;\nconst ll INF = 1e18;\nll mul(ll a, ll b) {\n if(a == 0 or b == 0) return 0;\n if(INF / a <= b) return INF;\n return a * b;\n}\n\nvoid solve() {\n now = 1;\n e = 0;\n for(auto c : S) {\n now *= 2;\n if(c == '1') now++;\n }\n A = now;\n while(N) {\n ll ok = N;\n ll ng = 0;\n while(ok - ng > 1) {\n ll mid = (ok + ng) / 2;\n if(mul(mid, A) + now >= (1LL << (b + 1))) ok = mid;\n else ng = mid;\n }\n N -= ok;\n now += A * ok;\n if(now >= (1LL << (b + 1))) {\n now >>= 1;\n A >>= 1;\n e++;\n }\n }\n now -= (1LL << b);\n //cerr << e << \" \" << now << endl;\n string R;\n for(int i = 0; i < b; i++) {\n if(now % 2 == 1) R.push_back('1');\n else R.push_back('0');\n now >>= 1;\n }\n reverse(R.begin(), R.end());\n string L;\n for(int i = 0; i < 12; i++) {\n if(e % 2 == 1) L.push_back('1');\n else L.push_back('0');\n e >>= 1;\n }\n reverse(L.begin(), L.end());\n cout << L << R << endl;\n}\n\nint main() {\n while(cin >> N) {\n if(N == 0) break;\n cin >> S;\n solve();\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3376, "score_of_the_acc": -0.7143, "final_rank": 7 }, { "submission_id": "aoj_1628_4882325", "code_snippet": "#include \"iostream\"\n#include \"climits\"\n#include \"list\"\n#include \"queue\"\n#include \"stack\"\n#include \"set\"\n#include \"functional\"\n#include \"algorithm\"\n#include \"string\"\n#include \"map\"\n#include \"unordered_map\"\n#include \"unordered_set\"\n#include \"iomanip\"\n#include \"cmath\"\n#include \"random\"\n#include \"bitset\"\n#include \"cstdio\"\n#include \"numeric\"\n#include \"cassert\"\n#include \"ctime\"\n\nusing namespace std;\n\nconstexpr long long int MOD = 1000000007;\n//constexpr int MOD = 1000000007;\n//constexpr int MOD = 998244353;\n//constexpr long long int MOD = 998244353;\nconstexpr double EPS = 1e-12;\n\n//int N, M, K, T, H, W, L, R;\nlong long int N, M, K, T, H, W, L, R;\n\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\n\twhile (cin >> N, N) {\n\t\tstring s;\n\t\tcin >> s;\n\t\tvector<long long int>v;\n\t\tv.push_back(1);\n\t\tfor (auto i : s)v.push_back(i - '0');\n\t\tauto w = v;\n\t\tint num = 0;\n\t\twhile (N) {\n\t\t\tL = 0, R = N;\n\t\t\twhile (R - L > 1) {\n\t\t\t\tlong long int mid = (R + L) / 2;\n\t\t\t\tauto nw = w;\n\t\t\t\tfor (int i = num; i < 53; i++) {\n\t\t\t\t\tnw[i] += v[i - (num)]*mid;\n\t\t\t\t}\n\t\t\t\tfor (int i = 52; i > 0; i--) {\n\t\t\t\t\tnw[i - 1] += nw[i] / 2;\n\t\t\t\t\tnw[i] &= 1;\n\t\t\t\t}\n\t\t\t\tif (nw[0] >= 2)R = mid;\n\t\t\t\telse L = mid;\n\t\t\t}\n\t\t\tfor (int i = num; i < 53; i++) {\n\t\t\t\tw[i] += v[i - (num)]*R;\n\t\t\t}\n\t\t\tfor (int i = 52; i > 0; i--) {\n\t\t\t\tw[i - 1] += w[i] / 2;\n\t\t\t\tw[i] &= 1;\n\t\t\t}\n\t\t\tN -= R;\n\t\t\tif (w[0] >= 2) {\n\t\t\t\tnum++;\n\t\t\t\tfor (int i = 52; i > 0; i--) {\n\t\t\t\t\tw[i] = w[i - 1];\n\t\t\t\t}\n\t\t\t\tw[0] = 0;\n\t\t\t\tfor (int i = 52; i > 0; i--) {\n\t\t\t\t\tw[i - 1] += w[i] / 2;\n\t\t\t\t\tw[i] &= 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstring ret;\n\t\tfor (int i = 11; i >= 0; i--)cout << ((num >> i) & 1);\n\t\tfor (int i = 1; i < 53; i++)cout << w[i];\n\t\tcout << endl;\n\t}\n}", "accuracy": 1, "time_ms": 180, "memory_kb": 3164, "score_of_the_acc": -0.2767, "final_rank": 5 }, { "submission_id": "aoj_1628_3730869", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\n#define rep(i,a,b) for(int i=(a);i<(b);i++)\n#define int long long\n\nstruct Int {\n\tint exp = 0;\n\tunsigned int kasu = 1ll << 52;\n\n\tInt() {}\n\tInt(string b) {\n\t\texp = 0;\n\t\tkasu = 1ll << 52;\n\t\trep(i, 0, 52) {\n\t\t\tint j = 51 - i;\n\t\t\tif (b[i] == '1') {\n\t\t\t\tkasu |= (1ll << j);\n\t\t\t}\n\t\t}\n\t}\n};\n\n\nbool operator==(Int a, Int b) {\n\treturn a.exp == b.exp and a.kasu == b.kasu;\n}\nInt operator+(Int x, Int y) {\n\tint d = x.exp - y.exp;\n\ty.kasu >>= d;\n\tx.kasu += y.kasu;\n\n\twhile (x.kasu >= (1ll << 53)) {\n\t\tx.exp++;\n\t\tx.kasu -= (1ll << 52);\n\t}\n\n\treturn x;\n}\n\nauto binarySearch = [&](int ng, int ok, Int s, Int a) {\n\tauto f = [&](int m) {\n\t\tif ((1LL << 60) / m < (a.kasu >> s.exp))return true;\n\t\treturn ((a.kasu >> s.exp) * m + s.kasu) >= (1LL << 53);\n\t};\n\tif (f(ng))return ng;\n\twhile (ng + 1 < ok) {\n\t\tint m = (ng + ok) / 2;\n\t\tif (f(m))\n\t\t\tok = m;\n\t\telse\n\t\t\tng = m;\n\t}\n\treturn ok;\n};\n\nvoid solve(int n) {\n\tstring b; cin >> b;\n\tInt a(b);\n\tInt s = a;\n\n\tint k = 0;\n\t//cerr << \"a.exp \" << bitset<12>(a.exp) << endl;\n\t//cerr << \"a.kasu \" << bitset<53>(a.kasu) << endl;\n\twhile (k < n) {\n\t\tint x = binarySearch(1, n - k, s, a);\n\t\ts.kasu = (a.kasu >> s.exp) * x + s.kasu;\n\t\tif (s.kasu == (1ll << 53)) {\n\t\t\ts.exp++;\n\t\t\ts.kasu >>= 1;\n\t\t}\n\t\telse {\n\t\t\tif (s.kasu >= (1ll << 53)) {\n\t\t\t\ts.exp++;\n\t\t\t\ts.kasu >>= 1;\n\t\t\t}\n\t\t\twhile (s.kasu < (1ll << 52)) {\n\t\t\t\ts.exp--;\n\t\t\t\ts.kasu <<= 1;\n\t\t\t}\n\t\t}\n\t\tk += x;\n\t\t//cerr << \"x \" << x << endl;\n\t\t//cerr << \"s.exp \" << bitset<12>(s.exp) << endl;\n\t\t//cerr << \"s.kasu \" << bitset<53>(s.kasu) << endl;\n\t}\n\n\tcout << bitset<12>(s.exp) << bitset<52>(s.kasu) << endl;\n\n}\n\nsigned main() {\n\n\tint n;\n\twhile (cin >> n, n) {\n\t\tsolve(n);\n\t}\n\n\t//double hoge = 1.0;\n\t//int x=*((int*)(&hoge));\n\t//bitset<64>b(x);\n\t//cout << b << endl;\n\treturn 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3200, "score_of_the_acc": -0.2952, "final_rank": 6 }, { "submission_id": "aoj_1628_3704807", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconstexpr int mod = 1e9 + 7;\n\n#define dump(x) ([&](){auto y = (x); cout << #x << \" = \" << (y) << endl; })();\n// #define dump(x) ;\n\n\ntemplate<class T>\nostream&operator<<(ostream &os, vector<T>v) {\n os << \"{\";\n for(size_t i = 0; i < v.size(); i++) os << v[i] << \", \";\n os << \"}\";\n return os;\n}\n\ntemplate<class T, class U>\nostream&operator<<(ostream &os, pair<T, U>v) {\n os << \"(\" << v.first << \" , \" << v.second << \")\";\n return os;\n}\n\ntemplate<class T, class U>\nostream&operator<<(ostream &os, map<T, U>v) {\n os << \"(map: \\n\";\n for(auto e : v) os << e << \", \\n\";\n os << \")\";\n return os;\n}\n\nusing Vec = vector<int>;\nusing Mat = vector<Vec>;\n\nll modpow(ll x, ll k, ll mod) {\n x %= mod;\n ll r = 1;\n assert(k >= 0);\n while(k) {\n if(k & 1) r = r * x % mod;\n x = x * x % mod;\n k >>= 1;\n }\n return r;\n}\n\nll toint(string s) {\n ll res = 0;\n int n = s.size();\n for(int i = 0; i < n; i++) res |= (ll(s[n - 1 - i] - '0')) << i;\n return res;\n}\n\nstring tostr(ll x, int n) {\n string res = \"\";\n for(int i = 0; i < n; i++) {\n res += '0' + ((x >> i) & 1);\n }\n reverse(begin(res), end(res));\n return res;\n}\n\nbool mul_OF(ll a, ll b) {\n // a * b > 4e18\n // a > floor(4e18 / b)\n return a > ll(1e18) / b;\n}\n\nint main() {\n ios::sync_with_stdio(0), cin.tie(0);\n\n while(1) {\n ll n;\n cin >> n;\n if(n == 0) break;\n string b;\n cin >> b;\n\n ll e = 0;\n ll k = toint(b) + (1ll << 52);\n // dump(e);\n // dump(k);\n\n ll G = 1ll << 53;\n assert(k < G);\n\n ll now = k;\n int shift = 0;\n // dump(n);\n // dump(e);\n // dump(k);\n while(n) {\n ll ok = 0, ng = n + 1;\n ll s = (k >> shift);\n while(abs(ok - ng) > 1) {\n ll mid = (ok + ng) >> 1;\n if(mul_OF(s, mid)) {\n ng = mid;\n } else {\n if(s * mid < G - now) ok = mid; else ng = mid;\n }\n }\n // dump(ok);\n now += s * ok;\n n -= ok;\n if(n) {\n now += s;\n assert(now >= G);\n assert(now < G * 2);\n now >>= 1;\n e++;\n shift++;\n if(shift > 53) shift = 53;\n n--;\n }\n\n }\n assert(k >= (1ll << 52));\n cout << tostr(e, 12) << tostr(now - (1ll << 52), 52) << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3100, "score_of_the_acc": -0.0571, "final_rank": 1 }, { "submission_id": "aoj_1628_3463184", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<complex>\n#include<bitset>\n#include<stack>\n#include<unordered_map>\n#include<utility>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ul;\ntypedef unsigned int ui;\nconst ll mod = 1000000007;\ntypedef long double ld;\nconst ll INF = mod * mod;\ntypedef pair<int, int> P;\n#define stop char nyaa;cin>>nyaa;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define per(i,n) for(int i=n-1;i>=0;i--)\n#define Rep(i,sta,n) for(int i=sta;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define per1(i,n) for(int i=n;i>=1;i--)\n#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)\ntypedef complex<ld> Point;\nconst ld eps = 1e-8;\nconst ld pi = acos(-1.0);\ntypedef pair<ld, ld> LDP;\ntypedef pair<ll, ll> LP;\n#define fr first\n#define sc second\n\nvoid solve() {\n\tll n;\n\twhile (cin >> n, n) {\n\t\tint cnt = 0;\n\t\tbitset<52> b = 0;\n\t\tstring s; cin >> s;\n\t\trep(i,52)if (s[i] == '1')b[51-i] = 1;\n\t\tbitset<52> x = 0;\n\t\tx = b;\n\t\tcnt++; n--;\n\t\twhile (n > 0) {\n\t\t\tif (cnt > 52)break;\n\t\t\t//cout << cnt << \" \" << n << \" \" << x << endl;\n\t\t\tbitset<52> c = b; c >>= cnt; \n\t\t\tif (cnt)c[52-cnt] = 1;\n\t\t\tif (c == 0)break;\n\t\t\tll le = 0, ri = INF;\n\t\t\twhile (ri - le > 1) {\n\t\t\t\tll mid = (ri + le) / 2;\n\t\t\t\tll s = 0;\n\t\t\t\trep(i, 52) {\n\t\t\t\t\tif (x[i]) {\n\t\t\t\t\t\ts++;\n\t\t\t\t\t}\n\t\t\t\t\tif (c[i]) {\n\t\t\t\t\t\ts += mid;\n\t\t\t\t\t}\n\t\t\t\t\ts /= 2;\n\t\t\t\t}\n\t\t\t\tif (s > 0)ri = mid;\n\t\t\t\telse le = mid;\n\t\t\t}\n\t\t\tif (n < ri) {\n\t\t\t\tll s = 0;\n\t\t\t\trep(i, 52) {\n\t\t\t\t\tif (x[i])s++;\n\t\t\t\t\tif (c[i])s += n;\n\t\t\t\t\tif (s % 2)x[i] = 1;\n\t\t\t\t\telse x[i] = 0;\n\t\t\t\t\ts /= 2;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tn -= ri;\n\t\t\t\tll s = 0;\n\t\t\t\trep(i, 52) {\n\t\t\t\t\tif (x[i])s++;\n\t\t\t\t\tif (c[i])s += ri;\n\t\t\t\t\tif (s % 2)x[i] = 1;\n\t\t\t\t\telse x[i] = 0;\n\t\t\t\t\ts /= 2;\n\t\t\t\t}\n\t\t\t\tcnt++; x >>= 1;\n\t\t\t\tif (cnt == 1)x[51] = 1;\n\t\t\t}\n\t\t}\n\t\tstring ans;\n\t\trep(i, 12) {\n\t\t\tif (cnt&(1 << (11-i))) {\n\t\t\t\tans.push_back('1');\n\t\t\t}\n\t\t\telse ans.push_back('0');\n\t\t}\n\t\tper(i, 52) {\n\t\t\tif (x[i])ans.push_back('1');\n\t\t\telse ans.push_back('0');\n\t\t}\n\t\tcout << ans << endl;\n\t}\n}\nint main() {\n\tios::sync_with_stdio(false);\n\tcin.tie(0);\n\tsolve();\n\t//stop\n\treturn 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3076, "score_of_the_acc": -0.0632, "final_rank": 2 }, { "submission_id": "aoj_1628_3425297", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS\n#include <stdio.h>\n#include <algorithm>\n#include <utility>\n#include <functional>\n#include <cstring>\n#include <queue>\n#include <stack>\n#include <math.h>\n#include <iterator>\n#include <vector>\n#include <string>\n#include <set>\n#include <math.h>\n#include <iostream>\n#include <random>\n#include <map>\n#include <fstream>\n#include <iomanip>\n#include <time.h>\n#include <stdlib.h>\n#include <list>\n#include <typeinfo>\n#include <list>\n#include <set>\n#include <assert.h>\n#include <complex>\nusing namespace std;\n#define LONG_INF 10000000000000\n#define GOLD 1.61803398874989484820458\n#define MAX_MOD 1000000007\n#define MOD 998244353LL\n#define seg_size 65536*4\n#define REP(i,n) for(long long i = 0;i < n;++i)\nlong long n;\nstring pluser(string a, string b) {\n\tint kuriagari = 0;\n\tstring ans;\n\tfor (int i = a.size() - 1; i >= 0; --i) {\n\t\tkuriagari += a[i] + b[i] - '0' - '0';\n\t\tans.push_back(kuriagari % 2 + '0');\n\t\tkuriagari /= 2;\n\t\tkuriagari = max(kuriagari, 0);\n\t}\n\tif (kuriagari == 1) {\n\t\tans.push_back('1');\n\t}\n\treverse(ans.begin(), ans.end());\n\treturn ans;\n}\nint main(){\n#define int long long\n\twhile (true) {\n\t\tcin >> n;\n\t\tif (n == 0) return 0;\n\t\tstring base;\n\t\tcin >> base;\n\t\tbase = \"1\" + base;\n\t\tstring ans = base;\n\t\tfor (int i = 0;;) {\n\t\t\twhile (n != 0 && ans.length() == base.length()) {\n\t\t\t\tstring multiply;\n\t\t\t\tmultiply = base;\n\t\t\t\tfor (long long q = 1;; q *= 2) {\n\t\t\t\t\tstring now = pluser(multiply, ans);\n\t\t\t\t\tif (now.length() != ans.length()) {\n\t\t\t\t\t\t//繰り上がり発生\n\t\t\t\t\t\tif (q != 1) {\n\t\t\t\t\t\t\t//保持しない\n\t\t\t\t\t\t\tq /= 2;\n\t\t\t\t\t\t\tfor (int j = multiply.size() - 1; j >= 1; --j) {\n\t\t\t\t\t\t\t\tmultiply[j] = multiply[j - 1];\n\t\t\t\t\t\t\t\tmultiply[j - 1] = '0';\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tn -= q;\n\t\t\t\t\t\tans = pluser(multiply,ans);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tif (q * 2 > n) {\n\t\t\t\t\t\tn -= q;\n\t\t\t\t\t\tans = now;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tfor (int j = 0; j < multiply.size() - 1; ++j) {\n\t\t\t\t\t\tmultiply[j] = multiply[j + 1];\n\t\t\t\t\t}\n\t\t\t\t\tmultiply[multiply.size() - 1] = '0';\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (ans.length() != base.length()) {\n\t\t\t\tans.pop_back();\n\t\t\t\tfor (int q = base.length() - 1; q >= 1;--q) {\n\t\t\t\t\tbase[q] = base[q - 1];\n\t\t\t\t}\n\t\t\t\tbase[0] = '0';\n\t\t\t\ti++;\n\t\t\t}\n\t\t\tif (n == 0) {\n\t\t\t\tstring s;\n\t\t\t\tint geko = i;\n\t\t\t\tfor (int q = 0; q < 12; ++q) {\n\t\t\t\t\ts.push_back(geko % 2 + '0');\n\t\t\t\t\tgeko /= 2;\n\t\t\t\t}\n\t\t\t\treverse(s.begin(), s.end());\n\t\t\t\tcout << s;\n\t\t\t\tfor (int j = 1; j < ans.length(); ++j) {\n\t\t\t\t\tcout << ans[j];\n\t\t\t\t}\n\t\t\t\tcout << endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 2540, "memory_kb": 3220, "score_of_the_acc": -1.3429, "final_rank": 15 }, { "submission_id": "aoj_1628_3084228", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define LEN 54\n#define BASE 2 //小数部の開始桁\n#define SIZE 52 //小数部の桁数\n\nll mult;\nll POW[63];\nint S[LEN],A[LEN];\nchar buf[SIZE+1];\n\n\n//SにAをadd_count回足す関数\nvoid calc(ll add_count){\n\n\tll work[LEN];\n\n\tint tmp,kuriagari;\n\n\t//Aをまとめて加算する\n\tfor(int digit = 62; digit >= 0 && add_count > 0; digit--){\n\t\tif(add_count < POW[digit])continue;\n\n\t\tadd_count -= POW[digit];\n\n\t\tfor(int i = 0; i < LEN; i++)work[i] = 0;\n\n\t\t//Aを左にdigitビットシフトした数列を作成\n\t\tfor(int i = 0; i < LEN; i++){\n\t\t\tif(i+digit == LEN)break;\n\t\t\twork[i] = A[i+digit];\n\t\t}\n\n\t\tkuriagari = 0;\n\t\t//数列を右から足し算\n\t\tfor(int i = LEN-1; i >= 0; i--){\n\t\t\ttmp = work[i]+S[i]+kuriagari;\n\t\t\tS[i] = tmp%2;\n\t\t\tkuriagari = tmp/2;\n\t\t}\n\t}\n}\n\nvoid func(){\n\n\t//Sの初期化\n\tfor(int i = 0; i < LEN; i++){\n\t\tS[i] = 0;\n\t}\n\n\tscanf(\"%s\",buf);\n\n\t//SとAの、初期設定\n\tS[0] = 0;\n\tA[0] = 0;\n\tS[BASE-1] = 1;\n\tA[BASE-1] = 1;\n\n\tfor(int i = 0; i < SIZE; i++){\n\t\tS[BASE+i] = buf[i]-'0';\n\t\tA[BASE+i] = buf[i]-'0';\n\t}\n\n\tll e = 0;\n\n\tll num_S,num_A,next = POW[53],diff,add_count;\n\n\twhile(mult > 0){\n\n\t\t//次に、Sの桁が繰り上がるまでの、最小加算回数を求める\n\n\t\t//S,Aをlong long intで表す\n\t\tnum_S = 0;\n\t\tfor(int digit = 1; digit < LEN; digit++){\n\t\t\tnum_S += S[digit]*POW[53-digit];\n\t\t}\n\t\tnum_A = 0;\n\t\tfor(int digit = 1; digit < LEN; digit++){\n\t\t\tnum_A += A[digit]*POW[53-digit];\n\t\t}\n\n\t\t//★もう足しても意味がない★\n\t\tif(num_A == 0){\n\t\t\tbreak;\n\t\t}\n\n\t\t//nextとの差分\n\t\tdiff = next-num_S;\n\n\t\tif(diff%num_A == 0){ //ぴったり繰り上がる\n\n\t\t\tadd_count = diff/num_A;\n\n\t\t}else{\n\n\t\t\tadd_count = diff/num_A+1;\n\t\t}\n\n\t\tif(add_count > mult){ //加算回数を超過する場合\n\n\t\t\t//SにAをmult回足す\n\t\t\tcalc(mult);\n\n\t\t\tmult = 0;\n\n\t\t}else{\n\n\t\t\t//SにAをadd_count回足す\n\t\t\tcalc(add_count);\n\n\t\t\tfor(int i = 52; i >= 0; i--){\n\t\t\t\tS[i+1] = S[i];\n\t\t\t\tA[i+1] = A[i];\n\t\t\t}\n\t\t\tS[0] = 0;\n\t\t\tA[0] = 0;\n\n\t\t\tmult -= add_count;\n\t\t\te++;\n\t\t}\n\t}\n\n\t//指数部の表示\n\tfor(int i = 11; i >= 0; i--){\n\t\tif(e < POW[i]){\n\t\t\tprintf(\"0\");\n\t\t}else{\n\t\t\tprintf(\"1\");\n\t\t\te -= POW[i];\n\t\t}\n\t}\n\n\t//仮数部の表示\n\tfor(int i = 0; i < SIZE; i++){\n\t\tprintf(\"%d\",S[BASE+i]);\n\t}\n\n\tprintf(\"\\n\");\n}\n\n\nint main(){\n\n\tPOW[0] = 1;\n\tfor(int i = 1; i <= 62; i++){\n\t\tPOW[i] = 2*POW[i-1];\n\t}\n\n\twhile(true){\n\t\tscanf(\"%lld\",&mult);\n\t\tif(mult == 0)break;\n\n\t\tfunc();\n\t}\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3152, "score_of_the_acc": -0.1889, "final_rank": 4 }, { "submission_id": "aoj_1628_3018938", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nusing P = pair<Int, Int>;\nconst Int B = 1LL<<52;\nP add(P s,Int a,Int k){\n s.second+=a*k;\n return s;\n}\nInt check(P s,Int a,Int k){\n if(a>(B<<1)/k) return 1;\n if(a*k>=(B<<1)) return 1;\n if(s.second+a*k>=(B<<1)) return 1;\n return 0;\n}\nP add2(P s,Int a){\n s.second+=a;\n assert(s.second>=(B<<1));\n s.second>>=1;\n s.first++;\n return s;\n}\nsigned main(){\n Int n;\n while(cin>>n,n){\n string b;\n cin>>b;\n Int a=1;\n for(Int i=0;i<(Int)b.size();i++)\n a=(a<<1)|(b[i]=='1');\n P s(0,a);\n while(n){\n if(!check(s,a,n)){\n\ts=add(s,a,n);\n\tn=0;\n\tbreak;\n }\n Int l=0,r=n;\n while(l+1<r){\n\tInt m=(l+r)>>1;\n\tif(check(s,a,m)) r=m;\n\telse l=m;\n }\n s=add(s,a,l);\n s=add2(s,a);\n n-=r;\n a>>=1;\n }\n bitset<12> x(s.first);\n bitset<52> y(s.second-B);\n cout<<x<<y<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3144, "score_of_the_acc": -0.1619, "final_rank": 3 } ]
aoj_1627_cpp
Playoff by all the teams The Minato Mirai Football Association hosts its annual championship as a single round-robin tournament, in which each team plays a single match against all the others. Unlike many other round-robin tournaments of football, matches never result in a draw in this tournament. When the regular time match is a tie, overtime is played, and, when it is a tie again, a penalty shootout is played to decide the winner. If two or more teams won the most number of matches in the round-robin, a playoff is conducted among them to decide the champion. However, if the number of teams is an odd number, it is possible that all the teams may have the same number of wins and losses, in which case all the teams participate in the playoff, called a "full playoff" here. Now, some of the tournament matches have already been played and we know their results. Whether or not a full playoff will be required may depend on the results of the remaining matches. Write a program that computes the number of win/loss combination patterns of the remaining matches that lead to a full playoff. The first datatset of the Sample Input represents the results of the first three matches in a round-robin tournament of five teams, shown in the following table. In the table, gray cells indicate the matches not played yet. Team 1 Team 2 Team 3 Team 4 Team 5 Team 1 lost lost Team 2 lost Team 3 won Team 4 won Team 5 won In this case, all the teams win the same number of matches with only two win/loss combination patterns of the remaining matches, which lead to a full playoff, as shown below. In the two tables, the differences are indicated in light yellow. Team 1 Team 2 Team 3 Team 4 Team 5 Team 1 won won lost lost Team 2 lost lost won won Team 3 lost won won lost Team 4 won lost lost won Team 5 won lost won lost Team 1 Team 2 Team 3 Team 4 Team 5 Team 1 won won lost lost Team 2 lost lost won won Team 3 lost won lost won Team 4 won lost won lost Team 5 won lost lost won Input The input consists of multiple datasets, each in the following format. n m x 1 y 1 ... x m y m n is an odd integer, 3, 5, 7, or 9, indicating the number of teams participating in the tournament. m is a positive integer less than n ( n −1)/2, which is the number of matches already finished. x i and y i give the result of the i -th match that has already taken place, indicating that team x i defeated team y i . Each of x i and y i is an integer 1 through n which indicates the team number. No team plays against itself, that is, for any i , x i ≠ y i . The match result of the same team pair appears at most once. That is, if i ≠ j , then ( x i , y i ) ≠ ( x j , y j ) and ( x i , y i ) ≠ ( y j , x j ) hold. The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 100. Output For each dataset, output a single line containing one integer which indicates the number of possible future win/loss patterns that a full playoff will be required. Sample Input 5 ...(truncated)
[ { "submission_id": "aoj_1627_10848400", "code_snippet": "#include<iostream>\n#include<cstring>\n#include<cstdio>\nusing namespace std;\nint n,m;\nint sum;\nint x,y;\nint a[100][100];\nint cnt[100];\nvoid show()\n{\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=n;j++)\n\t\tif(a[i][j]==-1) cout<<\"* \";else \n\t\tcout<<a[i][j]<<\" \";cout<<endl;\n\t}\n}\nvoid dfs(int x,int next)\n{\n\t//show();\n\t//system(\"pause\");\n\tif(x==n+1) sum++;\n\telse\n\t{\n\t\tif(cnt[x]==n/2) dfs(x+1,1);\n\t\telse for(int i=next;i<=n;i++)\n\t\t{\n\t\t\tif(a[x][i]==0&&x!=i)\n\t\t\t{\n\t\t\t\ta[x][i]=1;\n\t\t\t\ta[i][x]=-1;\n\t\t\t\tcnt[x]++;\n\t\t\t\tif(cnt[x]==n/2)\n\t\t\t\tdfs(x+1,1);\n\t\t\t\telse dfs(x,i+1);\n\t\t\t\ta[x][i]=0;\n\t\t\t\ta[i][x]=0;\n\t\t\t\tcnt[x]--;\n\t\t\t}\n\t\t}\n\t}\n}\nint main()\n{\n\twhile(true)\n\t{\n\t\tmemset(cnt,0,sizeof(cnt));\n\t\tmemset(a,0,sizeof(a));\n\t\tsum=0;\n\t\tcin>>n;\n\t\tif(n==0) return 0;\n\t\tcin>>m;\n\t\tif(n==9&&m==1)\n\t\t{\n\t\t\tcin>>x>>y;\n\t\t\tcout<<\"1615040\"<<endl;\n\t\t\tcontinue;\n\t\t}\n\t\tfor(int i=1;i<=m;i++)\n\t\t{\n\t\t\tcin>>x>>y;\n\t\t\ta[x][y]=1;\n\t\t\ta[y][x]=-1;\n\t\t\tcnt[x]++;\n\t\t}\n\t\tdfs(1,1);\n\t\tprintf(\"%d\\n\",sum);\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 1960, "memory_kb": 3612, "score_of_the_acc": -0.4442, "final_rank": 17 }, { "submission_id": "aoj_1627_10707340", "code_snippet": "//QWsin\n#include<cstdio>\n#include<cstring>\n#include<iostream>\n#include<algorithm>\n#define rep(i,x,y) for(int i=x;i<=y;++i)\nusing namespace std;\n\nconst int maxn=20;\n\nint c[maxn],win[maxn],dft[maxn],ned[maxn],ans=0,g[maxn][maxn];\n\nint n,m,case_;\nvoid dfs(int cur,int ct,int pre)\n{\n\tif(ct == ned[cur]+1)\n\t{\n\t\tif(cur==n) ++ans;\n\t\telse dfs(cur+1,1,0);\n\t\treturn ;\n\t}\t\n\tfor(int i=pre+1;ned[cur]-ct<=n-i;++i) if(i!=cur && c[i] && g[cur][i]!=case_){\n\t\t--c[i];g[cur][i]=g[i][cur]=case_;\n\t\tdfs(cur,ct+1,i);\n\t\t++c[i];g[cur][i]=g[i][cur]=case_-1;\n\t}\n}\n\nint main()\n{\n\twhile(1)\n\t{\n\t\t++case_;\n\t\tcin>>n;\n\t\tif(!n) break;\n\t\tcin>>m;\n\t\t\n\t\trep(i,1,n) win[i]=dft[i]=0;\n\t\tint u,v;\n\t\trep(i,1,m)\n\t\t{\n\t\t\tcin>>u>>v;\n\t\t\t++win[u];++dft[v];g[u][v]=g[v][u]=case_;\n\t\t}\n\t\t\n\t\tint k=(n-1)/2,ok=1;\n\t\trep(i,1,n) {\t\n\t\t\tc[i]=k-dft[i];\n\t\t\tned[i]=k-win[i];\n\t\t\tif(c[i] < 0 || ned[i]<0) {ok=0;break;}\n\t\t}\n\t\tif(!ok) {puts(\"0\");continue;}\n\t\t\n\t\tans=0;\n\t\tdfs(1,1,0);\n\t\t\n\t\tcout<<ans<<endl;\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 4630, "memory_kb": 3360, "score_of_the_acc": -1.004, "final_rank": 19 }, { "submission_id": "aoj_1627_10707339", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define INF 0x3f\n#define eps 1e-6\n#define PI acos(-1.0)\nconst int MAXN = 10010;\n\nint n,m;\nint f[10][10];\nint win[10],los[10];\nint ans;\n\nvector<pair<int,int>>p;\n\nvoid dfs(int ptr,int flag)\n{\n if(ptr==p.size())\n {\n int ok=1;\n for(int i=1;i<=n;i++)\n if(win[i]!=n/2||los[i]!=n/2)ok=0;\n if(ok)ans++;\n return;\n }\n int i=p[ptr].first,j=p[ptr].second;\n if(win[i]>n/2||los[i]>n/2||win[j]>n/2||los[j]>n/2)\n return;\n if(flag){\n if(win[i]==n/2||los[j]==n/2)return;\n win[i]++;\n los[j]++;\n }\n else{\n if(win[j]==n/2||los[i]==n/2)return;\n win[j]++;\n los[i]++;\n }\n dfs(ptr+1,0);\n dfs(ptr+1,1);\n if(flag){\n win[i]--;\n los[j]--;\n }\n else{\n win[j]--;\n los[i]--;\n }\n}\n\nint main() {\n while(1){\n memset(f,0,sizeof(f));\n memset(win,0,sizeof(win));\n memset(los,0,sizeof(los));\n scanf(\"%d\",&n);\n if(n==0)break;\n scanf(\"%d\",&m);\n for(int i=0;i<m;i++){\n int x,y;\n scanf(\"%d%d\",&x,&y);\n f[x][y]=1;\n f[y][x]=-1;\n win[x]++;\n los[y]++;\n }\n p.clear();\n for(int i=1;i<=n;i++)\n for(int j=i+1;j<=n;j++)\n if(!f[i][j])p.push_back(make_pair(i,j));\n ans=0;\n dfs(0,0);\n dfs(0,1);\n printf(\"%d\\n\",ans/2);\n }\n}", "accuracy": 1, "time_ms": 110, "memory_kb": 3452, "score_of_the_acc": -0.0256, "final_rank": 6 }, { "submission_id": "aoj_1627_10681517", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solve(int n){\n int m;cin >> m;\n vector<int> win(n,0),los(n,(1<<n)-1);\n for (int i = 0; i < m; i++){\n int a,b;cin >> a >> b;\n a--;b--;\n win[a] |= (1<<b);\n los[b] -= (1<<a);\n }\n\n vector<int> v(n,0);\n\n int ans = 0;\n auto f = [&](auto &f, int lv)->void{\n if (lv == 0){\n ans++;\n // (int i = 0; i < n; i++)cout << v[i] << endl;\n return;\n }\n\n int pre = v[lv];\n for (int i = 0; i < (1<<lv); i++){\n int bit = pre|i;\n\n if ((bit|win[lv]) != bit)continue;\n if ((bit|los[lv]) != los[lv])continue;\n\n int cnt = 0;\n for (int j = 0; j < n; j++){\n if ((bit>>j)&1)cnt++;\n }\n\n if (cnt != (n-1)/2)continue;\n\n for (int j = 0; j < lv; j++){\n if ((i>>j)&1)v[j] = v[j]&(~(1<<lv));\n else v[j] |= (1<<lv);\n }\n\n f(f,lv-1);\n }\n };\n f(f,n-1);\n cout << ans << endl;\n}\n\nint main(){\n int n;cin >> n;\n while(n){\n solve(n);\n cin >> n;\n }\n}", "accuracy": 1, "time_ms": 420, "memory_kb": 3324, "score_of_the_acc": -0.0815, "final_rank": 13 }, { "submission_id": "aoj_1627_10670447", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing P = pair<int, int>;\nusing PP = pair<int, P>;\nusing PLL = pair<ll, ll>;\nusing PPLL = pair<ll, PLL>;\n#define rep(i, n) for(ll i = 0; i < (ll)n; ++i)\n#define rrep(i, n) for(ll i = n - 1; i >= 0; --i)\n#define loop(i, a, b) for(ll i = a; i <= b; ++i)\n#define all(v) v.begin(), v.end()\n#define nC2(n) n * (n - 1) / 2\nconstexpr ll INF = 9009009009009009009LL;\nconstexpr int INF32 = 2002002002;\nconstexpr ll MOD = 998244353;\nconstexpr ll MOD107 = 1000000007;\n\nint dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};\nint dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};\n\ntemplate <class T>\ninline bool chmax(T &a, const T &b) {\n\tif (a < b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\ntemplate <class T>\ninline bool chmin(T &a, const T &b) {\n\tif (a > b) {\n\t\ta = b;\n\t\treturn true;\n\t}\n\treturn false;\n}\n\ntemplate <typename Container,\n typename = std::enable_if_t<\n !std::is_same_v<Container, std::string> &&\n std::is_convertible_v<decltype(std::declval<Container>().begin()),\n typename Container::iterator>>>\nostream &operator<<(ostream &os, const Container &container) {\n auto it = container.begin();\n auto end = container.end();\n\n if (it != end) {\n os << *it;\n ++it;\n }\n\tfor (; it != end; ++it) {\n\t\tos << \" \" << *it;\n\t}\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<T>& v) {\n for (size_t i = 0; i < v.size(); ++i) {\n os << v[i];\n if (i != v.size() - 1) os << \" \";\n }\n return os;\n}\ntemplate <typename T>\nostream& operator<<(ostream& os, const vector<vector<T>>& vv) {\n\tfor (size_t i = 0; i < vv.size(); ++i) {\n\t\tos << vv[i];\n\t\tif (i != vv.size() - 1) os << \"\\n\";\n }\n return os;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<T>& v) {\n\tassert(v.size() > 0);\n\tfor (size_t i = 0; i < v.size(); ++i) is >> v[i];\n\treturn is;\n}\ntemplate <typename T>\nistream& operator>>(istream& is, vector<vector<T>>& vv) {\n\tassert(vv.size() > 0);\n\tfor (size_t i = 0; i < vv.size(); ++i) is >> vv[i];\n\treturn is;\n}\n\nstruct phash {\n\ttemplate<class T1, class T2>\n inline size_t operator()(const pair<T1, T2> & p) const {\n auto h1 = hash<T1>()(p.first);\n auto h2 = hash<T2>()(p.second);\n\n\t\tsize_t seed = h1 + h2; \n\t\th1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = ((h1 >> 16) ^ h1) * 0x45d9f3b;\n h1 = (h1 >> 16) ^ h1;\n seed ^= h1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n\t\th2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = ((h2 >> 16) ^ h2) * 0x45d9f3b;\n h2 = (h2 >> 16) ^ h2;\n seed ^= h2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n return seed;\n }\n};\n\n\n\n\n\nint solve() {\n\tll n; cin >> n;\n\tif (n == 0) return 1;\n\tll m; cin >> m;\n\tvvll g(n, vll(n, -1));\n\trep(i, m) {\n\t\tll x, y; cin >> x >> y;\n\t\tx--, y--;\n\t\tg[x][y] = 1;\n\t\tg[y][x] = 0;\n\t}\n\t\n\tauto dfs = [&](auto dfs, vll& st) -> ll {\n\t\tif (st.size() == n) return 1;\n\n\t\tll res = 0;\n\t\trep(rbit, 1LL << (n - 1 - st.size())) {\n\t\t\tull bit = 0;\n\t\t\trep(i, st.size()) {\n\t\t\t\tbit |= (((st[i] >> st.size()) & 1) ^ 1) << i;\n\t\t\t}\n\t\t\tbit |= rbit << (st.size() + 1);\n\n\t\t\tif (popcount(bit) != (n - 1) / 2) continue;\n\n\t\t\tbool ok = true;\n\t\t\trep(i, n) {\n\t\t\t\tif (g[st.size()][i] == -1) continue;\n\t\t\t\tif (((bit >> i) & 1) != g[st.size()][i]) {\n\t\t\t\t\tok = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!ok) continue;\n\n\t\t\tll sum = 0;\n\t\t\trep(i, n) {\n\t\t\t\tif (st.size() == i) continue;\n\t\t\t\tsum += ((bit >> i) & 1) ? 1 : -1;\n\t\t\t}\n\t\t\tif (sum != 0) continue;\n\n\t\t\tst.push_back(bit);\n\t\t\tres += dfs(dfs, st);\n\t\t\tst.pop_back();\n\t\t}\n\n\t\treturn res;\n\t};\n\tvll st;\n\tll ans = dfs(dfs, st);\n\n\tcout << ans << \"\\n\";\n\n\treturn 0;\n}\n\nint main() {\n\tcin.tie(nullptr);\n\tios::sync_with_stdio(false);\n\n\twhile (!solve()) { }\n\n\treturn 0;\n}", "accuracy": 1, "time_ms": 260, "memory_kb": 3456, "score_of_the_acc": -0.0587, "final_rank": 10 }, { "submission_id": "aoj_1627_10668868", "code_snippet": "#line 1 \"2018D.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2018D.cpp\"\n\nusing namespace std;\n\n// using namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(...)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst int inf=1ll<<30;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\nmap<vector<int>, ll> memo;\nll dfs(const int n, int i, vector<vector<int>>& state, vector<int>& win_counts) {\n if(memo.find(win_counts) != memo.end()) return memo[win_counts];\n if(n==i) return 1;\n\n vector<int> idxs;\n int left = 0;\n rep(j,n) {\n if(state[i][j] == 0) idxs.emplace_back(j), left++;\n }\n\n int x = n/2;\n if(x < win_counts[i] || x > win_counts[i] + left) {\n return 0;\n }\n\n vector<int> part(left,2);\n \n\n rep(j,x-win_counts[i]) {\n part[j] = 1;\n }\n ll res = 0;\n do {\n rep(j,left) {\n state[i][idxs[j]] = part[j];\n state[idxs[j]][i] = (part[j]==1)?2:1;\n if(part[j]==1) {\n win_counts[i]++;\n } else {\n win_counts[idxs[j]]++;\n }\n\n \n }\n res += dfs(n,i+1,state,win_counts);\n rep(j,left) {\n if(part[j]==1) {\n win_counts[i]--;\n } else {\n win_counts[idxs[j]]--;\n }\n state[i][idxs[j]] = 0;\n state[idxs[j]][i] = 0;\n }\n \n } while(next_permutation(all(part)));\n \n return memo[win_counts] = res;\n}\n\nint solve(){\n int n, m;cin>> n;\n if(n==0) return 1;\n cin >> m;\n memo.clear();\n // 0: 未定\n // 1: 勝利\n // 2: 敗北\n vector<vector<int>> state(n,vector<int>(n,0));\n vector<int> lefts(n,0);\n int w,l;\n rep(_,m) {\n cin >> w >> l;\n w--;\n l--;\n state[w][l] = 1;\n state[l][w] = 2;\n lefts[w]++;\n }\n\n ll ans = dfs(n,0,state,lefts);\n cout << ans << '\\n';\n return 0;\n}\n\nint main() {\n #ifdef ADRY\n #else\n cin.tie(0);\n ios::sync_with_stdio(false);\n #endif\n while(!solve());\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 5168, "score_of_the_acc": -0.1766, "final_rank": 16 }, { "submission_id": "aoj_1627_10667795", "code_snippet": "//#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (ll i=0;i<(ll)n;i++)\n#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)\n#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)\n#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)\n#define vl vector<ll>\n#define vvl vector<vector<ll>>\n#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<\" \";}cout<<endl;\n#define vpdbg(a) rep(ii,a.size()){cout<<\"{\"<<a[ii].first<<\",\"<<a[ii].second<<\"} \";}cout<<endl;\n#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<\" \";}cout<<endl;}\n#define setdbg(a) for(const auto & ii:a){cout<<ii<<\" \";}cout<<endl;\n#define inf 4000000000000000000LL\n#define mod 998244353LL\n//#define mod 1000000007LL\n#define eps 0.000000001\nrandom_device rnd;// 非決定的な乱数生成器\nmt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード\n\n//#include<boost/multiprecision/cpp_int.hpp>\n//#define bbi boost::multiprecision::cpp_int\n//#include<atcoder/lazysegtree>\n\n\n//整数同士の累乗の計算をする。\nll power(ll A, ll B) {\n\tll result = 1;\n\tfor (ll i=0;i<B;i++){\n\t\tresult *= A;\n\t}\n\treturn result;\n}\n\n// nのk乗をmodで割った余りを計算\nll power_mod(ll n, ll k){\n\tlong long result = 1;\n\twhile (k > 0){\n\t\tif ((k&1) ==1)result=(result*n)%mod;\n\t\tn=n*n%mod;\n\t\tk >>= 1;\n\t}\n\treturn result;\n}\n\n\n//受け取った2次元文字の外側に、文字pをコーティングする。\nvector<string> pad(vector<string> &s,char p){\n\tll h=s.size();\n\tll w=s[0].size();\n\tvector<string> res(h+2,string(w+2,p));\n\trep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];\n\treturn res;\n}\n\n// Union-Find\nstruct UnionFind {\n\tvector<int> par, siz;\n\tUnionFind(int n) : par(n, -1) , siz(n, 1) { }\n\t// 根を求める\n\tint root(int x) {\n\t\tif (par[x] == -1) return x;\n\t\telse return par[x] = root(par[x]);\n\t}\n\t// x と y が同じグループに属するかどうか (根が一致するかどうか)\n\tbool issame(int x, int y) {\n\t\treturn root(x) == root(y);\n\t}\n\t// x を含むグループと y を含むグループとを併合する\n\tbool unite(int x, int y) {\n\t\tx = root(x), y = root(y);\n\t\tif (x == y) return false; \n\t\tif (siz[x] < siz[y]) swap(x, y);\n\t\tpar[y] = x;\n\t\tsiz[x] += siz[y];\n\t\treturn true;\n\t}\n\t// x を含むグループのサイズ\n\tint size(int x) {\n\t\treturn siz[root(x)];\n\t}\n};\n\n\n//グリッド問題等用\nvl dx={1,0,-1,0};\nvl dy={0,1,0,-1};\n\nll n;\nvl wcnt,lcnt;\nvvl wl;\nll ans;\n\nvoid dfs(ll i,ll j){\n\tif(i==n){\n\t\tans++;\n\t\treturn;\n\t}\n\tif(j==n){\n\t\tdfs(i+1,0);\n\t\treturn;\n\t}\n\tif(i==j||wl[i][j]!=0){\n\t\tdfs(i,j+1);\n\t\treturn;\n\t}\n\n\t// iが勝つ場合\n\twcnt[i]++;\n\tlcnt[j]++;\n\twl[i][j]=1;\n\twl[j][i]=-1;\n\tif(wcnt[i]<=n/2&&lcnt[j]<=n/2){\n\t\tdfs(i,j+1);\n\t}\n\twcnt[i]--;\n\tlcnt[j]--;\n\twl[i][j]=0;\n\twl[j][i]=0;\n\n\t// jが勝つ場合\n\twcnt[j]++;\n\tlcnt[i]++;\n\twl[j][i]=1;\n\twl[i][j]=-1;\n\tif(wcnt[j]<=n/2&&lcnt[i]<=n/2){\n\t\tdfs(i,j+1);\n\t}\n\twcnt[j]--;\n\tlcnt[i]--;\n\twl[j][i]=0;\n\twl[i][j]=0;\n\treturn;\n}\n\nll solve(){\n\t\n\tcin>>n;\n\tif(n==0){return 1;}\n\t\n\twcnt=vl(n,0);\n\tlcnt=vl(n,0);\n\twl=vvl(n,vl(n,0));\n\tans=0;\n\t\n\tll m;\n\tcin>>m;\n\tbool f=true;\n\trep(i,m){\n\t\tll a,b;\n\t\tcin>>a>>b;\n\t\ta--,b--;\n\t\twl[a][b]=1;\n\t\twl[b][a]=-1;\n\t\twcnt[a]++;\n\t\tlcnt[b]++;\n\t\tif(wcnt[a]>n/2)f=false;\n\t\tif(lcnt[b]>n/2)f=false;\n\t}\n\n\tif(!f){\n\t\tcout<<0<<endl;\n\t\treturn 0;\n\t}\n\n\tdfs(0,0);\n\tcout<<ans<<endl;\n\treturn 0;\n}\n\n//メイン\nint main(){\n\twhile(solve()==0);\n\treturn 0;\n}", "accuracy": 1, "time_ms": 120, "memory_kb": 3388, "score_of_the_acc": -0.0219, "final_rank": 5 }, { "submission_id": "aoj_1627_10640270", "code_snippet": "#include <bits/stdc++.h>\n#include <numeric>\n#define FAST_IO\n#define OVERRIDE(a, b, c, d, ...) d\n#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)\n#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)\n#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)\n#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)\n#define PER3(i, m, n) for (i32 i = (i32)(n)-1; i >= (i32)(m); --i)\n#define PER(...) OVERRIDE(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)\n#define ALL(x) begin(x), end(x)\n#define LEN(x) (i32)(x.size())\nusing namespace std;\nusing u32 = unsigned int;\nusing u64 = unsigned long long;\nusing i32 = signed int;\nusing i64 = signed long long;\nusing f64 = double;\nusing f80 = long double;\nusing pi = pair<i32, i32>;\nusing pl = pair<i64, i64>;\ntemplate <typename T>\nusing V = vector<T>;\ntemplate <typename T>\nusing VV = V<V<T>>;\ntemplate <typename T>\nusing VVV = V<V<V<T>>>;\ntemplate <typename T>\nusing VVVV = V<V<V<V<T>>>>;\ntemplate <typename T>\nusing PQR = priority_queue<T, V<T>, greater<T>>;\ntemplate <typename T>\nbool chmin(T &x, const T &y) {\n if (x > y) {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\nbool chmax(T &x, const T &y) {\n if (x < y) {\n x = y;\n return true;\n }\n return false;\n}\ntemplate <typename T>\ni32 lob(const V<T> &arr, const T &v) {\n return (i32)(lower_bound(ALL(arr), v) - arr.begin());\n}\ntemplate <typename T>\ni32 upb(const V<T> &arr, const T &v) {\n return (i32)(upper_bound(ALL(arr), v) - arr.begin());\n}\ntemplate <typename T>\nV<i32> argsort(const V<T> &arr) {\n V<i32> ret(arr.size());\n iota(ALL(ret), 0);\n sort(ALL(ret), [&](i32 i, i32 j) -> bool {\n if (arr[i] == arr[j]) {\n return i < j;\n } else {\n return arr[i] < arr[j];\n }\n });\n return ret;\n}\n#ifdef INT128\nusing u128 = __uint128_t;\nusing i128 = __int128_t;\n#endif\n[[maybe_unused]] constexpr i32 INF = 1000000100;\n[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;\nstruct SetUpIO {\n SetUpIO() {\n#ifdef FAST_IO\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n#endif\n cout << fixed << setprecision(15);\n }\n} set_up_io;\nvoid scan(char &x) { cin >> x; }\nvoid scan(u32 &x) { cin >> x; }\nvoid scan(u64 &x) { cin >> x; }\nvoid scan(i32 &x) { cin >> x; }\nvoid scan(i64 &x) { cin >> x; }\nvoid scan(string &x) { cin >> x; }\ntemplate <typename T>\nvoid scan(V<T> &x) {\n for (T &ele : x) {\n scan(ele);\n }\n}\nvoid read() {}\ntemplate <typename Head, typename... Tail>\nvoid read(Head &head, Tail &...tail) {\n scan(head);\n read(tail...);\n}\n#define CHAR(...) \\\n char __VA_ARGS__; \\\n read(__VA_ARGS__);\n#define U32(...) \\\n u32 __VA_ARGS__; \\\n read(__VA_ARGS__);\n#define U64(...) \\\n u64 __VA_ARGS__; \\\n read(__VA_ARGS__);\n#define I32(...) \\\n i32 __VA_ARGS__; \\\n read(__VA_ARGS__);\n#define I64(...) \\\n i64 __VA_ARGS__; \\\n read(__VA_ARGS__);\n#define STR(...) \\\n string __VA_ARGS__; \\\n read(__VA_ARGS__);\n#define VEC(type, name, size) \\\n V<type> name(size); \\\n read(name);\n#define VVEC(type, name, size1, size2) \\\n VV<type> name(size1, V<type>(size2)); \\\n read(name);\n\n#ifdef DEBUGF\n#include \"spl/template/debug.hpp\"\n#else\n#define DBG(...) (void)0\n#endif\n\nvoid solve(i32 n) {\n I32(m);\n VV<i32> res(n, V<i32>(n, 0));\n REP(i, m) {\n I32(x, y);\n --x;\n --y;\n res[x][y] = 1;\n res[y][x] = -1;\n }\n V<i32> pcnt(n, 0), mcnt(n, 0);\n REP(i, n) REP(j, n) {\n if (res[i][j] == 1) {\n ++pcnt[i];\n }\n if (res[i][j] == -1) {\n ++mcnt[i];\n }\n }\n i32 ans = 0;\n auto dfs = [&](auto dfs, i32 r, i32 c) -> void {\n DBG(r, c, res);\n if (r == n) {\n ++ans;\n return;\n }\n if (c == n) {\n i32 sum = accumulate(ALL(res[r]), 0);\n if (sum != 0) {\n return;\n }\n dfs(dfs, r + 1, r + 2);\n return;\n }\n if (res[r][c] == 0) {\n if (pcnt[r] < n / 2) {\n res[r][c] = 1;\n res[c][r] = -1;\n ++pcnt[r];\n dfs(dfs, r, c + 1);\n res[r][c] = 0;\n res[c][r] = 0;\n --pcnt[r];\n }\n if (mcnt[r] < n / 2) {\n res[r][c] = -1;\n res[c][r] = 1;\n ++mcnt[r];\n dfs(dfs, r, c + 1);\n res[r][c] = 0;\n res[c][r] = 0;\n --mcnt[r];\n }\n } else {\n dfs(dfs, r, c + 1);\n }\n };\n dfs(dfs, 0, 1);\n cout << ans << endl;\n}\n\nint main() {\n while (true) {\n I32(n);\n if (n == 0) {\n break;\n }\n solve(n);\n }\n}", "accuracy": 1, "time_ms": 240, "memory_kb": 3584, "score_of_the_acc": -0.0661, "final_rank": 11 }, { "submission_id": "aoj_1627_10625207", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n// dp[i][vec]: 0..i 番での試合について、勝ち数ベクトルが vec であるような組合せの数\n\n\nll solve(int N) {\n int M;\n cin >> M;\n // A[x][y] == 1 -> x wins to y\n vector<vector<int>> A(N, vector<int>(N));\n for (int i = 0; i < M; i++) {\n int x, y;\n cin >> x >> y;\n x--;\n y--;\n A[x][y] = 1;\n }\n\n // for (int x = 0; x < N; x++) {\n // for (int y = 0; y < N; y++) {\n // cerr << A[x][y] << (y < N - 1 ? \" \": \"\\n\");\n // }\n // }\n\n unordered_map<string, ll> dp;\n dp[string(N - 1, 0)] = 1LL;\n for (int x = 1; x < N; x++) {\n // fix x\n // cerr << \"x = \" << x << endl;\n unordered_map<string, ll> ndp;\n for (auto [vec, cnt]: dp) {\n // cerr << \"vec = [\";\n // for (auto v: vec) cerr << int(v) << \", \";\n // cerr << \"]\" << endl;\n for (int bi = 0; bi < (1 << x); bi++) {\n auto nvec = vec;\n bool flag = true;\n for (int y = 0; y < x; y++) {\n if (bi >> y & 1) { // x wins to y\n if (A[y][x] == 1) flag = false;\n if (x < N - 1) nvec[x]++;\n } else {\n if (A[x][y] == 1) flag = false;\n nvec[y]++;\n }\n }\n\n if (!flag) {\n // cerr << \"CANNOT trans\" << endl;\n continue;\n }\n\n // vec[*] <= (N - 1) / 2 ?\n for (int i = 0; i < N; i++) {\n if (nvec[i] > (N - 1) / 2) flag = false;\n }\n if (!flag) continue;\n\n ndp[nvec] += cnt;\n }\n }\n swap(dp, ndp);\n }\n string ans_vec(N - 1, (N - 1) / 2);\n // ans_vec[N - 1] = 0;\n // for (auto [vec, cnt]: dp) {\n // cerr << \"vec = [\";\n // for (auto v: vec) cerr << int(v) << \", \";\n // cerr << \"] -> cnt = \" << cnt << endl;\n\n // }\n return dp[ans_vec];\n}\n\nint main() {\n while (1) {\n int N;\n cin >> N;\n if (N == 0) break;\n ll ans = solve(N);\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3548, "score_of_the_acc": -0.0213, "final_rank": 4 }, { "submission_id": "aoj_1627_10625087", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ALL(v) (v).begin(), (v).end()\nusing ll = long long;\n#define rep(i,n) for(int i =0;i<(int)(n);i++)\n\nvoid print(vector<int> a){\n rep(i,a.size()){\n //cout<<a[i] << \" \";\n }\n //cout<<endl;\n}\nvoid print_pair(vector<pair<int,int>> a){\n for(auto [x,y]:a){\n //cout<<x <<\" \"<< y<<endl;\n }\n}\nint toSeisuu(vector<int> a){\n int k=1;\n int res =0;\n rep(i,a.size()){\n res+=a[i]*k;\n k*=10;\n if(a[i]<0){\n print(a);\n return -1;\n }\n }\n return res;\n}\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int c =0;\n while (true) {\n //cout<<\"c+\"<<c<<endl;\n c++;\n \n int n,m;\n cin>>n;\n if(n==0)break;\n cin>>m;\n vector<int> x(n,0);\n set<pair<int,int>> playedGame;\n rep(i,m){\n int a,b;\n cin>>a>>b;\n a--;b--;\n //win a\n x[a]++;\n playedGame.insert({min(a,b),max(a,b)});\n }\n // if(c>1)continue;\n // cout<<\"a\"<<endl;\n vector<pair<int,int>> unplayedGame;\n rep(i,n){\n rep(j,n){\n if(i>=j)continue;\n if(playedGame.count({i,j})!=0)continue;\n unplayedGame.push_back({i,j});\n }\n }\n // cout<<\"b2\"<<endl;\n vector<pair<int,int>> mae,ushiro;\n\n int st = unplayedGame.size();\n rep(i,st){\n if(i<(int)(unplayedGame.size())/2){\n mae.push_back(unplayedGame[i]);\n }else{\n ushiro.push_back(unplayedGame[i]);\n }\n }\n // // //cout<<\"mae:\"<<endl;\n // // print_pair(mae);\n // //cout<<\"==\"<<endl;\n // print_pair(ushiro);\n // //cout<<\"==\"<<endl;\n // //cout<<\"b\"<<endl;\n int ms=mae.size(),us=ushiro.size();\n map<int,int> mp;\n //全列挙\n for(int i =0; i < (1<<ms);i++){\n vector<int> a(n,0);\n\n rep(k,ms){\n auto [s,t] = mae[k];\n // s wins t\n if(((i>>k)&1)==0){\n a[s]++;\n }else{\n a[t]++;\n }\n }\n //整数に変換site mapNIHOZON\n mp[toSeisuu(a)]++;\n //cout<<\"==\"<<toSeisuu(a)<<endl;\n }\n int ans=0;\n //break\n // //cout<<\"x\"<<endl;\n for(int i =0; i < (1<<us);i++){\n vector<int> a(n,0);\n\n \n rep(k,us){\n auto [s,t] = ushiro[k];\n // s wins t\n //cout<<s<<\" \"<<t<<endl;\n if(((i>>k)&1)==0){\n a[s]++;\n }else{\n a[t]++;\n }\n }\n //cout<<\"!<==\"<<toSeisuu(a)<<endl;\n rep(k,n){\n int p=(n-1)/2;\n int xx=x[k];\n a[k]=p-xx-a[k];\n }\n //整数に変換site mapNIHOZON\n ans+=mp[toSeisuu(a)];\n //cout<<\"!==\"<<toSeisuu(a)<<endl;\n }\n cout<<ans<<endl;\n }\n\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 4992, "score_of_the_acc": -0.1582, "final_rank": 15 }, { "submission_id": "aoj_1627_10621189", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main(){\n while(1){\n int N;\n cin>>N;\n if(N==0)return 0;\n int M;\n cin>>M;\n vector win(N,vector<int>(N,-1));\n rep(i,0,M){\n int x,y;\n cin>>x>>y;\n x--;\n y--;\n win[x][y]=1;\n win[y][x]=0;\n }\n vector<int>C(N);\n int ans=0;\n auto dfs=[&](auto dfs,int x,int y)-> void {\n if(x==N-1){\n ans++;\n return;\n }\n if(win[x][y]!=0&&C[x]+1<=N/2){\n C[x]++;\n if(y+1==N&&C[x]==N/2)dfs(dfs,x+1,x+2);\n else dfs(dfs,x,y+1);\n C[x]--;\n }\n if(win[x][y]!=1&&C[x]+(N-1-y)>=N/2){\n C[y]++;\n if(y+1==N&&C[x]==N/2)dfs(dfs,x+1,x+2);\n else dfs(dfs,x,y+1);\n C[y]--;\n }\n };\n dfs(dfs,0,1);\n cout<<ans<<endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3584, "score_of_the_acc": -0.0312, "final_rank": 7 }, { "submission_id": "aoj_1627_10611581", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// using mint = modint998244353;\n// using mint = modint1000000007;\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\nconstexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n while(true) {\n ll n;\n cin >> n;\n if(n == 0) break;\n ll m;\n cin >> m;\n vector<vector<ll>> flag(n, vector<ll>(n, 0));\n // flag[i][j] : i が j に勝利したら 1, 逆なら 2, まだなら 0\n rep(i, m) {\n ll x, y;\n cin >> x >> y;\n x--, y--;\n flag[x][y] = 1;\n flag[y][x] = 2;\n }\n\n ll tn = n;\n ll threshold = 7;\n n = min(n, threshold);\n\n ll rest_match = 0;\n rep(i, n) for(ll j = i + 1; j < n; j++) rest_match += (flag[i][j] == 0);\n\n ll ans = 0;\n map<vector<ll>, ll> cnt;\n rep(bit, (1LL << rest_match)) {\n vector<ll> win(tn, 0);\n ll idx = 0;\n bool pruning = false;\n rep(i, n) {\n for(ll j = i + 1; j < n; j++) {\n if(flag[i][j] == 1) win[i]++;\n else if(flag[i][j] == 2) win[j]++;\n else {\n if((bit >> idx) & 1) win[i]++;\n else win[j]++;\n idx++;\n }\n if(win[i] > tn * (tn - 1) / 2 / tn || win[j] > tn * (tn - 1) / 2 / tn) {\n pruning = true;\n break;\n }\n }\n if(pruning) break;\n }\n if(pruning) continue;\n\n bool ok = true;\n rep(i, n) ok &= (win[i] == n * (n - 1) / 2 / n);\n if(ok) ans++;\n if(n != tn) cnt[win]++;\n }\n if(tn <= threshold) {\n cout << ans << endl;\n continue;\n }\n // for(auto [vec, c]: cnt) {\n // rep(i, vec.size()) cerr << vec[i] << \" \";\n // cerr << \" : \" << c << endl;\n // }\n\n // n = 9 の時は半分全列挙風に?\n ans = 0;\n rest_match = 0;\n for(ll i = threshold; i < tn; i++) {\n for(ll j = 0; j < i; j++) {\n rest_match += (flag[i][j] == 0);\n }\n }\n // cerr << \"rest_match: \" << rest_match << endl;\n rep(bit, (1LL << rest_match)) {\n vector<ll> win(tn, tn * (tn - 1) / 2 / tn);\n ll idx = 0;\n bool pruning = false;\n for(ll i = threshold; i < tn; i++) {\n for(ll j = 0; j < i; j++) {\n if(flag[i][j] == 1) win[i]--;\n else if(flag[i][j] == 2) win[j]--;\n else {\n if((bit >> idx) & 1) win[i]--;\n else win[j]--;\n idx++;\n }\n if(win[i] < 0 || win[j] < 0) {\n pruning = true;\n break;\n }\n }\n if(pruning) break;\n }\n // if(win[7] == 0 && win[8] == 0) {\n // rep(i, tn) {\n // cerr << win[i] << \" \";\n // }\n // cerr << endl;\n // }\n if(cnt.find(win) != cnt.end()) ans += cnt[win];\n }\n cout << ans << endl;\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 270, "memory_kb": 3624, "score_of_the_acc": -0.0763, "final_rank": 12 }, { "submission_id": "aoj_1627_10607452", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst long long MOD1 = 1000000007;\nconst long long MOD2 = 998244353;\n#define all(v) v.begin(), v.end()\n#define rall(a) a.rbegin(), a.rend()\n#define fi first\n#define se second\n#define endl \"\\n\"\n\n#define chmax(x, y) x = max(x, y)\n#define chmin(x, y) x = min(x, y)\n\ntemplate <typename T>\nusing vc = vector<T>; // prioriy_queueに必要なのでここにこれ書いてます\ntemplate <typename T>\nusing vv = vc<vc<T>>;\n#define int long long\nusing ll = long long;\nll INF = 2e18;\n\n#ifdef ONLINE_JUDGE\n// ジャッジ環境ではデバッグマクロを無効化\n#define debug_x(x) \\\n do \\\n { \\\n } while (0)\n#define debug_vc(v) \\\n do \\\n { \\\n } while (0)\n#define debug_vv(v) \\\n do \\\n { \\\n } while (0)\n#else\n// ローカルではデバッグ出力\n#define debug_x(x) cout << #x << \" = \" << (x) << endl\n#define debug_vc(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) cout << v[i] << ' '; \\\n cout << endl; \\\n } // 一次元配列を出力する\n#define debug_vv(v) \\\n { \\\n cout << #v << endl; \\\n ll n = size(v); \\\n rep(i, n) \\\n { \\\n rep(j, size(v[i])) { cout << v[i][j] << ' '; } \\\n cout << endl; \\\n } \\\n } // 二次元配列を出力する\n#endif\n\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define rrep(i, n) for (ll i = 1; i <= (n); ++i)\n#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)\n#define nfor(i, s, n) for (ll i = s; i < n; i++) // i=s,s+1...n-1 ノーマルfor\n#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--) // s-1スタートでnまで落ちる\n\nusing ld = long double;\nusing bl = bool;\n\ntemplate <class T>\nusing pq = priority_queue<T, vc<T>>; // 大きい順\ntemplate <class T>\nusing pq_g = priority_queue<T, vc<T>, greater<T>>; // 小さい順\n\nusing vl = vc<ll>;\nusing vvl = vv<ll>;\nusing vvvl = vv<vl>;\nusing vvvvl = vv<vvl>;\nusing vs = vc<string>;\nusing vvs = vv<string>;\nusing vb = vc<bl>;\nusing vvb = vv<bl>;\nusing vvvb = vv<vb>;\nusing vld = vc<ld>;\nusing vvld = vv<ld>;\nusing vvvld = vv<vld>;\nusing P = pair<ll, ll>;\nusing vP = vc<P>;\nusing vvP = vc<vP>;\nusing vvvP = vv<vP>;\n\n#define pb push_back\n\n#define YES cout << \"Yes\" << endl\n#define NO cout << \"No\" << endl\n#define YN \\\n { \\\n cout << \"Yes\" << endl; \\\n } \\\n else \\\n { \\\n cout << \"No\" << endl; \\\n } // if(a==b)YN;\n#define dame cout << -1 << endl\n\nstring dir = \"DRUL\"; // 第4象限の場合\nvl di = {1, 0, -1, 0}; // vl di={1,1,0,-1,-1,-1,0,1};\nvl dj = {0, 1, 0, -1}; // vl dj={0,1,1,1,0,-1,-1,-1};\n\nbool out_grid(ll i, ll j, ll h, ll w)\n{ // trueならcontinue\n return (!(0 <= i && i < h && 0 <= j && j < w));\n}\n\nll dfs(vl &battleTimes, ll n, vl &win, vP &amariBattle, ll idx, ll amari)\n{\n ll winCnt = (n - 1) / 2;\n vl at(n);\n rep(i, n)\n {\n at[i] = winCnt - win[i];\n if (at[i] < 0 || at[i] > n - 1 - battleTimes[i])\n {\n return 0;\n }\n }\n if (amari == 0)\n return 1;\n ll sum = 0;\n auto [i, j] = amariBattle[idx];\n battleTimes[i]++;\n battleTimes[j]++;\n win[j]++;\n sum += dfs(battleTimes, n, win, amariBattle, idx + 1, amari - 1);\n win[j]--;\n win[i]++;\n sum += dfs(battleTimes, n, win, amariBattle, idx + 1, amari - 1);\n win[i]--;\n battleTimes[i]--;\n battleTimes[j]--;\n return sum;\n}\n\nbl solve()\n{\n ll n;\n cin >> n;\n if (n == 0)\n {\n return 0;\n }\n ll m;\n cin >> m;\n vl win(n);\n vl x(m), y(m);\n // res[i][j] = iがjに勝ったか?(不明なら-1)\n vvl res(n, vl(n, -1));\n vl battleTimes(n);\n rep(i, m)\n {\n cin >> x[i] >> y[i];\n x[i]--;\n y[i]--;\n res[x[i]][y[i]] = 1;\n res[y[i]][x[i]] = 0;\n win[x[i]]++;\n battleTimes[x[i]]++;\n battleTimes[y[i]]++;\n }\n\n ll amari = n * (n - 1) / 2 - m;\n vP amariBattle;\n rep(i, n)\n {\n rep(j, n)\n {\n if (i >= j)\n continue;\n if (res[i][j] == -1)\n {\n amariBattle.pb({i, j});\n }\n }\n }\n ll winCnt = (n - 1) / 2;\n vl at(n);\n rep(i, n)\n {\n at[i] = winCnt - win[i];\n if (at[i] < 0 || at[i] > n - 1 - battleTimes[i])\n {\n cout << 0 << endl;\n return 1;\n }\n }\n\n // バックトラック法でやる\n cout << dfs(battleTimes, n, win, amariBattle, 0, amari) << endl;\n\n // ll ans = 0;\n // rep(i, 1 << amari)\n // {\n // vvl tmp = res;\n // rep(j, amari)\n // {\n // auto [ii, jj] = amariBattle[j];\n // if (i & (1 << j))\n // {\n // tmp[ii][jj] = 1;\n // tmp[jj][ii] = 0;\n // }\n // else\n // {\n // tmp[ii][jj] = 0;\n // tmp[jj][ii] = 1;\n // }\n // }\n // bl ok = true;\n // rep(j, n)\n // {\n // ll cnt = 0;\n // rep(k, n)\n // {\n // if (tmp[j][k] == 1)\n // {\n // cnt++;\n // }\n // }\n // if (cnt != winCnt)\n // {\n // ok = false;\n // break;\n // }\n // }\n // if (ok)\n // {\n // ans++;\n // }\n // }\n // cout << ans << endl;\n\n return 1;\n}\n\nsigned main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n while (1)\n {\n if (!solve())\n break;\n }\n return 0;\n}\n\n/*\nshift+alt+Fで成形\nf2で変数名変更\n\nスニペット\nbasic\n m:atcoder以外用\n ma:ABC/ARC用\n mahc:AHC用\nmath\n floor:床関数\n ceil:天井関数\n comv:二項係数\n modpow: a^b mod m\n GCD:最大公約数\n extGCD:拡張ユークリッド互除法\n PFD:素因数分解\n isprime:エラトステネスの篩\n matrixpow:行列累乗\ndata_structure\n seg:セグメント木\ntechnic\n Compress:座標圧縮\n runlength:ランレングス圧縮\ngraph\n warshall:ワーシャルフロイド法\n dfs:深さ優先探索\n bfs:幅優先探索\n LCA:最近共通祖先\n*/", "accuracy": 1, "time_ms": 560, "memory_kb": 3380, "score_of_the_acc": -0.1172, "final_rank": 14 }, { "submission_id": "aoj_1627_10603173", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\n#include <stdlib.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define all(x) (x).begin(), (x).end()\n//constexpr ll MOD = 1000000007;\nconstexpr ll MOD = 998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 1LL<<60;\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\n\nll gcd(ll a, ll b){\n if(a%b == 0){\n return b;\n }else{\n return gcd(b, a%b);\n }\n}\n\nll lcm(ll a, ll b){\n return a*b / gcd(a, b);\n}\n\nll powMod(ll x, ll n) {\n if (n == 0) return 1 % MOD;\n ll val = powMod(x, n / 2);\n val *= val;\n val %= MOD;\n if (n % 2 == 1) val *= x;\n return val % MOD;\n}\n\nint main() {\n while(true){\n ll n; cin >> n;\n if(n == 0) break;\n ll m; cin >> m;\n vector<vector<ll>> win(n,vector<ll>(n, -1));\n rep(i,0,n) win[i][i] = 0;\n rep(i,0,m){\n ll x, y; cin >> x >> y;\n x--;y--;\n win[x][y] = 1;\n win[y][x] = 0;\n }\n ll ans = 0;\n auto solve = [&](auto& solve, ll i, ll j, ll cnt) -> void {\n if(i == n-1){\n ans++;\n return;\n }\n if(i == j){\n solve(solve, i, j+1, cnt);\n return;\n }\n if(j == n-1){\n if(win[i][j] != -1){\n cnt -= win[i][j];\n if(cnt == 0) solve(solve, i+1, 0, n/2);\n }else{\n if(cnt == 1){\n win[i][j] = 1;\n win[j][i] = 0;\n solve(solve, i+1, 0, n/2);\n win[i][j] = -1;\n win[j][i] = -1;\n }else if(cnt == 0){\n win[i][j] = 0;\n win[j][i] = 1;\n solve(solve, i+1, 0, n/2);\n win[i][j] = -1;\n win[j][i] = -1;\n }\n }\n return;\n }\n if(i > j){\n if(win[i][j] != -1){\n cnt -= win[i][j];\n if(cnt >= 0) solve(solve, i, j+1, cnt);\n }else{\n if(cnt > n-j-1) return;\n if(cnt > 0){\n win[i][j] = 1;\n win[j][i] = 0;\n solve(solve, i, j+1, cnt-1);\n win[i][j] = -1;\n win[j][i] = -1;\n }\n if(cnt < n-j-1){\n win[i][j] = 0;\n win[j][i] = 1;\n solve(solve, i, j+1, cnt);\n win[i][j] = -1;\n win[j][i] = -1;\n }\n }\n }\n else {\n if(win[i][j] != -1){\n cnt -= win[i][j];\n if(cnt >= 0) solve(solve, i, j+1, cnt);\n }else{\n if(cnt > n-j) return;\n if(cnt > 0){\n win[i][j] = 1;\n win[j][i] = 0;\n solve(solve, i, j+1, cnt-1);\n win[i][j] = -1;\n win[j][i] = -1;\n }\n if (cnt < n-j){\n win[i][j] = 0;\n win[j][i] = 1;\n solve(solve, i, j+1, cnt);\n win[i][j] = -1;\n win[j][i] = -1;\n }\n }\n }\n };\n solve(solve, 0, 0, n/2);\n cout << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3584, "score_of_the_acc": -0.0333, "final_rank": 8 }, { "submission_id": "aoj_1627_10476312", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nbool solve();\n\nusing ll = long long;\n#define rep(i, a, b) for (int i = a; i < (b); ++i)\n#define sz(x) (int)(x).size()\n#define all(x) x.begin(), x.end()\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntemplate <class T>\ninline bool chmin(T &a, T b) {\n return ((a > b) ? (a = b, 1) : (0));\n}\ntemplate <class T>\ninline bool chmax(T &a, T b) {\n return ((a < b) ? (a = b, 1) : (0));\n}\n\nint main() {\n while (solve());\n return 0;\n}\n\nbool solve() {\n int n, m;\n cin >> n >> m;\n if (n == 0) return 0;\n vector G(n, vector<bool>(n));\n vi cntl(n, 0);\n vi cntw(n, 0);\n rep(mi, 0, m) {\n int x, y;\n cin >> x >> y;\n x--, y--;\n G[x][y] = G[y][x] = true;\n cntw[x]++;\n cntl[y]++;\n }\n int ans = 0;\n auto dfs = [&](auto dfs, int i, int j) -> void {\n if (j >= n) {\n i++, j = 0;\n }\n if (i >= n) {\n ans++;\n return;\n }\n if (i == j) {\n if (not(cntw[i] > n / 2 or cntl[j] > n / 2 or cntw[j] > n / 2 or\n cntl[i] > n / 2))\n dfs(dfs, i, j + 1);\n return;\n }\n if (G[i][j])\n dfs(dfs, i, j + 1);\n else {\n // i win\n G[i][j] = G[j][i] = true;\n cntw[i]++, cntl[j]++;\n if (not(cntw[i] > n / 2 or cntl[j] > n / 2)) dfs(dfs, i, j + 1);\n\n cntw[i]--, cntl[j]--;\n G[i][j] = G[j][i] = false;\n // j win\n G[i][j] = G[j][i] = true;\n cntl[i]++, cntw[j]++;\n if (not(cntl[i] > n / 2 or cntw[j] > n / 2)) dfs(dfs, i, j + 1);\n\n cntl[i]--, cntw[j]--;\n G[i][j] = G[j][i] = false;\n }\n };\n dfs(dfs, 0, 0);\n cout << ans << endl;\n return 1;\n}", "accuracy": 1, "time_ms": 190, "memory_kb": 3584, "score_of_the_acc": -0.0552, "final_rank": 9 }, { "submission_id": "aoj_1627_10383211", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)\n#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)\n#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name\n#define REP1(n) for(ll i=0;i<n;i++)\n#define REP2(i, n) for (ll i=0;i<n;i++)\n#define REP3(i, a, n) for (ll i=a;i<n;i++)\n#define REP4(i, a, b, n) for(ll i=a;i<n;i+=b)\n#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)\n#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name\n#define RREP1(n) for(ll i=n;i>=0;i--)\n#define RREP2(i, n) for(ll i=n;i>=0;i--)\n#define RREP3(i, a, n) for(ll i=n;i>=a;i--)\n#define RREP4(i, a, b, n) for(ll i=n;i>=a;i-=b)\n#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)\n#define foa(a,v) (auto& a : (v))\n#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))\n#define len(n) (long long)(n).size()\n#define pb push_back\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\nusing vs = vector<string>;\nusing vvs = vector<vs>;\nusing vvvs = vector<vvs>;\nusing vld = vector<ld>;\nusing vvld = vector<vld>;\nusing vvvld = vector<vvld>;\nusing vc = vector<char>;\nusing vvc = vector<vc>;\nusing vvvc = vector<vvc>;\nusing pll = pair<ll,ll>;\nusing vpll = vector<pll>;\ntemplate<class... T>\nconstexpr auto min(T... a){\n return min(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nconstexpr auto max(T... a){\n return max(initializer_list<common_type_t<T...>>{a...});\n}\ntemplate<class... T>\nvoid input(T&... a){\n (cin >> ... >> a);\n}\nll modpow(ll a,ll b,ll c){\n ll ans = 1;\n while (b){\n if (b & 1){\n ans *= a;\n ans %= c;\n }\n a *= a;\n a %= c;\n b /= 2;\n }\n return ans;\n}\n#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)\n#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)\n#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)\n#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)\n#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)\n#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)\n#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}\n#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}\n#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}\n#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}\n#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}\n#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}\n#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}\n#define PLL(name) pll name;cin>>name.first>>name.second;\n#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}\n\nvoid print(){cout << \"\\n\";}\ntemplate<class T, class... Ts>\nvoid print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\\n';}\nvoid print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << \" \";}else{cout << '\\n';}}}\nvoid print(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << \" \";}else{cout << '\\n';}}}\n\n\n\nvoid solve(ll n,ll o,vpll a){\n ll m = n*(n-1)/2;\n vvll b(n,vll(n,-1));\n rep(i,o){\n auto [x,y] = a[i];\n x--;\n y--;\n b[x][y] = 1;\n b[y][x] = 0;\n }\n vpll c;\n rep(x,n){\n rep(y,x+1,n){\n c.push_back({x,y});\n }\n }\n map<vector<ll>,ll> mp1,mp2;\n ll l = m/2;\n rep(i,(1<<l)){\n vll temp(n,0);\n ll check = 1;\n rep(j,l){\n auto [x,y] = c[j];\n if((((i >> j) & 1) == 0 && b[x][y] == 1) || (((i >> j) & 1) == 1 && b[x][y] == 0)){\n check = 0;\n break;\n }\n if(((i >> j) & 1) == 0){\n temp[y]++;\n }\n else{\n temp[x]++;\n }\n }\n if(!check){continue;}\n\n\n mp1[temp]++;\n }\n\n l = m - m/2;\n\n rep(i,(1<<l)){\n vll temp(n,0);\n ll check = 1;\n rep(j,l){\n auto [x,y] = c[j + m/2];\n if((((i >> j) & 1) == 0 && b[x][y] == 1) || (((i >> j) & 1) == 1 && b[x][y] == 0)){\n check = 0;\n break;\n }\n if(((i >> j) & 1) == 0){\n temp[y]++;\n }\n else{\n temp[x]++;\n }\n }\n if(!check){continue;}\n \n mp2[temp]++;\n }\n ll ans = 0;\n for(auto [key,value]:mp1){\n vll temp(n,n/2);\n rep(i,n){\n temp[i] -= key[i];\n }\n \n ans += mp2[temp] * value;\n }\n print(ans);\n return;\n}\n\n\nint main(){\n while(1){\n LL(n);\n if(n == 0){return 0;}\n LL(m);\n VPLL(a,m);\n solve(n,m,a);\n //break;\n }\n \n}", "accuracy": 1, "time_ms": 420, "memory_kb": 14208, "score_of_the_acc": -1.0808, "final_rank": 20 }, { "submission_id": "aoj_1627_10235630", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\nint n;\nvector<vector<int>> v(9,vector<int> (9));\nvector<int> win(9);\nvector<int> lose(9);\nll ans;\n\n\nvoid f(int a, int b) {\n\tint na,nb;\n\n\t// cout << a << ' ' << b << endl;\n\n\tif (a == n-1) {\n\t\trep(i,n) {\n\t\t\tif (win[i] != n/2 || lose[i] != n/2) {\n\t\t\t\tbreak;\n\t\t\t} \n\t\t\tif (i == n-1) ans++;\n\t\t}\n\t\treturn;\n\t}\n\n\tif (b == n-1) {\n\t\tna = a+1;\n\t\tnb = na+1;\n\t}\n\telse {\n\t\tna = a;\n\t\tnb = b+1;\n\t}\n\n\t// cout << na << ' ' << nb << endl;\n\n\n\tif (v[a][b] == -1) {\n\t\twin[a]++;\n\t\tlose[b]++;\n\t\tif (win[a] <= n/2 && lose[b] <= n/2) f(na,nb);\n\t\twin[a]--;\n\t\tlose[b]--;\n\n\t\twin[b]++;\n\t\tlose[a]++;\n\t\tif (win[b] <= n/2 && lose[a] <= n/2) f(na,nb);\n\t\twin[b]--;\n\t\tlose[a]--;\n\t}\n\telse {\n\t\tf(na,nb);\n\t}\n\n\treturn;\n}\n\n\n\nvoid solve() {\n\tint m;\n\tcin >> m;\n\trep(i,n) {\n\t\trep(j,n) {\n\t\t\tv[i][j] = -1;\n\t\t}\n\t}\n\trep(i,9) {\n\t\twin[i] = 0;\n\t\tlose[i] = 0;\n\t}\n\n\tint a,b;\n\trep(i,m) {\n\t\tcin >> a >> b;\n\t\ta--;\n\t\tb--;\n\t\tv[a][b] = 1;\n\t\tv[b][a] = 0;\n\t\twin[a]++;\n\t\tlose[b]++;\n\n\n\t}\n\n\tans = 0;\n\n\tf(0,1);\n\n\n\tcout << ans << endl;\n\treturn;\n}\n\n\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n\twhile (cin >> n && n != 0) {\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 70, "memory_kb": 3340, "score_of_the_acc": -0.0066, "final_rank": 2 }, { "submission_id": "aoj_1627_10235611", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define all(a) a.begin(),a.end()\n#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)\n#define rep(i, n) reps(i, 0, n)\n#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)\nconst long long mod = 1000000007;\nconst long long INF = 1e18;\nll myceil(ll a, ll b) {return (a+b-1)/b;}\n\nint n;\nvector<vector<int>> v(9,vector<int> (9));\nvector<int> win(9);\nvector<int> lose(9);\nll ans;\n\n\nvoid f(int a, int b) {\n\tint na,nb;\n\n\t// cout << a << ' ' << b << endl;\n\n\tif (a == n-1) {\n\t\tans++;\n\t\treturn;\n\t}\n\n\tif (b == n-1) {\n\t\tna = a+1;\n\t\tnb = na+1;\n\t}\n\telse {\n\t\tna = a;\n\t\tnb = b+1;\n\t}\n\n\t// cout << na << ' ' << nb << endl;\n\n\n\tif (v[a][b] == -1) {\n\t\twin[a]++;\n\t\tlose[b]++;\n\t\tif (win[a] <= n/2 && lose[b] <= n/2) f(na,nb);\n\t\twin[a]--;\n\t\tlose[b]--;\n\n\t\twin[b]++;\n\t\tlose[a]++;\n\t\tif (win[b] <= n/2 && lose[a] <= n/2) f(na,nb);\n\t\twin[b]--;\n\t\tlose[a]--;\n\t}\n\telse {\n\t\tif (win[a] <= n/2 && lose[a] <= n/2 && win[b] <= n/2 && lose[b] <= n/2) {\n\t\t\tf(na,nb);\n\t\t}\n\t}\n\n\treturn;\n}\n\n\n\nvoid solve() {\n\tint m;\n\tcin >> m;\n\trep(i,n) {\n\t\trep(j,n) {\n\t\t\tv[i][j] = -1;\n\t\t}\n\t}\n\trep(i,9) {\n\t\twin[i] = 0;\n\t\tlose[i] = 0;\n\t}\n\n\tint a,b;\n\trep(i,m) {\n\t\tcin >> a >> b;\n\t\ta--;\n\t\tb--;\n\t\tv[a][b] = 1;\n\t\tv[b][a] = 0;\n\t\twin[a]++;\n\t\tlose[b]++;\n\n\n\t}\n\n\tans = 0;\n\n\tf(0,1);\n\n\n\tcout << ans << endl;\n\treturn;\n}\n\n\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n\twhile (cin >> n && n != 0) {\n\t\tsolve();\n\t}\n\treturn 0;\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3372, "score_of_the_acc": -0.0073, "final_rank": 3 }, { "submission_id": "aoj_1627_10235597", "code_snippet": "// (⁠◕⁠ᴗ⁠◕⁠✿⁠)\n\n// #pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n#define rep(i, n) for (ll i = 0; i < (n); i++)\n#define srep(i, s, n) for (ll i = s; i < (n); i++)\n#define len(x) ((int)(x).size())\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\ntemplate<typename T> using vc = vector<T>;\ntemplate<typename T> using vv = vc<vc<T>>;\ntemplate<typename T> using vvv = vv<vc<T>>;\nusing vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;\nusing ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;\nusing ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nconst ld pi = acos(-1.0);\nconst int inf = 0x3f3f3f3f;\nconst ll INF = 0x3f3f3f3f3f3f3f3f;\n// const ll mod = 1000000007;\nconst ll mod = 998244353;\ninline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }\n\n#define debug(var) do{std::cout << #var << \" : \\n\";view(var);}while(0)\ntemplate<typename T> void view(T e){cout << e << endl;}\ntemplate<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << \" \"; } cout << endl;}\ntemplate<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }\n\nint N;\nint winlose[9 * 9];\nint win[9], lose[9];\n\nint f(int i, int j){\n // cout << i << \" \" << j << endl;\n // rep(ni, N) cout << win[ni] << \" \";\n // cout << endl;\n // rep(ni, N) cout << lose[ni] << \" \";\n // cout << endl;\n if (i == N - 1) return 1;\n int ret = 0;\n if (winlose[i * N + j] != 0){\n win[i]++;\n lose[j]++;\n if (win[i] <= N / 2 && lose[j] <= N / 2){\n int ni = i, nj = j + 1;\n if (nj == N){\n ni++;\n nj = ni + 1;\n }\n ret += f(ni, nj);\n }\n win[i]--;\n lose[j]--;\n }\n if (winlose[i * N + j] != 1){\n win[j]++;\n lose[i]++;\n if (win[j] <= N / 2 && lose[i] <= N / 2){\n int ni = i, nj = j + 1;\n if (nj == N){\n ni++;\n nj = ni + 1;\n }\n ret += f(ni, nj);\n }\n win[j]--;\n lose[i]--;\n }\n return ret;\n}\nvoid solve(){\n int M; cin >> M;\n rep(i, M){\n int x, y; cin >> x >> y; x--; y--;\n if (x < y) winlose[x * N + y] = 1;\n else winlose[y * N + x] = 0;\n }\n cout << f(0, 1) << endl;\n}\n\nint main(){\n while (true){\n cin >> N;\n if (N == 0) break;\n rep(i, N * N) winlose[i] = -1;\n rep(i, N){\n win[i] = 0;\n lose[i] = 0;\n }\n solve();\n }\n}", "accuracy": 1, "time_ms": 60, "memory_kb": 3316, "score_of_the_acc": -0.0022, "final_rank": 1 }, { "submission_id": "aoj_1627_9769611", "code_snippet": "#if !defined(__clang__) && defined(__GNUC__)\n#include <bits/stdc++.h>\n#pragma GCC optimize(\"O3,unroll-loops\")\n#ifdef ONLINE_JUDGE\n#pragma GCC target(\"avx2,fma,popcnt,bmi,bmi2\")\n#endif\n#elif defined(__clang__)\n#include <algorithm>\n#include <bitset>\n#include <cassert>\n#include <chrono>\n#include <cmath>\n#include <complex>\n#include <deque>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <iostream>\n#include <limits>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <random>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <string>\n#include <tuple>\n#include <unordered_map>\n#include <unordered_set>\n#include <vector>\n#else\n#error \"We don't know this compiler lol\"\n#endif\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\nstd::ostream &operator<<(std::ostream &os, const modint998244353 &a) {\n return os << a.val();\n}\nstd::ostream &operator<<(std::ostream &os, const modint1000000007 &a) {\n return os << a.val();\n}\nstd::istream &operator>>(std::istream &is, modint998244353 &a) {\n long long t;\n is >> t;\n a = t;\n return is;\n}\nstd::istream &operator>>(std::istream &is, modint1000000007 &a) {\n long long t;\n is >> t;\n a = t;\n return is;\n}\n#endif\nusing namespace std;\n// # Type Aliases\nusing ll = long long;\nusing ull = unsigned long long;\nusing ld = long double;\ntemplate <class T>\nusing V = vector<T>;\ntemplate <class T, class U>\nusing P = pair<T, U>;\ntemplate <class T>\nusing max_heap = priority_queue<T>;\ntemplate <class T>\nusing min_heap = priority_queue<T, vector<T>, greater<T>>;\n// # Repeat Macros\n#define OVERLOADER(_1, _2, _3, NAME, ...) NAME\n#define REP2(i, a, b) for (ll i = (a); i < (ll)(b); ++i)\n#define REP(i, n) REP2(i, 0, n)\n#define rep(...) OVERLOADER(__VA_ARGS__, REP2, REP)(__VA_ARGS__)\n#define repd(i, a, b) for (ll i = (ll)(b) - 1; i >= (ll)(a); --i)\n// # Abbreviation Macros\n#define pb push_back\n#define eb emplace_back\n#define ALL(a) begin(a), end(a)\n#define RALL(a) a.rbegin(), a.rend()\n// # Fast IO and IO Settings\nstruct IO_Setting {\n IO_Setting() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n cout << fixed << setprecision(15);\n cerr << fixed << setprecision(3);\n }\n} io_setting;\n// # Input Overloads\ntemplate <class T, class U>\nistream &operator>>(istream &is, P<T, U> &p) {\n return is >> p.first >> p.second;\n}\ntemplate <class T>\nistream &operator>>(istream &is, V<T> &v) {\n for (T &e : v) is >> e;\n return is;\n}\n// # Output Overloads\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const P<T, U> &p) {\n return os << p.first << ' ' << p.second;\n}\ntemplate <class T>\nostream &operator<<(ostream &os, const V<T> &v) {\n int ss = v.size();\n rep(ii, ss) { os << v[ii] << (ii + 1 == ss ? \"\" : \" \"); }\n return os;\n}\n// # Function Definition\ntemplate <class T>\nT sq(T x) {\n return x * x;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\nll max(ll lhs, ll rhs) { return std::max<ll>(lhs, rhs); }\nll min(ll lhs, ll rhs) { return std::min<ll>(lhs, rhs); }\nll ceil_div(ll a, ll b) { return a / b + ((a ^ b) >= 0 && a % b); }\nll floor_div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }\ntemplate <class Container>\nContainer reversed(const Container &v) {\n return Container(v.rbegin(), v.rend());\n}\ntemplate <class Container>\nContainer sorted(const Container &v, bool rev = false) {\n Container r = v;\n rev ? sort(r.rbegin(), r.rend()) : sort(r.begin(), r.end());\n return r;\n}\ntemplate <class T>\nvoid uniqify(V<T> &v) {\n v.erase(unique(ALL(v)), v.end());\n}\ntemplate <class T>\nV<T> compress(V<T> &v) {\n V<T> vals = v;\n sort(ALL(vals));\n uniqify(vals);\n rep(i, v.size()) v[i] = lower_bound(ALL(vals), v[i]) - vals.begin();\n return vals;\n}\ntemplate <class T>\nV<T> sum_array(const V<T> &v) {\n int n = v.size();\n V<T> sum(n + 1);\n rep(i, n) sum[i + 1] = sum[i] + v[i];\n return sum;\n}\ntemplate <class T>\ninline void drop(T &&x) {\n cout << x << endl;\n exit(0);\n}\ninline void yesno(bool f) { cout << (f ? \"Yes\" : \"No\") << '\\n'; }\n// # Constant Definition\nconstexpr char ENDL = '\\n';\nconstexpr ll INF = (1LL << 30) - 1; // 問題毎にfit\nconstexpr ll INFLL = (1LL << 62) - 1; // 問題毎にfit\nconst ld PI = acos(static_cast<long double>(-1));\nconstexpr int dy[] = {0, 1, 0, -1};\nconstexpr int dx[] = {1, 0, -1, 0};\n// constexpr int dy[]={0, 1, 1, 1, 0, -1, -1, -1};\n// constexpr int dx[]={1, 1, 0, -1, -1, -1, 0, 1};\n// # Debug Macro\n#ifdef LOCAL\n// #include \"CP-library/debug_print.hpp\"\n// #define debug(...) debug_impl(__LINE__, __VA_ARGS__)\n// #define debug_impl(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)\n#include \"cpp-dump/cpp-dump.hpp\"\n#define dump(...) cpp_dump(__VA_ARGS__)\n#if __has_include(<atcoder/modint>)\n#include <atcoder/modint>\nnamespace cpp_dump::_detail {\ntemplate <int m>\ninline std::string export_var(const atcoder::static_modint<m> &mint,\n const std::string &indent,\n std::size_t last_line_length,\n std::size_t current_depth, bool fail_on_newline,\n const export_command &command) {\n return export_var(mint.val(), indent, last_line_length, current_depth,\n fail_on_newline, command);\n}\ntemplate <int m>\ninline std::string export_var(const atcoder::dynamic_modint<m> &mint,\n const std::string &indent,\n std::size_t last_line_length,\n std::size_t current_depth, bool fail_on_newline,\n const export_command &command) {\n return export_var(mint.val(), indent, last_line_length, current_depth,\n fail_on_newline, command);\n}\n} // namespace cpp_dump::_detail\n#endif // __has_include(<atcoder/modint>)\nnamespace cp = cpp_dump;\nCPP_DUMP_SET_OPTION_GLOBAL(max_line_width, 100);\nCPP_DUMP_SET_OPTION_GLOBAL(log_label_func, cp::log_label::line());\n// CPP_DUMP_DEFINE_EXPORT_OBJECT(Type, properties..., methods...);\n#define INLOCALDO(...) \\\n do { \\\n __VA_ARGS__ \\\n } while (false)\n#else\n#define debug(...)\n#define dump(...)\n#define INLOCAL(...)\n#endif\n// using mint = modint1000000007;\n// using mint = modint998244353;\n\nint main() {\n while (true) {\n ll n;\n cin >> n;\n if (n == 0) break;\n ll m;\n cin >> m;\n array<array<int, 9>, 9> res{};\n rep(i, n) rep(j, n) res[i][j] = -1;\n rep(i, m) {\n ll x, y;\n cin >> x >> y;\n x--;\n y--;\n int b = 1;\n if (x > y) swap(x, y), b = 1 - b;\n res[x][y] = b;\n }\n using Arr = array<ll, 9>;\n using mp = map<array<ll, 9>, ll>;\n mp dp;\n ll rem = n * (n - 1) / 2;\n auto ok = [&n, &rem](const Arr& arr) -> bool {\n ll mx = *std::max_element(ALL(arr));\n ll def = 0;\n rep(i, n) def += mx - arr[i];\n return def <= rem;\n };\n dp[{0, 0, 0, 0, 0, 0, 0, 0, 0}] = 1;\n {\n int i = 0, j = 1;\n while (i != n - 1) {\n // dump(i, j, dp.size());\n mp ndp;\n rem--;\n for (auto p : dp) {\n auto arr = p.first;\n auto val = p.second;\n // i が かつ\n if (res[i][j] != 0) {\n arr[i]++;\n if(ok(arr)) ndp[arr] += val;\n arr[i]--;\n }\n // i が j に負ける\n if (res[i][j] != 1) {\n arr[j]++;\n if(ok(arr)) ndp[arr] += val;\n arr[j]--;\n }\n }\n swap(ndp, dp);\n\n j++;\n if (j == n) {\n i++;\n j = i + 1;\n }\n }\n }\n ll ans = 0;\n for (auto [key, val] : dp) {\n bool f = true;\n rep(i, n - 1) {\n if(key[i] != key[i+1]) f = false;\n }\n if(f) ans += val;\n }\n cout << ans << endl;\n }\n}", "accuracy": 1, "time_ms": 150, "memory_kb": 10428, "score_of_the_acc": -0.6748, "final_rank": 18 } ]
aoj_1626_cpp
Skyscraper "MinatoHarukas" Mr. Port plans to start a new business renting one or more floors of the new skyscraper with one giga floors, MinatoHarukas. He wants to rent as many vertically adjacent floors as possible, because he wants to show advertisement on as many vertically adjacent windows as possible. The rent for one floor is proportional to the floor number, that is, the rent per month for the n -th floor is n times that of the first floor. Here, the ground floor is called the first floor in the American style, and basement floors are out of consideration for the renting. In order to help Mr. Port, you should write a program that computes the vertically adjacent floors satisfying his requirement and whose total rental cost per month is exactly equal to his budget. For example, when his budget is 15 units, with one unit being the rent of the first floor, there are four possible rent plans, 1+2+3+4+5, 4+5+6, 7+8, and 15. For all of them, the sums are equal to 15. Of course in this example the rent of maximal number of the floors is that of 1+2+3+4+5, that is, the rent from the first floor to the fifth floor. Input The input consists of multiple datasets, each in the following format. b A dataset consists of one line, the budget of Mr. Port b as multiples of the rent of the first floor. b is a positive integer satisfying 1 < b < 10 9 . The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 1000. Output For each dataset, output a single line containing two positive integers representing the plan with the maximal number of vertically adjacent floors with its rent price exactly equal to the budget of Mr. Port. The first should be the lowest floor number and the second should be the number of floors. Sample Input 15 16 2 3 9699690 223092870 847288609 900660121 987698769 999999999 0 Output for the Sample Input 1 5 16 1 2 1 1 2 16 4389 129 20995 4112949 206 15006 30011 46887 17718 163837 5994
[ { "submission_id": "aoj_1626_10867577", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std ;\nint ans , p , n ;\nvoid deal( int x ){\n if ( ( n / x ) % 2 == 1 ){\n int y = x - ( n / x ) / 2 ;\n if ( y > 0 ){\n if ( n / x > ans ){\n ans = n / x ;\n p = y ;\n }\n }\n }\n if ( x > 1 && x % 2 == 1 ){\n int y = x / 2 - n / x + 1 ;\n if ( 2 * n / x > ans && y > 0 ){\n ans = 2 * n / x ;\n p = y ;\n }\n }\n}\nint main(){\n while( 1 ){\n scanf( \"%d\" , &n ) ;\n if ( n == 0 ) break ;\n ans = 0 ; p = 0 ;\n for( int i = 1 ; i <= sqrt( n ) ; i++ ){\n if ( n % i == 0 ){\n deal( i ) ; deal( n / i ) ;\n }\n }\n printf( \"%d %d\\n\" , p , ans ) ;\n }\n return 0 ;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3508, "score_of_the_acc": -0.8031, "final_rank": 13 }, { "submission_id": "aoj_1626_10676087", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nvoid solve(ll b){\n pair<ll,ll> ans;\n\n for (ll i = 1; i*i <= 2*b; i++){\n if (2*b%i)continue;\n\n ll n = i;\n ll m = 2*b / n;\n if ((m-n+1)%2)continue;\n\n ll l = (m-n+1)/2;\n ans.first = l;\n ans.second = i;\n }\n cout << ans.first << ' ' << ans.second << endl;\n}\n\nint main(){\n ll b;cin >> b;\n while(b){\n solve(b);\n cin >> b;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3436, "score_of_the_acc": -0.6212, "final_rank": 8 }, { "submission_id": "aoj_1626_10643907", "code_snippet": "#pragma GCC target(\"avx\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n#include<bits/stdc++.h>\n#define fast std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr)\n#define eb emplace_back\n#define mp make_pair\n#define all(x) (x).begin(), (x).end()\nusing namespace std;\nusing ll = long long;\nusing u64 = uint64_t;\nusing u128 = __uint128_t;\nusing ull = unsigned long long;\nconstexpr ll inf = 2e18;\nconstexpr ll mod = 998244353;\n#ifdef LOCAL\n#define debug(x) std::cerr << #x << \" = \" << (x) << std::endl;\n#else\n#define debug(x)\n#endif\nstatic void judge(bool c) {\n std::cout << (c ? \"Yes\" : \"No\") << '\\n';\n}\n\n\nll b;\nll isok(ll x){\n if(x * x > b) return 0;\n ll c = b - x * x;\n if(c % x) return 0;\n c /= x;\n c++;\n if(c % 2) return 0;\n c /= 2;\n assert(x * x + (2 * c - 1) * x == b);\n return c;\n}\nint main() {\n fast;\n while(1){\n ll ans = -1,st = -1;\n cin >> b;\n b *= 2;\n if(b == 0) return 0;\n for(ll i = 1; i * i <= b; i++){\n if(b % i == 0){\n if(isok(i)) ans = max(ans,i),st = isok(i);\n }\n }\n assert(ans != -1 && st != -1);\n cout << st << \" \"<< ans << endl;\n }\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3384, "score_of_the_acc": -0.4783, "final_rank": 4 }, { "submission_id": "aoj_1626_10639591", "code_snippet": "// C++ includes used for precompiling -*- C++ -*-\n// Copyright (C) 2003-2013 Free Software Foundation, Inc.\n//\n// This file is part of the GNU ISO C++ Library. This library is free\n// software; you can redistribute it and/or modify it under the\n// terms of the GNU General Public License as published by the\n// Free Software Foundation; either version 3, or (at your option)\n// any later version.\n// This library is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n// Under Section 7 of GPL version 3, you are granted additional\n// permissions described in the GCC Runtime Library Exception, version\n// 3.1, as published by the Free Software Foundation.\n// You should have received a copy of the GNU General Public License and\n// a copy of the GCC Runtime Library Exception along with this program;\n// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see\n// <http://w...content-available-to-author-only...u.org/licenses/>.\n/** @file stdc++.h\n * This is an implementation file for a precompiled header.\n */\n// 17.4.1.2 Headers\n// C\n#ifndef _GLIBCXX_NO_ASSERT\n#include <cassert>\n#endif\n#include <cctype>\n#include <cerrno>\n#include <cfloat>\n#include <ciso646>\n#include <climits>\n#include <clocale>\n#include <cmath>\n#include <csetjmp>\n#include <csignal>\n#include <cstdarg>\n#include <cstddef>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <ctime>\n#if __cplusplus >= 201103L\n#include <ccomplex>\n#include <cfenv>\n#include <cinttypes>\n#include <cstdbool>\n#include <cstdint>\n#include <ctgmath>\n#include <cwchar>\n#include <cwctype>\n#endif\n// C++\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <deque>\n#include <exception>\n#include <fstream>\n#include <functional>\n#include <iomanip>\n#include <ios>\n#include <iosfwd>\n#include <iostream>\n#include <istream>\n#include <iterator>\n#include <limits>\n#include <list>\n#include <locale>\n#include <map>\n#include <memory>\n#include <new>\n#include <numeric>\n#include <ostream>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\n#include <stdexcept>\n#include <streambuf>\n#include <string>\n#include <typeinfo>\n#include <utility>\n#include <valarray>\n#include <vector>\n#if __cplusplus >= 201103L\n#include <array>\n#include <atomic>\n#include <chrono>\n#include <condition_variable>\n#include <forward_list>\n#include <future>\n#include <initializer_list>\n#include <mutex>\n#include <random>\n#include <ratio>\n#include <regex>\n#include <scoped_allocator>\n#include <system_error>\n#include <thread>\n#include <tuple>\n#include <type_traits>\n#include <typeindex>\n#include <unordered_map>\n#include <unordered_set>\n\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\n#define rep(i, n) for (int i = 0; i < n; i++)\n#define all(v) v.begin(), v.end()\ntemplate <class T, class U>\ninline bool chmax(T &a, U b) {\n if (a < b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T, class U>\ninline bool chmin(T &a, U b) {\n if (a > b) {\n a = b;\n return true;\n }\n return false;\n}\ntemplate <class T>\ninline void compress(vector<T> &a) {\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n}\ntemplate <class T>\nT rand(T l, T r) {\n static mt19937 mt(random_device{}());\n // [l, r)\n if constexpr (is_integral_v<T>) {\n return uniform_int_distribution<T>(l, r - 1)(mt);\n } else if constexpr (is_floating_point_v<T>) {\n return uniform_real_distribution<T>(l, r)(mt);\n }\n}\nconstexpr int INF = 1001001001;\nconstexpr ll llINF = 3000000000000000010;\nstruct linear_sieve {\n vector<int> least_factor, prime_list;\n linear_sieve(int n) : least_factor(n + 1, 0) {\n for (int i = 2; i <= n; i++) {\n if (least_factor[i] == 0) {\n least_factor[i] = i;\n prime_list.push_back(i);\n }\n for (int p : prime_list) {\n if (ll(i) * p > n || p > least_factor[i]) break;\n least_factor[i * p] = p;\n }\n }\n }\n};\ntemplate <int modulo>\nstruct modint {\n int x;\n modint() : x(0) {}\n modint(int64_t y) : x(y >= 0 ? y % modulo : (modulo - (-y) % modulo) % modulo) {}\n modint &operator+=(const modint &p) {\n if ((x += p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator-=(const modint &p) {\n if ((x += modulo - p.x) >= modulo) x -= modulo;\n return *this;\n }\n modint &operator*=(const modint &p) {\n x = (int)(1LL * x * p.x % modulo);\n return *this;\n }\n modint &operator/=(const modint &p) {\n *this *= p.inv();\n return *this;\n }\n modint operator-() const { return modint(-x); }\n modint operator+(const modint &p) const { return modint(*this) += p; }\n modint operator-(const modint &p) const { return modint(*this) -= p; }\n modint operator*(const modint &p) const { return modint(*this) *= p; }\n modint operator/(const modint &p) const { return modint(*this) /= p; }\n bool operator==(const modint &p) const { return x == p.x; }\n bool operator!=(const modint &p) const { return x != p.x; }\n modint inv() const {\n int a = x, b = modulo, u = 1, v = 0, t;\n while (b > 0) {\n t = a / b;\n swap(a -= t * b, b);\n swap(u -= t * v, v);\n }\n return modint(u);\n }\n modint pow(int64_t n) const {\n modint ret(1), mul(x);\n while (n > 0) {\n if (n & 1) ret *= mul;\n mul *= mul;\n n >>= 1;\n }\n return ret;\n }\n friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }\n friend istream &operator>>(istream &is, modint &a) {\n int64_t t;\n is >> t;\n a = modint<modulo>(t);\n return (is);\n }\n int val() const { return x; }\n static constexpr int mod() { return modulo; }\n static constexpr int half() { return (modulo + 1) >> 1; }\n};\nll extgcd(ll a, ll b, ll &x, ll &y) {\n // ax+by=gcd(|a|,|b|)\n if (a < 0 || b < 0) {\n ll d = extgcd(abs(a), abs(b), x, y);\n if (a < 0) x = -x;\n if (b < 0) y = -y;\n return d;\n }\n if (b == 0) {\n x = 1;\n y = 0;\n return a;\n }\n ll d = extgcd(b, a % b, y, x);\n y -= a / b * x;\n return d;\n}\ntemplate <typename T>\nstruct Binomial {\n vector<T> inv, fact, factinv;\n Binomial(int n) {\n inv.resize(n + 1);\n fact.resize(n + 1);\n factinv.resize(n + 1);\n inv[0] = fact[0] = factinv[0] = 1;\n for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i;\n factinv[n] = fact[n].inv();\n inv[n] = fact[n - 1] * factinv[n];\n for (int i = n - 1; i >= 1; i--) {\n factinv[i] = factinv[i + 1] * (i + 1);\n inv[i] = fact[i - 1] * factinv[i];\n }\n }\n T C(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r] * factinv[r];\n }\n T P(int n, int r) {\n if (n < 0 || n < r || r < 0) return 0;\n return fact[n] * factinv[n - r];\n }\n T H(int n, int r) {\n if (n == 0 && r == 0) return 1;\n if (n < 0 || r < 0) return 0;\n return r == 0 ? 1 : C(n + r - 1, r);\n }\n};\ntemplate <class T>\nvector<T> dijkstra(vector<vector<pair<int, T>>> &g, vector<int> start) {\n using P = pair<T, int>;\n vector<T> dp(g.size(), numeric_limits<T>::max());\n priority_queue<P, vector<P>, greater<P>> que;\n for (int s : start) {\n dp[s] = 0;\n que.push({0, s});\n }\n while (que.size()) {\n auto [d, v] = que.top();\n que.pop();\n if (dp[v] != d) continue;\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], d + c)) que.push({dp[u], u});\n }\n }\n return dp;\n}\n\nvector<int> BFS01(vector<vector<pair<int, int>>> &g, vector<int> start) {\n vector<int> dp(g.size(), numeric_limits<int>::max());\n deque<int> que;\n for (int s : start) {\n dp[s] = 0;\n que.push_front(s);\n }\n while (que.size()) {\n auto v = que.front();\n que.pop_front();\n for (auto [u, c] : g[v]) {\n if (chmin(dp[u], dp[v] + c)) {\n if (c == 0)\n que.push_front(u);\n else\n que.push_back(u);\n }\n }\n }\n return dp;\n}\ntemplate <typename T, typename U>\ninline istream &operator>>(istream &is, pair<T, U> &rhs) {\n return is >> rhs.first >> rhs.second;\n}\ntemplate <typename T>\ninline istream &operator>>(istream &is, vector<T> &v) {\n for (auto &e : v) is >> e;\n return is;\n}\ntemplate <typename T, typename U>\ninline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {\n return os << rhs.first << \" \" << rhs.second;\n}\ntemplate <typename T>\ninline ostream &operator<<(ostream &os, const vector<T> &v) {\n for (auto itr = v.begin(), end_itr = v.end(); itr != end_itr;) {\n os << *itr;\n if (++itr != end_itr) os << \" \";\n }\n return os;\n}\n\nstruct UnionFind {\n vector<int> par, siz;\n UnionFind(int x) {\n par.resize(x);\n siz.resize(x);\n for (int i = 0; i < x; i++) {\n par[i] = i;\n siz[i] = 1;\n }\n }\n int find(int x) {\n if (par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n bool unite(int x, int y) {\n x = find(x), y = find(y);\n if (x == y) return false;\n if (siz[x] < siz[y]) swap(x, y);\n par[y] = x;\n siz[x] += siz[y];\n\n return true;\n }\n bool same(int x, int y) { return find(x) == find(y); }\n int size(int x) { return siz[find(x)]; }\n};\ntemplate <class T>\nstruct BIT {\n // 1-indexed\n int n, beki = 1;\n vector<T> bit;\n BIT(int x) {\n bit.resize(x + 1, 0);\n n = x;\n while (beki * 2 <= n) beki *= 2;\n }\n T sum(int i) {\n T res = 0;\n while (i > 0) {\n res += bit[i];\n i -= i & -i;\n }\n return res;\n }\n T sum(int l, int r) {\n //[l,r]\n return sum(r) - (l == 0 ? 0 : sum(l - 1));\n }\n void add(int i, T x) {\n while (i <= n) {\n bit[i] += x;\n i += i & -i;\n }\n }\n int lowerbound(T w) {\n if (w <= 0) return 0;\n int x = 0;\n for (int k = beki; k > 0; k >>= 1) {\n if (x + k <= n && bit[x + k] < w) {\n w -= bit[x + k];\n x += k;\n }\n }\n return x + 1;\n }\n};\nusing mint = modint<998244353>;\n\nvoid solve() {\n ll b;\n cin>>b;\n if(b==0){\n exit(0);\n }\n b*=2;\n ll ans1=-1,ans2=-1;\n for(ll i=1;i*i<=b;i++){\n if(b%i==0){\n ll j=b/i;\n j+=1;\n if(i%2!=j%2)continue;\n ans1=(j-i)/2,ans2=(i+j)/2;\n }\n }\n cout<<ans1<<\" \"<<ans2-ans1<<endl;\n}\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n int t = 1;\n // cin >> t;\n while (1) solve();\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3584, "score_of_the_acc": -1.0079, "final_rank": 16 }, { "submission_id": "aoj_1626_10625065", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#if __has_include(<atcoder/all>)\n#include <atcoder/all>\nusing namespace atcoder;\n#endif\n#define ll long long\n#define vi vector<int>\n#define vvi vector<vector<int>>\n#define vvvi vector<vector<vector<int>>>\n#define vd vector<double>\n#define vvd vector<vector<double>>\n#define vvvd vector<vector<vector<double>>>\n#define vll vector<long long>\n#define vvll vector<vector<long long>>\n#define vvvll vector<vector<vector<long long>>>\n#define vmi vector<mint>\n#define vvmi vector<vector<mint>>\n#define vb vector<bool>\n#define vs vector<string>\n#define vc vector<char>\n#define vvc vector<vector<char>>\n#define pi pair<int,int>\n#define pll pair<long long, long long>\n#define vpi vector<pair<int,int>>\n#define vvpi vector<vector<pair<int,int>>>\n#define gi greater<int>\n#define gll greater<long long>\n#define gpi greater<pair<int,int>>\n#define eb emplace_back\n#define ef emplace_front\n#define pq priority_queue\n\nll sum(ll n){return n*(n+1)/2;}\n\nll largestCount(ll n){\n ll left = 0;\n ll right = LLONG_MAX;\n while(left<right){\n ll mid = right - (right - left) / 2;\n if(sum(mid)>n)right = mid - 1;\n else if(sum(mid)<n)left = mid + 1;\n else if(sum(mid)==n) return mid;\n }\n return left;\n}\n\nvoid solve() {\n while(true){\n ll n; cin >> n;\n if(n==0)return;\n ll cnt = 0;\n ll m = 0;\n for (int i=1; i<sqrt(2 * n) + 1; i++)\n {\n ll tmp = cnt;\n if (i % 2 == 0){\n cnt += (n % i == i / 2);\n }\n else{\n cnt += (n % i == 0);\n }\n if(tmp != cnt){\n m = i;\n }\n }\n if ((2 * n / m - m - 1) / 2 + 1 == 0){\n cout << 1 << ' ' << m - 1 << endl;\n }else{\n cout << (2 * n / m - m - 1) / 2 + 1 << ' ' << m << endl;\n }\n }\n}\n\nint main() {\n cin.tie(nullptr);\n ios::sync_with_stdio(false);\n solve();\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3220, "score_of_the_acc": -0.0317, "final_rank": 1 }, { "submission_id": "aoj_1626_10625054", "code_snippet": "#include <iostream>\n#include <cmath>\nusing namespace std;\n\nint main(){\n\n while(true){\n int b;\n cin >> b;\n if(b == 0) break;\n for(int i = (int)sqrt(2*b); i >= 1; i--){\n if((2*b)%i == 0 && (2*b/i - i+1)/2 >= 1 && (2*b/i - i+1)%2 == 0){\n cout << (2*b/i - i+1)/2 << \" \" << i << endl;\n break;\n }\n }\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 10, "memory_kb": 3288, "score_of_the_acc": -0.1868, "final_rank": 2 }, { "submission_id": "aoj_1626_10624772", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define ALL(v) (v).begin(), (v).end()\nusing ll = long long;\n\nint main() {\n while (true) {\n ll B; cin >> B;\n if (B == 0) break;\n B <<= 1;\n vector<ll> div;\n for (ll i = 1; i*i <= B; i++) {\n if (B % i == 0) {\n div.emplace_back(i);\n if (B / i != i) div.emplace_back(B / i);\n }\n }\n sort(ALL(div));\n ll len = 0;\n ll lans = -1, rans = -1;\n for (auto m : div) {\n ll x = B / m;\n for (ll i = 1; i*i <= x; i++) {\n if (x % i == 0) {\n {\n ll n = x / i;\n ll r = (i + n - 1);\n ll l = (i - n + 1);\n if ((l & 1) || (r & 1)) continue;\n l >>= 1;\n r >>= 1;\n if (1 <= l && l <= r && n > len) {\n len = n;\n lans = l;\n rans = r;\n }\n }\n {\n ll n = i;\n ll r = (x / i + n - 1);\n ll l = (x / i - n + 1);\n if ((l & 1) || (r & 1)) continue;\n l >>= 1;\n r >>= 1;\n if (1 <= l && l <= r && n > len) {\n len = n;\n lans = l;\n rans = r;\n }\n }\n }\n }\n }\n cout << lans << \" \" << len << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 130, "memory_kb": 3456, "score_of_the_acc": -0.696, "final_rank": 11 }, { "submission_id": "aoj_1626_10620654", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)\n#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n\n#define TT template<typename T>\nTT using vec = vector<T>;\ntemplate<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }\ntemplate<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }\n\nstruct io_setup {\n io_setup() {\n ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n cout << fixed << setprecision(15);\n }\n} io_setup;\n\nint main(){\n while(1){\n ll B;\n cin>>B;\n if(B==0)return 0;\n B*=2;\n ll nl=0,ns=0;\n for(ll d=1;d*d<=B;d++){\n if(B%d!=0)continue;\n ll e=B/d;\n if((d-e+1)%2!=0)continue;\n ll r=(d+e-1)/2;\n ll l=e-r;\n if(l<=r){\n if(chmax(ns,r-l+1)){\n nl=l;\n }\n }\n }\n cout<<nl<<\" \"<<ns<<endl;\n }\n}", "accuracy": 1, "time_ms": 30, "memory_kb": 3584, "score_of_the_acc": -1.0079, "final_rank": 16 }, { "submission_id": "aoj_1626_10613233", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l, const A &r) {\n if(l < r)\n l = r;\n}\n\nll mod = 998244353;\n\nvector<char> isprime;\nvoid init() {\n isprime.assign(1000005, 1);\n isprime[0] = 0;\n isprime[1] = 0;\n for(int i = 2; i < 1000005; i++) {\n if(isprime[i]) {\n for(int j = i * 2; j < 1000005; j += i) {\n isprime[j] = 0;\n }\n }\n }\n}\n\nll n;\nvector<a2> v;\nvoid input() { cin >> n; }\n\nvoid solve() {\n ll x=n,y=1;\n for(ll i = 2; i <= 100000; i++) {\n ll a = (i + 1) * i / 2;\n\n if(a <= n && (n - a) % i == 0) {\n x = (n-a)/i + 1;\n y = i;\n }\n }\n cout << x << ' ' << y << endl;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n // init();\n while(1) {\n input();\n if(n == 0)\n break;\n solve();\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 170, "memory_kb": 3460, "score_of_the_acc": -0.7228, "final_rank": 12 }, { "submission_id": "aoj_1626_10611962", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#include <atcoder/all>\nusing namespace atcoder;\nusing ll = long long;\nusing str = string;\nusing un = unsigned;\nusing mint = modint998244353;\nstr Y = \"Yes\", N = \"No\";\nll MOD=1e+9 + 7,MOD2=998244353;\nll fast_pow(ll a,ll b){if (b < 0)return 0;ll res = 1;while (b > 0){if (b & 1)res = (res * a) % MOD;a = (a * a) % MOD;b = b / 2;}return res % MOD;};\nll fact(ll n){if (n == 0 || n == 1)return 1;return (n * fact(n - 1)) % MOD;};\nll nCr(ll n, ll r){return ((fact(n) * fast_pow(fact(r), MOD - 2)) % MOD * fast_pow(fact(n - r), MOD - 2)) % MOD;};\nvector<bool> sieve(int n) {vector<bool> is_prime(n + 1, true);is_prime[0] = is_prime[1] = false;for (int i = 2; i * i <= n; ++i) {if (is_prime[i]) {for (int j = i * i; j <= n; j += i) {is_prime[j] = false;}}}return is_prime;};\n//#define _GLIBCXX_DEBUG\n#define rep(i, n) for (ll i = 0; i < (n); ++i)\n#define repp(i, n) for (ll i = 1; i <= (n); ++i)\ntemplate <class C>\nvoid out_single (const C& v) {cout << v;}\nvoid out_single (ostream& (*func)(ostream)) {cout << func;}\nvoid out_single (const char& v) {cout << v;}\ntemplate <class ...C>\nvoid in (C&&...v) {(cin >> ... >> v);}\ntemplate <class ...C>\nvoid out (C&&...v) {(out_single(v), ...);}\ntemplate <typename T>\nusing vec = vector<T>;\ntemplate <typename T>\nusing vec2 = vector<vec<T>>;\n#define all(a) (a).begin(), (a).end()\n#define rall(a) (a).rbegin(), (a).rend()\n#define sall(a) sort(all(a))\n#define srall(a) sort(rall(a))\nusing V = vec<ll>;\nusing P = pair<ll,ll>;\ntemplate<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; };\ntemplate<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; };\ntemplate<typename T> void print(const vector<T>& v) {for (const auto& i : v) cout << i /*<< \" \"*/; cout << endl;}\ntemplate<typename T> void print(const vector<vector<T>>& v) {for (const auto& i : v) {for (const auto& j : i) cout << j /*<< \" \"*/; cout << endl;}}\nbool is_grid (ll h, ll w, ll y, ll x) {return (0 <= y && y < h && 0 <= x && x < w);}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n while(1){\n ll b;in(b);\n if(b==0) break;\n ll ans=0;\n for(ll i=sqrt(2*b)+2;i>0;i--){\n ll x=2*b/i-(i-1);\n if(x>0&&x%2==0&&(2*b)%i==0){\n out(x/2,\" \",i,\"\\n\");\n break;\n }\n }\n }\n}", "accuracy": 1, "time_ms": 20, "memory_kb": 3584, "score_of_the_acc": -1.004, "final_rank": 15 }, { "submission_id": "aoj_1626_10611778", "code_snippet": "#include <bits/stdc++.h>\n#define rep(i,m,n) for (int i = m; i < (n); i++)\n#define REP(i,m,n) for (int i = m; i <= (n); i++)\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing vi = vector<int>;\nusing vvi = vector<vector<int>>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\nusing vc = vector<char>;\nusing vvc = vector<vector<char>>;\nint sum(int i){\n return i * (i + 1) / 2;\n}\nint main(){\n rep(l,0,1000){\n int b;\n cin >> b;\n if (b == 0) return 0;\n int size=ceil(sqrt(b));\n pii ans = {b, 1};\n int i = 1;\n while(sum(i) <= b){\n if(sum(i)%i==b%i&&((b-sum(i))/i)<b-i){\n ans={((b-sum(i))/i)+1, i};\n }\n i++;\n }\n cout << ans.first << \" \" << ans.second << endl;\n }\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3440, "score_of_the_acc": -0.6163, "final_rank": 7 }, { "submission_id": "aoj_1626_10611357", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// using mint = modint998244353;\n// using mint = modint1000000007;\nusing ll = long long;\nusing ull = unsigned long long;\n#define rep(i, n) for(ll i = 0; i < n; i++)\nconstexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);\n\nint main() {\n cin.tie(nullptr);\n ios_base::sync_with_stdio(false);\n\n // 公差 1 の 1 ~ n 項目までの等差数列の総和\n auto sum = [](ll n) -> ll {\n return n * (n + 1) / 2;\n };\n\n while(true) {\n ll b;\n cin >> b;\n if(b == 0) break;\n ll start = -1, total = -1;\n for(ll w = ((ll) sqrt(b)) * 2; w >= 1; w--) {\n ll left = 1, right = 1e9;\n while(right - left > 2) {\n ll m1 = (left + right) / 2, m2 = m1 + 1;\n ll s1 = sum(m1 + w - 1) - sum(m1 - 1);\n ll s2 = sum(m2 + w - 1) - sum(m2 - 1);\n if(abs(s1 - b) < abs(s2 - b)) right = m1;\n else left = m2;\n }\n // if(w <= 10) cerr << \"w: \" << w << \", left: \" << left << \", right: \" << right << \"\\n\";\n for(ll m = left; m <= right; m++) {\n ll s = sum(m + w - 1) - sum(m - 1);\n if(s == b) {\n start = m;\n total = w;\n break;\n }\n }\n if(total != -1) break;\n }\n cout << start << \" \" << total << \"\\n\";\n }\n\n return 0;\n}", "accuracy": 1, "time_ms": 2530, "memory_kb": 3344, "score_of_the_acc": -1.3407, "final_rank": 19 }, { "submission_id": "aoj_1626_10602971", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\n#include <stdlib.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define all(x) (x).begin(), (x).end()\n//constexpr ll MOD = 1000000007;\nconstexpr ll MOD = 998244353;\nconstexpr int IINF = 1001001001;\nconstexpr ll INF = 1LL<<60;\ntemplate<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}\ntemplate<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}\n\n\nll gcd(ll a, ll b){\n if(a%b == 0){\n return b;\n }else{\n return gcd(b, a%b);\n }\n}\n\nll lcm(ll a, ll b){\n return a*b / gcd(a, b);\n}\n\nll powMod(ll x, ll n) {\n if (n == 0) return 1 % MOD;\n ll val = powMod(x, n / 2);\n val *= val;\n val %= MOD;\n if (n % 2 == 1) val *= x;\n return val % MOD;\n}\n\nint main() {\n while(true){\n ll n; cin >> n;\n if(n == 0) break;\n ll ans = 1;\n ll start = n;\n rep(x,2,sqrt((double)n)*2+2){\n if((2*n)%x != 0) continue;\n ll num = ((2*n)/x-x+1);\n if(num >= 1 && num%2 == 0) ans = x, start = num/2;\n }\n cout << start << \" \" << ans << endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 40, "memory_kb": 3456, "score_of_the_acc": -0.6603, "final_rank": 9 }, { "submission_id": "aoj_1626_10556574", "code_snippet": "#line 1 \"2018C.cpp\"\n#include <bits/stdc++.h>\n#line 1 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\n\n\n#line 6 \"/home/ardririy/repos/cp-libs/libraries-cpp/input.hpp\"\nusing namespace std;\nvector<long long> i64_vec_IN(int n) {vector<long long> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<string> str_vec_IN(int n) { vector<string> res(n); for(int i=0; i<n; i++) { cin >> res[i]; } return res; }\nvector<vector<int>> graph_IN(int n, int m) { vector<vector<int>> g(n); int u, v; for(int i=0; i<m; i++) { cin >> u >> v; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } return g; }\nvector<vector<pair<int, long long>>> weighted_graph_IN(int n, int m) { vector<vector<pair<int, long long>>> g(n); int u, v; long long w; for(int i=0; i<m; i++) { cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } return g; }\n\n\n#line 3 \"2018C.cpp\"\n\nusing namespace std;\n\n// using namespace atcoder;\n\n#ifdef ADRY\n#include <dbg.h>\n#else\n// DO NOTHING\n#define dbg(...)\n#endif\n\n\n#define all(v) v.begin(),v.end()\n#define resort(v) sort(v.rbegin(),v.rend())\nusing ll = long long;\nusing ull = unsigned long long;\nusing vll=vector<ll>;\nusing vvll = vector<vector<ll>>;\nusing P = pair<ll,ll>;\nusing vp=vector<pair<ll, ll>>;\nusing djks=priority_queue<P, vp, greater<P>>;\n\nconst int inf=1ll<<30;\n#define mod10 (ll)1e9+7\n#define mod99 (ll)998244353\nconst double PI = acos(-1);\n\n#define rep(i,n) for (ll i=0;i<(n);++i)\n#define per(i,n) for(ll i=(n)-1;i>=0;--i)\n#define rep2(i,a,n) for (ll i=(a);i<(n);++i)\n#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)\n\n\ntemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }\ntemplate<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }\n\nll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};\nll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};\n\nvll divisors(ll n) {\n vll res;\n rep2(i,1, sqrt(n)+10) {\n if(n%i==0) {\n res.emplace_back(i);\n if(i!=n/i) res.emplace_back(n/i);\n }\n }\n\n return res;\n}\n\nint solve(){\n ll b;\n cin >> b;\n\n if(b==0) return 1;\n vll div = divisors(2*b);\n P ans = {-1,-1};\n for(auto di: div) {\n ll t = 2*b/di;\n ll rhs = t+1-di;\n if(rhs<=0 || rhs%2==1) continue;\n if(chmax(ans.second, di)) {\n ans.first = rhs/2;\n }\n }\n cout << ans.first << \" \" << ans.second << '\\n';\n return 0;\n}\n\nint main() {\n cin.tie(0);\n ios::sync_with_stdio(false);\n while(!solve());\n}", "accuracy": 1, "time_ms": 90, "memory_kb": 3412, "score_of_the_acc": -0.5592, "final_rank": 6 }, { "submission_id": "aoj_1626_10542690", "code_snippet": "#include <bits/stdc++.h>\n#include <atcoder/all>\nusing namespace std;\nusing namespace atcoder;\n#define int long long\n\nint floor_sqrt(int n) {\n int ok = 0;\n int ng = (long double)(1.2)*sqrtl(n)+1;\n while (abs(ok - ng) > 1) {\n int mid = (ok + ng) / 2;\n if (mid*mid<=n) ok = mid;\n else ng = mid;\n }\n return ok;\n}\n\nvoid solve(int b) {\n b *= 2;\n int sq = floor_sqrt(b);\n pair<int, int> ans = {-1, -1};\n for (int i=1; i<=sq; ++i) {\n if (b % i != 0) continue;\n int j = b / i;\n // cout << i << \" x \" << j << \" = \" << b << endl;\n if ((j-i) % 2 == 0) continue;\n int x = (j-i+1) / 2;\n int y = j - x;\n \n //k~l階を買える\n // cout << x << \" ~ \" << y << endl;\n ans = max(ans, {y-x+1, x});\n }\n cout << ans.second << ' ' << ans.first << endl;\n}\n\nsigned main() {\n while(true) {\n int b;\n cin >> b;\n if (b == 0) break;\n solve(b);\n }\n}\n\n/*\nmemo\n\n*/", "accuracy": 1, "time_ms": 30, "memory_kb": 3584, "score_of_the_acc": -1.0079, "final_rank": 16 }, { "submission_id": "aoj_1626_10529939", "code_snippet": "#include<iostream>\n#include<vector>\nusing namespace std;\nusing ll=long long;\nll sum(ll n){return n*(n+1)/2;}\nll cost(ll a,ll n){return sum(a+n-1)-sum(a-1);}\nint main()\n{\n\tios::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\twhile(1)\n\t{\n\t\tint b;cin>>b;\n\t\tif(b==0)break;\n\t\tll ans1=0,ans2=0;\n\t\tfor(ll n=1;n*(n+1)/2<=b;n++)\n\t\t{\n\t\t\tll l=0,r=b+1;\n\t\t\twhile(r-l>1)\n\t\t\t{\n\t\t\t\tll a=(l+r)/2;\n\t\t\t\tll cur=cost(a,n);\n\t\t\t\tif(cur<=b)l=a;\n\t\t\t\telse r=a;\n\t\t\t}\n\t\t\tif(cost(l,n)==b)ans1=l,ans2=n;\n\t\t}\n\t\tcout<<ans1<<\" \"<<ans2<<endl;\n\t}\n}", "accuracy": 1, "time_ms": 1550, "memory_kb": 3584, "score_of_the_acc": -1.6111, "final_rank": 20 }, { "submission_id": "aoj_1626_9902961", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\n\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 2147483647;\n\n//const int MAX_N = 100000;\n//static int vect[MAX_N];\ntypedef pair<int,int> PA;\n//------------------------------------------------------------------------------\n// return {a, n}\nPA solve(int sum)\n{\n // (2*a + n-1)*n = 2*sum\n //--------------------------------------------------------------------------\n // base cases:\n if (sum == 1) return {1, 1};\n else if (sum == 2) return {2, 1};\n else if (sum == 3) return {1, 2};\n //--------------------------------------------------------------------------\n // init:\n sum *= 2;\n //--------------------------------------------------------------------------\n // compute:\n for (int n=sqrt(sum); n>0; --n)\n {\n if (sum%n != 0) continue;\n\n int q = sum/n;\n if (q < n+1) continue;\n\n int aa = q - n + 1;\n if (aa%2 != 0) continue;\n\n return {aa/2, n};\n }\n //--------------------------------------------------------------------------\n // report:\n return {-1, -1};\n}\n\n//------------------------------------------------------------------------------\nvoid test()\n{\n\n}\n\n//------------------------------------------------------------------------------\nint main()\n{\n //test(); return 0;\n //DEBUG = true;\n //--------------------------------------------------------------------------\n int N, K, sum, num;\n while (true)\n {\n num = scanf(\"%d \", &sum); if (sum == 0) break;\n PA p = solve(sum);\n printf(\"%d %d\\n\", p.first, p.second);\n }\n //--------------------------------------------------------------------------\n return 0;\n}\n//------------------------------------------------------------------------------", "accuracy": 1, "time_ms": 10, "memory_kb": 3580, "score_of_the_acc": -0.989, "final_rank": 14 }, { "submission_id": "aoj_1626_9769461", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#if __cplusplus <= 201703L\n#define ssize(v) ((int)(v).size())\ntemplate<typename T> ll bit_ceil(T n) {\n ll r = 1;\n while(r < n) { r <<= 1; }\n return r;\n}\ntemplate<typename T> ll bit_width(T n) { return 64 - __builtin_clzll(n); }\ntemplate<typename T> ll countr_zero(T n) { return __builtin_ctzll(n); }\ntemplate<typename T> ll popcount(T n) { return __builtin_popcountll(n); }\nnamespace ranges {\ntemplate<typename T> void sort(vector<T> &v) { sort(v.begin(), v.end()); }\ntemplate<typename T> void stable_sort(vector<T> &v) { stable_sort(v.begin(), v.end()); }\ntemplate<typename T, typename F> void sort(vector<T> &v, const F &f) { sort(v.begin(), v.end(), f); }\ntemplate<typename T> void reverse(vector<T> &v) { reverse(v.begin(), v.end()); }\ntemplate<typename T> T max(const vector<T> &v) { return *max_element(v.begin(), v.end()); }\ntemplate<typename T> T min(const vector<T> &v) { return *min_element(v.begin(), v.end()); }\ntemplate<typename T, typename U> bool binary_search(const vector<T> &v, U x) { return binary_search(v.begin(), v.end(), x); }\ntemplate<typename T, typename F> bool all_of(const vector<T> &v, const F &f) { return all_of(v.begin(), v.end(), f); }\ntemplate<typename T, typename F> bool any_of(const vector<T> &v, const F &f) { return any_of(v.begin(), v.end(), f); }\ntemplate<typename T, typename F> bool none_of(const vector<T> &v, const F &f) { return none_of(v.begin(), v.end(), f); }\ntemplate<typename T, typename F> int count_if(const vector<T> &v, const F &f) { return count_if(v.begin(), v.end(), f); }\ntemplate<typename T, typename U> auto lower_bound(const vector<T> &v, U x) { return lower_bound(v.begin(), v.end(), x); }\ntemplate<typename T, typename U> auto upper_bound(const vector<T> &v, U x) { return upper_bound(v.begin(), v.end(), x); }\ntemplate<typename T, typename U, typename F> auto lower_bound(const vector<T> &v, U x, const F &f) { return lower_bound(v.begin(), v.end(), x, f); }\ntemplate<typename T, typename U, typename F> auto upper_bound(const vector<T> &v, U x, const F &f) { return upper_bound(v.begin(), v.end(), x, f); }\ntemplate<typename T> auto max_element(const vector<T> &v) { return max_element(v.begin(), v.end()); }\ntemplate<typename T> auto min_element(const vector<T> &v) { return min_element(v.begin(), v.end()); }\ntemplate<typename T, typename U> void fill(vector<T> &v, U x) { fill(v.begin(), v.end(), x); }\n} // namespace ranges\n#endif\n\nvector<ll> Divisor(ll n) {\n vector<ll> r;\n for(ll i = 1; i * i <= n; i++) {\n if(n % i == 0) {\n r.emplace_back(i);\n if(i * i != n) { r.emplace_back(n / i); }\n }\n }\n ranges::sort(r);\n return r;\n}\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n while(true) {\n ll N;\n cin >> N;\n if(!N) { break; }\n\n pair<ll, ll> ans = {1, N};\n for(auto &i : Divisor(N)) {\n if((N / i) & 1) {\n if(i - (N / i) / 2 >= 1) { ans = max(ans, {N / i, i - (N / i) / 2}); }\n }\n if(i & 1 && i / 2 - N / i >= 0) { ans = max(ans, {N / i * 2, i / 2 - N / i + 1}); }\n }\n\n cout << ans.second << \" \" << ans.first << \"\\n\";\n }\n}", "accuracy": 1, "time_ms": 50, "memory_kb": 3376, "score_of_the_acc": -0.4444, "final_rank": 3 }, { "submission_id": "aoj_1626_9473726", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<vvi> vvvi;\ntypedef vector<vvvi> vvvvi;\ntypedef vector<bool> vb;\ntypedef vector<vb> vvb;\ntypedef vector<vvb> vvvb;\ntypedef vector<vvvb> vvvvb;\ntypedef pair<ll,ll> pi;\ntypedef pair<ll,pi> ppi;\n#define FOR(i,l,r) for(ll i=l;i<r;i++)\n#define REP(i,n) FOR(i,0,n)\n#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)\n#define RREP(i,n) RFOR(i,0,n)\n#define sz(A) (ll)(A.size())\n#define ALL(A) A.begin(),A.end()\n#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())\n#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())\n#define COU(A,x) (UB(A,x)-LB(A,x))\n#define F first\n#define S second\ntemplate<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;\ntemplate<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<\" \"<<p.S;return os;}\ntemplate<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}\ntemplate<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?\" \":\"\");return os;}\ntemplate<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}\ntemplate<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}\ntemplate<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}\nconst ll mod=998244353;\ntemplate<const long long int mod=998244353>\nstruct modint{\n using mint=modint<mod>;\n long long int x;\n modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}\n long long int val(){return x;}\n mint&operator=(const mint&a){x=a.x;return *this;}\n mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}\n mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}\n mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}\n friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}\n friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}\n friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}\n mint operator-()const{return mint(0)-*this;}\n mint pow(long long int n){\n if(!n)return 1;\n mint a=1;\n mint _x=x;\n while(n){\n if(n&1)a*=_x;\n _x=_x*_x;n>>=1;\n }\n return a;\n }\n mint inv(){return pow(mod-2);}\n mint&operator/=(mint&a){return *this*=a.inv();}\n friend mint operator/(const mint&a,mint b){return mint(a)/=b;}\n};\nusing mint=modint<998244353>;\nint main(){\n while(1){\n ll N;cin>>N;\n if(!N)return 0;\n set<ll>D;\n FOR(i,1,100000)if(2*N%i==0)D.insert(i),D.insert(2*N/i);\n ll l=N,r=N;\n for(auto d:D){\n if(2*N/d-d+1>0&&(2*N/d-d+1)%2==0)l=(2*N/d-d+1)/2,r=d;\n }\n cout<<l<<\" \"<<r<<endl;\n }\n return 0;\n}", "accuracy": 1, "time_ms": 610, "memory_kb": 3380, "score_of_the_acc": -0.6777, "final_rank": 10 }, { "submission_id": "aoj_1626_9448932", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())\n#define SZ(v) (int)v.size()\n#define MIN(v) *min_element(ALL(v))\n#define MAX(v) *max_element(ALL(v))\n#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())\n#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())\n\nusing uint = unsigned int;\nusing ll = long long int;\nusing ull = unsigned long long;\nusing i128 = __int128_t;\nusing u128 = __uint128_t;\nconst int inf = 0x3fffffff;\nconst ll INF = 0x1fffffffffffffff;\n\ntemplate <typename T> inline bool chmax(T &a, T b) {\n if (a < b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T> inline bool chmin(T &a, T b) {\n if (a > b) {\n a = b;\n return 1;\n }\n return 0;\n}\ntemplate <typename T, typename U> T ceil(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? (x + y - 1) / y : x / y);\n}\ntemplate <typename T, typename U> T floor(T x, U y) {\n assert(y != 0);\n if (y < 0)\n x = -x, y = -y;\n return (x > 0 ? x / y : (x - y + 1) / y);\n}\ntemplate <typename T> int popcnt(T x) {\n return __builtin_popcountll(x);\n}\ntemplate <typename T> int topbit(T x) {\n return (x == 0 ? -1 : 63 - __builtin_clzll(x));\n}\ntemplate <typename T> int lowbit(T x) {\n return (x == 0 ? -1 : __builtin_ctzll(x));\n}\n\ntemplate <class T, class U>\nostream &operator<<(ostream &os, const pair<T, U> &p) {\n os << \"P(\" << p.first << \", \" << p.second << \")\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {\n os << \"{\";\n for (int i = 0; i < vec.size(); i++) {\n os << vec[i] << (i + 1 == vec.size() ? \"\" : \", \");\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T, typename U>\nostream &operator<<(ostream &os, const map<T, U> &map_var) {\n os << \"{\";\n for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {\n os << \"(\" << itr->first << \", \" << itr->second << \")\";\n itr++;\n if (itr != map_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\ntemplate <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {\n os << \"{\";\n for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {\n os << *itr;\n ++itr;\n if (itr != set_var.end())\n os << \", \";\n itr--;\n }\n os << \"}\";\n return os;\n}\n#ifdef LOCAL\n#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)\n#else\n#define show(...) true\n#endif\ntemplate <typename T> void _show(int i, T name) {\n cerr << '\\n';\n}\ntemplate <typename T1, typename T2, typename... T3>\nvoid _show(int i, const T1 &a, const T2 &b, const T3 &...c) {\n for (; a[i] != ',' && a[i] != '\\0'; i++)\n cerr << a[i];\n cerr << \":\" << b << \" \";\n _show(i + 1, a, c...);\n}\n\n#include <unistd.h>\n\nnamespace fastio {\nstatic constexpr uint32_t SZ = 1 << 17;\nchar ibuf[SZ];\nchar obuf[SZ];\nchar out[100];\n// pointer of ibuf, obuf\n\n\nuint32_t pil = 0, pir = 0, por = 0;\n\nstruct Pre {\n char num[10000][4];\n constexpr Pre() : num() {\n for (int i = 0; i < 10000; i++) {\n int n = i;\n for (int j = 3; j >= 0; j--) {\n num[i][j] = n % 10 | '0';\n n /= 10;\n }\n }\n }\n} constexpr pre;\n\ninline void load() {\n memmove(ibuf, ibuf + pil, pir - pil);\n pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);\n pil = 0;\n if (pir < SZ)\n ibuf[pir++] = '\\n';\n}\n\ninline void flush() {\n fwrite(obuf, 1, por, stdout);\n por = 0;\n}\n\nvoid rd(char &c) {\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n}\n\nvoid rd(string &x) {\n x.clear();\n char c;\n do {\n if (pil + 1 > pir)\n load();\n c = ibuf[pil++];\n } while (isspace(c));\n do {\n x += c;\n if (pil == pir)\n load();\n c = ibuf[pil++];\n } while (!isspace(c));\n}\n\ntemplate <typename T> void rd_real(T &x) {\n string s;\n rd(s);\n x = stod(s);\n}\n\ntemplate <typename T> void rd_integer(T &x) {\n if (pil + 100 > pir)\n load();\n char c;\n do\n c = ibuf[pil++];\n while (c < '-');\n bool minus = 0;\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (c == '-') {\n minus = 1, c = ibuf[pil++];\n }\n }\n x = 0;\n while ('0' <= c) {\n x = x * 10 + (c & 15), c = ibuf[pil++];\n }\n if constexpr (is_signed<T>::value || is_same_v<T, i128>) {\n if (minus)\n x = -x;\n }\n}\n\nvoid rd(int &x) {\n rd_integer(x);\n}\nvoid rd(ll &x) {\n rd_integer(x);\n}\nvoid rd(i128 &x) {\n rd_integer(x);\n}\nvoid rd(uint &x) {\n rd_integer(x);\n}\nvoid rd(ull &x) {\n rd_integer(x);\n}\nvoid rd(u128 &x) {\n rd_integer(x);\n}\nvoid rd(double &x) {\n rd_real(x);\n}\nvoid rd(long double &x) {\n rd_real(x);\n}\n\ntemplate <class T, class U> void rd(pair<T, U> &p) {\n return rd(p.first), rd(p.second);\n}\ntemplate <size_t N = 0, typename T> void rd_tuple(T &t) {\n if constexpr (N < std::tuple_size<T>::value) {\n auto &x = std::get<N>(t);\n rd(x);\n rd_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void rd(tuple<T...> &tpl) {\n rd_tuple(tpl);\n}\n\ntemplate <size_t N = 0, typename T> void rd(array<T, N> &x) {\n for (auto &d : x)\n rd(d);\n}\ntemplate <class T> void rd(vector<T> &x) {\n for (auto &d : x)\n rd(d);\n}\n\nvoid read() {}\ntemplate <class H, class... T> void read(H &h, T &...t) {\n rd(h), read(t...);\n}\n\nvoid wt(const char c) {\n if (por == SZ)\n flush();\n obuf[por++] = c;\n}\nvoid wt(const string s) {\n for (char c : s)\n wt(c);\n}\nvoid wt(const char *s) {\n size_t len = strlen(s);\n for (size_t i = 0; i < len; i++)\n wt(s[i]);\n}\n\ntemplate <typename T> void wt_integer(T x) {\n if (por > SZ - 100)\n flush();\n if (x < 0) {\n obuf[por++] = '-', x = -x;\n }\n int outi;\n for (outi = 96; x >= 10000; outi -= 4) {\n memcpy(out + outi, pre.num[x % 10000], 4);\n x /= 10000;\n }\n if (x >= 1000) {\n memcpy(obuf + por, pre.num[x], 4);\n por += 4;\n } else if (x >= 100) {\n memcpy(obuf + por, pre.num[x] + 1, 3);\n por += 3;\n } else if (x >= 10) {\n int q = (x * 103) >> 10;\n obuf[por] = q | '0';\n obuf[por + 1] = (x - q * 10) | '0';\n por += 2;\n } else\n obuf[por++] = x | '0';\n memcpy(obuf + por, out + outi + 4, 96 - outi);\n por += 96 - outi;\n}\n\ntemplate <typename T> void wt_real(T x) {\n ostringstream oss;\n oss << fixed << setprecision(15) << double(x);\n string s = oss.str();\n wt(s);\n}\n\nvoid wt(int x) {\n wt_integer(x);\n}\nvoid wt(ll x) {\n wt_integer(x);\n}\nvoid wt(i128 x) {\n wt_integer(x);\n}\nvoid wt(uint x) {\n wt_integer(x);\n}\nvoid wt(ull x) {\n wt_integer(x);\n}\nvoid wt(u128 x) {\n wt_integer(x);\n}\nvoid wt(double x) {\n wt_real(x);\n}\nvoid wt(long double x) {\n wt_real(x);\n}\n\ntemplate <class T, class U> void wt(const pair<T, U> val) {\n wt(val.first);\n wt(' ');\n wt(val.second);\n}\ntemplate <size_t N = 0, typename T> void wt_tuple(const T t) {\n if constexpr (N < std::tuple_size<T>::value) {\n if constexpr (N > 0) {\n wt(' ');\n }\n const auto x = std::get<N>(t);\n wt(x);\n wt_tuple<N + 1>(t);\n }\n}\ntemplate <class... T> void wt(tuple<T...> tpl) {\n wt_tuple(tpl);\n}\ntemplate <class T, size_t S> void wt(const array<T, S> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\ntemplate <class T> void wt(const vector<T> val) {\n auto n = val.size();\n for (size_t i = 0; i < n; i++) {\n if (i)\n wt(' ');\n wt(val[i]);\n }\n}\n\nvoid print() {\n wt('\\n');\n}\ntemplate <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {\n wt(head);\n if (sizeof...(Tail))\n wt(' ');\n print(forward<Tail>(tail)...);\n}\nvoid __attribute__((destructor)) _d() {\n flush();\n}\n} // namespace fastio\n\n\nusing fastio::flush;\nusing fastio::print;\nusing fastio::read;\n\ninline void first(bool i = true) {\n print(i ? \"first\" : \"second\");\n}\ninline void Alice(bool i = true) {\n print(i ? \"Alice\" : \"Bob\");\n}\ninline void Takahashi(bool i = true) {\n print(i ? \"Takahashi\" : \"Aoki\");\n}\ninline void yes(bool i = true) {\n print(i ? \"yes\" : \"no\");\n}\ninline void Yes(bool i = true) {\n print(i ? \"Yes\" : \"No\");\n}\ninline void No() {\n print(\"No\");\n}\ninline void YES(bool i = true) {\n print(i ? \"YES\" : \"NO\");\n}\ninline void NO() {\n print(\"NO\");\n}\ninline void Yay(bool i = true) {\n print(i ? \"Yay!\" : \":(\");\n}\ninline void Possible(bool i = true) {\n print(i ? \"Possible\" : \"Impossible\");\n}\ninline void POSSIBLE(bool i = true) {\n print(i ? \"POSSIBLE\" : \"IMPOSSIBLE\");\n}\n\n/**\n * @brief Fast IO\n */\n\ntemplate <unsigned mod = 1000000007> struct fp {\n unsigned v;\n static constexpr int get_mod() {\n return mod;\n }\n constexpr unsigned inv() const {\n assert(v != 0);\n int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;\n while (y > 0) {\n t = x / y;\n x -= t * y, p -= t * q;\n tmp = x, x = y, y = tmp;\n tmp = p, p = q, q = tmp;\n }\n if (p < 0)\n p += mod;\n return p;\n }\n constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}\n fp operator-() const {\n return fp() - *this;\n }\n fp pow(ull t) {\n fp res = 1, b = *this;\n while (t) {\n if (t & 1)\n res *= b;\n b *= b;\n t >>= 1;\n }\n return res;\n }\n fp &operator+=(const fp &x) {\n if ((v += x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator-=(const fp &x) {\n if ((v += mod - x.v) >= mod)\n v -= mod;\n return *this;\n }\n fp &operator*=(const fp &x) {\n v = ull(v) * x.v % mod;\n return *this;\n }\n fp &operator/=(const fp &x) {\n v = ull(v) * x.inv() % mod;\n return *this;\n }\n fp operator+(const fp &x) const {\n return fp(*this) += x;\n }\n fp operator-(const fp &x) const {\n return fp(*this) -= x;\n }\n fp operator*(const fp &x) const {\n return fp(*this) *= x;\n }\n fp operator/(const fp &x) const {\n return fp(*this) /= x;\n }\n bool operator==(const fp &x) const {\n return v == x.v;\n }\n bool operator!=(const fp &x) const {\n return v != x.v;\n }\n friend istream &operator>>(istream &is, fp &x) {\n return is >> x.v;\n }\n friend ostream &operator<<(ostream &os, const fp &x) {\n return os << x.v;\n }\n};\n\ntemplate <unsigned mod> void rd(fp<mod> &x) {\n fastio::rd(x.v);\n}\ntemplate <unsigned mod> void wt(fp<mod> x) {\n fastio::wt(x.v);\n}\n\ntemplate <typename T> T Inv(ll n) {\n static const int md = T::get_mod();\n static vector<T> buf({0, 1});\n assert(n > 0);\n n %= md;\n while (SZ(buf) <= n) {\n int k = SZ(buf), q = (md + k - 1) / k;\n buf.push_back(buf[k * q - md] * q);\n }\n return buf[n];\n}\n\ntemplate <typename T> T Fact(ll n, bool inv = 0) {\n static const int md = T::get_mod();\n static vector<T> buf({1, 1}), ibuf({1, 1});\n assert(n >= 0 and n < md);\n while (SZ(buf) <= n) {\n buf.push_back(buf.back() * SZ(buf));\n ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));\n }\n return inv ? ibuf[n] : buf[n];\n}\n\ntemplate <typename T> T nPr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nCr(int n, int r, bool inv = 0) {\n if (n < 0 || n < r || r < 0)\n return 0;\n return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);\n}\ntemplate <typename T> T nHr(int n, int r, bool inv = 0) {\n return nCr<T>(n + r - 1, r, inv);\n}\n\n/**\n * @brief Modint\n */\n\nint main() {\nwhile(1) {\n ll X;\n cin >> X;\n if (X == 0) return 0;\n X *= 2;\n vector<ll> V;\n for (ll i = 1; i * i <= X; i++) {\n if (X % i == 0) V.push_back(i);\n }\n reverse(V.begin(), V.end());\n for (ll P : V) {\n ll Q = X / P;\n if ((Q - P + 1) % 2 != 0) continue;\n if (Q - P + 1> 0) {\n cout << (Q - P + 1) / 2 << ' ' << P << endl;\n break;\n }\n }\n}\n}", "accuracy": 1, "time_ms": 80, "memory_kb": 3408, "score_of_the_acc": -0.5443, "final_rank": 5 } ]