c++ - Lazy Propagation - HORRIBLE spoj -


i attempting problem horrible spoj , here's link: http://www.spoj.com/problems/horrible/ trying teach myself lazy propagation segment tree. following code have written, have tried make concise , clear possible, please let me know if unclear.

#include <cstdio> #include <algorithm> using namespace std;  typedef long long ll;  ll tree[500010]; ll lazy[500010]={};  void buildst(ll a[], ll stidx, ll l, ll r) {     if(l==r)     {         tree[stidx]=a[l];         return;     }     ll left=2*stidx, right=left+1, mid=(l+r)/2;     buildst(a, left, l, mid);     buildst(a, right, mid+1, r);     tree[stidx]=tree[left]+tree[right]; }  void update(ll stidx, ll i, ll j, ll l, ll r, ll val) {     //printf("%lld %lld %lld %lld %lld\n", stidx, i, j, l, r);     if(lazy[stidx]!=0)     {         tree[stidx]+=(l-r+1)*lazy[stidx];         if(l!=r)         {             lazy[2*stidx]+=lazy[stidx];             lazy[2*stidx+1]+=lazy[stidx];         }         lazy[stidx]=0;     }     //printf("1\n");     if(l>r || l>j || r<i)         return;     //printf("1\n");     if(l>=i && r<=j)     {         //printf("%lld\n", stidx);         tree[stidx]+=(r-l+1)*val;         if(l!=r)         {             lazy[2*stidx]+=val;             lazy[2*stidx+1]+=val;         }         return;     }     ll mid=(l+r)/2;     update(2*stidx, i, j, l, mid, val);     update(2*stidx+1, i, j, mid+1, r, val);     tree[stidx]=tree[2*stidx]+tree[2*stidx+1]; }  ll query(ll stidx, ll i, ll j, ll l, ll r) {     if(lazy[stidx]!=0)     {         tree[stidx]+=lazy[stidx]*(r-l+1);         if(l!=r)         {             lazy[2*stidx]+=lazy[stidx];             lazy[2*stidx+1]+=lazy[stidx];         }         lazy[stidx]=0;     }     if(l>r || l>j || r<i)         return 0;     if(l>=i && r<=j)         return tree[stidx];     ll mid=(l+r)/2;     ll q1=query(2*stidx, i, j, l, mid);     ll q2=query(2*stidx+1, i, j, mid+1, r);     return q1+q2; }  ll a[100010];  int main() {     ll t, n, c, type, p, q, v, i;     scanf("%lld", &t);     while(t--)     {         scanf(" %lld %lld", &n, &c);         for(i=0; i<n; ++i)             a[i]=0;         for(i=0; i<4*n; ++i)             lazy[i]=0;         buildst(a, 1, 0, n-1);         while(c--)         {             scanf(" %lld", &type);             if(type==0)             {                 scanf(" %lld %lld %lld", &p, &q, &v);                 update(1, p-1, q-1, 0, n-1, v);                 //for(i=1; i<=15; ++i)                 //  printf("%lld ", tree[i]);                 //printf("\n");              }             if(type==1)             {                 scanf(" %lld %lld", &p, &q);                 printf("%lld\n", query(1, p-1, q-1, 0, n-1));             }         }     }     return 0; } 

the sample testcase provided gives correct answer code when submitted, wrong answer message. have checked code lot of times can't find bug.

any appreciated. thanks!

one of error can see in update function

instead of tree[stidx]+=(l-r+1)*lazy[stidx]; should tree[stidx]+=(r-l+1)*lazy[stidx];


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -