#include<bits/stdc++.h>
#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task2 "ftree"
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define mask(k) (1<<k)
#define mp make_pair
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
#define getchar_unlocked getchar
inline int scan()
{
char c = getchar_unlocked();
int x = 0;
while (c < '0' || c > '9')
{
c = getchar_unlocked();
}
while (c >= '0' && c <= '9')
{
x = (x << 1) + (x << 3) + c - '0';
c = getchar_unlocked();
}
return x;
}
void phongbeo();
const int inf = 2e9;
const ll mod2 = 1e9+7;
const int mod1 = 998244353;
const ll base=67;
int add(int x,int y)
{
if(x+y>=mod2) x-=mod2;
if(x+y<0)x+=mod2;
return x+y;
}
struct icd
{
long double a;
int b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a, b, c, d;
};
struct ie
{
int a, b, c, d, e;
};
ll n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
ll i, s10, s12,k1,k2,k3,s11,lim,w,l,r ;
ll kk;
ll el = 19;
main()
{
if(fopen(task2".inp","r"))
{
fin(task2);
fou(task2);
}
if(fopen(task".inp","r"))
{
fin(task);
fou(task);
}
NHP
/// cin>>s1;
//modwwe
phongbeo(),down
// checktime
}
stack<int>sc;
int a[200007],b[200007],in[200007],v[200007],root[200007],baby[200007],dp[200007],cost[200007],ou[200007],ans[200007];
vector<int> scc[200007],ke[200007],v2[200007],take[200007],v3[200007];
vector<ic> vc;
vector<ib>ask[200007];
ib c[200007];
bool dd[200007];
bool cmp(ib a,ib b)
{
return a.a<b.a;
}
bool cmp2(ic a,ic b)
{
return a.a<b.a;
}
struct bit
{
int bit[200007];
void upd(int x)
{
for(x; x; x-=x&-x)
bit[x]++;
}
int get(int x)
{
int s=0;
for(x; x<=n; x+=x&-x)s+=bit[x];
return s;
}
int get(int l,int r)
{
return get(l)-get(r+1);
}
} fen;
struct bit2
{
int bit[200007];
vector<int> bit2;
void hihi(vector<int>&ki,int x)
{
for(int i=1; i<=bit2.size(); i++)
bit[i]=0;
vector<int>().swap(bit2);
for(auto f:ki)
bit2.pb(f%x);
sort(bit2.begin(),bit2.end());
bit2.erase(unique(bit2.begin(),bit2.end()),bit2.end());
}
void upd(int x)
{
for(int t=lower_bound(bit2.begin(),bit2.end(),x)-bit2.begin()+1; t; t-=t&-t)
{
bit[t]++;
}
}
int get(int x)
{
int s=0;
for(int t=upper_bound(bit2.begin(),bit2.end(),x)-bit2.begin()+1; t<=bit2.size(); t+=t&-t)
{
s+=bit[t];
}
return s;
}
} fen2;
void dfs(int x)
{
sc.push(x);
in[x]=1;
if(in[v[x]]!=1)
{
ke[v[x]].pb(x);
}
if(in[v[x]]==0)dfs(v[x]);
else if(in[v[x]]==1)
{
s2=-1;
dem2++;
while(s2!=v[x])
{
s2=sc.top();
sc.pop();
root[s2]=x;
baby[s2]=dem2;
scc[dem2].pb(s2);
}
dd[x]=1;
}
in[x]=2;
}
void dfs2(int x,int y)
{
in[x]=++dem;
if(baby[x]!=0)y=x;
for(auto f:ke[x])
{
dp[f]=dp[x]+cost[f];
dfs2(f,y);
}
for(auto f:take[x])
{
vc.pb({f+dp[x],x,0});
v2[y].pb({f+dp[x]-dp[y]});
}
for(auto f:v2[x])
v3[baby[x]].pb({f+dp[x]});
ou[x]=dem;
}
void phongbeo()
{
cin>>n>>m>>lim>>k;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=m; i++)
cin>>b[i];
///first take
l=1;
for(int i=1; i<=n; i++)
{
while(l<=m&&b[l]<a[i])
{
int ss=-a[i-1];
if(i==1)ss+=lim-a[n];
int s=i-1;
if(s==0)s=n;
take[s].pb(ss+b[l]);
l++;
}
}
///second take
for(int i=l; i<=m; i++)
take[n].pb(b[i]-a[n]);
for(int i=1; i<=n; i++)
c[i]= {(a[i]+k)%lim,i};
l=1;
sort(c+1,c+1+n,cmp);
for(int i=1; i<=n; i++)
{
while(l<=n&&a[l]<c[i].a)
{
int s=i-1;
if(s==0)s=n;
v[l]=c[s].b;
int hihi=0;
if(i==1)hihi+=lim-c[n].a;
cost[l]=hihi+a[l]-c[i-1].a+k;
l++;
}
}
for(int i=l; i<=n; i++)
{
v[i]=c[n].b;
cost[i]=a[i]-c[n].a+k;
}
for(int i=1; i<=n; i++)
if(in[i]==0)
dfs(i);
dem=0;
for(int i=1; i<=n; i++)
if(dd[i])
{
dp[i]=cost[i];
dfs2(i,i);
}
cin>>m;
for(int i=1; i<=m; i++)
{
cin>>l>>r;
vc.pb({r+dp[l],l,i});
ask[l].pb({r,i});
}
sort(vc.begin(),vc.end(),cmp2);
for(auto x:vc)
{
if(x.c==0)
{
fen.upd(in[x.b]);
}
else
{
ans[x.c]=fen.get(in[x.b],ou[x.b]);
}
}
for(int i=1; i<=dem2; i++)
{
s4=0;
vc.clear();
for(auto x:scc[i])
s4+=cost[x];
int hihi=dp[scc[i].back()];
fen2.hihi(v3[i],s4);
s5=0;
s6=0;
for(auto x:scc[i])
for(auto f:ask[x])
vc.pb({f.a-hihi+dp[x],f.b});
for(auto x:v3[i])
vc.pb({x,0});
sort(vc.begin(),vc.end(),cmp2);
for(auto x:vc)
if(x.b==0)
{
fen2.upd(x.a%s4);
s5++;
s6+=x.a/s4;
}
else
{
ans[x.b]+=s5+s5*(x.a/s4)-s6-fen2.get(x.a%s4);
}
}
for(int i=1; i<=m; i++)
cout<<ans[i],down
}
Compilation message (stderr)
harvest.cpp:74:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
74 | main()
| ^~~~
harvest.cpp: In function 'int main()':
harvest.cpp:12:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
harvest.cpp:78:9: note: in expansion of macro 'fin'
78 | fin(task2);
| ^~~
harvest.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fou(x) freopen(x".out","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
harvest.cpp:79:9: note: in expansion of macro 'fou'
79 | fou(task2);
| ^~~
harvest.cpp:12:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
harvest.cpp:83:9: note: in expansion of macro 'fin'
83 | fin(task);
| ^~~
harvest.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fou(x) freopen(x".out","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
harvest.cpp:84:9: note: in expansion of macro 'fou'
84 | fou(task);
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |