情人节的电灯泡
题号:NC15172
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
情人节到了,小芳和小明手牵手,打算过一个完美的情人节,但是小刚偏偏也来了,当了一个明晃晃的电灯泡,小明很尴尬,就和小刚说,我交给你个任务,你完成了我俩就带你玩,否则你就回家吧。小刚很有当单身狗的觉悟,他坚决不想让小明过好情人节,同为单身狗的你能帮帮他吗?现在有一个n×n(1 <= n <= 1000)的格子,每一个格子都有一个电灯泡,可能是亮的,也可能是灭的(1代表亮, 0代表灭),现在有两种操作,一种是给你一个坐标,对于那个坐标上的灯泡,如果他是亮的,那么熄灭他,反之如果他是灭的,那么打开它。第二种操作是给你两个坐标,第一个坐标代表一个子矩阵的左上角,另一个坐标是右下角,请你求出当前子矩阵中有多少个灯泡是亮着的。燥起来吧!!!单身狗们!!!!
输入描述:
第一行两个整数,n(1 <= n <= 1000)和m(1 <= m <= 100000),分别代表正方形格子的边长和询问次数。 接下来n行,每一行有n个bool形数字(0或1),代表灯泡的状态。 接下来m行,每一行第一个数字f(1或2)代表操作的类型,如果f是1,那么接下来输入一个坐标(x, y)(1 <= x, y <= n),对于当前位置的灯泡状态进行改变,如果是2,那么接下来输入两个坐标(x1, y1)(1 <= x1, y1 <= n), (x2, y2)(1 <= x2, y2 <= n),确定子矩阵的位置,输出子矩阵中亮着的灯泡数量并换行。
输出描述:
对于每一个2操作,输出子矩阵中亮着的灯泡数量并换行。
示例1
输入
6 4 0 0 1 0 1 0 1 0 1 1 0 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 2 2 2 4 5 1 1 1 2 1 1 6 6 1 2 6
输出
4 14
/*
*@Author: GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
// return a>b?a:b;
//}
//inline double min(double a,double b){
// return a<b?a:b;
//}
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
//void Fire(){
// queue<node> p;
// p.push({fx,fy,0});
// memset(fire, -1, sizeof(fire));
// fire[fx][fy]=0;
// while(!p.empty()){
// node temp=p.front();
// p.pop();
// for(int i=0;i<8;i++){
// int x=temp.x+xd[i];
// int y=temp.y+yd[i];
// if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
// continue;
// }
// fire[x][y]=temp.val+1;
// p.push({x,y,temp.val+1});
// }
// }
//}
//int bfs(){
// queue<node> p;
// memset(vis, 0, sizeof(vis));
// p.push({sx,sy,0});
// while (!p.empty()) {
// node temp=p.front();
// vis[temp.x][temp.y]=1;
// p.pop();
// for(int i=0;i<4;i++){
// int x=temp.x+xd[i];
// int y=temp.y+yd[i];
// if(x<0||x>=n||y<0||y>=m) continue;
// if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
// if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
// p.push({x,y,temp.val+1});
// }
// }
// return -1;
//}
//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
// ll ans=0;
// for(int i=0;i<s.size();i++){
// ans*=bas;
// ans+=int(s[i]);
// ans%=mod1;
// }
// return ans;
//}
//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
// p1[0]=1;
// p2[0]=1;
// for(int i=1;i<=505;i++){
// p1[i]=p1[i-1]*bas1;
// p2[i]=p2[i-1]*bas2;
//
// }
//}
//void Hash(){
// _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
// for(int i=1;i<=n;i++){ //前缀和
// for(int j=1;j<=m;j++){
// _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
// }
// }
// for(int i=1;i<=n;i++){ //二维前缀和
// for(int j=1;j<=m;j++){
// _hash[i][j]+=_hash[i-1][j]*bas2;
// }
// }
//
//}
//int pre[1010];
//int in[1010];
//int post[1010];
//int k;
//struct node{
// int value;
// node *l,*r;
// node (int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
//};
//void builttree(int l,int r,int &t,node * &root){
// int flag=-1;
// for(int i=l;i<=r;i++){
// if(in[i]==pre[t]){
// flag=i;
// break;
// }
// }
// if(flag==-1) return;
// root=new node(in[flag]);
// t++;
// if(flag>l) builttree(l,flag-1,t,root->l);
// if(flag<r) builttree(flag+1,r,t,root->r);
//
//}
//void preorder(node *root){
// if(root!=NULL)
// {
// post[k++]=root->value;
// preorder(root->l);
// preorder(root->r);
//
// }
//}
//void inorder(node *root){
// if(root!=NULL)
// {
// inorder(root->l);
// post[k++]=root->value;
// inorder(root->r);
//
// }
//}
//void postorder(node *root){
// if(root!=NULL)
// {
// postorder(root->l);
// postorder(root->r);
// post[k++]=root->value;
// }
//}
//#define MAX 1000000
//int tree[MAX<<2];
//void pushup(int rt){
// tree[rt]=tree[rt<<1]+tree[rt<<1|1];
//}
//void update(int rt,int i,int val,int l,int r)
//{
// if(r==l) {
// tree[rt]+=val;
// return;
// }
// if(i<=mid){
// update(ls,i,val,l,mid);
// }
// else {
// update(rs,i,val,mid+1,r);
// }
// pushup(rt);
//}
//int query(int rt,int l,int r,int k){
// if(l==r) return l;
// if(tree[rt<<1|1]>=k)
// return query(rs,mid+1,r,k);
// else return query(ls,l,mid,k-tree[rt<<1|1]);
//}
//int main(){
// int t;
// cin>>t;
// while (t--) {
// int n,m;
// cin>>n>>m;
// mms(tree,0);
// for(int i=1;i<=n;i++){
// int x;
// cin>>x;
// update(1,x,1,1,MAX);
// }
// while (m--) {
// string s;
// int x;
// cin>>s>>x;
// if(s[0]=='q') {
// cout<<query(1,1,MAX,x)<<endl;
// }
// else if(s[0]=='i'){
// update(1,x,1,1,MAX);
// }
// else {
// update(1,x,-1,1,MAX);
// }
// }
// }
//}
int vis[1010][1010]; // 状态数组
int sum[1010][1010]; //树状数组的维护数组
int n,m;
int lowbit(int x){
return x&(-x);
}
void updata(int x,int y,int c) //更新状态
{
for(int i=x;i<=n;i+=lowbit(i)){
for(int j=y;j<=n;j+=lowbit(j)){
sum[i][j]+=c;
}
}
}
int getsum(int x,int y){ /
int ans=0;
for(int i=x;i>0;i-=lowbit(i)){
for(int j=y;j>0;j-=lowbit(j)){
ans+=sum[i][j];
}
}
return ans;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>vis[i][j];
if(vis[i][j])
updata(i,j,1);
}
}
while (m--) {
int a,x1,x2,y1,y2;
cin>>a>>x1>>y1;
if(a==1){
if(vis[x1][y1]){
vis[x1][y1]=0;
updata(x1,y1,-1);
}
else {
vis[x1][y1]=1;
updata(x1,y1,1);
}
}
else {
cin>>x2>>y2;
int ans=getsum(x2,y2)+getsum(x1-1,y1-1)-getsum(x2,y1-1)-getsum(x1-1,y2);
cout<<ans<<endl;
}
}
}