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. IDEA小技巧:Debug时如何优雅地制造异常?

    抛异常相信大家都会吧?只需要这样就可以了: throw new RuntimeException("didispace.com"); 但是,在开发过程中有一些情况,我们需要测试程序 ...

  2. 3.5 常用Linux命令

    1.touch命令 touch命令用于创建空白文件或设置文件的时间,语法格式为"touch [参数] 文件名称". 2.mkdir命令 mkdir命令用于创建空白的目录,英文全称为 ...

  3. python学习-Day29

    目录 今日内容详细 反射实际案例 面向对象的双下方法 __ str __ __ del __ __ getattr __ __ setattr __ __ call __ __ enter __ __ ...

  4. nginx + nginx-rtmp-module搭建直播流服务器实现推流实时直播功能

    业务需求 最近公司在做养老相关的业务,现在需要子女从小程序端对家里的老人通过家庭终端交互屏进行实时看护. 解决方案 第三方的一些现成的服务:腾讯音视频通话.直播功能; 阿里的音视频通信;两者都挺好的, ...

  5. Redis实现并发阻塞锁方案

    由于用户同时访问线上的下订单接口,导致在扣减库存时出现了异常,这是一个很典型的并发问题,本篇文章为解决并发问题而生,采用的技术为Redis锁机制+多线程的阻塞唤醒方法. 在实现Redis锁机制之前,我 ...

  6. CoreWCF 1.0.0 发布,微软正式支持WCF

    2022年4月28日,我们达到了一个重要的里程碑,并发布了CoreWCF的1.0.0版本.对Matt Connew (微软WCF团队成员)来说,这是5年前即 2017年1月开始的漫长旅程的结束.Mat ...

  7. 解决:Could not resolve dependencies for project xxx: Could not find artifact xxx

    引言 运行A module,找不到B module的依赖报错.A.B module都在project中. 报错信息 [INFO] Scanning for projects... [INFO] [IN ...

  8. 工程师姓什么很重要!别再叫我“X工”!!!

    关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 工程师之间都是这么互相打招呼的-- "高工,你设计图通过了么?" &quo ...

  9. 使用requests爬取梨视频、bilibili视频、汽车之家,bs4遍历文档树、搜索文档树,css选择器

    今日内容概要 使用requests爬取梨视频 requests+bs4爬取汽车之家 bs4遍历文档树 bs4搜索文档树 css选择器 内容详细 1.使用requests爬取梨视频 # 模拟发送http ...

  10. 2. 假设当前文件夹中data.csv文件中存放了2020年某饭店营业额,第一列为日期(如2020-02-03),第二列为每天交易额(如3560),文件中第一行为表头,其余行为实 际数据。

    假设当前文件夹中data.csv文件中存放了2020年某饭店营业额,第一列为日期(如2020-02-03),第二列为每天交易额(如3560),文件中第一行为表头,其余行为实  际数据.编写程序,完成下 ...