forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
56E.go
56 lines (52 loc) · 1.01 KB
/
56E.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bufio"
. "fmt"
"io"
"sort"
)
// github.com/EndlessCheng/codeforces-go
func CF56E(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
var n, posR int
Fscan(in, &n)
fa := make([]int, n)
var f func(int) int
f = func(x int) int {
if fa[x] != x {
fa[x] = f(fa[x])
}
return fa[x]
}
a := make([]struct{ x, h, i int }, n)
for i := range a {
Fscan(in, &a[i].x, &a[i].h)
a[i].i = i
fa[i] = i
}
sort.Slice(a, func(i, j int) bool { return a[i].x < a[j].x })
type pair struct{ v, i int }
stk := []pair{{2e9, n}}
for i := n - 1; i >= 0; i-- {
v := a[i].x + a[i].h
for {
if top := stk[len(stk)-1]; top.v > v {
posR = top.i
break
}
stk = stk[:len(stk)-1]
}
stk = append(stk, pair{v, i})
j := sort.Search(n, func(i int) bool { return a[i].x >= v }) - 1
if j >= posR {
j = posR
}
fa[f(i)] = f(j)
}
ans := make([]interface{}, n)
for i, p := range a {
ans[p.i] = f(i) - i + 1
}
Fprintln(out, ans...)
}
//func main() { CF56E(os.Stdin, os.Stdout) }