hdu 1892二维树状数组
这题我知道是用树状数组,可是好久没打树状数组了,就想用普通方法水过去~~结果……结果……水了好多方法都水不过,出题人真狠呐……我的水方法是对于每一次查询,初始化ans=(x2-x1+1)*(y2-y1+1),然后对于这个操作之前的每一个操作,对ans进行处理即可。可是交上去TLE,加上输入外挂,还是TLE,又加一个优化,即对于每一个查询,如果查询的区间小于10000,就直接数,还是TLE,服了,还是打树状数组吧~~~~~~~~~~~~~
我的水代码:
/** hdu1892/win.cpp* Created on: 2012-11-1* Author : ben*/ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <queue> #include <set> #include <map> #include <stack> #include <string> #include <vector> #include <deque> #include <list> #include <functional> #include <numeric> #include <cctype> using namespace std; const int MAXN = 1010; typedef struct Ope{int x1, x2, y1, y2;int n;char type; }Ope; vector<Ope> ope; int room[MAXN][MAXN]; inline int get_int() {int res = 0, ch;while (!((ch = getchar()) >= '0' && ch <= '9')) {if (ch == EOF)return 1 << 30;}res = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9')res = res * 10 + (ch - '0');return res; } inline bool inrect(int &x, int &y, int &x1, int &y1, int &x2, int &y2) {return x >= x1 && x <= x2 && y >= y1 && y <= y2; } void work2(int x1, int y1, int x2, int y2, int n) {int ans = (x2 - x1 + 1) * (y2 - y1 + 1);for(int i = 0; i < n; i++) {if(ope[i].type == 'M') {if(inrect(ope[i].x1, ope[i].y1, x1, y1, x2, y2)) {ans -= ope[i].n;}if(inrect(ope[i].x2, ope[i].y2, x1, y1, x2, y2)) {ans += ope[i].n;}}else if(ope[i].type == 'A') {if(inrect(ope[i].x1, ope[i].y1, x1, y1, x2, y2)) {ans += ope[i].n;}}else if(ope[i].type == 'D') {if(inrect(ope[i].x1, ope[i].y1, x1, y1, x2, y2)) {ans -= ope[i].n;}}}printf("%d\n", ans); } void work1(Ope &o) {int ans = (o.x2 - o.x1 + 1) * (o.y2 - o.y1 + 1);if(ans < 1000) {ans = 0;for(int i = o.x1; i <= o.x2; i++) {for(int j = o.y1; j <= o.y2; j++) {ans += room[i][j];}}printf("%d\n", ans);o.type = 0;} } inline void input(Ope &o) {char c = getchar();int x1, y1, x2, y2;while(c <= ' ') {c = getchar();}o.type = c;if(c == 'S') {x1 = get_int(), y1 = get_int(), x2 = get_int(), y2 = get_int();o.x1 = min(x1, x2); o.x2 = max(x1, x2);o.y1 = min(y1, y2); o.y2 = max(y1, y2);work1(o);}else if(c == 'A') {o.x1 = get_int(), o.y1 = get_int(), o.n = get_int();room[o.x1][o.y1] += o.n;}else if(c == 'D') {o.x1 = get_int(), o.y1 = get_int(), o.n = get_int();if(room[o.x1][o.y1] < o.n) {o.n = room[o.x1][o.y1];}room[o.x1][o.y1] -= o.n;}else if(c == 'M') {o.x1 = get_int(), o.y1 = get_int(), o.x2 = get_int(), o.y2 = get_int();o.n = get_int();if(room[o.x1][o.y1] < o.n) {o.n = room[o.x1][o.y1];}room[o.x1][o.y1] -= o.n;room[o.x2][o.y2] += o.n;} }int main() { #ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin); #endifint T, Q;T = get_int();for(int t = 1; t <= T; t++) {printf("Case %d:\n", t);Q = get_int();fill(*room, *room + MAXN * MAXN, 1);ope.resize(Q);for_each(ope.begin(), ope.end(), input);for(int i = 0, len = (int)ope.size(); i < len; i++) {if(ope[i].type == 'S') {work2(ope[i].x1, ope[i].y1, ope[i].x2, ope[i].y2, i);}}}return 0; }
二维树状数组代码:
/** hdu1892/win.cpp* Created on: 2011-9-6* Author : ben*/ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <map> using namespace std; typedef long long LL; const int MAXN = 1005; LL data[MAXN][MAXN]; int Row = 1001, Col = 1001; inline int Lowbit(const int &x) { // x > 0return x & (-x); } LL sum(int x, int y) {LL ret = 0;for(int i = x; i > 0; i -= Lowbit(i)) {for(int j = y; j > 0; j -= Lowbit(j)) {ret += data[i][j];}}return ret; } void update(int x, int y, int delta) {for(int i = x; i <= Row; i += Lowbit(i)) {for(int j = y; j <= Col; j += Lowbit(j)) {data[i][j] += delta;}} } void work() {char c;int x1, y1, x2, y2, n;scanf(" %c", &c);if(c == 'S') {scanf("%d%d%d%d", &x1, &y1, &x2, &y2);if(x1 > x2) {swap(x1, x2);}if(y1 > y2) {swap(y1, y2);}printf("%d\n", (int)(sum(x2 + 1, y2 + 1) - sum(x1, y2 + 1) + sum(x1, y1) - sum(x2 + 1, y1)));}else if(c == 'A') {scanf("%d%d%d", &x1, &y1, &n);update(x1 + 1, y1 + 1, n);}else if(c == 'D') {scanf("%d%d%d", &x1, &y1, &n);x1++, y1++;int t =sum(x1, y1) - sum(x1 - 1, y1) - sum(x1, y1 - 1) + sum(x1 - 1, y1 - 1);if(t < n) {n = t;}update(x1, y1, -n);}else if(c == 'M') {scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n);x1++, y1++;x2++, y2++;int t =sum(x1, y1) - sum(x1 - 1, y1) - sum(x1, y1 - 1) + sum(x1 - 1, y1 - 1);if(t < n) {n = t;}update(x1, y1, -n);update(x2, y2, n);} } void init() {for(int i = 1; i <= Row; i++) {for(int j = 1; j <= Col; j++) {update(i, j, 1);}} } int main() { #ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin); #endifint T, Q;scanf("%d", &T);for(int t = 1; t <= T; t++) {printf("Case %d:\n", t);scanf("%d", &Q);fill(*data, *data + MAXN * MAXN, 0);init();while(Q--) {work();}}return 0; }
转载于:https://www.cnblogs.com/moonbay/archive/2012/11/01/2749579.html
hdu 1892二维树状数组相关推荐
- hdu 1892【二维树状数组】
O(∩_∩)O哈哈~二维树状数组被我搞出来了,太开心了,第一道二维树状数组,(- o -)~zZ 虽然中途还是出问题了,就是S询问的时候我没有考虑坐标的大小问题,还是搜了别人的代码才知道的,看来自己考 ...
- HDU 5517---Triple(二维树状数组)
题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...
- HDU 5465 Clarke and puzzle (二维树状数组维护区间异或)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5465 解题思路: 因为要对大量的区间进行异或,所以考虑用二维树状数组就行维护. #include< ...
- HDU - 5517 Triple(三维偏序-二维树状数组/CDQ分治)
题目链接:点击查看 题目大意:给出 n 个二元对 ( a , b ) 和 m 个三元对 ( c , d , e ),对于所有 b == e 的二元对和三元对,可以通过某种运算形成一个新的三元对 ( a ...
- hdu 5465 Clarke and puzzle (二维树状数组+nim博弈)
解析: 利用二维树状数组来区间询问异或和,以及单点更新,然后利用nim博弈的结论判断胜负. mymy codecode #include <cstdio> #include <cst ...
- HDU-4456 Crowd 二维树状数组+坐标转换
题意:给定一个N*N的网格,现在M组操作,一种操作时改变网格上的某个单点的权值,另外一种操作是求到一点曼哈顿距离为小于等于k的所有的权值和,初始化网格所有点的权值为0. 解法:这题如果没有那些特定的条 ...
- 二维树状数组 ----2021广东省赛 ----- K - Kera‘s line segment[区间转二维平面+树状数组维护前缀最小最大值]
题目链接 题目大意: 就是一个一维的数轴上面有一堆线段用一个三元组(l,r,val)(l,r,val)(l,r,val)表示. 现在我们有两个操作: 就是往数轴上面添加线段 询问[L,R][L,R][ ...
- szu 寒训第二天 树状数组 二维树状数组详解,以及树状数组扩展应用【求逆序对,以及动态第k小数】
树状数组(Binary Index Tree) 树状数组可以解决可以转化为前缀和问题的问题 这是一类用以解决动态前缀和的问题 (有点像线段树简版) 1.对于 a1 + a2 + a3 + - + an ...
- 【二维树状数组】See you~
https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A ...
最新文章
- spring 依赖注入的3种方式
- Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词
- EBS业务学习之应付INVOICE类型
- 【软件工程】用户在软件项目中承担的工作
- k8s核心技术-Controller(Deployment)_发布应用---K8S_Google工作笔记0029
- java代码执行 打包jar_Java程序打包成jar文件包并执行的方法
- 2012年7月的主要目标
- 讯飞 tts 9.0 app_讯飞B1录音笔,到底值不值得买?
- 二分图匹配匈牙利算法BFS实现
- fir.im分发平台安卓苹果应用下载二维码合并步骤
- 最速下降法python_用Python实现最速下降法求极值的方法
- 领域驱动设计整理——概念架构
- java gdal开源库_基于GDAL库,读取.grd文件(以海洋地形数据为例)Java版
- UE4读取BackBuffer缓冲区贴图(屏幕表面)
- opencv 图像人物识别
- 源程序,目标程序,可执行程序
- 制作绿色软件 不在控制面板软件卸载中显示
- dpdk课程学习之练习笔记四(dns预备)
- react-create-app 配置alias别名
- 一加7t人脸识别_一加7T新机设计图发布 这款手机的外观设计如何
热门文章
- pythonexcel工具介绍_Python处理excel的强大工具
- f12控制台如何查看consul_Consul初探-从安装到运行
- 观山湖区计算机培训班学校,2020年观山湖区教育系统办公室主任培训顺利开班...
- Service Mesh 是新瓶装旧酒吗?
- 从零开始入门 K8s | 可观测性:你的应用健康吗?
- linux字体栅格化,响应式开发---网页的布局方式、媒体查询、栅格化布局、less语言...
- linux系统测试报告,[Linux-文件系统测试] -- Bonnie++测试
- 工业机器人电柜布线_协作并联,重新注解并联机器人
- 训练损失越来越大_无需contrastive学习,微软亚研提基于实例分类无监督预训练方法...
- 了解mysql文章_一篇文章带你深入了解MySQL 索引相关