• 洛谷 T284656 改试卷(paper)


    PS:如果读过题了可以跳过题目描述直接到题解部分
    提交链接:洛谷 T284656 改试卷(paper)

    题目

    题目背景

    zyc快速地浏览了一下手上的数学试卷,并且拿向下一张。

    “咦……是他的。唉,他怎么这个时候总是不在。”他叹道。“这下得好好改了。”

    “这是什么做法啊……这么复杂?这到底是不是对的啊?”

    他手上的试卷采用了一种极度复杂的暴算方法,并跳过了所有计算过程,只有若干个“解得”“可推得”。

    题目描述

    为了检查这个做法是否正确,他不得不维护一个初始为空的二维点集,并支持以下操作共 n n n 次:

    1. 向集合内加入一个点 ( x , y ) (x,y) (x,y)

    2. 从集合内删除第 x x x 次操作加入的点。保证第 x x x 次操作是 1 操作。

    3. 给出参数 a , b a,b a,b,询问对于当前点集内的所有点 ( x , y ) (x,y) (x,y) a x + b y ax+by ax+by 的最大值是多少。特别的,若当前点集为空,规定答案为0。

    zyc发现暴力检查的话就会像手上这张卷子的主人一样浪费整整一小时的时间才能算完。

    所以,他想找到更好的方法。

    不过,他总喜欢再检查一遍。所以,请你看看他和你算的结果是不是一样的吧。

    输入格式

    第一行一个整数 n n n,表示操作次数。
    接下来 n n n 行,每行为 1 x y2 x3 x y ,表示题面中给出的一种操作。

    输出格式

    对于每个 3 操作,输出对应的答案,单独占一行。

    样例 #1

    样例输入 #1

    10
    1 1 1
    3 9 10
    1 1 10
    2 1
    2 3
    1 4 5
    1 10 5
    1 8 8
    2 8
    3 10 6
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    样例输出 #1

    19
    130
    
    • 1
    • 2

    提示

    对于前 20 % 20\% 20% 的数据, n ≤ 2000 n\le 2000 n2000

    对于前 40 % 40\% 40% 的数据, n ≤ 5 × 1 0 4 n\le 5\times 10^4 n5×104

    对于前 70 % 70\% 70% 的数据, n ≤ 1 0 5 n\le 10^5 n105

    对于所有数据, 1 ≤ n ≤ 1 0 6 , 0 ≤ x , y , a , b ≤ 1 0 9 1\le n\le 10^6,0\le x,y,a,b\le 10^9 1n106,0x,y,a,b109

    保证操作2给出的 必定能对应上一个操作1。

    题解

    离线算法+线段树(无push_up)+维护凸包

    1. 离线算法:将每个点按照x轴排序,插入线段树。每次查询按斜率从大到小排序。
    2. 线段树:每个节点表示不同的时间范围,每次直接插入对应节点,不需要push_up(有些地方也叫push_down),查询的时候就从上到下log n个节点的答案取最大值。
    3. 维护凸包:可以先把线段树插入完一起维护,也可以像我一样一边插入一边维护。
    4. 查询优化:保存每个节点上一次查询到的点,从那个点继续查询。

    debug小插曲

    这个代码我真的改了整整一天,结果是check的参数传错了(当然还有二分查询写挂了,但那个是没有必要的,所以后来删了)……

    留着二分的时候只A了一个点,10pts,后来同学就说把二分删了直接打暴力,我就笑了,那不是肯定会T嘛,他就说那也有两个点,转了一会儿,结果一出来,A了!一看最优解,我的暴力居然还是排在第二个的!!!

    代码实现

    100pts暴力无优化

    //洛谷 T284656 改试卷(paper)
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int n;
    long long ans[1000010];
    vector<int>b[4000010];
    
    struct pro{
    	int l,r;
    	int opt;
    	int x;
    	int y;
    }a[1000010];
    
    void in(int &x){
    	int nt;
    	x=0;
    	while(!isdigit(nt=getchar()));
    	x=nt^'0';
    	while(isdigit(nt=getchar())){
    		x=(x<<3)+(x<<1)+(nt^'0');
    	}
    }
    
    bool cmp(pro x,pro y){
    	return (x.opt^y.opt)?(x.opt<y.opt):((!(x.opt^1))?(x.x^y.x?x.x<y.x:x.y<y.y):(1ll*x.x*y.y<1ll*x.y*y.x));
    }
    
    bool check(int x,int y,int z){
    	return 1ll*(a[z].x-a[y].x)*(a[x].y-a[y].y)-1ll*(a[z].y-a[y].y)*(a[x].x-a[y].x)<0;
    }
    
    void build(int rt,int ll,int rr,int l,int r,int x){
    	if(ll>=l&&rr<=r){
    		while(b[rt].size()>1&&check(b[rt][b[rt].size()-1],b[rt][b[rt].size()-2],x)){
    			b[rt].pop_back();
    		}
    		b[rt].push_back(x);
    		return;
    	}
    	int mid=(ll+rr)>>1;
    	if(l<=mid){
    		build(rt<<1,ll,mid,l,r,x);
    	}
    	if(r>mid){
    		build(rt<<1|1,mid+1,rr,l,r,x);
    	}
    }
    
    bool ch(int x,int y,int z){
    	return 1ll*a[z].x*(a[x].y-a[y].y)+1ll*(a[x].x-a[y].x)*a[z].y>=0;
    }
    
    long long query(int rt,int ll,int rr,int t,int x){
    	int l=0,r=b[rt].size()-1;
    	int mid=0;
    	long long temp=0;
    	for(int i=0;i<b[rt].size();++i) {
    		temp=max(temp,1ll*a[x].x*a[b[rt][i]].x+1ll*a[x].y*a[b[rt][i]].y);
    	}
    	if(ll==rr){
    		return temp;
    	}
    	mid=(ll+rr)>>1;
    	if(t<=mid){
    		temp=max(temp,query(rt<<1,ll,mid,t,x));
    	}
    	else{
    		temp=max(temp,query(rt<<1|1,mid+1,rr,t,x)); 
    	}
    	return temp;
    }
    
    signed main(){
    	register int i;
    	in(n);
    	for(i=1;i<=n;++i){
    		in(a[i].opt);
    		in(a[i].x);
    		a[i].l=i;
    		if(a[i].opt&1){
    			in(a[i].y);
    		}
    		else{
    			a[a[i].x].r=i;
    		}
    	}
    	sort(a+1,a+n+1,cmp);
    	memset(ans,-1,sizeof(ans));
    	for(i=1;i<=n;++i){
    		if(!(a[i].opt^1)){
    			if(!a[i].r){
    				a[i].r=n;
    			}
    			build(1,1,n,a[i].l,a[i].r,i);
    		}
    	}
    	for(i=1;i<=n;++i){
    		if(!(a[i].opt^3)){
    			ans[a[i].l]=query(1,1,n,a[i].l,i);
    		}
    	}
    	for(i=1;i<=n;++i){
    		if(~ans[i]){
    			printf("%lld\n",ans[i]);
    		}
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    100pts暴力优化

    //洛谷 T284656 改试卷(paper)
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int n;
    long long ans[1000010];
    vector<int>b[4000010];
    
    struct pro{
    	int l,r;
    	int opt;
    	int x;
    	int y;
    }a[1000010];
    
    void in(int &x){
    	int nt;
    	x=0;
    	while(!isdigit(nt=getchar()));
    	x=nt^'0';
    	while(isdigit(nt=getchar())){
    		x=(x<<3)+(x<<1)+(nt^'0');
    	}
    }
    
    bool cmp(pro x,pro y){
    	return (x.opt^y.opt)?(x.opt<y.opt):((!(x.opt^1))?(x.x^y.x?x.x<y.x:x.y<y.y):(1ll*x.x*y.y<1ll*x.y*y.x));
    }
    
    bool check(int x,int y,int z){
    	return 1ll*(a[z].x-a[y].x)*(a[x].y-a[y].y)-1ll*(a[z].y-a[y].y)*(a[x].x-a[y].x)<0;
    }
    
    void build(int rt,int ll,int rr,int l,int r,int x){
    	if(ll>=l&&rr<=r){
    		while(b[rt].size()>1&&check(b[rt][b[rt].size()-1],b[rt][b[rt].size()-2],x)){
    			b[rt].pop_back();
    		}
    		b[rt].push_back(x);
    		return;
    	}
    	int mid=(ll+rr)>>1;
    	if(l<=mid){
    		build(rt<<1,ll,mid,l,r,x);
    	}
    	if(r>mid){
    		build(rt<<1|1,mid+1,rr,l,r,x);
    	}
    }
    
    bool ch(int x,int y,int z){
    	return 1ll*a[z].x*(a[x].y-a[y].y)+1ll*(a[x].x-a[y].x)*a[z].y>=0;
    }
    
    int c[4000010];
    
    long long query(int rt,int ll,int rr,int t,int x){
    	int l=0,r=b[rt].size()-1;
    	int mid=0;
    	long long temp=0;
    	for(int i=c[rt];i<b[rt].size();++i) {
    		long long tmp=1ll*a[x].x*a[b[rt][i]].x+1ll*a[x].y*a[b[rt][i]].y;
    		if(temp<=tmp) temp=tmp,c[rt]=i;
    	}
    	if(ll==rr){
    		return temp;
    	}
    	mid=(ll+rr)>>1;
    	if(t<=mid){
    		temp=max(temp,query(rt<<1,ll,mid,t,x));
    	}
    	else{
    		temp=max(temp,query(rt<<1|1,mid+1,rr,t,x)); 
    	}
    	return temp;
    }
    
    signed main(){
    	register int i;
    	in(n);
    	for(i=1;i<=n;++i){
    		in(a[i].opt);
    		in(a[i].x);
    		a[i].l=i;
    		if(a[i].opt&1){
    			in(a[i].y);
    		}
    		else{
    			a[a[i].x].r=i;
    		}
    	}
    	sort(a+1,a+n+1,cmp);
    	memset(ans,-1,sizeof(ans));
    	for(i=1;i<=n;++i){
    		if(!(a[i].opt^1)){
    			if(!a[i].r){
    				a[i].r=n;
    			}
    			build(1,1,n,a[i].l,a[i].r,i);
    			
    		}
    	}
    	for(i=1;i<=n;++i){
    		if(!(a[i].opt^3)){
    			ans[a[i].l]=query(1,1,n,a[i].l,i);
    		}
    	}
    	for(i=1;i<=n;++i){
    		if(~ans[i]){
    			printf("%lld\n",ans[i]);
    		}
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    100pts最优解

    //洛谷 T284656 改试卷(paper)
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int n;
    long long ans[1000010];
    vector<int>b[4000010];
    
    struct pro{
    	int l,r;
    	int opt;
    	int x;
    	int y;
    }a[1000010];
    
    void in(int &x){
    	int nt;
    	x=0;
    	while(!isdigit(nt=getchar()));
    	x=nt^'0';
    	while(isdigit(nt=getchar())){
    		x=(x<<3)+(x<<1)+(nt^'0');
    	}
    }
    
    bool cmp(pro x,pro y){
    	return (x.opt^y.opt)?(x.opt<y.opt):((!(x.opt^1))?(x.x^y.x?x.x<y.x:x.y<y.y):(1ll*x.x*y.y<1ll*x.y*y.x));
    }
    
    bool check(int x,int y,int z){
    	return 1ll*(a[z].x-a[y].x)*(a[x].y-a[y].y)-1ll*(a[z].y-a[y].y)*(a[x].x-a[y].x)<0;
    }
    void build(int rt,int ll,int rr,int l,int r,int x){
    	if(ll>=l&&rr<=r){
    		while(b[rt].size()>1&&check(b[rt][b[rt].size()-1],b[rt][b[rt].size()-2],x)){
    			b[rt].pop_back();
    		}
    		b[rt].push_back(x);
    		return;
    	}
    	int mid=(ll+rr)>>1;
    	if(l<=mid){
    		build(rt<<1,ll,mid,l,r,x);
    	}
    	if(r>mid){
    		build(rt<<1|1,mid+1,rr,l,r,x);
    	}
    }
    
    bool ch(int x,int y,int z){
    	return 1ll*a[z].x*(a[x].y-a[y].y)+1ll*(a[x].x-a[y].x)*a[z].y>=0;
    }
    
    int c[4000010];
    
    long long query(int rt,int ll,int rr,int t,int x){
    	int l=0,r=b[rt].size()-1;
    	int mid=0;
    	long long temp=0;
    	for(int i=c[rt];i<b[rt].size();++i) {
    		long long tmp=1ll*a[x].x*a[b[rt][i]].x+1ll*a[x].y*a[b[rt][i]].y;
    		if(temp<=tmp) temp=tmp,c[rt]=i;
    		else break;
    	}
    	if(ll==rr){
    		return temp;
    	}
    	mid=(ll+rr)>>1;
    	if(t<=mid){
    		temp=max(temp,query(rt<<1,ll,mid,t,x));
    	}
    	else{
    		temp=max(temp,query(rt<<1|1,mid+1,rr,t,x)); 
    	}
    	return temp;
    }
    
    signed main(){
    	register int i;
    	in(n);
    	for(i=1;i<=n;++i){
    		in(a[i].opt);
    		in(a[i].x);
    		a[i].l=i;
    		if(a[i].opt&1){
    			in(a[i].y);
    		}
    		else{
    			a[a[i].x].r=i;
    		}
    	}
    	sort(a+1,a+n+1,cmp);
    	memset(ans,-1,sizeof(ans));
    	for(i=1;i<=n;++i){
    		if(!(a[i].opt^1)){
    			if(!a[i].r){
    				a[i].r=n;
    			}
    			build(1,1,n,a[i].l,a[i].r,i);
    			
    		}
    	}
    	for(i=1;i<=n;++i){
    		if(!(a[i].opt^3)){
    			ans[a[i].l]=query(1,1,n,a[i].l,i);
    		}
    	}
    	for(i=1;i<=n;++i){
    		if(~ans[i]){
    			printf("%lld\n",ans[i]);
    		}
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
  • 相关阅读:
    什么是 Node.js
    RabbitMQ:work结构
    Win11截图工具在哪里?
    Blazor流程编排的艺术:深入Z.Blazor.Diagrams库的使用与实践
    Flutter中GetX系列四--BottomSheet(底部弹框)
    【论文阅读】Search-Based Testing Approach for Deep Reinforcement Learning Agents
    python实现excel的数据提取
    centos7中python3.10找不到openssl解决方案
    【开发小记】vue项目优化
    JavaScript保姆级教程 ——— 重难点详细解析(万字长文,建议收藏)
  • 原文地址:https://blog.csdn.net/AmyLiu_1020/article/details/127434561