A Simple Problem with Integers

Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2571 Accepted Submission(s): 837

Problem Description
Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. The other is to query the value of some element.
 
Input
There are a lot of test cases.

The first line contains an integer N. (1 <= N <= 50000)

The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)

The third line contains an integer Q. (1 <= Q <= 50000)

Each of the following Q lines represents an operation.

"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)

"2 a" means querying the value of Aa. (1 <= a <= N)

 
Output
For each test case, output several lines to answer all query operations.
 
Sample Input
4
1 1 1 1
14
2 1
2 2
2 3
2 4
1 2 3 1 2
2 1
2 2
2 3
2 4
1 1 4 2 1
2 1
2 2
2 3
2 4
 
Sample Output
1
1
1
1
1
3
3
1
2
3
4
1
这题好卡内存,发现用结构比直接用数组要省很多啊,结果搞的我卡了不知道多少次啊!分析一下题,一看,就知道是要用线段树,但是和别的不一样,就是,它是单点更新,
为什么是单点更新呢?因为在一个区间上,并不是没一个都要更新!但这一点,对于线段树来说,就是非常不利的了,因为,线段树就是要成段更新,如果是,一个点一个点,
那么线段树便失去了作用,那么还有什么意义呢?所以,我们要用一个数组,先保存所有要更新的信息,也就是(i-a)%k==0转化成(i%k)==l%k;因为l%k是很快可以算出来的,我在在要更新区间上,把这个相应的数组加累加起来,也就是把所有的(l%k)加起来,到了最后查询的时候只把,(i%k),的加起来,这样,不但满足了题意,也用了延时标记,把点连成了线,最后要查询的时候,再把所有的和加起来并更新,就可以了!很好线段树了!
#include <string.h>
#include <iostream>
#include <stdio.h>
using namespace std;
#define MAXN 50005
#define lnum num<<1
#define rnum num<<1|1 struct node
{
int color,prime,listadd[55];
}tree[4*MAXN];
#define inf 0
int modleft[11][11];
void build(int num ,int l,int r)
{
memset(tree[num].listadd,0,sizeof(tree[num].listadd));
tree[num].color=0;
int mid=(l+r)>>1;
if(l>=r)
{
scanf("%d",&tree[num].prime);
return ;
}
build(lnum,l,mid);
build(rnum,mid+1,r);
}
void pushdown(int num)
{
int i;
if(tree[num].color!=inf)
{ tree[lnum].color=tree[num].color;
tree[rnum].color=tree[num].color;
tree[num].color=inf;//还原标记 for(i=0;i<55;i++)
{ tree[lnum].listadd[i]+=tree[num].listadd[i];
tree[rnum].listadd[i]+=tree[num].listadd[i];
tree[num].listadd[i]=0; }
}
}
void update(int s,int e,int a,int b,int num,int amk,int k,int c )
{
int i; if(a<=s&&b>=e)
{
tree[num].color=k;//需要更新
tree[num].listadd[modleft[k][amk]]+=c;
return ;
}
pushdown(num);
int mid=(s+e)>>1;
if(mid>=a)
update(s,mid,a,b,lnum,amk,k,c);
if(mid<b)
update(mid+1,e,a,b,rnum,amk,k,c);
}
int query(int s,int e,int num,int x)//充分利用这个延时标记
{
int i; if(s>=e)
{
int temp=tree[num].prime;
for(i=1;i<=10;i++)
{
temp+=tree[num].listadd[modleft[i][x%i]];
tree[num].listadd[modleft[i][x%i]]=0;
}
tree[num].prime=temp;//更新为新的值
return temp;
}
pushdown(num);
int mid=(s+e)>>1;
if(x>mid)
return query(mid+1,e,rnum,x);
else
{
return query(s,mid,lnum,x);
}
}
int main()
{
int asknum,num12,x,a,b,n,k,c;
int i,j,cnt=0;
for(i=1;i<=10;i++)
for(j=0;j<i;j++)
{
modleft[i][j]=cnt++;//省了一半的空间
}
while(scanf("%d",&n)!=EOF)
{
build(1,1,n); scanf("%d",&asknum);
while(asknum--)
{ scanf("%d",&num12);
if(num12==1)
{
scanf("%d%d%d%d",&a,&b,&k,&c); update(1,n,a,b,1,a%k,k,c);
}
else{
scanf("%d",&x);
printf("%d\n",query(1,n,1,x));
} }
}
return 0;
}

hdu4267 A Simple Problem with Integers的更多相关文章

  1. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  2. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  3. POJ 3468 A Simple Problem with Integers(线段树/区间更新)

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

  4. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  5. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  6. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  7. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  8. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

随机推荐

  1. Sonar入门(五):使用 Sonar 进行代码质量管理

    Sonar 概述 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具. 与持续集成工具(例如 Hudson/Jenkins ...

  2. Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo

    标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...

  3. [转载]C#开源项目(国外的还是很多)

    C#开源项目(国外的还是很多)一.Ajax框架Ajax.NET Professional (AjaxPro)是最先把AJAX技术在微软.NET环境下的实现的AJAX框架之一.它在客户端脚本之上创建代理 ...

  4. ASP.NET网页抓取数据

    我的数据通过一个TextBox输入,这些代码是写在一个button的点击事件里的. 网页数据抓取大概分为两步,第一步是获取网页源代码: 具体注释如下: var currentUrl = TextBox ...

  5. Asp.Net HttpApplication请求管道与Session(一)

    1.请求处理顺序执行事件 /********************请求处理顺序执行事件**********************/ /// <summary> /// 请求入站 /// ...

  6. c#转码解码

    ///反转码                          mdata[k].MNAME = unescape(mdata[k].MNAME);程家楠 13:51:00 Microsoft.JSc ...

  7. sql语句的分类

    这些天在看Oracle database 11g SQL开发指南,关于sql语句的分类,感觉有必要记录一下. sql语句主要分五类: DML(DATA MANIPULATION LANGUAGE, 数 ...

  8. trailingZeroes

    Given an integer n, return the number of trailing zeroes in n!. 给一个数字n,返回它n!数字后面有多少个0. public class ...

  9. 分布式Session共享(二):tomcat+memcached实现session共享

    一.前言 本文主要测试memcached实现session共享的实现方式,不讨论如何让nginx参与实现负载均衡等. 二.环境配置 本测试在Window下进行 name version port To ...

  10. 网页title标题的闪动效果

    通过网页title来提示用户有新消息这个功能很常见,比如现在的微博,还有一些邮箱,这个功能都很常见. 显示信息数: <input type="text" id="t ...