【HDOJ】3275 Light
这就是个简单线段树+延迟标记。因为对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的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
随机推荐
- Java Thread interrupt
现有线程对象threadA,调用threadA.interrupt(),则threadA中interrupted状态会被置成false,很多线程中都是通过isInterrupted()方法来检测线程是 ...
- ###学习《C++ Primer》- 1
点击查看Evernote原文. #@author: gr #@date: 2014-09-30 #@email: forgerui@gmail.com 记录读书过程中一些知识点.可能不系统,:-). ...
- 第九篇、UITabbar增加类别用来标红点
1.系统中只有设置bage的方式,设置为nil也是为一个红点,但是很大,并不是我们需要的 2.扩充标红点的方法 (常用于有新的动态提示标志) #import <UIKit/UIKit.h> ...
- 12天学好C语言——记录我的C语言学习之路(Day 12)
12天学好C语言--记录我的C语言学习之路 Day 12: 进入最后一天的学习,用这样一个程序来综合考量指针和字符串的关系,写完这个程序,你对字符串和指针的理解应该就不错了. //输入一个字符串,内有 ...
- HTML5 Canvas 绘制时钟
网上会看到很多绘制的时钟,看代码也是云里雾里,自学了下Canvas,觉得不难,就自己做了一个. 先看一下截图: 比较简陋,但是该有的都有了,样式只加了个阴影. html代码就不贴了,就一个canvas ...
- Microsoft Excel Sheet/表格 制作折线图
Microsoft Excel Sheet/表格 制作折线图 虽然比较简单,但是仍然需要稍微花一点功夫. 1.制作好表格数据 2.先将数据选定(不包括 横座标的 年月日或其他的刻度 的那一列) 3.插 ...
- JS判断上传图片格式是否正确
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 使用curl获取网站的http的状态码
发布:thebaby 来源:net [大 中 小] 本文分享一例shell脚本,一个使用curl命令获取网站的httpd状态码的例子,有需要的朋友参考下.本文转自:http://www.j ...
- Linux系统架设支持自助开通Shado wsocks及VPN前端的教程
程序实现:通过网页端注册,自助开通VPN帐号及Shadowsocks帐号.并可实现流量统计 系统要求 Debian 6 x64 纯净系统 by: Lop ①配置环境 apt-get updateapt ...
- 原生javascript效果:无缝滚动
<style type="text/css"> #con {width:400px; padding:10px; margin:20px auto; text-alig ...