# SPOJ: SUMSUM – Enjoy Sum with Operations

0

Solution Idea:

This one is a interesting problem. The main idea behind the problem is combinatorics. At first we need to count the number of 1 bit and number of 0 bit in the i_th position of a given number. Then we need to do some combinatorial calculation.

Now think about an array of 4 number A=[1,0,1,0]. If we perform OR operation in the range 1 to 4 what will we get? We get 5 as answer. If we form pair from this number and perform OR operation between then then we will get the answer. Now as here is 4 element so we can perform pair in 4*(4-1)/2 = 6 ways. Now comes the tricky part. For OR operation how many pair will contribute nothing ?? Here is 2 zero and we can form 1 pair from this two element who will not contribute anything. So for i_th bit position the contribution for OR operation is number_of_active_pair*(2^i).

For AND operation we need to consider the number of pair can be form by 1 bit and for XOR the number of pair will be number_of_one_bit*number_of_zero_bit.

We can consider a number as an array of 30 element whose value is either 0 or 1 and perform above operation.

```
#include &lt;bits/stdc++.h&gt;
#include &lt;ext/pb_ds/assoc_container.hpp&gt;
#include &lt;ext/pb_ds/tree_policy.hpp&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)             cerr&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;

//using namespace __gnu_pbds;
//typedef tree&lt;int, null_type, less&lt;int&gt;, rb_tree_tag, tree_order_statistics_node_update&gt; ordered_set;

/*----------------------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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

#define mx 100005

int tree[28][mx];

void update(int id, int idx, int val)
{
for(;idx&lt;mx &amp;&amp; idx;idx+=idx&amp;-idx)
tree[id][idx]+=val;
}

ll query(int id, int idx)
{
ll ret=0;
for(;idx;idx-=idx&amp;-idx)
ret+=tree[id][idx];
return ret;
}

int ara[mx];

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++) sf(ara[i]);

for(int i=1;i&lt;=n;i++)
{
for(int j=0;j&lt;28;j++)
{
if(check(ara[i],j))
update(j,i,1);
}
}

while(q--)
{
int a,b,c;
sf(a);
if(a==1)
{
sff(b,c);
for(int i=0;i&lt;28;i++)
{
if(check(ara,i))
update(i,c,-1);
}
ara=b;
for(int i=0;i&lt;28;i++)
{
if(check(ara,i))
update(i,c,1);
}
}
else
{
char ss[10];
scanf(&quot; %s&quot;,&amp;ss);
string str=string(ss);
sff(b,c);
ll temp[30];
for(int i=0;i&lt;28;i++)
{
temp[i]=query(i,c);
}
for(int i=0;i&lt;28;i++)
{
temp[i]-=query(i,b-1);
}

ll ans=0;

for(int i=0;i&lt;28;i++)
{
ll one=temp[i];
ll zero=(c-b+1)-one;
ll pairs=0;
if(str==&quot;OR&quot;)
{
ll total=one+zero;
pairs=(total*(total-1))/2;
pairs-=(zero*(zero-1))/2;
}
else if(str==&quot;AND&quot;)
{
pairs=(one*(one-1))/2;
}
else if(str==&quot;XOR&quot;)
{
pairs=one*zero;
}
ans+=(1LL&lt;&lt;i)*pairs;
}

printf(&quot;%lld\n&quot;,ans);
}
}

return 0;
}

```

# SPOJ: FREQUENT – Frequent values

0

Solution Idea:

In this problem an array of non-decreasing order is given. In each query a range l and r is given. You have to tell the maximum frequency of a number in the range l to r.

Now as the given array is in non-decreasing order. So you can replace array a = {1 1 1 2 2 3 3 3 3} by another array b = {3 2 4}. After this compression the problem is converted to a Range Maximum query problem which can be easily solved by segment tree or sparse table. Now for each query determine the index in the compress array and perform range maximum query on that range.

Think about the first and index of the query range. You need to handle them separately.

```
#include &lt;bits/stdc++.h&gt;
#include &lt;ext/pb_ds/assoc_container.hpp&gt;
#include &lt;ext/pb_ds/tree_policy.hpp&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)             cerr&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;

//using namespace __gnu_pbds;
//typedef tree&lt;int, null_type, less&lt;int&gt;, rb_tree_tag, tree_order_statistics_node_update&gt; ordered_set;

/*----------------------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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

#define mx 100005

pii block[mx];

int dp[mx][20];

int ara[mx],id[mx],cnt=0,table[mx];

int func(int idx, int p)
{
if(idx&gt;cnt) return 0;
if(p==0)
{
return dp[idx][p]=table[idx];
}
int &amp;ret=dp[idx][p];
if(ret!=-1) return ret;
ret=max(func(idx,p-1),func(idx+(1&lt;&lt;(p-1)),p-1));
return ret;
}

int query(int l, int r)
{
if(r&lt;l) return 0;
if(l==r) return table[l];
int log=(int)log2(r-l);
return max(func(l,log),func(r-(1&lt;&lt;log)+1,log));
}

int main()
{

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

int n,q;
while(sf(n))
{
if(n==0) break;
sf(q);
for(int i=1; i&lt;=n; i++)
{
sf(ara[i]);
}
cnt=0;
ms(table,0);
ms(dp,-1);
//        ms(block,0);
//        ms(id,0);
for(int i=1; i&lt;=n; i++)
{
int j=i;
cnt++;
block[cnt].ff=i;
while(ara[j]==ara[i] &amp;&amp; j&lt;=n)
{
id[j]=cnt;
table[cnt]++;
j++;
}
j--;
block[cnt].ss=j;
i=j;
}

while(q--)
{
int a,b;
sff(a,b);
if(id[a]==id[b])
{
printf(&quot;%d\n&quot;,b-a+1);
}
else
{
int l=id[a];
int r=id[b];
int ans=0;
ans=max(ans,block[l].ss-a+1);
l++;
ans=max(ans,b-block[r].ff+1);
r--;
ans=max(ans,query(l,r));
printf(&quot;%d\n&quot;,ans);
}
}

}

return 0;
}

```

# Timus: 1523. K-inversions

0

Solution Idea:

```
#include &lt;bits/stdc++.h&gt;
//#include &lt;ext/pb_ds/assoc_container.hpp&gt;
//#include &lt;ext/pb_ds/tree_policy.hpp&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)             cerr&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              1000000000
#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;

//using namespace __gnu_pbds;
//typedef tree&lt;int, null_type, less&lt;int&gt;, rb_tree_tag, tree_order_statistics_node_update&gt; ordered_set;

/*----------------------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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

#define mx 20006

ll tree[12][mx];
ll ara[mx],ara2[12][mx];

void update(int id, int idx, ll val)
{
for(; idx&lt;mx &amp;&amp; idx; idx+=idx&amp;-idx)
tree[id][idx]+=val;
}

ll query(int id, int idx)
{
ll ret=0;
for(; idx; idx-=idx&amp;-idx)
ret+=tree[id][idx];
return ret;
}

//vector&lt;ll&gt;v;

int main()
{

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

int n,k;
sff(n,k);
//    v.pb(0);
loop1(i,n)
{
sfl(ara[i]);
ara2[1][i]=1;
//        v.pb(ara[i]);
}
//    sort(all(v));

ll ans=0;
for(int j=1; j&lt;k; j++)
{
for(int i=n; i&gt;0; i--)
{
//        int id=lower_bound(all(v),ara[i])-v.begin();
int id=ara[i];
ara2[j+1][i]=query(j,id)%MOD;
update(j,id+1,ara2[j][i]);
//        update(0,id-1,-1);
}
}

//    for(int i=n; i&gt;0; i--)
//    {
////        int id=lower_bound(all(v),ara[i])-v.begin();
//        int id=ara[i];
//        ans+=query(1,id);
//        update(1,id+1,ara2[i]);
////        update(1,id-1,-ara2[i]);
//    }

for(int i=1; i&lt;=n; i++)
ans+=ara2[k][i];

printf(&quot;%lld\n&quot;,ans%MOD);

return 0;
}

```

# SPOJ: TRIPINV – Mega Inversions

0

Solution Idea:

```
#include &lt;bits/stdc++.h&gt;
#include &lt;ext/pb_ds/assoc_container.hpp&gt;
#include &lt;ext/pb_ds/tree_policy.hpp&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)             cerr&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;

//using namespace __gnu_pbds;
//typedef tree&lt;int, null_type, less&lt;int&gt;, rb_tree_tag, tree_order_statistics_node_update&gt; ordered_set;

/*----------------------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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

#define mx 100006

ll tree[2][mx];
ll ara[mx],ara2[mx];

void update(int id, int idx, ll val)
{
for(; idx&lt;mx &amp;&amp; idx; idx+=idx&amp;-idx)
tree[id][idx]+=val;
}

ll query(int id, int idx)
{
ll ret=0;
for(; idx; idx-=idx&amp;-idx)
ret+=tree[id][idx];
return ret;
}

//vector&lt;ll&gt;v;

int main()
{

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

int n;
sf(n);
//    v.pb(0);
loop1(i,n)
{
sfl(ara[i]);
//        v.pb(ara[i]);
}
//    sort(all(v));

ll ans=0;

for(int i=n; i&gt;0; i--)
{
//        int id=lower_bound(all(v),ara[i])-v.begin();
int id=ara[i];

ara2[i]=query(0,id);
update(0,id+1,1);
//        update(0,id-1,-1);
}

for(int i=n; i&gt;0; i--)
{
//        int id=lower_bound(all(v),ara[i])-v.begin();
int id=ara[i];
ans+=query(1,id);
update(1,id+1,ara2[i]);
//        update(1,id-1,-ara2[i]);
}

printf(&quot;%lld\n&quot;,ans);

return 0;
}

```

# Codeforces: 61 E. Enemy is weak

2

Solution Idea:

In this problem we need to count the number of triple inverse triplets on a given array. A triple inverse triplet is three positions i, j, k on an array A=[] such that i < j  Aj > Ak.

Now to solve this problem we need some range update and query data structure like segment tree or binary indexed tree. The idea is at first compress the array by mapping the numbers from range 1 to 10^6. After that read the array from reverse order and query the value of that mapped index on segment tree(let this mapped index is id)and store it on an array let name this array ara2=[]. After that increment the value of the index id+1 to n. It means that when we read any number from id+1 to n we can know that current number id have impact on this number. Here id is the k_th element of the triplet and all the number from id+1 to n is the j_th element of the triplet. Now each ara2[] index hold the number of inverse pair starting at id.

Now read the array element from the reverse order again and do the same thing. This time we need another segment tree. This time when we stand on position id add the value of the segment tree of position id to the answer and update all position in the range id+1 to n by ara2[index_of_id]. Do this for the whole array.

We can repeat this technique for 4,5,6 element inversion too.

```
#include &lt;bits/stdc++.h&gt;
#include &lt;ext/pb_ds/assoc_container.hpp&gt;
#include &lt;ext/pb_ds/tree_policy.hpp&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)             cerr&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;

//using namespace __gnu_pbds;
//typedef tree&lt;int, null_type, less&lt;int&gt;, rb_tree_tag, tree_order_statistics_node_update&gt; ordered_set;

/*----------------------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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

#define mx 1000006

ll tree[2][mx];
ll ara[mx],ara2[mx];

void update(int id, int idx, ll val)
{
for(; idx&lt;mx &amp;&amp; idx; idx+=idx&amp;-idx)
tree[id][idx]+=val;
}

ll query(int id, int idx)
{
ll ret=0;
for(; idx; idx-=idx&amp;-idx)
ret+=tree[id][idx];
return ret;
}

vector&lt;ll&gt;v;

int main()
{

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

int n;
sf(n);
v.pb(0);
loop1(i,n)
{
sfl(ara[i]);
v.pb(ara[i]);
}
sort(all(v));

ll ans=0;

for(int i=n; i&gt;0; i--)
{
int id=lower_bound(all(v),ara[i])-v.begin();
ara2[i]=query(0,id);
update(0,id+1,1);
//        update(0,id-1,-1);
}

for(int i=n; i&gt;0; i--)
{
int id=lower_bound(all(v),ara[i])-v.begin();
ans+=query(1,id);
update(1,id+1,ara2[i]);
//        update(1,id-1,-ara2[i]);
}

printf(&quot;%lld\n&quot;,ans);

return 0;
}

```

# Codeforces: 877E. Danil and a Part-time Job

0

Solution Idea:

Run a dfs on the given for getting the Euler tour order or inorder traversal order. Then the tree is converted to a liner 1D array. Now we can apply range query and update on this array.

Now use a range update query data structure. Here I have used segment tree. Now initialize the tree with the initial value and now our job is to just toggle a subtree of the given tree and perform query operation. Use a propagation for the update and answer the query.

```
#include &lt;bits/stdc++.h&gt;
#include &lt;ext/pb_ds/assoc_container.hpp&gt;
#include &lt;ext/pb_ds/tree_policy.hpp&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)             cerr&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;

//using namespace __gnu_pbds;
//typedef tree&lt;int, null_type, less&lt;int&gt;, rb_tree_tag, tree_order_statistics_node_update&gt; ordered_set;

/*----------------------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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

#define mx 200005
#define segment_tree int l=n*2,r=l+1,mid=(b+e)/2
pii info[mx];
vector&lt;int&gt;g[mx];
int cnt=0;
int maps[mx],ara[mx];

void dfs(int u, int par)
{
info[u].ff=++cnt;
maps[cnt]=u;
for(int i=0;i&lt;SZ(g[u]);i++)
{
int v=g[u][i];
if(v==par) continue;
dfs(v,u);
}
info[u].ss=cnt;
}

struct data
{
int val, prop;
};

data tree[3*mx];

void push_down(int n, int b, int e)
{
if(tree[n].prop%2)
{
segment_tree;
tree[l].val=(mid-b+1)-tree[l].val;
tree[l].prop++;
tree[r].val=(e-mid)-tree[r].val;
tree[r].prop++;
tree[n].prop=0;
}
}

void init(int n, int b, int e)
{
if(b==e)
{
tree[n].val=ara[maps[b]];
tree[n].prop=0;
return;
}
segment_tree;
init(l,b,mid);
init(r,mid+1,e);
tree[n].val=tree[l].val+tree[r].val;
}

void update(int n, int b, int e, int i, int j)
{
if(b&gt;j || e&lt;i) return;
if(b&gt;=i &amp;&amp; e&lt;=j)
{
tree[n].val=(e-b+1)-tree[n].val;
tree[n].prop++;
return;
}
push_down(n,b,e);
segment_tree;
update(l,b,mid,i,j);
update(r,mid+1,e,i,j);
tree[n].val=tree[l].val+tree[r].val;
}

int query(int n, int b, int e, int i, int j)
{
if(b&gt;j || e&lt;i) return 0;
if(b&gt;=i &amp;&amp; e&lt;=j) return tree[n].val;
push_down(n,b,e);
segment_tree;
int p=query(l,b,mid,i,j);
int q=query(r,mid+1,e,i,j);
return p+q;
}

char str[10];

int main()
{

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

int n;
sf(n);
for(int i=2;i&lt;=n;i++)
{
int a;
sf(a);
g[a].pb(i);
}

for(int i=1;i&lt;=n;i++) sf(ara[i]);
dfs(1,1);
init(1,1,n);
int q;
sf(q);
while(q--)
{
int a;
scanf(&quot; %s %d&quot;,str,&amp;a);
if(str[0]=='g')
{
int x=query(1,1,n,info[a].ff,info[a].ss);
printf(&quot;%d\n&quot;,x);
}
else
{
update(1,1,n,info[a].ff,info[a].ss);
}
}

return 0;
}

```

# Codeforce gym: 101484 F. No Link, Cut Tree!

0

Solution Idea:

My solution idea for this problem is at first make the given tree a full binary tree. A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node other than the leaves has two children. On the other hand A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. So we can convert a complete binary tree to a full binary tree by adding some dummy node whose cost is 0 so that they don’t contribute anything in the solution as well as they will help us to form our solution.

Now classify the nodes according to their level and make their cumulative sum or insert them in segment tree or in a binary indexed tree. We need this data structure for every level. Now map the nodes by the inorder traversal of the tree and position of the level array like 1,2,3….

Now here comes an important observation in a full binary tree at any level, if I want to delete a subtree starting at node u the will need to delete 1 node from level[u], 2 node from level[u]+1, 4 node from level[u]+2…2^i node form level[u]+i.

Now form the data structure (here I have used Binary Indexed Tree(BIT)) just query the contribution of the deleted node subtract them form the total contribution of that level and take the maximum. We need to do this from level[u] to last level and we need to consider the whole level from level 1 to level[u]-1.

```
#include &lt;bits/stdc++.h&gt;
#include &lt;ext/pb_ds/assoc_container.hpp&gt;
#include &lt;ext/pb_ds/tree_policy.hpp&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)             cerr&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;

//using namespace __gnu_pbds;
//typedef tree&lt;int, null_type, less&lt;int&gt;, rb_tree_tag, tree_order_statistics_node_update&gt; ordered_set;

/*----------------------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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

#define mx 200005

vector&lt;int&gt;g[mx];

vector&lt;int&gt;level_g[20];

int map1[mx],levell[mx];
int map2_level[mx],cost[mx],max_depth,disc;
int tree[20][mx];
int n,m;

void dfs1(int u, int level)
{
max_depth=max(max_depth,level);
for(int i=0;i&lt;SZ(g[u]);i++)
{
int v=g[u][i];
dfs1(v,level+1);
}
}

void dfs(int u, int par, int level)
{
levell[u]=level;
//    max_depth=max(max_depth,level);
map1[u]=++disc;
level_g[level].pb(u);

if(levell[u]!=max_depth)
{
if(SZ(g[u])==0)
g[u].pb(++n);
if(SZ(g[u])==1)
g[u].pb(++n);
}

for(int i=0;i&lt;SZ(g[u]);i++)
{
int v=g[u][i];
if(v==par) continue;
dfs(v,u,level+1);
}
}

void update(int id, int idx, int val)
{
for(int i=idx;i&lt;mx;i+=(i&amp;-i))
tree[id][i]+=val;
}

int query(int id, int idx)
{
int ret=0;
for(int i=idx;i&gt;0;i-=(i&amp;-i))
ret+=tree[id][i];
return ret;
}

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

sff(n,m);
sf(cost[1]);

for(int i=1;i&lt;n;i++)
{
int a,b,c;
sfff(a,b,c);
g[b].pb(a);
cost[a]=c;
}
dfs1(1,1);
//    int xx=n;
//    for(int i=1;i&lt;=xx;i++)
//    {
//        if(SZ(g[i])==1)
//            g[i].pb(++n);
//    }

dfs(1,1,1);

for(int i=1;i&lt;=max_depth;i++)
{
int num=0;
for(int j=0;j&lt;SZ(level_g[i]);j++)
{
int v=level_g[i][j];
map2_level[map1[v]]=++num;
update(i,num,cost[v]);
}
}

while(m--)
{
int u;
sf(u);
int ans=0;
for(int i=1;i&lt;levell[u];i++)
ans=max(ans,query(i,mx-1));
int last=levell[u],pp=0,cur=map1[u];
for(int i=levell[u];i&lt;=max_depth;i++)
{
int total=query(i,mx-1);
int sub=query(i,map2_level[cur]+(1&lt;&lt;pp)-1);
int sub1=query(i,map2_level[cur]-1);
total-=(sub-sub1);
ans=max(ans,total);
pp++;
cur++;
}

printf(&quot;%d\n&quot;,ans);

}

return 0;
}

```

# Codechef: Dividing Machine (DIVMAC)

0

Solution Idea:

In this problem, a tricky observation is the primary key of the solution. That observation is the input number is in range 1 to 10^6. So We can perform update operation on an index at most 20 times. Because if we divide a number of range 1 to 10^6 with it’s prime divisors we can divide them at most 20 times.

So in the update section of segment tree we can make a check whether the maximum least prime divisor of this sub tree is 1 or not. If it is a 1 then we don’t need to update in the sub tree. Otherwise we will explore this sub tree and update them.

After this observation this problem is a simple RMQ problem.

```
#include &lt;bits/stdc++.h&gt;
#include &lt;ext/pb_ds/assoc_container.hpp&gt;
#include &lt;ext/pb_ds/tree_policy.hpp&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)             cerr&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;

//using namespace __gnu_pbds;
//typedef tree&lt;int, null_type, less&lt;int&gt;, rb_tree_tag, tree_order_statistics_node_update&gt; ordered_set;

/*----------------------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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

#define mx 100005
#define segment_tree int l=n*2,r=l+1,mid=(b+e)/2
bitset&lt;mx/2&gt;vis;

vector&lt;int&gt;prime;

void sieve()
{
int x=mx/2,y=sqrt(mx)/2;
for(int i=1; i&lt;y; i++)
{
if(vis[i])
{
for(int j=i*(i+1)*2; j&lt;x; j+=(2*i+1))
vis[j]=1;
}
}
prime.pb(2);
for(int i=3; i&lt;mx; i+=2)
if(vis[i/2]==0) prime.pb(i);
}

deque&lt;int&gt;dq[mx];

int tree[3*mx];

void init(int n, int b, int e)
{
if(b==e)
{
tree[n]=dq[b].front();
return;
}
segment_tree;
init(l,b,mid);
init(r,mid+1,e);
tree[n]=max(tree[l],tree[r]);
}

void update(int n, int b, int e, int i, int j)
{
if(b&gt;j || e&lt;i) return;
if(tree[n]==1) return;
if(b==e)
{
dq[b].pop_front();
tree[n]=dq[b].front();
return;
}
segment_tree;
update(l,b,mid,i,j);
update(r,mid+1,e,i,j);
tree[n]=max(tree[l],tree[r]);
}

int query(int n, int b, int e, int i, int j)
{
if(b&gt;j || e&lt;i) return 0;
if(b&gt;=i &amp;&amp; e&lt;=j) return tree[n];
segment_tree;
int p=query(l,b,mid,i,j);
int q=query(r,mid+1,e,i,j);
return max(p,q);
}

int main()
{

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

sieve();

//    for(int i=0;i&lt;100;i++)
//        D(prime[i]);

int t;
sf(t);
TEST_CASE(t)
{
int n,m;
sff(n,m);
for(int i=1; i&lt;=n; i++)
{
int a;
sf(a);
int root=sqrt(a);
dq[i].clear();
for(int j=0;prime[j]&lt;=root;j++)
{
if(a%prime[j]==0)
{
while(a%prime[j]==0)
{
dq[i].pb(prime[j]);
a/=prime[j];
}
root=sqrt(a);
}
}
if(a&gt;1)
dq[i].pb(a);
dq[i].pb(1);
}

init(1,1,n);
bool check=0;
while(m--)
{
int a,b,c;
sfff(a,b,c);
if(a==0)
{
update(1,1,n,b,c);
}
else
{
if(check==0)
{
printf(&quot;%d&quot;,query(1,1,n,b,c));
check=1;
}
else
printf(&quot; %d&quot;,query(1,1,n,b,c));
}
}
printf(&quot;\n&quot;);
}

return 0;
}

```

# SPOJ KALTSUM – k Alternating Sum

0

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
/*------------------------------------------------*/

//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;
}

```

# Light OJ: 1348 – Aladdin and the Return Journey

0

```
#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
/*------------------------------------------------*/

//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));}
/*------------------------------------------------*/

inline void inp( int &amp;n )
{
n=0;
int ch=getchar_unlocked();
int sign=1;
while( ch &lt; '0' || ch &gt; '9' )
{
if(ch=='-')sign=-1;
ch=getchar_unlocked();
}
while(  ch &gt;= '0' &amp;&amp; ch &lt;= '9' )
n = (n&lt;&lt;3)+(n&lt;&lt;1) + ch-'0', ch=getchar_unlocked();
n=n*sign;
}

#define mx 30005

vector&lt;int&gt;g[mx];
int gini[mx];
int n;

int par[mx],level[mx], max_subtree[mx];
int sparse_par[mx][17];

int node_serial[mx],serial_node[mx];
int chain_no,indx;

ll tree[mx];

int dfs(int u, int from, int cnt)
{
sparse_par[u][0]=from;
level[u]=cnt;
int node=-1, maxi=0, total=1;
for(int i=0; i&lt;SZ(g[u]); i++)
{
int v=g[u][i];
if(v!=from)
{
int temp=dfs(v,u,cnt+1);
total+=temp;
if(temp&gt;maxi)
{
maxi=temp;
node=v;
}
}
}

max_subtree[u]=node;
}

void build_table(int n)
{
for(int j=1; 1&lt;&lt;j&lt;=n; j++)
{
for(int i=0; i&lt;n; i++)
{
sparse_par[i][j]=sparse_par[sparse_par[i][j-1]][j-1];
}
}
}

int LCA_query(int p, int q)
{
if(level[p]&lt;=level[q]) swap(p,q);
int log=log2(level[p]);

for(int i=log; i&gt;=0; i--)
{
if(level[p]-(1&lt;&lt;i)&gt;=level[q])
p=sparse_par[p][i];
}
if(p==q) return p;

for(int i=log; i&gt;=0; i--)
{
if(sparse_par[p][i]!=sparse_par[q][i])
{
p=sparse_par[p][i];
q=sparse_par[q][i];
}
}

return sparse_par[p][0];
}

void HLD(int u, int sz)
{
chain_indx[u]=chain_no;
chain_size[chain_no]=sz;

node_serial[u]=indx;
serial_node[indx]=u;
indx++;
if(max_subtree[u]==-1) return ;

HLD(max_subtree[u],sz+1);

for(int i=0; i&lt;SZ(g[u]); i++)
{
int v=g[u][i];
if(v!=sparse_par[u][0] &amp;&amp; v!=max_subtree[u])
{
chain_no++;
HLD(v,1);
}
}
}

void update(int idx, int val)
{
while(idx&lt;=indx)
{
tree[idx]+=val;
idx+=(idx&amp;-idx);
}
}

ll query(int a, int b)
{
ll ret=0;
ll ret2=0;
while(b)
{
ret+=tree[b];
b-=(b &amp; -b);
}
a--;
while(a)
{
ret2+=tree[a];
a-=(a&amp;-a);
}
return ret-ret2;
}

ll query_tree(int a, int b)
{
ll ret=0;
while(chain_indx[a]!=chain_indx[b])
{
}
ret+=query(node_serial[b],node_serial[a]);
return ret;
}

void update_tree(int a, int val)
{
update(node_serial[a],gini[a]*-1);
update(node_serial[a],val);
gini[a]=val;
}

inline void allclear(int n)
{
chain_no=1;
indx=1;
for(int i=0; i&lt;=n; i++)
{
g[i].clear();
}

ms(tree,0);
}

int main()
{

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

int t;
scanf(&quot;%d&quot;,&amp;t);
TEST_CASE(t)
{
scanf(&quot;%d&quot;,&amp;n);
for(int i=0; i&lt;n; i++) scanf(&quot;%d&quot;,&amp;gini[i]);
allclear(n+2);
for(int i=1; i&lt;n; i++)
{
int a,b;
scanf(&quot;%d %d&quot;,&amp;a,&amp;b);
g[a].pb(b);
g[b].pb(a);
}

dfs(0,0,1);
build_table(n);
HLD(0,1);

for(int i=1; i&lt;indx; i++)
{
update(i,gini[serial_node[i]]);
}

int m;
scanf(&quot;%d&quot;,&amp;m);
LINE_PRINT_CASE;
while(m--)
{
int a,b;
scanf(&quot;%d&quot;,&amp;a);
if(a==0)
{
scanf(&quot;%d %d&quot;,&amp;a,&amp;b);
int lca=LCA_query(a,b);
ll ans=query_tree(a,lca);
ans+=query_tree(b,lca);
ans-=gini[lca];
printf(&quot;%lld\n&quot;,ans);
}
else
{
scanf(&quot;%d %d&quot;,&amp;a,&amp;b);
update_tree(a,b);
}
}
}

return 0;
}

```