inlinevoidshuruyijichushihua(){ cin >> n >> m; for (int i = 1; i <= n; ++ i) cin >> a[i]; }
inlinevoidcaozuoyifan(){ int h = 1, t = 0; for (int i = 1; i <= n; ++ i) { while (h <= t && q[h] + m <= i) ++ h; while (h <= t && a[i] < a[q[t]]) -- t; q[++ t] = i; if (i >= m) cout << a[q[h]] << ' '; } cout << '\n'; memset(q, 0, sizeof q); for (int i = 1; i <= n; ++ i) { while (h <= t && q[h] + m <= i) ++ h; while (h <= t && a[i] > a[q[t]]) -- t; q[++ t] = i; if (i >= m) cout << a[q[h]] << ' '; } }
for (int i = 1; i <= n; ++ i) if (!rudu[i]) q.push(i);
while(q.size()) {
int now = q.front(); q.pop();
for (int i = head[now]; i; i = e[i].nxt) {
int y = e[i].to;
-- rudu[y];
if (!rudu[y]) q.push(y);
}
}
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> usingnamespace std; constint N = 1e4 + 10, M = 2e4 + 10, Q = 5e4 + 10; int fa[N],power[N],ans[Q]; map<pair<int, int>, int> mp; //离散存储 int n, m, q; structNode { int flag; //0表示query u,1表示destory u-v int u, v; voidsetNode(int flag, int u, int v){ this->flag = flag, this->u = u, this->v = v; } }ques[Q];
structNode2 { int flag; int u, v; }edg[M]; //保存两星球中的通道,flag表示存在否
intfind(int x) { if (fa[x] == -1) return x; return fa[x] = find(fa[x]); } voidunite(int u, int v) { int fu = find(u); int fv = find(v); if (fu == fv)return; if (power[fu] > power[fv] || power[fu] == power[fv] && fu<fv) fa[fv] = fu; else fa[fu] = fv; } voidinit() { memset(fa, -1, sizeof(fa)); for (int i = 0; i < m; i++) if (edg[i].flag) unite(edg[i].u, edg[i].v); } intmain() { int flag = 0; //控制连续的测试数据间隔空行 while (~scanf("%d", &n)) { mp.clear(); for (int i = 0; i < n; i++)scanf("%d", &power[i]); scanf("%d", &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); if (u > v)swap(u, v); mp[make_pair(u, v)] = i; edg[i].flag = 1; //存在边 edg[i].u = u, edg[i].v = v; } int q; scanf("%d", &q); for (int i = 0; i < q; i++) { char str[10]; scanf("%s", str); if (str[0] == 'q') { int s; scanf("%d", &s); ques[i].setNode(0, s, -1); } else { int u, v; scanf("%d%d", &u, &v); if (u > v)swap(u, v); ques[i].setNode(1, u, v); int tmp = mp[make_pair(ques[i].u, ques[i].v)]; edg[tmp].flag = 0; //逆过来看,该边不存在 } } init(); //初始化边集 for (int i = q-1; i >= 0; i--) { if (ques[i].flag) //destory unite(ques[i].u, ques[i].v); else { int tmp = find(ques[i].u); if (power[tmp] > power[ques[i].u]) ans[i] = tmp; else ans[i] = -1; } } if (flag) printf("\n"); else flag = 1; for(int i=0;i<q;i++) if(ques[i].flag==0)printf("%d\n", ans[i]); } return0; }
h = 1, t = 0;
for (int i = 1; i <= n; ++ i) {
while (h <= t && h+k <= i) ++ h;
while (h <= t && a[i] < a[q[t]]) -- t;
q[++ t] = i;
if (i >= k) cout << a[q[h]] << ' ';
}
puts("");
h = 1, t = 0;
memset (q, 0, sizeof q);
for (int i = 1; i <= n; ++ i) {
while (h <= t && h+k <= i) ++ h;
while (h <= t && a[i] > a[q[t]]) -- t;
q[++ t] = i;
if (i >= k) cout << a[q[h]] << ' ';
}