EntropyIncreaser

紫荆花之恋

Apr 9th, 2018
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.31 KB | None | 0 0
  1. #include <ctime>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5.  
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. #include <set>
  10.  
  11. #define LOG(FMT...) // fprintf(stderr, FMT)
  12.  
  13. using namespace std;
  14.  
  15. typedef long long ll;
  16.  
  17. struct Edge {
  18.     int v, w;
  19.     Edge* next;
  20. };
  21.  
  22. const int N = 100010, LGN = 20, LGAN = 47;
  23.  
  24. struct Treap {
  25.     struct Node {
  26.         static Node* reuse[N * LGAN];
  27.         static int rcnt;
  28.  
  29.         ll v;
  30.         int cnt, rnd;
  31.         union {
  32.             struct {
  33.                 Node *ls, *rs;
  34.             };
  35.             Node* ch[2];
  36.         };
  37.         Node* prt;
  38.  
  39.         inline void release() {
  40.             LOG("REL %d\n", rcnt + 1);
  41.             reuse[rcnt++] = this;
  42.             if (ls)
  43.                 ls->release();
  44.             if (rs)
  45.                 rs->release();
  46.         }
  47.  
  48.         inline bool relation() {
  49.             return this == prt->rs;
  50.         }
  51.  
  52.         inline void update() {
  53.             cnt = 1;
  54.             if (ls)
  55.                 cnt += ls->cnt;
  56.             if (rs)
  57.                 cnt += rs->cnt;
  58.         }
  59.  
  60.         inline void rotate() {
  61.             Node* p = prt;
  62.             bool f = relation();
  63.             if (prt->prt)
  64.                 prt->prt->ch[prt->relation()] = this;
  65.             prt = p->prt;
  66.             p->ch[f] = ch[!f];
  67.             if (ch[!f])
  68.                 ch[!f]->prt = p;
  69.             p->prt = this;
  70.             ch[!f] = p;
  71.             p->update();
  72.             update();
  73.         }
  74.  
  75.         inline int count(ll key) {
  76.             Node* p = this;
  77.             int ret = 0;
  78.             while (p) {
  79.                 if (p->v <= key) {
  80.                     ret += 1;
  81.                     if (p->ls)
  82.                         ret += p->ls->cnt;
  83.                     p = p->rs;
  84.                 } else
  85.                     p = p->ls;
  86.             }
  87.             return ret;
  88.         }
  89.  
  90.         inline void insert(Node* p) {
  91.             while (true) {
  92.                 if (p->v <= v) {
  93.                     if (p->rs)
  94.                         p = p->rs;
  95.                     else {
  96.                         p->rs = this;
  97.                         prt = p;
  98.                         break;
  99.                     }
  100.                 } else {
  101.                     if (p->ls)
  102.                         p = p->ls;
  103.                     else {
  104.                         p->ls = this;
  105.                         prt = p;
  106.                         break;
  107.                     }
  108.                 }
  109.             }
  110.             while (p) {
  111.                 p->update();
  112.                 p = p->prt;
  113.             }
  114.         }
  115.  
  116.         void ord() {
  117.             if (ls)
  118.                 ls->ord();
  119.             LOG("%lld ", v);
  120.             if (rs)
  121.                 rs->ord();
  122.         }
  123.     };
  124.  
  125.     Node* root;
  126.  
  127.     ~Treap() {
  128.         if (root)
  129.             root->release();
  130.     }
  131.  
  132.     inline void clear() {
  133.         if (root)
  134.             root->release();
  135.         root = NULL;
  136.     }
  137.  
  138.     inline void insert(ll v) {
  139.         static Node pool[N * LGAN];
  140.         static Node* ptop = pool;
  141.         Node* p;
  142.         if (Node::rcnt != 0)
  143.             p = Node::reuse[--Node::rcnt];
  144.         else
  145.             p = ptop++;
  146.         p->v = v;
  147.         p->cnt = 1;
  148.         p->rnd = rand();
  149.         p->ls = p->rs = p->prt = NULL;
  150.         if (!root)
  151.             root = p;
  152.         else {
  153.             p->insert(root);
  154.             while (p->prt && p->rnd < p->prt->rnd)
  155.                 p->rotate();
  156.             if (!p->prt)
  157.                 root = p;
  158.         }
  159.     }
  160.  
  161.     inline int count(ll v) {
  162.         if (!root)
  163.             return 0;
  164.         return root->count(v);
  165.     }
  166.  
  167.     void ord() {
  168.         root->ord();
  169.         LOG("\n");
  170.     }
  171. };
  172.  
  173. const double ALPHA = 0.78;
  174. const int P = 1e9;
  175.  
  176. int n, ql, qr;
  177. ll ans;
  178. int r[N], prt[N], sub[N], que[N];
  179. ll divdis[N];
  180. bool vis[N], cut[N];
  181. int gdep[N];
  182. ll gdis[N];
  183. int gprt[N][LGN];
  184. Edge* g[N];
  185. Treap t[N], t2[N];
  186. Treap save;
  187. set<int> ch[N];
  188. Treap::Node* Treap::Node::reuse[N * LGAN];
  189. int Treap::Node::rcnt;
  190.  
  191. void addEdge(int u, int v, int w);
  192. int lca(int u, int v);
  193. ll dis(int u, int v);
  194. int divide(int u);
  195. void bfs(int u);
  196.  
  197. int main() {
  198. //    freopen("test.in", "r", stdin);
  199. //    freopen("test.out", "w", stdout);
  200.     srand(time(NULL));
  201.     int tc, ig;
  202.     scanf("%d%d%d%d%d", &tc, &n, &ig, &ig, &r[1]);
  203.     fill(cut + 1, cut + n + 1, true);
  204.     puts("0");
  205.     sub[1] = 1;
  206.     t[1].insert(-r[1]);
  207.     for (int u = 2; u <= n; ++u) {
  208.         int v, w, xo = ans % P;
  209.         scanf("%d%d%d", &v, &w, &r[u]);
  210.         v ^= xo;
  211.         prt[u] = v;
  212.         addEdge(u, v, w);
  213.         addEdge(v, u, w);
  214.         ch[v].insert(u);
  215.         gdep[u] = gdep[v] + 1;
  216.         gdis[u] = gdis[v] + w;
  217.         gprt[u][0] = v;
  218.         for (int i = 1; i < LGN; ++i)
  219.             gprt[u][i] = gprt[gprt[u][i - 1]][i - 1];
  220.         for (int uu = u; uu; uu = prt[uu]) {
  221.             ++sub[uu];
  222.             ll k = r[u] - dis(u, uu);
  223.             ans += t[uu].count(k);
  224.             t[uu].insert(-k);
  225.             if (prt[uu]) {
  226.                 ll k2 = r[u] - dis(u, prt[uu]);
  227.                 ans -= t2[uu].count(k2);
  228.                 t2[uu].insert(-k2);
  229.             }
  230.         }
  231.         printf("%lld\n", ans);
  232.         int scape = 0;
  233.         for (int uu = u; prt[uu]; uu = prt[uu]) {
  234.             if (sub[uu] >= sub[prt[uu]] * ALPHA)
  235.                 scape = prt[uu];
  236.         }
  237.         if (!scape)
  238.             continue;
  239.         int ori = u;
  240.         LOG("SCAPE %d %d\n", scape, sub[scape]);
  241.         int sprt = prt[scape];
  242.         save.root = t2[scape].root;
  243.         t2[scape].root = NULL;
  244.         que[ql = 0] = scape;
  245.         qr = 1;
  246.         while (ql < qr) {
  247.             u = que[ql++];
  248.             cut[u] = false;
  249.             for (set<int>::iterator it = ch[u].begin(); it != ch[u].end(); ++it)
  250.                 que[qr++] = *it;
  251.             ch[u].clear();
  252.             t[u].clear();
  253.             t2[u].clear();
  254.         }
  255.         u = divide(scape);
  256.         if (sprt) {
  257.             prt[u] = sprt;
  258.             ch[sprt].erase(scape);
  259.             ch[sprt].insert(u);
  260.             t2[u].root = save.root;
  261.             save.root = NULL;
  262.         }
  263.         u = ori;
  264.         fflush(stdout);
  265.     }
  266.     return 0;
  267. }
  268.  
  269. inline void bfs(int u) {
  270.     que[ql = 0] = u;
  271.     qr = 1;
  272.     while (ql < qr) {
  273.         u = que[ql++];
  274.         vis[u] = true;
  275.         for (Edge* p = g[u]; p; p = p->next)
  276.             if (!vis[p->v] && !cut[p->v])
  277.                 que[qr++] = p->v;
  278.     }
  279. }
  280.  
  281. int divide(int u) {
  282.     static int mex[N];
  283.     bfs(u);
  284.     int div = que[qr - 1];
  285.     for (int i = qr - 1; i >= 0; --i) {
  286.         u = que[i];
  287.         vis[u] = false;
  288.         sub[u] = 1;
  289.         mex[u] = 0;
  290.         for (Edge* p = g[u]; p; p = p->next)
  291.             if (!vis[p->v] && !cut[p->v]) {
  292.                 sub[u] += sub[p->v];
  293.                 mex[u] = max(mex[u], sub[p->v]);
  294.             }
  295.         mex[u] = max(mex[u], qr - sub[u]);
  296.         if (mex[u] < mex[div])
  297.             div = u;
  298.     }
  299.     cut[div] = true;
  300.     sub[div] = qr;
  301.     divdis[div] = 0;
  302.     t[div].insert(-r[div]);
  303.     for (Edge* p = g[div]; p; p = p->next)
  304.         if (!cut[p->v]) {
  305.             divdis[p->v] = p->w;
  306.             bfs(p->v);
  307.             Treap cur;
  308.             cur.root = NULL;
  309.             for (int i = 0; i < qr; ++i) {
  310.                 int v = que[i];
  311.                 vis[v] = false;
  312.                 t[div].insert(divdis[v] - r[v]);
  313.                 cur.insert(divdis[v] - r[v]);
  314.                 for (Edge* q = g[v]; q; q = q->next)
  315.                     if(vis[q->v] && !cut[q->v])
  316.                         divdis[q->v] = divdis[v] + q->w;
  317.             }
  318.             int vdiv = divide(p->v);
  319.             ch[div].insert(vdiv);
  320.             prt[vdiv] = div;
  321.             t2[vdiv].root = cur.root;
  322.             cur.root = NULL;
  323.         }
  324.     prt[div] = 0;
  325.     return div;
  326. }
  327.  
  328. inline void addEdge(int u, int v, int w) {
  329.     static Edge pool[N * 2];
  330.     static Edge* p = pool;
  331.     p->v = v;
  332.     p->w = w;
  333.     p->next = g[u];
  334.     g[u] = p;
  335.     ++p;
  336. }
  337.  
  338. inline int lca(int u, int v) {
  339.     if (gdep[u] < gdep[v])
  340.         swap(u, v);
  341.     for (int i = LGN - 1; i >= 0; --i)
  342.         if (gdep[u] - (1 << i) >= gdep[v])
  343.             u = gprt[u][i];
  344.     if (u == v)
  345.         return u;
  346.     for (int i = LGN - 1; i >= 0; --i)
  347.         if (gprt[u][i] != gprt[v][i]) {
  348.             u = gprt[u][i];
  349.             v = gprt[v][i];
  350.         }
  351.     return gprt[u][0];
  352. }
  353.  
  354. inline ll dis(int u, int v) {
  355.     return gdis[u] + gdis[v] - 2 * gdis[lca(u, v)];
  356. }
Advertisement
Add Comment
Please, Sign In to add comment