Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接:
https://codeforces.com/contest/1093/problem/G
题目:

题意:
在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一种操作是查询[l,r]中曼哈顿距离最大的两个点的最大曼哈顿距离。
思路:
对于曼哈顿距离,我们将其绝对值去掉会发现如下规律(以二维为例):

故这题我们可以用线段树来维护[l,r]中上述每种情况的最大值和最小值,用二进制来枚举xy的符号(1为正,0为负),最后答案是 每种情况中区间最大值-区间最小值 的最大值。
代码实现如下:
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pli;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt", "r", stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 2e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, k, q, op, x, l, r;
int a[maxn][], num[]; struct node {
int l, r, mx, mn;
}segtree[maxn<<][]; void push_up(int rt, int pp) {
segtree[rt][pp].mx = max(segtree[lson][pp].mx, segtree[rson][pp].mx);
segtree[rt][pp].mn = min(segtree[lson][pp].mn, segtree[rson][pp].mn);
} void build(int rt, int l, int r, int pp) {
segtree[rt][pp].l = l, segtree[rt][pp].r = r;
segtree[rt][pp].mx = segtree[rt][pp].mn = ;
if(l == r) {
for(int i = ; i < k; i++) {
if(pp & (<<i)) {
segtree[rt][pp].mx += a[l][i];
segtree[rt][pp].mn += a[l][i];
} else {
segtree[rt][pp].mx -= a[l][i];
segtree[rt][pp].mn -= a[l][i];
}
}
return;
}
int mid = (l + r) >> ;
build(lson, l, mid, pp);
build(rson, mid + , r, pp);
push_up(rt, pp);
} void update(int rt, int pos, int pp) {
if(segtree[rt][pp].l == segtree[rt][pp].r) {
segtree[rt][pp].mx = segtree[rt][pp].mn = ;
for(int i = ; i < k; i++) {
if(pp & (<<i)) {
segtree[rt][pp].mx += num[i];
segtree[rt][pp].mn += num[i];
} else {
segtree[rt][pp].mx -= num[i];
segtree[rt][pp].mn -= num[i];
}
}
return;
}
int mid = (segtree[rt][pp].l + segtree[rt][pp].r) >> ;
if(pos <= mid) update(lson, pos, pp);
else update(rson, pos, pp);
push_up(rt, pp);
} int query(int rt, int l, int r, int pp, int op) {
if(segtree[rt][pp].l >= l && segtree[rt][pp].r <= r) {
if(op == ) {
return segtree[rt][pp].mx;
} else {
return segtree[rt][pp].mn;
}
}
int mid = (segtree[rt][pp].l + segtree[rt][pp].r) >> ;
if(r <= mid) return query(lson, l, r, pp, op);
else if(l > mid) return query(rson, l, r, pp, op);
else {
if(op == ) return max(query(lson, l, mid, pp, op), query(rson, mid + , r, pp, op));
else return min(query(lson, l, mid, pp, op), query(rson, mid + , r, pp, op));
}
} int main(){
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) {
for(int j = ; j < k; j++) {
scanf("%d", &a[i][j]);
}
}
for(int i = ; i < (<<k); i++) {
build(, , n, i);
}
scanf("%d", &q);
while(q--) {
scanf("%d", &op);
if(op == ) {
scanf("%d", &x);
for(int i = ; i < k; i++) {
scanf("%d", &num[i]);
}
for(int i = ; i <(<<k); i++) {
update(, x, i);
}
} else {
scanf("%d%d", &l, &r);
int mx = -inf;
for(int i = ; i < (<<k); i++) {
mx = max(mx, query(, l, r, i, ) - query(, l, r, i, ));
}
printf("%d\n", mx);
}
}
return ;
}
Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))的更多相关文章
- Educational Codeforces Round 56 (Rated for Div. 2) E(1093E) Intersection of Permutations (树套树,pb_ds)
题意和分析在之前的链接中有:https://www.cnblogs.com/pkgunboat/p/10160741.html 之前补题用三维偏序的cdq的分治A了这道题,但是感觉就算比赛再次遇到类似 ...
- Educational Codeforces Round 56 (Rated for Div. 2)
涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...
- 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) F - Vasya and Array dp好题
F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...
- Educational Codeforces Round 56 (Rated for Div. 2) F. Vasya and Array
题意:长度为n的数组,数组中的每个元素的取值在1-k的范围内或者是-1,-1代表这个元素要自己选择一个1-k的数字去填写,然后要求填完的数组中不能出现连续长度大于len的情况,询问填空的方案数. 题解 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)
题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模. 题解:要使得每个点所连的奇偶不 ...
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
随机推荐
- requests爬取豆瓣热门电视剧
# *_*coding:utf-8 *_* import requests payload = {'key1': 'value1', 'key2': 'value2'} headers = {'use ...
- Go 自学笔记
1. 最近花时间简单自学了一下go语言的语法..为了保证自己不是每次从0 开始 这次简单进行一下记录 保证 学习 效果. 2. 安装 直接下载go的包 进行安装 以及 暗转goland2018.3 进 ...
- Anaconda多版本Python管理以及TensorFlow版本的选择安装
Anaconda是一个集成python及包管理的软件,记得最早使用时在2014年,那时候网上还没有什么资料,需要同时使用py2和py3的时候,当时的做法是同时安装Anaconda2和Anaconda3 ...
- SpringBoot(八)_springboot集成swagger2
swagger是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试. (1) 引入依赖,我们选择现在最新的版本 <dependency> &l ...
- 小试javascript模版mustache
夜以深,人未眠,本该入睡,然逢周末,无聊甚哉,故于此作文打发时间----------- 前几日,无聊,小试了下javascript版本的mustache模版,说是小试,其实主要目的是阅读学习其源码.如 ...
- matlab dist函数
dist——欧式距离加权函数(Euclidean distance weight function) 语法: Z = dist(W,P) df = dist('deriv') D = di ...
- Python实现双色球和大乐透摇奖
实现代码: # code by kadycui # 模块引用 import random def select(): print('\n') print('请选择彩票种类') print('双色球输入 ...
- Luogu 3385 负环 | 我有特别的SPFA技巧
这样似乎跑得快: 初始化所有的dis是0,然后枚举每个点作为起点,用DFS更新所有点的dis: 如果更新到一个栈中节点,那么有负环. #include <cstdio> #include ...
- 【转】器件为什么只听英文Datasheet的话
浅谈为什么要阅读英文数据手册 ——带你Go Through Datasheet 系列 Unfortunately!从事软硬件(固件)开发的工程师都知道,我们所用的元器件,特别是高端器件和芯片,都是来自 ...
- wazuh安装手册
一.wazhu部署架构 1.服务器上运行的Agent端会将采集到的各种信息通过加密信道传输到管理端. 2.管理端负责分析从代理接收的数据,并在事件与告警规则匹配时触发警报. 3.LogStash会将告 ...