Codeforces Round #368 (Div. 2) [E] Garlands

来源:互联网 发布:二手车交易平台源码 编辑:程序博客网 时间:2024/05/02 01:19
Garlands
time limit per test:3 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Like all children, Alesha loves New Year celebration. During the celebration he and his whole family dress up the fir-tree. Like all children, Alesha likes to play with garlands — chains consisting of a lightbulbs.

Alesha uses a grid field sized n × m for playing. The rows of the field are numbered from1 to n from the top to the bottom and columns are numbered from1 to m from the left to the right.

Alesha has k garlands which he places at the field. He does so in the way such that each lightbulb of each garland lies in the center of some cell in the field, and each cell containsat most one lightbulb. Of course lightbulbs, which are neighbours in some garland, appears in cells neighbouring by a side.

The example of garland placing.

Each garland is turned off or turned on at any moment. If some garland is turned on then each of its lightbulbs is turned on, the same applies for garland turned off. Each lightbulb in the whole garland set is unique, and thus, being turned on, brings Alesha some pleasure, described by an integer value. Turned off lightbulbs don't bring Alesha any pleasure.

Alesha can turn garlands on and off and wants to know the sum of pleasure value which the lightbulbs, placed in the centers of the cells in some rectangular part of the field, bring him. Initiallyall the garlands are turned on.

Alesha is still very little and can't add big numbers. He extremely asks you to help him.

Input

The first line of the input contains three integers n,m and k (1 ≤ n, m, k ≤ 2000) — the number of field rows, the number of field columns and the number of garlands placed at the field respectively.

Next lines contains garlands set description in the following format:

The first line of a single garland description contains a single integer len (1 ≤ len ≤ 2000) — the number of lightbulbs in the garland.

Each of the next len lines contains three integersi, j andw (1 ≤ i ≤ n,1 ≤ j ≤ m, 1 ≤ w ≤ 109) — the coordinates of the cell containing a lightbullb and pleasure value Alesha gets from it if it is turned on. The lightbulbs are given in the order they are forming a chain in the garland. It is guaranteed that neighbouring lightbulbs are placed in the cells neighbouring by a side.

The next line contains single integer q (1 ≤ q ≤ 106) — the number of events in Alesha's game. The nextq lines describes events in chronological order. Thei-th of them describes the i-th event in the one of the following formats:

  • SWITCH i — Alesha turns offi-th garland if it is turned on, or turns it on if it is turned off. It is guaranteed that1 ≤ i ≤ k.
  • ASK x1y1 x2 y2 — Alesha wants to know the sum of pleasure values the lightbulbs, placed in a rectangular part of the field. Top-left cell of a part has coordinates (x1, y1) and right-bottom cell has coordinates(x2, y2). It is guaranteed that1 ≤ x1 ≤ x2 ≤ n and1 ≤ y1 ≤ y2 ≤ m. There isno more than 2000 events of this type in the input.

All the numbers in the input are integers.

Please note that the input is quite large, so be careful while using some input ways. In particular, it's not recommended to usecin in codes on C++ and class Scanner in codes on Java.

Output

For each ASK operation print the sum Alesha wants to know in a separate line. Print the answers in chronological order.

Examples
Input
4 4 351 1 21 2 32 2 12 1 43 1 741 3 12 3 32 4 31 4 174 1 14 2 93 2 83 3 34 3 44 4 13 4 12ASK 2 2 3 3ASK 1 1 4 4
Output
1552
Input
4 4 184 1 13 1 22 1 11 1 71 2 52 2 42 3 11 3 13ASK 1 1 3 2SWITCH 1ASK 1 1 3 2
Output
190
Note

This image illustrates the first sample case.



搞懂了其实很简单的一道题,反正我觉得比D题简单

首先搞一个二维树状数组,然后用last[i]记录第i个链在上一次询问之后的状态,flag[i]记录第i个链当前的状态

然后就可以乱搞了,我开始还很好奇为什么不超时,一看发现时限3秒,而且看数据出题人还没出什么坑爹数据

#include<cstdio>#include<iostream>#include<vector>#define lowbit(xx) (xx&(-xx))#define LL long longusing namespace std;const int maxn=2005,inf=1e9;inline void _read(int &x){    char t=getchar();bool sign=true;    while(t<'0'||t>'9')    {if(t=='-')sign=false;t=getchar();}    for(x=0;t>='0'&&t<='9';t=getchar())x=x*10+t-'0';    if(!sign)x=-x;}int n,m,k;LL c[maxn][maxn];vector<int>sx[maxn];vector<int>sy[maxn];vector<int>sw[maxn];bool flag[maxn],last[maxn];char ch[10];void modify(int x,int y,int w){int i,j;for(i=x;i<=n;i+=lowbit(i))    for(j=y;j<=m;j+=lowbit(j))        c[i][j]+=w;}LL getsum(int x,int y){LL sum=0;int i,j;for(i=x;i;i-=lowbit(i))    for(j=y;j;j-=lowbit(j))        sum+=c[i][j];return sum;}int main(){int i,j,len,x,y,a,b,w,q,p;_read(n);_read(m);_read(k);for(i=1;i<=k;i++){_read(len);last[i]=flag[i]=1;while(len--){_read(x);_read(y);_read(w);sx[i].push_back(x);sy[i].push_back(y);sw[i].push_back(w);modify(x,y,w);}}_read(p);for(i=1;i<=p;i++){scanf("%s",ch);if(ch[0]=='S'){_read(x);flag[x]^=1;}else {_read(x);_read(y);_read(a);_read(b);for(j=1;j<=k;j++){if(flag[j]==last[j])continue;if(flag[j]){for(q=0;q<sx[j].size();q++)    modify(sx[j][q],sy[j][q],sw[j][q]);last[j]=flag[j];}else {for(q=0;q<sx[j].size();q++)    modify(sx[j][q],sy[j][q],-sw[j][q]);last[j]=flag[j];}}cout<<getsum(a,b)+getsum(x-1,y-1)-getsum(a,y-1)-getsum(x-1,b)<<endl;}}}


0 0