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. 必应每日壁纸API封装

    简介 这个类封装了必应首页的每日壁纸查看功能,提供了查看.保存壁纸的方法,最大支持查看近8天的壁纸 使用方法 async Task Main() { try { var bing = BingWall ...

  2. Angular Material 18+ 高级教程 – CDK Drag and Drop

    前言 CDK Drag and Drop 和 CDK Scrolling 都是在 Angular Material v7 中推出的. 它们有一个巧妙的共同点,那就是与 Material Design ...

  3. CSS & JS Effect – Blue Tick Avatar

    效果 难点 难题只有一个, 那就是如何把 blue tick image 定位当 avatar 的右下角. HTML <div class="avatar-wrapper"& ...

  4. [rCore学习笔记 027]地址空间

    写在前面 本随笔是非常菜的菜鸡写的.如有问题请及时提出. 可以联系:1160712160@qq.com GitHhub:https://github.com/WindDevil (目前啥也没有 引言 ...

  5. .NET常见的几种项目架构模式,你知道几种?(附带使用情况投票)

    前言 项目架构模式在软件开发中扮演着至关重要的角色,它们为开发者提供了一套组织和管理代码的指导原则,以提高软件的可维护性.可扩展性.可重用性和可测试性. 假如你有其他的项目架构模式推荐,欢迎在文末留言 ...

  6. 配置windows update失败还原更改

    配置windows update失败还原更改_解决方案 解决方法: 方法1:     重启,按F8,选择最后一次正常启动.     如果还是需要等待.可采用方法2: 方法2:     重启,按F8,选 ...

  7. linux(centos7)安装curl和composer

    linux(centos7)安装curl和composer 先安装curl:直接用yum装,yum curl 使用命令下载: curl -sS https://getcomposer.org/inst ...

  8. 你对 Vue.js 的template 编译的理解?

    template 是 ES5 新出的语法 ,template 是不会被页面显示的,但是 vue 中会被翻译成 dom 结构 : template 编译的过程 : parse 解析生成ast 抽象语法树 ...

  9. vue3实现多层级的动态表单增减

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

  10. 一些新奇的玩意【php篇--持续更新】

    人不进步就等于退步! 接触越多的人以及事就能学到更多的东西. 以下仅为本人记录的一些新奇的东西,不喜勿喷! 1.??运算符号,在新的项目中突然发现很多红线报错,还以为是错误!看了下,是??运算的问题, ...