// (>_<)ムリ!! #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; } bool operator == (const node& x) const{ return (cnt == x.cnt) && (l == x.l) && (m == x.m); } }; 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; } }; ostream& operator << (ostream& os, const node& x){ os << "{" << x.cnt << ", " << x.l << ", " << x.m << "} "; return os; } ostream& operator << (ostream& os, const val& x){ os << "{ " << x.a << ", " << x.b << "} "; return os; } val comp(const val& x, const val& y){ val ret; if(x.a < y.a){ ret.a = y.a; if(x.a < y.b){ if(ret.a.m != y.b.m) ret.b = y.b; else ret.b = x.a; }else{ if(ret.a.m != x.a.m) ret.b = x.a; else ret.b = y.b; } }else{ ret.a = x.a; if(x.b < y.a){ if(ret.a.m != y.a.m) ret.b = y.a; else ret.b = x.b; }else{ if(ret.a.m != x.b.m) ret.b = x.b; else ret.b = y.a; } } return ret; } val hoge(const 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)){ ret = comp(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 = comp(ret, tmp); } }else{ ret = hoge(v[pos], a); } return ret; } val x; x = get(l,r, a,cnt, lb, (lb+ub)/2, pos*2+1); //x = hoge(x, a); val y; y = get(l,r, a,cnt, (lb+ub)/2, ub, pos*2+2); //y = hoge(y, a); val ret = comp(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