Skip to content

Instantly share code, notes, and snippets.

@fanzeyi
Created December 3, 2011 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanzeyi/1427334 to your computer and use it in GitHub Desktop.
Save fanzeyi/1427334 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
class SegTree {
private:
struct Node {
int l, r;
int max;
int min;
};
Node tree[1000000];
void Build(int left, int right, int p);
public:
SegTree(int left, int right);
void Insert(int what, int where, int p);
};
SegTree::SegTree(int left, int right) {
memset(tree, 0, sizeof(tree));
Build(left, right, 1);
}
void SegTree::Build(int left, int right, int p) {
tree[p].l = left;
tree[p].r = right;
tree[p].max = 0;
tree[p].min = 0x7FFFFFFF;
if(left < right) {
Build(left, (left + right) / 2, p * 2);
Build((left + right) / 2 + 1, right, p * 2 + 1);
}
}
void SegTree::Insert(int what, int where, int p) {
if(what > tree[p].max) {
tree[p].max = what;
}
if(what < tree[p].min) {
tree[p].min = what;
}
if(tree[p].l != tree[p].r) {
if(where <= tree[p*2].r) {
Insert(what, where, p * 2);
}else{
Insert(what, where, p * 2 + 1);
}
}
}
int main() {
FILE *fin = fopen("3264.in", "r");
FILE *fout = fopen("3264.out", "w");
int n, q;
int cow;
fscanf(fin, "%d %d", &n, &q);
SegTree Tree(1, n);
for(int i = 0; i < n; i++) {
fscanf(fin, "%d", &cow);
Tree.Insert(cow, i+1, 1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment