fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef long double ld;
  6. typedef double db;
  7. typedef string str;
  8.  
  9. typedef pair<int,int> pi;
  10. typedef pair<ll,ll> pl;
  11. typedef pair<db,db> pd;
  12.  
  13. typedef vector<int> vi;
  14. typedef vector<bool> vb;
  15. typedef vector<ll> vl;
  16. typedef vector<db> vd;
  17. typedef vector<str> vs;
  18. typedef vector<pi> vpi;
  19. typedef vector<pl> vpl;
  20. typedef vector<pd> vpd;
  21. template<class T> using V = vector<T>;
  22. template<class T, size_t SZ> using AR = array<T,SZ>;
  23.  
  24. #define mp make_pair
  25. #define f first
  26. #define s second
  27. #define sz(x) (int)(x).size()
  28. #define all(x) begin(x), end(x)
  29. #define rall(x) (x).rbegin(), (x).rend()
  30. #define sor(x) sort(all(x))
  31. #define rsz resize
  32. #define ins insert
  33. #define ft front()
  34. #define bk back()
  35. #define pf push_front
  36. #define pb push_back
  37. #define eb emplace_back
  38. #define lb lower_bound
  39. #define ub upper_bound
  40.  
  41. #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
  42. #define F0R(i,a) FOR(i,0,a)
  43. #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
  44. #define R0F(i,a) ROF(i,0,a)
  45. #define trav(a,x) for (auto& a: x)
  46.  
  47. const int MOD = 1e9+7; // 998244353;
  48. const int MX = 2e5+5;
  49. const ll INF = 1e18;
  50. const ld PI = acos((ld)-1);
  51. const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
  52. mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
  53.  
  54. template<class T> bool ckmin(T& a, const T& b) {
  55. return b < a ? a = b, 1 : 0; }
  56. template<class T> bool ckmax(T& a, const T& b) {
  57. return a < b ? a = b, 1 : 0; }
  58. constexpr int pct(int x) { return __builtin_popcount(x); }
  59. constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x))
  60. ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
  61. ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
  62. ll half(ll x) { return fdiv(x,2); }
  63.  
  64. template<class T, class U> T fstTrue(T lo, T hi, U f) {
  65. // note: if (lo+hi)/2 is used instead of half(lo+hi) then this will loop infinitely when lo=hi
  66. hi ++; assert(lo <= hi); // assuming f is increasing
  67. while (lo < hi) { // find first index such that f is true
  68. T mid = half(lo+hi);
  69. f(mid) ? hi = mid : lo = mid+1;
  70. }
  71. return lo;
  72. }
  73. template<class T, class U> T lstTrue(T lo, T hi, U f) {
  74. lo --; assert(lo <= hi); // assuming f is decreasing
  75. while (lo < hi) { // find first index such that f is true
  76. T mid = half(lo+hi+1);
  77. f(mid) ? lo = mid : hi = mid-1;
  78. }
  79. return lo;
  80. }
  81. template<class T> void remDup(vector<T>& v) {
  82. sort(all(v)); v.erase(unique(all(v)),end(v)); }
  83.  
  84. // INPUT
  85. template<class A> void re(complex<A>& c);
  86. template<class A, class B> void re(pair<A,B>& p);
  87. template<class A> void re(vector<A>& v);
  88. template<class A, size_t SZ> void re(array<A,SZ>& a);
  89.  
  90. template<class T> void re(T& x) { cin >> x; }
  91. void re(db& d) { str t; re(t); d = stod(t); }
  92. void re(ld& d) { str t; re(t); d = stold(t); }
  93. template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
  94.  
  95. template<class A> void re(complex<A>& c) { A a,b; re(a,b); c = {a,b}; }
  96. template<class A, class B> void re(pair<A,B>& p) { re(p.f,p.s); }
  97. template<class A> void re(vector<A>& x) { trav(a,x) re(a); }
  98. template<class A, size_t SZ> void re(array<A,SZ>& x) { trav(a,x) re(a); }
  99.  
  100. // TO_STRING
  101. #define ts to_string
  102. str ts(char c) { return str(1,c); }
  103. str ts(const char* s) { return (str)s; }
  104. str ts(str s) { return s; }
  105. str ts(bool b) {
  106. #ifdef LOCAL
  107. return b ? "true" : "false";
  108. #else
  109. return ts((int)b);
  110. #endif
  111. }
  112. template<class A> str ts(complex<A> c) {
  113. stringstream ss; ss << c; return ss.str(); }
  114. str ts(vector<bool> v) {
  115. str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]);
  116. res += "}"; return res; }
  117. template<size_t SZ> str ts(bitset<SZ> b) {
  118. str res = ""; F0R(i,SZ) res += char('0'+b[i]);
  119. return res; }
  120. template<class A, class B> str ts(pair<A,B> p);
  121. template<class T> str ts(T v) { // containers with begin(), end()
  122. #ifdef LOCAL
  123. bool fst = 1; str res = "{";
  124. for (const auto& x: v) {
  125. if (!fst) res += ", ";
  126. fst = 0; res += ts(x);
  127. }
  128. res += "}"; return res;
  129. #else
  130. bool fst = 1; str res = "";
  131. for (const auto& x: v) {
  132. if (!fst) res += " ";
  133. fst = 0; res += ts(x);
  134. }
  135. return res;
  136.  
  137. #endif
  138. }
  139. template<class A, class B> str ts(pair<A,B> p) {
  140. #ifdef LOCAL
  141. return "("+ts(p.f)+", "+ts(p.s)+")";
  142. #else
  143. return ts(p.f)+" "+ts(p.s);
  144. #endif
  145. }
  146.  
  147. // OUTPUT
  148. template<class A> void pr(A x) { cout << ts(x); }
  149. template<class H, class... T> void pr(const H& h, const T&... t) {
  150. pr(h); pr(t...); }
  151. void ps() { cout << endl; } // print w/ spaces
  152. template<class H, class... T> void ps(const H& h, const T&... t) {
  153. pr(h); if (sizeof...(t)) pr(" "); ps(t...); }
  154.  
  155. // DEBUG
  156. void DBG() { cerr << "]" << endl; }
  157. template<class H, class... T> void DBG(H h, T... t) {
  158. cerr << ts(h); if (sizeof...(t)) cerr << ", ";
  159. DBG(t...); }
  160. #ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
  161. #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  162. #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
  163. << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
  164. #else
  165. #define dbg(...) 0
  166. #define chk(...) 0
  167. #endif
  168.  
  169. // FILE I/O
  170. void setIn(str s) { freopen(s.c_str(),"r",stdin); }
  171. void setOut(str s) { freopen(s.c_str(),"w",stdout); }
  172. void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
  173. void setIO(str s = "") {
  174. unsyncIO();
  175. // cin.exceptions(cin.failbit);
  176. // throws exception when do smth illegal
  177. // ex. try to read letter into int
  178. if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
  179. }
  180.  
  181. int N,D,U,Q;
  182. vi H;
  183. set<int> cur[MX], lst[MX]; // OK
  184. V<AR<int,3>> mod[MX]; // OK
  185.  
  186. vector<vi> stor[MX];
  187. V<V<AR<int,3>>> mods[MX];
  188. V<vi> h[MX];
  189.  
  190. vi vals(int x, int ti) { // get friends of x at time ti
  191. // in O(D+U/D) time (assume D=500)
  192. int ind = 0;
  193. while (ind+1 < sz(mods[x]) && mods[x][ind].bk[0] <= ti) ind ++;
  194. // oops this is U/D time lol, can replace with log(U/D) binsearch
  195. trav(t,mods[x][ind]) if (t[0] <= ti)
  196. stor[x][ind][t[1]] += t[2];
  197. vi ans;
  198. F0R(i,sz(h[x][ind])) if (stor[x][ind][i]) ans.pb(h[x][ind][i]);
  199. trav(t,mods[x][ind]) if (t[0] <= ti)
  200. stor[x][ind][t[1]] -= t[2];
  201. return ans;
  202. }
  203.  
  204. void deal(int x) {
  205. h[x].eb(); stor[x].eb(), mods[x].eb();
  206. vi& v = h[x].bk;
  207. trav(t,lst[x]) v.pb(H[t]);
  208. trav(t,mod[x]) v.pb(H[t[1]]);
  209. remDup(v);
  210. auto ind = [&](int x) -> int {
  211. auto it = lb(all(v),H[x]);
  212. return it-begin(v);
  213. };
  214. stor[x].bk.rsz(sz(v));
  215. trav(t,lst[x]) stor[x].bk[ind(t)] ++;
  216. lst[x] = cur[x];
  217. trav(t,mod[x]) t[1] = ind(t[1]);
  218. swap(mods[x].bk,mod[x]);
  219. }
  220.  
  221. void ins(int x, int y, int ti) {
  222. if (cur[x].count(y)) {
  223. cur[x].erase(y);
  224. mod[x].pb({ti,y,-1});
  225. } else {
  226. cur[x].ins(y);
  227. mod[x].pb({ti,y,1});
  228. }
  229. if (sz(mod[x]) == D) deal(x); // a lot of updates happened, save the current state
  230. }
  231.  
  232. int main() {
  233. setIO(); re(N,D,U,Q);
  234. H.rsz(N); re(H);
  235. FOR(i,1,U+1) {
  236. int A,B; re(A,B);
  237. ins(A,B,i), ins(B,A,i);
  238. }
  239. F0R(i,N) deal(i);
  240. F0R(i,Q) {
  241. int x,y,v; re(x,y,v);
  242. vi a = vals(x,v), b = vals(y,v);
  243. int ans = 1e9;
  244. int ia = 0, ib = 0;
  245. while (ia < sz(a) && ib < sz(b)) {
  246. ckmin(ans,abs(a[ia]-b[ib]));
  247. if (a[ia] < b[ib]) ia ++;
  248. else ib ++;
  249. }
  250. ps(ans);
  251. }
  252. // you should actually read the stuff at the bottom
  253. }
  254.  
  255. /* stuff you should look for
  256. * int overflow, array bounds
  257. * special cases (n=1?)
  258. * do smth instead of nothing and stay organized
  259. * WRITE STUFF DOWN
  260. * DON'T GET STUCK ON ONE APPROACH
  261. */
Success #stdin #stdout 0.01s 41276KB
stdin
6 5 11 4
2 42 1000 54 68 234
0 1
2 0
3 4
3 5
3 5
1 3
5 3
0 5
3 0
1 3
3 5
0 3 4
3 0 8
0 5 5
3 0 11
stdout
26
0
1000000000
14