| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:二维树状数组 TLE 的同学们注意了 ! 下标从0开始。 附上测试数据。In Reply To:二维树状数组 TLE 的同学们注意了 ! 下标从0开始。 附上测试数据。 Posted by:lanjiongjiang at 2014-08-10 16:23:40 > TLE 的同学们注意了。下标从0开始。附上测试数据。
>
> in
> 0 1
> 1 0 0 28766
> 2 0 0 0 0
> 1 0 0 -2826
> 2 0 0 0 0
> 1 0 0 -587
> 2 0 0 0 0
> 1 0 0 1017
> 2 0 0 0 0
> 1 0 0 -17234
> 2 0 0 0 0
> 1 0 0 19618
> 2 0 0 0 0
> 1 0 0 -2237
> 2 0 0 0 0
> 1 0 0 -9192
> 2 0 0 0 0
> 1 0 0 -11132
> 2 0 0 0 0
> 1 0 0 1842
> 2 0 0 0 0
> 3
>
> out
> 28766
> 25940
> 25353
> 26370
> 9136
> 28754
> 26517
> 17325
> 6193
> 8035
>
> 过了这组应该就没有问题了。
>
> //////////////My AC Code///////////////////
> /* 641MS C++ */
> /* 1079MS G++ */
> #include <cstdio>
> #include <iostream>
> #include <cstring>
> #include <cmath>
> #include <string>
> #include <algorithm>
> #include <queue>
> #include <stack>
> using namespace std;
>
> const int MAXN = 1050;
> int s[MAXN][MAXN];
> int lowbit[MAXN];
> int n;
>
> int sum(int x, int y)
> {
> int res = 0;
> for(int i = x; i > 0; i -= lowbit[i])
> for(int j = y; j > 0; j -= lowbit[j])
> res += s[i][j];
> return res;
> }
>
> int main()
> {
> //freopen("in.txt", "r", stdin);
> //freopen("out.txt", "w", stdout);
> int op;
> int x, y, v;
> int x1, y1, x2, y2;
> for(int i = 1; i <= MAXN; i++)
> {
> lowbit[i] = i&-i;
> }
> while(1)
> {
> scanf("%d", &op);
> if(op == 1)
> {
> scanf("%d %d %d", &x, &y, &v);
> x++; y++;
> for(int i = x; i <= n; i += lowbit[i])
> for(int j = y; j <= n; j += lowbit[j])
> s[i][j] += v;
> }
> else if(op == 2)
> {
> scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
> x1++; y1++; x2++; y2++;
> int ans = sum(x2, y2)+sum(x1-1, y1-1)-sum(x1-1, y2)-sum(x2, y1-1);
> printf("%d\n", ans);
> }
> else if(op == 0)
> {
> scanf("%d", &n);
> memset(s, 0, sizeof(s));
> }
> else
> break;
> }
> return 0;
> }
>
过了也WAle
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator