codeforces contest 1140(D~G)
前言
A~C不想写博客了,就不写了,后面的题还是要推一推的,所以写一下
CF 1140 D
#include<bits/stdc++.h>
typedef long long ll;
#define rg register
template <typename T> inline void read(T&x){char cu=getchar();x=0;bool fla=0;while(!isdigit(cu)){if(cu=='-')fla=1;cu=getchar();}while(isdigit(cu))x=x*10+cu-'0',cu=getchar();if(fla)x=-x;}
template <typename T> inline void printe(const T x){if(x>=10)printe(x/10);putchar(x%10+'0');}
template <typename T> inline void print(const T x){if(x<0)putchar('-'),printe(-x);else printe(x);}
template <typename T> inline T min(const T a,const T b){return a<b?a:b;}
template <typename T> inline T max(const T a,const T b){return a>b?a:b;}
int n;
int main()
{read(n);print((n-1)*n*(n*2-1)/6+n*(n-1)/2-2);return 0;
}
CF 1140 E
#include<bits/stdc++.h>
typedef long long ll;
#define rg register
template <typename T> inline void read(T&x){char cu=getchar();x=0;bool fla=0;while(!isdigit(cu)){if(cu=='-')fla=1;cu=getchar();}while(isdigit(cu))x=x*10+cu-'0',cu=getchar();if(fla)x=-x;}
template <typename T> inline void printe(const T x){if(x>=10)printe(x/10);putchar(x%10+'0');}
template <typename T> inline void print(const T x){if(x<0)putchar('-'),printe(-x);else printe(x);}
template <typename T> inline T min(const T a,const T b){return a<b?a:b;}
template <typename T> inline T max(const T a,const T b){return a>b?a:b;}
const int mod=998244353;
int n,k,a[200001],b[200001],ta,tb;
ll A[200001],B[200001],C[200001];//A,A//A,B//A,non
ll ans=1;
int main()
{read(n),read(k);for(rg int i=1;i<=n;i++)if(i&1)read(a[++ta]);else read(b[++tb]);A[1]=k-1,B[1]=k-2,C[1]=k-1;A[0]=B[0]=C[0]=1;for(rg int i=2;i<=n;i++){A[i]=B[i-1]*(k-1)%mod;B[i]=(B[i-1]*(k-2)+A[i-1])%mod;C[i]=C[i-1]*(k-1)%mod;}for(rg int i=1;i<=ta;i++)if(a[i]==-1){int j=i;while(j<ta&&a[j+1]==-1)j++;if(i==1&&j==ta)ans=ans*C[ta-1]%mod*k%mod;else if(i==1||j==ta)ans=ans*C[j-i+1]%mod;else if(a[i-1]==a[j+1])ans=ans*A[j-i+1]%mod;else ans=ans*B[j-i+1]%mod;i=j+1;}else if(i!=ta&&a[i+1]!=-1&&a[i]==a[i+1])ans=0;for(rg int i=1;i<=tb;i++)if(b[i]==-1){int j=i;while(j<tb&&b[j+1]==-1)j++;if(i==1&&j==tb)ans=ans*C[tb-1]%mod*k%mod;else if(i==1||j==tb)ans=ans*C[j-i+1]%mod;else if(b[i-1]==b[j+1])ans=ans*A[j-i+1]%mod;else ans=ans*B[j-i+1]%mod;i=j+1;}else if(i!=tb&&b[i+1]!=-1&&b[i]==b[i+1])ans=0;print(ans);return 0;
}
CF 1140 F
#include<bits/stdc++.h>
typedef long long ll;
#define rg register
template <typename T> inline void read(T&x){char cu=getchar();x=0;bool fla=0;while(!isdigit(cu)){if(cu=='-')fla=1;cu=getchar();}while(isdigit(cu))x=x*10+cu-'0',cu=getchar();if(fla)x=-x;}
template <typename T> inline void printe(const T x){if(x>=10)printe(x/10);putchar(x%10+'0');}
template <typename T> inline void print(const T x){if(x<0)putchar('-'),printe(-x);else printe(x);}
template <typename T> inline T min(const T a,const T b){return a<b?a:b;}
template <typename T> inline T max(const T a,const T b){return a>b?a:b;}
const int MAX=2097152;
int q,l[MAX],r[MAX],mid[MAX];
std::vector<std::pair<int,int> >irt[MAX];
std::vector<std::pair<int,int> >::iterator pos;
void ini(const int root,const int ll,const int rr)
{l[root]=ll,r[root]=rr,mid[root]=(ll+rr)>>1;if(ll==rr)return;ini(root<<1,ll,mid[root]),ini(root<<1|1,mid[root]+1,rr);
}
void insert(const int root,const int wanl,const int wanr,std::pair<int,int>V)
{if(wanl==l[root]&&wanr==r[root]){irt[root].push_back(V);return;}if(wanr<=mid[root])insert(root<<1,wanl,wanr,V);else if(wanl>mid[root])insert(root<<1|1,wanl,wanr,V);else insert(root<<1,wanl,mid[root],V),insert(root<<1|1,mid[root]+1,wanr,V);
}
struct P
{int a,b,t;bool operator <(const P B)const{return a==B.a?b<B.b:a<B.a;}
};
std::set<P>Q;
std::set<P>::iterator Pos;
struct info
{int v,a,b;void clear(){v=a=b=0;}
};
struct node
{node*lson,*rson;info v;
}t[12600021],*root[600001],empty,*Null;
int usdto[600001],id;
node*newnode()
{node*R=&t[++usdto[id]];R->lson=R->rson=Null,R->v.clear();return R;
}
int pl;info val;
void INSERT(node*las,node*dq,const int root)
{if(l[root]==r[root]){dq->v=val;return;}if(pl<=mid[root])INSERT(las->lson,dq->lson=newnode(),root<<1),dq->rson=las->rson;else INSERT(las->rson,dq->rson=newnode(),root<<1|1),dq->lson=las->lson;
}
info RES;
void SEARCH(node*dq,const int root,const int pl)
{if(l[root]==r[root]){if(dq->v.v==0)RES=(info){pl,pl<=300000,pl>300000};else RES=dq->v;return;}if(pl<=mid[root])SEARCH(dq->lson,root<<1,pl);else SEARCH(dq->rson,root<<1|1,pl);
}
ll ans[600001];
info find(const int x)
{SEARCH(root[id],1,x);info u=RES;if(u.v==x)return u;return find(u.v);
}
void dodo(std::pair<int,int> V)
{// printf("dodo %d %d %d\n",ans[id],V.first,V.second);V.second+=300000;info u=find(V.first),v=find(V.second);if(u.v==v.v)id+=2,usdto[id]=usdto[id-2],root[id]=root[id-2],ans[id]=ans[id-2];else{if(u.a+u.b<v.a+v.b)std::swap(u,v);id++,usdto[id]=usdto[id-1];root[id]=newnode();pl=v.v,val=(info){u.v,0,0};INSERT(root[id-1],root[id],1);id++,usdto[id]=usdto[id-1];root[id]=newnode();pl=u.v,val=(info){u.v,u.a+v.a,u.b+v.b};INSERT(root[id-1],root[id],1);ans[id]=ans[id-2];// printf("(%lld %d %d %d %d)\n",ans[id],u.a,u.b,v.a,v.b);ans[id]+=(ll)(u.a+v.a)*(u.b+v.b)-(ll)u.a*u.b-(ll)v.a*v.b;}
}
void undo()
{// puts("undo");id-=2;
}
void dfs(const int root)
{if(l[root]>q)return;
// printf(">>%d %d\n",l[root],r[root]);for(pos=irt[root].begin();pos!=irt[root].end();pos++)dodo(*pos);if(l[root]==r[root])print(ans[id]),putchar(' ');else dfs(root<<1),dfs(root<<1|1);for(pos=irt[root].begin();pos!=irt[root].end();pos++)undo();
// printf("<<%d %d\n",l[root],r[root]);
}
int main()
{empty.lson=empty.rson=Null=∅root[0]=newnode();read(q);ini(1,1,600000);for(rg int i=1;i<=q;i++){P u;read(u.a),read(u.b);if((Pos=Q.find(u))==Q.end()){u.t=i;Q.insert(u);}else{insert(1,(*Pos).t,i-1,std::make_pair((*Pos).a,(*Pos).b));Q.erase(Pos);}}while(Q.size()){Pos=Q.begin();insert(1,(*Pos).t,q,std::make_pair((*Pos).a,(*Pos).b));Q.erase(Pos);}dfs(1);return 0;
}
xswl,我以为可持久化并查集是对的,其实是3个log的,T成大傻逼
上面贴出的是TLE代码
并查集是其实是可以O(1)\mathcal O(1)O(1)撤销的,只能重写
#include<bits/stdc++.h>
typedef long long ll;
#define rg register
template <typename T> inline void read(T&x){char cu=getchar();x=0;bool fla=0;while(!isdigit(cu)){if(cu=='-')fla=1;cu=getchar();}while(isdigit(cu))x=x*10+cu-'0',cu=getchar();if(fla)x=-x;}
template <typename T> inline void printe(const T x){if(x>=10)printe(x/10);putchar(x%10+'0');}
template <typename T> inline void print(const T x){if(x<0)putchar('-'),printe(-x);else printe(x);}
template <typename T> inline T min(const T a,const T b){return a<b?a:b;}
template <typename T> inline T max(const T a,const T b){return a>b?a:b;}
const int MAX=2097152;
int q,l[MAX],r[MAX],mid[MAX];
std::vector<std::pair<int,int> >irt[MAX];
std::vector<std::pair<int,int> >::iterator pos;
void ini(const int root,const int ll,const int rr)
{l[root]=ll,r[root]=rr,mid[root]=(ll+rr)>>1;if(ll==rr)return;ini(root<<1,ll,mid[root]),ini(root<<1|1,mid[root]+1,rr);
}
void insert(const int root,const int wanl,const int wanr,std::pair<int,int>V)
{if(wanl==l[root]&&wanr==r[root]){irt[root].push_back(V);return;}if(wanr<=mid[root])insert(root<<1,wanl,wanr,V);else if(wanl>mid[root])insert(root<<1|1,wanl,wanr,V);else insert(root<<1,wanl,mid[root],V),insert(root<<1|1,mid[root]+1,wanr,V);
}
struct P
{int a,b,t;bool operator <(const P B)const{return a==B.a?b<B.b:a<B.a;}
};
std::set<P>Q;
std::set<P>::iterator Pos;
ll ans;
int bcj[600001],ltot[600001],rtot[600001];
int find(const int x)
{if(x==bcj[x])return x;return find(bcj[x]);
}
int cha[600001],chb[600001],opt;
void dodo(std::pair<int,int> V)
{V.second+=300000;int u=find(V.first),v=find(V.second);opt++;if(u==v)cha[opt]=0;else{if(ltot[u]+rtot[u]<ltot[v]+rtot[v])std::swap(u,v);bcj[v]=u;ans+=(ll)(ltot[u]+ltot[v])*(rtot[u]+rtot[v])-(ll)ltot[u]*rtot[u]-(ll)ltot[v]*rtot[v];ltot[u]+=ltot[v];rtot[u]+=rtot[v];cha[opt]=u,chb[opt]=v;}
}
void undo()
{if(cha[opt]){int u=cha[opt],v=chb[opt];ltot[u]-=ltot[v];rtot[u]-=rtot[v];ans-=(ll)(ltot[u]+ltot[v])*(rtot[u]+rtot[v])-(ll)ltot[u]*rtot[u]-(ll)ltot[v]*rtot[v];bcj[v]=v;}opt--;
}
void dfs(const int root)
{for(pos=irt[root].begin();pos!=irt[root].end();pos++)dodo(*pos);if(l[root]==r[root])print(ans),putchar(' ');else dfs(root<<1),dfs(root<<1|1);for(pos=irt[root].begin();pos!=irt[root].end();pos++)undo();
}
int main()
{for(rg int i=1;i<=600000;i++){bcj[i]=i;if(i<=300000)ltot[i]++;else rtot[i]++;}read(q);ini(1,1,q);for(rg int i=1;i<=q;i++){P u;read(u.a),read(u.b);if((Pos=Q.find(u))==Q.end()){u.t=i;Q.insert(u);}else{insert(1,(*Pos).t,i-1,std::make_pair((*Pos).a,(*Pos).b));Q.erase(Pos);}}while(Q.size()){Pos=Q.begin();insert(1,(*Pos).t,q,std::make_pair((*Pos).a,(*Pos).b));Q.erase(Pos);}dfs(1);return 0;
}
CF 1140 G
#include<bits/stdc++.h>
typedef long long ll;
#define rg register
template <typename T> inline void read(T&x){char cu=getchar();x=0;bool fla=0;while(!isdigit(cu)){if(cu=='-')fla=1;cu=getchar();}while(isdigit(cu))x=x*10+cu-'0',cu=getchar();if(fla)x=-x;}
template <typename T> inline void printe(const T x){if(x>=10)printe(x/10);putchar(x%10+'0');}
template <typename T> inline void print(const T x){if(x<0)putchar('-'),printe(-x);else printe(x);}
template <typename T> inline T min(const T a,const T b){return a<b?a:b;}
template <typename T> inline T max(const T a,const T b){return a>b?a:b;}
template <typename T> inline void mind(T&a,const T b){a=a<b?a:b;}
const int maxn=300001,maxm=600001;
int n,q,dep[maxn];
int head[maxn],nxt[maxm],tow[maxm],tmp;ll vau1[maxm],vau2[maxm];
inline void addb(const int u,const int v,const ll w1,const ll w2)
{tmp++;nxt[tmp]=head[u];head[u]=tmp;tow[tmp]=v;vau1[tmp]=w1;vau2[tmp]=w2;
}
ll g[maxn];
void dfs1(const int u,const int fa)
{for(rg int i=head[u];i;i=nxt[i]){const int v=tow[i];if(v==fa)continue;dep[v]=dep[u]+1;dfs1(v,u);g[u]=min(g[u],vau1[i]+vau2[i]+g[v]);}
}
ll f[maxn][21][2][2];int p[maxn][21];
void dfs2(const int u,const int fa)
{for(rg int i=head[u];i;i=nxt[i]){const int v=tow[i];if(v==fa)continue;g[v]=min(g[v],vau1[i]+vau2[i]+g[u]);f[v][0][0][0]=vau1[i];f[v][0][0][1]=g[v]+vau2[i];mind(f[v][0][0][0],f[v][0][0][1]+g[u]);mind(f[v][0][0][1],f[v][0][0][0]+g[u]);f[v][0][1][0]=g[v]+vau1[i];f[v][0][1][1]=vau2[i];mind(f[v][0][1][0],f[v][0][1][1]+g[u]);mind(f[v][0][1][1],f[v][0][1][0]+g[u]);p[v][0]=u;dfs2(v,u);}
}
ll dpu[2],dpv[2],dp[2];
void C()
{std::swap(dpu[0],dpv[0]);std::swap(dpu[1],dpv[1]);
}
void CP(ll*D)
{dp[0]=D[0];dp[1]=D[1];
}
ll lca(int u,int v)
{if(u&1)u=(u+1)>>1,dpu[0]=0,dpu[1]=g[u];else u=(u+1)>>1,dpu[0]=g[u],dpu[1]=0;if(v&1)v=(v+1)>>1,dpv[0]=0,dpv[1]=g[v];else v=(v+1)>>1,dpv[0]=g[v],dpv[1]=0;if(dep[u]<dep[v])std::swap(u,v),C();const int DEP=dep[u]-dep[v];for(rg int i=0;i<=20;i++)if(DEP&(1<<i)){CP(dpu);dpu[0]=min(dp[0]+f[u][i][0][0],dp[1]+f[u][i][1][0]);dpu[1]=min(dp[0]+f[u][i][0][1],dp[1]+f[u][i][1][1]);u=p[u][i];mind(dpu[0],dpu[1]+g[u]);mind(dpu[1],dpu[0]+g[u]);}if(u==v)return min(dpu[0]+dpv[0],dpu[1]+dpv[1]);for(rg int i=20;i>=0;i--)if(p[u][i]!=p[v][i]){CP(dpu);dpu[0]=min(dp[0]+f[u][i][0][0],dp[1]+f[u][i][1][0]);dpu[1]=min(dp[0]+f[u][i][0][1],dp[1]+f[u][i][1][1]);u=p[u][i];mind(dpu[0],dpu[1]+g[u]);mind(dpu[1],dpu[0]+g[u]);CP(dpv);dpv[0]=min(dp[0]+f[v][i][0][0],dpv[1]+f[v][i][1][0]);dpv[1]=min(dp[0]+f[v][i][0][1],dpv[1]+f[v][i][1][1]);v=p[v][i];mind(dpv[0],dpv[1]+g[v]);mind(dpv[1],dpv[0]+g[v]);}CP(dpu);dpu[0]=min(dp[0]+f[u][0][0][0],dp[1]+f[u][0][1][0]);dpu[1]=min(dp[0]+f[u][0][0][1],dp[1]+f[u][0][1][1]);u=p[u][0];mind(dpu[0],dpu[1]+g[u]);mind(dpu[1],dpu[0]+g[u]);CP(dpv);dpv[0]=min(dp[0]+f[v][0][0][0],dpv[1]+f[v][0][1][0]);dpv[1]=min(dp[0]+f[v][0][0][1],dpv[1]+f[v][0][1][1]);v=p[v][0];mind(dpv[0],dpv[1]+g[v]);mind(dpv[1],dpv[0]+g[v]);return min(dpu[0]+dpv[0],dpu[1]+dpv[1]);
}
int main()
{read(n);for(rg int i=1;i<=n;i++)read(g[i]);for(rg int i=1;i<n;i++){int u,v;ll w1,w2;read(u),read(v),read(w1),read(w2);addb(u,v,w1,w2);addb(v,u,w1,w2);}dfs1(1,0);p[1][0]=1,dfs2(1,0);for(rg int j=1;j<=20;j++)for(rg int i=1;i<=n;i++){const int P=p[i][j-1];p[i][j]=p[P][j-1];const int T=p[i][j];f[i][j][0][0]=min(f[i][j-1][0][0]+f[P][j-1][0][0],f[i][j-1][0][1]+f[P][j-1][1][0]);f[i][j][0][1]=min(f[i][j-1][0][0]+f[P][j-1][0][1],f[i][j-1][0][1]+f[P][j-1][1][1]);mind(f[i][j][0][0],f[i][j][0][1]+g[T]);mind(f[i][j][0][1],f[i][j][0][0]+g[T]);f[i][j][1][0]=min(f[i][j-1][1][0]+f[P][j-1][0][0],f[i][j-1][1][1]+f[P][j-1][1][0]);f[i][j][1][1]=min(f[i][j-1][1][0]+f[P][j-1][0][1],f[i][j-1][1][1]+f[P][j-1][1][1]);mind(f[i][j][1][0],f[i][j][1][1]+g[T]);mind(f[i][j][1][1],f[i][j][1][0]+g[T]);}read(q);while(q--){int u,v;read(u),read(v);print(lca(u,v)),putchar('\n');}return 0;
}
codeforces contest 1140(D~G)相关推荐
- C++拾趣——STL容器的插入、删除、遍历和查找操作性能对比(ubuntu g++)——遍历和查找
相关环境和说明在<C++拾趣--STL容器的插入.删除.遍历和查找操作性能对比(ubuntu g++)--插入>已给出.本文将分析各个容器中遍历和查找的性能.(转载请指明出于breakso ...
- C++拾趣——STL容器的插入、删除、遍历和查找操作性能对比(ubuntu g++)——删除
相关环境和说明在<C++拾趣--STL容器的插入.删除.遍历和查找操作性能对比(ubuntu g++)--插入>已给出.本文将分析从头部.中间和尾部对各个容器进行删除的性能.(转载请指明出 ...
- Win10神州网信政府版(企业版G)安装
版权声明:本文为 月明星稀---原创文章,他的好同学遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_644926 ...
- 2021 ICPC Southeastern Europe Regional Contest Werewolves(树上背包)
2021 ICPC Southeastern Europe Regional Contest Werewolves(树上背包) 链接 题意:给出一个n个节点的树(n≤3000n\le3000n≤300 ...
- Codeforces Round #764 (Div. 3)(A~G)
考试结束rua!开始写题解. 这场打完后明显感觉到自己水平下降了很多,考试结束后该回回手了. 赛中过了A~D,F,E题题目读错了干想了一个多小时.....给整麻了 A. Plus One on the ...
- Codeforces Round #827 (Div. 4)(A~G)(F已更新)
"爷爷,你关注的up主要更新拉!" 今天起开始CF/At不定期更新 A. Sum(暴力) 只需判断a,b,c是否为和关系即可 #include <bits/stdc++.h& ...
- 2018 ACM-ICPC Asia Shenyang Regional Contest 题解(9 / 13)【每日亿题2021/2/24】
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A.(2018 ACM-ICPC Shenyang J)How Much Memory Your ...
- AtCoder Beginner Contest 198 (A ~ F)题解
目录 A. Div B. Palindrome with leading zeros C. Compass Walking D. Send More Money E. Unique Color F. ...
- 2016 ACM / ICPC Asia dalian Regional Contest 题解(11 / 11)【每日亿题2021 / 2 / 17】
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A .(2017 ACM ICPC dalian H)To begin or not to be ...
最新文章
- 怎么设置滚轮拖动_解决三斜线表头一拖动就变形的问题,再也不用一次一次的调整了...
- 自主学习 提问的智慧——学习中遇到难题怎么破?
- 中科院大学计算机研究生考试大纲,中国科学院大学《计算机原理》2019年硕士研究生入学考试大纲...
- trufflesuite/truffle-hdwallet-provider
- Flink-1.13集成hudi-0.10.0
- TensorFlow Lite 实现首次移植到 Arduino!
- bzoj 4017: 小Q的无敌异或
- 学科实践活动感悟50字_连江县高中综合实践活动学科马春晖名师工作室开展送教送培活动...
- **尾座体工艺工装设计尾座体工艺工装设计**
- Visio绘图记录(1)
- Itext学习(一)----官方网站学习
- 在Keil5中创建MM32工程
- 隔离电源模块的5个作用
- 菜鸟好文推荐(七)——他改了密码,姑娘说了“Yes, I do”
- 如何解决Css属性text-overflow:ellipsis 不起作用(文本溢出显示省略号)
- English - Grammar(未完)
- 谷歌浏览器无法翻译,最新谷歌翻译插件(chrome更强大的谷歌浏览器插件,腾讯翻译,一劳永逸)
- NoesisGUI入门及初步使用感想
- sql server数据库错误数据恢复(数据库连接失效,无法附加查询)
- ubuntu下安装librecad