标签:round 整数 sub err else output set 错误 tree
题目链接:https://vjudge.net/contest/182746#problem/B
敌兵布阵
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 117431 Accepted Submission(s): 49165
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int MAXN = 50000 + 100;
int n,a[MAXN];
struct tree
{
int l, r;
int s;
}trees[MAXN * 4];
void buildtree(int rs, int l, int r)
{
trees[rs].l = l;
trees[rs].r = r;
if (trees[rs].l == trees[rs].r)
{
trees[rs].s = a[l];
return;
}
int mid = (trees[rs].l + trees[rs].r) / 2;
buildtree(rs * 2, trees[rs].l, mid);
buildtree(rs * 2 + 1, mid + 1, trees[rs].r); //rs*2+1也可表示为 rs<<1|1
trees[rs].s = trees[rs * 2].s + trees[rs * 2 + 1].s;
}
void update(int rs, int ord, int goal)
{
if (trees[rs].l == ord && trees[rs].r == ord)
{
trees[rs].s = goal;
return;
}
int mid = (trees[rs].l + trees[rs].r) / 2;
if (ord <= mid)update(rs * 2, ord, goal);
else if (ord > mid)update(rs * 2 + 1, ord, goal);
trees[rs].s = trees[rs * 2].s + trees[rs * 2 + 1].s;
}
int querry(int rs, int l, int r)
{
if (trees[rs].l == l && trees[rs].r == r)
{
return trees[rs].s;
}
int mid = (trees[rs].l + trees[rs].r) / 2;
if (r <= mid)return querry(rs * 2, l, r);
else if (l > mid)return querry(rs * 2 + 1, l, r);
else if(l<=mid && r>mid)
{
return querry(rs * 2, l, mid) + querry(rs * 2 + 1, mid + 1, r);
}
return 0;
}
int main()
{
int t; cin >> t;
int ans = 0;
while (t--)
{
cin >> n;
memset(a, 0, sizeof(a));
memset(trees, 0, sizeof(trees));
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
buildtree(1, 1, n);
printf("Case %d:\n", ++ans);
string str;
while (cin >> str)
{
if (str == "End")break;
if (str == "Query")
{
int ac, bc;
scanf("%d %d", &ac, &bc);
printf("%d\n",querry(1, ac, bc));
}
else if (str == "Add")
{
int ac, bc;
scanf("%d %d", &ac, &bc);
int res = a[ac] + bc;
update(1, ac, res);
}
else if(str=="Sub")
{
int ac, bc;
scanf("%d %d", &ac, &bc);
int res = a[ac] - bc;
update(1, ac, res);
}
}
}
return 0;
}
标签:round 整数 sub err else output set 错误 tree
原文地址:https://www.cnblogs.com/00isok/p/9344639.html