E. Tracking Segments

给定初始长度为n,且全为0的序列a,然后给出m个线段,如果一个线段中1的个数严格大于0的个数,那么该线段称为一个漂亮线段,现在给出q次操作,每次操作使得序列a中位置x上的0变为1,请你求出第一次使得所有线段中出现漂亮线段的询问

题解:二分答案

  • 容易发现答案具有单调性,所以我们可以考虑二分答案
  • 我们发现在check的时候只要提前预处理好10个数前缀和数组即可,然后即可\(O(m)\)检验合法性
  • 分析其时间复杂度为:\(O(m\times logq)\)
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10, M = 4e5 + 10; int n, m, q;
int L[N], R[N];
int qry[N]; bool check(int mid)
{
vector<int> a(n + 10), pre0(n + 10), pre1(n + 10);
for (int i = 1; i <= mid; ++i)
a[qry[i]] = 1;
for (int i = 1; i <= n; ++i)
{
pre1[i] = pre1[i - 1] + (a[i] == 1);
pre0[i] = pre0[i - 1] + (a[i] == 0);
}
for (int i = 1; i <= m; ++i)
{
if (pre1[R[i]] - pre1[L[i] - 1] > pre0[R[i]] - pre0[L[i] - 1])
return true;
}
return false;
} void solve()
{
cin >> n >> m;
for (int i = 1; i <= m; ++i)
cin >> L[i] >> R[i];
cin >> q;
for (int i = 1; i <= q; ++i)
cin >> qry[i];
int l = 1, r = q;
bool flag = false;
while (l <= r)
{
int mid = l + r >> 1;
if (check(mid))
{
r = mid - 1;
flag = true;
}
else
l = mid + 1;
}
if (flag)
cout << l << endl;
else
cout << -1 << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}

F1. Omsk Metro (simple version)

给一棵有根树,每次给定点 v,询问从根节点v 路径上的某个连续子段的和能否等于k

题解:\(dp\)——最大/最小子段和

  • 容易发现从根节点到\(v\)的路径上子段和的取值范围为\([最小子段和,最大字段和]\)
  • 所以我们可以\(dp\)即可
  • 状态转移时有3种选择:
  • 选择接在前面的子段上
  • 不接在前面的字段上,变成一个新的子段
  • 选择成为空子段
  • 查询时验证\(k\)是否在范围内即可
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10, M = 4e5 + 10; int n; void solve()
{
cin >> n;
int idx = 1;
vector<int> fmin(n + 10, INF), fmax(n + 10, -INF);
vector<int> mi(n + 10, INF), mx(n + 10, -INF);
fmin[1] = 0;
fmax[1] = 1;
mi[1] = min({mi[1], fmin[1], 0LL});
mx[1] = max({mx[1], fmax[1], 0LL});
for (int i = 1; i <= n; ++i)
{
char op;
int u, v, k, x;
cin >> op;
if (op == '+')
{
cin >> u >> x;
idx++;
fmin[idx] = min({fmin[u] + x, x, 0LL});
fmax[idx] = max({fmax[u] + x, x, 0LL});
mi[idx] = min(mi[u], fmin[idx]);
mx[idx] = max(mx[u], fmax[idx]);
}
else
{
cin >> u >> v >> k;
if (mi[v] <= k && mx[v] >= k)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
}
signed main(void)
{
Zeoy;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}

Codeforces Round 881 (Div的更多相关文章

  1. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. P1543 [POI2004] SZP 题解

    P1543 [POI2004] SZP 题解 传送门. 题目简述 有 \(n\) 个人,每个人都会监视另一个人,要求选出尽可能多的同学,使得选出的每一名同学都必定会被监视到.且选出的同学不可再监视其他 ...

  2. 2024年常用的Net web框架

    ASP.NET Core 框架声明:是微软推出的新一代开源.跨平台的 Web 应用框架,用于构建高性能.现代化的 Web 应用程序. 官网地址:https://dotnet.microsoft.com ...

  3. Vue3——Pinia+Pinia持久化

    Pinia 安装 Pinia npm install pinia 创建一个 pinia 实例 (根 store) 并将其传递给应用 // store/index.ts // 仓库大仓库 import ...

  4. Python实现多维傅里叶变换

    技术背景 在前面一篇文章中,我们介绍了一维离散傅里叶变换和快速傅里叶变换的基本原理和简单的代码实现.本文补充一个多维傅里叶变换的场景,以及简单的Python实现. 二维傅里叶变换 首先回顾一下上一篇文 ...

  5. Java项目笔记(二)

    一.分页待解决的问题 分页是在service层实现的 在controller层和service层同时写了这句代码 PageHelper.startPage(Integer.valueOf(pageNo ...

  6. 4.2.2 等差数列的前n项和公式

    ${\color{Red}{欢迎到学科网下载资料学习 }}$ [ [基础过关系列]高二数学同步精品讲义与分层练习(人教A版2019)] ( https://www.zxxk.com/docpack/2 ...

  7. Vue3 和 Vue2 的区别 ?

    1. Vue3 和 VUe2 性能提升 :使用 proxy 代替 defainProperty 实现响应式数据 :使用 ts 书写代码 : 新特性有:组合 api compositionApi  :新 ...

  8. 39. 关于 diff 算法

    diff 算法是vue渲染列表数据的时候,把新的 Vnode 和旧的 Vnode 比较,通过 key 值的对应,变化的标签就更新视图,不变的就复用 :

  9. el-tree 懒加载复选框默认展开和默认选中

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  10. KubeSphere Helm 应用仓库源码分析

    作者:蔡锡生,LStack 平台研发工程师,近期专注于基于 OAM 的应用托管平台落地. 背景介绍 KubeSphere 应用商店简介 作为一个开源的.以应用为中心的容器平台,KubeSphere 在 ...