Codeforces 707 E. Garlands (二维树状数组)
题目链接:http://codeforces.com/problemset/problem/707/E
给你nxm的网格,有k条链,每条链上有len个节点,每个节点有一个值。
有q个操作,操作ask问你一个子矩阵的和是多少,操作switch将第i条链上的值0变原来的数or原来的数变0。
比较明显的二维数组数组暴力,注意的是ask操作不会超过2000,所以在switch操作的时候不能进行update操作,否则会超时。所以你在ask操作的时候update就会省时。
复杂度大概是2000*2000*log2(2000)*log2*(2000),cf上3s妥妥过。
- //#pragma comment(linker, "/STACK:102400000, 102400000")
- #include <algorithm>
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
- #include <vector>
- #include <cmath>
- #include <ctime>
- #include <list>
- #include <set>
- #include <map>
- using namespace std;
- typedef long long LL;
- typedef pair <int, int> P;
- const int N = 2e3 + ;
- int n, m;
- LL bit[N][N];
- struct data {
- int x[N], y[N], w[N], len;
- }a[N];
- bool change[N];
- int flag[N];
- void update(int x, int y, LL val) {
- for(int i = x; i <= n; i += (i&-i)) {
- for(int j = y; j <= m; j += (j&-j)) {
- bit[i][j] += val;
- }
- }
- }
- LL sum(int x, int y) {
- LL s = ;
- for(int i = x; i >= ; i -= (i&-i)) {
- for(int j = y; j >= ; j -= (j&-j)) {
- s += bit[i][j];
- }
- }
- return s;
- }
- int main()
- {
- int k, q;
- scanf("%d %d %d", &n, &m, &k);
- for(int i = ; i <= k; ++i) {
- scanf("%d", &a[i].len);
- for(int j = ; j <= a[i].len; ++j) {
- scanf("%d %d %d", &a[i].x[j], &a[i].y[j], &a[i].w[j]);
- update(a[i].x[j], a[i].y[j], (LL)a[i].w[j]);
- }
- }
- scanf("%d", &q);
- char query[];
- int x1, y1, x2, y2, c;
- for(int i = ; i <= q; ++i) {
- scanf("%s", query);
- if(query[] == 'A') {
- scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
- for(int id = ; id <= k; ++id) {
- if(change[id]) {
- if(flag[id] % ) {
- for(int j = ; j <= a[id].len; ++j) {
- update(a[id].x[j], a[id].y[j], (LL)a[id].w[j]);
- }
- } else {
- for(int j = ; j <= a[id].len; ++j) {
- update(a[id].x[j], a[id].y[j], (LL)-a[id].w[j]);
- }
- }
- change[id] = change[id] ? false: true;
- ++flag[id];
- }
- }
- printf("%lld\n", sum(x2, y2) - sum(x2, y1 - ) - sum(x1 - , y2) + sum(x1 - , y1 - ));
- } else {
- scanf("%d", &c);
- change[c] = change[c] ? false: true;
- }
- }
- return ;
- }
Codeforces 707 E. Garlands (二维树状数组)的更多相关文章
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- 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 ...
- Codeforces #590 D 二维树状数组
题意 给一个10^5之内的字符串(小写字母)时限2s 输入n,有n个操作 (n<10^5) 当操作是1的时候,输入位置x和改变的字母 当操作是2的时候,输入区间l和r,有多少不同的字母 思路 ...
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
随机推荐
- Dom对象的方法应用一getElementById技巧、getElementsByName() IE,firefox兼容
在document对象中有以下三个方法,对于程序员来说,真可谓无人不知,无人不晓,他们分别是: 1.getElementById() 返回对拥有指定 id 的第一个对 ...
- ecshop 优化_将商品详情页goods.php重命名为shangpin.php
有人说,将商品详情页的文件名 goods.php 改一个名字,对百度收录会有帮助,也许吧,这里不讨论是否有帮助,这里只讲解如何重命名. 例如:我们将 goods.php 改为 shangpin.php ...
- jQuery事件绑定方式(转)
bind() 简要描述 bind()向匹配元素添加一个或多个事件处理器. 使用方式 $(selector).bind(event,data,function) event:必需项:添加到元素的一个或多 ...
- Hdu 1521 排列组合
a1 n1 a2 n2 ... ak nkn=n1+n2+...+nk从n个数中选r个排列(不是组合噢)// 指数型母函数// 模板#include <iostream> #include ...
- NPOI 2.0导出word(docx格式)
大名鼎鼎的NPOI用来导出EXCEL的文章园子里面有很多,可是用来导出WORD文档的文章大都含糊不清,最近刚好完成一个导出WORD文档的需求,在此分享下. NPOI里面认为word文档的最基本的结构是 ...
- ubuntu 上更新安装 openoffice.org3的过程
方法一:手动安装1首先在“应用成程序--添加/删除”里卸载openoffice 2.4,可能openoffice.org 2.4 Draw因为关联而无法卸载,忽略,卸载其他几项.然后在中文官方网上下载 ...
- web开发利器 fiddler
http://mccxj.github.io/blog/20130531_introduce-to-fiddler.html
- c/c++ 编译器内存对齐问题
C语言结构体对齐问题详解 转载自:http://blog.csdn.net/tiany524/article/details/6295551 测试环境32位机 WinXP: 编译器VC6(MS cl. ...
- windows下mysql5.7安装及配置
装完msi后,复制my-default.ini文件,黏贴为my.ini文件,内容修改如下: # For advice on how to change settings please see# htt ...
- 让pre标签自动换行示例代码
pre 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码.对于技术博客经 ...