#line 1 "Main.cpp" #include #include #include #include #include #include #line 2 "nachia\\misc\\sorting.hpp" #line 4 "nachia\\misc\\sorting.hpp" #include #line 6 "nachia\\misc\\sorting.hpp" namespace nachia{ template void EnsurePermutation(Iterator first, Iterator last){ int n = std::distance(first, last); std::vector vis(n, 0); for(Iterator i=first; i!=last; i++){ assert(0 <= *i); assert(*i < n); assert(vis[*i] == 0); vis[*i] = 1; } } template std::vector Permute( const std::vector& src, const std::vector& perm ){ const bool DEBUG = true; int n = src.size(); if constexpr (DEBUG){ assert(perm.size() == src.size()); EnsurePermutation(perm.begin(), perm.end()); } std::vector res; res.reserve(n); for(int i=0; i std::vector& PermuteInPlace( std::vector& src, std::vector perm ){ const bool DEBUG = true; int n = src.size(); if constexpr (DEBUG){ assert(perm.size() == src.size()); EnsurePermutation(perm.begin(), perm.end()); } for(int s=0; s BucketSortPermutation( std::vector::const_iterator first, std::vector::const_iterator last, int maxVal ){ const bool DEBUG = true; int n = last - first; if constexpr (DEBUG){ for(auto itr = first; itr != last; itr++){ assert(0 <= *itr); assert(*itr <= maxVal); } } std::vector cnt(maxVal+2); std::vector res(n); for(int i=0; i std::vector BucketSortPermutation( typename std::vector::const_iterator first, typename std::vector::const_iterator last, const Mapping& f, int maxVal ){ std::vector buf(last - first); auto bufitr = buf.begin(); for(auto itr = first; itr != last; itr++, bufitr++) *bufitr = f(*itr); return BucketSortPermutation(buf.begin(), buf.end(), maxVal); } // stable sort template std::vector BucketSort( std::vector src, const Mapping& f, int maxVal ){ return PermuteInPlace(src, BucketSortPermutation(src.begin(), src.end(), f, maxVal)); } // stable sort template std::vector BucketSortPermutation( Iter first, Iter last, const Mapping& f, int maxVal ){ std::vector buf(std::distance(first, last)); auto bufitr = buf.begin(); for(auto itr = first; itr != last; itr++, bufitr++) *bufitr = f(*itr); return BucketSortPermutation(buf.begin(), buf.end(), maxVal); } template void PermuteInPlace( Iter srcFirst, Iter srcLast, std::vector perm ){ const bool DEBUG = true; int n = std::distance(srcFirst, srcLast); if constexpr (DEBUG){ assert(perm.size() == (size_t)n); EnsurePermutation(perm.begin(), perm.end()); } for(int s=0; s void BucketSort( Iter destFirst, Iter destLast, const Mapping& f, int maxVal ){ PermuteInPlace(destFirst, destLast, BucketSortPermutation(destFirst, destLast, f, maxVal)); } } #line 8 "Main.cpp" using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; int main(){ struct Range { int l, r, i; }; int N, Q; cin >> N >> Q; vector A(N); rep(i,N) cin >> A[i]; vector G(Q); int sqrtN = 1; while(sqrtN * sqrtN <= N) sqrtN++; rep(i,Q){ G[i].i = i+1; cin >> G[i].l >> G[i].r; G[i].l--; } G = nachia::BucketSort(G, [&](Range g){ return g.r%2 ? g.l%sqrtN : sqrtN-g.l%sqrtN; }, sqrtN); G = nachia::BucketSort(G, [&](Range g){ return g.l/sqrtN%2 ? g.r%sqrtN : sqrtN-g.r%sqrtN; }, sqrtN); G = nachia::BucketSort(G, [&](Range g){ return g.r/sqrtN%2 ? g.l/sqrtN : sqrtN-g.l/sqrtN; }, sqrtN); G = nachia::BucketSort(G, [&](Range g){ return g.r/sqrtN; }, sqrtN); rep(i,Q){ if(i) cout << ' '; cout << G[i].i; } cout << '\n'; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;