【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 ...
随机推荐
- gradle使用国内源
// 设置 maven 库地址 repositories { maven { url 'http://maven.oschina.net/content/groups/public/' } } ...
- IO流05_OutputStream和Writer输出流
[输出流中的字节流和字符流] [OutPutStream和Writer] [ OutputStream和Writer中包含的方法 ] void write(int c) 将指定的字节/字符 ...
- Codevs 3289 花匠 2013年NOIP全国联赛提高组
3289 花匠 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 花匠栋栋种了一排花,每株花都 ...
- BootstrapDialog.show函数底层简化
平台用的全部都是BootStrapDialog的弹窗,然后美工设计了一个统一的样式,每次写的时候,都要对其进行样式重写:写吐了快,所以对BootStrap.底层做了修改: 也就是说,只要你要写的界面包 ...
- sharepoint 脚本 强迫以管理员权限运行
#region 关键代码:强迫以管理员权限运行 $currentWi = [Security.Principal.WindowsIdentity]::GetCurrent() $currentWp = ...
- CSS浮动特性总结
1.假设现在CSS中没有浮动(float)属性,那么会变成一个什么样子.我们会发现,目前流行采用浮动方法实现的无论是分栏布局,还是列表排列我们都可以用其他一些CSS属性(不考虑table)代替实现,唯 ...
- Aspose.Cells.dll引用导入导出Excel
Aspose.Cells 导入导出EXCEL 文章出处:http://hi.baidu.com/leilongbing/item/c11467e1819e5417595dd8c1 修改样式 ...
- [HttpClient]SSL双向实例
package com.jerry.httpclient; import java.io.File; import java.io.FileInputStream; import java.secur ...
- UDP TCP应用场景
作者:陈硕链接:https://www.zhihu.com/question/20060141/answer/26735814来源:知乎著作权归作者所有,转载请联系作者获得授权. UDP 的使用范围很 ...
- mysql 远程连接 1045 Access denied for user 'root'@'XX.XX.XX.XX' (using password:YES)
用户名/密码错误,需要输入开放远程时设置的密码