水果~~~~

关于四个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. JQ 封装全选函数

    单击时触发效果: 如果有一个没有选中把全选的勾去了,如果select所有的都选中了,那就把全选勾上 html里: <div class="row cl"> <la ...

  2. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  3. C语言结构体的学习,以及gdb的调式

    #include <stdio.h> #include <string.h> #define format "%d\n%s\n%f\n%f\n%f\n" t ...

  4. shell -- shift用法

    shift是Unix中非常有用的命令.可以使命令参数左移,从而使脚本程序中命令参数位置不变的情况下依次遍历所有参数.如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1. ...

  5. Objective-C反射机制

    oc反射机制有三个用途: 1.获得Class Class LoginViewController = NSClassFromString(@"LoginViewController" ...

  6. 安装一个apk文件源代码

     /**   * 安装一个apk文件   *   * @param file   * 要安装的完整文件名   */  protected void installApk(File file) {   ...

  7. Qt_C++交换两个数

    在编程过程中,交换两个数是我用常用的 ,这里做下简单的搬运 bool Widget::swap(int a, int b) { int temp =a; a= b; b = temp; } 这种方式其 ...

  8. spring-boot分页插件

    1.分页插件,spring-boot.,第一次调用时,存值到 model.addAttribute("status", id);页面获取2.页面获取 后台存入的值,放在input ...

  9. 05-Mysql数据库----补充内容

    数据库命名规则: 数据库命名规则: 可以由字母.数字.下划线.@.#.$ 区分大小写 唯一性 不能使用关键字如 create select 不能单独使用数字 最长128位 # 基本上跟python或者 ...

  10. 51单片机实现外部中断0-F

    #include< reg51.h> #define uint unsigned int #define uchar unsigned char sfr P0M0 = 0x94; sfr ...