Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
gzshawnliang committed Mar 12, 2019
1 parent 91095d6 commit 684f046
Show file tree
Hide file tree
Showing 27 changed files with 53,921 additions and 12 deletions.
71 changes: 71 additions & 0 deletions SPOJ/GSS1_AC/GSS1_AC0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 2014-04-25
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[50000];
int sum[200000];
int maxlsum[200000];
int maxrsum[200000];
int maxisum[200000];
int N, M;
void update(int node) {
sum[node] = sum[2*node] + sum[2*node+1];
maxlsum[node] = max(maxlsum[2*node], sum[2*node] + maxlsum[2*node+1]);
maxrsum[node] = max(maxrsum[2*node+1], sum[2*node+1] + maxrsum[2*node]);
maxisum[node] = max(max(maxisum[2*node], maxisum[2*node+1]),
maxrsum[2*node] + maxlsum[2*node+1]);
}
void build_tree(int node, int l, int r) {
if (r == l+1) {
sum[node] = maxlsum[node] = maxrsum[node] = maxisum[node] = a[l];
} else {
int m = l + (r - l + 1)/2;
build_tree(2*node, l, m);
build_tree(2*node+1, m, r);
update(node);
}
}
void query_rec(vector<int>& nodelist,
int node, int tbegin, int tend, int abegin, int aend) {
if (tbegin >= abegin && tend <= aend) {
//fprintf(stderr, "DBG: %d %d\n", tbegin, tend);
nodelist.push_back(node);
} else {
int mid = tbegin + (tend - tbegin + 1)/2;
if (mid > abegin && tbegin < aend) {
query_rec(nodelist, 2*node, tbegin, mid, abegin, aend);
}
if (tend > abegin && mid < aend) {
query_rec(nodelist, 2*node+1, mid, tend, abegin, aend);
}
}
}
int query(int begin, int end) {
vector<int> nodelist;
query_rec(nodelist, 1, 0, N, begin, end);
int best = -2e9;
int tsum = -2e9;
for (int i = 0; i < nodelist.size(); i++) {
best = max(best, maxisum[nodelist[i]]);
best = max(best, tsum + maxlsum[nodelist[i]]);
tsum = max(maxrsum[nodelist[i]], tsum + sum[nodelist[i]]);
}
return best;
}
int main() {
freopen("GSS1_AC0.in", "r", stdin);
freopen("GSS1_AC0.out", "w", stdout);

scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", a + i);
}
build_tree(1, 0, N);
scanf("%d", &M);
for (int i = 0; i < M; i++) {
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", query(x - 1, y));
}
}
91 changes: 91 additions & 0 deletions SPOJ/GSS1_AC/GSS1_AC1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <cmath>
#include <cstring>
#include <vector>
#include <iostream>

using namespace std;
struct list
{
long sum,bestsum,left,right;
};

list* make_new_node(long s,long bs,long l,long r)
{
list* temp=new list();
temp->sum=s;
temp->bestsum=bs;
temp->left=l;
temp->right=r;
return temp;
}

void build(long arr[],list* tree[],int node,int low,int high)
{
if(low>high)
return;
if(low==high)
{
tree[node]=make_new_node(arr[low],arr[low],arr[low],arr[low]);
return;
}

build(arr,tree,2*node+1,low,(high+low)/2);
build(arr,tree,2*node+2,(high+low)/2+1,high);

// tree[node]=NULL;
long a=tree[2*node+1]->sum+tree[2*node+2]->sum;
long b=max(max(tree[2*node+1]->bestsum,tree[2*node+2]->bestsum),tree[2*node+1]->right+tree[2*node+2]->left);
long c=max(tree[2*node+1]->left,tree[2*node+1]->sum+tree[2*node+2]->left);
long d=max(tree[2*node+1]->right+tree[2*node+2]->sum,tree[2*node+2]->right);

tree[node]=make_new_node(a,b,c,d);
}

list* query(list* tree[],int node,int low,int high,int l,int r)
{
if(low==l && high==r)
return tree[node];
int p1 = 2*node+1, p2 = 2*node+2, mid = (low+high)/2;
if(r<=mid)
return query(tree,p1,low,mid,l,r);
if(l>mid)
return query(tree,p2,mid+1,high,l,r);
list *a=query(tree,2*node+1,low,(high+low)/2,l,mid);
list *b=query(tree,2*node+2,(high+low)/2+1,high,mid+1,r);
list *n;
long w=a->sum+b->sum;
long x=max(max(a->bestsum,b->bestsum),a->right+b->left);
long y=max(a->left,a->sum+b->left);
long z=max(a->right+b->sum,b->right);
n=make_new_node(w,x,y,z);

return n;
}

int main()
{
freopen("GSS1_AC1.in", "r", stdin);
freopen("GSS1_AC1.out", "w", stdout);

//ios_base::sync_with_stdio(false);
int n;
scanf("%d",&n);
// cin>>n;
long arr[n];
list* tree[5*n];
for(int i=0;i<n;i++)
scanf("%ld",&arr[i]);
// cin>>arr[i];
build(arr,tree,0,0,n-1);

int q;
scanf("%d",&q);
for(int i=0;i<q;i++)
{
int l,r;
scanf("%d%d",&l,&r);
// cout<<query(tree,0,0,n-1,l-1,r-1)->bestsum<<endl;
printf("%ld\n",query(tree,0,0,n-1,l-1,r-1)->bestsum);
}
return 0;
}
115 changes: 115 additions & 0 deletions SPOJ/GSS1_AC/GSS1_AC2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Author : pritish.thakkar
*/

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define clr(a) memset(a,0LL,sizeof a);
#define gc getchar

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

//Fast Reader :
template<class T>inline bool read(T &x){int c=gc();int sgn=1;while(~c&&c<'0'||c>'9'){if(c=='-')sgn=-1;c=gc();}for(x=0;~c&&'0'<=c&&c<='9';c=gc())x=x*10+c-'0';x*=sgn;return ~c;}

//_____________________________

ll n;
const ll N = 5*(1e5);
ll a[N<<4];
struct node{
ll sum, maxSum, left, right;
};

node st[N<<4];

ll getMid(ll a , ll b){
return a + (b - a) / 2;
}

void fillVal(ll index){
ll L = index*2+1;
ll R = index*2+2;
st[index].sum = st[L].sum + st[R].sum;
st[index].left = max(st[L].left, st[L].sum + st[R].left);
st[index].right = max(st[R].right, st[R].sum + st[L].right);
st[index].maxSum = max(max(st[L].maxSum, st[R].maxSum), st[R].left + st[L].right);
}

void build(ll from , ll to, ll index){
if(from == to){
st[index].sum = st[index].maxSum = st[index].left = st[index].right = a[from];
return;
}

ll mid = getMid(from , to);
build(from, mid, index*2+1);
build(mid+1, to, index*2+2);
fillVal(index);
}

node emptyNode(){
node ret;
ret.left = ret.right = ret.sum = ret.maxSum = 0;
return ret;
}

node Combination(node a, node b){
node ret;
ret.sum = a.sum + b.sum;
ret.left = max(a.left, a.sum + b.left);
ret.right = max(b.right, b.sum + a.right);
ret.maxSum = max(max(a.maxSum, b.maxSum), b.left + a.right);
return ret;
}

node query(ll index, ll from , ll to, ll l, ll r){
if(r < from || l > to){
return emptyNode();
}
if(from >= l && to <= r){
return st[index];
}
ll mid = getMid(from, to);
if(r <= mid){
return query(index*2+1, from , mid, l, r);
}
if(l > mid){
return query(index*2+2, mid+1, to, l, r);
}
node LChild = query(index*2+1, from, mid, l, r);
node RChild = query(index*2+2, mid+1, to, l, r);
return Combination(LChild, RChild);
}

void solve(){
read(n);
ll i;
rep(i,n){
read(a[i]);
}
build(0,n-1, 0);
ll Q;
read(Q);
while(Q--){
ll l, r;
read(l);
read(r);
printf("%lld\n", query(0, 0, n-1, l-1, r-1).maxSum);
}
}
//___________________________

int main(int argc, char *argv[]){
freopen("GSS1_AC2.in", "r", stdin);
freopen("GSS1_AC2.out", "w", stdout);

std::ios::sync_with_stdio(0);
solve();
}
21 changes: 21 additions & 0 deletions SPOJ/GSS1_AC/GSS1_AC2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
90
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
9
1 4
3 5
5 9
1 40
2 50
5 90
10 40
50 90
30 50
9 changes: 9 additions & 0 deletions SPOJ/GSS1_AC/GSS1_AC2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
350
32
787
903
903
903
903
903
903
55 changes: 55 additions & 0 deletions SPOJ/GSS1_AC/QueriesIData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <bits/stdc++.h> //includes everything, supported in CF, usaco, not POJ
#include "MyRandom.h"
#include "Windows.h"

using namespace std;

int main()
{
const int FileCount = 100; //note:文件数量
ofstream fout;
random rdNum;

for (int fileId = 1; fileId <= FileCount; ++fileId)
{
//note:每个文件案例的个数
int caseCount = 1;

fout.open(to_string(fileId) + ".in");

for(int kk=1;kk<=caseCount;++kk)
{
//***************************
//在此处写入测试数据
//***************************
int N = rdNum.GetRand(50000, 50000);

fout << N << "\n";
for (int i = 1; i <= N; ++i)
{

fout << (long long)rdNum.GetRand(0, 100)-(long long)rdNum.GetRand(0, 100) << " ";
}
fout << "\n";
int M = rdNum.GetRand(1, 2000);
fout << M << "\n";
for (int i = 1; i <= M; ++i)
{
vector<int> rd = rdNum.GetUniqueRand(1,N);
int A=rd.front();
int B=rd.back();

if(A>B)
{
swap(A,B);
}

fout << A << " " << B <<"\n";
}
}

fout.close();
}

return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 684f046

Please sign in to comment.