HDU 5213 Lucky 莫队+容斥
Lucky
Problem Description
If WLD can answer all the questions correctly,he'll be the luckiest man in the world.Can you help him?
For each case:
The first line contains an integer N(1≤N≤30000).
The following line contains an integer K(2≤K≤2∗N),WLD's lucky number.K is odd.
The following line contains N integers a1,a2,...,aN(1≤ai≤N).
The following line contains an integer M(1≤M≤30000),the sum of the questions WLD has to answer.
The following M lines,the i-th line contains 4 numbers Li,Ri,Ui,Vi(1≤Li≤Ri<Ui≤Vi≤N),describing the i-th question the stranger asks.
Print the total of pairs WLD can choose for each question.
3
1 2 1 2 3
1
1 2 3 5
a1+a4=a2+a3=3=K.
So we have two pairs of numbers (1,4) and (2,3).
Good luck!
题意 :
给你你n个数一个k
m次询问,每次给你两区间
问你这两个区间 任选两个数a[i] + a[j] = k 的对数
题解:
这道题需要一些莫队算法的知识 定义记号f(A,B)f(A,B)表示询问区间A,B时的答案 用记号+表示集合的并 利用莫队算法我们可以计算出任意f(A,A)f(A,A)的值
不妨假设A=[l1,r1],B=[l2,r2],C=[r1+1,l2-1]A=[l1,r1],B=[l2,r2],C=[r1+1,l2−1]
容易知道f(A,B)=f(A+B+C,A+B+C)+f(C,C)-f(A+C,A+C)-f(C+B,C+B)f(A,B)=f(A+B+C,A+B+C)+f(C,C)−f(A+C,A+C)−f(C+B,C+B)
因此一个询问被拆成四个可以用莫队算法做的询问 总的时间复杂度为O(msqrt(n))O(msqrt(n))
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 3e4+, M = 6e4+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int T,n,a[N],ans[N],belong[N],mp[M * ],k;
struct ss{
int l,r,id,res;
ss () {}
ss (int l,int r,int id,int res) : l(l), r(r), id(id), res(res) {}
}Q[N * ];
bool operator < (ss s1 , ss s2) {
if(belong[s1.l] == belong[s2.l]) return s1.r<s2.r;
else return belong[s1.l] < belong[s2.l];
} int main()
{
while(~scanf("%d",&n)) {
memset(mp,,sizeof(mp));
memset(ans,,sizeof(ans));
scanf("%d",&k);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
int q,cnt=;cin>>q;
for(int i = ; i <= q; ++i) {
int l1,r1,l2,r2;
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
Q[++cnt] = ss (l1,r2,i,);
if(l2->=r1+)Q[++cnt] = ss (r1+,l2-,i,);
Q[++cnt] = ss(r1+,r2,i,-);
Q[++cnt] = ss(l1,l2-,i,-);
}
int t = sqrt(n);
for(int i = ; i <= n; ++i) belong[i] = (i-) / t + ;
sort(Q+,Q+cnt+);
int l = , r = , ret = ;
for(int i = ; i <= cnt; ++i) {
for(;r<Q[i].r;r++) {
ret += mp[k-a[r+]+M];
mp[a[r+]+M]++;
}
for(;l>Q[i].l;l--) {
ret += mp[k-a[l-]+M];
mp[a[l-]+M]++;
}
for(;r>Q[i].r;r--) {
mp[a[r]+M]--;
ret -= mp[k-a[r]+M]; }
for(;l<Q[i].l;l++) {
mp[a[l]+M]--;
ret -= mp[k-a[l]+M]; }
// cout<<Q[i].l<<" "<<Q[i].r<<" ";
//cout<<Q[i].res*ret<<endl;
ans[Q[i].id] += Q[i].res*ret;
}
for(int i = ; i <= q; ++i) {
printf("%d\n",ans[i]);
}
}
}
HDU 5213 Lucky 莫队+容斥的更多相关文章
- Lucky HDU - 5213 (莫队,容斥)
WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a strang ...
- HDU 5145 分块 莫队
给定n个数,q个询问[l,r]区间,每次询问该区间的全排列多少种. 数值都是30000规模 首先考虑计算全排列,由于有同种元素存在,相当于每次在len=r-l+1长度的空格随意放入某种元素即$\bin ...
- HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
- HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法
题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd( ...
- hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- hdu 4336 Card Collector —— Min-Max 容斥
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...
- hdu 4638 Group 莫队算法
题目链接 很裸的莫队, 就不多说了... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) # ...
- HDU 6397 Character Encoding (组合数学 + 容斥)
题意: 析:首先很容易可以看出来使用FFT是能够做的,但是时间上一定会TLE的,可以使用公式化简,最后能够化简到最简单的模式. 其实考虑使用组合数学,如果这个 xi 没有限制,那么就是求 x1 + x ...
随机推荐
- 【leetcode】Reverse Nodes in k-Group
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- mongoengine
http://docs.mongodb.org/ecosystem/drivers/python/ http://www.cnblogs.com/holbrook/archive/2012/03/11 ...
- poj1155
题意:给定一个树形图,节点数量3000.叶子节点是用户,每个用户如果能看上电视会交一定的电视费.看上电视的条件是从根到该用户的路径全部被修好,修每条边有一个费用.在不亏损(用户交钱总额>=修路总 ...
- 手动安装python后,交互模式下退格键乱码
没有安装readline相关的模块 yum install readline readline-devel 再重新变异安装python就可以了
- Kafka集群环境搭建
Kafka是一个分布式.可分区.可复制的消息系统.Kafka将消息以topic为单位进行归纳:Kafka发布消息的程序称为producer,也叫生产者:Kafka预订topics并消费消息的程序称为c ...
- 24. javacript高级程序设计-最佳实践
1. 最佳实践 l 来自其他语言的代码约定可以用于决定何时进行注释,以及如何进行缩进,不过JavaScript需要针对其松散类型的性质创造一些特殊的约定 l javascript应该定义行为,html ...
- 用基础动画实现iOS控件循环旋转
- (void)viewDidLoad { [super viewDidLoad]; UIButton* ag=[[UIButton alloc]initWithFrame:CGRectMake(sc ...
- codeforces 492C. Vanya and Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/492/C 题目意思:给出 3 个整数:n, r, avg.然后有 n 行,每行有两个数:第 i 行有 ...
- tomcat浏览器地址支持中文方法
- 基于Spring的可扩展Schema进行开发自定义配置标签支持
一.背景 最近和朋友一起想开发一个类似alibaba dubbo的功能的工具,其中就用到了基于Spring的可扩展Schema进行开发自定义配置标签支持,通过上网查资料自己写了一个demo.今天在这里 ...