复习了一下线段树优化建图的姿势,在线段树上连边跑拓扑排序
这题竟然卡vector……丧病
#include <bits/stdc++.h>
#define N 1810000
using namespace std;
int s, n, m;
int pi[N], vis[N];
int lt[N], nt[N], bi[N], ci[N];
int lp[N], np[N], bp[N];
int tl;
int lb[N], rb[N], lc[N], rc[N];
int out[N], intr[N];
int Q[N], fr, bc;
inline void build(int a, int b, int c)
{
nt[++ tl] = lt[a]; lt[a] = tl; bi[tl] = b; ci[tl] = c;
np[tl] = lp[b]; lp[b] = tl; bp[tl] = a; out[a] ++;
/*
bi[a].push_back(b);
bp[b].push_back(a);
ci[a].push_back(c);
*/
//printf("%d %d %d\n", a, b, c);
}
int tot;
void dfs_build(int t, int l, int r, int x)
{
if (l > r) return;
if (l <= lb[t] && rb[t] <= r) {build(x, t, ); return;}
if (l <= rb[lc[t]]) dfs_build(lc[t], l, r, x);
if (r >= lb[rc[t]]) dfs_build(rc[t], l, r, x);
}
int build_tree(int l, int r)
{
int t = ++ tot;
lb[t] = l; rb[t] = r;
if (l != r)
{
lc[t] = build_tree(l, (l + r) / );
rc[t] = build_tree((l + r) / + , r);
build(t, lc[t], );
build(t, rc[t], );
}
else intr[l] = t;
return t;
} inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int main()
{
n = read(); s = read(); m = read();
build_tree(, n);
for (int i = ; i <= s; ++ i)
{
int p, d;
p = read(); d = read();
pi[intr[p]] = d;
} //return 0;
for (int i = ; i <= m; ++ i)
{
int l, r, k;
l = read(); r = read(); k = read(); for (int j = , p = l - ; j <= k + ; ++ j)
{
int a;
if (j <= k)
{
a = read();
build(intr[a], tot + i, );
}
else a = r + ;
if ((a - ) - (p + ) <= )
{
for (int q = p + ; q < a; ++ q)
build(tot + i, intr[q], );
}
else
dfs_build(, p + , a - , tot + i);
p = a;
}
}
for (int i = ; i <= m + tot; ++ i)
if (!out[i]) Q[bc ++] = i; while (fr != bc)
{
int hd = Q[fr ++]; vis[hd] = ;
int mx = ;
for (int i = lt[hd]; i; i = nt[i])
mx = max(mx, pi[bi[i]] + ci[i]);
if (!pi[hd])
{
if (mx > )
{
puts("NIE");
return ;
}
pi[hd] = mx;
}
else if (mx > pi[hd])
{
puts("NIE");
return ;
} for (int i = lp[hd]; i; i = np[i])
{
out[bp[i]] --;
if (!out[bp[i]])
Q[bc ++] = bp[i];
}
}
for (int i = ; i <= m + tot; ++ i)
if (!vis[i]) {puts("NIE"); return ;}
puts("TAK");
for (int i = ; i <= n; ++ i) printf("%d ", pi[intr[i]]);
}
话说是不是在主席树上也可以干一样的事情呢(手动斜眼