水果~~~~

关于四个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. thinkphp5数据库导入Excel表格

    $data=$order_info; //$data 你要下载谁 就去查谁 // $data= Db::name('order_info') // ->field('consignee,tel, ...

  2. hive自定义函数(UDF)

    首先什么是UDF,UDF的全称为user-defined function,用户定义函数,为什么有它的存在呢?有的时候 你要写的查询无法轻松地使用Hive提供的内置函数来表示,通过写UDF,Hive就 ...

  3. MongoDB在单机上搭建分片副本集群(windows)

    ------------------------------1.安装MongoDB...... ------------------------------2.准备好文件夹 --config:配置文件 ...

  4. Hive初识(四)

    Hive本质上是一个数据仓库,但不存储数据(只存储元数据(metadata),Hive中的元数据包括表的名字,表的列和分区及分区及其属性,表的属性(是否为外部表等),表的数据所在目录等),用户可以借助 ...

  5. Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)

    题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...

  6. Jersey2+swagger组建restful风格api及文档管理

    1.jar包引入 <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId& ...

  7. Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org/apache/hadoop/hbase/

    Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org/apache/hadoop/hbase/ ...

  8. Eclipse 导入项目与 svn 插件关联全过程记录

    文章摘自:http://www.cnblogs.com/xmmcn/archive/2013/03/01/2938365.html 感谢博友分享! Eclipse 导入项目与 svn 插件关联全过程记 ...

  9. python基础——元组、文件及其它

    Python核心数据类型--元组 元组对象(tuple)是序列,它具有不可改变性,和字符串类似.从语法上讲,它们便在圆括号中,它们支持任意类型.任意嵌套及常见的序列操作. 任意对象的有序集合:与字符串 ...

  10. Python 3基础教程21-列表和元组

    本文介绍列表也元组,先来看看他们的定义. # 元组和列表 # 元组的定义 x = 5,6,2,6 # 或者这样写 x = (5,6,2,6) # 列表定义 y = [5,6,2,6] # 元组的使用, ...