HDU5213 Lucky

本文探讨了在处理区间查询问题时,如何利用莫队算法进行高效求解,并结合容斥原理解决复杂查询,例如在两个区间内选择数值使它们的和等于特定值的问题。文章提供了详细的算法实现思路及代码示例。

题解:  若对于查询区间只是单独的区间我们考虑直接莫队就行 若查询的是两个区间各选一个值的方案数 我们可以采用容斥的做法 用Calc(l,rx)-Calc(l,lx-1)-Calc(r+1,rx)+Calc(r+1,lx-1)的值即可 即把一个区间拆分成四个区间容斥即可

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int MAXN=3e5+10;
const double eps=1e-8;
#define ll long long
using namespace std;
struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;}
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int a[MAXN];
int p[MAXN],sz;
typedef struct node{
	int l,r;int id;
	friend bool operator<(node aa,node bb){
		if(p[aa.l]==p[bb.l])return aa.r<bb.r;
		else return p[aa.l]<p[bb.l];
	}
}node;
node que[MAXN];
int vis[MAXN],k,n;
ll ans[MAXN],ans1;
void Delete(int t){
	vis[t]--;
	if(k-t<=0&&k-t>n)return ;
	ans1-=vis[k-t];
}
void Add(int t){
	vis[t]++;
	if(k-t<=0&&k-t>n)return ;
	ans1+=vis[k-t];
}
int main(){
	while(scanf("%d",&n)!=EOF){
		memset(vis,0,sizeof(vis));memset(ans,0,sizeof(ans));
		k=read();sz=(int)sqrt(n);
		inc(i,1,n)p[i]=(i-1)/sz+1;
		inc(i,1,n)a[i]=read();
		int q=read();
		int l,r,lx,rx,cnt;
		cnt=0;ans1=0;
		inc(i,1,q){
			l=read();r=read();lx=read();rx=read();
			que[++cnt].l=l;que[cnt].r=rx;que[cnt].id=i;
			que[++cnt].l=l;que[cnt].r=lx-1;que[cnt].id=-i;
			que[++cnt].l=r+1;que[cnt].r=rx;que[cnt].id=-i;
			if(r<lx-1){que[++cnt].l=r+1;que[cnt].r=lx-1;que[cnt].id=i;}
		}
		sort(que+1,que+cnt+1);
		int L=1;int R=0;
		for(int i=1;i<=cnt;i++){
			while(R>que[i].r){
				Delete(a[R]);
				R--;
			}
			while(R<que[i].r){
				R++;
				Add(a[R]);
			}
			while(L<que[i].l){
				Delete(a[L]);L++;
			}
			while(L>que[i].l){
				L--;
				Add(a[L]);
			}
			if(que[i].id<0)ans[-que[i].id]-=ans1;
			else ans[que[i].id]+=ans1;
		}
		inc(i,1,q)printf("%lld\n",ans[i]);
	}
	return 0;
}

 

Lucky

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1753    Accepted Submission(s): 616


Problem Description
WLD is always very lucky.His secret is a lucky number  K.k is a fixed odd number. Now he meets a stranger with N numbers:a1,a2,...,aN.The stranger asks him Mquestions.Each question is like this:Given two ranges [Li,Ri] and [Ui,Vi],you can choose two numbers X and Y to make aX+aY=K.The X you can choose is between Li and Ri and the Y you can choose is between Ui and Vi.How many pairs of numbers(X,Y) you can choose?
If WLD can answer all the questions correctly,he'll be the luckiest man in the world.Can you help him?
 

 

Input
There are multiple cases.(At MOST  5)

For each case:

The first line contains an integer N(1N30000).

The following line contains an integer K(2K2N),WLD's lucky number.K is odd.

The following line contains N integers a1,a2,...,aN(1aiN).

The following line contains an integer M(1M30000),the sum of the questions WLD has to answer.

The following M lines,the i-th line contains 4 numbers Li,Ri,Ui,Vi(1LiRi<UiViN),describing the i-th question the stranger asks.
 

 

Output
For each case:

Print the total of pairs WLD can choose for each question.
 

 

Sample Input
5 3 1 2 1 2 3 1 1 2 3 5
 

转载于:https://www.cnblogs.com/wang9897/p/9946185.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值