Submission #1286986

#TimeUsernameProblemLanguageResultExecution timeMemory
1286986kerem역사적 조사 (JOI14_historical)C++20
100 / 100
1043 ms9760 KiB
#include <bits/stdc++.h>
using namespace std;
//~ #define int int64_t
#define pb push_back
#define emb emplace_back
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define sp << " " <<
#define N 100000
#define inf (int)1e9
typedef pair<int,int> ii;
typedef tuple<int,int,int> iii;

const int kok=320;
struct Item{
	int l,r,i;
	Item(int ll,int rr,int ii){
		l=ll;r=rr;i=ii;
	}
	bool operator <(Item other){
		return make_pair(l/kok,r)<make_pair(other.l/kok,other.r);
	}
};

long long st[2*N];

void update(int x,int val,int n){
	x+=n-1;
	st[x]+=val;
	x>>=1;
	while(x){
		st[x]=max(st[x<<1],st[x<<1|1]);
		x>>=1;
	}
}

void solve(){
	int n,q;
	cin >> n >> q;
	map<int,int> mp;
	vector<Item> query;
	int a[n+1],ind[n+1],cnt=0;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		if(!mp[a[i]])
			mp[a[i]]=++cnt;
	}
	for(int i=1;i<=n;i++)
		ind[i]=mp[a[i]];
	
	for(int i=0;i<q;i++){
		int l,r;
		cin >> l >> r;
		query.emb(l,r,i);
	}
	sort(all(query));
	int l=1,r=0;
	long long ans[q];
	for(auto [ql,qr,i]:query){
		while(r<qr){
			++r;update(ind[r],a[r],cnt);
		}
		while(r>qr){
			update(ind[r],-a[r],cnt);--r;
		}
		while(l<ql){
			update(ind[l],-a[l],cnt);++l;
		}
		while(l>ql){
			--l;update(ind[l],a[l],cnt);
		}
		ans[i]=st[1];
	}
	for(int i=0;i<q;i++)
		cout << ans[i] << "\n";
}
int32_t main(){
	//~ freopen("hopscotch.in","r",stdin);
	//~ freopen("hopscotch.out","w",stdout);
	
	cout << fixed << setprecision(0);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	
	int test=1;
	//~ cin >> test;
	while(test--) solve();
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...