結果

問題 No.1000 Point Add and Array Add
ユーザー ytftytft
提出日時 2021-04-19 23:49:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 313 ms / 2,000 ms
コード長 2,451 bytes
コンパイル時間 5,438 ms
コンパイル使用メモリ 396,932 KB
実行使用メモリ 18,936 KB
最終ジャッジ日時 2023-09-17 08:33:43
合計ジャッジ時間 9,789 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,356 KB
testcase_04 AC 2 ms
4,356 KB
testcase_05 AC 1 ms
4,356 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,360 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,356 KB
testcase_12 AC 3 ms
4,356 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 4 ms
4,356 KB
testcase_15 AC 4 ms
4,356 KB
testcase_16 AC 228 ms
16,072 KB
testcase_17 AC 187 ms
11,964 KB
testcase_18 AC 313 ms
18,696 KB
testcase_19 AC 311 ms
18,936 KB
testcase_20 AC 282 ms
18,628 KB
testcase_21 AC 303 ms
18,716 KB
testcase_22 AC 307 ms
18,772 KB
testcase_23 AC 303 ms
18,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <boost/multiprecision/cpp_int.hpp>
namespace mp=boost::multiprecision;
template<typename type>
class segment_tree{
public:
    vector<type> tree;
    type unit;
    function<type(type,type)> fn;
    segment_tree(vector<type> a,function<type(type,type)> fn,type unit){//aは配列、fnはモノイドの演算、unitは単位元
        int n=0;
        long long N=a.size();
        while(N>(1<<n)){
            n++;
        }
        tree.resize((1<<(n+1))-1);//木の根が0,子が1,2,孫が3,4,5,6,...
        for(int i=0;i<N;++i){
            tree[(1<<n)-1+i]=a[i];
        }
        for(int i=N;i<(1<<n)-1;++i){
            tree[(1<<n)-1+i]=unit;
        }
        for(int i=n-1;i>=0;--i){
            for(int j=(1<<i)-1;j<(1<<(i+1))-1;++j){
                tree[j]=fn(tree[j*2+1],tree[j*2+2]);
            }
        }
        this->unit=unit;
        this->fn=fn;
    }
    type get(long long left,long long right){//区間の指定法は半開区間
        int n=0;
        type ans=unit;
        while((1<<n)<tree.size()){
            ++n;
        }
        for(int i=n-1;i>=0;--i){
            if(left==right){
                break;
            }
            if(left%2){
                ans=fn(ans,tree[((1<<i)-1)+left]);
            }
            left=(left+1)/2;
            if(right%2){
                ans=fn(ans,tree[((1<<i)-1)+right-1]);
            }
            right/=2;
        }
        return ans;
    }
    void update(long long index,type value){
        int n=0;
        while((1<<n)<tree.size()){
            ++n;
        }
        tree[(1<<(n-1))-1+index]=value;
        for(int i=n-2;i>=0;--i){
            index/=2;
            tree[(1<<i)-1+index]=fn(tree[((1<<i)-1+index)*2+1],tree[((1<<i)-1+index)*2+2]);
        }
    }
};


int main(){
    int N,Q;
    cin>>N>>Q;
    vector<char> c(Q+N,'A');
    vector<long long> x(Q+N),y(Q+N);
    for(int i=0;i!=N;++i){
        x[i]=i+1;
        cin>>y[i];
    }
    for(int i=N;i!=Q+N;++i){
        cin>>c[i]>>x[i]>>y[i];
    }
    vector<mp::cpp_int> b(N,0);
    segment_tree<int> t(vector<int>(N+1,0),[](int a,int b){return a+b;},0);
    for(int i=Q+N-1;i>=0;--i){
        if(c[i]=='B'){
            t.update(x[i]-1,t.get(x[i]-1,x[i])+1);

            t.update(y[i],t.get(y[i],y[i]+1)-1);
        }else{
            b[x[i]-1]+=t.get(0,x[i])*y[i];
        }
    }
    for(int i=0;i<N;i++){
        cout<<b[i]<<' ';
    }
}
0