답안 #983738

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
983738 2024-05-16T03:30:09 Z pcc 가로등 (APIO19_street_lamps) C++17
20 / 100
297 ms 53440 KB
#include <bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define fs first
#define sc second

const int mxn = 3e5+10;
int N,Q;
int arr[mxn];
pii tree[mxn];

pii req[mxn];

namespace TREE{
	int par[mxn][20];
	int dep[mxn];
	vector<int> st;
	void dfs(int now){
		for(int i = 1;i<20;i++){
			par[now][i] = par[par[now][i-1]][i-1];
		}
		if(tree[now].fs){
			par[tree[now].fs][0] = now;
			dep[tree[now].fs] = dep[now]+1;
			dfs(tree[now].fs);
		}
		if(tree[now].sc){
			par[tree[now].sc][0] = now;
			dep[tree[now].sc] = dep[now]+1;
			dfs(tree[now].sc);
		}
		return;
	}
	void BUILD(){
		for(int i = 1;i<=N;i++){
			while(!st.empty()&&arr[st.back()]<=arr[i]){
				tree[i].fs = st.back();
				st.pop_back();
			}
			if(!st.empty()){
				tree[st.back()].sc = i;
			}
			st.push_back(i);
		}
		par[st[0]][0] = st[0];
		dfs(st[0]);
	}
	int LCA(int a,int b){
		if(dep[a]<dep[b])swap(a,b);
		int d = dep[a]-dep[b];
		for(int i = 19;i>=0;i--){
			if(d&(1<<i))a = par[a][i];
		}
		for(int i = 19;i>=0;i--){
			if(par[a][i] != par[b][i])a = par[a][i],b = par[b][i];
		}
		return a== b?a:par[a][0];
	}
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N>>Q;
	if(N>mxn||Q>mxn)return 0;
	string s;
	cin>>s;
	s = "#"+s;
	fill(arr,arr+mxn,mxn);
	for(int i = 1;i<=N;i++){
		if(s[i] == '1')arr[i] = 0;
	}
	for(int i = 1;i<=Q;i++){
		string t;
		cin>>t;
		if(t[0] == 'q'){
			cin>>req[i].fs>>req[i].sc;
		}
		else{
			req[i].fs = 0;
			cin>>req[i].sc;
			arr[req[i].sc] = i;
		}
	}
	TREE::BUILD();
	for(int i = 1;i<=Q;i++){
		if(req[i].fs){
			int p = TREE::LCA(req[i].fs,req[i].sc-1);
			cout<<max(0,i-arr[p])<<'\n';
		}
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 7772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 56 ms 10068 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7768 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 67 ms 35516 KB Output is correct
6 Correct 108 ms 36240 KB Output is correct
7 Correct 139 ms 36800 KB Output is correct
8 Correct 295 ms 53440 KB Output is correct
9 Correct 71 ms 13396 KB Output is correct
10 Correct 77 ms 13664 KB Output is correct
11 Correct 78 ms 13772 KB Output is correct
12 Correct 281 ms 51840 KB Output is correct
13 Correct 297 ms 53180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7772 KB Output is correct
2 Incorrect 2 ms 7772 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 7772 KB Output isn't correct
2 Halted 0 ms 0 KB -