hdu 4970 树状数组区间更新 思维题

http://acm.hdu.edu.cn/showproblem.php?pid=4970

好像还没有用树状数组写过区间更新,但是树状数组的确比线段树快很多,不知道跟ZKW线段树比效率怎么样:
先贴个模板:

#include 

const int MAXN = 1024;
int B[MAXN], C[MAXN];

#define LOWBIT(x) ((x)&(-(x)))

void bit_update(int *a, int p, int d) {
    for ( ; p && p < MAXN ; p += LOWBIT(p))
        a[p] += d;
}

int bit_query(int *a, int p) {
    int s = 0;
    for ( ; p ; p -= LOWBIT(p))
        s += a[p];
    return s;
}

void bit_update2(int *a, int p, int d) {
    for ( ; p ; p -= LOWBIT(p))
        a[p] += d;
}

int bit_query2(int *a, int p) {
    int s = 0;
    for ( ; p && p < MAXN ; p += LOWBIT(p))
        s += a[p];
    return s;
}

inline void _insert(int p, int d) {
    bit_update(B, p, p*d);
    bit_update2(C, p-1, d);
}

inline int _query(int p) {
    return bit_query(B, p) + bit_query2(C, p) * p;
}

inline void insert_seg(int a, int b, int d) {
    _insert(a-1, -d);
    _insert(b, d);
}

inline int query_seg(int a, int b) {
    return _query(b) - _query(a-1);
}

int main() {
    int com, a, b, c;
    while (scanf("%d%d%d",&com,&a,&b) != EOF) {
        a += 2; b += 2;     //防止出现负数
        if (com == 0) {     //更新
            scanf("%d",&c);
            insert_seg(a, b, c);
        } else {            //查询
            printf("%d/n",query_seg(a,b));
        }
    }
    return 0;
}

本题比较裸,就是区间求和,然后查询当前的monster 距离N之间杀伤的和,如果大于hi那么怪物死去 500ms AC

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i>1;
const double EPS = 1e-8;
const int INF = 100000000;

const int MAXN = 100000+100;

ll b[MAXN],c[MAXN];
int n,k,m;
ll L[MAXN],r[MAXN],d[MAXN],h[MAXN],p[MAXN];

inline ll lowb(ll x){return x&(-x);}

void init()
{
    CL(b,0);
    CL(c,0);
}

void bitupdate(ll *a, ll p, ll d)
{

    ////////////////
    //cout << "#update# " <

0) { a[p]+=d; p-=lowb(p); } } ll bitquery(ll *a,ll p) { ll s=0; while(p) { s+=a[p]; p-=lowb(p); } return s; } ll bitquery2(ll *a, ll p) { ll s=0; while(p && p<=n) { s+=a[p]; p+=lowb(p); } return s; } inline void Insert(ll p, ll d) { bitupdate(b,p,p*d); bitupdate2(c,p-1,d); //////////////// //cout << "## " <

=h[i])ans++; } printf("%I64d\n",k-ans); } return 0; }


标程的方法:
hdu 4970 树状数组区间更新 思维题_第1张图片

其实也是树状数组的思想,但是我树状数组还是不怎么熟练啊 300ms AC

#include 
#include 
#include 
#include 
#include 
#define maxn 100010
using namespace std;
typedef long long ll;

ll delta[maxn];

int main()
{
  int n,m,l,r;
  ll v;
  while(scanf("%d",&n)&&n)
  {
    memset(delta,0,sizeof(delta[0])*(n+5));
    scanf("%d",&m);
    for(int i=0;i=1;i--)
    {
      delta[i]+=delta[i+1];
    }
    int ans=0;
    scanf("%d",&m);
    for(int i=0;idelta[r])
        ans++;
    }
    printf("%d\n",ans);
  }
  return 0;
}



你可能感兴趣的:(hdu 4970 树状数组区间更新 思维题)