結果

問題 No.877 Range ReLU Query
ユーザー sgswsgsw
提出日時 2021-08-12 23:41:25
言語 Go
(1.22.1)
結果
AC  
実行時間 861 ms / 2,000 ms
コード長 3,715 bytes
コンパイル時間 14,476 ms
コンパイル使用メモリ 223,828 KB
実行使用メモリ 42,624 KB
最終ジャッジ日時 2024-11-08 10:38:47
合計ジャッジ時間 23,332 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 3 ms
5,248 KB
testcase_03 AC 4 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 3 ms
5,248 KB
testcase_07 AC 3 ms
5,248 KB
testcase_08 AC 5 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 3 ms
5,248 KB
testcase_11 AC 785 ms
37,760 KB
testcase_12 AC 705 ms
36,352 KB
testcase_13 AC 535 ms
27,904 KB
testcase_14 AC 538 ms
28,416 KB
testcase_15 AC 857 ms
41,344 KB
testcase_16 AC 845 ms
40,960 KB
testcase_17 AC 861 ms
42,112 KB
testcase_18 AC 859 ms
42,624 KB
testcase_19 AC 680 ms
39,936 KB
testcase_20 AC 849 ms
42,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var wg = bufio.NewScanner(os.Stdin)

const (
	inf            = int(1e18)
	initialBufSize = int(1e6)
	maxBufSize     = int(1e6)
)

var buf []byte = make([]byte, initialBufSize)

// greatest common divisor (GCD) via Euclidean algorithm
func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

func init() {
	wg.Split(bufio.ScanWords)
	wg.Buffer(buf, maxBufSize)
}

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

/*
Monoid-Structure should be decleared some-where else.
-> op(Monoid,Monoid)Monoid & ide()Monoid is required.
*/

type Monoid interface {
	get() interface{}
	op(Monoid) Monoid
	ide() Monoid
}

type SegTree struct {
	op   func(Monoid) Monoid /* op */
	e    func() Monoid       /*identity*/
	d    []Monoid
	_d   []Monoid
	n    int /* size*/
	log  int
	size int
}

func Gen(monoid Monoid, n interface{}) (SegTree, bool) {

	seg := SegTree{op: monoid.op, e: monoid.ide, d: []Monoid{}, _d: []Monoid{}, n: 0, log: 0, size: 0}
	collect := true

	switch T := n.(type) {
	case int:
		seg.d = make([]Monoid, n.(int))
		for i := range seg.d {
			seg.d[i] = seg.e()
		}

	case []Monoid:
		_ = T
		seg.d = make([]Monoid, len(n.([]Monoid)))
		copy(seg.d, n.([]Monoid))

	default:
		collect = false
		return seg, collect
	}
	seg.n = len(seg.d)
	for ord := 0; (1 << ord) < seg.n; {
		ord++
		seg.log = ord
	}
	seg.size = 1 << seg.log
	seg._d = make([]Monoid, 2*seg.size)
	for i := range seg._d {
		seg._d[i] = seg.e()
	}

	for i := 0; i < seg.n; i++ {
		seg._d[seg.size+i] = seg.d[i]
	}
	for i := seg.size - 1; i > 0; i-- {
		seg._update(i)
	}
	return seg, collect
}

func (seg SegTree) _update(k int) {
	seg._d[k] = seg._d[2*k].op(seg._d[2*k+1])
}

func (seg SegTree) Set(p int, x Monoid) {
	//a[p] -> x
	p += seg.size
	seg._d[p] = x
	for i := 1; i <= seg.log; i++ {
		seg._update(p >> i)
	}
}

func (seg SegTree) Get(p int) Monoid {
	// a[p]
	return seg._d[p+seg.size]
}

func (seg SegTree) OverWrite(p int, f func(Monoid) Monoid) {
	p += seg.size
	m := seg._d[p]
	seg._d[p] = f(m)
	for i := 1; i <= seg.log; i++ {
		seg._update(p >> i)
	}
}

func (seg SegTree) Prod(l, r int) Monoid {
	//op(a[l..r))
	sml := seg.e()
	smr := seg.e()
	l += seg.size
	r += seg.size
	for l < r {
		if l&1 == 1 {
			sml = sml.op(seg._d[l])
			l++
		}
		if r&1 == 1 {
			r--
			smr = seg._d[r].op(smr)
		}
		l >>= 1
		r >>= 1
	}
	return sml.op(smr)
}

func (seg SegTree) All_prod() Monoid {
	return seg._d[1]
}

//独自のモノイド定義
type monoid struct {
	sum int
	cnt int
}

func (m monoid) get() interface{} {
	return m
}
func (x monoid) op(y Monoid) Monoid {
	a := x.get().(monoid)
	b := y.get().(monoid)
	return monoid{a.sum + b.sum, a.cnt + b.cnt}
}

func (m monoid) ide() Monoid {
	return monoid{0, 0}
}

type query struct {
	l, r, val, idx int
}
type pair struct {
	val, idx int
}

func main() {
	n, q := nextInt(), nextInt()
	a := make([]pair, n)
	Q := make([]query, q)
	for i := 0; i < n; i++ {
		a[i] = pair{nextInt(), i}
	}
	seg, _ := Gen(monoid{}, n)

	for i := 0; i < q; i++ {
		_, l, r, val := nextInt(), nextInt(), nextInt(), nextInt()
		l--
		r--
		Q[i] = query{l, r, val, i}
	}

	sort.Slice(Q, func(i, j int) bool {
		return Q[i].val > Q[j].val
	})

	sort.Slice(a, func(i, j int) bool {
		return a[i].val > a[j].val
	})

	ans := make([]int, q)
	for itr, i := 0, 0; i < q; i++ {
		for itr < n && a[itr].val >= Q[i].val {
			seg.Set(a[itr].idx, monoid{a[itr].val, 1})
			itr++
		}
		l, r, val, idx := Q[i].l, Q[i].r, Q[i].val, Q[i].idx
		m := seg.Prod(l, r+1).(monoid)
		ans[idx] = m.sum - val*m.cnt
	}

	for i := 0; i < q; i++ {
		fmt.Printf("%d\n", ans[i])
	}
}
0