Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4970
It seems that the Interval Update has not been written in the tree array, but the tree array is indeed much faster than the line tree. I don't know how efficient it is than the zkw line tree:
Add a template first:
# Include <cstdio> const int maxn = 1024; int B [maxn], C [maxn]; # define lowbit (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-= lowbit (p )) S + = A [p]; return s;} void bit_update2 (int * a, int P, int d) {for (; P-= lowbit (p )) A [p] + = D;} int bit_query2 (int * a, int p) {int S = 0; (; 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,, b, C; while (SC ANF ("% d", & COM, & A, & B )! = EOF) {A + = 2; B + = 2; // prevents negative values. If (COM = 0) {// update scanf ("% d ", & C); insert_seg (A, B, C);} else {// query printf ("% d/N", query_seg (A, B ));}} return 0 ;}
This question is bare, that is, the sum of the intervals, and then the sum of the current monster distance N is queried. If it is greater than hi, the monster will die for 500 ms AC
#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <iostream>#include <iomanip>#include <cmath>#include <map>#include <set>#include <queue>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<e;i++)#define repe(i,s,e) for(int i=s;i<=e;i++)#define CL(a,b) memset(a,b,sizeof(a))#define IN(s) freopen(s,"r",stdin)#define OUT(s) freopen(s,"w",stdout)const ll ll_INF = ((ull)(-1))>>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# " <<p << " " << d<<endl; ///////////////// while(p&&p<=n) { a[p]+=d; p+=lowb(p); }}void bitupdate2(ll *a, ll p,ll d){ //////////////// //cout << "#upda 222# " <<p << " " << d<<endl; ///////////////// while(p>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 << "## " <<p << " " << d<<endl; /////////////////}inline void insert_seg(ll x, ll y, ll d){ Insert(x-1,-d); Insert(y,d);}inline ll query(ll x){ return bitquery(b,x)+bitquery2(c,x)*x;}inline ll query_seg(ll a, ll b){ return query(b)-query(a-1);}int main(){ // IN("hdu4970.txt"); ll ans=0; while(~scanf("%d",&n) && n) { init(); scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%I64d%I64d%I64d",&L[i],&r[i],&d[i]); insert_seg(L[i],r[i],d[i]); } scanf("%d",&k); ans=0; for(int i=1;i<=k;i++) { scanf("%I64d%I64d",&h[i],&p[i]); ///////////////////////// //cout << "h[]" << i << " " << h[i] << "query " << query_seg(p[i],n) <<endl; ///////////////////////// if(query_seg(p[i],n)>=h[i])ans++; } printf("%I64d\n",k-ans); } return 0;}
Standard method:
In fact, it is also the idea of tree arrays, but I am still not very familiar with tree arrays. 300 ms AC
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#define maxn 100010using 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<m;i++) { scanf("%d%d%I64d",&l,&r,&v); delta[l]+=v; delta[r+1]-=v; } for(int i=2;i<=n;i++) { delta[i]+=delta[i-1]; } for(int i=n-1;i>=1;i--) { delta[i]+=delta[i+1]; } int ans=0; scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%I64d%d",&v,&r); if(v>delta[r]) ans++; } printf("%d\n",ans); } return 0;}
HDU 4970 tree array Interval Update thinking questions