SPOJ KALTSUM – k Alternating Sum

Problem Link : http://www.spoj.com/problems/KALTSUM/

Solution Idea:

Break the problem into two cases: When k is greater than sqrt(n) and when k is less tahn or equal sqrt(n). Deal with them in different ways.

Case 1: For any k>sqrt(N) there will be less than sqrt(N) alternating segment. If you have an array, having cumulative sum, you can get the sum of a contiguous segment in O(1). So you can just loop over the alternating parts and get the sum in O(sqrt(N)) for a single query.

Case 2: Now when k <= sqrt(n), you need to do some preprocessing.

let arr[k][i] be the "k alternating sum" of a subarray which starts at position i and keeps alternating untill it reaches a position x such that if you add another segment of size k then it will go out of the array. [ This array can be computed in O(N) , and as you're doing it for every k < sqrt(n), the total complexity is O(N * sqrt(N))] With the help of this array, you can answer every query having k < sqrt(N) in O(1).

This solution idea is from this link.


#include &lt;bits/stdc++.h&gt;

#define pii              pair &lt;int,int&gt;
#define pll              pair &lt;long long,long long&gt;
#define sc               scanf
#define pf               printf
#define Pi               2*acos(0.0)
#define ms(a,b)          memset(a, b, sizeof(a))
#define pb(a)            push_back(a)
#define MP               make_pair
#define db               double
#define ll               long long
#define EPS              10E-10
#define ff               first
#define ss               second
#define sqr(x)           (x)*(x)
#define D(x)             cout&lt;&lt;#x &quot; = &quot;&lt;&lt;(x)&lt;&lt;endl
#define VI               vector &lt;int&gt;
#define DBG              pf(&quot;Hi\n&quot;)
#define MOD              1000000007
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define SZ(a)            (int)a.size()
#define sf(a)            scanf(&quot;%d&quot;,&amp;a)
#define sfl(a)           scanf(&quot;%lld&quot;,&amp;a)
#define sff(a,b)         scanf(&quot;%d %d&quot;,&amp;a,&amp;b)
#define sffl(a,b)        scanf(&quot;%lld %lld&quot;,&amp;a,&amp;b)
#define sfff(a,b,c)      scanf(&quot;%d %d %d&quot;,&amp;a,&amp;b,&amp;c)
#define sfffl(a,b,c)     scanf(&quot;%lld %lld %lld&quot;,&amp;a,&amp;b,&amp;c)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i&lt;n;i++)
#define loop1(i,n)       for(int i=1;i&lt;=n;i++)
#define REP(i,a,b)       for(int i=a;i&lt;b;i++)
#define RREP(i,a,b)      for(int i=a;i&gt;=b;i--)
#define TEST_CASE(t)     for(int z=1;z&lt;=t;z++)
#define PRINT_CASE       printf(&quot;Case %d: &quot;,z)
#define LINE_PRINT_CASE  printf(&quot;Case %d:\n&quot;,z)
#define CASE_PRINT       cout&lt;&lt;&quot;Case &quot;&lt;&lt;z&lt;&lt;&quot;: &quot;
#define all(a)           a.begin(),a.end()
#define intlim           2147483648
#define infinity         (1&lt;&lt;28)
#define ull              unsigned long long
#define gcd(a, b)        __gcd(a, b)
#define lcm(a, b)        ((a)*((b)/gcd(a,b)))

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1&lt;&lt;pos);}
//int reset(int N,int pos){return N= N &amp; ~(1&lt;&lt;pos);}
//bool check(int N,int pos){return (bool)(N &amp; (1&lt;&lt;pos));}
/*------------------------------------------------*/

ll dp[318][100005];
ll csum[100005];
ll ara[100005];

int main()
{

//    freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
//	  freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);

    int n,q;

    sff(n,q);
    for(int i=1;i&lt;=n;i++)
    {
        sfl(ara[i]);
        csum[i]=csum[i-1]+ara[i];
    }

    int root=ceil(sqrt(n));

    for(int i=1;i&lt;=root;i++)
    {
        ll temp=0,sub=0;
        for(int j=n;j&gt;=1;j--)
        {
            temp+=ara[j];
            if(j+i&gt;n)
                sub=0;
            else
                sub=ara[j+i];
            temp-=sub;

            if(j+i-1&gt;n)
                dp[i][j]=0;
            else
                dp[i][j]=temp-dp[i][j+i];
        }
    }
//
//    for(int i=1;i&lt;=root;i++,cout&lt;&lt;endl)
//        for(int j=1;j&lt;=n;j++)
//        cout&lt;&lt;dp[i][j]&lt;&lt;&quot; &quot;;

    while(q--)
    {
        int a,b,k;
        sfff(a,b,k);
        ll ans=0;
        if(k&gt;root)
        {
            for(int i=a,j=0;i&lt;=b;i+=k,j++)
            {
                if(j%2==0)
                    ans+=csum[i+k-1]-csum[i-1];
                else
                    ans-=csum[i+k-1]-csum[i-1];
            }
            pf(&quot;%lld\n&quot;,ans);
        }
        else
        {
            int turn=(b-a+1)/k;
            if(turn%2==1)
                pf(&quot;%lld\n&quot;,dp[k][a]+dp[k][b+1]);
            else
                pf(&quot;%lld\n&quot;,dp[k][a]-dp[k][b+1]);
        }
    }


    return 0;
}


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments