Codeforces Global Round 3 题解

Posted gczdajuruo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Global Round 3 题解相关的知识,希望对你有一定的参考价值。

A

送分题

B

送分题

C

考虑先依次把\(2\sim n-1\)换到正确的位置,这个只要利用\(1,n\)两个位置交换就行了。

然后如果\(n\)在第一位就交换\(1,n\)

D

显然\(a<b\)\(a>b\)的可以分别考虑。不失一般性考虑\(a<b\)的。那么相当于要选出若干二元组并排序,要求满足\(b_i>a_i+1\)。考虑对数值建点,\(a\)\(b\)连边,所有\(i\)\(i-1\)连边,相当于要走一条路径经过最多\(a->b\)的边。这样可以直观地发现只要按\(b\)从大到小排序就可以经过所有边。

E

如果合法,一定存在一种方案使得初始位置和最终位置按顺序对应。设初始位置\(x\),最终位置\(y\),把\(x_i<y_i\)的看成\(y_i-x_i\)个左括号,把\(x_i>y_i\)的看成\(x_i-y_i\)个右括号,那么相当于要找一组括号匹配。从前往后扫,维护左括号的栈,碰到右括号时弹栈即可。

#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
typedef pair<int, int> pii;
#define mp make_pair
#define fi first
#define se second
#define pb push_back

int gi() 
  int x = 0, o = 1;
  char ch = getchar();
  while((ch < '0' || ch > '9') && ch != '-') 
    ch = getchar();
  
  if(ch == '-') 
    o = -1, ch = getchar();
  
  while(ch >= '0' && ch <= '9') 
    x = x * 10 + ch - '0', ch = getchar();
  
  return x * o;


struct dat 
  int x, y, d;
;
vector<dat> ans;

int n, t[N], tp = 0;
pii s[N], st[N];

void No() 
  puts("NO");
  exit(0);


int main() 
  cin >> n;
  for(int i = 1; i <= n; i++) 
    s[i] = mp(gi(), i);
  
  for(int i = 1; i <= n; i++) 
    t[i] = gi();
  
  sort(s + 1, s + n + 1);
  sort(t + 1, t + n + 1);
  for(int i = 1; i <= n; i++) 
    int d = t[i] - s[i].fi;
    if(d > 0) 
      st[++tp] = mp(d, s[i].se);
    
    while(d < 0) 
      if(!tp) 
        No();
      
      int t = min(-d, st[tp].fi);
      d += t, st[tp].fi -= t, ans.pb((dat) 
        st[tp].se, s[i].se, t
      );
      if(st[tp].fi == 0) 
        --tp;
      
    
  
  if(tp) 
    No();
  
  puts("YES");
  cout << ans.size() << endl;
  for(auto x : ans) 
    cout << x.x << ' ' << x.y << ' ' << x.d << endl;
  
  return 0;

F

先把\(\sum val\)变成正的。

\(ans_i\)表示\(s=i\)\(val\)的总和,可以发现\(\sum_i=0^2^62-1 ans_i=0\)。考虑一段区间\([l,r]\),如果\(\sum_i=l^rans_i<0\),那么把它划分成\([l,mid],[mid+1,r]\)两个区间,一定有一个的\(ans\)之和\(<0\),递归下去找就行了。一个细节是,如果\(\sum ans=0\)且划分成两个区间后\(\sum ans\)都为\(0\),那么往\([l,mid]\)递归,因为\(ans_0>0\),所以左半区间一定有\(<0\)的和\(ans_0\)抵消。

记得用\(\text__builtin_parityll\)而不是\(\text__builtin_parity\)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;

ll gi() 
  ll x = 0, o = 1;
  char ch = getchar();
  while((ch < '0' || ch > '9') && ch != '-') 
    ch = getchar();
  
  if(ch == '-') 
    o = -1, ch = getchar();
  
  while(ch >= '0' && ch <= '9') 
    x = x * 10 + ch - '0', ch = getchar();
  
  return x * o;


int n, lb[N], val[N];
ll mask[N], sum = 0;

ll calc(ll ans, int p) 
  ll ret = 0;
  for(int i = 1; i <= n; i++) if(lb[i] >= p) 
      ret += val[i] * (__builtin_parityll(ans & mask[i]) ? -1 : 1);
    
  return ret;


int main() 
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  cin >> n;
  for(int i = 1; i <= n; i++) 
    val[i] = gi(), mask[i] = gi(), lb[i] = __builtin_ctzll(mask[i]), sum += val[i];
  
  if(sum < 0) for(int i = 1; i <= n; i++) 
      val[i] *= -1;
    
  ll ans = 0;
  for(int i = 61; ~i; i--) if(calc(ans, i) > calc(ans | (1ll << i), i)) 
      ans |= 1ll << i;
    
  cout << ans;
  return 0;

G

先扫一遍,贪心地求出补图中的一个独立集\(A\)。一个集合中与一个数互质的数的个数可以\(O(2^8)\)容斥求。

如果\(|A|\ge k\),那么这是原图中一个大小为\(k\)的团,直接输出即可。

否则因为\(A\)是贪心求的,所以补图中连通块个数\(<k\),可以推出补图中 在至少有\(2\)个点的连通块中的点 个数\(\ge k\)

。任取\(k\)个这样的点,只要满足每个被取点的连通块中至少取出\(2\)个点即可。具体的连通块划分可以用整体二分,对于每个不在\(A\)中的点找到一个在\(A\)中的点满足补图中有边。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 1e7 + 10;
#define sz(x) int(x.size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<int> vi;
typedef pair<int, int> pii;

int gi() 
  int x = 0, o = 1;
  char ch = getchar();
  while((ch < '0' || ch > '9') && ch != '-') 
    ch = getchar();
  
  if(ch == '-') 
    o = -1, ch = getchar();
  
  while(ch >= '0' && ch <= '9') 
    x = x * 10 + ch - '0', ch = getchar();
  
  return x * o;


int n, k, cnt[M], pri[M], tt = 0, vis[M], minp[M], mu[M];
vi d[N], E[N], A, B;

void add(int i, int x, int v) 
  if(x == sz(d[i])) 
    cnt[v] += mu[v];
    return;
  
  add(i, x + 1, v), add(i, x + 1, v * d[i][x]);


void clr(int i, int x, int v) 
  if(x == sz(d[i])) 
    cnt[v] = 0;
    return;
  
  clr(i, x + 1, v), clr(i, x + 1, v * d[i][x]);


int qry(int i, int x, int v) 
  if(x == sz(d[i])) 
    return cnt[v];
  
  return qry(i, x + 1, v) + qry(i, x + 1, v * d[i][x]);


void solve(int l, int r, vi p) 
  if(l == r) 
    E[l] = p;
    return;
  
  int mid = (l + r) >> 1;
  for(int i = l; i <= mid; i++) 
    add(A[i], 0, 1);
  
  vi ls, rs;
  for(auto i : p) 
    if(qry(i, 0, 1)) 
      ls.pb(i);
     else 
      rs.pb(i);
    
  
  for(int i = l; i <= mid; i++) 
    clr(A[i], 0, 1);
  
  solve(l, mid, ls), solve(mid + 1, r, rs);


int main() 
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  cin >> n >> k;
  vis[1] = mu[1] = 1;
  for(int i = 2; i < M; i++) 
    if(!vis[i]) 
      pri[++tt] = i, mu[i] = -1, minp[i] = i;
    
    for(int j = 1; j <= tt && i * pri[j] < M; j++) 
      vis[i * pri[j]] = 1;
      minp[i * pri[j]] = pri[j];
      if(i % pri[j] == 0) 
        break;
       else 
        mu[i * pri[j]] = -mu[i];
      
    
  
  for(int i = 1; i <= n; i++) 
    int x = gi();
    while(x > 1) 
      int y = minp[x];
      d[i].pb(y);
      while(x % y == 0) 
        x /= y;
      
    
  
  for(int i = 1; i <= n; i++)
    if(!qry(i, 0, 1)) 
      add(i, 0, 1), A.pb(i);
     else 
      B.pb(i);
    
  if(sz(A) >= k) 
    for(int i = 0; i < k; i++) 
      cout << A[i] << ' ';
    
    return 0;
  
  for(auto i : A) 
    clr(i, 0, 1);
  
  solve(0, sz(A) - 1, B);
  vector<pii> v;
  for(int i = 0; i < sz(A); i++) 
    v.pb(mp(sz(E[i]), i));
  
  sort(v.begin(), v.end(), greater<pii>());
  int sum = 0;
  for(int i = 0; i < sz(v); i++) 
    sum += v[i].fi + 1;
    if(sum >= k) 
      vi ans;
      int req = k - (i + 1) * 2;
      for(int j = 0; j <= i; j++) 
        int tk = min(v[j].fi - 1, req);
        req -= tk;
        ++tk;
        ans.pb(A[v[j].se]);
        for(int k = 0; k < tk; k++) 
          ans.pb(E[v[j].se][k]);
        
      
      for(auto i : ans) 
        cout << i << ' ';
      
      return 0;
    
  
  return 0;

H

mex的取值只有\(O(n)\)个。从小到大枚举\(r\),对于一个\(r\),显然\(l\)递减时mex不降,所以可以把\(l\)分为若干段,每一段中mex相同。用数据结构维护这些段,考虑\(r\)增加1时对段的影响。

1.不存在mex为\(a_r+1\)的段。这种情况下没有影响。

2.存在mex为\(a_r+1\)的段。这种情况下这一个段又会被分为若干更小的段,且分割后仍然满足不降的性质。

可以考虑用set维护这些段,用线段树维护每一个值上一次出现的位置,第2种情况中更改段时在线段树上二分找出所有需要加入的新段。显然扫描完整个序列后只会有\(O(n)\)次加入段的操作。

再对每一个值用一个支持区间加和区间历史和的可持久化线段树维护答案即可。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair
#define fi first
#define se second
#define pb push_back
typedef vector<int> vi;
#define sz(a) int(a.size())
const int inf = 1e9;

int gi() 
  int x = 0, o = 1;
  char ch = getchar();
  while((ch < '0' || ch > '9') && ch != '-') 
    ch = getchar();
  
  if(ch == '-') 
    o = -1, ch = getchar();
  
  while(ch >= '0' && ch <= '9') 
    x = x * 10 + ch - '0', ch = getchar();
  
  return x * o;


int n;
ll ans = 0;
set<pii> s;

#define ls (x<<1)
#define rs (x<<1|1)

int mn[N << 2];

void mdf(int p, int v, int x = 1, int l = 0, int r = n) 
  if(l == r) 
    mn[x] = v;
    return;
  
  int mid = (l + r) >> 1;
  p <= mid ? mdf(p, v, ls, l, mid) : mdf(p, v, rs, mid + 1, r);
  mn[x] = min(mn[ls], mn[rs]);


int bin(int p, int v, int x = 1, int l = 0, int r = n) 
  if(l == r) 
    return mn[x] < p && l > v ? l : inf;
  
  int mid = (l + r) >> 1;
  if(mn[ls] < p && mid > v) 
    int w = bin(p, v, ls, l, mid);
    if(w < inf) 
      return w;
    
  
  return bin(p, v, rs, mid + 1, r);


int lst(int p, int x = 1, int l = 0, int r = n) 
  if(l == r) 
    return mn[x];
  
  int mid = (l + r) >> 1;
  return p <= mid ? lst(p, ls, l, mid) : lst(p, rs, mid + 1, r);


#undef ls
#undef rs

namespace ps_tree 

vector<pii> rt[N];

struct mat 
  ll v[2][2];
  mat() 
    memset(v, 0, sizeof(v));
  
  void init() 
    memset(v, 0, sizeof(v));
    v[0][0] = v[1][1] = 1;
  
  mat operator*(const mat &A) const 
    mat ret;
    for(int i = 0; i < 2; i++)
      for(int j = 0; j < 2; j++)
        for(int k = 0; k < 2; k++) 
          ret.v[i][j] += v[i][k] * A.v[k][j];
        
    return ret;
  
  mat operator+(const mat &A) const 
    mat ret;
    for(int i = 0; i < 2; i++)
      for(int j = 0; j < 2; j++) 
        ret.v[i][j] = v[i][j] + A.v[i][j];
      
    return ret;
  
  bool i() 
    for(int i = 0; i < 2; i++)
      for(int j = 0; j < 2; j++) if(v[i][j] != (i == j)) 
          return 0;
        
    return 1;
  
;

struct node 
  int ls, rs, sum = 0, tag = 0;
  mat summ, tagm;
  node() 
    tagm.init();
  
  void mdf(const mat &v, int add, int len) 
    summ = summ * v, tagm = tagm * v;
    sum += len * add, tag += add;
  
  ll cal(int t) 
    return 1ll * sum * t + summ.v[0][0];
  
 tr[N * 60];

int tt = 0;

void New(int &x, int len) 
  tr[++tt] = tr[x], x = tt;
  tr[x].summ.v[0][1] = len;


void down(int x, int l, int r) 
  int mid = (l + r) >> 1;
  if(tr[x].tagm.i()) 
    return;
  
  New(tr[x].ls, mid - l + 1), tr[tr[x].ls].mdf(tr[x].tagm, tr[x].tag, mid - l + 1);
  New(tr[x].rs, r - mid), tr[tr[x].rs].mdf(tr[x].tagm, tr[x].tag, r - mid);
  tr[x].tagm.init();
  tr[x].tag = 0;


void mdf(int &x, int L, int R, int v, int t, int l = 1, int r = n) 
  New(x, r - l + 1);
  if(L <= l && r <= R) 
    mat tmp;
    tmp.init();
    tmp.v[1][0] = -1ll * v * (t - 1);
    tr[x].mdf(tmp, v, r - l + 1);
    return;
  
  down(x, l, r);
  int mid = (l + r) >> 1;
  if(L <= mid) 
    mdf(tr[x].ls, L, R, v, t, l, mid);
  
  if(R > mid) 
    mdf(tr[x].rs, L, R, v, t, mid + 1, r);
  
  tr[x].summ = tr[tr[x].ls].summ + tr[tr[x].rs].summ;
  tr[x].sum = tr[tr[x].ls].sum + tr[tr[x].rs].sum;


ll qry(int x, int L, int R, int t, int l = 1, int r = n) 
  if(!x) 
    return 0;
  
  if(L <= l && r <= R) 
    return tr[x].cal(t);
  
  down(x, l, r);
  int mid = (l + r) >> 1;
  ll ret = 0;
  if(L <= mid) 
    ret += qry(tr[x].ls, L, R, t, l, mid);
  
  if(R > mid) 
    ret += qry(tr[x].rs, L, R, t, mid + 1, r);
  
  return ret;


void _mdf(int x, int l, int r, int v, int t) 
  int y = 0;
  if(!rt[x].empty()) 
    y = rt[x].back().se;
  
  rt[x].pb(mp(t, y));
  mdf(rt[x].back().se, l, r, v, t);


ll _qry(int x, int l, int r, int t) 
  auto y = upper_bound(rt[x].begin(), rt[x].end(), mp(t, inf));
  if(y == rt[x].begin()) 
    return 0;
   else 
    --y;
    return qry(y->se, l, r, t);
  




void add_seg(int x, int l, int r, int v, int t) 
  if(v == 1) 
    while(1) 
      auto y = s.lower_bound(mp(x, 0));
      if(y == s.end() || y->fi != x) 
        break;
      
      s.erase(y);
    
    s.insert(mp(x, r));
   else if(s.find(mp(x, r)) != s.end()) 
    s.erase(mp(x, r));
  
  ps_tree::_mdf(x, l, r, v, t);


ll qry(int x, int l, int r) 
  return ps_tree::_qry(x, l, r, r);


int main() 
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  cin >> n;
  for(int i = 1; i <= n; i++) 
    int a = (gi() + ans) % (n + 1), l = (gi() + ans) % i + 1, r = (gi() + ans) % i + 1, k = (gi() + ans) % (n + 1);
    if(l > r) 
      swap(l, r);
    
    mdf(a, i);
    auto x = s.lower_bound(mp(a, 0));
    if(x != s.end() && x->fi == a) 
      int L = 1, R = x->se, p = R, q = a;
      auto y = x;
      ++y;
      if(y != s.end()) 
        L = y->se + 1;
      
      add_seg(a, L, R, -1, i);
      while(p >= L) 
        int w = bin(p, q), z = max(lst(w) + 1, L);
        add_seg(w, z, p, 1, i);
        p = z - 1;
      
    
    add_seg(a == 0, i, i, 1, i);
    cout << (ans = qry(k, l, r)) << '\n';
  
  return 0;

以上是关于Codeforces Global Round 3 题解的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Global Round #12

Codeforces Global Round 7

Codeforces Global Round 6

Codeforces Global Round 3 B. Born This Way

Codeforces Global Round 3:B. Born This Way

Codeforces Global Round 19