[CF1093G]Multidimensional Queries:线段树
分析
非常有趣的一道题。
式子中的绝对值很难处理,但是我们发现:
\]
其中\(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:线段树的更多相关文章
- CF 1093G Multidimensional Queries——线段树(消去绝对值符号)
题目:http://codeforces.com/contest/1093/problem/G 只好看看题解:https://codeforces.com/blog/entry/63877 主要是把绝 ...
- [CF1093G]Multidimensional Queries
[CF1093G]Multidimensional Queries 题目大意: \(k(k\le5)\)维空间中有\(n(n\le2\times10^5)\)个点.\(m\)次操作,操作包含一下两种: ...
- 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 ...
- 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 ...
- Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~
...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- HDU-4027-Can you answer these queries?线段树+区间根号+剪枝
传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...
- 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 ...
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...
随机推荐
- webpack的介绍
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA5EAAAGpCAIAAACbBiUBAAAgAElEQVR4Aey9CVwb553/L04JcSPuw5
- c#中抽象类和接口的相同点跟区别
下面是自己写的一个demo,体现抽象类和接口的用法. using System; using System.Collections.Generic; using System.Linq; using ...
- CF 11D A Simple Task 题解
题面 这道题的数据范围一看就是dfs或状压啦~ 本文以状压的方式来讲解 f[i][j]表示目前的节点是i,已经经历过的节点的状态为j的简单环的个数: 具体的转移方程和细节请看代码: PS:(i& ...
- layer弹出框的简易封装和使用
1. 封装layer 下载layer绿色版和jquery引入页面 <!DOCTYPE html> <html lang="zh-CN"> . . . < ...
- Mysql 表空间和 数据页空洞
一.表空间1.表空间: innodb 引擎存储的最高层: 存放所有的数据2.独立表空间:Mysql 版本 5.6 后默认开启的单表单空间(1)Innodb 默认存储引擎页的大小为 16K :默认表空间 ...
- Win10下编译OpenJDK8
导航目录 Win10下编译OpenJDK8 相关参考文章 编译环境 编译前准备 1.安装 Visual Studio 2010 Professional 2. 准备OpenJDK8 3. 编译JDK环 ...
- hackthebox通关手记(持续更新)
简介: 花了点时间弄了几道题目.以前我是用windows渗透居多,在kali linux下渗透测试一直不怎么习惯.通过这几天做这些题目感觉顺手多了.有些题目脑洞也比较大,感觉很多也不适合于实际的环境 ...
- 配置 http 反向代理
[root@nginx ~]# cd /etc/nginx/ 1 [root@nginx nginx]# cp nginx.conf nginx.conf.bak #备份一个原配置文件 2 [root ...
- 015-Zabbix自动发现和自动注册
前言 对于监控服务器越来越多的情况,如果还单独一个一个添加,那效率也太低,因此就要实现批量添加监控服务器的操作,Zabbix提供两种批量自动监控的方式: 自动发现:由服务端主动发起,Zabbix ...
- JDK的二进制安装
JDK的二进制安装步骤 1. Jdk1.8二进制包下载路径http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- ...