제출 #752151

#제출 시각아이디문제언어결과실행 시간메모리
752151Sam_a17Dynamic Diameter (CEOI19_diameter)C++17
100 / 100
408 ms59980 KiB
#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(x) __builtin_popcount(x)
 
#define pb push_back
#define popf pop_front
#define popb pop_back
 
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(deque <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 << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// 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 != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "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 = 3e5 + 10, LOG = 21;
vector<pair<long long, long long>> adj[N];
long long n, q, w, cost[N], which[N];
long long p[N], first[N], last[N];
int op[N];

vector<int> path;
void dfs(int node, int parent) {
  for(auto i: adj[node]) {
    if(i.first == parent) continue;
    path.push_back(i.second);
    dfs(i.first, node);
    path.push_back(i.second);
  }
}

struct segTree {
  struct node {
    long long value, max_abc, max_a, max_bc, max_ab, max_b;
  };

  long long size = 1;
  vector<node> tree;

  void init(long long s) {
    while(size < s) {
      size *= 2;
    }
    tree.assign(2 * size - 1, {0, 0, 0, 0, 0, 0});
  }

  node lef(long long val) {
    return {val, val, val, val, 0, 0};
  }

  node rig(long long val) {
    return {-val, val, 0, val, val, 2 * val};
  }

  node merge(node a, node b) {
    a.max_abc = max({a.max_abc, b.max_abc, a.max_ab + b.max_a + a.value, a.max_a + b.max_bc - a.value});

    a.max_ab = max({a.max_ab, b.max_ab - a.value, a.max_a + b.max_b - 2 * a.value});
    a.max_bc = max({a.max_bc, b.max_bc - a.value, a.max_b + b.max_a + a.value});

    a.max_a = max({a.max_a, b.max_a + a.value});
    a.max_b = max(a.max_b, b.max_b - 2 * a.value);
    
    a.value = a.value + b.value;
    return a;
  }

  void upd(int u, int x, int lx, int rx) {
    if(rx - lx == 1) {
      long long cur = cost[path[lx - 1]];
      if(op[lx]) {
        tree[x] = lef(cur);
      } else {
        tree[x] = rig(cur);
      }
    } else {
      int mid = (lx + rx) / 2;
      if(u < mid) {
        upd(u, 2 * x + 1, lx, mid);
      } else {
        upd(u, 2 * x + 2, mid, rx);
      }
      tree[x] = merge(tree[2 * x + 1], tree[2 * x + 2]);
    }
  }

  void upd(int u) {
    upd(u, 0, 0, size);
  }

  node qry(int l, int r, int x, int lx, int rx) {

    if(lx >= r || rx <= l) {
      return {0, 0, 0, 0, 0, 0};
    }

    if(lx >= l && rx <= r) {
      return tree[x];
    }

    int mid = (lx + rx) / 2;
    node s1 = qry(l, r, 2 * x + 1, lx, mid);
    node s2 = qry(l, r, 2 * x + 2, mid, rx);
    return merge(s1, s2);
  }

  node qry(int l, int r) {
    return qry(l, r, 0, 0, size);
  }
};

segTree seg;

void solve_() {
  cin >> n >> q >> w;
  seg.init(2 * n + 10);
  for(int i = 1; i <= n - 1; i++) {
    long long a, b, c; cin >> a >> b >> c;
    if(a > b) swap(a, b);

    adj[a].push_back({b, i});
    adj[b].push_back({a, i});

    //
    cost[i] = c;
    which[i] = b;
  }

  dfs(1, 0);
  
  memset(first, -1, sizeof(first));
  memset(last, -1, sizeof(last));

  for(int i = 0; i < sz(path); i++) {
    if(first[path[i]] == -1) {
      first[path[i]] = (i + 1);
      op[i + 1] = 1;
    } 
    last[path[i]] = (i + 1);
  }

  for(int i = 1; i <= sz(path); i++) {
    seg.upd(i);
  }

  long long las = 0;
  while(q--) {
    long long d, e; cin >> d >> e;
    d = (d + las) % (n - 1);
    e = (e + las) % w;
    d++;
    cost[d] = e;

    seg.upd(first[d]);
    seg.upd(last[d]);
    
    cout << seg.tree[0].max_abc << '\n';
    las = seg.tree[0].max_abc;
  }
}

int main() {
  setIO();

  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };
 
  int test_cases = 1;
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
} 

컴파일 시 표준 에러 (stderr) 메시지

diameter.cpp: In function 'void setIO(std::string)':
diameter.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...