/* #pragma GCC optimize("O3,unroll-loops") */ /* #pragma GCC optimize("Ofast,unroll-loops") */ /* #pragma GCC target("avx2,popcnt") */ #include #include #include #define pb push_back #define mp make_pair #define all(a) begin(a),end(a) #define FOR(x,val,to) for(int x=(val);x pi; typedef std::vector vi; typedef std::vector vs; typedef int64_t ll; typedef uint64_t ull; #define umap unordered_map #define uset unordered_set using namespace std; using namespace __gnu_pbds; #ifdef ONLINE_JUDGE #define whatis(x) ; #endif #ifdef _WIN32 #define getchar_unlocked() _getchar_nolock() #define _CRT_DISABLE_PERFCRIT_LOCKS #endif template ostream& operator<<(ostream &os, pair P) { os<<"("< ostream& operator<<(ostream &os, map P) { for(auto const &vv: P)os<<"("< ostream& operator<<(ostream &os, set V) { os<<"[";for(auto const &vv:V)os< ostream& operator<<(ostream &os, vector V) { os<<"[";for(auto const &vv:V)os<32){str+=cur;cur=getchar_unlocked();}} template inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); } template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound template using ordered_map = tree, rb_tree_tag, tree_order_statistics_node_update>; /* #define N 1000001 */ #define N 200001 //Lazy Propagation on Increment Modifications, Sum queries /* // ll? */ #define int ll /* #define N 10000 */ int t[N << 2]; int lazy[N << 2]; void build(int v, int tl, int tr, int arr[]){ if(tl == tr){ t[v] = arr[tl]; return; } int tm = (tl+tr)>>1; build(v<<1,tl,tm,arr); build(v<<1|1,tm+1,tr,arr); t[v] = t[v<<1] + t[v<<1|1]; } void push(int v, int tl, int tr){ if(lazy[v]){ int tm = (tl+tr)>>1; t[v<<1] += (tm-tl+1)*lazy[v]; t[v<<1|1] += (tr-tm)*lazy[v]; lazy[v<<1] += lazy[v]; lazy[v<<1|1] += lazy[v]; lazy[v] = 0; } } void modify(int v, int tl, int tr, int l, int r, int val){ if(l > r) return; if(tl == l && tr == r){ lazy[v] += val; t[v] += val*(tr-tl+1); return; } push(v,tl,tr); int tm = (tl+tr)>>1; modify(v << 1,tl,tm,l,min(tm,r),val); modify(v << 1 | 1,tm+1,tr,max(l,tm+1),r,val); t[v] = t[v<<1] + t[v<<1|1]; } int query(int v, int tl, int tr, int l, int r){ /* whatis(mp(tl,tr)) */ /* whatis(t[v]) */ if(l > r) return 0; if(tl == l && tr == r){ return t[v]; } push(v,tl,tr); int tm = (tl+tr)>>1; return query(v<<1,tl,tm,l,min(tm,r)) + query(v<<1|1,tm+1,tr,max(l,tm+1),r); } namespace arek { // from https://codeforces.com/blog/entry/75885 struct SplayTree { struct Node { int ch[2] = {0, 0}, p = 0; long long self = 0, path = 0; // Path aggregates long long sub = 0, vir = 0; // Subtree aggregates bool flip = 0; // Lazy tags }; vector T; SplayTree(int n) : T(n + 1) {} void push(int x) { if (!x || !T[x].flip) return; int l = T[x].ch[0], r = T[x].ch[1]; T[l].flip ^= 1, T[r].flip ^= 1; swap(T[x].ch[0], T[x].ch[1]); T[x].flip = 0; } void pull(int x) { int l = T[x].ch[0], r = T[x].ch[1]; push(l); push(r); T[x].path = T[l].path + T[x].self + T[r].path; T[x].sub = T[x].vir + T[l].sub + T[r].sub + T[x].self; } void set(int x, int d, int y) { T[x].ch[d] = y; T[y].p = x; pull(x); } void splay(int x) { auto dir = [&](int x) { int p = T[x].p; if (!p) return -1; return T[p].ch[0] == x ? 0 : T[p].ch[1] == x ? 1 : -1; }; auto rotate = [&](int x) { int y = T[x].p, z = T[y].p, dx = dir(x), dy = dir(y); set(y, dx, T[x].ch[!dx]); set(x, !dx, y); if (~dy) set(z, dy, x); T[x].p = z; }; for (push(x); ~dir(x); ) { int y = T[x].p, z = T[y].p; push(z); push(y); push(x); int dx = dir(x), dy = dir(y); if (~dy) rotate(dx != dy ? x : y); rotate(x); } } }; struct LinkCut : SplayTree { LinkCut(int n) : SplayTree(n) {} int access(int x) { int u = x, v = 0; for (; u; v = u, u = T[u].p) { splay(u); int& ov = T[u].ch[1]; T[u].vir += T[ov].sub; T[u].vir -= T[v].sub; ov = v; pull(u); } return splay(x), v; } void reroot(int x) { access(x); T[x].flip ^= 1; push(x); } void Link(int u, int v) { reroot(u); access(v); T[v].vir += T[u].sub; T[u].p = v; pull(v); } void Cut(int u, int v) { reroot(u); access(v); T[v].ch[0] = T[u].p = 0; pull(v); } // Rooted tree LCA. Returns 0 if u and v arent connected. int LCA(int u, int v) { if (u == v) return u; access(u); int ret = access(v); return T[u].p ? ret : 0; } // Query subtree of u where v is outside the subtree. long long Subtree(int u, int v) { reroot(v); access(u); return T[u].vir + T[u].self; } // Query path [u..v] long long Path(int u, int v) { reroot(u); access(v); return T[v].path; } // Update vertex u with value v void Update(int u, long long v) { access(u); T[u].self = v; pull(u); } }; const int maxn = 1e5 + 10; vector > eds; void add_edge(int a, int b) { eds.push_back({a, b}); } vector calc() { int n = eds.size(); vector ans(n); LinkCut lin(maxn); int wsk = 0; for (int i = 0; i < n; ++i) { while (wsk < n) { int a = eds[wsk].first; int b = eds[wsk].second; if (lin.LCA(a, b) == 0) { lin.Link(a, b); } else { break; } ++wsk; } ans[i] = wsk - 1; lin.Cut(eds[i].first, eds[i].second); } return ans; } } /* int main(){ */ int32_t main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n,m; sc(n,m); int f,s; pi wh[m]; /* while(m--){ */ FOR(i,0,m){ sc(f,s); /* wh[i] = {f - 1, s - 1}; */ /* arek::add_edge(f - 1, s - 1); */ arek::add_edge(f, s); } /* vi vec = arek::calc(); */ vector tab = arek::calc(); /* reverse(wh, wh + m); */ // jednak nie. int q; sc(q); int res[q]; array qus[q]; FOR(i,0,q){ sc(qus[i][0], qus[i][1]); --qus[i][0]; --qus[i][1]; qus[i][2] = i; } sort(qus, qus + q); int it = m; /* int tab[m]; // od beg inc -> ile max razem z tym. */ int it2 = -1; /* FOR(i,0,wh){ */ /* // it2 mxe? */ /* while(it + 1 < m && sth){ */ /* ++it; */ /* } */ /* tab[i] = it2 - i + 1; */ /* // rem_edge */ /* } */ // reverse? // -> jednak nie. /* FOR(i,0,m) */ /* whatis(tab[i]) */ FOR(i,0,m){ /* tab[i] = tab[i] - i + 1; */ /* whatis(tab[i]) */ tab[i] = tab[i] - i + 1; /* whatis(tab[i]) */ } for(int t = q - 1; t >= 0; --t){ while(qus[t][0] < it){ /* while(it > 0 && qus[t][0] < it){ */ /* modify(1,0,m-1,it,it + tab[it], 1); */ --it; assert(it >= 0); assert(it + tab[it] - 1 < m); modify(1,0,m-1,it,it + tab[it] - 1, 1); /* --it; */ } res[qus[t][2]] = query(1,0,m-1,qus[t][0], qus[t][1]); } FORR(i,res) cout << i << '\n'; }