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
|
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <vector> #define MAXN (50000 + 5) #define LS(dq) ((dq) << 1) #define RS(dq) (((dq) << 1) | 1) #define Gary (201314) using namespace std; const int root = 1; struct node { int zh, le, ri, lazy; }b[MAXN << 2]; struct tnode { int fa, deep, begin, end, size, hs, top; }ta[MAXN]; struct tedg { int from, to, next; }tb[MAXN << 1]; int tg[MAXN], cntb, cnta, ans[MAXN]; void adn(int from, int to) { tb[++cntb].next = tg[from]; tb[cntb].from = from, tb[cntb].to = to; tg[from] = cntb; } void init(int dq) { ta[dq].size = 1; for (int i = tg[dq]; i; i = tb[i].next) if (tb[i].to != ta[dq].fa) { ta[tb[i].to].fa = dq; ta[tb[i].to].deep = ta[dq].deep + 1; init(tb[i].to); ta[dq].size += ta[tb[i].to].size; if (ta[tb[i].to].size > ta[ta[dq].hs].size) ta[dq].hs = tb[i].to; } } void dfs(int dq) { ta[dq].begin = ++cnta; if (ta[dq].hs) ta[ta[dq].hs].top = ta[dq].top, dfs(ta[dq].hs); for (int i = tg[dq]; i; i = tb[i].next) if (tb[i].to != ta[dq].fa && tb[i].to != ta[dq].hs) ta[tb[i].to].top = tb[i].to, dfs(tb[i].to); ta[dq].end = cnta; } void js(int dq, int le, int ri) { b[dq].le = le, b[dq].ri = ri; b[dq].lazy = 0; if (le == ri) return ; int mi = (le + ri) >> 1; js(LS(dq), le, mi); js(RS(dq), mi + 1, ri); } void xg(int dq, int le, int ri, int zh) { b[dq].zh = (b[dq].zh + (ri - le + 1) * zh) % Gary; if (b[dq].le == le && b[dq].ri == ri) { b[dq].lazy += zh; return ; } int mi = (b[dq].le + b[dq].ri) >> 1; if (le > mi) xg(RS(dq), le, ri, zh); else if (ri <= mi) xg(LS(dq), le, ri, zh); else xg(LS(dq), le, mi, zh), xg(RS(dq), mi + 1, ri, zh); } int cx(int dq, int le, int ri) { if (b[dq].le == le && b[dq].ri == ri) return b[dq].zh; int mi = (b[dq].le + b[dq].ri) >> 1, re = b[dq].lazy * (ri - le + 1) % Gary; if (le > mi) re = (re + cx(RS(dq), le, ri)) % Gary; else if (ri <= mi) re = (re + cx(LS(dq), le, ri)) % Gary; else re = (re + cx(LS(dq), le, mi) + cx(RS(dq), mi + 1, ri)) % Gary; return re; } void txg(int x) { while (ta[x].top != root) { xg(root, ta[ta[x].top].begin, ta[x].begin, 1); x = ta[ta[x].top].fa; } xg(root, ta[root].begin, ta[x].begin, 1); } int tcx(int x) { int re = 0; while (ta[x].top != root) { re = (re + cx(root, ta[ta[x].top].begin, ta[x].begin)) % Gary; x = ta[ta[x].top].fa; } re = (re + cx(root, ta[root].begin, ta[x].begin)) % Gary; return re; } int main() { int n, q; vector<int> nwz[MAXN], bh[MAXN]; scanf("%d%d", &n, &q); for (int i = 2, srx; i <= n; i++) { scanf("%d", &srx); ++srx; adn(srx, i), adn(i, srx); } ta[root].fa = root, ta[root].deep = 1, ta[root].top = root; init(root), dfs(root); js(root, 1, n); for (int i = 1, srx, sry, srz; i <= q; i++) scanf("%d%d%d", &srx, &sry, &srz), nwz[srx].push_back(-srz - 1), nwz[sry + 1].push_back(srz + 1), bh[srx].push_back(i), bh[sry + 1].push_back(i); for (int i = 1; i <= n; i++) { txg(i); for (int j = 0; j < nwz[i].size(); j++) if (nwz[i][j] > 0) ans[bh[i][j]] += tcx(nwz[i][j]); else ans[bh[i][j]] = -tcx(-nwz[i][j]); } for (int i = 1; i <= q; i++) while (ans[i] < 0) ans[i] = (ans[i] + Gary) % Gary; for (int i = 1; i <= q; i++) printf("%d\n", ans[i] % Gary); return 0; }
|