boom…
100→30
晚景凄凉
(1)sum
ksc
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
inline long long read(){
long long i=0,f=1;
char ch;
for(ch=getchar();!isdigit(ch);ch=getchar())
if(ch=='-') f=-1;
for(;isdigit(ch);ch=getchar())
i=(i<<3)+(i<<1)+(ch^48);
return i*f;
}
int buf[1024];
inline void write(long long x){
if(!x){putchar('0');return ;}
if(x<0){putchar('-');x=-x;}
while(x){buf[++buf[0]]=x%10,x/=10;}
while(buf[0]) putchar(buf[buf[0]--]+48);
return ;
}
#define stan 111111
int n;
long long a[stan],sum,tmp,mod,ans;
long long ksc(long long a,long long b){
long long ret=0;
while(b){
if(b&1) ret=(ret+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return ret;
}
signed main(){
n=read();mod=read();
for(int i=1;i<=n;++i)
a[i]=read();
sum=a[n];ans=a[n];
for(int i=n-1;i;--i){
sum=ksc((sum+1),a[i]);
ans=(ans+sum)%mod;
}
write(ans);
return 0;
}
(2)road
偶尔带个环的树形DP
本质是一道码农题
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define int long long
#define mod 1000000007
using namespace std;
inline int read(){
int i=0,f=1;
char ch;
for(ch=getchar();!isdigit(ch);ch=getchar())
if(ch=='-') f=-1;
for(;isdigit(ch);ch=getchar())
i=(i<<3)+(i<<1)+(ch^48);
return i*f;
}
int buf[1024];
inline void write(long long x){
if(!x){putchar('0');return ;}
if(x<0){putchar('-');x=-x;}
while(x){buf[++buf[0]]=x%10,x/=10;}
while(buf[0]) putchar(buf[buf[0]--]+48);
return ;
}
#define stan 555555
#define sten 333
int tot,nxt[stan],first[stan],goal[stan];
int vis[stan],stack[stan];
int ori,a,n,b,c,top;
long long to[stan],sze[stan],ans,total,dis[stan],f[sten][sten];
bool cir[stan];
void addedge(int a,int b,int c){
if(a!=b){
nxt[++tot]=first[a];
first[a]=tot;
goal[tot]=b;
dis[tot]=c;
}
return ;
}
void dfs(int u,int tager){
sze[u]=1;vis[u]=tager;
stack[++top]=u;
for(int p=first[u];p;p=nxt[p])
if(vis[goal[p]]){
if(cir[goal[p]]){
sze[u]+=sze[goal[p]];
to[u]=(to[u]+to[goal[p]]+(dis[p]*sze[goal[p]])%mod)%mod;
ans=(ans+(sze[goal[p]]*(1+dis[p]))%mod+to[goal[p]])%mod;
}else if(vis[goal[p]]==vis[u]){
ori=goal[p];
cir[u]=true;
total=dis[p];
return ;
}else{
to[u]=(to[u]+to[goal[p]]+(dis[p]*sze[goal[p]])%mod)%mod;
sze[u]+=sze[goal[p]];
ans=(ans+(sze[goal[p]]*(1+dis[p]))%mod+to[goal[p]])%mod;
}
}else{
dfs(goal[p],tager);
if(cir[goal[p]]&&u!=ori&&ori!=-1){
cir[u]=true;
total=(total+dis[p])%mod;
to[u]=(to[u]+to[goal[p]]+(dis[p]*sze[goal[p]])%mod)%mod;
sze[u]+=sze[goal[p]];
return ;
}else if(u==ori){
cir[u]=true;
total=(total+dis[p])%mod;
to[u]=(to[u]+to[goal[p]]+(dis[p]*sze[goal[p]])%mod)%mod;
sze[u]+=sze[goal[p]];
break;
}
to[u]=(to[u]+to[goal[p]]+(dis[p]*sze[goal[p]])%mod)%mod;
sze[u]+=sze[goal[p]];
ans=(ans+(sze[goal[p]]*(1+dis[p]))%mod+to[goal[p]])%mod;
}
if(cir[u]){
long long tmpsze=0;
int tagi=top;
while(cir[stack[tagi]]){
++tmpsze;
--tagi;
}
ans=(ans+((tmpsze*(tmpsze-1))%mod+(((tmpsze*(tmpsze-1)/2)%mod)*total)%mod))%mod;
for(int i=tagi+2;i<=top;++i)
sze[stack[i]]=top-tagi;
sze[u]=top-tagi;long long last=to[u];
for(int i=top;i>=tagi+2;--i){
to[stack[i]]=(last-total+(tmpsze*dis[first[stack[i]]])%mod)%mod;
last=to[stack[i]];
}
top=tagi;
ori=-1;
}
return ;
}
signed main(){
int _q=50<<20;
char *_p=(char*)malloc(_q)+_q;
__asm__("movl %0, %%esp\n"::"r"(_p));
ori=-1;
n=read();
for(int i=1;i<=n;++i){
b=read();c=read();
addedge(i,b,c);
}
for(int i=1;i<=n;++i)
if(!vis[i]) dfs(i,i);
ans=ans-(long long)(n-1)*n;
while(ans<0) ans+=mod;
write(ans);
return 0;
}
(3)shopping
线段树神操作
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define int long long
using namespace std;
inline int read(){
int i=0,f=1;
char ch;
for(ch=getchar();!isdigit(ch);ch=getchar())
if(ch=='-') f=-1;
for(;isdigit(ch);ch=getchar())
i=(i<<3)+(i<<1)+(ch^48);
return i*f;
}
int buf[1024];
inline void write(long long x){
if(!x){putchar('0');return ;}
if(x<0){putchar('-');x=-x;}
while(x){buf[++buf[0]]=x%10,x/=10;}
while(buf[0]) putchar(buf[buf[0]--]+48);
return ;
}
#define stan 200005
int n,m,len,w[stan],prc[stan],seq[stan],v[stan],pos[stan];
int mx[stan<<2];
struct option{
int typ,x,w,p;
}opt[stan];
bool cmp(int a,int b){return v[a]inline void update(int k,int l,int r,int p,int v){
if(l==r){mx[k]=v;return;}
int mid=(l+r)>>1;
if(mid>=p) update(k<<1,l,mid,p,v);
else update(k<<1|1,mid+1,r,p,v);
mx[k]=max(mx[k<<1],mx[k<<1|1]);
}
inline void modify(int x,int v,int p){
update(1,1,len,prc[x],0);
update(1,1,len,p,v);prc[x]=p;
}
inline int query(int k,int l,int r,int p){
if(l>p) return 0;
if(l==r) return mx[k];
int mid=(l+r)>>1;
if(preturn query(k<<1,l,mid,p);
return max(mx[k<<1],query(k<<1|1,mid+1,r,p));
}
inline int qprc(int k,int l,int r,int p,int now){
if(l==r) return v[l];
int mid=(l+r)>>1;
if(mid>p || mx[k<<1]==now) return qprc(k<<1,l,mid,p,now);
return qprc(k<<1|1,mid+1,r,p,now);
}
inline int find(int k){
int l=0,r=len+1,mid;
while(l1){
mid=(l+r)>>1;
if(v[mid]<=k) l=mid;
else r=mid;
}
return l;
}
signed main(){
n=read(),m=read();
for(int i=1;i<=n;++i){
w[i]=read();
prc[i]=read();
v[++len]=prc[i];
}
for(int i=1;i<=m;++i){
opt[i].typ=read();
if(opt[i].typ==2) opt[i].x=read();
else{
opt[i]=(option){1,read(),read(),read()};
v[++len]=opt[i].p;
}
}
for(int i=1;i<=len;++i)
seq[i]=i;
sort(seq+1,seq+1+len,cmp);
sort(v+1,v+1+len);
for(int i=1;i<=len;++i)
pos[seq[i]]=i;
for(int i=1;i<=n;++i){
update(1,1,len,pos[i],w[i]);
prc[i]=pos[i];
}
int np=n;
for(int i=1;i<=m;++i){
if(opt[i].typ==1){
np++;
modify(opt[i].x,opt[i].w,pos[np]);
}
else{
int res=opt[i].x;long long ans=0;
while(res){
int npos=find(res);
int rtn=query(1,1,len,npos);
if(!rtn) break;
int price = qprc(1,1,len,npos,rtn);
ans+=1ll*rtn*(res/price),res%=price;
}
write(ans);puts("");
}
}
return 0;
}