B. Mike and Fun
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes.

They play for q rounds. In each round, Mike chooses a bear (i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.

Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.

Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round.

Input

The first line of input contains three integers nm and q (1 ≤ n, m ≤ 500 and 1 ≤ q ≤ 5000).

The next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes).

The next q lines contain the information about the rounds. Each of them contains two integers i and j(1 ≤ i ≤ n and 1 ≤ j ≤ m), the row number and the column number of the bear changing his state.

Output

After each round, print the current score of the bears.

Sample test(s)
input
5 4 5
0 1 1 0
1 0 0 1
0 1 1 0
1 0 0 1
0 0 0 0
1 1
1 4
1 1
4 2
4 3
output
3
4
3
3
4

——————————————————————————————————————————————————————————————

本来挺简单的一道题,我却写得很复杂。不善于分析复杂度是硬伤啊!

———————————————————————————————————————————————————————————————

1. 读入复杂度N×M,这说明若果算法在与N×M相当的复杂度内是可以AC的

2. 对于一个长为L的01序列,可在O(L)时间内得到连续1的最大数目 (the maximum number of consecutive "1" s in it)。

int a[MAX_N];
int ans=, cur=;
for(int i=; i<=n; i++){
if(a[i]==){
cur++;
ans=max(ans, cur);
}
else cur=;
}

这个基本方法我却没想到,把简单问题搞得过于复杂,没怎么仔细考虑就决定用线段树维护,而且这线段树写得还很“丑陋”。

#include<bits/stdc++.h>
using namespace std;
const int MAX_N=;
//SegT_1
struct node{
int lb, rb;
int ma;
}T[MAX_N][MAX_N<<];
void renew(int i, int id){
node &now=T[i][id];
if(now.ma){
now.lb=now.rb=now.ma=;
}
else{
now.ma=now.lb=now.rb=;
}
}
void _renew(int i, int id, int L, int R){
node &fa=T[i][id], &ls=T[i][id<<], &rs=T[i][id<<|];
fa.lb=ls.lb==(R-L+)>>?ls.lb+rs.lb:ls.lb;
fa.rb=rs.rb==(R-L+)>>?rs.rb+ls.rb:rs.rb;
fa.ma=max(max(ls.ma, rs.ma), ls.rb+rs.lb);
}
void insert(int i, int id, int L, int R, int pos){
if(L==R){renew(i, id);return;}
int mid=(L+R)>>;
if(pos<=mid) insert(i, id<<, L, mid, pos);
else insert(i, id<<|, mid+, R, pos);
_renew(i, id, L, R);
}
//SegT_2
int mx[MAX_N<<];
void _insert(int id, int L, int R, int pos, int val){
if(L==R){mx[id]=val; return;}
int mid=(L+R)>>;
if(pos<=mid) _insert(id<<, L, mid, pos, val);
else _insert(id<<|, mid+, R, pos, val);
mx[id]=max(mx[id<<], mx[id<<|]);
}
//
int main(){
freopen("in", "r", stdin);
int N, M, Q;
cin>>N>>M>>Q;
int a;
for(int i=; i<=N; i++){
for(int j=; j<=M; j++)
if(cin>>a, a) insert(i, , , M, j);
_insert(, , N, i, T[i][].ma);
}
int i, j;
while(Q--){
cin>>i>>j;
insert(i, , , M, j);
_insert(, , N, i, T[i][].ma);
cout<<mx[]<<endl;
}
return ;
}

甚至于我还在想能不能在不牺牲时间复杂度的情况下将树状数组改造成支持单点改的RMQ(不想写两个线段树~<^>~),但我真是SB了。这题暴力的复杂度O(q(n+m)) (读入复杂度O(nm)相比之下可忽略了), 1e6的量级,1s完全可过了(况且都说Codeforces的机器快~)。

-----------------------------------------------------------------------------------------------------------------------

写复杂的原因呢,就是我不熟悉求一个01串内最长连续“1”的长度朴素的解法应该怎么写(怎么可以连这都不知道呢~),最终踏上了线段树的歧途。

(×&^伤#¥%), 最要紧的还是要把一些基础姿势get到。

最新文章

  1. MapReduce实现手机上网流量分析(业务逻辑)
  2. MySQL 性能优化 30个数据库设计的最佳实践
  3. oracle 科普
  4. python学习笔记-(七)python基础--集合、文件操作&amp;函数
  5. 在C语言源程序中的格式字符与空格等效
  6. CentOS7 Debian 8 安装VMware-tools
  7. 3月3日(4) Binary Tree Inorder Traversal
  8. jquery仿ios日期时间插件
  9. log4j级别输出
  10. 查看文件系统类型的Linux命令
  11. mysql-bin.000001文件的来源及处理方法
  12. Java &quot;==和equals区别&quot;
  13. FaceNet---深度学习与人脸识别的二次结合
  14. 大话Python正则表达式
  15. Python机器学习 (Python Machine Learning 中文版 PDF)
  16. iview form 表单的怪异小BUG
  17. Linux:rm:du命令
  18. jmeter -- 在beanshell中拿到请求body参数和header参数
  19. MFC中的一般经验之谈2
  20. PL/SQL之游标的使用

热门文章

  1. Android 屏幕适配(二)增强版百分比布局库(percent-support-lib)
  2. git config配置文件
  3. Linux内核启动
  4. puer工具的使用
  5. 学习Shell脚本编程(第4期)_在Shell程序中的使用变量
  6. Java系列:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
  7. 20145208 实验五 Java网络编程
  8. MVVM开源框架Knot.js 教程2 - 大幅改变前端框架开发体验的Debugger
  9. Java学习笔记(二十)——Java 散列表_算法内容
  10. css清楚浮动的几种常用方法