水果~~~~

关于四个while可行性的证明:区间有正确性所以不管那团小东西用没有duang~反它最终总会由于两次覆盖二准确

关于区间种数可行性的证明:他会在0 1间(或两边)来回跳动(过程中),最终会停在一个大于等于0的地方由于多次覆盖,最终也会趋于准确

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define M 1000005
#define N 100005
using namespace std;
int zero[N],a[N],b[N],pos[N],len,l,r,had[N],n,m;
struct Q
{
int l,r,ans1,ans2,z,y,id;
}q[M];
int comp(const Q x,const Q y)
{
return pos[x.l]<pos[y.l]||(pos[x.l]==pos[y.l]&&x.r<y.r);
}
int end_comp(const Q x,const Q y)
{
return x.id<y.id;
}
inline void update_a(int x,int i)
{
while(x<=n)
{
a[x]+=i;
x+=x&(-x);
}
}
inline void update_b(int x,int i)
{
while(x<=n)
{
b[x]+=i;
x+=x&(-x);
}
}
inline int sum_a(int x)
{
int ret=;
while(x>)
{
ret+=a[x];
x-=x&(-x);
}
return ret;
}
inline int sum_b(int x)
{
int ret=;
while(x>)
{
ret+=b[x];
x-=x&(-x);
}
return ret;
}
void pre()
{
scanf("%d%d",&n,&m);
len=(int)(sqrt(n+0.5));
for(int i=;i<=n;i++)
{
scanf("%d",&zero[i]);
pos[i]=(i-)/len+;
}
l=r=;
update_a(zero[],);
update_b(zero[],);
had[zero[]]=;
for(int i=;i<=m;i++)
{
scanf("%d%d%d%d",&q[i].l,&q[i].r,&q[i].z,&q[i].y);
q[i].id=i;
}
sort(q+,q+m+,comp);
}
inline void via(int pl,int i)
{
if(i==)
{
update_a(zero[pl],);
had[zero[pl]]++;
if(had[zero[pl]]==)
update_b(zero[pl],);
}
else
{
update_a(zero[pl],-);
had[zero[pl]]--;
if(had[zero[pl]]==)
update_b(zero[pl],-);
}
}
void work()
{
for(int i=;i<=m;i++)
{
while(l<q[i].l)via(l++,-);
while(l>q[i].l)via(--l,);
while(r<q[i].r)via(++r,);
while(r>q[i].r)via(r--,-);
q[i].ans1=sum_a(q[i].y)-sum_a(q[i].z-);
q[i].ans2=sum_b(q[i].y)-sum_b(q[i].z-);
}
}
void print()
{
sort(q+,q+m+,end_comp);
for(int i=;i<=m;i++)
printf("%d %d\n",q[i].ans1,q[i].ans2);
}
int main()
{
//freopen("ahoi2013_homework.in","r",stdin);
//freopen("ahoi2013_homework.out","w",stdout);
pre();
work();
print();
return ;
}

BZOJ3236: [Ahoi2013]作业 树状数组维护 莫队的更多相关文章

  1. BZOJ1878 [SDOI2009]HH的项链 树状数组 或 莫队

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1878 题意概括 给出一个长度为n的序列,用m次询问,问区间Li~Ri中有多少种不同的数. 0< ...

  2. luogu P1972 [SDOI2009]HH的项链 |树状数组 或 莫队

    题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链变得越来越长. ...

  3. [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)

    树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...

  4. HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...

  5. POJ 3321 Apple Tree(后根遍历将树转化成序列,用树状数组维护)

    题意:一棵树,有很多分叉,每个分叉上最多有1个苹果. 给出n,接下来n-1行,每行u,v,表示分叉u,v之间有树枝相连.这里数据中u相当于树中的父节点,v相当于子节点. 给出两个操作: 1.C x  ...

  6. 第十二届湖南省赛G - Parenthesis (树状数组维护)

    Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...

  7. LOJ107. 维护全序集【树状数组维护全序集】

    题目描述 这是一道模板题,其数据比「普通平衡树」更强. 如未特别说明,以下所有数据均为整数. 维护一个多重集 S ,初始为空,有以下几种操作: 把 x 加入 S 删除 S 中的一个 x,保证删除的 x ...

  8. 【BZOJ2124】等差子序列 树状数组维护hash值

    [BZOJ2124]等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】

    任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...

随机推荐

  1. Yaf学习(二)----Yaf初体验

    1.hello world 1.1 用yaf输出hello world 1.首先配置host,nginx 2.host不用多说,指向虚拟机IP即可 1.2 重点说一下nginx (只说server块) ...

  2. Mysql基础1-基础语法-字段类型

    主要: 基础 字段类型 基础 基本概念 1) 数据库分类 层次数据库,网状数据库,关系数据库 常见:SQL Server, Oracle,infomix,sybase,ibmDB2,Mysql 2)数 ...

  3. Drupal 网站漏洞修复以及网站安全防护加固方法

    drupal是目前网站系统使用较多一个开源PHP管理系统,架构使用的是php环境+mysql数据库的环境配置,drupal的代码开发较为严谨,安全性较高,但是再安全的网站系统,也会出现网站漏洞,dru ...

  4. Mult

    public class MULT { public static void main(String args[]) { int k; for(int x=1;x<=9;x++) { for(i ...

  5. java基础day05---界面

    java基础day05---界面 1.GUI:图形用户界面(Graphics User Interface) 开发工具包AWT抽象窗口把工具箱===>swing 解决了awt存在的lcd问题== ...

  6. Python3爬虫(八) 数据存储之TXT、JSON、CSV

    Infi-chu: http://www.cnblogs.com/Infi-chu/ TXT文本存储 TXT文本存储,方便,简单,几乎适用于任何平台.但是不利于检索. 1.举例: 使用requests ...

  7. 用ssh进行git clone出现 fatal: Could not read from remote repository.

    问题:在通过MobaXterm进行ssh连接的服务器上用ssh进行git clone出现 fatal: Could not read from remote repository. 解决方法:prox ...

  8. 在Android studio中用gradle打 jar 包(Mac下)

    这两天公司要重构项目,以前的项目在eclipse上,准备迁移到Android studio上,需要对项目打包,于是我学习了Android studio中gradle打包的内容.我在公司用的Mac,在家 ...

  9. fsync体会

    看这个链接:http://www.postgresql.org/docs/9.1/static/runtime-config-wal.html 是这样说的: fsync (boolean) If th ...

  10. VI的配置

    vi下设置tab键为4个空格 在每个用户的主目录下,都有一个 vi 的配置文件".vimrc"或".exrc",没有的可以新建一个.用户可以编辑它,使这些设置在 ...