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. Java并发编程扩展(线程通信、线程池)

    之前我说过,实现多线程的方式有4种,但是之前的文章中,我只介绍了两种,那么下面这两种,可以了解了解,不懂没关系. 之前的文章-->Java并发编程之多线程 使用ExecutorService.C ...

  2. 关于LVS中默认的DR模型图

    虽然常用,但是关于DR模型最大的一个缺点就是Director和RS必须在同一个网络中,通过交换机连接,中间不能有路由

  3. [AcWing 801] 二进制中1的个数

    点击查看代码 #include<iostream> using namespace std; int lowbit(int x) { return x & -x; } int ma ...

  4. GO语言学习——切片三 append()方法为切片添加元素、切片的扩容策略、使用copy()函数复制切片、从切片中删除元素

    append()方法为切片添加元素 注意:通过var声明的零值切片可以在append()函数直接使用,无需初始化. var s []int s = append(s, 1, 2, 3) append( ...

  5. 分享一款高逼格的Linux磁盘信息查看工具

    关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 可以使用df命令来显示在Linux.macOS和类Unix系统中挂载的文件系统上有多少可用磁盘 ...

  6. 如何查看Linux进程详情?(ps命令)

    点击关注上方"开源Linux", 后台回复"读书",有我为您特别筛选书籍资料~ 1. ps是什么? 要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是 ...

  7. Node.js 中的进程和线程

    线程和进程是计算机操作系统的基础概念,在程序员中属于高频词汇,那如何理解呢?Node.js 中的进程和线程又是怎样的呢? 一.进程和线程 1.1.专业性文字定义 进程(Process),进程是计算机中 ...

  8. Android 12(S) 图像显示系统 - SurfaceFlinger GPU合成/CLIENT合成方式 - 随笔1

    必读: Android 12(S) 图像显示系统 - 开篇 一.前言 SurfaceFlinger中的图层选择GPU合成(CLIENT合成方式)时,会把待合成的图层Layers通过renderengi ...

  9. 深入HTTP请求流程

    1.HTTP协议介绍 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,它是从WEB服务器传输超文本标记语言(HTML)到 ...

  10. VMware-workstation 安装步骤

    目录 VMware Workstation简介 软件获取 百度网盘获取 VM激活码: 安装步骤: VMware Workstation简介 VMware Workstation(中文名"威睿 ...