思路:用两个优先队列,表示是在0队里,还是在1队里。如果现在
ai
是0,把
bi−ci
插入第一个优先队列,(
ci
表示在
i−1
这一秒有多少次发功,因为把
b1…bi−1
加1,相当于把
bi
减一) ,在第二个优先队列中查找,如果优先队列的队首元素小于
bi−ci
,就删掉这个元素,一直删到优先队列的队首元素
≥bi−ci
。插入的复杂度是
log(n)
,所以复杂度
n∗log(n)
。
http://acm.hdu.edu.cn/showproblem.php?pid=5596
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
using namespace std;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e4+5;
const int MAXE = 2e5+5;
typedef long long LL;
typedef unsigned long long ULL;
int T,n,m,k;
struct Node {
int x;
bool operator < (const Node B) const {
return x > B.x;
}
Node(int _x) {x = _x;}
};
priority_queue<Node>p1;
priority_queue<Node>p2;
int a[MAXN];
int b[MAXN];
int c[MAXN];
void input() {
scanf("%d %d",&n,&m);
int tmp; cls(c,0);
rep(i,1,n) scanf("%d %d",&a[i],&b[i]);
rep(i,1,m) scanf("%d",&tmp) , c[tmp+1] ++ ;
rep(i,1,n) c[i] += c[i-1];
}
void solve() {
while(!p1.empty())p1.pop();
while(!p2.empty())p2.pop();
rep(i,1,n) {
int tmp = b[i] - c[i];
if(a[i]) {
while(!p2.empty() && p2.top().x < tmp)p2.pop();
p1.push(Node(tmp));
}
else {
while(!p1.empty() && p1.top().x < tmp)p1.pop();
p2.push(Node(tmp));
}
}
printf("%d\n",p1.size()+p2.size());
}
int main(void) {
scanf("%d",&T); while(T--) {
input();
solve();
}
return 0;
}