#include<cstdio>
#define MAXN 150005
long long int cut[MAXN];
long long int height[MAXN];
struct Node
{
Node *parent;
Node *lchild;
Node *rchild;
long long int val;
long long int lazym;
int no;
Node *maxnode;
long long int maxv;
}node[MAXN];
inline void lazyupdate(Node *n)
{
if(n->lazym)
{
n->val-=n->lazym;
n->maxv-=n->lazym;
if(n->rchild) n->rchild->lazym+=n->lazym;
if(n->lchild) n->lchild->lazym+=n->lazym;
n->lazym=0;
}
}
void cutn(Node *n)
{
if(n->parent)
{
n->lazym+=n->val-n->parent->val;
cutn(n->parent);
}
else
{
n->lazym+=n->val;
}
lazyupdate(n);
return;
}
void upd(Node *n)
{
while(n)
{
n->maxnode=n;
n->maxv=n->val;
if(n->lchild)
{
lazyupdate(n->lchild);
if( (n->maxv) < (n->lchild->maxv) )
{
n->maxv=(n->lchild->maxv);
n->maxnode=n->lchild->maxnode;
}
}
if(n->rchild)
{
lazyupdate(n->rchild);
if( (n->maxv) < (n->rchild->maxv) )
{
n->maxv=(n->rchild->maxv);
n->maxnode=n->rchild->maxnode;
}
}
n=n->parent;
}
}
void setlr(Node *n,long long int l,long long int r)
{
n->lazym=0;
if(n->parent)
n->val=(n->parent->val)+(height[n->no]-height[n->parent->no])*(r-l);
else
n->val=height[n->no]*(r-l);
if(n->lchild)
setlr(n->lchild,l,cut[n->no]);
if(n->rchild)
setlr(n->rchild,cut[(n->no)+1],r);
n->maxnode=n;
n->maxv=n->val;
if(n->lchild)
if( (n->maxv) < (n->lchild->maxv) )
{
n->maxv=(n->lchild->maxv);
n->maxnode=n->lchild->maxnode;
}
if(n->rchild)
if( (n->maxv) < (n->rchild->maxv) )
{
n->maxv=(n->rchild->maxv);
n->maxnode=n->rchild->maxnode;
}
return;
}
int main()
{
int n,k;
scanf("%d",&n);
n=n/2-1;
scanf("%*lld%*lld");
for(int i=0;i<n;i++)
{
scanf("%*lld%*lld");
scanf("%lld%lld",&cut[i+1],&height[i]);
}
scanf("%*lld%*lld");
scanf("%d",&k);
node[0].parent=node[0].lchild=node[0].rchild=NULL;
node[0].no=0;
for(int i=1;i<n;i++)
{
//connexion of node[i-1] and node[i]
Node *con=&node[i-1];
node[i].parent=node[i].lchild=node[i].rchild=NULL;
node[i].no=i;
while(true)
{
if( height[con->no] < height[i] )
{
if(con->rchild)
{
node[i].lchild=con->rchild;
con->rchild->parent=&node[i];
}
con->rchild=&node[i];
node[i].parent=con;
break;
}
else
{
if(con->parent)
con=con->parent;
else
{
con->parent=&node[i];
node[i].lchild=con;
break;
}
}
}
}
Node *root;
for(int i=0;i<n;i++)
if(!node[i].parent)
{
root=&node[i];
break;
}
setlr(root,cut[0],cut[n]);
long long int ans=0;
for(int i=0;i<k;i++)
{
ans+=root->maxv;
cutn(root->maxnode);
upd(root->maxnode);
}
printf("%lld",ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
12804 KB |
Output is correct |
2 |
Correct |
0 ms |
12804 KB |
Output is correct |
3 |
Correct |
0 ms |
12804 KB |
Output is correct |
4 |
Correct |
0 ms |
12804 KB |
Output is correct |
5 |
Correct |
0 ms |
12804 KB |
Output is correct |
6 |
Correct |
0 ms |
12804 KB |
Output is correct |
7 |
Correct |
0 ms |
12804 KB |
Output is correct |
8 |
Correct |
0 ms |
12804 KB |
Output is correct |
9 |
Correct |
0 ms |
12804 KB |
Output is correct |
10 |
Correct |
0 ms |
12804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
12804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
12804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
76 ms |
12804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
88 ms |
12804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |