Mail.Ru Cup 2018 Round 2 B. Alice and Hairdresser (bitset<> or 其他)
题意:
给出你序列 a,在序列 a 上执行两种操作;
① 0 :查询有多少连续的片段[L,...,R],满足 a[L,...,R] > l;
② 1 p d :将第 p 个数增加 d;
思路:
int n,m,l;
ll a[maxn];
int fa[maxn];///a[L,...,x] > l 的最小的L;
/**
_bit[0][x]:a[x] > l,_bit[0][x]=1,反之为0;
_bit[1][x]:a[L,...,R] > l,_bit[L]=1,_bit[L+1,...,R]=0,
即满足条件的连续片段[L,...R],只将开始位置L赋为1
*/
bitset<maxn>_bit[];
在 m 次操作中,只有出现 a[p] ≤ l && a[p]+d > l 时,才有可能合并区间;
即a[L,...,p-1] > l , a[p+1,...,R] > l ,现在 a[p]+d > l 使得 [L,...,p-1] 与 [p+1,...,R] 可以合并成 a[L,...R] > l;
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+; int n,m,l;
ll a[maxn];
int fa[maxn];///a[L,...,x] > l 的最小的L;
/**
_bit[0][x]:a[x] > l,_bit[0][x]=1,反之为0;
_bit[1][x]:a[L,...,R] > l,_bit[L]=1,_bit[L+1,...,R]=0,
即满足条件的连续片段[L,...R],只将开始位置L赋为1
*/
bitset<maxn>_bit[]; int Find(int x)
{
return x == fa[x] ? x:fa[x]=Find(fa[x]);
}
void Solve()
{
for(int i=;i <= n;++i)
{
if(a[i] <= l)
continue; _bit[].set(i); int x=i;
if(_bit[][i-])
x=Find(i-);
fa[i]=x;
_bit[].set(x);
}
for(int i=;i <= m;++i)
{
int que;
scanf("%d",&que);
if(!que)
printf("%d\n",_bit[].count());
else
{
int p,d;
scanf("%d%d",&p,&d);
if(_bit[][p])
continue; a[p] += d;
if(a[p] <= l)
continue; _bit[].set(p); int x=p;
if(_bit[][p-])
x=Find(p-);///查找a[L,...,p]>l的最小的L;
fa[p]=x;
///[L,...,R]
///查找最大的R
for(int j=p+;_bit[][j];++j)///每个数顶多遍历两边
{
fa[j]=x;///合并[L,...,R]到x上
_bit[][j]=;
}
_bit[].set(x);
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&l);
for(int i=;i <= n;++i)
{
fa[i]=i;
scanf("%lld",a+i);
}
Solve(); return ;
}
ac后看了一下standings,看到了 tourist ,然后,偷偷%了一眼大神的代码;
啊,简洁+高效,tql;
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+; int n,m,l;
ll a[maxn]; void Solve()
{
int ans=;
for(int i=;i <= n;++i)
if(a[i] > l && a[i-] <= l)///[L,..,R]只在L处使得ans+1
ans++; for(int i=;i <= m;++i)
{
int que;
scanf("%d",&que);
if(!que)
printf("%d\n",ans);
else
{
int p,d;
scanf("%d%d",&p,&d);
if(a[p] > l)
continue;
a[p] += d; ///[L,..,R]只在L处使得ans+1
if(a[p] > l && a[p-] <= l)
ans++; ///如果p使得[L,...,p-1]与[p+1,...,R]合并
///[L,...,p-1]使得ans+1,[p+1,...,R]使得ans+1
///所以ans需-1
if(a[p] > l && a[p+] > l)
ans--;
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&l);
for(int i=;i <= n;++i)
scanf("%lld",a+i);
Solve(); return ;
}
Mail.Ru Cup 2018 Round 2 B. Alice and Hairdresser (bitset<> or 其他)的更多相关文章
- Mail.Ru Cup 2018 Round 2 Solution
A. Metro Solved. 题意: 有两条铁轨,都是单向的,一条是从左往右,一条是从右往左,Bob要从第一条轨道的第一个位置出发,Alice的位置处于第s个位置,有火车会行驶在铁轨上,一共有n个 ...
- 【Mail.Ru Cup 2018 Round 2 B】 Alice and Hairdresser
[链接] 我是链接,点我呀:) [题意] [题解] 因为只会增加. 所以. 一开始暴力算出来初始答案 每次改变一个点的话. 就只需要看看和他相邻的数字的值就好. 看看他们是不是大于l 分情况增加.减少 ...
- Mail.Ru Cup 2018 Round 3 B. Divide Candies
题目链接 分析一下题意可以得到题目要求的是满足下面这个 公式的不同的i,ji,ji,j的方案数; 即(i2+j2)mod   m=0 (n ≤ ...
- Mail.Ru Cup 2018 Round 3
A:签到 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- Mail.Ru Cup 2018 Round 2
A:阅读理解. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
- [codeforces Mail.Ru Cup 2018 Round 3][B Divide Candies ][思维+数学]
https://codeforces.com/contest/1056/problem/B 题意:输入n,m 求((a*a)+(b*b))%m==0的(a,b)种数(1<=a,b<= ...
- [codeforces Mail.Ru Cup 2018 Round 1 D][ xor 操作]
http://codeforces.com/contest/1054/problem/D 题目大意:一个序列a1 a2...an,可以对若干个元素进行取反,使所得的新序列异或和为0的区间个数最多. 题 ...
- Mail.Ru Cup 2018 Round 3 Solution
A. Determine Line Water. #include <bits/stdc++.h> using namespace std; ]; int main() { while ( ...
- Mail.Ru Cup 2018 Round 1
A. Elevator or Stairs? 签. #include <bits/stdc++.h> using namespace std; ]; int main() { while ...
随机推荐
- phpcms万能字段的使用方法
今天想做一个单选的字段,里面要使用别的字段,于是研究了一下万能字段!刚开始使用的时候,在网上,论坛里找了好久,没发现一个贴子有针对万能字段的使用说明,官方的例子里也只有一个调用字段本身值的变量 {FI ...
- span元素和div元素的浮动效果
首先看一段代码: <style> #right {margin: 10px;float:right;color:red;} #left {float:left;color:blue;} & ...
- Mathcad 是一种工程计算软件,主要运算功能:代数运算、线性代数、微积分、符号计算、2D和3D图表、动画、函数、程序编写、逻辑运算、变量与单位的定义和计算等。
Mathcad软件包Mathcad是由MathSoft公司(2006 年4 月被美国PTC收购)推出的一种交互式数值计算系统. Mathcad 是一种工程计算软件,作为工程计算的全球标准,与专有的计算 ...
- laravel 图片
/** * 缩略图上传 */ public static function addPic() { $inputData = request()->all(); $rules = [ 'main_ ...
- 将数组对象转换成DataSet
public static DataSet ObjectArrayToDataSet(object[] objArr) { if (objArr.Length == 0) return null; D ...
- Unrecognised tag: 'build'
[ERROR] [ERROR] Some problems were encountered while processing the POMs:[ERROR] Malformed POM H:\ec ...
- PLAY2.6-SCALA(七) Streaming HTTP response
1.从HTTP1.1开始,服务端为了在single connection下对HTTP请求及响应提供服务,需要在response中提供响应的Content-Length. 默认情况下,不需要显示的指明C ...
- JavaScript--微博发布效果
效果图: 实现思路: 当发布按钮被点击时,又会分为三种情况 1.如果输入的内容为空,弹出提示:不能发布空微博 2.如果输入的文字超过120,弹出提示,微博内容不能超过120 3.正常发布微博到列表里 ...
- 【C++】为什么INT_MIN不是直接写成-2147483648(转载)
最近在编程中遇到一个问题: #include <iostream> using namespace std; int main() { int n = -2147483648; //cou ...
- poj 3280【区间dp】
poj 3280 题意:给定一个字符串和每个字符删去和增加的代价,求使字符串变成回文串操作所需的最小代价. 题解:哇!开心!终于亲自做对了!做完这两题这个就回了.uva10739 uva 10453 ...