Need Help in HORRIBLE Queries SPOJ , using SQRT decomposition [Range sum+ Query]

Can anyone find me one question based on range query and range update problem.

1 L R X =>update in array from L to R a[i] +=x

2 L R =>find sum from L to R

I guess this would be okay, if that is not what you were looking for then I just found these two blogs on Segment Tree Problems and Everything about SegTrees hope these help :sweat_smile:.

1 Like

Yes, as said above, you are looking for Segment Trees problems, I would really encourage you to go for Segment Trees Master class by Codeforces Edu! It’s just awesome!

https://www.spoj.com/problems/HORRIBLE/

https://cses.fi/problemset/task/1735
https://cses.fi/problemset/task/1736 (A bit harder than simple range sum)
https://codeforces.com/contest/446/problem/C

I tried this problem : “https://www.spoj.com/problems/HORRIBLE/

My code Using SQRT : “https://ideone.com/kCbylL

I don’t know where I did mistake and how can I resolve ? @carre @tanuj_khattar Please help .

why sqrt? It’s straight BIT or Lazy SegTree application. I doubt you manage to get it pass in the time limit using SQRT decomposition with updates

2 Likes

Bro actually I wanna do this with sqrt , ( learnt from unacademy ) , but on sample case it shows wa, plz see and correct my code.

you are not updating block[i]

For each block , I added value in lazy[block number] , so where I have to update block[i] ?

if a block is partialy updated you are just updating individual elements but if then the block is query entirely you just check block[i] value that you didn’t update

sum += block[i] + lazy[i]*(blk_sz);

Please edit my code if possible. @carre

code
/*
Author : Chandan Agrawal
College : Poornima College of Engg. jaipur, Raj
Mail : chandanagrawal23@gmail.com
" when you are not practicing someone else is ,
 and the day u meet them u will lose "
*/
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;

#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define MAX 100050

#define ll long long
#define ld long double
#define lli long long int

#define pb push_back
#define INF 1000000000000
#define mod 1000000007

// trignometric function always give value in Radians only
#define PI acos(-1) //3.1415926535897932384626433832795028
#define dsin(degree) sin(degree*(PI/180.0))
#define dcos(degree) cos(degree*(PI/180.0))
#define dtan(degree) tan(degree*(PI/180.0))

#define rsin(radian) sin(radian)
#define rcos(radian) cos(radian)
#define rtan(radian) tan(radian)

#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define memf(a) memset(a,false,sizeof(a))

#define loop(i,n)  for (lli i = 0; i < n; i++)
#define FOR(i,a,b) for (lli i = a; i < b; i++)

#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define makeuniq(v) v.resize(unique(all(v)) - v.begin()); //only uniq element in vector after this
#define sz(x) int(x.size())
#define F first
#define S second

#define mii map<lli,lli>

#define pii pair<lli,lli>

#define vi vector<lli>
#define vvi vector<vi>
#define vpi vector<pii>
#define vbool vector<bool>

#define seti set<lli>

#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (a/gcd(a,b))*b
#define abs(x) ((x < 0)?-(x):x)

#define endl '\n'

template <typename Head>
void print(Head&& head)
{
    cout<<head<<endl;
}
template <typename Head, typename... Tail>
void print(Head&& head, Tail... tail)
{
    cout<<head<<" ";
    print(tail...);
}

#define scanarr(a,n) for(lli i=0;i<n;i++)    cin>>a[i];
#define scanvec(a,n) for(lli i=0;i<n;i++){ lli x ; cin>>x; a.pb(x);}

#define printarr(a,n) for(lli i=0;i<n;i++)   cout<<a[i]<<" "; cout<<endl;
#define printvec(vec) for(auto xt : vec) cout<<xt<<" ";    cout<<"\n";

#define FD(N) fixed<<setprecision(N)

#define deb(x) cout<<#x<<" "<<x<<endl;

/*
1D vector -  vi dp(n,value);
2D vector -  vvi dp(n,vi(n,value));
*/

// chandan1,2
void chandan1(){int y=1;return;}
void chandan2(){
    loop(i,10){
        lli x=1;
    }
    return(chandan1());
}

//---------------------------------------------------SQRT_DECOMPO-------------------------------------------------------

lli n , a[MAX];
lli block[400];
lli lazy[400];
lli blk_sz;

// clear all data
void clear()
{
    mem0(a);
    mem0(block);
    mem0(lazy);
}

// preprocess array to sqrt decomposition
void preprocess(lli n)
{
    blk_sz = sqrt(n);

    lli blk_ind = -1;

    loop(i,n)
    {
        if(i % blk_sz == 0)
            blk_ind++;

        block[blk_ind]+=(a[i]);
    }
}

// query to find min from l to r in range
lli query(lli l , lli r)
{
    lli left_block = l/blk_sz;

    lli right_block = r/blk_sz;

    lli sum = 0;

    if(left_block == right_block)
    {
        for(lli i=l;i<=r;i++)
            sum += (a[i] + lazy[left_block]);
    }
    else
    {
        for(lli i=l;i<blk_sz*(left_block+1);i++) // traversing first block in range 
        {
            sum += (a[i] + lazy[left_block]);
        }
        for(lli i=left_block+1;i<right_block;i++) // traversing completely overlapped blocks in range 
        {
            sum += block[i];
        }

        for(lli i=blk_sz*right_block; i<=r; i++) // traversing last block in range 
        {
            sum += (a[i] + lazy[right_block]);
        }
    }

    return sum;
}

void update(lli l , lli r , lli val)
{
    lli left_block = l / blk_sz;

    lli right_block = r / blk_sz;

    if(left_block == right_block)
    {
        for(lli i=l;i<=r;i++)
            a[i] += val;
        block[left_block] += (r-l+1)*val;
    }
    else
    {
        for(lli i=l;i<blk_sz*(left_block+1);i++) // traversing first block in range 
            a[i] += val;
        block[left_block] += (blk_sz*(left_block+1)-l)*val;
        for(lli i=left_block+1;i<right_block;i++) { // traversing completely overlapped blocks in range
            lazy[i] += val;
            block[i] += val*blk_sz;
        }
        for(lli i=blk_sz*right_block;i<=r;i++) //// traversing last block in range 
            a[i] += val;
        block[right_block] += (r-blk_sz*right_block+1)*val;
    }
}

//---------------------------------------------------SQRT_DECOMPO-------------------------------------------------------

int main(){
    fastio
    chandan2();
    lli t;
    cin>>t;
    while(t--)
    {
        lli q;
        cin>>n>>q;
        loop(i,n)
            a[i]=0;

        preprocess(n);

        while(q--)
        {
            lli type;
            cin>>type;
            if(type == 1)
            {
                lli l,r;
                cin>>l>>r;
                l--;
                r--;
                print(query(l,r));
            }
            else
            {
                lli l,r,val;
                cin>>l>>r>>val;
                --l , --r;
                update(l,r,val);
            }
        }
        clear();
    }
    return 0;
}
1 Like

Thank You so much @carre . :pray:
SQRT also AC in above question.