結果

問題 No.833 かっこいい電車
ユーザー ChanyuhChanyuh
提出日時 2020-02-14 11:09:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 3,370 bytes
コンパイル時間 1,292 ms
コンパイル使用メモリ 115,936 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-16 00:12:25
合計ジャッジ時間 4,713 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
6,912 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 89 ms
7,040 KB
testcase_11 AC 126 ms
9,728 KB
testcase_12 AC 34 ms
5,888 KB
testcase_13 AC 24 ms
5,376 KB
testcase_14 AC 94 ms
9,728 KB
testcase_15 AC 45 ms
6,912 KB
testcase_16 AC 34 ms
7,808 KB
testcase_17 AC 33 ms
5,376 KB
testcase_18 AC 109 ms
7,424 KB
testcase_19 AC 33 ms
7,168 KB
testcase_20 AC 9 ms
5,376 KB
testcase_21 AC 86 ms
5,376 KB
testcase_22 AC 60 ms
10,112 KB
testcase_23 AC 39 ms
7,424 KB
testcase_24 AC 61 ms
9,856 KB
testcase_25 AC 98 ms
6,784 KB
testcase_26 AC 43 ms
8,704 KB
testcase_27 AC 71 ms
7,168 KB
testcase_28 AC 49 ms
5,376 KB
testcase_29 AC 60 ms
6,656 KB
testcase_30 AC 64 ms
10,752 KB
testcase_31 AC 103 ms
7,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;//二項演算
  T ti;//単位元
  vector<T> dat;

  SegmentTree(){}
  SegmentTree(F f,T ti):f(f),ti(ti){}

  void init(int n_){//sizeがn_のsegtreeを作る
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }

  void build(const vector<T> &v){//vによってsegtreeをbuildする
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }

  void set_val(int k,T x){//k番目をxにする
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }

  T query(int a,int b){//区間[a,b)に対しFを適応した値を返す
    if(a>=b) return ti;
    T vl=ti,vr=ti;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }

  template<typename C>
  int find(int st,C &check,T &acc,int k,int l,int r){//
    if(l+1==r){
      acc=f(acc,dat[k]);
      return check(acc)?k-n:-1;
    }
    int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l&&!check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }

  template<typename C>
  int find(int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }
};


void solve(){
    int n,q;
    vector<ll> A;
    cin >> n >> q;
    A.resize(n,0);
    rep(i,n){
        cin >> A[i];
    }
    auto f=[](ll a,ll b){return a+b;};
    SegmentTree<ll> seg(f,0);
    seg.build(A);
    set<int> se;
    rep(i,n+1){
        se.insert(i);
    }
    rep(k,q){
        int query,x;
        cin >> query >> x;x--;
        if(query==1){
            if(se.find(x+1)!=se.end()){
                se.erase(x+1);
            }
        }
        if(query==2){
            if(se.find(x+1)==se.end()){
                se.insert(x+1);
            }
        }
        if(query==3){
            seg.set_val(x,seg.query(x,x+1)+1);
        }
        if(query==4){
            auto r=se.upper_bound(x);auto l=r;l--;
            //cout << *l << " " << *r << endl;
            cout << seg.query(*l,*r) << endl;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0