#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#define REP(i,a,n)for(int i=a;i<=n;++i)
#define CLR(d,a)memset(d,a,sizeof(d)); using namespace std; void SetIO(string a){
string in=a+".in", out=a+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
} void End(){
fclose(stdin);
fclose(stdout);
} const int maxn=300000+4; int val[maxn]; int n,m; struct Queries{
int l,r,a,b;
Queries(int l=0,int r=0,int a=0,int b=0):l(l),r(r),a(a),b(b){}
}ask[maxn]; void Read(){
scanf("%d%d",&n,&m);
REP(i,1,n)scanf("%d",&val[i]);
REP(i,1,m){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
ask[i]=Queries(a,b,c,d);
}
} int block, belong[maxn], A[maxn]; int get_belong(int i){ return (i-1)/block+1; } bool cmp(int i,int j){
if(belong[ask[i].l]==belong[ask[j].l]) return ask[i].r<ask[j].r;
return belong[ask[i].l]<belong[ask[j].l];
} void Build(){
block=sqrt(n);
REP(i,1,n) belong[i]=get_belong(i);
REP(i,1,m) A[i]=i;
sort(A+1,A+1+m,cmp);
} int count1[maxn]; struct BIT{
int C[2][maxn]; int lowbit(int t){return t&(-t);} void update(int pos,int o,int delta){
while(pos<=n){
C[o][pos]+=delta;
pos+=lowbit(pos);
}
} int query(int pos,int o){
int sum=0;
while(pos>0){
sum+=C[o][pos];
pos-=lowbit(pos);
}
return sum;
}
void Modify(int i,int delta){
if(i==0) return ;
update(i,0,delta);
count1[i]+=delta;
if(delta<0 && count1[i]==0) update(i,1,delta);
if(delta>0 && count1[i]==1) update(i,1,delta);
} }Tree; int ans1[maxn], ans2[maxn]; void Work(){
int l=1,r=1;
Tree.Modify(val[1],1);
REP(i,1,m){
int cur=A[i];
int l2=ask[cur].l;
int r2=ask[cur].r; if(r<r2) {
++r;
while(r<=r2){ Tree.Modify(val[r],1), ++r; }
--r;
}
else
while(r>r2) { Tree.Modify(val[r],-1), --r; } if(l<l2) {
while(l<l2){ Tree.Modify(val[l],-1); ++l; }
}
else {
--l;
while(l>=l2) { Tree.Modify(val[l],1), --l; }
++l;
} ans1[cur]=Tree.query(ask[cur].b,0)-Tree.query(ask[cur].a-1,0);
ans2[cur]=Tree.query(ask[cur].b,1)-Tree.query(ask[cur].a-1,1);
}
} void Print(){
REP(i,1,m)
printf("%d %d\n",ans1[i],ans2[i]);
} int main(){
SetIO("input");
Read();
Build();
Work();
Print();
End();
return 0;
}

[AHOI2013]作业 莫队 树状数组的更多相关文章

  1. 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 ...

  2. COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)

    题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...

  3. BZOJ 3236: [Ahoi2013]作业(莫队+树状数组)

    传送门 解题思路 莫队+树状数组.把求\([a,b]\)搞成前缀和形式,剩下的比较裸吧,用\(cnt\)记一下数字出现次数.时间复杂度\(O(msqrt(n)log(n)\),莫名其妙过了. 代码 # ...

  4. bzoj3236 作业 莫队+树状数组

    莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...

  5. BZOJ 3236 AHOI 2013 作业 莫队+树状数组

    BZOJ 3236 AHOI 2013 作业 内存限制:512 MiB 时间限制:10000 ms 标准输入输出     题目类型:传统 评测方式:文本比较 题目大意: 此时己是凌晨两点,刚刚做了Co ...

  6. BZOJ_3289_Mato的文件管理_莫队+树状数组

    BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...

  7. bzoj 3289: Mato的文件管理 莫队+树状数组

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...

  8. 51nod 1290 Counting Diff Pairs | 莫队 树状数组

    51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...

  9. 【BZOJ3460】Jc的宿舍(树上莫队+树状数组)

    点此看题面 大致题意: 一棵树,每个节点有一个人,他打水需要\(T_i\)的时间,每次询问两点之间所有人去打水的最小等待时间. 伪·强制在线 这题看似强制在线,但实际上,\(pre\ mod\ 2\) ...

随机推荐

  1. 给SearchView设置样式

    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http:/ ...

  2. How to debug systemd step by step

    docker run -ti --name systemd --net host --privileged reg.docker.xxxxxxxx:latest /usr/lib/systemd/sy ...

  3. Vue学习之路第七篇:跑马灯项目实现

    前面六篇讲解了Vue的一些基础知识,正所谓:学以致用,今天我们将用前六篇的基础知识,来实现类似跑马灯的项目. 学前准备: 需要掌握定时器的两个函数:setInterval和clearInterval以 ...

  4. Python-基础-day6

    1.二进制 前言:计算机一共就能做两件事:计算和通信 2.字符编码 生活中的数字要想让计算机理解就必须转换成二进制.十进制到二进制的转换只能解决计算机理解数字的问题,那么文字要怎么让计算机理解呢? 于 ...

  5. Python字典嵌套

    import copy menu = { "计算机科学与技术":{ "程序编程":{ "传统语言":{ "C++":&q ...

  6. django-10-中间件和上下文管理器

    <<<中间件的引入>>> 用户<->中间件<->url->视图  在app目录里面 middleware.py  (1)中间件就是一个 ...

  7. typedef和define混用产生的错误

    最近在写代码过程中,发现一个问题,编译总是过不去,报错如下: stdint.h::: error: duplicate 'unsigned' stdint.h::: error: 'long long ...

  8. js 数组 : 差集、并集、交集、去重

    //input:[{name:'liujinyu'},{name:'noah'}] //output:['liujinyu','noah'] Array.prototype.choose = func ...

  9. SQL SERVER-NULL

    SQL SERVER判断NULL的函数 ISNULL().NVL().IFNULL() 和 COALESCE() 函数 来自为知笔记(Wiz)

  10. Qt之设置应用程序图标

    简述 应用程序图标,通常显示在应用程序的顶层窗口的左上角,通过调用QWindow:setWindowIcon()函数来实现. 为了改变可执行程序文件本身的图标,因为它被呈现在桌面上,它必须采用另一种依 ...