結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー tjaketjake
提出日時 2019-09-07 13:49:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 685 ms / 5,000 ms
コード長 5,355 bytes
コンパイル時間 1,038 ms
コンパイル使用メモリ 90,332 KB
実行使用メモリ 18,744 KB
最終ジャッジ日時 2023-10-24 02:39:44
合計ジャッジ時間 15,422 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,676 KB
testcase_01 AC 4 ms
5,788 KB
testcase_02 AC 5 ms
5,732 KB
testcase_03 AC 5 ms
5,732 KB
testcase_04 AC 4 ms
5,732 KB
testcase_05 AC 4 ms
5,788 KB
testcase_06 AC 3 ms
5,732 KB
testcase_07 AC 4 ms
5,788 KB
testcase_08 AC 5 ms
5,788 KB
testcase_09 AC 5 ms
5,732 KB
testcase_10 AC 5 ms
5,788 KB
testcase_11 AC 575 ms
18,628 KB
testcase_12 AC 562 ms
18,628 KB
testcase_13 AC 403 ms
18,628 KB
testcase_14 AC 550 ms
18,700 KB
testcase_15 AC 586 ms
18,704 KB
testcase_16 AC 623 ms
18,716 KB
testcase_17 AC 685 ms
18,744 KB
testcase_18 AC 676 ms
18,744 KB
testcase_19 AC 400 ms
18,700 KB
testcase_20 AC 406 ms
18,740 KB
testcase_21 AC 415 ms
18,680 KB
testcase_22 AC 405 ms
18,720 KB
testcase_23 AC 425 ms
18,684 KB
testcase_24 AC 364 ms
18,700 KB
testcase_25 AC 375 ms
18,740 KB
testcase_26 AC 382 ms
18,680 KB
testcase_27 AC 364 ms
18,720 KB
testcase_28 AC 380 ms
18,684 KB
testcase_29 AC 550 ms
18,700 KB
testcase_30 AC 588 ms
18,704 KB
testcase_31 AC 620 ms
18,716 KB
testcase_32 AC 193 ms
18,692 KB
testcase_33 AC 325 ms
18,700 KB
testcase_34 AC 339 ms
18,712 KB
testcase_35 AC 319 ms
18,704 KB
testcase_36 AC 322 ms
18,724 KB
testcase_37 AC 330 ms
18,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE ll
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

#define N (1 << 17)

class SegmentTree {
  static const ll inf = 1e18;
  static ll gcd(ll m, ll n) {
    ll r = m % n;
    return r ? gcd(n, r) : n;
  }

  struct Node {
    ll max_v, min_v;
    ll lazy, cnt, sum, lcm;
    bool has, lcm_inf;

    void init(ll x) {
      lcm = max_v = min_v = sum = x;
      cnt = 1;
    }
    void init_empty() {
      max_v = -inf; min_v = inf;
      lcm = 1;
      sum = cnt = 0;
    }

    inline void update_value(ll x) {
      lcm = lazy = x;
      sum = x * cnt;
      max_v = min_v = x;
      has = true;
    }

    inline void propagate(Node &left, Node &right) {
      if(!has) return;
      left.update_value(lazy);
      right.update_value(lazy);
      has = false;
    }

    inline void build(Node &left, Node &right) {
      max_v = max(left.max_v, right.max_v);
      min_v = min(left.min_v, right.min_v);
      sum = left.sum + right.sum;
      cnt = left.cnt + right.cnt;
      if(left.lcm_inf || right.lcm_inf) {
        lcm_inf = true;
      } else if(__builtin_smulll_overflow(left.lcm / gcd(left.lcm, right.lcm), right.lcm, &lcm)) {
        lcm_inf = true;
      }
    }
  } nds[2*N];

  int n0, n;
  void _set(ll x, int a, int b, int k, int l, int r) {
    Node &nd = nds[k];
    if(k < n0-1) nd.propagate(nds[2*k+1], nds[2*k+2]);
    if(b <= l || r <= a) {
      return;
    }
    if(a <= l && r <= b) {
      nd.update_value(x);
      return;
    }
    _set(x, a, b, 2*k+1, l, (l+r)/2);
    _set(x, a, b, 2*k+2, (l+r)/2, r);
    if(k < n0-1) nd.build(nds[2*k+1], nds[2*k+2]);
  }

  void _gcd(ll x, int a, int b, int k, int l, int r) {
    Node &nd = nds[k];
    if(k < n0-1) nd.propagate(nds[2*k+1], nds[2*k+2]);
    if(b <= l || r <= a || (!nd.lcm_inf && x % nd.lcm == 0)) {
      return;
    }
    if(a <= l && r <= b) {
      if(nd.max_v == nd.min_v) {
        nd.update_value(gcd(x, nd.max_v));
        return;
      }
    }
    _gcd(x, a, b, 2*k+1, l, (l+r)/2);
    _gcd(x, a, b, 2*k+2, (l+r)/2, r);
    if(k < n0-1) nd.build(nds[2*k+1], nds[2*k+2]);
  }

  ll _query_sum(int a, int b, int k, int l, int r) {
    Node &nd = nds[k];
    if(k < n0-1) nd.propagate(nds[2*k+1], nds[2*k+2]);
    if(b <= l || r <= a) {
      return 0;
    }
    if(a <= l && r <= b) {
      return nd.sum;
    }
    ll lv = _query_sum(a, b, 2*k+1, l, (l+r)/2);
    ll rv = _query_sum(a, b, 2*k+2, (l+r)/2, r);
    if(k < n0-1) nd.build(nds[2*k+1], nds[2*k+2]);
    return lv + rv;
  }

  ll _query_max(int a, int b, int k, int l, int r) {
    Node &nd = nds[k];
    if(k < n0-1) nd.propagate(nds[2*k+1], nds[2*k+2]);
    if(b <= l || r <= a) {
      return -inf;
    }
    if(a <= l && r <= b) {
      return nd.max_v;
    }
    ll lv = _query_max(a, b, 2*k+1, l, (l+r)/2);
    ll rv = _query_max(a, b, 2*k+2, (l+r)/2, r);
    if(k < n0-1) nd.build(nds[2*k+1], nds[2*k+2]);
    return max(lv, rv);
  }

public:
  void init(int n, ll *a) {
    n0 = 1;
    while(n0 < n) n0 <<= 1;
    for(int i=0; i<n; ++i) nds[i+n0-1].init(a[i]);
    for(int i=n; i<n0; ++i) nds[i+n0-1].init_empty();

    for(int i=0; i<2*n0-1; ++i) nds[i].lazy = 0, nds[i].has = false;
    for(int i=n0-2; i>=0; --i) {
      nds[i].build(nds[2*i+1], nds[2*i+2]);
    }
  }

  void set(int l, int r, ll x) {
    _set(x, l, r, 0, 0, n0);
  }

  void gcd(int l, int r, ll x) {
    _gcd(x, l, r, 0, 0, n0);
  }

  ll query_sum(int l, int r) {
    return _query_sum(l, r, 0, 0, n0);
  }

  ll query_max(int l, int r) {
    return _query_max(l, r, 0, 0, n0);
  }
};

ll a[N];
SegmentTree st; //(n, a);

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n) cin >> a[i];
  st.init(n, a);
  while(q--) {
    int t, l, r; ll x;
    cin >> t >> l >> r;
    switch(t) {
      case 1:
        cin >> x;
        st.set(l-1, r, x);
        break;
      case 2:
        cin >> x;
        st.gcd(l-1, r, x);
        break;
      case 3:
        cout << st.query_max(l-1, r) << endl;
        break;
      case 4:
        cout << st.query_sum(l-1, r) << endl;
        break;
    }
  }
  return 0;
}
0