Submission #534377
Source Code Expand
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <unordered_map>
#include <iterator>
#include <functional>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}
const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;
struct HLDecomp{
struct NodeInfo{
int idx, pos, cnt, par, heavy, depth, upar;
};
int n;
vector<NodeInfo> V;
vector<vi> e;
HLDecomp(const vector<vi> &g, int root = 0):n(g.size()), V(n){
stack<int> st;
st.push(root);
V[root].depth = 0;
V[root].par = -1;
while(!st.empty()){
int v = st.top(); st.pop();
if(v<0){
v = ~v;
if(v != root) V[V[v].par].cnt += V[v].cnt;
V[v].heavy = -1;
int mc = 0;
FOR(u, g[v])if(*u!=V[v].par && chmax(mc, V[*u].cnt)) V[v].heavy = *u;
}else{
V[v].cnt = 1;
st.push(~v);
FOR(u, g[v])if(*u!=V[v].par){
V[*u].upar = V[*u].par = v;
V[*u].depth = V[v].depth+1;
st.push(*u);
}
}
}
st.push(root);
while(!st.empty()){
vi d;
int v = st.top();st.pop();
int p = V[v].par;
for(;v>=0;v=V[v].heavy){
FOR(u, g[v])if(*u!=V[v].par && *u!=V[v].heavy) st.push(*u);
V[v].idx = e.size();
V[v].pos = d.size();
V[v].par = p;
d.pb(v);
}
e.pb(d);
}
// cout << e << endl;
}
size_t size(){return e.size();}
const vi &operator[](int i) const{return e[i];}
pii getPos(int v){return pii(V[v].idx, V[v].pos);}
struct Path{
int idx, s, t;
Path(int idx, int s, int t):idx(idx), s(s), t(t){}
};
vector<tuple<int, int, int>> path(int u, int v, int onlyEdge=0){
vector<tuple<int, int, int>> res, rev;
while(V[u].idx!=V[v].idx){
int pu = V[u].par;
int pv = V[v].par;
if((pu==-1 ? -1 : V[pu].depth) > (pv == -1 ? -1 : V[pv].depth)){
res.eb(V[u].idx, V[u].pos, 0);
u = V[u].par;
}else{
rev.eb(V[v].idx ,0 ,V[v].pos);
v = V[v].par;
}
}
int c = abs(V[u].pos - V[v].pos);
if(!onlyEdge) res.eb(V[u].idx, V[u].pos, V[v].pos);
else if(V[u].depth > V[v].depth) res.eb(V[u].idx, V[u].pos, V[v].pos + (V[v].pos < V[u].pos ? 1 : -1));
else if(V[u].depth < V[v].depth) res.eb(V[u].idx, V[u].pos + (V[u].pos < V[v].pos ? 1 : -1), V[v].pos);
res.insert(res.end(), RALL(rev));
return res;
}
int lca(int u, int v){
while(V[u].idx!=V[v].idx){
int pu = V[u].par;
int pv = V[v].par;
if((pu==-1 ? -1 : V[pu].depth) > (pv == -1 ? -1 : V[pv].depth)){
u = V[u].par;
}else{
v = V[v].par;
}
}
return (V[u].depth > V[v].depth) ? v : u;
}
int getDepth(int v){
return V[v].depth;
}
int par(int v){
return V[v].upar;
}
int par(int v, int d){
while(V[v].pos < d){
d -= V[v].pos + 1;
v = V[v].par;
}
return e[V[v].idx][V[v].pos - d];
}
int dist(int u, int v){
int t = lca(u, v);
return V[u].depth + V[v].depth - 2*V[t].depth;
}
int forward(int u, int v, int d, int t = -1){
if(t == -1) t = lca(u, v);
if(d <= V[u].depth - V[t].depth){
return par(u, d);
}else{
return par(v, V[u].depth + V[v].depth - 2*V[t].depth - d);
}
}
};
template<class H, class S>
struct SegTreeOnHLDecomp{
typedef typename H::val_t val_t;
vector<S> seg;
HLDecomp hld;
SegTreeOnHLDecomp(const vector<vi> &g, const vector<val_t> &val, int root=0)
:hld(g, root){
REP(i, hld.size()){
vector<val_t> d;
const vi& nodes = hld[i];
FOR(v, nodes) d.pb(val[*v]);
seg.eb(d);
reverse(ALL(d));
seg.eb(d);
}
}
val_t query(int u, int v){
auto res = H::def_val();
auto paths = hld.path(u, v);
FOR(p, paths){
int i, s, t; tie(i, s, t) = *p;
if(s <= t) res = H::merge(res, seg[2*i].query(s, t+1));
else res = H::merge(res, seg[2*i+1].query(hld[i].size()-1-s, hld[i].size()-t));
}
return res;
}
int lca(int u, int v){
return hld.lca(u, v);
}
int par(int u){
return hld.par(u);
}
};
template<typename Handler>
struct SegTree{
typedef typename Handler::val_t val_t;
vector<val_t> val;
Handler hdl;
int n;
SegTree(int size):hdl(){
n=1;
while(n<size) n<<=1;
val=vector<val_t>(2*n, hdl.def_val());
}
SegTree(const vector<val_t> &in):hdl(){
n=1;
while(n<in.size()) n<<=1;
val=vector<val_t>(2*n, hdl.def_val());
for(int i=n-1 + in.size()-1;i>=0;i--){
if(n-1 <= i) val[i] = in[i - (n-1)];
else val[i] = hdl.merge(val[i*2+1],val[i*2+2]);
}
}
val_t query(int a,int b,int k,int l,int r){
if(r<=a||b<=l) return hdl.def_val();
if(a<=l&&r<=b) return val[k];
return hdl.merge(query(a, b, k*2+1, l, (l+r)/2),
query(a, b, k*2+2, (l+r)/2, r)
);
}
val_t query(int a, int b){return query(a, b, 0, 0, n);}
val_t operator[](size_t i){return query(i, i+1);}
friend ostream& operator<<(ostream &os, SegTree<Handler> &t){
REP(i, t.n) os << (i ? ", " : "[") << t.query(i, i+1);
return os << "]";
}
};
struct handler{
typedef int val_t;
handler(){}
static val_t def_val(){ return INF; }
static val_t update(const val_t &l, const val_t &r){
return r;
}
static val_t merge(const val_t &l, const val_t &r){
return min(l, r);
}
};
int n, m;
int main(int argc, char *argv[]){
ios::sync_with_stdio(false);
cin >> n >> m;
vi d(n);
REP(i, n) cin >> d[i];
vector<vi> g(n);
REP(i, n-1){
int u, v;
cin >> u >> v; u--; v--;
g[u].pb(v);
g[v].pb(u);
}
SegTreeOnHLDecomp<handler, SegTree<handler>> seg(g, d);
REP(i, m){
int u, v, w;
cin >> w >> u >> v; u--; v--;
w %= d[u];
while(w && u != v){
u = seg.hld.forward(u, v, 1);
int dist = seg.hld.dist(u, v);
if(seg.query(u, v) > w){
u = v;
break;
}
int l=-1, r=dist; // [0, l] に w 以下は無い
// [0, r] に w 以下は存在する
while(l+1 < r){
int mid = (l+r)/2;
int t = seg.hld.forward(u, v, mid);
if(seg.query(u, t) <= w) r = mid;
else l = mid;
}
int t = seg.hld.forward(u, v, r);
//cout << u << ", " << v << ", " << w << ": " << t << endl;
w %= d[t];
u = t;
}
printf("%d\n", w);
}
return 0;
}
Submission Info
Submission Time |
|
Task |
J - MODクエリ |
User |
zerokugi |
Language |
C++11 (GCC 4.9.2) |
Score |
400 |
Code Size |
8233 Byte |
Status |
AC |
Exec Time |
1711 ms |
Memory |
27780 KB |
Judge Result
Set Name |
Small |
All |
Score / Max Score |
30 / 30 |
370 / 370 |
Status |
|
|
Set Name |
Test Cases |
Small |
00_sample_path_01.txt, 15_path_00.txt, 15_path_01.txt, 15_path_02.txt, 15_path_03.txt, 15_path_04.txt, 16_path_05.txt, 16_path_06.txt, 16_path_07.txt, 16_path_08.txt, 16_path_09.txt, 17_path_10.txt, 17_path_11.txt, 17_path_12.txt, 17_path_13.txt, 17_path_14.txt |
All |
00_sample_path_01.txt, 15_path_00.txt, 15_path_01.txt, 15_path_02.txt, 15_path_03.txt, 15_path_04.txt, 16_path_05.txt, 16_path_06.txt, 16_path_07.txt, 16_path_08.txt, 16_path_09.txt, 17_path_10.txt, 17_path_11.txt, 17_path_12.txt, 17_path_13.txt, 17_path_14.txt, 20_random_15.txt, 20_random_16.txt, 20_random_17.txt, 21_random_18.txt, 21_random_19.txt, 21_random_20.txt, 30_random_21.txt, 30_random_22.txt, 30_random_23.txt, 40_random_24.txt, 40_random_25.txt, 40_random_26.txt, 49_sample_02.txt, 61_test_27.txt, 61_test_28.txt, 61_test_29.txt, 63_star_30.txt, 63_star_31.txt, 63_star_32.txt, 64_three_33.txt, 64_three_34.txt, 64_three_35.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_path_01.txt |
AC |
29 ms |
784 KB |
15_path_00.txt |
AC |
30 ms |
920 KB |
15_path_01.txt |
AC |
32 ms |
804 KB |
15_path_02.txt |
AC |
27 ms |
800 KB |
15_path_03.txt |
AC |
26 ms |
804 KB |
15_path_04.txt |
AC |
26 ms |
928 KB |
16_path_05.txt |
AC |
27 ms |
924 KB |
16_path_06.txt |
AC |
27 ms |
808 KB |
16_path_07.txt |
AC |
26 ms |
800 KB |
16_path_08.txt |
AC |
25 ms |
804 KB |
16_path_09.txt |
AC |
25 ms |
928 KB |
17_path_10.txt |
AC |
1590 ms |
12776 KB |
17_path_11.txt |
AC |
1576 ms |
12776 KB |
17_path_12.txt |
AC |
1588 ms |
12780 KB |
17_path_13.txt |
AC |
1571 ms |
12768 KB |
17_path_14.txt |
AC |
1565 ms |
12780 KB |
20_random_15.txt |
AC |
26 ms |
796 KB |
20_random_16.txt |
AC |
24 ms |
804 KB |
20_random_17.txt |
AC |
25 ms |
916 KB |
21_random_18.txt |
AC |
26 ms |
708 KB |
21_random_19.txt |
AC |
25 ms |
928 KB |
21_random_20.txt |
AC |
26 ms |
804 KB |
30_random_21.txt |
AC |
24 ms |
800 KB |
30_random_22.txt |
AC |
26 ms |
792 KB |
30_random_23.txt |
AC |
26 ms |
800 KB |
40_random_24.txt |
AC |
1043 ms |
19152 KB |
40_random_25.txt |
AC |
1057 ms |
19148 KB |
40_random_26.txt |
AC |
1088 ms |
19148 KB |
49_sample_02.txt |
AC |
26 ms |
800 KB |
61_test_27.txt |
AC |
778 ms |
18896 KB |
61_test_28.txt |
AC |
730 ms |
18828 KB |
61_test_29.txt |
AC |
850 ms |
18824 KB |
63_star_30.txt |
AC |
335 ms |
27712 KB |
63_star_31.txt |
AC |
329 ms |
27780 KB |
63_star_32.txt |
AC |
335 ms |
27780 KB |
64_three_33.txt |
AC |
1672 ms |
12152 KB |
64_three_34.txt |
AC |
1675 ms |
13264 KB |
64_three_35.txt |
AC |
1711 ms |
12496 KB |