HDU4267(2012年长春站)
这道题真的是好题,让我对线段树有了全新的认识,至少让我真正感受到了线段树的神奇。
题意是就是线段树区间更新,单点询问的问题,不过这个题好就好在它的区间更新的点并不连续!
adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0.
关键在于更新的点所满足的条件 1.在区间[a,b]中。 2.关于k的余数与(a%k)相等。
其中第二个条件保证了更新的点不连续(但如果传统的先判断再更新,则线段树失去了优势,因为始终会超时)。
本题突破口在于K的大小(1 <= k <= 10),结合第二个条件,我们可以想到在线段树的 Lazy标记上做文章。
当k==1 余数有 0
当k==2 余数有 0,1
当k==3 余数有 0,1,2
。。。。。
可以看到,只有55种情况,那么我们只需要将线段树的 Lazy标记 扩充为大小55的数组,
然后根据相应的余数情况进行更新与查询即可。
#include<iostream>
#include<sstream>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cctype>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r #define inf 0x3f3f3f3f
#define N 50010
#define maxn 10001000
#define mod 1000000007
using namespace std; int a[N];
int b[][]; //用于将55种余数情况一 一 对 应
struct NODE{
int l,r;
int v;
int b[];
int mid(){
return (l+r)>>;
}
}node[N<<]; void build(int root,int l,int r)
{
node[root].l=l;
node[root].r=r;
node[root].v=;
memset(node[root].b,,sizeof(node[root].b));
if(l==r) return;
int mid=node[root].mid();
build(lson);
build(rson);
} void pushdown(int root) //关于pushdown函数,个人觉得只需要询问时调用就行了,而且也过了,应该没问题,有的话请指出,谢谢
{
for(int i=; i<=; ++i)
{
for(int j=; j<i; ++j)
{
node[root<<].b[b[i][j]]+=node[root].b[b[i][j]];
node[root<<|].b[b[i][j]]+=node[root].b[b[i][j]];
}
}
memset(node[root].b,,sizeof(node[root].b));
} void update(int x,int y,int root,int v,int k,int rd)
{
if(node[root].l==x&&node[root].r==y)
{
node[root].b[b[k][rd]]+=v;
node[root].v+=v;
return;
}
int mid=node[root].mid();
if(y<=mid) update(x,y,root<<,v,k,rd);
else if(x>mid) update(x,y,root<<|,v,k,rd);
else
{
update(x,mid,root<<,v,k,rd);
update(mid+,y,root<<|,v,k,rd);
}
} int query(int root,int x)
{
if(node[root].l==x&&node[root].r==x)
{
int ans=;
for(int i=; i<=; ++i)
ans+=node[root].b[b[i][x%i]];
return ans;
}
pushdown(root);
int mid=node[root].mid();
if(x<=mid) return query(root<<,x);
else return query(root<<|,x);
} int main()
{
// freopen("lxx.txt","r",stdin);
int group,figure,i,j,x,y,k,v,num;
int cnt=;
for(i=; i<=; ++i)
for(j=; j<i; ++j)
b[i][j]=cnt++;
while(~scanf("%d",&group))
{
build(,,group);
memset(a,,sizeof(a));
for(i=; i<=group; ++i) scanf("%d",&a[i]);
scanf("%d",&group);
while(group--)
{
scanf("%d",&num);
if(num==)
{
scanf("%d%d%d%d",&x,&y,&k,&v);
update(x,y,,v,k,x%k);
}
else
{
scanf("%d",&figure);
printf("%d\n",query(,figure)+a[figure]);
}
}
}
return ;
}
不过感觉自己的代码风格还不够成熟,继续努力!
HDU4267(2012年长春站)的更多相关文章
- Windows server 2012 添加中文语言包(英文转为中文)(离线)
Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...
- Windows Server 2012 NIC Teaming介绍及注意事项
Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...
- 1.初始Windows Server 2012 R2 Hyper-V + 系统安装详细
干啥的?现在企业服务器都是分开的,比如图片服务器,数据库服务器,redis服务器等等,或多或少一个网站都会用到多个服务器,而服务器的成本很高,要是动不动采购几十台,公司绝对吃不消的,于是虚拟化技术出来 ...
- 0.Win8.1,Win10,Windows Server 2012 安装 Net Framework 3.5
后期会在博客首发更新:http://dnt.dkill.net 网站部署之~Windows Server | 本地部署:http://www.cnblogs.com/dunitian/p/482280 ...
- windows 2012 r2 can't find kb2919355
问题 解决: 1.手动安装了 Windows8.1-KB2919442-x64 2.手动下载 KB2919355 更新成功 Turns out to have been a result ...
- Windows Server 2012 磁盘管理之 简单卷、跨区卷、带区卷、镜像卷和RAID-5卷
今天给客户配置故障转移群集,在Windows Server 2012 R2的系统上,通过iSCSI连接上DELL的SAN存储后,在磁盘管理里面发现可以新建 简单卷.跨区卷.带区卷.镜像卷.RAID-5 ...
- VMware下Windows Server 2012添加新磁盘
系统管理员在VM下新装了一台Windows Server 2012服务器,我在上面安装了SQL Server 2014 Standard版数据库,安装之初,只分配了一个C盘,我想在这台服务器上添加了三 ...
- 在Windows Server 2012中如何快速开关桌面上经典的“计算机、我的文档”等通用图标
我们都知道,在Windows Server 2012系列的服务器版本中都已经引入了Modern的现代界面作为默认的用户交互界面,同时满足视觉一致化,新版的服务器管理程序也做成了扁平化.因此传统的计算机 ...
- VmWare平台Windows Server 2012 无响应宕机
我们生产服务器都部署在VMware ESXi 5.5平台上,最近大半年的时间,偶尔就会出现操作系统为Windows Servre 2012的服务器出现没有任何响应(unresponsive)的情况,出 ...
随机推荐
- C语言指针的易错点
1.内存泄漏:申请的堆内存没有释放. 2.内存污染:前面非法操作使用内存(没有报错),后面写着写着就出错.如下代码: 当结构体中只有划线部分代码时,在编译器中编写不会报错,但此时已经造成非法操作内存, ...
- Linux下自动备份Oracle数据库并删除指定天数前的备份
说明: Oracle数据库服务器 操作系统:CentOS IP:192.168.0.198 端口:1521 SID:orcl Oracle数据库版本:Oracle11gR2 具体操作: 1.root用 ...
- 如何给app客户端进行埋点?
1.什么是数据埋点? 当一款app开发完成后,一定需要查看数据,来分析用户访问量,点击量,转化率等等指标.想要查看数据,就得先做好数据埋点 注:html和app数据埋点的方式不同 2.常用app数据分 ...
- sklearn中xgboost模块中plot_importance函数(特征重要性)
# -*- coding: utf-8 -*- """ ######################################################### ...
- js一点代码备用
加载次序 .1等页面加载完毕 <script type="text/javascript"> jQuery(window).load(function(){ ... } ...
- spring和hibernate整合,事务管理
一.spring和hibernate整合开发步骤 1 引入jar文件,用户libarary列表如下 //spring_core spring3..9core\commons-logging-1.2.j ...
- log4j使用示例
### set log levels ### log4j.rootLogger = INFO , D #INFO , C , D , E ### console ### #log4j.appender ...
- Java中数据库连接的一些方法资料汇总
Java中Connection方法笔记 http://www.cnblogs.com/bincoding/p/6554954.html ResultSet详解(转) https://www.cnbl ...
- python之圆周率
#!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: pi.py #Author: ...
- iOS swift 富文本显示 富文本在iOS中使用场景和解决方案
项目中很多地方都会用到富文本的内容:比如一般的商品详情,视频详情,资讯详情等,运营人员通过后台的富文本编辑器编辑的内容,前端拿到的就是一段富文本的字符串,这富文本大多都是图片和文字的组合.我们今天介绍 ...