codechef Chef and Churu
来源:互联网 发布:mac 批量 wav 转 mp3 编辑:程序博客网 时间:2024/05/29 19:37
传送门
将n个函数分块,预处理出每个位置在每个函数里用到多少次,修改时维护每个块内所有函数的和,询问时被完整覆盖的块直接累加和,被部分覆盖的块用树状数组暴力求和
要用unsigned long long
code:
#include<set>#include<map>#include<deque>#include<queue>#include<stack>#include<cmath>#include<ctime>#include<bitset>#include<string>#include<vector>#include<cstdio>#include<cstdlib>#include<cstring>#include<climits>#include<complex>#include<iostream>#include<algorithm>#define ll unsigned long longusing namespace std;const int maxn = 110000;const int maxnn = 350;int n,m,N;int lowbit(int x){return x&(-x);}void update(ll *s,int x,ll c){while(x<=n){s[x]+=c;x+=lowbit(x);}}ll query(ll *s,int x){ ll ret=0; while(x){ret+=s[x];x-=lowbit(x);} return ret;}int ti[maxn],tt[maxnn][maxn],L[maxn],R[maxn];int id[maxn],st[maxn];ll s[maxn],f[maxn],a[maxn];int main(){ scanf("%d",&n); N=sqrt(n); for(int i=1;i<=n;i++) { scanf("%llu",&a[i]); update(s,i,a[i]); } for(int i=1;i<=n;i++) { id[i]=(i-1)/N+1; scanf("%d%d",&L[i],&R[i]); } for(int i=1;i<=id[n];i++) st[i]=(i-1)*N+1; st[id[n]+1]=n+1; for(int i=1;i<=id[n];i++) { for(int j=st[i];j<st[i+1];j++) { f[i]+=query(s,R[j])-query(s,L[j]-1); ti[L[j]]++; ti[R[j]+1]--; } for(int j=1;j<=n;j++) tt[i][j]=tt[i][j-1]+ti[j]; for(int j=st[i];j<st[i+1];j++) { ti[L[j]]--; ti[R[j]+1]++; } } scanf("%d",&m); while(m--) { ll x,y,c; scanf("%llu%llu%llu",&c,&x,&y); if(c==1) { for(int i=1;i<=id[n];i++) f[i]+=tt[i][x]*(y-a[x]); update(s,x,y-a[x]); a[x]=y; } else { int t1=id[x],t2=id[y]; ll ret=0; if(t1==t2) { for(int i=x;i<=y;i++) ret+=query(s,R[i])-query(s,L[i]-1); } else { for(int i=x;i<st[t1+1];i++) ret+=query(s,R[i])-query(s,L[i]-1); for(int i=t1+1;i<t2;i++) ret+=f[i]; for(int i=st[t2];i<=y;i++) ret+=query(s,R[i])-query(s,L[i]-1); /*for(int i=x;i<=y;i++) ret+=query(s,R[i])-query(s,L[i]-1);*/ } printf("%llu\n",ret); } } return 0;}
0 0
- Codechef Chef and Churu
- codechef Chef and Churu
- CodeChef Chef and Churu Problem
- 【CodeChef】Chef and Churu 分块+树状数组
- codechef November Challenge 2014 .Chef and Churu
- 【codechef FNCS】【Chef and Churu】【分块】
- [CC]Chef and Churu
- CodeChef Chef and Segments
- codechef Chef and sequence
- codechef Chef and Swaps
- Codechef Chef and Reversing
- Codechef Chef and Frogs
- 【CodeChef】 Chef and Stones
- 【CodeChef】Chef and Interview
- codechef Chef and easy problem
- codechef Chef and Left-Right
- codechef-Chef and Prime Divisors
- CodeChef Chef and Strange Operations
- 公司有这些现象,立马踹了它!
- Android Context完全解析,你所不知道的Context的各种细节
- 牛客网刷题之平衡二叉树
- 数据结构-链式存储-基本运算
- 判断客户端是否是移动设备
- codechef Chef and Churu
- 100天土鸡饲养计划(76)
- 打开新窗口的几种方式及target=_blank指定窗口名称
- <转>Spark Streaming编程指南
- 如何修改PDF黑色背景色和文字颜色方便打印
- 【POJ 1717】Dominoes(dp)
- git命令记录
- SQL分页
- 逻辑地址&物理地址