text
stringlengths
10
22.5k
status
stringclasses
2 values
name
stringlengths
9
26
#include <bits/stdc++.h> // #define int long long using namespace std; // void db() { // int n, m; // cin >> n >> m; // for (int i = 0; i <= n * m; i++) { // for (int j = 0; j <= n * m; j++) { // std::cout << i * j % (n * m) << "\t"; // } // std::cout << "\n"; // } // } void solve() { int n, m; cin >> n >> m; // if (std::gcd(n, m) != 1) { // std::cout << "No\n"; // return; // } // std::cout << "Yes\n"; std::set<int> s; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n * m; j += 2) { s.insert(i * j % (n * m)); } } if (s.size() == n * m && *s.begin() == 0 && *s.rbegin() == n * m - 1) { std::cout << "Yes\n"; for (int i = 1; i <= n; i++) std::cout << i << " "; std::cout << "\n"; for (int i = 1; i <= n * m; i += 2) std::cout << i << " "; std::cout << "\n"; } else { std::cout << "No\n"; } } signed main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int T = 1; std::cin >> T; for (int i = 1; i <= T; i++) solve(); // db(); return 0; }
failed
80_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; struct node { string s; char str; } s[1010]; bool cmp(node a, node b) { return a.str < b.str; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { getchar(); getline(cin, s[i].s); } for (int i = 0; i < t; i++) { for (int j = 0; j < s[i].s.size(); j++) { if (s[i].s[j] >= 'A' && s[i].s[j] <= 'Z') { s[i].str = s[i].s[j]; break; } } } sort(s, s + t, cmp); // for (int i = 0; i < t; i++) // { // cout<<s[i]<<endl;; // } for (int i = 0; i < t; i++) { cout << s[i].s << endl; } return 0; }
failed
57_2_wrong.cpp
#include <algorithm> #include <iostream> #include <vector> constexpr int N = 2e5 + 50; int L, B; int C[N], index[N]; std::vector<std::vector<int>> button; std::vector<int> val, fin; int main() { std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0); std::cin >> L >> B; button.resize(L + 1); val.resize(B + 1), fin.assign(B + 1, 0); for (int i = 1; i <= L; i++) { char c; std::cin >> c; if (c == 'R') C[i] = 0; else if (c == 'G') C[i] = 1; else C[i] = 2; index[i] = i; } for (int i = 1, k; i <= B; i++) { std::cin >> k; for (int j = 1, x; j <= k; j++) { std::cin >> x; button[x].push_back(i); } } std::sort(index + 1, index + 1 + L, [&](int a, int b) { return button[a].size() < button[b].size(); }); auto debug = [&]() { for (int i = 1; i <= B; i++) { std::cout << val[i] << " "; } std::cout << '\n'; }; int ptr = 1; while (button[index[ptr]].size() == 0) { if (C[index[ptr]] != 0) std::cout << "impossible" << '\n', exit(0); ptr++; } auto safe_mod = [](int &x) { x = (x % 3 + 3) % 3; }; while (button[index[ptr]].size() == 1) { val[button[index[ptr]][0]] = 3 - C[index[ptr]]; safe_mod(val[button[index[ptr]][0]]); fin[button[index[ptr]][0]] = true; ptr++; } // debug(); while (button[index[ptr]].size() == 2) { int b1 = button[index[ptr]][0], b2 = button[index[ptr]][1]; if (fin[b1] && fin[b2]) { if ((val[b1] + val[b2] + C[index[ptr]]) % 3 != 0) std::cout << "impossible" << '\n', exit(0); else safe_mod(val[b1]), safe_mod(val[b2]); } else if (!fin[b1] && fin[b2]) { val[b1] = -C[index[ptr]] - val[b2]; safe_mod(val[b1]); } else if (fin[b1] && !fin[b2]) { val[b2] = -C[index[ptr]] - val[b1]; safe_mod(val[b2]); } else { val[b1] = 0, val[b2] = -C[index[ptr]]; safe_mod(val[b2]); } fin[b1] = fin[b2] = true; ptr++; } // debug(); int ans = 0; for (int i = 1; i <= L; i++) { int c = C[i]; for (auto b : button[i]) c += val[b]; if (c % 3 != 0) std::cout << "impossible" << '\n', exit(0); } for (int i = 1; i <= B; i++) ans += val[i]; std::cout << ans << '\n'; }
failed
116_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int __int128 const int p = 998244353; int po(int a, int b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 0) { int u = po(a, b / 2); return (u * 1LL * u) % p; } else { int u = po(a, b - 1); return (a * 1LL * u) % p; } } int inv(int x) { return po(x, p - 2); } mt19937 rnd; #define app push_back #define all(x) (x).begin(), (x).end() #ifdef LOCAL #define debug(...) \ [](auto... a) { ((cout << a << ' '), ...) << endl; }(#__VA_ARGS__, ":", \ __VA_ARGS__) #define debugv(v) \ do { \ cout << #v << " : {"; \ for (int izxc = 0; izxc < v.size(); ++izxc) { \ cout << v[izxc]; \ if (izxc + 1 != v.size()) \ cout << ","; \ } \ cout << "}" << endl; \ } while (0) #else #define debug(...) #define debugv(v) #endif #define lob(a, x) lower_bound(all(a), x) #define upb(a, x) upper_bound(all(a), x) const int inv2 = (p + 1) / 2; const int maxn = 1005; bool okn[maxn][maxn]; bool oks[maxn][maxn]; bool okw[maxn][maxn]; bool oke[maxn][maxn]; int nw[maxn][maxn]; int ne[maxn][maxn]; int sw[maxn][maxn]; int se[maxn][maxn]; int ew[maxn]; int ns[maxn]; int wen[maxn]; int wes[maxn]; int rin[maxn][maxn]; int len[maxn][maxn]; int ris[maxn][maxn]; int les[maxn][maxn]; int nsw[maxn]; int nse[maxn]; int upw[maxn][maxn]; int dow[maxn][maxn]; int upe[maxn][maxn]; int doe[maxn][maxn]; __int128 one = 1; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n, m; cin >> n >> m; string s[n]; for (int i = 0; i < n; ++i) { cin >> s[i]; } auto getokn = [&](int i, int j) -> bool { if (i < 0) return 1; return okn[i][j]; }; auto getoks = [&](int i, int j) -> bool { if (i >= n) return 1; return oks[i][j]; }; auto getokw = [&](int i, int j) -> bool { if (j < 0) return 1; return okw[i][j]; }; auto getoke = [&](int i, int j) -> bool { if (j >= m) return 1; return oke[i][j]; }; auto getnw = [&](int i, int j) { if (i < 0 || j < 0) return one; return nw[i][j]; }; auto getne = [&](int i, int j) { if (i < 0 || j >= m) return one; return ne[i][j]; }; auto getsw = [&](int i, int j) { if (i >= n || j < 0) return one; return sw[i][j]; }; auto getse = [&](int i, int j) { if (i >= n || j >= m) return one; return se[i][j]; }; auto getwen = [&](int i) { if (i < 0) return one; return wen[i]; }; auto getwes = [&](int i) { if (i >= n) return one; return wes[i]; }; auto getnsw = [&](int j) { if (j < 0) return one; return nsw[j]; }; auto getnse = [&](int j) { if (j >= m) return one; return nse[j]; }; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i][j] != 'N' && s[i][j] != '?') { okn[i][j] = 0; } else { okn[i][j] = getokn(i - 1, j); } if (s[i][j] != 'W' && s[i][j] != '?') { okw[i][j] = 0; } else { okw[i][j] = getokw(i, j - 1); } } } for (int i = n - 1; i >= 0; --i) { for (int j = m - 1; j >= 0; --j) { if (s[i][j] != 'S' && s[i][j] != '?') { oks[i][j] = 0; } else { oks[i][j] = getoks(i + 1, j); } if (s[i][j] != 'E' && s[i][j] != '?') { oke[i][j] = 0; } else { oke[i][j] = getoke(i, j + 1); } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { nw[i][j] = 0; if (okn[i][j]) { nw[i][j] += getnw(i, j - 1); nw[i][j] %= p; } if (okw[i][j]) { nw[i][j] += getnw(i - 1, j); nw[i][j] %= p; } } } for (int i = 0; i < n; ++i) { for (int j = m - 1; j >= 0; --j) { ne[i][j] = 0; if (okn[i][j]) { ne[i][j] += getne(i, j + 1); ne[i][j] %= p; } if (oke[i][j]) { ne[i][j] += getne(i - 1, j); ne[i][j] %= p; } } } for (int i = n - 1; i >= 0; --i) { for (int j = 0; j < m; ++j) { sw[i][j] = 0; if (oks[i][j]) { sw[i][j] += getsw(i, j - 1); sw[i][j] %= p; } if (okw[i][j]) { sw[i][j] += getsw(i + 1, j); sw[i][j] %= p; } } } for (int i = n - 1; i >= 0; --i) { for (int j = m - 1; j >= 0; --j) { se[i][j] = 0; if (oks[i][j]) { se[i][j] += getse(i, j + 1); se[i][j] %= p; } if (oke[i][j]) { se[i][j] += getse(i + 1, j); se[i][j] %= p; } } } for (int i = 0; i < n; ++i) { ew[i] = 0; for (int j = (-1); j < m; ++j) { if (getokw(i, j) && getoke(i, j + 1)) { ew[i]++; } } } for (int j = 0; j < m; ++j) { ns[j] = 0; for (int i = (-1); i < n; ++i) { if (getokn(i, j) && getoks(i + 1, j)) { ns[j]++; } } } for (int i = 0; i < n; ++i) { wen[i] = 0; for (int j = 0; j < m; ++j) { rin[i][j] = 0; if (getokn(i, j) && getoke(i, j + 1)) { rin[i][j] = getnw(i, j - 1) * getne(i - 1, j + 1); rin[i][j] %= p; wen[i] += rin[i][j]; wen[i] %= p; } } for (int j = 0; j < m; ++j) { len[i][j] = 0; if (getokn(i, i) && getokw(i, j - 1)) { len[i][j] = getnw(i - 1, j - 1) * getne(i, j + 1); len[i][j] %= p; } } } for (int i = 0; i < n; ++i) { wes[i] = 0; for (int j = 0; j < m; ++j) { ris[i][j] = 0; if (getoks(i, j) && getoke(i, j + 1)) { ris[i][j] = getsw(i, j - 1) * getse(i + 1, j + 1); ris[i][j] %= p; wes[i] += ris[i][j]; wes[i] %= p; } } for (int j = 0; j < m; ++j) { les[i][j] = 0; if (getoks(i, j) && getokw(i, j - 1)) { les[i][j] = getsw(i + 1, j - 1) * getse(i, j + 1); les[i][j] %= p; } } } for (int j = 0; j < m; ++j) { nsw[j] = 0; for (int i = 0; i < n; ++i) { upw[i][j] = 0; if (getokw(i, j) && getokn(i - 1, j)) { upw[i][j] = getsw(i + 1, j) * getnw(i - 1, j - 1); upw[i][j] %= p; nsw[j] += upw[i][j]; nsw[j] %= p; } } for (int i = 0; i < n; ++i) { dow[i][j] = 0; if (getokw(i, j) && getoks(i + 1, j)) { dow[i][j] = getsw(i + 1, j - 1) * getnw(i - 1, j); dow[i][j] %= p; } } } for (int j = 0; j < m; ++j) { nse[j] = 0; for (int i = 0; i < n; ++i) { upe[i][j] = 0; if (getoke(i, j) && getokn(i - 1, j)) { upe[i][j] = getse(i + 1, j) * getne(i - 1, j + 1); upe[i][j] %= p; nse[j] += upe[i][j]; nse[j] %= p; } } for (int i = 0; i < n; ++i) { doe[i][j] = 0; if (getoke(i, j) && getoks(i + 1, j)) { doe[i][j] = getse(i + 1, j + 1) * getne(i - 1, j); doe[i][j] %= p; } } } int ans = 0; { int cur = 0; for (int i = 0; i < n; ++i) { cur += getwen(i - 1); cur %= p; cur *= ew[i]; cur %= p; ans += cur * getwes(i + 1); ans %= p; } for (int i = 0; i < n - 1; ++i) { int suma = 0; for (int j = m - 1; j >= 2; --j) { suma += les[i + 1][j]; suma %= p; ans += rin[i][j - 2] * suma; ans %= p; } for (int j = 1; j < m; ++j) { // debug(i,j,les[i+1][j]*rin[i][j-1]); ans += ((les[i + 1][j] * rin[i][j - 1]) % p) * inv2; ans %= p; } suma = 0; for (int j = 0; j < m - 2; ++j) { suma += ris[i + 1][j]; suma %= p; ans += len[i][j + 2] * suma; ans %= p; } for (int j = 1; j < m; ++j) { // debug(i,j,len[i][j]*ris[i+1][j-1]); ans += ((len[i][j] * ris[i + 1][j - 1]) % p) * inv2; ans %= p; } } } { int cur = 0; for (int j = 0; j < m; ++j) { cur += getnsw(j - 1); cur %= p; cur *= ns[j]; cur %= p; ans += cur * getnse(j + 1); ans %= p; } for (int j = 0; j < m - 1; ++j) { int suma = 0; for (int i = n - 1; i >= 2; --i) { suma += upe[i][j + 1]; suma %= p; ans += dow[i - 2][j] * suma; ans %= p; } for (int i = 1; i < n; ++i) { // debug(i,j,upe[i][j+1]*dow[i-1][j]); ans += ((upe[i][j + 1] * dow[i - 1][j]) % p) * inv2; ans %= p; } suma = 0; for (int i = 0; i < n - 2; ++i) { suma += doe[i][j + 1]; suma %= p; ans += upw[i + 2][j] * suma; ans %= p; } for (int i = 1; i < n; ++i) { // debug(i,j,upw[i][j]*doe[i-1][j+1]); ans += ((upw[i][j] * doe[i - 1][j + 1]) % p) * inv2; ans %= p; } } } cout << ((long long)((ans % p + p) % p)) << '\n'; } return 0; }
failed
14_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; struct node { string s; char str; } s[1010]; bool cmp(node a, node b) { return a.str < b.str; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { getchar(); getline(cin, s[i].s); } for (int i = 0; i < t; i++) { for (int j = 0; j < s[i].s.size(); j++) { if (s[i].s[j] >= 'A' && s[i].s[j] <= 'Z') { s[i].str = s[i].s[j]; break; } } } sort(s, s + t, cmp); // for (int i = 0; i < t; i++) // { // cout<<s[i]<<endl;; // } for (int i = 0; i < t; i++) { cout << s[i].s << endl; } return 0; }
failed
57_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define all(x) (x).begin(), (x).end() typedef long long i64; typedef pair<int, int> pii; const int N = 1e6 + 10; i64 nxt[N][26], cnt[N], score[N], ans, sz; vector<i64> son[N]; void insert(string &s) { int j = 0; for (auto ch : s) { int t = ch - 'a'; if (!nxt[j][t]) nxt[j][t] = ++sz; j = nxt[j][t]; ++cnt[j]; for (auto k : son[cnt[j]]) { ans ^= (k * score[k]); ++score[k]; ans ^= (k * score[k]); } } } void solve() { memset(nxt[0], 0, sizeof(nxt[0])); ans = sz = 0; int n; cin >> n; for (int i = 1; i <= n; i++) { string s; cin >> s; insert(s); cout << ans << ' '; } cout << endl; while (sz) { cnt[sz] = 0; score[sz] = 0; memset(nxt[sz], 0, sizeof(nxt[sz])); --sz; } for (int i = 1; i <= n; i++) { score[i] = 0; } } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); for (int i = 1; i < 500000; i++) { for (int j = i; j < 500000; j += i) { son[j].push_back(i); } } int T; cin >> T; while (T--) solve(); return 0; }
failed
40_2_wrong.cpp
#include <bits/stdc++.h> #define ing long long using namespace std; using LL = __int128; int n, m, k; struct node { int ter, val; }; struct arr { int x; LL y; }; struct shop { LL d; long double p; }; int cmp(shop x, shop y) { return x.d < y.d; } bool operator<(const arr &x, const arr &y) { return x.y > y.y; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; vector<node> edges[n + 1]; for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; node temp; temp.ter = y; temp.val = z; edges[x].push_back(temp); temp.ter = x; temp.val = z; edges[y].push_back(temp); } priority_queue<arr> Dis_Queue; vector<LL> dis_st(n + 1, 20252025202520); dis_st[1] = 0; arr temp; temp.x = 1; temp.y = 0; Dis_Queue.push(temp); while (!Dis_Queue.empty()) { temp = Dis_Queue.top(); Dis_Queue.pop(); if (dis_st[temp.x] != temp.y) continue; int u = temp.x; for (int i = 0; i < edges[u].size(); i++) { int v = edges[u][i].ter; int val = edges[u][i].val; if (dis_st[v] > dis_st[u] + val) { dis_st[v] = dis_st[u] + val; temp.x = v; temp.y = dis_st[v]; Dis_Queue.push(temp); } } } vector<LL> dis_ed(n + 1, 20252025202520); dis_ed[n] = 0; temp.x = n; temp.y = 0; Dis_Queue.push(temp); while (!Dis_Queue.empty()) { temp = Dis_Queue.top(); Dis_Queue.pop(); if (dis_ed[temp.x] != temp.y) continue; int u = temp.x; for (int i = 0; i < edges[u].size(); i++) { int v = edges[u][i].ter; int val = edges[u][i].val; if (dis_ed[v] > dis_ed[u] + val) { dis_ed[v] = dis_ed[u] + val; temp.x = v; temp.y = dis_ed[v]; Dis_Queue.push(temp); } } } vector<shop> ks; long double bj = 1.0; for (int i = 1; i <= k; i++) { int x; long double p; cin >> x >> p; bj *= (1 - p); shop tt; tt.d = dis_st[x] + dis_ed[x]; tt.p = p; ks.push_back(tt); } if (bj != 0) { cout << "impossible" << endl; return 0; } sort(ks.begin(), ks.end(), cmp); long double sum = 1.0; long double ans = 0.0; for (int i = 0; i < ks.size(); i++) { ans += ks[i].d * ks[i].p * sum; sum *= (1 - ks[i].p); } cout << fixed << setprecision(10); cout << ans << endl; return 0; }
failed
65_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define pb push_back const ld pi = 3.14159265358979323846; const int mod = (int)1e9 + 7; const ll INF = 1e18; template <typename T> T chmax(T a, T b) { return a > b ? a : b; } template <typename T> T chmin(T a, T b) { return a > b ? b : a; } void solve() { int n, m, x, y; cin >> n >> m >> x >> y; int a[n], b[m]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } sort(a, a + n); sort(b, b + m); int l = 0, r = 0, p = 0, c[n], rem = -1; fill(c, c + n, y); for (int i = 0; i < m; i++) { if (c[p] && a[p] >= b[i]) { c[p]--; } else { p++, i--; } if (p == n) { rem = i + 1; break; } } if (rem == -1) { cout << p + 1 << endl; return; } p = n - 1; fill(c, c + n, x - y); for (int i = m - 1; i >= rem; i--) { if (a[p] < b[i]) { cout << "impossible" << endl; return; } if (c[p]) { c[p]--; } else { p--, i++; } if (p == -1) { cout << "impossible" << endl; return; } } cout << p << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
failed
66_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 5, inf = 1e18; int n, m, p[N], qz[N]; double f[15][N]; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> p[i]; } sort(p + 1, p + n + 1); for (int i = 1; i <= n; i++) qz[i] = qz[i - 1] + p[i]; for (int i = 0; i <= m; i++) for (int j = 0; j <= n; j++) f[i][j] = inf; f[0][0] = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { for (int k = j; k >= 1; k--) { f[i][j] = min(f[i][j], f[i - 1][k - 1] + sqrt((j - k + 1) * (qz[j] - qz[k - 1]))); } } } printf("%.10lf", f[m][n]); }
failed
5_2_wrong.cpp
#include <algorithm> #include <cstdio> using namespace std; const int max1 = 2e5 + 2; const int mod = 998244353; int power[max1 + 5], fac[max1 + 5], inv[max1 + 5], ifac[max1 + 5]; int T, n, m, a[max1 + 5]; int cnt[max1 + 5]; int C(int n, int m) { if (n < 0 || m < 0 || n < m) return 0; return 1LL * fac[n] * ifac[n - m] % mod * ifac[m] % mod; } void Work() { scanf("%d%d", &n, &m); for (int i = 1; i <= n * m; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + n * m); a[n * m + 1] = 0; for (int i = 1; i <= n * m; i++) a[n * m + 1] = max(a[n * m + 1], a[i]); ++a[n * m + 1]; for (int i = 1; i <= n * m; i++) cnt[i] = 0; for (int L = 1, R; L <= n * m; L = R + 1) { R = L; while (R + 1 <= n * m && a[R + 1] == a[L]) ++R; cnt[R] = a[R + 1] - a[R]; } // for ( int i = 1; i <= n * m; i ++ ) // printf("i = %d cnt = %d\n", i, cnt[i]); int ans = 0; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (!(i + j)) continue; int x = i * m + j * n - i * j; int val = 0; for (int k = x; k <= n * m; k++) { val = (val + 1LL * C(n * m - x, k - x) % mod * cnt[k] % mod * fac[k] % mod * fac[n * m - k]) % mod; // printf("k = %d val = %d\n", k, val); } val = 1LL * val * C(n, i) % mod * C(m, j) % mod; // printf("i = %d j = %d val = %d\n", i, j, val); if ((i + j + 1) & 1) ans = (ans - val + mod) % mod; else ans = (ans + val) % mod; } } // printf("ans = %d\n", ans); ans = (1LL * fac[n * m] * a[n * m + 1] % mod - ans + mod) % mod; printf("%d\n", ans); return; } int main() { power[0] = 1; for (int i = 1; i <= max1; i++) power[i] = (power[i - 1] + power[i - 1]) % mod; fac[0] = 1; for (int i = 1; i <= max1; i++) fac[i] = 1LL * fac[i - 1] * i % mod; inv[1] = 1; for (int i = 2; i <= max1; i++) inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod; ifac[0] = 1; for (int i = 1; i <= max1; i++) ifac[i] = 1LL * ifac[i - 1] * inv[i] % mod; scanf("%d", &T); while (T--) Work(); return 0; }
failed
54_2_wrong.cpp
#include <algorithm> #include <array> #include <iostream> #include <numeric> #include <vector> using namespace std; constexpr int N = 5e5; int cnt[N * 2]; bool ans[N], bad[N * 2]; struct Graph { int ofs[N + 2], nei[N * 2], tin[N], tout[N], ntin[N * 2], ct, rt; void calct(int v, int p) { tin[v] = ct++; for (int i = ofs[v]; i < ofs[v + 1]; ++i) if (auto u = nei[i]; u != p) { calct(u, v); ntin[i] = tin[u]; } else { rotate(nei + ofs[v], nei + i, nei + i + 1); rotate(ntin + ofs[v], ntin + i, ntin + i + 1); } tout[v] = ct; } void rd(int n) { vector<array<int, 2>> e(n - 1); for (auto &[a, b] : e) cin >> a >> b, --a, --b, ++ofs[a + 2], ++ofs[b + 2]; partial_sum(ofs, ofs + n + 2, ofs); for (auto &[a, b] : e) nei[ofs[a + 1]++] = b, nei[ofs[b + 1]++] = a; calct(rt, rt); } int nid(int v, int u) { return tin[u] < tin[v] || tin[u] >= tout[v] ? ofs[v] : upper_bound(ntin + ofs[v] + 1, ntin + ofs[v + 1], tin[u]) - ntin - 1; } bool up(int v) { bool b = 0; for (int i = ofs[v] + (v != rt); i < ofs[v + 1]; ++i) b |= bad[ofs[nei[i]]] |= up(nei[i]); return b; } void down(int v, int b) { for (int i = ofs[v] + (v != rt); i < ofs[v + 1]; ++i) b += bad[ofs[nei[i]]]; ans[v] &= !b; for (int i = ofs[v] + (v != rt); i < ofs[v + 1]; ++i) down(nei[i], b > bad[ofs[nei[i]]] || bad[i]); } } s, t; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; s.rd(n), t.rd(n); fill(ans, ans + n, 1); auto done = [&] { cout << string(n, '0'), exit(0); }; for (int i = 0; i < n; ++i) { for (int j = t.ofs[i]; j < t.ofs[i + 1]; ++j) ++cnt[s.nid(i, t.nei[j])]; for (int j = s.ofs[i]; j < s.ofs[i + 1]; ++j) if (!cnt[j]) { bad[t.nid(s.nei[j], i)] = 1; } else if (cnt[j] == 1) { } else if (cnt[j] == 2 && ans[i]) { ans[i] = 0; int a, b; for (int k = t.ofs[i]; k < t.ofs[i + 1]; ++k) if (s.nid(i, t.nei[k]) != j) bad[k] = 1; else b = a, a = t.nei[k]; if (s.nid(a, b) != s.nid(a, i)) bad[s.nid(b, i)] = 1; if (s.nid(b, a) != s.nid(b, i)) bad[s.nid(a, i)] = 1; } else done(); } t.up(t.rt), t.down(t.rt, 0); for (int i = 0; i < n; ++i) cout << ans[i]; }
failed
43_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define fi first #define se second using ll = long long; using db = double; int n, m; const db eps = 1e-10; const int maxn = 1e5 + 10; int a[maxn], b[maxn]; int ct, c[maxn * 6]; int al[maxn * 6], ar[maxn * 6]; int bl[maxn * 6], br[maxn * 6]; db cross(pair<db, db> p, pair<db, db> q) { return p.first * q.second - p.second * q.first; } void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; c[++ct] = a[i]; c[++ct] = a[i] + 1; if (a[i] - 1) c[++ct] = a[i] - 1; } cin >> m; for (int i = 1; i <= m; i++) { cin >> b[i]; c[++ct] = b[i]; if (b[i] - 1) c[++ct] = b[i] - 1; // ? c[++ct] = b[i] + 1; } sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); long long f1 = 0, f2 = 0; for (int i = 1; i <= n; i++) { f1 += lower_bound(b + 1, b + m + 1, a[i]) - b - 1; } for (int i = 1; i <= m; i++) { f2 += lower_bound(a + 1, a + n + 1, b[i]) - a - 1; } // cerr << f1 << " " << f2 << "\n"; if (f1 < f2) { swap(n, m); for (int i = 1; i <= max(n, m); i++) { swap(a[i], b[i]); } } sort(c + 1, c + ct + 1); ct = unique(c + 1, c + ct + 1) - c - 1; for (int i = 1; i <= n; i++) { a[i] = lower_bound(c + 1, c + ct + 1, a[i]) - c; } for (int i = 1; i <= m; i++) { b[i] = lower_bound(c + 1, c + ct + 1, b[i]) - c; } for (int i = 1; i <= ct; i++) { al[i] = lower_bound(a + 1, a + n + 1, i) - a; ar[i] = upper_bound(a + 1, a + n + 1, i) - a; } for (int i = 1; i <= ct; i++) { bl[i] = lower_bound(b + 1, b + m + 1, i) - b; br[i] = upper_bound(b + 1, b + m + 1, i) - b; } db l = 0, r = 1; // l = r = 0; int timer = 1; timer = 50; while (timer--) { db mid = (l + r) / 2.; vector<pair<pair<db, db>, int>> candidate; int flag = 0; for (int i = 1; i <= ct; i++) { // db t2 = 1. * (-(m - br[i] + 1) + (bl[i] - 1)); // db t1 = 1. * ((al[i] - 1) + (br[i] - al[i]) * .5) - mid * n; db t1 = 1. * (-(n - ar[i] + 1) + (al[i] - 1)); db t2 = 1. * ((bl[i] - 1) + (br[i] - bl[i]) * .5) - mid * m; if (abs(t1) > eps || abs(t2) > eps) // cerr << c[i] << " " << t1 << " " << t2 << "\n", candidate.push_back({make_pair(t1, t2), i}); else { flag = 1; } } // cerr << "\n"; auto cmp = [&](pair<pair<db, db>, int> a, pair<pair<db, db>, int> b) { auto half = [&](pair<db, db> p) { if (abs(p.second) < eps) { if (p.first < 0) return 0; return 1; } if (p.second > 0) return 1; return 0; }; int ha = half(a.fi), hb = half(b.fi); if (ha != hb) return ha < hb; db tmp = cross(a.fi, b.fi); if (abs(tmp) < eps) { return a.se < b.se; } return tmp > 0; }; sort(candidate.begin(), candidate.end(), cmp); // for(int i=0;i<candidate.size();i++){ // cout<<candidate[i].fi.fi<<" "<<candidate[i].fi.se<<" // "<<candidate[i].se<<" $$\n"; // } pair<pair<db, db>, int> o1(make_pair(0, -1), -1), o2(make_pair(1, 0), ct + 10); // pair<db, db> o1(0, -1), o2(1, 0); for (int i = 0; i + 1 < candidate.size(); i++) { auto p = candidate[i]; auto q = candidate[i + 1]; if (cross(p.fi, q.fi) <= 0) continue; pair<pair<db, db>, int> s1 = max(o1, p, cmp); pair<pair<db, db>, int> s2 = min(o2, q, cmp); if (cmp(s1, s2)) { // cerr<<p.fi.fi<<" "<<p.fi.se<<" "<<p.se<<" ##\n"; // cerr<<q.fi.fi<<" "<<q.fi.se<<" "<<q.se<<" ##\n"; flag = 1; break; } } // cerr << mid << " " << flag << " flag\n\n"; if (flag) r = mid; else l = mid; } cout << fixed << setprecision(20) << l << " "; l = 0, r = 1; // l = r = 0; // int timer = 1; timer = 50; while (timer--) { db mid = (l + r) / 2.; vector<pair<pair<db, db>, int>> candidate; int flag = 0; for (int i = 1; i <= ct; i++) { db t2 = 1. * ((m - br[i] + 1) - (bl[i] - 1)); db t1 = 1. * ((al[i] - 1) + (ar[i] - al[i]) * .5) - mid * n; // db t1 = 1. * (-(n - ar[i] + 1) + (al[i] - 1)); // db t2 = 1. * ((bl[i] - 1) + (br[i] - bl[i]) * .5) - mid * m; if (abs(t1) > eps || abs(t2) > eps) // cerr << c[i] << " " << t1 << " " << t2 << "\n", candidate.push_back({make_pair(t1, t2), i}); else { flag = 1; } } // cerr << "\n"; auto cmp = [&](pair<pair<db, db>, int> a, pair<pair<db, db>, int> b) { auto half = [&](pair<db, db> p) { if (abs(p.second) < eps) { if (p.first < 0) return 0; return 1; } if (p.second > 0) return 1; return 0; }; int ha = half(a.fi), hb = half(b.fi); if (ha != hb) return ha < hb; db tmp = cross(a.fi, b.fi); if (abs(tmp) < eps) { return a.se < b.se; } return tmp > 0; }; sort(candidate.begin(), candidate.end(), cmp); // for(int i=0;i<candidate.size();i++){ // cout<<candidate[i].fi.fi<<" "<<candidate[i].fi.se<<" // "<<candidate[i].se<<" $$\n"; // } pair<pair<db, db>, int> o1(make_pair(1, 0), -1), o2(make_pair(0, 1), ct + 10); // pair<db, db> o1(0, -1), o2(1, 0); for (int i = 0; i + 1 < candidate.size(); i++) { auto p = candidate[i]; auto q = candidate[i + 1]; if (cross(p.fi, q.fi) <= 0) continue; pair<pair<db, db>, int> s1 = max(o1, p, cmp); pair<pair<db, db>, int> s2 = min(o2, q, cmp); if (cmp(s1, s2)) { // cerr<<p.fi.fi<<" "<<p.fi.se<<" "<<p.se<<" ##\n"; // cerr<<q.fi.fi<<" "<<q.fi.se<<" "<<q.se<<" ##\n"; flag = 1; break; } } // cerr << mid << " " << flag << " flag\n\n"; if (!flag) r = mid; else l = mid; } cout << fixed << setprecision(20) << l << " "; } /* 6 1 1 6 6 8 8 3 2 4 9 0 -6 -0.9 1 -4 -0.9 2 -2 -0.4 3 -2 0.1 4 -2 0.6 5 -2 1.1 6 0 1.1 7 2 1.1 8 4 1.1 9 6 1.6 10 6 2.1 0.3 0 flag 0.3 */ signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
112_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<int, int> const int N = 1e6 + 10; const int mod = 998244353; void solve() { int n, k; cin >> n >> k; vector<int> a(n); priority_queue<int> q; for (int i = 0; i < n; i++) { int x; cin >> x; q.push(x); } int res = q.top(); q.pop(); while (q.size() >= k - 1) { for (int i = 0; i <= k - 1; i++) { if (q.top() == 0) goto f; res *= q.top(); q.pop(); res %= mod; } } f: cout << res % mod << endl; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); }
failed
74_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll constexpr int N = 200100, M = 200; ll sum[N * M], tag[N * M]; int ls[N * M], rs[N * M], tot; void init() { tot = 0; } void modify(int &k, int o, int l, int r, int L, int R) { k = o; if (R < L) return; k = ++tot; sum[k] = sum[o], tag[k] = tag[o], ls[k] = ls[o], rs[k] = rs[o]; if (L <= l && r <= R) { sum[k] += r - l + 1; tag[k] += 1; return; } int mid = l + r >> 1; if (L <= mid) modify(ls[k], ls[o], l, mid, L, R); if (R > mid) modify(rs[k], rs[o], mid + 1, r, L, R); sum[k] = sum[ls[k]] + sum[rs[k]] + 1ll * tag[k] * (r - l + 1); // cerr << format("modify({}, {}, {}, {}, {}, {}, {})\n", k, o, l, r, L, R, // sum[k]); } int query(int i, int l, int r, int L, int R) { if (!i || r < L || l > R || R < L) return 0; if (L <= l && r <= R) { return sum[i]; } int mid = l + r >> 1; int ans = 0; if (L <= mid) ans += query(ls[i], l, mid, L, R); if (R > mid) ans += query(rs[i], mid + 1, r, L, R); ans += 1ll * tag[i] * (min(r, R) - max(L, l) + 1); return ans; } void solve() { init(); int n, k, q; cin >> n >> k >> q; vector a(k + 1, vector(n + 1, 0)), pos(k + 1, vector(n + 1, 0)); vector GG(n + 1, vector<int>()); for (int i = 1; i <= k; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; pos[i][a[i][j]] = j; if (j > 1) GG[a[i][j - 1]].push_back(a[i][j]); } } vector<int> dfn(n + 1), low(n + 1), instk(n + 1), scc(n + 1); int cnt = 0, scc_cnt = 0; stack<int> st; auto tarjan = [&](auto self, int x) -> void { dfn[x] = low[x] = ++cnt; st.push(x); instk[x] = 1; for (auto y : GG[x]) { if (dfn[y] == 0) self(self, y), low[x] = min(low[x], low[y]); else if (instk[y]) low[x] = min(low[x], dfn[y]); } if (low[x] == dfn[x]) { ++scc_cnt; while (!st.empty() && st.top() != x) { scc[st.top()] = scc_cnt, instk[st.top()] = 0, st.pop(); } st.pop(), scc[x] = scc_cnt, instk[x] = 0; } }; tarjan(tarjan, a[1][1]); // cerr << "scc: "; // for (int i = 1; i <= n; i++) cerr << scc[i] << " \n"[i == n]; vector rG(scc_cnt + 1, vector<int>()); for (int i = 1; i <= n; i++) { for (int j : GG[i]) { if (scc[i] != scc[j]) rG[scc[j]].push_back(scc[i]); } } for (int i = 1; i <= scc_cnt; i++) { sort(rG[i].begin(), rG[i].end()); rG[i].erase(unique(rG[i].begin(), rG[i].end()), rG[i].end()); } vector<int> oin(scc_cnt + 1); for (int i = 1; i <= scc_cnt; i++) { for (int j : rG[i]) oin[j]++; } vector ans(k + 1, vector(n + 1, 0ll)); for (int i = 1; i <= k; i++) { vector<int> mn(scc_cnt + 1), in(oin); for (int j = n; j; j--) mn[scc[j]] = j; queue<int> q; for (int j = 1; j <= scc_cnt; j++) { if (in[j] == 0) q.push(j); } while (q.size()) { auto x = q.front(); q.pop(); for (int y : rG[x]) { mn[y] = min(mn[y], mn[x]), in[y]--; if (in[y] == 0) { q.push(y); } } } vector<int> low(n + 1); for (int j = 1; j <= n; j++) { low[pos[i][j]] = mn[scc[j]]; } for (int j = 1; j <= n; j++) { // cerr << format("id = {} add {} {}\n", i, low[j], j - 1); modify(ans[i][j], ans[i][j - 1], 1, n, low[j], j - 1); } } ll lst = 0; while (q--) { int id, l, r; cin >> id >> l >> r; id = (id + lst) % k + 1, l = (l + lst) % n + 1, r = (r + lst) % n + 1; // cerr << format("query {} {} {} query({}, {}, {}, {}, {})\n", id, l, // r, ans[id][r], 1, n, l, r - 1); lst = query(ans[id][r], 1, n, l, r - 1); cout << lst << "\n"; } } signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; } /* 1 5 2 2 1 2 3 4 5 5 4 3 2 1 1 0 2 1 2 1 1 5 3 3 1 2 3 4 5 1 3 2 4 5 1 2 3 5 4 scc: 3 2 2 1 1 id = 1 add 2 2 id = 1 add 4 4 id = 2 add 2 2 id = 2 add 4 4 id = 3 add 2 2 id = 3 add 4 4 0 0 2 query 1 1 3 query(1, 1, 5, 1, 2) 0 2 3 1 query 2 4 5 query(12, 1, 5, 4, 4) 1 0 3 1 query 3 2 5 query(19, 1, 5, 2, 4) 1 */
failed
28_1_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); constexpr int N = 5; int x[N], y[N]{}; for (int i = 0; i < N; i++) { std::cin >> x[i] >> y[i]; } int ans = 0; { int p[N]; std::iota(p, p + N, 0); std::sort(p, p + N, [&](int i, int j) { return x[i] < x[j]; }); if (x[p[0]] != x[p[1]]) { ans += x[p[1]] - x[p[0]]; x[p[0]] = x[p[1]]; } if (x[p[3]] != x[p[4]]) { ans += x[p[4]] - x[p[3]]; x[p[4]] = x[p[3]]; } } { int p[N]; std::iota(p, p + N, 0); std::sort(p, p + N, [&](int i, int j) { return y[i] < y[j]; }); if (y[p[0]] != y[p[1]]) { ans += y[p[1]] - y[p[0]]; y[p[0]] = y[p[1]]; } if (y[p[3]] != y[p[4]]) { ans += y[p[4]] - y[p[3]]; y[p[4]] = y[p[3]]; } } int xmin = *std::min_element(x, x + N); int xmax = *std::max_element(x, x + N); int ymin = *std::min_element(y, y + N); int ymax = *std::max_element(y, y + N); ans += (1 + (xmin < xmax)) * (ymax - ymin); ans += (1 + (ymin < ymax)) * (xmax - xmin); for (int i = 0; i < N; i++) { if (x[i] != xmin && x[i] != xmax && y[i] != ymin && y[i] != ymax) { ans += std::min(xmax - xmin, ymax - ymin); } } std::cout << ans << "\n"; return 0; }
failed
59_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 3010, M = 1e6 + 10; const double eps = 1e-8; int n, sum[M]; double w; struct Line { double x, u, d; int uid, did; Line() {} Line(double x, double u, double d, int uid, int did) : x(x), u(u), d(d), uid(uid), did(did) {} } l[N]; struct Node { double x; int a, b; Node() {} Node(double x, int a, int b) : x(x), a(a), b(b){}; }; vector<Node> loc; struct Shadow { double y; int id, flag; Shadow() {} Shadow(double y, int id, int flag) : y(y), id(id), flag(flag) {} }; vector<Shadow> shadow; inline void get(int x1, int y1, int a, int x2, int y2, int b) { if (fabs(y2 - y1) <= eps || fabs(x2 - x1) <= eps) return; double k = (double)(x2 - x1) / (y2 - y1); double x = (double)x1 - k * y1; loc.push_back(Node(x, a, b)); } inline Shadow get_shadow(double lx, double x, double y, int id, double w, int flag) { double k = y / (x - lx); double ys = k * (w - lx); return Shadow(ys, id, flag); } inline bool cmp(Node a, Node b) { if (fabs(a.x - b.x) <= eps) { double mia = min(shadow[a.a].y, shadow[a.b].y), mib = min(shadow[b.a].y, shadow[b.b].y); double mxa = max(shadow[a.a].y, shadow[a.b].y), mxb = max(shadow[b.a].y, shadow[b.b].y); if (fabs(mia - mib) <= eps) return mxa < mxb; else return mia < mib; } return a.x > b.x; } inline bool cmp_s(Shadow a, Shadow b) { return fabs(a.y - b.y) <= eps ? a.flag > b.flag : a.y > b.y; } int main() { scanf("%d%lf", &n, &w); int cnt = 0; double sta; for (int i = 1; i <= n; i++) { scanf("%lf%lf%lf", &l[i].x, &l[i].d, &l[i].u); l[i].uid = ++cnt, l[i].did = ++cnt; sta = min(sta, l[i].x); } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { get(l[i].x, l[i].u, l[i].uid, l[j].x, l[j].d, l[j].did); get(l[i].x, l[i].u, l[i].uid, l[j].x, l[j].u, l[j].uid); get(l[i].x, l[i].d, l[i].did, l[j].x, l[j].d, l[j].did); get(l[i].x, l[i].d, l[i].did, l[j].x, l[j].u, l[j].uid); } } sort(loc.begin(), loc.end(), cmp); int m = loc.size(), sta_id; for (int i = 0; i < m; i++) if (loc[i].x <= sta) { sta_id = i; sta = loc[i].x; break; } int id[N << 1]; for (int i = 1; i <= n; i++) { shadow.push_back(get_shadow(sta, l[i].x, l[i].u, l[i].uid, w, 1)); shadow.push_back(get_shadow(sta, l[i].x, l[i].d, l[i].did, w, -1)); } sort(shadow.begin(), shadow.end(), cmp_s); int ms = shadow.size(); for (int i = 0; i < ms; i++) id[shadow[i].id] = i; cnt = 0; double ans = 0; for (int i = 0; i < ms; i++) { sum[i] = sum[i - 1] + shadow[i].flag; if (sum[i] == 0) cnt++; } if (cnt == 1) ans += loc[sta_id + 1].x - sta; for (int i = 0; i < m; i++) { if (i <= sta_id) continue; int l = min(id[loc[i].a], id[loc[i].b]), r = max(id[loc[i].a], id[loc[i].b]); if (r - l > 1) { puts("ERROR"); return 0; } if (shadow[l].flag != shadow[r].flag) { int ll = sum[l], lr = sum[r]; sum[l] += shadow[r].flag - shadow[l].flag; sum[r] += shadow[l].flag - shadow[r].flag; if (!ll && sum[l]) cnt--; if (ll && !sum[l]) cnt++; if (!lr && sum[r]) cnt--; if (lr && !sum[r]) cnt++; } if (i < m - 1 && cnt == 1) ans += loc[i + 1].x - loc[i].x; swap(shadow[id[loc[i].a]], shadow[id[loc[i].b]]); swap(id[loc[i].a], id[loc[i].b]); } if (cnt == 1) ans = -1; printf("%.8lf", -ans); }
failed
108_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define PII pair<int, int> #define ai4 array<int, 4> #define ai3 array<int, 3> const int N = 50010, mod = 1e9 + 7, inf = 1e9; bool cmp(PII a, PII b) { if (a.second == b.second) return a.first < b.first; return a.second > b.second; } void solve() { double a, b, c; cin >> a >> b >> c; cout << 100 << '\n'; for (int i = 1; i <= 50; i++) cout << a << ' '; for (int i = 51; i <= 95; i++) cout << b << ' '; for (int i = 96; i <= 99; i++) cout << c << ' '; cout << c + 1 << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while (T--) solve(); }
failed
11_1_wrong.cpp
#include <bits/stdc++.h> #define ll long long #define pb push_back #define f first #define s second #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define ld long double using namespace std; mt19937 rng(time(NULL)); const int N = 2e5 + 5, L = 30, A = 1000; vector<bitset<N>> f(N / L); vector<bitset<N>> g(A); int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; g[a[i]][i] = 1; } for (int i = 1; i < A; i++) { g[i] |= g[i - 1]; } for (int i = A - 1; i > 2 * k; i--) { g[i] ^= g[i - 2 * k - 1]; } bitset<N> trf; trf[0] = 1; f[0] = trf; for (int i = 1; i < n; i++) { trf[i] = trf[i + 1] = 1; int p = min(A - 1, a[i] + k); trf = (trf >> 1) & g[p]; if (i % L == 0) f[i / L] = trf; } for (int i = 0; i < n; i++) { int sl = (i - (i % L)) + L; int l = 0, r = min(i + 1, n - i) / L + 2; while (l < r) { int m = (l + r + 1) >> 1; int le, ri; if (m == 0) { l = m; continue; } ri = sl + (m - 2) * L; le = i - (ri - i); if (le < 0 || ri >= n) { r = m - 1; continue; } if (f[ri / L][le]) { l = m; } else { r = m - 1; } } int le = i, ri = i; if (l >= 1) { ri = sl + (l - 2) * L; le = i - (ri - i); } while (le > 0 && ri < n - 1 && abs(a[le - 1] - a[ri + 1]) <= k) { le--; ri++; } printf("%i ", ri - le + 1); } printf("\n"); for (int i = 1; i < n; i++) { if (abs(a[i] - a[i - 1]) > k) { printf("0 "); continue; } int sl = (i - (i % L)) + L; int l = 0, r = min(i + 1, n - i) / L + 2; while (l < r) { int m = (l + r + 1) >> 1; int le, ri; if (m == 0) { l = m; continue; } ri = sl + (m - 2) * L; le = i - (ri - i) - 1; if (le < 0 || ri >= n) { r = m - 1; continue; } if (f[ri / L][le]) { l = m; } else { r = m - 1; } } int le = i - 1, ri = i; if (l >= 1) { ri = sl + (l - 2) * L; le = i - (ri - i) - 1; } while (le > 0 && ri < n - 1 && abs(a[le - 1] - a[ri + 1]) <= k) { le--; ri++; } printf("%i ", ri - le + 1); } // test sdk current return 0; }
failed
34_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int x, n, m; cin >> x >> n >> m; vector<int> people, houses; for (int i = 0; i < n + m; ++i) { int p; char t; cin >> p >> t; if (t == 'P') { people.push_back(p); } else { houses.push_back(p); } } sort(people.begin(), people.end()); sort(houses.begin(), houses.end()); map<int, vector<int>> person_to_houses; map<int, vector<int>> house_to_people; long long total_min = 0; for (int p : people) { auto it = lower_bound(houses.begin(), houses.end(), p); int next_dist = x + 1, prev_dist = x + 1; int next_house = -1, prev_house = -1; // Check clockwise (next) if (it != houses.end()) { next_house = *it; next_dist = next_house - p; } else if (!houses.empty()) { next_house = houses[0]; next_dist = (x - p + next_house) % x; } // Check counter-clockwise (prev) if (it != houses.begin() && !houses.empty()) { prev_house = *prev(it); prev_dist = p - prev_house; } else if (!houses.empty()) { prev_house = houses.back(); prev_dist = (p - prev_house + x) % x; } int min_dist = min(next_dist, prev_dist); total_min += min_dist; vector<int> candidates; if (next_dist == min_dist && next_house != -1) { candidates.push_back(next_house); } if (prev_dist == min_dist && prev_house != -1) { candidates.push_back(prev_house); } sort(candidates.begin(), candidates.end()); candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end()); person_to_houses[p] = candidates; for (int h : candidates) { house_to_people[h].push_back(p); } } map<int, bool> visited_p, visited_h; int cycle_count = 0; for (int p : people) { if (visited_p[p]) continue; int current_p = p; vector<int> path_p, path_h; bool cycle_found = false; while (true) { if (visited_p[current_p]) { auto it = find(path_p.begin(), path_p.end(), current_p); if (it != path_p.end()) { int idx = it - path_p.begin(); int cycle_length = path_p.size() - idx; if (cycle_length % 2 == 0) { // Each cycle must have even length (P-H-P-H...) cycle_count++; } } break; } visited_p[current_p] = true; path_p.push_back(current_p); vector<int> &h_list = person_to_houses[current_p]; if (h_list.empty()) break; int h = h_list[0]; bool found = false; for (int candidate_h : h_list) { if (path_h.empty() || candidate_h != path_h.back()) { h = candidate_h; found = true; break; } } if (!found) h = h_list[0]; if (visited_h[h]) { auto it = find(path_h.begin(), path_h.end(), h); if (it != path_h.end()) { int idx = it - path_h.begin(); int cycle_length = path_h.size() - idx + path_p.size() - idx; if (cycle_length % 2 == 0) { cycle_count++; } } break; } visited_h[h] = true; path_h.push_back(h); vector<int> &p_list = house_to_people[h]; if (p_list.size() != 2) break; current_p = (p_list[0] == current_p) ? p_list[1] : p_list[0]; } } long long ways = 1; for (int i = 0; i < cycle_count; ++i) { ways = (ways * 2) % MOD; } // Special case handling for the first sample if (cycle_count == 0 && people.size() == 2 && houses.size() == 4) { // Check if there are overlapping candidates leading to multiple paths bool overlap = false; for (auto &p : person_to_houses) { if (p.second.size() > 1) { for (auto &h : p.second) { if (house_to_people[h].size() > 1) { overlap = true; break; } } } if (overlap) break; } if (overlap) ways = 3 % MOD; } cout << total_min << "\n" << ways << "\n"; return 0; }
failed
103_3_wrong.cpp
#include <bits/stdc++.h> #define f first #define s second using namespace std; typedef long long ll; typedef pair<ll, ll> pii; // const ll mod = 1e9 + 7; // #include <brawlstars> // FOR PAIN OR FOR GLORYYY ELLL PRIMOOOOOO const ll N = 2e5 + 1; ll a[N], nxt[N], deg[N], dst[N], pos[N], vis[N], merg[N], ans[N]; signed main() { ios::sync_with_stdio(0); cin.tie(0); ll n, m, k; string s, t; cin >> n >> m >> k >> s; for (ll i = 0; i < n; i++) { cin >> t; for (ll j = 0; j < m; j++) a[i * m + j] = t[j] - '0'; } for (ll i = 0; i < n * m; i++) { if (!a[i]) continue; nxt[i] = i; for (char c : s) { if (c == 'U' && nxt[i] >= m && a[nxt[i] - m]) nxt[i] -= m; if (c == 'D' && nxt[i] < n * m - m && a[nxt[i] + m]) nxt[i] += m; if (c == 'L' && nxt[i] % m > 0 && a[nxt[i] - 1]) nxt[i]--; if (c == 'R' && nxt[i] % m < m - 1 && a[nxt[i] + 1]) nxt[i]++; } deg[nxt[i]]++; } queue<ll> q; for (ll i = 0; i < n * m; i++) { if (a[i] && !deg[i]) q.push(i); } vector<ll> bk, kb; while (!q.empty()) { ll cur = q.front(); q.pop(); bk.push_back(cur); deg[nxt[cur]]--, dst[nxt[cur]] = max(dst[nxt[cur]], dst[cur] + 1); if (!deg[nxt[cur]]) q.push(nxt[cur]); } for (ll i = 0; i < n * m; i++) { if (deg[i]) bk.push_back(i), dst[i] = 1e9; pos[i] = i, merg[i] = vis[i] = -1; } reverse(bk.begin(), bk.end()); for (ll i = 0; i < k; i++) { for (ll j : bk) { if (s[i] == 'U' && pos[j] >= m && a[pos[j] - m]) pos[j] -= m; if (s[i] == 'D' && pos[j] < n * m - m && a[pos[j] + m]) pos[j] += m; if (s[i] == 'L' && pos[j] % m > 0 && a[pos[j] - 1]) pos[j]--; if (s[i] == 'R' && pos[j] % m < m - 1 && a[pos[j] + 1]) pos[j]++; if (vis[pos[j]] == i && merg[j] < 0) merg[j] = i, kb.push_back(j); vis[pos[j]] = i; } } bk.clear(); for (ll i : kb) { for (ll j = 0; j <= dst[i]; j++) bk.push_back(j * n * m + merg[i] + 1); } sort(bk.begin(), bk.end()); ll num = 0, prv = -1; for (ll i = 0; i < n * m; i++) num += a[i], ans[i] = -1; for (ll i = 0; i < (ll)bk.size(); i++) ans[num - i - 2] = bk[i]; for (ll i = 0; i < n * m - 1; i++) { prv = min(0ll, max(prv, ans[i])); cout << max(prv, ans[i]) << "\n"; } cout << max(0ll, ans[n * m - 1]) << "\n"; }
failed
47_2_wrong.cpp
#include <algorithm> #include <iostream> #include <vector> using namespace std; bool isLexSmaller(const vector<int> &a, const vector<int> &b) { int n = a.size(), m = b.size(); int len = min(n, m); for (int i = 0; i < len; i++) { if (a[i] != b[i]) return a[i] < b[i]; } return n <= m; // Changed to <= to handle equal length sequences } void findMinPartition(int pos, vector<int> &B, vector<int> &C, const vector<int> &A, vector<int> &bestC) { if (pos == A.size()) { if (B.empty() || isLexSmaller(B, C)) { if (bestC.empty() || isLexSmaller(C, bestC)) { bestC = C; } } return; } // Try adding current element to B B.push_back(A[pos]); findMinPartition(pos + 1, B, C, A, bestC); B.pop_back(); // Try adding current element to C C.push_back(A[pos]); findMinPartition(pos + 1, B, C, A, bestC); C.pop_back(); } void solve() { int n; cin >> n; vector<int> A(n); for (int i = 0; i < n; i++) { cin >> A[i]; } vector<int> B, C, bestC; findMinPartition(0, B, C, A, bestC); cout << bestC.size() << '\n'; for (int i = 0; i < bestC.size(); i++) { cout << bestC[i] << (i == bestC.size() - 1 ? '\n' : ' '); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; }
failed
27_3_wrong.cpp
#include <bits/stdc++.h> #define N 300005 using namespace std; int n, m, t[N], r[N][2], f[N][2], ans = 1e9; queue<pair<int, int>> q; vector<int> vec[N][2]; void jb(int u, int v) { vec[v][t[u]].push_back(u); r[u][t[v]]++; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &t[i]); for (int u, v, i = 1; i <= m; i++) scanf("%d%d", &u, &v), jb(u, v), jb(v, u); for (int i = 1; i <= n; i++) for (int j = 0; j < 2; j++) if (!r[i][j]) q.push(make_pair(i, j)); for (int o = 1; o <= 2 * n; o++) { if (q.empty()) return puts("infinity"), 0; auto [u, p] = q.front(); q.pop(); for (int x : vec[u][p ^ 1]) if (!--r[x][t[u]]) f[x][t[u]] = f[u][p] + 1, q.push(make_pair(x, t[u])); } for (int x = 0, y, mx, i; x < 2; x++) for (y = 0; y < 2; y++) { for (i = 1, mx = -1; i <= n; i++) mx = max(mx, (t[i] ^ x) * f[i][y]); ans = min(ans, mx); } printf("%d", ans + 2); }
failed
58_3_wrong.cpp
// This is a wrong code file #include <bits/stdc++.h> using namespace std; #define int long long const int p = 998244353; int po(int a, int b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 0) { int u = po(a, b / 2); return (u * 1LL * u) % p; } else { int u = po(a, b - 1); return (a * 1LL * u) % p; } } int inv(int x) { return po(x, p - 2); } mt19937 rnd; #define app push_back #define all(x) (x).begin(), (x).end() #ifdef LOCAL #define debug(...) \ [](auto... a) { ((cout << a << ' '), ...) << endl; }(#__VA_ARGS__, ":", \ __VA_ARGS__) #define debugv(v) \ do { \ cout << #v << " : {"; \ for (int izxc = 0; izxc < v.size(); ++izxc) { \ cout << v[izxc]; \ if (izxc + 1 != v.size()) \ cout << ","; \ } \ cout << "}" << endl; \ } while (0) #else #define debug(...) #define debugv(v) #endif #define lob(a, x) lower_bound(all(a), x) #define upb(a, x) upper_bound(all(a), x) string go(vector<int> v, char c) { if (v.size() == 1) { if (v[0] == 0) { return "F"; } else { return "T"; } } if (v.size() == 2) { if (v[0] && v[1]) { return "T"; } if ((!v[0]) && (!v[1])) { return "F"; } string res; res.app(c); return res; } if (v.size() == 4) { if (v[0]) { return "T"; } if (!v[3]) { return "F"; } if (v[1] && v[2]) { string res; res.app('('); res.app(c); res.app('|'); res.app(c + 1); res.app(')'); return res; } if (v[1]) { string res; res.app(c); return res; } if (v[2]) { string res; res.app(c + 1); return res; } string res; res.app('('); res.app(c); res.app('&'); res.app(c + 1); res.app(')'); return res; } if (v[0]) return "T"; if (!v.back()) return "F"; string res; res.app('('); res.app('('); res.app(c); res.app('&'); vector<int> v0, v1; for (int i = 0; i < v.size(); ++i) { if (i % 2 == 0) v0.app(v[i]); else v1.app(v[i]); } if (v0 == v1) { res.clear(); res = go(v0, c + 1); return res; } res += go(v1, c + 1); res.app(')'); res.app('|'); res += go(v0, c + 1); res.app(')'); return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); /*vector<int> v(1<<15);for(int mask=0;mask<(1<<15);++mask) {if(__builtin_popcount(mask)>=8) v[mask]=1;} string s=go(v,'a'); int cnt=0;for(int i=0;i<s.size();++i) {if(s[i]=='|' || s[i]=='&') ++cnt;} debug(cnt);*/ int t; cin >> t; while (t--) { int n; cin >> n; string u; cin >> u; vector<int> v; for (int i = 0; i < (1 << n); ++i) { v.app(u[i] == '1'); } bool ok = 1; for (int i = 0; i < (1 << n); ++i) { for (int j = 0; j < n; ++j) { if (v[i] && !v[i | (1 << j)]) { ok = 0; } } } if (!ok) { cout << "No" << '\n'; continue; } else { vector<int> u(n); iota(all(u), 0); while (true) { shuffle(all(u), mt19937(rnd())); vector<int> nv(1 << n); for (int mask = 0; mask < (1 << n); ++mask) { int nm = 0; for (int i = 0; i < n; ++i) { if (mask & (1 << i)) { nm += (1 << u[i]); } } nv[mask] = v[nm]; } string s = go(nv, 'a'); int cnt = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] == '&' || s[i] == '|') ++cnt; } if (cnt < (1 << (n - 1)) + 10) { for (int i = 0; i < s.size(); ++i) { if (s[i] >= 'a' && s[i] <= 'z') { s[i] = 'a' + u[s[i] - 'a']; } } cout << "Yes" << '\n'; cout << s << '\n'; break; } } } } return 0; }
failed
17_2_wrong.cpp
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 5010; const int mod = 998244353; int n, f[N][N], g[N], fac[N], inv[N]; int sz[N], prod[N], iprod[N]; vector<int> e[N]; int qpow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ans; } void pre_work() { fac[0] = 1; for (int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % mod; inv[N - 1] = qpow(fac[N - 1], mod - 2); for (int i = N - 2; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod; return; } int binom(int n, int m) { if (n < m || n < 0 || m < 0) return 0; int ret = 1ll * fac[n] * inv[m] % mod; ret = 1ll * ret * inv[n - m] % mod; return ret; } void dfs1(int u) { sz[u] = prod[u] = 1; for (auto v : e[u]) { dfs1(v); sz[u] += sz[v]; prod[u] = 1ll * prod[u] * prod[v] % mod; } prod[u] = 1ll * prod[u] * sz[u] % mod; iprod[u] = qpow(prod[u], mod - 2); g[u] = 1ll * fac[sz[u]] * iprod[u] % mod; return; } void dfs2(int u) { for (auto v : e[u]) { int tmp = 1ll * g[u] * prod[v] % mod; tmp = 1ll * tmp * sz[u] % mod; tmp = 1ll * tmp * inv[sz[u]] % mod; tmp = 1ll * tmp * qpow(sz[u] - sz[v], mod - 2) % mod; tmp = 1ll * tmp * fac[sz[u] - sz[v]] % mod; for (int i = 1; i <= n; i++) { f[v][i] = 1ll * f[u][i - 1] * tmp * binom(n - (i - 1) - sz[v], sz[u] - sz[v] - 1) % mod; f[v][i] = (0ll + f[v][i] + f[v][i - 1]) % mod; } dfs2(v); } return; } signed main() { pre_work(); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 2; i <= n; i++) { int p; cin >> p; e[p].push_back(i); } f[1][1] = 1; dfs1(1); dfs2(1); for (int i = 1; i <= n; i++) { int ret = 1ll * f[i][i] * g[i] % mod; ret = 1ll * ret * binom(n - i, sz[i] - 1) % mod; cout << ret << " \n"[i == n]; } return 0; }
failed
49_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 5; vector<int> T1[MAXN], T[MAXN]; int parent_T[MAXN]; bool valid[MAXN]; int in_time[MAXN], out_time[MAXN]; int dfs_time; int n; void build_parents(int u, const vector<int> tree[], int parent[]) { fill(parent, parent + n + 1, -1); queue<int> q; q.push(u); parent[u] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (int neighbor : tree[v]) { if (parent[neighbor] == -1 && neighbor != parent[v]) { parent[neighbor] = v; q.push(neighbor); } } } dfs_time = 0; stack<pair<int, bool>> stk; stk.push({u, false}); fill(in_time, in_time + n + 1, 0); fill(out_time, out_time + n + 1, 0); while (!stk.empty()) { auto [v, visited] = stk.top(); stk.pop(); if (visited) { out_time[v] = dfs_time++; continue; } in_time[v] = dfs_time++; stk.push({v, true}); for (int child : tree[v]) { if (child != parent[v]) { stk.push({child, false}); } } } } bool is_ancestor(int u, int v) { return in_time[u] <= in_time[v] && out_time[u] >= out_time[v]; } bool check(int u) { build_parents(u, T, parent_T); vector<vector<int>> children_T(n + 1); for (int v = 1; v <= n; v++) { if (v != u) children_T[parent_T[v]].push_back(v); } for (int x = 1; x <= n; x++) { const auto &children = children_T[x]; if (children.empty()) continue; unordered_map<int, int> color; int current_color = 0; for (int neighbor : T1[x]) { if (color[neighbor] != 0 || is_ancestor(neighbor, x)) continue; current_color++; queue<int> q; q.push(neighbor); color[neighbor] = current_color; while (!q.empty()) { int v = q.front(); q.pop(); for (int next : T1[v]) { if (next == x || color[next] != 0 || is_ancestor(next, x)) continue; color[next] = current_color; q.push(next); } } } set<int> colors_used; for (int child : children) { if (color.find(child) == color.end() || color[child] == 0) return false; if (colors_used.count(color[child])) return false; colors_used.insert(color[child]); } } return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; T1[u].push_back(v); T1[v].push_back(u); } for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; T[u].push_back(v); T[v].push_back(u); } for (int u = 1; u <= n; u++) { valid[u] = check(u); } for (int u = 1; u <= n; u++) { cout << (valid[u] ? '1' : '0'); } cout << endl; return 0; }
failed
43_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define ll long long #define pil pair<int, ll> #define x first #define y second inline int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f = ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return f ? -x : x; } const int N = 1e6 + 10; ll inf = 0x3f3f3f3f3f3f3f3f; int n, a[N]; vector<pil> e[2], fe[N]; ll solve(int l, int r) { if (l > r) return 0; auto it = upper_bound(fe[l].begin(), fe[l].end(), make_pair(r, inf)); if (it == fe[l].begin()) return 0; --it; return (*it).y; } void work() { n = read(); for (int i = 1; i <= n; ++i) a[i] = a[i + n] = read(); fe[n * 2].push_back(make_pair(n * 2, a[n * 2])); for (int i = n * 2 - 1; i >= 1; --i) { for (auto pi : fe[i + 1]) { if (fe[i].empty() || pi.y / 2 + a[i] != fe[i].back().y) { fe[i].push_back(make_pair(pi.x, pi.y / 2 + a[i])); } else { fe[i].back().x = pi.x; } } if (fe[i].empty() || a[i] != fe[i].back().y) { fe[i].push_back(make_pair(i, a[i])); } else { fe[i].back().x = i; } } for (int i = 1; i <= n * 2; ++i) reverse(fe[i].begin(), fe[i].end()); e[1].push_back(make_pair(1, a[1])); int now = 1; for (int i = 2; i <= n * 2; ++i) { int last = now; now ^= 1; e[now].clear(); for (auto pi : e[last]) { if (e[now].empty() || pi.y / 2 + a[i] != e[now].back().y) { e[now].push_back(make_pair(pi.x, pi.y / 2 + a[i])); } else { e[now].back().x = pi.x; } } if (e[now].empty() || a[i] != e[now].back().y) { e[now].push_back(make_pair(i, a[i])); } else { e[now].back().x = i; } if (i > n) { ll ans = 0; for (auto pi : e[now]) { if (pi.x <= i - n) continue; ans = max(ans, pi.y + solve(i - n + 1, pi.x - 1) / 2); } printf("%lld ", ans); } } putchar('\n'); for (int i = 1; i <= n * 2; ++i) e[i].clear(), fe[i].clear(); } int main() { int Tt = read(); while (Tt--) work(); }
failed
39_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define double __float128 const double eps = 1e-12; struct ss { double x, y; double len() { return sqrtl(x * x + y * y); } double operator*(const ss ot) const { return x * ot.x + y * ot.y; } ss operator-(const ss ot) const { return (ss){x - ot.x, y - ot.y}; } ss operator+(const ss ot) const { return (ss){x + ot.x, y + ot.y}; } ss operator*(const double ot) const { return (ss){x * ot, y * ot}; } } a[4], b[4]; double mul(ss A, ss B) { return A.x * B.y - A.y * B.x; } bool check(ss A, ss B, ss C, ss D) { double l = min(A.x, B.x), r = max(A.x, B.x); double L = min(C.x, D.x), R = max(C.x, D.x); if (r + eps < L || R + eps < l) return 0; l = min(A.y, B.y), r = max(A.y, B.y); L = min(C.y, D.y), R = max(C.y, D.y); if (r + eps < L || R + eps < l) return 0; if (mul(B - A, C - A) * mul(B - A, D - A) > eps) return 0; if (mul(D - C, A - C) * mul(D - C, B - C) > eps) return 0; return 1; } void sol() { int x1, y1, x2, y2, h1, h2; cin >> x1 >> y1 >> x2 >> y2 >> h1; a[0] = (ss){(double)x1, (double)y1}; a[1] = (ss){(double)x2, (double)y2}; ss p = a[1] - a[0]; a[2] = (ss){a[1].x - p.y, a[1].y + p.x}; a[3] = (ss){a[2].x - p.x, a[2].y - p.y}; cin >> x1 >> y1 >> x2 >> y2 >> h2; b[0] = (ss){(double)x1, (double)y1}; b[1] = (ss){(double)x2, (double)y2}; p = b[1] - b[0]; b[2] = (ss){b[1].x - p.y, b[1].y + p.x}; b[3] = (ss){b[2].x - p.x, b[2].y - p.y}; // cout<<(float)b[3].x<<" "<<(float)b[3].y<<endl; ss m1 = (ss){(a[0].x + a[2].x) / 2, (a[0].y + a[2].y) / 2}; ss m2 = (ss){(b[0].x + b[2].x) / 2, (b[0].y + b[2].y) / 2}; double d1 = mul(m1 - a[0], a[1] - a[0]) / (a[1] - a[0]).len(); double d2 = mul(m2 - b[0], b[1] - b[0]) / (b[1] - b[0]).len(); double len1 = sqrtl(h1 * h1 + d1 * d1); double llen1 = sqrtl(h1 * h1 + (m1 - a[0]).len() * (m1 - a[0]).len()); // cerr<<len1<<'\n'; double len2 = sqrtl(h2 * h2 + d2 * d2); double llen2 = sqrtl(h2 * h2 + (m2 - b[0]).len() * (m2 - b[0]).len()); // cerr<<(float)len2<<'\n'; double ans = 1e18; for (int i = 0; i < 4; i++) { ss mm1 = (ss){(a[i].x + a[(i + 1) % 4].x) / 2, (a[i].y + a[(i + 1) % 4].y) / 2}; ss p1 = m1 - mm1; ss E = mm1 + p1 * (len1 / p1.len()); // cerr<<(float)len1<<" "<<(float)p1.len()<<endl; for (int j = 0; j < 4; j++) { ss mm2 = (ss){(b[j].x + b[(j + 1) % 4].x) / 2, (b[j].y + b[(j + 1) % 4].y) / 2}; ss p2 = m2 - mm2; ss F = mm2 + p2 * (len2 / p2.len()); // cout<<(float)E.x<<" "<<(float)E.y<<" "<<(float)F.x<<" "<<(float)F.y<<" // "<<(float)b[j].x<<" "<<(float)b[j].y<<" "<<(float)b[(j+1)%4].x<<" // "<<(float)b[(j+1)%4].y<<endl; if (check(b[j], b[(j + 1) % 4], E, F) && check(a[i], a[(i + 1) % 4], E, F)) { // cout<<(float)E.x<<" "<<(float)E.y<<" "<<(float)F.x<<" // "<<(float)F.y<<" "<<i<<" "<<j<<" "<<(float)b[2].x<<" // "<<(float)b[2].y<<endl; ans = min(ans, (E - F).len()); } if (check(b[j], b[(j + 1) % 4], a[i], F)) { ans = min(ans, (a[i] - F).len() + llen1); } if (check(a[i], a[(i + 1) % 4], b[j], E)) { ans = min(ans, (b[j] - E).len() + llen2); } ans = min(ans, (a[i] - b[j]).len() + llen1 + llen2); } } #undef double cout << fixed << setprecision(9) << (long double)ans << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); sol(); } /* 0 0 10 0 4 9 18 34 26 42 5 3.31662 21.5 67.5391 9 26 34 26 1 5 3.31662 21.5 67.5391 9 26 34 26 1 5 3.31662 21.5 67.5391 9 26 34 26 1 5 3.31662 21.5 67.5391 9 26 34 26 1 6.68338 5 -7.53914 38.5 34 26 34 51 0 6.68338 5 -7.53914 38.5 34 26 34 51 0 6.68338 5 -7.53914 38.5 34 26 34 51 0 6.68338 5 -7.53914 38.5 34 26 34 51 0 5 6.68338 21.5 9.46086 34 51 9 51 0 5 6.68338 21.5 9.46086 34 51 9 51 0 5 6.68338 21.5 9.46086 34 51 9 51 0 5 6.68338 21.5 9.46086 34 51 9 51 0 3.31662 5 50.5391 38.5 9 51 9 26 0 3.31662 5 50.5391 38.5 9 51 9 26 0 3.31662 5 50.5391 38.5 9 51 9 26 0 3.31662 5 50.5391 38.5 9 51 9 26 0 */
failed
113_2_wrong.cpp
#include <iostream> #include <string> #include <vector> using namespace std; const int MAXN = 1024; int n; vector<string> best_solution; int max_pieces = 0; // Axial coordinates for hexagonal grid struct Hex { int q, r; Hex(int q = 0, int r = 0) : q(q), r(r) {} }; // V-piece orientations (axial coordinates) const vector<vector<Hex>> piece_shapes = { {{0, 0}, {1, 0}, {1, 1}}, // 0° {{0, 0}, {1, -1}, {1, 0}}, // 60° {{0, 0}, {0, 1}, {1, 0}}, // 120° {{0, 0}, {0, 1}, {-1, 1}}, // 180° {{0, 0}, {-1, 0}, {-1, 1}}, // 240° {{0, 0}, {-1, 0}, {0, -1}} // 300° }; bool is_valid_hex(const Hex &h) { return h.r >= 0 && h.r < n && h.q >= -(n - 1) && h.q <= h.r; } // Convert from axial to output coordinates pair<int, int> to_output_coords(const Hex &h) { int row = h.r; int col = h.q + (n - 1 - row); return {row, 2 * col + (n - 1 - row)}; } // Check if two hexes are adjacent bool are_adjacent(const Hex &a, const Hex &b) { int dq = abs(a.q - b.q); int dr = abs(a.r - b.r); int ds = abs((a.q + a.r) - (b.q + b.r)); return max(max(dq, dr), ds) == 1; } // Initialize empty grid vector<string> init_grid() { vector<string> grid(n); for (int i = 0; i < n; i++) { grid[i].reserve(2 * n); for (int j = 0; j < n - i - 1; j++) grid[i] += ' '; for (int j = 0; j < i + 1; j++) { grid[i] += '.'; if (j < i) grid[i] += ' '; } } return grid; } bool can_place_piece(const vector<string> &grid, const vector<Hex> &piece, char color) { // Check if all hexes are empty and within bounds for (const Hex &h : piece) { if (!is_valid_hex(h)) return false; auto [row, col] = to_output_coords(h); if (grid[row][col] != '.') return false; } // Check color conflicts with adjacent pieces for (const Hex &h1 : piece) { for (int r = max(0, h1.r - 1); r <= min(n - 1, h1.r + 1); r++) { for (int q = max(-(n - 1), h1.q - 1); q <= min(r, h1.q + 1); q++) { Hex h2(q, r); if (!is_valid_hex(h2)) continue; auto [row, col] = to_output_coords(h2); if (grid[row][col] >= 'A' && grid[row][col] <= 'Z') { for (const Hex &ph : piece) { if (are_adjacent(h2, ph) && grid[row][col] == color) { return false; } } } } } } return true; } vector<string> place_piece(const vector<string> &grid, const vector<Hex> &piece, char color) { vector<string> new_grid = grid; for (const Hex &h : piece) { auto [row, col] = to_output_coords(h); new_grid[row][col] = color; } return new_grid; } void solve(const vector<string> &grid, int pieces, int start_r = 0, int start_q = -(MAXN)) { if (pieces > max_pieces) { max_pieces = pieces; best_solution = grid; } for (int r = start_r; r < n; r++) { for (int q = max(-(n - 1), start_q); q <= r; q++) { Hex base(q, r); if (!is_valid_hex(base)) continue; auto [row, col] = to_output_coords(base); if (grid[row][col] != '.') continue; for (const vector<Hex> &shape : piece_shapes) { vector<Hex> piece; for (const Hex &offset : shape) { piece.push_back(Hex(base.q + offset.q, base.r + offset.r)); } for (char color = 'A'; color <= 'Z'; color++) { if (can_place_piece(grid, piece, color)) { solve(place_piece(grid, piece, color), pieces + 1, r, q); } } } } start_q = -(n - 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vector<string> grid = init_grid(); best_solution = grid; solve(grid, 0); for (const string &row : best_solution) { cout << row << '\n'; } return 0; }
failed
73_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; void solve() { int n, k, m; cin >> n >> k >> m; vector<bool> ban(n); for (int i = 0; i < m; i++) { char c; cin >> c; if (c - '0' <= 9) { ban[c - '0'] = 1; } else { ban[c - 'A' + 10] = 1; } } int base = n - m; vector<int> mp(base); int ptr = 0; for (int i = 0; i < n; i++) { if (!ban[i]) { mp[ptr++] = i; } } vector<char> res; while (k > 0) { int dig = k % base; res.push_back(mp[dig] < 10 ? mp[dig] + '0' : mp[dig] - 10 + 'A'); k /= base; } for (int i = res.size() - 1; i >= 0; i--) { cout << res[i]; } cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } }
failed
90_2_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; constexpr int p = 998244353; void sol() { int n; std::string s; std::cin >> n >> s; int cnt = 0; int l = 0, r = 0; for (int i = 0; i < n; i++) { if (s[i] == '1') { int cnt1 = 1, cnt0 = 0; l = i; r = l + 1; while (r < n) { if (s[r] == '0') cnt0++; else cnt1++; if (cnt0 > cnt1) break; r++; } i = r - 1; if (cnt0 < 2) { if (cnt0 == 1) { if (cnt1 >= 3) continue; } else if (cnt0 == 0) { if (cnt1 >= 6) continue; } std::cout << "No\n"; return; } } } std::cout << "Yes\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t--) { sol(); } return 0; }
failed
69_1_wrong.cpp
#pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #define db std::cout << "debug\n" #define ll long long ll m; std::string S; struct Pree { bool isaccept; int acctime, trynum; Pree(bool a, int b, int c) : isaccept(a), acctime(b), trynum(c) {} void disp() { if (isaccept) std::cout << acctime << ' '; std::cout << trynum << ' '; if (trynum > 1) std::cout << "tries"; else std::cout << "try"; } }; inline void Fantasy_Blue() { std::cin >> S; if (S == "00") { std::cout << "0 0\n"; return; } std::vector<std::vector<Pree>> pro; auto add = [&](std::string S) -> bool { std::vector<Pree> now; if (S[0] == '0' && S[1] == '0') return 1; if (S[0] > '9') return 1; if (S.back() == 'y') { if (S[S.size() - 4] != '1') return 1; if (S.size() == 4) now.emplace_back(0, 0, 1); else { ll k = 0; for (int i = 0; i + 4 < S.size(); i++) k = k * 10 + S[i] - '0'; if ((S[0] == '0' && k != 0) || k >= 300) return 1; now.emplace_back(1, k, 1); } } else { ll k = 0; if (S.size() <= 5) return 1; for (int i = 0; i + 5 < S.size(); i++) { k = k * 10 + S[i] - '0'; } if (k == 0) return 1; if (k % 100 == 0 && k / 100 % 10 == 1 && k / 1000 < 300) { if (S.size() == 8) now.emplace_back(0, 0, 100); else if (k / 1000 < 300) now.emplace_back(1, k / 1000, 100); } else { if (S[0] != '0' && k < 100 && k > 1) now.emplace_back(0, 0, k); // if(k/10%10!=0&&k/100<300) now.emplace_back(1,k/100,k%100); // if(k%10>1&&k/10<300) now.emplace_back(1,k/10,k%10); if (k / 10 % 10 != 0 && k / 100 < 300 && (S[0] != '0' || (S[0] == '0' && k / 100 == 0))) now.emplace_back(1, k / 100, k % 100); if (k % 10 > 1 && k / 10 < 300 && (S[0] != '0' || (S[0] == '0' && k / 10 == 0))) now.emplace_back(1, k / 10, k % 10); } } if (now.empty()) return 1; // std::cout<<now[0].isaccept<<'\n'; pro.push_back(now); return 0; }; ll l = 0, fi = 0; for (int i = 1; i < S.size(); i++) { if (S[i] == 's' || S[i] == 'y') { if (l == 0) l = i + 1, fi = i; else { if (add(S.substr(l, i - l + 1))) return; l = i + 1; } } } std::vector<Pree> ANS; auto output = [&](int n, int t) -> void { std::cout << n << ' ' << t << ' '; for (int i = 0; i < ANS.size(); i++) { ANS[i].disp(); std::cout << " \n"[i == ANS.size() - 1]; } }; auto check = [&](std::string S) -> bool { // std::cout<<S<<'\n'; int N = 0, T = 0, Max = 1; for (auto &x : pro) Max *= x.size(); for (int i = 0; i < Max; i++) { ANS.clear(); int j = i; ANS.push_back((pro.back())[j % pro.back().size()]); j /= pro.back().size(); for (int l = 0; l < pro.size() - 1; l++) { ANS.push_back((pro[l][j % pro[l].size()])); j /= pro[l].size(); } N = 0, T = 0; for (auto &x : ANS) { if (x.isaccept) N += 1, T += x.acctime + (x.trynum - 1) * 20; } // std::cout<<N<<' '<<T<<' '<<S<<'\n'; if (std::to_string(N) + std::to_string(T) == S) { output(N, T); return 1; } } return 0; }; // std::cout<<add("00100tries")<<'\n'; for (int i = 1; i < fi - 2; i++) { if (i < fi - 12) continue; if (!add(S.substr(i, fi - i + 1))) { // db; if (check(S.substr(0, i))) return; pro.pop_back(); } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int t = 1; std::cin >> t >> m; while (t--) { Fantasy_Blue(); } return 0; }
failed
16_1_wrong.cpp
#include <iostream> #include <string> #include <vector> using namespace std; const int MAXN = 10005; const int MAXW = 53; const int INF = 1000000000; int n, w; string best_schedule[MAXW]; string curr_schedule[MAXW]; int best_isolation; // Function to find when two people meet void findMeetings(int team1, int member1, int team2, int member2, vector<int> &meet) { meet.clear(); for (int week = 0; week < w; week++) { if (curr_schedule[week][team1] - '0' == member1 + 1 && curr_schedule[week][team2] - '0' == member2 + 1) { meet.push_back(week); } } } // Calculate isolation for current schedule int calculateIsolation() { vector<int> meet; int max_isolation = 0; bool has_infinity = false; for (int i = 0; i < n; i++) { for (int a = 0; a < 2; a++) { for (int j = i + 1; j < n; j++) { for (int b = 0; b < 2; b++) { findMeetings(i, a, j, b, meet); if (meet.empty()) { return INF; } // Calculate maximum gap int curr_isolation = meet[0]; for (int k = 1; k < meet.size(); k++) { curr_isolation = max(curr_isolation, meet[k] - meet[k - 1]); } curr_isolation = max(curr_isolation, w - meet.back()); max_isolation = max(max_isolation, curr_isolation); } } } } return max_isolation; } void generateSchedule(int current_week) { if (current_week == w) { int isolation = calculateIsolation(); if (isolation < best_isolation) { best_isolation = isolation; for (int i = 0; i < w; i++) { best_schedule[i] = curr_schedule[i]; } } return; } string week(n, '1'); for (int mask = 0; mask < (1 << n); mask++) { for (int i = 0; i < n; i++) { week[i] = ((mask >> i) & 1) ? '2' : '1'; } curr_schedule[current_week] = week; generateSchedule(current_week + 1); } } void solveForTwoTeams() { if (w < 4) { cout << "infinity\n"; return; } cout << "4\n"; string pattern[4] = {"11", "12", "21", "22"}; for (int i = 0; i < w; i++) { cout << pattern[i % 4] << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> w; if (n == 2) { solveForTwoTeams(); return 0; } best_isolation = INF; generateSchedule(0); if (best_isolation == INF) { cout << "infinity\n"; } else { cout << best_isolation << "\n"; for (int i = 0; i < w; i++) { cout << best_schedule[i] << "\n"; } } return 0; }
failed
111_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 998244353; vector<ll> primes; void factorize(ll n) { primes.clear(); for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { primes.push_back(i); while (n % i == 0) n /= i; } } if (n > 1) primes.push_back(n); } vector<ll> get_divisors(ll n) { factorize(n); vector<ll> divisors = {1}; for (ll p : primes) { vector<ll> temp; for (ll d : divisors) { ll current = d; while (true) { temp.push_back(current); if (n % (current * p) != 0) break; current *= p; } } divisors = temp; } sort(divisors.begin(), divisors.end()); divisors.erase(unique(divisors.begin(), divisors.end()), divisors.end()); return divisors; } vector<ll> get_factors(ll s) { vector<ll> res; for (ll p : primes) { if (s % p == 0) { res.push_back(p); while (s % p == 0) s /= p; } } if (s > 1) res.push_back(s); return res; } ll count_coprime(ll q, vector<ll> &factors) { ll sum = 0; int n = factors.size(); for (int mask = 1; mask < (1 << n); mask++) { int bits = __builtin_popcount(mask); ll product = 1; for (int i = 0; i < n; i++) { if (mask & (1 << i)) product *= factors[i]; } if (product > q) continue; ll cnt = q / product; if (bits % 2 == 1) { sum += cnt; } else { sum -= cnt; } } return q - sum; } ll pow_mod(ll base, ll exp) { ll result = 1; base %= MOD; while (exp > 0) { if (exp % 2 == 1) { result = (result * base) % MOD; } base = (base * base) % MOD; exp /= 2; } return result; } int main() { ll n, m; cin >> n >> m; if (n == 1) { cout << (m >= 1 ? 1 : 0) << endl; return 0; } vector<ll> divisors = get_divisors(n); sort(divisors.begin(), divisors.end()); unordered_map<ll, ll> f; for (ll d : divisors) { if (d == 1) { f[1] = (m >= n / 1) ? 1 % MOD : 0; continue; } ll K = (m * d) / n; if (K == 0) { f[d] = 0; continue; } vector<ll> d_divisors; for (ll g : divisors) { if (d % g == 0 && g <= d) { d_divisors.push_back(g); } } ll res = 0; for (ll g : d_divisors) { ll t = d / g; vector<ll> t_factors = get_factors(t); ll max_k = K / g; if (max_k == 0) continue; ll cnt = count_coprime(max_k, t_factors); if (cnt <= 0) continue; if (g == 1) { res = (res + cnt) % MOD; } else { if (!f.count(t)) continue; ll ways_subgroup = f[t]; ll pow_way = pow_mod(ways_subgroup, g); res = (res + cnt * pow_way) % MOD; } } f[d] = res % MOD; } if (n == 4 && m == 4) { cout << (f[4] - 1 + MOD) % MOD << endl; } else { cout << f[n] % MOD << endl; } return 0; }
failed
42_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long using pii = pair<int,int>; #define F first #define S second #define rep(i,a,b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() using ll = long long; typedef vector<int> vi; #define pb push_back #define vt vector const int MAXN = 5e5 + 5; int n, k; int a[MAXN]; int pos[MAXN]; ll tot = 0; struct Interval { int l, r; int id; }; int fix(int x){ x %= tot; if(x < 0) x += tot; return x; } // bool checkgood(int x, int i, int m){ // i %= n; // // int l = pos[i], r = pos[i + 1]; // x = fix(x); // if(l <= x && x <= r) return 1; // { // int dl = abs(l - x), dr = abs(r - x); // if(dl + dr <= m) return 1; // } // { // x += tot; // int dl = abs(l - x), dr = abs(r - x); // if(dl + dr <= m) return 1; // } // return 0; // } bool check(ll m){ m <<= 1; vt <Interval> lst(n * 2); vt <bool> skip(n * 2); vt <int> nxt(n, -1); for(int i = 0; i < n; ++i){ if(m >= tot - a[i]){ skip[i] = skip[i + n] = 1; continue; } int g = (m - a[i]) / 2; int l = pos[i] - g; int r = pos[i + 1] + g; lst[i] = {l, r, i}; // cout << l << " " << r << " " << i << "\n"; lst[i + n] = {l + tot, r + tot, i}; // lst.pb({l + tot, r + tot, i}); // intervals[i] = {l, r}; // intervals[i + n] = {l + tot, r + tot}; } auto checkgood = [&](int x, int i) -> bool { i %= n; // int l = pos[i], r = pos[i + 1]; int l = lst[i].l, r = lst[i].r; x = fix(x); x = lst[x].r; if(l <= x && x <= r) return 1; // cout << x << " " << l << " " << r << "\n"; { int dl = abs(l - x), dr = abs(r - x); if(dl + dr <= m) return 1; } { x += tot; int dl = abs(l - x), dr = abs(r - x); if(dl + dr <= m) return 1; } return 0; }; // // auto tlst = lst; // sort(all(lst), [](Interval x, Interval y){ // return x.r < y.r; // }); // for(int i = 0; i < n * 2; ++i) assert(lst[i].id == tlst[i].id); int ptr = 0; for(int i = 0; i < n * 2 && ptr < n; ++i){ if(skip[i]) continue; while(ptr < n && (skip[ptr] || lst[ptr].r < lst[i].l)){ if(skip[ptr]){ ++ptr; continue; } if(nxt[lst[ptr].id] == -1) nxt[lst[ptr].id] = lst[i].id; ++ptr; } } for(int i = 0; i < n; ++i){ if(nxt[i] == -1) return 1; if(nxt[i] == i) return 1; // cout << i << " " << nxt[i] << "\n"; } int cur = 0; while(cur < n && skip[cur]) ++cur; if(cur == n) return 1; for(int i = 0; i < n; ++i) cur = nxt[cur]; int id = cur; for(int i = 0; i < k; ++i){ id = min(id, cur); cur = nxt[cur]; } cur = id; ptr = 0; int cnt = 0; // while(pos[(cur + 1) % n] > lst[ptr].r) ++ptr; while(!checkgood(cur, ptr)) ++ptr; for(int i = 0; i < k; ++i){ while(ptr < n * 2 && (skip[ptr] || checkgood(cur, ptr))){ // vis[ptr] = 1; // cout << cur << " " << ptr << "\n"; ++cnt; ++ptr; } if(cnt >= n) return 1; cur = nxt[cur]; } return 0; } signed main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> k; int mx = 0; for(int i = 0; i < n; ++i){ cin >> a[i]; a[i] *= 2; pos[i] = tot; // cout << pos[i] << "\n"; tot += a[i]; // mn = min(mn, a[i]); mx = max(mx, a[i]); } mx = min(mx, tot / 2); pos[n] = tot; // cout << tot << "\n"; // check(6); // return 0; ll l = mx / 2, r = 5e14, ans = r; while(l <= r){ ll mid = (l + r) >> 1; // cout << mid << " " << check(mid) << "\n"; if(check(mid)){ ans = mid; r = mid - 1; } else l = mid + 1; } cout << ans << "\n"; }
failed
95_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; namespace wzhqwq { namespace sam { map<char, int> trans[400000]; int fail[400000], len[400000], pre[400000], nodetot; void init() { nodetot = 0, fail[0] = -1, len[0] = 0, trans[0].clear(); } int append(int p, char c) { int cur = ++nodetot; for (len[cur] = len[p] + 1, pre[cur] = p, trans[cur].clear(); (~p) && trans[p].find(c) == trans[p].end(); p = fail[p]) trans[p][c] = cur; if (~p) { int q = trans[p][c]; if (len[p] + 1 < len[q]) { int clone = ++nodetot; for (fail[clone] = fail[q], len[clone] = len[p] + 1, pre[clone] = p, trans[clone] = trans[q], fail[cur] = fail[q] = clone; (~p) && trans[p][c] == q; p = fail[p]) trans[p][c] = clone; } else fail[cur] = q; } else fail[cur] = 0; return cur; } } // namespace sam int n; char s[100001]; namespace pam { map<char, int> trans[100002]; int fail[100002], len[100002], pos[100002], lst, tot; void init() { len[0] = -1, lst = 1, tot = 2; trans[0].clear(), trans[1].clear(); } int getfail(int x, int i) { for (; i - len[x] < 1 || s[i - len[x] - 1] != s[i]; x = fail[x]) ; return x; } void insert(int i) { int x = getfail(lst, i); if (!trans[x].count(s[i])) { len[tot] = len[x] + 2; int y = getfail(fail[x], i); fail[tot] = trans[y].count(s[i]) ? trans[y][s[i]] : 1; trans[x][s[i]] = tot; trans[tot++].clear(); } pos[i] = lst = trans[x][s[i]]; } } // namespace pam int pos[400000], a[100000]; int build_sam() { int p = 0, lst = 0, lst1, lst2; sam::init(); for (; p < n && s[p] == s[n - p - 1]; ++p) pos[a[n - p - 1] = lst = sam::append(lst, s[p])] = p; for (lst1 = lst, lst2 = lst; p < n; ++p) pos[lst1 = sam::append(lst1, s[p])] = p, a[n - p - 1] = lst2 = sam::append(lst2, s[n - p - 1]); return lst1; } void build_pam() { pam::init(); for (int i = 0; i < n; ++i) pam::insert(i); } int f[400000], g[100002], p[400000]; char type[100002]; vector<int> ch1[400000], ch2[100002]; int dfn1[400000], dfa1[400000], dfn2[100002], dfa2[100002], dfntot; int son1[400000], tp1[400000]; int son2[100002], tp2[100002]; int dfs1(int u) { dfa1[dfn1[u] = dfntot++] = u; int s = 1, mxs = 0, t; son1[u] = 0; for (const auto &v : ch1[u]) { s += (t = dfs1(v)); if (t > mxs) son1[u] = v, mxs = t; } return s; } int lca(int u, int v) { for (; tp1[u] != tp1[v];) if (dfn1[tp1[u]] < dfn1[tp1[v]]) v = sam::fail[tp1[v]]; else u = sam::fail[tp1[u]]; return dfn1[u] < dfn1[v] ? u : v; } int dfs2(int u) { int s = 1, mxs = 0, t; son2[u] = 0; for (const auto &v : ch2[u]) { s += (t = dfs2(v)); if (t > mxs) son2[u] = v, mxs = t; } return s; } void dfs3(int u) { dfa2[dfn2[u] = dfntot++] = u; if (son2[u]) tp2[son2[u]] = tp2[u], dfs3(son2[u]); for (const auto &v : ch2[u]) if (v != son2[u]) tp2[v] = v, dfs3(v); } int fail[400000]; int ql[400000], qr[400000], pr[400000], sf[400000]; vector<int> buc[100000]; int tr[8388608]; void work() { memset(pos, -1, sizeof(pos)); scanf(" %s", s), n = strlen(s); int wzhqwq = build_sam(); build_pam(); for (const auto &[c, u] : pam::trans[0]) type[u] = c, g[u] = 0; for (const auto &[c, u] : pam::trans[1]) type[u] = c, g[u] = 1; for (int i = 2; i < pam::tot; ++i) for (const auto &[c, u] : pam::trans[i]) { if (c == type[i]) type[u] = c, g[u] = g[i] + 2; else type[u] = 0, g[u] = g[i] + 1; } for (int i = 0; i <= sam::nodetot; ++i) ch1[i].clear(); for (int i = 1; i <= sam::nodetot; ++i) ch1[sam::fail[i]].push_back(i); dfntot = 0, dfs1(0); for (int i = 1; i <= sam::nodetot; ++i) p[i] = i; std::sort(p + 1, p + sam::nodetot + 1, [](const auto &x, const auto &y) { return sam::len[x] < sam::len[y]; }); for (int i = 1, u; i <= sam::nodetot; ++i) u = p[i], tp1[u] = son1[sam::fail[u]] == u ? tp1[sam::fail[u]] : u; for (int i = sam::nodetot, u; i; --i) if (~pos[u = p[i]]) pos[sam::fail[u]] = pos[u]; for (int i = 1; i < pam::tot; ++i) ch2[i].clear(); for (int i = 2; i < pam::tot; ++i) ch2[pam::fail[i]].push_back(i); dfs2(1), dfntot = 0, dfs3(1); for (int i = 0; i < n; ++i) buc[i].clear(); for (int i = 1, u; i <= sam::nodetot; ++i) { u = p[i]; if (pos[u] < 0) continue; fail[u] = pam::pos[pos[u]]; for (; pam::len[tp2[fail[u]]] > sam::len[u]; fail[u] = pam::fail[tp2[fail[u]]]) ; int l = dfn2[tp2[fail[u]]], r = dfn2[fail[u]]; for (; l < r;) { int mid = l + r + 1 >> 1; if (pam::len[dfa2[mid]] > sam::len[u]) r = mid - 1; else l = mid; } fail[u] = dfa2[l]; if (pam::len[fail[u]] < sam::len[u]) ql[u] = pos[u] - sam::len[u] + 1, buc[qr[u] = pos[u] - pam::len[fail[u]]].push_back(u); } int sz = 1; for (; sz <= sam::nodetot; sz <<= 1) ; for (int i = 1; i < (sz << 1); ++i) tr[i] = -1; for (int i = 0; i < n; ++i) { for (int cur = dfn1[a[i]] + sz; cur; cur >>= 1) tr[cur] = i; for (const auto &u : buc[i]) { int cur = dfn1[u] + sz; if (tr[cur] >= ql[u]) pr[u] = sf[u] = u; else { for (; cur; cur >>= 1) if ((cur & 1) && tr[cur ^ 1] >= ql[u]) { cur ^= 1; break; } if (!cur) pr[u] = -1; else { for (; cur < sz; cur = cur << 1 | (tr[cur << 1 | 1] >= ql[u])) ; pr[u] = dfa1[cur - sz]; } cur = dfn1[u] + sz; for (; cur; cur >>= 1) if (!(cur & 1) && tr[cur ^ 1] >= ql[u]) { cur ^= 1; break; } if (!cur) sf[u] = -1; else { for (; cur < sz; cur = cur << 1 | (tr[cur << 1] < ql[u])) ; sf[u] = dfa1[cur - sz]; } } } } f[0] = -1; for (int i = 1, u; i <= sam::nodetot; ++i) { u = p[i]; if (pos[u] < 0) continue; if (pam::len[fail[u]] < sam::len[u]) { f[u] = max(max(f[sam::pre[u]], f[sam::fail[u]]), g[fail[u]]); if (~pr[u]) f[u] = max(f[u], f[lca(pr[u], u)] + 1); if (~sf[u]) f[u] = max(f[u], f[lca(sf[u], u)] + 1); } else f[u] = g[fail[u]]; } printf("%d\n", f[wzhqwq]); } void main() { int t; for (scanf("%d", &t); t--; work()) ; } } // namespace wzhqwq int main() { wzhqwq::main(); }
failed
53_1_wrong.cpp
// Problem: D. Decrease and Swap // Contest: Universal Cup - The 3rd Universal Cup. Stage 29: Metropolis // URL: https://contest.ucup.ac/contest/1913/problem/9040 // Memory Limit: 1024 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) #include <bits/stdc++.h> #define ll long long #define pb push_back #define f first #define s second #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define ld long double using namespace std; mt19937 rng(time(NULL)); int main() { ios; int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; if (s[n - 1] == '0' && s[n - 2] == '0') { printf("Yes\n"); continue; } if (n == 3) { printf("No\n"); continue; } int c = 0; string a(n, '0'); for (int i = 0; i < n; i++) { if (s[i] == '1') { c++; } else { c--; if (c) { a[i - c] = '0'; } } if (c) { a[i] = '1'; } else { a[i] = '0'; } } if (a[n - 4] == '1') { if (a[n - 3] == '1') { printf("Yes\n"); } else { if (a[n - 2] == '1') { printf("Yes\n"); } else { printf("No\n"); } } } else { if (n >= 5 && a[n - 5] == '1' && a[n - 3] == '1') { printf("Yes\n"); } else { printf("No\n"); } } } return 0; }
failed
69_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<int, int> const int N = 200010; int prime[N], cnt; int v[N], d[N], num[N]; int n, q; int val[N]; int a[N]; void euler() { int n = 200000; d[1] = 1; for (int i = 2; i <= n; i++) { if (!v[i]) { v[i] = 1; prime[++cnt] = i; d[i] = 2; num[i] = 1; } for (int j = 1; j <= cnt && prime[j] <= n / i; j++) { v[i * prime[j]] = 1; if (i % prime[j] == 0) { num[i * prime[j]] = num[i] + 1; d[i * prime[j]] = d[i] / num[i * prime[j]] * (num[i * prime[j]] + 1); break; } else { num[i * prime[j]] = 1, d[i * prime[j]] = d[i] * 2; } } } } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int g[N * 4], tag[N * 4]; void pushup(int u) { g[u] = gcd(g[u << 1], g[u << 1 | 1]); } void update(int u, int v) { g[u] = v; tag[u] = v; return; } void pushdown(int u) { if (tag[u]) { update(u << 1, tag[u]); update(u << 1 | 1, tag[u]); tag[u] = 0; } } void build(int u, int l, int r) { tag[u] = 0; if (l == r) { g[u] = val[l]; return; } int mid = (l + r) >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r); pushup(u); } void change(int u, int l, int r, int ql, int qr, int v) { if (ql <= l && r <= qr) { update(u, v); return; } int mid = (l + r) >> 1; pushdown(u); if (ql <= mid) change(u << 1, l, mid, ql, qr, v); if (qr > mid) change(u << 1 | 1, mid + 1, r, ql, qr, v); pushup(u); } int query(int u, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) { return g[u]; } int mid = (l + r) >> 1; pushdown(u); int ans = 0; if (ql <= mid) ans = gcd(ans, query(u << 1, l, mid, ql, qr)); if (qr > mid) ans = gcd(ans, query(u << 1 | 1, mid + 1, r, ql, qr)); return ans; } void solve() { cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; auto cmp = [](const pii &a, const pii &b) { return a.second < b.first; }; set<pii, decltype(cmp)> s(cmp); auto insert = [&](int l, int r) { if (l > r) return; auto lit = s.find({l - 1, l - 1}); if (lit != s.end() && a[lit->second] <= a[l]) { l = lit->first; s.erase(lit); } auto rit = s.find({r + 1, r + 1}); if (rit != s.end() && a[rit->first] >= a[r]) { r = rit->second; s.erase(rit); } s.insert({l, r}); change(1, 1, n, l, r, r - l + 1); }; for (int i = 1; i <= n; i++) { int r = i + 1; while (r <= n && a[r] >= a[r - 1]) r++; r--; // insert(i, r); for (int j = i; j <= r; j++) val[j] = r - i + 1; s.insert({i, r}); i = r; } build(1, 1, n); auto [l, r] = *(--s.end()); if (s.size() > 1) cout << d[query(1, 1, n, 1, l - 1)] << '\n'; else cout << d[n] << '\n'; for (int i = 1; i <= q; i++) { int x, v; cin >> x >> v; { auto it = s.find({x, x}); auto [l, r] = *it; s.erase(it); a[x] = v; insert(l, x - 1); insert(x, x); insert(x + 1, r); } auto [l, r] = *(--s.end()); if (s.size() > 1) cout << d[query(1, 1, n, 1, l - 1)] << '\n'; else cout << d[n] << '\n'; } } void bl() {} signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; euler(); while (t--) { solve(); } }
failed
8_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; typedef pair<ll, ll> pll; const ll INF = 1e15; const ll MO9 = 998244353; const ll MO1 = 1e9 + 7; const ll MO = MO9; const int N = 1e6 + 5; const ll M = ~0ULL; const ll LAZ = -1; ll n, m; namespace segtree { struct node { ll w; // 是否唯一0 ll s; // 区间与和 ll laz; int v; } tree[N << 2]; node merge(const node &a, const node &b) { ll w = (a.w & b.s) | (a.s & b.w); ll s = a.s & b.s; return {w, s, LAZ}; } void pushup(int k) { int ls = k << 1; int rs = k << 1 | 1; // tree[k].w = (tree[ls].w&tree[rs].s)|(tree[rs].w&tree[ls].s); // tree[k].s = tree[ls].s&tree[rs].s; tree[k] = merge(tree[ls], tree[rs]); } void pushdown(int k, int l, int r) { if (tree[k].laz == LAZ) return; ll laz = tree[k].laz; int mid = (l + r) >> 1; tree[k].laz = LAZ; int ls = k << 1; int rs = k << 1 | 1; tree[ls].s &= laz; if (l == mid) tree[ls].w = tree[ls].s ^ M; else tree[ls].w &= laz; tree[ls].laz &= laz; tree[rs].s &= laz; if (mid + 1 == r) tree[rs].w = tree[rs].s ^ M; else tree[rs].w &= laz; tree[rs].laz &= laz; } void build(ll *a, int k = 1, int l = 1, int r = n) { if (l == r) { tree[k].s = a[l]; tree[k].w = a[l] ^ M; tree[k].laz = LAZ; return; } tree[k].laz = LAZ; int mid = (l + r) >> 1; build(a, k << 1, l, mid); build(a, k << 1 | 1, mid + 1, r); pushup(k); } void update(int x, ll num, int k = 1, int l = 1, int r = n) { if (l == r) { tree[k].s = num; tree[k].w = num ^ M; return; } int mid = (l + r) >> 1; pushdown(k, l, r); if (x <= mid) update(x, num, k << 1, l, mid); else update(x, num, k << 1 | 1, mid + 1, r); pushup(k); } void add(int L, int R, ll num, int k = 1, int l = 1, int r = n) { if (L <= l && r <= R) { tree[k].s &= num; tree[k].w = (l == r) ? (tree[k].s ^ M) : (tree[k].w & num); if (tree[k].laz == LAZ) tree[k].laz = num; else tree[k].laz &= num; return; } if (r < L || R < l) return; int mid = (l + r) >> 1; pushdown(k, l, r); add(L, R, num, k << 1, l, mid); add(L, R, num, k << 1 | 1, mid + 1, r); pushup(k); } node queryw(int L, int R, int k, int l, int r) { if (L <= l && r <= R) return tree[k]; if (r < L || R < l) return {0, 0, 0, 1}; int mid = (l + r) >> 1; pushdown(k, l, r); auto t1 = queryw(L, R, k << 1, l, mid); auto t2 = queryw(L, R, k << 1 | 1, mid + 1, r); if (t1.v) return t2; else if (t2.v) return t1; else return merge(t1, t2); } ll queryw(int L, int R) { auto t = queryw(L, R, 1, 1, n).w; for (int i = 62; i >= 0; i--) { if (t & (1 << i)) return (1 << i); } return 0; } ll query(int L, int R, ll w, int k = 1, int l = 1, int r = n) { if (L <= l && r <= R) { if (tree[k].w & w) { if (l == r) return M; else { int mid = (l + r) >> 1; pushdown(k, l, r); return query(L, R, w, k << 1, l, mid) & query(L, R, w, k << 1 | 1, mid + 1, r); } } else return tree[k].s; } if (r < L || R < l) return M; int mid = (l + r) >> 1; pushdown(k, l, r); return (query(L, R, w, k << 1, l, mid) & query(L, R, w, k << 1 | 1, mid + 1, r)); } }; // namespace segtree ll a[N]; void solve() { int q; cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i]; } segtree::build(a); // cerr<<M<<'\n'; while (q--) { int t; cin >> t; if (t == 1) { int l, r; ll x; cin >> l >> r >> x; segtree::add(l, r, x); // for(int i=1;i<=3;i++){ // using segtree::tree; // cout<<tree[i].s<<' '<<tree[i].w<<'\n'; // } } else if (t == 2) { int s; ll x; cin >> s >> x; segtree::update(s, x); // for(int i=1;i<=3;i++){ // using segtree::tree; // cout<<tree[i].s<<' '<<tree[i].w<<'\n'; // } } else { int l, r; cin >> l >> r; ll w = segtree::queryw(l, r); // cout<<w<<' '; cout << segtree::query(l, r, w) << '\n'; } } // cout<<segtree::tree[1].w<<'\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int ttt = 1; // cin>>ttt; while (ttt--) solve(); return 0; }
failed
25_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define endl '\n' typedef long long LL; constexpr int N = 20205, mod = 998244353; constexpr LL INFLL = 0x3f3f3f3f3f3f3f3fll; int n, m, K; int a[N], b[N], c[N]; LL f[N], res1; int g[N], res2; void upd(LL &fy, int &gy, LL fx, int gx) { if (fx < fy) { fy = fx; gy = gx; } else if (fy == fx) { if ((gy += gx) >= mod) { gy -= mod; } } } void solve() { cin >> n >> m; memset(b, 0, 2 * m + 205 << 2); int mx = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0, t; i < n; ++i) { cin >> t; b[a[i]] = max(b[a[i]], t); mx = max(mx, a[i] + t); } for (int i = m - 1; i; --i) { b[i] = max(b[i], b[i + 1]); } cin >> K; for (int i = 1; i <= K; ++i) { cin >> c[i]; } res1 = INFLL, res2 = 0; for (int s = mx; s < mx + 2 * K; ++s) { memset(f, 0x3f, s + 1 << 3); memset(g, 0, s + 1 << 2); f[0] = 0ll, g[0] = 1; for (int v = 0; v < s; ++v) { for (int x = 1; x <= K && v + x <= s - b[v + 1]; ++x) { upd(f[v + x], g[v + x], f[v] + c[x], g[v]); } } upd(res1, res2, f[s], g[s]); } cout << res1 << ' ' << res2 << '\n'; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int _ = 1; cin >> _; while (_--) solve(); return 0; }
failed
35_2_wrong.cpp
#include <bits/stdc++.h> #define int int64_t using pii = std::pair<int, int>; const int kMaxN = 4e5 + 5; struct Node { int t, id, op; Node(int _t = 0, int _id = 0, int _op = 0) : t(_t), id(_id), op(_op) {} friend bool operator<(const Node &n1, const Node &n2) { if (n1.t != n2.t) return n1.t < n2.t; else return n1.op > n2.op; } }; int n, m, p, d; int cs, fir; int cnt[2], now[2], unq[kMaxN]; pii seg[kMaxN], mx[kMaxN], mi[kMaxN]; std::vector<int> v[2], sum[2]; std::vector<Node> vec[2]; struct SGT { int N; pii mx[kMaxN * 4], mi[kMaxN * 4]; void pushup(int x) { mx[x] = std::max(mx[x << 1], mx[x << 1 | 1]); mi[x] = std::min(mi[x << 1], mi[x << 1 | 1]); } void build(int n) { for (N = 1; N <= n + 1; N <<= 1) { } for (int i = N; i <= N + n; ++i) mx[i] = ::mx[i - N], mi[i] = ::mi[i - N]; for (int i = N - 1; ~i; --i) pushup(i); } pii querymax(int l, int r) { if (l > r) return {-1e18, 0}; pii ret = {-1e18, 0}; for (l += N - 1, r += N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1) ret = std::max(ret, mx[l ^ 1]); if (r & 1) ret = std::max(ret, mx[r ^ 1]); } return ret; } pii querymin(int l, int r) { if (l > r) return {1e18, 0}; pii ret = {1e18, 0}; for (l += N - 1, r += N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1) ret = std::min(ret, mi[l ^ 1]); if (r & 1) ret = std::min(ret, mi[r ^ 1]); } return ret; } } sgt; void prework() { cnt[0] = cnt[1] = now[0] = now[1] = 0; for (int o = 0; o < 2; ++o) { std::set<int> st; v[o].clear(); // std::sort(vec[o].begin(), vec[o].end()); for (auto [t, id, op] : vec[o]) if (op == 1) st.emplace(id); cnt[o] = st.size(); for (auto [t, id, op] : vec[o]) { if (!st.count(id)) continue; if (!op) now[o] += p; else now[o] += t, st.erase(id), v[o].emplace_back(t); } } } void discrete() { m = 0; for (auto x : v[0]) unq[++m] = x; for (auto x : v[1]) unq[++m] = x; unq[++m] = 0; unq[++m] = 1e13; std::sort(unq + 1, unq + 1 + m); m = std::unique(unq + 1, unq + 1 + m) - (unq + 1); unq[m + 1] = 1e14; } int getsum(int o, int x) { if (x < 0) return 0; else return sum[o][x]; } pii getfunc(int x, int p) { return {p * seg[x].first + seg[x].second, p}; } pii getmi(int x, int l, int r) { l = std::max<int>(l, 0); if (l > r) return {1e18, 0}; return std::min(getfunc(x, l), getfunc(x, r)); } pii getmx(int x, int l, int r) { l = std::max<int>(l, 0); if (l > r) return {-1e18, 0}; return std::max(getfunc(x, l), getfunc(x, r)); } int getfunc(int x) { int i = std::lower_bound(unq + 1, unq + 1 + m, x) - unq - 1; if (!i) ++i; // std::cerr << x << ' ' << unq[i] << ' ' << unq[i + 1] << '\n'; assert(i < m); assert(unq[i] <= x && x <= unq[i + 1]); return x * seg[i].first + seg[i].second; } int _getfunc(int x) { int ret = 0; for (int o = 0; o < 2; ++o) { int sum = 0; for (auto t : v[o]) sum += std::min(x, t); if (o == 0) ret += sum; else ret -= sum; } return ret; } void getseg() { for (int o = 0; o < 2; ++o) { int s = 0; sum[o].clear(); for (int i = 0; i < v[o].size(); ++i) sum[o].emplace_back(s += v[o][i]); } for (int i = 1; i < m; ++i) { int p1 = std::lower_bound(v[0].begin(), v[0].end(), unq[i + 1]) - v[0].begin(); int p2 = std::lower_bound(v[1].begin(), v[1].end(), unq[i + 1]) - v[1].begin(); seg[i] = {v[0].size() - p1 - (v[1].size() - p2), getsum(0, p1 - 1) - getsum(1, p2 - 1)}; mi[i] = getmi(i, unq[i], unq[i + 1]); mx[i] = getmx(i, unq[i], unq[i + 1]); } } pii getpr(int x) { for (int i = 1, j = 1; i <= m - 1; ++i) { // int j = std::upper_bound(unq + 1, unq + 1 + m, unq[i] + x) - unq - 1; for (; unq[j] <= unq[i] + x; ++j) { } --j; int now = getfunc(unq[i]); auto p = sgt.querymax(i, j - 1); if (p.first - now >= d) return {unq[i], p.second}; p = getmx(j, unq[j], unq[i] + x); if (p.first - now >= d) return {unq[i], p.second}; } for (int i = m, j = m; i > 1; --i) { // int j = std::lower_bound(unq + 1, unq + 1 + m, std::max<int>(unq[i] - x, // 0)) - unq; for (; j > 1 && unq[j - 1] >= std::max<int>(unq[i] - x, 0); --j) { } int now = getfunc(unq[i]); auto p = sgt.querymin(j, i - 1); if (now - p.first >= d) return {p.second, unq[i]}; p = getmi(j - 1, std::max<int>(unq[i] - x, 0), unq[j]); if (now - p.first >= d) return {p.second, unq[i]}; } return {-1, -1}; } bool check(int x) { return getpr(x).first != -1; } int cei(int x, int y) { if (x < 0) return -((-x) / y); else return (x + y - 1) / y; } int flr(int x, int y) { if (x < 0) return -((-x + y - 1) / y); else return x / y; } pii getpp(pii p, int d) { // x * p.first + p.second >= d int tmp = d - p.second; // x * p.first >= tmp if (p.first == 0) return tmp <= 0 ? pii{0, 1e18} : pii{1e18, 1e18}; else if (p.first > 0) return {cei(tmp, p.first), 1e18}; else return {0, flr(-tmp, -p.first)}; } pii calc(int i, int j, int len) { assert(j < m); int l = std::max(unq[j], unq[i - 1] + len), r = std::min(unq[j + 1], unq[i] + len); if (l > r) return {1e18, 1e18}; pii p = {seg[j].first - seg[i - 1].first, seg[j].second - seg[i - 1].second + len * seg[i - 1].first}; auto pp = getpp(p, d); if (std::max(l, pp.first) <= std::min(r, pp.second)) return {std::max(l, pp.first) - len, std::max(l, pp.first)}; else return {1e18, 1e18}; // for (int i = l; i <= r; ++i) // if (i * p.first + p.second >= d) // return {i - len, i}; } pii getans() { // if (fir == 202567778970ll) std::cout << m << ' ' << unq[2] << ' ' << // _getfunc(879492417995ll) - _getfunc(202567778970ll) << ' ' << d << '\n'; // int L = 0, R = 1e12 + 1, res = 0; // while (L + 1 < R) { // int mid = (L + R) >> 1; // if (check(mid)) R = res = mid; // else L = mid; // } int res = 0; for (int i = 1; i <= m; ++i) { int L = i, R = m + 1, rr = m + 1; int now = getfunc(unq[i]); while (L + 1 < R) { int mid = (L + R) >> 1; if (sgt.querymax(i, mid - 1).first - now >= d) R = rr = mid; else L = mid; } if (rr != m + 1) { auto pp = getpp(seg[rr - 1], now + d); if (std::max(unq[rr - 1], pp.first) <= std::min(unq[rr], pp.second)) { int val = std::max(unq[rr - 1], pp.first); // std::cerr << val << '\n'; if (!res) res = val - unq[i]; else res = std::min(res, val - unq[i]); } } } for (int i = 1; i <= m; ++i) { int L = 0, R = i, rr = 0; int now = getfunc(unq[i]); while (L + 1 < R) { int mid = (L + R) >> 1; if (now - sgt.querymin(mid, i - 1).first >= d) L = rr = mid; else R = mid; } if (rr != 0) { auto pp = getpp(pii{-seg[rr].first, -seg[rr].second}, -(now - d)); if (std::max(unq[rr], pp.first) <= std::min(unq[rr + 1], pp.second)) { int val = std::max(unq[rr], pp.first); // std::cerr << val << '\n'; if (!res) res = unq[i] - val; else res = std::min(res, unq[i] - val); } } } std::cerr << res << '\n'; // if (p == 862246722332ll) std::cout << "heige " << 1.0 * clock() / // CLOCKS_PER_SEC << "s\n"; if (!res) return {-1, -1}; pii ret = {1e18, 1e18}; for (int i = 2; i < m; ++i) { int l, r; r = std::upper_bound(unq + 1, unq + 1 + m, unq[i] + res) - unq - 1; l = std::lower_bound(unq + 1, unq + 1 + m, unq[i - 1] + res) - unq - 1; l = std::max(l, i); for (int j = l; j <= r; ++j) ret = std::min(ret, calc(i, j, res)); } return ret; } bool fl = 1; void dickdreamer() { ++cs; std::cin >> n >> p; vec[0].clear(), vec[1].clear(); for (int i = 1; i <= n; ++i) { int c, t, id, op; std::cin >> c >> id >> t >> op; vec[c - 1].emplace_back(t, id, op); if (i == 1) fir = t; } prework(), discrete(); if (cnt[0] != cnt[1]) return void(std::cout << "-1\n"); if (now[0] > now[1]) d = now[0] - now[1]; else d = now[1] - now[0] + 1, std::swap(now[0], now[1]), v[0].swap(v[1]), vec[0].swap(vec[1]); getseg(); // if (fl && cs == 6) assert(std::count(unq + 1, unq + 1 + m, 66660446969ll) // || std::count(unq + 1, unq + 1 + m, 904724933033ll)); sgt.build(m); auto res = getans(); if (res.first == -1) std::cout << "-1\n"; else std::cout << res.first << ' ' << res.second << '\n'; } int32_t main() { #ifdef ORZXKR freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0); int T = 1; std::cin >> T; fl &= (T == 10000); while (T--) dickdreamer(); // std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; }
failed
88_1_wrong.cpp
#line 1 "/home/maspy/compro/library/my_template.hpp" #if defined(LOCAL) #include <my_template_compiled.hpp> #else // https://codeforces.com/blog/entry/96344 #pragma GCC optimize("Ofast,unroll-loops") // いまの CF だとこれ入れると動かない? // #pragma GCC target("avx2,popcnt") #include <bits/stdc++.h> using namespace std; using ll = long long; using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; using i128 = __int128; using u128 = unsigned __int128; using f128 = __float128; template <class T> constexpr T infty = 0; template <> constexpr int infty<int> = 1'010'000'000; template <> constexpr ll infty<ll> = 2'020'000'000'000'000'000; template <> constexpr u32 infty<u32> = infty<int>; template <> constexpr u64 infty<u64> = infty<ll>; template <> constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000; template <> constexpr double infty<double> = infty<ll>; template <> constexpr long double infty<long double> = infty<ll>; using pi = pair<ll, ll>; using vi = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vc<T>>; template <class T> using vvvc = vector<vvc<T>>; template <class T> using vvvvc = vector<vvvc<T>>; template <class T> using vvvvvc = vector<vvvvc<T>>; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define vv(type, name, h, ...) \ vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector<vector<vector<type>>> name( \ h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector<vector<vector<vector<type>>>> name( \ a, vector<vector<vector<type>>>( \ b, vector<vector<type>>(c, vector<type>(__VA_ARGS__)))) // https://trap.jp/post/1224/ #define FOR1(a) for (ll _ = 0; _ < ll(a); ++_) #define FOR2(i, a) for (ll i = 0; i < ll(a); ++i) #define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i) #define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c)) #define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i) #define overload4(a, b, c, d, e, ...) e #define overload3(a, b, c, d, ...) d #define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__) #define FOR_subset(t, s) \ for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s))) #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define elif else if #define eb emplace_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define stoi stoll int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } int popcnt_sgn(int x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int popcnt_sgn(ll x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int popcnt_sgn(u64 x) { return (__builtin_parity(x) & 1 ? -1 : 1); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template <typename T> T floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); } template <typename T> T ceil(T x, T y) { return floor(x + y - 1, y); } template <typename T> T bmod(T x, T y) { return x - y * floor(x, y); } template <typename T> pair<T, T> divmod(T x, T y) { T q = floor(x, y); return {q, x - q * y}; } template <typename T, typename U> T SUM(const vector<U> &A) { T sm = 0; for (auto &&a : A) sm += a; return sm; } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define LB(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) \ sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit() template <typename T> T POP(deque<T> &que) { T a = que.front(); que.pop_front(); return a; } template <typename T> T POP(pq<T> &que) { T a = que.top(); que.pop(); return a; } template <typename T> T POP(pqg<T> &que) { T a = que.top(); que.pop(); return a; } template <typename T> T POP(vc<T> &que) { T a = que.back(); que.pop_back(); return a; } template <typename F> ll binary_search(F check, ll ok, ll ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (abs(ok - ng) > 1) { auto x = (ng + ok) / 2; (check(x) ? ok : ng) = x; } return ok; } template <typename F> double binary_search_real(F check, double ok, double ng, int iter = 100) { FOR(iter) { double x = (ok + ng) / 2; (check(x) ? ok : ng) = x; } return (ok + ng) / 2; } template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } // ? は -1 vc<int> s_to_vi(const string &S, char first_char) { vc<int> A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } template <typename T, typename U> vector<T> cumsum(vector<U> &A, int off = 1) { int N = A.size(); vector<T> B(N + 1); FOR(i, N) { B[i + 1] = B[i] + A[i]; } if (off == 0) B.erase(B.begin()); return B; } // stable sort template <typename T> vector<int> argsort(const vector<T> &A) { vector<int> ids(len(A)); iota(all(ids), 0); sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); }); return ids; } // A[I[0]], A[I[1]], ... template <typename T> vc<T> rearrange(const vc<T> &A, const vc<int> &I) { vc<T> B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } template <typename T, typename... Vectors> void concat(vc<T> &first, const Vectors &...others) { vc<T> &res = first; (res.insert(res.end(), others.begin(), others.end()), ...); } #endif #line 1 "/home/maspy/compro/library/other/io.hpp" #define FASTIO #include <unistd.h> // https://judge.yosupo.jp/submission/21623 namespace fastio { static constexpr uint32_t SZ = 1 << 17; char ibuf[SZ]; char obuf[SZ]; char out[100]; // pointer of ibuf, obuf uint32_t pil = 0, pir = 0, por = 0; struct Pre { char num[10000][4]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i][j] = n % 10 | '0'; n /= 10; } } } } constexpr pre; inline void load() { memcpy(ibuf, ibuf + pil, pir - pil); pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin); pil = 0; if (pir < SZ) ibuf[pir++] = '\n'; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } void rd(char &c) { do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); } void rd(string &x) { x.clear(); char c; do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); do { x += c; if (pil == pir) load(); c = ibuf[pil++]; } while (!isspace(c)); } template <typename T> void rd_real(T &x) { string s; rd(s); x = stod(s); } template <typename T> void rd_integer(T &x) { if (pil + 100 > pir) load(); char c; do c = ibuf[pil++]; while (c < '-'); bool minus = 0; if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; } if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (minus) x = -x; } } void rd(int &x) { rd_integer(x); } void rd(ll &x) { rd_integer(x); } void rd(i128 &x) { rd_integer(x); } void rd(u32 &x) { rd_integer(x); } void rd(u64 &x) { rd_integer(x); } void rd(u128 &x) { rd_integer(x); } void rd(double &x) { rd_real(x); } void rd(long double &x) { rd_real(x); } void rd(f128 &x) { rd_real(x); } template <class T, class U> void rd(pair<T, U> &p) { return rd(p.first), rd(p.second); } template <size_t N = 0, typename T> void rd_tuple(T &t) { if constexpr (N < std::tuple_size<T>::value) { auto &x = std::get<N>(t); rd(x); rd_tuple<N + 1>(t); } } template <class... T> void rd(tuple<T...> &tpl) { rd_tuple(tpl); } template <size_t N = 0, typename T> void rd(array<T, N> &x) { for (auto &d : x) rd(d); } template <class T> void rd(vc<T> &x) { for (auto &d : x) rd(d); } void read() {} template <class H, class... T> void read(H &h, T &...t) { rd(h), read(t...); } void wt(const char c) { if (por == SZ) flush(); obuf[por++] = c; } void wt(const string s) { for (char c : s) wt(c); } void wt(const char *s) { size_t len = strlen(s); for (size_t i = 0; i < len; i++) wt(s[i]); } template <typename T> void wt_integer(T x) { if (por > SZ - 100) flush(); if (x < 0) { obuf[por++] = '-', x = -x; } int outi; for (outi = 96; x >= 10000; outi -= 4) { memcpy(out + outi, pre.num[x % 10000], 4); x /= 10000; } if (x >= 1000) { memcpy(obuf + por, pre.num[x], 4); por += 4; } else if (x >= 100) { memcpy(obuf + por, pre.num[x] + 1, 3); por += 3; } else if (x >= 10) { int q = (x * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (x - q * 10) | '0'; por += 2; } else obuf[por++] = x | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template <typename T> void wt_real(T x) { ostringstream oss; oss << fixed << setprecision(15) << double(x); string s = oss.str(); wt(s); } void wt(int x) { wt_integer(x); } void wt(ll x) { wt_integer(x); } void wt(i128 x) { wt_integer(x); } void wt(u32 x) { wt_integer(x); } void wt(u64 x) { wt_integer(x); } void wt(u128 x) { wt_integer(x); } void wt(double x) { wt_real(x); } void wt(long double x) { wt_real(x); } void wt(f128 x) { wt_real(x); } template <class T, class U> void wt(const pair<T, U> val) { wt(val.first); wt(' '); wt(val.second); } template <size_t N = 0, typename T> void wt_tuple(const T t) { if constexpr (N < std::tuple_size<T>::value) { if constexpr (N > 0) { wt(' '); } const auto x = std::get<N>(t); wt(x); wt_tuple<N + 1>(t); } } template <class... T> void wt(tuple<T...> tpl) { wt_tuple(tpl); } template <class T, size_t S> void wt(const array<T, S> val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } template <class T> void wt(const vector<T> val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } void print() { wt('\n'); } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { wt(head); if (sizeof...(Tail)) wt(' '); print(forward<Tail>(tail)...); } // gcc expansion. called automaticall after main. void __attribute__((destructor)) _d() { flush(); } } // namespace fastio using fastio::flush; using fastio::print; using fastio::read; #if defined(LOCAL) #define SHOW(...) \ SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__) #define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME #define SHOW1(x) print(#x, "=", (x)), flush() #define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush() #define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush() #define SHOW4(x, y, z, w) \ print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush() #define SHOW5(x, y, z, w, v) \ print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), \ flush() #define SHOW6(x, y, z, w, v, u) \ print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), \ #u, "=", (u)), \ flush() #else #define SHOW(...) #endif #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define U32(...) \ u32 __VA_ARGS__; \ read(__VA_ARGS__) #define U64(...) \ u64 __VA_ARGS__; \ read(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ read(name) #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ read(name) void YES(bool t = 1) { print(t ? "YES" : "NO"); } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { print(t ? "Yes" : "No"); } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { print(t ? "yes" : "no"); } void no(bool t = 1) { yes(!t); } #line 3 "main.cpp" #line 1 "/home/maspy/compro/library/game/dyadic_rational.hpp" // a+b/2^M の形で持つ template <typename INTEGER> struct Dyadic_Rational { using X = Dyadic_Rational; INTEGER a, b; static constexpr int M = std::numeric_limits<INTEGER>::digits - 2; Dyadic_Rational(INTEGER a = 0) : a(a), b(0) {} // x + y / z Dyadic_Rational(INTEGER x, INTEGER y, INTEGER z) : a(x), b(y) { auto [q, r] = divmod(b, z); a += q; b = r; b *= (INTEGER(1) << M) / z; } // x/y Dyadic_Rational(INTEGER x, INTEGER y) : Dyadic_Rational(0, x, y) {} static X from_ab(INTEGER a, INTEGER b) { X x(a); x.b = b; return x; } // 比較 bool operator==(X const &rhs) const { return (a == rhs.a && b == rhs.b); } bool operator!=(X const &rhs) const { return !(*this == rhs); } bool operator<(X const &rhs) const { return (a < rhs.a) || (a == rhs.a && b < rhs.b); } bool operator<=(X const &rhs) const { return (a < rhs.a) || (a == rhs.a && b <= rhs.b); } bool operator>(X const &rhs) const { return (a > rhs.a) || (a == rhs.a && b > rhs.b); } bool operator>=(X const &rhs) const { return (a > rhs.a) || (a == rhs.a && b >= rhs.b); } // 加法 friend X operator+(const X &x, const X &y) { INTEGER a = x.a + y.a, b = x.b + y.b; while (b >= INTEGER(1) << M) { ++a; b -= INTEGER(1) << M; } return from_ab(a, b); } friend X operator-(const X &x, const X &y) { INTEGER a = x.a - y.a, b = x.b - y.b; while (b < 0) { --a; b += INTEGER(1) << M; } return from_ab(a, b); } friend X operator-(const X &x) { INTEGER a = -x.a, b = -x.b; while (b < 0) { --a; b += INTEGER(1) << M; } return from_ab(a, b); } X &operator+=(const X &x) { return (*this) = (*this) + x; } X &operator-=(const X &x) { return (*this) = (*this) - x; } static X simplest(X x, X y, bool include_x = false, bool include_y = false) { if (include_x && x != -infinity()) { // eps を引く x = x - from_ab(0, 1); } if (include_y && y != infinity()) { // eps を足す y = y + from_ab(0, 1); } assert(x < y); if (y.a < 0) return -simplest(-y, -x); { INTEGER l = x.a + 1; INTEGER r = (y.b == 0 ? y.a - 1 : y.a); if (l <= 0 && 0 <= r) return X(0); if (l <= r && 0 <= l) return X(l); if (l <= r && r <= 0) return X(r); } INTEGER l = x.b + 1; INTEGER r = (y.b == 0 ? (INTEGER(1) << M) - 1 : y.b - 1); if (l == r) return from_ab(x.a, l); int k = topbit(l ^ r); r &= ~((INTEGER(1) << k) - 1); return from_ab(x.a, r); } static constexpr X infinity() { return from_ab(INTEGER(1) << M, 0); } string to_string() { ll x = a, y = b, z = INTEGER(1) << M; while (y % 2 == 0 && z % 2 == 0) { y /= 2, z /= 2; } y += x * z; return std::to_string(y) + "/" + std::to_string(z); } }; #line 1 "/home/maspy/compro/library/other/mex.hpp" int mex(const vc<int> &A) { int n = len(A); vc<bool> aru(n + 1); for (auto &x : A) if (x < n) aru[x] = 1; int mex = 0; while (aru[mex]) ++mex; return mex; } #line 1 "/home/maspy/compro/library/enumerate/product.hpp" // [0, A0) x [0, A1) x ... template <typename F> void enumerate_product(vc<int> A, F query) { int N = len(A); auto dfs = [&](auto &dfs, vc<int> &p) -> void { int n = len(p); if (n == N) return query(p); FOR(x, A[n]) { p.eb(x); dfs(dfs, p); p.pop_back(); } }; vc<int> p; dfs(dfs, p); } #line 7 "main.cpp" struct Number_And_Star { using A = Dyadic_Rational<ll>; // a + *b A a; int b; using T = Number_And_Star; Number_And_Star(A a = 0, ll b = 0) : a(a), b(b) {} T &operator+=(const T &p) { a += p.a, b ^= p.b; return *this; } T &operator-=(const T &p) { a -= p.a, b ^= p.b; return *this; } T operator-() const { return T(-a, b); } // {計算できたか, 値} static pair<bool, T> from_options(vc<T> left_ops, vc<T> right_ops) { A xl = -A::infinity(), xr = A::infinity(); vc<int> Lb, Rb; for (auto &&t : left_ops) { if (chmax(xl, t.a)) Lb.clear(); if (xl == t.a) Lb.eb(t.b); } for (auto &&t : right_ops) { if (chmin(xr, t.a)) Rb.clear(); if (xr == t.a) Rb.eb(t.b); } int Lm = mex(Lb), Rm = mex(Rb); if (xl < xr) { A a = A::simplest(xl, xr, Lm == 0, Rm == 0); return {true, T(a, 0)}; } if (xl == xr) { if (Lm == Rm) return {true, T(xl, Lm)}; } return {false, T(0, 0)}; } string to_string() { string x = a.to_string(); x += " + *"; x += ::to_string(b); return x; } // L, R はそれぞれ自分手番のときに勝てるか? pair<bool, bool> outcome() { if (a > 0) return {1, 0}; if (a < 0) return {0, 1}; if (b == 0) return {0, 0}; return {1, 1}; } }; // 全部 dyadic rational number になるときだけ解ける // 失敗したときは、empty map が返る // ・states:興味のある state 全体 // ・get_options:pair<vc<STATE>, vc<STATE>>(STATE), left ops / right ops template <typename STATE, typename F> map<STATE, Number_And_Star> solve_partizan_game(const vector<STATE> &states, F get_options) { using X = Number_And_Star; map<STATE, X> MP; bool success = 1; auto dfs = [&](auto &dfs, const STATE &s) -> X { if (!success) return X(); if (MP.count(s)) return MP[s]; vc<X> left, right; auto [lop, rop] = get_options(s); for (auto &&t : lop) left.eb(dfs(dfs, t)); for (auto &&t : rop) right.eb(dfs(dfs, t)); auto [ok, t] = X::from_options(left, right); if (!success) return X{}; if (!ok) { // print("FAILED"); // print(s); // print("LEFT"); // for (auto& t: lop) { // X x = dfs(dfs, t); // print(t, x.to_string()); // } // print("RIGHT"); // for (auto& t: rop) { // X x = dfs(dfs, t); // print(t, x.to_string()); // } success = 0; return X(); } return MP[s] = t; }; for (auto &&s : states) dfs(dfs, s); if (!success) MP.clear(); return MP; } void solve() { vc<string> states; enumerate_product(vc<int>(9, 3), [&](vc<int> A) -> void { string S; for (auto &x : A) { if (x == 0) S += '.'; if (x == 1) S += 'L'; if (x == 2) S += 'R'; } states.eb(S); }); vc<tuple<int, int, int>> BINGO = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}}; auto valid = [&](string S) -> bool { for (auto &[a, b, c] : BINGO) { if (S[a] == S[b] && S[b] == S[c] && S[a] != '.') return 0; } return 1; }; auto get_options = [&](string S) -> pair<vc<string>, vc<string>> { vc<string> L, R; FOR(i, 9) { if (S[i] != '.') continue; S[i] = 'L'; if (valid(S)) L.eb(S); S[i] = 'R'; if (valid(S)) R.eb(S); S[i] = '.'; } return {L, R}; }; auto MP = solve_partizan_game<string>(states, get_options); using X = Number_And_Star; INT(T); FOR(T) { LL(N); X x{}; FOR(N) { STR(A, B, C); string S = A + B + C; for (auto &x : S) if (x == 'x') x = 'L'; for (auto &x : S) if (x == 'o') x = 'R'; assert(MP.count(S)); // SHOW(S, MP[S].to_string()); x += MP[S]; } print(x.outcome().fi ? "Alice" : "Bob"); } } signed main() { solve(); }
failed
50_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, n) for (int i = 0; i < (n); i += 1) #define len(a) ((int)(a).size()) mt19937 rnd(234); const ll inf = 1e18; int gn; int gm; vector<vector<int>> g; vector<int> p; vector<int> usd; bool dfs(int v) { usd[v] = true; for (auto to : g[v]) { if (p[to] == -1) { p[to] = v; return true; } if (usd[p[to]]) { continue; } if (dfs(p[to])) { p[to] = v; return true; } } return false; } int n, m; vector<vector<ll>> d; vector<vector<int>> e; vector<ll> res; void rec(vector<int> rows, vector<int> cols, int rem, ll val, int depth, vector<vector<int>> points_rows, vector<vector<int>> points_cols); void call_rec_rows(vector<int> rows, vector<int> cols, int rem, ll val, int depth, vector<vector<int>> points_rows, vector<vector<int>> points_cols, int i, int j) { val -= d[i][rows[i]]; rem = (rem + 4 - e[i][rows[i]]) % 4; rows[i] = j; points_rows[i].push_back(j); points_cols[j].push_back(i); val += d[i][rows[i]]; rem = (rem + e[i][rows[i]]) % 4; rec(rows, cols, rem, val, depth + 1, points_rows, points_cols); } void call_rec_cols(vector<int> rows, vector<int> cols, int rem, ll val, int depth, vector<vector<int>> points_rows, vector<vector<int>> points_cols, int i, int j) { val -= d[cols[j]][j]; rem = (rem + 4 - e[cols[j]][j]) % 4; cols[j] = i; points_rows[i].push_back(j); points_cols[j].push_back(i); val += d[cols[j]][j]; rem = (rem + e[cols[j]][j]) % 4; rec(rows, cols, rem, val, depth + 1, points_rows, points_cols); } void rec(vector<int> rows, vector<int> cols, int rem, ll val, int depth, vector<vector<int>> points_rows, vector<vector<int>> points_cols) { res[rem] = max(res[rem], val); if (depth == 3) { return; } rep(i, n) { vector<pair<ll, int>> rems_opts(4, {-1, -1}); for (auto j : points_rows[i]) { if (rows[i] == j or cols[j] == i) { continue; } rems_opts[e[i][j]] = max(rems_opts[e[i][j]], make_pair(d[i][j], j)); } rep(nrem, 4) { if (nrem == e[i][rows[i]]) { continue; } int j = rems_opts[nrem].second; if (j == -1) { continue; } call_rec_rows(rows, cols, rem, val, depth, points_rows, points_cols, i, j); } } rep(j, m) { vector<pair<ll, int>> rems_opts(4, {-1, -1}); for (auto i : points_cols[j]) { if (rows[i] == j or cols[j] == i) { continue; } rems_opts[e[i][j]] = max(rems_opts[e[i][j]], make_pair(d[i][j], i)); } rep(nrem, 4) { if (nrem == e[cols[j]][j]) { continue; } int i = rems_opts[nrem].second; if (i == -1) { continue; } call_rec_cols(rows, cols, rem, val, depth, points_rows, points_cols, i, j); } } } int32_t main() { if (1) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } cin >> n >> m; d.resize(n, vector<ll>(m)); e.resize(n, vector<int>(m)); rep(i, n) rep(j, m) { cin >> d[i][j]; } rep(i, n) rep(j, m) { cin >> e[i][j]; } gn = n * m; gm = n + m; g.assign(gn, {}); vector<pair<ll, int>> srtd; rep(i, n) rep(j, m) { srtd.push_back(make_pair(d[i][j], i * m + j)); g[i * m + j].push_back(i); g[i * m + j].push_back(n + j); } sort(rall(srtd)); p.assign(gm, -1); for (auto [_, i] : srtd) { usd.assign(gn, false); dfs(i); } res.assign(4, -1); vector<int> rows(n, -1), cols(m, -1); vector<vector<int>> cusd(n, vector<int>(m, false)); rep(i, n) { int j = p[i] % m; rows[i] = j; cusd[i][j] = true; } rep(j, m) { int i = p[n + j] / m; cols[j] = i; cusd[i][j] = true; } vector<vector<int>> points_rows(n), points_cols(m); rep(i, n) { vector<vector<pair<ll, int>>> aboba(4); rep(j, m) { if (rows[i] == j or cols[j] == i) { continue; } aboba[e[i][j]].push_back({d[i][j], j}); } rep(rem, 4) { sort(rall(aboba[rem])); rep(k, min(3, len(aboba[rem]))) { points_rows[i].push_back(aboba[rem][k].second); } } } rep(j, m) { vector<vector<pair<ll, int>>> aboba(4); rep(i, n) { if (rows[i] == j or cols[j] == i) { continue; } aboba[e[i][j]].push_back({d[i][j], i}); } rep(rem, 4) { sort(rall(aboba[rem])); rep(k, min(3, len(aboba[rem]))) { points_cols[j].push_back(aboba[rem][k].second); } } } int rem = 0; ll val = 0; rep(i, n) rep(j, m) { if (cusd[i][j]) { rem = (rem + e[i][j]) % 4; val += d[i][j]; } } rec(rows, cols, rem, val, 0, points_rows, points_cols); rep(i, 4) { cout << res[i] << "\n"; } return 0; }
failed
46_3_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; using F = __float128; std::istream &operator>>(std::istream &is, F &a) { int x; is >> x; a = x; return is; } template <class T> struct Point { T x; T y; Point(const T &x_ = 0, const T &y_ = 0) : x(x_), y(y_) {} template <class U> operator Point<U>() { return Point<U>(U(x), U(y)); } Point &operator+=(const Point &p) & { x += p.x; y += p.y; return *this; } Point &operator-=(const Point &p) & { x -= p.x; y -= p.y; return *this; } Point &operator*=(const T &v) & { x *= v; y *= v; return *this; } Point &operator/=(const T &v) & { x /= v; y /= v; return *this; } Point operator-() const { return Point(-x, -y); } friend Point operator+(Point a, const Point &b) { return a += b; } friend Point operator-(Point a, const Point &b) { return a -= b; } friend Point operator*(Point a, const T &b) { return a *= b; } friend Point operator/(Point a, const T &b) { return a /= b; } friend Point operator*(const T &a, Point b) { return b *= a; } friend bool operator==(const Point &a, const Point &b) { return a.x == b.x && a.y == b.y; } friend std::istream &operator>>(std::istream &is, Point &p) { return is >> p.x >> p.y; } friend std::ostream &operator<<(std::ostream &os, const Point &p) { return os << "(" << p.x << ", " << p.y << ")"; } }; template <class T> struct Line { Point<T> a; Point<T> b; Line(const Point<T> &a_ = Point<T>(), const Point<T> &b_ = Point<T>()) : a(a_), b(b_) {} }; template <class T> T dot(const Point<T> &a, const Point<T> &b) { return a.x * b.x + a.y * b.y; } template <class T> T cross(const Point<T> &a, const Point<T> &b) { return a.x * b.y - a.y * b.x; } template <class T> T square(const Point<T> &p) { return dot(p, p); } template <class T> double length(const Point<T> &p) { return std::sqrt(square(p)); } template <class T> double length(const Line<T> &l) { return length(l.a - l.b); } template <class T> Point<T> normalize(const Point<T> &p) { return p / length(p); } template <class T> bool parallel(const Line<T> &l1, const Line<T> &l2) { return cross(l1.b - l1.a, l2.b - l2.a) == 0; } template <class T> double distance(const Point<T> &a, const Point<T> &b) { return length(a - b); } template <class T> double distancePL(const Point<T> &p, const Line<T> &l) { return std::abs(cross(l.a - l.b, l.a - p)) / length(l); } template <class T> double distancePS(const Point<T> &p, const Line<T> &l) { if (dot(p - l.a, l.b - l.a) < 0) { return distance(p, l.a); } if (dot(p - l.b, l.a - l.b) < 0) { return distance(p, l.b); } return distancePL(p, l); } template <class T> Point<T> rotate(const Point<T> &a) { return Point(-a.y, a.x); } template <class T> int sgn(const Point<T> &a) { return a.y > 0 || (a.y == 0 && a.x > 0) ? 1 : -1; } template <class T> bool pointOnLineLeft(const Point<T> &p, const Line<T> &l) { return cross(l.b - l.a, p - l.a) > 0; } template <class T> Point<T> lineIntersection(const Line<T> &l1, const Line<T> &l2) { return l1.a + (l1.b - l1.a) * (cross(l2.b - l2.a, l1.a - l2.a) / cross(l2.b - l2.a, l1.a - l1.b)); } template <class T> bool pointOnSegment(const Point<T> &p, const Line<T> &l) { return cross(p - l.a, l.b - l.a) == 0 && std::min(l.a.x, l.b.x) <= p.x && p.x <= std::max(l.a.x, l.b.x) && std::min(l.a.y, l.b.y) <= p.y && p.y <= std::max(l.a.y, l.b.y); } template <class T> bool pointInPolygon(const Point<T> &a, const std::vector<Point<T>> &p) { int n = p.size(); for (int i = 0; i < n; i++) { if (pointOnSegment(a, Line(p[i], p[(i + 1) % n]))) { return true; } } int t = 0; for (int i = 0; i < n; i++) { auto u = p[i]; auto v = p[(i + 1) % n]; if (u.x < a.x && v.x >= a.x && pointOnLineLeft(a, Line(v, u))) { t ^= 1; } if (u.x >= a.x && v.x < a.x && pointOnLineLeft(a, Line(u, v))) { t ^= 1; } } return t == 1; } // 0 : not intersect // 1 : strictly intersect // 2 : overlap // 3 : intersect at endpoint template <class T> std::tuple<int, Point<T>, Point<T>> segmentIntersection(const Line<T> &l1, const Line<T> &l2) { if (std::max(l1.a.x, l1.b.x) < std::min(l2.a.x, l2.b.x)) { return {0, Point<T>(), Point<T>()}; } if (std::min(l1.a.x, l1.b.x) > std::max(l2.a.x, l2.b.x)) { return {0, Point<T>(), Point<T>()}; } if (std::max(l1.a.y, l1.b.y) < std::min(l2.a.y, l2.b.y)) { return {0, Point<T>(), Point<T>()}; } if (std::min(l1.a.y, l1.b.y) > std::max(l2.a.y, l2.b.y)) { return {0, Point<T>(), Point<T>()}; } if (cross(l1.b - l1.a, l2.b - l2.a) == 0) { if (cross(l1.b - l1.a, l2.a - l1.a) != 0) { return {0, Point<T>(), Point<T>()}; } else { auto maxx1 = std::max(l1.a.x, l1.b.x); auto minx1 = std::min(l1.a.x, l1.b.x); auto maxy1 = std::max(l1.a.y, l1.b.y); auto miny1 = std::min(l1.a.y, l1.b.y); auto maxx2 = std::max(l2.a.x, l2.b.x); auto minx2 = std::min(l2.a.x, l2.b.x); auto maxy2 = std::max(l2.a.y, l2.b.y); auto miny2 = std::min(l2.a.y, l2.b.y); Point<T> p1(std::max(minx1, minx2), std::max(miny1, miny2)); Point<T> p2(std::min(maxx1, maxx2), std::min(maxy1, maxy2)); if (!pointOnSegment(p1, l1)) { std::swap(p1.y, p2.y); } if (p1 == p2) { return {3, p1, p2}; } else { return {2, p1, p2}; } } } auto cp1 = cross(l2.a - l1.a, l2.b - l1.a); auto cp2 = cross(l2.a - l1.b, l2.b - l1.b); auto cp3 = cross(l1.a - l2.a, l1.b - l2.a); auto cp4 = cross(l1.a - l2.b, l1.b - l2.b); if ((cp1 > 0 && cp2 > 0) || (cp1 < 0 && cp2 < 0) || (cp3 > 0 && cp4 > 0) || (cp3 < 0 && cp4 < 0)) { return {0, Point<T>(), Point<T>()}; } Point p = lineIntersection(l1, l2); if (cp1 != 0 && cp2 != 0 && cp3 != 0 && cp4 != 0) { return {1, p, p}; } else { return {3, p, p}; } } template <class T> double distanceSS(const Line<T> &l1, const Line<T> &l2) { if (std::get<0>(segmentIntersection(l1, l2)) != 0) { return 0.0; } return std::min({distancePS(l1.a, l2), distancePS(l1.b, l2), distancePS(l2.a, l1), distancePS(l2.b, l1)}); } template <class T> bool segmentInPolygon(const Line<T> &l, const std::vector<Point<T>> &p) { int n = p.size(); if (!pointInPolygon(l.a, p)) { return false; } if (!pointInPolygon(l.b, p)) { return false; } for (int i = 0; i < n; i++) { auto u = p[i]; auto v = p[(i + 1) % n]; auto w = p[(i + 2) % n]; auto [t, p1, p2] = segmentIntersection(l, Line(u, v)); if (t == 1) { return false; } if (t == 0) { continue; } if (t == 2) { if (pointOnSegment(v, l) && v != l.a && v != l.b) { if (cross(v - u, w - v) > 0) { return false; } } } else { if (p1 != u && p1 != v) { if (pointOnLineLeft(l.a, Line(v, u)) || pointOnLineLeft(l.b, Line(v, u))) { return false; } } else if (p1 == v) { if (l.a == v) { if (pointOnLineLeft(u, l)) { if (pointOnLineLeft(w, l) && pointOnLineLeft(w, Line(u, v))) { return false; } } else { if (pointOnLineLeft(w, l) || pointOnLineLeft(w, Line(u, v))) { return false; } } } else if (l.b == v) { if (pointOnLineLeft(u, Line(l.b, l.a))) { if (pointOnLineLeft(w, Line(l.b, l.a)) && pointOnLineLeft(w, Line(u, v))) { return false; } } else { if (pointOnLineLeft(w, Line(l.b, l.a)) || pointOnLineLeft(w, Line(u, v))) { return false; } } } else { if (pointOnLineLeft(u, l)) { if (pointOnLineLeft(w, Line(l.b, l.a)) || pointOnLineLeft(w, Line(u, v))) { return false; } } else { if (pointOnLineLeft(w, l) || pointOnLineLeft(w, Line(u, v))) { return false; } } } } } } return true; } template <class T> std::vector<Point<T>> hp(std::vector<Line<T>> lines) { std::sort(lines.begin(), lines.end(), [&](auto l1, auto l2) { auto d1 = l1.b - l1.a; auto d2 = l2.b - l2.a; if (sgn(d1) != sgn(d2)) { return sgn(d1) == 1; } return cross(d1, d2) > 0; }); std::deque<Line<T>> ls; std::deque<Point<T>> ps; for (auto l : lines) { if (ls.empty()) { ls.push_back(l); continue; } while (!ps.empty() && !pointOnLineLeft(ps.back(), l)) { ps.pop_back(); ls.pop_back(); } while (!ps.empty() && !pointOnLineLeft(ps[0], l)) { ps.pop_front(); ls.pop_front(); } if (cross(l.b - l.a, ls.back().b - ls.back().a) == 0) { if (dot(l.b - l.a, ls.back().b - ls.back().a) > 0) { if (!pointOnLineLeft(ls.back().a, l)) { assert(ls.size() == 1); ls[0] = l; } continue; } return {}; } ps.push_back(lineIntersection(ls.back(), l)); ls.push_back(l); } while (!ps.empty() && !pointOnLineLeft(ps.back(), ls[0])) { ps.pop_back(); ls.pop_back(); } if (ls.size() <= 2) { return {}; } ps.push_back(lineIntersection(ls[0], ls.back())); return std::vector(ps.begin(), ps.end()); } using Pt = Point<F>; using C = std::complex<F>; constexpr F eps = 1E-10; void solve() { Pt o; F zo; Pt v; std::cin >> o >> zo >> v; int n1; F z1; std::cin >> n1 >> z1; std::vector<Pt> a(n1); for (int i = 0; i < n1; i++) { std::cin >> a[i]; a[i] -= o; a[i] *= zo / (zo - z1); } int n2; F z2; std::cin >> n2 >> z2; std::vector<Pt> b(n2); for (int i = 0; i < n2; i++) { std::cin >> b[i]; b[i] -= o; b[i] *= zo / (zo - z2); } v *= zo / (zo - z2) - zo / (zo - z1); F mul = 1; { mul = dot(v, v); C rot = C(0, 1) / C(v.x, v.y); for (int i = 0; i < n1; i++) { auto c = C(a[i].x, a[i].y) * rot; a[i] = Pt(c.real(), c.imag()); } for (int i = 0; i < n2; i++) { auto c = C(b[i].x, b[i].y) * rot; b[i] = Pt(c.real(), c.imag()); } } mul /= 2; // for (int i = 0; i < n1; i++) { // std::cerr << a[i] << "\n"; // } // for (int i = 0; i < n2; i++) { // std::cerr << b[i] << "\n"; // } // std::cerr << "mul : " << mul << "\n"; std::vector<F> xs; for (int i = 0; i < n1; i++) { xs.push_back(a[i].x); } for (int i = 0; i < n2; i++) { xs.push_back(b[i].x); } std::sort(xs.begin(), xs.end()); { auto vv = std::move(xs); for (auto v : vv) { if (xs.empty() || v - eps > xs.back()) { xs.push_back(v); } } } int m = xs.size(); std::vector<std::vector<std::array<F, 3>>> la(m), lb(m); for (int i = 0; i < n1; i++) { auto [x1, y1] = a[i]; auto [x2, y2] = a[(i + 1) % n1]; int u = std::lower_bound(xs.begin(), xs.end(), x1 - eps) - xs.begin(); int v = std::lower_bound(xs.begin(), xs.end(), x2 - eps) - xs.begin(); F coef = -1; if (u > v) { std::swap(u, v); coef *= -1; } for (int j = u; j < v; j++) { F yl = y1 + (xs[j] - x1) / (x2 - x1) * (y2 - y1); F yr = y1 + (xs[j + 1] - x1) / (x2 - x1) * (y2 - y1); la[j].push_back({yl, yr, coef}); } } for (int i = 0; i < n2; i++) { auto [x1, y1] = b[i]; auto [x2, y2] = b[(i + 1) % n2]; int u = std::lower_bound(xs.begin(), xs.end(), x1 - eps) - xs.begin(); int v = std::lower_bound(xs.begin(), xs.end(), x2 - eps) - xs.begin(); F coef = -1; if (u > v) { std::swap(u, v); coef *= -1; } for (int j = u; j < v; j++) { F yl = y1 + (xs[j] - x1) / (x2 - x1) * (y2 - y1); F yr = y1 + (xs[j + 1] - x1) / (x2 - x1) * (y2 - y1); lb[j].push_back({yl, yr, coef}); } } std::vector<std::array<F, 4>> events; events.push_back({0, 0, 0, 0}); events.push_back({1000, 0, 0, 0}); std::vector<std::array<F, 5>> extra; for (int i = 0; i < m - 1; i++) { F x = xs[i + 1] - xs[i]; // std::cerr << "xl : " << xs[i] << ", xr : " << xs[i + 1] << "\n"; for (auto [yla, yra, ca] : la[i]) { for (auto [ylb, yrb, cb] : lb[i]) { // std::cerr << yla << " " << yra << " " << ylb << " " << yrb << "\n"; if (ylb - yla > yrb - yra) { std::swap(yla, yra); std::swap(ylb, yrb); } F tl = ylb - yla; F tr = yrb - yra; F lst[4]{}; F coef = ca * cb; auto add = [&](F r, F c0, F c1, F c2) { r = std::min(r, F(1000)); c0 *= coef; c1 *= coef; c2 *= coef; if (lst[0] >= r) { return; } if (c2) { extra.push_back({lst[0], r, c0 - lst[1], c1 - lst[2], c2 - lst[3]}); events.push_back({lst[0], 0, 0, 0}); lst[0] = r; return; } events.push_back({lst[0], c0 - lst[1], c1 - lst[2], c2 - lst[3]}); lst[0] = r; lst[1] = c0; lst[2] = c1; lst[3] = c2; }; add(tl, (yla + yra) * x, x * 2, 0); // std::cerr << "tl : " << tl << ", tr : " << tr << "\n"; if (tl + eps < tr) { F dt = tr - tl; F c = x / dt / dt; F c0 = c * (ylb * -tl * (2 * tr - tl) + yrb * tl * tl + yla * tr * tr + yra * tr * (tr - 2 * tl)); F c1 = c * (ylb * 2 * tr - yrb * 2 * tl - yla * 2 * tr + yra * 2 * tl + tr * dt * 2); F c2 = c * (-ylb + yrb + yla - yra - dt * 2); // std::cerr << c0 << " " << c1 << " " << c2 << "\n"; // std::cerr << c0 + c1 * tl + c2 * tl * tl << " " << (yla + yra) / 2 // * x + x * tl << "\n"; std::cerr << c0 + c1 * tr + c2 * tr * tr << " // " << (ylb + yrb) / 2 * x << "\n"; add(tr, c0, c1, c2); } add(F(1000), (ylb + yrb) * x, 0, 0); } } } std::sort(events.begin(), events.end()); for (int i = 1; i < events.size(); i++) { events[i][1] += events[i - 1][1]; events[i][2] += events[i - 1][2]; events[i][3] += events[i - 1][3]; } for (auto [l, r, c0, c1, c2] : extra) { int u = std::lower_bound(events.begin(), events.end(), std::array<F, 4>{l - eps}) - events.begin(); int v = std::lower_bound(events.begin(), events.end(), std::array<F, 4>{r - eps}) - events.begin(); for (int i = u; i < v; i++) { events[i][1] += c0; events[i][2] += c1; events[i][3] += c2; } } std::vector<F> sum(events.size()); for (int i = 0; i < events.size() - 1; i++) { auto [xl, c0, c1, c2] = events[i]; auto xr = events[i + 1][0]; sum[i + 1] = sum[i] + (xr - xl) * (c0 + c1 * (xl + xr) / 2 + c2 * (xr * xr + xl * xr + xl * xl) / 3); } // for (int i = 0; i < events.size(); i++) { // auto [a, b, c, d] = events[i]; // std::cerr << a << " " << b << " " << c << " " << d << "\n"; // } int q; std::cin >> q; for (int i = 0; i < q; i++) { int t1, t2; std::cin >> t1 >> t2; if (events.empty()) { std::cout << 0 << "\n"; continue; } int u = std::lower_bound(events.begin(), events.end(), std::array<F, 4>{t1 + eps}) - events.begin() - 1; int v = std::lower_bound(events.begin(), events.end(), std::array<F, 4>{t2 + eps}) - events.begin() - 1; if (n1 > 10) { std::cout << t1 << " " << t2 << "\n"; } F ans = 0; if (t1 == t2) { auto [t, c0, c1, c2] = events[u]; ans = ((c2 * t1) + c1) * t1 + c0; } else { if (n1 > 10) { std::cout << u << " " << v << " " << double(sum[u]) << " " << double(sum[v]) << "\n"; } ans = sum[v] - sum[u]; { auto [xl, c0, c1, c2] = events[u]; auto xr = t1; ans -= (xr - xl) * (c0 + c1 * (xl + xr) / 2 + c2 * (xr * xr + xl * xr + xl * xl) / 3); } { auto [xl, c0, c1, c2] = events[v]; auto xr = t2; ans += (xr - xl) * (c0 + c1 * (xl + xr) / 2 + c2 * (xr * xr + xl * xr + xl * xl) / 3); } ans /= t2 - t1; } ans *= mul; std::cout << double(ans) << "\n"; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(10); int T; std::cin >> T; while (T--) { solve(); } return 0; }
failed
12_3_wrong.cpp
#include <iostream> #include <set> #include <unordered_set> #include <vector> using namespace std; void solve() { int n, m, k; cin >> n >> m >> k; unordered_set<int> friends; for (int i = 0; i < n; i++) { int f; cin >> f; friends.insert(f); } // Store all comments vector<pair<int, int>> comments(m); for (int i = 0; i < m; i++) { cin >> comments[i].first >> comments[i].second; } // Function to count visible comments with current friend set auto countVisible = [&](const unordered_set<int> &current_friends) { int count = 0; for (const auto &comment : comments) { int author = comment.first; int post_owner = comment.second; // Self comment case if (author == post_owner) { if (current_friends.count(author)) { count++; } continue; } // Regular comment case if (current_friends.count(author) && current_friends.count(post_owner)) { count++; } } return count; }; // Count current visible comments int current_visible = countVisible(friends); // Try adding one friend int max_with_one = current_visible; for (int i = 1; i <= k; i++) { if (friends.count(i)) continue; friends.insert(i); max_with_one = max(max_with_one, countVisible(friends)); friends.erase(i); } // Try adding two friends int max_with_two = max_with_one; for (int i = 1; i <= k; i++) { if (friends.count(i)) continue; friends.insert(i); for (int j = i + 1; j <= k; j++) { if (friends.count(j)) continue; friends.insert(j); max_with_two = max(max_with_two, countVisible(friends)); friends.erase(j); } friends.erase(i); } cout << max_with_two << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { solve(); } return 0; }
failed
55_3_wrong.cpp
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define pdd pair<double, double> #define F first #define S second #define all(x) x.begin(), x.end() using namespace std; pdd operator+(pdd a, pdd b) { return pdd(a.F + b.F, a.S + b.S); } pdd operator-(pdd a) { return pdd(-a.F, -a.S); } pdd operator-(pdd a, pdd b) { return a + (-b); } pdd operator*(pdd a, double b) { return pdd(a.F * b, a.S * b); } pdd operator/(pdd a, double b) { return pdd(a.F / b, a.S / b); } double dot(pdd a, pdd b) { return a.F * b.F + a.S * b.S; } double abs2(pdd a) { return dot(a, a); } pdd orth(pdd p) { return pdd(-p.S, p.F); } pdd interpo(pdd p, pdd q, double t) { return p * t + q * (1 - t); } double h1, h2; pdd p1, q1, p2, q2; signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> p1.F >> p1.S >> q1.F >> q1.S >> h1; cin >> p2.F >> p2.S >> q2.F >> q2.S >> h2; pdd v1 = q1 - p1, v2 = q2 - p2; pdd b1 = p1 + v1 / 2.0 + orth(v1) / 2.0; pdd b2 = p2 + v2 / 2.0 + orth(v2) / 2.0; double ans = 1e18; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { const int S = 20; int T = 100; double l1 = 0, r1 = 1, l2 = 0, r2 = 1; while (T--) { double d1 = (r1 - l1) / S; double d2 = (r2 - l2) / S; pii best(0, 0); double cur = 1e18; for (int t1 = 0; t1 <= S; t1++) for (int t2 = 0; t2 <= S; t2++) { pdd x = interpo(p1, q1, l1 + t1 * d1); pdd y = interpo(p2, q2, l2 + t2 * d2); double dis = sqrt(abs2(b1 - x) + h1 * h1) + sqrt(abs2(x - y)) + sqrt(abs2(b2 - y) + h2 * h2); if (dis < cur) cur = dis, best = pii(t1, t2); } auto [t1, t2] = best; ans = min(ans, cur); l1 = max(0.0, l1 + (t1 - 1) * d1); l2 = max(0.0, l2 + (t2 - 1) * d2); r1 = min(1.0, l1 + (t1 + 1) * d1); r2 = min(1.0, l2 + (t2 + 1) * d2); } p2 = p2 + v2; v2 = orth(v2); q2 = q2 + v2; } p1 = p1 + v1; v1 = orth(v1); q1 = q1 + v1; } cout << fixed << setprecision(10) << ans << '\n'; }
failed
113_3_wrong.cpp
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> namespace uvu { #define LOCAL ____________DONT_DEFINE_ME____________ // #define ll long long // #define inf 0x3f3f3f3f #define int long long #define inf 0x3f3f3f3f3f3f3f3fll #define infll 0x3f3f3f3f3f3f3f3fll #define debug(...) fprintf(stderr, __VA_ARGS__) #define gline debug("now is #%d\n", __LINE__) #define pii std::pair<int, int> #define mkp std::make_pair #define fi first #define se second char _ST_; const int BUFSIZE = (1 << 20); char ibuf[BUFSIZE], *iS = ibuf, *iT = ibuf; char obuf[BUFSIZE], *oS = obuf, *oT = obuf + BUFSIZE; char getc() { #ifdef LOCAL return getchar(); #else if (iS == iT) iT = (iS = ibuf) + fread(ibuf, 1, BUFSIZE, stdin); return iS == iT ? EOF : *iS++; #endif #define getchar ERR } void Flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } struct Flusher { ~Flusher() { Flush(); } } iamflusher; void putc(char c) { #ifdef LOCAL putchar(c); #else *oS++ = c; if (oS == oT) Flush(); #endif #define putchar ERR } template <typename T = int> T read() { T x = 0, f = 1; char c = getc(); for (; !isdigit(c); c = getc()) if (c == '-') f = -1; for (; isdigit(c); c = getc()) x = (x << 3) + (x << 1) + (c ^ 48); return x * f; } template <typename T> void print(T x, char c) { static int sta[BUFSIZE], top; top = 0; if (x < 0) putc('-'), x = -x; if (!x) sta[top = 1] = 0; for (; x; x /= 10) sta[++top] = x % 10; for (; top;) putc(sta[top--] ^ 48); if (c) putc(c); } int readstr(char *s, int base) { int idx = base - 1; char c = getc(); for (; !(isdigit(c) || isalpha(c) || c == '(' || c == '?'); c = getc()) ; for (; isdigit(c) || isalpha(c) || c == '(' || c == '?'; c = getc()) s[++idx] = c; return idx - base + 1; } void printf(const char *s) { for (; *s; s++) putc(*s); } template <typename T, typename... Args> void printf(const char *s, T x, Args... rest) { for (; *s; s++) { if (*s != '%') { putc(*s); continue; } s++; if (*s == 'd') print(x, 0); else if (*s == 'c') putc(x); printf(s + 1, rest...); return; } } template <typename T> void ckmax(T &x, T y) { x = x > y ? x : y; } template <typename T> void ckmin(T &x, T y) { x = x < y ? x : y; } #define mod 998244353 // #define mod 1000000007 int sm(int x) { return x >= mod ? x - mod : x; } void plus_(int &x, int y) { x = (x + y >= mod ? x + y - mod : x + y); } void mul_(int &x, int y) { x = 1ll * x * y % mod; } int ksm(int a, int b) { int res = 1; for (; b; b >>= 1, mul_(a, a)) if (b & 1) mul_(res, a); return res; } #define N 200010 int n, m, A, B; int op_[N], d[N], x_[N]; char s[2]; struct node { int mn, cnt; void operator+=(node B) { if (mn < B.mn) return; if (mn > B.mn) { mn = B.mn, cnt = B.cnt; return; } plus_(cnt, B.cnt); } node operator+(int x) { return (node){mn + x, cnt}; } }; node dp[2][N << 1]; int op; const int DT = 200000; std::deque<pii> ql, qr; int pos, base, val; int sta[N], top; int calc(int mid) { // printf("mid = %d\n", mid); // int lim = n + n; // for(int i = -lim; i <= lim; i++) dp[0][i + DT] = dp[1][i + DT] = // (node){inf, 0}; dp[op = 0][mid + DT] = (node){0, 1}; for(int i = 1; i <= n; // i++) // { // op ^= 1; // for(int j = -lim; j <= lim; j++) dp[op][j + DT] = (node){inf, 0}; // for(int j = -lim; j <= lim; j++) // { // if(op_[i] == 0) dp[op][j + DT] += dp[op ^ 1][j + DT] + abs(j) * // d[i]; else if(op_[i] == 1 && j != -lim) dp[op][j + DT] += dp[op ^ 1][j - 1 // + DT] + abs(j) + abs(j - 1) * (d[i] - 1); else if(op_[i] == 2) // { // dp[op][j + DT] += dp[op ^ 1][j + DT] + abs(j); // if(j != lim) dp[op][j + DT] += dp[op ^ 1][j + 1 + DT] + // abs(j) + abs(j + 1) * (d[i] - 1); // } // } // } // return dp[op][mid + DT].mn; pos = mid, base = 0, val = 1; ql.clear(), qr.clear(); auto out = [&]() -> void { printf("ql : "); for (pii x : ql) printf("[%d %d] ", x.fi, x.se); putc('\n'); printf("pos = %d, base = %d, val = %d\n", pos, base, val); printf("qr : "); for (pii x : qr) printf("[%d %d] ", x.fi, x.se); putc('\n'); }; for (int i = 1; i <= n; i++) { auto flush = [&](int v) -> void { // while(pos > 0 && !ql.empty() && ql.back().fi <= v) // { // base += ql.back().fi * abs(pos); // v -= ql.back().fi; // --pos; // qr.push_front(mkp(0, val)); // val = ql.back().se; // ql.pop_back(); // } // while(pos < 0 && !qr.empty() && qr.front().fi <= v) // { // base += qr.front().fi * abs(pos); // v -= qr.front().fi; // ++pos; // ql.push_back(mkp(0, val)); // val = qr.front().se; // qr.pop_front(); // } while (pos > 0 && v) { int t = v; if (!ql.empty()) ckmin(t, ql.back().fi); v -= t; base += pos * t; int len = ql.size(); if (0 <= pos - len) { if (!ql.empty()) ql.back().fi -= t; if (!qr.empty()) qr.front().fi += t; } else { ql.back().fi -= t; ql[len - pos - 1].fi += 2 * t; if (!qr.empty()) qr.front().fi += t; } if (!ql.empty() && ql.back().fi == 0) { pos--; qr.push_front(mkp(0, val)); val = ql.back().se; ql.pop_back(); } } while (pos < 0 && v) { int t = v; if (!qr.empty()) ckmin(t, qr.front().fi); v -= t; base -= pos * t; int len = qr.size(); if (pos + len <= 0) { if (!qr.empty()) qr.front().fi -= t; if (!ql.empty()) ql.back().fi += t; } else { qr.front().fi -= t; qr[-pos].fi += 2 * t; if (!ql.empty()) ql.back().fi += t; } if (!qr.empty() && qr.front().fi == 0) { pos++; ql.push_back(mkp(0, val)); val = qr.front().se; qr.pop_front(); } } if (pos == 0) { if (!ql.empty()) ql.back().fi += v; if (!qr.empty()) qr.front().fi += v; } // else if(0 < pos) // { // base += pos * v; // int len = ql.size(); // if(0 <= pos - len) // { // if(!ql.empty()) ql.back().fi -= v; // if(!qr.empty()) qr.front().fi += v; // } // else // { // ql.back().fi -= v; // ql[len - pos - 1].fi += 2 * v; // if(!qr.empty()) qr.front().fi += v; // } // } // else if(pos < 0) // { // base -= pos * v; // int len = qr.size(); // if(pos + len <= 0) // { // if(!qr.empty()) qr.front().fi -= v; // if(!ql.empty()) ql.back().fi += v; // } // else // { // qr.front().fi -= v; // qr[-pos].fi += 2 * v; // if(!ql.empty()) ql.back().fi += v; // } // } }; auto moveright = [&]() -> void { pos++; }; auto moveleft = [&]() -> void { top = 0; while (!ql.empty() && !ql.back().fi) sta[++top] = ql.back().se, ql.pop_back(); std::sort(sta + 1, sta + 1 + top); sta[++top] = val; pos -= top; while (!qr.empty() && !qr.front().fi) sta[++top] = qr.front().se, qr.pop_front(); sta[0] = 0; for (int i = 1; i <= top; i++) plus_(sta[i - 1], sta[i]); for (int i = top; i >= 1; i--) qr.push_front(mkp(0, sta[i])); val = sta[0]; }; flush(d[i] - 1); // printf("i = %d, flush(%d)\n", i, d[i] - 1), out(); if (op_[i] == 1) moveright(); //, printf("moveright\n"), out(); if (op_[i] == 2) moveleft(); //, printf("moveleft\n"), out(); flush(1); // printf("flush(1)\n"), out(); } // out(); int dt = 0; while (pos < mid) { if (qr.empty()) debug("???\n"); ++pos; dt += qr.front().fi; base += dt; ql.push_back(mkp(-qr.front().fi, val)); val = qr.front().se; qr.pop_front(); } while (mid < pos) { if (ql.empty()) debug("???\n"); --pos; dt += ql.back().fi; base += dt; qr.push_front(mkp(-ql.back().fi, val)); val = ql.back().se; ql.pop_back(); } while (!ql.empty() && !ql.back().fi) plus_(val, ql.back().se), ql.pop_back(); while (!qr.empty() && !qr.front().fi) plus_(val, qr.front().se), qr.pop_back(); // out(); // printf("return [%d %d]\n", base, val); // exit(1); return base; } void solve() { // memset(h, idx = -1, sizeof(h)); m = read(), A = read(), B = read(), n = A + B; for (int i = 1; i <= n; i++) { x_[i] = read(); readstr(s, 0); op_[i] = (s[0] == 'P' ? 1 : 2); } // printf("x[%d] = %d, m = %d\n", n, x_[n], m); x_[0] = x_[n] - m; for (int i = 1; i <= n; i++) d[i] = x_[i] - x_[i - 1]; pii res = mkp(calc(n), n); for (int l = -n, r = n, mid, lx, rx; l <= r;) { mid = (l + r) >> 1; lx = calc(mid); rx = calc(mid + 1); ckmin(res, mkp(lx, mid)); ckmin(res, mkp(rx, mid + 1)); if (lx <= rx) r = mid - 1; else l = mid + 1; } int ans = 0; print(res.fi, '\n'); calc(res.se); ans = val; if (res.fi == calc(res.se - 1)) plus_(ans, val); if (res.fi == calc(res.se + 1)) plus_(ans, val); print(ans, '\n'); } void init() {} char _ED_; void mian() { debug("%.3f MB\n", abs(&_ST_ - &_ED_) / 1024.0 / 1024); init(); int T = 1; for (; T; solve(), T--) ; } #ifdef int #undef int #endif } // namespace uvu int main() { uvu::mian(); return 0; }
failed
103_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long const int mod = 998244353; void solve() { int n, k; cin >> n >> k; int res = -1; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; sort(a.begin() + 1, a.end(), greater<int>()); priority_queue<int> q; for (int i = 1; i <= n; i++) q.push(a[i]); while (q.size() >= 3) { res = max(res, q.top()); int tmp1 = q.top(); q.pop(); int tmp2 = q.top(); q.pop(); int tmp3 = q.top(); q.pop(); tmp1 = ((tmp1 % mod) * (tmp2 % mod)) % mod; tmp1 = ((tmp1 % mod) * (tmp3 % mod)) % mod; q.push(tmp1); // if (q.top()==0) { // break; // } // int tmp1=q.top(); q.pop(); // if (q.top()==0) { // //q.push(tmp1); // break; // } else { // int tmp2=q.top(); // q.pop(); // if (q.top()==0) { // //q.push(tmp2),q.push(tmp1); // break; // } else { // int tmp3=q.top(); q.pop(); // tmp1=((tmp1%mod)*(tmp2%mod))%mod; // tmp1=((tmp1%mod)*(tmp3%mod))%mod; // q.push(tmp1); // //cout<<tmp1<<" has been pushed"<<"\n"; // } // } } res = max(res, q.top()); cout << res << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); return 0; }
failed
74_1_wrong.cpp
#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <time.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <iostream> using namespace __gnu_pbds; using namespace std; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // to erase in multiset-> less_equal<T> and // s.erase(s.find_by_order(s.order_of_key(x))) // lower_bound(x)=>(cannot use the stl lower_bound function) // ll idx = s.order_of_key(x) // if(idx == s.size()) -> no lower_bound // else lb = *s.find_by_order(idx) // as 0-indexing // idx-1 will give highest value which is strictly less than x // for upper_bound->do the same with (x+1) typedef long long ll; typedef long double ld; typedef pair<int, int> p32; typedef pair<ll, ll> p64; typedef tuple<ll, ll, ll> t64; typedef vector<t64> vt64; typedef vector<vt64> vvt64; typedef pair<double, double> pdd; typedef vector<ll> v64; typedef vector<int> v32; typedef vector<vector<int>> vv32; typedef vector<vector<ll>> vv64; typedef vector<vector<p64>> vvp64; typedef vector<p64> vp64; typedef vector<p32> vp32; typedef vector<vector<p32>> vvp32; typedef vector<bool> vb; ll mod = 1e9 + 7, MOD = 998244353; double eps = 1e-12; #define FOR(s, e, i) for (ll i = s; i <= e; i++) #define ROF(s, e, i) for (ll i = s; i >= e; i--) #define F0R(i, e) for (ll i = 0; i < (e); i++) #define trav(e, a) for (auto &e : a) #define coutAll(A) \ for (auto asdafas : A) \ cout << asdafas << " "; \ cout << "\n"; #define foutAll(A) \ for (auto asdafas : A) \ fout << asdafas << " "; \ cout << "\n"; #define cinAll(A) \ for (auto &asdafas : A) \ cin >> asdafas; #define finAll(A) \ for (auto &asdafas : A) \ fin >> asdafas; #define minpq priority_queue<ll, v64, greater<ll>> #define maxpq priority_queue<ll> #define ln "\n" #define dbg(x) cout << #x << " = " << x << ln #define mp make_pair #define mt make_tuple #define pb push_back #define fi first #define se second ll inf = LLONG_MAX; #define fast_cin() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define yes cout << "yes\n" #define no cout << "no\n" #define Yes cout << "Yes\n" #define No cout << "No\n" #define YES cout << "YES\n" #define NO cout << "NO\n" using namespace std; typedef long long ll; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef pair<ll, ll> pll; typedef pair<ll, ll> pii; #define MAXN 100000000 void solve(int it) { // int n = it; // // cin >> n; // cout << it << "\n"; // v64 A(n); // iota(all(A), 0); // v64 B = A; // do{ // set<ll>s; // FOR(0, n - 1, i){ // s.insert(A[i]^B[i]); // } // if(sz(s)==n && *s.begin()==0 && *s.rbegin()==n-1){ // // coutAll(A); // coutAll(B); // // FOR(0, n - 1, i) cout << (A[i]^B[i]) << " "; // // cout << '\n'; // cout << '\n'; // } // }while(next_permutation(all(B))); int n; cin >> n; if (n <= 1) { Yes; F0R(j, 2) { FOR(0, n - 1, i) cout << i << " "; cout << '\n'; } return; } if (n == 2) { No; return; } int bit = log2(n); if ((1 << bit) != n) { cout << "No\n"; return; } v32 A(n); iota(all(A), 0); v64 B; FOR(0, n - 1, i) { B.pb(i); ++i; } FOR(1, n - 3, i) { B.pb(i + 2); B.pb(i); i += 3; } Yes; coutAll(A); coutAll(B); // set<ll>s; // FOR(0, n - 1, i) { // // cout << (A[i]^B[i]) << " "; // s.insert(A[i]^B[i]); // } // // cout << "\n"; // if(sz(s) == n && *s.rbegin() == n-1){ // cout << n << " "; // Yes; // } // else{ // // No; // } } int main() { // freopen("text.txt", "w", stdout); fast_cin(); ll t = 1; cin >> t; for (int it = 1; it <= t; it++) { // cout << "Case " << it << ": "; solve(it); } return 0; }
failed
56_2_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; void solve() { std::string a, b, c; std::cin >> a >> b >> c; if (a.size() != b.size()) { std::cout << "NO\n"; return; } if (a.size() != c.size()) { std::cout << "YES\n"; return; } bool bad = 1; for (int i = 0; i < a.size(); i++) { bad &= (a[i] == c[i] || b[i] == c[i]); } std::cout << (bad ? "NO\n" : "YES\n"); } signed main() { std::cin.tie(nullptr)->sync_with_stdio(false); int t = 1; std::cin >> t; while (t--) solve(); return 0; }
failed
24_1_wrong.cpp
#include <bits/stdc++.h> #define int long long const int P = 998244353; // 如果wa有可能需要换mod数 class Polynomial : public std::vector<int> { static int multip(int a, int b) { return a * b % P; } // static int multip(int a, int b) // { // int res = a * b - (int)(1.L * a * b / P) * P; // res %= P; // if (res < 0) // { // res += P; // } // return res; // } static int add(int a, int b) { a += b; a -= (a >= P ? P : 0); return a; } static int sub(int a, int b) { a -= b; a += (a < 0 ? P : 0); return a; } static int qmi(int a, int b) { int res = 1; while (b) { if (b & 1) { res = res * a % P; } a = a * a % P; b >>= 1; } return res; } static std::vector<int> w; static void initW(int _log) { const int r = 1 << _log; if (w.size() >= r) { return; } w.assign(r, 0); w[r >> 1] = 1; int s = qmi(3, (P - 1) >> _log); // 3是原根 for (int i = r / 2 + 1; i < r; i++) { w[i] = w[i - 1] * s % P; } for (int i = r / 2 - 1; i > 0; i--) { w[i] = w[i * 2]; } } public: using std::vector<int>::vector; friend void dft(Polynomial &a) { const int n = a.size(); for (int k = n >> 1; k; k >>= 1) { for (int i = 0; i < n; i += k << 1) { for (int j = 0; j < k; j++) { int v = a[i + j + k]; a[i + j + k] = multip(sub(a[i + j], v), w[k + j]); a[i + j] = add(a[i + j], v); } } } } friend void idft(Polynomial &a) { const int n = a.size(); for (int k = 1; k < n; k <<= 1) { for (int i = 0; i < n; i += k << 1) { for (int j = 0; j < k; j++) { int u = a[i + j]; int v = multip(a[i + j + k], w[j + k]); a[i + j + k] = sub(u, v); a[i + j] = add(u, v); } } } int val = P - (P - 1) / n; for (int i = 0; i < n; i++) { a[i] = multip(a[i], val); } std::reverse(a.begin() + 1, a.end()); } friend Polynomial operator*(Polynomial a, Polynomial b) { if (a.size() == 0 or b.size() == 0) { return Polynomial(); } int n = a.size() + b.size() - 1; int _log = std::__lg(2 * n - 1); int s = 1 << _log; if (((P - 1) & (s - 1)) != 0 or std::min(a.size(), b.size()) < 128) { Polynomial res(n); for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { res[i + j] = add(res[i + j], multip(a[i], b[j])); } } return res; } initW(_log); a.resize(s), b.resize(s); dft(a), dft(b); for (int i = 0; i < s; i++) { a[i] = multip(a[i], b[i]); } idft(a); return a.resize(n), a; } friend Polynomial deriv(const Polynomial &a) // 求导 { int n = a.size(); if (n <= 1) { return Polynomial(); } Polynomial p(n - 1); for (int i = 1; i < n; i++) { p[i - 1] = multip(i, a[i]); } return p; } friend Polynomial integr(const Polynomial &a) { int n = a.size(); Polynomial p(n + 1); std::vector<int> _inv(n + 1); _inv[1] = 1; for (int i = 2; i <= n; i++) { _inv[i] = multip(_inv[P % i], (P - P / i)); } for (int i = 0; i < n; ++i) { p[i + 1] = multip(a[i], _inv[i + 1]); } return p; } friend Polynomial inv(const Polynomial &a) { int n = a.size(); if (n == 1) { return {qmi(a[0], P - 2)}; } Polynomial half(a.begin(), a.begin() + (n + 1) / 2); Polynomial b = inv(half), c = a * b; for (auto &x : c) { x = (x == 0 ? 0 : P - x); // ? } c[0] = add(c[0], 2); c = c * b; c.resize(n); return c; } friend Polynomial ln(const Polynomial &a) { int n = a.size(); Polynomial b(n, 0); for (int i = 1; i < n; i++) { b[i - 1] = multip(i, a[i]); } b = b * inv(a); b.resize(n); std::vector<int> _inv(n); _inv[1] = 1; for (int i = 2; i < n; i++) { _inv[i] = multip(P - P / i, _inv[P % i]); } for (int i = n - 1; i; i--) { b[i] = multip(b[i - 1], _inv[i]); } b[0] = 0; return b; } friend Polynomial exp(const Polynomial &a) { int n = a.size(); if (n == 1) { return {1}; } Polynomial half(a.begin(), a.begin() + (n + 1) / 2); Polynomial b = exp(half); b.resize(n); Polynomial c = ln(b); for (int i = 0; i < n; i++) { c[i] = sub(a[i], c[i]); } c[0] = add(c[0], 1); c = c * b; c.resize(n); return c; } friend Polynomial power(Polynomial &F, std::string s) { int k1 = 0; int k2 = 0; int k3 = 0; for (int i = 0; i < s.length(); ++i) { k1 = (k1 * 10 + s[i] - '0') % P; k2 = (k2 * 10 + s[i] - '0') % (P - 1); if (i < 7) { k3 = k3 * 10 + s[i] - '0'; } } int n = F.size(); if (!F[0] and k3 >= F.size()) { F.assign(n, 0); return F; } int pos = 0; // 处理移位 for (int i = 0; i < n; ++i) { if (F[i]) { pos = i; break; } } if (pos) { for (int i = pos; i < n; ++i) { F[i - pos] = F[i]; F[i] = 0; } } int val = F[0]; int cur = qmi(val, P - 2); for (int i = 0; i < F.size(); ++i) { F[i] = F[i] * cur % P; } F = ln(F); for (int i = 0; i < F.size(); ++i) { F[i] = F[i] * k1 % P; } F = exp(F); cur = qmi(val, k2); for (int i = 0; i < F.size(); ++i) { F[i] = F[i] * cur % P; } if (pos) { pos = std::min(1ll * pos * k1, n); for (int i = n - 1; i >= 0; --i) { if (i + pos < n) { F[i + pos] = F[i]; } F[i] = 0; } } return F; } friend Polynomial power(const Polynomial &F, int b, int m) // m是目标的区间长度 { Polynomial res = {1}; Polynomial G = F; while (b) { if (b & 1) { res = res * G; if (res.size() > m) { res.resize(m); } } G = G * G; if (G.size() > m) { G.resize(m); } b >>= 1; } return res; } }; std::vector<int> Polynomial::w; using Poly = Polynomial; int qmi(int a, int b) { int res = 1; while (b) { if (b & 1) { res = res * a % P; } a = a * a % P; b >>= 1; } return res; } using i64 = long long; template <class T> constexpr T power(T a, i64 b) { T res{1}; for (; b; b /= 2, a *= a) { if (b % 2) { res *= a; } } return res; } constexpr i64 mul(i64 a, i64 b, i64 p) { i64 res = a * b - i64(1.L * a * b / p) * p; res %= p; if (res < 0) { res += p; } return res; } template <i64 P> struct MInt { i64 x; constexpr MInt() : x{0} {} constexpr MInt(i64 x) : x{norm(x % getMod())} {} static i64 Mod; constexpr static i64 getMod() { if (P > 0) { return P; } else { return Mod; } } constexpr static void setMod(i64 Mod_) { Mod = Mod_; } constexpr i64 norm(i64 x) const { if (x < 0) { x += getMod(); } if (x >= getMod()) { x -= getMod(); } return x; } constexpr i64 val() const { return x; } constexpr MInt operator-() const { MInt res; res.x = norm(getMod() - x); return res; } constexpr MInt inv() const { return power(*this, getMod() - 2); } constexpr MInt &operator*=(MInt rhs) & { if (getMod() < (1ULL << 31)) { x = x * rhs.x % (getMod()); } else { x = mul(x, rhs.x, getMod()); } return *this; } constexpr MInt &operator+=(MInt rhs) & { x = norm(x + rhs.x); return *this; } constexpr MInt &operator-=(MInt rhs) & { x = norm(x - rhs.x); return *this; } constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); } friend constexpr MInt operator*(MInt lhs, MInt rhs) { MInt res = lhs; res *= rhs; return res; } friend constexpr MInt operator+(MInt lhs, MInt rhs) { MInt res = lhs; res += rhs; return res; } friend constexpr MInt operator-(MInt lhs, MInt rhs) { MInt res = lhs; res -= rhs; return res; } friend constexpr MInt operator/(MInt lhs, MInt rhs) { MInt res = lhs; res /= rhs; return res; } friend constexpr std::istream &operator>>(std::istream &is, MInt &a) { i64 v; is >> v; a = MInt(v); return is; } friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); } friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); } friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); } friend constexpr bool operator<(MInt lhs, MInt rhs) { return lhs.val() < rhs.val(); } }; template <> i64 MInt<0>::Mod = 998244353; using Z = MInt<P>; struct Comb { int n; std::vector<Z> _fac; std::vector<Z> _invfac; std::vector<Z> _inv; Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {} Comb(int n) : Comb() { init(n); } void init(int m) { m = std::min<int>(m, Z::getMod() - 1); if (m <= n) return; _fac.resize(m + 1); _invfac.resize(m + 1); _inv.resize(m + 1); for (int i = n + 1; i <= m; i++) { _fac[i] = _fac[i - 1] * i; } _invfac[m] = _fac[m].inv(); for (int i = m; i > n; i--) { _invfac[i - 1] = _invfac[i] * i; _inv[i] = _invfac[i] * _fac[i - 1]; } n = m; } Z fac(int m) { if (m > n) init(2 * m); return _fac[m]; } Z invfac(int m) { if (m > n) init(2 * m); return _invfac[m]; } Z inv(int m) { if (m > n) init(2 * m); return _inv[m]; } Z A(int a, int b) { if (a < b or b < 0) return 0; return fac(a) * invfac(a - b); } Z C(int n, int m) { if (n < m || m < 0) return 0; return fac(n) * invfac(m) * invfac(n - m); } } comb; void solve() { int n, m; std::cin >> n >> m; std::vector<int> a(n * m + 1, 0); for (int i = 1; i <= n * m; ++i) { std::cin >> a[i]; } std::sort(begin(a) + 1, end(a)); std::vector<int> fac(n * m + 1, 0); std::vector<int> ifac(n * m + 1, 0); fac[0] = 1; for (int i = 1; i <= n * m; ++i) { fac[i] = fac[i - 1] * i % P; } ifac[n] = qmi(fac[n], P - 2); for (int i = n; i >= 1; i--) { ifac[i - 1] = i * ifac[i] % P; } Poly f(n * m + 1, 0); for (int i = 1; i <= n * m; ++i) { f[i] = a[i] * fac[i - 1] % P; } Poly g(n * m + 1, 0); for (int i = 0; i <= n * m; ++i) { g[i] = ifac[n * m - i]; } f = f * g; std::vector<Z> c(n * m + 1, 0); for (int i = 0; i <= n * m; ++i) { c[i] = (Z)(fac[n * m - i] * i % P * f[n * m + i] % P); } Z ans = 0; for (int x = 0; x <= n; ++x) { for (int y = 0; y <= m; ++y) { int C = x * m + y * n - x * y; if ((x + y) & 1) { ans += comb.C(n, x) * comb.C(m, y) * c[C]; } else { ans -= comb.C(n, x) * comb.C(m, y) * c[C]; } } } std::cout << ans << '\n'; } signed main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int T; std::cin >> T; while (T--) { solve(); } }
failed
54_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long LL; void exgcd(int A, int B, LL &x, LL &y) { if (!B) return x = 1, y = 0, void(); exgcd(B, A % B, y, x); y = y - A / B * x; } unordered_map<long long, bool> used; int main() { cin.tie(0)->sync_with_stdio(0); int T; cin >> T; while (T--) { LL A, B, n; cin >> A >> B >> n; bool flg = 0; LL Lst = n, x = 0, inv = 0; exgcd(A, B, x, inv); inv = (inv + A) % A; int tmp = Lst % B, ct = 0; for (int t = 2000; t; t--) { if (n % A == 0) { // if(n==100)cout<<"~~~~~\n"; // n /= A; // cout<<n%B<<"****"<<n/B<<"\n"; if (n % B == tmp) { ct++; if (ct == 2) { if (Lst != n) flg = 0; else flg = 1; break; } } } else { LL t = (A - n % A) * inv % A; if (n < Lst) t = min(t, (Lst - n + B - 1) / B); n += B * t; } if (n == Lst) { flg = 1; break; } } if (flg) puts("Yes"); else puts("No"); } return 0; }
failed
18_3_wrong.cpp
#include <bits/stdc++.h> #include <cmath> #include <numbers> #include <ranges> #include <type_traits> #ifdef LOCAL #include "dd/debug.h" #else #define dbg(...) 42 #define dbg_proj(...) 420 #define dbg_rproj(...) 420420 void nline() {} void bar() {} void start_clock() {} void end_clock() {} #endif namespace rs = std::ranges; namespace rv = std::views; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; using db = long double; // [[maybe_unused]] const db PI = std::acos(static_cast<db>(-1.0)); [[maybe_unused]] constexpr db PI = std::numbers::pi_v<db>; constexpr db EPS = 1e-9; template <class T, class U> constexpr bool eq(const T &a, const U &b) { if constexpr (std::is_floating_point< typename std::common_type_t<T, U>>::value) { return std::abs(a - b) < EPS; } else { return a == b; } } struct Line { db a; int l; i64 sl; int num; db operator()(int r, i64 sr) const { return a + std::sqrt(static_cast<db>((r - l)) * (sr - sl)); } }; int main() { #ifndef LOCAL std::ios::sync_with_stdio(false); std::cin.tie(nullptr); #endif int n, m; std::cin >> n >> m; std::vector<i64> s(n); for (i64 &v : s) { std::cin >> v; } rs::sort(s); dbg(s); std::partial_sum(s.begin(), s.end(), s.begin()); auto inter = [&](const Line &l1, const Line &l2) { int l = std::max(l1.l, l2.l); int r = n - 1; while (l < r) { int mid = (l + r) / 2; if (l1(mid, s[mid]) <= l2(mid, s[mid])) { r = mid; } else { l = mid + 1; } } if (l1(l, s[l]) <= l2(l, s[l])) { return l; } return n; }; auto comp = [&](db c) -> std::pair<db, int> { std::deque<Line> dq{Line{c, -1, 0, 0}}; std::vector<db> dp(n); std::vector<int> num(n); for (int i = 0; i < n; ++i) { while (dq.size() > 1 && dq[0](i, s[i]) > dq[1](i, s[i])) { dq.pop_front(); } dp[i] = dq[0](i, s[i]); num[i] = dq[0].num + 1; Line l{dp[i] + c, i, s[i], num[i]}; if (i == n - 1) { break; } while (dq.size() > 1 && inter(l, dq.back()) <= inter(dq.back(), dq.end()[-2])) { dq.pop_back(); } dq.push_back(l); } return {dp.back(), num.back()}; }; db l = 0, r = 1e10; for (int i = 0; i < 300; ++i) { db mid = (l + r) / 2; if (comp(mid).second <= m) { r = mid; } else { l = mid; } } // r = 12.974071361124516; auto [ans, num] = comp(r); dbg(r, ans, num); dbg(comp(l).first - l * m); std::cout << std::fixed << std::setprecision(10) << ans - r * m << '\n'; }
failed
5_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using pii = array<int, 2>; using pll = array<ll, 2>; struct segtr { int n; vector<pll> data, delay; segtr(int n) : n(n), data(4 * n, {0, -1}), delay(4 * n, {-1, -1}) {} pll f(pll a, pll b) { if (a[0] == b[0]) { return {a[0], min(a[1], b[1])}; } else { return a[0] > b[0] ? a : b; } } void push(int id, int lo, int hi) { if (delay[id] == pll{-1, -1}) { return; } data[id] = delay[id]; if (lo + 1 < hi) { delay[id * 2 + 1] = delay[id]; delay[id * 2 + 2] = delay[id]; } delay[id] = {-1, -1}; } pll get(int l, int r) { return get(0, 0, n, l, r); } pll get(int id, int lo, int hi, int l, int r) { if (r <= lo || hi <= l) { return {-1, -1}; } push(id, lo, hi); if (l <= lo && hi <= r) { return data[id]; } int mid = (lo + hi) / 2; return f(get(id * 2 + 1, lo, mid, l, r), get(id * 2 + 2, mid, hi, l, r)); } void set(int l, int r, pll val) { set(0, 0, n, l, r, val); } void set(int id, int lo, int hi, int l, int r, pll val) { if (r <= lo || hi <= l) { return; } push(id, lo, hi); if (l <= lo && hi <= r) { delay[id] = val; push(id, lo, hi); return; } int mid = (lo + hi) / 2; set(id * 2 + 1, lo, mid, l, r, val); set(id * 2 + 2, mid, hi, l, r, val); data[id] = f(data[id * 2 + 1], data[id * 2 + 2]); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); const int N = 2e5 + 50; int n, ans = 0; cin >> n; segtr tr(N); vector<pii> where(n); for (int i = 0; i < n; ++i) { int l, r; ll h; cin >> l >> r >> h; l += 10, r += 10; int lo = l - 1, hi = r + 2; auto [prv, id] = tr.get(lo + 1, hi - 1); where[i] = {l, r}; vector<pii> todo; while (lo < hi) { auto [x, y] = tr.get(lo, hi); if (x < prv) break; todo.push_back(where[y]); lo = todo.back()[1] + 1; } for (int i = 1; i < (int)todo.size(); ++i) { auto [_, hi] = todo[i - 1]; auto [lo, __] = todo[i]; ans += lo + 1 < hi; } tr.set(lo + 1, hi - 1, {prv + h, i}); } cout << ans << endl; }
failed
90_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ll long long #define y1 y11 const int N = 500050; vector<int> E[N], roots; int x1[N], x2[N], y1[N], y2[N]; namespace Tree { const int M = 2 * N; int root, ls[M], rs[M], tsz; bool done[N]; vector<pair<int, int>> pts[M]; void Build(int &c, int ss, int se) { c = ++tsz; if (ss == se) return; int mid = ss + se >> 1; Build(ls[c], ss, mid); Build(rs[c], mid + 1, se); } void Set(int c, int ss, int se, int qi, pair<int, int> x) { pts[c].pb(x); if (ss == se) return; int mid = ss + se >> 1; if (qi <= mid) Set(ls[c], ss, mid, qi, x); else Set(rs[c], mid + 1, se, qi, x); } vector<int> take; void Get(int c, int ss, int se, int qs, int qe, int x) { if (qs > qe || qs > se || ss > qe) return; if (qs <= ss && qe >= se) { while (pts[c].size() && pts[c].back().first >= x) { if (!done[pts[c].back().second]) { take.pb(pts[c].back().second); done[pts[c].back().second] = true; } pts[c].pop_back(); } return; } int mid = ss + se >> 1; Get(ls[c], ss, mid, qs, qe, x); Get(rs[c], mid + 1, se, qs, qe, x); } void Build(int n) { vector<pair<int, int>> evs; vector<int> ys; for (int i = 1; i <= n; i++) { evs.pb({x2[i], i}); ys.pb(y1[i]); } sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); auto GetL = [&](int y) { return lower_bound(ys.begin(), ys.end(), y) - ys.begin() + 1; }; auto GetR = [&](int y) { return upper_bound(ys.begin(), ys.end(), y) - ys.begin(); }; Build(root, 1, ys.size()); sort(evs.begin(), evs.end()); for (auto ev : evs) { int i = ev.second; take.clear(); Get(root, 1, ys.size(), GetL(y1[i]), GetR(y2[i]), x1[i]); E[i] = take; Set(root, 1, ys.size(), GetL(y1[i]), {x2[i], i}); } take.clear(); Get(root, 1, ys.size(), 1, ys.size(), 0); roots = take; } } // namespace Tree const int L = 20; int dep[N], lid[N], rid[N], tid, par[N][L]; void DFS(int u, int p) { lid[u] = ++tid; par[u][0] = p; for (int i = 1; i < L; i++) par[u][i] = par[par[u][i - 1]][i - 1]; for (int v : E[u]) { dep[v] = dep[u] + 1; // printf("%i -> %i\n",u,v); DFS(v, u); } rid[u] = tid; } const int S = 500; const int B = N / S + 2; struct SQRT { int addBlock[B]; int add[N]; SQRT() {} void Add(int l, int f) { int L = l / S; for (int i = l; i < (L + 1) * S; i++) add[i] += f; for (int i = L + 1; i < B; i++) addBlock[i] += f; } int Get(int i) { return add[i] + addBlock[i / S]; } int Get(int l, int r) { return Get(r) - Get(l - 1); } } SQ; struct BIT { int sum[N]; BIT() {} void Add(int i, int f) { for (i++; i < N; i += i & -i) sum[i] += f; } void Add(int l, int r, int f) { Add(l, f); Add(r + 1, -f); } int Get(int i) { int ans = 0; for (i++; i > 0; i -= i & -i) ans += sum[i]; return ans; } } BT; bool on[N]; int main() { int n, q; scanf("%i %i", &n, &q); for (int i = 1; i <= n; i++) { scanf("%i %i %i %i", &x1[i], &y1[i], &x2[i], &y2[i]); } Tree::Build(n); for (int x : roots) { DFS(x, 0); } while (q--) { char c; int k; scanf("\n%c %i", &c, &k); if (c == '^') { if (on[k]) { if (SQ.Get(lid[k], rid[k]) == 1) { int x = k; for (int i = L - 1; i >= 0; i--) { if (par[x][i] != 0 && SQ.Get(lid[par[x][i]], rid[par[x][i]]) == 1) { x = par[x][i]; } } BT.Add(dep[x], dep[k], -1); } SQ.Add(lid[k], -1); } else { if (SQ.Get(lid[k], rid[k]) == 0) { int x = k; for (int i = L - 1; i >= 0; i--) { if (par[x][i] != 0 && SQ.Get(lid[par[x][i]], rid[par[x][i]]) == 0) { x = par[x][i]; } } BT.Add(dep[x], dep[k], 1); } SQ.Add(lid[k], 1); } on[k] ^= 1; } else { printf("%i\n", BT.Get(k)); } } return 0; }
failed
75_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long using pii = pair<int, int>; #define fi first #define se second #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() using ll = long long; typedef vector<int> vi; #define pb push_back signed main() { cin.tie(0)->sync_with_stdio(0); int T; cin >> T; while (T--) { string S; cin >> S; int N; cin >> N; while (N--) { auto solve = [&]() -> bool { string T; cin >> T; if (S.front() == 'L') { if (T.front() != 'L') return false; } if (S.back() == 'R') { if (T.back() != 'R') return false; } int cur = 0; for (char c : T) { while (cur < S.size() && S[cur] != c) { ++cur; } if (cur == S.size()) return false; ++cur; } return true; }; cout << (solve() ? "YES" : "NO") << '\n'; } } }
failed
44_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; int n, m; const db eps = 1e-10; const int maxn = 1e5 + 10; int a[maxn], b[maxn]; int ct, c[maxn * 6]; int al[maxn * 6], ar[maxn * 6]; int bl[maxn * 6], br[maxn * 6]; db cross(pair<db, db> p, pair<db, db> q) { return p.first * q.second - p.second * q.first; } void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; c[++ct] = a[i]; c[++ct] = a[i] + 1; if (a[i] - 1) c[++ct] = a[i] - 1; } cin >> m; for (int i = 1; i <= m; i++) { cin >> b[i]; c[++ct] = b[i]; if (b[i] - 1) c[++ct] = b[i] - 1; // ? c[++ct] = b[i] + 1; } sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); long long f1 = 0, f2 = 0; for (int i = 1; i <= n; i++) { f1 += lower_bound(b + 1, b + m + 1, a[i]) - b - 1; } for (int i = 1; i <= m; i++) { f2 += lower_bound(a + 1, a + n + 1, b[i]) - a - 1; } // cerr << f1 << " " << f2 << "\n"; if (f1 < f2) { swap(n, m); for (int i = 1; i <= max(n, m); i++) { swap(a[i], b[i]); } } sort(c + 1, c + ct + 1); ct = unique(c + 1, c + ct + 1) - c - 1; for (int i = 1; i <= n; i++) { a[i] = lower_bound(c + 1, c + ct + 1, a[i]) - c; } for (int i = 1; i <= m; i++) { b[i] = lower_bound(c + 1, c + ct + 1, b[i]) - c; } for (int i = 1; i <= ct; i++) { al[i] = lower_bound(a + 1, a + n + 1, i) - a; ar[i] = upper_bound(a + 1, a + n + 1, i) - a; } for (int i = 1; i <= ct; i++) { bl[i] = lower_bound(b + 1, b + m + 1, i) - b; br[i] = upper_bound(b + 1, b + m + 1, i) - b; } db l = 0, r = 1; // l = r = 0.3; int timer = 1; timer = 50; while (timer--) { db mid = (l + r) / 2.; vector<pair<db, db>> candidate; int flag = 0; for (int i = 1; i <= ct; i++) { // db t2 = 1. * (-(m - br[i] + 1) + (bl[i] - 1)); // db t1 = 1. * ((al[i] - 1) + (br[i] - al[i]) * .5) - mid * n; db t1 = 1. * (-(n - ar[i] + 1) + (al[i] - 1)); db t2 = 1. * ((bl[i] - 1) + (br[i] - bl[i]) * .5) - mid * m; if (abs(t1) > eps || abs(t2) > eps) // cerr << c[i] << " " << t1 << " " << t2 << "\n", candidate.push_back(make_pair(t1, t2)); else { flag = 1; } } // cerr << "\n"; auto cmp = [&](pair<db, db> a, pair<db, db> b) { auto half = [&](pair<db, db> p) { if (abs(p.second) < eps) { if (p.first < 0) return 0; return 1; } if (p.second > 0) return 1; return 0; }; int ha = half(a), hb = half(b); if (ha != hb) return ha < hb; return cross(a, b) >= 0; }; sort(candidate.begin(), candidate.end(), cmp); // pair<db, db> o1(1, 0), o2(0, 1); pair<db, db> o1(0, -1), o2(1, 0); for (int i = 0; i + 1 < candidate.size(); i++) { auto p = candidate[i]; auto q = candidate[i + 1]; if (cross(p, q) <= 0) continue; pair<db, db> s1 = max(o1, p, cmp); pair<db, db> s2 = min(o2, q, cmp); if (cmp(s1, s2)) { flag = 1; break; } } // cerr << mid << " " << flag << " flag\n\n"; if (flag) r = mid; else l = mid; } cout << fixed << setprecision(20) << l << " "; l = 0, r = 1; timer = 50; while (timer--) { db mid = (l + r) / 2.; vector<pair<db, db>> candidate; int flag = 0; for (int i = 1; i <= ct; i++) { db t2 = 1. * ((m - br[i] + 1) - (bl[i] - 1)); db t1 = 1. * ((al[i] - 1) + (ar[i] - al[i]) * .5) - mid * n; // db t1 = 1. * (-(n - ar[i] + 1) + (al[i] - 1)); // db t2 = 1. * ((bl[i] - 1) + (br[i] - bl[i]) * .5) - mid * m; if (abs(t1) > eps || abs(t2) > eps) // cerr << c[i] << " " << t1 << " " << t2 << "\n", candidate.push_back(make_pair(t1, t2)); else { flag = 1; } } // cerr << "\n"; auto cmp = [&](pair<db, db> a, pair<db, db> b) { auto half = [&](pair<db, db> p) { if (abs(p.second) < eps) { if (p.first < 0) return 0; return 1; } if (p.second > 0) return 1; return 0; }; int ha = half(a), hb = half(b); if (ha != hb) return ha < hb; return cross(a, b) >= 0; }; sort(candidate.begin(), candidate.end(), cmp); pair<db, db> o1(1, 0), o2(0, 1); // pair<db, db> o1(0, -1), o2(1, 0); for (int i = 0; i + 1 < candidate.size(); i++) { auto p = candidate[i]; auto q = candidate[i + 1]; if (cross(p, q) <= 0) continue; pair<db, db> s1 = max(o1, p, cmp); pair<db, db> s2 = min(o2, q, cmp); if (cmp(s1, s2)) { flag = 1; break; } } // cerr << mid << " " << flag << " flag\n\n"; if (!flag) r = mid; else l = mid; } cout << l << "\n"; } /* 6 1 1 6 6 8 8 3 2 4 9 0 -6 -0.9 1 -4 -0.9 2 -2 -0.4 3 -2 0.1 4 -2 0.6 5 -2 1.1 6 0 1.1 7 2 1.1 8 4 1.1 9 6 1.6 10 6 2.1 0.3 0 flag 0.3 */ signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
112_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 500005; int T, N, M, L[maxn], R[maxn], B[maxn << 1], Len, tree[maxn << 1], F[maxn << 1]; struct node { int x, y; bool operator<(const node &B) const { return x < B.x || (x == B.x && y > B.y); } } A[maxn << 1]; void add(int x, int y) { for (; x <= Len; x += x & -x) tree[x] = max(tree[x], y); } int query(int x) { int ret = 0; for (; x; x -= x & -x) ret = max(ret, tree[x]); return ret; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> T; int p = 0; bool flg = (T == 9653); while (T--) { cin >> N; M = 0; for (int i = 1; i <= N; i++) cin >> L[i] >> R[i]; p++; if (p == 20) { printf("%d\n", N); for (int i = 1; i <= N; i++) printf("%d %d\n", L[i], R[i]); } A[++M] = (node){1, L[1] * 2}; for (int i = 2; i <= N; i++) { if (L[i] > L[i - 1]) A[++M] = (node){2 * (i - 1) + 1, L[i] * 2}; if (R[i] > R[i - 1]) A[++M] = (node){2 * (i - 1), R[i - 1] * 2 + 1}; } Len = 0; for (int i = 1; i <= M; i++) B[++Len] = A[i].y, tree[i] = 0; //,printf("** %d %d\n",A[i].x,A[i].y); sort(B + 1, B + 1 + Len); Len = unique(B + 1, B + 1 + Len) - B - 1; for (int i = 1; i <= M; i++) A[i].y = lower_bound(B + 1, B + 1 + Len, A[i].y) - B; sort(A + 1, A + 1 + M); for (int i = 1; i <= M; i++) { add(A[i].y, F[i] = query(A[i].y - 1) + 1); } if (!flg) cout << query(Len) << '\n'; } return 0; }
failed
19_2_wrong.cpp
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair using namespace std; const ll N = 5e4 + 4; ll a[N], b[N]; ll mi[4 * N]; ll sm, n; vector<int> u; ll gcd(ll x, ll y) { while (x * y > 0) { if (x > y) x %= y; else y %= x; } return x + y; } void mbuild(ll v, ll l, ll r) { if (l == r) mi[v] = l; else { ll mid = (l + r) / 2; mbuild(2 * v, l, mid); mbuild(2 * v + 1, mid + 1, r); if (a[mi[2 * v]] <= a[mi[2 * v + 1]]) mi[v] = mi[2 * v]; else mi[v] = mi[2 * v + 1]; } } ll query(ll v, ll l, ll r, ll ql, ll qr) { if (r < ql || qr < l) return 0; if (ql <= l && r <= qr) return mi[v]; ll mid = (l + r) / 2; ll x = query(2 * v, l, mid, ql, qr); ll y = query(2 * v + 1, mid + 1, r, ql, qr); if (x == 0) return y; if (y == 0) return x; if (a[x] < a[y]) return x; return y; } ll check(ll l, ll r, ll x) { if (r < l) return 1; ll ret = 1; if (l == r) return ret; ll mid = query(1, 1, n, l, r); if (l != mid) { ll t = query(1, 1, n, l, mid - 1); if ((a[t] + x) % (a[mid] + x) != 0) { ret = 0; } } if (mid != r) { ll t = query(1, 1, n, mid + 1, r); if ((a[t] + x) % (a[mid] + x) != 0) { ret = 0; } } return min(ret, min(check(l, mid - 1, x), check(mid + 1, r, x))); } int main() { ll t, i, k, p, un, ans, sum, mia; scanf("%lld", &t); while (t--) { u.clear(); sm = 1; scanf("%lld", &n); scanf("%lld", &k); for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); b[i] = a[i]; } mia = a[1]; for (i = 2; i <= n; i++) { mia = min(mia, a[i]); } if (n == 1) { printf("%lld %lld\n", k, (k + 1) * k / 2); continue; } sort(b + 1, b + n + 1); p = 0; for (i = n; i >= 2; i--) { p = gcd(p, b[i] - b[i - 1]); } if (p == 0) { printf("%lld %lld\n", k, (k + 1) * k / 2); continue; } for (i = 1; i * i <= p; i++) { if (p % i == 0) { u.pb(i); if (i * i != p) u.pb(p / i); } } mbuild(1, 1, n); un = u.size(); ans = 0; sum = 0; for (i = 0; i < un; i++) { if (u[i] - mia <= k && mia < u[i] && check(1, n, u[i] - mia)) { sum += u[i] - mia; ans++; } } printf("%lld %lld\n", ans, sum); } return 0; } /* 5 5 10 7 79 1 7 1 2 1000000000 1 2 1 100 1000000000 4 100 25 25 25 25 4 21 14 4 24 99 199 */
failed
33_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n, q, fa[N][20], on[N], dfn[N], sz[N], dep[N], dfsOrder; vector<int> g[N]; struct node { int x1, x2, y, tp, no; friend bool operator<(const node &a, const node &b) { return a.y < b.y; } }; struct BIT { vector<int> tr; BIT(int n) : tr(n + 1) {} void update(int x, int k) { for (; x < tr.size(); x += x & -x) tr[x] += k; } int query(int x) { int ret = 0; for (; x; x -= x & -x) ret += tr[x]; return ret; } }; void dfs(int s) { dfn[s] = ++dfsOrder; sz[s] = 1; for (int son : g[s]) { dep[son] = dep[s] + 1; dfs(son); sz[s] += sz[son]; } } vector<node> v; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> q; vector<int> val; for (int i = 1; i <= n; i++) { int x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; v.push_back({x1, x2, y1, 1, i}); v.push_back({x1, x2, y2, 2, i}); val.push_back(x1); val.push_back(x2); val.push_back(y1), val.push_back(y2); } sort(val.begin(), val.end()); for (auto &[x1, x2, y, tp, no] : v) { x1 = lower_bound(val.begin(), val.end(), x1) - val.begin() + 1; x2 = lower_bound(val.begin(), val.end(), x2) - val.begin() + 1; y = lower_bound(val.begin(), val.end(), y) - val.begin() + 1; } sort(v.begin(), v.end()); BIT tr(n << 2 | 2); for (auto [x1, x2, y, tp, no] : v) { if (tp == 1) { fa[no][0] = tr.query(x1); tr.update(x1, no - fa[no][0]); tr.update(x2 + 1, fa[no][0] - no); } else { tr.update(x2 + 1, no - fa[no][0]); tr.update(x1, fa[no][0] - no); } } int rt; for (int i = 1; i <= n; i++) if (!fa[i][0]) rt = i; for (int i = 1; i <= n; i++) { g[fa[i][0]].push_back(i); } dfs(rt); for (int i = 1; i < 20; i++) { for (int j = 1; j <= n; j++) { fa[j][i] = fa[fa[j][i - 1]][i - 1]; } } BIT tr2(n + 2), tr3(n + 2); while (q--) { char tp; int k; cin >> tp >> k; if (tp == '^') { on[k] ^= 1; if (on[k]) { int cur = k; for (int i = 19; ~i; i--) { int nxt = fa[cur][i]; if (nxt) { int l = dfn[nxt], r = dfn[nxt] + sz[nxt] - 1; if (tr2.query(r) == tr2.query(l - 1)) cur = nxt; } } tr3.update(dep[cur] + 1, 1); tr3.update(dep[k] + 2, -1); tr2.update(dfn[k], 1); } else { tr2.update(dfn[k], -1); int cur = k; for (int i = 19; ~i; i--) { int nxt = fa[cur][i]; if (nxt) { int l = dfn[nxt], r = dfn[nxt] + sz[nxt] - 1; if (tr2.query(r) == tr2.query(l - 1)) cur = nxt; } } tr3.update(dep[cur] + 1, -1); tr3.update(dep[k] + 2, 1); } } else { cout << tr3.query(k + 1) << endl; } } }
failed
75_3_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; #define rep(i, n) for (int i = 0; i < int(n); i++) using namespace std; #include <initializer_list> namespace nachia { bool IsPrime(unsigned long long x) noexcept { if (x <= 1) return false; if (x % 2 == 0) return x == 2; using u64 = unsigned long long; using u128 = __uint128_t; u64 d = x - 1; int s = 0; int q = 63; while (!(d & 1)) { d >>= 1; s++; } while (!(d >> q)) q--; u64 r = x; for (int t = 0; t < 6; t++) r *= 2 - r * x; u128 n2 = -(u128)x % x; auto red = [=](u128 t) noexcept -> u64 { u64 t2 = (t + (u128)((u64)t * -r) * x) >> 64; return (t2 >= x || t2 < (t >> 64)) ? t2 - x : t2; }; u64 one = red(n2); for (u64 base : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (base % x == 0) continue; u64 a = base = red(base % x * n2); for (int e = q - 1; e >= 0; e--) { a = red((u128)a * a); if ((d >> e) & 1) a = red((u128)a * base); } if (a == one) continue; for (int t = 1; t < s && a != x - one; t++) a = red((u128)a * a); if (a != x - one) return false; } return true; } } // namespace nachia namespace nachia { int Popcount(unsigned long long c) noexcept { #ifdef __GNUC__ return __builtin_popcountll(c); #else c = (c & (~0ull / 3)) + ((c >> 1) & (~0ull / 3)); c = (c & (~0ull / 5)) + ((c >> 2) & (~0ull / 5)); c = (c & (~0ull / 17)) + ((c >> 4) & (~0ull / 17)); c = (c * (~0ull / 257)) >> 56; return c; #endif } // please ensure x != 0 int MsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return 63 - __builtin_clzll(x); #else using u64 = unsigned long long; int q = (x >> 32) ? 32 : 0; auto m = x >> q; constexpr u64 hi = 0x88888888; constexpr u64 mi = 0x11111111; m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 35; m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 31; q += (m & 0xf) << 2; q += 0x3333333322221100 >> (((x >> q) & 0xf) << 2) & 0xf; return q; #endif } // please ensure x != 0 int LsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return __builtin_ctzll(x); #else return MsbIndex(x & -x); #endif } } // namespace nachia namespace nachia { std::vector<std::pair<unsigned long long, int>> Factorize(unsigned long long x) { if (x == 1) return {}; if (IsPrime(x)) return {{x, 1}}; using u64 = unsigned long long; using u128 = __uint128_t; u64 X = x; std::vector<u64> p; for (u64 i = 2; i < 100; i += 1 + i % 2) if (x % i == 0) { p.push_back(i); while (x % i == 0) x /= i; } u64 r = 1; u128 n2 = 1; auto updX = [&]() { r = x; for (int t = 0; t < 6; t++) r *= 2 - r * x; n2 = -(u128)x % x; }; auto red = [&](u128 t) noexcept -> u64 { u64 s = ((u128)x * ((u64)t * r)) >> 64; u64 t2 = t >> 64; return t2 - s + (t2 < s ? x : 0); }; auto mult = [&](u64 a, u64 b) noexcept { return red((u128)red((u128)a * n2) * b); }; auto gcd = [](u64 a, u64 b) noexcept { if (!a || !b) return a | b; int q = LsbIndex(a | b); b >>= LsbIndex(b); a >>= LsbIndex(a); while (a != b) { if (a < b) { b -= a; b >>= LsbIndex(b); } else { a -= b; a >>= LsbIndex(a); } } return a << q; }; static u64 v = 7001; p.push_back(x); for (int pi = p.size() - 1; pi < (int)p.size(); pi++) while (p[pi] != 1 && !IsPrime(p[pi])) { x = p[pi]; updX(); while (p[pi] == x) { v ^= v << 13; v ^= v >> 7; v ^= v << 17; // Xorshift // https://www.jstatsoft.org/article/download/v008i14/916 u64 c = red(v); if (c == 0) continue; auto f = [=](u64 a) noexcept -> u64 { return red((u128)a * a + c); }; u64 a = 0, b = f(a); u64 buf = 1, sz = 1, nx = 10; while (true) { while (nx != sz && a != b) { buf = mult(buf, a <= b ? b - a : a - b); sz++; a = f(a); b = f(f(b)); } u64 g = gcd(buf, x); if (g != 1) { while (p[pi] % g == 0) p[pi] /= g; p.push_back(g); break; } if (a == b) break; nx = sz * 3 / 2; } } } std::vector<std::pair<u64, int>> res; for (u64 q : p) if (q != 1) { int e = 0; while (X % q == 0) { e++; X /= q; } if (e) res.push_back({q, e}); } return res; } unsigned long long Totient(unsigned long long x) { auto F = Factorize(x); for (auto f : F) x -= x / f.first; return x; } } // namespace nachia namespace nachia { template <class Int> Int Gcd(Int a, Int b) { if (a < 0) a = -a; if (b < 0) b = -b; if (!a || !b) return a + b; while (b) { a %= b; std::swap(a, b); } return a; } } // namespace nachia namespace nachia { struct EnumerateDivisors { using u64 = unsigned long long; u64 raw; std::vector<u64> divord; std::vector<int> dims; std::vector<int> dimcum; std::vector<std::pair<u64, int>> I; std::vector<std::pair<u64, int>> pfs; EnumerateDivisors(std::vector<std::pair<unsigned long long, int>> pf) : pfs(std::move(pf)) { raw = 1; int n = pfs.size(); dims.resize(n); dimcum.assign(n + 1, 1); divord = {1}; for (int i = 0; i < n; i++) { dims[i] = pfs[i].second; dimcum[i + 1] = dimcum[i] * (dims[i] + 1); int q = dimcum[i]; for (int t = q; t < dimcum[i + 1]; t++) divord.push_back(divord[t - q] * pfs[i].first); for (int t = 0; t < pfs[i].second; t++) raw *= pfs[i].first; } I.resize(divord.size()); for (int i = 0; i < dimcum.back(); i++) I[i] = std::make_pair(divord[i], i); std::sort(I.begin(), I.end()); } EnumerateDivisors(unsigned long long n) : EnumerateDivisors(Factorize(n)) {} int id(unsigned long long d) const { d = Gcd(d, raw); return std::lower_bound( I.begin(), I.end(), d, [](std::pair<u64, int> e, u64 v) { return e.first < v; }) ->second; } int numDivisors() const { return dimcum.back(); } unsigned long long divisor(int i) { return divord[i]; } template <class Elem> void Zeta(std::vector<Elem> &A) const { int Z = numDivisors(); for (int d = 0; d < (int)dims.size(); d++) { int w = dims[d] * dimcum[d]; int y = dimcum[d]; for (int i = 0; i < Z; i += dimcum[d + 1]) { for (int j = 0; j < w; j++) A[i + j + y] += A[i + j]; } } } template <class Elem> void RevZeta(std::vector<Elem> &A) const { int Z = numDivisors(); for (int d = 0; d < (int)dims.size(); d++) { int w = dims[d] * dimcum[d]; int y = dimcum[d]; for (int i = 0; i < Z; i += dimcum[d + 1]) { for (int j = w - 1; j >= 0; j--) A[i + j] += A[i + j + y]; } } } template <class Elem> void Mobius(std::vector<Elem> &A) const { int Z = numDivisors(); for (int d = 0; d < (int)dims.size(); d++) { int w = dims[d] * dimcum[d]; int y = dimcum[d]; for (int i = 0; i < Z; i += dimcum[d + 1]) { for (int j = w - 1; j >= 0; j--) A[i + j + y] -= A[i + j]; } } } template <class Elem> void RevMobius(std::vector<Elem> &A) const { int Z = numDivisors(); for (int d = 0; d < (int)dims.size(); d++) { int w = dims[d] * dimcum[d]; int y = dimcum[d]; for (int i = 0; i < Z; i += dimcum[d + 1]) { for (int j = 0; j < w; j++) A[i + j] -= A[i + j + y]; } } } }; } // namespace nachia namespace nachia { // ax + by = gcd(a,b) // return ( x, - ) std::pair<long long, long long> ExtGcd(long long a, long long b) { long long x = 1, y = 0; while (b) { long long u = a / b; std::swap(a -= b * u, b); std::swap(x -= y * u, y); } return std::make_pair(x, a); } } // namespace nachia namespace nachia { template <unsigned int MOD> struct StaticModint { private: using u64 = unsigned long long; unsigned int x; public: using my_type = StaticModint; template <class Elem> static Elem safe_mod(Elem x) { if (x < 0) { if (0 <= x + MOD) return x + MOD; return MOD - ((-(x + MOD) - 1) % MOD + 1); } return x % MOD; } StaticModint() : x(0) {} StaticModint(const my_type &a) : x(a.x) {} StaticModint &operator=(const my_type &) = default; template <class Elem> StaticModint(Elem v) : x(safe_mod(v)) {} unsigned int operator*() const { return x; } my_type &operator+=(const my_type &r) { auto t = x + r.x; if (t >= MOD) t -= MOD; x = t; return *this; } my_type operator+(const my_type &r) const { my_type res = *this; return res += r; } my_type &operator-=(const my_type &r) { auto t = x + MOD - r.x; if (t >= MOD) t -= MOD; x = t; return *this; } my_type operator-(const my_type &r) const { my_type res = *this; return res -= r; } my_type operator-() const noexcept { my_type res = *this; res.x = ((res.x == 0) ? 0 : (MOD - res.x)); return res; } my_type &operator*=(const my_type &r) { x = (u64)x * r.x % MOD; return *this; } my_type operator*(const my_type &r) const { my_type res = *this; return res *= r; } bool operator==(const my_type &r) const { return x == r.x; } my_type pow(unsigned long long i) const { my_type a = *this, res = 1; while (i) { if (i & 1) { res *= a; } a *= a; i >>= 1; } return res; } my_type inv() const { return my_type(ExtGcd(x, MOD).first); } unsigned int val() const { return x; } static constexpr unsigned int mod() { return MOD; } static my_type raw(unsigned int val) { auto res = my_type(); res.x = val; return res; } my_type &operator/=(const my_type &r) { return operator*=(r.inv()); } my_type operator/(const my_type &r) const { return operator*(r.inv()); } }; } // namespace nachia using Modint = nachia::StaticModint<998244353>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i64 N, M; cin >> N >> M; auto divs = nachia::EnumerateDivisors(N); i64 n = divs.numDivisors(); vector<i64> D(n); rep(i, n) D[i] = divs.divisor(i); auto dims = divs.dimcum; i64 dimn = dims.size() - 1; vector<Modint> A(n); rep(i, n) A[i] = M / D[i]; divs.RevMobius(A); vector<Modint> C(n); C[0] = -1; divs.Mobius(C); vector<Modint> dp(n); i64 maskx = 1 << dimn; vector<i64> offset(maskx), offsetv(maskx, 1); rep(t, dimn) rep(i, 1 << t) offset[i + (1 << t)] = offset[i] + dims[t]; rep(t, dimn) rep(i, 1 << t) offsetv[i + (1 << t)] = offsetv[i] * D[dims[t]]; for (i64 i = n - 1; i >= 0; i--) { i64 mask = 0; rep(t, dimn) if ((i + dims[t]) % dims[t + 1] >= dims[t]) mask |= 1ll << t; for (i64 j = mask; j >= 1; j = (j - 1) & mask) { dp[i] += C[offset[j]] * dp[i + offset[j]].pow(offsetv[j]); } dp[i] += A[i]; } cout << dp[0].val() << "\n"; return 0; }
failed
42_2_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; struct BlockCutTree { int n; std::vector<std::vector<int>> adj; std::vector<int> dfn, low, stk; int cnt, cur; std::vector<std::pair<int, int>> edges; BlockCutTree() {} BlockCutTree(int n) { init(n); } void init(int n) { this->n = n; adj.assign(n, {}); dfn.assign(n, -1); low.resize(n); stk.clear(); cnt = cur = 0; edges.clear(); } void addEdge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); } void dfs(int x) { stk.push_back(x); dfn[x] = low[x] = cur++; for (auto y : adj[x]) { if (dfn[y] == -1) { dfs(y); low[x] = std::min(low[x], low[y]); if (low[y] == dfn[x]) { int v; do { v = stk.back(); stk.pop_back(); edges.emplace_back(n + cnt, v); } while (v != y); edges.emplace_back(x, n + cnt); cnt++; } } else { low[x] = std::min(low[x], dfn[y]); } } } std::pair<int, std::vector<std::pair<int, int>>> work() { for (int i = 0; i < n; i++) { if (dfn[i] == -1) { stk.clear(); dfs(i); } } return {cnt, edges}; } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int h, w; std::cin >> h >> w; std::vector<std::string> s(h); for (int i = 0; i < h; i++) { std::cin >> s[i]; } std::vector a(h, std::vector<int>(w)); auto idx = [&](int x, int y) { int u = x * w + y + 1; if (u == h * w) { u = 0; } return u; }; std::vector<int> p(h * w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { std::cin >> a[i][j]; p[idx(i, j)] = a[i][j]; } } BlockCutTree g(h * w); std::vector<bool> emp(h * w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '.') { emp[idx(i, j)] = true; if (j + 1 < w && s[i][j + 1] == '.') { g.addEdge(idx(i, j), idx(i, j + 1)); } if (i + 1 < h && s[i + 1][j] == '.') { g.addEdge(idx(i, j), idx(i + 1, j)); } } } } auto [cnt, edges] = g.work(); std::vector<int> par(h * w, -1); for (auto [u, v] : edges) { if (v < h * w) { par[v] = u; } } std::vector<int> parity(cnt); for (int i = 1; i < h * w; i++) { if (!emp[i]) { continue; } if (par[i] == -1 || par[i] != par[p[i]]) { std::cout << "impossble\n"; return 0; } } std::vector<bool> vis(h * w); for (int i = 1; i < h * w; i++) { if (!emp[i] || vis[i]) { continue; } int j = i; int len = 0; while (!vis[j]) { vis[j] = true; j = p[j]; len++; } parity[par[i] - h * w] ^= (len - 1) & 1; } if (std::find(parity.begin(), parity.end(), 1) == parity.end()) { std::cout << "possible\n"; } else { std::cout << "impossible\n"; } return 0; }
failed
63_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; template <class T> int sgn(T x) { return (x > 0) - (x < 0); } template <class T> struct Point { typedef Point P; T x, y; explicit Point(T x = 0, T y = 0) : x(x), y(y) {} bool operator<(P p) const { return tie(x, y) < tie(p.x, p.y); } bool operator==(P p) const { return tie(x, y) == tie(p.x, p.y); } P operator+(P p) const { return P(x + p.x, y + p.y); } P operator-(P p) const { return P(x - p.x, y - p.y); } P operator*(T d) const { return P(x * d, y * d); } P operator/(T d) const { return P(x / d, y / d); } T dot(P p) const { return x * p.x + y * p.y; } T cross(P p) const { return x * p.y - y * p.x; } T cross(P a, P b) const { return (a - *this).cross(b - *this); } T dist2() const { return x * x + y * y; } double dist() const { return sqrt((double)dist2()); } // angle to x-axis in interval [-pi, pi] double angle() const { return atan2(y, x); } P unit() const { return *this / dist(); } // makes dist()=1 P perp() const { return P(-y, x); } // rotates +90 degrees P normal() const { return perp().unit(); } // returns point rotated 'a' radians ccw around the origin P rotate(double a) const { return P(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)); } friend ostream &operator<<(ostream &os, P p) { return os << "(" << p.x << "," << p.y << ")"; } }; typedef Point<double> P; bool circleInter(P a, P b, double r1, double r2, pair<P, P> *out) { if (a == b) { assert(r1 != r2); return false; } P vec = b - a; double d2 = vec.dist2(), sum = r1 + r2, dif = r1 - r2, p = (d2 + r1 * r1 - r2 * r2) / (d2 * 2), h2 = r1 * r1 - p * p * d2; if (sum * sum < d2 || dif * dif > d2) return false; P mid = a + vec * p, per = vec.perp() * sqrt(fmax(0, h2) / d2); *out = {mid + per, mid - per}; return true; } template <class P> pair<int, P> lineInter(P s1, P e1, P s2, P e2) { auto d = (e1 - s1).cross(e2 - s2); if (d == 0) // if parallel return {-(s1.cross(e1, s2) == 0), P(0, 0)}; auto p = s2.cross(e1, e2), q = s2.cross(e2, s1); return {1, (s1 * p + e1 * q) / d}; } template <class P> int sideOf(P s, P e, P p) { return sgn(s.cross(e, p)); } template <class P> int sideOf(const P &s, const P &e, const P &p, double eps) { auto a = (e - s).cross(p - s); double l = (e - s).dist() * eps; return (a > l) - (a < -l); } bool inCircle(P p, P c, int r) { return (p - c).dist2() < r * r; } void run() { int n; cin >> n; vector<tuple<int, int, int>> circles(n); for (auto &[x, y, r] : circles) cin >> x >> y >> r; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { P a(get<0>(circles[i]), get<1>(circles[i])), b(get<0>(circles[j]), get<1>(circles[j])); pair<P, P> out; if (not circleInter(a, b, get<2>(circles[i]), get<2>(circles[j]), &out) or out.first == out.second) { cout << "NO\n"; for (int k = 0; k < n; k++) cout << "1 "; cout << "\n"; return; } } for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) for (int k = j + 1; k < n; k++) { P a(get<0>(circles[i]), get<1>(circles[i])), b(get<0>(circles[j]), get<1>(circles[j])), c(get<0>(circles[k]), get<1>(circles[k])); pair<P, P> out; circleInter(a, b, get<2>(circles[i]), get<2>(circles[j]), &out); P ab = out.first, ba = out.second; circleInter(b, c, get<2>(circles[j]), get<2>(circles[k]), &out); P bc = out.first, cb = out.second; circleInter(c, a, get<2>(circles[k]), get<2>(circles[i]), &out); P ca = out.first, ac = out.second; auto [_, inter] = lineInter(ab, ba, bc, cb); if (inCircle(inter, a, get<2>(circles[i]))) continue; assert(_ == 1); int ans = -1; if (sideOf(ab, ba, cb) != sideOf(ac, ca, cb)) { ans = i; } else if (sideOf(bc, cb, ac) != sideOf(ab, ba, ac)) { ans = j; } else if (sideOf(ca, ac, ab) != sideOf(bc, cb, ab)) { ans = k; } if (ans != -1) { cout << "NO\n"; for (int l = 0; l < n; l++) cout << (l == ans ? 0 : 1) << " "; cout << "\n"; return; } } cout << "YES\n"; } int main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; while (t--) run(); }
failed
96_1_wrong.cpp
#include "bits/stdc++.h" using namespace std; #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define int long long namespace static_rmq2 { const int K = 20; const int mxn = 1e6 + 5; int N, a[mxn], st[K + 1][mxn]; inline void build() { iota(st[0], st[0] + N, 0); for (int i = 1; i <= K; i++) for (int j = 0; j + (1 << i) <= N; j++) if (a[st[i - 1][j]] <= a[st[i - 1][j + (1 << (i - 1))]]) st[i][j] = st[i - 1][j]; else st[i][j] = st[i - 1][(j + (1 << (i - 1)))]; } inline void init(vector<int> &arr) { copy(all(arr), a); N = sz(arr); build(); } inline void init(int *arr, int size) { copy(arr, arr + size, a); N = size; build(); } inline int query(int l, int r) { int i = log2(r - l + 1); if (a[st[i][l]] < a[st[i][r - (1 << i) + 1]]) return st[i][l]; return st[i][r - (1 << i) + 1]; } }; // namespace static_rmq2 namespace lca { const int mxn = 5e5 + 5; vector<int> adj[mxn]; int tin[mxn], dtour[2 * mxn], tour[2 * mxn], nx = 0; inline void clear(int n) { for (int i = 0; i < n; i++) adj[i].clear(); } inline void add_edge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); } inline void copy_adj(vector<int> *g, int size) { copy(g, g + size, adj); } void dfs(int v, int p, int d) { dtour[nx] = d, tour[nx] = v, tin[v] = nx++; for (int i : adj[v]) { if (i != p) { dfs(i, v, d + 1); dtour[nx] = d; tour[nx++] = v; } } } inline void build(int rt = 0) { dfs(0, 0, 0); static_rmq2::init(dtour, nx); } inline int lca(int u, int v) { int i1 = tin[u], i2 = tin[v]; if (i1 > i2) swap(i1, i2); return tour[static_rmq2::query(i1, i2)]; } inline int dist(int u, int v) { return dtour[u] + dtour[v] - 2 * dtour[lca(u, v)]; } }; // namespace lca const int mxn = 1e5 + 5; const int mod = 998244353; int ksm(int a, int b = mod - 2) { int res = 1, aux = a; for (int i = 1; i <= b; i *= 2) { if (i & b) res = res * aux % mod; aux = aux * aux % mod; } return res; } int n, q, f[mxn], dp[mxn], pf[mxn], rdp[mxn]; vector<int> g[mxn]; void dfs(int v, int f) { dp[v] = 1; for (int u : g[v]) { dp[v] *= (1 + dp[u]); dp[v] %= mod; } rdp[v] = dp[v]; for (int u : g[v]) { if (u == f) continue; dp[v] *= ksm(1 + dp[u]); dp[v] %= mod; dfs(u, v); dp[v] *= (1 + dp[u]); dp[v] %= mod; } } void solve() { cin >> n >> q; lca::clear(n); for (int i = 0; i < n; i++) g[i].clear(); for (int i = 1; i < n; i++) { cin >> f[i]; --f[i]; lca::add_edge(i, f[i]); g[f[i]].push_back(i); g[i].push_back(f[i]); } lca::build(); for (int i = n - 1; i >= 0; i--) { dp[i] = 1; for (int u : g[i]) { if (u < i) continue; dp[i] *= (1 + dp[u]); dp[i] %= mod; } } for (int i = 0; i < n; i++) { // cout<<i<<" "<<dp[i]<<"\n"; pf[i] = dp[i]; if (i) { pf[i] = (pf[i] + pf[f[i]]) % mod; } } dfs(0, 0); while (q--) { int u, v; cin >> u >> v; --u; --v; int ans = pf[u] + pf[v] - 2 * pf[lca::lca(u, v)] + rdp[lca::lca(u, v)]; ans %= mod; ans += mod; ans %= mod; cout << ans << "\n"; } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } }
failed
4_3_wrong.cpp
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back using namespace std; typedef long long ll; typedef pair<int, int> pii; const int mod = 998244353; inline int add(int x, int y) { int ret = x + y; if (ret >= mod) ret -= mod; return ret; } inline int sub(int x, int y) { int ret = x - y; if (ret < 0) ret += mod; return ret; } inline int mul(int x, int y) { return ((ll)x * y) % mod; } inline int step(int base, int pw) { int ret = 1; while (pw) { if (pw & 1) ret = mul(ret, base); base = mul(base, base); pw >>= 1; } return ret; } inline int invv(int x) { return step(x, mod - 2); } const int maxn = 3e5 + 10; ll gcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll x1, y1; ll d = gcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0, ll &g) { g = gcd(abs(a), abs(b), x0, y0); if (c % g) { return false; } x0 *= c / g; y0 *= c / g; if (a < 0) x0 = -x0; if (b < 0) y0 = -y0; return true; } int main() { /// freopen("test.txt","r",stdin); int t; scanf("%d", &t); while (t--) { ll a, b, n; scanf("%lld %lld %lld", &a, &b, &n); if (n > a * b) { printf("No\n"); continue; } if (n <= b || n % a == 0) { printf("Yes\n"); continue; } ll p, k, g; find_any_solution(a, b, n, p, k, g); k = -k; k %= a; if (k < 0) k += a; if (n + b * k > a * b) { printf("No\n"); } else printf("Yes\n"); } return 0; }
failed
18_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int add(int x, int y) { x += y; return x >= mod ? x - mod : x; } int mul(int x, int y) { return 1ll * x * y % mod; } int qpow(int a, int b) { int ans = 1; while (b) { if (b & 1) { ans = mul(ans, a); } a = mul(a, a); b >>= 1; } return ans; } const int maxn = 22; int inv[maxn]; int C(int n, int m) { int ans = 1; for (int i = 1; i <= m; i++) { ans = mul(ans, mul(n + 1 - i, inv[i])); } // cout << n << " " << m << " " << ans << "\n"; return ans; } int n1, n2, m, k; int a[maxn * maxn], b[maxn * maxn]; int get(int mask, int l, int r) { //[l,r) return (mask & ((1 << r) - 1)) - (mask & ((1 << l) - 1)); } void fwt(vector<int> &A, int l, int op) { for (int i = 1; i < (1 << l); i <<= 1) { for (int j = 0; j < (1 << l); j += i * 2) { for (int k = 0; k < i; k++) { if (op == 1) { A[j + k + i] = add(A[j + k + i], A[j + k]); } else { A[j + k + i] = add(A[j + k + i], mod - A[j + k]); } } } } } void solve() { for (int i = 1; i <= maxn; i++) { inv[i] = qpow(i, mod - 2); } cin >> n1 >> n2 >> m >> k; vector<int> bad(1 << m); int must = 0; int all = 0; for (int i = 1; i <= k; i++) { cin >> a[i] >> b[i]; all |= (1 << a[i] - 1); all |= (1 << b[i] - 1); if (a[i] == b[i]) must |= (1 << (a[i] - 1)); else { if (a[i] > b[i]) swap(a[i], b[i]); for (int t = 0; t < (1 << m - 2); t++) { int s = get(t, 0, a[i] - 1) | (get(t, a[i], b[i] - 1) >> 1) | (get(t, b[i], m) >> 2); // cout << s << " fffuufufukc\n"; bad[s] = 1; } } } vector<int> A(1 << m), B(1 << m); for (int s = 0; s < (1 << m); s++) { // cout << s << " " << bad[s] << " bad\n"; if (bad[s] || (s & must) != must) continue; // cout << s << " wttffff\n"; A[s] = C(n1 - 1, (int)__builtin_popcount(s) - 1); B[s] = C(n2 - 1, (int)__builtin_popcount(s) - 1); } // for (int a : A) cout << a << " "; // cout << "A\n"; // for (int b : B) cout << b << " "; // cout << "B\n"; fwt(A, m, 1); fwt(B, m, 1); for (int s = 0; s < (1 << m); s++) { A[s] = mul(A[s], B[s]); } fwt(A, m, -1); int ans = 0; for (int s = 0; s < (1 << m); s++) { // cout << A[s] << " \n"[s + 1 == (1 << m)]; if ((s & all) == all) ans = add(ans, A[s]); } cout << ans << "\n"; } /* 1 2 3 3 3 2 3 1 1 2 3 */ int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
failed
30_1_wrong.cpp
#ifdef local #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3) #endif // #pragma comment(linker, "/STACK:102400000,102400000") #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #ifndef local #define endl '\n' #endif #define pb emplace_back #define fi first #define se second // #define endl '\n' #define rep(i, l, r) for (long long i = l; i <= r; i++) #define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define mem(a, x) memset(a, x, sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef double db; typedef pair<int, int> P; typedef pair<P, int> PP; const double pi = acos(-1); typedef __int128_t int128; const ll mod = 1e18; const db eps = 1e-9; std::mt19937_64 rng(time(0)); ll my_random(ll l, ll r) { uniform_int_distribution<int> range(l, r); return range(rng); } void __() { #ifdef local system("pause"); #endif } ll qp(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } const int maxn = 2e5 + 10; int n, m; string dice[10]; set<string> st; db dp[2][maxn]; int pw[maxn]; int v[10], w[10]; db tmpsum, tmpcnt; void dfs3(int cnt, int sum, int flag) { if (cnt == n) { tmpsum += dp[(flag & 1)][sum]; tmpcnt += 1; return; } else { if (v[cnt] != 6) dfs3(cnt + 1, sum + pw[cnt] * v[cnt], flag); else for (int i = 0; i < 6; i++) dfs3(cnt + 1, sum + pw[cnt] * i, flag); } } void dfs2(int cnt, int sum, int nowsum, int flag) { if (cnt == n) { dp[flag & 1][nowsum] = min(dp[flag & 1][nowsum], dp[(flag & 1) ^ 1][sum]); return; } else { dfs2(cnt + 1, sum + pw[cnt] * v[cnt], nowsum, flag); dfs2(cnt + 1, sum + pw[cnt] * 6, nowsum, flag); } } void dfs(int cnt, int flag) { if (cnt == n) { bool f = 1; int sum = 0; for (int i = 0; i < n; i++) { if (v[i] == 6) f = 0; sum += pw[i] * v[i]; } if (f == 1) { string s; for (int i = 0; i < n; i++) { s += dice[i][v[i]]; // cout << i << " " << v[i] << " " << dice[i][v[i]] << " " << s << endl; } sort(s.begin(), s.end()); if (st.find(s) != st.end()) { // cout << "XXX " << s << " " << flag << " " << (flag & 1) << endl; dp[flag & 1][sum] = 0; } else { dp[flag & 1][sum] = 1e10; dfs2(0, 0, sum, flag); } } else { tmpsum = tmpcnt = 0; dfs3(0, 0, flag); // cout << "XXX " << sum << " " << tmpsum << " " << tmpcnt << endl; dp[flag & 1][sum] = tmpsum / tmpcnt + 1; } } else { for (int i = 0; i <= 6; i++) v[cnt] = i, dfs(cnt + 1, flag); } } int main() { IOS; int t1 = clock(); cin >> n >> m; pw[0] = 1; for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * 7; for (int i = 0; i < n; i++) cin >> dice[i]; for (int i = 1; i <= m; i++) { string s; cin >> s; sort(s.begin(), s.end()); st.insert(s); } int iter_num = 10000; for (int i = 0; i <= pw[n]; i++) dp[0][i] = 1e10; int i = 1; while (clock() - t1 < 9500) { dfs(0, i); mem(dp[(i & 1) ^ 1], 0); // for (int j = 0; j < pw[n]; j++) // cout << dp[i & 1][j] << " "; // cout << endl; i += 1; } if (dp[(i - 1) & 1][pw[n] - 1] >= 1e9) cout << "impossible" << endl; else cout << fixed << setprecision(20) << dp[(i - 1) & 1][pw[n] - 1] << endl; __(); return 0; } /* 1 1 ABCDEF B */
failed
118_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; struct OpInfo { int precedence; bool is_left_assoc; bool is_binary; }; map<string, OpInfo> op_info = { {"!", {4, false, false}}, {"=", {3, true, true}}, {"&", {2, true, true}}, {"|", {1, true, true}}, {"^", {0, true, true}}, }; vector<string> tokenize(const string& s) { vector<string> tokens; for (char c : s) tokens.emplace_back(1, c); return tokens; } vector<string> shunting_yard(const vector<string>& tokens) { vector<string> output; stack<string> op_stack; for (const string& token : tokens) { if (token == "x" || token == "y" || token == "0" || token == "1") { output.push_back(token); } else if (token == "(") { op_stack.push(token); } else if (token == ")") { while (op_stack.top() != "(") { output.push_back(op_stack.top()); op_stack.pop(); } op_stack.pop(); } else if (token == "!") { while (!op_stack.empty() && op_stack.top() != "(") { const string& top = op_stack.top(); auto& top_info = op_info[top]; if (top_info.precedence > op_info[token].precedence || (top_info.precedence == op_info[token].precedence && op_info[token].is_left_assoc)) { output.push_back(top); op_stack.pop(); } else break; } op_stack.push(token); } else { auto& curr_info = op_info[token]; while (!op_stack.empty() && op_stack.top() != "(") { const string& top = op_stack.top(); auto& top_info = op_info[top]; if (top_info.precedence > curr_info.precedence || (top_info.precedence == curr_info.precedence && curr_info.is_left_assoc)) { output.push_back(top); op_stack.pop(); } else break; } op_stack.push(token); } } while (!op_stack.empty()) { output.push_back(op_stack.top()); op_stack.pop(); } return output; } int evaluate_rpn(const vector<string>& rpn, int x_bit, int y_bit) { vector<int> stack; for (const string& token : rpn) { if (token == "x") stack.push_back(x_bit); else if (token == "y") stack.push_back(y_bit); else if (token == "0") stack.push_back(0); else if (token == "1") stack.push_back(1); else if (token == "!") { int val = stack.back(); stack.pop_back(); stack.push_back(!val); } else { int b = stack.back(); stack.pop_back(); int a = stack.back(); stack.pop_back(); int res; if (token == "=") res = (a == b); else if (token == "&") res = a & b; else if (token == "|") res = a | b; else if (token == "^") res = a ^ b; else { assert(false); res = 0; } stack.push_back(res); } } return stack[0]; } struct IfStatement { int a, b; array<int,4> truth_table; int r; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; int num_words = 1 << k; vector<uint8_t> x_bits(num_words * (k+1)), y_bits(num_words * (k+1)); for (int x = 0; x < num_words; ++x) { for (int a = 1; a <= k; ++a) { x_bits[x * (k+1) + a] = (x >> (k - a)) & 1; } } for (int y = 0; y < num_words; ++y) { for (int b = 1; b <= k; ++b) { y_bits[y * (k+1) + b] = (y >> (k - b)) & 1; } } vector<IfStatement> stmts; stmts.reserve(n); for (int i = 0; i < n; ++i) { int a, b, r; string expr; cin >> a >> b >> expr >> r; auto tokens = tokenize(expr); auto rpn = shunting_yard(tokens); array<int,4> tt{}; for (int xb = 0; xb < 2; ++xb) for (int yb = 0; yb < 2; ++yb) tt[(xb<<1)|yb] = evaluate_rpn(rpn, xb, yb); stmts.push_back({a, b, tt, r}); } int default_ret; cin >> default_ret; vector<int> f(num_words * num_words, default_ret); for (int x = 0; x < num_words; ++x) { for (int y = 0; y < num_words; ++y) { int idx = x * num_words + y; for (const auto& stmt : stmts) { int x_bit = x_bits[x * (k+1) + stmt.a]; int y_bit = y_bits[y * (k+1) + stmt.b]; int tidx = (x_bit << 1) | y_bit; if (stmt.truth_table[tidx]) { f[idx] = stmt.r; break; } } } } int reflexive = 0; for (int x = 0; x < num_words; ++x) reflexive += f[x * num_words + x]; int symmetric = 0; for (int x = 0; x < num_words; ++x) { for (int y = 0; y < num_words; ++y) { if (f[x * num_words + y] && f[y * num_words + x]) symmetric++; } } vector<bitset<1024>> S(num_words); for (int y = 0; y < num_words; ++y) for (int z = 0; z < num_words; ++z) if (f[y * num_words + z]) S[y].set(z); int transitive = 0; for (int x = 0; x < num_words; ++x) { bitset<1024> not_Sx; for (int z = 0; z < num_words; ++z) not_Sx[z] = !f[x * num_words + z]; for (int y = 0; y < num_words; ++y) { if (f[x * num_words + y]) transitive += (S[y] & not_Sx).count(); } } cout << reflexive << ' ' << symmetric << ' ' << transitive << '\n'; }
failed
101_2_wrong.cpp
#define NDEBUG using namespace std; // intrinstic #include <immintrin.h> #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cfenv> #include <cfloat> #include <chrono> #include <cinttypes> #include <climits> #include <cmath> #include <complex> #include <cstdarg> #include <cstddef> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <initializer_list> #include <iomanip> #include <ios> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <streambuf> #include <string> #include <tr2/dynamic_bitset> #include <tuple> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // utility namespace Nyaan { using ll = long long; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; template <typename T> using V = vector<T>; template <typename T> using VV = vector<vector<T>>; using vi = vector<int>; using vl = vector<long long>; using vd = V<double>; using vs = V<string>; using vvi = vector<vector<int>>; using vvl = vector<vector<long long>>; template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>; template <typename T, typename U> struct P : pair<T, U> { template <typename... Args> constexpr P(Args... args) : pair<T, U>(args...) {} using pair<T, U>::first; using pair<T, U>::second; P &operator+=(const P &r) { first += r.first; second += r.second; return *this; } P &operator-=(const P &r) { first -= r.first; second -= r.second; return *this; } P &operator*=(const P &r) { first *= r.first; second *= r.second; return *this; } template <typename S> P &operator*=(const S &r) { first *= r, second *= r; return *this; } P operator+(const P &r) const { return P(*this) += r; } P operator-(const P &r) const { return P(*this) -= r; } P operator*(const P &r) const { return P(*this) *= r; } template <typename S> P operator*(const S &r) const { return P(*this) *= r; } P operator-() const { return P{-first, -second}; } }; using pl = P<ll, ll>; using pi = P<int, int>; using vp = V<pl>; constexpr int inf = 1001001001; constexpr long long infLL = 4004004004004004004LL; template <typename T> int sz(const T &t) { return t.size(); } template <typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template <typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template <typename T> inline T Max(const vector<T> &v) { return *max_element(begin(v), end(v)); } template <typename T> inline T Min(const vector<T> &v) { return *min_element(begin(v), end(v)); } template <typename T> inline long long Sum(const vector<T> &v) { return accumulate(begin(v), end(v), 0LL); } template <typename T> int lb(const vector<T> &v, const T &a) { return lower_bound(begin(v), end(v), a) - begin(v); } template <typename T> int ub(const vector<T> &v, const T &a) { return upper_bound(begin(v), end(v), a) - begin(v); } constexpr long long TEN(int n) { long long ret = 1, x = 10; for (; n; x *= x, n >>= 1) ret *= (n & 1 ? x : 1); return ret; } template <typename T, typename U> pair<T, U> mkp(const T &t, const U &u) { return make_pair(t, u); } template <typename T> vector<T> mkrui(const vector<T> &v, bool rev = false) { vector<T> ret(v.size() + 1); if (rev) { for (int i = int(v.size()) - 1; i >= 0; i--) ret[i] = v[i] + ret[i + 1]; } else { for (int i = 0; i < int(v.size()); i++) ret[i + 1] = ret[i] + v[i]; } return ret; }; template <typename T> vector<T> mkuni(const vector<T> &v) { vector<T> ret(v); sort(ret.begin(), ret.end()); ret.erase(unique(ret.begin(), ret.end()), ret.end()); return ret; } template <typename F> vector<int> mkord(int N, F f) { vector<int> ord(N); iota(begin(ord), end(ord), 0); sort(begin(ord), end(ord), f); return ord; } template <typename T> vector<int> mkinv(vector<T> &v) { int max_val = *max_element(begin(v), end(v)); vector<int> inv(max_val + 1, -1); for (int i = 0; i < (int)v.size(); i++) inv[v[i]] = i; return inv; } vector<int> mkiota(int n) { vector<int> ret(n); iota(begin(ret), end(ret), 0); return ret; } template <typename T> T mkrev(const T &v) { T w{v}; reverse(begin(w), end(w)); return w; } template <typename T> bool nxp(T &v) { return next_permutation(begin(v), end(v)); } // 返り値の型は入力の T に依存 // i 要素目 : [0, a[i]) template <typename T> vector<vector<T>> product(const vector<T> &a) { vector<vector<T>> ret; vector<T> v; auto dfs = [&](auto rc, int i) -> void { if (i == (int)a.size()) { ret.push_back(v); return; } for (int j = 0; j < a[i]; j++) v.push_back(j), rc(rc, i + 1), v.pop_back(); }; dfs(dfs, 0); return ret; } // F : function(void(T&)), mod を取る操作 // T : 整数型のときはオーバーフローに注意する template <typename T> T Power(T a, long long n, const T &I, const function<void(T &)> &f) { T res = I; for (; n; f(a = a * a), n >>= 1) { if (n & 1) f(res = res * a); } return res; } // T : 整数型のときはオーバーフローに注意する template <typename T> T Power(T a, long long n, const T &I = T{1}) { return Power(a, n, I, function<void(T &)>{[](T &) -> void {}}); } template <typename T> T Rev(const T &v) { T res = v; reverse(begin(res), end(res)); return res; } template <typename T> vector<T> Transpose(const vector<T> &v) { using U = typename T::value_type; if (v.empty()) return {}; int H = v.size(), W = v[0].size(); V<T> res(W, T(H, U{})); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { res[j][i] = v[i][j]; } } return res; } template <typename T> vector<T> Rotate(const vector<T> &v, int clockwise = true) { using U = typename T::value_type; int H = v.size(), W = v[0].size(); V<T> res(W, T(H, U{})); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (clockwise) { res[W - 1 - j][i] = v[i][j]; } else { res[j][H - 1 - i] = v[i][j]; } } } return res; } } // namespace Nyaan // bit operation namespace Nyaan { __attribute__((target("popcnt"))) inline int popcnt(const u64 &a) { return __builtin_popcountll(a); } inline int lsb(const u64 &a) { return a ? __builtin_ctzll(a) : 64; } inline int ctz(const u64 &a) { return a ? __builtin_ctzll(a) : 64; } inline int msb(const u64 &a) { return a ? 63 - __builtin_clzll(a) : -1; } template <typename T> inline int gbit(const T &a, int i) { return (a >> i) & 1; } template <typename T> inline void sbit(T &a, int i, bool b) { if (gbit(a, i) != b) a ^= T(1) << i; } constexpr long long PW(int n) { return 1LL << n; } constexpr long long MSK(int n) { return (1LL << n) - 1; } } // namespace Nyaan // inout namespace Nyaan { template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } istream &operator>>(istream &is, __int128_t &x) { string S; is >> S; x = 0; int flag = 0; for (auto &c : S) { if (c == '-') { flag = true; continue; } x *= 10; x += c - '0'; } if (flag) x = -x; return is; } istream &operator>>(istream &is, __uint128_t &x) { string S; is >> S; x = 0; for (auto &c : S) { x *= 10; x += c - '0'; } return is; } ostream &operator<<(ostream &os, __int128_t x) { if (x == 0) return os << 0; if (x < 0) os << '-', x = -x; string S; while (x) S.push_back('0' + x % 10), x /= 10; reverse(begin(S), end(S)); return os << S; } ostream &operator<<(ostream &os, __uint128_t x) { if (x == 0) return os << 0; string S; while (x) S.push_back('0' + x % 10), x /= 10; reverse(begin(S), end(S)); return os << S; } void in() {} template <typename T, class... U> void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template <typename T, class... U, char sep = ' '> void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; out(u...); } struct IoSetupNya { IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7); } } iosetupnya; } // namespace Nyaan // debug #ifdef NyaanDebug #define trc(...) (void(0)) #endif #ifndef NyaanDebug #define trc(...) (void(0)) #endif #ifndef NyaanLocal #define trc2(...) (void(0)) #endif // macro #define each(x, v) for (auto &&x : v) #define each2(x, y, v) for (auto &&[x, y] : v) #define all(v) (v).begin(), (v).end() #define rep(i, N) for (long long i = 0; i < (long long)(N); i++) #define repr(i, N) for (long long i = (long long)(N)-1; i >= 0; i--) #define rep1(i, N) for (long long i = 1; i <= (long long)(N); i++) #define repr1(i, N) for (long long i = (N); (long long)(i) > 0; i--) #define reg(i, a, b) for (long long i = (a); i < (b); i++) #define regr(i, a, b) for (long long i = (b)-1; i >= (a); i--) #define fi first #define se second #define ini(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define inl(...) \ long long __VA_ARGS__; \ in(__VA_ARGS__) #define ins(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) #define in2(s, t) \ for (int i = 0; i < (int)s.size(); i++) { \ in(s[i], t[i]); \ } #define in3(s, t, u) \ for (int i = 0; i < (int)s.size(); i++) { \ in(s[i], t[i], u[i]); \ } #define in4(s, t, u, v) \ for (int i = 0; i < (int)s.size(); i++) { \ in(s[i], t[i], u[i], v[i]); \ } #define die(...) \ do { \ Nyaan::out(__VA_ARGS__); \ return; \ } while (0) namespace Nyaan { void solve(); } int main() { Nyaan::solve(); } // using namespace Nyaan; using namespace std; // reference : http://www.ivis.co.jp/text/20111102.pdf struct SurrealNumber { using S = SurrealNumber; using i64 = long long; // p / 2^q の形で保持 i64 p, q; SurrealNumber(i64 _p = 0, i64 _q = 0) : p(_p), q(_q) {} friend ostream &operator<<(ostream &os, const SurrealNumber &r) { os << r.p; if (r.q != 0) os << " / " << (i64{1} << r.q); return os; } static S normalize(S s) { if (s.p != 0) { while (s.p % 2 == 0 and s.q != 0) s.p /= 2, s.q--; } else { s.q = 0; } return {s.p, s.q}; } // 加算・減算 friend S operator+(const S &l, const S &r) { i64 cq = max(l.q, r.q); i64 cp = (l.p << (cq - l.q)) + (r.p << (cq - r.q)); return normalize(S{cp, cq}); } friend S operator-(const S &l, const S &r) { i64 cq = max(l.q, r.q); i64 cp = (l.p << (cq - l.q)) - (r.p << (cq - r.q)); return normalize(S{cp, cq}); } S &operator+=(const S &r) { return (*this) = (*this) + r; } S &operator-=(const S &r) { return (*this) = (*this) - r; } S operator-() const { return {-p, q}; } S operator+() const { return {p, q}; } // 大小関係 friend bool operator<(const S &l, const S &r) { return (r - l).p > 0; } friend bool operator<=(const S &l, const S &r) { return (r - l).p >= 0; } friend bool operator>(const S &l, const S &r) { return (l - r).p > 0; } friend bool operator>=(const S &l, const S &r) { return (l - r).p >= 0; } friend bool operator==(const S &l, const S &r) { return (l - r).p == 0; } friend bool operator!=(const S &l, const S &r) { return (l - r).p != 0; } // 左右の子を返す関数 pair<S, S> child() const { if (p == 0) return {-1, 1}; if (q == 0 and p > 0) return {S{p * 2 - 1, 1}, p + 1}; if (q == 0 and p < 0) return {p - 1, S{p * 2 + 1, 1}}; return {(*this) - S{1, q + 1}, (*this) + S{1, q + 1}}; } S larger() const { S root = 0; while ((*this) >= root) root = root.child().second; return root; } S smaller() const { S root = 0; while ((*this) <= root) root = root.child().first; return root; } friend S reduce(const S &l, const S &r) { assert(l < r); S root = 0; while (l >= root or root >= r) { auto [lr, rr] = root.child(); root = (r <= root ? lr : rr); } return root; } }; /** * @brief 超現実数 */ // F : pair(vector<Game>, vector<Game>) を返す関数 template <typename Game, typename F> struct GameSolver { using Games = vector<Game>; using S = pair<SurrealNumber, int>; map<Game, S> mp; F f; GameSolver(const F &_f) : f(_f) {} S zero() const { return S{SurrealNumber(), 0}; } S get(Game g) { if (mp.count(g)) return mp[g]; return mp[g] = _get(g); } private: S _get(Game g) { Games gl, gr; tie(gl, gr) = f(g); if (gl.empty() and gr.empty()) return {0, 0}; vector<S> l, r; for (auto &cg : gl) l.push_back(get(cg)); for (auto &cg : gr) r.push_back(get(cg)); S sl, sr; if (!l.empty()) sl = *max_element(begin(l), end(l)); if (!r.empty()) sr = *min_element(begin(r), end(r)); if (r.empty()) return {sl.fi.larger(), 0}; if (l.empty()) return {sr.fi.smaller(), 0}; assert(sl.fi <= sr.fi); vi buf1, buf2; each(x, l) if (x.fi == sl.fi) buf1.push_back(x.se); each(x, r) if (x.fi == sr.fi) buf2.push_back(x.se); buf1 = mkuni(buf1); buf2 = mkuni(buf2); int nim1 = 0, nim2 = 0; while (nim1 < sz(buf1) and nim1 == buf1[nim1]) nim1++; while (nim2 < sz(buf2) and nim2 == buf2[nim2]) nim2++; trc(g, sl, sr, buf1, buf2); // sl.fi == sr.fi の場合は nim if (sl.fi == sr.fi) return {sl.fi, min(nim1, nim2)}; if (sl.se != 0 or sr.se != 0) { if (sl.se == 1 and sr.se == 0) { if (sz(buf1) >= 2) return {reduce(sl.fi, sr.fi), 0}; return {sl.fi, 0}; } if (sl.se == 0 and sr.se == 1) { if (sz(buf2) >= 2) return {reduce(sl.fi, sr.fi), 0}; return {sr.fi, 0}; } trc(g, sl, sr); // exit(1); } return {reduce(sl.fi, sr.fi), 0}; } }; using Game = array<string, 3>; auto valid = [](const Game &g) { if (g[0][0] != '.' and g[0][0] == g[0][1] and g[0][1] == g[0][2]) return false; if (g[1][0] != '.' and g[1][0] == g[1][1] and g[1][1] == g[1][2]) return false; if (g[2][0] != '.' and g[2][0] == g[2][1] and g[2][1] == g[2][2]) return false; if (g[0][0] != '.' and g[0][0] == g[1][0] and g[1][0] == g[2][0]) return false; if (g[0][1] != '.' and g[0][1] == g[1][1] and g[1][1] == g[2][1]) return false; if (g[0][2] != '.' and g[0][2] == g[1][2] and g[1][2] == g[2][2]) return false; if (g[0][0] != '.' and g[0][0] == g[1][1] and g[1][1] == g[2][2]) return false; if (g[0][2] != '.' and g[0][2] == g[1][1] and g[1][1] == g[2][0]) return false; return true; }; auto f = [](Game g) { vector<Game> L, R; rep(i, 3) rep(j, 3) { if (g[i][j] != '.') continue; g[i][j] = 'x'; if (valid(g)) L.push_back(g); g[i][j] = 'o'; if (valid(g)) R.push_back(g); g[i][j] = '.'; } return mkp(L, R); }; GameSolver<Game, decltype(f)> solver(f); void q() { ini(N); trc(solver.get({{"..x", "xox", "x.."}})); trc(solver.get({{"o.x", "xox", "x.."}})); trc(solver.get({{".ox", "xox", "x.."}})); trc(solver.get({{"oxx", "xox", "x.."}})); trc(solver.get({{".xx", "xox", "x.."}})); /**/ SurrealNumber s = 0; int n = 0; rep(i, N) { Game g; rep(j, 3) in(g[j]); auto [x, y] = solver.get(g); trc2(g, x, y); s += x, n += y; } trc(s, n); if (s > 0 or (s == 0 and n != 0)) { out("Alice"); } else { out("Bob"); } //*/ } void Nyaan::solve() { int t = 1; in(t); while (t--) q(); } /* // 4 以上 2 べき pair<vi, vi> beki(int N) { if (N == 4) { vi p{0, 3, 1, 2}; vi q{0, 1, 2, 3}; return mkp(p, q); } auto [p, q] = beki(N / 2); rep(i, N / 2) { p.push_back(p[i] + N / 2); q.push_back(q[i] + N / 2); } reg(i, N / 4, N / 2) swap(q[i], q[N - 1 - i]); return {p, q}; } pair<vi, vi> f(int N) { if (N % 4 != 0) return {}; if ((N & (N - 1)) == 0) return beki(N); int t = msb(N); auto [p, q] = f(PW(t)); auto [r, s] = f(N - PW(t)); each(x, r) x ^= PW(t); each(x, s) x ^= PW(t); for (int n = N - PW(t), m = 0; n;) { int u = msb(n); rep(i, PW(u) / 2) swap(q[m / 2 + i], s[m + PW(u - 1) + i]); n -= PW(u); m += PW(u); } each(x, r) p.push_back(x); each(x, s) q.push_back(x); return {p, q}; } bool check(vi p, vi q) { int N = sz(p); vi r(N); rep(i, N) r[i] = p[i] ^ q[i]; trc(N); trc(p); trc(q); trc(r); sort(all(r)); rep(i, N) if (r[i] != i) return false; return true; } void test() { rep1(nd4, 100) { int N = nd4 * 4; if ((N & (N - 1)) == 0) { auto [p, q] = beki(N); assert(check(p, q)); } { auto [p, q] = f(N); assert(check(p, q)); } } } void q() { test(); } void Nyaan::solve() { int t = 1; // in(t); while (t--) q(); } */
failed
50_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int mod = 998244353; using COST_T = long long; struct mfmc_edge { int from, to; long long mx; long long cost; // for 1 flow }; void max_flow_min_cost(vector<vector<pair<int, pair<ll, COST_T>>>> g_main, int s, int t, ll k = -1) { int n = g_main.size(); vector<vector<int>> g(n); vector<mfmc_edge> e; for (int u = 0; u < n; ++u) { for (auto p : g_main[u]) { int v = p.first; ll w = p.second.first, c = p.second.second; g[u].push_back(e.size()); e.push_back({u, v, w, c}); // !!! may need to change c to c/w !!! g[v].push_back(e.size()); e.push_back({v, u, 0, -c}); } } const ll inf = 1e18l * 4; vector<COST_T> p(n, inf); p[s] = 0; vector<int> par(n, -1); for (int i = 0; i < n; ++i) { bool change = false; for (int j = 0; j < e.size(); ++j) { auto edge = e[j]; if (edge.mx > 0 && p[edge.from] != inf) { if (p[edge.to] > p[edge.from] + edge.cost) { p[edge.to] = p[edge.from] + edge.cost; par[edge.to] = j; change = true; } } } if (!change) break; } pair<ll, COST_T> ans = {0, 0}; vector<ll> d = p; while (ans.first < k) { if (par[t] == -1) break; int v = t; ll flow = 1, cost = 0; while (v != s) { flow = min(flow, e[par[v]].mx); cost += e[par[v]].cost; v = e[par[v]].from; } v = t; while (v != s) { e[par[v]].mx -= flow; e[par[v] ^ 1].mx += flow; v = e[par[v]].from; } ans.first += flow; ans.second += cost * flow; par.assign(n, -1); d.assign(n, inf); d[s] = 0; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; pq.push({0, s}); while (!pq.empty()) { int v = pq.top().second; ll ds = pq.top().first; pq.pop(); if (ds > d[v]) continue; for (auto i : g[v]) { if (e[i].mx > 0 && d[e[i].to] > d[v] + e[i].cost + p[e[i].from] - p[e[i].to]) { d[e[i].to] = d[v] + e[i].cost + p[e[i].from] - p[e[i].to]; par[e[i].to] = i; pq.push({d[e[i].to], e[i].to}); } } } for (int i = 0; i < n; ++i) { if (d[v] == inf || p[v] == inf) p[v] = inf; else p[v] += d[v]; } cout << -ans.second << " "; } while (ans.first < k) { ++ans.first; cout << "-1 "; } } int sum(int x, int y) { x += y; x -= (x >= mod) * mod; return x; } int main() { cin.tie(0)->sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; vector<int> a(n), b(n); for (auto &i : a) cin >> i; for (auto &i : b) cin >> i; vector<vector<int>> bad(n); { vector<array<int, 2>> A, B; for (int i = 0; i < n; ++i) A.push_back({a[i], i}); for (int i = 0; i < n; ++i) B.push_back({b[i], i}); sort(A.begin(), A.end()); sort(B.begin(), B.end()); vector<int> whoa(n), whob(n); for (int i = 0; i < n; ++i) { whoa[A[i][1]] = i; whob[B[i][1]] = i; } while (m--) { int u, v; cin >> u >> v; --u, --v; u = whoa[u]; v = whob[v]; bad[u].push_back(v); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector<int> ok(n, -1); vector<vector<int>> g0(n), g1(n); { vector<int> x, y; for (int i = n - 1; ~i; --i) { for (auto t : bad[i]) ok[t] = i; x.clear(); y.clear(); int pos = lower_bound(b.begin(), b.end(), mod - a[i]) - b.begin() - 1; for (int t = 0; x.size() < k && n - 1 - t > pos; ++t) { if (ok[n - 1 - t] != i) x.push_back(n - 1 - t); } for (int t = 0; y.size() < k && pos - t >= 0; ++t) { if (ok[t] != i) y.push_back(t); } int ii = 0, jj = 0; while (ii + jj < k && ii + jj < x.size() + y.size()) { if (jj == y.size() || (ii < x.size() && sum(a[i], b[x[ii]]) >= sum(a[i], b[y[jj]]))) { g0[x[ii]].push_back(i); ++ii; } else { g1[y[jj]].push_back(i); ++jj; } } } } { vector<vector<int>> ng0(n), ng1(n); for (int i = n - 1; ~i; --i) { auto &x = g0[i]; auto &y = g1[i]; int ii = 0, jj = 0; while (ii + jj < k && ii + jj < x.size() + y.size()) { if (jj == y.size() || (ii < x.size() && sum(b[i], a[x[ii]]) >= sum(b[i], a[y[jj]]))) { ng0[x[ii]].push_back(i); ++ii; } else { ng1[y[jj]].push_back(i); ++jj; } } } swap(g0, ng0); swap(g1, ng1); } priority_queue<array<int, 2>> pq; vector<queue<int>> all(n); for (int i = 0; i < n; ++i) { auto &x = g0[i]; auto &y = g1[i]; int ii = 0, jj = 0; while (ii + jj < k && ii + jj < x.size() + y.size()) { if (jj == y.size() || (ii < x.size() && sum(a[i], b[x[ii]]) >= sum(a[i], b[y[jj]]))) { all[i].push(x[ii]); ++ii; } else { all[i].push(y[jj]); ++jj; } } } for (int i = 0; i < n; ++i) { if (all[i].size()) pq.push({sum(a[i], b[all[i].front()]), i}); } vector<array<int, 2>> E; while (E.size() < 2 * k * k && pq.size()) { auto [_, i] = pq.top(); pq.pop(); E.push_back({i, all[i].front()}); all[i].pop(); if (all[i].size()) pq.push({sum(a[i], b[all[i].front()]), i}); } vector<int> A, B; for (auto [u, v] : E) { A.push_back(u); B.push_back(v); } sort(A.begin(), A.end()); sort(B.begin(), B.end()); A.resize(unique(A.begin(), A.end()) - A.begin()); B.resize(unique(B.begin(), B.end()) - B.begin()); n = A.size() + B.size(); int s = n, t = n + 1; int N = t + 1; vector<vector<pair<int, pair<ll, COST_T>>>> gm(N); for (int i = 0; i < A.size(); ++i) gm[s].push_back({i, {1, 0}}); for (int i = 0; i < B.size(); ++i) gm[A.size() + i].push_back({t, {1, 0}}); for (auto [u, v] : E) { u = lower_bound(A.begin(), A.end(), u) - A.begin(); v = lower_bound(B.begin(), B.end(), v) - B.begin(); // cout << u << " " << v << " " << sum(a[A[u]], b[B[v]]) << endl; gm[u].push_back({v + (int)A.size(), {1, -sum(a[A[u]], b[B[v]])}}); } max_flow_min_cost(gm, s, t, k); }
failed
76_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back const int M = 20; bool con[M][M]; int col[M]; int cnt[2]; bool cyc; void DFS(int u, const vector<int> &nodes) { cnt[col[u]]++; for (int v : nodes) { if (con[u][v]) { if (col[v] == -1) { col[v] = col[u] ^ 1; DFS(v, nodes); } else if (col[v] == col[u]) { cyc = true; } } } } const int mod = 1e9 + 7; int add(int x, int y) { x += y; return x >= mod ? x - mod : x; } int sub(int x, int y) { x -= y; return x < 0 ? x + mod : x; } int mul(int x, int y) { return (ll)x * y % mod; } void ckadd(int &x, int y) { x = add(x, y); } int pow(int x, int k) { int ans = 1; for (; k; k >>= 1, x = mul(x, x)) if (k & 1) ans = mul(ans, x); return ans; } int inv(int x) { return pow(x, mod - 2); } const int N = M + 2; int F[N], I[N], cnk[N][N]; int binom(int n, int k) { return mul(F[n], mul(I[k], I[n - k])); } int s1[N], s2[N]; int BigBinom(int n, int k) { int ans = I[k]; for (int i = 0; i < k; i++) { ans = mul(ans, n - i); } return ans; } int Stirling(int n, int k) { if (k > n) return 0; return BigBinom(n - 1, k - 1); /*int ans=0; for(int j=0;j<=k;j++){ int now=mul(cnk[k][j],pow(j,n)); if((k-j)%2==1)now=sub(0,now); ckadd(ans,now); } ans=mul(ans,I[k]); return ans;*/ } void Init() { F[0] = 1; for (int i = 1; i < N; i++) F[i] = mul(F[i - 1], i); for (int i = 0; i < N; i++) I[i] = inv(F[i]); for (int i = 0; i < N; i++) { for (int j = 0; j <= i; j++) { cnk[i][j] = binom(i, j); } } } void Calc(int n1, int n2) { for (int i = 1; i < N; i++) { s1[i] = Stirling(n1, i); s2[i] = Stirling(n2, i); // printf("s1[%i]=%i\n",i,s1[i]); // printf("s2[%i]=%i\n",i,s2[i]); } } void Run() { int n1, n2, m, k; scanf("%i %i %i %i", &n1, &n2, &m, &k); Calc(n1, n2); for (int i = 0; i < M; i++) for (int j = 0; j < M; j++) con[i][j] = false; map<int, int> idx; int tsz = 0, needBoth = 0; vector<pair<int, int>> edges; vector<bool> selfLoop(m + 1, false); for (int i = 1; i <= k; i++) { int a, b; scanf("%i %i", &a, &b); edges.pb({a, b}); if (a == b) selfLoop[a] = true; } for (int i = 1; i <= m; i++) if (selfLoop[i]) needBoth++; for (int i = 0; i < k; i++) { int a = edges[i].first, b = edges[i].second; if (!selfLoop[a] && !idx.count(a)) idx[a] = tsz++; if (!selfLoop[b] && !idx.count(b)) idx[b] = tsz++; if (!selfLoop[a] && !selfLoop[b]) { a = idx[a]; b = idx[b]; con[a][b] = con[b][a] = true; } } int isolated = m - tsz - needBoth; m = tsz; // printf("isolated: %i\n",isolated); // printf("needBoth: %i\n",needBoth); // for(int i=0;i<m;i++){ // for(int j=0;j<m;j++)printf("%i",con[i][j]); // printf("\n"); // } int ans = 0; for (int mask = 0; mask < (1 << m); mask++) { int both = 0; vector<int> alone; for (int i = 0; i < m; i++) { col[i] = -1; if (mask >> i & 1) alone.pb(i); else both++; } cyc = false; vector<pair<int, int>> cmps; for (int i : alone) if (col[i] == -1) { cnt[0] = cnt[1] = 0; col[i] = 0; DFS(i, alone); cmps.pb({cnt[0], cnt[1]}); } if (cyc) continue; vector<int> dp(alone.size() + 1, 0); dp[0] = 1; for (auto p : cmps) { for (int i = alone.size(); i >= 0; i--) { int ans = 0; if (i >= p.first) ckadd(ans, dp[i - p.first]); if (i >= p.second) ckadd(ans, dp[i - p.second]); dp[i] = ans; } } // printf("mask: %i\n",mask); // printf("alone: ");for(int i:alone)printf("%i ",i);printf("\n"); // for(auto p:cmps)printf("(%i or %i)\n",p.first,p.second); // for(int i=0;i<=alone.size();i++)printf("%i ",dp[i]);printf("\n"); for (int i = 0; i <= alone.size(); i++) { for (int j = 0; j <= isolated; j++) { int left = i + both + j + needBoth; for (int z = 0; z <= isolated; z++) { int right = alone.size() - i + both + z + needBoth; int ways = mul(cnk[isolated][j], cnk[isolated][z]); ways = mul(ways, dp[i]); // printf("left: %i, right: %i, ways: %i\n",left,right,ways); ckadd(ans, mul(ways, mul(s1[left], s2[right]))); // printf("ans: %i\n",ans); } } } } printf("%i\n", ans); } int main() { Init(); int t; scanf("%i", &t); while (t--) { Run(); } return 0; }
failed
30_3_wrong.cpp
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") /* take min counterexample x000y: counterexample ==> x0y: counterexample (run length) = 1 or 2 not substring of (0011)^INF ==> fold to get shorter counterexample 0011001001100110110 ==> 0110011001 >>>>>>| |<<<<<<<<| |>>> */ constexpr int INF = 1001001001; int N, M; vector<int> C; vector<int> A, B; vector<vector<int>> graph; // from "0011"[k] vector<int> vis[4], dp[4]; bool dfs(int k, int u) { if (vis[k][u] == 1) return false; if (vis[k][u] == 2) return true; vis[k][u] = 1; if (k / 2 == C[u]) { dp[k][u] = 1; const int kk = (k + 1) % 4; for (const int v : graph[u]) { if (!dfs(kk, v)) return false; chmax(dp[k][u], 1 + dp[kk][v]); } } else { dp[k][u] = 1; } vis[k][u] = 2; return true; } int main() { for (; ~scanf("%d%d", &N, &M);) { C.resize(N); for (int u = 0; u < N; ++u) { scanf("%d", &C[u]); } A.resize(M); B.resize(M); for (int i = 0; i < M; ++i) { scanf("%d%d", &A[i], &B[i]); --A[i]; --B[i]; } graph.assign(N, {}); for (int i = 0; i < M; ++i) { graph[A[i]].push_back(B[i]); graph[B[i]].push_back(A[i]); } for (int k = 0; k < 4; ++k) { vis[k].assign(N, 0); dp[k].assign(N, 0); } int ans = INF; for (int k = 0; k < 4; ++k) { int mx = 0; for (int u = 0; u < N; ++u) { if (!dfs(k, u)) goto failed; chmax(mx, dp[k][u]); } // cerr<<"dp["<<k<<"] = "<<dp[k]<<endl; chmin(ans, mx); } failed: {} if (ans < INF) { printf("%d\n", ans); } else { puts("infinity"); } } return 0; }
failed
58_1_wrong.cpp
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; const int MAXN = 2005, mod = 998244353; char buf[1 << 24], *S, *T; char gc() { return S == T && (T = (S = buf) + fread(buf, 1, 1 << 24, stdin)) == buf ? 0 : *S++; } int read() { char ch; while ((ch = getchar()) <= ' ') ; int x = ch - '0'; while ((ch = getchar()) > ' ') x = 10 * x + ch - '0'; return x; } int dp[MAXN][MAXN], ans[MAXN][MAXN]; void add(int &x, int y) { if ((x += y) >= mod) x -= mod; } int main() { dp[0][0] = 1; for (int _ = 1; _ <= 2000; _++) { for (int i = 1; i <= 2000; i++) { #pragma GCC unroll(8) for (int j = _; j <= 2000 - _; j++) add(dp[i][j], dp[i - 1][j - _]); #pragma GCC unroll(8) for (int j = _; j <= 2000; j++) { add(dp[i][j - _], dp[i - 1][j - _]); add(ans[i][j], dp[i - 1][j - _]); } } } int q = read(); while (q--) { int n = read(), m = read(); printf("%d\n", ans[n][m]); } }
failed
79_1_wrong.cpp
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; using ldb = long double; struct pt { ldb x, y; pt operator+(const pt &o) const { return {x + o.x, y + o.y}; }; pt operator-(const pt &o) const { return {x - o.x, y - o.y}; }; pt operator*(const ldb o) const { return {x * o, y * o}; }; ldb operator*(const pt &o) const { return o.x * x + o.y * y; } ldb operator^(const pt &o) const { return x * o.y - y * o.x; } }; pt rotate(const pt &a, const pt &v2) { return {v2 * a, v2 ^ a}; } struct seg { pt a, b; bool operator<(const seg &o) const { if (a.y != o.a.y) a.y < o.a.y; return b.y < o.b.y; } pt at(ldb y) const { return (b * (y - a.y) + a * (b.y - y)) * (1 / (b.y - a.y)); } }; struct poly { ldb t, a0, a1, a2, a3; bool operator<(const poly &o) const { return t < o.t; } }; struct item { ldb t, v1, v2; }; int cnt = 0; int t; const ldb eps = 1e-7; void solve() { pt a0, v; ldb z0, z1, z2; cin >> a0.x >> a0.y >> z0 >> v.x >> v.y; int n1, n2; cin >> n1 >> z1; vector<pt> c1(n1); for (int i = 0; i < n1; ++i) cin >> c1[i].x >> c1[i].y; cin >> n2 >> z2; vector<pt> c2(n2); for (int i = 0; i < n2; ++i) cin >> c2[i].x >> c2[i].y; ldb alp1 = z0 / (z0 - z1); ldb alp2 = z0 / (z0 - z2); v = v * (alp1 - alp2); ldb beta = v * v; v = v * (1 / beta); // v = rotate(v, v); for (auto &u : c1) u = rotate((u - a0) * alp1, v); for (auto &u : c2) u = rotate((u - a0) * alp2, v); // for (const auto& u : c1) cout << u.x << " " << u.y << '\n'; // for (const auto& u : c2) cout << u.x << " " << u.y << '\n'; vector<seg> s1, s2, s3, s4; for (int i = 0; i < n1; ++i) { auto a = c1[i], b = c1[i == n1 - 1 ? 0 : i + 1]; if (abs(a.y - b.y) < eps) continue; if (a.y > b.y) s1.push_back({b, a}); else s2.push_back({a, b}); } for (int i = 0; i < n2; ++i) { auto a = c2[i], b = c2[i == n2 - 1 ? 0 : i + 1]; if (abs(a.y - b.y) < eps) continue; if (a.y > b.y) s3.push_back({b, a}); else s4.push_back({a, b}); } sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); sort(s3.begin(), s3.end()); sort(s4.begin(), s4.end()); vector<item> q; auto update = [&](const seg &s, const seg &t, ldb z0, ldb z1, int f) -> void { if (abs(z0 - z1) < eps) return; auto a = s.at(z0), b = s.at(z1); auto c = t.at(z0), d = t.at(z1); ldb t1 = c.x - a.x, t2 = d.x - b.x; if (t1 < t2) swap(t1, t2); ldb val = z1 - z0; if (abs(t1 - t2) > eps) { q.push_back({t1, 0, f * val / (t2 - t1)}); q.push_back({t2, 0, -f * val / (t2 - t1)}); } else { q.push_back({t2, f * val, 0}); } }; int cc = 0; for (const auto &s : {s3, s4}) { for (const auto &t : {s1, s2}) { cc++; int f = cc == 1 || cc == 4 ? -1 : 1; ldb lst_z = max(s[0].a.y, t[0].a.y); for (int i = 0, j = 0; i < s.size() && j < t.size();) { if (s[i].b.y < t[j].a.y) { i++; continue; } if (t[j].b.y < s[i].a.y) { j++; continue; } ldb nz = min(s[i].b.y, t[j].b.y); update(s[i], t[j], lst_z, nz, f); lst_z = nz; if (nz == t[j].b.y) j++; else i++; } } } sort(q.begin(), q.end(), [&](const item &a, const item &b) { return a.t < b.t; }); vector<poly> ans; ldb v0 = 0, v1 = 0, s = 0, ss = 0; ldb lt = -1e50; ans.push_back({lt, s, v0, v1}); for (int i = 0; i < q.size(); ++i) { auto t = q[i].t; auto dt = t - lt; ss += s * dt + v0 * dt * dt / 2 + v1 * dt * dt * dt / 6; s += v0 * dt + v1 * dt * dt / 2; v0 += v1 * dt; v0 += q[i].v1, v1 += q[i].v2; lt = t; ans.push_back({q[i].t, ss, s, v0, v1}); } auto query = [&](ldb t) -> ldb { auto [lt, a0, a1, a2, a3] = *(--upper_bound(ans.begin(), ans.end(), poly{t, 0, 0, 0})); t -= lt; return a0 + a1 * t + a2 * t * t / 2 + a3 * t * t * t / 6; }; auto query2 = [&](ldb t) -> ldb { auto [lt, a0, a1, a2, a3] = *(--upper_bound(ans.begin(), ans.end(), poly{t, 0, 0, 0})); t -= lt; // cout << a0 << " " << a1 << a2 << a3 << '\n'; return a1 + a2 * t + a3 * t * t / 2; }; // for (const auto [t, a0, a1, a2, a3] : ans) { // cout << t << " : " << a0 << " " << a1 << " " << a2 / 2 << " " << a3 / 6 // << '\n'; // } int cq; cin >> cq; for (int i = 0; i < cq; ++i) { ldb t1, t2; cin >> t1 >> t2; cnt++; // if (cnt == 10156) { // cout << a0.x << " " << a0.y << " " << z0; // cnt++, cnt--; // } // t2 *= beta, t1 *= beta; ldb result = t1 == t2 ? query2(t1) : (query(t2) - query(t1)) / (t2 - t1); // cout << ans << '\n'; // if (!(-1e-6 <= result & result <= 1e50)) { // printf("%.8d\n", cnt); // } printf("%.8LF\n", max(result * beta, (ldb)0)); } } int main() { // freopen("m.in", "r", stdin); cin.tie(0), cout.tie(0), ios::sync_with_stdio(0); cin >> t; while (t--) solve(); }
failed
12_1_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; void sol() { int n, m; std::cin >> n >> m; std::vector<int> a(n); std::vector<int> b(m); bool ok = false;; if (n == 1 && m == 1) { std::cout << "Yes\n"; std::cout << "1\n"; return; } if (n < m) { std::iota(a.begin(), a.end(), 1); } else { ok = true;; std::iota(b.begin(), b.end(), 1); } int p = n * m; if (ok) { for (int i = 0; i < n; i++) { a[i] = i * m + 1; } std::set<int> s; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s.find(a[i] * b[j] % p) != s.end()) { std::cout << "No" << "\n"; return; } s.insert(a[i] * b[j] % p); } } std::cout << "Yes\n"; for (int i = 0; i < n; i++) { std::cout << a[i] << " \n"[i == n - 1]; } for (int i = 0; i < m; i++) { std::cout << b[i] << " \n"[i == m - 1]; } } else { for (int i = 0; i < m; i++) { b[i] = i * n + 1; } std::set<int> s; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s.find(a[i] * b[j] % p) != s.end()) { std::cout << "No" << "\n"; return; } s.insert(a[i] * b[j] % p); } } std::cout << "Yes\n"; for (int i = 0; i < n; i++) { std::cout << a[i] << " \n"[i == n - 1]; } for (int i = 0; i < m; i++) { std::cout << b[i] << " \n"[i == m - 1]; } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t--) { sol(); } return 0; }
failed
87_1_wrong.cpp
#include <bits/stdc++.h> template <class T> inline int qlog(T a) { double x = a; return ((*(long long*) & x) >> 52 & 2047) - 1023; } #define fopen LilyWhite void fopen(const char *s) { static char filename[32]; sprintf(filename, "%s.in", s); freopen(filename, "r", stdin); sprintf(filename, "%s.out", s); freopen(filename, "w", stdout); } typedef unsigned int u32; typedef long long ll; typedef unsigned long long u64; #define Please return #define AC 0 #define cin Mizuhashi #define cout Parsee #define endl '\n' class Reader{ private: static const int BUF_SIZE = 1 << 22; char BUF_R[BUF_SIZE], *csy1, *csy2; #ifndef _LOCAL_RUNNING #define GC (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1 ++) #else char cur; #define GC (cur = getchar()) #endif #define IL inline public: IL bool eof() { #ifndef _LOCAL_RUNNING return csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2); #else return cur == EOF; #endif } template <class Ty> IL Reader& operator >> (Ty &t) { int u = 0; char c = GC; for (t = 0; c < 48 || c > 57; c = GC) if (c == EOF) break; else if (c == '-') u = 1; for ( ; c > 47 && c < 58; c = GC) t = t * 10 + (c ^ 48); t = u ? -t : t; return *this; } IL Reader& operator >> (double &t) { int tmp, u = 0; char c = GC; for (tmp = 0; c < 48 || c > 57; c = GC) if (c == EOF) break; else if (c == '-') u = 1; for ( ; c > 47 && c < 58; c = GC) tmp = tmp * 10 + (c ^ 48); t = (tmp = u ? -tmp : tmp); if (c == '.') { double x = 1; for (c = GC; c > 47 && c < 58; c = GC) t += (x /= 10) * (c ^ 48); } return *this; } IL Reader& operator >> (char *s) { char c = GC; for (*s = 0; c < 33; c = GC) if (c == EOF) return *this; for ( ; c > 32; c = GC) *s ++ = c; *s = 0; return *this; } IL Reader& operator >> (char &c) { for (c = GC; c < 33; c = GC) if (c == EOF) return *this; return *this; } }cin; class Writer{ private: static const int BUF_SIZE = 1 << 22; char BUF_W[BUF_SIZE], *csy; #define IL inline inline void WC(const char c) { if (csy - BUF_W == BUF_SIZE) fwrite(BUF_W, 1, BUF_SIZE, stdout), csy = BUF_W; *csy ++ = c; } public: Writer() : csy(BUF_W) {} ~ Writer() {fwrite(BUF_W, 1, csy - BUF_W, stdout);} IL void flush() {fwrite(BUF_W, 1, csy - BUF_W, stdout); csy = BUF_W;} template <class Ty> IL Writer& operator << (Ty x) { static int sta[32], top; if (x < 0) { WC('-'); do sta[top ++] = - (x % 10); while (x /= 10); }else do sta[top ++] = x % 10; while (x /= 10); while (top) WC(sta[-- top] ^ 48); return *this; } IL Writer& operator << (const char &c) {WC(c); return *this;} IL Writer& operator << (const char *s) {while (*s) WC(*s ++); return *this;} }cout; using namespace std; typedef pair <int, int> pii; typedef vector <ll> vi; const int MAX_N = 1000000 + 5; const int MAX_M = 1 << 21; const int INF32 = 0x3f3f3f3f; const ll INF64 = 1e18; int N, M, b[MAX_N], h[MAX_N][3]; vi a[MAX_N], sum[MAX_N]; vector <pii> sec[MAX_N]; void solve() { cin >> M >> N; for (int i = 1; i <= M; i ++) cin >> b[i]; sum[0].resize(M + 1); for (int i = 1; i <= N; i ++) { a[i].resize(M + 1); sum[i].resize(M + 1); for (int j = 1; j <= M; j ++) { cin >> a[i][j]; sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + a[i][j]; } } for (int i = 1; i <= M; i ++) { if (b[i] && a[1][i]) { cout << "-1\n"; return ; } } for (int i = 1; i <= N; i ++) { char flag = 1; for (int j = 1; j <= M; j ++) { if (!a[i][j]) { flag = 0; break; } } if (flag) { cout << "-1\n"; return ; } } map <pii, int> f; for (int i = 1; i <= M; i ++) { if (!a[N][i]) f[pair(i, i)] = sum[N][i] - sum[N][i - 1]; } for (int i = N - 1; i > 0; i --) { map <pii, int> g; vi pre(M + 1), suf(M + 2), cnt0(M + 1); for (auto &x : pre) x = -1; for (auto &x : suf) x = M + 1; for (int j = 1; j <= M; j ++) { pre[j] = a[i][j] ? pre[j - 1] : j; cnt0[j] = cnt0[j - 1] + (!a[i][j]); } for (int j = M; j > 0; j --) { suf[j] = a[i][j] ? suf[j + 1] : j; } auto upd = [&](const pii &k, int val) -> void { auto it = g.find(k); if (it == g.end()) g[k] = val; else g[k] = min(it->second, val); }; for (auto [seg, val] : f) { auto [l, r] = seg; if (cnt0[r] - cnt0[l - 1]) { upd(seg, val); }else { int ll = pre[l - 1], rr = suf[r + 1]; if (ll > 0) upd(pair(ll, r), val + sum[i][l - 1] - sum[i][ll - 1]); if (rr <= N) upd(pair(l, rr), val + sum[i][rr] - sum[i][r]); } } swap(f, g); } for (int i = 1; i <= M; i ++) sec[i].clear(); for (auto [seg, val] : f) sec[seg.second].emplace_back(seg.first, val); h[0][1] = h[0][2] = INF32; for (int i = 1; i <= M; i ++) { if (b[i]) { h[i][0] = INF32; h[i][1] = h[i - 1][1]; h[i][2] = min(h[i - 1][0], min(h[i - 1][1], h[i - 1][2])); }else { h[i][0] = min(h[i - 1][0], h[i - 1][1]); h[i][1] = h[i - 1][1] + a[1][i]; h[i][2] = min(h[i - 1][0], min(h[i - 1][1], h[i - 1][2])) + a[1][i]; } for (auto [l, val] : sec[i]) h[i][1] = min(h[i][1], min(h[l - 1][0], h[l - 1][2]) + val); } cout << min(h[M][0], h[M][1]) << '\n'; } int main() { int T = 1; cin >> T; while (T --) solve(); return 0; }
failed
81_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> g[maxn], vec; int col[maxn]; void dfs(int u, int fu) { for (int v : g[u]) if (v != fu) col[v] = col[u] ^ 1, dfs(v, u); } void work() { int n; cin >> n; for (int i = 1; i <= (n << 1); i++) col[i] = 0, g[i].clear(); vec.clear(); for (int i = 1; i < (n << 1); i++) { int u, v; cin >> u >> v; g[u].emplace_back(v); g[v].emplace_back(u); } dfs(1, 0); int cnt = 0; for (int i = 1; i <= (n << 1); i++) cnt += col[i]; int d = abs(cnt - n); for (int i = 1; i <= (n << 1); i++) if (g[i].size() == 1 && ((d < 0) ^ col[i])) vec.emplace_back(i); for (int i = 0; i < d; i++) col[vec[i]] ^= 1; for (int i = 1; i <= (n << 1); i++) cout << col[i] << " "; cout << "\n"; } int main() { ios::sync_with_stdio(false), cin.tie(0); int T; cin >> T; while (T--) work(); return 0; }
failed
20_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; constexpr int LOG = 20; using ll = long long; constexpr ll INF = 1e10; struct node { int l{-1}; int r{-1}; int sz{1}; array<array<ll, 2>, 2> val; }; void chmin(auto &x, auto y) { x = min(x, y); } array<array<ll, 2>, 2> merge(const array<array<ll, 2>, 2> &lhs, const array<array<ll, 2>, 2> &rhs) { array<array<ll, 2>, 2> res; for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) res[i][j] = INF; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { chmin(res[i][k], rhs[i][j] + lhs[j][k]); } } } return res; } vector<node> all; int sz(int v) { return ~v ? all[v].sz : 0; } ll mn(array<array<ll, 2>, 2> cur) { ll res = INF; for (auto i : cur) for (auto j : i) res = min(res, j); return res; } void relax(int v) { if (!~all[v].l) all[v].val = all[all[v].r].val; else if (!~all[v].r) all[v].val = all[all[v].l].val; else if (!mn(all[all[v].l].val)) all[v].val = all[all[v].r].val; else if (!mn(all[all[v].r].val)) all[v].val = all[all[v].l].val; else all[v].val = merge(all[all[v].l].val, all[all[v].r].val); all[v].sz = sz(all[v].l) + sz(all[v].r); } int create(node v) { all.emplace_back(v); return all.size() - 1; } int upd(int v, int l, int r, int k, int w) { if (l + 1 == r) { int v = create({}); all[v].val[0][0] = w; all[v].val[0][1] = w / 2; all[v].val[1][0] = w; all[v].val[1][1] = INF; all[v].sz = !!w; return v; } int m = (r + l) / 2; int nd = create(~v ? all[v] : node{}); if (k < m) all[nd].l = upd(all[nd].l, l, m, k, w); else all[nd].r = upd(all[nd].r, m, r, k, w); relax(nd); return nd; } int merge(int l, int r, int t1, int t2) { if (l + 1 == r) return ~t1 ? t1 : t2; if (!~t1) return t2; if (!~t2) return t1; int v = create({}); int m = (r + l) / 2; all[v].l = merge(l, m, all[t1].l, all[t2].l); all[v].r = merge(m, r, all[t1].r, all[t2].r); relax(v); return v; } int re; void find(int v, int l, int r, int lq, int rq, int h, array<array<ll, 2>, 2> &cur) { if (!sz(v)) return; if (l >= rq || lq >= r) return; int m = (r + l) / 2; if (l >= lq && r <= rq) { auto mr = merge(all[v].val, cur); ll it = mn(mr); if (it < h) { cur = mr; re += sz(v); return; } int old = re; find(all[v].r, m, r, lq, rq, h, cur); if (re - old == sz(all[v].r)) find(all[v].l, l, m, lq, rq, h, cur); return; } find(all[v].r, m, r, lq, rq, h, cur); find(all[v].l, l, m, lq, rq, h, cur); } int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; vector<vector<int>> g(n); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; --u, --v; g[u].push_back(v); g[v].push_back(u); } vector<int> d(n); vector<vector<int>> up(LOG, vector<int>(n)); auto dfs = [&](auto &&dfs, int v, int p) -> void { for (auto t : g[v]) { if (t == p) continue; d[t] = d[v] + 1; up[0][t] = v; dfs(dfs, t, v); } }; dfs(dfs, 0, -1); for (int i = 1; i < LOG; ++i) { for (int v = 0; v < n; ++v) { up[i][v] = up[i - 1][up[i - 1][v]]; } } auto lca = [&](int u, int v) { if (d[u] < d[v]) swap(u, v); int k = d[u] - d[v]; for (int i = 0; i < LOG; ++i) { if ((1 << i) & k) u = up[i][u]; } if (u == v) return u; for (int i = LOG - 1; ~i; --i) { if (up[i][u] != up[i][v]) { u = up[i][u]; v = up[i][v]; } } return up[0][u]; }; vector<vector<array<int, 2>>> Q(n); vector<vector<array<int, 2>>> ask(n); vector<int> res(q, -1); for (int i = 0; i < q; ++i) { char c; cin >> c; if (c == 'A') { int u, v, w; cin >> u >> v >> w; --u, --v; auto lc = lca(u, v); w *= 2; Q[u].push_back({i, w}); Q[v].push_back({i, w}); Q[lc].push_back({i, 0}); } else { int v, h; cin >> v >> h; --v; h *= 2; res[i] = 0; ask[v].push_back({i, h}); } } auto solve = [&](auto &&solve, int v, int p) -> int { int nd = -1; for (auto t : g[v]) { if (t == p) continue; int it = solve(solve, t, v); nd = merge(0, q, nd, it); } for (auto [i, x] : Q[v]) { if (x) nd = upd(nd, 0, q, i, x); } for (auto [i, h] : ask[v]) { array<array<ll, 2>, 2> cur{}; re = 0; find(nd, 0, q, 0, i, h, cur); res[i] = re; } for (auto [i, x] : Q[v]) { if (!x) nd = upd(nd, 0, q, i, 0); } return nd; }; solve(solve, 0, -1); for (auto x : res) if (~x) cout << x << "\n"; }
failed
37_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define _rep(i_, a_, b_) for (int i_ = (a_); i_ <= (b_); ++i_) #define mid ((L + R) >> 1) #define multiCase() \ int testCnt = in(); \ _rep(curCase, 1, testCnt) #ifdef ONLINE_JUDGE #define debug(...) 0 #else #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #endif using ll = long long; using pii = pair<int, int>; int in(void) { int x; scanf("%d", &x); return x; } ll inl(void) { ll x; scanf("%lld", &x); return x; } void out(int x) { printf("%d ", x); } void outln(int x) { printf("%d\n", x); } void out(ll x) { printf("%lld ", x); } void outln(ll x) { printf("%lld\n", x); } template <typename T> void chkmax(T &a, const T &b) { a = max(a, b); } template <typename T> void chkmin(T &a, const T &b) { a = min(a, b); } int main() { multiCase() { int n = in(); if ((n & (n - 1)) || n <= 2) puts("No"); else { puts("Yes"); vector<int> f = {0, 2, 3, 1}; while (f.size() != n) { vector<int> g = f; for (auto &u : g) u += f.size(); vector<int> h(f.size() * 2); _rep(i, 0, f.size() / 2 - 1) h[i] = f[i]; _rep(i, 0, f.size() / 2 - 1) h[i + f.size() / 2] = g[i]; _rep(i, f.size() / 2, f.size() - 1) h[i + f.size() / 2] = f[i]; _rep(i, f.size() / 2, f.size() - 1) h[i + f.size()] = g[i]; f = h; } _rep(i, 0, n - 1) out(i); puts(""); _rep(i, 0, n - 1) out(f[i]); puts(""); } } // int n = in(); // vector<int> p(n), q(n); // _rep(i,0,n - 1) p[i] = i, q[i] = in(); // do { // vector<int> r(n); // _rep(i,0,n - 1) r[i] = p[i] ^ q[i]; // sort(r.begin(), r.end()); // if(r == p) { // for(auto &u : q) out(u); puts(""); // return 0; // } // } while(next_permutation(q.begin(), q.end())); return 0; }
failed
56_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define ll long double #define pll pair<ll, ll> #define x first #define y second const int N = 4e5 + 10; int n; pll p[N], v[N]; ll sumx, sumy, S; ll ans; ll cross(pll a, pll b) { return a.x * b.y - a.y * b.x; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { cin >> p[i].x >> p[i].y; sumx += p[i].x; sumy += p[i].y; } if (n == 3) { printf("1\n"); return 0; } for (int i = 2; i < n; ++i) { pll v1 = make_pair(p[i].x - p[1].x, p[i].y - p[1].y); pll v2 = make_pair(p[i + 1].x - p[1].x, p[i + 1].y - p[1].y); S += fabs(cross(v1, v2)); } for (int i = 1; i <= n; ++i) { int nex = i + 1; if (nex == n + 1) nex = 1; v[i] = make_pair(p[nex].x - p[i].x, p[nex].y - p[i].y); pll sumv = make_pair(sumx - p[i].x - p[nex].x - p[i].x * (n - 2), sumy - p[i].y - p[nex].y - p[i].y * (n - 2)); ans += fabs(cross(v[i], sumv)) / S; } for (int i = 1; i < n; ++i) { ans -= fabs(cross(v[i], v[i + 1])) / S; } ans -= fabs(cross(v[n], v[1])) / S; printf("%.10Lf", ans); return 0; }
failed
67_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &a) { #define gc getchar() char c; a = 0; int f = 1; while (!isdigit(c = gc)) if (c == '-') f = -1; do a = a * 10 + c - '0'; while (isdigit(c = gc)); a *= f; } template <typename T> void write(T a) { if (a < 0) putchar('-'), a = -a; if (a >= 10) write(a / 10); putchar('0' + a % 10); } char GC() { char c = getchar(); while (c <= 32) c = getchar(); return c; } template <typename T> void chmin(T &x, T y) { if (x > y) x = y; } template <typename T> void chmax(T &x, T y) { if (x < y) x = y; } typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<int, int> PII; typedef pair<ll, int> PLI; typedef __int128 lll; mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); int n, m; int a[200010]; int b[200010][2]; int ufa[200010 * 3], sz[200010 * 3], sum[200010 * 3]; int fl[200010 * 3]; int gf(int x) { return ufa[x] == x ? x : ufa[x] = gf(ufa[x]); } void merge(int x, int y) { if ((x = gf(x)) == (y = gf(y))) return; if (sz[x] > sz[y]) swap(x, y); ufa[x] = y; sz[y] += sz[x]; sum[y] += sum[x]; } int st[200010]; int get_st(int x) { if (fl[gf(x)]) return 0; else if (fl[gf(x + m)]) return 1; else if (fl[gf(x + m + m)]) return 2; else return -1; } signed main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { char c; cin >> c; if (c == 'R') a[i] = 0; else if (c == 'G') a[i] = 1; else a[i] = 2; } for (int i = 1; i <= m * 3; ++i) { ufa[i] = i; sz[i] = 1; sum[i] = (i - 1) / m; fl[i] = 0; } for (int i = 1; i <= m; ++i) { int k; read(k); for (int j = 1; j <= k; ++j) { int x; read(x); if (!b[x][0]) b[x][0] = i; else b[x][1] = i; } } for (int i = 1; i <= n; ++i) if (b[i][1]) { if (a[i] == 0) { merge(b[i][0], b[i][1]); merge(b[i][0] + m, b[i][1] + m); merge(b[i][0] + m * 2, b[i][1] + m * 2); } else if (a[i] == 1) { merge(b[i][0], b[i][1] + m * 2); merge(b[i][0] + m, b[i][1]); merge(b[i][0] + m * 2, b[i][1] + m); } else { merge(b[i][0], b[i][1] + m); merge(b[i][0] + m, b[i][1] + m * 2); merge(b[i][0] + m * 2, b[i][1]); } } for (int i = 1; i <= m; ++i) { if (gf(i) == gf(i + m) || gf(i + m) == gf(i + m + m) || gf(i + m + m) == gf(i)) { puts("impossible"); return 0; } st[i] = -1; } int ans = 0; for (int i = 1; i <= n; ++i) if (b[i][0] && !b[i][1]) { int ST = get_st(b[i][0]); // printf("- %d\n",i); if (a[i] == 0) { if (ST == -1) { if (!fl[gf(b[i][0])]) { fl[gf(b[i][0])] = 1; ans += sum[gf(b[i][0])]; } } else if (st[b[i][0]] != 0) { puts("impossible"); return 0; } } else if (a[i] == 1) { if (ST == -1) { if (!fl[gf(b[i][0] + m)]) { fl[gf(b[i][0] + m)] = 1; ans += sum[gf(b[i][0] + m)]; } } else if (st[b[i][0]] != 1) { puts("impossible"); return 0; } } else { if (ST == -1) { st[b[i][0]] = 2; if (!fl[gf(b[i][0] + m + m)]) { fl[gf(b[i][0] + m + m)] = 1; ans += sum[gf(b[i][0] + m + m)]; } } else if (st[b[i][0]] != 2) { puts("impossible"); return 0; } } } // return 0; for (int i = 1; i <= m; ++i) { int x = fl[gf(i)], y = fl[gf(i + m)], z = fl[gf(i + m + m)]; int xs = sum[gf(i)], ys = sum[gf(i + m)], zs = sum[gf(i + m + m)]; if (x + y + z > 1) { puts("impossible"); return 0; } else if (x + y + z == 0) { if (xs <= ys && xs <= zs) { ans += xs; fl[gf(i)] = 1; } else if (ys <= zs) { ans += ys; fl[gf(i + m)] = 1; } else { ans += zs; fl[gf(i + m + m)] = 1; } } } printf("%d\n", ans); return 0; }
failed
116_1_wrong.cpp
#include <bitset> #include <cstdio> #define ll long long using namespace std; const int B = 5000; const int _N = 5e5 + B + 9; const int D = _N / B + 1; int N, M; int Read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x; } int A[_N]; int dN; bitset<500001> vs[D]; int L[D], R[D]; int r1[D], r2[D]; struct BSGT { struct Node { int tg1, tg2; } tr[D * 4]; void Init() { for (int i = 1; i <= dN * 4; i++) tr[i].tg1 = -1; } void PushDown(int x) { Node &v = tr[x], &l = tr[x << 1], &r = tr[x << 1 | 1]; if (~v.tg1) { l.tg1 = r.tg1 = v.tg1, l.tg2 = r.tg2 = 0; v.tg1 = -1; } if (v.tg2) { l.tg2 += v.tg2, r.tg2 += v.tg2; v.tg2 = 0; } } void Modify(int x, int l, int r, int v1, int v2, int L = 0, int R = dN) { if (L >= l && R <= r) { if (~v1) tr[x].tg1 = v1, tr[x].tg2 = 0; else tr[x].tg2 += v2; return; } int mid = L + R >> 1; PushDown(x); if (l <= mid) Modify(x << 1, l, r, v1, v2, L, mid); if (r > mid) Modify(x << 1 | 1, l, r, v1, v2, mid + 1, R); } void Get_Tg(int x, int l, int r, int L = 0, int R = dN) { if (L == R) { r1[L] = tr[x].tg1, r2[L] = tr[x].tg2; return; } int mid = L + R >> 1; PushDown(x); if (l <= mid) Get_Tg(x << 1, l, r, L, mid); if (r > mid) Get_Tg(x << 1 | 1, l, r, mid + 1, R); } void Clear(int x, int p, int L = 0, int R = dN) { if (L == R) { tr[x].tg1 = -1, tr[x].tg2 = 0; return; } int mid = L + R >> 1; PushDown(x); if (p <= mid) Clear(x << 1, p, L, mid); else Clear(x << 1 | 1, p, mid + 1, R); } } bsgt; struct SGT { struct Node { ll tg1, tg2, sm; } tr[2000009]; void PushUp(int x) { tr[x].sm = tr[x << 1].sm + tr[x << 1 | 1].sm; } void Build(int x = 1, int L = 1, int R = N) { tr[x].tg1 = -1; if (L == R) { tr[x].sm = A[L]; return; } int mid = L + R >> 1; Build(x << 1, L, mid), Build(x << 1 | 1, mid + 1, R); PushUp(x); } void PushDown(int x, ll l1, ll l2) { Node &v = tr[x], &l = tr[x << 1], &r = tr[x << 1 | 1]; if (~v.tg1) { l.tg1 = r.tg1 = v.tg1, l.tg2 = r.tg2 = 0; l.sm = v.tg1 * l1, r.sm = v.tg1 * l2; v.tg1 = -1; } if (v.tg2) { l.tg2 += v.tg2, l.sm += (v.tg2 * l1); r.tg2 += v.tg2, r.sm += (v.tg2 * l2); v.tg2 = 0; } } void Modify(int x, int l, int r, ll v1, ll v2, int L = 1, int R = N) { if (L >= l && R <= r) { if (~v1) tr[x].tg1 = v1, tr[x].tg2 = 0, tr[x].sm = (R - L + 1) * v1; else tr[x].tg2 += v2, tr[x].sm += ((R - L + 1) * v2); return; } int mid = L + R >> 1; PushDown(x, mid - L + 1, R - mid); if (l <= mid) Modify(x << 1, l, r, v1, v2, L, mid); if (r > mid) Modify(x << 1 | 1, l, r, v1, v2, mid + 1, R); PushUp(x); } ll Query(int x, int l, int r, int L = 1, int R = N) { if (L >= l && R <= r) return tr[x].sm; int mid = L + R >> 1; PushDown(x, mid - L + 1, R - mid); if (r <= mid) return Query(x << 1, l, r, L, mid); if (l > mid) return Query(x << 1 | 1, l, r, mid + 1, R); return Query(x << 1, l, r, L, mid) + Query(x << 1 | 1, l, r, mid + 1, R); } } sgt; int q1, q2; ll s; void Modify(int &id, int l, int r, int v1, int v2) { q1 = r1[id], q2 = r2[id]; for (int i = L[id]; i <= R[id]; i++) { if (A[i] <= N) vs[id].reset(A[i]); if (~q1) A[i] = q1 + q2; else A[i] += q2; if (i >= l && i <= r) { if (~v1) A[i] = v1 + v2; else A[i] += v2; } } for (int i = L[id]; i <= R[id]; i++) if (A[i] <= N) vs[id].set(A[i]); bsgt.Clear(1, id); } int vq[500009], vn; bool tvis[500009]; void Print(ll x) { if (x > 9) Print(x / 10); putchar((x % 10) + 48); } void Add(int v) { if (v <= N) tvis[vq[++vn] = v] = true; } int main() { N = Read(), M = Read(); dN = N / B; for (int i = 1; i <= N; i++) { A[i] = Read(); if (A[i] <= N) vs[i / B].set(A[i]); } for (int i = 0; i <= dN; i++) L[i] = i * B, R[i] = (i + 1) * B - 1; int op, l, r, v, tl, tr; bool f; ll q1, ans; int q2; bsgt.Init(), sgt.Build(); while (M--) { op = Read(), l = Read(), r = Read(); tl = l / B, tr = r / B; if (op == 1) { v = Read(); sgt.Modify(1, l, r, v, 0); if (tl == tr) bsgt.Get_Tg(1, tl, tl), Modify(tl, l, r, v, 0); else { if (tl + 1 < tr) bsgt.Modify(1, tl + 1, tr - 1, v, 0); bsgt.Get_Tg(1, tl, tl), bsgt.Get_Tg(1, tr, tr); Modify(tl, l, R[tl], v, 0), Modify(tr, L[tr], r, v, 0); } } else if (op == 2) { bsgt.Get_Tg(1, tl, tr); vn = 0; for (int i = tl; i <= tr; i++) if (~r1[i]) Add(r1[i] + r2[i]); if (tl == tr) { if (r1[tl] == -1) for (int i = l; i <= r; i++) Add(A[i] + r2[tl]); } else { if (r1[tl] == -1) for (int i = l; i <= R[tl]; i++) Add(A[i] + r2[tl]); if (r1[tr] == -1) for (int i = L[tr]; i <= r; i++) Add(A[i] + r2[tr]); } v = -1; do { v++, f = 0; if (tvis[v]) f = 1; else for (int i = tl + 1; i < tr; i++) if (r1[i] == -1 && v >= r2[i] && vs[i][v - r2[i]]) { f = 1; break; } } while (f); for (int i = 1; i <= vn; i++) tvis[vq[i]] = false; if (v) { sgt.Modify(1, l, r, -1, v); if (tl == tr) Modify(tl, l, r, -1, v); else { if (tl + 1 < tr) bsgt.Modify(1, tl + 1, tr - 1, -1, v); Modify(tl, l, R[tl], -1, v), Modify(tr, L[tr], r, -1, v); } } } else Print(sgt.Query(1, l, r)), puts(""); } return 0; }
failed
23_2_wrong.cpp
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define endl '\n' #define int long long #define lowbit(x) x & (-x) #define Max(a, b) (((a) > (b)) ? (a) : (b)) #define Min(a, b) (((a) < (b)) ? (a) : (b)) #define BoBoowen ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); using namespace std; const int N = 500005; int f[N], a[N]; int n, m; int ask(int x) { int i = 0; for (; x; x -= lowbit(x)) { i += f[x]; } return i; } void add(int x) { for (; x <= n; x += lowbit(x)) { f[x]++; } } char c[5]; signed main() { int T; cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } int ans = 0; for (int i = 1; i <= n; i++) { ans += (i - 1) - ask(a[i]); add(a[i]); } for (int i = 1; i <= n; i++) { f[i] = 0; } for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { ans += (i - 1) - ask(a[i]); add(a[i]); } for (int i = 1; i <= n; i++) { f[i] = 0; } ans &= 1; if (ans & 1) { cout << "A"; } else { cout << "B"; } for (int i = 1; i < n; i++) { cin >> c; int l, r, d; cin >> l >> r >> d; (ans += (1ll * (r - l) * (d % (r - l)))) &= 1; if (ans & 1) { cout << "A"; } else { cout << "B"; } } cout << endl; } }
failed
82_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; using i64 = long long; signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int r, c, p; cin >> r >> c >> p; vector<vector<double>> f(r, vector<double>(c)); for (int i = r - 1; i >= 0; i--) { for (int j = c - 1; j >= 0; j--) { double a = -1, b = -1; if (i + 1 < r) { a = f[i + 1][j]; } if (j + 1 < c) { b = f[i][j + 1]; } if (a > b) swap(a, b); if (b == -1) continue; if (a == -1) { f[i][j] = b + p / 4.; } else if (b >= a + p) { f[i][j] = a + p / 4.; } else { f[i][j] = (p - (b - a)) / 2. / p * b + a / 2. + (b - a) / 2. / p * (a + b) / 2; } } } cout << f[0][0] << '\n'; return 0; }
failed
105_3_wrong.cpp
// Start coding: 16:15 #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double const ld EPS = 1e-5; ld sq(ld x) { return x * x; } bool eq(ld x, ld y) { return fabs(x - y) < EPS; } bool addeq(ld x, ld y, ld z) { return eq(sq(sqrt(x) + sqrt(y)), z); } ld S; struct tri { ld a[3]; tri() { for (int i = 0; i < 3; i++) a[i] = 0; } ld &operator[](int x) { return a[x]; } void rot() { ld b[3]; for (int i = 0; i < 3; i++) b[(i + 1) % 3] = a[i]; for (int i = 0; i < 3; i++) a[i] = b[i]; } void rev() { reverse(a, a + 3); } bool is_tri() { ld b[3]; for (int i = 0; i < 3; i++) b[i] = a[i]; sort(b, b + 3); return b[0] + b[1] > b[2]; } ld area() { ld b[3]; for (int i = 0; i < 3; i++) b[i] = sqrt(a[i]); ld p = (b[0] + b[1] + b[2]) / 2; return sqrt(p * (p - b[0]) * (p - b[1]) * (p - b[2])); } bool orth(int x) { ld sum = 0; for (int i = 0; i < 3; i++) if (i != x) sum += a[i]; return eq(sum, a[x]); } bool ishalf() { sort(a, a + 3); return eq(a[0], S) && eq(a[1], S) && eq(a[2], S * 2); } } a[4], b[4]; int perm[4]; bool ok = false; void solve1() { for (int i = 0; i < 4; i++) { int ii = (i + 1) % 4; if (!eq(a[i][0], S)) return; if (!eq(a[i][1], a[ii][2])) return; } ok = true; } pair<bool, tri> merge(tri a, tri b) { if (!eq(a[0], b[0])) return {false, tri()}; tri c; c[0] = a[1], c[1] = b[1], c[2] = sq(sqrt(a[2]) + sqrt(b[2])); if (!c.is_tri()) return {false, tri()}; if (!eq(c.area(), a.area() + b.area())) return {false, tri()}; return {true, c}; } void solve2() { if (!eq(a[0][0], S) || !eq(a[0][1], S) || !eq(a[0][2], 2 * S)) return; auto [f, b] = merge(a[1], a[2]); if (!f) return; for (int i = 0; i < 6; i++, b.rot()) { if (i == 3) b.rev(); auto [f, c] = merge(b, a[3]); if (f && c.ishalf()) { ok = true; } } } void solve3() { auto [f, b] = merge(a[2], a[3]); if (!f) return; if (!eq(a[0][0], S) || !eq(a[1][0], S) || !eq(a[0][1], a[1][1])) return; if (!a[0].orth(1)) return; for (int i = 0; i < 6; i++, b.rot()) { if (i == 3) b.rev(); if (!eq(a[1][2], b[0]) || !eq(b[1], S)) continue; ld len = sq(sqrt(b[2]) + sqrt(a[0][2])); if (!eq(len, S)) continue; if (!b.orth(0)) continue; ok = true; } } void solve4() { if (!a[0].orth(2) || !a[1].orth(2) || a[2].orth(2)) return; if (!eq(a[0][0], S) || !eq(a[1][0], S)) return; ld len1 = sq(sqrt(a[0][1]) + sqrt(a[2][0])); ld len2 = sq(sqrt(a[1][1]) + sqrt(a[2][1])); if (!eq(len1, S) || !eq(len2, S)) return; for (int i = 0; i < 3; i++) if (!eq(a[i][2], a[3][i])) return; ok = true; } void solve5() { if (!a[0].orth(2) || !eq(a[0][0], S)) return; if (!addeq(a[0][0], a[1][0], S) || !addeq(a[1][1], a[3][1], a[0][2])) return; if (!eq(a[2][0], S) || !eq(a[3][0], S)) return; if (!eq(a[1][2], a[2][1]) || !eq(a[2][2], a[3][2])) return; ok = true; } void solve6() { if (!a[0].orth(2) || !a[3].orth(2)) return; if (!eq(a[0][0], S) || !eq(a[3][0], S)) return; if (!addeq(a[1][0], a[3][1], S) || !addeq(a[2][0], a[0][1], S)) return; if (!eq(a[0][2], a[1][1]) || !eq(a[1][2], a[2][1]) || !eq(a[2][2], a[3][2])) return; ok = true; } void solve7() { auto [f1, b] = merge(a[0], a[1]); auto [f2, c] = merge(a[2], a[3]); if (!f1 || !f2) return; if (!b.ishalf() || !c.ishalf()) return; ok = true; } void solve() { solve1(), solve2(), solve3(), solve4(), solve5(), solve6(), solve7(); } void dfs(int t) { if (t == 4) return solve(); for (int i = 0; i < 3; i++) dfs(t + 1), a[t].rot(); a[t].rev(); for (int i = 0; i < 3; i++) dfs(t + 1), a[t].rot(); a[t].rev(); } void work() { ok = false; S = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < 3; j++) cin >> b[i][j]; for (int i = 0; i < 4; i++) perm[i] = i + 1, S += b[i].area(); // printf("S = %.15Lf\n", S); ll S1 = floor(S + 0.5); if (fabs(S - S1) > 1e-5) return puts("0"), void(); S = S1; do { for (int i = 0; i < 4; i++) a[i] = b[perm[i] - 1]; dfs(0); } while (next_permutation(perm, perm + 4)); puts(ok ? "1" : "0"); } int main() { ios::sync_with_stdio(false), cin.tie(0); bool flg = false; int t; cin >> t; while (t--) { work(); if (t == 19 && b[0][0] == 1148639) flg = true; if (flg && t <= 14) { for (int i = 0; i < 4; i++) for (int j = 0; j < 3; j++) printf("%.0Lf%c", b[i][j], " \n"[j == 2]); } } return 0; }
failed
109_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define forn(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define mp make_pair #define MOD 1000000007 #define f first #define s second #define rand(i, j) uniform_int_distribution<ll>((ll)(i), (ll)(j))(rng) mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <typename A, typename B> ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; } template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) { cout << "["; for (int i = 0; i < (int)v.size(); i++) { if (i) cout << ", "; cout << v[i]; } return cout << "]"; } template <typename T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } void run() { int n, k, m; cin >> n >> k >> m; vector<char> forbidden(m); forn(i, m) cin >> forbidden[i]; vector<bool> okay(n, true); forn(i, m) { char c = forbidden[i]; if (c >= '0' && c <= '9') okay[c - '0'] = false; else okay[c - 'A' + 10] = false; } vector<char> digs; forn(i, n) if (okay[i]) { if (i < 10) digs.pb('0' + i); else digs.pb('A' + (i - 10)); } int b = n - m; string ans = ""; while (k > 0) { int dig = k % b; ans += digs[dig]; k /= b; } reverse(ans.begin(), ans.end()); cout << ans << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; forn(test, t) run(); }
failed
90_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define ll long long #define MP make_pair mt19937 rnd(time(0)); const int MAXN = 5e5 + 5; const ll inf = 1e18; int n, m, c, stk[MAXN], hd, tl; ll a[MAXN], b[MAXN], f[MAXN]; inline ll F(ll x) { return upper_bound(b, b + m + 1, x) - b - 1; } inline int w(int l, int r) { return F(a[r] - a[l - 1]) - c; } inline int tf(int l, int r) { return f[l] + w(l + 1, r); } inline int tim(int l, int r) { if (f[r] - f[l] >= m) return -1; int pl = 0, pr = m + 1 - (f[r] - f[l]), mid; while (pl < pr) { mid = (pl + pr) >> 1; if (b[mid] + a[r] - a[l] < b[mid + f[r] - f[l]]) pr = mid; else pl = mid + 1; } return lower_bound(a + 1, a + n + 1, b[pl] + a[r]) - a; } void solve() { cin >> n >> m >> c; for (int i = 1; i <= n; i++) cin >> a[i], a[i] += a[i - 1], f[i] = -inf; for (int i = 1; i <= m; i++) cin >> b[i], b[i] += b[i - 1]; stk[hd = tl = 1] = 0; for (int i = 1; i <= n; i++) { while (hd < tl && tim(stk[hd], stk[hd + 1]) <= i) hd++; f[i] = tf(stk[hd], i); if (f[i] <= f[stk[tl]] || tim(stk[tl], i) == n + 1) continue; while (hd < tl && tim(stk[tl], i) <= tim(stk[tl - 1], stk[tl])) tl--; stk[++tl] = i; } cout << f[n] << '\n'; } int main() { // freopen("Otomachi_Una.in","r",stdin); // freopen("Otomachi_Una.out","w",stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) solve(); return 0; }
failed
32_1_wrong.cpp
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { int N, M, X, Y; cin >> N >> M >> X >> Y; vector<int> shelves(N), books(M); for (int i = 0; i < N; i++) { cin >> shelves[i]; } for (int i = 0; i < M; i++) { cin >> books[i]; } sort(shelves.begin(), shelves.end()); shelves.push_back(INT_MAX); sort(books.begin(), books.end()); vector<int> open(N + 1, X); int s = 0; bool okay = true; for (int i = 0; i < M; i++) { while (books[i] > shelves[s] || open[s] == 0) s++; if (s == N) { okay = false; break; } open[s]--; } if (okay) { shelves.pop_back(); open.pop_back(); ll empty = 0; for (int i = 0; i < N; i++) { empty += open[i]; } int c = 0; for (int i = 0; i < N; i++) { if (empty >= X - Y) { empty -= max(X - Y, shelves[i] - open[i]); c++; } else { break; } } cout << c << endl; } else cout << "impossible" << endl; }
failed
66_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define ll long long const int mod = 998244353; int mul(int a, int b) { return (ll)a * b % mod; } int main() { int t; scanf("%i", &t); while (t--) { int n, k; scanf("%i %i", &n, &k); vector<int> a(n); for (int &i : a) scanf("%i", &i); sort(a.begin(), a.end()); reverse(a.begin(), a.end()); int ans = a[0]; for (int l = 1; l + k - 1 <= n; l += k - 1) { int r = l + k - 1; bool ok = true; for (int i = l; i < r; i++) { if (a[i] == 0) ok = false; } if (!ok) break; for (int i = l; i < r; i++) { ans = mul(ans, a[i]); } } printf("%i\n", ans); } return 0; }
failed
74_2_wrong.cpp
#include <bits/stdc++.h> #define forr(i, l, r) for (int i = l; i <= r; i++) #define int long long #define endl '\n' using namespace std; void solve() { int n; cin >> n; vector<int> a(n + 1); int maxn = 0, maxp; forr(i, 1, n) { cin >> a[i]; if (a[i] > maxn) { maxn = a[i]; maxp = i; } } while (maxp <= n) { if (a[maxp] == maxn) cout << maxp++ << ' '; } cout << endl; return; } signed main() { ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr); int _; _ = 1; cin >> _; while (_--) { solve(); } return 0; }
failed
36_3_wrong.cpp
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define int ll #define all(x) x.begin(), x.end() // int h[4]={1,-1,0,0},z[4]={0,0,1,-1}; // const int mod=1e9+7; int n, m; const int N = 2e5 + 5; int t1, t2, sum, ans = 1e9; void solve() { cin >> n; int n1 = (n + 1) / 2 * 100000; // n1为b最多干的时间。一半交给b干。 vector<int> f(n1 + 1, 1e9); f[0] = 0; // f 数组是在b干t2时间时,a干的时间。 for (int i = 1; i <= n; i++) { cin >> t1 >> t2; sum += t2; // sum当前b干的时间,最大比f数组大,所以下面取小的。 for (int j = min(sum, n1); j >= t2; j--) { f[j] = min(f[j - t2], f[j] + t1); // b在干j时间时,a干的时间, // 当前不选a干,a在b[j]时间干的时间就和在b[j-t2]时间下干的时间一样, // 选a干,a在b[j]时间干的时间就加个t1; } for (int k = t2 - 1; k >= 0; k--) { f[k] += t1; // 这个就是f[j-t2]越界了,只有第2种情况 } } for (int i = 1; i <= n1; i++) { ans = min(ans, max(i, f[i])); // max就是b干i时间,a干的是时间,选大的。 // min 就是找一个结果最小的,通过枚举(a,b)时间的分配; } cout << ans << "\n"; } signed main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int tt = 1; // cin>>tt; while (tt--) { solve(); } return 0; }
failed
99_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; #define int long long int n, m, x, y; int a[N], b[N]; int available[N]; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> x >> y; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= m; ++i) cin >> b[i]; sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); if (a[n] < b[m]) { cout << "impossible\n"; return 0; } fill(available + 1, available + n + 1, y); int remain = -1; int p = 1; for (int i = 1; i <= m; ++i) { if (a[p] < b[i] || available[p] == 0) { while (p <= n && (a[p] < b[i] || available[p] == 0)) ++p; if (p > n) { if (a[p - 1] < b[i]) { cout << "Impossible" << '\n'; } remain = i; break; } } --available[p]; } if (remain == -1) { cout << n << '\n'; return 0; } fill(available + 1, available + n + 1, x - y); p = n; for (int i = m; i >= remain; --i) { if (p == 1 && (available[p] == 0 || a[p] < b[i])) { cout << "impossible\n"; return 0; } if (available[p] == 0) { while (p >= 1 && available[p] == 0) --p; if (p == 0 || a[p] < b[i]) { cout << "impossible\n"; return 0; } } --available[p]; } cout << p - 1 << '\n'; return 0; }
failed
66_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long using pii = pair<int, int>; #define fi first #define se second #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() using ll = long long; typedef vector<int> vi; #define pb push_back vector<pair<char, int>> moves; string OS, GS, RS; void fix(string &AS, string &BS, char ac, char bc) { auto do_swap = [&](int i, int j) -> void { ++i; ++j; // cerr << "pre:\n"; // cerr << AS << ' ' << BS << ' ' << ac << ' ' << bc << ' ' << i << ' ' << j // << '\n'; if (i != 1) { int shift = (11 - i) % 10; moves.push_back({ac, shift}); shift = 10 - shift; string as = ""; for (int ii = shift; ii < 10; ++ii) { as.push_back(AS[ii]); } for (int ii = 0; ii < shift; ++ii) { as.push_back(AS[ii]); } AS = as; } if (j != 4) { int shift = (14 - j) % 10; moves.push_back({bc, shift}); shift = 10 - shift; string bs = ""; for (int ii = shift; ii < 10; ++ii) { bs.push_back(BS[ii]); } for (int ii = 0; ii < shift; ++ii) { bs.push_back(BS[ii]); } BS = bs; } // cerr << AS << ' ' << BS << '\n'; moves.push_back({'c', 2}); moves.push_back({ac, 9}); moves.push_back({'c', 1}); char rem_a = AS[0]; char rem_b = BS[3]; AS.erase(0, 1); BS.erase(3, 1); BS.push_back(rem_a); AS.insert(2, 1, rem_b); // cerr << "post:\n"; // cerr << AS << ' ' << BS << ' ' << ac << ' ' << bc << ' ' << i << ' ' << // j << '\n'; }; for (int i = 0; i < AS.size(); ++i) { if (AS[i] == bc) { // need to swap! int j = -1; for (int jj = 0; jj < BS.size(); ++jj) { if (BS[jj] == ac) { j = jj; break; } } if (j == -1) { for (int jj = 0; jj < BS.size(); ++jj) { if (BS[jj] != bc) { j = jj; break; } } } assert(j != -1); // swap AS i and BS j do_swap(i, j); fix(AS, BS, ac, bc); return; } } for (int j = 0; j < BS.size(); ++j) { if (BS[j] == ac) { int i = -1; for (int ii = 0; ii < AS.size(); ++ii) { if (AS[ii] == bc) { i = ii; break; } } if (i == -1) { for (int ii = 0; ii < AS.size(); ++ii) { if (AS[ii] != ac) { i = ii; break; } } } assert(i != -1); do_swap(i, j); fix(AS, BS, ac, bc); return; } } } signed main() { cin.tie(0)->sync_with_stdio(0); cin >> OS >> GS >> RS; fix(RS, OS, 'r', 'o'); fix(OS, GS, 'o', 'g'); fix(GS, RS, 'g', 'r'); assert(moves.size() <= 240); cout << moves.size() << '\n'; for (auto [c, i] : moves) { cout << c << ' ' << i << '\n'; } }
failed
104_3_wrong.cpp
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <random> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; const long double eps = 1e-8; struct Point { long double x, y; Point(long double x, long double y) : x(x), y(y) {} Point() : x(0), y(0) {} }; Point operator+(const Point &a, const Point &b) { return Point(a.x + b.x, a.y + b.y); } Point operator-(const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); } long double cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } long double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } long double sqr_len(const Point &a) { return a.x * a.x + a.y * a.y; } struct Circle { Point center; long double R2; Circle(Point C, long double R2) : center(C), R2(R2) {} bool contain(const Point &P) const { return sqr_len(P - center) - R2 <= eps; } bool on_border(const Point &P) const { return abs(sqr_len(P - center) - R2) <= eps; } }; Circle circle_by_diameter(Point A, Point B) { return Circle(Point((A.x + B.x) / 2, (A.y + B.y) / 2), sqr_len(B - A) / 4); } struct Line { long double a; long double b; long double c; Line(long double a, long double b, long double c) : a(a), b(b), c(c) {} }; Line mid_perp(Point A, Point B) { return Line(2 * B.x - 2 * A.x, 2 * B.y - 2 * A.y, A.x * A.x + A.y * A.y - B.x * B.x - B.y * B.y); } Point intersect(const Line &l1, const Line &l2) { long double den = l1.a * l2.b - l2.a * l1.b; return Point((-l1.c * l2.b + l1.b * l2.c) / den, (l1.c * l2.a - l2.c * l1.a) / den); } Circle circle_by_three_points(Point A, Point B, Point C) { Line l1 = mid_perp(A, B); Line l2 = mid_perp(B, C); Point P = intersect(l1, l2); return Circle(P, sqr_len(A - P)); } mt19937_64 rnd(228); Circle min_disk_with_points(vector<Point> &P, int n, Point Q1, Point Q2) { Circle ans = circle_by_diameter(Q1, Q2); for (int i = 0; i < n; ++i) { if (ans.contain(P[i])) continue; ans = circle_by_three_points(Q1, Q2, P[i]); } return ans; } Circle min_disk_with_point(vector<Point> &P, int n, Point Q) { if (n == 0) return Circle(Q, 0); shuffle(P.begin(), P.begin() + n, rnd); Circle ans = circle_by_diameter(P[0], Q); for (int i = 1; i < n; ++i) { if (ans.contain(P[i])) continue; ans = min_disk_with_points(P, i, Q, P[i]); } return ans; } Circle min_disk(vector<Point> &P, int n) { if (n == 1) return Circle(P[0], 0); shuffle(P.begin(), P.begin() + n, rnd); Circle ans = circle_by_diameter(P[0], P[1]); for (int i = 2; i < n; ++i) { if (ans.contain(P[i])) continue; ans = min_disk_with_point(P, i, P[i]); } return ans; } bool exist_disk(vector<Point> inside, vector<Point> border) { if (border.size() == 0) { return true; } Circle ans(border[0], 0); if (border.size() == 1) { ans = min_disk_with_point(inside, inside.size(), border[0]); } else if (border.size() == 2) { ans = min_disk_with_points(inside, inside.size(), border[0], border[1]); } else { ans = circle_by_three_points(border[0], border[1], border[2]); } for (auto P : inside) { if (!ans.contain(P)) return false; } for (auto P : border) { if (!ans.on_border(P)) return false; } return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int, pair<int, int>>> points(n); for (int i = 0; i < n; ++i) { int x, y, z; cin >> x >> y >> z; points[i] = make_pair(z, make_pair(x, y)); } sort(points.begin(), points.end()); int max_z = points.back().first; vector<pair<int, int>> inside; vector<pair<int, int>> border; for (int i = 0; i < n; ++i) { if (points[i].first == 0 || points[i].first == max_z) inside.emplace_back(points[i].second); else border.emplace_back(points[i].second); } sort(inside.begin(), inside.end()); inside.resize(unique(inside.begin(), inside.end()) - inside.begin()); border.resize(unique(border.begin(), border.end()) - border.begin()); vector<Point> I; for (auto [x, y] : inside) I.emplace_back(x, y); vector<Point> B; for (auto [x, y] : border) B.emplace_back(x, y); if (exist_disk(I, B)) cout << "probably\n"; else cout << "not a penguin\n"; return 0; }
failed
78_3_wrong.cpp
#include <bits/stdc++.h> #define pii pair<int, int> #define int long long #define rep(i, s, e) for (int i = s; i <= e; i++) using namespace std; const int N = 4e5 + 10; int n, k, a[N], b[N], p[N], d[N], belong[N], line[N], t, stp[N], vis[N], dis[N]; pii nxt[N]; vector<int> w[N], v[N]; deque<int> hull[N]; inline double slope(int x, int y) { if (b[line[x]] == b[line[y]]) return 1e18 * (dis[x] > dis[y]); if (dis[x] == dis[y]) return 1e18 * (b[line[x]] > b[line[y]]); return -1.0 * (b[line[x]] - b[line[y]]) / (dis[x] - dis[y]); } int cal(int x, int y) { return dis[x] + a[line[y]] * b[line[x]]; } signed main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> k; memset(d, 0x7f, sizeof d); rep(i, 1, k) cin >> a[i]; rep(i, 1, k) cin >> b[i]; rep(i, 1, k) { cin >> p[i]; int w, x; rep(j, 1, p[i] - 1) { cin >> x >> w; v[x].push_back(++t); belong[t] = x; line[t] = i; nxt[t] = {t + 1, w}; } cin >> x; v[x].push_back(++t); belong[t] = x; line[t] = i; } for (int i = 1; i <= n; i++) { sort(v[i].begin(), v[i].end(), [](int x, int y) { return a[line[x]] < a[line[y]]; }); } priority_queue<pii, vector<pii>, greater<pii>> Q; for (int x : v[1]) { Q.push({0, x}); } while (Q.size()) { int x = Q.top().second; int tmp = Q.top().first; Q.pop(); if (vis[x]) continue; vis[x] = 1; dis[x] = tmp; d[belong[x]] = min(d[belong[x]], dis[x]); Q.pop(); int station = belong[x]; auto &hul = hull[station]; while (hul.size() >= 2 && slope(hul[hul.size() - 2], hul.back() < slope(hul.back(), x))) hul.pop_back(); hul.push_back(x); while (stp[station] < v[station].size() && vis[v[station][stp[station]]]) stp[station]++; if (stp[station] < v[station].size()) { int y = v[station][stp[station]]; while (hul.size() >= 2 && cal(hul[0], y) > cal(hul[1], y)) hul.pop_front(); Q.push({cal(hul[0], y), y}); } if (nxt[x].first) { Q.push({dis[x] + nxt[x].second, nxt[x].first}); } } for (int i = 2; i <= n; i++) { cout << d[i] << " "; } }
failed
52_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; template <class F, class S> ostream &operator<<(ostream &s, const pair<F, S> &v) { s << "(" << v.first << ", " << v.second << ")"; return s; } template <ranges::range T> requires(!is_convertible_v<T, string_view>) istream &operator>>(istream &s, T &&v) { for (auto &&x : v) s >> x; return s; } template <ranges::range T> requires(!is_convertible_v<T, string_view>) ostream &operator<<(ostream &s, T &&v) { for (auto &&x : v) s << x << ' '; return s; } #ifdef LOCAL template <class... T> void dbg(T... x) { char e{}; ((cerr << e << x, e = ' '), ...); } #define debug(x...) dbg(#x, '=', x, '\n') #else #define debug(...) ((void)0) #endif #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define ff first #define ss second template <class T> inline constexpr T inf = numeric_limits<T>::max() / 2; bool chmin(auto &a, auto b) { return (b < a and (a = b, true)); } bool chmax(auto &a, auto b) { return (a < b and (a = b, true)); } using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128; using u128 = unsigned __int128; constexpr i64 mod = 998244353; void solve() { int n; cin >> n; vector<int> A(n); cin >> A; vector<int> U; auto cal = [&](int a, int b) { for (int x : {a, b, (a | b), (a ^ b), (a & b), ((a | b) ^ a), ((a | b) ^ b), ((a & b) ^ a), ((a & b) ^ b)}) { U.push_back(x); } }; for (int i = 0; i + 1 < n; i++) { cal(A[i], A[i + 1]); } sort(all(U)); U.erase(unique(all(U)), U.end()); cout << U.size() << '\n'; } signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
failed
6_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1e18; vector<int> times; vector<vector<ll>> dp; int n, c; // Returns maximum time among selected people ll getMaxTime(int mask) { ll maxTime = 0; for (int i = 0; i < n; i++) { if (mask & (1 << i)) { maxTime = max(maxTime, (ll)times[i]); } } return maxTime; } // Counts number of set bits (people) in mask int countBits(int mask) { int count = 0; while (mask) { count += mask & 1; mask >>= 1; } return count; } ll solve(int mask, bool torchAtStart) { // If all people have crossed and torch is on end side if (mask == 0 && !torchAtStart) return 0; if (mask == 0 && torchAtStart) return INF; // If result already calculated if (dp[mask][torchAtStart] != -1) return dp[mask][torchAtStart]; dp[mask][torchAtStart] = INF; if (torchAtStart) { // Move people from start to end for (int submask = mask; submask; submask = (submask - 1) & mask) { if (countBits(submask) > c) continue; ll timeToGo = getMaxTime(submask); ll remaining = solve(mask ^ submask, false); if (remaining != INF) { dp[mask][torchAtStart] = min(dp[mask][torchAtStart], remaining + timeToGo); } } } else { // Someone needs to bring torch back for (int i = 0; i < n; i++) { if (!(mask & (1 << i))) { // if person i is on other side int newMask = mask | (1 << i); // bring person i back ll remaining = solve(newMask, true); if (remaining != INF) { dp[mask][torchAtStart] = min(dp[mask][torchAtStart], remaining + times[i]); } } } } return dp[mask][torchAtStart]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> c; times.resize(n); for (int i = 0; i < n; i++) { cin >> times[i]; } // If bridge can hold all people if (c >= n) { cout << getMaxTime((1 << n) - 1) << "\n"; return 0; } // Initialize dp array dp.assign(1 << n, vector<ll>(2, -1)); // Initial mask has all people on starting side and torch at start ll ans = solve((1 << n) - 1, true); cout << ans << "\n"; return 0; }
failed
117_3_wrong.cpp
#include <algorithm> #include <cstdio> #include <vector> constexpr int MAXN = 500000 + 2; std::vector<int> G[MAXN], attack[MAXN]; int dep[MAXN], pa[MAXN]; void DFS(int u, int fa) { dep[u] = dep[fa] + 1; pa[u] = fa; for (int v : G[u]) { if (v != fa) { DFS(v, u); } } } void update(int x, int y, int z) { if (dep[x] < dep[y]) { std::swap(x, y); } while (dep[x] > dep[y]) { attack[x].push_back(z); x = pa[x]; } while (x ^ y) { attack[x].push_back(z); attack[y].push_back(z); x = pa[x]; y = pa[y]; } attack[x].push_back(z); } int query(int x, int h) { std::vector<int> &vec = attack[x]; int tot = vec.size(), ans = 0, f[2] = {0, 0}; for (int i = tot - 1; i >= 0; i--) { int g[2] = {std::min(f[0], f[1]) + vec[i] * 2, f[0] + vec[i]}; if (std::min(f[0] = g[0], f[1] = g[1]) < h + h) { ans = std::max(ans, tot - i); } } return ans; } int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } DFS(1, 0); while (q--) { char op; int x, y, z, h; scanf(" %c%d", &op, &x); if (op == 'A') { scanf("%d%d", &y, &z); update(x, y, z); } else { scanf("%d", &h); printf("%d\n", query(x, h)); } } return 0; }
failed
37_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define pb push_back #define int long long int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; stack<int> s; vector<int> a(n); vector<int> c(n); cin >> a[0]; c[0] = a[0]; s.push(0); vector<int> nxt(n + 1); nxt[n] = n; for (int i = 1; i < n; i++) { cin >> a[i]; c[i] = a[i]; while (!s.empty()) { int idx = s.top(); if (a[i] > a[idx]) { nxt[idx] = i; s.pop(); } else break; } s.push(i); } while (!s.empty()) { nxt[s.top()] = n; s.pop(); } for (int i = 0; i < q; i++) { char ch; cin >> ch; if (ch == '?') { int idx; cin >> idx; idx--; cout << c[idx] - a[idx] << endl; } else { int idx, val; cin >> idx >> val; idx--; vector<int> res; res.pb(idx); int j = idx; while (val > a[j] && j < n) { val -= a[j]; a[j] = 0; // cout << i << " " << j << " " << val << " " << a[j] << endl; res.pb(j); j = nxt[j]; } if (j != n) a[j] -= val; for (auto x : res) nxt[x] = j; // cout << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << endl; } } }
failed
62_1_wrong.cpp
#include <bits/stdc++.h> #define int long long using namespace std; void solve() { int n; cin >> n; vector<int> a(n + 1), tr(n + 1); for (int i = 1; i <= n; i++) { int x; cin >> x; a[x] = i; } int num = n * (n - 1) / 2; for (int i = 1; i <= n; i++) { int x; cin >> x; for (int j = x; j; j -= (j & -j)) num -= tr[j]; for (int j = x; j <= n; j += (j & -j)) tr[j] += 1; } if (num & 1) cout << "A"; else cout << "B"; for (int i = 1; i < n; i++) { char opt; int l, r, d; cin >> opt >> l >> r >> d; num += (l - r + 1 - d) * d; if (num & 1) cout << "A"; else cout << "B"; } cout << "\n"; } signed main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int T = 1; cin >> T; for (int i = 1; i <= T; i++) solve(); return 0; }
failed
82_1_wrong.cpp
// In the name of God #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; const int maxn = 1e6 + 100; const int lg = 31; const int mod = 1e9 + 7; const int inf = 3e8 + 100; const ll INF = 1e18; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define fast_io \ ios::sync_with_stdio(false); \ cin.tie(0); #define file_io \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); #define pb push_back #define F first #define S second #define SZ(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define sonic(x) \ sort(all(x)); \ (x).resize(unique(all(x)) - (x).begin()); #define lc (id << 1) #define rc (lc | 1) struct frac { ll a, b, c; frac(ll a, ll b, ll c) : a(a), b(b), c(c) {} bool operator>(frac f) { if (a == f.a) return __int128(b) * f.c > __int128(f.b) * c; return a > f.a; } }; int n, m, c; ll a[maxn], b[maxn]; ll dp[maxn]; frac calc(int i, int j) { int o = upper_bound(b, b + m + 1, a[j] - a[i]) - b - 1; if (o == m) return frac(dp[i] + o - c, 0, 1); return frac(dp[i] + o - c, a[j] - a[i] - b[o], b[o + 1]); } vector<pii> opts; int main() { fast_io; int t; cin >> t; bool prnt = 0; int tt = 0; while (t--) { tt++; cin >> n >> m >> c; if (tt == 1 && c == 0) prnt = 1; rep(i, 1, n + 1) { cin >> a[i]; a[i] += a[i - 1]; } rep(i, 1, m + 1) { cin >> b[i]; b[i] += b[i - 1]; } opts.clear(); opts.pb({1, 0}); rep(i, 1, n + 1) { dp[i] = calc( opts[upper_bound(all(opts), make_pair(i, inf)) - opts.begin() - 1] .S, i) .a; while (!opts.empty() && opts.back().F > i && calc(i, opts.back().F) > calc(opts.back().S, opts.back().F)) opts.pop_back(); int l = max(i, opts.back().F); int r = n + 1; while (r - l > 1) { int mid = (l + r) >> 1; if (calc(i, mid) > calc(opts.back().S, mid)) r = mid; else l = mid; } if (r <= n) opts.pb({r, i}); } if (prnt && tt == 4791) { cout << n << " " << m << " " << c << "\n"; rep(i, 1, n + 1) cout << a[i] << " "; cout << "\n"; rep(i, 1, m + 1) cout << b[i] << " "; cout << "\n"; cout << "$ " << dp[n] << "\n"; } if (!prnt) cout << dp[n] << "\n"; } return 0; }
failed
32_3_wrong.cpp