結果

問題 No.876 Range Compress Query
ユーザー Jiro_tech15Jiro_tech15
提出日時 2020-02-05 22:54:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,189 bytes
コンパイル時間 885 ms
コンパイル使用メモリ 102,192 KB
最終ジャッジ日時 2024-11-14 22:06:11
合計ジャッジ時間 1,565 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:73:13: error: 'function' does not name a type; did you mean 'union'?
   73 |   using F = function< Monoid(Monoid, Monoid) >;
      |             ^~~~~~~~
      |             union
main.cpp:78:9: error: 'F' does not name a type
   78 |   const F f;
      |         ^
main.cpp:82:28: error: 'F' does not name a type
   82 |   SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
      |                            ^
main.cpp: In constructor 'SegmentTree<Monoid>::SegmentTree(int, int, const Monoid&)':
main.cpp:82:53: error: class 'SegmentTree<Monoid>' does not have any field named 'f'
   82 |   SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
      |                                                     ^
main.cpp: In function 'int main()':
main.cpp:176:26: error: invalid user-defined conversion from 'main()::<lambda(long long int, long long int)>' to 'int' [-fpermissive]
  176 |   SegmentTree<ll> seg(n, [](ll a, ll b){return a + b;}, 0);
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:176:26: note: candidate is: 'constexpr main()::<lambda(long long int, long long int)>::operator long long int (*)(long long int, long long int)() const' (near match)
  176 |   SegmentTree<ll> seg(n, [](ll a, ll b){return a + b;}, 0);
      |                          ^
main.cpp:176:26: note:   no known conversion from 'long long int (*)(long long int, long long int)' to 'int'
main.cpp:82:30: note:   initializing argument 2 of 'SegmentTree<Monoid>::SegmentTree(int, int, const Monoid&) [with Monoid = long long int]'
   82 |   SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
      |                      ~~~~~~~~^
main.cpp: In instantiation of 'void SegmentTree<Monoid>::build() [with Monoid = long long int]':
main.cpp:196:12:   required from here
main.cpp:94:17: error: 'f' was not declared in this scope
   94 |       seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
      |                ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
using namespace std;
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define MOD (long long int)(998244353)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

ll mpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
  else return mpow(a,b-1) * a % MOD;
}
ll lpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = lpow(a,b/2); return memo*memo;}
  else return lpow(a,b-1) * a;
}
ll gcd(ll a, ll b){
  if(b==0) return a;
  else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
  if(kaijo_memo.size() > n) return kaijo_memo[n];
  if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
  while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
  return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n){
  if(gyaku_kaijo_memo.size() > n) return gyaku_kaijo_memo[n];
  if(gyaku_kaijo_memo.size() == 0) gyaku_kaijo_memo.push_back(1);
  while(gyaku_kaijo_memo.size() <= n) gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size()-1] * mpow(gyaku_kaijo_memo.size(), MOD-2) % MOD);
  return gyaku_kaijo_memo[n];
}

ll nCr(ll n, ll r){
  if(n == r) return 1;//0個の丸と-1個の棒みたいな時に時に効く?不安.
  if(n < r || r < 0) return 0;
  ll ret = 1;
  ret *= kaijo(n); ret %= MOD;
  ret *= gyaku_kaijo(r); ret %= MOD;
  ret *= gyaku_kaijo(n-r); ret %= MOD;
  return ret;
}

template< typename Monoid >
struct SegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;

  int sz;
  vector< Monoid > seg;

  const F f;
  const Monoid M1;

  //SegmentTree<int> seg(N, [](int a, int b) { return min(a, b); }, INT_MAX);
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) {
    seg[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const {
    return seg[k + sz];
  }

  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }


  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, seg[1]))) return find_subtree(1, check, L, false);
      return -1;
    }
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, seg[a]);
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }

  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(seg[1], R))) return find_subtree(1, check, R, true);
      return -1;
    }
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(seg[--b], R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

ll *B;


int main(void){
  fast_io
  cout<<fixed<<setprecision(15);

  ll n,q;cin>>n>>q;
  SegmentTree<ll> seg(n, [](ll a, ll b){return a + b;}, 0);
  ll A[n];
  ll B_[n+2];
  B = B_ + 1;
  rep(i,n){
    cin>>A[i];
  }
  rep(i,n-1){
    B[i] = A[i+1] - A[i];
  }
  B[-1] = -1;
  B[n-1] = -1;
  rep(i,n-1){
    if(B[i] == 0){
      seg.set(i, 0);
    }else{
      seg.set(i, 1);
    }
  }

  seg.build();

  rep(i,q){
    ll t,l,r;
    cin>>t>>l>>r;l--;r--;
    if(t == 1){
      ll x;cin>>x;
      B[l-1] += x;
      B[r] -= x;
      if(B[l-1] == 0){
        if(l-1 >= 0)
           seg.update(l-1, 0);
      }else{
        if(l-1 >= 0){
          seg.update(l-1, 1);
        }
      }
      if(r == n-1) continue;
      if(B[r] == 0){
        seg.update(r, 0);
      }else{
        seg.update(r, 1);
      }
    }else{
      cout<<seg.query(l,r)+1<<endl;
    }
  }

  return 0;
}
0