Submission #673979

# Submission time Handle Problem Language Result Execution time Memory
673979 2022-12-22T13:32:51 Z Sam_a17 Garaža (COCI17_garaza) C++17
160 / 160
1137 ms 40192 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount
 
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// for random generations
// mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
mt19937 myrand(373);
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if(str == "input") {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  } else {
    // freopen("skis.in", "r", stdin);
    // freopen("skis.out", "w", stdout);
  }
}
// Indexed Set  
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 3e5 + 10;
int n, q, a[N];

struct Node {
  vector<pair<int, long long>> L, R;
  long long ans = 0;

  void init(int curr) {
    L.clear();
    R.clear();

    L.emplace_back(curr, 1);
    R.emplace_back(curr, 1);
    ans = (curr > 1);
  }
};

Node empt;

struct segTree {  // Range Queries
  // 0 indexed
  // [l: r + 1]

  // for 1 index
  // [l - 1: r]

  vector<Node> mTree;
  int size;

  Node combine (Node a, Node b) {
    if(a.L.empty()) {
      return b;
    }  
    
    if(b.L.empty()) {
      return a;
    }

    Node c;
    
    //
    c.L = a.L;
    int last = c.L.back().first;
    for(int i = 0; i < sz(b.L); i++) {
      int j = i + 1, g = __gcd(b.L[i].first, last);
      int new_size = b.L[i].second;
      while(j < sz(b.L) && __gcd(b.L[j].first, g) == g) {
        new_size += b.L[j].second;
        j++;
      }
      
      if(g == c.L.back().first) {
        c.L.back().second += new_size;
      } else {
        c.L.emplace_back(g, new_size);
      }

      i = j - 1;
    }


    //
    c.R = b.R;
    last = c.R.back().first;
    for(int i = 0; i < sz(a.R); i++) {
      int j = i + 1, g = __gcd(a.R[i].first, last);
      int new_size = a.R[i].second;
      while(j < sz(a.R) && __gcd(a.R[j].first, g) == g) {
        new_size += a.R[j].second;
        j++;
      }
      
      if(g == c.R.back().first) {
        c.R.back().second += new_size;
      } else {
        c.R.emplace_back(g, new_size);
      }

      i = j - 1;
    }

    //
    c.ans = a.ans + b.ans;
    for(int i = 0; i < sz(a.R); i++) {
      int curr_gcd = a.R[i].first;
      for(int j = 0; j < sz(b.L); j++) {
        curr_gcd = __gcd(b.L[j].first, curr_gcd);
        if(curr_gcd > 1) {
          c.ans += a.R[i].second * b.L[j].second;
        } else {
          break;
        }
      }
    }

    return c;
  }

  void build(int x, int lx, int rx) {
    if(rx - lx == 1) {
      if(lx >= 1) {
        mTree[x].init(a[lx]);
      }
    } else {
      int mid = (lx + rx) / 2;
      build(2 * x + 1, lx, mid);
      build(2 * x + 2, mid, rx);
      mTree[x] = combine(mTree[2 * x + 1], mTree[2 * x + 2]);
    }
  }

  void init(long long n) {
    size = 1;
    while(size < n)  {
      size *= 2;
    }
    mTree.resize(2 * size - 1);
    build(0, 0, size);
  }

  void upd(int u, long long v, int x, int lx, int rx) { // set value at pos u
    if(rx - lx == 1) {
      mTree[x].init(v);
      return;
    }

    int m = (lx + rx) / 2;
    if(u < m) {
      upd(u, v, 2 * x + 1, lx, m);
    } else {
      upd(u, v, 2 * x + 2, m, rx);
    }
    mTree[x] = combine(mTree[2 * x + 1], mTree[2 * x + 2]);
  }

  void upd(int u, long long v) {
    upd(u, v, 0, 0, size);
  }

  Node qry(int l, int r, int x, int lx, int rx) { // range queries
    if(l >= rx || lx >= r) {
      return empt;
    }
    if(lx >= l && r >= rx) {
      return mTree[x];
    }

    int m = (rx + lx) / 2;
    Node s1 = qry(l, r, 2 * x + 1, lx, m);
    Node s2 = qry(l, r, 2 * x + 2, m, rx);
    return combine(s1, s2);
  }

  Node qry(int l, int r) {
    return qry(l, r, 0,0,size);
  }
};

segTree seg;

void solve_() {
  cin >> n >> q;

  for(int i = 1; i <= n; i++) {
    cin >> a[i];
    // seg.upd(i, a[i]);
  }

  seg.init(n + 1);

  while(q--) {
    int type; cin >> type;
    if(type == 1) {
      int x, v; cin >> x >> v;
      seg.upd(x, v);
    } else {
      int l, r; cin >> l >> r;
      cout << seg.qry(l, r + 1).ans << '\n';
    }
  }

}
 
int main() {
  setIO();
 
  auto solve = [&](int test_case)-> void {
    for(int i = 1; i <= test_case; i++) {
      // cout << "Case #" << i << ": ";
      solve_();
    }
  };
 
  int test_cases = 1;
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
} 

Compilation message

garaza.cpp: In function 'void setIO(std::string)':
garaza.cpp:69:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
garaza.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 596 KB Output is correct
2 Correct 20 ms 1492 KB Output is correct
3 Correct 33 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 9420 KB Output is correct
2 Correct 282 ms 10144 KB Output is correct
3 Correct 235 ms 9548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 503 ms 18892 KB Output is correct
2 Correct 612 ms 18728 KB Output is correct
3 Correct 478 ms 18580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1137 ms 39304 KB Output is correct
2 Correct 1107 ms 40192 KB Output is correct
3 Correct 866 ms 38416 KB Output is correct