HDU 1166 树状数组和线段树

2024-03-20 11:32
文章标签 数组 树状 hdu 线段 1166

本文主要是介绍HDU 1166 树状数组和线段树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

树状数组

#include<bits/stdc++.h>
using namespace std;
long long  data[50001],s[50001],T[50001];
long long  lowbit(long long t)
{return t&(-t);
}
long long  sum(long long  eend)
{long long  sum=0;while(eend>0){sum+=T[eend];eend-=lowbit(eend);}return sum;
}
void pplus(long long  pos,long long  num,long long  ccount)
{while(pos<=ccount){T[pos]+=num;pos+=lowbit(pos);}
}
int main()
{ios::sync_with_stdio(false);string str;int t,n,a,b;cin>>t;for(int i=1; i<=t; i++){cin>>n;T[0]=s[0]=data[0]=0;for(int j=1; j<=n; j++){cin>>data[j];s[j]=s[j-1]+data[j];T[j]=s[j]-s[j-lowbit(j)];}cout<<"Case "<<i<<":"<<endl;while(cin>>str&&str!="End"){cin>>a>>b;if(str=="Query")cout<<sum(b)-sum(a)+data[a]<<endl;else if(str=="Add")pplus(a,b,n),data[a]+=b;else pplus(a,-b,n),data[a]-=b;}}
}

线段树

#include<bits/stdc++.h>
using namespace std;
const int N= 50000;
struct node
{int l,r;long long sum;
};
node tree[N*3];
void buildtree(int l,int r,int pos)
{tree[pos].l=l;tree[pos].r=r;if(l==r){cin>>tree[pos].sum;return ;}int mid=(l+r)>>1;buildtree(l,mid,pos*2);buildtree(mid+1,r,pos*2+1);tree[pos].sum=tree[pos*2].sum+tree[pos*2+1].sum;
}
long long query(int l,int r,int pos)
{if(l==tree[pos].l&&r==tree[pos].r)return tree[pos].sum;else if(r<=(tree[pos].l+tree[pos].r)/2)return query(l,r,pos<<1);else if(l>=(tree[pos].l+tree[pos].r)/2+1)return query(l,r,(pos<<1)+1);elsereturn query(l,tree[pos*2].r,pos*2)+query(tree[pos*2+1].l,r,pos*2+1);
}
void update(int pos,int k,int add)
{if(k>=tree[pos].l&&k<=tree[pos].r){tree[pos].sum+=add;if(tree[pos].l==tree[pos].r)return ;update(pos*2,k,add);update(pos*2+1,k,add);}return ;
}
int main()
{ios::sync_with_stdio(false);string str;int t,n,a,b;cin>>t;for(int i=1; i<=t; i++){cin>>n;cout<<"Case "<<i<<":"<<endl;buildtree(1,n,1);while(cin>>str&&str!="End"){cin>>a>>b;if(str=="Query")cout<<query(a,b,1)<<endl;else if(str=="Add")update(1,a,b);else update(1,a,-b);}}return 0;
}


这篇关于HDU 1166 树状数组和线段树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/829322

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while