#include "game.h"
#include <iostream>
#include <numeric>
using namespace std;
// const int N=1e9;
int R,C;
class AVL{
public:
class node{
public:
long long subtree_gcd=0,own_value=0;
int height,key,mx=-1e9,mi=1e9;
node * left;
node * right;
node(int k,long long tp){
height = 1;
mx=mi=k;
key = k;
subtree_gcd=tp;
own_value=tp;
left = NULL;
right = NULL;
}
};
node * root = NULL;
int n;
void Update(int& x,long long& val){
root=insertUtil(root, x,val);
}
long long get(int& l,int& r)
{
// cout<<"GCd for "<<l<<' '<<r<<" is "<<get(root,l,r)<<endl;
return get(root,l,r);
}
void print()
{
print(root);
}
private:
void Rectify(node * head)
{
if(head==NULL)
return;
head->subtree_gcd=head->own_value;
head->mx=head->key;
head->mi=head->key;
if(head->left!=NULL)
{
head->mx=max(head->mx,head->left->mx);
head->mi=min(head->mi,head->left->mi);
head->subtree_gcd=gcd(head->subtree_gcd,head->left->subtree_gcd);
}
if(head->right!=NULL)
{
head->mx=max(head->mx,head->right->mx);
head->mi=min(head->mi,head->right->mi);
head->subtree_gcd=gcd(head->subtree_gcd,head->right->subtree_gcd);
}
}
void print(node* h)
{
if(h==NULL)
return;
print(h->left);
cout<<h->key<<' '<<h->mi<<' '<<h->mx<<' '<<h->own_value<<' '<<h->subtree_gcd<<endl;
print(h->right);
}
int height(node * head){
if(head==NULL) return 0;
return head->height;
}
node * rightRotation(node * head){
node * newhead = head->left;
head->left = newhead->right;
newhead->right = head;
head->height = 1+max(height(head->left), height(head->right));
newhead->height = 1+max(height(newhead->left), height(newhead->right));
Rectify(head);
Rectify(newhead);
return newhead;
}
node * leftRotation(node * head){
node * newhead = head->right;
head->right = newhead->left;
newhead->left = head;
head->height = 1+max(height(head->left), height(head->right));
newhead->height = 1+max(height(newhead->left), height(newhead->right));
Rectify(head);
Rectify(newhead);
return newhead;
}
long long get(node* head,int& l,int& r)
{
if(head==NULL)
return 0;
if(head->mx<l or r<head->mi)
return 0;
if(l<=head->mi and head->mx<=r)
return head->subtree_gcd;
long long ans=gcd(get(head->left,l,r),get(head->right,l,r));
if(l<=head->key and head->key<=r)
{
ans=gcd(ans,head->own_value);
}
return ans;
}
node * insertUtil(node * head, int& x,long long& tp){
if(head==NULL){
n+=1;
node * temp = new node(x,tp);
return temp;
}
if(head->key==x)
{
head->own_value=tp;
Rectify(head);
return head;
}
if(x < head->key) head->left = insertUtil(head->left, x,tp);
else if(x > head->key) head->right = insertUtil(head->right, x,tp);
Rectify(head);
head->height = 1 + max(height(head->left), height(head->right));
int bal = height(head->left) - height(head->right);
if(bal>1){
if(x < head->left->key){
return rightRotation(head);
}else{
head->left = leftRotation(head->left);
return rightRotation(head);
}
}else if(bal<-1){
if(x > head->right->key){
return leftRotation(head);
}else{
head->right = rightRotation(head->right);
return leftRotation(head);
}
}
return head;
}
};
struct SegmentTree2D
{
AVL heavy;
SegmentTree2D* next[2];
SegmentTree2D(int l,int r)
{
next[0]=next[1]=NULL;
}
long long get(int& sx,int& ex,int& sy,int& ey,int s=0,int e=R-1)
{
if(ex<s or e<sx)
return 0;
if(sx<=s and e<=ex)
return heavy.get(sy,ey);
long long ans=0;
if(next[0]!=NULL)
ans=gcd(ans,next[0]->get(sx,ex,sy,ey,s,(s+e)/2));
if(next[1]!=NULL)
ans=gcd(ans,next[1]->get(sx,ex,sy,ey,1+((s+e)/2),e));
return ans;
}
void Update(int& l,int& r,long long& val,int s=0,int e=R-1)
{
if(s==e)
{
heavy.Update(r,val);
return;
}
long long gdc=0;
if(l<=((s+e)/2))
{
if(next[0]==NULL)
next[0]=new SegmentTree2D(s,(s+e)/2);
next[0]->Update(l,r,val,s,(s+e)/2);
}
else
{
if(next[1]==NULL)
next[1]=new SegmentTree2D(1+((s+e)/2),e);
next[1]->Update(l,r,val,1+((s+e)/2),e);
}
if(next[0]!=NULL)
gdc=gcd(gdc,next[0]->heavy.get(r,r));
if(next[1]!=NULL)
gdc=gcd(gdc,next[1]->heavy.get(r,r));
heavy.Update(r,gdc);
}
};
SegmentTree2D* st;
void init(int r, int c)
{
C=c;
R=r;
st=new SegmentTree2D(0,R-1);
}
void update(int p, int q, long long k)
{
st->Update(p,q,k);
}
long long calculate(int P, int Q, int U, int V)
{
return st->get(P,U,Q,V);
}
Compilation message
game.cpp: In member function 'void AVL::Rectify(AVL::node*)':
game.cpp:42:11: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
42 | if(head==NULL)
| ^~
game.cpp:44:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
44 | head->subtree_gcd=head->own_value;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
365 ms |
11876 KB |
Output is correct |
5 |
Correct |
191 ms |
11348 KB |
Output is correct |
6 |
Correct |
332 ms |
8836 KB |
Output is correct |
7 |
Correct |
367 ms |
8340 KB |
Output is correct |
8 |
Correct |
246 ms |
7520 KB |
Output is correct |
9 |
Correct |
348 ms |
8416 KB |
Output is correct |
10 |
Correct |
337 ms |
8276 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
534 ms |
13444 KB |
Output is correct |
13 |
Correct |
910 ms |
7648 KB |
Output is correct |
14 |
Correct |
160 ms |
5560 KB |
Output is correct |
15 |
Correct |
938 ms |
9140 KB |
Output is correct |
16 |
Correct |
163 ms |
11588 KB |
Output is correct |
17 |
Correct |
485 ms |
9976 KB |
Output is correct |
18 |
Correct |
777 ms |
13120 KB |
Output is correct |
19 |
Correct |
713 ms |
13072 KB |
Output is correct |
20 |
Correct |
633 ms |
12328 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
368 ms |
11572 KB |
Output is correct |
13 |
Correct |
205 ms |
11348 KB |
Output is correct |
14 |
Correct |
329 ms |
9040 KB |
Output is correct |
15 |
Correct |
372 ms |
8532 KB |
Output is correct |
16 |
Correct |
246 ms |
7428 KB |
Output is correct |
17 |
Correct |
351 ms |
8628 KB |
Output is correct |
18 |
Correct |
345 ms |
8144 KB |
Output is correct |
19 |
Correct |
544 ms |
13460 KB |
Output is correct |
20 |
Correct |
879 ms |
7776 KB |
Output is correct |
21 |
Correct |
159 ms |
5624 KB |
Output is correct |
22 |
Correct |
946 ms |
9340 KB |
Output is correct |
23 |
Correct |
166 ms |
11324 KB |
Output is correct |
24 |
Correct |
475 ms |
9800 KB |
Output is correct |
25 |
Correct |
773 ms |
12904 KB |
Output is correct |
26 |
Correct |
694 ms |
12936 KB |
Output is correct |
27 |
Correct |
640 ms |
12444 KB |
Output is correct |
28 |
Correct |
300 ms |
39080 KB |
Output is correct |
29 |
Correct |
750 ms |
42064 KB |
Output is correct |
30 |
Correct |
945 ms |
30056 KB |
Output is correct |
31 |
Correct |
884 ms |
24624 KB |
Output is correct |
32 |
Correct |
220 ms |
10316 KB |
Output is correct |
33 |
Correct |
293 ms |
10636 KB |
Output is correct |
34 |
Correct |
223 ms |
35700 KB |
Output is correct |
35 |
Correct |
572 ms |
25432 KB |
Output is correct |
36 |
Correct |
1070 ms |
39804 KB |
Output is correct |
37 |
Correct |
875 ms |
39856 KB |
Output is correct |
38 |
Correct |
892 ms |
39500 KB |
Output is correct |
39 |
Correct |
732 ms |
32936 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
936 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
370 ms |
11348 KB |
Output is correct |
13 |
Correct |
196 ms |
11380 KB |
Output is correct |
14 |
Correct |
326 ms |
8684 KB |
Output is correct |
15 |
Correct |
402 ms |
8460 KB |
Output is correct |
16 |
Correct |
232 ms |
7352 KB |
Output is correct |
17 |
Correct |
353 ms |
8424 KB |
Output is correct |
18 |
Correct |
331 ms |
8276 KB |
Output is correct |
19 |
Correct |
523 ms |
13252 KB |
Output is correct |
20 |
Correct |
904 ms |
7748 KB |
Output is correct |
21 |
Correct |
161 ms |
5644 KB |
Output is correct |
22 |
Correct |
922 ms |
8948 KB |
Output is correct |
23 |
Correct |
159 ms |
11348 KB |
Output is correct |
24 |
Correct |
470 ms |
9860 KB |
Output is correct |
25 |
Correct |
744 ms |
12948 KB |
Output is correct |
26 |
Correct |
670 ms |
12940 KB |
Output is correct |
27 |
Correct |
667 ms |
12380 KB |
Output is correct |
28 |
Correct |
337 ms |
39088 KB |
Output is correct |
29 |
Correct |
784 ms |
41792 KB |
Output is correct |
30 |
Correct |
931 ms |
29804 KB |
Output is correct |
31 |
Correct |
876 ms |
24680 KB |
Output is correct |
32 |
Correct |
215 ms |
10068 KB |
Output is correct |
33 |
Correct |
291 ms |
10460 KB |
Output is correct |
34 |
Correct |
263 ms |
35412 KB |
Output is correct |
35 |
Correct |
567 ms |
25444 KB |
Output is correct |
36 |
Correct |
1193 ms |
39612 KB |
Output is correct |
37 |
Correct |
870 ms |
39816 KB |
Output is correct |
38 |
Correct |
869 ms |
39272 KB |
Output is correct |
39 |
Correct |
437 ms |
71372 KB |
Output is correct |
40 |
Correct |
1273 ms |
73188 KB |
Output is correct |
41 |
Correct |
1339 ms |
55396 KB |
Output is correct |
42 |
Correct |
1174 ms |
43684 KB |
Output is correct |
43 |
Correct |
424 ms |
68176 KB |
Output is correct |
44 |
Correct |
269 ms |
10624 KB |
Output is correct |
45 |
Correct |
793 ms |
33156 KB |
Output is correct |
46 |
Correct |
1528 ms |
72012 KB |
Output is correct |
47 |
Correct |
1589 ms |
72316 KB |
Output is correct |
48 |
Correct |
1595 ms |
72028 KB |
Output is correct |
49 |
Correct |
0 ms |
344 KB |
Output is correct |