#include <bits/stdc++.h>
#define MAX 300001
#define INF INT_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define gett(x,m) get<m>(x)
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define sorta(a,sz) sort(a,a+sz)
#define inputar(a,b){\
for(int i=0;i<b;i++){\
cin >> a[i];\
}\
}
#define inputvec(a,b){\
for(int i=0;i<b;i++){\
ll num;\
cin >> num;\
a.pb(num);\
}\
}
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << "\n";\
}
#define outputvec(a){\
for(auto x:a){\
cout << x << " ";\
}\
cout << "\n";\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
ll n,q,t=1,m,n2,m2,k,cnt=0,a[MAX],b[MAX],d[MAX],d2[MAX],x,y,z,x2,y2,z2,res1=0,cnt1,cnt2,cnt3;
ll c[1001][1001];
ll c2[1001][1001];
ll fact[MAX];
ll inv_fact[MAX];
//char c;
string str[MAX];
string s1,s2;
const int mod = 998244353;
ll modmul(ll x,ll y,ll md){
if(y==1){
return x;
}
if(y%2){
return (x+modmul(x,y-1,md))%md;
}
else{
return (modmul((x+x)%md,y/2,md))%md;
}
}
ll powmod(ll x,ll y,ll md){
x%=md;
if(x==0){
return 0;
}
ll res=1;
while(y){
if(y%2==1){
res*=x;
res%=md;
}
x*=x;
x%=md;
y/=2;
}
return res;
}
ll pow2(ll x,ll y){
if(x==0){
return 0;
}
ll res=1;
while(y>0){
if(y%2==1){
if(res>=1e18/x){
res=1e18;
}
else{
res*=x;
}
}
if(x>=1e18/x){
x=1e18;
}
else{
x*=x;
}
y/=2;
}
return res;
}
ll inv(ll n,ll md){
return powmod(n,md-2,md);
}
ll nCkm(ll n,ll k,ll md){
if(n-k<0){
return 0;
}
return fact[n]*inv_fact[k]%md*inv_fact[n-k]%md;
}
ll nCk(ll x,ll y){
if(x<y){
return 0;
}
ll res=1;
if(y>x-y){
for(int i=y+1;i<=x;i++){
res*=i;
}
for(int i=2;i<=x-y;i++){
res/=i;
}
}
else{
for(int i=x-y+1;i<=x;i++){
res*=i;
}
for(int i=2;i<=y;i++){
res/=i;
}
}
return res;
}
ll countbits(ll x){
ll cnt=0;
while(x){
cnt+=x&1;
x>>=1;
}
return cnt;
}
ll gcd(ll x,ll y){
if(y==0){
return x;
}
return gcd(y,x%y);
}
ll lcm(ll x,ll y){
return x*y/gcd(x,y);
}
bool alpha(char c1,char c2){
ll h1,h2;
if(c1>='a'){
h1=c1-'a';
}
else{
h1=c1-'A';
}
if(c2>='a'){
h2=c2-'a';
}
else{
h2=c2-'A';
}
if(h1==h2){
return c1<c2;
}
else{
return h1<h2;
}
}
ll dx[4]={1,0,0,-1};
ll dy[4]={0,1,-1,0};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
struct data{
ll dt[2][2];
};
data f(data x,data y){
data res;
res.dt[0][0]=res.dt[0][1]=res.dt[1][0]=res.dt[1][1]=INF;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
ll h=x.dt[i][0];
if(y.dt[1][j]<=INF-h){
h+=y.dt[1][j];
}
else{
h=INF;
}
res.dt[i][j]=min(res.dt[i][j],h);
h=x.dt[i][1];
if(y.dt[0][j]<=INF-h){
h+=y.dt[0][j];
}
else{
h=INF;
}
res.dt[i][j]=min(res.dt[i][j],h);
h=x.dt[i][1];
if(y.dt[1][j]<=INF-h){
h+=y.dt[1][j];
}
else{
h=INF;
}
res.dt[i][j]=min(res.dt[i][j],h);
}
}
return res;
}
struct segtree{
int size;
vector<data> dp;
void build(vector<ll> &a,int x,int lx,int rx){
if(rx-lx==1){
if(lx<(int)a.size()){
dp[x].dt[1][1]=a[lx];
dp[x].dt[0][0]=0;
}
return;
}
int m=(lx+rx)/2;
build(a,2*x+1,lx,m);
build(a,2*x+2,m,rx);
dp[x]=f(dp[2*x+1],dp[2*x+2]);
}
void build(vector<ll> &a){
build(a,0,0,size);
}
void init(int n){
size=1;
while(size<n){
size*=2;
}
data h;
h.dt[0][1]=h.dt[1][0]=INF;
h.dt[0][0]=h.dt[1][1]=INF;
dp.assign(2*size,h);
}
data get_res(int l,int r,int x,int lx,int rx){
if(lx>=r || rx<=l){
data h;
h.dt[0][1]=h.dt[1][0]=0;
h.dt[0][0]=h.dt[1][1]=INF;
return h;
}
if(lx>=l && rx<=r){
//cout << lx << " " << rx << " " << dp[x].dt[0][0] << " " << dp[x].dt[0][1] << " " << dp[x].dt[1][0] << " " << dp[x].dt[1][1] << "\n";
return dp[x];
}
data s1,s2;
int m=(lx+rx)/2;
s1=get_res(l,r,2*x+1,lx,m);
s2=get_res(l,r,2*x+2,m,rx);
data h=f(s1,s2);
//cout << lx << " " << rx << " " << h.dt[0][0] << " " << h.dt[0][1] << " " << h.dt[1][0] << " " << h.dt[1][1] << "\n";
return f(s1,s2);
}
data get_res(int l,int r){
return get_res(l,r,0,0,size);
}
void set(int i,int v,int x,int lx,int rx){
if(rx-lx==1){
dp[x].dt[1][1]=v;
dp[x].dt[0][0]=0;
return;
}
int m=(lx+rx)/2;
if(i<m){
set(i,v,2*x+1,lx,m);
}
else{
set(i,v,2*x+2,m,rx);
}
dp[x]=f(dp[2*x+1],dp[2*x+2]);
//cout << lx << " " << rx << " " << dp[x].dt[0][0] << " " << dp[x].dt[0][1] << " " << dp[x].dt[1][0] << " " << dp[x].dt[1][1] << "\n";
}
void set(int i,int v){
set(i,v,0,0,size);
}
};
struct dsu{
vector<ll> e;
void init(int n){
e.resize(n+1,-1);
}
int get(int x){
if(e[x]<0){
return x;
}
else{
return e[x]=get(e[x]);
}
}
int size(int x){
return -e[get(x)];
}
bool same_set(int x,int y){
return (get(x)==get(y));
}
bool unite(int x,int y){
x=get(x);
y=get(y);
if(x==y){
return false;
}
if(e[x]>e[y]){
swap(x,y);
}
e[x]+=e[y];
e[y]=x;
return true;
}
};
set<pll> st;
bool check(pll p){
if(st.size()>0){
auto it=st.upper_bound(mp(p.ss+1,0));
if(it!=st.begin()){
--it;
if((*it).ss>=p.ff){
return true;
}
}
}
return false;
}
void solve(){
cin >> n;
for(int i=1;i<=n;i++){
cin >> a[i] >> b[i];
}
ll j=1;
bool ok=false;
map<pll,int> c;
for(int i=1;i<=n;i++){
while(j<=n && !ok){
c[mp(a[j],b[j])]++;
ok=check(mp(a[j],b[j]));
st.ins(mp(a[j],b[j]));
j++;
}
if(!ok){
d[i]=INF;
}
else{
d[i]=j-1;
}
c[mp(a[i],b[i])]--;
if(c[mp(a[i],b[i])]==0){
st.erase(mp(a[i],b[i]));
if(check(mp(a[i],b[i]))){
auto it=st.upper_bound(mp(b[i]+1,0));
--it;
pll p=*it;
st.erase(p);
if(check(p)==false && c[p]==1){
ok=false;
}
st.ins(p);
}
}
else if(c[mp(a[i],b[i])]==1){
ok=false;
}
}
ll h,h1;
h=1;
h1=0;
while(h<n){
h*=2;
h1++;
}
ll sp[n+1][h1+1];
for(int i=1;i<=n;i++){
sp[i][0]=d[i];
}
for(int j=1;j<=h1;j++){
for(int i=1;i<=n;i++){
if(sp[i][j-1]==INF){
sp[i][j]=INF;
}
else{
sp[i][j]=sp[sp[i][j-1]][j-1];
}
}
}
cin >> q;
for(int i=0;i<q;i++){
cin >> x >> y;
ll res=1;
ll h2=h;
for(int j=h1;j>=0;j--){
if(sp[x][j]<=y){
x=sp[x][j];
res+=h2;
}
h2/=2;
}
cout << res << "\n";
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//USACO("poetry");
//freopen("input.txt","r",stdin);
//cin >> t;
ll cnt1=1;
while(t--){
solve();
cnt1++;
}
}
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
ifstream fin("template.in");
ofstream fout("template.out");
*/
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
ifstream fin("template.in");
ofstream fout("template.out");
*/
/*
ll b[51][51];
b[0][0] = 1;
for (int n = 1; n <= 50; ++n){
b[n][0] = b[n][n] = 1;
for (int k = 1; k < n; ++k)
b[n][k] = b[n - 1][k - 1] + b[n - 1][k];
}
*/
Compilation message
Main.cpp: In member function 'data segtree::get_res(int, int, int, int, int)':
Main.cpp:278:14: warning: variable 'h' set but not used [-Wunused-but-set-variable]
278 | data h=f(s1,s2);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
328 ms |
54612 KB |
Output is correct |
2 |
Correct |
283 ms |
57560 KB |
Output is correct |
3 |
Correct |
283 ms |
57772 KB |
Output is correct |
4 |
Correct |
301 ms |
58188 KB |
Output is correct |
5 |
Correct |
306 ms |
60500 KB |
Output is correct |
6 |
Correct |
95 ms |
44360 KB |
Output is correct |
7 |
Correct |
111 ms |
45004 KB |
Output is correct |
8 |
Correct |
111 ms |
45268 KB |
Output is correct |
9 |
Correct |
118 ms |
46216 KB |
Output is correct |
10 |
Correct |
123 ms |
44908 KB |
Output is correct |
11 |
Correct |
561 ms |
71080 KB |
Output is correct |
12 |
Correct |
558 ms |
67360 KB |
Output is correct |
13 |
Correct |
515 ms |
67184 KB |
Output is correct |
14 |
Correct |
510 ms |
60820 KB |
Output is correct |
15 |
Correct |
496 ms |
60820 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
101 ms |
46824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
10736 KB |
Output is correct |
2 |
Correct |
10 ms |
10700 KB |
Output is correct |
3 |
Correct |
11 ms |
10836 KB |
Output is correct |
4 |
Correct |
11 ms |
10860 KB |
Output is correct |
5 |
Correct |
10 ms |
10708 KB |
Output is correct |
6 |
Correct |
8 ms |
10508 KB |
Output is correct |
7 |
Correct |
8 ms |
10452 KB |
Output is correct |
8 |
Correct |
8 ms |
10452 KB |
Output is correct |
9 |
Correct |
10 ms |
10540 KB |
Output is correct |
10 |
Correct |
9 ms |
10452 KB |
Output is correct |
11 |
Correct |
12 ms |
11092 KB |
Output is correct |
12 |
Correct |
11 ms |
10964 KB |
Output is correct |
13 |
Correct |
11 ms |
11092 KB |
Output is correct |
14 |
Correct |
12 ms |
10836 KB |
Output is correct |
15 |
Correct |
13 ms |
10836 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
8 ms |
10452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
10736 KB |
Output is correct |
2 |
Correct |
10 ms |
10700 KB |
Output is correct |
3 |
Correct |
11 ms |
10836 KB |
Output is correct |
4 |
Correct |
11 ms |
10860 KB |
Output is correct |
5 |
Correct |
10 ms |
10708 KB |
Output is correct |
6 |
Correct |
8 ms |
10508 KB |
Output is correct |
7 |
Correct |
8 ms |
10452 KB |
Output is correct |
8 |
Correct |
8 ms |
10452 KB |
Output is correct |
9 |
Correct |
10 ms |
10540 KB |
Output is correct |
10 |
Correct |
9 ms |
10452 KB |
Output is correct |
11 |
Correct |
12 ms |
11092 KB |
Output is correct |
12 |
Correct |
11 ms |
10964 KB |
Output is correct |
13 |
Correct |
11 ms |
11092 KB |
Output is correct |
14 |
Correct |
12 ms |
10836 KB |
Output is correct |
15 |
Correct |
13 ms |
10836 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
8 ms |
10452 KB |
Output is correct |
18 |
Correct |
72 ms |
13444 KB |
Output is correct |
19 |
Correct |
65 ms |
13100 KB |
Output is correct |
20 |
Correct |
70 ms |
13516 KB |
Output is correct |
21 |
Correct |
70 ms |
13260 KB |
Output is correct |
22 |
Correct |
66 ms |
13320 KB |
Output is correct |
23 |
Correct |
63 ms |
12928 KB |
Output is correct |
24 |
Correct |
79 ms |
13036 KB |
Output is correct |
25 |
Correct |
71 ms |
13064 KB |
Output is correct |
26 |
Correct |
64 ms |
13156 KB |
Output is correct |
27 |
Correct |
60 ms |
12856 KB |
Output is correct |
28 |
Correct |
49 ms |
13096 KB |
Output is correct |
29 |
Correct |
55 ms |
13128 KB |
Output is correct |
30 |
Correct |
53 ms |
13216 KB |
Output is correct |
31 |
Correct |
50 ms |
13052 KB |
Output is correct |
32 |
Correct |
53 ms |
12984 KB |
Output is correct |
33 |
Correct |
5 ms |
9724 KB |
Output is correct |
34 |
Correct |
56 ms |
12872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
321 ms |
56652 KB |
Output is correct |
2 |
Correct |
282 ms |
59468 KB |
Output is correct |
3 |
Correct |
287 ms |
56012 KB |
Output is correct |
4 |
Correct |
266 ms |
55660 KB |
Output is correct |
5 |
Correct |
271 ms |
56908 KB |
Output is correct |
6 |
Correct |
93 ms |
44744 KB |
Output is correct |
7 |
Correct |
97 ms |
44380 KB |
Output is correct |
8 |
Correct |
104 ms |
44648 KB |
Output is correct |
9 |
Correct |
108 ms |
44372 KB |
Output is correct |
10 |
Correct |
137 ms |
47164 KB |
Output is correct |
11 |
Correct |
525 ms |
66820 KB |
Output is correct |
12 |
Correct |
633 ms |
72448 KB |
Output is correct |
13 |
Correct |
633 ms |
66676 KB |
Output is correct |
14 |
Correct |
471 ms |
59760 KB |
Output is correct |
15 |
Correct |
601 ms |
65312 KB |
Output is correct |
16 |
Correct |
5 ms |
9760 KB |
Output is correct |
17 |
Correct |
103 ms |
45232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
328 ms |
54612 KB |
Output is correct |
2 |
Correct |
283 ms |
57560 KB |
Output is correct |
3 |
Correct |
283 ms |
57772 KB |
Output is correct |
4 |
Correct |
301 ms |
58188 KB |
Output is correct |
5 |
Correct |
306 ms |
60500 KB |
Output is correct |
6 |
Correct |
95 ms |
44360 KB |
Output is correct |
7 |
Correct |
111 ms |
45004 KB |
Output is correct |
8 |
Correct |
111 ms |
45268 KB |
Output is correct |
9 |
Correct |
118 ms |
46216 KB |
Output is correct |
10 |
Correct |
123 ms |
44908 KB |
Output is correct |
11 |
Correct |
561 ms |
71080 KB |
Output is correct |
12 |
Correct |
558 ms |
67360 KB |
Output is correct |
13 |
Correct |
515 ms |
67184 KB |
Output is correct |
14 |
Correct |
510 ms |
60820 KB |
Output is correct |
15 |
Correct |
496 ms |
60820 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
101 ms |
46824 KB |
Output is correct |
18 |
Correct |
12 ms |
10736 KB |
Output is correct |
19 |
Correct |
10 ms |
10700 KB |
Output is correct |
20 |
Correct |
11 ms |
10836 KB |
Output is correct |
21 |
Correct |
11 ms |
10860 KB |
Output is correct |
22 |
Correct |
10 ms |
10708 KB |
Output is correct |
23 |
Correct |
8 ms |
10508 KB |
Output is correct |
24 |
Correct |
8 ms |
10452 KB |
Output is correct |
25 |
Correct |
8 ms |
10452 KB |
Output is correct |
26 |
Correct |
10 ms |
10540 KB |
Output is correct |
27 |
Correct |
9 ms |
10452 KB |
Output is correct |
28 |
Correct |
12 ms |
11092 KB |
Output is correct |
29 |
Correct |
11 ms |
10964 KB |
Output is correct |
30 |
Correct |
11 ms |
11092 KB |
Output is correct |
31 |
Correct |
12 ms |
10836 KB |
Output is correct |
32 |
Correct |
13 ms |
10836 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
8 ms |
10452 KB |
Output is correct |
35 |
Correct |
72 ms |
13444 KB |
Output is correct |
36 |
Correct |
65 ms |
13100 KB |
Output is correct |
37 |
Correct |
70 ms |
13516 KB |
Output is correct |
38 |
Correct |
70 ms |
13260 KB |
Output is correct |
39 |
Correct |
66 ms |
13320 KB |
Output is correct |
40 |
Correct |
63 ms |
12928 KB |
Output is correct |
41 |
Correct |
79 ms |
13036 KB |
Output is correct |
42 |
Correct |
71 ms |
13064 KB |
Output is correct |
43 |
Correct |
64 ms |
13156 KB |
Output is correct |
44 |
Correct |
60 ms |
12856 KB |
Output is correct |
45 |
Correct |
49 ms |
13096 KB |
Output is correct |
46 |
Correct |
55 ms |
13128 KB |
Output is correct |
47 |
Correct |
53 ms |
13216 KB |
Output is correct |
48 |
Correct |
50 ms |
13052 KB |
Output is correct |
49 |
Correct |
53 ms |
12984 KB |
Output is correct |
50 |
Correct |
5 ms |
9724 KB |
Output is correct |
51 |
Correct |
56 ms |
12872 KB |
Output is correct |
52 |
Correct |
321 ms |
56652 KB |
Output is correct |
53 |
Correct |
282 ms |
59468 KB |
Output is correct |
54 |
Correct |
287 ms |
56012 KB |
Output is correct |
55 |
Correct |
266 ms |
55660 KB |
Output is correct |
56 |
Correct |
271 ms |
56908 KB |
Output is correct |
57 |
Correct |
93 ms |
44744 KB |
Output is correct |
58 |
Correct |
97 ms |
44380 KB |
Output is correct |
59 |
Correct |
104 ms |
44648 KB |
Output is correct |
60 |
Correct |
108 ms |
44372 KB |
Output is correct |
61 |
Correct |
137 ms |
47164 KB |
Output is correct |
62 |
Correct |
525 ms |
66820 KB |
Output is correct |
63 |
Correct |
633 ms |
72448 KB |
Output is correct |
64 |
Correct |
633 ms |
66676 KB |
Output is correct |
65 |
Correct |
471 ms |
59760 KB |
Output is correct |
66 |
Correct |
601 ms |
65312 KB |
Output is correct |
67 |
Correct |
5 ms |
9760 KB |
Output is correct |
68 |
Correct |
103 ms |
45232 KB |
Output is correct |
69 |
Correct |
572 ms |
60484 KB |
Output is correct |
70 |
Correct |
593 ms |
63064 KB |
Output is correct |
71 |
Correct |
597 ms |
58932 KB |
Output is correct |
72 |
Correct |
574 ms |
61640 KB |
Output is correct |
73 |
Correct |
586 ms |
60860 KB |
Output is correct |
74 |
Correct |
397 ms |
51100 KB |
Output is correct |
75 |
Correct |
369 ms |
48236 KB |
Output is correct |
76 |
Correct |
385 ms |
51424 KB |
Output is correct |
77 |
Correct |
325 ms |
48140 KB |
Output is correct |
78 |
Correct |
335 ms |
48760 KB |
Output is correct |
79 |
Correct |
737 ms |
74916 KB |
Output is correct |
80 |
Correct |
664 ms |
70104 KB |
Output is correct |
81 |
Correct |
593 ms |
69708 KB |
Output is correct |
82 |
Correct |
589 ms |
62808 KB |
Output is correct |
83 |
Correct |
557 ms |
61900 KB |
Output is correct |
84 |
Correct |
5 ms |
9684 KB |
Output is correct |
85 |
Correct |
161 ms |
48200 KB |
Output is correct |