2015
04-16

# Development Value

StarCraft 2 (SC2) is a famous game. More and more people fall in love with this game.

As a crazy fan of SC2, Ahua (flower fairy) play it day and night. Recently, he found that the most important part of being a top player of SC2 is economic development, which means you should get as much mine as possible by training SCVs (space construction vehicle) to collect mine. Train a SCV at ith second costs Ci units of mine. After training, this SCV can collect Di units of mine each second. Training a SCV needs one second of time.

Based on that, he composes a formula to evaluate the development in a time span from xth second to yth second. Assume at xth second, Ahua has no SCV and mine. He trains one SCV at each second during xth second and yth second (the mount of mine can be negative, so that he always can train SCV). Each SCV will collect some amount of mines for Ahua in each second after it was trained. At ith second Ahua has Mi units of mine in total. The development value is defined as sum(Mi) (x ≤ i ≤ y). Now he asks you to help him calculate the development value. To make it more interesting, Ahua can apply following operations:

Cost x y z: the cost of training a SCV between xth second to yth second will increase by z units of mine. i.e. Ci for x ≤ i ≤ y will increase by z.

Collect x y z: each SCV trained between xth second and yth second can collect z more mines every second after it has been trained. i.e. Di for x ≤ i ≤ y will increase by z.

Query x y: output the development value between xth second and yth second.

First line of the input is a single integer T (T ≤ 10), indicates there are T test cases.
For each test case, the first line is an integer N (1 ≤ N ≤ 100000), means the maximum time you should deal with.

Following N lines, each contain two integers Ci and Di (0 ≤ Ci, Di ≤ 100000), the cost and collect speed of SCV training in ith second initially as described above.

The next line is an integer Q (1 ≤ Q ≤ 10000), the number of operations you should deal with. Then Q lines followed, each line will be “Cost x y z”, "Collect x y z” or “Query x y”.
1 ≤ x ≤ y ≤ N, 0 ≤ z ≤ 100000

First line of the input is a single integer T (T ≤ 10), indicates there are T test cases.
For each test case, the first line is an integer N (1 ≤ N ≤ 100000), means the maximum time you should deal with.

Following N lines, each contain two integers Ci and Di (0 ≤ Ci, Di ≤ 100000), the cost and collect speed of SCV training in ith second initially as described above.

The next line is an integer Q (1 ≤ Q ≤ 10000), the number of operations you should deal with. Then Q lines followed, each line will be “Cost x y z”, "Collect x y z” or “Query x y”.
1 ≤ x ≤ y ≤ N, 0 ≤ z ≤ 100000

1
5
1 3
2 3
3 1
2 2
3 3
5
Query 1 3
Cost 2 2 1
Query 1 3
Collect 1 1 5
Query 1 3

Case 1:
2
0
15

Sure原创，转载请注明出处。

#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
const int inf = 1 << 29;
const int maxn = 1010;
const int maxe = 2100000;
struct node
{
int v,w;
int next;
}edge[maxe];
int B[maxn][maxn],C[maxn],D[maxn];
int n,s,t,idx,sum;

void init()
{
memset(D,0,sizeof(D));
scanf("%d",&n);
s = idx = sum = 0;
t = n + 1;
return;
}

{
edge[idx].v = v;
edge[idx].w = w;

edge[idx].v = u;
edge[idx].w = 0;
return;
}

{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&B[i][j]);
D[j] += B[i][j];
}
}
for(int i=1;i<=n;i++)
{
scanf("%d",&C[i]);
sum += D[i];
}
return;
}

int isap(int N)
{
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
for(int i=0;i<N;i++)
{
}
int i,top = s;
int maxflow = 0,flow = inf;
gap[s] = N;
while(dis[s] < N)
{
for(i=cur[top];i != -1;i=edge[i].next)
{
if(edge[i].w > 0 && dis[top] == dis[edge[i].v] + 1)
{
break;
}
}
if(i != -1)
{
cur[top] = i;
if(edge[i].w < flow)
{
flow = edge[i].w;
}
top = edge[i].v;
pre[top] = i;
if(top == t)
{
maxflow += flow;
while(top != s)
{
edge[pre[top]].w -= flow;
edge[pre[top]^1].w += flow;
top = edge[pre[top]^1].v;
}
flow = inf;
}
}
else
{
if(--gap[dis[top]] == 0)
{
break;
}
dis[top] = N;
{
if(edge[j].w > 0 && dis[top] > dis[edge[j].v] + 1)
{
cur[top] = j;
dis[top] = dis[edge[j].v] + 1;
}
}
gap[dis[top]]++;
if(top != s)
{
top = edge[pre[top]^1].v;
}
}
}
return maxflow;
}

int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
init();
printf("%d\n",sum - isap(t+1));
}
return 0;
}


,
1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. 学算法中的数据结构学到一定程度会乐此不疲的，比如其中的2－3树，类似的红黑树，我甚至可以自己写个逻辑文件系统结构来。