URAL 2062 Ambitious Experiment(树状数组)
2016-08-23 21:43
447 查看
题目地址:http://acm.timus.ru/problem.aspx?space=1&num=2062
思路:对于x位置上的数,对其有贡献的数为位置为x的因数且属于修改区间[l,r]的数。所以对于查询操作ans=a[p]+sigma(sum(j)) (j为p的约数)。区间修改时仅需修改区间端点值:c[l]+=d,c[r+1]-=d(当数x在某个无重复区间[l,r]时,每次求前x项的和时已包含该区间即已加上该数贡献,当x>r时,求前x项和时,该区间和为0,即数x贡献为0)。
思路:对于x位置上的数,对其有贡献的数为位置为x的因数且属于修改区间[l,r]的数。所以对于查询操作ans=a[p]+sigma(sum(j)) (j为p的约数)。区间修改时仅需修改区间端点值:c[l]+=d,c[r+1]-=d(当数x在某个无重复区间[l,r]时,每次求前x项的和时已包含该区间即已加上该数贡献,当x>r时,求前x项和时,该区间和为0,即数x贡献为0)。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long LL; const int maxn=3e5+50; int n,q; LL c[maxn],a[maxn]; int lowbit(int x) { return x&(-x); } void add(int x,int w) { while(x<=n) { c[x]+=w; x+=lowbit(x); } } LL sum(int x) { LL tot=0; while(x) { tot+=c[x]; x-=lowbit(x); } return tot; } int main() { scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&a[i]); scanf("%d",&q); while(q--) { int id; scanf("%d",&id); if(id==2) { int l,r,w; scanf("%d%d%d",&l,&r,&w); add(l,w); add(r+1,-w); } else { int p; scanf("%d",&p); LL ans=a[p]; for(int i=1; i*i<=p; i++) { if(p%i) continue; ans+=sum(i); if(i!=p/i) ans+=sum(p/i); } printf("%I64d\n",ans); } } return 0; }
相关文章推荐
- Ural 1910. Titan Ruins: Hidden Entrance
- [HEOI2012] 采花题解报告
- Codeforces Coder-Strike 2014 - Finals (online edition, Div. 1)
- HDU 1166 敌兵布阵
- POJ 2352 Stars
- PAT 1057 Stack (30)
- neu 1438 树状数组求逆序数
- Binary
- poj 2352
- 树状数组
- 树状数组 Binary Indexed Tree
- hdu 4630 No Pain No Game 树状数组
- POJ2352 Stars(树状数组 or SplayTree)
- 树状数组区间求和的三种模型
- URAL - 2018 The Debut Album
- HDOJ1166 敌兵布阵 树状数组
- Ural 1068
- 二维树状数组
- Hoj 2275 Number Sequence
- POJ3321 Apple Tree