Submission #76931
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include <bits/stdc++.h> #define ll long long #define ldo long double #define INF INT_MAX #define endl "\n" using namespace std; struct segtree{ int sz; int start; int m; vector< int >seg; vector< int >size; segtree( int n, int s){ sz = 1; m = s; if (m == 0){ while (sz < n)sz <<= 1; seg.assign((sz*2)-1,start); } if (m == 1){ seg.assign(n+1,-1); size.resize(n+1,1); } if (m == 2){ while (sz < n)sz <<= 1; seg.assign((sz*2)-1,start); size.assign((sz*2)-1,0); } } void eval( int k, int l, int r){ //遅延評価 if (size[k] != 0){ seg[k] += size[k]; if (r-1 > 1){ size[2*k+1] += size[k]/2; //条件によって更新式を変更 size[2*k+2] += size[k]/2; } size[k] = 0; } } void query( int a, int b, long long x, int k = 0, int l = 0, int r = -1){ if (r < 0)r = sz; eval(k,l,r); if (b <= l || r <= a){ size[k] += (r-1)*x; eval(k,l,r); } else { query(a,b,x,2*k+1,l,(l+r)/2); query(a,b,x,2*k+1,(l+r)/2,r); seg[k] = seg[2*k+1] + seg[2*k+2]; //条件 } } void update( int a, int b){ a += sz-1; seg[a] = b; while (a > 0){ a =(a-1)>>1; seg[a] = min(seg[(2*a)+1],seg[(2*a)+2]); } } int rmq( int a, int b, int k, int l, int r){ if (a >= r||l >= b) return INF; if (m == 2)eval(k,l,r); if (a <= l&&b >= r) return seg[k]; return min(rmq(a,b,(k*2)+1,l,(l+r)>>1),rmq(a,b,(k*2)+2,(l+r)>>1,r)); } int rmq( int a, int b){ return rmq(a,b,0,0,sz); } int root( int x){ while (1){ if (seg[x] == -1){ return x; } x = seg[x]; } } void unite( int u, int v){ int RootU = root(u), RootV = root(v); if (RootU == RootV) return ; if (size[RootU] < size[RootV]) { seg[RootU] = RootV; size[RootV] += size[RootU]; } else { seg[RootV] = RootU; size[RootU] += size[RootV]; } return ; } }; int main(){ int n; cin >>n; segtree tree(n,0); //セグ木の作成 for ( int i = 0;i < n;i++){ int x; cin >>x; tree.update(i,x); //値の更新(i番目をxに設定) } queue< int >ans; while (1){ string s; int x,y; cin >>s; if (s == "min" ){ //min値の出力 cin >>x>>y; ans.push(tree.rmq(x,y+1)); //min値の出力はxは含む、yは含まず。 } else if (s == "update" ){ //値の更新 cin >>x>>y; tree.update(x,y); } else { break ; } } queue< int >debug = ans; while (!debug.empty()){ cout <<debug.front()<<endl; debug.pop(); } return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0459 - セグメントツリー練習 |
ユーザー名 | ei2332 |
投稿日時 | 2023-10-15 23:46:30 |
言語 | C++17 |
状態 | Accepted |
得点 | 10 |
ソースコード長 | 3124 Byte |
最大実行時間 | 44 ms |
最大メモリ使用量 | 1644 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 10 / 10 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
Input01 | AC | 33 ms | 604 KB |
1
|
Input02 | AC | 26 ms | 572 KB |
1
|
Input03 | AC | 20 ms | 540 KB |
1
|
Input04 | AC | 20 ms | 636 KB |
1
|
Input05 | AC | 19 ms | 612 KB |
1
|
Input06 | AC | 20 ms | 452 KB |
1
|
Input07 | AC | 22 ms | 548 KB |
1
|
Input08 | AC | 30 ms | 512 KB |
1
|
Input09 | AC | 28 ms | 720 KB |
1
|
Input10 | AC | 44 ms | 1644 KB |
1
|