Skip to content

Commit

Permalink
[2400][2A] CF12D 三维偏序 树状数组
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Feb 23, 2024
1 parent d0be15a commit 3a8bba4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
52 changes: 52 additions & 0 deletions main/1-99/12D.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"bufio"
. "fmt"
"io"
"sort"
)

// https://space.bilibili.com/206214
func cf12D(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
var n, ans int
Fscan(in, &n)
a := make([]struct{ x, y, z int }, n)
for i := range a {
Fscan(in, &a[i].x)
}
sy := make([]int, n)
for i := range a {
Fscan(in, &a[i].y)
sy[i] = a[i].y
}
for i := range a {
Fscan(in, &a[i].z)
}
sort.Slice(a, func(i, j int) bool { return a[i].x > a[j].x })
sort.Ints(sy)

t := make([]int, n+1)
for i := 0; i < n; {
st := i
v := a[st].x
for ; i < n && a[i].x == v; i++ {
a[i].y = n - sort.SearchInts(sy, a[i].y) // 要维护的是后缀最大值
for j := a[i].y - 1; j > 0; j &= j - 1 {
if t[j] > a[i].z {
ans++
break
}
}
}
for ; st < i; st++ {
for j := a[st].y; j <= n; j += j & -j {
t[j] = max(t[j], a[st].z)
}
}
}
Fprint(out, ans)
}

//func main() { cf12D(os.Stdin, os.Stdout) }
29 changes: 29 additions & 0 deletions main/1-99/12D_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Generated by copypasta/template/generator_test.go
package main

import (
"github.com/EndlessCheng/codeforces-go/main/testutil"
"testing"
)

// https://codeforces.com/problemset/problem/12/D
// https://codeforces.com/problemset/status/12/problem/D
func Test_cf12D(t *testing.T) {
testCases := [][2]string{
{
`3
1 4 2
4 3 2
2 5 3`,
`1`,
},
{
`5
2 8 10 0 7
7 7 3 0 10
2 8 3 2 2`,
`1`,
},
}
testutil.AssertEqualStringCase(t, testCases, 0, cf12D)
}

0 comments on commit 3a8bba4

Please sign in to comment.