CodeForces - 369E Valera and Queries 区间包含问题 (树状数组+离线)

题目连接

题意:给出N条线段,有M个询问,询问的内容是,k个点,共落在多少条所给线段上

做法:

将每一个查询的点记录下来,存入用这些点的补集,比如选中1,3,6

那么[2,2],[4,5] [7,N]就是补集,这个题反向做如果这些补集包含了之前已存在的区间。

那么这些点一定不会在存在的区间里面,所以只需看这些补集包含的区间数。

用总数减去区间数就是我们想要的答案。

然而我们还需要离线的存入每个查询的补集左右端点,若按照l大的排前面,r小的排后面。

那么可以保证,每次需要统计答案的时候,之前的l比现在的l大,那么只需统计到当前的r即可。

这样排序之后扫的答案一定是对的,另外用树状数组加速从1-N的前缀和的复杂度。

///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
///             __.'              ~.   .~              `.__
///           .'//                  \./                  \\`.
///        .'//                     |                     \\`.
///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
///     .'//.-"                 `-.  |  .-'                 "-.\\`.
///   .'//______.============-..   \ | /   ..-============.______\\`.
/// .'______________________________\|/______________________________`.
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/STACK:102400000,102400000")
//#pragma GCC target(sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx) 
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define S_1(x) scan_d(x)
#define S_2(x,y) scan_d(x),scan_d(y)
#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair <int, int> ii;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e3+5;
const int maxx=1e6+10;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}

inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

struct node {
	int l,r,id;
}Q[maxx*2];

int a[maxx],ans[maxx];
int n,m;
int cnt;
bool cmp(node a,node b) {
	if(a.l==b.l) {
		if(a.r==b.r) return a.id<b.id;
		return a.r<b.r;
	}
	return a.l>b.l;
}
int lowbit(int x) {
	return x&(-x);
}

void add(int x) {
	W(x<maxx) {
		a[x]++;
		x+=lowbit(x);
	}
}

int query(int x) {
	int ans=0;
	W(x) {
		ans+=a[x];
		x-=lowbit(x);
	}
	return ans;
}

void solve() {
	s_2(n,m);
	FOR(1,n,i) {
		s_2(Q[i].l,Q[i].r);
		Q[i].id=0;
	}
	cnt=n;
	FOR(1,m,i) {
		int num,p1,p2;
		s_2(num,p1);
		if(p1>1) Q[++cnt]=(node){1,p1-1,i};
		p2=p1;
		FOR(2,num,j) {
			s_1(p2);
			if(p1+1<=p2-1) Q[++cnt]=(node){p1+1,p2-1,i};
			p1=p2;
		}
		Q[++cnt]=(node){p2+1,maxx-5,i};
	}
	sort(Q+1,Q+1+cnt,cmp);
	FOR(1,cnt,i) {
		if(Q[i].id) ans[Q[i].id]+=query(Q[i].r);
		else add(Q[i].r);
	}
	FOR(1,m,i)
		print(n-ans[i]);
}
int main() {
    //freopen( "1.in" , "r" , stdin );
    //freopen( "1.out" , "w" , stdout );
    int t=1;
    //init();
    //s_1(t);
    for(int cas=1;cas<=t;cas++) {
        //printf("Case #%d: ",cas);
        solve();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36553623/article/details/82776373
今日推荐