Skip to main content

Posts

Showing posts from June, 2017

GSS1

                                                     GSS1                                                                 segment tree Problem Link #include <iostream> #include <stdio.h> using namespace std; struct node {     long bestSum, leftSum, rightSum, totalSum; }; int n; long t; node seg[200003]; long arr[50003]; node combine(node &lc, node &rc) {     node temp;     temp.totalSum = lc.totalSum + rc.totalSum;     temp.leftSum = max(lc.leftSum, lc.totalSum + rc.leftSum);     temp.rightSum = max(rc.rightSum, rc.totalSum + lc.rightSum);     temp.bestSum = max(lc.rightSum + rc.leftSum, max(lc.bestSum, rc.bestSum));     return temp; } void buildTree(int l, int r, int index) {     if(l == r)     {         seg[index].bestSum = seg[index].rightSum = seg[index].leftSum = seg[index].totalSum = arr[l];     }     else     {         int mid =  l + ((r - l) >> 1);         int lchild = (index &l

Sereja and Brackets

                                Sereja and Brackets                                   segment tree Problem Link #include <iostream> #include <cmath> #include <cstring> using namespace std; string s; struct node {     long int len; //max length of matched brackets     int tn, tp; // tp = total number of '(' brackets                 //tn = total number of ')' brackets }; node func(node a, node b) {     node temp;     int len = min(a.tp, b.tn);     temp.len = a.len + b.len + 2 * len;     temp.tp = a.tp + b.tp - len;     temp.tn = a.tn + b.tn - len;     return temp; } void buildTree(node seg[], int l, int r, int index) {     if(l == r)     {         seg[index].len = seg[index].tp = seg[index].tn = 0;         if(s[l] == ')')         {             seg[index].tn = 1;         }         else         {             seg[index].tp = 1;         }      }      el