#include<iostream> #include<algorithm> #include<set> usingnamespace std; constexprint MAXN = 1e6 + 5; int n, q, a[MAXN], b[MAXN], x[MAXN], ccnt; set<int> pos;
namespace BIT { int t[MAXN], s[MAXN]; intlowbit(int x){return x & -x;} voidinit(){ for (int i = 1; i <= n; i++) { s[i] = s[i-1] + x[i]; t[i] = s[i] - s[i - lowbit(i)]; } } voidadd(int x, int v){ for (int i = x; i <= n; i += lowbit(i)) t[i] += v; } intsum(int x){ int ret = 0; for (int i = x; i; i -= lowbit(i)) ret += t[i]; return ret; } intsum(int x, int y){ returnsum(y) - sum(x-1); } }
intmdy(int c, int v){ auto it = pos.lower_bound(c+1); if (it == pos.end()) return n + 2; int pw = *it; int l = 1, r = pw - 1, ret = pw; while (l <= r) { int m = (l + r) >> 1; if (BIT::sum(m, pw-1) == pw-1-m+1) { r = m - 1; ret = m; } else { l = m + 1; } } ccnt += (pw - 1 - ret + 1 + (ret != 1)) * v; return ret; }
voidcancel(int c, int v){ if (v == 9) { BIT::add(c, -1); } elseif (v >= 10) { mdy(c-1, -1); pos.erase(pos.find(c)); } }
voidenable(int c, int v){ if (v == 9) { BIT::add(c, 1); } elseif (v >= 10) { pos.insert(c); mdy(c-1, 1); } }
voidwork(){ int r, c, d; cin >> r >> c >> d; int lccnt = ccnt; int ori_c = (c >= mdy(c, -1) - 1); int ori = a[c] + b[c]; cancel(c, ori); if (r == 1) a[c] = d; else b[c] = d; int nwv = a[c] + b[c]; enable(c, nwv); int nwv_c = (c >= mdy(c, 1) - 1); int ans = abs(ccnt - lccnt) - (ori_c != nwv_c) + ((ori + ori_c) % 10 != (nwv + nwv_c) % 10); cout << (nwv + nwv_c) % 10 << ' ' << ans+ (nwv != ori) << '\n'; }
intmain() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> q; for (int i = 1; i <= n; i++) { char ch; cin >> ch; a[i] = ch - '0'; } for (int i = 1; i <= n; i++) { char ch; cin >> ch; b[i] = ch - '0'; } for (int i = 1; i <= n; i++) { int s = a[i] + b[i]; if (s == 9) x[i] = 1; elseif (s >= 10) { pos.insert(i); } } BIT::init(); for(auto c : pos) mdy(c-1, 1); while(q--) work(); return0; }
#include<iostream> #include<algorithm> #include<queue> usingnamespace std; using ll = longlong; using pr = pair<ll, int>; constexprint MAXN = 2005; constexprint MAXM = 5005; constexprint INF = 0x3f3f3f3f; constexpr ll LINF = 1e18; int n, m; ll c; structEdge { int v, nxt, w; }e[MAXM]; int cnt, head[MAXN];
voidaddedge(int u, int v, int w){ e[++cnt] = {v, head[u], w}; head[u] = cnt; }
ll dist[MAXN][MAXN]; int vis[MAXN];
voidSSSP(int s){ priority_queue<pr, vector<pr>, greater<pr> > pq; for (int i = 1; i <= n; i++) { dist[s][i] = LINF; vis[i] = 0; } dist[s][s] = 0; pq.push(pr(0, s)); while (!pq.empty()) { int u = pq.top().second; pq.pop(); if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v, w = e[i].w; if (dist[s][v] > dist[s][u] + w) { dist[s][v] = dist[s][u] + w; pq.push(pr(dist[s][v], v)); } } } }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> c; int mnw = INF; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; addedge(u, v, w); mnw = min(mnw, w); } if (mnw > c) { cout << 0 << '\n'; return0; } for (int i = 1; i <= n; i++) SSSP(i); ll ret = LINF; for (int u = 1; u <= n; u++) for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v, w = e[i].w; ret = min(ret, w + dist[v][u]); } if (ret > c) cout << 1 << '\n'; else cout << 2 << '\n'; return0; }
int _out, _in, s, t; Poly out, in; vector<db> dis; db nowans, circ, ans;
voidcalc(int p){ int q = p + 1; if (q == _in) q = 0; db ret = nowans; int sn = s + 1; if (sn == _out) sn = 0; int tp = t - 1; if (tp < 0) tp = _out - 1; Pnt a = CrossPoint(in[p], in[q], out[s], out[sn]); ret += Len(out[sn]-a); Pnt b = CrossPoint(in[p], in[q], out[tp], out[t]); ret += Len(b-out[tp]); ans += Len(in[q] - in[p]) * (ret / circ); }
voidadjust(int p){ int q = p + 1; if (q == _in) q = 0; int sn = s + 1; if (sn == _out) sn = 0; while (righthand(in[p], in[q], out[sn]) < 0) { nowans -= dis[sn]; s = sn; sn = s + 1; if (sn == _out) sn = 0; } while (righthand(in[p], in[q], out[t]) >= 0) { int tp = t - 1; if (tp < 0) tp = _out - 1; nowans += dis[tp]; t++; if (t == _out) t = 0; } }
signedmain() { scanf("%lld%lld", &_out, &_in); out.resize(_out); in.resize(_in); dis.resize(_out); for (int i = 0; i < _out; i++) { int x, y; x=getint();y=getint(); out[i].x = x; out[i].y = y; } for (int i = 0; i < _in; i++) { int x, y; x=getint();y=getint(); in[i].x = x; in[i].y = y; } for (int i = 0; i < _out; i++) { int j = i+1; if (j == _out) j = 0; dis[i] = Len(out[j] - out[i]); circ += dis[i]; } for(int i = 0; i < _out; i++) { int j = i + 1; if (j == _out) j = 0; if (righthand(in[0], in[1], out[i]) < 0 && righthand(in[0], in[1], out[j]) >= 0) { s = i; t = j; break; } } int flag = 0; while (!flag) { int nxt = t + 1; if (nxt == _out) nxt = 0; if (righthand(in[0], in[1], out[t]) >= 0 && righthand(in[0], in[1], out[nxt]) < 0) { flag = 1; } else { nowans += dis[t]; } t = nxt; } calc(0); for (int i = 1; i < _in; i++) { adjust(i); calc(i); } printf("%.15lf\n", (double)ans); return0; }
boolcheck(int k) { int lst = 0; for (int i = 1; i <= n; i++) if (s[i] == '1') { int le, ri; if (lst + 1 >= i - k + 1) { le = i - k + 1; ri = i + k; } else { le = i - k; ri = i + k - 1; } if (lst + 1 < le) return0; lst = ri; } return lst >= n; }
voidwork() { cin >> n; cin >> (s+1); int cnt = 0; for (int i = 1; i <= n; i++) if (s[i] == '1') ++cnt; if (cnt == n) { cout << 0 << '\n'; return; } int L = 1, R = n, ans = -1; while (L <= R) { int M = (L + R) >> 1; if (check(M)) { ans = M; R = M - 1; } else { L = M + 1; } } cout << ans << '\n'; } intmain() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) work(); return0; }
#include<iostream> #include<algorithm> #include<queue> using ll = longlong; usingnamespace std;
constexprint MAXN = 55; constexprint MAXM = 2505; constexpr ll lINF = 1e18; int n, m; int w[MAXM]; structEdge { int v, nxt, c; }e[MAXM]; int head[MAXN], cnt; voidaddedge(int u, int v, int c){ e[++cnt] = {v, head[u], c}; head[u] = cnt; }
int dep[MAXN], vis[MAXN], num[MAXM]; ll dist[MAXN];
voidbfs(){ vis[1] = 1; queue<int> que; que.push(1); while (!que.empty()) { int u = que.front(); que.pop(); for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v; if (vis[v]) continue; dep[v] = dep[u] + 1; vis[v] = 1; que.push(v); } } }
voiddfs(int u, ll nowans){ dist[u] = min(dist[u], nowans); for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v, c = e[i].c; if (dep[v] != dep[u] + 1) continue; num[c]++; dfs(v, nowans + num[c] * w[c]); num[c]--; } }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 1; i <= m; i++) cin >> w[i]; for (int i = 1; i <= m; i++) { int u, v, c; cin >> u >> v >> c; addedge(u, v, c); addedge(v, u, c); } bfs(); for (int i = 2; i <= n; i++) dist[i] = lINF; dfs(1, 0); for (int i = 2; i <= n; i++) cout << dist[i] << '\n'; return0; }