第一次提交的時候TLE,最後改成C輸入輸出成功AC,耗時156ms。
通過嘗試用不同格式的代碼提交之後,發現一個問題,效能影響:輸出 > 輸入 > 字串比較(這裡更多的是char* 和 string的效能差異)。所以在細節上進行效能最佳化的時候,應該優先考慮輸出、輸入。
演算法思路:(兩種方法)
<1>考察樹狀數組,區間資訊的維護和查詢方法。
<2>線段樹
樹狀數組代碼:
//模板開始#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 toString(T x){ostringstream sout; sout<<x; return sout.str();}typedef long 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//模板結束(通用部分)#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 敵兵布陣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%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;}
線段樹代碼:
//模板開始#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<class T> string toString(T x){ostringstream sout; sout<<x; return sout.str();}typedef long 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//模板結束(通用部分)#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", a) == 1 && strcmp(a, "End") != 0){//cin>>b>>c;scanf("%d%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;}