WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a stranger with numbers:.The stranger asks him questions.Each question is like this:Given two ranges and ,you can choose two numbers and to make .The you can choose is between and and the you can choose is between and .How many pairs of numbers 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 )

For each case:

The first line contains an integer .

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

The following line contains integers .

The following line contains an integer ,the sum of the questions WLD has to answer.

The following lines,the i-th line contains numbers ,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

Sample Output

2

Hint

a1+a4=a2+a3=3=K.

So we have two pairs of numbers (1,4) and (2,3).

Good luck!

题意:



给你一个长度为n的数组a,以及一个整数k,和m个询问。

每一个询问给你两个区间分别是 lr,uv,

问你有多少对 x,y 满足 a[x]+a[y]=k 并且 l<=x<=r, u<=y<=v

思路:

由于题目的数据范围和支持离线做法,我们容易想到莫队算法

但是我们知道莫队算法只可以处理一个连续的区间问题,不能处理两段连续的区间。

那么我们来考虑能否将两段区间转化为一个连续区间的问题。

我们设F(i,j ) 为 在区间i~j中,x,y (x!=y)满足 a[x]+a[y]=k 的个数。

那么根据容斥定律,我们可以知道。、

F(l1,r1)+F(l2,r2) = F(l1,r2) - F(l1,l2-1 ) - F(r1+1,r2)+F(r1+1,l2-1)

那么我们就可以把问题转化为莫队经典的区间询问问题了。

我们把每一个询问分成上面等式右边的四个部分,同时对于每一个部分维护它对这个询问的贡献是加还是减。

多组输入,记得初始化即可。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll ans[maxn];
ll Ans = 0ll;
int l = 1;
int r = 0;
struct node {
int l, r, id;
int op;
} a[maxn];
int pos[maxn];
int n, m;
int len;
bool cmp(node aa, node bb)
{
if (pos[aa.l] == pos[bb.l]) {
return aa.r < bb.r;
} else {
return pos[aa.l] < pos[bb.l];
}
}
int col[maxn];
int flag[maxn];
int k;
int cnt = 0;
void add(int x)
{
if ((k >= col[x])) {
Ans += flag[k - col[x]];
}
flag[col[x]]++;
} void del(int x)
{
flag[col[x]]--;
if((k>=col[x]))
Ans-=flag[k-col[x]];
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
while(~scanf("%d%d",&n,&k))
{ len = (int)(sqrt(n));
repd(i, 1, n) {
gg(col[i]);
}
gg(m);
repd(i,1,max(n,m))
{
ans[i]=0ll;
flag[i]=0ll;
}
cnt=0;
int l1, l2, r1, r2;
repd(i, 1, m) {
pos[i] = i / len;
gg(l1); gg(r1); gg(l2); gg(r2);
a[++cnt].l = l1; a[cnt].r = r2; a[cnt].op = 1;a[cnt].id = i;
a[++cnt].l = l1; a[cnt].r = l2-1; a[cnt].op = -1;a[cnt].id = i;
a[++cnt].l = r1+1; a[cnt].r = r2; a[cnt].op = -1;a[cnt].id = i;
a[++cnt].l = r1+1; a[cnt].r = l2-1; a[cnt].op = 1;a[cnt].id = i;
}
sort(a + 1, a + 1 + cnt, cmp);
repd(i, 1, cnt) {
while (l > a[i].l) {
l--;
add(l);
}
while (r < a[i].r) {
r++;
add(r);
}
while (l < a[i].l) {
del(l);
l++;
}
while (r > a[i].r) {
del(r);
r--;
}
// cout<<a[i].l<<" "<<a[i].r<<" "<<Ans<<" "<<a[i].op<<endl;
ans[a[i].id] += Ans * a[i].op;
}
repd(i, 1, m) {
printf("%lld\n", ans[i]);
}
} return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Lucky HDU - 5213 (莫队,容斥)的更多相关文章

  1. HDU 5213 Lucky 莫队+容斥

    Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...

  2. Hdu 5213-Lucky 莫队,容斥原理,分块

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5213 Lucky Time Limit: 6000/3000 MS (Java/Others)    Me ...

  3. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

  4. HDU 5297 Y sequence 容斥 迭代

    Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...

  5. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  6. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

  7. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  8. HDU 4609 3-idiots FFT+容斥

    一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完 ...

  9. HDU 4336 Card Collector(容斥)

    题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...

随机推荐

  1. Delphi动态创建菜单

    在程序运行中动态创建菜单,主要使用TMeunItem类,所有菜单的条目都是TMenuItem的一个实例. 打开Delphi7集成开发环境,在默认新建工程里,放置一个Button1按钮和MainMenu ...

  2. 【知乎】github,Empty reply from server?

    http://www.zhihu.com/question/26717343   问:用hexo搭了个blog,之前一直正常,最近在deploy的时候无法连接到github了. fatal: unab ...

  3. 调用redis封装好的JedisUtils接口实现锁库

    1.在进行数据库操作的方法前先定义一个key值,并添加一个能区别每个key的标识 2.首先判断如果定义的key值存在的话,就直接return方法,如果不存在的话,就把key值放在jedisutil中, ...

  4. login 模块,re 模块

    标准三流 标准输入流:sys. stdin # input的底层 标准输出流:sys. stdout     # print的底层 标准错误流:sys. stderr      # 异常及loggin ...

  5. pandas 分组统计

    # coding:utf-8 import pandas as pd import numpy as np # path = r'C:\Users\wuzaipei\Desktop\桂林三金项目签到情 ...

  6. 【C++ 学习笔记】 MFC CEdit

    环境:VS2008 -关联变量 方法一:在控件上右键-变量-CEdit类型变量-输入变量名. 方法二:添加代码 头文件中定义变量CEdit m_edit_fileName; 源文件中变量关联DDX_C ...

  7. inner join on会过滤掉两边空值的条件

    前两天工作过程中,遇到一个问题,关于join on查询的,对于查出来的结果一直都很疑惑,这里记录一下. 1.首先看下面这条sql查询语句: 查询出来的结果是25053 2.加个 o.lat = n.l ...

  8. Oracle表概念

    对于初学者来说,对表的概念也有一定的认识.因为我们对数据库的操作,90%以上是对表的操作. 常见表的规则表(Regular table),严格意义上来说又叫 heap table(堆表),也就是我们最 ...

  9. 什么是云数据库RDS PostgreSQL 版

    PostgreSQL被业界誉为“最先进的开源数据库”,面向企业复杂SQL处理的OLTP在线事务处理场景,支持NoSQL数据类型(JSON/XML/hstore).支持GIS地理信息处理. 优点 NoS ...

  10. ubuntu 18.04 配置notebook远程连接的坑

    jupyter-notebook的安装不在此说明 在网上搜了很多方案都不行,好不容易从坑里爬出来 以下为远程连接配置方法 1.生成配置文件 jupyter notebook --generate-co ...