Submission #47518
ソースコード
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | #include <iostream> #include <vector> #include <string> #include <stack> #include <queue> #include <set> #include <map> #include <unordered_map> #include <algorithm> #include <functional> #include <numeric> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <cassert> #define bogo_sort std::sort #define bozo_sort std::stable_sort #define alles(obj) obj.begin(), obj.end() #define elif else if #define unless(flg) if(!(flg)) #define elless(flg) else if(!(flg)) #define echo std::cout << #define read std::cin >> #define endl std::endl #define fin << '\n' #define bash push_back #define makePair std::make_pair #define _ << ' ' << // type-define #define Stack std::stack #define Queue std::queue #define Set std::set #define PQueue std::priority_queue #define Vector std::vector #define Pair std::pair #define Map std::map #define HashMap std::unordered_map #define Greater std::greater using String = std::string; using llong = long long ; using boolean = bool ; using Pii = Pair< int , int >; using Vi = Vector< int >; using Vii = Vector<Pii>; // utils constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; constexpr int INF = 0x3f3f3f3f; constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL; namespace { template < class A, class B> A power ( llong x, llong n, llong mod ) { llong ans = 1; while ( n > 0 ) { if ( n & 1 ) ans = ( ans * x ) % mod; x = ( x * x ) % mod; n >>= 1; } return ans; } template < class A, class B> A power ( A x, B n ) { return power( x, n, 1000000007 ); } template < class A> A gcd ( A x, A y ) { return x % y ? gcd( y, x % y ) : y; } template < class A, class B> A lcm ( A x, B y ) { return ( x / gcd(x, y) * y ); } template < class A> inline A abs ( A n ) { return ( n < 0 ) ? -n : n; } template < class A, class B> inline bool chmax ( A &a, const B &b ) { return b > a ? a = b, true : false ; } template < class A, class B> inline bool chmin ( A &a, const B &b ) { return b < a ? a = b, true : false ; } boolean isMovable ( int x, int y, int w, int h ) { return ( x >= 0 && y >= 0 && x < w && y < h ); } } struct SegMin { int size; Vi vi; SegMin ( int n ) { for ( size = 1; size < n; size *= 2 ); vi.resize( 2*size-1, 0 ); } void update ( int pos, int value ) { pos += size-1; vi[pos] = value; while ( pos > 0 ) { pos = ( pos - 1 ) / 2; vi[pos] = std::min ( vi[pos*2+1], vi[pos*2+2] ); } } int getMin ( int begin, int end, int pos=0, int left=0, int right=-1 ) { if ( right == -1 ) right = size; if ( right <= begin || end <= left ) return INF; if ( begin <= left && right <= end ) return vi[pos]; int next = ( left + right ) / 2; int resultLeft = getMin ( begin, end, pos*2+1, left, next ); int resultRight = getMin ( begin, end, pos*2+2, next, right ); return std::min( resultRight, resultLeft ); } }; struct SegMax { int size; Vi vi; SegMax ( int n ) { for ( size = 1; size < n; size *= 2 ); vi.resize( 2*size-1, 0 ); } void update ( int pos, int value ) { pos += size-1; vi[pos] = value; while ( pos > 0 ) { pos = ( pos - 1 ) / 2; vi[pos] = std::max ( vi[pos*2+1], vi[pos*2+2] ); } } int getMax ( int begin, int end, int pos=0, int left=0, int right=-1 ) { if ( right == -1 ) right = size; if ( right <= begin || end <= left ) return -INF; if ( begin <= left && right <= end ) return vi[pos]; int next = ( left + right ) / 2; int resultLeft = getMax ( begin, end, pos*2+1, left, next ); int resultRight = getMax ( begin, end, pos*2+2, next, right ); return std::max( resultRight, resultLeft ); } }; struct SegSum { int size; Vi vi; SegSum ( int n ) { for ( size = 1; size < n; size *= 2 ); vi.resize( 2*size-1, 0 ); } void update ( int pos, int value ) { pos += size-1; vi[pos] = value; while ( pos > 0 ) { pos = ( pos - 1 ) / 2; vi[pos] = vi[pos*2+1] + vi[pos*2+2]; } } int getSum ( int begin, int end, int pos=0, int left=0, int right=-1 ) { if ( right == -1 ) right = size; if ( right <= begin || end <= left ) return 0; if ( begin <= left && right <= end ) return vi[pos]; int next = ( left + right ) / 2; int resultLeft = getSum ( begin, end, pos*2+1, left, next ); int resultRight = getSum ( begin, end, pos*2+2, next, right ); return resultRight + resultLeft; } }; namespace Rlyeh { signed call_of_Cthulhu( signed datum ) { int n, m; read n >> m; SegMin min(n); SegMax max(n); SegSum sum(n); for ( int i = 0; i < m; i++ ) { String s; read s; if ( s == "update" ) { int pos, value; read pos >> value; min.update(pos, value); max.update(pos, value); sum.update(pos, value); } elif ( s == "add" ) { int pos, value; read pos >> value; min.update(pos, value+min.getMin(pos, pos+1)); max.update(pos, value+max.getMax(pos, pos+1)); sum.update(pos, value+sum.getSum(pos, pos+1)); } elif ( s == "getMin" ) { int begin, end; read begin >> end; echo min.getMin(begin, end) fin; } elif ( s == "getMax" ) { int begin, end; read begin >> end; echo max.getMax(begin, end) fin; } elif ( s == "getSum" ) { int begin, end; read begin >> end; echo sum.getSum(begin, end) fin; } } return 0; } } signed main(){std::cin.tie(0); std::ios::sync_with_stdio( false ); int main_result = Rlyeh::call_of_Cthulhu(114514); return 0;} |
ステータス
項目 | データ |
---|---|
問題 | 1072 - セグメントツリー技術基礎 |
ユーザー名 | r1825 |
投稿日時 | 2019-02-14 20:34:19 |
言語 | C++14 |
状態 | Accepted |
得点 | 1 |
ソースコード長 | 6599 Byte |
最大実行時間 | 68 ms |
最大メモリ使用量 | 9192 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 1 / 1 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in01 | AC | 26 ms | 604 KB |
1
|
in02 | AC | 45 ms | 940 KB |
1
|
in03 | AC | 39 ms | 3860 KB |
1
|
in04 | AC | 56 ms | 4048 KB |
1
|
in05 | AC | 34 ms | 4112 KB |
1
|
in06 | AC | 39 ms | 2768 KB |
1
|
in07 | AC | 45 ms | 2964 KB |
1
|
in08 | AC | 68 ms | 2512 KB |
1
|
in09 | AC | 53 ms | 4880 KB |
1
|
in10 | AC | 65 ms | 3792 KB |
1
|
in11 | AC | 26 ms | 2960 KB |
1
|
in12 | AC | 32 ms | 2388 KB |
1
|
in13 | AC | 52 ms | 5688 KB |
1
|
in14 | AC | 65 ms | 3824 KB |
1
|
in15 | AC | 46 ms | 6192 KB |
1
|
in16 | AC | 51 ms | 3700 KB |
1
|
in17 | AC | 60 ms | 6704 KB |
1
|
in18 | AC | 35 ms | 6644 KB |
1
|
in19 | AC | 51 ms | 7088 KB |
1
|
in20 | AC | 37 ms | 7024 KB |
1
|
in21 | AC | 40 ms | 5684 KB |
1
|
in22 | AC | 64 ms | 5876 KB |
1
|
in23 | AC | 47 ms | 7600 KB |
1
|
in24 | AC | 50 ms | 6380 KB |
1
|
in25 | AC | 60 ms | 5800 KB |
1
|
in26 | AC | 65 ms | 8300 KB |
1
|
in27 | AC | 59 ms | 8496 KB |
1
|
in28 | AC | 62 ms | 7280 KB |
1
|
in29 | AC | 34 ms | 8876 KB |
1
|
in30 | AC | 56 ms | 9192 KB |
1
|
in31 | AC | 41 ms | 7720 KB |
1
|
in32 | AC | 58 ms | 7916 KB |
1
|