結果

問題 No.235 めぐるはめぐる (5)
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2016-06-18 05:38:01
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,483 ms / 10,000 ms
コード長 15,929 bytes
コンパイル時間 1,298 ms
コンパイル使用メモリ 123,792 KB
実行使用メモリ 128,092 KB
最終ジャッジ日時 2024-10-09 20:22:47
合計ジャッジ時間 11,777 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,483 ms
104,820 KB
testcase_01 AC 1,587 ms
128,092 KB
testcase_02 AC 2,046 ms
111,180 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using System.Numerics;
using Point = System.Numerics.Complex;
using Number = System.Int32;
namespace Program
{
    public class Solver
    {
        public void Solve()
        {
            var n = sc.Integer();
            var G = new HLTreeGraph(n);
            G.A = sc.Integer(n);
            G.B = sc.Integer(n);
            for (int i = 0; i < n - 1; i++)
            {
                var a = sc.Integer() - 1;
                var b = sc.Integer() - 1;
                G.AddEdge(a, b);
            }
            G.Build(0);
            var q = sc.Integer();
            for (int i = 0; i < q; i++)
            {
                if (sc.Integer() == 0)
                {
                    var x = sc.Integer() - 1;
                    var y = sc.Integer() - 1;
                    var z = sc.Integer();
                    var lca = G.GetLCA(x, y);
                    G.Update(lca, x, z);
                    G.Update(lca, y, z);
                    G.Update(lca, lca, -z);
                }
                else
                {
                    var x = sc.Integer() - 1;
                    var y = sc.Integer() - 1;
                    var lca = G.GetLCA(x, y);
                    ModInteger ans = 0;
                    ans += G.Query(lca, x);
                    ans += G.Query(lca, y);
                    ans -= G.Query(lca, lca);
                    IO.Printer.Out.WriteLine(ans);
                }
            }
        }
        public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
        static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
        static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }


    }
}
#region main
static class Ex
{
    static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
    static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") { return string.Join(st, ie); }
    static public void Main()
    {
        var solver = new Program.Solver();
        solver.Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion
#region Ex
namespace Program.IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;
    public class Printer: StreamWriter
    {
        static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
        public static Printer Out { get; set; }
        public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
        public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
        public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
        public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); }
        public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); }
    }
    public class StreamScanner
    {
        public StreamScanner(Stream stream) { str = stream; }
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read()
        {
            if (isEof) return 0;
            if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } }
            return buf[ptr++];
        }
        public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; }

        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public string ScanLine()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }
        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        public int Integer() { return (isEof) ? int.MinValue : (int)Long(); }
        public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; }
        private T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n) { return enumerate(n, Char); }
        public string[] Scan(int n) { return enumerate(n, Scan); }
        public double[] Double(int n) { return enumerate(n, Double); }
        public int[] Integer(int n) { return enumerate(n, Integer); }
        public long[] Long(int n) { return enumerate(n, Long); }
    }
}
#endregion
#region ModNumber
public partial struct ModInteger
{
    public const long Mod = (long)1e9 + 7;
    public long num;
    public ModInteger(long n) : this() { num = n % Mod; if (num < 0) num += Mod; }
    public override string ToString() { return num.ToString(); }
    public static ModInteger operator +(ModInteger l, ModInteger r) { var n = l.num + r.num; if (n >= Mod) n -= Mod; return new ModInteger() { num = n }; }
    public static ModInteger operator -(ModInteger l, ModInteger r) { var n = l.num + Mod - r.num; if (n >= Mod) n -= Mod; return new ModInteger() { num = n }; }
    public static ModInteger operator *(ModInteger l, ModInteger r) { return new ModInteger(l.num * r.num); }
    public static ModInteger operator ^(ModInteger l, long r) { return ModInteger.Pow(l, r); }
    public static implicit operator ModInteger(long n) { return new ModInteger(n); }
    public static ModInteger Pow(ModInteger v, long k)
    {
        ModInteger ret = 1;
        var n = k;
        for (; n > 0; n >>= 1, v *= v)
        {
            if ((n & 1) == 1)
                ret = ret * v;
        }
        return ret;
    }
}
#endregion
#region HLTreeGraph

public class HLTreeGraph
{
    public void Update(int from, int to, int val)
    {
        var f = go[from];
        var t = go[to];
        if (f.id == t.id)
        {
            f.seg.Add(pos[from], pos[to] + 1, val);
        }
        else
        {
            var ch = GetAncestorAt(t, f.depth + 1);
            var med = H[ch];
            f.seg.Add(pos[from], pos[med.light.from] + 1, val);
            Update(med.light.to, to, val);
        }

    }
    public ModInteger Query(int from, int to)
    {
        var f = go[from];
        var t = go[to];
        if (f.id == t.id)
        {
            var val = f.seg.Query(pos[from], pos[to] + 1);
            return val;
        }
        else
        {
            var ch = GetAncestorAt(t, f.depth + 1);
            var med = H[ch];
            var val = f.seg.Query(pos[from], pos[med.light.from] + 1);
            var val2 = Query(med.light.to, to);
            return val + val2;

        }
    }
    public int[] A, B;
    int N;
    List<Edge>[] G;
    List<Node> H = new List<Node>();
    int[] subTreeSize;
    int[] par;
    int[] pos;
    int[,] parent;
    int[] depth;

    int[] ord;
    Node[] go;
    int[,] hlpar;
    public HLTreeGraph(int n)
    {
        N = n;
        G = Enumerate(n, x => new List<Edge>());
        subTreeSize = new int[n];
        pos = new int[n];
        depth = new int[n];
        par = new int[n];
        parent = new int[20, n];
        go = new Node[n];
        ord = new int[n];
    }
    public void AddEdge(int f, int t)
    {
        G[f].Add(new Edge(f, t));
        G[t].Add(new Edge(t, f));
    }
    #region impl
    public void Build(int root)
    {
        ComputeSubTreeSize(root);
        Decomposite(new Edge(-1, root), -1, 0, 0);
        BuildLCA();
        BuildHLLCA();

    }
    public void ComputeSubTreeSize(int root)
    {
        var ord = new int[N];
        par[root] = -1;
        depth[root] = 0;
        var ptr = 0;
        var last = 0;
        ord[last++] = root;
        while (ptr < last)
        {
            var p = ord[ptr++];
            foreach (var to in G[p])
            {
                if (to.to != par[p])
                {
                    par[to.to] = p;
                    ord[last++] = to.to;
                }
            }
        }
        for (int i = N - 1; i > 0; i--)
        {
            subTreeSize[i] += 1;
            subTreeSize[par[i]] += subTreeSize[i];
        }

    }
    public void Decomposite(Edge light, int prevId, int d, int lv)
    {
        var node = new Node() { light = light, id = H.Count, par = prevId, depth = lv };
        H.Add(node);
        var prev = light.from;
        var cur = light.to;
        while (cur != prev)
        {
            var next = cur;
            var max = 0;
            depth[cur] = d;
            par[cur] = prev;
            go[cur] = node;
            pos[cur] = node.heavy.Count;
            foreach (var to in G[cur])
            {
                var t = To(to);
                if (t != prev) max = Math.Max(max, subTreeSize[t]);
            }
            foreach (var to in G[cur])
            {
                var t = To(to);
                if (t == prev) continue;
                if (max == subTreeSize[t])
                {
                    //Debug.WriteLine("{0}->{1}", cur, t);
                    max = 1 << 30;
                    next = t;
                    node.heavy.Add(to);
                }
                else Decomposite(to, node.id, d + 1, lv + 1);
            }
            prev = cur;
            cur = next;
            d++;
        }
        node.init(this);

    }
    void BuildLCA()
    {
        for (int k = 0; k < 19; k++)
            for (int v = 0; v < G.Length; v++)
                parent[k, v] = -1;
        for (int i = 0; i < G.Length; i++)
            parent[0, i] = par[i];
        for (int k = 0; k < 19; k++)
        {
            for (int v = 0; v < G.Length; v++)
                if (parent[k, v] < 0) parent[k + 1, v] = -1;
                else parent[k + 1, v] = parent[k, parent[k, v]];
        }

    }
    void BuildHLLCA()
    {
        hlpar = new int[8, H.Count];
        for (int k = 0; k < 7; k++)
            for (int v = 0; v < H.Count; v++)
                hlpar[k, v] = -1;
        for (int i = 0; i < H.Count; i++)
            hlpar[0, i] = H[i].par;
        for (int k = 0; k < 7; k++)
        {
            for (int v = 0; v < H.Count; v++)
                if (hlpar[k, v] < 0) hlpar[k + 1, v] = -1;
                else hlpar[k + 1, v] = hlpar[k, hlpar[k, v]];
        }

    }
    public int GetAncestorAt(Node v, int d)
    {
        if (v.depth < d)
            throw new Exception();
        for (int i = 0; i < 8; i++)
            if ((((v.depth - d) >> i) & 1) == 1)
                v = H[hlpar[i, v.id]];
        return v.id;
    }
    public int To(Edge t) { return t.to; }
    public int GetLCA(int u, int v)
    {
        if (depth[u] > depth[v]) { var tmp = u; u = v; v = tmp; }
        for (int k = 0; k < 20; k++)
            if ((((depth[v] - depth[u]) >> k) & 1) == 1)
                v = parent[k, v];
        if (u == v)
            return u;
        for (int i = 19; i >= 0; i--)
            if (parent[i, u] != parent[i, v])
            {
                u = parent[i, u];
                v = parent[i, v];
            }
        return parent[0, u];
    }
    public int GetAncestor(int v, int k)
    {
        var to = depth[v] - k;
        for (int i = 0; i < 20; i++)
            if ((((depth[v] - to) >> i) & 1) == 1)
                v = parent[i, v];
        return v;
    }
    public int GetAncestorAt(int v, int d)
    {
        if (depth[v] < d)
            throw new Exception();
        for (int i = 0; i < 20; i++)
            if ((((depth[v] - d) >> i) & 1) == 1)
                v = parent[i, v];
        return v;
    }
    static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
    #endregion
}
public class Node
{
    public Edge light;
    public List<Edge> heavy = new List<Edge>();
    public int par;
    public int depth;
    public int id;
    public LazyRangeAddTree seg;
    public void init(HLTreeGraph G)
    {
        var a = new int[heavy.Count + 1];
        var b = new int[heavy.Count + 1];
        a[0] += G.A[light.to];
        b[0] = G.B[light.to];
        for (int i = 0; i < heavy.Count; i++)
        {
            a[i + 1] += G.A[heavy[i].to];
            b[i + 1] += G.B[heavy[i].to];
        }
        seg = new LazyRangeAddTree(heavy.Count + 1, a, b);
    }
    public override string ToString()
    {
        var s = new List<int>();
        s.Add(light.from);
        s.Add(light.to);
        foreach (var e in heavy)
            s.Add(e.to);
        return s.AsJoinedString("->");
    }
}
#endregion
#region TreeEdge
public struct Edge
{
    public int from, to;
    public Edge(int f, int t)
    {
        from = f; to = t;
    }
    public override string ToString()
    {
        return string.Format("{0}->{1}", from, to);
    }
}
#endregion

#region LazyRangeAdd
public class LazyRangeAddTree
{
    int n;
    ModInteger[] sum, add, lazy;
    public LazyRangeAddTree(int size, int[] a, int[] b)
    {
        n = 1;
        while (n < size)
            n <<= 1;
        add = new ModInteger[n << 1];
        sum = new ModInteger[n << 1];
        for (int i = 0; i < a.Length; i++)
        {

            add[i + n] += b[i];
            sum[i + n] += a[i];
        }
        lazy = new ModInteger[n << 1];
        for (int i = n - 1; i > 0; i--)
        {
            add[i] = add[i << 1] + add[(i << 1) + 1];
            sum[i] = sum[i << 1] + sum[(i << 1) + 1];
        }
    }
    private void lazyEval(int k, int a, int b)
    {
        if (lazy[k].num == 0)
            return;
        Add(a, (a + b) >> 1, lazy[k].num, k << 1, a, (a + b) >> 1);
        Add((a + b) >> 1, b, lazy[k].num, (k << 1) + 1, (a + b) >> 1, b);
        lazy[k] = 0;
    }
    private void eval(int k)
    {
        int l = k << 1, r = (k << 1) + 1;
        sum[k] = sum[l] + sum[r];
    }
    public void Add(int a, int b, long v)
    {
        Add(a, b, v, 1, 0, n);
    }
    private void Add(int a, int b, long v, int k, int l, int r)
    {
        if (r <= a || b <= l)
            return;
        else if (a <= l && r <= b)
        {
            lazy[k] += v;
            sum[k] += add[k] * v;
        }
        else
        {
            lazyEval(k, l, r);
            Add(a, b, v, k << 1, l, (l + r) >> 1);
            Add(a, b, v, (k << 1) + 1, (l + r) >> 1, r);
            eval(k);
        }
    }
    public ModInteger Query(int a, int b) { return Query(a, b, 1, 0, n); }
    private ModInteger Query(int a, int b, int k, int l, int r)
    {
        if (r <= a || b <= l)
            return 0;
        if (a <= l && r <= b)
            return sum[k];
        else
        {
            lazyEval(k, l, r);
            var vl = Query(a, b, k << 1, l, (l + r) >> 1);
            var vr = Query(a, b, (k << 1) + 1, (l + r) >> 1, r);
            eval(k);
            return vl + vr;
        }
    }

}
#endregion
0