結果

問題 No.1300 Sum of Inversions
ユーザー sgswsgsw
提出日時 2021-08-14 03:25:13
言語 Go
(1.22.1)
結果
AC  
実行時間 1,435 ms / 2,000 ms
コード長 4,274 bytes
コンパイル時間 11,667 ms
コンパイル使用メモリ 219,456 KB
実行使用メモリ 86,360 KB
最終ジャッジ日時 2024-04-15 01:03:30
合計ジャッジ時間 47,265 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1,081 ms
74,580 KB
testcase_04 AC 1,076 ms
72,340 KB
testcase_05 AC 820 ms
50,040 KB
testcase_06 AC 1,228 ms
76,160 KB
testcase_07 AC 1,186 ms
75,900 KB
testcase_08 AC 1,314 ms
77,916 KB
testcase_09 AC 1,403 ms
81,096 KB
testcase_10 AC 656 ms
38,320 KB
testcase_11 AC 686 ms
40,252 KB
testcase_12 AC 1,133 ms
72,860 KB
testcase_13 AC 1,129 ms
74,176 KB
testcase_14 AC 1,435 ms
77,880 KB
testcase_15 AC 1,370 ms
79,612 KB
testcase_16 AC 1,281 ms
74,160 KB
testcase_17 AC 623 ms
39,116 KB
testcase_18 AC 767 ms
45,972 KB
testcase_19 AC 995 ms
71,724 KB
testcase_20 AC 1,043 ms
74,744 KB
testcase_21 AC 983 ms
75,448 KB
testcase_22 AC 806 ms
46,312 KB
testcase_23 AC 1,240 ms
75,896 KB
testcase_24 AC 832 ms
46,140 KB
testcase_25 AC 714 ms
43,688 KB
testcase_26 AC 688 ms
41,280 KB
testcase_27 AC 783 ms
43,220 KB
testcase_28 AC 1,421 ms
75,832 KB
testcase_29 AC 989 ms
75,420 KB
testcase_30 AC 1,323 ms
81,560 KB
testcase_31 AC 800 ms
44,352 KB
testcase_32 AC 847 ms
48,012 KB
testcase_33 AC 858 ms
82,320 KB
testcase_34 AC 874 ms
80,196 KB
testcase_35 AC 856 ms
86,360 KB
testcase_36 AC 862 ms
85,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

	"reflect"
)

var wg = bufio.NewScanner(os.Stdin)

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

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

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 {
	monoid_type Monoid
	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 int) (SegTree, bool) {

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

	switch n > 0 {
	case true:
		seg.d = make([]Monoid, n)
		for i := range seg.d {
			seg.d[i] = seg.e()
		}
	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 SliceGen(seg SegTree, array []Monoid) bool {
	ok := true
	if len(array) != seg.n {
		ok = false
		return ok
	}

	for _, v := range array {
		if reflect.TypeOf(seg.monoid_type) != reflect.TypeOf(v) {
			ok = false
			return ok
		}
	}

	for i, v := range array {
		seg.Set(i, v)
	}

	for i := seg.size - 1; i > 0; i-- {
		seg._update(i)
	}

	return ok
}

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 numbers struct {
	val, idx int
}

type monoid struct {
	val, cnt int
}

func (x monoid) Get() interface{} {
	return x
}

func (x monoid) Op(y Monoid) Monoid {
	return monoid{(x.val + y.(monoid).val) % mod, x.cnt + y.(monoid).cnt}
}

func (x monoid) Ide() Monoid {
	return monoid{0, 0}
}

func main() {
	n := nextInt()
	a := make([]numbers, n)
	for i := 0; i < n; i++ {
		x := nextInt()
		a[i] = numbers{val: x, idx: i}
	}

	//left
	sort.Slice(a, func(i, j int) bool {
		if a[i].val > a[j].val {
			return true
		} else if a[i].val < a[j].val {
			return false
		} else {
			return a[i].idx > a[j].idx
		}
	})

	seg, _ := Gen(monoid{}, n)
	left := make([]monoid, n)
	for _, v := range a {
		left[v.idx] = seg.Prod(0, v.idx).(monoid)
		seg.Set(v.idx, monoid{v.val, 1})
	}

	//right

	sort.Slice(a, func(i, j int) bool {
		if a[i].val < a[j].val {
			return true
		} else if a[i].val > a[j].val {
			return false
		} else {
			return a[i].idx < a[j].idx
		}
	})

	seg1, _ := Gen(monoid{}, n)
	right := make([]monoid, n)
	for _, v := range a {
		right[v.idx] = seg1.Prod(v.idx+1, n).(monoid)
		seg1.Set(v.idx, monoid{v.val, 1})
	}

	var ans int64 = 0

	for i := 0; i < n; i++ {
		if left[a[i].idx].cnt*right[a[i].idx].cnt > 0 {
			lval, lcnt := left[a[i].idx].val, left[a[i].idx].cnt
			rval, rcnt := right[a[i].idx].val, right[a[i].idx].cnt
			middle := a[i].val
			ans += int64(lval*rcnt%mod) + int64(rval*lcnt%mod) + int64((middle*lcnt)%mod*rcnt%mod)
			ans %= mod
		}
	}
	fmt.Printf("%d\n", ans)
}
0