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
 
posted @ 2018-11-12 14:17  wang9897  阅读(87)  评论(0编辑  收藏  举报