Educational Codeforces Round 56 (Rated for Div. 2) G题(线段树,曼哈顿距离)
题目传送门
以二维为例,二维下两点间的曼哈顿距离最大值为\(max(|x_i-x_j| + |y_i-y_j|)\),可以通过枚举坐标符号正负来去掉绝对值。即\(max(x_i-x_j+y_i-y_j,x_i+x_j+y_i-y_j...)\)共16种情况。设\(f[i][t]\)表示第\(i\)个点各维度数值正负状态为\(t\)时的值,假如二维向量\(p[i]=(1,2)\),那么状态\(t\)的范围为\([0,3]\),如果\(t=2\),即二进制表示为\(10\),那么\(p\)向量第一维数值加个负号,第二维数值不变,类似状压过程。对于每个向量处理出\(f\)的值。最终结果即在\([l,r]\)范围内求\(max(f[i][m],f[j][n])(m+n=(1<<k)-1)\),具体用线段树实现。
#include<cstdio>
#include<vector>
#include<algorithm>
#define ls (u << 1)
#define rs (u << 1 | 1)
using namespace std;
const int N = 2e5 + 50;
struct Node{
vector<int> val;
Node() { val.resize(35); }
}node[N << 2];
int n, k, q;
int a[N][6];
void Max(const vector<int> &x, const vector<int> &y, vector<int> &z){
for(int i = 0; i < (1 << k); ++i) z[i] = max(x[i], y[i]);
}
void Max(const vector<int> &x, vector<int> &y){
for(int i = 0; i < (1 << k); ++i) y[i] = max(x[i], y[i]);
}
void build(int u, int l, int r){
if(l == r){
for(int s = 0; s < (1 << k); ++s){
for(int t = 1; t <= k; ++t){
if(s & (1 << (t - 1))) node[u].val[s] += a[l][t];
else node[u].val[s] -= a[l][t];
}
}
return ;
}
int mid = (l + r) >> 1;
if(l <= mid) build(ls, l, mid);
if(r > mid) build(rs, mid + 1, r);
Max(node[ls].val, node[rs].val, node[u].val);
}
void modify(int u, int l, int r, int pos){
if(l == r){
for(int i = 0; i < (1 << k); ++i) node[u].val[i] = 0;
for(int s = 0; s < (1 << k); ++s){
for(int t = 1; t <= k; ++t){
if(s & (1 << (t - 1))) node[u].val[s] += a[l][t];
else node[u].val[s] -= a[l][t];
}
}
return ;
}
int mid = (l + r) >> 1;
if(pos <= mid) modify(ls, l, mid, pos);
else modify(rs, mid + 1, r, pos);
Max(node[ls].val, node[rs].val, node[u].val);
}
vector<int> ans(35);
void query(int u, int l, int r, int ql, int qr){
if(ql <= l && qr >= r){
Max(node[u].val, ans);
return ;
}
int mid = (l + r) >> 1;
if(ql <= mid) query(ls, l, mid, ql, qr);
if(qr > mid) query(rs, mid + 1, r, ql, qr);
}
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; ++i)
for(int t = 1; t <= k; ++t)
scanf("%d", &a[i][t]);
build(1, 1, n);
scanf("%d", &q);
while(q--){
int opt, id, l, r;
scanf("%d", &opt);
if(opt == 1){
scanf("%d", &id);
for(int i = 1; i <= k; ++i) scanf("%d", &a[id][i]);
modify(1, 1, n, id);
}
else{
for(int i = 0; i < 35; ++i) ans[i] = -1e9;
scanf("%d%d", &l, &r);
query(1, 1, n, l, r);
int maxx = 0;
for(int s = 0; s < (1 << k); ++s){
maxx = max(maxx, ans[s] + ans[(1 << k) - 1 - s]);
}
printf("%d\n", maxx);
}
}
}
Educational Codeforces Round 56 (Rated for Div. 2) G题(线段树,曼哈顿距离)的更多相关文章
- Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)
预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 #d ...
- Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)
这道题里线段树用来区间更新(每次给更大的区间加上当前区间的权重),用log的复杂度加快了更新速度,也用了区间查询(查询当前区间向右直至最右中以当前区间端点向右一段区间的和中最大的那一段的和),也用lo ...
- Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;#define BUF_SIZE 100000 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】
传送门:http://codeforces.com/contest/1093/problem/D D. Beautiful Graph time limit per test 2 seconds me ...
- Educational Codeforces Round 56 (Rated for Div. 2) ABCD
题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D
给你一个无向图 以及点的个数和边 每个节点只能用1 2 3 三个数字 求相邻 两个节点和为奇数 能否构成以及有多少种构成方法 #include<bits/stdc++.h> usin ...
- Educational Codeforces Round 56 (Rated for Div. 2)
涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...
- Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题
F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...
随机推荐
- webpack是如何处理css/less资源的呢
上一篇文章 体验了webpack的打包过程,其中js文件不需要我们手动配置就可以成功解析,可其它类型的文件,比如css.less呢? css-loader 首先,创建一个空文件夹,通过 npm ini ...
- [nginx]日志中记录自定义请求头
前言 假设在请求中自定义了一个请求头,key为"version",参数值为"1.2.3",需要在日志中捕获这个请求头. nginx日志配置 只需要用变量http ...
- CAP 7.2 版本发布通告
前言 今天,我们很高兴宣布 CAP 发布 7.2 版本正式版,我们在这个版本中主要致力于 Dashboard 对 k8s 服务发现的支持. 从 7.1 版本以来,我们发布了4个小版本,在这些版本中我们 ...
- 【腾讯云 Cloud Studio 实战训练营】提升开发效率与协作:探索腾讯云 Cloud Studio 的强大功能与优势
一.前言 前几天发生了一个故事,发生了这样一个情景:一位新加入的同事刚刚入职不久,领取了一台崭新的电脑.随后,他投身于一个新项目,但却遇到了一个困扰:由于这台电脑没有管理员权限,他无法在上面安装所需的 ...
- 基于 ASP.NET 的投票系统
OnlineVoting 基于 ASP.NET 的投票系统 功能页面 登录 注册 首页 投票广场 查看别人发布的投票. 个人中心 个人资料 换头像.修改密码和其他信息. 发布投票 项目地址:https ...
- Redis系列18:过期数据的删除策略
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) Redis系列5: ...
- 一个可将执行文件打包成Windows服务的.Net开源工具
Windows服务一种在后台持续运行的程序,它可以在系统启动时自动启动,并在后台执行特定的任务,例如监视文件系统.管理硬件设备.执行定时任务等. 今天推荐一个可将执行文件打包成Windows 服务的工 ...
- C#利用Refit实现JWT自动续期
前言 笔者之前开发过一套C/S架构的桌面应用,采用了JWT作为用户的登录认证和授权.遇到的唯一问题就是JWT过期了该怎么办?设想当一个用户正在进行业务操作,突然因为Token过期失效,莫名其妙地跳转到 ...
- 代码随想录算法训练营第二十五天| 216.组合总和III 17.电话号码的字母组合
216.组合总和III 卡哥建议:如果把 组合问题理解了,本题就容易一些了. 题目链接/文章讲解:https://programmercarl.com/0216.%E7%BB%84%E5%90%8 ...
- Go 语言内置类型全解析:从布尔到字符串的全维度探究
关注微信公众号[TechLeadCloud],分享互联网架构.云服务技术的全维度知识.作者拥有10+年互联网服务架构.AI产品研发经验.团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云认证 ...