회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
2
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
2042번 - 구간 합 구하기
jhnah917
C++14
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int inf = 1e9+7; int arr[1010101]; struct Node{ Node *l, *r, *p; ll v, sz, sum; bool dummy; Node(ll _v, Node *p) : v(_v), p(p) { l = r = nullptr; sum = _v; sz = 1, dummy = 0; } Node(int _v) : Node(_v, nullptr) {} Node() : Node(0) {} ~Node(){ if(l) delete l; if(r) delete r; } }; struct SplayTree{ Node *root; Node *ptr[1010101]; //node pointer void update(Node *x){ x->sz = 1; x->sum = x->v; if(x->l){ x->sz += x->l->sz; x->sum += x->l->sum; } if(x->r){ x->sz += x->r->sz; x->sum += x->r->sum; } } Node* gather(int s, int e){ //gather [s, e] kth(e+1); auto tmp = root; kth(s-1); splay(tmp, root); return root->r->l; } void rotate(Node *x){ auto p = x->p; Node *y; if(x == p->l){ p->l = y = x->r; x->r = p; }else{ p->r = y = x->l; x->l = p; } x->p = p->p; p->p = x; if(y) y->p = p; if(x->p){ if(p == x->p->l) x->p->l = x; else x->p->r = x; } else root = x; update(p); update(x); } void splay(Node *x, Node *g = nullptr){ Node *y; while(x->p != g){ Node *p = x->p; if(p->p == g){ rotate(x); break; } auto pp = p->p; if((p->l == x) == (pp->l == p)){ rotate(p); rotate(x); }else{ rotate(x); rotate(x); } } if(!g) root = x; } SplayTree() : root() { memset(ptr, 0, sizeof ptr); } ~SplayTree(){ if(root) delete root; } void init(int n){ if(root) delete root; root = new Node(-inf); //left dummy node auto now = root; for(int i=1; i<=n; i++){ ptr[i] = now->r = new Node(arr[i], now); now = now->r; } now->r = new Node(inf, now); //right dummy node root->dummy = now->r->dummy = 1; for(int i=n; i>=1; i--) update(ptr[i]); splay(ptr[n/2]); } void kth(int k){ //1-based auto now = root; while(1){ while(now->l && now->l->sz > k){ now = now->l; } if(now->l) k -= now->l->sz; if(!k) break; k--; now = now->r; } splay(now); } }tree; int n, m, q; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; m += q; for(int i=1; i<=n; i++) cin >> arr[i]; tree.init(n); while(m--){ int op, a, b; cin >> op >> a >> b; if(op == 1){ tree.kth(a); tree.root->v = b; tree.update(tree.root); }else{ tree.gather(a, b); cout << tree.root->r->l->sum << "\n"; } } }
결과
메모리
시간
코드 길이
틀렸습니다
3093