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;
}