分析

非常有趣的一道题。

式子中的绝对值很难处理,但是我们发现:

\[\sum_{i=1}^{k}|a_{x,i}-a_{y,i}|=\sum_{i=1}^{k}max(a_{x,i}-a_{y,i},a_{y,i}-a_{x,i})=max\{\sum_{i=1}^{k}c_ia_{x,i}-\sum_{i=1}^{k}c_ia_{y,i}\}
\]

其中\(c\)是所有长度为\(k\)的只由\(-1\)和\(1\)组成的数列,共有\(2^k\)种。

所以我们可以对于每一种\(c\)维护一棵支持单点修改,查询区间最小值和最大值的线段树,对所有的极差取\(max\)即可。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
#define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
typedef long long LL;
using std::cin;
using std::cout;
using std::endl; inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
} const int MAXN=200005;
int n,k,q,cnt,loc,ql,qr,xx[MAXN][6];
struct sgt{
int maxn[33],minn[33];
}a[MAXN<<2],kk; #define mid ((l+r)>>1)
#define lc (o<<1)
#define rc ((o<<1)|1)
inline sgt mer(sgt x,sgt y){
rin(i,0,cnt-1) x.maxn[i]=std::max(x.maxn[i],y.maxn[i]),x.minn[i]=std::min(x.minn[i],y.minn[i]);
return x;
} void build(int o,int l,int r){
if(l==r){
rin(i,0,cnt-1){
rin(j,0,k-1){
if((i>>j)&1) a[o].maxn[i]+=xx[l][j];
else a[o].maxn[i]-=xx[l][j];
}
a[o].minn[i]=a[o].maxn[i];
}
return;
}
build(lc,l,mid);build(rc,mid+1,r);
a[o]=mer(a[lc],a[rc]);
} void upd(int o,int l,int r){
if(l==r){
a[o]=kk;
return;
}
if(loc<=mid) upd(lc,l,mid);
else upd(rc,mid+1,r);
a[o]=mer(a[lc],a[rc]);
} sgt query(int o,int l,int r){
if(ql<=l&&r<=qr) return a[o];
if(mid<ql) return query(rc,mid+1,r);
else if(mid>=qr) return query(lc,l,mid);
else return mer(query(lc,l,mid),query(rc,mid+1,r));
}
#undef mid
#undef lc
#undef rc int main(){
n=read(),k=read();cnt=(1<<k);
rin(i,1,n) rin(j,0,k-1) xx[i][j]=read();
build(1,1,n);
q=read();
while(q--){
int opt=read();
if(opt==1){
loc=read();rin(i,0,k-1) xx[0][i]=read();
rin(i,0,cnt-1){
kk.maxn[i]=0;
rin(j,0,k-1){
if((i>>j)&1) kk.maxn[i]+=xx[0][j];
else kk.maxn[i]-=xx[0][j];
}
kk.minn[i]=kk.maxn[i];
}
upd(1,1,n);
}
else{
ql=read(),qr=read();
sgt Ans=query(1,1,n);int ans=0;
rin(i,0,cnt-1) ans=std::max(ans,Ans.maxn[i]-Ans.minn[i]);
printf("%d\n",ans);
}
}
return 0;
}

[CF1093G]Multidimensional Queries:线段树的更多相关文章

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

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

  2. [CF1093G]Multidimensional Queries

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

  3. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...

  4. hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和

    Can you answer these queries? Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  5. Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~

                                                                                                        ...

  6. HDU4027 Can you answer these queries? —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...

  7. HDU-4027-Can you answer these queries?线段树+区间根号+剪枝

    传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...

  8. Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

    链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...

  9. HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

    题目 线段树 简单题意: 区间(单点?)更新,区间求和  更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...

随机推荐

  1. Oracle 单实例安装篇

    linux中启动网卡报错:Error: Connection activation failed: Connection 'eth0' is not available on the device e ...

  2. numpy使用数组进行数据处理

    numpy使用数组进行数据处理 meshgrid函数 理解: 二维坐标系中,X轴可以取三个值1,2,3, Y轴可以取三个值7,8, 请问可以获得多少个点的坐标? 显而易见是6个: (1,7)(2,7) ...

  3. 3D max导出的设置选项

    一3D max导出的设置选项

  4. 01Spring基于xml的IOC配置--入门

    01Spring基于xml的IOC配置 1.创建一个普通的maven工程 1.1 选择maven,不用骨架,点击下一步. 1.2 填写GroupId.ArtifactId.Version.填完点击下一 ...

  5. 28、周末看电影(每周五自动从top250中随机选取三部电影,并将下载链接发到邮箱里)

      练习介绍   在第3关的一个课后练习里,我们爬取了指定电影的下载链接:在第6关的一个课后练习里,我们存储了豆瓣TOP250的榜单信息.   现在,我们完善这个程序,每周五在存储的榜单中,随机抽三部 ...

  6. 配置ShiroFilter需要注意的问题(Shiro_DelegatingFilterProxy)

    ShiroFilter的工作原理 ShiroFilter:DelegatingFilterProxy作用是自动到Spring 容器查找名字为shiroFilter(filter-name)的bean并 ...

  7. 016-zabbix低级自动发现以及MySQL多实例

    1.概述 Zabbix的网络发现是指zabbix server通过配置好的规则,自动添加host,group,template Zabbix的主动注册刚好和网络发现是相反的,功能基本一致.zabbix ...

  8. Spring Framework Part1

    初识Spring 1.Spring是一个支持IOC(Inversion of Control),DI(Dependency Injection),AOP(Aspect Oriented Program ...

  9. Saving Tang Monk II HihoCoder - 1828 2018北京赛站网络赛A题

    <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chines ...

  10. 漫谈 MyCat 配置系统

    上篇文章<MyCat 启蒙:分布式系统的数据库架构演变>中,我们通过一个项目从零到百万级访问的变化,展示了这个过程中的数据层架构变化.其中说到了数据层架构变化所带来的三个问题: 读写数据源 ...