forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1183-computing_fast_average.cpp
166 lines (132 loc) · 3.19 KB
/
1183-computing_fast_average.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[100100];
vector <pair <ll, ll> > v;
struct sam {
int lazy;
int cnt;
int sum;
};
struct sam nodes[500100];
void initilize ( int node )
{
nodes[node].lazy = -1;
nodes[node].sum = 0;
nodes[node].cnt = 0;
}
void buildtree ( int node, int start, int end, int val )
{
if ( start > end ) {
return;
}
if ( start == end )
{
initilize ( node );
return;
}
int mid = start + end;
mid /= 2;
buildtree ( node * 2 + 1, start , mid, val );
buildtree ( node * 2 + 2, mid + 1, end, val );
initilize ( node );
return;
}
void update ( int node, int start, int end, int qstr, int qend, int val )
{
if ( nodes[node].lazy != -1 ) {
int temp = nodes[node].lazy ;
if ( start != end ) {
nodes[node * 2 + 1].lazy = temp, nodes[node * 2 + 2].lazy = temp;
}
nodes[node].sum = ( end - start + 1 ) * temp;
nodes[node].lazy = -1;
}
if ( start > end ) {
return;
}
if ( qend < start || qstr > end ) {
return;
}
if ( qstr <= start and qend >= end ) {
int temp = val;
if ( start != end ) {
nodes[node * 2 + 1].lazy = temp, nodes[node * 2 + 2].lazy = temp;
}
nodes[node].sum = ( end - start + 1 ) * temp;
nodes[node].lazy = -1;
return;
}
int mid = start + end;
mid /= 2;
update ( node * 2 + 1, start, mid, qstr, qend, val );
update ( node * 2 + 2, mid + 1, end, qstr, qend, val );
nodes[node].sum = nodes[node * 2 + 1].sum + nodes[node * 2 + 2].sum;
}
ll query ( int node, int start, int end, int qstr, int qend )
{
if ( nodes[node].lazy != -1 ) {
int temp = nodes[node].lazy ;
if ( start != end ) {
nodes[node * 2 + 1].lazy = temp, nodes[node * 2 + 2].lazy = temp;
}
nodes[node].sum = ( end - start + 1 ) * temp;
nodes[node].lazy = -1;
}
if ( start > end ) {
return 0;
}
if ( qend < start || qstr > end ) {
return 0;
}
if ( qstr <= start and qend >= end ) {
return nodes[node].sum;
}
int mid = start + end;
mid /= 2;
int left = query ( node * 2 + 1, start, mid, qstr, qend );
int right = query ( node * 2 + 2, mid + 1, end, qstr, qend );
return left + right;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen ( "/home/placements2016/input.sam", "r", stdin );
#endif
//ios_base::sync_with_stdio(false);
ll i, j, k, n, m, t, cont, a, b, c;
//cin >> t;
scanf ( "%lld", &t );
ll cases = t;
ll temp = 0;
while ( t-- ) {
//cin >> n >> m;
scanf ( "%lld %lld", &n, &m );
memset ( nodes, 0, sizeof nodes );
// cout << "Case " << cases-t << ":" << endl;
printf ( "Case %lld:\n", cases - t );
buildtree ( 0, 0, n - 1, 0 );
//cout << "after bldng :" << nodes[1].arr[0] << " " << nodes[2].arr[0] << " "<< nodes[0].arr[2] << endl;
for ( i = 0; i < m; i++ ) {
//cin >> k;
//cin >> a >> b;
scanf ( "%lld %lld %lld ", &k, &a, &b );
if ( k == 1 ) {
//cin >> c;
scanf ( "%lld", &c );
update ( 0, 0, n - 1, a, b, c );
} else if ( k == 2 ) {
ll ans ;
ans = query ( 0, 0, n - 1, a, b ) ;
if ( ans % ( b - a + 1 ) == 0 ) {
printf ( "%lld\n", ans / ( b - a + 1 ) );
}
else {
printf ( "%lld/%lld\n", ans / __gcd ( ans, ( b - a + 1 ) ), ( b - a + 1 ) / __gcd ( ans, ( b - a + 1 ) ) );
}
}
}
}
return 0;
}