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的更多相关文章

  1. POJ-2926-Requirements&&Educational Codeforces Round 56G. Multidimensional Queries 【哈夫曼距离】

    POJ2926 先学会这个哈夫曼距离的处理才能做 cf 的G #include <iostream> #include <stdio.h> #include <algor ...

  2. [CF1093G]Multidimensional Queries

    [CF1093G]Multidimensional Queries 题目大意: \(k(k\le5)\)维空间中有\(n(n\le2\times10^5)\)个点.\(m\)次操作,操作包含一下两种: ...

  3. CF 1093G Multidimensional Queries——线段树(消去绝对值符号)

    题目:http://codeforces.com/contest/1093/problem/G 只好看看题解:https://codeforces.com/blog/entry/63877 主要是把绝 ...

  4. Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))

    题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...

  5. CF 1051 G. Distinctification

    G. Distinctification 链接 分析: 线段树合并 + 并查集. 最后操作完后a连续递增的一段,b一定是递减的.最后的答案是$\sum (a_{new}-a_{odd}) \times ...

  6. CF 724 G. Xor-matic Number of the Graph

    G. Xor-matic Number of the Graph 链接 题意: 给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s, ...

  7. 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 ...

  8. CF 914 G Sum the Fibonacci —— 子集卷积,FWT

    题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...

  9. cf E. Valera and Queries

    http://codeforces.com/contest/369/problem/E 题意:输入n,m; n 代表有多少个线段,m代表有多少个询问点集.每一个询问输出这些点的集合所占的线段的个数. ...

随机推荐

  1. ASP.NET Web API 跨域访问(CORS)要注意的地方

    一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...

  2. iOS设计模式 - 访问者

    iOS设计模式 - 访问者 原理图 说明 表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 1.Visitor 抽象访问者角色,为该对象结构中具 ...

  3. 人工智能——搜索(1)回溯策略【N皇后问题】

    这学期学<人工智能>(马少平,朱小燕 编著)这本书,里面很多算法听老师讲都听不懂,就想试试写一下看看能不能写出来,就从最简单的回溯策略开始吧. 源码 题目描述 在一个n*n的国际象棋棋盘上 ...

  4. Alpha 冲刺报告

    Alpha 冲刺报告(4/10) 队名:洛基小队 峻雄(组长) 已完成:继续行动脚本的编写 明日计划:尽量完成角色的移动 剩余任务:物品背包交互代码 困难:具体编码进展比较缓慢 ----------- ...

  5. 走向Node与Webpack 之路 - CommonJS 模块化

    走向Node与Webpack 之路 - CommonJS 模块化 1. 参考资料 JavaScript 标准参考教程(alpha) CommonJS规范(推荐 - 阮一峰写的) 官方网站 (看半天,不 ...

  6. 学习python第四天——Oracle分组

    1.分组的概念: 关键字:group by子句 结论:在select列表中如果出现了聚合函数,不是聚合函数的列,必须都要定义到group by子句的后面 需求: 查询公司各个部门的平均工资? sele ...

  7. Spring常用jar包的功能

    jar名称 描述 spring-framework.jar spring框架比较完整的功能,core+aop+ioc+transaction spring-core.jar 基本上的核心工具类,一些u ...

  8. 【洛谷】【动态规划(二维)】P1508 Likecloud-吃、吃、吃

    [题目描述:] 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏眼花之时,眼前突然闪现出了一个n*m(n and m<=200)的矩型的巨 ...

  9. mongodb的学习-3-在Mac上的安装配置

    1.使用homebrew安装: brew install mongodb 查看安装好的版本: mongo --version MongoDB shell version v3.6.4 git vers ...

  10. Splay普及版

    辣么,我要介绍我自学的\(Splay\)了,虽然跟大佬们讲得会有些重复,但是自认为把一些玄妙的东西点出来了\(qwq\) \(0x01\) 引言 首先,我并没觉得\(Splay\)有多难--代码长的原 ...