首页 > 其他分享 >Codeforces Round #395 (Div. 1) 题解

Codeforces Round #395 (Div. 1) 题解

时间:2022-10-24 19:34:20浏览次数:49  
标签:ch return int 题解 ll Codeforces long Div define


A Timofey and a tree

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
int a[MAXN],c[MAXN];
int u[MAXN],v[MAXN];
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);

int n=read();
MEM(a)
For(i,n-1) cin>>u[i]>>v[i];
For(i,n) cin>>c[i];
int p=0;
For(i,n-1) if (c[u[i]]!=c[v[i]]) a[u[i]]++,a[v[i]]++,++p;

For(i,n) if (a[i]==p) {
printf("YES\n%d\n",i); return 0;
}
puts("NO");

return 0;
}

B. Timofey and rectangles

边长均为奇数,所以……

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int main()
{
// freopen("B.in","r",stdin);
// freopen(".out","w",stdout);
int n=read();
puts("YES");
For(i,n) {
int x1=read(),y1=read();
read(),read();
cout<<1+(x1%2+2)%2*2+(y1%2+2)%2<<endl;
}


return 0;
}

C - Timofey and remoduling

根据等差数列平方和公式
显然我们可以枚举a1在数列中的下一项,这样能得到n个可能的d
我们代入公式验证:
∑ni−1(ai)2=n∗a21+n(n−1)(2n−1)d2/6+n(n−1)∗d∗a1
然后暴力确认

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (m)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll n,m;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll pow2(ll a,ll b){
if (!b) return 1;
ll t=pow2(a,b/2);
t=mul(t,t);
if (b&1) t=mul(t,a);
return t;
}
#define MAXN (123456)
ll inv(ll a){return pow2(a,m-2);}
ll a[MAXN],b[MAXN];
int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout);
m=read(),n=read();
ll s0=0,s1=0;
For(i,n) {
a[i]=read();
upd(s0,a[i]);
upd(s1,a[i]*a[i]);
}
sort(a+1,a+1+n);
if (n==1) {
cout<<a[1]<<' '<<0<<endl;
return 0;
}
Fork(i,2,n) {
ll d=sub(a[i],a[1]);
ll a1=(s0-n*(n-1)/2*d%m+m)%m *inv(n) %m;
ll S=mul(mul(n,a1),a1);
upd(S,n*(n-1)/2*(2*n-1)/3%m*d%m*d%m);
upd(S,n*(n-1)%m*d%m*a1%m);
if (S!=s1) continue;

b[1]=a1;
Fork(i,2,n) b[i]=add(b[i-1],d);
sort(b+1,b+1+n);
bool fl=0;
For(i,n) if (a[i]!=b[i]) fl=1;
if (!fl) {
cout<<a1<<' '<<d<<endl; return 0;
}
}
puts("-1");
return 0;
}

E. Timofey and our friends animals

原题……

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
typedef pair<int,int> pii;
const int N=1e5+5,M=5234567,Q=1e5+5,NODE=N+M,INF=1e9;
int n,m,bit[N],ans[Q],val[NODE];
pii edge[M];
struct Query{
int L,R,id;
inline bool operator <(const Query &tmp)const{
return R<tmp.R;
}
}query[Q];
struct LCT{
int par[NODE],ch[NODE][2],mi[NODE],stk[NODE];
bool flip[NODE];
void clear(){
memset(ch,0,sizeof(ch));
memset(par,0,n+m+1<<2);
memset(flip,false,n+m+1);
for(int i=1;i<=n+m;++i)
mi[i]=i;
}
void push_up(int k){
mi[k]=k;
for(int i=0;i<2;++i)
if(ch[k][i]&&val[mi[ch[k][i]]]<val[mi[k]])mi[k]=mi[ch[k][i]];
}
void push_down(int k){
if(!flip[k])return;
swap(ch[k][0],ch[k][1]);
flip[ch[k][0]]^=1;
flip[ch[k][1]]^=1;
flip[k]=false;
}
inline bool is_root(int x){
return ch[par[x]][0]!=x&&ch[par[x]][1]!=x;
}
void rotate(int x,bool f){
int y=par[x];
par[ch[x][f]]=y;
ch[y][!f]=ch[x][f];
par[x]=par[y];
if(!is_root(y))ch[par[y]][ch[par[y]][1]==y]=x;
ch[x][f]=y;
par[y]=x;
push_up(y);
push_up(x);
}
void update(int x){
int top=0;
stk[top++]=x;
for(;!is_root(x);x=par[x])
stk[top++]=par[x];
while(top)push_down(stk[--top]);
}
void Splay(int x){
update(x);
while(!is_root(x)){
int y=par[x];
if(is_root(y))rotate(x,ch[y][0]==x);
else{
bool flag=ch[par[y]][0]==y;
if(ch[y][flag]==x)rotate(x,!flag);
else rotate(y,flag);
rotate(x,flag);
}
}
}
void Access(int x){
for(int y=0;x;y=x,x=par[x]){
Splay(x);
ch[x][1]=y;
push_up(x);
}
}
int find_root(int x){
Access(x);
Splay(x);
for(;;x=ch[x][0]){
if(!ch[x][0]){
Splay(x);
return x;
}
}
}
void make_root(int x){
Access(x);
Splay(x);
flip[x]^=1;
}
void link(int u,int v){
make_root(u);
par[u]=v;
Access(u);
}
void cut(int u,int v){
make_root(u);
Access(v);
Splay(v);
assert(ch[v][0]==u&&par[u]==v);
ch[v][0]=par[u]=0;
push_up(v);
}
int query_mi(int u,int v){
make_root(u);
Access(v);
Splay(v);
return mi[v];
}
}lct;
void rd(int &res){
res=0;
char c;
while(c=getchar(),c<48);
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>47);
}
void add(int x,int v){
while(x<=n){
bit[x]+=v;
x+=lowbit(x);
}
}
int sum(int x){
int res=0;
while(x){
res+=bit[x];
x-=lowbit(x);
}
return res;
}
void print(int x){
if(!x)return;
print(x/10);
putchar(x%10^48);
}
void solve(){
int q,KK;
rd(KK);
rd(m);
for(int i=1;i<=n;++i){
val[i]=INF;
bit[i]=0;
}
int a,b;
for(int i=0;i<m;++i){
rd(a);rd(b);
if(a>b)swap(a,b);
edge[i]=pii(b,a);
}
rd(q);
sort(edge,edge+m);
for(int i=0;i<q;++i){
rd(query[i].L);rd(query[i].R);
query[i].id=i;
}
sort(query,query+q);
lct.clear();
for(int i=0,j=0,tot=0;i<q;++i){
while(j<m&&edge[j].fi<=query[i].R){
int u=edge[j].fi,v=edge[j].se,id=++j+n;
val[id]=v;
if(lct.find_root(u)!=lct.find_root(v)){
lct.link(u,id);
lct.link(v,id);
add(v,1);
++tot;
}
else{
int mi=lct.query_mi(u,v);
if(val[mi]<val[id]){
lct.cut(mi,edge[mi-n-1].fi);
lct.cut(mi,edge[mi-n-1].se);
add(edge[mi-n-1].se,-1);
lct.link(u,id);
lct.link(v,id);
add(v,1);
}
}
}
ans[query[i].id]=n-(tot-sum(query[i].L-1));
ans[query[i].id]-=query[i].L-1;
ans[query[i].id]-=n-query[i].R;

}
for(int i=0;i<q;++i,putchar('\n'))
print(ans[i]);
}
int main(){
// freopen("e.in","r",stdin);
n=read();
solve();
return 0;
}


标签:ch,return,int,题解,ll,Codeforces,long,Div,define
From: https://blog.51cto.com/u_15724837/5791036

相关文章

  • Spring常见问题解决办法汇总
     解决Theprefix'context'forelement'context:component-scan'isnotbound<beansxmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://w......
  • BSOJ7075题解
    感觉这一类DP至少不应该被叫做“LCS模型”,本质应该是其他的东西......先来考虑经典的LCS:\(dp[n][m]\)表示\(S[n]\)和\(T[m]\)匹配上的最长的长度。那么我们不妨......
  • GCJ 2017 R2 题解(待续)
    ProblemA.FreshChocolateProblemYouarethepublicrelationsmanagerforachocolatemanufacturer.Unfortunately,thecompany’simagehassufferedbecausecus......
  • 北方大学 ACM 多校训练赛 第四场 题解
    A.恶魔包毁灭世界已知一张二分图,问哪些边是二分图的可行边?先跑最小流,再把残余网络建图,几个重要结论是:·最小割的可行边(满流&&2点不在一个SCC中)·最小割的必行边(可行......
  • GCJ Qualification Round 2017 题解(部分)
    OversizedPancakeFlipper#include<bits/stdc++.h>usingnamespacestd;#define#define#define#define#define#define#define#define#define#define#define#define#define#......
  • 长安大学第二届ACM程序设计竞赛校赛 题解
    ACountCircles描述StupidAguinfeelsconfusedwhilereading.Thebookshowsfollowingequations:6=9,8=1010,144=75,690=801StupidAguindoesn’tknow......
  • GCJ Round 1A 2017 题解
    AAlphabetCake给一个R*C矩阵,里面有大写字母和?(大写字母每个最多出现一次),用矩阵中出现的大写字母填满矩阵,要求每个字母出现的区域都恰为一子矩阵。直接把每个字母向行延展......
  • ASC 04 题解
    A求最小割方案是否唯一#include<iostream>#include<cmath>#include<algorithm>#include<cstdio>#include<cstring>#include<string>#include<vector>#include<map>#......
  • Codeforces Round #829 (Div. 2)/CodeForces1754
    CodeForces1754注:所有代码均为场上所书TechnicalSupport解析:题目大意给定一个只包含大写字母\(\texttt{Q}\)和\(\texttt{A}\)的字符串,如果字符串里的每一个\(\t......
  • hihoCoder挑战赛31 题解
    题目1:Numbers时间限制:8000ms单点时限:1000ms内存限制:256MB描述给定n个整数常数c[1],c[2],…,c[n]和一个整数k。现在需要给2k个整数变量x[1],x[2],…,x[k......