Submission #25962

# Submission time Handle Problem Language Result Execution time Memory
25962 2017-06-25T08:36:18 Z 서규호(#1088) Cake (CEOI14_cake) C++14
100 / 100
1139 ms 6904 KB
#include <bits/stdc++.h>

#define lld long long
#define pp pair<int,int>
#define pb push_back
#define MOD 1000000007
#define left lleft
#define right rright
#define INF 2000000000
#define Linf 1000000000000000000LL
#define next nnext
#define minus mminus

using namespace std;

int N,M,Q,K,nn;
int a[250002];
int seg[1000000];
int where[12],next[12];

int gett(int node,int l,int r,int x){
	if(r < x || x < l) return -INF;
	if(seg[node] != -INF) return seg[node];
	int mid = (l+r)/2;
	return max(gett(node*2,l,mid,x),gett(node*2+1,mid+1,r,x));
}
int get(int node,int l,int r,int x){
	int tmp = gett(1,1,nn,x);
	if(x <= M) tmp -= x;
	else tmp += x;
	return tmp;
}
void change(int node,int l,int r,int s,int e,int value){
	if(r < s || e < l || s > e) return;
	if(s <= l && r <= e){
		seg[node] = value;
		return;
	}
	if(seg[node] != -INF){
		seg[node*2] = seg[node*2+1] = seg[node];
		seg[node] = -INF;
	}
	int mid = (l+r)/2;
	change(node*2,l,mid,s,e,value);
	change(node*2+1,mid+1,r,s,e,value);
}

int main(){
	scanf("%d %d",&N,&M);
	K = min(10,N); where[K+1] = N+1;
	for(nn=1; nn<N; nn*=2);
	for(int i=1; i<nn*2; i++) seg[i] = -INF;
	for(int i=1; i<=N; i++){
		scanf("%d",&a[i]);
		for(int j=1; j<=K; j++){
			if(a[where[j]] < a[i]){
				for(int k=K; k>=j+1; k--) where[k] = where[k-1];
				where[j] = i;
				break;
			}
		}
	}
	int l,r;
	l = M; r = M; a[M] = 0;
	while(r-l != N-1){
		if(l == 1){
			r++;
			a[r] = r-l;
		}else if(r == N){
			l--;
			a[l] = r-l;
		}else if(a[l-1] < a[r+1]){
			l--;
			a[l] = r-l;
		}else{
			r++;
			a[r] = r-l;
		}
	}
	for(int i=1; i<=N; i++){
		if(i <= M) a[i] += i;
		else a[i] -= i;
		change(1,1,nn,i,i,a[i]);
	}
	scanf("%d",&Q);
	for(int i=1; i<=Q; i++){
		int x,y,t,t2;
		char op[3];
		scanf("%s %d",op,&x);
		if(op[0] == 'F'){
			printf("%d\n",get(1,1,nn,x));
		}else{
			scanf("%d",&y);
			t = -1;
			for(int j=1; j<=K; j++){
				if(where[j] == x){
					t = j;
					break;
				}
			}
			if(t == -1){
				for(int j=K; j>=y+1; j--) where[j] = where[j-1];
				where[y] = x;
			}else{
				for(int j=t; j>y; j--) where[j] = where[j-1];
				where[y] = x;
			}
			vector<pp> tmp;
			tmp.pb({0,0});
			for(int i=1; i<=K; i++) tmp.pb({where[i],i});
			sort(tmp.begin(),tmp.end());
			tmp.pb({0,K+1});
			for(int i=1; i<=K; i++){
				if(tmp[i].first >= M) break;
				next[tmp[i].second] = tmp[i-1].second;
			}
			for(int i=K; i>=1; i--){
				if(tmp[i].first <= M) break;
				next[tmp[i].second] = tmp[i+1].second;
			}
			if(M == x) continue;
			if(M < x){
				l = M-(get(1,1,nn,x)-(x-M));
				t = t2 = 0;
				for(int j=1; j<y; j++){
					if(where[t] < where[j] && where[j] <= l-1){
						t = j;
					}
				}
				change(1,1,nn,where[t]+1,l-1,x-1);
				t2 = y;
			}else if(M > x){
				r = M+(get(1,1,nn,x)-(M-x));
				t = y; t2 = K+1;
				for(int j=1; j<y; j++){
					if(where[t2] > where[j] && where[j] >= r+1){
						t2 = j;
					}
				}
				change(1,1,nn,r+1,where[t2]-1,r-x-(r+1));
			}
			while(!(t == 0 && t2 == K+1)){
                if(t == 0 || (t2 != K+1 && t2 > t)){
                    change(1,1,nn,where[t2],where[next[t2]]-1,where[t2]-where[t]-1-where[t2]);
                    t2 = next[t2];
                }else{
                    change(1,1,nn,where[next[t]]+1,where[t],where[t2]-where[t]-1+where[t]);
                    t = next[t];
                }
            }
		}
	}

	return 0;
}

Compilation message

cake.cpp: In function 'int main()':
cake.cpp:49:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&N,&M);
                      ^
cake.cpp:54:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&a[i]);
                    ^
cake.cpp:85:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&Q);
                ^
cake.cpp:89:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s %d",op,&x);
                       ^
cake.cpp:93:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d",&y);
                  ^
cake.cpp:143:27: warning: 't2' may be used uninitialized in this function [-Wmaybe-uninitialized]
                 if(t == 0 || (t2 != K+1 && t2 > t)){
                           ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 6904 KB Output is correct
2 Correct 0 ms 6904 KB Output is correct
3 Correct 0 ms 6904 KB Output is correct
4 Correct 6 ms 6904 KB Output is correct
5 Correct 16 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1063 ms 6904 KB Output is correct
2 Correct 953 ms 6904 KB Output is correct
3 Correct 1046 ms 6904 KB Output is correct
4 Correct 809 ms 6904 KB Output is correct
5 Correct 1139 ms 6904 KB Output is correct
6 Correct 1129 ms 6904 KB Output is correct
7 Correct 1022 ms 6904 KB Output is correct
8 Correct 926 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 6904 KB Output is correct
2 Correct 59 ms 6904 KB Output is correct
3 Correct 83 ms 6904 KB Output is correct
4 Correct 0 ms 6904 KB Output is correct
5 Correct 156 ms 6904 KB Output is correct
6 Correct 153 ms 6904 KB Output is correct
7 Correct 143 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 6904 KB Output is correct
2 Correct 69 ms 6904 KB Output is correct
3 Correct 153 ms 6904 KB Output is correct
4 Correct 163 ms 6904 KB Output is correct
5 Correct 203 ms 6904 KB Output is correct
6 Correct 326 ms 6904 KB Output is correct
7 Correct 316 ms 6904 KB Output is correct
8 Correct 539 ms 6904 KB Output is correct
9 Correct 939 ms 6904 KB Output is correct
10 Correct 686 ms 6904 KB Output is correct
11 Correct 793 ms 6904 KB Output is correct
12 Correct 986 ms 6904 KB Output is correct
13 Correct 826 ms 6904 KB Output is correct