답안 #204193

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
204193 2020-02-25T04:30:19 Z kig9981 Fire (JOI20_ho_t5) C++17
7 / 100
1000 ms 15352 KB
#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

struct Splay
{
	int p, l, r, v, cnt;
	long long sum;
	bool inv;
	Splay() : p(0), l(0), r(0), v(0), cnt(1), sum(0), inv(false) {}
} tree[200003];

vector<tuple<int,int,int,int>> Q;
vector<int> C, T;
int root, color[200001];
long long ans[200000];

int find_(int a)
{
	return color[a]=a==color[a] ? a:find_(color[a]);
}

void union_(int a, int b)
{
	a=find_(a); b=find_(b);
	color[a]=color[b]=max(a,b);
}

void lazy(int c)
{
	if(tree[c].inv) {
		swap(tree[c].l,tree[c].r);
		if(tree[c].l) tree[tree[c].l].inv^=1;
		if(tree[c].r) tree[tree[c].r].inv^=1;
		tree[c].inv=false;
	}
}

void update(int c)
{
	tree[c].cnt=tree[tree[c].l].cnt+tree[tree[c].r].cnt+1;
	tree[c].sum=tree[tree[c].l].sum+tree[tree[c].r].sum+tree[c].v;
}

void rotate(int c)
{
	int p=tree[c].p;
	if(tree[p].l==c) {
		if(tree[c].r) tree[tree[c].r].p=p;
		tree[p].l=tree[c].r;
		tree[c].r=p;
	}
	else {
		if(tree[c].l) tree[tree[c].l].p=p;
		tree[p].r=tree[c].l;
		tree[c].l=p;
	}
	if(tree[p].p) (tree[tree[p].p].l==p ? tree[tree[p].p].l:tree[tree[p].p].r)=c;
	tree[c].p=tree[p].p;
	tree[p].p=c;
	update(p); update(c);
}

void splay(int c)
{
	while(tree[c].p) {
		int p=tree[c].p;
		if(tree[p].p) lazy(tree[p].p);
		lazy(p); lazy(c);
		if(tree[p].p) rotate((tree[tree[p].p].l==p)==(tree[p].l==c) ? p:c);
		rotate(c);
	}
	lazy(root=c);
}

int kth(int k)
{
	int c=root;
	for(;;) {
		lazy(c);
		if(tree[tree[c].l].cnt>k) {
			c=tree[c].l;
			continue;
		}
		k-=tree[tree[c].l].cnt;
		if(!k--) break;
		c=tree[c].r;
	}
	splay(c);
	return c;
}

int interval(int l, int r)
{
	int ret, temp;
	kth(l-1);
	temp=root;
	root=tree[root].r;
	tree[root].p=0;
	kth(r-l+1);
	tree[temp].r=root;
	ret=tree[root].l;
	tree[root].p=temp;
	update(root=temp);
	return ret;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, M, j=0;
	cin>>N>>M; tree[0].cnt=0;
	for(int i=1;i<=N;i++) {
		cin>>tree[i+1].v;
		tree[i+1].l=i;
		tree[i].p=i+1;
		update(i+1);
		color[i]=i;
		if(tree[i].v>=tree[i+1].v) union_(i-1,i);
	}
	tree[N+2].l=N+1;
	root=tree[N+1].p=N+2;
	update(N+2);
	Q.resize(M);
	for(int i=0;i<M;i++) {
		auto &[t,l,r,idx]=Q[i];
		cin>>t>>l>>r; idx=i;
	}
	sort(Q.begin(),Q.end());
	for(int c=1;c<=N;c=find_(c)+1) {
		int n=find_(c), i, j;
		i=kth(c); j=kth(n);
		if(tree[i].v>tree[j].v) C.push_back(c);
	}
	for(int t=1;t<=N;t++) {
		for(auto c: C) {
			int n=find_(c), v, p;
			v=tree[kth(c)].v;
			tree[interval(c,n-1)].inv^=1;
			tree[interval(c,n)].inv^=1;
			tree[kth(c)].v=v;
			update(root);
			if(n<N) {
				p=kth(n); v=kth(n+1);
				if(tree[p].v>=tree[v].v) union_(n,n+1);
			}
		}
		T.clear();
		for(auto c: C) if(find_(c-1)!=find_(c) && tree[kth(c)].v>tree[kth(find_(c))].v) T.push_back(c);
		C=T;
		while(j<M && get<0>(Q[j])==t) {
			auto[t,l,r,idx]=Q[j++];
			ans[idx]=tree[interval(l,r)].sum;
		}
	}
	for(int i=0;i<M;i++) cout<<ans[i]<<'\n';
	return 0;
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:164:18: warning: unused variable 't' [-Wunused-variable]
    auto[t,l,r,idx]=Q[j++];
                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 8184 KB Output is correct
2 Correct 10 ms 8184 KB Output is correct
3 Correct 10 ms 8184 KB Output is correct
4 Correct 10 ms 8184 KB Output is correct
5 Correct 9 ms 8184 KB Output is correct
6 Correct 10 ms 8184 KB Output is correct
7 Correct 10 ms 8184 KB Output is correct
8 Correct 10 ms 8060 KB Output is correct
9 Correct 10 ms 8184 KB Output is correct
10 Correct 10 ms 8184 KB Output is correct
11 Correct 10 ms 8188 KB Output is correct
12 Correct 10 ms 8184 KB Output is correct
13 Correct 11 ms 8184 KB Output is correct
14 Correct 10 ms 8184 KB Output is correct
15 Correct 10 ms 8184 KB Output is correct
16 Correct 10 ms 8184 KB Output is correct
17 Correct 10 ms 8184 KB Output is correct
18 Correct 10 ms 8184 KB Output is correct
19 Correct 10 ms 8184 KB Output is correct
20 Correct 9 ms 8184 KB Output is correct
21 Correct 10 ms 8184 KB Output is correct
22 Correct 10 ms 8184 KB Output is correct
23 Correct 10 ms 8184 KB Output is correct
24 Correct 10 ms 8184 KB Output is correct
25 Correct 9 ms 8184 KB Output is correct
26 Correct 10 ms 8184 KB Output is correct
27 Correct 10 ms 8188 KB Output is correct
28 Correct 10 ms 8184 KB Output is correct
29 Correct 10 ms 8184 KB Output is correct
30 Correct 9 ms 8184 KB Output is correct
31 Correct 11 ms 8184 KB Output is correct
32 Correct 11 ms 8184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 8184 KB Output is correct
2 Execution timed out 1093 ms 12536 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 8184 KB Output is correct
2 Execution timed out 1097 ms 14164 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 721 ms 15096 KB Output is correct
2 Correct 713 ms 15096 KB Output is correct
3 Correct 722 ms 15220 KB Output is correct
4 Correct 715 ms 15096 KB Output is correct
5 Correct 720 ms 15224 KB Output is correct
6 Correct 694 ms 15224 KB Output is correct
7 Correct 714 ms 15224 KB Output is correct
8 Correct 712 ms 15224 KB Output is correct
9 Correct 715 ms 15168 KB Output is correct
10 Correct 711 ms 15200 KB Output is correct
11 Correct 716 ms 15288 KB Output is correct
12 Correct 712 ms 15352 KB Output is correct
13 Correct 720 ms 15352 KB Output is correct
14 Correct 718 ms 15280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 8184 KB Output is correct
2 Correct 10 ms 8184 KB Output is correct
3 Correct 10 ms 8184 KB Output is correct
4 Correct 10 ms 8184 KB Output is correct
5 Correct 9 ms 8184 KB Output is correct
6 Correct 10 ms 8184 KB Output is correct
7 Correct 10 ms 8184 KB Output is correct
8 Correct 10 ms 8060 KB Output is correct
9 Correct 10 ms 8184 KB Output is correct
10 Correct 10 ms 8184 KB Output is correct
11 Correct 10 ms 8188 KB Output is correct
12 Correct 10 ms 8184 KB Output is correct
13 Correct 11 ms 8184 KB Output is correct
14 Correct 10 ms 8184 KB Output is correct
15 Correct 10 ms 8184 KB Output is correct
16 Correct 10 ms 8184 KB Output is correct
17 Correct 10 ms 8184 KB Output is correct
18 Correct 10 ms 8184 KB Output is correct
19 Correct 10 ms 8184 KB Output is correct
20 Correct 9 ms 8184 KB Output is correct
21 Correct 10 ms 8184 KB Output is correct
22 Correct 10 ms 8184 KB Output is correct
23 Correct 10 ms 8184 KB Output is correct
24 Correct 10 ms 8184 KB Output is correct
25 Correct 9 ms 8184 KB Output is correct
26 Correct 10 ms 8184 KB Output is correct
27 Correct 10 ms 8188 KB Output is correct
28 Correct 10 ms 8184 KB Output is correct
29 Correct 10 ms 8184 KB Output is correct
30 Correct 9 ms 8184 KB Output is correct
31 Correct 11 ms 8184 KB Output is correct
32 Correct 11 ms 8184 KB Output is correct
33 Execution timed out 1089 ms 14068 KB Time limit exceeded
34 Halted 0 ms 0 KB -