树状数组的各种变换

 

study from :

https://www.cnblogs.com/RabbitHu/p/BIT.html

 

loj

130

https://loj.ac/problem/130

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <set>
 8 #include <map>
 9 #include <queue>
10 #include <iostream>
11 using namespace std;
12 
13 #define ll long long
14 
15 const int maxn=1e6+10;
16 const int inf=1e9;
17 const double eps=1e-8;
18 
19 ll f[maxn];
20 int n;
21 
22 void modify(int i,int x)
23 {
24     while (i<=n)
25     {
26         f[i]+=x;
27         i+=i&-i;
28     }
29 }
30 
31 ll cal(int i)
32 {
33     ll sum=0;
34     while (i)
35     {
36         sum+=f[i];
37         i-=i&-i;
38     }
39     return sum;
40 }
41 
42 int main()
43 {
44     int q,x,y,z,i;
45     scanf("%d%d",&n,&q);
46     for (i=1;i<=n;i++)
47     {
48         scanf("%d",&x);
49         modify(i,x);
50     }
51 
52     while (q--)
53     {
54         scanf("%d%d%d",&x,&y,&z);
55         if (x==1)
56             modify(y,z);
57         else
58             printf("%lld\n",cal(z)-cal(y-1));
59     }
60     return 0;
61 }
62 /*
63 
64 */

 

131

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <set>
 8 #include <map>
 9 #include <queue>
10 #include <iostream>
11 using namespace std;
12 
13 #define ll long long
14 
15 const int maxn=1e6+10;
16 const int inf=1e9;
17 const double eps=1e-8;
18 
19 ll f[maxn];
20 int n;
21 
22 void modify(int i,int x)
23 {
24     while (i<=n)
25     {
26         f[i]+=x;
27         i+=i&-i;
28     }
29 }
30 
31 ll cal(int i)
32 {
33     ll sum=0;
34     while (i)
35     {
36         sum+=f[i];
37         i-=i&-i;
38     }
39     return sum;
40 }
41 
42 int main()
43 {
44     int q,x,y,z,r,i;
45     scanf("%d%d",&n,&q);
46     y=0;
47     for (i=1;i<=n;i++)
48     {
49         scanf("%d",&x);
50         modify(i,x-y);
51         y=x;
52     }
53 
54     while (q--)
55     {
56         scanf("%d%d",&x,&y);
57         if (x==1)
58         {
59             scanf("%d%d",&z,&r);
60             modify(y,r);
61             modify(z+1,-r);
62         }
63         else
64             printf("%lld\n",cal(y));
65     }
66     return 0;
67 }
68 /*
69 
70 */

 

132

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <set>
 8 #include <map>
 9 #include <queue>
10 #include <iostream>
11 using namespace std;
12 
13 #define ll long long
14 
15 const int maxn=1e6+10;
16 const int inf=1e9;
17 const double eps=1e-8;
18 
19 ll f1[maxn],f2[maxn];
20 int n;
21 
22 void modify(int i,int x)
23 {
24     ll xx=1ll*x*i;
25     while (i<=n)
26     {
27         f1[i]+=x;
28         f2[i]+=xx;
29         i+=i&-i;
30     }
31 }
32 
33 ll cal(int i)
34 {
35     int c=i+1;
36     ll sum=0;
37     while (i)
38     {
39         sum+=f1[i]*c-f2[i];
40         i-=i&-i;
41     }
42     return sum;
43 }
44 
45 int main()
46 {
47     int q,x,y,z,r,i;
48     scanf("%d%d",&n,&q);
49     y=0;
50     for (i=1;i<=n;i++)
51     {
52         scanf("%d",&x);
53         modify(i,x-y);
54         y=x;
55     }
56 
57     while (q--)
58     {
59         scanf("%d%d%d",&x,&y,&z);
60         if (x==1)
61         {
62             scanf("%d",&r);
63             modify(y,r);
64             modify(z+1,-r);
65         }
66         else
67             printf("%lld\n",cal(z)-cal(y-1));
68     }
69     return 0;
70 }
71 /*
72 
73 */

 

133

看数组就好。公式推导无法理解。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <set>
 8 #include <map>
 9 #include <queue>
10 #include <iostream>
11 using namespace std;
12 
13 #define ll long long
14 
15 const int maxn=1e6+10;
16 const int inf=1e9;
17 const double eps=1e-8;
18 
19 ll f[1<<12|1][1<<12|1];
20 int n,m;
21 
22 void modify(int i,int j,int x)
23 {
24     int ii=i;
25     while (j<=m)
26     {
27         i=ii;
28         while (i<=n)
29         {
30             f[i][j]+=x;
31             i+=i&-i;
32         }
33         j+=j&-j;
34     }
35 }
36 
37 ll cal(int i,int j)
38 {
39     int ii=i;
40     ll sum=0;
41     while (j)
42     {
43         i=ii;
44         while (i)
45         {
46             sum+=f[i][j];
47             i-=i&-i;
48         }
49         j-=j&-j;
50     }
51     return sum;
52 }
53 
54 int main()
55 {
56     int x,y,z,s,t;
57     scanf("%d%d",&n,&m);
58     while (~scanf("%d%d%d%d",&x,&y,&z,&s))
59     {
60         if (x==1)
61             modify(y,z,s);
62         else
63         {
64             scanf("%d",&t);
65             printf("%lld\n",cal(s,t)-cal(y-1,t)-cal(s,z-1)+cal(y-1,z-1));
66         }
67     }
68     return 0;
69 }
70 /*
71 
72 */

 

135

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <set>
 8 #include <map>
 9 #include <queue>
10 #include <iostream>
11 using namespace std;
12 
13 #define ll long long
14 
15 const int maxn=1e6+10;
16 const int inf=1e9;
17 const double eps=1e-8;
18 
19 ll f1[1<<12|1][1<<12|1],f2[1<<12|1][1<<12|1],f3[1<<12|1][1<<12|1],f4[1<<12|1][1<<12|1];
20 int n,m;
21 
22 void modify(int i,int j,int x)
23 {
24     int ii=i;
25     ll x2=1ll*x*i,x3=1ll*x*j,x4=1ll*x*i*j;
26     while (j<=m)
27     {
28         i=ii;
29         while (i<=n)
30         {
31             f1[i][j]+=x;
32             f2[i][j]+=x2;
33             f3[i][j]+=x3;
34             f4[i][j]+=x4;
35             i+=i&-i;
36         }
37         j+=j&-j;
38     }
39 }
40 
41 ll cal(int i,int j)
42 {
43     ll c1=1ll*(i+1)*(j+1),c2=j+1,c3=i+1;
44     int ii=i;
45     ll sum=0;
46     while (j)
47     {
48         i=ii;
49         while (i)
50         {
51             sum+=c1*f1[i][j]-c2*f2[i][j]-c3*f3[i][j]+f4[i][j];
52             i-=i&-i;
53         }
54         j-=j&-j;
55     }
56     return sum;
57 }
58 
59 int main()
60 {
61     int x,y,z,s,t,r;
62     scanf("%d%d",&n,&m);
63 
64     while (~scanf("%d%d%d%d%d",&x,&y,&z,&s,&t))
65     {
66         if (x==1)
67         {
68             scanf("%d",&r);
69             modify(y,z,r);
70             modify(y,t+1,-r);
71             modify(s+1,z,-r);
72             modify(s+1,t+1,r);
73         }
74         else
75             printf("%lld\n",cal(s,t)-cal(y-1,t)-cal(s,z-1)+cal(y-1,z-1));
76     }
77     return 0;
78 }
79 /*
80 
81 */

 

posted @ 2019-04-11 19:14  congmingyige  阅读(166)  评论(0编辑  收藏  举报