这就是个简单线段树+延迟标记。因为对bool使用了~而不是!,wa了一下午找不到原因。

 /* 3275 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
bool f;
int ln, tot;
} node_t; const int maxn = 1e5+;
char s[maxn];
node_t nd[maxn<<];
int L, R; inline void PushUp(int rt) {
nd[rt].ln = nd[rt<<].ln + nd[rt<<|].ln;
} inline void PushDown(int rt) {
if (nd[rt].f) {
int lb = rt<<;
int rb = lb|;
nd[lb].f = !nd[lb].f;
nd[lb].ln = nd[lb].tot - nd[lb].ln;
nd[rb].f = !nd[rb].f;
nd[rb].ln = nd[rb].tot - nd[rb].ln;
nd[rt].f = false;
}
} void Build(int l, int r, int rt) {
nd[rt].f = false;
nd[rt].tot = r - l + ;
if (l == r) {
nd[rt].ln = (s[l] == '');
return ;
} int mid = (l + r) >> ;
Build(lson);
Build(rson); PushUp(rt);
} int Query(int l, int r, int rt) {
if (l == r)
return l; PushDown(rt);
int mid = (l + r) >> ;
int ret; if (nd[rt<<].ln == nd[rt<<].tot)
ret = Query(rson);
else
ret = Query(lson);
PushUp(rt); return ret;
} void Update(int l, int r, int rt) {
if (L<=l && R>=r) {
nd[rt].f = !nd[rt].f;
nd[rt].ln = nd[rt].tot - nd[rt].ln;
return ;
} PushDown(rt);
int mid = (l + r) >> ; if (R <= mid) {
Update(lson);
} else if (L > mid) {
Update(rson);
} else {
Update(lson);
Update(rson);
} PushUp(rt);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, l;
int ans, k; while (scanf("%d %d",&n,&l)!=EOF && (n||l)) {
scanf("%s", s+);
Build(, n, );
if (nd[].tot == nd[].ln) {
puts("");
continue;
}
if (l == ) {
puts("-1");
continue;
} ans = ;
while () {
++ans;
k = Query(, n, );
L = k;
R = L + l - ;
#ifndef ONLINE_JUDGE
// printf("k = %d\n", k);
#endif
if (R > n) {
ans = -;
break;
}
Update(, n, );
if (nd[].tot == nd[].ln)
break;
}
printf("%d\n", ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】3275 Light的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. html表单-双向绑定

    潜水多年.一直是只看不评不写多年,每每看到各位大牛分享的经典文章都是默默的收藏,对大牛技术分享技术表示感谢,这么多年从博客园学到了很多. 这段时间项目告一段落. 正好这段时间相对清闲,我也整理一些常用 ...

  2. php ticks 调试应用

    declare(ticks=1); register_tick_function('do_profile'); register_shutdown_function('show_profile'); ...

  3. dorado需要的包

    创建dorado示例中心项目WEB-INF下的lib里的包有很多,包括连接数据库的完整的包,新建的项目,可以直接从这里面拷贝包. 当然如果需要连接mySql数据库,还需要手动导入mySql的包.

  4. 2016ACM竞赛训练暑期课期末考试 a题

    描述 给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合. 输入 第一行是一个正整数n(n<=600).第二行是n个不同的整数,相邻两个整数之间用单个空格隔开. ...

  5. C++ Txt文档写入

    void writefile(student *s,int n,string filepath){ ofstream myfile; if(!myfile)//有错误 { exit(1); }else ...

  6. 暑假集训(2)第四弹 ----- 敌兵布阵(hdu1166)

    D - 敌兵布阵 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit ...

  7. java 中hashcode和equals 总结

    一.概述            在Java中hashCode的实现总是伴随着equals,他们是紧密配合的,你要是自己设计了其中一个,就要设计另外一个.当然在多数情况下,这两个方法是不用我们考虑的,直 ...

  8. JS 版的pnp in_array($str,$arr)

    var a = Array(1,2,3,4,5); function in_array(search,array){ for(var i in array){ if(array[i]==search) ...

  9. 【HeadFirst设计模式】10.状态模式

    定义: 允许对象在内部状态改变时改变它 行为,对象看起来好像修改了它的类. OO原则: 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程 为交互对象之间的松耦合设计而努力 类应该对扩展开放, ...

  10. Stable Matching 稳定匹配 婚姻算法 shapley 算法

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051286.html 稳定匹配问题:有N男N女,每个人对于异性都一个排名,先需要得到一种稳 ...