【HDOJ】4267 A Simple Problem with Integers
树状数组。Easy.
/* 4267 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
const int maxm = ;
int n; inline int lowest(int x) {
return -x & x;
} typedef struct arr_t {
int s[maxn]; void clr() {
memset(s, , sizeof(s));
} void update(int x, int delta) {
while (x <= n) {
s[x] += delta;
x += lowest(x);
}
} int sum(int x) {
int ret = ; while (x) {
ret += s[x];
x -= lowest(x);
}
return ret;
}
} arr_t; arr_t ts[maxm];
int A[maxn], M[][maxn];
int ID[][]; void init_() {
int cnt = ; rep(i, , ) {
rep(j, , i) {
ID[i][j] = cnt++;
}
} rep(i, , ) {
int k = i-;
rep(j, , maxn) {
M[k][j] = M[k][j-] + ;
if (M[k][j] == i)
M[k][j] = ;
}
}
} void init() {
rep(i, , maxm)
ts[i].clr();
} void update(int a, int b, int k, int delta) {
int from = a;
int to = a+(b-a)/k*k;
int findex = (from+k-)/k;
int tindex = (to+k-)/k;
int id = ID[k][a%k]; ts[id].update(findex, delta);
ts[id].update(tindex+, -delta);
} int sum(int x) {
int id, index, delta;
int ret = ; rep(i, , ) {
index = (x+i-)/i;
id = ID[i][M[i-][x]];
delta = ts[id].sum(index);
ret += delta;
}
return ret;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int type;
int a, b, k, c;
int q;
int ans; init_();
while (scanf("%d", &n)!=EOF) {
rep(i, , n+)
scanf("%d", &A[i]);
init();
scanf("%d", &q);
while (q--) {
scanf("%d %d", &type, &a);
if (type == ) {
scanf("%d %d %d", &b, &k, &c);
update(a, b, k, c);
} else {
ans = sum(a) + A[a];
printf("%d\n", ans);
}
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】4267 A Simple Problem with Integers的更多相关文章
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- 【POJ】3468 A Simple Problem with Integers
这题用线段树轻松解了,重新用树状数组解,关键点是区间更新.公式推导如下:sum[x] = org_sum[x] + delta[1]*x + delta[2]*(x-1) + delta[x]*1 ...
- HDU 4267 A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4267 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers
http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...
- 【HDOJ】4972 A simple dynamic programming problem
水题. #include <cstdio> #include <cstring> #include <cstdlib> int abs(int x) { ? -x: ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- HDOJ 4267 A Simple Problem with Integers (线段树)
题目: Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of opera ...
- 【HDOJ】3828 A + B problem
显然需要贪心,重叠越长越好,这样最终的串长尽可能短.需要注意的是,不要考虑中间结果,显然是个状态dp.先做预处理去重,然后求任意一对串的公共长度. /* 3828 */ #include <io ...
随机推荐
- 区分.net、c#、asp.net三者间的关系
1..net(dot net) .net是一个平台,抽象的平台概念. 实现形式是库:①定义了基本的类型(通用类型系统CTS,common type system). ②包含.net公共语言运行库( ...
- JS的词法作用域
词法作用域定义实现的规则: 1 函数作用域实在定义的时候决定的,而不是在执行时候决定 2 为了实现这种词法作用域,函数内部不仅包含函数代码逻辑,还必须引用当前的作用域链. 3 函数对象可以通过作用域链 ...
- hdu 5014 Number Sequence
为了a异或b的和最大,只需另b在不大于n的情况下按位取反即可. 这里有两个输出小技巧可以参考: 1.在用printf输出__int64时,在windows下使用格式"%I64d", ...
- Mac OS X平台上Java环境的配置
最近换了工作,以前是做c/c++的,但是现在公司的主打产品是使用Java开发,为了以后维护代码,现在开始抽空学习一下Java相关的内容. 在学习之前,首先需要搭建各种平台的开发环境,而我选用的操作系统 ...
- Python3 面向对象 高级编程
正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性. class Student(object): pass 然后,尝试 ...
- 算法之插入排序(inertionSort)
插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,适用于少量数据的排序,时间复杂度为O(n^2). 插入排序属于稳定排序. 插入排序不需要构造新的数据 ...
- [C#]获取一年中是第几个星期
关键代码: /// <summary> /// 获取日期是一年中第几个星期 /// </summary> /// <param name="date" ...
- SAP Java Connector(JCo)
JCo是一个高性能的,基于JNI的中间件,它实现了SAP的RFC(Remote Function Call)协议. 1.JCo的安装 从 http://files.cnblogs.com/byfhd/ ...
- springdata整合mongodb一些方法包括or,and,regex等等《有待更新》
这几天接触mongodb以及springdata,自己英语比较戳,所以整理这些方法花的时间多了点,不过也是我第一次在外国网站整理技术 不多说,直接上代码,这里只是给出一些操作方法而已,如果有需要源码的 ...
- MIT 2012分布式课程基础源码解析-事件管理封装
这部分的内容主要包括Epoll/select的封装,在封装好相应函数后,再使用一个类来管理相应事件,实现的文件为pollmgr.{h, cc}. 事件函数封装 可看到pollmgr.h文件下定一个了一 ...