Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct trip{
- int i,j,v;
- };
- bool cmp(trip i1, trip i2)
- {
- if(i1.i<i2.i)
- {
- return 1;
- }
- else
- {
- if(i1.i==i2.i&&i1.j<i2.j)
- {
- return 1;
- }
- }
- return 0;
- }
- void ins(trip X[], int &n, trip toIns)
- {
- int ok=0;
- X[n].i=-20;///Rezolva cazul cand ar fi cel mai mic element din sir
- n++;
- int it=0;
- while(X[it].i>toIns.i)
- it++;
- while(X[it].i==toIns.i&&X[it].j>toIns.j)
- it++;
- for(;it<n;it++)
- {
- std::swap(X[it],toIns);
- }
- }
- int partition (trip arr[], int low, int high)
- {
- trip pivot = arr[high]; // pivot
- int i = (low - 1); // Index of smaller element
- for (int j = low; j <= high - 1; j++)
- {
- // If current element is smaller than the pivot
- if (cmp(arr[j],pivot))
- {
- i++; // increment index of smaller element
- std::swap(arr[i], arr[j]);
- }
- }
- std::swap(arr[i + 1], arr[high]);
- return (i + 1);
- }
- /* The main function that implements QuickSort
- arr[] --> Array to be sorted,
- low --> Starting index,
- high --> Ending index */
- void quickSort(trip arr[], int low, int high)
- {
- if (low < high)
- {
- /* pi is partitioning index, arr[p] is now
- at right place */
- int pi = partition(arr, low, high);
- // Separately sort elements before
- // partition and after partition
- quickSort(arr, low, pi - 1);
- quickSort(arr, pi + 1, high);
- }
- }
- int getVal(trip X[],int i, int j)
- {
- int it=0;
- while(X[it].i>i)
- it++;
- while(X[it].i==i&&X[it].j>j)
- it++;
- if(X[it].i!=i||X[it].j!=j)
- return 0;
- else
- {
- return X[it].v;
- }
- }
- void readRare(trip X[],int &n)
- {
- n=0;
- while(1)
- {
- int x,y,v;
- std::cin>>x>>y>>v;
- if(x==-1 && y==-1 && v==-1)
- {
- quickSort(X,0,n-1);
- return;
- }
- else
- {
- X[n].i=x;
- X[n].j=y;
- X[n].v=v;
- n++;
- }
- }
- }
- void cpy(trip &a, trip b)
- {
- a.i=b.i;
- a.j=b.j;
- a.v=b.v;
- }
- void sumRare(trip A[],int a, trip B[],int b, trip C[], int &c)
- {
- c=0;
- int ia=0,ib=0;
- while(ia<a&&ib<b)
- {
- if(cmp(A[ia],B[ib]))//A[i]<B[ib]
- {
- cpy(C[c++],A[ia++]);
- }
- else if(cmp(B[ib],A[ia]))//B[ib]<A[ia]
- {
- cpy(C[c++],B[ib++]);
- }
- else//A[ia]==B[ib]
- {
- cpy(C[c],A[ia]);
- C[c].v+=B[ib].v;
- ia++,ib++,c++;
- }
- }
- while(ia<a)
- {
- cpy(C[c++],A[ia++]);
- }
- while(ib<b)
- {
- cpy(C[c++],B[ib++]);
- }
- }
- void prtRare(trip A[],int a,int n, int m )
- {
- int ai=0;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m;j++)
- {
- if((A[ai].i==i&&A[ai].j==j)&&ai<a)
- {
- std::cout<<A[ai].v;
- ai++;
- }
- else
- {
- std::cout<<"0";
- }
- std::cout<<" ";
- }
- std::cout<<"\n";
- }
- }
- int main() {
- trip A[200],B[200];
- int a,m,n,b;
- std::cin>>n>>m;
- readRare(A,a);
- readRare(B,b);
- trip C[200];
- int c;
- sumRare(A,a,B,b,C,c);
- prtRare(C,c,n,m);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement