Codeforces 920F - SUM and REPLACE(线段树)
2018-02-04 15:45
453 查看
题意:给你n个数,进行m次操作,分别是将区间[l,r]内的所有数替换成自己的因子数 和 对区间[l,r]进行求和。
题解:
调试了一下午,原来是运算符号优先级的问题。或运算符和+不要一起用
解析:线段树问题,但是直接做的话会超时,所以要加一个优化,可以发现2的因子个数还是2,1的因子个数还是1,所以如果某个数被更新成1或者2之后就不需要再进行更新了。
这里给张线段树的图
代码:
题解:
调试了一下午,原来是运算符号优先级的问题。或运算符和+不要一起用
解析:线段树问题,但是直接做的话会超时,所以要加一个优化,可以发现2的因子个数还是2,1的因子个数还是1,所以如果某个数被更新成1或者2之后就不需要再进行更新了。
这里给张线段树的图
代码:
#include <bits/stdc++.h> #define ll long long #define pb push_back #define inf 0x3f3f3f3f #define rep(i,a,b) for(int i=a;i<b;i++) #define rep1(i,a,b) for(int i=a;i>=b;i--) #define rson rt<<1|1,m+1,r #define lson rt<<1,l,m using namespace std; const int N=1e6+100; ll arr ,d ,tree[N<<2],mx[N<<2]; void build(int rt,int l,int r) { if(l==r) { tree[rt]=mx[rt]=arr[l]; return; } int m=(l+r)/2; build(lson); build(rson); tree[rt]=tree[rt<<1]+tree[rt<<1|1]; mx[rt]=max(mx[rt<<1],mx[rt<<1|1]); //cout<<rt<<' '<<tree[rt]<<endl; } void update(int L,int R,int rt,int l,int r) { if(mx[rt]<=2) return ; if(l==r) { tree[rt]=mx[rt]=d[tree[rt]]; return ; } int m=(l+r)>>1; if(L<=m) update(L,R,lson); if(R>m) update(L,R,rson); tree[rt]=tree[rt<<1]+tree[rt<<1|1]; mx[rt]=max(mx[rt<<1],mx[rt<<1|1]); } ll query(int L,int R,int rt,int l,int r) { if(L&l 4000 t;=l&&r<=R) { return tree[rt]; } int m=(l+r)>>1; ll ans=0; if(L<=m) ans+=query(L,R,lson); if(R>m) ans+=query(L,R,rson); return ans; } int main() { ios::sync_with_stdio(false); int n,m,t,l,r; for(int i=1;i<N;i++) { for(int j=i;j<N;j+=i) d[j]++; } cin>>n>>m; for(int i=1;i<=n;i++) cin>>arr[i]; build(1,1,n); while(m--) { cin>>t>>l>>r; if(t==1) update(l,r,1,1,n); else cout<<query(l,r,1,1,n)<<endl; } }
相关文章推荐
- Codeforces 920F - SUM and REPLACE【线段树】
- Codeforces 920F-SUM and REPLACE
- Codeforces-920F:SUM and REPLACE(set+树状数组)
- Educational Codeforces Round 37 (Rated for Div. 2) F. SUM and REPLACE(线段树)
- Educational Codeforces Round 37 (Rated for Div. 2) F. SUM and REPLACE(线段树,区间更新)
- Educational Codeforces Round 37 F - SUM and REPLACE (线段树)
- Codeforces 276C Little Girl and Maximum Sum 线段树区间累加
- Codeforces 276C Little Girl and Maximum Sum(线段树的区间更新)
- Educational Codeforces Round 37 (Rated for Div. 2)-F-SUM and REPLACE(线段树)
- CF EDU 37 F SUM and REPLACE 【线段树区间更新中的单点更新】 好题!
- 【codeforces】678 F. Lena and Queries【线段树维护凸壳】
- Codeforces-877E:Danil and a Part-time Job(DFS序列+线段树)
- 线段树 CodeForces 220B - Little Elephant and Array
- code forces 276C Little Girl and Maximum Sum (线段树/技巧)
- codeforces 920F 线段树
- [线段树] Codeforces 794F Round #414 F. Leha and security system
- [线段树] Codeforces 811E Round #416 (Div. 2) E. Vladik and Entertaining Flags
- CodeForces 703D Mishka and Interesting sum
- codeforces 438D. The Child and Sequence(线段树)
- Codeforces 610D Vika and Segments 【线段树扫描线 求面积并】