結果
問題 | No.1300 Sum of Inversions |
ユーザー | ccppjsrb |
提出日時 | 2020-11-27 22:14:36 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,063 bytes |
コンパイル時間 | 14,708 ms |
コンパイル使用メモリ | 223,488 KB |
実行使用メモリ | 19,996 KB |
最終ジャッジ日時 | 2024-07-26 13:11:09 |
合計ジャッジ時間 | 18,034 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 114 ms
15,192 KB |
testcase_04 | AC | 110 ms
14,808 KB |
testcase_05 | AC | 91 ms
12,756 KB |
testcase_06 | AC | 130 ms
18,004 KB |
testcase_07 | AC | 125 ms
16,084 KB |
testcase_08 | AC | 139 ms
17,276 KB |
testcase_09 | AC | 137 ms
17,204 KB |
testcase_10 | AC | 70 ms
11,084 KB |
testcase_11 | AC | 71 ms
11,212 KB |
testcase_12 | AC | 114 ms
14,932 KB |
testcase_13 | AC | 109 ms
14,552 KB |
testcase_14 | AC | 156 ms
19,948 KB |
testcase_15 | AC | 141 ms
17,160 KB |
testcase_16 | AC | 118 ms
15,448 KB |
testcase_17 | AC | 70 ms
10,828 KB |
testcase_18 | AC | 81 ms
11,728 KB |
testcase_19 | AC | 97 ms
13,524 KB |
testcase_20 | AC | 102 ms
13,780 KB |
testcase_21 | AC | 100 ms
13,776 KB |
testcase_22 | AC | 89 ms
12,752 KB |
testcase_23 | AC | 128 ms
16,656 KB |
testcase_24 | AC | 91 ms
13,140 KB |
testcase_25 | AC | 78 ms
11,728 KB |
testcase_26 | AC | 74 ms
11,728 KB |
testcase_27 | AC | 85 ms
12,500 KB |
testcase_28 | AC | 146 ms
17,692 KB |
testcase_29 | AC | 98 ms
13,648 KB |
testcase_30 | AC | 137 ms
17,196 KB |
testcase_31 | AC | 87 ms
12,880 KB |
testcase_32 | AC | 91 ms
13,140 KB |
testcase_33 | AC | 33 ms
14,220 KB |
testcase_34 | AC | 44 ms
15,856 KB |
testcase_35 | AC | 63 ms
19,996 KB |
testcase_36 | WA | - |
ソースコード
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)) rsum[aa[i][0]] = Mint(sum.Sum(aa[i][0]+1, n)) } 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])) lsum[aa[i][0]] = Mint(sum.Sum(0, aa[i][0])) } 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 }