hdu5569 RMQ

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>

#define LL long long
#define PB push_back
#define N 50050

using namespace std;
int a[N][2], c[N], sum[N];
int rmq[N][20][2];

int RMQ(int l, int r, int type) {
    int len = r-l+1;
    int ln = log(len) / log(2);
    return max( rmq[l][ln][type], rmq[r-(1<<ln)+1][ln][type]);
}
int main() {
	int t;
	scanf("%d", &t);
	while(t-- ) {
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i=0; i<n; ++i) {
            scanf("%d%d", &a[i][0], &a[i][1]);
        }
        memset(sum, 0, sizeof(sum));
        for(int i=0; i<m; ++i) {
            scanf("%d", &c[i]);
            --c[i];
            if(c[i] <= n) ++sum[ c[i] ];
        }
        for(int i=1; i<n; ++i) {
            sum[i] += sum[i-1];
        }
        for(int i=0; i<n; ++i)
        for(int j=0; j<20; ++j) {
            rmq[i][j][0] = rmq[i][j][1] = -INT_MAX;
        }
        for(int i=0; i<n; ++i) {
            a[i][1] -= (i==0 ? 0:sum[i-1]);
            rmq[i][0][ a[i][0] ] = a[i][1];
        }
        int ln = log(n) / log(2) + 1;
        for(int i=1; i<=ln; ++i) {
            for(int j=0; j+(1<<i)<=n; ++j) { //<=!!!!!
                rmq[j][i][0] = max(rmq[j][i-1][0], rmq[j+(1<<(i-1))][i-1][0]);
                rmq[j][i][1] = max(rmq[j][i-1][1], rmq[j+(1<<(i-1))][i-1][1]);
            }
        }
        int num = 0;
        for(int i=0; i<n-1; ++i) {
            if(RMQ(i+1, n-1, a[i][0]^1) > a[i][1]) num++;
        }
        printf("%d\n", n-num);
	}
	return 0;
}

你可能感兴趣的:(hdu5569 RMQ)