【codeforces 707E】Garlands
【题目链接】:http://codeforces.com/contest/707/problem/E
【题意】
给你一个n*m的方阵;
里面有k个联通块;
这k个联通块,每个连通块里面都是灯;
给你q个操作;
有以下两种类型
①将第i个连通块里面灯取反
②询问你(x1,y1)(x2,y2)这个矩形区域内灯的权值的和;
【题解】
要用到二维的树状数组;
取反操作只要O(1)就能完成;
即先不管它是什么,取反就是了;
然后在询问的时候,直接用二维树状数组累加;
这里的累加可能是减也可能是加;
也可能不变;
搞根据flag数组的值和cha数组的值来判断;
然后就是一个二维的前缀和了;
具体的看代码吧。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int K = 2e3+100;
struct abc{
int x, y, w;
};
int n, m, k, num[K],cha[K],flag[K];
abc a[K][K];
char s[7];
LL sum[K][K];
int lowbit(int x) {return x&(-x);}
void add(int x, int y, LL t){
while (x <= n){
int j = y;
while (j <= m) {
sum[x][j] += t;
j += lowbit(j);
}
x += lowbit(x);
}
}
LL get_sum(int x, int y)
{
LL temp = 0;
while (x > 0){
int j = y;
while (j > 0){
temp += sum[x][j];
j -= lowbit(j);
}
x -= lowbit(x);
}
return temp;
}
int main(){
//freopen("F:\\rush.txt", "r", stdin);
rei(n), rei(m), rei(k);
rep1(i, 1, k){
rei(num[i]);
rep1(j, 1, num[i]) rei(a[i][j].x), rei(a[i][j].y), rei(a[i][j].w);
cha[i] = 1;
}
int q;
rei(q);
while (q--) {
scanf("%s", s);
if (s[0] == 'S'){
int k; rei(k);
cha[k] = 1 - cha[k];
}
else{
rep1(i, 1, k)
if (cha[i]){
rep1(j, 1, num[i]) add(a[i][j].x, a[i][j].y, a[i][j].w*((flag[i] == 0) ? 1 : -1));
cha[i] = 0, flag[i] = 1 - flag[i];
}
int x1, y1, x2, y2;
rei(x1), rei(y1), rei(x2), rei(y2);
printf("%lld\n", get_sum(x2, y2) - get_sum(x1-1, y2) - get_sum(x2, y1-1) + get_sum(x1-1, y1-1));
}
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 707E】Garlands的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
- 【codeforces 515C】Drazil and Factorial
[题目链接]:http://codeforces.com/contest/515/problem/C [题意] 定义f(n)=n这个数各个位置上的数的阶乘的乘积; 给你a; 让你另外求一个不含0和1的 ...
随机推荐
- bzoj 3231: [Sdoi2008]递归数列【矩阵乘法】
今天真是莫名石乐志 一眼矩阵乘法,但是这个矩阵的建立还是挺有意思的,就是把sum再开一列,建成大概这样 然后记!得!开!long!long!! #include<iostream> #in ...
- P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- sshd服务器搭建管理和防止暴力破解
1.1 Linux服务前期环境准备,搭建一个RHEL7环境 1.2 sshd服务安装-ssh命令使用方法 1.3 sshd服务配置和管理 1.4 防止SSHD服务暴力破解的几种方式 1.1 Linux ...
- mysql——免安装配置
1.下载 (1)下载地址:https://dev.mysql.com/downloads/mysql/ (2)选择下载 2.配置环境变量 (1)解压目录:D:\mysql-8.0.16-winx64 ...
- Linux环境下安装JDK并配置环境变量
首先查看是否已经安装了JDK并配置环境变量. [root@dhcc_plat opt]# java -version -bash: java: command not found [root@dhcc ...
- 向listview控件中添加数据库数据
//连接字符串 string str = "Data Source=.;Initial Catalog=mu;User ID=sa;Password=111"; //创建数据库连接 ...
- 13 继续C#中的方法,带返回值的方法介绍
在这一个练习中,我们要使用带返回值的方法.如果一个方法带返回值,那么它的形式是这样的. 定义一个带返回值的C#方法 static 返回类型 方法名字 (参数类型 参数1的名字,参数类型 参数2的名字) ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- acm练习-day1
描述现在,有一行括号序列,请你检查这行括号是否配对.输入第一行输入一个数N(0<N<=100),表示有N组测试数据.后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于1 ...
- JVM GC调优一则–增大Eden Space提高性能
缘起 线上有Tomcat升级到7.0.52版,然后有应用的JVM FullGC变频繁,在高峰期socket连接数,Cpu使用率都暴增. 思路 思路是Tomcat本身的代码应该是没有问题的,有问题的可能 ...