結果

問題 No.877 Range ReLU Query
ユーザー keymoonkeymoon
提出日時 2019-09-06 22:09:16
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 554 ms / 2,000 ms
コード長 3,566 bytes
コンパイル時間 2,528 ms
コンパイル使用メモリ 112,000 KB
実行使用メモリ 47,868 KB
最終ジャッジ日時 2024-11-08 10:04:34
合計ジャッジ時間 9,770 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
19,712 KB
testcase_01 AC 50 ms
20,224 KB
testcase_02 AC 50 ms
20,352 KB
testcase_03 AC 51 ms
20,608 KB
testcase_04 AC 48 ms
19,968 KB
testcase_05 AC 45 ms
19,840 KB
testcase_06 AC 49 ms
20,352 KB
testcase_07 AC 49 ms
20,480 KB
testcase_08 AC 51 ms
20,480 KB
testcase_09 AC 48 ms
20,096 KB
testcase_10 AC 49 ms
20,480 KB
testcase_11 AC 525 ms
47,028 KB
testcase_12 AC 471 ms
44,704 KB
testcase_13 AC 385 ms
41,856 KB
testcase_14 AC 401 ms
41,344 KB
testcase_15 AC 554 ms
47,600 KB
testcase_16 AC 532 ms
47,868 KB
testcase_17 AC 550 ms
46,452 KB
testcase_18 AC 540 ms
46,248 KB
testcase_19 AC 529 ms
46,352 KB
testcase_20 AC 546 ms
47,328 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.IO;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

public static class P
{
    public static void Main()
    {
        var nq = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
        //クエリ先読み昇順ソート
        SegmentTree<long> rsq = new SegmentTree<long>(nq[0], 0, (x, y) => x + y);
        SegmentTree<long> rcq = new SegmentTree<long>(nq[0], 0, (x, y) => x + y);
        Stack<int> stack = new Stack<int>(a.Select((Elem, Index) => new { Elem, Index }).OrderBy(x => x.Elem).Select(x => x.Index));
        long[] res = new long[nq[1]];
        //大きいクエリから降ってくるので、できるところから追加
        foreach (var item in Enumerable.Range(0, nq[1]).Select(Index => new { Index, Elem = Console.ReadLine().Split().Select(int.Parse).ToArray() }).OrderByDescending(x => x.Elem[3]))
        {
            var l = item.Elem[1] - 1;
            var r = item.Elem[2] - 1;
            var x = item.Elem[3];
            while (stack.Count != 0 && x <= a[stack.Peek()])
            {
                var ind = stack.Pop();
                rsq[ind] = a[ind];
                rcq[ind] = 1;
            }

            res[item.Index] = rsq.Query(l, r) - rcq.Query(l, r) * x;
        }
        Console.WriteLine(string.Join("\n", res));
    }
}

class SegmentTree<T>
{
    public int Size { get; private set; }
    T IdentityElement;
    T[] Data;
    Func<T, T, T> Merge;
    int LeafCount;

    public SegmentTree(int size, T identityElement, Func<T, T, T> merge) : this(Enumerable.Repeat(identityElement, size).ToArray(), identityElement, merge) { }

    public SegmentTree(T[] elems, T identityElement, Func<T, T, T> merge)
    {
        Size = elems.Length;
        Merge = merge;
        IdentityElement = identityElement;
        LeafCount = 1;
        while (LeafCount < elems.Length) LeafCount <<= 1;

        Data = new T[LeafCount << 1];
        elems.CopyTo(Data, LeafCount);
        for (int i = LeafCount - 1; i >= 1; i--) Data[i] = Merge(Data[i << 1], Data[(i << 1) + 1]);
    }

    public T this[int index]
    {
        get { return Data[LeafCount + index]; }
        set { Update(index, value); }
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Update(int i, T x)
    {
        i += LeafCount;
        Data[i] = x;
        while (0 < (i >>= 1)) Data[i] = Merge(Data[i << 1], Data[(i << 1) + 1]);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Operate(int i, T x)
    {
        i += LeafCount;
        Data[i] = Merge(Data[i], x);
        while (0 < (i >>= 1)) Data[i] = Merge(Data[i << 1], Data[(i << 1) + 1]);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Query(int l, int r)
    {
        l += LeafCount; r += LeafCount;
        T lRes = IdentityElement, rRes = IdentityElement;
        while (l <= r)
        {
            if ((l & 1) == 1) lRes = Merge(lRes, Data[l]);
            if ((r & 1) == 0) rRes = Merge(Data[r], rRes);
            l = (l + 1) >> 1;
            r = (r - 1) >> 1;
        }
        return Merge(lRes, rRes);
    }
}
0