树状数组(插线问点)NYOJ123士兵杀敌(四)
2012-05-03 16:14
246 查看
题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=123
这道题目可以用树状数组做,是很典型很裸的插线问点的题目,可能也可以用险段树写吧。
其中插线的过程可以这样理解:如果向[star,end]这个区间加value,就可以看做是向[o,end]加value,然后向[0,star-1]加-value。这样就实现了~~~~
题意很简单,一看就知道,以下是个人代码:
这道题目可以用树状数组做,是很典型很裸的插线问点的题目,可能也可以用险段树写吧。
其中插线的过程可以这样理解:如果向[star,end]这个区间加value,就可以看做是向[o,end]加value,然后向[0,star-1]加-value。这样就实现了~~~~
题意很简单,一看就知道,以下是个人代码:
#include <stdio.h> #include <iostream> #include <string.h> #define N 1000005 using namespace std; int add ; int n,m; int lowbit(int k) { return k&(-k); } void update(int star,int value) { while(star>0) { add[star]+=value; star-=lowbit(star); } } int sum(int end) { int ans=0; while(end<=m) { ans+=add[end]; end+=lowbit(end); } return ans; } int main() { memset(add,0,sizeof(add)); int i,star,end,que,value; char ss[15]; scanf("%d%*c%d%*c",&n,&m); for(i=0;i<n;i++) { scanf("%s%*c",ss); if(ss[0]=='A') { scanf("%d%*c%d%*c%d%*c",&star,&end,&value); update(end,value); update(star-1,-value); } else { scanf("%d%*c",&que); printf("%d\n",sum(que)); } } }
相关文章推荐
- NYOJ 123士兵杀敌(四)(树状数组)(插线问点)
- NYOJ 123士兵杀敌(四)【树状数组||区间树】
- hdu-1556 Color the ball && nyoj -123 士兵杀敌(四)----------》树状数组
- nyoj 123 士兵杀敌(四)【树状数组】+【插线问点】
- nyoj 123 士兵杀敌(四)(树状数组)
- nyoj 123 士兵杀敌(四)【树状数组】+【插线问点】
- nyoj123士兵杀敌(四)(树状数组)
- nyoj~123 士兵杀敌(四)(树状数组)
- NYOJ 123 士兵杀敌4-树状数组的插线求点
- nyoj 123 士兵杀敌(四) 树状数组【单点查询+区间修改】
- nyoj 士兵杀敌(一)(树状数组)
- NYOJ116 士兵杀敌(二)【树状数组】
- nyoj 116 士兵杀敌(二)树状数组
- NYOJ 123 士兵杀敌(四)
- NYOJ-士兵杀敌(四) 线段树、区间数、树状数组
- NYOJ 123 士兵杀敌4-树状数组的插线求点
- nyoj123 士兵杀敌四
- nyoj 士兵杀敌(一)(树状数组)
- nyoj123 士兵杀敌(四) (线段树)
- NYOJ-123 士兵杀敌(四) (线段树)