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. Python学习---爬虫学习[requests模块]180411

    模块安装 安装requests模块 pip3 install requests 安装beautifulsoup4模块 [更多参考]https://blog.csdn.net/sunhuaqiang1/ ...

  2. windows server 2016部署服务

          绪论 有时候,如果安装系统数量比较多,而且数量较多,人工使用U盘或者光盘安装,效率较低,通过网络可以批量安装. 还可以做到无人安装. PXE PXE(preboot execute env ...

  3. 函数的调用 and 打印返回值 ret= mai() print(ret)

    def mai(): # mai 函数名 (yan) 形式参数 print("老板,给我一包中华") return"给你" # 返回值-- 给你ret = ma ...

  4. 关于Excel中的数据透视表没有数据

    在你想要使用数据透视表的时候,区域一定要正确 然后把你想要的数据按行列排好 如果没有数据 请点击刷新数据……刷新数据……刷新数据 我竟然被这个睿智的问题困扰好久……

  5. PHP设计模式系列 - 中介者模式

    中介者模式 中介者模式用于开发一个对象,这个对象能够在类似对象相互之间不直接相互的情况下传送或者调解对这些对象的集合的修改.一般处理具有类似属性,需要保持同步的非耦合对象时,最佳的做法就是中介者模式. ...

  6. Eclipse插件安装方法大全

    1. M2e maven2插件安装 参考地址:http://www.sonatype.com/books/m2eclipse-book/reference/install-sect-marketpla ...

  7. Java Math类学习

    1.  java.lang.Math Math类其成员皆为静态成员(static),无需创建对象,直接用类名Math作为前缀使用它们即可. 2.  Math类有两个静态常量:E(自然对数)和PI(圆周 ...

  8. 扯不清楚的virtual和abstract

    定义Person类: class Person { public void Say() { Console.WriteLine("I am a person"); } } 现在,我 ...

  9. BZOJ4241:历史研究(回滚莫队)

    Description IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记.JOI教授为了通过这份日记来研究古代IOI国的生活,开始着手调查日记中记载的事件. ...

  10. Linux - 版本控制系统SVN

    0. 摘要 本文通过搭建SVN多版本库为例,介绍SVN的使用. SVN是一个集中式版本控制系统,在服务端部署中央版本库,所有开发人员客户端连接到中央版本库进行代码的提交和更新. Apache Subv ...