Monday, December 10, 2018

[toph.co] Distinct Dishting!!

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : Distinct Dishting!!
Source            : toph.co
Category          : Data Structure
Algorithm         : MO's Algorithm with Update
Verdict           : Accepted
#include "bits/stdc++.h"
 
using namespace std;
 
#define FI              freopen("in.txt", "r", stdin)
#define FO              freopen("out.txt", "w", stdout)
#define FAST            ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
 
#define FOR(i, n)       for (int i = 1; i <= n; i++)
#define For(i, n)       for (int i = 0; i < n; i++)
#define ROF(i, n)       for (int i = n; i >= 1; i--)
#define Rof(i, n)       for (int i = n-1; i >= 0; i--)
#define FORI(i, n)      for (auto i : n)
 
#define ll              long long
#define ull             unsigned long long
#define vi              vector <int>
#define vl              vector <ll>
#define pii             pair <int, int>
#define pll             pair <ll, ll>
#define mk              make_pair
#define ff              first
#define ss              second
#define eb              emplace_back
#define em              emplace
#define pb              push_back
#define ppb             pop_back
#define All(a)          a.begin(), a.end()
#define memo(a, b)      memset(a, b, sizeof a)
#define Sort(a)         sort(All(a))
#define ED(a)           Sort(a), a.erase(unique(All(a)), a.end())
#define rev(a)          reverse(All(a))
//#define sz(a)           (int)a.size()
#define max3(a, b, c)   max(a, max(b, c))
#define min3(a, b, c)   min(a, min(b, c))
#define maxAll(a)       *max_element(All(a))
#define minAll(a)       *min_element(All(a))
#define allUpper(a)     transform(All(a), a.begin(), :: toupper)
#define allLower(a)     transform(All(a), a.begin(), :: tolower)
#define endl            '\n'
#define nl              puts("")
#define ub              upper_bound
#define lb              lower_bound
#define Exp             exp(1.0)
#define PIE             2*acos(0.0)
#define Sin(a)          sin(((a)*PIE)/180.0)
#define EPS             1e-9
 
// int dr[] = {1, -1, 0, 0}; // 4 Direction
// int dc[] = {0, 0, 1, -1};
// int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction
// int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};
// int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves
// int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};
 
inline int setbit(int mask, int pos)        { return mask |= (1 << pos); }
inline int resetbit(int mask, int pos)      { return mask &= ~(1 << pos); }
inline int togglebit(int mask, int pos)     { return mask ^= (1 << pos); }
inline bool checkbit(int mask, int pos)     { return (bool)(mask & (1 << pos)); }
 
#define popcount(mask)                       __builtin_popcount(mask) // count set bit
#define popcountLL(mask)                     __builtin_popcountll(mask) // for long long
 
inline int read()                           { int a; scanf("%d", &a); return a; }
inline ll readLL()                          { ll a; scanf("%lld", &a); return a; }
inline double readDD()                      { double a; scanf("%lf", &a); return a; }
 
template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
int toInt(string s)                          { int num; istringstream iss(s); iss >> num; return num;  }
 
static const int maxn = 311111;
static const int maxb = 320;
 
struct mo
{
      int l, r, id, t;
      mo(int l = 0, int r = 0, int id = 0, int t = 0) :
            l(l), r(r), id(id), t(t) {}
      friend bool operator<(mo A, mo B)
      {
            int AL = A.l / maxb;
            int BL = B.l / maxb;
            int AR = A.r / maxb;
            int BR = B.r / maxb;
            if (AL == BL)
            {
                  if (AR == BR) return A.t < B.t;
                  else return AR < BR;
            }
            return AL < BL;
      }
} queries[maxn];
 
struct node
{
      int pos, pre, now;
      node(int pos = 0, int pre = 0, int now = 0) :
            pos(pos), pre(pre), now(now) {}
} U[maxn];
 
int arr[maxn], last[maxn];
int freq[maxn];
ll sum, ans[maxn];
 
map <int, int> mapper;
int rmapper[maxn];
int temp[maxn];
int l, r, t;
 
inline void add(int pos)
{
      int num = arr[pos];
      freq[num]++;
      if (freq[num] == 1 && rmapper[num] % 3 == 0) sum += rmapper[num];
}
 
inline void remov(int pos)
{
      int num = arr[pos];
      freq[num]--;
      if (freq[num] == 0 && rmapper[num] % 3 == 0) sum -= rmapper[num];
}
 
 
inline void apply(int pos, int val)
{
      if (l <= pos && pos <= r)
      {
            remov(pos);
            arr[pos] = val;
            add(pos);
      }
      else
      {
            arr[pos] = val;
      }
}
 
 
int main()
{
//      freopen("in.txt", "r", stdin);
 
      int tnum = read();
      int tquery = read();
      for (int i = 0; i < tnum; i++)
      {
            arr[i] = read();
            last[i] = arr[i];
            temp[i] = arr[i];
      }
      sort(temp, temp+tnum);
      int id = 0;
      for (int i = 0; i < tnum; i++)
      {
            int val = temp[i];
            if (mapper.find(val) == mapper.end())
            {
                  id++;
                  mapper[val] = id;
                  rmapper[id] = val;
            }
      }
      for (int i = 0; i < tnum; i++)
      {
            last[i] = arr[i] = mapper[arr[i]];
      }
      int u = 0;
      int q = 0;
      for (int i = 0; i < tquery; i++)
      {
            int type = read();
            if (type == 0)      // update
            {
                  int pos = read();
                  int value = read();
                  pos--;
                  int tempValue;
                  if (mapper.find(value) == mapper.end())
                  {
                        id++;
                        mapper[value] = id;
                        tempValue = id;
                        rmapper[id] = value;
                  }
                  tempValue = mapper[value];
                  U[++u] = {pos, last[pos], tempValue};
                  last[pos] = tempValue;
            }
            else
            {
                  int a = read();
                  int b = read();
                  if (a > b) swap(a, b);
                  a--;
                  b--;
                  queries[q] = {a, b, q, u};
                  q++;
            }
      }
      sort(queries, queries+q);
      l = 0;
      r = -1;
      t = 0;
      sum = 0;
      for (int i = 0; i < q; i++)
      {
            int L = queries[i].l;
            int R = queries[i].r;
            int T = queries[i].t;
            while (t < T)
            {
                  t++;
                  apply(U[t].pos, U[t].now);
            }
            while (t > T)
            {
                  apply(U[t].pos, U[t].pre);
                  t--;
            }
            while (l > L)          add(--l);
            while (r < R)          add(++r);
            while (l < L)          remov(l++);
            while (r > R)          remov(r--);
            ans[queries[i].id] = sum;
      }
      for (int i = 0; i < q; i++) printf("%lld\n", ans[i]);
}
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.