传送门:HDU5454

Excited Database

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 129    Accepted Submission(s): 26

Problem Description
She says that any Pavarotti among the nightingales will serenade his mate while she sits on her eggs.
She says that any excited database can answer the queries efficiently.

You are given the two dimensional database as a matrix A with n rows and n columns. In the beginning, A[i][j]=0 for all 1≤i,j≤n .
Then q operations or queries will be given in turn.

You should maintain the database for two type of operations:
1 L R : for each element A[i][j] which satisfy L≤i+j≤R , increase the value to A[i][j]+1 , where 2≤L≤R≤2n .
2 L R : for each element A[i][j] which satisfy L≤i−j≤R , increase the value to A[i][j]+1 , where 1−n≤L≤R≤n−1 .
Meanwhile, you should answer the queries:
3 x1 x2 y1 y2 : count the value of elements A[i][j] which satisfy x1≤i≤x2 and y1≤j≤y2 , where 1≤x1<x2≤n and 1≤y1<y2≤n .

Input
The input contains several test cases. The first line of the input is a single integer t which is the number of test cases. Then t test cases follow.

Each test case contains several lines. The first line contains the integer n and q .
The i -th line of the next q lines contains an operation ‘‘1 L R" or ‘‘2 L R" , or a query ‘‘3 x1 x2 y1 y2" .

The sum of n for all test cases would not be larger than 200000 and the sum of q would not be larger than 50000 .

Output
For each test case, you should output answers to queries printed one per line.
Sample Input
  
26 6 2 0 1 3 1 4 3 5 3 2 5 2 3 1 5 7 3 1 4 3 5 3 2 5 2 36 26 2 -4 -1 3 1 4 2 5 3 3 6 4 6 1 4 7 3 2 5 2 3 3 1 4 2 5 2 -3 -1 3 1 4 3 5 1 3 5 1 2 3 3 2 5 2 3 3 1 4 2 5 3 3 6 4 6 2 0 4 3 1 4 3 5 3 1 4 2 5 1 9 11 2 1 2 3 2 5 2 3 3 3 6 4 6 2 -2 2 1 7 12 3 1 4 3 5 3 2 5 2 3 3 1 4 2 5 3 3 6 4 6
Sample Output
  
Case #1: 3 4 11 10 Case #2: 10 6 8 22 26 12 38 13 32 44 23 30 49 33 67 53
Source
2015 ACM/ICPC Asia Regional Shenyang Online
题意:在一个n*n的矩阵中,(n<=20w),有两种操作:操作1:将A[i]j](L<= i + j <=R)  +1,操作2:将A[i][j](L<=i - j < =R )   +1. 询问x1,y1到x2,y2区域内的和。
思路:按照主对角线和副对角线分别建立线段树,操作就是区间累加操作,维护需要维护区间的sum和A[i]*i的和,查询是将矩形分解为2个等腰直角三角形和一个平行四边形。分别进行询问。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef __int64 LL;
const int maxn = 300005;
struct tree
{LL suml[maxn << 2], sumr[maxn << 2], sum[maxn << 2];LL add[maxn << 2];inline void pushup(int id, int L, int R){LL mid = L + R >> 1;sum[id] = sum[id << 1] + sum[id << 1 | 1];suml[id] = suml[id << 1] + (mid - L + 1)*sum[id << 1 | 1] + suml[id << 1 | 1];sumr[id] = sumr[id << 1 | 1] + sumr[id << 1] + (R - mid)*sum[id << 1];}inline void pushdown(int id, int L, int R){int mid = L + R >> 1;if (add[id]){LL ln = mid - L + 1;LL rn = R - mid;add[id << 1] += add[id];add[id << 1 | 1] += add[id];sum[id << 1] += add[id] * ln;sum[id << 1 | 1] += add[id] * rn;suml[id << 1] += add[id] * (1 + ln)*ln >> 1;suml[id << 1 | 1] += add[id] * (1 + rn)*rn >> 1;sumr[id << 1] += add[id] * (1 + ln)*ln >> 1;sumr[id << 1 | 1] += add[id] * (1 + rn)*rn >> 1;add[id] = 0;}}void build(){memset(sum, 0, sizeof(sum));memset(suml, 0, sizeof(suml));memset(sumr, 0, sizeof(sumr));memset(add, 0, sizeof(add));}LL queL(int id, int L, int R, int l, int r){//  if(id>400000) while(1);if (l <= L&&R <= r) return (L - l)*sum[id] + suml[id];else{pushdown(id, L, R);int mid = L + R >> 1;LL res = 0;if (l <= mid) res += queL(id << 1, L, mid, l, r);if (mid<r) res += queL(id << 1 | 1, mid + 1, R, l, r);return res;}}LL queR(int id, int L, int R, int l, int r){if (l <= L&&R <= r) return (r - R)*sum[id] + sumr[id];else{pushdown(id, L, R);int mid = L + R >> 1;LL res = 0;if (l <= mid) res += queR(id << 1, L, mid, l, r);if (mid<r) res += queR(id << 1 | 1, mid + 1, R, l, r);return res;}}LL que(int id, int L, int R, int l, int r){if (l <= L&&R <= r) return sum[id];else{pushdown(id, L, R);int mid = L + R >> 1;LL res = 0;if (l <= mid) res += que(id << 1, L, mid, l, r);if (mid<r) res += que(id << 1 | 1, mid + 1, R, l, r);return res;}}void op(int id, int L, int R, int l, int r){if (l <= L&&R <= r){LL sn = R - L + 1;add[id]++;sum[id] += sn;suml[id] += (1 + sn)*sn >> 1;sumr[id] += (1 + sn)*sn >> 1;}else{pushdown(id, L, R);int mid = L + R >> 1;if (l <= mid) op(id << 1, L, mid, l, r);if (mid<r) op(id << 1 | 1, mid + 1, R, l, r);pushup(id, L, R);}}
}tz, tf;
int main()
{int T;scanf("%d", &T);int ks = 0;while (T--){printf("Case #%d:\n", ++ks);tz.build();tf.build();int n, m;scanf("%d %d", &n, &m);while (m--){int op;scanf("%d", &op);if (op == 1){int l, r;scanf("%d %d", &l, &r);tf.op(1, 1, n << 1, l, r);}else if (op == 2){int l, r;scanf("%d %d", &l, &r);l += n;r += n;tz.op(1, 1, n << 1, l, r);}else{int x1, y1, x2, y2;scanf("%d %d %d %d", &x1, &x2, &y1, &y2);LL ans = 0;int A, B, C, D;A = x1 - y1 + n;B = x1 - y2 + n;C = x2 - y2 + n;D = x2 - y1 + n;if (D >= max(A, C) + 1) ans += tz.queR(1, 1, n << 1, max(A, C) + 1, D);if (B <= min(A, C) - 1) ans += tz.queL(1, 1, n << 1, B, min(A, C) - 1);ans += tz.que(1, 1, n << 1, min(A, C), max(A, C))*(min(y2 - y1, x2 - x1) + 1);A = x1 + y1;B = x1 + y2;C = x2 + y2;D = x2 + y1;if (A <= min(B, D) - 1) ans += tf.queL(1, 1, n << 1, A, min(B, D) - 1);if (C >= max(B, D) + 1) ans += tf.queR(1, 1, n << 1, max(B, D) + 1, C);ans += tf.que(1, 1, n << 1, min(B, D), max(B, D))*(min(y2 - y1, x2 - x1) + 1);printf("%I64d\n", ans);}}}return 0;
}

HDU 5454 Excited Database 线段树的维护相关推荐

  1. hdu 5454 Excited Database(线段树)

    题目链接:hdu 5454 Excited Database 解题思路 维护两科线段树,一棵i+j,一棵i-j.因为对角线上的值一定都是相同的,只是查询时的个数不一样,但是根据区间范围可以计算出来每条 ...

  2. HDU 5454 Excited Database【线段树】

    简单线段树,就是推公式的过程有点不是特别熟练. 按照叉姐的方法,可以用三个树状数组维护,三种情况. 按照坦克工程师的方法,用容斥,将一个矩形分解成三个三角形,一个大的直角三角形减去两个小的直角三角形, ...

  3. HDU 5454 Excited Database (2015年沈阳赛区网络赛E题)

    1.题目描述:点击打开链接 2.解题思路:本题利用线段树解决,根据题意,我们需要建立两棵线段树,分别维护主对角线,副对角线.每个线段树的结点需要维护sum,sumL,sumR,其中,sum表示当前区间 ...

  4. 牛客 - 求函数(线段树+区间合并/线段树+矩阵维护)

    题目链接:点击查看 题目大意:现在有 n 个函数,每个函数都是诸如 f( x ) = k * x + b 的形式,只是每个函数的 k 和 b 都是相互独立的,现在给出两个操作: 1 pos k b:将 ...

  5. hdu 3397 Sequence operation(线段树,lazy,区间合并)

    hdu 3397 Sequence operation 线段树lazy和区间合并结合的一个题,相当于几个题集中到一起嘛,分开想就好了 0,1,2操作都要lazy,2的异或操作找到每一只含1或只含0的区 ...

  6. hdu 2871 Memory Control(线段树)

    题目链接:hdu 2871 Memory Control 题目大意:模拟一个内存分配机制. Reset:重置,释放全部空间 New x:申请内存为x的空间,输出左地址 Free x:释放地址x所在的内 ...

  7. 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)...

    HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道 ...

  8. HDU - 6183 暴力,线段树动态开点,cdq分治

    B - Color itHDU - 6183 题目大意:有三种操作,0是清空所有点,1是给点(x,y)涂上颜色c,2是查询满足1<=a<=x,y1<=b<=y2的(a,b)点一 ...

  9. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

最新文章

  1. JavaScript中的+运算
  2. JNI调用(github有代码可下载)
  3. 企业必须由真正在乎它的人掌控
  4. 拉杰尔安卓服务器注册上限,拉结尔多开养小号刷副本 用多多云手机离线能升级...
  5. iOS笔记之UIKit_UINavigationController
  6. SQL删除数据delete
  7. KEIL中遇到WARNING: MULTIPLE CALL TO SEGMENT的解决方法
  8. python画函数图像 保留_如何使用python的matplotlib画反正切函数图像
  9. python批量创建txt文件
  10. 第三次修正打坐的姿势
  11. WIFI热点软件测试,用手机WiFi热点已OUT 华为随行WiFi Plus评测
  12. require() 的基本用法
  13. ubuntu18.04 网络配置 ipv4ipv6DNS路由
  14. window系统如何禁止运行指定程序
  15. html bs架构调用客户端打印机用客户端及客户端局域网打印机打印,使用ScriptX.cab控件...
  16. Symantec Backup Exec Agent For Linux防火墙问题
  17. 为什么年终奖是一个彻头彻尾的职场圈套?
  18. 从零开始学习linux的I2C设备驱动框架——写一个简单的SHT20驱动
  19. py3 BeautifulSoup 利器 html 解析器使用
  20. android开发 硬件加速,Android开发的硬件加速

热门文章

  1. 我居然又回来了……(bushi灰太狼,之前拾贝,没坚持下去)
  2. Vue.js CLI:学习如何使用它
  3. 微信小程序----运动社区开发(一)
  4. 电脑C盘满了怎么快速清理
  5. 帝国CMS7.5全新后台 仿搜外问答模板 整站带演示数据源码
  6. python中for循环在遍历文件内容时的问题
  7. 导出收藏到html是什么意思,详细说明如何导出浏览器的收藏夹
  8. 01-【浏览器】chrome浏览器收藏夹(书签)的导出与导入
  9. 万字阐述智能驾驶汽车安全体系
  10. 基于C语言编写的超市管理系统