BZOJ1452 count (树状数组)

一道比较水的二维树状数组,开100个即可,只有100种颜色还是比较EZ的。

 1 Program BZOJ1452;
 2 const maxn=308;
 3       maxc=108;
 4 var a:array[0..maxn,0..maxn,0..maxc] of longint;
 5     f:array[0..maxn,0..maxn] of longint;
 6     m,n,i,j,x,y,x1,y1,x2,y2,z,Q,ch,sum:longint;
 7 procedure add(i,j,c,k:longint);
 8 var x,y:longint;
 9 begin
10     x:=i;
11     while x<=m do
12     begin
13         y:=j;
14         while y<=n do
15         begin
16             inc(a[x,y,c],k);
17             y:=y+y and -y;
18         end;
19         x:=x+x and -x;
20     end;
21 end;
22 function sigma(i,j,c:longint):longint;
23 var x,y,sum:longint;
24 begin
25     sum:=0;    x:=i;
26     while x>0 do
27     begin
28         y:=j;
29         while y>0 do
30         begin
31             inc(sum,a[x,y,c]);
32             y:=y-y and -y;
33         end;
34         x:=x-x and -x;
35     end;
36     exit(sum);
37 end;
38 begin
39     readln(m,n);
40     for i:=1 to m do
41         for j:=1 to n do
42         begin
43             read(x);
44             add(i,j,x,1);
45             f[i,j]:=x;
46         end;
47     readln(Q);
48     while Q>0 do
49     begin
50         read(ch);
51         if ch=1 then
52         begin
53             readln(x,y,z);
54             add(x,y,f[x,y],-1);
55             f[x,y]:=z;
56             add(x,y,z,1);
57         end else
58         begin
59             readln(x1,x2,y1,y2,z);
60             sum:=sigma(x2,y2,z)-sigma(x1-1,y2,z)-sigma(x2,y1-1,z)+sigma(x1-1,y1-1,z);
61             writeln(sum);
62         end;
63         dec(Q);
64     end;
65 end.
时间: 2024-12-10 00:03:00

BZOJ1452 count (树状数组)的相关文章

【BZOJ-1452】Count 树状数组 套 树状数组

1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1769  Solved: 1059[Submit][Status][Discuss] Description Input Output Sample Input Sample Output 1 2 HINT Source Solution 忽略标题的说法...撞壁用的.... 简单题,裸树状数组套树状数组 颜色数目$c<=100$很小,考虑对于每种颜色单独进

【BZOJ】1452: [JSOI2009]Count 树状数组

Description Input Output Sample Input Sample Output 1 2 HINT 题解: 二维的树状数组啊+一维的颜色状态,然后直接做就好……实际上比照一维的树状数组就是多了一个for循环,然后查询操作的时候就相当于查询某一矩阵的大小,树状数组起到一个类似前缀和的作用.   1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <

[bzoj1452][JSOI2009]Count(树状数组)

1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2057  Solved: 1216[Submit][Status][Discuss] Description Input Output Sample Input Sample Output 1 2 HINT Source 裸得不能再裸了 暴力100个二维即可 1 #include<stdio.h> 2 #include<stdlib.h> 3

BZOJ 1452: [JSOI2009]Count (二维树状数组)

Description Input Output Sample Input Sample Output 1 2 HINT 二维树状数组的简单应用,c数组的第一维坐标相当于哈希.如果是修改操作,修改前 将当前的值的个数以及祖先都减1, 修改后将个数加1. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include

BZOJ 1452: [JSOI2009]Count 二维树状数组

1452: [JSOI2009]Count Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1452 Description Input Output Sample Input Sample Output 1 2 HINT 题意 题解: 裸的二维树状数组 代码: //qscqesze #include <cstdio> #include <cmath&g

BZOJ 1452 JSOI 2009 Count 二维树状数组

题目大意:有一个m*n的方格,每一个格子有他自己的权值.2种操作: 1.改变一个格子的权值. 2.查询所有的x1 <= x <= x2 && y1 <= y <= y2的中,有多少个格子颜色是c. 思路:好像是二维树状数组的样子,但是不知道怎么搞.后来研究了数据范围,发现格子最大300*300,颜色最多才100种,于是算一下300*300*100*4/1024/1024大概是35M,题目要求64M,可以搞了.(这里算的精确一点,我当时没怎么算,吧颜色开成300的了,

深入理解树状数组

树状数组(Binary Indexed Tree(BIT), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值:经过简单修改可以在log(n)的复杂度下进行范围修改,但是这时只能查询其中一个元素的值(如果加入多个辅助数组则可以实现区间修改与区间查询). 百度上给出了令人难以理解的概念,其实这个东西我也是琢磨了一天,参考了大量博客的笔记才搞清楚了大致思路和原理,说说心得吧! 假设数组a[1..n],那么

Stars_树状数组

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given

POJ 2352 Stars(树状数组)

                                                                 Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44309   Accepted: 19236 Description Astronomers often examine star maps where stars are represented by points on a pla