Submission #77714
ソースコード
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 | #include <bits/stdc++.h> using namespace std; class segtree{ long long sz; long long start = INT_MAX; //!! vector< long long >seg; public : void maketree( long long n, long long x){ //make tree start = x; sz = 1; while (sz < n)sz <<= 1; seg.assign((sz*2)-1,start); } public : virtual long long f( long long x, long long y){ return min(x,y);} public : void update( long long a, long long b){ //one point update a += sz-1; seg[a] = b; while (a > 0){ a =(a-1)>>1; seg[a] = f(seg[(2*a)+1],seg[(2*a)+2]); //!! } } public : void add( long long a, long long b){ //one point update a += sz-1; seg[a] += b; while (a > 0){ a =(a-1)>>1; seg[a] = f(seg[(2*a)+1],seg[(2*a)+2]); //!! } } public : long long rmq( long long a, long long b, long long k, long long l, long long r){ //kukantansaku if (a >= r||l >= b) return start; if (a <= l&&b >= r) return seg[k]; return f(rmq(a,b,(k*2)+1,l,(l+r)>>1),rmq(a,b,(k*2)+2,(l+r)>>1,r)); //!! } public : long long rmq( long long a, long long b){ //short cut return rmq(a,b,0,0,sz); } }; class minseg : public segtree{ public : minseg( long long n){maketree(n,INT_MAX);} long long f( long long x, long long y) override { return min(x,y); }; }; class maxseg : public segtree{ public : maxseg( long long n){maketree(n,0);} long long f( long long x, long long y) override { return max(x,y); }; }; class sumseg : public segtree{ public : sumseg( long long n){maketree(n,0);} long long f( long long x, long long y) override { return x+y; }; }; int main(){ #define int long long int n; cin >>n; minseg tree(n); //セグ木の作成 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 |
投稿日時 | 2024-04-11 17:53:11 |
言語 | C++17 |
状態 | Accepted |
得点 | 10 |
ソースコード長 | 2531 Byte |
最大実行時間 | 53 ms |
最大メモリ使用量 | 2796 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 10 / 10 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
Input01 | AC | 28 ms | 604 KB |
1
|
Input02 | AC | 16 ms | 696 KB |
1
|
Input03 | AC | 24 ms | 536 KB |
1
|
Input04 | AC | 20 ms | 632 KB |
1
|
Input05 | AC | 19 ms | 472 KB |
1
|
Input06 | AC | 20 ms | 568 KB |
1
|
Input07 | AC | 27 ms | 536 KB |
1
|
Input08 | AC | 25 ms | 624 KB |
1
|
Input09 | AC | 28 ms | 812 KB |
1
|
Input10 | AC | 53 ms | 2796 KB |
1
|