Kick start round C 2020 solution

solution of Google Kick start Round C 2020
problem A
with explanation and code
solution

I need help with other problems

1 Like

i solved c …perfect subarray…
https://youtu.be/OdxUf9zaSrw

solution is correct passed small data case but got TLE in big test case.

Which one

#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <cstdio>
#include <numeric>
#include <vector>
#include <iterator> 
#include <map>
#include <set>
#include <climits>
#include <queue>
#include <iomanip>
#include <cmath>
#include <stack>
#include <cctype>
#include <bitset>
// #include <bits/stdc++.h>
#define int long long int
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define bp __builtin_popcount
#define pb push_back
using namespace std;//coded by abhijay mitra
const int N=2e5+1;
const int inf = /*0x3f3f3f3f*/1e15;
// const ll M = 998244353 ; // Mulo
const int M = 1e9+7 ; // Mulo
#define F first
#define S second
int n;
//building the tree
void build(int a[],int t[], int v=1, int tl=1, int tr=n) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(a,t, v*2, tl, tm);
        build(a,t, v*2+1, tm+1, tr);
        t[v] = t[v*2] + t[v*2+1];
    }
}
//queries
int sum(int l, int r,int t[],int v=1, int tl=1, int tr=n ) {
    if (l > r) 
        return 0;
    if (l == tl && r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return sum( l, min(r, tm),t,v*2, tl, tm)
           + sum(max(l, tm+1), r,t,v*2+1, tm+1, tr );
}
//
void update(int pos, int new_val,int t[],int v=1, int tl=1, int tr=n ) {
    if (tl == tr) {
        t[v] = new_val;
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(pos,new_val,t,v*2, tl, tm);
        else
            update(pos,new_val,t,v*2+1, tm+1, tr);
        t[v] = t[v*2] + t[v*2+1];
    }
}
int arr[5][N],a[N];char c;
int q,t[5][4*N];
void solve(){
  cin>>n>>q;
  memset(arr,0,sizeof arr);memset(a,0,sizeof a);memset(t,0,sizeof t);
  for(int i=1;i<=n;i++)cin>>a[i];
  for(int i=1;i<=n;i++){
    if(i&1)arr[1][i]=a[i];
    else arr[1][i]=-a[i];
    arr[2][i]=i*arr[1][i];
    arr[3][i]=-arr[1][i];
    arr[4][i]=-arr[2][i];
  }
  int ans=0;
  build(arr[1],t[1]);
  build(arr[2],t[2]);
  build(arr[3],t[3]);
  build(arr[4],t[4]);
  for(int i=1;i<=q;i++){
    cin>>c;
    if(c=='U'){
      int pos,val;
      cin>>pos>>val;
      if(pos&1){
        update(pos,val,t[1]);
        update(pos,val*pos,t[2]);
        update(pos,-val,t[3]);
        update(pos,-val*pos,t[4]);
      }
      else{
        update(pos,-val,t[1]);
        update(pos,-val*pos,t[2]);
        update(pos,val,t[3]);
        update(pos,val*pos,t[4]);
      }
    }
    else{
      int l,r;
      cin>>l>>r;
      if(l&1){
        // cout<<sum(l,r,t[2])-(l-1)*sum(l,r,t[1])<<"\n";
        ans+=sum(l,r,t[2])-(l-1)*sum(l,r,t[1]);
      }
      else{ 
        // cout<<sum(l,r,t[4])-(l-1)*sum(l,r,t[3])<<"\n";
      /*else */ans+=sum(l,r,t[4])-(l-1)*sum(l,r,t[3]);}
    }
  }
  cout<<ans;
}
int32_t main()
{
  ibs;cti;
  // solve(),cout<<"\n";
  int x=0;
  int t;cin>>t;while(t--){x++;cout<<"Case #"<<x<<": ";solve();cout<<"\n";}
  return 0;
}

solution to D

1 Like

thanks…

1 Like