Codeforces Round #791 (Div. 2) A-C

A

题目

https://codeforces.com/contest/1679/problem/A

题解

思路

知识点:数学,暴力,贪心。

考虑 \(n\%6\) 和 \(n\%4\) 的余数情况。

时间复杂度 \(O(1)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>

using namespace std;

int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
if(n<4 || n&1) cout<<-1<<'\n';
else{
long long d = n/6,r = n%6;
cout<<d + (r != 0)<<' ';
cout<<n/4<<'\n';
}
}
return 0;
}

B

题目

https://codeforces.com/contest/1679/problem/B

题解

思路

知识点:数据结构,模拟。

如果执行操作 \(2\) ,那么所有数都变成 \(x\) ,我们可以理解为把数组清空了,但 \(sum\) 的基础值是 \(n*x\) ,下次单点修改时只需要知道清空之后,这个元素有没有被修改过,即在数组中是否存在,分别做不同的处理。可以用 \(map\) 维护下标与值的映射。(也可以记录上次整体修改的访问时间,用一个数组保存每个位置在哪次整体修改之后修改的访问时间)

时间复杂度 \(O(n+q)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>

using namespace std;

int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,q;
cin>>n>>q;
map<int,int> a;
long long sum = 0;
for(int i = 1;i<=n;i++) cin>>a[i],sum+=a[i];
int pre = 0;
while(q--){
int t;
cin>>t;
if(t == 1){
int i,x;
cin>>i>>x;
if(a.count(i)) sum += x-a[i];
else sum = sum - pre + x;
a[i] = x;
cout<<sum<<'\n';
}
else if(t == 2){
int x;
cin>>x;
sum = 1LL * n * x;
pre = x;
a.clear();
cout<<sum<<'\n';
}
}
return 0;
}

C

题目

https://codeforces.com/contest/1679/problem/C

题解

思路

知识点:树状数组(或者线段树),模拟。

如果一个区块被攻击,那么肯定对应的所有行或者所有列都存在棋子,分别计算存在棋子的行和列的数量,行或列有一个满足与区块行或列相等,那么区块所有格子都能被攻击到。用树状数组的单点修改和区间和维护行和列的棋子存在状态和计算,用数组维护各行和列的当前棋子数量辅助树状数组的修改。

用线段树也可以,常数比较大。

(qwq太菜惹,抄大佬的板子)

时间复杂度 \(O(qlogn)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>

using namespace std;

template <typename T>
struct Fenwick{
const int n;
vector<T> a;
Fenwick(int n):n(n),a(n){}
void add(int x,T v) {
for (int i = x+1;i<=n;i += i&-i){
a[i-1] += v;
}
}
T sum(int x) {
T ans = 0;
for (int i = x;i>0;i -= i&-i){
ans += a[i-1];
}
return ans;
}
T rangeSum(int l,int r){
return sum(r) - sum(l);
}
}; int row[100007],col[100007]; int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,q;
cin>>n>>q;
Fenwick<int> rowb(n+7),colb(n+7);
while(q--){
int t;
cin>>t;
if(t == 1){
int x,y;
cin>>x>>y;
row[x]++;
col[y]++;
if(row[x] == 1) rowb.add(x,1);
if(col[y] == 1) colb.add(y,1);
}
else if(t == 2){
int x,y;
cin>>x>>y;
row[x]--;
col[y]--;
if(row[x] == 0) rowb.add(x,-1);
if(col[y] == 0) colb.add(y,-1);
}
else if(t == 3){
int x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
if(rowb.rangeSum(x1,x2+1) == x2-x1+1 || colb.rangeSum(y1,y2+1) == y2-y1+1) cout<<"Yes"<<'\n';
else cout<<"No"<<'\n';
}
}
return 0;
}

Codeforces Round #791 (Div. 2) A-C的更多相关文章

  1. Codeforces Round #791(Div 2)——D

    D Problem - D - Codeforces 题意: 给定一个有向图,每个点有自己的点权,求一条长度为K的路径使得路径上的最大点权最小,输出该条路径上的最大点权. 思路:(二分+拓扑排序) 最 ...

  2. 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 ...

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

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

  4. Codeforces Round #368 (Div. 2)

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

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

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

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

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

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #371 (Div. 1)

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

随机推荐

  1. 全栈交叉编译X86完成过程经验分享

    1 CMAKE的交叉编译配置 主要是C和C++编译器的配置和SYSROOT的配置. set (CMAKE_SYSTEM_NAME "Linux") set (CMAKE_SYSTE ...

  2. 尤娜故事-迷雾-springboot扮酷小技巧

    前情回顾 从前,有一个简单的通道系统叫尤娜-- 尤娜系统的第一次飞行中换引擎的架构垂直拆分改造 四种常用的微服务架构拆分方式 尤娜,我去面试了 正文 我回到日常的尤娜系统建设中,最近事情比较少,总有一 ...

  3. 集成算法(Bagging & Boosting)

    用多种分类器一起完成同一份任务 Bagging策略(有放回的,随机的,子集大小一样的,m个训练集用同一个模型) Boosting-提升策略(串联) AdaBoost算法

  4. 请收藏,Linux 运维必备的 40 个命令总结,收好了~

    点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 1.删除0字节文件 find -type f -size ...

  5. Android开发的开始

    Android 软件 手机软件哦,目前只有一层外壳,进一步功能等待开发. 下载地址

  6. 软件开发架构,网络编程简介,OSI七层协议,TCP和UDP协议

    软件开发架构 什么是软件开发架构 1.软件架构是一个系统的草图. 2.软件架构描述的对象是直接构成系统的抽象组件. 3.各个组件之间的连接则明确和相对细致地描述组件之间的通讯. 4.在实现阶段,这些抽 ...

  7. 探索ABP基础架构

    为了了解应用程序是如何配置和初始化,本文将探讨ASP.NET Core和ABP框架最基本的构建模块.我们将从 ASP.NET Core 的 Startup类开始了解为什么我们需要模块化系统,以及 AB ...

  8. ubuntu 获取 gateway 网关 ip

    输入以下命令,第一行中 Gateway 列就是网关 IP netstat -rn

  9. spring boot validation

    先简单打个草稿 @NotNull 不能为 null @NotEmpty 不能为空(允许空格) ,只能用于字符串 @NotBlank 不能为空(trim()后) 用于验证字符串不为空且不能全为空格,只能 ...

  10. C# .NET ML.NET 机器学习 图像分类

    一. 准备工作 IDE是 VS2019.先下载好"resnet_v2_50_299.meta"这个文件,放入"C:\Users\jk\AppData\Local\Temp ...