-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReplace Values From L to R Using Lazy.cpp
126 lines (109 loc) · 2.13 KB
/
Replace Values From L to R Using Lazy.cpp
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
118
119
120
121
122
123
124
125
126
// Complexity O(Qlogn+Nlogn)
#include <bits/stdc++.h>
using namespace std;
#define MX 200005
struct info
{
int lazy,sum;
info() : lazy(-1), sum(0) {}
} tree[4*MX];
void update_lazy(int node, int st, int en, int val)
{
tree[node].lazy = val;
tree[node].sum = (en-st+1)*val;
}
void update_node(int node, int st, int en)
{
int left=node*2;
int right=node*2+1;
int mid = (st+en)/2;
tree[left].lazy=tree[right].lazy=tree[node].lazy;
tree[left].sum= (mid-st+1)*tree[left].lazy;
tree[right].sum= (en-mid)*tree[right].lazy;
tree[node].lazy=-1;
}
void merger(int node)
{
int left=node*2;
tree[node].sum= tree[left].sum + tree[left+1].sum;
}
void update(int node, int s, int e, int i, int j, int val)
{
if(s==i && e==j)
{
update_lazy(node,s,e,val);
return;
}
if(tree[node].lazy!=-1)
{
update_node(node,s,e);
}
int left=node*2;
int mid=(s+e)/2;
if(j<=mid)
{
update(left,s,mid,i,j,val);
}
else if(mid<i)
{
update(left+1,mid+1,e,i,j,val);
}
else
{
update(left, s, mid, i, mid, val);
update(left+1, mid+1, e, mid+1, j, val);
}
merger(node);
}
int query(int node, int s, int e, int i, int j)
{
if(s==i && e==j)
{
return tree[node].sum;
}
if(tree[node].lazy!=-1)
{
update_node(node,s,e);
}
int left=node*2;
int mid=(s+e)/2;
int res=0;
if(j<=mid)
{
res+=query(left, s, mid, i, j);
}
else if(i>mid)
{
res+=query(left+1, mid+1, e, i, j);
}
else
{
res+=(query(left, s, mid, i, mid)+ query(left+1, mid+1, e, mid+1, j));
}
merger(node);
return res;
}
int main()
{
int t,no=0;
int n,m,l,r,x;
scanf("%d%d",&n,&m);
info p;
p.lazy = -1;
p.sum = 0;
fill_n(tree, sizeof(tree)/sizeof(*tree), p);
while(m--)
{
scanf("%d%d%d",&l,&r,&x);
update(1,1,n,l,r,x);
}
for(int i=1;i<=n;i++)
{
printf("%d",query(1,1,n,i,i));
if(i==n)
printf("\n");
else
printf(" ");
}
return 0;
}