标签:orm ogr matrix ble lang return arc 分享图片 lin
poj-1195-Mobile Phones
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 20907 | Accepted: 9662 |
Description
Input

Output
Sample Input
0 4 1 1 2 3 2 0 0 2 2 1 1 1 2 1 1 2 -1 2 1 1 2 3 3
Sample Output
3 4
Source
| 17857925 | 1195 | Accepted | 4500K | 1079MS | G++ | 1287B | 2017-11-18 20:33:00 |
二维树状数组。
/// poj -1195
#include <cstdio>
#include <cstring>
const int MAXN = 1025;
int s, mp[MAXN][MAXN];
inline int lowbit(int x){
return x & (-x);
}
long long Compute(int x1, int y1, int x2, int y2){
long long ans, ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
for(int i=x1-1; i>0; i-=lowbit(i)){
for(int j=y1-1; j>0; j-=lowbit(j)){
ans1 += mp[i][j];
}
}
for(int i=x2; i>0; i-=lowbit(i)){
for(int j=y2; j>0; j-=lowbit(j)){
ans2 += mp[i][j];
}
}
for(int i=x2; i>0; i-=lowbit(i)){
for(int j=y1-1; j>0; j-=lowbit(j)){
ans3 += mp[i][j];
}
}
for(int i=x1-1; i>0; i-=lowbit(i)){
for(int j=y2; j>0; j-=lowbit(j)){
ans4 += mp[i][j];
}
}
ans = ans1 + ans2 - ans4 - ans3;
return ans;
}
void Add(int x, int y, const int val){
for(int i=x; i<=s; i+=lowbit(i)){
for(int j=y; j<=s; j+=lowbit(j)){
mp[i][j] += val;
}
}
}
int main(){
freopen("in.txt", "r", stdin);
int n, x1, y1, x2, y2, val;
long long ans;
while(scanf("%d", &n ) != EOF){
if(n >= 3){
break;
}else if(n == 0){
scanf("%d", &s);
memset(mp, 0, sizeof(mp));
}else if(n == 1){
scanf("%d %d %d", &x1, &y1, &val);
Add(x1 + 1, y1 + 1, val);
}else{
scanf("%d %d %d %d", &x1, &y1, &x2, &y2 );
ans = Compute( x1 + 1, y1 + 1, x2 + 1, y2 + 1 );
printf("%lld\n", ans );
}
}
return 0;
}
标签:orm ogr matrix ble lang return arc 分享图片 lin
原文地址:http://www.cnblogs.com/zhang-yd/p/7857872.html