Codeforces Round #791 (Div. 2) A-C
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的更多相关文章
- Codeforces Round #791(Div 2)——D
D Problem - D - Codeforces 题意: 给定一个有向图,每个点有自己的点权,求一条长度为K的路径使得路径上的最大点权最小,输出该条路径上的最大点权. 思路:(二分+拓扑排序) 最 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
- 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 ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
随机推荐
- 从压测碰到的诡异断连问题聊聊Nginx的连接管理
本文主要分享一个在压测Nginx反向代理服务过程中碰到的连接异常断开问题,包括问题的定位与复现,最后由这个实际问题引申聊一下Nginx的连接管理. 本博客已迁移至CatBro's Blog,那是我自己 ...
- Python学习笔记: 用pprint更漂亮的打印数据
pprint是一个标准库,它提供了pprint()函数 ,用来打印复杂数据时更漂亮 >>> from pprint import pprint >>> data = ...
- MySql免安装版 Error 2003 Can connect to MySQL server on ...
现象描述:mysql只能本地登录,无法远程登录 解决方案: 1. 查看mysql端口(默认端口3306,命令端口根据需要修改),发现只有本地连接端口开放. netstat -an|findstr 33 ...
- git详情、git工作流程、常用命令、忽略文件、分支操作、gitee远程仓库使用
今日内容概要 git详情 git工作流程 git常用命令 过滤文件 分支操作 git远程仓库使用 可参照:https://www.cnblogs.com/liuqingzheng/p/15328319 ...
- C++ atomic 和 memory ordering 笔记
如果不使用任何同步机制(例如 mutex 或 atomic),在多线程中读写同一个变量,那么,程序的结果是难以预料的.简单来说,编译器以及 CPU 的一些行为,会影响到程序的执行结果: 即使是简单的语 ...
- CAS如何解决ABA问题
点赞再看,养成习惯,微信搜索「小大白日志」关注这个搬砖人. 文章不定期同步公众号,还有各种一线大厂面试原题.我的学习系列笔记. CAS如何解决ABA问题 什么是ABA:在CAS过程中,线程1.线程2分 ...
- 漏洞复现:MS12-020 远程桌面协议RDP远程代码执行漏洞
漏洞复现:MS12-020 远程桌面协议RDP远程代码执行漏洞 攻击机:Kali2019 靶机:Win7 64位 解题步骤: 1.打开Kali2019和Win7 64位 ,确定IP地址是多少 2.确定 ...
- python mysqldb 报错: ProgrammingError: must be real number, not str 解决
代码: sql = 'insert into book(book_name,book_desc,origin_price,publish_id,tag_id,book_img) values(%s,% ...
- Docker容器的安装和使用
Docker容器的安装和使用 安装: curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun 或国内:curl -sSL ...
- 面试简历书写、Flask框架介绍与快速使用、Flask演示登录页面、用户信息页面案例
今日内容概要 面试简历编写 Flask框架介绍与安装 内容详细 1.面试简历编写 # 千万不要几个小时把简历凑出来 几天到一周 # 有没有面试机会,取决于简历写得怎么样 简历写好是第一步 # 投简历的 ...