There are n people taking part in auction today. The rules of auction are classical. There were n bids made, though it's not guaranteed they were from different people. It might happen that some people made no bids at all.
Each bid is define by two integers (ai, bi), where ai is the index of the person, who made this bid and bi is its size. Bids are given in chronological order, meaning bi < bi + 1 for all i < n. Moreover, participant never makes two bids in a row (no one updates his own bid), i.e. ai ≠ ai + 1 for all i < n.
Now you are curious with the following question: who (and which bid) will win the auction if some participants were absent? Consider that if someone was absent, all his bids are just removed and no new bids are added.
Note, that if during this imaginary exclusion of some participants it happens that some of the remaining participants makes a bid twice (or more times) in a row, only first of these bids is counted. For better understanding take a look at the samples.
You have several questions in your mind, compute the answer for each of them.
The first line of the input contains an integer n (1 ≤ n ≤ 200 000) — the number of participants and bids.
Each of the following n lines contains two integers ai and bi (1 ≤ ai ≤ n, 1 ≤ bi ≤ 109, bi < bi + 1) — the number of participant who made the i-th bid and the size of this bid.
Next line contains an integer q (1 ≤ q ≤ 200 000) — the number of question you have in mind.
Each of next q lines contains an integer k (1 ≤ k ≤ n), followed by k integers lj (1 ≤ lj ≤ n) — the number of people who are not coming in this question and their indices. It is guarenteed that lj values are different for a single question.
It's guaranteed that the sum of k over all question won't exceed 200 000.
For each question print two integer — the index of the winner and the size of the winning bid. If there is no winner (there are no remaining bids at all), print two zeroes.
6
1 10
2 100
3 1000
1 10000
2 100000
3 1000000
3
1 3
2 2 3
2 1 2
2 100000
1 10
3 1000
3
1 10
2 100
1 1000
2
2 1 2
2 2 3
0 0
1 10
Consider the first sample:
- In the first question participant number 3 is absent so the sequence of bids looks as follows:
- 1 10
- 2 100
- 1 10 000
- 2 100 000
- In the second question participants 2 and 3 are absent, so the sequence of bids looks:
- 1 10
- 1 10 000
- In the third question participants 1 and 2 are absent and the sequence is:
- 3 1 000
- 3 1 000 000
分析:预处理每个人出价最高的时候并排序;
每次走掉K个人,赢家是剩下的最大的,出钱时在自己的出价表里二分次大值即可;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] #define sys system("pause") #define intxt freopen("in.txt","r",stdin) const int maxn=2e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline void umax(int &p,int q){if(p<q)p=q;} inline void umin(int &p,int q){if(p>q)p=q;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,k,t,a[maxn],id[maxn],cnt,vis[maxn]; bool cmp(const int &p,const int &q) { return a[p]<a[q]; } vi pq[maxn]; int main() { int i,j; scanf("%d",&n); rep(i,1,n)id[i]=i; rep(i,1,n)j=read(),k=read(),umax(a[j],k),pq[j].pb(k); sort(id+1,id+n+1,cmp); int q; scanf("%d",&q); while(q--) { ++cnt; int win=0; scanf("%d",&m); rep(i,1,m)j=read(),vis[j]=cnt; for(i=n;i>=1;i--) { if(vis[id[i]]!=cnt) { if(win)break; else win=id[i]; } } if(win&&a[win])printf("%d %d ",win,*lower_bound(pq[win].begin(),pq[win].end(),a[id[i]])); else puts("0 0"); } //system("Pause"); return 0; }