CF 1093 G. Multidimensional Queries
G. Multidimensional Queries
分析:
考虑如何去掉绝对值符号。
$\sum \limits_{i = 1}^{k} |a_{x, i} - a_{y, i}|$,由于k比较小,考虑枚举每一维的符号,发现如果不是最终的答案,结果会变小,不影响取max的操作。
然后就是单点修改,区间查询最大最小值。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define Root 1, n, 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
struct Node{ int x[]; }A[N];
struct Que{ int opt, x[], l, r; }Q[N];
int ans[N]; struct SegmentTree{
int mx[N << ], mn[N << ];
void update(int l,int r,int rt,int p,int x) {
if (l == r) { mn[rt] = mx[rt] = x; return; }
int mid = (l + r) >> ;
if (p <= mid) update(lson, p, x);
else update(rson, p, x);
mx[rt] = max(mx[rt << ], mx[rt << | ]);
mn[rt] = min(mn[rt << ], mn[rt << | ]);
}
int query_min(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) return mn[rt];
int mid = (l + r) >> ;
if (R <= mid) return query_min(lson, L, R);
else if (L > mid) return query_min(rson, L, R);
else return min(query_min(lson, L, R), query_min(rson, L, R));
}
int query_max(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) return mx[rt];
int mid = (l + r) >> ;
if (R <= mid) return query_max(lson, L, R);
else if (L > mid) return query_max(rson, L, R);
else return max(query_max(lson, L, R), query_max(rson, L, R));
}
}T; int main() {
int n = read(), k = read();
for (int i = ; i <= n; ++i)
for (int j = ; j < k; ++j) A[i].x[j] = read();
int m = read();
for (int i = ; i <= m; ++i) {
Q[i].opt = read(); Q[i].r = -;
if (Q[i].opt == ) {
Q[i].l = read();
for (int j = ; j < k; ++j) Q[i].x[j] = read();
}
else {
Q[i].l = read(), Q[i].r = read();
}
}
int S = ( << k) - , now;
for (int s = ; s <= S; ++s) {
for (int i = ; i <= n; ++i) {
now = ;
for (int j = ; j < k; ++j) now += ((s >> j) & ) ? (A[i].x[j]) : (-A[i].x[j]);
T.update(Root, i, now);
}
for (int i = ; i <= m; ++i) {
if (Q[i].opt == ) {
int mn = T.query_min(Root, Q[i].l, Q[i].r);
int mx = T.query_max(Root, Q[i].l, Q[i].r);
ans[i] = max(ans[i], mx - mn);
}
else {
now = ;
for (int j = ; j < k; ++j) now += ((s >> j) & ) ? (Q[i].x[j]) : (-Q[i].x[j]);
T.update(Root, Q[i].l, now);
}
}
}
for (int i = ; i <= m; ++i) {
if (Q[i].r == -) continue;
printf("%d\n", ans[i]);
}
return ;
}
CF 1093 G. Multidimensional Queries的更多相关文章
- POJ-2926-Requirements&&Educational Codeforces Round 56G. Multidimensional Queries 【哈夫曼距离】
POJ2926 先学会这个哈夫曼距离的处理才能做 cf 的G #include <iostream> #include <stdio.h> #include <algor ...
- [CF1093G]Multidimensional Queries
[CF1093G]Multidimensional Queries 题目大意: \(k(k\le5)\)维空间中有\(n(n\le2\times10^5)\)个点.\(m\)次操作,操作包含一下两种: ...
- CF 1093G Multidimensional Queries——线段树(消去绝对值符号)
题目:http://codeforces.com/contest/1093/problem/G 只好看看题解:https://codeforces.com/blog/entry/63877 主要是把绝 ...
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
- CF 1051 G. Distinctification
G. Distinctification 链接 分析: 线段树合并 + 并查集. 最后操作完后a连续递增的一段,b一定是递减的.最后的答案是$\sum (a_{new}-a_{odd}) \times ...
- CF 724 G. Xor-matic Number of the Graph
G. Xor-matic Number of the Graph 链接 题意: 给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s, ...
- CF 1003D Coins and Queries【位运算/硬币值都为2的幂/贪心】
Polycarp has n coins, the value of the i-th coin is ai. It is guaranteed that all the values are int ...
- CF 914 G Sum the Fibonacci —— 子集卷积,FWT
题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...
- cf E. Valera and Queries
http://codeforces.com/contest/369/problem/E 题意:输入n,m; n 代表有多少个线段,m代表有多少个询问点集.每一个询问输出这些点的集合所占的线段的个数. ...
随机推荐
- Azure Document DB Repository 的实现
阅读 需要大约 5 分钟. 前景: Azure Cosmos DB 由 Microsoft 提供,是全球分布式多模型数据库. 通过 Azure Cosmos DB 跨任意数量的 Azure 地理区域 ...
- 安卓 USB摄像头 开源库 UVCCamera 教程
https://github.com/saki4510t/UVCCamera UVCCamera 听名字就知道使用UVC( USB VEDIO CLASS) 协议的通用类库.linux原生支持,基本支 ...
- 裸机恢复 (BMR) 和系统状态恢复
DPM 将系统保护数据源视为两个组成部分 – 裸机恢复 (BMR) 和系统状态保护. BMR 涉及保护操作系统文件和重要卷上的所有数据,用户数据除外. 系统状态保护涉及保护操作系统文件. DPM 使用 ...
- python安装lib库
time:2015/11/11 双十一 一.初衷 看到一篇帖子[1],里面有python代码,就想实现一下,代码如下: import cv2 as cv import numpy as np from ...
- marquee 标签的鼠标放上去滚动效果 鼠标离开继续滚动
效果很实用,可以轻松的实现鼠标放上去停止滚动.鼠标离开继续滚动的效果.下面是具体的用法(特别注意onMouseOver和onMouseOut是需要同时写进去才会出现比较好的效果):onMouseOut ...
- java中判断对象中属性值是否为空的函数
public boolean checkObjFieldIsNull(Object obj) throws IllegalAccessException { boolean flag = false; ...
- 【2017.12.05 智能驾驶/汽车电子】转载:如何成为一名无人驾驶工程师 By刘少山
之前对无人驾驶的理解就是通过刘少山老师的书:第一本无人驾驶技术书 通读之后,对智能驾驶有了一个初步的认识,如感知.决策.控制都涉及哪些领域,有哪些可以利用的技术: 但经过一段时间的实践,发现即使是在我 ...
- ES(ElasticSearch)学习总结
基本概念 一个分布式多用户能力的全文搜索引擎,基于RESTful web接口. Elasticsearch和MongoDB/Redis/Memcache一样,是非关系型数据库.是一个接近实时的搜索平台 ...
- mysqldump.md
mysqldump命令 选项 -A, --all-databases:导出全部数据库 -Y, --all-tablespaces:导出全部表空间. -y, --no-tablespaces:不导出任何 ...
- Type Safety and Type Inference
Swift is a type-safe language. A type safe language encourages you to be clear about the types of va ...