【题解】CF1426D Non-zero Segments
\(\text{Solution:}\)
若\([l,r]\)子段和是\(0,\)则\(sum[r]=sum[l-1].\)
于是我们可以考虑维护当前哪一个前缀和出现过。对于区间\([l,r]\)若其子段和为\(0\)则在\(r-1\)的地方插入一个\(+\infty\)即可。
初始化要把\(0\)赋值为出现过。
#include<bits/stdc++.h>
using namespace std;
int n,a[500010];
map<long long,bool>mp;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
mp[0]=true;
long long s=0;
int ans=0;
for(int i=1;i<=n;++i){
s+=a[i];
if(mp[s]){
ans++;
mp.clear();
mp[0]=true;
s=a[i];
}
mp[s]=true;
}
cout<<ans<<endl;
return 0;
}
```cpp
【题解】CF1426D Non-zero Segments的更多相关文章
- LeetCode题解之Number of Segments in a String
1.题目描述 2.题目分析 找到字符串中的空格即可 3.代码 int countSegments(string s) { ){ ; } vector<string> v; ; i < ...
- 题解-CF1389F Bicolored Segments
题面 CF1389F Bicolored Segments 给 \(n\) 条线段 \([l_i,r_i]\),每条有个颜色 \(t_i\in\{0,1\}\),求最多选出多少条线段,使没有不同颜色的 ...
- CodeForces 430A Points and Segments (easy)(构造)题解
题意:之前愣是没看懂题意...就是给你n个点的坐标xi,然后还规定了Li,Ri,要求给每个点染色,每一组L,R内的点红色和黑色的个数不能相差大于1个,问你能不能染成功,不能输出-1,能就按照输入的顺序 ...
- PAT甲题题解-1104. Sum of Number Segments (20)-(水题)
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- POJ3304:Segments——题解
http://poj.org/problem?id=3304 题目大意:给n条线段,求是否存在一条直线,将所有线段投影到上面,使得所有投影至少交于一点. ——————————————————————— ...
- [CF846C]Four Segments题解
我们暴力枚举一下\(delim_{1}\) 然后对于每个\(delim_{1}\),O(n)扫一遍+前缀和求出最大\(delim_{0}\)和\(delim_{2}\),然后记录一下它们的位置就行啦 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are whit ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- usaco training 4.1.3 fence6 题解
Fence Loops题解 The fences that surround Farmer Brown's collection of pastures have gotten out of cont ...
随机推荐
- android,在java代码中,如何给textview设置不同的颜色,以及引用color中资源设置颜色
textV.setTextColor(Color.parseColor("#FF0000")); <pre name="code" class=" ...
- 腾讯大牛半年心血高级编程PDF,帮你轻松构建企业级Web应用
毫无疑问,Java 是这些年来最流行的编程语言之一.它无处不在一计算机. 手机.网站以及各种嵌入式设备中都存在着大量的Java 应用程序,而其中应用最为广泛的应该就是Java EE Web应用程序(以 ...
- 利用GetPrivateProfileString读取配置文件(.ini)
利用GetPrivateProfileString读取配置文件(.ini) 配置文件中经常用到ini文件,在VC中其函数分别为: 写入.ini文件:bool WritePrivateProfileSt ...
- JavaScript 时间都去那了(操作时间字符串加减时间)
---给时间充点时间吧--- 时间转换函数: function dateFormat(date, format) { date = new Date(date); var o = { 'M+': da ...
- Currency Exchange(SPFA判负环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- 2020 最新python入门知识
1. 基础语法 1.1 注释 在编写代码的时候,有些代码不需要执行或增加代码说明,那么就需要用到注释了. 被注释的文本或代码是不会被执行的. 注释可以使用如下三种方式: # 号 # 第一个注释,本行代 ...
- Agumaster点个按钮后台刷新前台一步步出状态
--2020年5月4日--
- Linux:使用SecureCRT来上传和下载文件
SecureCRT自带的有几种上传下载功能,SecureCRT下的文件传输协议有以下几种:ASCII.Xmodem.Ymodem.Zmodem. ASCII:这是最快的传输协议,但只能传送文本文件. ...
- python基础一(安装、变量、循环、git)
一.开发语言分类 系统的开发语言有java.c++.c#.python.ruby.php等等,开发语言可分为编译型语言和解释型语言. 编译型语言就是写好代码之后就把代码编译成二进制文件,运行的时候运行 ...
- UI自动化页面需要选择上传文件
UI自动化页面上传文件,百度查的时候说有4中方法,简便的方法是安装SendKeys,但是百度说这个只支持python2.7的 我的python版本是3.7的,目前还只碰到了非<input typ ...