Submission #732326


Source Code Expand

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <functional>
#include <numeric>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <map>
#include <set>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;

int n, q, a[100005];
int par[17][100005], pmin[17][100005], dep[100005];
vector<int> gph[100005];

void dfs(int x, int p){
	par[0][x] = p;
	pmin[0][x] = a[x];
	for(int i=1; i<17; i++){
		par[i][x] = par[i-1][par[i-1][x]];
		pmin[i][x] = min(pmin[i-1][x], pmin[i-1][par[i-1][x]]);
	}
	for(auto &i : gph[x]){
		if(i != p){
			dep[i] = dep[x] + 1;
			dfs(i, x);
		}
	}
}

int path_query(int s, int e){
	int dx = dep[e] - dep[s] + 1;
	int ret = 2e9;
	for(int i=0; i<=16; i++){
		if((dx >> i) & 1) {
			ret = min(ret, pmin[i][e]);
			e = par[i][e];
		}
	}
	return ret;
}

int level_ancestor(int x, int p){
	for(int i=0; i<=16; i++){
		if((p >> i) & 1) x = par[i][x];
	}
	return x;
}

int lca(int s, int e){
	if(dep[e] < dep[s]) swap(s, e);
	e = level_ancestor(e, dep[e] - dep[s]);
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e) return par[0][s];
	return s;
}

int lowpath(int s, int e, int x){
	int st = 0, ed = dep[e] - dep[s];
	while(st != ed){
		int m = (st+ed)/ 2;
		if(path_query(level_ancestor(e, m), e) <= x){
			ed = m;
		}
		else st = m+1;
	}
	if(path_query(level_ancestor(e, st), e) > x) return -1;
	return level_ancestor(e, st);
}

int highpath(int s, int e, int x){
	int st = 0, ed = dep[e] - dep[s];
	while(st != ed){
		int m = (st+ed+1)/ 2;
		if(path_query(s, level_ancestor(e, m)) <= x){
			st = m;
		}
		else ed = m-1;
	}
	if(path_query(s, level_ancestor(e, st)) > x) return -1;
	return level_ancestor(e, st);
}

int main(){
	scanf("%d %d",&n,&q);
	for(int i=1; i<=n; i++){
		scanf("%d",&a[i]);
	}
	memset(pmin ,0x3f, sizeof(pmin));
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d",&s,&e);
		gph[s].push_back(e);
		gph[e].push_back(s);
	}
	dep[1] = 1;
	dfs(1, 0);
	for(int i=0; i<q; i++){
		int s, e, x;
		scanf("%d %d %d",&x,&s,&e);
		int l = lca(s, e);
		while(1){
			int p = lowpath(l, s, x);
			if(p == -1) break;
			x %= a[p];
		}
		while(1){
			int p = highpath(l, e, x);
			if(p == -1) break;
			x %= a[p];
		}
		printf("%d\n",x);
	}
}

Submission Info

Submission Time
Task J - MODクエリ
User koosaga
Language C++11 (GCC 4.9.2)
Score 400
Code Size 2605 Byte
Status AC
Exec Time 1883 ms
Memory 26456 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:103:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&q);
                      ^
./Main.cpp:105:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&a[i]);
                    ^
./Main.cpp:110:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&s,&e);
                       ^
./Main.cpp:118:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&x,&s,&e);
                             ^

Judge Result

Set Name Small All
Score / Max Score 30 / 30 370 / 370
Status
AC × 16
AC × 38
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 394 ms 11224 KB
15_path_00.txt AC 57 ms 11220 KB
15_path_01.txt AC 56 ms 11220 KB
15_path_02.txt AC 55 ms 11348 KB
15_path_03.txt AC 58 ms 11228 KB
15_path_04.txt AC 57 ms 11220 KB
16_path_05.txt AC 58 ms 11224 KB
16_path_06.txt AC 60 ms 11224 KB
16_path_07.txt AC 62 ms 11224 KB
16_path_08.txt AC 59 ms 11220 KB
16_path_09.txt AC 59 ms 11212 KB
17_path_10.txt AC 1424 ms 26324 KB
17_path_11.txt AC 1525 ms 26444 KB
17_path_12.txt AC 1589 ms 26328 KB
17_path_13.txt AC 1422 ms 26352 KB
17_path_14.txt AC 1492 ms 26456 KB
20_random_15.txt AC 58 ms 11220 KB
20_random_16.txt AC 57 ms 11220 KB
20_random_17.txt AC 56 ms 11220 KB
21_random_18.txt AC 57 ms 11216 KB
21_random_19.txt AC 57 ms 11216 KB
21_random_20.txt AC 57 ms 11220 KB
30_random_21.txt AC 57 ms 11212 KB
30_random_22.txt AC 57 ms 11244 KB
30_random_23.txt AC 57 ms 11224 KB
40_random_24.txt AC 557 ms 21872 KB
40_random_25.txt AC 569 ms 21844 KB
40_random_26.txt AC 572 ms 21840 KB
49_sample_02.txt AC 57 ms 11212 KB
61_test_27.txt AC 1265 ms 24016 KB
61_test_28.txt AC 1180 ms 24028 KB
61_test_29.txt AC 1380 ms 23128 KB
63_star_30.txt AC 411 ms 22072 KB
63_star_31.txt AC 408 ms 22096 KB
63_star_32.txt AC 411 ms 22088 KB
64_three_33.txt AC 1820 ms 24400 KB
64_three_34.txt AC 1807 ms 24788 KB
64_three_35.txt AC 1883 ms 24028 KB