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)的情况,出 ...
 
随机推荐
- Springmvc UPDATE 数据时    ORA-01858:a non-numeric character was found where a numeric was expected
			
ORA-01858:a non-numeric character was found where a numeric was expected 异常. 我的代码: 主要是绑定变量带出来的问题. 出错 ...
 - jumpserver v0.5.0 创建用户和管理机器
			
用户管理-创建用户 data 用户详情 如下 创建用户组 data 资产列表添加资产 jumpserver 的 root 公钥需保持到 后端服务器的 authorized_keys 里, 然后测 ...
 - Git 创建仓库
			
本文将为大学介绍如何创建一个远程的Git仓库.您可以使用一个已经存在的目录作为Git仓库或创建一个空目录. 使用您当前的目录作为Git仓库,我们只需要使它初始化. git init 使用我们指定目录作 ...
 - SQLyog之MySQL客户端的下载、安装和使用(普通版)
			
本博文的主要内容有 .SQLyog的下载 .SQLyog的安装 .SQLyog的使用 前期,安装这个,不多说 MySQL Server类型之MySQL客户端工具的下载.安装和使用 1.SQLyog的下 ...
 - 任务调度Cron表达式及Quartz代码详解
			
在线Cron表达式生成器 http://cron.qqe2.com/ cron表达式详解 http://www.cnblogs.com/linjiqin/archive/2013/07/08/3178 ...
 - php的颜色定义表
			
http://outofmemory.cn/code-snippet/1960/php-color-define-table <? /////////////////////////////// ...
 - PHP案例:学生信息管理系统
			
-- Database: test -- 表的结构 message CREATE TABLE `message` ( `id` tinyint(1) NOT NULL PRIMARY KEY AUTO ...
 - JVM Specification 9th Edition (2) Chapter 1. Introduction
			
Chapter 1. Introduction 翻译太累了,我就这样的看英文吧. 内容列表 1.1. A Bit of History 1.2. The Java Virtual Machine 1. ...
 - 如何使用VMWare共享Win7中的文件夹,对应Linux中的哪个目录下面?
			
访问 /mnt/hgfs/你设置的共享名,如果找不到这个hgfs这个文件夹,那说明你还没正确安装好 install VMware tools
 - boa web服务器
			
Boa是一种非常小巧的Web服务器,其可执行代码只有大约60KB左右.作为一种单任务Web服务器,Boa只能依次完成用户的请求,而不会fork出新的进程来处理并发连接请求.但Boa支持CGI,能够 ...