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

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

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

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

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

[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. Oracle使用fy_recover_data恢复truncate删除的数据

    (一)truncate操作概述 在生产中,truncate是使用的多的命令,在使用不当的情况下,往往会造成表的数据全部丢失,恢复较为困难.对于truncate恢复,常见的有以下几种方法可以进行恢复: ...

  2. Git把本地代码推送到远程github仓库

    运用Git版本控制系统进行代码的管理,以便于团队成员的协作,由于之前是使用svn来进行版本控制,所以对于Git使用还有待熟练掌握.Git与svn类似,个人认为两者之间比较直观的区别就是 Git 不需要 ...

  3. 初识Cobalt Strike

    简介 Cobalt Strike 一款以metasploit为基础的GUI的框架式渗透工具,集成了端口转发.服务扫描,自动化溢出,多模式端口监听,win exe木马生成,win dll木马生成,jav ...

  4. MySQL为某字段加前缀、后缀

    在开发过程中,可能会遇到加前缀或者后缀的情况.比如为视频添加路径时,如果手动加起来肯定慢,而且比较不符合程序员的特点,我们就应该能让程序跑就不会手动加. 使用UPDATE sql 语句:update ...

  5. nginx history路由模式时,页面返回404重定向index.html

    1.路由默认是带#的,有时我们感觉不美观,就使其变为history模式,也就没有#字符 2.# 如果找不到当前页面(404),就返回index.html,重新分配路由 location ^~/prod ...

  6. git在用https进行push时候免输账密的方法

    先新建一个文件 $ touch ~/.git-credentials $ vim ~/.git-credentials 进去添加内容(github为github.com,码云为gitee.com) h ...

  7. Test Test...

    标题: Test(一级标题) Test(二级标题) Test(三级标题) 列表: test(列表) Alpha Beta Gamma test 2 Delte Epsilon 链接: 点兔成金斐波那契 ...

  8. 20199326《Linux内核原理与分析》第十二周作业

    Collabtive系统跨站请求伪造攻击实验 实验背景 CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/sessi ...

  9. Docker help详细帮助

    常用的 docker 命令 docker # docker 命令帮助 Commands: attach Attach to a running container # 当前 shell 下 attac ...

  10. java关于throw Exception的一个小秘密

    目录 简介 throw小诀窍 总结 java关于throw Exception的一个小秘密 简介 之前的文章我们讲到,在stream中处理异常,需要将checked exception转换为unche ...