【Hackerrank World9】【JZOJ5020】Box Operations 题解
来源:互联网 发布:java redis实战 pdf 编辑:程序博客网 时间:2024/04/29 22:04
题目大意
题解
(想到这里之后我在考场上是这样做的:对于一个区间,如果他们除出来都是相同的,就打上除法标记,否则递归下去。然后发现除法标记瞬间爆 longlong……然后我就设个阈值,除法标记达到阈值之后强制下传,然后发现阈值居然要小于100……就狗带了)
相关题
代码
#include<cstdio>#include<algorithm>#define fo(i,a,b) for(int i=a;i<=b;i++)using namespace std;typedef long long LL;const int maxn=1e5+5;struct TR{ LL nmin,nmax,sum,len;};int n,a[maxn];int ReadInt(){ char ch=getchar(); int data=0, tag=1; while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); do{ if (ch=='-') tag=-1; else data=data*10+ch-'0'; ch=getchar(); } while (ch>='0' && ch<='9' || ch=='-'); return data*tag;}LL DIV(LL x,LL y) {return x/y-(x<0 && x%y!=0);}TR tr[4*maxn];LL bz[4*maxn];void update(int k,int ls,int rs){ if (bz[k]==0) return; tr[ls].nmin+=bz[k]; tr[ls].nmax+=bz[k]; tr[ls].sum+=bz[k]*tr[ls].len; bz[ls]+=bz[k]; tr[rs].nmin+=bz[k]; tr[rs].nmax+=bz[k]; tr[rs].sum+=bz[k]*tr[rs].len; bz[rs]+=bz[k]; bz[k]=0;}void merge(int k,int ls,int rs){ tr[k].nmin=(tr[ls].nmin<tr[rs].nmin) ?tr[ls].nmin :tr[rs].nmin; tr[k].nmax=(tr[ls].nmax>tr[rs].nmax) ?tr[ls].nmax :tr[rs].nmax; tr[k].sum=tr[ls].sum+tr[rs].sum;}void tr_js(int k,int l,int r){ tr[k].len=r-l+1; if (l==r) { tr[k].nmin=tr[k].nmax=tr[k].sum=a[l]; return; } int t=k<<1, t1=(l+r)>>1; tr_js(t,l,t1), tr_js(t+1,t1+1,r); merge(k,t,t+1);}void xg_ad(int k,int l,int r,int x,int y,LL z){ if (l==x && r==y) { tr[k].nmin+=z; tr[k].nmax+=z; tr[k].sum+=tr[k].len*z; bz[k]+=z; return; } int t=k<<1, t1=(l+r)>>1; update(k,t,t+1); if (y<=t1) xg_ad(t,l,t1,x,y,z); else if (x>t1) xg_ad(t+1,t1+1,r,x,y,z); else xg_ad(t,l,t1,x,t1,z), xg_ad(t+1,t1+1,r,t1+1,y,z); merge(k,t,t+1);}void re_dv(int k,int l,int r,LL z){ LL tm=tr[k].nmin-DIV(tr[k].nmin,z); if (tm==tr[k].nmax-DIV(tr[k].nmax,z)) { tr[k].nmin-=tm; tr[k].nmax-=tm; tr[k].sum-=tm*tr[k].len; bz[k]-=tm; return; } int t=k<<1, t1=(l+r)>>1; update(k,t,t+1); re_dv(t,l,t1,z), re_dv(t+1,t1+1,r,z); merge(k,t,t+1);}void xg_dv(int k,int l,int r,int x,int y,LL z){ if (l==x && r==y) { re_dv(k,l,r,z); return; } int t=k<<1, t1=(l+r)>>1; update(k,t,t+1); if (y<=t1) xg_dv(t,l,t1,x,y,z); else if (x>t1) xg_dv(t+1,t1+1,r,x,y,z); else xg_dv(t,l,t1,x,t1,z), xg_dv(t+1,t1+1,r,t1+1,y,z); merge(k,t,t+1);}LL cx_min(int k,int l,int r,int x,int y){ if (l==x && r==y) return tr[k].nmin; int t=k<<1, t1=(l+r)>>1; update(k,t,t+1); if (y<=t1) return cx_min(t,l,t1,x,y); else if (x>t1) return cx_min(t+1,t1+1,r,x,y); else { LL rel=cx_min(t,l,t1,x,t1), rer=cx_min(t+1,t1+1,r,t1+1,y); return (rel<rer) ?rel :rer; }}LL cx_sum(int k,int l,int r,int x,int y){ if (l==x && r==y) return tr[k].sum; int t=k<<1, t1=(l+r)>>1; update(k,t,t+1); if (y<=t1) return cx_sum(t,l,t1,x,y); else if (x>t1) return cx_sum(t+1,t1+1,r,x,y); else return cx_sum(t,l,t1,x,t1)+cx_sum(t+1,t1+1,r,t1+1,y);}int q;int main(){ scanf("%d %d",&n,&q); fo(i,1,n) a[i]=ReadInt(); tr_js(1,1,n); while (q--) { int ty=ReadInt(), l=ReadInt(), r=ReadInt(); if (ty==1) { LL d=ReadInt(); xg_ad(1,1,n,l,r,d); } else if (ty==2) { LL d=ReadInt(); xg_dv(1,1,n,l,r,d); } else if (ty==3) { printf("%lld\n",cx_min(1,1,n,l,r)); } else { printf("%lld\n",cx_sum(1,1,n,l,r)); } }}
- 【Hackerrank World9】【JZOJ5020】Box Operations 题解
- Hackerrank World CodeSprint 9 Box Operations
- [Hackerrank-algorithm-warmup]题解
- hackerrank Hourrank16题解
- HackerRank Twin Arrays 题解
- HackerRank Pattern Count 题解
- Hackerrank - Game Of Rotation 题解
- 【Hackerrank World11】Road Trip 题解
- Hackerrank - Coin on the Table 题解
- hackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- codechef Row and Column Operations 题解
- Codeforces 498C Array and Operations 题解
- windows第二次实验代码
- Tomcat显示更详细的log
- redis 配置详解
- Binary Tree Paths
- 计算机程序是如何运动的
- 【Hackerrank World9】【JZOJ5020】Box Operations 题解
- java设计模式学习-适配器模式
- 解决“UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 3: ordinal not in range(128”
- Eclipse上安装Genymotion插件
- iphone滚动不流畅
- Css transition属性(添加动态过渡样式)
- windows 下编译log4cxx(x64)
- 函数
- DaMa │ 128win Online Casino Unlimited Daily Instant Cash Back(128win, 128win rebate bonus, 128win Un