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. 基于全志A40i开发板——Linux-RT内核应用开发教程(1)

    目录 1 Linux-RT内核简介 3 2 Linux系统实时性测试 3 3 rt_gpio_ctrl案例 10 4 rt_input案例 15 本文为Linux-RT内核应用开发教程的第一章节--L ...

  2. XCTF练习题---MISC---Excaliflag

    XCTF练习题---MISC---Excaliflag flag:3DS{Gr4b_Only_th1s_B1ts} 解题步骤: 1.观察题目,下载附件 2.拿到手以后是一张图片,话不多说,直接上Ste ...

  3. 【MyBatis】多关键字的模糊查询

    目录 情景分析 题目 要求 相关代码 Mapper.java Impl.java mapper.xml test.java 思路分析 1.最开始,参数没有使用List,引起的问题 2.Mybatis的 ...

  4. tensorflwo-gpu win10_64bit 的安装版本问题

    tensorflow 1.3 配 cuda8.0 + cudnn5.1tensorflow 1.4 配 cuda8.0 + cudnn6.0 有没有更大的字体???我要配!!!!!

  5. Linux虚拟网络技术学习

    一个执着于技术的公众号 地方 背景 在Linux虚拟化技术中,网络层面,通常重要的三个技术分别是Network Namespace.veth pair.以及网桥或虚拟交换机技术.今天就通过实验带大家一 ...

  6. 高级IPC DBus

    What is IPC IPC [Inter-Process Communication] 进程间通信,指至少两个进程或线程间传送数据或信号的一些技术或方法.在Linux/Unix中,提供了许多IPC ...

  7. python之函数的进阶

    1.名称空间: 定义:用来存放名字的(变量,函数名,类名,引入的模块名) 分类: 内置名称空间:python解释器提供好的一些内置内容 全局名称空间:py文件中自己写的变量 局部名称空间:执行函数时会 ...

  8. Vue3 与依赖注入

    Vue3 与依赖注入 本文写于 2021 年 2 月 19 日 在 React 中,我们可以通过 context 与 useContext 实现单例.注入--等诸多特性. 详细请看上一篇文章:如何利用 ...

  9. redis & redis sentinel

    Redis 命令参考 Redis Sentinel Cheat Sheet Redis 哨兵节点之间相互自动发现机制(自动重写哨兵节点的配置文件) Redis哨兵模式(sentinel)学习总结及部署 ...

  10. 编程语言与python与pycharm的下载

    目录 编程语言的发展史 编程语言的分类 python解释器 python解释器的下载与安装 环境变量 执行python程序方式 pycharm编辑器 编程语言的发展史 机器语言是最开始的编程语言,之后 ...