At the first submission, TLE finally changed to the C Input and Output command, which took 156 Ms.
After you try to submit code in different formats, you will find a problem. performance impact: output> input> string comparison (here more is the performance difference between char * and string ). Therefore, output and input should be given priority when optimizing performance in detail.
Algorithm logic: (two methods)
<1> evaluate the maintenance and query methods of tree arrays and interval information.
<2> line segment tree
Tree array code:
// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # include <string. h> # define SZ (x) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t ;} template <class T> string TOST Ring (t x) {ostringstream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B) ;}# define ifs CIN // template end (General Part) # define n 50005int C [N]; int A [n]; int lowbit (int x) {return X & (-x);} int sum (int x) {int ret = 0; while (x> 0) {RET + = C [X]; X-= lowbit (x);} return ret;} void add (int x, int D, int N) {While (x <= N) {C [x] + = D; x + = lowbit (x) ;}} void sub (int x, int D, int N) {While (x <= N) {C [x]-= D; x + = lowbit (x) ;}// hdoj 1166 enemy deployment int main () {// ifstream ifs ("shuju.txt", IOS: In); int N; int m; int data; int OP1, OP2; char S1 [10]; // ifs> m; scanf ("% d", & M); For (INT I = 0; I <m; I ++) {// cout <"case" <I + 1 <":" <Endl; printf ("case % d: \ n", I + 1 ); // ifs> N; scanf ("% d", & N); memset (C, 0, sizeof (c); memset (A, 0, sizeof (a); For (Int J = 1; j <= N; j ++) {// ifs> data; scanf ("% d ", & data); A [J] = data; add (J, Data, n) ;}while (scanf ("% s", S1 )) {If (S1 [0] = 'E') {break;} // ifs> OP1> OP2; scanf ("% d", & OP1, & OP2); If (S1 [0] = 'q') {// cout <sum (OP2)-sum (OP1-1) <Endl; printf ("% d \ n", sum (OP2)-sum (OP1-1);} else if (S1 [0] = 'A ') {A [OP1] + = OP2; add (OP1, OP2, n);} else if (S1 [0] ='s ') {A [OP1]-= OP2; sub (OP1, OP2, n) ;}} return 0 ;}
Line Segment Tree Code:
// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # include <queue> # include <string. h> # define SZ (x) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t;} template <CL Ass T> string tostring (t x) {ostringstream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B) ;}# define local // template end (General Part) # define maxn 2000200int sum [maxn]; int Ql, Qr; int P, v; int N; int zhishu; int query (int o, int L, int R) {int M = L + (R-l)/2, ANS = 0; if (QL <= L & R <= QR) {return sum [O];} If (QL <= m) {ans + = query (O * 2, l, m);} If (M <QR) {ans + = query (O * 2 + 1, m + 1, R);} return ans;} void update1 (int o, int L, int R) {int M = L + (R-l)/2; If (L = r) {sum [O] + = V;} else {If (P <= m) {update1 (O * 2, l, m);} else {update1 (O * 2 + 1, m + 1, R );} sum [O] = sum [O * 2] + sum [O * 2 + 1] ;}} void update2 (int o, int L, int R) {int M = L + (R-l)/2; If (L = r) {sum [O]-= V;} else {If (P <= m) {update2 (O * 2, l, m);} else {update2 (O * 2 + 1, m + 1, R );} sum [O] = sum [O * 2] + sum [O * 2 + 1] ;}} void Init () {zhishu = 0; // CIN> N; scanf ("% d", & N); While (1 <zhishu) <n) {zhishu ++;} int up_max = (1 <(Zhishu + 1)-1; int chuzhi = 1 <(zhishu); For (INT I = chuzhi; I <= chuzhi + n-1; I ++) {// CIN> sum [I]; scanf ("% d", & sum [I]) ;}for (INT I = chuzhi + N; I <= up_max; I ++) {sum [I] = 0 ;}for (INT I = (1 <zhishu)-1; I >= 1; I --) {sum [I] = sum [I * 2] + sum [I * 2 + 1] ;}} int main () {# ifdef local // freopen ("shuju.txt ", "r", stdin); # endifchar A [10]; int B, C; int t; // CIN> T; scanf ("% d", & T ); for (int tt = 1; TT <= T; TT ++) {cout <"case" <TT <":" <Endl; Init (); While (scanf ("% s",) = 1 & strcmp (a, "end ")! = 0) {// CIN> B> C; scanf ("% d", & B, & C); If (! Strcmp (a, "query") {QL = B; QR = C; cout <query (1, 1, 1 <zhishu) <Endl;} If (! Strcmp (a, "add") {P = B; V = C; update1 (1, 1, 1 <zhishu) ;}if (! Strcmp (a, "sub") {P = B; V = C; update2 (1, 1, 1 <zhishu) ;}} return 0 ;}