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. 《放弃繁琐的if-else》开启Assert断言的新时代

    一.场景再现 我们平时在service操作数据库,难免会出现这种情况: if(null == result){ }else{ } 这样的代码会吞噬掉你有限的代码空间,虽然通俗易懂,但一旦爆炸式的袭来, ...

  2. 解决Mac安装软件的“已损坏,无法打开...”问题

    解决Mac安装软件的"已损坏,无法打开. 您应该将它移到废纸篓"问题 不管在安装时会遇到以已损坏无法打开的困惑, 解决: 一.允许"任何来源"开启 苹果从mac ...

  3. TS2Vec: 面向通用的时间序列表示《TS2Vec: Towards Universal Representation of Time Series》(时间序列、对比学习、多尺度特征(池化操作)、分层对比、上下文一致性(时间戳掩码+随机裁剪))

    今天是2024年5月22日,10:24,今天看这篇经典的论文(如果你问我为什么最近频繁看论文,因为我的创新点无了,要找创新点+太菜了,菜就多看多学). 论文:TS2Vec: Towards Unive ...

  4. kali系统安装和CVE-2017-12615测试

    1 安装kali系统 1.1 下载VMware压缩包 kali-linux-2022.1 默认的用户和密码是kali 1.2 初始化系统 sudo apt update -y #kali sudo a ...

  5. Tauri2.0+Vite5聊天室|vue3+tauri2+element-plus仿微信|tauri聊天应用

    原创tauri2.0+vue3+pinai2仿QQ/微信客户端聊天Exe程序TauriWinChat. tauri2-vue3-winchat自研vite5+tauri2.0+vue3 setup+e ...

  6. Android Qcom USB Driver学习(十三)

    DPM Device Policy Manager deals with the USB Power Delivery resources used by one or more ports on t ...

  7. 介绍this指向问题

    this是js底层定义的变量,代表了代码的指向环境 a 函数的this是window b 方法的this是调用的对象 c 构造函数和原型对象上的方法的this指向实例化对象 d 箭头函数不会自己创建t ...

  8. oracle中排序分析函数row_number()、rank()、dense_rank() 的区别,与rownum的注意事项

    row_number()产生的序号不会重复,即1.2.3... rank()产生的序号会重复,但是会跳号,出现1.2.2.4...的情况 dense_rank()产生的序号会重复,不会跳号,会出现1. ...

  9. KubeKey 2.0.0 发布:让离线部署 K8s 更加便捷

    2022 年 3 月 8 日,KubeKey 2.0.0 正式发布,这是 KubeKey 的第 7 个正式版本,也是非常重要的一个版本.该版本新增了清单(manifest)和制品(artifact)的 ...

  10. KubeSphere v4 全解析:揭秘您最关心的 12 大热点问题

    为了助力大家更顺畅地使用 KubeSphere v4,我们精心汇总了十二个开发者高频关注的热点问题,这些问题全面覆盖了功能特性.性能表现.兼容性考量.安全保障以及升级流程等关键方面.接下来,我们将为大 ...