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到。

Codeforces 548B Mike and Fun的更多相关文章

  1. CodeForces 548B Mike and Fun (模拟)

    题意:给定一个n*m的矩阵,都是01矩阵,然后每次一个询问,改变一个格的值,然后问你最大有数是多少. 析:就是按他说的模拟,要预处理,只要把每行的最大值记下来,当改变时,再更新这一行的最大值. 代码如 ...

  2. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  3. codeforces 547E Mike and Friends

    codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...

  4. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  5. (CodeForces 548B 暴力) Mike and Fun

    http://codeforces.com/problemset/problem/548/B Mike and some bears are playing a game just for fun. ...

  6. Codeforces 798D Mike and distribution - 贪心

    Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...

  7. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  8. Codeforces 798A - Mike and palindrome

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. Codeforces 689C. Mike and Chocolate Thieves 二分

    C. Mike and Chocolate Thieves time limit per test:2 seconds memory limit per test:256 megabytes inpu ...

随机推荐

  1. Git 详解

    1. Git 1.1. Git是何方神圣? Git是用C语言开发的分布版本控制系统.版本控制系统可以保留一个文件集合的历史记录,并能回滚文件集合到另一个状态(历史记录状态).另一个状 态可以是不同的文 ...

  2. 注解与反射 ---Spring与Mybatis等框架的实现原理

    Java中各大框架,无论是AOP 还是 IoC 其基本实现思路都是源自Java 运行时支撑的反射功能, 而反射最基本的一点就是 任何一个类 其在JVM环境中,都有一个对象的Class对象,这个对象提供 ...

  3. POJ 1182 食物链

    G - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  4. salt进程查看插件&salt批量创建用户

    接受key 剔除主机   启动 salt-minion-d     软件包的安装   salt '*' state.sls init.env-init test=true   salt批量创建用户: ...

  5. JS 之性能优化(1)

    了解JS性能优化是学习前端必备的一项技能.下面就简单的列出几点: 1.注意作用域,避免全局查找. 访问全局变量比访问局部变量慢,是因为需要遍历作用域链,查找作用域链需要额外的时间.所以在一个函数中,将 ...

  6. 《图解tcp/ip》读书笔记(一)

           我先讲三句话:        一."万物互联的时代到了."我们生活在这样一个互联网急速发展的时代,也许很快就会发现,你能接触到的一切都可以连接到互联网了,电脑.手机这 ...

  7. 获取技能的成功经验和关于C语言学习的调查 2015528

    内容提要 你有什么技能比大多人(超过90%以上)更好?针对这个技能的获取你有什么成功的经验?与老师博客中的学习经验有什么共通之处? 有关C语言学习的调查 你是怎么学习C语言的?(作业,实验,教材,其他 ...

  8. 20145208 实验五 Java网络编程

    20145208 实验五 Java网络编程 实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务 ...

  9. MVC5 + EF6 + Bootstrap3 (12) 新建数据

    Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-create.html 系列教程:MVC5 + EF6 + ...

  10. js监听键盘回车

    //监听回车 $(document).keydown(function(e) { ) { $("#btnLogin").click(); } }) //input绑定回车 $('# ...