結果

問題 No.1300 Sum of Inversions
ユーザー sgswsgsw
提出日時 2021-08-14 03:25:13
言語 Go
(1.22.1)
結果
AC  
実行時間 1,584 ms / 2,000 ms
コード長 4,274 bytes
コンパイル時間 10,764 ms
コンパイル使用メモリ 222,264 KB
実行使用メモリ 87,224 KB
最終ジャッジ日時 2024-10-04 07:56:37
合計ジャッジ時間 47,898 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 1,135 ms
77,736 KB
testcase_04 AC 1,097 ms
69,500 KB
testcase_05 AC 850 ms
47,104 KB
testcase_06 AC 1,261 ms
77,056 KB
testcase_07 AC 1,233 ms
73,532 KB
testcase_08 AC 1,393 ms
76,480 KB
testcase_09 AC 1,367 ms
78,720 KB
testcase_10 AC 687 ms
43,264 KB
testcase_11 AC 677 ms
40,300 KB
testcase_12 AC 1,181 ms
74,240 KB
testcase_13 AC 1,154 ms
74,752 KB
testcase_14 AC 1,584 ms
81,664 KB
testcase_15 AC 1,489 ms
78,720 KB
testcase_16 AC 1,188 ms
74,368 KB
testcase_17 AC 688 ms
43,380 KB
testcase_18 AC 837 ms
42,624 KB
testcase_19 AC 1,023 ms
71,080 KB
testcase_20 AC 1,087 ms
75,148 KB
testcase_21 AC 1,027 ms
74,368 KB
testcase_22 AC 850 ms
43,648 KB
testcase_23 AC 1,339 ms
76,032 KB
testcase_24 AC 933 ms
48,012 KB
testcase_25 AC 765 ms
41,728 KB
testcase_26 AC 735 ms
41,088 KB
testcase_27 AC 823 ms
44,544 KB
testcase_28 AC 1,495 ms
78,944 KB
testcase_29 AC 1,022 ms
72,832 KB
testcase_30 AC 1,352 ms
78,720 KB
testcase_31 AC 832 ms
45,568 KB
testcase_32 AC 853 ms
44,672 KB
testcase_33 AC 882 ms
87,224 KB
testcase_34 AC 883 ms
84,600 KB
testcase_35 AC 878 ms
84,932 KB
testcase_36 AC 885 ms
84,508 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