fork(1) download
  1. #include<iostream>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <vector>
  5. #include<bits/stdc++.h>
  6. #include <algorithm>
  7. using namespace std;
  8. struct array{
  9. int x;
  10. int y;
  11. };
  12. int compare(const void* l,const void* m)
  13. {
  14. struct array* e1=(array*) l;
  15. struct array* e2=(array*) m;
  16. int p=e1->y-e1->x+1;
  17. int q=e2->y-e2->x+1;
  18. return p-q;
  19. }
  20. int main()
  21. {
  22. int t;
  23. cin>>t;
  24. while(t--)
  25. {
  26. int n,k;
  27. cin>>n>>k;
  28. array arr[k];
  29. int p,q;
  30. for(int i=0;i<k;i++)
  31. {
  32. cin>>p>>q;
  33. arr[i].x=p;
  34. arr[i].y=q;
  35. }
  36. /*for(int i=0;i<k;i++)
  37. {
  38.  
  39. cout<<arr[i].x<<" ";
  40. cout<<arr[i].y<<endl;
  41. } */
  42. int a[n];
  43. memset(a,-1,sizeof(a));
  44. qsort(arr,k,sizeof(array),compare);
  45.  
  46. for(int i=0;i<k;i++)
  47. {
  48. int p=arr[i].x;
  49. int q=arr[i].y;
  50. int flag=0;
  51. while(p<=q)
  52. {
  53. if(a[p]==-1)
  54. {
  55. if(flag==0)
  56. {
  57. a[p]=0;
  58. flag=~flag;
  59. }
  60. else{
  61. a[p]=1;
  62. flag=~flag;
  63. }
  64.  
  65. }
  66. p++;
  67. }
  68. }
  69. int flag=0;
  70. for(int i=0;i<n;i++)
  71. {
  72.  
  73. if(a[i]==0)
  74. cout<<"(";
  75. else if(a[i]==1)
  76. cout<<")";
  77. else if(a[i]==-1)//cout<<arr[i].y<<endl;
  78. {
  79. if(flag==0)
  80. {
  81. flag=~flag;
  82. cout<<"(";
  83.  
  84. }
  85. else
  86. {
  87. flag=~flag;
  88. cout<<")";
  89. }
  90. }
  91.  
  92. }
  93. cout<<endl;
  94. }
  95. return 0;
  96. }
  97.  
  98.  
Success #stdin #stdout 0s 3416KB
stdin
2
4 1
0 3
4 2
0 3
1 2
stdout
()()
(())