結果

問題 No.877 Range ReLU Query
ユーザー aru aruaru aru
提出日時 2020-11-11 00:05:12
言語 Go
(1.22.1)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 2,461 bytes
コンパイル時間 11,975 ms
コンパイル使用メモリ 229,068 KB
実行使用メモリ 13,784 KB
最終ジャッジ日時 2024-04-25 22:26:54
合計ジャッジ時間 14,729 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 122 ms
11,592 KB
testcase_12 AC 107 ms
11,728 KB
testcase_13 AC 93 ms
9,036 KB
testcase_14 AC 80 ms
9,164 KB
testcase_15 AC 116 ms
13,780 KB
testcase_16 AC 112 ms
13,784 KB
testcase_17 AC 118 ms
13,784 KB
testcase_18 AC 117 ms
13,784 KB
testcase_19 AC 111 ms
13,780 KB
testcase_20 AC 115 ms
13,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

type BIT struct {
	v []int
}

func newBIT(n int) *BIT {
	b := new(BIT)
	b.v = make([]int, n)
	return b
}
func (b BIT) sum(a int) int {
	ret := 0
	for i := a + 1; i > 0; i -= i & -i {
		ret += b.v[i-1]
	}
	return ret
}
func (b BIT) rangeSum(x, y int) int {
	if y == 0 {
		return 0
	}
	y--
	if x == 0 {
		return b.sum(y)
	} else {
		return b.sum(y) - b.sum(x-1)
	}
}
func (b BIT) add(a, w int) {
	n := len(b.v)
	for i := a + 1; i <= n; i += i & -i {
		b.v[i-1] += w
	}
}

type data struct {
	t       int
	i       int
	x, l, r int
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N, Q := getI(), getI()
	d := make([]data, 0, N+Q)
	for i := 0; i < N; i++ {
		d = append(d, data{0, i, getI(), 0, 0})
	}
	for i := 0; i < Q; i++ {
		_, l, r, x := getI(), getI(), getI(), getI()
		d = append(d, data{1, i, x, l - 1, r})
	}

	sort.Slice(d, func(i, j int) bool {
		// if d[i].x == d[j].x {
		// 	return d[i].t > d[j].t
		// }
		return d[i].x > d[j].x
	})

	b0 := newBIT(N)
	b1 := newBIT(N)
	ans := make([]int, Q)
	for _, e := range d {
		if e.t == 0 {
			b0.add(e.i, e.x)
			b1.add(e.i, 1)
		} else {
			ans[e.i] = b0.rangeSum(e.l, e.r) - b1.rangeSum(e.l, e.r)*e.x
		}
	}
	for i := 0; i < Q; i++ {
		out(ans[i])
	}
}
0