답안 #71353

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
71353 2018-08-24T11:43:38 Z KLPP Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
4941 ms 290472 KB
#include "bubblesort2.h"
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef long long int lld;
typedef std::map<lld,int>::iterator mit;
#define INF 1000000000000000
class FT{
	int arr[1000000];
	int n;
	public:
	void init(int N){
		n=N;
		for(int i=0;i<=n;i++){
			arr[i]=0;
		}
	}
	int query(int prefix){
		prefix++;
		int ans=0;
		for(;prefix>0;prefix-=(prefix&(-prefix))){
			ans+=arr[prefix];
		}return ans;
	}
	void update(int pos){
		pos++;
		for(;pos<=n;pos+=(pos&(-pos))){
			arr[pos]++;
		}
	}
	void update2(int pos){
		pos++;
		for(;pos<=n;pos+=(pos&(-pos))){
			arr[pos]--;
		}
	}
	void print(){
		for(int i=0;i<=n;i++){
			cout<<arr[i]<<" ";
		}cout<<endl;
	}
};
FT *F;
int compute(vector<int> v){
	int n=v.size();
	F->init(n);
	pair<int,int> arr[n];
	for(int i=0;i<n;i++){
		arr[i]=pair<int,int>(v[i],i);
	}sort(arr,arr+n);
	int prev=n-1;
	int ans=0;
	for(int i=n-1;i>-1;i--){
		if(i==0 || arr[i-1]!=arr[i]){
			for(int j=prev;j>=i;j--){
				ans=max(ans,F->query(arr[j].second));
			}
			for(int j=prev;j>=i;j--){
				F->update(arr[j].second);
			}
			prev=i-1;
		}
	}
	return ans;
}
class LP{
	lld segtree[4000000];
	lld lazy[4000000];
	int n;
	public:
	void build(int a, int b, int node){//cout<<a<<" "<<b<<endl;
		lazy[node]=0;
		if(a==b){
			segtree[node]=-INF;
			return;
		}
		int mid=(a+b)/2;
		build(a,mid,2*node);
		build(mid+1,b,2*node+1);
		segtree[node]=max(segtree[2*node],segtree[2*node+1]);
	}
	void init(int N){
		n=N;
		build(0,n-1,1);
	}
	void extend(int node){
		segtree[node]+=lazy[node];
		
			lazy[2*node]+=lazy[node];
			lazy[2*node+1]+=lazy[node];
		
		lazy[node]=0;
	}
	void update(int x, int y, lld val, int a, int b, int node){
		
		if(y<a || b<x)return;
		//cout<<x<<" "<<y<<" "<<a<<" "<<b<<" "<<node<<endl;
		if(x<=a && b<=y){
			lazy[node]+=val;
			return;
		}extend(node);
		int mid=(a+b)/2;
		update(x,y,val,a,mid,2*node);
		update(x,y,val,mid+1,b,2*node+1);
		segtree[node]=max(segtree[2*node]+lazy[2*node],segtree[2*node+1]+lazy[2*node+1]);
	}
	void Update(int x, int y, int val){
		if(x>y)return;
		update(x,y,val,0,n-1,1);
	}
	void set(int pos,lld val,int a, int b, int node){
		
		if(pos<a || pos>b)return;
		if(a==b){
			segtree[node]=val;
			lazy[node]=0;
			return;
		}extend(node);
		int mid=(a+b)/2;
		set(pos,val,a,mid,2*node);
		set(pos,val,mid+1,b,2*node+1);
		segtree[node]=max(segtree[2*node]+lazy[2*node],segtree[2*node+1]+lazy[2*node+1]);
	}
	void Set(int pos, lld val){
		set(pos,val,0,n-1,1);
	}
	lld query(){
		return segtree[1]+lazy[1];
	}
	void print(){
		for(int i=1;i<=2*n;i++){
			cout<<segtree[i]<<" "<<lazy[i]<<"   ";
		}cout<<endl;
	}
};
vector<lld> values;
int BSpos(lld x){
	int lo,hi;
	lo=-1;
	hi=values.size();
	while(hi-lo>1){
		int mid=(hi+lo)/2;
		if(values[mid]>x)hi=mid;
		else lo=mid;
	}
	if(values[lo]==x)return lo;
	return -1;
}
std::vector<int> countScans(std::vector<int> A,std::vector<int> X,std::vector<int> V){
	LP *L;
	int n=A.size();
	L=new LP();
	
	vector<int> ans;
	vector<lld>a;
	vector<lld>v;
	for(int i=0;i<n;i++)a.push_back(A[i]);
	int Q=X.size();
	for(int i=0;i<Q;i++)v.push_back(V[i]);
	for(int i=0;i<n;i++)values.push_back((lld)a[i]*1000000+i);
	for(int i=0;i<Q;i++)values.push_back((lld)v[i]*1000000+X[i]);
	sort(values.begin(),values.end());
	FT *F;
	F=new FT();
	for(int i=0;i<n;i++)a[i]=a[i]*1000000+i;
	for(int i=0;i<Q;i++)v[i]=v[i]*1000000+X[i];
	F->init(n+Q);
	for(int i=0;i<n;i++){
		int pos=BSpos(a[i]);
 
		F->update(pos);
 
	}L->init(n+Q);
	for(int i=0;i<n;i++){//cout<<i<<" 1"<<endl;
		int pos=BSpos(a[i]);
		//cout<<pos<<endl;
		lld questao=F->query(pos)-1;
		//cout<<i-questao<<endl;
		L->Set(pos,i-questao);
	}
	//L->print();
	//cout<<L->query()<<endl;
	for(int i=0;i<Q;i++){//cout<<i<<" 2"<<endl;
		int pos1=BSpos(a[X[i]]);
		int pos2=BSpos(v[i]);
		F->update2(pos1);
		F->update(pos2);
		int start,end;
		
		lld query=F->query(pos2)-1;
		L->Set(pos1,-INF);
		//cout<<X[i]-query<<endl;
		L->Set(pos2,X[i]-query);
		//cout<<pos1<<" "<<pos2<<endl;
		L->Update(pos2+1,n+Q-1,-1);
		L->Update(pos1+1,n+Q-1,1);
		//cout<<L->query()<<endl;
		//L->print();
		ans.push_back(L->query());
		a[X[i]]=v[i];
	}
	return ans;
}

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:191:7: warning: unused variable 'start' [-Wunused-variable]
   int start,end;
       ^~~~~
bubblesort2.cpp:191:13: warning: unused variable 'end' [-Wunused-variable]
   int start,end;
             ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 67064 KB Output is correct
2 Correct 61 ms 67064 KB Output is correct
3 Correct 68 ms 67128 KB Output is correct
4 Correct 57 ms 67236 KB Output is correct
5 Correct 68 ms 67248 KB Output is correct
6 Correct 66 ms 67264 KB Output is correct
7 Correct 65 ms 67292 KB Output is correct
8 Correct 60 ms 67312 KB Output is correct
9 Correct 63 ms 67328 KB Output is correct
10 Correct 67 ms 67336 KB Output is correct
11 Correct 68 ms 67348 KB Output is correct
12 Correct 65 ms 67348 KB Output is correct
13 Correct 73 ms 67348 KB Output is correct
14 Correct 70 ms 67348 KB Output is correct
15 Correct 69 ms 67372 KB Output is correct
16 Correct 66 ms 67372 KB Output is correct
17 Correct 63 ms 67372 KB Output is correct
18 Correct 55 ms 67372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 67064 KB Output is correct
2 Correct 61 ms 67064 KB Output is correct
3 Correct 68 ms 67128 KB Output is correct
4 Correct 57 ms 67236 KB Output is correct
5 Correct 68 ms 67248 KB Output is correct
6 Correct 66 ms 67264 KB Output is correct
7 Correct 65 ms 67292 KB Output is correct
8 Correct 60 ms 67312 KB Output is correct
9 Correct 63 ms 67328 KB Output is correct
10 Correct 67 ms 67336 KB Output is correct
11 Correct 68 ms 67348 KB Output is correct
12 Correct 65 ms 67348 KB Output is correct
13 Correct 73 ms 67348 KB Output is correct
14 Correct 70 ms 67348 KB Output is correct
15 Correct 69 ms 67372 KB Output is correct
16 Correct 66 ms 67372 KB Output is correct
17 Correct 63 ms 67372 KB Output is correct
18 Correct 55 ms 67372 KB Output is correct
19 Correct 84 ms 67716 KB Output is correct
20 Correct 98 ms 67736 KB Output is correct
21 Correct 76 ms 67736 KB Output is correct
22 Correct 75 ms 67772 KB Output is correct
23 Correct 85 ms 67816 KB Output is correct
24 Correct 77 ms 67816 KB Output is correct
25 Correct 71 ms 67816 KB Output is correct
26 Correct 78 ms 67816 KB Output is correct
27 Correct 73 ms 67816 KB Output is correct
28 Correct 73 ms 67828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 68136 KB Output is correct
2 Correct 217 ms 69420 KB Output is correct
3 Correct 327 ms 70452 KB Output is correct
4 Correct 323 ms 70452 KB Output is correct
5 Correct 326 ms 70452 KB Output is correct
6 Correct 293 ms 70460 KB Output is correct
7 Correct 293 ms 70536 KB Output is correct
8 Correct 285 ms 70536 KB Output is correct
9 Correct 253 ms 70536 KB Output is correct
10 Correct 233 ms 70536 KB Output is correct
11 Correct 262 ms 70536 KB Output is correct
12 Correct 235 ms 70560 KB Output is correct
13 Correct 224 ms 70560 KB Output is correct
14 Correct 211 ms 70560 KB Output is correct
15 Correct 240 ms 70560 KB Output is correct
16 Correct 221 ms 70560 KB Output is correct
17 Correct 212 ms 70560 KB Output is correct
18 Correct 196 ms 70560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 67064 KB Output is correct
2 Correct 61 ms 67064 KB Output is correct
3 Correct 68 ms 67128 KB Output is correct
4 Correct 57 ms 67236 KB Output is correct
5 Correct 68 ms 67248 KB Output is correct
6 Correct 66 ms 67264 KB Output is correct
7 Correct 65 ms 67292 KB Output is correct
8 Correct 60 ms 67312 KB Output is correct
9 Correct 63 ms 67328 KB Output is correct
10 Correct 67 ms 67336 KB Output is correct
11 Correct 68 ms 67348 KB Output is correct
12 Correct 65 ms 67348 KB Output is correct
13 Correct 73 ms 67348 KB Output is correct
14 Correct 70 ms 67348 KB Output is correct
15 Correct 69 ms 67372 KB Output is correct
16 Correct 66 ms 67372 KB Output is correct
17 Correct 63 ms 67372 KB Output is correct
18 Correct 55 ms 67372 KB Output is correct
19 Correct 84 ms 67716 KB Output is correct
20 Correct 98 ms 67736 KB Output is correct
21 Correct 76 ms 67736 KB Output is correct
22 Correct 75 ms 67772 KB Output is correct
23 Correct 85 ms 67816 KB Output is correct
24 Correct 77 ms 67816 KB Output is correct
25 Correct 71 ms 67816 KB Output is correct
26 Correct 78 ms 67816 KB Output is correct
27 Correct 73 ms 67816 KB Output is correct
28 Correct 73 ms 67828 KB Output is correct
29 Correct 94 ms 68136 KB Output is correct
30 Correct 217 ms 69420 KB Output is correct
31 Correct 327 ms 70452 KB Output is correct
32 Correct 323 ms 70452 KB Output is correct
33 Correct 326 ms 70452 KB Output is correct
34 Correct 293 ms 70460 KB Output is correct
35 Correct 293 ms 70536 KB Output is correct
36 Correct 285 ms 70536 KB Output is correct
37 Correct 253 ms 70536 KB Output is correct
38 Correct 233 ms 70536 KB Output is correct
39 Correct 262 ms 70536 KB Output is correct
40 Correct 235 ms 70560 KB Output is correct
41 Correct 224 ms 70560 KB Output is correct
42 Correct 211 ms 70560 KB Output is correct
43 Correct 240 ms 70560 KB Output is correct
44 Correct 221 ms 70560 KB Output is correct
45 Correct 212 ms 70560 KB Output is correct
46 Correct 196 ms 70560 KB Output is correct
47 Correct 1086 ms 76788 KB Output is correct
48 Correct 4213 ms 99280 KB Output is correct
49 Correct 4934 ms 106548 KB Output is correct
50 Correct 4806 ms 119360 KB Output is correct
51 Correct 4816 ms 132300 KB Output is correct
52 Correct 4727 ms 145064 KB Output is correct
53 Correct 4941 ms 158052 KB Output is correct
54 Correct 4101 ms 170864 KB Output is correct
55 Correct 4455 ms 184048 KB Output is correct
56 Correct 3935 ms 196936 KB Output is correct
57 Correct 4927 ms 209912 KB Output is correct
58 Correct 4125 ms 223088 KB Output is correct
59 Correct 3943 ms 234652 KB Output is correct
60 Correct 3625 ms 246488 KB Output is correct
61 Correct 3580 ms 257936 KB Output is correct
62 Correct 3613 ms 269512 KB Output is correct
63 Correct 3789 ms 281072 KB Output is correct
64 Correct 3621 ms 290456 KB Output is correct
65 Correct 3309 ms 290472 KB Output is correct
66 Correct 3558 ms 290472 KB Output is correct
67 Correct 3414 ms 290472 KB Output is correct