1202: [HNOI2005] Cunning businessman time limit: ten Sec Memory Limit: 162 MB
Submit: 2573 Solved: 1209
[Submit] [Status] [Discuss] Description
刁姹 received a task to investigate a businessman's ledger for the tax department to see if the ledger was forged. The ledger records the income of n months, of which the amount of income for the first month is AI (i=1,2,3...n-1,n). When the AI is greater than 0, this month is the profit AI, when the AI is less than 0 represents this month loss of Ai Yuan. The so-called total revenue for a period of time is the sum of each month's income during this period. 刁姹 's mission was carried out in secret, and she had to run to the merchant to investigate the merchant's ledger. She peeked at the ledger while the businessman was away, but she could not steal the ledger, and every time she looked at the ledger she could only see the income recorded in the ledger for a certain period of time, and she could only remember the total revenue during that time. Now, Diao cha a total of peek at the M-Time ledger, of course, also remember the M period of total revenue, your task is to remember this information to determine whether the ledger is false.
Input
The first behavior is a positive integer w, where W < 100, which indicates that there is a W group of data, i.e. W Ledger, that you need to judge. The first behavior of each group of data is two positive integers n and M, where N < 100,m < 1000, respectively, indicate how many months of income the corresponding ledger records and how many times the ledger was peeped. The next M-line represents the M-piece of information that Diao Cha peeks into the M-Ledger, with each piece of information having three integers s,t and V, representing a total revenue of V from month s to T months (including T-month), where S is always less than or equal to T.
Output
Contains w lines, each line is true or false, where the I behavior is true when and only if the group I data, that is, the I ledger is not false; I act false when and only if the group I data, that is, the first ledger is false.
Sample Input2
3 3
1 2 10
1 3-5
3 3-15
5 3
1 5 100
3 5 50
1 2 51
Sample Outputtrue
FalseHINT
Source
[Submit] [Status] [Discuss] the puzzle: and check the set + prefix and
For each point I maintains a prefix and Sum[i], which represents the sum of the represented nodes of the I to the owning collection.
Each merge does not merge the L,r, but merges the l-1,r so that each interval is connected and the sum of SUM[R]-SUM[L-1] is [l,r].
For L-1,r, if the two endpoints are already in a collection, then directly determine whether SUM[R]-SUM[L-1] equals the current value of the information.
If we are not in a collection, then we merge the L-1,r, we merge the representative elements back into the collection representing the front of the element, assuming that the smaller representative element is R1, the endpoint is x, and the larger representative element is R2, and the endpoint is Y.
so we're going to change R2 's FA to R1,so how to get the changesR2the prefix and that isSR2- SR1it? Becauseythe original prefix and is Sy - SR2, whilexto Y(current information value)of this paragraph andR1to x(Sum[x])This section is known, so considerxand theythe size of the relationship, ifyin thexthe latter should be the two, or subtract(Drawing). This will get theynow the prefix and (that is, the sum of Y to R1), you can updateR2now the prefix and the. Noteythe prefix and does not need to be updated because the current point FA's sum value is updated at the time of the path compression to update the current point's, and at that timeyof theFAor isR2. It is also important to note that the update is using FA that was not previously merged, but we need to make sure that the old FA has been updated. So you need to find (Fa[x]), then update sum, and finally path compression, change FA.
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include < Cstring>using namespace Std;int n,m,t;int fa[120],sum[120];int find (int x) {if (fa[x]==x) return x; int T=find (fa[x]); SUM[X]+=SUM[FA[X]]; fa[x]=t; return fa[x];} int main () {scanf ("%d", &t); for (int j=1;j<=t;j++) {scanf ("%d%d", &n,&m); for (int i=1;i<=n;i++) fa[i]=i,sum[i]=0; BOOL Mark=true; for (int i=1;i<=m;i++) {int x, y, Z; scanf ("%d%d%d", &x,&y,&z); x--; int R1=find (x); int R2=find (y); if (R1==R2) {if (sum[y]-sum[x]!=z) {mark=false; Break }} else{if (R1>R2) swap (x, y), swap (R1,R2); FA[R2]=R1; int k; if (x>y) k=sum[x]-z; else k=sum[x]+z; Sum[r2]=k-sum[y]; }} if (Mark) printf ("true\n"); else printf ("false\n"); }}
Bzoj 1202: [HNOI2005] cunning businessman (and check set + prefix and)