CF915E Physical Education Lessons

时间:2021-02-21
本文章向大家介绍CF915E Physical Education Lessons ,主要包括CF915E Physical Education Lessons 使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

CF915E Physical Education Lessons

这个题挺裸。

首先一开始想到线段树,然后写完一看数据范围 \(n\leq 10^9\),立马吓得删代码。

可是我们仔细看,\(1\leq 3*10^5\),不是很大,说明线段树上真正有效的区间很少。我们可以维护一个动态开点的线段树,安利一个 ctrl+z 把代码拉了回来改改过了。

有一个问题是可以开多少点呢?我们能开多少开多少,经计算:

\[259*1024*1024/4/4=16384000 \]

那就开 \(1.6*10^7\) 个呗~。

//Don't act like a loser.
//This code is written by huayucaiji
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
using namespace std;

int read() {
	char ch=getchar();
	int f=1,x=0;
	while(ch<'0'||ch>'9') {
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}

const int MAXN=16e6+10; 

int n,m,cnt,root;
struct seg_tree {
	int sum,lazy,lson,rson;
}s[MAXN];

int newnode() {
	cnt++;
	s[cnt].lazy=-1;
	return cnt;
}
void pushdown(int l,int r,int p) {
	if(l==r||s[p].lazy==-1) {
		return ;
	}
	if(!s[p].lson) {
		s[p].lson=newnode();
	}
	if(!s[p].rson) {
		s[p].rson=newnode();
	}
	
	int mid=(l+r)>>1;
	s[s[p].lson].sum=(mid-l+1)*s[p].lazy;
	s[s[p].rson].sum=(r-mid)*s[p].lazy;
	s[s[p].lson].lazy=s[s[p].rson].lazy=s[p].lazy;
	s[p].lazy=-1;
}
void modify(int l,int r,int &p,int x,int y,int val) {
	if(r<x||y<l) {
		return ;
	}
	if(!p) {
		p=newnode();
	}
	if(x<=l&&r<=y) {
		s[p].lazy=val;
		s[p].sum=(r-l+1)*val;
		return ;
	}
	pushdown(l,r,p);
	int mid=(l+r)>>1;
	modify(l,mid,s[p].lson,x,y,val);
	modify(mid+1,r,s[p].rson,x,y,val);
	s[p].sum=s[s[p].lson].sum+s[s[p].rson].sum;
}

signed main() {
	cin>>n>>m;
	modify(1,n,root,1,n,1);
	for(int i=1;i<=m;i++) {
		int l=read(),r=read(),opt=read();
		if(opt==1) {
			modify(1,n,root,l,r,0);
		}
		else {
			modify(1,n,root,l,r,1);
		}
		printf("%d\n",s[1].sum);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/huayucaiji/p/CF915E.html