package main import ( "bufio" "fmt" "math" "os" "strconv" ) func getScanner(fp *os.File) *bufio.Scanner { scanner := bufio.NewScanner(fp) scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 500001), 500000) return scanner } func getNextString(scanner *bufio.Scanner) string { scanner.Scan() return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextUint64(scanner *bufio.Scanner) uint64 { i, _ := strconv.ParseUint(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout if len(os.Args) > 1 { fp, _ = os.Open(os.Args[1]) if len(os.Args) > 2 { wfp, _ = os.Create(os.Args[2]) } } scanner := getScanner(fp) writer := bufio.NewWriter(wfp) n := getNextInt(scanner) q := getNextInt(scanner) seg := Segment{} seg.init(n) for i := 0; i < n; i++ { seg.bucket[0][i] = getNextInt(scanner) } for i := 0; i < q; i++ { t := getNextInt(scanner) l := getNextInt(scanner) - 1 r := getNextInt(scanner) - 1 if t == 1 { seg.rangeAdd(l, r+1, getNextInt(scanner)) continue } fmt.Fprintln(writer, seg.f(l, r)) } writer.Flush() } // Segment ... type Segment struct { n, h, i, chunk, inf int unit []int bucket [][]int } func (seg *Segment) init(n int) { seg.n = n seg.unit = make([]int, 1) seg.unit[0] = 1 seg.bucket = make([][]int, 1) seg.bucket[0] = make([]int, n) chunk := 8 for i := 0; n > 1; i++ { n = (n-1)/chunk + 1 seg.bucket = append(seg.bucket, make([]int, n)) seg.unit = append(seg.unit, seg.unit[i]*chunk) } seg.h = len(seg.unit) seg.chunk = chunk seg.inf = math.MaxInt32 } func (seg *Segment) get(index int) int { value := 0 for seg.i = 0; seg.i < seg.h; seg.i++ { value += seg.bucket[seg.i][index/seg.unit[seg.i]] index /= seg.unit[seg.i] } return value } func (seg *Segment) rangeAdd(l, r, value int) { for index := l; index < r; index += seg.unit[seg.i] { for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ { if index+seg.unit[seg.i+1] >= r { break } } seg.bucket[seg.i][index/seg.unit[seg.i]] += value } } func (seg *Segment) f(l, r int) int { if l == r { return 1 } return seg.g(seg.get(l), seg.get(l+1)) + seg.f(l+1, r) } func (seg *Segment) g(x, y int) int { if x == y { return 0 } return 1 }