Bryce1010模板

1、一维树状数组

https://vjudge.net/contest/239647#problem/A【HDU1556】

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=1e5+10;
int n,c[MAXN<<1]; //表示二进制x的最末尾的一个1
int lowbit(int x)
{
return x&(-x);
} //区间求和
int getSum(int x)
{
int ans=0;
while(x<=n)
{
ans+=c[x];
x+=lowbit(x);
}
return ans;
} //更新节点
void update(int pos,int val)
{
while(pos>0)
{
c[pos]+=val;
pos-=lowbit(pos);
}
} int main()
{
int a,b;
while(cin>>n)
{
memset(c,0,sizeof(c));
for(int i=0;i<n;i++)
{
cin>>a>>b;
update(b,1);
update(a-1,-1);
}
for(int i=1;i<n;i++)
cout<<getSum(i)<<" ";
cout<<getSum(n)<<endl;
}
}

2、二维树状数组

https://vjudge.net/contest/239647#problem/B【POJ 2155】

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long const int MAXN=1e3+123;
int c[MAXN][MAXN];
int n,q;
int lowbit(int x)
{
return x&(-x);
} void update(int posi,int posj,int val)
{
int tmpj;
while(posi<=n)
{
tmpj=posj;
while(tmpj<=n)
{
c[posi][tmpj]+=val;
tmpj+=lowbit(tmpj);
}
posi+=lowbit(posi);
}
} int getSum(int posi,int posj)
{
int ans=0;
int tmpj=posj;
while(posi>0)
{
tmpj=posj;
while(tmpj>0)
{
ans+=c[posi][tmpj];
tmpj-=lowbit(tmpj);
}
posi-=lowbit(posi);
}
return ans;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{ char ch;
int x1,y1,x2,y2;
int x,y;
scanf("%d%d",&n,&q);
memset(c,0,sizeof(c));
while(q--)
{
getchar();
scanf("%c",&ch);
if(ch=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
update(x1,y1,1);
update(x2+1,y1,1);
update(x1,y2+1,1);
update(x2+1,y2+1,1);
}
else
{
scanf("%d%d",&x,&y);
//查询前k的和对2取模即可
printf("%d\n",getSum(x,y)%2);
} }
if(t)printf("\n");
} return 0;
}

3、树状数组求逆序数

https://blog.csdn.net/Fire_to_cheat_/article/details/81129609

#include<bits/stdc++.h>
using namespace std;
#define ll long long const int MAXN=5e5+10; ll sum[MAXN],n;
struct Node
{
int id,digit;
Node(){}
Node(int i,int d):id(i),digit(d){}
}node[MAXN]; bool cmp(Node a,Node b)
{
return a.digit<b.digit;
} ll lowbit(ll x)
{
return x&(-x);
} void update(ll pos,ll val)
{
while(pos<=n)
{
sum[pos]+=val;
pos+=lowbit(pos);
}
} ll getSum(ll x)
{
ll ans=0;
while(x>0)
{
ans+=sum[x];
x-=lowbit(x);
}
return ans;
} int main()
{
while(~scanf("%d",&n))
{
if(n==0)break;
int x;
for(int i=1;i<=n;i++)
{
cin>>x;
node[i]=Node(i,x);
}
sort(node+1,node+n+1,cmp);
memset(sum,0,sizeof(sum));
ll cnt=0;
for(int i=1;i<=n;i++)
{
update(node[i].id,1);
cnt+=(i-getSum(node[i].id));
}
cout<<cnt<<endl; }
return 0; }

bryce1010专题训练——树状数组的更多相关文章

  1. NOIp 数据结构专题总结 (2):分块、树状数组、线段树

    系列索引: NOIp 数据结构专题总结 (1) NOIp 数据结构专题总结 (2) 分块 阅:<「分块」数列分块入门 1-9 by hzwer> 树状数组 Binary Indexed T ...

  2. 【算法系列学习】线段树vs树状数组 单点修改,区间查询 [kuangbin带你飞]专题七 线段树 A - 敌兵布阵

    https://vjudge.net/contest/66989#problem/A 单点修改,区间查询 方法一:线段树 http://www.cnblogs.com/kuangbin/archive ...

  3. 2018牛客网暑假ACM多校训练赛(第五场)H subseq 树状数组

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-H.html 题目传送门 - https://www.no ...

  4. 2018牛客网暑假ACM多校训练赛(第五场)F take 树状数组,期望

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-F.html 题目传送门 - https://www.no ...

  5. 树状数组训练题2:SuperBrother打鼹鼠(vijos1512)

    先给题目链接:打鼹鼠 这道题怎么写? 很明显是树状数组. 而且,很明显是二维树状数组. 如果你没学过二维的树状数组,那么戳开这里:二维树状数组 看完以后,你就会知道怎么做了. 没有什么好解释的,几乎就 ...

  6. 树状数组训练题1:弱弱的战壕(vijos1066)

    题目链接:弱弱的战壕 这道题似乎是vijos上能找到的最简单的树状数组题了. 原来,我有一个错误的思想,我的设计是维护两个树状数组,一个是横坐标,一个是纵坐标,然后读入每个点的坐标,扔进对应的树状数组 ...

  7. 牛客训练六:海啸(二维树状数组+vector函数的使用)

    题目链接:传送门 思路: 二维树状数组, vector(first,last)函数中assign函数相当于将first中的函数清空,然后将last中的值赋值给first. 参考文章:传送门 #incl ...

  8. CDOJ 838 母仪天下 树状数组 (2014数据结构专题

    母仪天下 Time Limit: 1 Sec  Memory Limit: 162 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/838 Descrip ...

  9. 牛客网多校训练第一场 J - Different Integers(树状数组 + 问题转换)

    链接: https://www.nowcoder.com/acm/contest/139/J 题意: 给出n个整数的序列a(1≤ai≤n)和q个询问(1≤n,q≤1e5),每个询问包含两个整数L和R( ...

随机推荐

  1. const *char p和char const *p

    const *char p和char const *p,const char*p的区别 char*const p——p必须初始化,且不能指向别处,即p是指针常量: char const*p——p指向的 ...

  2. hadoop mapred和mapreduce包

    mapred包是老的1.0的map reduce api mapreduce包是新的2.0的map reduce api

  3. spark通信原理

    https://github.com/apache/spark/tree/master/core/src/main/scala/org/apache/spark/network https://git ...

  4. CSS3 (一)

    属性选择器 1. E[attr^="value"]:指定了属性名,并且有属性值,属性值是以value开头的: .wrap a[href^="http://"]{ ...

  5. Mysql的Merge存储引擎实现分表查询

    对于数据量很大的一张表,i/o效率底下,分表势在必行! 使用程序分,对不同的查询,分配到不同的子表中,是个解决方案,但要改代码,对查询不透明. 好在mysql 有两个解决方案: Partition(分 ...

  6. ulink函数的使用【学习笔记】

    #include "apue.h" #include <fcntl.h> int main(void) { ) err_sys("open error&quo ...

  7. BZOJ_2443_[Usaco2011 Open]奇数度数 _并查集+树形DP

    BZOJ_2443_[Usaco2011 Open]奇数度数 _并查集. Description 奶牛们遭到了进攻!在他们的共和国里,有N(1 <= N <=50,000)个城市,由M(1 ...

  8. python 高性能web框架 gunicorn+gevent

    参考链接: http://rfyiamcool.blog.51cto.com/1030776/1276364/ http://www.cnblogs.com/nanrou/p/7026789.html ...

  9. unity aSSETBUNDEL (转)

    无论是模型资源还是UI资源,最好是先把他们放在Prefab中,然后在做成Assetbundle.我们以模型来举例,Assetbundle中可以放一个模型.也可以放多个模型,它是非常灵活了那么最需要考虑 ...

  10. html 样式之style属性的使用

    转自:https://www.ggbiji.com/html-style.html html中的style属性是用来改变html元素的样式的,样式是 在html 4 引入的,它是改变 html元素样式 ...