第一次提交的时候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 50005
int 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 2000200
int 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);
#endif
char 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;
}