题意:有一个n*m的矩形,一辆车从左上角出发,沿一条路径走,路径是由矩形上每个单元格的边构成的,最后回到左上角,求车子在每个格子转过圈数的平方和。

思路:假设需要记录每个格子转的顺时针的圈数(为负表示转的逆时针),可以考虑车子每次移动对各个格子的贡献:

  • 车子左移,路径上方所有格子转的圈数+1,路径下方所有格子-1,而上方和下方所有格子都形成大的矩形,于是相当于每次对矩形区域的格子全部执行加减操作。
  • 车子右移,上方-1,下方+1。
  • 车子上移,左边-1,右边+1。
  • 车子下移,左边+1,右边-1。

对于询问,就是求每个点最终的值。这就是一个“区间修改,单点求值”的问题,用二维树状数组即可解决。

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#pragma comment(linker, "/STACK:10240000")
#include <bits/stdc++.h>
using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii; #ifndef ONLINE_JUDGE
namespace Debug {
void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
#endif // ONLINE_JUDGE
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
/* -------------------------------------------------------------------------------- */ struct TA {
vector<vector<int> > r;
int n, m;
void resize(int n, int m) {
this->n = n;
this->m = m;
r.resize(n + );
for (int i = ; i <= n; i ++) {
r[i].clear();
r[i].resize(m + );
}
}
inline int lowbit(const int &x) {
return x & -x;
}
void update(int px, int py, int v) {
int buf = py;
while (px <= n) {
py = buf;
while (py <= m) {
r[px][py] += v;
py += lowbit(py);
}
px += lowbit(px);
}
}
void update(int px1, int py1, int px2, int py2, int v) {
update(px1, py1, v);
update(px1, py2 + , -v);
update(px2 + , py1, -v);
update(px2 + , py2 + , v);
}
int query(int px, int py) {
int ans = , buf = py;
while (px) {
py = buf;
while (py) {
ans += r[px][py];
py -= lowbit(py);
}
px -= lowbit(px);
}
return ans;
}
};
TA ta; ll sqr(int x) {
return (ll)x * x;
} const int dx[] = {, , , -};
const int dy[] = {, -, , }; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int T, cas = , n, m, k, s, x, y, xx, yy, d0, f[];
char d[];
f['R'] = ;
f['L'] = ;
f['D'] = ;
f['U'] = ;
cin >> T;
while (T --) {
cin >> n >> m >> k;
n ++;
m ++;
ta.resize(n, m);
x = y = ;
while (k --) {
scanf("%s%d", &d, &s);
d0 = f[d[]];
xx = x + dx[d0] * s;
yy = y + dy[d0] * s;
if (d[] == 'L') {
ta.update(, yy, x - , y - , );
ta.update(x, yy, n - , y - , -);
}
if (d[] == 'R') {
ta.update(, y, x - , yy - , -);
ta.update(x, y, n - , yy - , );
}
if (d[] == 'U') {
ta.update(xx, , x - , y - , -);
ta.update(xx, y, x - , m - , );
}
if (d[] == 'D') {
ta.update(x, , xx - , y - , );
ta.update(x, y, xx - , m - , -);
}
x = xx;
y = yy;
}
ll ans = ;
for (int i = ; i < n; i ++) {
for (int j = ; j < m; j ++) {
ans += sqr(ta.query(i, j) / );
}
}
cout << "Case #" << ++ cas << ": " << ans << endl;
}
return ;
}

[LA7139 Rotation(2014 shanghai onsite)]二维树状数组的更多相关文章

  1. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

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

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  3. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  4. POJMatrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22058   Accepted: 8219 Descripti ...

  5. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  6. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  7. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  8. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  9. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

随机推荐

  1. 测试需要用到的chrome调试

    模拟慢网速 断开网络 F12后勾选上offline 请求304 后来发现是选中了该浏览其的Disable cache,去掉就好了.

  2. 菜鸡试飞----SRCの信息收集手册

    whois信息 微步在线 https://x.threatbook.cn/ 站长之家 http://whois.chinaz.com/ dns信息-----检测是否存在dns域传送漏洞 子域名的收集 ...

  3. selenium 键盘鼠标模拟

    一.键盘模拟常用的键 sendKeys(Keys.BACK_SPACE);  //删除键--Backspace sendKeys(Keys.SPACE);   //空格键 Space sendKeys ...

  4. [php] 猴子偷桃

    <?php /* 10:五只猴子采得一堆桃子,猴子彼此约定隔天早起后再分食. 不过,就在半夜里,一只猴子偷偷起来,把桃子均分成五堆后, 发现还多一个,它吃掉这桃子,并拿走了其中一堆.第二只猴子醒 ...

  5. phpstudy之访问loaclhost显示目录

    phpstudy版本:phpstudy2018 具体操作: 当前版本的默认设置访问网站根目录是不会显示目录的,需要我们设置,其实也很简单,只需两步就可以搞定 1.找到phpstudy目录下的www文件 ...

  6. Websec level 30

    前言 昨天在易霖博搞的网络安全与执法竞赛看到的一道web题,实际上就是用两个原题凑起来的.. 不过后面的一关没见过这里简单记录一下 第一关 打开是个登录界面,和BJDCTF的简单注入一模一样,连密码都 ...

  7. 好用的反向代理工具NATAPP

    这里推荐一个好用的反向代理工具NATAPP NATAPP1分钟快速新手图文教程 有免费的和付费的个人建议付费的,免费还需要身份证验证,付费版最低9元/月,看个人需求! 这里给个邀请码贴在这需要的话可以 ...

  8. Visual Studio 2015 + Windows 2012 R2, c++/cli Array::Sort() 抛出异常

    在Windows7上编译就是正常. 可见Windows2012 R2缺少了一些东西. 另外,有一个现象一样,但原因不一样的 https://stackoverflow.com/questions/46 ...

  9. linux uniq 命令实用手册

    Linux uniq 命令用于处理文本内容中的重复行. 这里我们只介绍其常用参数,其完整用法可参见man uniq. 例如,我们有如下文件内容: >>> cat log.txt __ ...

  10. 为什么要你们现在要学习python

    说学习python之前,我们先来聊聊其他的.我们都认为成功靠的是勤奋和努力,但是事实是只靠勤奋和努力是不一定会成功的,而且很大一部分都不会成功. 你有没有想过,同样是做企业,有些公司年收入百万,而腾讯 ...