Problem p02630 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef double D;
typedef long long int LL;
typedef long double LD;
#define OR ||
#define AND &&
#define nl '\\n'
#define S string
#define inf INT_MAX
#define SQR(a) (a) * (a)
#define pb push_back
#define GCD(a, b) __gcd(a, b)
#define PI 2.0 * acos(0.0)
#define LCM(a, b) (a * b) / GCD(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
#define srtv(v) sort(v.begin(), v.end())
#define T                                                                      \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i >= b; i--)
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define inout                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)

#define si(x) scanf("%d", &x)
#define pi(x) printf("%d", x)
#define ss(str) scanf("%s", str)
#define pl(x) printf("%lld", x)
#define sl(x) scanf("%lld", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
using namespace std;

/*

bool bs(LL l,LL r)
{
    int mid,res=-1;

    while(l<=r)
    {
        mid=l+(r-l)/2;



    }

    return res;
}


*/

//--------------------------code starts
//here---------------------------------------
/*
LL sum[400002];
LL num[100002];

//call with buildSegemntTree(1,1,n)
void buildSegmentTree(int at,int L,int R)
{
    sum[at]=0;

    if(L==R)
    {
        sum[at]=num[L];

        return;
    }

    int mid=(L+R)/2;

    buildSegmentTree(at*2,L,mid);
    buildSegmentTree(at*2+1,mid+1,R);


    sum[at]=sum[at*2]+sum[at*2+1];
}


void updateSegemntTreeWithNew(int at,int L,int R,int pos,LL value)
{
    if(pos>R || pos<L)
    {
        return;
    }

    if(L>=pos  && R<=pos)
    {
        sum[at]=value;
        return;
    }


    int mid=(L+R)/2;

    updateSegemntTreeWithNew(at*2,L,mid,pos,value);

    updateSegemntTreeWithNew(at*2+1,mid+1,R,pos,value);

    sum[at]=sum[at*2]+sum[at*2+1];
}


//call with querySegmentTree(1,1,n,l,r)

LL querySegmentTree(int at,int L,int R,int l,int r)
{
    if(r<L  || R<l)
        return 0;

    if(l<=L  && R<=r) return sum[at];

    int mid=(L+R)/2;

    LL x=querySegmentTree(at*2,L,mid,l,r);
    LL y=querySegmentTree(at*2+1,mid+1,R,l,r);

    return x+y;
}*/
vector<LL> adj[100005];

int main() {
  int n;
  si(n);
  LL sum = 0, x, y, q;
  Rep(i, 1, n) {
    sl(x);
    sum += x;
    adj[x].pb(i);
  }

  // buildSegmentTree(1,1,n);

  sl(q);

  Rep(i, 1, q) {
    sll(x, y);

    if (adj[x].size() == 0) {

    } else {
      for (int j = 0; j < adj[x].size(); j++) {
        adj[y].pb(adj[x][j]);
        sum -= x;
        sum += y;
      }
    }

    adj[x].clear();

    cout << sum << nl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef double D;
typedef long long int LL;
typedef long double LD;
#define OR ||
#define AND &&
#define nl '\\n'
#define S string
#define inf INT_MAX
#define SQR(a) (a) * (a)
#define pb push_back
#define GCD(a, b) __gcd(a, b)
#define PI 2.0 * acos(0.0)
#define LCM(a, b) (a * b) / GCD(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
#define srtv(v) sort(v.begin(), v.end())
#define T                                                                      \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i >= b; i--)
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define inout                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)

#define si(x) scanf("%d", &x)
#define pi(x) printf("%d", x)
#define ss(str) scanf("%s", str)
#define pl(x) printf("%lld", x)
#define sl(x) scanf("%lld", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
using namespace std;

/*

bool bs(LL l,LL r)
{
    int mid,res=-1;

    while(l<=r)
    {
        mid=l+(r-l)/2;



    }

    return res;
}


*/

//--------------------------code starts
//here---------------------------------------

LL sum[400002];
LL num[100002];

// call with buildSegemntTree(1,1,n)
void buildSegmentTree(int at, int L, int R) {
  sum[at] = 0;

  if (L == R) {
    sum[at] = num[L];

    return;
  }

  int mid = (L + R) / 2;

  buildSegmentTree(at * 2, L, mid);
  buildSegmentTree(at * 2 + 1, mid + 1, R);

  sum[at] = sum[at * 2] + sum[at * 2 + 1];
}

void updateSegemntTreeWithNew(int at, int L, int R, int pos, LL value) {
  if (pos > R || pos < L) {
    return;
  }

  if (L >= pos && R <= pos) {
    sum[at] = value;
    return;
  }

  int mid = (L + R) / 2;

  updateSegemntTreeWithNew(at * 2, L, mid, pos, value);

  updateSegemntTreeWithNew(at * 2 + 1, mid + 1, R, pos, value);

  sum[at] = sum[at * 2] + sum[at * 2 + 1];
}

// call with querySegmentTree(1,1,n,l,r)

LL querySegmentTree(int at, int L, int R, int l, int r) {
  if (r < L || R < l)
    return 0;

  if (l <= L && R <= r)
    return sum[at];

  int mid = (L + R) / 2;

  LL x = querySegmentTree(at * 2, L, mid, l, r);
  LL y = querySegmentTree(at * 2 + 1, mid + 1, R, l, r);

  return x + y;
}
vector<LL> adj[100005];

int main() {
  int n;
  si(n);
  Rep(i, 1, n) {
    sl(num[i]);
    adj[num[i]].pb(i);
  }

  buildSegmentTree(1, 1, n);

  LL q, x, y;
  sl(q);

  Rep(i, 1, q) {
    sll(x, y);

    if (adj[x].size() == 0) {

    } else {
      for (int j = 0; j < adj[x].size(); j++) {
        adj[y].pb(adj[x][j]);
        updateSegemntTreeWithNew(1, 1, n, adj[x][j], y);
      }
    }

    adj[x].clear();

    printf("%lld\\n", querySegmentTree(1, 1, n, 1, n));
  }

  return 0;
}