結果

問題 No.259 セグメントフィッシング+
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2015-08-09 20:35:16
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 862 ms / 2,000 ms
コード長 13,355 bytes
コンパイル時間 999 ms
コンパイル使用メモリ 118,772 KB
実行使用メモリ 64,088 KB
最終ジャッジ日時 2024-07-18 06:16:56
合計ジャッジ時間 16,421 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
24,416 KB
testcase_01 AC 24 ms
24,364 KB
testcase_02 AC 25 ms
26,544 KB
testcase_03 AC 24 ms
24,500 KB
testcase_04 AC 25 ms
26,252 KB
testcase_05 AC 25 ms
24,624 KB
testcase_06 AC 24 ms
22,448 KB
testcase_07 AC 24 ms
24,324 KB
testcase_08 AC 482 ms
59,876 KB
testcase_09 AC 482 ms
59,616 KB
testcase_10 AC 464 ms
59,880 KB
testcase_11 AC 481 ms
59,620 KB
testcase_12 AC 470 ms
59,860 KB
testcase_13 AC 828 ms
61,912 KB
testcase_14 AC 836 ms
62,044 KB
testcase_15 AC 842 ms
61,792 KB
testcase_16 AC 862 ms
60,012 KB
testcase_17 AC 819 ms
61,788 KB
testcase_18 AC 831 ms
60,000 KB
testcase_19 AC 831 ms
64,088 KB
testcase_20 AC 808 ms
63,960 KB
testcase_21 AC 852 ms
61,796 KB
testcase_22 AC 810 ms
59,740 KB
testcase_23 AC 199 ms
29,436 KB
testcase_24 AC 722 ms
57,824 KB
testcase_25 AC 725 ms
61,916 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.Diagnostics;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using System.Numerics;

namespace Program
{

    public class Solver
    {
        public void Solve()
        {
            var n = sc.Integer();
            var q = sc.Integer();
            var a = new long[n];
            var left = RBST.Build(a, 0, n);
            var right = RBST.Build(a, 0, n);
            var last = 0;
            for (int _ = 0; _ < q; _++)
            {

                var c = sc.Char();
                var t = sc.Integer();
                var dt = (t - last) % (2 * n);

                var x = sc.Integer();
                var y = sc.Integer();
                if (dt <= n)
                {
                    var rl1 = left.Split(dt);
                    var rl2 = right.Split(n - dt);
                    left = RBST.Merge(rl1.rst, rl2.rst.Reverse(0, rl2.rst.Count()));
                    right = RBST.Merge(rl1.lst.Reverse(0, rl1.lst.Count()), rl2.lst);
                }
                else
                {
                    dt -= n;
                    var rl1 = left.Split(dt);
                    var rl2 = right.Split(n - dt);
                    left = RBST.Merge(rl2.lst.Reverse(0, rl2.lst.Count()), rl1.lst);
                    right = RBST.Merge(rl2.rst, rl1.rst.Reverse(0, rl1.rst.Count()));
                }

                if (c == 'L')
                    left.Add(x, x + 1, y);
                else if (c == 'R')
                    right.Add(x, x + 1, y);
                else
                {
                    var l = left.Sum(x, y);
                    var r = right.Sum(x, y);
                    IO.Printer.Out.WriteLine(l + r);
                }
                last = t;
            }

        }

        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 != '-' && (b < '0' || '9' < b));
            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() { return double.Parse(Scan(), CultureInfo.InvariantCulture); }
        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 RBSTNode

public class RBSTNode
{
    public long val;
    public RBSTNode lst, rst;
    public int cnt;
    public long add;
    public long sum;
    public bool rev;
    public RBSTNode(long v) { val = v; RBST.Update(this); }
    public RBSTNode(RBSTNode l, RBSTNode r) { lst = l; rst = r; }
    public override string ToString()
    {
        return string.Format("cnt:{0}, val:{1}, add:{2}, sum:{3}, rev:{4}", cnt, val, add, sum, rev);
    }
}
static public class RBST
{
    static void Swap(ref RBSTNode l, ref RBSTNode r) { var tmp = l; l = r; r = tmp; }
    /// <summary>
    /// long配列aの[b,e)から木を構築
    /// </summary>
    static public RBSTNode Build(long[] a, int b, int e)
    {
        var n = e - b;
        if (n == 0) return null;
        var m = b + (n >> 1);
        var l = Build(a, b, m);
        var r = Build(a, m + 1, e);
        var ret = new RBSTNode(a[m]) { lst = l, rst = r };
        return Update(ret);
    }



    /// <summary>
    /// 木を配列にコピーする.
    /// </summary>
    /// <param name="n"></param>
    /// <returns></returns>
    static public long[] ToArray(this RBSTNode n)
    {
        var a = new long[Count(n)];
        CopyTo(n, a, 0, 0);
        return a;
    }
    static int CopyTo(RBSTNode n, long[] a, int it, long add)
    {
        if (n == null)
            return it;
        add += n.add;
        if (n.rev)
            Reverse(n);
        it = CopyTo(n.lst, a, it, add);
        a[it++] = n.val + add;
        it = CopyTo(n.rst, a, it, add);
        return it;
    }
    /// <summary>
    /// ノードの部分木のサイズを取得
    /// </summary>
    static public int Count(this RBSTNode n) { return n == null ? 0 : n.cnt; }
    /// <summary>
    /// 区間[l,r)の総和を取得
    /// </summary>
    static public long Sum(this RBSTNode n, int l, int r)
    {
        if (n == null || r <= 0 || Count(n) <= l) return 0;
        if (l <= 0 && Count(n) <= r)
            return n.sum;
        long ret = 0;
        n = FallDown(n);
        if (0 < r && l < Count(n.lst)) ret += Sum(n.lst, l, r);
        if (Count(n.lst) + 1 < r && l < Count(n)) ret += Sum(n.rst, l - Count(n.lst) - 1, r - Count(n.lst) - 1);
        if (l <= Count(n.lst) && Count(n.lst) < r) ret += n.val;
        //ret += n.add * (Math.Min(r, Count(n)) - Math.Max(l, 0));
        return ret;
    }
    static void Reverse(RBSTNode n)
    {
        if (n == null) return;
        Swap(ref n.lst, ref n.rst);
        n.rev = false;
        if (n.lst != null) n.lst.rev = !n.lst.rev;
        if (n.rst != null) n.rst.rev = !n.rst.rev;
    }
    /// <summary>
    /// ノードの値の更新
    /// </summary>
    static public RBSTNode Update(this RBSTNode n)
    {
        if (n == null) return null;
        n.cnt = 1;
        if (n.lst != null) n.cnt += n.lst.cnt;
        if (n.rst != null) n.cnt += n.rst.cnt;

        n.sum = n.val + Count(n) * n.add;
        if (n.lst != null) n.sum += n.lst.sum;
        if (n.rst != null) n.sum += n.rst.sum;


        return n;
    }
    /// <summary>
    /// 区間[l,r)にvを加算.
    /// </summary>
    static public RBSTNode Add(this RBSTNode n, int l, int r, long v)
    {
        if (n == null || r <= 0 || Count(n) <= l) return n;
        FallDown(n);
        if (l <= 0 && Count(n) <= r)
        {
            n.add += v;
            return Update(n);
        }
        else
        {
            if (0 < r && l < Count(n.lst))
                n.lst = Add(n.lst, l, r, v);
            if (Count(n.lst) + 1 < r && l < Count(n))
                n.rst = Add(n.rst, l - Count(n.lst) - 1, r - Count(n.lst) - 1, v);
            if (l <= Count(n.lst) && Count(n.lst) < r)
                n.val += v;
            return Update(n);
        }
    }
    /// <summary>
    /// 区間[l,r)を反転.
    /// </summary>
    static public RBSTNode Reverse(this RBSTNode n, int l, int r)
    {
        if (n == null || r <= 0 || Count(n) <= l) return n;
        FallDown(n);
        var lmr = Split(n, l, r);
        lmr[1].rev = !lmr[1].rev;
        return Merge(lmr[0], lmr[1], lmr[2]);

    }

    /// <summary>
    /// 0-indexedでk番目のノードを取得
    /// </summary>
    static public RBSTNode GetNode(this RBSTNode n, int k)
    {
        while (n != null)
        {
            n = FallDown(n);
            if (k < Count(n.lst))
                n = n.lst;
            else if (k == Count(n.lst))
                break;
            else
            {
                k -= Count(n.lst) + 1;
                n = n.rst;
            }
        }
        return n;
    }
    static RBSTNode FallDown(RBSTNode n)
    {
        if (n == null) return null;
        if (n.rev)
            Reverse(n);
        if (n.add == 0) return n;
        if (n.lst != null)
        {
            n.lst.add += n.add;
            Update(n.lst);
        }
        if (n.rst != null)
        {
            n.rst.add += n.add;
            Update(n.rst);
        }
        n.val += n.add;
        n.add = 0;
        return Update(n);
    }

    static Random rand = new Random();

    static public RBSTNode Merge(RBSTNode a, RBSTNode b)
    {
        if (a == null || b == null)
            return a ?? b;
        if (rand.Next() % (Count(a) + Count(b)) < Count(a))
        {
            a = FallDown(a);
            a.rst = Merge(a.rst, b);
            return Update(a);
        }
        else
        {
            b = FallDown(b);
            b.lst = Merge(a, b.lst);
            return Update(b);
        }
    }
    static public RBSTNode Merge(RBSTNode l, RBSTNode m, RBSTNode r)
    {
        return Merge(Merge(l, m), r);
    }
    static public RBSTNode Split(this RBSTNode n, int k)
    {
        if (n == null) return new RBSTNode(null, null);
        n = FallDown(n);
        if (k <= Count(n.lst))
        {
            var s = Split(n.lst, k);
            n.lst = s.rst;
            s.rst = Update(n);
            return s;
        }
        else
        {
            var s = Split(n.rst, k - Count(n.lst) - 1);
            n.rst = s.lst;
            s.lst = Update(n);
            return s;
        }
    }
    static public RBSTNode[] Split(RBSTNode n, int l, int r)
    {
        var p1 = RBST.Split(n, r);
        var q1 = RBST.Split(p1.lst, l);
        return new RBSTNode[] { q1.lst, q1.rst, p1.rst };
    }
    static public RBSTNode Insert(this RBSTNode n, int k, RBSTNode item)
    {
        if (n == null) return item;
        n = FallDown(n);
        if (rand.Next() % (Count(n) + Count(item)) < Count(n))
        {
            if (k <= Count(n.lst))
                n.lst = Insert(n.lst, k, item);
            else n.rst = Insert(n.rst, k - Count(n.lst) - 1, item);
            return Update(n);
        }
        else
        {
            var s = Split(n, k);
            item.lst = s.lst; item.rst = s.rst;
            return Update(item);
        }
    }
    static public RBSTNode RemoveAt(this RBSTNode n, int k)
    {
        if (n == null) return null;
        n = FallDown(n);
        if (k < Count(n.lst))
        {
            n.lst = RemoveAt(n.lst, k);
            return Update(n);
        }
        else if (k == Count(n.lst))
        {
            var nn = Merge(n.lst, n.rst);
            Isolate(n);
            return nn;
        }
        else
        {
            n.rst = RemoveAt(n.rst, k - Count(n.rst) - 1);
            return Update(n);
        }
    }
    static public RBSTNode Isolate(RBSTNode n)
    {
        if (n == null) return null;
        n.lst = n.rst = null;
        return Update(n);
    }
}


#endregion
0