Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0c2ee2c

Browse files
Missing DMOJ problem
Segment Tree problem
1 parent 720c2fa commit 0c2ee2c

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

‎DMOJ/ddrp5.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
const int MAXN = 1e6 + 10;
5+
const int INF = 1e9 + 10;
6+
const int NEG = -INF;
7+
8+
struct node{
9+
int v1,v2,v3,v4;
10+
11+
node() : v1(0),v2(0),v3(0),v4(0){}
12+
node(int _v1,int _v2,int _v3,int _v4) : v1(_v1),v2(_v2),v3(_v3),v4(_v4) {}
13+
14+
node operator+(const node& other)const{
15+
node novo(v1,v2,v3,v4);
16+
if(other.v1 > novo.v1){
17+
novo.v2 = novo.v1;
18+
novo.v1 = other.v1;
19+
}
20+
else if(other.v1 > novo.v2){
21+
novo.v2 = other.v1;
22+
}
23+
if(other.v2 > novo.v2){
24+
novo.v2 = other.v2;
25+
}
26+
if(other.v3 < novo.v3){
27+
novo.v4 = novo.v3;
28+
novo.v3 = other.v3;
29+
}
30+
else if(other.v3 < novo.v4){
31+
novo.v4 = other.v3;
32+
}
33+
if(other.v4 < novo.v4){
34+
novo.v4 = other.v4;
35+
}
36+
return novo;
37+
}
38+
39+
int soma_max(){
40+
return v1 + v2;
41+
}
42+
43+
int soma_min(){
44+
return v3 + v4;
45+
}
46+
47+
};
48+
49+
int vetor[MAXN],N,M,Q;
50+
node seg[4*MAXN];
51+
52+
void build(int pos,int left,int right){
53+
//printf("P %d L %d R %d\n",pos,left,right);
54+
if(left == right){
55+
seg[pos].v1 = seg[pos].v3 = vetor[left];
56+
seg[pos].v2 = NEG;
57+
seg[pos].v4 = INF;
58+
return;
59+
}
60+
int mid = (left+right)/2;
61+
build(2*pos,left,mid);
62+
build(2*pos+1,mid+1,right);
63+
seg[pos] = seg[2*pos] + seg[2*pos+1];
64+
}
65+
66+
void update(int pos,int left,int right,int x,int val){
67+
if(left == right){
68+
seg[pos].v1 = seg[pos].v3 = val;
69+
seg[pos].v2 = NEG;
70+
seg[pos].v4 = INF;
71+
return;
72+
}
73+
int mid = (left+right)/2;
74+
if(x <= mid) update(2*pos,left,mid,x,val);
75+
else update(2*pos+1,mid+1,right,x,val);
76+
seg[pos] = seg[2*pos] + seg[2*pos+1];
77+
}
78+
79+
node query(int pos,int left,int right,int i,int j){
80+
//printf("P %d L %d R %d I %d J %d\n",pos,left,right,i,j);
81+
if(left >= i && right <= j) return seg[pos];
82+
int mid = (left+right)/2;
83+
if(j <= mid) return query(2*pos,left,mid,i,j);
84+
else if(i >= mid + 1) return query(2*pos+1,mid+1,right,i,j);
85+
else return query(2*pos,left,mid,i,j) + query(2*pos+1,mid+1,right,i,j);
86+
}
87+
88+
int main(){
89+
scanf("%d %d",&N,&Q);
90+
M = (int)ceil(sqrt(N));
91+
for(int i = 1;i<=N;i++) scanf("%d",&vetor[i]);
92+
build(1,1,N);
93+
int last = 0;
94+
while(Q--){
95+
int op,a,b;
96+
scanf("%d %d %d",&op,&a,&b);
97+
a ^= last;b ^= last;
98+
if(op == 1){
99+
update(1,1,N,a,b);
100+
}
101+
else if(op == 2){
102+
a = (a-1)*M + 1;
103+
b *= M;
104+
last = query(1,1,N,a,b).soma_min();
105+
printf("%d\n",last);
106+
}
107+
else{
108+
a = (a-1)*M + 1;
109+
b *= M;
110+
last = query(1,1,N,a,b).soma_max();
111+
printf("%d\n",last);
112+
}
113+
}
114+
return 0;
115+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /