poj 2115 Matrix
题意:
给出一个矩阵,有两种操作:
1.翻转给定的子矩阵;
2.查询a[i][j]的值。
思路:
树状数组是从小到大更新的。
这个题用二维树状数组可以解决,假设是一维树状数组,
0 0 0 0 0 0
我们把第三个到第四个翻转,变成
0 0 1 1 -1 0
sum[1] = 0,sum[2] = 0,sum[3] = 1,sum[4] = 1,sum[5] = 0,sum[6] = 0
所以类似一维的bit,但是要用到容斥原理,多减的要加回来。
代码:
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- const int N = ;
- int c[N][N];
- int n;
- int lowbit(int x)
- {
- return x&(-x);
- }
- void add(int x,int y)
- {
- for (int i = x;i <= n;i += lowbit(i))
- {
- for (int j = y;j <= n;j += lowbit(j))
- {
- c[i][j] += ;
- }
- }
- }
- int getsum(int x,int y)
- {
- int ans = ;
- for (int i = x;i > ;i -= lowbit(i))
- {
- for (int j = y;j > ;j -= lowbit(j))
- {
- ans += c[i][j];
- }
- }
- return ans;
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while (T--)
- {
- int op;
- scanf("%d%d",&n,&op);
- memset(c,,sizeof(c));
- while (op--)
- {
- char s[];
- scanf("%s",s);
- if (s[] == 'C')
- {
- int x1,y1,x2,y2;
- scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
- add(x1,y1);
- add(x2 + ,y1);
- add(x1,y2 + );
- add(x2 + ,y2 + );
- }
- if (s[] == 'Q')
- {
- int x,y;
- scanf("%d%d",&x,&y);
- int ans = getsum(x,y);
- if (ans % ) puts("");
- else puts("");
- }
- }
- if (T) puts("");
- }
- return ;
- }
poj 2115 Matrix的更多相关文章
- POJ 2115 C Looooops(扩展欧几里得应用)
题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...
- POJ poj 2155 Matrix
题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...
- 【题解】POJ 2115 C Looooops (Exgcd)
POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...
- 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series
poj 1575 Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...
- POJ 2155 Matrix
二维树状数组.... Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- poj 3685 Matrix(二分搜索之查找第k大的值)
Description Given a N × N matrix A, whose element × i + j2 - × j + i × j, you are to find the M-th s ...
- POJ 2155 Matrix (D区段树)
http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 1 ...
随机推荐
- python3 配置logging日志类
配置类config_file: from configparser import ConfigParser class config_file: def __init__(self,conf_file ...
- django——CRM项目
1.引言 CRM,客户关系管理系统(Customer Relationship Management).企业用CRM技术来管理与客户之间的关系,以求提升企业成功的管理方式,其目的是协助企业管理销售循环 ...
- GMA Round 1 新程序
传送门 新程序 程序框图如图所示,当输入的n=时,输出结果的ans是多少? 容易看出该程序求n以内质数个数,50以内有15个. 定位:简单题
- P1316 丢瓶盖--(二分答案)
题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...
- duilib 新增数据迁移界面
xml界面配置: <?xml version="1.0" encoding="utf-8"?> <Window caption="0 ...
- CentOS7 安装 jexus-5.8.2-x64
前要提示: 如果你要安装asp.net 请参考: http://www.cnblogs.com/xiaoruilin/p/7867823.html 背景: CentOS7 Mono (Mono JIT ...
- convert(varchar(10),字段名,转换格式
sql 时间转换格式 ) convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121)CONVERT为日期转换函数,一般就是在时 ...
- Java+面向对象初学感想
在先导课之前我未曾接触C和Python之外的语言,在大一学年的课程中,用面向过程的思想方法足以完成绝大多数课程要求,故而我也对面向对象的编程思想知之甚少,虽然多有耳闻但是对它一直没有一个正式的学习了解 ...
- [daily][archlinux] TODO LIST
reminder: https://wikemacs.org/wiki/Buffer_management TO READ: https://www.kernel.org/doc/htmldocs/k ...
- HTML技巧篇:如何让单行文本以及多行文本溢出时显示省略号(…)
参考:https://baijiahao.baidu.com/s?id=1621362934713048315&wfr=spider&for=pc 本篇文章主要给大家介绍一下在html ...