BZOJ 3236 莫队+树状数组
思路:
莫队+树状数组
(据说此题卡常数)
yzy写了一天(偷笑)
复杂度有点儿爆炸 O(msqrt(n)logn)
//By SiriusRen
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1005000
int n,m,a[N],pos[N],w[N],W[N],tmp,ww[N];
struct Node{int l,r,a,b,ansx,ansy,id;}node[N];
bool cmp(Node a,Node b){if(pos[a.l]==pos[b.l])return a.r<b.r;return a.l<b.l;}
bool cmp2(Node a,Node b){return a.id<b.id;}
int lowbit(int x){return x&(-x);}
int sum(int x){int ans=0;for(int i=x;i;i-=lowbit(i))ans+=w[i];return ans;}
void add(int x,int z){for(int i=x;i<=n;i+=lowbit(i))w[i]+=z;}
int sum2(int x){int ans=0;for(int i=x;i;i-=lowbit(i))ans+=ww[i];return ans;}
void add2(int x,int z){for(int i=x;i<=n;i+=lowbit(i))ww[i]+=z;}
void update(int x,int wei){
if(!W[x])add2(x,1);
add(x,wei);
W[x]+=wei;
if(!W[x])add2(x,-1);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;node[i].id=i,i++)scanf("%d%d%d%d",&node[i].l,&node[i].r,&node[i].a,&node[i].b);
int block=sqrt(n);
for(int i=1;i<=n;i++)pos[i]=(i-1)/block+1;
sort(node+1,node+1+m,cmp);
for(int i=1,l=1,r=0;i<=m;i++){
for(;r<node[i].r;r++)update(a[r+1],1);
for(;r>node[i].r;r--)update(a[r],-1);
for(;l<node[i].l;l++)update(a[l],-1);
for(;l>node[i].l;l--)update(a[l-1],1);
node[i].ansx=sum(node[i].b)-sum(node[i].a-1);
node[i].ansy=sum2(node[i].b)-sum2(node[i].a-1);
}
sort(node+1,node+1+m,cmp2);
for(int i=1;i<=m;i++)
printf("%d %d\n",node[i].ansx,node[i].ansy);
}
BZOJ 3236 莫队+树状数组的更多相关文章
- BZOJ 3236: [Ahoi2013]作业(莫队+树状数组)
传送门 解题思路 莫队+树状数组.把求\([a,b]\)搞成前缀和形式,剩下的比较裸吧,用\(cnt\)记一下数字出现次数.时间复杂度\(O(msqrt(n)log(n)\),莫名其妙过了. 代码 # ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
- bzoj3236 作业 莫队+树状数组
莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- BZOJ_3289_Mato的文件管理_莫队+树状数组
BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...
- BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
- 【BZOJ3460】Jc的宿舍(树上莫队+树状数组)
点此看题面 大致题意: 一棵树,每个节点有一个人,他打水需要\(T_i\)的时间,每次询问两点之间所有人去打水的最小等待时间. 伪·强制在线 这题看似强制在线,但实际上,\(pre\ mod\ 2\) ...
- HihoCoder 1488 : 排队接水(莫队+树状数组)
描述 有n个小朋友需要接水,其中第i个小朋友接水需要ai分钟. 由于水龙头有限,小Hi需要知道如果为第l个到第r个小朋友分配一个水龙头,如何安排他们的接水顺序才能使得他们等待加接水的时间总和最小. 小 ...
随机推荐
- 【Computer Vision】图像单应性变换/投影/仿射/透视
一.基础概念 1. projective transformation = homography = collineation. 2. 齐次坐标:使用N+1维坐标来表示N维坐标,例如在2D笛卡尔坐标 ...
- Spring Boot基础教程》 第1节工具的安装和使用
<Spring Boot基础教程> 第1节 工具的安装和使用 Spring Boot文档 https://qbgbook.gitbooks.io/spring-boot-reference ...
- 队列(Queue)-c实现
相对而言,队列是比较简单的. 代码还有些warning,我改不动,要找gz帮忙. #include <stdio.h> typedef struct node { int data; st ...
- Rancher介绍安装以及对docker的管理
原文:Rancher介绍安装以及对docker的管理 一.简介 Rancher是一个开源的企业级全栈化容器部署及管理平台.Rancher为容器提供一揽子基础架构服务:CNI兼容的网络服务.存储服务.主 ...
- 《Effective Modern C++》翻译--条款4:了解怎样查看推导出的类型
条款4:了解怎样查看推导出的类型 那些想要了解编译器怎样推导出的类型的人通常分为两个阵营. 第一种阵营是实用主义者.他们的动力通常来自于编敲代码过程中(比如他们还在调试解决中),他们利用编译器进行寻找 ...
- mysql-组合查询
一.组合查询 mysql允许执行多个查询(多条select语句),并将结果作为单个查询结果集返回.这些组合查询通常称为并(union)或复合查询(compound query). 有两种情况需要使用组 ...
- nodejs02---demo
1.Hello World 打一个一个文本编辑器,在其中输入 console.log('Hello World'); 并保存为helloworld.js.打开dos窗口进入该文件的目录运行 node ...
- JS实现文字图片无缝滚动
今天做项目遇到一个滚动的效果,本来打算用marquee做的,因为它是html自带的标签,写起来简单,但是有一个问题就是marquee不能实现无缝滚动,上网找了一些方法,发现marquee可以实现无缝, ...
- Format operator
The argument of write has to be a string, so if we want to put other values in a file, we have to co ...
- Most common words
To find the most common words, we can apply the DSU pattern; most_common takes a histogram and retur ...