Submission #47404
ソースコード
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 | #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, INF ); } 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, -INF ); } 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; read n; SegSum segSum(n); for ( int i = 1; i <= n; i++ ) { int a; read a; segSum.update(i, a); } echo 0; for ( int i = 1; i <= n; i++ ) { echo ' ' << segSum.getSum ( 0, i+1 ); } echo endl; int q; read q; for ( int i = 0; i < q; i++ ) { int a, b; read a >> b; echo segSum.getSum(a, b+1) 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;} |
ステータス
項目 | データ |
---|---|
問題 | 0733 - プログラミング入門:累積和 |
ユーザー名 | r1825 |
投稿日時 | 2019-02-13 21:03:03 |
言語 | C++14 |
状態 | Accepted |
得点 | 5 |
ソースコード長 | 5833 Byte |
最大実行時間 | 769 ms |
最大メモリ使用量 | 108908 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 5 / 5 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
Input01 | AC | 266 ms | 20184 KB |
1
|
Input02 | AC | 257 ms | 31904 KB |
1
|
Input03 | AC | 291 ms | 45152 KB |
1
|
Input04 | AC | 746 ms | 77092 KB |
1
|
Input05 | AC | 769 ms | 108908 KB |
1
|