Exemplo n.º 1
0
    static void Main()
    {
        var n  = int.Parse(Console.ReadLine());
        var ps = new int[n].Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()).ToArray();

        var dy = ps.Select(v => v[1]).Concat(ps.Select(v => v[3])).Distinct().OrderBy(y => y).Select((y, i) => new { y, i }).ToDictionary(_ => _.y, _ => _.i);

        var xs = ps.Where(v => v[1] == v[3]).SelectMany(v => new[] { new[] { -1, Math.Min(v[0], v[2]), dy[v[1]] }, new[] { 1, Math.Max(v[0], v[2]), dy[v[1]] } }).ToArray();
        var ys = ps.Where(v => v[0] == v[2]).Select(v => new[] { 0, v[0], dy[Math.Min(v[1], v[3])], dy[Math.Max(v[1], v[3])] }).ToArray();
        var qs = xs.Concat(ys).OrderBy(q => q[1]).ThenBy(q => q[0]);

        var r  = 0L;
        var st = new ST(dy.Count);

        foreach (var q in qs)
        {
            if (q[0] == 0)
            {
                r += st.Subsum(q[2], q[3] + 1);
            }
            else
            {
                st.Add(q[2], -q[0]);
            }
        }
        Console.WriteLine(r);
    }