結果

問題 No.1300 Sum of Inversions
ユーザー sgswsgsw
提出日時 2021-08-14 03:22:00
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 4,233 bytes
コンパイル時間 10,442 ms
コンパイル使用メモリ 224,408 KB
実行使用メモリ 84,152 KB
最終ジャッジ日時 2024-10-04 07:49:53
合計ジャッジ時間 43,333 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 969 ms
81,440 KB
testcase_04 AC 959 ms
70,880 KB
testcase_05 AC 678 ms
47,344 KB
testcase_06 AC 1,141 ms
77,692 KB
testcase_07 AC 1,058 ms
74,164 KB
testcase_08 AC 1,166 ms
75,748 KB
testcase_09 AC 1,155 ms
76,516 KB
testcase_10 AC 589 ms
43,928 KB
testcase_11 AC 583 ms
39,800 KB
testcase_12 AC 1,037 ms
70,908 KB
testcase_13 AC 919 ms
71,960 KB
testcase_14 AC 1,259 ms
77,308 KB
testcase_15 AC 1,157 ms
79,484 KB
testcase_16 AC 1,035 ms
74,536 KB
testcase_17 AC 570 ms
43,820 KB
testcase_18 AC 655 ms
42,812 KB
testcase_19 AC 873 ms
70,788 KB
testcase_20 AC 908 ms
71,260 KB
testcase_21 AC 862 ms
75,468 KB
testcase_22 AC 724 ms
45,924 KB
testcase_23 AC 1,139 ms
78,936 KB
testcase_24 AC 776 ms
45,184 KB
testcase_25 AC 656 ms
41,336 KB
testcase_26 AC 660 ms
45,076 KB
testcase_27 AC 725 ms
45,908 KB
testcase_28 AC 1,300 ms
80,660 KB
testcase_29 AC 885 ms
75,464 KB
testcase_30 AC 1,210 ms
78,984 KB
testcase_31 AC 747 ms
44,908 KB
testcase_32 AC 779 ms
43,428 KB
testcase_33 AC 773 ms
84,152 KB
testcase_34 AC 790 ms
76,612 KB
testcase_35 AC 779 ms
83,588 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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, 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})
	}

	ans := 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 += lval*rcnt%mod + rval*lcnt%mod + middle*lcnt%mod*rcnt%mod
			ans %= mod
		}
	}
	fmt.Printf("%d\n", ans)
}
0