CF 1093G Multidimensional Queries——线段树(消去绝对值符号)
题目:http://codeforces.com/contest/1093/problem/G
只好看看题解:https://codeforces.com/blog/entry/63877
主要是把绝对值符号消掉,变成枚举正负。因为答案不会变差,所以不用管符号应该是什么,直接对应地取 max 、 min 即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls Ls[cr]
#define rs Rs[cr]
using namespace std;
const int N=2e5+5,M=N<<1,K=10,tK=35,INF=5e6+5;
int n,k,lm,bin[K],Q,a[N][K],tot,Ls[M],Rs[M];
int Mx(int a,int b){return a>b?a:b;}
int Mn(int a,int b){return a<b?a:b;}
struct Node{
int mx[tK],mn[tK];
Node operator+ (const Node &b)const
{
Node c;
for(int i=0;i<lm;i++)
{
c.mx[i]=Mx(mx[i],b.mx[i]);
c.mn[i]=Mn(mn[i],b.mn[i]);
}
return c;
}
}sm[M];
int rdn()
{
int ret=0;bool fx=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();}
while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
return fx?ret:-ret;
}
void dfs(int cr,int lj,int s,int id,int bh)
{
if(cr>k)
{
sm[bh].mn[s]=sm[bh].mx[s]=lj;
return;
}
dfs(cr+1,lj+a[id][cr],s|bin[cr],id,bh);
dfs(cr+1,lj-a[id][cr],s,id,bh);
}
void build(int l,int r,int cr)
{
if(l==r){dfs(1,0,0,l,cr);return;}
int mid=l+r>>1;
ls=++tot;build(l,mid,ls);
rs=++tot;build(mid+1,r,rs);
sm[cr]=sm[ls]+sm[rs];
}
void updt(int l,int r,int cr,int p)
{
if(l==r){dfs(1,0,0,l,cr);return;}
int mid=l+r>>1;
if(p<=mid)updt(l,mid,ls,p);
else updt(mid+1,r,rs,p);
sm[cr]=sm[ls]+sm[rs];
}
Node query(int l,int r,int cr,int L,int R)
{
if(l>=L&&r<=R)return sm[cr];
int mid=l+r>>1;
if(L>mid)return query(mid+1,r,rs,L,R);
if(R<=mid)return query(l,mid,ls,L,R);
return query(l,mid,ls,L,R)+query(mid+1,r,rs,L,R);
}
int main()
{
n=rdn();k=rdn();lm=(1<<k);
bin[1]=1;for(int i=2;i<=k;i++)bin[i]=bin[i-1]<<1;
for(int i=1;i<=n;i++)
for(int j=1;j<=k;j++)a[i][j]=rdn();
tot=1;build(1,n,1);
Q=rdn();
for(int i=1,op,id,l,r;i<=Q;i++)
{
op=rdn();
if(op==1)
{
id=rdn();
for(int j=1;j<=k;j++)a[id][j]=rdn();
updt(1,n,1,id);
}
else
{
l=rdn();r=rdn();
Node d=query(1,n,1,l,r);int ans=-INF;
for(int i=0;i<lm;i++)
ans=Mx(ans,d.mx[i]-d.mn[i]);
printf("%d\n",ans);
}
}
return 0;
}
CF 1093G Multidimensional Queries——线段树(消去绝对值符号)的更多相关文章
- 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 ...
- CF 1023D Array Restoration - 线段树
题解 非常容易想到的线段树, 还可以用并查集来. 还有一位大神用了$O(n)$ 就过了Orz 要判断是否能染色出输入给出的序列,必须满足两个条件: 1. 序列中必须存在一个$q$ 2. 两个相同的数$ ...
- 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 ...
- CF 787D Legacy(线段树思想构图+最短路)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- 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:优化:因为每次更新都 ...
随机推荐
- 51NOD-1960-数学/贪心
1960 范德蒙矩阵 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 LYK最近在研究范德蒙矩阵与矩阵乘法,一个范德蒙矩阵的形式如下: 它想通过构 ...
- 通过windows的超级终端连接华为交换机
如果你的电脑没有带COM接口 可以找一个COM--->USB的转换器 我前面有介绍过这么安装COM转USB设备的驱动 2 先打开你电脑的设备管理器 看看你的com接口数字编号 这里看到的是COM ...
- vux配置i18n
根据使用文档,先引入i18n import VueI18n from 'vue-i18n'; Vue.use(VueI18n) const i18n = new VueI18n({ locale: ' ...
- React-Native进阶_2.加载指示动画 ActivityIndicator
在安卓原始 App中使用的加载框 ProgressBar 在React -Native 中也是有相对应的视图,叫做ActivityIndicator,对应ios 中React-Native 提供的是 ...
- Xcode9 新功能
翻译: https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/WhatsNewXcod ...
- js `` 手机不支持
- 如何制作dll库的API文档,自动生成微软风格的chm文件 Sandcastle Help File Builder 使用方法
当你开发了一个库的时候,就需要给库开发一个api文档,微软提供了一个C#库的自动生成工具.我在使用的过程中记录了相关的信息,以供大家学习和查阅,如有不正之处,欢迎指出. 首先先下载一个软件,下载地址在 ...
- JavaScript class 使用
/********************************************************************* * JavaScript class 使用 * 说明: * ...
- 有关linux中,<math.h>的调用方法
h{font-weight:bold;color:green;font-size:105%} p{font-size:100%} linux下C语言程序中,若要用到math.h中的函数(如:sin() ...
- js之隔行换色
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...