Hdu 4267 A Simple Problem with Integers

本文介绍如何使用线段树解决整数数组上的区间加法和查询问题,包括输入解析、操作类型处理和输出生成。

A Simple Problem with Integers

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


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
 
 
  
Source
 
 
  
Recommend
liuyiding
//怎么说呢、自己真的好弱
//线段树关键在于节点存储信息的意义便于求解
//本来不连续的更新,通过研究、使其成段更新,
h[i][j]代表某个数mod i 的余数为j ,共有55种情况
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <queue> #define lson l,m,k<<1 #define rson m+1,r,k<<1|1 using namespace std; #define N 50010 struct node { int rec[55]; int sum; }st[N<<2]; int h[11][11]; void build(int l,int r,int k) { int m; for(m=0;m<55;m++) st[k].rec[m]=0; if(l==r) { scanf("%d",&st[k].sum); return ; } m=(l+r)>>1; build(lson); build(rson); } int kk,c,remain; void update(int &L,int &R,int l,int r,int k) { if(L<=l&&R>=r) { st[k].rec[h[kk][remain]]+=c; return ; } int m=(l+r)>>1; if(L<=m) update(L,R,lson); if(R>m) update(L,R,rson); } void down(int &k) { int i; for(i=0;i<55;i++) { if(st[k].rec[i]){ st[k<<1].rec[i]+=st[k].rec[i]; st[k<<1|1].rec[i]+=st[k].rec[i]; st[k].rec[i]=0; } } } int query(int &id,int l,int r,int k) { if(l==r) { int i,t=0; for(i=1;i<=10;i++) t+=st[k].rec[h[i][id%i]]; return t+st[k].sum; } int m=(l+r)>>1; down(k); if(id<=m) return query(id,lson); else return query(id,rson); } int main() { int n; int q,i,a,b=0; for(i=1;i<=10;i++) for(a=0;a<i;a++) h[i][a]=b++; while(scanf("%d",&n)!=EOF) { build(1,n,1); scanf("%d",&q); while(q--) { scanf("%d",&i); if(i==1) { scanf("%d %d %d %d",&a,&b,&kk,&c); remain=a%kk; update(a,b,1,n,1); } else { scanf("%d",&a); printf("%d\n",query(a,1,n,1)); } } } return 0; }

转载于:https://www.cnblogs.com/372465774y/archive/2012/09/12/2682536.html

HDU 2034 是一道经典的 A-B Problem 题目,通常涉及简单的数学运算或者字符串处理逻辑。以下是对此类问题的分析以及可能的解决方法。 ### HDU 2034 的题目概述 该题目要求计算两个数之间的差值 \(A - B\) 并输出结果。需要注意的是,输入数据可能存在多种情况,因此程序需要能够适应不同的边界条件和特殊情况[^1]。 #### 输入描述 - 多组测试数据。 - 每组测试数据包含两行,分别表示整数 \(A\) 和 \(B\)。 #### 输出描述 对于每组测试数据,输出一行表示 \(A - B\) 的结果。 --- ### 解决方案 此类问题的核心在于正确读取多组输入并执行减法操作。以下是实现此功能的一种常见方式: ```python while True: try: a = int(input()) b = int(input()) print(a - b) except EOFError: break ``` 上述代码片段通过循环不断接收输入直到遇到文件结束符 (EOF),适用于批量处理多组测试数据的情况。 --- ### 特殊考虑事项 尽管基本思路简单明了,在实际编码过程中仍需注意以下几点: 1. **大数值支持**:如果题目中的 \(A\) 或 \(B\) 可能非常大,则应选用可以容纳高精度的数据类型来存储这些变量。 2. **负数处理**:当 \(B>A\) 导致结果为负时,确保程序不会因符号错误而失效。 3. **异常捕获**:为了防止运行期间由于非法字符或其他意外状况引发崩溃,建议加入必要的错误检测机制。 --- ### 示例解释 假设给定如下样例输入: ``` 5 3 7 2 ``` 按照以上算法流程依次完成各步操作后得到的结果应当分别为 `2` 和 `5`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值