来自FallDream的博客,未经允许,请勿转载,谢谢。


给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出
 
get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次。
n,q<=50000
Ans(l1,r1,l2,r2)=Ans(1,r1,1,r2)-Ans(1,l1-1,1,r2)-Ans(1,r1,1,l2-1)+Ans(1,l1-1,1,l2-1)
然后就可以莫队了
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#define MN 50000
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct ques{int l,r,id,ad;}q[MN*+];
int n,block[MN+],size,a[MN+],num1[MN+],num2[MN+],m,tot=,L=,R=;
long long res=,Ans[MN+];
bool cmp(const ques&a,const ques&b){return block[a.l]==block[b.l]?a.r<b.r:a.l<b.l;}
int main()
{
n=read();size=sqrt(n);
for(int i=;i<=n;++i) block[i]=(i-)/size+;
for(int i=;i<=n;++i) a[i]=read();
m=read();
for(int i=;i<=m;++i)
{
int l1=read(),r1=read(),l2=read(),r2=read();
q[++tot]=(ques){r1,r2,i,};
q[++tot]=(ques){r1,l2-,i,-};
q[++tot]=(ques){l1-,r2,i,-};
q[++tot]=(ques){l1-,l2-,i,};
}
sort(q+,q+tot+,cmp);
for(int i=;i<=tot;++i)
{
while(L<q[i].l) ++L,res+=num2[a[L]],++num1[a[L]];
while(L>q[i].l) res-=num2[a[L]],--num1[a[L]],--L;
while(R<q[i].r) ++R,res+=num1[a[R]],++num2[a[R]];
while(R>q[i].r) res-=num1[a[R]],--num2[a[R]],--R;
Ans[q[i].id]+=q[i].ad*res;
}
for(int i=;i<=m;++i) printf("%lld\n",Ans[i]);
return ;
}

[bzoj5016][Snoi2017]一个简单的询问的更多相关文章

  1. Gym101138D Strange Queries/BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥

    传送门--Gym 传送门--BZOJ THUWC2019D1T1撞题可还行 以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及-- 设\(que_{i,j}\)表示询问\((1,i,1,j ...

  2. BZOJ5016:[SNOI2017]一个简单的询问(莫队)

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...

  3. BZOJ5016 Snoi2017一个简单的询问(莫队)

    容易想到区间转化成前缀和.这样每个询问有了二维坐标,莫队即可. #include<iostream> #include<cstdio> #include<cmath> ...

  4. 【BZOJ5016】[Snoi2017]一个简单的询问 莫队

    [BZOJ5016][Snoi2017]一个简单的询问 Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计 ...

  5. [SNOI2017]一个简单的询问

    [SNOI2017]一个简单的询问 题目大意: 给定一个长度为\(n(n\le50000)\)的序列\(A(1\le A_i\le n)\),定义\(\operatorname{get}(l,r,x) ...

  6. bzoj P5016[Snoi2017]一个简单的询问——solution

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出   get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input ...

  7. 【bzoj5016】[Snoi2017]一个简单的询问 莫队算法

    题目描述 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. 输入 第一行,一个数字N,表 ...

  8. bzoj5016 & loj2254 [Snoi2017]一个简单的询问 莫队

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5016 https://loj.ac/problem/2254 题解 原式是这样的 \[ \su ...

  9. bzoj 5016: [Snoi2017]一个简单的询问

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...

随机推荐

  1. 团队作业4——第一次项目冲刺(Alpha版本)11.18

    a. 提供当天站立式会议照片一张 举行站立式会议,讨论项目安排: 整理各自的任务汇报: 全分享遇到的困难一起讨论: 讨论接下来的计划: b. 每个人的工作 (有work item 的ID) 1.前两天 ...

  2. JavaSE阶段初期的一些问题

    ​​​对于如下问题1:编译阶段Demo1会报错,Demo2不会报错. class Demo1{ int i; i = 0; } class Demo2{ int i = 0; } 事实上,在java中 ...

  3. sql 几种循环方式

    1:游标方式 ALTER PROCEDURE [dbo].[testpro] as ) --日期拼接 ) --仪表编号 ) --数据采集表 ) --数据采集备份表 ) ) begin set @yea ...

  4. 作业五:RE 模块模拟计算器

    # !/usr/bin/env python3 # _*_coding:utf-8_*_ ''' 实现模拟计算器的功能: 公式: - * ( (- +(-/) * (-*/ + /*/* + * / ...

  5. 新概念英语(1-17)How do you do ?

    Is there a problem wtih the Customers officer? What are Michael Baker and Jeremy Short's jobs? A:Com ...

  6. Spring Security 入门(1-3-3)Spring Security - logout 退出登录

    要实现退出登录的功能我们需要在 http 元素下定义 logout 元素,这样 Spring Security 将自动为我们添加用于处理退出登录的过滤器 LogoutFilter 到 FilterCh ...

  7. SpringCloud的注解:EnableEurekaClient vs EnableDiscoveryClient

    What's the difference between EnableEurekaClient and EnableDiscoveryClient? In some applications, I ...

  8. jprofiler配置

    cataline.sh JAVA_OPTS="$JAVA_OPTS -agentlib:jprofilerti=port=8849"JAVA_OPTS="$JAVA_OP ...

  9. leetcode算法:Trim a Binar Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  10. Struts(十八):通过CURD来学习PrepareInterceptor拦截器

    PrepareInterceptor拦截器的用法: 1.若Action实现了Preparable接口,则Action方法需实现prepare()方法: 2.PrepareInterceptor拦截器S ...