結果

問題 No.1300 Sum of Inversions
ユーザー ccppjsrbccppjsrb
提出日時 2020-11-27 22:16:29
言語 Go
(1.22.1)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 5,087 bytes
コンパイル時間 14,859 ms
コンパイル使用メモリ 221,696 KB
実行使用メモリ 19,996 KB
最終ジャッジ日時 2024-07-26 13:14:13
合計ジャッジ時間 20,194 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 118 ms
15,188 KB
testcase_04 AC 114 ms
14,808 KB
testcase_05 AC 92 ms
12,752 KB
testcase_06 AC 134 ms
17,988 KB
testcase_07 AC 136 ms
15,984 KB
testcase_08 AC 147 ms
17,280 KB
testcase_09 AC 149 ms
17,208 KB
testcase_10 AC 76 ms
11,084 KB
testcase_11 AC 77 ms
11,212 KB
testcase_12 AC 113 ms
14,936 KB
testcase_13 AC 108 ms
14,544 KB
testcase_14 AC 169 ms
19,944 KB
testcase_15 AC 146 ms
17,160 KB
testcase_16 AC 125 ms
15,444 KB
testcase_17 AC 73 ms
10,832 KB
testcase_18 AC 84 ms
11,988 KB
testcase_19 AC 102 ms
13,524 KB
testcase_20 AC 106 ms
13,780 KB
testcase_21 AC 103 ms
13,776 KB
testcase_22 AC 90 ms
12,752 KB
testcase_23 AC 138 ms
16,656 KB
testcase_24 AC 99 ms
13,136 KB
testcase_25 AC 81 ms
11,732 KB
testcase_26 AC 79 ms
11,728 KB
testcase_27 AC 90 ms
12,496 KB
testcase_28 AC 149 ms
17,696 KB
testcase_29 AC 101 ms
13,648 KB
testcase_30 AC 143 ms
17,276 KB
testcase_31 AC 92 ms
12,880 KB
testcase_32 AC 96 ms
13,140 KB
testcase_33 AC 39 ms
14,220 KB
testcase_34 AC 48 ms
15,900 KB
testcase_35 AC 66 ms
19,996 KB
testcase_36 AC 70 ms
19,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func getNextString(scanner *bufio.Scanner) string {
	if !scanner.Scan() {
		panic("scan failed")
	}
	return scanner.Text()
}
func atoi(s string) int                             { x, _ := strconv.Atoi(s); return x }
func getNextInt(scanner *bufio.Scanner) int         { return atoi(getNextString(scanner)) }
func atoi64(s string) int64                         { x, _ := strconv.ParseInt(s, 10, 64); return x }
func getNextInt64(scanner *bufio.Scanner) int64     { return atoi64(getNextString(scanner)) }
func atof64(s string) float64                       { x, _ := strconv.ParseFloat(s, 64); return x }
func getNextFloat64(scanner *bufio.Scanner) float64 { return atof64(getNextString(scanner)) }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	SetMod(Mod998244353)
	n := getNextInt(scanner)
	type pair [2]int
	aa := make([]pair, n)
	for i := 0; i < n; i++ {
		aa[i][0] = i
		aa[i][1] = getNextInt(scanner)
	}
	sort.Slice(aa, func(i, j int) bool {
		return aa[i][1] < aa[j][1]
	})
	cnt := NewFenwickTree(n)
	sum := NewFenwickTree(n)
	lcnt := make([]Mint, n)
	rcnt := make([]Mint, n)
	lsum := make([]Mint, n)
	rsum := make([]Mint, n)
	l := 0
	for i := 0; i < n; i++ {
		for aa[l][1] < aa[i][1] {
			cnt.Add(aa[l][0], 1)
			sum.Add(aa[l][0], int64(aa[l][1]))
			l++
		}
		rcnt[aa[i][0]] = Mint(cnt.Sum(aa[i][0]+1, n)).Mod()
		rsum[aa[i][0]] = Mint(sum.Sum(aa[i][0]+1, n)).Mod()
	}
	sort.Slice(aa, func(i, j int) bool {
		return aa[i][1] > aa[j][1]
	})
	cnt = NewFenwickTree(n)
	sum = NewFenwickTree(n)
	l = 0
	for i := 0; i < n; i++ {
		for aa[l][1] > aa[i][1] {
			cnt.Add(aa[l][0], 1)
			sum.Add(aa[l][0], int64(aa[l][1]))
			l++
		}
		lcnt[aa[i][0]] = Mint(cnt.Sum(0, aa[i][0])).Mod()
		lsum[aa[i][0]] = Mint(sum.Sum(0, aa[i][0])).Mod()
	}
	var ans Mint
	for i := 0; i < n; i++ {
		ans.AddAs(lcnt[aa[i][0]].Mul(rsum[aa[i][0]]))
		ans.AddAs(rcnt[aa[i][0]].Mul(lsum[aa[i][0]]))
		ans.AddAs(rcnt[aa[i][0]].Mul(lcnt[aa[i][0]]).Mul(Mint(aa[i][1])))
	}
	fmt.Fprintln(writer, ans)
}

// Mod constants.
const (
	Mod1000000007 = 1000000007
	Mod998244353  = 998244353
)

var (
	mod  Mint
	fmod func(Mint) Mint
)

// Mint treats the modular arithmetic
type Mint int64

// SetMod sets the mod. It must be called first.
func SetMod(newmod Mint) {
	switch newmod {
	case Mod1000000007:
		fmod = staticMod1000000007
	case Mod998244353:
		fmod = staticMod998244353
	default:
		mod = newmod
		fmod = dynamicMod
	}
}
func dynamicMod(m Mint) Mint {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}
func staticMod1000000007(m Mint) Mint {
	m %= Mod1000000007
	if m < 0 {
		return m + Mod1000000007
	}
	return m
}
func staticMod998244353(m Mint) Mint {
	m %= Mod998244353
	if m < 0 {
		return m + Mod998244353
	}
	return m
}

// Mod returns m % mod.
func (m Mint) Mod() Mint {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Mint) Inv() Mint {
	return m.Pow(Mint(0).Sub(2))
}

// Pow returns m^n
func (m Mint) Pow(n Mint) Mint {
	p := Mint(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Mint) Add(x Mint) Mint {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Mint) Sub(x Mint) Mint {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Mint) Mul(x Mint) Mint {
	return (m * x).Mod()
}

// Div returns m/x
func (m Mint) Div(x Mint) Mint {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Mint) AddAs(x Mint) *Mint {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Mint) SubAs(x Mint) *Mint {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Mint) MulAs(x Mint) *Mint {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Mint) DivAs(x Mint) *Mint {
	*m = m.Div(x)
	return m
}

// FenwickTree Data structure that can efficiently update elements and calculate prefix sums in a table of numbers.
type FenwickTree struct {
	n    int
	data []int64
}

// NewFenwickTree Constructor
func NewFenwickTree(n int) *FenwickTree {
	data := make([]int64, n+1)
	return &FenwickTree{n: n, data: data}
}

// Add processes a[p] += x.
func (f *FenwickTree) Add(p int, x int64) {
	for p++; p <= f.n; p += p & -p {
		f.data[p] += x
	}
}

// Sum returns a[l] + a[l - 1] + ... + a[r - 1].
func (f *FenwickTree) Sum(l, r int) int64 {
	return f.sum(r) - f.sum(l)
}
func (f *FenwickTree) sum(r int) int64 {
	s := int64(0)
	for ; r > 0; r -= r & -r {
		s += f.data[r]
	}
	return s
}
0