标签:
input | output |
---|---|
2 2 1 1 1 1 1 1 1 0 0 0 1 1 0 1 0 3 2 0 0 0 0 0 0 2 0 0 0 0 1 0 1 0 1 0 -2 2 0 0 0 1 1 1 3 |
0 1 4 2
|
分析:三维树状数组,求和时类似于容斥;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, rt<<1 #define Rson mid+1, R, rt<<1|1 const int maxn=1e5+10; const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}}; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} int n,m,k,t; ll a[129][129][129]; void add(int x,int y,int z,int w) { for(int i=x;i<=n;i+=(i&(-i))) for(int j=y;j<=n;j+=(j&(-j))) for(int k=z;k<=n;k+=(k&(-k))) a[i][j][k]+=w; } ll get(int x,int y,int z) { ll ans=0; for(int i=x;i;i-=(i&(-i))) for(int j=y;j;j-=(j&(-j))) for(int k=z;k;k-=(k&(-k))) ans+=a[i][j][k]; return ans; } int main() { int i,j; scanf("%d",&n); while(~scanf("%d",&m)&&m!=3) { if(m==1) { int b[4]; rep(i,0,3)scanf("%d",&b[i]); add(++b[0],++b[1],++b[2],b[3]); } else { int b[6]; rep(i,0,5)scanf("%d",&b[i]),b[i]++; printf("%lld\n",get(b[3],b[4],b[5]) -get(b[0]-1,b[4],b[5])-get(b[3],b[1]-1,b[5])-get(b[3],b[4],b[2]-1) +get(b[0]-1,b[1]-1,b[5])+get(b[3],b[1]-1,b[2]-1)+get(b[0]-1,b[4],b[2]-1) -get(b[0]-1,b[1]-1,b[2]-1)); } } //system("Pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/dyzll/p/5802139.html