codeforces 915E Physical Education Lessons

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 l, r 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 li, ri 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
Copy
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 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 queue<int>Q;
 9 int pos,ch[15000001][2],c[15000001],n,q,lazy[15000001],root;
10 void pushdown(int rt,int l,int mid,int r)
11 {
12   if (lazy[rt])
13     {
14       if (!ch[rt][0]&&lazy[rt]==2)
15     ch[rt][0]=++pos;
16       if (!ch[rt][1]&&lazy[rt]==2)
17     ch[rt][1]=++pos;
18       lazy[ch[rt][0]]=lazy[rt];
19       lazy[ch[rt][1]]=lazy[rt];
20       c[ch[rt][0]]=(lazy[rt]-1)*(mid-l+1);
21       c[ch[rt][1]]=(lazy[rt]-1)*(r-mid);
22       lazy[rt]=0;
23     }
24 }
25 void update(int &rt,int l,int r,int L,int R,int d)
26 {
27   if (!rt)
28     {
29       if (Q.empty()==0) rt=Q.front();
30       else rt=++pos;
31     }
32   if (l>=L&&r<=R)
33     {
34       c[rt]=d*(r-l+1);
35       lazy[rt]=d+1;
36       return;
37     }
38   int mid=(l+r)/2;
39   pushdown(rt,l,mid,r);
40   if (L<=mid) update(ch[rt][0],l,mid,L,R,d);
41   if (R>mid) update(ch[rt][1],mid+1,r,L,R,d);
42   c[rt]=c[ch[rt][0]]+c[ch[rt][1]];
43 }
44 int main()
45 {int l,r,k;
46   cin>>n>>q;
47   while (q--)
48     {
49       scanf("%d%d%d",&l,&r,&k);
50       if (k==1)
51     {
52       update(root,1,n,l,r,1);
53     }
54       else
55     {
56       update(root,1,n,l,r,0);
57     }
58       printf("%d\n",n-c[root]);
59     }
60 }

 

转载于:https://www.cnblogs.com/Y-E-T-I/p/8682148.html

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值