Submission #1755593


Source Code Expand

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int max_size = 3e7;

template <typename T> class RBST
{
public:
    struct node{
        T val, lazy, al;
        int st_size;   // 部分木のサイズ
        node* left; node* right;
        node(){};
        node(T v) : val(v), al(v), left(nullptr), right(nullptr), st_size(1), lazy(0){}
        ~node() { delete left; delete right; }
    };
    using pnn = pair<node*,node*>;
    node* pool;
    int pool_size;
    node* root;
    T id = 0, id2 = 0;
    T merge(T l, T r) {
    	return l + r;
    }
    T merge2(T l, T r) {
    	return l + r;
    }
    //永続化
    node* fix(node* t){
        if(!t)  return t;
        if(pool_size >= max_size) assert(false);
        pool[pool_size] = *t;
        return &pool[pool_size++];
    }
    int size(node* t) { return t ? t->st_size : 0; }
    T que(node* t) { return t ? t->al + t->lazy*size(t) : id; }
    //遅延伝播
    node* push(node* t){
        if(!t) return t;
        t = fix(t);
        if(t->left){
            t->left = fix(t->left);
    		t->left->lazy = merge2(t->left->lazy, t->lazy);
        }
        if(t->right){
            t->right = fix(t->right);
            t->right->lazy = merge2(t->right->lazy,t->lazy);
        }
        t->val = merge2(t->val, t->lazy);
        t->al = merge(que(t->left), merge(t->val, que(t->right)));
        t->lazy = id2;
        return t;
    }
    node* update(node *t){
        node* l = t->left; node* r = t->right;
        t->st_size = size(l) + size(r) + 1;
        t->al = que(l) + que(r) + t->val;
        return t;
    }
    unsigned rnd(){
        static unsigned x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    	unsigned t = x ^ (x << 11);
    	x = y; y = z; z = w;
    	return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
    }
    node* merge(node* l, node* r){
        if (!l || !r) return (!l) ? r : l;
        if(rnd() % (size(l) + size(r)) < (unsigned)size(l)){
            l = push(l);
            l->right = merge(l->right, r);
            return update(l);
        }else{
            r = push(r);
            r->left = merge(l, r->left);
            return update(r);
        }
    }
    pnn split(node* t, int k){   //木のサイズが(k,n-k)となるように分割する
        if(!t) return pnn(nullptr, nullptr);
        t = push(t);
        if(k <= size(t->left)){
            pnn s = split(t->left, k);
            t->left = s.second;
            return pnn(s.first,update(t));
        }else{
            pnn s = split(t->right, k-size(t->left)-1);
            t->right = s.first;
            return pnn(update(t), s.second);
        }
    }
    vector<T> x;    //再構築の際に用いる
    int node_size;
    int x_size;
    void build(){
        node_size = (int)x.size();
        pool = new node[max_size];
    	pool_size = 0;
    	node *res = nullptr;
    	for (int i = 0; i < node_size; i++) {
    		pool[pool_size] = node(x[i]);
    		res = merge(res, &pool[pool_size++]);
    	}
    	root = res;
    }
    void up(node *t) {
    	if (t == nullptr) return;
    	t = push(t);
    	up(t->left);
    	if(x_size >= node_size) return;
    	x[x_size++] = t->val;
    	up(t->right);
    }
    void rebuild(node* t) {
    	x_size = 0;
    	up(t);
        build();
    }
    void add(int l, int r, T val){
        node* t = root;
        auto sr = split(t, l);
        auto sl = split(sr.second, r-l);
        node* lr = sl.first;
        lr->lazy = val;
        root = merge(sr.first,merge(sl.first,sl.second));
    }
    void trans(int p, int q, int r, int s){
        node* t = root;
        auto sq = split(t,p);
        auto sp = split(sq.second,q-p);
        auto ss = split(t,r);
        auto sr = split(ss.second,s-r);
        root = merge(sq.first,merge(sr.first,sp.second));
    }
    T query(int l,int r){
        node* t = root;
        auto sr = split(t,l);
        auto sl = split(sr.second,r-l);
        T res = que(sl.first);
        root = t;
        return res;
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,q;
    cin >> n >> q;
    RBST<ll> rb;
    rb.x.resize(n);
    rep(i,n){
        cin >> rb.x[i];
    }
    rb.build();
    rep(i,q){
        int a;
        cin >> a;
        if(a == 1){
            int b,c,d;
            cin >> b >> c >> d;
            rb.add(b-1,c,d);
        }else if(a == 2){
            int b,c,d,e;
            cin >> b >> c >> d >> e;
            rb.trans(b-1,c,d-1,e);
        }else{
            int b,c;
            cin >> b >> c;
            cout << rb.query(b-1,c) << "\n";
        }
        if(rb.pool_size >= max_size / 2){
            rb.rebuild(rb.root);
        }
    }
    return 0;
}

Submission Info

Submission Time
Task D - グラフではない
User kopricky
Language C++14 (GCC 5.4.1)
Score 0
Code Size 5948 Byte
Status RE
Exec Time 1483 ms
Memory 2039936 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 13
MLE × 3
RE × 6
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask1_largerandom_t1_01.txt, subtask1_largerandom_t1_02.txt, subtask1_largerandom_t2_03.txt, subtask1_largerandom_t2_04.txt, subtask1_largespecial01.txt, subtask1_largespecial02.txt, subtask1_largespecial03.txt, subtask1_largespecial04.txt, subtask1_largespecial05.txt, subtask1_largespecial06.txt, subtask1_smallrandom_01.txt, subtask1_smallrandom_02.txt, subtask1_smallrandom_03.txt, subtask1_smallrandom_04.txt, subtask1_smallrandom_05.txt, subtask1_smallrandom_06.txt, subtask1_smallrandom_07.txt, subtask1_smallrandom_08.txt, subtask1_smallrandom_09.txt, subtask1_smallrandom_10.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 1 ms 256 KB
subtask0_sample_02.txt AC 1 ms 256 KB
subtask1_largerandom_t1_01.txt RE 1483 ms -2023552 KB
subtask1_largerandom_t1_02.txt RE 1474 ms -2024448 KB
subtask1_largerandom_t2_03.txt RE 1220 ms -2023040 KB
subtask1_largerandom_t2_04.txt RE 1212 ms -2023680 KB
subtask1_largespecial01.txt RE 1226 ms -2024704 KB
subtask1_largespecial02.txt RE 1286 ms -2025216 KB
subtask1_largespecial03.txt MLE 973 ms 2039936 KB
subtask1_largespecial04.txt MLE 707 ms 1430144 KB
subtask1_largespecial05.txt MLE 909 ms 1888896 KB
subtask1_largespecial06.txt AC 68 ms 26624 KB
subtask1_smallrandom_01.txt AC 2 ms 2560 KB
subtask1_smallrandom_02.txt AC 2 ms 640 KB
subtask1_smallrandom_03.txt AC 2 ms 2560 KB
subtask1_smallrandom_04.txt AC 2 ms 640 KB
subtask1_smallrandom_05.txt AC 2 ms 2432 KB
subtask1_smallrandom_06.txt AC 2 ms 640 KB
subtask1_smallrandom_07.txt AC 2 ms 640 KB
subtask1_smallrandom_08.txt AC 2 ms 640 KB
subtask1_smallrandom_09.txt AC 2 ms 640 KB
subtask1_smallrandom_10.txt AC 2 ms 640 KB