QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#346848#8279. Segment Treeucup-team087TL 611ms49940kbC++2024.2kb2024-03-09 03:03:312024-03-09 03:03:31

Judging History

你现在查看的是最新测评结果

  • [2024-03-09 03:03:31]
  • 评测
  • 测评结果:TL
  • 用时:611ms
  • 内存:49940kb
  • [2024-03-09 03:03:31]
  • 提交

answer

#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
//#define int ll

bool dbg=false;

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

ll rand_int(ll l, ll r) { //[l, r]
	//#ifdef LOCAL
	static mt19937_64 gen;
	/*#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif*/
	return uniform_int_distribution<ll>(l, r)(gen);
}

ll rand_int(ll k){ //[0,k)
	return rand_int(0,k-1);
}
string rand_string(int n,char lw,char up){
	string s(n,'?');
	rep(i,n)s[i]=rand_int(lw,up);
	return s;
}

int current_run_id,run_batch_size=1000;
int calc_random_limit(){
	return current_run_id/run_batch_size+1;
}
#ifndef int
void generate_single(int&a){
	a=rand_int(1,calc_random_limit());
}
#endif
void generate_single(ll&a){
	a=rand_int(1,calc_random_limit());
}
void generate_single(string&a){
	int n;generate_single(n);
	a=rand_string(n,'a','b');
}
//https://trap.jp/post/1224/
template<class... Args>
void input(Args&... a){
	if(dbg){
		(generate_single(a),...);
	}else{
		(cin >> ... >> a);
	}
}
#define INT(...) int __VA_ARGS__;input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;input(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;input(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;input(__VA_ARGS__)
#define overload3(a,b,c,d,...) d
#define VI2(name,size) vi name(size);rep(i,size)input(name[i]);
#define VI3(name,size,offset) vi name(size);rep(i,size)input(name[i]),name[i]+=offset;
#define VI(...) overload3(__VA_ARGS__,VI3,VI2)(__VA_ARGS__)

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

pi readpi(int off=0){
	int a,b;cin>>a>>b;
	return pi(a+off,b+off);
}

template<class t>
void print_single(t x,int suc=1){
	cout<<x;
	if(suc==1){
		if(dbg)cout<<endl;
		else cout<<"\n";
	}
	if(suc==2)
		cout<<" ";
}

template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
	print_single(p.a,2);
	print_single(p.b,suc);
}

template<class T>
void print_single(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print_single(v[i],i==int(v.size())-1?suc:2);
}

template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
	rep(i,v.size())
		print_single(v[i]+off,i==int(v.size())-1?suc:2);
}

template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
	rep(i,N)
		print_single(v[i],i==int(N)-1?suc:2);
}

template<class T>
void print(const T&t){
	print_single(t);
}

template<class T,class ...Args>
void print(const T&t,const Args&...args){
	print_single(t,2);
	print(args...);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

void YES(bool ex=true){
	cout<<"YES\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void NO(bool ex=true){
	cout<<"NO\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void Yes(bool ex=true){
	cout<<"Yes\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void No(bool ex=true){
	cout<<"No\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}*/
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
int popcount(ull t){
	return __builtin_popcountll(t);
}
int bitparity(ll t){
	return __builtin_parityll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}
ull umask(int i){
	return (ull(1)<<i)-1;
}
ll minp2(ll n){
	if(n<=1)return 1;
	else return ll(1)<<(topbit(n-1)+1);
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class S> void mkuni(S&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

template<class t> bool isuni(vc<t> v){
	int s=si(v);
	mkuni(v);
	return si(v)==s;
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class S,class u>
int lwb(const S&v,const u&a){
	return lower_bound(all(v),a)-v.bg;
}
template<class t,class u>
bool bis(const vc<t>&v,const u&a){
	return binary_search(all(v),a);
}

vvc<int> readGraph(int n,int m){
	vvc<int> g(n);
	rep(i,m){
		int a,b;
		cin>>a>>b;
		//sc.read(a,b);
		a--;b--;
		g[a].pb(b);
		g[b].pb(a);
	}
	return g;
}

vvc<int> readTree(int n){
	return readGraph(n,n-1);
}

template<class t>
vc<t> presum(const vc<t>&a){
	vc<t> s(si(a)+1);
	rep(i,si(a))s[i+1]=s[i]+a[i];
	return s;
}
vc<ll> presum(const vi&a){
	vc<ll> s(si(a)+1);
	rep(i,si(a))s[i+1]=s[i]+a[i];
	return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
	gnr(i,1,si(a))a[i]-=a[i-1];
	return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
	int n=si(a),m=si(a[0]);
	vvc<ll> b(n+1,vc<ll>(m+1));
	rep(i,n)rep(j,m)
		b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
	return b;
}

//verify してないや
void transvvc(int&n,int&m){
	swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
	assert(si(a)==n);
	vvc<t> b(m,vi(n));
	rep(i,n){
		assert(si(a[i])==m);
		rep(j,m)b[j][i]=a[i][j];
	}
	a.swap(b);
	transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
	swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
	assert(si(a)==n);
	vvc<t> b(m,vi(n));
	rep(i,n){
		assert(si(a[i])==m);
		rep(j,m)b[m-1-j][i]=a[i][j];
	}
	a.swap(b);
	rotvvc(n,m,args...);
}

//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
	int n=si(a);
	vi idx(n);iota(all(idx),0);
	sort(all(idx),[&](int i,int j){return a[i]<a[j];});
	return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
	int n=si(a);
	assert(si(idx)==n);
	vc<t> vs(n);
	rep(i,n)vs[i]=a[idx[i]];
	return vs;
}
//CF850C
vi invperm(const vi&p){
	int n=si(p);
	vi q(n);
	rep(i,n)q[p[i]]=i;
	return q;
}

template<class t,class s=t>
s SUM(const vc<t>&a){
	return accumulate(all(a),s(0));
}
template<class t,size_t K,class s=t>
s SUM(const array<t,K>&a){
	return accumulate(all(a),s(0));
}

template<class t>
t MAX(const vc<t>&a){
	return *max_element(all(a));
}

template<class t>
pair<t,int> MAXi(const vc<t>&a){
	auto itr=max_element(all(a));
	return mp(*itr,itr-a.bg);
}

template<class A>
auto MIN(const A&a){
	return *min_element(all(a));
}

template<class t>
pair<t,int> MINi(const vc<t>&a){
	auto itr=min_element(all(a));
	return mp(*itr,itr-a.bg);
}

vi vid(int n){
	vi res(n);iota(all(res),0);
	return res;
}

template<class S>
void soin(S&s){
	sort(all(s));
}

template<class S,class F>
void soin(S&s,F&&f){
	sort(all(s),forward<F>(f));
}

template<class S>
S soout(S s){
	soin(s);
	return s;
}

template<class S>
void rein(S&s){
	reverse(all(s));
}

template<class S>
S reout(S s){
	rein(s);
	return s;
}

template<class t,class u>
pair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){
	a.a+=b.a;a.b+=b.b;return a;}
template<class t,class u>
pair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){
	a.a-=b.a;a.b-=b.b;return a;}
template<class t,class u>
pair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a){return mp(-a.a,-a.b);}
template<class t,class u>
istream&operator>>(istream&is,pair<t,u>&a){
	return is>>a.a>>a.b;
}

template<class t>
t gpp(vc<t>&vs){
	assert(si(vs));
	t res=move(vs.back());
	vs.pop_back();
	return res;
}

template<class t,class u>
void pb(vc<t>&a,const vc<u>&b){
	a.insert(a.ed,all(b));
}

template<class t,class...Args>
vc<t> cat(vc<t> a,Args&&...b){
	(pb(a,forward<Args>(b)),...);
	return a;
}

template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
	for(auto&v:a)v+=x;
	return a;
}

template<class t,class u>
vc<t> operator+(vc<t> a,u x){
	return a+=x;
}

template<class t>
vc<t> operator+(const vc<t>&a,const vc<t>&b){
	vc<t> c(max(si(a),si(b)));
	rep(i,si(a))c[i]+=a[i];
	rep(i,si(b))c[i]+=b[i];
	return c;
}

template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
	for(auto&v:a)v-=x;
	return a;
}

template<class t,class u>
vc<t>& operator-(vc<t> a,u x){
	return a-=x;
}

template<class t,class u>
void remval(vc<t>&a,const u&v){
	a.erase(remove(all(a),v),a.ed);
}
//消した要素の個数を返してくれる
//UCUP 2-8-F
template<class t,class F>
int remif(vc<t>&a,F f){
	auto itr=remove_if(all(a),f);
	int res=a.ed-itr;
	a.erase(itr,a.ed);
	return res;
}

template<class VS,class u>
void fila(VS&vs,const u&a){
	fill(all(vs),a);
}

template<class t,class u>
int findid(const vc<t>&vs,const u&a){
	auto itr=find(all(vs),a);
	if(itr==vs.ed)return -1;
	else return itr-vs.bg;
}

template<class t>
void rtt(vc<t>&vs,int i){
	rotate(vs.bg,vs.bg+i,vs.ed);
}

//Multiuni2023-8 C
//f(lw)=false,...,f(n-1)=false,f(n)=true,...,f(up)=true,
//のときに n を返す
template<class F>
int find_min_true(int lw,int up,F f){
	while(up-lw>1){
		const int mid=(lw+up)/2;
		if(f(mid))up=mid;
		else lw=mid;
	}
	return up;
}
//f(lw)=true,f(up)=false
template<class F>
int find_max_true(int lw,int up,F f){
	while(up-lw>1){
		const int mid=(lw+up)/2;
		if(f(mid))lw=mid;
		else up=mid;
	}
	return lw;
}

//N() が単位元
//VERIFY: yosupo
//CF407E
template<class N>
struct segtree{
	vc<N> x;
	int n,s;
	segtree(){}
	template<class t>
	segtree(const vc<t>&a){
		n=a.size();
		s=1;
		while(s<n){s*=2;}
		x.resize(s*2);
		rep(i,n)
			x[s+i]=N(a[i]);
		gnr(i,1,s)
			x[i]=N::merge(x[i*2],x[i*2+1]);
	}
	//NOT Verified
	segtree(int nn){
		resize(nn);
	}
	void resize(int nn){
		n=nn;
		s=1;
		while(s<n){s*=2;}
		x.assign(s*2,N());
		gnr(i,1,s)
			x[i]=N::merge(x[i*2],x[i*2+1]);
	}
	template<class t>
	void load(const vc<t>&a){
		n=a.size();
		s=1;
		while(s<n){s*=2;}
		x.resize(s*2);
		rep(i,n)
			x[s+i]=N(a[i]);
		rng(i,n,s)
			x[s+i]=N();
		gnr(i,1,s)
			x[i]=N::merge(x[i*2],x[i*2+1]);
	}
	void clear(){
		rep(i,n)
			x[s+i]=N();
		gnr(i,1,s)
			x[i]=N::merge(x[i*2],x[i*2+1]);
	}
	N point_get(int i){
		assert(inc(0,i,n-1));
		return x[i+s];
	}
	void point_set(int i,const N&t){
		assert(inc(0,i,n-1));
		i+=s;
		x[i]=t;
		while(i>>=1)x[i]=N::merge(x[i*2],x[i*2+1]);
	}
	void point_merge(int i,const N&t){
		assert(inc(0,i,n-1));
		i+=s;
		x[i]=N::merge(x[i],t);
		while(i>>=1)x[i]=N::merge(x[i*2],x[i*2+1]);
	}
	template<class F,class...Args>
	void point_change(int i,F f,Args&&...args){
		assert(inc(0,i,n-1));
		i+=s;
		(x[i].*f)(forward<Args>(args)...);
		while(i>>=1)x[i]=N::merge(x[i*2],x[i*2+1]);
	}
	N composite(int b,int e){
		assert(0<=b&&b<=e&&e<=n);
		N lf,rt;
		for(int l=b+s,r=e+s;l<r;l>>=1,r>>=1){
			if (l&1){
				lf=N::merge(lf,x[l]);
				l++;
			}
			if (r&1){
				r--;
				rt=N::merge(x[r],rt);
			}
		}
		return N::merge(lf,rt);
	}
	N getall(){
		return x[1];
	}
	//UTPC2020E
	//n 超えるかもしれない
	template <class F,class... Args> 
	pair<int,N> max_right(int l,F f,Args&&... args){
		assert((N().*f)(forward<Args>(args)...));
		assert(0<=l&&l<=n);
		if(l==n)return mp(n,N());
		l+=s;
		
		N sm;
		assert((sm.*f)(forward<Args>(args)...));
		do {
			while (l % 2 == 0) l >>= 1;
			if (!(N::merge(sm,x[l]).*f)(forward<Args>(args)...)){
				while (l < s) {
					l = (2 * l);
					N tmp=N::merge(sm,x[l]);
					if ((tmp.*f)(forward<Args>(args)...)) {
						sm = tmp;
						l++;
					}
				}
				return mp(l - s,sm);
			}
			sm = N::merge(sm, x[l]);
			l++;
		} while ((l & -l) != l);
		return mp(n,sm);
	}
	//UCUP-2-22-K
	template <class F,class... Args> 
	pair<int,N> min_left_withinit(int r,N sm,F f,Args&&... args){
		assert((sm.*f)(forward<Args>(args)...));
		assert(0<=r&&r<=n);
        if(r==0)return mp(0,sm);
        r+=s;
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!(N::merge(x[r],sm).*f)(forward<Args>(args)...)) {
                while (r < s) {
                    r = (2 * r + 1);
                    N tmp=N::merge(x[r],sm);
                    if ((tmp.*f)(forward<Args>(args)...)) {
                        sm = tmp;
                        r--;
                    }
                }
                return mp(r + 1 - s,sm);
            }
            sm = N::merge(x[r], sm);
        } while ((r & -r) != r);
        return mp(0,sm);
    }
	//UTPC2020E
	template <class F,class... Args> 
	pair<int,N> min_left(int r,F f,Args&&... args){
		return min_left_withinit(r,N(),f,forward<Args>(args)...);
    }
    //行列とか乗せて必要なのはベクトルとの積,みたいなときに使える?
    //CF Goodbye 2016 E
    //CF 896 F
	template<class F,class T,class... Args>
	T accumulate(int l,int r,F f,T t,Args&&... args) {
		assert(0<=l&&l<=r&&r<=n);
		static int buf[2][30];
		int cnt[2]{};
		for(l+=s,r+=s;l<r;l>>=1,r>>=1){
			if(l&1)buf[0][cnt[0]++]=l;
			if(r&1)buf[1][cnt[1]++]=r-1;
			l++;
		}
		rep(i,cnt[0])t=(x[buf[0][i]].*f)(t,forward<Args>(args)...);
		per(i,cnt[1])t=(x[buf[1][i]].*f)(t,forward<Args>(args)...);
		return t;
	}
};


struct MaxNode{
	int v;
	MaxNode(int vv=-inf):v(vv){}
	static MaxNode merge(const MaxNode&a,const MaxNode&b){
		return MaxNode(max(a.v,b.v));
	}
	bool ok(int x){return v<x;}
};

//mint107 は verify してねえ
//#define DYNAMIC_MOD

struct modinfo{uint mod,root;
#ifdef DYNAMIC_MOD
constexpr modinfo(uint m,uint r):mod(m),root(r),im(0){set_mod(m);}
ull im;
constexpr void set_mod(uint m){
	mod=m;
	im=ull(-1)/m+1;
}
uint product(uint a,uint b)const{
	ull z=ull(a)*b;
	uint x=((unsigned __int128)z*im)>>64;
	uint v=uint(z)-x*mod;
	return v<mod?v:v+mod;
}
#endif
};
template<modinfo const&ref>
struct modular{
	static constexpr uint const &mod=ref.mod;
	static modular root(){return modular(ref.root);}
	uint v;
	//modular(initializer_list<uint>ls):v(*ls.bg){}
	modular(ll vv=0){s(vv%mod+mod);}
	modular& s(uint vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	modular operator-()const{return modular()-*this;}
	modular& operator+=(const modular&rhs){return s(v+rhs.v);}
	modular&operator-=(const modular&rhs){return s(v+mod-rhs.v);}
	modular&operator*=(const modular&rhs){
		#ifndef DYNAMIC_MOD
		v=ull(v)*rhs.v%mod;
		#else
		v=ref.product(v,rhs.v);
		#endif
		return *this;
	}
	modular&operator/=(const modular&rhs){return *this*=rhs.inv();}
	modular operator+(const modular&rhs)const{return modular(*this)+=rhs;}
	modular operator-(const modular&rhs)const{return modular(*this)-=rhs;}
	modular operator*(const modular&rhs)const{return modular(*this)*=rhs;}
	modular operator/(const modular&rhs)const{return modular(*this)/=rhs;}
	modular pow(ll n)const{
		if(n<0)return inv().pow(-n);
		modular res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modular inv()const{return pow(mod-2);}
	/*modular inv()const{
		int x,y;
		int g=extgcd<ll>(v,mod,x,y);
		assert(g==1);
		if(x<0)x+=mod;
		return modular(x);
	}*/
	friend modular operator+(ll x,const modular&y){
		return modular(x)+y;
	}
	friend modular operator-(ll x,const modular&y){
		return modular(x)-y;
	}
	friend modular operator*(ll x,const modular&y){
		return modular(x)*y;
	}
	friend modular operator/(ll x,const modular&y){
		return modular(x)/y;
	}
	friend ostream& operator<<(ostream&os,const modular&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,modular&m){
		ll x;is>>x;
		m=modular(x);
		return is;
	}
	bool operator<(const modular&r)const{return v<r.v;}
	bool operator==(const modular&r)const{return v==r.v;}
	bool operator!=(const modular&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};

#ifndef DYNAMIC_MOD
extern constexpr modinfo base{998244353,3};
//extern constexpr modinfo base{1000000007,0};
//extern constexpr modinfo base{2147483579,1689685080};//2^31 未満の最大の安全素数
//modinfo base{1,0};
#ifdef USE_GOOD_MOD
static_assert(base.mod==998244353);
#endif
#else
modinfo base(1,0);
extern constexpr modinfo base107(1000000007,0);
using mint107=modular<base107>;
#endif
using mint=modular<base>;

mint parity(int i){
	return i%2==0?1:-1;
}

#ifdef LOCAL
const int vmax=10010;
#else
const int vmax=(1<<21)+10;
#endif
mint fact[vmax],finv[vmax],invs[vmax];
void initfact(){
	fact[0]=1;
	rng(i,1,vmax){
		fact[i]=fact[i-1]*i;
	}
	finv[vmax-1]=fact[vmax-1].inv();
	for(int i=vmax-2;i>=0;i--){
		finv[i]=finv[i+1]*(i+1);
	}
	for(int i=vmax-1;i>=1;i--){
		invs[i]=finv[i]*fact[i-1];
	}
}
mint choose(int n,int k){
	return inc(0,k,n)?fact[n]*finv[n-k]*finv[k]:0;
}
mint binom(int a,int b){
	return 0<=a&&0<=b?fact[a+b]*finv[a]*finv[b]:0;
}
mint catalan(int n){
	return binom(n,n)-(n-1>=0?binom(n-1,n+1):0);
}
//対角線を超えず (x,y) に至る方法の数
mint catalan(int x,int y){
	assert(y<=x);
	return binom(x,y)-binom(x+1,y-1);
}
//y=x+c を超えず (x,y) に至る方法の数
mint catalan(int x,int y,int c){
	assert(y<=x+c);
	return binom(x,y)-binom(x+c+1,y-c-1);
}

/*
const int vmax=610;
mint fact[vmax+1],binbuf[vmax+1][vmax+1];
mint choose(int n,int k){
	return 0<=k&&k<=n?binbuf[n-k][k]:0;
}
mint binom(int a,int b){
	return 0<=a&&0<=b?binbuf[a][b]:0;
}
void initfact(int n){
	fact[0]=1;
	rep(i,n)fact[i+1]=fact[i]*(i+1);
	rep(i,n+1)rep(j,n+1){
		if(i==0&&j==0){
			binbuf[i][j]=1;
		}else{
			binbuf[i][j]=0;
			if(i)binbuf[i][j]+=binbuf[i-1][j];
			if(j)binbuf[i][j]+=binbuf[i][j-1];
		}
	}
}
*/

mint p2[vmax],p2inv[vmax];
void initp2(){
	p2[0]=1;
	rep(i,vmax-1)p2[i+1]=p2[i]*2;
	p2inv[vmax-1]=p2[vmax-1].inv();
	per(i,vmax-1)p2inv[i]=p2inv[i+1]*2;
}


//VERIFY: yosupo
//KUPC2017J
//AOJDSL1A
//without rank
struct unionfind{
	vi p,s;
	int c;
	unionfind(int n):p(n,-1),s(n,1),c(n){}
	void clear(){
		fill(all(p),-1);
		fill(all(s),1);
		c=si(p);
	}
	int find(int a){
		return p[a]==-1?a:(p[a]=find(p[a]));
	}
	//set b to a child of a
	bool unite(int a,int b){
		a=find(a);
		b=find(b);
		if(a==b)return false;
		p[b]=a;
		s[a]+=s[b];
		c--;
		return true;
	}
	bool same(int a,int b){
		return find(a)==find(b);
	}
	int sz(int a){
		return s[find(a)];
	}
};

void slv(){
	INT(n,rangenum);
	VI(cut,n-1);
	
	vi l2r(n+1,n+1),r2l(n+1,-1);
	{
		unionfind uf(n+1);
		vvc<int> pos(n+1);
		rep(i,rangenum){
			INT(l,r);
			pos[r].pb(l);
		}
		segtree<MaxNode> seg(n+1);
		rep(i,n+1)if(si(pos[i])){
			vi ls=pos[i];
			for(auto j:pos[i]){
				while(1){
					int x=seg.min_left(j+1,&MaxNode::ok,j).a-1;
					if(x<0)break;
					seg.point_set(x,-inf);
					ls.pb(x);
				}
			}
			for(auto v:ls)uf.unite(i,v);
			seg.point_set(MIN(ls),i);
		}
		
		vi last(n+1,-1);
		rep(i,n+1){
			int j=uf.find(i);
			if(last[j]!=-1){
				l2r[last[j]]=i;
				r2l[i]=last[j];
			}
			last[j]=i;
		}
	}
	dmp(l2r);
	dmp(r2l);
	
	vc<mint> c(n);
	int head=0;
	auto dfs=[&](auto self,int l,int r)->pair<mint,mint>{
		if(r-l==1){
			mint a=1,b=1;
			if(l2r[l]==r){
				l2r[l]=n+1;
				r2l[r]=-1;
			}else{
				c[l]=1;
			}
			return mp(a,b);
		}else{
			int m=cut[head++];
			auto [la,lb]=self(self,l,m);
			auto [ra,rb]=self(self,m,r);
			
			vi xs,ys;
			if(m-l<r-m){
				gnr(i,l,m)if(l2r[i]<=r){
					xs.pb(i);
					ys.pb(l2r[i]);
				}
			}else{
				rng(i,m+1,r+1)if(r2l[i]>=l){
					xs.pb(r2l[i]);
					ys.pb(i);
				}
			}
			for(auto i:xs)l2r[i]=n+1;
			for(auto j:ys)r2l[j]=-1;
			
			vc<mint> tmp(n);
			
			mint a,b;
			//aa
			a+=la*ra*2;
			//ab
			a+=la*rb;
			b+=la*rb;
			//ba
			a+=lb*ra;
			b+=lb*ra;
			//bb -> none
			xs.insert(xs.bg,m);
			ys.insert(ys.bg,m);
			//ac
			rng(j,ys.back(),r){
				tmp[j]+=la*c[j];
			}
			//ca
			rng(i,l,xs.back()){
				tmp[i]+=c[i]*ra;
			}
			//bc -> none
			//cb -> none
			{
				int xend=l;
				gnr(i,l,m+1)if(l2r[i]<=n||r2l[i]>=0){
					xend=i;
					break;
				}
				xs.pb(xend);
				int yend=r;
				rng(j,m,r+1)if(r2l[j]>=0||l2r[j]<=n){
					yend=j;
					break;
				}
				ys.pb(yend);
			}
			//cc
			rep(k,si(xs)-1){
				mint x=0,y=0;
				rng(i,xs[k+1],xs[k])x+=c[i];
				rng(i,ys[k],ys[k+1])y+=c[i];
				a+=x*y;
				b+=x*y;
				if(k==si(xs)-2){
					tmp[m]+=x*y;
				}
			}
			
			rng(i,l,r)c[i]=tmp[i];
			dmp2(l,r,a,b,vc<mint>(c.bg+l,c.bg+r));
			return mp(a,b);
		}
	};
	auto [a,b]=dfs(dfs,0,n);
	
	dmp(l2r);
	dmp(r2l);
	dmp2(a,b,c);
	
	mint ans=a+SUM(c);
	print(ans);
}

signed main(signed argc,char*argv[]){
	if(argc>1&&strcmp(argv[1],"D")==0)dbg=true;
	
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	
	if(dbg){
		while(1){
			if(current_run_id%run_batch_size==0){
				cerr<<"Current Run "<<current_run_id<<endl;
			}
			slv();
			current_run_id++;
		}
	}else{
		//int t;cin>>t;rep(_,t)
		slv();
	}
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 44624kb

input:

2 1
1
0 2

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 3ms
memory: 44600kb

input:

2 1
1
1 2

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 44516kb

input:

5 2
2 1 4 3
1 3
2 5

output:

193

result:

ok 1 number(s): "193"

Test #4:

score: 0
Accepted
time: 0ms
memory: 44664kb

input:

10 10
5 2 1 3 4 7 6 8 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10

output:

70848

result:

ok 1 number(s): "70848"

Test #5:

score: 0
Accepted
time: 6ms
memory: 44828kb

input:

2 2
1
0 1
0 2

output:

4

result:

ok 1 number(s): "4"

Test #6:

score: 0
Accepted
time: 0ms
memory: 44564kb

input:

3 3
1 2
0 1
0 2
0 3

output:

14

result:

ok 1 number(s): "14"

Test #7:

score: 0
Accepted
time: 4ms
memory: 44512kb

input:

4 4
1 2 3
0 1
0 2
0 3
0 4

output:

48

result:

ok 1 number(s): "48"

Test #8:

score: 0
Accepted
time: 0ms
memory: 44560kb

input:

5 5
3 1 2 4
0 1
0 2
0 3
0 4
0 5

output:

164

result:

ok 1 number(s): "164"

Test #9:

score: 0
Accepted
time: 0ms
memory: 44604kb

input:

6 6
4 2 1 3 5
0 1
0 2
0 3
0 4
0 5
0 6

output:

544

result:

ok 1 number(s): "544"

Test #10:

score: 0
Accepted
time: 0ms
memory: 44564kb

input:

7 7
3 2 1 5 4 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7

output:

1856

result:

ok 1 number(s): "1856"

Test #11:

score: 0
Accepted
time: 0ms
memory: 44568kb

input:

8 8
3 1 2 4 7 5 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8

output:

6528

result:

ok 1 number(s): "6528"

Test #12:

score: 0
Accepted
time: 0ms
memory: 44572kb

input:

9 9
3 1 2 4 7 6 5 8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9

output:

21520

result:

ok 1 number(s): "21520"

Test #13:

score: 0
Accepted
time: 4ms
memory: 44564kb

input:

10 10
8 2 1 3 4 6 5 7 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10

output:

71296

result:

ok 1 number(s): "71296"

Test #14:

score: 0
Accepted
time: 0ms
memory: 44604kb

input:

2 3
1
0 1
0 2
1 2

output:

4

result:

ok 1 number(s): "4"

Test #15:

score: 0
Accepted
time: 0ms
memory: 44568kb

input:

3 6
1 2
0 1
0 2
0 3
1 2
1 3
2 3

output:

14

result:

ok 1 number(s): "14"

Test #16:

score: 0
Accepted
time: 0ms
memory: 44860kb

input:

4 10
1 2 3
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 4

output:

48

result:

ok 1 number(s): "48"

Test #17:

score: 0
Accepted
time: 0ms
memory: 44564kb

input:

5 15
1 4 3 2
0 1
0 2
0 3
0 4
0 5
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

output:

164

result:

ok 1 number(s): "164"

Test #18:

score: 0
Accepted
time: 4ms
memory: 44568kb

input:

6 21
5 3 1 2 4
0 1
0 2
0 3
0 4
0 5
0 6
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6

output:

544

result:

ok 1 number(s): "544"

Test #19:

score: 0
Accepted
time: 0ms
memory: 44628kb

input:

7 28
4 1 2 3 6 5
0 1
0 2
0 3
0 4
0 5
0 6
0 7
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7

output:

1912

result:

ok 1 number(s): "1912"

Test #20:

score: 0
Accepted
time: 4ms
memory: 44860kb

input:

8 36
5 2 1 3 4 7 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
1 2
1 3
1 4
1 5
1 6
1 7
1 8
2 3
2 4
2 5
2 6
2 7
2 8
3 4
3 5
3 6
3 7
3 8
4 5
4 6
4 7
4 8
5 6
5 7
5 8
6 7
6 8
7 8

output:

6304

result:

ok 1 number(s): "6304"

Test #21:

score: 0
Accepted
time: 0ms
memory: 44832kb

input:

9 45
6 2 1 4 3 5 7 8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
2 3
2 4
2 5
2 6
2 7
2 8
2 9
3 4
3 5
3 6
3 7
3 8
3 9
4 5
4 6
4 7
4 8
4 9
5 6
5 7
5 8
5 9
6 7
6 8
6 9
7 8
7 9
8 9

output:

20736

result:

ok 1 number(s): "20736"

Test #22:

score: 0
Accepted
time: 0ms
memory: 44628kb

input:

10 55
6 3 2 1 4 5 8 7 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
3 4
3 5
3 6
3 7
3 8
3 9
3 10
4 5
4 6
4 7
4 8
4 9
4 10
5 6
5 7
5 8
5 9
5 10
6 7
6 8
6 9
6 10
7 8
7 9
7 10
8 9
8 10
9 10

output:

70784

result:

ok 1 number(s): "70784"

Test #23:

score: 0
Accepted
time: 0ms
memory: 44792kb

input:

2 1
1
0 2

output:

5

result:

ok 1 number(s): "5"

Test #24:

score: 0
Accepted
time: 4ms
memory: 44856kb

input:

3 1
2 1
2 3

output:

21

result:

ok 1 number(s): "21"

Test #25:

score: 0
Accepted
time: 0ms
memory: 44624kb

input:

4 1
2 1 3
0 1

output:

85

result:

ok 1 number(s): "85"

Test #26:

score: 0
Accepted
time: 0ms
memory: 44856kb

input:

5 1
4 1 3 2
0 5

output:

341

result:

ok 1 number(s): "341"

Test #27:

score: 0
Accepted
time: 0ms
memory: 44624kb

input:

6 1
5 1 2 3 4
0 2

output:

1260

result:

ok 1 number(s): "1260"

Test #28:

score: 0
Accepted
time: 3ms
memory: 44868kb

input:

7 1
2 1 6 4 3 5
3 4

output:

5545

result:

ok 1 number(s): "5545"

Test #29:

score: 0
Accepted
time: 0ms
memory: 44600kb

input:

8 1
5 4 2 1 3 6 7
4 7

output:

14745

result:

ok 1 number(s): "14745"

Test #30:

score: 0
Accepted
time: 4ms
memory: 44864kb

input:

9 1
3 2 1 8 7 6 4 5
3 6

output:

101031

result:

ok 1 number(s): "101031"

Test #31:

score: 0
Accepted
time: 4ms
memory: 44868kb

input:

10 1
7 4 2 1 3 5 6 9 8
9 10

output:

373889

result:

ok 1 number(s): "373889"

Test #32:

score: 0
Accepted
time: 0ms
memory: 44660kb

input:

10 2
1 9 8 5 4 3 2 7 6
1 8
2 5

output:

261049

result:

ok 1 number(s): "261049"

Test #33:

score: 0
Accepted
time: 4ms
memory: 44628kb

input:

10 3
5 1 4 3 2 7 6 9 8
1 8
3 9
6 8

output:

122329

result:

ok 1 number(s): "122329"

Test #34:

score: 0
Accepted
time: 0ms
memory: 44564kb

input:

10 4
4 1 2 3 8 6 5 7 9
0 8
0 10
1 2
4 7

output:

175630

result:

ok 1 number(s): "175630"

Test #35:

score: 0
Accepted
time: 0ms
memory: 44628kb

input:

10 5
3 1 2 8 6 5 4 7 9
3 8
5 6
6 7
7 8
8 9

output:

176820

result:

ok 1 number(s): "176820"

Test #36:

score: 0
Accepted
time: 0ms
memory: 44564kb

input:

10 6
7 6 4 1 3 2 5 9 8
0 2
2 3
2 4
3 5
5 10
6 10

output:

123102

result:

ok 1 number(s): "123102"

Test #37:

score: 0
Accepted
time: 4ms
memory: 44660kb

input:

10 7
6 1 3 2 5 4 8 7 9
2 5
2 7
2 10
3 10
4 7
5 10
8 10

output:

107712

result:

ok 1 number(s): "107712"

Test #38:

score: 0
Accepted
time: 0ms
memory: 44868kb

input:

10 8
1 3 2 7 4 6 5 9 8
0 2
1 7
1 10
2 9
3 7
4 6
4 8
5 7

output:

70272

result:

ok 1 number(s): "70272"

Test #39:

score: 0
Accepted
time: 5ms
memory: 44864kb

input:

10 9
4 1 2 3 5 8 7 6 9
0 7
0 10
1 7
1 9
1 10
3 6
4 5
4 7
6 10

output:

82208

result:

ok 1 number(s): "82208"

Test #40:

score: 0
Accepted
time: 5ms
memory: 44568kb

input:

10 10
6 4 2 1 3 5 8 7 9
0 4
1 3
1 9
2 5
2 9
2 10
3 8
4 5
4 8
4 9

output:

89920

result:

ok 1 number(s): "89920"

Test #41:

score: 0
Accepted
time: 6ms
memory: 44604kb

input:

10 15
6 2 1 3 5 4 8 7 9
0 3
0 10
1 5
2 4
2 10
3 6
3 9
4 6
5 7
5 8
6 7
6 9
6 10
8 10
9 10

output:

71168

result:

ok 1 number(s): "71168"

Test #42:

score: 0
Accepted
time: 0ms
memory: 44860kb

input:

10 20
3 1 2 8 6 5 4 7 9
0 4
0 5
0 10
1 2
1 5
1 6
1 9
2 4
2 10
3 5
3 6
3 7
3 9
4 8
4 10
5 9
5 10
6 7
8 9
8 10

output:

70336

result:

ok 1 number(s): "70336"

Test #43:

score: 0
Accepted
time: 0ms
memory: 44860kb

input:

10 30
6 1 2 4 3 5 9 7 8
0 1
0 2
0 4
0 5
0 6
0 7
0 9
0 10
1 2
1 3
1 6
1 7
1 8
1 10
2 6
2 8
2 10
3 4
3 5
3 6
3 10
4 6
4 10
5 6
5 7
5 8
5 10
6 8
7 9
8 10

output:

73856

result:

ok 1 number(s): "73856"

Test #44:

score: 0
Accepted
time: 0ms
memory: 44576kb

input:

10 40
8 3 1 2 7 6 4 5 9
0 1
0 2
0 3
0 4
0 7
1 2
1 3
1 5
1 6
1 7
1 9
1 10
2 3
2 5
2 6
2 7
2 8
2 9
2 10
3 4
3 5
3 6
3 7
3 10
4 6
4 8
4 9
4 10
5 7
5 8
5 9
5 10
6 7
6 8
6 9
7 8
7 9
7 10
8 10
9 10

output:

73024

result:

ok 1 number(s): "73024"

Test #45:

score: 0
Accepted
time: 0ms
memory: 44668kb

input:

100 1
31 25 5 3 1 2 4 17 6 9 8 7 12 10 11 16 14 13 15 18 23 21 20 19 22 24 30 29 28 27 26 38 33 32 35 34 36 37 65 59 39 45 44 42 40 41 43 52 47 46 50 49 48 51 58 57 55 54 53 56 63 61 60 62 64 99 97 90 68 66 67 86 69 76 70 73 72 71 74 75 81 78 77 80 79 84 83 82 85 87 89 88 95 94 91 92 93 96 98
38 95

output:

552106926

result:

ok 1 number(s): "552106926"

Test #46:

score: 0
Accepted
time: 3ms
memory: 44868kb

input:

100 5
68 14 10 6 5 3 1 2 4 9 8 7 13 12 11 63 41 36 30 27 15 16 17 19 18 22 21 20 23 24 25 26 29 28 34 32 31 33 35 38 37 40 39 60 48 46 42 43 45 44 47 52 49 51 50 54 53 57 55 56 58 59 62 61 66 64 65 67 97 75 74 70 69 72 71 73 76 95 80 78 77 79 90 84 83 81 82 86 85 88 87 89 93 92 91 94 96 98 99
5 62
1...

output:

413773825

result:

ok 1 number(s): "413773825"

Test #47:

score: 0
Accepted
time: 0ms
memory: 44828kb

input:

100 10
86 24 4 2 1 3 9 6 5 7 8 22 15 10 12 11 14 13 18 16 17 19 21 20 23 77 33 29 27 25 26 28 32 30 31 70 60 52 43 40 35 34 39 36 37 38 42 41 47 46 44 45 51 50 49 48 57 53 56 55 54 59 58 65 62 61 63 64 69 66 67 68 73 72 71 76 74 75 84 83 78 82 80 79 81 85 91 88 87 89 90 98 97 92 94 93 96 95 99
5 30
...

output:

62575440

result:

ok 1 number(s): "62575440"

Test #48:

score: 0
Accepted
time: 0ms
memory: 44672kb

input:

100 100
87 47 5 2 1 3 4 27 22 16 13 12 10 9 8 6 7 11 14 15 21 17 19 18 20 24 23 25 26 28 37 32 29 30 31 35 33 34 36 41 39 38 40 45 43 42 44 46 56 50 48 49 51 53 52 55 54 60 57 59 58 77 64 62 61 63 65 66 75 70 68 67 69 73 72 71 74 76 83 79 78 81 80 82 86 84 85 96 93 89 88 90 92 91 94 95 99 97 98
1 11...

output:

409347680

result:

ok 1 number(s): "409347680"

Test #49:

score: 0
Accepted
time: 0ms
memory: 44568kb

input:

100 200
33 5 3 1 2 4 28 13 12 7 6 11 9 8 10 18 14 17 16 15 23 20 19 22 21 26 24 25 27 31 29 30 32 59 57 36 35 34 38 37 46 41 39 40 43 42 45 44 47 49 48 54 52 51 50 53 56 55 58 88 87 85 79 69 61 60 66 63 62 64 65 68 67 75 70 73 71 72 74 77 76 78 80 84 83 81 82 86 91 90 89 94 92 93 98 95 97 96 99
0 2
...

output:

390437573

result:

ok 1 number(s): "390437573"

Test #50:

score: 0
Accepted
time: 4ms
memory: 44868kb

input:

100 300
79 29 24 12 8 3 2 1 4 5 6 7 11 10 9 19 13 17 14 16 15 18 21 20 22 23 26 25 28 27 44 41 33 32 30 31 38 37 34 35 36 39 40 43 42 70 46 45 65 47 63 58 51 48 49 50 54 53 52 56 55 57 62 61 60 59 64 66 67 69 68 75 74 71 73 72 77 76 78 95 90 80 86 85 83 81 82 84 87 89 88 92 91 93 94 97 96 98 99
0 44...

output:

819705181

result:

ok 1 number(s): "819705181"

Test #51:

score: 0
Accepted
time: 0ms
memory: 44844kb

input:

100 1000
93 57 56 11 2 1 7 5 4 3 6 8 9 10 30 12 26 25 20 18 14 13 16 15 17 19 21 24 22 23 27 29 28 35 34 31 32 33 36 45 44 37 38 43 42 39 40 41 53 49 48 47 46 51 50 52 55 54 91 78 63 60 59 58 62 61 75 71 68 64 67 66 65 70 69 74 73 72 77 76 83 81 79 80 82 86 84 85 89 87 88 90 92 99 94 98 96 95 97
0 3...

output:

750766504

result:

ok 1 number(s): "750766504"

Test #52:

score: 0
Accepted
time: 0ms
memory: 44628kb

input:

100 5050
59 44 36 35 15 7 6 2 1 3 4 5 9 8 14 12 10 11 13 26 16 20 18 17 19 23 22 21 24 25 32 27 29 28 30 31 33 34 41 39 37 38 40 43 42 45 49 46 48 47 55 50 52 51 53 54 57 56 58 65 60 61 62 64 63 67 66 84 83 72 69 68 70 71 81 75 74 73 78 76 77 80 79 82 93 85 88 86 87 89 92 90 91 96 94 95 98 97 99
0 1...

output:

331243901

result:

ok 1 number(s): "331243901"

Test #53:

score: 0
Accepted
time: 0ms
memory: 44868kb

input:

100 100
71 14 3 1 2 9 6 4 5 8 7 13 12 11 10 60 51 27 20 17 16 15 18 19 21 23 22 25 24 26 43 40 35 28 33 29 31 30 32 34 39 36 38 37 41 42 50 44 48 47 45 46 49 55 52 54 53 59 58 56 57 69 65 61 64 62 63 66 68 67 70 74 73 72 87 78 77 75 76 80 79 86 81 85 83 82 84 99 92 90 88 89 91 98 94 93 96 95 97
0 1
...

output:

614246259

result:

ok 1 number(s): "614246259"

Test #54:

score: 0
Accepted
time: 4ms
memory: 44576kb

input:

100 100
3 2 1 96 93 5 4 90 6 11 8 7 9 10 16 15 14 12 13 21 17 18 19 20 88 26 23 22 25 24 84 27 79 29 28 33 31 30 32 37 34 36 35 38 42 40 39 41 46 45 43 44 78 76 50 47 48 49 74 73 70 66 54 51 53 52 63 55 60 58 57 56 59 61 62 65 64 67 68 69 71 72 75 77 82 81 80 83 87 86 85 89 91 92 94 95 97 98 99
0 23...

output:

663163875

result:

ok 1 number(s): "663163875"

Test #55:

score: 0
Accepted
time: 0ms
memory: 44576kb

input:

100 500
96 93 4 1 3 2 90 5 7 6 10 9 8 89 14 13 12 11 17 16 15 22 21 20 18 19 24 23 88 86 85 27 26 25 30 28 29 32 31 84 36 34 33 35 37 82 38 41 40 39 44 42 43 47 45 46 52 49 48 51 50 78 76 53 56 54 55 73 69 66 60 58 57 59 65 64 61 62 63 68 67 72 70 71 75 74 77 81 80 79 83 87 91 92 94 95 97 98 99
0 3
...

output:

62035584

result:

ok 1 number(s): "62035584"

Test #56:

score: 0
Accepted
time: 0ms
memory: 44548kb

input:

500 10
457 239 205 66 2 1 33 19 6 3 5 4 8 7 10 9 12 11 17 16 15 14 13 18 25 22 21 20 23 24 28 27 26 29 32 31 30 64 54 44 39 36 34 35 37 38 43 41 40 42 49 46 45 48 47 53 51 50 52 55 58 57 56 61 60 59 62 63 65 157 135 104 83 81 78 74 70 69 68 67 73 71 72 76 75 77 79 80 82 96 93 88 84 86 85 87 90 89 91...

output:

388044595

result:

ok 1 number(s): "388044595"

Test #57:

score: 0
Accepted
time: 0ms
memory: 44824kb

input:

500 100
239 5 4 2 1 3 148 146 97 65 24 7 6 18 9 8 12 11 10 15 13 14 17 16 23 21 20 19 22 63 55 29 26 25 27 28 35 32 30 31 33 34 50 38 37 36 43 39 41 40 42 46 44 45 47 48 49 52 51 53 54 61 59 58 56 57 60 62 64 91 69 67 66 68 81 79 73 72 70 71 75 74 78 77 76 80 82 88 83 87 84 85 86 89 90 96 95 92 94 9...

output:

292284846

result:

ok 1 number(s): "292284846"

Test #58:

score: 0
Accepted
time: 4ms
memory: 44640kb

input:

500 500
62 11 1 4 2 3 10 6 5 9 8 7 44 18 12 17 16 14 13 15 21 19 20 37 35 31 30 23 22 29 28 26 24 25 27 33 32 34 36 42 40 38 39 41 43 48 46 45 47 55 54 52 51 49 50 53 61 57 56 59 58 60 152 141 130 78 72 64 63 69 65 68 67 66 71 70 75 73 74 77 76 128 85 84 80 79 83 81 82 97 91 89 86 87 88 90 93 92 96 ...

output:

118898834

result:

ok 1 number(s): "118898834"

Test #59:

score: 0
Accepted
time: 0ms
memory: 44916kb

input:

500 2000
234 206 70 5 1 4 2 3 56 42 29 15 14 12 11 7 6 9 8 10 13 26 17 16 20 18 19 22 21 24 23 25 27 28 31 30 38 37 36 33 32 35 34 40 39 41 43 50 49 48 44 45 47 46 52 51 54 53 55 62 59 58 57 60 61 69 64 63 67 66 65 68 101 82 81 75 73 71 72 74 80 78 76 77 79 87 85 83 84 86 88 95 94 90 89 91 93 92 99 ...

output:

567391097

result:

ok 1 number(s): "567391097"

Test #60:

score: 0
Accepted
time: 8ms
memory: 45384kb

input:

500 125250
36 26 9 2 1 3 7 6 5 4 8 25 22 16 15 10 12 11 13 14 21 18 17 20 19 24 23 29 27 28 35 32 31 30 34 33 80 38 37 67 42 41 40 39 48 44 43 45 46 47 62 54 50 49 52 51 53 60 59 57 55 56 58 61 65 63 64 66 74 68 71 70 69 72 73 78 76 75 77 79 100 92 89 87 82 81 85 84 83 86 88 91 90 95 93 94 96 97 98 ...

output:

882819037

result:

ok 1 number(s): "882819037"

Test #61:

score: 0
Accepted
time: 0ms
memory: 44876kb

input:

500 500
215 39 16 4 3 1 2 6 5 15 10 9 7 8 12 11 14 13 21 20 19 18 17 23 22 25 24 35 26 27 30 28 29 32 31 34 33 38 36 37 98 42 40 41 75 65 58 45 43 44 55 49 48 47 46 53 50 52 51 54 57 56 64 62 60 59 61 63 71 66 70 69 67 68 74 73 72 96 76 91 84 81 79 78 77 80 83 82 89 86 85 87 88 90 94 93 92 95 97 132...

output:

384573809

result:

ok 1 number(s): "384573809"

Test #62:

score: 0
Accepted
time: 0ms
memory: 44696kb

input:

500 500
497 1 6 5 2 3 4 7 495 492 10 8 9 12 11 491 16 15 14 13 19 18 17 20 25 21 23 22 24 487 484 479 27 26 29 28 31 30 477 475 35 34 32 33 473 37 36 40 39 38 45 44 41 42 43 47 46 469 51 49 48 50 468 55 54 52 53 59 56 57 58 466 463 462 458 454 60 63 62 61 65 64 450 449 445 67 66 68 440 72 69 70 71 4...

output:

175047909

result:

ok 1 number(s): "175047909"

Test #63:

score: 0
Accepted
time: 3ms
memory: 44716kb

input:

500 3000
497 493 5 3 2 1 4 490 485 10 6 9 8 7 15 11 12 14 13 484 16 482 20 19 18 17 479 477 475 471 469 21 466 24 22 23 465 26 25 462 30 29 28 27 458 34 32 31 33 456 35 453 449 444 40 37 36 38 39 443 45 42 41 43 44 438 434 432 48 47 46 49 50 430 426 424 55 52 51 54 53 419 57 56 58 417 414 412 62 60 ...

output:

208249623

result:

ok 1 number(s): "208249623"

Test #64:

score: 0
Accepted
time: 0ms
memory: 44936kb

input:

500 500
4 1 2 3 499 496 492 6 5 488 485 8 7 483 481 10 9 15 13 12 11 14 20 18 17 16 19 478 477 21 475 25 23 22 24 26 30 29 27 28 34 32 31 33 38 35 37 36 470 42 41 40 39 469 46 45 43 44 464 459 457 456 453 51 50 49 47 48 449 56 53 52 55 54 57 59 58 60 61 448 443 441 64 63 62 440 68 67 65 66 73 71 70 ...

output:

57890760

result:

ok 1 number(s): "57890760"

Test #65:

score: 0
Accepted
time: 7ms
memory: 44872kb

input:

5000 20
2811 687 23 10 8 2 1 5 4 3 7 6 9 12 11 14 13 21 16 15 20 18 17 19 22 459 394 248 96 37 32 26 25 24 30 29 27 28 31 35 33 34 36 47 43 41 40 39 38 42 45 44 46 79 63 49 48 52 50 51 56 55 53 54 62 59 58 57 61 60 75 73 67 64 66 65 70 69 68 71 72 74 77 76 78 94 84 80 82 81 83 87 85 86 89 88 90 93 9...

output:

927919648

result:

ok 1 number(s): "927919648"

Test #66:

score: 0
Accepted
time: 8ms
memory: 44956kb

input:

5000 1000
3687 2811 1266 13 6 4 1 2 3 5 11 7 9 8 10 12 850 622 93 53 52 30 19 15 14 18 17 16 27 24 23 22 21 20 26 25 29 28 40 39 32 31 38 37 36 35 34 33 42 41 51 49 45 43 44 48 46 47 50 57 54 56 55 79 75 64 62 60 59 58 61 63 71 68 66 65 67 70 69 72 74 73 78 76 77 80 84 83 82 81 92 86 85 89 87 88 91 ...

output:

965999797

result:

ok 1 number(s): "965999797"

Test #67:

score: 0
Accepted
time: 4ms
memory: 45276kb

input:

5000 5000
4909 1283 241 76 50 36 7 3 1 2 6 4 5 19 15 9 8 13 10 12 11 14 18 16 17 30 20 28 27 21 23 22 24 25 26 29 32 31 34 33 35 42 41 38 37 39 40 49 46 43 44 45 47 48 67 52 51 54 53 57 56 55 61 59 58 60 62 63 64 66 65 71 68 69 70 75 72 74 73 187 87 83 79 78 77 81 80 82 86 85 84 92 90 88 89 91 125 1...

output:

462834466

result:

ok 1 number(s): "462834466"

Test #68:

score: 0
Accepted
time: 29ms
memory: 46652kb

input:

5000 200000
3441 3112 1065 678 466 324 8 1 7 5 4 2 3 6 169 36 35 9 17 13 12 10 11 16 14 15 30 25 23 18 20 19 21 22 24 26 27 29 28 32 31 33 34 65 61 50 49 45 37 42 41 40 38 39 43 44 46 48 47 52 51 54 53 60 58 57 56 55 59 63 62 64 108 97 72 70 69 66 68 67 71 75 73 74 92 80 78 76 77 79 90 83 82 81 86 8...

output:

944502858

result:

ok 1 number(s): "944502858"

Test #69:

score: 0
Accepted
time: 4ms
memory: 45140kb

input:

5000 5000
66 60 17 3 1 2 7 5 4 6 11 9 8 10 16 14 12 13 15 31 27 21 19 18 20 25 24 22 23 26 28 29 30 47 41 36 34 32 33 35 39 38 37 40 45 42 43 44 46 58 51 49 48 50 55 53 52 54 57 56 59 61 64 63 62 65 4137 4059 874 591 308 292 118 79 77 71 69 68 67 70 76 74 73 72 75 78 81 80 103 98 86 85 82 83 84 88 8...

output:

992123431

result:

ok 1 number(s): "992123431"

Test #70:

score: 0
Accepted
time: 18ms
memory: 45288kb

input:

5000 1000
4996 4991 5 3 1 2 4 4988 6 10 9 7 8 14 12 11 13 18 15 16 17 23 22 20 19 21 4984 25 24 4980 29 27 26 28 4979 32 30 31 4974 33 4973 34 37 35 36 4968 4967 4964 41 38 39 40 4961 43 42 45 44 47 46 4959 4955 49 48 4954 50 4950 52 51 57 54 53 55 56 60 58 59 63 62 61 64 4945 4944 65 4939 69 66 68 ...

output:

942332279

result:

ok 1 number(s): "942332279"

Test #71:

score: 0
Accepted
time: 17ms
memory: 45328kb

input:

5000 5000
4997 4995 4994 4991 4 1 2 3 4989 4984 4982 4981 4979 8 7 5 6 11 9 10 16 12 13 14 15 4975 4970 19 17 18 4966 4964 20 22 21 4959 4954 23 24 4949 26 25 30 28 27 29 4948 31 34 32 33 38 37 36 35 42 39 40 41 44 43 48 47 45 46 4947 53 50 49 52 51 58 54 55 56 57 4946 4942 61 59 60 4941 4936 4932 6...

output:

458864653

result:

ok 1 number(s): "458864653"

Test #72:

score: 0
Accepted
time: 47ms
memory: 46816kb

input:

5000 200000
4997 4 1 2 3 6 5 4995 4991 10 8 7 9 4989 4986 12 11 4981 4976 4974 13 4972 14 4971 17 15 16 4970 19 18 4968 20 4965 4962 22 21 4959 25 24 23 4954 27 26 31 30 28 29 4949 33 32 4945 4944 34 38 35 37 36 40 39 4939 4936 43 42 41 4933 45 44 49 48 47 46 50 4930 54 52 51 53 55 4927 4925 59 58 5...

output:

248680221

result:

ok 1 number(s): "248680221"

Test #73:

score: 0
Accepted
time: 14ms
memory: 45424kb

input:

5000 5000
4998 4 1 2 3 8 6 5 7 4994 4993 4992 4989 4985 4983 4982 9 13 11 10 12 14 18 15 16 17 23 22 19 21 20 25 24 4978 4976 4974 4973 26 30 29 28 27 31 32 4972 4969 4968 33 4967 4962 38 35 34 36 37 43 39 40 41 42 44 4959 48 47 45 46 53 51 50 49 52 4956 4951 55 54 4950 59 58 57 56 4948 4943 62 60 6...

output:

284890525

result:

ok 1 number(s): "284890525"

Test #74:

score: 0
Accepted
time: 2ms
memory: 44624kb

input:

2 1
1
0 2

output:

5

result:

ok 1 number(s): "5"

Test #75:

score: 0
Accepted
time: 0ms
memory: 44564kb

input:

3 1
2 1
2 3

output:

21

result:

ok 1 number(s): "21"

Test #76:

score: 0
Accepted
time: 611ms
memory: 49940kb

input:

100000 1
94687 15935 4789 3507 2824 2099 1138 92 75 24 23 19 3 2 1 4 16 12 5 7 6 10 9 8 11 14 13 15 18 17 22 21 20 59 33 27 25 26 28 29 31 30 32 48 39 37 35 34 36 38 46 40 44 43 41 42 45 47 58 57 56 51 49 50 53 52 55 54 69 60 64 61 63 62 67 65 66 68 71 70 73 72 74 82 80 76 78 77 79 81 90 89 88 87 83...

output:

54900200

result:

ok 1 number(s): "54900200"

Test #77:

score: -100
Time Limit Exceeded

input:

199999 1
39981 30792 15067 5153 4932 1167 698 157 7 2 1 4 3 6 5 141 37 23 17 8 14 12 10 9 11 13 15 16 20 18 19 22 21 24 35 26 25 29 27 28 33 32 30 31 34 36 109 74 71 51 39 38 48 42 41 40 44 43 45 46 47 49 50 59 57 53 52 55 54 56 58 69 61 60 63 62 64 67 65 66 68 70 72 73 93 92 79 75 78 77 76 85 83 81...

output:


result: