# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1089462 |
2024-09-16T14:40:07 Z |
phong |
Harvest (JOI20_harvest) |
C++17 |
|
80 ms |
131672 KB |
#include<bits/stdc++.h>
#define ll long long
#define int long long
const int nmax = 5e5 + 5, N = 1e6;
const ll oo = 1e9 + 5, base = 311;
const int lg = 62, tx = 26;
const ll mod = 1e9 + 7, mod2 = 1e9+3;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
#define NAME code
using namespace std;
int n, m, L, C;
pii a[nmax], b[nmax];
pii c[nmax];
vector<pii> inp[nmax], out[nmax], adj[nmax];
map<int, int>mp[nmax];
bool vis[nmax];
vector<int> tmp;
int root;
void dfs(int u, int p){
vis[u] = 1;
tmp.push_back(u);
for(auto [w, v] : adj[u]){
if(v == p) continue;
// cout << u << ' ' << v << ' ' << mp[v][u] << endl;
if(vis[v]) root = v;
else{
vis[v] = 1;
dfs(v, u);
}
}
}
/*
3 2 7 3
1 4 6
0 5
3
1 7
2 3
3 8
*/
int root_2, par[nmax];
ll dis[nmax];
int len = 0;
void dfs_2(int u){
vis[u] = 1;
for(auto [w, v] : out[u]){
// cout << u << ' ' << v << ' ' << mp[v][u] << endl;
if(vis[v]){
root_2 = u;
continue;
}
dis[v] = dis[u] + mp[v][u];
par[v] = u;
dfs_2(v);
}
}
int tour[nmax], st[nmax], en[nmax], Time = 0;
void dfs_3(int u, int p){
tour[++Time] = u;
st[u] = Time;
for(auto [w, v]: out[u]){
if(v == p || vis[p]) continue;
dfs_3(v, u);
}
en[u] = Time;
}
ll ans[nmax];
vector<int> tree[nmax << 2];
void build(int id, int l, int r){
tree[id].clear();
if(l == r){
int x = tour[l];
if(x > n) tree[id].push_back(dis[x]);
return;
}
int mid = r + l >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid +1, r);
tree[id].resize(tree[id << 1].size() + tree[id << 1 | 1].size());
merge(tree[id << 1].begin(), tree[id << 1].end(), tree[id << 1 | 1].begin(), tree[id << 1 | 1].end(), tree[id].begin());
}
int get(int id, int l, int r, int u, int v, int k){
if(tree[id].empty()) return 0;
if(l >= u && r <= v){
return upper_bound(tree[id].begin(), tree[id].end(), k) - tree[id].begin();
}
int mid = r + l >> 1;
if(mid < u) return get(id << 1 | 1, mid + 1, r, u, v, k);
if(mid + 1 > v) return get(id << 1, l, mid, u, v, k);
return get(id << 1, l, mid, u, v, k) +get(id << 1 | 1, mid + 1, r, u, v, k);
}
vector<pii> Q[nmax];
int dos[nmax];
void dfs_4(int u){
vis[u] = 1;
for(auto [w,v] : inp[u]){
len += w;
if(vis[v]) continue;
dos[v] = dos[u] + w;
dfs_4(v);
}
}
vector<int> hos[nmax];
void dfs_5(int u, int p){
if(u > n) hos[p].push_back(u);
for(auto [w, v] : out[u]){
if(vis[v]) continue;
dfs_5(v,p);
}
}
struct cay2{
}str;
void solve(int i){
len = 0;
dfs(i, -1);//tìm tplt
for(auto p : tmp) vis[p] = 0;
dfs_2(root);//tạo dis()
int x = root_2;
vector<int> one;
while(x != root){
one.push_back(x);
x = par[x];
}
one.push_back(root);
reverse(one.begin(), one.end());
//
for(auto p : tmp) vis[p] = 0;
Time = 0;
dfs_3(root, -1);//xây euler tour
for(auto p : tmp) vis[p] = 0;
for(auto p : one) vis[p] = 1;
build(1, 1, Time);
for(auto u : tmp){
if(vis[u]) continue;
for(auto [id, T] : Q[u]){
// cout << id << ' ';
int l = st[u], r = en[u];
ans[id] = get(1, 1, Time, l, r, T + dis[u]);
}
}
//
for(auto p : tmp) vis[p] = 0;
dfs_4(root);//xây dos vòng tròn
vector<int> nen;
for(int i = 1; i <= one.size(); ++i){
int u = one[i - 1];
hos[i].clear();
dfs_5(u, i);
for(auto j : hos[i]){
nen.push_back(dis[j]);
nen.push_back(dis[j] % len);
}
for(auto [id, T] : Q[u]){
nen.push_back(T - dos[u]);
nen.push_back((T - dos[u]) % len);
}
}
sort(nen.begin(), nen.end());
nen.erase(unique(nen.begin(), nen.end()), nen.end());
for(int i = 1; i <= one.size(); ++i){
int u = one[i - 1];
for(auto [id, T] : Q[u]){
// cout << id << ' ';
for(int p = 1; p < i; ++p){
for(auto j : hos[p]){
// cout << dis[j] + dos[u] << ' ' << j << ' ' << u << endl;
if(T - dos[u] >= dis[j]){
ans[id] += (T - dos[u] - dis[j]) / len;
ans[id]++;
}
}
}
for(int p = i; p <= one.size(); ++p){
for(auto j : hos[p]){
if(T + dis[u] >= dis[j]){
ans[id] += (T + dis[u] - dis[j]) / len;
ans[id]++;
}
}
}
}
}
for(auto p : tmp) vis[p] = 1;
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >> m >> L >> C;
for(int i = 1; i <= n; ++i) cin >> a[i].fi, a[i].se = i;
for(int i = 1; i <= m; ++i) cin >> b[i].fi, b[i].se = i + n;
int q; cin >> q;
for(int i = 1; i <= n; ++i){
cin >> c[i].fi >> c[i].se;
Q[c[i].fi].push_back({i, c[i].se});
}
for(int i = n + 1; i <= 2 * n; ++i){
a[i] = a[i - n];
int l = i - n + 1, r = i, kq = i, cur;
int x = C % L;
while(l <= r){
int mid = r + l >> 1;
int dist = 0;
if(a[mid].fi > a[i].fi){
dist = L - a[mid].fi + a[i].fi;
}
else dist = a[i].fi - a[mid].fi;
if(dist >= x){
l = mid + 1;
kq = mid;
cur = L * (C / L) + dist;
}
else r = mid - 1;
}
if(kq != -1){
// cout << a[i].se << ' ' << a[kq].se << endl;
mp[a[i].se][a[kq].se] = cur;
adj[a[i].se].push_back({cur, a[kq].se});
adj[a[kq].se].push_back({cur, a[i].se});
inp[a[i].se].push_back({cur, a[kq].se});
out[a[kq].se].push_back({cur, a[i].se});
}
}
for(int i = 1; i <= m; ++i){
int l = 1, r = n, kq = -1;
while(l <= r){
int mid = r+ l >> 1;
if(a[mid].fi <= b[i].fi){
l = mid + 1;
kq = mid;
}
else r = mid - 1;
}
if(kq != -1){
int cur = b[i].fi - a[kq].fi;
mp[b[i].se][a[kq].se] = cur;
adj[b[i].se].push_back({cur, a[kq].se});
adj[a[kq].se].push_back({cur, b[i].se});
inp[b[i].se].push_back({cur, a[kq].se});
out[a[kq].se].push_back({cur, b[i].se});
}
else{
kq = n;
int cur = b[i].fi + (L - a[kq].fi);
mp[b[i].se][a[kq].se] = cur;
adj[b[i].se].push_back({cur, a[kq].se});
adj[a[kq].se].push_back({cur, b[i].se});
inp[b[i].se].push_back({cur, a[kq].se});
out[a[kq].se].push_back({cur, b[i].se});
}
}
for(int i = 1; i <= n; ++i){
if(!vis[i]){
solve(i);
}
}
// cout << dis[4]<< ' ' <<dis[3] << endl;
for(int i = 1; i <= q; ++i) cout << ans[i] <<endl;
}
/*
3 2 7 3
1 4 6
0 5
3
1 7
2 3
3 8
*/
Compilation message
harvest.cpp: In function 'void build(long long int, long long int, long long int)':
harvest.cpp:83:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | int mid = r + l >> 1;
| ~~^~~
harvest.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int, long long int)':
harvest.cpp:95:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
95 | int mid = r + l >> 1;
| ~~^~~
harvest.cpp: In function 'void solve(long long int)':
harvest.cpp:159:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for(int i = 1; i <= one.size(); ++i){
| ~~^~~~~~~~~~~~~
harvest.cpp:175:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
175 | for(int i = 1; i <= one.size(); ++i){
| ~~^~~~~~~~~~~~~
harvest.cpp:188:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
188 | for(int p = i; p <= one.size(); ++p){
| ~~^~~~~~~~~~~~~
harvest.cpp: At global scope:
harvest.cpp:204:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
204 | main(){
| ^~~~
harvest.cpp: In function 'int main()':
harvest.cpp:222:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
222 | int mid = r + l >> 1;
| ~~^~~
harvest.cpp:247:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
247 | int mid = r+ l >> 1;
| ~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
129616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
80 ms |
131672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
129616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |