#include #include #include #include #include #include #include #include #include #include using namespace std; struct node{ int cnt; int l; int m; node():cnt(0),l(0),m(0){} bool operator < (const node& x) const{ return cnt < x.cnt; } }; struct val{ node a; node b; val operator +(const val& x){ val ret; priority_queue, less> pq; pq.push(a); pq.push(b); pq.push(x.a); pq.push(x.b); ret.a = pq.top(); pq.pop(); bool ok = false; while(pq.size() && !ok){ if(pq.top().l != ret.a.l){ ok = true; ret.b = pq.top(); }else{ pq.pop(); } } return ret; } }; val hoge(val& x, int a){ val ret; if(x.a.l != a){ ret.a = x.a; if(x.b.l != a){ ret.b = x.b; } }else if(x.b.l != a){ ret.a = x.b; } return ret; } class seg{ public: vector v; int size; seg(int n){ size = 1; while(size1)){ val tmp = get(l,r,a,1, lb,(lb+ub)/2, pos*2+1) + get(l,r,a,1, (lb+ub)/2,ub, pos*2+2); ret = ret + tmp; } }else{ ret = hoge(v[pos], a); } return ret; } val x; if(!((lb+ub)/2<=l || r<=lb)) x = get(l,r, a,cnt, lb, (lb+ub)/2, pos*2+1); //x = hoge(x, a); val y; if(!(ub<=l || r<=(lb+ub)/2)) y = get(l,r, a,cnt, (lb+ub)/2, ub, pos*2+2); //y = hoge(y, a); val ret = x+y; return ret; } val get(int l, int r, int a){ return get(l,r, a,2, 0,size,0); } }; template vector compress(vector a, int& size){ auto b = a; sort(b.begin(), b.end()); b.erase( unique(b.begin(), b.end()), b.end()); for(int i=0; i> n; vector a(n); for(int i=0; i