inline ll read(){ ll res = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); } while (ch <= '9' && ch >= '0') { res = (res << 3) + (res << 1) + ch - '0'; ch = getchar(); } return f ? (~res + 1) : res; }
int ma[102][102]; structnode { int x1, x2; char c; } a[N];
int n, m; int cntt;
inlinebooldfs(int x, int y, int cnt){ if (cnt == cntt) return1; if (a[cnt].c == 'R') { for (int i = 0; i < a[cnt].x1; ++i) if (y + i >= m || ma[x][y + i]) return0;
for (int i = a[cnt].x1; i <= a[cnt].x2; ++i) { if (y + i >= m || ma[x][y + i]) break; if (dfs(x, y + i, cnt + 1)) return1; } } if (a[cnt].c == 'L') { for (int i = 0; i < a[cnt].x1; ++i) if (y - i < 0 || ma[x][y - i]) return0;
for (int i = a[cnt].x1; i <= a[cnt].x2; ++i) { if (y - i < 0 || ma[x][y - i]) break; if (dfs(x, y - i, cnt + 1)) return1; } } if (a[cnt].c == 'U') { for (int i = 0; i < a[cnt].x1; ++i) if (x - i < 0 || ma[x - i][y]) return0; for (int i = a[cnt].x1; i <= a[cnt].x2; ++i) { if (x - i < 0 || ma[x - i][y]) break; if (dfs(x - i, y, cnt + 1)) return1; } } if (a[cnt].c == 'D') { for (int i = 0; i < a[cnt].x1; ++i) if (x + i >= n || ma[x + i][y]) return0; for (int i = a[cnt].x1; i <= a[cnt].x2; ++i) { if (x + i >= n || ma[x + i][y]) break; if (dfs(x + i, y, cnt + 1)) return1; } } return0; }
intmain(){ int t, x, y; cin >> t; while (t--) { cntt = 0; cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> ma[i][j]; } } while (1) { cin >> x >> y; if (!x && !y) break; a[cntt].x1 = x; a[cntt].x2 = y; cin >> a[cntt++].c; } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (!ma[i][j] && dfs(i, j, 0)) ans++; } } cout << ans << "\n";
inline ll read(){ ll res = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); } while (ch <= '9' && ch >= '0') { res = (res << 3) + (res << 1) + ch - '0'; ch = getchar(); } return f ? (~ res + 1) : res; }
int a[1000]; intmain(){ int n, m; n = read(), m = read(); if (n == 1 && m == 0){ puts("0 0"); return0; } if (!m || n * 9 < m){ puts("-1 -1"); return0; }
int s1 = m; a[1] = 1; s1 -= a[1]; for (int i = n; i >= 2; --i){ if (s1 >= 9){ a[i] = 9; s1 -= 9; } else{ a[i] = s1; s1 = 0; } // cout << s1 << "\n"; } a[1] += s1; for (int i = 1; i <= n; ++i) cout << a[i]; putchar(' '); s1 = m; ms(a, 0); a[1] = min(9, s1); s1 -= a[1]; for (int i = 2; i <= n; ++i){ if (s1 >= 9){ a[i] = 9; s1 -= 9; } else{ a[i] = s1; s1 = 0; } } for (int i = 1; i <= n; ++i) cout << a[i]; putchar(10);
inline ll read(){ ll res = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); } while (ch <= '9' && ch >= '0') { res = (res << 3) + (res << 1) + ch - '0'; ch = getchar(); } return f ? (~res + 1) : res; }
structedge { int v, w, ne; } ed[N];
int head[N], vis[N], dis[N]; int cnt; int tim[N]; inlinevoidinit(){ ms(head, -1); ms(vis, 0); ms(dis, INF); ms(tim, 0); cnt = 0; }
inlinevoidadd(int u, int v, int w){ ed[cnt] = {v, w, head[u]}; head[u] = cnt++; }
int n, m;
inlineboolspfa(int u){ dis[u] = 0; queue<int>q; q.push(u); while(!q.empty()){ u = q.front(); q.pop(); vis[u] = 0; tim[u]++; if (tim[u] > n + 1) return0; // 注意这里是大于点的个数 for (int i = head[u]; ~i; i = ed[i].ne){ int v = ed[i].v, w = ed[i].w; if (dis[v] > dis[u] + w){ dis[v] = dis[u] + w; if (!vis[v]){ q.push(v), vis[v] = 1; } } } } return1; }
int a, b, d; char c[10];
intmain(){ while (~scanf("%d", &n) && n) { m = read(); init(); while (m--) { a = read(), b = read(); scanf("%s", c); d = read(); if (c[0] == 'g') add(b + a, a - 1, - d - 1); elseif (c[0] == 'l') add(a - 1, b + a, d - 1); } for (int i = 0; i <= n; ++i){ add(n + 1, i, 0); } if (spfa(n + 1)) puts("lamentable kingdom"); elseputs("successful conspiracy"); } return0; }
inline ll read(){ ll res = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); } while (ch <= '9' && ch >= '0') { res = (res << 3) + (res << 1) + ch - '0'; ch = getchar(); } return f ? (~ res + 1) : res; }
structnode{ int a, b; }m[5003]; boolcmp(node x, node y){ if (x.a != y.a) return x.a < y.a; return x.b < y.b; } intmain(){ int n; n = read(); for (int i = 1; i <= n; ++i){ m[i].a = read(), m[i].b = read(); } sort(m + 1, m + 1 + n, cmp); int last = min(m[1].a, m[1].b); for (int i = 2; i <= n; ++i){ if (m[i].b >= last) last = m[i].b; else last = m[i].a; } cout << last << "\n"; return0; }
inline ll read(){ ll res = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); } while (ch <= '9' && ch >= '0') { res = (res << 3) + (res << 1) + ch - '0'; ch = getchar(); } return f ? (~res + 1) : res; }
int bs[1003], ds[1003], st[1003]; int n, x; vector<int>ve[1003];
intmain(){ while(~scanf("%d", &n)){ for (int i = 1; i <= n; ++i) bs[read()] = i; for (int i = 1; i <= n; ++i) ds[i] = read(); int po = 1; st[po] = ds[1]; st[++po] = ds[2]; ve[ds[1]].push_back(ds[2]); int cnt = 3; while(cnt <= n){ if (bs[ds[cnt]] > bs[st[po]] + 1){ ve[st[po]].push_back(ds[cnt]); st[++po] = ds[cnt]; cnt++; } else po--; } for (int i = 1; i <= n; ++i){ cout << i << ": "; if (!ve[i].empty()){ for (int j = 0; j < ve[i].size() - 1; ++j){ cout << ve[i][j] << " "; } cout << ve[i].back(); } putchar(10); ve[i].clear(); }
}
return0; }
4. 后继
比赛的时候前面思想都想到了, 就是没想到用栈处理, 难受qaq
K HYSBZ 1036 lzwの树
1. 题目大意
一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w。我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身