답안 #673882

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
673882 2022-12-22T10:33:26 Z Sam_a17 Sirni (COCI17_sirni) C++11
140 / 140
2140 ms 786376 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount
 
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// for random generations
// mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
mt19937 myrand(373);
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if(str == "input") {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  } else {
    // freopen("skis.in", "r", stdin);
    // freopen("skis.out", "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10, M = 1e7 + 10;
vector<pair<int, int>> adj[M];
int a[N], n, b[M], p[N], sz[N];
int contain[M];

// dsu
int find(int a) {
  if(a != p[a]) {
    p[a] = find(p[a]);
  }
  return p[a];
}

int same(int a, int b) {
  if(find(a) == find(b)) {
    return 1;
  }
  return 0;
}

int merge(int a, int b) {
  a = find(a), b = find(b);
  if(a == b) {
    return 0;
  }
  if(sz[b] > sz[a]) {
    swap(a, b);
  }
  p[b] = a, sz[a] += sz[b];
  return 1;
}

void solve_() {
  cin >> n;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]] = i;
    contain[a[i]] = i;
  }

  for(int i = M - 5; i >= 1; i--) {
    if(!b[i]) {
      b[i] = b[i + 1];
    }
  }

  for(int i = 1; i < M - 5; i++) {
    if(!contain[i]) continue;
    if(b[i + 1]) {
      adj[a[b[i + 1]] % i].emplace_back(contain[i], b[i + 1]);
    }
    
    for(int j = 2 * i; j < M - 5; j += i) {
      if(!b[j]) continue;
      adj[a[b[j]] % i].emplace_back(contain[i], b[j]);
    }
  }

  for(int i = 1; i <= N; i++) {
    p[i] = i, sz[i] = 1;
  }

  long long answ = 0;
  for(int i = 0; i < M - 5; i++) {
    for(auto j: adj[i]) {
      int first = j.first, second = j.second;

      if(merge(first, second)) {
        answ += i;
      }
    }
  }

  cout << answ << '\n';
}
 
int main() {
  setIO();
 
  auto solve = [&](int test_case)-> void {
    for(int i = 1; i <= test_case; i++) {
      // cout << "Case #" << i << ": ";
      solve_();
    }
  };
 
  int test_cases = 1;
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
} 

Compilation message

sirni.cpp: In function 'void setIO(std::string)':
sirni.cpp:69:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp: In function 'void solve_()':
sirni.cpp:137:10: warning: iteration 200009 invokes undefined behavior [-Waggressive-loop-optimizations]
  137 |     p[i] = i, sz[i] = 1;
      |     ~~~~~^~~
sirni.cpp:136:20: note: within this loop
  136 |   for(int i = 1; i <= N; i++) {
      |                  ~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 178 ms 279628 KB Output is correct
2 Correct 263 ms 307264 KB Output is correct
3 Correct 177 ms 280136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 233 ms 276008 KB Output is correct
2 Correct 1326 ms 671196 KB Output is correct
3 Correct 176 ms 280524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 175 ms 279960 KB Output is correct
2 Correct 172 ms 278304 KB Output is correct
3 Correct 178 ms 280000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 288 ms 289652 KB Output is correct
2 Correct 549 ms 318128 KB Output is correct
3 Correct 339 ms 300712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 281864 KB Output is correct
2 Correct 433 ms 302108 KB Output is correct
3 Correct 341 ms 287976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 417 ms 301168 KB Output is correct
2 Correct 673 ms 335480 KB Output is correct
3 Correct 328 ms 298780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 280204 KB Output is correct
2 Correct 718 ms 336356 KB Output is correct
3 Correct 326 ms 300556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 256 ms 328356 KB Output is correct
2 Correct 1610 ms 673140 KB Output is correct
3 Correct 327 ms 330860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 316 ms 332268 KB Output is correct
2 Correct 2140 ms 786376 KB Output is correct
3 Correct 435 ms 387616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 312268 KB Output is correct
2 Correct 1897 ms 666244 KB Output is correct
3 Correct 341 ms 302624 KB Output is correct