1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| #include <algorithm> #include <array> #include <cstring> #include <iostream> #include <queue> #include <vector>
using namespace std; const int N = 4e5 + 5; #define meow(args...) fprintf(stderr, args) int S, T; namespace NetworkFlow { const int INF = 0x3f3f3f3f; struct Graph { struct Node { int v, w, nxt; }; vector<Node> edge; vector<int> head; Graph() { } Graph(int n) : head(n, -1) { } void resize(int n) { head.resize(n); head.assign(n, -1); } void add(int u, int v, int w) { edge.emplace_back(Node{v, w, head[u]}); head[u] = edge.size() - 1; } void add_flow(int u, int v, int w) { add(u, v, w); add(v, u, 0); } }; int level[N]; bool bfs(int S, int T, const Graph &G) { memset(level, 0, sizeof(level)); level[T] = 1; std::queue<int> q; q.push(T); while (!q.empty()) { int now = q.front(); q.pop(); for (int i = G.head[now]; ~i; i = G.edge[i].nxt) { int v = G.edge[i].v; if (!level[v] && G.edge[i ^ 1].w) { level[v] = level[now] + 1; q.push(v); if (v == S) { return level[S]; } } } } return level[S]; } vector<int> cur; int dfs(int x, int T, int maxflow, Graph &G) { if (x == T) { return maxflow; } int res = 0; for (int i = cur[x]; ~i && res < maxflow; i = G.edge[i].nxt) { cur[x] = i; int v = G.edge[i].v; if (G.edge[i].w && level[v] == level[x] - 1) { int x = dfs(v, T, std::min(G.edge[i].w, maxflow - res), G); if (x) { G.edge[i].w -= x; G.edge[i ^ 1].w += x; res += x; } } } if (res < maxflow) { level[x] = -1; } return res; } int MaxFlow(const int S, const int T, const Graph &G) { cur.resize(G.head.size()); Graph tmpG = G; int res = 0; while (bfs(S, T, tmpG)) { cur.assign(tmpG.head.begin(), tmpG.head.end()); int x; while (x = dfs(S, T, INF, tmpG)) { res += x; } } return res; } } array<int, N> id, pos; NetworkFlow::Graph G; namespace KDT { using NetworkFlow::INF; struct Point { int p[2]; const int &operator[](const int &x) const { return p[x]; } int &operator[](const int &x) { return p[x]; } } p[N]; struct Rectangle { Point l, r; }; struct Node { Rectangle range; Point p; int id; int ls, rs; } tr[N]; void pushup(int x) { for (int i = 0; i < 2; i++) { tr[x].range.l[i] = tr[x].range.r[i] = tr[x].p[i]; if (tr[x].ls) { tr[x].range.l[i] = min(tr[x].range.l[i], tr[tr[x].ls].range.l[i]); tr[x].range.r[i] = max(tr[x].range.r[i], tr[tr[x].ls].range.r[i]); } if (tr[x].rs) { tr[x].range.l[i] = min(tr[x].range.l[i], tr[tr[x].rs].range.l[i]); tr[x].range.r[i] = max(tr[x].range.r[i], tr[tr[x].rs].range.r[i]); } } } int tot; void build(int rt, int l, int r, bool d) { id[rt] = ++tot; if (l == r) { tr[rt].range.l[0] = tr[rt].range.r[0] = p[l].p[0]; tr[rt].range.l[1] = tr[rt].range.r[1] = p[l].p[1]; pos[l] = id[rt]; return; } const int mid = l + r >> 1; std::nth_element(p + l, p + mid, p + r, [&](const Point &A, const Point &B) -> bool { return A[d] < B[d]; }); build(rt << 1, l, mid, d ^ 1); build(rt << 1 | 1, mid + 1, r, d ^ 1); G.add_flow(id[rt], id[rt << 1], INF); G.add_flow(id[rt], id[rt << 1 | 1], INF); tr[rt].ls = rt * 2; tr[rt].rs = rt * 2 + 1; for (int i = 0; i < 2; i++) { tr[rt].range.l[i] = min(tr[tr[rt].ls].range.l[i], tr[tr[rt].rs].range.l[i]); tr[rt].range.r[i] = max(tr[tr[rt].ls].range.r[i], tr[tr[rt].rs].range.r[i]); } } bool In(const Rectangle &A, const Rectangle &B) { return (A.l[0] <= B.l[0] && A.l[1] <= B.l[1] && A.r[0] >= B.r[0] && A.r[1] >= B.r[1]); }
bool At(const Rectangle &A, const Rectangle &B) { return !(A.l[0] > B.r[0] || A.r[0] < B.l[0] || A.l[1] > B.r[1] || A.r[1] < B.l[1]); } void link(int rt, const Rectangle &A, int pos) { if (In(A, tr[rt].range)) { G.add_flow(pos, id[rt], INF); return; } if (At(A, tr[tr[rt].ls].range)) { link(tr[rt].ls, A, pos); } if (At(A, tr[tr[rt].rs].range)) { link(tr[rt].rs, A, pos); } } } struct Graph { vector<int> edge[N]; void add(int u, int v) { edge[u].push_back(v); } } Tr; int n, m; array<int, N> st, ed, p; int tim; void dfs(int x, const Graph &G) { st[x] = ++tim; KDT::p[tim].p[1] = p[x]; KDT::p[tim].p[0] = tim; for (auto v : G.edge[x]) { dfs(v, G); } ed[x] = tim; } int root; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 2; i <= n; i++) { int fa; cin >> fa; Tr.add(fa, i); } for (int i = 1; i <= n; i++) { cin >> p[i]; } dfs(1, Tr); G.resize(max(5 * n, 10000)); KDT::build(1, 1, n, 0); S = ++KDT::tot; T = ++KDT::tot; for (int i = 1; i <= n; i++) { G.add_flow(pos[i], T, 1); } while (m--) { int l, r, d, mx; cin >> l >> r >> d >> mx; KDT::Rectangle nya; nya.l[1] = l; nya.r[1] = r; nya.l[0] = st[d]; nya.r[0] = ed[d]; ++KDT::tot; KDT::link(1, nya, KDT::tot); G.add_flow(S, KDT::tot, mx); } cout << NetworkFlow::MaxFlow(S, T, G) << endl; cout.flush(); }
|