Submission #1797466
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_N = 100005;
//永続化遅延処理型平衡二分探索木
const int max_size = 27000000;
template <typename T> class RBST {
public:
struct node{
T val, lazy, al;
int st_size; // 部分木のサイズ
node* left; node* right;
bool lazy_flag;
node(){};
node(T v) : val(v), al(v), left(nullptr), right(nullptr), st_size(1), lazy(0), lazy_flag(false){}
~node() { delete left; delete right; }
};
using pnn = pair<node*,node*>;
node* memo; //永続配列
int pool_size;
node* root;
T id1,id2; //opr1,opr2の単位元
//クエリに答える演算(sumに対応)
T opr1(T l, T r) {
return l + r;
}
//遅延処理の演算(addに対応)
T opr2(T l, T r) {
return l + r;
}
RBST(){
memo = new node[max_size];
id1 = id2 = 0;
}
//永続化
node* fix(node* t){
if(!t) return t;
if(pool_size >= max_size) assert(false);
memo[pool_size] = *t;
return &memo[pool_size++];
}
int size(node* t) { return t ? t->st_size : 0; }
T que(node* t) { return t ? t->al + t->lazy*size(t) : id1; }
//遅延伝播
node* push(node* t){
if(!t) return t;
t = fix(t);
if(t->lazy_flag){
if(t->left){
t->left = fix(t->left);
t->left->lazy_flag = true;
t->left->lazy = opr2(t->left->lazy, t->lazy);
}
if(t->right){
t->right = fix(t->right);
t->right->lazy_flag = true;
t->right->lazy = opr2(t->right->lazy,t->lazy);
}
t->val = opr2(t->val, t->lazy);
t->al = opr1(que(t->left), opr1(t->val, que(t->right)));
t->lazy = id2;
t->lazy_flag = false;
}
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 y = 2463534242;
y = y ^ (y << 13); y = y ^ (y >> 17);
return y = y ^ (y << 5);
}
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_size = 0;
node *res = nullptr;
for (int i = 0; i < node_size; i++) {
memo[pool_size] = node(x[i]);
res = merge(res, &memo[pool_size++]);
}
root = res;
}
void up(node *t) {
if (!t) 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){
auto sr = split(root, r);
auto sl = split(sr.first, l);
node* lr = sl.second;
lr->lazy = val;
lr->lazy_flag = true;
root = merge(merge(sl.first,sl.second),sr.second);
}
//[r,s)を[p,q)にコピー
void trans(int p, int q, int r, int s){
auto sq = split(root,q);
auto sp = split(sq.first,p);
auto ss = split(root,s);
auto sr = split(ss.first,r);
root = merge(merge(sp.first,sr.second),sq.second);
}
T query(int l,int r){
auto sr = split(root,r);
auto sl = split(sr.first,l);
T res = que(sl.second);
return res;
}
};
int main()
{
int n,q;
scanf("%d%d",&n,&q);
RBST<ll> rb;
rb.x.resize(n);
rep(i,n){
cin >> rb.x[i];
}
rb.build();
rep(i,q){
int a;
scanf("%d",&a);
if(a == 1){
int b,c,d;
scanf("%d%d%d",&b,&c,&d);
rb.add(b-1,c,d);
}else if(a == 2){
int b,c,d,e;
scanf("%d%d%d%d",&b,&c,&d,&e);
rb.trans(b-1,c,d-1,e);
}else{
int b,c;
scanf("%d%d",&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 |
100 |
Code Size |
6309 Byte |
Status |
AC |
Exec Time |
948 ms |
Memory |
765056 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:189:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&q);
^
./Main.cpp:198:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a);
^
./Main.cpp:201:37: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&b,&c,&d);
^
./Main.cpp:205:42: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d",&b,&c,&d,&e);
^
./Main.cpp:209:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunus...
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
100 / 100 |
Status |
|
|
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 |
AC |
948 ms |
762880 KB |
subtask1_largerandom_t1_02.txt |
AC |
943 ms |
764416 KB |
subtask1_largerandom_t2_03.txt |
AC |
845 ms |
764800 KB |
subtask1_largerandom_t2_04.txt |
AC |
842 ms |
764032 KB |
subtask1_largespecial01.txt |
AC |
580 ms |
756480 KB |
subtask1_largespecial02.txt |
AC |
703 ms |
753920 KB |
subtask1_largespecial03.txt |
AC |
379 ms |
658816 KB |
subtask1_largespecial04.txt |
AC |
486 ms |
765056 KB |
subtask1_largespecial05.txt |
AC |
471 ms |
763904 KB |
subtask1_largespecial06.txt |
AC |
76 ms |
30336 KB |
subtask1_smallrandom_01.txt |
AC |
1 ms |
512 KB |
subtask1_smallrandom_02.txt |
AC |
1 ms |
512 KB |
subtask1_smallrandom_03.txt |
AC |
2 ms |
2432 KB |
subtask1_smallrandom_04.txt |
AC |
1 ms |
512 KB |
subtask1_smallrandom_05.txt |
AC |
1 ms |
512 KB |
subtask1_smallrandom_06.txt |
AC |
1 ms |
512 KB |
subtask1_smallrandom_07.txt |
AC |
1 ms |
512 KB |
subtask1_smallrandom_08.txt |
AC |
1 ms |
512 KB |
subtask1_smallrandom_09.txt |
AC |
1 ms |
512 KB |
subtask1_smallrandom_10.txt |
AC |
1 ms |
512 KB |