#pragma GCC optimize("Ofast,unroll-loops")
#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 task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#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 = 1e17;
const int mod2 = 1e9 + 9;
const int mod1 = 998244353;
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;
};
int n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, l, r, mid, l2, r2, center;
int i, s10, s12,k1,k2,s11;
int kk;
int el = 19;
main()
{
//fin(task);
// fou(task);
NHP
/// cin>>s1;
///modwwe
phongbeo();
// checktime
}
int s,t;
vector<ib> a[100001];
int b[100001];
vector<int> c[100001];
int ans[300001];
ib d[100001];
ib st[17][100001];
int f[100001];
int cd[100001];
ib dp[100001];
vector<int> vv;
vector<ib> v[100001];
int S=30;
bool cmp(ib a,ib b)
{
return a.a<b.a;
}
void phongbeo()
{
cin>>n>>t;
for(int i=1; i<n; i++)
{
cin>>k;
b[i]=k;
a[i].resize(k+1);
c[i].resize(k+1);
s9+=k;
for(int j=1; j<=k; j++)
cin>>a[i][j].a>>a[i][j].b,c[i][j]=++dem,d[dem]= {i,j};
sort(a[i].begin()+1,a[i].end(),cmp);
for(int j=k-1; j>=1; --j)
a[i][j].b=min(a[i][j].b,a[i][j+1].b);
}
for(int i=1; i<n-1; i++)
{
vv.clear();
for(int j=1; j<=b[i+1]; j++)
vv.pb(a[i+1][j].a);
for(int j=1; j<=b[i]; j++)
{
if(a[i][j].b>vv.back())
{
s3=vv[0]+t-a[i][j].a;
st[0][c[i][j]]= {c[i+1][1],s3};
}
else
{
s2=lower_bound(vv.begin(),vv.end(),a[i][j].b)-vv.begin()+1;
s3=a[i+1][s2].a-a[i][j].a;
st[0][c[i][j]]= {c[i+1][s2],s3};
}
}
}
for(int i=1; i<17; i++)
for(int j=1; j<=s9; j++)
st[i][j].a=st[i-1][st[i-1][j].a].a,
st[i][j].b=st[i-1][j].b+st[i-1][st[i-1][j].a].b;
s5=n+1;
for(int i=n-1; i>=1; --i)
{
if(b[i]<=S)s5=i;
f[i]=s5;
}
cin>>m;
for(int i=1; i<=m; i++)
{
cin>>l>>r;
if(l==r)
{
ans[i]=0;
continue;
}
if(b[l]>S)
v[l].pb({r,i});
else
{
for(int j=1; j<=b[l]; j++)
{
s=r-l-1;
s3=c[l][j];
s4=0;
for(s; s; s-=s&-s)
{
int g=31-__builtin_clz(s&-s);
s4+=st[g][s3].b;
s3=st[g][s3].a;
}
if(j==1)s5=s4+a[r-1][d[s3].b].b-a[r-1][d[s3].b].a;
else s5=min(s4+a[r-1][d[s3].b].b-a[r-1][d[s3].b].a,s5);
}
ans[i]=s5;
}
}
for(int i=1; i<n; i++)
{
if(v[i].size()!=0)
{
for(int j=1; j<=b[i];j++)
dp[j]= {c[i][j],0};
sort(v[i].begin(),v[i].end(),cmp);
bool de=0;
s4=i;
s9=-1;
for(auto x:v[i])
{
if(x.a==s9){ans[x.b]=s6; continue;}
s6=inf;
if(x.a<=f[i])
{
for(int j=1; j<=b[i]; j++)
{
s3=dp[j].a;
s5=dp[j].b;
s=x.a-s4-1;
for(s; s; s-=s&-s)
{
s7=31-__builtin_clz(s&-s);
s5+=st[s7][s3].b;
s3=st[s7][s3].a;
}
s6=min(s6,s5+a[x.a-1][d[s3].b].b-a[x.a-1][d[s3].b].a);
dp[j]= {s3,s5};
}
s4=x.a-1;
}
else
{
if(!de)
{
for(int j=1; j<=b[f[i]]; j++)
cd[c[f[i]][j]]=inf;
for(int j=1; j<=b[i]; j++)
{
s3=dp[j].a;
s5=dp[j].b;
s=f[i]-s4;
for(s; s; s-=s&-s)
{
s7=31-__builtin_clz(s&-s);
s5+=st[s7][s3].b;
s3=st[s7][s3].a;
}
//dp[j]= {s3,s5};
cd[s3]=min(cd[s3],s5);
}
}
de=1;
for(int j=1; j<=b[f[i]]; j++)
{
s=x.a-f[i]-1;
s3=c[f[i]][j];
s5=cd[s3];
for(s; s; s-=s&-s)
{
s7=31-__builtin_clz(s&-s);
s5+=st[s7][s3].b;
s3=st[s7][s3].a;
}
s6=min(s6,s5+a[x.a-1][d[s3].b].b-a[x.a-1][d[s3].b].a);
}
}
s9=x.a;
ans[x.b]=s6;
}
}
}
for(int i=1;i<=m;i++)
cout<<ans[i],down
}
Compilation message
Main.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
67 | main()
| ^~~~
Main.cpp: In function 'void phongbeo()':
Main.cpp:158:21: warning: statement has no effect [-Wunused-value]
158 | for(s; s; s-=s&-s)
| ^
Main.cpp:193:29: warning: statement has no effect [-Wunused-value]
193 | for(s; s; s-=s&-s)
| ^
Main.cpp:215:33: warning: statement has no effect [-Wunused-value]
215 | for(s; s; s-=s&-s)
| ^
Main.cpp:232:29: warning: statement has no effect [-Wunused-value]
232 | for(s; s; s-=s&-s)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7516 KB |
Output is correct |
2 |
Correct |
39 ms |
13692 KB |
Output is correct |
3 |
Correct |
51 ms |
14160 KB |
Output is correct |
4 |
Correct |
70 ms |
16212 KB |
Output is correct |
5 |
Correct |
63 ms |
14676 KB |
Output is correct |
6 |
Correct |
75 ms |
16212 KB |
Output is correct |
7 |
Correct |
80 ms |
16236 KB |
Output is correct |
8 |
Correct |
55 ms |
14220 KB |
Output is correct |
9 |
Correct |
76 ms |
16204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7516 KB |
Output is correct |
2 |
Correct |
39 ms |
13692 KB |
Output is correct |
3 |
Correct |
51 ms |
14160 KB |
Output is correct |
4 |
Correct |
70 ms |
16212 KB |
Output is correct |
5 |
Correct |
63 ms |
14676 KB |
Output is correct |
6 |
Correct |
75 ms |
16212 KB |
Output is correct |
7 |
Correct |
80 ms |
16236 KB |
Output is correct |
8 |
Correct |
55 ms |
14220 KB |
Output is correct |
9 |
Correct |
76 ms |
16204 KB |
Output is correct |
10 |
Correct |
4 ms |
7516 KB |
Output is correct |
11 |
Correct |
70 ms |
15680 KB |
Output is correct |
12 |
Correct |
74 ms |
15436 KB |
Output is correct |
13 |
Correct |
74 ms |
15680 KB |
Output is correct |
14 |
Correct |
73 ms |
15748 KB |
Output is correct |
15 |
Correct |
69 ms |
15696 KB |
Output is correct |
16 |
Correct |
44 ms |
13904 KB |
Output is correct |
17 |
Correct |
77 ms |
16180 KB |
Output is correct |
18 |
Correct |
69 ms |
15912 KB |
Output is correct |
19 |
Correct |
78 ms |
16084 KB |
Output is correct |
20 |
Correct |
69 ms |
15848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7516 KB |
Output is correct |
2 |
Correct |
39 ms |
13692 KB |
Output is correct |
3 |
Correct |
51 ms |
14160 KB |
Output is correct |
4 |
Correct |
70 ms |
16212 KB |
Output is correct |
5 |
Correct |
63 ms |
14676 KB |
Output is correct |
6 |
Correct |
75 ms |
16212 KB |
Output is correct |
7 |
Correct |
80 ms |
16236 KB |
Output is correct |
8 |
Correct |
55 ms |
14220 KB |
Output is correct |
9 |
Correct |
76 ms |
16204 KB |
Output is correct |
10 |
Correct |
193 ms |
44040 KB |
Output is correct |
11 |
Correct |
311 ms |
55124 KB |
Output is correct |
12 |
Correct |
294 ms |
55124 KB |
Output is correct |
13 |
Correct |
236 ms |
52816 KB |
Output is correct |
14 |
Correct |
262 ms |
55124 KB |
Output is correct |
15 |
Correct |
297 ms |
55124 KB |
Output is correct |
16 |
Correct |
169 ms |
44368 KB |
Output is correct |
17 |
Correct |
266 ms |
55124 KB |
Output is correct |
18 |
Correct |
106 ms |
45192 KB |
Output is correct |
19 |
Correct |
97 ms |
44884 KB |
Output is correct |
20 |
Correct |
120 ms |
45016 KB |
Output is correct |
21 |
Correct |
122 ms |
45172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7516 KB |
Output is correct |
2 |
Correct |
39 ms |
13692 KB |
Output is correct |
3 |
Correct |
51 ms |
14160 KB |
Output is correct |
4 |
Correct |
70 ms |
16212 KB |
Output is correct |
5 |
Correct |
63 ms |
14676 KB |
Output is correct |
6 |
Correct |
75 ms |
16212 KB |
Output is correct |
7 |
Correct |
80 ms |
16236 KB |
Output is correct |
8 |
Correct |
55 ms |
14220 KB |
Output is correct |
9 |
Correct |
76 ms |
16204 KB |
Output is correct |
10 |
Correct |
4 ms |
7516 KB |
Output is correct |
11 |
Correct |
70 ms |
15680 KB |
Output is correct |
12 |
Correct |
74 ms |
15436 KB |
Output is correct |
13 |
Correct |
74 ms |
15680 KB |
Output is correct |
14 |
Correct |
73 ms |
15748 KB |
Output is correct |
15 |
Correct |
69 ms |
15696 KB |
Output is correct |
16 |
Correct |
44 ms |
13904 KB |
Output is correct |
17 |
Correct |
77 ms |
16180 KB |
Output is correct |
18 |
Correct |
69 ms |
15912 KB |
Output is correct |
19 |
Correct |
78 ms |
16084 KB |
Output is correct |
20 |
Correct |
69 ms |
15848 KB |
Output is correct |
21 |
Correct |
193 ms |
44040 KB |
Output is correct |
22 |
Correct |
311 ms |
55124 KB |
Output is correct |
23 |
Correct |
294 ms |
55124 KB |
Output is correct |
24 |
Correct |
236 ms |
52816 KB |
Output is correct |
25 |
Correct |
262 ms |
55124 KB |
Output is correct |
26 |
Correct |
297 ms |
55124 KB |
Output is correct |
27 |
Correct |
169 ms |
44368 KB |
Output is correct |
28 |
Correct |
266 ms |
55124 KB |
Output is correct |
29 |
Correct |
106 ms |
45192 KB |
Output is correct |
30 |
Correct |
97 ms |
44884 KB |
Output is correct |
31 |
Correct |
120 ms |
45016 KB |
Output is correct |
32 |
Correct |
122 ms |
45172 KB |
Output is correct |
33 |
Correct |
247 ms |
48720 KB |
Output is correct |
34 |
Correct |
243 ms |
48724 KB |
Output is correct |
35 |
Correct |
205 ms |
45308 KB |
Output is correct |
36 |
Correct |
221 ms |
45316 KB |
Output is correct |
37 |
Correct |
236 ms |
46420 KB |
Output is correct |
38 |
Correct |
195 ms |
42620 KB |
Output is correct |
39 |
Correct |
258 ms |
52132 KB |
Output is correct |
40 |
Correct |
207 ms |
35820 KB |
Output is correct |
41 |
Correct |
185 ms |
38992 KB |
Output is correct |
42 |
Correct |
265 ms |
48976 KB |
Output is correct |
43 |
Correct |
189 ms |
45136 KB |
Output is correct |
44 |
Correct |
236 ms |
46676 KB |
Output is correct |
45 |
Correct |
106 ms |
41384 KB |
Output is correct |
46 |
Correct |
102 ms |
40788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7512 KB |
Output is correct |
2 |
Correct |
4 ms |
7516 KB |
Output is correct |
3 |
Correct |
95 ms |
40216 KB |
Output is correct |
4 |
Correct |
98 ms |
41040 KB |
Output is correct |
5 |
Correct |
100 ms |
41252 KB |
Output is correct |
6 |
Correct |
102 ms |
41004 KB |
Output is correct |
7 |
Correct |
102 ms |
41300 KB |
Output is correct |
8 |
Correct |
99 ms |
41300 KB |
Output is correct |
9 |
Correct |
51 ms |
40784 KB |
Output is correct |
10 |
Correct |
51 ms |
39880 KB |
Output is correct |
11 |
Correct |
108 ms |
46400 KB |
Output is correct |
12 |
Correct |
107 ms |
43840 KB |
Output is correct |
13 |
Correct |
103 ms |
41836 KB |
Output is correct |
14 |
Correct |
67 ms |
41132 KB |
Output is correct |
15 |
Correct |
68 ms |
41172 KB |
Output is correct |
16 |
Correct |
72 ms |
41460 KB |
Output is correct |
17 |
Correct |
76 ms |
41004 KB |
Output is correct |
18 |
Correct |
93 ms |
40776 KB |
Output is correct |
19 |
Correct |
64 ms |
41192 KB |
Output is correct |
20 |
Correct |
57 ms |
46740 KB |
Output is correct |
21 |
Correct |
74 ms |
45652 KB |
Output is correct |
22 |
Correct |
54 ms |
44116 KB |
Output is correct |
23 |
Correct |
67 ms |
43092 KB |
Output is correct |
24 |
Correct |
68 ms |
43016 KB |
Output is correct |
25 |
Correct |
56 ms |
43456 KB |
Output is correct |
26 |
Correct |
70 ms |
42936 KB |
Output is correct |
27 |
Correct |
107 ms |
45648 KB |
Output is correct |
28 |
Correct |
110 ms |
46324 KB |
Output is correct |
29 |
Correct |
110 ms |
46224 KB |
Output is correct |
30 |
Correct |
68 ms |
41164 KB |
Output is correct |
31 |
Correct |
50 ms |
41420 KB |
Output is correct |
32 |
Correct |
78 ms |
41828 KB |
Output is correct |
33 |
Correct |
58 ms |
41084 KB |
Output is correct |
34 |
Correct |
66 ms |
41552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7516 KB |
Output is correct |
2 |
Correct |
39 ms |
13692 KB |
Output is correct |
3 |
Correct |
51 ms |
14160 KB |
Output is correct |
4 |
Correct |
70 ms |
16212 KB |
Output is correct |
5 |
Correct |
63 ms |
14676 KB |
Output is correct |
6 |
Correct |
75 ms |
16212 KB |
Output is correct |
7 |
Correct |
80 ms |
16236 KB |
Output is correct |
8 |
Correct |
55 ms |
14220 KB |
Output is correct |
9 |
Correct |
76 ms |
16204 KB |
Output is correct |
10 |
Correct |
4 ms |
7516 KB |
Output is correct |
11 |
Correct |
70 ms |
15680 KB |
Output is correct |
12 |
Correct |
74 ms |
15436 KB |
Output is correct |
13 |
Correct |
74 ms |
15680 KB |
Output is correct |
14 |
Correct |
73 ms |
15748 KB |
Output is correct |
15 |
Correct |
69 ms |
15696 KB |
Output is correct |
16 |
Correct |
44 ms |
13904 KB |
Output is correct |
17 |
Correct |
77 ms |
16180 KB |
Output is correct |
18 |
Correct |
69 ms |
15912 KB |
Output is correct |
19 |
Correct |
78 ms |
16084 KB |
Output is correct |
20 |
Correct |
69 ms |
15848 KB |
Output is correct |
21 |
Correct |
193 ms |
44040 KB |
Output is correct |
22 |
Correct |
311 ms |
55124 KB |
Output is correct |
23 |
Correct |
294 ms |
55124 KB |
Output is correct |
24 |
Correct |
236 ms |
52816 KB |
Output is correct |
25 |
Correct |
262 ms |
55124 KB |
Output is correct |
26 |
Correct |
297 ms |
55124 KB |
Output is correct |
27 |
Correct |
169 ms |
44368 KB |
Output is correct |
28 |
Correct |
266 ms |
55124 KB |
Output is correct |
29 |
Correct |
106 ms |
45192 KB |
Output is correct |
30 |
Correct |
97 ms |
44884 KB |
Output is correct |
31 |
Correct |
120 ms |
45016 KB |
Output is correct |
32 |
Correct |
122 ms |
45172 KB |
Output is correct |
33 |
Correct |
247 ms |
48720 KB |
Output is correct |
34 |
Correct |
243 ms |
48724 KB |
Output is correct |
35 |
Correct |
205 ms |
45308 KB |
Output is correct |
36 |
Correct |
221 ms |
45316 KB |
Output is correct |
37 |
Correct |
236 ms |
46420 KB |
Output is correct |
38 |
Correct |
195 ms |
42620 KB |
Output is correct |
39 |
Correct |
258 ms |
52132 KB |
Output is correct |
40 |
Correct |
207 ms |
35820 KB |
Output is correct |
41 |
Correct |
185 ms |
38992 KB |
Output is correct |
42 |
Correct |
265 ms |
48976 KB |
Output is correct |
43 |
Correct |
189 ms |
45136 KB |
Output is correct |
44 |
Correct |
236 ms |
46676 KB |
Output is correct |
45 |
Correct |
106 ms |
41384 KB |
Output is correct |
46 |
Correct |
102 ms |
40788 KB |
Output is correct |
47 |
Correct |
4 ms |
7512 KB |
Output is correct |
48 |
Correct |
4 ms |
7516 KB |
Output is correct |
49 |
Correct |
95 ms |
40216 KB |
Output is correct |
50 |
Correct |
98 ms |
41040 KB |
Output is correct |
51 |
Correct |
100 ms |
41252 KB |
Output is correct |
52 |
Correct |
102 ms |
41004 KB |
Output is correct |
53 |
Correct |
102 ms |
41300 KB |
Output is correct |
54 |
Correct |
99 ms |
41300 KB |
Output is correct |
55 |
Correct |
51 ms |
40784 KB |
Output is correct |
56 |
Correct |
51 ms |
39880 KB |
Output is correct |
57 |
Correct |
108 ms |
46400 KB |
Output is correct |
58 |
Correct |
107 ms |
43840 KB |
Output is correct |
59 |
Correct |
103 ms |
41836 KB |
Output is correct |
60 |
Correct |
67 ms |
41132 KB |
Output is correct |
61 |
Correct |
68 ms |
41172 KB |
Output is correct |
62 |
Correct |
72 ms |
41460 KB |
Output is correct |
63 |
Correct |
76 ms |
41004 KB |
Output is correct |
64 |
Correct |
93 ms |
40776 KB |
Output is correct |
65 |
Correct |
64 ms |
41192 KB |
Output is correct |
66 |
Correct |
57 ms |
46740 KB |
Output is correct |
67 |
Correct |
74 ms |
45652 KB |
Output is correct |
68 |
Correct |
54 ms |
44116 KB |
Output is correct |
69 |
Correct |
67 ms |
43092 KB |
Output is correct |
70 |
Correct |
68 ms |
43016 KB |
Output is correct |
71 |
Correct |
56 ms |
43456 KB |
Output is correct |
72 |
Correct |
70 ms |
42936 KB |
Output is correct |
73 |
Correct |
107 ms |
45648 KB |
Output is correct |
74 |
Correct |
110 ms |
46324 KB |
Output is correct |
75 |
Correct |
110 ms |
46224 KB |
Output is correct |
76 |
Correct |
68 ms |
41164 KB |
Output is correct |
77 |
Correct |
50 ms |
41420 KB |
Output is correct |
78 |
Correct |
78 ms |
41828 KB |
Output is correct |
79 |
Correct |
58 ms |
41084 KB |
Output is correct |
80 |
Correct |
66 ms |
41552 KB |
Output is correct |
81 |
Correct |
192 ms |
54564 KB |
Output is correct |
82 |
Correct |
191 ms |
54620 KB |
Output is correct |
83 |
Correct |
191 ms |
54288 KB |
Output is correct |
84 |
Correct |
191 ms |
54584 KB |
Output is correct |
85 |
Correct |
186 ms |
54344 KB |
Output is correct |
86 |
Correct |
191 ms |
55120 KB |
Output is correct |
87 |
Correct |
108 ms |
52560 KB |
Output is correct |
88 |
Correct |
237 ms |
54868 KB |
Output is correct |
89 |
Correct |
229 ms |
54864 KB |
Output is correct |
90 |
Correct |
216 ms |
55124 KB |
Output is correct |
91 |
Correct |
83 ms |
49528 KB |
Output is correct |
92 |
Correct |
150 ms |
51544 KB |
Output is correct |
93 |
Correct |
162 ms |
51136 KB |
Output is correct |
94 |
Correct |
165 ms |
51792 KB |
Output is correct |
95 |
Correct |
170 ms |
51164 KB |
Output is correct |
96 |
Correct |
172 ms |
51796 KB |
Output is correct |
97 |
Correct |
122 ms |
54352 KB |
Output is correct |
98 |
Correct |
104 ms |
60500 KB |
Output is correct |
99 |
Correct |
151 ms |
57940 KB |
Output is correct |
100 |
Correct |
100 ms |
59220 KB |
Output is correct |
101 |
Correct |
134 ms |
56092 KB |
Output is correct |
102 |
Correct |
138 ms |
54588 KB |
Output is correct |
103 |
Correct |
104 ms |
56120 KB |
Output is correct |
104 |
Correct |
166 ms |
54412 KB |
Output is correct |
105 |
Correct |
123 ms |
49868 KB |
Output is correct |
106 |
Correct |
93 ms |
53204 KB |
Output is correct |
107 |
Correct |
206 ms |
54100 KB |
Output is correct |
108 |
Correct |
125 ms |
53200 KB |
Output is correct |
109 |
Correct |
124 ms |
54344 KB |
Output is correct |