Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).q events are about to happen (in chronological order). They are of three types:  Application x generates a notification (this new notification is unread).  Thor reads all notifications generated so far by application x (he may re-read some notifications).  Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.

The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).

Print the number of unread notifications after each event.

NoteIn the first sample:  Application 3 generates a notification (there is 1 unread notification).  Application 1 generates a notification (there are 2 unread notifications).  Application 2 generates a notification (there are 3 unread notifications).  Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test:  Application 2 generates a notification (there is 1 unread notification).  Application 4 generates a notification (there are 2 unread notifications).  Application 2 generates a notification (there are 3 unread notifications).  Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.  Application 3 generates a notification (there is 1 unread notification).  Application 3 generates a notification (there are 2 unread notifications).

package main

import (
	"bufio"
	"strconv"
	"os"
)

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}

func getI() int {
  return int(getI64())
}

func getS() string {
  scanner.Scan()
  return scanner.Text()
}

var (
	unread int
	messageCount int
	queues [300001][]int
	joinedQueue []Message
)

type Message struct {
	app int
	idx int
}

func main()  {

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	_, q := getI(), getI()
	messageCount = 1
	for opNo := 0; opNo < q; opNo++ {
		command, i := getI(), getI()
		if command == 1 {
			joinedQueue = append(joinedQueue, Message{app: i, idx: messageCount})
			queues[i] = append(queues[i], messageCount)
			messageCount += 1
			unread += 1
		} else if command == 2 {
			unread -= len(queues[i])
			queues[i] = make([]int, 0)
		} else {
			for len(joinedQueue) > 0 && joinedQueue[0].idx <= i {
				message := joinedQueue[0]
				joinedQueue = joinedQueue[1:]
				if  len(queues[message.app]) > 0 && queues[message.app][0] == message.idx {
					queues[message.app] = queues[message.app][1:]
					unread -= 1
				}
			}
		}

		writer.WriteString(strconv.Itoa(unread))
		writer.WriteByte('\n')
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/704/problem/A

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Query struct {
	QueryType int
	Value     int
}

type Event struct {
	Index int
	App   int
}

func Solve(n int, q int, queries []Query) []int {
	queues := make([][]int, n)
	events := []Event{}
	unread := 0
	ans := make([]int, q)
	eventCount := 0
	for i, q := range queries {
		if q.QueryType == 1 {
			eventCount += 1
			unread += 1
			queues[q.Value-1] = append(queues[q.Value-1], eventCount)
			events = append(events, Event{eventCount, q.Value})
		} else if q.QueryType == 2 {
			unread -= len(queues[q.Value-1])
			queues[q.Value-1] = []int{}
		} else {
			for len(events) > 0 && events[0].Index <= q.Value {
				event := events[0]
				events = events[1:]
				if len(queues[event.App-1]) > 0 && queues[event.App-1][0] == event.Index {
					unread -= 1
					queues[event.App-1] = queues[event.App-1][1:]
				}
			}
		}
		ans[i] = unread
	}
	return ans
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, q int
	fmt.Fscanf(reader, "%d %d\n", &n, &q)
	queries := make([]Query, q)
	for i := 0; i < q; i++ {
		fmt.Fscanf(reader, "%d %d\n", &queries[i].QueryType, &queries[i].Value)
	}

	ans := Solve(n, q, queries)
	for _, elem := range ans {
		fmt.Fprintf(writer, "%d\n", elem)
	}
}

package main

import (
	"bufio"
	"os"
	"strconv"
)

const maxn = 500005

var n, q, res, pos, tot int
var cnt, viewed, which, moved [maxn]int

func gen(x int) {
	res++
	cnt[x]++
	tot++
	which[tot] = x
}

func read(x int) {
	res -= (cnt[x] - viewed[x])
	viewed[x] = cnt[x]
}

func first(t int) {
	for pos < t {
		pos++
		x := which[pos]
		moved[x]++
		if moved[x] > viewed[x] {
			viewed[x]++
			res--
		}
	}
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	in.Scan()
	q, _ = strconv.Atoi(in.Text())

	for i := 1; i <= n; i++ {
		cnt[i] = 0
		viewed[i] = 0
		moved[i] = 0
	}

	var kind, x, t int

	pos = 0
	tot = 0

	for i := 1; i <= q; i++ {
		in.Scan()
		kind, _ = strconv.Atoi(in.Text())

		if kind == 1 {
			in.Scan()
			x, _ = strconv.Atoi(in.Text())
			gen(x)
		} else if kind == 2 {
			in.Scan()
			x, _ = strconv.Atoi(in.Text())
			read(x)
		} else {
			in.Scan()
			t, _ = strconv.Atoi(in.Text())
			first(t)
		}

		out.WriteString(strconv.Itoa(res))
		out.WriteByte('\n')
	}
}

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}

func getI() int {
  return int(getI64())
}

func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type State struct {
	bookCount int
	shelfIdxs []int
}

type Command struct {
	command int

}

type Node struct {
	opNo     int
	children []*Node
}

func (node *Node) visit () {
	//fmt.Println("HELLO", node.opNo, node.children)
	for _, child := range node.children {
		command := commands[child.opNo - 1]

		if command[0] == 1 {
			i, j := command[1], command[2]
			if !shelves[i][j] {
				bookCount[child.opNo] = bookCount[child.opNo - 1] + 1
				shelves[i][j] = true
				child.visit()
				shelves[i][j]= false
			} else {
				bookCount[child.opNo] = bookCount[child.opNo - 1]
				child.visit()
			}
		} else if command[0] == 2 {
			i, j := command[1], command[2]
			if shelves[i][j] {
				bookCount[child.opNo] = bookCount[child.opNo - 1] - 1
				shelves[i][j] = false
				child.visit()
				shelves[i][j] = true
			} else {
				bookCount[child.opNo] = bookCount[child.opNo - 1]
				child.visit()
			}
		} else if command[0] == 3 {
			i := command[1]
			newBookCount := 0
			for idx:=0; idx < m; idx++ {
				if shelves[i][idx] { shelves[i][idx] = false } else {
					shelves[i][idx] = true
					newBookCount += 1
				}
			}

			bookCount[child.opNo] = bookCount[child.opNo - 1] - (m - newBookCount) + newBookCount
			child.visit()

			for idx:=0; idx < m; idx++ {
				shelves[i][idx] = !shelves[i][idx]
			}

		} else if command[0] == 4 {
			stateNo := command[1]
			bookCount[child.opNo] = bookCount[stateNo]
			child.visit()
		}
	}
}

var (
	m, n, q int
	commands [100001][]int
	bookCount [100001]int
	shelves [1000][1000]bool
	nodes [100001]*Node
)

func main()  {

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n = getI()
	m = getI()
	q = getI()

	bookCount[0] = 0
	nodes[0] = &Node{opNo: 0, children:make([]*Node, 0)}

	for opNo := 0; opNo < q; opNo++ {
		command := getI()
		if command == 1 {
			commands[opNo] = []int{command, getI() - 1, getI() - 1}
			node := &Node{opNo: opNo+1, children:make([]*Node, 0)}
			nodes[opNo + 1] = node
			nodes[opNo].children = append(nodes[opNo].children, node)
		} else if command == 2 {
			commands[opNo] = []int{command, getI() - 1, getI() - 1}
			node := &Node{opNo: opNo+1, children:make([]*Node, 0)}
			nodes[opNo + 1] = node
			nodes[opNo].children = append(nodes[opNo].children, node)
		} else if command == 3 {
			commands[opNo] = []int{command, getI() - 1}
			node := &Node{opNo: opNo+1, children:make([]*Node, 0)}
			nodes[opNo + 1] = node
			nodes[opNo].children = append(nodes[opNo].children, node)
		} else {
			stateNo := getI()
			commands[opNo] = []int{command, stateNo}
			node := &Node{opNo: opNo+1, children:make([]*Node, 0)}
			nodes[opNo + 1] = node
			nodes[stateNo].children = append(nodes[stateNo].children, node)
		}
	}

	nodes[0].visit()

	for i:=1; i<=q; i++{
		writer.WriteString(strconv.Itoa(bookCount[i]))
		writer.WriteByte('\n')
	}
}

package main

import (
	"bufio"
	"strconv"
	"os"
)

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}

func getI() int {
  return int(getI64())
}

func getS() string {
  scanner.Scan()
  return scanner.Text()
}

var (
	unread int
	messageCount int
	queues [300001][]int
	joinedQueue []Message
)

type Message struct {
	app int
	idx int
}

func main()  {

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	_, q := getI(), getI()
	messageCount = 1
	for opNo := 0; opNo < q; opNo++ {
		command, i := getI(), getI()
		if command == 1 {
			joinedQueue = append(joinedQueue, Message{app: i, idx: messageCount})
			queues[i] = append(queues[i], messageCount)
			messageCount += 1
			unread += 1
		} else if command == 2 {
			unread -= len(queues[i])
			queues[i] = make([]int, 0)
		} else {
			for ((len(joinedQueue) > 0) && joinedQueue[len(joinedQueue) - 1].idx >= messageCount - i) {
				message := joinedQueue[len(joinedQueue) - 1]
				if len(queues[message.app]) > 0 && queues[message.app][len(queues[message.app]) - 1] == message.idx {
					queues[message.app] = queues[message.app][:len(queues[message.app]) - 1]
					unread -= 1
				}
				joinedQueue = joinedQueue[:len(joinedQueue) - 1]
			}
		}
		writer.WriteString(strconv.Itoa(unread))
		writer.WriteByte('\n')
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/704/problem/A

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Query struct {
	QueryType int
	Value     int
}

type Event struct {
	Index int
	App   int
}

func Solve(n int, q int, queries []Query) []int {
	queues := make([][]int, n)
	events := []Event{}
	unread := 0
	ans := make([]int, q)
	for i, q := range queries {
		if q.QueryType == 1 {
			unread += 1
			queues[q.Value-1] = append(queues[q.Value-1], i+1)
			events = append(events, Event{len(events) + 1, q.Value})
		} else if q.QueryType == 2 {
			unread -= len(queues[q.Value-1])
			queues[q.Value-1] = []int{}
		} else {
			for len(events) > 0 && events[0].Index <= q.Value {
				event := events[0]
				events = events[1:]
				if len(queues[event.App-1]) > 0 && queues[event.App-1][0] == event.Index {
					unread -= 1
					queues[event.App-1] = queues[event.App-1][1:]
				}
			}
		}
		ans[i] = unread
	}
	return ans
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, q int
	fmt.Fscanf(reader, "%d %d\n", &n, &q)
	queries := make([]Query, q)
	for i := 0; i < q; i++ {
		fmt.Fscanf(reader, "%d %d\n", &queries[i].QueryType, &queries[i].Value)
	}

	ans := Solve(n, q, queries)
	for _, elem := range ans {
		fmt.Fprintf(writer, "%d\n", elem)
	}
}


Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form "reader entered room", "reader left room". Every reader is assigned a registration number during the registration procedure at the library — it's a unique integer from 1 to 106. Thus, the system logs events of two forms:  "+ ri" — the reader with registration number ri entered the room;  "- ri" — the reader with registration number ri left the room. The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you.

The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ ri" or "- ri", where ri is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers). It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors.

Print a single integer — the minimum possible capacity of the reading room.

NoteIn the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.

package main
import (
	"fmt"
)
func main(){
	var n, register int
	var action string
	var peopleMap map[int] bool
	peopleMap = make(map[int]bool)
	var tmpList []int
	var peopleList []int
	res := 0
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++{
		fmt.Scanf("%s%d\n", &action, &register)
		if action == "+"{
			peopleMap[register] = true
			tmpList = append(tmpList, register)
		}else{
			tmpList = append(tmpList, register * -1)
			_, ok := peopleMap[register]
			if ok{
				delete(peopleMap, register)
			}else{
				peopleList = append(peopleList, register)
			}
		}
	}
	peopleMap = make(map[int]bool)
	peopleList = append(peopleList, tmpList...)
	for i := 0; i < len(peopleList); i++{
		if peopleList[i] > 0{
			peopleMap[peopleList[i]] = true
		}else{
			now := peopleList[i] * -1
			delete(peopleMap, now)
		}
		res = Max(res, len(peopleMap))
	}
	fmt.Println(res)
}

func Max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := map[int]bool{}
	people, maxPeople := 0, 0

	for i := 0; i < n; i++ {
		isPhus := next() == "+"
		a := nextInt()

		if isPhus {
			m[a] = true
			people++
			if people > maxPeople {
				maxPeople = people
			}
		} else {
			if _, ok := m[a]; ok {
				delete(m, a)
				people--
			} else {
				maxPeople++
			}
		}
	}

	println(maxPeople)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"regexp"
	"strconv"
	"strings"
)

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	current := 0
	capacity := 0
	record := make(map[int]bool)

	for i := 0; i < n; i++ {
		if _in, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			log := strings.Split(re.ReplaceAllString(_in, ""), " ")
			if len(log) != 2 {
				return
			}

			action := log[0]
			id, err := strconv.Atoi(log[1])
			if err != nil {
				return
			}

			if action == "+" {
				current++
				record[id] = true
				if abs(current) > capacity {
					capacity = abs(current)
				}
			} else if action == "-" {
				if _, ok := record[id]; ok {
					current--
					record[id] = false
				} else {
					capacity++
				}
			}

		}
	}

	fmt.Println(capacity)
}

func abs(n int) int {
	if n >= 0 {
		return n
	} else {
		return -n
	}
}

package main

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

func main() {
	var n int
	fmt.Scan(&n)

	in := make([]bool, 1000001, 1000001)
	cur := 0
	ans := 0

	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		c := bs.Text()[0]
		bs.Scan()
		p, _ := strconv.Atoi(bs.Text())
		if c == '+' {
			in[p] = true
			cur++
			if ans < cur {
				ans = cur
			}
		} else {
			if in[p] == true {
				cur--
			} else {
				ans++
			}
			in[p] = false
		}
	}

	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)

	num := 0
	fmt.Fscanf(r, "%d\n", &num)
	m := map[int]bool{}

	capacity := 0
	log := []int{}

	var id int
	var sign int
	for i := 0; i < num; i++ {
		fmt.Fscanf(r, "%c %d\n", &sign, &id)
		switch sign {
		case '-':
			log = append(log, -id)
			if _, ok := m[id]; !ok {
				capacity++
			}
		case '+':
			m[id] = true
			log = append(log, id)
		}
	}

	maxCap := capacity
	for _, id := range log {
		if id > 0 {
			capacity++
			if maxCap < capacity {
				maxCap = capacity
			}
		} else {
			capacity--
		}
	}

	fmt.Println(maxCap)
}

package main
import (
	"fmt"
)
func main(){
	var n, register int
	var action string
	var peopleMap map[int] bool
	peopleMap = make(map[int]bool)
	var tmpList []int
	var peopleList []int
	res := 0
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++{
		fmt.Scanf("%s%d", &action, &register)
		if action == "+"{
			peopleMap[register] = true
			tmpList = append(tmpList, register)
		}else{
			tmpList = append(tmpList, register * -1)
			_, ok := peopleMap[register]
			if ok{
				delete(peopleMap, register)
			}else{
				peopleList = append(peopleList, register)
			}
		}
	}
	peopleMap = make(map[int]bool)
	peopleList = append(peopleList, tmpList...)
	for i := 0; i < len(peopleList); i++{
		if peopleList[i] > 0{
			peopleMap[peopleList[i]] = true
		}else{
			now := peopleList[i] * -1
			delete(peopleMap, now)
		}
		res = Max(res, len(peopleMap))
	}
	fmt.Println(res)
}

func Max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

package main

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

func main() {
	var n int
	fmt.Scan(&n)

	in := make([]bool, 1000001, 1000001)
	cur := 0
	ans := 0

	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		c := bs.Text()[0]
		bs.Scan()
		p, _ := strconv.Atoi(bs.Text())
		if c == '+' {
			//in[p] = true
			cur++
			//if ans < cur {
			//		ans = cur
			//	}
		} else {
			//	if in[p] {
			//		cur--
			//	} else {
			//		ans++
			//	}
			in[p] = false
		}
	}

	fmt.Println(ans)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	in := make([]bool, 1000001, 1000001)
	cur := 0
	ans := 0

	var c byte
	var p int
	for i := 0; i < n; i++ {
		fmt.Scanf("%c %d", &c, &p)
		if c == '+' {
			in[p] = true
			cur++
			if ans < cur {
				ans = cur
			}
		} else {
			if in[p] {
				cur--
			} else {
				ans++
			}
			in[p] = false
		}
	}

	fmt.Println(ans)
}

package main

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

func main() {
	var n int
	fmt.Scan(&n)

	//in := make([]bool, 1000001, 1000001)
	//cur := 0
	ans := 0

	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		//c := bs.Text()[0]
		bs.Scan()
		//p, _ := strconv.Atoi(bs.Text())
		//if c == '+' {
		//	in[p] = true
		//	cur++
		//	if ans < cur {
		//		ans = cur
		//	}
		//} else {
		//	if in[p] {
		//		cur--
		//	} else {
		//		ans++
		//	}
		//	in[p] = false
		//}
	}

	fmt.Println(ans)
}


There is an array with n elements a1, a2, ..., an and the number x.In one operation you can select some i (1 ≤ i ≤ n) and replace element ai with ai & x, where & denotes the bitwise and operation.You want the array to have at least two equal elements after applying some operations (possibly, none). In other words, there should be at least two distinct indices i ≠ j such that ai = aj. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply.

The first line contains integers n and x (2 ≤ n ≤ 100 000, 1 ≤ x ≤ 100 000), number of elements in the array and the number to and with. The second line contains n integers ai (1 ≤ ai ≤ 100 000), the elements of the array.

Print a single integer denoting the minimal number of operations to do, or -1, if it is impossible.

NoteIn the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move.In the second example the array already has two equal elements.In the third example applying the operation won't change the array at all, so it is impossible to make some pair of elements equal.

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

const maxn = 120000
var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	//sort.Sort(sort.IntSlice(a))
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {

	var n, x = -1, -1
	a := []int{0}

	buf, error := ioutil.ReadAll(os.Stdin)

	var input = string(buf)
	if error != nil {
		log.Fatal(error)
		return
	}

	var t = 0
	var valid = false
	var k = 0
	for _,ch := range input {
		if ch >= '0' && ch <= '9' {
			t = t * 10 + int(ch - '0')
			valid = true
		}else {
			if valid {
				if n == -1 {
					n = t
					a = make([]int, n)
				} else if x == - 1{
					x = t
				} else {
					a[k] = t
					k++
				}
				valid = false
			}
			t = 0
		}
	}

	if valid {
		a[k] = t
		k++
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

type Scanner struct{
	buf []byte
	len int
	index int
}

func readInt(scanner *Scanner) int{
	var ret = 0
	var valid = false
	for i := scanner.index; i < scanner.len; i++ {
		var ch = scanner.buf[i]
		if ch >= '0' && ch <= '9' {
			ret = ret * 10 + int(ch - '0')
			valid = true
		}else if valid{
			scanner.index = i + 1
			break
		}
	}
	return ret
}

var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {

	buffer, error := ioutil.ReadAll(os.Stdin)

	if error != nil {
		log.Fatal(error)
		return
	}

	var scanner = &Scanner{
		buf:buffer,
		len:len(buffer),
		index:0,
	}

	var n = readInt(scanner)
	var x = readInt(scanner)
	var a = make([]int,n)
	for i := 0; i < n; i++ {
		a[i] = readInt(scanner)
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

const maxn = 120000
var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	//sort.Sort(sort.IntSlice(a))
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {

	var n, x = -1, -1
	a := []int{0}

	buf, error := ioutil.ReadAll(os.Stdin)

	var input = string(buf)
	if error != nil {
		log.Fatal(error)
		return
	}

	var t = 0
	var valid = false
	var k = 0
	for _,ch := range input {
		if ch >= '0' && ch <= '9' {
			t = t * 10 + int(ch - '0')
			valid = true
		}else {
			if valid {
				if n == -1 {
					n = t
					a = make([]int, n)
				} else if x == - 1{
					x = t
				} else {
					a[k] = t
					k++
				}
				valid = false
			}
			t = 0
		}
	}

	if valid {
		a[k] = t
		k++
	}

	fmt.Println(run(a,n,x))
}

package main

import "fmt"

const maxn = 120000
var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)
var a = make([]int, maxn)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	//sort.Sort(sort.IntSlice(a))
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {

	var n, x int

	fmt.Scanf("%d %d",&n,&x)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d",&a[i])
	}

	fmt.Printf("%d\n",run(a,n,x))
}

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

type Scanner struct{
	buf []byte
	len int
	index int
}

func readInt(scannr *Scanner) int{
	var ret = 0
	for i := scannr.index; i < scannr.len; i++ {
		var ch = scannr.buf[i]
		if ch >= '0' && ch <= '9' {
			ret = ret * 10 + int(ch - '0')
		}else {
			scannr.index = i + 1
			break
		}
	}
	return ret
}

const maxn = 120000
var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	//sort.Sort(sort.IntSlice(a))
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {

	buffer, error := ioutil.ReadAll(os.Stdin)

	if error != nil {
		log.Fatal(error)
		return
	}

	var scanner = &Scanner{
		buf:buffer,
		len:len(buffer),
		index:0,
	}

	var n = readInt(scanner)
	var x = readInt(scanner)
	var a = make([]int,n)
	//fmt.Println(n,x)
	for i := 0; i < n; i++ {
		a[i] = readInt(scanner)
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
	"sort"
)

const maxn = 12000

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	var keyMap = make(map[int]int)
	var bitwise = make(map[int]int)
	sort.Sort(sort.IntSlice(a))
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] == k {
			return 0
		}
		keyMap[k] = k
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if bitwise[t] == t {
			return 1
		}

		if bitwise[k] == t {
			return 2
		}
	}
	return -1
}

func main()  {
	var n, x = 0,0

	fmt.Scan(&n,&x)
	var a = make([]int,n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

type Scanner struct{
	buf []byte
	len int
	index int
}

func readInt(scannr *Scanner) int{
	var ret = 0
	for i := scannr.index; i < scannr.len; i++ {
		var ch = scannr.buf[i]
		if ch >= '0' && ch <= '9' {
			ret = ret * 10 + int(ch - '0')
		}else {
			scannr.index = i + 1
			break
		}
	}
	return ret
}

var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {

	buffer, error := ioutil.ReadAll(os.Stdin)

	if error != nil {
		log.Fatal(error)
		return
	}

	var scanner = &Scanner{
		buf:buffer,
		len:len(buffer),
		index:0,
	}

	var n = readInt(scanner)
	var x = readInt(scanner)
	var a = make([]int,n)
	fmt.Println(n,x,a)
	for i := 0; i < n; i++ {
		a[i] = readInt(scanner)
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

type Scanner struct{
	buf []byte
	len int
	index int
}

func readInt(scannr *Scanner) int{
	var ret = 0
	for i := scannr.index; i < scannr.len; i++ {
		var ch = scannr.buf[i]
		if ch >= '0' && ch <= '9' {
			ret = ret * 10 + int(ch - '0')
		}else {
			scannr.index = i + 1
			break
		}
	}
	return ret
}

var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {

	buffer, error := ioutil.ReadAll(os.Stdin)

	if error != nil {
		log.Fatal(error)
		return
	}

	var scanner = &Scanner{
		buf:buffer,
		len:len(buffer),
		index:0,
	}

	var n = readInt(scanner)
	var x = readInt(scanner)
	var a = make([]int,n)
	for i := 0; i < n; i++ {
		a[i] = readInt(scanner)
	}
	fmt.Println(n,x,a)

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
)

const maxn = 12000

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	var keyMap = make(map[int]bool)
	var bitwise = make(map[int]bool)
	for _,k := range a {
		if keyMap[k] {
			return 0
		}

		var t = k & x
		if keyMap[t] {
			return 1
		}
		keyMap[k] = true
	}

	for _, k:= range a {
		var t = k & x

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {
	var n, x = 0,0

	fmt.Scan(&n,&x)
	var a = make([]int,n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
)

const maxn = 12000

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	var keyMap = make(map[int]bool)
	var bitwise = make(map[int]bool)
	//sort.Sort(sort.IntSlice(a))
	for _,k := range a {
		if keyMap[k] {
			return 0
		}
		keyMap[k] = true
	}

	for _,k := range a {
		var t = k & x
		if keyMap[t] {
			return 1
		}

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {
	var n, x = 0,0

	fmt.Scan(&n,&x)
	var a = make([]int,n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
	"sort"
)

const maxn = 12000

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	var keyMap = make(map[int]int)
	var bitwise = make(map[int]int)
	sort.Sort(sort.IntSlice(a))
	for i,k := range a {
		if keyMap[k] != 0 {
			return 0
		}

		var t = k & x
		if keyMap[t] != 0 {
			return 1
		}
		keyMap[k] = t

		if bitwise[t] != 0 {
			return 2
		}

		bitwise[t] = i + 1
	}

	return -1
}

func main()  {
	var n, x = 0,0

	fmt.Scan(&n,&x)
	var a = make([]int,n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
)

const maxn = 120000

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	var keyMap = make(map[int]bool)
	var bitwise = make(map[int]bool)
	//sort.Sort(sort.IntSlice(a))
	for _,k := range a {
		if keyMap[k] {
			return 0
		}
		keyMap[k] = true
	}

	for _,k := range a {
		var t = k & x
		if keyMap[t] {
			return 1
		}

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {
	var n, x = 0,0

	fmt.Scan(&n,&x)
	var a = make([]int,n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

const maxn = 120000
var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)
var a = make([]int, maxn)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	//sort.Sort(sort.IntSlice(a))
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {
	defer writer.Flush()

	var n, x int

	scanf("%d %d\n",&n,&x)
	for i := 0; i < n; i++ {
		scanf("%d\n",&a[i])
	}

	printf("%d\n",run(a,n,x))
}

package main

import (
	"fmt"
)

const maxn = 120000
var keyMap = make(map[int]int)
var bitwise = make(map[int]bool)
var a = make([]int, maxn)

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	//sort.Sort(sort.IntSlice(a))
	for i := 0; i < n; i++ {
		var k = a[i]
		if keyMap[k] != 0{
			return 0
		}
		keyMap[k] = i + 1
	}

	for i := 0; i < n; i++ {
		var k = a[i]
		var t = k & x
		if keyMap[t] != 0 && keyMap[t] != i + 1 {
			return 1
		}

		if bitwise[t]  {
			return 2
		}

		bitwise[t] = true
	}

	return -1
}

func main()  {
	var n, x = 0,0

	fmt.Scanf("%d%d",&n,&x)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d",&a[i])
	}

	fmt.Println(run(a,n,x))
}

package main

import (
	"fmt"
	"sort"
)

const maxn = 12000

func run(a []int, n int, x int) int {
	if n == 1{
		return -1
	}
	var keyMap = make(map[int]int)
	var bitwise = make(map[int]int)
	sort.Sort(sort.IntSlice(a))
	for _,k := range a {
		if keyMap[k] == k {
			return 0
		}
		keyMap[k] = k
	}

	for _, k := range  a{
		var t = k & x
		if bitwise[t] == t {
			return 1
		}

		if bitwise[k] == t {
			return 2
		}

		bitwise[k] = t
	}
	return -1
}

func main()  {
	var n, x = 0,0

	fmt.Scan(&n,&x)
	var a = make([]int,n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Println(run(a,n,x))
}


An array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following:   Choose any integer $$$x$$$.  For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.

NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.

package main

import (
	"bufio"
	. "fmt"
	"os"
)

func A2() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n int

		Fscan(in, &n)
		a := make([]int, n)
		far := make(map[int]int)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			far[a[i]] = i
		}
		m := make(map[int]bool)

		var f bool
		var p = n
		for i := n - 2; i >= 0; i-- {
			if a[i] > a[i+1] {
				p = i
				f = true
				break
			}

		}
		if f {
			for i := 0; i <= p; i++ {
				m[a[i]] = true
				if far[a[i]] > p {
					p = far[a[i]]
				}
			}
			Fprintln(out, len(m))
		} else {
			Fprintln(out, 0)
		}

	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A2() }

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {

	defer out.Flush()

	t := 0
	if _, err := fmt.Fscan(in, &t); err != nil {
		return
	}

	for ; t > 0; t-- {
		n, ans := 0, 0
		if _, err := fmt.Fscan(in, &n); err != nil {
			return
		}
		nums := make([]int, n)
		for i := 0; i < n; i++ {
			if _, err := fmt.Fscan(in, &nums[i]); err != nil {
				return
			}
		}
		indices := make(map[int]int)
		zero := make(map[int]bool)
		for i := n - 1; i > 0; i-- {
			if _, ok := indices[nums[i]]; !ok {
				indices[nums[i]] = i
			}
			if nums[i-1] > nums[i] {
				k := 0
				if v, ok := indices[nums[i-1]]; ok {
					k = v
				} else {
					k = i - 1
				}
				for j := 0; j <= k; j++ {
					if _, ok := zero[nums[j]]; !ok {
						zero[nums[j]] = true
						ans++
					}
					if v, ok := indices[nums[j]]; ok && v > k {
						k = v
					}
				}
				break
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

func Solve(_in io.Reader, _out io.Writer) {
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		if n == 1 {
			Fprintln(out, 0)
			continue
		}
		a := make([]int, n)
		// 统计 a[i] 最早出现位置
		mp := map[int]int{}
		cnt := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			cnt[i] = len(mp)
			if _, ok := mp[a[i]]; !ok {
				mp[a[i]] = i
			}
		}
		j := n-1
		for ; j >= 0; j-- {
			if j >= 1 && a[j] == a[j-1] {
				continue
			}
			if i, ok := mp[a[j]]; ok && i < j {
				break
			}
			if j >= 1 && a[j] < a[j-1] {
				break
			}
		}
		if j >= 0 {
			Fprintln(out, cnt[j])
		} else {
			Fprintln(out, 0)
		}
	}
}

func main() { Solve(os.Stdin, os.Stdout) }

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	l, r := 0, n+1
	ans := n
	for l <= r {
		border := (l + r) / 2
		localAns := 0
		var s set[int]
		for i := 0; i < border; i++ {
			if !s.has(arr[i]) {
				localAns++
				s.add(arr[i])
			}
		}
		skip := false
		for i := border; i < n-1; i++ {
			num1, num2 := arr[i], arr[i+1]
			if s.has(num1) {
				num1 = 0
			}
			if s.has(num2) {
				num2 = 0
			}
			if num1 > num2 {
				skip = true
				break
			}
		}
		if skip {
			l = border + 1
		} else {
			r = border - 1
			ans = min(ans, localAns)
		}
	}
	fmt.Fprintln(out, ans)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		P := readNNums(reader, n)
		res := solve(P)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}
func solve(A []int) int {
	n := len(A)
	first := make([]int, n+1)
	for i := 0; i <= n; i++ {
		first[i] = -1
	}
	last := make([]int, n+1)
	for i := 0; i < n; i++ {
		if first[A[i]] < 0 {
			first[A[i]] = i
		}
		last[A[i]] = i
	}

	type Pair struct {
		first  int
		second int
	}

	var events []Pair

	for i := 1; i <= n; i++ {
		if first[i] >= 0 {
			events = append(events, Pair{first[i], i})
			events = append(events, Pair{last[i], -i})
		}
	}

	sort.Slice(events, func(i, j int) bool {
		return events[i].first < events[j].first || events[i].first == events[j].first && events[i].second > events[j].second
	})

	sorted := make([]bool, n)
	sorted[n-1] = true
	for i := n - 2; i >= 0; i-- {
		sorted[i] = false
		if i+1 < n && sorted[i+1] && A[i] <= A[i+1] {
			sorted[i] = true
		}
	}

	if sorted[0] {
		return 0
	}

	res := n
	var open int
	nums := make(map[int]bool)
	for _, evt := range events {
		if evt.second > 0 {
			open++
		} else {
			open--
		}
		nums[abs(evt.second)] = true
		if open == 0 {
			x := evt.first
			if x+1 < n && sorted[x+1] {
				res = min(res, len(nums))
			}
		}
	}

	return min(res, len(nums))
}

func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

func min(a, b int) int {
	if a <= b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a >= b {
		return a
	}
	return b
}

package main

import (

	"fmt"
	"os"
	"bufio"
)

var (

	in = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func solve(n int) {

	var (

		a []int
		cnt = 0
		mp = map[int]int{}
	)
	for i := 0; i < n; i++ {

		var x int
		fmt.Fscan(in, &x)
		a = append(a, x)
		mp[x] = x
	}
	for j := n - 1; j > 0; j-- {

		if mp[a[j]] < mp[a[j-1]] {

			mp[a[j-1]] = 0
			cnt += 1
		}
	}
	for j := n - 1; j > 0; j-- {

		if mp[a[j]] < mp[a[j-1]] {

			mp[a[j-1]] = 0
			cnt += 1
		}
	}

	fmt.Fprintln(out, cnt)
}

func main() {

	defer out.Flush()
	var T int
	fmt.Fscan(in, &T)
	for i := 0; i < T; i++ {

		var n int
		fmt.Fscan(in, &n)
		solve(n)
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

var _, _, _ = main, rw, do

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	rw(r, w)
}

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		w1 := do(n, a)
		fmt.Fprintln(w, w1)
	}
}

func do(n int, a []int) int {
	m := make(map[int]bool)

	for i := n - 2; i >= 0; i-- {
		if a[i] > a[i+1] {
			for j := 0; j <= i; j++ {
				m[a[j]] = true
			}
			break
		}
	}
	for i := n - 1; i >= 0; i-- {
		if m[a[i]] {
			for j := 0; j <= i; j++ {
				m[a[j]] = true
			}
			break
		}
	}
	return len(m)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var bufferedStdin = bufio.NewReader(os.Stdin)
var bufferedStdout = bufio.NewWriter(os.Stdout)

func read(value ...interface{}) {
	_, _ = fmt.Fscan(bufferedStdin, value...)
}

func printf(format string, value ...interface{}) {
	_, _ = fmt.Fprintf(bufferedStdout, format, value...)
}

func printfErr(format string, value ...interface{}) {
	_, _ = fmt.Fprintf(os.Stderr, format, value...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func findElementIds(array []int, f func(int) bool) []int {
	var result = make([]int, 0)
	for i := 0; i < len(array); i++ {
		if f(array[i]) {
			result = append(result, i)
		}
	}
	return result
}

func reduce(array []int, defaultValue int, f func(int, int) int) int {
	for i := 0; i < len(array); i++ {
		defaultValue = f(defaultValue, array[i])
	}
	return defaultValue
}

func solve(test int) {
	var n int
	read(&n)
	var a = make([]int, n)
	for i := 0; i < n; i++ {
		read(&a[i])
	}
	var m = make(map[int]int)
	var lastInd = -1
	for i := n - 2; i >= 0; i-- {
		m[a[i+1]] = max(m[a[i+1]], i+1)
		if a[i] > a[i+1] {
			lastInd = i + 1
			break
		}
	}
	for i := lastInd - 1; i >= 0; i-- {
		if m[a[i]] != 0 {
			lastInd = max(lastInd, m[a[i]]+1)
		}
	}
	var used = make(map[int]bool)
	var ans = 0
	for i := lastInd - 1; i >= 0; i-- {
		if !used[a[i]] {
			ans++
		}
		used[a[i]] = true
	}
	printf("%d\n", ans)
}

func main() {
	var t = 1
	_, _ = fmt.Fscan(bufferedStdin, &t)
	for i := 0; i < t; i++ {
		solve(i + 1)
	}
	_ = bufferedStdout.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type void struct{}
var member void

var (
	reader bufio.Reader
	writer bufio.Writer
)

func max(a int, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func solve() {
	var n int
	scan(&n)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		scan(&a[i])
	}

	pos := make([]int, n + 1)
	for i := 0; i < n; i++ {
		pos[a[i]] = i
	}

	var mx int = -1
	for i := 1; i < n; i++ {
		if a[i - 1] > a[i] {
			mx = max(mx, pos[a[i - 1]])
		}
	}
	for i := 0; i <= mx; i++ {
		mx = max(mx, pos[a[i]])
	}

	q := make(map[int]void)
	for i := 0; i <= mx; i++ {
		q[a[i]] = member
	}

	printf("%v\n", len(q))
}

func main() {
	inp, err := os.Open("in.txt")
	if err == nil {
		os.Stdin = inp
		outp, _ := os.Create("out.txt")
		os.Stdout = outp
		defer outp.Close()
		defer inp.Close()
	}
	reader = *bufio.NewReader(os.Stdin)
	writer = *bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int
	T = 1
	scan(&T)

	for ; T > 0; T-- {
		solve()
	}
}

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(&reader, a...)
}

func printf(format string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(&writer, format, a...)
}

package main

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

func readLine() string {
	s, err := in.ReadString('\n')
	if err != nil {
		panic(err)
	}
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return s
}

func readLines(n int) []string {
	res := make([]string, n)
	for i := 0; i < n; i++ {
		res[i] = readLine()
	}
	return res
}

func readInt() int {
	n, err := strconv.ParseInt(readLine(), 10, 32)
	if err != nil {
		panic(err)
	}
	return int(n)
}

func readInt2() (int, int) {
	as := readInts(2)
	return as[0], as[1]
}

func readInt3() (int, int, int) {
	as := readInts(3)
	return as[0], as[1], as[2]
}

func readUint() uint64 {
	n, err := strconv.ParseUint(readLine(), 10, 64)
	if err != nil {
		panic(err)
	}
	return n
}

func readInts(n int) []int {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	arr := make([]int, n)
	for k, a := range as {
		x, _ := strconv.ParseInt(a, 10, 64)
		arr[k] = int(x)
	}
	return arr
}

func readStrings(n int) []string {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	return as
}

func arr(n, m int) [][]int {
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
	}
	return a
}

const MAXN int = 1e5 + 7

var in = bufio.NewReader(os.Stdin)
var a []int = make([]int, MAXN)
var rcd []int = make([]int, MAXN)

func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, n, i int
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		fmt.Fscan(in, &n)
		for i = 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
			rcd[i] = 0
		}
		if n == 1 {
			fmt.Fprintln(out, 0)
			continue
		}
		ans := 0
		for i = 1; i <= n; i++ {
			rcd[a[i]]++
			if rcd[a[i]] == 1 {
				ans++
			}
		}
		maxLen, pos, last := 0, n+1, n+1
		for pos > 1 {
			temp, val, l := pos-1, a[pos-1], 0
			if val > last {
				break
			}
			for a[temp] == val {
				temp--
				l++
			}
			if rcd[val] == l {
				maxLen += l
				last, pos = val, pos-l
				ans--
			} else {
				break
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	sortZeroRead()
}

func sortZeroRead() {
	in := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscan(in, &t)

	for i := 0; i < t; i++ {
		var n int

		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}

		// solve
		res := sortZero(arr, n)
		fmt.Println(res)
	}
}

func sortZero(arr []int, n int) int {
	invalid := make(map[int]bool)
	pos := make(map[int]int)
	foundInvalid := false
	start, end := -1, -1
	for i := len(arr) - 1; i > 0; i-- {
		if _, ok := pos[arr[i]]; !ok {
			pos[arr[i]] = i
		}
		if arr[i] < arr[i-1] || foundInvalid {
			invalid[arr[i-1]] = true
			posn := max(pos[arr[i-1]], i-1)
			end = max(end, posn)
			if !foundInvalid {
				start = i - 1
			}
			foundInvalid = true
		}
	}
	// check 0th element
	if foundInvalid && arr[0] != 0 {
		invalid[arr[0]] = true
		posn := max(pos[arr[0]], 0)
		end = max(end, posn)
	}

	for i := end; i > start; i-- {
		invalid[arr[i]] = true
	}

	return len(invalid)
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}

		mm := make([]bool, n+1)
		seen := []int{}
		var tot int
		for i := 0; i < n; i++ {
			if len(seen) > 0 && seen[len(seen)-1] == aa[i] {
				continue
			}

			idx := sort.SearchInts(seen, aa[i])
			if mm[aa[i]] && len(seen) > 0 || idx < len(seen) {
				for _, v := range seen {
					mm[v] = true
				}
				tot += len(seen)
				if mm[aa[i]] {
					seen = []int{}
				} else {
					seen = []int{aa[i]}
				}
			} else {
				if !mm[aa[i]] {
					seen = append(seen, aa[i])
				}
			}
		}
		fmt.Fprintln(out, tot)
	}
}

// 00
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		m := make(map[int]int)
		ans := 0
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
			m[a[i]] = a[i]
		}
		i := n - 2
		for i >= 0 {
			if a[i] > a[i+1] {
				break
			}
			i--
		}
		for i >= 0 {
			if m[a[i]] != 0 {
				m[a[i]] = 0
				ans++
			}
			i--
		}

		for i := 0; i < n; i++ {
			a[i] = m[a[i]]
		}

		i = n - 2
		for i >= 0 {
			if a[i] > a[i+1] {
				break
			}
			i--
		}
		for i >= 0 {
			if m[a[i]] != 0 {
				m[a[i]] = 0
				ans++
			}
			i--
		}

		fmt.Println(ans)

	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

// func max(a, b int) int {
// 	if a < b {
// 		return b
// 	} else {
// 		return a
// 	}
// }

// func maxSquare(a int) int {
// 	tmp := int(math.Sqrt(float64(a)))
// 	if tmp*tmp == a {
// 		return tmp * tmp
// 	} else {
// 		return (tmp + 1) * (tmp + 1)
// 	}
// }

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(in, &n)
		slc := make([]int, n)
		count := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &slc[i])
			count[slc[i]]++
		}
		// find rightmost non dec array; delete first part; check in map all before max
		index := 0
		for i := 1; i < n; i++ {
			if slc[i-1] > slc[i] {
				index = i
			}
		}
		//[index:len]
		m := make(map[int]int)
		for i := index; i < n; i++ {
			m[slc[i]]++
		}
		used := make(map[int]bool)
		max := 0
		res := 0
		for i := 0; i < index; i++ {
			//res += count[slc[i]]
			if !used[slc[i]] {
				res++
				used[slc[i]] = true
			}
			// slc[i]
			if m[slc[i]] > 0 {
				m[slc[i]] = 0
				if slc[i] > max {
					max = slc[i]
				}
			}
		}
		//fmt.Println("DEBUG: PREVRES", res, m)
		for k, v := range m {
			if k < max {
				res += min(v, 1)
			}
		}
		//fmt.Println("DEBUG:", index)
		fmt.Fprintln(out, res)
		// add remaining to res
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {
	defer out.Flush()

	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		fmt.Fprintln(out, solve())
	}
}

func solve() int {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	mp := make(map[int]bool)
	res := 0
	for {
		ok := true
		for i := n - 1; i > 0; i-- {
			if mp[a[i]] {
				a[i] = 0
			}
			if mp[a[i-1]] {
				a[i-1] = 0
			}
			// fmt.Println(a, mp)
			if a[i-1] > a[i] {
				res++
				mp[a[i-1]] = true
				a[i-1] = 0
				ok = false
			}
		}
		if ok {
			break
		}
	}
	return res
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		n := readInt(input)
		arr := readArrInt(input)
		fmt.Println(solve(n, arr))
	}
}

func solve(n int, arr []int) int {
	value := [100002]int{}
	for i := 0; i <= n; i++ {
		value[i] = i
	}
	result := 0
	for {
		prev_result := result
		for i := n - 1; i > 0; i-- {
			if value[arr[i-1]] > value[arr[i]] {
				value[arr[i-1]] = 0
				result++
			}
		}
		if prev_result == result {
			return result
		}
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return int64(n)
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

func A2() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, ans int

		Fscan(in, &n)
		a := make([]int, n)
		cnt := make(map[int]bool)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			cnt[a[i]] = true
		}
		m := make(map[int]bool)
		var max = a[n-1]
		var f bool
		var p = n - 1
		for i := n - 2; i >= 0; i-- {
			if a[i] > a[i+1] {
				f = true
			}
			if a[i] == max {
				p = i
			}
		}
		if f {
			Fprintln(out, len(cnt))
			return
		}
		for i := p - 1; i >= 0; i-- {
			if a[i] > max {
				if _, ok := m[a[i]]; !ok {
					ans++
					max = 0
					m[a[i]] = true
				}
			} else {
				max = a[i]
			}
		}
		Fprintln(out, ans)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A2() }

package main

import (
	"bufio"
	. "fmt"
	"os"
)

func A2() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, ans int

		Fscan(in, &n)
		a := make([]int, n)
		cnt := make(map[int]bool)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			cnt[a[i]] = true
		}
		m := make(map[int]bool)
		var max = a[n-1]
		var f bool
		var p = n - 1
		for i := n - 2; i >= 0; i-- {
			if a[i] > a[i+1] {
				f = true
			}
			if a[i] == max {
				p = i
			}
		}
		if p != n-1 && f {
			Fprintln(out, len(cnt))
			return
		}
		for i := p - 1; i >= 0; i-- {
			if a[i] > max {
				if _, ok := m[a[i]]; !ok {
					ans++
					max = 0
					m[a[i]] = true
				}
			}
		}
		Fprintln(out, ans)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A2() }

package main

import (
	"bufio"
	. "fmt"
	"os"
)

func A2() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, ans int

		Fscan(in, &n)
		a := make([]int, n)
		cnt := make(map[int]bool)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			cnt[a[i]] = true
		}
		m := make(map[int]bool)
		var max = a[n-1]
		var f bool
		var p = n - 1
		for i := n - 2; i >= 0; i-- {
			if a[i] > a[i+1] {
				f = true
			}
			if a[i] == max {
				p = i
				break
			}
		}
		if p != n-1 && f {
			Fprintln(out, len(cnt))
			return
		}
		for i := p - 1; i >= 0; i-- {
			if a[i] > max {
				if _, ok := m[a[i]]; !ok {
					ans++
					max = 0
					m[a[i]] = true
				}
			}
		}
		Fprintln(out, ans)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A2() }

package main

import "fmt"

func main() {

	t := 0
	if _, err := fmt.Scan(&t); err != nil {
		return
	}

	for ; t > 0; t-- {
		n, ans := 0, 0
		if _, err := fmt.Scan(&n); err != nil {
			return
		}
		nums := make([]int, n)
		for i := 0; i < n; i++ {
			if _, err := fmt.Scan(&nums[i]); err != nil {
				return
			}
		}
		indices := make(map[int]int)
		zero := make(map[int]bool)
		for i := n - 1; i > 0; i-- {
			if _, ok := indices[nums[i]]; !ok {
				indices[nums[i]] = i
			}
			if nums[i-1] > nums[i] {
				k := 0
				if v, ok := indices[nums[i-1]]; ok {
					k = v
				} else {
					k = i - 1
				}
				for j := 0; j <= k; j++ {
					if _, ok := zero[nums[j]]; !ok {
						zero[nums[j]] = true
						ans++
					}
				}
				break
			}
		}
		fmt.Println(ans)
	}
}

package main

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

func Solve(_in io.Reader, _out io.Writer) {
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		if n == 1 {
			Fprintln(out, 0)
			continue
		}
		a := make([]int, n)
		// 统计 a[i] 最早出现位置
		mp := map[int]int{}
		cnt := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			cnt[i] = len(mp)
			if _, ok := mp[a[i]]; !ok {
				mp[a[i]] = i
			}
		}
		j := n-1
		for ; j >= 0; j-- {
			if i, ok := mp[a[j]]; ok && i < j-1 {
				break
			}
			if j >= 1 && a[j] < a[j-1] {
				break
			}
		}
		if j >= 0 {
			Fprintln(out, cnt[j])
		} else {
			Fprintln(out, 0)
		}
	}
}

func main() { Solve(os.Stdin, os.Stdout) }

package main

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

func Solve(_in io.Reader, _out io.Writer) {
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		if n == 1 {
			Fprintln(out, 0)
			continue
		}
		a := make([]int, n)
		// 统计 a[i] 最早出现位置
		mp := map[int]int{}
		cnt := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			cnt[i] = len(mp)
			if _, ok := mp[a[i]]; !ok {
				mp[a[i]] = i
			}
		}
		j := n-1
		for ; j >= 0; j-- {
			if i, ok := mp[a[j]]; ok && i < j {
				break
			}
			if j >= 1 && a[j] < a[j-1] {
				break
			}
		}
		if j >= 0 {
			Fprintln(out, cnt[j])
		} else {
			Fprintln(out, 0)
		}
	}
}

func main() { Solve(os.Stdin, os.Stdout) }

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	l, r := 0, n+1
	ans := n
	for l <= r {
		border := (l + r) / 2
		localAns := 0
		var s set[int]
		for i := 0; i < border; i++ {
			if !s.has(arr[i]) {
				localAns++
				s.add(arr[i])
			}
		}
		skip := false
		for i := border; i < n; i++ {
			if (i != border && s.has(arr[i])) || (i < n-1 && arr[i] > arr[i+1]) {
				skip = true
				break
			}
		}
		if skip {
			l = border + 1
		} else {
			r = border - 1
			ans = min(ans, localAns)
		}
	}
	fmt.Fprintln(out, ans)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	l, r := 0, n+1
	ans := n
	for l <= r {
		border := (l + r) / 2
		localAns := 0
		var s set[int]
		for i := 0; i < border; i++ {
			if !s.has(arr[i]) {
				localAns++
				s.add(arr[i])
			}
		}
		skip := false
		for i := border; i < n; i++ {
			if s.has(arr[i]) || (i < n-1 && arr[i] > arr[i+1]) {
				skip = true
				break
			}
		}
		if skip {
			l = border + 1
		} else {
			r = border - 1
			ans = min(ans, localAns)
		}
	}
	fmt.Fprintln(out, ans)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}


Since the giant heads have appeared in the sky all humanity is in danger, so all Ricks and Mortys from all parallel universes are gathering in groups to find a solution to get rid of them. There are n parallel universes participating in this event (n Ricks and n Mortys). I. e. each of n universes has one Rick and one Morty. They're gathering in m groups. Each person can be in many groups and a group can contain an arbitrary number of members.Ricks and Mortys have registered online in these groups. So, a person can have joined a group more than once (developer of this website hadn't considered this possibility).  Summer from universe #1 knows that in each parallel universe (including hers) exactly one of Rick and Morty from that universe is a traitor and is loyal, but no one knows which one. She knows that we are doomed if there's a group such that every member in that group is a traitor (they will plan and destroy the world). Summer knows that if there's a possibility that world ends (there's a group where all members are traitors) she should immediately cancel this event. So she wants to know if she should cancel the event. You have to tell her yes if and only if there's at least one scenario (among all 2n possible scenarios, 2 possible scenarios for who a traitor in each universe) such that in that scenario the world will end.

The first line of input contains two integers n and m (1 ≤ n, m ≤ 104) — number of universes and number of groups respectively. The next m lines contain the information about the groups. i-th of them first contains an integer k (number of times someone joined i-th group, k > 0) followed by k integers vi, 1, vi, 2, ..., vi, k. If vi, j is negative, it means that Rick from universe number  - vi, j has joined this group and otherwise it means that Morty from universe number vi, j has joined it. Sum of k for all groups does not exceed 104.

In a single line print the answer to Summer's question. Print "YES" if she should cancel the event and "NO" otherwise.

NoteIn the first sample testcase, 1st group only contains the Rick from universe number 3, so in case he's a traitor, then all members of this group are traitors and so Summer should cancel the event.

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, m, k int
	var v float64
	var output string

	fmt.Scan(&n, &m)

	output = "NO"

	for i := 0; i < m; i++ {
		fmt.Scan(&k)
		tmpMap := make(map[float64]int)

		for z := 0; z < k; z++ {
			fmt.Scan(&v)
			tmpMap[v] = 1
		}

		tmpMap2 := make(map[float64]int)

		for k := range tmpMap {
			tmpMap2[math.Abs(k)] = tmpMap2[math.Abs(k)] + 1
		}

		tmpCount := 0

		for _, v := range tmpMap2 {
			if v == 1 {
				tmpCount++
			}
		}

		if tmpCount == len(tmpMap) {
			output = "YES"
			continue
		}
	}
	fmt.Println(output)
}

package main

import "fmt"

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	ans := make(chan bool)
	for group := 0; group < m; group++ {
		var k int
		fmt.Scan(&k)
		arr := make([]int, k)
		for person := 0; person < k; person++ {
			fmt.Scan(&arr[person])
		}
		go func() {
			m := make(map[int]bool)
			for _, i := range arr {
				m[i] = true
			}
			for _, i := range arr {
				if m[-i] {
					ans <- true
					return
				}
			}
			ans <- false
		}()
	}
	for i := 0; i < m; i++ {
		x := <-ans
		if x == false {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

package main

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

func solve() {
	n := readInt()
	m := readInt()
	ans := "NO"
	for i := 0; i < m; i++ {
		k := readInt()
		a := make([]int, n+1)
		cancel := true
		for j := 0; j < k; j++ {
			v := readInt()
			if a[abs(v)] == -v {
				cancel = false
			}
			a[abs(v)] = v
		}
		if cancel {
			ans = "YES"
		}
	}
	fmt.Println(ans)
}

func abs(val int) int {
	if val < 0 {
		return -val
	}
	return val
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

func main() {
	var flag, globalflag bool
	var line string
	var n, m, tmp int
	var arrS []string
	var arrI, arrTmp []int
	reader := bufio.NewReader(os.Stdin)
	fmt.Scanf("%d %d\n", &n, &m)
	for i := 0; i < m; i++ {
		arrI, arrTmp = []int{}, []int{}
		line, _ = reader.ReadString('\n')
		arrS = strings.Split(strings.TrimSpace(line), " ")
		for j := range arrS {
			tmp, _ = strconv.Atoi(arrS[j])
			arrI = append(arrI, tmp)
		}
		arrI = arrI[1:]
		for h := 0; h < len(arrI); h++ {
			for g := 0; g < len(arrTmp); g++ {
				if -arrI[h] == arrTmp[g] {
					flag = true
					break
				}
			}
			if flag {
				break
			} else {
				arrTmp = append(arrTmp, arrI[h])
			}
		}
		if flag {
			flag = false
		} else {
			globalflag = true
			break
		}
	}
	if globalflag {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, m, k int
	var tmp float64
	var output string

	fmt.Scan(&n, &m)

	output = "NO"

	for i := 0; i < m; i++ {
		fmt.Scan(&k)
		tmpMap := make(map[float64]float64)

		for z := 0; z < k; z++ {
			fmt.Scan(&tmp)
			tmpAbs := math.Abs(tmp)
			tmpMap[tmpAbs] = tmpMap[tmpAbs] + 1
		}

		tmpCount := 0

		for _, v := range tmpMap {
			if v == 1 {
				tmpCount++
			}
		}
		// fmt.Println(tmpCount, len(tmpMap))
		if tmpCount == len(tmpMap) {
			output = "YES"
			continue
		}
	}
	fmt.Println(output)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, m, k int
	var tmp float64
	var output string

	fmt.Scan(&n, &m)

	output = "NO"

	for i := 0; i < m; i++ {
		fmt.Scan(&k)
		tmpMap := make(map[float64]float64)

		for z := 0; z < k; z++ {
			fmt.Scan(&tmp)
			tmpAbs := math.Abs(tmp)
			tmpMap[tmpAbs] = tmpMap[tmpAbs] + 1
		}

		if len(tmpMap) == 1 {
			output = "YES"
			continue
		}

		tmpCount := 0

		for _, v := range tmpMap {
			if v == 1 {
				tmpCount++
			}
		}
		// fmt.Println(tmpCount, len(tmpMap))
		if tmpCount == len(tmpMap) {
			output = "YES"
			continue
		}
	}
	fmt.Println(output)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, m, k int
	var v float64
	var output string

	fmt.Scan(&n, &m)

	output = "NO"

	for i := 0; i < m; i++ {
		fmt.Scan(&k)
		tmpMap := make(map[float64]int)

		for z := 0; z < k; z++ {
			fmt.Scan(&v)
			tmpMap[math.Abs(v)] = tmpMap[math.Abs(v)] + 1
		}

		tmpCount := 0

		for _, v := range tmpMap {
			if v == 1 {
				tmpCount++
			}
		}

		if tmpCount == len(tmpMap) {
			output = "YES"
			continue
		}
	}
	fmt.Println(output)
}

package main

import (
	"fmt"
)

func main() {
	var n, m, k, ch int
	var arr []int
	var flag bool
	var globalflag bool
	fmt.Scanf("%d %d\n", &n, &m)
	for i := 0; i < m; i++ {
		arr = []int{}
		fmt.Scanf("%d", &k)
		for j := 0; j < k; j++ {
			fmt.Scanf("%d", &ch)
			for l := 0; l < len(arr); l++ {
				if -ch == arr[l] {
					flag = true
					break
				}
			}
			if flag {
				break
			} else {
				arr = append(arr, ch)
			}
		}
		if flag {
			//fmt.Printf("Group %d is OK\n", i)
			flag = false
		} else {
			//fmt.Printf("Group %d is not OK\n", i)
			globalflag = true
			break
		}
	}
	if globalflag {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}


Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.  Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.

The first line of input contains integer n (1 ≤ n ≤ 106), the number of weights. The second line contains n integers w1, ..., wn separated by spaces (0 ≤ wi ≤ 106 for each 1 ≤ i ≤ n), the powers of two forming the weights values.

Print the minimum number of steps in a single line.

NoteIn the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer = bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  weights := make([]int, n)
  for i := range weights {
    weights[i] = getI()
  }
  sort.Ints(weights)
  result := 0
  power := weights[0]
  count := 1
  for i := 1; i < n; i++ {
    w := weights[i]
    for power < w {
      result += count % 2
      count /= 2
      power += 1
    }
    count += 1
  }
  for count != 0 {
    result += count % 2
    count /= 2
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func ReadInt32() int64 {
	scanner.Scan()
	return toInt(scanner.Bytes())
}

func toInt(buf []byte) (n int64) {
	for _, v := range buf {
		n = n*10 + int64(v-'0')
	}
	return n
}

func main() {
	//:	fmt.Print64(strconv.IntSize)
	scanner.Split(bufio.ScanWords)

	//var n int64
	m := ReadInt32()
	var totalWeight [1000040]int
	var length int = 1000040
	var result int
	for ; m > 0; m-- {
		wi := ReadInt32()
		totalWeight[wi]++
	}

	for i := 0; i < length; i++ {
		if totalWeight[i] > 1 {
			totalWeight[i+1] += totalWeight[i] / 2
			totalWeight[i] = totalWeight[i] % 2
		}
		result += totalWeight[i]

	}
	fmt.Print(result)

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, t int

	fmt.Fscanln(reader, &n)
	cnt := make([]int, 1e6+1e4)

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &t)
		cnt[t]++
	}

	res := 0

	for i := 0; i <= 1e6 + 1000; i++ {
		cnt[i+1] += cnt[i] / 2
		cnt[i] %= 2
		res += cnt[i]
	}

	fmt.Println(res)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, t int

	fmt.Fscanln(reader, &n)
	cnt := make([]int, 1e6+10)

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &t)
		cnt[t]++
	}

	res := 0

	for i := 0; i <= 1e6 + 5; i++ {
		cnt[i+1] += cnt[i] / 2
		cnt[i] %= 2
		res += cnt[i]
	}

	fmt.Println(res)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, t int

	fmt.Fscanln(reader, &n)
	cnt := make([]int, 1e6+5)

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &t)
		cnt[t]++
	}

	res := 0

	for i := 0; i <= 1e6; i++ {
		cnt[i+1] += cnt[i] / 2
		cnt[i] %= 2
		res += cnt[i]
	}

	fmt.Println(res)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, t int

	fmt.Fscanln(reader, &n)
	cnt := make([]int, 1e6+5)

	for i := 0; i < n; i++ {
		fmt.Scan(&t)
		cnt[t]++
	}

	res := 0

	for i := 0; i <= 1e6; i++ {
		cnt[i+1] = cnt[i] / 2
		cnt[i] %= 2
		res += cnt[i]
	}

	fmt.Println(res)
}


On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya.Every man is characterized by two numbers: ai, which is the importance of his current task (the greater the number is, the more important the task is) and number ci, which is a picture of his conscience. Numbers ai form the permutation of numbers from 1 to n.Let the queue consist of n - 1 people at the moment. Let's look at the way the person who came number n behaves. First, he stands at the end of the queue and the does the following: if importance of the task ai of the man in front of him is less than an, they swap their places (it looks like this: the man number n asks the one before him: "Erm... Excuse me please but it's very important for me... could you please let me move up the queue?"), then he again poses the question to the man in front of him and so on. But in case when ai is greater than an, moving up the queue stops. However, the man number n can perform the operation no more than cn times.In our task let us suppose that by the moment when the man number n joins the queue, the process of swaps between n - 1 will have stopped. If the swap is possible it necessarily takes place.Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops.

The first input line contains an integer n which is the number of people who has joined the queue (1 ≤ n ≤ 105). In the next n lines descriptions of the people are given in order of their coming — space-separated integers ai and ci (1 ≤ ai ≤ n, 0 ≤ ci ≤ n). Every description is located on s single line. All the ai's are different.

Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space.

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

type RB struct {
	black       bool
	left, right *RB
	count       int
	a, maxa     int
	index       int
}

var rb *RB

func isBlack(root *RB) bool {
	return root == nil || root.black
}
func isRed(root *RB) bool {
	return !isBlack(root)
}

func setBlack(root *RB) {
	if root != nil {
		root.black = true
	}
}

func resetValue(root *RB) {
	if root == nil {
		return
	}
	root.count = 1
	root.maxa = root.a
	if root.left != nil {
		root.count += root.left.count
		if root.left.maxa > root.maxa {
			root.maxa = root.left.maxa
		}
	}
	if root.right != nil {
		root.count += root.right.count
		if root.right.maxa > root.maxa {
			root.maxa = root.right.maxa
		}
	}
}

func insert(grandRoot **RB, root **RB, a int, c int, index int) {
	if *root == nil {
		*root = &RB{grandRoot == nil, nil, nil, 1, a, a, index}
		return
	}
	left := true
	var child **RB
	if c == 0 || ((*root).right != nil && (*root).right.count >= c) || (*root).a > a || ((*root).right != nil && (*root).right.maxa > a) {
		left = false
		insert(root, &(*root).right, a, c, index)
	} else if (*root).right == nil {
		insert(root, &(*root).left, a, c-1, index)
	} else {
		insert(root, &(*root).left, a, c-1-(*root).right.count, index)
	}
	if left {
		child = &(*root).left
	} else {
		child = &(*root).right
	}
	if grandRoot == nil || isBlack(*root) || isBlack(*child) {
		resetValue(*root)
		return
	}
	leftChild := true
	var sibling **RB
	if *root == (*grandRoot).left {
		sibling = &(*grandRoot).right
	} else if *root == (*grandRoot).right {
		leftChild = false
		sibling = &(*grandRoot).left
	} else {
		resetValue(*root)
		return
	}
	tmpChild, tmpRoot, tmpGrandRoot := *child, *root, *grandRoot
	if isRed(*sibling) {
		setBlack(*root)
		setBlack(*sibling)
		(*grandRoot).black = false
		resetValue(tmpRoot)
		return
	} else if left && leftChild {

		setBlack((*root))
		tmpGrandRoot.black = false
		tmpGrandRoot.left = (*root).right
		tmpRoot.right = tmpGrandRoot
		*grandRoot = tmpRoot
		resetValue(tmpRoot.right)
		resetValue(tmpRoot)
	} else if !left && leftChild {
		setBlack(*child)
		tmpGrandRoot.black = false
		tmpRoot.right = tmpChild.left
		tmpChild.left = *root
		tmpGrandRoot.left = tmpChild.right
		tmpChild.right = *grandRoot
		*grandRoot = tmpChild
		resetValue(tmpChild.left)
		resetValue(tmpChild.right)
		resetValue(tmpChild)
	} else if !left && !leftChild {
		setBlack((*root))
		tmpGrandRoot.black = false
		tmpGrandRoot.right = (*root).left
		tmpRoot.left = tmpGrandRoot
		*grandRoot = tmpRoot
		resetValue(tmpRoot.left)
		resetValue(tmpRoot)
	} else if left && !leftChild {
		setBlack(*child)
		tmpGrandRoot.black = false
		tmpRoot.left = tmpChild.right
		tmpChild.right = *root
		tmpGrandRoot.right = tmpChild.left
		tmpChild.left = *grandRoot
		*grandRoot = tmpChild
		resetValue(tmpChild.left)
		resetValue(tmpChild.right)
		resetValue(tmpChild)
	}
	return
}
func preOrder(root *RB) {
	if root == nil {
		return
	}
	fmt.Printf("%d:", root.index)
	if root.black {
		fmt.Printf("B ")
	} else {
		fmt.Printf("R ")
	}
	preOrder(root.left)
	preOrder(root.right)
}
func midOrder(root *RB) {
	if root == nil {
		return
	}
	midOrder(root.left)
	fmt.Printf("%d ", root.index)
	midOrder(root.right)
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	var a, c int

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &a, &c)
		insert(nil, &rb, a, c, i)
		setBlack(rb)
	}
	midOrder(rb)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

type RB struct {
	black       bool
	left, right *RB
	count       int
	a, maxa     int
	index       int
}

var rb *RB

func isBlack(root *RB) bool {
	return root == nil || root.black
}
func isRed(root *RB) bool {
	return !isBlack(root)
}

func setBlack(root *RB) {
	if root != nil {
		root.black = true
	}
}

func resetValue(root *RB) {
	if root == nil {
		return
	}
	root.count = 1
	root.maxa = root.a
	if root.left != nil {
		root.count += root.left.count
		if root.left.maxa > root.maxa {
			root.maxa = root.left.maxa
		}
	}
	if root.right != nil {
		root.count += root.right.count
		if root.right.maxa > root.maxa {
			root.maxa = root.right.maxa
		}
	}
}

func insert(grandRoot **RB, root **RB, a int, c int, index int) {
	if *root == nil {
		*root = &RB{grandRoot == nil, nil, nil, 1, a, a, index}
		return
	}
	left := true
	var child **RB
	if c == 0 || ((*root).right != nil && (*root).right.count >= c) || (*root).a > a || ((*root).right != nil && (*root).right.maxa > a) {
		left = false
		insert(root, &(*root).right, a, c, index)
	} else if (*root).right == nil {
		insert(root, &(*root).left, a, c-1, index)
	} else {
		insert(root, &(*root).left, a, c-1-(*root).right.count, index)
	}
	if left {
		child = &(*root).left
	} else {
		child = &(*root).right
	}
	if grandRoot == nil || isBlack(*root) || isBlack(*child) {
		resetValue(*root)
		return
	}
	leftChild := true
	var sibling **RB
	if *root == (*grandRoot).left {
		sibling = &(*grandRoot).right
	} else if *root == (*grandRoot).right {
		leftChild = false
		sibling = &(*grandRoot).left
	} else {
		resetValue(*root)
		return
	}
	tmpChild, tmpRoot, tmpGrandRoot := *child, *root, *grandRoot
	if isRed(*sibling) {
		setBlack(*root)
		setBlack(*sibling)
		(*grandRoot).black = false
		resetValue(tmpRoot)
		return
	} else if left && leftChild {
		setBlack((*root))
		tmpGrandRoot.black = false
		tmpGrandRoot.left = (*root).right
		tmpRoot.right = tmpGrandRoot
		*grandRoot = tmpRoot
		resetValue(tmpRoot.right)
		resetValue(tmpRoot)
	} else if !left && leftChild {
		setBlack(*child)
		tmpGrandRoot.black = false
		tmpRoot.right = tmpChild.left
		tmpChild.left = *root
		tmpChild.right = *grandRoot
		tmpGrandRoot.left = nil
		*grandRoot = tmpChild
		resetValue(tmpChild.left)
		resetValue(tmpChild.right)
		resetValue(tmpChild)
	} else if !left && !leftChild {
		setBlack((*root))
		tmpGrandRoot.black = false
		tmpGrandRoot.right = (*root).left
		tmpRoot.left = tmpGrandRoot
		*grandRoot = tmpRoot
		resetValue(tmpRoot.left)
		resetValue(tmpRoot)
	} else if left && !leftChild {
		setBlack(*child)
		tmpGrandRoot.black = false
		tmpRoot.left = tmpChild.right
		tmpChild.right = *root
		tmpChild.left = *grandRoot
		tmpGrandRoot.right = nil
		*grandRoot = tmpChild
		resetValue(tmpChild.left)
		resetValue(tmpChild.right)
		resetValue(tmpChild)
	}
	return
}
func preOrder(root *RB) {
	if root == nil {
		return
	}
	fmt.Printf("%d:", root.index)
	if root.black {
		fmt.Printf("B ")
	} else {
		fmt.Printf("R ")
	}
	preOrder(root.left)
	preOrder(root.right)
}
func midOrder(root *RB) {
	if root == nil {
		return
	}
	midOrder(root.left)
	fmt.Printf("%d ", root.index)
	midOrder(root.right)
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	var a, c int

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &a, &c)
		insert(nil, &rb, a, c, i)
		setBlack(rb)
	}
	midOrder(rb)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

type Splay struct {
	left, right           *Splay
	leftCount, rightCount int
	a, maxa               int
	index                 int
}

func insert(root **Splay, a int, c int, index int) *Splay {

	var ret *Splay
	if *root == nil {
		*root = &Splay{nil, nil, 0, 0, a, a, index}
		return *root
	}

	if (*root).rightCount >= c || (*root).a > a || ((*root).right != nil && (*root).right.maxa > c) {
		ret = insert(&(*root).right, a, c, index)
		(*root).rightCount++
	} else {
		ret = insert(&(*root).left, a, c-((*root).rightCount+1), index)
		(*root).leftCount++
	}
	if ret.a > (*root).maxa {
		(*root).maxa = ret.a
	}
	return ret
}

func midOrder(root *Splay) {
	if root == nil {
		return
	}
	midOrder(root.left)
	fmt.Printf("%d ", root.index)
	midOrder(root.right)
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	var root *Splay
	var a, c int

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &a, &c)
		insert(&root, a, c, i)
	}
	midOrder(root)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

type RB struct {
	left, right *RB
	count       int
	a, maxa     int
	index       int
}

var rb *RB

func insert(root **RB, a int, c int, index int) {
	if *root == nil {
		*root = &RB{nil, nil, 1, a, a, index}
		return
	}

	if ((*root).right != nil && (*root).count >= c) || (*root).a > a || ((*root).right != nil && (*root).right.maxa > a) {
		insert(&(*root).right, a, c, index)
	} else if (*root).right == nil {
		insert(&(*root).left, a, c-1, index)
	} else {
		insert(&(*root).left, a, c-1-(*root).right.count, index)
	}
	(*root).count++
	(*root).maxa = (*root).a
	if (*root).right != nil && (*root).right.maxa > (*root).maxa {
		(*root).maxa = (*root).right.maxa
	}
	if (*root).left != nil && (*root).left.maxa > (*root).maxa {
		(*root).maxa = (*root).left.maxa
	}
	return
}

func midOrder(root *RB) {
	if root == nil {
		return
	}
	midOrder(root.left)
	fmt.Printf("%d ", root.index)
	midOrder(root.right)
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	var a, c int

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &a, &c)
		insert(&rb, a, c, i)
	}
	midOrder(rb)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

type RB struct {
	left, right *RB
	count       int
	a, maxa     int
	index       int
}

func insert(root **RB, a int, c int, index int) {
	if *root == nil {
		*root = &RB{nil, nil, 1, a, a, index}
		return
	}
	if ((*root).right != nil && (*root).right.count >= c) || (*root).a > a || ((*root).right != nil && (*root).right.maxa > a) {
		insert(&(*root).right, a, c, index)
	} else if (*root).right == nil {
		insert(&(*root).left, a, c-1, index)
	} else {
		insert(&(*root).left, a, c-1-(*root).right.count, index)
	}
	(*root).count++
	(*root).maxa = (*root).a
	if (*root).right != nil && (*root).right.maxa > (*root).maxa {
		(*root).maxa = (*root).right.maxa
	}
	if (*root).left != nil && (*root).left.maxa > (*root).maxa {
		(*root).maxa = (*root).left.maxa
	}
	return
}

func midOrder(root *RB) {
	if root == nil {
		return
	}
	midOrder(root.left)
	fmt.Printf("%d ", root.index)
	midOrder(root.right)
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	var a, c int

	var root *RB
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &a, &c)
		insert(&root, a, c, i)
	}
	midOrder(root)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

type RB struct {
	black       bool
	left, right *RB
	count       int
	a, maxa     int
	index       int
}

func isBlack(root *RB) bool {
	return root == nil || root.black
}
func isRed(root *RB) bool {
	return !isBlack(root)
}

func setBlack(root *RB) {
	if root != nil {
		root.black = true
	}
}

func resetValue(root *RB) {
	if root == nil {
		return
	}
	root.count = 1
	root.maxa = root.a
	if root.left != nil {
		root.count += root.left.count
		if root.left.maxa > root.maxa {
			root.maxa = root.left.maxa
		}
	}
	if root.right != nil {
		root.count += root.right.count
		if root.right.maxa > root.maxa {
			root.maxa = root.right.maxa
		}
	}
}

func insert(grandRoot **RB, root **RB, a int, c int, index int) {
	if *root == nil {
		*root = &RB{grandRoot == nil, nil, nil, 1, a, a, index}
		return
	}
	left := true
	var child **RB
	if c == 0 || ((*root).right != nil && (*root).right.count >= c) || (*root).a > a || ((*root).right != nil && (*root).right.maxa > a) {
		left = false
		insert(root, &(*root).right, a, c, index)
	} else if (*root).right == nil {
		insert(root, &(*root).left, a, c-1, index)
	} else {
		insert(root, &(*root).left, a, c-1-(*root).right.count, index)
	}
	if left {
		child = &(*root).left
	} else {
		child = &(*root).right
	}
	if grandRoot == nil || isBlack(*root) || isBlack(*child) {
		resetValue(*root)
		return
	}
	leftChild := true
	var sibling **RB
	if *root == (*grandRoot).left {
		sibling = &(*grandRoot).right
	} else if *root == (*grandRoot).right {
		leftChild = false
		sibling = &(*grandRoot).left
	} else {
		resetValue(*root)
		return
	}
	tmpChild, tmpRoot, tmpGrandRoot := *child, *root, *grandRoot
	if isRed(*sibling) {
		setBlack(*root)
		setBlack(*sibling)
		(*grandRoot).black = false
		resetValue(tmpRoot)
		return
	} else if left && leftChild {
		setBlack((*root))
		tmpGrandRoot.black = false
		tmpGrandRoot.left = (*root).right
		tmpRoot.right = tmpGrandRoot
		*grandRoot = tmpRoot
		resetValue(tmpRoot.right)
		resetValue(tmpRoot)
	} else if !left && leftChild {
		setBlack(*child)
		tmpGrandRoot.black = false
		tmpChild.left = *root
		tmpChild.right = *grandRoot
		tmpGrandRoot.left = nil
		tmpRoot.right = nil
		*grandRoot = tmpChild
		resetValue(tmpChild.left)
		resetValue(tmpChild.right)
		resetValue(tmpChild)
	} else if !left && !leftChild {
		setBlack((*root))
		tmpGrandRoot.black = false
		tmpGrandRoot.right = (*root).left
		tmpRoot.left = tmpGrandRoot
		*grandRoot = tmpRoot
		resetValue(tmpRoot.left)
		resetValue(tmpRoot)
	} else if left && !leftChild {
		setBlack(*child)
		tmpGrandRoot.black = false
		tmpChild.right = *root
		tmpChild.left = *grandRoot
		tmpGrandRoot.right = nil
		tmpRoot.left = nil
		*grandRoot = tmpChild
		resetValue(tmpChild.left)
		resetValue(tmpChild.right)
		resetValue(tmpChild)
	}
	return
}
func preOrder(root *RB) {
	if root == nil {
		return
	}
	fmt.Printf("%d ", root.index)
	preOrder(root.left)
	preOrder(root.right)
}
func midOrder(root *RB) {
	if root == nil {
		return
	}
	midOrder(root.left)
	fmt.Printf("%d ", root.index)
	midOrder(root.right)
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	var a, c int

	var root *RB
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &a, &c)
		insert(nil, &root, a, c, i)
	}
	midOrder(root)
}