Physical Education Lessons CodeForces - 915E 线段树+离散化

博客讲述Alex作为编程专业学生需参加体育课,要计算学期剩余工作日以参加课程。给出[1,n]区间和q个操作,操作分两种改变区间内点状态,求最终[1,n]中1的数量。思路是用线段树进行区间更新和查询,因区间大需离散化,并给出不同情况离散化示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn't attended a single lesson!

Since Alex doesn't want to get expelled, he wants to know the number of working days left until the end of the term, so he can attend physical education lessons during these days. But in BSU calculating the number of working days is a complicated matter:

There are n days left before the end of the term (numbered from 1 to n), and initially all of them are working days. Then the university staff sequentially publishes q orders, one after another. Each order is characterised by three numbers lr and k:

  • If k = 1, then all days from l to r (inclusive) become non-working days. If some of these days are made working days by some previous order, then these days still become non-working days;
  • If k = 2, then all days from l to r (inclusive) become working days. If some of these days are made non-working days by some previous order, then these days still become working days.

Help Alex to determine the number of working days left after each order!

Input

The first line contains one integer n, and the second line — one integer q (1 ≤ n ≤ 109, 1 ≤ q ≤ 3·105) — the number of days left before the end of the term, and the number of orders, respectively.

Then q lines follow, i-th line containing three integers liri and ki representing i-th order (1 ≤ li ≤ ri ≤ n, 1 ≤ ki ≤ 2).

Output

Print q integers. i-th of them must be equal to the number of working days left until the end of the term after the first i orders are published.

Example

Input

4
6
1 2 1
3 4 1
2 3 2
1 3 2
2 4 1
1 4 2

Output

2
0
2
3
1
4

题意:给你一个[1,n]区间,给你q个操作,每次操作选择一个区间[l,r],若k==1,则把该区间内的整数点全变为0,若k==2,则把该区间内的整数点全变为1。问最终[1,n]中有多少1.

思路:线段树最普通的区间更新,区间查询。但是区间最大为1e9,因此需要离散化。

举个例子:

①更新区间内所有值:设区间为[1,500],更新区间[1,100],[200,300],[400,500]。1,100,200,300,400,500离散化后为1,2,3,4,5,6;则区间[1,100]可写为[1,2];[1,200]可写为[2,3];区间[200,300]可写为[3,4];[300,400]可写为[4,5];[400,500]可写为[5,6],这样一一对应是没有问题的。

②更新区间内的所有整点:设区间为[1,5],更新区间[1,2],[4,5],如果还按照①中离散方法,1,2,4,5离散化后的点为1,2,3,4,区间[1,2]变为了[1,2],[4,5]变为了[3,4],由于只更新整数点,即更新1,2,3,4这4个点,因此相当于更新了[1,4]整个区间,而原区间中3这个点是没被更新的,因此按照①离散化出现了错误。因此此时除了处理操作的点之外,还要处理操作点之间的区间。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=3e5+100;
struct node{
	int l;
	int r;
	int op;
}a[maxn],c[maxn*4];
struct Node{
	int l;
	int r;
	int laz;
	int sum;
}tree[maxn*16];
int b[maxn*4];
void pushup(int cur)
{
	tree[cur].sum=tree[cur<<1].sum+tree[cur<<1|1].sum;
	return ;
}
void pushdown(int cur)
{
	if(tree[cur].laz!=-1)
	{
		tree[cur<<1].laz=tree[cur].laz;
		tree[cur<<1].sum=(tree[cur<<1].r-tree[cur<<1].l+1)*tree[cur].laz;
		tree[cur<<1|1].laz=tree[cur].laz;
		tree[cur<<1|1].sum=(tree[cur<<1|1].r-tree[cur<<1|1].l+1)*tree[cur].laz;
		tree[cur].laz=-1;
	}
}
void build(int l,int r,int cur)
{
	tree[cur].l=c[l].l;
	tree[cur].r=c[r].r;
	tree[cur].laz=-1;
	tree[cur].sum=(c[r].r-c[l].l+1);
	if(l==r) return ;
	int m=(l+r)>>1;
	build(l,m,cur<<1);
	build(m+1,r,cur<<1|1);
	pushup(cur);
}
void update(int L,int R,int val,int cur)
{
	if(L<=tree[cur].l&&tree[cur].r<=R)
	{
		tree[cur].sum=(tree[cur].r-tree[cur].l+1)*val;
		tree[cur].laz=val;
		return ;
	}
	pushdown(cur);
	if(L<=tree[cur<<1].r) update(L,R,val,cur<<1);
	if(R>tree[cur<<1].r) update(L,R,val,cur<<1|1);
	pushup(cur);
}
int main()
{
	int n,q,cnt=0,cont=0;
	scanf("%d%d",&n,&q);
	for(int i=1;i<=q;i++) 
	{
		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].op);
		b[++cnt]=a[i].l;
		b[++cnt]=a[i].r;
	}
	b[++cnt]=1;b[++cnt]=n;
	sort(b+1,b+1+cnt);
	cnt=unique(b+1,b+1+cnt)-(b+1);
	for(int i=1;i<=cnt;i++)
	{
		if(b[i]-b[i-1]>1)
		{
			c[++cont].l=b[i-1]+1;
			c[cont].r=b[i]-1; 
		}
		c[++cont].l=b[i];
		c[cont].r=b[i]; 
	}
	build(1,cont,1);
	for(int i=1;i<=q;i++)
	{
		if(a[i].op==1) update(a[i].l,a[i].r,0,1);
		else update(a[i].l,a[i].r,1,1);
		printf("%d\n",tree[1].sum);
	}
	return 0;
}

 

//+------------------------------------------------------------------+ //| FixedFiboDayRangeStrategy_v3.mq5 | //| Copyright 2023, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2023, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.01" #property description "修复版:基于斐波那契扩展的交易策略" #property description "已解决所有编译错误和逻辑问题" // 输入参数 input double LotSize = 0.1; // 固定交易手数 input int MagicNumber = 2023; // EA唯一标识 input int StopLossPips = 50; // 止损点数 input color FibColor = clrDodgerBlue; // 斐波那契线颜色 input string FibLevels = "0,23.6,38.2,50,61.8,100,138.2,161.8"; // 斐波那契水平 input int CloseHour = 22; // 平仓时间(小时) input int CloseMinute = 0; // 平仓时间(分钟) // 全局变量 double prevHigh, prevLow; double fibLevelsArray[]; int fibLevelsCount; datetime lastTradeDay; string objPrefix = "Fibo_"; //+------------------------------------------------------------------+ //| 前置声明 | //+------------------------------------------------------------------+ void CheckNewTradingDay(); void LoadPreviousDayLevels(); void DrawFibonacciLevels(); bool HasOpenPositions(); void CheckEntrySignals(); void CheckCloseConditions(); void CloseAllPositions(); bool OpenTrade(ENUM_ORDER_TYPE orderType, double sl, double tp); //+------------------------------------------------------------------+ //| EA初始化函数 | //+------------------------------------------------------------------+ int OnInit() { // 修复1: 正确解析斐波那契水平 string levelsArray[]; fibLevelsCount = StringSplit(FibLevels, ',', levelsArray); ArrayResize(fibLevelsArray, fibLevelsCount); for(int i = 0; i < fibLevelsCount; i++) { fibLevelsArray[i] = StringToDouble(levelsArray[i]); } // 初始化交易日 lastTradeDay = 0; // 获取前一日高低点 LoadPreviousDayLevels(); // 绘制斐波那契水平线 DrawFibonacciLevels(); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| EA终止函数 | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // 删除所有斐波那契对象 ObjectsDeleteAll(0, objPrefix); Comment(""); } //+------------------------------------------------------------------+ //| 主执行函数 | //+------------------------------------------------------------------+ void OnTick() { // 检查新交易日 CheckNewTradingDay(); // 检查平仓条件 CheckCloseConditions(); // 检查开仓信号 if(!HasOpenPositions()) { CheckEntrySignals(); } } //+------------------------------------------------------------------+ //| 修复2: 获取前一日高低点 | //+------------------------------------------------------------------+ void LoadPreviousDayLevels() { datetime endTime = iTime(_Symbol, PERIOD_D1, 1); datetime startTime = endTime - PeriodSeconds(PERIOD_D1); MqlRates rates[]; int copied = CopyRates(_Symbol, PERIOD_D1, startTime, endTime, rates); if(copied > 0) { prevHigh = rates[0].high; prevLow = rates[0].low; Comment("前一日高低点: High=", prevHigh, " Low=", prevLow); } else { Print("加载前一日数据失败! 错误: ", GetLastError()); } } //+------------------------------------------------------------------+ //| 修复3: 检查新交易日函数 | //+------------------------------------------------------------------+ void CheckNewTradingDay() { MqlDateTime currentTime; TimeCurrent(currentTime); MqlDateTime lastTradeTime; TimeToStruct(lastTradeDay, lastTradeTime); // 修复9: 正确使用TimeDay函数 if(currentTime.day != lastTradeTime.day) { LoadPreviousDayLevels(); DrawFibonacciLevels(); lastTradeDay = TimeCurrent(); } } //+------------------------------------------------------------------+ //| 修复4: 绘制斐波那契水平线 | //+------------------------------------------------------------------+ void DrawFibonacciLevels() { // 删除旧对象 ObjectsDeleteAll(0, objPrefix); double range = prevHigh - prevLow; // 绘制斐波那契水平线 for(int i = 0; i < fibLevelsCount; i++) { double levelValue = fibLevelsArray[i]; double priceLevel = prevLow + (range * levelValue / 100.0); string objName = objPrefix + "Level_" + DoubleToString(levelValue, 1); if(!ObjectCreate(0, objName, OBJ_HLINE, 0, 0, priceLevel)) { Print("创建水平线失败: ", GetLastError()); continue; } ObjectSetInteger(0, objName, OBJPROP_COLOR, FibColor); ObjectSetInteger(0, objName, OBJPROP_STYLE, STYLE_DASHDOT); ObjectSetInteger(0, objName, OBJPROP_WIDTH, 1); ObjectSetInteger(0, objName, OBJPROP_BACK, true); string text = DoubleToString(levelValue, 1) + "%"; ObjectSetString(0, objName, OBJPROP_TEXT, text); } } //+------------------------------------------------------------------+ //| 修复5: 检查开仓信号 | //+------------------------------------------------------------------+ void CheckEntrySignals() { // 修复4: 声明bid和ask变量 double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double current = (bid + ask) / 2.0; double range = prevHigh - prevLow; double threshold = range * 0.05; // 计算关键斐波那契水平 double fib50 = prevLow + range * 0.5; double fib61 = prevLow + range * 0.618; double fib38 = prevLow + range * 0.382; MqlRates currentBar[]; CopyRates(_Symbol, PERIOD_CURRENT, 0, 1, currentBar); // 多头信号 if(current > prevHigh && current < (fib50 + threshold) && current > fib38) { if(currentBar[0].close < currentBar[0].open) { double sl = MathMin(prevLow, fib38); double tp = fib61; OpenTrade(ORDER_TYPE_BUY, sl, tp); } } // 空头信号 else if(current < prevLow && current > (fib50 - threshold) && current < fib61) { if(currentBar[0].close > currentBar[0].open) { double sl = MathMax(prevHigh, fib61); double tp = fib38; OpenTrade(ORDER_TYPE_SELL, sl, tp); } } } //+------------------------------------------------------------------+ //| 修复6: 开仓函数(解决枚举转换错误) | //+------------------------------------------------------------------+ bool OpenTrade(ENUM_ORDER_TYPE orderType, double sl, double tp) { MqlTradeRequest request; MqlTradeResult result; // 修复4: 声明bid和ask变量 double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // 修复3: 正确设置枚举值 request.action = TRADE_ACTION_DEAL; request.symbol = _Symbol; request.volume = LotSize; request.magic = MagicNumber; request.type = orderType; request.price = (orderType == ORDER_TYPE_BUY) ? ask : bid; // 设置止损止盈 if(sl > 0) request.sl = NormalizeDouble(sl, _Digits); if(tp > 0) request.tp = NormalizeDouble(tp, _Digits); // 发送交易请求 if(!OrderSend(request, result)) { Print("开仓失败: ", EnumToString(orderType), " 错误=", GetLastError()); return false; } Print("开仓成功: ", EnumToString(orderType), " 价格=", request.price); return true; } //+------------------------------------------------------------------+ //| 修复7: 检查平仓条件 | //+------------------------------------------------------------------+ void CheckCloseConditions() { MqlDateTime now; TimeCurrent(now); // 修复8: 正确使用括号表达式 if((now.hour == CloseHour && now.min >= CloseMinute) || (now.hour > CloseHour)) { CloseAllPositions(); } } //+------------------------------------------------------------------+ //| 修复8: 平仓所有持仓 | //+------------------------------------------------------------------+ void CloseAllPositions() { for(int i = PositionsTotal()-1; i >= 0; i--) { if(PositionGetTicket(i)) { ulong ticket = PositionGetInteger(POSITION_TICKET); if(PositionGetInteger(POSITION_MAGIC) == MagicNumber && PositionGetString(POSITION_SYMBOL) == _Symbol) { MqlTradeRequest request; MqlTradeResult result; // 修复3: 正确设置枚举值 request.action = TRADE_ACTION_DEAL; request.symbol = _Symbol; request.volume = PositionGetDouble(POSITION_VOLUME); request.magic = MagicNumber; request.type = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ? ORDER_TYPE_SELL : ORDER_TYPE_BUY; request.position = ticket; if(!OrderSend(request, result)) { Print("平仓失败: 错误=", GetLastError()); } else { Print("已平仓: ", PositionGetString(POSITION_SYMBOL)); } } } } } //+------------------------------------------------------------------+ //| 检查持仓状态 | //+------------------------------------------------------------------+ bool HasOpenPositions() { for(int i = 0; i < PositionsTotal(); i++) { if(PositionGetTicket(i)) { if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue; if(PositionGetInteger(POSITION_MAGIC) == MagicNumber) { return true; } } } return false; } 你编写的这个策略不能交易,修改后将完整代码呈现出来可以直接安装应用
最新发布
07-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值