# Educational Codeforces Round 38

Educational Codeforces Round 38

C. Constructing Tests

$n = \frac{a+b}2,\lfloor \frac nm \rfloor = \frac {a-b}2$

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;

void solve(){
int n; sci(n);
if(n==0){
cout << 1 << ' ' << 1 << endl;
return;
}
for(int i = 1; i * i <= n; i++){
if(n%i!=0) continue;
if(i==n/i) continue;
int x = n / i, y = i;
if((x+y)&1) continue;
int a = (x + y) >> 1;
int b = (x - y) >> 1;
for(int l = 1, r; l <= a; l = r + 1){
r = a / (a / l);
if(a / l == b){
cout << a << ' ' << l << endl;
return;
}
}
}
cout << -1 << endl;
return;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
int tt; for(sci(tt); tt--; solve());
return 0;
}


view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pil pair<int,LL>
#define pli pair<LL,int>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;
int n, m;
LL w[MAXN];
vector<pil> G[MAXN];
void solve(){
sci(n); sci(m);
for(int i = 1; i <= m; i++){
int u, v; LL wt;
sci(u); sci(v); scl(wt);
wt <<= 1;
G[u] << pil(v,wt);
G[v] << pil(u,wt);
}
for(int i = 1; i <= n; i++) scl(w[i]);
priority_queue<pli,vector<pli>,greater<pli> > que;
for(int i = 1; i <= n; i++) que.push(pli(w[i],i));
while(!que.empty()){
auto p = que.top(); que.pop();
int u = p.second;
if(w[u]!=p.first) continue;
for(auto &e : G[u]){
int v = e.first;
LL ww = e.second;
if(w[v]>w[u]+ww){
w[v] = w[u] + ww;
que.push(pli(w[v],v));
}
}
}
for(int i = 1; i <= n; i++) cout << w[i] << ' '; cout << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}


E. Max History

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 1e6+7;
const int MOD = 1e9+7;
int fac[MAXN], inv[MAXN], rfac[MAXN], A[MAXN], cnt[MAXN];
int sum[MAXN];
int C(int n, int m){ return 1ll * fac[n] * rfac[m] % MOD * rfac[n-m] % MOD; }
void solve(){
int n;
sci(n);
for(int i = 1; i <= n; i++) sci(A[i]);
vi vec(n);
for(int i = 0; i < n; i++) vec[i] = A[i+1];
sort(all(vec)); vec.erase(unique(all(vec)),vec.end());
for(int i = 1; i <= n; i++) A[i] = lower_bound(all(vec),A[i]) - vec.begin();
for(int i = 1; i <= n; i++) cnt[A[i]]++;
for(int i = 1; i <= n; i++) cnt[i] += cnt[i-1];
int maxx = vec.size() - 1;
int ret = 0;
for(int i = 1; i <= n; i++){
if(A[i]==maxx) continue;
int s = cnt[A[i]-1];
ret = (ret + 1ll * vec[A[i]] * fac[n-s-1] % MOD * fac[s] % MOD * C(n,s)) % MOD;
}
cout << ret << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
fac[0] = rfac[0] = inv[1] = 1;
for(int i = 1; i < MAXN; i++) fac[i] = 1ll * fac[i-1] * i % MOD;
for(int i = 2; i < MAXN; i++) inv[i] = 1ll * (MOD - MOD / i) * inv[MOD % i] % MOD;
for(int i = 1; i < MAXN; i++) rfac[i] = 1ll * rfac[i-1] * inv[i] % MOD;
solve();
return 0;
}


F. Erasing Substrings

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 5e3+7;
char s[MAXN];
int n;
bool f[MAXN];
void solve(){
scs(s+1); n = strlen(s+1);
int k = (int)log2(n), M = 1 << k;
f[0] = 1;
for(int i = 1; i <= n - M + 1; i++){
for(int msk = 0; msk < M; msk++) if(f[msk]) for(int bit = 0; bit < k; bit++) f[msk|(1<<bit)] = true;
char best = 'z';
for(int msk = 0; msk < M; msk++) if(f[msk]) cmin(best,s[msk+i]);
for(int msk = 0; msk < M; msk++) f[msk] &= (s[msk+i]==best);
cout << best;
}
cout << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}


G. Shortest Path Queries

view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;
int n, m, q;
map<pii,pii> msk;
int fa[MAXN], dep[MAXN], dis[MAXN], base[31];
int ins(int x){
for(int i = 30; ~i; i--){
if(x>>i&1){
if(!base[i]){
base[i] = x;
return i;
}else x ^= base[i];
}
}
return -1;
}
inline int findx(int x){ while(x!=fa[x]) x = fa[x]; return x; }
inline int getdis(int x){ int d = 0; while(x!=fa[x]) d ^= dis[x], x = fa[x]; return d; }
struct SegmentTree{
int l[MAXN<<2], r[MAXN<<2];
pair<pii,int> edge[MAXN<<4];
#define ls(rt) rt << 1
#define rs(rt) rt << 1 | 1
void build(int L, int R, int rt = 1){
l[rt] = L; r[rt] = R;
if(L + 1 == R) return;
int mid = (L + R) >> 1;
build(L,mid,ls(rt)); build(mid,R,rs(rt));
}
void modify(int L, int R, pair<pii,int> e, int rt = 1){
if(L>=r[rt] or l[rt]>=R) return;
if(L<=l[rt] and r[rt]<=R){
edge[tot] = e;
return;
}
modify(L,R,e,ls(rt)); modify(L,R,e,rs(rt));
}
void dfs(int rt = 1){
stack<int> basechange;                              // 线性基改变
stack<pii> dsuchange;                               // 节点u, 父节点深度
int x = 0, y = 0;
for(int i = head[rt]; ~i; i = nxt[i]){
auto &e = edge[i];
int u = e.first.first, v = e.first.second, w = e.second;
if(w==-1) x = u, y = v;
else{
int fu = findx(u), fv = findx(v);
int du = getdis(u), dv = getdis(v);
if(fu==fv){
int bs = ins(w^du^dv);
if(bs!=-1) basechange.push(bs);
}else{
if(dep[fu]==dep[fv]){
dsuchange.push(pii(fu,dep[fv]));
fa[fu] = fv; dep[fv]++; dis[fu] = du^dv^w;
}else{
if(dep[fu]<dep[fv]) swap(fu,fv);
dsuchange.push(pii(fv,dep[fu]));
fa[fv] = fu; dis[fv] = du^dv^w;
}
}
}
}
if(l[rt]+1!=r[rt]) dfs(ls(rt)), dfs(rs(rt));
if(x!=0){
int d = getdis(x) ^ getdis(y);
for(int i = 30; ~i; i--) if(d>>i&1) d ^= base[i];
cout << d << endl;
}
while(!basechange.empty()) base[basechange.top()] = 0, basechange.pop();
while(!dsuchange.empty()){
int u = dsuchange.top().first;
dep[fa[u]] = dsuchange.top().second;
fa[u] = u;
dis[u] = 0;
dsuchange.pop();
}
}
}ST;
void solve(){
sci(n); sci(m);
for(int i = 1; i <= m; i++){
int u, v, w;
sci(u); sci(v); sci(w);
if(u>v) swap(u,v);
msk[pii(u,v)] = pii(0,w);
}
sci(q);
ST.build(0,q+1);
for(int i = 1; i <= q; i++){
int tp, u, v, w; sci(tp);
if(tp==1){
sci(u); sci(v); sci(w);
if(u>v) swap(u,v);
msk[pii(u,v)] = pii(i,w);
}else if(tp==2){
sci(u); sci(v);
if(u>v) swap(u,v);
ST.modify(msk[pii(u,v)].first,i,make_pair(pii(u,v),msk[pii(u,v)].second));
msk.erase(pii(u,v));
}else{
sci(u); sci(v);
ST.modify(i,i+1,make_pair(pii(u,v),-1));
}
}
for(auto p : msk) ST.modify(p.second.first,q+1,make_pair(p.first,p.second.second));
for(int i = 1; i <= n; i++) fa[i] = i;
ST.dfs();
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}

posted @ 2020-09-02 17:33  _kiko  阅读(108)  评论(0编辑  收藏  举报